]> source.dussan.org Git - iciql.git/commitdiff
add support condition oneOf / noneOf
authorSotaro SUZUKI <sotaro.suzuki@architector.jp>
Wed, 1 Oct 2014 06:32:06 +0000 (15:32 +0900)
committerJames Moger <james.moger@gitblit.com>
Mon, 6 Oct 2014 14:08:50 +0000 (10:08 -0400)
var-length parameterized IN(?, ?, ...), NOT IN(?, ?, ...) support

src/main/java/com/iciql/CompareType.java
src/main/java/com/iciql/Condition.java
src/main/java/com/iciql/Query.java
src/main/java/com/iciql/QueryCondition.java
src/main/java/com/iciql/util/Utils.java
src/test/java/com/iciql/test/IciqlSuite.java
src/test/java/com/iciql/test/OneOfTest.java [new file with mode: 0644]

index 84e29fe34e10aa06311466d4a017600b3181da88..87ce77e1bc91bbfc847f2b1a5156ad7ff24589fa 100644 (file)
@@ -24,7 +24,7 @@ package com.iciql;
 enum CompareType {\r
        EQUAL("=", true), EXCEEDS(">", true), AT_LEAST(">=", true), LESS_THAN("<", true), AT_MOST("<=", true), NOT_EQUAL(\r
                        "<>", true), IS_NOT_NULL("IS NOT NULL", false), IS_NULL("IS NULL", false), LIKE("LIKE", true), BETWEEN(\r
-                       "BETWEEN", true);\r
+                       "BETWEEN", true), IN("IN", true), NOT_IN("NOT IN", true);\r
 \r
        private String text;\r
        private boolean hasRightExpression;\r
index 17cb1176692150e483f6bf6627e14a54f18e94a2..0ed1d06e4ec3ace00eb4a39e63b65a0609a7d991 100644 (file)
@@ -27,28 +27,47 @@ package com.iciql;
 class Condition<A> implements Token {\r
        CompareType compareType;\r
        A x, y, z;\r
+       Iterable<A> i;\r
+\r
+       Condition(A x, CompareType compareType) {\r
+               this(x, null, null, null, compareType);\r
+       }\r
 \r
        Condition(A x, A y, CompareType compareType) {\r
-               this(x, y, null, compareType);\r
+               this(x, y, null, null, compareType);\r
        }\r
 \r
        Condition(A x, A y, A z, CompareType compareType) {\r
+               this(x, y, z, null, compareType);\r
+       }\r
+\r
+       Condition(A x, Iterable<A> i, CompareType compareType) {\r
+               this(x,  null, null, i, compareType);\r
+       }\r
+\r
+       Condition(A x, A y, A z, Iterable<A> i, CompareType compareType) {\r
                this.compareType = compareType;\r
                this.x = x;\r
                this.y = y;\r
                this.z = z;\r
+               this.i = i;\r
        }\r
 \r
+       @SuppressWarnings("unchecked")\r
        public <T> void appendSQL(SQLStatement stat, Query<T> query) {\r
                query.appendSQL(stat, null, x);\r
                stat.appendSQL(" ");\r
                stat.appendSQL(compareType.getString());\r
                if (compareType.hasRightExpression()) {\r
-                       stat.appendSQL(" ");\r
-                       if (z == null) {\r
-                               query.appendSQL(stat, x, y);\r
+                       if (i == null) {\r
+                               stat.appendSQL(" ");\r
+                               if (z == null) {\r
+                                       query.appendSQL(stat, x, y);\r
+                               } else {\r
+                                       query.appendSQL(stat, x, y, z, compareType);\r
+                               }\r
                        } else {\r
-                               query.appendSQL(stat, x, y, z, compareType);\r
+                               query.appendSQL(stat, x, (Iterable<Object>)i, compareType);\r
                        }\r
                }\r
        }\r
index 45de08b414aa793187cd63dc69fc3650ffdf1e83..5f29edf0ef0f412c898bc6b1b406a7eeae3379e5 100644 (file)
@@ -792,6 +792,22 @@ public class Query<T> {
                addParameter(stat, alias, valueRight);\r
        }\r
 \r
+       public void appendSQL(SQLStatement stat, Object alias, Iterable<Object> values,\r
+                       CompareType compareType) {\r
+               boolean first = true;\r
+               stat.appendSQL("(");\r
+               for (Object value : values) {\r
+                       if (first) {\r
+                               first = false;\r
+                       } else {\r
+                               stat.appendSQL(", ");\r
+                       }\r
+                       stat.appendSQL("?");\r
+                       addParameter(stat, alias, value);\r
+               }\r
+               stat.appendSQL(")");\r
+       }\r
+\r
        private void addParameter(SQLStatement stat, Object alias, Object value) {\r
                if (alias != null && value.getClass().isEnum()) {\r
                        SelectColumn<T> col = getColumnByReference(alias);\r
index 9613b1ba93ae2cf684d324e34b22c98f7ffdcbf3..cef95c19f54be94f8135d1723ecb6937be0c2f92 100644 (file)
@@ -17,6 +17,8 @@
 
 package com.iciql;
 
+import com.iciql.util.Utils;
+
 /**
  * This class represents a query with an incomplete condition.
  * 
@@ -41,6 +43,24 @@ public class QueryCondition<T, A> {
                return new QueryWhere<T>(query);
        }
 
+       public QueryWhere<T> oneOf(A... a) {
+               return oneOf(Utils.newArrayIterable(a));
+       }
+
+       public QueryWhere<T> oneOf(Iterable<A> i) {
+               query.addConditionToken(new Condition<A>(x, i, CompareType.IN));
+               return new QueryWhere<T>(query);
+       }
+
+       public QueryWhere<T> noneOf(A... a) {
+               return noneOf(Utils.newArrayIterable(a));
+       }
+
+       public QueryWhere<T> noneOf(Iterable<A> i) {
+               query.addConditionToken(new Condition<A>(x, i, CompareType.NOT_IN));
+               return new QueryWhere<T>(query);
+       }
+
        public QueryWhere<T> is(A y) {
                query.addConditionToken(new Condition<A>(x, y, CompareType.EQUAL));
                return new QueryWhere<T>(query);
@@ -52,12 +72,12 @@ public class QueryCondition<T, A> {
        }
 
        public QueryWhere<T> isNull() {
-               query.addConditionToken(new Condition<A>(x, null, CompareType.IS_NULL));
+               query.addConditionToken(new Condition<A>(x, CompareType.IS_NULL));
                return new QueryWhere<T>(query);
        }
 
        public QueryWhere<T> isNotNull() {
-               query.addConditionToken(new Condition<A>(x, null, CompareType.IS_NOT_NULL));
+               query.addConditionToken(new Condition<A>(x, CompareType.IS_NOT_NULL));
                return new QueryWhere<T>(query);
        }
 
index 77110b8cbeaba3b9528992acd9064929d9037535..bc636d1122bddcf9632a780162d9a8c8960730d7 100644 (file)
@@ -28,6 +28,7 @@ import java.math.BigInteger;
 import java.sql.Blob;\r
 import java.sql.Clob;\r
 import java.util.ArrayList;\r
+import java.util.Arrays;\r
 import java.util.Collection;\r
 import java.util.Collections;\r
 import java.util.HashMap;\r
@@ -78,6 +79,10 @@ public class Utils {
                }\r
        }\r
 \r
+       public static <T> Iterable<T> newArrayIterable(final T[] a) {\r
+               return Arrays.asList(a);\r
+       }\r
+\r
        public static <T> ArrayList<T> newArrayList() {\r
                return new ArrayList<T>();\r
        }\r
index a47440423994b7108d4baa5a48669764285f616e..9c9ba39e7e6469fdf626673e95da3e32df936a20 100644 (file)
@@ -77,7 +77,7 @@ import com.iciql.util.Utils;
 \r
 /**\r
  * JUnit 4 iciql test suite.\r
- * \r
+ *\r
  * By default this test suite will run against the H2 database. You can change\r
  * this by switching the DEFAULT_TEST_DB value.\r
  * <p>\r
@@ -87,11 +87,11 @@ import com.iciql.util.Utils;
  * NOTE: If you want to test against MySQL or PostgreSQL you must create an\r
  * "iciql" database and allow user "sa" password "sa" complete control of that\r
  * database.\r
- * \r
+ *\r
  */\r
 @RunWith(Suite.class)\r
 @SuiteClasses({ AliasMapTest.class, AnnotationsTest.class, BooleanModelTest.class, ClobTest.class,\r
-               ConcurrencyTest.class, EnumsTest.class, ModelsTest.class, PrimitivesTest.class,\r
+               ConcurrencyTest.class, EnumsTest.class, ModelsTest.class, PrimitivesTest.class, OneOfTest.class,\r
                RuntimeQueryTest.class, SamplesTest.class, UpdateTest.class, UpgradesTest.class, JoinTest.class,\r
                UUIDTest.class, ViewsTest.class, ForeignKeyTest.class, TransactionTest.class })\r
 public class IciqlSuite {\r
@@ -140,7 +140,7 @@ public class IciqlSuite {
        /**\r
         * Open a new Db object. All connections are cached and re-used to eliminate\r
         * embedded database startup costs.\r
-        * \r
+        *\r
         * @return a fresh Db object\r
         */\r
        public static Db openNewDb() {\r
@@ -196,7 +196,7 @@ public class IciqlSuite {
 \r
        /**\r
         * Open the current database.\r
-        * \r
+        *\r
         * @return the current database\r
         */\r
        public static Db openCurrentDb() {\r
@@ -208,7 +208,7 @@ public class IciqlSuite {
 \r
        /**\r
         * Returns the name of the underlying database engine for the Db object.\r
-        * \r
+        *\r
         * @param db\r
         * @return the database engine name\r
         */\r
@@ -223,7 +223,7 @@ public class IciqlSuite {
 \r
        /**\r
         * Returns true if the underlying database engine is Derby.\r
-        * \r
+        *\r
         * @param db\r
         * @return true if underlying database engine is Derby\r
         */\r
@@ -233,7 +233,7 @@ public class IciqlSuite {
 \r
        /**\r
         * Returns true if the underlying database engine is H2.\r
-        * \r
+        *\r
         * @param db\r
         * @return true if underlying database engine is H2\r
         */\r
@@ -243,7 +243,7 @@ public class IciqlSuite {
 \r
        /**\r
         * Returns true if the underlying database engine is MySQL.\r
-        * \r
+        *\r
         * @param db\r
         * @return true if underlying database engine is MySQL\r
         */\r
@@ -253,7 +253,7 @@ public class IciqlSuite {
 \r
        /**\r
         * Gets the default schema of the underlying database engine.\r
-        * \r
+        *\r
         * @param db\r
         * @return the default schema\r
         */\r
@@ -272,7 +272,7 @@ public class IciqlSuite {
        /**\r
         * Main entry point for the test suite. Executing this method will run the\r
         * test suite on all registered databases.\r
-        * \r
+        *\r
         * @param args\r
         * @throws Exception\r
         */\r
@@ -491,7 +491,7 @@ public class IciqlSuite {
 \r
        /**\r
         * Start an HSQL tcp server.\r
-        * \r
+        *\r
         * @return an HSQL server instance\r
         * @throws Exception\r
         */\r
@@ -503,7 +503,7 @@ public class IciqlSuite {
                // set up the rest of properties\r
 \r
                // alternative to the above is\r
-               org.hsqldb.Server server = new org.hsqldb.Server();             \r
+               org.hsqldb.Server server = new org.hsqldb.Server();\r
                server.setProperties(p);\r
                server.setLogWriter(null);\r
                server.setErrWriter(null);\r
@@ -513,7 +513,7 @@ public class IciqlSuite {
 \r
        /**\r
         * Start the H2 tcp server.\r
-        * \r
+        *\r
         * @return an H2 server instance\r
         * @throws Exception\r
         */\r
@@ -555,7 +555,7 @@ public class IciqlSuite {
                }\r
 \r
                int getStatementRate() {\r
-                       return Double.valueOf(((double) statements) / (runtime / 1000d)).intValue();\r
+                       return Double.valueOf((statements) / (runtime / 1000d)).intValue();\r
                }\r
 \r
                String describeDatabase() {\r
diff --git a/src/test/java/com/iciql/test/OneOfTest.java b/src/test/java/com/iciql/test/OneOfTest.java
new file mode 100644 (file)
index 0000000..2ca7cc7
--- /dev/null
@@ -0,0 +1,107 @@
+/*
+ * Copyright (c) 2009-2014, Architector Inc., Japan
+ * All rights reserved.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.iciql.test;
+
+import static org.junit.Assert.assertEquals;
+import java.util.ArrayList;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import com.iciql.Db;
+import com.iciql.test.models.Customer;
+import com.iciql.test.models.PrimitivesModel;
+
+public class OneOfTest {
+
+       private Db db;
+
+       @Before
+       public void setUp() {
+               db = IciqlSuite.openNewDb();
+       }
+
+       @After
+       public void tearDown() {
+               db.close();
+       }
+
+       @SuppressWarnings("serial")
+       @Test
+       public void oneOfTest() {
+               PrimitivesModel p = new PrimitivesModel();
+               assertEquals(
+                               db.from(p)
+                                               .where(p.myInteger).oneOf(0)
+                                               .toSQL(),
+                               "SELECT * FROM PrimitivesTest WHERE myInteger IN(0)");
+               assertEquals(
+                               db.from(p)
+                                               .where(p.myInteger).oneOf(0, 1)
+                                               .toSQL(),
+                               "SELECT * FROM PrimitivesTest WHERE myInteger IN(0, 1)");
+               Customer c = new Customer();
+               assertEquals(
+                               db.from(c)
+                                               .where(c.customerId).oneOf(new ArrayList<String>() {{
+                                                       this.add("a");
+                                               }})
+                                               .toSQL(),
+                               "SELECT * FROM Customer WHERE customerId IN('a')");
+               assertEquals(
+                               db.from(c)
+                                               .where(c.customerId).oneOf(new ArrayList<String>() {{
+                                                       this.add("a");
+                                                       this.add("b");
+                                               }})
+                                               .toSQL(),
+                               "SELECT * FROM Customer WHERE customerId IN('a', 'b')");
+       }
+
+       @SuppressWarnings("serial")
+       @Test
+       public void noneOfTest() {
+               PrimitivesModel p = new PrimitivesModel();
+               assertEquals(
+                               db.from(p)
+                                               .where(p.myInteger).noneOf(0)
+                                               .toSQL(),
+                               "SELECT * FROM PrimitivesTest WHERE myInteger NOT IN(0)");
+               assertEquals(
+                               db.from(p)
+                                               .where(p.myInteger).noneOf(0, 1)
+                                               .toSQL(),
+                               "SELECT * FROM PrimitivesTest WHERE myInteger NOT IN(0, 1)");
+               Customer c = new Customer();
+               assertEquals(
+                               db.from(c)
+                                               .where(c.customerId).noneOf(new ArrayList<String>() {{
+                                                       this.add("a");
+                                               }})
+                                               .toSQL(),
+                               "SELECT * FROM Customer WHERE customerId NOT IN('a')");
+               assertEquals(
+                               db.from(c)
+                                               .where(c.customerId).noneOf(new ArrayList<String>() {{
+                                                       this.add("a");
+                                                       this.add("b");
+                                               }})
+                                               .toSQL(),
+                               "SELECT * FROM Customer WHERE customerId NOT IN('a', 'b')");
+       }
+
+}