1 32 33 package com.knowgate.datacopy; 34 35 import java.sql.DriverManager ; 36 import java.sql.Connection ; 37 import java.sql.Timestamp ; 38 import java.sql.SQLException ; 39 import java.sql.CallableStatement ; 40 import java.sql.Statement ; 41 import java.sql.PreparedStatement ; 42 import java.sql.ResultSet ; 43 import java.sql.ResultSetMetaData ; 44 45 import java.io.IOException ; 46 import java.io.File ; 47 import java.io.FileReader ; 48 import java.io.BufferedReader ; 49 import java.io.StringBufferInputStream ; 50 import java.io.FileInputStream ; 51 import java.io.FileWriter ; 52 import java.io.StringReader ; 53 54 import java.lang.ArrayIndexOutOfBoundsException ; 55 import java.lang.ClassNotFoundException ; 56 import java.math.BigDecimal ; 57 import java.util.Vector ; 58 import java.util.LinkedList ; 59 import java.util.ListIterator ; 60 import java.util.HashMap ; 61 import java.util.Properties ; 62 import java.util.Enumeration ; 63 import java.util.Set ; 64 import java.util.Iterator ; 65 import org.xml.sax.*; 66 import org.xml.sax.helpers.*; 67 68 import org.apache.oro.text.regex.*; 69 70 import com.knowgate.debug.DebugFile; 71 72 79 public class DataStruct extends DefaultHandler implements ContentHandler { 80 81 public DataStruct() { 82 initMembers(); 83 } 85 87 public DataStruct(String sPathXMLFile) throws ClassNotFoundException , IllegalAccessException , InstantiationException , IOException , SAXException { 88 initMembers(); 89 parse(sPathXMLFile); 90 } 92 94 public DataStruct(String sPathXMLFile, Properties oProps) throws ClassNotFoundException , IllegalAccessException , InstantiationException , IOException , SAXException { 95 initMembers(); 96 parse(sPathXMLFile, oProps); 97 } 99 101 private void initMembers() { 102 cTables = 0; 104 105 bOrPrepared = false; 107 bTrPrepared = false; 108 109 iTrStatus = iOrStatus = DISCONNECTED; 111 112 DataRowSets = new Vector (); 114 FieldMaps = new Vector (); 116 FieldDefs = new Vector (); 118 Transformations = new Vector (); 120 Before = new Vector (); 122 After = new Vector (); 124 } 128 130 public void setAutoCommit(boolean bAutoCommit) throws SQLException { 131 oTrConn.setAutoCommit(bAutoCommit); 132 } 133 134 136 public void commit() throws SQLException { 137 oTrConn.commit(); 138 } 139 140 142 public void rollback() throws SQLException { 143 oTrConn.rollback(); 144 } 145 146 147 149 public void connectOrigin(String sDriver, String sURL, String sUsr, String sPwd, String sSchema) throws SQLException ,ClassNotFoundException { 150 if (DebugFile.trace) DebugFile.writeln ("Begin DataStruct.connectOrigin(" + sDriver + "," + sURL + "," + sUsr + "," + sPwd + ")"); 152 153 Class oDriver = Class.forName(sDriver); 155 156 if (DebugFile.trace) DebugFile.writeln (" " + sDriver + " JDBC driver loaded"); 157 158 oOrConn = DriverManager.getConnection(sURL, sUsr, sPwd); 159 iOrStatus = CONNECTED; 160 161 if (DebugFile.trace) DebugFile.writeln ("End DataStruct.connectOrigin()"); 162 } 164 166 public void connectTarget(String sDriver, String sURL, String sUsr, String sPwd, String sSchema) throws SQLException ,ClassNotFoundException { 167 if (DebugFile.trace) DebugFile.writeln ("Begin DataStruct.connectTarget(" + sDriver + "," + sURL + "," + sUsr + "," + sPwd + ")"); 169 170 Class oDriver = Class.forName(sDriver); 172 173 if (DebugFile.trace) DebugFile.writeln (" " + sDriver + " JDBC driver loaded"); 174 175 oTrConn = DriverManager.getConnection(sURL, sUsr, sPwd); 176 iTrStatus = CONNECTED; 177 178 if (DebugFile.trace) DebugFile.writeln ("End DataStruct.connectTarget()"); 179 } 180 181 183 public Connection getOriginConnection() { 184 return oOrConn; 185 } 186 187 189 public Connection getTargetConnection() { 190 return oTrConn; 191 } 192 193 195 public void setOriginConnection(Connection oConn) { 196 oOrConn = oConn; 198 iOrStatus = REFERENCED; 199 } 200 201 203 public void setTargetConnection(Connection oConn) { 204 oTrConn = oConn; 206 iTrStatus = REFERENCED; 207 } 208 209 211 public void clear() throws SQLException { 212 int t; 214 215 if (DebugFile.trace) DebugFile.writeln ("Begin DataStruct.clear()"); 216 217 if (bOrPrepared) { 218 for (t=0; t<cTables; t++) { 219 if (null!=OrStatements[t]) OrStatements[t].close(); 220 OrStatements[t] = null; 221 if (null!=UpStatements[t]) UpStatements[t].close(); 222 UpStatements[t] = null; 223 } 224 OrMetaData = null; 225 bOrPrepared = false; 226 } 227 228 if (bTrPrepared) { 229 for (t=0; t<cTables; t++) { 230 if (null!=TrStatements[t]) TrStatements[t].close(); 231 TrStatements[t] = null; 232 if (null!=DlStatements[t]) DlStatements[t].close(); 233 DlStatements[t] = null; 234 } 235 TrMetaData = null; 236 bTrPrepared = false; 237 } 238 239 cTables = 0; 241 242 if (null!=After) After.clear(); 243 if (null!=Before) Before.clear(); 244 if (null!=Transformations) Transformations.clear(); 245 if (null!=FieldDefs) FieldDefs.clear(); 246 if (null!=FieldMaps) FieldMaps.clear(); 247 if (null!=DataRowSets) DataRowSets.clear(); 248 249 if (DebugFile.trace) DebugFile.writeln ("End DataStruct.clear()"); 250 251 } 253 255 public void disconnectAll() throws SQLException { 256 int t; 258 259 if (DebugFile.trace) DebugFile.writeln ("Begin DataStruct.disconnectAll()"); 260 261 clear(); 262 263 if (CONNECTED==iOrStatus) { oOrConn.close(); iOrStatus=DISCONNECTED; } 265 if (CONNECTED==iTrStatus) { oTrConn.close(); iTrStatus=DISCONNECTED; } 266 267 if (DebugFile.trace) DebugFile.writeln ("End DataStruct.disconnectAll()"); 268 } 270 272 protected DataRowSet getRowSet(int i) { 273 return (DataRowSet) DataRowSets.get(i); 274 } 275 276 278 protected Object getResult(int iRow, int iCol) { 279 return ((Vector ) oResults.get(iRow)).get(iCol); 280 } 281 282 284 private boolean isEmpty(String sStr) { 285 if (null==sStr) 287 return true; 288 else if (0==sStr.length()) 289 return true; 290 else 291 return false; 292 } 294 296 protected void execCommands(String sTime, int iTable, Object PK[], int cParams) 297 throws SQLException ,NullPointerException { 298 CallableStatement oCall; 300 Statement oStmt; 301 ResultSet rCount; 302 int cAffected; 303 String sSQL; 304 String sTable; 305 ListIterator oIter; 306 307 if (DebugFile.trace) { 308 if (iTable!=-1) 309 sTable = getRowSet(iTable).OriginTable; 310 else 311 sTable = ""; 312 DebugFile.writeln ("Begin DataStruct.execCommands(" + sTime + ", " + sTable + ", ..., " + String.valueOf(cParams) + ")"); 313 DebugFile.incIdent(); 314 } 315 316 if (-1==iTable) { 318 if (sTime.equals("INIT")) { 319 if (null==InitStmts) 320 throw new NullPointerException ("DataStruct.execCommands() InitStmts list not initialized"); 321 oIter = InitStmts.listIterator(); 322 } else { 323 if (null==TermStmts) 324 throw new NullPointerException ("DataStruct.execCommands() TermStmts list not initialized"); 325 oIter = TermStmts.listIterator(); 326 } 327 } 328 else { 329 if (sTime.equals("BEFORE")) 330 oIter = ((LinkedList )Before.get(iTable)).listIterator(); 331 else 332 oIter = ((LinkedList )After.get(iTable)).listIterator(); 333 } 334 335 while (oIter.hasNext()) { 336 sSQL = oIter.next().toString(); 337 338 if (sSQL.startsWith("{") || sSQL.startsWith("k_sp")) { 340 if (DebugFile.trace) DebugFile.writeln ("Connection.prepareCall(" + sSQL + ")"); 341 oCall = oTrConn.prepareCall(sSQL); 342 for (int p=0; p<cParams; p++) { 343 if (DebugFile.trace) DebugFile.writeln ("CallableStatement.setObject(" + String.valueOf(p+1) + "," + PK[p].toString() + ")"); 344 oCall.setObject(p+1, PK[p]); 345 } if (DebugFile.trace) DebugFile.writeln ("Connection.execute(" + sSQL + ")"); 347 oCall.execute(); 348 oCall.close(); 349 oCall = null; 350 } 351 else { 353 oStmt = oTrConn.createStatement(); 354 if (DebugFile.trace) DebugFile.writeln ("Connection.execute(" + sSQL + ")"); 355 oStmt.execute(sSQL); 356 oStmt.close(); 357 oStmt = null; 358 } 359 } 361 if (DebugFile.trace) { 362 DebugFile.decIdent(); 363 DebugFile.writeln ("End DataStruct.execCommands()"); 364 } 365 } 367 369 public void prepareStatements() throws SQLException { 370 373 HashMap oMap; 374 String sSQL; 375 boolean bIsMapped; 376 boolean bHasDefault; 377 int iTrCols; 378 String sCol; 379 int iCol; 380 int c; 381 382 if (DebugFile.trace) { 383 DebugFile.writeln ("Begin DataStruct.prepareStatements()"); 384 DebugFile.incIdent(); 385 } 386 387 oInsertTexts = new HashMap (2*cTables); oSelectTexts = new HashMap (2*cTables); 390 OrStatements = new PreparedStatement [cTables]; TrStatements = new PreparedStatement [cTables]; UpStatements = new PreparedStatement [cTables]; DlStatements = new PreparedStatement [cTables]; 395 OrMetaData = new DataTblDef[cTables]; TrMetaData = new DataTblDef[cTables]; 398 for (int s=0; s<cTables; s++) { 400 401 if (CONNECTED==iTrStatus || REFERENCED==iTrStatus) { 402 403 TrMetaData[s] = new DataTblDef(); 406 if (DebugFile.trace) DebugFile.writeln ("DataTblDef.readMetaData (TargetConnection, " + getRowSet(s).TargetTable + ", " + (String ) oToPKs.get(getRowSet(s).TargetTable) + ")"); 407 408 if (oToPKs.get(getRowSet(s).TargetTable)!=null) 410 TrMetaData[s].readMetaData(oTrConn, getRowSet(s).TargetTable, oToPKs.get(getRowSet(s).TargetTable).toString()); 411 else 412 TrMetaData[s].readMetaData(oTrConn, getRowSet(s).TargetTable, null); 413 415 sSQL = "DELETE FROM " + getRowSet(s).TargetTable; 418 if (!isEmpty(getRowSet(s).EraseClause)) 419 sSQL += " WHERE " + getRowSet(s).EraseClause; 420 else 421 if (!isEmpty(getRowSet(s).WhereClause)) 422 sSQL += " WHERE " + getRowSet(s).WhereClause; 423 424 if (DebugFile.trace) DebugFile.writeln ("Connection.prepareStatement(" + sSQL + ")"); 425 DlStatements[s] = oTrConn.prepareStatement(sSQL); 426 428 iTrCols = TrMetaData[s].ColCount; 429 430 sSQL = "INSERT INTO " + getRowSet(s).TargetTable + " VALUES ("; 433 for (c=iTrCols; c>=1; c--) 434 sSQL += (c!=1) ? "?," : "?)"; 435 436 if (DebugFile.trace) DebugFile.writeln ("Connection.prepareStatement(" + sSQL + ")"); 437 438 oInsertTexts.put (getRowSet(s).OriginTable, sSQL); 441 442 TrStatements[s] = oTrConn.prepareStatement(sSQL); 443 445 sSQL = "UPDATE " + getRowSet(s).TargetTable + " SET "; 448 for (c=0; c<iTrCols; c++) 449 if (!TrMetaData[s].isPrimaryKey(c)) 450 sSQL += TrMetaData[s].ColNames[c] + "=?,"; 451 sSQL = sSQL.substring(0, sSQL.length()-1) + " WHERE "; 452 for (c=0; c<TrMetaData[s].cPKs; c++) 453 sSQL += TrMetaData[s].PrimaryKeys[c] + "=? AND "; 454 sSQL = sSQL.substring(0, sSQL.length()-5); 455 456 if (DebugFile.trace) DebugFile.writeln ("Connection.prepareStatement(" + sSQL + ")"); 457 UpStatements[s] = oTrConn.prepareStatement(sSQL); 458 } 461 if (CONNECTED==iOrStatus || REFERENCED==iOrStatus) { 462 463 OrMetaData[s] = new DataTblDef(); 466 if (DebugFile.trace) DebugFile.writeln ("DataTblDef.readMetaData (OriginConnection, " + getRowSet(s).OriginTable + ", " + (String ) oToPKs.get(getRowSet(s).OriginTable) + ")"); 467 OrMetaData[s].readMetaData(oOrConn, getRowSet(s).OriginTable, (String ) oFromPKs.get(getRowSet(s).OriginTable)); 468 469 if (CONNECTED==iTrStatus || REFERENCED==iTrStatus) 470 iTrCols = TrMetaData[s].ColCount; 471 else 472 iTrCols = OrMetaData[s].ColCount; 473 474 if (DebugFile.trace) DebugFile.writeln ("Column count = " + String.valueOf(iTrCols)); 475 476 if (getRowSet(s).FieldList.compareTo("*")!=0) { 478 sSQL = "SELECT " + getRowSet(s).FieldList + " "; 479 } 480 else { 481 sSQL = "SELECT "; 482 for (c=0; c<iTrCols; c++) { 483 sCol = TrMetaData[s].ColNames[c]; 485 486 try { 487 oMap = (HashMap ) FieldMaps.get(s); 488 bIsMapped = oMap.containsKey(sCol); 493 if (bIsMapped) 494 sCol = (String ) oMap.get(sCol); 495 else { 496 bIsMapped = oMap.containsKey(sCol.toUpperCase()); 497 if (bIsMapped) 498 sCol = (String ) oMap.get(sCol.toUpperCase()); 499 else { 500 bIsMapped = oMap.containsKey(sCol.toLowerCase()); 501 if (bIsMapped) 502 sCol = (String ) oMap.get(sCol.toLowerCase()); 503 } 504 } 505 } 506 catch (ArrayIndexOutOfBoundsException e) { bIsMapped=false; } 507 508 iCol = OrMetaData[s].findColumnPosition(sCol); 510 if (iCol!=-1) 511 sSQL += sCol + ((c<iTrCols-1) ? "," : " "); 512 else { 513 try { 514 oMap = (HashMap ) FieldDefs.get(s); 515 bHasDefault = oMap.containsKey(sCol); 516 } 517 catch (ArrayIndexOutOfBoundsException e) { 518 bHasDefault = false; 522 oMap = null; 523 } 524 if (bHasDefault) 525 sSQL += (String ) oMap.get(sCol) + " AS " + TrMetaData[s].ColNames[c] + ((c<iTrCols-1) ? "," : " "); 527 else 528 if (bIsMapped) 529 sSQL += sCol + " AS " + TrMetaData[s].ColNames[c] + ((c<iTrCols-1) ? "," : " "); 530 else 531 sSQL += "NULL AS " + TrMetaData[s].ColNames[c] + ((c<iTrCols-1) ? "," : " "); 533 } } } 537 sSQL += "FROM " + getRowSet(s).OriginTable; 538 if (!isEmpty(getRowSet(s).WhereClause)) { 539 if (getRowSet(s).WhereClause.trim().toUpperCase().startsWith("START")) 540 sSQL += " " + getRowSet(s).WhereClause; 541 else 542 sSQL += " WHERE " + getRowSet(s).WhereClause; 543 } 545 if (DebugFile.trace) DebugFile.writeln ("Connection.prepareStatement(" + sSQL + ")"); 546 547 oSelectTexts.put (getRowSet(s).OriginTable, sSQL); 548 549 OrStatements[s] = oOrConn.prepareStatement(sSQL, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 550 } } 554 if (CONNECTED==iOrStatus || REFERENCED==iOrStatus) bOrPrepared = true; 555 if (CONNECTED==iTrStatus || REFERENCED==iTrStatus) bTrPrepared = true; 556 557 if (DebugFile.trace) { 558 DebugFile.decIdent(); 559 DebugFile.writeln ("End DataStruct.prepareStatements()"); 560 } 561 } 563 565 public Object convert (Object oValue, int iSQLType) { 566 568 Object oRetVal; 569 String sClass; 570 571 if (null==oValue) { 572 oRetVal = null; 573 } 574 else { 575 sClass = oValue.getClass().getName(); 576 577 if (sClass.equals("java.lang.Short") || 578 sClass.equals("java.lang.Integer") || 579 sClass.equals("java.math.BigDecimal")) { 580 if (java.sql.Types.VARCHAR == iSQLType || java.sql.Types.CHAR == iSQLType || 581 java.sql.Types.LONGVARCHAR == iSQLType) 582 oRetVal = oValue.toString(); 583 else if (java.sql.Types.DECIMAL == iSQLType || 584 java.sql.Types.NUMERIC == iSQLType) 585 oRetVal = new BigDecimal (oValue.toString()); 586 else if (java.sql.Types.INTEGER == iSQLType) 587 oRetVal = new Integer (oValue.toString()); 588 else if (java.sql.Types.SMALLINT == iSQLType) 589 oRetVal = new Short (oValue.toString()); 590 else 591 oRetVal = oValue; 592 } 593 else if (sClass.equals("java.lang.String")) { 594 if (java.sql.Types.DECIMAL == iSQLType || 595 java.sql.Types.NUMERIC == iSQLType ) 596 oRetVal = new BigDecimal (oValue.toString()); 597 else if (java.sql.Types.SMALLINT == iSQLType) 598 oRetVal = new Short (oValue.toString()); 599 else if (java.sql.Types.INTEGER == iSQLType) { 600 String str = oValue.toString(); 601 oRetVal = new Integer (str); 602 } 603 else 604 oRetVal = oValue; 605 } 606 else if (sClass.equals("java.sql.Timestamp")) { 607 oRetVal = new java.sql.Date (((Timestamp )oValue).getTime()); 608 } 609 else 610 oRetVal = oValue; 611 } 612 return oRetVal; 613 } 615 617 public int mapType(int iSQLType) { 618 621 int iRetType; 622 623 switch(iSQLType) { 624 case java.sql.Types.TIMESTAMP: 625 iRetType = java.sql.Types.DATE; 626 break; 627 default: 628 iRetType = iSQLType; 629 } 630 return iRetType; 631 } 633 635 667 668 670 protected void getRows(Object [] OrPK, Object [] TrPK, int cParams, int iTable) throws SQLException { 671 674 int iPK; 675 int iFetchBurst = 500; int iSQLType; 677 int cTransforms; 678 Vector oRow; 679 HashMap oTransforms; 680 Object oOriginalValue; 681 Object oTransformedValue; 682 ResultSet oRSet; 683 ResultSetMetaData oRDat; 684 DataRowSet oDatR; 685 String sColName; 686 DataTblDef oMDat = OrMetaData[iTable]; 687 DataTransformation oDatT; 688 PreparedStatement oStmt = OrStatements[iTable]; 689 PreparedStatement oStmt2; 690 691 693 oDatR = getRowSet(iTable); 694 if (oDatR.WhereClause!=null) { 696 if (oDatR.WhereClause.indexOf("?")>0) { 697 for (int p=0;p<cParams; p++) { 698 if (DebugFile.trace) DebugFile.writeln ("binding query input parameter " + String.valueOf(p+1)); 699 oStmt.setObject(p+1, OrPK[p]); 700 } } } 704 if (DebugFile.trace) DebugFile.writeln ("PreparedStatement.executeQuery()"); 705 706 oRSet = oStmt.executeQuery(); 708 713 714 oDatR = null; 715 716 if (DebugFile.trace) { 717 if (DataRowSets.get(iTable)!=null) 718 DebugFile.writeln ("FieldList=" + getRowSet(iTable).FieldList); 719 else 720 DebugFile.writeln ("ERROR: getRowSet(" + String.valueOf(iTable) + ") == null"); 721 } 722 723 if (getRowSet(iTable).FieldList.compareTo("*")!=0) 727 iCols = oRSet.getMetaData().getColumnCount(); 728 else 729 iCols = TrMetaData[iTable].ColCount; 730 731 if (DebugFile.trace) DebugFile.writeln ("reading " + String.valueOf(iCols) + " columns"); 732 733 oResults = new Vector (iFetchBurst, iFetchBurst); 735 736 if (DebugFile.trace) DebugFile.writeln ("new Vector(" + String.valueOf(iFetchBurst) + ")"); 737 738 iRows = 0; 739 740 try { 741 oTransforms = (HashMap ) Transformations.get(iTable); 743 cTransforms = oTransforms.size(); 744 } 745 catch (ArrayIndexOutOfBoundsException e) { 746 if (DebugFile.trace) DebugFile.writeln ("table has no transformation replacements"); 747 oTransforms = null; 748 cTransforms = 0; 749 } 750 751 if (0 == cTransforms) { 752 while (oRSet.next() && iRows<iFetchBurst) { 754 iRows++; 755 if (DebugFile.trace) DebugFile.writeln ("caching row " + String.valueOf(iRows)); 756 oRow = new Vector (iCols); 757 for (int c=1; c<=iCols; c++) 758 oRow.add(oRSet.getObject(c)); 759 oResults.add(oRow); 760 } } 762 else { oRDat = oRSet.getMetaData(); while (oRSet.next() && iRows<iFetchBurst) { 765 iRows++; 766 if (DebugFile.trace) DebugFile.writeln ("caching row " + String.valueOf(iRows)); 767 oRow = new Vector (iCols); 769 iPK = 0; 771 for (int c=1; c<=iCols; c++) { 772 try { 773 sColName = oRDat.getColumnName(c); 777 778 oDatT = (DataTransformation) oTransforms.get(sColName); 779 780 if (null==oDatT) 782 oRow.add(oRSet.getObject(c)); 783 else { 784 oOriginalValue = oRSet.getObject(c); 785 786 oTransformedValue = oDatT.transform(getOriginConnection(),getTargetConnection(), oOriginalValue); 787 788 if (DebugFile.trace) DebugFile.writeln (sColName + " " + oOriginalValue + " transformed to " + (oTransformedValue!=null ? oTransformedValue : "NULL")); 789 790 oRow.add(oTransformedValue); 791 } 792 } 793 catch (ArrayIndexOutOfBoundsException e) { 794 oRow.add(oRSet.getObject(c)); 795 } 796 } oResults.add(oRow); 798 } oRDat = null; 800 } 802 if (DebugFile.trace) DebugFile.writeln ("row count = " + String.valueOf(iRows)); 803 804 oRSet.close(); 805 oRSet = null; 806 } 808 810 public void insert(Object [] OrPK, Object [] TrPK, int cParams) 811 throws SQLException ,NullPointerException { 812 816 String sField; 817 DataTblDef oMDat; 818 PreparedStatement oInsrt; 819 StringReader oReader; 820 Object oValue; 821 String sValue; 822 int r; 823 int q; 824 int iPK; 825 int iSQLType; 826 827 if (DebugFile.trace) { 828 DebugFile.writeln ("Begin DataStruct.insert(OrPK[], TrPK[], " + String.valueOf(cParams) + ")"); 829 DebugFile.incIdent(); 830 } 831 832 execCommands("INIT", -1, OrPK, cParams); 833 834 if (!bTrPrepared || !bTrPrepared) prepareStatements(); 836 837 for (int s=0; s<cTables; s++) { 839 if (DebugFile.trace) DebugFile.writeln ("processing rowset from " + getRowSet(s).OriginTable + " to " + getRowSet(s).TargetTable); 840 841 execCommands("BEFORE", s, OrPK, cParams); 842 843 getRows(OrPK, TrPK, cParams, s); 845 oMDat = TrMetaData[s]; 846 oInsrt = TrStatements[s]; 847 848 for (r=0; r<iRows; r++) { 850 iPK = 0; 851 for (q=0; q<iCols; q++) { 853 sField = oMDat.ColNames[q]; 854 iSQLType = oMDat.ColTypes[q]; 855 oValue = getResult(r,q); 856 857 if (oMDat.isPrimaryKey(q)) { 858 if (iPK<cParams && oMDat.inheritsPK(TrMetaData[0])) 859 if (null!=TrPK[iPK]) { 860 if (oValue.getClass().equals(TrPK[iPK].getClass()) && 861 oMDat.bestMatch(q, TrMetaData[0], iPK)) { 862 if (DebugFile.trace) DebugFile.writeln ("swaping PK " + oValue.toString() + " to " + TrPK[iPK].toString() + " before insert"); 863 oValue = TrPK[iPK]; 864 } } iPK++; 867 } 869 if (DebugFile.trace) 870 if (oValue!=null) 871 DebugFile.writeln ("binding " + sField + "=" + oValue.toString() + " as SQLType " + String.valueOf(iSQLType)); 872 else 873 DebugFile.writeln ("binding " + sField + "=NULL as SQLType " + String.valueOf(iSQLType)); 874 875 if (iSQLType==java.sql.Types.LONGVARCHAR) { 876 sValue = oValue.toString() + " "; 877 oReader = new StringReader (sValue); 878 oInsrt.setCharacterStream(q+1, oReader, sValue.length()-1); 879 } 880 else 881 oInsrt.setObject(q+1, convert(oValue, iSQLType), mapType(iSQLType)); 882 } 884 if (DebugFile.trace) DebugFile.writeln ("PreparedStatement.execute()"); 885 oInsrt.execute(); 886 } 888 oResults.clear(); 889 oResults = null; 890 891 execCommands("AFTER", s, OrPK, cParams); 892 } 894 execCommands("TERM", -1, OrPK, cParams); 895 896 if (DebugFile.trace) { 897 DebugFile.decIdent(); 898 DebugFile.writeln ("End DataStruct.insert()"); 899 } 900 } 902 904 public void update(Object [] OrPK, Object [] TrPK, int cParams) throws SQLException { 905 909 String sField; 910 DataTblDef oMDat; 911 PreparedStatement oInsrt; 912 PreparedStatement oUpdt; 913 Object oValue; 914 String sValue; 915 StringReader oReader; 916 int r; 917 int q; 918 int iPK; 919 int cUpdated; 920 int iSQLType; 921 922 if (DebugFile.trace) { 923 DebugFile.writeln ("Begin DataStruct.update(OrPK[], TrPK[], " + String.valueOf(cParams) + ")"); 924 DebugFile.incIdent(); 925 } 926 927 execCommands("INIT", -1, OrPK, cParams); 928 929 if (!bTrPrepared || !bTrPrepared) prepareStatements(); 931 932 for (int s=0; s<cTables; s++) { 934 if (DebugFile.trace) DebugFile.writeln ("processing rowset from " + getRowSet(s).OriginTable + " to " + getRowSet(s).TargetTable); 935 936 execCommands("BEFORE", s, OrPK, cParams); 937 938 getRows(OrPK, TrPK, cParams, s); 940 oMDat = TrMetaData[s]; 941 oUpdt = UpStatements[s]; 942 943 for (r=0; r<iRows; r++) { 945 iPK = 0; 947 if (oMDat.ColCount>oMDat.cPKs) { 948 949 for (q=0; q<iCols; q++) { 951 sField = oMDat.ColNames[q]; iSQLType = oMDat.ColTypes[q]; 954 if (oMDat.isPrimaryKey(q)) { 955 if (iPK<cParams && oMDat.inheritsPK(TrMetaData[0])) { 956 if (null!=TrPK[iPK]) { 957 if (getResult(r,q).getClass().equals(TrPK[iPK].getClass()) && 958 oMDat.bestMatch(q, TrMetaData[0], iPK)) { 959 if (DebugFile.trace) DebugFile.writeln("swaping PK " + getResult(r, q).toString() + " to " + TrPK[iPK].toString() + " before update"); 960 oValue = TrPK[iPK]; 961 } 962 else { 963 oValue = getResult(r,q); 964 } } 966 else { 967 oValue = getResult(r, q); 968 } } 970 else { 971 oValue = getResult(r,q); 972 } 974 if (DebugFile.trace) 975 if (oValue==null) 976 DebugFile.writeln ("binding " + sField + "=null as SQLType " + String.valueOf(iSQLType) + " at parameter " + String.valueOf(iCols-oMDat.cPKs+iPK+1)); 977 else 978 DebugFile.writeln ("binding " + sField + "=" + oValue.toString() + " as SQLType " + String.valueOf(iSQLType) + " at parameter " + String.valueOf(iCols-oMDat.cPKs+iPK+1)); 979 980 oUpdt.setObject(iCols-oMDat.cPKs+iPK+1, convert(oValue, iSQLType), mapType(iSQLType)); 981 iPK++; 982 } 983 else { 984 if (DebugFile.trace) DebugFile.writeln ("binding " + sField + " as SQLType " + String.valueOf(iSQLType) + " at parameter " + String.valueOf(q+1-iPK)); 985 986 if (iSQLType==java.sql.Types.LONGVARCHAR) { 987 sValue = getResult(r,q).toString() + " "; 988 oReader = new StringReader (sValue); 989 oUpdt.setCharacterStream(q+1-iPK, oReader, sValue.length()-1); 990 } 991 else 992 oUpdt.setObject(q+1-iPK, convert(getResult(r,q), iSQLType), mapType(iSQLType)); 993 } 994 } 996 if (DebugFile.trace) DebugFile.writeln ("PreparedStatement.executeUpdate()"); 997 cUpdated = oUpdt.executeUpdate(); 998 999 if (DebugFile.trace) DebugFile.writeln (String.valueOf(cUpdated) + " rows updated"); 1000 } 1001 else { 1002 cUpdated = 0; 1003 if (DebugFile.trace) 1004 DebugFile.writeln ("pk count=" + String.valueOf(oMDat.cPKs) + " column count=" + String.valueOf(oMDat.ColCount) + " row not updated because no non-pk columns found"); 1005 } 1006 1008 if (0==cUpdated) { 1009 oInsrt = TrStatements[s]; 1010 iPK = 0; 1011 for (q=0; q<iCols; q++) { 1013 sField = oMDat.ColNames[q]; 1014 iSQLType = oMDat.ColTypes[q]; 1015 oValue = getResult(r,q); 1016 1017 if (oMDat.isPrimaryKey(q)) { 1018 if (iPK<cParams && oMDat.inheritsPK(TrMetaData[0])) { 1019 if (null!=TrPK[iPK]) { 1020 if (oValue.getClass().equals(TrPK[iPK].getClass()) && 1021 oMDat.bestMatch(q, TrMetaData[0], iPK)) { 1022 if (DebugFile.trace) DebugFile.writeln ("swaping PK " + oValue.toString() + " to " + TrPK[iPK].toString()); 1023 oValue = TrPK[iPK]; 1024 } 1025 } 1026 } iPK++; 1028 } 1030 if (DebugFile.trace) DebugFile.writeln ("binding " + sField + " as SQLType " + String.valueOf(iSQLType)); 1031 1032 if (iSQLType==java.sql.Types.LONGVARCHAR) { 1033 sValue = oValue.toString() + " "; 1034 oReader = new StringReader (sValue); 1035 oInsrt.setCharacterStream(q+1, oReader, sValue.length()-1); 1036 } 1037 else 1038 oInsrt.setObject(q+1, convert(oValue, iSQLType), mapType(iSQLType)); 1039 } 1041 if (DebugFile.trace) DebugFile.writeln ("PreparedStatement.execute()"); 1042 oInsrt.execute(); 1043 } } 1046 oResults.clear(); 1047 oResults = null; 1048 1049 execCommands("AFTER", s, OrPK, cParams); 1050 } 1052 execCommands("TERM", -1, OrPK, cParams); 1053 1054 if (DebugFile.trace) { 1055 DebugFile.decIdent(); 1056 DebugFile.writeln ("End DataStruct.update()"); 1057 } 1058 } 1060 1062 protected void seekReferal(DataTransformation oTransform) { 1063 DataTransformation oTransformRef; 1064 HashMap oTransformsRef; 1065 Set oTransformSet; 1066 Iterator oSetIterator; 1067 String sChildTable; 1068 String sReferedTable = oTransform.ReferedTable; 1069 1070 if (DebugFile.trace) { 1071 DebugFile.writeln ("Begin DataStruct.seekReferal(ReferedTable=" + sReferedTable + ", ReferedField=" + oTransform.ReferedField + ")"); 1072 DebugFile.incIdent(); 1073 } 1074 1075 for (int r=0; r<DataRowSets.size(); r++) { 1076 sChildTable = getRowSet(r).OriginTable; 1080 1081 if (sChildTable.equalsIgnoreCase(sReferedTable)) { 1082 oTransformsRef = (HashMap ) Transformations.get(r); 1083 oTransformSet = oTransformsRef.keySet(); 1085 oSetIterator = oTransformSet.iterator(); 1086 while (oSetIterator.hasNext()) { 1087 oTransformRef = (DataTransformation) oTransformsRef.get(oSetIterator.next()); 1088 if (oTransformRef.OriginField.equalsIgnoreCase(oTransform.ReferedField)) { 1089 oTransform.setReferedValues(oTransformRef); 1090 if (DebugFile.trace) DebugFile.writeln (oTransform.OriginTable + " references " + oTransform.ReferedTable + "." + oTransform.ReferedField); 1091 } } } } 1096 if (DebugFile.trace) { 1097 DebugFile.decIdent(); 1098 DebugFile.writeln ("End DataStruct.seekReferal()"); 1099 } 1100 } 1102 1104 1108 1109 public void startDocument() throws SAXException { 1110 1111 if (DebugFile.trace) { 1112 DebugFile.writeln ("Begin DataStruct.startDocument()"); 1113 DebugFile.incIdent(); 1114 } 1115 1116 fElements = 0; 1117 fCharacters = 0; 1118 sContext = ""; 1119 sNode = ""; 1120 oFromPKs = new HashMap (5,3); 1121 oToPKs = new HashMap (5,3); 1122 1123 InitStmts = new LinkedList (); 1124 TermStmts = new LinkedList (); 1125 1126 if (DebugFile.trace) { 1127 DebugFile.decIdent(); 1128 DebugFile.writeln ("End DataStruct.startDocument()"); 1129 } 1130 } 1132 1134 public void startElement(String uri, String local, String raw, 1135 Attributes attrs) throws SAXException { 1136 fElements++; 1137 sChars = ""; 1138 1139 if (local.equalsIgnoreCase("ROWSET")) { 1140 oCurrRowSet = new DataRowSet(); 1141 DataRowSets.add(oCurrRowSet); 1142 cTables++; 1143 1144 oCurrMap = new HashMap (13,5); 1145 FieldMaps.add(oCurrMap); 1146 1147 oCurrTransform = new HashMap (5,3); 1148 Transformations.add(oCurrTransform); 1149 1150 oCurrDef = new HashMap (13,5); 1151 FieldDefs.add(oCurrDef); 1152 1153 oCurrBef = new LinkedList (); 1154 Before.add(oCurrBef); 1155 1156 oCurrAft = new LinkedList (); 1157 After.add(oCurrAft); 1158 } 1160 if (local.equalsIgnoreCase("ACTION") || 1161 local.equalsIgnoreCase("MAPPINGS") || 1162 local.equalsIgnoreCase("DEFVALS") || 1163 local.equalsIgnoreCase("NULLVALS") || 1164 local.equalsIgnoreCase("BEFORE") || 1165 local.equalsIgnoreCase("AFTER") || 1166 local.equalsIgnoreCase("INIT") || 1167 local.equalsIgnoreCase("TERM") ) 1168 sNode = sContext = local.toUpperCase(); 1169 else 1170 sNode = local.toUpperCase(); 1171 1172 if ((sNode.equals("MAPPING") && attrs.getLength()>0) || 1173 (sNode.equals("DEFVAL") && attrs.getLength()>0)) 1174 sTransform = attrs.getValue(0); 1175 else 1176 sTransform = null; 1177 1178 } 1180 1182 public void endElement(String uri, String localName, String qname) throws SAXException { 1183 int iComma; 1184 String sOrFld; 1185 String sOrVal; 1186 String sTrFld; 1187 DataTransformation oTransform; 1188 1189 if (sContext.equalsIgnoreCase("ACTION")) { 1190 if (sNode.equalsIgnoreCase("FROM")) 1191 oCurrRowSet.OriginTable = sChars.trim(); 1192 else if (sNode.equalsIgnoreCase("TO")) 1193 oCurrRowSet.TargetTable = sChars.trim(); 1194 else if (sNode.equalsIgnoreCase("JOIN")) 1195 oCurrRowSet.JoinTables = sChars; 1196 else if (sNode.equalsIgnoreCase("WHERE")) 1197 oCurrRowSet.WhereClause = sChars; 1198 else if (sNode.equalsIgnoreCase("ERASE")) 1199 oCurrRowSet.EraseClause = sChars; 1200 else if (sNode.equalsIgnoreCase("FIELDLIST")) 1201 oCurrRowSet.FieldList = sChars; 1202 else if (sNode.equalsIgnoreCase("FROM_PK")) 1203 oFromPKs.put(oCurrRowSet.OriginTable, sChars.trim()); 1204 else if (sNode.equalsIgnoreCase("TO_PK")) 1205 oToPKs.put(oCurrRowSet.TargetTable, sChars.trim()); 1206 } else if (sContext.equalsIgnoreCase("MAPPINGS")) { 1208 if (sNode.equalsIgnoreCase("MAPPING")) { 1209 iComma = sChars.lastIndexOf(","); 1210 sOrFld = sChars.substring(0, iComma).trim(); 1211 sTrFld = sChars.substring(iComma+1).trim(); 1212 oCurrMap.put(sTrFld, sOrFld); 1213 1214 if (null!=sTransform) { 1217 oTransform = new DataTransformation(sTransform, 1219 oCurrRowSet.OriginTable, sOrFld, 1220 oCurrRowSet.TargetTable, sTrFld); 1221 1222 oCurrTransform.put (sOrFld, oTransform); 1224 1225 if (oTransform.OperationCode==DataTransformation.Operations.REFER) 1228 seekReferal( oTransform); 1229 } } } else if (sContext.equalsIgnoreCase("DEFVALS")) { 1233 if (sNode.equalsIgnoreCase("DEFVAL")) { 1234 iComma = sChars.indexOf(","); 1235 sTrFld = sChars.substring(0, iComma).trim(); 1236 sOrFld = sChars.substring(iComma+1).trim(); 1237 oCurrDef.put(sTrFld, sOrFld); 1238 if (null!=sTransform) { 1240 oTransform = new DataTransformation(sTransform, 1241 oCurrRowSet.OriginTable, sTrFld, 1242 oCurrRowSet.TargetTable, sTrFld); 1243 oCurrTransform.put (sTrFld, oTransform); 1245 if (oTransform.OperationCode==DataTransformation.Operations.REFER) 1248 seekReferal( oTransform); 1249 } } } else if (sContext.equalsIgnoreCase("BEFORE")) { 1253 if (localName.equalsIgnoreCase("EXEC") || 1254 localName.equalsIgnoreCase("CALL")) 1255 oCurrBef.addLast(sChars); 1256 } else if (sContext.equalsIgnoreCase("AFTER")) { 1258 if (localName.equalsIgnoreCase("EXEC") || 1259 localName.equalsIgnoreCase("CALL")) 1260 oCurrAft.addLast(sChars); 1261 } else if (sContext.equalsIgnoreCase("INIT")) { 1263 if (localName.equalsIgnoreCase("EXEC") || 1264 localName.equalsIgnoreCase("CALL")) 1265 InitStmts.addLast(sChars); 1266 } else if (sContext.equalsIgnoreCase("TERM")) { 1268 if (localName.equalsIgnoreCase("EXEC") || 1269 localName.equalsIgnoreCase("CALL")) 1270 TermStmts.addLast(sChars); 1271 } } 1274 1276 1277 public void characters(char ch[], int start, int length) throws SAXException { 1278 fCharacters += length; 1279 sChars += new String (ch,start,length); 1280 } 1282 1284 1288 1289 public void warning(SAXParseException ex) throws SAXException { 1290 if (DebugFile.trace) DebugFile.write(composeError("Warning", ex)); 1291 } 1293 1294 public void error(SAXParseException ex) throws SAXException { 1295 if (DebugFile.trace) DebugFile.write(composeError("Error", ex)); 1296 throw ex; 1297 } 1299 1300 public void fatalError(SAXParseException ex) throws SAXException { 1301 if (DebugFile.trace) DebugFile.write(composeError("Fatal Error", ex)); 1302 throw ex; 1303 } 1305 1307 1311 1312 protected String composeError(String type, SAXParseException ex) { 1313 String sErrDesc = ""; 1314 String systemId = null; 1315 int index; 1316 1317 sErrDesc += "[SAX " + type + "] "; 1318 1319 if (ex==null) 1320 sErrDesc += "!!!"; 1321 else 1322 systemId = ex.getSystemId(); 1323 1324 if (systemId != null) { 1325 index = systemId.lastIndexOf('/'); 1326 if (index != -1) systemId = systemId.substring(index + 1); 1327 sErrDesc += systemId; 1328 } 1329 1330 sErrDesc += " Line:" + ex.getLineNumber(); 1331 sErrDesc += " Column:" + ex.getColumnNumber(); 1332 sErrDesc += " Cause: " + ex.getMessage(); 1333 sErrDesc += "\n"; 1334 1335 return sErrDesc; 1336 } 1338 1340 1344 public void parse(String sXMLFile) throws InstantiationException ,IllegalAccessException ,ClassNotFoundException ,IOException ,SAXException { 1345 Properties oProps = new Properties (); 1346 parse(sXMLFile, oProps); 1347 } 1348 1349 public void parse(String sXMLFile, Properties oProps) throws InstantiationException ,IllegalAccessException ,ClassNotFoundException ,IOException ,SAXException { 1350 1352 XMLReader parser; 1354 Parser sax1Parser; 1355 File oFile; 1356 FileReader oFileRead; 1357 BufferedReader oBuff; 1358 StringBufferInputStream oStrBuff; 1359 InputSource ioSrc; 1360 FileInputStream oStream; 1361 String sXMLSource; 1362 String sParam; 1363 Enumeration oEnum; 1364 Pattern oPattern; 1365 PatternMatcher oMatcher = new Perl5Matcher(); 1366 PatternCompiler oCompiler = new Perl5Compiler(); 1367 byte byBuffer[]; 1368 1369 if (DebugFile.trace) { 1370 DebugFile.writeln ("Begin DataStruct.parse(" + sXMLFile + ")"); 1371 DebugFile.incIdent(); 1372 } 1373 1374 try { 1375 if (DebugFile.trace) DebugFile.writeln ("XMLReaderFactory.createXMLReader(" + DEFAULT_PARSER_NAME + ")"); 1376 1377 parser = XMLReaderFactory.createXMLReader(DEFAULT_PARSER_NAME); 1378 } 1379 catch (Exception e) { 1380 if (DebugFile.trace) DebugFile.writeln ("ParserFactory.makeParser(" + DEFAULT_PARSER_NAME + ")"); 1381 1382 sax1Parser = ParserFactory.makeParser(DEFAULT_PARSER_NAME); 1383 1384 parser = new ParserAdapter(sax1Parser); 1385 if (DebugFile.trace) 1386 DebugFile.writeln("warning: Features and properties not supported on SAX1 parsers."); 1387 } 1388 try { 1389 parser.setFeature(NAMESPACES_FEATURE_ID, DEFAULT_NAMESPACES); 1390 parser.setFeature(VALIDATION_FEATURE_ID, DEFAULT_VALIDATION); 1391 } 1392 catch (SAXException e) { 1393 } 1394 1395 parser.setContentHandler(this); 1397 parser.setErrorHandler(this); 1398 1399 oEnum = oProps.keys(); 1400 if (sXMLFile.startsWith("<?xml")) { 1401 while (oEnum.hasMoreElements()) { 1403 sParam = (String ) oEnum.nextElement(); 1404 try { 1405 oPattern = oCompiler.compile("{#" + sParam + "}"); 1406 } catch (MalformedPatternException e) { oPattern=null; } 1407 1408 sXMLFile = Util.substitute(oMatcher, oPattern, 1409 new Perl5Substitution(oProps.getProperty(sParam), Perl5Substitution.INTERPOLATE_ALL), 1410 sXMLFile, Util.SUBSTITUTE_ALL); 1411 } 1413 oStrBuff = new StringBufferInputStream (sXMLFile); 1414 ioSrc = new InputSource(oStrBuff); 1415 parser.parse(ioSrc); 1416 oStrBuff.close(); 1417 } 1418 else { 1419 if (oProps.isEmpty()) { 1420 oFileRead = new FileReader (sXMLFile); 1421 oBuff = new BufferedReader (oFileRead, 32767); 1422 ioSrc = new InputSource(oBuff); 1423 parser.parse(ioSrc); 1424 oBuff.close(); 1425 oFileRead.close(); 1426 } 1427 else { 1428 oFile = new File (sXMLFile); 1429 byBuffer = new byte[new Long (oFile.length()).intValue()]; 1430 1431 oStream = new FileInputStream (oFile); 1432 oStream.read(byBuffer); 1433 sXMLSource = new String (byBuffer); 1434 oStream.close(); 1435 1436 while (oEnum.hasMoreElements()) { 1437 sParam = (String ) oEnum.nextElement(); 1438 try { 1439 oPattern = oCompiler.compile("{#" + sParam + "}"); 1440 } catch (MalformedPatternException e) { oPattern=null; } 1441 1442 sXMLSource = Util.substitute(oMatcher, oPattern, 1443 new Perl5Substitution(oProps.getProperty(sParam), Perl5Substitution.INTERPOLATE_ALL), 1444 sXMLSource, Util.SUBSTITUTE_ALL); 1445 } 1447 oStrBuff = new StringBufferInputStream (sXMLSource); 1448 ioSrc = new InputSource(oStrBuff); 1449 parser.parse(ioSrc); 1450 oStrBuff.close(); 1451 } 1452 } 1453 1454 if (DebugFile.trace) { 1455 DebugFile.decIdent(); 1456 DebugFile.writeln ("End DataStruct.parse()"); 1457 } 1458 } 1460 1462 public void createClassSource(String sPackage, String sSubClassName, String sFile) throws IOException { 1463 FileWriter oFile = new FileWriter (sFile); 1464 DataTransformation oDatT; 1465 Iterator oIter; 1466 LinkedList oList; 1467 Object oPKs; 1468 String sItem; 1469 1470 oFile.write("package " + sPackage + ";\n"); 1471 oFile.write("import java.util.Vector;\n"); 1472 oFile.write("import java.util.LinkedList;\n"); 1473 oFile.write("import java.util.ListIterator;\n"); 1474 oFile.write("import java.util.HashMap;\n"); 1475 oFile.write("import java.util.Iterator;\n"); 1476 oFile.write("import com.knowgate.datacopy.DataRowSet;\n"); 1477 oFile.write("import com.knowgate.datacopy.DataStruct;\n"); 1478 oFile.write("import com.knowgate.datacopy.DataTransformation;\n"); 1479 oFile.write("\n"); 1480 1481 oFile.write("public class " + sSubClassName + " extends DataStruct {\n"); 1482 oFile.write(" public " + sSubClassName + "() {\n"); 1483 oFile.write(" DataRowSet oRowSet;\n"); 1484 oFile.write(" DataTransformation oTransForm;\n"); 1485 oFile.write(" LinkedList oBefore;\n"); 1486 oFile.write(" LinkedList oAfter;\n"); 1487 oFile.write(" HashMap oMappings;\n"); 1488 oFile.write(" HashMap oDefaults;\n"); 1489 oFile.write(" HashMap oTransforms;\n\n"); 1490 oFile.write(" InitStmts = new LinkedList();\n"); 1491 oFile.write(" TermStmts = new LinkedList();\n"); 1492 oFile.write(" oToPKs = new HashMap();\n"); 1493 oFile.write(" oFromPKs = new HashMap();\n"); 1494 oFile.write(" cTables = " + String.valueOf(DataRowSets.size()) + ";\n"); 1495 oFile.write("\n"); 1496 1497 oIter = InitStmts.listIterator(); 1498 while (oIter.hasNext()) 1499 oFile.write(" InitStmts.addLast(\"" + oIter.next().toString() + "\");\n"); 1500 oFile.write("\n"); 1501 1502 oIter = TermStmts.listIterator(); 1503 while (oIter.hasNext()) 1504 oFile.write(" TermStmts.addLast(\"" + oIter.next().toString() + "\");\n"); 1505 oFile.write("\n"); 1506 1507 for (int c=0;c<cTables;c++) { 1508 oCurrRowSet = getRowSet(c); 1509 1510 oFile.write(" oRowSet = new DataRowSet(\"" + oCurrRowSet.OriginTable + "\",\"" + oCurrRowSet.TargetTable + "\",\"" + oCurrRowSet.JoinTables + "\",\"" + oCurrRowSet.WhereClause + "\",\"" + oCurrRowSet.EraseClause + "\");\n"); 1511 oFile.write(" oRowSet.FieldList = \"" + oCurrRowSet.FieldList.trim() + "\";\n"); 1512 oFile.write(" DataRowSets.add(oRowSet);\n"); 1513 oFile.write(" oBefore = new LinkedList();\n"); 1514 oFile.write(" oAfter = new LinkedList();\n"); 1515 oFile.write(" oMappings = new HashMap();\n"); 1516 oFile.write(" oDefaults = new HashMap();\n"); 1517 oFile.write(" oTransforms = new HashMap();\n"); 1518 1519 oPKs = oFromPKs.get(oCurrRowSet.OriginTable); 1520 if (null!=oPKs) 1521 oFile.write(" oFromPKs.put(\"" + oCurrRowSet.OriginTable + "\",\"" + oPKs.toString() + "\");\n"); 1522 oPKs = oToPKs.get(oCurrRowSet.TargetTable); 1523 if (null!=oPKs) 1524 oFile.write(" oToPKs.put(\"" + oCurrRowSet.TargetTable + "\",\"" + oPKs.toString() + "\");\n\n"); 1525 1526 oList = (LinkedList ) Before.get(c); 1527 if (oList.size()>0) { 1528 oIter = oList.iterator(); 1529 while (oIter.hasNext()) 1530 oFile.write(" oBefore.addLast(\"" + oIter.next().toString() + "\");\n"); 1531 oIter = null; 1532 } oList = null; 1534 oFile.write(" Before.add(oBefore);\n"); 1535 1536 oList = (LinkedList ) After.get(c); 1537 if (oList.size()>0) { 1538 oIter = oList.iterator(); 1539 while (oIter.hasNext()) 1540 oFile.write(" oAfter.addLast(\"" + oIter.next().toString() + "\");\n"); 1541 oIter = null; 1542 } oList = null; 1544 oFile.write(" After.add(oAfter);\n"); 1545 1546 try { 1547 oCurrMap = (HashMap ) FieldMaps.get(c); 1548 oIter = oCurrMap.keySet().iterator(); 1549 while (oIter.hasNext()) { 1550 sItem = (String ) oIter.next(); 1551 oFile.write(" oMappings.put(\"" + sItem + "\",\"" + oCurrMap.get(sItem).toString() + "\");\n"); 1552 } } catch (ArrayIndexOutOfBoundsException e) { } 1554 1555 oFile.write(" FieldMaps.add(oMappings);\n"); 1556 1557 try { 1558 oFile.write(" Transformations.add(oTransforms);\n"); 1559 oCurrTransform = (HashMap ) Transformations.get(c); 1560 oIter = oCurrTransform.keySet().iterator(); 1561 while (oIter.hasNext()) { 1562 sItem = oIter.next().toString(); 1563 oDatT = (DataTransformation) oCurrTransform.get(sItem); 1564 oFile.write(" oTransForm = new DataTransformation(" + String.valueOf(oDatT.OperationCode) + "," + 1565 "\"" + oDatT.OriginTable + "\",\"" + oDatT.OriginField + "\"," + 1566 "\"" + oDatT.TargetTable + "\",\"" + oDatT.TargetField + "\"," + 1567 (null==oDatT.ReferedTable ? "null" : "\"" + oDatT.ReferedTable + "\"") + "," + 1568 (null==oDatT.ReferedField ? "null" : "\"" + oDatT.ReferedField + "\"") + "," + 1569 (null==oDatT.IfNullValue ? "null" : "\"" + oDatT.IfNullValue + "\"") + ");\n"); 1570 oFile.write(" oTransforms.put(\"" + sItem + "\", oTransForm);\n"); 1571 if (oDatT.OperationCode==DataTransformation.Operations.REFER) oFile.write(" seekReferal(oTransForm);\n"); 1572 } } catch (ArrayIndexOutOfBoundsException e) { } 1574 1575 try { 1576 oCurrDef = (HashMap ) FieldDefs.get(c); 1577 oIter = oCurrDef.keySet().iterator(); 1578 while (oIter.hasNext()) { 1579 sItem = (String ) oIter.next(); 1580 oFile.write(" oMappings.put(\"" + sItem + "\",\"" + oCurrDef.get(sItem).toString() + "\");\n"); 1581 } } catch (ArrayIndexOutOfBoundsException e) { } 1583 1584 oFile.write(" FieldDefs.add(oDefaults);\n"); 1585 1586 oFile.write("\n"); 1587 } 1589 oFile.write(" }\n}"); 1590 1591 oFile.close(); 1592 oFile = null; 1593 } 1595 1597 private boolean bOrPrepared; private boolean bTrPrepared; private Connection oOrConn; private Connection oTrConn; private int iOrStatus; private int iTrStatus; 1604 protected Vector oResults; protected int iCols; protected int iRows; 1608 1611 protected int cTables; protected Vector FieldMaps; protected Vector FieldDefs; protected Vector DataRowSets; protected Vector Before; protected Vector After; protected LinkedList InitStmts; protected LinkedList TermStmts; protected Vector Transformations; protected HashMap oFromPKs; 1621 protected HashMap oToPKs; 1622 1623 1626 protected PreparedStatement OrStatements[]; protected PreparedStatement TrStatements[]; protected PreparedStatement UpStatements[]; protected PreparedStatement DlStatements[]; protected DataTblDef OrMetaData[]; protected DataTblDef TrMetaData[]; public HashMap oInsertTexts; 1633 public HashMap oSelectTexts; 1634 1635 1638 private String sChars; private long fElements; 1640 private long fCharacters; 1641 private String sTransform; 1642 private String sContext; 1643 private String sNode; 1644 private DataRowSet oCurrRowSet; 1645 private HashMap oCurrMap; 1646 private HashMap oCurrDef; 1647 private HashMap oCurrTransform; 1648 private LinkedList oCurrBef; 1649 private LinkedList oCurrAft; 1650 1651 private static final int DISCONNECTED = 0; 1652 private static final int CONNECTED = 1; 1653 private static final int REFERENCED = 2; 1654 1655 1657 protected static final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces"; 1658 protected static final String NAMESPACE_PREFIXES_FEATURE_ID = "http://xml.org/sax/features/namespace-prefixes"; 1659 protected static final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation"; 1660 protected static final String SCHEMA_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/schema"; 1661 protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking"; 1662 protected static final String DYNAMIC_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/dynamic"; 1663 1664 1666 protected static final String DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser"; 1667 protected static final int DEFAULT_REPETITION = 1; 1668 protected static final boolean DEFAULT_NAMESPACES = true; 1669 protected static final boolean DEFAULT_NAMESPACE_PREFIXES = false; 1670 protected static final boolean DEFAULT_VALIDATION = false; 1671 protected static final boolean DEFAULT_SCHEMA_VALIDATION = false; 1672 protected static final boolean DEFAULT_SCHEMA_FULL_CHECKING = false; 1673 protected static final boolean DEFAULT_DYNAMIC_VALIDATION = false; 1674 protected static final boolean DEFAULT_MEMORY_USAGE = false; 1675 protected static final boolean DEFAULT_TAGGINESS = false; 1676} 1677
| Popular Tags
|