Class DBReader
- All Implemented Interfaces:
Closeable,AutoCloseable,RecordData,DBContextAware
This class is used to perform database queries from a DBCommand object and access the results.
In oder to perform a query call the open() function or - for single row queries - call getRecordData();
You can iterate through the rows using moveNext() or an iterator.
However take care: A reader must always be explicitly closed using the close() method!
Otherwise you may lock the JDBC connection and run out of resources.
Use
try { ... } finally { reader.close(); } to make sure the reader is closed.
To access and work with the query result you can do one of the following:
- access field values directly by using one of the get... functions (see
DBRecordData) - get the rows as a list of Java Beans using by using
getBeanList(Class, int) - get the rows as an XML-Document using
getXmlDocument() - initialize a DBRecord with the current row data using
initRecord(DBRecordBase)
This will allow you to modify and update the data.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassThis is an iterator for forward only resultsets.classDBReaderIterator Base class for DBReader interatorsclassThis is an iterator for scrolling resultsets. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintAdds all children to a parent.intaddXmlData(Element parent) Adds all children to a parent.intaddXmlMeta(Element parent) Moves the cursor down one row from its current position.static voidCall this if you want to check whether there are any unclosed resultsets It logs stack traces to help find piece of code where a DBReader was opened but not closed.voidclose()Closes the DBRecordSet object, the Statement object and detach the columns.
A reader must always be closed immediately after using it.static booleanenableOpenResultSetTracking(boolean enable) Enables or disabled tracking of open ResultSetsprotected voidSupport for finding code errors where a DBRecordSet is opened but not closed.protected Constructor<?>findBeanConstructor(Class<?> beanClass) Returns a constructor for a bean class for the set of parameters or null if no suitable constructor is foundprotected intfindFieldIndex(ColumnExpr column) finds the field Index of a given column expression Internally used as helper for getFieldIndex()final <T> List<T>getBeanList(Class<T> t) Returns the result of a query as a list of objects.final <T> List<T>getBeanList(Class<T> t, int maxItems) Returns the result of a query as a list of objects.<L extends List<T>,T>
LgetBeanList(L list, Class<T> t, Object parent, int maxCount) Returns the result of a query as a list of objects restricted to a maximum number of objects (unless maxCount is -1).getColumn(int iColumn) Get the column Expression at positionprotected final DBColumnExpr[]Access the column expression listReturns the current Contextfinal DBDatabaseReturns the current DBDatabase object.intreturns the number of the elements of the colList arrayintgetFieldIndex(String column) Returns the index value by a specified column name.intgetFieldIndex(ColumnExpr column) Returns the index value by a specified DBColumnExpr object.voidOpens the reader by executing the given SQL command and moves to the first row.
If true is returned data of the row can be accessed through the functions on the RecordData interface.
This function is intended for single row queries and provided for convenience.
However it behaves exacly as calling reader.open() and reader.moveNext()longgetRecordId(Entity entity) Returns the record id for a type of entity which has a single numeric primary keyObject[]getRecordKey(Entity entity) Returns the record key for a type of entityprotected final ResultSetAccess the JDBC-ResultSetbooleangetValue(int index) Returns a data value identified by the column index.protected DBXmlDictionaryreturns the DBXmlDictionary that should used to generate XMLDocumentsReturns a XML document with the field description an values of this record.protected voidinit(DBDatabase db, DBColumnExpr[] columns, ResultSet rset) Initialize the reader from an open JDBC-ResultSetvoidinitRecord(DBRecordBase rec) initializes a DBRecord object with the values of the current row.booleanisNull(int index) Checks wehter a column value is null Unlike the base class implementation, this class directly check the value fromt the resultset.booleanisOpen()Checks if the rowset is openfinal Iterator<DBRecordData>iterator()Returns an row iterator for this reader.iterator(int maxCount) Returns an row iterator for this reader.
There can only be one iterator at a time.booleanmoveNext()Moves the cursor down one row from its current position.final voidopen(DBCommandExpr cmd) Opens the reader by executing the given SQL command.voidopen(DBCommandExpr cmd, boolean scrollable) Opens the reader by executing the given SQL command.
After the reader is open, the reader's position is before the first record.
Use moveNext or iterator() to step through the rows.
Data of the current row can be accessed through the functions on the RecordData interface.booleanskipRows(int count) Moves the cursor down the given number of rows.protected voidSupport for finding code errors where a DBRecordSet is opened but not closed.Methods inherited from class org.apache.empire.db.DBRecordData
formatValue, get, get, getArray, getBoolean, getBoolean, getDate, getDate, getDateTime, getDateTime, getDecimal, getDecimal, getDouble, getDouble, getEnum, getEnum, getEnum, getInt, getInt, getLocalDate, getLocalDate, getLocalDateTime, getLocalDateTime, getLong, getLong, getString, getString, getText, getText, getTimestamp, getTimestamp, getValue, isNull, isZero, setBeanProperties, setBeanProperties, setBeanPropertyMethods inherited from class org.apache.empire.db.DBObject
checkParamNull
-
Field Details
-
log
protected static final org.slf4j.Logger log -
context
-
-
Constructor Details
-
DBReader
Constructs an empty DBRecordSet object.- Parameters:
context- the database contextuseFieldIndexMap- flag whether to use a fieldIndexMap
-
DBReader
Constructs a default DBReader object with the fieldIndexMap enabled.- Parameters:
context- the database context
-
-
Method Details
-
getContext
Returns the current Context- Specified by:
getContextin interfaceDBContextAware- Returns:
- the database context
-
getDatabase
Returns the current DBDatabase object.- Specified by:
getDatabasein classDBObject- Returns:
- the current DBDatabase object
-
getScrollable
public boolean getScrollable() -
getFieldIndex
Returns the index value by a specified DBColumnExpr object.- Specified by:
getFieldIndexin interfaceRecordData- Specified by:
getFieldIndexin classDBRecordData- Parameters:
column- the column for which to return the index- Returns:
- the index value
-
getColumn
Get the column Expression at position- Specified by:
getColumnin interfaceRecordData- Parameters:
iColumn- field index of the column expression- Returns:
- the column expression object or null if the index is out of range
-
getFieldIndex
Returns the index value by a specified column name.- Specified by:
getFieldIndexin interfaceRecordData- Specified by:
getFieldIndexin classDBRecordData- Parameters:
column- the column name- Returns:
- the index value
-
isNull
public boolean isNull(int index) Checks wehter a column value is null Unlike the base class implementation, this class directly check the value fromt the resultset.- Specified by:
isNullin interfaceRecordData- Overrides:
isNullin classDBRecordData- Parameters:
index- index of the column- Returns:
- true if the value is null or false otherwise
-
getValue
Returns a data value identified by the column index.- Specified by:
getValuein interfaceRecordData- Specified by:
getValuein classDBRecordData- Parameters:
index- index of the column- Returns:
- the value
-
getRecordKey
Returns the record key for a type of entity- Parameters:
entity- the entity type or rowset for which to get key- Returns:
- the record key
-
getRecordId
Returns the record id for a type of entity which has a single numeric primary key- Parameters:
entity- the entity type or rowset for which to get key- Returns:
- the record id
- Throws:
InvalidArgumentException- if the entity has not a single numeric primary key
-
isOpen
public boolean isOpen()Checks if the rowset is open- Returns:
- true if the rowset is open
-
open
Opens the reader by executing the given SQL command.
After the reader is open, the reader's position is before the first record.
Use moveNext or iterator() to step through the rows.
Data of the current row can be accessed through the functions on the RecordData interface.
ATTENTION: After using the reader it must be closed using the close() method!
Usetry { ... } finally { reader.close(); }to make sure the reader is closed.
- Parameters:
cmd- the SQL-Command with cmd.getSelect()scrollable- true if the reader should be scrollable or false if not
-
open
Opens the reader by executing the given SQL command.
- Parameters:
cmd- the SQL-Command with cmd.getSelect()
-
getRecordData
Opens the reader by executing the given SQL command and moves to the first row.
If true is returned data of the row can be accessed through the functions on the RecordData interface.
This function is intended for single row queries and provided for convenience.
However it behaves exacly as calling reader.open() and reader.moveNext()
ATTENTION: After using the reader it must be closed using the close() method!
Usetry { ... } finally { reader.close(); }to make sure the reader is closed.
- Parameters:
cmd- the SQL-Command with cmd.getSelect()
-
close
public void close()Closes the DBRecordSet object, the Statement object and detach the columns.
A reader must always be closed immediately after using it.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
skipRows
public boolean skipRows(int count) Moves the cursor down the given number of rows.- Parameters:
count- the number of rows to skip- Returns:
- true if the reader is on a valid record or false otherwise
-
moveNext
public boolean moveNext()Moves the cursor down one row from its current position.- Returns:
- true if the reader is on a valid record or false otherwise
-
iterator
Returns an row iterator for this reader.
There can only be one iterator at a time.- Parameters:
maxCount- the maximum number of item that should be returned by this iterator- Returns:
- the row iterator
-
iterator
Returns an row iterator for this reader. There can only be one iterator at a time.
- Returns:
- the row iterator
-
initRecord
initializes a DBRecord object with the values of the current row. At least all primary key columns of the target rowset must be provided by this reader. This function is equivalent to calling rowset.initRecord(rec, reader) set also
DBRowSet.initRecord(DBRecordBase, RecordData));- Parameters:
rec- the record which to initialize
-
getBeanList
Returns the result of a query as a list of objects restricted to a maximum number of objects (unless maxCount is -1).- Type Parameters:
L- the list typeT- the list item type- Parameters:
list- the collection to add the objects tot- the class type of the objects in the listparent- the bean parentmaxCount- the maximum number of objects- Returns:
- the list of T
-
getBeanList
Returns the result of a query as a list of objects.- Type Parameters:
T- the list item type- Parameters:
t- the class type of the objects in the listmaxItems- the maximum number of objects- Returns:
- the list of T
-
getBeanList
Returns the result of a query as a list of objects.- Type Parameters:
T- the list item type- Parameters:
t- the class type of the objects in the list- Returns:
- the list of T
-
addXmlMeta
Moves the cursor down one row from its current position.- Specified by:
addXmlMetain classDBRecordData- Returns:
- the number of column descriptions added to the Element
-
addXmlData
Adds all children to a parent.- Specified by:
addXmlDatain classDBRecordData- Parameters:
parent- the parent element below which to search the child- Returns:
- the number of row values added to the element
-
addRows
Adds all children to a parent.- Parameters:
parent- the parent element below which to search the child- Returns:
- the number of rows added to the element
-
getXmlDictionary
returns the DBXmlDictionary that should used to generate XMLDocuments- Returns:
- the DBXmlDictionary
-
getXmlDocument
Returns a XML document with the field description an values of this record.- Specified by:
getXmlDocumentin classDBRecordData- Returns:
- the new XML Document object
-
getFieldCount
public int getFieldCount()returns the number of the elements of the colList array- Specified by:
getFieldCountin interfaceRecordData- Specified by:
getFieldCountin classDBRecordData- Returns:
- field count
-
init
Initialize the reader from an open JDBC-ResultSet- Parameters:
db- the databasecolumns- the query column expressionsrset- the JDBC-ResultSet
-
getColumnExprList
Access the column expression list- Returns:
- the column expression list
-
getResultSet
Access the JDBC-ResultSet- Returns:
- the JDBC-ResultSet
-
findFieldIndex
finds the field Index of a given column expression Internally used as helper for getFieldIndex()- Parameters:
column- the column to find- Returns:
- the index value
-
findBeanConstructor
Returns a constructor for a bean class for the set of parameters or null if no suitable constructor is found- Parameters:
beanClass- the bean class- Returns:
- a constructor for the readers columns or null if not suitable constructor is available
-
trackThisResultSet
protected void trackThisResultSet()Support for finding code errors where a DBRecordSet is opened but not closed. -
endTrackingThisResultSet
protected void endTrackingThisResultSet()Support for finding code errors where a DBRecordSet is opened but not closed. -
enableOpenResultSetTracking
public static boolean enableOpenResultSetTracking(boolean enable) Enables or disabled tracking of open ResultSets- Parameters:
enable- true to enable or false otherwise- Returns:
- the previous state of the trackOpenResultSets
-
checkOpenResultSets
public static void checkOpenResultSets()Call this if you want to check whether there are any unclosed resultsets It logs stack traces to help find piece of code where a DBReader was opened but not closed.
-