| 1 19 20 package org.webdocwf.util.i18njdbc; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.Reader ; 26 import java.io.StringReader ; 27 import java.math.BigDecimal ; 28 import java.net.URL ; 29 import java.sql.*; 30 import java.util.Calendar ; 31 import java.util.Enumeration ; 32 import java.util.Map ; 33 34 40 public class I18nResultSet implements ResultSet { 41 42 43 protected ResultSetMetaData resultSetMetaData; 44 45 46 protected I18nProperties properties; 47 48 49 protected String tableName; 50 51 52 protected String [] columnNames; 53 54 protected String [] columnTypes; 55 56 protected String [] whereColumnNames; 57 58 protected String [] whereColumnValues; 59 60 61 protected int lastIndexRead = -1; 62 63 64 protected InputStream is; 65 66 protected Enumeration en; 67 68 private String currKey = ""; 69 70 protected I18nConnection connection; 71 72 73 82 protected I18nResultSet(I18nStatement statement, 83 String tableName, 84 String [] columnNames, 85 String [] columnWhereNames, 86 String [] columnWhereValues) throws SQLException { 87 88 try { 89 this.connection = (I18nConnection) statement.getConnection(); 90 } catch (SQLException e) { 91 throw e; 92 } 93 this.tableName = tableName; 94 this.columnNames = columnNames; 95 this.whereColumnNames = columnWhereNames; 96 this.whereColumnValues = columnWhereValues; 97 this.properties = statement.getProperties(); 98 this.en = statement.getProperties().keys(); 99 100 if (columnNames[0].equals("*")) { 101 this.columnNames = this.connection.getColumnNames(); 102 } 103 } 104 113 protected I18nResultSet(I18nPreparedStatement statement, 114 String tableName, 115 String [] columnNames, 116 String [] columnWhereNames, 117 String [] columnWhereValues) throws SQLException { 118 try { 119 this.connection = (I18nConnection) statement.getConnection(); 120 } catch (SQLException e) { 121 throw e; 122 } 123 this.tableName = tableName; 124 this.columnNames = columnNames; 125 this.whereColumnNames = columnWhereNames; 126 this.whereColumnValues = columnWhereValues; 127 this.properties = statement.getProperties(); 128 this.en = statement.getProperties().keys(); 129 130 if (columnNames[0].equals("*")) { 131 this.columnNames = this.connection.getColumnNames(); 132 } 133 } 134 150 public boolean next() throws SQLException { 151 152 boolean retVal = false; 153 mainLoop : while (en.hasMoreElements()) { 154 this.currKey = en.nextElement().toString(); 155 retVal = true; 156 out : for (int i = 0; i < this.whereColumnNames.length; i++) { 157 if (whereColumnValues[i] == null || whereColumnValues[i].equals("null")) 158 whereColumnValues[i] = ""; 159 if (this.whereColumnNames[i].equalsIgnoreCase(this.connection.getNameColumn())) { 160 if (!this.currKey.equals(this.whereColumnValues[i])) { 161 retVal = false; 162 break out; 163 } 164 } else if (this.whereColumnNames[i].equalsIgnoreCase(this.connection.getValueColumn())) { 165 if (!(properties.getProperty(this.currKey).equals(this.whereColumnValues[i]))) { 166 retVal = false; 167 break out; 168 } 169 } 170 } 171 if (retVal == true) 172 return true; 173 } 174 return false; 175 } 176 177 193 public void close() throws SQLException { 194 } 196 197 209 public boolean wasNull() throws SQLException { 210 if (lastIndexRead >= 0) { 211 return getString(lastIndexRead) == null; 212 } else { 213 throw new SQLException("No previous getter method called"); 214 } 215 } 216 217 221 231 public String getString(int columnIndex) throws SQLException { 232 String retValue = ""; 234 preAccessor(columnIndex); 235 if (columnIndex < 1 || columnIndex > columnNames.length) { 236 throw new SQLException("Column not found: invalid index: " + columnIndex); 237 } else if (columnIndex >= 1) { 238 String colName = this.connection.getNameColumn(); 240 String colValue = this.connection.getValueColumn(); 241 String column = columnNames[columnIndex - 1]; 242 if (column.equalsIgnoreCase(colName)) { 243 retValue = this.currKey; 244 } else if (column.equalsIgnoreCase(colValue)) { 245 retValue = properties.getProperty(this.currKey); 246 } 247 248 } 249 return retValue; 250 251 } 252 253 263 public boolean getBoolean(int columnIndex) throws SQLException { 264 String str = getString(columnIndex); 265 return (str == null) ? false : Boolean.valueOf(str).booleanValue(); 266 } 267 268 278 public byte getByte(int columnIndex) throws SQLException { 279 String str = getString(columnIndex); 280 return (str == null) ? 0 : Byte.parseByte(str); 281 } 282 283 293 public short getShort(int columnIndex) throws SQLException { 294 String str = getString(columnIndex); 295 return (str == null) ? 0 : Short.parseShort(str); 296 } 297 298 308 public int getInt(int columnIndex) throws SQLException { 309 String str = getString(columnIndex); 310 return (str == null) ? 0 : Integer.parseInt(str); 311 } 312 313 323 public long getLong(int columnIndex) throws SQLException { 324 String str = getString(columnIndex); 325 return (str == null) ? 0L : Long.parseLong(str); 326 } 327 328 338 public float getFloat(int columnIndex) throws SQLException { 339 String str = getString(columnIndex); 340 return (str == null) ? 0F : Float.parseFloat(str); 341 } 342 343 353 public double getDouble(int columnIndex) throws SQLException { 354 String str = getString(columnIndex); 355 return (str == null) ? 0D : Double.parseDouble(str); 356 } 357 358 370 public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException { 371 return getBigDecimal(columnIndex); 373 } 374 375 386 public byte[] getBytes(int columnIndex) throws SQLException { 387 String str = getString(columnIndex); 388 return (str == null || str.equals("")) ? null : Utils.hexStringToBytes(str); 389 } 390 391 401 public Date getDate(int columnIndex) throws SQLException { 402 String str = getString(columnIndex); 403 return (str == null) ? null : Date.valueOf(str); 404 } 405 406 416 public Time getTime(int columnIndex) throws SQLException { 417 String str = getString(columnIndex); 418 return (str == null) ? null : Time.valueOf(str); 419 } 420 421 431 public Timestamp getTimestamp(int columnIndex) throws SQLException { 432 String str = getString(columnIndex); 433 return (str == null) ? null : Timestamp.valueOf(str); 434 } 435 436 458 public InputStream getAsciiStream(int columnIndex) throws SQLException { 459 String str = getString(columnIndex); 460 is = new ByteArrayInputStream (str.getBytes()); 461 return (str == null) ? null : is; 462 } 463 464 493 public InputStream getUnicodeStream(int columnIndex) throws SQLException { 494 return getAsciiStream(columnIndex); 496 } 497 498 519 public InputStream getBinaryStream(int columnIndex) throws SQLException { 520 return getAsciiStream(columnIndex); 522 } 523 524 528 538 public String getString(String columnName) throws SQLException { 539 preAccessor(columnName); 541 String colName = this.connection.getNameColumn(); 542 String colValue = this.connection.getValueColumn(); 543 String retValue = ""; 544 if (columnName.equalsIgnoreCase(colName)) { 545 retValue = this.currKey; 546 } else if (columnName.equalsIgnoreCase(colValue)) { 547 retValue = properties.getProperty(this.currKey); 548 } 549 return retValue; 550 } 551 552 562 public boolean getBoolean(String columnName) throws SQLException { 563 String str = getString(columnName); 564 return (str == null) ? false : Boolean.valueOf(str).booleanValue(); 565 } 566 567 577 public byte getByte(String columnName) throws SQLException { 578 String str = getString(columnName); 579 return (str == null) ? 0 : Byte.parseByte(str); 580 } 581 582 592 public short getShort(String columnName) throws SQLException { 593 String str = getString(columnName); 594 return (str == null) ? 0 : Short.parseShort(str); 595 } 596 597 607 public int getInt(String columnName) throws SQLException { 608 String str = getString(columnName); 609 return (str == null) ? 0 : Integer.parseInt(str); 610 } 611 612 622 public long getLong(String columnName) throws SQLException { 623 String str = getString(columnName); 624 return (str == null) ? 0L : Long.parseLong(str); 625 } 626 627 637 public float getFloat(String columnName) throws SQLException { 638 String str = getString(columnName); 639 return (str == null) ? 0F : Float.parseFloat(str); 640 } 641 642 652 public double getDouble(String columnName) throws SQLException { 653 String str = getString(columnName); 654 return (str == null) ? 0D : Double.parseDouble(str); 655 } 656 657 669 public BigDecimal getBigDecimal(String columnName, int scale) throws SQLException { 670 return getBigDecimal(columnName); 672 } 673 674 685 public byte[] getBytes(String columnName) throws SQLException { 686 String str = getString(columnName); 687 return (str == null) ? null : Utils.hexStringToBytes(str); 688 } 689 690 700 public Date getDate(String columnName) throws SQLException { 701 String str = getString(columnName); 702 return (str == null) ? null : Date.valueOf(str); 703 } 704 705 716 public Time getTime(String columnName) throws SQLException { 717 String str = getString(columnName); 718 return (str == null) ? null : Time.valueOf(str); 719 } 720 721 731 public Timestamp getTimestamp(String columnName) throws SQLException { 732 String str = getString(columnName); 733 return (str == null) ? null : Timestamp.valueOf(str); 734 } 735 736 758 public InputStream getAsciiStream(String columnName) throws SQLException { 759 String str = getString(columnName); 760 is = new ByteArrayInputStream (str.getBytes()); 761 return (str == null) ? null : is; 762 } 763 764 791 public InputStream getUnicodeStream(String columnName) throws SQLException { 792 return getAsciiStream(columnName); 794 } 795 796 817 public InputStream getBinaryStream(String columnName) throws SQLException { 818 return getAsciiStream(columnName); 820 } 821 822 826 849 public SQLWarning getWarnings() throws SQLException { 850 throw new UnsupportedOperationException ("ResultSet.getWarnings() unsupported"); 851 } 852 853 861 public void clearWarnings() throws SQLException { 862 throw new UnsupportedOperationException ("ResultSet.clearWarnings() unsupported"); 863 } 864 865 |