use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class JdbcService method getScanner.
@SuppressFBWarnings("OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE")
public Scanner getScanner(Scan scan, Connection connection) throws SQLException, ExecutionException {
if (scan instanceof ScanAll) {
operationChecker.check((ScanAll) scan);
} else {
operationChecker.check(scan);
}
TableMetadata tableMetadata = tableMetadataManager.getTableMetadata(scan);
SelectQuery selectQuery = scan instanceof ScanAll ? buildSelectQueryForScanAll((ScanAll) scan, tableMetadata) : buildSelectQueryForScan(scan, tableMetadata);
PreparedStatement preparedStatement = connection.prepareStatement(selectQuery.sql());
selectQuery.bind(preparedStatement);
ResultSet resultSet = preparedStatement.executeQuery();
return new ScannerImpl(new ResultInterpreter(scan.getProjections(), tableMetadata), connection, preparedStatement, resultSet);
}
use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class JdbcService method get.
public Optional<Result> get(Get get, Connection connection) throws SQLException, ExecutionException {
operationChecker.check(get);
TableMetadata tableMetadata = tableMetadataManager.getTableMetadata(get);
SelectQuery selectQuery = queryBuilder.select(get.getProjections()).from(get.forNamespace().get(), get.forTable().get(), tableMetadata).where(get.getPartitionKey(), get.getClusteringKey()).build();
try (PreparedStatement preparedStatement = connection.prepareStatement(selectQuery.sql())) {
selectQuery.bind(preparedStatement);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
Optional<Result> ret = Optional.of(new ResultInterpreter(get.getProjections(), tableMetadata).interpret(resultSet));
if (resultSet.next()) {
throw new IllegalArgumentException("please use scan() for non-exact match selection");
}
return ret;
}
return Optional.empty();
}
}
}
use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class JdbcAdminTest method createTable_WithClusteringOrderForX_shouldExecuteCreateTableStatement.
private void createTable_WithClusteringOrderForX_shouldExecuteCreateTableStatement(RdbEngine rdbEngine, Optional<String> tableMetadataSchema, String... expectedSqlStatements) throws SQLException, ExecutionException {
// Arrange
String namespace = "my_ns";
String table = "foo_table";
TableMetadata metadata = TableMetadata.newBuilder().addPartitionKey("c3").addClusteringKey("c1", Order.DESC).addClusteringKey("c4", Order.ASC).addColumn("c1", DataType.TEXT).addColumn("c2", DataType.BIGINT).addColumn("c3", DataType.BOOLEAN).addColumn("c4", DataType.BLOB).addColumn("c5", DataType.INT).addColumn("c6", DataType.DOUBLE).addColumn("c7", DataType.FLOAT).addSecondaryIndex("c1").addSecondaryIndex("c4").build();
List<Statement> mockedStatements = new ArrayList<>();
for (int i = 0; i < expectedSqlStatements.length; i++) {
mockedStatements.add(mock(Statement.class));
}
when(connection.createStatement()).thenReturn(mockedStatements.get(0), mockedStatements.subList(1, mockedStatements.size()).toArray(new Statement[0]));
when(dataSource.getConnection()).thenReturn(connection);
if (tableMetadataSchema.isPresent()) {
when(config.getTableMetadataSchema()).thenReturn(tableMetadataSchema);
}
JdbcAdmin admin = new JdbcAdmin(dataSource, rdbEngine, config);
// Act
admin.createTable(namespace, table, metadata, new HashMap<>());
// Assert
for (int i = 0; i < expectedSqlStatements.length; i++) {
verify(mockedStatements.get(i)).execute(expectedSqlStatements[i]);
}
}
use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class JdbcAdminTest method getTableMetadata_forX_ShouldReturnTableMetadata.
private void getTableMetadata_forX_ShouldReturnTableMetadata(RdbEngine rdbEngine, Optional<String> tableMetadataSchema, String expectedSelectStatements) throws ExecutionException, SQLException {
// Arrange
String namespace = "ns";
String table = "table";
PreparedStatement selectStatement = mock(PreparedStatement.class);
ResultSet resultSet = mockResultSet(Arrays.asList(new GetColumnsResultSetMocker.Row("c3", DataType.BOOLEAN.toString(), "PARTITION", null, false), new GetColumnsResultSetMocker.Row("c1", DataType.TEXT.toString(), "CLUSTERING", Order.DESC.toString(), false), new GetColumnsResultSetMocker.Row("c4", DataType.BLOB.toString(), "CLUSTERING", Order.ASC.toString(), true), new GetColumnsResultSetMocker.Row("c2", DataType.BIGINT.toString(), null, null, false), new GetColumnsResultSetMocker.Row("c5", DataType.INT.toString(), null, null, false), new GetColumnsResultSetMocker.Row("c6", DataType.DOUBLE.toString(), null, null, false), new GetColumnsResultSetMocker.Row("c7", DataType.FLOAT.toString(), null, null, false)));
when(selectStatement.executeQuery()).thenReturn(resultSet);
when(connection.prepareStatement(any())).thenReturn(selectStatement);
when(dataSource.getConnection()).thenReturn(connection);
if (tableMetadataSchema.isPresent()) {
when(config.getTableMetadataSchema()).thenReturn(tableMetadataSchema);
}
JdbcAdmin admin = new JdbcAdmin(dataSource, rdbEngine, config);
// Act
TableMetadata actualMetadata = admin.getTableMetadata(namespace, table);
// Assert
TableMetadata expectedMetadata = TableMetadata.newBuilder().addPartitionKey("c3").addClusteringKey("c1", Order.DESC).addClusteringKey("c4", Order.ASC).addColumn("c1", DataType.TEXT).addColumn("c2", DataType.BIGINT).addColumn("c3", DataType.BOOLEAN).addColumn("c4", DataType.BLOB).addColumn("c5", DataType.INT).addColumn("c6", DataType.DOUBLE).addColumn("c7", DataType.FLOAT).addSecondaryIndex("c4").build();
assertThat(actualMetadata).isEqualTo(expectedMetadata);
verify(connection).prepareStatement(expectedSelectStatements);
}
use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class JdbcAdminTest method createTable_forX_shouldExecuteCreateTableStatement.
private void createTable_forX_shouldExecuteCreateTableStatement(RdbEngine rdbEngine, String... expectedSqlStatements) throws SQLException, ExecutionException {
// Arrange
String namespace = "my_ns";
String table = "foo_table";
TableMetadata metadata = TableMetadata.newBuilder().addPartitionKey("c3").addClusteringKey("c1").addClusteringKey("c4").addColumn("c1", DataType.TEXT).addColumn("c2", DataType.BIGINT).addColumn("c3", DataType.BOOLEAN).addColumn("c4", DataType.BLOB).addColumn("c5", DataType.INT).addColumn("c6", DataType.DOUBLE).addColumn("c7", DataType.FLOAT).addSecondaryIndex("c1").addSecondaryIndex("c4").build();
List<Statement> mockedStatements = new ArrayList<>();
for (int i = 0; i < expectedSqlStatements.length; i++) {
mockedStatements.add(mock(Statement.class));
}
when(connection.createStatement()).thenReturn(mockedStatements.get(0), mockedStatements.subList(1, mockedStatements.size()).toArray(new Statement[0]));
when(dataSource.getConnection()).thenReturn(connection);
JdbcAdmin admin = new JdbcAdmin(dataSource, rdbEngine, config);
// Act
admin.createTable(namespace, table, metadata, new HashMap<>());
// Assert
for (int i = 0; i < expectedSqlStatements.length; i++) {
verify(mockedStatements.get(i)).execute(expectedSqlStatements[i]);
}
}
Aggregations