public class Select extends QuerySource
Builds a SELECT statement with its parameter values; as a QuerySource it can also be nested in another query.
  • Constructor Details

    • Select

      public Select(String... tables)
      Creates a query over the given tables; null entries are skipped, but at least one table is required.
    • Select

      public Select(List<String> tables)
      Creates a query over the given tables; null entries are skipped, but at least one table is required.
    • Select

      public Select(QuerySource... querySources)
      Creates a query over the given sources, such as tables, nested queries or Unions; at least one is required.
  • Method Details

    • setPretty

      public Select setPretty(boolean pretty)
      Description copied from class: SqlPart
      Indicates if SqlPart.getSqlFragment() adds line breaks and indentation instead of using one line (default is false).
      Overrides:
      setPretty in class QuerySource
    • setNestedAlias

      public Select setNestedAlias(String nestedAlias)
      Description copied from class: QuerySource
      Sets the alias written after this source in a FROM clause (null for none).
      Overrides:
      setNestedAlias in class QuerySource
    • getQuerySources

      public List<QuerySource> getQuerySources()
    • getTables

      public List<String> getTables()
      Description copied from class: QuerySource
      Returns the tables this source reads from, including those of nested sources but not joined tables.
      Specified by:
      getTables in class QuerySource
    • getTable

      public String getTable()

      Returns the first table in the list of tables.


      If this select query involves more than one table, use getTables() instead of this method.
      Specified by:
      getTable in class QuerySource
    • getJoins

      public List<Join> getJoins()
    • containsTable

      public boolean containsTable(String table)
      Description copied from class: QuerySource
      Returns true if this source, a nested source or a join refers to the table (ignoring case).
      Specified by:
      containsTable in class QuerySource
    • getSelection

      public QuerySelection getSelection()
      Returns the SELECT column list (never null).
    • setSelection

      public Select setSelection(QuerySelection selection)
      Replaces the SELECT column list; null installs an empty one, which selects *.
    • select

      public Select select(String... fields)
      Adds columns or expressions to the SELECT list; * is selected if none are added.
    • innerJoin

      public Select innerJoin(String table, String condition)
    • leftJoin

      public Select leftJoin(String table, String condition)
    • rightJoin

      public Select rightJoin(String table, String condition)
    • fullJoin

      public Select fullJoin(String table, String condition)
    • getCriteria

      @Deprecated public QueryCriteria getCriteria()
      Deprecated.
    • getWhere

      public QueryCriteria getWhere()
      Returns the WHERE criteria (never null).
    • setWhere

      public Select setWhere(QueryCriteria where)
      Replaces the WHERE criteria, changing its type to WHERE; null installs empty criteria.
    • where

      public Select where(String criteria, Object... value)
      ANDs a condition, such as "id = ?", and its parameter values with the existing WHERE conditions.
    • where

      public Select where(boolean andConjunction, String criteria, Object... value)
      Adds a condition and its parameter values to the WHERE clause with AND (if andConjunction is true) or OR.
    • where

      public Select where(QueryCriteriaPart criteria)
      Replaces all WHERE conditions with the given part.
    • clearWhere

      public Select clearWhere()
    • getGrouping

      public QueryGrouping getGrouping()
      Returns the GROUP BY fields (never null).
    • setGrouping

      public Select setGrouping(QueryGrouping grouping)
      Replaces the GROUP BY fields; null installs an empty grouping.
    • groupBy

      public Select groupBy(String... fields)
    • getHaving

      public QueryCriteria getHaving()
      Returns the HAVING criteria (never null).
    • setHaving

      public Select setHaving(QueryCriteria having)
      Replaces the HAVING criteria, changing its type to HAVING; null installs empty criteria.
    • having

      public Select having(String criteria, Object... value)
      ANDs a condition, such as "SUM(total) > ?", and its parameter values with the existing HAVING conditions.
    • having

      public Select having(boolean andConjunction, String criteria, Object... value)
      Adds a condition and its parameter values to the HAVING clause with AND (if andConjunction is true) or OR.
    • having

      public Select having(QueryCriteriaPart criteria)
      Replaces all HAVING conditions with the given part.
    • clearHaving

      public Select clearHaving()
    • getOrder

      public QueryOrder getOrder()
      Returns the ORDER BY fields (never null).
    • setOrder

      public Select setOrder(QueryOrder order)
      Replaces the ORDER BY fields; null installs an empty ordering.
    • orderBy

      public Select orderBy(String... fields)
      Appends the fields to the ORDER BY clause in ascending order.
    • orderBy

      public Select orderBy(FieldList fields)
      Appends the fields to the ORDER BY clause in ascending order.
    • orderBy

      public Select orderBy(String field, boolean ascending)
    • orderBy

      public Select orderBy(String field, boolean ascending, Boolean nullsFirst)
      Appends a field to the ORDER BY clause, adding NULLS FIRST or NULLS LAST unless nullsFirst is null.
    • limit

      public Select limit(int count)
      Limits the result to count rows and clears any offset; a negative count removes the limit.
    • limit

      public Select limit(Integer offset, Integer count)
      Sets the rows to skip (OFFSET) and the maximum to return (LIMIT); a null or negative value omits that clause.
    • clearLimit

      public Select clearLimit()
    • page

      public Select page(int page, int pageSize)
      Limits the result to the given 0-based page of pageSize rows.
    • getOffset

      public Integer getOffset()
      Returns the number of rows to skip or null if not set.
    • getCount

      public Integer getCount()
      Returns the maximum number of rows to return or null if unlimited.
    • collectSqlFragment

      public void collectSqlFragment(CodeWriter writer)
      Description copied from class: SqlPart
      Writes this part's SQL to the writer; used by SqlPart.getSqlFragment() and by enclosing parts.
      Specified by:
      collectSqlFragment in class SqlPart
    • collectQuerySourceSqlFragment

      protected void collectQuerySourceSqlFragment(CodeWriter writer, QuerySource querySource)
      Writes a FROM source, in parentheses if QuerySource.shouldWrapWhenNested() is true, followed by its nested alias if any.
    • collectParameterValues

      public void collectParameterValues(List<Object> values)
      Description copied from class: SqlPart
      Appends the values for this part's ? parameters to the list in order; adds nothing by default.
      Overrides:
      collectParameterValues in class SqlPart