Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

DbiAsciiDbImporter.cxx

Go to the documentation of this file.
00001 // $Id: DbiAsciiDbImporter.cxx,v 1.5 2008/02/14 11:12:04 west Exp $
00002 
00004 //
00005 // DbiAsciiDbImporter
00006 // Acknowledgments 
00007 //   The code is essentially a translation of
00008 //   RDBC/TSQLImporter by Valeriy Onuchin 21/03/2001
00009 //
00011 
00012 
00013 #include <TSQLServer.h>
00014 #include <TSQLStatement.h>
00015 #include "TSystem.h"
00016 #include <TUrl.h>
00017 
00018 #include <DatabaseInterface/DbiAsciiDbImporter.h>
00019 #include <DatabaseInterface/DbiAsciiTablePreparer.h>
00020 #include <DatabaseInterface/DbiExceptionLog.h>
00021 #include "MessageService/MsgService.h"
00022 #include "LeakChecker/Lea.h"
00023 
00024 ClassImpQ(DbiAsciiDbImporter)
00025 
00026 //   Definition of static data members
00027 //   *********************************
00028 
00029 CVSID("$Id: DbiAsciiDbImporter.cxx,v 1.5 2008/02/14 11:12:04 west Exp $");
00030 
00031 //    Definition of all member functions (static or otherwise)
00032 //    *******************************************************
00033 //
00034 //    -  ordered: ctors, dtor, operators then in alphabetical order.
00035 
00036 
00037 //___________________________________________________________________
00038 DbiAsciiDbImporter::DbiAsciiDbImporter():
00039   fServer(0),
00040   fTablePreparer(0)
00041 {
00042    // ctor
00043 
00044    LEA_CTOR    //Leak Checker
00045 
00046      MSG("Dbi", Msg::kVerbose) << "Creating DbiAsciiDbImporter " << (void*) this << endl;
00047 
00048    fStatus = HTTP_BAD_REQUEST; // not OK
00049 }
00050 
00051 //___________________________________________________________________
00052 DbiAsciiDbImporter::DbiAsciiDbImporter(const TString& url,TSQLServer* server):
00053   fServer(server),
00054   fTablePreparer(0)
00055 {
00056    // ctor
00057 
00058    LEA_CTOR    //Leak Checker
00059 
00060    MSG("Dbi", Msg::kVerbose) << "Creating DbiAsciiDbImporter "  << (void*) this << endl;
00061 
00062    Import(url,server);
00063 }
00064 
00065 //___________________________________________________________________
00066 DbiAsciiDbImporter::~DbiAsciiDbImporter()
00067 {
00068    // dtor
00069 
00070    LEA_DTOR    //Leak Checker
00071 
00072    MSG("Dbi", Msg::kVerbose) << "Destroying DbiAsciiDbImporter" << endl;
00073    delete fTablePreparer;
00074    fTablePreparer = 0;
00075 }
00076 
00077 //___________________________________________________________________
00078 void DbiAsciiDbImporter::LoadTable(const TString& url)
00079 {
00080    //
00081 
00082    fStatus = HTTP_BAD_REQUEST;
00083 
00084    // Prepare the table for importing.
00085    delete fTablePreparer;
00086    fTablePreparer = 0;
00087    fTablePreparer = new DbiAsciiTablePreparer(url);
00088 
00089    // Check for exceptions from table preparer and include them.
00090    if(!fTablePreparer->IsValid()) {  
00091       fStatus = fTablePreparer->GetStatus();
00092       const DbiExceptionLog& el = fTablePreparer->GetExceptionLog();
00093       if(! el.IsEmpty() ) fExceptionLog.AddLog(el);
00094       delete fTablePreparer;
00095       fTablePreparer = 0;
00096       return;
00097    }
00098 
00099    TString cols  = fTablePreparer->GetColumns();   // read column names,types
00100    TString table = fTablePreparer->GetTableName();
00101    TString file  = fTablePreparer->GetLocalFile();
00102 
00103    TString query("CREATE TEMPORARY TABLE ");
00104    query += table + "(" + cols + ")";
00105 
00106    MSG("Dbi",Msg::kSynopsis) << "Creating table with: " << query << endl;
00107    if(! fServer->Exec(query.Data()) ) {
00108       fExceptionLog.AddEntry(*fServer);
00109       delete fTablePreparer;
00110       fTablePreparer = 0;
00111       fStatus = HTTP_NOT_ACCEPTABLE;
00112       return;
00113    }
00114 
00115    query =  "LOAD DATA ";
00116    query += fTablePreparer->GetLocal() + " INFILE '";
00117    query += file;
00118    query += "' INTO TABLE ";
00119    query += table;
00120    query += " FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '";
00121    query += '\"';
00122    query += "'";
00123 //   query += " ESCAPED BY '\\'";
00124 //   query += " LINES TERMINATED BY '\n'";
00125 
00126    if(fTablePreparer->GetSkipLines()) {
00127       query += " IGNORE ";
00128       query += Form("%d LINES",fTablePreparer->GetSkipLines());
00129    }
00130 
00131    MSG("Dbi",Msg::kSynopsis) << "Filling table with: " << query << endl;
00132    if (! fServer->Exec(query.Data()) ) {
00133       fExceptionLog.AddEntry(*fServer);
00134       delete fTablePreparer;
00135       fTablePreparer = 0;
00136       fStatus = HTTP_NOT_ACCEPTABLE;
00137       return;
00138    }
00139 
00140    fImportedTableNames.push_back(table.Data());
00141    fStatus = HTTP_OK;
00142    return;
00143 }
00144 
00145 //___________________________________________________________________
00146 void DbiAsciiDbImporter::LoadCatalog(const TString& url)
00147 {
00148 
00149   // Laod the catalogue
00150    LoadTable(url);
00151 
00152    if( (fStatus!=HTTP_OK) || !fTablePreparer ) {
00153       return;
00154    }
00155 
00156    TString table = fTablePreparer->GetTableName();
00157    TString query = "SELECT * FROM " + table;
00158 
00159    MSG("Dbi",Msg::kSynopsis) << "Reading catalogue with: " << query << endl;
00160 
00161    TSQLStatement* stmt = fServer->Statement(query.Data());
00162 
00163    if ( ! stmt ) {
00164       fExceptionLog.AddEntry(*fServer);
00165       fStatus = HTTP_NOT_ACCEPTABLE;
00166       return;
00167    } 
00168    stmt->EnableErrorOutput(false);
00169    if ( ! stmt->Process() || ! stmt->StoreResult() ) {
00170       fExceptionLog.AddEntry(*fServer);
00171       fStatus = HTTP_NOT_ACCEPTABLE;
00172       return;
00173    }
00174 
00175    if(fTablePreparer) {
00176       delete fTablePreparer;
00177       fTablePreparer = 0;
00178    }
00179 
00180    while(stmt->NextResultRow()) {
00181       table = stmt->GetString(0); // first column is URL/file
00182       gSystem->ExpandPathName(table);
00183       LoadTable(table);
00184    }
00185 
00186    if(fTablePreparer) {
00187       delete fTablePreparer;
00188       fTablePreparer = 0;
00189    }
00190 
00191    fStatus = HTTP_OK; 
00192    if (stmt) delete stmt;
00193    return;   
00194 }
00195 
00196 //___________________________________________________________________
00197 Int_t DbiAsciiDbImporter::Import(const TString& url,TSQLServer* server)
00198 {
00199    // import data from url to server
00200  
00201    fStatus = HTTP_BAD_REQUEST;
00202 
00203    if( !server ) {
00204       fServer = 0;
00205       fExceptionLog.AddEntry("No server supplied");
00206       return fStatus = HTTP_FORBIDDEN;
00207    }
00208 
00209    fServer = server;
00210    TString ext = strrchr(url.Data(),'.');  // get file extention
00211 
00212    MSG("Dbi",Msg::kSynopsis) << "Importing ASCII data for " << url << endl;
00213 
00214    if( (ext==".cat") || (ext==".db") ) LoadCatalog(url);
00215    else LoadTable(url); 
00216 
00217    return fStatus = HTTP_OK;
00218 }
00219 
00220 //___________________________________________________________________
00221 Bool_t DbiAsciiDbImporter::IsValid() const
00222 {
00223    // 
00224 
00225    return (fStatus < HTTP_BAD_REQUEST);
00226 }
00227 
00228 

Generated on Mon Feb 15 11:06:34 2010 for loon by  doxygen 1.3.9.1