Class DefaultRelationalQuery
- java.lang.Object
-
- io.ebeaninternal.server.querydefn.DefaultRelationalQuery
-
- All Implemented Interfaces:
SqlQuery
,SpiSqlBinding
,SpiSqlQuery
,Serializable
public class DefaultRelationalQuery extends Object implements SpiSqlQuery
Default implementation of SQuery - SQL Query.- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.ebean.SqlQuery
SqlQuery.TypeQuery<T>
-
-
Constructor Summary
Constructors Constructor Description DefaultRelationalQuery(SpiEbeanServer server, String query)
Additional supply a query detail object.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
findEach(Consumer<SqlRow> consumer)
Execute the SqlQuery iterating a row at a time.void
findEachRow(RowConsumer consumer)
Execute the query reading each row from ResultSet using the RowConsumer.void
findEachWhile(Predicate<SqlRow> consumer)
Execute the SqlQuery iterating a row at a time with the ability to stop consuming part way through.List<SqlRow>
findList()
Execute the query returning a list.<T> List<T>
findList(RowMapper<T> mapper)
Deprecated migrate to useSqlQuery.mapTo(RowMapper)
SqlRow
findOne()
Execute the query returning a single row or null.<T> T
findOne(RowMapper<T> mapper)
Deprecated migrate to useSqlQuery.mapTo(RowMapper)
Optional<SqlRow>
findOneOrEmpty()
Execute the query returning an optional row.<T> T
findSingleAttribute(Class<T> cls)
Deprecated - migrate to.mapToScalar(attributeType).findOne()
.<T> List<T>
findSingleAttributeList(Class<T> cls)
Deprecated - migrate to.mapToScalar(Long.class).findList()
.BigDecimal
findSingleDecimal()
Deprecated - migrate to.mapToScalar(BigDecimal.class).findOne()
.Long
findSingleLong()
Deprecated - migrate to.mapToScalar(Long.class).findOne()
.BindParams
getBindParams()
Return the named or positioned parameters.int
getBufferFetchSizeHint()
Return the hint for Statement.setFetchSize().int
getFirstRow()
Return the first row to fetch.String
getLabel()
Return the label (to collect metrics on when set).int
getMaxRows()
Return the maximum number of rows to fetch.String
getQuery()
return the query.int
getTimeout()
Return the query timeout.<T> SqlQuery.TypeQuery<T>
mapTo(RowMapper<T> mapper)
Use a RowMapper to map the result to beans.<T> SqlQuery.TypeQuery<T>
mapToScalar(Class<T> attributeType)
The query result maps to a single scalar value like Long, BigDecimal, String, UUID, OffsetDateTime etc.DefaultRelationalQuery
setBufferFetchSizeHint(int bufferFetchSizeHint)
A hint which for JDBC translates to the Statement.fetchSize().DefaultRelationalQuery
setFirstRow(int firstRow)
Set the index of the first row of the results to return.DefaultRelationalQuery
setLabel(String label)
Set a label that can be put on performance metrics that are collected.DefaultRelationalQuery
setMaxRows(int maxRows)
Set the maximum number of query results to return.DefaultRelationalQuery
setParameter(int position, Object value)
Bind the parameter by its index position (1 based like JDBC).DefaultRelationalQuery
setParameter(Object value)
Set the next bind parameter by position.DefaultRelationalQuery
setParameter(String paramName, Object value)
Bind the named parameter value.DefaultRelationalQuery
setParameters(Object... values)
Set one of more positioned parameters.DefaultRelationalQuery
setParams(Object... values)
Deprecated.DefaultRelationalQuery
setTimeout(int secs)
Set a timeout on this query.String
toString()
-
-
-
Constructor Detail
-
DefaultRelationalQuery
public DefaultRelationalQuery(SpiEbeanServer server, String query)
Additional supply a query detail object.
-
-
Method Detail
-
findEach
public void findEach(Consumer<SqlRow> consumer)
Description copied from interface:SqlQuery
Execute the SqlQuery 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.
-
findEachWhile
public void findEachWhile(Predicate<SqlRow> consumer)
Description copied from interface:SqlQuery
Execute the SqlQuery 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.
- Specified by:
findEachWhile
in interfaceSqlQuery
-
findList
public List<SqlRow> findList()
Description copied from interface:SqlQuery
Execute the query returning a list.
-
findSingleDecimal
public BigDecimal findSingleDecimal()
Description copied from interface:SqlQuery
Deprecated - migrate to.mapToScalar(BigDecimal.class).findOne()
..mapToScalar(BigDecimal.class) .findOne();
- Specified by:
findSingleDecimal
in interfaceSqlQuery
-
findSingleLong
public Long findSingleLong()
Description copied from interface:SqlQuery
Deprecated - migrate to.mapToScalar(Long.class).findOne()
..mapToScalar(Long.class) .findOne();
- Specified by:
findSingleLong
in interfaceSqlQuery
-
findSingleAttribute
public <T> T findSingleAttribute(Class<T> cls)
Description copied from interface:SqlQuery
Deprecated - migrate to.mapToScalar(attributeType).findOne()
..mapToScalar(BigDecimal.class) .findOne();
- Specified by:
findSingleAttribute
in interfaceSqlQuery
-
findSingleAttributeList
public <T> List<T> findSingleAttributeList(Class<T> cls)
Description copied from interface:SqlQuery
Deprecated - migrate to.mapToScalar(Long.class).findList()
..mapToScalar(Long.class) .findList();
- Specified by:
findSingleAttributeList
in interfaceSqlQuery
-
findOne
public <T> T findOne(RowMapper<T> mapper)
Description copied from interface:SqlQuery
Deprecated migrate to useSqlQuery.mapTo(RowMapper)
-
findList
public <T> List<T> findList(RowMapper<T> mapper)
Description copied from interface:SqlQuery
Deprecated migrate to useSqlQuery.mapTo(RowMapper)
-
findEachRow
public void findEachRow(RowConsumer consumer)
Description copied from interface:SqlQuery
Execute the query reading each row from ResultSet using the RowConsumer.This provides a low level option that reads directly from the JDBC ResultSet and is good for processing very large results where (unlike findList) we don't hold all the results in memory but instead can process row by row.
String sql = "select id, name, status from customer order by name desc"; DB.sqlQuery(sql) .findEachRow((resultSet, rowNum) -> { // read directly from ResultSet long id = resultSet.getLong(1); String name = resultSet.getString(2); // do something interesting with the data });
- Specified by:
findEachRow
in interfaceSqlQuery
- Parameters:
consumer
- Used to read and process each ResultSet row.
-
findOne
public SqlRow findOne()
Description copied from interface:SqlQuery
Execute the query returning a single row or null.If this query finds 2 or more rows then it will throw a PersistenceException.
-
findOneOrEmpty
public Optional<SqlRow> findOneOrEmpty()
Description copied from interface:SqlQuery
Execute the query returning an optional row.- Specified by:
findOneOrEmpty
in interfaceSqlQuery
-
setParams
@Deprecated public DefaultRelationalQuery setParams(Object... values)
Deprecated.Description copied from interface:SqlQuery
Deprecated migrate to setParameters(Object... values)
-
setParameters
public DefaultRelationalQuery setParameters(Object... values)
Description copied from interface:SqlQuery
Set one of more positioned parameters.This is a convenient alternative to multiple calls to
SqlQuery.setParameter(Object)
.String sql = "select id, name from customer where name like ? and status = ?"; List<SqlRow> list = DB.sqlQuery(sql) .setParameters("Rob", Status.NEW) .findList(); // effectively the same as ... .setParameter("Rob") .setParameter("Status.NEW) // and ... .setParameter(1, "Rob") .setParameter(2, "Status.NEW)
- Specified by:
setParameters
in interfaceSqlQuery
-
setParameter
public DefaultRelationalQuery setParameter(Object value)
Description copied from interface:SqlQuery
Set the next bind parameter by position.String sql = "select id, name from customer where name like ? and status = ?"; List<SqlRow> list = DB.sqlQuery(sql) .setParameter("Rob") .setParameter("Status.NEW) .findList(); // the same as ... .setParameters("Rob", Status.NEW) // and ... .setParameter(1, "Rob") .setParameter(2, "Status.NEW)
- Specified by:
setParameter
in interfaceSqlQuery
- Parameters:
value
- The value to bind
-
setParameter
public DefaultRelationalQuery setParameter(int position, Object value)
Description copied from interface:SqlQuery
Bind the parameter by its index position (1 based like JDBC).- Specified by:
setParameter
in interfaceSqlQuery
-
setParameter
public DefaultRelationalQuery setParameter(String paramName, Object value)
Description copied from interface:SqlQuery
Bind the named parameter value.- Specified by:
setParameter
in interfaceSqlQuery
-
getFirstRow
public int getFirstRow()
Description copied from interface:SpiSqlBinding
Return the first row to fetch.- Specified by:
getFirstRow
in interfaceSpiSqlBinding
-
setFirstRow
public DefaultRelationalQuery setFirstRow(int firstRow)
Description copied from interface:SqlQuery
Set the index of the first row of the results to return.- Specified by:
setFirstRow
in interfaceSqlQuery
-
getMaxRows
public int getMaxRows()
Description copied from interface:SpiSqlBinding
Return the maximum number of rows to fetch.- Specified by:
getMaxRows
in interfaceSpiSqlBinding
-
setMaxRows
public DefaultRelationalQuery setMaxRows(int maxRows)
Description copied from interface:SqlQuery
Set the maximum number of query results to return.- Specified by:
setMaxRows
in interfaceSqlQuery
-
getTimeout
public int getTimeout()
Description copied from interface:SpiSqlBinding
Return the query timeout.- Specified by:
getTimeout
in interfaceSpiSqlBinding
-
setTimeout
public DefaultRelationalQuery setTimeout(int secs)
Description copied from interface:SqlQuery
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.
- Specified by:
setTimeout
in interfaceSqlQuery
- Parameters:
secs
- the query timeout limit in seconds. Zero means there is no limit.
-
getLabel
public String getLabel()
Description copied from interface:SpiSqlBinding
Return the label (to collect metrics on when set).- Specified by:
getLabel
in interfaceSpiSqlBinding
-
setLabel
public DefaultRelationalQuery setLabel(String label)
Description copied from interface:SqlQuery
Set a label that can be put on performance metrics that are collected.
-
getBindParams
public BindParams getBindParams()
Description copied from interface:SpiSqlBinding
Return the named or positioned parameters.- Specified by:
getBindParams
in interfaceSpiSqlBinding
-
setBufferFetchSizeHint
public DefaultRelationalQuery setBufferFetchSizeHint(int bufferFetchSizeHint)
Description copied from interface:SqlQuery
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.
- Specified by:
setBufferFetchSizeHint
in interfaceSqlQuery
-
getBufferFetchSizeHint
public int getBufferFetchSizeHint()
Description copied from interface:SpiSqlBinding
Return the hint for Statement.setFetchSize().- Specified by:
getBufferFetchSizeHint
in interfaceSpiSqlBinding
-
getQuery
public String getQuery()
Description copied from interface:SpiSqlBinding
return the query.- Specified by:
getQuery
in interfaceSpiSqlBinding
-
mapToScalar
public <T> SqlQuery.TypeQuery<T> mapToScalar(Class<T> attributeType)
Description copied from interface:SqlQuery
The query result maps to a single scalar value like Long, BigDecimal, String, UUID, OffsetDateTime etc.Any scalar type Ebean is aware of can be used including java time types like Instant, LocalDate, OffsetDateTime, UUID, Inet, Cdir etc.
String sql = " select min(updtime) from o_order_detail " + " where unit_price > ? and updtime is not null "; OffsetDateTime minCreated = DB.sqlQuery(sql) .setParameter(42) .mapToScalar(OffsetDateTime.class) .findOne();
- Specified by:
mapToScalar
in interfaceSqlQuery
- Parameters:
attributeType
- The type the result is returned as- Returns:
- The query to execute via findOne() findList() etc
-
mapTo
public <T> SqlQuery.TypeQuery<T> mapTo(RowMapper<T> mapper)
Description copied from interface:SqlQuery
Use a RowMapper to map the result to beans.
-
-