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);
}
}
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;
}
}
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;
}
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;
}
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;
}
Aggregations