org.h2.command.dml

## Class Select

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

    public class Select
    extends Query

This class represents a simple SELECT statement. For each select statement, visibleColumnCount <= distinctColumnCount <= expressionCount. The expression list count could include ORDER BY and GROUP BY expressions that are not in the select list. The call sequence is init(), mapColumns() if it's a subquery, prepare().

### Field Summary

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

`create, parameters, prepareAlways, session, sqlStatement`

### Constructor Summary

Constructors
Constructor and Description
`Select(Session session, Select parentSelect)`

### Method Summary

Methods
`void` `addCondition(Expression cond)` Add a condition to the list of conditions.
`void` `addGlobalCondition(Parameter param, int columnId, int comparisonType)` Add a condition to the query.
`void` `addTableFilter(TableFilter filter, boolean isTop)` Add a table to the query.
`boolean` `addWindow(java.lang.String name, Window window)` Adds a named window definition.
`boolean` `allowGlobalConditions()` Check whether adding condition to the query is allowed.
`void` `fireBeforeSelectTriggers()` Call the before triggers on all tables.
`int` `getColumnCount()` Get the column count of this query.
`Expression` `getCondition()`
`double` `getCost()` Calculate the cost to execute this query.
`java.util.ArrayList<Expression>` `getGroupBy()`
`SelectGroups` `getGroupDataIfCurrent(boolean window)` Get the group data if there is currently a group-by active.
`Expression` `getHaving()`
`JoinBatch` `getJoinBatch()`
`Select` `getParentSelect()` Returns parent select, or null.
`java.lang.String` `getPlanSQL(boolean alwaysQuote)` Get the SQL statement with the execution plan.
`Expression` `getQualify()`
`SortOrder` `getSortOrder()`
`java.util.HashSet<Table>` `getTables()` Get all tables that are involved in this query.
`java.util.ArrayList<TableFilter>` `getTopFilters()`
`TableFilter` `getTopTableFilter()`
`Window` `getWindow(java.lang.String name)` Returns a window with specified name, or null.
`void` `init()` Initialize the query.
`boolean` `isAnyDistinct()`
`boolean` `isCacheable()`
`boolean` `isEverything(ExpressionVisitor visitor)` Check if this expression and all sub-expressions can fulfill a criteria.
`boolean` `isGroupQuery()` Checks if this query is a group query.
`boolean` `isGroupWindowStage2()` Checks if window stage of group window query is performed.
`boolean` `isQuickAggregateQuery()` Check if this is an aggregate query with direct lookup, for example a query of the type SELECT COUNT(*) FROM TEST or SELECT MAX(ID) FROM TEST.
`boolean` `isReadOnly()` Check if this command is read only.
`boolean` `isUnion()` Check if this is a UNION query.
`boolean` `isWindowQuery()` Checks if this query contains window functions.
`void` `mapColumns(ColumnResolver resolver, int level)` Map the columns to the given column resolver.
`void` `prepare()` Prepare this statement.
`void` `prepareJoinBatch()` Prepare join batching.
`ResultInterface` `queryMeta()` Get an empty result set containing the meta data.
`protected ResultInterface` `queryWithoutCache(int maxRows, ResultTarget target)` Execute the query without checking the cache.
`void` `setDistinct()` Set the distinct flag.
`void` `setDistinct(Expression[] distinctExpressions)` Set the DISTINCT ON expressions.
`void` `setDistinctIfPossible()` Set the distinct flag only if it is possible, may be used as a possible optimization only.
`void` `setEvaluatable(TableFilter tableFilter, boolean b)` Change the evaluatable flag.
`void` `setExpressions(java.util.ArrayList<Expression> expressions)`
`void` `setForUpdate(boolean b)` Set the 'for update' flag.
`void` `setGroupBy(java.util.ArrayList<Expression> group)`
`void` `setGroupQuery()` Set when SELECT statement contains (non-window) aggregate functions, GROUP BY clause or HAVING clause.
`void` `setHaving(Expression having)`
`void` `setQualify(Expression qualify)`
`void` `setWildcard()` Sets a wildcard expression as in "SELECT * FROM TEST".
`void` `setWindowQuery()` Called if this query contains window functions.
`void` `updateAggregate(Session s, int stage)` Update all aggregate function values.

### Methods inherited from class org.h2.command.dml.Query

