Package com.d3x.core.db
Class DatabaseSelect<T>
- java.lang.Object
-
- com.d3x.core.db.DatabaseSelect<T>
-
public class DatabaseSelect<T> extends java.lang.ObjectA class used to setup and execute a database select- Author:
- Xavier Witdouck
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceDatabaseSelect.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>argsprivate Databasedbprivate com.d3x.core.util.Option<java.lang.Integer>fetchSizeprivate com.d3x.core.util.Option<java.lang.Integer>limitprivate com.d3x.core.util.Option<java.lang.String>sqlprivate com.d3x.core.util.Option<java.time.Duration>timeoutprivate DatabaseTimingtimingprivate java.lang.Class<T>type
-
Constructor Summary
Constructors Constructor Description DatabaseSelect(Database db, java.lang.Class<T> type)Constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Tapply(DatabaseSelect.Handler<T> handler)Returns an object generated from a ResultSet according to handlerDatabaseSelect<T>args(java.lang.Object... args)Binds arguments to this selectDatabaseSelect<T>args(java.util.Collection<?> args)Binds arguments to this selectintcount()Returns the record count that match this selectDatabaseSelect<T>fetchSize(int fetchSize)Sets the statement fetch size for this operationcom.d3x.core.util.Option<T>first()Returns the first object matched by this selectcom.d3x.core.util.Option<T>first(DatabaseMapping.Mapper<T> mapper)Returns the first object matched by this selectvoidforEach(java.util.function.Consumer<T> consumer)Iterates over all records that match this query applying each record to the consumerDatabaseTiminggetTiming()Returns the timing instance for this operationDatabaseIterator<T>iterator()Returns a iterator of objects that match this selectDatabaseIterator<T>iterator(DatabaseMapping.Mapper<T> mapper)Returns a iterator over the objects that match this selectprivate DatabaseIterator<T>iterator(java.sql.ResultSet rs, int limit, DatabaseMapping.Mapper<T> mapper, java.lang.AutoCloseable... closeables)Returns a DatabaseIterator over a database ResultSetDatabaseSelect<T>limit(int limit)Sets the max number of records to includejava.util.List<T>list()Returns the list of objects that match this selectjava.util.List<T>list(DatabaseMapping.Mapper<T> mapper)Returns the list of objects that match this selectprivate DatabaseMapping.Mapper<T>mapper()Returns the default mapper for this selectprivate java.lang.StringresolveSql()Returns the SQL expression for this selectjava.util.Set<T>set()Returns the set of objects that match this selectDatabaseSelect<T>sql(java.lang.String sql)Binds a custom SQL statement to this selectDatabaseSelect<T>timeout(java.time.Duration duration)Sets the query timeout for this select
-
-
-
Field Detail
-
db
private Database db
-
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
-
timing
private DatabaseTiming timing
-
-
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 overlimit- the max number of records to includemapper- the mapper to map objects- Returns:
- the newly created iterator
-
-