com.scalar.db.sql.statement.CreateIndexStatement Maven / Gradle / Ivy
package com.scalar.db.sql.statement;
import com.google.common.collect.ImmutableMap;
import com.scalar.db.sql.TableRef;
import java.util.Objects;
import javax.annotation.concurrent.Immutable;
@Immutable
public class CreateIndexStatement
implements DdlStatement, NamespaceNameOmittable {
public final TableRef table;
public final String columnName;
public final boolean ifNotExists;
public final ImmutableMap options;
private CreateIndexStatement(
TableRef table,
String columnName,
boolean ifNotExists,
ImmutableMap options) {
this.table = Objects.requireNonNull(table);
this.columnName = Objects.requireNonNull(columnName);
this.ifNotExists = ifNotExists;
this.options = Objects.requireNonNull(options);
}
@Override
public String toSql() {
StringBuilder builder = new StringBuilder("CREATE INDEX ");
if (ifNotExists) {
builder.append("IF NOT EXISTS ");
}
builder.append("ON ");
StatementUtils.appendTable(builder, table);
builder.append('(');
StatementUtils.appendObjectName(builder, columnName);
builder.append(')');
if (!options.isEmpty()) {
builder.append(" WITH ");
StatementUtils.appendOptions(builder, options);
}
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 CreateIndexStatement setNamespaceNameIfOmitted(String namespaceName) {
if (namespaceNameOmitted()) {
return create(TableRef.of(namespaceName, table.tableName), columnName, ifNotExists, options);
}
return this;
}
@Override
public String toString() {
return toSql();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof CreateIndexStatement)) {
return false;
}
CreateIndexStatement that = (CreateIndexStatement) o;
return ifNotExists == that.ifNotExists
&& Objects.equals(table, that.table)
&& Objects.equals(columnName, that.columnName)
&& Objects.equals(options, that.options);
}
@Override
public int hashCode() {
return Objects.hash(table, columnName, ifNotExists, options);
}
public static CreateIndexStatement create(
TableRef table,
String columnName,
boolean ifNotExists,
ImmutableMap options) {
return new CreateIndexStatement(table, columnName, ifNotExists, options);
}
}