package cn.reghao.tnb.content.app.config.shard;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;

/**
 * @author reghao
 * @date 2022-03-15 18:58:56
 */
public class DataSourceUtil {
    private static final String HOST = "127.0.0.1";
    private static final int PORT = 3306;
    private static final String USERNAME = "dev";
    private static final String PASSWORD = "Test_123456";

    public static DataSource createDataSource(final String database) {
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setMinimumIdle(5);
        hikariConfig.setMaximumPoolSize(10);
        hikariConfig.setAutoCommit(true);
        hikariConfig.setIdleTimeout(30000);
        hikariConfig.setPoolName("EvaluationHikariCP");
        hikariConfig.setMaxLifetime(1800000);
        hikariConfig.setConnectionTimeout(30000);
        hikariConfig.setConnectionTestQuery("SELECT 1");
        hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");

        String prefix = String.format("jdbc:mysql://%s:%s/%s?", HOST, PORT, database);
        String jdbcUrl = prefix + "useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8";
        hikariConfig.setJdbcUrl(jdbcUrl);
        hikariConfig.setUsername(USERNAME);
        hikariConfig.setPassword(PASSWORD);
        return new HikariDataSource(hikariConfig);
    }
}
