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.Array; import java.sql.Blob; import java.sql.Clob; import java.sql.Connection; import java.sql.Date; import java.sql.NClob; import java.sql.ParameterMetaData; import java.sql.PreparedStatement; import java.sql.Ref; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.RowId; import java.sql.SQLException; import java.sql.SQLWarning; import java.sql.SQLXML; import java.sql.Time; import java.sql.Timestamp; import java.util.Calendar; import java.util.Comparator; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; /** * 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 JdbcLogger logger; private PreparedStatement statement; private String sql; private QueryType type; private boolean autocommit; private long milli; private long nano; private final Map<Integer, Parameter> parameters = new TreeMap<Integer, Parameter>(); public PreparedStatementLogger(JdbcLogger logger, PreparedStatement statement) throws SQLException { this.logger = logger; this.statement = statement; this.sql = ""; this.type = QueryType.UNKNOWN; 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, new TreeMap<Integer, Parameter>(parameters)); } public PreparedStatementLogger(JdbcLogger logger, Connection connection, String sql) throws SQLException { this.logger = logger; this.sql = sql; this.type = QueryType.PREPARED; clearTime(); statement = connection.prepareStatement(sql); } public PreparedStatementLogger(JdbcLogger logger, Connection connection, String sql, int resultSetType, int resultSetConcurrency) throws SQLException { this.logger = logger; this.sql = sql; this.type = QueryType.PREPARED; clearTime(); statement = connection.prepareStatement(sql, resultSetType, resultSetConcurrency); } public PreparedStatementLogger(JdbcLogger logger, Connection connection, String sql, int autoGeneratedKeys) throws SQLException { this.logger = logger; this.sql = sql; this.type = QueryType.PREPARED; clearTime(); statement = connection.prepareStatement(sql, autoGeneratedKeys); } public PreparedStatementLogger(JdbcLogger logger, Connection connection, String sql, int[] columnIndexes) throws SQLException { this.logger = logger; this.sql = sql; this.type = QueryType.PREPARED; clearTime(); statement = connection.prepareStatement(sql, columnIndexes); } public PreparedStatementLogger(JdbcLogger logger, Connection connection, String sql, String[] columnNames) throws SQLException { this.logger = logger; 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); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.NULL)); } public void setBoolean(int parameterIndex, boolean x) throws SQLException { statement.setBoolean(parameterIndex, x); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.BOOLEAN, x)); } public void setByte(int parameterIndex, byte x) throws SQLException { statement.setByte(parameterIndex, x); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.BYTE, x)); } public void setShort(int parameterIndex, short x) throws SQLException { statement.setShort(parameterIndex, x); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.SHORT, x)); } public void setInt(int parameterIndex, int x) throws SQLException { statement.setInt(parameterIndex, x); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.INT, x)); } public void setLong(int parameterIndex, long x) throws SQLException { statement.setLong(parameterIndex, x); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.LONG, x)); } public void setFloat(int parameterIndex, float x) throws SQLException { statement.setFloat(parameterIndex, x); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.FLOAT, x)); } public void setDouble(int parameterIndex, double x) throws SQLException { statement.setDouble(parameterIndex, x); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.DOUBLE, x)); } public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException { statement.setBigDecimal(parameterIndex, x); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.BIG_DECIMAL, x)); } public void setString(int parameterIndex, String x) throws SQLException { statement.setString(parameterIndex, x); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.STRING, x)); } public void setBytes(int parameterIndex, byte x[]) throws SQLException { statement.setBytes(parameterIndex, x); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.BYTES, x)); } public void setDate(int parameterIndex, Date x) throws SQLException { statement.setDate(parameterIndex, x); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.DATE, x)); } public void setTime(int parameterIndex, Time x) throws SQLException { statement.setTime(parameterIndex, x); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.TIME, x)); } public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException { statement.setTimestamp(parameterIndex, x); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.TIMESTAMP, x)); } public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException { statement.setAsciiStream(parameterIndex, x, length); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.ASCII_STR)); } public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException { statement.setUnicodeStream(parameterIndex, x, length); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.UNICODE_STR)); } public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException { statement.setBinaryStream(parameterIndex, x, length); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.BIN_STR)); } public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException { statement.setObject(parameterIndex, x, targetSqlType); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.OBJECT, x)); } public void setObject(int parameterIndex, Object x) throws SQLException { statement.setObject(parameterIndex, x); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.OBJECT, x)); } public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException { statement.setCharacterStream(parameterIndex, reader, length); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.CHAR_STR, reader)); } public void setRef(int parameterIndex, Ref x) throws SQLException { statement.setRef(parameterIndex, x); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.REF, x)); } public void setBlob(int parameterIndex, Blob x) throws SQLException { statement.setBlob(parameterIndex, x); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.BLOB, x)); } public void setClob(int parameterIndex, Clob x) throws SQLException { statement.setClob(parameterIndex, x); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.CLOB, x)); } public void setArray(int parameterIndex, Array x) throws SQLException { statement.setArray(parameterIndex, x); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.ARRAY, x)); } public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException { statement.setDate(parameterIndex, x, cal); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.DATE, x)); } public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException { statement.setTime(parameterIndex, x, cal); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.TIME, x)); } public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException { statement.setTimestamp(parameterIndex, x, cal); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.TIMESTAMP, x)); } public void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException { statement.setNull(parameterIndex, sqlType, typeName); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.NULL)); } public void setURL(int parameterIndex, URL x) throws SQLException { statement.setURL(parameterIndex, x); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.URL, x)); } public void setRowId(int parameterIndex, RowId x) throws SQLException { statement.setRowId(parameterIndex, x); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.ROW_ID, x)); } public void setNString(int parameterIndex, String value) throws SQLException { statement.setNString(parameterIndex, value); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.NSTRING, value)); } public void setNCharacterStream(int parameterIndex, Reader value, long length) throws SQLException { statement.setNCharacterStream(parameterIndex, value, length); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.NCHAR_STR, value)); } public void setNClob(int parameterIndex, NClob value) throws SQLException { statement.setNClob(parameterIndex, value); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.NCLOB, value)); } public void setClob(int parameterIndex, Reader reader, long length) throws SQLException { statement.setClob(parameterIndex, reader, length); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.CLOB, reader)); } public void setBlob(int parameterIndex, InputStream inputStream, long length) throws SQLException { statement.setBlob(parameterIndex, inputStream, length); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.BLOB)); } public void setNClob(int parameterIndex, Reader reader, long length) throws SQLException { statement.setNClob(parameterIndex, reader, length); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.NCLOB)); } public void setSQLXML(int parameterIndex, SQLXML xmlObject) throws SQLException { statement.setSQLXML(parameterIndex, xmlObject); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.SQLXML)); } public void setObject(int parameterIndex, Object x, int targetSqlType, int scaleOrLength) throws SQLException { statement.setObject(parameterIndex, x, targetSqlType, scaleOrLength); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.OBJECT, x)); } public void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException { statement.setAsciiStream(parameterIndex, x, length); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.ASCII_STR)); } public void setBinaryStream(int parameterIndex, InputStream x, long length) throws SQLException { statement.setBinaryStream(parameterIndex, x, length); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.BIN_STR)); } public void setCharacterStream(int parameterIndex, Reader reader, long length) throws SQLException { statement.setCharacterStream(parameterIndex, reader, length); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.CHAR_STR)); } public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException { statement.setAsciiStream(parameterIndex, x); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.ASCII_STR)); } public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException { statement.setBinaryStream(parameterIndex, x); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.BIN_STR)); } public void setCharacterStream(int parameterIndex, Reader reader) throws SQLException { statement.setCharacterStream(parameterIndex, reader); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.CHAR_STR)); } public void setNCharacterStream(int parameterIndex, Reader value) throws SQLException { statement.setNCharacterStream(parameterIndex, value); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.NCHAR_STR)); } public void setClob(int parameterIndex, Reader reader) throws SQLException { statement.setClob(parameterIndex, reader); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.CLOB)); } public void setBlob(int parameterIndex, InputStream inputStream) throws SQLException { statement.setBlob(parameterIndex, inputStream); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.BLOB)); } public void setNClob(int parameterIndex, Reader reader) throws SQLException { statement.setNClob(parameterIndex, reader); parameters.put(parameterIndex, new Parameter(parameterIndex, Parameter.ParameterType.NCLOB)); } public void close() throws SQLException { sql = ""; type = QueryType.UNKNOWN; milli = 0; nano = 0; statement.close(); parameters.clear(); } public ResultSetMetaData getMetaData() throws SQLException { return statement.getMetaData(); } public void clearParameters() throws SQLException { statement.clearParameters(); parameters.clear(); } 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 { 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 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); } }