org.wamblee.test.persistence
Class DatabaseUtils

java.lang.Object
  extended by org.wamblee.test.persistence.DatabaseUtils

public class DatabaseUtils
extends java.lang.Object

Database utilities is a simple support class for common tasks in working with databases.


Nested Class Summary
static interface DatabaseUtils.JdbcUnitOfWork<T>
          Represents a unit of work (transaction).
static interface DatabaseUtils.TableSet
          Represents a set of tables.
static interface DatabaseUtils.TableSetOperation
          Operation to be executed on a set of tables for each table individually.
 
Constructor Summary
DatabaseUtils(javax.sql.DataSource aDataSource)
          Constructs the database utils.
 
Method Summary
 void cleanDatabase(org.dbunit.dataset.filter.ITableFilterSimple aSelection)
          Cleans a number of database tables.
 int countResultSet(java.sql.ResultSet aResultSet)
          Counts the results in a result set.
 org.dbunit.IDatabaseTester createDbTester(org.dbunit.dataset.filter.ITableFilterSimple aTables)
          Creates database tester.
 org.dbunit.IDatabaseTester createDbTester(java.lang.String[] aTables)
          Creates database tester.
 void dropTable(java.lang.String aTable)
          Drops a table.
 void dropTables(org.dbunit.dataset.filter.ITableFilterSimple aTables)
          Drops tables.
 void emptyTable(java.lang.String aTable)
          Deprecated. 
 void emptyTables(org.dbunit.dataset.filter.ITableFilterSimple aSelection)
          Deprecated. 
<T> T
executeInTransaction(DatabaseUtils.JdbcUnitOfWork<T> aWork)
          Executes a unit of work within a transaction.
 void executeOnTables(org.dbunit.dataset.filter.ITableFilterSimple aTables, DatabaseUtils.TableSetOperation aOperation)
          Executes an operation on a set of tables.
 java.sql.ResultSet executeQuery(java.sql.Connection aConnection, java.lang.String aSql)
          Executes an SQL query.
 java.sql.ResultSet executeQuery(java.sql.Connection aConnection, java.lang.String aSql, java.lang.Object aArg)
          Executes a query with a single argument.
 java.sql.ResultSet executeQuery(java.sql.Connection aConnection, java.lang.String aSql, java.lang.Object[] aArgs)
          Executes a query.
 int executeSql(java.lang.String aSql)
          Executes an SQL statement within a transaction.
 int executeSql(java.lang.String aSql, java.lang.Object aArg)
          Executes an SQL statement within a transaction.
 int executeSql(java.lang.String aSql, java.lang.Object[] aArgs)
          Executes an sql statement.
 int executeUpdate(java.sql.Connection aConnection, java.lang.String aSql, java.lang.Object... aArgs)
          Executes an update.
 java.lang.String[] getTableNames(org.dbunit.dataset.filter.ITableFilterSimple aSelection)
          Returns table names based on a table filter.
 int getTableSize(java.lang.String aTable)
          Gets the table size.
 void start()
          Starts the database utils.
 void stop()
          Stops the database utils, closing any JDBC connections that were created by this utility.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DatabaseUtils

public DatabaseUtils(javax.sql.DataSource aDataSource)
Constructs the database utils. Before use, start() must be called.

Parameters:
aDataSource - Datasource.
Method Detail

start

public void start()
Starts the database utils.


stop

public void stop()
Stops the database utils, closing any JDBC connections that were created by this utility. Note that connections obtained from the datasource directly must still be closed by the user. The involved connections are only those that are created by this utility.


createDbTester

public org.dbunit.IDatabaseTester createDbTester(org.dbunit.dataset.filter.ITableFilterSimple aTables)
                                          throws java.lang.Exception
Creates database tester.

Parameters:
aTables - Tables to create the tester for.
Returns:
Database tester.
Throws:
java.lang.Exception

createDbTester

public org.dbunit.IDatabaseTester createDbTester(java.lang.String[] aTables)
                                          throws java.lang.Exception
Creates database tester.

Parameters:
aTables - Tables to create the tester for.
Returns:
Database tester.
Throws:
java.lang.Exception

executeOnTables

public void executeOnTables(org.dbunit.dataset.filter.ITableFilterSimple aTables,
                            DatabaseUtils.TableSetOperation aOperation)
                     throws java.lang.Exception
Executes an operation on a set of tables.

Parameters:
aTables - Tables.
aOperation - Operation.
Throws:
java.lang.Exception

cleanDatabase

public void cleanDatabase(org.dbunit.dataset.filter.ITableFilterSimple aSelection)
                   throws java.lang.Exception
