Search in sources :

Example 6 with ErrorResponse

use of com.google.cloud.spanner.pgadapter.wireoutput.ErrorResponse 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

ErrorResponse (com.google.cloud.spanner.pgadapter.wireoutput.ErrorResponse)6 ReadyResponse (com.google.cloud.spanner.pgadapter.wireoutput.ReadyResponse)3 TerminateResponse (com.google.cloud.spanner.pgadapter.wireoutput.TerminateResponse)2 SpannerException (com.google.cloud.spanner.SpannerException)1 CopyStatement (com.google.cloud.spanner.pgadapter.statements.CopyStatement)1 MutationWriter (com.google.cloud.spanner.pgadapter.utils.MutationWriter)1 CopyInResponse (com.google.cloud.spanner.pgadapter.wireoutput.CopyInResponse)1 EmptyQueryResponse (com.google.cloud.spanner.pgadapter.wireoutput.EmptyQueryResponse)1 RowDescriptionResponse (com.google.cloud.spanner.pgadapter.wireoutput.RowDescriptionResponse)1 BufferedOutputStream (java.io.BufferedOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 IOException (java.io.IOException)1 SocketException (java.net.SocketException)1