public class SelectQuery extends BaseCTEQuery<SelectQuery>
Note that the "OFFSET" and "FETCH NEXT" clauses are supported from "SQL 2008".
If Columns are used for any referenced columns, and no complicated joins
are required, the table list may be left empty and it will be
auto-generated in the append call. Note, that this is not the most
efficient method (as this list will not be cached for the future due to
mutability constraints on appendTo
).
Note that this query supports custom SQL syntax, see SelectQuery.Hook
for more
details.
Modifier and Type | Class and Description |
---|---|
static class |
SelectQuery.Hook
The HookAnchors supported for SELECT queries.
|
static class |
SelectQuery.JoinType
Enum which defines the join types supported in a FROM clause.
|
ALL_SYMBOL, NULL_VALUE, QUESTION_MARK
Constructor and Description |
---|
SelectQuery() |
SelectQuery(boolean isDistinct) |
Modifier and Type | Method and Description |
---|---|
SelectQuery |
addAliasedColumn(Object column,
String alias)
Adds the given column with the given alias to the SELECT column list.
|
SelectQuery |
addAllColumns()
Adds the ALL_SYMBOL to the select column list.
|
SelectQuery |
addAllTableColumns(Table table)
Adds a
"<alias>.*" column to the select column
list. |
SelectQuery |
addColumns(Column... columns)
Adds the given columns to the SELECT column list.
|
SelectQuery |
addCondition(Condition newCondition)
Adds a condition to the WHERE clause for the select query (AND'd with any
other WHERE conditions).
|
SelectQuery |
addCustomColumns(Object... columnStrs)
Adds the given columns to the SELECT column list.
|
SelectQuery |
addCustomFromTable(Object tableStr)
Adds a table to the FROM clause, should not be used with any
add*Join methods |
SelectQuery |
addCustomGroupings(Object... columnStrs)
Adds the given columns to the "GROUP BY" clause
|
SelectQuery |
addCustomization(CustomSyntax obj)
Adds custom SQL to this query.
|
SelectQuery |
addCustomization(SelectQuery.Hook hook,
HookType type,
Object obj)
Adds custom SQL to this query.
|
SelectQuery |
addCustomJoin(Object joinStr)
Adds a custom join string.
|
SelectQuery |
addCustomJoin(SelectQuery.JoinType joinType,
Object fromTableStr,
Object toTableStr,
Condition joinCond)
Adds a join of the given type from fromTableStr to toTableStr on
joinCond of the given join type.
|
SelectQuery |
addCustomOrdering(Object columnStr,
OrderObject.Dir dir)
Adds the given column with the given direction to the "ORDER BY"
clause
|
SelectQuery |
addCustomOrderings(Object... columnStrs)
Adds the given columns to the "ORDER BY" clause
|
SelectQuery |
addFromTable(Table table)
Adds a table to the FROM clause, should not be used with any
add*Join methods |
SelectQuery |
addGroupings(Column... columns)
Adds the given columns to the "GROUP BY" clause
|
SelectQuery |
addHaving(Condition newCondition)
Adds a condition to the HAVING clause for the select query (AND'd with
any other HAVING conditions).
|
SelectQuery |
addIndexedOrdering(Integer columnIdx,
OrderObject.Dir dir)
Adds the given column index with the given direction to the "ORDER BY"
clause
|
SelectQuery |
addIndexedOrderings(Integer... columnIdxs)
Adds the given column index to the "ORDER BY" clause
|
SelectQuery |
addJoin(SelectQuery.JoinType joinType,
Table fromTable,
Table toTable,
Column fromColumn,
Column toColumn)
Adds a join of the given type from fromTable to toTable with a join
condition requiring fromColumn to equal toColumn.
|
SelectQuery |
addJoin(SelectQuery.JoinType joinType,
Table fromTable,
Table toTable,
Condition joinCond)
Adds a join of the given type from fromTable to toTable on joinCond of
the given join type.
|
SelectQuery |
addJoin(SelectQuery.JoinType joinType,
Table fromTable,
Table toTable,
List<? extends Column> fromColumns,
List<? extends Column> toColumns)
Adds a join of the given type from fromTable to toTable with a join
condition requiring each column in fromColumns to equal the corresponding
column in toColumns.
|
SelectQuery |
addJoins(SelectQuery.JoinType joinType,
Join... joins)
Adds all of the joins of the given join type where each join is from
join.getFromTable() to join.getToTable() with a join condition
requiring each column in join.getFromColumns() to equal the
corresponding column in join.getToColumns().
|
SelectQuery |
addOrdering(Column column,
OrderObject.Dir dir)
Adds the given column with the given direction to the "ORDER BY"
clause
|
SelectQuery |
addOrderings(Column... columns)
Adds the given columns to the "ORDER BY" clause
|
SelectQuery |
addWindowDefinition(String name,
Object window)
Adds a named window definition to the select query's WINDOW definitions
|
protected void |
appendTo(AppendableExt app,
SqlContext newContext)
Appends the sql query to the given AppendableExt within the given,
modifiable SqlContext.
|
protected void |
collectSchemaObjects(ValidationContext vContext)
Used during Query.validate() calls to collect the dbschema objects
referenced in a query.
|
ComboCondition |
getHavingClause()
Allows access to the AND ComboCondition of the having clause to
facilitate common condition building code.
|
ComboCondition |
getWhereClause()
Allows access to the AND ComboCondition of the where clause to facilitate
common condition building code.
|
SelectQuery |
setFetchNext(Object fetchCount)
Sets the value for the "FETCH NEXT" clause.
|
SelectQuery |
setForUpdate(boolean forUpdate)
Iff forUpdate is
true , adds the FOR UPDATE clause to the
end of the SELECT clause |
SelectQuery |
setIsDistinct(boolean isDistinct)
Iff isDistinct is
true , adds the DISTINCT keyword to the
SELECT clause so that only unique rows are returned |
SelectQuery |
setOffset(Object offset)
Sets the value for the "OFFSET" clause.
|
void |
validate(ValidationContext vContext)
Runs validation on this verifiable object using a previously collected
ValidationContext.
|
protected static void |
validateOrdering(int numColumns,
SqlObjectList<SqlObject> ordering,
boolean ignoreColumnCount)
Checks any indexed ordering values for validity using a variety of
criteria.
|
addCommonTableExpression, prependTo, setRecursive
appendTo, getThisType, validate, validateTables
addCustomization, customAppendTo, customAppendTo, maybeAppendTo, maybeAppendTo
collectSchemaObjects, doValidate, toString
toString, toString
public SelectQuery()
public SelectQuery(boolean isDistinct)
public SelectQuery setIsDistinct(boolean isDistinct)
true
, adds the DISTINCT keyword to the
SELECT clause so that only unique rows are returnedpublic SelectQuery setForUpdate(boolean forUpdate)
true
, adds the FOR UPDATE clause to the
end of the SELECT clausepublic SelectQuery addCustomColumns(Object... columnStrs)
Object
-> SqlObject
conversions handled by
Converter.COLUMN_VALUE_TO_OBJ
.
public SelectQuery addAllColumns()
public SelectQuery addAllTableColumns(Table table)
"<alias>.*"
column to the select column
list.public SelectQuery addColumns(Column... columns)
public SelectQuery addAliasedColumn(Object column, String alias)
addCustomColumns(Converter.toColumnSqlObject(column, alias))
.public SelectQuery addCustomFromTable(Object tableStr)
add*Join
methods
Object
-> SqlObject
conversions handled by
Converter.toCustomTableDefSqlObject(Object)
.
public SelectQuery addFromTable(Table table)
add*Join
methodspublic SelectQuery addCustomJoin(Object joinStr)
Object
-> SqlObject
conversions handled by
Converter.toCustomTableDefSqlObject(Object)
.
public SelectQuery addCustomJoin(SelectQuery.JoinType joinType, Object fromTableStr, Object toTableStr, Condition joinCond)
Object
-> SqlObject
conversions handled by
Converter.toCustomTableDefSqlObject(Object)
.
public SelectQuery addJoin(SelectQuery.JoinType joinType, Table fromTable, Table toTable, Condition joinCond)
public SelectQuery addJoin(SelectQuery.JoinType joinType, Table fromTable, Table toTable, List<? extends Column> fromColumns, List<? extends Column> toColumns)
public SelectQuery addJoin(SelectQuery.JoinType joinType, Table fromTable, Table toTable, Column fromColumn, Column toColumn)
public SelectQuery addJoins(SelectQuery.JoinType joinType, Join... joins)
public SelectQuery addCustomOrdering(Object columnStr, OrderObject.Dir dir)
Object
-> SqlObject
conversions handled by
Converter.toCustomColumnSqlObject(Object)
.
public SelectQuery addCustomOrderings(Object... columnStrs)
Note, NULL ordering can be specified by passing an appropriately
configured instance of OrderObject
to this method.
Object
-> SqlObject
conversions handled by
Converter.CUSTOM_COLUMN_TO_OBJ
.
public SelectQuery addOrdering(Column column, OrderObject.Dir dir)
public SelectQuery addOrderings(Column... columns)
public SelectQuery addIndexedOrdering(Integer columnIdx, OrderObject.Dir dir)
public SelectQuery addIndexedOrderings(Integer... columnIdxs)
public SelectQuery addCustomGroupings(Object... columnStrs)
Object
-> SqlObject
conversions handled by
Converter.CUSTOM_COLUMN_TO_OBJ
.
public SelectQuery addGroupings(Column... columns)
public ComboCondition getWhereClause()
public SelectQuery addCondition(Condition newCondition)
For convenience purposes, the SelectQuery generates it's own ComboCondition allowing multiple conditions to be AND'd together. To OR conditions or perform other logic, the ComboCondition must be built and added to the SelectQuery.
public ComboCondition getHavingClause()
public SelectQuery addHaving(Condition newCondition)
For convenience purposes, the SelectQuery generates it's own ComboCondition allowing multiple HAVING conditions to be AND'd together. To OR conditions or perform other logic, the ComboCondition must be built and added to the SelectQuery.
public SelectQuery addWindowDefinition(String name, Object window)
Object
-> SqlObject
conversions handled by
Converter.toCustomSqlObject(java.lang.Object)
.
public SelectQuery setOffset(Object offset)
Object
-> SqlObject
conversions handled by
Converter.toValueSqlObject(java.lang.Object)
.
public SelectQuery setFetchNext(Object fetchCount)
Object
-> SqlObject
conversions handled by
Converter.toValueSqlObject(java.lang.Object)
.
public SelectQuery addCustomization(SelectQuery.Hook hook, HookType type, Object obj)
com.healthmarketscience.sqlbuilder.custom
for more details on
custom SQL syntax.hook
- the part of the query being customizedtype
- the type of customizationobj
- the custom sql. The Object
-> SqlObject
conversions handled by Converter.toCustomSqlObject(java.lang.Object)
.public SelectQuery addCustomization(CustomSyntax obj)
com.healthmarketscience.sqlbuilder.custom
for more details on
custom SQL syntax.obj
- the custom sql syntax on which the
CustomSyntax.apply(SelectQuery)
method will be
invoked (may be null
).protected void collectSchemaObjects(ValidationContext vContext)
SqlObject
collectSchemaObjects
in class BaseCTEQuery<SelectQuery>
vContext
- handle to the current validation contextpublic void validate(ValidationContext vContext) throws ValidationException
Verifiable
In general, this method will only be called internally, not by users.
validate
in interface Verifiable<SelectQuery>
validate
in class Query<SelectQuery>
vContext
- handle to the current, filled-in validation contextValidationException
protected static void validateOrdering(int numColumns, SqlObjectList<SqlObject> ordering, boolean ignoreColumnCount) throws ValidationException
numColumns
- number of column objects in the queryordering
- the ordering objects for the queryignoreColumnCount
- whether the given numColumns is meaningful
(i.e. does not include a '*' character)ValidationException
protected void appendTo(AppendableExt app, SqlContext newContext) throws IOException
Query
Query.appendTo(AppendableExt)
method within the context of calls to
SqlContext.pushContext(com.healthmarketscience.common.util.AppendableExt)
and SqlContext.popContext(com.healthmarketscience.common.util.AppendableExt, com.healthmarketscience.sqlbuilder.SqlContext)
, so
the implementation is free to modify the given SqlContext.appendTo
in class Query<SelectQuery>
app
- the target for the sql query generationnewContext
- modifiable SqlContext for nested AppendeesIOException
Copyright © 2006–2021 OpenHMS. All rights reserved.