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.
    Author:
    Collin Alpert
    • Constructor Summary

      Constructors 
      Constructor Description
      Query​(Query<T> subSelect, BaseMapper<T> mapper)
      Constructor for creating a DQL statement which includes a sub select.
      Query​(java.lang.String tableName, BaseMapper<T> mapper)
      Constructor for creating a basic DQL statement for a given table name.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Query<T> as​(java.lang.String alias)
      Applies an alias to the result.
      java.util.List<T> get()
      Gets the values returned from the query.
      java.util.Optional<T> getFirst()
      Gets the first row of a query.
      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> orderBy​(SqlFunction<T,?> function)
      Adds an ORDER BY clause to the DQL statement.
      Query<T> orderBy​(SqlFunction<T,?> function, OrderTypes type)
      Adds an ORDER BY clause to the DQL statement.
      java.lang.String toString()  
      Query<T> where​(SqlPredicate<T> predicate)
      Adds a WHERE clause to 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.String tableName,
                     BaseMapper<T> mapper)
        Constructor for creating a basic DQL statement for a given table name. This constructor should not be used directly, but through the BaseService.selectQuery() method which every service can use due to inheritance.
        Parameters:
        tableName - The table to query.
        mapper - The mapper for mapping entities.
      • Query

        public Query​(Query<T> subSelect,
                     BaseMapper<T> mapper)
        Constructor for creating a DQL statement which includes a sub select. This constructor should not be used directly, but through the BaseService.subSelectQuery(Query) method which every service class can use due to inheritance.
        Parameters:
        subSelect - The sub select object.
        mapper - The mapper for mapping entities.
    • Method Detail

      • getFirst

        public java.util.Optional<T> getFirst()
        Gets the first row of a query.
        Returns:
        The first row as an entity wrapped in an Optional if there is at least one row. Otherwise Optional.empty() is returned.
      • get

        public java.util.List<T> get()
        Gets the values returned from the query.
        Returns:
        A list of entities representing the result rows.
      • as

        public Query<T> as​(java.lang.String alias)
        Applies an alias to the result.
        Parameters:
        alias - The name of the alias.
        Returns:
        This Query object, now with the added alias.
      • where

        public Query<T> where​(SqlPredicate<T> predicate)
        Adds a WHERE clause to the DQL statement.
        Parameters:
        predicate - The predicate describing the WHERE clause.
        Returns:
        This Query object, now with the added WHERE clause.
      • orderBy

        public Query<T> orderBy​(SqlFunction<T,?> function)
        Adds an ORDER BY clause to the DQL statement. The order will be ascending.
        Parameters:
        function - The value to order by.
        Returns:
        This Query object, now with the added ORDER BY clause.
      • orderBy

        public Query<T> orderBy​(SqlFunction<T,?> function,
                                OrderTypes type)
        Adds an ORDER BY clause to the DQL statement. The order will be ascending.
        Parameters:
        function - The value to order by.
        type - The type of ordering that should be applied.
        Returns:
        This Query object, now with the added ORDER BY clause.
      • 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 the added 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