org.springframework.jdbc.object
Class SqlFunction

org.springframework.jdbc.object.RdbmsOperation
  |
  +--org.springframework.jdbc.object.SqlOperation
        |
        +--org.springframework.jdbc.object.SqlQuery
              |
              +--org.springframework.jdbc.object.MappingSqlQueryWithParameters
                    |
                    +--org.springframework.jdbc.object.MappingSqlQuery
                          |
                          +--org.springframework.jdbc.object.SqlFunction

public class SqlFunction
extends MappingSqlQuery

SQL "function" wrapper for a query 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 that return a single result using a query like "select user()" or "select sysdate from dual". It is not intended for calling more complex stored functions or for using a callableStatement to invoke a stored procedure or stored function. Use StoredProcedure or SqlCall for this type of processing.

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
Version: $Id: SqlFunction.java,v 1.5 2003/12/15 13:52:02 trisberg Exp $
See Also: org.springframework.jdbc.object.StoredProcedure History

Constructor Summary
SqlFunction()
          Constructor to allow use as a JavaBean.
SqlFunction(DataSource ds, String sql, int[] types)
          Create a new SQLFunction object with SQL and parameters.
SqlFunction(DataSource ds, String sql, int[] types, int retType)
          Create a new SQLFunction object with SQL, parameters and a return type
SqlFunction(DataSource ds, String sql)
          Create a new SQLFunction object with SQL, but without parameters.
SqlFunction(DataSource ds, String sql, int retType)
          Create a new SQLFunction object with SQL and return type, but without parameters.

Method Summary
 intrun()
          Convenient method to run the function without arguments.
 intrun(int p)
          Convenient method to run the function with a single int argument.
 intrun(Object[] args)
          Analogous to the SqlQuery.execute([]) method.
 ObjectrunGeneric()
          Convenient method to run the function without arguments, returning the value as an object
 ObjectrunGeneric(int p)
          Convenient method to run the function with a single int argument.
 ObjectrunGeneric(Object[] args)
          Analogous to the SqlQuery.execute([]) method.

Methods inherited from class org.springframework.jdbc.object.SqlQuery
execute, execute, execute, execute, execute, execute, execute, execute, execute, execute, execute, execute, findObject, findObject, findObject, findObject, findObject, findObject, findObject, findObject, findObject, findObject, getRowsExpected, setRowsExpected

Methods inherited from class org.springframework.jdbc.object.RdbmsOperation
afterPropertiesSet, compile, declareParameter, getSql, isCompiled, setDataSource, setJdbcTemplate, setSql, setTypes

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(DataSource ds, 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(DataSource ds, String sql, int[] types, int retType)
throws org.springframework.dao.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(DataSource ds, 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(DataSource ds, 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

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(Object[] args)
Analogous to the SqlQuery.execute([]) method. This is a generic method to execute a query, taken a number of arguments.
Parameters:
args - array of arguments. These will be objects or object wrapper types for primitives.
Returns: the value of the function

runGeneric

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

runGeneric

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