View Javadoc
1   /**
2    *    Copyright 2009-2019 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.scripting.defaults;
17  
18  import java.util.HashMap;
19  
20  import org.apache.ibatis.builder.SqlSourceBuilder;
21  import org.apache.ibatis.mapping.BoundSql;
22  import org.apache.ibatis.mapping.SqlSource;
23  import org.apache.ibatis.scripting.xmltags.DynamicContext;
24  import org.apache.ibatis.scripting.xmltags.DynamicSqlSource;
25  import org.apache.ibatis.scripting.xmltags.SqlNode;
26  import org.apache.ibatis.session.Configuration;
27  
28  /**
29   * Static SqlSource. It is faster than {@link DynamicSqlSource} because mappings are
30   * calculated during startup.
31   *
32   * @since 3.2.0
33   * @author Eduardo Macarron
34   */
35  public class RawSqlSource implements SqlSource {
36  
37    private final SqlSource sqlSource;
38  
39    public RawSqlSource(Configuration configuration, SqlNode rootSqlNode, Class<?> parameterType) {
40      this(configuration, getSql(configuration, rootSqlNode), parameterType);
41    }
42  
43    public RawSqlSource(Configuration configuration, String sql, Class<?> parameterType) {
44      SqlSourceBuildertml#SqlSourceBuilder">SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration);
45      Class<?> clazz = parameterType == null ? Object.class : parameterType;
46      sqlSource = sqlSourceParser.parse(sql, clazz, new HashMap<>());
47    }
48  
49    private static String getSql(Configuration configuration, SqlNode rootSqlNode) {
50      DynamicContexts/DynamicContext.html#DynamicContext">DynamicContext context = new DynamicContext(configuration, null);
51      rootSqlNode.apply(context);
52      return context.getSql();
53    }
54  
55    @Override
56    public BoundSql getBoundSql(Object parameterObject) {
57      return sqlSource.getBoundSql(parameterObject);
58    }
59  
60  }