import java.util.regex.Pattern;
import com.iciql.Iciql.DataTypeAdapter;
-import com.iciql.Iciql.TypeAdapter;
import com.iciql.util.JdbcUtils;
import com.iciql.util.StringUtils;
import com.iciql.util.Utils;
|| java.util.Date.class.isAssignableFrom(returnType)
|| byte[].class.isAssignableFrom(returnType);
- // determine the return type adapter, if any
- DataTypeAdapter<?> adapter = null;
- for (Annotation annotation : method.getAnnotations()) {
- if (annotation.annotationType() == TypeAdapter.class) {
- TypeAdapter typeAdapter = (TypeAdapter) annotation;
- adapter = db.getDialect().getAdapter(typeAdapter.value());
- break;
- } else if (annotation.annotationType().isAnnotationPresent(TypeAdapter.class)) {
- TypeAdapter typeAdapter = annotation.annotationType().getAnnotation(TypeAdapter.class);
- adapter = db.getDialect().getAdapter(typeAdapter.value());
- break;
- }
+ Class<? extends DataTypeAdapter<?>> adapter = Utils.getDataTypeAdapter(method.getAnnotations());
+ if (adapter == null) {
+ adapter = Utils.getDataTypeAdapter(returnType.getAnnotations());
}
/*
while (rs.next()) {
- Object o = rs.getObject(1);
- Object value;
-
- if (adapter == null) {
- // use internal Iciql type conversion
- value = Utils.convert(o, returnType);
- } else {
- // use the type adapter to convert the JDBC object to a domain type
- value = adapter.deserialize(o);
- }
-
+ Object value = db.getDialect().deserialize(rs, 1, returnType, adapter);
objects.add(value);
if (!isArray) {
}
// prepare the parameter
- parameters[i] = prepareParameter(db, value, typeAdapter);
+ parameters[i] = db.getDialect().serialize(value, typeAdapter);
}
}
- /**
- * Prepares a method argument to an sql parameter for transmission to a
- * database.
- *
- * @param db
- * @param arg
- * @param typeAdapter
- * @return a prepared parameter
- */
- private Object prepareParameter(Db db, Object arg, Class<? extends DataTypeAdapter<?>> typeAdapter) {
-
- if (typeAdapter != null) {
-
- // use a type adapter to serialize the method argument
- Object o = db.getDialect().serialize(arg, typeAdapter);
- return o;
-
- } else {
-
- // use the method argument
- return arg;
-
- }
-
- }
-
@Override
public String toString() {
return sql;
package com.iciql;\r
\r
import java.lang.reflect.Field;\r
-import java.sql.Blob;\r
-import java.sql.Clob;\r
import java.sql.ResultSet;\r
import java.sql.SQLException;\r
import java.util.ArrayList;\r
import java.util.IdentityHashMap;\r
import java.util.List;\r
\r
+import com.iciql.Iciql.DataTypeAdapter;\r
import com.iciql.Iciql.EnumType;\r
import com.iciql.NestedConditions.And;\r
import com.iciql.NestedConditions.Or;\r
return select(true);\r
}\r
\r
- @SuppressWarnings("unchecked")\r
public <X, Z> X selectFirst(Z x) {\r
List<X> list = limit(1).select(x);\r
return list.isEmpty() ? null : list.get(0);\r
appendFromWhere(stat);\r
ResultSet rs = stat.executeQuery();\r
List<X> result = Utils.newArrayList();\r
+ Class<? extends DataTypeAdapter<?>> typeAdapter = Utils.getDataTypeAdapter(x.getClass().getAnnotations());\r
try {\r
while (rs.next()) {\r
- X value;\r
- Object o = rs.getObject(1);\r
- // Convert CLOB and BLOB now because we close the resultset\r
- if (Clob.class.isAssignableFrom(o.getClass())) {\r
- value = (X) Utils.convert(o, String.class);\r
- } else if (Blob.class.isAssignableFrom(o.getClass())) {\r
- value = (X) Utils.convert(o, byte[].class);\r
- } else {\r
- value = (X) o;\r
- }\r
+ X value = (X) db.getDialect().deserialize(rs, 1, x.getClass(), typeAdapter);\r
result.add(value);\r
}\r
} catch (Exception e) {\r
stat.addParameter(y);\r
} else if (col != null) {\r
// object\r
- Object parameter = db.getDialect().serialize(value, col.getFieldDefinition().typeAdapter);\r
+ Class<? extends DataTypeAdapter<?>> typeAdapter = col.getFieldDefinition().typeAdapter;\r
+ Object parameter = db.getDialect().serialize(value, typeAdapter);\r
stat.addParameter(parameter);\r
} else {\r
// primitive\r
package com.iciql;
+import java.sql.ResultSet;
+
import com.iciql.Iciql.DataTypeAdapter;
import com.iciql.TableDefinition.IndexDefinition;
/**
* Deserialize the object received from the database into a Java type.
*
- * @param value
+ * @param rs
+ * @param columnIndex
+ * @param targetType
* @param typeAdapter
* @return the deserialized object
*/
- Object deserialize(Object value, Class<? extends DataTypeAdapter<?>> typeAdapter);
+ Object deserialize(ResultSet rs, int columnIndex, Class<?> targetType, Class<? extends DataTypeAdapter<?>> typeAdapter);
/**
* Configure the dialect.
package com.iciql;
+import java.sql.Blob;
+import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
}
@Override
- public Object deserialize(Object value, Class<? extends DataTypeAdapter<?>> typeAdapter) {
+ public Object deserialize(ResultSet rs, int columnIndex, Class<?> targetType, Class<? extends DataTypeAdapter<?>> typeAdapter) {
if (typeAdapter == null) {
- // pass-through
+ // standard object deserialization
+ Object value = null;
+ try {
+ // use internal Iciql type conversion
+ Object o = rs.getObject(columnIndex);
+ if (Clob.class.isAssignableFrom(o.getClass())) {
+ value = Utils.convert(o, String.class);
+ } else if (Blob.class.isAssignableFrom(o.getClass())) {
+ value = Utils.convert(o, byte[].class);
+ } else {
+ value = Utils.convert(o, targetType);
+ }
+ } catch (SQLException e) {
+ throw new IciqlException(e, "Can not convert the value at column {0} to {1}",
+ columnIndex, targetType.getName());
+ }
+
return value;
- }
+ } else {
+ // custom object deserialization with a DataTypeAdapter
+ DataTypeAdapter<?> dta = getAdapter(typeAdapter);
+ Object object = null;
+ try {
+ object = rs.getObject(columnIndex);
+ } catch (SQLException e) {
+ throw new IciqlException(e, "Can not convert the value at column {0} to {1}",
+ columnIndex, targetType.getName());
+ }
- DataTypeAdapter<?> dta = getAdapter(typeAdapter);
- return dta.deserialize(value);
+ Object value = dta.deserialize(object);
+ return value;
+ }
}
@Override
import com.iciql.Iciql.IQContraintUnique;
import com.iciql.Iciql.IQContraintsForeignKey;
import com.iciql.Iciql.IQContraintsUnique;
-import com.iciql.Iciql.IQEnum;
import com.iciql.Iciql.IQIgnore;
import com.iciql.Iciql.IQIndex;
import com.iciql.Iciql.IQIndexes;
private Object initWithNewObject(Object obj) {
Object o = Utils.newObject(field.getType());
- setValue(null, obj, o);
+ setValue(obj, o);
return o;
}
- private void setValue(SQLDialect dialect, Object obj, Object o) {
+ private void setValue(Object obj, Object o) {
try {
if (!field.isAccessible()) {
field.setAccessible(true);
}
- Class<?> targetType = field.getType();
- if (targetType.isEnum()) {
- o = Utils.convertEnum(o, targetType, enumType);
- } else if (dialect != null && typeAdapter != null) {
- o = dialect.deserialize(o, typeAdapter);
- } else {
- o = Utils.convert(o, targetType);
- }
- if (targetType.isPrimitive() && o == null) {
+ if (field.getType().isPrimitive() && o == null) {
// do not attempt to set a primitive to null
return;
}
}
}
- private Object read(ResultSet rs, int columnIndex) {
- try {
- return rs.getObject(columnIndex);
- } catch (SQLException e) {
- throw new IciqlException(e);
- }
- }
-
@Override
public int hashCode() {
return columnName.hashCode();
int scale = 0;
boolean trim = false;
boolean nullable = !f.getType().isPrimitive();
- EnumType enumType = null;
- Class<?> enumTypeClass = null;
String defaultValue = "";
String constraint = "";
String dataType = null;
Class<? extends DataTypeAdapter<?>> typeAdapter = null;
// configure Java -> SQL enum mapping
- if (f.getType().isEnum()) {
- enumType = EnumType.DEFAULT_TYPE;
- if (f.getType().isAnnotationPresent(IQEnum.class)) {
- // enum definition is annotated for all instances
- IQEnum iqenum = f.getType().getAnnotation(IQEnum.class);
- enumType = iqenum.value();
- }
- if (f.isAnnotationPresent(IQEnum.class)) {
- // this instance of the enum is annotated
- IQEnum iqenum = f.getAnnotation(IQEnum.class);
- enumType = iqenum.value();
- }
-
- if (EnumId.class.isAssignableFrom(f.getType())) {
- // custom enumid mapping
- enumTypeClass = ((EnumId<?>) f.getType().getEnumConstants()[0]).enumIdClass();
- }
- }
+ EnumType enumType = Utils.getEnumType(f);
+ Class<?> enumTypeClass = Utils.getEnumTypeClass(f);
// try using default object
try {
void readRow(SQLDialect dialect, Object item, ResultSet rs, int[] columns) {
for (int i = 0; i < fields.size(); i++) {
FieldDefinition def = fields.get(i);
- int index = columns[i];
- Object o = def.read(rs, index);
- def.setValue(dialect, item, o);
+ Class<?> targetType = def.field.getType();
+ Object o;
+ if (targetType.isEnum()) {
+ Object obj;
+ try {
+ obj = rs.getObject(columns[i]);
+ } catch (SQLException e) {
+ throw new IciqlException(e);
+ }
+ o = Utils.convertEnum(obj, targetType, def.enumType);
+ } else {
+ o = dialect.deserialize(rs, columns[i], targetType, def.typeAdapter);
+ }
+ def.setValue(item, o);
}
}
import java.io.StringWriter;\r
import java.lang.annotation.Annotation;\r
import java.lang.reflect.Constructor;\r
+import java.lang.reflect.Field;\r
import java.math.BigDecimal;\r
import java.math.BigInteger;\r
import java.sql.Blob;\r
import com.iciql.Iciql.DataTypeAdapter;\r
import com.iciql.Iciql.EnumId;\r
import com.iciql.Iciql.EnumType;\r
+import com.iciql.Iciql.IQEnum;\r
import com.iciql.Iciql.TypeAdapter;\r
import com.iciql.IciqlException;\r
\r
throw new IciqlException("Can not convert the value {0} from {1} to {2}", o, currentType, targetType);\r
}\r
\r
+ /**\r
+ * Identify the EnumType for the field.\r
+ *\r
+ * @param f\r
+ * @return null or the EnumType\r
+ */\r
+ public static EnumType getEnumType(Field f) {\r
+ EnumType enumType = null;\r
+ if (f.getType().isEnum()) {\r
+ enumType = EnumType.DEFAULT_TYPE;\r
+ if (f.getType().isAnnotationPresent(IQEnum.class)) {\r
+ // enum definition is annotated for all instances\r
+ IQEnum iqenum = f.getType().getAnnotation(IQEnum.class);\r
+ enumType = iqenum.value();\r
+ }\r
+ if (f.isAnnotationPresent(IQEnum.class)) {\r
+ // this instance of the enum is annotated\r
+ IQEnum iqenum = f.getAnnotation(IQEnum.class);\r
+ enumType = iqenum.value();\r
+ }\r
+ }\r
+ return enumType;\r
+ }\r
+\r
+ /**\r
+ * Identify the EnumType from the annotations.\r
+ *\r
+ * @param annotations\r
+ * @return null or the EnumType\r
+ */\r
+ public static EnumType getEnumType(Annotation [] annotations) {\r
+ EnumType enumType = null;\r
+ if (annotations != null) {\r
+ for (Annotation annotation : annotations) {\r
+ if (annotation instanceof IQEnum) {\r
+ enumType = ((IQEnum) annotation).value();\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ return enumType;\r
+ }\r
+\r
+ public static Class<?> getEnumTypeClass(Field f) {\r
+ if (f.getType().isEnum()) {\r
+ if (EnumId.class.isAssignableFrom(f.getType())) {\r
+ // custom enumid mapping\r
+ return ((EnumId<?>) f.getType().getEnumConstants()[0]).enumIdClass();\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+\r
public static Object convertEnum(Enum<?> o, EnumType type) {\r
if (o == null) {\r
return null;\r
* Identify the data type adapter class in the annotations.\r
*\r
* @param annotations\r
- * @return null or the dtaa type adapter class\r
+ * @return null or the data type adapter class\r
*/\r
public static Class<? extends DataTypeAdapter<?>> getDataTypeAdapter(Annotation [] annotations) {\r
Class<? extends DataTypeAdapter<?>> typeAdapter = null;\r