From 9ac1b2a5ce32426615e7aecd4c18c113f79a15e3 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Thu, 20 Sep 2007 01:12:19 +0000 Subject: Fix support for char data type when used with an adapter. --- .../carbonado/repo/jdbc/JDBCStorableGenerator.java | 38 +++++++++++++-- .../repo/jdbc/JDBCStorableIntrospector.java | 55 ++-------------------- .../carbonado/repo/jdbc/JDBCStorableProperty.java | 19 -------- .../amazon/carbonado/repo/jdbc/JDBCStorage.java | 12 +++-- 4 files changed, 48 insertions(+), 76 deletions(-) (limited to 'src') diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableGenerator.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableGenerator.java index a41eebe..3f71d1c 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableGenerator.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableGenerator.java @@ -1511,7 +1511,13 @@ class JDBCStorableGenerator { } fromType = propertyType; } else { - Method adaptMethod = property.getAppliedAdapterFromMethod(); + Class toClass = psClass; + if (java.sql.Blob.class.isAssignableFrom(toClass)) { + toClass = com.amazon.carbonado.lob.Blob.class; + } else if (java.sql.Clob.class.isAssignableFrom(toClass)) { + toClass = com.amazon.carbonado.lob.Clob.class; + } + Method adaptMethod = adapter.findAdaptMethod(property.getType(), toClass); TypeDesc adaptType = TypeDesc.forClass(adaptMethod.getReturnType()); if (mode != INITIAL_VERSION) { // Invoke special inherited protected method that gets the field @@ -1765,7 +1771,9 @@ class JDBCStorableGenerator { b.ifComparisonBranch(target, branchIfDirty ? "==" : "!="); } - private void defineExtractAllMethod(Map, Class> lobLoaderMap) { + private void defineExtractAllMethod(Map, Class> lobLoaderMap) + throws SupportException + { MethodInfo mi = mClassFile.addMethod (Modifiers.PRIVATE, EXTRACT_ALL_METHOD_NAME, null, new TypeDesc[] {TypeDesc.forClass(ResultSet.class), TypeDesc.INT}); @@ -1791,7 +1799,9 @@ class JDBCStorableGenerator { b.returnVoid(); } - private void defineExtractDataMethod(Map, Class> lobLoaderMap) { + private void defineExtractDataMethod(Map, Class> lobLoaderMap) + throws SupportException + { MethodInfo mi = mClassFile.addMethod (Modifiers.PRIVATE, EXTRACT_DATA_METHOD_NAME, null, new TypeDesc[] {TypeDesc.forClass(ResultSet.class), TypeDesc.INT, @@ -1807,6 +1817,7 @@ class JDBCStorableGenerator { LocalVariable rsVar, LocalVariable initialOffsetVar, LocalVariable lobArrayVar, Iterable> properties, Map, Class> lobLoaderMap) + throws SupportException { LocalVariable offsetVar = null; int lobIndex = 0; @@ -1944,7 +1955,26 @@ class JDBCStorableGenerator { // Set protected field directly, since no adapter. b.storeField(superType, property.getName(), propertyType); } else { - Method adaptMethod = property.getAppliedAdapterToMethod(); + Method adaptMethod = adapter.findAdaptMethod + (resultSetType.toClass(), property.getType()); + + if (adaptMethod == null) { + if (resultSetType == TypeDesc.STRING) { + // Check if special case for converting String to character. + adaptMethod = adapter.findAdaptMethod(char.class, property.getType()); + if (adaptMethod == null) { + adaptMethod = adapter.findAdaptMethod + (Character.class, property.getType()); + } + } + + if (adaptMethod == null) { + throw new SupportException + ("Unable to adapt " + + resultSetType.toClass().getName() + " to " + property.getType()); + } + } + TypeDesc adaptType = TypeDesc.forClass(adaptMethod.getParameterTypes()[0]); convertFromResultSet(b, property, resultSetType, adaptType); wasNull.setLocation(); diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableIntrospector.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableIntrospector.java index dacb02f..27c0b13 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableIntrospector.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableIntrospector.java @@ -362,9 +362,7 @@ public class JDBCStorableIntrospector extends StorableIntrospector { autoIncrement, accessInfo.mResultSetGet, accessInfo.mPreparedStatementSet, - accessInfo.getAdapter(), - accessInfo.getAdapterFromMethod(), - accessInfo.getAdapterToMethod()); + accessInfo.getAdapter()); break findName; } @@ -533,13 +531,12 @@ public class JDBCStorableIntrospector extends StorableIntrospector { for (Method toMethod : toMethods) { Class fromType = toMethod.getParameterTypes()[0]; // Verify that reverse adapt method exists as well... - Method fromMethod = adapter.findAdaptMethod(property.getType(), fromType); - if (fromMethod != null) { + if (adapter.findAdaptMethod(property.getType(), fromType) != null) { // ...and try to get access info for fromType. info = getAccessInfo (fromType, dataType, dataTypeName, columnSize, decimalDigits); if (info != null) { - info.setAdapter(adapter, fromMethod, toMethod); + info.setAdapter(adapter); return info; } } @@ -917,8 +914,6 @@ public class JDBCStorableIntrospector extends StorableIntrospector { // Is null if no adapter needed. private StorablePropertyAdapter mAdapter; - private Method mAdapterFromMethod; - private Method mAdapterToMethod; AccessInfo(String suffix, Class actualClass) { try { @@ -934,18 +929,8 @@ public class JDBCStorableIntrospector extends StorableIntrospector { return mAdapter; } - void setAdapter(StorablePropertyAdapter adapter, Method fromMethod, Method toMethod) { + void setAdapter(StorablePropertyAdapter adapter) { mAdapter = adapter; - mAdapterFromMethod = fromMethod; - mAdapterToMethod = toMethod; - } - - Method getAdapterFromMethod() { - return mAdapterFromMethod; - } - - Method getAdapterToMethod() { - return mAdapterToMethod; } } @@ -1137,8 +1122,6 @@ public class JDBCStorableIntrospector extends StorableIntrospector { private final Method mResultSetGet; private final Method mPreparedStatementSet; private final StorablePropertyAdapter mAdapter; - private final Method mAdapterFromMethod; - private final Method mAdapterToMethod; private final Integer mColumnSize; private final Integer mDecimalDigits; private final Integer mCharOctetLength; @@ -1156,9 +1139,7 @@ public class JDBCStorableIntrospector extends StorableIntrospector { boolean autoIncrement, Method resultSetGet, Method preparedStatementSet, - StorablePropertyAdapter adapter, - Method adapterFromMethod, - Method adapterToMethod) + StorablePropertyAdapter adapter) { mMainProperty = mainProperty; mColumnName = columnInfo.columnName; @@ -1167,27 +1148,11 @@ public class JDBCStorableIntrospector extends StorableIntrospector { mResultSetGet = resultSetGet; mPreparedStatementSet = preparedStatementSet; mAdapter = adapter; - mAdapterFromMethod = adapterFromMethod; - mAdapterToMethod = adapterToMethod; mColumnSize = columnInfo.columnSize; mDecimalDigits = columnInfo.decimalDigits; mCharOctetLength = columnInfo.charOctetLength; mOrdinalPosition = columnInfo.ordinalPosition; mAutoIncrement = autoIncrement; - - if (adapterToMethod != null) { - if (!getType().isAssignableFrom(adapterToMethod.getReturnType())) { - throw new IllegalArgumentException - ("Illegal adapter \"to\" method: " + adapterToMethod); - } - } - - if (adapterFromMethod != null) { - if (!adapterFromMethod.getParameterTypes()[0].isAssignableFrom(getType())) { - throw new IllegalArgumentException - ("Illegal adapter \"from\" method: " + adapterFromMethod); - } - } } JProperty(StorableProperty mainProperty) { @@ -1198,8 +1163,6 @@ public class JDBCStorableIntrospector extends StorableIntrospector { mResultSetGet = null; mPreparedStatementSet = null; mAdapter = null; - mAdapterFromMethod = null; - mAdapterToMethod = null; mColumnSize = null; mDecimalDigits = null; mCharOctetLength = null; @@ -1364,14 +1327,6 @@ public class JDBCStorableIntrospector extends StorableIntrospector { return mAdapter; } - public Method getAppliedAdapterFromMethod() { - return mAdapterFromMethod; - } - - public Method getAppliedAdapterToMethod() { - return mAdapterToMethod; - } - public Integer getColumnSize() { return mColumnSize; } diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableProperty.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableProperty.java index 824e0fc..bbaf5fb 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableProperty.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableProperty.java @@ -98,25 +98,6 @@ public interface JDBCStorableProperty extends StorableProper */ StorablePropertyAdapter getAppliedAdapter(); - /** - * Returns the applied adapter method for converting from this property's - * actual type. The method's single argument type is the same as for this - * property. - * - * @return null if property is unsupported or if adapter not needed. - * @since 1.2 - */ - Method getAppliedAdapterFromMethod(); - - /** - * Returns the applied adapter method for converting to this property's - * actual type. The method's return type is the same as for this property. - * - * @return null if property is unsupported or if adapter not needed. - * @since 1.2 - */ - Method getAppliedAdapterToMethod(); - /** * The column size is either the maximum number of characters or the * numeric precision. diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java index a45c90a..25b0209 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java @@ -484,9 +484,15 @@ class JDBCStorage extends StandardQueryFactory JDBCStorableProperty jProperty = mRepository.getJDBCStorableProperty(property); - mPreparedStatementSetMethods[i] = jProperty.getPreparedStatementSetMethod(); - mAdapterMethods[i] = jProperty.getAppliedAdapterFromMethod(); - mAdapterInstances[i] = jProperty.getAppliedAdapter(); + Method psSetMethod = jProperty.getPreparedStatementSetMethod(); + mPreparedStatementSetMethods[i] = psSetMethod; + + StorablePropertyAdapter adapter = jProperty.getAppliedAdapter(); + if (adapter != null) { + Class toType = psSetMethod.getParameterTypes()[1]; + mAdapterMethods[i] = adapter.findAdaptMethod(jProperty.getType(), toType); + mAdapterInstances[i] = adapter.getAdapterInstance(); + } } } -- cgit v1.2.3