abstract class AbstractQuery extends java.lang.Object implements Query
Query
interface. This class
implements all methods and also adds protected methods for generating the
main and count queries. Implementors extending this class only needs
to add methods for executing the query.Modifier and Type | Field and Description |
---|---|
private java.util.Map<java.lang.Class<?>,AutoJoiner<?,?>> |
autoJoiners |
private static boolean |
debugEnabled
So we don't always have to call logSql.debug()
|
private boolean |
distinct
If the query should only return the same item once or not.
|
private boolean |
failSafe
If transactions must be able to continue after an SQL failure or not.
|
private int |
firstResult
The index of the first result that should be returned by the query.
|
private java.util.List<Expression> |
groups
List of group expressions added to the query.
|
private boolean |
hasAutoJoined |
private java.util.List<Restriction> |
havings
List of having restrictions added to the query.
|
private static java.lang.Object |
HOLDER |
private boolean |
isCounting
If the
Query.count(DbControl) method is executing or not. |
private java.util.List<Join> |
joins
List of joins added to the query.
|
private static org.slf4j.Logger |
logSql
Log all SQL statements.
|
private int |
maxResults
The maximum number of items that should be returned by the query.
|
private java.util.List<Order> |
orders
List of orders added to the query.
|
private java.util.Map<java.lang.String,QueryParameter> |
parameters
Parameters values added to the query.
|
private java.util.List<Expression> |
permanentGroups
List of permanent group expressions added to the query.
|
private java.util.List<Restriction> |
permanentHavings
List of permanent having restrictions added to the query.
|
private java.util.List<Join> |
permanentJoins
List of permanent joins added to the query.
|
private java.util.List<Order> |
permanentOrders
List of permanent orders added to the query.
|
private java.util.List<Restriction> |
permanentRestrictions
List of permanent restrictions added to the query.
|
private java.util.List<Select> |
permanentSelects
List of permanent selections added to the query.
|
private QuerySection |
querySection
The section of the query that is currently beeing built.
|
private java.util.List<Restriction> |
restrictions
List of restrictions added to the query.
|
private boolean |
returnTotalCount
If the query should calculate the total number of matching items
in the case that limits have been set with
firstResult
or maxResults . |
private java.lang.String |
rootEntity
The root enitity.
|
private java.util.List<Select> |
selects
List of selections added to the query.
|
private java.util.List<Join> |
temporaryJoins
Tempoarary joins, that are automatically cleared after each build.
|
private java.util.List<Select> |
temporarySelects
Tempoarary selections, that are automatically cleared after each build.
|
private java.util.List<java.lang.String> |
thetaJoinConditions
Temporary list to hold theta join conditions if needed by the database.
|
Constructor and Description |
---|
AbstractQuery(java.lang.String rootEntity)
Create a new query.
|
Modifier and Type | Method and Description |
---|---|
void |
addAutoJoiner(AutoJoiner<?,?> autoJoiner) |
private void |
addChildElements(java.util.Map<QueryElement,java.lang.Object> all,
QueryElement root) |
private int |
appendGroups(java.lang.StringBuilder ql,
DbControl dc,
java.util.List<Expression> groups,
int alreadyAppended) |
private int |
appendHavings(java.lang.StringBuilder ql,
DbControl dc,
java.util.List<Restriction> havings,
int alreadyAppended) |
private int |
appendJoins(java.lang.StringBuilder ql,
DbControl dc,
java.util.List<Join> joins,
boolean useThetaJoin,
int alreadyAppended) |
private int |
appendOrders(java.lang.StringBuilder ql,
DbControl dc,
java.util.List<Order> orders,
int alreadyAppended) |
private int |
appendRestrictions(java.lang.StringBuilder ql,
DbControl dc,
java.util.List<Restriction> restrictions,
int alreadyAppended) |
private int |
appendSelects(java.lang.StringBuilder ql,
DbControl dc,
java.util.List<Select> selects,
int alreadyAppended) |
private void |
autoJoin() |
(package private) java.lang.String |
getCountQuery(DbControl dc,
boolean autoJoin)
Build the query string for returning the total count.
|
int |
getFirstResult()
The number of the row the query should start returning.
|
(package private) java.lang.String |
getMainQuery(DbControl dc,
boolean autoJoin)
Build the main query string.
|
int |
getMaxResults()
The maximum number of rows returned by the query.
|
java.util.Set<java.lang.String> |
getParameterNames()
Get a set with the names for all parameters in this
query.
|
(package private) java.util.Map<java.lang.String,QueryParameter> |
getParameters()
Get all query parameters.
|
(package private) Type |
getParameterType(java.lang.String name)
Get the type of the parameter, or null if not known.
|
(package private) java.lang.Object |
getParameterValue(java.lang.String name)
Get the value of the named parameter.
|
(package private) java.util.List<Select> |
getPermanentSelects()
Get all permanent selects.
|
QueryParameter |
getQueryParameter(java.lang.String name)
Get parameter information for the parameter with given name.
|
QuerySection |
getQuerySection()
Get the current section of the query that is beeing built.
|
(package private) java.util.List<Select> |
getSelects()
Get all non-permanent selects.
|
void |
group(Expression expression)
Add an expression query element to the group list.
|
void |
groupPermanent(Expression expression)
Permanently add an expression query element to the group list.
|
boolean |
hasParameterValue(java.lang.String name)
Check if a value for the specified parameter has been set or not.
|
void |
having(Restriction restriction)
Add a restriction query element to the having list.
|
void |
havingPermanent(Restriction restriction)
Permanently add a restriction query element to the having list.
|
boolean |
isCounting()
Check if the query is executing the
Query.count(DbControl) method or not |
boolean |
isDistinct()
If this query returns distinct results of not.
|
boolean |
isFailSafe()
Check if a failure of this query should still allow the current transaction
to continue.
|
boolean |
isReadonly()
If this query is readonly and cannot be structurally modified.
|
boolean |
isReturningTotalCount()
If this query returns the total count or not.
|
void |
join(Join join)
Add a join query element to the join list.
|
void |
joinPermanent(Join join)
Permanently add a join query element to the join list.
|
void |
order(Order order)
Add an ordering query element to the orderby list.
|
void |
orderPermanent(Order order)
Permanently add an ordering query element to the orderby list.
|
protected java.lang.String |
postProcessQuery(java.lang.String query)
Allows a subclass to modify the query.
|
void |
reset()
Resets the query, allowing you to add more restrictions, joins, order objects
and parameters.
|
(package private) void |
resetTemporary() |
void |
restrict(Restriction restriction)
Add a restriction query element to the restriction list.
|
void |
restrictPermanent(Restriction restriction)
Permanently add a restriction query element to the restriction list.
|
void |
select(Select select)
Add a selection query element to the selection list.
|
void |
selectPermanent(Select select)
Permanently add a selection query element to the selection list.
|
(package private) void |
setCounting(boolean isCounting)
Set the counting status of the query.
|
void |
setDistinct(boolean flag)
Specify if the query should only return distinct rows.
|
void |
setFailSafe(boolean failSafe)
Set this flag to ensure that the transaction used to execute the query
is allowed to continue even after a failure at the SQL level of this query.
|
void |
setFirstResult(int firstResult)
Specify that the query should start returning rows from the
specified row number. 0 = start returning from the first row.
|
void |
setMaxResults(int maxResults)
Specify that the query should at most return the specified number of
rows.
|
private void |
setParameter(java.lang.String name,
boolean permanent,
java.lang.Object value,
Type valueType) |
void |
setParameter(java.lang.String name,
java.lang.Object value,
Type valueType)
Set the value of a query parameter.
|
void |
setPermanentParameter(java.lang.String name,
java.lang.Object value,
Type valueType)
Permanently set the value of a query parameter.
|
void |
setReturnTotalCount(boolean flag)
Specify if the query should return a count for the total number
of items that would have been returned if the
Query.setFirstResult(int)
and Query.setMaxResults(int) had been disabled, or if the results are
loaded by an iterator where the number of rows not are known beforehand. |
(package private) void |
temporaryJoin(Join join) |
(package private) void |
temporarySelect(Select select) |
java.lang.String |
toQl(DbControl dc)
Generate the query string that is going to be sent to the
underlying query system (eg.
|
java.lang.String |
toString() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
count, getQueryType, getRootAlias
private static final org.slf4j.Logger logSql
private static final boolean debugEnabled
private boolean hasAutoJoined
private java.util.Map<java.lang.Class<?>,AutoJoiner<?,?>> autoJoiners
private java.util.List<Select> selects
private java.util.List<Select> permanentSelects
private java.util.List<Select> temporarySelects
private final java.lang.String rootEntity
private java.util.List<Join> joins
private java.util.List<Join> permanentJoins
private java.util.List<Join> temporaryJoins
private java.util.List<Restriction> restrictions
private java.util.List<Restriction> permanentRestrictions
private java.util.List<Expression> groups
private java.util.List<Expression> permanentGroups
private java.util.List<Restriction> havings
private java.util.List<Restriction> permanentHavings
private java.util.List<Order> orders
private java.util.List<Order> permanentOrders
private java.util.Map<java.lang.String,QueryParameter> parameters
private int firstResult
private int maxResults
private boolean returnTotalCount
firstResult
or maxResults
.private boolean distinct
private java.util.List<java.lang.String> thetaJoinConditions
private boolean isCounting
Query.count(DbControl)
method is executing or not.private QuerySection querySection
private boolean failSafe
private static final java.lang.Object HOLDER
AbstractQuery(java.lang.String rootEntity)
rootEntity
- The root entity of the querypublic void addAutoJoiner(AutoJoiner<?,?> autoJoiner) throws InvalidDataException
addAutoJoiner
in interface Query
InvalidDataException
public void select(Select select) throws InvalidDataException, BaseException
Query
select
in interface Query
select
- The query element to addInvalidDataException
BaseException
Query.selectPermanent(Select)
public void selectPermanent(Select select) throws InvalidDataException, BaseException
Query
Query.reset()
method.selectPermanent
in interface Query
select
- The query element to addInvalidDataException
BaseException
Query.select(Select)
public void join(Join join) throws InvalidDataException, BaseException
Query
join
in interface Query
join
- The query element to addInvalidDataException
BaseException
Query.joinPermanent(Join)
public void joinPermanent(Join join) throws InvalidDataException, BaseException
Query
Query.reset()
method.joinPermanent
in interface Query
join
- The query element to addInvalidDataException
BaseException
Query.join(Join)
public void restrict(Restriction restriction) throws InvalidDataException, BaseException
Query
restrict
in interface Query
restriction
- The query element to addInvalidDataException
BaseException
Query.restrictPermanent(Restriction)
public void restrictPermanent(Restriction restriction) throws InvalidDataException, BaseException
Query
Query.reset()
method.restrictPermanent
in interface Query
restriction
- The query element to addInvalidDataException
BaseException
Query.restrict(Restriction)
public void group(Expression expression) throws InvalidDataException, BaseException
Query
group
in interface Query
expression
- The query element to addInvalidDataException
BaseException
Query.groupPermanent(Expression)
public void groupPermanent(Expression expression) throws InvalidDataException, BaseException
Query
Query.reset()
method.groupPermanent
in interface Query
expression
- The query element to addInvalidDataException
BaseException
Query.group(Expression)
public void having(Restriction restriction) throws InvalidDataException, BaseException
Query
having
in interface Query
restriction
- The query element to addInvalidDataException
BaseException
Query.havingPermanent(Restriction)
public void havingPermanent(Restriction restriction) throws InvalidDataException, BaseException
Query
Query.reset()
method.havingPermanent
in interface Query
restriction
- The query element to addInvalidDataException
BaseException
Query.having(Restriction)
public void order(Order order) throws InvalidDataException, BaseException
Query
order
in interface Query
order
- The query element to addInvalidDataException
BaseException
Query.orderPermanent(Order)
public void orderPermanent(Order order) throws InvalidDataException, BaseException
Query
Query.reset()
method.orderPermanent
in interface Query
order
- The query element to addInvalidDataException
BaseException
Query.order(Order)
private void setParameter(java.lang.String name, boolean permanent, java.lang.Object value, Type valueType)
public void setParameter(java.lang.String name, java.lang.Object value, Type valueType) throws InvalidDataException, BaseException
Query
Query.setPermanentParameter(String, Object, Type)
method, an InvalidDataException
is thrown.setParameter
in interface Query
name
- The name of the parametervalue
- The value of the parametervalueType
- The type of the value, or null if not neededInvalidDataException
- If the value of the parameter has already
been permanently setBaseException
Query.setPermanentParameter(String, Object, Type)
public void setPermanentParameter(java.lang.String name, java.lang.Object value, Type valueType) throws InvalidDataException, BaseException
Query
InvalidDataException
is thrown.setPermanentParameter
in interface Query
name
- The name of the parametervalue
- The value of the parametervalueType
- The type of the value, or null if not neededInvalidDataException
- If the value of the parameter has already
been permanently setBaseException
Query.setParameter(String, Object, Type)
public boolean hasParameterValue(java.lang.String name)
Query
hasParameterValue
in interface Query
public java.util.Set<java.lang.String> getParameterNames()
Query
getParameterNames
in interface Query
public QueryParameter getQueryParameter(java.lang.String name)
Query
getQueryParameter
in interface Query
name
- The name of the parameterpublic void setFirstResult(int firstResult)
Query
Query.setMaxResults(int)
option.setFirstResult
in interface Query
firstResult
- The number of the first row starting at 0Query.setMaxResults(int)
public int getFirstResult()
Query
getFirstResult
in interface Query
Query.setFirstResult(int)
public void setMaxResults(int maxResults)
Query
Query.setFirstResult(int)
option.setMaxResults
in interface Query
maxResults
- The maximum number of rows to returnQuery.setFirstResult(int)
public int getMaxResults()
Query
getMaxResults
in interface Query
Query.setMaxResults(int)
public void setReturnTotalCount(boolean flag)
Query
Query.setFirstResult(int)
and Query.setMaxResults(int)
had been disabled, or if the results are
loaded by an iterator where the number of rows not are known beforehand.setReturnTotalCount
in interface Query
flag
- TRUE if the query should return the total count, FALSE otherwisepublic boolean isReturningTotalCount()
Query
isReturningTotalCount
in interface Query
Query.setReturnTotalCount(boolean)
public void setDistinct(boolean flag)
Query
setDistinct
in interface Query
flag
- TRUE if the query should return distinct results, FALSE otherwisepublic boolean isDistinct()
Query
isDistinct
in interface Query
Query.setDistinct(boolean)
public boolean isReadonly()
Query
Query.reset()
would unlock the query and allow it to be modified again.isReadonly
in interface Query
public void reset()
public boolean isCounting()
Query
Query.count(DbControl)
method or notisCounting
in interface Query
count
method, FALSE otherwisepublic QuerySection getQuerySection()
Query
QueryElement
:s if they need
different syntax in different sections.getQuerySection
in interface Query
public java.lang.String toQl(DbControl dc)
Query
public void setFailSafe(boolean failSafe)
Query
setFailSafe
in interface Query
failSafe
- TRUE to ensure that the transaction is allowed to continue,
FALSE if it doesn't matterpublic boolean isFailSafe()
Query
isFailSafe
in interface Query
public java.lang.String toString()
toString
in class java.lang.Object
void setCounting(boolean isCounting)
void temporarySelect(Select select) throws InvalidDataException, BaseException
InvalidDataException
BaseException
void temporaryJoin(Join join) throws InvalidDataException, BaseException
InvalidDataException
BaseException
void resetTemporary()
java.util.List<Select> getSelects()
java.util.List<Select> getPermanentSelects()
java.util.Map<java.lang.String,QueryParameter> getParameters()
java.lang.Object getParameterValue(java.lang.String name)
name
- The name of the parameterType getParameterType(java.lang.String name)
java.lang.String getMainQuery(DbControl dc, boolean autoJoin) throws BaseException
BaseException
java.lang.String getCountQuery(DbControl dc, boolean autoJoin) throws BaseException
BaseException
protected java.lang.String postProcessQuery(java.lang.String query)
private int appendSelects(java.lang.StringBuilder ql, DbControl dc, java.util.List<Select> selects, int alreadyAppended) throws BaseException
BaseException
private int appendJoins(java.lang.StringBuilder ql, DbControl dc, java.util.List<Join> joins, boolean useThetaJoin, int alreadyAppended) throws BaseException
BaseException
private int appendRestrictions(java.lang.StringBuilder ql, DbControl dc, java.util.List<Restriction> restrictions, int alreadyAppended) throws BaseException
BaseException
private int appendGroups(java.lang.StringBuilder ql, DbControl dc, java.util.List<Expression> groups, int alreadyAppended) throws BaseException
BaseException
private int appendHavings(java.lang.StringBuilder ql, DbControl dc, java.util.List<Restriction> havings, int alreadyAppended) throws BaseException
BaseException
private int appendOrders(java.lang.StringBuilder ql, DbControl dc, java.util.List<Order> orders, int alreadyAppended) throws BaseException
BaseException
private void autoJoin()
private void addChildElements(java.util.Map<QueryElement,java.lang.Object> all, QueryElement root)