BRAT 2.4.5
Class index
Full class index
brahmlib
BRAHMS
ROOT page
// 
// 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:18 $
// $Copyright: BRAHMS Collabration
// $Id: BrRdbmMainDb.cxx,v 1.1 2001/10/08 10:55:18 cholm Exp $
//
// 20-jun-2000  ah  Substitute hostname for "localhost"
// 20-jun-2000  ah  BrRdbmMainDb now uses the passwd parameter when connecting

#include <BrRdbmMainDb.h>
#include <TString.h>
#ifndef BRAT_BrException
#include "BrException.h"
#endif
#ifndef ROOT_TSystem
#include "TSystem.h"
#endif
#include <iostream.h>


//____________________________________________________________________
ClassImp(BrRdbmMainDb); // Database manager class for BRAT

//____________________________________________________________________
 BrRdbmMainDb::BrRdbmMainDb(const Char_t* name, const Char_t* title) 
{
  fImplementation = new BrRdbmDb(name, title);
}

//____________________________________________________________________
 BrMainDb* BrRdbmMainDb::Instance(void) 
{
  if (!fgInstance)
    fgInstance = new BrRdbmMainDb;

  return fgInstance;
}

//____________________________________________________________________
 Bool_t BrRdbmMainDb::Connect(Option_t* option) 
{
  // Overloaded here, since we need to get the gPerson. 
  // Options: 
  //   See BrDb::Connect
  if (!fImplementation->Connect()) 
    return kFALSE;
  
  if (!gPerson) {
    gPerson = GetPerson(Form(" email LIKE '%%%s%%'", GetUserName()));
    if (!gPerson) {
      Error("Connect", "Unauthorized access. "
	    "Couldn't find you in Person table. "
	    "Make sure you're listed there."); 
      return kFALSE;
    }
  }
  return kTRUE;
}

//____________________________________________________________________
 void BrRdbmMainDb::AddDb(BrDbDb* db) 
{
  // Add a new db to the db table. Throws a BrException in case of
  // error.   
  if (!IsConnected()) 
    return;

#if 0
  BrDbQuery* query = 
    BrDbQuery::Select(BrDbDb::kTableName,0,
		      Form("id = %d", db->GetDBID()));
#else 
  BrDbQuery* query = 
    BrDbQuery::Select(BrDbDb::kTableName,0,
		      Form("name = '%s'", db->GetName()));
#endif

  TSQLResult* res = Query(query);
  if(res->GetRowCount() != 0)
    throw new BrWarning("AddDb", "Db %s already exists", 
			db->GetName());
  delete res;
  
  LockTables(BrDbDb::kTableName);

  db->SetDBID(Increment());

  Query(db->Insert());
  UnLockTables();

}

//____________________________________________________________________
 void BrRdbmMainDb::AddPerson(BrDbPerson* person) 
{
  // Add a new person to the detector database. Throws a BrException
  // in case of error.  
  if (!IsConnected()) 
    return;

  BrDbQuery* query = 
    BrDbQuery::Select(BrDbPerson::kTableName,0,
		      Form("lastname = '%s' AND firstnames ='%s'",
			   person->GetLastName(), 
			   person->GetFirstNames())); 
			   
  TSQLResult* res = Query(query);
  if(res->GetRowCount() != 0)
    throw new BrWarning("AddPerson", "Person %s %s already exists", 
			person->GetLastName(), 
			person->GetFirstNames());
  delete res;
  
  LockTables(BrDbPerson::kTableName);

  person->SetDBID(Increment());

  Query(person->Insert());
  UnLockTables();

}

//____________________________________________________________________
 void BrRdbmMainDb::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;

  BrDbQuery* query = 
    BrDbQuery::Select(BrDbSector::kTableName,0,
		      Form("name LIKE '%s'",  
			  sector->GetName()));

  TSQLResult* res = Query(query);
  if (res->GetRowCount() != 0)
    throw new BrWarning("BrRdbmMainDb::AddSector", 
			"Sector %s already exists", 
			sector->GetName());

  delete res;

  LockTables(BrDbSector::kTableName);

  sector->SetDBID(Increment());
  
  Query(sector->Insert());  

  UnLockTables();  
}

  
//____________________________________________________________________
 void BrRdbmMainDb::AddDetector(BrDbDetector* det)
{
  // Add a detector to the database. Throws a BrException in case of
  // Error. 
  if (!IsConnected())
    return;

  if (strlen(det->GetName()) < 1)
    throw new BrError("BrRdbmMainDb::AddDetector",
		      "Detector MUST have a name");
  if (det->GetMaintainerID() < 1)
    throw new BrError("BrRdbmMainDb::AddDetector",
		      "Detector %s MUST have a maintainer",
		      det->GetName());
  if (det->GetTypeID() < 1)
    throw new BrError("BrRdbmMainDb::AddDetector",
		      "Detector %s MUST have a type",
		      det->GetName());

  BrDbQuery* query = BrDbQuery::Select(BrDbDetector::kTableName,0,
					Form("name LIKE '%s'",
					     det->GetName()));
  TSQLResult* res = Query(query);
  if (res->GetRowCount() != 0)
    throw new BrWarning("BrRdbmMainDb::AddDetector", 
			"Detector %s already exists", 
			det->GetName());
  delete res;

  LockTables(BrDbDetector::kTableName);

  det->SetDBID(Increment());

  Query(det->Insert());

  UnLockTables();
}

