org.wamblee.support.persistence
Class DatabaseUtils

java.lang.Object
  extended by org.wamblee.support.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>
           
static interface DatabaseUtils.TableSet
           
static interface DatabaseUtils.TableSetOperation
           
 
Constructor Summary
DatabaseUtils(javax.sql.DataSource aDataSource, org.dbunit.dataset.filter.ITableFilterSimple aTables)
          Constructs the database utils.
 
Method Summary
 void cleanDatabase()
           
 void cleanDatabase(org.dbunit.dataset.filter.ITableFilterSimple aSelection)
           
 int countResultSet(java.sql.ResultSet aResultSet)
           
 org.dbunit.IDatabaseTester createDbTester()
           
 org.dbunit.IDatabaseTester createDbTester(java.lang.String[] aTables)
           
 void dropTable(java.lang.String aTable)
           
 void dropTables()
           
 void dropTables(org.dbunit.dataset.filter.ITableFilterSimple aTables)
           
 void emptyTable(java.lang.String aTable)
           
 void emptyTables()
           
 void emptyTables(org.dbunit.dataset.filter.ITableFilterSimple aSelection)
           
<T> T
executeInTransaction(DatabaseUtils.JdbcUnitOfWork<T> aCallback)
           
 void executeOnTables(org.dbunit.dataset.filter.ITableFilterSimple aTables, DatabaseUtils.TableSetOperation aOperation)
           
 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)
           
 java.lang.String[] getTableNames()
           
 java.lang.String[] getTableNames(org.dbunit.dataset.filter.ITableFilterSimple aSelection)
           
 int getTableSize(java.lang.String aTable)
           
 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,
                     org.dbunit.dataset.filter.ITableFilterSimple aTables)
Constructs the database utils. Before use, start() must be called.

Parameters:
aDataSource - Datasource.
aTables - Tables to work with.
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()
                                          throws java.lang.Exception
Throws:
java.lang.Exception

createDbTester

public org.dbunit.IDatabaseTester createDbTester(java.lang.String[] aTables)
                                          throws java.lang.Exception
Throws:
java.lang.Exception

cleanDatabase

public void cleanDatabase()
                   throws java.lang.Exception
Throws:
java.lang.Exception

executeOnTables

public void executeOnTables(org.dbunit.dataset.filter.ITableFilterSimple aTables,
                            DatabaseUtils.TableSetOperation aOperation)
                     throws java.lang.Exception
Throws:
java.lang.Exception

cleanDatabase

public void cleanDatabase(org.dbunit.dataset.filter.ITableFilterSimple aSelection)
                   throws java.lang.Exception
Throws:
java.lang.Exception

executeInTransaction

public <T> T executeInTransaction(DatabaseUtils.JdbcUnitOfWork<T> aCallback)
                       throws java.lang.Exception
Throws:
java.lang.Exception

getTableNames

public java.lang.String[] getTableNames()
                                 throws java.lang.Exception
Throws:
java.lang.Exception

getTableNames

public java.lang.String[] getTableNames(org.dbunit.dataset.filter.ITableFilterSimple aSelection)
                                 throws java.lang.Exception
Throws:
java.sql.SQLException
java.lang.Exception

emptyTables

public void emptyTables()
                 throws java.lang.Exception
Throws:
java.lang.Exception

emptyTables

public void emptyTables(org.dbunit.dataset.filter.ITableFilterSimple aSelection)
                 throws java.lang.Exception
Throws:
java.sql.SQLException
java.lang.Exception

emptyTable

public void emptyTable(java.lang.String aTable)
                throws java.lang.Exception
Throws:
java.sql.SQLException
java.lang.Exception

dropTables

public void dropTables()
                throws java.lang.Exception
Throws:
java.lang.Exception

dropTables

public void dropTables(org.dbunit.dataset.filter.ITableFilterSimple aTables)
                throws java.lang.Exception
Throws:
java.lang.Exception

dropTable

public void dropTable(java.lang.String aTable)
               throws java.lang.Exception
Throws:
java.sql.SQLException
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)

getTableSize

public int getTableSize(java.lang.String aTable)
                 throws java.lang.Exception
Returns:
Throws:
java.sql.SQLException
java.lang.Exception

countResultSet

public int countResultSet(java.sql.ResultSet aResultSet)
                   throws java.sql.SQLException
Throws:
java.sql.SQLException


Copyright © 2010. All Rights Reserved.