use of com.google.cloud.spanner.pgadapter.statements.IntermediateStatement in project pgadapter by GoogleCloudPlatform.
the class StatementTest method testBasicStatementExceptionGetsSetOnExceptedExecution.
@Test
public void testBasicStatementExceptionGetsSetOnExceptedExecution() {
SpannerException thrownException = SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "test error");
when(connection.execute(Statement.of("SELECT * FROM users"))).thenThrow(thrownException);
when(connectionHandler.getSpannerConnection()).thenReturn(connection);
IntermediateStatement intermediateStatement = new IntermediateStatement(options, parse("SELECT * FROM users"), connectionHandler);
intermediateStatement.execute();
assertTrue(intermediateStatement.hasException(0));
assertEquals(thrownException, intermediateStatement.getException(0));
}
use of com.google.cloud.spanner.pgadapter.statements.IntermediateStatement in project pgadapter by GoogleCloudPlatform.
the class StatementTest method testBatchStatementsWithEmptyStatements.
@Test
public void testBatchStatementsWithEmptyStatements() {
String sql = "INSERT INTO users (id) VALUES (1); ;;; INSERT INTO users (id) VALUES (2);";
IntermediateStatement intermediateStatement = new IntermediateStatement(options, parse(sql), connectionHandler);
assertTrue(intermediateStatement.isBatchedQuery());
assertEquals(2, intermediateStatement.getStatements().size());
assertEquals("INSERT INTO users (id) VALUES (1)", intermediateStatement.getStatement(0));
assertEquals("INSERT INTO users (id) VALUES (2)", intermediateStatement.getStatement(1));
}
use of com.google.cloud.spanner.pgadapter.statements.IntermediateStatement in project pgadapter by GoogleCloudPlatform.
the class StatementTest method testPreparedStatement.
@Test
public void testPreparedStatement() {
when(connectionHandler.getSpannerConnection()).thenReturn(connection);
String sqlStatement = "SELECT * FROM users WHERE age > $2 AND age < $3 AND name = $1";
int[] parameterDataTypes = new int[] { Oid.VARCHAR, Oid.INT8, Oid.INT4 };
Statement statement = Statement.newBuilder(sqlStatement).bind("p1").to("userName").bind("p2").to(20L).bind("p3").to(30).build();
when(statementResult.getResultType()).thenReturn(StatementResult.ResultType.UPDATE_COUNT);
when(statementResult.getUpdateCount()).thenReturn(0L);
when(connection.execute(statement)).thenReturn(statementResult);
IntermediatePreparedStatement intermediateStatement = new IntermediatePreparedStatement(connectionHandler, options, "", parse(sqlStatement));
intermediateStatement.setParameterDataTypes(parameterDataTypes);
assertEquals(sqlStatement, intermediateStatement.getSql());
byte[][] parameters = { "userName".getBytes(), "20".getBytes(), "30".getBytes() };
IntermediatePortalStatement intermediatePortalStatement = intermediateStatement.bind("", parameters, Arrays.asList((short) 0, (short) 0, (short) 0), new ArrayList<>());
intermediateStatement.execute();
verify(connection).execute(statement);
assertEquals(sqlStatement, intermediatePortalStatement.getSql());
assertEquals("SELECT", intermediatePortalStatement.getCommand(0));
assertFalse(intermediatePortalStatement.isExecuted());
assertTrue(intermediateStatement.isBound());
}
use of com.google.cloud.spanner.pgadapter.statements.IntermediateStatement in project pgadapter by GoogleCloudPlatform.
the class StatementTest method testAdditionalBatchStatements.
@Test
public void testAdditionalBatchStatements() {
String sql = "BEGIN TRANSACTION; INSERT INTO users (id) VALUES (1); INSERT INTO users (id) VALUES (2); INSERT INTO users (id) VALUES (3); COMMIT;";
IntermediateStatement intermediateStatement = new IntermediateStatement(options, parse(sql), connectionHandler);
assertTrue(intermediateStatement.isBatchedQuery());
assertEquals(5, intermediateStatement.getStatements().size(), 5);
assertEquals("BEGIN TRANSACTION", intermediateStatement.getStatement(0));
assertEquals("INSERT INTO users (id) VALUES (1)", intermediateStatement.getStatement(1));
assertEquals("INSERT INTO users (id) VALUES (2)", intermediateStatement.getStatement(2));
assertEquals("INSERT INTO users (id) VALUES (3)", intermediateStatement.getStatement(3));
assertEquals("COMMIT", intermediateStatement.getStatement(4));
}
use of com.google.cloud.spanner.pgadapter.statements.IntermediateStatement in project pgadapter by GoogleCloudPlatform.
the class StatementTest method testBasicZeroUpdateCountResultStatement.
@Test
public void testBasicZeroUpdateCountResultStatement() throws Exception {
when(statementResult.getResultType()).thenReturn(StatementResult.ResultType.UPDATE_COUNT);
when(statementResult.getUpdateCount()).thenReturn(0L);
when(connection.execute(Statement.of("UPDATE users SET name = someName WHERE id = -1"))).thenReturn(statementResult);
when(connectionHandler.getSpannerConnection()).thenReturn(connection);
IntermediateStatement intermediateStatement = new IntermediateStatement(options, parse("UPDATE users SET name = someName WHERE id = -1"), connectionHandler);
assertFalse(intermediateStatement.isExecuted());
assertEquals("UPDATE", intermediateStatement.getCommand(0));
intermediateStatement.execute();
Mockito.verify(connection).execute(Statement.of("UPDATE users SET name = someName WHERE id = -1"));
assertFalse(intermediateStatement.containsResultSet(0));
assertEquals(0L, intermediateStatement.getUpdateCount(0));
assertTrue(intermediateStatement.isExecuted());
assertEquals(StatementType.UPDATE, intermediateStatement.getStatementType(0));
assertNull(intermediateStatement.getStatementResult(0));
assertFalse(intermediateStatement.isHasMoreData(0));
assertFalse(intermediateStatement.hasException(0));
assertEquals(0, intermediateStatement.getResultFormatCode(0));
intermediateStatement.close();
verify(resultSet, never()).close();
}
Aggregations