]> source.dussan.org Git - iciql.git/commitdiff
Support mapping SQL BOOLEAN -> primitive numeric types
authorJames Moger <james.moger@gitblit.com>
Mon, 6 Oct 2014 16:12:41 +0000 (12:12 -0400)
committerJames Moger <james.moger@gitblit.com>
Mon, 6 Oct 2014 16:12:41 +0000 (12:12 -0400)
releases.moxie
src/main/java/com/iciql/util/Utils.java
src/test/java/com/iciql/test/BooleanModelTest.java
src/test/java/com/iciql/test/models/BooleanModel.java

index 53a19a015772a10f8caca183819cf881c82112cc..36b9af6a193b3626f108a3fb59e03ed903e68398 100644 (file)
@@ -17,6 +17,7 @@ r21: {
     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
index 92ca8252fa12488432863193579cd2eb76724b4d..64fe6b3cd6671ec254237ae0cbbb1a539c08b83f 100644 (file)
@@ -273,10 +273,22 @@ public class Utils {
                // 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
index 1e1630a16fde3c13fd21c3202c5b27088efbc5fc..f5cd5e7d4f2a3a94543603911b84fdd0af3ff681 100644 (file)
@@ -26,6 +26,7 @@ import org.junit.Test;
 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
@@ -33,6 +34,7 @@ import com.iciql.test.models.BooleanModel.BooleanAsIntModel;
  * <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
@@ -125,4 +127,47 @@ public class BooleanModelTest {
                }\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
index d22e3c11d39eeeb7058927fff59a431a1ef25c73..4d75ad9f736641aac03165c414617541510e1099 100644 (file)
@@ -47,7 +47,7 @@ public class BooleanModel {
        }\r
 \r
        /**\r
-        * Test boolean as int\r
+        * Test boolean as Integer\r
         */\r
        @IQTable(name = "BooleanTest")\r
        public static class BooleanAsIntModel {\r
@@ -70,4 +70,29 @@ public class BooleanModel {
                                        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