Skip to content
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
10 changes: 10 additions & 0 deletions ebean-api/src/main/java/io/ebean/QueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ public interface QueryBuilder<SELF extends QueryBuilder<SELF, T>, T> extends Que
*/
SELF alsoIf(BooleanSupplier predicate, Consumer<SELF> apply);

/**
* Apply changes to the query when the supplied value is non-null.
* <p>
* Typically, the changes are extra predicates etc.
*
* @param value The value which when non-null the changes are applied
* @param apply The changes to apply to the query
*/
SELF alsoIfPresent(@Nullable Object value, Consumer<SELF> apply);

/**
* Perform an 'As of' query using history tables to return the object graph
* as of a time in the past.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ public Query<T> alsoIf(BooleanSupplier predicate, Consumer<Query<T>> apply) {
throw new RuntimeException("EB102: Only select() and fetch() clause is allowed on FetchGroup");
}

@Override
public Query<T> alsoIfPresent(@Nullable Object value, Consumer<Query<T>> apply) {
throw new RuntimeException("EB102: Only select() and fetch() clause is allowed on FetchGroup");
}

@Override
public Query<T> usingTransaction(Transaction transaction) {
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 @@ -307,6 +307,14 @@ public Query<T> alsoIf(BooleanSupplier predicate, Consumer<Query<T>> consumer) {
return this;
}

@Override
public Query<T> alsoIfPresent(@Nullable Object value, Consumer<Query<T>> consumer) {
if (value != null) {
consumer.accept(this);
}
return this;
}

@Override
public final void addSoftDeletePredicate(String softDeletePredicate) {
if (softDeletePredicates == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ public final R alsoIf(BooleanSupplier predicate, Consumer<R> apply) {
return root;
}

@Override
public final R alsoIfPresent(@Nullable Object value, Consumer<R> apply) {
if (value != null) {
apply.accept(root);
}
return root;
}

@Override
public final R asOf(Timestamp asOf) {
query.asOf(asOf);
Expand Down
30 changes: 28 additions & 2 deletions ebean-querybean/src/test/java/org/querytest/QueryAlsoIfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void apply() {
.query();

q.findList();
assertThat(q.getGeneratedSql()).isEqualTo("select /* QueryAlsoIfTest.apply */ t0.id, t0.name from be_customer t0 where t0.name is not null and t0.status = ?");
assertThat(q.getGeneratedSql()).contains("from be_customer t0 where t0.name is not null and t0.status = ?");
}

@Test
Expand All @@ -48,7 +48,33 @@ void notApply() {
.query();

q.findList();
assertThat(q.getGeneratedSql()).isEqualTo("select /* QueryAlsoIfTest.notApply */ t0.id, t0.name from be_customer t0 where t0.name is not null");
assertThat(q.getGeneratedSql()).contains("from be_customer t0 where t0.name is not null");
}

@Test
void applyIfPresent() {
Object value = "yes";
var q = new QCustomer()
.select(name)
.name.isNotNull()
.alsoIfPresent(value, query -> query.status.equalTo(Customer.Status.GOOD))
.query();

q.findList();
assertThat(q.getGeneratedSql()).isEqualTo("select /* QueryAlsoIfTest.applyIfPresent */ t0.id, t0.name from be_customer t0 where t0.name is not null and t0.status = ?");
}

@Test
void notApplyIfPresent() {
Object value = null;
var q = new QCustomer()
.select(name)
.name.isNotNull()
.alsoIfPresent(value, query -> query.status.equalTo(Customer.Status.GOOD))
.query();

q.findList();
assertThat(q.getGeneratedSql()).isEqualTo("select /* QueryAlsoIfTest.notApplyIfPresent */ t0.id, t0.name from be_customer t0 where t0.name is not null");
}

@Test
Expand Down
24 changes: 24 additions & 0 deletions ebean-test/src/test/java/org/tests/query/TestQueryAlsoIf.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,28 @@ void notApply() {
query.findList();
assertThat(query.getGeneratedSql()).isEqualTo("select t0.id, t0.name from o_customer t0");
}

@Test
void applyIfPresent() {
ResetBasicData.reset();
Object value = "yes";
Query<Customer> query = DB.find(Customer.class)
.select("name")
.alsoIfPresent(value, qy -> qy.where().isNotNull("name"));

query.findList();
assertThat(query.getGeneratedSql()).isEqualTo("select t0.id, t0.name from o_customer t0 where t0.name is not null");
}

@Test
void notApplyIfPresent() {
ResetBasicData.reset();
Object value = null;
Query<Customer> query = DB.find(Customer.class)
.select("name")
.alsoIfPresent(value, qy -> qy.where().isNotNull("name"));

query.findList();
assertThat(query.getGeneratedSql()).isEqualTo("select t0.id, t0.name from o_customer t0");
}
}
Loading