/*
* $Id: MemoryHSQLInMemoryStorage.java,v 1.1 2006/03/06 11:30:53 azzazzel Exp $
*
* Copyright 2006 Commsen International
*
* Licensed under the Common Public License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.opensource.org/licenses/cpl1.0.txt
*
* THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS
* OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
*/
package com.commsen.stopwatch.storages;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import org.apache.log4j.Logger;
import com.commsen.stopwatch.Report;
import com.commsen.stopwatch.StopwatchStorageException;
import com.commsen.stopwatch.reports.MemoryStopwatchReport;
/**
* TODO Dokumentacja
*
* @author Milen Dyankov
*
*/
public class MemoryHSQLInMemoryStorage extends DefaultHSQLInMemoryStorage {
/**
* Logger for this class
*/
private static final Logger log = Logger.getLogger(MemoryHSQLInMemoryStorage.class);
protected String getTableName() { return "memory_stopwatch"; }
protected String getCreateTableQuery() {
return
" create table " + getTableName() + " (" +
" _id INT GENERATED BY DEFAULT AS IDENTITY, " +
" _group VARCHAR, " +
" _label VARCHAR, " +
" _start TIMESTAMP, " +
" _end TIMESTAMP, " +
" _start_mem int, " +
" _end_mem int " +
")";
}
protected String getReturnColumns() {
return
" count(1), " + // 3
" min (DATEDIFF('ms', _start, _end)), " + // 4
" max (DATEDIFF('ms', _start, _end)), " + // 5
" avg (DATEDIFF('ms', _start, _end)), " + // 6
" sum (DATEDIFF('ms', _start, _end)), " + // 7
" min (_end_mem - _start_mem), " + // 8
" max (_end_mem - _start_mem), " + // 9
" avg (_end_mem - _start_mem) "; // 10
}
public String getInsertQuery() {
return "insert into " + getTableName() + " (_group, _label, _start, _start_mem) values (?, ?, ?, ?)";
}
protected String getUpdateQuery() {
return "update " + getTableName() + " set _end = ?, _end_mem = ? where _id = ? and _end IS NULL";
}
/**
* @see com.commsen.stopwatch.StopwatchStorage#newRecord(java.lang.Object[])
*/
public long newRecord(Object[] parameters) throws StopwatchStorageException {
if (insertPreparedStatement == null) return -1;
try {
synchronized (insertPreparedStatement.getConnection()) {
insertPreparedStatement.setString(1, (String)parameters[0]);
insertPreparedStatement.setString(2, (String)parameters[1]);
insertPreparedStatement.setTimestamp(3, new Timestamp(((Long)parameters[2]).longValue()));
insertPreparedStatement.setLong(4, ((Long)parameters[3]).longValue());
insertPreparedStatement.executeUpdate();
ResultSet resultSet = lastIdentityStatement.executeQuery();
resultSet.next();
long result = resultSet.getLong(1);
resultSet.close();
return result;
}
} catch (SQLException e) {
throw new StopwatchStorageException("database error", e);
}
}
/**
* @see com.commsen.stopwatch.StopwatchStorage#completeRecord(long, Object[])
*/
public boolean completeRecord(long id, Object[] parameters) throws StopwatchStorageException {
if (id < 0) return false;
try {
synchronized (updatePreparedStatement.getConnection()) {
updatePreparedStatement.setTimestamp(1, new Timestamp(((Long)parameters[0]).longValue()));
updatePreparedStatement.setLong(2, ((Long)parameters[1]).longValue());
updatePreparedStatement.setLong(3, id);
updatePreparedStatement.executeUpdate();
return true;
}
} catch (SQLException e) {
throw new StopwatchStorageException("database error", e);
}
}
/**
*
* @param preparedStatement
* @return array of reports
* @throws SQLException
*/
protected Report[] prepareReports (PreparedStatement preparedStatement, Object[] params) throws SQLException {
if (preparedStatement == null) return new Report[0];
ArrayList list = new ArrayList();
synchronized (preparedStatement.getConnection()) {
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject(i+1, params[i]);
}
}
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
list.add(new MemoryStopwatchReport(
resultSet.getString(1),
resultSet.getString(2),
resultSet.getLong(3),
resultSet.getLong(4),
resultSet.getLong(5),
resultSet.getLong(6),
resultSet.getLong(7),
resultSet.getLong(8),
resultSet.getLong(9),
resultSet.getLong(10)
));
}
}
return (Report[])list.toArray(new Report[list.size()]);
}
protected Logger getLogger () {
return log;
}
}