Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3461 - Add also() for query beans #3472

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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