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> aliases  
    protected Mappable<E> mapper  
    protected static TableModule tableModule  
    protected java.lang.Class<E> type  
  • Constructor Summary

    Constructors 
    Constructor Description
    SingleEntityQuery​(java.lang.Class<E> type)  
  • Method Summary

    Modifier and Type Method Description
    protected void buildWhereClause​(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.String getQuery()
    Builds the query from the set query options.
    java.lang.String getTableName()
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 EntityQuery object, 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 EntityQuery object, 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:
      first in interface SingleQueryable<E extends BaseEntity>
      Returns:
      The first row as an entity wrapped in an Optional if there is at least one row. Otherwise Optional.empty() is returned.
    • getQuery

      public java.lang.String getQuery()
      Builds the query from the set query options.
      Specified by:
      getQuery in interface SingleQueryable<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 - The StringBuilder to 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.