public class JtdsStatement
extends java.lang.Object
implements java.sql.Statement
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:
Statement,
Connection.createStatement(),
ResultSet| Modifier and Type | Field and Description |
|---|---|
private java.util.concurrent.atomic.AtomicInteger |
_Closed
0
- this statement is open
1
- this statement is currently being closed
2
- this statement is closed
|
protected java.util.ArrayList |
batchValues
Batched SQL Statement array.
|
(package private) static int |
BOOLEAN |
(package private) static int |
CLOSE_ALL_RESULTS |
(package private) static int |
CLOSE_CURRENT_RESULT |
protected ColInfo[] |
colMetaData
The cached column meta data.
|
protected JtdsConnection |
connection
The connection owning this statement object.
|
protected JtdsResultSet |
currentResult
The current
ResultSet. |
protected java.lang.String |
cursorName
The cursor name to be used for positioned updates.
|
(package private) static int |
DATALINK |
(package private) static int |
DEFAULT_FETCH_SIZE |
protected boolean |
escapeProcessing
True if SQL statements should be preprocessed.
|
(package private) static java.lang.Integer |
EXECUTE_FAILED |
protected int |
fetchDirection
The fetch direction for result sets.
|
protected int |
fetchSize
The fetch size (default 100, only used by cursor
ResultSets). |
(package private) static java.lang.String |
GENKEYCOL
Column name to be used for retrieving generated keys from the server:
"_JTDS_GENE_RATED_KEYS_"
|
protected CachedResultSet |
genKeyResultSet
Dummy result set for getGeneratedKeys.
|
(package private) static int |
KEEP_CURRENT_RESULT |
protected int |
maxFieldSize
The maximum field size (not used at present).
|
protected int |
maxRows
The maximum number of rows to return (not used at present).
|
protected SQLDiagnostic |
messages
SQL Diagnostic exceptions and warnings.
|
(package private) static int |
NO_GENERATED_KEYS |
protected java.util.ArrayList |
openResultSets
List of open result sets.
|
protected int |
queryTimeout
The read query timeout in seconds
|
protected java.util.LinkedList |
resultQueue
List of queued results (update counts, possibly followed by a
ResultSet). |
protected int |
resultSetConcurrency
The concurrency of result sets created by this statement.
|
protected int |
resultSetType
The type of result sets created by this statement.
|
(package private) static int |
RETURN_GENERATED_KEYS |
(package private) static java.lang.Integer |
SUCCESS_NO_INFO |
protected TdsCore |
tds
The TDS object used for server access.
|
private int |
updateCount
The current update count.
|
| Constructor and Description |
|---|
JtdsStatement(JtdsConnection connection,
int resultSetType,
int resultSetConcurrency)
Construct a new Statement object.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addBatch(java.lang.String sql) |
(package private) void |
addWarning(java.sql.SQLWarning w)
Add an SQLWarning object to the statement warnings list.
|
protected void |
cacheResults()
Cache as many results as possible (up to the first
ResultSet). |
void |
cancel() |
protected void |
checkCursorException(java.sql.SQLException e)
Check that the exception is caused by the failure to open a
cursor and not by a more serious SQL error.
|
protected void |
checkOpen()
Check that this statement is still open.
|
void |
clearBatch() |
void |
clearWarnings() |
void |
close() |
(package private) void |
closeAllResultSets()
Close all result sets.
|
(package private) void |
closeCurrentResultSet()
Close current result set (if any).
|
void |
closeOnCompletion() |
boolean |
execute(java.lang.String sql) |
boolean |
execute(java.lang.String sql,
int autoGeneratedKeys) |
boolean |
execute(java.lang.String sql,
int[] columnIndexes) |
boolean |
execute(java.lang.String sql,
java.lang.String[] columnNames) |
int[] |
executeBatch()
Execute batch of SQL Statements.
|
private boolean |
executeImpl(java.lang.String sql,
int autoGeneratedKeys,
boolean update)
Implements the common functionality for plain statement
execute(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 java.sql.SQLException |
executeMSBatch(int size,
int executeSize,
java.util.ArrayList counts)
Execute the SQL batch on a MS server.
|
java.sql.ResultSet |
executeQuery(java.lang.String sql) |
protected boolean |
executeSQL(java.lang.String sql,
java.lang.String spName,
ParamInfo[] params,
boolean update,
boolean useCursor)
Executes any type of SQL.
|
protected java.sql.ResultSet |
executeSQLQuery(java.lang.String sql,
java.lang.String spName,
ParamInfo[] params,
boolean useCursor)
Executes SQL to obtain a result set.
|
protected java.sql.SQLException |
executeSybaseBatch(int size,
int executeSize,
java.util.ArrayList counts)
Execute the SQL batch on a Sybase server.
|
int |
executeUpdate(java.lang.String sql) |
int |
executeUpdate(java.lang.String sql,
int autoGeneratedKeys) |
int |
executeUpdate(java.lang.String sql,
int[] columnIndexes) |
int |
executeUpdate(java.lang.String sql,
java.lang.String[] columnNames) |
protected void |
finalize()
Called when this object goes out of scope to close any
ResultSet object and this statement. |
java.sql.Connection |
getConnection() |
(package private) int |
getDefaultFetchSize()
Retrieve the default fetch size for this statement.
|
int |
getFetchDirection() |
int |
getFetchSize() |
java.sql.ResultSet |
getGeneratedKeys() |
int |
getMaxFieldSize() |
int |
getMaxRows() |
(package private) SQLDiagnostic |
getMessages()
Get the statement's warnings list.
|
boolean |
getMoreResults() |
boolean |
getMoreResults(int current) |
int |
getQueryTimeout() |
java.sql.ResultSet |
getResultSet() |
int |
getResultSetConcurrency() |
int |
getResultSetHoldability() |
int |
getResultSetType() |
(package private) TdsCore |
getTds()
Get the Statement's TDS object.
|
int |
getUpdateCount() |
java.sql.SQLWarning |
getWarnings() |
boolean |
isClosed() |
boolean |
isCloseOnCompletion() |
boolean |
isPoolable() |
boolean |
isWrapperFor(java.lang.Class arg0) |
(package private) static void |
notImplemented(java.lang.String method)
Report that user tried to call a method which has not been implemented.
|
private boolean |
processResults(boolean update)
Queue up update counts into
resultQueue until the end of the
response is reached or a ResultSet is encountered. |
protected void |
reset()
Resets the
Statement, by cleaning up all queued and
unprocessed results. |
void |
setCursorName(java.lang.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 |
unwrap(java.lang.Class<T> iface) |
protected boolean |
useCursor(boolean returnKeys,
java.lang.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. |
static final java.lang.String GENKEYCOL
static final int RETURN_GENERATED_KEYS
static final int NO_GENERATED_KEYS
static final int CLOSE_CURRENT_RESULT
static final int KEEP_CURRENT_RESULT
static final int CLOSE_ALL_RESULTS
static final int BOOLEAN
static final int DATALINK
static final java.lang.Integer SUCCESS_NO_INFO
static final java.lang.Integer EXECUTE_FAILED
static final int DEFAULT_FETCH_SIZE
protected JtdsConnection connection
protected TdsCore tds
protected int queryTimeout
protected JtdsResultSet currentResult
ResultSet.private int updateCount
protected int fetchDirection
protected int resultSetType
protected int resultSetConcurrency
protected int fetchSize
ResultSets).protected java.lang.String cursorName
protected int maxFieldSize
protected int maxRows
protected boolean escapeProcessing
protected final SQLDiagnostic messages
protected java.util.ArrayList batchValues
protected CachedResultSet genKeyResultSet
protected final java.util.LinkedList resultQueue
ResultSet).protected java.util.ArrayList openResultSets
protected ColInfo[] colMetaData
private final java.util.concurrent.atomic.AtomicInteger _Closed
| 0 | - this statement is open |
| 1 | - this statement is currently being closed |
| 2 | - this statement is closed |
JtdsStatement(JtdsConnection connection, int resultSetType, int resultSetConcurrency) throws java.sql.SQLException
connection - The parent connection.resultSetType - The result set type for example TYPE_FORWARD_ONLY.resultSetConcurrency - The concurrency for example CONCUR_READ_ONLY.java.sql.SQLExceptionprotected void finalize()
throws java.lang.Throwable
ResultSet object and this statement.finalize in class java.lang.Objectjava.lang.ThrowableTdsCore getTds()
TdsCore Object.SQLDiagnostic getMessages()
SQLDiagnostic.protected void checkOpen()
throws java.sql.SQLException
java.sql.SQLException - if statement closed.protected void checkCursorException(java.sql.SQLException e)
throws java.sql.SQLException
e - the exception returned by the cursor classjava.sql.SQLException - if exception is not due to a cursor errorstatic void notImplemented(java.lang.String method)
throws java.sql.SQLException
method - The method name to report in the error message.java.sql.SQLExceptionvoid closeCurrentResultSet()
throws java.sql.SQLException
java.sql.SQLExceptionvoid closeAllResultSets()
throws java.sql.SQLException
java.sql.SQLExceptionvoid addWarning(java.sql.SQLWarning w)
w - The SQLWarning to add.protected java.sql.SQLException executeMSBatch(int size,
int executeSize,
java.util.ArrayList counts)
throws java.sql.SQLException
size - the total size of the batchexecuteSize - the maximum number of statements to send in one requestcounts - the returned update countsSQLExceptionjava.sql.SQLException - if a serious error occurs during executionprotected java.sql.SQLException executeSybaseBatch(int size,
int executeSize,
java.util.ArrayList counts)
throws java.sql.SQLException
PreparedStatements.size - the total size of the batchexecuteSize - the maximum number of statements to send in one requestcounts - the returned update countsSQLExceptionjava.sql.SQLException - if a serious error occurs during executionprotected java.sql.ResultSet executeSQLQuery(java.lang.String sql,
java.lang.String spName,
ParamInfo[] params,
boolean useCursor)
throws java.sql.SQLException
sql - the SQL statement to executespName - optional stored procedure nameparams - optional parametersuseCursor - whether a cursor should be created for the SQLjava.sql.SQLExceptionprotected boolean executeSQL(java.lang.String sql,
java.lang.String spName,
ParamInfo[] params,
boolean update,
boolean useCursor)
throws java.sql.SQLException
sql - the SQL statement to executespName - optional stored procedure nameparams - optional parametersupdate - whether the caller is executeUpdate(java.lang.String)useCursor - whether the requested result set type or concurrency
or connection properties request usage of a cursortrue if the first result is a result setjava.sql.SQLException - if an error condition occursprivate boolean processResults(boolean update)
throws java.sql.SQLException
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.update - true if the method is called from within
executeUpdatetrue if there are any results, false otherwisejava.sql.SQLException - if an error condition occursprotected void cacheResults()
throws java.sql.SQLException
ResultSet). Called by ResultSets when the
end is reached.java.sql.SQLExceptionprotected void reset()
throws java.sql.SQLException
Statement, by cleaning up all queued and
unprocessed results. Called by all execute methods and close()java.sql.SQLException - if an error occursprivate boolean executeImpl(java.lang.String sql,
int autoGeneratedKeys,
boolean update)
throws java.sql.SQLException
execute(java.lang.String)
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(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)true if the first result is a
ResultSet, false if it's an update
countjava.sql.SQLExceptionexecute(java.lang.String),
executeUpdate(java.lang.String)protected boolean useCursor(boolean returnKeys,
java.lang.String sqlWord)
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(java.lang.String)true if a cursor should be used, false
if notint getDefaultFetchSize()
ResultSetpublic int getFetchDirection()
throws java.sql.SQLException
getFetchDirection in interface java.sql.Statementjava.sql.SQLExceptionpublic int getFetchSize()
throws java.sql.SQLException
getFetchSize in interface java.sql.Statementjava.sql.SQLExceptionpublic int getMaxFieldSize()
throws java.sql.SQLException
getMaxFieldSize in interface java.sql.Statementjava.sql.SQLExceptionpublic int getMaxRows()
throws java.sql.SQLException
getMaxRows in interface java.sql.Statementjava.sql.SQLExceptionpublic int getQueryTimeout()
throws java.sql.SQLException
getQueryTimeout in interface java.sql.Statementjava.sql.SQLExceptionpublic int getResultSetConcurrency()
throws java.sql.SQLException
getResultSetConcurrency in interface java.sql.Statementjava.sql.SQLExceptionpublic int getResultSetHoldability()
throws java.sql.SQLException
getResultSetHoldability in interface java.sql.Statementjava.sql.SQLExceptionpublic int getResultSetType()
throws java.sql.SQLException
getResultSetType in interface java.sql.Statementjava.sql.SQLExceptionpublic int getUpdateCount()
throws java.sql.SQLException
getUpdateCount in interface java.sql.Statementjava.sql.SQLExceptionpublic void cancel()
throws java.sql.SQLException
cancel in interface java.sql.Statementjava.sql.SQLExceptionpublic void clearBatch()
throws java.sql.SQLException
clearBatch in interface java.sql.Statementjava.sql.SQLExceptionpublic void clearWarnings()
throws java.sql.SQLException
clearWarnings in interface java.sql.Statementjava.sql.SQLExceptionpublic void close()
throws java.sql.SQLException
close in interface java.lang.AutoCloseableclose in interface java.sql.Statementjava.sql.SQLExceptionpublic boolean getMoreResults()
throws java.sql.SQLException
getMoreResults in interface java.sql.Statementjava.sql.SQLExceptionpublic int[] executeBatch()
throws java.sql.SQLException,
java.sql.BatchUpdateException
EXECUTE_FAILED.
There is a problem with certain statements, returning more update counts
than there are batch operations. (see bug [])executeBatch in interface java.sql.Statementint[]java.sql.SQLExceptionjava.sql.BatchUpdateExceptionpublic void setFetchDirection(int direction)
throws java.sql.SQLException
setFetchDirection in interface java.sql.Statementjava.sql.SQLExceptionpublic void setFetchSize(int rows)
throws java.sql.SQLException
setFetchSize in interface java.sql.Statementjava.sql.SQLExceptionpublic void setMaxFieldSize(int max)
throws java.sql.SQLException
setMaxFieldSize in interface java.sql.Statementjava.sql.SQLExceptionpublic void setMaxRows(int max)
throws java.sql.SQLException
setMaxRows in interface java.sql.Statementjava.sql.SQLExceptionpublic void setQueryTimeout(int seconds)
throws java.sql.SQLException
setQueryTimeout in interface java.sql.Statementjava.sql.SQLExceptionpublic boolean getMoreResults(int current)
throws java.sql.SQLException
getMoreResults in interface java.sql.Statementjava.sql.SQLExceptionpublic void setEscapeProcessing(boolean enable)
throws java.sql.SQLException
setEscapeProcessing in interface java.sql.Statementjava.sql.SQLExceptionpublic int executeUpdate(java.lang.String sql)
throws java.sql.SQLException
executeUpdate in interface java.sql.Statementjava.sql.SQLExceptionpublic void addBatch(java.lang.String sql)
throws java.sql.SQLException
addBatch in interface java.sql.Statementjava.sql.SQLExceptionpublic void setCursorName(java.lang.String name)
throws java.sql.SQLException
setCursorName in interface java.sql.Statementjava.sql.SQLExceptionpublic boolean execute(java.lang.String sql)
throws java.sql.SQLException
execute in interface java.sql.Statementjava.sql.SQLExceptionpublic int executeUpdate(java.lang.String sql,
int autoGeneratedKeys)
throws java.sql.SQLException
executeUpdate in interface java.sql.Statementjava.sql.SQLExceptionpublic boolean execute(java.lang.String sql,
int autoGeneratedKeys)
throws java.sql.SQLException
execute in interface java.sql.Statementjava.sql.SQLExceptionpublic int executeUpdate(java.lang.String sql,
int[] columnIndexes)
throws java.sql.SQLException
executeUpdate in interface java.sql.Statementjava.sql.SQLExceptionpublic boolean execute(java.lang.String sql,
int[] columnIndexes)
throws java.sql.SQLException
execute in interface java.sql.Statementjava.sql.SQLExceptionpublic java.sql.Connection getConnection()
throws java.sql.SQLException
getConnection in interface java.sql.Statementjava.sql.SQLExceptionpublic java.sql.ResultSet getGeneratedKeys()
throws java.sql.SQLException
getGeneratedKeys in interface java.sql.Statementjava.sql.SQLExceptionpublic java.sql.ResultSet getResultSet()
throws java.sql.SQLException
getResultSet in interface java.sql.Statementjava.sql.SQLExceptionpublic java.sql.SQLWarning getWarnings()
throws java.sql.SQLException
getWarnings in interface java.sql.Statementjava.sql.SQLExceptionpublic int executeUpdate(java.lang.String sql,
java.lang.String[] columnNames)
throws java.sql.SQLException
executeUpdate in interface java.sql.Statementjava.sql.SQLExceptionpublic boolean execute(java.lang.String sql,
java.lang.String[] columnNames)
throws java.sql.SQLException
execute in interface java.sql.Statementjava.sql.SQLExceptionpublic java.sql.ResultSet executeQuery(java.lang.String sql)
throws java.sql.SQLException
executeQuery in interface java.sql.Statementjava.sql.SQLExceptionpublic boolean isClosed()
throws java.sql.SQLException
isClosed in interface java.sql.StatementJtdsStatement has been closedjava.sql.SQLExceptionpublic boolean isPoolable()
throws java.sql.SQLException
isPoolable in interface java.sql.Statementjava.sql.SQLExceptionpublic void setPoolable(boolean poolable)
throws java.sql.SQLException
setPoolable in interface java.sql.Statementjava.sql.SQLExceptionpublic boolean isWrapperFor(java.lang.Class arg0)
throws java.sql.SQLException
isWrapperFor in interface java.sql.Wrapperjava.sql.SQLExceptionpublic <T> T unwrap(java.lang.Class<T> iface)
throws java.sql.SQLException
unwrap in interface java.sql.Wrapperjava.sql.SQLExceptionpublic void closeOnCompletion()
throws java.sql.SQLException
closeOnCompletion in interface java.sql.Statementjava.sql.SQLExceptionpublic boolean isCloseOnCompletion()
throws java.sql.SQLException
isCloseOnCompletion in interface java.sql.Statementjava.sql.SQLExceptionGenerated on June 8 2013