Search in sources :

Example 1 with StatementType

use of org.firebirdsql.gds.ng.StatementType 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)

Aggregations

IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 SQLWarning (java.sql.SQLWarning)1 FbExceptionBuilder (org.firebirdsql.gds.ng.FbExceptionBuilder)1 StatementState (org.firebirdsql.gds.ng.StatementState)1 StatementType (org.firebirdsql.gds.ng.StatementType)1 WarningMessageCallback (org.firebirdsql.gds.ng.WarningMessageCallback)1