Search in sources :

Example 11 with StatementContext

use of org.apache.derby.iapi.sql.conn.StatementContext in project derby by apache.

the class GenericLanguageConnectionContext method pushCompilerContext.

/**
 * Push a CompilerContext on the context stack with
 * the passed in schema sd as the default schema
 * we compile against.
 *
 * @param sd the default schema
 *
 * @return the compiler context
 *
 * For the parameter sd, there are 3 possible values(of interest) that can
 * get passed into this method:
 *
 * a) A null SchemaDescriptor which indicates to the system to use the
 *    CURRENT SCHEMA as the compilation schema.
 *
 * b) A SchemaDescriptor with its UUID == null, this indicates that either
 *    the schema has not been physically created yet or that the LCC's
 *    getDefaultSchema() is not yet up-to-date with its actual UUID.
 *    The system will use the CURRENT SCHEMA as the compilation schema.
 *
 * c) A SchemaDescriptor with its UUID != null, this means that the schema
 *    has been physically created.  The system will use this schema as the
 *    compilation schema (e.g.: for trigger or view recompilation cases,
 *    etc.).
 *
 * The compiler context's compilation schema will be set accordingly based
 * on the given input above.
 */
public CompilerContext pushCompilerContext(SchemaDescriptor sd) {
    CompilerContext cc;
    boolean firstCompilerContext = false;
    // DEBUG   END
    cc = (CompilerContext) (getContextManager().getContext(CompilerContext.CONTEXT_ID));
    /*
        ** If there is no compiler context, this is the first one on the
        ** stack, so don't pop it when we're done (saves time).
        */
    if (cc == null) {
        firstCompilerContext = true;
    }
    if (cc == null || cc.getInUse()) {
        cc = new CompilerContextImpl(getContextManager(), this, tcf);
        if (firstCompilerContext) {
            cc.firstOnStack();
        }
    } else {
        /* Reset the next column,table, subquery and ResultSet numbers at 
            * the beginning of each statement 
            */
        cc.resetContext();
    }
    cc.setInUse(true);
    StatementContext sc = getStatementContext();
    if (sc.getSystemCode())
        cc.setReliability(CompilerContext.INTERNAL_SQL_LEGAL);
    /*
         * Set the compilation schema when its UUID is available.
         * i.e.:  Schema may not have been physically created yet, so
         *        its UUID will be null.
         * 
         * o For trigger SPS recompilation, the system must use its
         *   compilation schema to recompile the statement. 
         * 
         * o For view recompilation, we set the compilation schema
         *   for this compiler context if its UUID is available.
         *   Otherwise, the compilation schema will be determined
         *   at execution time of view creation.
         */
    if (sd != null && sd.getUUID() != null) {
        cc.setCompilationSchema(sd);
    }
    return cc;
}
Also used : CompilerContext(org.apache.derby.iapi.sql.compile.CompilerContext) CompilerContextImpl(org.apache.derby.impl.sql.compile.CompilerContextImpl) StatementContext(org.apache.derby.iapi.sql.conn.StatementContext)

Example 12 with StatementContext

use of org.apache.derby.iapi.sql.conn.StatementContext in project derby by apache.

the class BasicDependencyManager method addInMemoryDependency.

/**
 * Adds the dependency as an in-memory dependency.
 *
 * @param d the dependent
 * @param p the provider
 * @param cm context manager
 * @throws StandardException if adding the dependency fails
 * @see #addStoredDependency
 */
private synchronized void addInMemoryDependency(Dependent d, Provider p, ContextManager cm) throws StandardException {
    Dependency dy = new BasicDependency(d, p);
    // Duplicate dependencies are not added to the lists.
    // If we find that the dependency we are trying to add in
    // one list is a duplicate, then it should be a duplicate in the
    // other list.
    boolean addedToProvs = false;
    boolean addedToDeps = addDependencyToTable(dependents, d.getObjectID(), dy);
    if (addedToDeps) {
        addedToProvs = addDependencyToTable(providers, p.getObjectID(), dy);
    } else if (SanityManager.DEBUG) {
        addedToProvs = addDependencyToTable(providers, p.getObjectID(), dy);
    }
    // Dependency should have been added to both or neither.
    if (SanityManager.DEBUG) {
        if (addedToDeps != addedToProvs) {
            SanityManager.THROWASSERT("addedToDeps (" + addedToDeps + ") and addedToProvs (" + addedToProvs + ") are expected to agree");
        }
    }
    // Add the dependency to the StatementContext, so that
    // it can be cleared on a pre-execution error.
    StatementContext sc = (StatementContext) cm.getContext(org.apache.derby.shared.common.reference.ContextId.LANG_STATEMENT);
    sc.addDependency(dy);
}
Also used : Dependency(org.apache.derby.iapi.sql.depend.Dependency) StatementContext(org.apache.derby.iapi.sql.conn.StatementContext)

Example 13 with StatementContext

