#1956 - Add SqlQuery - mapTo( RowMapper) ... to deprecate findOne(RowMapper)

This commit is contained in:
rob bygrave
2020-02-22 10:50:23 +13:00
parent 92995b807e
commit 3878b02cff
4 changed files with 68 additions and 34 deletions
+3 -2
View File
@@ -18,7 +18,7 @@ import java.sql.SQLException;
* <pre>{@code
*
* //
* // A mapper from ResultSet into our CustomerDto bean
* // Map from ResultSet to CustomerDto bean
* //
* class CustomerMapper implements RowMapper<CustomerDto> {
*
@@ -42,7 +42,8 @@ import java.sql.SQLException;
*
* CustomerDto rob = DB.sqlQuery(sql)
* .setParameter(1, "Rob")
* .findOne(CUSTOMER_MAPPER);
* .mapTo(CUSTOMER_MAPPER)
* .findOne();
*
*
* }</pre>
+20 -27
View File
@@ -75,17 +75,15 @@ public interface SqlQuery extends Serializable {
SqlRow findOne();
/**
* Execute the query returning a single result using the mapper.
*
* @param mapper Used to map each ResultSet row into the result object.
* Deprecated migrate to use {@link #mapTo(RowMapper)}
*/
@Deprecated
<T> T findOne(RowMapper<T> mapper);
/**
* Execute the query returning a list using the mapper.
*
* @param mapper Used to map each ResultSet row into the result object.
* Deprecated migrate to use {@link #mapTo(RowMapper)}
*/
@Deprecated
<T> List<T> findList(RowMapper<T> mapper);
/**
@@ -158,28 +156,14 @@ public interface SqlQuery extends Serializable {
Long findSingleLong();
/**
* Execute the query returning a list of scalar attribute values.
*
* Deprecated - migrate to <code>.mapToScalar(Long.class).findList()</code>.
* <pre>{@code
*
* String sql =
* " select (unit_price * order_qty) " +
* " from o_order_detail " +
* " where unit_price > ? " +
* " order by (unit_price * order_qty) desc";
*
* List<BigDecimal> lineAmounts =
* DB.sqlQuery(sql)
* .setParameter(42)
* .findSingleAttributeList(BigDecimal.class);
*
* }</pre>
*
* <p>
* The attributeType can be any scalar type that Ebean supports (includes javax time types, Joda types etc).
*
* @param attributeType The type of the returned value
* .mapToScalar(Long.class)
* .findList();
* }
*/
@Deprecated
<T> List<T> findSingleAttributeList(Class<T> attributeType);
/**
@@ -312,14 +296,23 @@ public interface SqlQuery extends Serializable {
* @param attributeType The type the result is returned as
* @return The query to execute via findOne() findList() etc
*/
<T> ScalarQuery<T> mapToScalar(Class<T> attributeType);
<T> TypeQuery<T> mapToScalar(Class<T> attributeType);
/**
* Use a RowMapper to map the result to beans.
*
* @param mapper Maps rows to beans
* @param <T> The type of beans mapped to
* @return The query to execute by findOne() findList() etc
*/
<T> TypeQuery<T> mapTo(RowMapper<T> mapper);
/**
* Query mapping to single scalar values.
*
* @param <T> The type of the scalar values
*/
interface ScalarQuery<T> {
interface TypeQuery<T> {
/**
* Return the single value.
@@ -208,12 +208,25 @@ public class DefaultRelationalQuery implements SpiSqlQuery {
return query;
}
<T> T mapperFindOne(RowMapper<T> mapper) {
return server.findOneMapper(this, mapper);
}
<T> List<T> mapperFindList(RowMapper<T> mapper) {
return server.findListMapper(this, mapper);
}
@Override
public <T> ScalarQuery<T> mapToScalar(Class<T> attributeType) {
public <T> TypeQuery<T> mapToScalar(Class<T> attributeType) {
return new Scalar(attributeType);
}
private class Scalar<T> implements SqlQuery.ScalarQuery<T> {
@Override
public <T> TypeQuery<T> mapTo(RowMapper<T> mapper) {
return new Mapper(mapper);
}
private class Scalar<T> implements SqlQuery.TypeQuery<T> {
private final Class<T> type;
@@ -236,4 +249,28 @@ public class DefaultRelationalQuery implements SpiSqlQuery {
return findSingleAttributeList(type);
}
}
private class Mapper<T> implements SqlQuery.TypeQuery<T> {
private final RowMapper<T> mapper;
Mapper(RowMapper<T> mapper) {
this.mapper = mapper;
}
@Override
public T findOne() {
return mapperFindOne(mapper);
}
@Override
public Optional<T> findOneOrEmpty() {
return Optional.ofNullable(findOne());
}
@Override
public List<T> findList() {
return mapperFindList(mapper);
}
}
}