package com.eactive.eai.common.jdbc;


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

public class StatementProxy implements Statement {
    //static Logger logger = Logger.getLogger("");
    //static Logger loggerDB = Logger.getLogger("trace.db");
    //StopWatch stopWatch = new StopWatch();

    private String sql;
    private Connection con;
    private Statement stmt;

    public StatementProxy(Connection con, Statement stmt) {
        this.con = con;
        this.stmt = stmt;
    }

    public ResultSet executeQuery(String sql) throws SQLException {
        try {
            this.sql = sql;
    
            //stopWatch.start();
    
            ResultSet rs = stmt.executeQuery(sql);
    
            return new ResultSetProxy(this, rs);
        } catch(SQLException e) {
            //logger.error("[StmtProxy] SQL-FAIL : " + e.toString() + "\n" + sql);
            throw e;
        } finally {
            /*
            stopWatch.stop();
            if (stopWatch.getTime() >= DBPool.TRACE_SQL_TIMEOUT) {
                loggerDB.warn("[StmtProxy] SQL-TOO-LONG : " + stopWatch + "\n" + sql);
            }
            if (DBPool.TRACE_SQL_DUMP) {
                loggerDB.warn("[StmtProxy] Thread Dump (NOT ERROR!) -----");
                Thread.dumpStack();
            }
            */
        }
    }

    public int executeUpdate(String sql) throws SQLException {
        try {
            this.sql = sql;
            //stopWatch.start();
    
            int result = stmt.executeUpdate(sql);
            return result;
        } catch(SQLException e) {
            //logger.error("[StmtProxy] SQL-FAIL : " + e.toString() + "\n" + sql);
            throw e;
        } finally {
            /*
            stopWatch.stop();
            if (stopWatch.getTime() > DBPool.TRACE_SQL_TIMEOUT) {
                loggerDB.warn("[StmtProxy] SQL-TOO-LONG : " + stopWatch + "\n" + sql);
            }
            if (DBPool.TRACE_SQL_DUMP) {
                loggerDB.warn("[StmtProxy] Thread Dump (NOT ERROR!) -----");
                Thread.dumpStack();
            }
            */
        }
    }

    public void close() throws SQLException {
        stmt.close();
    }

    public int getMaxFieldSize() throws SQLException {
        return stmt.getMaxFieldSize();
    }

    public void setMaxFieldSize(int max) throws SQLException {
        stmt.setMaxFieldSize(max);
    }

    public int getMaxRows() throws SQLException {
        return stmt.getMaxRows();
    }

    public void setMaxRows(int max) throws SQLException {
        stmt.setMaxRows(max);
    }

    public void setEscapeProcessing(boolean enable) throws SQLException {
        stmt.setEscapeProcessing(enable);
    }

    public int getQueryTimeout() throws SQLException {
        return stmt.getQueryTimeout();
    }

    public void setQueryTimeout(int seconds) throws SQLException {
        stmt.setQueryTimeout(seconds);
    }

    public void cancel() throws SQLException {
        stmt.cancel();
    }

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

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

    public void setCursorName(String name) throws SQLException {
        stmt.setCursorName(name);
    }

    public boolean execute(String sql) throws SQLException {
        try {
            this.sql = sql;
            //stopWatch.start();
    
            boolean result = stmt.execute(sql);
    
            return result;
        } catch(SQLException e) {
            //logger.error("[StmtProxy] SQL-FAIL : " + e.toString() + "\n" + sql);
            throw e;
        } finally {
            /*
            stopWatch.stop();
            if (stopWatch.getTime() > DBPool.TRACE_SQL_TIMEOUT) {
                loggerDB.warn("[StmtProxy] SQL-TOO-LONG : " + stopWatch + "\n" + sql);
            }
            if (DBPool.TRACE_SQL_DUMP) {
                loggerDB.warn("[StmtProxy] Thread Dump (NOT ERROR!) -----");
                Thread.dumpStack();
            }
            */
        }
    }

    public ResultSet getResultSet() throws SQLException {
        return new ResultSetProxy(this, stmt.getResultSet());
    }

    public int getUpdateCount() throws SQLException {
        return stmt.getUpdateCount();
    }

    public boolean getMoreResults() throws SQLException {
        return stmt.getMoreResults();
    }

    public void setFetchDirection(int direction) throws SQLException {
        stmt.setFetchDirection(direction);
    }

    public int getFetchDirection() throws SQLException {
        return stmt.getFetchDirection();
    }

    public void setFetchSize(int rows) throws SQLException {
        stmt.setFetchSize(rows);
    }

    public int getFetchSize() throws SQLException {
        return stmt.getFetchSize();
    }

    public int getResultSetConcurrency() throws SQLException {
        return stmt.getResultSetConcurrency();
    }

    public int getResultSetType() throws SQLException {
        return stmt.getResultSetType();
    }

    public void addBatch(String sql) throws SQLException {
        stmt.addBatch(sql);
    }

    public void clearBatch() throws SQLException {
        stmt.clearBatch();
    }

    public int[] executeBatch() throws SQLException {
        return stmt.executeBatch();
    }

    public Connection getConnection() throws SQLException {
        return this.con;
    }

    public String toString() {
        return sql;
    }


    // BEGIN_COMMENT_FOR_JAVA_1.3
    // END_COMMENT_FOR_JAVA_1.3

