|
//____________________________________________________________________ // // Base class for BRAHMS databases. Individual database classes can // inherit from this and then define the mathods in needs. E.g., a // calibration databse classs, can define the methods // "BrCalibObj* GetAround(time_t t)" to get calibration valid around // the unix_timestamp t, and a run database could define "BrRunInfo* // GetRun(int no)" etc. // // The actual database name is stored on the TNamed::fTitle, while the // symbolic name is stored in TNamed::fName // //____________________________________________________________________ // // $Id: BrDb.cxx,v 1.7 2002/03/21 23:35:40 cholm Exp $ // $Author: cholm $ // $Date: 2002/03/21 23:35:40 $ // $Copyright: (C) 2001 BRAHMS Collaboration <brahmlib@rhic.bnl.gov> // #ifndef BRAT_BrDb #include "BrDb.h" #endif #include <Getline.h> #ifndef ROOT_TString #include "TString.h" #endif #ifndef ROOT_TSystem #include "TSystem.h" #endif #ifndef ROOT_TInetAddress #include "TInetAddress.h" #endif #ifndef ROOT_TRegexp #include "TRegexp.h" #endif #ifndef __IOSTREAM__ #include <iostream> #endif #ifndef __FSTREAM__ #include <fstream> #endif #ifndef __IOMANIP__ #include <iomanip> #endif #include <sys/stat.h> #ifdef BR_USE_EXCEPTIONS #undef BR_USE_EXCEPTIONS #endif //____________________________________________________________________ ClassImp(BrDb); //____________________________________________________________________ BrDb::BrDb() { // Default constructor fImplementation = 0; fVerbose = 0; fDebugLevel = 0; } //____________________________________________________________________ const Char_t BrDb::kHexNumbers[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; //____________________________________________________________________ Char_t* BrDb::Addr2String(void* addr, Ssiz_t size) { // Converts an array of size bytes at address addr, to a C string // 2*size+1 long, conytaining a character representation of the // hexadecimail numbers at the address. const int n = size/sizeof(Byte_t); Char_t* out = new Char_t[2 * n + 1]; Byte_t* byte = (Byte_t*)addr; for (Int_t i = 0; i < n; i++) { out[2 * i + 0] = BrDb::kHexNumbers[int(*byte / 16)]; out[2 * i + 1] = BrDb::kHexNumbers[int(*byte % 16)]; out[2 * i + 2] = '0'; byte++; } return out; } //____________________________________________________________________ Char_t* BrDb::Short2String(void* addr, const Int_t size) { // Converts an array of size bytes at address addr, to a C string // 2*size+1 long, conytaining a character representation of the // hexadecimail numbers at the address. const int n = size/sizeof(Short_t); Short_t * arr = (Short_t*) addr; for(Int_t i=0; i< n; i++) arr[i]=host2net(arr[i]); return BrDb::Addr2String((Byte_t*) arr, size); } //____________________________________________________________________ Char_t* BrDb::Int2String(void* addr, const Int_t size) { // Converts an array of size bytes at address addr, to a C string // 2*size+1 long, conytaining a character representation of the // hexadecimail numbers at the address. const int n = size / sizeof(Int_t); Int_t * arr = (Int_t*) addr; for(Int_t i=0; i< n; i++) arr[i]=host2net(arr[i]); return BrDb::Addr2String((Byte_t*) arr, size); } //____________________________________________________________________ Char_t* BrDb::Long2String(void* addr, const Int_t size) { // Converts an array of size bytes at address addr, to a C string // 2*size+1 long, containing a character representation of the // hexadecimail numbers at the address properly swapped according to // the net representation of the Root datatypes. // const int n = size / sizeof(Long_t); Long_t * arr = (Long_t*) addr; for(Int_t i=0; i< n; i++) arr[i]=host2net(arr[i]); return BrDb::Addr2String((Byte_t*) arr, size); } //____________________________________________________________________ Char_t* BrDb::Float2String(void* addr, const Int_t size) { // Converts an array of size bytes at address addr, to a C string // 2*size+1 long, conytaining a character representation of the // hexadecimail numbers at the address. const int n = size / sizeof(Float_t); Float_t* arr = (Float_t*)addr; for(Int_t i=0; i< n; i++) arr[i]=BrDb::Host2net(arr[i]); return BrDb::Addr2String((Byte_t*) arr, size); } //____________________________________________________________________ Char_t* BrDb::Double2String(void* addr, const Int_t size) { // Converts an array of size bytes at address addr, to a C string // 2*size+1 long, conytaining a character representation of the // hexadecimail numbers at the address. const int n = size / sizeof(Double_t); Double_t * arr = (Double_t*) addr; for(Int_t i=0; i< n; i++) arr[i]=BrDb::Host2net(arr[i]); return BrDb::Addr2String((Byte_t*) arr, size); } //____________________________________________________________________ Byte_t* BrDb::String2Byte(const Char_t* str, const Int_t length) { // Converts a character representation of Hexadecimal numbers in // str, length long, to an array of Byte_t's, (length+1)/2 long. Int_t len = int(length+1) / 2; Byte_t *byte = new Byte_t[len]; for (Int_t i = 0; i < len; i++) { Int_t j; Int_t k; for (j = 0; j < 16; j++) { if (str[2 * i] == BrDb::kHexNumbers[j]) { break; } } for (k = 0; k < 16; k++) { if (str[2 * i + 1] == BrDb::kHexNumbers[k]) { break; } } byte[i] = j * 16 + k; } return byte; } //____________________________________________________________________ Int_t* BrDb::String2Int(const Char_t* str, const Int_t length) { // All of these should go in two steps // a) String2Byte // b) convert 'bytes into proper type using the net2host routines; // Int_t * arr = (Int_t*) BrDb::String2Byte(str, length); Int_t len =(length+1)/2/sizeof(Int_t); for (Int_t i = 0; i < len; i++) { arr[i]= net2host(arr[i]); } return arr; } //____________________________________________________________________ Long_t* BrDb::String2Long(const Char_t* str, const Int_t length) { // All of these should go in two steps // a) String2Byte // b) convert 'bytes into proper type using the net2host routines; // Long_t * arr = (Long_t*) BrDb::String2Byte(str, length); Int_t len =(length+1)/2/sizeof(Long_t); for (Int_t i = 0; i < len; i++) { arr[i]= net2host(arr[i]); } return arr; } //____________________________________________________________________ Short_t* BrDb::String2Short(const Char_t* str, const Int_t length) { // All of these should go in two steps // a) String2Byte // b) convert 'bytes into proper type using the net2host routines; // Short_t * arr = (Short_t*) BrDb::String2Byte(str, length); Int_t len =(length+1)/2/sizeof(Short_t); for (Int_t i = 0; i < len; i++) { arr[i]= net2host(arr[i]); } return arr; } //____________________________________________________________________ Float_t* BrDb::String2Float(const Char_t* str, const Int_t length) { // All of these should go in two steps // a) String2Byte // b) convert 'bytes into proper type using the net2host routines; // Float_t * arr = (Float_t*) BrDb::String2Byte(str, length); Int_t len = (length+1)/2/ sizeof(Float_t); for (Int_t i = 0; i < len; i++) { arr[i]= BrDb::Net2host(arr[i]); } return arr; } //____________________________________________________________________ Double_t* BrDb::String2Double(const Char_t* str, const Int_t length) { // All of these should go in two steps // a) String2Byte // b) convert 'bytes into proper type using the net2host routines; // Double_t * arr = (Double_t*) BrDb::String2Byte(str, length); Int_t len =(length+1)/2/sizeof(Double_t); for (Int_t i = 0; i < len; i++) { arr[i]= BrDb::Net2host(arr[i]); } return arr; } //____________________________________________________________________ Bool_t* BrDb::String2Bool(const Char_t* str, const Int_t length) { // Converts a character representation of Hexadecimal numbers in // str, length long, to an array of Bool_t's, (length-1)/2 long. return (Bool_t*)String2Byte(str,length); } //____________________________________________________________________ TString* BrDb::SplitList(const Char_t* commalist, const Char_t* extra) { // Split a list of comma seperated names in a string. TString* all = new TString; TString* tail = new TString(commalist); TString* head = new TString; TRegexp* regexp = new TRegexp(",[ t]*", kFALSE); while(kTRUE) { Int_t j = tail->Index(*regexp,0); if ( j >= 0 ) { *head = tail->operator()(0,j); *head = head->Strip(TString::kBoth,' '); *head = head->Strip(TString::kBoth,'t'); *all += *head; *all += " "; *all += extra; *tail = tail->operator()(j+1,tail->Length()); *tail = tail->Strip(TString::kBoth,' '); *tail = tail->Strip(TString::kBoth,'t'); *all += (tail->Length() > 0) ? ", " : ""; } else { *all += *tail; *all += " "; *all += extra; break; } } delete regexp; delete head; delete tail; return all; } //____________________________________________________________________ void BrDb::Print(Option_t* option) const { // Print infortmation on this database cout << this->ClassName() << ": " << endl << "--------------------------------------------------" << endl; TString opt(option); opt.ToLower(); if (opt.Contains("d")) { cout << " Connected: " << (IsConnected() ? "yes" : "no") << endl; if (!fImplementation) return; cout << " Type: " << fImplementation->GetRdbmType() << endl << " Host: " << fImplementation->GetHostName() << endl << " Name: " << fImplementation->GetDbName() << endl << " User: " << fImplementation->GetUserName() << endl; } } //____________________________________________________________________ TObject* BrDb::GetConnection() { if (!fImplementation) return 0; return fImplementation->GetConnection(); } //____________________________________________________________________ const Char_t* BrDb::GetDbName() const { // Forward to implementation if it exists if (!fImplementation) return 0; return fImplementation->GetTitle(); } //____________________________________________________________________ const Char_t* BrDb::GetUserName() const { // Forward to implementation if it exists if (!fImplementation) return 0; return fImplementation->GetUserName(); } //____________________________________________________________________ const Char_t* BrDb::GetHostName() const { // Forward to implementation if it exists if (!fImplementation) return 0; return fImplementation->GetHostName(); } //____________________________________________________________________ const Char_t* BrDb::GetRdbmType() const { // Forward to implementation if it exists if (!fImplementation) return 0; return fImplementation->GetRdbmType(); } //____________________________________________________________________ void BrDb::SetDbName(const Char_t* dbName) { // Forward to implementation if it exists if (!fImplementation) return; fImplementation->SetTitle(dbName); } //____________________________________________________________________ void BrDb::SetUserName(const Char_t* name) { // Forward to implementation if it exists if (!fImplementation) return; fImplementation->SetUserName(name); } //____________________________________________________________________ void BrDb::SetHostName(const Char_t* host) { // Forward to implementation if it exists if (!fImplementation) return; fImplementation->SetHostName(host); } //____________________________________________________________________ void BrDb::SetRdbmType(const Char_t* type) { // Forward to implementation if it exists if (!fImplementation) return; fImplementation->SetRdbmType(type); } //____________________________________________________________________ Bool_t BrDb::Connect(Option_t* option="") { // Forward to implementation if it exists if (!fImplementation) return kFALSE; if (fDebugLevel > 4) cout << "BrDb::Connect: trying to establish a connection to " << GetDbName() << "@" << GetHostName() << "..." << endl; return fImplementation->Connect(option); } //____________________________________________________________________ void BrDb::Close(Option_t* option="") { // Forward to implementation if it exists if (!fImplementation) return; if (fDebugLevel > 4) cout << "BrDb::Close: closing connection to " << GetDbName() << "@" << GetHostName() << "..." << endl; fImplementation->Close(option); } //____________________________________________________________________ TSQLResult* BrDb::Query(BrDbQuery* query) { // Forward to implementation if it exists if (!IsConnected()) return 0; if (fDebugLevel > 4) cout << "BrDb::Query: Sending the query '" << query << "'" << endl; return fImplementation->Query(query); } //____________________________________________________________________ TSQLRow* BrDb::GetSingle(const Char_t* table, const Char_t* condition) { // Forward to implementation if it exists if (!IsConnected()) return 0; return fImplementation->GetSingle(table,condition); } //____________________________________________________________________ TSQLResult* BrDb::GetMultiple(const Char_t* table, const Char_t* condition) { // Forward to implementation if it exists if (!IsConnected()) return 0; return fImplementation->GetMultiple(table,condition); } //____________________________________________________________________ Bool_t BrDb::IsConnected() const { // Forward to implementation if it exists if (!fImplementation) return kFALSE; return fImplementation->IsConnected(); } //____________________________________________________________________ void BrDb::LockTables(const Char_t* writetables,int i=0) { // Forward to implementation if it exists if (!IsConnected()) return; if (fDebugLevel > 4) cout << "BrDb::LockTables: Locking tables " << writetables << endl; fImplementation->LockTables(writetables,i); } //____________________________________________________________________ void BrDb::UnLockTables() { // Forward to implementation if it exists if (!IsConnected()) return; if (fDebugLevel > 4) cout << "BrDb::UnLockTables: freeing tables " << endl; fImplementation->UnLockTables(); } //____________________________________________________________________ Int_t BrDb::Increment() { // Forward to implementation if it exists if (!IsConnected()) return -1; return fImplementation->Increment(); } //____________________________________________________________________ void BrDb::CreateSequence() { // Forward to implementation if it exists if (!IsConnected()) return; fImplementation->CreateSequence(); } //____________________________________________________________________ ClassImp(BrVirtualDb); //____________________________________________________________________ BrVirtualDb::BrVirtualDb(const Char_t* name, const Char_t* title) : TNamed(name, title), fIsLocked(kFALSE) {} //____________________________________________________________________ BrVirtualDb::~BrVirtualDb(void) { // Empty DTOR } // EOF |
||||||
This page automatically generated by script docBrat by Christian Holm |
Copyright ; 2002 BRAHMS Collaboration
<brahmlib@rcf.rhic.bnl.gov>
|