Cleans a number of database tables. This means deleting the content not dropping the tables. This may fail in case of cyclic dependencies between the tables (current limitation).

Parameters:
aSelection - Tables.
Throws:
java.lang.Exception

executeInTransaction

public <T> T executeInTransaction(DatabaseUtils.JdbcUnitOfWork<T> aWork)
                       throws java.lang.Exception
Executes a unit of work within a transaction.

Type Parameters:
T - Result type of th ework.
Parameters:
aWork - Unit of work.
Returns:
Throws:
java.lang.Exception

getTableNames

public java.lang.String[] getTableNames(org.dbunit.dataset.filter.ITableFilterSimple aSelection)
                                 throws java.lang.Exception
Returns table names based on a table filter.

Parameters:
aSelection - Table filter.
Returns:
Table names.
Throws:
java.lang.Exception

emptyTables

@Deprecated
public void emptyTables(org.dbunit.dataset.filter.ITableFilterSimple aSelection)
                 throws java.lang.Exception
Deprecated. 

Use cleanDatabase(ITableFilterSimple) instead.

Throws:
java.lang.Exception

emptyTable

@Deprecated
public void emptyTable(java.lang.String aTable)
                throws java.lang.Exception
Deprecated. 

User cleanDatabase(ITableFilterSimple) instead.

Throws:
java.lang.Exception

dropTables

public void dropTables(org.dbunit.dataset.filter.ITableFilterSimple aTables)
                throws java.lang.Exception
Drops tables. This only works if there are no cyclic dependencies between the tables.

Parameters:
aTables - Tables to drop.
Throws:
java.lang.Exception

dropTable

public void dropTable(java.lang.String aTable)
               throws java.lang.Exception
Drops a table.

Parameters:
aTable - Table to drop.
Throws:
java.lang.Exception

executeSql

public int executeSql(java.lang.String aSql)
               throws java.lang.Exception
Executes an SQL statement within a transaction.

Parameters:
aSql - SQL statement.
Returns:
Return code of the corresponding JDBC call.
Throws:
java.lang.Exception

executeSql

public int executeSql(java.lang.String aSql,
                      java.lang.Object aArg)
               throws java.lang.Exception
Executes an SQL statement within a transaction. See setPreparedParam(int, PreparedStatement, Object)for details on supported argument types.

Parameters:
aSql - SQL statement.
aArg - Argument of the sql statement.
Returns:
Return code of the corresponding JDBC call.
Throws:
java.lang.Exception

executeSql

public int executeSql(java.lang.String aSql,
                      java.lang.Object[] aArgs)
               throws java.lang.Exception
Executes an sql statement. See setPreparedParam(int, PreparedStatement, Object)for details on supported argument types.

Parameters:
aSql - SQL query to execute.
aArgs - Arguments.
Returns:
Number of rows updated.
Throws:
java.lang.Exception

executeQuery

public java.sql.ResultSet executeQuery(java.sql.Connection aConnection,
                                       java.lang.String aSql)
Executes an SQL query.

Parameters:
aSql - Query to execute.
Returns:
Result set.

executeQuery

public java.sql.ResultSet executeQuery(java.sql.Connection aConnection,
                                       java.lang.String aSql,
                                       java.lang.Object aArg)
Executes a query with a single argument. See setPreparedParam(int, PreparedStatement, Object)for details on supported argument types.

Parameters:
aSql - Query.
aArg - Argument.
Returns:
Result set.

executeQuery

public java.sql.ResultSet executeQuery(java.sql.Connection aConnection,
                                       java.lang.String aSql,
                                       java.lang.Object[] aArgs)
Executes a query. See setPreparedParam(int, PreparedStatement, Object)for details on supported argument types.

Parameters:
aSql - Sql query.
aArgs - Arguments to the query.
Returns:
Result set.

executeUpdate

public int executeUpdate(java.sql.Connection aConnection,
                         java.lang.String aSql,
                         java.lang.Object... aArgs)
Executes an update.

Parameters:
aConnection - Connection to use.
aSql - SQL update to use.
aArgs - Arguments to the update.
Returns:
Number of rows updated.

getTableSize

public int getTableSize(java.lang.String aTable)
                 throws java.lang.Exception
Gets the table size.

Parameters:
aTable - Table.
Returns:
Table size.
Throws:
java.sql.SQLException
java.lang.Exception

countResultSet

public int countResultSet(java.sql.ResultSet aResultSet)
                   throws java.sql.SQLException
Counts the results in a result set.

Parameters:
aResultSet - Resultset.
Returns:
Number of rows in the set.
Throws:
java.sql.SQLException


Copyright © 2010. All Rights Reserved.