KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > dods > builder > generator > query > RDBColumnValue


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: RDBColumnValue.java,v 1.1 2004/09/03 13:42:41 sinisa Exp $
22  */

23 package com.lutris.dods.builder.generator.query;
24
25 import java.math.BigDecimal JavaDoc;
26
27 /**
28  * After a Vector of RDBColumn objects is used to construct
29  * an instance of QueryBuilder, the QueryBuilder.getNextRow() method
30  * is used to return an RDBRow object which contains RDBColumnValue objects
31  * representing the column values in the returned row.
32  * An instance of RDBColumnValue is returned by the RDBRow.get() method.
33  * The value contained in the RDBColumnValue object is extracted
34  * using RDBColumnValue.get methods.
35  * @see QueryBuilder
36  * @author Jay Gunter
37  */

38 public class RDBColumnValue extends RDBColumn {
39
40     /**
41      * The constructor for RDBColumnValue is used only by QueryBuilder,
42      * never by the application developer.
43      *
44      * return the RDBColumnValue object containing the value for the
45      * RDBColumn specified.
46      * @param column the column specified by QueryBuilder during row access.
47      * @param val the value for the column in the currently accessed row.
48      * @see QueryBuilder
49      * author Jay Gunter
50      */

51     public RDBColumnValue(RDBColumn column, Object JavaDoc val) {
52         super(column.table, column.name);
53         value = val;
54     }
55     private Object JavaDoc value;
56
57     /**
58      * Return the column value as an Object.
59      * Rarely used by the application developer
60      * (only when the type of the column value is not known.)
61      *
62      * @return the column value as an Object whose type can be interrogated.
63      * @see QueryBuilder
64      * author Jay Gunter
65      */

66     public Object JavaDoc getValue() {
67         return value;
68     }
69
70     /**
71      * Used only by the QueryBuilder.
72      * Sets the value for the RDBColumnValue object.
73      *
74      * @param o the value as an Object
75      * author Jay Gunter
76      */

77     public void setValue(Object JavaDoc o) {
78         value = o;
79     }
80
81     /**
82      * Retreive column value as a String.
83      *
84      * @return the column value as a String.
85      * @exception ColumnTypeException If the column value is not a String
86      * (if the column type is not CHAR, VARCHAR, VARCHAR2, etc.)
87      * @see QueryBuilder
88      * author Jay Gunter
89      */

90     public String JavaDoc getString()
91         throws ColumnTypeException {
92         if (!(value instanceof String JavaDoc)) {
93             throw new ColumnTypeException(this, "String");
94         }
95         return (String JavaDoc) value;
96     }
97
98     /**
99      * Retreive column value as an int.
100      *
101      * @return the column value as an int.
102      * @param nullOk if false and the column in the row contains
103      * a database NULL value, a ColumnNullException is thrown.
104      * If true and the column in the row contains a NULL, 0 is returned.
105      * @exception ColumnTypeException If the column value is not an Integer
106      * (if the column type is not INTEGER, LONG, etc.) or if NULL is not
107      * an allowed value (nullOk==false.)
108      * @see QueryBuilder
109      * author Jay Gunter
110      */

111     public int getInteger(boolean nullOk)
112         throws ColumnTypeException, ColumnNullException {
113         if (!(value instanceof Integer JavaDoc)) {
114             throw new ColumnTypeException(this, "Integer");
115         }
116         if (null == value) {
117             if (!nullOk) {
118                 throw new ColumnNullException(this);
119             } else {
120                 return 0;
121             }
122         }
123         return ((Integer JavaDoc) value).intValue();
124     }
125
126     /**
127      * Retreive column value as an int.
128      *
129      * @return the column value as an int. If the column value is NULL,
130      * 0 is returned.
131      * @exception ColumnTypeException If the column value is not an Integer
132      * (if the column type is not INTEGER, LONG, etc.)
133      * @see QueryBuilder
134      * author Jay Gunter
135      */

136     public Integer JavaDoc getInteger()
137         throws ColumnTypeException {
138         if (!(value instanceof Integer JavaDoc)) {
139             throw new ColumnTypeException(this, "Integer");
140         }
141         return (Integer JavaDoc) value;
142     }
143
144     /**
145      * Retreive column value as an double.
146      *
147      * @return the column value as an double.
148      * @param nullOk if false and the column in the row contains
149      * a database NULL value, a ColumnNullException is thrown.
150      * If true and the column in the row contains a NULL, 0 is returned.
151      * @exception ColumnTypeException If the column value is not a Double
152      * (if the column type is not FLOAT, DOUBLE, NUMERIC, DECIMAL, etc.)
153      * or if NULL is not an allowed value (nullOk==false.)
154      * @see QueryBuilder
155      * author Jay Gunter
156      */

157     public double getDouble(boolean nullOk)
158         throws ColumnTypeException, ColumnNullException {
159         if (null == value) {
160             if (!nullOk) {
161                 throw new ColumnNullException(this);
162             } else {
163                 return 0;
164             }
165         }
166         Double JavaDoc d = getDouble();
167
168         return d.doubleValue();
169     }
170
171     /**
172      * Retreive column value as an double.
173      *
174      * @return the column value as an double. If the column value is NULL,
175      * 0 is returned.
176      * @exception ColumnTypeException If the column value is not a Double
177      * (if the column type is not FLOAT, DOUBLE, NUMERIC, DECIMAL, etc.)
178      * @see QueryBuilder
179      * author Jay Gunter
180      */

181     public Double JavaDoc getDouble()
182         throws ColumnTypeException {
183         if (value instanceof Double JavaDoc) {
184             return (Double JavaDoc) value;
185         }
186         if (value instanceof BigDecimal JavaDoc) {
187             BigDecimal JavaDoc b = (BigDecimal JavaDoc) value;
188
189             return (Double JavaDoc) new Double JavaDoc(b.toString());
190         }
191         throw new ColumnTypeException(this, "Double/BigDecimal");
192     }
193
194     /**
195      * Retreive column value as a BigDecimal.
196      *
197      * @return the column value as a BigDecimal.
198      * If the column value is NULL, 0 is returned.
199      * @exception ColumnTypeException If the column value is not a BigDecimal
200      * (if the column type is not NUMERIC, or DECIMAL.)
201      * @see QueryBuilder
202      * author Jay Gunter
203      */

204     public BigDecimal JavaDoc getBigDecimal()
205         throws ColumnTypeException {
206         // Accomodate InstantDB which returns Long objects for DECIMAL columns.
207
if (value instanceof Long JavaDoc) {
208             return new BigDecimal JavaDoc(value.toString());
209         }
210         if (!(value instanceof BigDecimal JavaDoc)) {
211             throw new ColumnTypeException(this, "BigDecimal");
212         }
213         return (BigDecimal JavaDoc) value;
214     }
215 }
216
Popular Tags