Package net.sf.basedb.core
Class AbstractQuery
java.lang.Object
net.sf.basedb.core.AbstractQuery
- All Implemented Interfaces:
Query
- Direct Known Subclasses:
AbstractHqlQuery
,AbstractSqlQuery
An abstract implementation of the
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.- Version:
- 2.0
- Author:
- Samuel, Nicklas
- Last modified
- $Date: 2015-04-20 11:08:18 +0200 (må, 20 apr 2015) $
-
Field Summary
Modifier and TypeFieldDescriptionprivate Map<Class<?>,
AutoJoiner<?, ?>> private static final boolean
So we don't always have to call logSql.debug()private boolean
If the query should only return the same item once or not.private boolean
If transactions must be able to continue after an SQL failure or not.private int
The index of the first result that should be returned by the query.private List<Expression>
List of group expressions added to the query.private boolean
private List<Restriction>
List of having restrictions added to the query.private static final Object
private boolean
If theQuery.count(DbControl)
method is executing or not.List of joins added to the query.private static final Logger
Log all SQL statements.private int
The maximum number of items that should be returned by the query.List of orders added to the query.private Map<String,
QueryParameter> Parameters values added to the query.private List<Expression>
List of permanent group expressions added to the query.private List<Restriction>
List of permanent having restrictions added to the query.List of permanent joins added to the query.List of permanent orders added to the query.private List<Restriction>
List of permanent restrictions added to the query.List of permanent selections added to the query.private QuerySection
The section of the query that is currently beeing built.private List<Restriction>
List of restrictions added to the query.private boolean
If the query should calculate the total number of matching items in the case that limits have been set withfirstResult
ormaxResults
.private final String
The root enitity.List of selections added to the query.Tempoarary joins, that are automatically cleared after each build.Tempoarary selections, that are automatically cleared after each build.Temporary list to hold theta join conditions if needed by the database. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addAutoJoiner
(AutoJoiner<?, ?> autoJoiner) private void
addChildElements
(Map<QueryElement, Object> all, QueryElement root) private int
appendGroups
(StringBuilder ql, DbControl dc, List<Expression> groups, int alreadyAppended) private int
appendHavings
(StringBuilder ql, DbControl dc, List<Restriction> havings, int alreadyAppended) private int
appendJoins
(StringBuilder ql, DbControl dc, List<Join> joins, boolean useThetaJoin, int alreadyAppended) private int
appendOrders
(StringBuilder ql, DbControl dc, List<Order> orders, int alreadyAppended) private int
appendRestrictions
(StringBuilder ql, DbControl dc, List<Restriction> restrictions, int alreadyAppended) private int
appendSelects
(StringBuilder ql, DbControl dc, List<Select> selects, int alreadyAppended) private void
autoJoin()
(package private) String
getCountQuery
(DbControl dc, boolean autoJoin) Build the query string for returning the total count.int
The number of the row the query should start returning.(package private) String
getIdQuery
(DbControl dc, boolean autoJoin) Build a query string that is identical to the main query but only return the ID values.(package private) String
getMainQuery
(DbControl dc, boolean autoJoin) Build the main query string.int
The maximum number of rows returned by the query.Get a set with the names for all parameters in this query.(package private) Map<String,
QueryParameter> Get all query parameters.(package private) Type
getParameterType
(String name) Get the type of the parameter, or null if not known.(package private) Object
getParameterValue
(String name) Get the value of the named parameter.Get all permanent selects.getQueryParameter
(String name) Get parameter information for the parameter with given name.Get the current section of the query that is beeing built.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
(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
Check if the query is executing theQuery.count(DbControl)
method or notboolean
If this query returns distinct results of not.boolean
Check if a failure of this query should still allow the current transaction to continue.boolean
If this query is readonly and cannot be structurally modified.boolean
If this query returns the total count or not.void
Add a join query element to the join list.void
joinPermanent
(Join join) Permanently add a join query element to the join list.void
Add an ordering query element to the orderby list.void
orderPermanent
(Order order) Permanently add an ordering query element to the orderby list.protected String
postProcessQuery
(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
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
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
(String name, boolean permanent, Object value, Type valueType) void
setParameter
(String name, Object value, Type valueType) Set the value of a query parameter.void
setPermanentParameter
(String name, 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 theQuery.setFirstResult(int)
andQuery.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) Generate the query string that is going to be sent to the underlying query system (eg.toString()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface net.sf.basedb.core.query.Query
count, getQueryType, getRootAlias
-
Field Details
-
logSql
Log all SQL statements. -
debugEnabled
private static final boolean debugEnabledSo we don't always have to call logSql.debug() -
hasAutoJoined
private boolean hasAutoJoined -
autoJoiners
-
selects
List of selections added to the query. -
permanentSelects
List of permanent selections added to the query. -
temporarySelects
Tempoarary selections, that are automatically cleared after each build. -
rootEntity
The root enitity. -
joins
List of joins added to the query. -
permanentJoins
List of permanent joins added to the query. -
temporaryJoins
Tempoarary joins, that are automatically cleared after each build. -
restrictions
List of restrictions added to the query. -
permanentRestrictions
List of permanent restrictions added to the query. -
groups
List of group expressions added to the query. -
permanentGroups
List of permanent group expressions added to the query. -
havings
List of having restrictions added to the query. -
permanentHavings
List of permanent having restrictions added to the query. -
orders
List of orders added to the query. -
permanentOrders
List of permanent orders added to the query. -
parameters
Parameters values added to the query. -
firstResult
private int firstResultThe index of the first result that should be returned by the query. -
maxResults
private int maxResultsThe maximum number of items that should be returned by the query. -
returnTotalCount
private boolean returnTotalCountIf the query should calculate the total number of matching items in the case that limits have been set withfirstResult
ormaxResults
. -
distinct
private boolean distinctIf the query should only return the same item once or not. -
thetaJoinConditions
Temporary list to hold theta join conditions if needed by the database. -
isCounting
private boolean isCountingIf theQuery.count(DbControl)
method is executing or not. -
querySection
The section of the query that is currently beeing built. -
failSafe
private boolean failSafeIf transactions must be able to continue after an SQL failure or not. -
HOLDER
-
-
Constructor Details
-
AbstractQuery
AbstractQuery(String rootEntity) Create a new query.- Parameters:
rootEntity
- The root entity of the query
-
-
Method Details
-
addAutoJoiner
- Specified by:
addAutoJoiner
in interfaceQuery
- Throws:
InvalidDataException
-
select
Description copied from interface:Query
Add a selection query element to the selection list.- Specified by:
select
in interfaceQuery
- Parameters:
select
- The query element to add- Throws:
InvalidDataException
BaseException
- See Also:
-
selectPermanent
Description copied from interface:Query
Permanently add a selection query element to the selection list. The query element is not cleared by theQuery.reset()
method.- Specified by:
selectPermanent
in interfaceQuery
- Parameters:
select
- The query element to add- Throws:
InvalidDataException
BaseException
- See Also:
-
join
Description copied from interface:Query
Add a join query element to the join list.- Specified by:
join
in interfaceQuery
- Parameters:
join
- The query element to add- Throws:
InvalidDataException
BaseException
- See Also:
-
joinPermanent
Description copied from interface:Query
Permanently add a join query element to the join list. The query element is not cleared by theQuery.reset()
method.- Specified by:
joinPermanent
in interfaceQuery
- Parameters:
join
- The query element to add- Throws:
InvalidDataException
BaseException
- See Also:
-
restrict
Description copied from interface:Query
Add a restriction query element to the restriction list.- Specified by:
restrict
in interfaceQuery
- Parameters:
restriction
- The query element to add- Throws:
InvalidDataException
BaseException
- See Also:
-
restrictPermanent
Description copied from interface:Query
Permanently add a restriction query element to the restriction list. The query element is not cleared by theQuery.reset()
method.- Specified by:
restrictPermanent
in interfaceQuery
- Parameters:
restriction
- The query element to add- Throws:
InvalidDataException
BaseException
- See Also:
-
group
Description copied from interface:Query
Add an expression query element to the group list.- Specified by:
group
in interfaceQuery
- Parameters:
expression
- The query element to add- Throws:
InvalidDataException
BaseException
- See Also:
-
groupPermanent
Description copied from interface:Query
Permanently add an expression query element to the group list. The query element is not cleared by theQuery.reset()
method.- Specified by:
groupPermanent
in interfaceQuery
- Parameters:
expression
- The query element to add- Throws:
InvalidDataException
BaseException
- See Also:
-
having
Description copied from interface:Query
Add a restriction query element to the having list.- Specified by:
having
in interfaceQuery
- Parameters:
restriction
- The query element to add- Throws:
InvalidDataException
BaseException
- See Also:
-
havingPermanent
Description copied from interface:Query
Permanently add a restriction query element to the having list. The query element is not cleared by theQuery.reset()
method.- Specified by:
havingPermanent
in interfaceQuery
- Parameters:
restriction
- The query element to add- Throws:
InvalidDataException
BaseException
- See Also:
-
order
Description copied from interface:Query
Add an ordering query element to the orderby list.- Specified by:
order
in interfaceQuery
- Parameters:
order
- The query element to add- Throws:
InvalidDataException
BaseException
- See Also:
-
orderPermanent
Description copied from interface:Query
Permanently add an ordering query element to the orderby list. The query element is not cleared by theQuery.reset()
method.- Specified by:
orderPermanent
in interfaceQuery
- Parameters:
order
- The query element to add- Throws:
InvalidDataException
BaseException
- See Also:
-
setParameter
-
setParameter
public void setParameter(String name, Object value, Type valueType) throws InvalidDataException, BaseException Description copied from interface:Query
Set the value of a query parameter. If the value of this parameter has already been set by a call to theQuery.setPermanentParameter(String, Object, Type)
method, anInvalidDataException
is thrown.- Specified by:
setParameter
in interfaceQuery
- Parameters:
name
- The name of the parametervalue
- The value of the parametervalueType
- The type of the value, or null if not needed- Throws:
InvalidDataException
- If the value of the parameter has already been permanently setBaseException
- See Also:
-
setPermanentParameter
public void setPermanentParameter(String name, Object value, Type valueType) throws InvalidDataException, BaseException Description copied from interface:Query
Permanently set the value of a query parameter. If the value of this parameter has already been set by a call to this method, anInvalidDataException
is thrown.- Specified by:
setPermanentParameter
in interfaceQuery
- Parameters:
name
- The name of the parametervalue
- The value of the parametervalueType
- The type of the value, or null if not needed- Throws:
InvalidDataException
- If the value of the parameter has already been permanently setBaseException
- See Also:
-
hasParameterValue
Description copied from interface:Query
Check if a value for the specified parameter has been set or not. The method should check both permanent and non-permanent parameters.- Specified by:
hasParameterValue
in interfaceQuery
- Returns:
- TRUE if a value (including null) has been set, FALSE otherwise
-
getParameterNames
Description copied from interface:Query
Get a set with the names for all parameters in this query.- Specified by:
getParameterNames
in interfaceQuery
- Returns:
- A set (empty if no parameters exists)
- Since:
- 2.9
-
getQueryParameter
Description copied from interface:Query
Get parameter information for the parameter with given name.- Specified by:
getQueryParameter
in interfaceQuery
- Parameters:
name
- The name of the parameter- Returns:
- A QueryParameter object or null if no parameter with that name exists
- Since:
- 2.9
-
setFirstResult
public void setFirstResult(int firstResult) Description copied from interface:Query
Specify that the query should start returning rows from the specified row number. 0 = start returning from the first row. If the value is higher than the total number of rows, no rows are returned. This option is best combined with theQuery.setMaxResults(int)
option.- Specified by:
setFirstResult
in interfaceQuery
- Parameters:
firstResult
- The number of the first row starting at 0- See Also:
-
getFirstResult
public int getFirstResult()Description copied from interface:Query
The number of the row the query should start returning.- Specified by:
getFirstResult
in interfaceQuery
- See Also:
-
setMaxResults
public void setMaxResults(int maxResults) Description copied from interface:Query
Specify that the query should at most return the specified number of rows. If the matching number of rows is less than this value only the matching rows are returned. A value of 0 disabled this option. This option is best combined with theQuery.setFirstResult(int)
option.- Specified by:
setMaxResults
in interfaceQuery
- Parameters:
maxResults
- The maximum number of rows to return- See Also:
-
getMaxResults
public int getMaxResults()Description copied from interface:Query
The maximum number of rows returned by the query. A value of 0 indicates no limit.- Specified by:
getMaxResults
in interfaceQuery
- See Also:
-
setReturnTotalCount
public void setReturnTotalCount(boolean flag) Description copied from interface:Query
Specify if the query should return a count for the total number of items that would have been returned if theQuery.setFirstResult(int)
andQuery.setMaxResults(int)
had been disabled, or if the results are loaded by an iterator where the number of rows not are known beforehand.- Specified by:
setReturnTotalCount
in interfaceQuery
- Parameters:
flag
- TRUE if the query should return the total count, FALSE otherwise
-
isReturningTotalCount
public boolean isReturningTotalCount()Description copied from interface:Query
If this query returns the total count or not.- Specified by:
isReturningTotalCount
in interfaceQuery
- See Also:
-
setDistinct
public void setDistinct(boolean flag) Description copied from interface:Query
Specify if the query should only return distinct rows. Exctly what this means depends on what is selected by the query. For example, if we are selecting items no item would be returned more than once, if we are selecting numbers the same number wouldn't be returned more than once.- Specified by:
setDistinct
in interfaceQuery
- Parameters:
flag
- TRUE if the query should return distinct results, FALSE otherwise
-
isDistinct
public boolean isDistinct()Description copied from interface:Query
If this query returns distinct results of not.- Specified by:
isDistinct
in interfaceQuery
- See Also:
-
isReadonly
public boolean isReadonly()Description copied from interface:Query
If this query is readonly and cannot be structurally modified. Ie. no more query elements can be added. It is still possible to set parameter values. A call toQuery.reset()
would unlock the query and allow it to be modified again.- Specified by:
isReadonly
in interfaceQuery
- Returns:
- TRUE if the query is readonly, FALSE otherwise
-
reset
public void reset()Resets the query, allowing you to add more restrictions, joins, order objects and parameters. Existing restrictions, joins and order objects are cleared. Permanent restrictions, orders and parameters cannot be cleared. -
isCounting
public boolean isCounting()Description copied from interface:Query
Check if the query is executing theQuery.count(DbControl)
method or not- Specified by:
isCounting
in interfaceQuery
- Returns:
- TRUE if the query is currently executing the
count
method, FALSE otherwise
-
getQuerySection
Description copied from interface:Query
Get the current section of the query that is beeing built. This method can be used byQueryElement
:s if they need different syntax in different sections.- Specified by:
getQuerySection
in interfaceQuery
- Returns:
- A QuerySection
-
toQl
Description copied from interface:Query
Generate the query string that is going to be sent to the underlying query system (eg. HQL/Hibernate or SQL/JDBC). -
setFailSafe
public void setFailSafe(boolean failSafe) Description copied from interface:Query
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. Setting this flag to TRUE should allow the transaction to continue, setting it to FALSE may or may not allow it to continue. The default setting is FALSE.- Specified by:
setFailSafe
in interfaceQuery
- Parameters:
failSafe
- TRUE to ensure that the transaction is allowed to continue, FALSE if it doesn't matter
-
isFailSafe
public boolean isFailSafe()Description copied from interface:Query
Check if a failure of this query should still allow the current transaction to continue.- Specified by:
isFailSafe
in interfaceQuery
- Returns:
- TRUE if the transaction should be allowed to continue, FALSE if it doesn't matter
-
toString
-
setCounting
void setCounting(boolean isCounting) Set the counting status of the query. -
temporarySelect
- Throws:
InvalidDataException
BaseException
-
temporaryJoin
- Throws:
InvalidDataException
BaseException
-
resetTemporary
void resetTemporary() -
getSelects
Get all non-permanent selects. -
getPermanentSelects
Get all permanent selects. -
getParameters
Map<String,QueryParameter> getParameters()Get all query parameters. -
getParameterValue
Get the value of the named parameter. This method first looks among the permanent parameters then among the non-permanent parameters.- Parameters:
name
- The name of the parameter- Returns:
- The value of the parameter, or null if not found
-
getParameterType
Get the type of the parameter, or null if not known. -
getMainQuery
Build the main query string.- Throws:
BaseException
-
getIdQuery
Build a query string that is identical to the main query but only return the ID values.- Throws:
BaseException
- Since:
- 3.5
-
getCountQuery
Build the query string for returning the total count.- Throws:
BaseException
-
postProcessQuery
Allows a subclass to modify the query. This implementation simply returns the original query. -
appendSelects
private int appendSelects(StringBuilder ql, DbControl dc, List<Select> selects, int alreadyAppended) throws BaseException - Throws:
BaseException
-
appendJoins
private int appendJoins(StringBuilder ql, DbControl dc, List<Join> joins, boolean useThetaJoin, int alreadyAppended) throws BaseException - Throws:
BaseException
-
appendRestrictions
private int appendRestrictions(StringBuilder ql, DbControl dc, List<Restriction> restrictions, int alreadyAppended) throws BaseException - Throws:
BaseException
-
appendGroups
private int appendGroups(StringBuilder ql, DbControl dc, List<Expression> groups, int alreadyAppended) throws BaseException - Throws:
BaseException
-
appendHavings
private int appendHavings(StringBuilder ql, DbControl dc, List<Restriction> havings, int alreadyAppended) throws BaseException - Throws:
BaseException
-
appendOrders
private int appendOrders(StringBuilder ql, DbControl dc, List<Order> orders, int alreadyAppended) throws BaseException - Throws:
BaseException
-
autoJoin
private void autoJoin() -
addChildElements
-