⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 statement.java

📁 在资料浩瀚的互联网中
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
            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);    }    /**     * 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)    }    /**     * Returns the id used when profiling     *     * @return the id used when profiling.     */    protected int getId() {        return this.statementId;    }    /**     * Checks if closed() has been called, and throws an exception if so     *     * @throws SQLException if this statement has been closed     */    protected void checkClosed() throws SQLException {        if (this.isClosed) {            throw new SQLException(Messages.getString("Statement.49"), //$NON-NLS-1$                SQLError.SQL_STATE_CONNECTION_NOT_OPEN); //$NON-NLS-1$        }    }    /**     * Checks if the given SQL query with the given first non-ws char is a DML     * statement. Throws an exception if it is.     *     * @param sql the SQL to check     * @param firstStatementChar the UC first non-ws char of the statement     *     * @throws SQLException if the statement contains DML     */    protected void checkForDml(String sql, char firstStatementChar)        throws SQLException {        if ((firstStatementChar == 'I') || (firstStatementChar == 'U') ||                (firstStatementChar == 'D') || (firstStatementChar == 'A') ||                (firstStatementChar == 'C')) {            if (StringUtils.startsWithIgnoreCaseAndWs(sql, "INSERT") //$NON-NLS-1$                     ||StringUtils.startsWithIgnoreCaseAndWs(sql, "UPDATE") //$NON-NLS-1$                     ||StringUtils.startsWithIgnoreCaseAndWs(sql, "DELETE") //$NON-NLS-1$                     ||StringUtils.startsWithIgnoreCaseAndWs(sql, "DROP") //$NON-NLS-1$                     ||StringUtils.startsWithIgnoreCaseAndWs(sql, "CREATE") //$NON-NLS-1$                     ||StringUtils.startsWithIgnoreCaseAndWs(sql, "ALTER")) { //$NON-NLS-1$                throw new SQLException(Messages.getString("Statement.57"), //$NON-NLS-1$                    SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$            }        }    }    /**     * Method checkNullOrEmptyQuery.     *     * @param sql the SQL to check     *     * @throws SQLException if query is null or empty.     */    protected void checkNullOrEmptyQuery(String sql) throws SQLException {        if (sql == null) {            throw new SQLException(Messages.getString("Statement.59"), //$NON-NLS-1$                SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$ //$NON-NLS-2$        }        if (sql.length() == 0) {            throw new SQLException(Messages.getString("Statement.61"), //$NON-NLS-1$                SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$ //$NON-NLS-2$        }    }    /**     * Close any open result sets that have been 'held open'     */    protected void closeAllOpenResults() {        if (this.openResults != null) {            for (Iterator iter = this.openResults.iterator(); iter.hasNext();) {                ResultSet element = (ResultSet) iter.next();                try {                    element.realClose(false);                } catch (SQLException sqlEx) {                    AssertionFailedException.shouldNotHappen(sqlEx);                }            }            this.openResults.clear();        }    }    /**     * We only stream result sets when they are forward-only, read-only, and     * the fetch size has been set to Integer.MIN_VALUE     *     * @return true if this result set should be streamed row at-a-time, rather     *         than read all at once.     */    protected boolean createStreamingResultSet() {        return ((this.resultSetType == java.sql.ResultSet.TYPE_FORWARD_ONLY) &&        (this.resultSetConcurrency == java.sql.ResultSet.CONCUR_READ_ONLY) &&        (this.fetchSize == Integer.MIN_VALUE));    }    /**     * Caches CachedResultSetMetaData that has been placed in the cache using     * the given SQL as a key.     *     * @param sql DOCUMENT ME!     * @param cachedMetaData DOCUMENT ME!     * @param resultSet DOCUMENT ME!     *     * @throws SQLException DOCUMENT ME!     */    protected void initializeResultsMetadataFromCache(String sql,        CachedResultSetMetaData cachedMetaData, ResultSet resultSet)        throws SQLException {        synchronized (resultSet) {            if (cachedMetaData == null) {                // read from results                cachedMetaData = new CachedResultSetMetaData();                cachedMetaData.fields = this.results.fields;                // assume that users will use named-based                // lookups                resultSet.buildIndexMapping();                cachedMetaData.columnNameToIndex = resultSet.columnNameToIndex;                cachedMetaData.fullColumnNameToIndex = resultSet.fullColumnNameToIndex;                cachedMetaData.metadata = resultSet.getMetaData();                if (this.resultSetMetadataCache == null) {                    this.resultSetMetadataCache = new LRUCache(this.connection.getMetadataCacheSize());                }                this.resultSetMetadataCache.put(sql, cachedMetaData);            } else {                // initialize results from cached data                resultSet.fields = cachedMetaData.fields;                resultSet.columnNameToIndex = cachedMetaData.columnNameToIndex;                resultSet.fullColumnNameToIndex = cachedMetaData.fullColumnNameToIndex;                resultSet.hasBuiltIndexMapping = true;                // results.resultSetMetaData = cachedMetaData.metadata;            }        }    }    /**     * Closes this statement, and frees resources.     *     * @param calledExplicitly was this called from close()?     *     * @throws SQLException if an error occurs     */    protected void realClose(boolean calledExplicitly)        throws SQLException {        if (this.isClosed) {            return;        }        if (this.useUsageAdvisor) {            if (!calledExplicitly) {                String message = Messages.getString("Statement.63") //$NON-NLS-1$                     +Messages.getString("Statement.64"); //$NON-NLS-1$                this.eventSink.consumeEvent(new ProfilerEvent(                        ProfilerEvent.TYPE_WARN, "", //$NON-NLS-1$                        this.currentCatalog, this.connection.getId(),                        this.getId(), -1, System.currentTimeMillis(), 0, null,                        this.pointOfOrigin, message));            }        }        if (this.results != null) {        	if (this.connection != null &&         		!this.connection.getHoldResultsOpenOverStatementClose()) {        		try {        			this.results.close();        		} catch (Exception ex) {        			;        		}        	}        }        if (this.connection != null) {            if (this.maxRowsChanged) {                this.connection.unsetMaxRows(this);            }            if (!this.connection.getDontTrackOpenResources()) {            	this.connection.unregisterStatement(this);            }        }        this.closeAllOpenResults();                this.results = null;        this.connection = null;        this.warningChain = null;             this.openResults = null;        this.isClosed = true;    }    /**     * Sets the concurrency for result sets generated by this statement     *     * @param concurrencyFlag DOCUMENT ME!     */    synchronized void setResultSetConcurrency(int concurrencyFlag) {        this.resultSetConcurrency = concurrencyFlag;    }    /**     * Sets the result set type for result sets generated by this statement     *     * @param typeFlag DOCUMENT ME!     */    synchronized void setResultSetType(int typeFlag) {        this.resultSetType = typeFlag;    }    /**     * 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);            if (!Character.isDigit(c)) {                break;            }            recordsBuf.append(c);        }        recordsCount = Integer.parseInt(recordsBuf.toString());        StringBuffer duplicatesBuf = new StringBuffer();        for (; i < length; i++) {            c = serverInfo.charAt(i);            if (Character.isDigit(c)) {                break;            }        }        duplicatesBuf.append(c);        i++;        for (; i < length; i++) {            c = serverInfo.charAt(i);            if (!Character.isDigit(c)) {                break;            }            duplicatesBuf.append(c);        }        duplicatesCount = Integer.parseInt(duplicatesBuf.toString());        return recordsCount - duplicatesCount;    }    class CachedResultSetMetaData {        /** Map column names (and all of their permutations) to column indices */        Map columnNameToIndex = null;        /** Map of fully-specified column names to column indices */        Map fullColumnNameToIndex = null;        /** Cached ResultSetMetaData */        java.sql.ResultSetMetaData metadata;        /** Cached Field info */        Field[] fields;    }}

⌨️ 快捷键说明

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