Search in sources :

Example 66 with ExecRow

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

the class ScrollInsensitiveResultSet method getLastRow.

/**
 * Returns the last row from the query, and returns NULL when there
 * are no rows.
 *
 * @return	The last row, or NULL if no rows.
 *
 * @exception StandardException		Thrown on failure
 * @see org.apache.derby.iapi.sql.Row
 */
public ExecRow getLastRow() throws StandardException {
    if (!isOpen) {
        throw StandardException.newException(SQLState.LANG_RESULT_SET_NOT_OPEN, "next");
    }
    if (!seenLast) {
        attachStatementContext();
        if (SanityManager.DEBUG) {
            if (!isTopResultSet) {
                SanityManager.THROWASSERT(this + "expected to be the top ResultSet");
            }
        }
        /* Scroll to the end, filling the hash table as
			 * we scroll, and return the last row that we find.
			 */
        ExecRow result = null;
        while ((result = getNextRowFromSource()) != null) ;
    }
    if (SanityManager.DEBUG && !seenLast) {
        SanityManager.THROWASSERT(this + "expected to have seen last");
    }
    beforeFirst = false;
    afterLast = false;
    // Special case if table is empty
    if (lastPosition == 0) {
        currentRow = null;
        return null;
    } else {
        return getRowFromHashTable(lastPosition);
    }
}
Also used : ExecRow(org.apache.derby.iapi.sql.execute.ExecRow)

Example 67 with ExecRow

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

the class ScrollInsensitiveResultSet method checkRowPosition.

/**
 * Determine if the cursor is before the first row in the result
 * set.
 *
 * @return true if before the first row, false otherwise. Returns
 * false when the result set contains no rows.
 * @exception StandardException Thrown on error.
 */
public boolean checkRowPosition(int isType) throws StandardException {
    switch(isType) {
        case ISBEFOREFIRST:
            if (!beforeFirst) {
                return false;
            }
            // Spec says to return false if result set is empty
            if (seenFirst) {
                return true;
            } else {
                ExecRow firstRow = getFirstRow();
                if (firstRow == null) {
                    // ResultSet is empty
                    return false;
                } else {
                    // ResultSet is not empty - reset position
                    getPreviousRow();
                    return true;
                }
            }
        case ISFIRST:
            return (currentPosition == 1);
        case ISLAST:
            if (beforeFirst || afterLast || currentPosition == 0 || currentPosition < positionInSource) {
                return false;
            }
            /* If we have seen the last row, we can tell if we are 
			 * on it by comparing currentPosition with lastPosition.
			 * Otherwise, we check if there is a next row.
			 */
            if (seenLast) {
                return (currentPosition == lastPosition);
            } else {
                final int savePosition = currentPosition;
                final boolean retval = (getNextRowFromSource() == null);
                getRowFromHashTable(savePosition);
                return retval;
            }
        case ISAFTERLAST:
            return afterLast;
        default:
            return false;
    }
}
Also used : ExecRow(org.apache.derby.iapi.sql.execute.ExecRow)

Example 68 with ExecRow

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

the class ScrollInsensitiveResultSet method getNextRowFromSource.

/* Get the next row from the source ResultSet tree and insert into the hash table */
private ExecRow getNextRowFromSource() throws StandardException {
    ExecRow sourceRow = null;
    ExecRow result = null;
    /* Don't give back more rows than requested */
    if (maxRows > 0 && maxRows == positionInSource) {
        seenLast = true;
        lastPosition = positionInSource;
        afterLast = true;
        return null;
    }
    if (needsRepositioning) {
        positionInLastFetchedRow();
        needsRepositioning = false;
    }
    sourceRow = source.getNextRowCore();
    if (sourceRow != null) {
        seenFirst = true;
        beforeFirst = false;
        long beginTCTime = getCurrentTimeMillis();
        /* If this is the first row from the source then we create a new row
			 * for use when fetching from the hash table.
			 */
        if (resultRow == null) {
            resultRow = activation.getExecutionFactory().getValueRow(sourceRowWidth);
        }
        positionInSource++;
        currentPosition = positionInSource;
        RowLocation rowLoc = null;
        if (source.isForUpdate()) {
            rowLoc = ((CursorResultSet) source).getRowLocation();
        }
        addRowToHashTable(sourceRow, currentPosition, rowLoc, false);
    } else // Remember whether or not we're past the end of the table
    {
        if (!seenLast) {
            lastPosition = positionInSource;
        }
        seenLast = true;
        // Special case for empty table (afterLast is never true)
        if (positionInSource == 0) {
            afterLast = false;
        } else {
            afterLast = true;
            currentPosition = positionInSource + 1;
        }
    }
    return sourceRow;
}
Also used : ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) RowLocation(org.apache.derby.iapi.types.RowLocation)

