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

📄 statement.java

📁 基于b/s的网上书店
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                    results = connection.execSQL(sql, maxRows,
                            resultSetConcurrency, createStreamingResultSet(),
                            true, this.currentCatalog);
                } else {
                    if (maxRows <= 0) {
                        connection.execSQL("SET OPTION SQL_SELECT_LIMIT=DEFAULT",
                            -1, this.currentCatalog);
                    } else {
                        connection.execSQL("SET OPTION SQL_SELECT_LIMIT="
                            + maxRows, -1, this.currentCatalog);
                    }

                    results = connection.execSQL(sql, -1, resultSetConcurrency,
                            createStreamingResultSet(), true,
                            this.currentCatalog);

                    if (oldCatalog != null) {
                        connection.setCatalog(oldCatalog);
                    }
                }
            } else {
                results = connection.execSQL(sql, -1, resultSetConcurrency,
                        createStreamingResultSet(), true, this.currentCatalog);
            }

            if (oldCatalog != null) {
                connection.setCatalog(oldCatalog);
            }
        }

        lastInsertId = results.getUpdateID();
        nextResults = results;
        results.setConnection(connection);
        results.setResultSetType(resultSetType);
        results.setResultSetConcurrency(resultSetConcurrency);
        results.setStatement(this);

        if (!results.reallyResult()) {
            if (!connection.getAutoCommit()) {
                connection.rollback();
            }

            throw new SQLException("Can not issue INSERT/UPDATE/DELETE with executeQuery()",
                SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
        }

        return (java.sql.ResultSet) results;
    }

    /**
     * Execute a SQL INSERT, UPDATE or DELETE statement.  In addition SQL
     * statements that return nothing such as SQL DDL statements can be
     * executed Any IDs generated for AUTO_INCREMENT fields can be retrieved
     * by casting this Statement to org.gjt.mm.mysql.Statement and calling the
     * getLastInsertID() method.
     *
     * @param sql a SQL statement
     *
     * @return either a row count, or 0 for SQL commands
     *
     * @exception SQLException if a database access error occurs
     */
    public synchronized int executeUpdate(String sql) throws SQLException {
    	return executeUpdate(sql, true);
    }
    
    private synchronized int executeUpdate(String sql, boolean clearWarnings) throws SQLException {
        if (Driver.TRACE) {
            Object[] args = { sql };
            Debug.methodCall(this, "executeUpdate", args);
        }

        if (connection.isReadOnly()) {
            throw new SQLException("Connection is read-only. "
                + "Queries leading to data modification are not allowed",
                SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
        }

        char firstStatementChar = StringUtils.firstNonWsCharUc(sql);

        if ((firstStatementChar == 'S')
                && StringUtils.startsWithIgnoreCaseAndWs(sql, "select")) {
            throw new SQLException("Can not issue SELECT via executeUpdate()",
                SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
        }

        checkClosed();

        if (this.doEscapeProcessing) {
            sql = EscapeProcessor.escapeSQL(sql, this.serverSupportsConvertFn);
        }

        // The checking and changing of catalogs
        // must happen in sequence, so synchronize
        // on the same mutex that _conn is using
        ResultSet rs = null;

        synchronized (connection.getMutex()) {
        	if (clearWarnings) {
        		clearWarnings();
        	}
        	
            String oldCatalog = null;

            if (!connection.getCatalog().equals(currentCatalog)) {
                oldCatalog = connection.getCatalog();
                connection.setCatalog(currentCatalog);
            }

            //
            // Only apply max_rows to selects
            //
            if (connection.useMaxRows()) {
                connection.execSQL("SET OPTION SQL_SELECT_LIMIT=DEFAULT", -1,
                    this.currentCatalog);
            }

            rs = connection.execSQL(sql, -1,
                    java.sql.ResultSet.CONCUR_READ_ONLY, false, false,
                    this.currentCatalog);
            rs.setConnection(connection);

            if (oldCatalog != null) {
                connection.setCatalog(oldCatalog);
            }
        }

        this.results = rs;

		rs.setFirstCharOfQuery(firstStatementChar);
		
        updateCount = rs.getUpdateCount();

        int truncatedUpdateCount = 0;

        if (updateCount > Integer.MAX_VALUE) {
            truncatedUpdateCount = Integer.MAX_VALUE;
        } else {
            truncatedUpdateCount = (int) updateCount;
        }

        lastInsertId = rs.getUpdateID();

        return truncatedUpdateCount;
    }

    /**
     * @see Statement#executeUpdate(String, int)
     */
    public int executeUpdate(String sql, int returnGeneratedKeys)
        throws SQLException {
        if (returnGeneratedKeys == 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);
                }
            }
        } else {
            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);
                }
            }
        } else {
            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);
                }
            }
        } else {
            return executeUpdate(sql);
        }
    }

    /**
     * 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("No operations allowed after statement closed",
                SQLError.SQL_STATE_GENERAL_ERROR);
        }
    }

    /**
     * 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.close();
                } 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 ((resultSetType == java.sql.ResultSet.TYPE_FORWARD_ONLY)
        && (resultSetConcurrency == java.sql.ResultSet.CONCUR_READ_ONLY)
        && (fetchSize == Integer.MIN_VALUE));
    }

    /**
     * Sets the concurrency for result sets generated by this statement
     *
     * @param concurrencyFlag DOCUMENT ME!
     */
    void setResultSetConcurrency(int concurrencyFlag) {
        resultSetConcurrency = concurrencyFlag;
    }

    /**
     * Sets the result set type for result sets generated by this statement
     *
     * @param typeFlag DOCUMENT ME!
     */
    void setResultSetType(int typeFlag) {
        resultSetType = typeFlag;
    }
    
    protected void addWarning(SQLWarning warning) {
    	if (this.pendingWarnings == null) {
			this.pendingWarnings = warning;
    	} else {
    		// find the 'end'... this could be handled more
    		// efficiently, but the only thing that currently
    		// sets 'client-side' warnings is prepared statements
    		// when clipping +/- INF and NaN.
    		
    		SQLWarning lastWarning = this.pendingWarnings;
    		
    		while (lastWarning.getNextWarning() != null) {
    			lastWarning = lastWarning.getNextWarning();
    		}
    		
    		lastWarning.setNextWarning(warning);
    	}
    	
    }

    /**
     * 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;
    }
}

⌨️ 快捷键说明

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