RawLanguageDriver.java

  1. /*
  2.  *    Copyright 2009-2021 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. import org.apache.ibatis.builder.BuilderException;
  18. import org.apache.ibatis.mapping.SqlSource;
  19. import org.apache.ibatis.parsing.XNode;
  20. import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;
  21. import org.apache.ibatis.session.Configuration;

  22. /**
  23.  * As of 3.2.4 the default XML language is able to identify static statements
  24.  * and create a {@link RawSqlSource}. So there is no need to use RAW unless you
  25.  * want to make sure that there is not any dynamic tag for any reason.
  26.  *
  27.  * @since 3.2.0
  28.  * @author Eduardo Macarron
  29.  */
  30. public class RawLanguageDriver extends XMLLanguageDriver {

  31.   @Override
  32.   public SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType) {
  33.     SqlSource source = super.createSqlSource(configuration, script, parameterType);
  34.     checkIsNotDynamic(source);
  35.     return source;
  36.   }

  37.   @Override
  38.   public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {
  39.     SqlSource source = super.createSqlSource(configuration, script, parameterType);
  40.     checkIsNotDynamic(source);
  41.     return source;
  42.   }

  43.   private void checkIsNotDynamic(SqlSource source) {
  44.     if (!RawSqlSource.class.equals(source.getClass())) {
  45.       throw new BuilderException("Dynamic content is not allowed when using RAW language");
  46.     }
  47.   }

  48. }