All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.viiyue.plugins.mybatis.template.TemplateHandler Maven / Gradle / Ivy

Go to download

Mybatis generic mapper plugin for solving most basic operations, simplifying sql syntax and improving dynamic execution efficiency

There is a newer version: 1.3.7
Show newest version
/**
 * Copyright (C) 2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.viiyue.plugins.mybatis.template;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.session.Configuration;

import com.viiyue.plugins.mybatis.Constants;
import com.viiyue.plugins.mybatis.enums.Setting;
import com.viiyue.plugins.mybatis.template.builder.ColumnBuilder;
import com.viiyue.plugins.mybatis.template.handler.DynamicTemplateHandler;
import com.viiyue.plugins.mybatis.template.handler.ExceptionHandler;
import com.viiyue.plugins.mybatis.template.handler.ExpressionHandler;
import com.viiyue.plugins.mybatis.template.handler.KeywordsHandler;
import com.viiyue.plugins.mybatis.template.handler.MybatisParameterHandler;
import com.viiyue.plugins.mybatis.template.handler.ParameterHandler;
import com.viiyue.plugins.mybatis.template.handler.StaticTemplateHandler;
import com.viiyue.plugins.mybatis.template.handler.base.AbstractHandler;
import com.viiyue.plugins.mybatis.utils.StringUtil;

/**
 * Template syntax handler for parsing all template syntax that appears in text
 *
 * @author tangxbai
 * @since 1.1.0
 */
public final class TemplateHandler {
	
	/**
	 * Template exception handler
	 * 
	 * 

* <error>message</error> *

*/ private static final ExceptionHandler exceptionHandler = new ExceptionHandler(); // message /** * Expression template handler * *

* @{expression} *

*/ private static final ExpressionHandler expressionHandler = new ExpressionHandler(); // @{expression} /** * Expression template handler * *

* [keywords] *

*/ private static final KeywordsHandler keywordsHandler = new KeywordsHandler(); // [expression] /** * Parameter template handler * *

* {{parameter expression}} *

* *

Internal value expression:

*
    *
  • {{env.xxx}} See {@link Constants#ENV_PROP_NAMES}
  • *
  • {{system.xxx}} See {@link TemplateEngine#systemContext()}
  • *
*/ private static final ParameterHandler parameterTemplateHandler = new ParameterHandler(); // {{expression}} /** * Dynamic expression template handler * *

* %{expression} *

*/ private static final DynamicTemplateHandler dynamicTemplateHandler = new DynamicTemplateHandler(); // %{expression} /** * Static expression template handler * *

* @{expression} *

*/ private static final StaticTemplateHandler staticTemplateHandler = new StaticTemplateHandler(); // @{expression} /** * Mybatis parameter expression template handler * *

* #{propertyName, javaType=String, jdbcType=VARCHAR} *

*/ private static final MybatisParameterHandler mybatisParameterHandler = new MybatisParameterHandler(); // #{property, jdbcType=?, javaType=?} /** * All template handlers */ private static final AbstractHandler[] allHandlers = { exceptionHandler, keywordsHandler, expressionHandler, parameterTemplateHandler, dynamicTemplateHandler, mybatisParameterHandler }; /** * Check if the template text contains exception information * *

* <error>message</error> *

* * @param content template text content * @param namespace statement node id * @return */ public static void checkException( String content, String namespace ) { exceptionHandler.parse( null, null, null, content, namespace ); } /** * Obtain the mapping of each part by parsing the mybatis parameter text * *

* #{propertyName, javaType=String, jdbcType=VARCHAR} *

* * @param content template content text * @return mybatis parameter mappings */ public static List> processMybatisParameters( String content ) { return mybatisParameterHandler.parse( content ); } /** * Used to process style expression templates * *

* @{expression} *

* * @param content template text content * @param column column builder * @return processed text */ public static String processExpressionTemplate( String content, ColumnBuilder column ) { return expressionHandler.parse( null, null, null, content, column ); } /** * Translate keyword conversions in text content. If you are using uppercase * keywords, {@code [select]} will be translated into {@code [SELECT]}. * *

* [keywords] *

* * @param configuration mybatis core configuration object * @param content template text content * @return processed text */ public static String processKeyWordsTemplate( Configuration configuration, String content ) { return keywordsHandler.parse( configuration, null, null, content, Setting.KeywordsToUppercase.isEnable() ); } /** * Handling static template content in text * *

* @{expression} *

* * @param configuration mybatis core configuration object * @param statemntType statement command type * @param content template text content * @param modelBeanType database model bean type * @return processed text */ public static String processStaticTemplate( Configuration configuration, SqlCommandType statemntType, String content, Class modelBeanType ) { return staticTemplateHandler.parse( configuration, statemntType, modelBeanType, content, null ); } /** * The dynamic template content in the text is processed, and the dynamic * translation is performed according to the parameters passed during the * execution of the SQL. * *

* %{expression} *

* * @param configuration mybatis core configuration object * @param statemntType statement command type * @param content template text content * @param modelBeanType database model bean type * @param parameter dynamic parameter object * @return processed text */ public static String processDynamicTemplate( Configuration configuration, SqlCommandType statemntType, String content, Class modelBeanType, Object parameter ) { return dynamicTemplateHandler.parse( configuration, statemntType, modelBeanType, content, parameter ); } /** * Handling parameter value expressions in text * *

* {{parameter expression}} *

* * @param configuration mybatis core configuration object * @param statemntType statement command type * @param content template text content * @param modelBeanType database model bean type * @param parameter dynamic parameter object * @return processed text */ public static String processParameterTemplate( Configuration configuration, SqlCommandType statemntType, String content, Class modelBeanType, Object parameter ) { return parameterTemplateHandler.parse( configuration, statemntType, modelBeanType, content, parameter ); } /** * Remove all comments in the text, support multi-line text comments. * * @param content template text content * @return the clean text */ public static String processTextComments( String content ) { // Replace all comments with blanks content = Constants.TEXT_COMMENTS_PATTERN.matcher( content ).replaceAll( "$2" ); // Instead of replaceAll() // Remove the excess blanks content = StringUtil.normalizeSpace( content ); // Instead of replaceAll() return content; } /** * Whether the dynamic template syntax is also included in the car template * text * *

* %{expression} *

* * @param content template text content * @return {@code true} means that the dynamic template syntax is included, {@code false} means no. */ public static boolean isNeedDynamicProcessing( String content ) { for ( AbstractHandler handler : allHandlers ) { if ( handler.isDynamicHandler() && handler.canHandle( content ) ) { return true; } } return false; } /** * Detect if any template syntax that any handler can handle * * @param content template text content * @return {@code true} means it can be processed, {@code false} can't. */ public static boolean isTemplateContent( String content ) { for ( AbstractHandler handler : allHandlers ) { if ( handler.canHandle( content ) ) { return true; } } return false; } }