2.17.2: 2011-06-17

net.sf.basedb.core
Class AbstractQuery

java.lang.Object
  extended by net.sf.basedb.core.AbstractQuery
All Implemented Interfaces:
Query
Direct Known Subclasses:
AbstractHqlQuery, AbstractSqlQuery

abstract class AbstractQuery
extends Object
implements Query

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: 2011-02-04 09:25:16 +0100 (Fri, 04 Feb 2011) $

Field Summary
private  Map<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  int firstResult
          The index of the first result that should be returned by the query.
private  List<Expression> groups
          List of group expressions added to the query.
private  boolean hasAutoJoined
           
private  List<Restriction> havings
          List of having restrictions added to the query.
private static Object HOLDER
           
private  boolean isCounting
          If the Query.count(DbControl) method is executing or not.
private  List<Join> joins
          List of joins added to the query.
private static Logger logSql
          Log all SQL statements.
private  int maxResults
          The maximum number of items that should be returned by the query.
private  List<Order> orders
          List of orders added to the query.
private  Map<String,QueryParameter> parameters
          Parameters values added to the query.
private  List<Expression> permanentGroups
          List of permanent group expressions added to the query.
private  List<Restriction> permanentHavings
          List of permanent having restrictions added to the query.
private  List<Join> permanentJoins
          List of permanent joins added to the query.
private  List<Order> permanentOrders
          List of permanent orders added to the query.
private  List<Restriction> permanentRestrictions
          List of permanent restrictions added to the query.
private  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  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  String rootEntity
          The root enitity.
private  List<Select> selects
          List of selections added to the query.
private  List<Join> temporaryJoins
          Tempoarary joins, that are automatically cleared after each build.
private  List<Select> temporarySelects
          Tempoarary selections, that are automatically cleared after each build.
private  List<String> thetaJoinConditions
          Temporary list to hold theta join conditions if needed by the database.
 
Constructor Summary
AbstractQuery(String rootEntity)
          Create a new query.
 
Method Summary
 void 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 getFirstResult()
          The number of the row the query should start returning.
(package private)  String getMainQuery(DbControl dc, boolean autoJoin)
          Build the main query string.
 int getMaxResults()
          The maximum number of rows returned by the query.
 Set<String> getParameterNames()
          Get a set with the names for all parameters in this query.
(package private)  Map<String,QueryParameter> getParameters()
          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.
