package cn.fateverse.query.handler.reader;


import cn.fateverse.common.core.exception.CustomException;
import cn.fateverse.query.entity.DataAdapter;
import cn.fateverse.query.handler.adapter.DataAdapterHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 数据适配器的执行器
 *
 * @author Clay
 * @date 2023-10-31  20:55
 */
@Slf4j
@Component
public class DataAdapterHandlerReader {

    private final List<DataAdapterHandler> handlerList;


    public DataAdapterHandlerReader(List<DataAdapterHandler> handlerList) {
        this.handlerList = handlerList;
    }


    public Object mockExecute(DataAdapter dataAdapter, Object params) {
        for (DataAdapterHandler dataAdapterHandler : handlerList) {
            Object result = dataAdapterHandler.mockExecute(dataAdapter, params);
            if (result != null) {
                return result;
            }
        }
        throw new CustomException("当前数据源类型不支持!");
    }


    public Object execute(DataAdapter dataAdapter, Object params) {
        for (DataAdapterHandler dataAdapterHandler : handlerList) {
            Object result = dataAdapterHandler.execute(dataAdapter, params);
            if (result != null) {
                return result;
            }
        }
        throw new CustomException("当前数据源类型不支持!");
    }


}