//____________________________________________________________________
 void BrRdbmMainDb::AddDetectorType(BrDbDetectorType* type)
{
  // Add a Detector type to the database. Throws a BrException in case of
  // Error. 
  if (!IsConnected())
    return;

  if (strlen(type->GetName()) < 1)
    throw new BrError("BrRdbmMainDb::AddDetectorType",
		      "Detector Type MUST have a name");
  BrDbQuery* query = BrDbQuery::Select(BrDbDetectorType::kTableName,0,
					Form("name LIKE '%s'",
					     type->GetName()));

  TSQLResult* res = Query(query);
  if (res->GetRowCount() != 0)
    throw new BrWarning("BrRdbmMainDb::AddDetectorType", 
			"Detector type %s already exists", 
			type->GetName());
  delete res;

  LockTables(BrDbDetectorType::kTableName);

  type->SetDBID(Increment());

  Query(type->Insert());

  UnLockTables();
}

//____________________________________________________________________
 void BrRdbmMainDb::AddComponent(BrDbComponent* comp)
{
  // Add a component to the database. Throws a BrException in case of
  // Error. 
  if (!IsConnected())
    return;

  if (strlen(comp->GetName()) < 1)
    throw new BrError("BrRdbmMainDb::AddComponent",
		      "Component MUST have a name");
  if (comp->GetTypeID() < 1)
    throw new BrError("BrRdbmMainDb::AddComponent",
		      "Component %s MUST have a type",
		      comp->GetName());

  BrDbQuery* query = BrDbQuery::Select(BrDbComponent::kTableName,0,
					Form("name LIKE '%s'",
					     comp->GetName()));
  TSQLResult* res = Query(query);
  if (res->GetRowCount() != 0)
    throw new BrWarning("BrRdbmMainDb::AddComponent", 
			"Component %s already exists", 
			comp->GetName());
  delete res;

  LockTables(BrDbComponent::kTableName);

  comp->SetDBID(Increment());

  Query(comp->Insert());
  
  UnLockTables();
}

//____________________________________________________________________
 void BrRdbmMainDb::AddComponentType(BrDbComponentType* type)
{
  // Add a Component type to the database. Throws a BrException in case of
  // Error. 
  if (IsConnected())
    return;

  if (strlen(type->GetName()) < 1)
    throw new BrError("BrRdbmMainDb::AddComponentType",
		      "Detector Type MUST have a name");
  BrDbQuery* query = BrDbQuery::Select(BrDbComponentType::kTableName,0,
					Form("name LIKE '%s'",
					     type->GetName()));

  TSQLResult* res = Query(query);
  if (res->GetRowCount() != 0)
    throw new BrWarning("BrRdbmMainDb::AddComponentType", 
			"Detector type %s already exists", 
			type->GetName());
  delete res;

  LockTables(BrDbComponentType::kTableName);

  type->SetDBID(Increment());

  Query(type->Insert());

  UnLockTables();
}

//____________________________________________________________________
 BrDbDb* BrRdbmMainDb::GetDb(const Char_t* name)
{
  // Returns the actual database name and host in a BrDbDb for the
  // symbolic database called name. The BrDbDb object must be deleted
  // by user.   
  if (!IsConnected())
    return 0;

  TSQLRow* row = GetSingle(BrDbDb::kTableName, 
			   Form("name LIKE '%%%s%%'", name)); 
  return BrDbDb::SingleInstance(row);
}

  

//____________________________________________________________________
 BrDbPerson* BrRdbmMainDb::GetPerson(const Char_t* condition) 
{
  // See GetSector and BrDb::GetSingle.
  if (!IsConnected())
    return 0;

  TSQLRow* row = GetSingle(BrDbPerson::kTableName, condition);
  return BrDbPerson::SingleInstance(row);
}

//____________________________________________________________________
 BrDbSector* BrRdbmMainDb::GetSector(const Char_t* condition) 
{
  // Selects a Sector from database, according to the conditions in
  // the argument e.g., "name LIKE '%%%s%%', "FMS"".  See also
  // BrDb::GetSingle. Valid fields are 
  //    name (string)
  if (!IsConnected())
    return 0;
  TSQLRow* row = GetSingle(BrDbSector::kTableName, condition);
  return BrDbSector::SingleInstance(row);
}

