net.spy.db
Class DBSP

java.lang.Object
  extended bynet.spy.SpyObject
      extended bynet.spy.SpyDB
          extended bynet.spy.db.SpyCacheDB
              extended bynet.spy.db.DBSP
All Implemented Interfaces:
DBSPLike
Direct Known Subclasses:
DBCP, DBSQL

public abstract class DBSP
extends SpyCacheDB
implements DBSPLike

Super class for all stored procedure calls.


Nested Class Summary
 class DBSP.Argument
          Arguments for DBSPs.
 class DBSP.NamedObject
          Objects with names.
 class DBSP.Parameter
          Parameters for DBSPs.
 
Field Summary
 
Fields inherited from class net.spy.SpyDB
INIT_FROM_CONFIG, INIT_FROM_CONN
 
Constructor Summary
DBSP(Connection conn)
          Get a new DBSP object using the given Connection
DBSP(SpyConfig conf)
          Get a new DBSP object with a given config.
 
Method Summary
protected  void addParameter(DBSP.Parameter p)
          Define a parameter.
protected  void applyArgs(Collection v)
          Fill in the arguments (with types) for the given list of parameters.
protected  void checkArgs()
          Verify that the arguments, as given, are acceptable.
 void close()
          Close this statement and whatever the superclass wants to do.
 ResultSet executeQuery()
          Execute the query.
 int executeUpdate()
          Execute a query for update only.
 List getArguments()
          Get the arguments in parameter order.
 long getCacheTime()
          Get the cache time configured for this SP.
 List getOptionalArgs()
          Deprecated. use getParameters() or getParameters(int) instead
 List getOutputArgs()
          Deprecated. use getParameters() or getParameters(int) instead
 List getParameters()
          Get a List containing all parameters.
 List getParameters(int type)
          Get a List of parameters of a specific type.
 int getParameterType(String var)
          Get the DBSP.Parameter type.
protected  List getParams()
          Get a List of all of the parameters in the order in which they were defined.
protected  PreparedStatement getPreparedStatement()
          Get the prepared statement on which this DBSP will operate.
protected  String getQuery()
          Get the query this DBSP will be calling.
 int getQueryTimeout()
          Get the timeout for this query
 List getRequiredArgs()
          Deprecated. use getParameters() or getParameters(int) instead
protected  String getSPName()
          Get the SP name.
 int getType(String var)
          Get the Types type of the given parameter.
 SQLWarning getWarnings()
          Get the warnings.
protected  boolean isDebugEnabled()
          Find out if debug is enabled.
static void main(String[] args)
          Commandline test for SPs that return result sets.
 ResultSet nextResults()
          Get the next result set.
protected  void prepare()
          Prepare the statement for execution.
protected  void resetArgs()
          Reset all arguments.
 void set(String which, BigDecimal a1)
          Set field which to the value a1 of the type java.math.BigDecimal
 void set(String which, boolean a1)
          Set field which to the value a1 of the type boolean
 void set(String which, Boolean b)
          Set field which to the value of b.
 void set(String which, Date a1)
          Set field which to the value a1 of the type java.sql.Date
 void set(String which, double a1)
          Set field which to the value a1 of the type double
 void set(String which, float a1)
          Set field which to the value a1 of the type float
 void set(String which, Float a1)
          Set field which to the value a1 of the type float
 void set(String which, int a1)
          Set field which to the value a1 of the type int
 void set(String which, Integer a1)
          Set field which to the value a1 of the type java.lang.Integer
 void set(String which, long a1)
          Set field which to the value a1 of the type long
 void set(String which, Object obj)
           
 void set(String which, short a1)
          Set field which to the value a1 of the type short
 void set(String which, Short s)
          Set the field which to the value s of the type short.
 void set(String which, String a1)
          Set field which to the value a1 of the type java.lang.String
 void set(String which, Time a1)
          Set field which to the value a1 of the type Time
 void set(String which, Timestamp a1)
          Set field which to the value a1 of the type Timestamp
protected  void setArg(String which, Object what, int type)
          Set the named argument to the value contained in the given object for the given type.
 void setCacheTime(long time)
          Set the number of seconds the results of this SP will be valid.
 void setCoerced(String var, String value)
          Set a field in the DBSP after coercing the String value to the required value for the given field.
 void setCursorName(String name)
          Set the cursor name to be handed to the underlying Statement.
protected  void setDebug(boolean db)
          Set the value of debug for this instance.
 void setMaxRows(int maxRows)
          Set the maximum number of rows that should be returned from this query.
protected  void setNull(String which, int a1)
          Set field which to a null of the given type.
protected  void setOptional(String name, int type)
          Define a field to be optional.
protected  void setOutput(String name, int type)
          Define a field to be output.
protected  void setPreparedStatement(PreparedStatement pst)
          Set the prepared statement on which this DBSP will operate.
protected  void setQuery(String query)
          Set the SQL query to call
 void setQueryTimeout(int timeout)
          Set the timeout for this query.
protected  void setRequired(String name, int type)
          Define a field to be required.
 void setSmallInt(String which, short a1)
          Set field which to the value a1 of the type short
protected  void setSPName(String to)
          Set the stored procedure to the given value.
 
Methods inherited from class net.spy.db.SpyCacheDB
executeQuery, prepareStatement
 
Methods inherited from class net.spy.SpyDB
dbquoteStr, executeQuery, executeUpdate, freeDBConn, getConfig, getConn, getInitType, getSource, init, isClosed, prepareCall, prepareStatement
 
Methods inherited from class net.spy.SpyObject
getLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DBSP

public DBSP(SpyConfig conf)
     throws SQLException
Get a new DBSP object with a given config.


DBSP

public DBSP(Connection conn)
     throws SQLException
Get a new DBSP object using the given Connection

Method Detail

executeQuery

public ResultSet executeQuery()
                       throws SQLException
Execute the query.

Specified by:
executeQuery in interface DBSPLike
Returns:
the Results from the query
Throws:
SQLException - if there's a problem executing the query

executeUpdate

public int executeUpdate()
                  throws SQLException
Execute a query for update only.

Specified by:
executeUpdate in interface DBSPLike
Returns:
the number of rows affected by this update
Throws:
SQLException - if there's a problem executing the query

nextResults

public ResultSet nextResults()
                      throws SQLException
Get the next result set.

Returns:
null if there are no more result sets.
Throws:
SQLException

getWarnings

public SQLWarning getWarnings()
                       throws SQLException
Get the warnings.

Throws:
SQLException

setCursorName

public void setCursorName(String name)
Set the cursor name to be handed to the underlying Statement.

Parameters:
name - cursor name (or null to disable cursor handling)

setCacheTime

public void setCacheTime(long time)
Set the number of seconds the results of this SP will be valid. This option uses SpyCacheDB.prepareStatement(String,long) instead of the native driver's version.

Specified by:
setCacheTime in interface DBSPLike
Parameters:
time - time (in seconds) to keep the results around

getCacheTime

public long getCacheTime()
Get the cache time configured for this SP.

Specified by:
getCacheTime in interface DBSPLike
Returns:
the time (in seconds) the results will be valid

setPreparedStatement

protected void setPreparedStatement(PreparedStatement pst)
Set the prepared statement on which this DBSP will operate.

Parameters:
pst - the prepared statement

getPreparedStatement

protected PreparedStatement getPreparedStatement()
Get the prepared statement on which this DBSP will operate.


addParameter

protected void addParameter(DBSP.Parameter p)
                     throws SQLException
Define a parameter.

Parameters:
p - the parameter
Throws:
SQLException - if the parameter has already been added

setRequired

protected void setRequired(String name,
                           int type)
                    throws SQLException
Define a field to be required.

Parameters:
name - the name of the field
type - the type
Throws:
SQLException - if the type has already been added
See Also:
Types

setOptional

protected void setOptional(String name,
                           int type)
                    throws SQLException
Define a field to be optional.

Parameters:
name - the name of the field
type - the type
Throws:
SQLException - if the type has already been added
See Also:
Types

setOutput

protected void setOutput(String name,
                         int type)
                  throws SQLException
Define a field to be output.

Parameters:
name - the name of the field
type - the type
Throws:
SQLException - if the type has already been added
See Also:
Types

setArg

protected void setArg(String which,
                      Object what,
                      int type)
               throws SQLException
Set the named argument to the value contained in the given object for the given type.

Parameters:
which - which variable to set
what - what value to set it to
type - type of variable this is
Throws:
SQLException

getArguments

public List getArguments()
Get the arguments in parameter order.

Returns:
an unmodifiable list of DBSP.Argument objects

getParams

protected List getParams()
Get a List of all of the parameters in the order in which they were defined.

Returns:
a List of DBSP.Parameter objects.

resetArgs

protected void resetArgs()
Reset all arguments.


setQueryTimeout

public void setQueryTimeout(int timeout)
Set the timeout for this query. See JDCB Statement documentation.

Specified by:
setQueryTimeout in interface DBSPLike
See Also:
Statement#setQueryTimeout(int)

setMaxRows

public void setMaxRows(int maxRows)
Set the maximum number of rows that should be returned from this query. See JDBC Statement documentation.


getQueryTimeout

public int getQueryTimeout()
Get the timeout for this query

Specified by:
getQueryTimeout in interface DBSPLike
See Also:
Statement#getQueryTimeout()

setSPName

protected void setSPName(String to)
Set the stored procedure to the given value.


getSPName

protected String getSPName()
Get the SP name.

Returns:
the name of the SP to call, or null if not applicable

checkArgs

protected void checkArgs()
                  throws SQLException
Verify that the arguments, as given, are acceptable.

Throws:
SQLException

prepare

protected void prepare()
                throws SQLException
Prepare the statement for execution.

Throws:
SQLException

setDebug

protected void setDebug(boolean db)
Set the value of debug for this instance.

Parameters:
db - the debug value to set

isDebugEnabled

protected boolean isDebugEnabled()
Find out if debug is enabled.

Returns:
true if debug is enabled

setQuery

protected void setQuery(String query)
Set the SQL query to call


getQuery

protected String getQuery()
Get the query this DBSP will be calling.


applyArgs

protected void applyArgs(Collection v)
                  throws SQLException
Fill in the arguments (with types) for the given list of parameters.

Parameters:
v - the list of Parameters we need to add, in order
Throws:
SQLException

set

public void set(String which,
                BigDecimal a1)
         throws SQLException
Set field which to the value a1 of the type java.math.BigDecimal

Parameters:
which - which field to set
a1 - the value to set
Throws:
SQLException - if there's an error setting this argument.

set

public void set(String which,
                boolean a1)
         throws SQLException
Set field which to the value a1 of the type boolean

Parameters:
which - which field to set
a1 - the value to set
Throws:
SQLException - if there's an error setting this argument.

set

public void set(String which,
                Boolean b)
         throws SQLException
Set field which to the value of b.

Parameters:
which - the field to set
b - the value to set
Throws:
SQLException - if there's an error setting this argument

set

public void set(String which,
                Date a1)
         throws SQLException
Set field which to the value a1 of the type java.sql.Date

Parameters:
which - which field to set
a1 - the value to set
Throws:
SQLException - if there's an error setting this argument.

set

public void set(String which,
                double a1)
         throws SQLException
Set field which to the value a1 of the type double

Parameters:
which - which field to set
a1 - the value to set
Throws:
SQLException - if there's an error setting this argument.

set

public void set(String which,
                float a1)
         throws SQLException
Set field which to the value a1 of the type float

Parameters:
which - which field to set
a1 - the value to set
Throws:
SQLException - if there's an error setting this argument.

set

public void set(String which,
                Float a1)
         throws SQLException
Set field which to the value a1 of the type float

Parameters:
which - which field to set
a1 - the value to set
Throws:
SQLException - if there's an error setting this argument.

set

public void set(String which,
                Integer a1)
         throws SQLException
Set field which to the value a1 of the type java.lang.Integer

Parameters:
which - which field to set
a1 - the value to set
Throws:
SQLException - if there's an error setting this argument.

set

public void set(String which,
                int a1)
         throws SQLException
Set field which to the value a1 of the type int

Parameters:
which - which field to set
a1 - the value to set
Throws:
SQLException - if there's an error setting this argument.

set

public void set(String which,
                long a1)
         throws SQLException
Set field which to the value a1 of the type long

Parameters:
which - which field to set
a1 - the value to set
Throws:
SQLException - if there's an error setting this argument.

setNull

protected void setNull(String which,
                       int a1)
                throws SQLException
Set field which to a null of the given type. Do not call this directly, instead, if you have an empty String, for example, consider:

  set("mycol", (String)null);
 

Parameters:
which - which field to set
a1 - the value to set
Throws:
SQLException - if there's an error setting this argument.

setSmallInt

public void setSmallInt(String which,
                        short a1)
                 throws SQLException
Set field which to the value a1 of the type short

Parameters:
which - which field to set
a1 - the value to set
Throws:
SQLException - if there's an error setting this argument.

set

public void set(String which,
                short a1)
         throws SQLException
Set field which to the value a1 of the type short

Parameters:
which - which field to set
a1 - the value to set
Throws:
SQLException - if there's an error setting this argument.

set

public void set(String which,
                Short s)
         throws SQLException
Set the field which to the value s of the type short.

Parameters:
which - the field to set (by name)
s - the new value
Throws:
SQLException - if there's a problem setting this value

set

public void set(String which,
                String a1)
         throws SQLException
Set field which to the value a1 of the type java.lang.String

Parameters:
which - which field to set
a1 - the value to set
Throws:
SQLException - if there's an error setting this argument.

set

public void set(String which,
                Time a1)
         throws SQLException
Set field which to the value a1 of the type Time

Parameters:
which - which field to set
a1 - the value to set
Throws:
SQLException - if there's an error setting this argument.

set

public void set(String which,
                Timestamp a1)
         throws SQLException
Set field which to the value a1 of the type Timestamp

Parameters:
which - which field to set
a1 - the value to set
Throws:
SQLException - if there's an error setting this argument.

set

public void set(String which,
                Object obj)
         throws SQLException
Throws:
SQLException

getParameters

public List getParameters()
Get a List containing all parameters.

Returns:
an umodifiable List of DBSP.Parameter objects.

getParameters

public List getParameters(int type)
Get a List of parameters of a specific type.

Parameters:
type - the Parameter type.
Returns:
a list of DBSP.Parameters of the specified type.

getRequiredArgs

public List getRequiredArgs()
Deprecated. use getParameters() or getParameters(int) instead

Get the required arguments.

Returns:
a List of DBSP.Parameters representing the required arguments in the order in which they will be passed into the query

getOptionalArgs

public List getOptionalArgs()
Deprecated. use getParameters() or getParameters(int) instead

Get the optional arguments.

Returns:
a List of DBSP.Parameters representing the optional arguments in the order in which they will be passed into the query

getOutputArgs

public List getOutputArgs()
Deprecated. use getParameters() or getParameters(int) instead

Get the output arguments.

Returns:
a List of DBSP.Parameters representing the output arguments in the order in which they will be passed into the query

getType

public int getType(String var)
Get the Types type of the given parameter.

Returns:
the type, or -1 if there's no such variable

getParameterType

public int getParameterType(String var)
Get the DBSP.Parameter type.

Parameters:
var - the name of the parameter
Returns:
the DBSP.Parameter type, or -1 if there's no such parameter

close

public void close()
Close this statement and whatever the superclass wants to do.

Specified by:
close in interface DBSPLike
Overrides:
close in class SpyDB

setCoerced

public void setCoerced(String var,
                       String value)
                throws SQLException
Set a field in the DBSP after coercing the String value to the required value for the given field. This should rarely be used, but is useful for accessing DBSPs through web forms.

Parameters:
var - the field to set
value - the String representation of the value to set
Throws:
SQLException - if there's a problem with coersion

main

public static void main(String[] args)
                 throws Exception
Commandline test for SPs that return result sets. Invoked thusly:

java net.spy.db.DBSP net.spy.db.sp.SPClassName configpath key value [...]

Throws:
Exception