|
// // Class BrRdbmMainDb // // Singleton class, that connects woth top level database // Name and host of database is set at compile time in // BrDBInc.hh, unless overriden in ctor. // // Provides methods to connect ot additional databases. // // $Author: cholm $ // $Date: 2001/10/08 10:55:31 $ // $Copyright: BRAHMS Collabration // $Id: BrRootMainDb.cxx,v 1.1 2001/10/08 10:55:31 cholm Exp $ // // 20-jun-2000 ah Substitute hostname for "localhost" // 20-jun-2000 ah BrRdbmMainDb now uses the passwd parameter when connecting #include <BrRootMainDb.h> #include <TString.h> #ifndef BRAT_BrException #include "BrException.h" #endif #ifndef ROOT_TSystem #include "TSystem.h" #endif #include <iostream.h> //____________________________________________________________________ ClassImp(BrRootMainDb); // Database manager class for BRAT //____________________________________________________________________ BrRootMainDb::BrRootMainDb(const Char_t* name, const Char_t* title) { fImplementation = new BrRootDb(name, title); fDb = 0; fPerson = 0; fSector = 0; fDetector = 0; fDetectorType = 0; fComponent = 0; fComponentType = 0; } //____________________________________________________________________ BrMainDb* BrRootMainDb::Instance(void) { if (!fgInstance) fgInstance = new BrRootMainDb("MainDb", BRAHMS_MAIN_DB_NAME); return fgInstance; } //____________________________________________________________________ Bool_t BrRootMainDb::Connect(Option_t* option) { // Overloaded here, since we need to get the gPerson. // Options: // See BrDb::Connect if (!fImplementation->Connect()) return kFALSE; TFile* connection = (TFile*)fImplementation->GetConnection(); if (!connection) return kFALSE; Bool_t retval = kTRUE; // Find the tables we need fDb = (TObjArray*)connection->Get(BrDbDb::kTableName); if (!fDb) { Error("Connect", "could not get table %s", BrDbDb::kTableName); retval = kFALSE; } fPerson = (TObjArray*)connection->Get(BrDbPerson::kTableName); if (!fPerson) { Error("Connect", "could not get table %s", BrDbPerson::kTableName); retval = kFALSE; } fSector = (TObjArray*)connection->Get(BrDbSector::kTableName); if (!fSector) { Error("Connect", "could not get table %s", BrDbSector::kTableName); retval = kFALSE; } fDetector = (TObjArray*)connection->Get(BrDbDetector::kTableName); if (!fDetector) { Error("Connect", "could not get table %s", BrDbDetector::kTableName); retval = kFALSE; } fDetectorType = (TObjArray*)connection->Get(BrDbDetectorType::kTableName); if (!fDetectorType) { Error("Connect", "could not get table %s", BrDbDetectorType::kTableName ); retval = kFALSE; } fComponent = (TObjArray*)connection->Get(BrDbComponent::kTableName); if (!fComponent) { Error("Connect", "could not get table %s", BrDbComponent::kTableName); retval = kFALSE; } fComponentType = (TObjArray*)connection->Get(BrDbComponentType::kTableName); if (!fComponentType) { Error("Connect", "could not get table %s", BrDbComponentType::kTableName ); retval = kFALSE; } // If some table was not found, close connection and exit with // kFALSE if (!retval) { Close(); return retval; } if (!gPerson) { TIter next(fPerson); BrDbPerson* p = 0; while ((p = (BrDbPerson*)next())) { TString email(p->GetEmail()); if (email.Contains(GetUserName())) break; } gPerson = p; if (!gPerson) { Error("Connect", "Unauthorized access. " "Couldn't find you in Person table. " "Make sure you're listed there."); return kFALSE; } } return connection ? kTRUE : kFALSE; } //____________________________________________________________________ void BrRootMainDb::AddDb(BrDbDb* db) { // Add a new person to the detector database. Throws a BrException // in case of error. if (!IsConnected()) return; if (fDb->FindObject(db->GetName())) { Error("AddDb", "db %s already exists", db->GetName()); return; } db->SetDBID(Increment()); fDb->Add(db); fDb->Write(BrDbDb::kTableName, TObject::kOverwrite|TObject::kSingleKey); } //____________________________________________________________________ void BrRootMainDb::AddPerson(BrDbPerson* person) { // Add a new person to the detector database. Throws a BrException // in case of error. if (!IsConnected()) return; TString lname(person->GetLastName()); TString fname(person->GetFirstNames()); TIter next(fPerson); BrDbPerson* p = 0; while ((p = (BrDbPerson*)next())) { if (lname == p->GetLastName() && fname == p->GetFirstNames()) { Error("AddPerson", "Person %s %s already exists", lname.Data(), fname.Data()); return; } } person->SetDBID(Increment()); fPerson->Add(person); fPerson->Write(BrDbPerson::kTableName, TObject::kOverwrite|TObject::kSingleKey); } //____________________________________________________________________ void BrRootMainDb::AddSector(BrDbSector* sector) { // Add a new Sector to the Sectors table. The BrDbSector object is // updated to contain the database ID number. Throws a BrException // in case of errors. if (!IsConnected()) return; if (fSector->FindObject(sector->GetName())) { Error("AddSector", "Sector %s already exists", sector->GetName()); return; } sector->SetDBID(Increment()); fSector->Add(sector); fSector->Write(BrDbSector::kTableName, TObject::kOverwrite|TObject::kSingleKey); } //____________________________________________________________________ void BrRootMainDb::AddDetector(BrDbDetector* detector) { // Add a detector to the database. Throws a BrException in case of // Error. if (!IsConnected()) return; if (strlen(detector->GetName()) < 1) throw new BrError("BrRootMainDb::AddDetector", "Detector MUST have a name"); if (detector->GetMaintainerID() < 1) throw new BrError("BrRootMainDb::AddDetector", "Detector %s MUST have a maintainer", detector->GetName()); if (detector->GetTypeID() < 1) throw new BrError("BrRootMainDb::AddDetector", "Detector %s MUST have a type", detector->GetName()); if (fDetector->FindObject(detector->GetName())) { Error("AddDetector", "Detector %s already exists", detector->GetName()); return; } detector->SetDBID(Increment()); fDetector->Add(detector); fDetector->Write(BrDbDetector::kTableName, TObject::kOverwrite|TObject::kSingleKey); } //____________________________________________________________________ void BrRootMainDb::AddDetectorType(BrDbDetectorType* detectorType) { // Add a Detector type to the database. Throws a BrException in case of // Error. if (!IsConnected()) return; if (strlen(detectorType->GetName()) < 1) throw new BrError("BrRootMainDb::AddDetectorType", "Detector Type MUST have a name"); if (fDetectorType->FindObject(detectorType->GetName())) { Error("AddDetectorType", "Detector type %s already exists", detectorType->GetName()); return; } detectorType->SetDBID(Increment()); fDetectorType->Add(detectorType); fDetectorType->Write(BrDbDetectorType::kTableName, TObject::kOverwrite|TObject::kSingleKey); } //____________________________________________________________________ void BrRootMainDb::AddComponent(BrDbComponent* component) { // Add a component to the database. Throws a BrException in case of // Error. if (!IsConnected()) return; if (strlen(component->GetName()) < 1) throw new BrError("BrRootMainDb::AddComponent", "Component MUST have a name"); if (component->GetTypeID() < 1) throw new BrError("BrRootMainDb::AddComponent", "Component %s MUST have a type", component->GetName()); if (fComponent->FindObject(component->GetName())) { Error("AddComponent", "Component %s already exists", component->GetName()); return; } component->SetDBID(Increment()); fComponent->Add(component); fComponent->Write(BrDbComponent::kTableName, TObject::kOverwrite|TObject::kSingleKey); } //____________________________________________________________________ void BrRootMainDb::AddComponentType(BrDbComponentType* componentType) { // Add a Component type to the database. Throws a BrException in case of // Error. if (IsConnected()) return; if (strlen(componentType->GetName()) < 1) throw new BrError("BrRootMainDb::AddComponentType", "Component Type MUST have a name"); if (fComponentType->FindObject(componentType->GetName())) { Error("AddComponentType", "Component type %s already exists", componentType->GetName()); return; } componentType->SetDBID(Increment()); fComponentType->Add(componentType); fComponentType->Write(BrDbComponentType::kTableName, TObject::kOverwrite|TObject::kSingleKey); } //____________________________________________________________________ BrDbDb* BrRootMainDb::GetDb(const Char_t* name) { // Returns the actual database name and host in a TNamed (name is // dbname, title is host) for the symbolic database called name. // The TNamed object must be deleted by user. if(!IsConnected()) return 0; return (BrDbDb*)fDb->FindObject(name); } //____________________________________________________________________ BrDbPerson* BrRootMainDb::GetPerson(const Char_t* condition) { // Returns 0 always (General SQL queries not implmented) return 0; } //____________________________________________________________________ BrDbSector* BrRootMainDb::GetSector(const Char_t* condition) { // Returns 0 always (General SQL queries not implmented) return 0; } //____________________________________________________________________ BrDbDetector* BrRootMainDb::GetDetector(const Char_t* condition) { // Returns 0 always (General SQL queries not implmented) return 0; } //____________________________________________________________________ BrDbDetectorType* BrRootMainDb::GetDetectorType(const Char_t* condition) { // Returns 0 always (General SQL queries not implmented) return 0; } //____________________________________________________________________ BrDbComponent* BrRootMainDb::GetComponent(const Char_t* condition) { // Returns 0 always (General SQL queries not implmented) return 0; } //____________________________________________________________________ BrDbComponentType* BrRootMainDb::GetComponentType(const Char_t* condition) { // Returns 0 always (General SQL queries not implmented) return 0; } //____________________________________________________________________ TObjArray* BrRootMainDb::GetXPerson(const Char_t* condition) { // Returns the full list of persons. return fPerson; } //____________________________________________________________________ TObjArray* BrRootMainDb::GetXSector(const Char_t* condition) { // Returns the full list of sectors. return fSector; } //____________________________________________________________________ TObjArray* BrRootMainDb::GetXDetector(const Char_t* condition) { // Returns the full list of detectors. return fDetector; } //____________________________________________________________________ TObjArray* BrRootMainDb::GetXDetectorType(const Char_t* condition) { // Returns the full list of detector types. return fDetectorType; } //____________________________________________________________________ TObjArray* BrRootMainDb::GetXComponent(const Char_t* condition) { // Returns the full list of components. return fComponent; } //____________________________________________________________________ TObjArray* BrRootMainDb::GetXComponentType(const Char_t* condition) { // Returns the full list of component types. return fComponentType; } //____________________________________________________________________ BrDbPerson* BrRootMainDb::GetPersonByName(const Char_t* lastname, const Char_t* firstnames) { // Get an entry for individual [<firstnames>] <lastname>. Note that // both arguments must be specified, thought the second my be the // empty string, NULL, or zero. if (!IsConnected()) return 0; TString fname(firstnames); TString lname(lastname); TIter next(fPerson); BrDbPerson* p = 0; while ((p = (BrDbPerson*)next())) { if (lname == p->GetLastName()) { if (!fname.IsNull() && fname != p->GetFirstNames()) continue; break; } } return p; } //____________________________________________________________________ BrDbSector* BrRootMainDb::GetSectorByName(const Char_t* name) { if (!IsConnected()) return 0; return (BrDbSector*)fSector->FindObject(name); } //____________________________________________________________________ BrDbDetector* BrRootMainDb::GetDetectorByName(const Char_t* name) { if (!IsConnected()) return 0; return (BrDbDetector*)fDetector->FindObject(name); } //____________________________________________________________________ BrDbDetectorType* BrRootMainDb::GetDetectorTypeByName(const Char_t* name) { if (!IsConnected()) return 0; return (BrDbDetectorType*)fDetectorType->FindObject(name); } //____________________________________________________________________ BrDbComponent* BrRootMainDb::GetComponentByName(const Char_t* name, Int_t detectorId) { if (!IsConnected()) return 0; BrDbComponent* c = (BrDbComponent*)fComponent->FindObject(name); if (c->GetDetectorID() != detectorId) return 0; return c; } //____________________________________________________________________ BrDbComponentType* BrRootMainDb::GetComponentTypeByName(const Char_t* name) { if (!IsConnected()) return 0; return (BrDbComponentType*)fComponentType->FindObject(name); } //____________________________________________________________________ Bool_t BrRootMainDb::PurgeDetector(const Char_t* name) { // Completly remove all references to the detector named name from // the databases. This involves: // // Removing all revisions for detectors parameters // Removing all parameters for the detector // Remove the detector entry itself // // This is a DANGEROUS method, and should not be used lightly. // Normal users do not have the privileges to do this, so they // should not worry too much about it, but privileged user can do // ereperable damage to the database by using this method. // // The method return kFALSE in case something went wrong. return kFALSE; } // EOF |
||||||
This page automatically generated by script docBrat by Christian Holm |
Copyright ; 2002 BRAHMS Collaboration
<brahmlib@rcf.rhic.bnl.gov>
|