public class SelectStatement extends SQLStatement
SELECT {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.
Modifier and Type | Class and Description |
---|---|
protected class |
SelectStatement.SelectedItem |
Modifier and Type | Field and Description |
---|---|
protected boolean |
aggregated
whether there is an aggregate expression present in the select
|
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).
|
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.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).
|
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 and 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.
|
Modifier and Type | Method and 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.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,
Column column,
String alias)
Add a select clause for the specified column.
|
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).
|
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.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.
|
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
protected boolean allowUnions
protected List<SelectStatement> unions
protected boolean distinct
protected List<SelectStatement.SelectedItem> selectedItems
protected boolean aggregated
protected List<SQLExpression> groupingExpressions
protected BooleanExpression having
protected SQLExpression[] orderingExpressions
protected boolean[] orderingDirections
protected org.datanucleus.query.NullOrderingType[] orderNullDirectives
protected long rangeOffset
protected long rangeCount
public SelectStatement(RDBMSStoreManager rdbmsMgr, Table table, DatastoreIdentifier alias, String tableGroupName)
rdbmsMgr
- Store Managertable
- The primary table to DELETEalias
- Alias for the primary tabletableGroupName
- Group name for the primary tablepublic SelectStatement(RDBMSStoreManager rdbmsMgr, Table table, DatastoreIdentifier alias, String tableGroupName, Map<String,Object> extensions)
rdbmsMgr
- Store Managertable
- The primary table to DELETEalias
- Alias for the primary tabletableGroupName
- Group name for the primary tableextensions
- Any extensions (optional)public SelectStatement(SQLStatement parentStmt, RDBMSStoreManager rdbmsMgr, Table table, DatastoreIdentifier alias, String tableGroupName)
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 tablepublic SelectStatement(SQLStatement parentStmt, RDBMSStoreManager rdbmsMgr, Table table, DatastoreIdentifier alias, String tableGroupName, Map<String,Object> extensions)
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)public boolean isDistinct()
public void setDistinct(boolean distinct)
distinct
- Whether to return distinctpublic int getNumberOfSelects()
public int[] select(SQLExpression expr, String alias)
expr
- The expression to add to the select statementalias
- Optional alias for this selected expressionpublic int[] select(SQLTable table, JavaTypeMapping mapping, String alias, boolean applyToUnions)
table
- The SQLTable to select from (null implies the primary table)mapping
- The mapping for the fieldalias
- optional aliasapplyToUnions
- Whether to apply to unionspublic int[] select(SQLTable table, JavaTypeMapping mapping, String alias)
table
- The SQLTable to select from (null implies the primary table)mapping
- The mapping for the fieldalias
- optional aliaspublic int select(SQLTable table, Column column, String alias)
table
- The SQLTable to select from (null implies the primary table)column
- The columnalias
- Optional aliasprotected int selectItem(SQLText st, String alias, boolean primary)
st
- SQLText for this selected itemalias
- Any alias (optional)primary
- Whether this selected item is a primary component (i.e column)public void addAndConditionToJoinForTable(SQLTable sqlTbl, BooleanExpression andCondition, boolean applyToUnions)
addAndConditionToJoinForTable
in class SQLStatement
sqlTbl
- The tableandCondition
- The 'ON' condition to addapplyToUnions
- Whether to apply to unions (see SelectStatement)public void addGroupingExpression(SQLExpression expr)
expr
- The expressionpublic void setHaving(BooleanExpression expr)
expr
- Boolean expression for the having clausepublic void setOrdering(SQLExpression[] exprs, boolean[] descending)
exprs
- The expressions to order bydescending
- Whether each expression is ascending/descendingpublic void setOrdering(SQLExpression[] exprs, boolean[] descending, org.datanucleus.query.NullOrderingType[] nullOrders)
exprs
- The expressions to order bydescending
- Whether each expression is ascending/descendingnullOrders
- Ordering for nulls (if provided)public void setRange(long offset, long count)
offset
- The offset to start fromcount
- The number of records to returnpublic SQLText getSQLText()
getSQLText
in class SQLStatement
protected SQLText getSqlForJoins(boolean lock)
lock
- Whether to add locking on the join clause (only for some RDBMS)protected SQLText generateOrderingStatement()
protected void addOrderComponent(SQLText orderST, String orderString, SQLExpression orderExpr, boolean orderDirection, org.datanucleus.query.NullOrderingType orderNullDirective, DatastoreAdapter dba)
protected void addOrderingColumnsToSelect()
public void setAllowUnions(boolean flag)
public int getNumberOfUnions()
public List<SelectStatement> getUnions()
public void union(SelectStatement stmt)
stmt
- The other SQL statement to unionpublic boolean allUnionsForSamePrimaryTable()
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)
SQLStatement
join
in class SQLStatement
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")public SQLTable join(SQLJoin.JoinType joinType, SQLTable sourceTable, Table target, String targetAlias, String tableGrpName, BooleanExpression joinCondition, boolean applyToUnions)
SQLStatement
join
in class SQLStatement
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)public String removeCrossJoin(SQLTable targetSqlTbl)
SQLStatement
removeCrossJoin
in class SQLStatement
targetSqlTbl
- The table to drop the cross join forpublic void whereAnd(BooleanExpression expr, boolean applyToUnions)
whereAnd
in class SQLStatement
expr
- The conditionapplyToUnions
- whether to apply this and to any UNIONs in the statementpublic void whereOr(BooleanExpression expr, boolean applyToUnions)
whereOr
in class SQLStatement
expr
- The conditionapplyToUnions
- Whether to apply to unionsCopyright © 2019. All rights reserved.