`disableCache, getCostAsExpression, getExpressions, getLimit, getMaxDataModificationId, getOffset, getType, hasOrder, isFetchPercent, isNeverLazy, isQuery, isRandomAccessResult, isStandardDistinct, isTransactional, isWithTies, prepareOrder, query, query, setFetchPercent, setLimit, setNeverLazy, setOffset, setOrder, setRandomAccessResult, setSampleSize, setWithTies`

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

`checkCanceled, checkParameters, getCteCleanups, getCurrentRowNumber, getObjectId, getParameters, getPersistedObjectId, getSession, getSimpleSQL, getSQL, getSQL, needRecompile, setCommand, setCteCleanups, setCurrentRowNumber, setParameterList, setPersistedObjectId, setPrepareAlways, setRow, setSession, setSQL, toString, update`

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

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

### Constructor Detail

#### Select

    public Select(Session session,
          Select parentSelect)

### Method Detail

#### isUnion

    public boolean isUnion()

Description copied from class: `Query`
Check if this is a UNION query.

Specified by:
  `isUnion` in class `Query`
Returns:
  `true` if this is a UNION query

#### addTableFilter

    public void addTableFilter(TableFilter filter,
                      boolean isTop)

Add a table to the query.

Parameters:
  `filter` - the table to add
  `isTop` - if the table can be the first table in the query plan

#### getTopFilters

    public java.util.ArrayList<TableFilter> getTopFilters()

#### setExpressions

    public void setExpressions(java.util.ArrayList<Expression> expressions)

#### setWildcard

    public void setWildcard()

Sets a wildcard expression as in "SELECT * FROM TEST".

#### setGroupQuery

    public void setGroupQuery()

Set when SELECT statement contains (non-window) aggregate functions, GROUP BY clause or HAVING clause.

#### setWindowQuery

    public void setWindowQuery()

Called if this query contains window functions.

#### setGroupBy

    public void setGroupBy(java.util.ArrayList<Expression> group)

#### getGroupBy

    public java.util.ArrayList<Expression> getGroupBy()

#### getGroupDataIfCurrent

    public SelectGroups getGroupDataIfCurrent(boolean window)

Get the group data if there is currently a group-by active.

Parameters:
  `window` - is this a window function
Returns:
  the grouped data

#### setDistinct

    public void setDistinct()

Description copied from class: `Query`
Set the distinct flag.

Overrides:
  `setDistinct` in class `Query`

#### setDistinct

    public void setDistinct(Expression[] distinctExpressions)

Set the DISTINCT ON expressions.

Parameters:
  `distinctExpressions` - array of expressions

#### setDistinctIfPossible

    public void setDistinctIfPossible()

Description copied from class: `Query`
Set the distinct flag only if it is possible, may be used as a possible optimization only.

Specified by:
  `setDistinctIfPossible` in class `Query`

#### isAnyDistinct

    public boolean isAnyDistinct()

Overrides:
  `isAnyDistinct` in class `Query`
Returns:
  whether this query is a `DISTINCT` or `DISTINCT ON (...)` query

#### addWindow

    public boolean addWindow(java.lang.String name,
                    Window window)

Adds a named window definition.

Parameters:
  `name` - name
  `window` - window definition
Returns:
  true if a new definition was added, false if old definition was replaced

#### getWindow

    public Window getWindow(java.lang.String name)

Returns a window with specified name, or null.

Parameters:
  `name` - name of the window
Returns:
  the window with specified name, or null

#### addCondition

    public void addCondition(Expression cond)

Add a condition to the list of conditions.

Parameters:
  `cond` - the condition to add

#### getCondition

    public Expression getCondition()

#### queryMeta

    public ResultInterface queryMeta()

Description copied from class: `Prepared`
Get an empty result set containing the meta data.

Specified by:
  `queryMeta` in class `Prepared`
Returns:
  the result set

#### queryWithoutCache

    protected ResultInterface queryWithoutCache(int maxRows,
                                    ResultTarget target)

Description copied from class: `Query`
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.

Specified by:
  `queryWithoutCache` in class `Query`
Parameters:
  `maxRows` - the limit as specified in the JDBC method call
  `target` - the target to write results to
Returns:
  the result

#### init

    public void init()

Description copied from class: `Query`
Initialize the query.

Specified by:
  `init` in class `Query`

#### prepare

    public void prepare()

Description copied from class: `Prepared`
Prepare this statement.

Overrides:
  `prepare` in class `Prepared`

#### prepareJoinBatch

    public void prepareJoinBatch()

Description copied from class: `Query`
Prepare join batching.

Specified by:
  `prepareJoinBatch` in class `Query`

