From 0a8fd25fd85b28c804e4c81c90bd35fdb2e57a66 Mon Sep 17 00:00:00 2001 From: James Moger Date: Mon, 30 Jan 2017 16:38:24 -0500 Subject: [PATCH] Added bitwise AND and XOR DSL for where clauses --- CHANGELOG.md | 4 + src/main/java/com/iciql/BitCondition.java | 82 ++++++++++++++++++ src/main/java/com/iciql/Bitwise.java | 31 +++++++ src/main/java/com/iciql/QueryCondition.java | 13 +++ src/main/java/com/iciql/SQLDialect.java | 4 + .../java/com/iciql/SQLDialectDefault.java | 14 +++ src/main/java/com/iciql/SQLDialectDerby.java | 11 +++ src/main/java/com/iciql/SQLDialectH2.java | 19 +++++ src/main/java/com/iciql/SQLDialectHSQL.java | 19 +++++ .../java/com/iciql/SQLDialectPostgreSQL.java | 6 ++ src/main/java/com/iciql/SQLDialectSQLite.java | 5 ++ .../com/iciql/test/BitwiseConditionTest.java | 85 +++++++++++++++++++ src/test/java/com/iciql/test/IciqlSuite.java | 2 +- 13 files changed, 294 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/iciql/BitCondition.java create mode 100644 src/main/java/com/iciql/Bitwise.java create mode 100644 src/test/java/com/iciql/test/BitwiseConditionTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bb1219..144da0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +### [Unreleased][unreleased] +#### Added +- Added bitwise AND and XOR DSL for where clauses + ### [2.1.1] - 2016-04-07 #### Added - Add explicit `where(String)` method to help with non-Java language interop diff --git a/src/main/java/com/iciql/BitCondition.java b/src/main/java/com/iciql/BitCondition.java new file mode 100644 index 0000000..e76333f --- /dev/null +++ b/src/main/java/com/iciql/BitCondition.java @@ -0,0 +1,82 @@ +/* + * Copyright 2017 James Moger. + * + * 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; + +/** + * A bitwise condition contains two operands, a bit operation, and a comparison test. + * + * @param the operand type + */ + +class BitCondition implements Token, Bitwise { + + enum Bitwise { + AND, XOR + } + + Bitwise bitwiseType; + CompareType compareType; + A x, y, z; + Query query; + + BitCondition(A x, A y, Bitwise bitwiseType, Query query) { + this.bitwiseType = bitwiseType; + this.x = x; + this.y = y; + this.query = query; + } + + public QueryWhere exceeds(A y) { + z = y; + compareType = CompareType.EXCEEDS; + return new QueryWhere(query); + } + + public QueryWhere atLeast(A y) { + z = y; + compareType = CompareType.AT_LEAST; + return new QueryWhere(query); + } + + public QueryWhere lessThan(A y) { + z = y; + compareType = CompareType.LESS_THAN; + return new QueryWhere(query); + } + + public QueryWhere atMost(A y) { + z = y; + compareType = CompareType.AT_MOST; + return new QueryWhere(query); + } + + @SuppressWarnings("unchecked") + public void appendSQL(SQLStatement stat, Query query) { + stat.appendSQL("("); + switch (bitwiseType) { + case AND: + query.getDb().getDialect().prepareBitwiseAnd(stat, query, x, y); + break; + case XOR: + query.getDb().getDialect().prepareBitwiseXor(stat, query, x, y); + break; + } + stat.appendSQL(")"); + stat.appendSQL(compareType.getString()); + stat.appendSQL("" + z); + } +} diff --git a/src/main/java/com/iciql/Bitwise.java b/src/main/java/com/iciql/Bitwise.java new file mode 100644 index 0000000..a2d1da5 --- /dev/null +++ b/src/main/java/com/iciql/Bitwise.java @@ -0,0 +1,31 @@ +/* + * Copyright 2017 James Moger. + * + * 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; + +/** + * @author James Moger + */ +public interface Bitwise { + + QueryWhere exceeds(A y); + + QueryWhere atLeast(A y); + + QueryWhere lessThan(A y); + + QueryWhere atMost(A y); +} diff --git a/src/main/java/com/iciql/QueryCondition.java b/src/main/java/com/iciql/QueryCondition.java index 56de976..b7cb0d4 100644 --- a/src/main/java/com/iciql/QueryCondition.java +++ b/src/main/java/com/iciql/QueryCondition.java @@ -147,4 +147,17 @@ public class QueryCondition { query.addConditionToken(new RuntimeParameter(x, CompareType.LIKE)); return new QueryWhere(query); } + + public Bitwise bitAnd(A y) { + BitCondition bitCondition = new BitCondition(x, y, BitCondition.Bitwise.AND, query); + query.addConditionToken(bitCondition); + return bitCondition; + } + + public Bitwise bitXor(A y) { + BitCondition bitCondition = new BitCondition(x, y, BitCondition.Bitwise.XOR, query); + query.addConditionToken(bitCondition); + return bitCondition; + } + } diff --git a/src/main/java/com/iciql/SQLDialect.java b/src/main/java/com/iciql/SQLDialect.java index 1cb4931..081b7e6 100644 --- a/src/main/java/com/iciql/SQLDialect.java +++ b/src/main/java/com/iciql/SQLDialect.java @@ -202,4 +202,8 @@ public interface SQLDialect { */ String extractColumnName(String name); + void prepareBitwiseAnd(SQLStatement stat, Query query, A x, A y); + + void prepareBitwiseXor(SQLStatement stat, Query query, A x, A y); + } diff --git a/src/main/java/com/iciql/SQLDialectDefault.java b/src/main/java/com/iciql/SQLDialectDefault.java index 4f635a9..98f63c4 100644 --- a/src/main/java/com/iciql/SQLDialectDefault.java +++ b/src/main/java/com/iciql/SQLDialectDefault.java @@ -529,4 +529,18 @@ public class SQLDialectDefault implements SQLDialect { return o.toString(); } + @Override + public void prepareBitwiseAnd(SQLStatement stat, Query query, A x, A y) { + query.appendSQL(stat, null, x); + stat.appendSQL(" & "); + query.appendSQL(stat, x, y); + } + + @Override + public void prepareBitwiseXor(SQLStatement stat, Query query, A x, A y) { + query.appendSQL(stat, null, x); + stat.appendSQL(" ^ "); + query.appendSQL(stat, x, y); + } + } diff --git a/src/main/java/com/iciql/SQLDialectDerby.java b/src/main/java/com/iciql/SQLDialectDerby.java index 77fc73e..fcaee1e 100644 --- a/src/main/java/com/iciql/SQLDialectDerby.java +++ b/src/main/java/com/iciql/SQLDialectDerby.java @@ -68,4 +68,15 @@ public class SQLDialectDerby extends SQLDialectDefault { stat.setSQL(buff.toString()); return; } + + @Override + public void prepareBitwiseAnd(SQLStatement stat, Query query, A x, A y) { + throw new IciqlException("{0} does not support bitwise operator AND", databaseName); + } + + @Override + public void prepareBitwiseXor(SQLStatement stat, Query query, A x, A y) { + throw new IciqlException("{0} does not support bitwise operator XOR", databaseName); + } + } \ No newline at end of file diff --git a/src/main/java/com/iciql/SQLDialectH2.java b/src/main/java/com/iciql/SQLDialectH2.java index 12cc161..71c11a5 100644 --- a/src/main/java/com/iciql/SQLDialectH2.java +++ b/src/main/java/com/iciql/SQLDialectH2.java @@ -133,4 +133,23 @@ public class SQLDialectH2 extends SQLDialectDefault { buff.append(')'); stat.setSQL(buff.toString()); } + + @Override + public void prepareBitwiseAnd(SQLStatement stat, Query query, A x, A y) { + stat.appendSQL("BITAND("); + query.appendSQL(stat, null, x); + stat.appendSQL(","); + query.appendSQL(stat, x, y); + stat.appendSQL(")"); + } + + @Override + public void prepareBitwiseXor(SQLStatement stat, Query query, A x, A y) { + stat.appendSQL("BITXOR("); + query.appendSQL(stat, null, x); + stat.appendSQL(","); + query.appendSQL(stat, x, y); + stat.appendSQL(")"); + } + } \ No newline at end of file diff --git a/src/main/java/com/iciql/SQLDialectHSQL.java b/src/main/java/com/iciql/SQLDialectHSQL.java index 74a3dcc..43e6588 100644 --- a/src/main/java/com/iciql/SQLDialectHSQL.java +++ b/src/main/java/com/iciql/SQLDialectHSQL.java @@ -147,4 +147,23 @@ public class SQLDialectHSQL extends SQLDialectDefault { buff.append(')'); stat.setSQL(buff.toString()); } + + @Override + public void prepareBitwiseAnd(SQLStatement stat, Query query, A x, A y) { + stat.appendSQL("BITAND("); + query.appendSQL(stat, null, x); + stat.appendSQL(","); + query.appendSQL(stat, x, y); + stat.appendSQL(")"); + } + + @Override + public void prepareBitwiseXor(SQLStatement stat, Query query, A x, A y) { + stat.appendSQL("BITXOR("); + query.appendSQL(stat, null, x); + stat.appendSQL(","); + query.appendSQL(stat, x, y); + stat.appendSQL(")"); + } + } \ No newline at end of file diff --git a/src/main/java/com/iciql/SQLDialectPostgreSQL.java b/src/main/java/com/iciql/SQLDialectPostgreSQL.java index 97b92f8..103c480 100644 --- a/src/main/java/com/iciql/SQLDialectPostgreSQL.java +++ b/src/main/java/com/iciql/SQLDialectPostgreSQL.java @@ -158,4 +158,10 @@ public class SQLDialectPostgreSQL extends SQLDialectDefault { stat.setSQL(buff.toString()); } + @Override + public void prepareBitwiseXor(SQLStatement stat, Query query, A x, A y) { + query.appendSQL(stat, null, x); + stat.appendSQL(" # "); + query.appendSQL(stat, x, y); + } } \ No newline at end of file diff --git a/src/main/java/com/iciql/SQLDialectSQLite.java b/src/main/java/com/iciql/SQLDialectSQLite.java index b99bf96..35cd0f6 100644 --- a/src/main/java/com/iciql/SQLDialectSQLite.java +++ b/src/main/java/com/iciql/SQLDialectSQLite.java @@ -177,4 +177,9 @@ public class SQLDialectSQLite extends SQLDialectDefault { } return super.prepareStringParameter(o); } + + @Override + public void prepareBitwiseXor(SQLStatement stat, Query query, A x, A y) { + throw new IciqlException("{0} does not support bitwise operator XOR", databaseName); + } } diff --git a/src/test/java/com/iciql/test/BitwiseConditionTest.java b/src/test/java/com/iciql/test/BitwiseConditionTest.java new file mode 100644 index 0000000..17b4948 --- /dev/null +++ b/src/test/java/com/iciql/test/BitwiseConditionTest.java @@ -0,0 +1,85 @@ +/* + * Copyright 2017 James Moger. + * + * 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 com.iciql.Db; +import com.iciql.test.models.Product; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.sql.SQLException; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * @author James Moger + */ +public class BitwiseConditionTest { + + /** + * This object represents a database (actually a connection to the + * database). + */ + + private Db db; + + @Before + public void setUp() { + db = IciqlSuite.openNewDb(); + db.insertAll(Product.getList()); + } + + @After + public void tearDown() { + db.close(); + } + + @Test + public void testBitwiseAndWhereCondition() throws SQLException { + Product products = new Product(); + List list = db.from(products).where(products.unitsInStock).bitAnd(1).exceeds(0).select(); + + assertTrue("Bitwise AND list is empty!", !list.isEmpty()); + assertEquals("Unexpected results for bitwise AND test", 7, list.size()); + Set ids = new HashSet(); + for (Product p : list) { + ids.add(p.getId()); + } + assertTrue("Unexpected id in Bitwise AND where condition test", ids.containsAll(Arrays.asList(1, 2, 3, 4, 7, 9, 10))); + } + + @Test + public void testBitwiseXorWhereCondition() throws SQLException { + Product products = new Product(); + List list = db.from(products).where(products.unitsInStock).bitXor(39).exceeds(0).select(); + + assertTrue("Bitwise XOR list is empty!", !list.isEmpty()); + assertEquals("Unexpected results for bitwise XOR test", 9, list.size()); + Set ids = new HashSet(); + for (Product p : list) { + ids.add(p.getId()); + } + assertTrue("Expected id in Bitwise XOR where condition test", ids.containsAll(Arrays.asList( 2, 3, 4, 5, 6, 7, 8, 9, 10))); + } + +} diff --git a/src/test/java/com/iciql/test/IciqlSuite.java b/src/test/java/com/iciql/test/IciqlSuite.java index df5e4d6..a5470d9 100644 --- a/src/test/java/com/iciql/test/IciqlSuite.java +++ b/src/test/java/com/iciql/test/IciqlSuite.java @@ -95,7 +95,7 @@ import java.util.Map; ConcurrencyTest.class, EnumsTest.class, ModelsTest.class, PrimitivesTest.class, OneOfTest.class, RuntimeQueryTest.class, SamplesTest.class, UpdateTest.class, UpgradesTest.class, JoinTest.class, UUIDTest.class, ViewsTest.class, ForeignKeyTest.class, TransactionTest.class, NestedConditionsTest.class, - DataTypeAdapterTest.class, ProductDaoTest.class}) + DataTypeAdapterTest.class, ProductDaoTest.class, BitwiseConditionTest.class}) public class IciqlSuite { private final static File baseFolder = new File(System.getProperty("user.dir"), "/testdbs"); private static final TestDb[] TEST_DBS = { -- 2.39.5