Class EntityQuery<E extends BaseEntity>

  • All Implemented Interfaces:
    Queryable<E>

    public class EntityQuery<E extends BaseEntity>
    extends java.lang.Object
    implements Queryable<E>
    A 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 the ForeignKeyEntity attribute) can be filled.
    Author:
    Collin Alpert
    • Constructor Summary

      Constructors 
      Constructor Description
      EntityQuery​(java.lang.Class<E> type, Mappable<E> mapper)
      Constructor for creating a DQL statement for a given entity.
    • Method Summary

      Modifier and Type Method Description
      java.lang.String generateQueryClauses​(java.lang.String tableName)
      Creates the query clauses for a DQL statement.
      java.util.Optional<E> getFirst()
      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.
      EntityQuery<E> limit​(int limit)
      Limits the result of the rows returned to a maximum of the passed integer.
      EntityQuery<E> limit​(int limit, int offset)
      Limits the result of the rows returned to a maximum of the passed integer with an offset.
      EntityQuery<E> orderBy​(OrderTypes type, com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​?>... functions)
      Sets multiple ORDER BY clauses for the DQL statement.
      EntityQuery<E> orderBy​(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​?>... functions)
      Sets multiple ORDER BY clauses for the DQL statement.
      EntityQuery<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.
      java.util.List<E> toList()
      Executes the query and returns the result as a List
      java.util.stream.Stream<E> toStream()
      Executes the query and returns the result as a Stream
      EntityQuery<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
    • Constructor Detail

      • EntityQuery

        public EntityQuery​(java.lang.Class<E> type,
                           Mappable<E> 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 the BaseService.
        Parameters:
        type - The entity to query.
        mapper - The mapper for mapping entities.
    • Method Detail

      • where

        public EntityQuery<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 EntityQuery<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.
      • orderBy

        @SafeVarargs
        public final EntityQuery<E> orderBy​(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​?>... functions)
        Sets multiple ORDER BY clauses for the DQL statement. The resulting ORDER BY statement will coalesce the passed columns, if more than one is supplied.
        Parameters:
        functions - The columns to order by in a coalescing manner.
        Returns:
        This EntityQuery object, now with a coalesced ORDER BY clause.
      • orderBy

        @SafeVarargs
        public final EntityQuery<E> orderBy​(OrderTypes type,
                                            com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​?>... functions)
        Sets multiple ORDER BY clauses for the DQL statement. The resulting ORDER BY statement will coalesce the passed columns, if more than one is supplied.
        Parameters:
        type - The type of ordering that should be applied.
        functions - The columns to order by in a coalescing manner.
        Returns:
        This EntityQuery object, now with a coalesced ORDER BY clause.
      • limit

        public EntityQuery<E> 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 EntityQuery object, now with a LIMIT with an OFFSET.
      • limit

        public EntityQuery<E> 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 EntityQuery object, now with a LIMIT.
      • 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.
      • getFirst

        public java.util.Optional<E> getFirst()
        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:
        getFirst 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 java.util.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 java.util.stream.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.
      • getQuery

        public java.lang.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.
      • generateQueryClauses

        public java.lang.String generateQueryClauses​(java.lang.String tableName)
        Creates the query clauses for a DQL statement. This contains constraints like a WHERE, an ORDER BY and a LIMIT statement.
        Parameters:
        tableName - The table name which is targeted.
        Returns:
        A string containing the clauses which can then be appended to the end of a DQL statement.
      • getTableName

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