package com.eactive.eai.common.jdbc;

import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;

import javax.sql.DataSource;

@SuppressWarnings({"rawtypes","unchecked"})
public class ConnectionProxy implements Connection {
    //static Logger logger = Logger.getLogger("trace.db");
    //StopWatch stopWatch = new StopWatch();
    private Connection con;

    public ConnectionProxy(Connection con) {
    //    stopWatch.start();

        this.con = con;
    }

    public ConnectionProxy(DataSource ds) throws SQLException {
        this.con = ds.getConnection();
    }

    public ConnectionProxy(DataSource ds, String username, String password)
        throws SQLException {
        this.con = ds.getConnection(username, password);
    }

    public Statement createStatement() throws SQLException {
        return new StatementProxy(this, con.createStatement());
    }

    public PreparedStatement prepareStatement(String sql)
        throws SQLException {
        return new PreparedStatementProxy(this, sql, con.prepareStatement(sql));
    }

    public CallableStatement prepareCall(String sql) throws SQLException {
        return new CallableStatementProxy(this, sql, con.prepareCall(sql));
    }

    public String nativeSQL(String sql) throws SQLException {
        return con.nativeSQL(sql);
    }

    public void setAutoCommit(boolean autoCommit) throws SQLException {
        con.setAutoCommit(autoCommit);
    }

    public boolean getAutoCommit() throws SQLException {
        return con.getAutoCommit();
    }

    public void commit() throws SQLException {
        con.commit();
    }

    public void rollback() throws SQLException {
        con.rollback();
    }

    public void close() throws SQLException {
        //con.setAutoCommit(true);
        //con.commit();
        con.close();
        System.out.println("[ConnectionProxy] "+ toString());
        
/*
        stopWatch.stop();
        if (stopWatch.getTime() > DBPool.TRACE_CONN_TIMEOUT) {
            logger.warn("[ConnectionProxy] CONN-TOO-LONG : " + stopWatch);
        }
        if (DBPool.TRACE_CONN_DUMP) {
            logger.warn("[ConnectionProxy] Thread Dump (NOT ERROR!) -----");
            Thread.dumpStack();
        }
*/
    }

    public boolean isClosed() throws SQLException {
        return con.isClosed();
    }

    public DatabaseMetaData getMetaData() throws SQLException {
        return con.getMetaData();
    }

    public void setReadOnly(boolean readOnly) throws SQLException {
        con.setReadOnly(readOnly);
    }

    public boolean isReadOnly() throws SQLException {
        return con.isReadOnly();
    }

    public void setCatalog(String catalog) throws SQLException {
        con.setCatalog(catalog);
    }

    public String getCatalog() throws SQLException {
        return con.getCatalog();
    }

    public void setTransactionIsolation(int level) throws SQLException {
        con.setTransactionIsolation(level);
    }

    public int getTransactionIsolation() throws SQLException {
        return con.getTransactionIsolation();
    }

    public SQLWarning getWarnings() throws SQLException {
        return con.getWarnings();
    }

    public void clearWarnings() throws SQLException {
        con.clearWarnings();
    }

    public Statement createStatement(int resultSetType, int resultSetConcurrency)
        throws SQLException {
        return new StatementProxy(this,
            con.createStatement(resultSetType, resultSetConcurrency));
    }

    public PreparedStatement prepareStatement(String sql, int resultSetType,
        int resultSetConcurrency) throws SQLException {
        return new PreparedStatementProxy(this, sql,
            con.prepareStatement(sql, resultSetType, resultSetConcurrency));
    }

    public CallableStatement prepareCall(String sql, int resultSetType,
        int resultSetConcurrency) throws SQLException {
        return new CallableStatementProxy(this, sql,
            con.prepareCall(sql, resultSetType, resultSetConcurrency));
    }

	public Map getTypeMap() throws SQLException {
        return con.getTypeMap();
    }

//    public void setTypeMap(Map map) throws SQLException {
//        con.setTypeMap(map);
//    }



    // BEGIN_COMMENT_FOR_JAVA_1.3
    // END_COMMENT_FOR_JAVA_1.3

    public Statement createStatement(int arg0, int arg1, int arg2) throws SQLException
    {
        return con.createStatement(arg0,arg1,arg2);
    }

    public int getHoldability() throws SQLException
    {
        return con.getHoldability();
    }

    public CallableStatement prepareCall(String arg0, int arg1, int arg2, int arg3) throws SQLException
    {
        return con.prepareCall(arg0,arg1,arg2,arg3);
    }

    public PreparedStatement prepareStatement(String arg0, String[] arg1) throws SQLException
    {
        return con.prepareStatement(arg0,arg1);
    }

    public PreparedStatement prepareStatement(String arg0, int arg1) throws SQLException
    {
        return con.prepareStatement(arg0,arg1);
    }

    public PreparedStatement prepareStatement(String arg0, int arg1, int arg2, int arg3) throws SQLException
    {
        return con.prepareStatement(arg0,arg1,arg2,arg3);
    }

    public PreparedStatement prepareStatement(String arg0, int[] arg1) throws SQLException
    {
        return con.prepareStatement(arg0,arg1);
    }

    public void releaseSavepoint(Savepoint arg0) throws SQLException
    {
        con.releaseSavepoint(arg0);
    }

    public void rollback(Savepoint arg0) throws SQLException
    {
        con.rollback(arg0);
    }

    public void setHoldability(int arg0) throws SQLException
    {
        con.setHoldability(arg0);
    }

    public Savepoint setSavepoint() throws SQLException
    {
        return con.setSavepoint();
    }

    public Savepoint setSavepoint(String arg0) throws SQLException
    {
        return con.setSavepoint(arg0);
    }

	
	public <T> T unwrap(Class<T> iface) throws SQLException {

		return null;
	}

	
	public boolean isWrapperFor(Class<?> iface) throws SQLException {

		return false;
	}

//	
//	public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
//
//		
//	}

	
	public Clob createClob() throws SQLException {

		return null;
	}

	
	public Blob createBlob() throws SQLException {

		return null;
	}

	
	public NClob createNClob() throws SQLException {

		return null;
	}

	
	public SQLXML createSQLXML() throws SQLException {

		return null;
	}

	
	public boolean isValid(int timeout) throws SQLException {

		return false;
	}

	
	public void setClientInfo(String name, String value)
			throws SQLClientInfoException {

		
	}

	
	public void setClientInfo(Properties properties)
			throws SQLClientInfoException {

		
	}

	
	public String getClientInfo(String name) throws SQLException {

		return null;
	}

	
	public Properties getClientInfo() throws SQLException {

		return null;
	}

	
	public Array createArrayOf(String typeName, Object[] elements)
			throws SQLException {

		return null;
	}

	
	public Struct createStruct(String typeName, Object[] attributes)
			throws SQLException {

		return null;
	}

	@Override
	public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
		
	}

	public void setSchema(String schema) throws SQLException {
		
	}

	public String getSchema() throws SQLException {
		return null;
	}

	public void abort(Executor executor) throws SQLException {
		
	}

	public void setNetworkTimeout(Executor executor, int milliseconds)
			throws SQLException {
		
	}

	public int getNetworkTimeout() throws SQLException {
		return 0;
	}
}
