| 1 19 20 package org.relique.jdbc.csv; 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.Map ; 32 33 38 public class CsvResultSet implements ResultSet { 39 40 41 protected ResultSetMetaData resultSetMetaData; 42 43 44 protected Statement statement; 45 46 47 protected CsvReader reader; 48 49 50 protected String tableName; 51 52 53 protected String [] columnNames; 54 55 protected Map columnTypes; 56 protected String [] whereColumnNames; 57 58 protected String [] whereColumnValues; 59 60 61 62 protected int lastIndexRead = -1; 63 64 65 protected InputStream is; 66 67 75 87 95 protected CsvResultSet(Statement statement, 96 CsvReader reader, 97 String tableName, 98 String [] columnNames, 99 String [] columnWhereNames, 100 String [] columnWhereValues, 101 Map columnTypes) { 102 this.statement = statement; 103 this.reader = reader; 104 this.tableName = tableName; 105 this.columnNames = columnNames; 106 this.whereColumnNames = columnWhereNames; 107 this.whereColumnValues = columnWhereValues; 108 this.columnTypes = columnTypes; 109 if(columnNames[0].equals("*")) { 110 this.columnNames = reader.getColumnNames(); 111 } 112 } 113 114 130 public boolean next() throws SQLException { 131 mainLoop: 132 while(reader.next()) { 133 boolean isRow = true; 134 out: 135 for (int i = 0; i < this.whereColumnNames.length; i++) { 136 if ( !( Utils.compareValues( reader.getColumn(this.whereColumnNames[i]), this.whereColumnValues[i]) ) ) { 140 isRow = false; 141 break out; 142 } 143 144 } 149 if (isRow == true) 150 return true; 151 } 152 return false; 153 } 154 155 170 public void close() throws SQLException { 171 reader.close(); 172 } 173 174 186 public boolean wasNull() throws SQLException { 187 if(lastIndexRead >= 0) { 188 return getString(lastIndexRead) == null; 189 } else { 190 throw new SQLException("No previous getter method called"); 191 } 192 } 193 194 198 208 public String getString(int columnIndex) throws SQLException { 209 preAccessor(columnIndex); 211 if (columnIndex < 1 || columnIndex > columnNames.length) { 213 throw new SQLException("Column not found: invalid index: "+columnIndex); 214 } 215 String retValue = reader.getColumn(columnNames[columnIndex-1]); 216 return retValue; 219 } 220 221 231 public boolean getBoolean(int columnIndex) throws SQLException { 232 String str = getString(columnIndex); 233 return (str == null) ? false : Boolean.valueOf(str).booleanValue(); 234 } 235 236 246 public byte getByte(int columnIndex) throws SQLException { 247 String str = getString(columnIndex); 248 return (str == null) ? 0 : Byte.parseByte(str); 249 } 250 251 261 public short getShort(int columnIndex) throws SQLException { 262 String str = getString(columnIndex); 263 return (str == null) ? 0 : Short.parseShort(str); 264 } 265 266 276 public int getInt(int columnIndex) throws SQLException { 277 String str = getString(columnIndex); 278 return (str == null) ? 0 : Integer.parseInt(str); 279 } 280 281 291 public long getLong(int columnIndex) throws SQLException { 292 String str = getString(columnIndex); 293 return (str == null) ? 0L : Long.parseLong(str); 294 } 295 296 306 public float getFloat(int columnIndex) throws SQLException { 307 String str = getString(columnIndex); 308 return (str == null) ? 0F : Float.parseFloat(str); 309 } 310 311 321 public double getDouble(int columnIndex) throws SQLException { 322 String str = getString(columnIndex); 323 return (str == null) ? 0D : Double.parseDouble(str); 324 } 325 326 338 public BigDecimal getBigDecimal(int columnIndex, int scale) 339 throws SQLException { 340 return getBigDecimal(columnIndex); 342 } 343 344 355 public byte[] getBytes(int columnIndex) throws SQLException { 356 String str = getString(columnIndex); 357 return (str == null || str.equals("")) ? null : Utils.hexStringToBytes(str); 358 } 359 360 370 public Date getDate(int columnIndex) throws SQLException { 371 String str = getString(columnIndex); 372 return (str == null) ? null : Date.valueOf(str); 373 } 374 375 385 public Time getTime(int columnIndex) throws SQLException { 386 String str = getString(columnIndex); 387 return (str == null) ? null : Time.valueOf(str); 388 } 389 390 400 public Timestamp getTimestamp(int columnIndex) throws SQLException { 401 String str = getString(columnIndex); 402 return (str == null) ? null : Timestamp.valueOf(str); 403 } 404 405 427 public InputStream getAsciiStream(int columnIndex) throws SQLException { 428 String str = getString(columnIndex); 429 is = new ByteArrayInputStream (str.getBytes()); 430 return (str == null) ? null : is; 431 } 432 433 462 public InputStream getUnicodeStream(int columnIndex) throws SQLException { 463 return getAsciiStream(columnIndex); 465 } 466 467 488 public InputStream getBinaryStream(int columnIndex) throws SQLException { 489 return getAsciiStream(columnIndex); 491 } 492 493 497 507 public String getString(String columnName) throws SQLException { 508 preAccessor(columnName); 510 String retValue = reader.getColumn(columnName); 512 return retValue; 515 } 516 517 527 public boolean getBoolean(String columnName) throws SQLException { 528 String str = getString(columnName); 529 return (str == null) ? false : Boolean.valueOf(str).booleanValue(); 530 } 531 532 542 public byte getByte(String columnName) throws SQLException { 543 String str = getString(columnName); 544 return (str == null) ? 0 : Byte.parseByte(str); 545 } 546 547 557 public short getShort(String columnName) throws SQLException { 558 String str = getString(columnName); 559 return (str == null) ? 0 : Short.parseShort(str); 560 } 561 562 572 public int getInt(String columnName) throws SQLException { 573 String str = getString(columnName); 574 return (str == null) ? 0 : Integer.parseInt(str); 575 } 576 577 587 public long getLong(String columnName) throws SQLException { 588 String str = getString(columnName); 589 return (str == null) ? 0L : Long.parseLong(str); 590 } 591 592 602 public float getFloat(String columnName) throws SQLException { 603 String str = getString(columnName); 604 return (str == null) ? 0F : Float.parseFloat(str); 605 } 606 607 617 public double getDouble(String columnName) throws SQLException { 618 String str = getString(columnName); 619 return (str == null) ? 0D : Double.parseDouble(str); 620 } 621 622 634 public BigDecimal getBigDecimal(String columnName, int scale) 635 throws SQLException { 636 return getBigDecimal(columnName); 638 } 639 640 651 public byte[] getBytes(String columnName) throws SQLException { 652 String str = getString(columnName); 653 return (str == null) ? null : Utils.hexStringToBytes(str); 654 } 655 656 666 public Date getDate(String columnName) throws SQLException { 667 String str = getString(columnName); 668 return (str == null) ? null : Date.valueOf(str); 669 } 670 671 682 public Time getTime(String columnName) throws SQLException { 683 String str = getString(columnName); 684 return (str == null) ? null : Time.valueOf(str); 685 } 686 687 697 public Timestamp getTimestamp(String columnName) throws SQLException { 698 String str = getString(columnName); 699 return (str == null) ? null : Timestamp.valueOf(str); 700 } 701 702 724 public InputStream getAsciiStream(String columnName) throws SQLException { 725 String str = getString(columnName); 726 is = new ByteArrayInputStream (str.getBytes()); 727 return (str == null) ? null : is; 728 } 729 730 757 public InputStream getUnicodeStream(String columnName) throws SQLException { 758 return getAsciiStream(columnName); 760 } 761 762 783 public InputStream getBinaryStream(String columnName) throws SQLException { 784 return getAsciiStream(columnName); 786 } 787 788 792 815 public SQLWarning getWarnings() throws SQLException { 816 throw new UnsupportedOperationException ( 817 "ResultSet.getWarnings() unsupported"); 818 } 819 820 828 public void clearWarnings() throws SQLException { 829 throw new UnsupportedOperationException ( 830 "ResultSet.clearWarnings() unsupported"); 831 } 832 833 856 public String getCursorName() throws SQLException { 857 throw new UnsupportedOperationException ( 858 "ResultSet.getCursorName() unsupported"); 859 } 860 861 868 public ResultSetMetaData getMetaData() throws SQLException { 869 if (resultSetMetaData == null) { 870 resultSetMetaData = new CsvResultSetMetaData(tableName,columnNames,columnTypes); 871 } 872 return resultSetMetaData; 873 } 874 875 901 public Object getObject(int columnIndex) throws SQLException { 902 return getString(columnIndex); 905 } 906 |