Skip to content

Commit

Permalink
feat!: upgrade to Java 8 and JDBC 4.2 (#397)
Browse files Browse the repository at this point in the history
* feat!: upgrade to Java 8 and JDBC 4.2

Upgrade to Java 8 and implement new methods added in Java 8 for JDBC.
Drops support for Java 7.

Closes #396

* fix: remove Java 7 from CI

* test: add additional test cases

* test: add tests for batches

* build: remove java7 configs
  • Loading branch information
olavloite committed Mar 22, 2021
1 parent 8482652 commit 7eedfbc
Show file tree
Hide file tree
Showing 21 changed files with 595 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
java: [7, 8, 11]
java: [8, 11]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v1
Expand Down
7 changes: 0 additions & 7 deletions .kokoro/nightly/java7.cfg

This file was deleted.

7 changes: 0 additions & 7 deletions .kokoro/presubmit/java7.cfg

This file was deleted.

12 changes: 12 additions & 0 deletions pom.xml
Expand Up @@ -197,6 +197,18 @@
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<compilerArgument>-Xlint:unchecked</compilerArgument>
<compilerArgument>-Xlint:deprecation</compilerArgument>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
Expand Down
Expand Up @@ -31,6 +31,7 @@
import java.sql.ResultSet;
import java.sql.RowId;
import java.sql.SQLException;
import java.sql.SQLType;
import java.sql.SQLXML;
import java.sql.Time;
import java.sql.Timestamp;
Expand Down Expand Up @@ -321,6 +322,21 @@ public void setObject(int parameterIndex, Object value, int targetSqlType, int s
parameters.setParameter(parameterIndex, value, targetSqlType, scaleOrLength);
}

@Override
public void setObject(int parameterIndex, Object value, SQLType targetSqlType)
throws SQLException {
checkClosed();
parameters.setParameter(parameterIndex, value, targetSqlType.getVendorTypeNumber());
}

@Override
public void setObject(int parameterIndex, Object value, SQLType targetSqlType, int scaleOrLength)
throws SQLException {
checkClosed();
parameters.setParameter(
parameterIndex, value, targetSqlType.getVendorTypeNumber(), scaleOrLength);
}

@Override
public void setAsciiStream(int parameterIndex, InputStream value, long length)
throws SQLException {
Expand Down
Expand Up @@ -29,6 +29,7 @@
import java.sql.RowId;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLType;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Time;
Expand Down Expand Up @@ -233,11 +234,22 @@ public void updateObject(int columnIndex, Object x, int scaleOrLength) throws SQ
throw new SQLFeatureNotSupportedException();
}

@Override
public void updateObject(int columnIndex, Object x, SQLType type, int scaleOrLength)
throws SQLException {
throw new SQLFeatureNotSupportedException();
}

@Override
public void updateObject(int columnIndex, Object x) throws SQLException {
throw new SQLFeatureNotSupportedException();
}

@Override
public void updateObject(int columnIndex, Object x, SQLType type) throws SQLException {
throw new SQLFeatureNotSupportedException();
}

@Override
public void updateNull(String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException();
Expand Down Expand Up @@ -335,6 +347,17 @@ public void updateObject(String columnLabel, Object x) throws SQLException {
throw new SQLFeatureNotSupportedException();
}

@Override
public void updateObject(String columnLabel, Object x, SQLType type) throws SQLException {
throw new SQLFeatureNotSupportedException();
}

@Override
public void updateObject(String columnLabel, Object x, SQLType type, int scaleOrLength)
throws SQLException {
throw new SQLFeatureNotSupportedException();
}

@Override
public void insertRow() throws SQLException {
throw new SQLFeatureNotSupportedException();
Expand Down
Expand Up @@ -201,20 +201,32 @@ private ResultSet executeQuery(
* Executes a SQL statement on the connection of this {@link Statement} as an update (DML)
* statement.
*
* @param statement The SQL statement to execute.
* @return the number of rows that was inserted/updated/deleted.
* @param statement The SQL statement to execute
* @return the number of rows that was inserted/updated/deleted
* @throws SQLException if a database error occurs, or if the number of rows affected is larger
* than {@link Integer#MAX_VALUE}.
* than {@link Integer#MAX_VALUE}
*/
int executeUpdate(com.google.cloud.spanner.Statement statement) throws SQLException {
long count = executeLargeUpdate(statement);
if (count > Integer.MAX_VALUE) {
throw JdbcSqlExceptionFactory.of(
"update count too large for executeUpdate: " + count, Code.OUT_OF_RANGE);
}
return (int) count;
}

/**
* Executes a SQL statement on the connection of this {@link Statement} as an update (DML)
* statement.
*
* @param statement The SQL statement to execute
* @return the number of rows that was inserted/updated/deleted
* @throws SQLException if a database error occurs
*/
long executeLargeUpdate(com.google.cloud.spanner.Statement statement) throws SQLException {
StatementTimeout originalTimeout = setTemporaryStatementTimeout();
try {
long count = connection.getSpannerConnection().executeUpdate(statement);
if (count > Integer.MAX_VALUE) {
throw JdbcSqlExceptionFactory.of(
"update count too large for executeUpdate: " + count, Code.OUT_OF_RANGE);
}
return (int) count;
return connection.getSpannerConnection().executeUpdate(statement);
} catch (SpannerException e) {
throw JdbcSqlExceptionFactory.of(e);
} finally {
Expand Down Expand Up @@ -357,11 +369,22 @@ public int getMaxRows() throws SQLException {
return 0;
}

@Override
public long getLargeMaxRows() throws SQLException {
checkClosed();
return 0L;
}

@Override
public void setMaxRows(int max) throws SQLException {
checkClosed();
}

@Override
public void setLargeMaxRows(long max) throws SQLException {
checkClosed();
}

@Override
public void setEscapeProcessing(boolean enable) throws SQLException {
checkClosed();
Expand Down
Expand Up @@ -66,7 +66,8 @@ static String getSpannerTypeName(int sqlType) {
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) return Type.timestamp().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;
Expand All @@ -84,7 +85,8 @@ static String getClassName(int sqlType) {
|| 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) return Timestamp.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;
Expand Down
Expand Up @@ -1630,4 +1630,15 @@ public ResultSet getPseudoColumns(
public boolean generatedKeyAlwaysReturned() throws SQLException {
return false;
}

@Override
public long getMaxLogicalLobSize() throws SQLException {
// BYTES(MAX)
return 10485760L;
}

@Override
public boolean supportsRefCursors() throws SQLException {
return false;
}
}
7 changes: 4 additions & 3 deletions src/main/java/com/google/cloud/spanner/jdbc/JdbcDriver.java
Expand Up @@ -106,7 +106,8 @@
*/
public class JdbcDriver implements Driver {
private static final String JDBC_API_CLIENT_LIB_TOKEN = "sp-jdbc";
static final int MAJOR_VERSION = 1;
// Updated to version 2 when upgraded to Java 8 (JDBC 4.2)
static final int MAJOR_VERSION = 2;
static final int MINOR_VERSION = 0;
private static final String JDBC_URL_FORMAT =
"jdbc:" + ConnectionOptions.Builder.SPANNER_URI_FORMAT;
Expand Down Expand Up @@ -245,12 +246,12 @@ private String parseUriProperty(String uri, String property, String defaultValue

@Override
public int getMajorVersion() {
return 1;
return MAJOR_VERSION;
}

@Override
public int getMinorVersion() {
return 0;
return MINOR_VERSION;
}

@Override
Expand Down
Expand Up @@ -204,7 +204,9 @@ private boolean isTypeSupported(int sqlType) {
case Types.LONGNVARCHAR:
case Types.DATE:
case Types.TIME:
case Types.TIME_WITH_TIMEZONE:
case Types.TIMESTAMP:
case Types.TIMESTAMP_WITH_TIMEZONE:
case Types.BINARY:
case Types.VARBINARY:
case Types.LONGVARBINARY:
Expand Down Expand Up @@ -249,7 +251,9 @@ private boolean isValidTypeAndValue(Object value, int sqlType) {
|| value instanceof URL;
case Types.DATE:
case Types.TIME:
case Types.TIME_WITH_TIMEZONE:
case Types.TIMESTAMP:
case Types.TIMESTAMP_WITH_TIMEZONE:
return value instanceof Date || value instanceof Time || value instanceof Timestamp;
case Types.BINARY:
case Types.VARBINARY:
Expand Down Expand Up @@ -522,7 +526,9 @@ private Builder setParamWithKnownType(ValueBinder<Builder> binder, Object value,
}
throw JdbcSqlExceptionFactory.of(value + " is not a valid date", Code.INVALID_ARGUMENT);
case Types.TIME:
case Types.TIME_WITH_TIMEZONE:
case Types.TIMESTAMP:
case Types.TIMESTAMP_WITH_TIMEZONE:
if (value instanceof Date) {
return binder.to(JdbcTypeConverter.toGoogleTimestamp((Date) value));
} else if (value instanceof Time) {
Expand Down Expand Up @@ -715,7 +721,9 @@ private Builder setArrayValue(ValueBinder<Builder> binder, int type, Object valu
case Types.DATE:
return binder.toDateArray((Iterable<com.google.cloud.Date>) null);
case Types.TIME:
case Types.TIME_WITH_TIMEZONE:
case Types.TIMESTAMP:
case Types.TIMESTAMP_WITH_TIMEZONE:
return binder.toTimestampArray((Iterable<com.google.cloud.Timestamp>) null);
case Types.BINARY:
case Types.VARBINARY:
Expand Down Expand Up @@ -843,8 +851,9 @@ private Builder setNullValue(ValueBinder<Builder> binder, Integer sqlType) throw
case Types.SQLXML:
return binder.to((String) null);
case Types.TIME:
return binder.to((com.google.cloud.Timestamp) null);
case Types.TIME_WITH_TIMEZONE:
case Types.TIMESTAMP:
case Types.TIMESTAMP_WITH_TIMEZONE:
return binder.to((com.google.cloud.Timestamp) null);
case Types.TINYINT:
return binder.to((Long) null);
Expand Down
Expand Up @@ -74,6 +74,11 @@ public int executeUpdate() throws SQLException {
return executeUpdate(createStatement());
}

public long executeLargeUpdate() throws SQLException {
checkClosed();
return executeLargeUpdate(createStatement());
}

@Override
public boolean execute() throws SQLException {
checkClosed();
Expand Down
Expand Up @@ -127,6 +127,16 @@ private JdbcSqlBatchUpdateException(int[] updateCounts, SpannerBatchUpdateExcept
this.code = Code.forNumber(cause.getCode());
}

private JdbcSqlBatchUpdateException(long[] updateCounts, SpannerBatchUpdateException cause) {
super(
cause.getMessage(),
cause.getErrorCode().toString(),
cause.getCode(),
updateCounts,
cause);
this.code = Code.forNumber(cause.getCode());
}

@Override
public Code getCode() {
return code;
Expand Down Expand Up @@ -315,4 +325,10 @@ static BatchUpdateException batchException(
int[] updateCounts, SpannerBatchUpdateException cause) {
return new JdbcSqlBatchUpdateException(updateCounts, cause);
}

/** Creates a {@link JdbcSqlException} for large batch update exceptions. */
static BatchUpdateException batchException(
long[] updateCounts, SpannerBatchUpdateException cause) {
return new JdbcSqlBatchUpdateException(updateCounts, cause);
}
}

0 comments on commit 7eedfbc

Please sign in to comment.