/** * 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; }
@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 testDropTable() throws Exception { DropTable table = TableBuilder.drop("test_keyspace", "test_table"); String cql = table.getQueryString(); assertEquals("DROP TABLE test_keyspace.test_table", cql); }
statement = TableBuilder.create(keyspace, table) .column("col1", "text") .column("col2", "text") statement = TableBuilder.create(keyspace, table) .ifNotExists() .column("col1", "text") CreateTable createTable = TableBuilder.create(keyspace, table) .column("col1", "text") .column("col2", "text") List<AlterTable> alterStatements = TableBuilder.alter(tableMetadata, createTable); assertEquals(5, tableMetadata.getColumns().size()); statement = TableBuilder.drop(keyspace, table); session.execute(statement); statement = TableBuilder.drop(keyspace, table) .ifExists(); session.execute(statement); statement = TableBuilder.drop(keyspace, table); session.execute(statement); fail("Table should not exist, drop should have throw InvalidQueryException");
@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); }
@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); }
/** * 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); }
@Test public void testIfExists() throws Exception { DropTable table = TableBuilder.drop("test_keyspace", "test_table").ifExists(); String cql = table.getQueryString(); assertEquals("DROP TABLE IF EXISTS test_keyspace.test_table", 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; }
assertNotNull(value); Statement statement = TableBuilder.create(keyspace, "test") .column("id", "text") .primaryKey("id");
@Test public void testAdd() throws Exception { AlterTable alter = TableBuilder.alter("test_keyspace", "test_table") .addColumn("col1", "text"); String cql = alter.getQueryString(); assertEquals("ALTER TABLE test_keyspace.test_table ADD col1 text", cql); }
@Test public void testRename() throws Exception { AlterTable alter = TableBuilder.alter("test_keyspace", "test_table") .renameColumn("col1", "col2"); String cql = alter.getQueryString(); assertEquals("ALTER TABLE test_keyspace.test_table RENAME col1 TO col2", cql); } }
@Test public void testAlter() throws Exception { AlterTable alter = TableBuilder.alter("test_keyspace", "test_table") .alterColumn("col1", "text"); String cql = alter.getQueryString(); assertEquals("ALTER TABLE test_keyspace.test_table ALTER col1 TYPE text", cql); }
@Test public void testDrop() throws Exception { AlterTable alter = TableBuilder.alter("test_keyspace", "test_table") .dropColumn("col1"); String cql = alter.getQueryString(); assertEquals("ALTER TABLE test_keyspace.test_table DROP col1", cql); }