Skip to content

Commit

Permalink
#3461 - Add also() for query beans that takes QueryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Aug 27, 2024
1 parent f896470 commit 100d53f
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 25 deletions.
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 @@ -20,13 +20,23 @@
* @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> {

/**
* Set root table alias.
*/
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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @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> {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,7 @@

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;
Expand Down Expand Up @@ -230,6 +207,11 @@ public Query<T> apply(FetchPath fetchPath) {
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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ public final Query<T> apply(FetchPath fetchPath) {
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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
* @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> {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
* @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> {

/**
Expand Down Expand Up @@ -275,6 +276,13 @@ public final R apply(FetchPath pathProperties) {
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()) {
Expand Down
29 changes: 29 additions & 0 deletions ebean-querybean/src/test/java/org/querytest/QueryAlsoIfTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,45 @@
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;

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()
Expand Down
35 changes: 34 additions & 1 deletion ebean-test/src/test/java/org/tests/query/TestQueryAlsoIf.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,49 @@

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();
Expand Down

0 comments on commit 100d53f

Please sign in to comment.