Class AbstractSqlQuery

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

public abstract class AbstractSqlQuery extends AbstractQuery implements SqlQuery
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) $
  • Field Details

    • 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 Details

    • AbstractSqlQuery

      AbstractSqlQuery(String rootEntity)
  • Method Details

    • 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
    • createCountWork

      private AbstractSqlQuery.CountWork createCountWork(DbControl dc)
      Creates a work item for counting the number of total hits by the query.
    • getDefaultSelects

      abstract Select[] getDefaultSelects()
    • getJepFunctions

      public abstract JepFunction[] getJepFunctions(DbControl dc, boolean restrictions)
      Create custom JEP function that can be used with the current query. The actual function need to implement either JepExpressionFunction or JepRestrictionFunction.
      Parameters:
      restrictions - If FALSE the metod should only return functions that can be used as Expression:s, otherwise it should return both expression and restriction functions
      Since:
      2.15 and 3.12
    • iterate

      public DynamicResultIterator iterate(DbControl dc) throws BaseException
      Execute the query.
      Specified by:
      iterate in interface SqlQuery
      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
    • 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 statement
      parameterOrder - The names of the parameters in the order they appear in the prepared statement
      parameterIndex - 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 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
    • closeResultSet

      protected void closeResultSet(ResultSet result, String msg)
      Close the result set without throwing an exception. If the ResultSet.close() method throws an exception it is logged together with the message.
      Parameters:
      msg - A message to write to the log if the close fails
      result - The result set to close
      Since:
      3.1
    • closeStatement

      protected void closeStatement(Statement stmt, String msg)
      Close the statement without throwing an exception. If the Statement.close() method throws an exception it is logged together with the message.
      Parameters:
      msg - A message to write to the log if the close fails
      stmt - The statement to close
      Since:
      3.1
    • rollbackToSavepoint

      protected void rollbackToSavepoint(Connection conn, Savepoint savepoint, String msg)
      Rollback the current transaction to the given savepoint.
      Parameters:
      conn - The connection to rollback
      savepoint - The savepoint to rollback to
      msg - A message to write to the log if the rollback fails
      Since:
      3.1
    • releaseSavepoint

      protected void releaseSavepoint(Connection conn, Savepoint savepoint, String msg)
      Release the given savepoint.
      Parameters:
      conn - The connection that holds the savepoint
      savepoint - The savepoint to release
      msg - A message to write to the log if the release fails
      Since:
      3.1