de.mathema.pride
Class StoredProcedure

java.lang.Object
  extended by de.mathema.pride.StoredProcedure

public abstract class StoredProcedure
extends java.lang.Object

This class simplifies calling stored procedures from Java code. Derived types just have to declare the procedure's parameters as public members. Input parameters must be declared final, output parameters as non-final members. In/out parameters are not supported because I didn't find an elegant way to do yet. The procedure is called by running the execute(java.sql.Connection) method. E.g. the following class would run the procedure myproc with two input Strings and one output integer:

   public class myproc extends StoredProcedure {

     public final String p1;
     public final String p2;
     public int p3;

     public myproc(String p1, String p2) {
       this.p1 = p1;
       this.p2 = p2;
     }
   }
 
The SQL types of the parameters are derived from the member's Java types. Currently there are the following types supported:

Author:
Jan Lessner

Field Summary
protected  Database db
           
static java.lang.String REVISION_ID
           
 
Constructor Summary
StoredProcedure()
           
 
Method Summary
protected  java.lang.String assembleCallString()
           
 void execute(java.sql.Connection con)
          Executes the stored procedure represented by this object on the passed database connection.
 void execute(Database db)
          Executes the stored procedure represented by this object on the passed database.
 java.lang.String format(java.lang.Object value)
          Format a value as string.
 Database getDatabase()
          Returns a database required by formatting and logging functions.
protected  java.lang.String getName()
          Returns the name of the stored procedure to call.
protected  int getNumParams()
          Returns the number of parameters for the SP call.
protected  java.lang.String getOutParam(java.sql.CallableStatement stmt, java.lang.reflect.Field field, int index)
           
protected  java.lang.String getOutParams(java.sql.CallableStatement stmt)
           
protected  int getSQLType(java.lang.Class type)
          Returns the SQL type equivalent to the passed Java type
 boolean isLogging()
          Returns true if the execution of the stored procedure should be logged.
 void log(java.lang.String operation)
          Called by the execution method when logging is enabled (see isLogging()) to write an execution string to log media.
protected  java.lang.String setInParam(java.sql.CallableStatement stmt, java.lang.reflect.Field field, int index)
          Sets an input values for an SP call
protected  java.lang.String setOutParamType(java.sql.CallableStatement stmt, java.lang.reflect.Field field, int index)
          Sets an output value types for an SP call
protected  java.lang.String setParams(java.sql.CallableStatement stmt)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

db

protected Database db

REVISION_ID

public static final java.lang.String REVISION_ID
See Also:
Constant Field Values
Constructor Detail

StoredProcedure

public StoredProcedure()
Method Detail

getDatabase

public Database getDatabase()
Returns a database required by formatting and logging functions. If the execute(java.sql.Connection) method was called with a Database object. This object is returned, otherwise DatabaseFactory.getDatabase()


isLogging

public boolean isLogging()
Returns true if the execution of the stored procedure should be logged. The function returns getDatabase().isLogging() by default and can be overridden in derived classes if required.


log

public void log(java.lang.String operation)
Called by the execution method when logging is enabled (see isLogging()) to write an execution string to log media. This function calls getDatabase().sqlLog() by default and can be overridden in derived classes if required.


format

public java.lang.String format(java.lang.Object value)
Format a value as string. This is only used for logging purposes in this class


getName

protected java.lang.String getName()
Returns the name of the stored procedure to call. This is by default the name of the derived class (without package).


getNumParams

protected int getNumParams()
Returns the number of parameters for the SP call. This is extracted from counting all non-static, public data members of the class.


setInParam

protected java.lang.String setInParam(java.sql.CallableStatement stmt,
                                      java.lang.reflect.Field field,
                                      int index)
                               throws IllegalDescriptorException,
                                      java.sql.SQLException
Sets an input values for an SP call

Parameters:
stmt - The callable statement, representing the SP to call
field - The data member holding the value to set
index - The parameter's index in the statement's signature, beginning with index 1.
Throws:
IllegalDescriptorException
java.sql.SQLException

getSQLType

protected int getSQLType(java.lang.Class type)
                  throws IllegalDescriptorException
Returns the SQL type equivalent to the passed Java type

Throws:
IllegalDescriptorException

setOutParamType

protected java.lang.String setOutParamType(java.sql.CallableStatement stmt,
                                           java.lang.reflect.Field field,
                                           int index)
                                    throws IllegalDescriptorException,
                                           java.sql.SQLException
Sets an output value types for an SP call

Parameters:
stmt - The callable statement, representing the SP to call
field - The data member to derive the type from.
index - The parameter's index in the statement's signature, beginning with index 1.
Throws:
IllegalDescriptorException
java.sql.SQLException

setParams

protected java.lang.String setParams(java.sql.CallableStatement stmt)
                              throws IllegalDescriptorException,
                                     java.sql.SQLException
Throws:
IllegalDescriptorException
java.sql.SQLException

getOutParam

protected java.lang.String getOutParam(java.sql.CallableStatement stmt,
                                       java.lang.reflect.Field field,
                                       int index)
                                throws IllegalDescriptorException,
                                       java.sql.SQLException
Throws:
IllegalDescriptorException
java.sql.SQLException

getOutParams

protected java.lang.String getOutParams(java.sql.CallableStatement stmt)
                                 throws IllegalDescriptorException,
                                        java.sql.SQLException
Throws:
IllegalDescriptorException
java.sql.SQLException

assembleCallString

protected java.lang.String assembleCallString()

execute

public void execute(java.sql.Connection con)
             throws java.sql.SQLException,
                    IllegalDescriptorException
Executes the stored procedure represented by this object on the passed database connection. If logging is enabled, ths functions writes an appropriate string to the log media.

Throws:
java.sql.SQLException - if the stored procedure call failed
{@link - IllegalDescriptorException} if the stored procedure class is somehow malformed.
IllegalDescriptorException

execute

public void execute(Database db)
             throws java.sql.SQLException,
                    IllegalDescriptorException
Executes the stored procedure represented by this object on the passed database. If logging is enabled, the functions writes an appropriate string to the log media.

Throws:
java.sql.SQLException - if the stored procedure call failed
{@link - IllegalDescriptorException} if the stored procedure class is somehow malformed.
IllegalDescriptorException