View Javadoc

1   /*
2    * $Id: DefaultHSQLStorage.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.SQLException;
20  import java.sql.Statement;
21  
22  import org.apache.log4j.Logger;
23  
24  import com.commsen.stopwatch.Stopwatch;
25  import com.commsen.stopwatch.StopwatchStorageException;
26  
27  /***
28   * TODO Dokumentacja
29   * 
30   * @author Milen Dyankov
31   * 
32   */
33  public class DefaultHSQLStorage extends AbstractDatabaseStorage {
34  
35  	/***
36  	 * Logger for this class
37  	 */
38  	private static final Logger log = Logger.getLogger(DefaultHSQLStorage.class);
39  
40  	private String DB_DRIVER = "db.driver";
41  	private String DB_CONNECTION = "db.connectionString";
42  	private String DB_USER = "db.user";
43  	private String DB_PASSWORD = "db.password";
44  	private String DB_TABLE = "db.tableName";
45  
46  
47  	// protected String getDriver() { return "org.hsqldb.jdbcDriver"; }
48  	protected String getDriver() {
49  		return properties.getProperty(DB_DRIVER, "org.hsqldb.jdbcDriver");
50  	}
51  
52  
53  	// protected String getConnectionString() { return
54  	// "jdbc:hsqldb:hsql://localhost:9001/stopwatch"; }
55  	protected String getConnectionString() {
56  		return properties.getProperty(DB_CONNECTION, "jdbc:hsqldb:hsql://localhost:9001/stopwatch");
57  	}
58  
59  
60  	// protected String getUser() { return "sa"; }
61  	protected String getUser() {
62  		return properties.getProperty(DB_USER, "sa");
63  	}
64  
65  
66  	// protected String getPassword() { return ""; }
67  	protected String getPassword() {
68  		return properties.getProperty(DB_PASSWORD, "");
69  	}
70  
71  
72  	protected String getLastIdentityQuery() {
73  		return "CALL IDENTITY()";
74  	}
75  
76  
77  	// protected String getTableName() { return "stopwatch"; }
78  	protected String getTableName() {
79  		return Stopwatch.getProperty(DB_TABLE, "stopwatch");
80  	}
81  
82  
83  	protected String getReturnColumns() {
84  		return "  count(1), " + "  min (DATEDIFF('ms', _start, _end)) as minTime," + "  max (DATEDIFF('ms', _start, _end)) as maxTime," + "  avg (DATEDIFF('ms', _start, _end)) as avgTime,"
85  		        + "  sum (DATEDIFF('ms', _start, _end)) as totalTime ";
86  	}
87  
88  	private boolean debugEnabled;
89  
90  
91  	/***
92  	 * @see com.commsen.stopwatch.StopwatchStorage#freeze()
93  	 */
94  	public void freeze() throws StopwatchStorageException {
95  		Statement statement = null;
96  		try {
97  			statement = updateConnection.createStatement();
98  			statement.execute("delete from " + getTableName() + " where _end is NULL");
99  		} catch (SQLException e) {
100 			getLogger().error(e, e);
101 		} finally {
102 			if (statement != null) {
103 				try {
104 					statement.close();
105 				} catch (SQLException e) {
106 					getLogger().error(e, e);
107 				}
108 			}
109 		}
110 	}
111 
112 
113 	/***
114 	 * 
115 	 * @throws StopwatchStorageException
116 	 * @see com.commsen.stopwatch.StopwatchStorage#close()
117 	 */
118 	public void close() throws StopwatchStorageException {
119 		Statement statement = null;
120 		try {
121 			statement = updateConnection.createStatement();
122 			statement.execute("drop table " + getTableName());
123 		} catch (SQLException e) {
124 			throw new StopwatchStorageException("database error", e);
125 		} finally {
126 			if (statement != null) {
127 				try {
128 					statement.close();
129 				} catch (SQLException e) {
130 					throw new StopwatchStorageException("database error", e);
131 				}
132 			}
133 		}
134 		super.close();
135 	}
136 
137 
138 	protected Logger getLogger() {
139 		return log;
140 	}
141 
142 
143 	/***
144 	 * @return Returns the debugEnabled.
145 	 */
146 	public boolean isDebugEnabled() {
147 		return debugEnabled;
148 	}
149 
150 
151 	/***
152 	 * @param debugEnabled The debugEnabled to set.
153 	 */
154 	public void setDebugEnabled(boolean debugEnabled) {
155 		this.debugEnabled = debugEnabled;
156 	}
157 
158 }