package nl.astraeus.jdbc;

import java.sql.*;

/**
 * User: rnentjes
 * Date: 4/17/12
 * Time: 9:32 PM
 */
public class StatementLogger implements Statement {

    private Statement statement;

    private String sql;
    private long milli;
    private long nano;

    public StatementLogger(Statement statement) {
        this.statement = statement;

        this.sql = "";
    }

    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() == null ? true : getConnection().getAutoCommit());
    }

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

        ResultSet result = statement.executeQuery(sql);

        log(QueryType.STATEMENT, sql);

        return result;
    }

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

        int result = statement.executeUpdate(sql);

        log(QueryType.STATEMENT, sql);

        return result;
    }

    public void close() throws SQLException {
        clearTime();

        statement.close();

        log(QueryType.STATEMENT, "close");
    }

    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 {
        clearTime();

        statement.cancel();

        log(QueryType.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 {
        clearTime();

        boolean result = statement.execute(sql);

        log(QueryType.STATEMENT, sql);

        return result;
    }

    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 {
        this.sql += "\n"+sql;
        statement.addBatch(sql);
    }

    public void clearBatch() throws SQLException {
        this.sql = "";
        statement.clearBatch();
    }

    public int[] executeBatch() throws SQLException {
        clearTime();

        int [] result = statement.executeBatch();

        log(QueryType.STATEMENT, sql);

        return result;
    }

    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 executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
        clearTime();

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

        log(QueryType.STATEMENT, sql);

        return result;
    }

    public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
        clearTime();

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

        log(QueryType.STATEMENT, sql);

        return result;
    }

    public int executeUpdate(String sql, String[] columnNames) throws SQLException {
        clearTime();

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

        log(QueryType.STATEMENT, sql);

        return result;
    }

    public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
        clearTime();

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

        log(QueryType.STATEMENT, sql);

        return result;
    }

    public boolean execute(String sql, int[] columnIndexes) throws SQLException {
        clearTime();

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

        log(QueryType.STATEMENT, sql);

        return result;
    }

    public boolean execute(String sql, String[] columnNames) throws SQLException {
        clearTime();

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

        log(QueryType.STATEMENT, sql);

        return result;
    }

    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);
    }
}
