Merge pull request #3472 from ebean-orm/feature/3461-take-2

#3461 - Add also() for query beans
This commit is contained in:
Rob Bygrave
2024-09-09 22:18:40 +12:00
committed by GitHub
5 changed files with 36 additions and 0 deletions
@@ -27,6 +27,13 @@ public interface QueryBuilder<SELF, T> extends QueryBuilderProjection<SELF, T> {
*/
SELF alias(String alias);
/**
* Apply changes to the query using a function.
*
* @param apply Function that applies changes to the query.
*/
SELF also(Consumer<SELF> apply);
/**
* Apply changes to the query conditional on the supplied predicate.
* <p>
@@ -230,6 +230,11 @@ final class DefaultFetchGroupQuery<T> implements SpiFetchGroupQuery<T>, SpiQuery
throw new RuntimeException("EB102: Only select() and fetch() clause is allowed on FetchGroup");
}
@Override
public Query<T> also(Consumer<Query<T>> apply) {
throw new RuntimeException("EB102: Only select() and fetch() clause is allowed on FetchGroup");
}
@Override
public Query<T> alsoIf(BooleanSupplier predicate, Consumer<Query<T>> apply) {
throw new RuntimeException("EB102: Only select() and fetch() clause is allowed on FetchGroup");
@@ -293,6 +293,12 @@ public class DefaultOrmQuery<T> extends AbstractQuery implements SpiQuery<T> {
return this;
}
@Override
public Query<T> also(Consumer<Query<T>> apply) {
apply.accept(this);
return this;
}
@Override
public Query<T> alsoIf(BooleanSupplier predicate, Consumer<Query<T>> consumer) {
if (predicate.getAsBoolean()) {
@@ -275,6 +275,12 @@ public abstract class QueryBean<T, R> implements IQueryBean<T, R> {
return root;
}
@Override
public final R also(Consumer<R> apply) {
apply.accept(root);
return root;
}
@Override
public final R alsoIf(BooleanSupplier predicate, Consumer<R> apply) {
if (predicate.getAsBoolean()) {
@@ -11,6 +11,18 @@ class QueryAlsoIfTest {
int dummy = 1;
@Test
void also() {
var q = new QCustomer()
.select(name)
.name.isNotNull()
.also(query -> query.email.isNotNull())
.query();
q.findList();
assertThat(q.getGeneratedSql()).isEqualTo("select /* QueryAlsoIfTest.also */ t0.id, t0.name from be_customer t0 where t0.name is not null and t0.email is not null");
}
@Test
void apply() {
var q = new QCustomer()