Class Select
java.lang.Object
org.tentackle.dbms.dsl.Select
- All Implemented Interfaces:
SqlElement
A
SELECT-statement.
Created by DSL.select(Expression...) and refined by the clause methods, each returning this select:
List<Turnover> turnovers =
DSL.select(ORDER.CUSTOMER, DSL.sum(ORDER.NET).as("turnover"))
.from(ORDER)
.where(ORDER.ORDERED.ge(firstOfYear))
.groupBy(ORDER.CUSTOMER)
.orderBy(ORDER.CUSTOMER.asc())
.toList(db, Turnover.class);
The clauses may be added in any order and more than once. Conditions passed to
where(Condition...) and having(Condition...) are combined by a logical AND, which is what
queries assembled step by step usually need:
Select select = DSL.select().from(CUSTOMER);
if (name != null) {
select.where(CUSTOMER.NAME.like(name + "%"));
}
A select is a mutable builder and thus not thread-safe. The expressions and
conditions it is built from, however, are immutable and may be shared.
Selects deliver plain data, not PDOs, and there is no security manager involved. Use them for projections, aggregates and reports, not to load domain objects.
- Author:
- harald
-
Constructor Summary
ConstructorsConstructorDescriptionSelect(boolean distinct, Collection<? extends Expression<?>> projection) Creates a select. -
Method Summary
Modifier and TypeMethodDescriptionExecutes this select.
The application is responsible for closing the returned resultset.fetchSize(int fetchSize) Sets the fetch size.Adds tables to theFROM-clause.Adds a full outer join.intgetRowCount(Db db) Gets the number of rows returned by this select.
The rows are counted by the backend, i.e. they are not retrieved.groupBy(Expression<?>... expressions) Adds expressions to theGROUP BY-clause.Adds conditions to theHAVING-clause.
All conditions of a select are combined by a logical AND.Adds an inner join.Adds a join.Adds a left outer join.limit(int limit) Sets the maximum number of rows to retrieve.
TheLIMIT-clause is backend-specific and thus added by theBackend.maxRows(int maxRows) Sets the maximum number of rows fetched in total.offset(int offset) Sets the number of rows to skip.
TheOFFSET-clause is backend-specific and thus added by theBackend.orderBy(Expression<?>... expressions) Adds ascending expressions to theORDER BY-clause.Adds sort fields to theORDER BY-clause.voidrender(RenderContext context) Renders this select as a sub-select.Adds a right outer join.statementCached(boolean statementCached) Enables caching of the prepared statement.<T> List<T> Executes this select and maps the rows to DTOs.<T> Optional<T> toOptional(Db db, Class<T> dtoClass) Executes this select and maps at most one row to a DTO.Creates the query for this select.Creates the query for this select.<T> Optional<T> Executes this select and retrieves the value of the first expression of the projection of at most one row.<T> List<T> toScalarList(Db db, Class<T> type) Executes this select and retrieves the value of the first expression of the projection for each row.Creates the SQL-code of this select.
Notice that theLIMIT- andOFFSET-clauses are added by the backend when the query is executed and thus are not part of the returned code.toString()Adds conditions to theWHERE-clause.
All conditions of a select are combined by a logical AND.
-
Constructor Details
-
Select
Creates a select.- Parameters:
distinct- true forSELECT DISTINCTprojection- the selected expressions, empty to select all columns
-
-
Method Details
-
from
-
join
-
leftJoin
-
rightJoin
-
fullJoin
-
join
-
where
-
groupBy
Adds expressions to theGROUP BY-clause.- Parameters:
expressions- the expressions- Returns:
- this select
-
having
-
orderBy
-
orderBy
Adds ascending expressions to theORDER BY-clause.- Parameters:
expressions- the expressions- Returns:
- this select
-
limit
Sets the maximum number of rows to retrieve.
TheLIMIT-clause is backend-specific and thus added by theBackend. It applies to the executed statement only, i.e. a select used as a sub-select must not define a limit.- Parameters:
limit- the maximum number of rows, 0 if unlimited (default)- Returns:
- this select
- See Also:
-
offset
Sets the number of rows to skip.
TheOFFSET-clause is backend-specific and thus added by theBackend. It applies to the executed statement only, i.e. a select used as a sub-select must not define an offset.- Parameters:
offset- the number of rows to skip, 0 if none (default)- Returns:
- this select
- See Also:
-
fetchSize
Sets the fetch size.- Parameters:
fetchSize- the fetch size, 0 for the driver's default- Returns:
- this select
- See Also:
-
maxRows
Sets the maximum number of rows fetched in total.- Parameters:
maxRows- the maximum number of rows, 0 if unlimited- Returns:
- this select
- See Also:
-
statementCached
Enables caching of the prepared statement.- Parameters:
statementCached- true to prepare only once and reuse the cached statement, false if one-shot (default)- Returns:
- this select
- See Also:
-
render
Renders this select as a sub-select.Renders this element into the given context.
- Specified by:
renderin interfaceSqlElement- Parameters:
context- the render context
-
toSql
-
toQuery
-
toQuery
-
execute
Executes this select.
The application is responsible for closing the returned resultset.- Parameters:
db- the session- Returns:
- the resultset
-
toList
-
toOptional
Executes this select and maps at most one row to a DTO.- Type Parameters:
T- the DTO type- Parameters:
db- the sessiondtoClass- the DTO class- Returns:
- the optional DTO, empty if no row at all
- Throws:
PersistenceException- if more than one row was returned
-
toScalarList
-
toScalar
Executes this select and retrieves the value of the first expression of the projection of at most one row.- Type Parameters:
T- the value type- Parameters:
db- the sessiontype- the java type of the value- Returns:
- the optional value, empty if no row at all or if the value is null
- Throws:
PersistenceException- if more than one row was returned
-
getRowCount
Gets the number of rows returned by this select.
The rows are counted by the backend, i.e. they are not retrieved.- Parameters:
db- the session- Returns:
- the number of rows
- See Also:
-
toString
-