package com.platform.oracle;

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

import com.base.Custom4exception;
import com.base.CustomException;
import com.platform.entities.OracleConnectorParams;
import com.platform.utils.Configs;
import com.platform.utils.FileOperateHelper;

public class OracleConnector {	
	static {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Configs.CONSOLE_LOGGER.info("Oracle驱动加载成功");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			new CustomException(Custom4exception.OracleSQL_Except, e);
			e.printStackTrace();
		}
	}	
	

	public synchronized static Connection ConnectionBuilder(String url, String user,
			String password, OracleConnectorParams oc) {	
		Connection conn=null;
		try {
			conn = DriverManager.getConnection(url, user, password);
		} catch (SQLException e) {		
			new CustomException(Custom4exception.OracleSQL_Except, e);
			Configs.CONSOLE_LOGGER.info("创建oracle连接失败: [" + e.getMessage() + "]");
			if (null != oc) {
				FileOperateHelper.fileWrite(Configs.EXTRACT_LOG_LOCALTION
						+ oc.getName(), "创建oracle连接失败: [" + e.getMessage() + "]\r\n");
			}
		}
		return conn;
	}

	public synchronized static boolean canConnect(String url, String user, String password) {
		return (null != ConnectionBuilder(url, user, password, null));
	}

	public synchronized static ResultSet getSQLExecResultSet(Connection conn, String sql, String filePath) {
		ResultSet resultSet = null;
		try {
			Statement statement = conn
					.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
							ResultSet.CONCUR_UPDATABLE);
			resultSet = statement.executeQuery(sql);
			FileOperateHelper
			.fileWrite(filePath, sql+ "\r\n"+"OK \r\n");
		} catch (SQLException e) {
			FileOperateHelper
			.fileWrite(filePath, sql+ "\r\n"+e.getMessage()+"\r\n");
			new CustomException(Custom4exception.OracleSQL_Except, e);
		}

		return resultSet;
	}

	public synchronized static ResultSet getSQLExecResultSet(String url, String user,
			String password, String sql, String filePath) {
		return getSQLExecResultSet(ConnectionBuilder(url, user, password, null), sql, filePath);
	}

	/**
	 * 执行对oracle数据库的增、删
	 * @param conn
	 * @param sql
	 * @return true:执行的不返回集合数据的sql成功，  是否执行成功
	 */
	public synchronized static boolean execOracleSQL(Connection conn, String sql, String filePath) {
		boolean flag = false;
		try {
			Statement statement = conn.createStatement();
			statement.execute(sql);
			flag = true;
			FileOperateHelper
			.fileWrite(filePath, sql+ "\r\n"+"OK \r\n");
		} catch (SQLException e) {
			flag = false;
			FileOperateHelper
			.fileWrite(filePath, sql+ "\r\n"+e.getMessage()+"\r\n");
			new CustomException(Custom4exception.OracleSQL_Except, e);
		}
		return flag;
	}
	/**
	 * 执行对oracle数据库的增、删
	 * @param conn
	 * @param sql
	 * @return true:执行结果大于1，即有数据    是否执行成功
	 */
	public synchronized static boolean execUpdateOracleSQL(Connection conn, String sql, String filePath) {
		boolean flag = false;
		try {
			Statement statement = conn.createStatement();
			if(statement.executeUpdate(sql) > 0)
			{
				flag = true;
				FileOperateHelper
				.fileWrite(filePath, sql+ "\r\n"+"OK \r\n");
			}
		} catch (SQLException e) {
			flag = false;
			FileOperateHelper
			.fileWrite(filePath, sql+ "\r\n"+e.getMessage()+"\r\n");
			new CustomException(Custom4exception.OracleSQL_Except, e, sql);
		}
		return flag;
	}
}