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);
}
}
}
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);
}
}
}
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));
}
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));
}
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));
}
Aggregations