Author: cbegin Date: Sun Jun 10 15:19:10 2007 New Revision: 545954 URL: http://svn.apache.org/viewvc?view=rev&rev=545954 Log: improved type safety for parameter map config
Modified: ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlMapParser.java ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/ParameterMapConfig.java ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/SqlMapConfiguration.java ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/mapping/parameter/BasicParameterMap.java ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/mapping/parameter/BasicParameterMapping.java Modified: ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlMapParser.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlMapParser.java?view=diff&rev=545954&r1=545953&r2=545954 ============================================================================== --- ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlMapParser.java (original) +++ ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlMapParser.java Sun Jun 10 15:19:10 2007 @@ -143,7 +143,8 @@ private void addParameterMapNodelets() { parser.addNodelet("/sqlMap/parameterMap/end()", new Nodelet() { public void process(Node node) throws Exception { - state.getParamConfig().saveParameterMap(); + state.getConfig().getErrorContext().setMoreInfo(null); + state.getConfig().getErrorContext().setObjectId(null); state.setParamConfig(null); } }); @@ -152,8 +153,14 @@ Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps()); String id = state.applyNamespace(attributes.getProperty("id")); String parameterClassName = attributes.getProperty("class"); - ParameterMapConfig paramConf = state.getConfig().newParameterMapConfig(id, parameterClassName); - state.setParamConfig(paramConf); + parameterClassName = state.getConfig().getTypeHandlerFactory().resolveAlias(parameterClassName); + try { + state.getConfig().getErrorContext().setMoreInfo("Check the parameter class."); + ParameterMapConfig paramConf = state.getConfig().newParameterMapConfig(id, Resources.classForName(parameterClassName)); + state.setParamConfig(paramConf); + } catch (Exception e) { + throw new SqlMapException("Error configuring ParameterMap. Could not set ParameterClass. Cause: " + e, e); + } } }); parser.addNodelet("/sqlMap/parameterMap/parameter", new Nodelet() { @@ -167,8 +174,30 @@ String nullValue = childAttributes.getProperty("nullValue"); String mode = childAttributes.getProperty("mode"); String callback = childAttributes.getProperty("typeHandler"); - String numericScale = childAttributes.getProperty("numericScale"); - state.getParamConfig().addParameterMapping(propertyName, javaType, jdbcType, nullValue, mode, type, numericScale, callback, resultMap); + String numericScaleProp = childAttributes.getProperty("numericScale"); + + callback = state.getConfig().getTypeHandlerFactory().resolveAlias(callback); + Object typeHandlerImpl = null; + if (callback != null) { + typeHandlerImpl = Resources.instantiate(callback); + } + + javaType = state.getConfig().getTypeHandlerFactory().resolveAlias(javaType); + Class javaClass = null; + try { + if (javaType != null && javaType.length() > 0) { + javaClass = Resources.classForName(javaType); + } + } catch (ClassNotFoundException e) { + throw new RuntimeException("Error setting javaType on parameter mapping. Cause: " + e); + } + + Integer numericScale = null; + if (numericScaleProp != null) { + numericScale = new Integer(numericScaleProp); + } + + state.getParamConfig().addParameterMapping(propertyName, javaClass, jdbcType, nullValue, mode, type, numericScale, typeHandlerImpl, resultMap); } }); } Modified: ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/ParameterMapConfig.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/ParameterMapConfig.java?view=diff&rev=545954&r1=545953&r2=545954 ============================================================================== --- ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/ParameterMapConfig.java (original) +++ ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/ParameterMapConfig.java Sun Jun 10 15:19:10 2007 @@ -1,7 +1,5 @@ package com.ibatis.sqlmap.engine.conifg; -import com.ibatis.common.resources.*; -import com.ibatis.sqlmap.client.*; import com.ibatis.sqlmap.client.extensions.*; import com.ibatis.sqlmap.engine.impl.*; import com.ibatis.sqlmap.engine.mapping.parameter.*; @@ -11,66 +9,46 @@ import java.util.*; public class ParameterMapConfig { + public static final String MODE_IN = "IN"; + public static final String MODE_OUT = "OUT"; + public static final String MODE_INOUT = "INUOT"; + private SqlMapConfiguration config; private ErrorContext errorContext; private ExtendedSqlMapClient client; - private TypeHandlerFactory typeHandlerFactory; private BasicParameterMap parameterMap; private List parameterMappingList; - ParameterMapConfig(SqlMapConfiguration config, String id, String parameterClassName) { + ParameterMapConfig(SqlMapConfiguration config, String id, Class parameterClass) { this.config = config; this.errorContext = config.getErrorContext(); this.client = config.getClient(); - this.typeHandlerFactory = config.getTypeHandlerFactory(); errorContext.setActivity("building a parameter map"); parameterMap = new BasicParameterMap(client.getDelegate()); - parameterClassName = typeHandlerFactory.resolveAlias(parameterClassName); parameterMap.setId(id); parameterMap.setResource(errorContext.getResource()); errorContext.setObjectId(id + " parameter map"); - Class parameterClass; - try { - errorContext.setMoreInfo("Check the parameter class."); - parameterClass = Resources.classForName(parameterClassName); - } catch (Exception e) { - throw new SqlMapException("Error configuring ParameterMap. Could not set ParameterClass. Cause: " + e, e); - } parameterMap.setParameterClass(parameterClass); errorContext.setMoreInfo("Check the parameter mappings."); this.parameterMappingList = new ArrayList(); + client.getDelegate().addParameterMap(parameterMap); } - public void addParameterMapping(String propertyName, String javaType, String jdbcType, String nullValue, String mode, String outParamType, String numericScale, String typeHandlerCallback, String resultMap) { - typeHandlerCallback = typeHandlerFactory.resolveAlias(typeHandlerCallback); - javaType = typeHandlerFactory.resolveAlias(javaType); + public void addParameterMapping(String propertyName, Class javaClass, String jdbcType, String nullValue, String mode, String outParamType, Integer numericScale, Object typeHandlerImpl, String resultMap) { errorContext.setObjectId(propertyName + " mapping of the " + parameterMap.getId() + " parameter map"); TypeHandler handler; - if (typeHandlerCallback != null) { - errorContext.setMoreInfo("Check the parameter mapping typeHandler attribute '" + typeHandlerCallback + "' (must be a TypeHandler or TypeHandlerCallback implementation)."); - try { - Object impl = Resources.instantiate(typeHandlerCallback); - if (impl instanceof TypeHandlerCallback) { - handler = new CustomTypeHandler((TypeHandlerCallback) impl); - } else if (impl instanceof TypeHandler) { - handler = (TypeHandler) impl; - } else { - throw new RuntimeException("The class '" + typeHandlerCallback + "' is not a valid implementation of TypeHandler or TypeHandlerCallback"); - } - } catch (Exception e) { - throw new RuntimeException("Error occurred during custom type handler configuration. Cause: " + e, e); + if (typeHandlerImpl != null) { + errorContext.setMoreInfo("Check the parameter mapping typeHandler attribute '" + typeHandlerImpl + "' (must be a TypeHandler or TypeHandlerCallback implementation)."); + if (typeHandlerImpl instanceof TypeHandlerCallback) { + handler = new CustomTypeHandler((TypeHandlerCallback) typeHandlerImpl); + } else if (typeHandlerImpl instanceof TypeHandler) { + handler = (TypeHandler) typeHandlerImpl; + } else { + throw new RuntimeException("The class '" + typeHandlerImpl + "' is not a valid implementation of TypeHandler or TypeHandlerCallback"); } } else { errorContext.setMoreInfo("Check the parameter mapping property type or name."); - try { - Class javaClass = null; - if (javaType != null) { - javaClass = Resources.classForName(javaType); - } - handler = config.resolveTypeHandler(client.getDelegate().getTypeHandlerFactory(), parameterMap.getParameterClass(), propertyName, javaClass, jdbcType); - } catch (ClassNotFoundException e) { - throw new RuntimeException("Error setting type handler on parameter mapping. Cause: " + e); - } + handler = config.resolveTypeHandler(client.getDelegate().getTypeHandlerFactory(), parameterMap.getParameterClass(), propertyName, javaClass, jdbcType); } BasicParameterMapping mapping = new BasicParameterMapping(); mapping.setPropertyName(propertyName); @@ -82,33 +60,10 @@ mapping.setMode(mode); } mapping.setTypeHandler(handler); - try { - if (javaType != null && javaType.length() > 0) { - mapping.setJavaType(Resources.classForName(javaType)); - } - } catch (ClassNotFoundException e) { - throw new RuntimeException("Error setting javaType on parameter mapping. Cause: " + e); - } - if (numericScale != null) { - try { - Integer scale = Integer.valueOf(numericScale); - if (scale.intValue() < 0) { - throw new RuntimeException("Error setting numericScale on parameter mapping. Cause: scale must be greater than or equal to zero"); - } - mapping.setNumericScale(scale); - } catch (NumberFormatException e) { - throw new RuntimeException("Error setting numericScale on parameter mapping. Cause: " + numericScale + " is not a valid integer"); - } - } + mapping.setJavaType(javaClass); + mapping.setNumericScale(numericScale); parameterMappingList.add(mapping); - } - - public void saveParameterMap() { parameterMap.setParameterMappingList(parameterMappingList); - client.getDelegate().addParameterMap(parameterMap); - errorContext.setMoreInfo(null); - errorContext.setObjectId(null); } - } Modified: ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/SqlMapConfiguration.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/SqlMapConfiguration.java?view=diff&rev=545954&r1=545953&r2=545954 ============================================================================== --- ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/SqlMapConfiguration.java (original) +++ ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/SqlMapConfiguration.java Sun Jun 10 15:19:10 2007 @@ -133,8 +133,8 @@ return new CacheModelConfig(this, id, controller, readOnly, serialize); } - public ParameterMapConfig newParameterMapConfig(String id, String parameterClassName) { - return new ParameterMapConfig(this, id, parameterClassName); + public ParameterMapConfig newParameterMapConfig(String id, Class parameterClass) { + return new ParameterMapConfig(this, id, parameterClass); } public ResultMapConfig newResultMapConfig(String id, String resultClassName, String groupBy, String extended, String xmlName) { Modified: ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/mapping/parameter/BasicParameterMap.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/mapping/parameter/BasicParameterMap.java?view=diff&rev=545954&r1=545953&r2=545954 ============================================================================== --- ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/mapping/parameter/BasicParameterMap.java (original) +++ ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/mapping/parameter/BasicParameterMap.java Sun Jun 10 15:19:10 2007 @@ -83,6 +83,7 @@ public void setParameterMappingList(List parameterMappingList) { this.parameterMappings = (BasicParameterMapping[]) parameterMappingList.toArray(new BasicParameterMapping[parameterMappingList.size()]); + parameterMappingIndex.clear(); for (int i = 0; i < parameterMappings.length; i++) { parameterMappingIndex.put(parameterMappings[i].getPropertyName(), new Integer(i)); } Modified: ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/mapping/parameter/BasicParameterMapping.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/mapping/parameter/BasicParameterMapping.java?view=diff&rev=545954&r1=545953&r2=545954 ============================================================================== --- ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/mapping/parameter/BasicParameterMapping.java (original) +++ ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/mapping/parameter/BasicParameterMapping.java Sun Jun 10 15:19:10 2007 @@ -165,6 +165,9 @@ } public void setNumericScale(Integer numericScale) { + if (numericScale != null && numericScale.intValue() < 0) { + throw new RuntimeException("Error setting numericScale on parameter mapping. Cause: scale must be greater than or equal to zero"); + } this.numericScale = numericScale; }