/** * Adds multiple columns * * @param names the column names * @param types the column CQL3 types * @return the current {@link com.englishtown.vertx.cassandra.tablebuilder.BuiltTableStatement} */ public CreateTable columns(String[] names, String[] types) { if (names.length != types.length) { throw new IllegalArgumentException(String.format("Got %d names but %d types", names.length, types.length)); } for (int i = 0; i < names.length; i++) { column(names[i], types[i]); } return this; }
/** * {@inheritDoc} */ @Override public String toString() { return getQueryString(); }
/** * Returns a {@link com.englishtown.vertx.cassandra.tablebuilder.CreateTable} builder * * @param keyspace the keyspace for the table to create * @param table the table name * @return the create table builder */ public static CreateTable create(String keyspace, String table) { return new CreateTable(keyspace, table); }
@Test public void testCreateTable() throws Exception { CreateTable table = TableBuilder.create("test_keyspace", "test_table") .ifNotExists() .column("col1", "text") .column("col2", "int") .staticColumn("col3", "blob") .primaryKeys("col1", "col2"); String cql = table.getQueryString(); assertEquals("CREATE TABLE IF NOT EXISTS test_keyspace.test_table (col1 text, col2 int, col3 blob STATIC, PRIMARY KEY(col1, col2))", cql); }
@Test public void testColumns() throws Exception { CreateTable table = TableBuilder.create("test_keyspace", "test_table") .columns(new String[]{"col1", "col2", "col3"}, new String[]{"text", "int", "blob"}) .column("col4", "text") .column("col3", "text") .column("col1", "int") .primaryKey("col1"); String cql = table.getQueryString(); assertEquals("CREATE TABLE test_keyspace.test_table (col1 int, col2 int, col3 text, col4 text, PRIMARY KEY(col1))", cql); }
/** * Returns a {@link com.datastax.driver.core.BatchStatement} with all the alter statements necessary to modify an existing table. * <p> * Note: Columns will only be added or modified, not dropped. * * @param existing the existing table to be modified * @param desired the desired end result * @return a set of statements to modify an existing table */ public static List<AlterTable> alter(AbstractTableMetadata existing, CreateTable desired) { List<AlterTable> results = new ArrayList<>(); for (BuiltTableStatement.Column column : desired.getColumns()) { ColumnMetadata columnMetadata = existing.getColumn(column.getName()); if (columnMetadata == null) { results.add(alter(desired.getKeyspace(), desired.getTable()).addColumn(column.getName(), column.getType())); } else if (!columnMetadata.getType().toString().equalsIgnoreCase(column.getType())) { if (columnMetadata.isStatic()) { throw new IllegalArgumentException("A static column cannot have its type modified"); } results.add(alter(desired.getKeyspace(), desired.getTable()).alterColumn(column.getName(), column.getType())); } } return results; }
@Test public void testCreateTable_Composite_Partition_Key() throws Exception { CreateTable table = TableBuilder.create("test_keyspace", "test_table") .column("col1", "text") .column("col2", "int") .column("col3", "blob") .column("col4", "uuid") .primaryKey("col1", PrimaryKeyType.PARTITIONING) .primaryKey("col2", PrimaryKeyType.PARTITIONING) .primaryKey("col3", PrimaryKeyType.CLUSTERING) .primaryKey("col4"); String cql = table.getQueryString(); assertEquals("CREATE TABLE test_keyspace.test_table (col1 text, col2 int, col3 blob, col4 uuid, PRIMARY KEY((col1, col2), col3, col4))", cql); }
.column("col1", "text") .column("col2", "text") .primaryKey("col1"); .ifNotExists() .column("col1", "text") .column("col2", "text") .primaryKey("col1"); .column("col1", "text") .column("col2", "text") .column("col3", "int") .column("col4", "boolean") .column("col5", "uuid") .primaryKey("col1");
@Test public void testAlter() throws Exception { CreateTable desired = TableBuilder.create("test_keyspace", "test_table") .column("col1", "text") .column("col2", "bigint") .column("col3", "int") .column("col4", "text") .primaryKey("col1"); List<AlterTable> statements = TableBuilder.alter(existing, desired); assertEquals(3, statements.size()); assertEquals("ALTER TABLE test_keyspace.test_table ALTER col2 TYPE bigint", statements.get(0).toString()); assertEquals("ALTER TABLE test_keyspace.test_table ADD col3 int", statements.get(1).toString()); assertEquals("ALTER TABLE test_keyspace.test_table ADD col4 text", statements.get(2).toString()); }
/** * Adds a column * * @param name the column name * @param type the column CQL3 type * @return the current {@link com.englishtown.vertx.cassandra.tablebuilder.BuiltTableStatement} */ public CreateTable column(String name, String type) { return addColumn(new Column(name, type, false)); }
/** * Returns a {@link com.datastax.driver.core.BatchStatement} with all the alter statements necessary to modify an existing table. * <p> * Note: Columns will only be added or modified, not dropped. * * @param existing the existing table to be modified * @param desired the desired end result * @return a set of statements to modify an existing table */ public static List<AlterTable> alter(TableMetadata existing, CreateTable desired) { List<AlterTable> results = new ArrayList<>(); for (BuiltTableStatement.Column column : desired.getColumns()) { ColumnMetadata columnMetadata = existing.getColumn(column.getName()); if (columnMetadata == null) { results.add(alter(desired.getKeyspace(), desired.getTable()).addColumn(column.getName(), column.getType())); } else if (!columnMetadata.getType().toString().equalsIgnoreCase(column.getType())) { if (columnMetadata.isStatic()) { throw new IllegalArgumentException("A static column cannot have its type modified"); } results.add(alter(desired.getKeyspace(), desired.getTable()).alterColumn(column.getName(), column.getType())); } } return results; }
.ifNotExists() .column("id", "text") .column("value", "text") .primaryKey("id");
.column("id", "text") .primaryKey("id");
/** * Adds a column * * @param name the column name * @param type the column CQL3 type * @return the current {@link com.englishtown.vertx.cassandra.tablebuilder.BuiltTableStatement} */ public CreateTable staticColumn(String name, String type) { return addColumn(new Column(name, type, true)); }
/** * Returns a {@link com.datastax.driver.core.BatchStatement} with all the alter statements necessary to modify an existing table. * <p> * Note: Columns will only be added or modified, not dropped. * * @param existing the existing table to be modified * @param desired the desired end result * @return a set of statements to modify an existing table */ public static List<AlterTable> alter(AbstractTableMetadata existing, CreateTable desired) { List<AlterTable> results = new ArrayList<>(); for (BuiltTableStatement.Column column : desired.getColumns()) { ColumnMetadata columnMetadata = existing.getColumn(column.getName()); if (columnMetadata == null) { results.add(alter(desired.getKeyspace(), desired.getTable()).addColumn(column.getName(), column.getType())); } else if (!columnMetadata.getType().toString().equalsIgnoreCase(column.getType())) { if (columnMetadata.isStatic()) { throw new IllegalArgumentException("A static column cannot have its type modified"); } results.add(alter(desired.getKeyspace(), desired.getTable()).alterColumn(column.getName(), column.getType())); } } return results; }
/** * {@inheritDoc} */ @Override public String toString() { return getQueryString(); }
/** * Adds multiple columns * * @param names the column names * @param types the column CQL3 types * @return the current {@link com.englishtown.vertx.cassandra.tablebuilder.BuiltTableStatement} */ public CreateTable columns(String[] names, String[] types) { if (names.length != types.length) { throw new IllegalArgumentException(String.format("Got %d names but %d types", names.length, types.length)); } for (int i = 0; i < names.length; i++) { column(names[i], types[i]); } return this; }
/** * Adds a column * * @param name the column name * @param type the column CQL3 type * @return the current {@link com.englishtown.vertx.cassandra.tablebuilder.BuiltTableStatement} */ public CreateTable column(String name, String type) { return addColumn(new Column(name, type, false)); }
/** * Returns a {@link com.englishtown.vertx.cassandra.tablebuilder.CreateTable} builder * * @param keyspace the keyspace for the table to create * @param table the table name * @return the create table builder */ public static CreateTable create(String keyspace, String table) { return new CreateTable(keyspace, table); }
/** * {@inheritDoc} */ @Override public String toString() { return getQueryString(); }