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 like
 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.
  • Field Details

    • 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.
    • orderingColumnIndexes

      private int[] orderingColumnIndexes
      Positions of order columns in the SELECT (for datastores that require ordering using those).
  • Constructor Details

    • SelectStatement

      public SelectStatement(RDBMSStoreManager rdbmsMgr, Table table, DatastoreIdentifier alias, String tableGroupName)
      Constructor for a SELECT statement.
      Parameters:
      rdbmsMgr - Store Manager
      table - The primary table to DELETE
      alias - Alias for the primary table
      tableGroupName - 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 Manager
      table - The primary table to DELETE
      alias - Alias for the primary table
      tableGroupName - Group name for the primary table
      extensions - 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 Manager
      table - The primary table to DELETE
      alias - Alias for the primary table
      tableGroupName - 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 Manager
      table - The primary table to DELETE
      alias - Alias for the primary table
      tableGroupName - Group name for the primary table
      extensions - Any extensions (optional)
  • Method Details

    • 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 statement
      alias - 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 field
      alias - optional alias
      applyToUnions - 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 field
      alias - 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 column
      alias - 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 item
      alias - 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 class SQLStatement
      Parameters:
      sqlTbl - The table
      andCondition - The 'ON' condition to add
      applyToUnions - 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 by
      descending - 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 by
      descending - Whether each expression is ascending/descending
      nullOrders - 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 from
      count - The number of records to return
    • getSQLText

      public SQLText getSQLText()
      Overrides:
      getSQLText in class SQLStatement
    • reorderJoins

      private List<SQLJoin> reorderJoins(List<SQLJoin> joinsToAdd)
      Convenience method to reorder the joins to be in logical order. If a join needed to be changed during the generation process, it will have been removed and then the replacement added later. This method reorders the joins so that the joins are only relative to "known" tables.
      Parameters:
      joinsToAdd - List of joins
      Returns:
      The ordered list of joins
    • 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 class SQLStatement
      Parameters:
      joinType - Type of join.
      sourceTable - SQLTable for the source (null implies primaryTable)
      sourceMapping - Mapping in this table to join from
      sourceParentMapping - Optional, if this source mapping is a sub mapping (e.g interface impl).
      target - Table to join to
      targetAlias - 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 class SQLStatement
      Parameters:
      joinType - Type of join.
      sourceTable - SQLTable for the source (null implies primaryTable)
      target - Table to join to
      targetAlias - 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 join
      applyToUnions - 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 class SQLStatement
      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 class SQLStatement
      Parameters:
      expr - The condition
      applyToUnions - 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 class SQLStatement
      Parameters:
      expr - The condition
      applyToUnions - Whether to apply to unions