Search in sources :

Example 26 with SQLWarning

use of java.sql.SQLWarning in project derby by apache.

the class NetworkServerControlImpl method throwSQLWarning.

/**
 * Throw a SQL Warning which was sent over by a server
 * Format of the msg is SQLSTATE:localized message\nSQLSTATE:next localized message
 *
 * @param msg       msg containing SQL Warning
 *
 * @throws SQLWarning
 */
private void throwSQLWarning(String msg) throws SQLWarning {
    SQLWarning se = null;
    SQLWarning ne;
    SQLWarning ce = null;
    StringBuffer strbuf = new StringBuffer();
    StringTokenizer tokenizer = new StringTokenizer(msg, "\n");
    String sqlstate = null;
    String str;
    while (tokenizer.hasMoreTokens()) {
        str = tokenizer.nextToken();
        // start of the next message
        if (str.charAt(5) == ':') {
            if (strbuf.length() > 0) {
                if (se == null) {
                    se = new SQLWarning(strbuf.toString(), sqlstate);
                    ce = se;
                } else {
                    ne = new SQLWarning(strbuf.toString(), sqlstate);
                    ce.setNextException(ne);
                    ce = ne;
                }
                strbuf = new StringBuffer();
            }
            strbuf.append(str.substring(6));
            sqlstate = str.substring(0, 5);
        } else
            strbuf.append(str);
    }
    if (strbuf.length() > 0) {
        if (se == null) {
            se = new SQLWarning(strbuf.toString(), sqlstate);
            ce = se;
        } else {
            ne = new SQLWarning(strbuf.toString(), sqlstate);
            ce.setNextException(ne);
            ce = ne;
        }
    }
    throw se;
}
Also used : SQLWarning(java.sql.SQLWarning) StringTokenizer(java.util.StringTokenizer)

Example 27 with SQLWarning

use of java.sql.SQLWarning in project derby by apache.

the class EmbedResultSet method updateRow.

/**
 * JDBC 2.0
 *
 * Update the underlying database with the new contents of the
 * current row.  Cannot be called when on the insert row.
 *
 * @exception SQLException if a database-access error occurs, or
 * if called when on the insert row
 */
