org.ujorm.orm
Class Query<UJO extends OrmUjo>

java.lang.Object
  extended by org.ujorm.orm.Query<UJO>
All Implemented Interfaces:
Iterable<UJO>

public class Query<UJO extends OrmUjo>
extends Object
implements Iterable<UJO>

ORM query.


Constructor Summary
Query(MetaTable table, Criterion<UJO> criterion)
          Create new ORM query without a session.
Query(MetaTable table, Criterion<UJO> criterion, Session session)
          Create new ORM query.
 
Method Summary
 Query<UJO> addColumn(Key<UJO,?> column)
          Set the one column to reading from database table.
 void addCriterion(Criterion<UJO> criterion)
          Add a new Criterion.
protected  void addMissingColumn(ColumnWrapper column, boolean addChilds, boolean checkDuplicities)
          Add a missing column.
 Query<UJO> addOrderBy(Key<UJO,?> key)
          Add an item to the end of order list.
 boolean exists()
          The method performs a new database request and returns result of the function UjoIterator.hasNext().
 ColumnWrapper[] getColumnArray()
          Create a new column List.
 List<ColumnWrapper> getColumns()
          Get Column Collection
 long getCount()
          Returns a count of the items
 Criterion<UJO> getCriterion()
          Criterion
 CriterionDecoder getDecoder()
          Method builds and retuns a criterion decoder.
protected  List<ColumnWrapper> getDefaultColumns()
          Returns all direct columns of the base table.
 int getFetchSize()
          Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed.
 int getLimit()
          The max row count for the resultset.
 long getLimitedCount()
          Returns a database row count along a current limit and offset attribues.
 long getOffset()
          Get the first row to retrieve (offset).
 Key<UJO,?>[] getOrderAsArray()
          Get the order item array.
 List<Key<UJO,?>> getOrderBy()
          Get the order item list.
 Session getSession()
          Session
 SqlParameters getSqlParameters()
          Get a SQL parameters of the Native view
 PreparedStatement getStatement()
          Create a PreparedStatement including assigned parameter values
 String getStatementInfo()
          Get the SQL statement or null, of statement is not known yet.
 MetaTable getTableModel()
          Table Type
 boolean isDistinct()
          Select DISTINCT for a unique row result
 boolean isLimit()
          The max row count for the resultset.
 boolean isLockRequest()
          Pessimistic lock request
 boolean isOffset()
          Has this Query an offset?
 UjoIterator<UJO> iterate()
          Deprecated. Use iterator() instead of.
 UjoIterator<UJO> iterator()
          Create a new iterator by the query.
 List<UJO> list()
          There is recommended to use the method iterator() rather.
 List<UJO> list(int depth)
          Create list and Load all lazy values for the current parameter recursively until optional depth.
<T> Map<T,UJO>
map()
          Returns an instance of the Map where the key is DB table primary key and a value is the row.
<T> Map<T,UJO>
map(Key<UJO,T> mapKey, Map<T,UJO> result)
          Returns an instance of the Map where the key is DB table primary key and a value is the row.
 Query<UJO> orderBy(Collection<Key> orderItems)
          Set an order of the rows by a SQL ORDER BY phrase.
 Query<UJO> orderBy(Key<UJO,?> orderItem)
          Set an order of the rows by a SQL ORDER BY phrase.
 Query<UJO> orderBy(Key<UJO,?> orderItem1, Key<UJO,?> orderItem2)
          Set an order of the rows by a SQL ORDER BY phrase.
 Query<UJO> orderBy(Key<UJO,?> orderItem1, Key<UJO,?> orderItem2, Key<UJO,?> orderItem3)
          Set an order of the rows by a SQL ORDER BY phrase.
 Query<UJO> orderByMany(Key... orderItems)
          Set an order of the rows by a SQL ORDER BY phrase.
 MetaColumn readOrderColumn(int i)
          Returns an order column.
 Query<UJO> setColumn(Key<UJO,?> column)
          Set the one column to reading from database table.
 Query<UJO> setColumns(boolean addPrimaryKey, boolean addChilds, Key... columns)
          Set an list of required columns to reading from database table.
 Query<UJO> setColumns(boolean addPrimaryKey, Key... columns)
          Set an list of required columns to reading from database table.
 Query<UJO> setColumns(Collection<ColumnWrapper> columns)
          Set an list of required columns to reading from database table.
 Query<UJO> setCriterion(Criterion<UJO> criterion)
          Set a new Criterion.
 Query<UJO> setDistinct()
          Select DISTINCT for a unique row result
 Query<UJO> setDistinct(boolean distinct)
          Select DISTINCT for a unique row result
 Query<UJO> setFetchSize(int fetchSize)
          Retrieves the number of result set rows that is the default fetch size for ResultSet objects generated from this Statement object.
 Query<UJO> setLimit(int limit)
          The max row count for the resultset.
 Query<UJO> setLimit(int limit, long offset)
          Set a limit and offset.
 Query<UJO> setLockRequest()
          Set pessimistic lock request.
 Query<UJO> setLockRequest(boolean lockRequest)
          Pessimistic lock request.
 Query<UJO> setMaxRows(int limit)
          Deprecated. 
 Query<UJO> setOffset(int offset)
          Get the first row to retrieve (offset).
 Query<UJO> setOrder(Key... order)
          Deprecated. Use the orderByMany(org.ujorm.Key[]) method instead of
