001: /*
002: * $Id: MemoryHSQLInMemoryStorage.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 MemoryHSQLInMemoryStorage extends
038: DefaultHSQLInMemoryStorage {
039:
040: /**
041: * Logger for this class
042: */
043: private static final Logger log = Logger
044: .getLogger(MemoryHSQLInMemoryStorage.class);
045:
046: protected String getTableName() {
047: return "memory_stopwatch";
048: }
049:
050: protected String getCreateTableQuery() {
051: return " create table " + getTableName() + " ("
052: + " _id INT GENERATED BY DEFAULT AS IDENTITY, "
053: + " _group VARCHAR, " + " _label VARCHAR, "
054: + " _start TIMESTAMP, " + " _end TIMESTAMP, "
055: + " _start_mem int, " + " _end_mem int " + ")";
056: }
057:
058: protected String getReturnColumns() {
059: return " count(1), " + // 3
060: " min (DATEDIFF('ms', _start, _end)), " + // 4
061: " max (DATEDIFF('ms', _start, _end)), " + // 5
062: " avg (DATEDIFF('ms', _start, _end)), " + // 6
063: " sum (DATEDIFF('ms', _start, _end)), " + // 7
064: " min (_end_mem - _start_mem), " + // 8
065: " max (_end_mem - _start_mem), " + // 9
066: " avg (_end_mem - _start_mem) "; // 10
067: }
068:
069: public String getInsertQuery() {
070: return "insert into "
071: + getTableName()
072: + " (_group, _label, _start, _start_mem) values (?, ?, ?, ?)";
073: }
074:
075: protected String getUpdateQuery() {
076: return "update "
077: + getTableName()
078: + " set _end = ?, _end_mem = ? where _id = ? and _end IS NULL";
079: }
080:
081: /**
082: * @see com.commsen.stopwatch.StopwatchStorage#newRecord(java.lang.Object[])
083: */
084: public long newRecord(Object[] parameters)
085: throws StopwatchStorageException {
086: if (insertPreparedStatement == null)
087: return -1;
088: try {
089: synchronized (insertPreparedStatement.getConnection()) {
090: insertPreparedStatement.setString(1,
091: (String) parameters[0]);
092: insertPreparedStatement.setString(2,
093: (String) parameters[1]);
094: insertPreparedStatement.setTimestamp(3, new Timestamp(
095: ((Long) parameters[2]).longValue()));
096: insertPreparedStatement.setLong(4,
097: ((Long) parameters[3]).longValue());
098: insertPreparedStatement.executeUpdate();
099: ResultSet resultSet = lastIdentityStatement
100: .executeQuery();
101: resultSet.next();
102: long result = resultSet.getLong(1);
103: resultSet.close();
104: return result;
105: }
106: } catch (SQLException e) {
107: throw new StopwatchStorageException("database error", e);
108: }
109: }
110:
111: /**
112: * @see com.commsen.stopwatch.StopwatchStorage#completeRecord(long, Object[])
113: */
114: public boolean completeRecord(long id, Object[] parameters)
115: throws StopwatchStorageException {
116: if (id < 0)
117: return false;
118: try {
119: synchronized (updatePreparedStatement.getConnection()) {
120: updatePreparedStatement.setTimestamp(1, new Timestamp(
121: ((Long) parameters[0]).longValue()));
122: updatePreparedStatement.setLong(2,
123: ((Long) parameters[1]).longValue());
124: updatePreparedStatement.setLong(3, id);
125: updatePreparedStatement.executeUpdate();
126: return true;
127: }
128: } catch (SQLException e) {
129: throw new StopwatchStorageException("database error", e);
130: }
131:
132: }
133:
134: /**
135: *
136: * @param preparedStatement
137: * @return array of reports
138: * @throws SQLException
139: */
140: protected Report[] prepareReports(
141: PreparedStatement preparedStatement, Object[] params)
142: throws SQLException {
143: if (preparedStatement == null)
144: return new Report[0];
145: ArrayList list = new ArrayList();
146: synchronized (preparedStatement.getConnection()) {
147:
148: if (params != null && params.length > 0) {
149: for (int i = 0; i < params.length; i++) {
150: preparedStatement.setObject(i + 1, params[i]);
151: }
152: }
153:
154: ResultSet resultSet = preparedStatement.executeQuery();
155: while (resultSet.next()) {
156: list.add(new MemoryStopwatchReport(resultSet
157: .getString(1), resultSet.getString(2),
158: resultSet.getLong(3), resultSet.getLong(4),
159: resultSet.getLong(5), resultSet.getLong(6),
160: resultSet.getLong(7), resultSet.getLong(8),
161: resultSet.getLong(9), resultSet.getLong(10)
162:
163: ));
164: }
165: }
166: return (Report[]) list.toArray(new Report[list.size()]);
167: }
168:
169: protected Logger getLogger() {
170: return log;
171: }
172: }
|