Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.apache.tapestry.contrib.jdbc
Class StatementAssembly  view StatementAssembly download StatementAssembly.java

java.lang.Object
  extended byorg.apache.tapestry.contrib.jdbc.StatementAssembly

public class StatementAssembly
extends java.lang.Object

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

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

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

addParameter(Integer) 55 (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) 55 (and all overloaded versions of it) simply invokes the appropriate addParameter(int) 55 , adding the separator in between parameters.

Version:
$Id: StatementAssembly.java,v 1.3 2004/02/19 17:37:58 hlship Exp $

Field Summary
private  java.lang.StringBuffer _buffer
           
private  int _indent
           
private  int _lineLength
           
private  int _maxLineLength
           
private  java.util.List _parameters
          List of IParameter
private static java.lang.String NULL
           
static java.lang.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(java.lang.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(java.lang.String[] items, java.lang.String separator)
           
 void addParameter(boolean value)
           
 void addParameter(java.lang.Boolean value)
           
 void addParameter(java.util.Date date)
          Adds a date value to a StatementAssembly converting it to a java.sql.Timestamp first.
 void addParameter(double value)
           
 void addParameter(java.lang.Double value)
           
 void addParameter(float value)
           
 void addParameter(java.lang.Float value)
           
 void addParameter(int value)
           
 void addParameter(java.lang.Integer value)
           
private  void addParameter(IParameter parameter)
           
 void addParameter(long value)
           
 void addParameter(java.lang.Long value)
           
 void addParameter(java.lang.Object value)
           
 void addParameter(short value)
           
 void addParameter(java.lang.Short value)
           
 void addParameter(java.lang.String value)
           
 void addParameter(java.sql.Timestamp timestamp)
           
 void addParameterList(double[] items, java.lang.String separator)
           
 void addParameterList(int[] items, java.lang.String separator)
           
 void addParameterList(java.lang.Integer[] items, java.lang.String separator)
           
 void addParameterList(long[] items, java.lang.String separator)
           
 void addParameterList(java.lang.Long[] items, java.lang.String separator)
           
 void addParameterList(java.lang.String[] items, java.lang.String separator)
           
 void addSep(java.lang.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(java.sql.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(java.lang.String text)
          Starts a new line, then adds the given text.
 java.lang.String toString()
          Convert this Object to a human-readable String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_buffer

private java.lang.StringBuffer _buffer

NULL

private static final java.lang.String NULL
See Also:
Constant Field Values

SEP

public static final java.lang.String SEP
See Also:
Constant Field Values

_parameters

private java.util.List _parameters
List of IParameter


_lineLength

private int _lineLength

_maxLineLength

private int _maxLineLength

_indent

private int _indent
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(java.lang.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) 55 .


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(java.util.Date date)
Adds a date value to a StatementAssembly converting it to a java.sql.Timestamp first.


addSep

public void addSep(java.lang.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(java.lang.String text)
Starts a new line, then adds the given text.


addList

public void addList(java.lang.String[] items,
                    java.lang.String separator)

addParameterList

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

addParameterList

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

addParameterList

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

addParameterList

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

addParameterList

public void addParameterList(java.lang.String[] items,
                             java.lang.String separator)

addParameterList

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

addParameter

public void addParameter(java.lang.Object value)

addParameter

public void addParameter(java.sql.Timestamp timestamp)

addParameter

public void addParameter(java.lang.String value)

addParameter

public void addParameter(int value)

addParameter

public void addParameter(java.lang.Integer value)

addParameter

public void addParameter(long value)

addParameter

public void addParameter(java.lang.Long value)

addParameter

public void addParameter(float value)

addParameter

public void addParameter(java.lang.Float value)

addParameter

public void addParameter(double value)

addParameter

public void addParameter(java.lang.Double value)

addParameter

public void addParameter(short value)

addParameter

public void addParameter(java.lang.Short value)

addParameter

public void addParameter(boolean value)

addParameter

public void addParameter(java.lang.Boolean value)

addParameter

private void addParameter(IParameter parameter)

createStatement

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


toString

public java.lang.String toString()
Description copied from class: java.lang.Object
Convert this Object to a human-readable String. There are no limits placed on how long this String should be or what it should contain. We suggest you make it as intuitive as possible to be able to place it into System.out.println() 55 and such.

It is typical, but not required, to ensure that this method never completes abruptly with a java.lang.RuntimeException.

This method will be called when performing string concatenation with this object. If the result is null, string concatenation will instead use "null".

The default implementation returns getClass().getName() + "@" + Integer.toHexString(hashCode()).