Search in sources :

Example 91 with ExecRow

use of org.apache.derby.iapi.sql.execute.ExecRow in project derby by apache.

the class DeleteCascadeResultSet method mergeResultSets.

@SuppressWarnings("UseOfObsoleteCollectionType")
private void mergeResultSets() throws StandardException {
    Vector<TemporaryRowHolder> sVector = activation.getParentResultSet(resultSetId);
    int size = sVector.size();
    // temporary result set.
    if (size > 1) {
        ExecRow row;
        int rowHolderId = 0;
        // copy all the vallues in the result set to the current resultset row holder
        while (rowHolderId < size) {
            if (rowHolderId == tempRowHolderId) {
                // skipping the row holder that  we are copying the rows into.
                rowHolderId++;
                continue;
            }
            TemporaryRowHolder currentRowHolder = sVector.elementAt(rowHolderId);
            CursorResultSet rs = currentRowHolder.getResultSet();
            rs.open();
            while ((row = rs.getNextRow()) != null) {
                rowHolder.insert(row);
            }
            rs.close();
            rowHolderId++;
        }
    }
}
Also used : CursorResultSet(org.apache.derby.iapi.sql.execute.CursorResultSet) TemporaryRowHolder(org.apache.derby.iapi.sql.execute.TemporaryRowHolder) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow)

Example 92 with ExecRow

use of org.apache.derby.iapi.sql.execute.ExecRow in project derby by apache.

the class DeleteResultSet method runFkChecker.

/**
 * Make sure foreign key constraints are not violated
 */
void runFkChecker(boolean restrictCheckOnly) throws StandardException {
    if (fkChecker != null) {
        /*
			** Second scan to make sure all the foreign key
			** constraints are ok.  We have to do this after
			** we have completed the deletes in case of self referencing
			** constraints.
			*/
        CursorResultSet rs = rowHolder.getResultSet();
        try {
            rs.open();
            ExecRow defRLRow;
            while ((defRLRow = rs.getNextRow()) != null) {
                // Argument "1" below: If a PK referenced by an FK is
                // deferred, require at least one to be present in the
                // primary table since we have deleted the row unless
                // postCheck == true, in which the call to postChecks does
                // the actual checking, and we need at least one to fulfill
                // the constraint.
                fkChecker.doPKCheck(activation, defRLRow, restrictCheckOnly, 1);
            }
            if (restrictCheckOnly) {
                fkChecker.postCheck();
            }
        } finally {
            rs.close();
        }
    }
}
Also used : CursorResultSet(org.apache.derby.iapi.sql.execute.CursorResultSet) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow)

Example 93 with ExecRow

use of org.apache.derby.iapi.sql.execute.ExecRow in project derby by apache.

the class DistinctScalarAggregateResultSet method loadSorter.

// /////////////////////////////////////////////////////////////////////////////
// 
// MISC UTILITIES
// 
// /////////////////////////////////////////////////////////////////////////////
/**
 * Load up the sorter.  Feed it every row from the
 * source scan.  If we have a vector aggregate, initialize
 * the aggregator for each source row.  When done, close
 * the source scan and open the sort.  Return the sort
 * scan controller.
 *
 * @exception StandardException thrown on failure.
 *
 * @return	the sort controller
 */
private ScanController loadSorter() throws StandardException {
    SortController sorter;
    ExecRow sourceRow;
    ExecIndexRow sortTemplateRow = getRowTemplate();
    int inputRowCountEstimate = (int) optimizerEstimatedRowCount;
    TransactionController tc = getTransactionController();
    /*
		** We have a distinct aggregate so, we'll need
		** to do a sort.  We use all of the sorting columns and
		** drop the aggregation on the distinct column.  Then
		** we'll feed this into the sorter again w/o the distinct
		** column in the ordering list.
		*/
    GenericAggregator[] aggsNoDistinct = getSortAggregators(aggInfoList, true, activation.getLanguageConnectionContext(), source);
    SortObserver sortObserver = new AggregateSortObserver(true, aggsNoDistinct, aggregates, sortTemplateRow);
    sortId = tc.createSort((Properties) null, sortTemplateRow.getRowArray(), order, sortObserver, // not in order
    false, // est rows, -1 means no idea
    inputRowCountEstimate, // est rowsize
    maxRowSize);
    sorter = tc.openSort(sortId);
    dropDistinctAggSort = true;
    while ((sourceRow = source.getNextRowCore()) != null) {
        sorter.insert(sourceRow.getRowArray());
        rowsInput++;
    }
    /*
		** End the sort and open up the result set
		*/
    sorter.completedInserts();
    scanController = tc.openSortScan(sortId, activation.getResultSetHoldability());
    /*
		** Aggs are initialized and input rows
		** are in order.
		*/
    inputRowCountEstimate = rowsInput;
    return scanController;
}
Also used : ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) TransactionController(org.apache.derby.iapi.store.access.TransactionController) Properties(java.util.Properties) ExecIndexRow(org.apache.derby.iapi.sql.execute.ExecIndexRow) SortObserver(org.apache.derby.iapi.store.access.SortObserver) SortController(org.apache.derby.iapi.store.access.SortController)

