View Javadoc

1   // 
2   // Copyright (c) 2003-2005, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   // All rights reserved. 
4   //   
5   // Redistribution and use in source and binary forms, with or without modification,  
6   // are permitted provided that the following conditions are met: 
7   //   
8   // * Redistributions of source code must retain the above copyright notice,  
9   // this list of conditions and the following disclaimer. 
10  // * Redistributions in binary form must reproduce the above copyright notice,  
11  // this list of conditions and the following disclaimer in the documentation  
12  // and/or other materials provided with the distribution. 
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  // nor the names of its contributors may be used to endorse or promote products  
15  // derived from this software without specific prior written permission. 
16  // 
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  // POSSIBILITY OF SUCH DAMAGE. 
27  //
28  
29  package org.objectledge.database.impl;
30  
31  import java.io.InputStream;
32  import java.io.Reader;
33  import java.math.BigDecimal;
34  import java.net.URL;
35  import java.sql.Array;
36  import java.sql.Blob;
37  import java.sql.Clob;
38  import java.sql.Date;
39  import java.sql.ParameterMetaData;
40  import java.sql.PreparedStatement;
41  import java.sql.Ref;
42  import java.sql.ResultSet;
43  import java.sql.ResultSetMetaData;
44  import java.sql.SQLException;
45  import java.sql.Time;
46  import java.sql.Timestamp;
47  import java.util.ArrayList;
48  import java.util.Calendar;
49  import java.util.List;
50  
51  /***
52   * A delegation pattern wrapper for java.sql.PreparedStatement.
53   *
54   * @author <a href="rafal@caltha.pl">RafaƂ Krzewski</a>
55   * @version $Id: DelegatingPreparedStatement.java,v 1.4 2005/10/10 09:44:28 rafal Exp $
56   */
57  @SuppressWarnings("deprecation")
58  public class DelegatingPreparedStatement
59      extends DelegatingStatement
60      implements PreparedStatement
61  {
62      private final PreparedStatement preparedStatement;
63      
64      private final String sql;
65      
66      private final ArrayList<Object> parameterList = new ArrayList<Object>();
67      
68      /***
69       * Creates a new DelegatingPreparedStatement instance.
70       *
71       * @param preparedStatement the delegate prepared statement.
72       * @param sql the statement body.
73       */
74      public DelegatingPreparedStatement(final PreparedStatement preparedStatement, final String sql)
75      {
76          super(preparedStatement);
77          this.preparedStatement = preparedStatement;
78          this.sql = sql;
79      }
80      
81      private void setParameter(int parameterIndex, Object value)
82      {
83          parameterList.ensureCapacity(parameterIndex);
84          if(parameterIndex <= parameterList.size())
85          {
86              parameterList.set(parameterIndex - 1, value);
87          }
88          else
89          {
90              for(int i  = parameterList.size() + 1; i < parameterIndex; i++)
91              {
92                  parameterList.add(null);
93              }
94              parameterList.add(value);
95          }
96      }
97      
98      /***
99       * Returns the statements SQL string.
100      * 
101      * @return the statements SQL string.
102      */
103     protected String getSQL()
104     {
105         return sql;
106     }
107     
108     /***
109      * Returns the current parameter list.
110      * 
111      * @return the current parameter list.
112      */
113     protected List<Object> getParameterList()
114     {
115         return parameterList;
116     }
117     
118     /***
119      * Clear parmateter information stored by the wrapper.
120      * 
121      * <p>remember to invoke super.clearParameters2()</p>
122      */
123     protected void clearParameters2()
124     {
125         parameterList.clear();        
126     }
127 
128     /***
129      * Returns the statement body with current parameters.
130      * 
131      * @return the statement body with current parameters.
132      */
133     protected String getBody()
134     {
135         StringBuilder body = new StringBuilder();
136         body.append(sql);
137         if(!parameterList.isEmpty())
138         {
139             body.append(" with ");
140             body.append(parameterList.toString());
141         }
142         return body.toString();
143     }
144        
145     // .. PreparedStatement ..................................................................... 
146     
147     /***
148      * {@inheritDoc}
149      */
150     public void addBatch()
151         throws SQLException
152     {
153         addToBatchBuffer(getBody());
154         preparedStatement.addBatch();
155     }
156 
157     /***
158      * {@inheritDoc}
159      */
160     public void clearParameters()
161         throws SQLException
162     {
163         clearParameters2();
164         preparedStatement.clearParameters();
165     }
166 
167     /***
168      * {@inheritDoc}
169      */
170     public boolean execute()
171         throws SQLException
172     {
173         clearParameters2();
174         return preparedStatement.execute();
175     }
176 
177     /***
178      * {@inheritDoc}
179      */
180     public ResultSet executeQuery()
181         throws SQLException
182     {
183         clearParameters2();
184         return preparedStatement.executeQuery();
185     }
186 
187     /***
188      * {@inheritDoc}
189      */
190     public int executeUpdate()
191         throws SQLException
192     {
193         clearParameters2();
194         return preparedStatement.executeUpdate();
195     }
196 
197     /***
198      * {@inheritDoc}
199      */
200     public ResultSetMetaData getMetaData()
201         throws SQLException
202     {
203         return preparedStatement.getMetaData();
204     }
205 
206     /***
207      * {@inheritDoc}
208      */
209     public ParameterMetaData getParameterMetaData()
210         throws SQLException
211     {
212         return preparedStatement.getParameterMetaData();
213     }
214 
215     /***
216      * {@inheritDoc}
217      */
218     public void setArray(int parameterIndex, Array x)
219         throws SQLException
220     {
221         setParameter(parameterIndex, x);
222         preparedStatement.setArray(parameterIndex, x);
223     }
224 
225     /***
226      * {@inheritDoc}
227      */
228     public void setAsciiStream(int parameterIndex, InputStream x, int length)
229         throws SQLException
230     {
231         setParameter(parameterIndex, x);
232         preparedStatement.setAsciiStream(parameterIndex, x, length);
233     }
234 
235     /***
236      * {@inheritDoc}
237      */
238     public void setBigDecimal(int parameterIndex, BigDecimal x)
239         throws SQLException
240     {
241         setParameter(parameterIndex, x);
242         preparedStatement.setBigDecimal(parameterIndex, x);
243     }
244 
245     /***
246      * {@inheritDoc}
247      */
248     public void setBinaryStream(int parameterIndex, InputStream x, int length)
249         throws SQLException
250     {
251         setParameter(parameterIndex, x);
252         preparedStatement.setBinaryStream(parameterIndex, x, length);
253     }
254 
255     /***
256      * {@inheritDoc}
257      */
258     public void setBlob(int parameterIndex, Blob x)
259         throws SQLException
260     {
261         setParameter(parameterIndex, x);
262         preparedStatement.setBlob(parameterIndex, x);
263     }
264 
265     /***
266      * {@inheritDoc}
267      */
268     public void setBoolean(int parameterIndex, boolean x)
269         throws SQLException
270     {
271         setParameter(parameterIndex, x);
272         preparedStatement.setBoolean(parameterIndex, x);
273     }
274 
275     /***
276      * {@inheritDoc}
277      */
278     public void setByte(int parameterIndex, byte x)
279         throws SQLException
280     {
281         setParameter(parameterIndex, x);
282         preparedStatement.setByte(parameterIndex, x);
283     }
284 
285     /***
286      * {@inheritDoc}
287      */
288     public void setBytes(int parameterIndex, byte[] x)
289         throws SQLException
290     {
291         setParameter(parameterIndex, x);
292         preparedStatement.setBytes(parameterIndex, x);
293     }
294 
295     /***
296      * {@inheritDoc}
297      */
298     public void setCharacterStream(int parameterIndex, Reader reader, int length)
299         throws SQLException
300     {
301         setParameter(parameterIndex, reader);
302         preparedStatement.setCharacterStream(parameterIndex, reader, length);
303     }
304 
305     /***
306      * {@inheritDoc}
307      */
308     public void setClob(int parameterIndex, Clob x)
309         throws SQLException
310     {
311         setParameter(parameterIndex, x);
312         preparedStatement.setClob(parameterIndex, x);
313     }
314 
315     /***
316      * {@inheritDoc}
317      */
318     public void setDate(int parameterIndex, Date x, Calendar cal)
319         throws SQLException
320     {
321         setParameter(parameterIndex, x);
322         preparedStatement.setDate(parameterIndex, x, cal);
323     }
324 
325     /***
326      * {@inheritDoc}
327      */
328     public void setDate(int parameterIndex, Date x)
329         throws SQLException
330     {
331         setParameter(parameterIndex, x);
332         preparedStatement.setDate(parameterIndex, x);
333     }
334 
335     /***
336      * {@inheritDoc}
337      */
338     public void setDouble(int parameterIndex, double x)
339         throws SQLException
340     {
341         setParameter(parameterIndex, x);
342         preparedStatement.setDouble(parameterIndex, x);
343     }
344 
345     /***
346      * {@inheritDoc}
347      */
348     public void setFloat(int parameterIndex, float x)
349         throws SQLException
350     {
351         setParameter(parameterIndex, x);
352         preparedStatement.setFloat(parameterIndex, x);
353     }
354 
355     /***
356      * {@inheritDoc}
357      */
358     public void setInt(int parameterIndex, int x)
359         throws SQLException
360     {
361         setParameter(parameterIndex, x);
362         preparedStatement.setInt(parameterIndex, x);
363     }
364 
365     /***
366      * {@inheritDoc}
367      */
368     public void setLong(int parameterIndex, long x)
369         throws SQLException
370     {
371         setParameter(parameterIndex, x);
372         preparedStatement.setLong(parameterIndex, x);
373     }
374 
375     /***
376      * {@inheritDoc}
377      */
378     public void setNull(int parameterIndex, int sqlType, String typeName)
379         throws SQLException
380     {
381         setParameter(parameterIndex, "NULL");
382         preparedStatement.setNull(parameterIndex, sqlType, typeName);
383     }
384 
385     /***
386      * {@inheritDoc}
387      */
388     public void setNull(int parameterIndex, int sqlType)
389         throws SQLException
390     {
391         setParameter(parameterIndex, "NULL");
392         preparedStatement.setNull(parameterIndex, sqlType);
393     }
394 
395     /***
396      * {@inheritDoc}
397      */
398     public void setObject(int parameterIndex, Object x, int targetSqlType, int scale)
399         throws SQLException
400     {
401         setParameter(parameterIndex, x);
402         preparedStatement.setObject(parameterIndex, x, targetSqlType, scale);
403     }
404 
405     /***
406      * {@inheritDoc}
407      */
408     public void setObject(int parameterIndex, Object x, int targetSqlType)
409         throws SQLException
410     {
411         setParameter(parameterIndex, x);
412         preparedStatement.setObject(parameterIndex, x, targetSqlType);
413     }
414 
415     /***
416      * {@inheritDoc}
417      */
418     public void setObject(int parameterIndex, Object x)
419         throws SQLException
420     {
421         setParameter(parameterIndex, x);
422         preparedStatement.setObject(parameterIndex, x);
423     }
424 
425     /***
426      * {@inheritDoc}
427      */
428     public void setRef(int parameterIndex, Ref x)
429         throws SQLException
430     {
431         setParameter(parameterIndex, x);
432         preparedStatement.setRef(parameterIndex, x);
433     }
434 
435     /***
436      * {@inheritDoc}
437      */
438     public void setShort(int parameterIndex, short x)
439         throws SQLException
440     {
441         setParameter(parameterIndex, x);
442         preparedStatement.setShort(parameterIndex, x);
443     }
444 
445     /***
446      * {@inheritDoc}
447      */
448     public void setString(int parameterIndex, String x)
449         throws SQLException
450     {
451         setParameter(parameterIndex, x);
452         preparedStatement.setString(parameterIndex, x);
453     }
454 
455     /***
456      * {@inheritDoc}
457      */
458     public void setTime(int parameterIndex, Time x, Calendar cal)
459         throws SQLException
460     {
461         setParameter(parameterIndex, x);
462         preparedStatement.setTime(parameterIndex, x, cal);
463     }
464 
465     /***
466      * {@inheritDoc}
467      */
468     public void setTime(int parameterIndex, Time x)
469         throws SQLException
470     {
471         setParameter(parameterIndex, x);
472         preparedStatement.setTime(parameterIndex, x);
473     }
474 
475     /***
476      * {@inheritDoc}
477      */
478     public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal)
479         throws SQLException
480     {
481         setParameter(parameterIndex, x);
482         preparedStatement.setTimestamp(parameterIndex, x, cal);
483     }
484 
485     /***
486      * {@inheritDoc}
487      */
488     public void setTimestamp(int parameterIndex, Timestamp x)
489         throws SQLException
490     {
491         setParameter(parameterIndex, x);
492         preparedStatement.setTimestamp(parameterIndex, x);
493     }
494 
495     /***
496      * {@inheritDoc}
497      */
498     public void setUnicodeStream(int parameterIndex, InputStream x, int length)
499         throws SQLException
500     {
501         setParameter(parameterIndex, x);
502         preparedStatement.setUnicodeStream(parameterIndex, x, length);
503     }
504 
505     /***
506      * {@inheritDoc}
507      */
508     public void setURL(int parameterIndex, URL x)
509         throws SQLException
510     {
511         setParameter(parameterIndex, x);
512         preparedStatement.setURL(parameterIndex, x);
513     }
514 }