]> source.dussan.org Git - iciql.git/commitdiff
Use JavaType as the key for DataTypeAdapter registration
authorJames Moger <james.moger@gitblit.com>
Thu, 16 Apr 2015 23:47:27 +0000 (19:47 -0400)
committerJames Moger <james.moger@gitblit.com>
Fri, 17 Apr 2015 00:06:15 +0000 (20:06 -0400)
src/main/java/com/iciql/DaoProxy.java
src/main/java/com/iciql/Query.java
src/main/java/com/iciql/SQLDialect.java
src/main/java/com/iciql/SQLDialectDefault.java
src/main/java/com/iciql/SQLDialectH2.java
src/main/java/com/iciql/SQLDialectHSQL.java
src/main/java/com/iciql/SQLDialectMySQL.java
src/main/java/com/iciql/SQLDialectSQLite.java
src/main/java/com/iciql/TableDefinition.java

index 34187c4388c28744713f72a9185b366d1bd0eaa0..deebecafc6ecd2d14d4ebdbe3a9859d0918f9477 100644 (file)
@@ -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);
 
                        }
 
index d52451eb6d427b0cb8c5c986bdde0e8add2a15bc..946a460c2bdfc8ba1011c2d06eca9174768278a7 100644 (file)
@@ -423,9 +423,13 @@ public class Query<T> {
                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
@@ -840,8 +844,7 @@ public class Query<T> {
                        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
index ccc04529fbeddffcd6c9d428fd76bc51c2670587..11b1af8c3f921d675e8bf269923fc0316dd2b3e0 100644 (file)
@@ -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.
index 8547f3c8a1a14cbfb16392df97d56db7726d2375..7b4852d16fbd566c8959a305dcf8106c49204311 100644 (file)
@@ -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);
index 2d7d0fd7039518b72d0fa79c1644921a251d1d0f..206937c8cd3c83d11ae3b2f29b603e21420cddc4 100644 (file)
@@ -127,7 +127,7 @@ public class SQLDialectH2 extends SQLDialectDefault {
                        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
index 8b05ca4b7f6ae1eb6af0dcbf462b778a5675fb69..44256671bbc1fa1d1fac2f6e8439447e568ea2d3 100644 (file)
@@ -91,7 +91,7 @@ public class SQLDialectHSQL extends SQLDialectDefault {
                        }\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
index ec5923f6eb7c53bbb3a8a84f35820676809a137d..ea19384a2416efdfdb47b9101b381fcfb3e8fe34 100644 (file)
@@ -77,7 +77,7 @@ public class SQLDialectMySQL extends SQLDialectDefault {
                        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
index 4777234324f11d1af56000dcba493e808ca4706d..28ae852639c0dde5bc09ea5fddca418e9e1c135b 100644 (file)
@@ -22,7 +22,6 @@ import java.sql.SQLException;
 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
@@ -135,7 +134,7 @@ public class SQLDialectSQLite extends SQLDialectDefault {
                        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
@@ -143,11 +142,11 @@ public class SQLDialectSQLite extends SQLDialectDefault {
        }\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
index 8248f187edf8f8c18446ddb04e8b897737039332..6ed94627ef1cfdac2c63b9bbb6b92614319a221b 100644 (file)
@@ -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);
                }