TDbiAsciiTablePreparer Class Reference

Concept A helper class to prepare an ASCII database table file for importing More...

#include <TDbiAsciiTablePreparer.hxx>

Collaboration diagram for TDbiAsciiTablePreparer:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 TDbiAsciiTablePreparer (const TString &url)
virtual ~TDbiAsciiTablePreparer ()
const TDbiExceptionLogGetExceptionLog () 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)
TDbiExceptionLog 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

Id
TDbiAsciiTablePreparer.hxx,v 1.1 2011/01/18 05:49:19 finch Exp

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

Contact: A.Finch@lancaster.ac.uk

Definition at line 36 of file TDbiAsciiTablePreparer.hxx.


Constructor & Destructor Documentation

TDbiAsciiTablePreparer::TDbiAsciiTablePreparer ( const TString &  url  ) 

Definition at line 78 of file TDbiAsciiTablePreparer.cxx.

References fLocalFile, fMustDeleteLocalFile, fSkipLines, fStatus, fTableName, fUrl, GET(), Init(), SK_DBI_Log, and SK_DBI_Trace.

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

Here is the call graph for this function:

TDbiAsciiTablePreparer::~TDbiAsciiTablePreparer (  )  [virtual]

Definition at line 125 of file TDbiAsciiTablePreparer.cxx.

References Clean(), and SK_DBI_Trace.

00126 {
00127    // dtor.
00128 
00129 
00130    SK_DBI_Trace( "Destroying TDbiAsciiTablePreparer "  << (void*) this << "  ");
00131 
00132    Clean();
00133 }

Here is the call graph for this function:


Member Function Documentation

void TDbiAsciiTablePreparer::Clean (  )  [private, virtual]

Definition at line 136 of file TDbiAsciiTablePreparer.cxx.

References fLocalFile, fMustDeleteLocalFile, and fUrl.

Referenced by ~TDbiAsciiTablePreparer().

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

Here is the caller graph for this function:

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

Definition at line 148 of file TDbiAsciiTablePreparer.cxx.

References TDbiExceptionLog::AddEntry(), fExceptionLog, fLocalFile, fStatus, and HTTP_FORBIDDEN.

Referenced by TDbiAsciiTablePreparer().

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

Here is the call graph for this function:

Here is the caller graph for this function:

TString TDbiAsciiTablePreparer::GetColumns (  )  const [inline]

Definition at line 46 of file TDbiAsciiTablePreparer.hxx.

References fColumns.

Referenced by TDbiAsciiDbImporter::LoadTable().

00046 { return fColumns; }

Here is the caller graph for this function:

const TDbiExceptionLog& TDbiAsciiTablePreparer::GetExceptionLog (  )  const [inline]

Definition at line 43 of file TDbiAsciiTablePreparer.hxx.

References fExceptionLog.

Referenced by TDbiAsciiDbImporter::LoadTable().

00043 { return fExceptionLog; }

Here is the caller graph for this function:

TString TDbiAsciiTablePreparer::GetLocal (  )  const [inline]

Definition at line 44 of file TDbiAsciiTablePreparer.hxx.

References fLocalFile.

Referenced by TDbiAsciiDbImporter::LoadTable().

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

Here is the caller graph for this function:

TString TDbiAsciiTablePreparer::GetLocalFile (  )  const [inline]

Definition at line 47 of file TDbiAsciiTablePreparer.hxx.

References fLocalFile.

Referenced by TDbiAsciiDbImporter::LoadTable().

00047 { return fLocalFile; }

Here is the caller graph for this function:

Int_t TDbiAsciiTablePreparer::GetSkipLines (  )  const [inline]

Definition at line 48 of file TDbiAsciiTablePreparer.hxx.

References fSkipLines.

Referenced by TDbiAsciiDbImporter::LoadTable().

00048 { return fSkipLines; }

Here is the caller graph for this function:

Int_t TDbiAsciiTablePreparer::GetStatus (  )  const [inline]

Definition at line 49 of file TDbiAsciiTablePreparer.hxx.

References fStatus.

Referenced by TDbiAsciiDbImporter::LoadTable().

00049 { return fStatus; }

Here is the caller graph for this function:

TString TDbiAsciiTablePreparer::GetTableName (  )  const [inline]

Definition at line 45 of file TDbiAsciiTablePreparer.hxx.

References fTableName.

Referenced by TDbiAsciiDbImporter::LoadCatalog(), and TDbiAsciiDbImporter::LoadTable().

00045 { return fTableName; }

Here is the caller graph for this function:

Int_t TDbiAsciiTablePreparer::Init (  )  [private, virtual]

Definition at line 208 of file TDbiAsciiTablePreparer.cxx.

References TDbiExceptionLog::AddEntry(), fColumns, fExceptionLog, fLocalFile, fSkipLines, fStatus, HTTP_FORBIDDEN, HTTP_NOT_ACCEPTABLE, HTTP_NOT_FOUND, HTTP_OK, and SK_DBI_Warn.

Referenced by TDbiAsciiTablePreparer().

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

Here is the call graph for this function:

Here is the caller graph for this function:

Bool_t TDbiAsciiTablePreparer::IsValid (  )  const [inline]

Definition at line 50 of file TDbiAsciiTablePreparer.hxx.

References fStatus.

Referenced by TDbiAsciiDbImporter::LoadTable().

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

Here is the caller graph for this function:


Member Data Documentation

column names & types

Definition at line 77 of file TDbiAsciiTablePreparer.hxx.

Referenced by GetColumns(), and Init().

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

Definition at line 65 of file TDbiAsciiTablePreparer.hxx.

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

local file

Definition at line 71 of file TDbiAsciiTablePreparer.hxx.

Referenced by Clean(), GET(), GetLocal(), GetLocalFile(), Init(), and TDbiAsciiTablePreparer().

local file will be deleted (downloaded file)

Definition at line 61 of file TDbiAsciiTablePreparer.hxx.

Referenced by Clean(), and TDbiAsciiTablePreparer().

number of lines to skip

Definition at line 83 of file TDbiAsciiTablePreparer.hxx.

Referenced by GetSkipLines(), Init(), and TDbiAsciiTablePreparer().

status (corresponds HTTP Status Codes)

Definition at line 80 of file TDbiAsciiTablePreparer.hxx.

Referenced by GET(), GetStatus(), Init(), IsValid(), and TDbiAsciiTablePreparer().

table name

Definition at line 74 of file TDbiAsciiTablePreparer.hxx.

Referenced by GetTableName(), and TDbiAsciiTablePreparer().

url

Definition at line 68 of file TDbiAsciiTablePreparer.hxx.

Referenced by Clean(), and TDbiAsciiTablePreparer().


The documentation for this class was generated from the following files:

Generated on 11 Aug 2013 for SKDatabase by  doxygen 1.6.1