Skip to content

Commit

Permalink
#3455 - Improve query beans such that filterMany() expressions are on…
Browse files Browse the repository at this point in the history
…ly on ToMany relationships

Use protected helper methods on TQAssocBean for the filterMany() methods generated onto AssocMany beans.
  • Loading branch information
rbygrave committed Sep 9, 2024
1 parent 4d79815 commit a2e66bc
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 80 deletions.
50 changes: 34 additions & 16 deletions ebean-querybean/src/main/java/io/ebean/typequery/TQAssocBean.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.ebean.typequery;

import io.ebean.Expr;
import io.ebean.ExpressionList;
import io.ebean.FetchConfig;
import io.ebean.FetchGroup;
Expand All @@ -18,7 +19,7 @@
* @param <QB> the query bean type
*/
@SuppressWarnings("rawtypes")
public abstract class TQAssocBean<T, R, QB> extends TQAssoc<T, R> implements TQBase<R> {
public abstract class TQAssocBean<T, R, QB> extends TQAssoc<T, R> {

private static final FetchConfig FETCH_DEFAULT = FetchConfig.ofDefault();
private static final FetchConfig FETCH_QUERY = FetchConfig.ofQuery();
Expand All @@ -42,21 +43,6 @@ public TQAssocBean(String name, R root, String prefix) {
super(name, root, prefix);
}

@Override
public ExpressionList _expr() {
return expr();
}

@Override
public String _name() {
return _name;
}

@Override
public R _root() {
return _root;
}

/**
* Eagerly fetch this association fetching all the properties.
*/
Expand Down Expand Up @@ -203,4 +189,36 @@ private Set<String> properties(TQProperty<?, ?>... props) {
return set;
}

protected final R _filterMany(ExpressionList<T> filter) {
@SuppressWarnings("unchecked")
ExpressionList<T> expressionList = (ExpressionList<T>) expr().filterMany(_name);
expressionList.addAll(filter);
return _root;
}

@Deprecated(forRemoval = true)
protected final R _filterMany(String expressions, Object... params) {
expr().filterMany(_name, expressions, params);
return _root;
}

protected final R _filterManyRaw(String rawExpressions, Object... params) {
expr().filterManyRaw(_name, rawExpressions, params);
return _root;
}

protected final R _isEmpty() {
expr().isEmpty(_name);
return _root;
}

protected final R _isNotEmpty() {
expr().isNotEmpty(_name);
return _root;
}

protected final <S> ExpressionList<S> _newExpressionList() {
return Expr.factory().expressionList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@
* @param <T> The entity bean type
* @param <R> The root query bean type
*/
public interface TQMany<T, R> extends TQBase<R> {
public interface TQAssocMany<T, R, QB> {

/**
* Apply a filter when fetching these beans.
* Filter the beans fetched for this relationship.
*
* @param filter The filter to apply
*/
default R filterMany(ExpressionList<T> filter) {
@SuppressWarnings("unchecked")
ExpressionList<T> expressionList = (ExpressionList<T>) _expr().filterMany(_name());
expressionList.addAll(filter);
return _root();
}
R filterMany(java.util.function.Consumer<QB> filter);

/**
* Filter the beans fetched for this relationship.
*/
R filterMany(ExpressionList<T> filter);

/**
* @param expressions The expressions including and, or, not etc with ? and ?1 bind params.
* @param params The bind parameter values
* @deprecated for removal - migrate to {@link #filterManyRaw(String, Object...)}.
* <p>
* Apply a filter when fetching these beans.
Expand All @@ -46,15 +50,9 @@ default R filterMany(ExpressionList<T> filter) {
* .findList();
*
* }</pre>
*
* @param expressions The expressions including and, or, not etc with ? and ?1 bind params.
* @param params The bind parameter values
*/
@Deprecated(forRemoval = true)
default R filterMany(String expressions, Object... params) {
_expr().filterMany(_name(), expressions, params);
return _root();
}
R filterMany(String expressions, Object... params);

/**
* Add filter expressions for the many path. The expressions can include SQL functions if
Expand All @@ -72,38 +70,25 @@ default R filterMany(String expressions, Object... params) {
* }</pre>
*
* @param rawExpressions The raw expressions which can include ? and ?1 style bind parameter placeholders
* @param params The parameter values to bind
* @param params The parameter values to bind
*/
default R filterManyRaw(String rawExpressions, Object... params) {
_expr().filterManyRaw(_name(), rawExpressions, params);
return _root();
}
R filterManyRaw(String rawExpressions, Object... params);

/**
* Is empty for a collection property.
* <p>
* This effectively adds a not exists sub-query on the collection property.
* </p>
* <p>
* This expression only works on OneToMany and ManyToMany properties.
* </p>
*/
default R isEmpty() {
_expr().isEmpty(_name());
return _root();
}
R isEmpty();

/**
* Is not empty for a collection property.
* <p>
* This effectively adds an exists sub-query on the collection property.
* </p>
* <p>
* This expression only works on OneToMany and ManyToMany properties.
* </p>
*/
default R isNotEmpty() {
_expr().isNotEmpty(_name());
return _root();
}
R isNotEmpty();
}
25 changes: 0 additions & 25 deletions ebean-querybean/src/main/java/io/ebean/typequery/TQBase.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -300,22 +300,37 @@ private void writeAssocMany() {
writer.append(" /** Associated ToMany query bean */").eol();
writer.append(" ").append(Constants.AT_GENERATED).eol();
writer.append(" ").append(Constants.AT_TYPEQUERYBEAN).eol();
writer.append(" public static final class AssocMany<R> extends Assoc<R> implements io.ebean.typequery.TQMany<%s, R>{", beanFullName).eol();
writer.append(" public static final class AssocMany<R> extends Assoc<R> implements io.ebean.typequery.TQAssocMany<%s, R, Q%s>{", beanFullName, shortInnerName).eol();
writeAssocBeanConstructor("public AssocMany");
writeAssocFilterMany();
writer.append(" }").eol();
}

private void writeAssocFilterMany() {
writer.eol();
writer.append(" @SuppressWarnings({\"unchecked\", \"rawtypes\"})").eol();
writer.append(" @Override").eol();
writer.append(" public R filterMany(java.util.function.Consumer<Q%s> apply) {", shortName).eol();
writer.append(" final io.ebean.ExpressionList list = io.ebean.Expr.factory().expressionList();", shortName).eol();
writer.append(" final var qb = new Q%s(list);", shortName).eol();
writer.append(" apply.accept(qb);").eol();
writer.append(" expr().filterMany(_name).addAll(list);").eol();
writer.append(" return _root;").eol();
writer.append(" final io.ebean.ExpressionList<%s> list = _newExpressionList();", beanFullName).eol();
writer.append(" apply.accept(new Q%s(list));", shortName).eol();
writer.append(" return _filterMany(list);").eol();
writer.append(" }").eol();
writer.eol();
writer.append(" @Override").eol();
writer.append(" public R filterMany(io.ebean.ExpressionList<%s> filter) { return _filterMany(filter); }", beanFullName).eol();
writer.eol();
writer.append(" @Override").eol();
writer.append(" public R filterManyRaw(String rawExpressions, Object... params) { return _filterManyRaw(rawExpressions, params); }").eol();
writer.eol();
writer.append(" @Override").eol();
writer.append(" @Deprecated(forRemoval = true)").eol();
writer.append(" public R filterMany(String expressions, Object... params) { return _filterMany(expressions, params); }").eol();
writer.eol();
writer.append(" @Override").eol();
writer.append(" public R isEmpty() { return _isEmpty(); }").eol();
writer.eol();
writer.append(" @Override").eol();
writer.append(" public R isNotEmpty() { return _isNotEmpty(); }").eol();

}

private void writeAssocBeanConstructor(String prefix) {
Expand Down

0 comments on commit a2e66bc

Please sign in to comment.