<ITEM> void
setParameter(Key<UJO,ITEM> key, ITEM value)
           
 Query<UJO> setSession(Session session)
          An open session must be assigned before executing a database request.
 Query<UJO> setSqlParameters(Object... parameters)
          Set a SQL parameters of the Native View
 Query<UJO> setSqlParameters(SqlParameters sqlParameters)
          Set a SQL parameters of the Native View
 String toString()
           
 UJO uniqueResult()
          Returns a unique result or null if no result item (database row) was found.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Query

public Query(MetaTable table,
             Criterion<UJO> criterion,
             Session session)
Create new ORM query. A one from argument is mandatory.

Parameters:
table - Table model is mandatory
criterion - If criterion is null, then the ForAll criterion is used.
session - Session

Query

public Query(MetaTable table,
             Criterion<UJO> criterion)
Create new ORM query without a session. An open session must be assigned before executing a database request.

Parameters:
table - Table model
criterion - If criterion is null, then a TRUE constant criterion is used.
See Also:
setSession(org.ujorm.orm.Session)
Method Detail

setSession

public Query<UJO> setSession(Session session)
An open session must be assigned before executing a database request.


getLimitedCount

public long getLimitedCount()
Returns a database row count along a current limit and offset attribues.

See Also:
getCount()

getCount

public long getCount()
Returns a count of the items

See Also:
getLimitedCount()

setParameter

public <ITEM> void setParameter(Key<UJO,ITEM> key,
                                ITEM value)

addCriterion

public void addCriterion(Criterion<UJO> criterion)
                  throws IllegalArgumentException
Add a new Criterion.

Parameters:
criterion - Parameter is mandatory and must not be NULL.
Throws:
IllegalArgumentException
See Also:
createQuery(Criterion), setCriterion(..)

setCriterion

public Query<UJO> setCriterion(Criterion<UJO> criterion)
Set a new Criterion. There is recommended

Parameters:
criterion - The value NULL is allowed because the value is replaced internally to expression Criterion.where(true). A MetaTable parameter must be specified in the constuctor for this case.
See Also:
createQuery(Criteron), addCriterion(..)

getCriterion

public Criterion<UJO> getCriterion()
Criterion


getDecoder

public final CriterionDecoder getDecoder()
Method builds and retuns a criterion decoder. The new decoder is cached to a next order by change.


getSession

public Session getSession()
Session


getTableModel

public MetaTable getTableModel()
Table Type


getColumns

public List<ColumnWrapper> getColumns()
Get Column Collection


getDefaultColumns

protected List<ColumnWrapper> getDefaultColumns()
Returns all direct columns of the base table.


getColumnArray

public ColumnWrapper[] getColumnArray()
Create a new column List.


iterator

public UjoIterator<UJO> iterator()
Create a new iterator by the query. The result iterator can be used in the Java statement for(...) directly.
NOTE: The items can be iterated inside a database transaction only, in other case call the next expression:
iterator().toList()

Specified by:
iterator in interface Iterable<UJO extends OrmUjo>
See Also:
uniqueResult(), exists()

iterate

@Deprecated
public final UjoIterator<UJO> iterate()
Deprecated. Use iterator() instead of.

Create a new iterator by the query.

See Also:
iterator()

list

public List<UJO> list()
There is recommended to use the method iterator() rather. The method calls internally the next statement:
iterator().toList()

See Also:
iterator(), OrmTools.loadLazyValues(java.lang.Iterable, int), OrmTools.loadLazyValuesAsBatch(org.ujorm.orm.Query)

map

public <T> Map<T,UJO> map()
Returns an instance of the Map where the key is DB table primary key and a value is the row. The method calls internally the next statement:
iterator()

See Also:
iterator()

map

public <T> Map<T,UJO> map(Key<UJO,T> mapKey,
                          Map<T,UJO> result)
Returns an instance of the Map where the key is DB table primary key and a value is the row. The method calls internally the next statement:
iterator()

