com.commsen.stopwatch.storages
Class AbstractDatabaseStorage

java.lang.Object
  extended by com.commsen.stopwatch.storages.AbstractDatabaseStorage
All Implemented Interfaces:
StopwatchStorage
Direct Known Subclasses:
DefaultHSQLInMemoryStorage, DefaultHSQLStorage

public abstract class AbstractDatabaseStorage
extends java.lang.Object
implements StopwatchStorage

Abstract class representig database storage. It has predefined methods to crete table(s) as well as insert, update and delete records. The default implementation is limited to the very basic measurment data - time and count. If any other info is to be stored, the extending class should overwrite appropriate getXxxxQuery() method(s)

Author:
Milen Dyankov

Field Summary
protected  java.sql.PreparedStatement allByGroupReportStatement
          Prepared statement initialized in open() method with query returned by getAllByGroupReportQuery()
protected  java.sql.PreparedStatement allByLabelReportStatement
          Prepared statement initialized in open() method with query returned by getAllByLabelReportQuery()
protected  java.sql.PreparedStatement allReportStatement
          Prepared statement initialized in open() method with query returned by getAllReportQuery()
protected  java.sql.PreparedStatement deletePreparedStatement
          Prepared statement initialized in open() method with query returned by getDeleteQuery()
protected  java.sql.PreparedStatement groupLabelLoadStatement
           
protected  java.sql.PreparedStatement groupLoadStatement
           
protected  java.sql.PreparedStatement groupReportStatement
          Prepared statement initialized in open() method with query returned by getGroupReportQuery()
protected  java.sql.PreparedStatement insertPreparedStatement
          Prepared statement initialized in open() method with query returned by getInsertQuery()
protected  java.sql.PreparedStatement labelLoadStatement
           
protected  java.sql.PreparedStatement labelReportStatement
          Prepared statement initialized in open() method with query returned by getLabelReportQuery()
protected  java.sql.PreparedStatement lastIdentityStatement
          Prepared statement initialized in open() method with query returned by getLastIdentityQuery()
protected  java.sql.PreparedStatement loadStatement
           
protected  java.util.Properties properties
           
protected  java.sql.PreparedStatement singleReportStatement
          Prepared statement initialized in open() method with query returned by getSingleReportQuery()
protected  java.sql.PreparedStatement updatePreparedStatement
          Prepared statement initialized in open() method with query returned by getUpdateQuery()
 
Constructor Summary
AbstractDatabaseStorage()
           
 
Method Summary
 void close()
          Called when engine is about to be stopped or for some other reason will no more use this storage.
 boolean completeRecord(long id, java.lang.Object[] parameters)
          Instructs the storage to complete (at least remember the time) the record identified by given parameters.
 void freeze()
          Called when engine is about to be paused or for some other reason will temporary not use this storage.
protected  java.lang.String getAllByGroupReportQuery()
          Called to obtain the query to be executed when summary report about all groups is to be generated
 Report[] getAllByGroupReports()
          This method simply calls prepareReports(PreparedStatement, Object[]) passing allByGroupReportStatement as statement and no parameters (null value).
protected  java.lang.String getAllByLabelReportQuery()
          Called to obtain the query to be executed when summary report about all labels is to be generated
 Report[] getAllByLabelReports()
          This method simply calls prepareReports(PreparedStatement, Object[]) passing allByLabelReportStatement as statement and no parameters (null value).
protected  java.lang.String getAllReportQuery()
          Called to obtain the query to be executed when report about all groups and labels is to be generated
protected  java.lang.String getCheckTableQuery()
          Provides SQL query which will be executed to check if table exist
protected abstract  java.lang.String getConnectionString()
          Called to obtain the connection string
protected  java.lang.String getCreateTableQuery()
          Called to obtain the query to be executed when table needs to be created.
protected  java.lang.String getDeleteQuery()
           
protected abstract  java.lang.String getDriver()
          Called to obtain the JDBC driver to use
protected  java.lang.String getGroupBy()
          The group by clause used in all report queries Default is: _group, _label
protected  java.lang.String getGroupLabelLoadQuery()
           
protected  java.lang.String getGroupLoadQuery()
           
protected  java.lang.String getGroupReportQuery()
          Called to obtain the query to be executed when report about given group and label is to be generated
 Report[] getGroupReports(java.lang.String group)
          This method simply calls prepareReports(PreparedStatement, Object[]) passing groupReportStatement as statement and group as parameter.
 java.lang.String getInsertQuery()
           
