org.unitils.core.dbsupport
Class DefaultSQLHandler

java.lang.Object
  extended by org.unitils.core.dbsupport.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(DataSource dataSource)
          Constructs a new instance that connects to the given DataSource
DefaultSQLHandler(DataSource dataSource, boolean doExecuteUpdates)
          Constructs a new instance that connects to the given DataSource
 
Method Summary
 void executeQuery(String sql)
          Executes the given query.
 int executeUpdate(String sql)
          Executes the given statement.
 int executeUpdateAndCommit(String sql)
          Executes the given statement and commits.
 boolean exists(String sql)
          Returns true if the query returned a record.
 DataSource getDataSource()
           
 long getItemAsLong(String sql)
          Returns the long extracted from the result of the given query.
 String getItemAsString(String sql)
          Returns the value extracted from the result of the given query.
 Set<String> getItemsAsStringSet(String sql)
          Returns the items extracted from the result of the given query.
 boolean isDoExecuteUpdates()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultSQLHandler

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

Parameters:
dataSource - The data source, not null

DefaultSQLHandler

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

Parameters:
dataSource - The data source, not null
doExecuteUpdates - Boolean indicating whether updates should effectively be executed on the underlying database
Method Detail

executeUpdate

public int executeUpdate(String sql)
Description copied from interface: SQLHandler
Executes the given statement.

Specified by:
executeUpdate in interface SQLHandler
Parameters:
sql - The sql statement
Returns:
The nr of updates

executeQuery

public void executeQuery(String sql)
Description copied from interface: SQLHandler
Executes the given query. Note that no result is returned: this method is only useful in case you want to execute a query that has some desired side-effect (in fact, this method perfoms an update which is disguised as a query ;-) )

Specified by:
executeQuery in interface SQLHandler
Parameters:
sql - The sql query

executeUpdateAndCommit

public int executeUpdateAndCommit(String sql)
Description copied from interface: SQLHandler
Executes the given statement and commits.

Specified by:
executeUpdateAndCommit in interface SQLHandler
Parameters:
sql - The sql statement
Returns:
The nr of updates

getItemAsLong

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

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

getItemAsString

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

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

getItemsAsStringSet

public Set<String> getItemsAsStringSet(String sql)
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
Returns:
The items, not null

exists

public boolean exists(String sql)
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
Returns:
True if a record was returned

getDataSource

public DataSource getDataSource()
Specified by:
getDataSource in interface SQLHandler
Returns:
The DataSource

isDoExecuteUpdates

public boolean isDoExecuteUpdates()
Specified by:
isDoExecuteUpdates in interface SQLHandler
Returns:
Whether updates are executed on the database or not


Copyright © 2011. All Rights Reserved.