ibatis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cbe...@apache.org
Subject svn commit: r746639 - in /ibatis/trunk/java/ibatis-3/ibatis-3-core/src: main/java/org/apache/ibatis/binding/ main/java/org/apache/ibatis/parser/ test/java/org/apache/ibatis/binding/
Date Sun, 22 Feb 2009 05:48:19 GMT
Author: cbegin
Date: Sun Feb 22 05:48:19 2009
New Revision: 746639

URL: http://svn.apache.org/viewvc?rev=746639&view=rev
Log:
added selectOne test and refactoring

Modified:
    ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/binding/MapperRegistry.java
    ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/MapperParser.java
    ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/SqlSourceParser.java
    ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/binding/BindingTest.java
    ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/binding/BoundBlogMapper.java

Modified: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/binding/MapperRegistry.java
URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/binding/MapperRegistry.java?rev=746639&r1=746638&r2=746639&view=diff
==============================================================================
--- ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/binding/MapperRegistry.java
(original)
+++ ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/binding/MapperRegistry.java
Sun Feb 22 05:48:19 2009
@@ -3,15 +3,11 @@
 import org.apache.ibatis.session.SqlSession;
 import org.apache.ibatis.mapping.*;
 import static org.apache.ibatis.annotations.Annotations.*;
-import org.apache.ibatis.reflection.MetaObject;
 import org.apache.ibatis.cache.Cache;
-import org.apache.ibatis.parser.InlineSqlSource;
 import org.apache.ibatis.parser.SqlSourceParser;
-import org.apache.ibatis.xml.GenericTokenParser;
 
 import java.util.*;
 import java.lang.reflect.Method;
-import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.TypeVariable;
 import java.lang.annotation.Annotation;
 
@@ -75,45 +71,76 @@
   private MappedStatement parseMappedStatement(Method method, Cache cache) {
     Class annotationType = getSqlAnnotationType(method);
     if (annotationType != null) {
-      String sql = getSqlAnnotationValue(method, annotationType);
-      String mappedStatementId = method.getDeclaringClass().getName() + "." + method.getName();
-      SqlSource sqlSource = new BasicSqlSource(sql);
+      final String sql = getSqlAnnotationValue(method, annotationType);
+      final String mappedStatementId = method.getDeclaringClass().getName() + "." + method.getName();
+      final SqlSource sqlSource = new SqlSourceParser(config).parse(sql);
+
       MappedStatement.Builder builder = new MappedStatement.Builder(config, mappedStatementId,
sqlSource);
       builder.resource(method.getDeclaringClass().getName().replace('.', '/') + ".java (best
guess)");
-
-      Options options = method.getAnnotation(Options.class);
-      if (options != null) {
-        builder.useCache(options.useCache());
-        builder.flushCacheRequired(options.flushCache());
-        builder.resultSetType(options.resultSetType());
-        builder.statementType(options.statementType());
-        builder.fetchSize(options.fetchSize());
-        builder.timeout(options.timeout());
-      }
-
       builder.cache(cache);
-
-      Class returnType = method.getReturnType();
-      if (returnType.isAssignableFrom(Collection.class)) {
-        TypeVariable<? extends Class<?>>[] returnTypeVariables = returnType.getTypeParameters();
-        if (returnTypeVariables.length == 1) {
-          returnType = returnTypeVariables[0].getGenericDeclaration();
-        }
-      }
-
-      builder.parameterMap(new ParameterMap.Builder(config, "", Object.class, new ArrayList<ParameterMapping>()).build());
-      final ResultMap resultMap = new ResultMap.Builder(config, "", returnType, new ArrayList<ResultMapping>()).build();
-      builder.resultMaps(new ArrayList<ResultMap>() {
-        {
-          add(resultMap);
-        }
-      });
+      setOptions(method, builder);
+      setParameterMap(method, mappedStatementId, builder);
+      setResultMaps(method, mappedStatementId, builder);
 
       return builder.build();
     }
     return null;
   }
 
