001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.util
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.util;
023:
024: import java.sql.ResultSet;
025: import java.sql.SQLException;
026: import java.sql.CallableStatement;
027: import java.sql.PreparedStatement;
028: import java.math.BigDecimal;
029:
030: /**
031: * BigDecimalHandler provides wrappers for JDBC API methods which use BigDecimal.
032: * When writing tests which use BigDecimal, the methods in this class can be called
033: * instead of directly calling JDBC methods. This way the same test can be used in JVMs
034: * like J2ME/CDC/Foundation Profile, which do not have BigDecimal class.
035: *
036: * * @author deepa
037: *
038: */
039: public class BigDecimalHandler {
040:
041: public static int representation;
042: public static final int STRING_REPRESENTATION = 1;
043: public static final int BIGDECIMAL_REPRESENTATION = 2;
044:
045: static {
046: try {
047: Class.forName("java.math.BigDecimal");
048: representation = BIGDECIMAL_REPRESENTATION;
049: } catch (ClassNotFoundException e) {
050: //Used for J2ME/Foundation
051: representation = STRING_REPRESENTATION;
052: }
053: }
054:
055: //Type conversions supported by ResultSet getBigDecimal method - JDBC3.0 Table B-6
056: private static final int[] bdConvertibleTypes = {
057: java.sql.Types.TINYINT, java.sql.Types.SMALLINT,
058: java.sql.Types.INTEGER, java.sql.Types.BIGINT,
059: java.sql.Types.REAL, java.sql.Types.FLOAT,
060: java.sql.Types.DOUBLE, java.sql.Types.DECIMAL,
061: java.sql.Types.NUMERIC, java.sql.Types.BIT,
062: //java.sql.Types.BOOLEAN, //Not supported in jdk13
063: java.sql.Types.CHAR, java.sql.Types.VARCHAR,
064: java.sql.Types.LONGVARCHAR };
065:
066: /** This method is a wrapper for the ResultSet method getBigDecimal(int columnIndex).
067: *
068: * @param rs ResultSet
069: * @param columnIndex Column Index
070: * @return String value of getXXX(columnIndex)method on the ResultSet
071: * @throws SQLException
072: */
073: public static String getBigDecimalString(ResultSet rs,
074: int columnIndex) throws SQLException {
075: String bigDecimalString = null;
076:
077: switch (representation) {
078: case BIGDECIMAL_REPRESENTATION:
079: //Call toString() only for non-null values, else return null
080: if (rs.getBigDecimal(columnIndex) != null)
081: bigDecimalString = rs.getBigDecimal(columnIndex)
082: .toString();
083: break;
084: case STRING_REPRESENTATION:
085: bigDecimalString = rs.getString(columnIndex);
086: int columnType = rs.getMetaData()
087: .getColumnType(columnIndex);
088: if ((bigDecimalString != null)
089: && !canConvertToDecimal(columnType))
090: throw new SQLException(
091: "Invalid data conversion. Method not called.");
092: break;
093: default:
094: new Exception("Failed: Invalid Big Decimal representation")
095: .printStackTrace();
096: }
097: return bigDecimalString;
098: }
099:
100: /** This method is a wrapper for ResultSet method getBigDecimal(String columnName).
101: *
102: * @param rs ResultSet
103: * @param columnName Column Name
104: * @param columnIndex Coulumn Index
105: * @return String value of getXXX(columnName)method on the ResultSet
106: * @throws SQLException
107: */
108: public static String getBigDecimalString(ResultSet rs,
109: String columnName, int columnIndex) throws SQLException {
110: String 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)
117: .toString();
118: }
119: break;
120: case STRING_REPRESENTATION:
121: bigDecimalString = rs.getString(columnName);
122: int columnType = rs.getMetaData()
123: .getColumnType(columnIndex);
124: if ((bigDecimalString != null)
125: && !canConvertToDecimal(columnType))
126: throw new SQLException(
127: "Invalid data conversion. Method not called.");
128: break;
129: default:
130: new Exception("Failed: Invalid Big Decimal representation")
131: .printStackTrace();
132: }
133: return bigDecimalString;
134: }
135:
136: /** This method is a wrapper for ResultSet method getObject(int columnIndex)
137: *
138: * @param rs ResultSet
139: * @param columnIndex ColumnIndex
140: * @return String value of getXXX(columnIndex) method on the ResultSet
141: * @throws SQLException
142: */
143: public static String getObjectString(ResultSet rs, int columnIndex)
144: throws SQLException {
145: String objectString = null;
146:
147: switch (representation) {
148: case BIGDECIMAL_REPRESENTATION:
149: //Call toString() only for non-null values, else return null
150: if (rs.getObject(columnIndex) != null)
151: objectString = rs.getObject(columnIndex).toString();
152: break;
153: case STRING_REPRESENTATION:
154: int columnType = rs.getMetaData()
155: .getColumnType(columnIndex);
156: if (columnType == java.sql.Types.DECIMAL) {
157: objectString = rs.getString(columnIndex);
158: } else
159: //Call toString() only for non-null values, else return null
160: if (rs.getObject(columnIndex) != null)
161: objectString = rs.getObject(columnIndex).toString();
162: break;
163: default:
164: new Exception("Failed: Invalid Big Decimal representation")
165: .printStackTrace();
166: }
167: return objectString;
168: }
169:
170: /** This method is a wrapper for ResultSet method getObject(String columnName)
171: * @param rs ResultSet
172: * @param columnName Column Name
173: * @param columnIndex Column Index
174: * @return String value of getXXX(columnName) method on the ResultSet
175: * @throws SQLException
176: */
177: public static String getObjectString(ResultSet rs,
178: String columnName, int columnIndex) throws SQLException {
179: String objectString = null;
180:
181: switch (representation) {
182: case BIGDECIMAL_REPRESENTATION:
183: //Call toString() only for non-null values, else return null
184: if (rs.getObject(columnName) != null)
185: objectString = rs.getObject(columnName).toString();
186: break;
187: case STRING_REPRESENTATION:
188: int columnType = rs.getMetaData()
189: .getColumnType(columnIndex);
190: if (columnType == java.sql.Types.DECIMAL) {
191: objectString = rs.getString(columnName);
192: } else
193: //Call toString() only for non-null values, else return null
194: if (rs.getObject(columnName) != null)
195: objectString = rs.getObject(columnName).toString();
196: break;
197: default:
198: new Exception("Failed: Invalid Big Decimal representation")
199: .printStackTrace();
200: }
201: return objectString;
202: }
203:
204: /** This method is a wrapper for ResultSet method
205: * updateBigDecimal(int columnIndex, BigDecimal x)
206: * @param rs ResultSet
207: * @param columnIndex Column Index
208: * @param bdString String to be used in updateXXX method
209: * @throws SQLException
210: */
211: public static void updateBigDecimalString(ResultSet rs,
212: int columnIndex, String bdString) throws SQLException {
213:
214: switch (representation) {
215: case BIGDECIMAL_REPRESENTATION:
216: BigDecimal bd = (bdString == null) ? null : new BigDecimal(
217: bdString);
218: rs.updateBigDecimal(columnIndex, bd);
219: break;
220: case STRING_REPRESENTATION:
221: rs.updateString(columnIndex, bdString);
222: break;
223: default:
224: new Exception("Failed: Invalid Big Decimal representation")
225: .printStackTrace();
226: }
227: }
228:
229: /** This method is a wrapper for ResultSet method
230: * updateBigDecimal(String columnName, BigDecimal x)
231: * @param rs ResultSet
232: * @param columnName Column Name
233: * @param bdString String to be used in updateXXX method
234: * @throws SQLException
235: */
236: public static void updateBigDecimalString(ResultSet rs,
237: String columnName, String bdString) throws SQLException {
238:
239: switch (representation) {
240: case BIGDECIMAL_REPRESENTATION:
241: BigDecimal bd = (bdString == null) ? null : new BigDecimal(
242: bdString);
243: rs.updateBigDecimal(columnName, bd);
244: break;
245: case STRING_REPRESENTATION:
246: rs.updateString(columnName, bdString);
247: break;
248: default:
249: new Exception("Failed: Invalid Big Decimal representation")
250: .printStackTrace();
251: }
252: }
253:
254: /** This method is a wrapper for the CallableStatement method getBigDecimal(int parameterIndex).
255: * The wrapper method needs the parameterType as an input since ParameterMetaData is not available in JSR169.
256: *
257: * @param cs CallableStatement
258: * @param parameterIndex Parameter Index
259: * @param parameterType Parameter Type
260: * @return String value of getXXX(parameterIndex)method on the CallableStatement
261: * @throws SQLException
262: */
263: public static String getBigDecimalString(CallableStatement cs,
264: int parameterIndex, int parameterType) throws SQLException {
265: String bigDecimalString = null;
266:
267: switch (representation) {
268: case BIGDECIMAL_REPRESENTATION:
269: //Call toString() only for non-null values, else return null
270: if (cs.getBigDecimal(parameterIndex) != null)
271: bigDecimalString = cs.getBigDecimal(parameterIndex)
272: .toString();
273: break;
274: case STRING_REPRESENTATION:
275: bigDecimalString = cs.getString(parameterIndex);
276: if ((bigDecimalString != null)
277: && !canConvertToDecimal(parameterType))
278: throw new SQLException(
279: "Invalid data conversion. Method not called.");
280: break;
281: default:
282: new Exception("Failed: Invalid Big Decimal representation")
283: .printStackTrace();
284: }
285: return bigDecimalString;
286: }
287:
288: /** This method is a wrapper for the PreparedStatement method setBigDecimal(int parameterIndex,BigDecimal x)
289: *
290: * @param ps PreparedStatement
291: * @param parameterIndex Parameter Index
292: * @param bdString String to be used in setXXX method
293: * @throws SQLException
294: */
295: public static void setBigDecimalString(PreparedStatement ps,
296: int parameterIndex, String bdString) throws SQLException {
297:
298: switch (representation) {
299: case BIGDECIMAL_REPRESENTATION:
300: BigDecimal bd = (bdString == null) ? null : new BigDecimal(
301: bdString);
302: ps.setBigDecimal(parameterIndex, bd);
303: break;
304: case STRING_REPRESENTATION:
305: //setString is used since setBigDecimal is not available in JSR169
306: //If bdString cannot be converted to short,int or long, this will throw
307: //"Invalid character string format exception"
308: ps.setString(parameterIndex, bdString);
309: break;
310: default:
311: new Exception("Failed: Invalid Big Decimal representation")
312: .printStackTrace();
313: }
314: }
315:
316: /** This method is a wrapper for the PreparedStatement method setObject(int parameterIndex, Object x)
317: *
318: * @param ps PreparedStatement
319: * @param parameterIndex Parameter Index
320: * @param bdString String to be used in setObject method
321: * @throws SQLException
322: */
323: public static void setObjectString(PreparedStatement ps,
324: int parameterIndex, String objectString)
325: throws SQLException {
326:
327: switch (representation) {
328: case BIGDECIMAL_REPRESENTATION:
329: BigDecimal bd = (objectString == null) ? null
330: : new BigDecimal(objectString);
331: ps.setObject(parameterIndex, bd);
332: break;
333: case STRING_REPRESENTATION:
334: ps.setObject(parameterIndex, objectString);
335: break;
336: default:
337: new Exception("Failed: Invalid Big Decimal representation")
338: .printStackTrace();
339: }
340: }
341:
342: /** This method checks that the SQL type can be converted to Decimal
343: *
344: * @param rs ResultSet
345: * @param columnIndex Column Index
346: * @return true if the SQL type is convertible to DECIMAL, false otherwise.
347: * @throws SQLException
348: */
349: protected static boolean canConvertToDecimal(int type)
350: throws SQLException {
351: boolean canConvert = false;
352:
353: for (int bdType = 0; bdType < bdConvertibleTypes.length; bdType++) {
354: if (type == bdConvertibleTypes[bdType]) {
355: canConvert = true;
356: break;
357: }
358: }
359:
360: return canConvert;
361: }
362:
363: }
|