Search in sources :

Example 6 with StatementContext

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

the class RowCountResultSet method getNextRowCore.

/**
 * Return the requested values computed from the next row (if any)
 * <p>
 * @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;
    beginTime = getCurrentTimeMillis();
    if (virginal) {
        if (offsetMethod != null) {
            DataValueDescriptor offVal = (DataValueDescriptor) offsetMethod.invoke(activation);
            if (offVal.isNotNull().getBoolean()) {
                offset = offVal.getLong();
                if (offset < 0) {
                    throw StandardException.newException(SQLState.LANG_INVALID_ROW_COUNT_OFFSET, Long.toString(offset));
                } else {
                    offset = offVal.getLong();
                }
            } else {
                throw StandardException.newException(SQLState.LANG_ROW_COUNT_OFFSET_FIRST_IS_NULL, "OFFSET");
            }
        } else {
            // not given
            offset = 0;
        }
        if (fetchFirstMethod != null) {
            DataValueDescriptor fetchFirstVal = (DataValueDescriptor) fetchFirstMethod.invoke(activation);
            if (fetchFirstVal.isNotNull().getBoolean()) {
                fetchFirst = fetchFirstVal.getLong();
                // 
                if (hasJDBClimitClause && (fetchFirst == 0)) {
                    fetchFirst = Long.MAX_VALUE;
                }
                if (fetchFirst < 1) {
                    throw StandardException.newException(SQLState.LANG_INVALID_ROW_COUNT_FIRST, Long.toString(fetchFirst));
                }
            } else {
                throw StandardException.newException(SQLState.LANG_ROW_COUNT_OFFSET_FIRST_IS_NULL, "FETCH FIRST/NEXT");
            }
        }
        if (offset > 0) {
            // Only skip rows the first time around
            virginal = false;
            long offsetCtr = offset;
            do {
                result = source.getNextRowCore();
                offsetCtr--;
                if (result != null && offsetCtr >= 0) {
                    rowsFiltered++;
                } else {
                    break;
                }
            } while (true);
        } else {
            if (fetchFirstMethod != null && rowsFetched >= fetchFirst) {
                result = null;
            } else {
                result = source.getNextRowCore();
            }
        }
    } else {
        if (fetchFirstMethod != null && rowsFetched >= fetchFirst) {
            result = null;
        } else {
            result = source.getNextRowCore();
        }
    }
    if (result != null) {
        rowsFetched++;
        rowsSeen++;
    }
    setCurrentRow(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) StatementContext(org.apache.derby.iapi.sql.conn.StatementContext)

Example 7 with StatementContext

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

the class SavepointConstantAction method executeConstantAction.

// INTERFACE METHODS
/**
 *	This is the guts of the Execution-time logic for CREATE TABLE.
 *
 *	@see ConstantAction#executeConstantAction
 *
 * @exception StandardException		Thrown on failure
 */
public void executeConstantAction(Activation activation) throws StandardException {
    LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
    // Bug 4507 - savepoint not allowed inside trigger
    StatementContext stmtCtxt = lcc.getStatementContext();
    if (stmtCtxt != null && stmtCtxt.inTrigger())
        throw StandardException.newException(SQLState.NO_SAVEPOINT_IN_TRIGGER);
    if (savepointStatementType == 1) {
        // this is set savepoint
        if (// to enforce DB2 restriction which is savepoint name can't start with SYS
        savepointName.startsWith("SYS"))
            throw StandardException.newException(SQLState.INVALID_SCHEMA_SYS, "SYS");
        lcc.languageSetSavePoint(savepointName, savepointName);
    } else if (savepointStatementType == 2) {
        // this is rollback savepoint
        lcc.internalRollbackToSavepoint(savepointName, true, savepointName);
    } else {
        // this is release savepoint
        lcc.releaseSavePoint(savepointName, savepointName);
    }
}
Also used : LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) StatementContext(org.apache.derby.iapi.sql.conn.StatementContext)

Example 8 with StatementContext

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

the class NoRowsResultSetImpl method setup.

/**
 * Set up the result set for use. Should always be called from
 * <code>open()</code>.
 *
 * @exception StandardException thrown on error
 */
void setup() throws StandardException {
    isOpen = true;
    StatementContext sc = lcc.getStatementContext();
    sc.setTopResultSet(this, subqueryTrackingArray);
    // Pick up any materialized subqueries
    if (subqueryTrackingArray == null) {
        subqueryTrackingArray = sc.getSubqueryTrackingArray();
    }
}
Also used : StatementContext(org.apache.derby.iapi.sql.conn.StatementContext)

Example 9 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)

Example 10 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)

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