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.query.QueryColumn;
019import com.mybatisflex.core.util.ClassUtil;
020import com.mybatisflex.core.util.StringUtil;
021import org.apache.ibatis.io.ResolverUtil;
022
023import java.io.Serializable;
024import java.lang.reflect.Field;
025import java.lang.reflect.Modifier;
026import java.util.HashMap;
027import java.util.List;
028import java.util.Map;
029import java.util.Set;
030
031/**
032 * @author michael
033 */
034public class TableDefs implements Serializable {
035
036    private static final Map<String, TableDef> TABLE_DEF_MAP = new HashMap<>();
037    private static final Map<String, Map<String, QueryColumn>> QUERY_COLUMN_MAP = new HashMap<>();
038
039    public static void init(String packageName) {
040        ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<>();
041        resolverUtil.find(new ResolverUtil.IsA(TableDef.class), packageName);
042        Set<Class<? extends Class<?>>> typeSet = resolverUtil.getClasses();
043        for (Class<?> type : typeSet) {
044            if (!type.isAnonymousClass() && !type.isInterface() && !type.isMemberClass()) {
045                try {
046                    registerTableDef(type);
047                } catch (IllegalAccessException e) {
048                    e.printStackTrace();
049                }
050            }
051        }
052    }
053
054
055    public static TableDef getTableDef(Class<?> entityClass, String tableNameWithSchema) {
056        TableDef tableDef = TABLE_DEF_MAP.get(tableNameWithSchema);
057        if (tableDef == null) {
058            init(entityClass.getPackage().getName());
059            tableDef = TABLE_DEF_MAP.get(tableNameWithSchema);
060        }
061        return tableDef;
062    }
063
064
065    public static QueryColumn getQueryColumn(Class<?> entityClass, String tableNameWithSchema, String column) {
066        Map<String, QueryColumn> queryColumnMap = QUERY_COLUMN_MAP.get(tableNameWithSchema);
067        if (queryColumnMap == null) {
068            init(entityClass.getPackage().getName());
069            queryColumnMap = QUERY_COLUMN_MAP.get(tableNameWithSchema);
070        }
071        return queryColumnMap != null ? queryColumnMap.get(column) : null;
072    }
073
074
075    public static void registerTableDef(Class<?> tableDefClass) throws IllegalAccessException {
076        TableDef tableDef = (TableDef) ClassUtil.getFirstField(tableDefClass, field -> {
077            int mod = Modifier.fieldModifiers();
078            return Modifier.isPublic(mod) && Modifier.isStatic(mod);
079        }).get(null);
080
081        String key = StringUtil.buildSchemaWithTable(tableDef.getSchema(), tableDef.getTableName());
082
083        TABLE_DEF_MAP.put(key, tableDef);
084
085        List<Field> allFields = ClassUtil.getAllFields(tableDef.getClass(), field -> field.getType() == QueryColumn.class);
086
087        Map<String, QueryColumn> columnMap = new HashMap<>(allFields.size());
088        for (Field field : allFields) {
089            QueryColumn queryColumn = (QueryColumn) field.get(tableDef);
090            columnMap.put(queryColumn.getName(), queryColumn);
091        }
092
093        QUERY_COLUMN_MAP.put(key, columnMap);
094    }
095
096
097}