Concept A utility to prepare an ASCII database from a a collection of ASCII files. More...
#include <TDbiAsciiDbImporter.hxx>
Public Member Functions | |
TDbiAsciiDbImporter () | |
TDbiAsciiDbImporter (const TString &url, TSQLServer *server) | |
virtual | ~TDbiAsciiDbImporter () |
const TDbiExceptionLog & | GetExceptionLog () const |
Int_t | Import (const TString &url, TSQLServer *server) |
const std::list< std::string > & | GetImportedTableNames () const |
Bool_t | IsValid () const |
Private Member Functions | |
void | LoadCatalog (const TString &url) |
void | LoadTable (const TString &url) |
Private Attributes | |
Int_t | fStatus |
Status of import procedure, fStatus < 400 status is OK. | |
TDbiExceptionLog | fExceptionLog |
Log of exceptions generated. | |
std::list< std::string > | fImportedTableNames |
List of imported table names. | |
TSQLServer * | fServer |
Where to import data. Not owned. | |
TDbiAsciiTablePreparer * | fTablePreparer |
Helper class used to prepare a single table. May be null. |
Concept A utility to prepare an ASCII database from a a collection of ASCII files.
Purpose To prepare a temporary (process specific) ASCII database.
Acknowledgments The code is essentially a translation of RDBC/TSQLImporter by Valeriy Onuchin 21/03/2001
Contact: A.Finch@lancaster.ac.uk
Definition at line 37 of file TDbiAsciiDbImporter.hxx.
TDbiAsciiDbImporter::TDbiAsciiDbImporter | ( | ) |
TDbiAsciiDbImporter::TDbiAsciiDbImporter | ( | const TString & | url, | |
TSQLServer * | server | |||
) |
Definition at line 51 of file TDbiAsciiDbImporter.cxx.
References Import(), and SK_DBI_Trace.
00051 : 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 }
TDbiAsciiDbImporter::~TDbiAsciiDbImporter | ( | ) | [virtual] |
Definition at line 64 of file TDbiAsciiDbImporter.cxx.
References fTablePreparer, and SK_DBI_Trace.
00065 { 00066 // dtor 00067 00068 00069 SK_DBI_Trace( "Destroying TDbiAsciiDbImporter" << " "); 00070 delete fTablePreparer; 00071 fTablePreparer = 0; 00072 }
const TDbiExceptionLog& TDbiAsciiDbImporter::GetExceptionLog | ( | ) | const [inline] |
Definition at line 44 of file TDbiAsciiDbImporter.hxx.
References fExceptionLog.
Referenced by TDbiConnection::Open().
00044 { return fExceptionLog; }
const std::list<std::string>& TDbiAsciiDbImporter::GetImportedTableNames | ( | ) | const [inline] |
Definition at line 46 of file TDbiAsciiDbImporter.hxx.
References fImportedTableNames.
Referenced by TDbiConnection::Open().
00046 { return fImportedTableNames; }
Int_t TDbiAsciiDbImporter::Import | ( | const TString & | url, | |
TSQLServer * | server | |||
) |
Definition at line 194 of file TDbiAsciiDbImporter.cxx.
References TDbiExceptionLog::AddEntry(), fExceptionLog, fServer, fStatus, HTTP_BAD_REQUEST, HTTP_FORBIDDEN, HTTP_OK, LoadCatalog(), LoadTable(), and SK_DBI_Log.
Referenced by TDbiAsciiDbImporter().
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 }
Bool_t TDbiAsciiDbImporter::IsValid | ( | ) | const |
Definition at line 218 of file TDbiAsciiDbImporter.cxx.
References fStatus, and HTTP_BAD_REQUEST.
00219 { 00220 // 00221 00222 return (fStatus < HTTP_BAD_REQUEST); 00223 }
void TDbiAsciiDbImporter::LoadCatalog | ( | const TString & | url | ) | [private] |
Definition at line 143 of file TDbiAsciiDbImporter.cxx.
References TDbiExceptionLog::AddEntry(), fExceptionLog, fServer, fStatus, fTablePreparer, TDbiAsciiTablePreparer::GetTableName(), HTTP_NOT_ACCEPTABLE, HTTP_OK, LoadTable(), and SK_DBI_Log.
Referenced by Import().
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 }
void TDbiAsciiDbImporter::LoadTable | ( | const TString & | url | ) | [private] |
Definition at line 75 of file TDbiAsciiDbImporter.cxx.
References TDbiExceptionLog::AddEntry(), TDbiExceptionLog::AddLog(), fExceptionLog, fImportedTableNames, fServer, fStatus, fTablePreparer, TDbiAsciiTablePreparer::GetColumns(), TDbiAsciiTablePreparer::GetExceptionLog(), TDbiAsciiTablePreparer::GetLocal(), TDbiAsciiTablePreparer::GetLocalFile(), TDbiAsciiTablePreparer::GetSkipLines(), TDbiAsciiTablePreparer::GetStatus(), TDbiAsciiTablePreparer::GetTableName(), HTTP_BAD_REQUEST, HTTP_NOT_ACCEPTABLE, HTTP_OK, TDbiExceptionLog::IsEmpty(), TDbiAsciiTablePreparer::IsValid(), and SK_DBI_Log.
Referenced by Import(), and LoadCatalog().
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 }
Log of exceptions generated.
Definition at line 59 of file TDbiAsciiDbImporter.hxx.
Referenced by GetExceptionLog(), Import(), LoadCatalog(), and LoadTable().
std::list<std::string> TDbiAsciiDbImporter::fImportedTableNames [private] |
List of imported table names.
Definition at line 62 of file TDbiAsciiDbImporter.hxx.
Referenced by GetImportedTableNames(), and LoadTable().
TSQLServer* TDbiAsciiDbImporter::fServer [private] |
Where to import data. Not owned.
Definition at line 66 of file TDbiAsciiDbImporter.hxx.
Referenced by Import(), LoadCatalog(), and LoadTable().
Int_t TDbiAsciiDbImporter::fStatus [private] |
Status of import procedure, fStatus < 400 status is OK.
Definition at line 56 of file TDbiAsciiDbImporter.hxx.
Referenced by Import(), IsValid(), LoadCatalog(), and LoadTable().
Helper class used to prepare a single table. May be null.
Definition at line 69 of file TDbiAsciiDbImporter.hxx.
Referenced by LoadCatalog(), LoadTable(), and ~TDbiAsciiDbImporter().