mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-09-20 19:17:55 +00:00
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3b627a3cef | ||
|
|
0af43db678 | ||
|
|
100d53fded |
@@ -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()
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.test.LoggedSql;
|
||||
import io.ebean.xtest.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.FutureIds;
|
||||
@@ -28,8 +29,13 @@ public class TestFetchId extends BaseTestCase {
|
||||
.gt("details.id", 0)
|
||||
.query();
|
||||
|
||||
LoggedSql.start();
|
||||
List<Object> ids = query.findIds();
|
||||
assertThat(ids).isNotEmpty();
|
||||
List<String> sql = LoggedSql.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
assertThat(sql.get(0)).doesNotContain("order by");
|
||||
assertThat(sql.get(0)).doesNotContain("join o_order_detail t1");
|
||||
|
||||
FutureIds<Order> futureIds = query.findFutureIds();
|
||||
|
||||
|
||||
@@ -230,5 +230,6 @@ public class TestQueryJoinToAssocOne extends BaseTestCase {
|
||||
String originQuery = trimSql(loggedSql.get(0), 5);
|
||||
assertThat(originQuery).contains("select t0.id, t0.status, t0.ship_date, t1.id, t1.order_qty, t1.unit_price");
|
||||
assertThat(originQuery).contains(" from o_order t0 left join o_order_detail t1 ");
|
||||
assertThat(originQuery).contains(" order by t0.id, t1.id asc, t1.order_qty asc, t1.cretime desc;");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,7 +185,7 @@ public class TestOrderedList extends BaseTestCase {
|
||||
DB.save(masterDb);
|
||||
|
||||
masterDb = DB.find(OmCacheOrderedMaster.class, master.getId());
|
||||
assertThat(masterDb.getDetails()).containsExactly(detail3, detail1);
|
||||
assertThat(masterDb.getDetails()).containsExactlyInAnyOrder(detail3, detail1);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,5 +181,6 @@ public class TestManyWhereJoin extends BaseTestCase {
|
||||
|
||||
// additional join for fetching the many details
|
||||
assertThat(sql).contains(" left join o_order_detail t1 on t1.order_id = t0.id");
|
||||
assertThat(sql).contains("order by t0.cretime, t0.id, t1.id asc, t1.order_qty asc, t1.cretime desc");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -213,6 +213,7 @@ public class TestQueryFilterMany extends BaseTestCase {
|
||||
assertThat(sql).hasSize(1);
|
||||
assertThat(sql.get(0)).contains("from o_customer t0 left join o_order t1");
|
||||
assertThat(sql.get(0)).contains("where t1.order_date is not null");
|
||||
assertThat(sql.get(0)).contains("order by t0.id");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -269,6 +270,7 @@ public class TestQueryFilterMany extends BaseTestCase {
|
||||
List<String> sqlList = LoggedSql.stop();
|
||||
assertEquals(1, sqlList.size());
|
||||
assertThat(sqlList.get(0)).contains("select count(*) from o_customer");
|
||||
assertThat(sqlList.get(0)).doesNotContain("order by");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,17 +1,27 @@
|
||||
package org.tests.query;
|
||||
|
||||
import io.ebean.test.LoggedSql;
|
||||
import io.ebean.xtest.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.MUser;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestQueryFilterManyOnM2M extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
LoggedSql.start();
|
||||
DB.find(MUser.class).fetch("roles").filterMany("roles").ilike("roleName", "Jim%").findList();
|
||||
|
||||
List<String> sql = LoggedSql.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
assertThat(sql.get(0)).contains("select t0.userid, t0.user_name, t0.user_type_id, t1.roleid, t1.role_name from muser t0 left join mrole_muser t1z_ on t1z_.muser_userid = t0.userid left join mrole t1 on t1.roleid = t1z_.mrole_roleid where lower(t1.role_name) like");
|
||||
assertThat(sql.get(0)).contains("order by t0.userid;");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.tests.query;
|
||||
|
||||
import io.ebean.test.LoggedSql;
|
||||
import io.ebean.xtest.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -10,6 +11,8 @@ import org.tests.model.basic.ResetBasicData;
|
||||
import java.sql.Date;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestQueryFilterManySimple extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
@@ -20,6 +23,7 @@ public class TestQueryFilterManySimple extends BaseTestCase {
|
||||
// not really last week :)
|
||||
Date lastWeek = Date.valueOf("2010-01-01");
|
||||
|
||||
LoggedSql.start();
|
||||
List<Customer> list = DB.find(Customer.class)
|
||||
// .join("orders", new JoinConfig().lazy())
|
||||
// .join("orders", new JoinConfig().query())
|
||||
@@ -29,5 +33,14 @@ public class TestQueryFilterManySimple extends BaseTestCase {
|
||||
|
||||
// invoke lazy loading
|
||||
list.get(0).getOrders().size();
|
||||
List<String> sql = LoggedSql.stop();
|
||||
assertThat(sql).hasSize(2);
|
||||
assertThat(sql.get(0)).contains("from o_customer t0 left join o_order t1 on t1.kcustomer_id = t0.id and t1.order_date is not null left join o_customer t2 on t2.id = t1.kcustomer_id where");
|
||||
assertThat(sql.get(0)).contains("order by t0.id");
|
||||
if (isPostgresCompatible()) {
|
||||
assertThat(sql.get(1)).contains("from contact t0 where (t0.customer_id) = any(?) and t0.first_name is not null;");
|
||||
} else {
|
||||
assertThat(sql.get(1)).contains("from contact t0 where (t0.customer_id) in (?) and t0.first_name is not null;");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.tests.query;
|
||||
|
||||
import io.ebean.test.LoggedSql;
|
||||
import io.ebean.xtest.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
@@ -19,6 +20,7 @@ public class TestRowCount extends BaseTestCase {
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
LoggedSql.start();
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.fetch("details")
|
||||
@@ -32,6 +34,13 @@ public class TestRowCount extends BaseTestCase {
|
||||
List<Object> ids = query.findIds();
|
||||
|
||||
List<Order> list = query.findList();
|
||||
|
||||
List<String> sql = LoggedSql.stop();
|
||||
assertThat(sql).hasSize(3);
|
||||
assertThat(sql.get(0)).doesNotContain("order by");
|
||||
assertThat(sql.get(1)).contains("order by");
|
||||
assertThat(sql.get(2)).contains("order by t0.id desc");
|
||||
|
||||
System.out.println(list);
|
||||
for (Order order : list) {
|
||||
order.getStatus();
|
||||
|
||||
Reference in New Issue
Block a user