KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > db > DbProfiledQuery


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.db;
4
5 import java.sql.Connection JavaDoc;
6 import java.sql.ResultSet JavaDoc;
7
8 /**
9  * 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 JavaDoc conn, String JavaDoc sql) {
14         super(conn, sql);
15     }
16
17     public DbProfiledQuery(Connection JavaDoc conn, String JavaDoc sqlString, DbQueryMode mode) {
18         super(conn, sqlString, mode);
19     }
20
21     public DbProfiledQuery(DbSession session, String JavaDoc sqlString, DbQueryMode mode) {
22         super(session, sqlString, mode);
23     }
24
25     public DbProfiledQuery(DbSession session, String JavaDoc sqlString) {
26         super(session, sqlString);
27     }
28
29     public DbProfiledQuery(String JavaDoc sqlString, DbQueryMode mode) {
30         super(sqlString, mode);
31     }
32
33     public DbProfiledQuery(String JavaDoc sqlString) {
34         super(sqlString);
35     }
36
37     // ---------------------------------------------------------------- profile
38

39     long start;
40     long elapsed = -1;
41
42     public int executeUpdate() {
43         start = System.currentTimeMillis();
44         int result = super.executeUpdate();
45         elapsed = System.currentTimeMillis() - start;
46         return result;
47     }
48
49     public ResultSet JavaDoc execute() {
50         start = System.currentTimeMillis();
51         ResultSet JavaDoc result = super.execute();
52         elapsed = System.currentTimeMillis() - start;
53         return result;
54     }
55
56     /**
57      * Returns query execution elapsed time in ms.
58      * Returns <code>-1</code> if query is still not executed.
59      */

60     public long getExecutionElapsedTime() {
61         return elapsed;
62     }
63
64     // ---------------------------------------------------------------- toString
65

66
67     public String JavaDoc getQueryString() {
68         StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.getQueryString());
69         if (elapsed != -1) {
70             result.append("\nExecution time: ").append(elapsed).append("ms.");
71         }
72         return result.toString();
73     }
74
75 }
76
Popular Tags