package kr.co.kihyun.db;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBManager extends DBPool {

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

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

	protected void setStatment(String sql, Object... params) throws SQLException, FixMeJDOException {
		if (con == null || con.isClosed()) {
	    	con = getConnection();
	    	pstmt = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
		}

		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]);
			}
		}
	}
	
	protected void setSql(StringBuilder sql) throws SQLException, FixMeJDOException, FileNotFoundException, IOException {
		if (con == null || con.isClosed()) {
	    	 con = getConnection();
	    	 pstmt = con.prepareStatement(sql.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
		}
	}

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


	protected 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();
	}

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


	protected 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();
	}

    //디비 코밋 작업후 컨넥션 끊는 구문. 중요
    protected int executeUpdate(String sql, Object... params) throws Exception {
        int result = 0;

		try {
			setStatment(sql, params);
            result = pstmt.executeUpdate();
            con.commit();
        }catch(Exception e){
            if(con != null) con.rollback();
            e.printStackTrace();
        }finally{
            if(pstmt != null){
                try{
                	pstmt.close();
                }catch(Exception e){
                    //31.오류 상황 대응 부재_CWE-390 Add by YOUNGJUN,CHO
                    e.printStackTrace();
                    //++++++++++++++++++++++++++++++++++++++++++++++++
                }
            }
            if(con != null){
                try{
                    con.close();
                }catch(Exception e){
                    //31.오류 상황 대응 부재_CWE-390 Add by YOUNGJUN,CHO
                    e.printStackTrace();
                    //++++++++++++++++++++++++++++++++++++++++++++++++
                }
            }
        }
        return result;
    }

    protected void commit(){
    	try{
    		if(null != con) con.commit();
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    }

    protected void rollback(){
    	try{
    		if(null != con) con.rollback();
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    }

	protected void execClose() {
		if (null != stmt) close(stmt);
		if (null != pstmt) close(pstmt);
		if (null != con) close(con);
	}
	
	/*******
	 * addbatch
	 *
	 * @throws SQLException
	 ********/
	protected void addBatch(String sql, Object... params) throws SQLException, FileNotFoundException, IOException {
		try {
			setStatment(sql, params);
		} catch (FixMeJDOException sqlEx) {
			sqlEx.printStackTrace();
			isStackTraced = true;
			addBatch(sql, params);
		}
		pstmt.addBatch();
	}
	
	/*******
	 * execbatch
	 *
	 * @throws SQLException
	 ********/
	protected int execBatch() throws SQLException, FileNotFoundException, IOException {
		int successCount = 0;
		int[] batchCount = pstmt.executeBatch();
		for (int tmp : batchCount) {
			if (tmp > -3) {
				successCount++;
			}
		}
		pstmt.clearBatch();
		return successCount;
	}
}
