com.boylesoftware.cb2.util.sql
Class StatementWrapper

java.lang.Object
  extended bycom.boylesoftware.cb2.util.sql.StatementWrapper
All Implemented Interfaces:
java.sql.Statement
Direct Known Subclasses:
PreparedStatementWrapper

public class StatementWrapper
extends java.lang.Object
implements java.sql.Statement

Wrapper around a JDBC's Statement interface implementation. You can derive your statement wrapper classes from this one and override some of its methods, while all the others will be calling appropriate methods on the wrapped, "real" statement.

This class also provides some "hook" methods called in certain situations. Note that if your redefine interface methods in your subclass and you use hooks you will have to call the base method from your redefined one in order to make the hooks invoked. Alternatively you call them yourself directly from the redefined interface method.

Version:
$Id: StatementWrapper.java,v 1.4 2003/10/01 22:02:36 levahim Exp $
Author:
Lev Himmelfarb

Field Summary
protected  java.sql.Connection con
          Connection, which will be returned by the getConnection method.
static int EXECUTE_TYPE_BATCH
          Indicates executeBatch call.
static int EXECUTE_TYPE_GENERIC
          Indicates generic execute call.
static int EXECUTE_TYPE_QUERY
          Indicates executeQuery call.
static int EXECUTE_TYPE_UPDATE
          Indicates executeUpdate call.
protected  java.sql.Statement stmt
          The wrapped statement.
 
Fields inherited from interface java.sql.Statement
CLOSE_ALL_RESULTS, CLOSE_CURRENT_RESULT, EXECUTE_FAILED, KEEP_CURRENT_RESULT, NO_GENERATED_KEYS, RETURN_GENERATED_KEYS, SUCCESS_NO_INFO
 
Constructor Summary
StatementWrapper()
          Creates a new wrapper.
StatementWrapper(java.sql.Connection con, java.sql.Statement stmt)
          Creates a new wrapper and sets the wrapped statement to the specified one.
 
Method Summary
 void addBatch(java.lang.String sql)
          Interface method wrapper.
 void cancel()
          Interface method wrapper.
 void clearBatch()
          Interface method wrapper.
 void clearWarnings()
          Interface method wrapper.
 void close()
          Interface method wrapper.
 boolean execute(java.lang.String sql)
          Interface method wrapper.
 boolean execute(java.lang.String sql, int autoGeneratedKeys)
          Interface method wrapper.
 boolean execute(java.lang.String sql, int[] columnIndexes)
          Interface method wrapper.
 boolean execute(java.lang.String sql, java.lang.String[] columnNames)
          Interface method wrapper.
 int[] executeBatch()
          Interface method wrapper.
 java.sql.ResultSet executeQuery(java.lang.String sql)
          Interface method wrapper.
 int executeUpdate(java.lang.String sql)
          Interface method wrapper.
 int executeUpdate(java.lang.String sql, int autoGeneratedKeys)
          Interface method wrapper.
 int executeUpdate(java.lang.String sql, int[] columnIndexes)
          Interface method wrapper.
 int executeUpdate(java.lang.String sql, java.lang.String[] columnNames)
          Interface method wrapper.
 java.sql.Connection getConnection()
          Interface method wrapper.
 int getFetchDirection()
          Interface method wrapper.
 int getFetchSize()
          Interface method wrapper.
 java.sql.ResultSet getGeneratedKeys()
          Interface method wrapper.
 int getMaxFieldSize()
          Interface method wrapper.
 int getMaxRows()
          Interface method wrapper.
 boolean getMoreResults()
          Interface method wrapper.
 boolean getMoreResults(int current)
          Interface method wrapper.
 int getQueryTimeout()
          Interface method wrapper.
 java.sql.ResultSet getResultSet()
          Interface method wrapper.
 int getResultSetConcurrency()
          Interface method wrapper.
 int getResultSetHoldability()
          Interface method wrapper.
 int getResultSetType()
          Interface method wrapper.
 int getUpdateCount()
          Interface method wrapper.
 java.sql.SQLWarning getWarnings()
          Interface method wrapper.
 java.sql.Statement getWrappedStatement()
          Returns the wrapped statement.
protected  void postCallHook()
          This hook is invoked after every interface method call.
protected  void postexecuteHook(java.sql.ResultSet rs, int cnt, boolean isResultSet)
          This hook method is called from all the methods that cause the statement execution after the actuall method on the underlying statement returns.
