org.apache.tapestry.contrib.jdbc
Class StatementAssembly

java.lang.Object
  extended by org.apache.tapestry.contrib.jdbc.StatementAssembly

public class StatementAssembly
extends Object

Class for creating and executing JDBC statements. Allows statements to be assembled incrementally (like a StringBuffer), but also tracks parameters, shielding the developer from the differences between constructing and using a JDBC Statement and a JDBC PreparedStatement. This class is somewhat skewed towards Oracle, which works more efficiently with prepared staments than simple SQL.

In addition, implements toString() in a useful way (you can see the SQL and parameters), which is invaluable when debugging.

addParameter(int) (and all overloaded versions of it for scalar types) adds a "?" to the statement and records the parameter value.

addParameter(Integer) (and all overloaded version of it for wrapper types) does the same ... unless the value is null, in which case "NULL" is inserted into the statement.

addParameterList(int[], String) (and all overloaded versions of it) simply invokes the appropriate addParameter(int), adding the separator in between parameters.

Version:
$Id: StatementAssembly.java 243791 2004-02-19 17:38:13Z hlship $
Author:
Howard Lewis Ship

Field Summary
static String SEP
           
 
Constructor Summary
StatementAssembly()
          Default constructor; uses a maximum line length of 80 and an indent of 5.
StatementAssembly(int maxLineLength, int indent)
           
 
Method Summary
 void add(double value)
           
 void add(float value)
           
 void add(int value)
           
 void add(long value)
           
 void add(short value)
           
 void add(String text)
          Adds text to the current line, unless that would make the line too long, in which case a new line is started (and indented) before adding the text.
 void addList(String[] items, String separator)
           
 void addParameter(boolean value)
           
 void addParameter(Boolean value)
           
 void addParameter(Date date)
          Adds a date value to a StatementAssembly converting it to a Timestamp first.
 void addParameter(double value)
           
 void addParameter(Double value)
           
 void addParameter(float value)
           
 void addParameter(Float value)
           
 void addParameter(int value)
           
 void addParameter(Integer value)
           
 void addParameter(long value)
           
 void addParameter(Long value)
           
 void addParameter(Object value)
           
 void addParameter(short value)
           
 void addParameter(Short value)
           
 void addParameter(String value)
           
 void addParameter(Timestamp timestamp)
           
 void addParameterList(double[] items, String separator)
           
 void addParameterList(int[] items, String separator)
           
 void addParameterList(Integer[] items, String separator)
           
 void addParameterList(long[] items, String separator)
           
 void addParameterList(Long[] items, String separator)
           
 void addParameterList(String[] items, String separator)
           
 void addSep(String text)
          Adds a separator (usually a comma and a space) to the current line, regardless of line length.
 void clear()
          Clears the assembly, preparing it for re-use.
 IStatement createStatement(Connection connection)
          Creates and returns an IStatement based on the SQL and parameters acquired.
 int getIndent()
          Number of spaces to indent continuation lines by.
 int getMaxLineLength()
          Maximum length of a line.
 void newLine()
          Starts a new line, without indenting.
 void newLine(String text)
          Starts a new line, then adds the given text.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SEP

public static final String SEP
See Also:
Constant Field Values
Constructor Detail

StatementAssembly

public StatementAssembly()
Default constructor; uses a maximum line length of 80 and an indent of 5.


StatementAssembly

public StatementAssembly(int maxLineLength,
                         int indent)
Method Detail

clear

public void clear()
Clears the assembly, preparing it for re-use.

Since:
1.0.7

getMaxLineLength

public int getMaxLineLength()
Maximum length of a line.


getIndent

public int getIndent()
Number of spaces to indent continuation lines by.


add

public void add(String text)
Adds text to the current line, unless that would make the line too long, in which case a new line is started (and indented) before adding the text.

Text is added as-is, with no concept of quoting. To add arbitrary strings (such as in a where clause), use addParameter(String).


add

public void add(short value)

add

public void add(int value)

add

public void add(long value)

add

public void add(float value)

add

public void add(double value)

addParameter

public void addParameter(Date date)
Adds a date value to a StatementAssembly converting it to a Timestamp first.


addSep

public void addSep(String text)
Adds a separator (usually a comma and a space) to the current line, regardless of line length. This is purely aesthetic ... it just looks odd if a separator gets wrapped to a new line by itself.


newLine

public void newLine()
Starts a new line, without indenting.


newLine

public void newLine(String text)
Starts a new line, then adds the given text.


addList

public void addList(String[] items,
                    String separator)

addParameterList

public void addParameterList(int[] items,
                             String separator)

addParameterList

public void addParameterList(Integer[] items,
                             String separator)

addParameterList

public void addParameterList(long[] items,
                             String separator)

addParameterList

public void addParameterList(Long[] items,
                             String separator)

addParameterList

public void addParameterList(String[] items,
                             String separator)

addParameterList

public void addParameterList(double[] items,
                             String separator)

addParameter

public void addParameter(Object value)

addParameter

public void addParameter(Timestamp timestamp)

addParameter

public void addParameter(String value)

addParameter

public void addParameter(int value)

addParameter

public void addParameter(Integer value)

addParameter

public void addParameter(long value)

addParameter

public void addParameter(Long value)

addParameter

public void addParameter(float value)

addParameter

public void addParameter(Float value)

addParameter

public void addParameter(double value)

addParameter

public void addParameter(Double value)

addParameter

public void addParameter(short value)

addParameter

public void addParameter(Short value)

addParameter

public void addParameter(boolean value)

addParameter

public void addParameter(Boolean value)

createStatement

public IStatement createStatement(Connection connection)
                           throws SQLException
Creates and returns an IStatement based on the SQL and parameters acquired.

Throws:
SQLException

toString

public String toString()
Overrides:
toString in class Object