#### getJoinBatch

    public JoinBatch getJoinBatch()

#### getCost

    public double getCost()

Description copied from class: `Query`
Calculate the cost to execute this query.

Specified by:
  `getCost` in class `Query`
Returns:
  the cost

#### getTables

    public java.util.HashSet<Table> getTables()

Description copied from class: `Query`
Get all tables that are involved in this query.

Specified by:
  `getTables` in class `Query`
Returns:
  the set of tables

#### fireBeforeSelectTriggers

    public void fireBeforeSelectTriggers()

Description copied from class: `Query`
Call the before triggers on all tables.

Specified by:
  `fireBeforeSelectTriggers` in class `Query`

#### getPlanSQL

    public java.lang.String getPlanSQL(boolean alwaysQuote)

Description copied from class: `Prepared`
Get the SQL statement with the execution plan.

Overrides:
  `getPlanSQL` in class `Prepared`
Parameters:
  `alwaysQuote` - quote all identifiers
Returns:
  the execution plan

#### setHaving

    public void setHaving(Expression having)

#### getHaving

    public Expression getHaving()

#### setQualify

    public void setQualify(Expression qualify)

#### getQualify

    public Expression getQualify()

#### getColumnCount

    public int getColumnCount()

Description copied from class: `Query`
Get the column count of this query.

Specified by:
  `getColumnCount` in class `Query`
Returns:
  the column count

#### getTopTableFilter

    public TableFilter getTopTableFilter()

#### setForUpdate

    public void setForUpdate(boolean b)

Description copied from class: `Query`
Set the 'for update' flag.

Specified by:
  `setForUpdate` in class `Query`
Parameters:
  `b` - the new setting

#### mapColumns

    public void mapColumns(ColumnResolver resolver,
                  int level)

Description copied from class: `Query`
Map the columns to the given column resolver.

Specified by:
  `mapColumns` in class `Query`
Parameters:
  `resolver` - the resolver
  `level` - the subquery level (0 is the top level query, 1 is the first subquery level)

#### setEvaluatable

    public void setEvaluatable(TableFilter tableFilter,
                      boolean b)

Description copied from class: `Query`
Change the evaluatable flag. This is used when building the execution plan.

Specified by:
  `setEvaluatable` in class `Query`
Parameters:
  `tableFilter` - the table filter
  `b` - the new value

#### isQuickAggregateQuery

    public boolean isQuickAggregateQuery()

Check if this is an aggregate query with direct lookup, for example a query of the type SELECT COUNT(*) FROM TEST or SELECT MAX(ID) FROM TEST.

Returns:
  true if a direct lookup is possible

#### isGroupQuery

    public boolean isGroupQuery()

Checks if this query is a group query.

Returns:
  whether this query is a group query.

#### isWindowQuery

    public boolean isWindowQuery()

Checks if this query contains window functions.

Returns:
  whether this query contains window functions

#### isGroupWindowStage2

    public boolean isGroupWindowStage2()

Checks if window stage of group window query is performed. If true, column resolver may not be used.

Returns:
  true if window stage of group window query is performed

#### addGlobalCondition

    public void addGlobalCondition(Parameter param,
                          int columnId,
                          int comparisonType)

Description copied from class: `Query`
Add a condition to the query. This is used for views.

Specified by:
  `addGlobalCondition` in class `Query`
Parameters:
  `param` - the parameter
  `columnId` - the column index (0 meaning the first column)
  `comparisonType` - the comparison type

#### updateAggregate

    public void updateAggregate(Session s,
                       int stage)

Description copied from class: `Query`
Update all aggregate function values.

Specified by:
  `updateAggregate` in class `Query`
Parameters:
  `s` - the session
  `stage` - select stage

#### isEverything

    public boolean isEverything(ExpressionVisitor visitor)

Description copied from class: `Query`
Check if this expression and all sub-expressions can fulfill a criteria. If any part returns false, the result is false.

Specified by:
  `isEverything` in class `Query`
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

#### isCacheable

    public boolean isCacheable()

Overrides:
  `isCacheable` in class `Prepared`

#### allowGlobalConditions

    public boolean allowGlobalConditions()

Description copied from class: `Query`
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.

Specified by:
  `allowGlobalConditions` in class `Query`
Returns:
  true if adding global conditions is allowed

#### getSortOrder

    public SortOrder getSortOrder()

#### getParentSelect

    public Select getParentSelect()

Returns parent select, or null.

Returns:
  parent select, or null