+  private void setResultMaps(Method method, final String mappedStatementId, MappedStatement.Builder
builder) {
+    final Class returnType = getReturnType(method);
+    builder.resultMaps(new ArrayList<ResultMap>() {{
+      add(new ResultMap.Builder(
+        config,
+        mappedStatementId+"-BoundResultMap",
+        returnType,
+        new ArrayList<ResultMapping>()).build());
+    }});
+  }
+
+  private void setParameterMap(Method method, String mappedStatementId, MappedStatement.Builder
builder) {
+    final Class parameterType = getParameterType(method);
+    builder.parameterMap(new ParameterMap.Builder(
+        config,
+        mappedStatementId+"-BoundParameterMap",
+        parameterType,
+        new ArrayList<ParameterMapping>()).build());
+  }
+
+  private void setOptions(Method method, MappedStatement.Builder builder) {
+    Options options = method.getAnnotation(Options.class);
+    if (options != null) {
+      builder.useCache(options.useCache());
+      builder.flushCacheRequired(options.flushCache());
+      builder.resultSetType(options.resultSetType());
+      builder.statementType(options.statementType());
+      builder.fetchSize(options.fetchSize());
+      builder.timeout(options.timeout());
+    }
+  }
+
+  private Class getParameterType(Method method) {
+    Class parameterType = null;
+    Class[] parameterTypes = method.getParameterTypes();
+    if (parameterTypes.length ==1 || parameterTypes.length == 3) {
+      // Methods with 1 or 3 parameters have a value parameter
+      // (the other two params are offset/limit parameters for multiple selects)
+      parameterType = parameterTypes[0];
+    }
+    return parameterType;
+  }
+
+  private Class getReturnType(Method method) {
+    Class returnType = method.getReturnType();
+    if (returnType.isAssignableFrom(Collection.class)) {
+      TypeVariable<? extends Class<?>>[] returnTypeVariables = returnType.getTypeParameters();
+      if (returnTypeVariables.length == 1) {
+        returnType = returnTypeVariables[0].getGenericDeclaration();
+      }
+    }
+    return returnType;
+  }
+
   private String getSqlAnnotationValue(Method method, Class annotationType) {
     Annotation annotation = method.getAnnotation(annotationType);
     if (annotation != null) {

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=746639&r1=746638&r2=746639&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
Sun Feb 22 05:48:19 2009
@@ -299,7 +299,7 @@
     id = applyNamespace(id);
 
     //String sql = context.getStringBody();
-    SqlSource sqlSource = new SqlSourceParser(configuration).parse(context);
+    SqlSource sqlSource = new SqlSourceParser(configuration).parse(context.getStringBody());
 
     MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration,
id, sqlSource);
     statementBuilder.resource(resource);

Modified: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/SqlSourceParser.java
URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/SqlSourceParser.java?rev=746639&r1=746638&r2=746639&view=diff
==============================================================================
--- ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/SqlSourceParser.java
(original)
+++ ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/SqlSourceParser.java
Sun Feb 22 05:48:19 2009
@@ -5,7 +5,6 @@
 import org.apache.ibatis.mapping.SqlSource;
 import org.apache.ibatis.type.TypeHandler;
 import org.apache.ibatis.xml.GenericTokenParser;
-import org.apache.ibatis.xml.NodeletContext;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -19,10 +18,10 @@
     this.typeHandlerRegistry = configuration.getTypeHandlerRegistry();
   }
 
-  public SqlSource parse(NodeletContext context) {
+  public SqlSource parse(String originalSql) {
     ParameterMappingTokenHandler handler = new ParameterMappingTokenHandler();
     GenericTokenParser parser = new GenericTokenParser("#{", "}", handler);
-    String sql = parser.parse(context.getStringBody());
+    String sql = parser.parse(originalSql);
     return new InlineSqlSource(sql, handler.getParameterMappings());
   }
 

Modified: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/binding/BindingTest.java
URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/binding/BindingTest.java?rev=746639&r1=746638&r2=746639&view=diff
==============================================================================
--- ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/binding/BindingTest.java
(original)
+++ ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/binding/BindingTest.java
Sun Feb 22 05:48:19 2009
@@ -17,7 +17,7 @@
   }
 
   @Test
-  public void shouldExecuteBoundSelectStatement() {
+  public void shouldExecuteBoundSelectListStatement() {
     SqlSession session = sqlSessionFactory.openSession();
     try {
       BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
@@ -29,5 +29,18 @@
 
   }
 
+  @Test
+  public void shouldExecuteBoundSelectOneStatement() {
+    SqlSession session = sqlSessionFactory.openSession();
+    try {
+      BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
+      Blog blogs = mapper.selectBlog(1);
+      System.out.println(blogs.getId());
+    } finally {
+      session.close();
+    }
+
+  }
+
 
 }

Modified: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/binding/BoundBlogMapper.java
URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/binding/BoundBlogMapper.java?rev=746639&r1=746638&r2=746639&view=diff
==============================================================================
--- ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/binding/BoundBlogMapper.java
(original)
+++ ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/binding/BoundBlogMapper.java
Sun Feb 22 05:48:19 2009
@@ -27,4 +27,8 @@
       })
   List<Blog> selectBlogs();
 
+  @Select("SELECT * FROM " +
+      "blog WHERE id = #{id}")
+  Blog selectBlog(int id);
+
 }



Mime
View raw message