Skip to content

Commit bc7d5bd

Browse files
authored
fix: ResultSet#get(...) methods should auto convert values (#143)
* fix: ResultSet#get(...) methods should auto convert values * tests: add additional tests * tests: add more conversion tests * tests: add more timestamp tests * tests: add timestamp tests * review: process review comments
1 parent 537fc47 commit bc7d5bd

File tree

9 files changed

+1272
-106
lines changed

9 files changed

+1272
-106
lines changed

β€Žsrc/main/java/com/google/cloud/spanner/jdbc/AbstractJdbcWrapper.javaβ€Ž

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
import java.sql.Date;
2323
import java.sql.SQLException;
2424
import java.sql.SQLFeatureNotSupportedException;
25+
import java.sql.Time;
2526
import java.sql.Timestamp;
2627
import java.sql.Types;
2728
import java.sql.Wrapper;
29+
import java.util.Calendar;
2830

2931
/** Base class for all Cloud Spanner JDBC classes that implement the {@link Wrapper} interface. */
3032
abstract class AbstractJdbcWrapper implements Wrapper {
@@ -147,6 +149,126 @@ static float checkedCastToFloat(double val) throws SQLException {
147149
return (float) val;
148150
}
149151

152+
/**
153+
* Parses the given string value as a long. Throws {@link SQLException} if the string is not a
154+
* valid long value.
155+
*/
156+
static long parseLong(String val) throws SQLException {
157+
Preconditions.checkNotNull(val);
158+
try {
159+
return Long.valueOf(val);
160+
} catch (NumberFormatException e) {
161+
throw JdbcSqlExceptionFactory.of(
162+
String.format("%s is not a valid number", val), com.google.rpc.Code.INVALID_ARGUMENT, e);
163+
}
164+
}
165+
166+
/**
167+
* Parses the given string value as a double. Throws {@link SQLException} if the string is not a
168+
* valid double value.
169+
*/
170+
static double parseDouble(String val) throws SQLException {
171+
Preconditions.checkNotNull(val);
172+
try {
173+
return Double.valueOf(val);
174+
} catch (NumberFormatException e) {
175+
throw JdbcSqlExceptionFactory.of(
176+
String.format("%s is not a valid number", val), com.google.rpc.Code.INVALID_ARGUMENT, e);
177+
}
178+
}
179+
180+
/**
181+
* Parses the given string value as a {@link Date} value. Throws {@link SQLException} if the
182+
* string is not a valid {@link Date} value.
183+
*/
184+
static Date parseDate(String val) throws SQLException {
185+
Preconditions.checkNotNull(val);
186+
try {
187+
return JdbcTypeConverter.toSqlDate(com.google.cloud.Date.parseDate(val));
188+
} catch (IllegalArgumentException e) {
189+
throw JdbcSqlExceptionFactory.of(
190+
String.format("%s is not a valid date", val), com.google.rpc.Code.INVALID_ARGUMENT, e);
191+
}
192+
}
193+
194+
/**
195+
* Parses the given string value as a {@link Date} value in the timezone of the given {@link
196+
* Calendar}. Throws {@link SQLException} if the string is not a valid {@link Date} value.
197+
*/
198+
static Date parseDate(String val, Calendar cal) throws SQLException {
199+
Preconditions.checkNotNull(val);
200+
Preconditions.checkNotNull(cal);
201+
try {
202+
return JdbcTypeConverter.toSqlDate(com.google.cloud.Date.parseDate(val), cal);
203+
} catch (IllegalArgumentException e) {
204+
throw JdbcSqlExceptionFactory.of(
205+
String.format("%s is not a valid date", val), com.google.rpc.Code.INVALID_ARGUMENT, e);
206+
}
207+
}
208+
209+
/**
210+
* Parses the given string value as a {@link Time} value. Throws {@link SQLException} if the
211+
* string is not a valid {@link Time} value.
212+
*/
213+
static Time parseTime(String val) throws SQLException {
214+
Preconditions.checkNotNull(val);
215+
try {
216+
return Time.valueOf(val);
217+
} catch (IllegalArgumentException e) {
218+
throw JdbcSqlExceptionFactory.of(
219+
String.format("%s is not a valid time", val), com.google.rpc.Code.INVALID_ARGUMENT, e);
220+
}
221+
}
222+
223+
/**
224+
* Parses the given string value as a {@link Time} value in the timezone of the given {@link
225+
* Calendar}. Throws {@link SQLException} if the string is not a valid {@link Time} value.
226+
*/
227+
static Time parseTime(String val, Calendar cal) throws SQLException {
228+
Preconditions.checkNotNull(val);
229+
Preconditions.checkNotNull(cal);
230+
try {
231+
return JdbcTypeConverter.parseSqlTime(val, cal);
232+
} catch (IllegalArgumentException e) {
233+
throw JdbcSqlExceptionFactory.of(
234+
String.format("%s is not a valid time", val), com.google.rpc.Code.INVALID_ARGUMENT, e);
235+
}
236+
}
237+
238+
/**
239+
* Parses the given string value as a {@link Timestamp} value. Throws {@link SQLException} if the
240+
* string is not a valid {@link Timestamp} value.
241+
*/
242+
static Timestamp parseTimestamp(String val) throws SQLException {
243+
Preconditions.checkNotNull(val);
244+
try {
245+
return JdbcTypeConverter.toSqlTimestamp(com.google.cloud.Timestamp.parseTimestamp(val));
246+
} catch (Exception e) {
247+
throw JdbcSqlExceptionFactory.of(
248+
String.format("%s is not a valid timestamp", val),
249+
com.google.rpc.Code.INVALID_ARGUMENT,
250+
e);
251+
}
252+
}
253+
254+
/**
255+
* Parses the given string value as a {@link Timestamp} value in the timezone of the given {@link
256+
* Calendar}. Throws {@link SQLException} if the string is not a valid {@link Timestamp} value.
257+
*/
258+
static Timestamp parseTimestamp(String val, Calendar cal) throws SQLException {
259+
Preconditions.checkNotNull(val);
260+
Preconditions.checkNotNull(cal);
261+
try {
262+
return JdbcTypeConverter.setTimestampInCalendar(
263+
com.google.cloud.Timestamp.parseTimestamp(val).toSqlTimestamp(), cal);
264+
} catch (Exception e) {
265+
throw JdbcSqlExceptionFactory.of(
266+
String.format("%s is not a valid timestamp", val),
267+
com.google.rpc.Code.INVALID_ARGUMENT,
268+
e);
269+
}
270+
}
271+
150272
/** Should return true if this object has been closed */
151273
public abstract boolean isClosed() throws SQLException;
152274

0 commit comments

Comments
 (0)