Search in sources :

Example 11 with IntermediateStatement

use of com.google.cloud.spanner.pgadapter.statements.IntermediateStatement in project pgadapter by GoogleCloudPlatform.

the class StatementTest method testBatchStatements.

@Test
public void testBatchStatements() {
    String sql = "INSERT INTO users (id) VALUES (1); INSERT INTO users (id) VALUES (2);INSERT INTO users (id) VALUES (3);";
    IntermediateStatement intermediateStatement = new IntermediateStatement(options, parse(sql), connectionHandler);
    assertTrue(intermediateStatement.isBatchedQuery());
    assertEquals(3, intermediateStatement.getStatements().size(), 3);
    assertEquals("INSERT INTO users (id) VALUES (1)", intermediateStatement.getStatement(0));
    assertEquals("INSERT INTO users (id) VALUES (2)", intermediateStatement.getStatement(1));
    assertEquals("INSERT INTO users (id) VALUES (3)", intermediateStatement.getStatement(2));
}
Also used : IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement) Test(org.junit.Test)

Example 12 with IntermediateStatement

use of com.google.cloud.spanner.pgadapter.statements.IntermediateStatement in project pgadapter by GoogleCloudPlatform.

the class StatementTest method testBasicSelectStatement.

@Test
public void testBasicSelectStatement() throws Exception {
    when(connection.execute(Statement.of("SELECT * FROM users"))).thenReturn(statementResult);
    when(statementResult.getResultType()).thenReturn(StatementResult.ResultType.RESULT_SET);
    when(statementResult.getResultSet()).thenReturn(resultSet);
    when(resultSet.next()).thenReturn(true);
    when(connectionHandler.getSpannerConnection()).thenReturn(connection);
    IntermediateStatement intermediateStatement = new IntermediateStatement(options, parse("SELECT * FROM users"), connectionHandler);
    assertFalse(intermediateStatement.isExecuted());
    assertEquals("SELECT", intermediateStatement.getCommand(0));
    intermediateStatement.execute();
    verify(connection).execute(Statement.of("SELECT * FROM users"));
    assertTrue(intermediateStatement.containsResultSet(0));
    assertTrue(intermediateStatement.isExecuted());
    assertEquals(StatementType.QUERY, intermediateStatement.getStatementType(0));
    assertEquals(resultSet, intermediateStatement.getStatementResult(0));
    assertTrue(intermediateStatement.isHasMoreData(0));
    assertFalse(intermediateStatement.hasException(0));
    assertEquals(0, intermediateStatement.getResultFormatCode(0));
    intermediateStatement.close();
    verify(resultSet).close();
}
Also used : IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement) Test(org.junit.Test)

Example 13 with IntermediateStatement

use of com.google.cloud.spanner.pgadapter.statements.IntermediateStatement in project pgadapter by GoogleCloudPlatform.

the class ConnectionHandler method cancelActiveStatement.

/**
 * To be used by a cancellation command to cancel a currently running statement, as contained in a
 * specific connection identified by connectionId. Since cancellation is a flimsy contract at
 * best, it is not imperative that the cancellation run, but it should be attempted nonetheless.
 *
 * @param connectionId The connection owhose statement must be cancelled.
 * @param secret The secret value linked to this connection. If it does not match, we cannot
 *     cancel.
 */
public synchronized void cancelActiveStatement(int connectionId, int secret) {
    int expectedSecret = ConnectionHandler.connectionToSecretMapping.get(connectionId);
    if (secret != expectedSecret) {
        logger.log(Level.WARNING, () -> MessageFormat.format("User attempted to cancel a connection with the incorrect secret." + "Connection: {}, Secret: {}, Expected Secret: {}", connectionId, secret, expectedSecret));
        // Since the user does not accept a response, there is no need to except here: simply return.
        return;
    }
    if (activeStatementsMap.containsKey(connectionId)) {
        IntermediateStatement statement = activeStatementsMap.remove(connectionId);
        // We can mostly ignore the exception since cancel does not expect any result (positive or
        // otherwise)
        statement.getConnection().cancel();
    }
}
Also used : IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement)

Aggregations

IntermediateStatement (com.google.cloud.spanner.pgadapter.statements.IntermediateStatement)13 Test (org.junit.Test)12 SpannerException (com.google.cloud.spanner.SpannerException)1 Statement (com.google.cloud.spanner.Statement)1 ParsedStatement (com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement)1 CopyStatement (com.google.cloud.spanner.pgadapter.statements.CopyStatement)1 IntermediatePortalStatement (com.google.cloud.spanner.pgadapter.statements.IntermediatePortalStatement)1 IntermediatePreparedStatement (com.google.cloud.spanner.pgadapter.statements.IntermediatePreparedStatement)1 QueryMessage (com.google.cloud.spanner.pgadapter.wireprotocol.QueryMessage)1 WireMessage (com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1