1 32 33 package com.knowgate.dataobjs; 34 35 import java.io.FileNotFoundException ; 36 import java.io.UnsupportedEncodingException ; 37 import java.io.IOException ; 38 import java.io.OutputStream ; 39 import java.io.PrintWriter ; 40 41 import java.sql.Connection ; 42 import java.sql.ResultSet ; 43 import java.sql.ResultSetMetaData ; 44 import java.sql.Statement ; 45 import java.sql.CallableStatement ; 46 import java.sql.PreparedStatement ; 47 import java.sql.SQLException ; 48 import java.sql.Time ; 49 import java.sql.Timestamp ; 50 import java.sql.Types ; 51 52 import java.math.BigDecimal ; 53 54 import java.text.DecimalFormat ; 55 import java.text.SimpleDateFormat ; 56 57 import java.util.StringTokenizer ; 58 import java.util.Vector ; 59 import java.util.Date ; 60 import java.util.List ; 61 import java.util.Map ; 62 import java.util.HashMap ; 63 64 import com.knowgate.debug.DebugFile; 65 import com.knowgate.jdc.JDCConnection; 66 import com.knowgate.misc.Gadgets; 67 import com.knowgate.misc.CSVParser; 68 import com.knowgate.math.Money; 69 70 83 84 public final class DBSubset { 85 86 98 99 public DBSubset (String sTableName, String sColumnList, String sFilterClause, int iFetchSize) { 100 if (DebugFile.trace) 101 DebugFile.writeln ("new DBSubset(" + sTableName + "," + sColumnList + "," + sFilterClause + "," + String.valueOf(iFetchSize)+")"); 102 103 sTable = sTableName; 104 sColList = sColumnList; 105 106 if (null!=sFilterClause) { 107 sFilter = sFilterClause; 108 109 if (sFilter.length()>0) 110 sSelect = "SELECT " + sColList + " FROM " + sTable + " WHERE " + sFilter; 111 else 112 sSelect = "SELECT " + sColList + " FROM " + sTable; 113 } 114 else { 115 sFilter = ""; 116 sSelect = "SELECT " + sColList + " FROM " + sTable; 117 } 118 119 if (DebugFile.trace) DebugFile.writeln (sSelect); 120 121 oResults = null; 122 sInsert = ""; 123 iFetch = iFetchSize; 124 iColCount = 0; 125 iMaxRows = -1; 126 iTimeOut = 60; 127 bEOF = true; 128 sColDelim = "`"; 129 sRowDelim = "�"; 130 sTxtQualifier = "\""; 131 oShortDate = null; 132 } 133 134 136 143 public int clear(Connection oConn, Object [] aFilterValues) throws SQLException  144 { 145 int iAffected=0; 146 PreparedStatement oStmt; 147 148 if (DebugFile.trace) 149 { 150 DebugFile.writeln("Begin DBSubset.clear([Connection], Object[])"); 151 DebugFile.incIdent(); 152 } 153 154 156 if (sFilter.length()>0) 157 oStmt = oConn.prepareStatement("DELETE FROM " + sTable + " WHERE " + sFilter); 158 else 159 oStmt = oConn.prepareStatement("DELETE FROM " + sTable); 160 161 try { oStmt.setQueryTimeout(iTimeOut); } catch (SQLException sqle) { if (DebugFile.trace) DebugFile.writeln("Error at PreparedStatement.setQueryTimeout(" + String.valueOf(iTimeOut) + ")" + sqle.getMessage()); } 162 163 for (int c=0; c<aFilterValues.length; c++) 164 oStmt.setObject(c+1, aFilterValues[c]); 165 166 iAffected = oStmt.executeUpdate(); 167 oStmt.close(); 168 169 171 oResults = null; 172 173 if (DebugFile.trace) 174 { 175 DebugFile.decIdent(); 176 DebugFile.writeln("End DBSubset.clear()"); 177 } 178 179 return iAffected; 180 } 182 184 191 public boolean eof() { 192 return bEOF; 193 } 194 195 197 200 public int getMaxRows() { 201 return iMaxRows; 202 } 203 204 206 215 216 public void setMaxRows(int iMax) { 217 iMaxRows = iMax; 218 } 219 220 222 225 226 public int getQueryTimeout() { 227 return iTimeOut; 228 } 229 230 232 236 public void setQueryTimeout(int iMaxSeconds) { 237 iTimeOut = iMaxSeconds; 238 } 239 240 242 private void setFetchSize(JDCConnection oConn, ResultSet oRSet) 243 throws SQLException { 244 245 if (DebugFile.trace) { 246 DebugFile.writeln("Begin DBSubset.setFetchSize()"); 247 DebugFile.incIdent(); 248 } 249 250 if (oConn.getDataBaseProduct()==JDCConnection.DBMS_POSTGRESQL) 251 iFetch = 1; 253 254 else { 255 256 try { 257 if (0!=iFetch) 258 oRSet.setFetchSize (iFetch); 259 else 260 iFetch = oRSet.getFetchSize(); 261 } 262 catch (SQLException e) { 263 if (DebugFile.trace) DebugFile.writeln(e.getMessage()); 264 iFetch = 1; 265 } 266 } 267 268 if (DebugFile.trace) { 269 DebugFile.decIdent(); 270 DebugFile.writeln("End DBSubset.setFetchSize() : " + iFetch); 271 } 272 } 274 276 private int fetchResultSet (ResultSet oRSet, int iSkip) 277 throws SQLException , ArrayIndexOutOfBoundsException  278 { 279 Vector oRow; 280 int iCol; 281 int iRetVal = 0; 282 int iMaxRow = iMaxRows<0 ? 2147483647 : iMaxRows; 283 long lFetchTime = 0; 284 Object oFieldValue; 285 286 if (DebugFile.trace) { 287 DebugFile.writeln("Begin DBSubset.fetchResultSet([ResultSet], " + String.valueOf(iSkip) + ")"); 288 DebugFile.incIdent(); 289 DebugFile.writeln("column count = " + String.valueOf(iColCount)); 290 DebugFile.writeln("max. rows = " + String.valueOf(iMaxRows)); 291 DebugFile.writeln("new Vector(" + String.valueOf(iFetch) + "," + String.valueOf(iFetch) + ")"); 292 lFetchTime = System.currentTimeMillis(); 293 } 294 295 oResults = new Vector (iFetch, iFetch); 296 297 if (0!=iSkip) { 298 oRSet.next(); 299 300 if (DebugFile.trace) DebugFile.writeln("ResultSet.relative(" + String.valueOf(iSkip-1) + ")"); 301 302 oRSet.relative (iSkip-1); 303 } 305 boolean bHasNext = oRSet.next(); 306 307 while (bHasNext && iRetVal<iMaxRow) { 308 309 iRetVal++; 310 oRow = new Vector (iColCount); 311 312 for (iCol=1; iCol<=iColCount; iCol++) { 313 oFieldValue = oRSet.getObject(iCol); 314 if (oRSet.wasNull()) 315 oRow.add (null); 316 else 317 oRow.add (oFieldValue); 318 } 320 oResults.add(oRow); 321 322 bHasNext = oRSet.next(); 323 } 325 if (0==iRetVal || iRetVal<iMaxRow) { 326 bEOF = true; 327 if (DebugFile.trace) DebugFile.writeln("readed " + String.valueOf(iRetVal) + " rows eof() = true"); 328 } 329 else { 330 bEOF = !bHasNext; 331 332 if (DebugFile.trace) DebugFile.writeln("readed max " + String.valueOf(iMaxRow) + " rows eof() = " + String.valueOf(bEOF)); 333 } 334 335 if (DebugFile.trace) { 336 DebugFile.writeln("fetching done in " + String.valueOf(System.currentTimeMillis()-lFetchTime) + " ms"); 337 DebugFile.decIdent(); 338 DebugFile.writeln("End DBSubset.fetchResultSet() : " + String.valueOf(iRetVal)); 339 } 340 341 return iRetVal; 342 } 344 346 352 353 public int call (JDCConnection oConn) throws SQLException { 354 return call(oConn,0); 355 } 356 357 359 368 369 public int call (JDCConnection oConn, int iSkip) 370 throws SQLException , IllegalArgumentException , ArrayIndexOutOfBoundsException  371 { 372 CallableStatement oStmt; 373 ResultSet oRSet; 374 ResultSetMetaData oMDat; 375 int iRows = 0; 376 int iType = (iSkip==0 ? ResultSet.TYPE_FORWARD_ONLY : ResultSet.TYPE_SCROLL_INSENSITIVE); 377 378 if (DebugFile.trace) 379 { 380 DebugFile.writeln("Begin DBSubset.call([Connection]," + iSkip + ")"); 381 DebugFile.incIdent(); 382 } 383 384 386 if (DebugFile.trace) DebugFile.writeln("Connection.prepareCall({call " + sTable + "()}"); 387 oStmt = oConn.prepareCall("{call " + sTable + "()}", iType, ResultSet.CONCUR_READ_ONLY); 388 389 if (DebugFile.trace) DebugFile.writeln("Connection.executeQuery(" + sTable + ")"); 390 391 oRSet = oStmt.executeQuery(); 392 393 oMDat = oRSet.getMetaData(); 394 iColCount = oMDat.getColumnCount(); 395 ColNames = new String [iColCount]; 396 397 for (int c=1; c<=iColCount; c++) { 398 ColNames[c-1] = oMDat.getColumnName(c).toLowerCase(); 399 } 400 oMDat = null; 401 402 setFetchSize(oConn, oRSet); 403 404 iRows = fetchResultSet(oRSet,iSkip); 405 406 oRSet.close(); 407 oRSet = null; 408 409 oStmt.close(); 410 oStmt = null; 411 412 414 if (DebugFile.trace) 415 { 416 DebugFile.decIdent(); 417 DebugFile.writeln("End DBSubset.call()"); 418 } 419 420 return iRows; 421 } 423 425 432 433 public int call (JDCConnection oConn, Object [] aFilterValues) throws SQLException { 434 return call(oConn, aFilterValues, 0); 435 } 436 437 439 451 452 public int call (JDCConnection oConn, Object [] aFilterValues, int iSkip) 453 throws SQLException , IllegalArgumentException , ArrayIndexOutOfBoundsException  454 { 455 CallableStatement oStmt; 456 ResultSet oRSet; 457 ResultSetMetaData oMDat; 458 459 int iRows = 0; 460 int iType = (iSkip==0 ? ResultSet.TYPE_FORWARD_ONLY : ResultSet.TYPE_SCROLL_INSENSITIVE); 461 462 if (DebugFile.trace) 463 { 464 DebugFile.writeln("Begin DBSubset.call([Connection], Object[]," + iSkip + ")"); 465 DebugFile.incIdent(); 466 } 467 468 if (DebugFile.trace) DebugFile.writeln("Connection.prepareCall({call " + sTable + "()}"); 470 oStmt = oConn.prepareCall("{call " + sTable + "()}", iType, ResultSet.CONCUR_READ_ONLY); 471 472 for (int p=0; p<aFilterValues.length; p++) 473 oStmt.setObject(p+1, aFilterValues[p]); 474 475 if (DebugFile.trace) DebugFile.writeln("Connection.executeQuery()"); 476 477 oRSet = oStmt.executeQuery(); 478 479 oMDat = oRSet.getMetaData(); 480 iColCount = oMDat.getColumnCount(); 481 ColNames = new String [iColCount]; 482 483 for (int c=1; c<=iColCount; c++) { 484 ColNames[c-1] = oMDat.getColumnName(c).toLowerCase(); 485 } 486 oMDat = null; 487 488 setFetchSize(oConn, oRSet); 489 490 iRows = fetchResultSet(oRSet, iSkip); 491 492 oRSet.close(); 493 oRSet = null; 494 495 oStmt.close(); 496 oStmt = null; 497 498 500 if (DebugFile.trace) 501 { 502 DebugFile.decIdent(); 503 DebugFile.writeln("End DBSubset.call()"); 504 } 505 506 return iRows; 507 } 509 511 519 520 public int load (JDCConnection oConn) 521 throws SQLException , ArrayIndexOutOfBoundsException , NullPointerException { 522 return load(oConn, 0); 523 } 524 525 527 543 544 public int load (JDCConnection oConn, int iSkip) 545 throws SQLException , IllegalArgumentException , 546 ArrayIndexOutOfBoundsException , NullPointerException { 547 548 Statement oStmt = null; 549 ResultSet oRSet = null; 550 ResultSetMetaData oMDat; 551 int iRows = 0; 552 int iType = (iSkip==0 ? ResultSet.TYPE_FORWARD_ONLY : ResultSet.TYPE_SCROLL_INSENSITIVE); 553 long lQueryTime = 0; 554 555 if (DebugFile.trace) 556 { 557 DebugFile.writeln("Begin DBSubset.load([Connection]," + iSkip + ")"); 558 lQueryTime = System.currentTimeMillis(); 559 } 560 561 if (iSkip<0) 562 throw new IllegalArgumentException ("row offset must be equal to or greater than zero"); 563 564 if (null==oConn) 565 throw new NullPointerException ("DBSubset.load() JDCConnection parameter is null"); 566 567 if (DebugFile.trace) 568 DebugFile.incIdent(); 569 570 try { 571 572 oStmt = oConn.createStatement (iType, ResultSet.CONCUR_READ_ONLY); 573 574 if (iMaxRows>0) { 575 576 switch (oConn.getDataBaseProduct()) { 577 578 case JDCConnection.DBMS_MSSQL: 579 if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(" + sSelect + " OPTION (FAST " + String.valueOf(iMaxRows) + ")" + ")"); 580 oRSet = oStmt.executeQuery(sSelect + " OPTION (FAST " + String.valueOf(iMaxRows) + ")"); 581 break; 582 583 case JDCConnection.DBMS_MYSQL: 584 if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(" + sSelect + " LIMIT " + String.valueOf(iSkip) + "," + String.valueOf(iMaxRows+2) + ")"); 585 oRSet = oStmt.executeQuery(sSelect + " LIMIT " + String.valueOf(iSkip) + "," + String.valueOf(iMaxRows+2)); 586 iSkip = 0; break; 588 589 case JDCConnection.DBMS_POSTGRESQL: 590 if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(" + sSelect + " LIMIT " + String.valueOf(iMaxRows+2) + " OFFSET " + String.valueOf(iSkip) + ")"); 591 oRSet = oStmt.executeQuery(sSelect + " LIMIT " + String.valueOf(iMaxRows+2) + " OFFSET " + String.valueOf(iSkip)); 592 iSkip = 0; break; 594 595 default: 596 if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(" + sSelect + ")"); 597 oRSet = oStmt.executeQuery(sSelect); 598 } } 600 else { 601 switch (oConn.getDataBaseProduct()) { 602 603 case JDCConnection.DBMS_MYSQL: 604 if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(" + sSelect + " LIMIT " + String.valueOf(iSkip) + ",2147483647)"); 605 oRSet = oStmt.executeQuery(sSelect + " LIMIT " + String.valueOf(iSkip) + ",2147483647" ); 606 iSkip = 0; break; 608 609 case JDCConnection.DBMS_POSTGRESQL: 610 if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(" + sSelect + " OFFSET " + String.valueOf(iSkip) + ")"); 611 oRSet = oStmt.executeQuery(sSelect + " OFFSET " + String.valueOf(iSkip)); 612 iSkip = 0; break; 614 615 default: 616 if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(" + sSelect + ")"); 617 oRSet = oStmt.executeQuery(sSelect); 618 } } 620 621 if (DebugFile.trace) { 622 DebugFile.writeln("query executed in " + String.valueOf(System.currentTimeMillis()-lQueryTime) + " ms"); 623 DebugFile.writeln("ResultSet.getMetaData()"); 624 } 625 626 oMDat = oRSet.getMetaData(); 627 iColCount = oMDat.getColumnCount(); 628 ColNames = new String [iColCount]; 629 630 for (int c=1; c<=iColCount; c++) { 631 ColNames[c-1] = oMDat.getColumnName(c).toLowerCase(); 632 } 633 634 oMDat = null; 635 636 setFetchSize(oConn, oRSet); 637 638 iRows = fetchResultSet(oRSet,iSkip); 639 640 if (DebugFile.trace) DebugFile.writeln("ResultSet.close()"); 641 642 oRSet.close(); 643 oRSet = null; 644 645 if (DebugFile.trace) DebugFile.writeln("PreparedStatement.close()"); 646 647 oStmt.close(); 648 oStmt = null; 649 650 } 651 catch (SQLException sqle) { 652 try { if (null!=oRSet) oRSet.close(); 653 } catch (Exception logit) { if (DebugFile.trace) DebugFile.writeln(logit.getClass().getName()+" "+logit.getMessage()); } 654 try { if (null!=oStmt) oStmt.close(); 655 } catch (Exception logit) { if (DebugFile.trace) DebugFile.writeln(logit.getClass().getName()+" "+logit.getMessage()); } 656 throw new SQLException (sqle.getMessage(), sqle.getSQLState(), sqle.getErrorCode()); 657 } 658 catch (ArrayIndexOutOfBoundsException aiob) { 659 try { if (null!=oRSet) oRSet.close(); 660 } catch (Exception logit) { if (DebugFile.trace) DebugFile.writeln(logit.getClass().getName()+" "+logit.getMessage()); } 661 try { if (null!=oStmt) oStmt.close(); 662 } catch (Exception logit) { if (DebugFile.trace) DebugFile.writeln(logit.getClass().getName()+" "+logit.getMessage()); } 663 throw new ArrayIndexOutOfBoundsException ("DBSubset.load() " + aiob.getMessage()); 664 } 665 catch (NullPointerException npe) { 666 try { if (null!=oRSet) oRSet.close(); 667 } catch (Exception logit) { if (DebugFile.trace) DebugFile.writeln(logit.getClass().getName()+" "+logit.getMessage()); } 668 try { if (null!=oStmt) oStmt.close(); 669 } catch (Exception logit) { if (DebugFile.trace) DebugFile.writeln(logit.getClass().getName()+" "+logit.getMessage()); } 670 throw new NullPointerException ("DBSubset.load()"); 671 } 672 673 if (DebugFile.trace) 674 { 675 DebugFile.decIdent(); 676 DebugFile.writeln("End DBSubset.load() : "+String.valueOf(iRows)); 677 } 678 679 return iRows; 680 } 682 684 693 694 public int load (JDCConnection oConn, Object [] aFilterValues) 695 throws SQLException , ArrayIndexOutOfBoundsException , NullPointerException { 696 697 return load(oConn, aFilterValues, 0); 698 } 699 700 715 716 718 public int load (JDCConnection oConn, Object [] aFilterValues, int iSkip) 719 throws SQLException , IllegalArgumentException , 720 ArrayIndexOutOfBoundsException , NullPointerException { 721 722 PreparedStatement oStmt = null; 723 ResultSet oRSet = null; 724 ResultSetMetaData oMDat; 725 726 int iRows = 0; 727 int iType = (iSkip==0 ? ResultSet.TYPE_FORWARD_ONLY : ResultSet.TYPE_SCROLL_INSENSITIVE); 728 long lQueryTime = 0; 729 730 if (DebugFile.trace) 731 DebugFile.writeln("Begin DBSubset.load([Connection], Object[]," + iSkip + ")"); 732 733 if (iSkip<0) 734 throw new IllegalArgumentException ("row offset must be equal to or greater than zero"); 735 736 if (null==oConn) 737 throw new NullPointerException ("DBSubset.load() JDCConnection parameter is null"); 738 739 if (DebugFile.trace) 740 DebugFile.incIdent(); 741 742 try { 743 744 if (iMaxRows>0) { 745 746 switch (oConn.getDataBaseProduct()) { 747 748 case JDCConnection.DBMS_MSSQL: 749 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(" + sSelect + " OPTION (FAST " + String.valueOf(iMaxRows) + ")" + ")"); 750 oStmt = oConn.prepareStatement(sSelect + " OPTION (FAST " + String.valueOf(iMaxRows) + ")", iType, ResultSet.CONCUR_READ_ONLY); 751 break; 752 753 case JDCConnection.DBMS_POSTGRESQL: 754 case JDCConnection.DBMS_MYSQL: 755 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(" + sSelect + " LIMIT " + String.valueOf(iMaxRows+2) + " OFFSET " + String.valueOf(iSkip) + ")"); 756 oStmt = oConn.prepareStatement(sSelect + " LIMIT " + String.valueOf(iMaxRows+2) + " OFFSET " + String.valueOf(iSkip), ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 757 iSkip = 0; break; 759 760 default: 761 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(" + sSelect + ")"); 762 oStmt = oConn.prepareStatement(sSelect, iType, ResultSet.CONCUR_READ_ONLY); 763 } } 765 766 else { 767 switch (oConn.getDataBaseProduct()) { 768 769 case JDCConnection.DBMS_POSTGRESQL: 770 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(" + sSelect + " OFFSET " + String.valueOf(iSkip) + ")"); 771 oStmt = oConn.prepareStatement(sSelect + " OFFSET " + String.valueOf(iSkip), ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 772 iSkip = 0; break; 774 775 default: 776 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(" + sSelect + ")"); 777 oStmt = oConn.prepareStatement(sSelect, iType, ResultSet.CONCUR_READ_ONLY); 778 } 780 } 782 try { oStmt.setQueryTimeout(iTimeOut); } catch (SQLException sqle) { if (DebugFile.trace) DebugFile.writeln("Error at PreparedStatement.setQueryTimeout(" + String.valueOf(iTimeOut) + ")" + sqle.getMessage()); } 783 784 for (int p=0; p<aFilterValues.length; p++) { 785 Object oParam = aFilterValues[p]; 786 if (DebugFile.trace) { 787 if (null==oParam) 788 DebugFile.writeln("PreparedStatement.setObject("+String.valueOf(p+1)+",null)"); 789 else 790 DebugFile.writeln("PreparedStatement.setObject("+String.valueOf(p+1)+","+oParam.toString()+")"); 791 } 792 oStmt.setObject(p+1, oParam); 793 } 794 795 if (DebugFile.trace) { 796 DebugFile.writeln("PreparedStatement.executeQuery()"); 797 lQueryTime = System.currentTimeMillis(); 798 } 799 800 oRSet = oStmt.executeQuery(); 801 802 if (DebugFile.trace) { 803 DebugFile.writeln("query executed in " + String.valueOf(System.currentTimeMillis()-lQueryTime) + " ms"); 804 DebugFile.writeln("ResultSet.getMetaData()"); 805 } 806 807 oMDat = oRSet.getMetaData(); 808 iColCount = oMDat.getColumnCount(); 809 ColNames = new String [iColCount]; 810 811 for (int c=1; c<=iColCount; c++) { 812 ColNames[c-1] = oMDat.getColumnName(c).toLowerCase(); 813 } 814 oMDat = null; 815 816 setFetchSize(oConn, oRSet); 817 818 iRows = fetchResultSet(oRSet, iSkip); 819 820 if (DebugFile.trace) DebugFile.writeln("ResultSet.close()"); 821 822 oRSet.close(); 823 oRSet = null; 824 825 if (DebugFile.trace) DebugFile.writeln("PreparedStatement.close()"); 826 827 oStmt.close(); 828 oStmt = null; 829 } 830 catch (SQLException sqle) { 831 try { if (null!=oRSet) oRSet.close(); 832 } catch (Exception logit) { if (DebugFile.trace) DebugFile.writeln(logit.getClass().getName()+" "+logit.getMessage()); } 833 try { if (null!=oStmt) oStmt.close(); 834 } catch (Exception logit) { if (DebugFile.trace) DebugFile.writeln(logit.getClass().getName()+" "+logit.getMessage()); } 835 throw new SQLException (sqle.getMessage(), sqle.getSQLState(), sqle.getErrorCode()); 836 } 837 catch (ArrayIndexOutOfBoundsException aiob) { 838 try { if (null!=oRSet) oRSet.close(); 839 } catch (Exception logit) { if (DebugFile.trace) DebugFile.writeln(logit.getClass().getName()+" "+logit.getMessage()); } 840 try { if (null!=oStmt) oStmt.close(); 841 } catch (Exception logit) { if (DebugFile.trace) DebugFile.writeln(logit.getClass().getName()+" "+logit.getMessage()); } 842 throw new ArrayIndexOutOfBoundsException ("DBSubset.load() " + aiob.getMessage()); 843 } 844 catch (NullPointerException npe) { 845 try { if (null!=oRSet) oRSet.close(); 846 } catch (Exception logit) { if (DebugFile.trace) DebugFile.writeln(logit.getClass().getName()+" "+logit.getMessage()); } 847 try { if (null!=oStmt) oStmt.close(); 848 } catch (Exception logit) { if (DebugFile.trace) DebugFile.writeln(logit.getClass().getName()+" "+logit.getMessage()); } 849 throw new NullPointerException ("DBSubset.load()"); 850 } 851 852 if (DebugFile.trace) 853 { 854 DebugFile.decIdent(); 855 DebugFile.writeln("End DBSubset.load() : "+String.valueOf(iRows)); 856 } 857 858 return iRows; 859 } 861 863 872 873 public int find (int iCol, Object oVal) { 874 int iFound = -1; 875 int iRowCount; 876 Object objCol; 877 878 if (DebugFile.trace) { 879 if (null==oVal) 880 DebugFile.writeln("Begin DBSubset.find(" + String.valueOf(iCol)+ ", null)"); 881 else 882 DebugFile.writeln("Begin DBSubset.find(" + String.valueOf(iCol)+ ", " + oVal.toString() + ")"); 883 884 DebugFile.incIdent(); 885 } 886 887 if (oResults!=null) 888 iRowCount = oResults.size(); 889 else 890 iRowCount = -1; 891 892 if (DebugFile.trace) 893 DebugFile.writeln("row count is " + String.valueOf(iRowCount)); 894 895 for (int iRow=0; iRow<iRowCount; iRow++) { 896 897 objCol = get(iCol,iRow); 898 899 if (null!=objCol) { 900 if (null!=oVal) { 901 if (objCol.equals(oVal)) { 902 iFound = iRow; 903 break; 904 } 905 } 906 } 907 else if (null==oVal) { 908 iFound = iRow; 909 break; 910 } 912 } 914 if (DebugFile.trace) { 915 DebugFile.decIdent(); 916 DebugFile.writeln("Begin DBSubset.find() : " + String.valueOf(iFound)); 917 } 918 919 return iFound; 920 } 922 924 927 public String getColumnDelimiter() { 928 return sColDelim; 929 } 930 931 933 937 public void setColumnDelimiter(String sDelim) { 938 sColDelim=sDelim; 939 } 940 941 943 946 public String getRowDelimiter() { 947 return sRowDelim; 948 } 949 950 952 956 957 public void setRowDelimiter(String sDelim) { 958 sRowDelim=sDelim; 959 } 960 961 963 966 public String getTextQualifier() { 967 return sTxtQualifier; 968 } 969 970 972 975 public void setTextQualifier(String sQualifier) { 976 sTxtQualifier=sQualifier; 977 } 978 979 981 984 public int getColumnCount() { 985 return iColCount; 986 } 987 988 990 995 public String [] getColumnNames() { 996 return ColNames; 997 } 998 999 1001 1005 public int getColumnPosition(String sColumnName) { 1006 int iColPos = -1; 1007 1008 for (int iCol=0; iCol<iColCount; iCol++) { 1009 if (sColumnName.equalsIgnoreCase(ColNames[iCol])) { 1010 iColPos = iCol; 1011 break; 1012 } 1013 } 1015 return iColPos; 1016 } 1018 1020 1023 public int getRowCount() { 1024 int iRows; 1025 1026 if (null==oResults) 1027 iRows = 0; 1028 else 1029 iRows = oResults.size(); 1030 1031 return iRows; 1032 1033 } 1035 1037 1044 public List getColumnAsList (int iCol) 1045 throws ArrayIndexOutOfBoundsException ,IllegalStateException { 1046 Vector oRow, oCol; 1047 int iRowCount; 1048 if (oResults==null) 1049 throw new IllegalStateException ("DBSubset.getColumnAsList("+String.valueOf(iCol)+") DBSubset not loaded"); 1050 else 1051 iRowCount = oResults.size(); 1052 if (0==iRowCount) { 1053 oCol = new Vector (); 1054 } else { 1055 oCol = new Vector (iRowCount); 1056 for (int iRow=0; iRow<iRowCount; iRow++) { 1057 oRow = (Vector ) oResults.get(iRow); 1058 oCol.add(oRow.get(iCol)); 1059 } } 1061 return oCol; 1062 } 1064 1066 1073 public List getRowAsList (int iRow) 1074 throws ArrayIndexOutOfBoundsException ,IllegalStateException { 1075 if (oResults!=null) 1076 return (List ) oResults.get(iRow); 1077 else 1078 throw new IllegalStateException ("DBSubset.getRowAsList("+String.valueOf(iRow)+") DBSubset not loaded"); 1079 } 1081 1083 1090 public Map getRowAsMap (int iRow) 1091 throws ArrayIndexOutOfBoundsException ,IllegalStateException { 1092 if (oResults==null) 1093 throw new IllegalStateException ("DBSubset.getRowAsMap("+String.valueOf(iRow)+") DBSubset not loaded"); 1094 1095 Vector oRow = (Vector ) oResults.get(iRow); 1096 HashMap oRetVal = new HashMap (iColCount*2); 1097 1098 for (int iCol=0; iCol<iColCount; iCol++) { 1099 oRetVal.put(ColNames[iCol], oRow.get(iCol)); 1100 } 1102 return oRetVal; 1103 } 1105 1107 1115 public Vector getRowAsVector (int iRow) 1116 throws ArrayIndexOutOfBoundsException ,IllegalStateException { 1117 if (oResults!=null) 1118 return (Vector ) oResults.get(iRow); 1119 else 1120 throw new IllegalStateException ("DBSubset.getRowAsList("+String.valueOf(iRow)+") DBSubset not loaded"); 1121 } 1123 1125 1131 public Object get (int iCol, int iRow) throws ArrayIndexOutOfBoundsException { 1132 return ((Vector ) oResults.get(iRow)).get(iCol); 1133 } 1134 1135 1137 1143 public Object get (String sCol, int iRow) 1144 throws ArrayIndexOutOfBoundsException { 1145 1146 int iCol = getColumnPosition(sCol); 1147 1148 if (iCol==-1) 1149 throw new ArrayIndexOutOfBoundsException ("Column " + sCol + " not found"); 1150 1151 return ((Vector ) oResults.get(iRow)).get(iCol); 1152 } 1153 1154 1156 1165 public boolean getBoolean (int iCol, int iRow) 1166 throws ClassCastException ,ArrayIndexOutOfBoundsException ,NullPointerException { 1167 1168 boolean bRetVal; 1169 Object oObj = get(iCol, iRow); 1170 1171 if (oObj.getClass().equals(Integer.TYPE)) 1172 1173 bRetVal = (((Integer )oObj).intValue()!=0 ? true : false); 1174 1175 else if (oObj.getClass().equals(Short.TYPE)) 1176 bRetVal = (((Short )oObj).shortValue()!=(short)0 ? true : false); 1177 1178 else 1179 bRetVal = ((Boolean ) get(iCol, iRow)).booleanValue(); 1180 1181 return bRetVal; 1182 } 1183 1184 1186 1193 1194 public java.util.Date getDate(int iCol, int iRow) 1195 throws ClassCastException ,ArrayIndexOutOfBoundsException { 1196 Object oDt = ((Vector ) oResults.get(iRow)).get(iCol); 1197 1198 if (null!=oDt) { 1199 if (oDt.getClass().equals(ClassUtilDate)) 1200 return (java.util.Date ) oDt; 1201 else if (oDt.getClass().equals(ClassTimestamp)) 1202 return new java.util.Date (((java.sql.Timestamp ) oDt).getTime()); 1203 else if (oDt.getClass().equals(ClassSQLDate)) 1204 return new java.util.Date (((java.sql.Date ) oDt).getYear(), ((java.sql.Date ) oDt).getMonth(), ((java.sql.Date ) oDt).getDate()); 1205 else 1206 throw new ClassCastException ("Cannot cast " + oDt.getClass().getName() + " to Date"); 1207 } 1208 else 1209 return null; 1210 1211 } 1213 1215 1224 1225 public java.sql.Date getSQLDate(int iCol, int iRow) 1226 throws ClassCastException ,ArrayIndexOutOfBoundsException { 1227 Object oDt = ((Vector ) oResults.get(iRow)).get(iCol); 1228 1229 if (null!=oDt) { 1230 if (oDt.getClass().equals(ClassSQLDate)) 1231 return (java.sql.Date ) oDt; 1232 else if (oDt.getClass().equals(ClassTimestamp)) 1233 return new java.sql.Date (((java.sql.Timestamp ) oDt).getTime()); 1234 else if (oDt.getClass().equals(ClassUtilDate)) 1235 return new java.sql.Date (((java.util.Date ) oDt).getTime()); 1236 else 1237 throw new ClassCastException ("Cannot cast " + oDt.getClass().getName() + " to Date"); 1238 } 1239 else 1240 return null; 1241 } 1243 1252 public java.sql.Date getSQLDate (String sCol, int iRow) throws ArrayIndexOutOfBoundsException { 1253 int iCol = getColumnPosition(sCol); 1254 1255 if (iCol==-1) 1256 throw new ArrayIndexOutOfBoundsException ("Column " + sCol + " not found"); 1257 1258 return getSQLDate(iCol, iRow); 1259 } 1261 1263 1271 1272 public java.sql.Time getSQLTime(int iCol, int iRow) 1273 throws ClassCastException ,ArrayIndexOutOfBoundsException { 1274 Object oDt = ((Vector ) oResults.get(iRow)).get(iCol); 1275 1276 if (null!=oDt) { 1277 if (oDt.getClass().equals(ClassSQLTime)) 1278 return (java.sql.Time ) oDt; 1279 else if (oDt.getClass().equals(ClassTimestamp)) 1280 return new java.sql.Time (((java.sql.Timestamp ) oDt).getTime()); 1281 else if (oDt.getClass().equals(ClassUtilDate)) 1282 return new java.sql.Time (((java.util.Date ) oDt).getTime()); 1283 else 1284 throw new ClassCastException ("Cannot cast " + oDt.getClass().getName() + " to Time"); 1285 } 1286 else 1287 return null; 1288 } 1290 1292 1301 public java.sql.Time getSQLTime (String sCol, int iRow) 1302 throws ClassCastException , ArrayIndexOutOfBoundsException { 1303 int iCol = getColumnPosition(sCol); 1304 1305 if (iCol==-1) 1306 throw new ArrayIndexOutOfBoundsException ("Column " + sCol + " not found"); 1307 1308 return getSQLTime(iCol, iRow); 1309 } 1311 1313 1320 1321 public java.util.Date getDate (String sCol, int iRow) throws ArrayIndexOutOfBoundsException { 1322 int iCol = getColumnPosition(sCol); 1323 1324 if (iCol==-1) 1325 throw new ArrayIndexOutOfBoundsException ("Column " + sCol + " not found"); 1326 1327 return getDate(iCol, iRow); 1328 } 1330 1332 1339 public String getDateShort(int iCol, int iRow) { 1340 java.util.Date oDt = getDate(iCol, iRow); 1341 1342 if (null==oShortDate) oShortDate = new SimpleDateFormat ("yyyy-MM-dd"); 1343 1344 if (null!=oDt) 1345 return oShortDate.format(oDt); 1346 else 1347 return null; 1348 1349 } 1351 1353 1361 1362 public String getDateTime24(int iCol, int iRow) { 1363 java.util.Date oDt = getDate(iCol, iRow); 1364 1365 SimpleDateFormat oDateTime24 = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); 1366 1367 if (null!=oDt) 1368 return oDateTime24.format(oDt); 1369 else 1370 return null; 1371 1372 } 1374 1376 1384 1385 public String getDateTime(int iCol, int iRow) 1386 throws ClassCastException , ArrayIndexOutOfBoundsException { 1387 1388 java.util.Date oDt = getDate(iCol, iRow); 1389 1390 if (null==oDateTime) oDateTime = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss"); 1391 1392 if (null!=oDt) 1393 return oDateTime.format(oDt); 1394 else 1395 return null; 1396 } 1398 1400 1410 1411 public String getDateFormated(int iCol, int iRow, String sFormat) 1412 throws ArrayIndexOutOfBoundsException , ClassCastException { 1413 1414 java.util.Date oDt = getDate(iCol, iRow); 1415 SimpleDateFormat oSimpleDate; 1416 1417 if (null!=oDt) { 1418 oSimpleDate = new SimpleDateFormat (sFormat); 1419 return oSimpleDate.format(oDt); 1420 } 1421 else 1422 return null; 1423 1424 } 1426 1428 1435 1436 public short getShort (int iCol, int iRow) 1437 throws NullPointerException ,ArrayIndexOutOfBoundsException { 1438 1439 Object oVal = (((Vector ) oResults.get(iRow)).get(iCol)); 1440 Class oCls; 1441 short iRetVal; 1442 1443 oCls = oVal.getClass(); 1444 1445 try { 1446 if (oCls.equals(Short.TYPE)) 1447 iRetVal = ((Short ) oVal).shortValue(); 1448 else if (oCls.equals(Integer.TYPE)) 1449 iRetVal = (short) ((Integer ) oVal).intValue(); 1450 else if (oCls.equals(Class.forName("java.math.BigDecimal"))) 1451 iRetVal = (short) ((java.math.BigDecimal ) oVal).intValue(); 1452 else if (oCls.equals(Float.TYPE)) 1453 iRetVal = (short) ((Float ) oVal).intValue(); 1454 else if (oCls.equals(Double.TYPE)) 1455 iRetVal = (short) ((Double ) oVal).intValue(); 1456 else 1457 iRetVal = new Short (oVal.toString()).shortValue(); 1458 } catch (ClassNotFoundException cnfe) { iRetVal = (short)0; } 1459 1460 return iRetVal; 1461 } 1463 1465 1472 1473 public int getInt (int iCol, int iRow) 1474 throws NullPointerException ,ArrayIndexOutOfBoundsException { 1475 1476 Object oVal = (((Vector ) oResults.get(iRow)).get(iCol)); 1477 1478 if (oVal.getClass().equals(Integer.TYPE)) 1479 return ((Integer )oVal).intValue(); 1480 else 1481 return getInteger(iCol, iRow).intValue(); 1482 } 1483 1484 1486 1493 1494 public int getInt (String sCol, int iRow) 1495 throws ArrayIndexOutOfBoundsException , NullPointerException { 1496 1497 int iCol = getColumnPosition(sCol); 1498 1499 if (iCol==-1) 1500 throw new ArrayIndexOutOfBoundsException ("Column " + sCol + " not found"); 1501 1502 Object oVal = (((Vector ) oResults.get(iRow)).get(iCol)); 1503 1504 if (oVal.getClass().equals(Integer.TYPE)) 1505 1506 return ((Integer )oVal).intValue(); 1507 1508 else 1509 1510 return getInteger(iCol, iRow).intValue(); 1511 1512 } 1514 1516 1523 1524 public double getDouble (int iCol, int iRow) 1525 throws NullPointerException ,ArrayIndexOutOfBoundsException { 1526 1527 Object oVal = (((Vector ) oResults.get(iRow)).get(iCol)); 1528 Class oCls; 1529 double dRetVal; 1530 1531 oCls = oVal.getClass(); 1532 1533 try { 1534 if (oCls.equals(Short.TYPE)) 1535 dRetVal = (double) ((Short ) oVal).shortValue(); 1536 else if (oCls.equals(Integer.TYPE)) 1537 dRetVal = (double) ((Integer ) oVal).intValue(); 1538 else if (oCls.equals(Class.forName("java.math.BigDecimal"))) 1539 dRetVal = ((java.math.BigDecimal ) oVal).doubleValue(); 1540 else if (oCls.equals(Float.TYPE)) 1541 dRetVal = ((Float ) oVal).doubleValue(); 1542 else if (oCls.equals(Double.TYPE)) 1543 dRetVal = ((Double ) oVal).doubleValue(); 1544 else 1545 dRetVal = new Double (Gadgets.removeChar(oVal.toString(),',')).doubleValue(); 1546 } catch (ClassNotFoundException cnfe) { dRetVal = 0d; } 1547 1548 return dRetVal; 1549 } 1551 1553 1560 1561 public float getFloat (int iCol, int iRow) 1562 throws NullPointerException ,ArrayIndexOutOfBoundsException { 1563 1564 Object oVal = (((Vector ) oResults.get(iRow)).get(iCol)); 1565 Class oCls; 1566 float fRetVal; 1567 1568 oCls = oVal.getClass(); 1569 1570 try { 1571 if (oCls.equals(Short.TYPE)) 1572 fRetVal = (float) ((Short ) oVal).shortValue(); 1573 else if (oCls.equals(Integer.TYPE)) 1574 fRetVal = (float) ((Integer ) oVal).intValue(); 1575 else if (oCls.equals(Class.forName("java.math.BigDecimal"))) 1576 fRetVal = ((java.math.BigDecimal ) oVal).floatValue(); 1577 else if (oCls.equals(Float.TYPE)) 1578 fRetVal = ((Float ) oVal).floatValue(); 1579 else if (oCls.equals(Double.TYPE)) 1580 fRetVal = ((Double ) oVal).floatValue(); 1581 else 1582 fRetVal = new Float (Gadgets.removeChar(oVal.toString(),',')).floatValue(); 1583 } catch (ClassNotFoundException cnfe) { fRetVal = 0f; } 1584 1585 return fRetVal; 1586 } 1588 1590 1597 1598 public float getFloat (String sCol, int iRow) 1599 throws NullPointerException ,ArrayIndexOutOfBoundsException { 1600 1601 int iCol = getColumnPosition(sCol); 1602 1603 if (iCol==-1) 1604 throw new ArrayIndexOutOfBoundsException ("Column " + sCol + " not found"); 1605 1606 return getFloat(iCol, iRow); 1607 } 1608 1609 1611 1619 1620 public float getFloat (int iCol, int iRow, int iDecimals) 1621 throws NullPointerException ,ArrayIndexOutOfBoundsException { 1622 1623 float p, f = getFloat (iCol, iRow); 1624 int i; 1625 1626 if (0==iDecimals) 1627 1628 return (float) ((int) f); 1629 1630 else { 1631 1632 p = 10f; 1633 for (int d=0; d<iDecimals; d++) p*=10; 1634 i = (int) (f * p); 1635 1636 return ((float)i) / p; 1637 } 1638 } 1640 1642 1650 1651 public float getFloat (String sCol, int iRow, int iDecimals) 1652 throws ArrayIndexOutOfBoundsException , NullPointerException { 1653 1654 int iCol = getColumnPosition(sCol); 1655 1656 if (iCol==-1) 1657 throw new ArrayIndexOutOfBoundsException ("Column " + sCol + " not found"); 1658 1659 return getFloat(iCol, iRow, iDecimals); 1660 } 1662 1664 1670 1671 public Integer getInteger (int iCol, int iRow) 1672 throws ArrayIndexOutOfBoundsException { 1673 1674 Object oVal = (((Vector ) oResults.get(iRow)).get(iCol)); 1675 Class oCls; 1676 Integer iRetVal; 1677 1678 if (null==oVal) return null; 1679 1680 oCls = oVal.getClass(); 1681 1682 try { 1683 if (oCls.equals(Short.TYPE)) 1684 iRetVal = new Integer (((Short ) oVal).intValue()); 1685 else if (oCls.equals(Integer.TYPE)) 1686 iRetVal = (Integer ) oVal; 1687 else if (oCls.equals(Class.forName("java.math.BigDecimal"))) 1688 iRetVal = new Integer (((java.math.BigDecimal ) oVal).intValue()); 1689 else if (oCls.equals(Float.TYPE)) 1690 iRetVal = new Integer (((Float ) oVal).intValue()); 1691 else if (oCls.equals(Double.TYPE)) 1692 iRetVal = new Integer (((Double ) oVal).intValue()); 1693 else 1694 iRetVal = new Integer (oVal.toString()); 1695 } catch (ClassNotFoundException cnfe) { iRetVal = null; } 1696 1697 return iRetVal; 1698 1699 } 1701 1703 1710 1711 public Integer getInteger (String sCol, int iRow) 1712 throws ArrayIndexOutOfBoundsException { 1713 1714 int iCol = getColumnPosition(sCol); 1715 1716 if (iCol==-1) 1717 throw new ArrayIndexOutOfBoundsException ("Column " + sCol + " not found"); 1718 1719 return getInteger(iCol, iRow); 1720 } 1721 1722 1724 1737 1738 public BigDecimal getDecimal (int iCol, int iRow) 1739 throws java.lang.ClassCastException , java.lang.NumberFormatException { 1740 Object oVal = (((Vector ) oResults.get(iRow)).get(iCol)); 1741 Class oCls; 1742 BigDecimal oDecVal; 1743 1744 if (oVal==null) return null; 1745 1746 oCls = oVal.getClass(); 1747 1748 if (oCls.equals(Short.TYPE)) 1749 oDecVal = new BigDecimal (((Short ) oVal).doubleValue()); 1750 else if (oCls.equals(Integer.TYPE)) 1751 oDecVal = new BigDecimal (((Integer ) oVal).doubleValue()); 1752 else if (oCls.equals(Float.TYPE)) 1753 oDecVal = new BigDecimal (((Float ) oVal).doubleValue()); 1754 else if (oCls.equals(Double.TYPE)) 1755 oDecVal = new BigDecimal (((Double ) oVal).doubleValue()); 1756 else if (oCls.getName().equalsIgnoreCase("java.lang.String")) 1757 oDecVal = new BigDecimal (Gadgets.removeChar((String ) oVal, ',')); 1758 else { 1759 try { 1760 oDecVal = (BigDecimal ) oVal; 1761 } catch (ClassCastException cce) { 1762 throw new ClassCastException ("Cannot cast column of type " + oVal.getClass().getName() + " to BigDecimal"); 1763 } 1764 } 1765 1766 return oDecVal; 1767 } 1769 1771 1778 public BigDecimal getDecimal (String sCol, int iRow) 1779 throws ArrayIndexOutOfBoundsException { 1780 1781 int iCol = getColumnPosition(sCol); 1782 1783 if (iCol==-1) 1784 throw new ArrayIndexOutOfBoundsException ("Column " + sCol + " not found"); 1785 1786 return getDecimal(iCol, iRow); 1787 } 1789 1791 1802 public String getDecimalFormated (int iCol, int iRow, String sPattern) 1803 throws java.lang.ClassCastException , java.lang.NumberFormatException , 1804 java.lang.NullPointerException , java.lang.IllegalArgumentException { 1805 BigDecimal oDecVal = getDecimal(iCol, iRow); 1806 1807 if (null==oDecVal) { 1808 return null; 1809 } else { 1810 if (oDecFmt==null) { 1811 oDecFmt = new DecimalFormat (sPattern); 1812 return oDecFmt.format(oDecVal.doubleValue()); 1813 } else { 1814 if (oDecFmt.toPattern().equals(sPattern)) { 1815 return oDecFmt.format(oDecVal.doubleValue()); 1816 } else { 1817 oDecFmt = new DecimalFormat (sPattern); 1818 return oDecFmt.format(oDecVal.doubleValue()); 1819 } 1820 } 1821 } 1822 } 1824 1826 1838 public String getDecimalFormated (String sCol, int iRow, String sPattern) 1839 throws java.lang.ClassCastException , java.lang.NumberFormatException , 1840 java.lang.NullPointerException , java.lang.IllegalArgumentException , 1841 java.lang.ArrayIndexOutOfBoundsException { 1842 1843 int iCol = getColumnPosition(sCol); 1844 1845 if (iCol==-1) 1846 throw new ArrayIndexOutOfBoundsException ("Column " + sCol + " not found"); 1847 1848 return getDecimalFormated(iCol, iRow, sPattern); 1849 } 1851 1853 1864 public Money getMoney(int iCol, int iRow) 1865 throws ArrayIndexOutOfBoundsException ,NumberFormatException { 1866 Object obj = (((Vector ) oResults.get(iRow)).get(iCol)); 1867 1868 if (null!=obj) 1869 if (obj.toString().length()>0) 1870 return Money.parse(obj.toString()); 1871 else 1872 return null; 1873 else 1874 return null; 1875 } 1877 1879 1890 public Money getMoney(String sCol, int iRow) 1891 throws ArrayIndexOutOfBoundsException ,NumberFormatException { 1892 int iCol = getColumnPosition(sCol); 1893 1894 if (iCol==-1) throw new ArrayIndexOutOfBoundsException ("Column " + sCol + " not found"); 1895 1896 return getMoney(iCol, iRow); 1897 } 1899 1901 1908 1909 public String getString (int iCol, int iRow) 1910 throws ArrayIndexOutOfBoundsException { 1911 1912 Object obj = (((Vector ) oResults.get(iRow)).get(iCol)); 1913 1914 if (null!=obj) 1915 return obj.toString(); 1916 else 1917 return null; 1918 1919 } 1921 1923 1930 1931 public String getStringNull (int iCol, int iRow, String sDef) 1932 throws ArrayIndexOutOfBoundsException { 1933 String str = getString(iCol,iRow); 1934 1935 return (null!=str ? str : sDef); 1936 1937 } 1939 1941 1948 1949 public String getString (String sCol, int iRow) 1950 throws ArrayIndexOutOfBoundsException { 1951 int iCol = getColumnPosition(sCol); 1952 1953 if (iCol==-1) 1954 throw new ArrayIndexOutOfBoundsException ("Column " + sCol + " not found"); 1955 1956 Object obj = (((Vector ) oResults.get(iRow)).get(iCol)); 1957 1958 if (null!=obj) 1959 return obj.toString(); 1960 else 1961 return null; 1962 } 1964 1966 1974 1975 public String getStringNull (String sCol, int iRow, String sDef) 1976 throws ArrayIndexOutOfBoundsException { 1977 int iCol = getColumnPosition(sCol); 1978 1979 if (iCol==-1) 1980 throw new ArrayIndexOutOfBoundsException ("Column " + sCol + " not found"); 1981 1982 String str = getString(iCol,iRow); 1983 1984 return (null!=str ? str : sDef); 1985 } 1987 1989 1998 public Time getTimeOfDay (int iCol, int iRow) 1999 throws ArrayIndexOutOfBoundsException , ClassCastException { 2000 2001 Object obj = (((Vector ) oResults.get(iRow)).get(iCol)); 2002 2003 if (null!=obj) 2004 return (Time ) obj; 2005 else 2006 return null; 2007 } 2009 2011 2020 public Timestamp getTimestamp(int iCol, int iRow) 2021 throws ArrayIndexOutOfBoundsException ,ClassCastException { 2022 Object obj = (((Vector ) oResults.get(iRow)).get(iCol)); 2023 2024 if (null!=obj) { 2025 if (obj instanceof Timestamp ) 2026 return (Timestamp ) obj; 2027 else if (obj instanceof Date ) 2028 return new Timestamp (((Date )obj).getTime()); 2029 else 2030 throw new ClassCastException ("Cannot cast "+obj.getClass().getName()+" to Timestamp"); 2031 } 2032 else 2033 return null; 2034 } 2035 2036 2038 2047 public long getTimeMilis(int iCol, int iRow) 2048 throws ArrayIndexOutOfBoundsException ,ClassCastException { 2049 Object obj = (((Vector ) oResults.get(iRow)).get(iCol)); 2050 2051 if (null!=obj) { 2052 if (obj instanceof Timestamp ) 2053 return ((Timestamp ) obj).getTime(); 2054 else if (obj instanceof Date ) 2055 return ((Date ) obj).getTime(); 2056 else 2057 throw new ClassCastException ("Cannot cast "+obj.getClass().getName()+" to Timestamp"); 2058 } 2059 else 2060 return 0; 2061 } 2062 2063 2065 2077 public long getIntervalMilis (int iCol, int iRow) 2078 throws ArrayIndexOutOfBoundsException ,ClassCastException { 2079 Object obj = (((Vector ) oResults.get(iRow)).get(iCol)); 2080 2081 if (null==obj) 2082 return 0l; 2083 else if (obj.getClass().getName().equals("org.postgresql.util.PGInterval")) { 2084 final long SecMilis = 1000l, MinMilis = 60000l, HourMilis=3600000l, DayMilis=86400000l; 2085 long lInterval; 2086 String [] aHMS; 2087 String sInt = obj.toString(); 2088 int iDays = sInt.indexOf("days"); 2089 if (iDays>0) { 2090 lInterval = Long.parseLong(sInt.substring(0,iDays-1)); 2091 aHMS = Gadgets.split(sInt.substring(iDays+5),':'); 2092 } 2093 else { 2094 lInterval =0; 2095 aHMS = Gadgets.split(sInt,':'); 2096 } 2097 lInterval += Long.parseLong(aHMS[0])*HourMilis+Long.parseLong(aHMS[1])*MinMilis+Long.parseLong(aHMS[2])*SecMilis; 2098 return lInterval; 2099 } 2100 else 2101 throw new ClassCastException ("Cannot cast "+obj.getClass().getName()+" to Timestamp"); 2102 } 2104 2106 2112 public void ensureCapacity(int nCols, int nRows) { 2113 if (DebugFile.trace) { 2114 DebugFile.writeln("Begin DBSubset.ensureCapacity("+String.valueOf(nCols)+","+String.valueOf(nRows)+")"); 2115 DebugFile.incIdent(); 2116 } 2117 2118 oResults = new Vector (nRows); 2119 for (int r=0;r<nRows; r++) { 2120 Vector oNewRow = new Vector (nCols); 2121 for (int c=0; c<nCols; c++) { 2122 oNewRow.add(null); 2123 } 2124 oResults.add(oNewRow); 2125 } 2126 if (DebugFile.trace) { 2127 DebugFile.decIdent(); 2128 DebugFile.writeln("End DBSubset.ensureCapacity()"); 2129 } 2130 } 2132 2134 2141 public void setElementAt (Object oObj, int iCol, int iRow) throws ArrayIndexOutOfBoundsException { 2142 2143 if (DebugFile.trace) { 2144 if (oObj==null) 2145 DebugFile.writeln("DBSubset.setElementAt(null,"+String.valueOf(iCol)+","+String.valueOf(iRow)+")"); 2146 else 2147 DebugFile.writeln("DBSubset.setElementAt("+oObj.toString()+","+String.valueOf(iCol)+","+String.valueOf(iRow)+")"); 2148 DebugFile.incIdent(); 2149 } 2150 2151 if (null==oResults) { 2152 if (DebugFile.trace) DebugFile.writeln("new Vector("+String.valueOf(iFetch)+",1)"); 2153 oResults = new Vector (iFetch, 1); 2154 } 2155 2156 Vector oRow; 2157 Object oRaw = oResults.get(iRow); 2158 2159 if (null==oRaw) { 2160 if (DebugFile.trace) DebugFile.writeln("new Vector("+String.valueOf(iCol)+",1)"); 2161 oRow = new Vector (iCol, 1); 2162 oResults.add(iRow, oRow); 2163 } 2164 else { 2165 oRow = (Vector ) oRaw; 2166 } 2167 2168 oRow.setElementAt (oObj, iCol); 2169 2170 if (DebugFile.trace) { 2171 DebugFile.decIdent(); 2172 DebugFile.writeln("End DBSubset.setElementAt()"); 2173 } 2174 } 2176 2178 2185 public void setElementAt (Object oObj, String sCol, int iRow) 2186 throws ArrayIndexOutOfBoundsException { 2187 int iCol = getColumnPosition(sCol); 2188 if (-1==iCol) 2189 throw new ArrayIndexOutOfBoundsException ("DBSubset.setElementAt() column "+sCol+" not found"); 2190 else 2191 setElementAt (oObj, iCol, iRow); 2192 } 2194 2196 2202 public boolean isNull (int iCol, int iRow) 2203 throws ArrayIndexOutOfBoundsException { 2204 Object obj = (((Vector ) oResults.get(iRow)).get(iCol)); 2205 2206 return (null==obj); 2207 2208 } 2210 2211 2213 2219 2220 public boolean isNull (String sCol, int iRow) throws ArrayIndexOutOfBoundsException { 2221 int iCol = getColumnPosition(sCol); 2222 2223 if (iCol==-1) 2224 throw new ArrayIndexOutOfBoundsException ("Column " + sCol + " not found"); 2225 2226 Object obj = (((Vector ) oResults.get(iRow)).get(iCol)); 2227 2228 return (null==obj); 2229 } 2231 2233 2238 2239 public String toString() { 2240 Vector vRow; 2241 int iCol; 2242 int iRowCount; 2243 StringBuffer strBuff; 2244 2245 if (oResults==null) return ""; 2246 2247 iRowCount = oResults.size(); 2248 2249 if (iRowCount==0) return ""; 2250 2251 strBuff = new StringBuffer (64*iRowCount); 2252 2253 for (int iRow=0; iRow<iRowCount; iRow++) 2254 { 2255 vRow = (Vector ) oResults.get(iRow); 2256 iCol = 0; 2257 while (iCol<iColCount) 2258 { 2259 strBuff.append(vRow.get(iCol)); 2260 if (++iCol<iColCount) strBuff.append(sColDelim); 2261 } 2262 strBuff.append(sRowDelim); 2263 } 2264 2265 return strBuff.toString(); 2266 } 2268 2270 2279 2280 public String toXML(String sIdent, String sNode, 2281 String sDateFormat, String sDecimalFormat) { 2282 Vector vRow; 2283 int iAs; 2284 int iCol; 2285 int iDot; 2286 int iRowCount; 2287 int iTokCount; 2288 StringBuffer strBuff; 2289 StringTokenizer strTok; 2290 String sLabel; 2291 String sNodeName; 2292 Object oColValue; 2293 Class oColClass, ClassString = null, ClassDate = null, 2294 ClassBigDecimal = null, ClassDouble = null, ClassFloat = null; 2295 SimpleDateFormat oXMLDate; 2296 DecimalFormat oDecFmt = null; 2297 2298 if (sDateFormat==null) 2299 oXMLDate = new SimpleDateFormat ("yyyy-MM-dd'T'hh:mm:ss"); 2300 else if (sDateFormat.length()==0) 2301 oXMLDate = new SimpleDateFormat ("yyyy-MM-dd'T'hh:mm:ss"); 2302 else 2303 oXMLDate = new SimpleDateFormat (sDateFormat); 2304 2305 if (null!=sDecimalFormat) { 2306 if (sDecimalFormat.length()>0) 2307 oDecFmt = new DecimalFormat (sDecimalFormat); 2308 } 2310 if (DebugFile.trace) { 2311 DebugFile.writeln("Begin DBSubset.toXML(" + sNode + ")"); 2312 DebugFile.incIdent(); 2313 } 2314 2315 try { 2316 ClassString = Class.forName("java.lang.String"); 2317 ClassDate = Class.forName("java.util.Date"); 2318 ClassBigDecimal = Class.forName("java.math.BigDecimal"); 2319 ClassDouble = Class.forName("java.lang.Double"); 2320 ClassFloat = Class.forName("java.lang.Float"); 2321 } catch (ClassNotFoundException ignore) { } 2322 2323 if (oResults!=null) { 2324 2325 sNodeName = (null!=sNode ? sNode : sTable); 2326 2327 iRowCount = oResults.size(); 2328 strBuff = new StringBuffer (256*iRowCount); 2329 2330 strTok = new StringTokenizer (sColList,","); 2331 iTokCount = strTok.countTokens(); 2332 String [] Labels = new String [iTokCount]; 2333 2334 for (int iTok=0; iTok<iTokCount; iTok++) { 2335 sLabel = strTok.nextToken(); 2336 iAs = sLabel.toUpperCase().indexOf(" AS "); 2337 if (-1!=iAs) sLabel = sLabel.substring(iAs+4); 2338 iDot = sLabel.indexOf('.'); 2339 if (-1!=iDot) sLabel = sLabel.substring(++iDot); 2340 Labels[iTok] = sLabel.trim(); 2341 } 2343 for (int iRow=0; iRow<iRowCount; iRow++) 2344 { 2345 vRow = (Vector ) oResults.get(iRow); 2346 iCol = 0; 2347 strBuff.append(sIdent + "<" + sNodeName + ">\n"); 2348 while (iCol<iColCount) 2349 { 2350 strBuff.append(sIdent + " <" + Labels[iCol] + ">"); 2351 oColValue = vRow.get(iCol); 2352 if (null!=oColValue) { 2353 oColClass = oColValue.getClass(); 2354 2355 if (oColClass.equals(ClassString) && !Labels[iCol].startsWith("gu_")) 2356 strBuff.append("<![CDATA[" + oColValue + "]]>"); 2357 2358 else if (oColClass.equals(ClassDate)) 2359 strBuff.append (oXMLDate.format((java.util.Date ) oColValue)); 2360 2361 else if (oColClass.equals(ClassBigDecimal) && (oDecFmt!=null)) 2362 strBuff.append (oDecFmt.format((java.math.BigDecimal ) oColValue)); 2363 2364 else if (oColClass.equals(ClassDouble) && (oDecFmt!=null)) 2365 strBuff.append (oDecFmt.format(((java.lang.Double ) oColValue).doubleValue())); 2366 2367 else if (oColClass.equals(ClassFloat) && (oDecFmt!=null)) 2368 strBuff.append (oDecFmt.format((double)((java.lang.Float ) oColValue).floatValue())); 2369 2370 else 2371 strBuff.append(oColValue); 2372 } 2373 strBuff.append("</" + Labels[iCol] + ">\n"); 2374 iCol++; 2375 } 2376 strBuff.append(sIdent + "</" + sNodeName + ">\n"); 2377 } } 2379 else 2380 strBuff = new StringBuffer (); 2381 2382 if (DebugFile.trace) { 2383 DebugFile.writeln("End DBSubset.toXML() : " + String.valueOf(strBuff.length())); 2384 DebugFile.decIdent(); 2385 } 2386 2387 return strBuff.toString(); 2388 2389 } 2391 2399 2400 public String toXML(String sIdent, String sNode) { 2401 return toXML(sIdent, sNode, null, null); 2402 } 2403 2404 2406 2420 2421 public void print(Connection oConn, OutputStream oOutStrm) throws SQLException { 2422 String sCol; 2423 int iRows; 2424 int iCol; 2425 short jCol; 2426 float fCol; 2427 double dCol; 2428 Date dtCol; 2429 BigDecimal bdCol; 2430 Object oCol; 2431 boolean bQualify = sTxtQualifier.length()>0; 2432 2433 if (DebugFile.trace) { 2434 DebugFile.writeln("Begin DBSubset.print([Connection], [Object])"); 2435 DebugFile.incIdent(); 2436 } 2437 2438 Statement oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 2439 2440 if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(" + sSelect + ")"); 2441 2442 ResultSet oRSet = oStmt.executeQuery(sSelect); 2443 2444 if (DebugFile.trace) DebugFile.writeln("ResultSet.getMetaData()"); 2445 2446 ResultSetMetaData oMDat = oRSet.getMetaData(); 2447 int iCols = oMDat.getColumnCount(); 2448 2449 if (DebugFile.trace) DebugFile.writeln("column count = " + String.valueOf(iCols)); 2450 2451 PrintWriter oWriter = new PrintWriter (oOutStrm); 2452 2453 iRows = 0; 2454 while (oRSet.next()) { 2455 for (int c=1; c<=iCols; c++) { 2456 switch (oMDat.getColumnType(c)) { 2457 case Types.VARCHAR: 2458 case Types.CHAR: 2459 sCol = oRSet.getString(c); 2460 if (!oRSet.wasNull()) { 2461 sCol = sCol.replace('\n',' '); 2462 if (bQualify) 2463 oWriter.print(sTxtQualifier + sCol + sTxtQualifier); 2464 else 2465 oWriter.print(sCol); 2466 } 2467 break; 2468 case Types.DATE: 2469 dtCol = oRSet.getDate(c); 2470 if (!oRSet.wasNull()) oWriter.write(dtCol.toString()); 2471 break; 2472 case Types.INTEGER: 2473 iCol = oRSet.getInt(c); 2474 if (!oRSet.wasNull()) oWriter.print(iCol); 2475 break; 2476 case Types.SMALLINT: 2477 jCol = oRSet.getShort(c); 2478 if (!oRSet.wasNull()) oWriter.print(jCol); 2479 break; 2480 case Types.FLOAT: 2481 fCol = oRSet.getFloat(c); 2482 if (!oRSet.wasNull()) oWriter.print(fCol); 2483 break; 2484 case Types.REAL: 2485 dCol = oRSet.getDouble(c); 2486 if (!oRSet.wasNull()) oWriter.print(dCol); 2487 break; 2488 case Types.DECIMAL: 2489 bdCol = oRSet.getBigDecimal(c); 2490 if (!oRSet.wasNull()) oWriter.print(bdCol.toString()); 2491 break; 2492 default: 2493 oCol = oRSet.getObject(c); 2494 if (!oRSet.wasNull()) oWriter.print(oCol.toString()); 2495 break; 2496 } if (c<iCols) oWriter.print(getColumnDelimiter()); 2498 } oWriter.print(getRowDelimiter()); 2500 iRows++; 2501 } 2503 oWriter.flush(); 2504 oWriter.close(); 2505 oWriter = null; 2506 2507 oRSet.close(); 2508 oRSet = null; 2509 oStmt.close(); 2510 oStmt = null; 2511 2512 if (DebugFile.trace) { 2513 DebugFile.decIdent(); 2514 DebugFile.writeln("End DBSubset.print() : " + String.valueOf(iRows)); 2515 } 2516 2517 } 2519 2521 private static String removeQuotes (String sStr) { 2522 final int iLen = sStr.length(); 2523 StringBuffer oStr = new StringBuffer (iLen); 2524 char c; 2525 2526 for (int i=0; i<iLen; i++) { 2527 c = sStr.charAt(i); 2528 if (c!='"' && c!=' ' && c!='\n' && c!='\t' && c!='\r') 2529 oStr.append(c); 2530 } 2532 return oStr.toString(); 2533 } 2535 2537 2559 public SQLException [] store (JDCConnection oConn, Class oDBPersistSubclass, boolean bStopOnError) 2560 throws SQLException , IllegalAccessException , InstantiationException , ArrayIndexOutOfBoundsException { 2561 2562 DBPersist oDBP; 2563 DBTable oTbl; 2564 Object oFld; 2565 Statement oStmt; 2566 ResultSet oRSet; 2567 ResultSetMetaData oMDat; 2568 SQLException [] aExceptions; 2569 int iExceptions = 0; 2570 int iType = Types.NULL; 2571 2572 if (DebugFile.trace) { 2573 if (null==oDBPersistSubclass) 2574 DebugFile.writeln("Begin DBSubset.store([Connection],null"); 2575 else 2576 DebugFile.writeln("Begin DBSubset.store([Connection],[" + oDBPersistSubclass.getName() + "]"); 2577 DebugFile.incIdent(); 2578 } 2579 2580 String [] aCols = Gadgets.split(removeQuotes(sColList), ','); 2581 2582 iColCount = aCols.length ; 2583 2584 if (oDBPersistSubclass!=null) { 2585 oDBP = (DBPersist) oDBPersistSubclass.newInstance(); 2586 oTbl = oDBP.getTable(); 2587 2588 sColList = ""; 2589 for (int c=0; c<iColCount; c++) 2590 if (null!=oTbl.getColumnByName(aCols[c])) 2591 sColList += (c==0 ? "" : "," ) + aCols[c]; 2592 else 2593 sColList += (c==0 ? "" : "," ) + "'void' AS " + aCols[c]; 2594 } 2595 2596 final int iRowCount = getRowCount(); 2597 2598 if (bStopOnError) 2599 aExceptions = null; 2600 else 2601 aExceptions = new SQLException [iRowCount]; 2602 2603 oStmt = oConn.createStatement(); 2604 2605 if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(SELECT " + sColList + " FROM " + sTable + " WHERE 1=0)"); 2606 2607 oRSet = oStmt.executeQuery("SELECT " + sColList + " FROM " + sTable + " WHERE 1=0"); 2608 oMDat = oRSet.getMetaData(); 2609 2610 int[] aTypes = new int[oMDat.getColumnCount()]; 2611 2612 ColNames = new String [oMDat.getColumnCount()]; 2613 2614 for (int t=1; t<=iColCount; t++) { 2615 ColNames[t-1] = oMDat.getColumnName(t).toLowerCase(); 2616 aTypes [t-1] = oMDat.getColumnType(t); 2617 } 2618 2619 oMDat = null; 2620 oRSet.close(); 2621 oStmt.close(); 2622 2623 if (oDBPersistSubclass!=null) 2624 oDBP = (DBPersist) oDBPersistSubclass.newInstance(); 2625 else 2626 oDBP = new DBPersist(sTable, sTable); 2627 2628 for (int r=0; r<iRowCount; r++) { 2629 2630 if (DebugFile.trace) DebugFile.writeln("processing row " + String.valueOf(r)); 2631 2632 for (int c=0; c<iColCount; c++) { 2633 2634 oFld = get(c, r); 2635 2636 if (null!=oFld) { 2637 iType = aTypes[c]; 2638 if (iType==Types.BLOB) iType = Types.LONGVARBINARY; 2639 if (iType==Types.CLOB) iType = Types.LONGVARCHAR; 2640 try { 2641 if (oFld.toString().length()>0 && !oDBP.AllVals.containsKey(aCols[c])) { 2642 if (oFld.getClass().getName().equals("java.util.Date")) 2643 oDBP.put(aCols[c], (java.util.Date ) oFld); 2644 else 2645 oDBP.put(aCols[c], oFld.toString(), iType); 2646 } 2647 } catch (FileNotFoundException e) { } 2648 } } 2651 if (bStopOnError) { 2652 2653 oDBP.store(oConn); 2654 } 2655 else { 2656 2657 try { 2658 2659 oDBP.store(oConn); 2660 aExceptions[r] = null; 2661 2662 } catch (SQLException sqle) { 2663 iExceptions++; 2664 aExceptions[r] = sqle; 2665 } 2666 } 2668 oDBP.clear(); 2669 } 2671 ColNames = null; 2672 2673 aTypes = null; 2674 2675 bEOF = (0==iExceptions); 2676 2677 if (DebugFile.trace) { 2678 DebugFile.decIdent(); 2679 DebugFile.writeln("End DBSubset.store() : " + String.valueOf(iExceptions)); 2680 } 2681 2682 return aExceptions; 2683 } 2685 2687 private boolean swapRows(int iRow1, int iRow2) 2688 throws ArrayIndexOutOfBoundsException { 2689 Object oRow1 = oResults.get(iRow1); 2690 Object oRow2 = oResults.get(iRow2); 2691 oResults.setElementAt(oRow2, iRow1); 2692 oResults.setElementAt(oRow1, iRow2); 2693 return true; 2694 } 2695 2696 2705 public void sortBy(int iCol) 2706 throws ArrayIndexOutOfBoundsException , ClassCastException { 2707 2708 if (DebugFile.trace) { 2709 DebugFile.writeln("Begin DBSubset.sortBy("+String.valueOf(iCol)+")"); 2710 DebugFile.incIdent(); 2711 } 2712 2713 final int iRows = getRowCount(); 2714 final int iRows1 = iRows-1; 2715 boolean bSwapFlag = true; 2716 2717 for (int q=0; q<iRows && bSwapFlag; q++) { 2718 bSwapFlag = false; 2719 for (int r=0; r<iRows1; r++) { 2720 if (!isNull(iCol,r) || !isNull(iCol,r+1)) { 2721 if (!isNull(iCol,r) && isNull(iCol,r+1)) 2722 bSwapFlag = swapRows(r,r+1); 2723 else if (((Comparable ) get(iCol, r)).compareTo(get(iCol, r+1))>0) 2724 bSwapFlag = swapRows(r,r+1); 2725 } } } 2729 if (DebugFile.trace) { 2730 DebugFile.decIdent(); 2731 DebugFile.writeln("End DBSubset.sortBy("+String.valueOf(iCol)+")"); 2732 } 2733 } 2735 2737 private BigDecimal sumDecimal(int iCol) 2738 throws NumberFormatException , ArrayIndexOutOfBoundsException { 2739 BigDecimal oRetVal = new BigDecimal (0); 2740 final int iRows = getRowCount(); 2741 2742 for (int r=0; r<iRows; r++) 2743 if (!isNull(iCol, r)) 2744 oRetVal.add(getDecimal(iCol, r)); 2745 2746 return oRetVal; 2747 } 2748 2749 private Integer sumInteger(int iCol) { 2750 int iRetVal = 0; 2751 final int iRows = getRowCount(); 2752 2753 for (int r=0; r<iRows; r++) 2754 if (!isNull(iCol, r)) 2755 iRetVal += getInt(iCol, r); 2756 2757 return new Integer (iRetVal); 2758 } 2759 2760 private Short sumShort(int iCol) { 2761 short iRetVal = 0; 2762 final int iRows = getRowCount(); 2763 2764 for (int r=0; r<iRows; r++) 2765 if (!isNull(iCol, r)) 2766 iRetVal += getShort(iCol, r); 2767 2768 return new Short (iRetVal); 2769 } 2770 2771 private Float sumFloat(int iCol) { 2772 float fRetVal = 0; 2773 final int iRows = getRowCount(); 2774 2775 for (int r=0; r<iRows; r++) 2776 if (!isNull(iCol, r)) 2777 fRetVal += getFloat(iCol, r); 2778 2779 return new Float (fRetVal); 2780 } 2781 2782 private Double sumDouble(int iCol) { 2783 double dRetVal = 0; 2784 final int iRows = getRowCount(); 2785 2786 for (int r=0; r<iRows; r++) 2787 if (!isNull(iCol, r)) 2788 dRetVal += getDouble(iCol, r); 2789 2790 return new Double (dRetVal); 2791 } 2792 2793 public Object sum(int iCol) 2794 throws NumberFormatException , ArrayIndexOutOfBoundsException { 2795 final int iRows = getRowCount(); 2796 2797 if (0==iRows) return null; 2798 2799 Object oFirst = null; 2800 int r = 0; 2801 do 2802 oFirst = get(iCol, 0); 2803 while ((null==oFirst) && (r<iRows)); 2804 2805 if (null==oFirst) return new BigDecimal (0); 2806 2807 if (oFirst.getClass().getName().equals("java.math.BigDecimal")) 2808 return sumDecimal(iCol); 2809 else if (oFirst.getClass().getName().equals("java.lang.Integer")) 2810 return sumInteger(iCol); 2811 else if (oFirst.getClass().getName().equals("java.lang.Short")) 2812 return sumShort(iCol); 2813 else if (oFirst.getClass().getName().equals("java.lang.Float")) 2814 return sumFloat(iCol); 2815 else if (oFirst.getClass().getName().equals("java.lang.Double")) 2816 return sumDouble(iCol); 2817 else 2818 throw new NumberFormatException ("Column " + String.valueOf(iCol) + " is not a suitable type for sum()"); 2819 } 2820 2821 2823 2830 public void union(DBSubset oDbs) 2831 throws ArrayIndexOutOfBoundsException ,NullPointerException { 2832 if (this.getColumnCount()!=oDbs.getColumnCount()) { 2833 throw new ArrayIndexOutOfBoundsException ("DBSubset.union() subsets to be unified must have the same number of columns"); 2834 } 2835 final int iDbsRows = oDbs.getRowCount(); 2836 if (iDbsRows>0) { 2837 oResults.ensureCapacity(getRowCount()+iDbsRows); 2838 for (int r=0; r<iDbsRows; r++) { 2839 oResults.add(oDbs.oResults.get(r)); 2840 } } } 2844 2846 2859 2860 public void parseCSV (String sFilePath, String sCharSet) 2861 throws ArrayIndexOutOfBoundsException ,IOException ,FileNotFoundException , 2862 RuntimeException ,NullPointerException ,IllegalArgumentException { 2863 2864 if (DebugFile.trace) { 2865 DebugFile.writeln("Begin DBSubset.parseCSV(" + sFilePath + ")"); 2866 DebugFile.incIdent(); 2867 } 2868 2869 Vector oRow; 2870 2871 String [] aCols = Gadgets.split (removeQuotes(sColList), ','); 2872 2873 iColCount = aCols.length; 2874 2875 CSVParser oParser = new CSVParser (sCharSet); 2876 2877 oParser.parseFile (sFilePath, sColList.replace(',',sColDelim.charAt(0))); 2878 2879 final int iRowCount = oParser.getLineCount(); 2880 2881 oResults = new Vector (iRowCount, 1); 2882 2883 for (int r=0; r<iRowCount; r++) { 2884 oRow = new Vector (iColCount); 2885 2886 for (int c=0; c<iColCount; c++) 2887 oRow.add (oParser.getField(c,r)); 2888 2889 oResults.add (oRow); 2890 } 2892 if (DebugFile.trace) { 2893 DebugFile.decIdent(); 2894 DebugFile.writeln("End DBSubset.parseCSV()"); 2895 } 2896 } 2898 2900 2905 2906 public void parseCSV (String sFilePath) 2907 throws ArrayIndexOutOfBoundsException ,IOException ,FileNotFoundException , 2908 RuntimeException ,NullPointerException ,IllegalArgumentException { 2909 2910 parseCSV (sFilePath, null); 2911 2912 } 2914 2916 2922 2923 public void parseCSV (char[] aData, String sCharSet) 2924 throws ArrayIndexOutOfBoundsException , RuntimeException , NullPointerException , 2925 IllegalArgumentException , UnsupportedEncodingException { 2926 2927 if (DebugFile.trace) { 2928 DebugFile.writeln("Begin DBSubset.parseCSV(char[], " + sCharSet + ")"); 2929 DebugFile.incIdent(); 2930 } 2931 2932 Vector oRow; 2933 2934 String [] aCols = Gadgets.split (removeQuotes(sColList), ','); 2935 2936 CSVParser oParser = new CSVParser (sCharSet); 2937 2938 oParser.parseData (aData, sColList.replace(',',sColDelim.charAt(0))); 2939 2940 final int iRowCount = oParser.getLineCount(); 2941 iColCount = aCols.length; 2942 2943 oResults = new Vector (iRowCount, 1); 2944 2945 for (int r=0; r<iRowCount; r++) { 2946 oRow = new Vector (iColCount); 2947 2948 for (int c=0; c<iColCount; c++) 2949 oRow.add (oParser.getField(c,r)); 2950 2951 oResults.add (oRow); 2952 } 2954 if (DebugFile.trace) { 2955 DebugFile.decIdent(); 2956 DebugFile.writeln("End DBSubset.parseCSV()"); 2957 } 2958 } 2960 2962 2967 2968 public void parseCSV (char[] aData) 2969 throws ArrayIndexOutOfBoundsException , RuntimeException , NullPointerException , 2970 IllegalArgumentException , UnsupportedEncodingException { 2971 2972 parseCSV(aData, null); 2973 } 2974 2975 2978 private static Class getClassForName(String sClassName) { 2979 Class oRetVal; 2980 try { 2981 oRetVal = Class.forName(sClassName); 2982 } 2983 catch (ClassNotFoundException cnfe) { oRetVal = null; } 2984 2985 return oRetVal; 2986 } 2987 2988 2990 private static Class ClassLangString = getClassForName("java.lang.String"); 2991 private static Class ClassUtilDate = getClassForName("java.util.Date"); 2992 private static Class ClassSQLDate = getClassForName("java.sql.Date"); 2993 private static Class ClassSQLTime = getClassForName("java.sql.Time"); 2994 private static Class ClassTimestamp = getClassForName("java.sql.Timestamp"); 2995 2996 private int iFetch; 2997 private int iTimeOut; 2998 private int iColCount; 2999 private int iMaxRows; 3000 private boolean bEOF; 3001 private String sTable; 3002 private String sColList; 3003 private String sFilter; 3004 private String sSelect; 3005 private String sInsert; 3006 private String sColDelim; 3007 private String sRowDelim; 3008 private String sTxtQualifier; 3009 private Vector oResults; 3010 private String ColNames[]; 3011 private SimpleDateFormat oShortDate; 3012 private SimpleDateFormat oDateTime; 3013 private DecimalFormat oDecFmt; 3014 3015}
| Popular Tags
|