additions:
- Add syntax for IN and NOT IN (pr-7)
- Add support for nested AND/OR conditions (pr-8)
+ - Add support for mapping SQL BOOLEAN to primitive numeric types, not just object numeric types
dependencyChanges: ~
contributors:
- James Moger
// convert from boolean to number\r
if (Boolean.class.isAssignableFrom(currentType)) {\r
Boolean b = (Boolean) o;\r
+ Integer n = b ? 1 : 0;\r
if (Number.class.isAssignableFrom(targetType)) {\r
- return b ? 1 : 0;\r
- }\r
- if (boolean.class.isAssignableFrom(targetType)) {\r
+ return n.intValue();\r
+ } else if (byte.class.isAssignableFrom(targetType)) {\r
+ return n.byteValue();\r
+ } else if (short.class.isAssignableFrom(targetType)) {\r
+ return n.shortValue();\r
+ } else if (int.class.isAssignableFrom(targetType)) {\r
+ return n.intValue();\r
+ } else if (long.class.isAssignableFrom(targetType)) {\r
+ return n.longValue();\r
+ } else if (float.class.isAssignableFrom(targetType)) {\r
+ return n.floatValue();\r
+ } else if (double.class.isAssignableFrom(targetType)) {\r
+ return n.doubleValue();\r
+ } else if (boolean.class.isAssignableFrom(targetType)) {\r
return b.booleanValue();\r
}\r
}\r
import com.iciql.Db;\r
import com.iciql.test.models.BooleanModel;\r
import com.iciql.test.models.BooleanModel.BooleanAsIntModel;\r
+import com.iciql.test.models.BooleanModel.BooleanAsPrimitiveShortModel;\r
\r
/**\r
* Tests interchangeable mapping of INT columns with Booleans and BOOL columns\r
* <ul>\r
* <li>mapping a BIT/BOOLEAN column as an Integer\r
* <li>mapping a INT column as a Boolean.\r
+ * <li>mapping a BIT/BOOLEAN column as a primitive short\r
* </ul>\r
*/\r
public class BooleanModelTest {\r
}\r
db.close();\r
}\r
+\r
+ @Test\r
+ public void testPrimitiveShortBooleanColumn() {\r
+ Db db = IciqlSuite.openNewDb();\r
+ db.insertAll(BooleanModel.getList());\r
+ BooleanAsPrimitiveShortModel b = new BooleanAsPrimitiveShortModel();\r
+ List<BooleanAsPrimitiveShortModel> models = db.from(b).select();\r
+ int count = 0;\r
+ for (BooleanAsPrimitiveShortModel model : models) {\r
+ if ((model.id % 2) == 1) {\r
+ // assert that odd ids are true\r
+ assertTrue(model.mybool > 0);\r
+ } else {\r
+ // assert that even ids are false\r
+ assertTrue(model.mybool == 0);\r
+ }\r
+\r
+ // count true values\r
+ if (model.mybool > 0) {\r
+ count++;\r
+ }\r
+ }\r
+ assertEquals(2, count);\r
+\r
+ // invert boolean values and update\r
+ for (BooleanAsPrimitiveShortModel model : models) {\r
+ model.mybool = (short) (model.mybool > 0 ? 0 : 1);\r
+ }\r
+ db.updateAll(models);\r
+\r
+ // check even ids are true\r
+ models = db.from(b).select();\r
+ for (BooleanAsPrimitiveShortModel model : models) {\r
+ if ((model.id % 2) == 1) {\r
+ // assert that odd ids are false\r
+ assertTrue(model.mybool == 0);\r
+ } else {\r
+ // assert that even ids are true\r
+ assertTrue(model.mybool > 0);\r
+ }\r
+ }\r
+ db.close();\r
+ }\r
}\r
}\r
\r
/**\r
- * Test boolean as int\r
+ * Test boolean as Integer\r
*/\r
@IQTable(name = "BooleanTest")\r
public static class BooleanAsIntModel {\r
new BooleanAsIntModel(3, true), new BooleanAsIntModel(4, false));\r
}\r
}\r
+\r
+ /**\r
+ * Test boolean as primitive short\r
+ */\r
+ @IQTable(name = "BooleanTest")\r
+ public static class BooleanAsPrimitiveShortModel {\r
+ @IQColumn(primaryKey = true)\r
+ public Integer id;\r
+\r
+ @IQColumn\r
+ public short mybool;\r
+\r
+ public BooleanAsPrimitiveShortModel() {\r
+ }\r
+\r
+ BooleanAsPrimitiveShortModel(int id, boolean val) {\r
+ this.id = id;\r
+ this.mybool = (short) (val ? 1 : 0);\r
+ }\r
+\r
+ public static List<BooleanAsPrimitiveShortModel> getList() {\r
+ return Arrays.asList(new BooleanAsPrimitiveShortModel(1, true), new BooleanAsPrimitiveShortModel(2, false),\r
+ new BooleanAsPrimitiveShortModel(3, true), new BooleanAsPrimitiveShortModel(4, false));\r
+ }\r
+ }\r
}\r