use of com.swiftmq.impl.routing.single.smqpr.v400.CommitRequest in project pgadapter by GoogleCloudPlatform.
the class JdbcSimpleModeMockServerTest method testSelectAtStartOfBatch.
@Test
public void testSelectAtStartOfBatch() throws SQLException {
String sql = "SELECT 1; INSERT INTO FOO VALUES (1); SELECT 2;";
try (Connection connection = DriverManager.getConnection(createUrl())) {
try (Statement statement = connection.createStatement()) {
assertTrue(statement.execute(sql));
try (ResultSet resultSet = statement.getResultSet()) {
assertTrue(resultSet.next());
assertEquals(1L, resultSet.getLong(1));
assertFalse(resultSet.next());
}
// getMoreResults() returns false as the next result is an update count.
assertFalse(statement.getMoreResults());
assertEquals(1, statement.getUpdateCount());
assertTrue(statement.getMoreResults());
try (ResultSet resultSet = statement.getResultSet()) {
assertTrue(resultSet.next());
assertEquals(2L, resultSet.getLong(1));
assertFalse(resultSet.next());
}
// getMoreResults() should now return false. We should also check getUpdateCount() as that
// method should return -1 to indicate that there is also no update count available.
assertFalse(statement.getMoreResults());
assertEquals(-1, statement.getUpdateCount());
}
List<ExecuteSqlRequest> requests = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class);
assertEquals(3, requests.size());
assertEquals(SELECT1.getSql(), requests.get(0).getSql());
assertTrue(requests.get(0).getTransaction().hasBegin());
assertEquals(INSERT_STATEMENT.getSql(), requests.get(1).getSql());
assertTrue(requests.get(1).getTransaction().hasId());
assertEquals(SELECT2.getSql(), requests.get(2).getSql());
assertTrue(requests.get(2).getTransaction().hasId());
List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
assertEquals(1, commitRequests.size());
}
}
use of com.swiftmq.impl.routing.single.smqpr.v400.CommitRequest in project pgadapter by GoogleCloudPlatform.
the class JdbcSimpleModeMockServerTest method testBatchInActiveTransaction.
@Test
public void testBatchInActiveTransaction() throws SQLException {
String sql = String.format("%s; %s; %s;", INSERT_STATEMENT, "COMMIT", SELECT2);
try (Connection connection = DriverManager.getConnection(createUrl())) {
try (java.sql.Statement statement = connection.createStatement()) {
// Start an explicit transaction before executing batch
assertFalse(statement.execute("BEGIN; SELECT 1"));
assertEquals(0, statement.getUpdateCount());
assertTrue(statement.getMoreResults());
try (ResultSet resultSet = statement.getResultSet()) {
assertTrue(resultSet.next());
assertEquals(1L, resultSet.getLong(1));
assertFalse(resultSet.next());
}
assertFalse(statement.getMoreResults());
assertEquals(-1, statement.getUpdateCount());
// Execute Batch
assertFalse(statement.execute(sql));
assertEquals(1, statement.getUpdateCount());
assertFalse(statement.getMoreResults());
assertEquals(0, statement.getUpdateCount());
assertTrue(statement.getMoreResults());
try (ResultSet resultSet = statement.getResultSet()) {
assertTrue(resultSet.next());
assertEquals(2L, resultSet.getLong(1));
assertFalse(resultSet.next());
}
assertFalse(statement.getMoreResults());
assertEquals(-1, statement.getUpdateCount());
}
}
List<ExecuteSqlRequest> requests = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class);
assertEquals(3, requests.size());
assertEquals(SELECT1.getSql(), requests.get(0).getSql());
assertTrue(requests.get(0).getTransaction().hasBegin());
assertEquals(INSERT_STATEMENT.getSql(), requests.get(1).getSql());
assertFalse(requests.get(1).getTransaction().hasBegin());
assertTrue(requests.get(1).getTransaction().hasId());
assertEquals(SELECT2.getSql(), requests.get(2).getSql());
assertTrue(requests.get(2).getTransaction().hasSingleUse());
List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
assertEquals(1, commitRequests.size());
}
use of com.swiftmq.impl.routing.single.smqpr.v400.CommitRequest in project pgadapter by GoogleCloudPlatform.
the class JdbcSimpleModeMockServerTest method testErrorHandlingInImplicitTransaction.
@Test
public void testErrorHandlingInImplicitTransaction() throws SQLException {
String sql = String.format("%s; %s; %s; %s; %s;", INSERT_STATEMENT, "COMMIT", SELECT1, INVALID_SELECT, SELECT2);
try (Connection connection = DriverManager.getConnection(createUrl())) {
try (java.sql.Statement statement = connection.createStatement()) {
SQLException exception = assertThrows(SQLException.class, () -> statement.execute(sql));
assertThat(exception.getMessage(), containsString("INVALID_ARGUMENT: Statement is invalid."));
// Verify that the transaction was rolled back and that the connection is usable.
assertTrue(statement.execute("show transaction isolation level"));
}
}
List<ExecuteSqlRequest> requests = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class);
assertEquals(3, requests.size());
assertEquals(INSERT_STATEMENT.getSql(), requests.get(0).getSql());
assertTrue(requests.get(0).getTransaction().hasBegin());
// The rest of the statements in the batch are all selects, so PGAdapter tries to use a
// read-only transaction.
assertEquals(1, mockSpanner.getRequestsOfType(BeginTransactionRequest.class).size());
assertTrue(mockSpanner.getRequestsOfType(BeginTransactionRequest.class).get(0).getOptions().hasReadOnly());
assertEquals(SELECT1.getSql(), requests.get(1).getSql());
assertTrue(requests.get(1).getTransaction().hasId());
assertEquals(INVALID_SELECT.getSql(), requests.get(2).getSql());
assertTrue(requests.get(2).getTransaction().hasId());
// We get one commit for the read/write transaction. The read-only transaction is not committed
// or rolled back, as that is not necessary for read-only transactions.
List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
assertEquals(1, commitRequests.size());
List<RollbackRequest> rollbackRequests = mockSpanner.getRequestsOfType(RollbackRequest.class);
assertEquals(0, rollbackRequests.size());
}
use of com.swiftmq.impl.routing.single.smqpr.v400.CommitRequest in project pgadapter by GoogleCloudPlatform.
the class JdbcSimpleModeMockServerTest method testBeginAndDml.
@Test
public void testBeginAndDml() throws SQLException {
try (Connection connection = DriverManager.getConnection(createUrl())) {
try (java.sql.Statement statement = connection.createStatement()) {
// batch a BEGIN [TRANSACTION] statement together with an update statement.
assertFalse(statement.execute(String.format("BEGIN; %s;", UPDATE_STATEMENT)));
assertEquals(0, statement.getUpdateCount());
assertFalse(statement.getMoreResults());
assertEquals(2, statement.getUpdateCount());
// getMoreResults() should now return false. We should also check getUpdateCount() as that
// method should return -1 to indicate that there is also no update count available.
assertFalse(statement.getMoreResults());
assertEquals(-1, statement.getUpdateCount());
// Execute an insert statement as part of the transaction.
assertFalse(statement.execute(INSERT_STATEMENT.getSql()));
assertEquals(1, statement.getUpdateCount());
assertFalse(statement.getMoreResults());
assertEquals(-1, statement.getUpdateCount());
statement.execute("COMMIT");
}
}
List<ExecuteSqlRequest> requests = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class);
assertEquals(2, requests.size());
// The first statement in the transaction will include the BeginTransaction option.
assertEquals(UPDATE_STATEMENT.getSql(), requests.get(0).getSql());
assertTrue(requests.get(0).getTransaction().hasBegin());
assertEquals(INSERT_STATEMENT.getSql(), requests.get(1).getSql());
assertTrue(requests.get(1).getTransaction().hasId());
List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
assertEquals(1, commitRequests.size());
}
use of com.swiftmq.impl.routing.single.smqpr.v400.CommitRequest in project pgadapter by GoogleCloudPlatform.
the class JdbcSimpleModeMockServerTest method testTwoDmlStatements.
@Test
public void testTwoDmlStatements() throws SQLException {
try (Connection connection = DriverManager.getConnection(createUrl())) {
try (java.sql.Statement statement = connection.createStatement()) {
// Statement#execute(String) returns false if the result is an update count or no result.
assertFalse(statement.execute(String.format("%s; %s;", INSERT_STATEMENT, UPDATE_STATEMENT)));
// Note that we have sent two DML statements to the database in one string. These should be
// treated as separate statements, and there should therefore be two results coming back
// from the server. That is; The first update count should be 1 (the INSERT), and the second
// should be 2 (the UPDATE).
assertEquals(1, statement.getUpdateCount());
// The following is a prime example of how not to design an API, but this is how JDBC works.
// getMoreResults() returns true if the next result is a ResultSet. However, if the next
// result is an update count, it returns false, and we have to check getUpdateCount() to
// verify whether there were any more results.
assertFalse(statement.getMoreResults());
assertEquals(2, statement.getUpdateCount());
// There are no more results. This is indicated by getMoreResults returning false AND
// getUpdateCount returning -1.
assertFalse(statement.getMoreResults());
assertEquals(-1, statement.getUpdateCount());
}
}
// Verify that the DML statements were batched together by PgAdapter.
List<ExecuteBatchDmlRequest> requests = mockSpanner.getRequestsOfType(ExecuteBatchDmlRequest.class);
assertEquals(1, requests.size());
ExecuteBatchDmlRequest request = requests.get(0);
assertEquals(2, request.getStatementsCount());
assertEquals(INSERT_STATEMENT.getSql(), request.getStatements(0).getSql());
assertEquals(UPDATE_STATEMENT.getSql(), request.getStatements(1).getSql());
List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
assertEquals(1, commitRequests.size());
}
Aggregations