Newer
Older
simple-jdbc-stats / src / nl / astraeus / jdbc / StatementLogger.java
package nl.astraeus.jdbc;

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

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

    private JdbcLogger logger;
    private Statement statement;

    private String sql;
    private boolean autocommit;
    private long milli;
    private long nano;

    public StatementLogger(JdbcLogger logger, Statement statement) throws SQLException {
        this.logger = logger;
        this.statement = statement;

        this.sql = "";
        this.autocommit = statement.getConnection().getAutoCommit();
    }

    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;

        logger.logEntry(type, sql, m, n, autocommit);
    }

    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.BATCH, 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);
    }
}