#355 - ENH: Add notIn(...) expressions. Just to make life easier in those cases

This commit is contained in:
Robin Bygrave
2015-07-27 20:39:34 +12:00
parent 5a55b5235d
commit 87b485cfa4
9 changed files with 237 additions and 22 deletions
@@ -0,0 +1,90 @@
package com.avaje.ebeaninternal.server.expression;
import com.avaje.ebeaninternal.api.HashQueryPlanBuilder;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
public class InExpressionTest {
@Test
public void queryPlanHash_given_diffPropertyName_should_differentPlanHash() throws Exception {
List<Integer> values = values(42, 92);
InExpression ex1 = new InExpression("foo", values, false);
InExpression ex2 = new InExpression("bar", values, false);
HashQueryPlanBuilder b1 = new HashQueryPlanBuilder();
ex1.queryPlanHash(null, b1);
HashQueryPlanBuilder b2 = new HashQueryPlanBuilder();
ex2.queryPlanHash(null, b2);
assertNotEquals(b1.build(), b2.build());
}
@Test
public void queryPlanHash_given_diffBindCount_should_differentPlanHash() throws Exception {
List<Integer> values1 = values(42, 92);
List<Integer> values2 = values(42, 92, 82);
InExpression ex1 = new InExpression("foo", values1, false);
InExpression ex2 = new InExpression("foo", values2, false);
HashQueryPlanBuilder b1 = new HashQueryPlanBuilder();
ex1.queryPlanHash(null, b1);
HashQueryPlanBuilder b2 = new HashQueryPlanBuilder();
ex2.queryPlanHash(null, b2);
assertNotEquals(b1.build(), b2.build());
}
@Test
public void queryPlanHash_given_diffNotFlag_should_differentPlanHash() throws Exception {
List<Integer> values = values(42, 92);
InExpression ex1 = new InExpression("foo", values, true);
InExpression ex2 = new InExpression("foo", values, false);
HashQueryPlanBuilder b1 = new HashQueryPlanBuilder();
ex1.queryPlanHash(null, b1);
HashQueryPlanBuilder b2 = new HashQueryPlanBuilder();
ex2.queryPlanHash(null, b2);
assertNotEquals(b1.build(), b2.build());
}
@Test
public void queryPlanHash_given_sameNotFlag_should_samePlanHash() throws Exception {
List<Integer> values = values(42, 92);
InExpression ex1 = new InExpression("foo", values, true);
InExpression ex2 = new InExpression("foo", values, true);
HashQueryPlanBuilder b1 = new HashQueryPlanBuilder();
ex1.queryPlanHash(null, b1);
HashQueryPlanBuilder b2 = new HashQueryPlanBuilder();
ex2.queryPlanHash(null, b2);
assertEquals(b1.build(), b2.build());
}
List<Integer> values(int... vals) {
ArrayList list = new ArrayList<Integer>();
for (int i = 0; i < vals.length; i++) {
list.add(vals[i]);
}
return list;
}
}
@@ -10,8 +10,10 @@ import com.avaje.tests.model.basic.CKeyParent;
import com.avaje.tests.model.basic.ResetBasicData;
public class TestQueryAlias extends BaseTestCase {
@Test
public void testExists() {
ResetBasicData.reset();
Query<CKeyParent> sq = Ebean.createQuery(CKeyParent.class)
@@ -24,7 +26,6 @@ public class TestQueryAlias extends BaseTestCase {
String sql = pq.getGeneratedSql();
System.out.println(sql);
// Without alias command is should be:
// select t0.one_key c0, t0.two_key c1, t0.name c2, t0.version c3, t0.assoc_id c4 from ckey_parent t0 where (t0
// .one_key) in (select t0.one_key from ckey_parent t0)
@@ -32,7 +33,34 @@ public class TestQueryAlias extends BaseTestCase {
// select myt0.one_key c0, myt0.two_key c1, myt0.name c2, myt0.version c3, myt0.assoc_id c4 from ckey_parent myt0
// where (myt0.one_key) in (select st0.one_key from ckey_parent st0)
Assert.assertTrue(sql.indexOf("ckey_parent myt0") > 0);
Assert.assertTrue(sql.indexOf("in (select st0.one_key from ckey_parent st0)") > 0);
Assert.assertTrue(sql.contains("ckey_parent myt0"));
Assert.assertTrue(sql.contains("(myt0.one_key) in (select st0.one_key from ckey_parent st0)"));
}
@Test
public void testNotExists() {
ResetBasicData.reset();
Query<CKeyParent> sq = Ebean.createQuery(CKeyParent.class)
.select("id.oneKey").alias("st0")
.setAutofetch(false).where().query();
Query<CKeyParent> pq = Ebean.find(CKeyParent.class).alias("myt0").where().notIn("id.oneKey", sq).query();
pq.findList();
String sql = pq.getGeneratedSql();
// Without alias command is should be:
// select t0.one_key c0, t0.two_key c1, t0.name c2, t0.version c3, t0.assoc_id c4 from ckey_parent t0 where (t0
// .one_key) in (select t0.one_key from ckey_parent t0)
// but with alias command SQL should look like this:
// select myt0.one_key c0, myt0.two_key c1, myt0.name c2, myt0.version c3, myt0.assoc_id c4 from ckey_parent myt0
// where (myt0.one_key) in (select st0.one_key from ckey_parent st0)
Assert.assertTrue(sql.contains("ckey_parent myt0"));
Assert.assertTrue(sql.contains("(myt0.one_key) not in (select st0.one_key from ckey_parent st0)"));
}
}