Package net.sourceforge.jtds.jdbc
Class JtdsStatement
java.lang.Object
net.sourceforge.jtds.jdbc.JtdsStatement
- All Implemented Interfaces:
AutoCloseable,Statement,Wrapper
- Direct Known Subclasses:
JtdsPreparedStatement
jTDS implementation of the java.sql.Statement interface.
NB. As allowed by the JDBC standard and like most other drivers, this implementation only allows one open result set at a time.
Implementation notes:
I experimented with allowing multiple open result sets as supported by the original jTDS but rejected this approach for the following reasons:
- It is more difficult to ensure that there are no memory leaks and that cursors are closed if multiple open sets are allowed.
- The use of one result set allows cursor and non cursor result sets to be derived from exeuteQuery() or execute() and getResultSet() in the same way that other drivers do.
- Author:
- Mike Hutchinson, Holger Rehn
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AtomicInteger0 - this statement is open 1 - this statement is currently being closed 2 - this statement is closedprotected ArrayListBatched SQL Statement array.(package private) static final int(package private) static final int(package private) static final intprotected ColInfo[]The cached column meta data.protected JtdsConnectionThe connection owning this statement object.protected JtdsResultSetThe currentResultSet.protected StringThe cursor name to be used for positioned updates.(package private) static final int(package private) static final intprotected booleanTrue if SQL statements should be preprocessed.(package private) static final Integerprotected intThe fetch direction for result sets.protected intThe fetch size (default 100, only used by cursorResultSets).(package private) static final StringColumn name to be used for retrieving generated keys from the server: "_JTDS_GENE_RATED_KEYS_"protected CachedResultSetDummy result set for getGeneratedKeys.(package private) static final intprotected intThe maximum field size (not used at present).protected intThe maximum number of rows to return (not used at present).protected final SQLDiagnosticSQL Diagnostic exceptions and warnings.(package private) static final intprotected ArrayListList of open result sets.protected intThe read query timeout in secondsprotected final LinkedListList of queued results (update counts, possibly followed by aResultSet).protected intThe concurrency of result sets created by this statement.protected intThe type of result sets created by this statement.(package private) static final int(package private) static final Integerprotected TdsCoreThe TDS object used for server access.private intThe current update count. -
Constructor Summary
ConstructorsConstructorDescriptionJtdsStatement(JtdsConnection connection, int resultSetType, int resultSetConcurrency) Construct a new Statement object. -
Method Summary
Modifier and TypeMethodDescriptionvoid(package private) voidAdd an SQLWarning object to the statement warnings list.protected voidCache as many results as possible (up to the firstResultSet).voidcancel()protected voidCheck that the exception is caused by the failure to open a cursor and not by a more serious SQL error.protected voidCheck that this statement is still open.voidvoidvoidclose()(package private) voidClose all result sets.(package private) voidClose current result set (if any).voidbooleanbooleanbooleanbooleanint[]Execute batch of SQL Statements.private booleanexecuteImpl(String sql, int autoGeneratedKeys, boolean update) Implements the common functionality for plain statementexecute(java.lang.String)and {#link #executeUpdate}: basic checks, cleaning up of previous results, setting up and executing the query and loading the first results.protected SQLExceptionexecuteMSBatch(int size, int executeSize, ArrayList counts) Execute the SQL batch on a MS server.executeQuery(String sql) protected booleanexecuteSQL(String sql, String spName, ParamInfo[] params, boolean update, boolean useCursor) Executes any type of SQL.protected ResultSetexecuteSQLQuery(String sql, String spName, ParamInfo[] params, boolean useCursor) Executes SQL to obtain a result set.protected SQLExceptionexecuteSybaseBatch(int size, int executeSize, ArrayList counts) Execute the SQL batch on a Sybase server.intexecuteUpdate(String sql) intexecuteUpdate(String sql, int autoGeneratedKeys) intexecuteUpdate(String sql, int[] columnIndexes) intexecuteUpdate(String sql, String[] columnNames) protected voidfinalize()Called when this object goes out of scope to close anyResultSetobject and this statement.(package private) intRetrieve the default fetch size for this statement.intintintint(package private) SQLDiagnosticGet the statement's warnings list.booleanbooleangetMoreResults(int current) intintintint(package private) TdsCoregetTds()Get the Statement's TDS object.intbooleanisClosed()booleanbooleanbooleanisWrapperFor(Class arg0) (package private) static voidnotImplemented(String method) Report that user tried to call a method which has not been implemented.private booleanprocessResults(boolean update) Queue up update counts intoresultQueueuntil the end of the response is reached or aResultSetis encountered.protected voidreset()Resets theStatement, by cleaning up all queued and unprocessed results.voidsetCursorName(String name) voidsetEscapeProcessing(boolean enable) voidsetFetchDirection(int direction) voidsetFetchSize(int rows) voidsetMaxFieldSize(int max) voidsetMaxRows(int max) voidsetPoolable(boolean poolable) voidsetQueryTimeout(int seconds) <T> Tprotected booleanDetermines whether a cursor should be used based on the requested result set type and concurrency, whether a cursor name has been set, theuseCursorsconnection property has been set, the first word in the SQL query is either SELECT or EXEC/EXECUTE and no generated keys are returned.Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.sql.Statement
enquoteIdentifier, enquoteLiteral, enquoteNCharLiteral, executeLargeBatch, executeLargeUpdate, executeLargeUpdate, executeLargeUpdate, executeLargeUpdate, getLargeMaxRows, getLargeUpdateCount, isSimpleIdentifier, setLargeMaxRows
-
Field Details
-
GENKEYCOL
Column name to be used for retrieving generated keys from the server: "_JTDS_GENE_RATED_KEYS_"- See Also:
-
RETURN_GENERATED_KEYS
static final int RETURN_GENERATED_KEYS- See Also:
-
NO_GENERATED_KEYS
static final int NO_GENERATED_KEYS- See Also:
-
CLOSE_CURRENT_RESULT
static final int CLOSE_CURRENT_RESULT- See Also:
-
KEEP_CURRENT_RESULT
static final int KEEP_CURRENT_RESULT- See Also:
-
CLOSE_ALL_RESULTS
static final int CLOSE_ALL_RESULTS- See Also:
-
BOOLEAN
static final int BOOLEAN- See Also:
-
DATALINK
static final int DATALINK- See Also:
-
SUCCESS_NO_INFO
-
EXECUTE_FAILED
-
DEFAULT_FETCH_SIZE
static final int DEFAULT_FETCH_SIZE- See Also:
-
connection
The connection owning this statement object. -
tds
The TDS object used for server access. -
queryTimeout
protected int queryTimeoutThe read query timeout in seconds -
currentResult
The currentResultSet. -
updateCount
private int updateCountThe current update count. -
fetchDirection
protected int fetchDirectionThe fetch direction for result sets. -
resultSetType
protected int resultSetTypeThe type of result sets created by this statement. -
resultSetConcurrency
protected int resultSetConcurrencyThe concurrency of result sets created by this statement. -
fetchSize
protected int fetchSizeThe fetch size (default 100, only used by cursorResultSets). -
cursorName
The cursor name to be used for positioned updates. -
maxFieldSize
protected int maxFieldSizeThe maximum field size (not used at present). -
maxRows
protected int maxRowsThe maximum number of rows to return (not used at present). -
escapeProcessing
protected boolean escapeProcessingTrue if SQL statements should be preprocessed. -
messages
SQL Diagnostic exceptions and warnings. -
batchValues
Batched SQL Statement array. -
genKeyResultSet
Dummy result set for getGeneratedKeys. -
resultQueue
List of queued results (update counts, possibly followed by aResultSet). -
openResultSets
List of open result sets. -
colMetaData
The cached column meta data. -
_Closed
0 - this statement is open 1 - this statement is currently being closed 2 - this statement is closed
-
-
Constructor Details
-
JtdsStatement
JtdsStatement(JtdsConnection connection, int resultSetType, int resultSetConcurrency) throws SQLException Construct a new Statement object.- Parameters:
connection- The parent connection.resultSetType- The result set type for example TYPE_FORWARD_ONLY.resultSetConcurrency- The concurrency for example CONCUR_READ_ONLY.- Throws:
SQLException
-
-
Method Details
-
finalize
Called when this object goes out of scope to close anyResultSetobject and this statement. -
getTds
TdsCore getTds()Get the Statement's TDS object.- Returns:
- The TDS support as a
TdsCore Object.
-
getMessages
SQLDiagnostic getMessages()Get the statement's warnings list.- Returns:
- The warnings list as a
SQLDiagnostic.
-
checkOpen
Check that this statement is still open.- Throws:
SQLException- if statement closed.
-
checkCursorException
Check that the exception is caused by the failure to open a cursor and not by a more serious SQL error.- Parameters:
e- the exception returned by the cursor class- Throws:
SQLException- if exception is not due to a cursor error
-
notImplemented
Report that user tried to call a method which has not been implemented.- Parameters:
method- The method name to report in the error message.- Throws:
SQLException
-
closeCurrentResultSet
Close current result set (if any).- Throws:
SQLException
-
closeAllResultSets
Close all result sets.- Throws:
SQLException
-
addWarning
Add an SQLWarning object to the statement warnings list.- Parameters:
w- The SQLWarning to add.
-
executeMSBatch
protected SQLException executeMSBatch(int size, int executeSize, ArrayList counts) throws SQLException Execute the SQL batch on a MS server.- Parameters:
size- the total size of the batchexecuteSize- the maximum number of statements to send in one requestcounts- the returned update counts- Returns:
- chained exceptions linked to a
SQLException - Throws:
SQLException- if a serious error occurs during execution
-
executeSybaseBatch
protected SQLException executeSybaseBatch(int size, int executeSize, ArrayList counts) throws SQLException Execute the SQL batch on a Sybase server. Sybase needs to have the SQL concatenated into one TDS language packet. This method will be overriden forPreparedStatements.- Parameters:
size- the total size of the batchexecuteSize- the maximum number of statements to send in one requestcounts- the returned update counts- Returns:
- chained exceptions linked to a
SQLException - Throws:
SQLException- if a serious error occurs during execution
-
executeSQLQuery
protected ResultSet executeSQLQuery(String sql, String spName, ParamInfo[] params, boolean useCursor) throws SQLException Executes SQL to obtain a result set.- Parameters:
sql- the SQL statement to executespName- optional stored procedure nameparams- optional parametersuseCursor- whether a cursor should be created for the SQL- Returns:
- the result set generated by the query
- Throws:
SQLException
-
executeSQL
protected boolean executeSQL(String sql, String spName, ParamInfo[] params, boolean update, boolean useCursor) throws SQLException Executes any type of SQL.- Parameters:
sql- the SQL statement to executespName- optional stored procedure nameparams- optional parametersupdate- whether the caller isexecuteUpdate(java.lang.String)useCursor- whether the requested result set type or concurrency or connection properties request usage of a cursor- Returns:
trueif the first result is a result set- Throws:
SQLException- if an error condition occurs
-
processResults
Queue up update counts intoresultQueueuntil the end of the response is reached or aResultSetis encountered. CallingprocessResultswhile aResultSetis open will not close it, but will consume all remaining rows.- Parameters:
update-trueif the method is called from withinexecuteUpdate- Returns:
trueif there are any results,falseotherwise- Throws:
SQLException- if an error condition occurs
-
cacheResults
Cache as many results as possible (up to the firstResultSet). Called byResultSets when the end is reached.- Throws:
SQLException
-
reset
Resets theStatement, by cleaning up all queued and unprocessed results. Called by all execute methods andclose()- Throws:
SQLException- if an error occurs
-
executeImpl
Implements the common functionality for plain statementexecute(java.lang.String)and {#link #executeUpdate}: basic checks, cleaning up of previous results, setting up and executing the query and loading the first results.- Parameters:
sql- an SQLINSERT,UPDATEorDELETEstatement or an SQL statement that returns nothing, such as an SQL DDL statementautoGeneratedKeys- a flag indicating whether auto-generated keys should be made available for retrievalupdate- boolean flag indicating whether the caller isexecuteUpdate(java.lang.String)-- in this case an exception is thrown if the first result is not an update count and no cursor is created (direct execution)- Returns:
trueif the first result is aResultSet,falseif it's an update count- Throws:
SQLException- See Also:
-
useCursor
Determines whether a cursor should be used based on the requested result set type and concurrency, whether a cursor name has been set, theuseCursorsconnection property has been set, the first word in the SQL query is either SELECT or EXEC/EXECUTE and no generated keys are returned.- Parameters:
returnKeys- indicates whether keys will be returned by the querysqlWord- the first word in the SQL query; can benullif the caller isexecuteQuery(java.lang.String)- Returns:
trueif a cursor should be used,falseif not
-
getDefaultFetchSize
int getDefaultFetchSize()Retrieve the default fetch size for this statement.- Returns:
- the default fetch size for a new
ResultSet
-
getFetchDirection
- Specified by:
getFetchDirectionin interfaceStatement- Throws:
SQLException
-
getFetchSize
- Specified by:
getFetchSizein interfaceStatement- Throws:
SQLException
-
getMaxFieldSize
- Specified by:
getMaxFieldSizein interfaceStatement- Throws:
SQLException
-
getMaxRows
- Specified by:
getMaxRowsin interfaceStatement- Throws:
SQLException
-
getQueryTimeout
- Specified by:
getQueryTimeoutin interfaceStatement- Throws:
SQLException
-
getResultSetConcurrency
- Specified by:
getResultSetConcurrencyin interfaceStatement- Throws:
SQLException
-
getResultSetHoldability
- Specified by:
getResultSetHoldabilityin interfaceStatement- Throws:
SQLException
-
getResultSetType
- Specified by:
getResultSetTypein interfaceStatement- Throws:
SQLException
-
getUpdateCount
- Specified by:
getUpdateCountin interfaceStatement- Throws:
SQLException
-
cancel
- Specified by:
cancelin interfaceStatement- Throws:
SQLException
-
clearBatch
- Specified by:
clearBatchin interfaceStatement- Throws:
SQLException
-
clearWarnings
- Specified by:
clearWarningsin interfaceStatement- Throws:
SQLException
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceStatement- Throws:
SQLException
-
getMoreResults
- Specified by:
getMoreResultsin interfaceStatement- Throws:
SQLException
-
executeBatch
Execute batch of SQL Statements. The JDBC3 standard says that the behavior of this method must be consistent for any DBMS. As Sybase (and to a lesser extent SQL Server) will sometimes continue after a batch execution error, the only way to comply with the standard is to always return an array of update counts the same size as the batch list. Slots in the array beyond the last executed statement are set toEXECUTE_FAILED. There is a problem with certain statements, returning more update counts than there are batch operations. (see bug [])- Specified by:
executeBatchin interfaceStatement- Returns:
- update counts as an
int[] - Throws:
SQLExceptionBatchUpdateException
-
setFetchDirection
- Specified by:
setFetchDirectionin interfaceStatement- Throws:
SQLException
-
setFetchSize
- Specified by:
setFetchSizein interfaceStatement- Throws:
SQLException
-
setMaxFieldSize
- Specified by:
setMaxFieldSizein interfaceStatement- Throws:
SQLException
-
setMaxRows
- Specified by:
setMaxRowsin interfaceStatement- Throws:
SQLException
-
setQueryTimeout
- Specified by:
setQueryTimeoutin interfaceStatement- Throws:
SQLException
-
getMoreResults
- Specified by:
getMoreResultsin interfaceStatement- Throws:
SQLException
-
setEscapeProcessing
- Specified by:
setEscapeProcessingin interfaceStatement- Throws:
SQLException
-
executeUpdate
- Specified by:
executeUpdatein interfaceStatement- Throws:
SQLException
-
addBatch
- Specified by:
addBatchin interfaceStatement- Throws:
SQLException
-
setCursorName
- Specified by:
setCursorNamein interfaceStatement- Throws:
SQLException
-
execute
- Specified by:
executein interfaceStatement- Throws:
SQLException
-
executeUpdate
- Specified by:
executeUpdatein interfaceStatement- Throws:
SQLException
-
execute
- Specified by:
executein interfaceStatement- Throws:
SQLException
-
executeUpdate
- Specified by:
executeUpdatein interfaceStatement- Throws:
SQLException
-
execute
- Specified by:
executein interfaceStatement- Throws:
SQLException
-
getConnection
- Specified by:
getConnectionin interfaceStatement- Throws:
SQLException
-
getGeneratedKeys
- Specified by:
getGeneratedKeysin interfaceStatement- Throws:
SQLException
-
getResultSet
- Specified by:
getResultSetin interfaceStatement- Throws:
SQLException
-
getWarnings
- Specified by:
getWarningsin interfaceStatement- Throws:
SQLException
-
executeUpdate
- Specified by:
executeUpdatein interfaceStatement- Throws:
SQLException
-
execute
- Specified by:
executein interfaceStatement- Throws:
SQLException
-
executeQuery
- Specified by:
executeQueryin interfaceStatement- Throws:
SQLException
-
isClosed
- Specified by:
isClosedin interfaceStatement- Returns:
- whether this
JtdsStatementhas been closed - Throws:
SQLException
-
isPoolable
- Specified by:
isPoolablein interfaceStatement- Throws:
SQLException
-
setPoolable
- Specified by:
setPoolablein interfaceStatement- Throws:
SQLException
-
isWrapperFor
- Specified by:
isWrapperForin interfaceWrapper- Throws:
SQLException
-
unwrap
- Specified by:
unwrapin interfaceWrapper- Throws:
SQLException
-
closeOnCompletion
- Specified by:
closeOnCompletionin interfaceStatement- Throws:
SQLException
-
isCloseOnCompletion
- Specified by:
isCloseOnCompletionin interfaceStatement- Throws:
SQLException
-