View Javadoc
1   /*
2    *    Copyright 2009-2023 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    *       https://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.xmltags;
17  
18  import org.apache.ibatis.builder.xml.XMLMapperEntityResolver;
19  import org.apache.ibatis.executor.parameter.ParameterHandler;
20  import org.apache.ibatis.mapping.BoundSql;
21  import org.apache.ibatis.mapping.MappedStatement;
22  import org.apache.ibatis.mapping.SqlSource;
23  import org.apache.ibatis.parsing.PropertyParser;
24  import org.apache.ibatis.parsing.XNode;
25  import org.apache.ibatis.parsing.XPathParser;
26  import org.apache.ibatis.scripting.LanguageDriver;
27  import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
28  import org.apache.ibatis.scripting.defaults.RawSqlSource;
29  import org.apache.ibatis.session.Configuration;
30  
31  /**
32   * @author Eduardo Macarron
33   */
34  public class XMLLanguageDriver implements LanguageDriver {
35  
36    @Override
37    public ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject,
38        BoundSql boundSql) {
39      return new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
40    }
41  
42    @Override
43    public SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType) {
44      XMLScriptBuilder builder = new XMLScriptBuilder(configuration, script, parameterType);
45      return builder.parseScriptNode();
46    }
47  
48    @Override
49    public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {
50      // issue #3
51      if (script.startsWith("<script>")) {
52        XPathParser parser = new XPathParser(script, false, configuration.getVariables(), new XMLMapperEntityResolver());
53        return createSqlSource(configuration, parser.evalNode("/script"), parameterType);
54      }
55      // issue #127
56      script = PropertyParser.parse(script, configuration.getVariables());
57      TextSqlNode textSqlNode = new TextSqlNode(script);
58      if (textSqlNode.isDynamic()) {
59        return new DynamicSqlSource(configuration, textSqlNode);
60      } else {
61        return new RawSqlSource(configuration, script, parameterType);
62      }
63    }
64  
65  }