use of com.google.cloud.spanner.pgadapter.statements.IntermediatePortalStatement in project pgadapter by GoogleCloudPlatform.
the class ProtocolTest method testExecuteMessage.
@Test
public void testExecuteMessage() throws Exception {
byte[] messageMetadata = { 'E' };
String statementName = "some portal\0";
int totalRows = 99999;
byte[] length = intToBytes(4 + statementName.length() + 4);
byte[] value = Bytes.concat(messageMetadata, length, statementName.getBytes(), intToBytes(totalRows));
String expectedStatementName = "some portal";
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(value));
ByteArrayOutputStream result = new ByteArrayOutputStream();
DataOutputStream outputStream = new DataOutputStream(result);
when(connectionHandler.getPortal(anyString())).thenReturn(intermediatePortalStatement);
when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
when(connectionMetadata.getInputStream()).thenReturn(inputStream);
when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
WireMessage message = ControlMessage.create(connectionHandler);
assertEquals(ExecuteMessage.class, message.getClass());
assertEquals(expectedStatementName, ((ExecuteMessage) message).getName());
assertEquals(totalRows, ((ExecuteMessage) message).getMaxRows());
verify(connectionHandler).getPortal("some portal");
ExecuteMessage messageSpy = (ExecuteMessage) spy(message);
doReturn(false).when(messageSpy).sendSpannerResult(anyInt(), any(IntermediatePortalStatement.class), any(QueryMode.class), anyLong());
messageSpy.send();
verify(intermediatePortalStatement).execute();
verify(messageSpy).sendSpannerResult(0, intermediatePortalStatement, QueryMode.EXTENDED, totalRows);
verify(connectionHandler).cleanUp(intermediatePortalStatement);
}
use of com.google.cloud.spanner.pgadapter.statements.IntermediatePortalStatement 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.IntermediatePortalStatement in project pgadapter by GoogleCloudPlatform.
the class StatementTest method testPortalStatement.
@Test
public void testPortalStatement() {
when(connectionHandler.getSpannerConnection()).thenReturn(connection);
String sqlStatement = "SELECT * FROM users WHERE age > $1 AND age < $2 AND name = $3";
when(connection.executeQuery(Statement.of(sqlStatement))).thenReturn(resultSet);
IntermediatePortalStatement intermediateStatement = new IntermediatePortalStatement(connectionHandler, options, "", parse(sqlStatement));
intermediateStatement.describe();
verify(connection).executeQuery(Statement.of(sqlStatement));
assertEquals(0, intermediateStatement.getParameterFormatCode(0));
assertEquals(0, intermediateStatement.getParameterFormatCode(1));
assertEquals(0, intermediateStatement.getParameterFormatCode(2));
assertEquals(0, intermediateStatement.getResultFormatCode(0));
assertEquals(0, intermediateStatement.getResultFormatCode(1));
assertEquals(0, intermediateStatement.getResultFormatCode(2));
intermediateStatement.setParameterFormatCodes(Collections.singletonList((short) 1));
intermediateStatement.setResultFormatCodes(Collections.singletonList((short) 1));
assertEquals(1, intermediateStatement.getParameterFormatCode(0));
assertEquals(1, intermediateStatement.getParameterFormatCode(1));
assertEquals(1, intermediateStatement.getParameterFormatCode(2));
assertEquals(1, intermediateStatement.getResultFormatCode(0));
assertEquals(1, intermediateStatement.getResultFormatCode(1));
assertEquals(1, intermediateStatement.getResultFormatCode(2));
intermediateStatement.setParameterFormatCodes(Arrays.asList((short) 0, (short) 1, (short) 0));
intermediateStatement.setResultFormatCodes(Arrays.asList((short) 0, (short) 1, (short) 0));
assertEquals(0, intermediateStatement.getParameterFormatCode(0));
assertEquals(1, intermediateStatement.getParameterFormatCode(1));
assertEquals(0, intermediateStatement.getParameterFormatCode(2));
assertEquals(0, intermediateStatement.getResultFormatCode(0));
assertEquals(1, intermediateStatement.getResultFormatCode(1));
assertEquals(0, intermediateStatement.getResultFormatCode(2));
}
use of com.google.cloud.spanner.pgadapter.statements.IntermediatePortalStatement in project pgadapter by GoogleCloudPlatform.
the class StatementTest method testPortalStatementDescribePropagatesFailure.
@Test
public void testPortalStatementDescribePropagatesFailure() {
when(connectionHandler.getSpannerConnection()).thenReturn(connection);
String sqlStatement = "SELECT * FROM users WHERE age > $1 AND age < $2 AND name = $3";
IntermediatePortalStatement intermediateStatement = new IntermediatePortalStatement(connectionHandler, options, "", parse(sqlStatement));
when(connection.executeQuery(Statement.of(sqlStatement))).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "test error"));
SpannerException exception = assertThrows(SpannerException.class, intermediateStatement::describe);
assertEquals(ErrorCode.INVALID_ARGUMENT, exception.getErrorCode());
}
use of com.google.cloud.spanner.pgadapter.statements.IntermediatePortalStatement in project pgadapter by GoogleCloudPlatform.
the class ConnectionHandler method closeAllPortals.
/**
* Closes all named and unnamed portals on this connection.
*/
private void closeAllPortals() {
for (IntermediatePortalStatement statement : portalsMap.values()) {
try {
statement.close();
} catch (Exception e) {
logger.log(Level.SEVERE, e, () -> String.format("Unable to close portal: %s", e.getMessage()));
}
}
this.portalsMap.clear();
this.statementsMap.clear();
}
Aggregations