All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scalar.db.sql.statement.AlterTableAddColumnStatement Maven / Gradle / Ivy

There is a newer version: 3.14.0
Show newest version
package com.scalar.db.sql.statement;

import com.scalar.db.sql.DataType;
import com.scalar.db.sql.TableRef;
import java.util.Objects;
import javax.annotation.concurrent.Immutable;

@Immutable
public class AlterTableAddColumnStatement
    implements DdlStatement, NamespaceNameOmittable {

  public final TableRef table;
  public final String columnName;
  public final DataType columnDataType;
  public final boolean encrypted;

  private AlterTableAddColumnStatement(
      TableRef table, String columnName, DataType columnDataType, boolean encrypted) {
    this.table = Objects.requireNonNull(table);
    this.columnName = Objects.requireNonNull(columnName);
    this.columnDataType = Objects.requireNonNull(columnDataType);
    this.encrypted = encrypted;
  }

  @Override
  public String toSql() {
    StringBuilder builder = new StringBuilder("ALTER TABLE ");
    StatementUtils.appendTable(builder, table);
    builder.append(" ADD COLUMN ");
    StatementUtils.appendObjectName(builder, columnName);
    builder.append(" ").append(columnDataType);
    if (encrypted) {
      builder.append(" ENCRYPTED");
    }
    return builder.toString();
  }

  @Override
  public  R accept(StatementVisitor visitor, C context) {
    return visitor.visit(this, context);
  }

  @Override
  public  R accept(DdlStatementVisitor visitor, C context) {
    return visitor.visit(this, context);
  }

  @Override
  public boolean namespaceNameOmitted() {
    return table.namespaceName == null;
  }

  @Override
  public AlterTableAddColumnStatement setNamespaceNameIfOmitted(String namespaceName) {
    if (namespaceNameOmitted()) {
      return create(
          TableRef.of(namespaceName, table.tableName), columnName, columnDataType, encrypted);
    }
    return this;
  }

  @Override
  public String toString() {
    return toSql();
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (!(o instanceof AlterTableAddColumnStatement)) {
      return false;
    }
    AlterTableAddColumnStatement that = (AlterTableAddColumnStatement) o;
    return Objects.equals(table, that.table)
        && Objects.equals(columnName, that.columnName)
        && columnDataType == that.columnDataType;
  }

  @Override
  public int hashCode() {
    return Objects.hash(table, columnName, columnDataType);
  }

  public static AlterTableAddColumnStatement create(
      TableRef table, String columnName, DataType columnDataType, boolean encrypted) {
    return new AlterTableAddColumnStatement(table, columnName, columnDataType, encrypted);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy