ITableRepositoryImpl implements ITableRepository { protected String generateTableCreationSyntax(Index index) { String newTableName = Utils.calculateITableName(index); StringBuilder fieldCreateStatement = new StringBuilder(); StringBuilder primaryKeyCreateStatement = new StringBuilder(); boolean first = true; for (IndexField field : index.getFields()) { if (!first) { fieldCreateStatement.append(", "); primaryKeyCreateStatement.append(", "); } else { first = false; } fieldCreateStatement.append(field.getField()).append(" ").append(field.getType().mapToCassandaraDataType()); primaryKeyCreateStatement.append(field.getField()); } if (!index.isUnique()) { primaryKeyCreateStatement.append(", ").append("id"); } String finalStatement = String.format(TABLE_CREATE_CQL, newTableName, fieldCreateStatement, primaryKeyCreateStatement); logger.debug("For index: " + index.toString() + ", the table create SQL is: " + finalStatement); return finalStatement; } ITableRepositoryImpl(Session session); @Override boolean iTableExists(Index index); @Override boolean iTableExists(IndexIdentifier indexId); @Override void createITable(Index index); @Override void deleteITable(Index index); @Override void deleteITable(IndexIdentifier indexId); @Override void deleteITable(String tableName);  }
@Test public void testGenerateTableCreationSyntax() { System.out.println("generateTableCreationSyntax"); ITableRepositoryImpl instance = new ITableRepositoryImpl(f.getSession()); String response = instance.generateTableCreationSyntax(Fixtures.createTestIndexOneField()); Assert.assertNotNull(response); assertEquals("CREATE TABLE IF NOT EXISTS mydb_mytable_myindexwithonefield (bucket bigint, id uuid, object blob, created_at timestamp, updated_at timestamp, myindexedfield varchar, PRIMARY KEY ((bucket), myindexedfield, id));", response); response = instance.generateTableCreationSyntax(Fixtures.createTestIndexTwoField()); Assert.assertNotNull(response); assertEquals("CREATE TABLE IF NOT EXISTS mydb_mytable_myindexwithtwofields (bucket bigint, id uuid, object blob, created_at timestamp, updated_at timestamp, myindexedfield1 varchar, myindexedfield2 varchar, PRIMARY KEY ((bucket), myindexedfield1, myindexedfield2, id));", response); }
@Test public void testGenerateTableCreationSyntaxUnique() { System.out.println("testGenerateTableCreationSyntaxUnique"); ITableRepositoryImpl instance = new ITableRepositoryImpl(f.getSession()); Index one = Fixtures.createTestIndexOneField(); one.isUnique(true); String response = instance.generateTableCreationSyntax(one); Assert.assertNotNull(response); assertEquals("CREATE TABLE IF NOT EXISTS mydb_mytable_myindexwithonefield (bucket bigint, id uuid, object blob, created_at timestamp, updated_at timestamp, myindexedfield varchar, PRIMARY KEY ((bucket), myindexedfield));", response); Index two = Fixtures.createTestIndexTwoField(); two.isUnique(true); response = instance.generateTableCreationSyntax(two); Assert.assertNotNull(response); assertEquals("CREATE TABLE IF NOT EXISTS mydb_mytable_myindexwithtwofields (bucket bigint, id uuid, object blob, created_at timestamp, updated_at timestamp, myindexedfield1 varchar, myindexedfield2 varchar, PRIMARY KEY ((bucket), myindexedfield1, myindexedfield2));", response); }
@Test public void testGenerateTableCreationSyntaxWithDataTypes() { System.out.println("generateTableCreationSyntax"); ITableRepositoryImpl instance = new ITableRepositoryImpl(f.getSession()); String response = instance.generateTableCreationSyntax(Fixtures.createTestIndexNumericField()); Assert.assertNotNull(response); assertEquals("CREATE TABLE IF NOT EXISTS mydb_mytable_myindexnumericfield (bucket bigint, id uuid, object blob, created_at timestamp, updated_at timestamp, myindexedfield3 int, PRIMARY KEY ((bucket), myindexedfield3, id));", response); response = instance.generateTableCreationSyntax(Fixtures.createTestIndexUUIDField()); Assert.assertNotNull(response); assertEquals("CREATE TABLE IF NOT EXISTS mydb_mytable_myindexuuidfield (bucket bigint, id uuid, object blob, created_at timestamp, updated_at timestamp, myindexedfield4 uuid, PRIMARY KEY ((bucket), myindexedfield4, id));", response); }