package com.eactive.eai.common.jdbc;

import java.io.InputStream;
import java.io.Reader;
import java.io.Serializable;
import java.math.BigDecimal;
import java.net.URL;

import java.sql.*;

import java.util.*;


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

    private Connection con;
    private PreparedStatement ps;
    private String sql;
    private Hashtable<Integer, Serializable> hashtable;

    public PreparedStatementProxy(Connection con, String sql,
        PreparedStatement ps) {
        this.con = con;
        this.sql = sql;
        this.ps = ps;

        hashtable = new Hashtable<Integer, Serializable>();
    }

    public void setNull(int parameterIndex, int sqlType)
        throws SQLException {
        ps.setNull(parameterIndex, sqlType);
    }

    public void setBoolean(int parameterIndex, boolean x)
        throws SQLException {
        hashtable.put(new Integer(parameterIndex), new Boolean(x));
        ps.setBoolean(parameterIndex, x);
    }

    public void setByte(int parameterIndex, byte x) throws SQLException {
        hashtable.put(new Integer(parameterIndex), new Byte(x));
        ps.setByte(parameterIndex, x);
    }

    public void setShort(int parameterIndex, short x) throws SQLException {
        hashtable.put(new Integer(parameterIndex), new Short(x));
        ps.setShort(parameterIndex, x);
    }

    public void setInt(int parameterIndex, int x) throws SQLException {
        hashtable.put(new Integer(parameterIndex), new Integer(x));
        ps.setInt(parameterIndex, x);
    }

    public void setLong(int parameterIndex, long x) throws SQLException {
        hashtable.put(new Integer(parameterIndex), new Long(x));
        ps.setLong(parameterIndex, x);
    }

    public void setFloat(int parameterIndex, float x) throws SQLException {
        hashtable.put(new Integer(parameterIndex), new Float(x));
        ps.setFloat(parameterIndex, x);
    }

    public void setDouble(int parameterIndex, double x)
        throws SQLException {
        hashtable.put(new Integer(parameterIndex), new Double(x));
        ps.setDouble(parameterIndex, x);
    }

    public void setBigDecimal(int parameterIndex, BigDecimal x)
        throws SQLException {
        hashtable.put(new Integer(parameterIndex), x);
        ps.setBigDecimal(parameterIndex, x);
    }

    public void setString(int parameterIndex, String x)
        throws SQLException {
        hashtable.put(new Integer(parameterIndex), "'" + x + "'");
        ps.setString(parameterIndex, x);
    }

    public void setBytes(int parameterIndex, byte[] x)
        throws SQLException {
        hashtable.put(new Integer(parameterIndex), new String(x));
        ps.setBytes(parameterIndex, x);
    }

    public void setDate(int parameterIndex, java.sql.Date x)
        throws SQLException {
        hashtable.put(new Integer(parameterIndex), x);
        ps.setDate(parameterIndex, x);
    }

    public void setTime(int parameterIndex, java.sql.Time x)
        throws SQLException {
        hashtable.put(new Integer(parameterIndex), x);
        ps.setTime(parameterIndex, x);
    }

    public void setTimestamp(int parameterIndex, java.sql.Timestamp x)
        throws SQLException {
        hashtable.put(new Integer(parameterIndex), x);
        ps.setTimestamp(parameterIndex, x);
    }

    public void setAsciiStream(int parameterIndex, java.io.InputStream x,
        int length) throws SQLException {
        ps.setAsciiStream(parameterIndex, x, length);
    }

    @SuppressWarnings("deprecation")
	public void setUnicodeStream(int parameterIndex, java.io.InputStream x,
        int length) throws SQLException {
        ps.setUnicodeStream(parameterIndex, x, length);
    }

    public void setBinaryStream(int parameterIndex, java.io.InputStream x,
        int length) throws SQLException {
        ps.setBinaryStream(parameterIndex, x, length);
    }

    public void clearParameters() throws SQLException {
        ps.clearParameters();
    }

    public void setObject(int parameterIndex, Object x, int targetSqlType,
        int scale) throws SQLException {
        ps.setObject(parameterIndex, x, targetSqlType, scale);
    }

    public void setObject(int parameterIndex, Object x, int targetSqlType)
        throws SQLException {
        ps.setObject(parameterIndex, x, targetSqlType);
    }

    public void setObject(int parameterIndex, Object x)
        throws SQLException {
        ps.setObject(parameterIndex, x);
    }

    public void addBatch() throws SQLException {
        ps.addBatch();
    }

    public void setCharacterStream(int parameterIndex, java.io.Reader reader,
        int length) throws SQLException {
        ps.setCharacterStream(parameterIndex, reader, length);
    }

    public void setRef(int i, Ref x) throws SQLException {
        ps.setRef(i, x);
    }

    public void setBlob(int i, Blob x) throws SQLException {
        ps.setBlob(i, x);
    }

    public void setClob(int i, Clob x) throws SQLException {
        ps.setClob(i, x);
    }

    public void setArray(int i, Array x) throws SQLException {
        ps.setArray(i, x);
    }

    public ResultSetMetaData getMetaData() throws SQLException {
        return ps.getMetaData();
    }

    public void setDate(int parameterIndex, java.sql.Date x, Calendar cal)
        throws SQLException {
        ps.setDate(parameterIndex, x, cal);
    }

    public void setTime(int parameterIndex, java.sql.Time x, Calendar cal)
        throws SQLException {
        ps.setTime(parameterIndex, x, cal);
    }

    public void setTimestamp(int parameterIndex, java.sql.Timestamp x,
        Calendar cal) throws SQLException {
        ps.setTimestamp(parameterIndex, x, cal);
    }

    public void setNull(int paramIndex, int sqlType, String typeName)
        throws SQLException {
        ps.setNull(paramIndex, sqlType, typeName);
    }

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    /*
        try {
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            
            this.updateCount = pstmt.executeUpdate();

            if (stopWatch.getTime() > 1000L && loggerDB.isWarnEnabled() && !loggerDB.isInfoEnabled() && pstmt instanceof sscard.fds.startup.fw.jdbc.PreparedStatementProxy)
                loggerDB.warn("[" + this.getClass().getName() + ": Update time too long] " + stopWatch.getTime() + " : \n" + pstmt.toString());
            stopWatch.stop();
        } catch (SQLException e) {
            throw e;
        }
    */
    public ResultSet executeQuery() throws SQLException {
        try {
            //stopWatch.start();
   
            ResultSet result = new ResultSetProxy(this, ps.executeQuery());
            return result;
        } catch(SQLException e) {
            //logger.error("[PstmtProxy] SQL-FAIL : " + e.toString() + "\n" + toString());
            throw e;
        } finally {
            System.out.println("[PstmtProxy] "+ toString());
            /*
            stopWatch.stop();
            if (stopWatch.getTime() >= DBPool.TRACE_SQL_TIMEOUT) {
                loggerDB.warn("[PstmtProxy] SQL-TOO-LONG : " + stopWatch + "\n" + toString());
            }
            if (DBPool.TRACE_SQL_DUMP) {
                loggerDB.warn("[PstmtProxy] Thread Dump (NOT ERROR!) -----");
                Thread.dumpStack();
            }
            */
        }
    }

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

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

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

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

    public boolean execute(String sql) throws SQLException {
        try {
            //stopWatch.start();

            boolean result = ps.execute(sql);
            return result;
        } catch(SQLException e) {
            //logger.error("[PstmtProxy] SQL-FAIL : " + e.toString() + "\n" + toString());
            throw e;
        } finally {
            System.out.println("[PstmtProxy] "+ toString());
            /*
            stopWatch.stop();
            if (stopWatch.getTime() > DBPool.TRACE_SQL_TIMEOUT) {
                loggerDB.warn("[PstmtProxy] SQL-TOO-LONG : " + stopWatch + "\n" + toString());
            }
            if (DBPool.TRACE_SQL_DUMP) {
                loggerDB.warn("[PstmtProxy] Thread Dump (NOT ERROR!) -----");
                Thread.dumpStack();
            }
            */
        }
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    public String toString() {
        String str;
        int index;

        String sqlTmp = new String(sql);
        for (int i = 1; i < (hashtable.size() + 1); i++) {
            str = hashtable.get(new Integer(i)).toString();
            index = sqlTmp.indexOf("?");
            sqlTmp = sqlTmp.substring(0, index) + str 
                + sqlTmp.substring(index + 1, sqlTmp.length());
        }
        
        return sqlTmp;
    }


    // BEGIN_COMMENT_FOR_JAVA_1.3
    // END_COMMENT_FOR_JAVA_1.3

    public ParameterMetaData getParameterMetaData() throws SQLException
    {
        return ps.getParameterMetaData();
    }

    public void setURL(int arg0, URL arg1) throws SQLException
    {
        ps.setURL(arg0,arg1);
    }

    public boolean execute(String arg0, String[] arg1) throws SQLException
    {
        try {
            //stopWatch.start();

            boolean result = ps.execute(arg0,arg1);
            return result;
        } catch(SQLException e) {
            //logger.error("[PstmtProxy] SQL-FAIL : " + e.toString() + "\n" + toString());
            throw e;
        } finally {
            System.out.println("[PstmtProxy] "+ toString());
            /*
            stopWatch.stop();
            if (stopWatch.getTime() > DBPool.TRACE_SQL_TIMEOUT) {
                loggerDB.warn("[PstmtProxy] SQL-TOO-LONG : " + stopWatch + "\n" + toString());
            }
            if (DBPool.TRACE_SQL_DUMP) {
                loggerDB.warn("[PstmtProxy] Thread Dump (NOT ERROR!) -----");
                Thread.dumpStack();
            }
            */
        }        
    }

    public boolean execute(String arg0, int arg1) throws SQLException
    {
        try {
            //stopWatch.start();

            boolean result = ps.execute(arg0,arg1);
            return result;
        } catch(SQLException e) {
            //logger.error("[PstmtProxy] SQL-FAIL : " + e.toString() + "\n" + toString());
            throw e;
        } finally {
            System.out.println("[PstmtProxy] "+ toString());
            /*
            stopWatch.stop();
            if (stopWatch.getTime() > DBPool.TRACE_SQL_TIMEOUT) {
                loggerDB.warn("[PstmtProxy] SQL-TOO-LONG : " + stopWatch + "\n" + toString());
            }
            if (DBPool.TRACE_SQL_DUMP) {
                loggerDB.warn("[PstmtProxy] Thread Dump (NOT ERROR!) -----");
                Thread.dumpStack();
            }
            */
        }        
    }

    public boolean execute(String arg0, int[] arg1) throws SQLException
    {
        try {
            //stopWatch.start();

            boolean result = ps.execute(arg0,arg1);
            return result;
        } catch(SQLException e) {
            //logger.error("[PstmtProxy] SQL-FAIL : " + e.toString() + "\n" + toString());
            throw e;
        } finally {
            System.out.println("[PstmtProxy] "+ toString());
            /*
            stopWatch.stop();
            if (stopWatch.getTime() > DBPool.TRACE_SQL_TIMEOUT) {
                loggerDB.warn("[PstmtProxy] SQL-TOO-LONG : " + stopWatch + "\n" + toString());
            }
            if (DBPool.TRACE_SQL_DUMP) {
                loggerDB.warn("[PstmtProxy] Thread Dump (NOT ERROR!) -----");
                Thread.dumpStack();
            }
            */
        }        
    }

    public int executeUpdate(String arg0, String[] arg1) throws SQLException
    {
        try {
            //stopWatch.start();

            int result = ps.executeUpdate(arg0,arg1);
            return result;
        } catch(SQLException e) {
            //logger.error("[PstmtProxy] SQL-FAIL : " + e.toString() + "\n" + toString());
            throw e;
        } finally {
            System.out.println("[PstmtProxy] "+ toString());
            /*
            stopWatch.stop();
            if (stopWatch.getTime() > DBPool.TRACE_SQL_TIMEOUT) {
                loggerDB.warn("[PstmtProxy] SQL-TOO-LONG : " + stopWatch + "\n" + toString());
            }
            if (DBPool.TRACE_SQL_DUMP) {
                loggerDB.warn("[PstmtProxy] Thread Dump (NOT ERROR!) -----");
                Thread.dumpStack();
            }
            */
        }        
    }

    public int executeUpdate(String arg0, int arg1) throws SQLException
    {
        try {
            //stopWatch.start();

            int result = ps.executeUpdate(arg0,arg1);
            return result;
        } catch(SQLException e) {
            //logger.error("[PstmtProxy] SQL-FAIL : " + e.toString() + "\n" + toString());
            throw e;
        } finally {
            System.out.println("[PstmtProxy] "+ toString());
            /*
            stopWatch.stop();
            if (stopWatch.getTime() > DBPool.TRACE_SQL_TIMEOUT) {
                loggerDB.warn("[PstmtProxy] SQL-TOO-LONG : " + stopWatch + "\n" + toString());
            }
            if (DBPool.TRACE_SQL_DUMP) {
                loggerDB.warn("[PstmtProxy] Thread Dump (NOT ERROR!) -----");
                Thread.dumpStack();
            }
            */
        }        
    }

    public int executeUpdate(String arg0, int[] arg1) throws SQLException
    {
        try {
            //stopWatch.start();

            int result = ps.executeUpdate(arg0,arg1);
            return result;
        } catch(SQLException e) {
            //logger.error("[PstmtProxy] SQL-FAIL : " + e.toString() + "\n" + toString());
            throw e;
        } finally {
            System.out.println("[PstmtProxy] "+ toString());
            /*
            stopWatch.stop();
            if (stopWatch.getTime() > DBPool.TRACE_SQL_TIMEOUT) {
                loggerDB.warn("[PstmtProxy] SQL-TOO-LONG : " + stopWatch + "\n" + toString());
            }
            if (DBPool.TRACE_SQL_DUMP) {
                loggerDB.warn("[PstmtProxy] Thread Dump (NOT ERROR!) -----");
                Thread.dumpStack();
            }
            */
        }        
    }

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

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

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

	
	public boolean isClosed() throws SQLException {

		return false;
	}

	
	public void setPoolable(boolean poolable) throws SQLException {

		
	}

	
	public boolean isPoolable() throws SQLException {

		return false;
	}

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

		return null;
	}

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

		return false;
	}

	
	public void setRowId(int parameterIndex, RowId x) throws SQLException {

		
	}

	
	public void setNString(int parameterIndex, String value)
			throws SQLException {

		
	}

	
	public void setNCharacterStream(int parameterIndex, Reader value,
			long length) throws SQLException {

		
	}

	
	public void setNClob(int parameterIndex, NClob value) throws SQLException {

		
	}

	
	public void setClob(int parameterIndex, Reader reader, long length)
			throws SQLException {

		
	}

	
	public void setBlob(int parameterIndex, InputStream inputStream, long length)
			throws SQLException {

		
	}

	
	public void setNClob(int parameterIndex, Reader reader, long length)
			throws SQLException {

		
	}

	
	public void setSQLXML(int parameterIndex, SQLXML xmlObject)
			throws SQLException {

		
	}

	
	public void setAsciiStream(int parameterIndex, InputStream x, long length)
			throws SQLException {

		
	}

	
	public void setBinaryStream(int parameterIndex, InputStream x, long length)
			throws SQLException {

		
	}

	
	public void setCharacterStream(int parameterIndex, Reader reader,
			long length) throws SQLException {

		
	}

	
	public void setAsciiStream(int parameterIndex, InputStream x)
			throws SQLException {

		
	}

	
	public void setBinaryStream(int parameterIndex, InputStream x)
			throws SQLException {

		
	}

	
	public void setCharacterStream(int parameterIndex, Reader reader)
			throws SQLException {

		
	}

	
	public void setNCharacterStream(int parameterIndex, Reader value)
			throws SQLException {

		
	}

	
	public void setClob(int parameterIndex, Reader reader) throws SQLException {

		
	}

	
	public void setBlob(int parameterIndex, InputStream inputStream)
			throws SQLException {

		
	}

	
	public void setNClob(int parameterIndex, Reader reader) throws SQLException {

		
	}

	public void closeOnCompletion() throws SQLException {
		
	}

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