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 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> 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

      public 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 Queryable<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.
    • toList

      public List<E> toList()
      Executes the query and returns the result as a List.
      Specified by:
      toList in interface Queryable<E extends BaseEntity>
      Returns:
      A list of entities representing the result rows.
    • toStream

      public Stream<E> toStream()
      Executes the query and returns the result as a Stream.
      Specified by:
      toStream in interface Queryable<E extends BaseEntity>
      Returns:
      A list of entities representing the result rows.
    • toArray

      public E[] toArray()
      Executes a new query and returns the result as an array.
      Specified by:
      toArray in interface Queryable<E extends BaseEntity>
      Returns:
      An array of entities representing the result rows.
    • toMap

      public <K,​ V> Map<K,​V> toMap​(Function<E,​K> keyMapping, Function<E,​V> valueMapping)
      Executes a new query and returns the result as a Map.
      Specified by:
      toMap in interface Queryable<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

      public Set<E> toSet()
      Executes the query and returns the result as a Set.
      Specified by:
      toSet in interface Queryable<E extends BaseEntity>
      Returns:
      A set of entities representing the result rows.
    • getQuery

      public String getQuery()
      Builds the query from the set query options.
      Specified by:
      getQuery in interface Queryable<E extends BaseEntity>
      Returns:
      The DQL statement for getting data from the database.
    • getTableName

      protected String getTableName()
      Gets the table name which this query targets.
      Returns:
      The table name which this query targets.