// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package dbcache;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbcp2.DriverManagerConnectionFactory;
import org.apache.commons.dbcp2.PoolableConnection;
import org.apache.commons.dbcp2.PoolableConnectionFactory;
import org.apache.commons.dbcp2.PoolingDriver;
import org.apache.commons.pool2.impl.GenericObjectPool;

public class DAL_JDBC implements DAL {
    private static final String POOL_PREFIX = "jdbc:apache:commons:dbcp:";
    private static final String POOL_NAME = "test";
    private static final String POOL = POOL_PREFIX + POOL_NAME;
    public DAL_JDBC(String constr, String usr, String pwd) throws SQLException, ClassNotFoundException {
        Class.forName("org.apache.commons.dbcp2.PoolingDriver");
        PoolingDriver driver = (PoolingDriver)DriverManager.getDriver(POOL_PREFIX);
        PoolableConnectionFactory pcf = new PoolableConnectionFactory(new DriverManagerConnectionFactory(constr, usr, pwd), null);
        GenericObjectPool<PoolableConnection> cp = new GenericObjectPool<PoolableConnection>(pcf);
        pcf.setPool(cp);
        driver.registerPool(POOL_NAME, cp);
    }
    @Override
    public Big getBig(int id) {
        Big res = null;
        try {
            try(Connection con = DriverManager.getConnection(POOL)) {
                try(PreparedStatement pstmt =  con.prepareStatement("SELECT id,data1,data2 FROM big WHERE id = ?")) {
                    pstmt.setInt(1, id);
                    try(ResultSet rs = pstmt.executeQuery()) {
                        if(rs.next()) {
                            res = new Big(rs.getInt(1), rs.getString(2), rs.getString(3));
                        }
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return res;
    }
    @Override
    public Integer getCounter(String id) {
        Integer res = null;
        try {
            try(Connection con = DriverManager.getConnection(POOL)) {
                try(PreparedStatement pstmt =  con.prepareStatement("SELECT val FROM counter WHERE id = ?")) {
                    pstmt.setString(1, id);
                    try(ResultSet rs = pstmt.executeQuery()) {
                        if(rs.next()) {
                            res = rs.getInt(1);
                        }
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return res;
    }
    @Override
    public void incrementCounter(String id) {
        try {
            try(Connection con = DriverManager.getConnection(POOL)) {
                try(PreparedStatement pstmt =  con.prepareStatement("UPDATE counter SET val = val + 1 WHERE id = ?")) {
                    pstmt.setString(1, id);
                    pstmt.executeUpdate();
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void close() {
        try {
            PoolingDriver driver = (PoolingDriver)DriverManager.getDriver(POOL_PREFIX);
            driver.closePool(POOL_NAME);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
