diff options
author | James Moger <james.moger@gitblit.com> | 2017-01-30 16:38:24 -0500 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2017-01-30 16:46:03 -0500 |
commit | 0a8fd25fd85b28c804e4c81c90bd35fdb2e57a66 (patch) | |
tree | 44a0ba94fc3ae9199dd1feb44413b0b9947c3451 /src/main/java/com/iciql | |
parent | cf7ea9600595749065129d48fbdff6f781db87b3 (diff) | |
download | iciql-0a8fd25fd85b28c804e4c81c90bd35fdb2e57a66.tar.gz iciql-0a8fd25fd85b28c804e4c81c90bd35fdb2e57a66.zip |
Added bitwise AND and XOR DSL for where clauses
Diffstat (limited to 'src/main/java/com/iciql')
-rw-r--r-- | src/main/java/com/iciql/BitCondition.java | 82 | ||||
-rw-r--r-- | src/main/java/com/iciql/Bitwise.java | 31 | ||||
-rw-r--r-- | src/main/java/com/iciql/QueryCondition.java | 13 | ||||
-rw-r--r-- | src/main/java/com/iciql/SQLDialect.java | 4 | ||||
-rw-r--r-- | src/main/java/com/iciql/SQLDialectDefault.java | 14 | ||||
-rw-r--r-- | src/main/java/com/iciql/SQLDialectDerby.java | 11 | ||||
-rw-r--r-- | src/main/java/com/iciql/SQLDialectH2.java | 19 | ||||
-rw-r--r-- | src/main/java/com/iciql/SQLDialectHSQL.java | 19 | ||||
-rw-r--r-- | src/main/java/com/iciql/SQLDialectPostgreSQL.java | 6 | ||||
-rw-r--r-- | src/main/java/com/iciql/SQLDialectSQLite.java | 5 |
10 files changed, 204 insertions, 0 deletions
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);
+ }
}
|