protected  java.lang.String getLabelLoadQuery()
           
protected  java.lang.String getLabelReportQuery()
          Called to obtain the query to be executed when report about given group and label is to be generated
 Report[] getLabelReports(java.lang.String label)
          This method simply calls prepareReports(PreparedStatement, Object[]) passing labelReportStatement as statement and label as parameter.
protected abstract  java.lang.String getLastIdentityQuery()
           
 long[] getLoad(java.lang.String group, java.lang.String label, int field, int value)
          Implementing methods should generate and return information of how many instances of the code specified by group and label ware running for the last numberOfPeriods periods.
protected  java.lang.String getLoadQuery()
           
protected abstract  org.apache.log4j.Logger getLogger()
          Returns the logger for this class
protected  java.lang.String getOrderBy()
          The order by clause used in all report queries Default is: _group, _label
protected abstract  java.lang.String getPassword()
          Called to obtain the database password
 java.util.Properties getProperties()
          Returns storage properties.
 Report getReport(java.lang.String group, java.lang.String label)
          This method simply calls prepareReports(PreparedStatement, Object[]) passing singleReportStatement as statement and group and label as parameters.
 Report[] getReports()
          This method simply calls prepareReports(PreparedStatement, Object[]) passing allReportStatement as statement and no parameters (null value).
protected abstract  java.lang.String getReturnColumns()
          Called to obtain the columns to be returned.
protected  java.lang.String getSingleReportQuery()
          Called to obtain the query to be executed when report about given group and label is to be generated
protected abstract  java.lang.String getTableName()
          Provides the table name
protected  java.lang.String getTruncTableQuery()
          Called to obtain the query to be executed when table needs to be truncated
protected  java.lang.String getUpdateQuery()
           
protected abstract  java.lang.String getUser()
          Called to obtain the database user name
protected  boolean isDebug()
          Checks if debug log level is enabled in both Stopwatch and Log4j.
abstract  boolean isDebugEnabled()
           
 long newCompleteRecord(java.lang.Object[] startParameters, java.lang.Object[] endParameters)
          Instructs the storage to create new complete record and store passed parameters.
 long newRecord(java.lang.Object[] parameters)
          Instructs the storage to create new record and store passed parameters.
 void open()
          Called when engine is about to use the storage for the first time.
protected  Report[] prepareReports(java.sql.PreparedStatement statement, java.lang.Object[] params)
          This method simply executes given statement with given params.
 boolean removeRecord(long id)
          Instructs the storage to remove the record identified by given parameters.
 void setProperties(java.util.Properties properties)
          Called by Stopwatch to set storage properties .
 void unfreeze()
          Called when engine is about to be resumed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.commsen.stopwatch.StopwatchStorage
setDebugEnabled
 

Field Detail

properties

protected java.util.Properties properties

insertPreparedStatement

protected java.sql.PreparedStatement insertPreparedStatement
Prepared statement initialized in open() method with query returned by getInsertQuery()


updatePreparedStatement

protected java.sql.PreparedStatement updatePreparedStatement
Prepared statement initialized in open() method with query returned by getUpdateQuery()


deletePreparedStatement

protected java.sql.PreparedStatement deletePreparedStatement
Prepared statement initialized in open() method with query returned by getDeleteQuery()


lastIdentityStatement

protected java.sql.PreparedStatement lastIdentityStatement
Prepared statement initialized in open() method with query returned by getLastIdentityQuery()


allReportStatement

protected java.sql.PreparedStatement allReportStatement
Prepared statement initialized in open() method with query returned by getAllReportQuery()


allByGroupReportStatement

protected java.sql.PreparedStatement allByGroupReportStatement
Prepared statement initialized in open() method with query returned by getAllByGroupReportQuery()


allByLabelReportStatement

protected java.sql.PreparedStatement allByLabelReportStatement
Prepared statement initialized in open() method with query returned by getAllByLabelReportQuery()


groupReportStatement

protected java.sql.PreparedStatement groupReportStatement
Prepared statement initialized in open() method with query returned by getGroupReportQuery()


labelReportStatement

protected java.sql.PreparedStatement labelReportStatement
Prepared statement initialized in open() method with query returned by getLabelReportQuery()


singleReportStatement

protected java.sql.PreparedStatement singleReportStatement
Prepared statement initialized in open() method with query returned by getSingleReportQuery()


groupLabelLoadStatement

protected java.sql.PreparedStatement groupLabelLoadStatement

