Search in sources :

Example 96 with SQLWarning

use of java.sql.SQLWarning in project jaybird by FirebirdSQL.

the class JnaConnection method processStatusVector.

protected void processStatusVector(ISC_STATUS[] statusVector, WarningMessageCallback warningMessageCallback) throws SQLException {
    if (warningMessageCallback == null) {
        throw new NullPointerException("warningMessageCallback is null");
    }
    boolean debug = log.isDebugEnabled();
    final FbExceptionBuilder builder = new FbExceptionBuilder();
    int vectorIndex = 0;
    processingLoop: while (vectorIndex < statusVector.length) {
        int arg = statusVector[vectorIndex++].intValue();
        int errorCode;
        switch(arg) {
            case isc_arg_gds:
                errorCode = statusVector[vectorIndex++].intValue();
                if (debug)
                    log.debug("readStatusVector arg:isc_arg_gds int: " + errorCode);
                if (errorCode != 0) {
                    builder.exception(errorCode);
                }
                break;
            case isc_arg_warning:
                errorCode = statusVector[vectorIndex++].intValue();
                if (debug)
                    log.debug("readStatusVector arg:isc_arg_warning int: " + errorCode);
                if (errorCode != 0) {
                    builder.warning(errorCode);
                }
                break;
            case isc_arg_interpreted:
            case isc_arg_string:
            case isc_arg_sql_state:
                long stringPointerAddress = statusVector[vectorIndex++].longValue();
                if (stringPointerAddress == 0L) {
                    log.warn("Received NULL pointer address for isc_arg_interpreted, isc_arg_string or isc_arg_sql_state");
                    break processingLoop;
                }
                Pointer stringPointer = new Pointer(stringPointerAddress);
                String stringValue = stringPointer.getString(0, getEncodingDefinition().getJavaEncodingName());
                if (arg != isc_arg_sql_state) {
                    if (debug)
                        log.debug("readStatusVector string: " + stringValue);
                    builder.messageParameter(stringValue);
                } else {
                    if (debug)
                        log.debug("readStatusVector sqlstate: " + stringValue);
                    builder.sqlState(stringValue);
                }
                break;
            case isc_arg_cstring:
                int stringLength = statusVector[vectorIndex++].intValue();
                long cStringPointerAddress = statusVector[vectorIndex++].longValue();
                Pointer cStringPointer = new Pointer(cStringPointerAddress);
                byte[] stringData = cStringPointer.getByteArray(0, stringLength);
                String cStringValue = getEncoding().decodeFromCharset(stringData);
                builder.messageParameter(cStringValue);
                break;
            case isc_arg_number:
                int intValue = statusVector[vectorIndex++].intValue();
                if (debug)
                    log.debug("readStatusVector arg:isc_arg_number int: " + intValue);
                builder.messageParameter(intValue);
                break;
            case isc_arg_end:
                break processingLoop;
            default:
                int e = statusVector[vectorIndex++].intValue();
                if (debug)
                    log.debug("readStatusVector arg: " + arg + " int: " + e);
                builder.messageParameter(e);
                break;
        }
    }
    if (!builder.isEmpty()) {
        SQLException exception = builder.toFlatSQLException();
        if (exception instanceof SQLWarning) {
            warningMessageCallback.processWarning((SQLWarning) exception);
        } else {
            throw exception;
        }
    }
}
Also used : SQLWarning(java.sql.SQLWarning) SQLException(java.sql.SQLException) Pointer(com.sun.jna.Pointer)

Example 97 with SQLWarning

use of java.sql.SQLWarning in project jaybird by FirebirdSQL.

the class TestDatabaseListenerDispatcher method testWarningReceived.

/**
 * Tests if calls to {@link org.firebirdsql.gds.ng.listeners.DatabaseListenerDispatcher#warningReceived(org.firebirdsql.gds.ng.FbDatabase, java.sql.SQLWarning)}
 * forwarded correctly.
 */
@Test
public void testWarningReceived() {
    final Expectations expectations = new Expectations();
    final SQLWarning warning = new SQLWarning();
    expectations.exactly(1).of(listener).warningReceived(database, warning);
    context.checking(expectations);
    dispatcher.warningReceived(database, warning);
}
Also used : Expectations(org.jmock.Expectations) SQLWarning(java.sql.SQLWarning) Test(org.junit.Test)

Example 98 with SQLWarning

use of java.sql.SQLWarning in project jaybird by FirebirdSQL.

the class TestDatabaseListenerDispatcher method testWarningReceived_withException.

/**
 * Tests if listeners throwing exceptions will still cause other listeners to be notified and not result in
 * exceptions thrown to call of the dispatcher.
 */
@Test
public void testWarningReceived_withException() {
    final DatabaseListener listener2 = context.mock(DatabaseListener.class, "listener2");
    dispatcher.addListener(listener2);
    final SQLWarning warning = new SQLWarning();
    final Expectations expectations = new Expectations();
    for (DatabaseListener currentListener : Arrays.asList(listener, listener2)) {
        expectations.exactly(1).of(currentListener).warningReceived(database, warning);
        expectations.will(throwException(new RuntimeException()));
    }
    context.checking(expectations);
    dispatcher.warningReceived(database, warning);
}
Also used : SQLWarning(java.sql.SQLWarning) Expectations(org.jmock.Expectations) Test(org.junit.Test)

Example 99 with SQLWarning

use of java.sql.SQLWarning in project jaybird by FirebirdSQL.

the class TestServiceListenerDispatcher method testWarningReceived_withException.

/**
 * Tests if listeners throwing exceptions will still cause other listeners to be notified and not result in
 * exceptions thrown to call of the dispatcher.
 */
@Test
public void testWarningReceived_withException() {
    final ServiceListener listener2 = context.mock(ServiceListener.class, "listener2");
    dispatcher.addListener(listener2);
    final SQLWarning warning = new SQLWarning();
    final Expectations expectations = new Expectations();
    for (ServiceListener currentListener : Arrays.asList(listener, listener2)) {
        expectations.exactly(1).of(currentListener).warningReceived(service, warning);
        expectations.will(throwException(new RuntimeException()));
    }
    context.checking(expectations);
    dispatcher.warningReceived(service, warning);
}
Also used : SQLWarning(java.sql.SQLWarning) Expectations(org.jmock.Expectations) Test(org.junit.Test)

Example 100 with SQLWarning

use of java.sql.SQLWarning 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

SQLWarning (java.sql.SQLWarning)153 SQLException (java.sql.SQLException)49 Statement (java.sql.Statement)37 PreparedStatement (java.sql.PreparedStatement)30 Connection (java.sql.Connection)26 ResultSet (java.sql.ResultSet)23 Test (org.testng.annotations.Test)19 Test (org.junit.Test)17 BaseTest (util.BaseTest)15 CallableStatement (java.sql.CallableStatement)13 ArrayList (java.util.ArrayList)9 JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)8 ResultSetMetaData (java.sql.ResultSetMetaData)6 Expectations (org.jmock.Expectations)6 IOException (java.io.IOException)4 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)3 PrestoWarning (com.facebook.presto.spi.PrestoWarning)3 Properties (java.util.Properties)3 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)3 StatementContext (org.apache.derby.iapi.sql.conn.StatementContext)3