use of org.apache.derby.iapi.sql.conn.StatementContext in project derby by apache.

the class BasicNoPutResultSetImpl method checkCancellationFlag.

/**
 * Checks whether the currently executing statement has been cancelled.
 * This is done by checking the statement's allocated StatementContext
 * object.
 *
 * @see StatementContext
 */
public void checkCancellationFlag() throws StandardException {
    LanguageConnectionContext lcc = getLanguageConnectionContext();
    StatementContext localStatementContext = lcc.getStatementContext();
    if (localStatementContext == null) {
        return;
    }
    InterruptStatus.throwIf(lcc);
    if (localStatementContext.isCancelled()) {
        throw StandardException.newException(SQLState.LANG_STATEMENT_CANCELLED_OR_TIMED_OUT);
    }
}
Also used : LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) StatementContext(org.apache.derby.iapi.sql.conn.StatementContext)

Example 14 with StatementContext

use of org.apache.derby.iapi.sql.conn.StatementContext in project derby by apache.

the class ProjectRestrictResultSet method getNextRowCore.

/**
 * Return the requested values computed
 * from the next row (if any) for which
 * the restriction evaluates to true.
 * <p>
 * restriction and projection parameters
 * are evaluated for each row.
 *
 * @exception StandardException thrown on failure.
 * @exception StandardException ResultSetNotOpen thrown if not yet open.
 *
 * @return the next row in the result
 */
public ExecRow getNextRowCore() throws StandardException {
    if (isXplainOnlyMode())
        return null;
    ExecRow candidateRow = null;
    ExecRow result = null;
    boolean restrict = false;
    DataValueDescriptor restrictBoolean;
    long beginRT = 0;
    /* Return null if open was short circuited by false constant expression */
    if (shortCircuitOpen) {
        return result;
    }
    beginTime = getCurrentTimeMillis();
    do {
        if (validatingCheckConstraint) {
            candidateRow = null;
            while (rowLocations.hasMoreElements() && candidateRow == null) {
                DataValueDescriptor[] row = (DataValueDescriptor[]) rowLocations.nextElement();
                RowLocation rl = (RowLocation) ((SQLRef) row[0]).getObject();
                ((ValidateCheckConstraintResultSet) source).positionScanAtRowLocation(rl);
                candidateRow = source.getNextRowCore();
            // if null (deleted), we move to next
            }
        } else {
            candidateRow = source.getNextRowCore();
        }
        if (candidateRow != null) {
            beginRT = getCurrentTimeMillis();
            /* If restriction is null, then all rows qualify */
            if (restriction == null) {
                restrict = true;
            } else {
                setCurrentRow(candidateRow);
                restrictBoolean = (DataValueDescriptor) restriction.invoke(activation);
                restrictionTime += getElapsedMillis(beginRT);
                // if the result is null, we make it false --
                // so the row won't be returned.
                restrict = ((!restrictBoolean.isNull()) && restrictBoolean.getBoolean());
                if (!restrict) {
                    rowsFiltered++;
                }
            }
            /* Update the run time statistics */
            rowsSeen++;
        }
    } while ((candidateRow != null) && (!restrict));
    if (candidateRow != null) {
        beginRT = getCurrentTimeMillis();
        result = doProjection(candidateRow);
        projectionTime += getElapsedMillis(beginRT);
    } else /* Clear the current row, if null */
    {
        clearCurrentRow();
    }
    currentRow = result;
    if (runTimeStatsOn) {
        if (!isTopResultSet) {
            /* This is simply for RunTimeStats */
            /* We first need to get the subquery tracking array via the StatementContext */
            StatementContext sc = activation.getLanguageConnectionContext().getStatementContext();
            subqueryTrackingArray = sc.getSubqueryTrackingArray();
        }
        nextTime += getElapsedMillis(beginTime);
    }
    return result;
}
Also used : ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) RowLocation(org.apache.derby.iapi.types.RowLocation) StatementContext(org.apache.derby.iapi.sql.conn.StatementContext)

Example 15 with StatementContext

use of org.apache.derby.iapi.sql.conn.StatementContext in project derby by apache.

the class HashTableResultSet method getNextRowCore.

/**
 * Return the requested values computed
 * from the next row (if any) for which
 * the restriction evaluates to true.
 * <p>
 * restriction and projection parameters
 * are evaluated for each row.
 *
 * @exception StandardException thrown on failure.
 * @exception StandardException ResultSetNotOpen thrown if not yet open.
 *
 * @return the next row in the result
 */
