Class SingleEntityQuery<E extends BaseEntity>
java.lang.Object
com.github.collinalpert.java2db.queries.SingleEntityQuery<E>
- All Implemented Interfaces:
Queryable<E>
- Direct Known Subclasses:
AsyncSingleEntityQuery
public class SingleEntityQuery<E extends BaseEntity> extends Object implements Queryable<E>
- Author:
- Collin Alpert
-
Field Summary
Fields Modifier and Type Field Description protected ConnectionConfigurationconnectionConfigurationprotected IQueryBuilder<E>queryBuilderprotected QueryParameters<E>queryParameters -
Constructor Summary
Constructors Constructor Description SingleEntityQuery(Class<E> type, ConnectionConfiguration connectionConfiguration) -
Method Summary
Modifier and Type Method Description Optional<E>first()Gets the first record of a result.StringgetQuery()Builds the query from the set query options.protected StringgetTableName()Gets the table name which this query targets.SingleEntityQuery<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.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 aStream.SingleEntityQuery<E>where(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate)Sets or appends a WHERE clause for the DQL statement.
-
Field Details
-
Constructor Details
-
Method Details
-
where
public SingleEntityQuery<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 SingleEntityQuery<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.
-
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
Builds the query from the set query options.- Specified by:
getQueryin interfaceQueryable<E extends BaseEntity>- Returns:
- The DQL statement for getting data from the database.
-
getTableName
Gets the table name which this query targets.- Returns:
- The table name which this query targets.
-