diff --git a/src/main/java/com/google/cloud/spanner/jdbc/JdbcPreparedStatement.java b/src/main/java/com/google/cloud/spanner/jdbc/JdbcPreparedStatement.java index 3ab302ce9..0b7939880 100644 --- a/src/main/java/com/google/cloud/spanner/jdbc/JdbcPreparedStatement.java +++ b/src/main/java/com/google/cloud/spanner/jdbc/JdbcPreparedStatement.java @@ -16,6 +16,7 @@ package com.google.cloud.spanner.jdbc; +import com.google.cloud.spanner.Dialect; import com.google.cloud.spanner.Options.QueryOption; import com.google.cloud.spanner.ReadContext.QueryAnalyzeMode; import com.google.cloud.spanner.ResultSets; @@ -36,7 +37,6 @@ class JdbcPreparedStatement extends AbstractJdbcPreparedStatement { private static final char POS_PARAM_CHAR = '?'; private final String sql; - private final String sqlWithoutComments; private final ParametersInfo parameters; private final ImmutableList generatedKeysColumns; @@ -46,9 +46,15 @@ class JdbcPreparedStatement extends AbstractJdbcPreparedStatement { super(connection); this.sql = sql; try { - this.sqlWithoutComments = parser.removeCommentsAndTrim(this.sql); + // The PostgreSQL parser allows comments to be present in the SQL string that is used to parse + // the query parameters. + String sqlForParameterExtraction = + getConnection().getDialect() == Dialect.POSTGRESQL + ? this.sql + : parser.removeCommentsAndTrim(this.sql); this.parameters = - parser.convertPositionalParametersToNamedParameters(POS_PARAM_CHAR, sqlWithoutComments); + parser.convertPositionalParametersToNamedParameters( + POS_PARAM_CHAR, sqlForParameterExtraction); } catch (SpannerException e) { throw JdbcSqlExceptionFactory.of(e); } diff --git a/src/test/java/com/google/cloud/spanner/jdbc/ExecuteMockServerTest.java b/src/test/java/com/google/cloud/spanner/jdbc/ExecuteMockServerTest.java index b62449eb5..65a54a89a 100644 --- a/src/test/java/com/google/cloud/spanner/jdbc/ExecuteMockServerTest.java +++ b/src/test/java/com/google/cloud/spanner/jdbc/ExecuteMockServerTest.java @@ -23,10 +23,12 @@ import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; +import com.google.cloud.spanner.Dialect; import com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult; import com.google.cloud.spanner.SessionPoolOptions; import com.google.cloud.spanner.connection.AbstractMockServerTest; import com.google.cloud.spanner.connection.ConnectionOptions; +import com.google.cloud.spanner.connection.SpannerPool; import com.google.longrunning.Operation; import com.google.protobuf.Any; import com.google.protobuf.Empty; @@ -50,27 +52,62 @@ import org.junit.Test; import org.junit.function.ThrowingRunnable; import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; /** * Test class for verifying that the methods execute, executeQuery, and executeUpdate work as - * intended. + * intended. It also verifies that they always also include any comments in the statement for the + * PostgreSQL dialect, as these may contain hints. */ -@RunWith(JUnit4.class) +@RunWith(Parameterized.class) public class ExecuteMockServerTest extends AbstractMockServerTest { - private static final String QUERY = "select * from my_table"; - private static final String DML = "insert into my_table (id, value) values (1, 'One')"; - private static final String DML_THEN_RETURN_ID = DML + "\nTHEN RETURN `id`"; - private static final String LARGE_DML = "update my_table set value='new value' where true"; - private static final String LARGE_DML_THEN_RETURN_ID = LARGE_DML + "\nTHEN RETURN `id`"; - private static final String DML_RETURNING = - "insert into my_table (id, value) values (1, 'One') THEN RETURN *"; + + @Parameters(name = "dialect = {0}") + public static Object[] parameters() { + return Dialect.values(); + } + + @Parameter public Dialect dialect; + private static final String DDL = "create table my_table"; private static final long LARGE_UPDATE_COUNT = 2L * Integer.MAX_VALUE; + private String query; + private String dml; + private String largeDml; + private String dmlReturning; + @Before public void setupResults() { + query = + dialect == Dialect.POSTGRESQL + ? "/*@ lock_scanned_ranges = exclusive */ select * from my_table" + : "select * from my_table"; + dml = + dialect == Dialect.POSTGRESQL + ? "/*@ lock_scanned_ranges = exclusive */ insert into my_table (id, value) values (1, 'One')" + : "insert into my_table (id, value) values (1, 'One')"; + String DML_THEN_RETURN_ID = + dml + (dialect == Dialect.POSTGRESQL ? "\nRETURNING \"id\"" : "\nTHEN RETURN `id`"); + largeDml = + dialect == Dialect.POSTGRESQL + ? "/*@ lock_scanned_ranges = exclusive */ update my_table set value='new value' where true" + : "update my_table set value='new value' where true"; + String LARGE_DML_THEN_RETURN_ID = + largeDml + (dialect == Dialect.POSTGRESQL ? "\nRETURNING \"id\"" : "\nTHEN RETURN `id`"); + dmlReturning = + dialect == Dialect.POSTGRESQL + ? "/*@ lock_scanned_ranges = exclusive */ insert into my_table (id, value) values (1, 'One') RETURNING *" + : "insert into my_table (id, value) values (1, 'One') THEN RETURN *"; + + // This forces a refresh of the Spanner instance that is used for a connection, which again is + // needed in order to refresh the dialect of the database. + SpannerPool.closeSpannerPool(); + mockSpanner.putStatementResult(StatementResult.detectDialectResult(dialect)); super.setupResults(); + com.google.spanner.v1.ResultSet resultSet = com.google.spanner.v1.ResultSet.newBuilder() .setMetadata( @@ -114,15 +151,15 @@ public void setupResults() { .build()) .build(); mockSpanner.putStatementResult( - StatementResult.query(com.google.cloud.spanner.Statement.of(QUERY), resultSet)); + StatementResult.query(com.google.cloud.spanner.Statement.of(query), resultSet)); mockSpanner.putStatementResult( - StatementResult.update(com.google.cloud.spanner.Statement.of(DML), 1L)); + StatementResult.update(com.google.cloud.spanner.Statement.of(dml), 1L)); mockSpanner.putStatementResult( StatementResult.update( - com.google.cloud.spanner.Statement.of(LARGE_DML), LARGE_UPDATE_COUNT)); + com.google.cloud.spanner.Statement.of(largeDml), LARGE_UPDATE_COUNT)); mockSpanner.putStatementResult( StatementResult.query( - com.google.cloud.spanner.Statement.of(DML_RETURNING), + com.google.cloud.spanner.Statement.of(dmlReturning), resultSet .toBuilder() .setStats(ResultSetStats.newBuilder().setRowCountExact(1L).build()) @@ -163,14 +200,14 @@ private Connection createConnection() throws SQLException { public void testStatementExecuteQuery() throws SQLException { try (Connection connection = createConnection(); Statement statement = connection.createStatement()) { - try (ResultSet resultSet = statement.executeQuery(QUERY)) { + try (ResultSet resultSet = statement.executeQuery(query)) { verifyResultSet(resultSet); } - try (ResultSet resultSet = statement.executeQuery(DML_RETURNING)) { + try (ResultSet resultSet = statement.executeQuery(dmlReturning)) { verifyResultSet(resultSet); } - verifyException(() -> statement.executeQuery(DML)); - verifyException(() -> statement.executeQuery(LARGE_DML)); + verifyException(() -> statement.executeQuery(dml)); + verifyException(() -> statement.executeQuery(largeDml)); verifyException(() -> statement.executeQuery(DDL)); } } @@ -179,11 +216,11 @@ public void testStatementExecuteQuery() throws SQLException { public void testStatementExecuteUpdate() throws SQLException { try (Connection connection = createConnection(); Statement statement = connection.createStatement()) { - assertEquals(1, statement.executeUpdate(DML)); + assertEquals(1, statement.executeUpdate(dml)); assertEquals(0, statement.executeUpdate(DDL)); - verifyOverflow(() -> statement.executeUpdate(LARGE_DML)); - verifyException(() -> statement.executeUpdate(QUERY)); - verifyException(() -> statement.executeUpdate(DML_RETURNING)); + verifyOverflow(() -> statement.executeUpdate(largeDml)); + verifyException(() -> statement.executeUpdate(query)); + verifyException(() -> statement.executeUpdate(dmlReturning)); } } @@ -192,11 +229,11 @@ public void testStatementExecuteUpdateReturnGeneratedKeys() throws SQLException try (Connection connection = createConnection(); Statement statement = connection.createStatement()) { // TODO: Add tests for RETURN_GENERATED_KEYS when that is supported. - assertEquals(1, statement.executeUpdate(DML, Statement.NO_GENERATED_KEYS)); + assertEquals(1, statement.executeUpdate(dml, Statement.NO_GENERATED_KEYS)); assertEquals(0, statement.executeUpdate(DDL, Statement.NO_GENERATED_KEYS)); - verifyOverflow(() -> statement.executeUpdate(LARGE_DML, Statement.NO_GENERATED_KEYS)); - verifyException(() -> statement.executeUpdate(QUERY, Statement.NO_GENERATED_KEYS)); - verifyException(() -> statement.executeUpdate(DML_RETURNING, Statement.NO_GENERATED_KEYS)); + verifyOverflow(() -> statement.executeUpdate(largeDml, Statement.NO_GENERATED_KEYS)); + verifyException(() -> statement.executeUpdate(query, Statement.NO_GENERATED_KEYS)); + verifyException(() -> statement.executeUpdate(dmlReturning, Statement.NO_GENERATED_KEYS)); } } @@ -204,12 +241,12 @@ public void testStatementExecuteUpdateReturnGeneratedKeys() throws SQLException public void testStatementExecuteUpdateReturnColumnNames() throws SQLException { try (Connection connection = createConnection(); Statement statement = connection.createStatement()) { - assertEquals(1, statement.executeUpdate(DML, new String[] {"id"})); + assertEquals(1, statement.executeUpdate(dml, new String[] {"id"})); assertEquals(0, statement.executeUpdate(DDL, new String[] {"id"})); - verifyOverflow(() -> statement.executeUpdate(LARGE_DML, new String[] {"id"})); + verifyOverflow(() -> statement.executeUpdate(largeDml, new String[] {"id"})); verifyException( - () -> statement.executeUpdate(QUERY, new String[] {"id"}), Code.FAILED_PRECONDITION); - assertEquals(1, statement.executeUpdate(DML_RETURNING, new String[] {"id"})); + () -> statement.executeUpdate(query, new String[] {"id"}), Code.FAILED_PRECONDITION); + assertEquals(1, statement.executeUpdate(dmlReturning, new String[] {"id"})); } } @@ -217,11 +254,11 @@ public void testStatementExecuteUpdateReturnColumnNames() throws SQLException { public void testStatementExecuteUpdateReturnColumnIndexes() throws SQLException { try (Connection connection = createConnection(); Statement statement = connection.createStatement()) { - assertEquals(1, statement.executeUpdate(DML, new int[] {1})); + assertEquals(1, statement.executeUpdate(dml, new int[] {1})); assertEquals(0, statement.executeUpdate(DDL, new int[] {1})); - verifyOverflow(() -> statement.executeUpdate(LARGE_DML, new int[] {1})); - verifyException(() -> statement.executeUpdate(QUERY, new int[] {1})); - verifyException(() -> statement.executeUpdate(DML_RETURNING, new int[] {1})); + verifyOverflow(() -> statement.executeUpdate(largeDml, new int[] {1})); + verifyException(() -> statement.executeUpdate(query, new int[] {1})); + verifyException(() -> statement.executeUpdate(dmlReturning, new int[] {1})); } } @@ -229,11 +266,11 @@ public void testStatementExecuteUpdateReturnColumnIndexes() throws SQLException public void testStatementLargeExecuteUpdate() throws SQLException { try (Connection connection = createConnection(); Statement statement = connection.createStatement()) { - assertEquals(1L, statement.executeLargeUpdate(DML)); + assertEquals(1L, statement.executeLargeUpdate(dml)); assertEquals(0L, statement.executeLargeUpdate(DDL)); - assertEquals(LARGE_UPDATE_COUNT, statement.executeLargeUpdate(LARGE_DML)); - verifyException(() -> statement.executeLargeUpdate(QUERY)); - verifyException(() -> statement.executeLargeUpdate(DML_RETURNING)); + assertEquals(LARGE_UPDATE_COUNT, statement.executeLargeUpdate(largeDml)); + verifyException(() -> statement.executeLargeUpdate(query)); + verifyException(() -> statement.executeLargeUpdate(dmlReturning)); } } @@ -242,13 +279,13 @@ public void testStatementExecuteLargeUpdateReturnGeneratedKeys() throws SQLExcep try (Connection connection = createConnection(); Statement statement = connection.createStatement()) { // TODO: Add tests for RETURN_GENERATED_KEYS when that is supported. - assertEquals(1, statement.executeLargeUpdate(DML, Statement.NO_GENERATED_KEYS)); + assertEquals(1, statement.executeLargeUpdate(dml, Statement.NO_GENERATED_KEYS)); assertEquals(0, statement.executeLargeUpdate(DDL, Statement.NO_GENERATED_KEYS)); assertEquals( - LARGE_UPDATE_COUNT, statement.executeLargeUpdate(LARGE_DML, Statement.NO_GENERATED_KEYS)); - verifyException(() -> statement.executeLargeUpdate(QUERY, Statement.NO_GENERATED_KEYS)); + LARGE_UPDATE_COUNT, statement.executeLargeUpdate(largeDml, Statement.NO_GENERATED_KEYS)); + verifyException(() -> statement.executeLargeUpdate(query, Statement.NO_GENERATED_KEYS)); verifyException( - () -> statement.executeLargeUpdate(DML_RETURNING, Statement.NO_GENERATED_KEYS)); + () -> statement.executeLargeUpdate(dmlReturning, Statement.NO_GENERATED_KEYS)); } } @@ -256,13 +293,12 @@ public void testStatementExecuteLargeUpdateReturnGeneratedKeys() throws SQLExcep public void testStatementExecuteLargeUpdateReturnColumnNames() throws SQLException { try (Connection connection = createConnection(); Statement statement = connection.createStatement()) { - assertEquals(1, statement.executeLargeUpdate(DML, new String[] {"id"})); + assertEquals(1, statement.executeLargeUpdate(dml, new String[] {"id"})); assertEquals(0, statement.executeLargeUpdate(DDL, new String[] {"id"})); - assertEquals( - LARGE_UPDATE_COUNT, statement.executeLargeUpdate(LARGE_DML, new String[] {"id"})); + assertEquals(LARGE_UPDATE_COUNT, statement.executeLargeUpdate(largeDml, new String[] {"id"})); verifyException( - () -> statement.executeLargeUpdate(QUERY, new String[] {"id"}), Code.FAILED_PRECONDITION); - assertEquals(1L, statement.executeLargeUpdate(DML_RETURNING, new String[] {"id"})); + () -> statement.executeLargeUpdate(query, new String[] {"id"}), Code.FAILED_PRECONDITION); + assertEquals(1L, statement.executeLargeUpdate(dmlReturning, new String[] {"id"})); } } @@ -270,11 +306,11 @@ public void testStatementExecuteLargeUpdateReturnColumnNames() throws SQLExcepti public void testStatementExecuteLargeUpdateReturnColumnIndexes() throws SQLException { try (Connection connection = createConnection(); Statement statement = connection.createStatement()) { - assertEquals(1, statement.executeLargeUpdate(DML, new int[] {1})); + assertEquals(1, statement.executeLargeUpdate(dml, new int[] {1})); assertEquals(0, statement.executeLargeUpdate(DDL, new int[] {1})); - assertEquals(LARGE_UPDATE_COUNT, statement.executeLargeUpdate(LARGE_DML, new int[] {1})); - verifyException(() -> statement.executeLargeUpdate(QUERY, new int[] {1})); - verifyException(() -> statement.executeLargeUpdate(DML_RETURNING, new int[] {1})); + assertEquals(LARGE_UPDATE_COUNT, statement.executeLargeUpdate(largeDml, new int[] {1})); + verifyException(() -> statement.executeLargeUpdate(query, new int[] {1})); + verifyException(() -> statement.executeLargeUpdate(dmlReturning, new int[] {1})); } } @@ -282,11 +318,11 @@ public void testStatementExecuteLargeUpdateReturnColumnIndexes() throws SQLExcep public void testStatementExecute() throws SQLException { try (Connection connection = createConnection(); Statement statement = connection.createStatement()) { - verifyUpdateCount(statement, () -> statement.execute(DML), 1L); - verifyUpdateCount(statement, () -> statement.execute(LARGE_DML), LARGE_UPDATE_COUNT); + verifyUpdateCount(statement, () -> statement.execute(dml), 1L); + verifyUpdateCount(statement, () -> statement.execute(largeDml), LARGE_UPDATE_COUNT); verifyUpdateCount(statement, () -> statement.execute(DDL), Statement.SUCCESS_NO_INFO); - verifyResultSet(statement, () -> statement.execute(QUERY)); - verifyResultSet(statement, () -> statement.execute(DML_RETURNING)); + verifyResultSet(statement, () -> statement.execute(query)); + verifyResultSet(statement, () -> statement.execute(dmlReturning)); } } @@ -295,18 +331,18 @@ public void testStatementExecuteReturnGeneratedKeys() throws SQLException { try (Connection connection = createConnection(); Statement statement = connection.createStatement()) { // TODO: Add tests for RETURN_GENERATED_KEYS when that is supported. - verifyUpdateCount(statement, () -> statement.execute(DML, Statement.NO_GENERATED_KEYS), 1L); + verifyUpdateCount(statement, () -> statement.execute(dml, Statement.NO_GENERATED_KEYS), 1L); verifyUpdateCount( statement, - () -> statement.execute(LARGE_DML, Statement.NO_GENERATED_KEYS), + () -> statement.execute(largeDml, Statement.NO_GENERATED_KEYS), LARGE_UPDATE_COUNT); verifyUpdateCount( statement, () -> statement.execute(DDL, Statement.NO_GENERATED_KEYS), Statement.SUCCESS_NO_INFO); - verifyResultSet(statement, () -> statement.execute(QUERY, Statement.NO_GENERATED_KEYS)); + verifyResultSet(statement, () -> statement.execute(query, Statement.NO_GENERATED_KEYS)); verifyResultSet( - statement, () -> statement.execute(DML_RETURNING, Statement.NO_GENERATED_KEYS)); + statement, () -> statement.execute(dmlReturning, Statement.NO_GENERATED_KEYS)); } } @@ -314,13 +350,13 @@ public void testStatementExecuteReturnGeneratedKeys() throws SQLException { public void testStatementExecuteReturnColumnNames() throws SQLException { try (Connection connection = createConnection(); Statement statement = connection.createStatement()) { - verifyUpdateCount(statement, () -> statement.execute(DML, new String[] {"id"}), 1L); + verifyUpdateCount(statement, () -> statement.execute(dml, new String[] {"id"}), 1L); verifyUpdateCount( - statement, () -> statement.execute(LARGE_DML, new String[] {"id"}), LARGE_UPDATE_COUNT); + statement, () -> statement.execute(largeDml, new String[] {"id"}), LARGE_UPDATE_COUNT); verifyUpdateCount( statement, () -> statement.execute(DDL, new String[] {"id"}), Statement.SUCCESS_NO_INFO); - verifyResultSet(statement, () -> statement.execute(QUERY, new String[] {"id"})); - verifyResultSet(statement, () -> statement.execute(DML_RETURNING, new String[] {"id"})); + verifyResultSet(statement, () -> statement.execute(query, new String[] {"id"})); + verifyResultSet(statement, () -> statement.execute(dmlReturning, new String[] {"id"})); } } @@ -328,27 +364,27 @@ public void testStatementExecuteReturnColumnNames() throws SQLException { public void testStatementExecuteReturnColumnIndexes() throws SQLException { try (Connection connection = createConnection(); Statement statement = connection.createStatement()) { - verifyUpdateCount(statement, () -> statement.execute(DML, new int[] {1}), 1L); + verifyUpdateCount(statement, () -> statement.execute(dml, new int[] {1}), 1L); verifyUpdateCount( - statement, () -> statement.execute(LARGE_DML, new int[] {1}), LARGE_UPDATE_COUNT); + statement, () -> statement.execute(largeDml, new int[] {1}), LARGE_UPDATE_COUNT); verifyUpdateCount( statement, () -> statement.execute(DDL, new int[] {1}), Statement.SUCCESS_NO_INFO); - verifyResultSet(statement, () -> statement.execute(QUERY, new int[] {1})); - verifyResultSet(statement, () -> statement.execute(DML_RETURNING, new int[] {1})); + verifyResultSet(statement, () -> statement.execute(query, new int[] {1})); + verifyResultSet(statement, () -> statement.execute(dmlReturning, new int[] {1})); } } @Test public void testPreparedStatementExecuteQuery() throws SQLException { try (Connection connection = createConnection()) { - try (ResultSet resultSet = connection.prepareStatement(QUERY).executeQuery()) { + try (ResultSet resultSet = connection.prepareStatement(query).executeQuery()) { verifyResultSet(resultSet); } - try (ResultSet resultSet = connection.prepareStatement(DML_RETURNING).executeQuery()) { + try (ResultSet resultSet = connection.prepareStatement(dmlReturning).executeQuery()) { verifyResultSet(resultSet); } - verifyException(() -> connection.prepareStatement(DML).executeQuery()); - verifyException(() -> connection.prepareStatement(LARGE_DML).executeQuery()); + verifyException(() -> connection.prepareStatement(dml).executeQuery()); + verifyException(() -> connection.prepareStatement(largeDml).executeQuery()); verifyException(() -> connection.prepareStatement(DDL).executeQuery()); } } @@ -356,14 +392,14 @@ public void testPreparedStatementExecuteQuery() throws SQLException { @Test public void testPreparedStatementExecuteUpdate() throws SQLException { try (Connection connection = createConnection()) { - assertEquals(1, connection.prepareStatement(DML).executeUpdate()); + assertEquals(1, connection.prepareStatement(dml).executeUpdate()); // TODO: Enable the next statement once PreparedStatement supports executing DDL through the // executeUpdate() method. // assertEquals(0, connection.prepareStatement(DDL).executeUpdate()); - verifyOverflow(() -> connection.prepareStatement(LARGE_DML).executeUpdate()); - verifyException(() -> connection.prepareStatement(QUERY).executeUpdate()); + verifyOverflow(() -> connection.prepareStatement(largeDml).executeUpdate()); + verifyException(() -> connection.prepareStatement(query).executeUpdate()); verifyException( - () -> connection.prepareStatement(DML_RETURNING).executeUpdate(), Code.INVALID_ARGUMENT); + () -> connection.prepareStatement(dmlReturning).executeUpdate(), Code.INVALID_ARGUMENT); } } @@ -372,20 +408,19 @@ public void testPreparedStatementExecuteUpdateReturnGeneratedKeys() throws SQLEx try (Connection connection = createConnection()) { // TODO: Add tests for RETURN_GENERATED_KEYS when that is supported. assertEquals( - 1, connection.prepareStatement(DML, Statement.NO_GENERATED_KEYS).executeUpdate()); + 1, connection.prepareStatement(dml, Statement.NO_GENERATED_KEYS).executeUpdate()); // TODO: Enable the next statement once PreparedStatement supports executing DDL through the // executeUpdate() method. // assertEquals(0, connection.prepareStatement(DDL, // Statement.NO_GENERATED_KEYS).executeUpdate()); verifyOverflow( - () -> - connection.prepareStatement(LARGE_DML, Statement.NO_GENERATED_KEYS).executeUpdate()); + () -> connection.prepareStatement(largeDml, Statement.NO_GENERATED_KEYS).executeUpdate()); verifyException( - () -> connection.prepareStatement(QUERY, Statement.NO_GENERATED_KEYS).executeUpdate()); + () -> connection.prepareStatement(query, Statement.NO_GENERATED_KEYS).executeUpdate()); verifyException( () -> connection - .prepareStatement(DML_RETURNING, Statement.NO_GENERATED_KEYS) + .prepareStatement(dmlReturning, Statement.NO_GENERATED_KEYS) .executeUpdate(), Code.INVALID_ARGUMENT); } @@ -394,31 +429,31 @@ public void testPreparedStatementExecuteUpdateReturnGeneratedKeys() throws SQLEx @Test public void testPreparedStatementExecuteUpdateReturnColumnNames() throws SQLException { try (Connection connection = createConnection()) { - assertEquals(1, connection.prepareStatement(DML, new String[] {"id"}).executeUpdate()); + assertEquals(1, connection.prepareStatement(dml, new String[] {"id"}).executeUpdate()); // TODO: Enable the next statement once PreparedStatement supports executing DDL through the // executeUpdate() method. // assertEquals(0, connection.prepareStatement(DDL, new String[] {"id"}).executeUpdate()); verifyOverflow( - () -> connection.prepareStatement(LARGE_DML, new String[] {"id"}).executeUpdate()); + () -> connection.prepareStatement(largeDml, new String[] {"id"}).executeUpdate()); verifyException( - () -> connection.prepareStatement(QUERY, new String[] {"id"}).executeUpdate(), + () -> connection.prepareStatement(query, new String[] {"id"}).executeUpdate(), Code.FAILED_PRECONDITION); assertEquals( - 1, connection.prepareStatement(DML_RETURNING, new String[] {"id"}).executeUpdate()); + 1, connection.prepareStatement(dmlReturning, new String[] {"id"}).executeUpdate()); } } @Test public void testPreparedStatementExecuteUpdateReturnColumnIndexes() throws SQLException { try (Connection connection = createConnection()) { - assertEquals(1, connection.prepareStatement(DML, new int[] {1}).executeUpdate()); + assertEquals(1, connection.prepareStatement(dml, new int[] {1}).executeUpdate()); // TODO: Enable the next statement once PreparedStatement supports executing DDL through the // executeUpdate() method. // assertEquals(0, connection.prepareStatement(DDL, new int[] {1}).executeUpdate()); - verifyOverflow(() -> connection.prepareStatement(LARGE_DML, new int[] {1}).executeUpdate()); - verifyException(() -> connection.prepareStatement(QUERY, new int[] {1}).executeUpdate()); + verifyOverflow(() -> connection.prepareStatement(largeDml, new int[] {1}).executeUpdate()); + verifyException(() -> connection.prepareStatement(query, new int[] {1}).executeUpdate()); verifyException( - () -> connection.prepareStatement(DML_RETURNING, new int[] {1}).executeUpdate(), + () -> connection.prepareStatement(dmlReturning, new int[] {1}).executeUpdate(), Code.INVALID_ARGUMENT); } } @@ -426,14 +461,14 @@ public void testPreparedStatementExecuteUpdateReturnColumnIndexes() throws SQLEx @Test public void testPreparedStatementLargeExecuteUpdate() throws SQLException { try (Connection connection = createConnection()) { - assertEquals(1L, connection.prepareStatement(DML).executeLargeUpdate()); + assertEquals(1L, connection.prepareStatement(dml).executeLargeUpdate()); // TODO: Enable the next statement once PreparedStatement supports executing DDL through the // executeUpdate() method. // assertEquals(0L, connection.prepareStatement(DDL).executeLargeUpdate()); - assertEquals(LARGE_UPDATE_COUNT, connection.prepareStatement(LARGE_DML).executeLargeUpdate()); - verifyException(() -> connection.prepareStatement(QUERY).executeLargeUpdate()); + assertEquals(LARGE_UPDATE_COUNT, connection.prepareStatement(largeDml).executeLargeUpdate()); + verifyException(() -> connection.prepareStatement(query).executeLargeUpdate()); verifyException( - () -> connection.prepareStatement(DML_RETURNING).executeLargeUpdate(), + () -> connection.prepareStatement(dmlReturning).executeLargeUpdate(), Code.INVALID_ARGUMENT); } } @@ -443,21 +478,21 @@ public void testPreparedStatementExecuteLargeUpdateReturnGeneratedKeys() throws try (Connection connection = createConnection()) { // TODO: Add tests for RETURN_GENERATED_KEYS when that is supported. assertEquals( - 1, connection.prepareStatement(DML, Statement.NO_GENERATED_KEYS).executeLargeUpdate()); + 1, connection.prepareStatement(dml, Statement.NO_GENERATED_KEYS).executeLargeUpdate()); // TODO: Enable the next statement once PreparedStatement supports executing DDL through the // executeUpdate() method. // assertEquals(0, connection.prepareStatement(DDL, // Statement.NO_GENERATED_KEYS).executeLargeUpdate()); assertEquals( LARGE_UPDATE_COUNT, - connection.prepareStatement(LARGE_DML, Statement.NO_GENERATED_KEYS).executeLargeUpdate()); + connection.prepareStatement(largeDml, Statement.NO_GENERATED_KEYS).executeLargeUpdate()); verifyException( () -> - connection.prepareStatement(QUERY, Statement.NO_GENERATED_KEYS).executeLargeUpdate()); + connection.prepareStatement(query, Statement.NO_GENERATED_KEYS).executeLargeUpdate()); verifyException( () -> connection - .prepareStatement(DML_RETURNING, Statement.NO_GENERATED_KEYS) + .prepareStatement(dmlReturning, Statement.NO_GENERATED_KEYS) .executeLargeUpdate(), Code.INVALID_ARGUMENT); } @@ -466,35 +501,35 @@ public void testPreparedStatementExecuteLargeUpdateReturnGeneratedKeys() throws @Test public void testPreparedStatementExecuteLargeUpdateReturnColumnNames() throws SQLException { try (Connection connection = createConnection()) { - assertEquals(1, connection.prepareStatement(DML, new String[] {"id"}).executeLargeUpdate()); + assertEquals(1, connection.prepareStatement(dml, new String[] {"id"}).executeLargeUpdate()); // TODO: Enable the next statement once PreparedStatement supports executing DDL through the // executeUpdate() method. // assertEquals(0, connection.prepareStatement(DDL, new String[] // {"id"}).executeLargeUpdate()); assertEquals( LARGE_UPDATE_COUNT, - connection.prepareStatement(LARGE_DML, new String[] {"id"}).executeLargeUpdate()); + connection.prepareStatement(largeDml, new String[] {"id"}).executeLargeUpdate()); verifyException( - () -> connection.prepareStatement(QUERY, new String[] {"id"}).executeLargeUpdate(), + () -> connection.prepareStatement(query, new String[] {"id"}).executeLargeUpdate(), Code.FAILED_PRECONDITION); assertEquals( - 1L, connection.prepareStatement(DML_RETURNING, new String[] {"id"}).executeLargeUpdate()); + 1L, connection.prepareStatement(dmlReturning, new String[] {"id"}).executeLargeUpdate()); } } @Test public void testPreparedStatementExecuteLargeUpdateReturnColumnIndexes() throws SQLException { try (Connection connection = createConnection()) { - assertEquals(1, connection.prepareStatement(DML, new int[] {1}).executeLargeUpdate()); + assertEquals(1, connection.prepareStatement(dml, new int[] {1}).executeLargeUpdate()); // TODO: Enable the next statement once PreparedStatement supports executing DDL through the // executeUpdate() method. // assertEquals(0, connection.prepareStatement(DDL, new int[] {1}).executeLargeUpdate()); assertEquals( LARGE_UPDATE_COUNT, - connection.prepareStatement(LARGE_DML, new int[] {1}).executeLargeUpdate()); - verifyException(() -> connection.prepareStatement(QUERY, new int[] {1}).executeLargeUpdate()); + connection.prepareStatement(largeDml, new int[] {1}).executeLargeUpdate()); + verifyException(() -> connection.prepareStatement(query, new int[] {1}).executeLargeUpdate()); verifyException( - () -> connection.prepareStatement(DML_RETURNING, new int[] {1}).executeLargeUpdate(), + () -> connection.prepareStatement(dmlReturning, new int[] {1}).executeLargeUpdate(), Code.INVALID_ARGUMENT); } } @@ -502,14 +537,14 @@ public void testPreparedStatementExecuteLargeUpdateReturnColumnIndexes() throws @Test public void testPreparedStatementExecute() throws SQLException { try (Connection connection = createConnection()) { - verifyPreparedUpdateCount(connection.prepareStatement(DML), PreparedStatement::execute, 1L); + verifyPreparedUpdateCount(connection.prepareStatement(dml), PreparedStatement::execute, 1L); verifyPreparedUpdateCount( - connection.prepareStatement(LARGE_DML), PreparedStatement::execute, LARGE_UPDATE_COUNT); + connection.prepareStatement(largeDml), PreparedStatement::execute, LARGE_UPDATE_COUNT); verifyPreparedUpdateCount( connection.prepareStatement(DDL), PreparedStatement::execute, Statement.SUCCESS_NO_INFO); - verifyPreparedResultSet(connection.prepareStatement(QUERY), PreparedStatement::execute); + verifyPreparedResultSet(connection.prepareStatement(query), PreparedStatement::execute); verifyPreparedResultSet( - connection.prepareStatement(DML_RETURNING), PreparedStatement::execute); + connection.prepareStatement(dmlReturning), PreparedStatement::execute); } } @@ -518,11 +553,11 @@ public void testPreparedStatementExecuteReturnGeneratedKeys() throws SQLExceptio try (Connection connection = createConnection()) { // TODO: Add tests for RETURN_GENERATED_KEYS when that is supported. verifyPreparedUpdateCount( - connection.prepareStatement(DML, Statement.NO_GENERATED_KEYS), + connection.prepareStatement(dml, Statement.NO_GENERATED_KEYS), PreparedStatement::execute, 1L); verifyPreparedUpdateCount( - connection.prepareStatement(LARGE_DML, Statement.NO_GENERATED_KEYS), + connection.prepareStatement(largeDml, Statement.NO_GENERATED_KEYS), PreparedStatement::execute, LARGE_UPDATE_COUNT); verifyPreparedUpdateCount( @@ -530,10 +565,10 @@ public void testPreparedStatementExecuteReturnGeneratedKeys() throws SQLExceptio PreparedStatement::execute, Statement.SUCCESS_NO_INFO); verifyPreparedResultSet( - connection.prepareStatement(QUERY, Statement.NO_GENERATED_KEYS), + connection.prepareStatement(query, Statement.NO_GENERATED_KEYS), PreparedStatement::execute); verifyPreparedResultSet( - connection.prepareStatement(DML_RETURNING, Statement.NO_GENERATED_KEYS), + connection.prepareStatement(dmlReturning, Statement.NO_GENERATED_KEYS), PreparedStatement::execute); } } @@ -542,9 +577,9 @@ public void testPreparedStatementExecuteReturnGeneratedKeys() throws SQLExceptio public void testPreparedStatementExecuteReturnColumnNames() throws SQLException { try (Connection connection = createConnection()) { verifyPreparedUpdateCount( - connection.prepareStatement(DML, new String[] {"id"}), PreparedStatement::execute, 1L); + connection.prepareStatement(dml, new String[] {"id"}), PreparedStatement::execute, 1L); verifyPreparedUpdateCount( - connection.prepareStatement(LARGE_DML, new String[] {"id"}), + connection.prepareStatement(largeDml, new String[] {"id"}), PreparedStatement::execute, LARGE_UPDATE_COUNT); verifyPreparedUpdateCount( @@ -552,9 +587,9 @@ public void testPreparedStatementExecuteReturnColumnNames() throws SQLException PreparedStatement::execute, Statement.SUCCESS_NO_INFO); verifyPreparedResultSet( - connection.prepareStatement(QUERY, new String[] {"id"}), PreparedStatement::execute); + connection.prepareStatement(query, new String[] {"id"}), PreparedStatement::execute); verifyPreparedResultSet( - connection.prepareStatement(DML_RETURNING, new String[] {"id"}), + connection.prepareStatement(dmlReturning, new String[] {"id"}), PreparedStatement::execute); } } @@ -563,9 +598,9 @@ public void testPreparedStatementExecuteReturnColumnNames() throws SQLException public void testPreparedStatementExecuteReturnColumnIndexes() throws SQLException { try (Connection connection = createConnection()) { verifyPreparedUpdateCount( - connection.prepareStatement(DML, new int[] {1}), PreparedStatement::execute, 1L); + connection.prepareStatement(dml, new int[] {1}), PreparedStatement::execute, 1L); verifyPreparedUpdateCount( - connection.prepareStatement(LARGE_DML, new int[] {1}), + connection.prepareStatement(largeDml, new int[] {1}), PreparedStatement::execute, LARGE_UPDATE_COUNT); verifyPreparedUpdateCount( @@ -573,9 +608,9 @@ public void testPreparedStatementExecuteReturnColumnIndexes() throws SQLExceptio PreparedStatement::execute, Statement.SUCCESS_NO_INFO); verifyPreparedResultSet( - connection.prepareStatement(QUERY, new int[] {1}), PreparedStatement::execute); + connection.prepareStatement(query, new int[] {1}), PreparedStatement::execute); verifyPreparedResultSet( - connection.prepareStatement(DML_RETURNING, new int[] {1}), PreparedStatement::execute); + connection.prepareStatement(dmlReturning, new int[] {1}), PreparedStatement::execute); } } @@ -671,7 +706,7 @@ public void testInvalidExecuteUpdate_shouldNotLeakSession() throws SQLException for (int i = 0; i < (maxSessions + 1); i++) { SQLException exception = assertThrows( - SQLException.class, () -> connection.createStatement().executeUpdate(QUERY)); + SQLException.class, () -> connection.createStatement().executeUpdate(query)); assertTrue(exception instanceof JdbcSqlException); JdbcSqlException jdbcSqlException = (JdbcSqlException) exception; // This would be RESOURCE_EXHAUSTED if the query leaked a session.