protected  java.lang.String preexecuteHook(int executeType, java.lang.String sql)
          This hook method is called from all the methods that cause the statement execution before the control is actually passed down to the underlying statement.
 void setConnection(java.sql.Connection con)
          Sets the connection, which will be returned by the getConnection method.
 void setCursorName(java.lang.String name)
          Interface method wrapper.
 void setEscapeProcessing(boolean enable)
          Interface method wrapper.
 void setFetchDirection(int direction)
          Interface method wrapper.
 void setFetchSize(int rows)
          Interface method wrapper.
 void setMaxFieldSize(int max)
          Interface method wrapper.
 void setMaxRows(int max)
          Interface method wrapper.
 void setQueryTimeout(int seconds)
          Interface method wrapper.
 void setWrappedStatement(java.sql.Statement stmt)
          Sets the wrapped statement.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EXECUTE_TYPE_QUERY

public static final int EXECUTE_TYPE_QUERY
Indicates executeQuery call.

See Also:
Constant Field Values

EXECUTE_TYPE_UPDATE

public static final int EXECUTE_TYPE_UPDATE
Indicates executeUpdate call.

See Also:
Constant Field Values

EXECUTE_TYPE_GENERIC

public static final int EXECUTE_TYPE_GENERIC
Indicates generic execute call.

See Also:
Constant Field Values

EXECUTE_TYPE_BATCH

public static final int EXECUTE_TYPE_BATCH
Indicates executeBatch call.

See Also:
Constant Field Values

con

protected java.sql.Connection con
Connection, which will be returned by the getConnection method. If not set, then getConnection on the wrapped statement will be used.


stmt

protected java.sql.Statement stmt
The wrapped statement.

Constructor Detail

StatementWrapper

public StatementWrapper()
Creates a new wrapper. The wrapped statement should be set by a setWrappedStatement call before using the instance.


StatementWrapper

public StatementWrapper(java.sql.Connection con,
                        java.sql.Statement stmt)
Creates a new wrapper and sets the wrapped statement to the specified one.

Parameters:
con - connection, which will be returned by the getConnection method. If null, then getConnection on the wrapped statement will be used.
stmt - the statement to be wrapped.
Method Detail

setConnection

public void setConnection(java.sql.Connection con)
Sets the connection, which will be returned by the getConnection method. If null, then getConnection on the wrapped statement will be used.

Parameters:
con - the connection to set.

setWrappedStatement

public void setWrappedStatement(java.sql.Statement stmt)
Sets the wrapped statement.

Parameters:
stmt - statement to wrap.

getWrappedStatement

public java.sql.Statement getWrappedStatement()
Returns the wrapped statement.

Returns:
the wrapped statement.

executeQuery

public java.sql.ResultSet executeQuery(java.lang.String sql)
                                throws java.sql.SQLException
Interface method wrapper.

Also calles preexecuteHook and postexecuteHook hooks.

Specified by:
executeQuery in interface java.sql.Statement
Throws:
java.sql.SQLException

executeUpdate

public int executeUpdate(java.lang.String sql)
                  throws java.sql.SQLException
Interface method wrapper.

Also calles preexecuteHook and postexecuteHook hooks.

Specified by:
executeUpdate in interface java.sql.Statement
Throws:
java.sql.SQLException

close

public void close()
           throws java.sql.SQLException
Interface method wrapper.

Specified by:
close in interface java.sql.Statement
Throws:
java.sql.SQLException

getMaxFieldSize

public int getMaxFieldSize()
                    throws java.sql.SQLException
Interface method wrapper.

Specified by:
getMaxFieldSize in interface java.sql.Statement
Throws:
java.sql.SQLException

setMaxFieldSize

public void setMaxFieldSize(int max)
                     throws java.sql.SQLException
Interface method wrapper.

Specified by:
setMaxFieldSize in interface java.sql.Statement
Throws:
java.sql.SQLException

getMaxRows

public int getMaxRows()
               throws java.sql.SQLException
Interface method wrapper.

Specified by:
getMaxRows in interface java.sql.Statement
Throws:
java.sql.SQLException

setMaxRows

public void setMaxRows(int max)
                throws java.sql.SQLException
Interface method wrapper.

Specified by:
setMaxRows in interface java.sql.Statement
Throws:
java.sql.SQLException

