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.
The object is always aware whether the pointer of the delegate ResultSet is on a valid row by
intercepting the result of all invoked methods which moves the cursor in the delegate. This also have the
advantage of a bit of performance boost as it is able to detect and short-cut the call to the delegate in
cases the result is false / null.
| 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 boolean |
available
Tracks the 'available' flag of the inner
ResultSet. |
private static CentralLogger |
LOG
Logger
|
private ScrollableResults.SandboxRun<java.lang.Void> |
onClose
On close hook
|
private java.util.ArrayList<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.ArrayList<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.
|
java.lang.Object[] |
get(boolean forceOnlyId)
Obtain the full set of values on current row.
|
java.lang.Object[] |
get(DmoMeta dmoInfo,
java.lang.String idCol,
java.lang.String[] cols)
Obtain the selection 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. |
private java.lang.Object[] |
getImpl(boolean forceOnlyId)
Obtain the full set of values on 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.
|
boolean |
next()
Advance to the next result row.
|
ScrollableResults<T> |
onClose(ScrollableResults.SandboxRun<java.lang.Void> hook)
Set on close hook
|
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 static final CentralLogger LOG
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.ArrayList<RowStructure> rowStructure
FqlToSqlConverter.private final Session session
Session to be used.private ScrollableResults.SandboxRun<java.lang.Void> onClose
private boolean available
ResultSet.public ScrollableResults(java.sql.Statement stmt,
java.sql.ResultSet rs,
java.util.ArrayList<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 ScrollableResults<T> onClose(ScrollableResults.SandboxRun<java.lang.Void> hook)
hook - new hook;thispublic 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.Objects 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 Records. The caller is responsible for handling both
cases (including hydration, if needed).public java.lang.Object[] get(boolean forceOnlyId)
Records will be
returned.forceOnlyId - Indicates whether it's only the primary keys that must be returned,
or if the records must be hydrated and all data found is returned.Objects if the cursor is on a valid position.
If the cursor is not on a valid position then 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 Records.
The caller is responsible for handling both cases (including hydration, if needed).
When forceOnlyId is true, an array of Long only with the PKs.public java.lang.Object[] get(DmoMeta dmoInfo, java.lang.String idCol, java.lang.String[] cols)
dmoInfo - The target data descriptionidCol - The name of the column which holds entity's idcols - Array of names of other columns to retrieve.Object s if the cursor is on a valid position. Otherwise null is
returned.public T get(int i, java.lang.Class<T> tClass)
i-th column of the current row.i - column numbertClass - columni-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 java.lang.Object[] getImpl(boolean forceOnlyId)
throws java.sql.SQLException
Records will be
returned.forceOnlyId - Indicates whether it's only the primary keys that must be returned,
or if the records must be hydrated and all data found is returned.Objects if the cursor is on a valid position.
If the cursor is not on a valid position then 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 Records.
The caller is responsible for handling both cases (including hydration, if needed).
When forceOnlyId is true, an array of Long only with the PKs.java.sql.SQLExceptionprivate 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.