m.put(java.sql.Date.class, "DATE");
m.put(java.sql.Time.class, "TIME");
m.put(byte[].class, "BLOB");
+
+ // map primitives
+ m.put(boolean.class, m.get(Boolean.class));
+ m.put(byte.class, m.get(Byte.class));
+ m.put(short.class, m.get(Short.class));
+ m.put(int.class, m.get(Integer.class));
+ m.put(long.class, m.get(Long.class));
+ m.put(float.class, m.get(Float.class));
+ m.put(double.class, m.get(Double.class));
}
/**
@SuppressWarnings({ "unchecked", "rawtypes" })\r
public static <T> T newObject(Class<T> clazz) {\r
// must create new instances\r
- if (clazz == Integer.class) {\r
+ if (clazz == int.class || clazz == Integer.class) {\r
return (T) new Integer((int) COUNTER.getAndIncrement());\r
} else if (clazz == String.class) {\r
return (T) ("" + COUNTER.getAndIncrement());\r
- } else if (clazz == Long.class) {\r
+ } else if (clazz == long.class || clazz == Long.class) {\r
return (T) new Long(COUNTER.getAndIncrement());\r
- } else if (clazz == Short.class) {\r
+ } else if (clazz == short.class || clazz == Short.class) {\r
return (T) new Short((short) COUNTER.getAndIncrement());\r
- } else if (clazz == Byte.class) {\r
+ } else if (clazz == byte.class || clazz == Byte.class) {\r
return (T) new Byte((byte) COUNTER.getAndIncrement());\r
- } else if (clazz == Float.class) {\r
+ } else if (clazz == float.class || clazz == Float.class) {\r
return (T) new Float(COUNTER.getAndIncrement());\r
- } else if (clazz == Double.class) {\r
+ } else if (clazz == double.class || clazz == Double.class) {\r
return (T) new Double(COUNTER.getAndIncrement());\r
- } else if (clazz == Boolean.class) {\r
+ } else if (clazz == boolean.class || clazz == Boolean.class) {\r
return (T) new Boolean(false);\r
} else if (clazz == BigDecimal.class) {\r
return (T) new BigDecimal(COUNTER.getAndIncrement());\r
}\r
\r
// convert from number to boolean\r
- if (Boolean.class.isAssignableFrom(targetType)) {\r
+ if (Boolean.class.isAssignableFrom(targetType) || boolean.class.isAssignableFrom(targetType)) {\r
if (Number.class.isAssignableFrom(currentType)) {\r
Number n = (Number) o;\r
return n.intValue() > 0;\r
\r
// convert from boolean to number\r
if (Boolean.class.isAssignableFrom(currentType)) {\r
+ Boolean b = (Boolean) o;\r
if (Number.class.isAssignableFrom(targetType)) {\r
- Boolean b = (Boolean) o;\r
return b ? 1 : 0;\r
}\r
+ if (boolean.class.isAssignableFrom(targetType)) {\r
+ return b.booleanValue();\r
+ }\r
}\r
\r
// convert from number to number\r
if (Number.class.isAssignableFrom(currentType)) {\r
Number n = (Number) o;\r
- if (targetType == Byte.class) {\r
+ if (targetType == byte.class || targetType == Byte.class) {\r
return n.byteValue();\r
- } else if (targetType == Short.class) {\r
+ } else if (targetType == short.class || targetType == Short.class) {\r
return n.shortValue();\r
- } else if (targetType == Integer.class) {\r
+ } else if (targetType == int.class || targetType == Integer.class) {\r
return n.intValue();\r
- } else if (targetType == Long.class) {\r
+ } else if (targetType == long.class || targetType == Long.class) {\r
return n.longValue();\r
- } else if (targetType == Double.class) {\r
+ } else if (targetType == double.class || targetType == Double.class) {\r
return n.doubleValue();\r
- } else if (targetType == Float.class) {\r
+ } else if (targetType == float.class || targetType == Float.class) {\r
return n.floatValue();\r
}\r
}\r
--- /dev/null
+/*\r
+ * Copyright 2011 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.test;\r
+\r
+import static org.junit.Assert.assertTrue;\r
+\r
+import org.junit.Test;\r
+\r
+import com.iciql.Db;\r
+import com.iciql.test.models.PrimitivesModel;\r
+\r
+/**\r
+ * Tests primitives with autoboxing within the framework.\r
+ */\r
+public class PrimitivesTest {\r
+\r
+ @Test\r
+ public void testPrimitives() {\r
+ Db db = Db.open("jdbc:h2:mem:", "sa", "sa");\r
+\r
+ // insert random model\r
+ PrimitivesModel model = new PrimitivesModel();\r
+ db.insert(model);\r
+\r
+ PrimitivesModel p = new PrimitivesModel();\r
+ \r
+ // retrieve model and compare\r
+ PrimitivesModel retrievedModel = db.from(p).selectFirst();\r
+ assertTrue(model.equivalentTo(retrievedModel));\r
+\r
+ // retrieve with conditions and compare \r
+// StatementLogger.activateConsoleLogger();\r
+// retrievedModel = db.from(p).where(p.myLong).is(model.myLong).and(p.myInteger).is(model.myInteger)\r
+// .selectFirst();\r
+// assertTrue(model.equivalentTo(retrievedModel));\r
+// \r
+// // update myInteger and compare\r
+// db.from(p).set(p.myInteger).to(10).where(p.myLong).is(model.myLong).update();\r
+// retrievedModel = db.from(p).selectFirst();\r
+ \r
+// assertEquals(10, retrievedModel.myInteger);\r
+\r
+ db.close();\r
+ }\r
+}\r
--- /dev/null
+/*\r
+ * Copyright 2011 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
+package com.iciql.test.models;\r
+\r
+import java.util.Random;\r
+\r
+import com.iciql.Iciql.IQColumn;\r
+import com.iciql.Iciql.IQTable;\r
+\r
+/**\r
+ * Primitive types model.\r
+ */\r
+@IQTable(name = "PrimitivesTest")\r
+public class PrimitivesModel {\r
+\r
+ @IQColumn(primaryKey = true)\r
+ public long myLong;\r
+\r
+ @IQColumn\r
+ public int myInteger;\r
+\r
+ @IQColumn\r
+ public short myShort;\r
+\r
+ @IQColumn\r
+ public byte myByte;\r
+\r
+ @IQColumn\r
+ public boolean myBoolean;\r
+\r
+ @IQColumn\r
+ public double myDouble;\r
+\r
+ @IQColumn\r
+ public float myFloat;\r
+\r
+ public PrimitivesModel() {\r
+ Random rand = new Random();\r
+ myLong = rand.nextLong();\r
+ myInteger = rand.nextInt();\r
+ myShort = (short) rand.nextInt(Short.MAX_VALUE);\r
+ myByte = (byte) rand.nextInt(Byte.MAX_VALUE);\r
+ myBoolean = rand.nextInt(1) == 1;\r
+ myDouble = rand.nextDouble();\r
+ myFloat = rand.nextFloat();\r
+ }\r
+\r
+ public boolean equivalentTo(PrimitivesModel p) {\r
+ boolean same = true;\r
+ same &= myLong == p.myLong;\r
+ same &= myInteger == p.myInteger;\r
+ same &= myShort == p.myShort;\r
+ same &= myByte == p.myByte;\r
+ same &= myBoolean == p.myBoolean;\r
+ same &= myDouble == p.myDouble;\r
+ same &= myFloat == p.myFloat;\r
+ return same;\r
+ }\r
+}\r