Search in sources :

Example 11 with CopyStatement

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

the class ProtocolTest method testCopyDoneMessage.

@Test
public void testCopyDoneMessage() throws Exception {
    byte[] messageMetadata = { 'c' };
    byte[] length = intToBytes(4);
    byte[] value = Bytes.concat(messageMetadata, length);
    DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(value));
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    DataOutputStream outputStream = new DataOutputStream(result);
    CopyStatement copyStatement = mock(CopyStatement.class);
    when(connectionHandler.getActiveStatement()).thenReturn(copyStatement);
    when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
    when(connectionHandler.getStatus()).thenReturn(ConnectionStatus.COPY_IN);
    when(connectionMetadata.getInputStream()).thenReturn(inputStream);
    when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
    MutationWriter mb = mock(MutationWriter.class);
    when(copyStatement.getMutationWriter()).thenReturn(mb);
    WireMessage message = ControlMessage.create(connectionHandler);
    assertEquals(CopyDoneMessage.class, message.getClass());
    CopyDoneMessage messageSpy = (CopyDoneMessage) spy(message);
    doReturn(false).when(messageSpy).sendSpannerResult(anyInt(), any(IntermediateStatement.class), any(QueryMode.class), anyLong());
    messageSpy.send();
    verify(messageSpy).sendSpannerResult(0, copyStatement, QueryMode.SIMPLE, 0L);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CopyStatement(com.google.cloud.spanner.pgadapter.statements.CopyStatement) DataOutputStream(java.io.DataOutputStream) QueryMode(com.google.cloud.spanner.pgadapter.ConnectionHandler.QueryMode) MutationWriter(com.google.cloud.spanner.pgadapter.utils.MutationWriter) WireMessage(com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage) IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement) CopyDoneMessage(com.google.cloud.spanner.pgadapter.wireprotocol.CopyDoneMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 12 with CopyStatement

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

the class QueryMessage method handleQuery.

/**
 * Simple Query handler, which examined the state of the statement and processes accordingly (if
 * error, handle error, otherwise sends the result and if contains result set, send row
 * description)
 *
 * @throws Exception if handling the query fails
 */
public void handleQuery() throws Exception {
    // Skip unexecuted statements, as no response needs be returned
    for (int index = 0; index < statement.getExecutedCount(); index++) {
        if (this.statement.hasException(index)) {
            new ErrorResponse(this.outputStream, this.statement.getException(index), State.InternalError).send();
        } else if (this.statement.getCommand(index).equalsIgnoreCase(COPY)) {
            CopyStatement copyStatement = (CopyStatement) this.statement;
            new CopyInResponse(this.outputStream, copyStatement.getTableColumns().size(), copyStatement.getFormatCode()).send();
            this.connection.setStatus(ConnectionStatus.COPY_IN);
            // Return early as we do not respond with CommandComplete after a COPY command.
            return;
        } else if ("".equals(this.statement.getCommandTag(index))) {
            new EmptyQueryResponse(outputStream).send();
        } else {
            if (this.statement.containsResultSet(index)) {
                new RowDescriptionResponse(this.outputStream, this.statement, this.statement.getStatementResult(index), this.connection.getServer().getOptions(), QueryMode.SIMPLE).send(false);
            }
            this.sendSpannerResult(index, this.statement, QueryMode.SIMPLE, 0L);
        }
    }
    if (connection.getSpannerConnection().isInTransaction() && connection.getStatus() == ConnectionStatus.TRANSACTION_ABORTED) {
        // Actively rollback the aborted transaction but still block clients
        // Clear any statement tags, as these are not allowed for rollbacks.
        connection.getSpannerConnection().setStatementTag(null);
        connection.getSpannerConnection().rollback();
    }
    new ReadyResponse(this.outputStream, connection.getStatus().getReadyResponseStatus()).send();
    this.connection.cleanUp(this.statement);
}
Also used : RowDescriptionResponse(com.google.cloud.spanner.pgadapter.wireoutput.RowDescriptionResponse) ReadyResponse(com.google.cloud.spanner.pgadapter.wireoutput.ReadyResponse) CopyStatement(com.google.cloud.spanner.pgadapter.statements.CopyStatement) EmptyQueryResponse(com.google.cloud.spanner.pgadapter.wireoutput.EmptyQueryResponse) CopyInResponse(com.google.cloud.spanner.pgadapter.wireoutput.CopyInResponse) ErrorResponse(com.google.cloud.spanner.pgadapter.wireoutput.ErrorResponse)

Aggregations

CopyStatement (com.google.cloud.spanner.pgadapter.statements.CopyStatement)12 Test (org.junit.Test)11 MutationWriter (com.google.cloud.spanner.pgadapter.utils.MutationWriter)10 OptionsMetadata (com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata)5 SpannerException (com.google.cloud.spanner.SpannerException)4 WireMessage (com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 DataInputStream (java.io.DataInputStream)4 IntermediateStatement (com.google.cloud.spanner.pgadapter.statements.IntermediateStatement)3 File (java.io.File)3 ResultSet (com.google.cloud.spanner.ResultSet)2 Statement (com.google.cloud.spanner.Statement)2 IntermediatePortalStatement (com.google.cloud.spanner.pgadapter.statements.IntermediatePortalStatement)2 IntermediatePreparedStatement (com.google.cloud.spanner.pgadapter.statements.IntermediatePreparedStatement)2 CopyDataMessage (com.google.cloud.spanner.pgadapter.wireprotocol.CopyDataMessage)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 DatabaseClient (com.google.cloud.spanner.DatabaseClient)1 ParsedStatement (com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement)1 QueryMode (com.google.cloud.spanner.pgadapter.ConnectionHandler.QueryMode)1