Search in sources :

Example 41 with ParsedStatement

use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project pgadapter by GoogleCloudPlatform.

the class StatementParser method parseCommands.

public static ImmutableList<String> parseCommands(ImmutableList<ParsedStatement> statements) {
    Preconditions.checkNotNull(statements);
    ImmutableList.Builder<String> builder = ImmutableList.builder();
    for (ParsedStatement stat : statements) {
        builder.add(parseCommand(stat.getSqlWithoutComments()));
    }
    return builder.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement)

Example 42 with ParsedStatement

use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.

the class ConnectionImpl method executeUpdateAsync.

public ApiFuture<Long> executeUpdateAsync(Statement update) {
    Preconditions.checkNotNull(update);
    ConnectionPreconditions.checkState(!isClosed(), CLOSED_ERROR_MSG);
    ParsedStatement parsedStatement = getStatementParser().parse(update);
    if (parsedStatement.isUpdate()) {
        switch(parsedStatement.getType()) {
            case UPDATE:
                return internalExecuteUpdateAsync(parsedStatement);
            case CLIENT_SIDE:
            case QUERY:
            case DDL:
            case UNKNOWN:
            default:
        }
    }
    throw SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "Statement is not an update statement: " + parsedStatement.getSqlWithoutComments());
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement)

Example 43 with ParsedStatement

use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.

the class ConnectionImpl method executeBatchUpdate.

@Override
public long[] executeBatchUpdate(Iterable<Statement> updates) {
    Preconditions.checkNotNull(updates);
    ConnectionPreconditions.checkState(!isClosed(), CLOSED_ERROR_MSG);
    // Check that there are only DML statements in the input.
    List<ParsedStatement> parsedStatements = new LinkedList<>();
    for (Statement update : updates) {
        ParsedStatement parsedStatement = getStatementParser().parse(update);
        switch(parsedStatement.getType()) {
            case UPDATE:
                parsedStatements.add(parsedStatement);
                break;
            case CLIENT_SIDE:
            case QUERY:
            case DDL:
            case UNKNOWN:
            default:
                throw SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "The batch update list contains a statement that is not an update statement: " + parsedStatement.getSqlWithoutComments());
        }
    }
    return get(internalExecuteBatchUpdateAsync(parsedStatements));
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Statement(com.google.cloud.spanner.Statement) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) LinkedList(java.util.LinkedList)

Example 44 with ParsedStatement

use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.

the class ConnectionImpl method executeAsync.

@Override
public AsyncStatementResult executeAsync(Statement statement) {
    Preconditions.checkNotNull(statement);
    ConnectionPreconditions.checkState(!isClosed(), CLOSED_ERROR_MSG);
    ParsedStatement parsedStatement = getStatementParser().parse(statement, this.queryOptions);
    switch(parsedStatement.getType()) {
        case CLIENT_SIDE:
            return AsyncStatementResultImpl.of(parsedStatement.getClientSideStatement().execute(connectionStatementExecutor, parsedStatement.getSqlWithoutComments()), spanner.getAsyncExecutorProvider());
        case QUERY:
            return AsyncStatementResultImpl.of(internalExecuteQueryAsync(parsedStatement, AnalyzeMode.NONE));
        case UPDATE:
            return AsyncStatementResultImpl.of(internalExecuteUpdateAsync(parsedStatement));
        case DDL:
            return AsyncStatementResultImpl.noResult(executeDdlAsync(parsedStatement));
        case UNKNOWN:
        default:
    }
    throw SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "Unknown statement: " + parsedStatement.getSqlWithoutComments());
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement)

Example 45 with ParsedStatement

use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.

the class ConnectionImpl method parseAndExecuteQuery.

/**
 * Parses the given statement as a query and executes it. Throws a {@link SpannerException} if the
 * statement is not a query.
 */
private ResultSet parseAndExecuteQuery(Statement query, AnalyzeMode analyzeMode, QueryOption... options) {
    Preconditions.checkNotNull(query);
    Preconditions.checkNotNull(analyzeMode);
    ConnectionPreconditions.checkState(!isClosed(), CLOSED_ERROR_MSG);
    ParsedStatement parsedStatement = getStatementParser().parse(query, this.queryOptions);
    if (parsedStatement.isQuery()) {
        switch(parsedStatement.getType()) {
            case CLIENT_SIDE:
                return parsedStatement.getClientSideStatement().execute(connectionStatementExecutor, parsedStatement.getSqlWithoutComments()).getResultSet();
            case QUERY:
                return internalExecuteQuery(parsedStatement, analyzeMode, options);
            case UPDATE:
            case DDL:
            case UNKNOWN:
            default:
        }
    }
    throw SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "Statement is not a query: " + parsedStatement.getSqlWithoutComments());
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement)

Aggregations

ParsedStatement (com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement)75 Test (org.junit.Test)61 Statement (com.google.cloud.spanner.Statement)23 SpannerException (com.google.cloud.spanner.SpannerException)20 ResultSet (com.google.cloud.spanner.ResultSet)12 AsyncResultSet (com.google.cloud.spanner.AsyncResultSet)6 DatabaseClient (com.google.cloud.spanner.DatabaseClient)6 TimestampBound (com.google.cloud.spanner.TimestampBound)6 LinkedList (java.util.LinkedList)3 Mockito.anyString (org.mockito.Mockito.anyString)3 AbortedException (com.google.cloud.spanner.AbortedException)2 QueryOption (com.google.cloud.spanner.Options.QueryOption)2 Mutation (com.google.cloud.spanner.Mutation)1 ReadContext (com.google.cloud.spanner.ReadContext)1 TransactionContext (com.google.cloud.spanner.TransactionContext)1 TransactionManager (com.google.cloud.spanner.TransactionManager)1 ImmutableList (com.google.common.collect.ImmutableList)1 Duration (com.google.protobuf.Duration)1 BigDecimal (java.math.BigDecimal)1 TimeUnit (java.util.concurrent.TimeUnit)1