OSDN Git Service

20170423
[rapideact/rapideact.git] / com / rapide_act / RapideMetaLoader.java
1 package com.rapide_act;
2
3 import java.io.BufferedInputStream;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.InputStreamReader;
8 import java.io.File;
9 import java.io.FileReader;
10 import java.io.FileWriter;
11 import java.io.BufferedWriter;
12 import java.io.BufferedReader;
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;
19 import java.sql.Connection;
20 import java.sql.DriverManager;
21 import java.sql.ResultSet;
22 import java.sql.ResultSetMetaData;
23 import java.sql.SQLException;
24 import java.sql.PreparedStatement;
25 import java.sql.Timestamp;
26 import java.sql.Blob;
27 import java.sql.Clob;
28
29 public class RapideMetaLoader{
30         
31         private String database = null;
32
33         public static void main(String args[]){
34                 try {
35                         if (args.length > 0){
36                                 RapideMetaLoader RapideMetaLoader = new RapideMetaLoader(args[0]);
37                                 RapideMetaLoader.metaLoad();
38                         } else {
39                                 RapideMetaLoader RapideMetaLoader = new RapideMetaLoader(null);
40                                 RapideMetaLoader.metaLoad();
41                         }
42                 } catch (Exception e) {
43                         e.printStackTrace();
44                 }
45         }
46
47         RapideMetaLoader(String _database) {
48                 super();
49                 database = _database;
50         }
51
52
53         private void metaLoad(){
54                 Connection conn = null;
55                 PreparedStatement stmt = null;
56                 BufferedReader br = null;
57                 File folder = null;
58                 File subFolder = null;
59                 String strLine = null;
60                 String strMltLine = null;
61                 String tbName   = null;
62                 String viewName         = null;
63                 String procName         = null;
64                 String [] flName = null;
65                 File [] inFiles = null;
66                 int tb_count = 0;
67                 String [] sqlLine = null;
68
69                 CmnProperty cp = null;
70                 DataAccessObjects dao = null;
71                 
72                 try {
73                         cp = new CmnProperty();
74                         cp.setProperty(database);
75                         
76                         if (cp.dbType != cp.DB_TYPE_ORACLE){
77                                 throw new Exception("\83f\81[\83^\83x\81[\83X\82ª\91Î\8fÛ\8aO\82Å\82·\81B[" + cp.DB_TYPE_NAME[cp.dbType] + "]");
78                         }
79                         if(cp.inFolder != null){
80                                 folder = new File(cp.inFolder);
81                         } else {
82                                 if (database != null){
83                                         folder = new File(cp.DEFAULT_IN_FOLDER + "/DDL_" + database.toUpperCase());
84                                 } else {
85                                         folder = new File(cp.DEFAULT_IN_FOLDER + "/DDL_" + cp.DB_TYPE_NAME[cp.dbType].toUpperCase());
86                                 }
87                         }
88                         CmnUtils.infoPrint("-->\93ü\97Í\8c³\83t\83H\83\8b\83_='" + folder + "'");
89                         CmnUtils.infoPrint("\83\81\83^\83f\81[\83^\83\8d\81[\83h\82ð\8aJ\8en\82µ\82Ü\82µ\82½\81B");
90
91                         // Connect
92                         dao = new DataAccessObjects(cp);
93                         conn = dao.connect();
94
95                         if(cp.isDrop){
96                                 executeSqlFile(dao, stmt, folder + "/" + cp.SQL_FILE_NAME[cp.DROP_TABLE], cp, true);
97                                 executeSqlFile(dao, stmt, folder + "/" + cp.SQL_FILE_NAME[cp.DROP_VIEW], cp, true);
98                                 executeSqlFile(dao, stmt, folder + "/" + cp.SQL_FILE_NAME[cp.DROP_PROCEDURE], cp, true);
99                                 executeSqlFile(dao, stmt, folder + "/" + cp.SQL_FILE_NAME[cp.DROP_SYNONYM], cp, true);
100                                 executeSqlFile(dao, stmt, folder + "/" + cp.SQL_FILE_NAME[cp.DROP_SEQUENCE], cp, true);
101                                 executeSqlFile(dao, stmt, folder + "/" + cp.SQL_FILE_NAME[cp.DROP_FK], cp, true);
102                         }
103                         subFolder = new File(folder + "/TABLES");
104                         inFiles = subFolder.listFiles();
105                         tb_count = 0;
106                         if(inFiles != null) {
107                                 for(int k=0;k<inFiles.length;k++){
108                                         flName = CmnUtils.splitDot(inFiles[k].getName());
109                                         tbName = flName[0];
110                                         executeSqlFile(dao, stmt, subFolder + "/" + inFiles[k].getName(), cp, false);
111                                 }
112                         }
113                         executeSqlFile(dao, stmt, folder + "/" + cp.SQL_FILE_NAME[cp.CREATE_PKEY], cp, false);
114                         executeSqlFile(dao, stmt, folder + "/" + cp.SQL_FILE_NAME[cp.CREATE_UKEY], cp, false);
115                         executeSqlFile(dao, stmt, folder + "/" + cp.SQL_FILE_NAME[cp.CREATE_INDEX], cp, false);
116                         subFolder = new File(folder + "/VIEWS");
117                         inFiles = subFolder.listFiles();
118                         int view_count = 0;
119                         if(inFiles != null) {
120                                 for(int k=0;k<inFiles.length;k++){
121                                         flName = CmnUtils.splitDot(inFiles[k].getName());
122                                         viewName = flName[0];
123                                         executeSqlFile(dao, stmt, subFolder + "/" + inFiles[k].getName(), cp, false);
124                                 }
125                         }
126                         subFolder = new File(folder + "/PROCEDURES");
127                         inFiles = subFolder.listFiles();
128                         int proc_count = 0;
129                         if(inFiles != null) {
130                                 for(int k=0;k<inFiles.length;k++){
131                                         flName = CmnUtils.splitDot(inFiles[k].getName());
132                                         procName = flName[0];
133                                         executeSqlFile(dao, stmt, subFolder + "/" + inFiles[k].getName(), cp, false);
134                                 }
135                         }
136                         executeSqlFile(dao, stmt, folder + "/" + cp.SQL_FILE_NAME[cp.CREATE_SEQUENCE], cp, false);
137                         executeSqlFile(dao, stmt, folder + "/" + cp.SQL_FILE_NAME[cp.CREATE_FK], cp, false);
138
139                         CmnUtils.infoPrint("\83\81\83^\83f\81[\83^\83\8d\81[\83h\82ð\8fI\97¹\82µ\82Ü\82µ\82½\81B");
140
141                 } catch(SQLException se) {
142                         try{
143                                 CmnUtils.errorPrint(se.toString());
144                                 se.printStackTrace();
145                                 se = se.getNextException();
146                                 if(se != null) {
147                                         System.out.println(se.getMessage());
148                                 }
149                                 dao.rollback();
150                         } catch (Exception see) {}
151                 } catch (Exception e) {                 
152                         try{
153                                 CmnUtils.errorPrint(e.toString());
154                                 e.printStackTrace();
155                                 dao.rollback();
156                         } catch (Exception ee) {}
157                 } finally{
158                         try{
159                                 dao.disconnect();
160                                 if(stmt != null){
161                                         stmt.close();
162                                         stmt=null;
163                                 }
164                                 if(br!=null){
165                                         br.close();
166                                         br=null;
167                                 }
168                         } catch (Exception e) {}
169                 }
170         }
171         
172         private void executeSqlFile(DataAccessObjects _dao, PreparedStatement _stmt, String _infile, CmnProperty _cp, boolean _isResume) throws Exception{
173                 BufferedReader br = null;
174                 String strLine = null;
175                 String strMltLine = "";
176                 String [] sqlLine = null;
177
178                 File infile = new File(_infile);
179                 if (infile.exists()){
180                         br = new BufferedReader(new InputStreamReader(new FileInputStream(_infile), _cp.fileEncoding));
181                         while((strLine=br.readLine()) != null){
182                                 strMltLine += _cp.inColLineSeparator + strLine;
183                         }
184                         sqlLine = CmnUtils.split(strMltLine, ";");
185                         for(int l = 0;l<sqlLine.length;l++){
186                                 if(!sqlLine[l].equals("")){
187                                         try {
188                                                 _stmt = _dao.prepareSql(sqlLine[l]);
189                                                 _stmt.executeUpdate();
190                                                 if(_stmt != null){
191                                                         _stmt.close();
192                                                         _stmt=null;
193                                                 }
194                                         } catch (Exception e) {
195                                                 if(!_isResume){
196                                                         throw e;
197                                                 }
198                                         }
199                                 }
200                         }
201                 }
202         }
203
204 }
205