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

DbiAsciiTablePreparer Class Reference

Concept A helper class to prepare an ASCII database table file for importing Purpose To simplifly the construction of a temporary (process specific) ASCII database. Acknowledgments The code is essentially a translation of RDBC/TSQLImporterClient by Valeriy Onuchin 21/03/2001 More...

#include <DbiAsciiTablePreparer.h>

List of all members.

Public Member Functions

 DbiAsciiTablePreparer (const TString &url)
virtual ~DbiAsciiTablePreparer ()
const DbiExceptionLogGetExceptionLog () const
TString GetLocal () const
TString GetTableName () const
TString GetColumns () const
TString GetLocalFile () const
Int_t GetSkipLines () const
Int_t GetStatus () const
Bool_t IsValid () const

Private Member Functions

virtual Int_t Init ()
virtual void GET (const TString &url)
virtual void Clean ()

Private Attributes

Bool_t fMustDeleteLocalFile
 local file will be deleted (downloaded file)
DbiExceptionLog fExceptionLog
TUrl * fUrl
 url
TString fLocalFile
 local file
TString fTableName
 table name
TString fColumns
 column names & types
Int_t fStatus
 status (corresponds HTTP Status Codes)
Int_t fSkipLines
 number of lines to skip


Detailed Description

Concept A helper class to prepare an ASCII database table file for importing Purpose To simplifly the construction of a temporary (process specific) ASCII database. Acknowledgments The code is essentially a translation of RDBC/TSQLImporterClient by Valeriy Onuchin 21/03/2001

Id
DbiAsciiTablePreparer.h,v 1.3 2007/04/26 14:19:57 west Exp

DatabaseInterface

Contact: n.west1@physics.ox.ac.uk

Definition at line 35 of file DbiAsciiTablePreparer.h.


Constructor & Destructor Documentation

DbiAsciiTablePreparer::DbiAsciiTablePreparer const TString &  url  ) 
 

Definition at line 78 of file DbiAsciiTablePreparer.cxx.

References fLocalFile, fMustDeleteLocalFile, Form(), fSkipLines, fStatus, fTableName, fUrl, GET(), gSystem(), Init(), LEA_CTOR, and MSG.

00079 {
00080    // ctor.
00081 
00082    LEA_CTOR    //Leak Checker
00083 
00084    MSG("Dbi", Msg::kVerbose) << "Creating DbiAsciiTablePreparer "  << (void*) this << endl;
00085 
00086    fUrl = new TUrl(url);
00087    fStatus = 0;
00088    TString host(fUrl->GetHost());
00089 
00090    TString str(fUrl->GetFile());
00091    fLocalFile = str;
00092 
00093    fTableName = TString(gSystem->BaseName(fLocalFile.Data()));
00094    TString ext = strrchr(fTableName.Data(),'.');
00095 
00096    if(!ext.IsNull()) {
00097       Int_t pidx = fTableName.Index(ext.Data());
00098       if(pidx>1) {
00099          fTableName =  fTableName(0,pidx);
00100       }
00101 
00102       fTableName.ReplaceAll(".","_");
00103    }
00104 
00105 
00106    if( host=="localhost" || host.IsNull() )  {
00107       fMustDeleteLocalFile = kFALSE;
00108       MSG("Dbi", Msg::kSynopsis) << "Preparing table " << fTableName
00109                                  << " from local file " << fLocalFile << endl;
00110    } else {
00111       fMustDeleteLocalFile = kTRUE;
00112       fLocalFile = Form("/tmp/%s%d",gSystem->BaseName(fUrl->GetFile()),gSystem->GetPid());
00113       MSG("Dbi", Msg::kSynopsis) << "Preparing table " << fTableName
00114                                  << " by downloading remote file " << fUrl->GetFile()
00115                                  << " from remote host " << host
00116                                  << " to local file " << fLocalFile << endl;
00117       GET(url);   // download 
00118    }
00119 
00120    fSkipLines = 1; // default , first line is a header describes the columns
00121 
00122    this->Init();
00123 }

DbiAsciiTablePreparer::~DbiAsciiTablePreparer  )  [virtual]
 

Definition at line 126 of file DbiAsciiTablePreparer.cxx.

References Clean(), LEA_DTOR, and MSG.

00127 {
00128    // dtor.
00129 
00130    LEA_DTOR    //Leak Checker
00131 
00132    MSG("Dbi", Msg::kVerbose) << "Destroying DbiAsciiTablePreparer "  << (void*) this << endl;
00133 
00134    Clean();
00135 }


Member Function Documentation

void DbiAsciiTablePreparer::Clean  )  [private, virtual]
 

