01: // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
02:
03: package jodd.db;
04:
05: import java.sql.Connection;
06: import java.sql.ResultSet;
07:
08: /**
09: * Profiled {@link DbQuery} measures query execution time. May be used for debugging purposes.
10: */
11: public class DbProfiledQuery extends DbQuery {
12:
13: public DbProfiledQuery(Connection conn, String sql) {
14: super (conn, sql);
15: }
16:
17: public DbProfiledQuery(Connection conn, String sqlString,
18: DbQueryMode mode) {
19: super (conn, sqlString, mode);
20: }
21:
22: public DbProfiledQuery(DbSession session, String sqlString,
23: DbQueryMode mode) {
24: super (session, sqlString, mode);
25: }
26:
27: public DbProfiledQuery(DbSession session, String sqlString) {
28: super (session, sqlString);
29: }
30:
31: public DbProfiledQuery(String sqlString, DbQueryMode mode) {
32: super (sqlString, mode);
33: }
34:
35: public DbProfiledQuery(String sqlString) {
36: super (sqlString);
37: }
38:
39: // ---------------------------------------------------------------- profile
40:
41: long start;
42: long elapsed = -1;
43:
44: @Override
45: public int executeUpdate() {
46: start = System.currentTimeMillis();
47: int result = super .executeUpdate();
48: elapsed = System.currentTimeMillis() - start;
49: return result;
50: }
51:
52: @Override
53: public ResultSet execute() {
54: start = System.currentTimeMillis();
55: ResultSet result = super .execute();
56: elapsed = System.currentTimeMillis() - start;
57: return result;
58: }
59:
60: /**
61: * Returns query execution elapsed time in ms.
62: * Returns <code>-1</code> if query is still not executed.
63: */
64: public long getExecutionElapsedTime() {
65: return elapsed;
66: }
67:
68: // ---------------------------------------------------------------- toString
69:
70: @Override
71: public String getQueryString() {
72: StringBuilder result = new StringBuilder(super .getQueryString());
73: if (elapsed != -1) {
74: result.append("\nExecution time: ").append(elapsed).append(
75: "ms.");
76: }
77: return result.toString();
78: }
79:
80: }
|