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.CallableStatement;
38  import java.sql.Clob;
39  import java.sql.Date;
40  import java.sql.Ref;
41  import java.sql.SQLException;
42  import java.sql.Time;
43  import java.sql.Timestamp;
44  import java.util.Calendar;
45  import java.util.HashMap;
46  import java.util.List;
47  import java.util.Map;
48  
49  /***
50   * A delegation pattern wrapper for java.sql.CallableStatement.
51   *
52   * @author <a href="rafal@caltha.pl">RafaƂ Krzewski</a>
53   * @version $Id: DelegatingCallableStatement.java,v 1.2 2005/10/10 08:27:46 rafal Exp $
54   */
55  @SuppressWarnings("deprecation")
56  public class DelegatingCallableStatement
57      extends DelegatingPreparedStatement
58      implements CallableStatement
59  {
60  
61      private final CallableStatement callableStatement;
62      
63      private final Map<String, Object> parameterMap = new HashMap<String, Object>();
64  
65      /***
66       * Creates a new DelegatingCallableStatement instance.
67       *
68       * @param callableStatement the delegate callable statement.
69       * @param sql the statement body.
70       */
71      public DelegatingCallableStatement(CallableStatement callableStatement, String sql)
72      {
73          super(callableStatement, sql);
74          this.callableStatement = callableStatement;
75      }
76      
77      private void setParameter(String parameterName, Object value)
78      {
79          parameterMap.put(parameterName, value);
80      }
81      
82      /***
83       * {@inheritDoc}
84       */
85      @Override
86      protected void clearParameters2()
87      {
88          super.clearParameters2();
89          parameterMap.clear();        
90      }
91      
92      /***
93       * {@inheritDoc}
94       */
95      @Override
96      protected String getBody()
97      {
98          List<Object> parameterList = getParameterList();
99          StringBuilder body = new StringBuilder();
100         body.append(getSQL());
101         if(!parameterList.isEmpty() || !parameterMap.isEmpty())
102         {
103             body.append(" with ");
104         }
105         if(!parameterList.isEmpty())
106         {
107             body.append(parameterList.toString());
108         }
109         if(!parameterList.isEmpty() && !parameterMap.isEmpty())
110         {
111             body.append(", ");
112         }
113         if(!parameterMap.isEmpty())
114         {
115             body.append(parameterMap.toString());
116         }
117         return body.toString();
118     }
119 
120     // .. CallableStatement .....................................................................
121     
122     /***
123      * {@inheritDoc}
124      */
125     public void registerOutParameter(int parameterIndex, int sqlType)
126         throws SQLException
127     {
128         callableStatement.registerOutParameter(parameterIndex, sqlType);
129     }
130 
131     /***
132      * {@inheritDoc}
133      */
134     public void registerOutParameter(int parameterIndex, int sqlType, int scale)
135         throws SQLException
136     {
137         callableStatement.registerOutParameter(parameterIndex, sqlType, scale);
138     }
139 
140     /***
141      * {@inheritDoc}
142      */
143     public void registerOutParameter(int paramIndex, int sqlType, String typeName)
144         throws SQLException
145     {
146         callableStatement.registerOutParameter(paramIndex, sqlType, typeName);
147     }
148 
149     /***
150      * {@inheritDoc}
151      */
152     public void registerOutParameter(String parameterName, int sqlType)
153         throws SQLException
154     {
155         callableStatement.registerOutParameter(parameterName, sqlType);
156     }
157 
158     /***
159      * {@inheritDoc}
160      */
161     public void registerOutParameter(String parameterName, int sqlType, int scale)
162         throws SQLException
163     {
164         callableStatement.registerOutParameter(parameterName, sqlType, scale);
165     }
166 
167     /***
168      * {@inheritDoc}
169      */
170     public void registerOutParameter(String parameterName, int sqlType, String typeName)
171         throws SQLException
172     {
173         callableStatement.registerOutParameter(parameterName, sqlType, typeName);
174     }
175 
176     /***
177      * {@inheritDoc}
178      */
179     public boolean wasNull()
180         throws SQLException
181     {
182         return callableStatement.wasNull();
183     }
184 
185     /***
186      * {@inheritDoc}
187      */
188     public String getString(int parameterIndex)
189         throws SQLException
190     {
191         return callableStatement.getString(parameterIndex);
192     }
193 
194     /***
195      * {@inheritDoc}
196      */
197     public boolean getBoolean(int parameterIndex)
198         throws SQLException
199     {
200         return callableStatement.getBoolean(parameterIndex);
201     }
202 
203     /***
204      * {@inheritDoc}
205      */
206     public byte getByte(int parameterIndex)
207         throws SQLException
208     {
209         return callableStatement.getByte(parameterIndex);
210     }
211 
212     /***
213      * {@inheritDoc}
214      */
215     public short getShort(int parameterIndex)
216         throws SQLException
217     {
218         return callableStatement.getShort(parameterIndex);
219     }
220 
221     /***
222      * {@inheritDoc}
223      */
224     public int getInt(int parameterIndex)
225         throws SQLException
226     {
227         return callableStatement.getInt(parameterIndex);
228     }
229 
230     /***
231      * {@inheritDoc}
232      */
233     public long getLong(int parameterIndex)
234         throws SQLException
235     {
236         return callableStatement.getLong(parameterIndex);
237     }
238 
239     /***
240      * {@inheritDoc}
241      */
242     public float getFloat(int parameterIndex)
243         throws SQLException
244     {
245         return callableStatement.getFloat(parameterIndex);
246     }
247 
248     /***
249      * {@inheritDoc}
250      */
251     public double getDouble(int parameterIndex)
252         throws SQLException
253     {
254         return callableStatement.getDouble(parameterIndex);
255     }
256     
257     /***
258      * {@inheritDoc}
259      */
260     public BigDecimal getBigDecimal(int parameterIndex)
261     throws SQLException
262     {
263         return callableStatement.getBigDecimal(parameterIndex);
264     }
265 
266     /***
267      * {@inheritDoc}
268      */
269     public BigDecimal getBigDecimal(int parameterIndex, int scale)
270         throws SQLException
271     {
272         return callableStatement.getBigDecimal(parameterIndex, scale);
273     }
274 
275     /***
276      * {@inheritDoc}
277      */
278     public byte[] getBytes(int parameterIndex)
279         throws SQLException
280     {
281         return callableStatement.getBytes(parameterIndex);
282     }
283 
284     /***
285      * {@inheritDoc}
286      */
287     public Date getDate(int parameterIndex)
288         throws SQLException
289     {
290         return callableStatement.getDate(parameterIndex);
291     }
292 
293     /***
294      * {@inheritDoc}
295      */
296     public Time getTime(int parameterIndex)
297         throws SQLException
298     {
299         return callableStatement.getTime(parameterIndex);
300     }
301 
302     /***
303      * {@inheritDoc}
304      */
305     public Timestamp getTimestamp(int parameterIndex)
306         throws SQLException
307     {
308         return callableStatement.getTimestamp(parameterIndex);
309     }
310 
311     /***
312      * {@inheritDoc}
313      */
314     public Object getObject(int parameterIndex)
315         throws SQLException
316     {
317         return callableStatement.getObject(parameterIndex);
318     }
319 
320     /***
321      * {@inheritDoc}
322      */
323     public Object getObject(int parameterIndex, Map<String, Class<?>> typeMap)
324         throws SQLException
325     {
326         return callableStatement.getObject(parameterIndex, typeMap);
327     }
328 
329     /***
330      * {@inheritDoc}
331      */
332     public Ref getRef(int i)
333         throws SQLException
334     {
335         return callableStatement.getRef(i);
336     }
337 
338     /***
339      * {@inheritDoc}
340      */
341     public Blob getBlob(int i)
342         throws SQLException
343     {
344         return callableStatement.getBlob(i);
345     }
346 
347     /***
348      * {@inheritDoc}
349      */
350     public Clob getClob(int i)
351         throws SQLException
352     {
353         return callableStatement.getClob(i);
354     }
355 
356     /***
357      * {@inheritDoc}
358      */
359     public Array getArray(int i)
360         throws SQLException
361     {
362         return callableStatement.getArray(i);
363     }
364 
365     /***
366      * {@inheritDoc}
367      */
368     public Date getDate(int parameterIndex, Calendar cal)
369         throws SQLException
370     {
371         return callableStatement.getDate(parameterIndex, cal);
372     }
373 
374     /***
375      * {@inheritDoc}
376      */
377     public Time getTime(int parameterIndex, Calendar cal)
378         throws SQLException
379     {
380         return callableStatement.getTime(parameterIndex, cal);
381     }
382 
383     /***
384      * {@inheritDoc}
385      */
386     public Timestamp getTimestamp(int parameterIndex, Calendar cal)
387         throws SQLException
388     {
389         return callableStatement.getTimestamp(parameterIndex, cal);
390     }
391 
392     /***
393      * {@inheritDoc}
394      */
395     public URL getURL(int parameterIndex)
396         throws SQLException
397     {
398         return callableStatement.getURL(parameterIndex);
399     }
400 
401     /***
402      * {@inheritDoc}
403      */
404     public String getString(String parameterName)
405         throws SQLException
406     {
407         return callableStatement.getString(parameterName);
408     }
409 
410     /***
411      * {@inheritDoc}
412      */
413     public boolean getBoolean(String parameterName)
414         throws SQLException
415     {
416         return callableStatement.getBoolean(parameterName);
417     }
418 
419     /***
420      * {@inheritDoc}
421      */
422     public byte getByte(String parameterName)
423         throws SQLException
424     {
425         return callableStatement.getByte(parameterName);
426     }
427 
428     /***
429      * {@inheritDoc}
430      */
431     public short getShort(String parameterName)
432         throws SQLException
433     {
434         return callableStatement.getShort(parameterName);
435     }
436 
437     /***
438      * {@inheritDoc}
439      */
440     public int getInt(String parameterName)
441         throws SQLException
442     {
443         return callableStatement.getInt(parameterName);
444     }
445 
446     /***
447      * {@inheritDoc}
448      */
449     public long getLong(String parameterName)
450         throws SQLException
451     {
452         return callableStatement.getLong(parameterName);
453     }
454 
455     /***
456      * {@inheritDoc}
457      */
458     public float getFloat(String parameterName)
459         throws SQLException
460     {
461         return callableStatement.getFloat(parameterName);
462     }
463 
464     /***
465      * {@inheritDoc}
466      */
467     public double getDouble(String parameterName)
468         throws SQLException
469     {
470         return callableStatement.getDouble(parameterName);
471     }
472 
473     /***
474      * {@inheritDoc}
475      */
476     public byte[] getBytes(String parameterName)
477         throws SQLException
478     {
479         return callableStatement.getBytes(parameterName);
480     }
481 
482     /***
483      * {@inheritDoc}
484      */
485     public Date getDate(String parameterName)
486         throws SQLException
487     {
488         return callableStatement.getDate(parameterName);
489     }
490 
491     /***
492      * {@inheritDoc}
493      */
494     public Time getTime(String parameterName)
495         throws SQLException
496     {
497         return callableStatement.getTime(parameterName);
498     }
499 
500     /***
501      * {@inheritDoc}
502      */
503     public Timestamp getTimestamp(String parameterName)
504         throws SQLException
505     {
506         return callableStatement.getTimestamp(parameterName);
507     }
508 
509     /***
510      * {@inheritDoc}
511      */
512     public BigDecimal getBigDecimal(String parameterName)
513         throws SQLException
514     {
515         return callableStatement.getBigDecimal(parameterName);
516     }
517 
518     /***
519      * {@inheritDoc}
520      */
521     public Object getObject(String parameterName)
522         throws SQLException
523     {
524         return callableStatement.getObject(parameterName);
525     }
526 
527     /***
528      * {@inheritDoc}
529      */
530     public Object getObject(String parameterName, Map<String, Class<?>> typeMap)
531         throws SQLException
532     {
533         return callableStatement.getObject(parameterName, typeMap);
534     }
535 
536     /***
537      * {@inheritDoc}
538      */
539     public Ref getRef(String parameterName)
540         throws SQLException
541     {
542         return callableStatement.getRef(parameterName);
543     }
544 
545     /***
546      * {@inheritDoc}
547      */
548     public Blob getBlob(String parameterName)
549         throws SQLException
550     {
551         return callableStatement.getBlob(parameterName);
552     }
553 
554     /***
555      * {@inheritDoc}
556      */
557     public Clob getClob(String parameterName)
558         throws SQLException
559     {
560         return callableStatement.getClob(parameterName);
561     }
562 
563     /***
564      * {@inheritDoc}
565      */
566     public Array getArray(String parameterName)
567         throws SQLException
568     {
569         return callableStatement.getArray(parameterName);
570     }
571 
572     /***
573      * {@inheritDoc}
574      */
575     public Date getDate(String parameterName, Calendar cal)
576         throws SQLException
577     {
578         return callableStatement.getDate(parameterName, cal);
579     }
580 
581     /***
582      * {@inheritDoc}
583      */
584     public Time getTime(String parameterName, Calendar cal)
585         throws SQLException
586     {
587         return callableStatement.getTime(parameterName, cal);
588     }
589 
590     /***
591      * {@inheritDoc}
592      */
593     public Timestamp getTimestamp(String parameterName, Calendar cal)
594         throws SQLException
595     {
596         return callableStatement.getTimestamp(parameterName, cal);
597     }
598 
599     /***
600      * {@inheritDoc}
601      */
602     public URL getURL(String parameterName)
603         throws SQLException
604     {
605         return callableStatement.getURL(parameterName);
606     }
607 
608     /***
609      * {@inheritDoc}
610      */
611     public void setURL(String parameterName, URL x)
612         throws SQLException
613     {
614         setParameter(parameterName, x);
615         callableStatement.setURL(parameterName, x);
616     }
617 
618     /***
619      * {@inheritDoc}
620      */
621     public void setNull(String parameterName, int sqlType)
622         throws SQLException
623     {
624         setParameter(parameterName, "NULL");
625         callableStatement.setNull(parameterName, sqlType);
626     }
627 
628     /***
629      * {@inheritDoc}
630      */
631     public void setBoolean(String parameterName, boolean x)
632         throws SQLException
633     {
634         setParameter(parameterName, x);
635         callableStatement.setBoolean(parameterName, x);
636     }
637 
638     /***
639      * {@inheritDoc}
640      */
641     public void setByte(String parameterName, byte x)
642         throws SQLException
643     {
644         setParameter(parameterName, x);
645         callableStatement.setByte(parameterName, x);
646     }
647 
648     /***
649      * {@inheritDoc}
650      */
651     public void setShort(String parameterName, short x)
652         throws SQLException
653     {
654         setParameter(parameterName, x);
655         callableStatement.setShort(parameterName, x);
656     }
657 
658     /***
659      * {@inheritDoc}
660      */
661     public void setInt(String parameterName, int x)
662         throws SQLException
663     {
664         setParameter(parameterName, x);
665         callableStatement.setInt(parameterName, x);
666     }
667 
668     /***
669      * {@inheritDoc}
670      */
671     public void setLong(String parameterName, long x)
672         throws SQLException
673     {
674         setParameter(parameterName, x);
675         callableStatement.setLong(parameterName, x);
676     }
677 
678     /***
679      * {@inheritDoc}
680      */
681     public void setFloat(String parameterName, float x)
682         throws SQLException
683     {
684         setParameter(parameterName, x);
685         callableStatement.setFloat(parameterName, x);
686     }
687 
688     /***
689      * {@inheritDoc}
690      */
691     public void setDouble(String parameterName, double x)
692         throws SQLException
693     {
694         setParameter(parameterName, x);
695         callableStatement.setDouble(parameterName, x);
696     }
697 
698     /***
699      * {@inheritDoc}
700      */
701     public void setBigDecimal(String parameterName, BigDecimal x)
702         throws SQLException
703     {
704         setParameter(parameterName, x);
705         callableStatement.setBigDecimal(parameterName, x);
706     }
707 
708     /***
709      * {@inheritDoc}
710      */
711     public void setString(String parameterName, String x)
712         throws SQLException
713     {
714         setParameter(parameterName, x);
715         callableStatement.setString(parameterName, x);
716     }
717 
718     /***
719      * {@inheritDoc}
720      */
721     public void setBytes(String parameterName, byte[] x)
722         throws SQLException
723     {
724         setParameter(parameterName, x);
725         callableStatement.setBytes(parameterName, x);
726     }
727 
728     /***
729      * {@inheritDoc}
730      */
731     public void setDate(String parameterName, Date x)
732         throws SQLException
733     {
734         setParameter(parameterName, x);
735         callableStatement.setDate(parameterName, x);
736     }
737 
738     /***
739      * {@inheritDoc}
740      */
741     public void setTime(String parameterName, Time x)
742         throws SQLException
743     {
744         setParameter(parameterName, x);
745         callableStatement.setTime(parameterName, x);
746     }
747 
748     /***
749      * {@inheritDoc}
750      */
751     public void setTimestamp(String parameterName, Timestamp x)
752         throws SQLException
753     {
754         setParameter(parameterName, x);
755         callableStatement.setTimestamp(parameterName, x);
756     }
757 
758     /***
759      * {@inheritDoc}
760      */
761     public void setAsciiStream(String parameterName, InputStream x, int length)
762         throws SQLException
763     {
764         setParameter(parameterName, x);
765         callableStatement.setAsciiStream(parameterName, x, length);
766     }
767 
768     /***
769      * {@inheritDoc}
770      */
771     public void setBinaryStream(String parameterName, InputStream x, int length)
772         throws SQLException
773     {
774         setParameter(parameterName, x);
775         callableStatement.setBinaryStream(parameterName, x, length);
776     }
777 
778     /***
779      * {@inheritDoc}
780      */
781     public void setObject(String parameterName, Object x, int targetSqlType, int scale)
782         throws SQLException
783     {
784         setParameter(parameterName, x);
785         callableStatement.setObject(parameterName, x, targetSqlType, scale);
786     }
787 
788     /***
789      * {@inheritDoc}
790      */
791     public void setObject(String parameterName, Object x, int targetSqlType)
792         throws SQLException
793     {
794         setParameter(parameterName, x);
795         callableStatement.setObject(parameterName, x, targetSqlType);
796     }
797 
798     /***
799      * {@inheritDoc}
800      */
801     public void setObject(String parameterName, Object x)
802         throws SQLException
803     {
804         setParameter(parameterName, x);
805         callableStatement.setObject(parameterName, x);
806     }
807 
808     /***
809      * {@inheritDoc}
810      */
811     public void setCharacterStream(String parameterName, Reader reader, int length)
812         throws SQLException
813     {
814         setParameter(parameterName, reader);
815         callableStatement.setCharacterStream(parameterName, reader, length);
816     }
817 
818     /***
819      * {@inheritDoc}
820      */
821     public void setDate(String parameterName, Date x, Calendar cal)
822         throws SQLException
823     {
824         setParameter(parameterName, x);
825         callableStatement.setDate(parameterName, x, cal);
826     }
827 
828     /***
829      * {@inheritDoc}
830      */
831     public void setTime(String parameterName, Time x, Calendar cal)
832         throws SQLException
833     {
834         setParameter(parameterName, x);
835         callableStatement.setTime(parameterName, x, cal);
836     }
837 
838     /***
839      * {@inheritDoc}
840      */
841     public void setTimestamp(String parameterName, Timestamp x, Calendar cal)
842         throws SQLException
843     {
844         setParameter(parameterName, x);
845         callableStatement.setTimestamp(parameterName, x, cal);
846     }
847 
848     /***
849      * {@inheritDoc}
850      */
851     public void setNull(String parameterName, int sqlType, String typeName)
852         throws SQLException
853     {
854         setParameter(parameterName, "NULL");
855         callableStatement.setNull(parameterName, sqlType, typeName);
856     }
857 }