T
- The expected type of the row elements.
Note: 1. in the case of single-column result this is clearly OK, but when a table with a
non homogeneous set of columns is queried, this type is not appropriate.
2. The ScrollingResults
is a very similar class that adds very little code
compared to this object to which it delegates all Result
API.
TODO: decide whether we merge these classespublic class ScrollableResults<T>
extends java.lang.Object
ResultSet
by providing scrolling primitives
(first
, next
, prev
, last
) and accessor to currently selected row.Modifier and Type | Class and Description |
---|---|
(package private) static interface |
ScrollableResults.SandboxRun<T>
Functional interface for passing in a piece of code that potentially throws a
SQLException . |
Modifier and Type | Field and Description |
---|---|
private java.util.List<RowStructure> |
rowStructure
The expected structure of a query, as generated by the
FqlToSqlConverter . |
private java.sql.ResultSet |
rs
The delegate
ResultSet . |
private Session |
session
The
Session to be used. |
private java.sql.Statement |
stmt
The statement to close.
|
Constructor and Description |
---|
ScrollableResults(java.sql.Statement stmt,
java.sql.ResultSet rs,
java.util.List<RowStructure> rowStructure,
Session session)
Constructs a
ScrollableResults object for further use. |
Modifier and Type | Method and Description |
---|---|
boolean |
beforeFirst()
Test whether the cursor is located before the first row in this result set.
|
void |
close()
Closes the result set by releasing all resources.
|
private static <T> T |
execute(ScrollableResults.SandboxRun<T> code,
T defVal)
Internal worker method.
|
boolean |
first()
Go to result row.
|
java.lang.Object[] |
get()
Obtain the full set of values on current row.
|
T |
get(int i,
java.lang.Class<T> tClass)
Obtain the value of a
i -th column of the current row. |
int |
getRowNumber()
Obtain the index of current row in the result set.
|
boolean |
isFirst()
Test whether the current row is the first in this result set.
|
boolean |
isLast()
Test whether the current row is the last in this result set.
|
boolean |
last()
Advance to the last result row.
|
private static void |
logException(java.lang.Exception e)
Internal worker method for logging an exception case.
|
boolean |
next()
Advance to the next result row.
|
boolean |
previous()
Go back to previous result row.
|
boolean |
scroll(int rows)
Scroll an given amount of rows, backward of forward.
|
boolean |
setRowNumber(int row)
Configures the index of current row in the result set.
|
private final java.sql.Statement stmt
rs
is the single result set for this statement.private final java.sql.ResultSet rs
ResultSet
.private final java.util.List<RowStructure> rowStructure
FqlToSqlConverter
.private final Session session
Session
to be used.public ScrollableResults(java.sql.Statement stmt, java.sql.ResultSet rs, java.util.List<RowStructure> rowStructure, Session session)
ScrollableResults
object for further use.stmt
- The JDBC statement to close.rs
- The ResultSet
to be used for extracting data.rowStructure
- The expected structure of a query, as generated by the FqlToSqlConverter
.session
- The Session
to be used.public boolean first()
true
if there is a result under the cursor and false
otherwise
(there are no more records or some error occurred).public boolean last()
true
if there is a result under the cursor and false
otherwise
(there are no more records or some error occurred).public boolean next()
true
if there is a result under the cursor and false
otherwise
(there are no more records or some error occurred).public boolean previous()
true
if there is a result under the cursor and false
otherwise
(there are no more records or some error occurred).public boolean isFirst()
true
if this is the first row of results.public boolean isLast()
true
if this is the last row of results.public java.lang.Object[] get()
Record
will be
returned.Object
s if the cursor is on a valid position. Otherwise null
is
returned. The type of returned elements may be both Long
when an array of PKs (recids)
is queried or an array of full Record
s. The caller is responsible for handling both
cases (including hydration, if needed).public T get(int i, java.lang.Class<T> tClass)
i
-th column of the current row.i
-th column of the current row if i
and current row
are valid and null
otherwise.public int getRowNumber()
0
).0
, or -1
if there is no current row.public boolean setRowNumber(int row)
0
).row
- The 0-based index of the row to be selected under cursor. Use negative values to
position the cursor form the end.true
if there is a row selected.public boolean beforeFirst()
true
if the cursor is located before the first row in this result set.public boolean scroll(int rows)
rows
- The number of rows to scroll. If a positive value is provided the scroll is forward
and if a negative value is provided the scroll is performed backward with number of
rows equals with the absolute value of rows
.true
if there is a valid row at the new location.public void close() throws PersistenceException
PersistenceException
- if some error occurs in the process.private static <T> T execute(ScrollableResults.SandboxRun<T> code, T defVal)
SQLException
and returns the result. If the exception is throws, a default value is
used for return.T
- The class type of the returned object.code
- The code to be executed.defVal
- The default value to be returned when a SQLException
is caught.code
or the defVal
if evaluation
failed.private static void logException(java.lang.Exception e)
e
- The exception holding the message stack trace.