📄 jdbcstatement.java
字号:
/* Copyright (c) 2001-2005, The HSQL Development Group
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the HSQL Development Group nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hsqldb.jdbc;
//#ifdef JAVA2
import java.sql.BatchUpdateException;
//#endif JAVA2
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import org.hsqldb.HsqlException;
import org.hsqldb.Result;
import org.hsqldb.ResultConstants;
import org.hsqldb.Trace;
import org.hsqldb.Types;
// fredt@users 20020320 - patch 1.7.0 - JDBC 2 support and error trapping
// JDBC 2 methods can now be called from jdk 1.1.x - see javadoc comments
// SCROLL_INSENSITIVE and FORWARD_ONLY types for ResultSet are now supported
// boucherb@users 20020509 - added "throws SQLException" to all methods where
// it was missing here but specified in the java.sql.Statement interface,
// updated generic documentation to JDK 1.4, and added JDBC3 methods and docs
// boucherb@users and fredt@users - 20020505 extensive review and update
// of docs and behaviour to comply with java.sql specification
// fredt@users 20030620 - patch 1.7.2 - rewritten and simplified
// boucherb@users 200404xx - javadoc updates toward 1.7.2 final
/**
* <!-- start generic documentation -->
* The object used for executing a static SQL statement
* and returning the results it produces.
* <P>
* By default, only one <code>ResultSet</code> object per <code>Statement</code>
* object can be open at the same time. Therefore, if the reading of one
* <code>ResultSet</code> object is interleaved
* with the reading of another, each must have been generated by
* different <code>Statement</code> objects. All execution methods in the
* <code>Statement</code> interface implicitly close a statment's current
* <code>ResultSet</code> object if an open one exists.<p>
* <!-- end generic documentation-->
*
* <!-- start release-specific documentation -->
* <div class="ReleaseSpecificDocumentation">
* <h3>HSQLDB-Specific Information:</h3><p>
*
* <b>JRE 1.1.x Notes:</b> <p>
*
* In general, JDBC 2 support requires Java 1.2 and above, and JDBC3 requires
* Java 1.4 and above. In HSQLDB, support for methods introduced in different
* versions of JDBC depends on the JDK version used for compiling and building
* HSQLDB.<p>
*
* Since 1.7.0, all JDBC 2 methods can be called while executing under the
* version 1.1.x
* <em>Java Runtime Environment</em><sup><font size="-2">TM</font></sup>.
* However, in addition to this technique requiring explicit casts to the
* org.hsqldb.jdbcXXX classes, some of these method calls require
* <code>int</code> values that are defined only in the JDBC 2 or greater
* version of the
* <a href="http://java.sun.com/j2se/1.4/docs/api/java/sql/ResultSet.html">
* <code>ResultSet</code></a> interface. For this reason these values are
* defined in {@link jdbcResultSet jdbcResultSet}.<p>
*
* In a JRE 1.1.x environment, calling JDBC 2 methods that take or return the
* JDBC2-only <code>ResultSet</code> values can be achieved by referring
* to them in parameter specifications and return value comparisons,
* respectively, as follows: <p>
*
* <pre class="JavaCodeExample">
* jdbcResultSet.FETCH_FORWARD
* jdbcResultSet.TYPE_FORWARD_ONLY
* jdbcResultSet.TYPE_SCROLL_INSENSITIVE
* jdbcResultSet.CONCUR_READ_ONLY
* //etc.
* </pre> <p>
*
* However, please note that code written to use HSQLDB JDBC 2 features under
* JDK 1.1.x will not be compatible for use with other JDBC 2 drivers. Please
* also note that this feature is offered solely as a convenience to developers
* who must work under JDK 1.1.x due to operating constraints, yet wish to
* use some of the more advanced features available under the JDBC 2
* specification. <p>
*
* (fredt@users)<br>
* (boucherb@users)<p>
*
* </div>
* <!-- end release-specific documentation -->
*
* @author boucherb@users
* @author fredt@user
* @version 1.8.0
* @see jdbcConnection#createStatement
* @see jdbcResultSet
*/
public class jdbcStatement implements Statement {
/**
* Whether this Statement has been explicitly closed. A jdbcConnection
* object now explicitly closes all of its open jdbcXXXStatement objects
* when it is closed.
*/
volatile boolean isClosed;
/** Is escape processing enabled? */
private boolean isEscapeProcessing = true;
/** The connection used to execute this statement. */
protected jdbcConnection connection;
/** The maximum number of rows to generate when executing this statement. */
protected int maxRows;
/** The result of executing this statement. */
protected Result resultIn;
/** The result set type obtained by executing this statement. */
protected int rsType = jdbcResultSet.TYPE_FORWARD_ONLY;
/** Used by this statement to communicate non-batched requests. */
protected Result resultOut = new Result(ResultConstants.SQLEXECDIRECT);
/** Use by this statement to communicate batched execution requests */
protected Result batchResultOut = null;
// boucherb@users
// NOTE:
// This method is synchronized since resultIn is an instance attribute
// and thus it is theoretically possible that a race condition occurs
// in which a different thread executes fetchResult(sql), replacing
// resultIn before it gets assigned propery to the new result set.
// fredt - this class is not supposed to be called multi-threaded -
// For example, if two threads call execute() then both call getResult() in
// the wrong order, the ResultSet object for one call could actually belong
// to the other call.
/**
* <!-- start generic documentation -->
* Executes the given SQL statement, which returns a single
* <code>ResultSet</code> object. <p>
* <!-- end generic documentation -->
* <!-- start release-specific documentation -->
* <div class="ReleaseSpecificDocumentation">
* <h3>HSQLDB-Specific Information:</h3> <p>
*
* This method should not be used for statements other than SELECT queries.<p>
*
* Including 1.7.2, HSQLDB does not throw an exception when the statement
* is a DDL statement or an UPDATE or DELETE statement. This will certainly
* change in future version.
* </div>
* <!-- end release-specific documentation -->
*
* @param sql an SQL statement to be sent to the database, typically a
* static SQL <code>SELECT</code> statement
* @return a <code>ResultSet</code> object that contains the data produced
* by the given query; never <code>null</code>
* @exception SQLException if a database access error occurs or the given
* SQL statement produces anything other than a single
* <code>ResultSet</code> object
*/
public ResultSet executeQuery(String sql) throws SQLException {
checkClosed();
connection.clearWarningsNoCheck();
fetchResult(sql);
return new jdbcResultSet(this, resultIn, connection.connProperties,
connection.isNetConn);
}
/**
* <!-- start generic documentation -->
* Executes the given SQL statement, which may be an <code>INSERT</code>,
* <code>UPDATE</code>, or <code>DELETE</code> statement or an
* SQL statement that returns nothing, such as an SQL DDL statement. <p>
* <!-- end generic documentation -->
*
* @param sql an SQL <code>INSERT</code>, <code>UPDATE</code> or
* <code>DELETE</code> statement or an SQL statement that returns nothing
* @return either the row count for <code>INSERT</code>, <code>UPDATE</code>
* or <code>DELETE</code> statements, or <code>0</code> for SQL statements
* that return nothing
* @exception SQLException if a database access error occurs or the given
* SQL statement produces a <code>ResultSet</code> object
*/
public int executeUpdate(String sql) throws SQLException {
checkClosed();
connection.clearWarningsNoCheck();
fetchResult(sql);
if (resultIn == null || resultIn.mode == ResultConstants.DATA) {
/**
* @todo: - fredt@users - check for type of statement _must_ be done
* in the engine and error returned _without_ executing
*/
throw new SQLException(
Trace.getMessage(Trace.jdbcStatement_executeUpdate));
} else if (resultIn.mode == ResultConstants.ERROR) {
Util.throwError(resultIn);
}
return resultIn.getUpdateCount();
}
/**
* <!-- start generic documentation -->
* Releases this <code>Statement</code> object's database
* and JDBC resources immediately instead of waiting for
* this to happen when it is automatically closed.
* It is generally good practice to release resources as soon as
* you are finished with them to avoid tying up database
* resources.
* <P>
* Calling the method <code>close</code> on a <code>Statement</code>
* object that is already closed has no effect.
* <P>
* <B>Note:</B> A <code>Statement</code> object is automatically closed
* when it is garbage collected. When a <code>Statement</code> object is
* closed, its current <code>ResultSet</code> object, if one exists, is
* also closed. <p>
* <!-- end generic documentation -->
*
* @exception SQLException if a database access error occurs
*/
public synchronized void close() throws SQLException {
if (isClosed) {
return;
}
batchResultOut = null;
connection = null;
resultIn = null;
resultOut = null;
isClosed = true;
}
//----------------------------------------------------------------------
/**
* <!-- start generic documentation -->
* Retrieves the maximum number of bytes that can be
* returned for character and binary column values in a <code>ResultSet</code>
* object produced by this <code>Statement</code> object.
* This limit applies only to <code>BINARY</code>,
* <code>VARBINARY</code>, <code>LONGVARBINARY</code>, <code>CHAR</code>,
* <code>VARCHAR</code>, and <code>LONGVARCHAR</code>
* columns. If the limit is exceeded, the excess data is silently
* discarded. <p>
* <!-- end generic documentation -->
*
* <!-- start release-specific documentation -->
* <div class="ReleaseSpecificDocumentation">
* <h3>HSQLDB-Specific Information:</h3> <p>
*
* Including 1.7.2, HSQLDB always returns zero, meaning there
* is no limit.
* </div>
* <!-- end release-specific documentation -->
*
* @return the current column size limit for columns storing character and
* binary values; zero means there is no limit
* @exception SQLException if a database access error occurs
* @see #setMaxFieldSize
*/
public int getMaxFieldSize() throws SQLException {
checkClosed();
return 0;
}
/**
* <!-- start generic documentation -->
* Sets the limit for the maximum number of bytes in a <code>ResultSet</code>
* column storing character or binary values to
* the given number of bytes. This limit applies
* only to <code>BINARY</code>, <code>VARBINARY</code>,
* <code>LONGVARBINARY</code>, <code>CHAR</code>, <code>VARCHAR</code>, and
* <code>LONGVARCHAR</code> fields. If the limit is exceeded, the excess data
* is silently discarded. For maximum portability, use values
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -