org.dbmaintain.database.impl
Class DefaultSQLHandler

java.lang.Object
  extended by org.dbmaintain.database.impl.DefaultSQLHandler
All Implemented Interfaces:
SQLHandler

public class DefaultSQLHandler
extends Object
implements SQLHandler

Class to which database updates and queries are passed. Is in fact a utility class, but is a concrete instance to enable decorating it or switching it with another implementation, allowing things like a dry run, creating a script file or logging updates to a log file or database table.

Author:
Filip Neven, Tim Ducheyne

Constructor Summary
DefaultSQLHandler()
          Constructs a new instance that connects to the given DataSource
DefaultSQLHandler(boolean doExecuteUpdates)
          Constructs a new instance that connects to the given DataSource
 
Method Summary
 void closeAllConnections()
          Closes all connections that were created and cached by this SQLHandler.
 void endTransactionAndCommit(DataSource dataSource)
          Ends a transaction that was started using startTransaction by committing and turning auto commit back on.
 void endTransactionAndRollback(DataSource dataSource)
          Ends a transaction that was started using startTransaction by rolling back and turning auto commit back on.
 void execute(String sql, DataSource dataSource)
          Executes the given statement.
 int executeUpdateAndCommit(String sql, DataSource dataSource)
          Executes the given statement and commits the changes to the database
 boolean exists(String sql, DataSource dataSource)
          Returns true if the query returned a record.
protected  Connection getConnection(DataSource dataSource)
          Returns a Connection to the given DataSource.
 long getItemAsLong(String sql, DataSource dataSource)
          Returns the long extracted from the result of the given query.
 String getItemAsString(String sql, DataSource dataSource)
          Returns the value extracted from the result of the given query.
 Set<String> getItemsAsStringSet(String sql, DataSource dataSource)
          Returns the items extracted from the result of the given query.
 void startTransaction(DataSource dataSource)
          Starts a transaction by turning of auto commit.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultSQLHandler

public DefaultSQLHandler()
Constructs a new instance that connects to the given DataSource


DefaultSQLHandler

public DefaultSQLHandler(boolean doExecuteUpdates)
Constructs a new instance that connects to the given DataSource

Parameters:
doExecuteUpdates - Boolean indicating whether updates should effectively be executed on the underlying database
Method Detail

execute

public void execute(String sql,
                    DataSource dataSource)
Description copied from interface: SQLHandler
Executes the given statement.

Specified by:
execute in interface SQLHandler
Parameters:
sql - The sql string for retrieving the items
dataSource - The dataSource, not null

executeUpdateAndCommit

public int executeUpdateAndCommit(String sql,
                                  DataSource dataSource)
Description copied from interface: SQLHandler
Executes the given statement and commits the changes to the database

Specified by:
executeUpdateAndCommit in interface SQLHandler
Parameters:
sql - The sql string for retrieving the items
dataSource - The dataSource, not null
Returns:
The nr of updates

getItemAsLong

public long getItemAsLong(String sql,
                          DataSource dataSource)
Description copied from interface: SQLHandler
Returns the long extracted from the result of the given query. If no value is found, a DbMaintainException is thrown.

Specified by:
getItemAsLong in interface SQLHandler
Parameters:
sql - The sql string for retrieving the items
dataSource - The dataSource, not null
Returns:
The long item value

getItemAsString

public String getItemAsString(String sql,
                              DataSource dataSource)
Description copied from interface: SQLHandler
Returns the value extracted from the result of the given query. If no value is found, a DbMaintainException is thrown.

Specified by:
getItemAsString in interface SQLHandler
Parameters:
sql - The sql string for retrieving the items
dataSource - The dataSource, not null
Returns:
The string item value

getItemsAsStringSet

public Set<String> getItemsAsStringSet(String sql,
                                       DataSource dataSource)
Description copied from interface: SQLHandler
Returns the items extracted from the result of the given query.

Specified by:
getItemsAsStringSet in interface SQLHandler
Parameters:
sql - The sql string for retrieving the items
dataSource - The dataSource, not null
Returns:
The items, not null

exists

public boolean exists(String sql,
                      DataSource dataSource)
Description copied from interface: SQLHandler
Returns true if the query returned a record.

Specified by:
exists in interface SQLHandler
Parameters:
sql - The sql string for checking the existence
dataSource - The dataSource, not null
Returns:
True if a record was returned

startTransaction

public void startTransaction(DataSource dataSource)
Starts a transaction by turning of auto commit. Make sure to call endTransaction at the end of the transaction

Specified by:
startTransaction in interface SQLHandler
Parameters:
dataSource - The data source, not null

endTransactionAndCommit

public void endTransactionAndCommit(DataSource dataSource)
Ends a transaction that was started using startTransaction by committing and turning auto commit back on.

Specified by:
endTransactionAndCommit in interface SQLHandler
Parameters:
dataSource - The data source, not null

endTransactionAndRollback

public void endTransactionAndRollback(DataSource dataSource)
Ends a transaction that was started using startTransaction by rolling back and turning auto commit back on.

Specified by:
endTransactionAndRollback in interface SQLHandler
Parameters:
dataSource - The data source, not null

closeAllConnections

public void closeAllConnections()
Closes all connections that were created and cached by this SQLHandler. This method must always be invoked before disposing this object.

Specified by:
closeAllConnections in interface SQLHandler

getConnection

protected Connection getConnection(DataSource dataSource)
Returns a Connection to the given DataSource. The first time a Connection is requested, a new one is created using the given DataSource. All subsequent calls with the same DataSource as parameter will return the same Connection instance.

Parameters:
dataSource - provides access to the database
Returns:
a Connection to the database for the given DataSource.


Copyright © 2011. All Rights Reserved.