📄 embedstatement.java
字号:
/* Derby - Class org.apache.derby.impl.jdbc.EmbedStatement Copyright 1997, 2004 The Apache Software Foundation or its licensors, as applicable. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package org.apache.derby.impl.jdbc;import org.apache.derby.iapi.reference.JDBC20Translation;import org.apache.derby.iapi.reference.JDBC30Translation;import org.apache.derby.iapi.reference.SQLState;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.sql.Activation;import org.apache.derby.iapi.sql.PreparedStatement;import org.apache.derby.iapi.sql.ResultSet;import org.apache.derby.iapi.sql.ParameterValueSet;import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;import org.apache.derby.iapi.error.StandardException;import org.apache.derby.iapi.jdbc.EngineStatement;import java.sql.SQLException;import java.sql.SQLWarning;import java.util.Vector;/* We would import these, but have name-overlapimport java.sql.Statement;import java.sql.ResultSet;*//** * * EmbedStatement is a local JDBC statement. * <P><B>Supports</B> <UL> <LI> JSR169 - no subsetting for java.sql.Statement <LI> JDBC 2.0 <LI> JDBC 3.0 - no new dependencies on new JDBC 3.0 or JDK 1.4 classes, new methods can safely be added into implementation. </UL> * @author ames */public class EmbedStatement extends ConnectionChild implements EngineStatement { private final java.sql.Connection applicationConnection; /** * Statement reference the application is using to execute * this Statement. Normally set to this, but if this was * created by a Connection from an XAConnection then this * will be a reference to the BrokeredStatement. */ private EngineStatement applicationStatement; int updateCount = -1; java.sql.ResultSet results; //for jdbc3.0 feature, where you can get a resultset of rows inserted //for auto generated columns after an insert private java.sql.ResultSet autoGeneratedKeysResultSet; private String cursorName; private final boolean forMetaData; final int resultSetType; private final int resultSetConcurrency; private final int resultSetHoldability; final LanguageConnectionContext lcc; private SQLWarning warnings; String SQLText; private int fetchSize = 1; private int fetchDirection = JDBC20Translation.FETCH_FORWARD; int MaxFieldSize; //the state of this statement, set to false when close() is called private boolean active = true; //in case of batch update, save the individual statements in the batch in this vector //this is only used by JDBC 2.0 Vector batchStatements; // The maximum # of rows to return per result set. // (0 means no limit.) int maxRows; private ParameterValueSet pvs; // // constructor // public EmbedStatement (EmbedConnection connection, boolean forMetaData, int resultSetType, int resultSetConcurrency, int resultSetHoldability) { super(connection); this.forMetaData = forMetaData; this.resultSetType = resultSetType; this.resultSetConcurrency = resultSetConcurrency; this.resultSetHoldability = resultSetHoldability; lcc = getEmbedConnection().getLanguageConnection(); applicationConnection = getEmbedConnection().getApplicationConnection(); applicationStatement = this; } // // java.sql.Statement interface // the comments are those from the JDBC interface, // so we know what we're supposed to to. /** * Execute a SQL statement that returns a single ResultSet. * * @param sql typically this is a static SQL SELECT statement * @return a ResultSet that contains the data produced by the * query; never null * @exception SQLException thrown on failure. */ public java.sql.ResultSet executeQuery(String sql) throws SQLException { execute(sql, true, false, JDBC30Translation.NO_GENERATED_KEYS, null, null); if (SanityManager.DEBUG) { if (results == null) SanityManager.THROWASSERT("no results returned on executeQuery()"); } return results; } /** * Execute a SQL INSERT, UPDATE or DELETE statement. In addition, * SQL statements that return nothing such as SQL DDL statements * can be executed. * * @param sql a SQL INSERT, UPDATE or DELETE statement or a SQL * statement that returns nothing * @return either the row count for INSERT, UPDATE or DELETE; or 0 * for SQL statements that return nothing * @exception SQLException thrown on failure. */ public int executeUpdate(String sql) throws SQLException { execute(sql, false, true, JDBC30Translation.NO_GENERATED_KEYS, null, null); return updateCount; } /** * JDBC 3.0 * * Execute the given SQL statement and signals the driver with the given flag * about whether the auto-generated keys produced by this Statement object * should be made available for retrieval. * * @param sql a SQL INSERT, UPDATE or DELETE statement or a SQL * statement that returns nothing * @param autoGeneratedKeys - a flag indicating whether auto-generated keys * should be made available for retrieval; one of the following constants: * Statement.RETURN_GENERATED_KEYS Statement.NO_GENERATED_KEYS * @return either the row count for INSERT, UPDATE or DELETE; or 0 * for SQL statements that return nothing * @exception SQLException if a database access error occurs */ public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { execute(sql, false, true, autoGeneratedKeys, null, null); return updateCount; } /** * JDBC 3.0 * * Executes the given SQL statement and signals the driver that the * auto-generated keys indicated in the given array should be made * available for retrieval. The driver will ignore the array if the SQL * statement is not an INSERT statement * * @param sql a SQL INSERT, UPDATE or DELETE statement or a SQL * statement that returns nothing * @param columnIndexes - an array of column indexes indicating the * columns that should be returned from the inserted row * @return either the row count for INSERT, UPDATE or DELETE; or 0 * for SQL statements that return nothing * @exception SQLException if a database access error occurs */ public int executeUpdate(String sql, int[] columnIndexes) throws SQLException { throw Util.notImplemented("executeUpdate(String, int[])"); } /** * JDBC 3.0 * * Executes the given SQL statement and signals the driver that the * auto-generated keys indicated in the given array should be made * available for retrieval. The driver will ignore the array if the SQL * statement is not an INSERT statement * * @param sql a SQL INSERT, UPDATE or DELETE statement or a SQL * statement that returns nothing * @param columnNames - an array of the names of the columns * that should be returned from the inserted row * @return either the row count for INSERT, UPDATE or DELETE; or 0 * for SQL statements that return nothing * @exception SQLException if a database access error occurs */ public int executeUpdate(String sql, String[] columnNames) throws SQLException { throw Util.notImplemented("executeUpdate(String, String[])"); } final void checkIfInMiddleOfBatch() throws SQLException { /* If batchStatements is not null then we are in the middle * of a batch. That's an invalid state. We need to finish the * batch either by clearing the batch or executing the batch. * executeUpdate is not allowed inside the batch. */ if (batchStatements != null) throw newSQLException(SQLState.MIDDLE_OF_BATCH); } /** * In many cases, it is desirable to immediately release a * Statements's database and JDBC resources instead of waiting for * this to happen when it is automatically closed; the close * method provides this immediate release. * * <P><B>Note:</B> A Statement is automatically closed when it is * garbage collected. When a Statement is closed its current * ResultSet, if one exists, is also closed. * @exception SQLException thrown on failure. */ public final void close() throws SQLException { /* The close() method is the only method * that is allowed to be called on a closed * Statement, as per Jon Ellis. */ if (!active) { return; } synchronized (getConnectionSynchronization()) { closeActions(); //we first set the status active = false; //first, clear the resutl set clearResultSets(); //next, release other resource cursorName = null; warnings = null; SQLText = null; batchStatements = null; } } // allow sub-classes to execute additional close // logic while holding the synchronization. protected void closeActions() throws SQLException { } //---------------------------------------------------------------------- /** * 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 thrown on failure. */ public int getMaxFieldSize() throws SQLException { checkStatus(); return MaxFieldSize; } /** * The maxFieldSize limit (in bytes) is set to limit the size of * data that can be returned for any column value; it only applies * to BINARY, VARBINARY, LONGVARBINARY, CHAR, VARCHAR, and * LONGVARCHAR fields. If the limit is exceeded, the excess data * is silently discarded. * * @param max the new max column size limit; zero means unlimited * @exception SQLException thrown on failure. */ public void setMaxFieldSize(int max) throws SQLException { checkStatus(); if (max < 0) { throw newSQLException(SQLState.INVALID_MAXFIELD_SIZE, new Integer(max)); } this.MaxFieldSize = max; } /** * The maxRows limit is the maximum number of rows that a * ResultSet can contain. If the limit is exceeded, the excess * rows are silently dropped. * * @return the current max row limit; zero means unlimited * @exception SQLException thrown on failure. */ public int getMaxRows() throws SQLException { checkStatus(); return maxRows; } /** * 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. * * @param max the new max rows limit; zero means unlimited * @exception SQLException thrown on failure. */ public void setMaxRows(int max) throws SQLException { checkStatus(); if (max < 0) { throw newSQLException(SQLState.INVALID_MAX_ROWS_VALUE, new Integer(max)); } this.maxRows = max; } /** * If escape scanning is on (the default) the driver will do * escape substitution before sending the SQL to the database. * * @param enable true to enable; false to disable * @exception SQLException thrown on failure. */ public void setEscapeProcessing(boolean enable) throws SQLException { checkStatus(); // Nothing to do in our server , just ignore it. } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -