Interface DtoQuery<T>
-
public interface DtoQuery<T>
Query for performing native SQL queries that return DTO Bean's.These beans are just normal classes. They must have public constructors and setters.
Constructors with arguments are used if the number of constructor arguments matches the number of columns in the resultSet.
If the number of columns in the resultSet is greater than the largest constructor then the largest constructor is used for the first columns and remaining columns are mapped by setter methods.
// CustomerDto is just a 'bean like' class // with public constructor(s) and public setter methods String sql = "select id, name from customer where name like :name and status_code = :status"; List<CustomerDto> beans = DB.findDto(CustomerDto.class, sql) .setParameter("name", "Acme%") .setParameter("status", "ACTIVE") .findList();
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
findEach(int batch, Consumer<List<T>> consumer)
Execute the query iterating the results and batching them for the consumer.void
findEach(Consumer<T> consumer)
Execute the query iterating a row at a time.void
findEachWhile(Predicate<T> consumer)
Execute the query iterating a row at a time with the ability to stop consuming part way through.List<T>
findList()
Execute the query returning a list.T
findOne()
Execute the query returning a single bean.Optional<T>
findOneOrEmpty()
Execute the query returning an optional bean.DtoQuery<T>
setBufferFetchSizeHint(int bufferFetchSizeHint)
A hint which for JDBC translates to the Statement.fetchSize().DtoQuery<T>
setFirstRow(int firstRow)
Set the index of the first row of the results to return.DtoQuery<T>
setLabel(String label)
Set a label on the query to make it easier to identify queries related to query execution statistics.DtoQuery<T>
setMaxRows(int maxRows)
Set the maximum number of query results to return.DtoQuery<T>
setParameter(int position, Object value)
Bind the parameter by its index position (1 based like JDBC).DtoQuery<T>
setParameter(Object value)
Bind the next parameter using index position.DtoQuery<T>
setParameter(String name, Object value)
Bind the named parameter.DtoQuery<T>
setParameters(Object... value)
Bind all the parameters using index positions.DtoQuery<T>
setProfileLocation(ProfileLocation profileLocation)
Set the profile location of this query.DtoQuery<T>
setRelaxedMode()
When resultSet columns are not able to be mapped to a bean property then instead of throwing effectively skip reading that column.DtoQuery<T>
setTimeout(int secs)
Set a timeout on this query.DtoQuery<T>
usingTransaction(Transaction transaction)
Use the explicit transaction to execute the query.
-
-
-
Method Detail
-
findEach
void findEach(Consumer<T> consumer)
Execute the query iterating a row at a time.This streaming type query is useful for large query execution as only 1 row needs to be held in memory.
-
findEach
void findEach(int batch, Consumer<List<T>> consumer)
Execute the query iterating the results and batching them for the consumer.This runs like findEach streaming results from the database but just collects the results into batches to pass to the consumer.
- Parameters:
batch
- The number of dto beans to collect before given them to the consumerconsumer
- The consumer to process the batch of DTO beans
-
findEachWhile
void findEachWhile(Predicate<T> consumer)
Execute the query iterating a row at a time with the ability to stop consuming part way through.Returning false after processing a row stops the iteration through the query results.
This streaming type query is useful for large query execution as only 1 row needs to be held in memory.
-
findOneOrEmpty
@Nonnull Optional<T> findOneOrEmpty()
Execute the query returning an optional bean.
-
setParameters
DtoQuery<T> setParameters(Object... value)
Bind all the parameters using index positions.Binds each parameter moving the index position each time.
A convenience for multiple calls to
setParameter(Object)
-
setParameter
DtoQuery<T> setParameter(Object value)
Bind the next parameter using index position.Bind the parameter using index position starting at 1 and incrementing.
-
setParameter
DtoQuery<T> setParameter(String name, Object value)
Bind the named parameter.
-
setParameter
DtoQuery<T> setParameter(int position, Object value)
Bind the parameter by its index position (1 based like JDBC).
-
setFirstRow
DtoQuery<T> setFirstRow(int firstRow)
Set the index of the first row of the results to return.
-
setMaxRows
DtoQuery<T> setMaxRows(int maxRows)
Set the maximum number of query results to return.
-
setRelaxedMode
DtoQuery<T> setRelaxedMode()
When resultSet columns are not able to be mapped to a bean property then instead of throwing effectively skip reading that column.
-
setLabel
DtoQuery<T> setLabel(String label)
Set a label on the query to make it easier to identify queries related to query execution statistics.- Parameters:
label
- A label that is unique to the DTO bean type.
-
setProfileLocation
DtoQuery<T> setProfileLocation(ProfileLocation profileLocation)
Set the profile location of this query. This is used to relate query execution metrics back to a location like a specific line of code.
-
setTimeout
DtoQuery<T> setTimeout(int secs)
Set a timeout on this query.This will typically result in a call to setQueryTimeout() on a preparedStatement. If the timeout occurs an exception will be thrown - this will be a SQLException wrapped up in a PersistenceException.
- Parameters:
secs
- the query timeout limit in seconds. Zero means there is no limit.
-
setBufferFetchSizeHint
DtoQuery<T> setBufferFetchSizeHint(int bufferFetchSizeHint)
A hint which for JDBC translates to the Statement.fetchSize().Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed for ResultSet.
-
usingTransaction
DtoQuery<T> usingTransaction(Transaction transaction)
Use the explicit transaction to execute the query.
-
-