TDbiCascader.hxx

Go to the documentation of this file.
00001 
00002 #ifndef DBICASCADER_H
00003 #define DBICASCADER_H
00004 
00005 //////////////////////////////////////////////////////////////////////////
00006 ////////////////////////////     ROOT API     ////////////////////////////
00007 //////////////////////////////////////////////////////////////////////////
00008 
00009 /**
00010  *
00011  *
00012  * \class TDbiCascader
00013  *
00014  *
00015  * \brief
00016  * <b>Concept</b> A cascade (prioritorised list) of TDbiConnection s, one
00017  *  for each database in the cascade.
00018  *
00019  * \brief
00020  * <b>Purpose</b> Implements the concept of a cascade allowing user to
00021  *   overrride parts of the standard database by introducing higher
00022  *   priority non-standard ones above it in a cascade.
00023  *
00024  * Contact: A.Finch@lancaster.ac.uk
00025  *
00026  *
00027  */
00028 
00029 #include <map>
00030 #include <ostream>
00031 using std::ostream;
00032 #include <string>
00033 using std::string;
00034 #include <vector>
00035 using std::vector;
00036 
00037 #if !defined(__CINT__) || defined(__MAKECINT__)
00038 #include "Rtypes.h"
00039 #endif
00040 
00041 #include "TDbiConnection.hxx"
00042 #include "TDbiStatement.hxx"
00043 class TDbiCascader;
00044 ostream& operator<<(ostream& os, const TDbiCascader& cascader) ;
00045 class TSQL_Statement;
00046 
00047 
00048 class TDbiCascader
00049 {
00050 
00051   friend class TDbiDatabaseManager;  //Only it can create
00052   friend ostream& operator<<(ostream& s, const TDbiCascader& cascader);
00053 
00054 public:
00055 
00056   /// Check we can connect to the database defined in the environment variables. Allows a
00057     /// user program to skip using the database if it doesn't need to and it is not there, or
00058     /// if it does need it then die in a civilised manner.
00059   static bool canConnect();
00060   friend class TDbiValidate;      //Allow TDbiValidate access SetAuthorisingEntry
00061 
00062   enum Status { kFailed, kClosed, kOpen };
00063 
00064 // State testing member functions
00065 
00066 // Cascade entry-specific getters.
00067 
00068 /// Create a TDbiStatement.  Caller must delete.
00069  TDbiStatement* CreateStatement(UInt_t dbNo) const;
00070 
00071 /// Return associated TDbiConnection. TDbiCascader retains ownership.
00072 const TDbiConnection*
00073                GetConnection(UInt_t dbNo) const;
00074       TDbiConnection*
00075                GetConnection(UInt_t dbNo) ;
00076 
00077         string GetDbName(UInt_t dbNo) const;
00078          Int_t GetDbNo(const string& dbName) const;
00079          Int_t GetStatus(UInt_t dbNo) const {
00080                  if ( dbNo >= GetNumDb() || ! fConnections[dbNo]  ) return kFailed;
00081                  return fConnections[dbNo]->IsClosed() ? kClosed : kOpen; }
00082         string GetStatusAsString(UInt_t dbNo) const ;
00083         string GetURL(UInt_t dbNo) const {
00084                       return ( dbNo < GetNumDb() ) ? fConnections[dbNo]-> GetUrl(): ""; }
00085         Bool_t IsTemporaryTable(const string& tableName,
00086                                 Int_t dbNo) const;
00087 // Cascade-wide getters.
00088 
00089          Int_t AllocateSeqNo(const string& tableName,
00090                              Int_t requireGlobal = 0,
00091                              Int_t dbNo = 0) const;
00092          Int_t GetAuthorisingDbNo() const { return fGlobalSeqNoDbNo; }
00093         UInt_t GetNumDb() const {return fConnections.size();}
00094         Int_t  GetTableDbNo(const string& tableName, Int_t selectDbNo = -1) const;
00095         Bool_t TableExists(const string& tableName, Int_t selectDbNo = -1) const {
00096                             return this->GetTableDbNo(tableName,selectDbNo) >= 0; }
00097 
00098 // State changing member functions
00099 
00100            Int_t CreateTemporaryTable(const string& tableName,
00101                                       const string& tableDescr);
00102            void HoldConnections();
00103            void ReleaseConnections();
00104            void SetPermanent(UInt_t dbNo, Bool_t permanent = true);
00105 
00106 protected:
00107 
00108 private:
00109 
00110      Int_t ReserveNextSeqNo(const string& tableName,
00111                             Bool_t isGlobal,
00112                             UInt_t dbNo) const;
00113      void SetAuthorisingEntry(Int_t entry) {fGlobalSeqNoDbNo = entry;}
00114 
00115 // Constructors and destructors.
00116      TDbiCascader(bool beQuiet=false);
00117   virtual ~TDbiCascader();
00118 TDbiCascader(const TDbiCascader&);  // Not implemented
00119 
00120 // Data members
00121 
00122 /// 1st db in cascade with GlobalSeqNo table
00123   Int_t fGlobalSeqNoDbNo;
00124 
00125 /// Vector of TDbiConnections, one for each DB
00126   vector<TDbiConnection*> fConnections;
00127 
00128 /// Mapping Name->DbNo for temporary tables.
00129   std::map<string,Int_t> fTemporaryTables;
00130 
00131 
00132 /// Private Locker object used by TDbiCascader
00133 class Lock {
00134 
00135 public:
00136        Lock(TDbiStatement* stmtDB, const string& seqnoTable, const string& dataTable);
00137       ~Lock();
00138 
00139 Bool_t IsLocked() const { return fLocked; }
00140 
00141 private:
00142 
00143   void SetLock(Bool_t setting = kTRUE);
00144 
00145    TDbiStatement* fStmt;            // Statement to be used to issue lock
00146           string fSeqnoTableName;  // The SEQNO table that is locked
00147           string fDataTableName;   // The data table that is locked
00148           Bool_t fLocked;          // Lock status
00149 
00150 };
00151 
00152  ClassDef(TDbiCascader,0)     //A cascade of TDbiConnectionss
00153 
00154 };
00155 
00156 
00157 #endif  // DBISKEL_H
00158 

Generated on 11 Aug 2013 for SKDatabase by  doxygen 1.6.1