Modified: ibatis/trunk/java/ibatis-3/ibatis-3-monarch/src/main/java/org/apache/ibatis/monarch/builder/MapperParser.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-monarch/src/main/java/org/apache/ibatis/monarch/builder/MapperParser.java?rev=691430&r1=691429&r2=691430&view=diff ============================================================================== --- ibatis/trunk/java/ibatis-3/ibatis-3-monarch/src/main/java/org/apache/ibatis/monarch/builder/MapperParser.java (original) +++ ibatis/trunk/java/ibatis-3/ibatis-3-monarch/src/main/java/org/apache/ibatis/monarch/builder/MapperParser.java Tue Sep 2 16:28:04 2008 @@ -10,6 +10,9 @@ public class MapperParser extends BaseParser { + protected Reader reader; + protected NodeletParser parser; + private ParameterMap.Builder parameterMapBuilder; private List<ParameterMapping> parameterMappings; @@ -32,6 +35,15 @@ this.parser.setEntityResolver(new MapperEntityResolver()); } + public void parse() { + assert reader != null; + assert parser != null; + assert configuration != null; + assert typeAliasRegistry != null; + assert typeHandlerRegistry != null; + parser.parse(reader); + } + // <parameterMap id="" type=""> @Nodelet("/mapper/parameterMap") public void parameterMapElement(NodeletContext context) throws Exception { @@ -39,7 +51,7 @@ String type = context.getStringAttribute("type"); Class parameterClass = resolveClass(type); parameterMappings = new ArrayList<ParameterMapping>(); - parameterMapBuilder = new ParameterMap.Builder(id, parameterClass, parameterMappings); + parameterMapBuilder = new ParameterMap.Builder(configuration, id, parameterClass, parameterMappings); } // <parameterMap id="" type=""> @@ -67,7 +79,7 @@ Class typeClass = resolveClass(type); resultMappings = new ArrayList<ResultMapping>(); - resultMapBuilder = new ResultMap.Builder(id, typeClass, resultMappings); + resultMapBuilder = new ResultMap.Builder(configuration, id, typeClass, resultMappings); if (extend != null) { ResultMap resultMap = configuration.getResultMap(extend); @@ -136,7 +148,7 @@ public void resultMapDiscriminatorElement(NodeletContext context) throws Exception { ResultMapping.Builder resultMappingBuilder = buildResultMappingFromContext(context); discriminatorMap = new HashMap<String, String>(); - discriminatorBuilder = new Discriminator.Builder(resultMappingBuilder.build(), discriminatorMap); + discriminatorBuilder = new Discriminator.Builder(configuration, resultMappingBuilder.build(), discriminatorMap); } // <discriminator column="" javaType="" jdbcType=""> @@ -232,6 +244,7 @@ List<ParameterMapping> parameterMappings = new ArrayList<ParameterMapping>(); Class parameterTypeClass = resolveClass(parameterType); ParameterMap.Builder inlineParameterMapBuilder = new ParameterMap.Builder( + configuration, context.getStringAttribute("id") + "-inline-parameter-map", parameterTypeClass, parameterMappings); @@ -251,6 +264,7 @@ } else if (resultType != null) { Class resultTypeClass = resolveClass(resultType); ResultMap.Builder inlineResultMapBuilder = new ResultMap.Builder( + configuration, context.getStringAttribute("id")+ "-inline-result-map", resultTypeClass, new ArrayList<ResultMapping>()); @@ -278,17 +292,18 @@ String jdbcType = context.getStringAttribute("jdbcType"); String nestedSelect = context.getStringAttribute("select"); String nestedResultMap = context.getStringAttribute("resultMap"); + String typeHandler = context.getStringAttribute("typeHandler"); Class resultType = resultMapBuilder.type(); Class javaTypeClass = resolveResultJavaType(resultType, property, javaType); - TypeHandler typeHandlerInstance = resolveResultTypeHandler(context, resultType); + TypeHandler typeHandlerInstance = (TypeHandler) resolveInstance(typeHandler); JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType); - ResultMapping.Builder builder = new ResultMapping.Builder(property, column, typeHandlerInstance); - builder.javaType(javaTypeClass); + ResultMapping.Builder builder = new ResultMapping.Builder(configuration, property, column, javaTypeClass); builder.jdbcType(jdbcTypeEnum); builder.nestedQueryId(nestedSelect); builder.nestedResultMapId(nestedResultMap); + builder.typeHandler(typeHandlerInstance); return builder; } @@ -305,51 +320,27 @@ return javaTypeClass; } - private TypeHandler resolveResultTypeHandler(NodeletContext context, Class resultType) { - String property = context.getStringAttribute("property"); - String javaType = context.getStringAttribute("javaType"); - String jdbcType = context.getStringAttribute("jdbcType"); - String typeHandler = context.getStringAttribute("typeHandler"); - JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType); - Class javaTypeClass = resolveClass(javaType); - TypeHandler typeHandlerInstance = (TypeHandler) resolveInstance(typeHandler); - if (typeHandler == null) { - if (javaTypeClass == null) { - if (property != null) { - Class propertyType = resolveResultJavaType(resultType, property, javaType); - typeHandlerInstance = typeHandlerRegistry.getTypeHandler(propertyType, jdbcTypeEnum); - } - } else { - typeHandlerInstance = typeHandlerRegistry.getTypeHandler(javaTypeClass, jdbcTypeEnum); - } - } - if (typeHandlerInstance == null) { - throw new RuntimeException("Could not determine typehandler for result. Specify property, javaType or typeHandler attribute."); - } - return typeHandlerInstance; - } - private ParameterMapping.Builder buildParameterMappingFromContext(NodeletContext context) { String property = context.getStringAttribute("property"); String javaType = context.getStringAttribute("javaType"); String jdbcType = context.getStringAttribute("jdbcType"); String resultMap = context.getStringAttribute("resultMap"); String mode = context.getStringAttribute("mode"); + String typeHandler = context.getStringAttribute("typeHandler"); Integer numericScale = context.getIntAttribute("numericScale",null); ParameterMode modeEnum = resolveParameterMode(mode); - Class resultType = resultMapBuilder.type(); Class javaTypeClass = resolveParameterJavaType(resultType, property, javaType); - TypeHandler typeHandlerInstance = resolveParameterTypeHandler(context, resultType); JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType); + TypeHandler typeHandlerInstance = (TypeHandler) resolveInstance(typeHandler); - ParameterMapping.Builder builder = new ParameterMapping.Builder(property, typeHandlerInstance); - builder.javaType(javaTypeClass); + ParameterMapping.Builder builder = new ParameterMapping.Builder(configuration, property, javaTypeClass); builder.jdbcType(jdbcTypeEnum); builder.resultMapId(resultMap); builder.mode(modeEnum); builder.numericScale(numericScale); + builder.typeHandler(typeHandlerInstance); return builder; } @@ -366,28 +357,5 @@ return javaTypeClass; } - private TypeHandler resolveParameterTypeHandler(NodeletContext context, Class resultType) { - String property = context.getStringAttribute("property"); - String javaType = context.getStringAttribute("javaType"); - String jdbcType = context.getStringAttribute("jdbcType"); - String typeHandler = context.getStringAttribute("typeHandler"); - JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType); - Class javaTypeClass = resolveClass(javaType); - TypeHandler typeHandlerInstance = (TypeHandler) resolveInstance(typeHandler); - if (typeHandler == null) { - if (javaTypeClass == null) { - if (property != null) { - Class propertyType = resolveParameterJavaType(resultType, property, javaType); - typeHandlerInstance = typeHandlerRegistry.getTypeHandler(propertyType, jdbcTypeEnum); - } - } else { - typeHandlerInstance = typeHandlerRegistry.getTypeHandler(javaTypeClass, jdbcTypeEnum); - } - } - if (typeHandlerInstance == null) { - throw new RuntimeException("Could not determine typehandler for result. Specify property, javaType or typeHandler attribute."); - } - return typeHandlerInstance; - } }
Added: ibatis/trunk/java/ibatis-3/ibatis-3-monarch/src/main/java/org/apache/ibatis/monarch/builder/SqlSourceParser.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-monarch/src/main/java/org/apache/ibatis/monarch/builder/SqlSourceParser.java?rev=691430&view=auto ============================================================================== --- ibatis/trunk/java/ibatis-3/ibatis-3-monarch/src/main/java/org/apache/ibatis/monarch/builder/SqlSourceParser.java (added) +++ ibatis/trunk/java/ibatis-3/ibatis-3-monarch/src/main/java/org/apache/ibatis/monarch/builder/SqlSourceParser.java Tue Sep 2 16:28:04 2008 @@ -0,0 +1,67 @@ +package org.apache.ibatis.monarch.builder; + +import org.apache.ibatis.mapping.*; +import org.apache.ibatis.xml.*; +import org.apache.ibatis.type.TypeHandler; + +import java.util.*; + +public class SqlSourceParser extends BaseParser { + + public SqlSourceParser(MonarchConfiguration configuration) { + this.configuration = configuration; + this.typeAliasRegistry = configuration.getTypeAliasRegistry(); + this.typeHandlerRegistry = configuration.getTypeHandlerRegistry(); + } + + public SqlSource parse(NodeletContext context) { + ParameterMappingTokenHandler handler = new ParameterMappingTokenHandler(); + GenericTokenParser parser = new GenericTokenParser("#{", "}", handler); + String sql = parser.parse(context.getStringBody()); + return new InlineSqlSource(sql, handler.getParameterMappings()); + } + + private class ParameterMappingTokenHandler implements GenericTokenParser.TokenHandler { + + private List<ParameterMapping> parameterMappings = new ArrayList<ParameterMapping>(); + + public List<ParameterMapping> getParameterMappings() { + return parameterMappings; + } + + public String handleToken(String content) { + parameterMappings.add(buildParameterMapping(content)); + return "?"; + } + private ParameterMapping buildParameterMapping(String content) { + StringTokenizer parameterMappingParts = new StringTokenizer(content,", "); + String property = parameterMappingParts.nextToken(); + ParameterMapping.Builder builder = new ParameterMapping.Builder(configuration, property, Object.class); + while (parameterMappingParts.hasMoreTokens()) { + String attribute = parameterMappingParts.nextToken(); + StringTokenizer attributeParts = new StringTokenizer(attribute,"="); + if (attributeParts.countTokens() == 2) { + String name = attributeParts.nextToken(); + String value = attributeParts.nextToken(); + if ("javaType".equals(name)) { + builder.javaType(resolveClass(value)); + } else if ("jdbcType".equals(name)) { + builder.jdbcType(resolveJdbcType(value)); + } else if ("mode".equals(name)) { + builder.mode(resolveParameterMode(value)); + } else if ("numericScale".equals(name)) { + builder.numericScale(Integer.valueOf(value)); + } else if ("resultMap".equals(name)) { + builder.resultMapId(value); + } else if ("typeHandler".equals(name)) { + builder.typeHandler((TypeHandler) resolveInstance(value)); + } + } else { + throw new RuntimeException("Improper inline parameter map format. Should be: #{propName,attr1=val1,attr2=val2}"); + } + } + return builder.build(); + } + } + +}