Skip to content

Commit

Permalink
fix: ResultSet#get(...) methods should auto convert values (#143)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
olavloite committed Jun 9, 2020
1 parent 537fc47 commit bc7d5bd
Show file tree
Hide file tree
Showing 9 changed files with 1,272 additions and 106 deletions.
122 changes: 122 additions & 0 deletions src/main/java/com/google/cloud/spanner/jdbc/AbstractJdbcWrapper.java
Expand Up @@ -22,9 +22,11 @@
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 {
Expand Down Expand Up @@ -147,6 +149,126 @@ static float checkedCastToFloat(double val) throws SQLException {
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);
}
}

/**
* 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;

Expand Down

0 comments on commit bc7d5bd

Please sign in to comment.