use of com.datastax.driver.core.TableMetadata in project presto by prestodb.
the class NativeCassandraSession method getTableMetadata.
private TableMetadata getTableMetadata(SchemaTableName schemaTableName) {
String schemaName = schemaTableName.getSchemaName();
String tableName = schemaTableName.getTableName();
KeyspaceMetadata keyspaceMetadata = getCheckedKeyspaceMetadata(schemaName);
TableMetadata tableMetadata = keyspaceMetadata.getTable(tableName);
if (tableMetadata != null) {
return tableMetadata;
}
for (TableMetadata table : keyspaceMetadata.getTables()) {
if (table.getName().equalsIgnoreCase(tableName)) {
return table;
}
}
throw new TableNotFoundException(schemaTableName);
}
use of com.datastax.driver.core.TableMetadata in project cassandra-driver-mapping by valchkou.
the class SchemaSyncTest method testAlter.
@Test
public void testAlter() {
SchemaSync.drop(keyspace, session, EntityWithIndexes.class);
SchemaSync.sync(keyspace, session, EntityWithIndexes.class);
SchemaSync.sync(keyspace, session, EntityWithIndexesV2.class);
EntityTypeMetadata entityMetadata = EntityTypeParser.getEntityMetadata(EntityWithIndexes.class);
TableMetadata tableMetadata = cluster.getMetadata().getKeyspace(keyspace).getTable(entityMetadata.getTableName());
assertNotNull(tableMetadata);
assertEquals("test_entity_index", tableMetadata.getName());
assertEquals(6, tableMetadata.getColumns().size());
ColumnMetadata columnMetadata = tableMetadata.getColumn("uuid");
//assertNull(columnMetadata.getIndex());
columnMetadata = tableMetadata.getColumn("email");
//assertNotNull(columnMetadata.getIndex());
//assertEquals("test_entity_email_idx", columnMetadata.getIndex().getName());
columnMetadata = tableMetadata.getColumn("timestamp");
//assertNull(columnMetadata.getIndex());
columnMetadata = tableMetadata.getColumn("counter2");
//assertEquals("test_entity_counter_idx", columnMetadata.getIndex().getName());
columnMetadata = tableMetadata.getColumn("name");
assertNull(columnMetadata);
columnMetadata = tableMetadata.getColumn("ref");
assertNotNull(columnMetadata);
columnMetadata = tableMetadata.getColumn("pets");
assertNotNull(columnMetadata);
}
use of com.datastax.driver.core.TableMetadata in project cassandra-driver-mapping by valchkou.
the class SchemaSyncTest method testCreate.
@Test
public void testCreate() {
EntityTypeParser.getEntityMetadata(EntityWithIndexes.class).markUnSynced(keyspace);
SchemaSync.sync(keyspace, session, EntityWithIndexes.class);
EntityTypeMetadata entityMetadata = EntityTypeParser.getEntityMetadata(EntityWithIndexes.class);
TableMetadata tableMetadata = cluster.getMetadata().getKeyspace(keyspace).getTable(entityMetadata.getTableName());
assertNotNull(tableMetadata);
assertEquals("test_entity_index", tableMetadata.getName());
assertEquals(6, tableMetadata.getColumns().size());
ColumnMetadata columnMetadata = tableMetadata.getColumn("uuid");
//assertNull(columnMetadata.getIndex());
columnMetadata = tableMetadata.getColumn("email");
//assertNotNull(columnMetadata.getIndex());
//assertEquals("test_entity_index_email_idx", columnMetadata.getIndex().getName());
columnMetadata = tableMetadata.getColumn("timestamp");
//assertNotNull(columnMetadata.getIndex());
//assertEquals("test_entity_timestamp_idx", columnMetadata.getIndex().getName());
columnMetadata = tableMetadata.getColumn("counter");
//assertNull(columnMetadata.getIndex());
columnMetadata = tableMetadata.getColumn("name");
assertNotNull(columnMetadata);
}
use of com.datastax.driver.core.TableMetadata in project cassandra-driver-mapping by valchkou.
the class SchemaSync method alterTableStatements.
/**
* Compare TableMetadata against Entity metadata and generate alter statements if necessary.
* <p>
* Cannot alter clustered and primary key columns.
*
* @param class the class to generate statements for or indexed
* @return a new {@code List<RegularStatement>}.
*/
private static <T> List<RegularStatement> alterTableStatements(String keyspace, Session session, EntityTypeMetadata entityMetadata, SyncOptions syncOptions) {
boolean doNotAddCols = false;
boolean doDropCols = true;
if (syncOptions != null) {
List<SyncOptionTypes> opts = syncOptions.getOptions(entityMetadata.getEntityClass());
doNotAddCols = opts.contains(SyncOptionTypes.DoNotAddColumns);
doDropCols = !opts.contains(SyncOptionTypes.DoNotDropColumns);
}
List<RegularStatement> statements = new ArrayList<RegularStatement>();
// get EntityTypeMetadata
String table = entityMetadata.getTableName();
// get TableMetadata - requires connection to cassandra
Cluster cluster = session.getCluster();
KeyspaceMetadata keyspaceMetadata = cluster.getMetadata().getKeyspace(keyspace);
TableMetadata tableMetadata = keyspaceMetadata.getTable(table);
// build statements for a new column or a columns with changed datatype.
for (EntityFieldMetaData field : entityMetadata.getFields()) {
String column = field.getColumnName();
String fieldType = field.getDataType().name();
ColumnMetadata columnMetadata = tableMetadata.getColumn(column);
String colIndex = null;
/*if (columnMetadata!= null && columnMetadata.getIndex() != null) {
colIndex = columnMetadata.getIndex().getName();
}*/
String fieldIndex = null;
if (entityMetadata.getIndex(column) != null) {
fieldIndex = entityMetadata.getIndex(column);
}
if (columnMetadata == null) {
if (doNotAddCols)
continue;
// if column not exists in Cassandra then build add column Statement
String colType = fieldType;
if (field.isGenericType()) {
colType = field.getGenericDef();
}
AlterTable statement = new AlterTable.Builder().addColumn(keyspace, table, column, colType);
statements.add(statement);
if (fieldIndex != null) {
statements.add(new DropIndex(column, fieldIndex));
statements.add(new CreateIndex(keyspace, table, column, fieldIndex));
}
} else if (colIndex != null || fieldIndex != null) {
if (colIndex == null) {
statements.add(new CreateIndex(keyspace, table, column, fieldIndex));
} else if (fieldIndex == null) {
statements.add(new DropIndex(column, colIndex));
} else if (!"".equals(fieldIndex) && !fieldIndex.equals(colIndex)) {
statements.add(new DropIndex(column, colIndex));
statements.add(new CreateIndex(keyspace, table, column, fieldIndex));
}
} else if (!fieldType.equals(columnMetadata.getType().getName().name())) {
// can't change datatype for clustered columns
if (tableMetadata.getClusteringColumns().contains(columnMetadata)) {
continue;
}
// can't change datatype for PK columns
if (tableMetadata.getPrimaryKey().contains(columnMetadata)) {
continue;
}
// drop index if any
/*if (columnMetadata.getIndex() != null) {
statements.add(new DropIndex(column, columnMetadata.getIndex().getName()));
}*/
// alter column datatype
statements.add(new AlterTable.Builder().alterColumn(keyspace, table, column, fieldType));
// create index if any
if (entityMetadata.getIndex(column) != null) {
statements.add(new CreateIndex(keyspace, table, column, entityMetadata.getIndex(column)));
}
}
}
// column is in Cassandra but not in entity anymore
if (doDropCols) {
for (ColumnMetadata colmeta : tableMetadata.getColumns()) {
colmeta.getName();
boolean exists = false;
for (EntityFieldMetaData field : entityMetadata.getFields()) {
if (colmeta.getName().equalsIgnoreCase(field.getColumnName())) {
exists = true;
break;
}
}
if (!exists) {
AlterTable statement = new AlterTable.Builder().dropColumn(keyspace, table, colmeta.getName());
statements.add(statement);
}
}
}
return statements;
}
use of com.datastax.driver.core.TableMetadata in project cassandra-driver-mapping by valchkou.
the class SchemaSync method buildSyncStatements.
/**
* Generate alter, drop or create statements for the given Entity
*
* @param keyspace
* @param session
* @param entityMetadata
* @param syncOptions
* @return RegularStatements
*/
public static List<RegularStatement> buildSyncStatements(String keyspace, Session session, EntityTypeMetadata entityMetadata, SyncOptions syncOptions) {
String table = entityMetadata.getTableName();
session.execute("USE " + keyspace);
Cluster cluster = session.getCluster();
KeyspaceMetadata keyspaceMetadata = cluster.getMetadata().getKeyspace(keyspace);
TableMetadata tableMetadata = keyspaceMetadata.getTable(table);
List<RegularStatement> statements = new ArrayList<RegularStatement>();
if (tableMetadata == null) {
statements = createTableStatements(keyspace, entityMetadata);
} else {
statements = alterTableStatements(keyspace, session, entityMetadata, syncOptions);
}
return statements;
}
Aggregations