Example 94 with ExecRow

use of org.apache.derby.iapi.sql.execute.ExecRow in project derby by apache.

the class DistinctScanResultSet method getNextRowCore.

// 
// ResultSet interface (override methods from HashScanResultSet)
// 
/**
 * Return the next row (if any) from the scan (if open).
 *
 * @exception StandardException thrown on failure to get next row
 */
public ExecRow getNextRowCore() throws StandardException {
    if (isXplainOnlyMode())
        return null;
    ExecRow result = null;
    beginTime = getCurrentTimeMillis();
    if (isOpen) {
        if (firstNext) {
            element = hashtable.elements();
            firstNext = false;
        }
        if (element.hasMoreElements()) {
            DataValueDescriptor[] columns = unpackHashValue(element.nextElement());
            setCompatRow(compactRow, columns);
            rowsSeen++;
            result = compactRow;
        }
    // else done
    }
    setCurrentRow(result);
    nextTime += getElapsedMillis(beginTime);
    return result;
}
Also used : ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor)

Example 95 with ExecRow

use of org.apache.derby.iapi.sql.execute.ExecRow in project derby by apache.

the class GenericTriggerExecutor method executeSPS.

/**
 * Execute the given stored prepared statement.  We
 * just grab the prepared statement from the spsd,
 * get a new activation holder and let er rip.
 *
 * @param sps the SPS to execute
 * @param isWhen {@code true} if the SPS is for the WHEN clause,
 *               {@code false} otherwise
 * @return {@code true} if the SPS is for a WHEN clause and it evaluated
 *         to {@code TRUE}, {@code false} otherwise
 * @exception StandardException on error
 */
