/
AbstractJdbcWrapper.java
550 lines (511 loc) · 19.1 KB
/
AbstractJdbcWrapper.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*
* 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.Dialect;
import com.google.cloud.spanner.Type;
import com.google.cloud.spanner.Type.Code;
import com.google.common.base.Preconditions;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.sql.Wrapper;
import java.util.Calendar;
/** Base class for all Cloud Spanner JDBC classes that implement the {@link Wrapper} interface. */
abstract class AbstractJdbcWrapper implements Wrapper {
static final String OTHER_NAME = "OTHER";
/**
* Extract {@link java.sql.Types} code from Spanner {@link Type}.
*
* @param type The Cloud Spanner type to convert. May not be <code>null</code>.
*/
static int extractColumnType(Type type) {
Preconditions.checkNotNull(type);
switch (type.getCode()) {
case BOOL:
return Types.BOOLEAN;
case BYTES:
return Types.BINARY;
case DATE:
return Types.DATE;
case FLOAT64:
return Types.DOUBLE;
case INT64:
return Types.BIGINT;
case NUMERIC:
case PG_NUMERIC:
return Types.NUMERIC;
case STRING:
case JSON:
case PG_JSONB:
return Types.NVARCHAR;
case TIMESTAMP:
return Types.TIMESTAMP;
case ARRAY:
return Types.ARRAY;
case STRUCT:
default:
return Types.OTHER;
}
}
static String getSpannerTypeName(Type type, Dialect dialect) {
// TODO: Use com.google.cloud.spanner.Type#getSpannerTypeName() when available.
Preconditions.checkNotNull(type);
switch (type.getCode()) {
case BOOL:
return dialect == Dialect.POSTGRESQL ? "boolean" : "BOOL";
case BYTES:
return dialect == Dialect.POSTGRESQL ? "bytea" : "BYTES";
case DATE:
return dialect == Dialect.POSTGRESQL ? "date" : "DATE";
case FLOAT64:
return dialect == Dialect.POSTGRESQL ? "double precision" : "FLOAT64";
case INT64:
return dialect == Dialect.POSTGRESQL ? "bigint" : "INT64";
case NUMERIC:
return "NUMERIC";
case PG_NUMERIC:
return "numeric";
case STRING:
return dialect == Dialect.POSTGRESQL ? "character varying" : "STRING";
case JSON:
return "JSON";
case PG_JSONB:
return "jsonb";
case TIMESTAMP:
return dialect == Dialect.POSTGRESQL ? "timestamp with time zone" : "TIMESTAMP";
case STRUCT:
return "STRUCT";
case ARRAY:
switch (type.getArrayElementType().getCode()) {
case BOOL:
return dialect == Dialect.POSTGRESQL ? "boolean[]" : "ARRAY<BOOL>";
case BYTES:
return dialect == Dialect.POSTGRESQL ? "bytea[]" : "ARRAY<BYTES>";
case DATE:
return dialect == Dialect.POSTGRESQL ? "date[]" : "ARRAY<DATE>";
case FLOAT64:
return dialect == Dialect.POSTGRESQL ? "double precision[]" : "ARRAY<FLOAT64>";
case INT64:
return dialect == Dialect.POSTGRESQL ? "bigint[]" : "ARRAY<INT64>";
case NUMERIC:
return "ARRAY<NUMERIC>";
case PG_NUMERIC:
return "numeric[]";
case STRING:
return dialect == Dialect.POSTGRESQL ? "character varying[]" : "ARRAY<STRING>";
case JSON:
return "ARRAY<JSON>";
case PG_JSONB:
return "jsonb[]";
case TIMESTAMP:
return dialect == Dialect.POSTGRESQL
? "timestamp with time zone[]"
: "ARRAY<TIMESTAMP>";
case STRUCT:
return "ARRAY<STRUCT>";
}
default:
return null;
}
}
/**
* Extract Spanner type name from {@link java.sql.Types} code.
*
* @deprecated Use {@link #getSpannerTypeName(Type, Dialect)} instead.
*/
@Deprecated
static String getSpannerTypeName(int sqlType) {
if (sqlType == Types.BOOLEAN) return Type.bool().getCode().name();
if (sqlType == Types.BINARY) return Type.bytes().getCode().name();
if (sqlType == Types.DATE) return Type.date().getCode().name();
if (sqlType == Types.DOUBLE || sqlType == Types.FLOAT) return Type.float64().getCode().name();
if (sqlType == Types.BIGINT
|| sqlType == Types.INTEGER
|| sqlType == Types.SMALLINT
|| sqlType == Types.TINYINT) return Type.int64().getCode().name();
if (sqlType == Types.NUMERIC || sqlType == Types.DECIMAL)
return Type.numeric().getCode().name();
if (sqlType == Types.NVARCHAR) return Type.string().getCode().name();
if (sqlType == Types.TIMESTAMP || sqlType == Types.TIMESTAMP_WITH_TIMEZONE)
return Type.timestamp().getCode().name();
if (sqlType == Types.ARRAY) return Code.ARRAY.name();
return OTHER_NAME;
}
/**
* Get corresponding Java class name from {@link java.sql.Types} code.
*
* @deprecated Use {@link #getClassName(Type)} instead.
*/
@Deprecated
static String getClassName(int sqlType) {
if (sqlType == Types.BOOLEAN) return Boolean.class.getName();
if (sqlType == Types.BINARY) return Byte[].class.getName();
if (sqlType == Types.DATE) return Date.class.getName();
if (sqlType == Types.DOUBLE || sqlType == Types.FLOAT) return Double.class.getName();
if (sqlType == Types.BIGINT
|| sqlType == Types.INTEGER
|| sqlType == Types.SMALLINT
|| sqlType == Types.TINYINT) return Long.class.getName();
if (sqlType == Types.NUMERIC || sqlType == Types.DECIMAL) return BigDecimal.class.getName();
if (sqlType == Types.NVARCHAR) return String.class.getName();
if (sqlType == Types.TIMESTAMP || sqlType == Types.TIMESTAMP_WITH_TIMEZONE)
return Timestamp.class.getName();
if (sqlType == Types.ARRAY) return Object.class.getName();
return null;
}
/**
* Get corresponding Java class name from Spanner {@link Type}.
*
* @param type The Cloud Spanner type to convert. May not be <code>null</code>.
*/
static String getClassName(Type type) {
Preconditions.checkNotNull(type);
switch (type.getCode()) {
case BOOL:
return Boolean.class.getName();
case BYTES:
return byte[].class.getName();
case DATE:
return Date.class.getName();
case FLOAT64:
return Double.class.getName();
case INT64:
return Long.class.getName();
case NUMERIC:
case PG_NUMERIC:
return BigDecimal.class.getName();
case STRING:
case JSON:
case PG_JSONB:
return String.class.getName();
case TIMESTAMP:
return Timestamp.class.getName();
case ARRAY:
switch (type.getArrayElementType().getCode()) {
case BOOL:
return Boolean[].class.getName();
case BYTES:
return byte[][].class.getName();
case DATE:
return Date[].class.getName();
case FLOAT64:
return Double[].class.getName();
case INT64:
return Long[].class.getName();
case NUMERIC:
case PG_NUMERIC:
return BigDecimal[].class.getName();
case STRING:
case JSON:
case PG_JSONB:
return String[].class.getName();
case TIMESTAMP:
return Timestamp[].class.getName();
}
case STRUCT:
default:
return null;
}
}
/** Standard error message for out-of-range values. */
private static final String OUT_OF_RANGE_MSG = "Value out of range for %s: %s";
/** Cast value and throw {@link SQLException} if out-of-range. */
static byte checkedCastToByte(long val) throws SQLException {
if (val > Byte.MAX_VALUE || val < Byte.MIN_VALUE) {
throw JdbcSqlExceptionFactory.of(
String.format(OUT_OF_RANGE_MSG, "byte", val), com.google.rpc.Code.OUT_OF_RANGE);
}
return (byte) val;
}
/** Cast value and throw {@link SQLException} if out-of-range. */
static byte checkedCastToByte(BigDecimal val) throws SQLException {
try {
return val.byteValueExact();
} catch (ArithmeticException e) {
throw JdbcSqlExceptionFactory.of(
String.format(OUT_OF_RANGE_MSG, "byte", val), com.google.rpc.Code.OUT_OF_RANGE);
}
}
/** Cast value and throw {@link SQLException} if out-of-range. */
static byte checkedCastToByte(BigInteger val) throws SQLException {
try {
return val.byteValueExact();
} catch (ArithmeticException e) {
throw JdbcSqlExceptionFactory.of(
String.format(OUT_OF_RANGE_MSG, "byte", val), com.google.rpc.Code.OUT_OF_RANGE);
}
}
/** Cast value and throw {@link SQLException} if out-of-range. */
static short checkedCastToShort(long val) throws SQLException {
if (val > Short.MAX_VALUE || val < Short.MIN_VALUE) {
throw JdbcSqlExceptionFactory.of(
String.format(OUT_OF_RANGE_MSG, "short", val), com.google.rpc.Code.OUT_OF_RANGE);
}
return (short) val;
}
/** Cast value and throw {@link SQLException} if out-of-range. */
static short checkedCastToShort(BigDecimal val) throws SQLException {
try {
return val.shortValueExact();
} catch (ArithmeticException e) {
throw JdbcSqlExceptionFactory.of(
String.format(OUT_OF_RANGE_MSG, "short", val), com.google.rpc.Code.OUT_OF_RANGE);
}
}
/** Cast value and throw {@link SQLException} if out-of-range. */
static short checkedCastToShort(BigInteger val) throws SQLException {
try {
return val.shortValueExact();
} catch (ArithmeticException e) {
throw JdbcSqlExceptionFactory.of(
String.format(OUT_OF_RANGE_MSG, "short", val), com.google.rpc.Code.OUT_OF_RANGE);
}
}
/** Cast value and throw {@link SQLException} if out-of-range. */
static int checkedCastToInt(long val) throws SQLException {
if (val > Integer.MAX_VALUE || val < Integer.MIN_VALUE) {
throw JdbcSqlExceptionFactory.of(
String.format(OUT_OF_RANGE_MSG, "int", val), com.google.rpc.Code.OUT_OF_RANGE);
}
return (int) val;
}
/** Cast value and throw {@link SQLException} if out-of-range. */
static int checkedCastToInt(BigDecimal val) throws SQLException {
try {
return val.intValueExact();
} catch (ArithmeticException e) {
throw JdbcSqlExceptionFactory.of(
String.format(OUT_OF_RANGE_MSG, "int", val), com.google.rpc.Code.OUT_OF_RANGE);
}
}
/** Cast value and throw {@link SQLException} if out-of-range. */
static int checkedCastToInt(BigInteger val) throws SQLException {
try {
return val.intValueExact();
} catch (ArithmeticException e) {
throw JdbcSqlExceptionFactory.of(
String.format(OUT_OF_RANGE_MSG, "int", val), com.google.rpc.Code.OUT_OF_RANGE);
}
}
/** Cast value and throw {@link SQLException} if out-of-range. */
static float checkedCastToFloat(double val) throws SQLException {
if (val > Float.MAX_VALUE || val < -Float.MAX_VALUE) {
throw JdbcSqlExceptionFactory.of(
String.format(OUT_OF_RANGE_MSG, "float", val), com.google.rpc.Code.OUT_OF_RANGE);
}
return (float) val;
}
/**
* Parses the given string value as a long. Throws {@link SQLException} if the string is not a
* valid long value.
*/
static long parseLong(String val) throws SQLException {
Preconditions.checkNotNull(val);
try {
return Long.parseLong(val);
} catch (NumberFormatException e) {
throw JdbcSqlExceptionFactory.of(
String.format("%s is not a valid number", val), com.google.rpc.Code.INVALID_ARGUMENT, e);
}
}
/** Cast value and throw {@link SQLException} if out-of-range. */
static BigInteger checkedCastToBigInteger(BigDecimal val) throws SQLException {
try {
return val.toBigIntegerExact();
} catch (ArithmeticException e) {
throw JdbcSqlExceptionFactory.of(
String.format(OUT_OF_RANGE_MSG, "BigInteger", val), com.google.rpc.Code.OUT_OF_RANGE);
}
}
/** Cast value and throw {@link SQLException} if out-of-range. */
static long checkedCastToLong(BigDecimal val) throws SQLException {
try {
return val.longValueExact();
} catch (ArithmeticException e) {
throw JdbcSqlExceptionFactory.of(
String.format(OUT_OF_RANGE_MSG, "long", val), com.google.rpc.Code.OUT_OF_RANGE);
}
}
/** Cast value and throw {@link SQLException} if out-of-range. */
static long checkedCastToLong(BigInteger val) throws SQLException {
try {
return val.longValueExact();
} catch (ArithmeticException e) {
throw JdbcSqlExceptionFactory.of(
String.format(OUT_OF_RANGE_MSG, "long", val), com.google.rpc.Code.OUT_OF_RANGE);
}
}
/**
* Parses the given string value as a double. Throws {@link SQLException} if the string is not a
* valid double value.
*/
static double parseDouble(String val) throws SQLException {
Preconditions.checkNotNull(val);
try {
return Double.parseDouble(val);
} catch (NumberFormatException e) {
throw JdbcSqlExceptionFactory.of(
String.format("%s is not a valid number", val), com.google.rpc.Code.INVALID_ARGUMENT, e);
}
}
/**
* Parses the given string value as a float. Throws {@link SQLException} if the string is not a
* valid float value.
*/
static float parseFloat(String val) throws SQLException {
Preconditions.checkNotNull(val);
try {
return Float.parseFloat(val);
} catch (NumberFormatException e) {
throw JdbcSqlExceptionFactory.of(
String.format("%s is not a valid number", val), com.google.rpc.Code.INVALID_ARGUMENT, e);
}
}
/**
* Parses the given string value as a {@link Date} value. Throws {@link SQLException} if the
* string is not a valid {@link Date} value.
*/
static Date parseDate(String val) throws SQLException {
Preconditions.checkNotNull(val);
try {
return JdbcTypeConverter.toSqlDate(com.google.cloud.Date.parseDate(val));
} catch (IllegalArgumentException e) {
throw JdbcSqlExceptionFactory.of(
String.format("%s is not a valid date", val), com.google.rpc.Code.INVALID_ARGUMENT, e);
}
}
/**
* Parses the given string value as a {@link Date} value in the timezone of the given {@link
* Calendar}. Throws {@link SQLException} if the string is not a valid {@link Date} value.
*/
static Date parseDate(String val, Calendar cal) throws SQLException {
Preconditions.checkNotNull(val);
Preconditions.checkNotNull(cal);
try {
return JdbcTypeConverter.toSqlDate(com.google.cloud.Date.parseDate(val), cal);
} catch (IllegalArgumentException e) {
throw JdbcSqlExceptionFactory.of(
String.format("%s is not a valid date", val), com.google.rpc.Code.INVALID_ARGUMENT, e);
}
}
/**
* Parses the given string value as a {@link Time} value. Throws {@link SQLException} if the
* string is not a valid {@link Time} value.
*/
static Time parseTime(String val) throws SQLException {
Preconditions.checkNotNull(val);
try {
return Time.valueOf(val);
} catch (IllegalArgumentException e) {
throw JdbcSqlExceptionFactory.of(
String.format("%s is not a valid time", val), com.google.rpc.Code.INVALID_ARGUMENT, e);
}
}
/**
* Parses the given string value as a {@link Time} value in the timezone of the given {@link
* Calendar}. Throws {@link SQLException} if the string is not a valid {@link Time} value.
*/
static Time parseTime(String val, Calendar cal) throws SQLException {
Preconditions.checkNotNull(val);
Preconditions.checkNotNull(cal);
try {
return JdbcTypeConverter.parseSqlTime(val, cal);
} catch (IllegalArgumentException e) {
throw JdbcSqlExceptionFactory.of(
String.format("%s is not a valid time", val), com.google.rpc.Code.INVALID_ARGUMENT, e);
}
}
/**
* Parses the given string value as a {@link Timestamp} value. Throws {@link SQLException} if the
* string is not a valid {@link Timestamp} value.
*/
static Timestamp parseTimestamp(String val) throws SQLException {
Preconditions.checkNotNull(val);
try {
return JdbcTypeConverter.toSqlTimestamp(com.google.cloud.Timestamp.parseTimestamp(val));
} catch (Exception e) {
throw JdbcSqlExceptionFactory.of(
String.format("%s is not a valid timestamp", val),
com.google.rpc.Code.INVALID_ARGUMENT,
e);
}
}
/**
* Parses the given string value as a {@link Timestamp} value in the timezone of the given {@link
* Calendar}. Throws {@link SQLException} if the string is not a valid {@link Timestamp} value.
*/
static Timestamp parseTimestamp(String val, Calendar cal) throws SQLException {
Preconditions.checkNotNull(val);
Preconditions.checkNotNull(cal);
try {
return JdbcTypeConverter.setTimestampInCalendar(
com.google.cloud.Timestamp.parseTimestamp(val).toSqlTimestamp(), cal);
} catch (Exception e) {
throw JdbcSqlExceptionFactory.of(
String.format("%s is not a valid timestamp", val),
com.google.rpc.Code.INVALID_ARGUMENT,
e);
}
}
/**
* Parses the given string value as a {@link BigDecimal} value. Throws {@link SQLException} if the
* string is not a valid {@link BigDecimal} value.
*/
static BigDecimal parseBigDecimal(String val) throws SQLException {
Preconditions.checkNotNull(val);
try {
return new BigDecimal(val);
} catch (NumberFormatException e) {
throw JdbcSqlExceptionFactory.of(
String.format("%s is not a valid number", val), com.google.rpc.Code.INVALID_ARGUMENT, e);
}
}
/** Should return true if this object has been closed */
public abstract boolean isClosed();
/** Throws a {@link SQLException} if this object is closed */
void checkClosed() throws SQLException {
if (isClosed()) {
throw JdbcSqlExceptionFactory.of(
"This " + getClass().getName() + " has been closed",
com.google.rpc.Code.FAILED_PRECONDITION);
}
}
/**
* Throws a {@link SQLException} if this object is closed and otherwise a {@link
* SQLFeatureNotSupportedException} with the given message
*/
<T> T checkClosedAndThrowUnsupported(String message) throws SQLException {
checkClosed();
throw JdbcSqlExceptionFactory.unsupported(message);
}
@Override
public boolean isWrapperFor(Class<?> iface) {
return iface != null && iface.isAssignableFrom(getClass());
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
if (isWrapperFor(iface)) {
return iface.cast(this);
}
throw JdbcSqlExceptionFactory.of(
"Cannot unwrap to " + iface.getName(), com.google.rpc.Code.INVALID_ARGUMENT);
}
}