package com.tofly.analyse.util.analysis;

import com.supermap.data.Datasource;
import com.supermap.data.DatasourceConnectionInfo;
import com.supermap.data.EngineType;
import com.supermap.data.Workspace;
import com.tofly.analyse.config.ConfigProperty;
import com.tofly.analyse.entity.em.DataTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DatasourceConfig {
    /**
     * 配置文件
     */
    @Autowired
    private ConfigProperty configProperty;

    public Datasource getDatasource(Workspace workspace) {
        DatasourceConnectionInfo info = new DatasourceConnectionInfo();
        if (configProperty.getDataType().equals(DataTypeEnum.MYSQL.getCode())) {
            info.setEngineType(EngineType.MYSQL);
        }
        if (configProperty.getDataType().equals(DataTypeEnum.ORACLE.getCode())) {
            info.setEngineType(EngineType.ORACLEPLUS);
        }
        if (configProperty.getDataType().equals(DataTypeEnum.POSTGRESQL.getCode())) {
            info.setEngineType(EngineType.POSTGRESQL);
        }
        info.setDatabase(configProperty.getDatabase());
        info.setServer(configProperty.getUrl());
        info.setUser(configProperty.getUsername());
        info.setPassword(configProperty.getPwd());
        Datasource datasource = workspace.getDatasources().open(info);
        return datasource;
    }

}