2.17.2: 2011-06-17

net.sf.basedb.core
Class AbstractSqlQuery

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

abstract class AbstractSqlQuery
extends AbstractQuery
implements SqlQuery

An abstract implementation of the SqlQuery interface.

Version:
2.0
Author:
Nicklas
Last modified
$Date: 2011-02-04 09:25:16 +0100 (Fri, 04 Feb 2011) $

Field Summary
private static boolean debugParamEnabled
          So we don't always have to call logParam.debug()
private static boolean debugSqlEnabled
          So we don't always have to call logSql.debug()
private static Logger logParam
          Log all parameter bindings to prepared statement.
private static Logger logSql
          Log all SQL statements.
private static Pattern namedParameter
          Pattern that finds a named parameter in an SQL string.
 
Constructor Summary
AbstractSqlQuery(String rootEntity)
           
 
Method Summary
(package private)  String bindLimits(String sql, List<String> parameterOrder)
          Parse the given SQL and add LIMIT and OFFSET parameters to it.
 long count(DbControl dc)
          Count the number of items/rows that are returned by the query.
(package private) abstract  Select[] getDefaultSelects()
           
 QueryType getQueryType()
          Get the type of query.
 DynamicResultIterator iterate(DbControl dc)
          Execute the query.
(package private)  String parseParameters(String sql, List<String> parameterOrder)
          Parse the given SQL and find named parameters.
(package private)  void setParameters(PreparedStatement ps, List<String> parameterOrder)
          Set parameter values on a prepared statement.
private  void setParameterValue(PreparedStatement ps, int index, Object parameterValue, Type valueType)
          Set a parameter value on a prepared statement.
 
Methods inherited from class net.sf.basedb.core.AbstractQuery
addAutoJoiner, getCountQuery, getFirstResult, getMainQuery, getMaxResults, getParameterNames, getParameters, getParameterType, getParameterValue, getPermanentSelects, getQueryParameter, getQuerySection, getSelects, group, groupPermanent, hasParameterValue, having, havingPermanent, isCounting, isDistinct, isReadonly, isReturningTotalCount, join, joinPermanent, order, orderPermanent, postProcessQuery, reset, resetTemporary, restrict, restrictPermanent, select, selectPermanent, setCounting, setDistinct, setFirstResult, setMaxResults, setParameter, setPermanentParameter, setReturnTotalCount, temporaryJoin, temporarySelect, toQl, 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
addAutoJoiner, getFirstResult, getMaxResults, getParameterNames, getQueryParameter, getQuerySection, getRootAlias, group, groupPermanent, hasParameterValue, having, havingPermanent, isCounting, isDistinct, isReadonly, isReturningTotalCount, join, joinPermanent, order, orderPermanent, reset, restrict, restrictPermanent, select, selectPermanent, setDistinct, setFirstResult, setMaxResults, setParameter, setPermanentParameter, setReturnTotalCount, toQl
 

Field Detail

logSql

private static final Logger logSql
Log all SQL statements.


debugSqlEnabled

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


logParam

private static final Logger logParam
Log all parameter bindings to prepared statement.


debugParamEnabled

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


namedParameter

private static final Pattern namedParameter
Pattern that finds a named parameter in an SQL string. A named parameter is a colon (:) followed by one or more alphanumeric characters.

Constructor Detail

AbstractSqlQuery

AbstractSqlQuery(String rootEntity)
Method Detail

getQueryType

public QueryType getQueryType()
Description copied from interface: Query
Get the type of query. Ie. The query language.

Specified by:
getQueryType in interface Query
Returns:
QueryType.SQL

count

public long count(DbControl dc)
           throws BaseException
Description copied from interface: Query
Count the number of items/rows that are returned by the query. The method ignores the Query.getFirstResult() and Query.getMaxResults() settings.

Specified by:
count in interface Query
Returns:
The number of rows/items returned by the query
Throws:
BaseException - If there is an error

getDefaultSelects

abstract Select[] getDefaultSelects()

iterate

public DynamicResultIterator iterate(DbControl dc)
                              throws BaseException
Execute the query.

Parameters:
dc - The DbControl to use for database access
Returns:
A DynamicResultIterator object
Throws:
BaseException - If there is an error

parseParameters

String parseParameters(String sql,
                       List<String> parameterOrder)
Parse the given SQL and find named parameters. A named parameter is a colon (:) followed by one or more alphanumeric characters. Replaces the named parameters with a JDBC parameter placeholder, ie. a question mark. The name of the parameters are put in the specified list in the order they appear in the SQL string.

Parameters:
sql - The original SQL string, which may contain named parameters
parameterOrder - A list that should store the name of the parameters in the correct order
Returns:
The new SQL statement with JDBC question marks instead of named parameters

bindLimits

String bindLimits(String sql,
                  List<String> parameterOrder)
Parse the given SQL and add LIMIT and OFFSET parameters to it. This method checks the capabilities of the installed Hibernate Dialect and uses it for generating the actual SQL. If limit parameters are added to the query this method adds entries for those parameters in the parameterOrder list and values using the regular Query.setParameter(String, Object, Type) method. The parameter names used are _limit_ and _offset_.

If the underlying database doesn't support limit parameters the query will be executed and the result starting point moved forward to the correct offset and the a limit will be passed to the DynamicResultIterator object.

Parameters:
sql - The original SQL without limit parameters
parameterOrder - A list that stores the names of the parameters in the order they should be binf by JDBC
Returns:
The new SQL with limit parameters

setParameters

void setParameters(PreparedStatement ps,
                   List<String> parameterOrder)
             throws SQLException
Set parameter values on a prepared statement.

Parameters:
ps - The prepared statement
parameterOrder - The names of the parameters in the order they appear in the prepared statement
Throws:
SQLException

setParameterValue

private void setParameterValue(PreparedStatement ps,
                               int index,
                               Object parameterValue,
                               Type valueType)
                        throws SQLException
Set a parameter value on a prepared statement. If the object is a BasicItem the id of the object is used a value.

Parameters:
ps - The prepared statement
index - The index of the parameter, starting at 1
parameterValue - The value of the parameter
Throws:
SQLException

2.17.2: 2011-06-17