Package net.sourceforge.jtds.jdbc
Class MSCursorResultSet
- java.lang.Object
-
- net.sourceforge.jtds.jdbc.JtdsResultSet
-
- net.sourceforge.jtds.jdbc.MSCursorResultSet
-
- All Implemented Interfaces:
java.lang.AutoCloseable,java.sql.ResultSet,java.sql.Wrapper
public class MSCursorResultSet extends JtdsResultSet
This class extends the JtdsResultSet to support scrollable and or updateable cursors on Microsoft servers.The undocumented Microsoft sp_cursor procedures are used.
Implementation notes:
- All of Alin's cursor result set logic is incorporated here.
- This logic was originally implemented in the JtdsResultSet class but on reflection it seems that Alin's original approch of having a dedicated cursor class leads to a more flexible and maintainable design.
- Version:
- $Id: MSCursorResultSet.java,v 1.59 2007-07-11 20:02:45 bheineman Exp $
- Author:
- Alin Sinpalean, Mike Hutchinson
-
-
Field Summary
Fields Modifier and Type Field Description private booleanasyncCursorThe cursor is being built asynchronously.private static intCURSOR_CONCUR_OPTIMISTICprivate static intCURSOR_CONCUR_OPTIMISTIC_VALUESprivate static intCURSOR_CONCUR_READ_ONLYprivate static intCURSOR_CONCUR_SCROLL_LOCKSprivate static java.lang.IntegerCURSOR_OP_DELETEprivate static java.lang.IntegerCURSOR_OP_INSERTprivate static java.lang.IntegerCURSOR_OP_UPDATEprivate static intCURSOR_TYPE_AUTO_FETCHprivate static intCURSOR_TYPE_DYNAMICprivate static intCURSOR_TYPE_FASTFORWARDONLYprivate static intCURSOR_TYPE_FORWARDprivate static intCURSOR_TYPE_KEYSETprivate static intCURSOR_TYPE_PARAMETERIZEDprivate static intCURSOR_TYPE_STATICprivate intcursorPosActual position of the cursor.private static java.lang.IntegerFETCH_ABSOLUTEprivate static java.lang.IntegerFETCH_FIRSTprivate static java.lang.IntegerFETCH_INFOprivate static java.lang.IntegerFETCH_LASTprivate static java.lang.IntegerFETCH_NEXTprivate static java.lang.IntegerFETCH_PREVIOUSprivate static java.lang.IntegerFETCH_RELATIVEprivate static java.lang.IntegerFETCH_REPEATprivate ParamInfo[]insertRowThe "insert row".private booleanonInsertRowSet whenmoveToInsertRow()was called.private ParamInfoPARAM_CURSOR_HANDLECursor handle parameter.private ParamInfoPARAM_FETCHTYPEsp_cursorfetchfetchtype parameter.private ParamInfoPARAM_NUMROWS_INsp_cursorfetchnumrows IN parameter (for actual fetches).private ParamInfoPARAM_NUMROWS_OUTsp_cursorfetchnumrows OUT parameter (for FETCH_INFO).private ParamInfoPARAM_OPTYPEsp_cursoroptype parameter.private ParamInfoPARAM_ROWNUMsp_cursorrownum parameter.private ParamInfoPARAM_ROWNUM_INsp_cursorfetchrownum IN parameter (for actual fetches).private ParamInfoPARAM_ROWNUM_OUTsp_cursorfetchrownum OUT parameter (for FETCH_INFO).private ParamInfoPARAM_TABLEsp_cursortable parameter.private java.lang.Object[][]rowCacheThe row cache used insteadJtdsResultSet.currentRow.private static java.lang.IntegerSQL_ROW_DELETEDThe row has been deleted.private static java.lang.IntegerSQL_ROW_DIRTYThe row is dirty and needs to be reloaded (internal state).private static java.lang.IntegerSQL_ROW_SUCCESSThe row is valid.private ParamInfo[]updateRowThe "update row".-
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, wasNull
-
-
Constructor Summary
Constructors Constructor Description MSCursorResultSet(JtdsStatement statement, java.lang.String sql, java.lang.String procName, ParamInfo[] procedureParams, int resultSetType, int concurrency)Construct a cursor result set using Microsoft sp_cursorcreate etc.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanabsolute(int row)voidafterLast()voidbeforeFirst()voidcancelRowUpdates()voidclose()private voidcursor(java.lang.Integer opType, ParamInfo[] row)Support general cursor operations such as delete, update etc.private voidcursorClose()Close a server side cursor.private voidcursorCreate(java.lang.String sql, java.lang.String procName, ParamInfo[] parameters)Create a new Cursor result set using the internal sp_cursoropen procedure.private booleancursorFetch(java.lang.Integer fetchType, int rowNum)Fetch the next result row from a cursor using the internal sp_cursorfetch procedure.voiddeleteRow()booleanfirst()protected java.lang.ObjectgetColumn(int index)Get the specified column's data item.protected java.lang.Object[]getCurrentRow()Retrieve the current row data.(package private) static intgetCursorConcurrencyOpt(int resultSetConcurrency)Translates a JDBC result set concurrency into SQL Server native @ccOpt value for use with stored procedures such as sp_cursoropen, sp_cursorprepare or sp_cursorprepexec.(package private) static intgetCursorScrollOpt(int resultSetType, int resultSetConcurrency, boolean parameterized)Translates a JDBC result set type into SQL Server native @scrollOpt value for use with stored procedures such as sp_cursoropen, sp_cursorprepare or sp_cursorprepexec.voidinsertRow()booleanisLast()booleanlast()voidmoveToCurrentRow()voidmoveToInsertRow()booleannext()booleanprevious()private voidprocessOutput(TdsCore tds, boolean setMeta)Processes the output of a cursor open or fetch operation.voidrefreshRow()booleanrelative(int row)booleanrowDeleted()booleanrowInserted()booleanrowUpdated()protected java.lang.ObjectsetColValue(int colIndex, int jdbcType, java.lang.Object value, int length)Set the specified column's data value.voidupdateRow()-
Methods 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, getColumnCount, getColumns, getConcurrency, getCursorName, 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, setFetchSize, 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, wasNull
-
-
-
-
Field Detail
-
FETCH_FIRST
private static final java.lang.Integer FETCH_FIRST
-
FETCH_NEXT
private static final java.lang.Integer FETCH_NEXT
-
FETCH_PREVIOUS
private static final java.lang.Integer FETCH_PREVIOUS
-
FETCH_LAST
private static final java.lang.Integer FETCH_LAST
-
FETCH_ABSOLUTE
private static final java.lang.Integer FETCH_ABSOLUTE
-
FETCH_RELATIVE
private static final java.lang.Integer FETCH_RELATIVE
-
FETCH_REPEAT
private static final java.lang.Integer FETCH_REPEAT
-
FETCH_INFO
private static final java.lang.Integer FETCH_INFO
-
CURSOR_TYPE_KEYSET
private static final int CURSOR_TYPE_KEYSET
- See Also:
- Constant Field Values
-
CURSOR_TYPE_DYNAMIC
private static final int CURSOR_TYPE_DYNAMIC
- See Also:
- Constant Field Values
-
CURSOR_TYPE_FORWARD
private static final int CURSOR_TYPE_FORWARD
- See Also:
- Constant Field Values
-
CURSOR_TYPE_STATIC
private static final int CURSOR_TYPE_STATIC
- See Also:
- Constant Field Values
-
CURSOR_TYPE_FASTFORWARDONLY
private static final int CURSOR_TYPE_FASTFORWARDONLY
- See Also:
- Constant Field Values
-
CURSOR_TYPE_PARAMETERIZED
private static final int CURSOR_TYPE_PARAMETERIZED
- See Also:
- Constant Field Values
-
CURSOR_TYPE_AUTO_FETCH
private static final int CURSOR_TYPE_AUTO_FETCH
- See Also:
- Constant Field Values
-
CURSOR_CONCUR_READ_ONLY
private static final int CURSOR_CONCUR_READ_ONLY
- See Also:
- Constant Field Values
-
CURSOR_CONCUR_SCROLL_LOCKS
private static final int CURSOR_CONCUR_SCROLL_LOCKS
- See Also:
- Constant Field Values
-
CURSOR_CONCUR_OPTIMISTIC
private static final int CURSOR_CONCUR_OPTIMISTIC
- See Also:
- Constant Field Values
-
CURSOR_CONCUR_OPTIMISTIC_VALUES
private static final int CURSOR_CONCUR_OPTIMISTIC_VALUES
- See Also:
- Constant Field Values
-
CURSOR_OP_INSERT
private static final java.lang.Integer CURSOR_OP_INSERT
-
CURSOR_OP_UPDATE
private static final java.lang.Integer CURSOR_OP_UPDATE
-
CURSOR_OP_DELETE
private static final java.lang.Integer CURSOR_OP_DELETE
-
SQL_ROW_DIRTY
private static final java.lang.Integer SQL_ROW_DIRTY
The row is dirty and needs to be reloaded (internal state).
-
SQL_ROW_SUCCESS
private static final java.lang.Integer SQL_ROW_SUCCESS
The row is valid.
-
SQL_ROW_DELETED
private static final java.lang.Integer SQL_ROW_DELETED
The row has been deleted.
-
onInsertRow
private boolean onInsertRow
Set whenmoveToInsertRow()was called.
-
insertRow
private ParamInfo[] insertRow
The "insert row".
-
updateRow
private ParamInfo[] updateRow
The "update row".
-
rowCache
private java.lang.Object[][] rowCache
The row cache used insteadJtdsResultSet.currentRow.
-
cursorPos
private int cursorPos
Actual position of the cursor.
-
asyncCursor
private boolean asyncCursor
The cursor is being built asynchronously.
-
PARAM_CURSOR_HANDLE
private final ParamInfo PARAM_CURSOR_HANDLE
Cursor handle parameter.
-
PARAM_FETCHTYPE
private final ParamInfo PARAM_FETCHTYPE
sp_cursorfetchfetchtype parameter.
-
PARAM_ROWNUM_IN
private final ParamInfo PARAM_ROWNUM_IN
sp_cursorfetchrownum IN parameter (for actual fetches).
-
PARAM_NUMROWS_IN
private final ParamInfo PARAM_NUMROWS_IN
sp_cursorfetchnumrows IN parameter (for actual fetches).
-
PARAM_ROWNUM_OUT
private final ParamInfo PARAM_ROWNUM_OUT
sp_cursorfetchrownum OUT parameter (for FETCH_INFO).
-
PARAM_NUMROWS_OUT
private final ParamInfo PARAM_NUMROWS_OUT
sp_cursorfetchnumrows OUT parameter (for FETCH_INFO).
-
PARAM_OPTYPE
private final ParamInfo PARAM_OPTYPE
sp_cursoroptype parameter.
-
PARAM_ROWNUM
private final ParamInfo PARAM_ROWNUM
sp_cursorrownum parameter.
-
PARAM_TABLE
private final ParamInfo PARAM_TABLE
sp_cursortable parameter.
-
-
Constructor Detail
-
MSCursorResultSet
MSCursorResultSet(JtdsStatement statement, java.lang.String sql, java.lang.String procName, ParamInfo[] procedureParams, int resultSetType, int concurrency) throws java.sql.SQLException
Construct a cursor result set using Microsoft sp_cursorcreate etc.- Parameters:
statement- The parent statement object or null.resultSetType- one of FORWARD_ONLY, SCROLL_INSENSITIVE, SCROLL_SENSITIVE.concurrency- One of CONCUR_READ_ONLY, CONCUR_UPDATE.- Throws:
java.sql.SQLException
-
-
Method Detail
-
setColValue
protected java.lang.Object setColValue(int colIndex, int jdbcType, java.lang.Object value, int length) throws java.sql.SQLExceptionSet 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:
java.sql.SQLException
-
getColumn
protected java.lang.Object getColumn(int index) throws java.sql.SQLExceptionGet the specified column's data item.- Overrides:
getColumnin classJtdsResultSet- Parameters:
index- the column index in the row- Returns:
- the column value as an
Object - Throws:
java.sql.SQLException- if the index is out of bounds or there is no current row
-
getCursorScrollOpt
static int getCursorScrollOpt(int resultSetType, int resultSetConcurrency, boolean parameterized)Translates a JDBC result set type into SQL Server native @scrollOpt value for use with stored procedures such as sp_cursoropen, sp_cursorprepare or sp_cursorprepexec.- Parameters:
resultSetType- JDBC result set type (one of theResultSet.TYPE_XXXvalues)resultSetConcurrency- JDBC result set concurrency (one of theResultSet.CONCUR_XXXvalues)- Returns:
- a value for the @scrollOpt parameter
-
getCursorConcurrencyOpt
static int getCursorConcurrencyOpt(int resultSetConcurrency)
Translates a JDBC result set concurrency into SQL Server native @ccOpt value for use with stored procedures such as sp_cursoropen, sp_cursorprepare or sp_cursorprepexec.- Parameters:
resultSetConcurrency- JDBC result set concurrency (one of theResultSet.CONCUR_XXXvalues)- Returns:
- a value for the @scrollOpt parameter
-
cursorCreate
private void cursorCreate(java.lang.String sql, java.lang.String procName, ParamInfo[] parameters) throws java.sql.SQLExceptionCreate a new Cursor result set using the internal sp_cursoropen procedure.- Parameters:
sql- The SQL SELECT statement.procName- Optional procedure name for cursors based on a stored procedure.parameters- Optional stored procedure parameters.- Throws:
java.sql.SQLException
-
cursorFetch
private boolean cursorFetch(java.lang.Integer fetchType, int rowNum) throws java.sql.SQLExceptionFetch the next result row from a cursor using the internal sp_cursorfetch procedure.- Parameters:
fetchType- The type of fetch eg FETCH_ABSOLUTE.rowNum- The row number to fetch.- Returns:
booleantrue if a result set row is returned.- Throws:
java.sql.SQLException
-
cursor
private void cursor(java.lang.Integer opType, ParamInfo[] row) throws java.sql.SQLExceptionSupport general cursor operations such as delete, update etc.- Parameters:
opType- the type of operation to performrow- the row number to update- Throws:
java.sql.SQLException
-
cursorClose
private void cursorClose() throws java.sql.SQLExceptionClose a server side cursor.- Throws:
java.sql.SQLException
-
processOutput
private void processOutput(TdsCore tds, boolean setMeta) throws java.sql.SQLException
Processes the output of a cursor open or fetch operation. Fetches a batch of rows from theTdsCore, loading them into the row cache and optionally sets the column meta data (if called on cursor open). Consumes all the response and checks for server returned errors.- Parameters:
tds- theTdsCoreinstancesetMeta- whether column meta data needs to be loaded (cursor open)- Throws:
java.sql.SQLException- if an error occurs or an error message is returned by the server
-
afterLast
public void afterLast() throws java.sql.SQLException- Specified by:
afterLastin interfacejava.sql.ResultSet- Overrides:
afterLastin classJtdsResultSet- Throws:
java.sql.SQLException
-
beforeFirst
public void beforeFirst() throws java.sql.SQLException- Specified by:
beforeFirstin interfacejava.sql.ResultSet- Overrides:
beforeFirstin classJtdsResultSet- Throws:
java.sql.SQLException
-
cancelRowUpdates
public void cancelRowUpdates() throws java.sql.SQLException- Specified by:
cancelRowUpdatesin interfacejava.sql.ResultSet- Overrides:
cancelRowUpdatesin classJtdsResultSet- Throws:
java.sql.SQLException
-
close
public void close() throws java.sql.SQLException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.sql.ResultSet- Overrides:
closein classJtdsResultSet- Throws:
java.sql.SQLException
-
deleteRow
public void deleteRow() throws java.sql.SQLException- Specified by:
deleteRowin interfacejava.sql.ResultSet- Overrides:
deleteRowin classJtdsResultSet- Throws:
java.sql.SQLException
-
insertRow
public void insertRow() throws java.sql.SQLException- Specified by:
insertRowin interfacejava.sql.ResultSet- Overrides:
insertRowin classJtdsResultSet- Throws:
java.sql.SQLException
-
moveToCurrentRow
public void moveToCurrentRow() throws java.sql.SQLException- Specified by:
moveToCurrentRowin interfacejava.sql.ResultSet- Overrides:
moveToCurrentRowin classJtdsResultSet- Throws:
java.sql.SQLException
-
moveToInsertRow
public void moveToInsertRow() throws java.sql.SQLException- Specified by:
moveToInsertRowin interfacejava.sql.ResultSet- Overrides:
moveToInsertRowin classJtdsResultSet- Throws:
java.sql.SQLException
-
refreshRow
public void refreshRow() throws java.sql.SQLException- Specified by:
refreshRowin interfacejava.sql.ResultSet- Overrides:
refreshRowin classJtdsResultSet- Throws:
java.sql.SQLException
-
updateRow
public void updateRow() throws java.sql.SQLException- Specified by:
updateRowin interfacejava.sql.ResultSet- Overrides:
updateRowin classJtdsResultSet- Throws:
java.sql.SQLException
-
first
public boolean first() throws java.sql.SQLException- Specified by:
firstin interfacejava.sql.ResultSet- Overrides:
firstin classJtdsResultSet- Throws:
java.sql.SQLException
-
isLast
public boolean isLast() throws java.sql.SQLException- Specified by:
isLastin interfacejava.sql.ResultSet- Overrides:
isLastin classJtdsResultSet- Throws:
java.sql.SQLException
-
last
public boolean last() throws java.sql.SQLException- Specified by:
lastin interfacejava.sql.ResultSet- Overrides:
lastin classJtdsResultSet- Throws:
java.sql.SQLException
-
next
public boolean next() throws java.sql.SQLException- Specified by:
nextin interfacejava.sql.ResultSet- Overrides:
nextin classJtdsResultSet- Throws:
java.sql.SQLException
-
previous
public boolean previous() throws java.sql.SQLException- Specified by:
previousin interfacejava.sql.ResultSet- Overrides:
previousin classJtdsResultSet- Throws:
java.sql.SQLException
-
rowDeleted
public boolean rowDeleted() throws java.sql.SQLException- Specified by:
rowDeletedin interfacejava.sql.ResultSet- Overrides:
rowDeletedin classJtdsResultSet- Throws:
java.sql.SQLException
-
rowInserted
public boolean rowInserted() throws java.sql.SQLException- Specified by:
rowInsertedin interfacejava.sql.ResultSet- Overrides:
rowInsertedin classJtdsResultSet- Throws:
java.sql.SQLException
-
rowUpdated
public boolean rowUpdated() throws java.sql.SQLException- Specified by:
rowUpdatedin interfacejava.sql.ResultSet- Overrides:
rowUpdatedin classJtdsResultSet- Throws:
java.sql.SQLException
-
absolute
public boolean absolute(int row) throws java.sql.SQLException- Specified by:
absolutein interfacejava.sql.ResultSet- Overrides:
absolutein classJtdsResultSet- Throws:
java.sql.SQLException
-
relative
public boolean relative(int row) throws java.sql.SQLException- Specified by:
relativein interfacejava.sql.ResultSet- Overrides:
relativein classJtdsResultSet- Throws:
java.sql.SQLException
-
getCurrentRow
protected java.lang.Object[] getCurrentRow()
Description copied from class:JtdsResultSetRetrieve the current row data.- Overrides:
getCurrentRowin classJtdsResultSet- Returns:
- The current row data as an
Object[].
-
-