1 package com.knowgate.datacopy; 2 3 import java.sql.Date ; 4 import java.sql.Timestamp ; 5 import java.sql.SQLException ; 6 7 import java.io.FileWriter ; 8 import java.io.IOException ; 9 10 import org.xml.sax.*; 11 12 import java.lang.ClassNotFoundException ; 13 14 import java.util.Vector ; 15 16 import com.knowgate.debug.DebugFile; 17 18 public class DataStructOrcl extends DataStruct { 19 20 public DataStructOrcl() { 21 } 22 23 public DataStructOrcl(String sPathXMLFile) throws ClassNotFoundException , IllegalAccessException , InstantiationException , IOException , SAXException { 24 super(sPathXMLFile); 25 } 26 27 29 private String sqlldr (Object oValue) { 30 String sClass; 31 String sRetVal; 32 Date dtValue; 33 Timestamp tsValue; 34 35 if (null==oValue) { 36 sRetVal = "NULL"; 37 } 38 else { 39 sClass = oValue.getClass().getName(); 40 41 if (sClass.equals("java.lang.String")) 42 sRetVal = "\"" + oValue.toString() + "\""; 43 else if (sClass.equals("java.util.Date") || sClass.equals("java.sql.Date")) { 44 dtValue = (Date ) oValue; 45 sRetVal = String.valueOf(dtValue.getYear()+1900) + "-"; 46 sRetVal += (dtValue.getMonth()+1<10 ? "0" + String.valueOf(dtValue.getMonth()+1) : String.valueOf(dtValue.getMonth()+1)) + "-"; 47 sRetVal += (dtValue.getDay()+1<10 ? "0" + String.valueOf(dtValue.getDay()+1) : String.valueOf(dtValue.getDay()+1)) + " "; 48 sRetVal += (dtValue.getHours()<10 ? "0" + String.valueOf(dtValue.getHours()) : String.valueOf(dtValue.getHours())) + ":"; 49 sRetVal += (dtValue.getMinutes()<10 ? "0" + String.valueOf(dtValue.getMinutes()) : String.valueOf(dtValue.getMinutes())) + ":"; 50 sRetVal += (dtValue.getSeconds()<10 ? "0" + String.valueOf(dtValue.getSeconds()) : String.valueOf(dtValue.getSeconds())); 51 dtValue = null; 52 } 53 else if (sClass.equals("java.sql.Timestamp")) { 54 tsValue = (Timestamp ) oValue; 55 sRetVal = String.valueOf(tsValue.getYear()+1900) + "-"; 56 sRetVal += (tsValue.getMonth()+1<10 ? "0" + String.valueOf(tsValue.getMonth()+1) : String.valueOf(tsValue.getMonth()+1)) + "-"; 57 sRetVal += (tsValue.getDay()+1<10 ? "0" + String.valueOf(tsValue.getDay()+1) : String.valueOf(tsValue.getDay()+1)) + " "; 58 sRetVal += (tsValue.getHours()<10 ? "0" + String.valueOf(tsValue.getHours()) : String.valueOf(tsValue.getHours())) + ":"; 59 sRetVal += (tsValue.getMinutes()<10 ? "0" + String.valueOf(tsValue.getMinutes()) : String.valueOf(tsValue.getMinutes())) + ":"; 60 sRetVal += (tsValue.getSeconds()<10 ? "0" + String.valueOf(tsValue.getSeconds()) : String.valueOf(tsValue.getSeconds())); 61 tsValue = null; 62 } 63 else { 64 sRetVal = oValue.toString(); 65 } 66 } 68 return sRetVal; 69 } 70 71 73 public void createSQLLoaderFiles(String sBasePath) throws IOException ,SQLException { 74 DataRowSet oDatR; 75 DataTblDef oTblD; 76 FileWriter oFilW; 77 78 if (DebugFile.trace) { 79 DebugFile.writeln ("Begin DataStruct.createSQLLoaderFiles(" + sBasePath + ")"); 80 DebugFile.incIdent(); 81 } 82 83 prepareStatements(); 84 85 for (int t=0; t<cTables; t++) { 86 oDatR = getRowSet(t); 87 oTblD = TrMetaData[t]; 88 oFilW = new FileWriter (sBasePath + oDatR.OriginTable + ".CTL", false); 89 oFilW.write("LOAD DATA\n"); 90 oFilW.write("INFILE *\n"); 91 oFilW.write("REPLACE INTO TABLE " + oDatR.TargetTable +"\n"); 92 oFilW.write("FIELDS TERMINATED BY \"`\" OPTIONALLY ENCLOSED BY '\"'\n"); 93 oFilW.write("(\n"); 94 for (int c=0; c<oTblD.ColCount; c++) { 95 oFilW.write(" " + oTblD.ColNames[c]); 96 if (oTblD.ColTypes[c]==java.sql.Types.DATE || oTblD.ColTypes[c]==java.sql.Types.TIMESTAMP) 97 oFilW.write(" DATE \"YYYY-MM-DD HH24-MI-SS\""); 98 oFilW.write(" NULLIF (" + oTblD.ColNames[c] + " = \"NULL\")"); 99 oFilW.write(c<oTblD.ColCount-1 ? ",\n" : ")\n"); 100 } oFilW.write("BEGINDATA\n"); 102 oFilW.close(); 103 } 105 if (DebugFile.trace) { 106 DebugFile.decIdent(); 107 DebugFile.writeln ("End DataStruct.createSQLLoaderFiles()"); 108 } 109 } 111 113 public void dump(Object [] OrPK, Object [] TrPK, int cParams, String sBasePath) throws SQLException ,IOException { 114 118 DataTblDef oMDat; 119 Object oValue; 120 int iPK; 121 FileWriter TblFiles[] = new FileWriter [cTables]; 122 123 if (DebugFile.trace) { 124 DebugFile.writeln ("Begin DataStruct.dump(OrPK[], TrPK[], " + String.valueOf(cParams) + ", " + sBasePath + ")"); 125 DebugFile.incIdent(); 126 } 127 128 for (int t=0; t<cTables; t++) 129 TblFiles[t] = new FileWriter (sBasePath + getRowSet(t).OriginTable + ".CTL", true); 130 131 execCommands("INIT", -1, OrPK, cParams); 132 133 for (int s=0; s<cTables; s++) { 135 if (DebugFile.trace) DebugFile.writeln ("processing rowset from " + getRowSet(s).OriginTable + " to " + getRowSet(s).TargetTable); 136 137 execCommands("BEFORE", s, OrPK, cParams); 138 139 getRows(OrPK, TrPK, cParams, s); 141 oMDat = TrMetaData[s]; 142 143 for (int r=0; r<iRows; r++) { 145 iPK = 0; 146 for (int q=0; q<iCols; q++) { 148 oValue = ((Vector ) oResults.get(r)).get(q); 149 150 if ((oMDat.isPrimaryKey(q))) { 151 if (iPK>=cParams) 152 TblFiles[s].write(sqlldr(oValue)); 153 else if (null==TrPK[iPK]) 154 TblFiles[s].write(sqlldr(oValue)); 155 else 156 TblFiles[s].write(sqlldr(TrPK[iPK])); 157 iPK++; 158 } 159 else 160 TblFiles[s].write(sqlldr(oValue)); 161 if (q<iCols-1) TblFiles[s].write("`"); 162 } TblFiles[s].write("\n"); 164 } 166 oResults.clear(); 167 oResults = null; 168 169 execCommands("AFTER", s, OrPK, cParams); 170 } 172 execCommands("TERM", -1, OrPK, cParams); 173 174 for (int t=0; t<cTables; t++) 175 TblFiles[t].close(); 176 177 if (DebugFile.trace) { 178 DebugFile.decIdent(); 179 DebugFile.writeln ("End DataStruct.dump()"); 180 } 181 } }
| Popular Tags
|