Search in sources :

Example 1 with SelectQuery

use of com.scalar.db.storage.jdbc.query.SelectQuery in project scalardb by scalar-labs.

the class JdbcService method scan.

public List<Result> scan(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);
    try (PreparedStatement preparedStatement = connection.prepareStatement(selectQuery.sql())) {
        selectQuery.bind(preparedStatement);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            List<Result> ret = new ArrayList<>();
            ResultInterpreter resultInterpreter = new ResultInterpreter(scan.getProjections(), tableMetadata);
            while (resultSet.next()) {
                ret.add(resultInterpreter.interpret(resultSet));
            }
            return ret;
        }
    }
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) SelectQuery(com.scalar.db.storage.jdbc.query.SelectQuery) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) ScanAll(com.scalar.db.api.ScanAll) PreparedStatement(java.sql.PreparedStatement) Result(com.scalar.db.api.Result)

Example 2 with SelectQuery

use of com.scalar.db.storage.jdbc.query.SelectQuery 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 3 with SelectQuery

use of com.scalar.db.storage.jdbc.query.SelectQuery 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)

Aggregations

TableMetadata (com.scalar.db.api.TableMetadata)3 SelectQuery (com.scalar.db.storage.jdbc.query.SelectQuery)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 Result (com.scalar.db.api.Result)2 ScanAll (com.scalar.db.api.ScanAll)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 ArrayList (java.util.ArrayList)1