Class SingleEntityQuery<E extends BaseEntity>
- java.lang.Object
-
- com.github.collinalpert.java2db.queries.SingleEntityQuery<E>
-
- All Implemented Interfaces:
SingleQueryable<E>
- Direct Known Subclasses:
AsyncSingleEntityQuery,EntityQuery
public class SingleEntityQuery<E extends BaseEntity> extends java.lang.Object implements SingleQueryable<E>
- Author:
- Collin Alpert
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Map<java.lang.String,java.lang.String>aliasesprotected Mappable<E>mapperprotected static TableModuletableModuleprotected java.lang.Class<E>type
-
Constructor Summary
Constructors Constructor Description SingleEntityQuery(java.lang.Class<E> type)
-
Method Summary
Modifier and Type Method Description protected voidbuildWhereClause(java.lang.StringBuilder builder, java.lang.String tableName)Builds the WHERE clause in a select statement.java.util.Optional<E>first()Gets the first record of a result.java.lang.StringgetQuery()Builds the query from the set query options.java.lang.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> SingleQueryable<R>project(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,R> projection)Selects only a single column from a table.SingleEntityQuery<E>where(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate)Sets or appends a WHERE clause for the DQL statement.
-
-
-
Field Detail
-
tableModule
protected static final TableModule tableModule
-
type
protected final java.lang.Class<E extends BaseEntity> type
-
mapper
protected final Mappable<E extends BaseEntity> mapper
-
aliases
protected java.util.Map<java.lang.String,java.lang.String> aliases
-
-
Constructor Detail
-
SingleEntityQuery
public SingleEntityQuery(java.lang.Class<E> type)
-
-
Method Detail
-
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> SingleQueryable<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
public java.util.Optional<E> 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 interfaceSingleQueryable<E extends BaseEntity>- Returns:
- The first row as an entity wrapped in an
Optionalif there is at least one row. OtherwiseOptional.empty()is returned.
-
getQuery
public java.lang.String getQuery()
Builds the query from the set query options.- Specified by:
getQueryin interfaceSingleQueryable<E extends BaseEntity>- Returns:
- The DQL statement for getting data from the database.
-
buildWhereClause
protected void buildWhereClause(java.lang.StringBuilder builder, java.lang.String tableName)Builds the WHERE clause in a select statement.- Parameters:
builder- TheStringBuilderto append the clause to.tableName- The name of the table the where clause will affect.
-
getTableName
public java.lang.String getTableName()
Gets the table name which this query targets.- Returns:
- The table name which this query targets.
-
-