groupLoadStatement

protected java.sql.PreparedStatement groupLoadStatement

labelLoadStatement

protected java.sql.PreparedStatement labelLoadStatement

loadStatement

protected java.sql.PreparedStatement loadStatement
Constructor Detail

AbstractDatabaseStorage

public AbstractDatabaseStorage()
Method Detail

getDriver

protected abstract java.lang.String getDriver()
Called to obtain the JDBC driver to use

Returns:
the fully qualified class name

getConnectionString

protected abstract java.lang.String getConnectionString()
Called to obtain the connection string

Returns:
the connection string

getUser

protected abstract java.lang.String getUser()
Called to obtain the database user name

Returns:
database user name

getPassword

protected abstract java.lang.String getPassword()
Called to obtain the database password

Returns:
database password

getTableName

protected abstract java.lang.String getTableName()
Provides the table name

Returns:
table name

getCheckTableQuery

protected java.lang.String getCheckTableQuery()
Provides SQL query which will be executed to check if table exist

Default is: select 1 from getTableName()

Returns:
SQL query to check if table exist

getCreateTableQuery

protected java.lang.String getCreateTableQuery()
Called to obtain the query to be executed when table needs to be created.

Default is:

 create table getTableName() (
        _id INT GENERATED BY DEFAULT AS IDENTITY,
        _group VARCHAR,
        _label VARCHAR,
        _start TIMESTAMP,
        _end TIMESTAMP
 )
 

Returns:
SQL query to create table

getTruncTableQuery

protected java.lang.String getTruncTableQuery()
Called to obtain the query to be executed when table needs to be truncated

Default is: delete from getTableName()

Returns:
SQL query to truncate table

getReturnColumns

protected abstract java.lang.String getReturnColumns()
Called to obtain the columns to be returned. The columns returned by this method are used in all report queries.

Returns:
part of SQL query

getGroupBy

protected java.lang.String getGroupBy()
The group by clause used in all report queries Default is: _group, _label

Returns:
part of SQL query

getOrderBy

protected java.lang.String getOrderBy()
The order by clause used in all report queries Default is: _group, _label

Returns:
part of SQL query

getAllReportQuery

protected java.lang.String getAllReportQuery()
Called to obtain the query to be executed when report about all groups and labels is to be generated

Default is:

 select getReturnColumns() 
 from getTableName() 
 where _end is not null
 group by getGroupBy()
 order by getOrderBy()
 )
 

Returns:
SQL query used to generate report

getAllByGroupReportQuery

protected java.lang.String getAllByGroupReportQuery()
Called to obtain the query to be executed when summary report about all groups is to be generated

Default is:

 select getReturnColumns() 
 from getTableName() 
 where _end is not null
 group by _group
 order by _group
 )
 

Returns:
SQL query used to generate report

getAllByLabelReportQuery

protected java.lang.String getAllByLabelReportQuery()
Called to obtain the query to be executed when summary report about all labels is to be generated

Default is:

 select getReturnColumns() 
 from getTableName() 
 where _end is not null
 group by _label
 order by _label
 )
 

Returns:
SQL query used to generate report

getSingleReportQuery

protected java.lang.String getSingleReportQuery()
Called to obtain the query to be executed when report about given group and label is to be generated

Default is:

 select getReturnColumns() 
 from getTableName() 
 where _end is not null and _group = ? and _label = ? 
 group by getGroupBy()
 order by getOrderBy()
 )
 

Returns:
SQL query used to generate report

getLabelReportQuery

protected java.lang.String getLabelReportQuery()
Called to obtain the query to be executed when report about given group and label is to be generated

Default is:

 select getReturnColumns() 
 from getTableName() 
 where _end is not null and _label = ? 
 group by getGroupBy()
 order by getOrderBy()
 )
 

Returns:
SQL query used to generate report

getGroupReportQuery

protected java.lang.String getGroupReportQuery()
Called to obtain the query to be executed when report about given group and label is to be generated

Default is:

 select getReturnColumns() 
 from getTableName() 
 where _end is not null and _group = ? 
 group by getGroupBy()
 order by getOrderBy()
 )
 

Returns:
SQL query used to generate report

getInsertQuery

public java.lang.String getInsertQuery()

getUpdateQuery

protected java.lang.String getUpdateQuery()

getDeleteQuery

protected java.lang.String getDeleteQuery()

getLastIdentityQuery

protected abstract java.lang.String getLastIdentityQuery()

getLoadQuery

