#1334 - ENH: Convert ORM query into DTO query ... with dynamic formula support

This commit is contained in:
Rob Bygrave committed 2018-03-07 01:52:18 +13:00
1 parent 83ef204f3c
commit 55a257a64c
28 files changed
+720 -138

No files matched your search

@@ -4,6 +4,7 @@ import io.ebean.DtoQuery;
import io.ebeaninternal.api.BindParams;
import io.ebeaninternal.api.SpiDtoQuery;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.server.dto.DtoBeanDescriptor;
import io.ebeaninternal.server.dto.DtoMappingRequest;
import io.ebeaninternal.server.dto.DtoQueryPlan;
@@ -22,6 +23,8 @@ public class DefaultDtoQuery<T> implements SpiDtoQuery<T> {
private final DtoBeanDescriptor<T> descriptor;
private final SpiQuery<?> ormQuery;
private String sql;
private int firstRow;
@@ -42,11 +45,21 @@ public class DefaultDtoQuery<T> implements SpiDtoQuery<T> {
private final BindParams bindParams = new BindParams();
/**
* Additional supply a query detail object.
* Create given an underlying ORM query.
*/
public DefaultDtoQuery(SpiEbeanServer server, DtoBeanDescriptor<T> descriptor, SpiQuery<?> ormQuery) {
this.server = server;
this.descriptor = descriptor;
this.ormQuery = ormQuery;
}
/**
* Create given a native SQL query.
*/
public DefaultDtoQuery(SpiEbeanServer server, DtoBeanDescriptor<T> descriptor, String sql) {
this.server = server;
this.descriptor = descriptor;
this.ormQuery = null;
this.sql = sql;
}
@@ -56,7 +69,7 @@ public class DefaultDtoQuery<T> implements SpiDtoQuery<T> {
}
@Override
public DtoQueryPlan getQueryPlan(String planKey) {
public DtoQueryPlan getQueryPlan(Object planKey) {
return descriptor.getQueryPlan(planKey);
}
@@ -66,7 +79,7 @@ public class DefaultDtoQuery<T> implements SpiDtoQuery<T> {
}
@Override
public void putQueryPlan(String planKey, DtoQueryPlan plan) {
public void putQueryPlan(Object planKey, DtoQueryPlan plan) {
descriptor.putQueryPlan(planKey, plan);
}
@@ -97,13 +110,21 @@ public class DefaultDtoQuery<T> implements SpiDtoQuery<T> {
@Override
public DtoQuery<T> setParameter(int position, Object value) {
bindParams.setParameter(position, value);
if (ormQuery != null) {
ormQuery.setParameter(position, value);
} else {
bindParams.setParameter(position, value);
}
return this;
}
@Override
public DtoQuery<T> setParameter(String paramName, Object value) {
bindParams.setParameter(paramName, value);
if (ormQuery != null) {
ormQuery.setParameter(paramName, value);
} else {
bindParams.setParameter(paramName, value);
}
return this;
}
@@ -117,6 +138,10 @@ public class DefaultDtoQuery<T> implements SpiDtoQuery<T> {
return descriptor.getType();
}
public SpiQuery<?> getOrmQuery() {
return ormQuery;
}
@Override
public DtoQuery<T> setRelaxedMode() {
this.relaxedMode = true;
@@ -147,6 +172,9 @@ public class DefaultDtoQuery<T> implements SpiDtoQuery<T> {
@Override
public DtoQuery<T> setFirstRow(int firstRow) {
this.firstRow = firstRow;
if (ormQuery != null) {
ormQuery.setFirstRow(firstRow);
}
return this;
}
@@ -158,6 +186,9 @@ public class DefaultDtoQuery<T> implements SpiDtoQuery<T> {
@Override
public DtoQuery<T> setMaxRows(int maxRows) {
this.maxRows = maxRows;
if (ormQuery != null) {
ormQuery.setMaxRows(maxRows);
}
return this;
}
@@ -2,6 +2,7 @@ package io.ebeaninternal.server.querydefn;
import io.ebean.CacheMode;
import io.ebean.CountDistinctOrder;
import io.ebean.DtoQuery;
import io.ebean.Expression;
import io.ebean.ExpressionFactory;
import io.ebean.ExpressionList;
@@ -130,6 +131,11 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
private String lazyLoadManyPath;
/**
* Flag set for report/DTO beans when we may choose to explicitly include the Id property.
*/
private boolean manualId;
/**
* Set to true by a user wanting a DISTINCT query (id property must be excluded).
*/
@@ -280,6 +286,11 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
this.nativeSql = nativeSql;
}
@Override
public <D> DtoQuery<D> asDto(Class<D> dtoClass) {
return server.findDto(dtoClass, this);
}
@Override
public BeanDescriptor<T> getBeanDescriptor() {
return beanDescriptor;
@@ -668,7 +679,7 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
*/
@Override
public boolean isWithId() {
return !distinct && !singleAttribute;
return !manualId && !distinct && !singleAttribute;
}
@Override
@@ -1027,7 +1038,7 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
queryPlanKey = new NativeSqlQueryPlanKey(nativeSql + "-" + firstRow + "-" + maxRows);
} else {
queryPlanKey = new OrmQueryPlanKey(beanDescriptor.getDiscValue(), m2mIncludeJoin, type, detail, maxRows, firstRow,
disableLazyLoading, orderBy,
disableLazyLoading, manualId, orderBy,
distinct, sqlDistinct, mapKey, id, bindParams, whereExpressions, havingExpressions,
temporalMode, forUpdate, rootTableAlias, rawSql, updateProperties, countDistinctOrder);
}
@@ -1044,6 +1055,11 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
return nativeSql;
}
@Override
public Object getQueryPlanKey() {
return queryPlanKey;
}
/**
* Prepare the query which prepares any expressions (sub-query expressions etc) and calculates the query plan key.
*/
@@ -1475,6 +1491,16 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
return this;
}
@Override
public boolean isManualId() {
return manualId;
}
@Override
public void setManualId(boolean manualId) {
this.manualId = manualId;
}
/**
* return true if user specified to use SQL DISTINCT (effectively excludes id property).
*/
@@ -22,7 +22,7 @@ class OrmQueryPlanKey implements CQueryPlanKey {
private final String description;
OrmQueryPlanKey(String discValue, TableJoin m2mIncludeTable, SpiQuery.Type type, OrmQueryDetail detail, int maxRows, int firstRow, boolean disableLazyLoading,
OrderBy<?> orderBy, boolean distinct, boolean sqlDistinct, String mapKey, Object id, BindParams bindParams,
boolean manualId, OrderBy<?> orderBy, boolean distinct, boolean sqlDistinct, String mapKey, Object id, BindParams bindParams,
SpiExpression whereExpressions, SpiExpression havingExpressions, SpiQuery.TemporalMode temporalMode,
Query.ForUpdate forUpdate, String rootTableAlias, SpiRawSql rawSql, OrmUpdateProperties updateProperties, CountDistinctOrder countDistinctOrder) {
@@ -42,6 +42,9 @@ class OrmQueryPlanKey implements CQueryPlanKey {
if (id != null) {
sb.append(",id:");
}
if (manualId) {
sb.append(",manId:");
}
if (distinct) {
sb.append(",dist:");
}