aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md4
-rw-r--r--src/main/java/com/iciql/BitCondition.java82
-rw-r--r--src/main/java/com/iciql/Bitwise.java31
-rw-r--r--src/main/java/com/iciql/QueryCondition.java13
-rw-r--r--src/main/java/com/iciql/SQLDialect.java4
-rw-r--r--src/main/java/com/iciql/SQLDialectDefault.java14
-rw-r--r--src/main/java/com/iciql/SQLDialectDerby.java11
-rw-r--r--src/main/java/com/iciql/SQLDialectH2.java19
-rw-r--r--src/main/java/com/iciql/SQLDialectHSQL.java19
-rw-r--r--src/main/java/com/iciql/SQLDialectPostgreSQL.java6
-rw-r--r--src/main/java/com/iciql/SQLDialectSQLite.java5
-rw-r--r--src/test/java/com/iciql/test/BitwiseConditionTest.java85
-rw-r--r--src/test/java/com/iciql/test/IciqlSuite.java2
13 files changed, 294 insertions, 1 deletions
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 <A> the operand type
+ */
+
+class BitCondition<A, T> implements Token, Bitwise<A, T> {
+
+ enum Bitwise {
+ AND, XOR
+ }
+
+ Bitwise bitwiseType;
+ CompareType compareType;
+ A x, y, z;
+ Query<T> query;
+
+ BitCondition(A x, A y, Bitwise bitwiseType, Query<T> query) {
+ this.bitwiseType = bitwiseType;
+ this.x = x;
+ this.y = y;
+ this.query = query;
+ }
+
+ public QueryWhere<T> exceeds(A y) {
+ z = y;
+ compareType = CompareType.EXCEEDS;
+ return new QueryWhere<T>(query);
+ }
+
+ public QueryWhere<T> atLeast(A y) {
+ z = y;
+ compareType = CompareType.AT_LEAST;
+ return new QueryWhere<T>(query);
+ }
+
+ public QueryWhere<T> lessThan(A y) {
+ z = y;
+ compareType = CompareType.LESS_THAN;
+ return new QueryWhere<T>(query);
+ }
+
+ public QueryWhere<T> atMost(A y) {
+ z = y;
+ compareType = CompareType.AT_MOST;
+ return new QueryWhere<T>(query);
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T> void appendSQL(SQLStatement stat, Query<T> 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<A, T> {
+
+ QueryWhere<T> exceeds(A y);
+
+ QueryWhere<T> atLeast(A y);
+
+ QueryWhere<T> lessThan(A y);
+
+ QueryWhere<T> 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<T, A> {
query.addConditionToken(new RuntimeParameter<A>(x, CompareType.LIKE));
return new QueryWhere<T>(query);
}
+
+ public Bitwise<A, T> bitAnd(A y) {
+ BitCondition<A, T> bitCondition = new BitCondition<A, T>(x, y, BitCondition.Bitwise.AND, query);
+ query.addConditionToken(bitCondition);
+ return bitCondition;
+ }
+
+ public Bitwise<A, T> bitXor(A y) {
+ BitCondition<A, T> bitCondition = new BitCondition<A, T>(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);
+ <T, A> void prepareBitwiseAnd(SQLStatement stat, Query<T> query, A x, A y);
+
+ <T, A> void prepareBitwiseXor(SQLStatement stat, Query<T> 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 <T, A> void prepareBitwiseAnd(SQLStatement stat, Query<T> query, A x, A y) {
+ query.appendSQL(stat, null, x);
+ stat.appendSQL(" & ");
+ query.appendSQL(stat, x, y);
+ }
+
+ @Override
+ public <T, A> void prepareBitwiseXor(SQLStatement stat, Query<T> 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 <T, A> void prepareBitwiseAnd(SQLStatement stat, Query<T> query, A x, A y) {
+ throw new IciqlException("{0} does not support bitwise operator AND", databaseName);
+ }
+
+ @Override
+ public <T, A> void prepareBitwiseXor(SQLStatement stat, Query<T> 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 <T, A> void prepareBitwiseAnd(SQLStatement stat, Query<T> query, A x, A y) {
+ stat.appendSQL("BITAND(");
+ query.appendSQL(stat, null, x);
+ stat.appendSQL(",");
+ query.appendSQL(stat, x, y);
+ stat.appendSQL(")");
+ }
+
+ @Override
+ public <T, A> void prepareBitwiseXor(SQLStatement stat, Query<T> 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 <T, A> void prepareBitwiseAnd(SQLStatement stat, Query<T> query, A x, A y) {
+ stat.appendSQL("BITAND(");
+ query.appendSQL(stat, null, x);
+ stat.appendSQL(",");
+ query.appendSQL(stat, x, y);
+ stat.appendSQL(")");
+ }
+
+ @Override
+ public <T, A> void prepareBitwiseXor(SQLStatement stat, Query<T> 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 <T, A> void prepareBitwiseXor(SQLStatement stat, Query<T> 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 <T, A> void prepareBitwiseXor(SQLStatement stat, Query<T> 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<Product> 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<Integer> ids = new HashSet<Integer>();
+ 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<Product> 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<Integer> ids = new HashSet<Integer>();
+ 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 = {