Skip to content

Commit cbac00a

Browse files
authored
Merge pull request #3472 from ebean-orm/feature/3461-take-2
#3461 - Add also() for query beans
2 parents 416f50e + 33b2c39 commit cbac00a

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed

ebean-api/src/main/java/io/ebean/QueryBuilder.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ public interface QueryBuilder<SELF, T> extends QueryBuilderProjection<SELF, T> {
2727
*/
2828
SELF alias(String alias);
2929

30+
/**
31+
* Apply changes to the query using a function.
32+
*
33+
* @param apply Function that applies changes to the query.
34+
*/
35+
SELF also(Consumer<SELF> apply);
36+
3037
/**
3138
* Apply changes to the query conditional on the supplied predicate.
3239
* <p>

ebean-core/src/main/java/io/ebeaninternal/server/query/DefaultFetchGroupQuery.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ public Query<T> apply(FetchPath fetchPath) {
230230
throw new RuntimeException("EB102: Only select() and fetch() clause is allowed on FetchGroup");
231231
}
232232

233+
@Override
234+
public Query<T> also(Consumer<Query<T>> apply) {
235+
throw new RuntimeException("EB102: Only select() and fetch() clause is allowed on FetchGroup");
236+
}
237+
233238
@Override
234239
public Query<T> alsoIf(BooleanSupplier predicate, Consumer<Query<T>> apply) {
235240
throw new RuntimeException("EB102: Only select() and fetch() clause is allowed on FetchGroup");

ebean-core/src/main/java/io/ebeaninternal/server/querydefn/DefaultOrmQuery.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,12 @@ public final Query<T> apply(FetchPath fetchPath) {
293293
return this;
294294
}
295295

296+
@Override
297+
public Query<T> also(Consumer<Query<T>> apply) {
298+
apply.accept(this);
299+
return this;
300+
}
301+
296302
@Override
297303
public Query<T> alsoIf(BooleanSupplier predicate, Consumer<Query<T>> consumer) {
298304
if (predicate.getAsBoolean()) {

ebean-querybean/src/main/java/io/ebean/typequery/QueryBean.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,12 @@ public final R apply(FetchPath pathProperties) {
275275
return root;
276276
}
277277

278+
@Override
279+
public final R also(Consumer<R> apply) {
280+
apply.accept(root);
281+
return root;
282+
}
283+
278284
@Override
279285
public final R alsoIf(BooleanSupplier predicate, Consumer<R> apply) {
280286
if (predicate.getAsBoolean()) {

ebean-querybean/src/test/java/org/querytest/QueryAlsoIfTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ class QueryAlsoIfTest {
1111

1212
int dummy = 1;
1313

14+
@Test
15+
void also() {
16+
var q = new QCustomer()
17+
.select(name)
18+
.name.isNotNull()
19+
.also(query -> query.email.isNotNull())
20+
.query();
21+
22+
q.findList();
23+
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");
24+
}
25+
1426
@Test
1527
void apply() {
1628
var q = new QCustomer()

0 commit comments

Comments
 (0)