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