package com.ruoyi.common.utils.datamodeling;

import com.ruoyi.common.config.datamodeling.GenConfig;
import com.ruoyi.common.constant.DataSourceType;
import com.ruoyi.common.constant.LoadDriverConstants;
import com.ruoyi.common.core.domain.entity.DataSource;

public class DataSourceUtils {

    public static GenConfig changeDataSource(DataSource dataSource) {


        GenConfig genConfig = new GenConfig();
        genConfig.setDbtype(dataSource.getDatabaseType());
        genConfig.setUsername(dataSource.getUsername());
        genConfig.setPassword(dataSource.getPassword());
        if(dataSource.getDatabaseType().equals(DataSourceType.MYSQL.getDataSourceName())){
            genConfig.setDriverClassName(LoadDriverConstants.MYSQL);
            genConfig.setUrl("jdbc:mysql://"+dataSource.getDatabaseIp()+":"+dataSource.getPortNumber()+"/"+dataSource.getDatabaseName());
        }else if(dataSource.getDatabaseType().equals(DataSourceType.SQLSERVER.getDataSourceName())){
            genConfig.setDriverClassName(LoadDriverConstants.SQLSERVER);
            genConfig.setUrl("jdbc:sqlserver://"+dataSource.getDatabaseIp()+":"+dataSource.getPortNumber()+";DatabaseName="+dataSource.getDatabaseName());
        }else if(dataSource.getDatabaseType().equals(DataSourceType.ORACLE.getDataSourceName())){
            genConfig.setDriverClassName(LoadDriverConstants.ORACLE);
            genConfig.setUrl("jdbc:oracle:thin:@"+dataSource.getDatabaseIp()+":"+dataSource.getPortNumber()+":"+dataSource.getDatabaseName());
        }else if(dataSource.getDatabaseType().equals(DataSourceType.DM.getDataSourceName())){
            genConfig.setDriverClassName(LoadDriverConstants.DM);
            genConfig.setUrl("jdbc:dm://"+dataSource.getDatabaseIp()+":"+dataSource.getPortNumber()+"?schema="+dataSource.getDatabaseName());
        }
        return genConfig;
    }
}