setEscapeProcessing

public void setEscapeProcessing(boolean enable)
                         throws java.sql.SQLException
Interface method wrapper.

Specified by:
setEscapeProcessing in interface java.sql.Statement
Throws:
java.sql.SQLException

getQueryTimeout

public int getQueryTimeout()
                    throws java.sql.SQLException
Interface method wrapper.

Specified by:
getQueryTimeout in interface java.sql.Statement
Throws:
java.sql.SQLException

setQueryTimeout

public void setQueryTimeout(int seconds)
                     throws java.sql.SQLException
Interface method wrapper.

Specified by:
setQueryTimeout in interface java.sql.Statement
Throws:
java.sql.SQLException

cancel

public void cancel()
            throws java.sql.SQLException
Interface method wrapper.

Specified by:
cancel in interface java.sql.Statement
Throws:
java.sql.SQLException

getWarnings

public java.sql.SQLWarning getWarnings()
                                throws java.sql.SQLException
Interface method wrapper.

Specified by:
getWarnings in interface java.sql.Statement
Throws:
java.sql.SQLException

clearWarnings

public void clearWarnings()
                   throws java.sql.SQLException
Interface method wrapper.

Specified by:
clearWarnings in interface java.sql.Statement
Throws:
java.sql.SQLException

setCursorName

public void setCursorName(java.lang.String name)
                   throws java.sql.SQLException
Interface method wrapper.

Specified by:
setCursorName in interface java.sql.Statement
Throws:
java.sql.SQLException

execute

public boolean execute(java.lang.String sql)
                throws java.sql.SQLException
Interface method wrapper.

Also calles preexecuteHook and postexecuteHook hooks.

Specified by:
execute in interface java.sql.Statement
Throws:
java.sql.SQLException

getResultSet

public java.sql.ResultSet getResultSet()
                                throws java.sql.SQLException
Interface method wrapper.

Specified by:
getResultSet in interface java.sql.Statement
Throws:
java.sql.SQLException

getUpdateCount

public int getUpdateCount()
                   throws java.sql.SQLException
Interface method wrapper.

Specified by:
getUpdateCount in interface java.sql.Statement
Throws:
java.sql.SQLException

getMoreResults

public boolean getMoreResults()
                       throws java.sql.SQLException
Interface method wrapper.

Specified by:
getMoreResults in interface java.sql.Statement
Throws:
java.sql.SQLException

setFetchDirection

public void setFetchDirection(int direction)
                       throws java.sql.SQLException
Interface method wrapper.

Specified by:
setFetchDirection in interface java.sql.Statement
Throws:
java.sql.SQLException

getFetchDirection

public int getFetchDirection()
                      throws java.sql.SQLException
Interface method wrapper.

Specified by:
getFetchDirection in interface java.sql.Statement
Throws:
java.sql.SQLException

setFetchSize

public void setFetchSize(int rows)
                  throws java.sql.SQLException
Interface method wrapper.

Specified by:
setFetchSize in interface java.sql.Statement
Throws:
java.sql.SQLException

getFetchSize

public int getFetchSize()
                 throws java.sql.SQLException
Interface method wrapper.

Specified by:
getFetchSize in interface java.sql.Statement
Throws:
java.sql.SQLException

getResultSetConcurrency

public int getResultSetConcurrency()
                            throws java.sql.SQLException
Interface method wrapper.

Specified by:
getResultSetConcurrency in interface java.sql.Statement
Throws:
java.sql.SQLException

getResultSetType

public int getResultSetType()
                     throws java.sql.SQLException
Interface method wrapper.

Specified by:
getResultSetType in interface java.sql.Statement
Throws:
java.sql.SQLException

addBatch

public void addBatch(java.lang.String sql)
              throws java.sql.SQLException
Interface method wrapper.

Specified by:
addBatch in interface java.sql.Statement
Throws:
java.sql.SQLException

clearBatch

public void clearBatch()
                throws java.sql.SQLException
Interface method wrapper.

Specified by:
clearBatch in interface java.sql.Statement
Throws:
java.sql.SQLException

executeBatch

public int[] executeBatch()
                   throws java.sql.SQLException
Interface method wrapper.

Also calles preexecuteHook and postexecuteHook hooks.

Specified by:
executeBatch in interface java.sql.Statement
Throws:
java.sql.SQLException

