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