View Javadoc

1   /*
2    * The Working-Dogs.com License, Version 1.1
3    *
4    * Copyright (c) 1999 Working-Dogs.com.  All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met:
9    *
10   * 1. Redistributions of source code must retain the above copyright
11   *    notice, this list of conditions and the following disclaimer.
12   *
13   * 2. Redistributions in binary form must reproduce the above copyright
14   *    notice, this list of conditions and the following disclaimer in
15   *    the documentation and/or other materials provided with the
16   *    distribution.
17   *
18   * 3. The end-user documentation included with the redistribution, if
19   *    any, must include the following acknowlegement:
20   *       "This product includes software developed by the
21   *        Working-Dogs.com <http://www.Working-Dogs.com/>."
22   *    Alternately, this acknowlegement may appear in the software itself,
23   *    if and wherever such third-party acknowlegements normally appear.
24   *
25   * 4. The names "Working-Dogs.com" and "Village" must not be used to
26   *    endorse or promote products derived from this software without
27   *    prior written permission. For written permission, please contact
28   *    jon@working-dogs.com.
29   *
30   * 5. Products derived from this software may not be called
31   *    "Working-Dogs.com" nor may "Village" appear in their names
32   *    without prior written permission of Working-Dogs.com.
33   *
34   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
38   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
41   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
42   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
44   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45   * SUCH DAMAGE.
46   * ====================================================================
47   *
48   * This software consists of voluntary contributions made by many
49   * individuals on behalf of the Working-Dogs.com.  For more
50   * information on the Working-Dogs.com, please see
51   * <http://www.Working-Dogs.com/>.
52   */
53  /*
54   * The Working-Dogs.com License, Version 1.1
55   *
56   * Copyright (c) 1999 Working-Dogs.com.  All rights reserved.
57   *
58   * Redistribution and use in source and binary forms, with or without
59   * modification, are permitted provided that the following conditions
60   * are met:
61   *
62   * 1. Redistributions of source code must retain the above copyright
63   *    notice, this list of conditions and the following disclaimer.
64   *
65   * 2. Redistributions in binary form must reproduce the above copyright
66   *    notice, this list of conditions and the following disclaimer in
67   *    the documentation and/or other materials provided with the
68   *    distribution.
69   *
70   * 3. The end-user documentation included with the redistribution, if
71   *    any, must include the following acknowlegement:
72   *       "This product includes software developed by the
73   *        Working-Dogs.com <http://www.Working-Dogs.com/>."
74   *    Alternately, this acknowlegement may appear in the software itself,
75   *    if and wherever such third-party acknowlegements normally appear.
76   *
77   * 4. The names "Working-Dogs.com" and "Village" must not be used to
78   *    endorse or promote products derived from this software without
79   *    prior written permission. For written permission, please contact
80   *    jon@working-dogs.com.
81   *
82   * 5. Products derived from this software may not be called
83   *    "Working-Dogs.com" nor may "Village" appear in their names
84   *    without prior written permission of Working-Dogs.com.
85   *
86   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
87   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
88   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
89   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
90   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
91   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
92   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
93   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
94   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
95   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
96   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
97   * SUCH DAMAGE.
98   * ====================================================================
99   *
100  * This software consists of voluntary contributions made by many
101  * individuals on behalf of the Working-Dogs.com.  For more
102  * information on the Working-Dogs.com, please see
103  * <http://www.Working-Dogs.com/>.
104  */
105 package com.workingdogs.village;
106 
107 import java.math.BigDecimal;
108 
109 import java.sql.Blob;
110 import java.sql.PreparedStatement;
111 import java.sql.ResultSet;
112 import java.sql.SQLException;
113 import java.sql.Time;
114 import java.sql.Timestamp;
115 import java.sql.Types;
116 
117 import java.util.Calendar;
118 
119 /***
120  * A Value represents a single cell in a database table. In other words, it is the cross between a row and column and contains the
121  * information held there.
122  *
123  * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
124  * @version $Revision: 568 $
125  */
126 public class Value
127 {
128     /*** the object that is stored in this object */
129     private Object valueObject;
130 
131     /*** the column number that this object came from */
132     private int columnNumber;
133 
134     /*** what sql type of object is this? */
135     private int type;
136 
137     /***
138      * Creates a new Value object based on the ResultSet, columnNumber and type
139      *
140      * @param rs
141      * @param columnNumber
142      * @param type
143      *
144      * @exception SQLException
145      */
146     public Value(ResultSet rs, int columnNumber, int type)
147             throws SQLException
148     {
149         this.columnNumber = columnNumber;
150         this.type = type;
151         this.valueObject = null;
152 
153         if (rs == null)
154         {
155             return;
156         }
157 
158         switch (type())
159         {
160         case Types.BIT:
161 
162             String tmp = rs.getString(columnNumber);
163 
164             if (tmp == null)
165             {
166                 valueObject = Boolean.FALSE;
167             }
168             else if (isTrue(tmp))
169             {
170                 valueObject = Boolean.TRUE;
171             }
172             else
173             {
174                 valueObject = Boolean.FALSE;
175             }
176 
177             break;
178 
179         case Types.TINYINT:
180             valueObject = new Byte(rs.getByte(columnNumber));
181 
182             break;
183 
184         case Types.BIGINT:
185             valueObject = new Long(rs.getLong(columnNumber));
186 
187             break;
188 
189         case Types.SMALLINT:
190             valueObject = new Short(rs.getShort(columnNumber));
191 
192             break;
193 
194         case Types.INTEGER:
195             valueObject = new Integer(rs.getInt(columnNumber));
196 
197             break;
198 
199         case Types.REAL:
200             valueObject = new Float(rs.getFloat(columnNumber));
201 
202             break;
203 
204         case Types.FLOAT:
205         case Types.DOUBLE:
206             valueObject = new Double(rs.getDouble(columnNumber));
207 
208             break;
209 
210         case Types.NUMERIC:
211         case Types.DECIMAL:
212 
213             String number = rs.getString(columnNumber);
214 
215             if (number == null)
216             {
217                 valueObject = null;
218             }
219             else
220             {
221                 valueObject = new BigDecimal(number);
222             }
223 
224             break;
225 
226         case Types.LONGVARBINARY:
227         case Types.VARBINARY:
228         case Types.BINARY:
229             valueObject = rs.getBytes(columnNumber);
230 
231             break;
232 
233         case Types.BLOB:
234 
235             Blob blob = rs.getBlob(columnNumber);
236             valueObject = blob.getBytes(1, (int) blob.length());
237 
238             break;
239 
240         case Types.LONGVARCHAR:
241         case Types.CHAR:
242         case Types.VARCHAR:
243         case Types.OTHER:
244             valueObject = rs.getString(columnNumber);
245 
246             break;
247 
248         case Types.DATE:
249             valueObject = rs.getDate(columnNumber);
250 
251             break;
252 
253         case Types.TIME:
254             valueObject = rs.getTime(columnNumber);
255 
256             break;
257 
258         case Types.TIMESTAMP:
259             valueObject = rs.getTimestamp(columnNumber);
260 
261             break;
262 
263         case Types.NULL:
264             valueObject = null;
265 
266             break;
267 
268         default:
269             valueObject = rs.getString(columnNumber);
270 
271             break;
272         }
273 
274         if (rs.wasNull())
275         {
276             valueObject = null;
277         }
278 
279         return;
280     }
281 
282     /***
283      * Sets the value of this object
284      *
285      * @param value
286      */
287     void setValue(Object value)
288     {
289         this.valueObject = value;
290     }
291 
292     /***
293      * Gets the object from this Value
294      *
295      * @return the object from this Value
296      */
297     Object getValue()
298     {
299         return this.valueObject;
300     }
301 
302     /***
303      * This is used in Record in order to do a saveWithInsert/Update/Delete
304      *
305      * @param stmt
306      * @param stmtNumber
307      *
308      * @exception DataSetException
309      * @exception SQLException
310      */
311     void setPreparedStatementValue(PreparedStatement stmt, int stmtNumber)
312             throws DataSetException, SQLException
313     {
314         if (isNull())
315         {
316             stmt.setNull(stmtNumber, type());
317 
318             return;
319         }
320 
321         switch (type())
322         {
323         case Types.BIT:
324             stmt.setBoolean(stmtNumber, this.asBoolean());
325 
326             break;
327 
328         case Types.TINYINT:
329             stmt.setByte(stmtNumber, this.asByte());
330 
331             break;
332 
333         case Types.BIGINT:
334             stmt.setLong(stmtNumber, this.asLong());
335 
336             break;
337 
338         case Types.SMALLINT:
339             stmt.setShort(stmtNumber, this.asShort());
340 
341             break;
342 
343         case Types.INTEGER:
344             stmt.setInt(stmtNumber, this.asInt());
345 
346             break;
347 
348         case Types.REAL:
349             stmt.setFloat(stmtNumber, this.asFloat());
350 
351             break;
352 
353         case Types.FLOAT:
354         case Types.DOUBLE:
355             stmt.setDouble(stmtNumber, this.asDouble());
356 
357             break;
358 
359         case Types.NUMERIC:
360         case Types.DECIMAL:
361             stmt.setBigDecimal(stmtNumber, this.asBigDecimal());
362 
363             break;
364 
365         case Types.LONGVARBINARY:
366         case Types.VARBINARY:
367         case Types.BINARY:
368         case Types.BLOB:
369 
370             // The following form is reported to work and be necessary for
371             // Oracle when the blob exceeds 4k.
372             byte [] value = this.asBytes();
373             stmt.setBinaryStream(stmtNumber, new java.io.ByteArrayInputStream(value), value.length);
374 
375             break;
376 
377         case Types.LONGVARCHAR:
378         case Types.CHAR:
379         case Types.VARCHAR:
380         case Types.OTHER:
381             stmt.setString(stmtNumber, this.asString());
382 
383             break;
384 
385         case Types.DATE:
386             stmt.setDate(stmtNumber, this.asDate());
387 
388             break;
389 
390         case Types.TIME:
391             stmt.setTime(stmtNumber, this.asTime());
392 
393             break;
394 
395         case Types.TIMESTAMP:
396             stmt.setTimestamp(stmtNumber, this.asTimestamp());
397 
398             break;
399 
400         case Types.NULL:
401             stmt.setNull(stmtNumber, 0);
402 
403             break;
404 
405         default:
406             stmt.setString(stmtNumber, this.asString());
407 
408             break;
409         }
410     }
411 
412     /***
413      * Returns the string representation of this object
414      *
415      * @return a string
416      */
417     public String toString()
418     {
419         return this.asString();
420     }
421 
422     /***
423      * Returns the string representation of this object
424      *
425      * @return a string
426      */
427     public String asString()
428     {
429         if (isNull())
430         {
431             return null;
432         }
433         else if (isString())
434         {
435             return (String) valueObject;
436         }
437         else if (isBytes())
438         {
439             return new String((byte []) valueObject);
440         }
441         else
442         {
443             return valueObject.toString();
444         }
445     }
446 
447     /***
448      * Get the value as a BigDecimal
449      *
450      * @return a BigDecimal
451      *
452      * @exception DataSetException
453      */
454     public BigDecimal asBigDecimal()
455             throws DataSetException
456     {
457         try
458         {
459             if (isNull())
460             {
461                 return null;
462             }
463             else if (isBigDecimal())
464             {
465                 return (BigDecimal) valueObject;
466             }
467             else if (isString() || isDouble() || isFloat() || isInt() || isLong() || isShort() || isByte())
468             {
469                 return new BigDecimal(asString());
470             }
471             else
472             {
473                 return null;
474             }
475         }
476         catch (Exception e)
477         {
478             throw new DataSetException("Illegal conversion: " + e.toString());
479         }
480     }
481 
482     /***
483      * Get the value as a BigDecimal
484      *
485      * @param scale TODO: DOCUMENT ME!
486      *
487      * @return a BigDecimal
488      *
489      * @exception DataSetException
490      */
491     public BigDecimal asBigDecimal(int scale)
492             throws DataSetException
493     {
494         try
495         {
496             if (isNull())
497             {
498                 return null;
499             }
500             else if (isBigDecimal())
501             {
502                 return ((BigDecimal) valueObject).setScale(scale);
503             }
504             else if (isString() || isDouble() || isFloat() || isInt() || isLong() || isShort() || isByte())
505             {
506                 return new BigDecimal(asString()).setScale(scale);
507             }
508             else
509             {
510                 return null;
511             }
512         }
513         catch (Exception e)
514         {
515             throw new DataSetException("Bad conversion: " + e.toString());
516         }
517     }
518 
519     /***
520      * Get the value as a asBoolean
521      *
522      * @return a boolean
523      *
524      * @exception DataSetException
525      */
526     public boolean asBoolean()
527             throws DataSetException
528     {
529         try
530         {
531             if (isNull())
532             {
533                 return false;
534             }
535             else if (isBoolean())
536             {
537                 return ((Boolean) valueObject).booleanValue();
538             }
539 
540             String check = asString();
541 
542             return (check == null) ? false : isTrue(check);
543         }
544         catch (Exception e)
545         {
546             throw new DataSetException("Bad conversion: " + e.toString());
547         }
548     }
549 
550     /***
551      * Get the value as a Boolean object
552      *
553      * @return a Boolean
554      *
555      * @exception DataSetException
556      */
557     public Boolean asBooleanObj()
558             throws DataSetException
559     {
560         try
561         {
562             if (isNull())
563             {
564                 return null;
565             }
566             else if (isBoolean())
567             {
568                 return (Boolean) valueObject;
569             }
570 
571             String check = asString();
572 
573             if (check == null)
574             {
575                 return null;
576             }
577             else if (isTrue(check))
578             {
579                 return Boolean.TRUE;
580             }
581             else
582             {
583                 return Boolean.FALSE;
584             }
585         }
586         catch (Exception e)
587         {
588             throw new DataSetException("Bad conversion: " + e.toString());
589         }
590     }
591 
592     /***
593      * Get the value as a asInt
594      *
595      * @return an int
596      *
597      * @exception DataSetException
598      */
599     public int asInt()
600             throws DataSetException
601     {
602         try
603         {
604             if (isNull())
605             {
606                 return 0;
607             }
608             else if (isInt())
609             {
610                 return ((Integer) valueObject).intValue();
611             }
612             else if (isString())
613             {
614                 return Integer.valueOf((String) valueObject).intValue();
615             }
616             else if (isLong())
617             {
618                 return ((Long) valueObject).intValue();
619             }
620             else if (isDouble())
621             {
622                 return ((Double) valueObject).intValue();
623             }
624             else if (isFloat())
625             {
626                 return ((Float) valueObject).intValue();
627             }
628             else if (isBigDecimal())
629             {
630                 return ((BigDecimal) valueObject).intValue();
631             }
632             else
633             {
634                 return Integer.valueOf(asString()).intValue();
635             }
636         }
637         catch (Exception e)
638         {
639             throw new DataSetException("Bad conversion: " + e.toString());
640         }
641     }
642 
643     /***
644      * Get the value as a Integer Ojbect
645      *
646      * @return an Integer
647      *
648      * @exception DataSetException
649      */
650     public Integer asIntegerObj()
651             throws DataSetException
652     {
653         try
654         {
655             if (isNull())
656             {
657                 return null;
658             }
659             else if (isInt())
660             {
661                 return ((Integer) valueObject);
662             }
663             else if (isString() || isDouble() || isFloat() || isBigDecimal() || isLong() || isShort() || isByte())
664             {
665                 return new Integer(asString());
666             }
667             else
668             {
669                 throw new DataSetException("Invalid type for Integer");
670             }
671         }
672         catch (Exception e)
673         {
674             throw new DataSetException("Illegal conversion: " + e.toString());
675         }
676     }
677 
678     /***
679      * Get the value as a asByte
680      *
681      * @return a byte
682      *
683      * @exception DataSetException
684      */
685     public byte asByte()
686             throws DataSetException
687     {
688         try
689         {
690             if (isNull())
691             {
692                 return 0;
693             }
694             else if (isByte())
695             {
696                 return ((Byte) valueObject).byteValue();
697             }
698             else if (isString())
699             {
700                 return Integer.valueOf((String) valueObject).byteValue();
701             }
702             else if (isShort())
703             {
704                 return ((Short) valueObject).byteValue();
705             }
706             else if (isInt())
707             {
708                 return ((Integer) valueObject).byteValue();
709             }
710             else if (isLong())
711             {
712                 return ((Long) valueObject).byteValue();
713             }
714             else if (isDouble())
715             {
716                 return ((Double) valueObject).byteValue();
717             }
718             else if (isFloat())
719             {
720                 return ((Float) valueObject).byteValue();
721             }
722             else if (isBigDecimal())
723             {
724                 return ((BigDecimal) valueObject).byteValue();
725             }
726             else
727             {
728                 return Integer.valueOf(asString()).byteValue();
729             }
730         }
731         catch (Exception e)
732         {
733             throw new DataSetException("Bad conversion: " + e.toString());
734         }
735     }
736 
737     /***
738      * Get the value as a Byte Object
739      *
740      * @return a Byte
741      *
742      * @exception DataSetException
743      */
744     public Byte asByteObj()
745             throws DataSetException
746     {
747         try
748         {
749             if (isNull())
750             {
751                 return null;
752             }
753             else if (isByte())
754             {
755                 return ((Byte) valueObject);
756             }
757             else if (isString() || isDouble() || isFloat() || isInt() || isLong() || isShort() || isBigDecimal())
758             {
759                 return new Byte(asString());
760             }
761             else
762             {
763                 throw new DataSetException("Invalid type for Byte");
764             }
765         }
766         catch (Exception e)
767         {
768             throw new DataSetException("Illegal conversion: " + e.toString());
769         }
770     }
771 
772     /***
773      * Get the value as a asBytes
774      *
775      * @return a byte array
776      *
777      * @exception DataSetException
778      */
779     public byte [] asBytes()
780             throws DataSetException
781     {
782         try
783         {
784             if (isNull())
785             {
786                 return new byte[0];
787             }
788             else if (isBytes())
789             {
790                 return (byte []) valueObject;
791             }
792             else if (isString())
793             {
794                 return ((String) valueObject).getBytes();
795             }
796         }
797         catch (Exception e)
798         {
799             throw new DataSetException("Bad conversion: " + e.toString());
800         }
801 
802         return new byte[0];
803     }
804 
805     /***
806      * Get the value as a asShort
807      *
808      * @return a short
809      *
810      * @exception DataSetException
811      */
812     public short asShort()
813             throws DataSetException
814     {
815         try
816         {
817             if (isNull())
818             {
819                 return 0;
820             }
821             else if (isShort())
822             {
823                 return ((Short) valueObject).shortValue();
824             }
825             else if (isString())
826             {
827                 return Integer.valueOf((String) valueObject).shortValue();
828             }
829             else if (isInt())
830             {
831                 return ((Integer) valueObject).shortValue();
832             }
833             else if (isLong())
834             {
835                 return ((Long) valueObject).shortValue();
836             }
837             else if (isDouble())
838             {
839                 return ((Double) valueObject).shortValue();
840             }
841             else if (isFloat())
842             {
843                 return ((Float) valueObject).shortValue();
844             }
845             else if (isBigDecimal())
846             {
847                 return ((BigDecimal) valueObject).shortValue();
848             }
849             else
850             {
851                 return Integer.valueOf(asString()).shortValue();
852             }
853         }
854         catch (Exception e)
855         {
856             throw new DataSetException("Bad conversion: " + e.toString());
857         }
858     }
859 
860     /***
861      * Get the value as a Short Object
862      *
863      * @return a Short
864      *
865      * @exception DataSetException
866      */
867     public Short asShortObj()
868             throws DataSetException
869     {
870         try
871         {
872             if (isNull())
873             {
874                 return null;
875             }
876             else if (isShort())
877             {
878                 return ((Short) valueObject);
879             }
880             else if (isString() || isDouble() || isFloat() || isInt() || isLong() || isBigDecimal() || isByte())
881             {
882                 return new Short(asString());
883             }
884             else
885             {
886                 throw new DataSetException("Invalid type for Short");
887             }
888         }
889         catch (Exception e)
890         {
891             throw new DataSetException("Illegal conversion: " + e.toString());
892         }
893     }
894 
895     /***
896      * Get the value as a asLong
897      *
898      * @return a long
899      *
900      * @exception DataSetException
901      */
902     public long asLong()
903             throws DataSetException
904     {
905         try
906         {
907             if (isNull())
908             {
909                 return 0;
910             }
911             else if (isLong())
912             {
913                 return ((Long) valueObject).longValue();
914             }
915             else if (isString())
916             {
917                 return Integer.valueOf((String) valueObject).longValue();
918             }
919             else if (isShort())
920             {
921                 return ((Short) valueObject).longValue();
922             }
923             else if (isInt())
924             {
925                 return ((Integer) valueObject).longValue();
926             }
927             else if (isDouble())
928             {
929                 return ((Double) valueObject).longValue();
930             }
931             else if (isFloat())
932             {
933                 return ((Float) valueObject).longValue();
934             }
935             else if (isBigDecimal())
936             {
937                 return ((BigDecimal) valueObject).longValue();
938             }
939             else
940             {
941                 return Integer.valueOf(asString()).longValue();
942             }
943         }
944         catch (Exception e)
945         {
946             throw new DataSetException("Bad conversion: " + e.toString());
947         }
948     }
949 
950     /***
951      * Get the value as a Long Object
952      *
953      * @return a Long
954      *
955      * @exception DataSetException
956      */
957     public Long asLongObj()
958             throws DataSetException
959     {
960         try
961         {
962             if (isNull())
963             {
964                 return null;
965             }
966             else if (isLong())
967             {
968                 return ((Long) valueObject);
969             }
970             else if (isString() || isDouble() || isFloat() || isInt() || isBigDecimal() || isShort() || isByte())
971             {
972                 return new Long(asString());
973             }
974             else
975             {
976                 throw new DataSetException("Invalid type for Long");
977             }
978         }
979         catch (Exception e)
980         {
981             throw new DataSetException("Illegal conversion: " + e.toString());
982         }
983     }
984 
985     /***
986      * Get the value as a asDouble
987      *
988      * @return a double
989      *
990      * @exception DataSetException
991      */
992     public double asDouble()
993             throws DataSetException
994     {
995         try
996         {
997             if (isNull())
998             {
999                 return 0.0D;
1000             }
1001             else if (isDouble())
1002             {
1003                 return ((Double) valueObject).doubleValue();
1004             }
1005             else if (isString())
1006             {
1007                 return Integer.valueOf((String) valueObject).doubleValue();
1008             }
1009             else if (isShort())
1010             {
1011                 return ((Short) valueObject).doubleValue();
1012             }
1013             else if (isInt())
1014             {
1015                 return ((Integer) valueObject).doubleValue();
1016             }
1017             else if (isLong())
1018             {
1019                 return ((Long) valueObject).doubleValue();
1020             }
1021             else if (isFloat())
1022             {
1023                 return ((Float) valueObject).doubleValue();
1024             }
1025             else if (isBigDecimal())
1026             {
1027                 return ((BigDecimal) valueObject).doubleValue();
1028             }
1029             else
1030             {
1031                 return Integer.valueOf(asString()).doubleValue();
1032             }
1033         }
1034         catch (Exception e)
1035         {
1036             throw new DataSetException("Bad conversion: " + e.toString());
1037         }
1038     }
1039 
1040     /***
1041      * Get the value as a Double Object
1042      *
1043      * @return a Double
1044      *
1045      * @exception DataSetException
1046      */
1047     public Double asDoubleObj()
1048             throws DataSetException
1049     {
1050         try
1051         {
1052             if (isNull())
1053             {
1054                 return null;
1055             }
1056             else if (isDouble())
1057             {
1058                 return ((Double) valueObject);
1059             }
1060             else if (isString() || isBigDecimal() || isFloat() || isInt() || isLong() || isShort() || isByte())
1061             {
1062                 return new Double(asString());
1063             }
1064             else
1065             {
1066                 throw new DataSetException("Invalid type for Double");
1067             }
1068         }
1069         catch (Exception e)
1070         {
1071             throw new DataSetException("Illegal conversion: " + e.toString());
1072         }
1073     }
1074 
1075     /***
1076      * Get the value as a asFloat
1077      *
1078      * @return a float
1079      *
1080      * @exception DataSetException
1081      */
1082     public float asFloat()
1083             throws DataSetException
1084     {
1085         try
1086         {
1087             if (isNull())
1088             {
1089                 return 0.0F;
1090             }
1091             else if (isFloat())
1092             {
1093                 return ((Float) valueObject).floatValue();
1094             }
1095             else if (isString())
1096             {
1097                 return Integer.valueOf((String) valueObject).floatValue();
1098             }
1099             else if (isShort())
1100             {
1101                 return ((Short) valueObject).floatValue();
1102             }
1103             else if (isInt())
1104             {
1105                 return ((Integer) valueObject).floatValue();
1106             }
1107             else if (isLong())
1108             {
1109                 return ((Long) valueObject).floatValue();
1110             }
1111             else if (isDouble())
1112             {
1113                 return ((Double) valueObject).floatValue();
1114             }
1115             else if (isBigDecimal())
1116             {
1117                 return ((BigDecimal) valueObject).floatValue();
1118             }
1119             else
1120             {
1121                 return Integer.valueOf(asString()).floatValue();
1122             }
1123         }
1124         catch (Exception e)
1125         {
1126             throw new DataSetException("Bad conversion: " + e.toString());
1127         }
1128     }
1129 
1130     /***
1131      * Get the value as a Float Obj
1132      *
1133      * @return a Float
1134      *
1135      * @exception DataSetException
1136      */
1137     public Float asFloatObj()
1138             throws DataSetException
1139     {
1140         try
1141         {
1142             if (isNull())
1143             {
1144                 return null;
1145             }
1146             else if (isFloat())
1147             {
1148                 return ((Float) valueObject);
1149             }
1150             else if (isString() || isDouble() || isBigDecimal() || isInt() || isLong() || isShort() || isByte())
1151             {
1152                 return new Float(asString());
1153             }
1154             else
1155             {
1156                 throw new DataSetException("Invalid type for Float");
1157             }
1158         }
1159         catch (Exception e)
1160         {
1161             throw new DataSetException("Illegal conversion: " + e.toString());
1162         }
1163     }
1164 
1165     /***
1166      * Get the value as a asTime
1167      *
1168      * @return a Time
1169      *
1170      * @exception DataSetException
1171      */
1172     public Time asTime()
1173             throws DataSetException
1174     {
1175         try
1176         {
1177             if (isNull())
1178             {
1179                 return null;
1180             }
1181             else if (isTime())
1182             {
1183                 return (Time) valueObject;
1184             }
1185 
1186             Calendar cal = Calendar.getInstance();
1187 
1188             if (isTimestamp())
1189             {
1190                 cal.setTime((Timestamp) valueObject);
1191 
1192                 return new Time(cal.getTime().getTime());
1193             }
1194             else if (isUtilDate())
1195             {
1196                 cal.setTime((java.util.Date) valueObject);
1197 
1198                 return new Time(cal.getTime().getTime());
1199             }
1200             else if (isString())
1201             {
1202                 return Time.valueOf((String) valueObject);
1203             }
1204             else
1205             {
1206                 return Time.valueOf(asString());
1207             }
1208         }
1209         catch (IllegalArgumentException a)
1210         {
1211             throw new DataSetException("Bad date value - Java Time Objects cannot be earlier than 1/1/70");
1212         }
1213         catch (Exception b)
1214         {
1215             throw new DataSetException("Bad conversion: " + b.toString());
1216         }
1217     }
1218 
1219     /***
1220      * Get the value as a asTimestamp
1221      *
1222      * @return a Timestamp
1223      *
1224      * @exception DataSetException
1225      */
1226     public Timestamp asTimestamp()
1227             throws DataSetException
1228     {
1229         try
1230         {
1231             if (isNull())
1232             {
1233                 return null;
1234             }
1235             else if (isTimestamp())
1236             {
1237                 return (Timestamp) valueObject;
1238             }
1239 
1240             if (isTime())
1241             {
1242                 Calendar cal = Calendar.getInstance();
1243                 cal.setTime((Time) valueObject);
1244 
1245                 return new Timestamp(cal.getTime().getTime());
1246             }
1247             else if (isUtilDate())
1248             {
1249                 return new Timestamp(((java.util.Date) valueObject).getTime());
1250             }
1251             else if (isString())
1252             {
1253                 return Timestamp.valueOf((String) valueObject);
1254             }
1255             else
1256             {
1257                 return Timestamp.valueOf(asString());
1258             }
1259         }
1260         catch (IllegalArgumentException a)
1261         {
1262             throw new DataSetException("Bad date value - Java Timestamp Objects cannot be earlier than 1/1/70");
1263         }
1264         catch (Exception b)
1265         {
1266             throw new DataSetException("Bad conversion: " + b.toString());
1267         }
1268     }
1269 
1270     /***
1271      * Get the value as a asDate
1272      *
1273      * @return a java.sql.Date
1274      *
1275      * @exception DataSetException
1276      */
1277     public java.sql.Date asDate()
1278             throws DataSetException
1279     {
1280         try
1281         {
1282             if (isNull())
1283             {
1284                 return null;
1285             }
1286             else if (isDate())
1287             {
1288                 return (java.sql.Date) valueObject;
1289             }
1290 
1291             Calendar cal = Calendar.getInstance();
1292 
1293             if (isTimestamp())
1294             {
1295                 Timestamp ts = (Timestamp) valueObject;
1296                 long date = ts.getTime();
1297                 int nanos = ts.getNanos();
1298 
1299                 return new java.sql.Date(date + (nanos / 1000000));
1300             }
1301             else if (isTime())
1302             {
1303                 cal.setTime((Time) valueObject);
1304 
1305                 return java.sql.Date.valueOf(cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
1306                     + cal.get(Calendar.DAY_OF_MONTH));
1307             }
1308             else if (isUtilDate())
1309             {
1310                 cal.setTime((java.util.Date) valueObject);
1311 
1312                 return java.sql.Date.valueOf(cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
1313                     + cal.get(Calendar.DAY_OF_MONTH));
1314             }
1315             else if (isString())
1316             {
1317                 return java.sql.Date.valueOf((String) valueObject);
1318             }
1319             else
1320             {
1321                 return java.sql.Date.valueOf(asString());
1322             }
1323         }
1324         catch (IllegalArgumentException a)
1325         {
1326             throw new DataSetException("Bad date value - Java Timestamp Objects cannot be earlier than 1/1/70");
1327         }
1328         catch (Exception b)
1329         {
1330             throw new DataSetException("Bad conversion: " + b.toString());
1331         }
1332     }
1333 
1334     /***
1335      * Get the value as a asUtilDate
1336      *
1337      * @return a java.util.Date
1338      *
1339      * @exception DataSetException
1340      */
1341     public java.util.Date asUtilDate()
1342             throws DataSetException
1343     {
1344         try
1345         {
1346             if (isNull())
1347             {
1348                 return null;
1349             }
1350             else if (isUtilDate())
1351             {
1352                 return (java.util.Date) valueObject;
1353             }
1354 
1355             Calendar cal = Calendar.getInstance();
1356 
1357             if (isTimestamp())
1358             {
1359                 Timestamp ts = (Timestamp) valueObject;
1360                 long date = ts.getTime();
1361                 int nanos = ts.getNanos();
1362 
1363                 return new java.util.Date(date + (nanos / 1000000));
1364             }
1365             else if (isTime())
1366             {
1367                 cal.setTime((Time) valueObject);
1368 
1369                 return java.sql.Date.valueOf(cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
1370                     + cal.get(Calendar.DAY_OF_MONTH));
1371             }
1372             else if (isUtilDate())
1373             {
1374                 cal.setTime((java.util.Date) valueObject);
1375 
1376                 return java.sql.Date.valueOf(cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
1377                     + cal.get(Calendar.DAY_OF_MONTH));
1378             }
1379             else
1380             {
1381                 return null;
1382             }
1383         }
1384         catch (IllegalArgumentException a)
1385         {
1386             throw new DataSetException("Bad date value - Java java.util.Date Objects cannot be earlier than 1/1/70");
1387         }
1388         catch (Exception b)
1389         {
1390             throw new DataSetException("Bad conversion: " + b.toString());
1391         }
1392     }
1393 
1394     /***
1395      * Is the value a isBigDecimal
1396      *
1397      * @return true if BigDecimal
1398      */
1399     public boolean isBigDecimal()
1400     {
1401         return valueObject instanceof BigDecimal;
1402     }
1403 
1404     /***
1405      * Is the value a isByte
1406      *
1407      * @return true if is Byte
1408      */
1409     public boolean isByte()
1410     {
1411         return valueObject instanceof Byte;
1412     }
1413 
1414     /***
1415      * Is the value a isBytes
1416      *
1417      * @return true if is byte[]
1418      */
1419     public boolean isBytes()
1420     {
1421         return valueObject instanceof byte [];
1422     }
1423 
1424     /***
1425      * Is the value a isDate
1426      *
1427      * @return true if is java.sql.Date
1428      */
1429     public boolean isDate()
1430     {
1431         return valueObject instanceof java.sql.Date;
1432     }
1433 
1434     /***
1435      * Is the value a isShort
1436      *
1437      * @return true if is Short
1438      */
1439     public boolean isShort()
1440     {
1441         return valueObject instanceof Short;
1442     }
1443 
1444     /***
1445      * Is the value a isInt
1446      *
1447      * @return true if is Integer
1448      */
1449     public boolean isInt()
1450     {
1451         return valueObject instanceof Integer;
1452     }
1453 
1454     /***
1455      * Is the value a isLong
1456      *
1457      * @return true if is Long
1458      */
1459     public boolean isLong()
1460     {
1461         return valueObject instanceof Long;
1462     }
1463 
1464     /***
1465      * Is the value a isDouble
1466      *
1467      * @return true if is Double
1468      */
1469     public boolean isDouble()
1470     {
1471         return valueObject instanceof Double;
1472     }
1473 
1474     /***
1475      * Is the value a isFloat
1476      *
1477      * @return true if is Float
1478      */
1479     public boolean isFloat()
1480     {
1481         return valueObject instanceof Float;
1482     }
1483 
1484     /***
1485      * Is the value a isBoolean
1486      *
1487      * @return true if is Boolean
1488      */
1489     public boolean isBoolean()
1490     {
1491         return valueObject instanceof Boolean;
1492     }
1493 
1494     /***
1495      * Is the value a isNull
1496      *
1497      * @return true if is null
1498      */
1499     public boolean isNull()
1500     {
1501         return valueObject == null;
1502     }
1503 
1504     /***
1505      * Is the value a isString
1506      *
1507      * @return true if is String
1508      */
1509     public boolean isString()
1510     {
1511         return valueObject instanceof String;
1512     }
1513 
1514     /***
1515      * Is the value a isTime
1516      *
1517      * @return true if is java.sql.Time
1518      */
1519     public boolean isTime()
1520     {
1521         return valueObject instanceof java.sql.Time;
1522     }
1523 
1524     /***
1525      * Is the value a isTimestamp
1526      *
1527      * @return true if is java.sql.Timestamp
1528      */
1529     public boolean isTimestamp()
1530     {
1531         return valueObject instanceof java.sql.Timestamp;
1532     }
1533 
1534     /***
1535      * Is the value a isUtilDate
1536      *
1537      * @return true if is java.util.Date
1538      */
1539     public boolean isUtilDate()
1540     {
1541         return valueObject instanceof java.util.Date;
1542     }
1543 
1544     /***
1545      * Return the type of this value
1546      *
1547      * @return the type of this value
1548      */
1549     public int type()
1550     {
1551         return this.type;
1552     }
1553 
1554     /***
1555      * Gets the columnNumber which this value represents.
1556      *
1557      * @return an int
1558      */
1559     int columnNumber()
1560     {
1561         return this.columnNumber;
1562     }
1563 
1564     /***
1565      * DOCUMENT ME!
1566      *
1567      * @param value TODO: DOCUMENT ME!
1568      *
1569      * @return true if (true || t | yes | y | 1)
1570      */
1571     private boolean isTrue(String value)
1572     {
1573         return (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("t") || value.equalsIgnoreCase("yes")
1574         || value.equalsIgnoreCase("y") || value.equals("1"));
1575     }
1576 }