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 023/** 024 * @author michael 025 */ 026public class TableManager { 027 028 private TableManager() { 029 } 030 031 private static DynamicTableProcessor dynamicTableProcessor; 032 private static DynamicSchemaProcessor dynamicSchemaProcessor; 033 034 private static final ThreadLocal<Map<String, String>> tableNameMappingTL = new ThreadLocal<>(); 035 private static final ThreadLocal<Map<String, String>> schemaMappingTL = new ThreadLocal<>(); 036 037 038 public static DynamicTableProcessor getDynamicTableProcessor() { 039 return dynamicTableProcessor; 040 } 041 042 public static void setDynamicTableProcessor(DynamicTableProcessor dynamicTableProcessor) { 043 TableManager.dynamicTableProcessor = dynamicTableProcessor; 044 } 045 046 public static DynamicSchemaProcessor getDynamicSchemaProcessor() { 047 return dynamicSchemaProcessor; 048 } 049 050 public static void setDynamicSchemaProcessor(DynamicSchemaProcessor dynamicSchemaProcessor) { 051 TableManager.dynamicSchemaProcessor = dynamicSchemaProcessor; 052 } 053 054 public static void setHintTableMapping(String tableName, String mappingTable) { 055 Map<String, String> hintTables = tableNameMappingTL.get(); 056 if (hintTables == null) { 057 hintTables = new HashMap<>(); 058 tableNameMappingTL.set(hintTables); 059 } 060 hintTables.put(tableName, mappingTable); 061 } 062 063 public static String getHintTableMapping(String tableName) { 064 return tableNameMappingTL.get().get(tableName); 065 } 066 067 public static void setHintSchemaMapping(String schema, String mappingSchema) { 068 Map<String, String> hintTables = schemaMappingTL.get(); 069 if (hintTables == null) { 070 hintTables = new HashMap<>(); 071 schemaMappingTL.set(hintTables); 072 } 073 hintTables.put(schema, mappingSchema); 074 } 075 076 public static String getHintSchemaMapping(String schema) { 077 return schemaMappingTL.get().get(schema); 078 } 079 080 081 public static String getRealTable(String tableName) { 082 083 Map<String, String> mapping = tableNameMappingTL.get(); 084 if (mapping != null) { 085 String dynamicTableName = mapping.get(tableName); 086 if (StringUtil.isNotBlank(dynamicTableName)) { 087 return dynamicTableName; 088 } 089 } 090 091 if (dynamicTableProcessor == null) { 092 return tableName; 093 } 094 095 String dynamicTableName = dynamicTableProcessor.process(tableName); 096 return StringUtil.isNotBlank(dynamicTableName) ? dynamicTableName : tableName; 097 } 098 099 100 public static String getRealSchema(String schema, String table) { 101 Map<String, String> mapping = schemaMappingTL.get(); 102 if (mapping != null) { 103 String dynamicSchema = mapping.get(schema); 104 if (StringUtil.isNotBlank(dynamicSchema)) { 105 return dynamicSchema; 106 } 107 } 108 109 if (dynamicSchemaProcessor == null) { 110 return schema; 111 } 112 113 String dynamicSchema = dynamicSchemaProcessor.process(schema, table); 114 return StringUtil.isNotBlank(dynamicSchema) ? dynamicSchema : schema; 115 } 116 117 118 public static void clear() { 119 tableNameMappingTL.remove(); 120 schemaMappingTL.remove(); 121 } 122 123}