Search in sources :

Example 56 with TableMetadata

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);
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) SelectQuery(com.scalar.db.storage.jdbc.query.SelectQuery) ResultSet(java.sql.ResultSet) ScanAll(com.scalar.db.api.ScanAll) PreparedStatement(java.sql.PreparedStatement) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 57 with TableMetadata

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();
        }
    }
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) SelectQuery(com.scalar.db.storage.jdbc.query.SelectQuery) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Result(com.scalar.db.api.Result)

Example 58 with TableMetadata

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]);
    }
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ArrayList(java.util.ArrayList)

Example 59 with TableMetadata

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);
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 60 with TableMetadata

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]);
    }
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ArrayList(java.util.ArrayList)

Aggregations

TableMetadata (com.scalar.db.api.TableMetadata)101 Test (org.junit.jupiter.api.Test)39 Get (com.scalar.db.api.Get)11 PreparedStatement (java.sql.PreparedStatement)10 ExecutionException (com.scalar.db.exception.storage.ExecutionException)9 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 Key (com.scalar.db.io.Key)6 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)6 Mutation (com.scalar.db.api.Mutation)5 Result (com.scalar.db.api.Result)5 GetTableMetadataResponse (com.scalar.db.rpc.GetTableMetadataResponse)5 ResultSet (java.sql.ResultSet)5 Nonnull (javax.annotation.Nonnull)5 HashMap (java.util.HashMap)4 Options (com.datastax.driver.core.schemabuilder.Create.Options)3 KeyspaceOptions (com.datastax.driver.core.schemabuilder.KeyspaceOptions)3 TableOptions (com.datastax.driver.core.schemabuilder.TableOptions)3 ScanAll (com.scalar.db.api.ScanAll)3 NoMutationException (com.scalar.db.exception.storage.NoMutationException)3