public ExecRow getNextRowCore() throws StandardException {
    if (isXplainOnlyMode())
        return null;
    ExecRow result = null;
    DataValueDescriptor[] columns = null;
    beginTime = getCurrentTimeMillis();
    if (isOpen) {
        /* We use a do/while loop to ensure that we continue down
			 * the duplicate chain, if one exists, until we find a
			 * row that matches on all probe predicates (or the
			 * duplicate chain is exhausted.)
			 */
        do {
            if (firstNext) {
                firstNext = false;
                /* Hash key could be either a single column or multiple 
                     * columns.  If a single column, then it is the datavalue 
                     * wrapper, otherwise it is a KeyHasher.
					 */
                Object hashEntry;
                if (keyColumns.length == 1) {
                    hashEntry = ht.get(nextQualifiers[0][0].getOrderable());
                } else {
                    KeyHasher mh = new KeyHasher(keyColumns.length);
                    for (int index = 0; index < keyColumns.length; index++) {
                        // RESOLVE (mikem) - will need to change when we
                        // support OR's in qualifiers.
                        mh.setObject(index, nextQualifiers[0][index].getOrderable());
                    }
                    hashEntry = ht.get(mh);
                }
                if (hashEntry instanceof List) {
                    entryVector = (List) hashEntry;
                    entryVectorSize = entryVector.size();
                    columns = (DataValueDescriptor[]) entryVector.get(0);
                } else {
                    entryVector = null;
                    entryVectorSize = 0;
                    columns = (DataValueDescriptor[]) hashEntry;
                }
            } else if (numFetchedOnNext < entryVectorSize) {
                // We are walking a list and there are more rows left.
                columns = (DataValueDescriptor[]) entryVector.get(numFetchedOnNext);
            }
            if (columns != null) {
                if (SanityManager.DEBUG) {
                    // Columns is really a Storable[]
                    for (int i = 0; i < columns.length; i++) {
                        if (!(columns[i] instanceof Storable)) {
                            SanityManager.THROWASSERT("columns[" + i + "] expected to be Storable, not " + columns[i].getClass().getName());
                        }
                    }
                }
                // See if the entry satisfies all of the other qualifiers
                boolean qualifies = true;
                if (SanityManager.DEBUG) {
                    // we don't support 2 d qualifiers yet.
                    SanityManager.ASSERT(nextQualifiers.length == 1);
                }
                for (int index = 0; index < nextQualifiers[0].length; index++) {
                    Qualifier q = nextQualifiers[0][index];
                    qualifies = columns[q.getColumnId()].compare(q.getOperator(), q.getOrderable(), q.getOrderedNulls(), q.getUnknownRV());
                    if (q.negateCompareResult()) {
                        qualifies = !(qualifies);
                    }
                    // Stop if any predicate fails
                    if (!qualifies) {
                        break;
                    }
                }
                if (qualifies) {
                    for (int index = 0; index < columns.length; index++) {
                        nextCandidate.setColumn(index + 1, columns[index]);
                    }
                    result = doProjection(nextCandidate);
                } else {
                    result = null;
                }
                numFetchedOnNext++;
            } else {
                result = null;
            }
        } while (result == null && numFetchedOnNext < entryVectorSize);
    }
    setCurrentRow(result);
    nextTime += getElapsedMillis(beginTime);
    if (runTimeStatsOn) {
        if (!isTopResultSet) {
            /* This is simply for RunTimeStats */
            /* We first need to get the subquery tracking array via the StatementContext */
            StatementContext sc = activation.getLanguageConnectionContext().getStatementContext();
            subqueryTrackingArray = sc.getSubqueryTrackingArray();
        }
        nextTime += getElapsedMillis(beginTime);
    }
    return result;
}
Also used : KeyHasher(org.apache.derby.iapi.store.access.KeyHasher) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) List(java.util.List) Qualifier(org.apache.derby.iapi.store.access.Qualifier) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) Storable(org.apache.derby.iapi.services.io.Storable) StatementContext(org.apache.derby.iapi.sql.conn.StatementContext)

Aggregations

StatementContext (org.apache.derby.iapi.sql.conn.StatementContext)26 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)9 Activation (org.apache.derby.iapi.sql.Activation)5 ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)5 ResultSet (org.apache.derby.iapi.sql.ResultSet)4 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)4 SQLWarning (java.sql.SQLWarning)3 CompilerContext (org.apache.derby.iapi.sql.compile.CompilerContext)3 SQLSessionContext (org.apache.derby.iapi.sql.conn.SQLSessionContext)3 CursorActivation (org.apache.derby.iapi.sql.execute.CursorActivation)3 TransactionController (org.apache.derby.iapi.store.access.TransactionController)3 XATransactionController (org.apache.derby.iapi.store.access.XATransactionController)3 StandardException (org.apache.derby.shared.common.error.StandardException)3 DataTruncation (java.sql.DataTruncation)2 GeneratedClass (org.apache.derby.iapi.services.loader.GeneratedClass)2 ResultDescription (org.apache.derby.iapi.sql.ResultDescription)2 ExecCursorTableReference (org.apache.derby.iapi.sql.execute.ExecCursorTableReference)2 Timestamp (java.sql.Timestamp)1 List (java.util.List)1 UUID (org.apache.derby.catalog.UUID)1