public class DBReader extends DBRecordData implements Closeable
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:
DBRecordData
)getBeanList(Class, int)
getXmlDocument()
initRecord(DBRecordBase)
Modifier and Type | Class and Description |
---|---|
class |
DBReader.DBReaderForwardIterator
This is an iterator for forward only resultsets.
|
class |
DBReader.DBReaderIterator
DBReaderIterator
Base class for DBReader interators
|
class |
DBReader.DBReaderScrollableIterator
This is an iterator for scrolling resultsets.
|
Modifier and Type | Field and Description |
---|---|
protected DBContext |
context |
protected static org.slf4j.Logger |
log |
Constructor and Description |
---|
DBReader(DBContext context)
Constructs a default DBReader object with the fieldIndexMap enabled.
|
DBReader(DBContext context,
boolean useFieldIndexMap)
Constructs an empty DBRecordSet object.
|
Modifier and Type | Method and Description |
---|---|
int |
addRows(Element parent)
Adds all children to a parent.
|
int |
addXmlData(Element parent)
Adds all children to a parent.
|
int |
addXmlMeta(Element parent)
Moves the cursor down one row from its current position.
|
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.
|
void |
close()
Closes the DBRecordSet object, the Statement object and detach the columns.
|
static boolean |
enableOpenResultSetTracking(boolean enable)
Enables or disabled tracking of open ResultSets
|
protected void |
endTrackingThisResultSet()
Support 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 found
|
protected int |
findFieldIndex(ColumnExpr column)
finds the field Index of a given column expression
Internally used as helper for getFieldIndex()
|
<T> List<T> |
getBeanList(Class<T> t)
Returns the result of a query as a list of objects.
|
<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> |
getBeanList(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).
|
DBColumnExpr |
getColumn(int iColumn)
Get the column Expression at position
|
protected DBColumnExpr[] |
getColumnExprList()
Access the column expression list
|
DBContext |
getContext()
Returns the current Context
|
DBDatabase |
getDatabase()
Returns the current DBDatabase object.
|
int |
getFieldCount()
returns the number of the elements of the colList array
|
int |
getFieldIndex(ColumnExpr column)
Returns the index value by a specified DBColumnExpr object.
|
int |
getFieldIndex(String column)
Returns the index value by a specified column name.
|
void |
getRecordData(DBCommandExpr cmd)
Opens the reader by executing the given SQL command and moves to the first row.
|
long |
getRecordId(EntityType entityType)
Returns the record id for a type of entity which has a single numeric primary key
|
Object[] |
getRecordKey(EntityType entityType)
Returns the record key for a type of entity
|
protected ResultSet |
getResultSet()
Access the JDBC-ResultSet
|
boolean |
getScrollable() |
Object |
getValue(int index)
Returns a data value identified by the column index.
|
protected DBXmlDictionary |
getXmlDictionary()
returns the DBXmlDictionary that should used to generate XMLDocuments
|
Document |
getXmlDocument()
Returns a XML document with the field description an values of this record.
|
protected void |
init(DBDatabase db,
DBColumnExpr[] columns,
ResultSet rset)
Initialize the reader from an open JDBC-ResultSet
|
void |
initRecord(DBRecordBase rec)
initializes a DBRecord object with the values of the current row.
|
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.
|
boolean |
isOpen()
Checks if the rowset is open
|
Iterator<DBRecordData> |
iterator()
Returns an row iterator for this reader.
|
Iterator<DBRecordData> |
iterator(int maxCount)
Returns an row iterator for this reader.
|
boolean |
moveNext()
Moves the cursor down one row from its current position.
|
void |
open(DBCommandExpr cmd)
Opens the reader by executing the given SQL command.
|
void |
open(DBCommandExpr cmd,
boolean scrollable)
Opens the reader by executing the given SQL command.
|
boolean |
skipRows(int count)
Moves the cursor down the given number of rows.
|
protected void |
trackThisResultSet()
Support for finding code errors where a DBRecordSet is opened but not closed.
|
formatValue, get, get, getArray, getBoolean, getBoolean, getDateTime, getDateTime, getDecimal, getDecimal, getDouble, getDouble, getEnum, getEnum, getEnum, getInt, getInt, getLocalDate, getLocalDate, getLocalDateTime, getLocalDateTime, getLong, getLong, getString, getString, getText, getValue, getValue, getValues, isNull, setBeanProperties, setBeanProperties, setBeanProperty
checkParamNull
protected static final org.slf4j.Logger log
protected final DBContext context
public DBReader(DBContext context, boolean useFieldIndexMap)
useFieldIndexMap
- public DBReader(DBContext context)
public DBContext getContext()
getContext
in interface DBContextAware
public final DBDatabase getDatabase()
getDatabase
in class DBObject
public boolean getScrollable()
public int getFieldIndex(ColumnExpr column)
getFieldIndex
in interface RecordData
getFieldIndex
in class DBRecordData
column
- the column for which to return the indexpublic DBColumnExpr getColumn(int iColumn)
getColumn
in interface RecordData
iColumn
- field index of the column expressionpublic int getFieldIndex(String column)
getFieldIndex
in interface RecordData
getFieldIndex
in class DBRecordData
column
- the column namepublic boolean isNull(int index)
isNull
in interface RecordData
isNull
in class DBRecordData
index
- index of the columnpublic Object getValue(int index)
getValue
in interface RecordData
getValue
in class DBRecordData
index
- index of the columnpublic Object[] getRecordKey(EntityType entityType)
entityType
- the entity type or rowset for which to get keypublic long getRecordId(EntityType entityType)
entityType
- the entity type or rowset for which to get keyInvalidArgumentException
- if the entity has not a single numeric primary keypublic boolean isOpen()
public void open(DBCommandExpr cmd, boolean scrollable)
ATTENTION: After using the reader it must be closed using the close() method!
Use
try { ... } finally { reader.close(); }to make sure the reader is closed.
cmd
- the SQL-Command with cmd.getSelect()scrollable
- true if the reader should be scrollable or false if notpublic final void open(DBCommandExpr cmd)
cmd
- the SQL-Command with cmd.getSelect()public void getRecordData(DBCommandExpr cmd)
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!
Use
try { ... } finally { reader.close(); }to make sure the reader is closed.
cmd
- the SQL-Command with cmd.getSelect()public void close()
close
in interface Closeable
close
in interface AutoCloseable
public boolean skipRows(int count)
count
- the number of rows to skippublic boolean moveNext()
public Iterator<DBRecordData> iterator(int maxCount)
maxCount
- the maximum number of item that should be returned by this iteratorpublic final Iterator<DBRecordData> iterator()
Returns an row iterator for this reader. There can only be one iterator at a time.
public void initRecord(DBRecordBase rec)
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, DBRecordData)
);
rec
- the record which to initializepublic <L extends List<T>,T> L getBeanList(L list, Class<T> t, Object parent, int maxCount)
list
- the collection to add the objects tot
- the class type of the objects in the listmaxCount
- the maximum number of objectspublic final <T> List<T> getBeanList(Class<T> t, int maxItems)
t
- the class type of the objects in the listmaxItems
- the maximum number of objectspublic final <T> List<T> getBeanList(Class<T> t)
t
- the class type of the objects in the listpublic int addXmlMeta(Element parent)
addXmlMeta
in class DBRecordData
public int addXmlData(Element parent)
addXmlData
in class DBRecordData
parent
- the parent element below which to search the childpublic int addRows(Element parent)
parent
- the parent element below which to search the childprotected DBXmlDictionary getXmlDictionary()
public Document getXmlDocument()
getXmlDocument
in class DBRecordData
public int getFieldCount()
getFieldCount
in interface RecordData
getFieldCount
in class DBRecordData
protected void init(DBDatabase db, DBColumnExpr[] columns, ResultSet rset)
db
- the databasecolumns
- the query column expressionsrset
- the JDBC-ResultSetprotected final DBColumnExpr[] getColumnExprList()
protected final ResultSet getResultSet()
protected int findFieldIndex(ColumnExpr column)
protected Constructor<?> findBeanConstructor(Class<?> beanClass)
beanClass
- the bean classprotected void trackThisResultSet()
protected void endTrackingThisResultSet()
public static boolean enableOpenResultSetTracking(boolean enable)
enable
- true to enable or false otherwisepublic 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.
Copyright © 2008–2023 Apache Software Foundation. All rights reserved.