package com.qiaoba.common.database.handlers.schema;

import com.qiaoba.common.base.enums.DataBaseEnum;
import com.qiaoba.common.base.exceptions.ServiceException;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * SchemaHandlerFactory-用于选择哪种类型处理器
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023/6/20 9:22
 */
@Configuration
@RequiredArgsConstructor
public class SchemaHandlerFactory {

    private static Map<String, SchemaHandler> HANDLERS = new ConcurrentHashMap<>();

    private final MysqlSchemaHandler mysqlSchemaHandler;

    @PostConstruct
    public void register() {
        HANDLERS.put(DataBaseEnum.MY_SQL.getType(), mysqlSchemaHandler);
    }

    public static SchemaHandler getHandler(String name) {
        SchemaHandler schemaHandler = HANDLERS.get(name);
        if (Objects.isNull(schemaHandler)) {
            throw new ServiceException("Schema处理器工厂异常, 类型:[" + name + "]找不到相对应的解析器");
        }
        return schemaHandler;
    }
}
