Class DatabaseSelect<T>


  • public class DatabaseSelect<T>
    extends java.lang.Object
    A class used to setup and execute a database select
    Author:
    Xavier Witdouck
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  DatabaseSelect.Handler<T>
      An interface to a component that can generate an Object from the contents of a ResultSet
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.List<java.lang.Object> args  
      private Database db  
      private com.d3x.core.util.Option<java.lang.Integer> fetchSize  
      private com.d3x.core.util.Option<java.lang.Integer> limit  
      private com.d3x.core.util.Option<java.lang.String> sql  
      private com.d3x.core.util.Option<java.time.Duration> timeout  
      private DatabaseTiming timing  
      private java.lang.Class<T> type  
    • Constructor Summary

      Constructors 
      Constructor Description
      DatabaseSelect​(Database db, java.lang.Class<T> type)
      Constructor
    • Field Detail

      • type

        private java.lang.Class<T> type
      • args

        private java.util.List<java.lang.Object> args
      • sql

        private com.d3x.core.util.Option<java.lang.String> sql
      • limit

        private com.d3x.core.util.Option<java.lang.Integer> limit
      • timeout

        private com.d3x.core.util.Option<java.time.Duration> timeout
      • fetchSize

        private com.d3x.core.util.Option<java.lang.Integer> fetchSize
    • Constructor Detail

      • DatabaseSelect

        DatabaseSelect​(Database db,
                       java.lang.Class<T> type)
        Constructor
        Parameters:
        db - the database reference
        type - the data type for this operation
    • Method Detail

      • sql

        public DatabaseSelect<T> sql​(java.lang.String sql)
        Binds a custom SQL statement to this select
        Parameters:
        sql - the SQL statement or classpath resource to SQL statement
        Returns:
        this select object
      • args

        public DatabaseSelect<T> args​(java.lang.Object... args)
        Binds arguments to this select
        Parameters:
        args - the arguments for SQL string
        Returns:
        this select object
      • args

        public DatabaseSelect<T> args​(java.util.Collection<?> args)
        Binds arguments to this select
        Parameters:
        args - the arguments for SQL string
        Returns:
        this select object
      • timeout

        public DatabaseSelect<T> timeout​(java.time.Duration duration)
        Sets the query timeout for this select
        Parameters:
        duration - the timeout duration
        Returns:
        this select object
      • fetchSize

        public DatabaseSelect<T> fetchSize​(int fetchSize)
        Sets the statement fetch size for this operation
        Parameters:
        fetchSize - the fetch size
        Returns:
        this select object
        See Also:
        ResultSet.setFetchSize(int)
      • limit

        public DatabaseSelect<T> limit​(int limit)
        Sets the max number of records to include
        Parameters:
        limit - the max number of records, must be > 0
        Returns:
        this select object
      • mapper

        private DatabaseMapping.Mapper<T> mapper()
        Returns the default mapper for this select
        Returns:
        the default mapper for this
      • first

        public com.d3x.core.util.Option<T> first()
        Returns the first object matched by this select
        Returns:
        the optional first object
      • set

        public java.util.Set<T> set()
        Returns the set of objects that match this select
        Returns:
        the set of resulting objects
      • list

        public java.util.List<T> list()
        Returns the list of objects that match this select
        Returns:
        the list of resulting objects
      • iterator

        public DatabaseIterator<T> iterator()
        Returns a iterator of objects that match this select
        Returns:
        the iterator of resulting objects
      • getTiming

        public DatabaseTiming getTiming()
        Returns the timing instance for this operation
        Returns:
        the timing instance
      • resolveSql

        private java.lang.String resolveSql()
        Returns the SQL expression for this select
        Returns:
        the SQL expression for select
      • first

        public com.d3x.core.util.Option<T> first​(DatabaseMapping.Mapper<T> mapper)
        Returns the first object matched by this select
        Parameters:
        mapper - the mapper used to generate objects from ResultSet
        Returns:
        the optional first object
      • count

        public int count()
        Returns the record count that match this select
        Returns:
        the record count
      • forEach

        public void forEach​(java.util.function.Consumer<T> consumer)
        Iterates over all records that match this query applying each record to the consumer
        Parameters:
        consumer - the consumer to accept each record that matches this query
      • list

        public java.util.List<T> list​(DatabaseMapping.Mapper<T> mapper)
        Returns the list of objects that match this select
        Parameters:
        mapper - the mapper to generate objects from ResultSet
        Returns:
        the list of resulting objects
      • iterator

        public DatabaseIterator<T> iterator​(DatabaseMapping.Mapper<T> mapper)
        Returns a iterator over the objects that match this select
        Parameters:
        mapper - the mapper to generate objects from ResultSet
        Returns:
        the iterator of resulting objects
      • apply

        public T apply​(DatabaseSelect.Handler<T> handler)
        Returns an object generated from a ResultSet according to handler
        Parameters:
        handler - the handler that extracts data from result set to create an object
        Returns:
        the object generated from ResultSet
      • iterator

        private DatabaseIterator<T> iterator​(java.sql.ResultSet rs,
                                             int limit,
                                             DatabaseMapping.Mapper<T> mapper,
                                             java.lang.AutoCloseable... closeables)
        Returns a DatabaseIterator over a database ResultSet
        Parameters:
        rs - the result set to iterate over
        limit - the max number of records to include
        mapper - the mapper to map objects
        Returns:
        the newly created iterator