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