Class Query<T extends BaseEntity>


  • public class Query<T extends BaseEntity>
    extends java.lang.Object
    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
      Query​(java.lang.Class<T> type, IMapper<T> mapper)
      Constructor for creating a DQL statement for a given entity.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.Optional<T> getFirst()
      Gets the first record of a result.
      java.util.concurrent.CompletableFuture<java.util.Optional<T>> getFirstAsync()
      The asynchronous version of the getFirst() method.
      java.util.concurrent.CompletableFuture<java.lang.Void> getFirstAsync​(java.util.function.Consumer<? super java.util.Optional<T>> callback)
      The asynchronous version of the getFirst() method.
      java.lang.String getQuery()  
      Query<T> limit​(int limit)
      Limits the result of the rows returned to a maximum of the passed integer.
      Query<T> limit​(int limit, int offset)
      Limits the result of the rows returned to a maximum of the passed integer with an offset.
      Query<T> orderBy​(OrderTypes type, com.github.collinalpert.lambda2sql.functions.SqlFunction<T,​?>... functions)
      Sets multiple ORDER BY clauses for the DQL statement.
      Query<T> orderBy​(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,​?>... functions)
      Sets multiple ORDER BY clauses for the DQL statement.
      Query<T> orWhere​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)
      Sets or appends an OR WHERE clause to the DQL statement.
      T[] toArray()
      Executes a new query and returns the result as an array.
      java.util.concurrent.CompletableFuture<T[]> toArrayAsync()
      The asynchronous version of the toArray() method.
      java.util.concurrent.CompletableFuture<java.lang.Void> toArrayAsync​(java.util.function.Consumer<? super T[]> callback)
      The asynchronous version of the toArray() method.
      java.util.List<T> toList()
      Executes the query and returns the result as a List
      java.util.concurrent.CompletableFuture<java.util.List<T>> toListAsync()
      The asynchronous version of the toList() method.
      java.util.concurrent.CompletableFuture<java.lang.Void> toListAsync​(java.util.function.Consumer<? super java.util.List<T>> callback)
      The asynchronous version of the toList() method.
      java.util.stream.Stream<T> toStream()
      Executes the query and returns the result as a Stream
      java.util.concurrent.CompletableFuture<java.util.stream.Stream<T>> toStreamAsync()
      The asynchronous version of the toStream() method.
      java.util.concurrent.CompletableFuture<java.lang.Void> toStreamAsync​(java.util.function.Consumer<? super java.util.stream.Stream<T>> callback)
      The asynchronous version of the toStream() method.
      java.lang.String toString()  
      Query<T> where​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> 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, wait, wait, wait
    • Constructor Detail

      • Query

        public Query​(java.lang.Class<T> type,
                     IMapper<T> 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

      • getFirstAsync

        public java.util.concurrent.CompletableFuture<java.util.Optional<T>> getFirstAsync()
        The asynchronous version of the getFirst() method.
        Returns:
        The asynchronous operation which will retrieve the data from the database. Custom handling for the CompletableFuture can be done here.
        See Also:
        getFirst()
      • getFirstAsync

        public java.util.concurrent.CompletableFuture<java.lang.Void> getFirstAsync​(java.util.function.Consumer<? super java.util.Optional<T>> callback)
        The asynchronous version of the getFirst() method.
        Parameters:
        callback - The action to be applied to the result once it is fetched from the database.
        Returns:
        The asynchronous operation which will retrieve the data from the database and apply the given action to the result.
        See Also:
        getFirst()
      • toListAsync

        public java.util.concurrent.CompletableFuture<java.util.List<T>> toListAsync()
        The asynchronous version of the toList() method.
        Returns:
        The asynchronous operation which will retrieve the data from the database. Custom handling for the CompletableFuture can be done here.
        See Also:
        toList()
      • toListAsync

        public java.util.concurrent.CompletableFuture<java.lang.Void> toListAsync​(java.util.function.Consumer<? super java.util.List<T>> callback)
        The asynchronous version of the toList() method.
        Parameters:
        callback - The action to be applied to the result once it is fetched from the database.
        Returns:
        The asynchronous operation which will retrieve the data from the database and apply the given action to the result.
        See Also:
        toList()
      • toStreamAsync

        public java.util.concurrent.CompletableFuture<java.util.stream.Stream<T>> toStreamAsync()
        The asynchronous version of the toStream() method.
        Returns:
        The asynchronous operation which will retrieve the data from the database. Custom handling for the CompletableFuture can be done here.
        See Also:
        toStream()
      • toStreamAsync

        public java.util.concurrent.CompletableFuture<java.lang.Void> toStreamAsync​(java.util.function.Consumer<? super java.util.stream.Stream<T>> callback)
        The asynchronous version of the toStream() method.
        Parameters:
        callback - The action to be applied to the result once it is fetched from the database.
        Returns:
        The asynchronous operation which will retrieve the data from the database and apply the given action to the result.
        See Also:
        toStream()
      • toArrayAsync

        public java.util.concurrent.CompletableFuture<T[]> toArrayAsync()
        The asynchronous version of the toArray() method.
        Returns:
        The asynchronous operation which will retrieve the data from the database. Custom handling for the CompletableFuture can be done here.
        See Also:
        toArray()
      • toArrayAsync

        public java.util.concurrent.CompletableFuture<java.lang.Void> toArrayAsync​(java.util.function.Consumer<? super T[]> callback)
        The asynchronous version of the toArray() method.
        Parameters:
        callback - The action to be applied to the result once it is fetched from the database.
        Returns:
        The asynchronous operation which will retrieve the data from the database and apply the given action to the result.
        See Also:
        toArray()
      • getFirst

        public java.util.Optional<T> 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.
        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<T> toList()
        Executes the query and returns the result as a List
        Returns:
        A list of entities representing the result rows.
      • toStream

        public java.util.stream.Stream<T> toStream()
        Executes the query and returns the result as a Stream
        Returns:
        A list of entities representing the result rows.
      • toArray

        public T[] toArray()
        Executes a new query and returns the result as an array.
        Returns:
        An array of entities representing the result rows.
      • where

        public Query<T> where​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)
        Sets or appends a WHERE clause for the DQL statement.
        Parameters:
        predicate - The predicate describing the WHERE clause.
        Returns:
        This Query object, now with an (appended) WHERE clause.
      • orWhere

        public Query<T> orWhere​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)
        Sets or appends an OR WHERE clause to the DQL statement.
        Parameters:
        predicate - The predicate describing the OR WHERE clause.
        Returns:
        This Query object, now with an (appended) OR WHERE clause.
      • orderBy

        @SafeVarargs
        public final Query<T> orderBy​(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,​?>... 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 Query object, now with a coalesced ORDER BY clause.
      • orderBy

        @SafeVarargs
        public final Query<T> orderBy​(OrderTypes type,
                                      com.github.collinalpert.lambda2sql.functions.SqlFunction<T,​?>... 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 Query object, now with a coalesced ORDER BY clause.
      • limit

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

        public Query<T> 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 Query object, now with a LIMIT.
      • getQuery

        public java.lang.String getQuery()
        Returns:
        the query as a String
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object