Search in sources :

Example 1 with StatementResult

use of com.google.cloud.spanner.connection.StatementResult in project pgadapter by GoogleCloudPlatform.

the class IntermediatePreparedStatement method execute.

@Override
public void execute() {
    // TODO(230579451): Refactor to use ClientSideStatement information.
    if (connectionHandler.getStatus() == ConnectionStatus.TRANSACTION_ABORTED) {
        handleTransactionAborted();
        return;
    }
    // don't need to do that once more.
    if (getStatementResult(0) == null) {
        this.executedCount++;
        try {
            if (!connection.isInTransaction() && // TODO(230579451): Refactor to use ClientSideStatement information.
            this.parsedStatement.getType().equals(StatementType.CLIENT_SIDE) && (this.commands.get(0).equals("ROLLBACK") || this.commands.get(0).equals("COMMIT"))) {
                // TODO(230579929): Return warning that no transaction if connection status == IDLE.
                connectionHandler.setStatus(ConnectionStatus.IDLE);
            } else {
                StatementResult result = connection.execute(this.statement);
                this.updateResultCount(0, result);
                connectionHandler.setStatus(connection.isInTransaction() ? ConnectionStatus.TRANSACTION : ConnectionStatus.IDLE);
            }
        } catch (SpannerException exception) {
            handleExecutionExceptionAndTransactionStatus(0, exception);
        }
    }
}
Also used : StatementResult(com.google.cloud.spanner.connection.StatementResult) SpannerException(com.google.cloud.spanner.SpannerException)

Example 2 with StatementResult

use of com.google.cloud.spanner.connection.StatementResult in project pgadapter by GoogleCloudPlatform.

the class IntermediateStatement method executeSingleStatement.

private void executeSingleStatement(int index) {
    // Before executing the statement, handle specific statements that change the transaction status
    String command = getCommand(index);
    String statement = getStatement(index);
    if (isBegin(index)) {
        // Executing a BEGIN statement when a transaction is already active will set the execution
        // mode to EXPLICIT_TRANSACTION. The current transaction is not committed.
        executionStatus = ExecutionStatus.EXPLICIT_TRANSACTION;
        if (!connection.isInTransaction()) {
            connection.execute(Statement.of(statement));
        }
        return;
    }
    if (isCommit(index) || isRollback(index)) {
        if (connectionHandler.getStatus() == ConnectionStatus.TRANSACTION_ABORTED) {
            connectionHandler.setStatus(ConnectionStatus.IDLE);
            // COMMIT rollbacks aborted transaction
            statement = "ROLLBACK";
            commandTags.set(index, "ROLLBACK");
        }
        // Executing ROLLBACK or COMMIT when there is no active transaction is a no-op, but will
        // change the transaction mode to implicit.
        executionStatus = ExecutionStatus.IMPLICIT_TRANSACTION;
        if (!connection.isInTransaction()) {
            return;
        }
    }
    // Ignore empty statements.
    if (!"".equals(statement)) {
        try {
            StatementResult result = this.connection.execute(Statement.of(statement));
            updateResultCount(index, result);
        } catch (SpannerException e) {
            handleExecutionExceptionAndTransactionStatus(index, e);
        }
    }
}
Also used : StatementResult(com.google.cloud.spanner.connection.StatementResult) SpannerException(com.google.cloud.spanner.SpannerException)

Example 3 with StatementResult

use of com.google.cloud.spanner.connection.StatementResult in project pgadapter by GoogleCloudPlatform.

the class IntermediateStatementTest method testUpdateResultCount_ResultSet.

@Test
public void testUpdateResultCount_ResultSet() {
    when(connectionHandler.getSpannerConnection()).thenReturn(connection);
    IntermediateStatement statement = new IntermediateStatement(mock(OptionsMetadata.class), parse("select foo from bar"), connectionHandler);
    ResultSet resultSet = mock(ResultSet.class);
    when(resultSet.next()).thenReturn(true, false);
    StatementResult result = mock(StatementResult.class);
    when(result.getResultType()).thenReturn(ResultType.RESULT_SET);
    when(result.getResultSet()).thenReturn(resultSet);
    statement.updateResultCount(0, result);
    assertTrue(statement.hasMoreData[0]);
    assertEquals(-1, statement.getUpdateCount(0));
    assertSame(resultSet, statement.getStatementResult(0));
}
Also used : StatementResult(com.google.cloud.spanner.connection.StatementResult) ResultSet(com.google.cloud.spanner.ResultSet) OptionsMetadata(com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata) Test(org.junit.Test)

Example 4 with StatementResult

use of com.google.cloud.spanner.connection.StatementResult in project pgadapter by GoogleCloudPlatform.

the class IntermediateStatementTest method testUpdateResultCount_UpdateCount.

@Test
public void testUpdateResultCount_UpdateCount() {
    when(connectionHandler.getSpannerConnection()).thenReturn(connection);
    IntermediateStatement statement = new IntermediateStatement(mock(OptionsMetadata.class), parse("update bar set foo=1"), connectionHandler);
    StatementResult result = mock(StatementResult.class);
    when(result.getResultType()).thenReturn(ResultType.UPDATE_COUNT);
    when(result.getUpdateCount()).thenReturn(100L);
    statement.updateResultCount(0, result);
    assertFalse(statement.hasMoreData[0]);
    assertEquals(100L, statement.getUpdateCount(0));
    assertNull(statement.getStatementResult(0));
}
Also used : StatementResult(com.google.cloud.spanner.connection.StatementResult) OptionsMetadata(com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata) Test(org.junit.Test)

Example 5 with StatementResult

use of com.google.cloud.spanner.connection.StatementResult in project pgadapter by GoogleCloudPlatform.

the class IntermediateStatementTest method testUpdateResultCount_NoResult.

@Test
public void testUpdateResultCount_NoResult() {
    when(connectionHandler.getSpannerConnection()).thenReturn(connection);
    IntermediateStatement statement = new IntermediateStatement(mock(OptionsMetadata.class), parse("create table bar (foo bigint primary key)"), connectionHandler);
    StatementResult result = mock(StatementResult.class);
    when(result.getResultType()).thenReturn(ResultType.NO_RESULT);
    statement.updateResultCount(0, result);
    assertFalse(statement.hasMoreData[0]);
    assertEquals(0, statement.getUpdateCount(0));
    assertNull(statement.getStatementResult(0));
}
Also used : StatementResult(com.google.cloud.spanner.connection.StatementResult) OptionsMetadata(com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata) Test(org.junit.Test)

Aggregations

StatementResult (com.google.cloud.spanner.connection.StatementResult)10 Test (org.junit.Test)5 SpannerException (com.google.cloud.spanner.SpannerException)3 OptionsMetadata (com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata)3 Connection (com.google.cloud.spanner.connection.Connection)2 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 Dialect (com.google.cloud.spanner.Dialect)1 ErrorCode (com.google.cloud.spanner.ErrorCode)1 ResultSet (com.google.cloud.spanner.ResultSet)1 SpannerExceptionFactory (com.google.cloud.spanner.SpannerExceptionFactory)1 Statement (com.google.cloud.spanner.Statement)1 Type (com.google.cloud.spanner.Type)1 AbstractStatementParser (com.google.cloud.spanner.connection.AbstractStatementParser)1 ResultType (com.google.cloud.spanner.connection.StatementResult.ResultType)1 JdbcSqlExceptionImpl (com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 Code (com.google.rpc.Code)1 SQLException (java.sql.SQLException)1 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)1