org.jdesktop.dataset.provider.sql
Class AbstractSqlCommand

java.lang.Object
  extended by org.jdesktop.dataset.DataCommand
      extended by org.jdesktop.dataset.provider.sql.AbstractSqlCommand
Direct Known Subclasses:
SQLCommand

public abstract class AbstractSqlCommand
extends DataCommand

An AbstractSqlCommand is a DataCommand meant to be used with a SQLDataProvider by defining methods to generate a SELECT, INSERT, UPDATE or DELETE statement. How these SQL statements are built is up to the concrete implementation of an AbstractSqlCommand.

The methods for retrieving the SQL statements (as PreparedStatements) are all protected, meaning they can be accessed by subclasses as well as classes within this package. Thus, within the package, the AbstractSqlCommand defines an interface for preparing a PreparedStatement for consumers of the command.

An AbstractSqlCommand also defines methods to normalize SQL statements to make them easier to process using JDBC--namely, to convert between named- parameter style statements and index-based parameterized statements.

AbstractSqlCommand is useful in defining the structure of a concrete SQLCommand or TableCommand, and is not meant to be used on its own.

Author:
rbair

Constructor Summary
AbstractSqlCommand()
           
 
Method Summary
protected  java.lang.String constructSql(java.lang.String sql, java.util.Map<java.lang.String,java.util.List<java.lang.Integer>> indexes)
          Generates a new String for a SQL statement, replacing named parameters with ? symbols, as required by the Connection.prepareStatement(String) method.
protected abstract  java.sql.PreparedStatement getDeleteStatement(JDBCDataConnection conn, DataRow row)
           
protected abstract  java.sql.PreparedStatement getInsertStatement(JDBCDataConnection conn, DataRow row)
           
 java.lang.String[] getParameterNames(java.lang.String[] statements)
          Searches the statements for param names, and returns the unique set of param names.
protected abstract  java.sql.PreparedStatement getSelectStatement(JDBCDataConnection conn)
           
protected abstract  java.sql.PreparedStatement getUpdateStatement(JDBCDataConnection conn, DataRow row)
           
protected  java.sql.PreparedStatement prepareStatement(java.lang.String sql, java.util.Map<java.lang.String,java.lang.Object> values, JDBCDataConnection conn)
          Creates a PreparedStatement from a SQL statement, setting parameter values using the supplied Map of parameter names to values.
 
Methods inherited from class org.jdesktop.dataset.DataCommand
addPropertyChangeListener, addPropertyChangeListener, clearParameter, clearParameters, firePropertyChange, getParameter, getParameterNames, getParameterValues, getShortDescription, removePropertyChangeListener, setParameter, setShortDescription
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractSqlCommand

public AbstractSqlCommand()
Method Detail

getSelectStatement

protected abstract java.sql.PreparedStatement getSelectStatement(JDBCDataConnection conn)
                                                          throws java.lang.Exception
Parameters:
conn - An active JDBCDataConnection to use to prepare the statement
Returns:
A PreparedStatement, ready to execute, for the SELECT SQL statement.
Throws:
java.lang.Exception

getInsertStatement

protected abstract java.sql.PreparedStatement getInsertStatement(JDBCDataConnection conn,
                                                                 DataRow row)
                                                          throws java.lang.Exception
Parameters:
conn - An active JDBCDataConnection to use to prepare the statement
row - The DataRow that will be inserted
Returns:
A PreparedStatement, ready to execute, for the INSERT SQL statement.
Throws:
java.lang.Exception

getUpdateStatement

protected abstract java.sql.PreparedStatement getUpdateStatement(JDBCDataConnection conn,
                                                                 DataRow row)
                                                          throws java.lang.Exception
Parameters:
conn - An active JDBCDataConnection to use to prepare the statement
row - The DataRow that will be updated
Returns:
A PreparedStatement, ready to execute, for the UPDATE SQL statement.
Throws:
java.lang.Exception

getDeleteStatement

protected abstract java.sql.PreparedStatement getDeleteStatement(JDBCDataConnection conn,
                                                                 DataRow row)
                                                          throws java.lang.Exception
Parameters:
conn - An active JDBCDataConnection to use to prepare the statement
row - The DataRow that will be deleted
Returns:
A PreparedStatement, ready to execute, for the DELETE SQL statement.
Throws:
java.lang.Exception

constructSql

protected java.lang.String constructSql(java.lang.String sql,
                                        java.util.Map<java.lang.String,java.util.List<java.lang.Integer>> indexes)
Generates a new String for a SQL statement, replacing named parameters with ? symbols, as required by the Connection.prepareStatement(String) method. The Map parameter is populated with parameter names, mapped to a List of indexes numbering that parameter within the SQL statement. Thus, as each named parameter is replaced, we get a list of the position that parameter had within the statement; the List of indexes can then be used on a call to PreparedStatement.setObject(int, Object), using the index in the list as the index parameter in setObject().

Parameters:
sql - A SQL statement with 1 or more named parameters.
indexes - An empty Map which will be populated with a list of parameter names, and the number of that parameter within the SQL statement.
Returns:
A SQL statement ready to use in the JDBC prepareStatement() method; note that the Map parameter is also populated in this method call.

prepareStatement

protected java.sql.PreparedStatement prepareStatement(java.lang.String sql,
                                                      java.util.Map<java.lang.String,java.lang.Object> values,
                                                      JDBCDataConnection conn)
                                               throws java.lang.Exception
Creates a PreparedStatement from a SQL statement, setting parameter values using the supplied Map of parameter names to values. If there are parameters in the SQL that require values assigned in the PreparedStatements, the parameter in the SQL should appear as ":", and the parameter name should be assigned a value in the Map argument to this method.

Parameters:
sql - A SQL statement, including named parameters if desired.
values - A Map of parameter names to Object values for the parameter-- the values used for each parameter when this statement is executed.
conn - An valid JDBCDataConnection.
Returns:
A PreparedStatement build from the SQL argument, with parameters assigned.
Throws:
java.lang.Exception - if any error occurs during execution.

getParameterNames

public java.lang.String[] getParameterNames(java.lang.String[] statements)
Searches the statements for param names, and returns the unique set of param names.

Parameters:
statements - An array of SQL statements, optionally with named parameters embedded, in the form ":".
Returns:
Array of parameter names, unique across all the statements.


Copyright © 2005 Sun Microsystems All Rights Reserved.