001: /*
002: * $Id: MemoryHSQLStorage.java,v 1.1 2006/03/06 11:30:53 azzazzel Exp $
003: *
004: * Copyright 2006 Commsen International
005: *
006: * Licensed under the Common Public License, Version 1.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.opensource.org/licenses/cpl1.0.txt
011: *
012: * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013: * EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS
014: * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
015: *
016: */
017: package com.commsen.stopwatch.storages;
018:
019: import java.sql.PreparedStatement;
020: import java.sql.ResultSet;
021: import java.sql.SQLException;
022: import java.sql.Timestamp;
023: import java.util.ArrayList;
024:
025: import org.apache.log4j.Logger;
026:
027: import com.commsen.stopwatch.Report;
028: import com.commsen.stopwatch.StopwatchStorageException;
029: import com.commsen.stopwatch.reports.MemoryStopwatchReport;
030:
031: /**
032: * TODO Dokumentacja
033: *
034: * @author Milen Dyankov
035: *
036: */
037: public class MemoryHSQLStorage extends DefaultHSQLStorage {
038:
039: /**
040: * Logger for this class
041: */
042: private static final Logger log = Logger
043: .getLogger(MemoryHSQLStorage.class);
044:
045: /**
046: * @see com.commsen.stopwatch.storages.AbstractDatabaseStorage#getTableName()
047: */
048: protected String getTableName() {
049: return "memory_stopwatch";
050: }
051:
052: protected String getCreateTableQuery() {
053: return " create table " + getTableName() + " ("
054: + " _id INT GENERATED BY DEFAULT AS IDENTITY, "
055: + " _group VARCHAR, " + " _label VARCHAR, "
056: + " _start TIMESTAMP, " + " _end TIMESTAMP, "
057: + " _start_mem int, " + " _end_mem int " + ")";
058: }
059:
060: protected String getReturnColumns() {
061: return " count(1), " + // 3
062: " min (DATEDIFF('ms', _start, _end)), " + // 4
063: " max (DATEDIFF('ms', _start, _end)), " + // 5
064: " avg (DATEDIFF('ms', _start, _end)), " + // 6
065: " sum (DATEDIFF('ms', _start, _end)), " + // 7
066: " min (_end_mem - _start_mem), " + // 8
067: " max (_end_mem - _start_mem), " + // 9
068: " avg (_end_mem - _start_mem) "; // 10
069: }
070:
071: public String getInsertQuery() {
072: return "insert into "
073: + getTableName()
074: + " (_group, _label, _start, _start_mem) values (?, ?, ?, ?)";
075: }
076:
077: protected String getUpdateQuery() {
078: return "update "
079: + getTableName()
080: + " set _end = ?, _end_mem = ? where _id = ? and _end IS NULL";
081: }
082:
083: /**
084: * @see com.commsen.stopwatch.StopwatchStorage#newRecord(java.lang.Object[])
085: */
086: public long newRecord(Object[] parameters)
087: throws StopwatchStorageException {
088: if (insertPreparedStatement == null)
089: return -1;
090: try {
091: synchronized (insertPreparedStatement.getConnection()) {
092: insertPreparedStatement.setString(1,
093: (String) parameters[0]);
094: insertPreparedStatement.setString(2,
095: (String) parameters[1]);
096: insertPreparedStatement.setTimestamp(3, new Timestamp(
097: ((Long) parameters[2]).longValue()));
098: insertPreparedStatement.setLong(4,
099: ((Long) parameters[3]).longValue());
100: insertPreparedStatement.executeUpdate();
101: ResultSet resultSet = lastIdentityStatement
102: .executeQuery();
103: resultSet.next();
104: long result = resultSet.getLong(1);
105: resultSet.close();
106: return result;
107: }
108: } catch (SQLException e) {
109: throw new StopwatchStorageException("database error", e);
110: }
111: }
112:
113: /**
114: * @see com.commsen.stopwatch.StopwatchStorage#completeRecord(long, Object[])
115: */
116: public boolean completeRecord(long id, Object[] parameters)
117: throws StopwatchStorageException {
118: if (id < 0)
119: return false;
120: try {
121: synchronized (updatePreparedStatement.getConnection()) {
122: updatePreparedStatement.setTimestamp(1, new Timestamp(
123: ((Long) parameters[0]).longValue()));
124: updatePreparedStatement.setLong(2,
125: ((Long) parameters[1]).longValue());
126: updatePreparedStatement.setLong(3, id);
127: updatePreparedStatement.executeUpdate();
128: return true;
129: }
130: } catch (SQLException e) {
131: throw new StopwatchStorageException("database error", e);
132: }
133:
134: }
135:
136: /**
137: *
138: * @param preparedStatement
139: * @return array of reports
140: * @throws SQLException
141: */
142: protected Report[] prepareReports(
143: PreparedStatement preparedStatement, Object[] params)
144: throws SQLException {
145: if (preparedStatement == null)
146: return new Report[0];
147: ArrayList list = new ArrayList();
148: synchronized (preparedStatement.getConnection()) {
149:
150: if (params != null && params.length > 0) {
151: for (int i = 0; i < params.length; i++) {
152: preparedStatement.setObject(i + 1, params[i]);
153: }
154: }
155:
156: ResultSet resultSet = preparedStatement.executeQuery();
157: while (resultSet.next()) {
158: list.add(new MemoryStopwatchReport(resultSet
159: .getString(1), resultSet.getString(2),
160: resultSet.getLong(3), resultSet.getLong(4),
161: resultSet.getLong(5), resultSet.getLong(6),
162: resultSet.getLong(7), resultSet.getLong(8),
163: resultSet.getLong(9), resultSet.getLong(10)
164:
165: ));
166: }
167: }
168: return (Report[]) list.toArray(new Report[list.size()]);
169: }
170:
171: protected Logger getLogger() {
172: return log;
173: }
174: }
|