package nl.astraeus.jdbc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.*;
import java.util.Calendar;

/**
 * User: riennentjes
 * Date: Jul 10, 2008
 * Time: 7:47:57 PM
 */
public class PreparedStatementLogger implements PreparedStatement {
    private final static Logger log = LoggerFactory.getLogger(PreparedStatementLogger.class);

    private PreparedStatement statement;

    private String sql;
    private QueryType type;
    private long milli;
    private long nano;

    //private ArrayList parameters;

    public PreparedStatementLogger(PreparedStatement statement) {
        this.statement = statement;
        this.sql = "";
        this.type = QueryType.UNKNOWN;
    }

    private void clearTime() {
        milli = System.currentTimeMillis();
        nano = System.nanoTime();
    }

    private void log(QueryType type, String sql) throws SQLException {
        long m = System.currentTimeMillis() - milli;
        long n = System.nanoTime() - nano;

        JdbcLogger.log(type, sql, m, n, getConnection().getAutoCommit());
    }

    public PreparedStatementLogger(Connection connection, String sql) throws SQLException {
        this.sql = sql;
        this.type = QueryType.PREPARED;
        clearTime();
        statement = connection.prepareStatement(sql);
    }

    public PreparedStatementLogger(Connection connection, String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
        this.sql = sql;
        this.type = QueryType.PREPARED;
        clearTime();
        statement = connection.prepareStatement(sql, resultSetType, resultSetConcurrency);
    }

    public PreparedStatementLogger(Connection connection, String sql, int autoGeneratedKeys) throws SQLException {
        this.sql = sql;
        this.type = QueryType.PREPARED;
        clearTime();
        statement = connection.prepareStatement(sql, autoGeneratedKeys);
    }

    public PreparedStatementLogger(Connection connection, String sql, int[] columnIndexes) throws SQLException {
        this.sql = sql;
        this.type = QueryType.PREPARED;
        clearTime();
        statement = connection.prepareStatement(sql, columnIndexes);
    }

    public PreparedStatementLogger(Connection connection, String sql, String[] columnNames) throws SQLException {
        this.sql = sql;
        this.type = QueryType.PREPARED;
        clearTime();
        statement = connection.prepareStatement(sql, columnNames);
    }

    public boolean execute() throws SQLException {
        clearTime();

        boolean result = statement.execute();

        log(type, sql);

        return result;
    }

    public ResultSet executeQuery() throws SQLException {
        clearTime();

        ResultSet result = statement.executeQuery();

        log(type, sql);

        return result;
    }

    public int executeUpdate() throws SQLException {
        clearTime();

        int result = statement.executeUpdate();

        log(type, sql);

        return result;
    }

    public ResultSet executeQuery(String sql) throws SQLException {
        this.sql = sql;
        this.type = QueryType.PLAIN;

        clearTime();

        ResultSet result = statement.executeQuery(sql);

        log(type, sql);

        return result;
    }

    public int executeUpdate(String sql) throws SQLException {
        this.sql = sql;
        this.type = QueryType.PLAIN;

        clearTime();

        int result = statement.executeUpdate(sql);

        log(type, sql);

        return result;
    }

    public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
        this.sql = sql;
        this.type = QueryType.PLAIN;

        clearTime();

        int result = statement.executeUpdate(sql, autoGeneratedKeys);

        log(type, sql);

