#728 - Remove Query.where(String) ... migrate to where().raw(...)

This commit is contained in:
Robin Bygrave
2016-06-01 19:54:02 +12:00
parent 71f70c01ae
commit 92caf69cb3
9 changed files with 106 additions and 505 deletions
-57
View File
@@ -917,40 +917,6 @@ public interface Query<T> {
*/
Object getId();
/**
* Add additional clause(s) to the where clause.
* <p>
* This typically contains named parameters which will need to be set via
* {@link #setParameter(String, Object)}.
* </p>
*
* <pre>{@code
*
* Query<Order> query = ebeanServer.createQuery(Order.class, "top");
* ...
* if (...) {
* query.where("status = :status and lower(customer.name) like :custName");
* query.setParameter("status", Order.NEW);
* query.setParameter("custName", "rob%");
* }
*
* }</pre>
*
* <p>
* Internally the addToWhereClause string is processed by removing named
* parameters (replacing them with ?) and by converting logical property names
* to database column names (with table alias). The rest of the string is left
* as is and it is completely acceptable and expected for the addToWhereClause
* string to include sql functions and columns.
* </p>
*
* @param addToWhereClause
* the clause to append to the where clause which typically contains
* named parameters.
* @return The query object
*/
Query<T> where(String addToWhereClause);
/**
* Add a single Expression to the where clause returning the query.
*
@@ -1061,29 +1027,6 @@ public interface Query<T> {
*/
ExpressionList<T> having();
/**
* Add additional clause(s) to the having clause.
* <p>
* This typically contains named parameters which will need to be set via
* {@link #setParameter(String, Object)}.
* </p>
*
* <pre>{@code
*
* List<ReportOrder> query =
* ebeanServer.find(ReportOrder.class)
* .having("score > :min").setParameter("min", 1)
* .findList();
*
* }</pre>
*
* @param addToHavingClause
* the clause to append to the having clause which typically contains
* named parameters.
* @return The query object
*/
Query<T> having(String addToHavingClause);
/**
* Add an expression to the having clause returning the query.
* <p>
@@ -475,12 +475,6 @@ public interface SpiQuery<T> extends Query<T> {
*/
OrderBy<T> getOrderBy();
/**
* Return additional where clause. This should be added to any where clause
* that was part of the original query.
*/
String getAdditionalWhere();
/**
* Can return null if no expressions where added to the where clause.
*/
@@ -496,12 +490,6 @@ public interface SpiQuery<T> extends Query<T> {
*/
SpiExpressionList<T> getTextExpression();
/**
* Return additional having clause. Where raw String expressions are added
* to having clause rather than Expression objects.
*/
String getAdditionalHaving();
/**
* Returns true if either firstRow or maxRows has been set.
*/
@@ -622,11 +610,6 @@ public interface SpiQuery<T> extends Query<T> {
*/
void setDefaultSelectClause();
/**
* Return the where clause from a parsed string query.
*/
String getRawWhereClause();
/**
* Set the generated sql for debug purposes.
*/
@@ -2,7 +2,6 @@ package com.avaje.ebeaninternal.server.query;
import com.avaje.ebean.RawSql;
import com.avaje.ebeaninternal.api.BindParams;
import com.avaje.ebeaninternal.api.BindParams.OrderedList;
import com.avaje.ebeaninternal.api.SpiExpressionList;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.server.core.OrmQueryRequest;
@@ -54,11 +53,6 @@ public class CQueryPredicates {
*/
private final BindParams bindParams;
/**
* Named bind parameters for the having clause.
*/
private OrderedList havingNamedParams;
/**
* Bind values from the where expressions.
*/
@@ -79,11 +73,6 @@ public class CQueryPredicates {
*/
private String whereExprSql;
/**
* SQL generated from where with named parameters.
*/
private String whereRawSql;
/**
* Bind values for having expression.
*/
@@ -94,11 +83,6 @@ public class CQueryPredicates {
*/
private String havingExprSql;
/**
* SQL generated from having with named parameters.
*/
private String havingRawSql;
private String dbHaving;
/**
@@ -193,11 +177,6 @@ public class CQueryPredicates {
}
}
if (havingNamedParams != null) {
// bind named parameters in having...
binder.bind(havingNamedParams.list(), dataBind, dataBind.log());
}
if (having != null) {
having.bind(dataBind);
}
@@ -214,84 +193,26 @@ public class CQueryPredicates {
}
}
private void buildBindHavingRawSql(boolean buildSql, boolean parseRaw, DeployParser deployParser) {
if (buildSql || bindParams != null) {
// having clause with named parameters...
havingRawSql = query.getAdditionalHaving();
if (parseRaw) {
havingRawSql = deployParser.parse(havingRawSql);
}
if (havingRawSql != null && bindParams != null) {
// convert and order named parameters if required
havingNamedParams = BindParamsParser.parseNamedParams(bindParams, havingRawSql);
havingRawSql = havingNamedParams.getPreparedSql();
}
}
}
/**
* Convert named parameters into an OrderedList.
*/
private void buildBindWhereRawSql(boolean buildSql, boolean parseRaw, DeployParser parser) {
if (buildSql || bindParams != null) {
whereRawSql = buildWhereRawSql();
boolean hasRaw = !"".equals(whereRawSql);
if (hasRaw && parseRaw) {
// parse with encrypted property awareness. This means that if we have
// an encrypted property we will insert special named parameter place
// holders for binding the encryption key values
parser.setEncrypted(true);
whereRawSql = parser.parse(whereRawSql);
parser.setEncrypted(false);
}
private void buildBindWhereRawSql(boolean buildSql) {
if (bindParams != null) {
if (hasRaw) {
whereRawSql = BindParamsParser.parse(bindParams, whereRawSql, request.getBeanDescriptor());
} else if (query.isRawSql() && !buildSql) {
// RawSql query hit cached query plan. Need to convert
// named parameters into positioned parameters so that
// the named parameters are bound
RawSql.Sql sql = query.getRawSql().getSql();
String s = sql.isParsed() ? sql.getPreWhere() : sql.getUnparsedSql();
if (bindParams.requiresNamedParamsPrepare()) {
BindParamsParser.parse(bindParams, s);
}
}
}
if (!buildSql && query.isRawSql() && bindParams != null && bindParams.requiresNamedParamsPrepare()) {
// RawSql query hit cached query plan. Need to convert
// named parameters into positioned parameters so that
// the named parameters are bound
RawSql.Sql sql = query.getRawSql().getSql();
String s = sql.isParsed() ? sql.getPreWhere() : sql.getUnparsedSql();
BindParamsParser.parse(bindParams, s);
}
}
private String buildWhereRawSql() {
// this is the where part of a OQL query which
// may contain bind parameters...
String whereRaw = query.getRawWhereClause();
if (whereRaw == null) {
whereRaw = "";
}
// add any additional stuff to the where clause
String additionalWhere = query.getAdditionalWhere();
if (additionalWhere != null) {
whereRaw += additionalWhere;
}
return whereRaw;
}
public void prepare(boolean buildSql) {
DeployParser deployParser = request.createDeployParser();
prepare(buildSql, true, deployParser);
}
/**
* This combines the sql from named/positioned parameters and expressions.
*/
private void prepare(boolean buildSql, boolean parseRaw, DeployParser deployParser) {
buildUpdateClause(buildSql, deployParser);
buildBindWhereRawSql(buildSql, parseRaw, deployParser);
buildBindHavingRawSql(buildSql, parseRaw, deployParser);
buildBindWhereRawSql(buildSql);
SpiExpressionList<?> whereExp = query.getWhereExpressions();
if (whereExp != null) {
@@ -360,7 +281,7 @@ public class CQueryPredicates {
}
private String deriveWhere(DeployParser deployParser) {
return parse(whereRawSql, whereExprSql, deployParser);
return parse(whereExprSql, deployParser);
}
/**
@@ -387,12 +308,9 @@ public class CQueryPredicates {
return s == null || s.length() == 0;
}
private String parse(String raw, String expr, DeployParser deployParser) {
private String parse(String expr, DeployParser deployParser) {
StringBuilder sb = new StringBuilder();
if (!isEmpty(raw)) {
sb.append(raw);
}
if (!isEmpty(expr)) {
if (sb.length() > 0) {
sb.append(" and ");
@@ -403,7 +321,7 @@ public class CQueryPredicates {
}
private String deriveHaving(DeployParser deployParser) {
return parse(havingRawSql, havingExprSql, deployParser);
return parse(havingExprSql, deployParser);
}
private String parseOrderBy() {
@@ -81,11 +81,6 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
*/
private int lazyLoadBatchSize;
/**
* The where clause from a parsed query string.
*/
private String rawWhereClause;
private OrderBy<T> orderBy;
private String loadMode;
@@ -99,10 +94,6 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
*/
private String query;
private String additionalWhere;
private String additionalHaving;
private String lazyLoadProperty;
private String lazyLoadManyPath;
@@ -563,8 +554,6 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
copy.query = query;
copy.rootTableAlias = rootTableAlias;
copy.additionalWhere = additionalWhere;
copy.additionalHaving = additionalHaving;
copy.distinct = distinct;
copy.sqlDistinct = sqlDistinct;
copy.timeout = timeout;
@@ -580,7 +569,6 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
copy.temporalMode = temporalMode;
copy.firstRow = firstRow;
copy.maxRows = maxRows;
copy.rawWhereClause = rawWhereClause;
if (orderBy != null) {
copy.orderBy = orderBy.copy();
}
@@ -599,7 +587,6 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
copy.parentNode = parentNode;
copy.forUpdate = forUpdate;
copy.rawSql = rawSql;
copy.rawWhereClause = rawWhereClause;
return copy;
}
@@ -831,7 +818,7 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
CQueryPlanKey createQueryPlanKey() {
queryPlanKey = new OrmQueryPlanKey(includeTableJoin, type, detail, maxRows, firstRow,
disableLazyLoading, rawWhereClause, orderBy, query, additionalWhere, additionalHaving,
disableLazyLoading, orderBy, query,
distinct, sqlDistinct, mapKey, id, bindParams, whereExpressions, havingExpressions,
temporalMode, forUpdate, rootTableAlias, rawSql, updateProperties);
@@ -901,14 +888,6 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
return rawSql != null;
}
/**
* Return any additional where clauses.
*/
@Override
public String getAdditionalWhere() {
return additionalWhere;
}
/**
* Return the timeout.
*/
@@ -917,14 +896,6 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
return timeout;
}
/**
* Return any additional having clauses.
*/
@Override
public String getAdditionalHaving() {
return additionalHaving;
}
@Override
public boolean hasMaxRowsOrFirstRow() {
return maxRows > 0 || firstRow > 0;
@@ -1002,10 +973,6 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
return this;
}
protected void setRawWhereClause(String rawWhereClause) {
this.rawWhereClause = rawWhereClause;
}
@Override
public DefaultOrmQuery<T> select(String columns) {
detail.select(columns);
@@ -1164,14 +1131,6 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
return orderBy;
}
/**
* Return the order by clause.
*/
@Override
public String getRawWhereClause() {
return rawWhereClause;
}
@Override
public OrderBy<T> orderBy() {
return order();
@@ -1335,22 +1294,9 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
return query;
}
@Override
public DefaultOrmQuery<T> where(String addToWhereClause) {
if (additionalWhere == null) {
additionalWhere = addToWhereClause;
} else {
additionalWhere += " " + addToWhereClause;
}
return this;
}
@Override
public DefaultOrmQuery<T> where(Expression expression) {
if (whereExpressions == null) {
whereExpressions = new DefaultExpressionList<T>(this, null);
}
whereExpressions.add(expression);
where().add(expression);
return this;
}
@@ -1371,22 +1317,9 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
return whereExpressions;
}
@Override
public DefaultOrmQuery<T> having(String addToHavingClause) {
if (additionalHaving == null) {
additionalHaving = addToHavingClause;
} else {
additionalHaving += " " + addToHavingClause;
}
return this;
}
@Override
public DefaultOrmQuery<T> having(Expression expression) {
if (havingExpressions == null) {
havingExpressions = new DefaultExpressionList<T>(this, null);
}
havingExpressions.add(expression);
having().add(expression);
return this;
}
@@ -36,14 +36,6 @@ public class OrmQueryDetailParser {
return detail;
}
protected void assign(DefaultOrmQuery<?> query) {
query.setOrmQueryDetail(detail);
query.setFirstRow(firstRow);
query.setMaxRows(maxRows);
query.setRawWhereClause(rawWhereClause);
query.order(rawOrderBy);
}
private void processInitial() {
if (parser.isMatch("select")) {
readSelect();
@@ -25,10 +25,7 @@ public class OrmQueryPlanKey implements CQueryPlanKey {
private final int maxRows;
private final int firstRow;
private final boolean disableLazyLoading;
private final String rawWhereClause;
private final String query;
private final String additionalWhere;
private final String additionalHaving;
private final boolean distinct;
private final boolean sqlDistinct;
private final String mapKey;
@@ -40,7 +37,7 @@ public class OrmQueryPlanKey implements CQueryPlanKey {
private final int planHash;
private final int bindCount;
public OrmQueryPlanKey(TableJoin includeTableJoin, SpiQuery.Type type, OrmQueryDetail detail, int maxRows, int firstRow, boolean disableLazyLoading, String rawWhereClause, OrderBy<?> orderBy, String query, String additionalWhere, String additionalHaving, boolean distinct, boolean sqlDistinct, String mapKey, Object id, BindParams bindParams, SpiExpression whereExpressions, SpiExpression havingExpressions, SpiQuery.TemporalMode temporalMode, boolean forUpdate, String rootTableAlias, RawSql rawSql, OrmUpdateProperties updateProperties) {
public OrmQueryPlanKey(TableJoin includeTableJoin, SpiQuery.Type type, OrmQueryDetail detail, int maxRows, int firstRow, boolean disableLazyLoading, OrderBy<?> orderBy, String query, boolean distinct, boolean sqlDistinct, String mapKey, Object id, BindParams bindParams, SpiExpression whereExpressions, SpiExpression havingExpressions, SpiQuery.TemporalMode temporalMode, boolean forUpdate, String rootTableAlias, RawSql rawSql, OrmUpdateProperties updateProperties) {
this.includeTableJoin = includeTableJoin;
this.type = type;
@@ -48,11 +45,8 @@ public class OrmQueryPlanKey implements CQueryPlanKey {
this.maxRows = maxRows;
this.firstRow = firstRow;
this.disableLazyLoading = disableLazyLoading;
this.rawWhereClause = rawWhereClause;
this.orderByAsSting = (orderBy == null) ? null : orderBy.toStringFormat();
this.query = query;
this.additionalWhere = additionalWhere;
this.additionalHaving = additionalHaving;
this.distinct = distinct;
this.sqlDistinct = sqlDistinct;
this.mapKey = mapKey;
@@ -72,7 +66,6 @@ public class OrmQueryPlanKey implements CQueryPlanKey {
builder.add(distinct).add(sqlDistinct).add(query);
builder.add(firstRow).add(maxRows);
builder.add(orderBy).add(forUpdate);
builder.add(rawWhereClause).add(additionalWhere).add(additionalHaving);
builder.add(mapKey);
builder.add(disableLazyLoading);
builder.add(hasIdValue);
@@ -138,10 +131,7 @@ public class OrmQueryPlanKey implements CQueryPlanKey {
// if (detail != null ? !detail.equals(that.detail) : that.detail != null) return false;
if (rawWhereClause != null ? !rawWhereClause.equals(that.rawWhereClause) : that.rawWhereClause != null) return false;
if (query != null ? !query.equals(that.query) : that.query != null) return false;
if (additionalWhere != null ? !additionalWhere.equals(that.additionalWhere) : that.additionalWhere != null) return false;
if (additionalHaving != null ? !additionalHaving.equals(that.additionalHaving) : that.additionalHaving != null) return false;
if (mapKey != null ? !mapKey.equals(that.mapKey) : that.mapKey != null) return false;
return rootTableAlias != null ? rootTableAlias.equals(that.rootTableAlias) : that.rootTableAlias == null;
}