| 1 18 19 package org.webdocwf.util.xml; 20 21 import java.io.ByteArrayInputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.Reader ; 25 import java.io.StringReader ; 26 import java.math.BigDecimal ; 27 import java.net.URL ; 28 import java.sql.*; 29 import java.util.ArrayList ; 30 import java.util.Map ; 31 import java.util.Calendar ; 32 33 38 public class XmlResultSet implements ResultSet { 39 40 41 protected ResultSetMetaData resultSetMetaData; 42 43 44 protected Statement statement; 45 46 47 protected XmlReader reader; 48 49 50 protected String tableName; 51 52 53 protected String [] columnNames; 54 55 56 protected String [] columnValues; 57 58 protected String [] whereColumnNames; 59 60 protected String [] whereColumnValues; 61 62 63 64 protected int lastIndexRead = -1; 65 66 67 protected InputStream is; 68 69 79 protected XmlResultSet(Statement statement, XmlReader reader , String tableName , String [] columnNames 80 , String [] whereColumnNames , String [] whereColumnValues ) { 81 this.statement = statement; 82 this.reader = reader; 83 this.tableName = tableName; 84 this.columnNames = columnNames; 85 this.whereColumnNames = whereColumnNames; 86 this.whereColumnValues = whereColumnValues; 87 } 88 89 90 public void select() throws SQLException { 91 reader.select( tableName , columnNames , whereColumnNames , whereColumnValues); 92 this.rset = reader.getResultSet(); 93 } 94 95 public void selectTableNames() throws SQLException { 96 reader.selectTableNames(); 97 this.rset = reader.getResultSet(); 98 } 99 public void close() { 100 } 101 102 private int index = 0; 103 private ArrayList rset = new ArrayList (); 104 111 public boolean next() throws SQLException { 112 boolean retVal = false; 113 try { 114 if( !(rset.size() <= index) ) { 115 this.columnValues = (String [])rset.get( index ); 116 index++; 117 retVal = true; 118 } else { 119 index = 0; 120 rset = new ArrayList (); 121 this.columnValues = new String [0]; 122 retVal = false; 123 } 124 }catch( Exception e ) { throw new SQLException("Error in ResultSet.next() : "+e.getMessage()); } 125 return retVal; 126 } 127 128 public String getString(int i) throws SQLException { 129 try { 130 return this.columnValues[i-1].toString(); 131 }catch(Exception e) { throw new SQLException("Error ResultSet.getString( index ) : "+e.getMessage()); } 132 } 133 134 146 public boolean wasNull() throws SQLException { 147 if(lastIndexRead >= 0) { 148 return getString(lastIndexRead) == null; 149 } else { 150 throw new SQLException("No previous getter method called"); 151 } 152 } 153 154 158 159 169 public boolean getBoolean(int columnIndex) throws SQLException { 170 String str = getString(columnIndex); 171 return (str == null) ? false : Boolean.valueOf(str).booleanValue(); 172 } 173 174 184 public byte getByte(int columnIndex) throws SQLException { 185 String str = getString(columnIndex); 186 return (str == null) ? 0 : Byte.parseByte(str); 187 } 188 189 199 public short getShort(int columnIndex) throws SQLException { 200 String str = getString(columnIndex); 201 return (str == null) ? 0 : Short.parseShort(str); 202 } 203 204 214 public int getInt(int columnIndex) throws SQLException { 215 String str = getString(columnIndex); 216 return (str == null) ? 0 : Integer.parseInt(str); 217 } 218 219 229 public long getLong(int columnIndex) throws SQLException { 230 String str = getString(columnIndex); 231 return (str == null) ? 0L : Long.parseLong(str); 232 } 233 234 244 public float getFloat(int columnIndex) throws SQLException { 245 String str = getString(columnIndex); 246 return (str == null) ? 0F : Float.parseFloat(str); 247 } 248 249 259 public double getDouble(int columnIndex) throws SQLException { 260 String str = getString(columnIndex); 261 return (str == null) ? 0D : Double.parseDouble(str); 262 } 263 264 276 public BigDecimal getBigDecimal(int columnIndex, int scale) 277 throws SQLException { 278 return getBigDecimal(columnIndex); 280 } 281 282 293 public byte[] getBytes(int columnIndex) throws SQLException { 294 String str = getString(columnIndex); 295 return (str == null) ? null : Utils.hexStringToBytes(str); 296 } 297 298 308 public Date getDate(int columnIndex) throws SQLException { 309 String str = getString(columnIndex); 310 return (str == null) ? null : Date.valueOf(str); 311 } 312 313 323 public Time getTime(int columnIndex) throws SQLException { 324 String str = getString(columnIndex); 325 return (str == null) ? null : Time.valueOf(str); 326 } 327 328 338 public Timestamp getTimestamp(int columnIndex) throws SQLException { 339 String str = getString(columnIndex); 340 return (str == null) ? null : Timestamp.valueOf(str); 341 } 342 343 365 public InputStream getAsciiStream(int columnIndex) throws SQLException { 366 String str = getString(columnIndex); 367 is = new ByteArrayInputStream (str.getBytes()); 368 return (str == null) ? null : is; 369 } 370 371 400 public InputStream getUnicodeStream(int columnIndex) throws SQLException { 401 return getAsciiStream(columnIndex); 403 } 404 405 426 public InputStream getBinaryStream(int columnIndex) throws SQLException { 427 return getAsciiStream(columnIndex); 429 } 430 431 435 445 public String getString(String columnName) throws SQLException { 446 int colIndex = -1; 447 for( int i = 0; i < this.columnNames.length; i++ ) { 448 if(columnName.equalsIgnoreCase( this.columnNames[i] )) 449 colIndex = i; 450 } 451 if( colIndex == -1 ) 452 throw new SQLException("Column "+columnName+" not found."); 453 return this.columnValues[colIndex]; 454 } 455 456 466 public boolean getBoolean(String columnName) throws SQLException { 467 String str = getString(columnName); 468 return (str == null) ? false : Boolean.valueOf(str).booleanValue(); 469 } 470 471 481 public byte getByte(String columnName) throws SQLException { 482 String str = getString(columnName); 483 return (str == null) ? 0 : Byte.parseByte(str); 484 } 485 486 496 public short getShort(String columnName) throws SQLException { 497 String str = getString(columnName); 498 return (str == null) ? 0 : Short.parseShort(str); 499 } 500 501 511 public int getInt(String columnName) throws SQLException { 512 String str = getString(columnName); 513 return (str == null) ? 0 : Integer.parseInt(str); 514 } 515 516 526 public long getLong(String columnName) throws SQLException { 527 String str = getString(columnName); 528 return (str == null) ? 0L : Long.parseLong(str); 529 } 530 531 541 public float getFloat(String columnName) throws SQLException { 542 String str = getString(columnName); 543 return (str == null) ? 0F : Float.parseFloat(str); 544 } 545 546 556 public double getDouble(String columnName) throws SQLException { 557 String str = getString(columnName); 558 return (str == null) ? 0D : Double.parseDouble(str); 559 } 560 561 573 public BigDecimal getBigDecimal(String columnName, int scale) 574 throws SQLException { 575 return getBigDecimal(columnName); 577 } 578 579 590 public byte[] getBytes(String columnName) throws SQLException { 591 String str = getString(columnName); 592 return (str == null) ? null : str.getBytes(); 593 } 594 595 605 public Date getDate(String columnName) throws SQLException { 606 String str = getString(columnName); 607 return (str == null) ? null : Date.valueOf(str); 608 } 609 610 621 public Time getTime(String columnName) throws SQLException { 622 String str = getString(columnName); 623 return (str == null) ? null : Time.valueOf(str); 624 } 625 626 636 public Timestamp getTimestamp(String columnName) throws SQLException { 637 String str = getString(columnName); 638 return (str == null) ? null : Timestamp.valueOf(str); 639 } 640 641 663 public InputStream getAsciiStream(String columnName) throws SQLException { 664 String str = getString(columnName); 665 is = new ByteArrayInputStream (str.getBytes()); 666 return (str == null) ? null : is; 667 } 668 669 696 public InputStream getUnicodeStream(String columnName) throws SQLException { 697 return getAsciiStream(columnName); 699 } 700 701 722 public InputStream getBinaryStream(String columnName) throws SQLException { 723 return getAsciiStream(columnName); 725 } 726 727 731 754 public SQLWarning getWarnings() throws SQLException { 755 throw new UnsupportedOperationException ( 756 "ResultSet.getWarnings() unsupported"); 757 } 758 759 767 public void clearWarnings() throws SQLException { 768 throw new UnsupportedOperationException ( 769 "ResultSet.clearWarnings() unsupported"); 770 } 771 772 795 public String getCursorName() throws SQLException { 796 throw new UnsupportedOperationException ( 797 "ResultSet.getCursorName() unsupported"); 798 } 799 800 807 public ResultSetMetaData getMetaData() throws SQLException { 808 if (resultSetMetaData == null) { 809 resultSetMetaData = new XmlResultSetMetaData(tableName, columnNames); 810 } 811 return resultSetMetaData; 812 813 } 814 815 841 public Object getObject(int columnIndex) throws SQLException { 842 return getString(columnIndex); 845 } 846 872 public Object getObject(String columnName) throws SQLException { 873 return getString(columnName); 876 } 877 878 887 public int findColumn(String columnName) throws SQLException { 888 throw new UnsupportedOperationException ( 889 "ResultSet.findColumn(String) unsupported"); 890 } 891 892 894 898 909 public Reader getCharacterStream(int columnIndex) throws SQLException { 910 String str = getString(columnIndex); 911 return (str == null) ? null : new StringReader (str); 912 } 913 914 925 public Reader getCharacterStream(String columnName) throws SQLException { 926 String str = getString(columnName); 927 return (str == null) ? null : new StringReader (str); 928 } 929 930 941 public BigDecimal getBigDecimal(int columnIndex) throws SQLException { 942 BigDecimal retval = null; 943 String str = getString(columnIndex); 944 if(str != null) { 945 try { 946 retval = new BigDecimal (str); 947 } 948 catch (NumberFormatException e) { 949 throw new SQLException("Could not convert '" + str + "' to " + 950 "a java.math.BigDecimal object"); 951 } 952 } 953 return retval; 954 } 955 956 967 public BigDecimal getBigDecimal(String columnName) throws SQLException { 968 BigDecimal retval = null; 969 String str = getString(columnName); 970 if(str != null) { 971 try { 972 retval = new BigDecimal (str); 973 } 974 catch (NumberFormatException e) { 975 throw new SQLException("Could not convert '" + str + "' to " + 976 "a java.math.BigDecimal object"); 977 } 978 } 979 return retval; 980 } 981 982 986 995 public boolean isBeforeFirst() throws SQLException { 996 throw new UnsupportedOperationException ( 997 "ResultSet.isBeforeFirst() unsupported"); 998 } 999 1000 1009public boolean isAfterLast() throws SQLException { 1010 throw new UnsupportedOperationException ( 1011 "ResultSet.isAfterLast() unsupported"); 1012} 1013 1014 1022public boolean isFirst() throws SQLException { 1023 throw new UnsupportedOperationException ( 1024 "ResultSet.isFirst() unsupported"); 1025} 1026 1027 1039public boolean isLast() throws SQLException { 1040 throw new UnsupportedOperationException ( 1041 "ResultSet.isLast() unsupported"); 1042} 1043 1044 |