KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > dbutils > wrappers > SqlNullCheckedResultSet


1 /*
2  * $Header: /home/cvs/jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/wrappers/SqlNullCheckedResultSet.java,v 1.3 2003/11/09 18:18:04 dgraham Exp $
3  * $Revision: 1.3 $
4  * $Date: 2003/11/09 18:18:04 $
5  *
6  * ====================================================================
7  *
8  * The Apache Software License, Version 1.1
9  *
10  * Copyright (c) 2003 The Apache Software Foundation. All rights
11  * reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  *
20  * 2. Redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in
22  * the documentation and/or other materials provided with the
23  * distribution.
24  *
25  * 3. The end-user documentation included with the redistribution, if
26  * any, must include the following acknowledgement:
27  * "This product includes software developed by the
28  * Apache Software Foundation (http://www.apache.org/)."
29  * Alternately, this acknowledgement may appear in the software itself,
30  * if and wherever such third-party acknowledgements normally appear.
31  *
32  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33  * Foundation" must not be used to endorse or promote products derived
34  * from this software without prior written permission. For written
35  * permission, please contact apache@apache.org.
36  *
37  * 5. Products derived from this software may not be called "Apache"
38  * nor may "Apache" appear in their names without prior written
39  * permission of the Apache Software Foundation.
40  *
41  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  * ====================================================================
54  *
55  * This software consists of voluntary contributions made by many
56  * individuals on behalf of the Apache Software Foundation. For more
57  * information on the Apache Software Foundation, please see
58  * <http://www.apache.org/>.
59  */

60
61 package org.apache.commons.dbutils.wrappers;
62
63 import java.io.InputStream JavaDoc;
64 import java.io.Reader JavaDoc;
65 import java.lang.reflect.InvocationHandler JavaDoc;
66 import java.lang.reflect.Method JavaDoc;
67 import java.math.BigDecimal JavaDoc;
68 import java.net.URL JavaDoc;
69 import java.sql.Blob JavaDoc;
70 import java.sql.Clob JavaDoc;
71 import java.sql.Date JavaDoc;
72 import java.sql.Ref JavaDoc;
73 import java.sql.ResultSet JavaDoc;
74 import java.sql.Time JavaDoc;
75 import java.sql.Timestamp JavaDoc;
76 import java.util.HashMap JavaDoc;
77 import java.util.Map JavaDoc;
78
79 import org.apache.commons.dbutils.ProxyFactory;
80
81 /**
82  * Decorates a <code>ResultSet</code> with checks for a SQL NULL value on each
83  * <code>getXXX</code> method. If a column value obtained by a
84  * <code>getXXX</code> method is not SQL NULL, the column value is returned. If
85  * the column value is SQL null, an alternate value is returned. The alternate
86  * value defaults to the Java <code>null</code> value, which can be overridden
87  * for instances of the class.
88  *
89  * <p>
90  * Usage example:
91  * <blockquote>
92  * <pre>
93  * Connection conn = // somehow get a connection
94  * Statement stmt = conn.createStatement();
95  * ResultSet rs = stmt.executeQuery("SELECT col1, col2 FROM table1");
96  *
97  * // Wrap the result set for SQL NULL checking
98  * SqlNullCheckedResultSet wrapper = new SqlNullCheckedResultSet(rs);
99  * wrapper.setNullString("---N/A---"); // Set null string
100  * wrapper.setNullInt(-999); // Set null integer
101  * rs = ProxyFactory.instance().createResultSet(wrapper);
102  *
103  * while (rs.next()) {
104  * // If col1 is SQL NULL, value returned will be "---N/A---"
105  * String col1 = rs.getString("col1");
106  * // If col2 is SQL NULL, value returned will be -999
107  * int col2 = rs.getInt("col2");
108  * }
109  * rs.close();
110  * </pre>
111  * </blockquote>
112  * </p>
113  *
114  * @author <a HREF="stevencaswell@apache.org">Steven Caswell</a>
115  * @author David Graham
116  * @version $Id: SqlNullCheckedResultSet.java,v 1.3 2003/11/09 18:18:04 dgraham Exp $
117  */