public void updateRow() throws SQLException {
    synchronized (getConnectionSynchronization()) {
        checksBeforeUpdateOrDelete("updateRow", -1);
        // Check that the cursor is not positioned on insertRow
        checkNotOnInsertRow();
        setupContextStack();
        LanguageConnectionContext lcc = getLanguageConnectionContext(getEmbedConnection());
        StatementContext statementContext = null;
        try {
            if (// nothing got updated on this row
            currentRowHasBeenUpdated == false)
                // nothing to do since no updates were made to this row
                return;
            // now construct the update where current of sql
            boolean foundOneColumnAlready = false;
            StringBuffer updateWhereCurrentOfSQL = new StringBuffer("UPDATE ");
            CursorActivation activation = lcc.lookupCursorActivation(getCursorName());
            ExecCursorTableReference targetTable = activation.getPreparedStatement().getTargetTable();
            // got the underlying (schema.)table name
            updateWhereCurrentOfSQL.append(getFullBaseTableName(targetTable));
            updateWhereCurrentOfSQL.append(" SET ");
            ResultDescription rd = theResults.getResultDescription();
            for (int i = 1; i <= rd.getColumnCount(); i++) {
                // in this for loop we are constructing columnname=?,... part of the update sql
                if (columnGotUpdated[i - 1]) {
                    // if the column got updated, do following
                    if (foundOneColumnAlready)
                        updateWhereCurrentOfSQL.append(",");
                    // using quotes around the column name to preserve case sensitivity
                    updateWhereCurrentOfSQL.append(IdUtil.normalToDelimited(rd.getColumnDescriptor(i).getName()) + "=?");
                    foundOneColumnAlready = true;
                }
            }
            // using quotes around the cursor name to preserve case sensitivity
            updateWhereCurrentOfSQL.append(" WHERE CURRENT OF " + IdUtil.normalToDelimited(getCursorName()));
            StatementContext currSC = lcc.getStatementContext();
            Activation parentAct = null;
            if (currSC != null) {
                parentAct = currSC.getActivation();
            }
            // Context used for preparing, don't set any timeout (use 0)
            statementContext = lcc.pushStatementContext(isAtomic, false, updateWhereCurrentOfSQL.toString(), null, false, 0L);
            // A priori, the new statement context inherits the activation of
            // the existing statementContext, so that that activation ends up
            // as parent of the new activation 'act' created below, which will
            // be the activation of the pushed statement context.
            statementContext.setActivation(parentAct);
            org.apache.derby.iapi.sql.PreparedStatement ps = lcc.prepareInternalStatement(updateWhereCurrentOfSQL.toString());
            Activation act = ps.getActivation(lcc, false);
            statementContext.setActivation(act);
            // in this for loop we are assigning values for parameters in sql constructed earlier with columnname=?,...
            for (int i = 1, paramPosition = 0; i <= rd.getColumnCount(); i++) {
                if (// if the column got updated, do following
                columnGotUpdated[i - 1])
                    act.getParameterValueSet().getParameterForSet(paramPosition++).setValue(updateRow.getColumn(i));
            }
            // Don't set any timeout when updating rows (use 0)
            // Execute the update where current of sql.
            org.apache.derby.iapi.sql.ResultSet rs = ps.executeSubStatement(activation, act, true, 0L);
            SQLWarning w = act.getWarnings();
            if (w != null) {
                addWarning(w);
            }
            act.close();
            // For forward only resultsets, after a update, the ResultSet will be positioned right before the next row.
            if (getType() == TYPE_FORWARD_ONLY) {
                currentRow = null;
            } else {
                movePosition(RELATIVE, 0, "relative");
            }
            lcc.popStatementContext(statementContext, null);
            InterruptStatus.restoreIntrFlagIfSeen(lcc);
        } catch (Throwable t) {
            throw closeOnTransactionError(t);
        } finally {
            if (statementContext != null)
                lcc.popStatementContext(statementContext, null);
            restoreContextStack();
            initializeUpdateRowModifiers();
        }
    }
}
Also used : SQLWarning(java.sql.SQLWarning) CursorActivation(org.apache.derby.iapi.sql.execute.CursorActivation) Activation(org.apache.derby.iapi.sql.Activation) CursorActivation(org.apache.derby.iapi.sql.execute.CursorActivation) StatementContext(org.apache.derby.iapi.sql.conn.StatementContext) ExecCursorTableReference(org.apache.derby.iapi.sql.execute.ExecCursorTableReference) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) ResultDescription(org.apache.derby.iapi.sql.ResultDescription) ResultSet(org.apache.derby.iapi.sql.ResultSet)

Example 28 with SQLWarning

use of java.sql.SQLWarning in project derby by apache.

the class EmbedResultSet method deleteRow.

/**
 * JDBC 2.0
 *
 * Delete the current row from the result set and the underlying
 * database.  Cannot be called when on the insert row.
 *
 * @exception SQLException if a database-access error occurs, or if
 * called when on the insert row.
 */
