KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > util > BigDecimalHandler


1 /*
2
3 Derby - Class org.apache.derbyTesting.functionTests.util
4
5 Licensed to the Apache Software Foundation (ASF) under one or more
6 contributor license agreements. See the NOTICE file distributed with
7 this work for additional information regarding copyright ownership.
8 The ASF licenses this file to You under the Apache License, Version 2.0
9 (the "License"); you may not use this file except in compliance with
10 the License. You may obtain a copy of the License at
11
12    http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19
20 */

21
22 package org.apache.derbyTesting.functionTests.util;
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.sql.CallableStatement JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.math.BigDecimal JavaDoc;
28
29 /**
30  * BigDecimalHandler provides wrappers for JDBC API methods which use BigDecimal.
31  * When writing tests which use BigDecimal, the methods in this class can be called
32  * instead of directly calling JDBC methods. This way the same test can be used in JVMs
33  * like J2ME/CDC/Foundation Profile, which do not have BigDecimal class.
34  *
35  * * @author deepa
36  *
37  */

38 public class BigDecimalHandler {
39     
40     public static int representation;
41     public static final int STRING_REPRESENTATION = 1;
42     public static final int BIGDECIMAL_REPRESENTATION = 2;
43     
44     static{
45         try{
46             Class.forName("java.math.BigDecimal");
47             representation = BIGDECIMAL_REPRESENTATION;
48         }
49         catch(ClassNotFoundException JavaDoc e){
50             //Used for J2ME/Foundation
51
representation = STRING_REPRESENTATION;
52         }
53     }
54     
55     //Type conversions supported by ResultSet getBigDecimal method - JDBC3.0 Table B-6
56
private static final int[] bdConvertibleTypes =
57     { java.sql.Types.TINYINT,
58         java.sql.Types.SMALLINT,
59         java.sql.Types.INTEGER,
60         java.sql.Types.BIGINT,
61         java.sql.Types.REAL,
62         java.sql.Types.FLOAT,
63         java.sql.Types.DOUBLE,
64         java.sql.Types.DECIMAL,
65         java.sql.Types.NUMERIC,
66         java.sql.Types.BIT,
67         //java.sql.Types.BOOLEAN, //Not supported in jdk13
68
java.sql.Types.CHAR,
69         java.sql.Types.VARCHAR,
70         java.sql.Types.LONGVARCHAR
71     };
72     
73     /** This method is a wrapper for the ResultSet method getBigDecimal(int columnIndex).
74      *
75      * @param rs ResultSet
76      * @param columnIndex Column Index
77      * @return String value of getXXX(columnIndex)method on the ResultSet
78      * @throws SQLException
79      */

80     public static String JavaDoc getBigDecimalString(ResultSet JavaDoc rs, int columnIndex) throws SQLException JavaDoc{
81         String JavaDoc bigDecimalString=null;
82         
83         switch(representation){
84             case BIGDECIMAL_REPRESENTATION:
85                 //Call toString() only for non-null values, else return null
86
if(rs.getBigDecimal(columnIndex) != null)
87                     bigDecimalString = rs.getBigDecimal(columnIndex).toString();
88                 break;
89             case STRING_REPRESENTATION:
90                 bigDecimalString = rs.getString(columnIndex);
91                 int columnType= rs.getMetaData().getColumnType(columnIndex);
92                 if((bigDecimalString != null) && !canConvertToDecimal(columnType))
93                     throw new SQLException JavaDoc("Invalid data conversion. Method not called.");
94                 break;
95             default:
96                 new Exception JavaDoc("Failed: Invalid Big Decimal representation").printStackTrace();
97         }
98         return bigDecimalString;
99     }
100     
101     /** This method is a wrapper for ResultSet method getBigDecimal(String columnName).
102      *
103      * @param rs ResultSet
104      * @param columnName Column Name
105      * @param columnIndex Coulumn Index
106      * @return String value of getXXX(columnName)method on the ResultSet
107      * @throws SQLException
108      */

109     public static String JavaDoc getBigDecimalString(ResultSet JavaDoc rs, String JavaDoc columnName, int columnIndex) throws SQLException JavaDoc{
110         String JavaDoc bigDecimalString = null;
111                 
112         switch(representation){
113             case BIGDECIMAL_REPRESENTATION:
114                 //Call toString() only for non-null values, else return null
115
if(rs.getBigDecimal(columnName) != null){
116                     bigDecimalString = rs.getBigDecimal(columnName).toString();
117                 }
118                 break;
119             case STRING_REPRESENTATION:
120                 bigDecimalString = rs.getString(columnName);
121                 int columnType= rs.getMetaData().getColumnType(columnIndex);
122                 if((bigDecimalString != null) && !canConvertToDecimal(columnType))
123                     throw new SQLException JavaDoc("Invalid data conversion. Method not called.");
124                 break;
125             default:
126                 new Exception JavaDoc("Failed: Invalid Big Decimal representation").printStackTrace();
127         }
128         return bigDecimalString;
129     }
130     
131     /** This method is a wrapper for ResultSet method getObject(int columnIndex)
132      *
133      * @param rs ResultSet
134      * @param columnIndex ColumnIndex
135      * @return String value of getXXX(columnIndex) method on the ResultSet
136      * @throws SQLException
137      */

138     public static String JavaDoc getObjectString(ResultSet JavaDoc rs, int columnIndex) throws SQLException JavaDoc{
139         String JavaDoc objectString = null;
140         
141         switch(representation){
142             case BIGDECIMAL_REPRESENTATION:
143                 //Call toString() only for non-null values, else return null
144
if(rs.getObject(columnIndex) != null)
145                     objectString = rs.getObject(columnIndex).toString();
146                 break;
147             case STRING_REPRESENTATION:
148                 int columnType= rs.getMetaData().getColumnType(columnIndex);
149                 if(columnType == java.sql.Types.DECIMAL){
150                     objectString = rs.getString(columnIndex);
151                 }
152                 else
153                     //Call toString() only for non-null values, else return null
154
if(rs.getObject(columnIndex) != null)
155                         objectString = rs.getObject(columnIndex).toString();
156                     break;
157             default:
158                 new Exception JavaDoc("Failed: Invalid Big Decimal representation").printStackTrace();
159         }
160         return objectString;
161     }
162     
163     /** This method is a wrapper for ResultSet method getObject(String columnName)
164      * @param rs ResultSet
165      * @param columnName Column Name
166      * @param columnIndex Column Index
167      * @return String value of getXXX(columnName) method on the ResultSet
168      * @throws SQLException
169      */

170     public static String JavaDoc getObjectString(ResultSet JavaDoc rs, String JavaDoc columnName, int columnIndex) throws SQLException JavaDoc{
171         String JavaDoc objectString = null;
172                 
173         switch(representation){
174             case BIGDECIMAL_REPRESENTATION:
175                 //Call toString() only for non-null values, else return null
176
if(rs.getObject(columnName) != null)
177                     objectString = rs.getObject(columnName).toString();
178                 break;
179             case STRING_REPRESENTATION:
180                 int columnType= rs.getMetaData().getColumnType(columnIndex);
181                 if(columnType == java.sql.Types.DECIMAL){
182                     objectString = rs.getString(columnName);
183                 }
184                 else
185                     //Call toString() only for non-null values, else return null
186
if(rs.getObject(columnName) != null)
187                         objectString = rs.getObject(columnName).toString();
188                     break;
189             default:
190                 new Exception JavaDoc("Failed: Invalid Big Decimal representation").printStackTrace();
191         }
192         return objectString;
193     }
194     
195     /** This method is a wrapper for ResultSet method
196      * updateBigDecimal(int columnIndex, BigDecimal x)
197      * @param rs ResultSet
198      * @param columnIndex Column Index
199      * @param bdString String to be used in updateXXX method
200      * @throws SQLException
201      */

202     public static void updateBigDecimalString(ResultSet JavaDoc rs, int columnIndex, String JavaDoc bdString) throws SQLException JavaDoc{
203                 
204         switch(representation){
205             case BIGDECIMAL_REPRESENTATION:
206                 BigDecimal JavaDoc bd = (bdString == null) ? null : new BigDecimal JavaDoc(bdString);
207                 rs.updateBigDecimal(columnIndex, bd);
208                 break;
209             case STRING_REPRESENTATION:
210                 rs.updateString(columnIndex, bdString);
211                 break;
212             default:
213                 new Exception JavaDoc("Failed: Invalid Big Decimal representation").printStackTrace();
214         }
215     }
216     
217     /** This method is a wrapper for ResultSet method
218      * updateBigDecimal(String columnName, BigDecimal x)
219      * @param rs ResultSet
220      * @param columnName Column Name
221      * @param bdString String to be used in updateXXX method
222      * @throws SQLException
223      */

224     public static void updateBigDecimalString(ResultSet JavaDoc rs, String JavaDoc columnName,String JavaDoc bdString) throws SQLException JavaDoc{
225                 
226         switch(representation){
227             case BIGDECIMAL_REPRESENTATION:
228                 BigDecimal JavaDoc bd = (bdString == null) ? null : new BigDecimal JavaDoc(bdString);
229                 rs.updateBigDecimal(columnName, bd);
230                 break;
231             case STRING_REPRESENTATION:
232                 rs.updateString(columnName, bdString);
233                 break;
234             default:
235                 new Exception JavaDoc("Failed: Invalid Big Decimal representation").printStackTrace();
236         }
237     }
238
239     /** This method is a wrapper for the CallableStatement method getBigDecimal(int parameterIndex).
240      * The wrapper method needs the parameterType as an input since ParameterMetaData is not available in JSR169.
241      *
242      * @param cs CallableStatement
243      * @param parameterIndex Parameter Index
244      * @param parameterType Parameter Type
245      * @return String value of getXXX(parameterIndex)method on the CallableStatement
246      * @throws SQLException
247      */

248     public static String JavaDoc getBigDecimalString(CallableStatement JavaDoc cs, int parameterIndex, int parameterType) throws SQLException JavaDoc{
249         String JavaDoc bigDecimalString = null;
250         
251         switch(representation){
252             case BIGDECIMAL_REPRESENTATION:
253                 //Call toString() only for non-null values, else return null
254
if(cs.getBigDecimal(parameterIndex) != null)
255                     bigDecimalString = cs.getBigDecimal(parameterIndex).toString();
256                 break;
257             case STRING_REPRESENTATION:
258                 bigDecimalString = cs.getString(parameterIndex);
259                 if((bigDecimalString != null) && !canConvertToDecimal(parameterType))
260                     throw new SQLException JavaDoc("Invalid data conversion. Method not called.");
261                 break;
262             default:
263                 new Exception JavaDoc("Failed: Invalid Big Decimal representation").printStackTrace();
264         }
265         return bigDecimalString;
266     }
267
268     /** This method is a wrapper for the PreparedStatement method setBigDecimal(int parameterIndex,BigDecimal x)
269      *
270      * @param ps PreparedStatement
271      * @param parameterIndex Parameter Index
272      * @param bdString String to be used in setXXX method
273      * @throws SQLException
274      */

275     public static void setBigDecimalString(PreparedStatement JavaDoc ps, int parameterIndex, String JavaDoc bdString) throws SQLException JavaDoc{
276         
277         switch(representation){
278             case BIGDECIMAL_REPRESENTATION:
279                 BigDecimal JavaDoc bd = (bdString == null) ? null : new BigDecimal JavaDoc(bdString);
280                 ps.setBigDecimal(parameterIndex, bd);
281                 break;
282             case STRING_REPRESENTATION:
283                 //setString is used since setBigDecimal is not available in JSR169
284
//If bdString cannot be converted to short,int or long, this will throw
285
//"Invalid character string format exception"
286
ps.setString(parameterIndex,bdString);
287                 break;
288             default:
289                 new Exception JavaDoc("Failed: Invalid Big Decimal representation").printStackTrace();
290         }
291     }
292     
293     /** This method is a wrapper for the PreparedStatement method setObject(int parameterIndex, Object x)
294      *
295      * @param ps PreparedStatement
296      * @param parameterIndex Parameter Index
297      * @param bdString String to be used in setObject method
298      * @throws SQLException
299      */

300     public static void setObjectString(PreparedStatement JavaDoc ps, int parameterIndex, String JavaDoc objectString) throws SQLException JavaDoc{
301         
302         switch(representation){
303             case BIGDECIMAL_REPRESENTATION:
304                 BigDecimal JavaDoc bd = (objectString == null) ? null : new BigDecimal JavaDoc(objectString);
305                 ps.setObject(parameterIndex,bd);
306                 break;
307             case STRING_REPRESENTATION:
308                 ps.setObject(parameterIndex,objectString);
309                 break;
310             default:
311                 new Exception JavaDoc("Failed: Invalid Big Decimal representation").printStackTrace();
312         }
313     }
314     
315     /** This method checks that the SQL type can be converted to Decimal
316      *
317      * @param rs ResultSet
318      * @param columnIndex Column Index
319      * @return true if the SQL type is convertible to DECIMAL, false otherwise.
320      * @throws SQLException
321      */

322     protected static boolean canConvertToDecimal(int type) throws SQLException JavaDoc{
323         boolean canConvert = false;
324         
325         for (int bdType = 0; bdType < bdConvertibleTypes.length; bdType++){
326             if(type == bdConvertibleTypes[bdType]){
327                 canConvert = true;
328                 break;
329             }
330         }
331         
332         return canConvert;
333     }
334     
335 }
336
Popular Tags