1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.commsen.stopwatch.storages;
18
19 import java.sql.SQLException;
20 import java.sql.Statement;
21
22 import org.apache.log4j.Logger;
23
24 import com.commsen.stopwatch.Stopwatch;
25 import com.commsen.stopwatch.StopwatchStorageException;
26
27 /***
28 * TODO Dokumentacja
29 *
30 * @author Milen Dyankov
31 *
32 */
33 public class DefaultHSQLStorage extends AbstractDatabaseStorage {
34
35 /***
36 * Logger for this class
37 */
38 private static final Logger log = Logger.getLogger(DefaultHSQLStorage.class);
39
40 private String DB_DRIVER = "db.driver";
41 private String DB_CONNECTION = "db.connectionString";
42 private String DB_USER = "db.user";
43 private String DB_PASSWORD = "db.password";
44 private String DB_TABLE = "db.tableName";
45
46
47
48 protected String getDriver() {
49 return properties.getProperty(DB_DRIVER, "org.hsqldb.jdbcDriver");
50 }
51
52
53
54
55 protected String getConnectionString() {
56 return properties.getProperty(DB_CONNECTION, "jdbc:hsqldb:hsql://localhost:9001/stopwatch");
57 }
58
59
60
61 protected String getUser() {
62 return properties.getProperty(DB_USER, "sa");
63 }
64
65
66
67 protected String getPassword() {
68 return properties.getProperty(DB_PASSWORD, "");
69 }
70
71
72 protected String getLastIdentityQuery() {
73 return "CALL IDENTITY()";
74 }
75
76
77
78 protected String getTableName() {
79 return Stopwatch.getProperty(DB_TABLE, "stopwatch");
80 }
81
82
83 protected String getReturnColumns() {
84 return " count(1), " + " min (DATEDIFF('ms', _start, _end)) as minTime," + " max (DATEDIFF('ms', _start, _end)) as maxTime," + " avg (DATEDIFF('ms', _start, _end)) as avgTime,"
85 + " sum (DATEDIFF('ms', _start, _end)) as totalTime ";
86 }
87
88 private boolean debugEnabled;
89
90
91 /***
92 * @see com.commsen.stopwatch.StopwatchStorage#freeze()
93 */
94 public void freeze() throws StopwatchStorageException {
95 Statement statement = null;
96 try {
97 statement = updateConnection.createStatement();
98 statement.execute("delete from " + getTableName() + " where _end is NULL");
99 } catch (SQLException e) {
100 getLogger().error(e, e);
101 } finally {
102 if (statement != null) {
103 try {
104 statement.close();
105 } catch (SQLException e) {
106 getLogger().error(e, e);
107 }
108 }
109 }
110 }
111
112
113 /***
114 *
115 * @throws StopwatchStorageException
116 * @see com.commsen.stopwatch.StopwatchStorage#close()
117 */
118 public void close() throws StopwatchStorageException {
119 Statement statement = null;
120 try {
121 statement = updateConnection.createStatement();
122 statement.execute("drop table " + getTableName());
123 } catch (SQLException e) {
124 throw new StopwatchStorageException("database error", e);
125 } finally {
126 if (statement != null) {
127 try {
128 statement.close();
129 } catch (SQLException e) {
130 throw new StopwatchStorageException("database error", e);
131 }
132 }
133 }
134 super.close();
135 }
136
137
138 protected Logger getLogger() {
139 return log;
140 }
141
142
143 /***
144 * @return Returns the debugEnabled.
145 */
146 public boolean isDebugEnabled() {
147 return debugEnabled;
148 }
149
150
151 /***
152 * @param debugEnabled The debugEnabled to set.
153 */
154 public void setDebugEnabled(boolean debugEnabled) {
155 this.debugEnabled = debugEnabled;
156 }
157
158 }