package cn.fateverse.query.factory.impl;

import cn.fateverse.query.entity.DataSourceManage;
import cn.fateverse.query.mapper.DynamicDataSourceMapper;
import cn.fateverse.query.mapper.dynamic.MySqlDynamicDataSourceMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

/**
 * @author Clay
 * @date 2023-07-16
 */
public class MySqlDynamicDataSourceFactory extends AbstractDynamicDataSourceFactory {


    @Override
    public SqlSessionFactory getSqlSessionFactory(DataSourceManage dataSourceManage) {
        return super.getSqlSessionFactory(dataSourceManage, "classpath:mapper/dynamic/MySqlDynamicDataSourceMapper.xml");
    }


    @Override
    public DynamicDataSourceMapper getTableMapper(SqlSession sqlSession) {
        return sqlSession.getMapper(MySqlDynamicDataSourceMapper.class);
    }

    @Override
    public String getDataBaseUrl(DataSourceManage dataSource) {
        return "jdbc:mysql://" + dataSource.getHost() + ":" + dataSource.getPort() + "/" + dataSource.getDbName() + (null == dataSource.getArgs() ? "" : dataSource.getArgs());
    }

}