        return result;
    }

    public int executeUpdate(String sql, int columnIndexes[]) throws SQLException {
        this.sql = sql;
        this.type = QueryType.PLAIN;

        clearTime();

        int result = statement.executeUpdate(sql, columnIndexes);

        log(type, sql);

        return result;
    }

    public int executeUpdate(String sql, String columnNames[]) throws SQLException {
        this.sql = sql;
        this.type = QueryType.PLAIN;

        clearTime();

        int result = statement.executeUpdate(sql, columnNames);

        log(type, sql);

        return result;
    }

    public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
        this.sql = sql;
        this.type = QueryType.PLAIN;

        clearTime();

        boolean result = statement.execute(sql, autoGeneratedKeys);

        log(type, sql);

        return result;
    }

    public boolean execute(String sql, int columnIndexes[]) throws SQLException {
        this.sql = sql;
        this.type = QueryType.PLAIN;

        clearTime();

        boolean result = statement.execute(sql, columnIndexes);

        log(type, sql);

        return result;
    }

    public boolean execute(String sql, String columnNames[]) throws SQLException {
        this.sql = sql;
        this.type = QueryType.PLAIN;

        clearTime();

        boolean result = statement.execute(sql, columnNames);

        log(type, sql);

        return result;
    }


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

    public void setBoolean(int parameterIndex, boolean x) throws SQLException {
        statement.setBoolean(parameterIndex, x);
    }

    public void setByte(int parameterIndex, byte x) throws SQLException {
        statement.setByte(parameterIndex, x);
    }

    public void setShort(int parameterIndex, short x) throws SQLException {
        statement.setShort(parameterIndex, x);
    }

    public void setInt(int parameterIndex, int x) throws SQLException {
        statement.setInt(parameterIndex, x);
    }

    public void setLong(int parameterIndex, long x) throws SQLException {
        statement.setLong(parameterIndex, x);
    }

    public void setFloat(int parameterIndex, float x) throws SQLException {
        statement.setFloat(parameterIndex, x);
    }

    public void setDouble(int parameterIndex, double x) throws SQLException {
        statement.setDouble(parameterIndex, x);
    }

    public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException {
        statement.setBigDecimal(parameterIndex, x);
    }

    public void setString(int parameterIndex, String x) throws SQLException {
        statement.setString(parameterIndex, x);
    }

    public void setBytes(int parameterIndex, byte x[]) throws SQLException {
        statement.setBytes(parameterIndex, x);
    }

    public void setDate(int parameterIndex, Date x) throws SQLException {
        statement.setDate(parameterIndex, x);
    }

    public void setTime(int parameterIndex, Time x) throws SQLException {
        statement.setTime(parameterIndex, x);
    }

    public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException {
        statement.setTimestamp(parameterIndex, x);
    }

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

    public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException {
        statement.setUnicodeStream(parameterIndex, x, length);
    }

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

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

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

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

    public void setRef(int parameterIndex, Ref x) throws SQLException {
        statement.setRef(parameterIndex, x);
    }

    public void setBlob(int parameterIndex, Blob x) throws SQLException {
        statement.setBlob(parameterIndex, x);
    }

    public void setClob(int parameterIndex, Clob x) throws SQLException {
        statement.setClob(parameterIndex, x);
    }

    public void setArray(int parameterIndex, Array x) throws SQLException {
        statement.setArray(parameterIndex, x);
    }

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

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

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

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

    public void setURL(int parameterIndex, URL x) throws SQLException {
        statement.setURL(parameterIndex, x);
    }

    public void setRowId(int parameterIndex, RowId x) throws SQLException {
        statement.setRowId(parameterIndex, x);
    }

    public void setNString(int parameterIndex, String value) throws SQLException {
        statement.setNString(parameterIndex, value);
    }

    public void setNCharacterStream(int parameterIndex, Reader value, long length) throws SQLException {
        statement.setNCharacterStream(parameterIndex, value, length);
    }

    public void setNClob(int parameterIndex, NClob value) throws SQLException {
        statement.setNClob(parameterIndex, value);
    }

    public void setClob(int parameterIndex, Reader reader, long length) throws SQLException {
        statement.setClob(parameterIndex, reader, length);
    }

    public void setBlob(int parameterIndex, InputStream inputStream, long length) throws SQLException {
        statement.setBlob(parameterIndex, inputStream, length);
    }

    public void setNClob(int parameterIndex, Reader reader, long length) throws SQLException {
        statement.setNClob(parameterIndex, reader, length);
    }

    public void setSQLXML(int parameterIndex, SQLXML xmlObject) throws SQLException {
        statement.setSQLXML(parameterIndex, xmlObject);
    }

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

    public void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException {
        statement.setAsciiStream(parameterIndex, x, length);
    }

    public void setBinaryStream(int parameterIndex, InputStream x, long length) throws SQLException {
        statement.setBinaryStream(parameterIndex, x, length);
    }

    public void setCharacterStream(int parameterIndex, Reader reader, long length) throws SQLException {
        statement.setCharacterStream(parameterIndex, reader, length);
    }

    public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException {
        statement.setAsciiStream(parameterIndex, x);
    }

    public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException {
        statement.setBinaryStream(parameterIndex, x);
    }

    public void setCharacterStream(int parameterIndex, Reader reader) throws SQLException {
        statement.setCharacterStream(parameterIndex, reader);
    }

    public void setNCharacterStream(int parameterIndex, Reader value) throws SQLException {
        statement.setNCharacterStream(parameterIndex, value);
    }

    public void setClob(int parameterIndex, Reader reader) throws SQLException {
        statement.setClob(parameterIndex, reader);
    }

    public void setBlob(int parameterIndex, InputStream inputStream) throws SQLException {
        statement.setBlob(parameterIndex, inputStream);
    }

    public void setNClob(int parameterIndex, Reader reader) throws SQLException {
        statement.setNClob(parameterIndex, reader);
    }


    public void close() throws SQLException {
        sql = "";
        type = QueryType.UNKNOWN;
        milli = 0;
        nano = 0;

        statement.close();
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    public boolean execute(String sql) throws SQLException {
        return statement.execute(sql);
    }

    public ResultSet getResultSet() throws SQLException {
        return statement.getResultSet();
    }

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

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

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

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

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

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

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

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

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

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

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

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

    public boolean getMoreResults(int current) throws SQLException {
        return statement.getMoreResults(current);
    }

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

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

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

    public void setPoolable(boolean poolable) throws SQLException {
        statement.setPoolable(poolable);
    }

    public boolean isPoolable() throws SQLException {
        return statement.isPoolable();
    }

    public void closeOnCompletion() throws SQLException {
        statement.closeOnCompletion();;
    }

    public boolean isCloseOnCompletion() throws SQLException {
        return statement.isCloseOnCompletion();
    }

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

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