Skip to content

Commit 50a38e1

Browse files
committed
Allow replacing configuration properties on ProviderSqlSource
Fixes gh-1057
1 parent 45712f3 commit 50a38e1

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.ibatis.builder.SqlSourceBuilder;
2424
import org.apache.ibatis.mapping.BoundSql;
2525
import org.apache.ibatis.mapping.SqlSource;
26+
import org.apache.ibatis.parsing.PropertyParser;
2627
import org.apache.ibatis.reflection.ParamNameResolver;
2728
import org.apache.ibatis.session.Configuration;
2829

@@ -32,6 +33,7 @@
3233
*/
3334
public class ProviderSqlSource implements SqlSource {
3435

36+
private final Configuration configuration;
3537
private final SqlSourceBuilder sqlSourceParser;
3638
private final Class<?> providerType;
3739
private Method providerMethod;
@@ -44,17 +46,18 @@ public class ProviderSqlSource implements SqlSource {
4446
* @deprecated Please use the {@link #ProviderSqlSource(Configuration, Object, Class, Method)} instead of this.
4547
*/
4648
@Deprecated
47-
public ProviderSqlSource(Configuration config, Object provider) {
48-
this(config, provider, null, null);
49+
public ProviderSqlSource(Configuration configuration, Object provider) {
50+
this(configuration, provider, null, null);
4951
}
5052

5153
/**
5254
* @since 3.4.5
5355
*/
54-
public ProviderSqlSource(Configuration config, Object provider, Class<?> mapperType, Method mapperMethod) {
56+
public ProviderSqlSource(Configuration configuration, Object provider, Class<?> mapperType, Method mapperMethod) {
5557
String providerMethodName;
5658
try {
57-
this.sqlSourceParser = new SqlSourceBuilder(config);
59+
this.configuration = configuration;
60+
this.sqlSourceParser = new SqlSourceBuilder(configuration);
5861
this.providerType = (Class<?>) provider.getClass().getMethod("type").invoke(provider);
5962
providerMethodName = (String) provider.getClass().getMethod("method").invoke(provider);
6063

@@ -67,7 +70,7 @@ public ProviderSqlSource(Configuration config, Object provider, Class<?> mapperT
6770
+ "'. Sql provider method can not overload.");
6871
}
6972
this.providerMethod = m;
70-
this.providerMethodArgumentNames = new ParamNameResolver(config, m).getNames();
73+
this.providerMethodArgumentNames = new ParamNameResolver(configuration, m).getNames();
7174
this.providerMethodParameterTypes = m.getParameterTypes();
7275
}
7376
}
@@ -125,7 +128,7 @@ private SqlSource createSqlSource(Object parameterObject) {
125128
+ " using a specifying parameterObject. In this case, please specify a 'java.util.Map' object.");
126129
}
127130
Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
128-
return sqlSourceParser.parse(sql, parameterType, new HashMap<String, Object>());
131+
return sqlSourceParser.parse(replacePlaceholder(sql), parameterType, new HashMap<String, Object>());
129132
} catch (BuilderException e) {
130133
throw e;
131134
} catch (Exception e) {
@@ -158,4 +161,8 @@ private Object[] extractProviderMethodArguments(Map<String, Object> params, Stri
158161
return args;
159162
}
160163

164+
private String replacePlaceholder(String sql) {
165+
return PropertyParser.parse(sql, configuration.getVariables());
166+
}
167+
161168
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public String buildSelectByIdProviderContextOnly(ProviderContext context) {
157157
FROM(tableName);
158158
WHERE("id = #{id}");
159159
if (!containsLogicalDelete){
160-
WHERE("logical_delete = false");
160+
WHERE("logical_delete = ${Constants.LOGICAL_DELETE_OFF}");
161161
}
162162
}}.toString();
163163
}
@@ -172,7 +172,7 @@ public String buildSelectByNameOneParamAndProviderContext(ProviderContext contex
172172
WHERE("name like #{name} || '%'");
173173
}
174174
if (!containsLogicalDelete){
175-
WHERE("logical_delete = false");
175+
WHERE("logical_delete = ${LOGICAL_DELETE_OFF:0}");
176176
}
177177
}}.toString();
178178
}

src/test/java/org/apache/ibatis/submitted/sqlprovider/mybatis-config.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222

2323
<configuration>
2424

25+
<properties>
26+
<property name="Constants.LOGICAL_DELETE_OFF" value="false"/>
27+
<property name="org.apache.ibatis.parsing.PropertyParser.enable-default-value" value="true"/>
28+
</properties>
29+
2530
<environments default="development">
2631
<environment id="development">
2732
<transactionManager type="JDBC">

0 commit comments

Comments
 (0)