Search in sources :

Example 51 with CommitRequest

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());
    }
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 52 with CommitRequest

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());
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) Connection(java.sql.Connection) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 53 with CommitRequest

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());
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Statement(java.sql.Statement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) RollbackRequest(com.google.spanner.v1.RollbackRequest) Test(org.junit.Test)

Example 54 with CommitRequest

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());
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) Connection(java.sql.Connection) Statement(java.sql.Statement) Test(org.junit.Test)

Example 55 with CommitRequest

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());
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) Connection(java.sql.Connection) Statement(java.sql.Statement) ExecuteBatchDmlRequest(com.google.spanner.v1.ExecuteBatchDmlRequest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)85 CommitRequest (com.google.spanner.v1.CommitRequest)47 CommitRequest (com.google.firestore.v1.CommitRequest)40 CommitResponse (com.google.firestore.v1.CommitResponse)40 ArrayList (java.util.ArrayList)26 Connection (java.sql.Connection)23 ByteString (com.google.protobuf.ByteString)14 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)12 SQLException (java.sql.SQLException)12 Statement (java.sql.Statement)12 CopyManager (org.postgresql.copy.CopyManager)11 BaseConnection (org.postgresql.core.BaseConnection)11 ExecuteBatchDmlRequest (com.google.spanner.v1.ExecuteBatchDmlRequest)10 ArrayValue (com.google.firestore.v1.ArrayValue)9 MapValue (com.google.firestore.v1.MapValue)9 Value (com.google.firestore.v1.Value)9 Write (com.google.firestore.v1.Write)9 StringReader (java.io.StringReader)9 HashMap (java.util.HashMap)9 AbstractMessage (com.google.protobuf.AbstractMessage)8