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;
}
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());
}
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());
}
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);
}
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());
}
Aggregations