1 package com.rapide_act;
3 import java.io.BufferedInputStream;
4 import java.io.BufferedReader;
5 import java.io.BufferedWriter;
6 import java.io.FileInputStream;
7 import java.io.InputStream;
8 import java.io.InputStreamReader;
9 import java.io.IOException;
11 import java.io.FileReader;
12 import java.io.FileWriter;
13 import java.io.PrintWriter;
14 import java.io.PrintWriter;
15 import java.util.Date;
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.Properties;
21 import java.sql.Connection;
22 import java.sql.DriverManager;
23 import java.sql.PreparedStatement;
24 import java.sql.ResultSet;
25 import java.sql.ResultSetMetaData;
26 import java.sql.SQLException;
27 import java.sql.Timestamp;
28 import java.math.BigDecimal;
29 import java.math.BigInteger;
31 public class RapideLoader{
32 private String database = null;
34 public static void main(String args[]){
37 RapideLoader rapideLoader = new RapideLoader(args[0]);
40 RapideLoader rapideLoader = new RapideLoader(null);
43 } catch (Exception e) {
45 CmnUtils.errorPrint(e.toString());
47 } catch (Exception ee){
54 RapideLoader(String _database) {
60 Connection conn = null;
61 PreparedStatement stmt = null;
62 PreparedStatement stmt_ins = null;
63 BufferedReader br = null;
64 String sql_load = null;
65 String sql_insert = null;
66 String strLine = null;
67 String strMltLine = null;
69 String [] flName = null;
70 String [] colData = null;
71 String [] colName = null;
72 String [] colTypeName = null;
73 Integer [] isNullable = null;
74 Boolean [] colIncl = null;
77 ArrayList<String> alData = null;
78 ArrayList<String> alColName = null;
79 ArrayList<String> alColTypeName = null;
80 ArrayList<Integer> alIsNullable = null;
81 ArrayList<Boolean> alColMask = null;
82 ArrayList<Boolean> alColIncl = null;
87 boolean isLob = false;
88 boolean isContinue = false;
89 CmnProperty cp = null;
90 DataAccessObjects dao = null;
93 cp = new CmnProperty();
94 cp.setProperty(database);
96 if(cp.inFolder != null){
97 folder = new File(cp.inFolder);
99 if (database != null){
100 folder = new File(cp.DEFAULT_IN_FOLDER + "/" + database.toUpperCase());
102 folder = new File(cp.DEFAULT_IN_FOLDER + "/" + cp.DB_TYPE_NAME[cp.dbType].toUpperCase());
105 CmnUtils.infoPrint("-->
\93ü
\97Í
\8c³
\83t
\83H
\83\8b\83_='" + folder + "'");
106 CmnUtils.infoPrint("
\83f
\81[
\83^
\83\8d\81[
\83h
\82ð
\8aJ
\8en
\82µ
\82Ü
\82µ
\82½
\81B");
107 dao = new DataAccessObjects(cp);
108 conn = dao.connect();
109 conn.setAutoCommit(false);
111 alData = dao.getArrayList();
112 tb_count = dao.getColumnCount();
113 int tb_rec_count = 0;
114 int fl_rec_count = 0;
115 int ins_rec_count = 0;
116 int tb_fl_match_count = 0;
117 File [] inFiles = folder.listFiles();
118 if(inFiles != null) {
119 for(int k=0;k<inFiles.length;k++){
120 flName = CmnUtils.splitDot(inFiles[k].getName());
121 tbName = flName[0].toUpperCase();
122 StringBuffer sbColumnName = null;
124 for(int i=0;i<alData.size();i++){
125 if(tbName.toUpperCase().equals(alData.get(i).toUpperCase())){
126 if(cp.isInclTables && !CmnUtils.isMatch(cp.inclTablesArray,tbName.toUpperCase())){
127 CmnUtils.debugPrint("cp.inclTables=" + cp.inclTables);
128 CmnUtils.debugPrint("table=" + tbName + "," +CmnUtils.isMatch(cp.inclTablesArray,tbName.toUpperCase()));
131 if(cp.isExclTables && CmnUtils.isMatch(cp.exclTablesArray,tbName.toUpperCase())){
132 CmnUtils.debugPrint("cp.exclTables=" + cp.exclTables);
133 CmnUtils.debugPrint("table=" + tbName + "," +CmnUtils.isMatch(cp.exclTablesArray,tbName.toUpperCase()));
136 br = new BufferedReader(new InputStreamReader(new FileInputStream(folder + "/" + alData.get(i) + "." + flName[1]), cp.fileEncoding));
137 sql_load = "select * from " + alData.get(i);
138 stmt = dao.prepareSql(sql_load);
139 dao.executeSql(stmt);
140 alColName = dao.getArrayColumnNameList();
141 alColTypeName = dao.getArrayColumnTypeNameList();
142 alIsNullable = dao.getArrayIsNullableList();
143 alColMask = new ArrayList<Boolean>();
144 alColIncl = new ArrayList<Boolean>();
145 colName = (String[])alColName.toArray(new String[0]);
146 colTypeName = (String[])alColTypeName.toArray(new String[0]);
147 isNullable = (Integer[])alIsNullable.toArray(new Integer[0]);
148 tb_col_count = dao.getColumnCount();
149 int [] tb_col_seq = new int[tb_col_count];
150 int [] fl_col_seq = null;
151 dao.closeRecordSet();
156 tb_fl_match_count = 0;
160 for(int j=0;j<colTypeName.length;j++)if(CmnUtils.isLob(colTypeName[j]))isLob = true;
161 CmnUtils.debugPrint("LOB="+isLob);
162 while((strLine=br.readLine()) != null){
163 if (fl_rec_count == 0){
165 colData = CmnUtils.split(strLine,"\"" + cp.delimiter + "\"");
167 colData = CmnUtils.split(strLine,cp.delimiter);
169 fl_col_count = colData.length;
170 fl_col_seq = new int[colData.length];
171 CmnUtils.debugPrint("TableName=" + tbName);
173 for(int j=0;j<colData.length;j++){
174 for(int l=0;l<tb_col_count;l++){
175 if(colName[l].equals(colData[j].replaceAll("\"",""))){
176 if(cp.isInclCols || cp.isExclCols){
180 CmnUtils.isMatch(cp.inclColsArray, tbName.toUpperCase(), colName[l].toUpperCase())
184 CmnUtils.isMatch(cp.inclColsArray, tbName.toUpperCase(), colName[l].toUpperCase()) &&
185 !CmnUtils.isMatch(cp.exclColsArray, tbName.toUpperCase(), colName[l].toUpperCase())
189 !CmnUtils.isMatch(cp.exclColsArray, tbName.toUpperCase(), colName[l].toUpperCase())
192 CmnUtils.debugPrint("
\91Î
\8fÛ
\83J
\83\89\83\80=" + colName[l].toUpperCase());
195 alColIncl.add(false);
205 colIncl = (Boolean[])alColIncl.toArray(new Boolean[0]);
206 sql_insert = "INSERT INTO " + tbName + "(";
207 for(int j=0;j<colData.length;j++){
208 for(int l=0;l<tb_col_count;l++){
209 if(colName[l].equals(colData[j].replaceAll("\"",""))){
210 if (colIncl[tb_fl_match_count]) {
211 if (ins_rec_count == 0){
212 sql_insert += colName[l];
214 sql_insert += "," + colName[l];
216 tb_col_seq[ins_rec_count]=l;
217 fl_col_seq[ins_rec_count]=j;
225 if (ins_rec_count > 0){
226 sql_insert += ") VALUES(";
227 for(int j=0;j<ins_rec_count;j++){
235 stmt_ins = dao.prepareSql(sql_insert);
236 CmnUtils.debugPrint("************Record Start");
237 CmnUtils.infoPrinting(String.format("%1$-30s",tbName));
240 CmnUtils.debugPrint("
\8ew
\92è
\82µ
\82½
\8bæ
\90Ø
\82è
\95¶
\8e\9a\82Å
\82Ì
\83J
\83\89\83\80\90\94\82ª0
\82Å
\82·
\81B");
245 if(stmt_ins != null){
253 colData = CmnUtils.split(strLine,"\"" + cp.delimiter + "\"");
255 colData = CmnUtils.split(strLine,cp.delimiter);
257 if(colData.length == fl_col_count){
258 if(!strMltLine.equals("")){
260 CmnUtils.errorPrint("
\8aY
\93\96\83\8c\83R
\81[
\83h
\82Ì
\83J
\83\89\83\80\90\94\82ª
\83A
\83\93\83}
\83b
\83`
\82Å
\82·
\81B[" + tb_rec_count + "]");
261 CmnUtils.debugPrint("************Record End");
266 if(stmt_ins != null){
275 if (strMltLine.equals("")){
276 strMltLine += strLine;
279 strMltLine += cp.inColLineSeparator + strLine;
281 colData = CmnUtils.split(strMltLine,"\"" + cp.delimiter + "\"");
283 colData = CmnUtils.split(strMltLine,cp.delimiter);
285 if(colData.length == fl_col_count){
287 } else if(colData.length >fl_col_count){
289 CmnUtils.errorPrint("
\8aY
\93\96\83\8c\83R
\81[
\83h
\82Ì
\83J
\83\89\83\80\90\94\82ª
\83A
\83\93\83}
\83b
\83`
\82Å
\82·
\81B[" + tb_rec_count + "]");
290 CmnUtils.debugPrint("************Record End");
295 if(stmt_ins != null){
305 if(isContinue == false){
306 for(int j=0;j<ins_rec_count;j++){
309 if(colData[fl_col_seq[j]].length()>1){
310 colData[fl_col_seq[j]]=colData[fl_col_seq[j]].substring(1,colData[fl_col_seq[j]].length());
312 colData[fl_col_seq[j]]=colData[fl_col_seq[j]].substring(1,colData[fl_col_seq[j]].length()).replaceAll("\"","");
316 if(j==ins_rec_count-1){
318 if(colData[fl_col_seq[j]].length()>1){
319 colData[fl_col_seq[j]]=colData[fl_col_seq[j]].substring(0,colData[fl_col_seq[j]].length()-1);
321 colData[fl_col_seq[j]]=colData[fl_col_seq[j]].substring(0,colData[fl_col_seq[j]].length()).replaceAll("\"","");
326 CmnUtils.deepPrint("[" + fl_rec_count + "]" + "[" + j + "]" + colName[tb_col_seq[j]].toUpperCase() + "." + colTypeName[tb_col_seq[j]] + "='" + colData[fl_col_seq[j]] + "'");
327 if (CmnUtils.isColString(colTypeName[tb_col_seq[j]])){
330 (cp.isInclColsMask &&
331 !cp.isExclColsMask &&
332 CmnUtils.isMatch(cp.inclColsMaskArray,tbName.toUpperCase(), colName[tb_col_seq[j]].toUpperCase())
334 (cp.isInclColsMask &&
336 CmnUtils.isMatch(cp.inclColsMaskArray,tbName.toUpperCase(), colName[tb_col_seq[j]].toUpperCase()) &&
337 !CmnUtils.isMatch(cp.exclColsMaskArray,tbName.toUpperCase(),colName[tb_col_seq[j]].toUpperCase())
339 (!cp.isInclColsMask &&
341 !CmnUtils.isMatch(cp.exclColsMaskArray,tbName.toUpperCase(),colName[tb_col_seq[j]].toUpperCase())
344 StringBuffer sb = new StringBuffer();
345 for(int l=0;l<colData[fl_col_seq[j]].length();l++){
346 if((cp.maskPattern.toUpperCase().equals(cp.MASK_PTN[cp.MASK_PTN_ALT]) &&
348 (cp.maskPattern.toUpperCase().equals(cp.MASK_PTN[cp.MASK_PTN_EDGE]) &&
349 !(l == 0 || j == colData[fl_col_seq[j]].length() - 1) ||
350 (cp.maskPattern.toUpperCase().equals(cp.MASK_PTN[cp.MASK_PTN_ALL]))
353 if(CmnUtils.isHankaku(colData[fl_col_seq[j]].charAt(l))){
354 sb.append(cp.singleByteMaskChar);
356 sb.append(cp.doubleByteMaskChar);
359 sb.append(colData[fl_col_seq[j]].charAt(l));
362 colData[fl_col_seq[j]] = sb.toString();
365 if (!colData[fl_col_seq[j]].equals("")){
367 stmt_ins.setString(j+1,colData[fl_col_seq[j]].replaceAll("\"\"","\""));
369 stmt_ins.setString(j+1,colData[fl_col_seq[j]]);
372 if(isNullable[j] == 0){
373 stmt_ins.setString(j+1," ");
375 stmt_ins.setString(j+1,null);
378 } else if (CmnUtils.isColDate(colTypeName[tb_col_seq[j]])){
379 if (!colData[fl_col_seq[j]].equals("")){
380 stmt_ins.setDate(j+1,java.sql.Date.valueOf(colData[fl_col_seq[j]]));
382 stmt_ins.setDate(j+1,null);
384 } else if (CmnUtils.isColYear(colTypeName[tb_col_seq[j]])){
385 if (!colData[fl_col_seq[j]].equals("")){
386 stmt_ins.setString(j+1,colData[fl_col_seq[j]]);
388 stmt_ins.setDate(j+1,null);
390 } else if (CmnUtils.isColTime(colTypeName[tb_col_seq[j]])){
391 if (!colData[fl_col_seq[j]].equals("")){
392 stmt_ins.setTime(j+1,java.sql.Time.valueOf(colData[fl_col_seq[j]]));
394 stmt_ins.setTime(j+1,null);
396 } else if (CmnUtils.isColTimestamp(colTypeName[tb_col_seq[j]])){
397 if (!colData[fl_col_seq[j]].equals("")){
398 stmt_ins.setTimestamp(j+1,java.sql.Timestamp.valueOf(colData[fl_col_seq[j]]));
399 //CmnUtils.debugPrint("Timestamp=" + colData[fl_col_seq[j]]);
401 stmt_ins.setTimestamp(j+1,null);
403 } else if (CmnUtils.isColBigDecimal(colTypeName[tb_col_seq[j]])){
404 if (!colData[fl_col_seq[j]].equals("")){
405 stmt_ins.setBigDecimal(j+1,new BigDecimal(colData[fl_col_seq[j]]));
407 stmt_ins.setNull(j+1,java.sql.Types.DECIMAL);
409 } else if (CmnUtils.isColShort(colTypeName[tb_col_seq[j]])){
410 if (!colData[fl_col_seq[j]].equals("")){
411 stmt_ins.setShort(j+1,Short.parseShort(colData[fl_col_seq[j]]));
413 stmt_ins.setNull(j+1,java.sql.Types.SMALLINT);
415 } else if (CmnUtils.isColInt(colTypeName[tb_col_seq[j]])){
416 if (!colData[fl_col_seq[j]].equals("")){
417 stmt_ins.setInt(j+1,Integer.parseInt(colData[fl_col_seq[j]]));
419 stmt_ins.setNull(j+1,java.sql.Types.INTEGER);
421 } else if (CmnUtils.isColLong(colTypeName[tb_col_seq[j]])){
422 if (!colData[fl_col_seq[j]].equals("")){
423 stmt_ins.setLong(j+1,Long.parseLong(colData[fl_col_seq[j]]));
425 stmt_ins.setNull(j+1,java.sql.Types.BIGINT);
427 } else if (CmnUtils.isColFloat(colTypeName[tb_col_seq[j]])){
428 if (!colData[fl_col_seq[j]].equals("")){
429 stmt_ins.setFloat(j+1,Float.parseFloat(colData[fl_col_seq[j]]));
431 stmt_ins.setNull(j+1,java.sql.Types.REAL);
433 } else if (CmnUtils.isColDouble(colTypeName[tb_col_seq[j]])){
434 if (!colData[fl_col_seq[j]].equals("")){
435 stmt_ins.setDouble(j+1,Double.parseDouble(colData[fl_col_seq[j]]));
437 stmt_ins.setNull(j+1,java.sql.Types.FLOAT);
439 } else if (CmnUtils.isColBytes(colTypeName[tb_col_seq[j]])){
440 if (!colData[fl_col_seq[j]].equals("")){
441 stmt_ins.setBytes(j+1,CmnUtils.base64ToBytes(colData[fl_col_seq[j]]));
443 stmt_ins.setBytes(j+1,null);
445 } else if (CmnUtils.isColBlob(colTypeName[tb_col_seq[j]])){
446 if (!colData[fl_col_seq[j]].equals("")){
447 stmt_ins.setBytes(j+1,CmnUtils.base64ToBytes(colData[fl_col_seq[j]]));
449 stmt_ins.setBlob(j+1,null,0);
451 } else if (CmnUtils.isColClob(colTypeName[tb_col_seq[j]])){
452 if (!colData[fl_col_seq[j]].equals("")){
453 stmt_ins.setString(j+1,colData[fl_col_seq[j]]);
455 stmt_ins.setClob(j+1,null,0);
458 CmnUtils.errorPrint("
\83J
\83\89\83\80\82Ì
\83f
\81[
\83^
\8c^
\82ª
\91Î
\8fÛ
\8aO
\82Å
\82·
\81B[" + colTypeName[tb_col_seq[j]] + "]");
463 stmt_ins.executeUpdate();
472 if (fl_rec_count % cp.batchCount == 0){
473 if(!isLob)stmt_ins.executeBatch();
474 CmnUtils.debugPrint("insert record=" + fl_rec_count);
477 CmnUtils.debugPrint("sql_insert=" + sql_insert);
478 if(!isLob)stmt_ins.executeBatch();
480 System.out.println(String.format("%1$10d",tb_rec_count) + "
\8ds
\83\8d\81[
\83h
\82³
\82ê
\82Ü
\82µ
\82½
\81B");
481 CmnUtils.debugPrint("************Record End");
486 if(stmt_ins != null){
495 CmnUtils.errorPrint("
\93ü
\97Í
\8c³
\83t
\83H
\83\8b\83_
\82ª
\8c©
\82Â
\82©
\82è
\82Ü
\82¹
\82ñ
\81B[" + folder + "]");
498 CmnUtils.infoPrint("
\83f
\81[
\83^
\83\8d\81[
\83h
\82ð
\8fI
\97¹
\82µ
\82Ü
\82µ
\82½
\81B");
500 } catch (Exception e) {
502 CmnUtils.errorPrint(e.toString());
505 } catch (Exception ee) {}
513 if(stmt_ins != null){
521 } catch (Exception e) {}