Add Query alsoIfPresent() as helper for Nullable conditional expressions (#3756)

Add this as a helper for when using alsoIf(BooleanPredicate) seems overkill
and there isn't a built in IfPresent option for the desired expression and
wanting to keep the fluid style.
This commit is contained in:
Rob Bygrave
2026-04-28 23:24:50 +12:00
committed by GitHub
parent 292dcf4b0e
commit 7e4a2db9aa
6 changed files with 83 additions and 2 deletions
@@ -36,7 +36,7 @@ class QueryAlsoIfTest {
.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
@@ -48,7 +48,33 @@ class QueryAlsoIfTest {
.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