protected java.lang.String getLoadQuery()

getGroupLoadQuery

protected java.lang.String getGroupLoadQuery()

getLabelLoadQuery

protected java.lang.String getLabelLoadQuery()

getGroupLabelLoadQuery

protected java.lang.String getGroupLabelLoadQuery()

open

public void open()
          throws StopwatchStorageException
Description copied from interface: StopwatchStorage
Called when engine is about to use the storage for the first time. Gives storage a chance to open connections, prepare statements, etc.

Specified by:
open in interface StopwatchStorage
Throws:
StopwatchStorageException - if there is problem preparing the storage.
See Also:
StopwatchStorage.open()

freeze

public void freeze()
            throws StopwatchStorageException
Description copied from interface: StopwatchStorage
Called when engine is about to be paused or for some other reason will temporary not use this storage. Gives storage a chance to free resources.

Specified by:
freeze in interface StopwatchStorage
Throws:
StopwatchStorageException - if there is a problem with freezing the storage.
See Also:
StopwatchStorage.freeze()

unfreeze

public void unfreeze()
              throws StopwatchStorageException
Description copied from interface: StopwatchStorage
Called when engine is about to be resumed. Simply indicates that is about to use this storage again. Gives storage a chance to re-connect, etc.

Specified by:
unfreeze in interface StopwatchStorage
Throws:
StopwatchStorageException - if there is a problem with unfreezing the storage.
See Also:
StopwatchStorage.unfreeze()

close

public void close()
           throws StopwatchStorageException
Description copied from interface: StopwatchStorage
Called when engine is about to be stopped or for some other reason will no more use this storage. Gives storage a chance to clean up.

Specified by:
close in interface StopwatchStorage
Throws:
StopwatchStorageException - if there is a problem closing the storage
See Also:
StopwatchStorage.close()

newRecord

public long newRecord(java.lang.Object[] parameters)
               throws StopwatchStorageException
Description copied from interface: StopwatchStorage
Instructs the storage to create new record and store passed parameters. Engines should know what parameters storage expects.

Specified by:
newRecord in interface StopwatchStorage
Returns:
the id of newly created record
Throws:
StopwatchStorageException
See Also:
StopwatchStorage.newRecord(java.lang.Object[])

newCompleteRecord

public long newCompleteRecord(java.lang.Object[] startParameters,
                              java.lang.Object[] endParameters)
                       throws StopwatchStorageException
Description copied from interface: StopwatchStorage
Instructs the storage to create new complete record and store passed parameters. It is used in DELAYED mode. Start parameters are kept in memory until the end of given measurments and then passed here together with the end parameters. Engines should know what parameters storage expects.

Specified by:
newCompleteRecord in interface StopwatchStorage
Parameters:
startParameters - parameters describing start condition
endParameters - parameters describing end condition
Returns:
true if record was completed successfuly, false otherwise
Throws:
StopwatchStorageException - on error
See Also:
StopwatchStorage.newRecord(java.lang.Object[])

removeRecord

public boolean removeRecord(long id)
                     throws StopwatchStorageException
Description copied from interface: StopwatchStorage
Instructs the storage to remove the record identified by given parameters. Engines should know what parameters storage expects. Most storages will assume that parameters passed uniquely identify only one record.

Specified by:
removeRecord in interface StopwatchStorage
Parameters:
id - of the database record to be removed
Returns:
true if record was removed successfuly, false otherwise
Throws:
StopwatchStorageException - on error
See Also:
StopwatchStorage.removeRecord(long)

completeRecord

public boolean completeRecord(long id,
                              java.lang.Object[] parameters)
                       throws StopwatchStorageException
Description copied from interface: StopwatchStorage
Instructs the storage to complete (at least remember the time) the record identified by given parameters. Engines should know what parameters storage expects. Most storages will assume that parameters passed uniquely identify only one record.

Specified by:
completeRecord in interface StopwatchStorage
Parameters:
id - of the database record to be updated
parameters - used to find the record
Returns:
true if record was completed successfuly, false otherwise
Throws:
StopwatchStorageException - on error
See Also:
StopwatchStorage.completeRecord(long, Object[])

getReports

public Report[] getReports()
This method simply calls prepareReports(PreparedStatement, Object[]) passing allReportStatement as statement and no parameters (null value).

Specified by:
getReports in interface StopwatchStorage
Returns:
array of reports.
See Also:
StopwatchStorage.getReports()

getAllByGroupReports

