1 21 22 package org.apache.derbyTesting.functionTests.util; 23 import java.sql.ResultSet ; 24 import java.sql.SQLException ; 25 import java.sql.CallableStatement ; 26 import java.sql.PreparedStatement ; 27 import java.math.BigDecimal ; 28 29 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 e){ 50 representation = STRING_REPRESENTATION; 52 } 53 } 54 55 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.CHAR, 69 java.sql.Types.VARCHAR, 70 java.sql.Types.LONGVARCHAR 71 }; 72 73 80 public static String getBigDecimalString(ResultSet rs, int columnIndex) throws SQLException { 81 String bigDecimalString=null; 82 83 switch(representation){ 84 case BIGDECIMAL_REPRESENTATION: 85 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 ("Invalid data conversion. Method not called."); 94 break; 95 default: 96 new Exception ("Failed: Invalid Big Decimal representation").printStackTrace(); 97 } 98 return bigDecimalString; 99 } 100 101 109 public static String getBigDecimalString(ResultSet rs, String columnName, int columnIndex) throws SQLException { 110 String bigDecimalString = null; 111 112 switch(representation){ 113 case BIGDECIMAL_REPRESENTATION: 114 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 ("Invalid data conversion. Method not called."); 124 break; 125 default: 126 new Exception ("Failed: Invalid Big Decimal representation").printStackTrace(); 127 } 128 return bigDecimalString; 129 } 130 131 138 public static String getObjectString(ResultSet rs, int columnIndex) throws SQLException { 139 String objectString = null; 140 141 switch(representation){ 142 case BIGDECIMAL_REPRESENTATION: 143 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 if(rs.getObject(columnIndex) != null) 155 objectString = rs.getObject(columnIndex).toString(); 156 break; 157 default: 158 new Exception ("Failed: Invalid Big Decimal representation").printStackTrace(); 159 } 160 return objectString; 161 } 162 163 170 public static String getObjectString(ResultSet rs, String columnName, int columnIndex) throws SQLException { 171 String objectString = null; 172 173 switch(representation){ 174 case BIGDECIMAL_REPRESENTATION: 175 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 if(rs.getObject(columnName) != null) 187 objectString = rs.getObject(columnName).toString(); 188 break; 189 default: 190 new Exception ("Failed: Invalid Big Decimal representation").printStackTrace(); 191 } 192 return objectString; 193 } 194 195 202 public static void updateBigDecimalString(ResultSet rs, int columnIndex, String bdString) throws SQLException { 203 204 switch(representation){ 205 case BIGDECIMAL_REPRESENTATION: 206 BigDecimal bd = (bdString == null) ? null : new BigDecimal (bdString); 207 rs.updateBigDecimal(columnIndex, bd); 208 break; 209 case STRING_REPRESENTATION: 210 rs.updateString(columnIndex, bdString); 211 break; 212 default: 213 new Exception ("Failed: Invalid Big Decimal representation").printStackTrace(); 214 } 215 } 216 217 224 public static void updateBigDecimalString(ResultSet rs, String columnName,String bdString) throws SQLException { 225 226 switch(representation){ 227 case BIGDECIMAL_REPRESENTATION: 228 BigDecimal bd = (bdString == null) ? null : new BigDecimal (bdString); 229 rs.updateBigDecimal(columnName, bd); 230 break; 231 case STRING_REPRESENTATION: 232 rs.updateString(columnName, bdString); 233 break; 234 default: 235 new Exception ("Failed: Invalid Big Decimal representation").printStackTrace(); 236 } 237 } 238 239 248 public static String getBigDecimalString(CallableStatement cs, int parameterIndex, int parameterType) throws SQLException { 249 String bigDecimalString = null; 250 251 switch(representation){ 252 case BIGDECIMAL_REPRESENTATION: 253 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 ("Invalid data conversion. Method not called."); 261 break; 262 default: 263 new Exception ("Failed: Invalid Big Decimal representation").printStackTrace(); 264 } 265 return bigDecimalString; 266 } 267 268 275 public static void setBigDecimalString(PreparedStatement ps, int parameterIndex, String bdString) throws SQLException { 276 277 switch(representation){ 278 case BIGDECIMAL_REPRESENTATION: 279 BigDecimal bd = (bdString == null) ? null : new BigDecimal (bdString); 280 ps.setBigDecimal(parameterIndex, bd); 281 break; 282 case STRING_REPRESENTATION: 283 ps.setString(parameterIndex,bdString); 287 break; 288 default: 289 new Exception ("Failed: Invalid Big Decimal representation").printStackTrace(); 290 } 291 } 292 293 300 public static void setObjectString(PreparedStatement ps, int parameterIndex, String objectString) throws SQLException { 301 302 switch(representation){ 303 case BIGDECIMAL_REPRESENTATION: 304 BigDecimal bd = (objectString == null) ? null : new BigDecimal (objectString); 305 ps.setObject(parameterIndex,bd); 306 break; 307 case STRING_REPRESENTATION: 308 ps.setObject(parameterIndex,objectString); 309 break; 310 default: 311 new Exception ("Failed: Invalid Big Decimal representation").printStackTrace(); 312 } 313 } 314 315 322 protected static boolean canConvertToDecimal(int type) throws SQLException { 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 |