private boolean executeSPS(SPSDescriptor sps, boolean isWhen) throws StandardException {
    boolean recompile = false;
    boolean whenClauseWasTrue = false;
    // The prepared statement and the activation may already be available
    // if the trigger has been fired before in the same statement. (Only
    // happens with row triggers that are triggered by a statement that
    // touched multiple rows.) The WHEN clause and the trigger action have
    // their own prepared statement and activation. Fetch the correct set.
    ExecPreparedStatement ps = isWhen ? whenPS : actionPS;
    Activation spsActivation = isWhen ? spsWhenActivation : spsActionActivation;
    while (true) {
        /*
			** Only grab the ps the 1st time through.  This
			** way a row trigger doesn't do any unnecessary
			** setup work.
			*/
        if (ps == null || recompile) {
            // The SPS activation will set its parent activation from
            // the statement context. Reset it to the original parent
            // activation first so that it doesn't use the activation of
            // the previously executed SPS as parent. DERBY-6348.
            lcc.getStatementContext().setActivation(activation);
            /*
				** We need to clone the prepared statement so we don't
				** wind up marking that ps that is tied to sps as finished
				** during the course of execution.
				*/
            ps = sps.getPreparedStatement();
            ps = ps.getClone();
            // it should be valid since we've just prepared for it
            ps.setValid();
            spsActivation = ps.getActivation(lcc, false);
            /*
				** Normally, we want getSource() for an sps invocation
				** to be EXEC STATEMENT xxx, but in this case, since
				** we are executing the SPS in our own fashion, we want
				** the text to be the trigger action.  So set it accordingly.
				*/
            ps.setSource(sps.getText());
            ps.setSPSAction();
            // trigger fires multiple times.
            if (isWhen) {
                whenPS = ps;
                spsWhenActivation = spsActivation;
            } else {
                actionPS = ps;
                spsActionActivation = spsActivation;
            }
        }
        // save the active statement context for exception handling purpose
        StatementContext active_sc = lcc.getStatementContext();
        /*
			** Execute the activation.  If we have an error, we
			** are going to go to some extra work to pop off
			** our statement context.  This is because we are
			** a nested statement (we have 2 activations), but
			** we aren't a nested connection, so we have to
			** pop off our statementcontext to get error handling	
			** to work correctly.  This is normally a no-no, but
			** we are an unusual case.
			*/
        try {
            // This is a substatement; for now, we do not set any timeout
            // for it. We might change this behaviour later, by linking
            // timeout to its parent statement's timeout settings.
            ResultSet rs = ps.executeSubStatement(activation, spsActivation, false, 0L);
            if (isWhen) {
                // This is a WHEN clause. Expect a single BOOLEAN value
                // to be returned.
                ExecRow row = rs.getNextRow();
                if (SanityManager.DEBUG && row.nColumns() != 1) {
                    SanityManager.THROWASSERT("Expected WHEN clause to have exactly " + "one column, found: " + row.nColumns());
                }
                DataValueDescriptor value = row.getColumn(1);
                if (SanityManager.DEBUG) {
                    SanityManager.ASSERT(value instanceof SQLBoolean);
                }
                whenClauseWasTrue = !value.isNull() && value.getBoolean();
                if (SanityManager.DEBUG) {
                    SanityManager.ASSERT(rs.getNextRow() == null, "WHEN clause returned more than one row");
                }
            } else if (rs.returnsRows()) {
                // The result set was opened in ps.execute()
                while (rs.getNextRow() != null) {
                }
            }
            rs.close();
        } catch (StandardException e) {
            /* 
				** When a trigger SPS action is executed and results in 
				** an exception, the system needs to clean up the active 
				** statement context(SC) and the trigger execution context
				** (TEC) in language connection context(LCC) properly (e.g.:  
				** "Maximum depth triggers exceeded" exception); otherwise, 
				** this will leave old TECs lingering and may result in 
				** subsequent statements within the same connection to throw 
				** the same exception again prematurely.  
				**    
				** A new statement context will be created for the SPS before
				** it is executed.  However, it is possible for some 
				** StandardException to be thrown before a new statement 
				** context is pushed down to the context stack; hence, the 
				** trigger executor needs to ensure that the current active SC 
				** is associated with the SPS, so that it is cleaning up the 
				** right statement context in LCC. 
                **
                ** It is also possible that the error has already been handled
                ** on a lower level, especially if the trigger re-enters the
                ** JDBC layer. In that case, the current SC will be null.
				**    
				** When the active SC is cleaned up, the TEC will be removed
				** from LCC and the SC object will be popped off from the LCC 
				** as part of cleanupOnError logic.  
				 */
            /* retrieve the current active SC */
            StatementContext sc = lcc.getStatementContext();
            /* make sure that the cleanup is on the new SC */
            if (sc != null && active_sc != sc) {
                sc.cleanupOnError(e);
            }
            /* Handle dynamic recompiles */
            if (e.getMessageId().equals(SQLState.LANG_STATEMENT_NEEDS_RECOMPILE)) {
                recompile = true;
                sps.revalidate(lcc);
                continue;
            }
            spsActivation.close();
            throw e;
        }
        /* Done with execution without any recompiles */
        return whenClauseWasTrue;
    }
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) ExecPreparedStatement(org.apache.derby.iapi.sql.execute.ExecPreparedStatement) ResultSet(org.apache.derby.iapi.sql.ResultSet) CursorResultSet(org.apache.derby.iapi.sql.execute.CursorResultSet) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) Activation(org.apache.derby.iapi.sql.Activation) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) SQLBoolean(org.apache.derby.iapi.types.SQLBoolean) StatementContext(org.apache.derby.iapi.sql.conn.StatementContext)

Aggregations

ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)155 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)62 ExecIndexRow (org.apache.derby.iapi.sql.execute.ExecIndexRow)34 RowLocation (org.apache.derby.iapi.types.RowLocation)27 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)23 ConglomerateController (org.apache.derby.iapi.store.access.ConglomerateController)22 ScanController (org.apache.derby.iapi.store.access.ScanController)22 SQLVarchar (org.apache.derby.iapi.types.SQLVarchar)22 SQLChar (org.apache.derby.iapi.types.SQLChar)21 SQLLongint (org.apache.derby.iapi.types.SQLLongint)21 UUID (org.apache.derby.catalog.UUID)19 Properties (java.util.Properties)12 TransactionController (org.apache.derby.iapi.store.access.TransactionController)12 CursorResultSet (org.apache.derby.iapi.sql.execute.CursorResultSet)11 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)10 ConglomerateDescriptor (org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)10 DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)10 UserType (org.apache.derby.iapi.types.UserType)9 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)8 ConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor)7