(package private)  List<Select> getPermanentSelects()
          Get all permanent selects.
 QueryParameter getQueryParameter(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)  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(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 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  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 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 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 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)
           
 String toQl(DbControl dc)
          Generate the query string that is going to be sent to the underlying query system (eg.
 String 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 Detail

logSql

private static final Logger logSql
Log all SQL statements.


debugEnabled

private static final boolean debugEnabled
So we don't always have to call logSql.debug()


hasAutoJoined

private boolean hasAutoJoined

autoJoiners

private Map<Class<?>,AutoJoiner<?,?>> autoJoiners

selects

private List<Select> selects
List of selections added to the query.


permanentSelects

private List<Select> permanentSelects
List of permanent selections added to the query.


temporarySelects

private List<Select> temporarySelects
Tempoarary selections, that are automatically cleared after each build.


rootEntity

private final String rootEntity
The root enitity.


joins

private List<Join> joins
List of joins added to the query.


permanentJoins

private List<Join> permanentJoins
List of permanent joins added to the query.


temporaryJoins

private List<Join> temporaryJoins
Tempoarary joins, that are automatically cleared after each build.


restrictions

private List<Restriction> restrictions
List of restrictions added to the query.


permanentRestrictions

private List<Restriction> permanentRestrictions
List of permanent restrictions added to the query.


groups

private List<Expression> groups
List of group expressions added to the query.


permanentGroups

private List<Expression> permanentGroups
List of permanent group expressions added to the query.


havings

private List<Restriction> havings
List of having restrictions added to the query.


permanentHavings

private List<Restriction> permanentHavings
List of permanent having restrictions added to the query.


orders

private List<Order> orders
List of orders added to the query.


permanentOrders

private List<Order> permanentOrders
List of permanent orders added to the query.


parameters

private Map<String,QueryParameter> parameters
Parameters values added to the query.


firstResult

private int firstResult
The index of the first result that should be returned by the query.


maxResults

private int maxResults
The maximum number of items that should be returned by the query.


returnTotalCount

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.


distinct

private boolean distinct
If the query should only return the same item once or not.


thetaJoinConditions

private List<String> thetaJoinConditions
Temporary list to hold theta join conditions if needed by the database.


isCounting

private boolean isCounting
If the Query.count(DbControl) method is executing or not.


querySection

private QuerySection querySection
The section of the query that is currently beeing built.


HOLDER

private static final Object HOLDER
Constructor Detail

AbstractQuery

AbstractQuery(String rootEntity)
Create a new query.

Parameters:
rootEntity - The root entity of the query
Method Detail

addAutoJoiner

public void addAutoJoiner(AutoJoiner<?,?> autoJoiner)
                   throws InvalidDataException
Specified by:
addAutoJoiner in interface Query
Throws:
InvalidDataException

select

public void select(Select select)
            throws InvalidDataException,
                   BaseException
Description copied from interface: Query
Add a selection query element to the selection list.

Specified by:
select in interface Query
Parameters:
select - The query element to add
Throws:
InvalidDataException
BaseException
See Also:
Query.selectPermanent(Select)

selectPermanent

public void selectPermanent(Select select)
                     throws InvalidDataException,
                            BaseException
Description copied from interface: Query
Permanently add a selection query element to the selection list. The query element is not cleared by the Query.reset() method.

Specified by:
selectPermanent in interface Query
Parameters:
select - The query element to add
Throws:
InvalidDataException
BaseException
See Also:
Query.select(Select)

join

public void join(Join join)
          throws InvalidDataException,
                 BaseException
Description copied from interface: Query
Add a join query element to the join list.

Specified by:
join in interface Query
Parameters:
join - The query element to add
Throws:
InvalidDataException
BaseException
See Also:
Query.joinPermanent(Join)

joinPermanent

public void joinPermanent(Join join)
                   throws InvalidDataException,
                          BaseException
Description copied from interface: Query
Permanently add a join query element to the join list. The query element is not cleared by the Query.reset() method.

Specified by:
joinPermanent in interface Query
Parameters:
join - The query element to add
Throws:
InvalidDataException
BaseException
See Also:
Query.join(Join)

restrict

public void restrict(Restriction restriction)
              throws InvalidDataException,
                     BaseException
Description copied from interface: Query
Add a restriction query element to the restriction list.

Specified by:
restrict in interface Query
Parameters:
restriction - The query element to add
Throws:
InvalidDataException
BaseException
See Also:
Query.restrictPermanent(Restriction)

restrictPermanent

public void restrictPermanent(Restriction restriction)
                       throws InvalidDataException,
                              BaseException
Description copied from interface: Query
Permanently add a restriction query element to the restriction list. The query element is not cleared by the Query.reset() method.

Specified by:
restrictPermanent in interface Query
Parameters:
restriction - The query element to add
Throws:
InvalidDataException
BaseException
See Also:
Query.restrict(Restriction)

group

public void group(Expression expression)
           throws InvalidDataException,
                  BaseException
Description copied from interface: Query
Add an expression query element to the group list.

Specified by:
group in interface Query
Parameters:
expression - The query element to add
Throws:
InvalidDataException
BaseException
See Also:
Query.groupPermanent(Expression)

groupPermanent

public void groupPermanent(Expression expression)
                    throws InvalidDataException,
                           BaseException
Description copied from interface: Query
Permanently add an expression query element to the group list. The query element is not cleared by the Query.reset() method.

Specified by:
groupPermanent in interface Query
Parameters:
expression - The query element to add
Throws:
InvalidDataException
BaseException
See Also:
Query.group(Expression)

having

public void having(Restriction restriction)
            throws InvalidDataException,
                   BaseException
Description copied from interface: Query
Add a restriction query element to the having list.

Specified by:
having in interface Query
Parameters:
restriction - The query element to add
Throws:
InvalidDataException
BaseException
See Also:
Query.havingPermanent(Restriction)

havingPermanent

public void havingPermanent(Restriction restriction)
                     throws InvalidDataException,
                            BaseException
Description copied from interface: Query
Permanently add a restriction query element to the having list. The query element is not cleared by the Query.reset() method.

Specified by:
havingPermanent in interface Query
Parameters:
restriction - The query element to add
Throws:
InvalidDataException
BaseException
See Also:
Query.having(Restriction)

order

public void order(Order order)
           throws InvalidDataException,
                  BaseException
Description copied from interface: Query
Add an ordering query element to the orderby list.

Specified by:
order in interface Query
Parameters:
order - The query element to add
Throws:
InvalidDataException
BaseException
See Also:
Query.orderPermanent(Order)

orderPermanent

public void orderPermanent(Order order)
                    throws InvalidDataException,
                           BaseException
Description copied from interface: Query
Permanently add an ordering query element to the orderby list. The query element is not cleared by the Query.reset() method.

Specified by:
orderPermanent in interface Query
Parameters:
order - The query element to add
Throws:
InvalidDataException
BaseException
See Also:
Query.order(Order)

setParameter

private void setParameter(String name,
                          boolean permanent,
                          Object value,
                          Type valueType)

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 the Query.setPermanentParameter(String, Object, Type) method, an InvalidDataException is thrown.

Specified by:
setParameter in interface Query
Parameters:
name - The name of the parameter
value - The value of the parameter
valueType - The type of the value, or null if not needed
Throws:
InvalidDataException - If the value of the parameter has already been permanently set
BaseException
See Also:
Query.setPermanentParameter(String, Object, Type)

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, an InvalidDataException is thrown.

Specified by:
setPermanentParameter in interface Query
Parameters:
name - The name of the parameter
value - The value of the parameter
valueType - The type of the value, or null if not needed
Throws:
InvalidDataException - If the value of the parameter has already been permanently set
BaseException
See Also:
Query.setParameter(String, Object, Type)

hasParameterValue

public boolean hasParameterValue(String name)
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 interface Query
Returns:
TRUE if a value (including null) has been set, FALSE otherwise

getParameterNames

public Set<String> getParameterNames()
Description copied from interface: Query
Get a set with the names for all parameters in this query.

Specified by:
getParameterNames in interface Query
Returns:
A set (empty if no parameters exists)
Since:
2.9

getQueryParameter

public QueryParameter getQueryParameter(String name)
Description copied from interface: Query
Get parameter information for the parameter with given name.

Specified by:
getQueryParameter in interface Query
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 the Query.setMaxResults(int) option.

Specified by:
setFirstResult in interface Query
Parameters:
firstResult - The number of the first row starting at 0
See Also:
Query.setMaxResults(int)

getFirstResult

public int getFirstResult()
Description copied from interface: Query
The number of the row the query should start returning.

Specified by:
getFirstResult in interface Query
See Also:
Query.setFirstResult(int)

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 the Query.setFirstResult(int) option.

Specified by:
setMaxResults in interface Query
Parameters:
maxResults - The maximum number of rows to return
See Also:
Query.setFirstResult(int)

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 interface Query
See Also:
Query.setMaxResults(int)

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 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.

Specified by:
setReturnTotalCount in interface Query
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 interface Query
See Also:
Query.setReturnTotalCount(boolean)

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 interface Query
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 interface Query
See Also:
Query.setDistinct(boolean)

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 to Query.reset() would unlock the query and allow it to be modified again.

Specified by:
isReadonly in interface Query
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.

Specified by:
reset in interface Query

isCounting

public boolean isCounting()
Description copied from interface: Query
Check if the query is executing the Query.count(DbControl) method or not

Specified by:
isCounting in interface Query
Returns:
TRUE if the query is currently executing the count method, FALSE otherwise

getQuerySection

public QuerySection getQuerySection()
Description copied from interface: Query
Get the current section of the query that is beeing built. This method can be used by QueryElement:s if they need different syntax in different sections.

Specified by:
getQuerySection in interface Query
Returns:
A QuerySection

toQl

public String toQl(DbControl dc)
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).