//____________________________________________________________________
 BrDbDetector* BrRdbmMainDb::GetDetector(const Char_t* condition) 
{
  // See GetSector and BrDb::GetSingle. Valid fields are 
  //   name (string), type (int), sector (int), maintainer (int)  
  if (!IsConnected())
    return 0;
  TSQLRow* row = GetSingle(BrDbDetector::kTableName, condition);
  return BrDbDetector::SingleInstance(row);
}

//____________________________________________________________________
 BrDbDetectorType* BrRdbmMainDb::GetDetectorType(const Char_t* condition) 
{
  // See GetSector and BrDb::GetSingle. Valid fields are 
  //    lastname, firstnames, email, institute (all strings)
  if (!IsConnected())
    return 0;
  TSQLRow* row = GetSingle(BrDbDetectorType::kTableName, condition);
  return BrDbDetectorType::SingleInstance(row);
}

//____________________________________________________________________
 BrDbComponent* BrRdbmMainDb::GetComponent(const Char_t* condition) 
{
  // See GetSector and BrDb::GetSingle. Valid fields are 
  //   name (string), type (int), sector (int), maintainer (int)  
  if (!IsConnected())
    return 0;
  TSQLRow* row = GetSingle(BrDbComponent::kTableName, condition);
  return BrDbComponent::SingleInstance(row);
}

//____________________________________________________________________
BrDbComponentType*
 BrRdbmMainDb::GetComponentType(const Char_t* condition) 
{
  // See GetSector and BrDb::GetSingle. Valid fields are 
  //    lastname, firstnames, email, institute (all strings)
  if (!IsConnected())
    return 0;
  TSQLRow* row = GetSingle(BrDbComponentType::kTableName, condition);
  return BrDbComponentType::SingleInstance(row);
}

//____________________________________________________________________
TObjArray*     
 BrRdbmMainDb::GetXPerson(const Char_t* condition) 
{
  // You need to explicitly provide the "WHERE" in the where claus. 
  // See also BrDb::GetMulitple
  if (!IsConnected())
    return 0;
  TSQLResult* res = GetMultiple(BrDbPerson::kTableName, condition);
  return BrDbPerson::MultipleInstance(res);
}

//____________________________________________________________________
TObjArray*     
 BrRdbmMainDb::GetXSector(const Char_t* condition) 
{
  // You need to explicitly provide the "WHERE" in the where claus. 
  // See also BrDb::GetMulitple
  if (!IsConnected())
    return 0;
  TSQLResult* res = GetMultiple(BrDbSector::kTableName, condition);
  return BrDbSector::MultipleInstance(res);
}

//____________________________________________________________________
TObjArray*     
 BrRdbmMainDb::GetXDetector(const Char_t* condition)
{
  // You need to explicitly provide the "WHERE" in the where claus. 
  // See also BrDb::GetMulitple
  if (!IsConnected())
    return 0;
  TSQLResult* res = GetMultiple(BrDbDetector::kTableName, condition);
  return BrDbDetector::MultipleInstance(res);
}

//____________________________________________________________________
TObjArray*     
 BrRdbmMainDb::GetXDetectorType(const Char_t* condition)
{
  // You need to explicitly provide the "WHERE" in the where claus. 
  // See also BrDb::GetMulitple
  if (!IsConnected())
    return 0;
  TSQLResult* res = GetMultiple(BrDbDetectorType::kTableName, condition);
  return BrDbDetectorType::MultipleInstance(res);
}

//____________________________________________________________________
TObjArray*     
 BrRdbmMainDb::GetXComponent(const Char_t* condition)
{
  // You need to explicitly provide the "WHERE" in the where claus. 
  // See also BrDb::GetMulitple
  if (!IsConnected())
    return 0;
  TSQLResult* res = GetMultiple(BrDbComponent::kTableName, condition);
  return BrDbComponent::MultipleInstance(res);
}

//____________________________________________________________________
TObjArray*     
 BrRdbmMainDb::GetXComponentType(const Char_t* condition)
{
  // You need to explicitly provide the "WHERE" in the where claus. 
  // See also BrDb::GetMulitple
  if (!IsConnected())
    return 0;
  TSQLResult* res = GetMultiple(BrDbComponentType::kTableName, condition);
  return BrDbComponentType::MultipleInstance(res);
}

//____________________________________________________________________
 BrDbPerson* BrRdbmMainDb::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;
  if (firstnames && strlen(firstnames) > 0)
    return GetPerson(Form("lastname LIKE '%s' AND firstNames LIKE '%s'",
			  lastname, firstnames));
  else 
    return GetPerson(Form("lastname LIKE '%s'",
			  lastname));
}
  

