feat: support analyzeUpdate (#1867) · googleapis/java-spanner@2d8cfa4 (original) (raw)
`@@ -48,6 +48,7 @@
`
48
48
`import com.google.common.base.Preconditions;
`
49
49
`import com.google.common.util.concurrent.MoreExecutors;
`
50
50
`import com.google.spanner.v1.ExecuteSqlRequest.QueryOptions;
`
``
51
`+
import com.google.spanner.v1.ResultSetStats;
`
51
52
`import java.util.ArrayList;
`
52
53
`import java.util.Arrays;
`
53
54
`import java.util.Collections;
`
`@@ -293,6 +294,9 @@ public void close() {
`
293
294
`public ApiFuture closeAsync() {
`
294
295
`if (!isClosed()) {
`
295
296
`List<ApiFuture> futures = new ArrayList<>();
`
``
297
`+
if (isBatchActive()) {
`
``
298
`+
abortBatch();
`
``
299
`+
}
`
296
300
`if (isTransactionStarted()) {
`
297
301
`futures.add(rollbackAsync());
`
298
302
` }
`
`@@ -970,6 +974,7 @@ public long executeUpdate(Statement update) {
`
970
974
`"Statement is not an update statement: " + parsedStatement.getSqlWithoutComments());
`
971
975
` }
`
972
976
``
``
977
`+
@Override
`
973
978
`public ApiFuture executeUpdateAsync(Statement update) {
`
974
979
`Preconditions.checkNotNull(update);
`
975
980
`ConnectionPreconditions.checkState(!isClosed(), CLOSED_ERROR_MSG);
`
`@@ -990,6 +995,27 @@ public ApiFuture executeUpdateAsync(Statement update) {
`
990
995
`"Statement is not an update statement: " + parsedStatement.getSqlWithoutComments());
`
991
996
` }
`
992
997
``
``
998
`+
@Override
`
``
999
`+
public ResultSetStats analyzeUpdate(Statement update, QueryAnalyzeMode analyzeMode) {
`
``
1000
`+
Preconditions.checkNotNull(update);
`
``
1001
`+
ConnectionPreconditions.checkState(!isClosed(), CLOSED_ERROR_MSG);
`
``
1002
`+
ParsedStatement parsedStatement = getStatementParser().parse(update);
`
``
1003
`+
if (parsedStatement.isUpdate()) {
`
``
1004
`+
switch (parsedStatement.getType()) {
`
``
1005
`+
case UPDATE:
`
``
1006
`+
return get(internalAnalyzeUpdateAsync(parsedStatement, AnalyzeMode.of(analyzeMode)));
`
``
1007
`+
case CLIENT_SIDE:
`
``
1008
`+
case QUERY:
`
``
1009
`+
case DDL:
`
``
1010
`+
case UNKNOWN:
`
``
1011
`+
default:
`
``
1012
`+
}
`
``
1013
`+
}
`
``
1014
`+
throw SpannerExceptionFactory.newSpannerException(
`
``
1015
`+
ErrorCode.INVALID_ARGUMENT,
`
``
1016
`+
"Statement is not an update statement: " + parsedStatement.getSqlWithoutComments());
`
``
1017
`+
}
`
``
1018
+
993
1019
`@Override
`
994
1020
`public long[] executeBatchUpdate(Iterable updates) {
`
995
1021
`Preconditions.checkNotNull(updates);
`
`@@ -1101,7 +1127,9 @@ private ResultSet internalExecuteQuery(
`
1101
1127
`final AnalyzeMode analyzeMode,
`
1102
1128
`final QueryOption... options) {
`
1103
1129
`Preconditions.checkArgument(
`
1104
``
`-
statement.getType() == StatementType.QUERY, "Statement must be a query");
`
``
1130
`+
statement.getType() == StatementType.QUERY
`
``
1131
`+
|| (statement.getType() == StatementType.UPDATE && analyzeMode != AnalyzeMode.NONE),
`
``
1132
`+
"Statement must either be a query or a DML mode with analyzeMode!=NONE");
`
1105
1133
`UnitOfWork transaction = getCurrentUnitOfWorkOrStartNewUnitOfWork();
`
1106
1134
`return get(
`
1107
1135
`transaction.executeQueryAsync(
`
`@@ -1131,6 +1159,15 @@ private ApiFuture internalExecuteUpdateAsync(
`
1131
1159
`update, mergeUpdateRequestOptions(mergeUpdateStatementTag(options)));
`
1132
1160
` }
`
1133
1161
``
``
1162
`+
private ApiFuture internalAnalyzeUpdateAsync(
`
``
1163
`+
final ParsedStatement update, AnalyzeMode analyzeMode, UpdateOption... options) {
`
``
1164
`+
Preconditions.checkArgument(
`
``
1165
`+
update.getType() == StatementType.UPDATE, "Statement must be an update");
`
``
1166
`+
UnitOfWork transaction = getCurrentUnitOfWorkOrStartNewUnitOfWork();
`
``
1167
`+
return transaction.analyzeUpdateAsync(
`
``
1168
`+
update, analyzeMode, mergeUpdateRequestOptions(mergeUpdateStatementTag(options)));
`
``
1169
`+
}
`
``
1170
+
1134
1171
`private ApiFuture<long[]> internalExecuteBatchUpdateAsync(
`
1135
1172
`List updates, UpdateOption... options) {
`
1136
1173
`UnitOfWork transaction = getCurrentUnitOfWorkOrStartNewUnitOfWork();
`