Skip to content

Commit

Permalink
Merge pull request #3472 from ebean-orm/feature/3461-take-2
Browse files Browse the repository at this point in the history
#3461 - Add also() for query beans
  • Loading branch information
rbygrave authored Sep 9, 2024
2 parents 416f50e + 33b2c39 commit cbac00a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ebean-api/src/main/java/io/ebean/QueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ public Query<T> apply(FetchPath fetchPath) {
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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ public final Query<T> apply(FetchPath fetchPath) {
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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@ public final R apply(FetchPath pathProperties) {
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()) {
Expand Down
12 changes: 12 additions & 0 deletions ebean-querybean/src/test/java/org/querytest/QueryAlsoIfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit cbac00a

Please sign in to comment.