From 3ddd7099b51bd15fb460dff171bd81b8d10a0d6b Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Fri, 18 Mar 2016 13:06:10 +1300 Subject: [PATCH] #605 - Add and() and or() ... as synonyms for conjunction() and disjunction() ... to the "Query Criteria API" --- .../java/com/avaje/ebean/ExpressionList.java | 12 +++++++++ .../expression/DefaultExpressionList.java | 10 ++++++++ .../server/expression/JunctionExpression.java | 10 ++++++++ .../query/TestExprNestedDisjunction.java | 22 ++++++++++++++++ ...TestImplicitJoinOnParentRelationship.java} | 25 ++++++++++++------- 5 files changed, 70 insertions(+), 9 deletions(-) rename src/test/java/com/avaje/tests/query/{TestImplicitJoinOnParentRelatonship.java => TestImplicitJoinOnParentRelationship.java} (71%) diff --git a/src/main/java/com/avaje/ebean/ExpressionList.java b/src/main/java/com/avaje/ebean/ExpressionList.java index 894b228ce..22491210e 100644 --- a/src/main/java/com/avaje/ebean/ExpressionList.java +++ b/src/main/java/com/avaje/ebean/ExpressionList.java @@ -888,6 +888,18 @@ public interface ExpressionList { */ ExpressionList not(Expression exp); + /** + * Return a list of expressions that will be joined by AND's. + * This is exactly the same as conjunction(); + */ + Junction and(); + + /** + * Return a list of expressions that will be joined by OR's. + * This is exactly the same as disjunction(); + */ + Junction or(); + /** * Return a list of expressions that will be joined by AND's. */ diff --git a/src/main/java/com/avaje/ebeaninternal/server/expression/DefaultExpressionList.java b/src/main/java/com/avaje/ebeaninternal/server/expression/DefaultExpressionList.java index 00d62e2c8..d0dfb166d 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/expression/DefaultExpressionList.java +++ b/src/main/java/com/avaje/ebeaninternal/server/expression/DefaultExpressionList.java @@ -652,6 +652,16 @@ public class DefaultExpressionList implements SpiExpressionList { return junction; } + @Override + public Junction and() { + return conjunction(); + } + + @Override + public Junction or() { + return disjunction(); + } + @Override public Junction conjunction() { Junction conjunction = expr.conjunction(query, this); diff --git a/src/main/java/com/avaje/ebeaninternal/server/expression/JunctionExpression.java b/src/main/java/com/avaje/ebeaninternal/server/expression/JunctionExpression.java index 7519b25c0..64708122c 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/expression/JunctionExpression.java +++ b/src/main/java/com/avaje/ebeaninternal/server/expression/JunctionExpression.java @@ -320,6 +320,16 @@ abstract class JunctionExpression implements SpiJunction, SpiExpression, E return exprList.betweenProperties(lowProperty, highProperty, value); } + @Override + public Junction and() { + return conjunction(); + } + + @Override + public Junction or() { + return disjunction(); + } + @Override public Junction conjunction() { return exprList.conjunction(); diff --git a/src/test/java/com/avaje/tests/query/TestExprNestedDisjunction.java b/src/test/java/com/avaje/tests/query/TestExprNestedDisjunction.java index c9e0cc733..3e2bbb697 100644 --- a/src/test/java/com/avaje/tests/query/TestExprNestedDisjunction.java +++ b/src/test/java/com/avaje/tests/query/TestExprNestedDisjunction.java @@ -30,4 +30,26 @@ public class TestExprNestedDisjunction extends BaseTestCase { Assert.assertTrue(s.contains(" and t0.anniversary = ? ) or (t0.status = ? and t0.id > ? )")); } + + @Test + public void test_nested_and() { + + ResetBasicData.reset(); + + java.sql.Date onAfter = java.sql.Date.valueOf("2009-08-31"); + + Query q = Ebean.find(Customer.class) + .where() + .or() + .and() + .startsWith("name", "r").eq("anniversary", onAfter).endJunction() + .and().eq("status", Customer.Status.ACTIVE).gt("id", 0).endJunction().orderBy() + .asc("name"); + + q.findList(); + String s = q.getGeneratedSql(); + + Assert.assertTrue(s.contains("(t0.name like ? ")); + Assert.assertTrue(s.contains(" and t0.anniversary = ? ) or (t0.status = ? and t0.id > ? )")); + } } diff --git a/src/test/java/com/avaje/tests/query/TestImplicitJoinOnParentRelatonship.java b/src/test/java/com/avaje/tests/query/TestImplicitJoinOnParentRelationship.java similarity index 71% rename from src/test/java/com/avaje/tests/query/TestImplicitJoinOnParentRelatonship.java rename to src/test/java/com/avaje/tests/query/TestImplicitJoinOnParentRelationship.java index 735cf8fec..07dcff8d0 100644 --- a/src/test/java/com/avaje/tests/query/TestImplicitJoinOnParentRelatonship.java +++ b/src/test/java/com/avaje/tests/query/TestImplicitJoinOnParentRelationship.java @@ -9,7 +9,7 @@ import com.avaje.ebean.Query; import com.avaje.tests.model.basic.Customer; import com.avaje.tests.model.basic.ResetBasicData; -public class TestImplicitJoinOnParentRelatonship extends BaseTestCase { +public class TestImplicitJoinOnParentRelationship extends BaseTestCase { @Test public void test() { @@ -50,14 +50,21 @@ public class TestImplicitJoinOnParentRelatonship extends BaseTestCase { String expectedSql = "select distinct t0.id c0, t0.name c1 from o_customer t0 left outer join o_order u1 on u1.kcustomer_id = t0.id left outer join o_order_detail u2 on u2.order_id = u1.id left outer join o_product u3 on u3.id = u2.product_id where (u3.name = ? or t0.id = ? ) "; Assert.assertEquals(expectedSql, query.getGeneratedSql()); - - // select distinct t0.id c0, t0.name c1 - // from o_customer t0 select distinct t0.id c0, t0.name c1 from o_customer t0 join o_order u1 on u1.kcustomer_id = t0.id join o_order_detail u2 on u2.order_id = u1.id join o_product u3 on u3.id = u2.product_id where u3.name = ? - // left outer join o_order u1 on u1.kcustomer_id = t0.id - // left outer join o_order_detail u2 on u2.order_id = u1.id - // left outer join o_product u3 on u3.id = u2.product_id - // where (u3.name = ? or t0.id = ? ) ; --bind(Desk,4) - } + @Test + public void testWithOr() { + + ResetBasicData.reset(); + + Query query = Ebean.find(Customer.class) + .select("id, name") + .where().or().eq("orders.details.product.name", "Desk").eq("id", 4).endJunction() + .query(); + + query.findList(); + + String expectedSql = "select distinct t0.id c0, t0.name c1 from o_customer t0 left outer join o_order u1 on u1.kcustomer_id = t0.id left outer join o_order_detail u2 on u2.order_id = u1.id left outer join o_product u3 on u3.id = u2.product_id where (u3.name = ? or t0.id = ? ) "; + Assert.assertEquals(expectedSql, query.getGeneratedSql()); + } }