Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ResultSet#get(...) methods should auto convert values #143

Merged
merged 6 commits into from Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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