// -*- mode: c++ -*- // // $Id: BrDb.h,v 1.6 2002/03/21 23:35:05 cholm Exp $ // $Author: cholm $ // $Date: 2002/03/21 23:35:05 $ // $Copyright: (C) 2001 BRAHMS Collaboration // #ifndef BRAT_BrDb #define BRAT_BrDb #ifndef ROOT_TNamed #include "TNamed.h" #endif #ifndef ROOT_TString #include "TString.h" #endif #ifndef ROOT_TSQLServer #include "TSQLServer.h" #endif #ifndef ROOT_TSQLResult #include "TSQLResult.h" #endif #ifndef ROOT_TSQLRow #include "TSQLRow.h" #endif #ifndef BRAT_BrDbTable #include "BrDbTable.h" #endif #ifndef BRAT_BrDbQuery #include "BrDbQuery.h" #endif #define BRAHMS_RDBM_TYPE "mysql" #define BR_USE_DISCONNECT class BrVirtualDb : public TNamed { protected: TString fRdbmType; // What kind of server TString fHostName; // Host name for this database TString fUserName; //! Cache DBM user name TString fPasswd; //! Cache DBM password Bool_t fIsLocked; //! Is some tables looked public: BrVirtualDb(const Char_t* name, const Char_t* title=""); virtual ~BrVirtualDb(void); virtual Bool_t Connect(Option_t* option="") = 0; virtual void Close(Option_t* option="") = 0; virtual TSQLResult* Query(BrDbQuery* query) = 0; virtual TSQLRow* GetSingle(const Char_t* table, const Char_t* condition) = 0; virtual TSQLResult* GetMultiple(const Char_t* table, const Char_t* condition) = 0; virtual TObject* GetConnection() = 0; const Char_t* GetDbName() const { return GetTitle(); } const Char_t* GetUserName() const { return fUserName.Data(); } const Char_t* GetHostName() const { return fHostName.Data(); } const Char_t* GetRdbmType() const { return fRdbmType.Data(); } void SetDbName(const Char_t* dbName) { SetTitle(dbName); } void SetUserName(const Char_t* name) { fUserName = name; } void SetHostName(const Char_t* host) { fHostName = host; } void SetRdbmType(const Char_t* type) { fRdbmType = type; } virtual Bool_t IsConnected() const = 0; virtual void LockTables(const Char_t* writetables,int i=0) = 0; virtual void UnLockTables() = 0; virtual Int_t Increment() = 0; virtual void CreateSequence() = 0; ClassDef(BrVirtualDb,0) // ABC for Database connectivity } ; class BrDb : public TNamed { protected: BrVirtualDb* fImplementation; // Pointer to interface implemetation static const Char_t kHexNumbers[16]; // Character values for hex numbers Int_t fVerbose; //! Int_t fDebugLevel; //! virtual TObject* GetConnection(); BrVirtualDb* GetImplementation() { return fImplementation; } public: BrDb(); virtual ~BrDb() { delete fImplementation; } static Char_t* Addr2String(void* src, Ssiz_t size); static Char_t* Short2String(void* src, const Int_t length); static Char_t* Int2String(void* src, const Int_t length); static Char_t* Long2String(void* src, const Int_t length); static Char_t* Float2String(void* src, const Int_t length); static Char_t* Double2String(void* src, const Int_t length); static Float_t* String2Float(const Char_t* str, const Int_t length); static Double_t* String2Double(const Char_t* str, const Int_t length); static Int_t* String2Int(const Char_t* str, const Int_t length); static Long_t* String2Long(const Char_t* str, const Int_t length); static Short_t* String2Short(const Char_t* str, const Int_t length); static Byte_t* String2Byte(const Char_t* str, const Int_t length); static Bool_t* String2Bool(const Char_t* str, const Int_t length); static TString* SplitList(const Char_t* commalist, const Char_t* extra); const Char_t* GetDbName() const; const Char_t* GetUserName() const; const Char_t* GetHostName() const; const Char_t* GetRdbmType() const; void SetDbName(const Char_t* dbName); void SetUserName(const Char_t* name); void SetHostName(const Char_t* host); void SetRdbmType(const Char_t* type); void SetDebugLevel(Int_t level=0) { fDebugLevel = level; } void SetVerbose(Int_t level=0) { fVerbose = level; } Int_t DebugLevel() const { return fDebugLevel; } Int_t Verbose() const { return fVerbose; } virtual void Print(Option_t* option="") const; virtual Bool_t Connect(Option_t* option=""); virtual void Close(Option_t* option=""); virtual TSQLResult* Query(BrDbQuery* query); virtual TSQLRow* GetSingle(const Char_t* table, const Char_t* condition); virtual TSQLResult* GetMultiple(const Char_t* table, const Char_t* condition); virtual Bool_t IsConnected() const; virtual void LockTables(const Char_t* writetables,int i=0); virtual void UnLockTables(); virtual Int_t Increment(); virtual void CreateSequence(); //____________________________________________________________________ #ifdef R__BYTESWAP static inline Float_t Host2net(Float_t a) { #if defined(__linux) && defined(__i386__) && defined __GNUC__ && __GNUC__ >= 2 UInt_t t = Rbswap_32(*((UInt_t*)&a)); return *(Float_t*)&t; #else // UInt_t *x = (UInt_t *)&xx; UInt_t *x = (UInt_t *)&a; *x = (((*x & 0x000000ffU) << 24) | ((*x & 0x0000ff00U) << 8) | ((*x & 0x00ff0000U) >> 8) | ((*x & 0xff000000U) >> 24)); return a; return xx; #endif } static inline Double_t Host2net(Double_t a){ #if defined(__linux) && defined(__i386__) && defined __GNUC__ && __GNUC__ >= 2 unsigned long long t = Rbswap_64(*((unsigned long long*)&a)); return *(Double_t*)&t; #else char sw[sizeof(Double_t)]; // *(Double_t *)sw = x; *(Double_t *)sw = a; // char *sb = (char *)&x; char *sb = (char *)&a; sb[0] = sw[7]; sb[1] = sw[6]; sb[2] = sw[5]; sb[3] = sw[4]; sb[4] = sw[3]; sb[5] = sw[2]; sb[6] = sw[1]; sb[7] = sw[0]; // return x; return a; #endif } #else /* R__BYTESWAP */ static inline Float_t Host2net(Float_t x) { return x; } static inline Double_t Host2net(Double_t x) { return x; } #endif static inline Float_t Net2host(Float_t a){return Host2net(a);} static inline Double_t Net2host(Double_t a){return Host2net(a);} ClassDef(BrDb,0) // Wrapper for DB interface implementation }; #endif //____________________________________________________________________ // // $Log: BrDb.h,v $ // Revision 1.6 2002/03/21 23:35:05 cholm // Added the preprocessor flag BRAT_USE_DISCONNECT. If defined, the // managers will disconnect from the database after an Update. This is // so that we don't take up too many slots on the server. MySQL, in it's // current setup has a limit of 1000 simultaneous connections, and so // disconnecting from the server may allow more jobs to run. // // Revision 1.5 2001/11/27 18:24:10 cholm // Corrected a mistake in Host2net that prevented compilation on Digital // Unix. // // Revision 1.4 2001/11/26 20:33:06 videbaek // Fix my mistake for Double coversions // // Revision 1.3 2001/11/26 18:36:03 videbaek // Update for modified DB Revision Tables. // // Revision 1.2 2001/10/08 10:31:14 cholm // Changed to allow polymorphic access classes. The class BrDb defines the // interface, while BrRdbmDb and BrRootDb implements the interface. Other // implmentations could be BrAsciiDb, BrObjyDb (shudder), BrXmlDb, and so on. // The table representation of the BrahmsMain.DB table has been moved from // BrDb to BrDbDb. // //