Class Query<T extends BaseEntity>
- java.lang.Object
-
- com.github.collinalpert.java2db.queries.Query<T>
-
public class Query<T extends BaseEntity> extends java.lang.ObjectA 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 theForeignKeyEntityattribute) can be filled.- Author:
- Collin Alpert
-
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.util.Optional<T>getFirst()Gets the first row of a query.java.lang.StringgetQuery()Query<T>limit(int limit)Limits the result of the rows returned to a maximum of the passed integer.Query<T>limit(int limit, int offset)Limits the result of the rows returned to a maximum of the passed integer with an offset.Query<T>orderBy(OrderTypes type, com.github.collinalpert.lambda2sql.functions.SqlFunction<T,?>... functions)Sets multiple ORDER BY clauses for the DQL statement.Query<T>orderBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,?> function)Deprecated, for removal: This API element is subject to removal in a future version.Since the coalescing feature for ORDER BY statements was introduced, there is no need for the single-parameter methods.Query<T>orderBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,?>... functions)Sets multiple ORDER BY clauses for the DQL statement.Query<T>orderBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,?> function, OrderTypes type)Deprecated, for removal: This API element is subject to removal in a future version.Since the coalescing feature for ORDER BY statements was introduced, there is no need for the single-parameter methods.Query<T>orWhere(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)Sets or appends an OR WHERE clause to the DQL statement.T[]toArray()Executes a new query and returns the result as an array.java.util.List<T>toList()Executes the query and returns the result as aListjava.util.stream.Stream<T>toStream()Executes the query and returns the result as aStreamjava.lang.StringtoString()Query<T>where(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)Sets or appends a WHERE clause for the DQL statement.
-
-
-
Constructor Detail
-
Query
public Query(java.lang.Class<T> type, Mapper<T> mapper)
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.mapper- The mapper for mapping entities.
-
-
Method Detail
-
getFirst
public java.util.Optional<T> getFirst()
Gets the first row of a query.- Returns:
- The first row as an entity wrapped in an
Optionalif there is at least one row. OtherwiseOptional.empty()is returned.
-
toList
public java.util.List<T> toList()
Executes the query and returns the result as aList- Returns:
- A list of entities representing the result rows.
-
toStream
public java.util.stream.Stream<T> toStream()
Executes the query and returns the result as aStream- Returns:
- A list of entities representing the result rows.
-
toArray
public T[] toArray()
Executes a new query and returns the result as an array.- Returns:
- An array of entities representing the result rows.
-
where
public Query<T> where(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)
Sets or appends a WHERE clause for the DQL statement.- Parameters:
predicate- The predicate describing the WHERE clause.- Returns:
- This
Queryobject, now with an (appended) WHERE clause.
-
orWhere
public Query<T> orWhere(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)
Sets or appends an OR WHERE clause to the DQL statement.- Parameters:
predicate- The predicate describing the OR WHERE clause.- Returns:
- This
Queryobject, now with an (appended) OR WHERE clause.
-
orderBy
@Deprecated(since="3.1.2", forRemoval=true) public Query<T> orderBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,?> function)Deprecated, for removal: This API element is subject to removal in a future version.Since the coalescing feature for ORDER BY statements was introduced, there is no need for the single-parameter methods. As removing them would be a breaking change, these methods will be removed as part of release 4.0.Sets an ORDER BY clause for the DQL statement. The order will be ascending.- Parameters:
function- The property to order by.- Returns:
- This
Queryobject, now with an ORDER BY clause.
-
orderBy
@Deprecated(since="3.1.2", forRemoval=true) public Query<T> orderBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,?> function, OrderTypes type)Deprecated, for removal: This API element is subject to removal in a future version.Since the coalescing feature for ORDER BY statements was introduced, there is no need for the single-parameter methods. As removing them would be a breaking change, these methods will be removed as part of release 4.0.Sets an ORDER BY clause for the DQL statement.- Parameters:
function- The property to order by.type- The type of ordering that should be applied.- Returns:
- This
Queryobject, now with an ORDER BY clause.
-
orderBy
@SafeVarargs public final Query<T> orderBy(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,?>... 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
Queryobject, now with a coalesced ORDER BY clause.
-
orderBy
@SafeVarargs public final Query<T> orderBy(OrderTypes type, com.github.collinalpert.lambda2sql.functions.SqlFunction<T,?>... functions)
Sets multiple ORDER BY clauses for the DQL statement. The resulting ORDER BY statement will coalesce the passed columns.- Parameters:
type- The type of ordering that should be applied.functions- The columns to order by in a coalescing manner.- Returns:
- This
Queryobject, now with a coalesced ORDER BY clause.
-
limit
public Query<T> limit(int limit, int offset)
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
Queryobject, now with a LIMIT with an OFFSET.
-
limit
public Query<T> limit(int 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
Queryobject, now with a LIMIT.
-
getQuery
public java.lang.String getQuery()
- Returns:
- the query as a
String
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-