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

com.datastax.driver.core.schemabuilder.AbstractCreateStatement 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.driver.core.schemabuilder;

import com.datastax.driver.core.DataType;
import com.google.common.base.Optional;
import java.util.LinkedHashMap;
import java.util.Map;

public abstract class AbstractCreateStatement>
    extends SchemaStatement {

  protected Optional keyspaceName = Optional.absent();
  protected boolean ifNotExists;
  protected Map simpleColumns = new LinkedHashMap();

  @SuppressWarnings("unchecked")
  private T self = (T) this;

  /**
   * Add the 'IF NOT EXISTS' condition to this CREATE statement.
   *
   * @return this CREATE statement.
   */
  public T ifNotExists() {
    this.ifNotExists = true;
    return self;
  }

  /**
   * Add a column definition to this CREATE statement.
   *
   * 

* *

To add a list column: * *


   * addColumn("myList",DataType.list(DataType.text()))
   * 
* *

To add a set column: * *


   * addColumn("mySet",DataType.set(DataType.text()))
   * 
* *

To add a map column: * *


   * addColumn("myMap",DataType.map(DataType.cint(),DataType.text()))
   * 
* * @param columnName the name of the column to be added. * @param dataType the data type of the column to be added. * @return this CREATE statement. */ public T addColumn(String columnName, DataType dataType) { validateNotEmpty(columnName, "Column name"); validateNotNull(dataType, "Column type"); validateNotKeyWord( columnName, String.format( "The column name '%s' is not allowed because it is a reserved keyword", columnName)); simpleColumns.put(columnName, new NativeColumnType(dataType)); return self; } /** * Add a column definition to this CREATE statement, when the type contains a UDT. * * @param columnName the name of the column to be added. * @param udtType the UDT type of the column to be added. Use {@link SchemaBuilder#frozen(String)} * or {@link SchemaBuilder#udtLiteral(String)}. * @return this CREATE statement. */ public T addUDTColumn(String columnName, UDTType udtType) { validateNotEmpty(columnName, "Column name"); validateNotNull(udtType, "Column type"); validateNotKeyWord( columnName, String.format( "The column name '%s' is not allowed because it is a reserved keyword", columnName)); simpleColumns.put(columnName, udtType); return self; } /** * Shorthand to add a column definition to this CREATE statement, when the type is a list of UDT. * * @param columnName the name of the column to be added * @param udtType the udt type of the column to be added. Use {@link * SchemaBuilder#frozen(String)}. * @return this CREATE statement. */ public T addUDTListColumn(String columnName, UDTType udtType) { validateNotEmpty(columnName, "Column name"); validateNotNull(udtType, "Column element type"); validateNotKeyWord( columnName, String.format( "The column name '%s' is not allowed because it is a reserved keyword", columnName)); simpleColumns.put(columnName, UDTType.list(udtType)); return self; } /** * Shorthand to add a column definition to this CREATE statement, when the type is a set of UDT. * * @param columnName the name of the column to be added * @param udtType the udt type of the column to be added. Use {@link * SchemaBuilder#frozen(String)}. * @return this CREATE statement. */ public T addUDTSetColumn(String columnName, UDTType udtType) { validateNotEmpty(columnName, "Column name"); validateNotNull(udtType, "Column element type"); validateNotKeyWord( columnName, String.format( "The column name '%s' is not allowed because it is a reserved keyword", columnName)); simpleColumns.put(columnName, UDTType.set(udtType)); return self; } /** * Shorthand to add a column definition to this CREATE statement, when the type is a map with a * UDT value type. * *

Example: * *

   *     addUDTMapColumn("addresses", DataType.text(), frozen("address"));
   * 
* * @param columnName the name of the column to be added. * @param keyType the key type of the column to be added. * @param valueUdtType the value UDT type of the column to be added. Use {@link * SchemaBuilder#frozen(String)}. * @return this CREATE statement. */ public T addUDTMapColumn(String columnName, DataType keyType, UDTType valueUdtType) { validateNotEmpty(columnName, "Column name"); validateNotNull(keyType, "Map key type"); validateNotNull(valueUdtType, "Map value UDT type"); validateNotKeyWord( columnName, String.format( "The column name '%s' is not allowed because it is a reserved keyword", columnName)); simpleColumns.put(columnName, UDTType.mapWithUDTValue(keyType, valueUdtType)); return self; } /** * Shorthand to add a column definition to this CREATE statement, when the type is a map with a * UDT key type. * *

Example: * *

   *     addUDTMapColumn("roles", frozen("user"), DataType.text());
   * 
* * @param columnName the name of the column to be added. * @param udtKeyType the key UDT type of the column to be added. Use {@link * SchemaBuilder#frozen(String)}. * @param valueType the value raw type of the column to be added. * @return this CREATE statement. */ public T addUDTMapColumn(String columnName, UDTType udtKeyType, DataType valueType) { validateNotEmpty(columnName, "Column name"); validateNotNull(udtKeyType, "Map key UDT type"); validateNotNull(valueType, "Map value type"); validateNotKeyWord( columnName, String.format( "The column name '%s' is not allowed because it is a reserved keyword", columnName)); simpleColumns.put(columnName, UDTType.mapWithUDTKey(udtKeyType, valueType)); return self; } /** * Shorthand to add a column definition to this CREATE statement, when the type is a map with UDT * key and value types. * *

Example: * *

   *     addUDTMapColumn("users", frozen("user"), frozen("address"));
   * 
* * @param columnName the name of the column to be added. * @param udtKeyType the key UDT type of the column to be added. Use {@link * SchemaBuilder#frozen(String)}. * @param udtValueType the value UDT type of the column to be added. Use {@link * SchemaBuilder#frozen(String)}. * @return this CREATE statement. */ public T addUDTMapColumn(String columnName, UDTType udtKeyType, UDTType udtValueType) { validateNotEmpty(columnName, "Column name"); validateNotNull(udtKeyType, "Map key UDT type"); validateNotNull(udtValueType, "Map value UDT type"); validateNotKeyWord( columnName, String.format( "The column name '%s' is not allowed because it is a reserved keyword", columnName)); simpleColumns.put(columnName, UDTType.mapWithUDTKeyAndValue(udtKeyType, udtValueType)); return self; } protected String buildColumnType(Map.Entry entry) { final ColumnType columnType = entry.getValue(); return entry.getKey() + " " + columnType.asCQLString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy