aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--releases.moxie1
-rw-r--r--src/main/java/com/iciql/util/Utils.java18
-rw-r--r--src/test/java/com/iciql/test/BooleanModelTest.java45
-rw-r--r--src/test/java/com/iciql/test/models/BooleanModel.java27
4 files changed, 87 insertions, 4 deletions
diff --git a/releases.moxie b/releases.moxie
index 53a19a0..36b9af6 100644
--- a/releases.moxie
+++ b/releases.moxie
@@ -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
diff --git a/src/main/java/com/iciql/util/Utils.java b/src/main/java/com/iciql/util/Utils.java
index 92ca825..64fe6b3 100644
--- a/src/main/java/com/iciql/util/Utils.java
+++ b/src/main/java/com/iciql/util/Utils.java
@@ -273,10 +273,22 @@ public class Utils {
// convert from boolean to number
if (Boolean.class.isAssignableFrom(currentType)) {
Boolean b = (Boolean) o;
+ Integer n = b ? 1 : 0;
if (Number.class.isAssignableFrom(targetType)) {
- return b ? 1 : 0;
- }
- if (boolean.class.isAssignableFrom(targetType)) {
+ return n.intValue();
+ } else if (byte.class.isAssignableFrom(targetType)) {
+ return n.byteValue();
+ } else if (short.class.isAssignableFrom(targetType)) {
+ return n.shortValue();
+ } else if (int.class.isAssignableFrom(targetType)) {
+ return n.intValue();
+ } else if (long.class.isAssignableFrom(targetType)) {
+ return n.longValue();
+ } else if (float.class.isAssignableFrom(targetType)) {
+ return n.floatValue();
+ } else if (double.class.isAssignableFrom(targetType)) {
+ return n.doubleValue();
+ } else if (boolean.class.isAssignableFrom(targetType)) {
return b.booleanValue();
}
}
diff --git a/src/test/java/com/iciql/test/BooleanModelTest.java b/src/test/java/com/iciql/test/BooleanModelTest.java
index 1e1630a..f5cd5e7 100644
--- a/src/test/java/com/iciql/test/BooleanModelTest.java
+++ b/src/test/java/com/iciql/test/BooleanModelTest.java
@@ -26,6 +26,7 @@ import org.junit.Test;
import com.iciql.Db;
import com.iciql.test.models.BooleanModel;
import com.iciql.test.models.BooleanModel.BooleanAsIntModel;
+import com.iciql.test.models.BooleanModel.BooleanAsPrimitiveShortModel;
/**
* Tests interchangeable mapping of INT columns with Booleans and BOOL columns
@@ -33,6 +34,7 @@ import com.iciql.test.models.BooleanModel.BooleanAsIntModel;
* <ul>
* <li>mapping a BIT/BOOLEAN column as an Integer
* <li>mapping a INT column as a Boolean.
+ * <li>mapping a BIT/BOOLEAN column as a primitive short
* </ul>
*/
public class BooleanModelTest {
@@ -125,4 +127,47 @@ public class BooleanModelTest {
}
db.close();
}
+
+ @Test
+ public void testPrimitiveShortBooleanColumn() {
+ Db db = IciqlSuite.openNewDb();
+ db.insertAll(BooleanModel.getList());
+ BooleanAsPrimitiveShortModel b = new BooleanAsPrimitiveShortModel();
+ List<BooleanAsPrimitiveShortModel> models = db.from(b).select();
+ int count = 0;
+ for (BooleanAsPrimitiveShortModel model : models) {
+ if ((model.id % 2) == 1) {
+ // assert that odd ids are true
+ assertTrue(model.mybool > 0);
+ } else {
+ // assert that even ids are false
+ assertTrue(model.mybool == 0);
+ }
+
+ // count true values
+ if (model.mybool > 0) {
+ count++;
+ }
+ }
+ assertEquals(2, count);
+
+ // invert boolean values and update
+ for (BooleanAsPrimitiveShortModel model : models) {
+ model.mybool = (short) (model.mybool > 0 ? 0 : 1);
+ }
+ db.updateAll(models);
+
+ // check even ids are true
+ models = db.from(b).select();
+ for (BooleanAsPrimitiveShortModel model : models) {
+ if ((model.id % 2) == 1) {
+ // assert that odd ids are false
+ assertTrue(model.mybool == 0);
+ } else {
+ // assert that even ids are true
+ assertTrue(model.mybool > 0);
+ }
+ }
+ db.close();
+ }
}
diff --git a/src/test/java/com/iciql/test/models/BooleanModel.java b/src/test/java/com/iciql/test/models/BooleanModel.java
index d22e3c1..4d75ad9 100644
--- a/src/test/java/com/iciql/test/models/BooleanModel.java
+++ b/src/test/java/com/iciql/test/models/BooleanModel.java
@@ -47,7 +47,7 @@ public class BooleanModel {
}
/**
- * Test boolean as int
+ * Test boolean as Integer
*/
@IQTable(name = "BooleanTest")
public static class BooleanAsIntModel {
@@ -70,4 +70,29 @@ public class BooleanModel {
new BooleanAsIntModel(3, true), new BooleanAsIntModel(4, false));
}
}
+
+ /**
+ * Test boolean as primitive short
+ */
+ @IQTable(name = "BooleanTest")
+ public static class BooleanAsPrimitiveShortModel {
+ @IQColumn(primaryKey = true)
+ public Integer id;
+
+ @IQColumn
+ public short mybool;
+
+ public BooleanAsPrimitiveShortModel() {
+ }
+
+ BooleanAsPrimitiveShortModel(int id, boolean val) {
+ this.id = id;
+ this.mybool = (short) (val ? 1 : 0);
+ }
+
+ public static List<BooleanAsPrimitiveShortModel> getList() {
+ return Arrays.asList(new BooleanAsPrimitiveShortModel(1, true), new BooleanAsPrimitiveShortModel(2, false),
+ new BooleanAsPrimitiveShortModel(3, true), new BooleanAsPrimitiveShortModel(4, false));
+ }
+ }
}