/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hu.nir.nmail.migration;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author attila
 */
public class DatabaseManager {
    private Connection connection;
    private String host; 
    private int port;
    private String database; 
    private String account; 
    private String password;
    
    public DatabaseManager(String host, int port, String database, String account, String password) {
        this.host = host;
        this.port = port;
        this.database = database;
        this.account = account;
        this.password = password;
    }
    
    private void connect() 
            throws ClassNotFoundException, SQLException {
        Class.forName("org.postgresql.Driver");
        String url = "jdbc:postgresql://"+host+":"+port+"/"+database;
        connection = DriverManager.getConnection(url, account, password);
    }
    
    public boolean testConnection() {
        boolean returnValue = true;
        try {
            connect();
        } catch (Exception ex) {
            returnValue = false;
        } finally {
            connectionClose();
        }
        
        return returnValue;
    }
    
    public ResultSet executeSelect(String select) throws SQLException, ClassNotFoundException {
        if (connection == null || connection.isClosed()) {
            connect();
        }
        Logger.getLogger(DatabaseManager.class.getName()).log(Level.INFO, select);
        Statement statement = connection.createStatement();
        return statement.executeQuery(select);
    }
    
    public boolean executeInsert(String insert) throws SQLException, ClassNotFoundException {
        if (connection == null || connection.isClosed()) {
            connect();
        }
        Logger.getLogger(DatabaseManager.class.getName()).log(Level.INFO, insert);
        Statement statement = connection.createStatement();
        return statement.execute(insert);
    }
    
    public void connectionClose() {
        try {
            if (connection != null && !connection.isClosed())
                connection.close();
        } catch (SQLException ex) {}
    }
    
    public Savepoint creatTransaction(String name) throws SQLException, ClassNotFoundException {
        if (connection == null || connection.isClosed()) {
            connect();
        }
        
        connection.setAutoCommit(false);
        return connection.setSavepoint(name);
    }
    
    public void rollback(Savepoint savepoint) throws SQLException {
        if (connection == null || connection.isClosed()) {
            return;
        }
        
        connection.rollback(savepoint);
    }
    
    public void commit() throws SQLException {
        if (connection == null || connection.isClosed()) {
            return;
        }
        connection.commit();
    }
}
