📄 statement.java
字号:
String oldCatalog = null; if (!this.connection.getCatalog().equals(this.currentCatalog)) { oldCatalog = this.connection.getCatalog(); this.connection.setCatalog(this.currentCatalog); } // // Only apply max_rows to selects // if (this.connection.useMaxRows()) { this.connection.execSQL(this, "SET OPTION SQL_SELECT_LIMIT=DEFAULT", //$NON-NLS-1$ -1, null, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY, false, false, this.currentCatalog, true); } rs = this.connection .execSQL(this, sql, -1, null, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY, false, false, this.currentCatalog, true /* * force read of field * info on DML */); if (oldCatalog != null) { this.connection.setCatalog(oldCatalog); } } this.results = rs; rs.setFirstCharOfQuery(firstStatementChar); this.updateCount = rs.getUpdateCount(); int truncatedUpdateCount = 0; if (this.updateCount > Integer.MAX_VALUE) { truncatedUpdateCount = Integer.MAX_VALUE; } else { truncatedUpdateCount = (int) this.updateCount; } this.lastInsertId = rs.getUpdateID(); return truncatedUpdateCount; } /** * @see Statement#executeUpdate(String, int) */ public int executeUpdate(String sql, int returnGeneratedKeys) throws SQLException { if (returnGeneratedKeys == java.sql.Statement.RETURN_GENERATED_KEYS) { checkClosed(); synchronized (this.connection.getMutex()) { // If this is a 'REPLACE' query, we need to be able to parse // the 'info' message returned from the server to determine // the actual number of keys generated. boolean readInfoMsgState = this.connection .isReadInfoMsgEnabled(); this.connection.setReadInfoMsgEnabled(true); try { return executeUpdate(sql); } finally { this.connection.setReadInfoMsgEnabled(readInfoMsgState); } } } return executeUpdate(sql); } /** * @see Statement#executeUpdate(String, int[]) */ public int executeUpdate(String sql, int[] generatedKeyIndices) throws SQLException { if ((generatedKeyIndices != null) && (generatedKeyIndices.length > 0)) { checkClosed(); synchronized (this.connection.getMutex()) { // If this is a 'REPLACE' query, we need to be able to parse // the 'info' message returned from the server to determine // the actual number of keys generated. boolean readInfoMsgState = this.connection .isReadInfoMsgEnabled(); this.connection.setReadInfoMsgEnabled(true); try { return executeUpdate(sql); } finally { this.connection.setReadInfoMsgEnabled(readInfoMsgState); } } } return executeUpdate(sql); } /** * @see Statement#executeUpdate(String, String[]) */ public int executeUpdate(String sql, String[] generatedKeyNames) throws SQLException { if ((generatedKeyNames != null) && (generatedKeyNames.length > 0)) { checkClosed(); synchronized (this.connection.getMutex()) { // If this is a 'REPLACE' query, we need to be able to parse // the 'info' message returned from the server to determine // the actual number of keys generated. boolean readInfoMsgState = this.connection .isReadInfoMsgEnabled(); this.connection.setReadInfoMsgEnabled(true); try { return executeUpdate(sql); } finally { this.connection.setReadInfoMsgEnabled(readInfoMsgState); } } } return executeUpdate(sql); } /** * Required by JDBC spec */ /* protected void finalize() throws Throwable { if (!this.isClosed) { realClose(false); } }*/ /** * Returns cached metadata (or null if not cached) for the given query, * which must match _exactly_. Note this method is guarded against * concurrent access via the synchronized{} block in execute() and * executeQuery(). * * @param sql * the query that is the key to the cache * * @return DOCUMENT ME! */ protected CachedResultSetMetaData getCachedMetaData(String sql) { if (this.resultSetMetadataCache != null) { return (CachedResultSetMetaData) this.resultSetMetadataCache .get(sql); } return null; // no cache exists (yet) } /** * JDBC 2.0 Return the Connection that produced the Statement. * * @return the Connection that produced the Statement * * @throws SQLException * if an error occurs */ public synchronized java.sql.Connection getConnection() throws SQLException { return this.connection; } /** * JDBC 2.0 Determine the fetch direction. * * @return the default fetch direction * * @exception SQLException * if a database-access error occurs */ public int getFetchDirection() throws SQLException { return java.sql.ResultSet.FETCH_FORWARD; } /** * JDBC 2.0 Determine the default fetch size. * * @return the number of rows to fetch at a time * * @throws SQLException * if an error occurs */ public synchronized int getFetchSize() throws SQLException { return this.fetchSize; } /** * DOCUMENT ME! * * @return DOCUMENT ME! * * @throws SQLException * DOCUMENT ME! */ public synchronized java.sql.ResultSet getGeneratedKeys() throws SQLException { return getGeneratedKeysInternal(); } /* * Needed because there's no concept of super.super to get to this * implementation from ServerPreparedStatement when dealing with batched * updates. */ protected synchronized java.sql.ResultSet getGeneratedKeysInternal() throws SQLException { Field[] fields = new Field[1]; fields[0] = new Field("", "GENERATED_KEY", Types.BIGINT, 17); //$NON-NLS-1$ //$NON-NLS-2$ fields[0].setConnection(this.connection); ArrayList rowSet = new ArrayList(); long beginAt = getLastInsertID(); int numKeys = getUpdateCount(); String serverInfo = this.results.getServerInfo(); // // Only parse server info messages for 'REPLACE' // queries // if ((numKeys > 0) && (this.results.getFirstCharOfQuery() == 'R') && (serverInfo != null) && (serverInfo.length() > 0)) { numKeys = getRecordCountFromInfo(serverInfo); } if ((beginAt > 0) && (numKeys > 0)) { for (int i = 0; i < numKeys; i++) { byte[][] row = new byte[1][]; row[0] = Long.toString(beginAt++).getBytes(); rowSet.add(row); } } return new com.mysql.jdbc.ResultSet(this.currentCatalog, fields, new RowDataStatic(rowSet), this.connection, this); } /** * Returns the id used when profiling * * @return the id used when profiling. */ protected int getId() { return this.statementId; } /** * getLastInsertID returns the value of the auto_incremented key after an * executeQuery() or excute() call. * * <p> * This gets around the un-threadsafe behavior of "select LAST_INSERT_ID()" * which is tied to the Connection that created this Statement, and * therefore could have had many INSERTS performed before one gets a chance * to call "select LAST_INSERT_ID()". * </p> * * @return the last update ID. */ public synchronized long getLastInsertID() { return this.lastInsertId; } /** * getLongUpdateCount returns the current result as an update count, if the * result is a ResultSet or there are no more results, -1 is returned. It * should only be called once per result. * * <p> * This method returns longs as MySQL server versions newer than 3.22.4 * return 64-bit values for update counts * </p> * * @return the current update count. */ public synchronized long getLongUpdateCount() { if (this.results == null) { return -1; } if (this.results.reallyResult()) { return -1; } return this.updateCount; } /** * The maxFieldSize limit (in bytes) is the maximum amount of data returned * for any column value; it only applies to BINARY, VARBINARY, * LONGVARBINARY, CHAR, VARCHAR and LONGVARCHAR columns. If the limit is * exceeded, the excess data is silently discarded. * * @return the current max column size limit; zero means unlimited * * @exception SQLException * if a database access error occurs */ public synchronized int getMaxFieldSize() throws SQLException { return this.maxFieldSize; } /** * The maxRows limit is set to limit the number of rows that any ResultSet * can contain. If the limit is exceeded, the excess rows are silently * dropped. * * @return the current maximum row limit; zero means unlimited * * @exception SQLException * if a database access error occurs */ public synchronized int getMaxRows() throws SQLException { if (this.maxRows <= 0) { return 0; } return this.maxRows; } /** * getMoreResults moves to a Statement's next result. If it returns true, * this result is a ResulSet. * * @return true if the next ResultSet is valid * * @exception SQLException * if a database access error occurs */ public boolean getMoreResults() throws SQLException { return getMoreResults(CLOSE_CURRENT_RESULT); } /** * @see Statement#getMoreResults(int) */ public synchronized boolean getMoreResults(int current) throws SQLException { if (this.results == null) { return false; } ResultSet nextResultSet = this.results.getNextResultSet(); switch (current) { case java.sql.Statement.CLOSE_CURRENT_RESULT: if (this.results != null) { this.results.close(); this.results.clearNextResult(); } break; case java.sql.Statement.CLOSE_ALL_RESULTS: if (this.results != null) { this.results.close(); this.results.clearNextResult(); } closeAllOpenResults(); break; case java.sql.Statement.KEEP_CURRENT_RESULT: if (!this.connection.getDontTrackOpenResources()) { this.openResults.add(this.results); } this.results.clearNextResult(); // nobody besides us should // ever need this value... break; default: throw new SQLException(Messages.getString("Statement.19"), //$NON-NLS-1$ SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$ } this.results = nextResultSet; if (this.results == null) { this.updateCount = -1; this.lastInsertId = -1; } else if (this.results.reallyResult()) { this.updateCount = -1; this.lastInsertId = -1; } else { this.updateCount = this.results.getUpdateCount(); this.lastInsertId = this.results.getUpdateID(); } return ((this.results != null) && this.results.reallyResult()) ? true : false; } /** * The queryTimeout limit is the number of seconds the driver will wait for * a Statement to execute. If the limit is exceeded, a SQLException is * thrown. * * @return the current query timeout limit in seconds; 0 = unlimited * * @exception SQLException * if a database access error occurs */ public int getQueryTimeout() throws SQLException { return this.timeout; } /** * Parses actual record count from 'info' message * * @param serverInfo * DOCUMENT ME! * * @return DOCUMENT ME! */ private int getRecordCountFromInfo(String serverInfo) { StringBuffer recordsBuf = new StringBuffer(); int recordsCount = 0; int duplicatesCount = 0; char c = (char) 0; int length = serverInfo.length(); int i = 0; for (; i < length; i++) { c = serverInfo.charAt(i); if (Character.isDigit(c)) { break; } } recordsBuf.append(c); i++; for (; i < length; i++) { c = serverInfo.charAt(i);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -