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
--- /dev/null
+/*\r
+ * Copyright 2017 James Moger.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package com.iciql;\r
+\r
+/**\r
+ * A bitwise condition contains two operands, a bit operation, and a comparison test.\r
+ *\r
+ * @param <A> the operand type\r
+ */\r
+\r
+class BitCondition<A, T> implements Token, Bitwise<A, T> {\r
+\r
+ enum Bitwise {\r
+ AND, XOR\r
+ }\r
+\r
+ Bitwise bitwiseType;\r
+ CompareType compareType;\r
+ A x, y, z;\r
+ Query<T> query;\r
+\r
+ BitCondition(A x, A y, Bitwise bitwiseType, Query<T> query) {\r
+ this.bitwiseType = bitwiseType;\r
+ this.x = x;\r
+ this.y = y;\r
+ this.query = query;\r
+ }\r
+\r
+ public QueryWhere<T> exceeds(A y) {\r
+ z = y;\r
+ compareType = CompareType.EXCEEDS;\r
+ return new QueryWhere<T>(query);\r
+ }\r
+\r
+ public QueryWhere<T> atLeast(A y) {\r
+ z = y;\r
+ compareType = CompareType.AT_LEAST;\r
+ return new QueryWhere<T>(query);\r
+ }\r
+\r
+ public QueryWhere<T> lessThan(A y) {\r
+ z = y;\r
+ compareType = CompareType.LESS_THAN;\r
+ return new QueryWhere<T>(query);\r
+ }\r
+\r
+ public QueryWhere<T> atMost(A y) {\r
+ z = y;\r
+ compareType = CompareType.AT_MOST;\r
+ return new QueryWhere<T>(query);\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ public <T> void appendSQL(SQLStatement stat, Query<T> query) {\r
+ stat.appendSQL("(");\r
+ switch (bitwiseType) {\r
+ case AND:\r
+ query.getDb().getDialect().prepareBitwiseAnd(stat, query, x, y);\r
+ break;\r
+ case XOR:\r
+ query.getDb().getDialect().prepareBitwiseXor(stat, query, x, y);\r
+ break;\r
+ }\r
+ stat.appendSQL(")");\r
+ stat.appendSQL(compareType.getString());\r
+ stat.appendSQL("" + z);\r
+ }\r
+}\r
--- /dev/null
+/*
+ * 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);
+}
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;
+ }
+
}
*/
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);
+
}
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);
+ }
+
}
stat.setSQL(buff.toString());\r
return;\r
}\r
+\r
+ @Override\r
+ public <T, A> void prepareBitwiseAnd(SQLStatement stat, Query<T> query, A x, A y) {\r
+ throw new IciqlException("{0} does not support bitwise operator AND", databaseName);\r
+ }\r
+\r
+ @Override\r
+ public <T, A> void prepareBitwiseXor(SQLStatement stat, Query<T> query, A x, A y) {\r
+ throw new IciqlException("{0} does not support bitwise operator XOR", databaseName);\r
+ }\r
+\r
}
\ No newline at end of file
buff.append(')');\r
stat.setSQL(buff.toString());\r
}\r
+\r
+ @Override\r
+ public <T, A> void prepareBitwiseAnd(SQLStatement stat, Query<T> query, A x, A y) {\r
+ stat.appendSQL("BITAND(");\r
+ query.appendSQL(stat, null, x);\r
+ stat.appendSQL(",");\r
+ query.appendSQL(stat, x, y);\r
+ stat.appendSQL(")");\r
+ }\r
+\r
+ @Override\r
+ public <T, A> void prepareBitwiseXor(SQLStatement stat, Query<T> query, A x, A y) {\r
+ stat.appendSQL("BITXOR(");\r
+ query.appendSQL(stat, null, x);\r
+ stat.appendSQL(",");\r
+ query.appendSQL(stat, x, y);\r
+ stat.appendSQL(")");\r
+ }\r
+\r
}
\ No newline at end of file
buff.append(')');\r
stat.setSQL(buff.toString());\r
}\r
+\r
+ @Override\r
+ public <T, A> void prepareBitwiseAnd(SQLStatement stat, Query<T> query, A x, A y) {\r
+ stat.appendSQL("BITAND(");\r
+ query.appendSQL(stat, null, x);\r
+ stat.appendSQL(",");\r
+ query.appendSQL(stat, x, y);\r
+ stat.appendSQL(")");\r
+ }\r
+\r
+ @Override\r
+ public <T, A> void prepareBitwiseXor(SQLStatement stat, Query<T> query, A x, A y) {\r
+ stat.appendSQL("BITXOR(");\r
+ query.appendSQL(stat, null, x);\r
+ stat.appendSQL(",");\r
+ query.appendSQL(stat, x, y);\r
+ stat.appendSQL(")");\r
+ }\r
+\r
}
\ No newline at end of file
stat.setSQL(buff.toString());\r
}\r
\r
+ @Override\r
+ public <T, A> void prepareBitwiseXor(SQLStatement stat, Query<T> query, A x, A y) {\r
+ query.appendSQL(stat, null, x);\r
+ stat.appendSQL(" # ");\r
+ query.appendSQL(stat, x, y);\r
+ }\r
}
\ No newline at end of file
}\r
return super.prepareStringParameter(o);\r
}\r
+\r
+ @Override\r
+ public <T, A> void prepareBitwiseXor(SQLStatement stat, Query<T> query, A x, A y) {\r
+ throw new IciqlException("{0} does not support bitwise operator XOR", databaseName);\r
+ }\r
}\r
--- /dev/null
+/*
+ * 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)));
+ }
+
+}
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 = {