public Report[] getAllByGroupReports()
This method simply calls prepareReports(PreparedStatement, Object[]) passing allByGroupReportStatement as statement and no parameters (null value).

Specified by:
getAllByGroupReports in interface StopwatchStorage
Returns:
array of reports.
See Also:
StopwatchStorage.getReports()

getAllByLabelReports

public Report[] getAllByLabelReports()
This method simply calls prepareReports(PreparedStatement, Object[]) passing allByLabelReportStatement as statement and no parameters (null value).

Specified by:
getAllByLabelReports in interface StopwatchStorage
Returns:
array of reports.
See Also:
StopwatchStorage.getReports()

getReport

public Report getReport(java.lang.String group,
                        java.lang.String label)
This method simply calls prepareReports(PreparedStatement, Object[]) passing singleReportStatement as statement and group and label as parameters.

Specified by:
getReport in interface StopwatchStorage
Parameters:
group - the group for which report should be generated
label - the label for which report should be generated
Returns:
single report for provided group and label.
See Also:
StopwatchStorage.getReport(java.lang.String, java.lang.String)

getGroupReports

public Report[] getGroupReports(java.lang.String group)
This method simply calls prepareReports(PreparedStatement, Object[]) passing groupReportStatement as statement and group as parameter.

Specified by:
getGroupReports in interface StopwatchStorage
Parameters:
group - the name of group for which report should be generated
Returns:
array of reports.
See Also:
StopwatchStorage.getGroupReports(java.lang.String)

getLabelReports

public Report[] getLabelReports(java.lang.String label)
This method simply calls prepareReports(PreparedStatement, Object[]) passing labelReportStatement as statement and label as parameter.

Specified by:
getLabelReports in interface StopwatchStorage
Parameters:
label - the label for which report should be generated
Returns:
array of reports.
See Also:
StopwatchStorage.getLabelReports(java.lang.String)

prepareReports

protected Report[] prepareReports(java.sql.PreparedStatement statement,
                                  java.lang.Object[] params)
                           throws java.sql.SQLException
This method simply executes given statement with given params. All report queries call this method to obtain array of reports. This method should be overwriten by extending classes that need to provide more measurment information then basic time and count.

Parameters:
statement - the prepared statement to execute
params - the params to passed to the prepared statement
Returns:
array of Report
Throws:
java.sql.SQLException - on database connection error or other errors

getLoad

public long[] getLoad(java.lang.String group,
                      java.lang.String label,
                      int field,
                      int value)
Description copied from interface: StopwatchStorage
Implementing methods should generate and return information of how many instances of the code specified by group and label ware running for the last numberOfPeriods periods. Period length is defined by periodField which can be one of Calendar.FIELD_NAME

For example to see how many instances of code labeled "l1" in group "g1" were running per minute for the last 30 minutes, one could use:

                long[] load = Stopwatch.getLoad("g1, "l1", Calendar.MINUTE, 30);
 
which will forward the call to the appropriate storage implementation. In this case load[0] will contain the number of code instances running 30 minutes ago and load[29] number of code instances running in the last minute.

If group is null - summary load of all masurments labeled label should be returned. If label is null - summary load of all masurments in group gtroup should be returned. If both group and label are null - summary load of all masurments should be returned.

Specified by:
getLoad in interface StopwatchStorage
Parameters:
group - the group for which load report should be generated
label - the label for which load report should be generated
field - can be one of Calendar.FIELD_NAME
value - number of periods
Returns:
array of length numberOfPeriods where every element represents the load for given pariod.
See Also:
com.commsen.stopwatch.StopwatchStorage#getLoad(int, int)

getLogger

protected abstract org.apache.log4j.Logger getLogger()
Returns the logger for this class

Returns:
the logger for this class

isDebug

protected boolean isDebug()
Checks if debug log level is enabled in both Stopwatch and Log4j.

Returns:
true if debug log level is enabled, false otherwise.

isDebugEnabled

public abstract boolean isDebugEnabled()
Returns:
Returns the debugEnabled.
See Also:
StopwatchEngine.setDebugEnabled(boolean)

getProperties

public java.util.Properties getProperties()
Returns storage properties.

Specified by:
getProperties in interface StopwatchStorage
Returns:
storage properties

setProperties

public void setProperties(java.util.Properties properties)
Called by Stopwatch to set storage properties .

Specified by:
setProperties in interface StopwatchStorage
Parameters:
properties - the properties


Copyright © 2006-2008 Commsen International. All Rights Reserved.