Commit 7274e07a authored by Matt Tucker's avatar Matt Tucker Committed by matt

Work on using dynamic proxies.

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@6090 b35dd754-fafc-0310-a699-88a17e54d16e
parent d0b38e27
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2004 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.database;
import java.sql.*;
import java.math.BigDecimal;
import java.util.Map;
import java.util.Calendar;
import java.net.URL;
import java.io.InputStream;
import java.io.Reader;
/**
* An implementation of the CallableStatement interface that wraps an underlying
* CallableStatement object.
*
* @author Gaston Dombiak
*/
public abstract class CallableStatementWrapper extends StatementWrapper
implements CallableStatement {
protected CallableStatement cstmt;
public CallableStatementWrapper(CallableStatement cstmt) {
super(cstmt);
this.cstmt = cstmt;
}
public void registerOutParameter(int parameterIndex, int sqlType) throws SQLException {
cstmt.registerOutParameter(parameterIndex, sqlType);
}
public void registerOutParameter(int parameterIndex, int sqlType, int scale)
throws SQLException {
cstmt.registerOutParameter(parameterIndex, sqlType, scale);
}
public boolean wasNull() throws SQLException {
return cstmt.wasNull();
}
public String getString(int parameterIndex) throws SQLException {
return cstmt.getString(parameterIndex);
}
public boolean getBoolean(int parameterIndex) throws SQLException {
return cstmt.getBoolean(parameterIndex);
}
public byte getByte(int parameterIndex) throws SQLException {
return cstmt.getByte(parameterIndex);
}
public short getShort(int parameterIndex) throws SQLException {
return cstmt.getShort(parameterIndex);
}
public int getInt(int parameterIndex) throws SQLException {
return cstmt.getInt(parameterIndex);
}
public long getLong(int parameterIndex) throws SQLException {
return cstmt.getLong(parameterIndex);
}
public float getFloat(int parameterIndex) throws SQLException {
return cstmt.getFloat(parameterIndex);
}
public double getDouble(int parameterIndex) throws SQLException {
return cstmt.getDouble(parameterIndex);
}
@Deprecated public BigDecimal getBigDecimal(int parameterIndex, int scale) throws SQLException {
return cstmt.getBigDecimal(parameterIndex, scale);
}
public byte[] getBytes(int parameterIndex) throws SQLException {
return cstmt.getBytes(parameterIndex);
}
public Date getDate(int parameterIndex) throws SQLException {
return cstmt.getDate(parameterIndex);
}
public Time getTime(int parameterIndex) throws SQLException {
return cstmt.getTime(parameterIndex);
}
public Timestamp getTimestamp(int parameterIndex) throws SQLException {
return cstmt.getTimestamp(parameterIndex);
}
public Object getObject(int parameterIndex) throws SQLException {
return cstmt.getObject(parameterIndex);
}
public BigDecimal getBigDecimal(int parameterIndex) throws SQLException {
return cstmt.getBigDecimal(parameterIndex);
}
public Object getObject(int i, Map<String, Class<?>> map) throws SQLException {
return cstmt.getObject(i, map);
}
public Ref getRef(int i) throws SQLException {
return cstmt.getRef(i);
}
public Blob getBlob(int i) throws SQLException {
return cstmt.getBlob(i);
}
public Clob getClob(int i) throws SQLException {
return cstmt.getClob(i);
}
public Array getArray(int i) throws SQLException {
return cstmt.getArray(i);
}
public Date getDate(int parameterIndex, Calendar cal) throws SQLException {
return cstmt.getDate(parameterIndex, cal);
}
public Time getTime(int parameterIndex, Calendar cal) throws SQLException {
return cstmt.getTime(parameterIndex, cal);
}
public Timestamp getTimestamp(int parameterIndex, Calendar cal) throws SQLException {
return cstmt.getTimestamp(parameterIndex, cal);
}
public void registerOutParameter(int paramIndex, int sqlType, String typeName)
throws SQLException {
cstmt.registerOutParameter(paramIndex, sqlType, typeName);
}
public void registerOutParameter(String parameterName, int sqlType) throws SQLException {
cstmt.registerOutParameter(parameterName, sqlType);
}
public void registerOutParameter(String parameterName, int sqlType, int scale)
throws SQLException {
cstmt.registerOutParameter(parameterName, sqlType, scale);
}
public void registerOutParameter(String parameterName, int sqlType, String typeName)
throws SQLException {
cstmt.registerOutParameter(parameterName, sqlType, typeName);
}
public URL getURL(int parameterIndex) throws SQLException {
return cstmt.getURL(parameterIndex);
}
public void setURL(String parameterName, URL val) throws SQLException {
cstmt.setURL(parameterName, val);
}
public void setNull(String parameterName, int sqlType) throws SQLException {
cstmt.setNull(parameterName, sqlType);
}
public void setBoolean(String parameterName, boolean x) throws SQLException {
cstmt.setBoolean(parameterName, x);
}
public void setByte(String parameterName, byte x) throws SQLException {
cstmt.setByte(parameterName, x);
}
public void setShort(String parameterName, short x) throws SQLException {
cstmt.setShort(parameterName, x);
}
public void setInt(String parameterName, int x) throws SQLException {
cstmt.setInt(parameterName, x);
}
public void setLong(String parameterName, long x) throws SQLException {
cstmt.setLong(parameterName, x);
}
public void setFloat(String parameterName, float x) throws SQLException {
cstmt.setFloat(parameterName, x);
}
public void setDouble(String parameterName, double x) throws SQLException {
cstmt.setDouble(parameterName, x);
}
public void setBigDecimal(String parameterName, BigDecimal x) throws SQLException {
cstmt.setBigDecimal(parameterName, x);
}
public void setString(String parameterName, String x) throws SQLException {
cstmt.setString(parameterName, x);
}
public void setBytes(String parameterName, byte x[]) throws SQLException {
cstmt.setBytes(parameterName, x);
}
public void setDate(String parameterName, Date x) throws SQLException {
cstmt.setDate(parameterName, x);
}
public void setTime(String parameterName, Time x) throws SQLException {
cstmt.setTime(parameterName, x);
}
public void setTimestamp(String parameterName, Timestamp x) throws SQLException {
cstmt.setTimestamp(parameterName, x);
}
public void setAsciiStream(String parameterName, InputStream x, int length)
throws SQLException {
cstmt.setAsciiStream(parameterName, x, length);
}
public void setBinaryStream(String parameterName, InputStream x, int length)
throws SQLException {
cstmt.setBinaryStream(parameterName, x, length);
}
public void setObject(String parameterName, Object x, int targetSqlType, int scale)
throws SQLException {
cstmt.setObject(parameterName, x, targetSqlType, scale);
}
public void setObject(String parameterName, Object x, int targetSqlType) throws SQLException {
cstmt.setObject(parameterName, x, targetSqlType);
}
public void setObject(String parameterName, Object x) throws SQLException {
cstmt.setObject(parameterName, x);
}
public void setCharacterStream(String parameterName, Reader reader, int length)
throws SQLException {
cstmt.setCharacterStream(parameterName, reader, length);
}
public void setDate(String parameterName, Date x, Calendar cal) throws SQLException {
cstmt.setDate(parameterName, x, cal);
}
public void setTime(String parameterName, Time x, Calendar cal) throws SQLException {
cstmt.setTime(parameterName, x, cal);
}
public void setTimestamp(String parameterName, Timestamp x, Calendar cal) throws SQLException {
cstmt.setTimestamp(parameterName, x, cal);
}
public void setNull(String parameterName, int sqlType, String typeName) throws SQLException {
cstmt.setNull(parameterName, sqlType, typeName);
}
public String getString(String parameterName) throws SQLException {
return cstmt.getString(parameterName);
}
public boolean getBoolean(String parameterName) throws SQLException {
return cstmt.getBoolean(parameterName);
}
public byte getByte(String parameterName) throws SQLException {
return cstmt.getByte(parameterName);
}
public short getShort(String parameterName) throws SQLException {
return cstmt.getShort(parameterName);
}
public int getInt(String parameterName) throws SQLException {
return cstmt.getInt(parameterName);
}
public long getLong(String parameterName) throws SQLException {
return cstmt.getLong(parameterName);
}
public float getFloat(String parameterName) throws SQLException {
return cstmt.getFloat(parameterName);
}
public double getDouble(String parameterName) throws SQLException {
return cstmt.getDouble(parameterName);
}
public byte[] getBytes(String parameterName) throws SQLException {
return cstmt.getBytes(parameterName);
}
public Date getDate(String parameterName) throws SQLException {
return cstmt.getDate(parameterName);
}
public Time getTime(String parameterName) throws SQLException {
return cstmt.getTime(parameterName);
}
public Timestamp getTimestamp(String parameterName) throws SQLException {
return cstmt.getTimestamp(parameterName);
}
public Object getObject(String parameterName) throws SQLException {
return cstmt.getObject(parameterName);
}
public BigDecimal getBigDecimal(String parameterName) throws SQLException {
return cstmt.getBigDecimal(parameterName);
}
public Object getObject(String parameterName, Map<String, Class<?>> map) throws SQLException {
return cstmt.getObject(parameterName, map);
}
public Ref getRef(String parameterName) throws SQLException {
return cstmt.getRef(parameterName);
}
public Blob getBlob(String parameterName) throws SQLException {
return cstmt.getBlob(parameterName);
}
public Clob getClob(String parameterName) throws SQLException {
return cstmt.getClob(parameterName);
}
public Array getArray(String parameterName) throws SQLException {
return cstmt.getArray(parameterName);
}
public Date getDate(String parameterName, Calendar cal) throws SQLException {
return cstmt.getDate(parameterName, cal);
}
public Time getTime(String parameterName, Calendar cal) throws SQLException {
return cstmt.getTime(parameterName, cal);
}
public Timestamp getTimestamp(String parameterName, Calendar cal) throws SQLException {
return cstmt.getTimestamp(parameterName, cal);
}
public URL getURL(String parameterName) throws SQLException {
return cstmt.getURL(parameterName);
}
public ResultSet executeQuery() throws SQLException {
return cstmt.executeQuery();
}
public int executeUpdate() throws SQLException {
return cstmt.executeUpdate();
}
public void setNull(int parameterIndex, int sqlType) throws SQLException {
cstmt.setNull(parameterIndex, sqlType);
}
public void setBoolean(int parameterIndex, boolean x) throws SQLException {
cstmt.setBoolean(parameterIndex, x);
}
public void setByte(int parameterIndex, byte x) throws SQLException {
cstmt.setByte(parameterIndex, x);
}
public void setShort(int parameterIndex, short x) throws SQLException {
cstmt.setShort(parameterIndex, x);
}
public void setInt(int parameterIndex, int x) throws SQLException {
cstmt.setInt(parameterIndex, x);
}
public void setLong(int parameterIndex, long x) throws SQLException {
cstmt.setLong(parameterIndex, x);
}
public void setFloat(int parameterIndex, float x) throws SQLException {
cstmt.setFloat(parameterIndex, x);
}
public void setDouble(int parameterIndex, double x) throws SQLException {
cstmt.setDouble(parameterIndex, x);
}
public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException {
cstmt.setBigDecimal(parameterIndex, x);
}
public void setString(int parameterIndex, String x) throws SQLException {
cstmt.setString(parameterIndex, x);
}
public void setBytes(int parameterIndex, byte x[]) throws SQLException {
cstmt.setBytes(parameterIndex, x);
}
public void setDate(int parameterIndex, Date x) throws SQLException {
cstmt.setDate(parameterIndex, x);
}
public void setTime(int parameterIndex, Time x) throws SQLException {
cstmt.setTime(parameterIndex, x);
}
public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException {
cstmt.setTimestamp(parameterIndex, x);
}
public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException {
cstmt.setAsciiStream(parameterIndex, x, length);
}
@Deprecated public void setUnicodeStream(int parameterIndex, InputStream x, int length)
throws SQLException {
cstmt.setUnicodeStream(parameterIndex, x, length);
}
public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException {
cstmt.setBinaryStream(parameterIndex, x, length);
}
public void clearParameters() throws SQLException {
cstmt.clearParameters();
}
public void setObject(int parameterIndex, Object x, int targetSqlType, int scale)
throws SQLException {
cstmt.setObject(parameterIndex, x, targetSqlType, scale);
}
public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException {
cstmt.setObject(parameterIndex, x, targetSqlType);
}
public void setObject(int parameterIndex, Object x) throws SQLException {
cstmt.setObject(parameterIndex, x);
}
public boolean execute() throws SQLException {
return cstmt.execute();
}
public void addBatch() throws SQLException {
cstmt.addBatch();
}
public void setCharacterStream(int parameterIndex, Reader reader, int length)
throws SQLException {
cstmt.setCharacterStream(parameterIndex, reader, length);
}
public void setRef(int i, Ref x) throws SQLException {
cstmt.setRef(i, x);
}
public void setBlob(int i, Blob x) throws SQLException {
cstmt.setBlob(i, x);
}
public void setClob(int i, Clob x) throws SQLException {
cstmt.setClob(i, x);
}
public void setArray(int i, Array x) throws SQLException {
cstmt.setArray(i, x);
}
public ResultSetMetaData getMetaData() throws SQLException {
return cstmt.getMetaData();
}
public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException {
cstmt.setDate(parameterIndex, x, cal);
}
public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException {
cstmt.setTime(parameterIndex, x, cal);
}
public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException {
cstmt.setTimestamp(parameterIndex, x, cal);
}
public void setNull(int paramIndex, int sqlType, String typeName) throws SQLException {
cstmt.setNull(paramIndex, sqlType, typeName);
}
public void setURL(int parameterIndex, URL x) throws SQLException {
cstmt.setURL(parameterIndex, x);
}
public ParameterMetaData getParameterMetaData() throws SQLException {
return cstmt.getParameterMetaData();
}
}
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2004 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.database;
import java.sql.*;
import java.math.BigDecimal;
import java.io.InputStream;
import java.io.Reader;
import java.util.Calendar;
import java.net.URL;
/**
* An implementation of the PreparedStatement interface that wraps an underlying
* PreparedStatement object.
*
* @author Gaston Dombiak
*/
public abstract class PreparedStatementWrapper extends StatementWrapper
implements PreparedStatement {
protected PreparedStatement pstmt;
public PreparedStatementWrapper(PreparedStatement pstmt) {
super(pstmt);
this.pstmt = pstmt;
}
public ResultSet executeQuery() throws SQLException {
return pstmt.executeQuery();
}
public int executeUpdate() throws SQLException {
return pstmt.executeUpdate();
}
public void setNull(int parameterIndex, int sqlType) throws SQLException {
pstmt.setNull(parameterIndex, sqlType);
}
public void setBoolean(int parameterIndex, boolean x) throws SQLException {
pstmt.setBoolean(parameterIndex, x);
}
public void setByte(int parameterIndex, byte x) throws SQLException {
pstmt.setByte(parameterIndex, x);
}
public void setShort(int parameterIndex, short x) throws SQLException {
pstmt.setShort(parameterIndex, x);
}
public void setInt(int parameterIndex, int x) throws SQLException {
pstmt.setInt(parameterIndex, x);
}
public void setLong(int parameterIndex, long x) throws SQLException {
pstmt.setLong(parameterIndex, x);
}
public void setFloat(int parameterIndex, float x) throws SQLException {
pstmt.setFloat(parameterIndex, x);
}
public void setDouble(int parameterIndex, double x) throws SQLException {
pstmt.setDouble(parameterIndex, x);
}
public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException {
pstmt.setBigDecimal(parameterIndex, x);
}
public void setString(int parameterIndex, String x) throws SQLException {
pstmt.setString(parameterIndex, x);
}
public void setBytes(int parameterIndex, byte x[]) throws SQLException {
pstmt.setBytes(parameterIndex, x);
}
public void setDate(int parameterIndex, Date x) throws SQLException {
pstmt.setDate(parameterIndex, x);
}
public void setTime(int parameterIndex, Time x) throws SQLException {
pstmt.setTime(parameterIndex, x);
}
public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException {
pstmt.setTimestamp(parameterIndex, x);
}
public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException {
pstmt.setAsciiStream(parameterIndex, x, length);
}
@Deprecated public void setUnicodeStream(int parameterIndex, InputStream x, int length)
throws SQLException {
pstmt.setUnicodeStream(parameterIndex, x, length);
}
public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException {
pstmt.setBinaryStream(parameterIndex, x, length);
}
public void clearParameters() throws SQLException {
pstmt.clearParameters();
}
public void setObject(int parameterIndex, Object x, int targetSqlType, int scale)
throws SQLException {
pstmt.setObject(parameterIndex, x, targetSqlType, scale);
}
public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException {
pstmt.setObject(parameterIndex, x, targetSqlType);
}
public void setObject(int parameterIndex, Object x) throws SQLException {
pstmt.setObject(parameterIndex, x);
}
public boolean execute() throws SQLException {
return pstmt.execute();
}
public void addBatch() throws SQLException {
pstmt.addBatch();
}
public void setCharacterStream(int parameterIndex, Reader reader, int length)
throws SQLException {
pstmt.setCharacterStream(parameterIndex, reader, length);
}
public void setRef(int i, Ref x) throws SQLException {
pstmt.setRef(i, x);
}
public void setBlob(int i, Blob x) throws SQLException {
pstmt.setBlob(i, x);
}
public void setClob(int i, Clob x) throws SQLException {
pstmt.setClob(i, x);
}
public void setArray(int i, Array x) throws SQLException {
pstmt.setArray(i, x);
}
public ResultSetMetaData getMetaData() throws SQLException {
return pstmt.getMetaData();
}
public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException {
pstmt.setDate(parameterIndex, x, cal);
}
public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException {
pstmt.setTime(parameterIndex, x, cal);
}
public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException {
pstmt.setTimestamp(parameterIndex, x, cal);
}
public void setNull(int paramIndex, int sqlType, String typeName) throws SQLException {
pstmt.setNull(paramIndex, sqlType, typeName);
}
public void setURL(int parameterIndex, URL x) throws SQLException {
pstmt.setURL(parameterIndex, x);
}
public ParameterMetaData getParameterMetaData() throws SQLException {
return pstmt.getParameterMetaData();
}
}
......@@ -12,13 +12,14 @@
package org.jivesoftware.database;
import java.sql.*;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.*;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
/**
* Wraps a Connection object and collects statistics about the database queries
* that are performed.<p>
* <p/>
* that are performed.<p/>
*
* Statistics of the profiled Connections can be obtained from the static
* methods of this class. Instances of this class are the actual wrappers that
* perform profiling.
......@@ -27,25 +28,28 @@ import java.util.Hashtable;
*/
public class ProfiledConnection extends AbstractConnection {
/**
* Constant for SELECT database queries.
*/
public static final int SELECT = 0;
public enum Type {
/**
* Constant for UPDATE database queries.
*/
public static final int UPDATE = 1;
/**
* SELECT database queries.
*/
select,
/**
* Constant for INSERT database queries.
*/
public static final int INSERT = 2;
/**
* UPDATE database queries.
*/
update,
/**
* Constant for DELETE database queries.
*/
public static final int DELETE = 3;
/**
* INSERT database queries.
*/
insert,
/**
* DLETE database queries.
*/
delete
}
private static long startInsertTime = 0;
private static long startUpdateTime = 0;
......@@ -67,10 +71,14 @@ public class ProfiledConnection extends AbstractConnection {
private static long totalSelectTime = 0;
private static long totalDeleteTime = 0;
private static Hashtable insertQueries = new Hashtable();
private static Hashtable updateQueries = new Hashtable();
private static Hashtable selectQueries = new Hashtable();
private static Hashtable deleteQueries = new Hashtable();
private static Map<String, ProfiledConnectionEntry> insertQueries =
new Hashtable<String, ProfiledConnectionEntry>();
private static Map<String, ProfiledConnectionEntry> updateQueries =
new Hashtable<String, ProfiledConnectionEntry>();
private static Map<String, ProfiledConnectionEntry> selectQueries =
new Hashtable<String, ProfiledConnectionEntry>();
private static Map<String, ProfiledConnectionEntry> deleteQueries =
new Hashtable<String, ProfiledConnectionEntry>();
/**
* Start profiling.
......@@ -89,21 +97,22 @@ public class ProfiledConnection extends AbstractConnection {
/**
* Returns the total number database queries of a particular type performed.
* Valid types are ProfiledConnection.SELECT, ProfiledConnection.UPDATE,
* ProfiledConnection.INSERT, and ProfiledConnection.DELETE.
* Valid types are {@link ProfiledConnection.Type#select},
* {@link ProfiledConnection.Type#update}, {@link ProfiledConnection.Type#insert},
* and {@link ProfiledConnection.Type#delete}
*
* @param type the type of query to get the count for.
* @return the number queries of type <tt>type</tt> performed.
*/
public static long getQueryCount(int type) {
public static long getQueryCount(Type type) {
switch (type) {
case SELECT:
case select:
return selectCount;
case UPDATE:
case update:
return updateCount;
case INSERT:
case insert:
return insertCount;
case DELETE:
case delete:
return deleteCount;
default:
throw new IllegalArgumentException("Invalid type");
......@@ -111,10 +120,13 @@ public class ProfiledConnection extends AbstractConnection {
}
/**
* @param sql the insert sql string.
* Adds a query to the list of those that have been run.
*
* @param type the query type.
* @param sql the insert sql string.
* @param time the length of time the query took in milliseconds
*/
public static void addQuery(int type, String sql, long time) {
public static void addQuery(Type type, String sql, long time) {
// Do nothing if we didn't receive a sql statement
if (sql == null || sql.equals("")) {
return;
......@@ -126,39 +138,39 @@ public class ProfiledConnection extends AbstractConnection {
// remove values from query
sql = removeQueryValues(sql);
ProfiledConnectionEntry entry = null;
ProfiledConnectionEntry entry;
switch (type) {
case SELECT:
case select:
selectCount++;
totalSelectTime += time;
entry = (ProfiledConnectionEntry)selectQueries.get(sql);
entry = selectQueries.get(sql);
if (entry == null) {
entry = new ProfiledConnectionEntry(sql);
selectQueries.put(sql, entry);
}
break;
case UPDATE:
case update:
updateCount++;
totalUpdateTime += time;
entry = (ProfiledConnectionEntry)updateQueries.get(sql);
entry = updateQueries.get(sql);
if (entry == null) {
entry = new ProfiledConnectionEntry(sql);
updateQueries.put(sql, entry);
}
break;
case INSERT:
case insert:
insertCount++;
totalInsertTime += time;
entry = (ProfiledConnectionEntry)insertQueries.get(sql);
entry = insertQueries.get(sql);
if (entry == null) {
entry = new ProfiledConnectionEntry(sql);
insertQueries.put(sql, entry);
}
break;
case DELETE:
case delete:
deleteCount++;
totalDeleteTime += time;
entry = (ProfiledConnectionEntry)deleteQueries.get(sql);
entry = deleteQueries.get(sql);
if (entry == null) {
entry = new ProfiledConnectionEntry(sql);
deleteQueries.put(sql, entry);
......@@ -182,26 +194,26 @@ public class ProfiledConnection extends AbstractConnection {
* @return the average number of queries of a certain typed performed per
* second.
*/
public static double getQueriesPerSecond(int type) {
public static double getQueriesPerSecond(Type type) {
long count, start, end;
switch (type) {
case SELECT:
case select:
count = selectCount;
start = startSelectTime;
end = endSelectTime;
break;
case UPDATE:
case update:
count = updateCount;
start = startUpdateTime;
end = endUpdateTime;
break;
case INSERT:
case insert:
count = insertCount;
start = startInsertTime;
end = endInsertTime;
break;
case DELETE:
case delete:
count = deleteCount;
start = startDeleteTime;
end = endDeleteTime;
......@@ -232,23 +244,23 @@ public class ProfiledConnection extends AbstractConnection {
* @return a double representing the average time spent executing the type
* of query.
*/
public static double getAverageQueryTime(int type) {
public static double getAverageQueryTime(Type type) {
long time, count;
switch (type) {
case SELECT:
case select:
count = selectCount;
time = totalSelectTime;
break;
case UPDATE:
case update:
count = updateCount;
time = totalUpdateTime;
break;
case INSERT:
case insert:
count = insertCount;
time = totalInsertTime;
break;
case DELETE:
case delete:
count = deleteCount;
time = totalDeleteTime;
break;
......@@ -273,15 +285,15 @@ public class ProfiledConnection extends AbstractConnection {
* @return the number of milliseconds spent executing the specified type of
* query.
*/
public static long getTotalQueryTime(int type) {
public static long getTotalQueryTime(Type type) {
switch (type) {
case SELECT:
case select:
return totalSelectTime;
case UPDATE:
case update:
return totalUpdateTime;
case INSERT:
case insert:
return totalInsertTime;
case DELETE:
case delete:
return totalDeleteTime;
default:
throw new IllegalArgumentException("Invalid type");
......@@ -291,46 +303,38 @@ public class ProfiledConnection extends AbstractConnection {
/**
* Returns an array of sorted queries (as ProfiledConnectionEntry objects) by type
*
* @param type the type of query to check
* @param type the type of query to check
* @param sortByTime sort the resulting list by Time if true,
* otherwise sort by count if false (default)
* otherwise sort by count if false (default)
* @return an array of ProfiledConnectionEntry objects
*/
public static ProfiledConnectionEntry[] getSortedQueries(int type, boolean sortByTime) {
Hashtable queries;
public static ProfiledConnectionEntry[] getSortedQueries(Type type, boolean sortByTime) {
Map<String, ProfiledConnectionEntry> queries;
switch (type) {
case SELECT:
case select:
queries = selectQueries;
break;
case UPDATE:
case update:
queries = updateQueries;
break;
case INSERT:
case insert:
queries = insertQueries;
break;
case DELETE:
case delete:
queries = deleteQueries;
break;
default:
throw new IllegalArgumentException("Invalid type");
}
ProfiledConnectionEntry[] result = new ProfiledConnectionEntry[queries.size()];
// no queries, return null set
if (queries.size() < 1) {
// No queries, return null
if (queries.isEmpty()) {
return null;
}
// since the values of the hashtable contain everything that
// we need (including the sql statement), ignore the keys
Enumeration e = queries.elements();
int c = 0;
while (e.hasMoreElements()) {
result[c++] = (ProfiledConnectionEntry)e.nextElement();
}
ProfiledConnectionEntry [] result = queries.values().toArray(
new ProfiledConnectionEntry[queries.size()]);
quickSort(result, sortByTime, 0, result.length - 1);
return result;
......@@ -352,10 +356,10 @@ public class ProfiledConnection extends AbstractConnection {
}
/**
* @param entries entries
* @param entries entries
* @param sortByTime sort by time if true, otherwise sort by count
* @param first first index to sort on. Normally 0
* @param last last index to sort on. Normally length -1
* @param first first index to sort on. Normally 0
* @param last last index to sort on. Normally length -1
*/
private static void quickSort(ProfiledConnectionEntry[] entries, boolean sortByTime, int first, int last) {
......@@ -596,528 +600,350 @@ public class ProfiledConnection extends AbstractConnection {
public Statement createStatement() throws SQLException {
// Returned a TimedStatement so that we can do db timings.
return new TimedStatement(connection.createStatement());
return (Statement)TimedStatement.newInstance(connection.createStatement());
}
public PreparedStatement prepareStatement(String sql) throws SQLException {
// Returned a TimedPreparedStatement so that we can do db timings.
return new TimedPreparedStatement(connection.prepareStatement(sql), sql);
return (PreparedStatement)TimedPreparedStatement.newInstance(
connection.prepareStatement(sql), sql);
}
public Statement createStatement(int resultSetType, int resultSetConcurrency)
throws SQLException {
return new TimedStatement(connection.createStatement(resultSetType,
throws SQLException
{
return (Statement)TimedStatement.newInstance(connection.createStatement(resultSetType,
resultSetConcurrency));
}
public PreparedStatement prepareStatement(String sql, int resultSetType,
int resultSetConcurrency) throws SQLException {
return new TimedPreparedStatement(connection.prepareStatement(sql, resultSetType, resultSetConcurrency), sql);
return (PreparedStatement)TimedPreparedStatement.newInstance(
connection.prepareStatement(sql, resultSetType, resultSetConcurrency), sql);
}
public CallableStatement prepareCall(String sql) throws SQLException {
return new TimedCallableStatement(connection.prepareCall(sql), sql);
return (CallableStatement)TimedCallableStatement.newInstance(
connection.prepareCall(sql), sql);
}
public CallableStatement prepareCall(String sql, int i, int i1) throws SQLException {
return new TimedCallableStatement(connection.prepareCall(sql, i, i1), sql);
public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency) throws SQLException {
return (CallableStatement)TimedCallableStatement.newInstance(
connection.prepareCall(sql, resultSetType, resultSetConcurrency), sql);
}
/**
* An implementation of the Statement interface that wraps an underlying
* Statement object and performs timings of the database queries. The class
* A dynamic proxy for the Statement interface that times usage. The class
* does not handle batch queries but should generally work otherwise.
*/
class TimedStatement extends StatementWrapper {
private Statement stmt;
public static class TimedStatement implements InvocationHandler {
/**
* Creates a new TimedStatement that wraps <tt>stmt</tt>.
*/
public TimedStatement(Statement stmt) {
super(stmt);
this.stmt = stmt;
}
public boolean execute(String sql) throws SQLException {
long t1 = System.currentTimeMillis();
boolean result = stmt.execute(sql);
long t2 = System.currentTimeMillis();
// determine the type of query
String sqlL = sql.toLowerCase().trim();
// Preloaded Method objects that we override.
private static Method execute;
private static Method executeQuery;
private static Method executeUpdate;
if (sqlL.startsWith("insert")) {
addQuery(INSERT, sql, t2 - t1);
}
else if (sqlL.startsWith("update")) {
addQuery(UPDATE, sql, t2 - t1);
static {
try {
execute = Statement.class.getMethod("execute", String.class);
executeQuery = Statement.class.getMethod("executeQuery", String.class);
executeUpdate = Statement.class.getMethod("executeUpdate", String.class);
}
else if (sqlL.startsWith("delete")) {
addQuery(DELETE, sql, t2 - t1);
}
else {
addQuery(SELECT, sql, t2 - t1);
catch (NoSuchMethodException e) {
throw new NoSuchMethodError(e.getMessage());
}
return result;
}
public ResultSet executeQuery(String sql) throws SQLException {
long t1 = System.currentTimeMillis();
ResultSet result = stmt.executeQuery(sql);
long t2 = System.currentTimeMillis();
public static Object newInstance(Statement stmt) {
return java.lang.reflect.Proxy.newProxyInstance(
stmt.getClass().getClassLoader(),
stmt.getClass().getInterfaces(),
new TimedStatement(stmt));
}
// determine the type of query
String sqlL = sql.toLowerCase().trim();
private Statement stmt;
if (sqlL.startsWith("insert")) {
addQuery(INSERT, sql, t2 - t1);
}
else if (sqlL.startsWith("update")) {
addQuery(UPDATE, sql, t2 - t1);
}
else if (sqlL.startsWith("delete")) {
addQuery(DELETE, sql, t2 - t1);
}
else {
addQuery(SELECT, sql, t2 - t1);
}
return result;
/**
* Creates a new TimedStatement dynamic proxy.
*
* @param stmt the Statement to proxy.
*/
private TimedStatement(Statement stmt) {
this.stmt = stmt;
}
public int executeUpdate(String sql) throws SQLException {
long t1 = System.currentTimeMillis();
int result = stmt.executeUpdate(sql);
long t2 = System.currentTimeMillis();
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.equals(execute) || method.equals(executeQuery) || method.equals(executeUpdate))
{
long t1 = System.currentTimeMillis();
Object result = method.invoke(stmt, args);
long t2 = System.currentTimeMillis();
// determine the type of query
String sqlL = sql.toLowerCase().trim();
String sql = ((String)args[0]).toLowerCase().trim();
if (sqlL.startsWith("insert")) {
addQuery(INSERT, sql, t2 - t1);
}
else if (sqlL.startsWith("update")) {
addQuery(UPDATE, sql, t2 - t1);
}
else if (sqlL.startsWith("delete")) {
addQuery(DELETE, sql, t2 - t1);
}
else {
addQuery(SELECT, sql, t2 - t1);
if (sql.startsWith("insert")) {
addQuery(Type.insert, sql, t2 - t1);
}
else if (sql.startsWith("update")) {
addQuery(Type.update, sql, t2 - t1);
}
else if (sql.startsWith("delete")) {
addQuery(Type.delete, sql, t2 - t1);
}
else {
addQuery(Type.select, sql, t2 - t1);
}
return result;
}
return result;
// Invoke the method normally if all else fails.
return method.invoke(stmt, args);
}
}
/**
* An implementation of the PreparedStatement interface that wraps an
* underlying PreparedStatement object and performs timings of the database
* queries.
* A dynamic proxy for the PreparedStatement interface that times usage.
*/
class TimedPreparedStatement extends PreparedStatementWrapper {
private String sql;
private int type = SELECT;
public static class TimedPreparedStatement implements InvocationHandler {
public TimedPreparedStatement(PreparedStatement pstmt, String sql) {
super(pstmt);
this.sql = sql;
// determine the type of query
String sqlL = sql.toLowerCase().trim();
// Preloaded Method objects that we override.
private static Method execute;
private static Method executeWithParam;
private static Method executeQuery;
private static Method executeQueryWithParam;
private static Method executeUpdate;
private static Method executeUpdateWithParam;
private static Method executeBatch;
if (sqlL.startsWith("insert")) {
type = INSERT;
static {
try {
execute = PreparedStatement.class.getMethod("execute");
executeWithParam = PreparedStatement.class.getMethod("execute", String.class);
executeQuery = PreparedStatement.class.getMethod("executeQuery");
executeQueryWithParam = PreparedStatement.class.getMethod("executeQuery", String.class);
executeUpdate = PreparedStatement.class.getMethod("executeUpdate");
executeUpdateWithParam = PreparedStatement.class.getMethod("executeUpdate", String.class);
executeBatch = PreparedStatement.class.getMethod("executeBatch");
}
else if (sqlL.startsWith("update")) {
type = UPDATE;
}
else if (sqlL.startsWith("delete")) {
type = DELETE;
}
else {
type = SELECT;
catch (NoSuchMethodException e) {
throw new NoSuchMethodError(e.getMessage());
}
}
public boolean execute() throws SQLException {
// Perform timing of this method.
long t1 = System.currentTimeMillis();
boolean result = pstmt.execute();
long t2 = System.currentTimeMillis();
switch (type) {
case SELECT:
addQuery(SELECT, sql, t2 - t1);
break;
case UPDATE:
addQuery(UPDATE, sql, t2 - t1);
break;
case INSERT:
addQuery(INSERT, sql, t2 - t1);
break;
case DELETE:
addQuery(DELETE, sql, t2 - t1);
break;
}
return result;
public static Object newInstance(PreparedStatement pstmt, String sql) {
return java.lang.reflect.Proxy.newProxyInstance(
pstmt.getClass().getClassLoader(),
pstmt.getClass().getInterfaces(),
new TimedPreparedStatement(pstmt, sql));
}
/*
* This is one of the methods that we wish to time
*/
public ResultSet executeQuery() throws SQLException {
long t1 = System.currentTimeMillis();
ResultSet result = pstmt.executeQuery();
long t2 = System.currentTimeMillis();
switch (type) {
case SELECT:
addQuery(SELECT, sql, t2 - t1);
break;
case UPDATE:
addQuery(UPDATE, sql, t2 - t1);
break;
case INSERT:
addQuery(INSERT, sql, t2 - t1);
break;
case DELETE:
addQuery(DELETE, sql, t2 - t1);
break;
}
return result;
}
private PreparedStatement pstmt;
private String sql;
private Type type = Type.select;
/*
* This is one of the methods that we wish to time
/**
* Creates a new TimedStatement dynamic proxy.
*
* @param pstmt the PreparedStatement to proxy.
* @param sql the SQL.
*/
public int executeUpdate() throws SQLException {
long t1 = System.currentTimeMillis();
int result = pstmt.executeUpdate();
long t2 = System.currentTimeMillis();
switch (type) {
case SELECT:
addQuery(SELECT, sql, t2 - t1);
break;
case UPDATE:
addQuery(UPDATE, sql, t2 - t1);
break;
case INSERT:
addQuery(INSERT, sql, t2 - t1);
break;
case DELETE:
addQuery(DELETE, sql, t2 - t1);
break;
}
return result;
}
// The following methods are from the Statement class - the
// SuperInterface of PreparedStatement
// without these this class won't compile
public boolean execute(String _sql) throws SQLException {
long t1 = System.currentTimeMillis();
boolean result = pstmt.execute(_sql);
long t2 = System.currentTimeMillis();
private TimedPreparedStatement(PreparedStatement pstmt, String sql) {
this.pstmt = pstmt;
this.sql = sql;
// determine the type of query
String sqlL = _sql.toLowerCase().trim();
// Determine the type of query
String sqlL = sql.toLowerCase().trim();
if (sqlL.startsWith("insert")) {
addQuery(INSERT, _sql, t2 - t1);
type = Type.insert;
}
else if (sqlL.startsWith("update")) {
addQuery(UPDATE, _sql, t2 - t1);
type = Type.update;
}
else if (sqlL.startsWith("delete")) {
addQuery(DELETE, _sql, t2 - t1);
type = Type.delete;
}
else {
addQuery(SELECT, _sql, t2 - t1);
type = Type.select;
}
return result;
}
public int[] executeBatch() throws SQLException {
long t1 = System.currentTimeMillis();
int[] result = pstmt.executeBatch();
long t2 = System.currentTimeMillis();
switch (type) {
case SELECT:
addQuery(SELECT, sql, t2 - t1);
break;
case UPDATE:
addQuery(UPDATE, sql, t2 - t1);
break;
case INSERT:
addQuery(INSERT, sql, t2 - t1);
break;
case DELETE:
addQuery(DELETE, sql, t2 - t1);
break;
}
return result;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.equals(execute) || method.equals(executeQuery) ||
method.equals(executeUpdate) || method.equals(executeBatch))
{
long t1 = System.currentTimeMillis();
Object result = method.invoke(pstmt, args);
long t2 = System.currentTimeMillis();
public ResultSet executeQuery(String _sql) throws SQLException {
long t1 = System.currentTimeMillis();
ResultSet result = pstmt.executeQuery(_sql);
long t2 = System.currentTimeMillis();
// determine the type of query
String sqlL = _sql.toLowerCase().trim();
switch (type) {
case select:
addQuery(Type.select, sql, t2 - t1);
break;
case update:
addQuery(Type.update, sql, t2 - t1);
break;
case insert:
addQuery(Type.insert, sql, t2 - t1);
break;
case delete:
addQuery(Type.delete, sql, t2 - t1);
break;
}
if (sqlL.startsWith("insert")) {
addQuery(INSERT, _sql, t2 - t1);
}
else if (sqlL.startsWith("update")) {
addQuery(UPDATE, _sql, t2 - t1);
}
else if (sqlL.startsWith("delete")) {
addQuery(DELETE, _sql, t2 - t1);
return result;
}
else {
addQuery(SELECT, _sql, t2 - t1);
}
return result;
}
else if (method.equals(executeWithParam) || method.equals(executeQueryWithParam) ||
method.equals(executeUpdateWithParam))
{
long t1 = System.currentTimeMillis();
Object result = method.invoke(pstmt, args);
long t2 = System.currentTimeMillis();
public int executeUpdate(String _sql) throws SQLException {
String sql = ((String)args[0]).toLowerCase().trim();
long t1 = System.currentTimeMillis();
int result = pstmt.executeUpdate(_sql);
long t2 = System.currentTimeMillis();
// determine the type of query
String sqlL = _sql.toLowerCase().trim();
if (sql.startsWith("insert")) {
addQuery(Type.insert, sql, t2 - t1);
}
else if (sql.startsWith("update")) {
addQuery(Type.update, sql, t2 - t1);
}
else if (sql.startsWith("delete")) {
addQuery(Type.delete, sql, t2 - t1);
}
else {
addQuery(Type.select, sql, t2 - t1);
}
if (sqlL.startsWith("insert")) {
addQuery(INSERT, _sql, t2 - t1);
}
else if (sqlL.startsWith("update")) {
addQuery(UPDATE, _sql, t2 - t1);
return result;
}
else if (sqlL.startsWith("delete")) {
addQuery(DELETE, _sql, t2 - t1);
}
else {
addQuery(SELECT, _sql, t2 - t1);
}
return result;
// Invoke the method normally if all else fails.
return method.invoke(pstmt, args);
}
}
/**
* An implementation of the CallableStatement interface that wraps an
* underlying CallableStatement object and performs timings of the database
* queries.
* A dynamic proxy for the CallableStatement interface that times usage.
*/
class TimedCallableStatement extends CallableStatementWrapper {
public static class TimedCallableStatement implements InvocationHandler {
private String sql;
private int type = SELECT;
public TimedCallableStatement(CallableStatement cstmt, String sql) {
super(cstmt);
this.sql = sql;
// determine the type of query
String sqlL = sql.toLowerCase().trim();
// Preloaded Method objects that we override.
private static Method execute;
private static Method executeWithParam;
private static Method executeQuery;
private static Method executeQueryWithParam;
private static Method executeUpdate;
private static Method executeUpdateWithParam;
private static Method executeBatch;
if (sqlL.startsWith("insert")) {
type = INSERT;
}
else if (sqlL.startsWith("update")) {
type = UPDATE;
}
else if (sqlL.startsWith("delete")) {
type = DELETE;
static {
try {
execute = CallableStatement.class.getMethod("execute");
executeWithParam = CallableStatement.class.getMethod("execute", String.class);
executeQuery = CallableStatement.class.getMethod("executeQuery");
executeQueryWithParam = CallableStatement.class.getMethod("executeQuery", String.class);
executeUpdate = CallableStatement.class.getMethod("executeUpdate");
executeUpdateWithParam = CallableStatement.class.getMethod("executeUpdate", String.class);
executeBatch = CallableStatement.class.getMethod("executeBatch");
}
else {
type = SELECT;
catch (NoSuchMethodException e) {
throw new NoSuchMethodError(e.getMessage());
}
}
public boolean execute() throws SQLException {
// Perform timing of this method.
long t1 = System.currentTimeMillis();
boolean result = cstmt.execute();
long t2 = System.currentTimeMillis();
switch (type) {
case SELECT:
addQuery(SELECT, sql, t2 - t1);
break;
case UPDATE:
addQuery(UPDATE, sql, t2 - t1);
break;
case INSERT:
addQuery(INSERT, sql, t2 - t1);
break;
case DELETE:
addQuery(DELETE, sql, t2 - t1);
break;
}
return result;
public static Object newInstance(CallableStatement cstmt, String sql) {
return java.lang.reflect.Proxy.newProxyInstance(
cstmt.getClass().getClassLoader(),
cstmt.getClass().getInterfaces(),
new TimedCallableStatement(cstmt, sql));
}
/*
* This is one of the methods that we wish to time
*/
public ResultSet executeQuery() throws SQLException {
long t1 = System.currentTimeMillis();
ResultSet result = cstmt.executeQuery();
long t2 = System.currentTimeMillis();
switch (type) {
case SELECT:
addQuery(SELECT, sql, t2 - t1);
break;
case UPDATE:
addQuery(UPDATE, sql, t2 - t1);
break;
case INSERT:
addQuery(INSERT, sql, t2 - t1);
break;
case DELETE:
addQuery(DELETE, sql, t2 - t1);
break;
}
return result;
}
private CallableStatement cstmt;
private String sql;
private Type type = Type.select;
/*
* This is one of the methods that we wish to time
/**
* Creates a new TimedStatement dynamic proxy.
*
* @param cstmt the CallableStatement to proxy.
* @param sql the SQL.
*/
public int executeUpdate() throws SQLException {
long t1 = System.currentTimeMillis();
int result = cstmt.executeUpdate();
long t2 = System.currentTimeMillis();
switch (type) {
case SELECT:
addQuery(SELECT, sql, t2 - t1);
break;
case UPDATE:
addQuery(UPDATE, sql, t2 - t1);
break;
case INSERT:
addQuery(INSERT, sql, t2 - t1);
break;
case DELETE:
addQuery(DELETE, sql, t2 - t1);
break;
}
return result;
}
// The following methods are from the Statement class - the
// SuperInterface of PreparedStatement
// without these this class won't compile
public boolean execute(String _sql) throws SQLException {
long t1 = System.currentTimeMillis();
boolean result = cstmt.execute(_sql);
long t2 = System.currentTimeMillis();
private TimedCallableStatement(CallableStatement cstmt, String sql) {
this.cstmt = cstmt;
this.sql = sql;
// determine the type of query
String sqlL = _sql.toLowerCase().trim();
// Determine the type of query
String sqlL = sql.toLowerCase().trim();
if (sqlL.startsWith("insert")) {
addQuery(INSERT, _sql, t2 - t1);
type = Type.insert;
}
else if (sqlL.startsWith("update")) {
addQuery(UPDATE, _sql, t2 - t1);
type = Type.update;
}
else if (sqlL.startsWith("delete")) {
addQuery(DELETE, _sql, t2 - t1);
type = Type.delete;
}
else {
addQuery(SELECT, _sql, t2 - t1);
}
return result;
}
public int[] executeBatch() throws SQLException {
long t1 = System.currentTimeMillis();
int[] result = cstmt.executeBatch();
long t2 = System.currentTimeMillis();
switch (type) {
case SELECT:
addQuery(SELECT, sql, t2 - t1);
break;
case UPDATE:
addQuery(UPDATE, sql, t2 - t1);
break;
case INSERT:
addQuery(INSERT, sql, t2 - t1);
break;
case DELETE:
addQuery(DELETE, sql, t2 - t1);
break;
type = Type.select;
}
return result;
}
public ResultSet executeQuery(String _sql) throws SQLException {
long t1 = System.currentTimeMillis();
ResultSet result = cstmt.executeQuery(_sql);
long t2 = System.currentTimeMillis();
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.equals(execute) || method.equals(executeQuery) ||
method.equals(executeUpdate) || method.equals(executeBatch))
{
long t1 = System.currentTimeMillis();
Object result = method.invoke(cstmt, args);
long t2 = System.currentTimeMillis();
// determine the type of query
String sqlL = _sql.toLowerCase().trim();
switch (type) {
case select:
addQuery(Type.select, sql, t2 - t1);
break;
case update:
addQuery(Type.update, sql, t2 - t1);
break;
case insert:
addQuery(Type.insert, sql, t2 - t1);
break;
case delete:
addQuery(Type.delete, sql, t2 - t1);
break;
}
if (sqlL.startsWith("insert")) {
addQuery(INSERT, _sql, t2 - t1);
}
else if (sqlL.startsWith("update")) {
addQuery(UPDATE, _sql, t2 - t1);
return result;
}
else if (sqlL.startsWith("delete")) {
addQuery(DELETE, _sql, t2 - t1);
}
else {
addQuery(SELECT, _sql, t2 - t1);
}
return result;
}
public int executeUpdate(String _sql) throws SQLException {
else if (method.equals(executeWithParam) || method.equals(executeQueryWithParam) ||
method.equals(executeUpdateWithParam))
{
long t1 = System.currentTimeMillis();
Object result = method.invoke(cstmt, args);
long t2 = System.currentTimeMillis();
long t1 = System.currentTimeMillis();
int result = cstmt.executeUpdate(_sql);
long t2 = System.currentTimeMillis();
String sql = ((String)args[0]).toLowerCase().trim();
// determine the type of query
String sqlL = _sql.toLowerCase().trim();
if (sql.startsWith("insert")) {
addQuery(Type.insert, sql, t2 - t1);
}
else if (sql.startsWith("update")) {
addQuery(Type.update, sql, t2 - t1);
}
else if (sql.startsWith("delete")) {
addQuery(Type.delete, sql, t2 - t1);
}
else {
addQuery(Type.select, sql, t2 - t1);
}
if (sqlL.startsWith("insert")) {
addQuery(INSERT, _sql, t2 - t1);
}
else if (sqlL.startsWith("update")) {
addQuery(UPDATE, _sql, t2 - t1);
return result;
}
else if (sqlL.startsWith("delete")) {
addQuery(DELETE, _sql, t2 - t1);
}
else {
addQuery(SELECT, _sql, t2 - t1);
}
return result;
// Invoke the method normally if all else fails.
return method.invoke(cstmt, args);
}
}
}
}
\ No newline at end of file
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2004 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.database;
import org.jivesoftware.util.Log;
import java.sql.*;
import java.lang.reflect.Method;
/**
* An implementation of the Statement interface that wraps an underlying
* Statement object.
*
* @author Gaston Dombiak
*/
public abstract class StatementWrapper implements Statement {
protected Statement stmt;
/**
* Creates a new StatementWrapper that wraps <tt>stmt</tt>.
*
* @param stmt the Statement.
*/
public StatementWrapper(Statement stmt) {
this.stmt = stmt;
}
public ResultSet executeQuery(String sql) throws SQLException {
return stmt.executeQuery(sql);
}
public int executeUpdate(String sql) throws SQLException {
return stmt.executeUpdate(sql);
}
public void close() throws SQLException {
stmt.close();
}
public int getMaxFieldSize() throws SQLException {
return stmt.getMaxFieldSize();
}
public void setMaxFieldSize(int max) throws SQLException {
stmt.setMaxFieldSize(max);
}
public int getMaxRows() throws SQLException {
return stmt.getMaxRows();
}
public void setMaxRows(int max) throws SQLException {
stmt.setMaxRows(max);
}
public void setEscapeProcessing(boolean enable) throws SQLException {
stmt.setEscapeProcessing(enable);
}
public int getQueryTimeout() throws SQLException {
return stmt.getQueryTimeout();
}
public void setQueryTimeout(int seconds) throws SQLException {
stmt.setQueryTimeout(seconds);
}
public void cancel() throws SQLException {
stmt.cancel();
}
public SQLWarning getWarnings() throws SQLException {
return stmt.getWarnings();
}
public void clearWarnings() throws SQLException {
stmt.clearWarnings();
}
public void setCursorName(String name) throws SQLException {
stmt.setCursorName(name);
}
public boolean execute(String sql) throws SQLException {
return stmt.execute(sql);
}
public ResultSet getResultSet() throws SQLException {
return stmt.getResultSet();
}
public int getUpdateCount() throws SQLException {
return stmt.getUpdateCount();
}
public boolean getMoreResults() throws SQLException {
return stmt.getMoreResults();
}
public void setFetchDirection(int direction) throws SQLException {
stmt.setFetchDirection(direction);
}
public int getFetchDirection() throws SQLException {
return stmt.getFetchDirection();
}
public void setFetchSize(int rows) throws SQLException {
stmt.setFetchSize(rows);
}
public int getFetchSize() throws SQLException {
return stmt.getFetchSize();
}
public int getResultSetConcurrency() throws SQLException {
return stmt.getResultSetConcurrency();
}
public int getResultSetType() throws SQLException {
return stmt.getResultSetType();
}
public void addBatch(String sql) throws SQLException {
stmt.addBatch(sql);
}
public void clearBatch() throws SQLException {
stmt.clearBatch();
}
public int[] executeBatch() throws SQLException {
return stmt.executeBatch();
}
public Connection getConnection() throws SQLException {
return stmt.getConnection();
}
public boolean getMoreResults(int current) throws SQLException {
return stmt.getMoreResults(current);
}
public ResultSet getGeneratedKeys() throws SQLException {
return stmt.getGeneratedKeys();
}
public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
return stmt.executeUpdate(sql, autoGeneratedKeys);
}
public int executeUpdate(String sql, int columnIndexes[]) throws SQLException {
return stmt.executeUpdate(sql, columnIndexes);
}
public int executeUpdate(String sql, String columnNames[]) throws SQLException {
return stmt.executeUpdate(sql, columnNames);
}
public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
return stmt.execute(sql, autoGeneratedKeys);
}
public boolean execute(String sql, int columnIndexes[]) throws SQLException {
return stmt.execute(sql, columnIndexes);
}
public boolean execute(String sql, String columnNames[]) throws SQLException {
return stmt.execute(sql, columnNames);
}
public int getResultSetHoldability() throws SQLException {
return stmt.getResultSetHoldability();
}
// JDK 1.6 Methods. We must handle these using reflection so that the code will compile on
// both JDK 1.6 and 1.5.
public boolean isClosed() throws SQLException {
try {
Method method = stmt.getClass().getMethod("isClosed");
return (Boolean)method.invoke(stmt);
}
catch (Exception e) {
if (e instanceof SQLException) {
throw (SQLException)e;
}
// Simply log reflection exceptions.
Log.error(e);
return false;
}
}
public void setPoolable(boolean poolable) throws SQLException {
try {
Method method = stmt.getClass().getMethod("setPoolable", Boolean.class);
method.invoke(stmt, poolable);
}
catch (Exception e) {
if (e instanceof SQLException) {
throw (SQLException)e;
}
// Simply log reflection exceptions.
Log.error(e);
}
}
public boolean isPoolable() throws SQLException {
try {
Method method = stmt.getClass().getMethod("isPoolable");
return (Boolean)method.invoke(stmt);
}
catch (Exception e) {
if (e instanceof SQLException) {
throw (SQLException)e;
}
// Simply log reflection exceptions.
Log.error(e);
return false;
}
}
public <T> T unwrap(Class<T> iface) throws SQLException {
try {
Method method = stmt.getClass().getMethod("unwrap", Class.class);
return (T)method.invoke(stmt, iface);
}
catch (Exception e) {
if (e instanceof SQLException) {
throw (SQLException)e;
}
// Simply log reflection exceptions.
Log.error(e);
return null;
}
}
public boolean isWrapperFor(Class<?> iface) throws SQLException {
try {
Method method = stmt.getClass().getMethod("isWrapperFor", Class.class);
return (Boolean)method.invoke(stmt, iface);
}
catch (Exception e) {
if (e instanceof SQLException) {
throw (SQLException)e;
}
// Simply log reflection exceptions.
Log.error(e);
return false;
}
}
}
\ No newline at end of file
......@@ -105,7 +105,7 @@
<h3><fmt:message key="server.db_stats.settings" /></h3>
<form action="server-db-stats.jsp">
<table cellpadding="3" cellspacing="1" border="0" width="730">
<table cellpadding="3" cellspacing="5" border="0">
<tr>
<td>
<fmt:message key="server.db_stats.refresh" />:
......@@ -139,33 +139,33 @@
</div>
<h3><fmt:message key="server.db_stats.select_stats" /></h3>
<b><fmt:message key="server.db_stats.select_stats" /></b>
<ul>
<table bgcolor="#aaaaaa" cellpadding="0" cellspacing="0" border="0" width="730">
<table bgcolor="#aaaaaa" cellpadding="0" cellspacing="0" border="0" width="600">
<tr><td>
<table bgcolor="#aaaaaa" cellpadding="3" cellspacing="1" border="0" width="100%">
<tr bgcolor="#ffffff">
<td><fmt:message key="server.db_stats.operations" /></td>
<td><%= intFormat.format(ProfiledConnection.getQueryCount(ProfiledConnection.SELECT)) %></td>
<td><%= intFormat.format(ProfiledConnection.getQueryCount(ProfiledConnection.Type.select)) %></td>
</tr>
<tr bgcolor="#ffffff">
<td><fmt:message key="server.db_stats.total_time" /></td>
<td><%= intFormat.format(ProfiledConnection.getTotalQueryTime(ProfiledConnection.SELECT)) %></td>
<td><%= intFormat.format(ProfiledConnection.getTotalQueryTime(ProfiledConnection.Type.select)) %></td>
</tr>
<tr bgcolor="#ffffff">
<td><fmt:message key="server.db_stats.avg_rate" /></td>
<td><%= decFormat.format(ProfiledConnection.getAverageQueryTime(ProfiledConnection.SELECT)) %></td>
<td><%= decFormat.format(ProfiledConnection.getAverageQueryTime(ProfiledConnection.Type.select)) %></td>
</tr>
<tr bgcolor="#ffffff">
<td><fmt:message key="server.db_stats.total_rate" /></td>
<td><%= decFormat.format(ProfiledConnection.getQueriesPerSecond(ProfiledConnection.SELECT)) %></td>
<td><%= decFormat.format(ProfiledConnection.getQueriesPerSecond(ProfiledConnection.Type.select)) %></td>
</tr>
<tr bgcolor="#ffffff">
<td><fmt:message key="server.db_stats.queries" /></td>
<td bgcolor="#ffffff"><%
ProfiledConnectionEntry[] list = ProfiledConnection.getSortedQueries(ProfiledConnection.SELECT, doSortByTime);
ProfiledConnectionEntry[] list = ProfiledConnection.getSortedQueries(ProfiledConnection.Type.select, doSortByTime);
if (list == null || list.length < 1) {
out.println(LocaleUtils.getLocalizedString("server.db_stats.no_queries"));
......@@ -180,7 +180,7 @@
<br />
<table bgcolor="#aaaaaa" cellpadding="0" cellspacing="0" border="0" width="730">
<table bgcolor="#aaaaaa" cellpadding="0" cellspacing="0" border="0" width="600">
<tr><td>
<table bgcolor="#aaaaaa" cellpadding="3" cellspacing="0" border="0" width="100%">
<tr bgcolor="#ffffff"><td>
......@@ -204,7 +204,7 @@
</td></tr>
</table>
</ul>
<b><fmt:message key="server.db_stats.insert_stats" /></b>
......@@ -215,24 +215,24 @@
<table bgcolor="#aaaaaa" cellpadding="3" cellspacing="1" border="0" width="100%">
<tr bgcolor="#ffffff">
<td><fmt:message key="server.db_stats.operations" /></td>
<td><%= intFormat.format(ProfiledConnection.getQueryCount(ProfiledConnection.INSERT)) %></td>
<td><%= intFormat.format(ProfiledConnection.getQueryCount(ProfiledConnection.Type.insert)) %></td>
</tr>
<tr bgcolor="#ffffff">
<td><fmt:message key="server.db_stats.total_time" /></td>
<td><%= intFormat.format(ProfiledConnection.getTotalQueryTime(ProfiledConnection.INSERT)) %></td>
<td><%= intFormat.format(ProfiledConnection.getTotalQueryTime(ProfiledConnection.Type.insert)) %></td>
</tr>
<tr bgcolor="#ffffff">
<td><fmt:message key="server.db_stats.avg_rate" /></td>
<td><%= decFormat.format(ProfiledConnection.getAverageQueryTime(ProfiledConnection.INSERT)) %></td>
<td><%= decFormat.format(ProfiledConnection.getAverageQueryTime(ProfiledConnection.Type.insert)) %></td>
</tr>
<tr bgcolor="#ffffff">
<td><fmt:message key="server.db_stats.total_rate" /></td>
<td><%= decFormat.format(ProfiledConnection.getQueriesPerSecond(ProfiledConnection.INSERT)) %></td>
<td><%= decFormat.format(ProfiledConnection.getQueriesPerSecond(ProfiledConnection.Type.insert)) %></td>
</tr>
<tr bgcolor="#ffffff">
<td><fmt:message key="server.db_stats.queries" /></td>
<td bgcolor="#ffffff"><%
list = ProfiledConnection.getSortedQueries(ProfiledConnection.INSERT, doSortByTime);
list = ProfiledConnection.getSortedQueries(ProfiledConnection.Type.insert, doSortByTime);
if (list == null || list.length < 1) {
out.println(LocaleUtils.getLocalizedString("server.db_stats.no_queries"));
......@@ -282,24 +282,24 @@
<table bgcolor="#aaaaaa" cellpadding="3" cellspacing="1" border="0" width="100%">
<tr bgcolor="#ffffff">
<td><fmt:message key="server.db_stats.operations" /></td>
<td><%= intFormat.format(ProfiledConnection.getQueryCount(ProfiledConnection.UPDATE)) %></td>
<td><%= intFormat.format(ProfiledConnection.getQueryCount(ProfiledConnection.Type.update)) %></td>
</tr>
<tr bgcolor="#ffffff">
<td><fmt:message key="server.db_stats.total_time" /></td>
<td><%= intFormat.format(ProfiledConnection.getTotalQueryTime(ProfiledConnection.UPDATE)) %></td>
<td><%= intFormat.format(ProfiledConnection.getTotalQueryTime(ProfiledConnection.Type.update)) %></td>
</tr>
<tr bgcolor="#ffffff">
<td><fmt:message key="server.db_stats.avg_rate" /></td>
<td><%= decFormat.format(ProfiledConnection.getAverageQueryTime(ProfiledConnection.UPDATE)) %></td>
<td><%= decFormat.format(ProfiledConnection.getAverageQueryTime(ProfiledConnection.Type.update)) %></td>
</tr>
<tr bgcolor="#ffffff">
<td><fmt:message key="server.db_stats.total_rate" /></td>
<td><%= decFormat.format(ProfiledConnection.getQueriesPerSecond(ProfiledConnection.UPDATE)) %></td>
<td><%= decFormat.format(ProfiledConnection.getQueriesPerSecond(ProfiledConnection.Type.update)) %></td>
</tr>
<tr bgcolor="#ffffff">
<td><fmt:message key="server.db_stats.queries" /></td>
<td bgcolor="#ffffff"><%
list = ProfiledConnection.getSortedQueries(ProfiledConnection.UPDATE, doSortByTime);
list = ProfiledConnection.getSortedQueries(ProfiledConnection.Type.update, doSortByTime);
if (list == null || list.length < 1) {
out.println(LocaleUtils.getLocalizedString("server.db_stats.no_queries"));
......@@ -349,24 +349,24 @@
<table bgcolor="#aaaaaa" cellpadding="3" cellspacing="1" border="0" width="100%">
<tr bgcolor="#ffffff">
<td><fmt:message key="server.db_stats.operations" /></td>
<td><%= intFormat.format(ProfiledConnection.getQueryCount(ProfiledConnection.DELETE)) %></td>
<td><%= intFormat.format(ProfiledConnection.getQueryCount(ProfiledConnection.Type.delete)) %></td>
</tr>
<tr bgcolor="#ffffff">
<td><fmt:message key="server.db_stats.total_time" /></td>
<td><%= intFormat.format(ProfiledConnection.getTotalQueryTime(ProfiledConnection.DELETE)) %></td>
<td><%= intFormat.format(ProfiledConnection.getTotalQueryTime(ProfiledConnection.Type.delete)) %></td>
</tr>
<tr bgcolor="#ffffff">
<td><fmt:message key="server.db_stats.avg_rate" /></td>
<td><%= decFormat.format(ProfiledConnection.getAverageQueryTime(ProfiledConnection.DELETE)) %></td>
<td><%= decFormat.format(ProfiledConnection.getAverageQueryTime(ProfiledConnection.Type.delete)) %></td>
</tr>
<tr bgcolor="#ffffff">
<td><fmt:message key="server.db_stats.total_rate" /></td>
<td><%= decFormat.format(ProfiledConnection.getQueriesPerSecond(ProfiledConnection.DELETE)) %></td>
<td><%= decFormat.format(ProfiledConnection.getQueriesPerSecond(ProfiledConnection.Type.delete)) %></td>
</tr>
<tr bgcolor="#ffffff">
<td><fmt:message key="server.db_stats.queries" /></td>
<td bgcolor="#ffffff"><%
list = ProfiledConnection.getSortedQueries(ProfiledConnection.DELETE, doSortByTime);
list = ProfiledConnection.getSortedQueries(ProfiledConnection.Type.delete, doSortByTime);
if (list == null || list.length < 1) {
out.println(LocaleUtils.getLocalizedString("server.db_stats.no_queries"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment