Coverage Report - org.apache.tapestry.contrib.jdbc.StatementAssembly
 
Classes in this File Line Coverage Branch Coverage Complexity
StatementAssembly
0%
0/164
0%
0/70
1.925
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.contrib.jdbc;
 16  
 
 17  
 import java.sql.Connection;
 18  
 import java.sql.SQLException;
 19  
 import java.sql.Timestamp;
 20  
 import java.util.ArrayList;
 21  
 import java.util.Calendar;
 22  
 import java.util.Date;
 23  
 import java.util.List;
 24  
 
 25  
 /**
 26  
  *  Class for creating and executing JDBC statements.  Allows statements to be assembled
 27  
  *  incrementally (like a {@link StringBuffer}), but also tracks parameters, shielding
 28  
  *  the developer from the differences between constructing and 
 29  
  *  using a JDBC 
 30  
  *  {@link java.sql.Statement} and 
 31  
  *  a JDBC {@link java.sql.PreparedStatement}.  This class is somewhat skewed towards
 32  
  *  Oracle, which works more efficiently with prepared staments than
 33  
  *  simple SQL.
 34  
  * 
 35  
  *  <p>In addition, implements {@link #toString()} in a useful way (you can see the
 36  
  *  SQL and parameters), which is invaluable when debugging.
 37  
  * 
 38  
  *  <p>{@link #addParameter(int)} (and all overloaded versions of it for scalar types)
 39  
  *  adds a "?" to the statement and records the parameter value.
 40  
  * 
 41  
  *  <p>{@link #addParameter(Integer)} (and all overloaded version of it for wrapper
 42  
  *  types) does the same ... unless the value is null, in which case "NULL" is
 43  
  *  inserted into the statement.
 44  
  * 
 45  
  *  <p>{@link #addParameterList(int[], String)} (and all overloaded versions of it)
 46  
  *  simply invokes the appropriate {@link #addParameter(int)}, adding the
 47  
  *  separator in between parameters.
 48  
  *
 49  
  *  @author Howard Lewis Ship
 50  
  *
 51  
  **/
 52  
 
 53  
 public class StatementAssembly
 54  
 {
 55  
     public static final String SEP = ", ";
 56  
     
 57  
     private static final String NULL = "NULL";   
 58  
     
 59  0
     private StringBuffer _buffer = new StringBuffer();
 60  
 
 61  
     /**
 62  
      *  List of {@link IParameter}.
 63  
      * 
 64  
      **/
 65  
 
 66  
     private List _parameters;
 67  
 
 68  
     private int _lineLength;
 69  0
     private int _maxLineLength = 80;
 70  0
     private int _indent = 5;
 71  
 
 72  
     /**
 73  
      *  Default constructor; uses a maximum line length of 80 and an indent of 5.
 74  
      *
 75  
      **/
 76  
 
 77  
     public StatementAssembly()
 78  0
     {
 79  0
     }
 80  
 
 81  
     public StatementAssembly(int maxLineLength, int indent)
 82  0
     {
 83  0
         _maxLineLength = maxLineLength;
 84  0
         _indent = indent;
 85  0
     }
 86  
 
 87  
     /**
 88  
      *  Clears the assembly, preparing it for re-use.
 89  
      * 
 90  
      *  @since 1.0.7
 91  
      * 
 92  
      **/
 93  
 
 94  
     public void clear()
 95  
     {
 96  0
         _buffer.setLength(0);
 97  0
         _lineLength = 0;
 98  
 
 99  0
         if (_parameters != null)
 100  0
             _parameters.clear();
 101  0
     }
 102  
 
 103  
     /**
 104  
      *  Maximum length of a line.
 105  
      *
 106  
      **/
 107  
 
 108  
     public int getMaxLineLength()
 109  
     {
 110  0
         return _maxLineLength;
 111  
     }
 112  
 
 113  
     /**
 114  
      *  Number of spaces to indent continuation lines by.
 115  
      *
 116  
      **/
 117  
 
 118  
     public int getIndent()
 119  
     {
 120  0
         return _indent;
 121  
     }
 122  
 
 123  
     /**
 124  
      *  Adds text to the current line, unless that would make the line too long, in
 125  
      *  which case a new line is started (and indented) before adding the text.
 126  
      *
 127  
      *  <p>Text is added as-is, with no concept of quoting.  To add arbitrary strings
 128  
      *  (such as in a where clause), use {@link #addParameter(String)}.
 129  
      *
 130  
      *
 131  
      **/
 132  
 
 133  
     public void add(String text)
 134  
     {
 135  
         int textLength;
 136  
 
 137  0
         textLength = text.length();
 138  
 
 139  0
         if (_lineLength + textLength > _maxLineLength)
 140  
         {
 141  0
             _buffer.append('\n');
 142  
 
 143  0
             for (int i = 0; i < _indent; i++)
 144  0
                 _buffer.append(' ');
 145  
 
 146  0
             _lineLength = _indent;
 147  
         }
 148  
 
 149  0
         _buffer.append(text);
 150  0
         _lineLength += textLength;
 151  0
     }
 152  
     
 153  
     public void add(short value)
 154  
     {
 155  0
         add(Short.toString(value));
 156  0
     }
 157  
     
 158  
     public void add(int value)
 159  
     {
 160  0
         add(Integer.toString(value));
 161  0
     }
 162  
     
 163  
     public void add(long value)
 164  
     {
 165  0
         add(Long.toString(value));
 166  0
     }
 167  
     
 168  
     public void add(float value)
 169  
     {
 170  0
         add(Float.toString(value));
 171  0
     }
 172  
     
 173  
     public void add(double value)
 174  
     {
 175  0
         add(Double.toString(value));
 176  0
     }
 177  
 
 178  
     /**
 179  
      *  Adds a date value to a {@link StatementAssembly} converting
 180  
      *  it to a {@link java.sql.Timestamp} first.
 181  
      *
 182  
      **/
 183  
 
 184  
     public void addParameter(Date date)
 185  
     {
 186  0
         if (date == null)
 187  
         {
 188  0
             add("NULL");
 189  0
             return;
 190  
         }
 191  
 
 192  0
         Calendar calendar = Calendar.getInstance();
 193  
 
 194  0
         calendar.setTime(date);
 195  0
         calendar.set(Calendar.MILLISECOND, 0);
 196  
 
 197  0
         Date adjusted = calendar.getTime();
 198  
 
 199  0
         Timestamp timestamp = new Timestamp(adjusted.getTime());
 200  
 
 201  0
         addParameter(timestamp);
 202  0
     }
 203  
 
 204  
     /** 
 205  
      *  Adds a separator (usually a comma and a space) to the current line, regardless
 206  
      *  of line length.  This is purely aesthetic ... it just looks odd if a separator
 207  
      *  gets wrapped to a new line by itself.
 208  
      *
 209  
      **/
 210  
 
 211  
     public void addSep(String text)
 212  
     {
 213  0
         _buffer.append(text);
 214  0
         _lineLength += text.length();
 215  0
     }
 216  
 
 217  
     /**
 218  
      *  Starts a new line, without indenting.
 219  
      *
 220  
      **/
 221  
 
 222  
     public void newLine()
 223  
     {
 224  0
         if (_buffer.length() != 0)
 225  0
             _buffer.append('\n');
 226  
 
 227  0
         _lineLength = 0;
 228  0
     }
 229  
 
 230  
     /**
 231  
      * Starts a new line, then adds the given text.
 232  
      *
 233  
      **/
 234  
 
 235  
     public void newLine(String text)
 236  
     {
 237  0
         if (_buffer.length() != 0)
 238  0
             _buffer.append('\n');
 239  
 
 240  0
         _buffer.append(text);
 241  
 
 242  0
         _lineLength = text.length();
 243  0
     }
 244  
 
 245  
     public void addList(String[] items, String separator)
 246  
     {
 247  0
         for (int i = 0; i < items.length; i++)
 248  
         {
 249  0
             if (i > 0)
 250  0
                 addSep(separator);
 251  
 
 252  0
             add(items[i]);
 253  
         }
 254  0
     }
 255  
 
 256  
     public void addParameterList(int[] items, String separator)
 257  
     {
 258  0
         for (int i = 0; i < items.length; i++)
 259  
         {
 260  0
             if (i > 0)
 261  0
                 addSep(separator);
 262  
 
 263  0
             addParameter(items[i]);
 264  
         }
 265  0
     }
 266  
 
 267  
     public void addParameterList(Integer[] items, String separator)
 268  
     {
 269  0
         for (int i = 0; i < items.length; i++)
 270  
         {
 271  0
             if (i > 0)
 272  0
                 addSep(separator);
 273  
 
 274  0
             addParameter(items[i]);
 275  
         }
 276  0
     }
 277  
 
 278  
     public void addParameterList(long[] items, String separator)
 279  
     {
 280  0
         for (int i = 0; i < items.length; i++)
 281  
         {
 282  0
             if (i > 0)
 283  0
                 addSep(separator);
 284  
 
 285  0
             addParameter(items[i]);
 286  
         }
 287  0
     }
 288  
 
 289  
     public void addParameterList(Long[] items, String separator)
 290  
     {
 291  0
         for (int i = 0; i < items.length; i++)
 292  
         {
 293  0
             if (i > 0)
 294  0
                 addSep(separator);
 295  
 
 296  0
             addParameter(items[i]);
 297  
         }
 298  0
     }
 299  
 
 300  
     public void addParameterList(String[] items, String separator)
 301  
     {
 302  0
         for (int i = 0; i < items.length; i++)
 303  
         {
 304  0
             if (i > 0)
 305  0
                 addSep(separator);
 306  
 
 307  0
             addParameter(items[i]);
 308  
         }
 309  0
     }
 310  
 
 311  
     public void addParameterList(double[] items, String separator)
 312  
     {
 313  0
         for (int i = 0; i < items.length; i++)
 314  
         {
 315  0
             if (i > 0)
 316  0
                 addSep(separator);
 317  
 
 318  0
             addParameter(items[i]);
 319  
         }
 320  0
     }
 321  
 
 322  
     public void addParameter(Object value)
 323  
     {
 324  0
         if (value == null)
 325  0
             add(NULL);
 326  
         else
 327  0
             addParameter(new ObjectParameter(value));
 328  0
     }
 329  
 
 330  
     public void addParameter(Timestamp timestamp)
 331  
     {
 332  0
         if (timestamp == null)
 333  0
             add(NULL);
 334  
         else
 335  0
             addParameter(new TimestampParameter(timestamp));
 336  0
     }
 337  
 
 338  
     public void addParameter(String value)
 339  
     {
 340  0
         if (value == null)
 341  0
             add(NULL);
 342  
         else
 343  0
             addParameter(new StringParameter(value));
 344  0
     }
 345  
 
 346  
     public void addParameter(int value)
 347  
     {
 348  0
         addParameter(new IntegerParameter(value));
 349  0
     }
 350  
 
 351  
     public void addParameter(Integer value)
 352  
     {
 353  0
         if (value == null)
 354  0
             add(NULL);
 355  
         else
 356  0
             addParameter(value.intValue());
 357  0
     }
 358  
 
 359  
     public void addParameter(long value)
 360  
     {
 361  0
         addParameter(new LongParameter(value));
 362  0
     }
 363  
 
 364  
     public void addParameter(Long value)
 365  
     {
 366  0
         if (value == null)
 367  0
             add(NULL);
 368  
         else
 369  0
             addParameter(value.longValue());
 370  0
     }
 371  
 
 372  
     public void addParameter(float value)
 373  
     {
 374  0
         addParameter(new FloatParameter(value));
 375  0
     }
 376  
 
 377  
     public void addParameter(Float value)
 378  
     {
 379  0
         if (value == null)
 380  0
             add(NULL);
 381  
         else
 382  0
             addParameter(value.floatValue());
 383  0
     }
 384  
 
 385  
     public void addParameter(double value)
 386  
     {
 387  0
         addParameter(new DoubleParameter(value));
 388  0
     }
 389  
 
 390  
     public void addParameter(Double value)
 391  
     {
 392  0
         if (value == null)
 393  0
             add(NULL);
 394  
         else
 395  0
             addParameter(value.doubleValue());
 396  0
     }
 397  
 
 398  
     public void addParameter(short value)
 399  
     {
 400  0
         addParameter(new ShortParameter(value));
 401  0
     }
 402  
 
 403  
     public void addParameter(Short value)
 404  
     {
 405  0
         if (value == null)
 406  0
             add(NULL);
 407  
         else
 408  0
             addParameter(value.shortValue());
 409  0
     }
 410  
 
 411  
     public void addParameter(boolean value)
 412  
     {
 413  0
         addParameter(value ? BooleanParameter.TRUE : BooleanParameter.FALSE);
 414  0
     }
 415  
 
 416  
     public void addParameter(Boolean value)
 417  
     {
 418  0
         if (value == null)
 419  0
             add(NULL);
 420  
         else
 421  0
             addParameter(value.booleanValue());
 422  0
     }
 423  
 
 424  
     private void addParameter(IParameter parameter)
 425  
     {
 426  0
         if (_parameters == null)
 427  0
             _parameters = new ArrayList();
 428  
 
 429  0
         _parameters.add(parameter);
 430  
 
 431  0
         add("?");
 432  0
     }
 433  
 
 434  
     /**
 435  
      *  Creates and returns an {@link IStatement} based on the SQL and parameters
 436  
      *  acquired.
 437  
      *
 438  
      **/
 439  
 
 440  
     public IStatement createStatement(Connection connection) throws SQLException
 441  
     {
 442  0
         String sql = _buffer.toString();
 443  
 
 444  0
         if (_parameters == null || _parameters.isEmpty())
 445  0
             return new SimpleStatement(sql, connection);
 446  
 
 447  0
         return new ParameterizedStatement(sql, connection, _parameters);
 448  
     }
 449  
 
 450  
     public String toString()
 451  
     {
 452  0
         StringBuffer buffer = new StringBuffer("StatementAssembly@");
 453  
 
 454  0
         buffer.append(Integer.toHexString(hashCode()));
 455  0
         buffer.append("[SQL=\n<");
 456  0
         buffer.append(_buffer);
 457  0
         buffer.append("\n>");
 458  
 
 459  0
         if (_parameters != null)
 460  
         {
 461  0
             int count = _parameters.size();
 462  0
             for (int i = 0; i < count; i++)
 463  
             {
 464  0
                 Object parameter = _parameters.get(i);
 465  
 
 466  0
                 buffer.append(" ?");
 467  0
                 buffer.append(i + 1);
 468  0
                 buffer.append('=');
 469  
 
 470  0
                 buffer.append(parameter);
 471  
             }
 472  
         }
 473  
 
 474  0
         buffer.append(']');
 475  
 
 476  0
         return buffer.toString();
 477  
     }
 478  
 }