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

com.scalar.db.sql.statement.builder.CreateIndexStatementBuilder Maven / Gradle / Ivy

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

import com.google.common.collect.ImmutableMap;
import com.scalar.db.sql.TableRef;
import com.scalar.db.sql.statement.CreateIndexStatement;
import java.util.Map;
import javax.annotation.Nullable;

public class CreateIndexStatementBuilder {

  private CreateIndexStatementBuilder() {}

  public static class Start extends OnTable {
    Start() {
      super(false);
    }

    /**
     * Specifies that the index should not be created if it already exists.
     *
     * @return a builder object
     */
    public OnTable ifNotExists() {
      return new OnTable(true);
    }

    /**
     * Specifies whether the index should not be created if it already exists.
     *
     * @param ifNotExists whether the index should not be created if it already exists
     * @return a builder object
     */
    public OnTable ifNotExists(boolean ifNotExists) {
      return new OnTable(ifNotExists);
    }
  }

  public static class OnTable {
    private final boolean ifNotExists;

    private OnTable(boolean ifNotExists) {
      this.ifNotExists = ifNotExists;
    }

    /**
     * Specifies which table this index is on.
     *
     * @param namespaceName a namespace name of the target table
     * @param tableName a table name of the target table
     * @return a builder object
     */
    public Column onTable(@Nullable String namespaceName, String tableName) {
      return new Column(namespaceName, tableName, ifNotExists);
    }

    /**
     * Specifies which table this index is on.
     *
     * @param tableName a table name of the target table
     * @return a builder object
     */
    public Column onTable(String tableName) {
      return onTable(null, tableName);
    }
  }

  public static class Column {
    @Nullable private final String namespaceName;
    private final String tableName;
    private final boolean ifNotExists;

    private Column(@Nullable String namespaceName, String tableName, boolean ifNotExists) {
      this.namespaceName = namespaceName;
      this.tableName = tableName;
      this.ifNotExists = ifNotExists;
    }

    /**
     * Specifies the column to create the index on.
     *
     * @param columnName a column name of the target column
     * @return a builder object
     */
    public Buildable column(String columnName) {
      return new Buildable(namespaceName, tableName, columnName, ifNotExists);
    }
  }

  public static class Buildable {
    @Nullable private final String namespaceName;
    private final String tableName;
    private final String columnName;
    private final boolean ifNotExists;
    private final ImmutableMap.Builder optionsBuilder = ImmutableMap.builder();

    private Buildable(
        @Nullable String namespaceName, String tableName, String columnName, boolean ifNotExists) {
      this.namespaceName = namespaceName;
      this.tableName = tableName;
      this.columnName = columnName;
      this.ifNotExists = ifNotExists;
    }

    /**
     * Adds a creation option.
     *
     * @param name an option name to add
     * @param value an option value to add
     * @return a builder object
     */
    public Buildable withOption(String name, String value) {
      optionsBuilder.put(name, value);
      return this;
    }

    /**
     * Adds creation options.
     *
     * @param options options to add
     * @return a builder object
     */
    public Buildable withOptions(Map options) {
      optionsBuilder.putAll(options);
      return this;
    }

    /**
     * Builds a CreateIndexStatement object.
     *
     * @return a CreateIndexStatement object
     */
    public CreateIndexStatement build() {
      return CreateIndexStatement.create(
          TableRef.of(namespaceName, tableName), columnName, ifNotExists, optionsBuilder.build());
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy