com.interface21.jdbc.object
Class SqlFunction

java.lang.Object
  |
  +--com.interface21.jdbc.object.RdbmsOperation
        |
        +--com.interface21.jdbc.object.SqlOperation
              |
              +--com.interface21.jdbc.object.SqlQuery
                    |
                    +--com.interface21.jdbc.object.MappingSqlQueryWithParameters
                          |
                          +--com.interface21.jdbc.object.MappingSqlQuery
                                |
                                +--com.interface21.jdbc.object.SqlFunction
All Implemented Interfaces:
InitializingBean

public class SqlFunction
extends MappingSqlQuery

SQL function wrapper that returns a single row of results. The default behavior is to return an int, but that can be overridden by using the methods with an extra return type parameter.Intended to use to call SQL functions.
This is a concrete class, which there is normally no need to subclass. Code using this package can create an object of this type, declaring SQL and parameters, and then invoke the appropriate run() method repeatedly to execute the function.
Like all RdbmsOperation objects, SqlFunction objects are threadsafe.

Author:
Rod Johnson, Isabelle Muszynski, Jean-Pierre Pawlak History : 20/04/2003 : modified by IM to allow returning something else than an int 09/05/2003 : modified by JPP, added case Types.BIGINT in function extract

Fields inherited from class com.interface21.jdbc.object.RdbmsOperation
logger
 
Constructor Summary
SqlFunction()
          Constructor to allow use as a JavaBean.
SqlFunction(javax.sql.DataSource ds, java.lang.String sql)
          Create a new SQLFunction object with SQL, but without parameters.
SqlFunction(javax.sql.DataSource ds, java.lang.String sql, int retType)
          Create a new SQLFunction object with SQL and return type, but without parameters.
SqlFunction(javax.sql.DataSource ds, java.lang.String sql, int[] types)
          Create a new SQLFunction object with SQL and parameters.
SqlFunction(javax.sql.DataSource ds, java.lang.String sql, int[] types, int retType)
          Create a new SQLFunction object with SQL, parameters and a return type
 
Method Summary
protected  java.lang.Object mapRow(java.sql.ResultSet rs, int rownum)
          This implementation of this method extracts a single value from the single row returned by the function.
 int run()
          Convenient method to run the function without arguments
 int run(int p)
          Convenient method to run the function with a single int argument
 int run(java.lang.Object[] args)
          Analogous to the SqlQuery.execute([]) method.
 java.lang.Object runGeneric()
          Convenient method to run the function without arguments, returning the value as an object
 java.lang.Object runGeneric(int p)
          Convenient method to run the function with a single int argument
 java.lang.Object runGeneric(java.lang.Object[] args)
          Analogous to the SqlQuery.execute([]) method.
 
Methods inherited from class com.interface21.jdbc.object.MappingSqlQuery
mapRow
 
Methods inherited from class com.interface21.jdbc.object.MappingSqlQueryWithParameters
newResultReader
 
Methods inherited from class com.interface21.jdbc.object.SqlQuery
execute, execute, execute, execute, execute, findObject, findObject, findObject, findObject, getRowsExpected, onCompileInternal, setRowsExpected
 
Methods inherited from class com.interface21.jdbc.object.SqlOperation
compileInternal, getJdbcTemplate, newPreparedStatementCreator
 
Methods inherited from class com.interface21.jdbc.object.RdbmsOperation
afterPropertiesSet, compile, declareParameter, getDataSource, getDeclaredParameters, getSql, isCompiled, setDataSource, setSql, setTypes, validateParameters
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SqlFunction

public SqlFunction()
Constructor to allow use as a JavaBean. A DataSource, SQL and any parameters must be supplied before invoking the compile() method and using this object.

SqlFunction

public SqlFunction(javax.sql.DataSource ds,
                   java.lang.String sql,
                   int[] types)
Create a new SQLFunction object with SQL and parameters.
Parameters:
ds - DataSource to obtain connections from
sql - SQL to execute
types - SQL types of the parameters, as defined in the java.sql.Types class

SqlFunction

public SqlFunction(javax.sql.DataSource ds,
                   java.lang.String sql,
                   int[] types,
                   int retType)
            throws InvalidDataAccessApiUsageException
Create a new SQLFunction object with SQL, parameters and a return type
Parameters:
ds - DataSource to obtain connections from
sql - SQL to execute
types - SQL types of the parameters, as defined in the java.sql.Types class
retType - SQL type of the return value, as defined in the java.sql.Types class
Throws:
InvalidDataAccessApiUsageException - is thrown if the return type is not numeric or char

SqlFunction

public SqlFunction(javax.sql.DataSource ds,
                   java.lang.String sql)
Create a new SQLFunction object with SQL, but without parameters. Must add parameters or settle with none.
Parameters:
ds - DataSource to obtain connections from
sql - SQL to execute

SqlFunction

public SqlFunction(javax.sql.DataSource ds,
                   java.lang.String sql,
                   int retType)
Create a new SQLFunction object with SQL and return type, but without parameters. Must add parameters or settle with none.
Parameters:
ds - DataSource to obtain connections from
sql - SQL to execute
retType - SQL type of the return value, as defined in the java.sql.Types class
Method Detail

mapRow

protected java.lang.Object mapRow(java.sql.ResultSet rs,
                                  int rownum)
                           throws java.sql.SQLException,
                                  InvalidDataAccessApiUsageException
This implementation of this method extracts a single value from the single row returned by the function. If there are a different number of rows returned, this is treated as an error.
Overrides:
mapRow in class MappingSqlQuery
See Also:
ManualExtractionSqlQuery#extract(ResultSet, int)

run

public int run()
Convenient method to run the function without arguments
Returns:
the value of the function

run

public int run(int p)
Convenient method to run the function with a single int argument
Parameters:
p - single int argument
Returns:
the value of the function

run

public int run(java.lang.Object[] args)
Analogous to the SqlQuery.execute([]) method. This is a generic method to execute a query, taken a number of arguments.
Parameters:
array - of arguments. These will be objects or object wrapper types for primitives.
Returns:
the value of the function

runGeneric

public java.lang.Object runGeneric()
Convenient method to run the function without arguments, returning the value as an object
Returns:
the value of the function

runGeneric

public java.lang.Object runGeneric(int p)
Convenient method to run the function with a single int argument
Parameters:
p - single int argument
Returns:
the value of the function as an Object

runGeneric

public java.lang.Object runGeneric(java.lang.Object[] args)
Analogous to the SqlQuery.execute([]) method. This is a generic method to execute a query, taken a number of arguments.
Parameters:
array - of arguments. These will be objects or object wrapper types for primitives.
Returns:
the value of the function, as an Object


Rod Johnson and Spring contributors 2001-2003.