aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJames Moger <james.moger@gitblit.com>2015-04-16 19:47:27 -0400
committerJames Moger <james.moger@gitblit.com>2015-04-16 20:06:15 -0400
commite670abca93ddc6455ab75a01cfdd07abc3b3e9ad (patch)
treec92616711ce399142c69286c685fd834eca2359f /src
parent5f71b4c044a382b89bd05577ef6fed86ed593c31 (diff)
downloadiciql-e670abca93ddc6455ab75a01cfdd07abc3b3e9ad.tar.gz
iciql-e670abca93ddc6455ab75a01cfdd07abc3b3e9ad.zip
Use JavaType as the key for DataTypeAdapter registration
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/iciql/DaoProxy.java14
-rw-r--r--src/main/java/com/iciql/Query.java9
-rw-r--r--src/main/java/com/iciql/SQLDialect.java12
-rw-r--r--src/main/java/com/iciql/SQLDialectDefault.java33
-rw-r--r--src/main/java/com/iciql/SQLDialectH2.java2
-rw-r--r--src/main/java/com/iciql/SQLDialectHSQL.java2
-rw-r--r--src/main/java/com/iciql/SQLDialectMySQL.java2
-rw-r--r--src/main/java/com/iciql/SQLDialectSQLite.java9
-rw-r--r--src/main/java/com/iciql/TableDefinition.java13
9 files changed, 52 insertions, 44 deletions
diff --git a/src/main/java/com/iciql/DaoProxy.java b/src/main/java/com/iciql/DaoProxy.java
index 34187c4..deebeca 100644
--- a/src/main/java/com/iciql/DaoProxy.java
+++ b/src/main/java/com/iciql/DaoProxy.java
@@ -193,13 +193,18 @@ final class DaoProxy<X extends Dao> implements InvocationHandler, Dao {
} 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) {
@@ -683,8 +688,13 @@ final class DaoProxy<X extends Dao> implements InvocationHandler, Dao {
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);
}
diff --git a/src/main/java/com/iciql/Query.java b/src/main/java/com/iciql/Query.java
index d52451e..946a460 100644
--- a/src/main/java/com/iciql/Query.java
+++ b/src/main/java/com/iciql/Query.java
@@ -423,9 +423,13 @@ public class Query<T> {
ResultSet rs = stat.executeQuery();
List<X> result = Utils.newArrayList();
Class<? extends DataTypeAdapter<?>> typeAdapter = Utils.getDataTypeAdapter(x.getClass().getAnnotations());
+ if (typeAdapter != null) {
+ DataTypeAdapter<?> dta = Utils.newObject(typeAdapter);
+ db.getDialect().registerAdapter(dta);
+ }
try {
while (rs.next()) {
- X value = (X) db.getDialect().deserialize(rs, 1, x.getClass(), typeAdapter);
+ X value = (X) db.getDialect().deserialize(rs, 1, x.getClass());
result.add(value);
}
} catch (Exception e) {
@@ -840,8 +844,7 @@ public class Query<T> {
stat.addParameter(y);
} else if (col != null) {
// object
- Class<? extends DataTypeAdapter<?>> typeAdapter = col.getFieldDefinition().typeAdapter;
- Object parameter = db.getDialect().serialize(value, typeAdapter);
+ Object parameter = db.getDialect().serialize(value);
stat.addParameter(parameter);
} else {
// primitive
diff --git a/src/main/java/com/iciql/SQLDialect.java b/src/main/java/com/iciql/SQLDialect.java
index ccc0452..11b1af8 100644
--- a/src/main/java/com/iciql/SQLDialect.java
+++ b/src/main/java/com/iciql/SQLDialect.java
@@ -37,21 +37,20 @@ public interface SQLDialect {
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.
@@ -59,10 +58,9 @@ public interface SQLDialect {
* @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.
diff --git a/src/main/java/com/iciql/SQLDialectDefault.java b/src/main/java/com/iciql/SQLDialectDefault.java
index 8547f3c..7b4852d 100644
--- a/src/main/java/com/iciql/SQLDialectDefault.java
+++ b/src/main/java/com/iciql/SQLDialectDefault.java
@@ -55,10 +55,10 @@ public class SQLDialectDefault implements SQLDialect {
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
@@ -420,7 +420,7 @@ public class SQLDialectDefault implements SQLDialect {
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 ");
@@ -432,7 +432,7 @@ public class SQLDialectDefault implements SQLDialect {
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);
}
}
@@ -452,35 +452,33 @@ public class SQLDialectDefault implements SQLDialect {
@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 {
@@ -501,7 +499,6 @@ public class SQLDialectDefault implements SQLDialect {
return value;
} else {
// custom object deserialization with a DataTypeAdapter
- DataTypeAdapter<?> dta = getAdapter(typeAdapter);
Object object = null;
try {
object = rs.getObject(columnIndex);
diff --git a/src/main/java/com/iciql/SQLDialectH2.java b/src/main/java/com/iciql/SQLDialectH2.java
index 2d7d0fd..206937c 100644
--- a/src/main/java/com/iciql/SQLDialectH2.java
+++ b/src/main/java/com/iciql/SQLDialectH2.java
@@ -127,7 +127,7 @@ public class SQLDialectH2 extends SQLDialectDefault {
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(')');
diff --git a/src/main/java/com/iciql/SQLDialectHSQL.java b/src/main/java/com/iciql/SQLDialectHSQL.java
index 8b05ca4..4425667 100644
--- a/src/main/java/com/iciql/SQLDialectHSQL.java
+++ b/src/main/java/com/iciql/SQLDialectHSQL.java
@@ -91,7 +91,7 @@ public class SQLDialectHSQL extends SQLDialectDefault {
}
buff.append(')');
Object value = def.getValue(obj, field);
- Object parameter = serialize(value, field.typeAdapter);
+ Object parameter = serialize(value);
stat.addParameter(parameter);
}
diff --git a/src/main/java/com/iciql/SQLDialectMySQL.java b/src/main/java/com/iciql/SQLDialectMySQL.java
index ec5923f..ea19384 100644
--- a/src/main/java/com/iciql/SQLDialectMySQL.java
+++ b/src/main/java/com/iciql/SQLDialectMySQL.java
@@ -77,7 +77,7 @@ public class SQLDialectMySQL extends SQLDialectDefault {
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(") ON DUPLICATE KEY UPDATE ");
diff --git a/src/main/java/com/iciql/SQLDialectSQLite.java b/src/main/java/com/iciql/SQLDialectSQLite.java
index 4777234..28ae852 100644
--- a/src/main/java/com/iciql/SQLDialectSQLite.java
+++ b/src/main/java/com/iciql/SQLDialectSQLite.java
@@ -22,7 +22,6 @@ import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
-import com.iciql.Iciql.DataTypeAdapter;
import com.iciql.TableDefinition.FieldDefinition;
import com.iciql.TableDefinition.IndexDefinition;
import com.iciql.util.IciqlLogger;
@@ -135,7 +134,7 @@ public class SQLDialectSQLite extends SQLDialectDefault {
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(')');
@@ -143,11 +142,11 @@ public class SQLDialectSQLite extends SQLDialectDefault {
}
@Override
- public Object deserialize(ResultSet rs, int columnIndex, Class<?> targetType, Class<? extends DataTypeAdapter<?>> typeAdapter) {
+ public Object deserialize(ResultSet rs, int columnIndex, Class<?> targetType) {
try {
- return super.deserialize(rs, columnIndex, targetType, typeAdapter);
+ return super.deserialize(rs, columnIndex, targetType);
} catch (IciqlException e) {
- if (typeAdapter == null && e.getMessage().startsWith("Can not convert")) {
+ if (e.getMessage().startsWith("Can not convert")) {
try {
// give the SQLite JDBC driver an opportunity to deserialize DateTime objects
if (Timestamp.class.equals(targetType)) {
diff --git a/src/main/java/com/iciql/TableDefinition.java b/src/main/java/com/iciql/TableDefinition.java
index 8248f18..6ed9462 100644
--- a/src/main/java/com/iciql/TableDefinition.java
+++ b/src/main/java/com/iciql/TableDefinition.java
@@ -475,8 +475,9 @@ public class TableDefinition<T> {
}
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);
@@ -673,7 +674,7 @@ public class TableDefinition<T> {
// 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(')');
@@ -709,7 +710,7 @@ public class TableDefinition<T> {
// 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(')');
@@ -784,7 +785,7 @@ public class TableDefinition<T> {
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);
}
}
@@ -1177,7 +1178,7 @@ public class TableDefinition<T> {
}
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);
}