From 0a8fd25fd85b28c804e4c81c90bd35fdb2e57a66 Mon Sep 17 00:00:00 2001 From: James Moger Date: Mon, 30 Jan 2017 16:38:24 -0500 Subject: Added bitwise AND and XOR DSL for where clauses --- 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 ++ src/main/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 ++++++ src/main/java/com/iciql/SQLDialectPostgreSQL.java | 6 ++ src/main/java/com/iciql/SQLDialectSQLite.java | 5 ++ 10 files changed, 204 insertions(+) create mode 100644 src/main/java/com/iciql/BitCondition.java create mode 100644 src/main/java/com/iciql/Bitwise.java (limited to 'src/main/java/com/iciql') 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); + } } -- cgit v1.2.3