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