
package com.lideeyunji.tool.framework.sql.flyweight;

import com.lideeyunji.tool.framework.exception.lideeYunJiException;
import com.lideeyunji.tool.framework.sql.enums.DbTypeEnum;
import com.lideeyunji.tool.framework.sql.template.AbstractDbTemplate;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 数据库享元工厂
 */
public class FlyweightDBFactory {

    /**
     * Db缓存池
     */
    private static final Map<DbTypeEnum, AbstractDbTemplate> DB_POOL = new HashMap<>();

    static {
        Arrays.stream(DbTypeEnum.values())
                .forEach(dbTypeEnum -> {
                    try {
                        AbstractDbTemplate dbTemplate = dbTypeEnum.getDbTemplate();
                        DB_POOL.put(dbTypeEnum, dbTemplate);
                    } catch (Exception e) {

                    }
                });
    }

    /**
     * 获取数据库产品类型
     */
    public static AbstractDbTemplate getDbType(DbTypeEnum dbTypeEnum) {
        AbstractDbTemplate dbTemplate = DB_POOL.get(dbTypeEnum);
        if (dbTemplate == null) {
            throw new lideeYunJiException("获取不到该数据类型，需要先添加到DB_POOL");
        } else {
            return dbTemplate;
        }
    }

}
