Please wait. This can take some minutes ...
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.
com.datastax.dse.driver.internal.querybuilder.schema.DefaultCreateDseTable Maven / Gradle / Ivy
/*
* Copyright DataStax, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datastax.dse.driver.internal.querybuilder.schema;
import static com.datastax.oss.driver.internal.querybuilder.schema.Utils.appendSet;
import com.datastax.dse.driver.api.querybuilder.schema.CreateDseTable;
import com.datastax.dse.driver.api.querybuilder.schema.CreateDseTableStart;
import com.datastax.dse.driver.api.querybuilder.schema.CreateDseTableWithOptions;
import com.datastax.dse.driver.api.querybuilder.schema.DseGraphEdgeSide;
import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder;
import com.datastax.oss.driver.api.core.type.DataType;
import com.datastax.oss.driver.internal.querybuilder.CqlHelper;
import com.datastax.oss.driver.internal.querybuilder.ImmutableCollections;
import com.datastax.oss.driver.internal.querybuilder.schema.OptionsUtils;
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap;
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.Map;
import net.jcip.annotations.Immutable;
@Immutable
public class DefaultCreateDseTable
implements CreateDseTableStart, CreateDseTable, CreateDseTableWithOptions {
private final CqlIdentifier keyspace;
private final CqlIdentifier tableName;
private final boolean ifNotExists;
private final boolean compactStorage;
private final ImmutableMap options;
private final ImmutableMap columnsInOrder;
private final ImmutableSet partitionKeyColumns;
private final ImmutableSet clusteringKeyColumns;
private final ImmutableSet staticColumns;
private final ImmutableSet regularColumns;
private final ImmutableMap orderings;
private final DseTableVertexOperation vertexOperation;
private final DseTableEdgeOperation edgeOperation;
public DefaultCreateDseTable(@Nullable CqlIdentifier keyspace, @NonNull CqlIdentifier tableName) {
this(
keyspace,
tableName,
false,
false,
ImmutableMap.of(),
ImmutableSet.of(),
ImmutableSet.of(),
ImmutableSet.of(),
ImmutableSet.of(),
ImmutableMap.of(),
null,
null,
ImmutableMap.of());
}
public DefaultCreateDseTable(
@Nullable CqlIdentifier keyspace,
@NonNull CqlIdentifier tableName,
boolean ifNotExists,
boolean compactStorage,
@NonNull ImmutableMap columnsInOrder,
@NonNull ImmutableSet partitionKeyColumns,
@NonNull ImmutableSet clusteringKeyColumns,
@NonNull ImmutableSet staticColumns,
@NonNull ImmutableSet regularColumns,
@NonNull ImmutableMap orderings,
@Nullable DseTableVertexOperation vertexOperation,
@Nullable DseTableEdgeOperation edgeOperation,
@NonNull ImmutableMap options) {
this.keyspace = keyspace;
this.tableName = tableName;
this.ifNotExists = ifNotExists;
this.compactStorage = compactStorage;
this.columnsInOrder = columnsInOrder;
this.partitionKeyColumns = partitionKeyColumns;
this.clusteringKeyColumns = clusteringKeyColumns;
this.staticColumns = staticColumns;
this.regularColumns = regularColumns;
this.orderings = orderings;
this.options = options;
this.vertexOperation = vertexOperation;
this.edgeOperation = edgeOperation;
}
@NonNull
@Override
public CreateDseTableStart ifNotExists() {
return new DefaultCreateDseTable(
keyspace,
tableName,
true,
compactStorage,
columnsInOrder,
partitionKeyColumns,
clusteringKeyColumns,
staticColumns,
regularColumns,
orderings,
vertexOperation,
edgeOperation,
options);
}
@NonNull
@Override
public CreateDseTable withPartitionKey(
@NonNull CqlIdentifier columnName, @NonNull DataType dataType) {
return new DefaultCreateDseTable(
keyspace,
tableName,
ifNotExists,
compactStorage,
ImmutableCollections.append(columnsInOrder, columnName, dataType),
appendSet(partitionKeyColumns, columnName),
clusteringKeyColumns,
staticColumns,
regularColumns,
orderings,
vertexOperation,
edgeOperation,
options);
}
@NonNull
@Override
public CreateDseTable withClusteringColumn(
@NonNull CqlIdentifier columnName, @NonNull DataType dataType) {
return new DefaultCreateDseTable(
keyspace,
tableName,
ifNotExists,
compactStorage,
ImmutableCollections.append(columnsInOrder, columnName, dataType),
partitionKeyColumns,
appendSet(clusteringKeyColumns, columnName),
staticColumns,
regularColumns,
orderings,
vertexOperation,
edgeOperation,
options);
}
@NonNull
@Override
public CreateDseTable withColumn(@NonNull CqlIdentifier columnName, @NonNull DataType dataType) {
return new DefaultCreateDseTable(
keyspace,
tableName,
ifNotExists,
compactStorage,
ImmutableCollections.append(columnsInOrder, columnName, dataType),
partitionKeyColumns,
clusteringKeyColumns,
staticColumns,
appendSet(regularColumns, columnName),
orderings,
vertexOperation,
edgeOperation,
options);
}
@NonNull
@Override
public CreateDseTable withStaticColumn(
@NonNull CqlIdentifier columnName, @NonNull DataType dataType) {
return new DefaultCreateDseTable(
keyspace,
tableName,
ifNotExists,
compactStorage,
ImmutableCollections.append(columnsInOrder, columnName, dataType),
partitionKeyColumns,
clusteringKeyColumns,
appendSet(staticColumns, columnName),
regularColumns,
orderings,
vertexOperation,
edgeOperation,
options);
}
@NonNull
@Override
public CreateDseTableWithOptions withCompactStorage() {
return new DefaultCreateDseTable(
keyspace,
tableName,
ifNotExists,
true,
columnsInOrder,
partitionKeyColumns,
clusteringKeyColumns,
staticColumns,
regularColumns,
orderings,
vertexOperation,
edgeOperation,
options);
}
@NonNull
@Override
public CreateDseTableWithOptions withClusteringOrderByIds(
@NonNull Map orderings) {
return withClusteringOrders(ImmutableCollections.concat(this.orderings, orderings));
}
@NonNull
@Override
public CreateDseTableWithOptions withClusteringOrder(
@NonNull CqlIdentifier columnName, @NonNull ClusteringOrder order) {
return withClusteringOrders(ImmutableCollections.append(orderings, columnName, order));
}
@NonNull
public CreateDseTableWithOptions withClusteringOrders(
@NonNull ImmutableMap orderings) {
return new DefaultCreateDseTable(
keyspace,
tableName,
ifNotExists,
compactStorage,
columnsInOrder,
partitionKeyColumns,
clusteringKeyColumns,
staticColumns,
regularColumns,
orderings,
vertexOperation,
edgeOperation,
options);
}
@NonNull
@Override
public CreateDseTableWithOptions withVertexLabel(@Nullable CqlIdentifier vertexLabelId) {
return new DefaultCreateDseTable(
keyspace,
tableName,
ifNotExists,
compactStorage,
columnsInOrder,
partitionKeyColumns,
clusteringKeyColumns,
staticColumns,
regularColumns,
orderings,
new DseTableVertexOperation(DseTableGraphOperationType.WITH, vertexLabelId),
edgeOperation,
options);
}
@NonNull
@Override
public CreateDseTableWithOptions withEdgeLabel(
@Nullable CqlIdentifier edgeLabelId,
@NonNull DseGraphEdgeSide from,
@NonNull DseGraphEdgeSide to) {
return new DefaultCreateDseTable(
keyspace,
tableName,
ifNotExists,
compactStorage,
columnsInOrder,
partitionKeyColumns,
clusteringKeyColumns,
staticColumns,
regularColumns,
orderings,
vertexOperation,
new DseTableEdgeOperation(DseTableGraphOperationType.WITH, edgeLabelId, from, to),
options);
}
@NonNull
@Override
public CreateDseTable withOption(@NonNull String name, @NonNull Object value) {
return new DefaultCreateDseTable(
keyspace,
tableName,
ifNotExists,
compactStorage,
columnsInOrder,
partitionKeyColumns,
clusteringKeyColumns,
staticColumns,
regularColumns,
orderings,
vertexOperation,
edgeOperation,
ImmutableCollections.append(options, name, value));
}
@NonNull
@Override
public String asCql() {
StringBuilder builder = new StringBuilder();
builder.append("CREATE TABLE ");
if (ifNotExists) {
builder.append("IF NOT EXISTS ");
}
CqlHelper.qualify(keyspace, tableName, builder);
if (columnsInOrder.isEmpty()) {
// no columns provided yet.
return builder.toString();
}
boolean singlePrimaryKey = partitionKeyColumns.size() == 1 && clusteringKeyColumns.size() == 0;
builder.append(" (");
boolean first = true;
for (Map.Entry column : columnsInOrder.entrySet()) {
if (first) {
first = false;
} else {
builder.append(',');
}
builder
.append(column.getKey().asCql(true))
.append(' ')
.append(column.getValue().asCql(true, true));
if (singlePrimaryKey && partitionKeyColumns.contains(column.getKey())) {
builder.append(" PRIMARY KEY");
} else if (staticColumns.contains(column.getKey())) {
builder.append(" STATIC");
}
}
if (!singlePrimaryKey) {
builder.append(",");
CqlHelper.buildPrimaryKey(partitionKeyColumns, clusteringKeyColumns, builder);
}
builder.append(')');
boolean firstOption = true;
if (compactStorage) {
firstOption = false;
builder.append(" WITH COMPACT STORAGE");
}
if (!orderings.isEmpty()) {
if (firstOption) {
builder.append(" WITH ");
firstOption = false;
} else {
builder.append(" AND ");
}
builder.append("CLUSTERING ORDER BY (");
boolean firstClustering = true;
for (Map.Entry ordering : orderings.entrySet()) {
if (firstClustering) {
firstClustering = false;
} else {
builder.append(',');
}
builder
.append(ordering.getKey().asCql(true))
.append(' ')
.append(ordering.getValue().toString());
}
builder.append(')');
}
if (vertexOperation != null) {
if (firstOption) {
builder.append(" WITH ");
firstOption = false;
} else {
builder.append(" AND ");
}
vertexOperation.append(builder);
} else if (edgeOperation != null) {
if (firstOption) {
builder.append(" WITH ");
firstOption = false;
} else {
builder.append(" AND ");
}
edgeOperation.append(builder);
}
builder.append(OptionsUtils.buildOptions(options, firstOption));
return builder.toString();
}
@Override
public String toString() {
return asCql();
}
@NonNull
@Override
public Map getOptions() {
return options;
}
@Nullable
public CqlIdentifier getKeyspace() {
return keyspace;
}
@NonNull
public CqlIdentifier getTable() {
return tableName;
}
public boolean isIfNotExists() {
return ifNotExists;
}
public boolean isCompactStorage() {
return compactStorage;
}
@NonNull
public ImmutableMap getColumnsInOrder() {
return columnsInOrder;
}
@NonNull
public ImmutableSet getPartitionKeyColumns() {
return partitionKeyColumns;
}
@NonNull
public ImmutableSet getClusteringKeyColumns() {
return clusteringKeyColumns;
}
@NonNull
public ImmutableSet getStaticColumns() {
return staticColumns;
}
@NonNull
public ImmutableSet getRegularColumns() {
return regularColumns;
}
@NonNull
public ImmutableMap getOrderings() {
return orderings;
}
}