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.PreparedStatement;
20 import java.sql.ResultSet;
21 import java.sql.SQLException;
22 import java.sql.Timestamp;
23 import java.util.ArrayList;
24
25 import org.apache.log4j.Logger;
26
27 import com.commsen.stopwatch.Report;
28 import com.commsen.stopwatch.StopwatchStorageException;
29 import com.commsen.stopwatch.reports.MemoryStopwatchReport;
30
31 /***
32 * TODO Dokumentacja
33 *
34 * @author Milen Dyankov
35 *
36 */
37 public class MemoryHSQLStorage extends DefaultHSQLStorage {
38
39 /***
40 * Logger for this class
41 */
42 private static final Logger log = Logger.getLogger(MemoryHSQLStorage.class);
43
44
45 /***
46 * @see com.commsen.stopwatch.storages.AbstractDatabaseStorage#getTableName()
47 */
48 protected String getTableName() {
49 return "memory_stopwatch";
50 }
51
52
53 protected String getCreateTableQuery() {
54 return " create table " + getTableName() + " (" + " _id INT GENERATED BY DEFAULT AS IDENTITY, " + " _group VARCHAR, " + " _label VARCHAR, " + " _start TIMESTAMP, "
55 + " _end TIMESTAMP, " + " _start_mem int, " + " _end_mem int " + ")";
56 }
57
58
59 protected String getReturnColumns() {
60 return " count(1), " +
61 " min (DATEDIFF('ms', _start, _end)), " +
62 " max (DATEDIFF('ms', _start, _end)), " +
63 " avg (DATEDIFF('ms', _start, _end)), " +
64 " sum (DATEDIFF('ms', _start, _end)), " +
65 " min (_end_mem - _start_mem), " +
66 " max (_end_mem - _start_mem), " +
67 " avg (_end_mem - _start_mem) ";
68 }
69
70
71 public String getInsertQuery() {
72 return "insert into " + getTableName() + " (_group, _label, _start, _start_mem) values (?, ?, ?, ?)";
73 }
74
75
76 protected String getUpdateQuery() {
77 return "update " + getTableName() + " set _end = ?, _end_mem = ? where _id = ? and _end IS NULL";
78 }
79
80
81 /***
82 * @see com.commsen.stopwatch.StopwatchStorage#newRecord(java.lang.Object[])
83 */
84 public long newRecord(Object[] parameters) throws StopwatchStorageException {
85 if (insertPreparedStatement == null) return -1;
86 try {
87 synchronized (insertPreparedStatement.getConnection()) {
88 insertPreparedStatement.setString(1, (String) parameters[0]);
89 insertPreparedStatement.setString(2, (String) parameters[1]);
90 insertPreparedStatement.setTimestamp(3, new Timestamp(((Long) parameters[2]).longValue()));
91 insertPreparedStatement.setLong(4, ((Long) parameters[3]).longValue());
92 insertPreparedStatement.executeUpdate();
93 ResultSet resultSet = lastIdentityStatement.executeQuery();
94 resultSet.next();
95 long result = resultSet.getLong(1);
96 resultSet.close();
97 return result;
98 }
99 } catch (SQLException e) {
100 throw new StopwatchStorageException("database error", e);
101 }
102 }
103
104
105 /***
106 * @see com.commsen.stopwatch.StopwatchStorage#completeRecord(long, Object[])
107 */
108 public boolean completeRecord(long id, Object[] parameters) throws StopwatchStorageException {
109 if (id < 0) return false;
110 try {
111 synchronized (updatePreparedStatement.getConnection()) {
112 updatePreparedStatement.setTimestamp(1, new Timestamp(((Long) parameters[0]).longValue()));
113 updatePreparedStatement.setLong(2, ((Long) parameters[1]).longValue());
114 updatePreparedStatement.setLong(3, id);
115 updatePreparedStatement.executeUpdate();
116 return true;
117 }
118 } catch (SQLException e) {
119 throw new StopwatchStorageException("database error", e);
120 }
121
122 }
123
124
125 /***
126 *
127 * @param preparedStatement
128 * @return array of reports
129 * @throws SQLException
130 */
131 protected Report[] prepareReports(PreparedStatement preparedStatement, Object[] params) throws SQLException {
132 if (preparedStatement == null) return new Report[0];
133 ArrayList list = new ArrayList();
134 synchronized (preparedStatement.getConnection()) {
135
136 if (params != null && params.length > 0) {
137 for (int i = 0; i < params.length; i++) {
138 preparedStatement.setObject(i + 1, params[i]);
139 }
140 }
141
142 ResultSet resultSet = preparedStatement.executeQuery();
143 while (resultSet.next()) {
144 list.add(new MemoryStopwatchReport(resultSet.getString(1), resultSet.getString(2), resultSet.getLong(3), resultSet.getLong(4), resultSet.getLong(5), resultSet.getLong(6), resultSet
145 .getLong(7), resultSet.getLong(8), resultSet.getLong(9), resultSet.getLong(10)
146
147 ));
148 }
149 resultSet.close();
150 }
151 return (Report[]) list.toArray(new Report[list.size()]);
152 }
153
154
155 protected Logger getLogger() {
156 return log;
157 }
158 }