Search in sources :

Example 36 with XdrOutputStream

use of org.firebirdsql.gds.impl.wire.XdrOutputStream in project jaybird by FirebirdSQL.

the class V10Statement method sendExecute.

/**
 * Sends the execute (for <code>op_execute</code> or <code>op_execute2</code>) to the database.
 *
 * @param operation
 *         Operation (<code>op_execute</code> or <code>op_execute2</code>)
 * @param parameters
 *         Parameters
 * @throws IOException
 * @throws SQLException
 */
protected void sendExecute(final int operation, final RowValue parameters) throws IOException, SQLException {
    assert operation == WireProtocolConstants.op_execute || operation == WireProtocolConstants.op_execute2 : "Needs to be called with operation op_execute or op_execute2";
    final XdrOutputStream xdrOut = getXdrOut();
    xdrOut.writeInt(operation);
    xdrOut.writeInt(getHandle());
    xdrOut.writeInt(getTransaction().getHandle());
    if (parameters != null && parameters.getCount() > 0) {
        final RowDescriptor parameterDescriptor = getParameterDescriptor();
        xdrOut.writeBuffer(calculateBlr(parameterDescriptor, parameters));
        // message number = in_message_type
        xdrOut.writeInt(0);
        // Number of messages
        xdrOut.writeInt(1);
        writeSqlData(parameterDescriptor, parameters);
    } else {
        xdrOut.writeBuffer(null);
        // message number = in_message_type
        xdrOut.writeInt(0);
        // Number of messages
        xdrOut.writeInt(0);
    }
    if (operation == WireProtocolConstants.op_execute2) {
        final RowDescriptor fieldDescriptor = getFieldDescriptor();
        xdrOut.writeBuffer(fieldDescriptor != null && fieldDescriptor.getCount() > 0 ? calculateBlr(fieldDescriptor) : null);
        // out_message_number = out_message_type
        xdrOut.writeInt(0);
    }
}
Also used : XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream)

Example 37 with XdrOutputStream

use of org.firebirdsql.gds.impl.wire.XdrOutputStream in project jaybird by FirebirdSQL.

the class V10Statement method setCursorName.

public void setCursorName(String cursorName) throws SQLException {
    try {
        synchronized (getSynchronizationObject()) {
            checkStatementValid();
            try {
                final XdrOutputStream xdrOut = getXdrOut();
                xdrOut.writeInt(WireProtocolConstants.op_set_cursor);
                xdrOut.writeInt(getHandle());
                // Null termination is needed due to a quirk of the protocol
                xdrOut.writeString(cursorName + '\0', getDatabase().getEncoding());
                // Cursor type
                xdrOut.writeInt(0);
                xdrOut.flush();
            } catch (IOException ex) {
                switchState(StatementState.ERROR);
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(ex).toSQLException();
            }
            try {
                // TODO Do we need to do anything else with this response?
                getDatabase().readGenericResponse(getStatementWarningCallback());
            } catch (IOException ex) {
                switchState(StatementState.ERROR);
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_read_err).cause(ex).toSQLException();
            }
        }
    } catch (SQLException e) {
        exceptionListenerDispatcher.errorOccurred(e);
        throw e;
    }
}
Also used : SQLException(java.sql.SQLException) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) IOException(java.io.IOException)

Example 38 with XdrOutputStream

use of org.firebirdsql.gds.impl.wire.XdrOutputStream in project jaybird by FirebirdSQL.

the class V10Transaction method getTransactionInfo.

@Override
public byte[] getTransactionInfo(byte[] requestItems, int maxBufferLength) throws SQLException {
    try {
        synchronized (getSynchronizationObject()) {
            try {
                final XdrOutputStream xdrOut = getXdrOut();
                xdrOut.writeInt(op_info_transaction);
                xdrOut.writeInt(getHandle());
                // incarnation(?)
                xdrOut.writeInt(0);
                xdrOut.writeBuffer(requestItems);
                xdrOut.writeInt(maxBufferLength);
                xdrOut.flush();
            } catch (IOException ioex) {
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(ioex).toSQLException();
            }
            try {
                final GenericResponse genericResponse = getDatabase().readGenericResponse(null);
                return genericResponse.getData();
            } catch (IOException ex) {
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_read_err).cause(ex).toSQLException();
            }
        }
    } catch (SQLException e) {
        exceptionListenerDispatcher.errorOccurred(e);
        throw e;
    }
}
Also used : GenericResponse(org.firebirdsql.gds.ng.wire.GenericResponse) SQLException(java.sql.SQLException) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) IOException(java.io.IOException)