Definition at line 138 of file DbiAsciiTablePreparer.cxx.

References fLocalFile, and gSystem().

Referenced by ~DbiAsciiTablePreparer().

00139 {
00140    //
00141 
00142    if(fMustDeleteLocalFile) {
00143       gSystem->Unlink(fLocalFile.Data());
00144    }
00145    if(fUrl) delete fUrl; 
00146 }

void DbiAsciiTablePreparer::GET const TString &  url  )  [private, virtual]
 

Definition at line 150 of file DbiAsciiTablePreparer.cxx.

References DbiExceptionLog::AddEntry(), fExceptionLog, fLocalFile, Form(), fStatus, and s().

Referenced by DbiAsciiTablePreparer().

00151 {
00152    // Download url into local temporary file
00153  
00154    TString str;
00155    const Int_t buflen=8192;
00156    static char buf[buflen];
00157 
00158    TString filename = url;
00159    filename.ReplaceAll(" ","");
00160 
00161    TUrl u(filename);
00162  
00163    TSocket s(u.GetHost(), u.GetPort());
00164 
00165    if (!s.IsValid()) {
00166      std::ostringstream oss;
00167      oss << "Unable to open socket to host " << u.GetHost()
00168          <<" port " <<  u.GetPort();
00169      fExceptionLog.AddEntry(oss.str());
00170      fStatus = HTTP_FORBIDDEN;
00171      return;
00172    }
00173 
00174    TString msg = Form("GET %s HTTP/1.0\015\012\015\012", u.GetFile());
00175    s.SendRaw(msg.Data(), msg.Length());
00176 
00177    while(s.RecvRaw(buf, buflen)>0) {         
00178       str += buf; 
00179       memset(buf,0,buflen);
00180    }
00181    s.Close();
00182 
00183    // cutoff HTTP header
00184    Int_t idx;
00185    idx = str.Index("\015\012\015\012");
00186    if(idx!=kNPOS) str = str(idx+4,str.Length()-idx-4);
00187 
00188    std::ofstream out_file(fLocalFile.Data());
00189    if(!out_file) {
00190      std::ostringstream oss;
00191      oss << "Unable to open to " << fLocalFile << " for writing";
00192      fExceptionLog.AddEntry(oss.str());
00193      fStatus = HTTP_FORBIDDEN;
00194    }
00195 
00196    else {
00197      out_file << str;
00198      if( out_file.fail() )  {
00199         std::ostringstream oss;
00200         oss << "Unable to write to " << fLocalFile;
00201         fExceptionLog.AddEntry(oss.str());
00202         fStatus = HTTP_FORBIDDEN;
00203      }
00204    }
00205    out_file.close();
00206    return;
00207 }

TString DbiAsciiTablePreparer::GetColumns  )  const [inline]
 

Definition at line 45 of file DbiAsciiTablePreparer.h.

00045 { return fColumns; } 

const DbiExceptionLog& DbiAsciiTablePreparer::GetExceptionLog  )  const [inline]
 

Definition at line 42 of file DbiAsciiTablePreparer.h.

00042 { return fExceptionLog; }

TString DbiAsciiTablePreparer::GetLocal  )  const [inline]
 

Definition at line 43 of file DbiAsciiTablePreparer.h.

00043 { return fLocalFile.IsNull() ? 0 : "LOCAL"; }

TString DbiAsciiTablePreparer::GetLocalFile  )  const [inline]
 

Definition at line 46 of file DbiAsciiTablePreparer.h.

00046 { return fLocalFile; }

Int_t DbiAsciiTablePreparer::GetSkipLines  )  const [inline]
 

Definition at line 47 of file DbiAsciiTablePreparer.h.

00047 { return fSkipLines; }

Int_t DbiAsciiTablePreparer::GetStatus  )  const [inline]
 

Definition at line 48 of file DbiAsciiTablePreparer.h.

00048 { return fStatus; }

TString DbiAsciiTablePreparer::GetTableName  )  const [inline]
 

Definition at line 44 of file DbiAsciiTablePreparer.h.

00044 { return fTableName; }

Int_t DbiAsciiTablePreparer::Init  )  [private, virtual]
 

Definition at line 210 of file DbiAsciiTablePreparer.cxx.

References DbiExceptionLog::AddEntry(), fColumns, fExceptionLog, fLocalFile, Form(), fSkipLines, fStatus, gSystem(), MSG, and Validate().

Referenced by DbiAsciiTablePreparer().

