/
AbstractJdbcWrapper.java
366 lines (334 loc) · 14.2 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
/*
* 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.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);
if (type.equals(Type.bool())) return Types.BOOLEAN;
if (type.equals(Type.bytes())) return Types.BINARY;
if (type.equals(Type.date())) return Types.DATE;
if (type.equals(Type.float64())) return Types.DOUBLE;
if (type.equals(Type.int64())) return Types.BIGINT;
if (type.equals(Type.numeric())) return Types.NUMERIC;
if (type.equals(Type.string())) return Types.NVARCHAR;
if (type.equals(Type.timestamp())) return Types.TIMESTAMP;
if (type.getCode() == Code.ARRAY) return Types.ARRAY;
return Types.OTHER;
}
/** Extract Spanner type name from {@link java.sql.Types} code. */
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. */
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);
if (type == Type.bool()) return Boolean.class.getName();
if (type == Type.bytes()) return byte[].class.getName();
if (type == Type.date()) return Date.class.getName();
if (type == Type.float64()) return Double.class.getName();
if (type == Type.int64()) return Long.class.getName();
if (type == Type.numeric()) return BigDecimal.class.getName();
if (type == Type.string()) return String.class.getName();
if (type == Type.timestamp()) return Timestamp.class.getName();
if (type.getCode() == Code.ARRAY) {
if (type.getArrayElementType() == Type.bool()) return Boolean[].class.getName();
if (type.getArrayElementType() == Type.bytes()) return byte[][].class.getName();
if (type.getArrayElementType() == Type.date()) return Date[].class.getName();
if (type.getArrayElementType() == Type.float64()) return Double[].class.getName();
if (type.getArrayElementType() == Type.int64()) return Long[].class.getName();
if (type.getArrayElementType() == Type.numeric()) return BigDecimal[].class.getName();
if (type.getArrayElementType() == Type.string()) return String[].class.getName();
if (type.getArrayElementType() == Type.timestamp()) return Timestamp[].class.getName();
}
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 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 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 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.valueOf(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);
}
}
/**
* 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.valueOf(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);
}
}
/** Should return true if this object has been closed */
public abstract boolean isClosed() throws SQLException;
/** 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) throws SQLException {
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);
}
}