Compare commits

...
Author SHA1 Message Date
Rob Bygrave 100d53fded #3461 - Add also() for query beans that takes QueryBuilder 2024-08-27 23:10:38 +12:00
8 changed files with 95 additions and 25 deletions
@@ -20,6 +20,7 @@ import java.util.stream.Stream;
* @param <SELF> The type of the builder
* @param <T> The entity bean type
*/
//public interface QueryBuilder<SELF extends QueryBuilder<SELF, T>, T> extends QueryBuilderProjection<SELF, T> {
public interface QueryBuilder<SELF, T> extends QueryBuilderProjection<SELF, T> {
/**
@@ -27,6 +28,15 @@ public interface QueryBuilder<SELF, T> extends QueryBuilderProjection<SELF, T> {
*/
SELF alias(String alias);
/**
* Apply changes to the query using a function.
* <p>
* This can be used to apply generic features to queries.
*
* @param apply The changes to apply to the query
*/
SELF also(Consumer<QueryBuilder<?, ?>> apply);
/**
* Apply changes to the query conditional on the supplied predicate.
* <p>
@@ -6,6 +6,7 @@ package io.ebean;
* @param <SELF> The builder type
* @param <T> The entity bean type
*/
//public interface QueryBuilderProjection<SELF extends QueryBuilderProjection<SELF, T>, T> {
public interface QueryBuilderProjection<SELF, T> {
/**
@@ -2,30 +2,7 @@ package io.ebeaninternal.server.query;
import io.avaje.lang.NonNullApi;
import io.avaje.lang.Nullable;
import io.ebean.CacheMode;
import io.ebean.CountDistinctOrder;
import io.ebean.Database;
import io.ebean.DtoQuery;
import io.ebean.Expression;
import io.ebean.ExpressionFactory;
import io.ebean.ExpressionList;
import io.ebean.FetchConfig;
import io.ebean.FetchGroup;
import io.ebean.FetchPath;
import io.ebean.FutureIds;
import io.ebean.FutureList;
import io.ebean.FutureRowCount;
import io.ebean.OrderBy;
import io.ebean.PagedList;
import io.ebean.PersistenceContextScope;
import io.ebean.ProfileLocation;
import io.ebean.Query;
import io.ebean.QueryIterator;
import io.ebean.QueryType;
import io.ebean.RawSql;
import io.ebean.Transaction;
import io.ebean.UpdateQuery;
import io.ebean.Version;
import io.ebean.*;
import io.ebean.service.SpiFetchGroupQuery;
import io.ebeaninternal.api.SpiQueryFetch;
import io.ebeaninternal.server.querydefn.OrmQueryDetail;
@@ -230,6 +207,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> also(Consumer<QueryBuilder<?, ?>> 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");
@@ -293,6 +293,12 @@ public class DefaultOrmQuery<T> extends AbstractQuery implements SpiQuery<T> {
return this;
}
@Override
public Query<T> also(Consumer<QueryBuilder<?, ?>> apply) {
apply.accept(this);
return this;
}
@Override
public Query<T> alsoIf(BooleanSupplier predicate, Consumer<Query<T>> consumer) {
if (predicate.getAsBoolean()) {
@@ -44,6 +44,7 @@ import java.util.Collection;
* @param <T> the entity bean type (normal entity bean type e.g. Customer)
* @param <R> the specific query bean type (e.g. QCustomer)
*/
//public interface IQueryBean<T, R extends IQueryBean<T, R>> extends QueryBuilder<R, T> {
public interface IQueryBean<T, R> extends QueryBuilder<R, T> {
/**
@@ -57,6 +57,7 @@ import java.util.stream.Stream;
* @param <R> the specific root query bean type (e.g. QCustomer)
*/
@NonNullApi
//public abstract class QueryBean<T, R extends QueryBean<T, R>> implements IQueryBean<T, R> {
public abstract class QueryBean<T, R> implements IQueryBean<T, R> {
/**
@@ -275,6 +276,13 @@ public abstract class QueryBean<T, R> implements IQueryBean<T, R> {
return root;
}
@Override
public R also(Consumer<QueryBuilder<?, ?>> apply) {
apply.accept(this);
return root;
}
@Override
public final R alsoIf(BooleanSupplier predicate, Consumer<R> apply) {
if (predicate.getAsBoolean()) {
@@ -1,9 +1,12 @@
package org.querytest;
import io.ebean.QueryBuilder;
import org.example.domain.Customer;
import org.example.domain.query.QCustomer;
import org.junit.jupiter.api.Test;
import java.util.function.Consumer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.example.domain.query.QCustomer.Alias.name;
@@ -11,6 +14,32 @@ class QueryAlsoIfTest {
int dummy = 1;
@Test
void also() {
var myPager = new MyPager(10);
var q = new QCustomer()
.select(name)
.also(myPager)
.query();
q.findList();
assertThat(q.getGeneratedSql()).contains("select /* QueryAlsoIfTest.also */ t0.id, t0.name from be_customer t0 limit 10");
}
static class MyPager implements Consumer<QueryBuilder<?,?>> {
final int maxRows;
MyPager(int maxRows) {
this.maxRows = maxRows;
}
@Override
public void accept(QueryBuilder<?, ?> queryBuilder) {
queryBuilder.setMaxRows(maxRows);
}
}
@Test
void apply() {
var q = new QCustomer()
@@ -2,16 +2,49 @@ package org.tests.query;
import io.ebean.DB;
import io.ebean.Query;
import io.ebean.QueryBuilder;
import io.ebean.xtest.BaseTestCase;
import org.junit.jupiter.api.Test;
import org.tests.model.basic.Customer;
import org.tests.model.basic.ResetBasicData;
import java.util.function.Consumer;
import static org.assertj.core.api.Assertions.assertThat;
class TestQueryAlsoIf {
class TestQueryAlsoIf extends BaseTestCase {
int dummy = 1;
@Test
void also() {
ResetBasicData.reset();
MyPager myPager = new MyPager(10);
Query<Customer> query = DB.find(Customer.class)
.select("name")
.also(myPager);
query.findList();
if (isLimitOffset()) {
assertThat(query.getGeneratedSql()).isEqualTo("select t0.id, t0.name from o_customer t0 limit 10");
}
}
static class MyPager implements Consumer<QueryBuilder<?, ?>> {
final int maxRows;
MyPager(int maxRows) {
this.maxRows = maxRows;
}
@Override
public void accept(QueryBuilder<?, ?> queryBuilder) {
queryBuilder.setMaxRows(maxRows);
}
}
@Test
void apply() {
ResetBasicData.reset();