-
Notifications
You must be signed in to change notification settings - Fork 46
/
JdbcStatement.java
424 lines (387 loc) · 14.6 KB
/
JdbcStatement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.spanner.jdbc;
import com.google.cloud.spanner.Options;
import com.google.cloud.spanner.ResultSets;
import com.google.cloud.spanner.SpannerBatchUpdateException;
import com.google.cloud.spanner.SpannerException;
import com.google.cloud.spanner.Statement;
import com.google.cloud.spanner.Type;
import com.google.cloud.spanner.Type.StructField;
import com.google.cloud.spanner.connection.StatementResult;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.rpc.Code;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/** Implementation of {@link java.sql.Statement} for Google Cloud Spanner. */
class JdbcStatement extends AbstractJdbcStatement {
enum BatchType {
NONE,
DML,
DDL
}
private ResultSet currentResultSet;
private long currentUpdateCount;
private int fetchSize;
private BatchType currentBatchType = BatchType.NONE;
final List<Statement> batchedStatements = new ArrayList<>();
JdbcStatement(JdbcConnection connection) throws SQLException {
super(connection);
}
@Override
public ResultSet executeQuery(String sql) throws SQLException {
checkClosed();
return executeQuery(Statement.of(sql));
}
/**
* @see java.sql.Statement#executeUpdate(String)
* <p>This method allows both DML and DDL statements to be executed. It assumes that the user
* knows what kind of statement is being executed, and the method will therefore return 0 for
* both DML statements that changed 0 rows as well as for all DDL statements.
*/
@Override
public int executeUpdate(String sql) throws SQLException {
long result = executeLargeUpdate(sql);
if (result > Integer.MAX_VALUE) {
throw JdbcSqlExceptionFactory.of("update count too large: " + result, Code.OUT_OF_RANGE);
}
return (int) result;
}
/**
* @see java.sql.Statement#executeLargeUpdate(String)
* <p>This method allows both DML and DDL statements to be executed. It assumes that the user
* knows what kind of statement is being executed, and the method will therefore return 0 for
* both DML statements that changed 0 rows as well as for all DDL statements.
*/
@Override
public long executeLargeUpdate(String sql) throws SQLException {
checkClosed();
Statement statement = Statement.of(sql);
StatementResult result = execute(statement);
switch (result.getResultType()) {
case RESULT_SET:
throw JdbcSqlExceptionFactory.of(
"The statement is not a non-returning DML or DDL statement", Code.INVALID_ARGUMENT);
case UPDATE_COUNT:
return result.getUpdateCount();
case NO_RESULT:
return 0L;
default:
throw JdbcSqlExceptionFactory.of(
"unknown result: " + result.getResultType(), Code.FAILED_PRECONDITION);
}
}
@Override
public boolean execute(String sql) throws SQLException {
checkClosed();
return executeStatement(Statement.of(sql));
}
boolean executeStatement(Statement statement) throws SQLException {
StatementResult result = execute(statement);
switch (result.getResultType()) {
case RESULT_SET:
currentResultSet = JdbcResultSet.of(this, result.getResultSet());
currentUpdateCount = JdbcConstants.STATEMENT_RESULT_SET;
return true;
case UPDATE_COUNT:
currentResultSet = null;
currentUpdateCount = result.getUpdateCount();
return false;
case NO_RESULT:
currentResultSet = null;
currentUpdateCount = JdbcConstants.STATEMENT_NO_RESULT;
return false;
default:
throw JdbcSqlExceptionFactory.of(
"unknown result: " + result.getResultType(), Code.FAILED_PRECONDITION);
}
}
@Override
public ResultSet getResultSet() throws SQLException {
checkClosed();
return currentResultSet;
}
/**
* Returns the update count of the last update statement. Will return {@link
* JdbcConstants#STATEMENT_RESULT_SET} if the last statement returned a {@link ResultSet} and will
* return {@link JdbcConstants#STATEMENT_NO_RESULT} if the last statement did not have any return
* value, such as for example DDL statements.
*/
@Override
public int getUpdateCount() throws SQLException {
checkClosed();
if (currentUpdateCount > Integer.MAX_VALUE) {
throw JdbcSqlExceptionFactory.of(
"update count too large: " + currentUpdateCount, Code.OUT_OF_RANGE);
}
return (int) currentUpdateCount;
}
/**
* Returns the update count of the last update statement as a {@link Long}. Will return {@link
* JdbcConstants#STATEMENT_RESULT_SET} if the last statement returned a {@link ResultSet} and will
* return {@link JdbcConstants#STATEMENT_NO_RESULT} if the last statement did not have any return
* value, such as for example DDL statements.
*/
@Override
public long getLargeUpdateCount() throws SQLException {
checkClosed();
return currentUpdateCount;
}
@Override
public boolean getMoreResults() throws SQLException {
checkClosed();
return getMoreResults(CLOSE_CURRENT_RESULT);
}
@Override
public boolean getMoreResults(int current) throws SQLException {
checkClosed();
if (currentResultSet != null
&& !currentResultSet.isClosed()
&& (current == CLOSE_CURRENT_RESULT || current == CLOSE_ALL_RESULTS)) {
currentResultSet.close();
}
currentResultSet = null;
currentUpdateCount = -1L;
return false;
}
/** This value is set as the value for {@link Options#prefetchChunks(int)} */
@Override
public void setFetchSize(int rows) throws SQLException {
checkClosed();
this.fetchSize = rows;
}
/** This value is set as the value for {@link Options#prefetchChunks(int)} */
@Override
public int getFetchSize() throws SQLException {
checkClosed();
return fetchSize;
}
/**
* Determine the batch type (DML/DDL) based on the sql statement.
*
* @throws SQLException if the sql statement is not allowed for batching.
*/
private BatchType determineStatementBatchType(String sql) throws SQLException {
String sqlWithoutComments = parser.removeCommentsAndTrim(sql);
if (parser.isDdlStatement(sqlWithoutComments)) {
return BatchType.DDL;
} else if (parser.isUpdateStatement(sqlWithoutComments)) {
return BatchType.DML;
}
throw JdbcSqlExceptionFactory.of(
"The statement is not suitable for batching. Only DML and DDL statements are allowed for batching.",
Code.INVALID_ARGUMENT);
}
/**
* Check that the sql statement is of the same type as the current batch on this statement. If
* there is no active batch on this statement, a batch will be started with the type that is
* determined from the sql statement (DML/DDL).
*
* @throws SQLException if the sql statement is of a different type than the already active batch
* on this statement, if the statement is not allowed for batching (i.e. it is a query or a
* client side statement) or if the connection of this statement has an active batch.
*/
void checkAndSetBatchType(String sql) throws SQLException {
checkConnectionHasNoActiveBatch();
BatchType type = determineStatementBatchType(sql);
if (this.currentBatchType == BatchType.NONE) {
this.currentBatchType = type;
} else if (this.currentBatchType != type) {
throw JdbcSqlExceptionFactory.of(
"Mixing DML and DDL statements in a batch is not allowed.", Code.INVALID_ARGUMENT);
}
}
private void checkConnectionHasNoActiveBatch() throws SQLException {
if (getConnection().getSpannerConnection().isDdlBatchActive()
|| getConnection().getSpannerConnection().isDmlBatchActive()) {
throw JdbcSqlExceptionFactory.of(
"Calling addBatch() is not allowed when a DML or DDL batch has been started on the connection.",
Code.FAILED_PRECONDITION);
}
}
@Override
public void addBatch(String sql) throws SQLException {
checkClosed();
checkAndSetBatchType(sql);
batchedStatements.add(Statement.of(sql));
}
@Override
public void clearBatch() throws SQLException {
checkClosed();
checkConnectionHasNoActiveBatch();
batchedStatements.clear();
this.currentBatchType = BatchType.NONE;
}
@Override
public int[] executeBatch() throws SQLException {
return convertUpdateCounts(executeBatch(false));
}
public long[] executeLargeBatch() throws SQLException {
return executeBatch(true);
}
private long[] executeBatch(boolean large) throws SQLException {
checkClosed();
checkConnectionHasNoActiveBatch();
StatementTimeout originalTimeout = setTemporaryStatementTimeout();
try {
switch (this.currentBatchType) {
case DML:
try {
return getConnection().getSpannerConnection().executeBatchUpdate(batchedStatements);
} catch (SpannerBatchUpdateException e) {
if (large) {
throw JdbcSqlExceptionFactory.batchException(e.getUpdateCounts(), e);
} else {
throw JdbcSqlExceptionFactory.batchException(
convertUpdateCounts(e.getUpdateCounts()), e);
}
} catch (SpannerException e) {
throw JdbcSqlExceptionFactory.of(e);
}
case DDL:
try {
getConnection().getSpannerConnection().startBatchDdl();
for (Statement statement : batchedStatements) {
execute(statement);
}
getConnection().getSpannerConnection().runBatch();
long[] res = new long[batchedStatements.size()];
Arrays.fill(res, java.sql.Statement.SUCCESS_NO_INFO);
return res;
} catch (SpannerBatchUpdateException e) {
long[] res = new long[batchedStatements.size()];
Arrays.fill(res, java.sql.Statement.EXECUTE_FAILED);
convertUpdateCountsToSuccessNoInfo(e.getUpdateCounts(), res);
if (large) {
throw JdbcSqlExceptionFactory.batchException(res, e);
} else {
throw JdbcSqlExceptionFactory.batchException(convertUpdateCounts(res), e);
}
} catch (SpannerException e) {
throw JdbcSqlExceptionFactory.of(e);
}
case NONE:
// There is no batch on this statement, this is a no-op.
return new long[0];
default:
throw JdbcSqlExceptionFactory.unsupported(
String.format("Unknown batch type: %s", this.currentBatchType.name()));
}
} finally {
resetStatementTimeout(originalTimeout);
batchedStatements.clear();
this.currentBatchType = BatchType.NONE;
}
}
@VisibleForTesting
int[] convertUpdateCounts(long[] updateCounts) throws SQLException {
int[] res = new int[updateCounts.length];
for (int index = 0; index < updateCounts.length; index++) {
if (updateCounts[index] > Integer.MAX_VALUE) {
throw JdbcSqlExceptionFactory.of(
String.format("Update count too large for int: %d", updateCounts[index]),
Code.OUT_OF_RANGE);
}
res[index] = (int) updateCounts[index];
}
return res;
}
@VisibleForTesting
void convertUpdateCountsToSuccessNoInfo(long[] updateCounts, long[] res) {
Preconditions.checkNotNull(updateCounts);
Preconditions.checkNotNull(res);
Preconditions.checkArgument(res.length >= updateCounts.length);
for (int index = 0; index < updateCounts.length; index++) {
if (updateCounts[index] > 0L) {
res[index] = java.sql.Statement.SUCCESS_NO_INFO;
} else {
res[index] = java.sql.Statement.EXECUTE_FAILED;
}
}
}
@Override
public ResultSet getGeneratedKeys() throws SQLException {
checkClosed();
// Return an empty result set instead of throwing an exception, to facilitate any application
// that might not check on beforehand whether the driver supports any generated keys.
com.google.cloud.spanner.ResultSet rs =
ResultSets.forRows(
Type.struct(
StructField.of("COLUMN_NAME", Type.string()),
StructField.of("VALUE", Type.int64())),
Collections.emptyList());
return JdbcResultSet.of(rs);
}
@Override
public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
checkClosed();
JdbcPreconditions.checkSqlFeatureSupported(
autoGeneratedKeys == java.sql.Statement.NO_GENERATED_KEYS,
JdbcConnection.ONLY_NO_GENERATED_KEYS);
return executeUpdate(sql);
}
@Override
public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
checkClosed();
return executeUpdate(sql);
}
@Override
public int executeUpdate(String sql, String[] columnNames) throws SQLException {
checkClosed();
return executeUpdate(sql);
}
@Override
public long executeLargeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
checkClosed();
JdbcPreconditions.checkSqlFeatureSupported(
autoGeneratedKeys == java.sql.Statement.NO_GENERATED_KEYS,
JdbcConnection.ONLY_NO_GENERATED_KEYS);
return executeLargeUpdate(sql);
}
@Override
public long executeLargeUpdate(String sql, int[] columnIndexes) throws SQLException {
checkClosed();
return executeLargeUpdate(sql);
}
@Override
public long executeLargeUpdate(String sql, String[] columnNames) throws SQLException {
checkClosed();
return executeLargeUpdate(sql);
}
@Override
public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
checkClosed();
JdbcPreconditions.checkSqlFeatureSupported(
autoGeneratedKeys == java.sql.Statement.NO_GENERATED_KEYS,
JdbcConnection.ONLY_NO_GENERATED_KEYS);
return execute(sql);
}
@Override
public boolean execute(String sql, int[] columnIndexes) throws SQLException {
checkClosed();
return execute(sql);
}
@Override
public boolean execute(String sql, String[] columnNames) throws SQLException {
checkClosed();
return execute(sql);
}
}