    public boolean execute(String arg0, String[] arg1) throws SQLException
    {
        try {
            this.sql = arg0;
            //stopWatch.start();
    
            boolean result = stmt.execute(arg0,arg1);
            return result;
        } catch(SQLException e) {
            //logger.error("[StmtProxy] SQL-FAIL : " + e.toString() + "\n" + sql);
            throw e;
        } finally {
            /*
            stopWatch.stop();
            if (stopWatch.getTime() > DBPool.TRACE_SQL_TIMEOUT) {
                loggerDB.warn("[StmtProxy] SQL-TOO-LONG : " + stopWatch + "\n" + sql);
            }
            if (DBPool.TRACE_SQL_DUMP) {
                loggerDB.warn("[StmtProxy] Thread Dump (NOT ERROR!) -----");
                Thread.dumpStack();
            }
            */
        }
    }

    public boolean execute(String arg0, int arg1) throws SQLException
    {
        try {
            this.sql = arg0;
            //stopWatch.start();
    
            boolean result = stmt.execute(arg0,arg1);
            return result;
        } catch(SQLException e) {
            //logger.error("[StmtProxy] SQL-FAIL : " + e.toString() + "\n" + sql);
            throw e;
        } finally {
            /*
            stopWatch.stop();
            if (stopWatch.getTime() > DBPool.TRACE_SQL_TIMEOUT) {
                loggerDB.warn("[StmtProxy] SQL-TOO-LONG : " + stopWatch + "\n" + sql);
            }
            if (DBPool.TRACE_SQL_DUMP) {
                loggerDB.warn("[StmtProxy] Thread Dump (NOT ERROR!) -----");
                Thread.dumpStack();
            }
            */
        }    
    }

    public boolean execute(String arg0, int[] arg1) throws SQLException
    {
        try {
            this.sql = arg0;
            //stopWatch.start();
    
            boolean result = stmt.execute(arg0,arg1);
            return result;
        } catch(SQLException e) {
            //logger.error("[StmtProxy] SQL-FAIL : " + e.toString() + "\n" + sql);
            throw e;
        } finally {
            /*
            stopWatch.stop();
            if (stopWatch.getTime() > DBPool.TRACE_SQL_TIMEOUT) {
                loggerDB.warn("[StmtProxy] SQL-TOO-LONG : " + stopWatch + "\n" + sql);
            }
            if (DBPool.TRACE_SQL_DUMP) {
                loggerDB.warn("[StmtProxy] Thread Dump (NOT ERROR!) -----");
                Thread.dumpStack();
            }
            */
        }    
    }

    public int executeUpdate(String arg0, String[] arg1) throws SQLException
    {
        try {
            this.sql = arg0;
            //stopWatch.start();
    
            int result = stmt.executeUpdate(arg0,arg1);
            return result;
        } catch(SQLException e) {
            //logger.error("[StmtProxy] SQL-FAIL : " + e.toString() + "\n" + sql);
            throw e;
        } finally {
            /*
            stopWatch.stop();
            if (stopWatch.getTime() > DBPool.TRACE_SQL_TIMEOUT) {
                loggerDB.warn("[StmtProxy] SQL-TOO-LONG : " + stopWatch + "\n" + sql);
            }
            if (DBPool.TRACE_SQL_DUMP) {
                loggerDB.warn("[StmtProxy] Thread Dump (NOT ERROR!) -----");
                Thread.dumpStack();
            }
            */
        }    
    }

    public int executeUpdate(String arg0, int arg1) throws SQLException
    {
        try {
            this.sql = arg0;
            //stopWatch.start();
    
            int result = stmt.executeUpdate(arg0,arg1);
            return result;
        } catch(SQLException e) {
            //logger.error("[StmtProxy] SQL-FAIL : " + e.toString() + "\n" + sql);
            throw e;
        } finally {
            /*
            stopWatch.stop();
            if (stopWatch.getTime() > DBPool.TRACE_SQL_TIMEOUT) {
                loggerDB.warn("[StmtProxy] SQL-TOO-LONG : " + stopWatch + "\n" + sql);
            }
            if (DBPool.TRACE_SQL_DUMP) {
                loggerDB.warn("[StmtProxy] Thread Dump (NOT ERROR!) -----");
                Thread.dumpStack();
            }
            */
        }    
    }

    public int executeUpdate(String arg0, int[] arg1) throws SQLException
    {
        try {
            this.sql = arg0;
            //stopWatch.start();
    
            int result = stmt.executeUpdate(arg0,arg1);
            return result;
        } catch(SQLException e) {
            //logger.error("[StmtProxy] SQL-FAIL : " + e.toString() + "\n" + sql);
            throw e;
        } finally {
            /*
            stopWatch.stop();
            if (stopWatch.getTime() > DBPool.TRACE_SQL_TIMEOUT) {
                loggerDB.warn("[StmtProxy] SQL-TOO-LONG : " + stopWatch + "\n" + sql);
            }
            if (DBPool.TRACE_SQL_DUMP) {
                loggerDB.warn("[StmtProxy] Thread Dump (NOT ERROR!) -----");
                Thread.dumpStack();
            }
            */
        }    
    }

    public ResultSet getGeneratedKeys() throws SQLException
    {
        return stmt.getGeneratedKeys();
    }

    public boolean getMoreResults(int arg0) throws SQLException
    {
        return stmt.getMoreResults(arg0);
    }

    public int getResultSetHoldability() throws SQLException
    {
        return stmt.getResultSetHoldability();
    }

	public <T> T unwrap(Class<T> iface) throws SQLException {
		return null;
	}

	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return false;
	}

	public boolean isClosed() throws SQLException {
		return false;
	}

	public void setPoolable(boolean poolable) throws SQLException {
		
	}

	public boolean isPoolable() throws SQLException {
		return false;
	}

	public void closeOnCompletion() throws SQLException {
		
	}

	public boolean isCloseOnCompletion() throws SQLException {
		return false;
	}
}
