View Javadoc

1   /*
2    * $Id: MemoryHSQLInMemoryStorage.java,v 1.1 2006/03/06 11:30:53 azzazzel Exp $
3    *
4    * Copyright 2006 Commsen International
5    * 
6    * Licensed under the Common Public License, Version 1.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * 
10   *      http://www.opensource.org/licenses/cpl1.0.txt
11   * 
12   * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS 
14   * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
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 MemoryHSQLInMemoryStorage extends DefaultHSQLInMemoryStorage {
38  
39  	/***
40  	 * Logger for this class
41  	 */
42  	private static final Logger log = Logger.getLogger(MemoryHSQLInMemoryStorage.class);
43  
44  
45  	protected String getTableName() {
46  		return "memory_stopwatch";
47  	}
48  
49  
50  	protected String getCreateTableQuery() {
51  		return " create table " + getTableName() + " (" + "   _id INT GENERATED BY DEFAULT AS IDENTITY, " + "   _group VARCHAR, " + "   _label VARCHAR, " + "   _start TIMESTAMP, "
52  		        + "   _end TIMESTAMP, " + "   _start_mem int, " + "   _end_mem int " + ")";
53  	}
54  
55  
56  	protected String getReturnColumns() {
57  		return "  count(1), " + // 3
58  		        "  min (DATEDIFF('ms', _start, _end)), " + // 4
59  		        "  max (DATEDIFF('ms', _start, _end)), " + // 5
60  		        "  avg (DATEDIFF('ms', _start, _end)), " + // 6
61  		        "  sum (DATEDIFF('ms', _start, _end)), " + // 7
62  		        "  min (_end_mem - _start_mem), " + // 8
63  		        "  max (_end_mem - _start_mem), " + // 9
64  		        "  avg (_end_mem - _start_mem) "; // 10
65  	}
66  
67  
68  	public String getInsertQuery() {
69  		return "insert into " + getTableName() + " (_group, _label, _start, _start_mem) values (?, ?, ?, ?)";
70  	}
71  
72  
73  	protected String getUpdateQuery() {
74  		return "update " + getTableName() + " set _end = ?, _end_mem = ? where _id = ? and _end IS NULL";
75  	}
76  
77  
78  	/***
79  	 * @see com.commsen.stopwatch.StopwatchStorage#newRecord(java.lang.Object[])
80  	 */
81  	public long newRecord(Object[] parameters) throws StopwatchStorageException {
82  		if (insertPreparedStatement == null) return -1;
83  		try {
84  			synchronized (insertPreparedStatement.getConnection()) {
85  				insertPreparedStatement.setString(1, (String) parameters[0]);
86  				insertPreparedStatement.setString(2, (String) parameters[1]);
87  				insertPreparedStatement.setTimestamp(3, new Timestamp(((Long) parameters[2]).longValue()));
88  				insertPreparedStatement.setLong(4, ((Long) parameters[3]).longValue());
89  				insertPreparedStatement.executeUpdate();
90  				ResultSet resultSet = lastIdentityStatement.executeQuery();
91  				resultSet.next();
92  				long result = resultSet.getLong(1);
93  				resultSet.close();
94  				return result;
95  			}
96  		} catch (SQLException e) {
97  			throw new StopwatchStorageException("database error", e);
98  		}
99  	}
100 
101 
102 	/***
103 	 * @see com.commsen.stopwatch.StopwatchStorage#completeRecord(long, Object[])
104 	 */
105 	public boolean completeRecord(long id, Object[] parameters) throws StopwatchStorageException {
106 		if (id < 0) return false;
107 		try {
108 			synchronized (updatePreparedStatement.getConnection()) {
109 				updatePreparedStatement.setTimestamp(1, new Timestamp(((Long) parameters[0]).longValue()));
110 				updatePreparedStatement.setLong(2, ((Long) parameters[1]).longValue());
111 				updatePreparedStatement.setLong(3, id);
112 				updatePreparedStatement.executeUpdate();
113 				return true;
114 			}
115 		} catch (SQLException e) {
116 			throw new StopwatchStorageException("database error", e);
117 		}
118 
119 	}
120 
121 
122 	/***
123 	 * 
124 	 * @param preparedStatement
125 	 * @return array of reports
126 	 * @throws SQLException
127 	 */
128 	protected Report[] prepareReports(PreparedStatement preparedStatement, Object[] params) throws SQLException {
129 		if (preparedStatement == null) return new Report[0];
130 		ArrayList list = new ArrayList();
131 		synchronized (preparedStatement.getConnection()) {
132 
133 			if (params != null && params.length > 0) {
134 				for (int i = 0; i < params.length; i++) {
135 					preparedStatement.setObject(i + 1, params[i]);
136 				}
137 			}
138 
139 			ResultSet resultSet = preparedStatement.executeQuery();
140 			while (resultSet.next()) {
141 				String group = resultSet.getString(1);
142 				String name = resultSet.getString(2);
143 				long count = resultSet.getLong(3);
144 				double min = resultSet.getDouble(4);
145 				double max = resultSet.getDouble(5);
146 				double total = resultSet.getDouble(7);
147 				// double avg = resultSet.getDouble(6);
148 				double avg = total / count;
149 
150 				list.add(new MemoryStopwatchReport(group, name, count, min, max, avg, total, resultSet.getLong(8), resultSet.getLong(9), resultSet.getLong(10)));
151 			}
152 			resultSet.close();
153 		}
154 		return (Report[]) list.toArray(new Report[list.size()]);
155 	}
156 
157 
158 	protected Logger getLogger() {
159 		return log;
160 	}
161 }