ibatis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cbe...@apache.org
Subject svn commit: r747283 - in /ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser: BaseParser.java MapperConfigurator.java MapperParser.java
Date Tue, 24 Feb 2009 06:05:46 GMT
Author: cbegin
Date: Tue Feb 24 06:05:45 2009
New Revision: 747283

URL: http://svn.apache.org/viewvc?rev=747283&view=rev
Log:
Extracted common configuration functionality from MapperParser

Added:
    ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/MapperConfigurator.java
Modified:
    ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/BaseParser.java
    ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/MapperParser.java

Modified: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/BaseParser.java
URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/BaseParser.java?rev=747283&r1=747282&r2=747283&view=diff
==============================================================================
--- ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/BaseParser.java
(original)
+++ ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/BaseParser.java
Tue Feb 24 06:05:45 2009
@@ -70,6 +70,15 @@
     }
   }
 
+  protected Object resolveInstance(Class type) {
+    if (type == null) return null;
+    try {
+      return type.newInstance();
+    } catch (Exception e) {
+      throw new ParserException("Error instantiating class. Cause: " + e, e);
+    }
+  }
+
   protected String resolveAlias(String alias) {
     return typeAliasRegistry.resolveAlias(alias);
   }

Added: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/MapperConfigurator.java
URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/MapperConfigurator.java?rev=747283&view=auto
==============================================================================
--- ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/MapperConfigurator.java
(added)
+++ ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/MapperConfigurator.java
Tue Feb 24 06:05:45 2009
@@ -0,0 +1,356 @@
+package org.apache.ibatis.parser;
+
+import org.apache.ibatis.cache.Cache;
+import org.apache.ibatis.cache.decorators.LruCache;
+import org.apache.ibatis.cache.impl.PerpetualCache;
+import org.apache.ibatis.executor.ErrorContext;
+import org.apache.ibatis.mapping.*;
+import org.apache.ibatis.reflection.MetaClass;
+import org.apache.ibatis.type.JdbcType;
+import org.apache.ibatis.type.TypeHandler;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Properties;
+
+public class MapperConfigurator extends BaseParser {
+
+  protected String namespace;
+  private String resource;
+
+  private ParameterMap.Builder parameterMapBuilder;
+  private List<ParameterMapping> parameterMappings;
+
+  private ResultMap.Builder resultMapBuilder;
+  private List<ResultMapping> resultMappings;
+
+  private Discriminator.Builder discriminatorBuilder;
+  private HashMap<String, String> discriminatorMap;
+
+  private Cache cache;
+
+  public MapperConfigurator(Configuration configuration, String resource) {
+    ErrorContext.instance().resource(resource);
+    this.resource = resource;
+    this.configuration = configuration;
+    this.typeAliasRegistry = configuration.getTypeAliasRegistry();
+    this.typeHandlerRegistry = configuration.getTypeHandlerRegistry();
+  }
+
+  public void namespace(String namespace) {
+    this.namespace = namespace;
+    if (namespace == null) {
+      throw new ParserException("The mapper element requires a namespace attribute to be
specified.");
+    }
+  }
+
+  public void cacheRef(String namespace) {
+    if (namespace == null) {
+      throw new ParserException("cache-ref element requires a namespace attribute.");
+    }
+    cache = configuration.getCache(namespace);
+    if (cache == null) {
+      throw new ParserException("No cache for namespace '" + namespace + "' could be found.");
+    }
+  }
+
+  //  <cache type="LRU" flushInterval="3600000" size="1000" readOnly="false" />
+  public void cache(Class typeClass,
+                    Class evictionClass,
+                    Long flushInterval,
+                    Integer size,
+                    boolean readOnly,
+                    Properties props) {
+    typeClass = valueOrDefault(typeClass, PerpetualCache.class);
+    evictionClass = valueOrDefault(evictionClass, LruCache.class);
+    cache = new CacheBuilder(namespace)
+        .implementation(typeClass)
+        .addDecorator(evictionClass)
+        .clearInterval(flushInterval)
+        .size(size)
+        .readWrite(!readOnly)
+        .properties(props)
+        .build();
+    configuration.addCache(cache);
+  }
+
+  //  <parameterMap id="" type="">
+  public void parameterMapStart(String id, Class parameterClass) {
+    id = applyNamespace(id);
+    parameterMappings = new ArrayList<ParameterMapping>();
+    parameterMapBuilder = new ParameterMap.Builder(configuration, id, parameterClass, parameterMappings);
+  }
+
+  //  <parameterMap id="" type="">
+  //    <param property="id" javaType="" jdbcType="" typeHandler="" mode="" scale="" resultMap=""/>
+  public void parameterMapping(
+      String property,
+      Class javaType,
+      JdbcType jdbcType,
+      String resultMap,
+      ParameterMode parameterMode,
+      Class typeHandler,
+      Integer numericScale) {
+    resultMap = applyNamespace(resultMap);
+
+    Class resultType = parameterMapBuilder.type();
+    Class javaTypeClass = resolveParameterJavaType(resultType, property, javaType);
+    TypeHandler typeHandlerInstance = (TypeHandler) resolveInstance(typeHandler);
+
+    ParameterMapping.Builder builder = new ParameterMapping.Builder(configuration, property,
javaTypeClass);
+    builder.jdbcType(jdbcType);
+    builder.resultMapId(resultMap);
+    builder.mode(parameterMode);
+    builder.numericScale(numericScale);
+    builder.typeHandler(typeHandlerInstance);
+    parameterMappings.add(builder.build());
+  }
+
+  //  </parameterMap>
+  public void parameterMapEnd() {
+    configuration.addParameterMap(parameterMapBuilder.build());
+  }
+
+  //  <resultMap id="" type="" extends="">
+  public void resultMapStart(
+      String id,
+      Class type,
+      String extend) {
+    id = applyNamespace(id);
+    extend = applyNamespace(extend);
+
+    resultMappings = new ArrayList<ResultMapping>();
+    resultMapBuilder = new ResultMap.Builder(configuration, id, type, resultMappings);
+
+    if (extend != null) {
+      ResultMap resultMap = configuration.getResultMap(extend);
+      resultMappings.addAll(resultMap.getResultMappings());
+    }
+  }
+
+  //  <constructor>
+  //    <id column="" javaType="" jdbcType="" typeHandler=""/>
+  //  <constructor>
+  //    <result column="" javaType="" jdbcType="" typeHandler=""/>
+  //  <id property="" column="" javaType="" jdbcType="" typeHandler=""/>
+  //  <result property="" column="" javaType="" jdbcType="" typeHandler=""/>
+  //  <collection property="" column="" javaType="" select="" resultMap=""/>
+  //  <association property="" column="" javaType="" select="" resultMap=""/>
+  public void resultMapping(
+      String property,
+      String column,
+      Class javaType,
+      JdbcType jdbcType,
+      String nestedSelect,
+      String nestedResultMap,
+      Class typeHandler,
+      ArrayList<ResultFlag> flags) {
+    ResultMapping resultMapping = buildResultMapping(
+        property,
+        column,
+        javaType,
+        jdbcType,
+        nestedSelect,
+        nestedResultMap,
+        typeHandler,
+        flags);
+    resultMappings.add(resultMapping);
+  }
+
+
+  //  <discriminator column="" javaType="" jdbcType="">
+  public void resultMapDiscriminatorStart(
+      String property,
+      String column,
+      Class javaType,
+      JdbcType jdbcType,
+      String nestedSelect,
+      String nestedResultMap,
+      Class typeHandler) {
+    ResultMapping resultMapping = buildResultMapping(
+        property,
+        column,
+        javaType,
+        jdbcType,
+        nestedSelect,
+        nestedResultMap,
+        typeHandler,
+        new ArrayList<ResultFlag>());
+    discriminatorMap = new HashMap<String, String>();
+    discriminatorBuilder = new Discriminator.Builder(configuration, resultMapping, discriminatorMap);
+  }
+
+  //  <discriminator column="" javaType="" jdbcType="">
+  //    <case value="" resultMap=""/>
+  public void resultMapDiscriminatorCase(
+      String value,
+      String resultMap) {
+    resultMap = applyNamespace(resultMap);
+    discriminatorMap.put(value, resultMap);
+  }
+
+  //  </discriminator>
+  public void resultMapDiscriminatorEnd() {
+    resultMapBuilder.discriminator(discriminatorBuilder.build());
+  }
+
+  //  </resultMap>
+  public void resultMapEnd() {
+    configuration.addResultMap(resultMapBuilder.build());
+  }
+
+  public void statement(
+      String id,
+      String sql,
+      Integer fetchSize,
+      Integer timeout,
+      String parameterMap,
+      Class parameterType,
+      String resultMap,
+      Class resultType,
+      ResultSetType resultSetType,
+      boolean isSelect,
+      boolean flushCache,
+      boolean useCache,
+      StatementType statementType) {
+    id = applyNamespace(id);
+
+    SqlSource sqlSource = new SqlSourceParser(configuration).parse(sql);
+
+    MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration,
id, sqlSource);
+    statementBuilder.resource(resource);
+    statementBuilder.fetchSize(fetchSize);
+    statementBuilder.statementType(statementType);
+    setStatementTimeout(timeout, statementBuilder);
+
+    setStatementParameterMap(parameterMap, parameterType, statementBuilder);
+    setStatementResultMap(resultMap, resultType, resultSetType, statementBuilder);
+    setStatementCache(isSelect, flushCache, useCache, statementBuilder);
+
+    MappedStatement statement = statementBuilder.build();
+    configuration.addMappedStatement(statement);
+  }
+
+  private <T> T valueOrDefault(T value, T defaultValue) {
+    return value == null ? defaultValue : value;
+  }
+
+  private String applyNamespace(String base) {
+    if (base == null) return null;
+    if (base.contains(".")) return base;
+    return namespace + "." + base;
+  }
+
+  private void setStatementCache(
+      boolean isSelect,
+      boolean flushCache,
+      boolean useCache,
+      MappedStatement.Builder statementBuilder) {
+    flushCache = valueOrDefault(flushCache, !isSelect);
+    useCache = valueOrDefault(useCache, isSelect);
+    statementBuilder.flushCacheRequired(flushCache);
+    statementBuilder.useCache(useCache);
+    statementBuilder.cache(cache);
+  }
+
+  private void setStatementParameterMap(
+      String parameterMap,
+      Class parameterTypeClass,
+      MappedStatement.Builder statementBuilder) {
+    parameterMap = applyNamespace(parameterMap);
+
+    if (parameterMap != null) {
+      statementBuilder.parameterMap(configuration.getParameterMap(parameterMap));
+    } else if (parameterTypeClass != null) {
+      List<ParameterMapping> parameterMappings = new ArrayList<ParameterMapping>();
+      ParameterMap.Builder inlineParameterMapBuilder = new ParameterMap.Builder(
+          configuration,
+          statementBuilder.id() + "-inline-parameter-map",
+          parameterTypeClass,
+          parameterMappings);
+      statementBuilder.parameterMap(inlineParameterMapBuilder.build());
+    }
+  }
+
+  private void setStatementResultMap(
+      String resultMap,
+      Class resultType,
+      ResultSetType resultSetType,
+      MappedStatement.Builder statementBuilder) {
+    resultMap = applyNamespace(resultMap);
+
+    List<ResultMap> resultMaps = new ArrayList<ResultMap>();
+    if (resultMap != null) {
+      String[] resultMapNames = resultMap.split(",");
+      for (String resultMapName : resultMapNames) {
+        resultMaps.add(configuration.getResultMap(resultMapName.trim()));
+      }
+    } else if (resultType != null) {
+      ResultMap.Builder inlineResultMapBuilder = new ResultMap.Builder(
+          configuration,
+          statementBuilder.id() + "-inline-result-map",
+          resultType,
+          new ArrayList<ResultMapping>());
+      resultMaps.add(inlineResultMapBuilder.build());
+    }
+    statementBuilder.resultMaps(resultMaps);
+
+    statementBuilder.resultSetType(resultSetType);
+  }
+
+  private void setStatementTimeout(Integer timeout, MappedStatement.Builder statementBuilder)
{
+    if (timeout != null) {
+      timeout = configuration.getDefaultStatementTimeout();
+    }
+    statementBuilder.timeout(timeout);
+  }
+
+  private ResultMapping buildResultMapping(
+      String property,
+      String column,
+      Class javaType,
+      JdbcType jdbcType,
+      String nestedSelect,
+      String nestedResultMap,
+      Class typeHandler,
+      ArrayList<ResultFlag> flags) {
+
+    nestedResultMap = applyNamespace(nestedResultMap);
+    Class resultType = resultMapBuilder.type();
+    Class javaTypeClass = resolveResultJavaType(resultType, property, javaType);
+    TypeHandler typeHandlerInstance = (TypeHandler) resolveInstance(typeHandler);
+
+    ResultMapping.Builder builder = new ResultMapping.Builder(configuration, property, column,
javaTypeClass);
+    builder.jdbcType(jdbcType);
+    builder.nestedQueryId(applyNamespace(nestedSelect));
+    builder.nestedResultMapId(applyNamespace(nestedResultMap));
+    builder.typeHandler(typeHandlerInstance);
+    builder.flags(flags);
+
+    return builder.build();
+  }
+
+  private Class resolveResultJavaType(Class resultType, String property, Class javaType)
{
+    if (javaType == null) {
+      MetaClass metaResultType = MetaClass.forClass(resultType);
+      javaType = metaResultType.getSetterType(property);
+    }
+    if (javaType == null) {
+      throw new ParserException("Could not determine javaType for result.  Specify property
or javaType attribute.");
+    }
+    return javaType;
+  }
+
+  private Class resolveParameterJavaType(Class resultType, String property, Class javaType)
{
+    if (javaType == null) {
+      MetaClass metaResultType = MetaClass.forClass(resultType);
+      javaType = metaResultType.getGetterType(property);
+    }
+    if (javaType == null) {
+      throw new ParserException("Could not determine javaType for result.  Specify property
or javaType attribute.");
+    }
+    return javaType;
+  }
+
+}

Modified: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/MapperParser.java
URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/MapperParser.java?rev=747283&r1=747282&r2=747283&view=diff
==============================================================================
--- ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/MapperParser.java
(original)
+++ ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/MapperParser.java
Tue Feb 24 06:05:45 2009
@@ -1,49 +1,24 @@
 package org.apache.ibatis.parser;
 
-import org.apache.ibatis.cache.Cache;
-import org.apache.ibatis.executor.ErrorContext;
 import org.apache.ibatis.mapping.*;
-import org.apache.ibatis.reflection.MetaClass;
 import org.apache.ibatis.type.JdbcType;
-import org.apache.ibatis.type.TypeHandler;
 import org.apache.ibatis.xml.Nodelet;
 import org.apache.ibatis.xml.NodeletContext;
 import org.apache.ibatis.xml.NodeletParser;
 
 import java.io.Reader;
 import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
 import java.util.Properties;
 
 public class MapperParser extends BaseParser {
 
-  protected String namespace;
   protected Reader reader;
   protected NodeletParser parser;
-
-  private String resource;
-
-  private ParameterMap.Builder parameterMapBuilder;
-  private List<ParameterMapping> parameterMappings;
-
-  private ResultMap.Builder resultMapBuilder;
-  private List<ResultMapping> resultMappings;
-
-  private Discriminator.Builder discriminatorBuilder;
-  private HashMap<String, String> discriminatorMap;
-
-  private Cache cache;
+  protected MapperConfigurator mapperBuilder;
 
   public MapperParser(Reader reader, Configuration configuration, String resource) {
-    ErrorContext.instance().resource(resource);
-    this.resource = resource;
+    this.mapperBuilder = new MapperConfigurator(configuration,resource);
     this.reader = reader;
-
-    this.configuration = configuration;
-    this.typeAliasRegistry = configuration.getTypeAliasRegistry();
-    this.typeHandlerRegistry = configuration.getTypeHandlerRegistry();
-
     this.parser = new NodeletParser();
     this.parser.addNodeletHandler(this);
     this.parser.setValidation(true);
@@ -63,167 +38,140 @@
   //  <configuration namespace="com.domain.MapperClass" />
   @Nodelet("/mapper")
   public void configurationElement(NodeletContext context) throws Exception {
-    namespace = context.getStringAttribute("namespace");
-    if (namespace == null) {
-      throw new ParserException("The mapper element requires a namespace attribute to be
specified.");
-    }
+    mapperBuilder.namespace(context.getStringAttribute("namespace"));
   }
 
   //  <cache type="LRU" flushInterval="3600000" size="1000" readOnly="false" />
   @Nodelet("/mapper/cache-ref")
   public void cacheRefElement(NodeletContext context) throws Exception {
-    String ns = context.getStringAttribute("namespace");
-    if (ns == null) {
-      throw new ParserException("cache-ref element requires a namespace attribute.");
-    }
-    cache = configuration.getCache(namespaceCacheId(ns));
-    if (cache == null) {
-      throw new ParserException("No cache for namespace '" + ns + "' could be found.");
-    }
+    mapperBuilder.cacheRef(context.getStringAttribute("namespace"));
   }
 
   //  <cache type="LRU" flushInterval="3600000" size="1000" readOnly="false" />
   @Nodelet("/mapper/cache")
   public void cacheElement(NodeletContext context) throws Exception {
-    String type = context.getStringAttribute("perpetual", "PERPETUAL");
+    String type = context.getStringAttribute("perpetual");
     type = typeAliasRegistry.resolveAlias(type);
     Class typeClass = Class.forName(type);
-
-    String eviction = context.getStringAttribute("eviction", "LRU");
+    String eviction = context.getStringAttribute("eviction");
     eviction = typeAliasRegistry.resolveAlias(eviction);
     Class evictionClass = Class.forName(eviction);
-
-    Long flushInterval = context.getLongAttribute("flushInterval", null);
-    Integer size = context.getIntAttribute("size", null);
+    Long flushInterval = context.getLongAttribute("flushInterval");
+    Integer size = context.getIntAttribute("size");
     boolean readOnly = context.getBooleanAttribute("readOnly", false);
-
     Properties props = context.getChildrenAsProperties();
-
-    cache = new CacheBuilder(namespaceCacheId(namespace))
-        .implementation(typeClass)
-        .addDecorator(evictionClass)
-        .clearInterval(flushInterval)
-        .size(size)
-        .readWrite(!readOnly)
-        .properties(props)
-        .build();
-
-    configuration.addCache(cache);
+    mapperBuilder.cache(typeClass,evictionClass,flushInterval,size,readOnly,props);
   }
 
   //  <parameterMap id="" type="">
   @Nodelet("/mapper/parameterMap")
   public void parameterMapElement(NodeletContext context) throws Exception {
     String id = context.getStringAttribute("id");
-    id = applyNamespace(id);
     String type = context.getStringAttribute("type");
     Class parameterClass = resolveClass(type);
-    parameterMappings = new ArrayList<ParameterMapping>();
-    parameterMapBuilder = new ParameterMap.Builder(configuration, id, parameterClass, parameterMappings);
+    mapperBuilder.parameterMapStart(id,parameterClass);
   }
 
   //  <parameterMap id="" type="">
   //    <param property="id" javaType="" jdbcType="" typeHandler="" mode="" scale="" resultMap=""/>
   @Nodelet("/mapper/parameterMap/parameter")
   public void parameterMapParameterElement(NodeletContext context) throws Exception {
-    ParameterMapping.Builder builder = buildParameterMappingFromContext(context);
-    ParameterMapping parameterMapping = builder.build();
-    parameterMappings.add(parameterMapping);
+    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 javaTypeClass = resolveClass(javaType);
+    JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType);
+    Class typeHandlerClass = resolveClass(typeHandler);
+    mapperBuilder.parameterMapping(property,javaTypeClass,jdbcTypeEnum,resultMap,modeEnum,typeHandlerClass,numericScale);
   }
 
   //  </parameterMap>
   @Nodelet("/mapper/parameterMap/end()")
   public void parameterMapClosingElement(NodeletContext context) throws Exception {
-    configuration.addParameterMap(parameterMapBuilder.build());
+    mapperBuilder.parameterMapEnd();
   }
 
   //  <resultMap id="" type="" extends="">
   @Nodelet("/mapper/resultMap")
   public void resultMapElement(NodeletContext context) throws Exception {
     String id = context.getStringAttribute("id");
-    id = applyNamespace(id);
-
     String type = context.getStringAttribute("type");
     String extend = context.getStringAttribute("extends");
-    extend = applyNamespace(extend);
-
     Class typeClass = resolveClass(type);
-
-    resultMappings = new ArrayList<ResultMapping>();
-    resultMapBuilder = new ResultMap.Builder(configuration, id, typeClass, resultMappings);
-
-    if (extend != null) {
-      ResultMap resultMap = configuration.getResultMap(extend);
-      resultMappings.addAll(resultMap.getResultMappings());
-    }
+    mapperBuilder.resultMapStart(id,typeClass,extend);
   }
 
   //  <constructor>
   //    <id column="" javaType="" jdbcType="" typeHandler=""/>
   @Nodelet("/mapper/resultMap/constructor/id")
   public void resultMapConstructorIdElement(NodeletContext context) throws Exception {
-    ResultMapping.Builder builder = buildResultMappingFromContext(context);
-    builder.flags(new ArrayList<ResultFlag>() {
+    buildResultMappingFromContext(context,
+        new ArrayList<ResultFlag>() {
       {
         add(ResultFlag.CONSTRUCTOR);
         add(ResultFlag.ID);
       }
     });
-    resultMappings.add(builder.build());
   }
 
   //  <constructor>
   //    <result column="" javaType="" jdbcType="" typeHandler=""/>
   @Nodelet("/mapper/resultMap/constructor/result")
   public void resultMapConstructorResultElement(NodeletContext context) throws Exception
{
-    ResultMapping.Builder builder = buildResultMappingFromContext(context);
-    builder.flags(new ArrayList<ResultFlag>() {
+    buildResultMappingFromContext(context, new ArrayList<ResultFlag>() {
       {
         add(ResultFlag.CONSTRUCTOR);
       }
     });
-    resultMappings.add(builder.build());
   }
 
   //  <id property="" column="" javaType="" jdbcType="" typeHandler=""/>
   @Nodelet("/mapper/resultMap/id")
   public void resultMapIdElement(NodeletContext context) throws Exception {
-    ResultMapping.Builder builder = buildResultMappingFromContext(context);
-    builder.flags(new ArrayList<ResultFlag>() {
+    buildResultMappingFromContext(context, new ArrayList<ResultFlag>() {
       {
         add(ResultFlag.ID);
       }
     });
-    resultMappings.add(builder.build());
   }
 
   //  <result property="" column="" javaType="" jdbcType="" typeHandler=""/>
   @Nodelet("/mapper/resultMap/result")
   public void resultMapResultElement(NodeletContext context) throws Exception {
-    ResultMapping.Builder builder = buildResultMappingFromContext(context);
-    resultMappings.add(builder.build());
+    buildResultMappingFromContext(context, new ArrayList<ResultFlag>());
   }
 
   //  <collection property="" column="" javaType="" select="" resultMap=""/>
   @Nodelet("/mapper/resultMap/collection")
   public void resultMapCollectionElement(NodeletContext context) throws Exception {
-    ResultMapping.Builder builder = buildResultMappingFromContext(context);
-    resultMappings.add(builder.build());
+    buildResultMappingFromContext(context, new ArrayList<ResultFlag>());
   }
 
   //  <association property="" column="" javaType="" select="" resultMap=""/>
   @Nodelet("/mapper/resultMap/association")
   public void resultMapAssociationElement(NodeletContext context) throws Exception {
-    ResultMapping.Builder builder = buildResultMappingFromContext(context);
-    resultMappings.add(builder.build());
+    buildResultMappingFromContext(context, new ArrayList<ResultFlag>());
   }
 
   //  <discriminator column="" javaType="" jdbcType="">
   @Nodelet("/mapper/resultMap/discriminator")
   public void resultMapDiscriminatorElement(NodeletContext context) throws Exception {
-    ResultMapping.Builder resultMappingBuilder = buildResultMappingFromContext(context);
-    discriminatorMap = new HashMap<String, String>();
-    discriminatorBuilder = new Discriminator.Builder(configuration, resultMappingBuilder.build(),
discriminatorMap);
+    String property = context.getStringAttribute("property");
+    String column = context.getStringAttribute("column");
+    String javaType = context.getStringAttribute("javaType");
+    String jdbcType = context.getStringAttribute("jdbcType");
+    String nestedSelect = context.getStringAttribute("select");
+    String nestedResultMap = context.getStringAttribute("resultMap");
+    String typeHandler = context.getStringAttribute("typeHandler");
+    Class javaTypeClass = resolveClass(javaType);
+    Class typeHandlerClass =  resolveClass(typeHandler);
+    JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType);
+    mapperBuilder.resultMapDiscriminatorStart(property,column,javaTypeClass,jdbcTypeEnum,nestedSelect,nestedResultMap,typeHandlerClass);
   }
 
   //  <discriminator column="" javaType="" jdbcType="">
@@ -232,230 +180,90 @@
   public void resultMapDiscriminatorCaseElement(NodeletContext context) throws Exception
{
     String value = context.getStringAttribute("value");
     String resultMap = context.getStringAttribute("resultMap");
-    resultMap = applyNamespace(resultMap);
-    discriminatorMap.put(value, resultMap);
+    mapperBuilder.resultMapDiscriminatorCase(value,resultMap);
   }
 
   //  </discriminator>
   @Nodelet("/mapper/resultMap/discriminator/end()")
   public void resultMapDiscriminatorClosingElement(NodeletContext context) throws Exception
{
-    resultMapBuilder.discriminator(discriminatorBuilder.build());
+    mapperBuilder.resultMapDiscriminatorEnd();
   }
 
   //  </resultMap>
   @Nodelet("/mapper/resultMap/end()")
   public void resultMapClosingElement(NodeletContext context) throws Exception {
-    configuration.addResultMap(resultMapBuilder.build());
+    mapperBuilder.resultMapEnd();
   }
 
   //  <select ...>
   @Nodelet("/mapper/select")
   public void selectElement(NodeletContext context) throws Exception {
-    buildStatement(context, StatementType.PREPARED);
+    buildStatementFromContext(context, StatementType.PREPARED);
   }
 
   //  <insert ...>
   @Nodelet("/mapper/insert")
   public void insertElement(NodeletContext context) throws Exception {
-    buildStatement(context, StatementType.PREPARED);
+    buildStatementFromContext(context, StatementType.PREPARED);
   }
 
   //  <update ...>
   @Nodelet("/mapper/update")
   public void updateElement(NodeletContext context) throws Exception {
-    buildStatement(context, StatementType.PREPARED);
+    buildStatementFromContext(context, StatementType.PREPARED);
   }
 
   //  <delete ...>
   @Nodelet("/mapper/delete")
   public void deleteElement(NodeletContext context) throws Exception {
-    buildStatement(context, StatementType.PREPARED);
+    buildStatementFromContext(context, StatementType.PREPARED);
   }
 
   //  <procedure ...>
   @Nodelet("/mapper/procedure")
   public void procedureElement(NodeletContext context) throws Exception {
-    buildStatement(context, StatementType.CALLABLE);
+    buildStatementFromContext(context, StatementType.CALLABLE);
   }
 
   //  <procedure ...>
   @Nodelet("/mapper/statement")
   public void statementElement(NodeletContext context) throws Exception {
-    buildStatement(context, StatementType.STATEMENT);
-  }
-
-  private String applyNamespace(String base) {
-    if (base == null) return null;
-    if (base.contains(".")) return base;
-    return namespace + "." + base;
+    buildStatementFromContext(context, StatementType.STATEMENT);
   }
 
-  private String namespaceCacheId(String ns) {
-    return ns + ".Cache";
-  }
-
-  private void buildStatement(NodeletContext context, StatementType statementType) {
+  private void buildStatementFromContext(NodeletContext context, StatementType statementType)
{
     String id = context.getStringAttribute("id");
-    id = applyNamespace(id);
-
-    //String sql = context.getStringBody();
-    SqlSource sqlSource = new SqlSourceParser(configuration).parse(context.getStringBody());
-
-    MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration,
id, sqlSource);
-    statementBuilder.resource(resource);
+    String sql = context.getStringBody();
     Integer fetchSize = context.getIntAttribute("fetchSize", null);
-    statementBuilder.fetchSize(fetchSize);
-    statementBuilder.statementType(statementType);
-    setStatementTimeout(context, statementBuilder);
-
-    setStatementParameterMap(context, statementBuilder);
-    setStatementResultMap(context, statementBuilder);
-    setStatementCache(context, statementBuilder);
-
-    MappedStatement statement = statementBuilder.build();
-    configuration.addMappedStatement(statement);
-  }
-
-  private void setStatementCache(NodeletContext context, MappedStatement.Builder statementBuilder)
{
+    Integer timeout = context.getIntAttribute("timeout", null);
     boolean isSelect = "select".equals(context.getNode().getNodeName());
-
     boolean flushCache = context.getBooleanAttribute("flushCache", !isSelect);
-    statementBuilder.flushCacheRequired(flushCache);
-
     boolean useCache = context.getBooleanAttribute("useCache", isSelect);
-    statementBuilder.useCache(useCache);
-
-    statementBuilder.cache(cache);
-  }
-
-  private void setStatementParameterMap(NodeletContext context, MappedStatement.Builder statementBuilder)
{
     String parameterMap = context.getStringAttribute("parameterMap");
-    parameterMap = applyNamespace(parameterMap);
-
     String parameterType = context.getStringAttribute("parameterType");
-    if (parameterMap != null) {
-      statementBuilder.parameterMap(configuration.getParameterMap(parameterMap));
-    } else if (parameterType != null) {
-      List<ParameterMapping> parameterMappings = new ArrayList<ParameterMapping>();
-      Class parameterTypeClass = resolveClass(parameterType);
-      ParameterMap.Builder inlineParameterMapBuilder = new ParameterMap.Builder(
-          configuration,
-          statementBuilder.id() + "-inline-parameter-map",
-          parameterTypeClass,
-          parameterMappings);
-      statementBuilder.parameterMap(inlineParameterMapBuilder.build());
-    }
-  }
-
-  private void setStatementResultMap(NodeletContext context, MappedStatement.Builder statementBuilder)
{
+    Class parameterTypeClass = resolveClass(parameterType);
     String resultMap = context.getStringAttribute("resultMap");
-    resultMap = applyNamespace(resultMap);
-
     String resultType = context.getStringAttribute("resultType");
-    List<ResultMap> resultMaps = new ArrayList<ResultMap>();
-    if (resultMap != null) {
-      String[] resultMapNames = resultMap.split(",");
-      for (String resultMapName : resultMapNames) {
-        resultMaps.add(configuration.getResultMap(resultMapName.trim()));
-      }
-    } else if (resultType != null) {
-      Class resultTypeClass = resolveClass(resultType);
-      ResultMap.Builder inlineResultMapBuilder = new ResultMap.Builder(
-          configuration,
-          statementBuilder.id() + "-inline-result-map",
-          resultTypeClass,
-          new ArrayList<ResultMapping>());
-      resultMaps.add(inlineResultMapBuilder.build());
-    }
-    statementBuilder.resultMaps(resultMaps);
-
+    Class resultTypeClass = resolveClass(resultType);
     String resultSetType = context.getStringAttribute("resultSetType");
     ResultSetType resultSetTypeEnum = resolveResultSetType(resultSetType);
-    statementBuilder.resultSetType(resultSetTypeEnum);
+    mapperBuilder.statement(id,sql,fetchSize,timeout,parameterMap, parameterTypeClass,
+        resultMap, resultTypeClass,resultSetTypeEnum,isSelect,flushCache,useCache,statementType);
   }
 
-  private void setStatementTimeout(NodeletContext context, MappedStatement.Builder statementBuilder)
{
-    Integer timeout = context.getIntAttribute("timeout", null);
-    if (timeout != null) {
-      timeout = configuration.getDefaultStatementTimeout();
-    }
-    statementBuilder.timeout(timeout);
-  }
 
-  private ResultMapping.Builder buildResultMappingFromContext(NodeletContext context) {
+  private void buildResultMappingFromContext(NodeletContext context, ArrayList<ResultFlag>
flags) {
     String property = context.getStringAttribute("property");
     String column = context.getStringAttribute("column");
     String javaType = context.getStringAttribute("javaType");
     String jdbcType = context.getStringAttribute("jdbcType");
     String nestedSelect = context.getStringAttribute("select");
     String nestedResultMap = context.getStringAttribute("resultMap");
-    nestedResultMap = applyNamespace(nestedResultMap);
-
     String typeHandler = context.getStringAttribute("typeHandler");
-
-    Class resultType = resultMapBuilder.type();
-    Class javaTypeClass = resolveResultJavaType(resultType, property, javaType);
-    TypeHandler typeHandlerInstance = (TypeHandler) resolveInstance(typeHandler);
-    JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType);
-
-    ResultMapping.Builder builder = new ResultMapping.Builder(configuration, property, column,
javaTypeClass);
-    builder.jdbcType(jdbcTypeEnum);
-    builder.nestedQueryId(applyNamespace(nestedSelect));
-    builder.nestedResultMapId(applyNamespace(nestedResultMap));
-    builder.typeHandler(typeHandlerInstance);
-
-    return builder;
-  }
-
-  private Class resolveResultJavaType(Class resultType, String property, String javaType)
{
     Class javaTypeClass = resolveClass(javaType);
-    if (javaTypeClass == null) {
-      MetaClass metaResultType = MetaClass.forClass(resultType);
-      javaTypeClass = metaResultType.getSetterType(property);
-    }
-    if (javaTypeClass == null) {
-      throw new ParserException("Could not determine javaType for result.  Specify property
or javaType attribute.");
-    }
-    return javaTypeClass;
-  }
-
-  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");
-    resultMap = applyNamespace(resultMap);
-    String mode = context.getStringAttribute("mode");
-    String typeHandler = context.getStringAttribute("typeHandler");
-    Integer numericScale = context.getIntAttribute("numericScale", null);
-
-    ParameterMode modeEnum = resolveParameterMode(mode);
-    Class resultType = parameterMapBuilder.type();
-    Class javaTypeClass = resolveParameterJavaType(resultType, property, javaType);
+    Class typeHandlerClass =  resolveClass(typeHandler);
     JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType);
-    TypeHandler typeHandlerInstance = (TypeHandler) resolveInstance(typeHandler);
-
-    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;
+    mapperBuilder.resultMapping(property,column,javaTypeClass,jdbcTypeEnum,nestedSelect,nestedResultMap,typeHandlerClass,flags);
   }
 
-  private Class resolveParameterJavaType(Class resultType, String property, String javaType)
{
-    Class javaTypeClass = resolveClass(javaType);
-    if (javaTypeClass == null) {
-      MetaClass metaResultType = MetaClass.forClass(resultType);
-      javaTypeClass = metaResultType.getGetterType(property);
-    }
-    if (javaTypeClass == null) {
-      throw new ParserException("Could not determine javaType for result.  Specify property
or javaType attribute.");
-    }
-    return javaTypeClass;
-  }
-
-
 }



Mime
View raw message