org.h2.command.dml

## Class Query

- java.lang.Object
- - org.h2.command.Prepared
  - - org.h2.command.dml.Query

Direct Known Subclasses:
  Select, SelectUnion

    public abstract class Query
    extends Prepared

Represents a SELECT statement (simple, or union).

### Field Summary

Fields
`protected boolean` `distinct` Whether the result must only contain distinct rows.
`protected Expression` `limitExpr` The limit expression as specified in the LIMIT or TOP clause.
`protected Expression` `offsetExpr` The offset expression as specified in the LIMIT ..
`protected boolean` `randomAccessResult` Whether the result needs to support random access.
`protected Expression` `sampleSizeExpr` The sample size expression as specified in the SAMPLE_SIZE clause.

### Fields inherited from class org.h2.command.Prepared

`create, parameters, prepareAlways, session, sqlStatement`

### Method Summary

Methods
`abstract void` `addGlobalCondition(Parameter param, int columnId, int comparisonType)` Add a condition to the query.
`abstract boolean` `allowGlobalConditions()` Check whether adding condition to the query is allowed.
`void` `disableCache()` Disable caching of result sets.
`abstract void` `fireBeforeSelectTriggers()` Call the before triggers on all tables.
`abstract int` `getColumnCount()` Get the column count of this query.
`abstract double` `getCost()` Calculate the cost to execute this query.
`int` `getCostAsExpression()` Calculate the cost when used as a subquery.
`abstract java.util.ArrayList<Expression>` `getExpressions()` The the list of select expressions.
`Expression` `getLimit()`
`long` `getMaxDataModificationId()`
`Expression` `getOffset()`
`Value[]` `getParameterValues()`
`abstract java.util.HashSet<Table>` `getTables()` Get all tables that are involved in this query.
`abstract boolean` `hasOrder()` Whether the query has an order.
`abstract void` `init()` Initialize the query.
`boolean` `isDistinct()`
`abstract boolean` `isEverything(ExpressionVisitor visitor)` Check if this expression and all sub-expressions can fulfill a criteria.
`boolean` `isQuery()` Check if this object is a query.
`boolean` `isTransactional()` Check if this command is transactional.
`abstract boolean` `isUnion()` Check if this is a UNION query.
`abstract void` `mapColumns(ColumnResolver resolver, int level)` Map the columns to the given column resolver.
`abstract void` `prepareJoinBatch()` Prepare join batching.
`SortOrder` `prepareOrder(java.util.ArrayList<SelectOrderBy> orderList, int expressionCount)` Create a `SortOrder` object given the list of `SelectOrderBy` objects.
`LocalResult` `query(int maxrows)` Execute the query.
`protected abstract LocalResult` `queryWithoutCache(int limit, ResultTarget target)` Execute the query without checking the cache.
`void` `setDistinct(boolean b)` Set the distinct flag.
`abstract void` `setEvaluatable(TableFilter tableFilter, boolean b)` Change the evaluatable flag.
`abstract void` `setForUpdate(boolean forUpdate)` Set the 'for update' flag.
`void` `setLimit(Expression limit)`
`void` `setOffset(Expression offset)`
`abstract void` `setOrder(java.util.ArrayList<SelectOrderBy> order)` Set the order by list.
`void` `setRandomAccessResult(boolean b)` Whether results need to support random access.
`void` `setSampleSize(Expression sampleSize)`
`abstract void` `updateAggregate(Session s)` Update all aggregate function values.

### Methods inherited from class org.h2.command.Prepared

`checkCanceled, checkParameters, getCurrentObjectId, getCurrentRowNumber, getObjectId, getParameters, getPlanSQL, getSQL, getSQL, getSQL, getType, isCacheable, isReadOnly, needRecompile, prepare, queryMeta, setCommand, setCurrentRowNumber, setObjectId, setParameterList, setPrepareAlways, setRow, setSession, setSQL, toString, update`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Field Detail

#### limitExpr

    protected Expression limitExpr

The limit expression as specified in the LIMIT or TOP clause.

#### offsetExpr

    protected Expression offsetExpr

The offset expression as specified in the LIMIT .. OFFSET clause.

#### sampleSizeExpr

    protected Expression sampleSizeExpr

The sample size expression as specified in the SAMPLE_SIZE clause.

#### distinct

    protected boolean distinct

Whether the result must only contain distinct rows.

#### randomAccessResult

    protected boolean randomAccessResult

Whether the result needs to support random access.

### Method Detail

#### isUnion

    public abstract boolean isUnion()

Check if this is a UNION query.

Returns:
  `true` if this is a UNION query

#### prepareJoinBatch

    public abstract void prepareJoinBatch()

Prepare join batching.

#### queryWithoutCache

    protected abstract LocalResult queryWithoutCache(int 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.

#### getExpressions

    public abstract java.util.ArrayList<Expression> getExpressions()

The 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 java.util.HashSet<Table> getTables()

Get all tables that are involved in this query.

Returns:
  the set of tables

#### setOrder

    public abstract void setOrder(java.util.ArrayList<SelectOrderBy> order)

Set the order by list.

Parameters:
  `order` - the order by list

#### hasOrder

    public abstract boolean hasOrder()

Whether the query has an order.

Returns:
  true if it has

#### setForUpdate

    public abstract void setForUpdate(boolean forUpdate)

Set the 'for update' flag.

Parameters:
  `forUpdate` - the new setting

#### getColumnCount

    public abstract int getColumnCount()

Get the column count of this query.

Returns:
  the column count

#### mapColumns

    public abstract void mapColumns(ColumnResolver resolver,
                  int level)

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)

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

#### updateAggregate

    public abstract void updateAggregate(Session s)

Update all aggregate function values.

Parameters:
  `s` - the session

#### fireBeforeSelectTriggers

    public abstract void fireBeforeSelectTriggers()

Call the before triggers on all tables.

#### setDistinct

    public void setDistinct(boolean b)

Set the distinct flag.

Parameters:
  `b` - the new value

#### isDistinct

    public boolean isDistinct()

#### setRandomAccessResult

    public void setRandomAccessResult(boolean b)

Whether results need to support random access.

Parameters:
  `b` - the new value

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

#### getParameterValues

    public final Value[] getParameterValues()

#### query

    public LocalResult query(int 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

#### prepareOrder

    public SortOrder prepareOrder(java.util.ArrayList<SelectOrderBy> orderList,
                         int expressionCount)

Create a `SortOrder` object given the list of `SelectOrderBy` objects. The expression list is extended if necessary.

Parameters:
  `orderList` - a list of `SelectOrderBy` elements
  `expressionCount` - the number of columns in the query
Returns:
  the `SortOrder` object

#### setOffset

    public void setOffset(Expression offset)

#### getOffset

    public Expression getOffset()

#### setLimit

    public void setLimit(Expression limit)

#### getLimit

    public Expression getLimit()

#### setSampleSize

    public void setSampleSize(Expression sampleSize)

#### getMaxDataModificationId

    public final long getMaxDataModificationId()