Package org.h2.command.query
Class Query
java.lang.Object
org.h2.command.Prepared
org.h2.command.query.Query
- Direct Known Subclasses:
Select,SelectUnion,TableValueConstructor
Represents a SELECT statement (simple, or union).
-
Field Summary
Fields inherited from class org.h2.command.Prepared
create, parameters, prepareAlways, session, sqlStatement, sqlTokens -
Method Summary
Modifier and TypeMethodDescriptionabstract voidaddGlobalCondition(Parameter param, int columnId, int comparisonType) Add a condition to the query.abstract booleanCheck whether adding condition to the query is allowed.voidcollectDependencies(HashSet<DbObject> dependencies) Find and collect all DbObjects, this Prepared depends on.voidDisable caching of result sets.final booleanexists()Execute the EXISTS predicate over the query.abstract voidCall the before triggers on all tables.intGet the column count of this query.abstract doublegetCost()Calculate the cost to execute this query.intCalculate the cost when used as a subquery.The the list of select expressions.getFetch()Returns FOR UPDATE clause, if any.If this query is determined as a single-row query, returns a replacement expression.final longReturns the scope of the outer query.Returns data type of rows.Get all tables that are involved in this query.intgetType()Get the command type as defined in CommandInterfacebooleanhasOrder()Whether the query has an order.abstract voidinit()Initialize the query.booleanbooleanCheck if this query will always return the same value and has no side effects.abstract booleanisEverything(ExpressionVisitor visitor) Check if this expression and all sub-expressions can fulfill a criteria.booleanbooleanbooleanisQuery()Check if this object is a query.booleanReturns whether results support random access.booleanCheck if this command is read only.booleanReturns is this command can be repeated again on locking failure.booleanbooleanCheck if this command is transactional.abstract booleanisUnion()Check if this is a UNION query.booleanabstract voidmapColumns(ColumnResolver resolver, int level, boolean outer) Map the columns to the given column resolver.final voidprepare()Prepare this statement.abstract voidabstract voidfinal ResultInterfacequery(long maxrows) Execute the query.final ResultInterfacequery(long limit, ResultTarget target) Execute the query, writing the result to the target result.Get an empty result set containing the meta data.protected abstract ResultInterfacequeryWithoutCache(long limit, ResultTarget target) Execute the query without checking the cache.voidSet the distinct flag only if it is possible, may be used as a possible optimization only.abstract voidsetEvaluatable(TableFilter tableFilter, boolean b) Change the evaluatable flag.voidsetFetch(Expression fetch) voidsetFetchPercent(boolean fetchPercent) abstract voidsetForUpdate(ForUpdate forUpdate) Set the FOR UPDATE clause.voidsetNeverLazy(boolean b) voidsetOffset(Expression offset) voidsetOrder(ArrayList<QueryOrderBy> order) Set the order by list.voidsetOuterQueryScope(QueryScope outerQueryScope) Sets the scope of the outer query.voidsetRandomAccessResult(boolean b) Whether results need to support random access.voidsetWithClause(LinkedHashMap<String, Table> withClause) Sets the WITH clause of this query.voidsetWithTies(boolean withTies) toTable(String alias, Column[] columnTemplates, ArrayList<Parameter> parameters, boolean forCreateView, Query topQuery) Converts this query to a table or a view.abstract voidupdateAggregate(SessionLocal s, int stage) Update all aggregate function values.protected voidwriteWithList(StringBuilder builder, int sqlFlags) Methods inherited from class org.h2.command.Prepared
checkCanceled, checkParameters, getCurrentRowNumber, getDatabase, getObjectId, getParameters, getPersistedObjectId, getPlanSQL, getPlanSQL, getSession, getSimpleSQL, getSQL, getSQLTokens, isCacheable, isWithParamValues, needRecompile, setCommand, setCurrentRowNumber, setParameterList, setPersistedObjectId, setPrepareAlways, setRow, setSession, setSQL, setWithParamValues, toString, update
-
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:
trueif this is a UNION query
-
queryMeta
Description copied from class:PreparedGet an empty result set containing the meta data. -
queryWithoutCache
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 calltarget- 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:PreparedPrepare this statement. -
prepareExpressions
public abstract void prepareExpressions() -
preparePlan
public abstract void preparePlan() -
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
Get all tables that are involved in this query.- Returns:
- the set of tables
-
setOrder
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
Returns FOR UPDATE clause, if any.- Returns:
- FOR UPDATE clause or
null
-
setForUpdate
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
Returns data type of rows.- Returns:
- data type of rows
-
mapColumns
Map the columns to the given column resolver.- Parameters:
resolver- the resolverlevel- 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
Change the evaluatable flag. This is used when building the execution plan.- Parameters:
tableFilter- the table filterb- the new value
-
addGlobalCondition
Add a condition to the query. This is used for views.- Parameters:
param- the parametercolumnId- 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
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:PreparedCheck if this command is read only.- Overrides:
isReadOnlyin classPrepared- Returns:
- true if it is
-
updateAggregate
Update all aggregate function values.- Parameters:
s- the sessionstage- 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
DISTINCTquery
-
isAnyDistinct
public boolean isAnyDistinct()- Returns:
- whether this query is a
DISTINCTorDISTINCT ON (...)query
-
isRandomAccessResult
public boolean isRandomAccessResult()Returns whether results support random access.- Returns:
- whether results support random access
-
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:PreparedCheck if this object is a query. -
isTransactional
public boolean isTransactional()Description copied from class:PreparedCheck if this command is transactional. If it is not, then it forces the current transaction to commit.- Specified by:
isTransactionalin classPrepared- Returns:
- true if it is
-
disableCache
public void disableCache()Disable caching of result sets. -
query
Description copied from class:PreparedExecute the query. -
query
Execute the query, writing the result to the target result.- Parameters:
limit- the maximum number of rows to returntarget- 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:PreparedGet the command type as defined in CommandInterface -
setOffset
-
getOffset
-
setFetch
-
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
Returns the scope of the outer query.- Returns:
- the scope of the outer query
-
setOuterQueryScope
Sets the scope of the outer query.- Parameters:
outerQueryScope- the scope of the outer query
-
setWithClause
Sets the WITH clause of this query.- Parameters:
withClause- the WITH clause of this query
-
writeWithList
-
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 viewcolumnTemplates- column templates, ornullparameters- the parametersforCreateView- if true, a system session will be used for the viewtopQuery- the top level query- Returns:
- the table or the view
-
collectDependencies
Description copied from class:PreparedFind and collect all DbObjects, this Prepared depends on.- Overrides:
collectDependenciesin classPrepared- 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
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:PreparedReturns is this command can be repeated again on locking failure.- Overrides:
isRetryablein classPrepared- Returns:
- is this command can be repeated again on locking failure
-