Package net.sf.basedb.core
Class AbstractSqlQuery
java.lang.Object
net.sf.basedb.core.AbstractQuery
net.sf.basedb.core.AbstractSqlQuery
- Direct Known Subclasses:
DynamicQuery
,DynamicRawDataQuery
,DynamicReporterQuery
An abstract implementation of the
SqlQuery
interface.- Version:
- 2.0
- Author:
- Nicklas
- Last modified
- $Date: 2017-12-08 14:18:12 +0100 (fr, 08 dec 2017) $
-
Nested Class Summary
-
Field Summary
Modifier and TypeFieldDescriptionprivate static final boolean
So we don't always have to call logParam.debug()private static final boolean
So we don't always have to call logSql.debug()private static final Logger
Log all parameter bindings to prepared statement.private static final Logger
Log all SQL statements.private static final Pattern
Pattern that finds a named parameter in an SQL string. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
closeResultSet
(ResultSet result, String msg) Close the result set without throwing an exception.protected void
closeStatement
(Statement stmt, String msg) Close the statement without throwing an exception.long
Count the number of items/rows that are returned by the query.private AbstractSqlQuery.CountWork
Creates a work item for counting the number of total hits by the query.(package private) abstract Select[]
abstract JepFunction[]
getJepFunctions
(DbControl dc, boolean restrictions) Create custom JEP function that can be used with the current query.(package private) LimitHandler
Get a handler for adding LIMIT and OFFSET parameters to an SQL statement.Get the type of query.(package private) RowSelection
Get a RowSelection object to be used with a LimitHandler so that it can modify and add parameters to the SQL.Execute the query.(package private) String
parseParameters
(String sql, List<String> parameterOrder) Parse the given SQL and find named parameters.protected void
releaseSavepoint
(Connection conn, Savepoint savepoint, String msg) Release the given savepoint.protected void
rollbackToSavepoint
(Connection conn, Savepoint savepoint, String msg) Rollback the current transaction to the given savepoint.(package private) int
setParameters
(PreparedStatement ps, List<String> parameterOrder, int parameterIndex) 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, getIdQuery, getMainQuery, getMaxResults, getParameterNames, getParameters, getParameterType, getParameterValue, getPermanentSelects, getQueryParameter, getQuerySection, getSelects, group, groupPermanent, hasParameterValue, having, havingPermanent, isCounting, isDistinct, isFailSafe, isReadonly, isReturningTotalCount, join, joinPermanent, order, orderPermanent, postProcessQuery, reset, resetTemporary, restrict, restrictPermanent, select, selectPermanent, setCounting, setDistinct, setFailSafe, 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, isFailSafe, isReadonly, isReturningTotalCount, join, joinPermanent, order, orderPermanent, reset, restrict, restrictPermanent, select, selectPermanent, setDistinct, setFailSafe, setFirstResult, setMaxResults, setParameter, setPermanentParameter, setReturnTotalCount, toQl
-
Field Details
-
logSql
Log all SQL statements. -
debugSqlEnabled
private static final boolean debugSqlEnabledSo we don't always have to call logSql.debug() -
logParam
Log all parameter bindings to prepared statement. -
debugParamEnabled
private static final boolean debugParamEnabledSo we don't always have to call logParam.debug() -
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 Details
-
AbstractSqlQuery
AbstractSqlQuery(String rootEntity)
-
-
Method Details
-
getQueryType
Description copied from interface:Query
Get the type of query. Ie. The query language.- Specified by:
getQueryType
in interfaceQuery
- Returns:
QueryType.SQL
-
count
Description copied from interface:Query
Count the number of items/rows that are returned by the query. The method ignores theQuery.getFirstResult()
andQuery.getMaxResults()
settings.- Specified by:
count
in interfaceQuery
- Returns:
- The number of rows/items returned by the query
- Throws:
BaseException
- If there is an error
-
createCountWork
Creates a work item for counting the number of total hits by the query. -
getDefaultSelects
-
getJepFunctions
Create custom JEP function that can be used with the current query. The actual function need to implement eitherJepExpressionFunction
orJepRestrictionFunction
.- Parameters:
restrictions
- If FALSE the metod should only return functions that can be used asExpression
:s, otherwise it should return both expression and restriction functions- Since:
- 2.15 and 3.12
-
iterate
Execute the query.- Specified by:
iterate
in interfaceSqlQuery
- Parameters:
dc
- TheDbControl
to use for database access- Returns:
- A
DynamicResultIterator
object - Throws:
BaseException
- If there is an error
-
parseParameters
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 parametersparameterOrder
- 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
-
getLimitHandler
LimitHandler getLimitHandler()Get a handler for adding LIMIT and OFFSET parameters to an SQL statement.- Since:
- 3.8
-
getRowSelectionForLimitHandler
RowSelection getRowSelectionForLimitHandler()Get a RowSelection object to be used with a LimitHandler so that it can modify and add parameters to the SQL.- Returns:
- A RowSelection object if limit/offset should be applied, null otherwise
- Since:
- 3.8
-
setParameters
int setParameters(PreparedStatement ps, List<String> parameterOrder, int parameterIndex) throws SQLException Set parameter values on a prepared statement.- Parameters:
ps
- The prepared statementparameterOrder
- The names of the parameters in the order they appear in the prepared statementparameterIndex
- The index of the first parameter to bind- Returns:
- The number of parameters bound
- 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 aBasicItem
the id of the object is used a value.- Parameters:
ps
- The prepared statementindex
- The index of the parameter, starting at 1parameterValue
- The value of the parameter- Throws:
SQLException
-
closeResultSet
Close the result set without throwing an exception. If theResultSet.close()
method throws an exception it is logged together with the message.- Parameters:
msg
- A message to write to the log if the close failsresult
- The result set to close- Since:
- 3.1
-
closeStatement
Close the statement without throwing an exception. If theStatement.close()
method throws an exception it is logged together with the message.- Parameters:
msg
- A message to write to the log if the close failsstmt
- The statement to close- Since:
- 3.1
-
rollbackToSavepoint
Rollback the current transaction to the given savepoint.- Parameters:
conn
- The connection to rollbacksavepoint
- The savepoint to rollback tomsg
- A message to write to the log if the rollback fails- Since:
- 3.1
-
releaseSavepoint
Release the given savepoint.- Parameters:
conn
- The connection that holds the savepointsavepoint
- The savepoint to releasemsg
- A message to write to the log if the release fails- Since:
- 3.1
-