Class Query

java.lang.Object
org.h2.command.Prepared
org.h2.command.query.Query
Direct Known Subclasses:
Select, SelectUnion, TableValueConstructor

public abstract class Query extends Prepared
Represents a SELECT statement (simple, or union).
  • Method Details

    • setNeverLazy

      public void setNeverLazy(boolean b)
    • isNeverLazy

      public boolean isNeverLazy()
    • isUnion

      public abstract boolean isUnion()
      Check if this is a UNION query.
      Returns:
      true if this is a UNION query
    • queryMeta

      public ResultInterface queryMeta()
      Description copied from class: Prepared
      Get an empty result set containing the metadata.
      Specified by:
      queryMeta in class Prepared
      Returns:
      the result set
    • queryWithoutCache

      protected abstract ResultInterface queryWithoutCache(long limit, ResultTarget target)
      Execute the query without checking the cache. If a target is specified, the results are written to it, and the method returns null. If no target is specified, a new LocalResult is created and returned.
      Parameters:
      limit - the limit as specified in the JDBC method call
      target - the target to write results to
      Returns:
      the result
    • init

      public abstract void init()
      Initialize the query.
    • prepare

      public final void prepare()
      Description copied from class: Prepared
      Prepare this statement.
      Overrides:
      prepare in class Prepared
    • prepareExpressions

      public abstract void prepareExpressions()
    • preparePlan

      public abstract void preparePlan()
    • getExpressions

      public ArrayList<Expression> getExpressions()
      The list of select expressions. This may include invisible expressions such as order by expressions.
      Returns:
      the list of expressions
    • getCost

      public abstract double getCost()
      Calculate the cost to execute this query.
      Returns:
      the cost
    • getCostAsExpression

      public int getCostAsExpression()
      Calculate the cost when used as a subquery. This method returns a value between 10 and 1000000, to ensure adding other values can't result in an integer overflow.
      Returns:
      the estimated cost as an integer
    • getTables

      public abstract HashSet<Table> getTables()
      Get all tables that are involved in this query.
      Returns:
      the set of tables
    • setOrder

      public void setOrder(ArrayList<QueryOrderBy> order)
      Set the order by list.
      Parameters:
      order - the order by list
    • hasOrder

      public boolean hasOrder()
      Whether the query has an order.
      Returns:
      true if it has
    • getForUpdate

      public ForUpdate getForUpdate()
      Returns FOR UPDATE clause, if any.
      Returns:
      FOR UPDATE clause or null
    • setForUpdate

      public abstract void setForUpdate(ForUpdate forUpdate)
      Set the FOR UPDATE clause.
      Parameters:
      forUpdate - the new FOR UPDATE clause
    • getColumnCount

      public int getColumnCount()
      Get the column count of this query.
      Returns:
      the column count
    • getRowDataType

      public TypeInfo getRowDataType()
      Returns data type of rows.
      Returns:
      data type of rows
    • mapColumns

      public abstract void mapColumns(ColumnResolver resolver, int level, boolean outer)
      Map the columns to the given column resolver.
      Parameters:
      resolver - the resolver
      level - the subquery level (0 is the top level query, 1 is the first subquery level)
      outer - whether this method was called from the outer query
    • setEvaluatable

      public abstract void setEvaluatable(TableFilter tableFilter, boolean b)
      Change the evaluatable flag. This is used when building the execution plan.
      Parameters:
      tableFilter - the table filter
      b - the new value
    • addGlobalCondition

      public abstract void addGlobalCondition(Parameter param, int columnId, int comparisonType)
      Add a condition to the query. This is used for views.
      Parameters:
      param - the parameter
      columnId - the column index (0 meaning the first column)
      comparisonType - the comparison type
    • allowGlobalConditions

      public abstract boolean allowGlobalConditions()
      Check whether adding condition to the query is allowed. This is not allowed for views that have an order by and a limit, as it would affect the returned results.
      Returns:
      true if adding global conditions is allowed
    • isEverything

      public abstract boolean isEverything(ExpressionVisitor visitor)
      Check if this expression and all sub-expressions can fulfill a criteria. If any part returns false, the result is false.
      Parameters:
      visitor - the visitor
      Returns:
      if the criteria can be fulfilled
    • isReadOnly

      public boolean isReadOnly()
      Description copied from class: Prepared
      Check if this command is read only.
      Overrides:
      isReadOnly in class Prepared
      Returns:
      true if it is
    • updateAggregate

      public abstract void updateAggregate(SessionLocal s, int stage)
      Update all aggregate function values.
      Parameters:
      s - the session
      stage - select stage
    • fireBeforeSelectTriggers

      public abstract void fireBeforeSelectTriggers()
      Call the before triggers on all tables.
    • setDistinctIfPossible

      public void setDistinctIfPossible()
      Set the distinct flag only if it is possible, may be used as a possible optimization only.
    • isStandardDistinct

      public boolean isStandardDistinct()
      Returns:
      whether this query is a plain DISTINCT query
    • isAnyDistinct

      public boolean isAnyDistinct()
      Returns:
      whether this query is a DISTINCT or DISTINCT ON (...) query
    • isInPredicateResult

      public boolean isInPredicateResult()
      Returns whether result is generated for the IN predicate.
      Returns:
      whether result is generated for the IN predicate
    • setInPredicateResult

      public void setInPredicateResult()
      Convert results to compatible with IN predicate.
    • setInPredicateResultSortTypes

      public void setInPredicateResultSortTypes(int[] inPredicateSortTypes)
      Sets sort types for the IN predicate.
      Parameters:
      inPredicateSortTypes - sort types for the IN predicate
    • isQuery

      public boolean isQuery()
      Description copied from class: Prepared
      Check if this object is a query.
      Overrides:
      isQuery in class Prepared
      Returns:
      true if it is
    • isTransactional

      public boolean isTransactional()
      Description copied from class: Prepared
      Check if this command is transactional. If it is not, then it forces the current transaction to commit.
      Specified by:
      isTransactional in class Prepared
      Returns:
      true if it is
    • disableCache

      public void disableCache()
      Disable caching of result sets.
    • query

      public final ResultInterface query(long maxrows)
      Description copied from class: Prepared
      Execute the query.
      Overrides:
      query in class Prepared
      Parameters:
      maxrows - the maximum number of rows to return
      Returns:
      the result set
    • query

      public final ResultInterface query(long limit, ResultTarget target)
      Execute the query, writing the result to the target result.
      Parameters:
      limit - the maximum number of rows to return
      target - the target result (null will return the result)
      Returns:
      the result set (if the target is not set).
    • exists

      public final boolean exists()
      Execute the EXISTS predicate over the query.
      Returns:
      EXISTS predicate result
    • getType

      public int getType()
      Description copied from class: Prepared
      Get the command type as defined in CommandInterface
      Specified by:
      getType in class Prepared
      Returns:
      the statement type
    • setOffset

      public void setOffset(Expression offset)
    • getOffset

      public Expression getOffset()
    • setFetch

      public void setFetch(Expression fetch)
    • getFetch

      public Expression getFetch()
    • setFetchPercent

      public void setFetchPercent(boolean fetchPercent)
    • isFetchPercent

      public boolean isFetchPercent()
    • setWithTies

      public void setWithTies(boolean withTies)
    • isWithTies

      public boolean isWithTies()
    • getMaxDataModificationId

      public final long getMaxDataModificationId()
    • getOuterQueryScope

      public QueryScope getOuterQueryScope()
      Returns the scope of the outer query.
      Returns:
      the scope of the outer query
    • setOuterQueryScope

      public void setOuterQueryScope(QueryScope outerQueryScope)
      Sets the scope of the outer query.
      Parameters:
      outerQueryScope - the scope of the outer query
    • setWithClause

      public void setWithClause(LinkedHashMap<String,Table> withClause)
      Sets the WITH clause of this query.
      Parameters:
      withClause - the WITH clause of this query
    • writeWithList

      protected void writeWithList(StringBuilder builder, int sqlFlags)
    • toTable

      public Table toTable(String alias, Column[] columnTemplates, ArrayList<Parameter> parameters, boolean forCreateView, Query topQuery)
      Converts this query to a table or a view.
      Parameters:
      alias - alias name for the view
      columnTemplates - column templates, or null
      parameters - the parameters
      forCreateView - if true, a system session will be used for the view
      topQuery - the top level query
      Returns:
      the table or the view
    • collectDependencies

      public void collectDependencies(HashSet<DbObject> dependencies)
      Description copied from class: Prepared
      Find and collect all DbObjects, this Prepared depends on.
      Overrides:
      collectDependencies in class Prepared
      Parameters:
      dependencies - collection of dependencies to populate
    • isConstantQuery

      public boolean isConstantQuery()
      Check if this query will always return the same value and has no side effects.
      Returns:
      if this query will always return the same value and has no side effects.
    • getIfSingleRow

      public Expression getIfSingleRow()
      If this query is determined as a single-row query, returns a replacement expression.
      Returns:
      the expression, or null
    • isRetryable

      public boolean isRetryable()
      Description copied from class: Prepared
      Returns is this command can be repeated on locking failure.
      Overrides:
      isRetryable in class Prepared
      Returns:
      is this command can be repeated on locking failure
    • invalidateCachedResult

      public void invalidateCachedResult(Table reason)
      Overrides:
      invalidateCachedResult in class Prepared