Specified by:
toQl in interface Query
Parameters:
dc - The DbControl that is going to be used for executing the query
Returns:
The query string

toString

public String toString()
Overrides:
toString in class Object

setCounting

void setCounting(boolean isCounting)
Set the counting status of the query.


temporarySelect

void temporarySelect(Select select)
               throws InvalidDataException,
                      BaseException
Throws:
InvalidDataException
BaseException

temporaryJoin

void temporaryJoin(Join join)
             throws InvalidDataException,
                    BaseException
Throws:
InvalidDataException
BaseException

resetTemporary

void resetTemporary()

getSelects

List<Select> getSelects()
Get all non-permanent selects.


getPermanentSelects

List<Select> getPermanentSelects()
Get all permanent selects.


getParameters

Map<String,QueryParameter> getParameters()
Get all query parameters.


getParameterValue

Object getParameterValue(String name)
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

Type getParameterType(String name)
Get the type of the parameter, or null if not known.


getMainQuery

String getMainQuery(DbControl dc,
                    boolean autoJoin)
              throws BaseException
Build the main query string.

Throws:
BaseException

getCountQuery

String getCountQuery(DbControl dc,
                     boolean autoJoin)
               throws BaseException
Build the query string for returning the total count.

Throws:
BaseException

postProcessQuery

protected String postProcessQuery(String query)
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

private void addChildElements(Map<QueryElement,Object> all,
                              QueryElement root)

2.17.2: 2011-06-17