public void deleteRow() throws SQLException {
    synchronized (getConnectionSynchronization()) {
        checksBeforeUpdateOrDelete("deleteRow", -1);
        // Check that the cursor is not positioned on insertRow
        checkNotOnInsertRow();
        setupContextStack();
        LanguageConnectionContext lcc = getLanguageConnectionContext(getEmbedConnection());
        StatementContext statementContext = null;
        // now construct the delete where current of sql
        try {
            StringBuffer deleteWhereCurrentOfSQL = new StringBuffer("DELETE FROM ");
            CursorActivation activation = lcc.lookupCursorActivation(getCursorName());
            // get the underlying (schema.)table name
            deleteWhereCurrentOfSQL.append(getFullBaseTableName(activation.getPreparedStatement().getTargetTable()));
            // using quotes around the cursor name to preserve case sensitivity
            deleteWhereCurrentOfSQL.append(" WHERE CURRENT OF " + IdUtil.normalToDelimited(getCursorName()));
            StatementContext currSC = lcc.getStatementContext();
            Activation parentAct = null;
            if (currSC != null) {
                parentAct = currSC.getActivation();
            }
            // Context used for preparing, don't set any timeout (use 0)
            statementContext = lcc.pushStatementContext(isAtomic, false, deleteWhereCurrentOfSQL.toString(), null, false, 0L);
            // A priori, the new statement context inherits the activation
            // of the existing statementContext, so that that activation
            // ends up as parent of the new activation 'act' created below,
            // which will be the activation of the pushed statement
            // context.
            statementContext.setActivation(parentAct);
            org.apache.derby.iapi.sql.PreparedStatement ps = lcc.prepareInternalStatement(deleteWhereCurrentOfSQL.toString());
            // Get activation, so that we can get the warning from it
            Activation act = ps.getActivation(lcc, false);
            statementContext.setActivation(act);
            // Don't set any timeout when deleting rows (use 0)
            // execute delete where current of sql
            org.apache.derby.iapi.sql.ResultSet rs = ps.executeSubStatement(activation, act, true, 0L);
            SQLWarning w = act.getWarnings();
            if (w != null) {
                addWarning(w);
            }
            act.close();
            // After a delete, the ResultSet will be positioned right before
            // the next row.
            currentRow = null;
            lcc.popStatementContext(statementContext, null);
            InterruptStatus.restoreIntrFlagIfSeen(lcc);
        } catch (Throwable t) {
            throw closeOnTransactionError(t);
        } finally {
            if (statementContext != null)
                lcc.popStatementContext(statementContext, null);
            restoreContextStack();
            initializeUpdateRowModifiers();
        }
    }
}
Also used : SQLWarning(java.sql.SQLWarning) CursorActivation(org.apache.derby.iapi.sql.execute.CursorActivation) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) Activation(org.apache.derby.iapi.sql.Activation) CursorActivation(org.apache.derby.iapi.sql.execute.CursorActivation) ResultSet(org.apache.derby.iapi.sql.ResultSet) StatementContext(org.apache.derby.iapi.sql.conn.StatementContext)

Example 29 with SQLWarning

use of java.sql.SQLWarning in project derby by apache.

the class EmbedResultSet method movePosition.

protected boolean movePosition(int position, int row, String positionText) throws SQLException {
    // closing currentStream does not depend on the
    closeCurrentStream();
    // underlying connection.  Do this outside of
    // the connection synchronization.
    // checking result set closure does not depend
    checkExecIfClosed(positionText);
    if (isOnInsertRow) {
        moveToCurrentRow();
    }
    synchronized (getConnectionSynchronization()) {
        setupContextStack();
        try {
            LanguageConnectionContext lcc = getLanguageConnectionContext(getEmbedConnection());
            final ExecRow newRow;
            try {
                /* Push and pop a StatementContext around a next call
				 * so that the ResultSet will get correctly closed down
				 * on an error.
				 * (Cache the LanguageConnectionContext)
				 */
                StatementContext statementContext = lcc.pushStatementContext(isAtomic, concurrencyOfThisResultSet == java.sql.ResultSet.CONCUR_READ_ONLY, getSQLText(), getParameterValueSet(), false, timeoutMillis);
                switch(position) {
                    case BEFOREFIRST:
                        newRow = theResults.setBeforeFirstRow();
                        break;
                    case FIRST:
                        newRow = theResults.getFirstRow();
                        break;
                    case NEXT:
                        newRow = theResults.getNextRow();
                        break;
                    case LAST:
                        newRow = theResults.getLastRow();
                        break;
                    case AFTERLAST:
                        newRow = theResults.setAfterLastRow();
                        break;
                    case PREVIOUS:
                        newRow = theResults.getPreviousRow();
                        break;
                    case ABSOLUTE:
                        newRow = theResults.getAbsoluteRow(row);
                        break;
                    case RELATIVE:
                        newRow = theResults.getRelativeRow(row);
                        break;
                    default:
                        newRow = null;
                        if (SanityManager.DEBUG) {
                            SanityManager.THROWASSERT("Unexpected value for position - " + position);
                        }
                }
                lcc.popStatementContext(statementContext, null);
                InterruptStatus.restoreIntrFlagIfSeen(lcc);
            } catch (Throwable t) {
                /*
				 * Need to close the result set here because the error might
				 * cause us to lose the current connection if this is an XA
				 * connection and we won't be able to do the close later
				 */
                throw closeOnTransactionError(t);
            }
            SQLWarning w = theResults.getWarnings();
            if (w != null) {
                addWarning(w);
            }
            boolean onRow = (currentRow = newRow) != null;
            // false. This will cause a commit if autocommit = true.
            if (!onRow && (position == NEXT)) {
                // LanguageConnectionContext lcc = getEmbedConnection().getLanguageConnection();
                if (forMetaData && (lcc.getActivationCount() > 1)) {
                // we do not want to commit here as there seems to be other
                // statements/resultSets currently opened for this connection.
                } else if (owningStmt != null && owningStmt.getResultSetType() == TYPE_FORWARD_ONLY) {
                    // allow the satement to commit if required.
                    owningStmt.resultSetClosing(this);
                }
            }
            // Clear the indication of which columns were fetched as streams.
            if (columnUsedFlags != null)
                Arrays.fill(columnUsedFlags, false);
            if (columnGotUpdated != null && currentRowHasBeenUpdated) {
                initializeUpdateRowModifiers();
            }
            return onRow;
        } finally {
            restoreContextStack();
        }
    }
}
Also used : SQLWarning(java.sql.SQLWarning) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) StatementContext(org.apache.derby.iapi.sql.conn.StatementContext)

