#642 - ENH: Add support for OneToMany expression isEmpty() / isNotEmpty() ... which is effectively an EXISTS and NOT EXISTS subquery

This commit is contained in:
Robin Bygrave committed 2016-04-08 13:41:43 +12:00
1 parent c58db0033d
commit 4059034891
20 files changed
+675 -294

No files matched your search

@@ -2,6 +2,7 @@ package com.avaje.ebeaninternal.server.deploy;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.text.StringParser;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
/**
@@ -123,6 +124,11 @@ public final class BeanFkeyProperty implements ElPropertyValue {
return null;
}
@Override
public String getAssocIsEmpty(SpiExpressionRequest request, String path) {
throw new RuntimeException("Not Supported or Expected");
}
/**
* Returns false as not an AssocOne.
*/
@@ -5,6 +5,7 @@ import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.config.EncryptKey;
import com.avaje.ebean.config.dbplatform.DbEncryptFunction;
import com.avaje.ebean.config.dbplatform.DbType;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeanservice.docstore.api.mapping.DocMappingBuilder;
import com.avaje.ebeanservice.docstore.api.mapping.DocPropertyMapping;
import com.avaje.ebean.plugin.Property;
@@ -834,6 +835,12 @@ public class BeanProperty implements ElPropertyValue, Property {
return false;
}
@Override
public String getAssocIsEmpty(SpiExpressionRequest request, String path) {
// overridden in BanePropertyAssocMany
throw new RuntimeException("Not Supported or Expected");
}
public Object[] getAssocIdValues(EntityBean bean) {
// Returns null as not an AssocOne.
return null;
@@ -11,6 +11,7 @@ import com.avaje.ebean.bean.BeanCollectionAdd;
import com.avaje.ebean.bean.BeanCollectionLoader;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.text.PathProperties;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.server.core.DefaultSqlUpdate;
import com.avaje.ebeaninternal.server.deploy.id.ImportedId;
@@ -481,6 +482,28 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> {
help.add(collection, bean, false);
}
@Override
public String getAssocIsEmpty(SpiExpressionRequest request, String path) {
StringBuilder sb = new StringBuilder();
SpiQuery<?> query = request.getQueryRequest().getQuery();
if (manyToMany) {
sb.append(query.isAsDraft() ? intersectionDraftTable : intersectionPublishTable);
} else {
sb.append(targetDescriptor.getBaseTable(query.getTemporalMode()));
}
sb.append(" where ");
for (int i = 0; i < exportedProperties.length; i++) {
if (i > 0) {
sb.append(" and ");
}
exportedProperties[i].appendWhere(sb, path);
}
return sb.toString();
}
/**
* Return the Id values from the given bean.
*/
@@ -11,43 +11,53 @@ import com.avaje.ebeaninternal.server.core.InternString;
*/
public class ExportedProperty {
private final String foreignDbColumn;
private final BeanProperty property;
private final String foreignDbColumn;
private final boolean embedded;
public ExportedProperty(boolean embedded, String foreignDbColumn, BeanProperty property) {
this.embedded = embedded;
this.foreignDbColumn = InternString.intern(foreignDbColumn);
this.property = property;
}
/**
* Return true if this is part of an embedded concatinated key.
*/
public boolean isEmbedded() {
return embedded;
}
/**
* Return the property value from the bean.
*/
public Object getValue(EntityBean bean){
return property.getValue(bean);
}
/**
* Return the foreign database column matching this property.
* <p>
* We use this foreign database column in the query predicates
* in preference to a parentProperty.idProperty = value.
* Just using the foreign database column avoids triggering
* a join to the 'parent' table.
* </p>
*/
public String getForeignDbColumn() {
return foreignDbColumn;
}
private final BeanProperty property;
private final boolean embedded;
public ExportedProperty(boolean embedded, String foreignDbColumn, BeanProperty property) {
this.embedded = embedded;
this.foreignDbColumn = InternString.intern(foreignDbColumn);
this.property = property;
}
/**
* Return true if this is part of an embedded concatinated key.
*/
public boolean isEmbedded() {
return embedded;
}
/**
* Return the property value from the bean.
*/
public Object getValue(EntityBean bean) {
return property.getValue(bean);
}
/**
* Return the foreign database column matching this property.
* <p>
* We use this foreign database column in the query predicates
* in preference to a parentProperty.idProperty = value.
* Just using the foreign database column avoids triggering
* a join to the 'parent' table.
* </p>
*/
public String getForeignDbColumn() {
return foreignDbColumn;
}
/**
* Append a logical where for the foreign db column to logical property name,
*/
public void appendWhere(StringBuilder sb, String path) {
sb.append(foreignDbColumn).append(" = ");
if (path != null) {
sb.append(path).append(".");
}
sb.append(property.getName());
}
}
@@ -2,6 +2,7 @@ package com.avaje.ebeaninternal.server.el;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.text.StringParser;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.lib.util.StringHelper;
import com.avaje.ebeaninternal.server.query.SplitName;
@@ -154,6 +155,11 @@ public class ElPropertyChain implements ElPropertyValue {
return lastElPropertyValue.isLocalEncrypted();
}
@Override
public String getAssocIsEmpty(SpiExpressionRequest request, String path) {
return lastElPropertyValue.getAssocIsEmpty(request, path);
}
public Object[] getAssocIdValues(EntityBean bean) {
// Don't navigate the object graph as bean
// is assumed to be the appropriate type
@@ -1,6 +1,7 @@
package com.avaje.ebeaninternal.server.el;
import com.avaje.ebean.plugin.ExpressionPath;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
/**
* The expression language object that can get values.
@@ -20,6 +21,11 @@ public interface ElPropertyValue extends ElPropertyDeploy, ExpressionPath {
*/
String getAssocIdInExpr(String prefix);
/**
* Return the logical where clause to support "Is empty".
*/
String getAssocIsEmpty(SpiExpressionRequest request, String path);
/**
* Return true if this is an ManyToOne or OneToOne associated bean property.
*/
@@ -28,12 +28,19 @@ public abstract class AbstractExpression implements SpiExpression {
@Override
public void containsMany(BeanDescriptor<?> desc, ManyWhereJoins manyWhereJoin) {
if (propName != null) {
ElPropertyDeploy elProp = desc.getElPropertyDeploy(propName);
propertyContainsMany(propName, desc, manyWhereJoin);
}
/**
* Check the logical property path for containing a 'many' property.
*/
protected void propertyContainsMany(String propertyName, BeanDescriptor<?> desc, ManyWhereJoins manyWhereJoin) {
if (propertyName != null) {
ElPropertyDeploy elProp = desc.getElPropertyDeploy(propertyName);
if (elProp != null) {
if (elProp.containsFormulaWithJoin()) {
// for findRowCount query select clause
manyWhereJoin.addFormulaWithJoin(propName);
manyWhereJoin.addFormulaWithJoin(propertyName);
}
if (elProp.containsMany()) {
// for findRowCount we join to a many property
@@ -348,6 +348,15 @@ public class DefaultExpressionFactory implements SpiExpressionFactory {
return new ExistsQueryExpression((SpiQuery<?>) subQuery, true);
}
@Override
public Expression isEmpty(String propertyName) {
return new IsEmptyExpression(propertyName, true);
}
@Override
public Expression isNotEmpty(String propertyName) {
return new IsEmptyExpression(propertyName, false);
}
/**
* Id Equal to - ID property is equal to the value.
@@ -742,6 +742,18 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
return this;
}
@Override
public ExpressionList<T> isEmpty(String propertyName) {
add(expr.isEmpty(propertyName));
return this;
}
@Override
public ExpressionList<T> isNotEmpty(String propertyName) {
add(expr.isNotEmpty(propertyName));
return this;
}
@Override
public ExpressionList<T> exists(Query<?> subQuery) {
add(expr.exists(subQuery));
@@ -0,0 +1,91 @@
package com.avaje.ebeaninternal.server.expression;
import com.avaje.ebeaninternal.api.HashQueryPlanBuilder;
import com.avaje.ebeaninternal.api.ManyWhereJoins;
import com.avaje.ebeaninternal.api.SpiExpression;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
import com.avaje.ebeaninternal.server.query.SplitName;
import java.io.IOException;
public class IsEmptyExpression extends AbstractExpression {
private final boolean empty;
private final String propertyPath;
public IsEmptyExpression(String propertyName, boolean empty) {
super(propertyName);
this.empty = empty;
this.propertyPath = SplitName.split(propertyName)[0];
}
@Override
public void writeDocQuery(DocQueryContext context) throws IOException {
}
public final String getPropName() {
return propName;
}
@Override
public void addBindValues(SpiExpressionRequest request) {
// no bind values
}
@Override
public void containsMany(BeanDescriptor<?> desc, ManyWhereJoins manyWhereJoin) {
// we don't want the extra join
propertyContainsMany(propertyPath, desc, manyWhereJoin);
}
@Override
public void addSql(SpiExpressionRequest request) {
ElPropertyValue prop = getElProp(request);
if (prop == null) {
throw new IllegalStateException("Property [" + propName + "] not found");
}
if (empty) {
request.append("not ");
}
request
.append("exists (select 1 from ")
.append(prop.getAssocIsEmpty(request, propertyPath))
.append(")");
}
/**
* Based on the type and propertyName.
*/
@Override
public void queryPlanHash(HashQueryPlanBuilder builder) {
builder.add(IsEmptyExpression.class).add(propName);
}
@Override
public int queryBindHash() {
return 1;
}
@Override
public boolean isSameByPlan(SpiExpression other) {
if (!(other instanceof IsEmptyExpression)) {
return false;
}
IsEmptyExpression that = (IsEmptyExpression) other;
return this.propName.equals(that.propName)
&& this.empty == that.empty;
}
@Override
public boolean isSameByBind(SpiExpression other) {
return (other instanceof IsEmptyExpression);
}
}
@@ -40,7 +40,7 @@ import java.util.Set;
*/
class JunctionExpression<T> implements SpiJunction<T>, SpiExpression, ExpressionList<T> {
protected final DefaultExpressionList<T> exprList;
private final DefaultExpressionList<T> exprList;
protected final Junction.Type type;
@@ -547,6 +547,16 @@ class JunctionExpression<T> implements SpiJunction<T>, SpiExpression, Expression
return exprList.notIn(propertyName, subQuery);
}
@Override
public ExpressionList<T> isEmpty(String propertyName) {
return exprList.isEmpty(propertyName);
}
@Override
public ExpressionList<T> isNotEmpty(String propertyName) {
return exprList.isNotEmpty(propertyName);
}
@Override
public ExpressionList<T> exists(Query<?> subQuery) {
return exprList.exists(subQuery);
@@ -2,6 +2,7 @@ package com.avaje.ebeaninternal.server.type;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.text.StringParser;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
@@ -52,6 +53,11 @@ public class CtCompoundPropertyElAdapter implements ElPropertyValue {
throw new RuntimeException("Not Supported or Expected");
}
@Override
public String getAssocIsEmpty(SpiExpressionRequest request, String path) {
throw new RuntimeException("Not Supported or Expected");
}
public Object[] getAssocIdValues(EntityBean bean) {
throw new RuntimeException("Not Supported or Expected");
}