Example 69 with ExecRow

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

the class ScrollInsensitiveResultSet method getNextRowCore.

/**
 * @exception StandardException thrown on failure
 */
public ExecRow getNextRowCore() throws StandardException {
    if (isXplainOnlyMode())
        return null;
    ExecRow result = null;
    beginTime = getCurrentTimeMillis();
    if (!isOpen)
        throw StandardException.newException(SQLState.LANG_RESULT_SET_NOT_OPEN, "next");
    if (seenLast && currentPosition == lastPosition) {
        return setAfterLastRow();
    }
    /* Should we get the next row from the source or the hash table? */
    if (currentPosition == positionInSource) {
        /* Current position is same as position in source.
			 * Get row from the source.
			 */
        result = getNextRowFromSource();
        if (result != null) {
            result = getRowFromHashTable(currentPosition);
        }
    } else if (currentPosition < positionInSource) {
        /* Current position is before position in source.
			 * Get row from the hash table.
			 */
        result = getRowFromHashTable(currentPosition + 1);
    } else {
        result = null;
    }
    if (result != null) {
        rowsSeen++;
        afterLast = false;
    }
    setCurrentRow(result);
    beforeFirst = false;
    nextTime += getElapsedMillis(beginTime);
    return result;
}
Also used : ExecRow(org.apache.derby.iapi.sql.execute.ExecRow)

Example 70 with ExecRow

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

the class TableScanResultSet method getNextRowCore.

/**
 * 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;
    checkCancellationFlag();
    if (currentRow == null || scanRepositioned) {
        currentRow = getCompactRow(candidate, accessedCols, isKeyed);
    }
    beginTime = getCurrentTimeMillis();
    ExecRow result = null;
    if (isOpen && !nextDone) {
        /* Only need to do 1 next per scan
			 * for 1 row scans.
			 */
        nextDone = oneRowScan;
        if (scanControllerOpened) {
            boolean moreRows = true;
            while (true) {
                // ValidateCheckConstraintResultSet..
                if (!(moreRows = loopControl(moreRows))) {
                    break;
                }
                rowsSeen++;
                rowsThisScan++;
                /*
					** Skip rows where there are start or stop positioners
					** that do not implement ordered null semantics and
					** there are columns in those positions that contain
					** null.
					** No need to check if start and stop positions are the
					** same, since all predicates in both will be ='s,
					** and hence evaluated in the store.
					*/
                if ((!sameStartStopPosition) && skipRow(candidate)) {
                    rowsFiltered++;
                    continue;
                }
                /* beetle 3865, updateable cursor use index. If we have a hash table that
					 * holds updated records, and we hit it again, skip it, and remove it from
					 * hash since we can't hit it again, and we have a space in hash, so can
					 * stop scanning forward.
					 */
                if (past2FutureTbl != null) {
                    RowLocation rowLoc = (RowLocation) currentRow.getColumn(currentRow.nColumns());
                    if (past2FutureTbl.remove(rowLoc) != null) {
                        continue;
                    }
                }
                result = currentRow;
                break;
            }
            /*
				** If we just finished a full scan of the heap, update
				** the number of rows in the scan controller.
				**
				** NOTE: It would be more efficient to only update the
				** scan controller if the optimizer's estimated number of
				** rows were wrong by more than some threshold (like 10%).
				** This would require a little more work than I have the
				** time for now, however, as the row estimate that is given
				** to this result set is the total number of rows for all
				** scans, not the number of rows per scan.
				*/
            if (!moreRows) {
                setRowCountIfPossible(rowsThisScan);
                currentRow = null;
            }
        }
    }
    setCurrentRow(result);
    currentRowIsValid = true;
    scanRepositioned = false;
    qualify = true;
    nextTime += getElapsedMillis(beginTime);
    return result;
}
Also used : ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) RowLocation(org.apache.derby.iapi.types.RowLocation)

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