KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > datacopy > DataStructOrcl


1 package com.knowgate.datacopy;
2
3 import java.sql.Date JavaDoc;
4 import java.sql.Timestamp JavaDoc;
5 import java.sql.SQLException JavaDoc;
6
7 import java.io.FileWriter JavaDoc;
8 import java.io.IOException JavaDoc;
9
10 import org.xml.sax.*;
11
12 import java.lang.ClassNotFoundException JavaDoc;
13
14 import java.util.Vector JavaDoc;
15
16 import com.knowgate.debug.DebugFile;
17
18 public class DataStructOrcl extends DataStruct {
19
20   public DataStructOrcl() {
21   }
22
23   public DataStructOrcl(String JavaDoc sPathXMLFile) throws ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc, InstantiationException JavaDoc, IOException JavaDoc, SAXException {
24     super(sPathXMLFile);
25   }
26
27   // ----------------------------------------------------------
28

29   private String JavaDoc sqlldr (Object JavaDoc oValue) {
30     String JavaDoc sClass;
31     String JavaDoc sRetVal;
32     Date JavaDoc dtValue;
33     Timestamp JavaDoc 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 JavaDoc) 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 JavaDoc) 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     } // fi(null==oValue)
67

68     return sRetVal;
69   }
70
71   // ----------------------------------------------------------
72

73   public void createSQLLoaderFiles(String JavaDoc sBasePath) throws IOException JavaDoc,SQLException JavaDoc {
74     DataRowSet oDatR;
75     DataTblDef oTblD;
76     FileWriter JavaDoc 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 JavaDoc(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       } // next(c)
101
oFilW.write("BEGINDATA\n");
102       oFilW.close();
103     } // next(t)
104

105     if (DebugFile.trace) {
106       DebugFile.decIdent();
107       DebugFile.writeln ("End DataStruct.createSQLLoaderFiles()");
108     }
109   } // createSQLLoaderFiles()
110

111   // ----------------------------------------------------------
112

113   public void dump(Object JavaDoc[] OrPK, Object JavaDoc[] TrPK, int cParams, String JavaDoc sBasePath) throws SQLException JavaDoc,IOException JavaDoc {
114     // Inserta registros del Origen en el Destino,
115
// si encuentra un registro duplicado lo actualiza sin dar ningún error,
116
// si el registro no está, lo inserta
117

118     DataTblDef oMDat;
119     Object JavaDoc oValue;
120     int iPK;
121     FileWriter JavaDoc TblFiles[] = new FileWriter JavaDoc[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 JavaDoc(sBasePath + getRowSet(t).OriginTable + ".CTL", true);
130
131     execCommands("INIT", -1, OrPK, cParams);
132
133     // Iterar sobre las tablas: para cada una de ellas leer sus registros e insertarlos en destino
134
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); // Modifica {iRows, iCols} como efecto lateral
140

141       oMDat = TrMetaData[s];
142
143       // Iterar sobre cada fila leida en origen y escribirla en el fichero correspondiente
144
for (int r=0; r<iRows; r++) {
145           iPK = 0;
146           // Iterador de parametros de entrada
147
for (int q=0; q<iCols; q++) {
148             oValue = ((Vector JavaDoc) 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           } // end for (q)
163
TblFiles[s].write("\n");
164       } // end for (r)
165

166       oResults.clear();
167       oResults = null;
168
169       execCommands("AFTER", s, OrPK, cParams);
170     } // end for (s)
171

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   } // dump()
182
}
Popular Tags