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 AtomicInteger
0 - this statement is open 1 - this statement is currently being closed 2 - this statement is closedprotected ArrayList
Batched SQL Statement array.(package private) static final int
(package private) static final int
(package private) static final int
protected ColInfo[]
The cached column meta data.protected JtdsConnection
The connection owning this statement object.protected JtdsResultSet
The currentResultSet
.protected String
The cursor name to be used for positioned updates.(package private) static final int
(package private) static final int
protected boolean
True if SQL statements should be preprocessed.(package private) static final Integer
protected int
The fetch direction for result sets.protected int
The fetch size (default 100, only used by cursorResultSet
s).(package private) static final String
Column name to be used for retrieving generated keys from the server: "_JTDS_GENE_RATED_KEYS_"protected CachedResultSet
Dummy result set for getGeneratedKeys.(package private) static final int
protected int
The maximum field size (not used at present).protected int
The maximum number of rows to return (not used at present).protected final SQLDiagnostic
SQL Diagnostic exceptions and warnings.(package private) static final int
protected ArrayList
List of open result sets.protected int
The read query timeout in secondsprotected final LinkedList
List of queued results (update counts, possibly followed by aResultSet
).protected int
The concurrency of result sets created by this statement.protected int
The type of result sets created by this statement.(package private) static final int
(package private) static final Integer
protected TdsCore
The TDS object used for server access.private int
The current update count. -
Constructor Summary
ConstructorsConstructorDescriptionJtdsStatement
(JtdsConnection connection, int resultSetType, int resultSetConcurrency) Construct a new Statement object. -
Method Summary
Modifier and TypeMethodDescriptionvoid
(package private) void
Add an SQLWarning object to the statement warnings list.protected void
Cache as many results as possible (up to the firstResultSet
).void
cancel()
protected void
Check that the exception is caused by the failure to open a cursor and not by a more serious SQL error.protected void
Check that this statement is still open.void
void
void
close()
(package private) void
Close all result sets.(package private) void
Close current result set (if any).void
boolean
boolean
boolean
boolean
int[]
Execute batch of SQL Statements.private boolean
executeImpl
(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 SQLException
executeMSBatch
(int size, int executeSize, ArrayList counts) Execute the SQL batch on a MS server.executeQuery
(String sql) protected boolean
executeSQL
(String sql, String spName, ParamInfo[] params, boolean update, boolean useCursor) Executes any type of SQL.protected ResultSet
executeSQLQuery
(String sql, String spName, ParamInfo[] params, boolean useCursor) Executes SQL to obtain a result set.protected SQLException
executeSybaseBatch
(int size, int executeSize, ArrayList counts) Execute the SQL batch on a Sybase server.int
executeUpdate
(String sql) int
executeUpdate
(String sql, int autoGeneratedKeys) int
executeUpdate
(String sql, int[] columnIndexes) int
executeUpdate
(String sql, String[] columnNames) protected void
finalize()
Called when this object goes out of scope to close anyResultSet
object and this statement.(package private) int
Retrieve the default fetch size for this statement.int
int
int
int
(package private) SQLDiagnostic
Get the statement's warnings list.boolean
boolean
getMoreResults
(int current) int
int
int
int
(package private) TdsCore
getTds()
Get the Statement's TDS object.int
boolean
isClosed()
boolean
boolean
boolean
isWrapperFor
(Class arg0) (package private) static void
notImplemented
(String method) Report that user tried to call a method which has not been implemented.private boolean
processResults
(boolean update) Queue up update counts intoresultQueue
until the end of the response is reached or aResultSet
is encountered.protected void
reset()
Resets theStatement
, by cleaning up all queued and unprocessed results.void
setCursorName
(String name) void
setEscapeProcessing
(boolean enable) void
setFetchDirection
(int direction) void
setFetchSize
(int rows) void
setMaxFieldSize
(int max) void
setMaxRows
(int max) void
setPoolable
(boolean poolable) void
setQueryTimeout
(int seconds) <T> T
protected boolean
Determines whether a cursor should be used based on the requested result set type and concurrency, whether a cursor name has been set, theuseCursors
connection 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, wait
Methods 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 cursorResultSet
s). -
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 anyResultSet
object 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:
true
if the first result is a result set- Throws:
SQLException
- if an error condition occurs
-
processResults
Queue up update counts intoresultQueue
until the end of the response is reached or aResultSet
is encountered. CallingprocessResults
while aResultSet
is open will not close it, but will consume all remaining rows.- Parameters:
update
-true
if the method is called from withinexecuteUpdate
- Returns:
true
if there are any results,false
otherwise- Throws:
SQLException
- if an error condition occurs
-
cacheResults
Cache as many results as possible (up to the firstResultSet
). Called byResultSet
s 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
,UPDATE
orDELETE
statement 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:
true
if the first result is aResultSet
,false
if 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, theuseCursors
connection 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 benull
if the caller isexecuteQuery(java.lang.String)
- Returns:
true
if a cursor should be used,false
if not
-
getDefaultFetchSize
int getDefaultFetchSize()Retrieve the default fetch size for this statement.- Returns:
- the default fetch size for a new
ResultSet
-
getFetchDirection
- Specified by:
getFetchDirection
in interfaceStatement
- Throws:
SQLException
-
getFetchSize
- Specified by:
getFetchSize
in interfaceStatement
- Throws:
SQLException
-
getMaxFieldSize
- Specified by:
getMaxFieldSize
in interfaceStatement
- Throws:
SQLException
-
getMaxRows
- Specified by:
getMaxRows
in interfaceStatement
- Throws:
SQLException
-
getQueryTimeout
- Specified by:
getQueryTimeout
in interfaceStatement
- Throws:
SQLException
-
getResultSetConcurrency
- Specified by:
getResultSetConcurrency
in interfaceStatement
- Throws:
SQLException
-
getResultSetHoldability
- Specified by:
getResultSetHoldability
in interfaceStatement
- Throws:
SQLException
-
getResultSetType
- Specified by:
getResultSetType
in interfaceStatement
- Throws:
SQLException
-
getUpdateCount
- Specified by:
getUpdateCount
in interfaceStatement
- Throws:
SQLException
-
cancel
- Specified by:
cancel
in interfaceStatement
- Throws:
SQLException
-
clearBatch
- Specified by:
clearBatch
in interfaceStatement
- Throws:
SQLException
-
clearWarnings
- Specified by:
clearWarnings
in interfaceStatement
- Throws:
SQLException
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceStatement
- Throws:
SQLException
-
getMoreResults
- Specified by:
getMoreResults
in 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:
executeBatch
in interfaceStatement
- Returns:
- update counts as an
int[]
- Throws:
SQLException
BatchUpdateException
-
setFetchDirection
- Specified by:
setFetchDirection
in interfaceStatement
- Throws:
SQLException
-
setFetchSize
- Specified by:
setFetchSize
in interfaceStatement
- Throws:
SQLException
-
setMaxFieldSize
- Specified by:
setMaxFieldSize
in interfaceStatement
- Throws:
SQLException
-
setMaxRows
- Specified by:
setMaxRows
in interfaceStatement
- Throws:
SQLException
-
setQueryTimeout
- Specified by:
setQueryTimeout
in interfaceStatement
- Throws:
SQLException
-
getMoreResults
- Specified by:
getMoreResults
in interfaceStatement
- Throws:
SQLException
-
setEscapeProcessing
- Specified by:
setEscapeProcessing
in interfaceStatement
- Throws:
SQLException
-
executeUpdate
- Specified by:
executeUpdate
in interfaceStatement
- Throws:
SQLException
-
addBatch
- Specified by:
addBatch
in interfaceStatement
- Throws:
SQLException
-
setCursorName
- Specified by:
setCursorName
in interfaceStatement
- Throws:
SQLException
-
execute
- Specified by:
execute
in interfaceStatement
- Throws:
SQLException
-
executeUpdate
- Specified by:
executeUpdate
in interfaceStatement
- Throws:
SQLException
-
execute
- Specified by:
execute
in interfaceStatement
- Throws:
SQLException
-
executeUpdate
- Specified by:
executeUpdate
in interfaceStatement
- Throws:
SQLException
-
execute
- Specified by:
execute
in interfaceStatement
- Throws:
SQLException
-
getConnection
- Specified by:
getConnection
in interfaceStatement
- Throws:
SQLException
-
getGeneratedKeys
- Specified by:
getGeneratedKeys
in interfaceStatement
- Throws:
SQLException
-
getResultSet
- Specified by:
getResultSet
in interfaceStatement
- Throws:
SQLException
-
getWarnings
- Specified by:
getWarnings
in interfaceStatement
- Throws:
SQLException
-
executeUpdate
- Specified by:
executeUpdate
in interfaceStatement
- Throws:
SQLException
-
execute
- Specified by:
execute
in interfaceStatement
- Throws:
SQLException
-
executeQuery
- Specified by:
executeQuery
in interfaceStatement
- Throws:
SQLException
-
isClosed
- Specified by:
isClosed
in interfaceStatement
- Returns:
- whether this
JtdsStatement
has been closed - Throws:
SQLException
-
isPoolable
- Specified by:
isPoolable
in interfaceStatement
- Throws:
SQLException
-
setPoolable
- Specified by:
setPoolable
in interfaceStatement
- Throws:
SQLException
-
isWrapperFor
- Specified by:
isWrapperFor
in interfaceWrapper
- Throws:
SQLException
-
unwrap
- Specified by:
unwrap
in interfaceWrapper
- Throws:
SQLException
-
closeOnCompletion
- Specified by:
closeOnCompletion
in interfaceStatement
- Throws:
SQLException
-
isCloseOnCompletion
- Specified by:
isCloseOnCompletion
in interfaceStatement
- Throws:
SQLException
-