Skip to content

Commit 6e1aa8e

Browse files
authored
chore: include client statement type in parsed statement (#1849)
Include the ClientSideStatementType in ParsedStatement. This enables clients to directly check what type of statement it is, which removes the need for textual checks. This will for example remove these types of checks: https://github.com/GoogleCloudPlatform/pgadapter/blob/2a3f2eb7cd16cc3c71ffc673544ca9e2baaba21c/src/main/java/com/google/cloud/spanner/pgadapter/statements/IntermediateStatement.java#L354
1 parent 182906f commit 6e1aa8e

File tree

6 files changed

+127
-1
lines changed

6 files changed

+127
-1
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/AbstractStatementParser.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
import com.google.cloud.spanner.SpannerException;
2323
import com.google.cloud.spanner.SpannerExceptionFactory;
2424
import com.google.cloud.spanner.Statement;
25+
import com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType;
2526
import com.google.common.annotations.VisibleForTesting;
2627
import com.google.common.base.Preconditions;
2728
import com.google.common.collect.ImmutableMap;
2829
import com.google.common.collect.ImmutableSet;
2930
import com.google.spanner.v1.ExecuteSqlRequest.QueryOptions;
31+
import java.util.Collections;
3032
import java.util.HashMap;
3133
import java.util.Map;
3234
import java.util.Objects;
@@ -270,6 +272,16 @@ public boolean isDdl() {
270272
return false;
271273
}
272274

275+
/**
276+
* Returns the {@link ClientSideStatementType} of this statement. This method may only be called
277+
* on statements of type {@link StatementType#CLIENT_SIDE}.
278+
*/
279+
@InternalApi
280+
public ClientSideStatementType getClientSideStatementType() {
281+
Preconditions.checkState(type == StatementType.CLIENT_SIDE);
282+
return clientSideStatement.getStatementType();
283+
}
284+
273285
Statement getStatement() {
274286
return statement;
275287
}
@@ -314,7 +326,12 @@ ClientSideStatement getClientSideStatement() {
314326
private final Set<ClientSideStatementImpl> statements;
315327

316328
AbstractStatementParser(Set<ClientSideStatementImpl> statements) {
317-
this.statements = statements;
329+
this.statements = Collections.unmodifiableSet(statements);
330+
}
331+
332+
@VisibleForTesting
333+
Set<ClientSideStatementImpl> getClientSideStatements() {
334+
return statements;
318335
}
319336

320337
/**

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/ClientSideStatement.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.cloud.spanner.connection;
1818

1919
import com.google.cloud.spanner.ResultSet;
20+
import com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType;
2021
import java.util.List;
2122

2223
/**
@@ -47,6 +48,9 @@ interface ClientSideStatement {
4748
/** @return <code>true</code> if this {@link ClientSideStatement} will return an update count. */
4849
boolean isUpdate();
4950

51+
/** @return the statement type */
52+
ClientSideStatementType getStatementType();
53+
5054
/**
5155
* Execute this {@link ClientSideStatement} on the given {@link ConnectionStatementExecutor}. The
5256
* executor calls the appropriate method(s) on the {@link Connection}. The statement argument is

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/ClientSideStatementImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.cloud.spanner.connection;
1818

1919
import com.google.cloud.spanner.SpannerException;
20+
import com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType;
2021
import com.google.cloud.spanner.connection.StatementResult.ResultType;
2122
import com.google.common.base.Preconditions;
2223
import java.lang.reflect.Constructor;
@@ -106,6 +107,8 @@ public String getMessage() {
106107
/** The result type of this statement. */
107108
private ResultType resultType;
108109

110+
private ClientSideStatementType statementType;
111+
109112
/** The regular expression that should be used to recognize this class of statements. */
110113
private String regex;
111114

@@ -183,6 +186,11 @@ public boolean isUpdate() {
183186
return resultType == ResultType.UPDATE_COUNT;
184187
}
185188

189+
@Override
190+
public ClientSideStatementType getStatementType() {
191+
return statementType;
192+
}
193+
186194
boolean matches(String statement) {
187195
Preconditions.checkState(pattern != null, "This statement has not been compiled");
188196
return pattern.matcher(statement).matches();

google-cloud-spanner/src/main/resources/com/google/cloud/spanner/connection/ClientSideStatements.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"name": "SHOW VARIABLE AUTOCOMMIT",
66
"executorName": "ClientSideStatementNoParamExecutor",
77
"resultType": "RESULT_SET",
8+
"statementType": "SHOW_AUTOCOMMIT",
89
"regex": "(?is)\\A\\s*show\\s+variable\\s+autocommit\\s*\\z",
910
"method": "statementShowAutocommit",
1011
"exampleStatements": ["show variable autocommit"]
@@ -13,6 +14,7 @@
1314
"name": "SHOW VARIABLE READONLY",
1415
"executorName": "ClientSideStatementNoParamExecutor",
1516
"resultType": "RESULT_SET",
17+
"statementType": "SHOW_READONLY",
1618
"regex": "(?is)\\A\\s*show\\s+variable\\s+readonly\\s*\\z",
1719
"method": "statementShowReadOnly",
1820
"exampleStatements": ["show variable readonly"]
@@ -21,6 +23,7 @@
2123
"name": "SHOW VARIABLE RETRY_ABORTS_INTERNALLY",
2224
"executorName": "ClientSideStatementNoParamExecutor",
2325
"resultType": "RESULT_SET",
26+
"statementType": "SHOW_RETRY_ABORTS_INTERNALLY",
2427
"regex": "(?is)\\A\\s*show\\s+variable\\s+retry_aborts_internally\\s*\\z",
2528
"method": "statementShowRetryAbortsInternally",
2629
"exampleStatements": ["show variable retry_aborts_internally"],
@@ -30,6 +33,7 @@
3033
"name": "SHOW VARIABLE AUTOCOMMIT_DML_MODE",
3134
"executorName": "ClientSideStatementNoParamExecutor",
3235
"resultType": "RESULT_SET",
36+
"statementType": "SHOW_AUTOCOMMIT_DML_MODE",
3337
"regex": "(?is)\\A\\s*show\\s+variable\\s+autocommit_dml_mode\\s*\\z",
3438
"method": "statementShowAutocommitDmlMode",
3539
"exampleStatements": ["show variable autocommit_dml_mode"]
@@ -38,6 +42,7 @@
3842
"name": "SHOW VARIABLE STATEMENT_TIMEOUT",
3943
"executorName": "ClientSideStatementNoParamExecutor",
4044
"resultType": "RESULT_SET",
45+
"statementType": "SHOW_STATEMENT_TIMEOUT",
4146
"regex": "(?is)\\A\\s*show\\s+variable\\s+statement_timeout\\s*\\z",
4247
"method": "statementShowStatementTimeout",
4348
"exampleStatements": ["show variable statement_timeout"]
@@ -46,6 +51,7 @@
4651
"name": "SHOW VARIABLE READ_TIMESTAMP",
4752
"executorName": "ClientSideStatementNoParamExecutor",
4853
"resultType": "RESULT_SET",
54+
"statementType": "SHOW_READ_TIMESTAMP",
4955
"regex": "(?is)\\A\\s*show\\s+variable\\s+read_timestamp\\s*\\z",
5056
"method": "statementShowReadTimestamp",
5157
"exampleStatements": ["show variable read_timestamp"],
@@ -55,6 +61,7 @@
5561
"name": "SHOW VARIABLE COMMIT_TIMESTAMP",
5662
"executorName": "ClientSideStatementNoParamExecutor",
5763
"resultType": "RESULT_SET",
64+
"statementType": "SHOW_COMMIT_TIMESTAMP",
5865
"regex": "(?is)\\A\\s*show\\s+variable\\s+commit_timestamp\\s*\\z",
5966
"method": "statementShowCommitTimestamp",
6067
"exampleStatements": ["show variable commit_timestamp"],
@@ -64,6 +71,7 @@
6471
"name": "SHOW VARIABLE READ_ONLY_STALENESS",
6572
"executorName": "ClientSideStatementNoParamExecutor",
6673
"resultType": "RESULT_SET",
74+
"statementType": "SHOW_READ_ONLY_STALENESS",
6775
"regex": "(?is)\\A\\s*show\\s+variable\\s+read_only_staleness\\s*\\z",
6876
"method": "statementShowReadOnlyStaleness",
6977
"exampleStatements": ["show variable read_only_staleness"]
@@ -72,6 +80,7 @@
7280
"name": "SHOW VARIABLE OPTIMIZER_VERSION",
7381
"executorName": "ClientSideStatementNoParamExecutor",
7482
"resultType": "RESULT_SET",
83+
"statementType": "SHOW_OPTIMIZER_VERSION",
7584
"regex": "(?is)\\A\\s*show\\s+variable\\s+optimizer_version\\s*\\z",
7685
"method": "statementShowOptimizerVersion",
7786
"exampleStatements": ["show variable optimizer_version"]
@@ -80,6 +89,7 @@
8089
"name": "SHOW VARIABLE OPTIMIZER_STATISTICS_PACKAGE",
8190
"executorName": "ClientSideStatementNoParamExecutor",
8291
"resultType": "RESULT_SET",
92+
"statementType": "SHOW_OPTIMIZER_STATISTICS_PACKAGE",
8393
"regex": "(?is)\\A\\s*show\\s+variable\\s+optimizer_statistics_package\\s*\\z",
8494
"method": "statementShowOptimizerStatisticsPackage",
8595
"exampleStatements": ["show variable optimizer_statistics_package"]
@@ -88,6 +98,7 @@
8898
"name": "SHOW VARIABLE RETURN_COMMIT_STATS",
8999
"executorName": "ClientSideStatementNoParamExecutor",
90100
"resultType": "RESULT_SET",
101+
"statementType": "SHOW_RETURN_COMMIT_STATS",
91102
"regex": "(?is)\\A\\s*show\\s+variable\\s+return_commit_stats\\s*\\z",
92103
"method": "statementShowReturnCommitStats",
93104
"exampleStatements": ["show variable return_commit_stats"]
@@ -96,6 +107,7 @@
96107
"name": "SHOW VARIABLE COMMIT_RESPONSE",
97108
"executorName": "ClientSideStatementNoParamExecutor",
98109
"resultType": "RESULT_SET",
110+
"statementType": "SHOW_COMMIT_RESPONSE",
99111
"regex": "(?is)\\A\\s*show\\s+variable\\s+commit_response\\s*\\z",
100112
"method": "statementShowCommitResponse",
101113
"exampleStatements": ["show variable commit_response"],
@@ -105,6 +117,7 @@
105117
"name": "SHOW VARIABLE STATEMENT_TAG",
106118
"executorName": "ClientSideStatementNoParamExecutor",
107119
"resultType": "RESULT_SET",
120+
"statementType": "SHOW_STATEMENT_TAG",
108121
"regex": "(?is)\\A\\s*show\\s+variable\\s+statement_tag\\s*\\z",
109122
"method": "statementShowStatementTag",
110123
"exampleStatements": ["show variable statement_tag"]
@@ -113,6 +126,7 @@
113126
"name": "SHOW VARIABLE TRANSACTION_TAG",
114127
"executorName": "ClientSideStatementNoParamExecutor",
115128
"resultType": "RESULT_SET",
129+
"statementType": "SHOW_TRANSACTION_TAG",
116130
"regex": "(?is)\\A\\s*show\\s+variable\\s+transaction_tag\\s*\\z",
117131
"method": "statementShowTransactionTag",
118132
"exampleStatements": ["show variable transaction_tag"]
@@ -121,6 +135,7 @@
121135
"name": "SHOW VARIABLE RPC_PRIORITY",
122136
"executorName": "ClientSideStatementNoParamExecutor",
123137
"resultType": "RESULT_SET",
138+
"statementType": "SHOW_TRANSACTION_TAG",
124139
"regex": "(?is)\\A\\s*show\\s+variable\\s+rpc_priority\\s*\\z",
125140
"method": "statementShowRPCPriority",
126141
"exampleStatements": ["show variable rpc_priority"]
@@ -129,6 +144,7 @@
129144
"name": "BEGIN TRANSACTION",
130145
"executorName": "ClientSideStatementNoParamExecutor",
131146
"resultType": "NO_RESULT",
147+
"statementType": "BEGIN",
132148
"regex": "(?is)\\A\\s*(?:begin|start)(?:\\s+transaction)?\\s*\\z",
133149
"method": "statementBeginTransaction",
134150
"exampleStatements": ["begin", "start", "begin transaction", "start transaction"]
@@ -137,6 +153,7 @@
137153
"name": "COMMIT TRANSACTION",
138154
"executorName": "ClientSideStatementNoParamExecutor",
139155
"resultType": "NO_RESULT",
156+
"statementType": "COMMIT",
140157
"regex": "(?is)\\A\\s*(?:commit)(?:\\s+transaction)?\\s*\\z",
141158
"method": "statementCommit",
142159
"exampleStatements": ["commit", "commit transaction"],
@@ -146,6 +163,7 @@
146163
"name": "ROLLBACK TRANSACTION",
147164
"executorName": "ClientSideStatementNoParamExecutor",
148165
"resultType": "NO_RESULT",
166+
"statementType": "ROLLBACK",
149167
"regex": "(?is)\\A\\s*(?:rollback)(?:\\s+transaction)?\\s*\\z",
150168
"method": "statementRollback",
151169
"exampleStatements": ["rollback", "rollback transaction"],
@@ -155,6 +173,7 @@
155173
"name": "START BATCH DDL",
156174
"executorName": "ClientSideStatementNoParamExecutor",
157175
"resultType": "NO_RESULT",
176+
"statementType": "START_BATCH_DDL",
158177
"regex": "(?is)\\A\\s*(?:start)(?:\\s+batch)(?:\\s+ddl)\\s*\\z",
159178
"method": "statementStartBatchDdl",
160179
"exampleStatements": ["start batch ddl"]
@@ -163,6 +182,7 @@
163182
"name": "START BATCH DML",
164183
"executorName": "ClientSideStatementNoParamExecutor",
165184
"resultType": "NO_RESULT",
185+
"statementType": "START_BATCH_DML",
166186
"regex": "(?is)\\A\\s*(?:start)(?:\\s+batch)(?:\\s+dml)\\s*\\z",
167187
"method": "statementStartBatchDml",
168188
"exampleStatements": ["start batch dml"]
@@ -171,6 +191,7 @@
171191
"name": "RUN BATCH",
172192
"executorName": "ClientSideStatementNoParamExecutor",
173193
"resultType": "NO_RESULT",
194+
"statementType": "RUN_BATCH",
174195
"regex": "(?is)\\A\\s*(?:run)(?:\\s+batch)\\s*\\z",
175196
"method": "statementRunBatch",
176197
"exampleStatements": ["run batch"],
@@ -180,6 +201,7 @@
180201
"name": "ABORT BATCH",
181202
"executorName": "ClientSideStatementNoParamExecutor",
182203
"resultType": "NO_RESULT",
204+
"statementType": "ABORT_BATCH",
183205
"regex": "(?is)\\A\\s*(?:abort)(?:\\s+batch)\\s*\\z",
184206
"method": "statementAbortBatch",
185207
"exampleStatements": ["abort batch"],
@@ -189,6 +211,7 @@
189211
"name": "SET AUTOCOMMIT = TRUE|FALSE",
190212
"executorName": "ClientSideStatementSetExecutor",
191213
"resultType": "NO_RESULT",
214+
"statementType": "SET_AUTOCOMMIT",
192215
"regex": "(?is)\\A\\s*set\\s+autocommit\\s*(?:=)\\s*(.*)\\z",
193216
"method": "statementSetAutocommit",
194217
"exampleStatements": ["set autocommit = true", "set autocommit = false"],
@@ -203,6 +226,7 @@
203226
"name": "SET READONLY = TRUE|FALSE",
204227
"executorName": "ClientSideStatementSetExecutor",
205228
"resultType": "NO_RESULT",
229+
"statementType": "SET_READONLY",
206230
"regex": "(?is)\\A\\s*set\\s+readonly\\s*(?:=)\\s*(.*)\\z",
207231
"method": "statementSetReadOnly",
208232
"exampleStatements": ["set readonly = true", "set readonly = false"],
@@ -217,6 +241,7 @@
217241
"name": "SET RETRY_ABORTS_INTERNALLY = TRUE|FALSE",
218242
"executorName": "ClientSideStatementSetExecutor",
219243
"resultType": "NO_RESULT",
244+
"statementType": "SET_RETRY_ABORTS_INTERNALLY",
220245
"regex": "(?is)\\A\\s*set\\s+retry_aborts_internally\\s*(?:=)\\s*(.*)\\z",
221246
"method": "statementSetRetryAbortsInternally",
222247
"exampleStatements": ["set retry_aborts_internally = true", "set retry_aborts_internally = false"],
@@ -232,6 +257,7 @@
232257
"name": "SET AUTOCOMMIT_DML_MODE = 'PARTITIONED_NON_ATOMIC'|'TRANSACTIONAL'",
233258
"executorName": "ClientSideStatementSetExecutor",
234259
"resultType": "NO_RESULT",
260+
"statementType": "SET_AUTOCOMMIT_DML_MODE",
235261
"regex": "(?is)\\A\\s*set\\s+autocommit_dml_mode\\s*(?:=)\\s*(.*)\\z",
236262
"method": "statementSetAutocommitDmlMode",
237263
"exampleStatements": ["set autocommit_dml_mode='PARTITIONED_NON_ATOMIC'", "set autocommit_dml_mode='TRANSACTIONAL'"],
@@ -246,6 +272,7 @@
246272
"name": "SET STATEMENT_TIMEOUT = '<duration>'|NULL",
247273
"executorName": "ClientSideStatementSetExecutor",
248274
"resultType": "NO_RESULT",
275+
"statementType": "SET_STATEMENT_TIMEOUT",
249276
"regex": "(?is)\\A\\s*set\\s+statement_timeout\\s*(?:=)\\s*(.*)\\z",
250277
"method": "statementSetStatementTimeout",
251278
"exampleStatements": ["set statement_timeout=null", "set statement_timeout='1s'", "set statement_timeout='100ms'", "set statement_timeout='10000us'", "set statement_timeout='9223372036854775807ns'"],
@@ -260,6 +287,7 @@
260287
"name": "SET TRANSACTION READ ONLY|READ WRITE",
261288
"executorName": "ClientSideStatementSetExecutor",
262289
"resultType": "NO_RESULT",
290+
"statementType": "SET_TRANSACTION_MODE",
263291
"regex": "(?is)\\A\\s*set\\s+transaction\\s*(?:\\s+)\\s*(.*)\\z",
264292
"method": "statementSetTransactionMode",
265293
"exampleStatements": ["set transaction read only", "set transaction read write"],
@@ -275,6 +303,7 @@
275303
"name": "SET READ_ONLY_STALENESS = 'STRONG' | 'MIN_READ_TIMESTAMP <timestamp>' | 'READ_TIMESTAMP <timestamp>' | 'MAX_STALENESS <int64>s|ms|us|ns' | 'EXACT_STALENESS (<int64>s|ms|us|ns)'",
276304
"executorName": "ClientSideStatementSetExecutor",
277305
"resultType": "NO_RESULT",
306+
"statementType": "SET_READ_ONLY_STALENESS",
278307
"regex": "(?is)\\A\\s*set\\s+read_only_staleness\\s*(?:=)\\s*(.*)\\z",
279308
"method": "statementSetReadOnlyStaleness",
280309
"exampleStatements": ["set read_only_staleness='STRONG'",
@@ -303,6 +332,7 @@
303332
"name": "SET OPTIMIZER_VERSION = '<version>'|'LATEST'|''",
304333
"executorName": "ClientSideStatementSetExecutor",
305334
"resultType": "NO_RESULT",
335+
"statementType": "SET_OPTIMIZER_VERSION",
306336
"regex": "(?is)\\A\\s*set\\s+optimizer_version\\s*(?:=)\\s*(.*)\\z",
307337
"method": "statementSetOptimizerVersion",
308338
"exampleStatements": ["set optimizer_version='1'", "set optimizer_version='200'", "set optimizer_version='LATEST'", "set optimizer_version=''"],
@@ -317,6 +347,7 @@
317347
"name": "SET OPTIMIZER_STATISTICS_PACKAGE = '<package>'|''",
318348
"executorName": "ClientSideStatementSetExecutor",
319349
"resultType": "NO_RESULT",
350+
"statementType": "SET_OPTIMIZER_STATISTICS_PACKAGE",
320351
"regex": "(?is)\\A\\s*set\\s+optimizer_statistics_package\\s*(?:=)\\s*(.*)\\z",
321352
"method": "statementSetOptimizerStatisticsPackage",
322353
"exampleStatements": ["set optimizer_statistics_package='auto_20191128_14_47_22UTC'", "set optimizer_statistics_package=''"],
@@ -331,6 +362,7 @@
331362
"name": "SET RETURN_COMMIT_STATS = TRUE|FALSE",
332363
"executorName": "ClientSideStatementSetExecutor",
333364
"resultType": "NO_RESULT",
365+
"statementType": "SET_RETURN_COMMIT_STATS",
334366
"regex": "(?is)\\A\\s*set\\s+return_commit_stats\\s*(?:=)\\s*(.*)\\z",
335367
"method": "statementSetReturnCommitStats",
336368
"exampleStatements": ["set return_commit_stats = true", "set return_commit_stats = false"],
@@ -345,6 +377,7 @@
345377
"name": "SET STATEMENT_TAG = '<tag>'",
346378
"executorName": "ClientSideStatementSetExecutor",
347379
"resultType": "NO_RESULT",
380+
"statementType": "SET_STATEMENT_TAG",
348381
"regex": "(?is)\\A\\s*set\\s+statement_tag\\s*(?:=)\\s*(.*)\\z",
349382
"method": "statementSetStatementTag",
350383
"exampleStatements": ["set statement_tag='tag1'", "set statement_tag='tag2'", "set statement_tag=''"],
@@ -359,6 +392,7 @@
359392
"name": "SET TRANSACTION_TAG = '<tag>'",
360393
"executorName": "ClientSideStatementSetExecutor",
361394
"resultType": "NO_RESULT",
395+
"statementType": "SET_TRANSACTION_TAG",
362396
"regex": "(?is)\\A\\s*set\\s+transaction_tag\\s*(?:=)\\s*(.*)\\z",
363397
"method": "statementSetTransactionTag",
364398
"exampleStatements": ["set transaction_tag='tag1'", "set transaction_tag='tag2'", "set transaction_tag=''"],
@@ -374,6 +408,7 @@
374408
"name": "SET RPC_PRIORITY = 'HIGH'|'MEDIUM'|'LOW'|'NULL'",
375409
"executorName": "ClientSideStatementSetExecutor",
376410
"resultType": "NO_RESULT",
411+
"statementType": "SET_RPC_PRIORITY",
377412
"regex": "(?is)\\A\\s*set\\s+rpc_priority\\s*(?:=)\\s*(.*)\\z",
378413
"method": "statementSetRPCPriority",
379414
"exampleStatements": [

0 commit comments

Comments
 (0)