Search in sources :

Example 36 with ParsedStatement

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

the class ReadWriteTransaction method executeBatchUpdateAsync.

@Override
public ApiFuture<long[]> executeBatchUpdateAsync(Iterable<ParsedStatement> updates, final UpdateOption... options) {
    Preconditions.checkNotNull(updates);
    final List<Statement> updateStatements = new LinkedList<>();
    for (ParsedStatement update : updates) {
        Preconditions.checkArgument(update.isUpdate(), "Statement is not an update statement: " + update.getSqlWithoutComments());
        updateStatements.add(update.getStatement());
    }
    checkValidTransaction();
    ApiFuture<long[]> res;
    if (retryAbortsInternally) {
        res = executeStatementAsync(RUN_BATCH_STATEMENT, () -> {
            checkTimedOut();
            return runWithRetry(() -> {
                try {
                    getStatementExecutor().invokeInterceptors(RUN_BATCH_STATEMENT, StatementExecutionStep.EXECUTE_STATEMENT, ReadWriteTransaction.this);
                    long[] updateCounts = get(txContextFuture).batchUpdate(updateStatements, options);
                    createAndAddRetriableBatchUpdate(updateStatements, updateCounts, options);
                    return updateCounts;
                } catch (AbortedException e) {
                    throw e;
                } catch (SpannerException e) {
                    createAndAddFailedBatchUpdate(e, updateStatements);
                    throw e;
                }
            });
        }, // ignore interceptors here as they are invoked in the Callable.
        InterceptorsUsage.IGNORE_INTERCEPTORS, ImmutableList.of(SpannerGrpc.getExecuteBatchDmlMethod()));
    } else {
        res = executeStatementAsync(RUN_BATCH_STATEMENT, () -> {
            checkTimedOut();
            checkAborted();
            return get(txContextFuture).batchUpdate(updateStatements);
        }, SpannerGrpc.getExecuteBatchDmlMethod());
    }
    ApiFutures.addCallback(res, new ApiFutureCallback<long[]>() {

        @Override
        public void onFailure(Throwable t) {
            if (t instanceof SpannerException) {
                handlePossibleInvalidatingException((SpannerException) t);
            }
        }

        @Override
        public void onSuccess(long[] result) {
        }
    }, MoreExecutors.directExecutor());
    return res;
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Statement(com.google.cloud.spanner.Statement) AbortedException(com.google.cloud.spanner.AbortedException) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) SpannerException(com.google.cloud.spanner.SpannerException) LinkedList(java.util.LinkedList)

Example 37 with ParsedStatement

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

the class ConnectionImpl method parseAndExecuteQueryAsync.

private AsyncResultSet parseAndExecuteQueryAsync(Statement query, AnalyzeMode analyzeMode, QueryOption... options) {
    Preconditions.checkNotNull(query);
    ConnectionPreconditions.checkState(!isClosed(), CLOSED_ERROR_MSG);
    ParsedStatement parsedStatement = getStatementParser().parse(query, this.queryOptions);
    if (parsedStatement.isQuery()) {
        switch(parsedStatement.getType()) {
            case CLIENT_SIDE:
                return ResultSets.toAsyncResultSet(parsedStatement.getClientSideStatement().execute(connectionStatementExecutor, parsedStatement.getSqlWithoutComments()).getResultSet(), spanner.getAsyncExecutorProvider(), options);
            case QUERY:
                return internalExecuteQueryAsync(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)

Example 38 with ParsedStatement

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

the class ConnectionImpl method executeUpdate.

@Override
public long executeUpdate(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 get(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 39 with ParsedStatement

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

the class ConnectionImpl method executeBatchUpdateAsync.

@Override
public ApiFuture<long[]> executeBatchUpdateAsync(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 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 40 with ParsedStatement

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

the class ConnectionImpl method execute.

@Override
public StatementResult execute(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 parsedStatement.getClientSideStatement().execute(connectionStatementExecutor, parsedStatement.getSqlWithoutComments());
        case QUERY:
            return StatementResultImpl.of(internalExecuteQuery(parsedStatement, AnalyzeMode.NONE));
        case UPDATE:
            return StatementResultImpl.of(get(internalExecuteUpdateAsync(parsedStatement)));
        case DDL:
            get(executeDdlAsync(parsedStatement));
            return StatementResultImpl.noResult();
        case UNKNOWN:
        default:
    }
    throw SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "Unknown statement: " + 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