001: package com.knowgate.datacopy;
002:
003: import java.sql.Date;
004: import java.sql.Timestamp;
005: import java.sql.SQLException;
006:
007: import java.io.FileWriter;
008: import java.io.IOException;
009:
010: import org.xml.sax.*;
011:
012: import java.lang.ClassNotFoundException;
013:
014: import java.util.Vector;
015:
016: import com.knowgate.debug.DebugFile;
017:
018: public class DataStructOrcl extends DataStruct {
019:
020: public DataStructOrcl() {
021: }
022:
023: public DataStructOrcl(String sPathXMLFile)
024: throws ClassNotFoundException, IllegalAccessException,
025: InstantiationException, IOException, SAXException {
026: super (sPathXMLFile);
027: }
028:
029: // ----------------------------------------------------------
030:
031: private String sqlldr(Object oValue) {
032: String sClass;
033: String sRetVal;
034: Date dtValue;
035: Timestamp tsValue;
036:
037: if (null == oValue) {
038: sRetVal = "NULL";
039: } else {
040: sClass = oValue.getClass().getName();
041:
042: if (sClass.equals("java.lang.String"))
043: sRetVal = "\"" + oValue.toString() + "\"";
044: else if (sClass.equals("java.util.Date")
045: || sClass.equals("java.sql.Date")) {
046: dtValue = (Date) oValue;
047: sRetVal = String.valueOf(dtValue.getYear() + 1900)
048: + "-";
049: sRetVal += (dtValue.getMonth() + 1 < 10 ? "0"
050: + String.valueOf(dtValue.getMonth() + 1)
051: : String.valueOf(dtValue.getMonth() + 1))
052: + "-";
053: sRetVal += (dtValue.getDay() + 1 < 10 ? "0"
054: + String.valueOf(dtValue.getDay() + 1) : String
055: .valueOf(dtValue.getDay() + 1))
056: + " ";
057: sRetVal += (dtValue.getHours() < 10 ? "0"
058: + String.valueOf(dtValue.getHours()) : String
059: .valueOf(dtValue.getHours()))
060: + ":";
061: sRetVal += (dtValue.getMinutes() < 10 ? "0"
062: + String.valueOf(dtValue.getMinutes()) : String
063: .valueOf(dtValue.getMinutes()))
064: + ":";
065: sRetVal += (dtValue.getSeconds() < 10 ? "0"
066: + String.valueOf(dtValue.getSeconds()) : String
067: .valueOf(dtValue.getSeconds()));
068: dtValue = null;
069: } else if (sClass.equals("java.sql.Timestamp")) {
070: tsValue = (Timestamp) oValue;
071: sRetVal = String.valueOf(tsValue.getYear() + 1900)
072: + "-";
073: sRetVal += (tsValue.getMonth() + 1 < 10 ? "0"
074: + String.valueOf(tsValue.getMonth() + 1)
075: : String.valueOf(tsValue.getMonth() + 1))
076: + "-";
077: sRetVal += (tsValue.getDay() + 1 < 10 ? "0"
078: + String.valueOf(tsValue.getDay() + 1) : String
079: .valueOf(tsValue.getDay() + 1))
080: + " ";
081: sRetVal += (tsValue.getHours() < 10 ? "0"
082: + String.valueOf(tsValue.getHours()) : String
083: .valueOf(tsValue.getHours()))
084: + ":";
085: sRetVal += (tsValue.getMinutes() < 10 ? "0"
086: + String.valueOf(tsValue.getMinutes()) : String
087: .valueOf(tsValue.getMinutes()))
088: + ":";
089: sRetVal += (tsValue.getSeconds() < 10 ? "0"
090: + String.valueOf(tsValue.getSeconds()) : String
091: .valueOf(tsValue.getSeconds()));
092: tsValue = null;
093: } else {
094: sRetVal = oValue.toString();
095: }
096: } // fi(null==oValue)
097:
098: return sRetVal;
099: }
100:
101: // ----------------------------------------------------------
102:
103: public void createSQLLoaderFiles(String sBasePath)
104: throws IOException, SQLException {
105: DataRowSet oDatR;
106: DataTblDef oTblD;
107: FileWriter oFilW;
108:
109: if (DebugFile.trace) {
110: DebugFile.writeln("Begin DataStruct.createSQLLoaderFiles("
111: + sBasePath + ")");
112: DebugFile.incIdent();
113: }
114:
115: prepareStatements();
116:
117: for (int t = 0; t < cTables; t++) {
118: oDatR = getRowSet(t);
119: oTblD = TrMetaData[t];
120: oFilW = new FileWriter(sBasePath + oDatR.OriginTable
121: + ".CTL", false);
122: oFilW.write("LOAD DATA\n");
123: oFilW.write("INFILE *\n");
124: oFilW.write("REPLACE INTO TABLE " + oDatR.TargetTable
125: + "\n");
126: oFilW
127: .write("FIELDS TERMINATED BY \"`\" OPTIONALLY ENCLOSED BY '\"'\n");
128: oFilW.write("(\n");
129: for (int c = 0; c < oTblD.ColCount; c++) {
130: oFilW.write(" " + oTblD.ColNames[c]);
131: if (oTblD.ColTypes[c] == java.sql.Types.DATE
132: || oTblD.ColTypes[c] == java.sql.Types.TIMESTAMP)
133: oFilW.write(" DATE \"YYYY-MM-DD HH24-MI-SS\"");
134: oFilW.write(" NULLIF (" + oTblD.ColNames[c]
135: + " = \"NULL\")");
136: oFilW.write(c < oTblD.ColCount - 1 ? ",\n" : ")\n");
137: } // next(c)
138: oFilW.write("BEGINDATA\n");
139: oFilW.close();
140: } // next(t)
141:
142: if (DebugFile.trace) {
143: DebugFile.decIdent();
144: DebugFile.writeln("End DataStruct.createSQLLoaderFiles()");
145: }
146: } // createSQLLoaderFiles()
147:
148: // ----------------------------------------------------------
149:
150: public void dump(Object[] OrPK, Object[] TrPK, int cParams,
151: String sBasePath) throws SQLException, IOException {
152: // Inserta registros del Origen en el Destino,
153: // si encuentra un registro duplicado lo actualiza sin dar ningún error,
154: // si el registro no está, lo inserta
155:
156: DataTblDef oMDat;
157: Object oValue;
158: int iPK;
159: FileWriter TblFiles[] = new FileWriter[cTables];
160:
161: if (DebugFile.trace) {
162: DebugFile.writeln("Begin DataStruct.dump(OrPK[], TrPK[], "
163: + String.valueOf(cParams) + ", " + sBasePath + ")");
164: DebugFile.incIdent();
165: }
166:
167: for (int t = 0; t < cTables; t++)
168: TblFiles[t] = new FileWriter(sBasePath
169: + getRowSet(t).OriginTable + ".CTL", true);
170:
171: execCommands("INIT", -1, OrPK, cParams);
172:
173: // Iterar sobre las tablas: para cada una de ellas leer sus registros e insertarlos en destino
174: for (int s = 0; s < cTables; s++) {
175: if (DebugFile.trace)
176: DebugFile.writeln("processing rowset from "
177: + getRowSet(s).OriginTable + " to "
178: + getRowSet(s).TargetTable);
179:
180: execCommands("BEFORE", s, OrPK, cParams);
181:
182: getRows(OrPK, TrPK, cParams, s); // Modifica {iRows, iCols} como efecto lateral
183:
184: oMDat = TrMetaData[s];
185:
186: // Iterar sobre cada fila leida en origen y escribirla en el fichero correspondiente
187: for (int r = 0; r < iRows; r++) {
188: iPK = 0;
189: // Iterador de parametros de entrada
190: for (int q = 0; q < iCols; q++) {
191: oValue = ((Vector) oResults.get(r)).get(q);
192:
193: if ((oMDat.isPrimaryKey(q))) {
194: if (iPK >= cParams)
195: TblFiles[s].write(sqlldr(oValue));
196: else if (null == TrPK[iPK])
197: TblFiles[s].write(sqlldr(oValue));
198: else
199: TblFiles[s].write(sqlldr(TrPK[iPK]));
200: iPK++;
201: } else
202: TblFiles[s].write(sqlldr(oValue));
203: if (q < iCols - 1)
204: TblFiles[s].write("`");
205: } // end for (q)
206: TblFiles[s].write("\n");
207: } // end for (r)
208:
209: oResults.clear();
210: oResults = null;
211:
212: execCommands("AFTER", s, OrPK, cParams);
213: } // end for (s)
214:
215: execCommands("TERM", -1, OrPK, cParams);
216:
217: for (int t = 0; t < cTables; t++)
218: TblFiles[t].close();
219:
220: if (DebugFile.trace) {
221: DebugFile.decIdent();
222: DebugFile.writeln("End DataStruct.dump()");
223: }
224: } // dump()
225: }
|