/*********************************************************************************************************
 * 프로그램명 : DBManager.java 프로그램설명 : 각 서블릿 엔진에서 제공하는 전용 pool을 사용하기 위한 클래스 getConnection() 호출마다 서버 체크를 하지 않기 위해 현재 환경만 비주석
 * 처리해서 재컴파일하도록 한다.
 *
 * 작성자 : 강원중 작성일 : 2003.12.16 변경일 : 2002.07.22
 **********************************************************************************************************/
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 kr.co.kihyun.db.FixMeJDOException;
import kr.co.kihyun.db.DBPoolCmcp;


public class DBManagerCmcp extends DBPoolCmcp {

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

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

	protected void setStatment(String sql, Object... params) throws SQLException, FixMeJDOException, FileNotFoundException, IOException {
		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 ResultSet execQuery(StringBuilder sql, Object... params) throws SQLException, FileNotFoundException, IOException {
		return this.execQuery(sql.toString(), params);
	}

	/*******
	 * Query(select)를 실행 주는 method
	 *
	 * @throws SQLException
	 ********/
	protected ResultSet execQuery(String sql, Object... params) throws SQLException, FileNotFoundException, IOException {
		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, FileNotFoundException, IOException {
		return this.execUpdate(sql.toString(), params);
	}

	/*******
	 * Query(insert, update, update)를 실행 주는 method
	 *
	 * @throws SQLException
	 ********/
	protected int execUpdate(String sql, Object... params) throws SQLException, FileNotFoundException, IOException {
		try {
			setStatment(sql, params);
		} catch (FixMeJDOException sqlEx) {
			sqlEx.printStackTrace();
			isStackTraced = true;
			execUpdate(sql, params);
		}
		return pstmt.executeUpdate(); // oracle does not allow empty string
	}

	/******* Connection, Statement, ResultSet instance close ********/
	protected void execClose() {
		if (null != stmt) close(stmt);
		if (null != pstmt) close(pstmt);
		if (null != con) close(con);
	}
}