//____________________________________________________________________
 BrDbSector* BrRdbmMainDb::GetSectorByName(const Char_t* name) 
{
  if (!IsConnected())
    return 0;
  return GetSector(Form("name LIKE '%s'", name));
}


//____________________________________________________________________
 BrDbDetector* BrRdbmMainDb::GetDetectorByName(const Char_t* name) 
{
  if (!IsConnected())
    return 0;
  return GetDetector(Form("name LIKE '%s'", name));
}

//____________________________________________________________________
 BrDbDetectorType* BrRdbmMainDb::GetDetectorTypeByName(const Char_t* name) 
{
  if (!IsConnected())
    return 0;
  return GetDetectorType(Form("name LIKE '%s'", name));
}

//____________________________________________________________________
 BrDbComponent* BrRdbmMainDb::GetComponentByName(const Char_t* name, 
					    Int_t detectorId)  
{
  if (!IsConnected())
    return 0;
  return GetComponent(Form("name LIKE '%s' AND detectorId = %d", 
			   name, detectorId));
}

//____________________________________________________________________
 BrDbComponentType* BrRdbmMainDb::GetComponentTypeByName(const Char_t* name) 
{
  if (!IsConnected())
    return 0;
  return GetComponentType(Form("name LIKE '%s'", name));
}

//____________________________________________________________________
 Bool_t BrRdbmMainDb::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. 
  BrDbDetector* det = GetDetectorByName(name);
  if (!det) {
    Error("PurgeDetector", "Error didn't get detector %s", name);
    return kFALSE;
  }  

  // If not connected to the calibration database, try do so now
  if (!fCalibDB->IsConnected()) {  
    ConnectToCalib();
    if (!fCalibDB->IsConnected()) 
      // If the connection failed, we return 
      return kFALSE; 
  }
  
  // Some variables we need. 
  UInt_t      detId = det->GetDBID();
  TSQLResult* r     = 0;
  BrDbQuery*  query = 0;
  BrDbParameter* p  = 0;
  TObjArray*  pl    = 
    fCalibDB->GetXParameter(Form("referenceId=%d", detId));

  // Loop ever the parameters, and delete the parameters revisions. 
  TIter          next(pl);
  while ((p = (BrDbParameter*)next())) {
    //________________________________________________________________
    // 
    // Now try to remove the entries from the Revision table. 
    // A friendly message 
    if (Verbose() > 0) 
      cout << "Deleting revisions for parameter " << p->GetName() << endl;
    query = new BrDbQuery(Form("DELETE FROM Revision WHERE parameterId=%d", 
			       p->GetDBID()));
    r = fCalibDB->Query(query);
    // query object not needed anymore 
    delete query;
    // Check the returend result 
    if (!r) {
      Error("PurgeDetector", 
	    "could not delete revisions for parameter %s", 
	    p->GetName());
      // Clean up
      delete r;
      return kFALSE;
    }
    // Clean up
    delete r;
  }
  //__________________________________________________________________
  // 
  // Now try to remove the entries from the Parameter table. 
  // 
  // A fiendly message 
  if (Verbose() > 0) 
    cout << "Deleting parameters for detector " << name << endl;
  // Make the SQL query 
  query = new BrDbQuery(Form("DELETE FROM Parameter WHERE referenceId=%d", 
			     detId));
  // Do the actual thing  
  r = fCalibDB->Query(query);
  // query object not needed anymore 
  delete query;
  // Check the returend result 
  if (!r) {
    Error("PurgeDetector", 
	  "could not delete parameters for detector %s", name);
    // Clean up
    delete r;
    return kFALSE;
  }
  // Clean up
  delete r; 

  //__________________________________________________________________
  // 
  // Now try to remove the entry from the Detector table. 
  // 
  // A fiendly message 
  if (Verbose() > 0) 
    cout << "Deleting detector " << name << endl;
  // Make the SQL query 
  query = new BrDbQuery(Form("DELETE FROM Detector WHERE Id=%d", detId));
  // Do the actual thing
  r = Query(query);
  // query object not needed anymore 
  delete query;
  // Check the returend result 
  if (!r) {
    Error("PurgeDetector", 
	  "could not delete detector %s", name);
    // Clean up
    delete r;
    return kFALSE;
  }
  // Clean up
  delete r; 

  // We succeded 
  return kTRUE;
}
  
// EOF

This page automatically generated by script docBrat by Christian Holm

Copyright ; 2002 BRAHMS Collaboration <brahmlib@rcf.rhic.bnl.gov>
Last Update on by

Validate HTML
Validate CSS