?? preparedstatement.java
字號:
protected int numberOfExecutions = 0; /** The SQL that was passed in to 'prepare' */ protected String originalSql = null; /** The number of parameters in this PreparedStatement */ protected int parameterCount; protected MysqlParameterMetadata parameterMetaData; private InputStream[] parameterStreams = null; private byte[][] parameterValues = null; /** * Only used by statement interceptors at the moment to * provide introspection of bound values */ protected int[] parameterTypes = null; private ParseInfo parseInfo; private java.sql.ResultSetMetaData pstmtResultMetaData; private byte[][] staticSqlStrings = null; private byte[] streamConvertBuf = new byte[4096]; private int[] streamLengths = null; private SimpleDateFormat tsdf = null; /** * Are we using a version of MySQL where we can use 'true' boolean values? */ protected boolean useTrueBoolean = false; protected boolean usingAnsiMode; protected String batchedValuesClause; /** Where does the statement text actually start? */ private int statementAfterCommentsPos; /** * have we checked whether we can rewrite this statement as a multi-value * insert? */ private boolean hasCheckedForRewrite = false; /** Can we actually rewrite this statement as a multi-value insert? */ private boolean canRewrite = false; private boolean doPingInstead; /** * Creates a prepared statement instance -- We need to provide factory-style * methods so we can support both JDBC3 (and older) and JDBC4 runtimes, * otherwise the class verifier complains when it tries to load JDBC4-only * interface classes that are present in JDBC4 method signatures. */ protected static PreparedStatement getInstance(ConnectionImpl conn, String catalog) throws SQLException { if (!Util.isJdbc4()) { return new PreparedStatement(conn, catalog); } return (PreparedStatement) Util.handleNewInstance( JDBC_4_PSTMT_2_ARG_CTOR, new Object[] { conn, catalog }); } /** * Creates a prepared statement instance -- We need to provide factory-style * methods so we can support both JDBC3 (and older) and JDBC4 runtimes, * otherwise the class verifier complains when it tries to load JDBC4-only * interface classes that are present in JDBC4 method signatures. */ protected static PreparedStatement getInstance(ConnectionImpl conn, String sql, String catalog) throws SQLException { if (!Util.isJdbc4()) { return new PreparedStatement(conn, sql, catalog); } return (PreparedStatement) Util.handleNewInstance( JDBC_4_PSTMT_3_ARG_CTOR, new Object[] { conn, sql, catalog }); } /** * Creates a prepared statement instance -- We need to provide factory-style * methods so we can support both JDBC3 (and older) and JDBC4 runtimes, * otherwise the class verifier complains when it tries to load JDBC4-only * interface classes that are present in JDBC4 method signatures. */ protected static PreparedStatement getInstance(ConnectionImpl conn, String sql, String catalog, ParseInfo cachedParseInfo) throws SQLException { if (!Util.isJdbc4()) { return new PreparedStatement(conn, sql, catalog, cachedParseInfo); } return (PreparedStatement) Util.handleNewInstance( JDBC_4_PSTMT_4_ARG_CTOR, new Object[] { conn, sql, catalog, cachedParseInfo }); } /** * Constructor used by server-side prepared statements * * @param conn * the connection that created us * @param catalog * the catalog in use when we were created * * @throws SQLException * if an error occurs */ public PreparedStatement(ConnectionImpl conn, String catalog) throws SQLException { super(conn, catalog); } /** * Constructor for the PreparedStatement class. * * @param conn * the connection creating this statement * @param sql * the SQL for this statement * @param catalog * the catalog/database this statement should be issued against * * @throws SQLException * if a database error occurs. */ public PreparedStatement(ConnectionImpl conn, String sql, String catalog) throws SQLException { super(conn, catalog); if (sql == null) { throw SQLError.createSQLException(Messages.getString("PreparedStatement.0"), //$NON-NLS-1$ SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } this.originalSql = sql; if (this.originalSql.startsWith(PING_MARKER)) { this.doPingInstead = true; } else { this.doPingInstead = false; } this.dbmd = this.connection.getMetaData(); this.useTrueBoolean = this.connection.versionMeetsMinimum(3, 21, 23); this.parseInfo = new ParseInfo(sql, this.connection, this.dbmd, this.charEncoding, this.charConverter); initializeFromParseInfo(); } /** * Creates a new PreparedStatement object. * * @param conn * the connection creating this statement * @param sql * the SQL for this statement * @param catalog * the catalog/database this statement should be issued against * @param cachedParseInfo * already created parseInfo. * * @throws SQLException * DOCUMENT ME! */ public PreparedStatement(ConnectionImpl conn, String sql, String catalog, ParseInfo cachedParseInfo) throws SQLException { super(conn, catalog); if (sql == null) { throw SQLError.createSQLException(Messages.getString("PreparedStatement.1"), //$NON-NLS-1$ SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } this.originalSql = sql; this.dbmd = this.connection.getMetaData(); this.useTrueBoolean = this.connection.versionMeetsMinimum(3, 21, 23); this.parseInfo = cachedParseInfo; this.usingAnsiMode = !this.connection.useAnsiQuotedIdentifiers(); initializeFromParseInfo(); } /** * JDBC 2.0 Add a set of parameters to the batch. * * @exception SQLException * if a database-access error occurs. * * @see StatementImpl#addBatch */ public void addBatch() throws SQLException { if (this.batchedArgs == null) { this.batchedArgs = new ArrayList(); } this.batchedArgs.add(new BatchParams(this.parameterValues, this.parameterStreams, this.isStream, this.streamLengths, this.isNull)); } public synchronized void addBatch(String sql) throws SQLException { this.batchHasPlainStatements = true; super.addBatch(sql); } protected String asSql() throws SQLException { return asSql(false); } protected String asSql(boolean quoteStreamsAndUnknowns) throws SQLException { if (this.isClosed) { return "statement has been closed, no further internal information available"; } StringBuffer buf = new StringBuffer(); try { for (int i = 0; i < this.parameterCount; ++i) { if (this.charEncoding != null) { buf.append(new String(this.staticSqlStrings[i], this.charEncoding)); } else { buf.append(new String(this.staticSqlStrings[i])); } if ((this.parameterValues[i] == null) && !this.isStream[i]) { if (quoteStreamsAndUnknowns) { buf.append("'"); } buf.append("** NOT SPECIFIED **"); //$NON-NLS-1$ if (quoteStreamsAndUnknowns) { buf.append("'"); } } else if (this.isStream[i]) { if (quoteStreamsAndUnknowns) { buf.append("'"); } buf.append("** STREAM DATA **"); //$NON-NLS-1$ if (quoteStreamsAndUnknowns) { buf.append("'"); } } else { if (this.charConverter != null) { buf.append(this.charConverter .toString(this.parameterValues[i])); } else { if (this.charEncoding != null) { buf.append(new String(this.parameterValues[i], this.charEncoding)); } else { buf.append(StringUtils .toAsciiString(this.parameterValues[i])); } } } } if (this.charEncoding != null) { buf.append(new String( this.staticSqlStrings[this.parameterCount], this.charEncoding)); } else { buf .append(StringUtils .toAsciiString(this.staticSqlStrings[this.parameterCount])); } } catch (UnsupportedEncodingException uue) { throw new RuntimeException(Messages .getString("PreparedStatement.32") //$NON-NLS-1$ + this.charEncoding + Messages.getString("PreparedStatement.33")); //$NON-NLS-1$ } return buf.toString(); } public synchronized void clearBatch() throws SQLException { this.batchHasPlainStatements = false; super.clearBatch(); } /** * In general, parameter values remain in force for repeated used of a * Statement. Setting a parameter value automatically clears its previous * value. However, in some cases, it is useful to immediately release the * resources used by the current parameter values; this can be done by * calling clearParameters * * @exception SQLException * if a database access error occurs */ public synchronized void clearParameters() throws SQLException { checkClosed(); for (int i = 0; i < this.parameterValues.length; i++) { this.parameterValues[i] = null; this.parameterStreams[i] = null; this.isStream[i] = false; this.isNull[i] = false; this.parameterTypes[i] = Types.NULL; } } /** * Closes this prepared statement and releases all resources. * * @throws SQLException * if database error occurs. */ public synchronized void close() throws SQLException { realClose(true, true); } private final void escapeblockFast(byte[] buf, Buffer packet, int size) throws SQLException { int lastwritten = 0; for (int i = 0; i < size; i++) { byte b = buf[i]; if (b == '\0') { // write stuff not yet written if (i > lastwritten) { packet.writeBytesNoNull(buf, lastwritten, i - lastwritten); } // write escape packet.writeByte((byte) '\\'); packet.writeByte((byte) '0'); lastwritten = i + 1; } else { if ((b == '\\') || (b == '\'') || (!this.usingAnsiMode && b == '"')) { // write stuff not yet written if (i > lastwritten) { packet.writeBytesNoNull(buf, lastwritten, i - lastwritten); } // write escape packet.writeByte((byte) '\\'); lastwritten = i; // not i+1 as b wasn't written. } } } // write out remaining stuff from buffer if (lastwritten < size) { packet.writeBytesNoNull(buf, lastwritten, size - lastwritten); } } private final void escapeblockFast(byte[] buf, ByteArrayOutputStream bytesOut, int size) { int lastwritten = 0; for (int i = 0; i < size; i++) { byte b = buf[i]; if (b == '\0') { // write stuff not yet written if (i > lastwritten) { bytesOut.write(buf, lastwritten, i - lastwritten); } // write escape bytesOut.write('\\'); bytesOut.write('0'); lastwritten = i + 1; } else { if ((b == '\\') || (b == '\'') || (!this.usingAnsiMode && b == '"')) { // write stuff not yet written if (i > lastwritten) { bytesOut.write(buf, lastwritten, i - lastwritten); } // write escape bytesOut.write('\\'); lastwritten = i; // not i+1 as b wasn't written. } } } // write out remaining stuff from buffer if (lastwritten < size) { bytesOut.write(buf, lastwritten, size - lastwritten); } } /** * Some prepared statements return multiple results; the execute method * handles these complex statements as well as the simpler form of * statements handled by executeQuery and executeUpdate * * @return true if the next result is a ResultSet; false if it is an update * count or there are no more results * * @exception SQLException * if a database access error occurs */ public boolean execute() throws SQLException { checkClosed(); ConnectionImpl locallyScopedConn = this.connection; if (locallyScopedConn.isReadOnly() && (this.firstCharOfStmt != 'S')) { throw SQLError.createSQLException(Messages.getString("PreparedStatement.20") //$NON-NLS-1$ + Messages.getString("PreparedStatement.21"), //$NON-NLS-1$ SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } ResultSetInternalMethods rs = null; CachedResultSetMetaData cachedMetadata = null; synchronized (locallyScopedConn.getMutex()) { boolean doStreaming = createStreamingResultSet(); clearWarnings(); // Adjust net_write_timeout to a higher value if we're // streaming result sets. More often than not, someone runs into // an issue where they blow net_write_timeout when using this // feature, and if they're willing to hold a result set open // for 30 seconds or more, one more round-trip isn't going to hurt // // This is reset by RowDataDynamic.close(). if (doStreaming && this.connection.getNetTimeoutForStreamingResults() > 0) { executeSimpleNonQuery(locallyScopedConn, "SET net_write_timeout=" + this.connection .getNetTimeoutForStreamingResults()); } this.batchedGeneratedKeys = null; Buffer sendPacket = fillSendPacket();
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -