## org.jboss.ejb.plugins.jaws.jdbc
Class JDBCCommand

    java.lang.Object
      |
      +--org.jboss.ejb.plugins.jaws.jdbc.JDBCCommand

Direct Known Subclasses:
  JDBCQueryCommand, JDBCUpdateCommand

public abstract class JDBCCommand
extends java.lang.Object

Abstract superclass for all JAWS Commands that use JDBC directly. Provides a Template Method for jdbcExecute(), default implementations for some of the methods called by this template, and a bunch of utility methods that database commands may need to call.

Version:
  $Revision: 1.33.2.4 $
Author:
  Justin Forder, Dirk Zimmermann

Field Summary
`static boolean` `debug` Gives compile-time control of tracing.
`protected JDBCCommandFactory` `factory`
`protected JawsEntityMetaData` `jawsEntity`
`protected Logger` `log`
`protected java.lang.String` `name`

Constructor Summary
`protected` `JDBCCommand(JDBCCommandFactory factory, java.lang.String name)` Construct a JDBCCommand with given factory and name.

Method Summary
`protected abstract java.lang.Object` `executeStatementAndHandleResult(java.sql.PreparedStatement stmt, java.lang.Object argOrArgs)` Executes the PreparedStatement and handles result of successful execution.
`protected java.lang.Object` `getCMPFieldValue(java.lang.Object instance, CMPFieldMetaData fieldMetaData)`
`protected java.sql.Connection` `getConnection()` Get a database connection
`protected int` `getJawsCMPFieldJDBCType(CMPFieldMetaData fieldMetaData)`
`protected int` `getJDBCType(java.lang.String name)` Gets the integer JDBC type code corresponding to the given name.
`protected java.lang.String` `getJDBCTypeName(int jdbcType)` Gets the JDBC type name corresponding to the given type code.
`protected java.lang.String` `getPkColumnList()` Returns the comma-delimited list of primary key column names for this entity.
`protected java.lang.String` `getPkColumnWhereList()` Returns the string to go in a WHERE clause based on the entity's primary key.
`protected java.lang.Object` `getPkFieldValue(java.lang.Object pk, PkFieldMetaData pkFieldMetaData)`
`protected java.lang.Object` `getResultObject(java.sql.ResultSet rs, int idx, java.lang.Class destination)` Used for all retrieval of results from `ResultSet`s.
`protected java.lang.Object` `getResultObject(java.sql.ResultSet rs, int idx, CMPFieldMetaData cmpField)` Wrapper around getResultObject(ResultSet rs, int idx, Class destination).
`protected java.lang.String` `getSQL(java.lang.Object argOrArgs)` Gets the SQL to be used in the PreparedStatement.
`protected java.lang.Object[]` `getState(EntityEnterpriseContext ctx)`
`protected boolean` `isBinaryType(int jdbcType)` Returns true if the JDBC type should be (de-)serialized as a binary stream and false otherwise.
`protected java.lang.Object` `jdbcExecute(java.lang.Object argOrArgs)` Template method handling the mundane business of opening a database connection, preparing a statement, setting its parameters, executing the prepared statement, handling the result, and cleaning up.
`protected void` `setCMPFieldValue(java.lang.Object instance, CMPFieldMetaData fieldMetaData, java.lang.Object value)`
`protected void` `setParameter(java.sql.PreparedStatement stmt, int idx, int jdbcType, java.lang.Object value)` Sets a parameter in this Command's PreparedStatement.
`protected void` `setParameters(java.sql.PreparedStatement stmt, java.lang.Object argOrArgs)` Default implementation does nothing.
`protected int` `setPrimaryKeyParameters(java.sql.PreparedStatement stmt, int parameterIndex, java.lang.Object id)` Sets the PreparedStatement parameters for a primary key in a WHERE clause.
`protected void` `setSQL(java.lang.String sql)` Used to set static SQL in subclass constructors.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### factory

    protected JDBCCommandFactory factory

### jawsEntity

    protected JawsEntityMetaData jawsEntity

### log

    protected Logger log

### name

    protected java.lang.String name

### debug

    public static boolean debug

  Gives compile-time control of tracing.

Constructor Detail

### JDBCCommand

    protected JDBCCommand(JDBCCommandFactory factory,
                          java.lang.String name)

  Construct a JDBCCommand with given factory and name.
  Parameters:
    `factory` - the factory which was used to create this JDBCCommand, which is also used as a common repository, shared by all an entity's Commands.
    `name` - the name to be used when tracing execution.

Method Detail

### jdbcExecute

    protected java.lang.Object jdbcExecute(java.lang.Object argOrArgs)
                                    throws java.lang.Exception

  Template method handling the mundane business of opening a database connection, preparing a statement, setting its parameters, executing the prepared statement, handling the result, and cleaning up.
  Parameters:
    `argOrArgs` - argument or array of arguments passed in from subclass execute method, and passed on to 'hook' methods for getting SQL and for setting parameters.
  Returns:
    any result produced by the handling of the result of executing the prepared statement.
  Throws:
    `java.lang.Exception` - if connection fails, or if any 'hook' method throws an exception.

### setSQL

    protected void setSQL(java.lang.String sql)

  Used to set static SQL in subclass constructors.
  Parameters:
    `sql` - the static SQL to be used by this Command.

### getSQL

    protected java.lang.String getSQL(java.lang.Object argOrArgs)
                               throws java.lang.Exception

  Gets the SQL to be used in the PreparedStatement. The default implementation returns the `sql` field value. This is appropriate in all cases where static SQL can be constructed in the Command constructor. Override if dynamically-generated SQL, based on the arguments given to execute(), is needed.
  Parameters:
    `argOrArgs` - argument or array of arguments passed in from subclass execute method.
  Returns:
    the SQL to use in the PreparedStatement.
  Throws:
    `java.lang.Exception` - if an attempt to generate dynamic SQL results in an Exception.

### setParameters

    protected void setParameters(java.sql.PreparedStatement stmt,
                                 java.lang.Object argOrArgs)
                          throws java.lang.Exception

  Default implementation does nothing. Override if parameters need to be set.
  Parameters:
    `stmt` - the PreparedStatement which will be executed by this Command.
    `argOrArgs` - argument or array of arguments passed in from subclass execute method.
  Throws:
    `java.lang.Exception` - if parameter setting fails.

### executeStatementAndHandleResult

    protected abstract java.lang.Object executeStatementAndHandleResult(java.sql.PreparedStatement stmt,
                                                                        java.lang.Object argOrArgs)
                                                                 throws java.lang.Exception

  Executes the PreparedStatement and handles result of successful execution. This is implemented in subclasses for queries and updates.
  Parameters:
    `stmt` - the PreparedStatement to execute.
    `argOrArgs` - argument or array of arguments passed in from subclass execute method.
  Returns:
    any result produced by the handling of the result of executing the prepared statement.
  Throws:
    `java.lang.Exception` - if execution or result handling fails.

### setParameter

    protected void setParameter(java.sql.PreparedStatement stmt,
                                int idx,
                                int jdbcType,
                                java.lang.Object value)
                         throws java.sql.SQLException

  Sets a parameter in this Command's PreparedStatement. Handles null values, and provides tracing.
  Parameters:
    `stmt` - the PreparedStatement whose parameter needs to be set.
    `idx` - the index (1-based) of the parameter to be set.
    `jdbcType` - the JDBC type of the parameter.
    `value` - the value which the parameter is to be set to.
  Throws:
    `java.sql.SQLException` - if parameter setting fails.

### setPrimaryKeyParameters

    protected int setPrimaryKeyParameters(java.sql.PreparedStatement stmt,
                                          int parameterIndex,
                                          java.lang.Object id)
                                   throws java.lang.IllegalAccessException,
                                          java.sql.SQLException

  Sets the PreparedStatement parameters for a primary key in a WHERE clause.
  Parameters:
    `stmt` - the PreparedStatement
    `parameterIndex` - the index (1-based) of the first parameter to set in the PreparedStatement
    `id` - the entity's ID
  Returns:
    the index of the next unset parameter
  Throws:
    `java.sql.SQLException` - if parameter setting fails
    `java.lang.IllegalAccessException` - if accessing a field in the PK class fails

### getResultObject

    protected java.lang.Object getResultObject(java.sql.ResultSet rs,
                                               int idx,
                                               java.lang.Class destination)
                                        throws java.sql.SQLException

  Used for all retrieval of results from `ResultSet`s. Implements tracing, and allows some tweaking of returned types.
  Parameters:
    `rs` - the `ResultSet` from which a result is being retrieved.
    `idx` - index of the result column.
    `destination` - The class of the variable this is going into

### getResultObject

    protected java.lang.Object getResultObject(java.sql.ResultSet rs,
                                               int idx,
                                               CMPFieldMetaData cmpField)
                                        throws java.sql.SQLException

  Wrapper around getResultObject(ResultSet rs, int idx, Class destination).

### getJDBCType

    protected final int getJDBCType(java.lang.String name)

  Gets the integer JDBC type code corresponding to the given name.
  Parameters:
    `name` - the JDBC type name.
  Returns:
    the JDBC type code.
  See Also:
    `Types`

### getJDBCTypeName

    protected final java.lang.String getJDBCTypeName(int jdbcType)

  Gets the JDBC type name corresponding to the given type code.
  Parameters:
    `jdbcType` - the integer JDBC type code.
  Returns:
    the JDBC type name.
  See Also:
    `Types`

### isBinaryType

    protected final boolean isBinaryType(int jdbcType)

  Returns true if the JDBC type should be (de-)serialized as a binary stream and false otherwise.
  Parameters:
    `jdbcType` - the JDBC type
  Returns:
    true if binary type, false otherwise

### getPkColumnList

    protected final java.lang.String getPkColumnList()

  Returns the comma-delimited list of primary key column names for this entity.
  Returns:
    comma-delimited list of primary key column names.

### getPkColumnWhereList

    protected final java.lang.String getPkColumnWhereList()

  Returns the string to go in a WHERE clause based on the entity's primary key.
  Returns:
    WHERE clause content, in the form `pkCol1Name=? AND pkCol2Name=?`

### getState

    protected java.lang.Object[] getState(EntityEnterpriseContext ctx)

### getCMPFieldValue

    protected java.lang.Object getCMPFieldValue(java.lang.Object instance,
                                                CMPFieldMetaData fieldMetaData)
                                         throws java.lang.IllegalAccessException

### setCMPFieldValue

    protected void setCMPFieldValue(java.lang.Object instance,
                                    CMPFieldMetaData fieldMetaData,
                                    java.lang.Object value)
                             throws java.lang.IllegalAccessException

### getPkFieldValue

    protected java.lang.Object getPkFieldValue(java.lang.Object pk,
                                               PkFieldMetaData pkFieldMetaData)
                                        throws java.lang.IllegalAccessException

### getJawsCMPFieldJDBCType

    protected int getJawsCMPFieldJDBCType(CMPFieldMetaData fieldMetaData)

### getConnection

    protected java.sql.Connection getConnection()
                                         throws java.sql.SQLException

  Get a database connection

Copyright © 2000 The JBoss Organization. All Rights Reserved.