Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.scalar.db.sql.statement.builder;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.scalar.db.sql.ClusteringOrder;
import com.scalar.db.sql.DataType;
import com.scalar.db.sql.TableRef;
import com.scalar.db.sql.statement.CreateTableStatement;
import java.util.Map;
import javax.annotation.Nullable;
public class CreateTableStatementBuilder {
private CreateTableStatementBuilder() {}
public static class Start extends WithPartitionKey {
Start(@Nullable String namespaceName, String tableName) {
super(namespaceName, tableName, false);
}
/**
* Specifies that the table should not be created if it already exists.
*
* @return a builder object
*/
public WithPartitionKey ifNotExists() {
return new WithPartitionKey(namespaceName, tableName, true);
}
/**
* Specifies whether the table should not be created if it already exists.
*
* @param ifNotExists whether the table should not be created if it already exists
* @return a builder object
*/
public WithPartitionKey ifNotExists(boolean ifNotExists) {
return new WithPartitionKey(namespaceName, tableName, ifNotExists);
}
}
public static class WithPartitionKey {
protected final @Nullable String namespaceName;
protected final String tableName;
private final boolean ifNotExists;
private WithPartitionKey(
@Nullable String namespaceName, String tableName, boolean ifNotExists) {
this.namespaceName = namespaceName;
this.tableName = tableName;
this.ifNotExists = ifNotExists;
}
/**
* Adds a partition key column definition. This includes the column declaration (you don't need
* an additional {@link Buildable#withColumn(String, DataType)}. Partition key columns are added
* in the order of their declaration.
*
* @param columnName the column name of the partition key column
* @param dataType the data type of the partition key column
* @return a builder object
*/
public Buildable withPartitionKey(String columnName, DataType dataType) {
ImmutableList.Builder columnsBuilder =
ImmutableList.builder();
columnsBuilder.add(CreateTableStatement.ColumnDef.create(columnName, dataType, false));
ImmutableSet.Builder partitionKeyColumnNamesBuilder = ImmutableSet.builder();
partitionKeyColumnNamesBuilder.add(columnName);
return new Buildable(
namespaceName, tableName, ifNotExists, columnsBuilder, partitionKeyColumnNamesBuilder);
}
}
public static class Buildable {
@Nullable private final String namespaceName;
private final String tableName;
private final boolean ifNotExists;
private final ImmutableList.Builder columnsBuilder;
private final ImmutableSet.Builder partitionKeyColumnNamesBuilder;
private final ImmutableSet.Builder clusteringKeyColumnNamesBuilder =
ImmutableSet.builder();
private final ImmutableMap.Builder clusteringOrdersBuilder =
ImmutableMap.builder();
private final ImmutableMap.Builder optionsBuilder = ImmutableMap.builder();
private Buildable(
@Nullable String namespaceName,
String tableName,
boolean ifNotExists,
ImmutableList.Builder columnsBuilder,
ImmutableSet.Builder partitionKeyColumnNamesBuilder) {
this.namespaceName = namespaceName;
this.tableName = tableName;
this.ifNotExists = ifNotExists;
this.columnsBuilder = columnsBuilder;
this.partitionKeyColumnNamesBuilder = partitionKeyColumnNamesBuilder;
}
/**
* Adds a partition key column definition. This includes the column declaration (you don't need
* an additional {@link #withColumn(String, DataType)}). Partition key columns are added in the
* order of their declaration.
*
* @param columnName the column name of the partition key column
* @param dataType the data type of the partition key column
* @return a builder object
*/
public Buildable withPartitionKey(String columnName, DataType dataType) {
columnsBuilder.add(CreateTableStatement.ColumnDef.create(columnName, dataType, false));
partitionKeyColumnNamesBuilder.add(columnName);
return this;
}
/**
* Adds a clustering key column definition. This includes the column declaration (you don't need
* an additional {@link #withColumn(String, DataType)}). Clustering key columns are added in the
* order of their declaration.
*
* @param columnName the column name of the clustering key column
* @param dataType the data type of the clustering key column
* @return a builder object
*/
public Buildable withClusteringKey(String columnName, DataType dataType) {
columnsBuilder.add(CreateTableStatement.ColumnDef.create(columnName, dataType, false));
clusteringKeyColumnNamesBuilder.add(columnName);
return this;
}
/**
* Specifies clustering order for the specified clustering key column.
*
* @param columnName the column name of the clustering key column
* @param clusteringOrder the clustering order of the specified clustering key column
* @return a builder object
*/
public Buildable withClusteringOrder(String columnName, ClusteringOrder clusteringOrder) {
if (!clusteringKeyColumnNamesBuilder.build().contains(columnName)) {
throw new IllegalArgumentException(columnName + " is not a clustering key column");
}
clusteringOrdersBuilder.put(columnName, clusteringOrder);
return this;
}
/**
* Adds a column definition.
*
* @param columnName a column name to add
* @param dataType a data type to add
* @return a builder object
*/
public Buildable withColumn(String columnName, DataType dataType) {
return withColumn(columnName, dataType, false);
}
/**
* Adds a column definition.
*
* @param columnName a column name to add
* @param dataType a data type to add
* @param encrypted whether the column should be encrypted
* @return a builder object
*/
public Buildable withColumn(String columnName, DataType dataType, boolean encrypted) {
columnsBuilder.add(CreateTableStatement.ColumnDef.create(columnName, dataType, encrypted));
return this;
}
/**
* 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 CreateTableStatement object.
*
* @return a CreateTableStatement object
*/
public CreateTableStatement build() {
return CreateTableStatement.create(
TableRef.of(namespaceName, tableName),
ifNotExists,
columnsBuilder.build(),
partitionKeyColumnNamesBuilder.build(),
clusteringKeyColumnNamesBuilder.build(),
clusteringOrdersBuilder.build(),
optionsBuilder.build());
}
}
}