/** * 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; }
@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); }
/** * 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 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; }
@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); }
@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()); }
@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); }
.column("id", "text") .primaryKey("id");