Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:22:06

0001 // $Id: TSQLImporter.cxx,v 1.1.1.1 2004/02/18 20:58:02 dave Exp $
0002 //*-- Author : Valeriy Onuchin 20/03/2001
0003 //
0004 
0005 ////////////////////////////////////////////////////////////////////
0006 //
0007 // TSQLImporter
0008 //
0009 ////////////////////////////////////////////////////////////////////
0010 #include <RDBC/TSQLImporter.h>
0011 #include <RDBC/TSQLImportClient.h>
0012 #include <RDBC/TSQLConnection.h>
0013 #include <RDBC/TSQLUrl.h>
0014 #include <RDBC/TSQLStatement.h>
0015 #include <RDBC/TSQLResultSet.h>
0016 #include <RDBC/TSQLDriverManager.h>
0017 
0018 ClassImpQ(TSQLImporter)
0019 /////////////////////////////////////////////////////////////////////
0020 //___________________________________________________________________
0021 TSQLImporter::TSQLImporter():TQObject()
0022 {
0023    // ctor
0024 
0025    fConnection = 0;
0026    fClient = 0;
0027    fStatus = 400; // not OK
0028 }
0029 
0030 //___________________________________________________________________
0031 void TSQLImporter::LoadTable(const TString& url)
0032 {
0033    //
0034 
0035    fStatus = 400;
0036    TSQLStatement* stmt = 0;
0037 
0038    fClient = new TSQLImportClient(url);
0039 
0040    // catch exceptions from client and re-emmit exception
0041    fClient->Init();
0042 
0043    if(!fClient->IsValid()) {  // failed to create client
0044       fStatus = fClient->GetStatus();
0045       TSQLException* e = fClient->GetException();
0046        if(e) {
0047      // Create a copy of the exception; the owning client is about to be destroyed.
0048         TSQLException* clientException = new TSQLException();
0049         *clientException = *e;
0050         Throw(clientException);
0051        }
0052       if(fClient) delete fClient;
0053       fClient = 0;
0054       return;
0055    }
0056 
0057    TString cols = fClient->GetColumns();   // read column names,types
0058    TString table = fClient->GetTableName();
0059    TString file = fClient->GetLocalFile();
0060 
0061    TString query;
0062    TString query1;
0063    Int_t update_count;
0064 
0065    stmt = fConnection->CreateStatement();
0066 
0067    if(!stmt) {
0068       Throw(new TSQLException("Failed to create SQL statement","",HTTP_NOT_ACCEPTABLE));
0069       if(fClient) delete fClient;
0070       Destroyed();
0071       fClient = 0;
0072       fStatus = HTTP_NOT_ACCEPTABLE;
0073       return;
0074    }
0075 
0076    query1 =  "CREATE TEMPORARY TABLE ";
0077    query1 += table + "(" + cols + ")";
0078 
0079    update_count = stmt->ExecuteUpdate(query1);
0080 
0081    query =  "LOAD DATA ";
0082    query += fClient->GetLocal() + " INFILE '";
0083    query += file;
0084    query += "' INTO TABLE ";
0085    query += table;
0086    query += " FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '";
0087    query += '\"';
0088    query += "'";
0089 //   query += " ESCAPED BY '\\'";
0090 //   query += " LINES TERMINATED BY '\n'";
0091 
0092    if(fClient->GetSkipLines()) {
0093       query += " IGNORE ";
0094       query += Form("%d LINES",fClient->GetSkipLines());
0095    }
0096 
0097    update_count = stmt->ExecuteUpdate(query);
0098 
0099    if(!update_count) {  //
0100       Throw(new TSQLException(Form("Wrong format:\n%s\n%s",query1.Data(),query.Data()),"",HTTP_NOT_ACCEPTABLE));
0101       Destroyed();
0102       SafeDelete(fClient);
0103       fStatus = HTTP_NOT_ACCEPTABLE;
0104    }
0105 
0106    fStatus = HTTP_OK;
0107    if(stmt) delete stmt;
0108    return;
0109 }
0110 
0111 //___________________________________________________________________
0112 void TSQLImporter::LoadCatalog(const TString& url)
0113 {
0114    //
0115 
0116    LoadTable(url);   // load
0117 
0118    if( (fStatus!=HTTP_OK) || !fClient ) {
0119       return;
0120    }
0121 
0122    TSQLStatement* stmt = fConnection->CreateStatement();
0123    TString table = fClient->GetTableName();
0124    TString query;
0125 
0126 //   query = "ALTER TABLE " + table;
0127 //   query += " ADD table_name varchar(64)";
0128 
0129    query = "SELECT * FROM " + table;
0130 
0131    TSQLResultSet* rs = stmt->ExecuteQuery(query);
0132 
0133    if(!rs) {
0134       fStatus = HTTP_NOT_ACCEPTABLE;
0135       return;
0136    } 
0137 
0138    if(fClient) {
0139       delete fClient;
0140       fClient = 0;
0141    }
0142 
0143    while(rs->Next()) {
0144       table = rs->GetString(1); // first column is URL/file
0145       LoadTable(table);
0146    }
0147 
0148    if(fClient) {
0149       delete fClient;
0150       fClient = 0;
0151    }
0152 
0153    fStatus = HTTP_OK; 
0154    if(stmt) delete stmt;
0155    return;   
0156 }
0157 
0158 //___________________________________________________________________
0159 Int_t TSQLImporter::Import(const TString& url,TSQLConnection* con)
0160 {
0161    // import data from url to con
0162  
0163    fStatus = 400;
0164 
0165    if(!con) {
0166       fConnection = 0;
0167       //Throw(new TSQLException("Connection not defined","",HTTP_FORBIDDEN));
0168       return fStatus = HTTP_FORBIDDEN;
0169    }
0170 
0171    fConnection = con;
0172    TString ext = strrchr(url.Data(),'.');  // get file extention
0173 
0174    if( (ext==".cat") || (ext==".db") ) LoadCatalog(url);
0175    else LoadTable(url); 
0176 
0177    return fStatus = HTTP_OK;
0178 }
0179 
0180 //___________________________________________________________________
0181 Bool_t TSQLImporter::IsValid() const
0182 {
0183    // 
0184 
0185    return (fStatus < 400);
0186 }
0187 
0188 //___________________________________________________________________
0189 TSQLImporter::~TSQLImporter()
0190 {
0191    // dtor
0192 
0193    SafeDelete(fClient); 
0194 }
0195