Changeset 37 for GSEXmlParser


Ignore:
Timestamp:
May 20, 2008, 3:12:21 PM (16 years ago)
Author:
sunny
Message:

add tomysql function

Location:
GSEXmlParser
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • GSEXmlParser/GSEXmlParser.cpp

    r30 r37  
    124124}
    125125
     126void   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
    126212String TrimSpace(String str) {
    127213  while (str.StartsWith(" ") && str.GetLength()) {
     
    136222CONSOLE_APP_MAIN
    137223{
    138   String in, out, fmt, arg_flag;
     224  String in, out, fmt, user, pwd, db, ip, arg_flag;
    139225  /*
    140226  in = "GSE2109_family.xml";
     
    153239    } else if (arg_flag.IsEqual("-f")) {
    154240      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];     
    155249    } else if (arg_flag.StartsWith("-")) {
    156250      //Error argument
     
    160254  }
    161255 
    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     ) {
    163260    Cout() << "# Usage #\n";
    164261    Cout() << "For linux:   GSEXmlParser -f (xml|tsv) -i (input file) -o (output file)\n";
    165262    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";
    166264    exit(0); 
    167   }
    168  
     265  }
    169266  GSEParser parser;
    170267  parser.LoadXML(in);
     
    173270  else if (fmt.IsEqual("tsv"))  {
    174271    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  
    44#include <Core/Core.h>
    55#include <stdio.h>
     6#include <MySql/MySql.h>
    67
    78using namespace Upp;
     
    1819    void   Dump();
    1920    String   ToXML();
    20     String   ToTSV();     
     21    String   ToTSV();        
    2122};
    2223
    23 struct   GSEParser {
     24struct   GSEParser { 
    2425  Array<Data>           data;
    2526  void     Dump();
     
    2728    String   ToTSV();
    2829    void     LoadXML(String filename); 
     30    void     ToMySQL(String username, String password, String db, String db_ip);
    2931};
    3032#endif
  • GSEXmlParser/GSEXmlParser.upp

    r29 r37  
    22
    33uses
    4   Core;
     4  Core,
     5  MySql;
    56
    67file
  • GSEXmlParser/init

    r29 r37  
    22#define _GSEXmlParser_icpp_init_stub
    33#include "Core/init"
     4#include "MySql/init"
    45#endif
Note: See TracChangeset for help on using the changeset viewer.