001/*
002 *  Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.mybatisflex.core.table;
017
018import com.mybatisflex.core.util.StringUtil;
019
020import java.util.HashMap;
021import java.util.Map;
022
023public class TableManager {
024
025    private TableManager() {
026    }
027
028    private static DynamicTableProcessor dynamicTableProcessor;
029    private static DynamicSchemaProcessor dynamicSchemaProcessor;
030
031    private static final ThreadLocal<Map<String, String>> tableNameMappingTL = ThreadLocal.withInitial(HashMap::new);
032    private static final ThreadLocal<Map<String, String>> schemaMappingTL = ThreadLocal.withInitial(HashMap::new);
033
034
035    public static DynamicTableProcessor getDynamicTableProcessor() {
036        return dynamicTableProcessor;
037    }
038
039    public static void setDynamicTableProcessor(DynamicTableProcessor dynamicTableProcessor) {
040        TableManager.dynamicTableProcessor = dynamicTableProcessor;
041    }
042
043    public static DynamicSchemaProcessor getDynamicSchemaProcessor() {
044        return dynamicSchemaProcessor;
045    }
046
047    public static void setDynamicSchemaProcessor(DynamicSchemaProcessor dynamicSchemaProcessor) {
048        TableManager.dynamicSchemaProcessor = dynamicSchemaProcessor;
049    }
050
051    public static void setHintTableMapping(String tableName, String mappingTable) {
052        tableNameMappingTL.get().put(tableName, mappingTable);
053    }
054
055    public static String getHintTableMapping(String tableName) {
056        return tableNameMappingTL.get().get(tableName);
057    }
058
059    public static void setHintSchemaMapping(String schema, String mappingSchema) {
060        schemaMappingTL.get().put(schema, mappingSchema);
061    }
062
063    public static String getHintSchemaMapping(String schema) {
064        return schemaMappingTL.get().get(schema);
065    }
066
067
068    public static String getRealTable(String tableName) {
069        if (dynamicTableProcessor == null) {
070            return tableName;
071        }
072
073        Map<String, String> mapping = tableNameMappingTL.get();
074
075        String dynamicTableName = mapping.get(tableName);
076        if (StringUtil.isNotBlank(dynamicTableName)) {
077            return dynamicTableName;
078        }
079
080        dynamicTableName = dynamicTableProcessor.process(tableName);
081        mapping.put(tableName, dynamicTableName);
082        return dynamicTableName;
083    }
084
085
086    public static String getRealSchema(String schema) {
087        if (dynamicSchemaProcessor == null) {
088            return schema;
089        }
090
091        Map<String, String> mapping = schemaMappingTL.get();
092        String dynamiSchema = mapping.get(schema);
093        if (StringUtil.isNotBlank(dynamiSchema)) {
094            return dynamiSchema;
095        }
096
097        dynamiSchema = dynamicSchemaProcessor.process(schema);
098        mapping.put(schema, dynamiSchema);
099        return dynamiSchema;
100    }
101
102
103//    public static void clear() {
104//        if (dynamicTableProcessor != null) {
105//            tableNameMappingTL.remove();
106//        }
107//        if (dynamicSchemaProcessor != null) {
108//            schemaMappingTL.remove();
109//        }
110//    }
111
112}