resultset.java

来自「derby database source code.good for you.」· Java 代码 · 共 1,556 行 · 第 1/5 页

JAVA
1,556
字号
/*   Derby - Class org.apache.derby.client.am.ResultSet   Copyright (c) 2001, 2005 The Apache Software Foundation or its licensors, where applicable.   Licensed under the Apache License, Version 2.0 (the "License");   you may not use this file except in compliance with the License.   You may obtain a copy of the License at      http://www.apache.org/licenses/LICENSE-2.0   Unless required by applicable law or agreed to in writing, software   distributed under the License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   See the License for the specific language governing permissions and   limitations under the License.*/package org.apache.derby.client.am;public abstract class ResultSet implements java.sql.ResultSet,        ResultSetCallbackInterface,        UnitOfWorkListener {    //---------------------navigational members-----------------------------------    public Statement statement_;    public ColumnMetaData resultSetMetaData_; // As obtained from the SQLDA    private SqlWarning warnings_;    public Cursor cursor_;    protected Agent agent_;    public Section generatedSection_ = null;    //---------------------navigational cheat-links-------------------------------    // Cheat-links are for convenience only, and are not part of the conceptual model.    // Warning:    //   Cheat-links should only be defined for invariant state data.    //   That is, the state data is set by the constructor and never changes.    // Alias for statement_.connection    public final Connection connection_;    //----------------------------- constants ------------------------------------    public final static int scrollOrientation_relative__ = 1;    public final static int scrollOrientation_absolute__ = 2;    public final static int scrollOrientation_after__ = 3;    public final static int scrollOrientation_before__ = 4;    public final static int scrollOrientation_prior__ = 5;    public final static int scrollOrientation_first__ = 6;    public final static int scrollOrientation_last__ = 7;    public final static int scrollOrientation_current__ = 8;    public final static int scrollOrientation_next__ = 0;    public final static int updatability_unknown__ = 0;    public final static int updatability_readOnly__ = 1;    public final static int updatability_delete__ = 2;    public final static int updatability_update__ = 4;    public final static int sensitivity_unknown__ = 0;    public final static int sensitivity_insensitive__ = 1;    public final static int sensitivity_sensitive_static__ = 2;    public final static int sensitivity_sensitive_dynamic__ = 3;    static final private int WAS_NULL = 1;    static final private int WAS_NOT_NULL = 2;    static final private int WAS_NULL_UNSET = 0;    static final public int NEXT_ROWSET = 1;    static final public int PREVIOUS_ROWSET = 2;    static final public int ABSOLUTE_ROWSET = 3;    static final public int FIRST_ROWSET = 4;    static final public int LAST_ROWSET = 5;    static final public int RELATIVE_ROWSET = 6;    static final public int REFRESH_ROWSET = 7;    //  determines if a cursor is a:    //    Return to Client - not to be read by the stored procedure only by client    //    Return to Caller    public static final byte DDM_RETURN_CALLER = 0x01;    public static final byte DDM_RETURN_CLIENT = 0x02;    //-----------------------------state------------------------------------------    // Note:    //   Result set meta data as described by the SQLDA is described in ColumnMetaData.    private int wasNull_ = WAS_NULL_UNSET;    // ResultSet returnability for Stored Procedure cursors    //  determines if a cursor is a:    //    Return to Client - not to be read by the stored procedure only by client    //    Return to Caller - only calling JSP can read it, not the client    protected byte rsReturnability_ = DDM_RETURN_CLIENT;    // This means the client-side jdbc result set object is open.    boolean openOnClient_ = true;    // This means a server-side DERBY query section (cursor) for this result set is in the open state.    // A jdbc result set may remain open even after the server has closed its cursor    // (openOnClient=true, openOnServer=false); this is known as the "close-only" state.    public boolean openOnServer_ = true;    // there is a query terminating sqlca returned from the server when the server closes    // it's cursor and the client moves to the close-only state.    public Sqlca queryTerminatingSqlca_;    // Only true for forward cursors after next() returns false (+100).    // Used to prevent multiple commits for subsequent next() calls.    boolean autoCommitted_ = false;    // Before the first call to next() or any cursor positioning method, the cursor position is invalid    // and getter methods cannot be called.    // Also, if a cursor is exhausted (+100), the cursor position is invalid.    public boolean isValidCursorPosition_ = false;    public boolean cursorHold_;    // query instance identifier returned on open by uplevel servers.    // this value plus the package information uniquely identifies a query.    // it is 64 bits long and it's value is unarchitected.    public long queryInstanceIdentifier_ = 0;    public int resultSetType_;    public int resultSetConcurrency_;    public int resultSetHoldability_;    public boolean scrollable_ = false;    public int sensitivity_;    public boolean isRowsetCursor_ = false;    public boolean isBeforeFirst_ = true;    public boolean isAfterLast_ = false;    public boolean isFirst_ = false;    public boolean isLast_ = false;    public boolean rowsetContainsLastRow_ = false;    public Sqlca[] rowsetSqlca_;    public int fetchSize_;    public int fetchDirection_;    public long rowCount_ = -1;    protected long absolutePosition_ = 0;       // absolute position of the current row    protected long firstRowInRowset_ = 0;       // absolute position of the first row in the current rowset    protected long lastRowInRowset_ = 0;        // absolute position of the last row in the current rowset    protected long currentRowInRowset_ = -1;     // relative position to the first row in the current rowsetwel    protected long absoluteRowNumberForTheIntendedRow_;    // This variable helps keep track of whether cancelRowUpdates() should have any effect.    protected boolean updateRowCalled_ = false;    private boolean isOnInsertRow_ = false;  // reserved for later    protected boolean isOnCurrentRow_ = true;    public int rowsReceivedInCurrentRowset_ = 0;  // keep track of the number of rows received in the    // current rowset so far    // maybe be able to consolidate with rowsReceivedInCurrentRowset_    // Could use the rowsReceivedInCurrentRowset_ flag. But since we are going to set it to the    // fetchSize and decrement it each time we successfully receiveds a row, the name will be confusing.    // Fetch size can be changed in the middle of a rowset, and since we don't pre-parse all the rows \    // for forward-only cursors like we do for scrollable cursors, we will lose the original fetchSize    // when it's reset.  By decrementing rowsYetToBeReceivedInRowset_, when we come across a fetch    // request, if rowsYetToBeReceivedInRowset_ is 0, then we can fetch using the "new" fetchSize,    // otherwise, we will use rowsYetToBeReceivedInRowset_ to complete the rowset.    public int rowsYetToBeReceivedForRowset_ = 0; // keep track of the number of rows still need to    // be received to complete the rowset    private Object updatedColumns_[];    // Keeps track of whether a column has been updated.  If a column is updated to null,    // the object array updatedColumns_ entry is null, and we will use this array to distinguish    // between column not updated and column updated to null.    private boolean columnUpdated_[];    public PreparedStatement preparedStatementForUpdate_;    public PreparedStatement preparedStatementForDelete_;    // Nesting level of the result set in a stored procedure    public int nestingLevel_ = -1;    // Whenever a commit occurs, it unpositions the cursor on the server.  We need to    // reposition the cursor before updating/deleting again.  This flag will be set to true    // whenever a commit happens, and reset to false again after we repositoin the cursor.    public boolean cursorUnpositionedOnServer_ = false;    //---------------------constructors/finalizer---------------------------------    protected ResultSet(Agent agent,                        Statement statement,                        Cursor cursor,                        int resultSetType,                        int resultSetConcurrency,                        int resultSetHoldability) {        agent_ = agent;        statement_ = statement;        connection_ = statement_.connection_;        cursor_ = cursor;        if (cursor_ != null) {            cursor_.maxFieldSize_ = statement_.maxFieldSize_;        }        resultSetType_ = resultSetType;        resultSetConcurrency_ = resultSetConcurrency;        resultSetHoldability_ = resultSetHoldability;        fetchDirection_ = statement_.fetchDirection_;        fetchSize_ = statement_.fetchSize_;        // Only set the warning if actual resultSetType returned by the server is less        // than the application requested resultSetType.        // TYPE_FORWARD_ONLY = 1003        // TYPE_SCROLL_INSENSITIVE = 1004        // TYPE_SCROLL_SENSITIVE = 1005        if (resultSetType_ < statement_.resultSetType_) {            statement_.accumulateWarning                    (new SqlWarning(agent_.logWriter_, "Unable to open resultSet type " +                    statement_.resultSetType_ + "." +                    " ResultSet type " + resultSetType_ + " opened."));        }        // Only set the warning if actual resultSetConcurrency returned by the server is        // less than the application requested resultSetConcurrency.        // CONCUR_READ_ONLY = 1007        // CONCUR_UPDATABLE = 1008        if (resultSetConcurrency_ < statement_.resultSetConcurrency_) {            statement_.accumulateWarning                    (new SqlWarning(agent_.logWriter_, "Unable to open ResultSet with concurrency  " +                    statement_.resultSetConcurrency_ + "." +                    " ResultSet concurrency " + resultSetConcurrency_ + " is used."));        }        listenToUnitOfWork();    }    // ---------------------------jdbc 1------------------------------------------    public final boolean next() throws SqlException {        synchronized (connection_) {            if (agent_.loggingEnabled()) {                agent_.logWriter_.traceEntry(this, "next");            }            boolean isValidCursorPosition = nextX();            if (agent_.loggingEnabled()) {                agent_.logWriter_.traceExit(this, "next", isValidCursorPosition);            }            return isValidCursorPosition;        }    }    // used by DBMD    boolean nextX() throws SqlException {        checkForClosedResultSet();        clearWarningsX();        wasNull_ = ResultSet.WAS_NULL_UNSET;        // discard all previous updates when moving the cursor        resetUpdatedColumns();        // for TYPE_FORWARD_ONLY ResultSet, just call cursor.next()        if (resultSetType_ == java.sql.ResultSet.TYPE_FORWARD_ONLY) {            // cursor is null for singleton selects that do not return data.            isValidCursorPosition_ = (cursor_ == null) ? false : cursor_.next();            // for forward-only cursors, if qryrowset was specificed on OPNQRY or EXCSQLSTT,            // then we must count the rows returned in the rowset to make sure we received a            // complete rowset.  if not, we need to complete the rowset on the next fetch.            if (fetchSize_ != 0) {                if (rowsYetToBeReceivedForRowset_ == 0) {                    rowsYetToBeReceivedForRowset_ = fetchSize_;                }                if (isValidCursorPosition_) {                    rowsYetToBeReceivedForRowset_--;                }            }            // Auto-commit semantics for exhausted cursors follows.            // From Connection.setAutoCommit() javadoc:            //   The commit occurs when the statement completes or the next execute occurs, whichever comes first.            //   In the case of statements returning a ResultSet object, the statement completes when the            //   last row of the ResultSet object has been retrieved or the ResultSet object has been closed.            //   In advanced cases, a single statement may return multiple results as well as output parameter values.            //   In these cases, the commit occurs when all results and output parameter values have been retrieved.            // we will check to see if the forward only result set has gone past the end,            // we will close the result set, the autocommit logic is in the closeX() method//    if (!isValidCursorPosition_ && // We've gone past the end (+100)//        cursor_ != null) {            if ((!isValidCursorPosition_ && cursor_ != null) ||                    (statement_.maxRows_ > 0 && cursor_.rowsRead_ > statement_.maxRows_)) {                isValidCursorPosition_ = false;                // if not on a valid row and the query is closed at the server.                // check for an error which may have caused the cursor to terminate.                // if there were no more rows because of an error, then this method                // should throw an SqlException rather than just returning false.                // note: closeX is still called and this will cause the                // result set to be closed on the client. any additional calls to                // next() will fail checkForClosedResultSet(), the query terminating exception is                // only thrown once.                // depending on how this works with scrollable cursors, there may be                // a better way/more common place for this logic.                SqlException sqlException = null;                if (!openOnServer_) {                    int sqlcode = Utils.getSqlcodeFromSqlca(queryTerminatingSqlca_);                    if (sqlcode > 0 && sqlcode != 100) {                        accumulateWarning(new SqlWarning(agent_.logWriter_, queryTerminatingSqlca_));                    } else if (sqlcode < 0) {                        sqlException = new SqlException(agent_.logWriter_, queryTerminatingSqlca_);                    }                }                try {                    closeX(); // the auto commit logic is in closeX()                } catch (SqlException sqle) {                    sqlException = Utils.accumulateSQLException(sqle, sqlException);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?