118 public class SqlNullCheckedResultSet implements InvocationHandler JavaDoc {
119
120     /**
121      * Maps normal method names (ie. "getBigDecimal") to the corresponding null
122      * Method object (ie. getNullBigDecimal).
123      */

124     private static final Map JavaDoc nullMethods = new HashMap JavaDoc();
125
126     static {
127         Method JavaDoc[] methods = SqlNullCheckedResultSet.class.getMethods();
128         for (int i = 0; i < methods.length; i++) {
129             String JavaDoc methodName = methods[i].getName();
130
131             if (methodName.startsWith("getNull")) {
132                 String JavaDoc normalName = "get" + methodName.substring(7);
133                 nullMethods.put(normalName, methods[i]);
134             }
135         }
136     }
137
138     /**
139      * The factory to create proxies with.
140      */

141     private static final ProxyFactory factory = ProxyFactory.instance();
142
143     /**
144      * Wraps the <code>ResultSet</code> in an instance of this class. This is
145      * equivalent to:
146      * <pre>
147      * ProxyFactory.instance().createResultSet(new SqlNullCheckedResultSet(rs));
148      * </pre>
149      *
150      * @param rs The <code>ResultSet</code> to wrap.
151      */

152     public static ResultSet JavaDoc wrap(ResultSet JavaDoc rs) {
153         return factory.createResultSet(new SqlNullCheckedResultSet(rs));
154     }
155
156     private InputStream JavaDoc nullAsciiStream = null;
157     private BigDecimal JavaDoc nullBigDecimal = null;
158     private InputStream JavaDoc nullBinaryStream = null;
159     private Blob JavaDoc nullBlob = null;
160     private boolean nullBoolean = false;
161     private byte nullByte = 0;
162     private byte[] nullBytes = null;
163     private Reader JavaDoc nullCharacterStream = null;
164     private Clob JavaDoc nullClob = null;
165     private Date JavaDoc nullDate = null;
166     private double nullDouble = 0.0;
167     private float nullFloat = 0.0f;
168     private int nullInt = 0;
169     private long nullLong = 0;
170     private Object JavaDoc nullObject = null;
171     private Ref JavaDoc nullRef = null;
172     private short nullShort = 0;
173     private String JavaDoc nullString = null;
174     private Time JavaDoc nullTime = null;
175     private Timestamp JavaDoc nullTimestamp = null;
176     private URL JavaDoc nullURL = null;
177
178     /**
179      * The wrapped result.
180      */

181     private final ResultSet JavaDoc rs;
182
183     /**
184      * Constructs a new instance of
185      * <code>SqlNullCheckedResultSet</code>
186      * to wrap the specified <code>ResultSet</code>.
187      */

188     public SqlNullCheckedResultSet(ResultSet JavaDoc rs) {
189         super();
190         this.rs = rs;
191     }
192
193     /**
194      * Returns the value when a SQL null is encountered as the result of
195      * invoking a <code>getAsciiStream</code> method.
196      *
197      * @return the value
198      */

199     public InputStream JavaDoc getNullAsciiStream() {
200         return this.nullAsciiStream;
201     }
202
203     /**
204      * Returns the value when a SQL null is encountered as the result of
205      * invoking a <code>getBigDecimal</code> method.
206      *
207      * @return the value
208      */

209     public BigDecimal JavaDoc getNullBigDecimal() {
210         return this.nullBigDecimal;
211     }
212
213     /**
214      * Returns the value when a SQL null is encountered as the result of
215      * invoking a <code>getBinaryStream</code> method.
216      *
217      * @return the value
218      */

219     public InputStream JavaDoc getNullBinaryStream() {
220         return this.nullBinaryStream;
221     }
222
223     /**
224      * Returns the value when a SQL null is encountered as the result of
225      * invoking a <code>getBlob</code> method.
226      *
227      * @return the value
228      */

229     public Blob JavaDoc getNullBlob() {
230         return this.nullBlob;
231     }
232
233     /**
234      * Returns the value when a SQL null is encountered as the result of
235      * invoking a <code>getBoolean</code> method.
236      *
237      * @return the value
238      */

239     public boolean getNullBoolean() {
240         return this.nullBoolean;
241     }
242
243     /**
244      * Returns the value when a SQL null is encountered as the result of
245      * invoking a <code>getByte</code> method.
246      *
247      * @return the value
248      */

249     public byte getNullByte() {
250         return this.nullByte;
251     }
252
253     /**
254      * Returns the value when a SQL null is encountered as the result of
255      * invoking a <code>getBytes</code> method.
256      *
257      * @return the value
258      */

259     public byte[] getNullBytes() {
260         return this.nullBytes;
261     }
262
263     /**
264      * Returns the value when a SQL null is encountered as the result of
265      * invoking a <code>getCharacterStream</code> method.
266      *
267      * @return the value
268      */

269     public Reader JavaDoc getNullCharacterStream() {
270         return this.nullCharacterStream;
271     }
272
273     /**
274      * Returns the value when a SQL null is encountered as the result of
275      * invoking a <code>getClob</code> method.
276      *
277      * @return the value
278      */

279     public Clob JavaDoc getNullClob() {
280         return this.nullClob;
281     }
282
283     /**
284      * Returns the value when a SQL null is encountered as the result of
285      * invoking a <code>getDate</code> method.
286      *
287      * @return the value
288      */

289     public Date JavaDoc getNullDate() {
290         return this.nullDate;
291     }
292
293     /**
294      * Returns the value when a SQL null is encountered as the result of
295      * invoking a <code>getDouble</code> method.
296      *
297      * @return the value
298      */

299     public double getNullDouble() {
300         return this.nullDouble;
301     }
302
303     /**
304      * Returns the value when a SQL null is encountered as the result of
305      * invoking a <code>getFloat</code> method.
306      *
307      * @return the value
308      */

309     public float getNullFloat() {
310         return this.nullFloat;
311     }
312
313     /**
314      * Returns the value when a SQL null is encountered as the result of
315      * invoking a <code>getInt</code> method.
316      *
317      * @return the value
318      */

319     public int getNullInt() {
320         return this.nullInt;
321     }
322
323     /**
324      * Returns the value when a SQL null is encountered as the result of
325      * invoking a <code>getLong</code> method.
326      *
327      * @return the value
328      */

329     public long getNullLong() {
330         return this.nullLong;
331     }
332
333     /**
334      * Returns the value when a SQL null is encountered as the result of
335      * invoking a <code>getObject</code> method.
336      *
337      * @return the value
338      */

339     public Object JavaDoc getNullObject() {
340         return this.nullObject;
341     }
342
343     /**
344      * Returns the value when a SQL null is encountered as the result of
345      * invoking a <code>getRef</code> method.
346      *
347      * @return the value
348      */

349     public Ref JavaDoc getNullRef() {
350         return this.nullRef;
351     }
352
353     /**
354      * Returns the value when a SQL null is encountered as the result of
355      * invoking a <code>getShort</code> method.
356      *
357      * @return the value
358      */

359     public short getNullShort() {
360         return this.nullShort;
361     }
362
363     /**
364      * Returns the value when a SQL null is encountered as the result of
365      * invoking a <code>getString</code> method.
366      *
367      * @return the value
368      */

369     public String JavaDoc getNullString() {
370         return this.nullString;
371     }
372
373     /**
374      * Returns the value when a SQL null is encountered as the result of
375      * invoking a <code>getTime</code> method.
376      *
377      * @return the value
378      */

379     public Time JavaDoc getNullTime() {
380         return this.nullTime;
381     }
382
383     /**
384      * Returns the value when a SQL null is encountered as the result of
385      * invoking a <code>getTimestamp</code> method.
386      *
387      * @return the value
388      */

389     public Timestamp JavaDoc getNullTimestamp() {
390         return this.nullTimestamp;
391     }
392
393     /**
394      * Returns the value when a SQL null is encountered as the result of
395      * invoking a <code>getURL</code> method.
396      *
397      * @return the value
398      */

399     public URL JavaDoc getNullURL() {
400         return this.nullURL;
401     }
402
403     /**
404      * Intercepts calls to <code>get*</code> methods and calls the appropriate
405      * <code>getNull*</code> method if the <code>ResultSet</code> returned
406      * <code>null</code>.
407      *
408      * @throws Throwable
409      * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
410      */

411     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
412         throws Throwable JavaDoc {
413
414         Object JavaDoc result = method.invoke(this.rs, args);
415
416         Method JavaDoc nullMethod = (Method JavaDoc) nullMethods.get(method.getName());
417
418         // Check nullMethod != null first so that we don't call wasNull()
419
// before a true getter method was invoked on the ResultSet.
420
return (nullMethod != null && this.rs.wasNull())
421             ? nullMethod.invoke(this, null)
422             : result;
423     }
424
425     /**
426      * Sets the value to return when a SQL null is encountered as the result of
427      * invoking a <code>getAsciiStream</code> method.
428      *
429      * @param nullAsciiStream the value
430      */

431     public void setNullAsciiStream(InputStream JavaDoc nullAsciiStream) {
432         this.nullAsciiStream = nullAsciiStream;
433     }
434
435     /**
436      * Sets the value to return when a SQL null is encountered as the result of
437      * invoking a <code>getBigDecimal</code> method.
438      *
439      * @param nullBigDecimal the value
440      */

441     public void setNullBigDecimal(BigDecimal JavaDoc nullBigDecimal) {
442         this.nullBigDecimal = nullBigDecimal;
443     }
444
445     /**
446      * Sets the value to return when a SQL null is encountered as the result of
447      * invoking a <code>getBinaryStream</code> method.
448      *
449      * @param nullBinaryStream the value
450      */

451     public void setNullBinaryStream(InputStream JavaDoc nullBinaryStream) {
452         this.nullBinaryStream = nullBinaryStream;
453     }
454
455     /**
456      * Sets the value to return when a SQL null is encountered as the result of
457      * invoking a <code>getBlob</code> method.
458      *
459      * @param nullBlob the value
460      */

461     public void setNullBlob(Blob JavaDoc nullBlob) {
462         this.nullBlob = nullBlob;
463     }
464
465     /**
466      * Sets the value to return when a SQL null is encountered as the result of
467      * invoking a <code>getBoolean</code> method.
468      *
469      * @param nullBoolean the value
470      */

471     public void setNullBoolean(boolean nullBoolean) {
472         this.nullBoolean = nullBoolean;
473     }
474
475     /**
476      * Sets the value to return when a SQL null is encountered as the result of
477      * invoking a <code>getByte</code> method.
478      *
479      * @param nullByte the value
480      */

481     public void setNullByte(byte nullByte) {
482         this.nullByte = nullByte;
483     }
484
485     /**
486      * Sets the value to return when a SQL null is encountered as the result of
487      * invoking a <code>getBytes</code> method.
488      *
489      * @param nullBytes the value
490      */

491     public void setNullBytes(byte[] nullBytes) {
492         this.nullBytes = nullBytes;
493     }
494
495     /**
496      * Sets the value to return when a SQL null is encountered as the result of
497      * invoking a <code>getCharacterStream</code> method.
498      *
499      * @param nullCharacterStream the value
500      */

501     public void setNullCharacterStream(Reader JavaDoc nullCharacterStream) {
502         this.nullCharacterStream = nullCharacterStream;
503     }
504
505     /**
506      * Sets the value to return when a SQL null is encountered as the result of
507      * invoking a <code>getClob</code> method.
508      *
509      * @param nullClob the value
510      */

511     public void setNullClob(Clob JavaDoc nullClob) {
512         this.nullClob = nullClob;
513     }
514
515     /**
516      * Sets the value to return when a SQL null is encountered as the result of
517      * invoking a <code>getDate</code> method.
518      *
519      * @param nullDate the value
520      */

521     public void setNullDate(Date JavaDoc nullDate) {
522         this.nullDate = nullDate;
523     }
524
525     /**
526      * Sets the value to return when a SQL null is encountered as the result of
527      * invoking a <code>getDouble</code> method.
528      *
529      * @param nullDouble the value
530      */

531     public void setNullDouble(double nullDouble) {
532         this.nullDouble = nullDouble;
533     }
534
535     /**
536      * Sets the value to return when a SQL null is encountered as the result of
537      * invoking a <code>getFloat</code> method.
538      *
539      * @param nullFloat the value
540      */

541     public void setNullFloat(float nullFloat) {
542         this.nullFloat = nullFloat;
543     }
544
545     /**
546      * Sets the value to return when a SQL null is encountered as the result of
547      * invoking a <code>getInt</code> method.
548      *
549      * @param nullInt the value
550      */

551     public void setNullInt(int nullInt) {
552         this.nullInt = nullInt;
553     }
554
555     /**
556      * Sets the value to return when a SQL null is encountered as the result of
557      * invoking a <code>getLong</code> method.
558      *
559      * @param nullLong the value
560      */

561     public void setNullLong(long nullLong) {
562         this.nullLong = nullLong;
563     }
564
565     /**
566      * Sets the value to return when a SQL null is encountered as the result of
567      * invoking a <code>getObject</code> method.
568      *
569      * @param nullObject the value
570      */

571     public void setNullObject(Object JavaDoc nullObject) {
572         this.nullObject = nullObject;
573     }
574
575     /**
576      * Sets the value to return when a SQL null is encountered as the result of
577      * invoking a <code>getRef</code> method.
578      *
579      * @param nullRef the value
580      */

581     public void setNullRef(Ref JavaDoc nullRef) {
582         this.nullRef = nullRef;
583     }
584
585     /**
586      * Sets the value to return when a SQL null is encountered as the result of
587      * invoking a <code>getShort</code> method.
588      *
589      * @param nullShort the value
590      */

591     public void setNullShort(short nullShort) {
592         this.nullShort = nullShort;
593     }
594
595     /**
596      * Sets the value to return when a SQL null is encountered as the result of
597      * invoking a <code>getString</code> method.
598      *
599      * @param nullString the value
600      */

601     public void setNullString(String JavaDoc nullString) {
602         this.nullString = nullString;
603     }
604
605     /**
606      * Sets the value to return when a SQL null is encountered as the result of
607      * invoking a <code>getTime</code> method.
608      *
609      * @param nullTime the value
610      */

611     public void setNullTime(Time JavaDoc nullTime) {
612         this.nullTime = nullTime;
613     }
614
615     /**
616      * Sets the value to return when a SQL null is encountered as the result of
617      * invoking a <code>getTimestamp</code> method.
618      *
619      * @param nullTimestamp the value
620      */

621     public void setNullTimestamp(Timestamp JavaDoc nullTimestamp) {
622         this.nullTimestamp = nullTimestamp;
623     }
624
625     /**
626      * Sets the value to return when a SQL null is encountered as the result of
627      * invoking a <code>getURL</code> method.
628      *
629      * @param nullURL the value
630      */

631     public void setNullURL(URL JavaDoc nullURL) {
632         this.nullURL = nullURL;
633     }
634
635 }
636
Popular Tags