XMLLanguageDriver.java

  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. import org.apache.ibatis.builder.xml.XMLMapperEntityResolver;
  18. import org.apache.ibatis.executor.parameter.ParameterHandler;
  19. import org.apache.ibatis.mapping.BoundSql;
  20. import org.apache.ibatis.mapping.MappedStatement;
  21. import org.apache.ibatis.mapping.SqlSource;
  22. import org.apache.ibatis.parsing.PropertyParser;
  23. import org.apache.ibatis.parsing.XNode;
  24. import org.apache.ibatis.parsing.XPathParser;
  25. import org.apache.ibatis.scripting.LanguageDriver;
  26. import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
  27. import org.apache.ibatis.scripting.defaults.RawSqlSource;
  28. import org.apache.ibatis.session.Configuration;

  29. /**
  30.  * @author Eduardo Macarron
  31.  */
  32. public class XMLLanguageDriver implements LanguageDriver {

  33.   @Override
  34.   public ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject,
  35.       BoundSql boundSql) {
  36.     return new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
  37.   }

  38.   @Override
  39.   public SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType) {
  40.     XMLScriptBuilder builder = new XMLScriptBuilder(configuration, script, parameterType);
  41.     return builder.parseScriptNode();
  42.   }

  43.   @Override
  44.   public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {
  45.     // issue #3
  46.     if (script.startsWith("<script>")) {
  47.       XPathParser parser = new XPathParser(script, false, configuration.getVariables(), new XMLMapperEntityResolver());
  48.       return createSqlSource(configuration, parser.evalNode("/script"), parameterType);
  49.     }
  50.     // issue #127
  51.     script = PropertyParser.parse(script, configuration.getVariables());
  52.     TextSqlNode textSqlNode = new TextSqlNode(script);
  53.     if (textSqlNode.isDynamic()) {
  54.       return new DynamicSqlSource(configuration, textSqlNode);
  55.     } else {
  56.       return new RawSqlSource(configuration, script, parameterType);
  57.     }
  58.   }

  59. }