View Javadoc

1   /*
2    * $Id: MemoryHSQLStorage.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 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), " + // 3
61  		        "  min (DATEDIFF('ms', _start, _end)), " + // 4
62  		        "  max (DATEDIFF('ms', _start, _end)), " + // 5
63  		        "  avg (DATEDIFF('ms', _start, _end)), " + // 6
64  		        "  sum (DATEDIFF('ms', _start, _end)), " + // 7
65  		        "  min (_end_mem - _start_mem), " + // 8
66  		        "  max (_end_mem - _start_mem), " + // 9
67  		        "  avg (_end_mem - _start_mem) "; // 10
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 }