net.sourceforge.sqlunit.handlers
Class SqlHandler

java.lang.Object
  extended by net.sourceforge.sqlunit.handlers.SqlHandler
All Implemented Interfaces:
IHandler
Direct Known Subclasses:
CallHandler, DynamicSqlHandler, FuncHandler

public class SqlHandler
extends java.lang.Object
implements IHandler

The SqlHandler class processes the contents of an sql tag in the input XML file.

Version:
$Revision: 1.23 $
Author:
Sujit Pal (spal@users.sourceforge.net)

Constructor Summary
SqlHandler()
           
 
Method Summary
protected  java.sql.Connection acquireConnection(java.lang.String connectionId)
          Acquires a Connection object with the specified connectionId.
protected  java.sql.Statement acquireStatementObject(java.sql.Connection conn, java.lang.String stmt)
          Builds the Statement object from the Connection and the SQL string.
protected  void clrOutputParameters(java.sql.Statement ps, Param[] params)
          Clears output parameter symbol table variables.
protected  void executeQuery(java.sql.Statement ps, DatabaseResult result)
          Executes the SQL query with a prefilled Statement object, and populates the DatabaseResult object in place.
protected  java.lang.Object executeSQL(java.lang.String connectionId, java.lang.String stmt, Param[] params)
          Executes JDBC calls to build the DatabaseResult object and returns it.
protected  DatabaseResult initDatabaseResult()
          Initializes a DatabaseResult object for use by the Statement after it finishes executing.
protected  boolean isMoreResultsAvailable(boolean hasResults, int rowsUpdated)
          Returns whether there are more resultsets to loop through in the returned result from the SQL call.
 java.lang.Object process(org.jdom.Element elSql)
          Runs the SQL statement contained in the sql tag in the input XML file.
protected  void setOutputParameters(java.sql.Statement ps, Param[] params, DatabaseResult result)
          Sets the output parameters into the DatabaseResult.
protected  java.sql.Statement setParameters(java.sql.Statement ps, Param[] params)
          Sets replaceable parameters, if any, into the Statement object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SqlHandler

public SqlHandler()
Method Detail

process

public java.lang.Object process(org.jdom.Element elSql)
                         throws java.lang.Exception
Runs the SQL statement contained in the sql tag in the input XML file.

Specified by:
process in interface IHandler
Parameters:
elSql - the JDOM Element representing the sql tag.
Returns:
a DatabaseResult created as a result of running the SQL.
Throws:
java.lang.Exception - if there was a problem running the SQL.

executeSQL

protected java.lang.Object executeSQL(java.lang.String connectionId,
                                      java.lang.String stmt,
                                      Param[] params)
                               throws java.lang.Exception
Executes JDBC calls to build the DatabaseResult object and returns it.

Parameters:
connectionId - the connection id string.
stmt - the SQL statement.
params - an array of Param objects.
Returns:
a DatabaseResult object.
Throws:
java.lang.Exception - if there was a problem building the DatabaseResult.

acquireConnection

protected java.sql.Connection acquireConnection(java.lang.String connectionId)
                                         throws java.lang.Exception
Acquires a Connection object with the specified connectionId.

Parameters:
connectionId - the connectionId to look up the ConnectionRegistry.
Returns:
a Connection object.
Throws:
java.lang.Exception - if there was a problem getting the Connection.

acquireStatementObject

protected java.sql.Statement acquireStatementObject(java.sql.Connection conn,
                                                    java.lang.String stmt)
                                             throws java.lang.Exception
Builds the Statement object from the Connection and the SQL string. This is meant to be overriden on a class by class basis, so for example, since we know that we will be dealing with a PreparedStatement in this handler, we will override this method to build and return a PreparedStatement object.

Parameters:
conn - the Connection object.
stmt - the SQL string.
Returns:
a Statement object.
Throws:
java.lang.Exception - if there was a problem building the Statement.

setParameters

protected java.sql.Statement setParameters(java.sql.Statement ps,
                                           Param[] params)
                                    throws java.lang.Exception
Sets replaceable parameters, if any, into the Statement object. Note that if there are no param elements, then an empty Param[] must be supplied. Updates the Statement object in place.

Parameters:
ps - the Statement object.
params - an array of Param objects.
Returns:
a Statement object which should be cast to PreparedStatement.
Throws:
java.lang.Exception - if there was a problem setting parameters.

initDatabaseResult

protected DatabaseResult initDatabaseResult()
Initializes a DatabaseResult object for use by the Statement after it finishes executing.

Returns:
a new empty DatabaseResult object.

executeQuery

protected void executeQuery(java.sql.Statement ps,
                            DatabaseResult result)
                     throws java.lang.Exception
Executes the SQL query with a prefilled Statement object, and populates the DatabaseResult object in place.

Parameters:
ps - the Statement object.
result - the DatabaseResult object.
Throws:
java.lang.Exception - if there was a problem in the execute().

isMoreResultsAvailable

protected boolean isMoreResultsAvailable(boolean hasResults,
                                         int rowsUpdated)
Returns whether there are more resultsets to loop through in the returned result from the SQL call. Since we are dealing with a PreparedStatement, this will always be false.

Parameters:
hasResults - a boolean generated from ps.execute().
rowsUpdated - an int generated from ps.getUpdateCount().
Returns:
true if we should look for more results in this call.

setOutputParameters

protected void setOutputParameters(java.sql.Statement ps,
                                   Param[] params,
                                   DatabaseResult result)
                            throws java.lang.Exception
Sets the output parameters into the DatabaseResult. This is a no-op in the case of this handler, but will contain code for the CallHandler.

Parameters:
ps - the Statement object.
params - the array of Param objects.
result - the DatabaseResult object.
Throws:
java.lang.Exception - if one is thrown by this method.

clrOutputParameters

protected void clrOutputParameters(java.sql.Statement ps,
                                   Param[] params)
                            throws java.lang.Exception
Clears output parameter symbol table variables. This is a no-op in case of this handler, but will contain code for the CallHandler.

Parameters:
ps - the Statement object.
params - the array of Param objects.
Throws:
java.lang.Exception - if one is thrown by this method.