|
@@ -1,7 +1,9 @@
|
|
package com.chelvc.framework.database.interceptor;
|
|
package com.chelvc.framework.database.interceptor;
|
|
|
|
|
|
import java.io.Serializable;
|
|
import java.io.Serializable;
|
|
|
|
+import java.util.ArrayDeque;
|
|
import java.util.Collections;
|
|
import java.util.Collections;
|
|
|
|
+import java.util.Deque;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
import java.util.Objects;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
@@ -28,9 +30,9 @@ import org.apache.ibatis.type.UnknownTypeHandler;
|
|
*/
|
|
*/
|
|
final class Tables {
|
|
final class Tables {
|
|
/**
|
|
/**
|
|
- * 执行上下文
|
|
|
|
|
|
+ * 上下文
|
|
*/
|
|
*/
|
|
- private static final ThreadLocal<Context> CONTEXT = new ThreadLocal<>();
|
|
|
|
|
|
+ private static final ThreadLocal<Deque<Context>> CONTEXT = ThreadLocal.withInitial(ArrayDeque::new);
|
|
|
|
|
|
/**
|
|
/**
|
|
* 表名称/对象映射表
|
|
* 表名称/对象映射表
|
|
@@ -64,6 +66,15 @@ final class Tables {
|
|
this.id = id;
|
|
this.id = id;
|
|
this.schema = schema;
|
|
this.schema = schema;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取命名空间
|
|
|
|
+ *
|
|
|
|
+ * @return 命名空间
|
|
|
|
+ */
|
|
|
|
+ public String getNamespace() {
|
|
|
|
+ return this.id.substring(0, this.id.lastIndexOf('.'));
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -187,7 +198,7 @@ final class Tables {
|
|
* @return 上下文实例
|
|
* @return 上下文实例
|
|
*/
|
|
*/
|
|
public static Context getContext() {
|
|
public static Context getContext() {
|
|
- return CONTEXT.get();
|
|
|
|
|
|
+ return CONTEXT.get().peek();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -196,10 +207,17 @@ final class Tables {
|
|
* @param context 上下文实例
|
|
* @param context 上下文实例
|
|
*/
|
|
*/
|
|
public static void setContext(Context context) {
|
|
public static void setContext(Context context) {
|
|
- if (context == null) {
|
|
|
|
|
|
+ CONTEXT.get().push(context);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 移除上下文
|
|
|
|
+ */
|
|
|
|
+ public static void removeContext() {
|
|
|
|
+ Deque<Context> deque = CONTEXT.get();
|
|
|
|
+ deque.poll();
|
|
|
|
+ if (deque.isEmpty()) {
|
|
CONTEXT.remove();
|
|
CONTEXT.remove();
|
|
- } else {
|
|
|
|
- CONTEXT.set(context);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -224,19 +242,17 @@ final class Tables {
|
|
* @return 表信息
|
|
* @return 表信息
|
|
*/
|
|
*/
|
|
public static Table getTable(@NonNull String name) {
|
|
public static Table getTable(@NonNull String name) {
|
|
- Context context = getContext();
|
|
|
|
String unquote = SQLUtils.unquote(name);
|
|
String unquote = SQLUtils.unquote(name);
|
|
- String schema = ObjectUtils.ifNull(context, Context::getSchema);
|
|
|
|
- String key = StringUtils.isEmpty(schema) ? unquote : (schema + StringPool.DOT + unquote);
|
|
|
|
|
|
+ Context context = Objects.requireNonNull(getContext());
|
|
|
|
+ String key = context.getSchema() + StringPool.DOT + unquote;
|
|
Table table = TABLE_NAME_MAPPING.get(key);
|
|
Table table = TABLE_NAME_MAPPING.get(key);
|
|
if (table == null) {
|
|
if (table == null) {
|
|
table = TABLE_NAME_MAPPING.computeIfAbsent(key, k -> {
|
|
table = TABLE_NAME_MAPPING.computeIfAbsent(key, k -> {
|
|
boolean quoted = SQLUtils.isQuoted(name);
|
|
boolean quoted = SQLUtils.isQuoted(name);
|
|
- String id = ObjectUtils.ifNull(context, Context::getId);
|
|
|
|
- String namespace = StringUtils.isEmpty(id) ? null : id.substring(0, id.lastIndexOf('.'));
|
|
|
|
|
|
+ String namespace = context.getNamespace();
|
|
for (TableInfo info : TableInfoHelper.getTableInfos()) {
|
|
for (TableInfo info : TableInfoHelper.getTableInfos()) {
|
|
- String s = info.getCurrentNamespace(), n = getSimpleName(info.getTableName());
|
|
|
|
- if ((Objects.equals(s, namespace) || StringUtils.isEmpty(namespace))
|
|
|
|
|
|
+ String n = getSimpleName(info.getTableName());
|
|
|
|
+ if (Objects.equals(info.getCurrentNamespace(), namespace)
|
|
&& (Objects.equals(n, name) || (quoted && Objects.equals(n, unquote)))) {
|
|
&& (Objects.equals(n, name) || (quoted && Objects.equals(n, unquote)))) {
|
|
return new Table(info);
|
|
return new Table(info);
|
|
}
|
|
}
|