Parameters:
mapKey - Ujo Key for the mapKey
See Also:
iterator()

list

public List<UJO> list(int depth)
Create list and Load all lazy values for the current parameter recursively until optional depth.
Performance note: all lazy values are loaded using the one more SQL statement per one relation Key. The method can consume a lot of memory in dependence on the database row count and content of the Criteron.

Parameters:
depth - The object resursion depth where value 0 means: do not any lazy loading. level. The current release supports only values: 0 and 1.
See Also:
iterator(), OrmTools.loadLazyValues(java.lang.Iterable, int), OrmTools.loadLazyValuesAsBatch(org.ujorm.orm.Query)

uniqueResult

public UJO uniqueResult()
                                throws NoSuchElementException
Returns a unique result or null if no result item (database row) was found.

Throws:
NoSuchElementException - Result is not unique.
See Also:
iterator(), exists()

exists

public boolean exists()
The method performs a new database request and returns result of the function UjoIterator.hasNext(). The result TRUE means the query covers one item (database row) at least.

See Also:
iterator(), uniqueResult()

getOrderBy

public final List<Key<UJO,?>> getOrderBy()
Get the order item list. The method returns a not null result always.


getOrderAsArray

public final Key<UJO,?>[] getOrderAsArray()
Get the order item array. The method returns a not null result always.


setOrder

@Deprecated
public Query<UJO> setOrder(Key... order)
Deprecated. Use the orderByMany(org.ujorm.Key[]) method instead of

Set an order of the rows by a SQL ORDER BY phrase.

See Also:
orderByMany(org.ujorm.Key[])

orderBy

public Query<UJO> orderBy(Key<UJO,?> orderItem)
Set an order of the rows by a SQL ORDER BY phrase.


orderBy

public Query<UJO> orderBy(Key<UJO,?> orderItem1,
                          Key<UJO,?> orderItem2)
Set an order of the rows by a SQL ORDER BY phrase.


orderBy

public Query<UJO> orderBy(Key<UJO,?> orderItem1,
                          Key<UJO,?> orderItem2,
                          Key<UJO,?> orderItem3)
Set an order of the rows by a SQL ORDER BY phrase.


orderByMany

public final Query<UJO> orderByMany(Key... orderItems)
Set an order of the rows by a SQL ORDER BY phrase.
WARNING: the parameters are not type checked.


addColumn

public Query<UJO> addColumn(Key<UJO,?> column)
                                    throws IllegalArgumentException
Set the one column to reading from database table. Other columns will return a default value, no exception will be throwed.
WARNING 1: assigning an column from a view is forbidden.
WARNING 2: the parameters are not type checked in compile time, use setColumn(..) and addColumn() for this feature.
WARNING 3: assigning an column from a view is forbidden.

Parameters:
column - A Property to select. A composite Property is allowed however only the first item will be used.
Throws:
IllegalArgumentException
See Also:
setColumn(Property)

setColumn

public Query<UJO> setColumn(Key<UJO,?> column)
                                    throws IllegalArgumentException
Set the one column to reading from database table. Other columns will return a default value, no exception will be throwed.
WARNING: assigning an column from a view is forbidden.

Parameters:
column - A Property to select. A composite Property is allowed however only the first item will be used.
Throws:
IllegalArgumentException
See Also:
addColumn(Property)

setColumns

public final Query<UJO> setColumns(boolean addPrimaryKey,
                                   Key... columns)
                                           throws IllegalArgumentException
Set an list of required columns to reading from database table. Other columns (out of the list) will return a default value, no exception will be throwed.

Parameters:
addPrimaryKey - If the column list does not contains a primary key then the one can be included.
columns - A Key list including a compositer one to database select. The method does not check collumn duplicities.
Throws:
IllegalArgumentException
See Also:
setColumn(Property), addColumn(Property)

setColumns

public final Query<UJO> setColumns(Collection<ColumnWrapper> columns)
                                           throws IllegalArgumentException
Set an list of required columns to reading from database table. Other columns (out of the list) will return a default value, no exception will be throwed.
WARNING 1: the parameters are not type checked in compile time, use setColumn(..) and addColumn() for this feature.
WARNING 2: assigning an column from a view is forbidden.

Parameters:
columns - A Key list including a compositer one to database select. The method does not check collumn duplicities.
Throws:
IllegalArgumentException
See Also:
setColumn(Property), addColumn(Property)

setColumns

public final Query<UJO> setColumns(boolean addPrimaryKey,
                                   boolean addChilds,
                                   Key... columns)
                                           throws IllegalArgumentException
Set an list of required columns to reading from database table. Other columns (out of the list) will return a default value, no exception will be throwed.
WARNING 1: the parameters are not type checked in compile time, use setColumn(..) and addColumn() for this feature.
WARNING 2: assigning an column from a view is forbidden.

