From 1005f177fd50f91495c491f3f94eece0084e2198 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Mon, 16 Oct 2017 23:13:49 +1300 Subject: [PATCH] #1170 - ENH: Add isIn() methods as alias to in() on ExpressionList (for use with Kotlin where `in` is a keyword) --- src/main/java/io/ebean/ExpressionList.java | 34 +++++++++++++++++-- .../java/org/tests/basic/TestInEmpty.java | 11 ++++++ .../org/tests/query/TestQueryInAssocOne.java | 18 ++++++++++ .../java/org/tests/query/TestSubQuery.java | 14 ++++++++ 4 files changed, 75 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/ebean/ExpressionList.java b/src/main/java/io/ebean/ExpressionList.java index e55d6351d..348c2e5a2 100644 --- a/src/main/java/io/ebean/ExpressionList.java +++ b/src/main/java/io/ebean/ExpressionList.java @@ -453,13 +453,13 @@ public interface ExpressionList { /** * Calls {@link #setUseQueryCache(CacheMode)} with ON or OFF. - * + * * @see Query#setUseQueryCache(CacheMode) */ default Query setUseQueryCache(boolean enabled) { return setUseQueryCache(enabled ? CacheMode.ON : CacheMode.OFF); } - + /** * Set to true if this query should execute against the doc store. *

@@ -807,6 +807,36 @@ public interface ExpressionList { */ ExpressionList in(String propertyName, Collection values); + /** + * In - using a subQuery. + * + * This is exactly the same as in() and provided due to "in" being a Kotlin keyword + * (and hence to avoid the slightly ugly escaping when using in() in Kotlin) + */ + default ExpressionList isIn(String propertyName, Query subQuery) { + return in(propertyName, subQuery); + } + + /** + * In - property has a value in the array of values. + * + * This is exactly the same as in() and provided due to "in" being a Kotlin keyword + * (and hence to avoid the slightly ugly escaping when using in() in Kotlin) + */ + default ExpressionList isIn(String propertyName, Object... values) { + return in(propertyName, values); + } + + /** + * In - property has a value in the collection of values. + * + * This is exactly the same as in() and provided due to "in" being a Kotlin keyword + * (and hence to avoid the slightly ugly escaping when using in() in Kotlin) + */ + default ExpressionList isIn(String propertyName, Collection values) { + return in(propertyName, values); + } + /** * Not In - property has a value in the array of values. */ diff --git a/src/test/java/org/tests/basic/TestInEmpty.java b/src/test/java/org/tests/basic/TestInEmpty.java index af4055d4f..8de09baf6 100644 --- a/src/test/java/org/tests/basic/TestInEmpty.java +++ b/src/test/java/org/tests/basic/TestInEmpty.java @@ -24,6 +24,17 @@ public class TestInEmpty extends BaseTestCase { assertEquals(0, list.size()); } + @Test + public void test_isIn_empty() { + + Query query = Ebean.find(Order.class).where().isIn("id", new Object[0]).gt("id", 0) + .query(); + + List list = query.findList(); + assertThat(query.getGeneratedSql()).contains("1=0"); + assertEquals(0, list.size()); + } + @Test public void test_notIn_empty() { diff --git a/src/test/java/org/tests/query/TestQueryInAssocOne.java b/src/test/java/org/tests/query/TestQueryInAssocOne.java index 7419b27b6..d95030ac2 100644 --- a/src/test/java/org/tests/query/TestQueryInAssocOne.java +++ b/src/test/java/org/tests/query/TestQueryInAssocOne.java @@ -30,6 +30,24 @@ public class TestQueryInAssocOne extends BaseTestCase { assertThat(sql).contains("t0.kcustomer_id in (?"); } + + @Test + public void test_isIn() { + + ResetBasicData.reset(); + + List list = Ebean.find(Customer.class).where().lt("id", 300).findList(); + + Query query = Ebean.find(Order.class).where().isIn("customer", list).query(); + + query.findList(); + String sql = query.getGeneratedSql(); + + assertThat(sql).contains("join o_customer t1 on t1.id = t0.kcustomer_id"); + assertThat(sql).contains("t0.kcustomer_id in (?"); + } + + @Test public void test_notIn() { diff --git a/src/test/java/org/tests/query/TestSubQuery.java b/src/test/java/org/tests/query/TestSubQuery.java index 3563b4333..817555e22 100644 --- a/src/test/java/org/tests/query/TestSubQuery.java +++ b/src/test/java/org/tests/query/TestSubQuery.java @@ -31,6 +31,20 @@ public class TestSubQuery extends BaseTestCase { Ebean.find(Order.class).where().in("id", sq).findList(); } + @Test + public void test_IsIn() { + + ResetBasicData.reset(); + + List productIds = new ArrayList<>(); + productIds.add(3); + + Query sq = Ebean.createQuery(Order.class).select("id").where() + .isIn("details.product.id", productIds).query(); + + Ebean.find(Order.class).where().isIn("id", sq).findList(); + } + public void testCompositeKey() { ResetBasicData.reset();