Search in sources :

Example 1 with StatementState

use of org.firebirdsql.gds.ng.StatementState in project jaybird by FirebirdSQL.

the class V10Statement method prepare.

@Override
public void prepare(final String statementText) throws SQLException {
    try {
        synchronized (getSynchronizationObject()) {
            checkTransactionActive(getTransaction());
            final StatementState currentState = getState();
            if (!isPrepareAllowed(currentState)) {
                throw new SQLNonTransientException(String.format("Current statement state (%s) does not allow call to prepare", currentState));
            }
            resetAll();
            final FbWireDatabase db = getDatabase();
            if (currentState == StatementState.NEW) {
                try {
                    sendAllocate();
                    getXdrOut().flush();
                } catch (IOException ex) {
                    switchState(StatementState.ERROR);
                    throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(ex).toSQLException();
                }
                try {
                    processAllocateResponse(db.readGenericResponse(getStatementWarningCallback()));
                } catch (IOException ex) {
                    switchState(StatementState.ERROR);
                    throw new FbExceptionBuilder().exception(ISCConstants.isc_net_read_err).cause(ex).toSQLException();
                }
            } else {
                checkStatementValid();
            }
            try {
                sendPrepare(statementText);
                getXdrOut().flush();
            } catch (IOException ex) {
                switchState(StatementState.ERROR);
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(ex).toSQLException();
            }
            try {
                processPrepareResponse(db.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 : StatementState(org.firebirdsql.gds.ng.StatementState) SQLNonTransientException(java.sql.SQLNonTransientException) SQLException(java.sql.SQLException) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) IOException(java.io.IOException)

Example 2 with StatementState

use of org.firebirdsql.gds.ng.StatementState in project jaybird by FirebirdSQL.

the class V10Statement method execute.

@Override
public void execute(final RowValue parameters) throws SQLException {
    final StatementState initialState = getState();
    try {
        synchronized (getSynchronizationObject()) {
            checkStatementValid();
            checkTransactionActive(getTransaction());
            validateParameters(parameters);
            reset(false);
            switchState(StatementState.EXECUTING);
            final StatementType statementType = getType();
            final boolean hasSingletonResult = hasSingletonResult();
            int expectedResponseCount = 0;
            try {
                if (hasSingletonResult) {
                    expectedResponseCount++;
                }
                sendExecute(hasSingletonResult ? WireProtocolConstants.op_execute2 : WireProtocolConstants.op_execute, parameters);
                expectedResponseCount++;
                getXdrOut().flush();
            } catch (IOException ex) {
                switchState(StatementState.ERROR);
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(ex).toSQLException();
            }
            final WarningMessageCallback statementWarningCallback = getStatementWarningCallback();
            try {
                final FbWireDatabase db = getDatabase();
                try {
                    expectedResponseCount--;
                    Response response = db.readResponse(statementWarningCallback);
                    if (hasSingletonResult) {
                        /* A type with a singleton result (ie an execute procedure with return fields), doesn't actually
                             * have a result set that will be fetched, instead we have a singleton result if we have fields
                             */
                        statementListenerDispatcher.statementExecuted(this, false, true);
                        if (response instanceof SqlResponse) {
                            processExecuteSingletonResponse((SqlResponse) response);
                            expectedResponseCount--;
                            response = db.readResponse(statementWarningCallback);
                        } else {
                            // We didn't get an op_sql_response first, something is iffy, maybe cancellation or very low level problem?
                            // We don't expect any more responses after this
                            expectedResponseCount = 0;
                            SQLWarning sqlWarning = new SQLWarning("Expected an SqlResponse, instead received a " + response.getClass().getName());
                            log.warn(sqlWarning.getMessage(), sqlWarning);
                            statementWarningCallback.processWarning(sqlWarning);
                        }
                        setAllRowsFetched(true);
                    } else {
                        // A normal execute is never a singleton result (even if it only produces a single result)
                        statementListenerDispatcher.statementExecuted(this, hasFields(), false);
                    }
                    // This should always be a GenericResponse, otherwise something went fundamentally wrong anyway
                    processExecuteResponse((GenericResponse) response);
                } finally {
                    db.consumePackets(expectedResponseCount, getStatementWarningCallback());
                }
                if (getState() != StatementState.ERROR) {
                    switchState(statementType.isTypeWithCursor() ? StatementState.CURSOR_OPEN : StatementState.PREPARED);
                }
            } catch (IOException ex) {
                switchState(StatementState.ERROR);
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_read_err).cause(ex).toSQLException();
            }
        }
    } catch (SQLException e) {
        if (getState() != StatementState.ERROR) {
            switchState(initialState);
        }
        exceptionListenerDispatcher.errorOccurred(e);
        throw e;
    }
}
Also used : SQLWarning(java.sql.SQLWarning) StatementState(org.firebirdsql.gds.ng.StatementState) WarningMessageCallback(org.firebirdsql.gds.ng.WarningMessageCallback) SQLException(java.sql.SQLException) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) StatementType(org.firebirdsql.gds.ng.StatementType) IOException(java.io.IOException)

Example 3 with StatementState

use of org.firebirdsql.gds.ng.StatementState in project jaybird by FirebirdSQL.

the class V11Statement method prepare.

@Override
public void prepare(final String statementText) throws SQLException {
    try {
        synchronized (getSynchronizationObject()) {
            checkTransactionActive(getTransaction());
            final StatementState currentState = getState();
            if (!isPrepareAllowed(currentState)) {
                throw new SQLNonTransientException(String.format("Current statement state (%s) does not allow call to prepare", currentState));
            }
            resetAll();
            int expectedResponseCount = 0;
            try {
                if (currentState == StatementState.NEW) {
                    sendAllocate();
                    expectedResponseCount++;
                } else {
                    checkStatementValid();
                }
                sendPrepare(statementText);
                expectedResponseCount++;
                getXdrOut().flush();
            } catch (IOException ex) {
                switchState(StatementState.ERROR);
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(ex).toSQLException();
            }
            try {
                final FbWireDatabase db = getDatabase();
                try {
                    if (currentState == StatementState.NEW) {
                        expectedResponseCount--;
                        processAllocateResponse(db.readGenericResponse(getStatementWarningCallback()));
                    }
                    expectedResponseCount--;
                    processPrepareResponse(db.readGenericResponse(getStatementWarningCallback()));
                } finally {
                    db.consumePackets(expectedResponseCount, 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 : StatementState(org.firebirdsql.gds.ng.StatementState) SQLNonTransientException(java.sql.SQLNonTransientException) SQLException(java.sql.SQLException) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) IOException(java.io.IOException) FbWireDatabase(org.firebirdsql.gds.ng.wire.FbWireDatabase)

Aggregations

IOException (java.io.IOException)3 SQLException (java.sql.SQLException)3 FbExceptionBuilder (org.firebirdsql.gds.ng.FbExceptionBuilder)3 StatementState (org.firebirdsql.gds.ng.StatementState)3 SQLNonTransientException (java.sql.SQLNonTransientException)2 SQLWarning (java.sql.SQLWarning)1 StatementType (org.firebirdsql.gds.ng.StatementType)1 WarningMessageCallback (org.firebirdsql.gds.ng.WarningMessageCallback)1 FbWireDatabase (org.firebirdsql.gds.ng.wire.FbWireDatabase)1