Search in sources :

Example 1 with IntermediatePreparedStatement

use of com.google.cloud.spanner.pgadapter.statements.IntermediatePreparedStatement 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 2 with IntermediatePreparedStatement

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

the class StatementTest method testPreparedStatementIllegalTypeThrowsException.

@Test
public void testPreparedStatementIllegalTypeThrowsException() {
    String sqlStatement = "SELECT * FROM users WHERE metadata = $1";
    int[] parameterDataTypes = new int[] { Oid.JSON };
    IntermediatePreparedStatement intermediateStatement = new IntermediatePreparedStatement(connectionHandler, options, "", parse(sqlStatement));
    intermediateStatement.setParameterDataTypes(parameterDataTypes);
    byte[][] parameters = { "{}".getBytes() };
    assertThrows(IllegalArgumentException.class, () -> intermediateStatement.bind("", parameters, new ArrayList<>(), new ArrayList<>()));
}
Also used : ArrayList(java.util.ArrayList) IntermediatePreparedStatement(com.google.cloud.spanner.pgadapter.statements.IntermediatePreparedStatement) Test(org.junit.Test)

Example 3 with IntermediatePreparedStatement

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

the class StatementTest method testPreparedStatementDescribeDoesNotThrowException.

@Test
public void testPreparedStatementDescribeDoesNotThrowException() {
    when(connectionHandler.getSpannerConnection()).thenReturn(connection);
    String sqlStatement = "SELECT * FROM users WHERE name = $1 AND age > $2 AND age < $3";
    when(connection.analyzeQuery(Statement.of(sqlStatement), QueryAnalyzeMode.PLAN)).thenReturn(resultSet);
    IntermediatePreparedStatement intermediateStatement = new IntermediatePreparedStatement(connectionHandler, options, "", parse(sqlStatement));
    int[] parameters = new int[3];
    Arrays.fill(parameters, Oid.INT8);
    intermediateStatement.setParameterDataTypes(parameters);
    intermediateStatement.describe();
}
Also used : IntermediatePreparedStatement(com.google.cloud.spanner.pgadapter.statements.IntermediatePreparedStatement) Test(org.junit.Test)

Example 4 with IntermediatePreparedStatement

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

the class StatementTest method testPreparedStatementDescribeThrowsException.

@Test(expected = IllegalStateException.class)
public void testPreparedStatementDescribeThrowsException() throws Exception {
    String sqlStatement = "SELECT * FROM users WHERE name = $1 AND age > $2 AND age < $3";
    IntermediatePreparedStatement intermediateStatement = new IntermediatePreparedStatement(sqlStatement, connection);
    intermediateStatement.describe();
}
Also used : IntermediatePreparedStatement(com.google.cloud.spanner.pgadapter.statements.IntermediatePreparedStatement) Test(org.junit.Test)

Aggregations

IntermediatePreparedStatement (com.google.cloud.spanner.pgadapter.statements.IntermediatePreparedStatement)4 Test (org.junit.Test)4 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 IntermediateStatement (com.google.cloud.spanner.pgadapter.statements.IntermediateStatement)1 ArrayList (java.util.ArrayList)1