Class EntityQuery<E extends BaseEntity>
java.lang.Object
com.github.collinalpert.java2db.queries.EntityQuery<E>
- All Implemented Interfaces:
Queryable<E>
- Direct Known Subclasses:
AsyncEntityQuery
public class EntityQuery<E extends BaseEntity> extends Object implements Queryable<E>
A class representing a DQL statement with different options, including where clauses, order by clauses and limits.
It also automatically joins foreign keys so the corresponding entities (marked with the
ForeignKeyEntity attribute) can be filled.- Author:
- Collin Alpert
-
Field Summary
Fields Modifier and Type Field Description protected ConnectionConfigurationconnectionConfigurationprotected IQueryBuilder<E>queryBuilderprotected QueryParameters<E>queryParameters -
Constructor Summary
Constructors Constructor Description EntityQuery(Class<E> type, ConnectionConfiguration connectionConfiguration)Constructor for creating a DQL statement for a given entity. -
Method Summary
Modifier and Type Method Description EntityQuery<E>distinct()Adds a DISTINCT modifier to the query.Optional<E>first()Gets the first record of a result.StringgetQuery()Responsible for building and returning the individual DQL statement.protected StringgetTableName()Gets the table name which this query targets.EntityQuery<E>groupBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?> groupBy)EntityQuery<E>groupBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?>... groupBy)EntityQuery<E>limit(int limit)Limits the result of the rows returned to a maximum of the passed integer.EntityQuery<E>limit(int limit, int offset)Limits the result of the rows returned to a maximum of the passed integer with an offset.EntityQuery<E>orderBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?> function)Sets a single column as the ORDER BY clause for the DQL statement in an ascending manner.EntityQuery<E>orderBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?>[] functions)Sets multiple ORDER BY clauses for the DQL statement.EntityQuery<E>orderBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?>[] functions, OrderTypes orderType)Sets multiple ORDER BY clauses for the DQL statement.EntityQuery<E>orderBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?> function, OrderTypes orderType)Sets an ORDER BY clauses for the DQL statement.EntityQuery<E>orderBy(List<com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?>> functions)Sets multiple ORDER BY clauses for the DQL statement.EntityQuery<E>orderBy(List<com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?>> functions, OrderTypes orderType)Sets multiple ORDER BY clauses for the DQL statement.EntityQuery<E>orWhere(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate)Sets or appends an OR WHERE clause to the DQL statement.<R> Queryable<R>project(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,R> projection)Selects only a single column from a table.EntityQuery<E>thenBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?> function)Sets a single column as the ORDER BY clause for the DQL statement in an ascending manner.EntityQuery<E>thenBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?>[] functions)Sets multiple ORDER BY clauses for the DQL statement.EntityQuery<E>thenBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?>[] functions, OrderTypes orderType)Sets multiple ORDER BY clauses for the DQL statement.EntityQuery<E>thenBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?> function, OrderTypes orderType)Sets an ORDER BY clauses for the DQL statement.EntityQuery<E>thenBy(List<com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?>> functions)Sets multiple ORDER BY clauses for the DQL statement.EntityQuery<E>thenBy(List<com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?>> functions, OrderTypes orderType)Sets multiple ORDER BY clauses for the DQL statement.E[]toArray()Executes a new query and returns the result as an array.List<E>toList()Executes the query and returns the result as aList<K, V> Map<K,V>toMap(Function<E,K> keyMapping, Function<E,V> valueMapping)Executes a new query and returns the result as aMap.Set<E>toSet()Executes the query and returns the result as aSet.Stream<E>toStream()Executes the query and returns the result as aStreamEntityQuery<E>where(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate)Sets or appends a WHERE clause for the DQL statement.
-
Field Details
-
Constructor Details
-
EntityQuery
Constructor for creating a DQL statement for a given entity. This constructor should not be used directly, but through the DQL methods defined in theBaseService.- Parameters:
type- The entity to query.
-
-
Method Details
-
where
public EntityQuery<E> where(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate)Sets or appends a WHERE clause for the DQL statement.- Parameters:
predicate- The predicate describing the WHERE clause.- Returns:
- This
EntityQueryobject, now with an (appended) WHERE clause.
-
orWhere
public EntityQuery<E> orWhere(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate)Sets or appends an OR WHERE clause to the DQL statement.- Parameters:
predicate- The predicate describing the OR WHERE clause.- Returns:
- This
EntityQueryobject, now with an (appended) OR WHERE clause.
-
orderBy
public EntityQuery<E> orderBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?> function)Sets a single column as the ORDER BY clause for the DQL statement in an ascending manner.- Parameters:
function- The column to order by.- Returns:
- This
EntityQueryobject, now with a ORDER BY clause.
-
orderBy
public EntityQuery<E> orderBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?> function, OrderTypes orderType)Sets an ORDER BY clauses for the DQL statement.- Parameters:
function- The column to order by.- Returns:
- This
EntityQueryobject, now with a ORDER BY clause.
-
orderBy
public EntityQuery<E> orderBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?>[] functions)Sets multiple ORDER BY clauses for the DQL statement. The resulting ORDER BY statement will coalesce the passed columns.- Parameters:
functions- The columns to order by in a coalescing manner.- Returns:
- This
EntityQueryobject, now with a coalesced ORDER BY clause.
-
orderBy
public EntityQuery<E> orderBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?>[] functions, OrderTypes orderType)Sets multiple ORDER BY clauses for the DQL statement. The resulting ORDER BY statement will coalesce the passed columns.- Parameters:
functions- The columns to order by in a coalescing manner.- Returns:
- This
EntityQueryobject, now with a coalesced ORDER BY clause.
-
orderBy
public EntityQuery<E> orderBy(List<com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?>> functions)Sets multiple ORDER BY clauses for the DQL statement. The resulting ORDER BY statement will coalesce the passed columns.- Parameters:
functions- The columns to order by in a coalescing manner.- Returns:
- This
EntityQueryobject, now with a coalesced ORDER BY clause.
-
orderBy
public EntityQuery<E> orderBy(List<com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?>> functions, OrderTypes orderType)Sets multiple ORDER BY clauses for the DQL statement. The resulting ORDER BY statement will coalesce the passed columns.- Parameters:
functions- The columns to order by in a coalescing manner.- Returns:
- This
EntityQueryobject, now with a coalesced ORDER BY clause.
-
groupBy
public EntityQuery<E> groupBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?> groupBy) -
groupBy
public EntityQuery<E> groupBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?>... groupBy) -
thenBy
public EntityQuery<E> thenBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?> function)Sets a single column as the ORDER BY clause for the DQL statement in an ascending manner.- Parameters:
function- The column to order by.- Returns:
- This
EntityQueryobject, now with a ORDER BY clause.
-
thenBy
public EntityQuery<E> thenBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?> function, OrderTypes orderType)Sets an ORDER BY clauses for the DQL statement.- Parameters:
function- The column to order by.- Returns:
- This
EntityQueryobject, now with a ORDER BY clause.
-
thenBy
public EntityQuery<E> thenBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?>[] functions)Sets multiple ORDER BY clauses for the DQL statement. The resulting ORDER BY statement will coalesce the passed columns.- Parameters:
functions- The columns to order by in a coalescing manner.- Returns:
- This
EntityQueryobject, now with a coalesced ORDER BY clause.
-
thenBy
public EntityQuery<E> thenBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?>[] functions, OrderTypes orderType)Sets multiple ORDER BY clauses for the DQL statement. The resulting ORDER BY statement will coalesce the passed columns.- Parameters:
functions- The columns to order by in a coalescing manner.- Returns:
- This
EntityQueryobject, now with a coalesced ORDER BY clause.
-
thenBy
public EntityQuery<E> thenBy(List<com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?>> functions)Sets multiple ORDER BY clauses for the DQL statement. The resulting ORDER BY statement will coalesce the passed columns.- Parameters:
functions- The columns to order by in a coalescing manner.- Returns:
- This
EntityQueryobject, now with a coalesced ORDER BY clause.
-
thenBy
public EntityQuery<E> thenBy(List<com.github.collinalpert.lambda2sql.functions.SqlFunction<E,?>> functions, OrderTypes orderType)Sets multiple ORDER BY clauses for the DQL statement. The resulting ORDER BY statement will coalesce the passed columns.- Parameters:
functions- The columns to order by in a coalescing manner.- Returns:
- This
EntityQueryobject, now with a coalesced ORDER BY clause.
-
limit
Limits the result of the rows returned to a maximum of the passed integer with an offset. For example, the call.limit(10, 5)would return the rows 6-15.- Parameters:
limit- The maximum of rows to be returned.offset- The offset of the limit.- Returns:
- This
EntityQueryobject, now with a LIMIT with an OFFSET.
-
limit
Limits the result of the rows returned to a maximum of the passed integer.- Parameters:
limit- The maximum of rows to be returned.- Returns:
- This
EntityQueryobject, now with a LIMIT.
-
distinct
Adds a DISTINCT modifier to the query.- Returns:
- This
EntityQueryobject, now with a DISTINCT clause.
-
project
public <R> Queryable<R> project(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,R> projection)Selects only a single column from a table. This is meant if you don't want to fetch an entire entity from the database.- Type Parameters:
R- The type of the column you want to retrieve.- Parameters:
projection- The column to project to.- Returns:
- A queryable containing the projection.
-
first
Gets the first record of a result. This method should be used when only one record is expected, i.e. when filtering by a unique identifier such as an id.- Specified by:
firstin interfaceQueryable<E extends BaseEntity>- Returns:
- The first row as an entity wrapped in an
Optionalif there is at least one row. OtherwiseOptional.empty()is returned.
-
toList
Executes the query and returns the result as aList- Specified by:
toListin interfaceQueryable<E extends BaseEntity>- Returns:
- A list of entities representing the result rows.
-
toStream
Executes the query and returns the result as aStream- Specified by:
toStreamin interfaceQueryable<E extends BaseEntity>- Returns:
- A list of entities representing the result rows.
-
toArray
Executes a new query and returns the result as an array.- Specified by:
toArrayin interfaceQueryable<E extends BaseEntity>- Returns:
- An array of entities representing the result rows.
-
toMap
Executes a new query and returns the result as aMap.- Specified by:
toMapin interfaceQueryable<E extends BaseEntity>- Type Parameters:
K- The type of the field representing the keys.V- The type of the field representing the values.- Parameters:
keyMapping- The field representing the keys of the map.valueMapping- The field representing the values of the map.- Returns:
- A map containing the result of the query.
-
toSet
Executes the query and returns the result as aSet.- Specified by:
toSetin interfaceQueryable<E extends BaseEntity>- Returns:
- A set of entities representing the result rows.
-
getQuery
Description copied from interface:QueryableResponsible for building and returning the individual DQL statement.- Specified by:
getQueryin interfaceQueryable<E extends BaseEntity>- Returns:
- The DQL statement which fetches data from the database.
-
getTableName
Gets the table name which this query targets.- Returns:
- The table name which this query targets.
-