📄 drdastatement.java
字号:
/* Derby - Class org.apache.derby.impl.drda.DRDAStatement Copyright 2002, 2004 The Apache Software Foundation or its licensors, as 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.impl.drda;import java.io.UnsupportedEncodingException;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.math.BigInteger;import java.sql.CallableStatement;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.Hashtable;import java.util.StringTokenizer;import java.util.Vector;import org.apache.derby.iapi.jdbc.BrokeredConnection;import org.apache.derby.iapi.jdbc.BrokeredPreparedStatement;import org.apache.derby.iapi.jdbc.EngineConnection;import org.apache.derby.iapi.jdbc.EngineStatement;import org.apache.derby.iapi.reference.JDBC30Translation;import org.apache.derby.iapi.sql.execute.ExecutionContext;import org.apache.derby.iapi.util.StringUtil;import org.apache.derby.impl.jdbc.EmbedParameterSetMetaData;import org.apache.derby.impl.jdbc.Util;/** DRDAStatement stores information about the statement being executed*/class DRDAStatement{ //NOTE! // // Since DRDAStatements are reused, ALL variables (except those noted in // the comments for reset method) should be set to their default values // in reset(). protected String typDefNam; //TYPDEFNAM for this statement protected int byteOrder; //deduced from typDefNam, save String comparisons protected int ccsidSBC; //CCSID for single byte characters protected int ccsidDBC; //CCSID for double byte characters protected int ccsidMBC; //CCSID for mixed byte characters protected String ccsidSBCEncoding; //Java encoding for CCSIDSBC protected String ccsidDBCEncoding; //Java encoding for CCSIDDBC protected String ccsidMBCEncoding; //Java encoding for CCSIDMBC protected Database database; // Database this statement is created for private Pkgnamcsn pkgnamcsn; // Package name/section # and consistency token protected ConsistencyToken pkgcnstkn; // Consistency token for the first result set protected String pkgid; // package id protected int pkgsn; // section number int withHoldCursor = -1; // hold cursor after commit attribute. protected int isolationLevel; //JCC isolation level for Statement protected String cursorName; protected int scrollType = ResultSet.TYPE_FORWARD_ONLY; // Sensitive or Insensitive scroll attribute protected int concurType = ResultSet.CONCUR_READ_ONLY;; // Concurency type protected long rowCount; // Number of rows we have processed protected byte [] rslsetflg; // Result Set Flags protected int maxrslcnt; // Maximum Result set count protected PreparedStatement ps; // Prepared statement protected EmbedParameterSetMetaData stmtPmeta; // param metadata protected boolean isCall; protected String procName; // callable statement's method name private int[] outputTypes; // jdbc type for output parameter or NOT_OUTPUT_PARAM // if not an output parameter. protected static int NOT_OUTPUT_PARAM = -100000; protected boolean outputExpected; // expect output from a callable statement private Statement stmt; // SQL statement private DRDAResultSet currentDrdaRs; // Current ResultSet private Hashtable resultSetTable; // Hashtable with resultsets private ArrayList resultSetKeyList; // ordered list of hash keys private int numResultSets = 0; // State for parameter data protected Vector cliParamDrdaTypes = new Vector(); protected Vector cliParamLens = new Vector(); protected ArrayList cliParamExtPositions = null; // Query options sent on EXCSQLSTT // These the default for ResultSets created for this statement. // These can be overriden by OPNQRY or CNTQRY, protected int nbrrow; // number of fetch or insert rows protected int qryrowset; // Query row set protected int blksize; // Query block size protected int maxblkext; // Maximum number of extra blocks protected int outovropt; // Output Override option protected boolean qryrfrtbl; // Query refresh answer set table private int qryprctyp = CodePoint.QRYBLKCTL_DEFAULT; // Protocol type boolean needsToSendParamData = false; boolean explicitlyPrepared = false; //Prepared with PRPSQLSTT (reusable) // constructor /** * DRDAStatement constructor * * @param database * */ protected DRDAStatement (Database database) { this.database = database; setTypDefValues(); this.currentDrdaRs = new DRDAResultSet(); } /** * set TypDef values * */ protected void setTypDefValues() { // initialize statement values to current database values this.typDefNam = database.typDefNam; this.byteOrder = database.byteOrder; this.ccsidSBC = database.ccsidSBC; this.ccsidDBC = database.ccsidDBC; this.ccsidMBC = database.ccsidMBC; this.ccsidSBCEncoding = database.ccsidSBCEncoding; this.ccsidDBCEncoding = database.ccsidDBCEncoding; this.ccsidMBCEncoding = database.ccsidMBCEncoding; } /** * Set database * * @param database */ protected void setDatabase(Database database) { this.database = database; setTypDefValues(); } /** * Set statement * * @param conn Connection * @exception SQLException */ protected void setStatement(Connection conn) throws SQLException { stmt = conn.createStatement(); //beetle 3849 - see prepareStatement for details if (cursorName != null) stmt.setCursorName(cursorName); } /** * Get the statement * * @return statement * @exception SQLException */ protected Statement getStatement() throws SQLException { return stmt; } /**Set resultSet defaults to match * the statement defaults sent on EXCSQLSTT * This might be overridden on OPNQRY or CNTQRY **/ protected void setRsDefaultOptions(DRDAResultSet drs) { drs.nbrrow = nbrrow; drs.qryrowset = qryrowset; drs.blksize = blksize; drs.maxblkext = maxblkext; drs.outovropt = outovropt; drs.rslsetflg = rslsetflg; drs.scrollType = scrollType; drs.concurType = concurType; drs.setQryprctyp(qryprctyp); drs.qryrowset = qryrowset; } /** * Set result set options to default for statement */ protected void setRsDefaultOptions() { setRsDefaultOptions(currentDrdaRs); } /** * Get the extData Objects * * @return ArrayList with extdta */ protected ArrayList getExtDtaObjects() { return currentDrdaRs.getExtDtaObjects(); } /** * Set the extData Objects * * @return ArrayList with extdta */ protected void setExtDtaObjects(ArrayList a) { currentDrdaRs.setExtDtaObjects(a); } public void setSplitQRYDTA(byte []data) { currentDrdaRs.setSplitQRYDTA(data); } public byte[]getSplitQRYDTA() { return currentDrdaRs.getSplitQRYDTA(); } /** * Add extDtaObject * @param o - object to add * @param jdbcIndex - jdbc index for parameter */ protected void addExtDtaObject (Object o, int jdbcIndex ) { currentDrdaRs.addExtDtaObject(o,jdbcIndex); } /** * Clear externalized lob objects in current result set */ protected void clearExtDtaObjects () { currentDrdaRs.clearExtDtaObjects(); } /** * * get resultSetHoldability. * * @return the resultSet holdability for the prepared statement * */ protected int getResultSetHoldability() throws SQLException { Statement rsstmt; ResultSet rs = getResultSet(); if (rs != null) rsstmt = rs.getStatement(); else rsstmt = getPreparedStatement(); int holdValue = ((EngineStatement) rsstmt).getResultSetHoldability(); return holdValue; } /** * * get resultSetHoldability. * * @param rs ResultSet * @return the resultSet holdability for the prepared statement * */ int getResultSetHoldability(ResultSet rs) throws SQLException { Statement rsstmt; if (rs != null) rsstmt = rs.getStatement(); else rsstmt = getPreparedStatement(); int holdValue = ((EngineStatement) rsstmt).getResultSetHoldability(); return holdValue; } /* * Is lob object nullable * @param index - offset starting with 0 * @return true if object is nullable */ protected boolean isExtDtaValueNullable(int index) { return currentDrdaRs.isExtDtaValueNullable(index); } /** * Delegation method to call DRDAResultSet to set query * options sent on OPNQRY. * @see DRDAResultSet#setOPNQRYOptions(int, int, int, int, int, int) */ protected void setOPNQRYOptions(int blksize, int qryblkctl, int maxblkext, int outovropt,int qryrowset,int qryclsimpl) { currentDrdaRs.setOPNQRYOptions( blksize, qryblkctl, maxblkext, outovropt, qryrowset, qryclsimpl); } /* * Set query options sent on CNTQRY */ protected void setQueryOptions(int blksize, boolean qryrelscr, long qryrownbr, boolean qryfrtbl,int nbrrow,int maxblkext, int qryscrorn, boolean qryrowsns, boolean qryblkrst, boolean qryrtndta,int qryrowset, int rtnextdta) { currentDrdaRs.blksize = blksize; currentDrdaRs.qryrelscr = qryrelscr; currentDrdaRs.qryrownbr = qryrownbr; currentDrdaRs.qryrfrtbl = qryrfrtbl; currentDrdaRs.nbrrow = nbrrow; currentDrdaRs.maxblkext = maxblkext; currentDrdaRs.qryscrorn = qryscrorn; currentDrdaRs.qryrowsns = qryrowsns; currentDrdaRs.qryblkrst = qryblkrst; currentDrdaRs.qryrtndta = qryrtndta; currentDrdaRs.qryrowset = qryrowset; currentDrdaRs.rtnextdta = rtnextdta; } protected void setQryprctyp(int qryprctyp) { this.qryprctyp = qryprctyp; currentDrdaRs.setQryprctyp(qryprctyp); } protected int getQryprctyp() throws SQLException { return currentDrdaRs.getQryprctyp(); } protected void setQryrownbr(long qryrownbr) { currentDrdaRs.qryrownbr = qryrownbr; } protected long getQryrownbr() { return currentDrdaRs.qryrownbr; } protected int getQryrowset() { return currentDrdaRs.qryrowset; } protected int getBlksize() { return currentDrdaRs.blksize; } protected void setQryrtndta(boolean qryrtndta) { currentDrdaRs.qryrtndta = qryrtndta; } protected boolean getQryrtndta() { return currentDrdaRs.qryrtndta; } protected void setQryscrorn(int qryscrorn) { currentDrdaRs.qryscrorn = qryscrorn; } protected int getQryscrorn() { return currentDrdaRs.qryscrorn; } protected void setScrollType(int scrollType) { currentDrdaRs.scrollType = scrollType; } protected int getScrollType() { return currentDrdaRs.scrollType; } /** * is this a scrollable cursor? * return true if this is not a forward only cursor */ protected boolean isScrollable() { return (getScrollType() != ResultSet.TYPE_FORWARD_ONLY); } protected void setConcurType(int scrollType) { currentDrdaRs.concurType = scrollType; } protected int getConcurType() { return currentDrdaRs.concurType; } protected void setOutovr_drdaType(int[] outovr_drdaType) { currentDrdaRs.outovr_drdaType = outovr_drdaType; } protected int[] getOutovr_drdaType() { return currentDrdaRs.outovr_drdaType; } protected boolean hasdata() { return currentDrdaRs.hasdata; } protected void setHasdata(boolean hasdata) { currentDrdaRs.hasdata = hasdata; } /** * This method is used to initialize the default statement of the database * for re-use. It is different from reset() method since default statements * get initiliazed differently. e.g: stmt variable used in default statement * is created only once in Database.makeConnection. * TODO: Need to see what exactly it means to initialize the default * statement. (DERBY-1002) * */ protected void initialize() { setTypDefValues(); } protected PreparedStatement explicitPrepare(String sqlStmt) throws SQLException { explicitlyPrepared = true; return prepare(sqlStmt); } protected boolean wasExplicitlyPrepared() { return explicitlyPrepared; } /** * Create a prepared statement * * @param sqlStmt - SQL statement * * @exception SQLException */ protected PreparedStatement prepare(String sqlStmt) throws SQLException { // save current prepare iso level int saveIsolationLevel = -1; boolean isolationSet = false; if (pkgnamcsn !=null && isolationLevel != Connection.TRANSACTION_NONE) { saveIsolationLevel = database.getPrepareIsolation(); database.setPrepareIsolation(isolationLevel); isolationSet = true; } if (isCallableSQL(sqlStmt)) { isCall = true; ps = database.getConnection().prepareCall(sqlStmt); setupCallableStatementParams((CallableStatement)ps); if (isolationSet) database.setPrepareIsolation(saveIsolationLevel); return ps; } parsePkgidToFindHoldability(); ps = prepareStatementJDBC3(sqlStmt, scrollType, concurType, withHoldCursor); // beetle 3849 - Need to change the cursor name to what // JCC thinks it will be, since there is no way in the // protocol to communicate the actual cursor name. JCC keeps // a mapping from the client cursor names to the DB2 style cursor names if (cursorName != null)//cursorName not null means we are dealing with dynamic pacakges ps.setCursorName(cursorName); if (isolationSet) database.setPrepareIsolation(saveIsolationLevel); return ps; } /** * Get prepared statement * * @return prepared statement */ protected PreparedStatement getPreparedStatement() throws SQLException { if (ps instanceof BrokeredPreparedStatement) return (PreparedStatement)( ((BrokeredPreparedStatement) ps).getStatement()); else return ps; } /** * Executes the prepared statement and populates the resultSetTable. * Access to the various resultSets is then possible by using * setCurrentDrdaResultSet(String pkgnamcsn) to set the current * resultSet and then calling getResultSet() or the other access * methods to get resultset data. * * @return true if the execution has resultSets */ protected boolean execute() throws SQLException { boolean hasResultSet = ps.execute(); // java.sql.Statement says any result sets that are opened // when the statement is re-executed must be closed; this // is handled by the call to "ps.execute()" above--but we
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -