Skip to content

Commit b9b50ab

Browse files
committed
Supports CharSequence on SQL provider method return type
Fixes gh-1134
1 parent 43f53f7 commit b9b50ab

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

src/main/java/org/apache/ibatis/builder/annotation/ProviderSqlSource.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,15 @@ public ProviderSqlSource(Configuration configuration, Object provider, Class<?>
6363
providerMethodName = (String) provider.getClass().getMethod("method").invoke(provider);
6464

6565
for (Method m : this.providerType.getMethods()) {
66-
if (providerMethodName.equals(m.getName())) {
67-
if (m.getReturnType() == String.class) {
68-
if (providerMethod != null){
69-
throw new BuilderException("Error creating SqlSource for SqlProvider. Method '"
70-
+ providerMethodName + "' is found multiple in SqlProvider '" + this.providerType.getName()
71-
+ "'. Sql provider method can not overload.");
72-
}
73-
this.providerMethod = m;
74-
this.providerMethodArgumentNames = new ParamNameResolver(configuration, m).getNames();
75-
this.providerMethodParameterTypes = m.getParameterTypes();
66+
if (providerMethodName.equals(m.getName()) && CharSequence.class.isAssignableFrom(m.getReturnType())) {
67+
if (providerMethod != null){
68+
throw new BuilderException("Error creating SqlSource for SqlProvider. Method '"
69+
+ providerMethodName + "' is found multiple in SqlProvider '" + this.providerType.getName()
70+
+ "'. Sql provider method can not overload.");
7671
}
72+
this.providerMethod = m;
73+
this.providerMethodArgumentNames = new ParamNameResolver(configuration, m).getNames();
74+
this.providerMethodParameterTypes = m.getParameterTypes();
7775
}
7876
}
7977
} catch (BuilderException e) {
@@ -166,7 +164,8 @@ private String invokeProviderMethod(Object... args) throws Exception {
166164
if (!Modifier.isStatic(providerMethod.getModifiers())) {
167165
targetObject = providerType.newInstance();
168166
}
169-
return (String) providerMethod.invoke(targetObject, args);
167+
CharSequence sql = (CharSequence) providerMethod.invoke(targetObject, args);
168+
return sql != null ? sql.toString() : null;
170169
}
171170

172171
private String replacePlaceholder(String sql) {

src/test/java/org/apache/ibatis/submitted/sqlprovider/SqlProviderTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -587,22 +587,24 @@ public interface StaticMethodSqlProviderMapper {
587587
String oneArgumentAndProviderContext(Integer value);
588588

589589
class SqlProvider {
590+
590591
public static String noArgument() {
591592
return "SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS";
592593
}
593594

594-
public static String oneArgument(Integer value) {
595-
return "SELECT " + value + " FROM INFORMATION_SCHEMA.SYSTEM_USERS";
595+
public static StringBuilder oneArgument(Integer value) {
596+
return new StringBuilder().append("SELECT ").append(value)
597+
.append(" FROM INFORMATION_SCHEMA.SYSTEM_USERS");
596598
}
597599

598-
public static String multipleArgument(@Param("value1") Integer value1,
600+
public static CharSequence multipleArgument(@Param("value1") Integer value1,
599601
@Param("value2") Integer value2) {
600602
return "SELECT " + (value1 + value2) + " FROM INFORMATION_SCHEMA.SYSTEM_USERS";
601603
}
602604

603-
public static String onlyProviderContext(ProviderContext context) {
604-
return "SELECT '" + context.getMapperMethod().getName()
605-
+ "' FROM INFORMATION_SCHEMA.SYSTEM_USERS";
605+
public static CharSequence onlyProviderContext(ProviderContext context) {
606+
return new StringBuilder().append("SELECT '").append(context.getMapperMethod().getName())
607+
.append("' FROM INFORMATION_SCHEMA.SYSTEM_USERS");
606608
}
607609

608610
public static String oneArgumentAndProviderContext(Integer value, ProviderContext context) {

0 commit comments

Comments
 (0)