Example 39 with XdrOutputStream

use of org.firebirdsql.gds.impl.wire.XdrOutputStream in project jaybird by FirebirdSQL.

the class V10Transaction method prepare.

@Override
public void prepare(byte[] recoveryInformation) throws SQLException {
    try {
        synchronized (getSynchronizationObject()) {
            switchState(TransactionState.PREPARING);
            try {
                final XdrOutputStream xdrOut = getXdrOut();
                if (recoveryInformation != null) {
                    xdrOut.writeInt(op_prepare2);
                    xdrOut.writeInt(handle);
                    xdrOut.writeBuffer(recoveryInformation);
                } else {
                    xdrOut.writeInt(op_prepare);
                    xdrOut.writeInt(handle);
                }
                xdrOut.flush();
            } catch (IOException ioex) {
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(ioex).toSQLException();
            }
            try {
                getDatabase().readResponse(null);
            } catch (IOException ioex) {
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_read_err).cause(ioex).toSQLException();
            }
            switchState(TransactionState.PREPARED);
        }
    } catch (SQLException e) {
        exceptionListenerDispatcher.errorOccurred(e);
        throw e;
    } finally {
        if (getState() != TransactionState.PREPARED) {
            log.warn("Prepare not completed", new RuntimeException("Prepare not completed"));
        }
    }
}
Also used : SQLException(java.sql.SQLException) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) IOException(java.io.IOException)

Example 40 with XdrOutputStream

use of org.firebirdsql.gds.impl.wire.XdrOutputStream in project jaybird by FirebirdSQL.

the class TestV10EventHandling method checkAsynchronousDisconnection.

private void checkAsynchronousDisconnection(int disconnectOperation) throws Exception {
    try (SimpleServer simpleServer = new SimpleServer()) {
        final FbWireAsynchronousChannel channel = new V10AsynchronousChannel(createDummyDatabase());
        Thread establishChannel = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    channel.connect("localhost", simpleServer.getPort(), 1);
                } catch (SQLException e) {
                // suppress
                }
            }
        });
        establishChannel.start();
        simpleServer.acceptConnection();
        AsynchronousProcessor.getInstance().registerAsynchronousChannel(channel);
        establishChannel.join(500);
        assertTrue("Expected connected channel", channel.isConnected());
        final XdrOutputStream out = new XdrOutputStream(simpleServer.getOutputStream());
        out.writeInt(disconnectOperation);
        out.flush();
        Thread.sleep(500);
        assertFalse("Expected disconnected channel", channel.isConnected());
    }
}
Also used : SimpleServer(org.firebirdsql.common.SimpleServer) SQLException(java.sql.SQLException) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream)

Aggregations

XdrOutputStream (org.firebirdsql.gds.impl.wire.XdrOutputStream)41 SQLException (java.sql.SQLException)25 IOException (java.io.IOException)24 FbExceptionBuilder (org.firebirdsql.gds.ng.FbExceptionBuilder)22 SimpleServer (org.firebirdsql.common.SimpleServer)4 Encoding (org.firebirdsql.encodings.Encoding)3 XdrInputStream (org.firebirdsql.gds.impl.wire.XdrInputStream)3 Test (org.junit.Test)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 SQLNonTransientConnectionException (java.sql.SQLNonTransientConnectionException)2 SQLNonTransientException (java.sql.SQLNonTransientException)2 BlobParameterBuffer (org.firebirdsql.gds.BlobParameterBuffer)2 GenericResponse (org.firebirdsql.gds.ng.wire.GenericResponse)2 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)1 SQLTimeoutException (java.sql.SQLTimeoutException)1 BitSet (java.util.BitSet)1 WireCrypt (org.firebirdsql.gds.ng.WireCrypt)1 ClientAuthBlock (org.firebirdsql.gds.ng.wire.auth.ClientAuthBlock)1 EncryptionIdentifier (org.firebirdsql.gds.ng.wire.crypt.EncryptionIdentifier)1