#606 - Add not() as a junction expression (like and() and or()) to the query criteria API

This commit is contained in:
Robin Bygrave
2016-03-18 14:11:51 +13:00
parent 3ddd7099b5
commit 5de67e1939
8 changed files with 291 additions and 204 deletions
@@ -2,6 +2,7 @@ package com.avaje.ebeaninternal.server.expression;
import com.avaje.ebean.Expr;
import com.avaje.ebean.Expression;
import com.avaje.ebean.Junction;
import org.junit.Test;
import static org.assertj.core.api.StrictAssertions.assertThat;
@@ -23,11 +24,11 @@ public class JunctionExpressionTest {
}
<T> JunctionExpression and(DefaultExpressionList<T> list) {
return new JunctionExpression.Conjunction<T>(list);
return new JunctionExpression<T>(Junction.Type.AND, list);
}
<T> JunctionExpression or(DefaultExpressionList<T> list) {
return new JunctionExpression.Disjunction<T>(list);
return new JunctionExpression<T>(Junction.Type.OR, list);
}
@Test
@@ -9,6 +9,8 @@ import com.avaje.ebean.Query;
import com.avaje.tests.model.basic.Customer;
import com.avaje.tests.model.basic.ResetBasicData;
import static org.assertj.core.api.Assertions.assertThat;
public class TestExprNestedDisjunction extends BaseTestCase {
@Test
@@ -42,9 +44,11 @@ public class TestExprNestedDisjunction extends BaseTestCase {
.where()
.or()
.and()
.startsWith("name", "r").eq("anniversary", onAfter).endJunction()
.and().eq("status", Customer.Status.ACTIVE).gt("id", 0).endJunction().orderBy()
.asc("name");
.startsWith("name", "r").eq("anniversary", onAfter).endAnd()
.and()
.eq("status", Customer.Status.ACTIVE).gt("id", 0).endAnd()
.endOr()
.orderBy().asc("name");
q.findList();
String s = q.getGeneratedSql();
@@ -52,4 +56,48 @@ public class TestExprNestedDisjunction extends BaseTestCase {
Assert.assertTrue(s.contains("(t0.name like ? "));
Assert.assertTrue(s.contains(" and t0.anniversary = ? ) or (t0.status = ? and t0.id > ? )"));
}
@Test
public void test_not() {
ResetBasicData.reset();
java.sql.Date onAfter = java.sql.Date.valueOf("2009-08-31");
Query<Customer> q = Ebean.find(Customer.class)
.where()
.not()
.gt("id", 1)
.eq("anniversary", onAfter)
.endNot()
.orderBy().asc("name");
q.findList();
String s = q.getGeneratedSql();
assertThat(s).contains("where not (t0.id > ? and t0.anniversary = ? ) order by t0.name");
}
@Test
public void test_not_nested() {
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()
.orderBy().asc("name");
q.findList();
String s = q.getGeneratedSql();
assertThat(s).contains("where (t0.status = ? or not (t0.id > ? and t0.anniversary = ? ) )");
}
}