Package org.datanucleus.store.rdbms.sql
Class SelectStatement
- java.lang.Object
-
- org.datanucleus.store.rdbms.sql.SQLStatement
-
- org.datanucleus.store.rdbms.sql.SelectStatement
-
public class SelectStatement extends SQLStatement
SQL SELECT Statement representation. This will create a statement likeSELECT {expr}, {expr}, ... FROM {tblExpr} [joinInfo {tblExpr} ON ...] ... WHERE {boolExpr} [AND|OR] {boolExpr} ... GROUP BY {expr}, {expr} HAVING {boolExpr} ORDER BY {expr} [ASC|DESC], {expr} [ASC|DESC], ...
and also supports UNIONs between SQLStatements, and having sub-queries of other SQLStatements.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
SelectStatement.SelectedItem
-
Field Summary
Fields Modifier and Type Field Description protected boolean
aggregated
whether there is an aggregate expression present in the selectprotected boolean
allowUnions
Whether to make use of any UNIONs on this statement (for when we just want to use this statement on its own).protected boolean
distinct
Whether the statement is distinct.protected List<SQLExpression>
groupingExpressions
Expression(s) for the GROUP BY clause.protected BooleanExpression
having
Having clause.protected boolean[]
orderingDirections
Directions for any ORDER BY expressions (1 for each orderingExpressions entry).protected SQLExpression[]
orderingExpressions
Expressions for any ORDER BY clause.protected org.datanucleus.store.query.NullOrderingType[]
orderNullDirectives
Directives for null handling of any ORDER BY expressions (1 for each orderingExpressions entry).protected long
rangeCount
The number of records to be retrieved in any range restriction.protected long
rangeOffset
The offset for any range restriction.protected List<SelectStatement.SelectedItem>
selectedItems
List of selected items, including any alias to use.protected List<SelectStatement>
unions
List of unioned SelectStatements (if any).-
Fields inherited from class org.datanucleus.store.rdbms.sql.SQLStatement
candidateClassName, clr, EXTENSION_LOCK_FOR_UPDATE, EXTENSION_LOCK_FOR_UPDATE_NOWAIT, EXTENSION_SQL_TABLE_NAMING_STRATEGY, extensions, joins, namer, parent, primaryTable, queryGenerator, rdbmsMgr, requiresJoinReorder, sql, tableGroups, tableNamerByName, tables, where
-
-
Constructor Summary
Constructors Constructor Description SelectStatement(RDBMSStoreManager rdbmsMgr, Table table, DatastoreIdentifier alias, String tableGroupName)
Constructor for a SELECT statement.SelectStatement(RDBMSStoreManager rdbmsMgr, Table table, DatastoreIdentifier alias, String tableGroupName, Map<String,Object> extensions)
Constructor for a SELECT statement.SelectStatement(SQLStatement parentStmt, RDBMSStoreManager rdbmsMgr, Table table, DatastoreIdentifier alias, String tableGroupName)
Constructor for a SELECT statement, maybe as a subquery.SelectStatement(SQLStatement parentStmt, RDBMSStoreManager rdbmsMgr, Table table, DatastoreIdentifier alias, String tableGroupName, Map<String,Object> extensions)
Constructor for a SELECT statement, maybe as a subquery.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addAndConditionToJoinForTable(SQLTable sqlTbl, BooleanExpression andCondition, boolean applyToUnions)
Method to find the JOIN for the specified table and add the specified 'and' condition to the JOIN as an 'ON' clause.void
addGroupingExpression(SQLExpression expr)
Method to add a grouping expression to the query.protected void
addOrderComponent(SQLText orderST, String orderString, SQLExpression orderExpr, boolean orderDirection, org.datanucleus.store.query.NullOrderingType orderNullDirective, DatastoreAdapter dba)
protected void
addOrderingColumnsToSelect()
Convenience method to add any necessary columns to the SELECT that are needed by the ordering constraint.boolean
allUnionsForSamePrimaryTable()
Convenience accessor for whether all unions of this statement are for the same primary table.protected SQLText
generateOrderingStatement()
Convenience method to generate the ordering statement to add to the overall query statement.int
getNumberOfSelects()
Accessor for the number of selected items in the SELECT clause.int
getNumberOfUnions()
protected SQLText
getSqlForJoins(boolean lock)
Convenience method to return the JOIN clause implied by the "joins" List.SQLText
getSQLText()
List<SelectStatement>
getUnions()
Accessor for the unioned statements.boolean
isDistinct()
Accessor for whether the statement restricts the results to distinct.SQLTable
join(SQLJoin.JoinType joinType, SQLTable sourceTable, JavaTypeMapping sourceMapping, JavaTypeMapping sourceParentMapping, Table target, String targetAlias, JavaTypeMapping targetMapping, JavaTypeMapping targetParentMapping, Object[] discrimValues, String tableGrpName, boolean applyToUnions, SQLJoin parentJoin)
Method to form a join to the specified table using the provided mappings, with the join condition derived from the source-target mappings.SQLTable
join(SQLJoin.JoinType joinType, SQLTable sourceTable, Table target, String targetAlias, String tableGrpName, BooleanExpression joinCondition, boolean applyToUnions)
Method to form a join to the specified table using the provided mappings and applying the provided join condition (rather than generating one from the source/target mappings).String
removeCrossJoin(SQLTable targetSqlTbl)
Method to remove a cross join for the specified table (if joined via cross join).int[]
select(SQLExpression expr, String alias)
Select an expression.int[]
select(SQLTable table, JavaTypeMapping mapping, String alias)
Add a select clause for the specified field (via its mapping) and apply to unions.int[]
select(SQLTable table, JavaTypeMapping mapping, String alias, boolean applyToUnions)
Add a select clause for the specified field (via its mapping).int
select(SQLTable table, Column column, String alias)
Add a select clause for the specified column.protected int
selectItem(SQLText st, String alias, boolean primary)
Internal method to find the position of an item in the select list and return the position if found (first position is 1).void
setAllowUnions(boolean flag)
void
setDistinct(boolean distinct)
Mutator for whether the query returns distinct results.void
setHaving(BooleanExpression expr)
Mutator for the "having" expression.void
setOrdering(SQLExpression[] exprs, boolean[] descending)
Mutator for the ordering criteria.void
setOrdering(SQLExpression[] exprs, boolean[] descending, org.datanucleus.store.query.NullOrderingType[] nullOrders)
Mutator for the ordering criteria.void
setRange(long offset, long count)
Method to add a range constraint on any SELECT.void
union(SelectStatement stmt)
Method to union this SQL statement with another SQL statement.void
whereAnd(BooleanExpression expr, boolean applyToUnions)
Method to add an AND condition to the WHERE clause.void
whereOr(BooleanExpression expr, boolean applyToUnions)
Method to add an OR condition to the WHERE clause.-
Methods inherited from class org.datanucleus.store.rdbms.sql.SQLStatement
addExtension, addJoin, getCandidateClassName, getClassLoaderResolver, getDatastoreAdapter, getJoinConditionForJoin, getJoinForTable, getJoinTypeForTable, getNumberOfTableGroups, getNumberOfTables, getParentStatement, getPrimaryTable, getQueryGenerator, getRDBMSManager, getSQLExpressionFactory, getTable, getTable, getTableForDatastoreContainer, getTableGroup, getTableNamer, getValueForExtension, invalidateStatement, isChildStatementOf, join, join, join, join, log, putSQLTableInGroup, setCandidateClassName, setClassLoaderResolver, setQueryGenerator
-
-
-
-
Field Detail
-
allowUnions
protected boolean allowUnions
Whether to make use of any UNIONs on this statement (for when we just want to use this statement on its own).
-
unions
protected List<SelectStatement> unions
List of unioned SelectStatements (if any).
-
distinct
protected boolean distinct
Whether the statement is distinct.
-
selectedItems
protected List<SelectStatement.SelectedItem> selectedItems
List of selected items, including any alias to use.
-
aggregated
protected boolean aggregated
whether there is an aggregate expression present in the select
-
groupingExpressions
protected List<SQLExpression> groupingExpressions
Expression(s) for the GROUP BY clause.
-
having
protected BooleanExpression having
Having clause.
-
orderingExpressions
protected SQLExpression[] orderingExpressions
Expressions for any ORDER BY clause.
-
orderingDirections
protected boolean[] orderingDirections
Directions for any ORDER BY expressions (1 for each orderingExpressions entry).
-
orderNullDirectives
protected org.datanucleus.store.query.NullOrderingType[] orderNullDirectives
Directives for null handling of any ORDER BY expressions (1 for each orderingExpressions entry).
-
rangeOffset
protected long rangeOffset
The offset for any range restriction.
-
rangeCount
protected long rangeCount
The number of records to be retrieved in any range restriction.
-
-
Constructor Detail
-
SelectStatement
public SelectStatement(RDBMSStoreManager rdbmsMgr, Table table, DatastoreIdentifier alias, String tableGroupName)
Constructor for a SELECT statement.- Parameters:
rdbmsMgr
- Store Managertable
- The primary table to DELETEalias
- Alias for the primary tabletableGroupName
- Group name for the primary table
-
SelectStatement
public SelectStatement(RDBMSStoreManager rdbmsMgr, Table table, DatastoreIdentifier alias, String tableGroupName, Map<String,Object> extensions)
Constructor for a SELECT statement.- Parameters:
rdbmsMgr
- Store Managertable
- The primary table to DELETEalias
- Alias for the primary tabletableGroupName
- Group name for the primary tableextensions
- Any extensions (optional)
-
SelectStatement
public SelectStatement(SQLStatement parentStmt, RDBMSStoreManager rdbmsMgr, Table table, DatastoreIdentifier alias, String tableGroupName)
Constructor for a SELECT statement, maybe as a subquery.- Parameters:
parentStmt
- Parent statement when this is a subquery SELECT.rdbmsMgr
- Store Managertable
- The primary table to DELETEalias
- Alias for the primary tabletableGroupName
- Group name for the primary table
-
SelectStatement
public SelectStatement(SQLStatement parentStmt, RDBMSStoreManager rdbmsMgr, Table table, DatastoreIdentifier alias, String tableGroupName, Map<String,Object> extensions)
Constructor for a SELECT statement, maybe as a subquery.- Parameters:
parentStmt
- Parent statement when this is a subquery SELECT.rdbmsMgr
- Store Managertable
- The primary table to DELETEalias
- Alias for the primary tabletableGroupName
- Group name for the primary tableextensions
- Any extensions (optional)
-
-
Method Detail
-
isDistinct
public boolean isDistinct()
Accessor for whether the statement restricts the results to distinct.- Returns:
- Whether results are distinct
-
setDistinct
public void setDistinct(boolean distinct)
Mutator for whether the query returns distinct results.- Parameters:
distinct
- Whether to return distinct
-
getNumberOfSelects
public int getNumberOfSelects()
Accessor for the number of selected items in the SELECT clause.- Returns:
- Number of selected items
-
select
public int[] select(SQLExpression expr, String alias)
Select an expression. This will be used when adding aggregates to the select clause (e.g "COUNT(*)").- Parameters:
expr
- The expression to add to the select statementalias
- Optional alias for this selected expression- Returns:
- The index(es) of the expression in the select
-
select
public int[] select(SQLTable table, JavaTypeMapping mapping, String alias, boolean applyToUnions)
Add a select clause for the specified field (via its mapping). If an alias is supplied and there are more than 1 column for this mapping then they will have names like "{alias}_n" where n is the column number (starting at 0).- Parameters:
table
- The SQLTable to select from (null implies the primary table)mapping
- The mapping for the fieldalias
- optional aliasapplyToUnions
- Whether to apply to unions- Returns:
- The column index(es) in the statement for the specified field (1 is first).
-
select
public int[] select(SQLTable table, JavaTypeMapping mapping, String alias)
Add a select clause for the specified field (via its mapping) and apply to unions. If an alias is supplied and there are more than 1 column for this mapping then they will have names like "{alias}_n" where n is the column number (starting at 0).- Parameters:
table
- The SQLTable to select from (null implies the primary table)mapping
- The mapping for the fieldalias
- optional alias- Returns:
- The column index(es) in the statement for the specified field (1 is first).
-
select
public int select(SQLTable table, Column column, String alias)
Add a select clause for the specified column.- Parameters:
table
- The SQLTable to select from (null implies the primary table)column
- The columnalias
- Optional alias- Returns:
- The column index in the statement for the specified column (1 is first).
-
selectItem
protected int selectItem(SQLText st, String alias, boolean primary)
Internal method to find the position of an item in the select list and return the position if found (first position is 1). If the item is not found then it is added and the new position returned.- Parameters:
st
- SQLText for this selected itemalias
- Any alias (optional)primary
- Whether this selected item is a primary component (i.e column)- Returns:
- Position in the selectedItems list (first position is 1)
-
addAndConditionToJoinForTable
public void addAndConditionToJoinForTable(SQLTable sqlTbl, BooleanExpression andCondition, boolean applyToUnions)
Method to find the JOIN for the specified table and add the specified 'and' condition to the JOIN as an 'ON' clause.- Overrides:
addAndConditionToJoinForTable
in classSQLStatement
- Parameters:
sqlTbl
- The tableandCondition
- The 'ON' condition to addapplyToUnions
- Whether to apply to unions (see SelectStatement)
-
addGroupingExpression
public void addGroupingExpression(SQLExpression expr)
Method to add a grouping expression to the query. Adds the grouping to any unioned queries- Parameters:
expr
- The expression
-
setHaving
public void setHaving(BooleanExpression expr)
Mutator for the "having" expression.- Parameters:
expr
- Boolean expression for the having clause
-
setOrdering
public void setOrdering(SQLExpression[] exprs, boolean[] descending)
Mutator for the ordering criteria.- Parameters:
exprs
- The expressions to order bydescending
- Whether each expression is ascending/descending
-
setOrdering
public void setOrdering(SQLExpression[] exprs, boolean[] descending, org.datanucleus.store.query.NullOrderingType[] nullOrders)
Mutator for the ordering criteria.- Parameters:
exprs
- The expressions to order bydescending
- Whether each expression is ascending/descendingnullOrders
- Ordering for nulls (if provided)
-
setRange
public void setRange(long offset, long count)
Method to add a range constraint on any SELECT. This typically will use LIMIT/OFFSET where they are supported by the underlying RDBMS.- Parameters:
offset
- The offset to start fromcount
- The number of records to return
-
getSQLText
public SQLText getSQLText()
- Overrides:
getSQLText
in classSQLStatement
-
getSqlForJoins
protected SQLText getSqlForJoins(boolean lock)
Convenience method to return the JOIN clause implied by the "joins" List.- Parameters:
lock
- Whether to add locking on the join clause (only for some RDBMS)- Returns:
- The SQL for the join clause
-
generateOrderingStatement
protected SQLText generateOrderingStatement()
Convenience method to generate the ordering statement to add to the overall query statement.- Returns:
- The ordering statement
-
addOrderComponent
protected void addOrderComponent(SQLText orderST, String orderString, SQLExpression orderExpr, boolean orderDirection, org.datanucleus.store.query.NullOrderingType orderNullDirective, DatastoreAdapter dba)
-
addOrderingColumnsToSelect
protected void addOrderingColumnsToSelect()
Convenience method to add any necessary columns to the SELECT that are needed by the ordering constraint.
-
setAllowUnions
public void setAllowUnions(boolean flag)
-
getNumberOfUnions
public int getNumberOfUnions()
-
getUnions
public List<SelectStatement> getUnions()
Accessor for the unioned statements.- Returns:
- The unioned SQLStatements
-
union
public void union(SelectStatement stmt)
Method to union this SQL statement with another SQL statement.- Parameters:
stmt
- The other SQL statement to union
-
allUnionsForSamePrimaryTable
public boolean allUnionsForSamePrimaryTable()
Convenience accessor for whether all unions of this statement are for the same primary table.- Returns:
- Whether all unions have the same primary table
-
join
public SQLTable join(SQLJoin.JoinType joinType, SQLTable sourceTable, JavaTypeMapping sourceMapping, JavaTypeMapping sourceParentMapping, Table target, String targetAlias, JavaTypeMapping targetMapping, JavaTypeMapping targetParentMapping, Object[] discrimValues, String tableGrpName, boolean applyToUnions, SQLJoin parentJoin)
Description copied from class:SQLStatement
Method to form a join to the specified table using the provided mappings, with the join condition derived from the source-target mappings.- Overrides:
join
in classSQLStatement
- Parameters:
joinType
- Type of join.sourceTable
- SQLTable for the source (null implies primaryTable)sourceMapping
- Mapping in this table to join fromsourceParentMapping
- Optional, if this source mapping is a sub mapping (e.g interface impl).target
- Table to join totargetAlias
- Alias for the target table (if known)targetMapping
- Mapping in the other table to join to (also defines the table to join to)targetParentMapping
- Optional, if this source mapping is a sub mapping (e.g interface impl).discrimValues
- Any discriminator values to apply for the joined table (null if not)tableGrpName
- Name of the table group for the target (null implies a new group)applyToUnions
- Whether to apply to any unioned statements (only applies to SELECT statements)parentJoin
- Parent join when this join will be a sub-join (part of "join grouping")- Returns:
- SQLTable for the target
-
join
public SQLTable join(SQLJoin.JoinType joinType, SQLTable sourceTable, Table target, String targetAlias, String tableGrpName, BooleanExpression joinCondition, boolean applyToUnions)
Description copied from class:SQLStatement
Method to form a join to the specified table using the provided mappings and applying the provided join condition (rather than generating one from the source/target mappings). This is used with JPQL where we allow two root entities to be joined using a provide "ON" condition.- Overrides:
join
in classSQLStatement
- Parameters:
joinType
- Type of join.sourceTable
- SQLTable for the source (null implies primaryTable)target
- Table to join totargetAlias
- Alias for the target table (if known)tableGrpName
- Name of the table group for the target (null implies a new group)joinCondition
- On clause for the joinapplyToUnions
- Whether to apply to any unioned statements (only applies to SELECT statements)- Returns:
- SQLTable for the target
-
removeCrossJoin
public String removeCrossJoin(SQLTable targetSqlTbl)
Description copied from class:SQLStatement
Method to remove a cross join for the specified table (if joined via cross join). Also removes the table from the list of tables. This is called where we have bound a variable via a CROSS JOIN (in the absence of better information) and found out later it could become an INNER JOIN. If the supplied table is not joined via a cross join then does nothing.- Overrides:
removeCrossJoin
in classSQLStatement
- Parameters:
targetSqlTbl
- The table to drop the cross join for- Returns:
- The removed alias
-
whereAnd
public void whereAnd(BooleanExpression expr, boolean applyToUnions)
Method to add an AND condition to the WHERE clause.- Overrides:
whereAnd
in classSQLStatement
- Parameters:
expr
- The conditionapplyToUnions
- whether to apply this and to any UNIONs in the statement
-
whereOr
public void whereOr(BooleanExpression expr, boolean applyToUnions)
Method to add an OR condition to the WHERE clause.- Overrides:
whereOr
in classSQLStatement
- Parameters:
expr
- The conditionapplyToUnions
- Whether to apply to unions
-
-