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 origianal 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.
In the event of an IO failure the setClosed() method forces this statement
and associated result set to close preventing the propogation of errors.
This class includes a finalize method which increases the chances of the
statement being closed tidly in a pooled environment where the user has
forgotten to explicitly close the statement before it goes out of scope.
BOOLEAN
(package private) static final int BOOLEAN
CLOSE_ALL_RESULTS
(package private) static final int CLOSE_ALL_RESULTS
CLOSE_CURRENT_RESULT
(package private) static final int CLOSE_CURRENT_RESULT
DATALINK
(package private) static final int DATALINK
DEFAULT_FETCH_SIZE
(package private) static final int DEFAULT_FETCH_SIZE
EXECUTE_FAILED
(package private) static final Integer EXECUTE_FAILED
KEEP_CURRENT_RESULT
(package private) static final int KEEP_CURRENT_RESULT
NO_GENERATED_KEYS
(package private) static final int NO_GENERATED_KEYS
RETURN_GENERATED_KEYS
(package private) static final int RETURN_GENERATED_KEYS
SUCCESS_NO_INFO
(package private) static final Integer SUCCESS_NO_INFO
batchValues
protected ArrayList batchValues
Batched SQL Statement array.
closed
protected boolean closed
True if this statement is closed.
colMetaData
protected ColInfo[] colMetaData
The cached column meta data.
connection
protected ConnectionJDBC2 connection
The connection owning this statement object.
currentResult
protected JtdsResultSet currentResult
The current ResultSet
.
cursorName
protected String cursorName
The cursor name to be used for positioned updates.
escapeProcessing
protected boolean escapeProcessing
True if SQL statements should be preprocessed.
fetchDirection
protected int fetchDirection
The fetch direction for result sets.
fetchSize
protected int fetchSize
The fetch size (default 100, only used by cursor
ResultSet
s).
genKeyResultSet
protected JtdsResultSet genKeyResultSet
Dummy result set for getGeneratedKeys.
maxFieldSize
protected int maxFieldSize
The maximum field size (not used at present).
maxRows
protected int maxRows
The maximum number of rows to return (not used at present).
messages
protected final SQLDiagnostic messages
SQL Diagnostic exceptions and warnings.
openResultSets
protected ArrayList openResultSets
List of open result sets.
queryTimeout
protected int queryTimeout
The read query timeout in seconds
resultQueue
protected final LinkedList resultQueue
List of queued results (update counts, possibly followed by a
ResultSet
).
resultSetConcurrency
protected int resultSetConcurrency
The concurrency of result sets created by this statement.
resultSetType
protected int resultSetType
The type of result sets created by this statement.
tds
protected TdsCore tds
The TDS object used for server access.
updateCount
private int updateCount
The current update count.
addBatch
public void addBatch(String sql)
throws SQLException
addWarning
(package private) void addWarning(SQLWarning w)
Add an SQLWarning object to the statment warnings list.
w
- The SQLWarning to add.
cacheResults
protected void cacheResults()
throws SQLException
Cache as many results as possible (up to the first
ResultSet
). Called by ResultSet
s when the
end is reached.
cancel
public void cancel()
throws SQLException
checkCursorException
protected void checkCursorException(SQLException e)
throws SQLException
Check that the exception is caused by the failure to open a
cursor and not by a more serious SQL error.
e
- the exception returned by the cursor class
checkOpen
protected void checkOpen()
throws SQLException
Check that this statement is still open.
clearBatch
public void clearBatch()
throws SQLException
clearWarnings
public void clearWarnings()
throws SQLException
close
public void close()
throws SQLException
closeAllResultSets
(package private) void closeAllResultSets()
throws SQLException
Close all result sets.
closeCurrentResultSet
(package private) void closeCurrentResultSet()
throws SQLException
Close current result set (if any).
execute
public boolean execute(String sql)
throws SQLException
execute
public boolean execute(String sql,
String[] columnNames)
throws SQLException
execute
public boolean execute(String sql,
int autoGeneratedKeys)
throws SQLException
execute
public boolean execute(String sql,
int[] columnIndexes)
throws SQLException
executeBatch
public int[] executeBatch()
throws SQLException,
BatchUpdateException
Execute batch of SQL Statements.
The JDBC3 standard says that the behaviour 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 to
EXECUTE_FAILED
.
- update counts as an
int[]
executeImpl
private boolean executeImpl(String sql,
int autoGeneratedKeys,
boolean update)
throws SQLException
Implements the common functionality for plain statement execute
and {#link #executeUpdate}: basic checks, cleaning up of previous
results, setting up and executing the query and loading the first
results.
sql
- an SQL INSERT
, UPDATE
or
DELETE
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 is
executeUpdate
-- in this case an exception is
thrown if the first result is not an update count and no
cursor is created (direct execution)
true
if the first result is a
ResultSet
, false
if it's an update
count
executeMSBatch
protected SQLException executeMSBatch(int size,
int executeSize,
ArrayList counts)
throws SQLException
Execute the SQL batch on a MS server.
size
- the total size of the batchexecuteSize
- the maximum number of statements to send in one requestcounts
- the returned update counts
- chained exceptions linked to a
SQLException
executeQuery
public ResultSet executeQuery(String sql)
throws SQLException
executeSQL
protected boolean executeSQL(String sql,
String spName,
ParamInfo[] params,
boolean returnKeys,
boolean update,
boolean useCursor)
throws SQLException
Executes any type of SQL.
sql
- the SQL statement to executespName
- optional stored procedure nameparams
- optional parametersreturnKeys
- whether the statement returns generated keysupdate
- whether the caller is executeUpdate
useCursor
- whether the requested result set type or concurrency
or connection properties request usage of a cursor
true
if the first result is a result set
executeSQLQuery
protected ResultSet executeSQLQuery(String sql,
String spName,
ParamInfo[] params,
boolean useCursor)
throws SQLException
Executes SQL to obtain a result set.
sql
- the SQL statement to executespName
- optional stored procedure nameparams
- optional parametersuseCursor
- whether a cursor should be created for the SQL
- the result set generated by the query
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 for
PreparedStatements
.
size
- the total size of the batchexecuteSize
- the maximum number of statements to send in one requestcounts
- the returned update counts
- chained exceptions linked to a
SQLException
executeUpdate
public int executeUpdate(String sql)
throws SQLException
executeUpdate
public int executeUpdate(String sql,
String[] columnNames)
throws SQLException
executeUpdate
public int executeUpdate(String sql,
int autoGeneratedKeys)
throws SQLException
executeUpdate
public int executeUpdate(String sql,
int[] columnIndexes)
throws SQLException
finalize
protected void finalize()
throws Throwable
Called when this object goes out of scope to close any
ResultSet
object and this statement.
getConnection
public Connection getConnection()
throws SQLException
getDefaultFetchSize
(package private) int getDefaultFetchSize()
Retrieve the default fetch size for this statement.
- the default fetch size for a new
ResultSet
getFetchDirection
public int getFetchDirection()
throws SQLException
getFetchSize
public int getFetchSize()
throws SQLException
getGeneratedKeys
public ResultSet getGeneratedKeys()
throws SQLException
getMaxFieldSize
public int getMaxFieldSize()
throws SQLException
getMaxRows
public int getMaxRows()
throws SQLException
getMessages
(package private) SQLDiagnostic getMessages()
Get the statement's warnings list.
- The warnings list as a
SQLDiagnostic
.
getMoreResults
public boolean getMoreResults()
throws SQLException
getMoreResults
public boolean getMoreResults(int current)
throws SQLException
getQueryTimeout
public int getQueryTimeout()
throws SQLException
getResultSet
public ResultSet getResultSet()
throws SQLException
getResultSetConcurrency
public int getResultSetConcurrency()
throws SQLException
getResultSetHoldability
public int getResultSetHoldability()
throws SQLException
getResultSetType
public int getResultSetType()
throws SQLException
getTds
(package private) TdsCore getTds()
Get the Statement's TDS object.
- The TDS support as a
TdsCore Object.
getUpdateCount
public int getUpdateCount()
throws SQLException
getWarnings
public SQLWarning getWarnings()
throws SQLException
initialize
protected void initialize()
throws SQLException
Initialize the Statement
, by cleaning up all queued and
unprocessed results. Called by all execute methods.
notImplemented
(package private) static void notImplemented(String method)
throws SQLException
Report that user tried to call a method which has not been implemented.
method
- The method name to report in the error message.
processResults
private boolean processResults(boolean returnKeys,
boolean update)
throws SQLException
Queue up update counts into
resultQueue
until the end of the
response is reached or a
ResultSet
is encountered. Calling
processResults
while a
ResultSet
is open will
not close it, but will consume all remaining rows.
returnKeys
- true
if a generated keys
ResultSet
is expectedupdate
- true
if the method is called from within
executeUpdate
true
if there are any results,
false
otherwise
setCursorName
public void setCursorName(String name)
throws SQLException
setEscapeProcessing
public void setEscapeProcessing(boolean enable)
throws SQLException
setFetchDirection
public void setFetchDirection(int direction)
throws SQLException
setFetchSize
public void setFetchSize(int rows)
throws SQLException
setMaxFieldSize
public void setMaxFieldSize(int max)
throws SQLException
setMaxRows
public void setMaxRows(int max)
throws SQLException
setQueryTimeout
public void setQueryTimeout(int seconds)
throws SQLException
useCursor
protected boolean useCursor(boolean returnKeys,
String sqlWord)
Determines whether a cursor should be used based on the requested result
set type and concurrency, whether a cursor name has been set, the
useCursors
connection property has been set, the first
word in the SQL query is either SELECT or EXEC/EXECUTE and no generated
keys are returned.
returnKeys
- indicates whether keys will be returned by the querysqlWord
- the first word in the SQL query; can be
null
if the caller is
executeQuery(String)
true
if a cursor should be used, false
if not