📄 scrollinsensitiveresultset.java
字号:
*/ public ExecRow getPreviousRow() throws StandardException { if ( ! isOpen ) { throw StandardException.newException(SQLState.LANG_RESULT_SET_NOT_OPEN, "next"); } if (SanityManager.DEBUG) { if (!isTopResultSet) { SanityManager.THROWASSERT( this + "expected to be the top ResultSet"); } } /* No row if we are positioned before the first row * or the result set is empty. */ if (beforeFirst || currentPosition == 0) { currentRow = null; return null; } // Get the last row, if we are after it if (afterLast) { // Special case for empty tables if (lastPosition == 0) { afterLast = false; beforeFirst = false; currentRow = null; return null; } else { return getRowFromHashTable(lastPosition); } } // Move back 1 currentPosition--; if (currentPosition == 0) { setBeforeFirstRow(); return null; } return getRowFromHashTable(currentPosition); } /** * 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 Row */ public ExecRow getLastRow() throws StandardException { ExecRow result; if ( ! isOpen ) { throw StandardException.newException(SQLState.LANG_RESULT_SET_NOT_OPEN, "next"); } /* Have we already seen the last row? */ if (seenLast) { // Return null if the set was empty if (lastPosition == 0) { currentRow = null; return null; } else { return getRowFromHashTable(lastPosition); } } 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. */ while ((result = getNextRowFromSource()) != null); beforeFirst = false; afterLast = false; // Special case if table is empty if (lastPosition == 0) { currentRow = null; return null; } else { return getRowFromHashTable(lastPosition); } } /** * Sets the current position to after the last row and returns NULL * because there is no current row. * * @return NULL. * * @exception StandardException Thrown on failure * @see Row */ public ExecRow setAfterLastRow() throws StandardException { if (! seenLast) { getLastRow(); } currentPosition = lastPosition + 1; afterLast = true; beforeFirst = false; currentRow = null; return null; } /** * 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) { return false; } /* If we've already seen the last row * then we can tell if we are on it by * the current position, * otherwise, we need to find the last * row in order to tell if the current row * is the last row. */ if (seenLast) { return (currentPosition == lastPosition && currentPosition != 0); } else { int savePosition = currentPosition; boolean retval = false; getLastRow(); if (savePosition == lastPosition && savePosition != 0) { retval = true; } getRowFromHashTable(savePosition); return retval; } case ISAFTERLAST: return afterLast; default: return false; } } /** * Returns the row number of the current row. Row * numbers start from 1 and go to 'n'. Corresponds * to row numbering used to position current row * in the result set (as per JDBC). * * @return the row number, or 0 if not on a row * */ public int getRowNumber() { return currentRow == null ? 0 : currentPosition; } /* 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; } 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; addRowToHashTable(sourceRow); } // Remember whether or not we're past the end of the table else { 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; } /** * If the result set has been opened, * close the open scan. * * @exception StandardException thrown on error */ public void close() throws StandardException { beginTime = getCurrentTimeMillis(); if ( isOpen ) { if (closeCleanup != null) { closeCleanup.invoke(activation); // let activation tidy up } currentRow = null; source.close(); if (ht != null) { ht.close(); ht = null; } super.close(); } else if (SanityManager.DEBUG) SanityManager.DEBUG("CloseRepeatInfo","Close of ScrollInsensitiveResultSet repeated"); setBeforeFirstRow(); closeTime += getElapsedMillis(beginTime); } public void finish() throws StandardException { source.finish(); finishAndRTS(); } /** * Return the total amount of time spent in this ResultSet * * @param type CURRENT_RESULTSET_ONLY - time spent only in this ResultSet * ENTIRE_RESULTSET_TREE - time spent in this ResultSet and below. * * @return long The total amount of time spent (in milliseconds). */ public long getTimeSpent(int type) { long totTime = constructorTime + openTime + nextTime + closeTime; if (type == NoPutResultSet.CURRENT_RESULTSET_ONLY) { return totTime - source.getTimeSpent(ENTIRE_RESULTSET_TREE); } else { return totTime; } } // // CursorResultSet interface // /** * Gets information from its source. We might want * to have this take a CursorResultSet in its constructor some day, * instead of doing a cast here? * * @see CursorResultSet * * @return the row location of the current cursor row. * * @exception StandardException thrown on failure */ public RowLocation getRowLocation() throws StandardException { if (SanityManager.DEBUG) SanityManager.ASSERT(source instanceof CursorResultSet, "source not CursorResultSet"); return ( (CursorResultSet)source ).getRowLocation(); } /** * Gets information from last getNextRow call. * * @see CursorResultSet * * @return the last row returned. */ /* RESOLVE - this should return activation.getCurrentRow(resultSetNumber), * once there is such a method. (currentRow is redundant) */ public ExecRow getCurrentRow() { return currentRow; } // // class implementation // /** * Add a row to the backing hash table, * keyed on positionInSource. * * @param sourceRow The row to add. * * @return Nothing. */ private void addRowToHashTable(ExecRow sourceRow) throws StandardException { DataValueDescriptor[] hashRowArray = new DataValueDescriptor[sourceRowWidth + 1]; // 1st element is the key hashRowArray[0] = new SQLInteger(positionInSource); /* Copy rest of elements from sourceRow. * NOTE: We need to clone the source row * and we do our own cloning since the 1st column * is not a wrapper. */ DataValueDescriptor[] sourceRowArray = sourceRow.getRowArrayClone(); System.arraycopy(sourceRowArray, 0, hashRowArray, 1, sourceRowArray.length); ht.put(false, hashRowArray); numToHashTable++; } /** * Get the row at the specified position * from the hash table. * * @param position The specified position. * * @return The row at that position. * * @exception StandardException thrown on failure */ private ExecRow getRowFromHashTable(int position) throws StandardException { // Get the row from the hash table DataValueDescriptor[] hashRowArray = (DataValueDescriptor[]) ht.get(new SQLInteger(position)); if (SanityManager.DEBUG) { SanityManager.ASSERT(hashRowArray != null, "hashRowArray expected to be non-null"); } // Copy out the Object[] without the position. DataValueDescriptor[] resultRowArray = new DataValueDescriptor[hashRowArray.length - 1]; System.arraycopy(hashRowArray, 1, resultRowArray, 0, resultRowArray.length); resultRow.setRowArray(resultRowArray); // Reset the current position to the user position currentPosition = position; numFromHashTable++; if (resultRow != null) { beforeFirst = false; afterLast = false; } currentRow = resultRow; return resultRow; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -