OSDN Git Service

20170625
[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                 CmnProps cp = null;
70                 CmnAccessObjects dao = null;
71                 
72                 try {
73                         cp = new CmnProps();
74                         cp.setProperty(database);
75                         
76                         if (cp.dbType > cp.DB_TYPE_MYSQL || cp.dbType <0){
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 CmnAccessObjects(cp);
93                         conn = dao.connect();
94
95                         if(cp.isDrop){
96                                 executeSqlFile(dao, stmt, folder + "/" + cp.SQL_FILE_NAME[cp.DROP_FK], cp, true, false);
97                                 executeSqlFile(dao, stmt, folder + "/" + cp.SQL_FILE_NAME[cp.DROP_VIEW], cp, true, false);
98                                 executeSqlFile(dao, stmt, folder + "/" + cp.SQL_FILE_NAME[cp.DROP_PROCEDURE], cp, true, false);
99                                 executeSqlFile(dao, stmt, folder + "/" + cp.SQL_FILE_NAME[cp.DROP_SYNONYM], cp, true, false);
100                                 executeSqlFile(dao, stmt, folder + "/" + cp.SQL_FILE_NAME[cp.DROP_SEQUENCE], cp, true, false);
101                                 executeSqlFile(dao, stmt, folder + "/" + cp.SQL_FILE_NAME[cp.DROP_TABLE], cp, true, false);
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, false);
111                                         CmnUtils.infoPrint(String.format("%1$-42s","[TABLE] " + tbName) + " \83\81\83^\83f\81[\83^\82ª\83\8d\81[\83h\82³\82ê\82Ü\82µ\82½\81B");
112                                 }
113                         }
114                         executeSqlFile(dao, stmt, folder + "/" + cp.SQL_FILE_NAME[cp.CREATE_PKEY], cp, false, false);
115                         executeSqlFile(dao, stmt, folder + "/" + cp.SQL_FILE_NAME[cp.CREATE_UKEY], cp, false, false);
116                         executeSqlFile(dao, stmt, folder + "/" + cp.SQL_FILE_NAME[cp.CREATE_INDEX], cp, false, false);
117                         subFolder = new File(folder + "/VIEWS");
118                         inFiles = subFolder.listFiles();
119                         int view_count = 0;
120                         if(inFiles != null) {
121                                 for(int k=0;k<inFiles.length;k++){
122                                         flName = CmnUtils.splitDot(inFiles[k].getName());
123                                         viewName = flName[0];
124                                         executeSqlFile(dao, stmt, subFolder + "/" + inFiles[k].getName(), cp, false, false);
125                                         CmnUtils.infoPrint(String.format("%1$-42s","[VIEW] " + viewName) + " \83\81\83^\83f\81[\83^\82ª\83\8d\81[\83h\82³\82ê\82Ü\82µ\82½\81B");
126                                 }
127                         }
128                         subFolder = new File(folder + "/PROCEDURES");
129                         inFiles = subFolder.listFiles();
130                         int proc_count = 0;
131                         if(inFiles != null) {
132                                 for(int k=0;k<inFiles.length;k++){
133                                         flName = CmnUtils.splitDot(inFiles[k].getName());
134                                         procName = flName[0];
135                                         executeSqlFile(dao, stmt, subFolder + "/" + inFiles[k].getName(), cp, false, true);
136                                         CmnUtils.infoPrint(String.format("%1$-42s","[PROCEDURE] " + procName) + " \83\81\83^\83f\81[\83^\82ª\83\8d\81[\83h\82³\82ê\82Ü\82µ\82½\81B");
137                                 }
138                         }
139                         executeSqlFile(dao, stmt, folder + "/" + cp.SQL_FILE_NAME[cp.CREATE_SYNONYM], cp, false, false);
140                         executeSqlFile(dao, stmt, folder + "/" + cp.SQL_FILE_NAME[cp.CREATE_SEQUENCE], cp, false, false);
141                         executeSqlFile(dao, stmt, folder + "/" + cp.SQL_FILE_NAME[cp.CREATE_FK], cp, false, false);
142
143                         CmnUtils.infoPrint("\83\81\83^\83f\81[\83^\83\8d\81[\83h\82ð\8fI\97¹\82µ\82Ü\82µ\82½\81B");
144
145                 } catch(SQLException se) {
146                         try{
147                                 CmnUtils.errorPrint(se.toString());
148                                 se.printStackTrace();
149                                 se = se.getNextException();
150                                 if(se != null) {
151                                         System.out.println(se.getMessage());
152                                 }
153                                 dao.rollback();
154                         } catch (Exception see) {}
155                 } catch (Exception e) {                 
156                         try{
157                                 CmnUtils.errorPrint(e.toString());
158                                 e.printStackTrace();
159                                 dao.rollback();
160                         } catch (Exception ee) {}
161                 } finally{
162                         try{
163                                 dao.disconnect();
164                                 if(stmt != null){
165                                         stmt.close();
166                                         stmt=null;
167                                 }
168                                 if(br!=null){
169                                         br.close();
170                                         br=null;
171                                 }
172                         } catch (Exception e) {}
173                 }
174         }
175         
176         private void executeSqlFile(CmnAccessObjects _dao, PreparedStatement _stmt, String _infile, CmnProps _cp, boolean _isResume, boolean _isReplace) throws Exception{
177                 BufferedReader br = null;
178                 String strLine = null;
179                 String strMltLine = "";
180                 String [] sqlLine = null;
181
182                 File infile = new File(_infile);
183                 if (infile.exists()){
184                         br = new BufferedReader(new InputStreamReader(new FileInputStream(_infile), _cp.fileEncoding));
185                         while((strLine=br.readLine()) != null){
186                                 if (_cp.dbType == _cp.DB_TYPE_SQLSERVER){
187                                         if(!strLine.equals("GO")){
188                                                 strMltLine += _cp.inColLineSeparator + strLine;
189                                         }
190                                 } else {
191                                         strMltLine += _cp.inColLineSeparator + strLine;
192                                 }
193                         }
194                         strMltLine += _cp.lineSeparator;
195                         if(_isReplace){
196                                 if(!strMltLine.equals("")){
197                                         try {
198                                                 if (_cp.dbType == _cp.DB_TYPE_ORACLE){
199                                                         _stmt = _dao.prepareSql(strMltLine.replaceAll("\r",""));
200                                                 } else {
201                                                         _stmt = _dao.prepareSql(strMltLine);
202                                                 }
203                                                 _stmt.executeUpdate();
204                                                 if(_stmt != null){
205                                                         _stmt.close();
206                                                         _stmt=null;
207                                                 }
208                                         } catch (Exception e) {
209                                                 if(!_isResume){
210                                                         throw e;
211                                                 }
212                                         }
213                                 }
214                         } else {
215                                 sqlLine = CmnUtils.split(strMltLine, _cp.SQL_TERMINATOR[_cp.dbType]);
216                                 for(int l = 0;l<sqlLine.length;l++){
217                                         if(!sqlLine[l].equals("") && !sqlLine[l].equals(_cp.lineSeparator)){
218                                                 try {
219                                                         CmnUtils.debugPrint("'" + sqlLine[l] + "'");
220                                                         if (_cp.dbType == _cp.DB_TYPE_ORACLE){
221                                                                 _stmt = _dao.prepareSql(sqlLine[l].replaceAll("\r",""));
222                                                         } else {
223                                                                 _stmt = _dao.prepareSql(sqlLine[l]);
224                                                         }
225                                                         _stmt.executeUpdate();
226                                                         if(_stmt != null){
227                                                                 _stmt.close();
228                                                                 _stmt=null;
229                                                         }
230                                                 } catch (Exception e) {
231                                                         if(!_isResume){
232                                                                 throw e;
233                                                         }
234                                                 }
235                                         }
236                                 }
237                         }
238                 }
239         }
240
241 }
242