Changeset 37 for GSEXmlParser
- Timestamp:
- May 20, 2008, 3:12:21 PM (17 years ago)
- Location:
- GSEXmlParser
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
GSEXmlParser/GSEXmlParser.cpp
r30 r37 124 124 } 125 125 126 void GSEParser::ToMySQL(String username, String password, String db, String db_ip) 127 { 128 // 129 MySqlSession session; 130 if(!session.Connect("gse", "nchcnchc", "gse", "localhost")) { 131 printf("Can't connect with MySql\n"); 132 return; 133 } 134 Sql sql(session); 135 sql.Execute("use gse"); 136 String cmd; 137 // 138 Cout() << "Total Record Count:" << data.GetCount() << "\n"; 139 int sid; 140 int did; 141 String name; 142 for (int i = 0; i < data.GetCount(); i++) { 143 // check gsefamily record 144 cmd = "select * from gsefamily where iid = \"" + data[i].iid + "\""; 145 sql.Execute(cmd); 146 if (sql.GetRowsProcessed()>0) { 147 Cout() << "Warnning: IID ["+data[i].iid+"] is exist." << "\n"; 148 } else { 149 // add a new gsefamily record 150 cmd = "insert into gsefamily (iid,source) values (\""+data[i].iid+"\",\""+data[i].source+"\");"; 151 if (!sql.Execute(cmd)) { 152 Cout() << "Execution Error: " << sql.GetErrorCodeString() << " Cmd: " << sql.GetErrorStatement() << "\n"; 153 } 154 } 155 //Cout() << "IID ["+data[i].iid+"]" << "\n"; 156 157 sid = 0; 158 // check supplementary 159 cmd = "select sid from supplementary where type = \"" + data[i].supp_type + "\";"; 160 sql.Execute(cmd); 161 if (sql.GetRowsProcessed()==0) { 162 //new a supplementary record 163 cmd = "insert into supplementary (type) values (\"" + data[i].supp_type + "\");"; 164 if (!sql.Execute(cmd)) { 165 Cout() << "Execution Error: " << sql.GetErrorCode() << " Cmd: " << sql.GetErrorStatement() << "\n"; 166 } 167 sid = sql.GetInsertedId(); 168 } else { 169 if (sql.Fetch()) 170 sid = sql[0]; 171 //Cout() << "sid: " << sid << "\n"; 172 } 173 174 //new a supplementary-map record 175 cmd = "insert into `supplementary-map` (iid,sid,value) values (\""+data[i].iid+"\",\""+AsString(sid)+"\",\""+data[i].supp_data+"\")"; 176 if (!sql.Execute(cmd)) { 177 Cout() << "\nExecution Error: " << sql.GetErrorCode() << " Cmd: " << sql.GetErrorStatement() << "\n"; 178 } 179 180 //Cout() << "data[" << i << "].desc.GetCount() = " << data[i].desc.GetCount() << "\n"; 181 VectorMap<String, String> desc = data[i].desc; 182 for (int j=0; j<desc.GetCount(); j++) { 183 184 // check description 185 name = desc.GetKey(j); 186 cmd = "select did from description where name = \"" + name + "\""; 187 sql.Execute(cmd); 188 189 did = 0; 190 if (sql.GetRowsProcessed()==0) { 191 //new a description record 192 cmd = "insert into description (name) values (\"" + name + "\");"; 193 if (!sql.Execute(cmd)) { 194 Cout() << "Execution Error: " << sql.GetErrorCodeString() << " Cmd: " << sql.GetErrorStatement() << "\n"; 195 } 196 did = sql.GetInsertedId(); 197 } else { 198 if (sql.Fetch()) 199 did = sql[0]; 200 } 201 202 cmd = "insert into `description-map` (iid,did,value) values (\"" + data[i].iid + "\",\"" + AsString(did) + "\",\"" + desc.Get(name) + "\")"; 203 if (!sql.Execute(cmd)) { 204 Cout() << "Execution Error: " << sql.GetErrorCodeString() << " Cmd: " << sql.GetErrorStatement() << "\n"; 205 } 206 207 } 208 209 } 210 } 211 126 212 String TrimSpace(String str) { 127 213 while (str.StartsWith(" ") && str.GetLength()) { … … 136 222 CONSOLE_APP_MAIN 137 223 { 138 String in, out, fmt, arg_flag;224 String in, out, fmt, user, pwd, db, ip, arg_flag; 139 225 /* 140 226 in = "GSE2109_family.xml"; … … 153 239 } else if (arg_flag.IsEqual("-f")) { 154 240 fmt = cmdline[i]; 241 } else if (arg_flag.IsEqual("-u")) { 242 user = cmdline[i]; 243 } else if (arg_flag.IsEqual("-p")) { 244 pwd = cmdline[i]; 245 } else if (arg_flag.IsEqual("-db")) { 246 db = cmdline[i]; 247 } else if (arg_flag.IsEqual("-ip")) { 248 ip = cmdline[i]; 155 249 } else if (arg_flag.StartsWith("-")) { 156 250 //Error argument … … 160 254 } 161 255 162 if (cmdline.GetCount()==0 || in.IsEmpty() || out.IsEmpty() || fmt.IsEmpty()) { 256 if (cmdline.GetCount()==0 || 257 ( (fmt.IsEqual("xml") || fmt.IsEqual("tsv")) && (in.IsEmpty() || out.IsEmpty() || fmt.IsEmpty()) ) || 258 ( fmt.IsEqual("mysql") && (in.IsEmpty() || user.IsEmpty() || db.IsEmpty() || ip.IsEmpty()) ) 259 ) { 163 260 Cout() << "# Usage #\n"; 164 261 Cout() << "For linux: GSEXmlParser -f (xml|tsv) -i (input file) -o (output file)\n"; 165 262 Cout() << "For windows: GSEXmlParser.exe -f (xml|tsv) -i (input file) -o (output file)\n"; 263 Cout() << " GSEXmlParser.exe -f (mysql) -i (input file) -u (username) -p (password) -db (database) -ip (db host)\n"; 166 264 exit(0); 167 } 168 265 } 169 266 GSEParser parser; 170 267 parser.LoadXML(in); … … 173 270 else if (fmt.IsEqual("tsv")) { 174 271 SaveFile(out,parser.ToTSV()); 175 } 176 177 178 179 } 180 272 } else if (fmt.IsEqual("mysql")) { 273 parser.ToMySQL(user,pwd,db,ip); 274 Cout() << "Completed.\n"; 275 } 276 277 278 279 } 280 -
GSEXmlParser/GSEXmlParser.h
r29 r37 4 4 #include <Core/Core.h> 5 5 #include <stdio.h> 6 #include <MySql/MySql.h> 6 7 7 8 using namespace Upp; … … 18 19 void Dump(); 19 20 String ToXML(); 20 String ToTSV(); 21 String ToTSV(); 21 22 }; 22 23 23 struct GSEParser { 24 struct GSEParser { 24 25 Array<Data> data; 25 26 void Dump(); … … 27 28 String ToTSV(); 28 29 void LoadXML(String filename); 30 void ToMySQL(String username, String password, String db, String db_ip); 29 31 }; 30 32 #endif -
GSEXmlParser/GSEXmlParser.upp
r29 r37 2 2 3 3 uses 4 Core; 4 Core, 5 MySql; 5 6 6 7 file -
GSEXmlParser/init
r29 r37 2 2 #define _GSEXmlParser_icpp_init_stub 3 3 #include "Core/init" 4 #include "MySql/init" 4 5 #endif
Note: See TracChangeset
for help on using the changeset viewer.