1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.scripting.xmltags;
17
18 import org.apache.ibatis.builder.SqlSourceBuilder;
19 import org.apache.ibatis.mapping.BoundSql;
20 import org.apache.ibatis.mapping.SqlSource;
21 import org.apache.ibatis.session.Configuration;
22
23
24
25
26 public class DynamicSqlSource implements SqlSource {
27
28 private final Configuration configuration;
29 private final SqlNode rootSqlNode;
30
31 public DynamicSqlSource(Configuration configuration, SqlNode rootSqlNode) {
32 this.configuration = configuration;
33 this.rootSqlNode = rootSqlNode;
34 }
35
36 @Override
37 public BoundSql getBoundSql(Object parameterObject) {
38 DynamicContext context = new DynamicContext(configuration, parameterObject);
39 rootSqlNode.apply(context);
40 SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration);
41 Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
42 SqlSource sqlSource = sqlSourceParser.parse(context.getSql(), parameterType, context.getBindings());
43 BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
44 context.getBindings().forEach(boundSql::setAdditionalParameter);
45 return boundSql;
46 }
47
48 }