Parameters:
addPrimaryKey - If the column list doesn't contain a primary key of the base Entity then the one will be included.
addChilds - Add all childs of the all foreign keys.
columns - A Key list including a compositer one to database select. The method does not check collumn duplicities.
Throws:
IllegalArgumentException
See Also:
setColumn(Property), addColumn(Property)

addMissingColumn

protected void addMissingColumn(ColumnWrapper column,
                                boolean addChilds,
                                boolean checkDuplicities)
Add a missing column. The method is for an internal use.

Parameters:
column - Add the column for case it is missing in the column list
addChilds - Add all childs of the foreign key.
checkDuplicities - Check a duplicity column

orderBy

public Query<UJO> orderBy(Collection<Key> orderItems)
Set an order of the rows by a SQL ORDER BY phrase. WARNING: the list items are not type checked. If you need an item chacking, use the method addOrderBy(org.ujorm.Key) rather.

See Also:
addOrderBy(org.ujorm.Key)

addOrderBy

public Query<UJO> addOrderBy(Key<UJO,?> key)
Add an item to the end of order list.


readOrderColumn

public MetaColumn readOrderColumn(int i)
                           throws IllegalStateException
Returns an order column. A method for an internal use only.

Throws:
IllegalStateException

isOffset

public boolean isOffset()
Has this Query an offset?


getOffset

public final long getOffset()
Get the first row to retrieve (offset). Default value is 0.


setOffset

public Query<UJO> setOffset(int offset)
Get the first row to retrieve (offset). Default value is 0.

See Also:
#setLimit(int, int)

isLimit

public final boolean isLimit()
The max row count for the resultset. The value -1 means no change, value 0 means no limit (or a default value by the JDBC driver implementation.

See Also:
getLimit()

getLimit

public final int getLimit()
The max row count for the resultset. The value -1 means no change, value 0 means no limit (or a default value by the JDBC driver implementation.

See Also:
isLimit()

setLimit

public Query<UJO> setLimit(int limit)
The max row count for the resultset. The value -1 means no change, value 0 means no limit (or a default value by the JDBC driver implementation.

See Also:
Statement.setMaxRows(int)

setLimit

public Query<UJO> setLimit(int limit,
                           long offset)
Set a limit and offset.

Parameters:
limit - The max row count for the resultset. The value -1 means no change, value 0 means no limit (or a default value by the JDBC driver implementation.
offset - Get the first row to retrieve (offset). Default value is 0.
See Also:
setLimit(int), setOffset(int)

setMaxRows

@Deprecated
public final Query<UJO> setMaxRows(int limit)
Deprecated. 

Use the method setLimit(int) rather.

See Also:
setLimit(int)

getFetchSize

public int getFetchSize()
Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed.

See Also:
Statement.getFetchSize()

setFetchSize

public Query<UJO> setFetchSize(int fetchSize)
Retrieves the number of result set rows that is the default fetch size for ResultSet objects generated from this Statement object.

See Also:
Statement.setFetchSize(int)

getStatement

public PreparedStatement getStatement()
Create a PreparedStatement including assigned parameter values


getStatementInfo

public String getStatementInfo()
Get the SQL statement or null, of statement is not known yet.


isLockRequest

public boolean isLockRequest()
Pessimistic lock request


setLockRequest

public Query<UJO> setLockRequest(boolean lockRequest)
Pessimistic lock request. A default value is false.

See Also:
HsqldbDialect

setLockRequest

public Query<UJO> setLockRequest()
Set pessimistic lock request. A default value is false.

See Also:
HsqldbDialect

toString

public String toString()
Overrides:
toString in class Object

isDistinct

public boolean isDistinct()
Select DISTINCT for a unique row result


setDistinct

public Query<UJO> setDistinct(boolean distinct)
Select DISTINCT for a unique row result


setDistinct

public Query<UJO> setDistinct()
Select DISTINCT for a unique row result


getSqlParameters

public SqlParameters getSqlParameters()
Get a SQL parameters of the Native view


setSqlParameters

public Query<UJO> setSqlParameters(SqlParameters sqlParameters)
                                           throws IllegalArgumentException
Set a SQL parameters of the Native View

Throws:
IllegalArgumentException - The SQL parameters can be used for the VIEW only
See Also:
View

setSqlParameters

public Query<UJO> setSqlParameters(Object... parameters)
                                           throws IllegalArgumentException
Set a SQL parameters of the Native View

Throws:
IllegalArgumentException - The SQL parameters can be used for the VIEW only
See Also:
View


Copyright 2013, Pavel Ponec