mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-17 22:56:28 +00:00
#1589 - Refactor internals move SqlQuery sqlDistinct ... to SqlTreeNodeRoot (query plan)
This commit is contained in:
1 parent
82def2710e
commit
0397140e0b
10 files changed
+39
-90
No files matched your search
@@ -280,13 +280,8 @@ class CQueryBuilder {
|
||||
if (sqlTree.isSingleProperty()) {
|
||||
request.setInlineCountDistinct();
|
||||
}
|
||||
} else {
|
||||
if (hasMany) {
|
||||
// need to count distinct id's ...
|
||||
query.setSqlDistinct(true);
|
||||
} else {
|
||||
sqlSelect = "select count(*)";
|
||||
}
|
||||
} else if (!hasMany) {
|
||||
sqlSelect = "select count(*)";
|
||||
}
|
||||
|
||||
SqlLimitResponse s = buildSql(sqlSelect, request, predicates, sqlTree);
|
||||
@@ -556,6 +551,7 @@ class CQueryBuilder {
|
||||
return rawSqlHandler.buildSql(request, predicates, query.getRawSql().getSql());
|
||||
}
|
||||
|
||||
boolean distinct = query.isDistinct() || select.isSqlDistinct();
|
||||
boolean useSqlLimiter = false;
|
||||
StringBuilder sb = new StringBuilder(500);
|
||||
String dbOrderBy = predicates.getDbOrderBy();
|
||||
@@ -568,7 +564,7 @@ class CQueryBuilder {
|
||||
|
||||
if (!useSqlLimiter) {
|
||||
sb.append("select ");
|
||||
if (query.isDistinctQuery()) {
|
||||
if (distinct) {
|
||||
if (request.isInlineCountDistinct()) {
|
||||
sb.append("count(");
|
||||
}
|
||||
@@ -590,7 +586,7 @@ class CQueryBuilder {
|
||||
if (request.isInlineCountDistinct()) {
|
||||
sb.append(")");
|
||||
}
|
||||
if (query.isDistinctQuery() && dbOrderBy != null && !query.isSingleAttribute()) {
|
||||
if (distinct && dbOrderBy != null && !query.isSingleAttribute()) {
|
||||
// add the orderBy columns to the select clause (due to distinct)
|
||||
sb.append(", ").append(DbOrderByTrim.trim(dbOrderBy));
|
||||
}
|
||||
@@ -692,7 +688,7 @@ class CQueryBuilder {
|
||||
|
||||
if (useSqlLimiter) {
|
||||
// use LIMIT/OFFSET, ROW_NUMBER() or rownum type SQL query limitation
|
||||
SqlLimitRequest r = new OrmQueryLimitRequest(sb.toString(), dbOrderBy, query, dbPlatform);
|
||||
SqlLimitRequest r = new OrmQueryLimitRequest(sb.toString(), dbOrderBy, query, dbPlatform, distinct);
|
||||
return sqlLimiter.limit(r);
|
||||
|
||||
} else {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package io.ebeaninternal.server.query;
|
||||
|
||||
import io.ebeaninternal.server.rawsql.SpiRawSql;
|
||||
import io.ebean.config.dbplatform.DatabasePlatform;
|
||||
import io.ebean.config.dbplatform.SqlLimitResponse;
|
||||
import io.ebean.config.dbplatform.SqlLimiter;
|
||||
@@ -9,6 +8,7 @@ import io.ebeaninternal.api.SpiQuery;
|
||||
import io.ebeaninternal.server.core.OrmQueryRequest;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import io.ebeaninternal.server.querydefn.OrmQueryLimitRequest;
|
||||
import io.ebeaninternal.server.rawsql.SpiRawSql;
|
||||
import io.ebeaninternal.server.util.BindParamsParser;
|
||||
|
||||
class CQueryBuilderRawSql {
|
||||
@@ -50,7 +50,7 @@ class CQueryBuilderRawSql {
|
||||
SpiQuery<?> query = request.getQuery();
|
||||
if (query.hasMaxRowsOrFirstRow() && sqlLimiter != null) {
|
||||
// wrap with a limit offset or ROW_NUMBER() etc
|
||||
return sqlLimiter.limit(new OrmQueryLimitRequest(sql, orderBy, query, dbPlatform));
|
||||
return sqlLimiter.limit(new OrmQueryLimitRequest(sql, orderBy, query, dbPlatform, rsql.isDistinct() || query.isDistinct()));
|
||||
|
||||
} else {
|
||||
// add back select keyword (it was removed to support sqlQueryLimiter)
|
||||
|
||||
@@ -65,6 +65,13 @@ class SqlTree {
|
||||
this.includeJoins = includeJoins;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the query mandates SQL Distinct due to ToMany inclusion.
|
||||
*/
|
||||
boolean isSqlDistinct() {
|
||||
return rootNode.isSqlDistinct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the query includes joins (not valid for rawSql).
|
||||
*/
|
||||
|
||||
@@ -70,6 +70,8 @@ public final class SqlTreeBuilder {
|
||||
|
||||
private SqlTreeNode rootNode;
|
||||
|
||||
private boolean sqlDistinct;
|
||||
|
||||
/**
|
||||
* Construct for RawSql query.
|
||||
*/
|
||||
@@ -172,7 +174,7 @@ public final class SqlTreeBuilder {
|
||||
|
||||
private String buildDistinctOn() {
|
||||
|
||||
if (rawSql || !distinctOnPlatform || !query.isSqlDistinct() || Type.COUNT == query.getType()) {
|
||||
if (rawSql || !distinctOnPlatform || !sqlDistinct || Type.COUNT == query.getType()) {
|
||||
return null;
|
||||
}
|
||||
ctx.startGroupBy();
|
||||
@@ -273,7 +275,7 @@ public final class SqlTreeBuilder {
|
||||
|
||||
if (prefix == null && !rawSql) {
|
||||
if (props.requireSqlDistinct(manyWhereJoins)) {
|
||||
query.setSqlDistinct(true);
|
||||
sqlDistinct = true;
|
||||
}
|
||||
addManyWhereJoins(myJoinList);
|
||||
}
|
||||
@@ -310,7 +312,7 @@ public final class SqlTreeBuilder {
|
||||
// Optional many property for lazy loading query
|
||||
STreePropertyAssocMany lazyLoadMany = (query == null) ? null : query.getLazyLoadMany();
|
||||
boolean withId = !rawNoId && !subQuery && (query == null || query.isWithId());
|
||||
return new SqlTreeNodeRoot(desc, props, myList, withId, includeJoin, lazyLoadMany, temporalMode, disableLazyLoad);
|
||||
return new SqlTreeNodeRoot(desc, props, myList, withId, includeJoin, lazyLoadMany, temporalMode, disableLazyLoad, sqlDistinct);
|
||||
|
||||
} else if (prop instanceof STreePropertyAssocMany) {
|
||||
return new SqlTreeNodeManyRoot(prefix, (STreePropertyAssocMany) prop, props, myList, temporalMode, disableLazyLoad);
|
||||
@@ -361,7 +363,7 @@ public final class SqlTreeBuilder {
|
||||
// as we are now going to join to the many then we need
|
||||
// to add the distinct to the sql query to stop duplicate
|
||||
// rows...
|
||||
query.setSqlDistinct(true);
|
||||
sqlDistinct = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,10 @@ interface SqlTreeNode {
|
||||
*/
|
||||
void buildRawSqlSelectChain(List<String> selectChain);
|
||||
|
||||
default boolean isSqlDistinct() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this node includes an aggregation.
|
||||
*/
|
||||
|
||||
@@ -14,14 +14,17 @@ final class SqlTreeNodeRoot extends SqlTreeNodeBean {
|
||||
|
||||
private final TableJoin includeJoin;
|
||||
|
||||
private final boolean sqlDistinct;
|
||||
|
||||
/**
|
||||
* Specify for SqlSelect to include an Id property or not.
|
||||
*/
|
||||
SqlTreeNodeRoot(STreeType desc, SqlTreeProperties props, List<SqlTreeNode> myList, boolean withId,
|
||||
TableJoin includeJoin, STreePropertyAssocMany many, SpiQuery.TemporalMode temporalMode, boolean disableLazyLoad) {
|
||||
TableJoin includeJoin, STreePropertyAssocMany many, SpiQuery.TemporalMode temporalMode, boolean disableLazyLoad, boolean sqlDistinct) {
|
||||
|
||||
super(desc, props, myList, withId, many, temporalMode, disableLazyLoad);
|
||||
this.includeJoin = includeJoin;
|
||||
this.sqlDistinct = sqlDistinct;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -29,6 +32,11 @@ final class SqlTreeNodeRoot extends SqlTreeNodeBean {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSqlDistinct() {
|
||||
return sqlDistinct;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append the property columns to the buffer.
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user