1 16 package org.apache.cocoon.util; 17 18 import java.io.BufferedInputStream ; 19 import java.io.ByteArrayInputStream ; 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.InputStream ; 23 import java.math.BigDecimal ; 24 import java.sql.Array ; 25 import java.sql.Blob ; 26 import java.sql.Clob ; 27 import java.sql.Date ; 28 import java.sql.PreparedStatement ; 29 import java.sql.ResultSet ; 30 import java.sql.SQLException ; 31 import java.sql.Struct ; 32 import java.sql.Time ; 33 import java.sql.Timestamp ; 34 import java.sql.Types ; 35 import java.util.Calendar ; 36 import java.util.Collections ; 37 import java.util.HashMap ; 38 import java.util.Map ; 39 40 import org.apache.avalon.framework.configuration.Configuration; 41 import org.apache.cocoon.servlet.multipart.Part; 42 import org.apache.commons.lang.BooleanUtils; 43 import org.apache.excalibur.source.Source; 44 45 46 83 public class JDBCTypeConversions { 84 public static final Map typeConstants; 85 86 static { 87 90 Map constants = new HashMap(); 91 constants.put("clob", new Integer (Types.CLOB)); 92 constants.put("ascii", new Integer (Types.CHAR)); 93 constants.put("big-decimal", new Integer (Types.BIGINT)); 94 constants.put("binary", new Integer (Types.VARBINARY)); 95 constants.put("blob", new Integer (Types.BLOB)); 96 constants.put("boolean", new Integer (Types.BIT)); 97 constants.put("byte", new Integer (Types.TINYINT)); 98 constants.put("string", new Integer (Types.VARCHAR)); 99 constants.put("date", new Integer (Types.DATE)); 100 constants.put("double", new Integer (Types.DOUBLE)); 101 constants.put("float", new Integer (Types.FLOAT)); 102 constants.put("int", new Integer (Types.INTEGER)); 103 constants.put("long", new Integer (Types.NUMERIC)); 104 constants.put("short", new Integer (Types.SMALLINT)); 105 constants.put("time", new Integer (Types.TIME)); 106 constants.put("time-stamp", new Integer (Types.TIMESTAMP)); 107 constants.put("array", new Integer (Types.ARRAY)); 108 constants.put("row", new Integer (Types.STRUCT)); 109 constants.put("object", new Integer (Types.OTHER)); 110 typeConstants = Collections.unmodifiableMap(constants); 111 } 112 113 118 public static Object convert(Object value, String jType) { 119 120 Object object=null; 121 if (jType.equalsIgnoreCase("string")) { 122 if (value instanceof String ) { 123 object = value; 124 } else { 125 object = value.toString(); 126 } 127 } else if (jType.equalsIgnoreCase("int")) { 128 if (value instanceof String ) { 129 object = Integer.decode((String )value); 130 } else if (value instanceof Integer ) { 131 object = value; 132 } else { 133 } 135 } else if (jType.equalsIgnoreCase("long")) { 136 if (value instanceof String ) { 137 object = Long.decode((String )value); 138 } else if (value instanceof Long ) { 139 object = value; 140 } else { 141 } 143 } else { 144 } 147 return object; 148 } 149 150 151 152 156 public static Object getColumn(ResultSet set, Configuration column ) throws Exception { 157 158 Integer type = (Integer ) JDBCTypeConversions.typeConstants.get(column.getAttribute("type")); 159 String dbcol = column.getAttribute("name"); 160 Object value = null; 161 162 switch (type.intValue()) { 163 case Types.CLOB: 164 case Types.CHAR: 165 Clob dbClob = set.getClob(dbcol); 166 int length = (int) dbClob.length(); 167 InputStream asciiStream = new BufferedInputStream (dbClob.getAsciiStream()); 168 byte[] buffer = new byte[length]; 169 asciiStream.read(buffer); 170 String str = new String (buffer); 171 asciiStream.close(); 172 value = str; 173 break; 174 case Types.BIGINT: 175 value = set.getBigDecimal(dbcol); 176 break; 177 case Types.TINYINT: 178 value = new Byte (set.getByte(dbcol)); 179 break; 180 case Types.VARCHAR: 181 value = set.getString(dbcol); 182 break; 183 case Types.DATE: 184 value = set.getDate(dbcol); 185 break; 186 case Types.DOUBLE: 187 value = new Double (set.getDouble(dbcol)); 188 break; 189 case Types.FLOAT: 190 value = new Float (set.getFloat(dbcol)); 191 break; 192 case Types.INTEGER: 193 value = new Integer (set.getInt(dbcol)); 194 break; 195 case Types.NUMERIC: 196 value = new Long (set.getLong(dbcol)); 197 break; 198 case Types.SMALLINT: 199 value = new Short (set.getShort(dbcol)); 200 break; 201 case Types.TIME: 202 value = set.getTime(dbcol); 203 break; 204 case Types.TIMESTAMP: 205 value = set.getTimestamp(dbcol); 206 break; 207 case Types.ARRAY: 208 value = set.getArray(dbcol); break; 210 case Types.BIT: 211 value = BooleanUtils.toBooleanObject(set.getBoolean(dbcol)); 212 break; 213 case Types.STRUCT: 214 value = (Struct ) set.getObject(dbcol); 215 break; 216 case Types.OTHER: 217 value = set.getObject(dbcol); 218 break; 219 220 default: 221 value = ""; 223 break; 224 } 225 226 return value; 227 } 228 229 230 237 public static void setColumn(PreparedStatement statement, int position, Object value, Integer typeObject) throws Exception { 238 if (value instanceof String ) { 239 value = ((String ) value).trim(); 240 } 241 if (typeObject == null) { 242 throw new SQLException ("Can't set column because the type is unrecognized"); 243 } 244 if (value == null) { 245 246 statement.setNull(position, typeObject.intValue()); 247 return; 248 } 249 if ("".equals(value)) { 250 switch (typeObject.intValue()) { 251 case Types.CHAR: 252 case Types.CLOB: 253 case Types.VARCHAR: 254 256 break; 257 default: 258 260 statement.setNull(position, typeObject.intValue()); 261 return; 262 } 263 } 264 265 File file = null; 266 int length = -1; 267 InputStream asciiStream = null; 268 269 switch (typeObject.intValue()) { 272 case Types.CLOB: 273 Clob clob = null; 275 if (value instanceof Clob ) { 276 clob = (Clob ) value; 277 } else if (value instanceof File ) { 278 File asciiFile = (File ) value; 279 asciiStream = new BufferedInputStream (new FileInputStream (asciiFile)); 280 length = (int) asciiFile.length(); 281 clob = new ClobHelper(asciiStream, length); 282 } else if (value instanceof Part) { 283 Part anyFile = (Part) value; 284 asciiStream = new BufferedInputStream (anyFile.getInputStream()); 285 length = anyFile.getSize(); 286 clob = new ClobHelper(asciiStream, length); 287 } else if (value instanceof JDBCxlobHelper) { 288 asciiStream = ((JDBCxlobHelper) value).inputStream; 289 length = ((JDBCxlobHelper) value).length; 290 clob = new ClobHelper(asciiStream, length); 291 } else if (value instanceof Source) { 292 asciiStream = ((Source) value).getInputStream(); 293 length = (int)((Source) value).getContentLength(); 294 clob = new ClobHelper(asciiStream, length); 295 } else { 296 String asciiText = value.toString(); 297 asciiStream = new ByteArrayInputStream (asciiText.getBytes()); 298 length = asciiText.length(); 299 clob = new ClobHelper(asciiStream, length); 300 } 301 302 statement.setClob(position, clob); 303 break; 304 case Types.CHAR: 305 308 if (value instanceof File ) { 309 File asciiFile = (File ) value; 310 asciiStream = new BufferedInputStream (new FileInputStream (asciiFile)); 311 length = (int) asciiFile.length(); 312 } else if (value instanceof JDBCxlobHelper) { 313 asciiStream = ((JDBCxlobHelper) value).inputStream; 314 length = ((JDBCxlobHelper) value).length; 315 } else if (value instanceof Source) { 316 asciiStream = ((Source) value).getInputStream(); 317 length = (int)((Source) value).getContentLength(); 318 } else if (value instanceof Part) { 319 Part anyFile = (Part) value; 320 asciiStream = new BufferedInputStream (anyFile.getInputStream()); 321 length = anyFile.getSize(); 322 clob = new ClobHelper(asciiStream, length); 323 } else { 324 String asciiText = value.toString(); 325 asciiStream = new BufferedInputStream (new ByteArrayInputStream (asciiText.getBytes())); 326 length = asciiText.length(); 327 } 328 329 statement.setAsciiStream(position, asciiStream, length); 330 break; 331 case Types.BIGINT: 332 BigDecimal bd = null; 334 335 if (value instanceof BigDecimal ) { 336 bd = (BigDecimal ) value; 337 } else if (value instanceof Number ) { 338 bd = BigDecimal.valueOf(((Number )value).longValue()); 339 } else { 340 bd = new BigDecimal (value.toString()); 341 } 342 343 statement.setBigDecimal(position, bd); 344 break; 345 case Types.TINYINT: 346 Byte b = null; 348 349 if (value instanceof Byte ) { 350 b = (Byte ) value; 351 } else if (value instanceof Number ) { 352 b = new Byte (((Number ) value).byteValue()); 353 } else { 354 b = new Byte (value.toString()); 355 } 356 357 statement.setByte(position, b.byteValue()); 358 break; 359 case Types.DATE: 360 Date d = null; 362 363 if (value instanceof Date ) { 364 d = (Date ) value; 365 } else if (value instanceof java.util.Date ) { 366 d = new Date (((java.util.Date ) value).getTime()); 367 } else if (value instanceof Calendar ) { 368 d = new Date (((Calendar ) value).getTime().getTime()); 369 } else { 370 d = Date.valueOf(value.toString()); 371 } 372 373 statement.setDate(position, d); 374 break; 375 case Types.DOUBLE: 376 double db; 378 379 if (value instanceof Number ) { 380 db = (((Number ) value).doubleValue()); 381 } else { 382 db = Double.parseDouble(value.toString()); 383 } 384 statement.setDouble(position, db); 385 break; 386 case Types.FLOAT: 387 float f; 389 390 if (value instanceof Number ) { 391 f = (((Number ) value).floatValue()); 392 } else { 393 f = Float.parseFloat(value.toString()); 394 } 395 statement.setFloat(position, f); 396 break; 397 case Types.NUMERIC: 398 long l; 400 401 if (value instanceof Number ) { 402 l = (((Number ) value).longValue()); 403 } else { 404 l = Long.parseLong(value.toString()); 405 } 406 407 statement.setLong(position, l); 408 break; 409 case Types.SMALLINT: 410 Short s = null; 412 413 if (value instanceof Short ) { 414 s = (Short ) value; 415 } else if (value instanceof Number ) { 416 s = new Short (((Number ) value).shortValue()); 417 } else { 418 s = new Short (value.toString()); 419 } 420 421 statement.setShort(position, s.shortValue()); 422 break; 423 case Types.TIME: 424 Time t = null; 426 427 if (value instanceof Time ) { 428 t = (Time ) value; 429 } else if (value instanceof java.util.Date ){ 430 t = new Time (((java.util.Date ) value).getTime()); 431 } else { 432 t = Time.valueOf(value.toString()); 433 } 434 435 statement.setTime(position, t); 436 break; 437 case Types.TIMESTAMP: 438 Timestamp ts = null; 440 441 if (value instanceof Time ) { 442 ts = (Timestamp ) value; 443 } else if (value instanceof java.util.Date ) { 444 ts = new Timestamp (((java.util.Date ) value).getTime()); 445 } else { 446 ts = Timestamp.valueOf(value.toString()); 447 } 448 449 statement.setTimestamp(position, ts); 450 break; 451 case Types.ARRAY: 452 statement.setArray(position, (Array ) value); break; 455 case Types.STRUCT: 456 case Types.OTHER: 458 statement.setObject(position, value); 460 break; 461 case Types.LONGVARBINARY: 462 statement.setTimestamp(position, new Timestamp ((new java.util.Date ()).getTime())); 464 break; 465 case Types.VARCHAR: 466 statement.setString(position, value.toString()); 468 break; 469 case Types.BLOB: 470 if (value instanceof JDBCxlobHelper) { 472 statement.setBinaryStream(position, ((JDBCxlobHelper)value).inputStream, ((JDBCxlobHelper)value).length); 473 } else if (value instanceof Source){ 474 statement.setBinaryStream(position, ((Source)value).getInputStream(), (int)((Source)value).getContentLength()); 475 } else { 476 Blob blob = null; 477 if (value instanceof Blob ) { 478 blob = (Blob ) value; 479 } else if( value instanceof File ) { 480 file = (File )value; 481 blob = new BlobHelper(new FileInputStream (file), (int) file.length()); 482 } else if (value instanceof String ) { 483 file = new File ((String )value); 484 blob = new BlobHelper(new FileInputStream (file), (int) file.length()); 485 } else if (value instanceof Part) { 486 Part anyFile = (Part) value; 487 blob = new BlobHelper(new BufferedInputStream (anyFile.getInputStream()), anyFile.getSize()); 488 } else { 489 throw new SQLException ("Invalid type for blob: "+value.getClass().getName()); 490 } 491 statement.setBlob(position, blob); 493 } 494 break; 495 case Types.VARBINARY: 496 if (value instanceof JDBCxlobHelper) { 498 statement.setBinaryStream(position, ((JDBCxlobHelper)value).inputStream, ((JDBCxlobHelper)value).length); 499 } else if (value instanceof Source){ 500 statement.setBinaryStream(position, ((Source)value).getInputStream(), (int)((Source)value).getContentLength()); 501 } else if (value instanceof Part) { 502 statement.setBinaryStream(position, ((Part)value).getInputStream(), ((Part)value).getSize()); 503 } else { 504 if (value instanceof File ) { 505 file = (File )value; 506 } else if (value instanceof String ) { 507 file = new File ((String )value); 508 } else { 509 throw new SQLException ("Invalid type for blob: "+value.getClass().getName()); 510 } 511 FileInputStream input = new FileInputStream (file); 513 statement.setBinaryStream(position, input, (int)file.length()); 514 } 515 break; 516 case Types.INTEGER: 517 Integer i = null; 519 if (value instanceof Integer ) { 520 i = (Integer ) value; 521 } else if (value instanceof Number ) { 522 i = new Integer (((Number ) value).intValue()); 523 } else { 524 i = new Integer (value.toString()); 525 } 526 statement.setInt(position, i.intValue()); 527 break; 528 case Types.BIT: 529 Boolean bo = null; 531 if (value instanceof Boolean ) { 532 bo = (Boolean )value; 533 } else if (value instanceof Number ) { 534 bo = BooleanUtils.toBooleanObject(((Number ) value).intValue()==1); 535 } else { 536 bo = BooleanUtils.toBooleanObject(value.toString()); 537 } 538 statement.setBoolean(position, bo.booleanValue()); 539 break; 540 541 default: 542 throw new SQLException ("Impossible exception - invalid type "); 544 } 545 } 547 } 548 | Popular Tags |