1 60 61 package org.apache.commons.dbutils.wrappers; 62 63 import java.io.InputStream ; 64 import java.io.Reader ; 65 import java.lang.reflect.InvocationHandler ; 66 import java.lang.reflect.Method ; 67 import java.math.BigDecimal ; 68 import java.net.URL ; 69 import java.sql.Blob ; 70 import java.sql.Clob ; 71 import java.sql.Date ; 72 import java.sql.Ref ; 73 import java.sql.ResultSet ; 74 import java.sql.Time ; 75 import java.sql.Timestamp ; 76 import java.util.HashMap ; 77 import java.util.Map ; 78 79 import org.apache.commons.dbutils.ProxyFactory; 80 81 118 public class SqlNullCheckedResultSet implements InvocationHandler { 119 120 124 private static final Map nullMethods = new HashMap (); 125 126 static { 127 Method [] methods = SqlNullCheckedResultSet.class.getMethods(); 128 for (int i = 0; i < methods.length; i++) { 129 String methodName = methods[i].getName(); 130 131 if (methodName.startsWith("getNull")) { 132 String normalName = "get" + methodName.substring(7); 133 nullMethods.put(normalName, methods[i]); 134 } 135 } 136 } 137 138 141 private static final ProxyFactory factory = ProxyFactory.instance(); 142 143 152 public static ResultSet wrap(ResultSet rs) { 153 return factory.createResultSet(new SqlNullCheckedResultSet(rs)); 154 } 155 156 private InputStream nullAsciiStream = null; 157 private BigDecimal nullBigDecimal = null; 158 private InputStream nullBinaryStream = null; 159 private Blob nullBlob = null; 160 private boolean nullBoolean = false; 161 private byte nullByte = 0; 162 private byte[] nullBytes = null; 163 private Reader nullCharacterStream = null; 164 private Clob nullClob = null; 165 private Date nullDate = null; 166 private double nullDouble = 0.0; 167 private float nullFloat = 0.0f; 168 private int nullInt = 0; 169 private long nullLong = 0; 170 private Object nullObject = null; 171 private Ref nullRef = null; 172 private short nullShort = 0; 173 private String nullString = null; 174 private Time nullTime = null; 175 private Timestamp nullTimestamp = null; 176 private URL nullURL = null; 177 178 181 private final ResultSet rs; 182 183 188 public SqlNullCheckedResultSet(ResultSet rs) { 189 super(); 190 this.rs = rs; 191 } 192 193 199 public InputStream getNullAsciiStream() { 200 return this.nullAsciiStream; 201 } 202 203 209 public BigDecimal getNullBigDecimal() { 210 return this.nullBigDecimal; 211 } 212 213 219 public InputStream getNullBinaryStream() { 220 return this.nullBinaryStream; 221 } 222 223 229 public Blob getNullBlob() { 230 return this.nullBlob; 231 } 232 233 239 public boolean getNullBoolean() { 240 return this.nullBoolean; 241 } 242 243 249 public byte getNullByte() { 250 return this.nullByte; 251 } 252 253 259 public byte[] getNullBytes() { 260 return this.nullBytes; 261 } 262 263 269 public Reader getNullCharacterStream() { 270 return this.nullCharacterStream; 271 } 272 273 279 public Clob getNullClob() { 280 return this.nullClob; 281 } 282 283 289 public Date getNullDate() { 290 return this.nullDate; 291 } 292 293 299 public double getNullDouble() { 300 return this.nullDouble; 301 } 302 303 309 public float getNullFloat() { 310 return this.nullFloat; 311 } 312 313 319 public int getNullInt() { 320 return this.nullInt; 321 } 322 323 329 public long getNullLong() { 330 return this.nullLong; 331 } 332 333 339 public Object getNullObject() { 340 return this.nullObject; 341 } 342 343 349 public Ref getNullRef() { 350 return this.nullRef; 351 } 352 353 359 public short getNullShort() { 360 return this.nullShort; 361 } 362 363 369 public String getNullString() { 370 return this.nullString; 371 } 372 373 379 public Time getNullTime() { 380 return this.nullTime; 381 } 382 383 389 public Timestamp getNullTimestamp() { 390 return this.nullTimestamp; 391 } 392 393 399 public URL getNullURL() { 400 return this.nullURL; 401 } 402 403 411 public Object invoke(Object proxy, Method method, Object [] args) 412 throws Throwable { 413 414 Object result = method.invoke(this.rs, args); 415 416 Method nullMethod = (Method ) nullMethods.get(method.getName()); 417 418 return (nullMethod != null && this.rs.wasNull()) 421 ? nullMethod.invoke(this, null) 422 : result; 423 } 424 425 431 public void setNullAsciiStream(InputStream nullAsciiStream) { 432 this.nullAsciiStream = nullAsciiStream; 433 } 434 435 441 public void setNullBigDecimal(BigDecimal nullBigDecimal) { 442 this.nullBigDecimal = nullBigDecimal; 443 } 444 445 451 public void setNullBinaryStream(InputStream nullBinaryStream) { 452 this.nullBinaryStream = nullBinaryStream; 453 } 454 455 461 public void setNullBlob(Blob nullBlob) { 462 this.nullBlob = nullBlob; 463 } 464 465 471 public void setNullBoolean(boolean nullBoolean) { 472 this.nullBoolean = nullBoolean; 473 } 474 475 481 public void setNullByte(byte nullByte) { 482 this.nullByte = nullByte; 483 } 484 485 491 public void setNullBytes(byte[] nullBytes) { 492 this.nullBytes = nullBytes; 493 } 494 495 501 public void setNullCharacterStream(Reader nullCharacterStream) { 502 this.nullCharacterStream = nullCharacterStream; 503 } 504 505 511 public void setNullClob(Clob nullClob) { 512 this.nullClob = nullClob; 513 } 514 515 521 public void setNullDate(Date nullDate) { 522 this.nullDate = nullDate; 523 } 524 525 531 public void setNullDouble(double nullDouble) { 532 this.nullDouble = nullDouble; 533 } 534 535 541 public void setNullFloat(float nullFloat) { 542 this.nullFloat = nullFloat; 543 } 544 545 551 public void setNullInt(int nullInt) { 552 this.nullInt = nullInt; 553 } 554 555 561 public void setNullLong(long nullLong) { 562 this.nullLong = nullLong; 563 } 564 565 571 public void setNullObject(Object nullObject) { 572 this.nullObject = nullObject; 573 } 574 575 581 public void setNullRef(Ref nullRef) { 582 this.nullRef = nullRef; 583 } 584 585 591 public void setNullShort(short nullShort) { 592 this.nullShort = nullShort; 593 } 594 595 601 public void setNullString(String nullString) { 602 this.nullString = nullString; 603 } 604 605 611 public void setNullTime(Time nullTime) { 612 this.nullTime = nullTime; 613 } 614 615 621 public void setNullTimestamp(Timestamp nullTimestamp) { 622 this.nullTimestamp = nullTimestamp; 623 } 624 625 631 public void setNullURL(URL nullURL) { 632 this.nullURL = nullURL; 633 } 634 635 } 636 | Popular Tags |