#727 - ENH: Add endOr(), endAnd(), endNot() ... to ExpressionList as synonym for endJunction()

This commit is contained in:
Robin Bygrave
2016-06-01 16:03:30 +12:00
parent 74d7652044
commit 71f70c01ae
4 changed files with 90 additions and 24 deletions
@@ -925,7 +925,7 @@ public interface ExpressionList<T> {
* This is exactly the same as conjunction();
* </p>
* <p>
* Use endJunction() to end the AND junction.
* Use endAnd() or endJunction() to end the AND junction.
* </p>
* <p>
* Note that a where() clause defaults to an AND junction so
@@ -943,11 +943,11 @@ public interface ExpressionList<T> {
* .and() // nested and
* .startsWith("name", "r")
* .eq("anniversary", onAfter)
* .endJunction() // end AND junction
* .endAnd()
* .and()
* .eq("status", Customer.Status.ACTIVE)
* .gt("id", 0)
* .endJunction() // end AND junction
* .endAnd()
* .orderBy().asc("name")
* .findList();
* }</pre>
@@ -959,7 +959,7 @@ public interface ExpressionList<T> {
* This is exactly the same as disjunction();
*
* <p>
* Use endJunction() to end the OR junction.
* Use endOr() or endJunction() to end the OR junction.
* </p>
*
* <pre>{@code
@@ -973,11 +973,11 @@ public interface ExpressionList<T> {
* .and()
* .startsWith("name", "r")
* .eq("anniversary", onAfter)
* .endJunction() // end AND junction
* .endAnd()
* .and()
* .eq("status", Customer.Status.ACTIVE)
* .gt("id", 0)
* .endJunction() // end AND junction
* .endAnd()
* .orderBy().asc("name")
* .findList();
*
@@ -988,7 +988,7 @@ public interface ExpressionList<T> {
/**
* Return a list of expressions that will be wrapped by NOT.
* <p>
* Use endJunction() to end expressions being added to the
* Use endNot() or endJunction() to end expressions being added to the
* NOT expression list.
* </p>
*
@@ -998,7 +998,7 @@ public interface ExpressionList<T> {
* .not()
* .gt("id", 1)
* .eq("anniversary", onAfter)
* .endJunction() // end the not expressions
* .endNot()
*
* }</pre>
*
@@ -1012,7 +1012,7 @@ public interface ExpressionList<T> {
* .not()
* .gt("id", 1)
* .eq("anniversary", onAfter)
* .endJunction() // end the not expressions
* .endNot()
* .orderBy()
* .asc("name")
* .findList();
@@ -1085,4 +1085,19 @@ public interface ExpressionList<T> {
*/
ExpressionList<T> endJunction();
/**
* End a AND junction - synonym for endJunction().
*/
ExpressionList<T> endAnd();
/**
* End a AND junction - synonym for endJunction().
*/
ExpressionList<T> endOr();
/**
* End a NOT junction - synonym for endJunction().
*/
ExpressionList<T> endNot();
}
@@ -925,6 +925,21 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
return parentExprList == null ? this : parentExprList;
}
@Override
public ExpressionList<T> endAnd() {
return endJunction();
}
@Override
public ExpressionList<T> endOr() {
return endJunction();
}
@Override
public ExpressionList<T> endNot() {
return endJunction();
}
@Override
public Junction<T> and() {
return conjunction();
@@ -776,4 +776,18 @@ class JunctionExpression<T> implements SpiJunction<T>, SpiExpression, Expression
return exprList.endJunction();
}
@Override
public ExpressionList<T> endAnd() {
return endJunction();
}
@Override
public ExpressionList<T> endOr() {
return endJunction();
}
@Override
public ExpressionList<T> endNot() {
return endJunction();
}
}
@@ -1,13 +1,11 @@
package com.avaje.tests.query;
import org.junit.Assert;
import org.junit.Test;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.Query;
import com.avaje.tests.model.basic.Customer;
import com.avaje.tests.model.basic.ResetBasicData;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
@@ -20,16 +18,17 @@ public class TestExprNestedDisjunction extends BaseTestCase {
java.sql.Date onAfter = java.sql.Date.valueOf("2009-08-31");
Query<Customer> q = Ebean.find(Customer.class).where().disjunction()
.conjunction().startsWith("name", "r").eq("anniversary", onAfter).endJunction()
.conjunction().eq("status", Customer.Status.ACTIVE).gt("id", 0).endJunction().orderBy()
.asc("name");
Query<Customer> q = Ebean.find(Customer.class).where()
.disjunction()
.conjunction().startsWith("name", "r").eq("anniversary", onAfter).endJunction()
.conjunction().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 > ? )"));
assertThat(s).contains("(t0.name like ? ");
assertThat(s).contains(" and t0.anniversary = ? ) or (t0.status = ? and t0.id > ? )");
}
@@ -44,16 +43,16 @@ public class TestExprNestedDisjunction extends BaseTestCase {
.where()
.or()
.and()
.startsWith("name", "r").eq("anniversary", onAfter).endJunction()
.startsWith("name", "r").eq("anniversary", onAfter).endAnd()
.and()
.eq("status", Customer.Status.ACTIVE).gt("id", 0).endJunction()
.eq("status", Customer.Status.ACTIVE).gt("id", 0).endAnd()
.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 > ? )"));
assertThat(s).contains("(t0.name like ? ");
assertThat(s).contains(" and t0.anniversary = ? ) or (t0.status = ? and t0.id > ? )");
}
@Test
@@ -68,7 +67,7 @@ public class TestExprNestedDisjunction extends BaseTestCase {
.not()
.gt("id", 1)
.eq("anniversary", onAfter)
.endJunction()
.endNot()
.orderBy().asc("name");
q.findList();
@@ -91,7 +90,30 @@ public class TestExprNestedDisjunction extends BaseTestCase {
.not()
.gt("id", 1)
.eq("anniversary", onAfter)
//.endNot()
.orderBy().asc("name");
q.findList();
String s = q.getGeneratedSql();
assertThat(s).contains("where (t0.status = ? or not (t0.id > ? and t0.anniversary = ? ) )");
}
@Test
public void test_not_nested_with_endNot() {
ResetBasicData.reset();
java.sql.Date onAfter = java.sql.Date.valueOf("2009-08-31");
Query<Customer> q = Ebean.find(Customer.class)
.where()
.or()
.eq("status", Customer.Status.ACTIVE)
.not()
.gt("id", 1)
.eq("anniversary", onAfter)
.endNot()
.endOr()
.orderBy().asc("name");
q.findList();