/*
 * Copyright (c) 2011-2020, baomidou (jobob@qq.com).
 * <p>
 * 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
 * <p>
 * https://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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.baomidou.mybatisplus.extension.parsers;

import com.baomidou.mybatisplus.core.parser.ISqlParser;
import com.baomidou.mybatisplus.core.parser.SqlInfo;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.TableNameParser;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Collection;
import java.util.Map;

/**
 * 动态表名 SQL 解析器
 *
 * @author jobob
 * @since 2019-04-23
 */
@Data
@Accessors(chain = true)
public class DynamicTableNameParser implements ISqlParser {
    private Map<String, ITableNameHandler> tableNameHandlerMap;

    @Override
    public SqlInfo parser(MetaObject metaObject, String sql) {
        Assert.isFalse(CollectionUtils.isEmpty(tableNameHandlerMap), "tableNameHandlerMap is empty.");
        Collection<String> tables = new TableNameParser(sql).tables();
        if (CollectionUtils.isNotEmpty(tables)) {
            int flag = 0;
            for (final String table : tables) {
                ITableNameHandler tableNameHandler = tableNameHandlerMap.get(table);
                if (null != tableNameHandler) {
                    tableNameHandler.process(metaObject, sql, table);
                    ++flag;
                }
            }
            if (flag > 0) {
                return SqlInfo.newInstance().setSql(sql);
            }
        }
        return null;
    }
}

@@ -1519,16 +1519,60 @@
 pty.%22);%0A
+        if (allowProcess(metaObject)) %7B%0A    
         
@@ -1638,24 +1638,28 @@
 ();%0A        
+    
 if (Collecti
@@ -1700,16 +1700,20 @@
         
+   
+ 
 int flag
@@ -1718,16 +1718,20 @@
 ag = 0;%0A
+    
         
@@ -1766,24 +1766,28 @@
 : tables) %7B%0A
+    
             
@@ -1871,24 +1871,28 @@
             
+ 
+   
 if (null != 
@@ -1907,24 +1907,28 @@
 eHandler) %7B%0A
+    
             
@@ -2009,16 +2009,20 @@
     
+   
+ 
 ++flag;%0A
     
@@ -2017,16 +2017,20 @@
 ++flag;%0A
+    
         
@@ -2043,32 +2043,36 @@
   %7D%0A            
+    
 %7D%0A            if
@@ -2065,24 +2065,28 @@
             
+ 
+   
 if (flag %3E 0
@@ -2085,24 +2085,28 @@
 flag %3E 0) %7B%0A
+    
             
@@ -2151,16 +2151,34 @@
 l(sql);%0A
+                %7D%0A
         
@@ -2220,10 +2220,228 @@
 ;%0A    %7D%0A
+%0A%0A    /**%0A     * %E5%88%A4%E6%96%AD%E6%98%AF%E5%90%A6%E5%85%81%E8%AE%B8%E6%89%A7%E8%A1%8C%0A     * %3Cp%3E%E4%BE%8B%E5%A6%82%EF%BC%9A%E9%80%BB%E8%BE%91%E5%88%A0%E9%99%A4%E5%8F%AA%E8%A7%A3%E6%9E%90 delete , update %E6%93%8D%E4%BD%9C%3C/p%3E%0A     *%0A     * @param metaObject %E5%85%83%E5%AF%B9%E8%B1%A1%0A     * @return true%0A     */%0A    public boolean allowProcess(MetaObject metaObject) %7B%0A        return true;%0A    %7D%0A
 %7D%0A

支持动态表名 SQL 解析