preparedstatement.java

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

JAVA
1,533
字号
/*   Derby - Class org.apache.derby.client.am.PreparedStatement   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 class PreparedStatement extends Statement        implements java.sql.PreparedStatement,        PreparedStatementCallbackInterface {    //---------------------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 downcast (MaterialPreparedStatementProxy) super.materialStatement.    public MaterialPreparedStatement materialPreparedStatement_ = null;    //-----------------------------state------------------------------------------    public String sql_;    // This variable is only used by Batch.    // True if a call sql statement has an OUT or INOUT parameter registered.    public boolean outputRegistered_ = false;    // Parameter inputs are cached as objects so they may be sent on execute()    public Object[] parameters_;    boolean[] parameterSet_;    boolean[] parameterRegistered_;    void setInput(int parameterIndex, Object input) {        parameters_[parameterIndex - 1] = input;        parameterSet_[parameterIndex - 1] = true;    }    public ColumnMetaData parameterMetaData_; // type information for input sqlda    // The problem with storing the scrollable ResultSet associated with cursorName in scrollableRS_ is    // that when the PreparedStatement is re-executed, it has a new ResultSet, however, we always do    // the reposition on the ResultSet that was stored in scrollableRS_, and we never update scrollableRS_    // when PreparedStatement is re-execute.  So the new ResultSet that needs to be repositioned never    // gets repositioned.    // So instead of caching the scrollableRS_, we will cache the cursorName.  And re-retrieve the scrollable    // result set from the map using this cursorName every time the PreparedStatement excutes.    String positionedUpdateCursorName_ = null;    private void initPreparedStatement() {        materialPreparedStatement_ = null;        sql_ = null;        outputRegistered_ = false;        parameters_ = null;        parameterSet_ = null;        parameterRegistered_ = null;        parameterMetaData_ = null;        isAutoCommittableStatement_ = true;        isPreparedStatement_ = true;    }    protected void initResetPreparedStatement() {        outputRegistered_ = false;        isPreparedStatement_ = true;        if (parameterMetaData_ != null) {            resetParameters();        }    }    public void reset(boolean fullReset) throws SqlException {        if (fullReset) {            connection_.resetPrepareStatement(this);        } else {            super.initResetPreparedStatement();            initResetPreparedStatement();        }    }    private void resetParameters() {        for (int i = 0; i < parameterMetaData_.columns_; i++) {            parameters_[i] = null;            parameterSet_[i] = false;            parameterRegistered_[i] = false;        }    }    // For for JDBC 2 positioned update statements.    // Called by material statement constructors.    public PreparedStatement(Agent agent,                             Connection connection,                             String sql,                             Section section) throws SqlException {        super(agent, connection);        initPreparedStatement(sql, section);    }    public void resetPreparedStatement(Agent agent,                                       Connection connection,                                       String sql,                                       Section section) throws SqlException {        super.resetStatement(agent, connection);        initPreparedStatement();        initPreparedStatement(sql, section);    }    private void initPreparedStatement(String sql, Section section) throws SqlException {        sql_ = sql;        isPreparedStatement_ = true;        parseSqlAndSetSqlModes(sql_);        section_ = section;    }    // Constructor for jdbc 2 prepared statements with scroll attributes.    // Called by material statement constructors.    public PreparedStatement(Agent agent,                             Connection connection,                             String sql,                             int type, int concurrency, int holdability, int autoGeneratedKeys, String[] columnNames) throws SqlException {        super(agent, connection, type, concurrency, holdability, autoGeneratedKeys, columnNames);        initPreparedStatement(sql);    }    public void resetPreparedStatement(Agent agent,                                       Connection connection,                                       String sql,                                       int type, int concurrency, int holdability, int autoGeneratedKeys, String[] columnNames) throws SqlException {        super.resetStatement(agent, connection, type, concurrency, holdability, autoGeneratedKeys, columnNames);        initPreparedStatement();        initPreparedStatement(sql);    }    private void initPreparedStatement(String sql) throws SqlException {        sql_ = super.escape(sql);        parseSqlAndSetSqlModes(sql_);        isPreparedStatement_ = true;        // Check for positioned update statement and assign a section from the        // same package as the corresponding query section.        // Scan the sql for an "update...where current of <cursor-name>".        String cursorName = null;        if (sqlUpdateMode_ == isDeleteSql__ || sqlUpdateMode_ == isUpdateSql__) {            String[] sqlAndCursorName = extractCursorNameFromWhereCurrentOf(sql_);            if (sqlAndCursorName != null) {                cursorName = sqlAndCursorName[0];                sql_ = sqlAndCursorName[1];            }        }        if (cursorName != null) {            positionedUpdateCursorName_ = cursorName;            // Get a new section from the same package as the query section            section_ = agent_.sectionManager_.getPositionedUpdateSection(cursorName, false); // false means get a regular section            if (section_ == null) {                throw new SqlException(agent_.logWriter_, "Invalid cursor name \"" + cursorName + "\" in the Update/Delete statement.");            }            //scrollableRS_ = agent_.sectionManager_.getPositionedUpdateResultSet (cursorName);            // if client's cursor name is set, and the cursor name in the positioned update            // string is the same as the client's cursor name, replace client's cursor name            // with the server's cursor name.            // if the cursor name supplied in the sql string is different from the cursorName            // set by setCursorName(), then server will return "cursor name not defined" error,            // and no subsititution is made here.            if (section_.getClientCursorName() != null && // cursor name is user defined                    cursorName.compareTo(section_.getClientCursorName()) == 0)            // client's cursor name is substituted with section's server cursor name            {                sql_ = substituteClientCursorNameWithServerCursorName(sql_, section_);            }        } else {            // We don't need to analyze the sql text to determine if it is a query or not.            // This is up to the server to decide, we just pass thru the sql on flowPrepare().            section_ = agent_.sectionManager_.getDynamicSection(resultSetHoldability_);        }    }    public void resetPreparedStatement(Agent agent,                                       Connection connection,                                       String sql,                                       Section section,                                       ColumnMetaData parameterMetaData,                                       ColumnMetaData resultSetMetaData) throws SqlException {        resetPreparedStatement(agent, connection, sql, section);        initPreparedStatement(parameterMetaData, resultSetMetaData);    }    private void initPreparedStatement(ColumnMetaData parameterMetaData,                                       ColumnMetaData resultSetMetaData) throws SqlException {        isPreparedStatement_ = true;        parameterMetaData_ = parameterMetaData;        resultSetMetaData_ = resultSetMetaData;        if (parameterMetaData_ != null) {            parameters_ = new Object[parameterMetaData_.columns_];            //parameterSetOrRegistered_ = new boolean[parameterMetaData_.columns_];            parameterSet_ = new boolean[parameterMetaData_.columns_];            parameterRegistered_ = new boolean[parameterMetaData_.columns_];        }    }    // called immediately after the constructor by Connection prepare*() methods    void prepare() throws SqlException {        try {            // flow prepare, no static initialization is needed            // already checked if columnNames is not null and server supports select from insert            // in prepareStatementX()            if (sqlUpdateMode_ == isInsertSql__ && generatedKeysColumnNames_ != null) {                flowPrepareForSelectFromInsert();            } else {                flowPrepareDescribeInputOutput();            }        } catch (SqlException e) {            this.markClosed();            throw e;        }    }    //------------------- Prohibited overrides from Statement --------------------    public boolean execute(String sql) throws SqlException {        if (agent_.loggingEnabled()) {            agent_.logWriter_.traceEntry(this, "execute", sql);        }        throw new SqlException(agent_.logWriter_,                "The method java.sql.Statement.execute (String sql) cannot be called on a " +                " prepared statement instance." +                " Use java.sql.PreparedStatement.execute () with no sql string argument.");    }    public java.sql.ResultSet executeQuery(String sql) throws SqlException {        if (agent_.loggingEnabled()) {            agent_.logWriter_.traceEntry(this, "executeQuery", sql);        }        throw new SqlException(agent_.logWriter_,                "The method java.sql.Statement.executeQuery (String sql) cannot be called on a " +                " prepared statement instance." +                " Use java.sql.PreparedStatement.executeQuery () with no sql string argument.");    }    public int executeUpdate(String sql) throws SqlException {        if (agent_.loggingEnabled()) {            agent_.logWriter_.traceEntry(this, "executeUpdate", sql);        }        throw new SqlException(agent_.logWriter_,                "The method java.sql.Statement.executeUpdate (String sql) cannot be called on a " +                " prepared statement instance." +                " Use java.sql.PreparedStatement.executeUpdate () with no sql string argument.");    }    // ---------------------------jdbc 1------------------------------------------    public java.sql.ResultSet executeQuery() throws SqlException {        synchronized (connection_) {            if (agent_.loggingEnabled()) {                agent_.logWriter_.traceEntry(this, "executeQuery");            }            ResultSet resultSet = executeQueryX();            if (agent_.loggingEnabled()) {                agent_.logWriter_.traceExit(this, "executeQuery", resultSet);            }            return resultSet;        }    }    // also called by some DBMD methods    ResultSet executeQueryX() throws SqlException {        flowExecute(executeQueryMethod__);        super.checkExecuteQueryPostConditions("java.sql.PreparedStatement");        return resultSet_;    }    public int executeUpdate() throws SqlException {        synchronized (connection_) {            if (agent_.loggingEnabled()) {                agent_.logWriter_.traceEntry(this, "executeUpdate");            }            int updateValue = executeUpdateX();            if (agent_.loggingEnabled()) {                agent_.logWriter_.traceExit(this, "executeUpdate", updateValue);            }            return updateValue;        }    }    // also used by Blob

⌨️ 快捷键说明

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