} else {
// query of (array of) standard Java type or a DataTypeAdapter type
+ if (adapter != null) {
+ DataTypeAdapter<?> dta = Utils.newObject(adapter);
+ db.getDialect().registerAdapter(dta);
+ }
+
objects = Utils.newArrayList();
ResultSet rs = db.executeQuery(preparedSql.sql, preparedSql.parameters);
try {
while (rs.next()) {
- Object value = db.getDialect().deserialize(rs, 1, returnType, adapter);
+ Object value = db.getDialect().deserialize(rs, 1, returnType);
objects.add(value);
if (!isArray) {
typeAdapter = Utils.getDataTypeAdapter(methodArg.getClass().getAnnotations());
}
+ if (typeAdapter != null) {
+ DataTypeAdapter<?> dta = Utils.newObject(typeAdapter);
+ db.getDialect().registerAdapter(dta);
+ }
+
// prepare the parameter
- parameters[i] = db.getDialect().serialize(value, typeAdapter);
+ parameters[i] = db.getDialect().serialize(value);
}
ResultSet rs = stat.executeQuery();\r
List<X> result = Utils.newArrayList();\r
Class<? extends DataTypeAdapter<?>> typeAdapter = Utils.getDataTypeAdapter(x.getClass().getAnnotations());\r
+ if (typeAdapter != null) {\r
+ DataTypeAdapter<?> dta = Utils.newObject(typeAdapter);\r
+ db.getDialect().registerAdapter(dta);\r
+ }\r
try {\r
while (rs.next()) {\r
- X value = (X) db.getDialect().deserialize(rs, 1, x.getClass(), typeAdapter);\r
+ X value = (X) db.getDialect().deserialize(rs, 1, x.getClass());\r
result.add(value);\r
}\r
} catch (Exception e) {\r
stat.addParameter(y);\r
} else if (col != null) {\r
// object\r
- Class<? extends DataTypeAdapter<?>> typeAdapter = col.getFieldDefinition().typeAdapter;\r
- Object parameter = db.getDialect().serialize(value, typeAdapter);\r
+ Object parameter = db.getDialect().serialize(value);\r
stat.addParameter(parameter);\r
} else {\r
// primitive\r
void registerAdapter(DataTypeAdapter<?> typeAdapter);
/**
- * Returns the registered instance of the type adapter.
+ * Returns the registered instance of the type adapter for the specified object class.
*
- * @param typeAdapter
+ * @param objectClass
* @return the type adapter instance
*/
- DataTypeAdapter<?> getAdapter(Class<? extends DataTypeAdapter<?>> typeAdapter);
+ DataTypeAdapter<?> getAdapter(Class<?> objectClass);
/**
* Serialize the Java object into a type or format that the database will accept.
*
* @param value
- * @param typeAdapter
* @return the serialized object
*/
- <T> Object serialize(T value, Class<? extends DataTypeAdapter<?>> typeAdapter);
+ <T> Object serialize(T value);
/**
* Deserialize the object received from the database into a Java type.
* @param rs
* @param columnIndex
* @param targetType
- * @param typeAdapter
* @return the deserialized object
*/
- Object deserialize(ResultSet rs, int columnIndex, Class<?> targetType, Class<? extends DataTypeAdapter<?>> typeAdapter);
+ Object deserialize(ResultSet rs, int columnIndex, Class<?> targetType);
/**
* Configure the dialect.
String databaseName;
String productVersion;
Mode mode;
- Map<Class<? extends DataTypeAdapter<?>>, DataTypeAdapter<?>> typeAdapters;
+ Map<Class<?>, DataTypeAdapter<?>> typeAdapters;
public SQLDialectDefault() {
- typeAdapters = new ConcurrentHashMap<Class<? extends DataTypeAdapter<?>>, DataTypeAdapter<?>>();
+ typeAdapters = new ConcurrentHashMap<Class<?>, DataTypeAdapter<?>>();
}
@Override
buff.appendExceptFirst(", ");
buff.append('?');
Object value = def.getValue(obj, field);
- Object parameter = serialize(value, field.typeAdapter);
+ Object parameter = serialize(value);
stat.addParameter(parameter);
}
buff.append(" FROM ");
buff.appendExceptFirst(" AND ");
buff.append(MessageFormat.format("{0} = ?", prepareColumnName(field.columnName)));
Object value = def.getValue(obj, field);
- Object parameter = serialize(value, field.typeAdapter);
+ Object parameter = serialize(value);
stat.addParameter(parameter);
}
}
@Override
public void registerAdapter(DataTypeAdapter<?> typeAdapter) {
- typeAdapters.put((Class<? extends DataTypeAdapter<?>>) typeAdapter.getClass(), typeAdapter);
+ typeAdapters.put(typeAdapter.getJavaType(), typeAdapter);
}
@Override
- public DataTypeAdapter<?> getAdapter(Class<? extends DataTypeAdapter<?>> typeAdapter) {
- DataTypeAdapter<?> dta = typeAdapters.get(typeAdapter);
- if (dta == null) {
- dta = Utils.newObject(typeAdapter);
- typeAdapters.put(typeAdapter, dta);
+ public DataTypeAdapter<?> getAdapter(Class<?> objectClass) {
+ DataTypeAdapter<?> dta = typeAdapters.get(objectClass);
+ if (dta != null) {
+ dta.setMode(mode);
}
- dta.setMode(mode);
return dta;
}
@SuppressWarnings("unchecked")
@Override
- public <T> Object serialize(T value, Class<? extends DataTypeAdapter<?>> typeAdapter) {
- if (typeAdapter == null) {
+ public <T> Object serialize(T value) {
+ DataTypeAdapter dta = getAdapter(value.getClass());
+ if (dta == null) {
// pass-through
return value;
}
-
- DataTypeAdapter<T> dta = (DataTypeAdapter<T>) getAdapter(typeAdapter);
return dta.serialize(value);
}
@Override
- public Object deserialize(ResultSet rs, int columnIndex, Class<?> targetType, Class<? extends DataTypeAdapter<?>> typeAdapter) {
- if (typeAdapter == null) {
+ public Object deserialize(ResultSet rs, int columnIndex, Class<?> targetType) {
+ DataTypeAdapter<?> dta = getAdapter(targetType);
+ if (dta == null) {
// standard object deserialization
Object value = null;
try {
return value;
} else {
// custom object deserialization with a DataTypeAdapter
- DataTypeAdapter<?> dta = getAdapter(typeAdapter);
Object object = null;
try {
object = rs.getObject(columnIndex);
buff.appendExceptFirst(", ");\r
buff.append('?');\r
Object value = def.getValue(obj, field);\r
- Object parameter = serialize(value, field.typeAdapter);\r
+ Object parameter = serialize(value);\r
stat.addParameter(parameter);\r
}\r
buff.append(')');\r
}\r
buff.append(')');\r
Object value = def.getValue(obj, field);\r
- Object parameter = serialize(value, field.typeAdapter);\r
+ Object parameter = serialize(value);\r
stat.addParameter(parameter);\r
}\r
\r
buff.appendExceptFirst(", ");\r
buff.append('?');\r
Object value = def.getValue(obj, field);\r
- Object parameter = serialize(value, field.typeAdapter);\r
+ Object parameter = serialize(value);\r
stat.addParameter(parameter);\r
}\r
buff.append(") ON DUPLICATE KEY UPDATE ");\r
import java.sql.Time;\r
import java.sql.Timestamp;\r
\r
-import com.iciql.Iciql.DataTypeAdapter;\r
import com.iciql.TableDefinition.FieldDefinition;\r
import com.iciql.TableDefinition.IndexDefinition;\r
import com.iciql.util.IciqlLogger;\r
buff.appendExceptFirst(", ");\r
buff.append('?');\r
Object value = def.getValue(obj, field);\r
- Object parameter = serialize(value, field.typeAdapter);\r
+ Object parameter = serialize(value);\r
stat.addParameter(parameter);\r
}\r
buff.append(')');\r
}\r
\r
@Override\r
- public Object deserialize(ResultSet rs, int columnIndex, Class<?> targetType, Class<? extends DataTypeAdapter<?>> typeAdapter) {\r
+ public Object deserialize(ResultSet rs, int columnIndex, Class<?> targetType) {\r
try {\r
- return super.deserialize(rs, columnIndex, targetType, typeAdapter);\r
+ return super.deserialize(rs, columnIndex, targetType);\r
} catch (IciqlException e) {\r
- if (typeAdapter == null && e.getMessage().startsWith("Can not convert")) {\r
+ if (e.getMessage().startsWith("Can not convert")) {\r
try {\r
// give the SQLite JDBC driver an opportunity to deserialize DateTime objects\r
if (Timestamp.class.equals(targetType)) {\r
}
if (typeAdapter != null) {
- DataTypeAdapter<?> dtt = db.getDialect().getAdapter(typeAdapter);
- dataType = dtt.getDataType();
+ DataTypeAdapter<?> dta = Utils.newObject(typeAdapter);
+ dataType = dta.getDataType();
+ db.getDialect().registerAdapter(dta);
}
boolean hasAnnotation = f.isAnnotationPresent(IQColumn.class);
// try to interpret and instantiate a default value
value = ModelUtils.getDefaultValue(field, db.getDialect().getDateTimeClass());
}
- Object parameter = db.getDialect().serialize(value, field.typeAdapter);
+ Object parameter = db.getDialect().serialize(value);
stat.addParameter(parameter);
}
buff.append(')');
// try to interpret and instantiate a default value
value = ModelUtils.getDefaultValue(field, db.getDialect().getDateTimeClass());
}
- Object parameter = db.getDialect().serialize(value, field.typeAdapter);
+ Object parameter = db.getDialect().serialize(value);
stat.addParameter(parameter);
}
buff.append(')');
buff.appendExceptFirst(", ");
buff.append(db.getDialect().prepareColumnName(field.columnName));
buff.append(" = ?");
- Object parameter = db.getDialect().serialize(value, field.typeAdapter);
+ Object parameter = db.getDialect().serialize(value);
stat.addParameter(parameter);
}
}
}
o = Utils.convertEnum(obj, targetType, def.enumType);
} else {
- o = dialect.deserialize(rs, columns[i], targetType, def.typeAdapter);
+ o = dialect.deserialize(rs, columns[i], targetType);
}
def.setValue(item, o);
}