You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ibatis.apache.org by cb...@apache.org on 2009/03/21 05:20:45 UTC
svn commit: r756873 - in
/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/binding:
DynamicInlineSqlSource.java MapperAnnotationParser.java
Author: cbegin
Date: Sat Mar 21 04:20:43 2009
New Revision: 756873
URL: http://svn.apache.org/viewvc?rev=756873&view=rev
Log:
Added SqlProvider support to annotation parser
Added:
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/binding/DynamicInlineSqlSource.java
Modified:
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/binding/MapperAnnotationParser.java
Added: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/binding/DynamicInlineSqlSource.java
URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/binding/DynamicInlineSqlSource.java?rev=756873&view=auto
==============================================================================
--- ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/binding/DynamicInlineSqlSource.java (added)
+++ ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/binding/DynamicInlineSqlSource.java Sat Mar 21 04:20:43 2009
@@ -0,0 +1,65 @@
+package org.apache.ibatis.binding;
+
+import static org.apache.ibatis.annotations.Annotations.SqlProvider;
+import org.apache.ibatis.mapping.Configuration;
+import org.apache.ibatis.mapping.ParameterMapping;
+import org.apache.ibatis.mapping.SqlSource;
+import org.apache.ibatis.parser.SqlSourceParser;
+
+import java.lang.reflect.Method;
+import java.util.List;
+
+public class DynamicInlineSqlSource implements SqlSource {
+
+ private SqlSourceParser sqlSourceParser;
+ private Class providerType;
+ private Method providerMethod;
+ private boolean providerTakesParameterObject;
+
+ public DynamicInlineSqlSource(Configuration config, SqlProvider provider) {
+ try {
+ this.sqlSourceParser = new SqlSourceParser(config);
+ this.providerType = provider.type();
+ Class providerType = provider.type();
+ String providerMethod = provider.method();
+ for (Method m : providerType.getMethods()) {
+ if (providerMethod.equals(m.getName())) {
+ if (m.getParameterTypes().length < 2
+ && m.getReturnType() == String.class) {
+ this.providerMethod = m;
+ this.providerTakesParameterObject = m.getParameterTypes().length == 1;
+ }
+ }
+ }
+ } catch (Exception e) {
+ throw new RuntimeException("Error creating SqlSource for SqlProvider. Cause: " + e, e);
+ }
+ }
+
+ public String getSql(Object parameterObject) {
+ SqlSource sqlSource = createSqlSource(parameterObject);
+ return sqlSource.getSql(parameterObject);
+ }
+
+ public List<ParameterMapping> getParameterMappings(Object parameterObject) {
+ SqlSource sqlSource = createSqlSource(parameterObject);
+ return sqlSource.getParameterMappings(parameterObject);
+ }
+
+ private SqlSource createSqlSource(Object parameterObject) {
+ try {
+ String sql;
+ if (providerTakesParameterObject) {
+ sql = (String) providerMethod.invoke(providerType.newInstance(), parameterObject);
+ } else {
+ sql = (String) providerMethod.invoke(providerType.newInstance());
+ }
+ return sqlSourceParser.parse(sql);
+ } catch (Exception e) {
+ throw new RuntimeException("Error invoking SqlProvider method ("
+ + providerType.getName() + "." + providerMethod.getName()
+ + "). Cause: " + e, e);
+ }
+ }
+
+}
Modified: ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/binding/MapperAnnotationParser.java
URL: http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/binding/MapperAnnotationParser.java?rev=756873&r1=756872&r2=756873&view=diff
==============================================================================
--- ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/binding/MapperAnnotationParser.java (original)
+++ ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/binding/MapperAnnotationParser.java Sat Mar 21 04:20:43 2009
@@ -153,7 +153,7 @@
Class annotationType = getSqlAnnotationType(method);
Options options = method.getAnnotation(Options.class);
if (annotationType != null) {
- final String sql = getSqlAnnotationValue(method, annotationType);
+ final SqlSource sqlSource = getSqlAnnotationValue(method, annotationType);
final String mappedStatementId = method.getDeclaringClass().getName() + "." + method.getName();
boolean isSelect = method.getAnnotation(Select.class) != null;
boolean flushCache = false;
@@ -170,7 +170,6 @@
statementType = options.statementType();
resultSetType = options.resultSetType();
}
- SqlSource sqlSource = new SqlSourceParser(configurator.getConfiguration()).parse(sql);
configurator.statement(
mappedStatementId,
sqlSource,
@@ -216,18 +215,22 @@
return returnType;
}
- private String getSqlAnnotationValue(Method method, Class annotationType) {
+ private SqlSource getSqlAnnotationValue(Method method, Class annotationType) {
Annotation annotation = method.getAnnotation(annotationType);
if (annotation != null) {
try {
- String[] strings = (String[]) annotation.getClass().getMethod("value").invoke(annotation);
+ final String[] strings = (String[]) annotation.getClass().getMethod("value").invoke(annotation);
+ final SqlProvider provider = (SqlProvider) annotation.getClass().getMethod("sqlProvider").invoke(annotation);
if (strings != null && strings.length > 0) {
StringBuilder sql = new StringBuilder();
for (String fragment : strings) {
sql.append(fragment);
sql.append(" ");
}
- return sql.toString();
+ SqlSourceParser parser = new SqlSourceParser(configurator.getConfiguration());
+ return parser.parse(sql.toString());
+ } else if (provider != null) {
+ return new DynamicInlineSqlSource(configurator.getConfiguration(), provider);
}
} catch (Exception e) {
throw new RuntimeException("Could not find value method on SQL annotation. Cause: " + e, e);