001: /*
002: * $Id: DefaultHSQLStorage.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.SQLException;
020: import java.sql.Statement;
021:
022: import org.apache.log4j.Logger;
023:
024: import com.commsen.stopwatch.Stopwatch;
025: import com.commsen.stopwatch.StopwatchStorageException;
026:
027: /**
028: * TODO Dokumentacja
029: *
030: * @author Milen Dyankov
031: *
032: */
033: public class DefaultHSQLStorage extends AbstractDatabaseStorage {
034:
035: /**
036: * Logger for this class
037: */
038: private static final Logger log = Logger
039: .getLogger(DefaultHSQLStorage.class);
040:
041: private String DB_DRIVER = "db.driver";
042: private String DB_CONNECTION = "db.connectionString";
043: private String DB_USER = "db.user";
044: private String DB_PASSWORD = "db.password";
045: private String DB_TABLE = "db.tableName";
046:
047: // protected String getDriver() { return "org.hsqldb.jdbcDriver"; }
048: protected String getDriver() {
049: return Stopwatch
050: .getProperty(DB_DRIVER, "org.hsqldb.jdbcDriver");
051: }
052:
053: // protected String getConnectionString() { return "jdbc:hsqldb:hsql://localhost:9001/stopwatch"; }
054: protected String getConnectionString() {
055: return Stopwatch.getProperty(DB_CONNECTION,
056: "jdbc:hsqldb:hsql://localhost:9001/stopwatch");
057: }
058:
059: // protected String getUser() { return "sa"; }
060: protected String getUser() {
061: return Stopwatch.getProperty(DB_USER, "sa");
062: }
063:
064: // protected String getPassword() { return ""; }
065: protected String getPassword() {
066: return Stopwatch.getProperty(DB_PASSWORD, "");
067: }
068:
069: protected String getLastIdentityQuery() {
070: return "CALL IDENTITY()";
071: }
072:
073: // protected String getTableName() { return "stopwatch"; }
074: protected String getTableName() {
075: return Stopwatch.getProperty(DB_TABLE, "stopwatch");
076: }
077:
078: protected String getReturnColumns() {
079: return " count(1), "
080: + " min (DATEDIFF('ms', _start, _end)) as minTime,"
081: + " max (DATEDIFF('ms', _start, _end)) as maxTime,"
082: + " avg (DATEDIFF('ms', _start, _end)) as avgTime,"
083: + " sum (DATEDIFF('ms', _start, _end)) as totalTime ";
084: }
085:
086: private boolean debugEnabled;
087:
088: /**
089: * @see com.commsen.stopwatch.StopwatchStorage#freeze()
090: */
091: public void freeze() throws StopwatchStorageException {
092: Statement statement;
093: try {
094: statement = updateConnection.createStatement();
095: statement
096: .execute("delete from stopwatch where _end is NULL");
097: statement.close();
098: } catch (SQLException e) {
099: getLogger().error(e, e);
100: }
101: }
102:
103: /**
104: *
105: * @throws StopwatchStorageException
106: * @see com.commsen.stopwatch.StopwatchStorage#close()
107: */
108: public void close() throws StopwatchStorageException {
109: try {
110: updateConnection.createStatement().execute(
111: "drop table stopwatch");
112: } catch (SQLException e) {
113: throw new StopwatchStorageException("database error", e);
114: }
115: super .close();
116: }
117:
118: protected Logger getLogger() {
119: return log;
120: }
121:
122: /**
123: * @return Returns the debugEnabled.
124: */
125: public boolean isDebugEnabled() {
126: return debugEnabled;
127: }
128:
129: /**
130: * @param debugEnabled The debugEnabled to set.
131: */
132: public void setDebugEnabled(boolean debugEnabled) {
133: this.debugEnabled = debugEnabled;
134: }
135:
136: }
|