package kr.co.kihyun.db;

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

public class CommonDBManager extends DBPool {

	public Connection con = null;
	public PreparedStatement pstmt = null;
	public Statement stmt = null;
	private boolean isStackTraced = true;
    String url = null;
    String user = null;
    String password = null;

	public void setStatment(StringBuilder sql, Object... params) throws SQLException, FixMeJDOException{
		this.setStatment(sql.toString(), params);
	}

	public void setStatment(String sql, Object... params) throws SQLException, FixMeJDOException {

		if (con == null || con.isClosed()) {
	    	con = getConnection();
		}
		pstmt = con.prepareStatement(sql);

		for (int i = 0; params != null && i < params.length; i++) {
			if (params[i] instanceof java.util.Date) {
				java.util.Date param = (java.util.Date) params[i];
				pstmt.setDate(i + 1, new java.sql.Date(param.getTime()));
			} else if (params[i] instanceof Boolean) {
				pstmt.setBoolean(i + 1, (Boolean) params[i]);
				if (!isStackTraced)
					throw new FixMeJDOException("BOOLEAN PARAM " + i + "\n" + sql);
			} else {
				pstmt.setObject(i + 1, params[i]);
			}
		}
	}

	public ResultSet execQuery(StringBuilder sql, Object... params) throws SQLException {
		return this.execQuery(sql.toString(), params);
	}


	public ResultSet execQuery(String sql, Object... params) throws SQLException {
		try {
			setStatment(sql, params);
		} catch (FixMeJDOException sqlEx) {
			sqlEx.printStackTrace();
			isStackTraced = true;
			execQuery(sql, params);
		}
		return pstmt.executeQuery();
	}

	public int execUpdate(StringBuilder sql, Object... params) throws SQLException {
		return this.execUpdate(sql.toString(), params);
	}


	public int execUpdate(String sql, Object... params) throws SQLException {
		try {
			setStatment(sql, params);
		} catch (FixMeJDOException sqlEx) {
			sqlEx.printStackTrace();
			isStackTraced = true;
			execUpdate(sql, params);
		}
		return pstmt.executeUpdate();
	}
    //디비 코밋 작업후 컨넥션 끊는 구문. 중요
    public int executeUpdate(String sql, Object... params) throws Exception {
        int result = 0;

		try {
			setStatment(sql, params);
            result = pstmt.executeUpdate();
            con.commit();
        }catch(SQLException e){
            con.rollback();
        }catch(Exception e){
            con.rollback();
		}finally{
            if(pstmt != null){
                try{
                    pstmt.close();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
            if(con != null){
                try{
                    con.close();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
    public boolean execute(String sql, Object... params){
    	boolean rslt=false;
    	Statement stmt = null;
    	try {
			if (con == null || con.isClosed()) {
				con = getConnection();
			}
			con.setAutoCommit(true);
			stmt = con.createStatement();
			rslt=stmt.execute(sql);

		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try{
				if(stmt!=null)stmt.close();
				execClose();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
    	return rslt;
    }

    public void commit(){
    	try{
    		con.commit();
    	} catch (SQLException e) {
    		e.printStackTrace();
    	}
    }

    public void rollback(){
    	try{
    		con.rollback();
    	} catch (SQLException e) {
    		e.printStackTrace();
    	}
    }

    public void pstmtClose(){
    	try {
			if (null != pstmt)
				pstmt.close();
		} catch (SQLException ex) {
                //31.오류 상황 대응 부재_CWE-390 Add by YOUNGJUN,CHO
                    ex.printStackTrace();
		}
    }

	public void execClose() {
		if (null != stmt) close(stmt);
		if (null != pstmt) close(pstmt);
		if (null != con) close(con);
	}
}
