package net.cobem.grizzly.storage;

import com.jolbox.bonecp.BoneCP;
import com.jolbox.bonecp.BoneCPConfig;
import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.utils.Configuration;

import java.sql.Connection;
import java.sql.SQLException;

public class DatabasePool {
    private BoneCP Pool;

    public DatabasePool(Configuration Config) {
        BoneCPConfig CPConfig = new BoneCPConfig();
        CPConfig.setJdbcUrl("jdbc:mysql://" + Config.get("jdbc.grizzly.host") + "/" + Config.get("jdbc.grizzly.database"));
        CPConfig.setMinConnectionsPerPartition(2);
        CPConfig.setMaxConnectionsPerPartition(10);
        CPConfig.setPartitionCount(1);
        CPConfig.setUsername(Config.get("jdbc.grizzly.username"));
        CPConfig.setPassword(Config.get("jdbc.grizzly.password"));

        try {
            Pool = new BoneCP(CPConfig);
        } catch (SQLException e) {
            Grizzly.write(e.getMessage());
        }
    }

    public Connection GrabConnection() {
        try {
            return Pool.getConnection();
        } catch (SQLException e) {
            return null;
        }
    }

    public BoneCP GrabPool() {
        return Pool;
    }
}
