Search in sources :

Example 1 with IntermediatePortalStatement

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ExecuteMessage(com.google.cloud.spanner.pgadapter.wireprotocol.ExecuteMessage) DataOutputStream(java.io.DataOutputStream) QueryMode(com.google.cloud.spanner.pgadapter.ConnectionHandler.QueryMode) WireMessage(com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) IntermediatePortalStatement(com.google.cloud.spanner.pgadapter.statements.IntermediatePortalStatement) Test(org.junit.Test)

Example 2 with 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());
}
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 3 with IntermediatePortalStatement

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

Example 4 with IntermediatePortalStatement

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

Example 5 with IntermediatePortalStatement

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();
}
Also used : IOException(java.io.IOException) EOFException(java.io.EOFException) SpannerException(com.google.cloud.spanner.SpannerException) IntermediatePortalStatement(com.google.cloud.spanner.pgadapter.statements.IntermediatePortalStatement)

Aggregations

IntermediatePortalStatement (com.google.cloud.spanner.pgadapter.statements.IntermediatePortalStatement)5 Test (org.junit.Test)4 SpannerException (com.google.cloud.spanner.SpannerException)2 Statement (com.google.cloud.spanner.Statement)1 ParsedStatement (com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement)1 QueryMode (com.google.cloud.spanner.pgadapter.ConnectionHandler.QueryMode)1 CopyStatement (com.google.cloud.spanner.pgadapter.statements.CopyStatement)1 IntermediatePreparedStatement (com.google.cloud.spanner.pgadapter.statements.IntermediatePreparedStatement)1 IntermediateStatement (com.google.cloud.spanner.pgadapter.statements.IntermediateStatement)1 ExecuteMessage (com.google.cloud.spanner.pgadapter.wireprotocol.ExecuteMessage)1 WireMessage (com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1