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
public abstract class Query extends Prepared
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
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract voidaddGlobalCondition(Parameter param, int columnId, int comparisonType)Add a condition to the query.abstract booleanallowGlobalConditions()Check whether adding condition to the query is allowed.voidcollectDependencies(java.util.HashSet<DbObject> dependencies)Find and collect all DbObjects, this Prepared depends on.voiddisableCache()Disable caching of result sets.booleanexists()Execute the EXISTS predicate over the query.abstract voidfireBeforeSelectTriggers()Call the before triggers on all tables.intgetColumnCount()Get the column count of this query.abstract doublegetCost()Calculate the cost to execute this query.intgetCostAsExpression()Calculate the cost when used as a subquery.java.util.ArrayList<Expression>getExpressions()The list of select expressions.ExpressiongetFetch()ForUpdategetForUpdate()Returns FOR UPDATE clause, if any.ExpressiongetIfSingleRow()If this query is determined as a single-row query, returns a replacement expression.longgetMaxDataModificationId()ExpressiongetOffset()QueryScopegetOuterQueryScope()Returns the scope of the outer query.TypeInfogetRowDataType()Returns data type of rows.abstract java.util.HashSet<Table>getTables()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.voidinvalidateCachedResult(Table reason)booleanisAnyDistinct()booleanisConstantQuery()Check 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.booleanisFetchPercent()booleanisInPredicateResult()Returns whether result is generated for the IN predicate.booleanisNeverLazy()booleanisQuery()Check if this object is a query.booleanisReadOnly()Check if this command is read only.booleanisRetryable()Returns is this command can be repeated on locking failure.booleanisStandardDistinct()booleanisTransactional()Check if this command is transactional.abstract booleanisUnion()Check if this is a UNION query.booleanisWithTies()abstract voidmapColumns(ColumnResolver resolver, int level, boolean outer)Map the columns to the given column resolver.voidprepare()Prepare this statement.abstract voidprepareExpressions()abstract voidpreparePlan()ResultInterfacequery(long maxrows)Execute the query.ResultInterfacequery(long limit, ResultTarget target)Execute the query, writing the result to the target result.ResultInterfacequeryMeta()Get an empty result set containing the metadata.protected abstract ResultInterfacequeryWithoutCache(long limit, ResultTarget target)Execute the query without checking the cache.voidsetDistinctIfPossible()Set 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.voidsetInPredicateResult()Convert results to compatible with IN predicate.voidsetInPredicateResultSortTypes(int[] inPredicateSortTypes)Sets sort types for the IN predicate.voidsetNeverLazy(boolean b)voidsetOffset(Expression offset)voidsetOrder(java.util.ArrayList<QueryOrderBy> order)Set the order by list.voidsetOuterQueryScope(QueryScope outerQueryScope)Sets the scope of the outer query.voidsetWithClause(java.util.LinkedHashMap<java.lang.String,Table> withClause)Sets the WITH clause of this query.voidsetWithTies(boolean withTies)TabletoTable(java.lang.String alias, Column[] columnTemplates, java.util.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(java.lang.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 Detail
-
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
public ResultInterface queryMeta()
Description copied from class:PreparedGet an empty result set containing the metadata.
-
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 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
public java.util.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 java.util.HashSet<Table> getTables()
Get all tables that are involved in this query.- Returns:
- the set of tables
-
setOrder
public void setOrder(java.util.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 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
public abstract void setEvaluatable(TableFilter tableFilter, boolean b)
Change the evaluatable flag. This is used when building the execution plan.- Parameters:
tableFilter- the table filterb- 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 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
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:PreparedCheck if this command is read only.- Overrides:
isReadOnlyin classPrepared- Returns:
- true if it is
-
updateAggregate
public abstract void updateAggregate(SessionLocal s, int stage)
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
-
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: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
public final ResultInterface query(long maxrows)
Description copied from class:PreparedExecute the query.
-
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 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
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(java.util.LinkedHashMap<java.lang.String,Table> withClause)
Sets the WITH clause of this query.- Parameters:
withClause- the WITH clause of this query
-
writeWithList
protected void writeWithList(java.lang.StringBuilder builder, int sqlFlags)
-
toTable
public Table toTable(java.lang.String alias, Column[] columnTemplates, java.util.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
public void collectDependencies(java.util.HashSet<DbObject> dependencies)
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
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:PreparedReturns is this command can be repeated on locking failure.- Overrides:
isRetryablein classPrepared- Returns:
- is this command can be repeated on locking failure
-
invalidateCachedResult
public void invalidateCachedResult(Table reason)
- Overrides:
invalidateCachedResultin classPrepared
-
-