00211 {
00212    // - read first line from local file 
00213    // - determine column names and types
00214 
00215    TString str;
00216 
00217    if(gSystem->AccessPathName(fLocalFile.Data())) {
00218       fStatus = HTTP_NOT_FOUND;
00219       str = "File ";
00220       str += fLocalFile + " not found";
00221       fExceptionLog.AddEntry(str.Data());
00222       return fStatus;
00223    }
00224 
00225    ifstream in_file(fLocalFile.Data());
00226 
00227    if( !in_file ) {
00228       in_file.close();
00229       fStatus = HTTP_FORBIDDEN;
00230       str = "You don't have read permission to ";
00231       str += fLocalFile;
00232       fExceptionLog.AddEntry(str.Data());
00233       return fStatus;
00234    }
00235    
00236    const Int_t buflen=8192;
00237    char buf[buflen];
00238 
00239    in_file.getline(buf,buflen);  // read first line         
00240    str = buf;
00241 
00242    if(str.IsNull()) {
00243       in_file.close(); // empty file
00244       fStatus = HTTP_NOT_ACCEPTABLE;
00245       str = "File ";
00246       str += fLocalFile + " is empty";
00247       fExceptionLog.AddEntry(str.Data());
00248       return fStatus;
00249    }
00250 
00251    TString tmp;
00252    Int_t i,k;
00253    Int_t ncols = 0;
00254    Bool_t wrongFormat = kFALSE;
00255 
00256    for( i=k=0; (i=str.Index(",",i))>0; k=i++ ) {
00257       ncols++;
00258       tmp = Validate(str(!k?0:k+1,!k?i:i-k-1));
00259       wrongFormat = wrongFormat || tmp.IsNull() || (tmp=="wrong format"); 
00260       if(!wrongFormat) fColumns +=  tmp + ",";
00261    } 
00262 
00263    ncols++;
00264    tmp = Validate(str(k+(ncols>1),str.Length())); // the rest of string
00265 
00266    wrongFormat = wrongFormat || (tmp=="wrong format"); 
00267    if(!wrongFormat)  {
00268       fColumns += tmp;
00269    }
00270    else {
00271       fColumns = "";
00272       for(i=1; i<ncols; i++) fColumns += Form("C%d TEXT NOT NULL,",i);
00273       fColumns += Form("C%d TEXT NOT NULL",ncols);
00274       fSkipLines = 0;
00275       MSG("Dbi",Msg::kWarning) << "Missing header line; treating first line as data" << endl;
00276    }
00277 
00278    in_file.close();
00279    return fStatus = HTTP_OK;
00280 }

Bool_t DbiAsciiTablePreparer::IsValid  )  const [inline]
 

Definition at line 49 of file DbiAsciiTablePreparer.h.

00049 { return ((fStatus >= 200)&&(fStatus < 300)); }


Member Data Documentation

TString DbiAsciiTablePreparer::fColumns [private]
 

column names & types

Definition at line 76 of file DbiAsciiTablePreparer.h.

Referenced by Init().

DbiExceptionLog DbiAsciiTablePreparer::fExceptionLog [private]
 

Log of exceptions generated. Cleared by Open Close and (implicitly) by CreatePreparedStatement, GetServer

Definition at line 64 of file DbiAsciiTablePreparer.h.

Referenced by GET(), and Init().

TString DbiAsciiTablePreparer::fLocalFile [private]
 

local file

Definition at line 70 of file DbiAsciiTablePreparer.h.

Referenced by Clean(), DbiAsciiTablePreparer(), GET(), and Init().

Bool_t DbiAsciiTablePreparer::fMustDeleteLocalFile [private]
 

local file will be deleted (downloaded file)

Definition at line 60 of file DbiAsciiTablePreparer.h.

Referenced by DbiAsciiTablePreparer().

Int_t DbiAsciiTablePreparer::fSkipLines [private]
 

number of lines to skip

Definition at line 82 of file DbiAsciiTablePreparer.h.

Referenced by DbiAsciiTablePreparer(), and Init().

Int_t DbiAsciiTablePreparer::fStatus [private]
 

status (corresponds HTTP Status Codes)

Definition at line 79 of file DbiAsciiTablePreparer.h.

Referenced by DbiAsciiTablePreparer(), GET(), and Init().

TString DbiAsciiTablePreparer::fTableName [private]
 

table name

Definition at line 73 of file DbiAsciiTablePreparer.h.

Referenced by DbiAsciiTablePreparer().

TUrl* DbiAsciiTablePreparer::fUrl [private]
 

url

Definition at line 67 of file DbiAsciiTablePreparer.h.

Referenced by DbiAsciiTablePreparer().


The documentation for this class was generated from the following files:
Generated on Mon Feb 15 11:09:02 2010 for loon by  doxygen 1.3.9.1