getConnection

public java.sql.Connection getConnection()
                                  throws java.sql.SQLException
Interface method wrapper.

Specified by:
getConnection in interface java.sql.Statement
Throws:
java.sql.SQLException

getMoreResults

public boolean getMoreResults(int current)
                       throws java.sql.SQLException
Interface method wrapper.

Specified by:
getMoreResults in interface java.sql.Statement
Throws:
java.sql.SQLException

getGeneratedKeys

public java.sql.ResultSet getGeneratedKeys()
                                    throws java.sql.SQLException
Interface method wrapper.

Specified by:
getGeneratedKeys in interface java.sql.Statement
Throws:
java.sql.SQLException

executeUpdate

public int executeUpdate(java.lang.String sql,
                         int autoGeneratedKeys)
                  throws java.sql.SQLException
Interface method wrapper.

Also calles preexecuteHook and postexecuteHook hooks.

Specified by:
executeUpdate in interface java.sql.Statement
Throws:
java.sql.SQLException

executeUpdate

public int executeUpdate(java.lang.String sql,
                         int[] columnIndexes)
                  throws java.sql.SQLException
Interface method wrapper.

Also calles preexecuteHook and postexecuteHook hooks.

Specified by:
executeUpdate in interface java.sql.Statement
Throws:
java.sql.SQLException

executeUpdate

public int executeUpdate(java.lang.String sql,
                         java.lang.String[] columnNames)
                  throws java.sql.SQLException
Interface method wrapper.

Also calles preexecuteHook and postexecuteHook hooks.

Specified by:
executeUpdate in interface java.sql.Statement
Throws:
java.sql.SQLException

execute

public boolean execute(java.lang.String sql,
                       int autoGeneratedKeys)
                throws java.sql.SQLException
Interface method wrapper.

Also calles preexecuteHook and postexecuteHook hooks.

Specified by:
execute in interface java.sql.Statement
Throws:
java.sql.SQLException

execute

public boolean execute(java.lang.String sql,
                       int[] columnIndexes)
                throws java.sql.SQLException
Interface method wrapper.

Also calles preexecuteHook and postexecuteHook hooks.

Specified by:
execute in interface java.sql.Statement
Throws:
java.sql.SQLException

execute

public boolean execute(java.lang.String sql,
                       java.lang.String[] columnNames)
                throws java.sql.SQLException
Interface method wrapper.

Also calles preexecuteHook and postexecuteHook hooks.

Specified by:
execute in interface java.sql.Statement
Throws:
java.sql.SQLException

getResultSetHoldability

public int getResultSetHoldability()
                            throws java.sql.SQLException
Interface method wrapper.

Specified by:
getResultSetHoldability in interface java.sql.Statement
Throws:
java.sql.SQLException

postCallHook

protected void postCallHook()
                     throws java.sql.SQLException
This hook is invoked after every interface method call. It is not called if the underlying implementation method has thrown an exception. The default implementation does nothing.

Throws:
java.sql.SQLException - to indicate an error in the method.

preexecuteHook

protected java.lang.String preexecuteHook(int executeType,
                                          java.lang.String sql)
                                   throws java.sql.SQLException
This hook method is called from all the methods that cause the statement execution before the control is actually passed down to the underlying statement. The default implementation just returns the passed sql parameter back, but you can override this method in a subclass.

Parameters:
executeType - one of EXECUTE_TYPE_xxx constants indicating the type of the call.
sql - SQL text passed to the execute method by the caller, or null if not applicable (for example it is a prepared statement)
Returns:
SQL text which will be passed to the underlying execute method.
Throws:
java.sql.SQLException - if an error happened.

postexecuteHook

protected void postexecuteHook(java.sql.ResultSet rs,
                               int cnt,
                               boolean isResultSet)
                        throws java.sql.SQLException
This hook method is called from all the methods that cause the statement execution after the actuall method on the underlying statement returns. The default implementation does nothing.

Note that this hook is not called if there was an exception in the underlying statement.

Parameters:
rs - result set returned by the underlying statement or null if not applicable.
cnt - update count returned by the underlying statement or -1 if not applicable.
isResultSet - true if the first result is a ResultSet object, false if it is an update count or there are no results.
Throws:
java.sql.SQLException - if an error happened.


Copyright © 2002,2003,2004 - Boyle Software, Inc.