Package net.sourceforge.jtds.jdbc
Class CachedResultSet
java.lang.Object
net.sourceforge.jtds.jdbc.JtdsResultSet
net.sourceforge.jtds.jdbc.CachedResultSet
- All Implemented Interfaces:
AutoCloseable,ResultSet,Wrapper
A memory cached scrollable/updateable result set.
Notes:
- For maximum performance use the scroll insensitive result set type.
- As the result set is cached in memory this implementation is limited to small result sets.
- Updateable or scroll sensitive result sets are limited to selects which reference one table only.
- Scroll sensitive result sets must have primary keys.
- Updates are optimistic. To guard against lost updates it is recommended that the table includes a timestamp column.
- This class is a plug-in replacement for the MSCursorResultSet class which may be advantageous in certain applications as the scroll insensitive result set implemented here is much faster than the server side cursor.
- Updateable result sets cannot be built from the output of stored procedures.
- This implementation uses 'select ... for browse' to obtain the column meta data needed to generate update statements etc.
- Named forward updateable cursors are also supported in which case positioned updates and deletes are used referencing a server side declared cursor.
- Named forward read only declared cursors can have a larger fetch size specified allowing a cursor alternative to the default direct select method.
- Version:
- $Id: CachedResultSet.java,v 1.26 2007-07-08 17:28:23 bheineman Exp $
- Author:
- Mike Hutchinson
- To do:
- Should add a "close statement" flag to the constructors
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected JtdsConnectionThe parent connection objectprotected final TdsCoreCursor TdsCore object.protected ParamInfo[]Buffer row used for inserts.protected booleanTable is keyed.protected booleanFlag to indicate Sybase.protected booleanIndicates currently inserting.protected final ParamInfo[]Original parameters.protected final StringOriginal procedure name.protected booleanIndicates that row has been deleted.protected booleanIndicates that row has been updated.protected booleanFetch size has been changed.protected StringOriginal SQL statement.protected StringFirst table name in select.protected final booleanTrue if this is a local temporary result set.protected ParamInfo[]The "update" row.protected final TdsCoreUpdates TdsCore object used for positioned updates.Fields inherited from class net.sourceforge.jtds.jdbc.JtdsResultSet
cancelled, CLOSE_CURSORS_AT_COMMIT, closed, columnCount, columns, concurrency, currentRow, cursorName, direction, fetchDirection, fetchSize, HOLD_CURSORS_OVER_COMMIT, INITIAL_ROW_COUNT, pos, POS_AFTER_LAST, POS_BEFORE_FIRST, resultSetType, rowData, rowPtr, rowsInResult, statement, wasNullFields inherited from interface java.sql.ResultSet
CONCUR_READ_ONLY, CONCUR_UPDATABLE, FETCH_FORWARD, FETCH_REVERSE, FETCH_UNKNOWN, TYPE_FORWARD_ONLY, TYPE_SCROLL_INSENSITIVE, TYPE_SCROLL_SENSITIVE -
Constructor Summary
ConstructorsConstructorDescriptionCachedResultSet(JtdsResultSet rs, boolean load) Creates a cached result set with the same columns (and optionally data) as an existing result set.CachedResultSet(JtdsStatement statement, String[] colName, int[] colType) Constructs a cached result set based on locally generated data.CachedResultSet(JtdsStatement statement, String sql, String procName, ParamInfo[] procedureParams, int resultSetType, int concurrency) Constructs a new cached result set.CachedResultSet(JtdsStatement statement, ColInfo[] columns, Object[] data) Creates a cached result set containing one row. -
Method Summary
Modifier and TypeMethodDescriptionbooleanabsolute(int row) (package private) voidWarning! Ensure the provided data matches the column layout of thisResultSet.voidvoidprotected static ParamInfobuildParameter(int pos, ColInfo info, Object value, boolean isUnicode) Creates a parameter object for an UPDATE, DELETE or INSERT statement.(package private) ParamInfo[]buildWhereClause(StringBuilder sql, ArrayList params, boolean select) Builds a WHERE clause for UPDATE or DELETE statements.voidvoidclose()private voidCloses the result set.private voidCreates a new scrollable result set in memory or a named server cursor.private booleancursorFetch(int rowNum) Fetches the next result row from the internal row array.voidbooleanfirst()void(package private) booleanAnalyses the tables in the result set and determines if the primary key columns needed to make it updateable exist.booleanisLast()booleanlast()voidvoidbooleannext()booleanprevious()protected voidRefreshes a result set row from keyed tables.protected voidRefreshes the row by rereading the result set.voidbooleanrelative(int row) booleanbooleanbooleanprotected ObjectsetColValue(int colIndex, int jdbcType, Object value, int length) Sets the specified column's data value.(package private) voidsetConcurrency(int concurrency) Modify the concurrency of the result set.voidsetFetchSize(int size) voidMethods inherited from class net.sourceforge.jtds.jdbc.JtdsResultSet
cacheResultSetRows, checkOpen, checkScrollable, checkUpdateable, clearWarnings, copyInfo, copyRow, findColumn, getArray, getArray, getAsciiStream, getAsciiStream, getBigDecimal, getBigDecimal, getBigDecimal, getBigDecimal, getBinaryStream, getBinaryStream, getBlob, getBlob, getBoolean, getBoolean, getByte, getByte, getBytes, getBytes, getCharacterStream, getCharacterStream, getClob, getClob, getColumn, getColumnCount, getColumns, getConcurrency, getCurrentRow, getDate, getDate, getDate, getDate, getDouble, getDouble, getFetchDirection, getFetchSize, getFloat, getFloat, getHoldability, getInt, getInt, getLong, getLong, getMetaData, getNCharacterStream, getNCharacterStream, getNClob, getNClob, getNString, getNString, getObject, getObject, getObject, getObject, getObject, getObject, getRef, getRef, getRow, getRowId, getRowId, getShort, getShort, getSQLXML, getSQLXML, getStatement, getString, getString, getTime, getTime, getTime, getTime, getTimestamp, getTimestamp, getTimestamp, getTimestamp, getType, getUnicodeStream, getUnicodeStream, getURL, getURL, getWarnings, isAfterLast, isBeforeFirst, isClosed, isFirst, isWrapperFor, newRow, notImplemented, setColLabel, setColName, setColType, setColumnCount, setFetchDirection, unwrap, updateArray, updateArray, updateAsciiStream, updateAsciiStream, updateAsciiStream, updateAsciiStream, updateAsciiStream, updateAsciiStream, updateBigDecimal, updateBigDecimal, updateBinaryStream, updateBinaryStream, updateBinaryStream, updateBinaryStream, updateBinaryStream, updateBinaryStream, updateBlob, updateBlob, updateBlob, updateBlob, updateBlob, updateBlob, updateBoolean, updateBoolean, updateByte, updateByte, updateBytes, updateBytes, updateCharacterStream, updateCharacterStream, updateCharacterStream, updateCharacterStream, updateCharacterStream, updateCharacterStream, updateClob, updateClob, updateClob, updateClob, updateClob, updateClob, updateDate, updateDate, updateDouble, updateDouble, updateFloat, updateFloat, updateInt, updateInt, updateLong, updateLong, updateNCharacterStream, updateNCharacterStream, updateNCharacterStream, updateNCharacterStream, updateNClob, updateNClob, updateNClob, updateNClob, updateNClob, updateNClob, updateNString, updateNString, updateNull, updateNull, updateObject, updateObject, updateObject, updateObject, updateRef, updateRef, updateRowId, updateRowId, updateShort, updateShort, updateSQLXML, updateSQLXML, updateString, updateString, updateTime, updateTime, updateTimestamp, updateTimestamp, wasNullMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.sql.ResultSet
updateObject, updateObject, updateObject, updateObject
-
Field Details
-
onInsertRow
protected boolean onInsertRowIndicates currently inserting. -
insertRow
Buffer row used for inserts. -
updateRow
The "update" row. -
rowUpdated
protected boolean rowUpdatedIndicates that row has been updated. -
rowDeleted
protected boolean rowDeletedIndicates that row has been deleted. -
tempResultSet
protected final boolean tempResultSetTrue if this is a local temporary result set. -
cursorTds
Cursor TdsCore object. -
updateTds
Updates TdsCore object used for positioned updates. -
isSybase
protected boolean isSybaseFlag to indicate Sybase. -
sizeChanged
protected boolean sizeChangedFetch size has been changed. -
sql
Original SQL statement. -
procName
Original procedure name. -
procedureParams
Original parameters. -
isKeyed
protected boolean isKeyedTable is keyed. -
tableName
First table name in select. -
connection
The parent connection object
-
-
Constructor Details
-
CachedResultSet
CachedResultSet(JtdsStatement statement, String sql, String procName, ParamInfo[] procedureParams, int resultSetType, int concurrency) throws SQLException Constructs a new cached result set. This result set will either be cached in memory or, if the cursor name is set, can be a forward only server side cursor. This latter form of cursor can also support positioned updates.- Parameters:
statement- the parent statement objectsql- the SQL statement used to build the result setprocName- an optional stored procedure nameprocedureParams- parameters for prepared statementsresultSetType- the result set type eg scrollableconcurrency- the result set concurrency eg updateable- Throws:
SQLException- if an error occurs
-
CachedResultSet
CachedResultSet(JtdsStatement statement, String[] colName, int[] colType) throws SQLException Constructs a cached result set based on locally generated data.- Parameters:
statement- the parent statement objectcolName- array of column namescolType- array of corresponding data types- Throws:
SQLException- if an error occurs
-
CachedResultSet
CachedResultSet(JtdsResultSet rs, boolean load) throws SQLException Creates a cached result set with the same columns (and optionally data) as an existing result set.- Parameters:
rs- the result set to copyload- load data from the supplied result set- Throws:
SQLException- if an error occurs
-
CachedResultSet
CachedResultSet(JtdsStatement statement, ColInfo[] columns, Object[] data) throws SQLException Creates a cached result set containing one row.- Parameters:
statement- the parent statement objectcolumns- the column descriptor arraydata- the row data- Throws:
SQLException- if an error occurs
-
-
Method Details
-
addRow
Warning! Ensure the provided data matches the column layout of this
ResultSet. All kind of weird behavior and errors could be expected otherwise.- Parameters:
data- data of the row to add
-
setConcurrency
void setConcurrency(int concurrency) Modify the concurrency of the result set. Use to make result set read only once loaded.- Parameters:
concurrency- the concurrency value egResultSet.CONCUR_READ_ONLY
-
cursorCreate
Creates a new scrollable result set in memory or a named server cursor.- Throws:
SQLException- if an error occurs
-
isCursorUpdateable
Analyses the tables in the result set and determines if the primary key columns needed to make it updateable exist. Sybase (and SQL 6.5) will automatically include any additional key and timestamp columns as hidden fields even if the user does not reference them in the select statement. If the table is unkeyed but there is an identity column then this is promoted to a key. Alternatively we can update, provided all the columns in the table row have been selected, by regarding all of them as keys. SQL Server 7+ does not return the correct primary key meta data for temporary tables so the driver has to query the catalog to locate any keys.- Returns:
trueif there is one table and it is keyed- Throws:
SQLException
-
cursorFetch
Fetches the next result row from the internal row array.- Parameters:
rowNum- the row number to fetch- Returns:
trueif a result set row is returned- Throws:
SQLException- if an error occurs
-
cursorClose
Closes the result set.- Throws:
SQLException
-
buildParameter
protected static ParamInfo buildParameter(int pos, ColInfo info, Object value, boolean isUnicode) throws SQLException Creates a parameter object for an UPDATE, DELETE or INSERT statement.- Parameters:
pos- the substitution position of the parameter marker in the SQLinfo- theColInfocolumn descriptorvalue- the column data item- Returns:
- the new parameter as a
ParamInfoobject - Throws:
SQLException
-
setColValue
protected Object setColValue(int colIndex, int jdbcType, Object value, int length) throws SQLException Sets the specified column's data value.- Overrides:
setColValuein classJtdsResultSet- Parameters:
colIndex- index of the columnvalue- new column valuelength- the length of a stream parameter- Returns:
- the value, possibly converted to an internal type
- Throws:
SQLException
-
buildWhereClause
ParamInfo[] buildWhereClause(StringBuilder sql, ArrayList params, boolean select) throws SQLException Builds a WHERE clause for UPDATE or DELETE statements.- Parameters:
sql- the SQL Statement to append the WHERE clause toparams- the parameter descriptor array for this statementselect- true if this WHERE clause will be used in a select statement- Returns:
- the parameter list as a
ParamInfo[] - Throws:
SQLException- if an error occurs
-
refreshKeyedRows
Refreshes a result set row from keyed tables. If all the tables in the result set have primary keys then the result set row can be refreshed by refetching the individual table rows.- Throws:
SQLException- if an error occurs
-
refreshReRead
Refreshes the row by rereading the result set. Obviously very slow on large result sets but may be the only option if tables do not have keys.- Throws:
SQLException
-
setFetchSize
- Specified by:
setFetchSizein interfaceResultSet- Overrides:
setFetchSizein classJtdsResultSet- Throws:
SQLException
-
afterLast
- Specified by:
afterLastin interfaceResultSet- Overrides:
afterLastin classJtdsResultSet- Throws:
SQLException
-
beforeFirst
- Specified by:
beforeFirstin interfaceResultSet- Overrides:
beforeFirstin classJtdsResultSet- Throws:
SQLException
-
cancelRowUpdates
- Specified by:
cancelRowUpdatesin interfaceResultSet- Overrides:
cancelRowUpdatesin classJtdsResultSet- Throws:
SQLException
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceResultSet- Overrides:
closein classJtdsResultSet- Throws:
SQLException
-
deleteRow
- Specified by:
deleteRowin interfaceResultSet- Overrides:
deleteRowin classJtdsResultSet- Throws:
SQLException
-
insertRow
- Specified by:
insertRowin interfaceResultSet- Overrides:
insertRowin classJtdsResultSet- Throws:
SQLException
-
moveToCurrentRow
- Specified by:
moveToCurrentRowin interfaceResultSet- Overrides:
moveToCurrentRowin classJtdsResultSet- Throws:
SQLException
-
moveToInsertRow
- Specified by:
moveToInsertRowin interfaceResultSet- Overrides:
moveToInsertRowin classJtdsResultSet- Throws:
SQLException
-
refreshRow
- Specified by:
refreshRowin interfaceResultSet- Overrides:
refreshRowin classJtdsResultSet- Throws:
SQLException
-
updateRow
- Specified by:
updateRowin interfaceResultSet- Overrides:
updateRowin classJtdsResultSet- Throws:
SQLException
-
first
- Specified by:
firstin interfaceResultSet- Overrides:
firstin classJtdsResultSet- Throws:
SQLException
-
isLast
- Specified by:
isLastin interfaceResultSet- Overrides:
isLastin classJtdsResultSet- Throws:
SQLException
-
last
- Specified by:
lastin interfaceResultSet- Overrides:
lastin classJtdsResultSet- Throws:
SQLException
-
next
- Specified by:
nextin interfaceResultSet- Overrides:
nextin classJtdsResultSet- Throws:
SQLException
-
previous
- Specified by:
previousin interfaceResultSet- Overrides:
previousin classJtdsResultSet- Throws:
SQLException
-
rowDeleted
- Specified by:
rowDeletedin interfaceResultSet- Overrides:
rowDeletedin classJtdsResultSet- Throws:
SQLException
-
rowInserted
- Specified by:
rowInsertedin interfaceResultSet- Overrides:
rowInsertedin classJtdsResultSet- Throws:
SQLException
-
rowUpdated
- Specified by:
rowUpdatedin interfaceResultSet- Overrides:
rowUpdatedin classJtdsResultSet- Throws:
SQLException
-
absolute
- Specified by:
absolutein interfaceResultSet- Overrides:
absolutein classJtdsResultSet- Throws:
SQLException
-
relative
- Specified by:
relativein interfaceResultSet- Overrides:
relativein classJtdsResultSet- Throws:
SQLException
-
getCursorName
- Specified by:
getCursorNamein interfaceResultSet- Overrides:
getCursorNamein classJtdsResultSet- Throws:
SQLException
-