Add Query alsoIf() to conditionally apply changes to a query

This commit is contained in:
Rob Bygrave
2023-08-15 21:56:50 +12:00
parent 1decbe0152
commit ec0c00eb72
6 changed files with 115 additions and 0 deletions
@@ -11,6 +11,7 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;
@@ -671,6 +672,16 @@ public interface Query<T> extends CancelableQuery {
*/
Query<T> apply(FetchPath fetchPath);
/**
* Apply changes to the query conditional on the supplied predicate.
* <p>
* Typically, the changes are extra predicates etc.
*
* @param predicate The predicate which when true the changes are applied
* @param apply The changes to apply to the query
*/
Query<T> alsoIf(BooleanSupplier predicate, Consumer<Query<T>> apply);
/**
* Execute the query using the given transaction.
*/
@@ -37,6 +37,7 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;
@@ -219,6 +220,11 @@ final class DefaultFetchGroupQuery<T> implements SpiFetchGroupQuery<T>, SpiQuery
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");
}
@Override
public Query<T> usingTransaction(Transaction transaction) {
throw new RuntimeException("EB102: Only select() and fetch() clause is allowed on FetchGroup");
@@ -26,6 +26,7 @@ import javax.persistence.PersistenceException;
import java.sql.Connection;
import java.sql.Timestamp;
import java.util.*;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;
@@ -282,6 +283,14 @@ public class DefaultOrmQuery<T> extends AbstractQuery implements SpiQuery<T> {
return this;
}
@Override
public Query<T> alsoIf(BooleanSupplier predicate, Consumer<Query<T>> consumer) {
if (predicate.getAsBoolean()) {
consumer.accept(this);
}
return this;
}
@Override
public final void addSoftDeletePredicate(String softDeletePredicate) {
if (softDeletePredicates == null) {
@@ -16,6 +16,7 @@ import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Timestamp;
import java.util.*;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;
@@ -486,6 +487,21 @@ public abstract class TQRootBean<T, R> {
return root;
}
/**
* Apply changes to the query conditional on the supplied predicate.
* <p>
* Typically, the changes are extra predicates etc.
*
* @param predicate The predicate which when true the changes are applied
* @param apply The changes to apply to the query
*/
public R alsoIf(BooleanSupplier predicate, Consumer<R> apply) {
if (predicate.getAsBoolean()) {
apply.accept(root);
}
return root;
}
/**
* Perform an 'As of' query using history tables to return the object graph
* as of a time in the past.
@@ -0,0 +1,37 @@
package org.querytest;
import org.example.domain.Customer;
import org.example.domain.query.QCustomer;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.example.domain.query.QCustomer.Alias.name;
class QueryAlsoIfTest {
int dummy = 1;
@Test
void apply() {
var q = new QCustomer()
.select(name)
.name.isNotNull()
.alsoIf(() -> dummy == 1, query -> query.status.equalTo(Customer.Status.GOOD))
.query();
q.findList();
assertThat(q.getGeneratedSql()).isEqualTo("select t0.id, t0.name from be_customer t0 where t0.name is not null and t0.status = ?");
}
@Test
void notApply() {
var q = new QCustomer()
.select(name)
.name.isNotNull()
.alsoIf(() -> dummy > 1, query -> query.status.equalTo(Customer.Status.GOOD))
.query();
q.findList();
assertThat(q.getGeneratedSql()).isEqualTo("select t0.id, t0.name from be_customer t0 where t0.name is not null");
}
}
@@ -0,0 +1,36 @@
package org.tests.query;
import io.ebean.DB;
import io.ebean.Query;
import org.junit.jupiter.api.Test;
import org.tests.model.basic.Customer;
import org.tests.model.basic.ResetBasicData;
import static org.assertj.core.api.Assertions.assertThat;
class TestQueryAlsoIf {
int dummy = 1;
@Test
void apply() {
ResetBasicData.reset();
Query<Customer> query = DB.find(Customer.class)
.select("name")
.alsoIf(() -> dummy == 1, 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 notApply() {
ResetBasicData.reset();
Query<Customer> query = DB.find(Customer.class)
.select("name")
.alsoIf(() -> dummy > 1, qy -> qy.where().isNotNull("name"));
query.findList();
assertThat(query.getGeneratedSql()).isEqualTo("select t0.id, t0.name from o_customer t0");
}
}