Search in sources :

Example 6 with PreparedStatement

use of org.apache.derby.iapi.sql.PreparedStatement in project derby by apache.

the class EmbedStatement method executeStatement.

// ///////////////////////////////////////////////////////////////////////
// 
// Implementation specific methods
// 
// ///////////////////////////////////////////////////////////////////////
/**
 *		Execute the current statement.
 *	    @exception SQLException thrown on failure.
 */
boolean executeStatement(Activation a, boolean executeQuery, boolean executeUpdate) throws SQLException {
    synchronized (getConnectionSynchronization()) {
        if (SanityManager.DEBUG) {
            // Ensure that clearResultSets has been called
            // to fulfill [JDBC4: section 15.2.5 ]
            // A ResultSet object is implicitly closed when:
            // The associated Statement object is re-executed
            SanityManager.ASSERT(results == null);
            SanityManager.ASSERT(dynamicResults == null);
            SanityManager.ASSERT(autoGeneratedKeysResultSet == null);
        }
        // make sure there's context
        setupContextStack();
        boolean retval;
        pvs = a.getParameterValueSet();
        try {
            clearWarnings();
            if (!forMetaData) {
                // commit the last statement if needed
                commitIfNeeded();
                needCommit();
            } else {
                if (lcc.getActivationCount() > 1) {
                // we do not want to commit here as there seems to be other
                // statements/resultSets currently opened for this connection.
                } else {
                    // we can legitimately commit
                    commitIfNeeded();
                    needCommit();
                }
            }
            // Get the statement. We don't care if it's invalid, because it
            // will be recompiled when we execute it if needed (DERBY-3024).
            PreparedStatement ps = a.getPreparedStatement();
            if (cursorName != null) {
                a.setCursorName(cursorName);
            }
            boolean executeHoldable = getExecuteHoldable();
            a.setResultSetHoldability(executeHoldable);
            // reset the activation to clear warnings
            // and clear existing result sets in case this has been cached
            a.reset();
            a.setMaxRows(maxRows);
            ResultSet resultsToWrap = ps.execute(a, forMetaData, timeoutMillis);
            addWarning(ps.getCompileTimeWarnings());
            addWarning(a.getWarnings());
            if (resultsToWrap.returnsRows()) {
                // executeUpdate() is not allowed.
                if (executeUpdate) {
                    throw StandardException.newException(SQLState.LANG_INVALID_CALL_TO_EXECUTE_UPDATE);
                }
                EmbedResultSet lresults = factory.newEmbedResultSet(getEmbedConnection(), resultsToWrap, forMetaData, this, ps.isAtomic());
                results = lresults;
                // outside of finalization.
                if (a.isSingleExecution())
                    lresults.singleUseActivation = a;
                updateCount = -1L;
                retval = true;
            } else {
                // the auto-generated keys resultset will be null if used for other statement
                if (a.getAutoGeneratedKeysResultsetMode() && (resultsToWrap.getAutoGeneratedKeysResultset() != null)) {
                    resultsToWrap.getAutoGeneratedKeysResultset().open();
                    autoGeneratedKeysResultSet = factory.newEmbedResultSet(getEmbedConnection(), resultsToWrap.getAutoGeneratedKeysResultset(), false, this, ps.isAtomic());
                }
                updateCount = resultsToWrap.modifiedRowCount();
                // note that we have none.
                results = null;
                int dynamicResultCount = 0;
                if (a.getDynamicResults() != null) {
                    dynamicResultCount = processDynamicResults(a.getDynamicResults(), a.getMaxDynamicResults());
                }
                // Don't need the result set any more
                resultsToWrap.close();
                // doesn't return exactly one ResultSet.
                if (executeQuery && dynamicResultCount != 1) {
                    throw StandardException.newException(SQLState.LANG_INVALID_CALL_TO_EXECUTE_QUERY);
                }
                // returns ResultSets.
                if (executeUpdate && dynamicResultCount > 0) {
                    throw StandardException.newException(SQLState.LANG_INVALID_CALL_TO_EXECUTE_UPDATE);
                }
                if (dynamicResultCount == 0) {
                    if (a.isSingleExecution()) {
                        a.close();
                    }
                    if (!forMetaData)
                        commitIfNeeded();
                    else {
                        if (lcc.getActivationCount() > 1) {
                        // we do not want to commit here as there seems to be other
                        // statements/resultSets currently opened for this connection.
                        } else {
                            // we can legitimately commit
                            commitIfNeeded();
                        }
                    }
                }
                retval = (dynamicResultCount > 0);
            }
            InterruptStatus.restoreIntrFlagIfSeen(lcc);
        } catch (Throwable t) {
            if (a.isSingleExecution()) {
                try {
                    a.close();
                } catch (Throwable tt) {
                    ;
                }
            }
            throw handleException(t);
        } finally {
            restoreContextStack();
        }
        return retval;
    }
}
Also used : ResultSet(org.apache.derby.iapi.sql.ResultSet) PreparedStatement(org.apache.derby.iapi.sql.PreparedStatement)

Aggregations

PreparedStatement (org.apache.derby.iapi.sql.PreparedStatement)6 ResultSet (org.apache.derby.iapi.sql.ResultSet)4 ArrayList (java.util.ArrayList)1 UUID (org.apache.derby.catalog.UUID)1 Activation (org.apache.derby.iapi.sql.Activation)1 ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)1 NoPutResultSet (org.apache.derby.iapi.sql.execute.NoPutResultSet)1 ResultSetStatistics (org.apache.derby.iapi.sql.execute.ResultSetStatistics)1 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)1 NumberDataValue (org.apache.derby.iapi.types.NumberDataValue)1 CheckInfo (org.apache.derby.impl.sql.execute.DeferredConstraintsMemory.CheckInfo)1 RealAnyResultSetStatistics (org.apache.derby.impl.sql.execute.rts.RealAnyResultSetStatistics)1 RealDeleteCascadeResultSetStatistics (org.apache.derby.impl.sql.execute.rts.RealDeleteCascadeResultSetStatistics)1 RealDeleteResultSetStatistics (org.apache.derby.impl.sql.execute.rts.RealDeleteResultSetStatistics)1 RealDeleteVTIResultSetStatistics (org.apache.derby.impl.sql.execute.rts.RealDeleteVTIResultSetStatistics)1 RealInsertResultSetStatistics (org.apache.derby.impl.sql.execute.rts.RealInsertResultSetStatistics)1 RealInsertVTIResultSetStatistics (org.apache.derby.impl.sql.execute.rts.RealInsertVTIResultSetStatistics)1 RealJoinResultSetStatistics (org.apache.derby.impl.sql.execute.rts.RealJoinResultSetStatistics)1 RealMaterializedResultSetStatistics (org.apache.derby.impl.sql.execute.rts.RealMaterializedResultSetStatistics)1 RealNormalizeResultSetStatistics (org.apache.derby.impl.sql.execute.rts.RealNormalizeResultSetStatistics)1