|
@@ -36,17 +36,17 @@ final class Tables {
|
|
|
/**
|
|
|
* 表名称/对象映射表
|
|
|
*/
|
|
|
- private static final Map<String, Table> TABLES = Maps.newConcurrentMap();
|
|
|
+ private static final Map<String, Table> TABLE_MAPPING = Maps.newConcurrentMap();
|
|
|
|
|
|
/**
|
|
|
- * 上下文
|
|
|
+ * 类对象/类型处理器实例映射表
|
|
|
*/
|
|
|
- private static final ThreadLocal<Deque<Context>> CONTEXT = ThreadLocal.withInitial(ArrayDeque::new);
|
|
|
+ private static final Map<Class<?>, TypeHandler<?>> HANDLER_MAPPING = Maps.newConcurrentMap();
|
|
|
|
|
|
/**
|
|
|
- * 类对象/类型处理器实例映射表
|
|
|
+ * 上下文
|
|
|
*/
|
|
|
- private static final Map<Class<?>, TypeHandler<?>> TYPE_HANDLER_MAPPING = Maps.newConcurrentMap();
|
|
|
+ private static final ThreadLocal<Deque<Context>> CONTEXT = ThreadLocal.withInitial(ArrayDeque::new);
|
|
|
|
|
|
private Tables() {
|
|
|
}
|
|
@@ -131,25 +131,10 @@ final class Tables {
|
|
|
private final TableFieldInfo target;
|
|
|
private final TypeHandler<?> handler;
|
|
|
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
public Field(@NonNull Table table, @NonNull TableFieldInfo target) {
|
|
|
this.table = table;
|
|
|
this.target = target;
|
|
|
-
|
|
|
- Class<? extends TypeHandler<?>> handlerType = target.getTypeHandler();
|
|
|
- if (handlerType == null || handlerType == UnknownTypeHandler.class) {
|
|
|
- this.handler = null;
|
|
|
- } else if (handlerType == EnumerationTypeHandler.class) {
|
|
|
- Type type = TypeParameterResolver.resolveFieldType(target.getField(), table.getType());
|
|
|
- this.handler = new EnumerationTypeHandler((Class<? extends Enumeration>) ObjectUtils.type2class(type));
|
|
|
- } else {
|
|
|
- TypeHandler<?> handler = TYPE_HANDLER_MAPPING.get(handlerType);
|
|
|
- if (handler == null) {
|
|
|
- handler = TYPE_HANDLER_MAPPING.computeIfAbsent(handlerType, k -> ObjectUtils.instance(handlerType));
|
|
|
- }
|
|
|
- this.handler = handler;
|
|
|
- }
|
|
|
-
|
|
|
+ this.handler = getTypeHandler(table.getTarget(), target);
|
|
|
this.sensitive = target.getField().isAnnotationPresent(Sensitive.class);
|
|
|
}
|
|
|
|
|
@@ -243,9 +228,9 @@ final class Tables {
|
|
|
Context context = getContext();
|
|
|
String unquote = SQLUtils.unquote(name);
|
|
|
String key = context.getSchema() + StringPool.DOT + unquote;
|
|
|
- Table table = TABLES.get(key);
|
|
|
+ Table table = TABLE_MAPPING.get(key);
|
|
|
if (table == null) {
|
|
|
- table = TABLES.computeIfAbsent(key, k -> {
|
|
|
+ table = TABLE_MAPPING.computeIfAbsent(key, k -> {
|
|
|
boolean quoted = SQLUtils.isQuoted(name);
|
|
|
String datasource = context.getDatasource();
|
|
|
for (TableInfo info : TableInfoHelper.getTableInfos()) {
|
|
@@ -281,4 +266,24 @@ final class Tables {
|
|
|
public static Field getTableField(@NonNull String table, @NonNull String column) {
|
|
|
return ObjectUtils.ifNull(getTable(table), tb -> tb.getField(column));
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取字段类型处理器
|
|
|
+ *
|
|
|
+ * @param table 表信息
|
|
|
+ * @param field 字段信息
|
|
|
+ * @return 类型处理器实例
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public static TypeHandler<?> getTypeHandler(@NonNull TableInfo table, @NonNull TableFieldInfo field) {
|
|
|
+ Class<? extends TypeHandler<?>> clazz = field.getTypeHandler();
|
|
|
+ if (clazz == null || clazz == UnknownTypeHandler.class) {
|
|
|
+ return null;
|
|
|
+ } else if (clazz == EnumerationTypeHandler.class) {
|
|
|
+ Type type = TypeParameterResolver.resolveFieldType(field.getField(), table.getEntityType());
|
|
|
+ return new EnumerationTypeHandler((Class<? extends Enumeration>) ObjectUtils.type2class(type));
|
|
|
+ }
|
|
|
+ TypeHandler<?> handler = HANDLER_MAPPING.get(clazz);
|
|
|
+ return handler != null ? handler : HANDLER_MAPPING.computeIfAbsent(clazz, k -> ObjectUtils.instance(clazz));
|
|
|
+ }
|
|
|
}
|