Example 30 with SQLWarning

use of java.sql.SQLWarning in project derby by apache.

the class DRDAConnThread method writeSQLDARD.

/**
 * Write SQLDARD
 *
 * SQLDARD : FDOCA EARLY ARRAY
 * SQL Descriptor Area Row Description with SQL Communications Area
 *
 * FORMAT FOR SQLAM &lt;= 6
 *   SQLCARD; ROW LID 0x64; ELEMENT TAKEN 0(all); REP FACTOR 1
 *   SQLNUMROW; ROW LID 0x68; ELEMENT TAKEN 0(all); REP FACTOR 1
 *   SQLDAROW; ROW LID 0x60; ELEMENT TAKEN 0(all); REP FACTOR 0(all)
 *
 * FORMAT FOR SQLAM &gt;= 7
 *   SQLCARD; ROW LID 0x64; ELEMENT TAKEN 0(all); REP FACTOR 1
 *   SQLDHROW; ROW LID 0xE0; ELEMENT TAKEN 0(all); REP FACTOR 1
 *   SQLNUMROW; ROW LID 0x68; ELEMENT TAKEN 0(all); REP FACTOR 1
 *
 * @param stmt  prepared statement
 *
 * @throws DRDAProtocolException
 * @throws SQLException
 */
private void writeSQLDARD(DRDAStatement stmt, boolean rtnOutput, SQLException e) throws DRDAProtocolException, SQLException {
    PreparedStatement ps = stmt.getPreparedStatement();
    ResultSetMetaData rsmeta = ps.getMetaData();
    ParameterMetaData pmeta = stmt.getParameterMetaData();
    int numElems = 0;
    if (e == null || e instanceof SQLWarning) {
        if (rtnOutput && (rsmeta != null)) {
            numElems = rsmeta.getColumnCount();
        } else if ((!rtnOutput) && (pmeta != null)) {
            numElems = pmeta.getParameterCount();
        }
    }
    writer.createDssObject();
    // all went well we will just write a null SQLCA
    writer.startDdm(CodePoint.SQLDARD);
    writeSQLCAGRP(e, 0, 0);
    if (sqlamLevel >= MGRLVL_7) {
        writeSQLDHROW(ps.getResultSetHoldability());
    }
    // SQLNUMROW
    if (SanityManager.DEBUG) {
        trace("num Elements = " + numElems);
    }
    writer.writeShort(numElems);
    for (int i = 0; i < numElems; i++) {
        writeSQLDAGRP(rsmeta, pmeta, i, rtnOutput);
    }
    writer.endDdmAndDss();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLWarning(java.sql.SQLWarning) EnginePreparedStatement(org.apache.derby.iapi.jdbc.EnginePreparedStatement) PreparedStatement(java.sql.PreparedStatement) ParameterMetaData(java.sql.ParameterMetaData)

Aggregations

SQLWarning (java.sql.SQLWarning)147 SQLException (java.sql.SQLException)47 Statement (java.sql.Statement)35 PreparedStatement (java.sql.PreparedStatement)30 Connection (java.sql.Connection)24 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 HashSet (java.util.HashSet)3 Properties (java.util.Properties)3 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)3