Search in sources :

Example 1 with IntermediateStatement

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));
}
Also used : IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement) SpannerException(com.google.cloud.spanner.SpannerException) Test(org.junit.Test)

Example 2 with IntermediateStatement

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));
}
Also used : IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement) Test(org.junit.Test)

Example 3 with IntermediateStatement

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());
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement) IntermediatePortalStatement(com.google.cloud.spanner.pgadapter.statements.IntermediatePortalStatement) IntermediatePreparedStatement(com.google.cloud.spanner.pgadapter.statements.IntermediatePreparedStatement) CopyStatement(com.google.cloud.spanner.pgadapter.statements.CopyStatement) Statement(com.google.cloud.spanner.Statement) IntermediatePreparedStatement(com.google.cloud.spanner.pgadapter.statements.IntermediatePreparedStatement) IntermediatePortalStatement(com.google.cloud.spanner.pgadapter.statements.IntermediatePortalStatement) Test(org.junit.Test)

Example 4 with IntermediateStatement

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));
}
Also used : IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement) Test(org.junit.Test)

Example 5 with IntermediateStatement

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();
}
Also used : IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement) Test(org.junit.Test)

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