|
// BrRdbmCalibrationsDb // // Database engine wrapper for calibration data. This is a singleton // object. After construction, one can get a pointer to the connection // via static message BrCalibrationsDb::Instance(). // // This is a realisation for connectivity to a Relation Database // Manager (RDBM) like MySQL or PostGreSQL // // // $Id: BrRdbmCalibrationsDb.cxx,v 1.4 2002/04/16 15:00:53 ouerdane Exp $ // $Author: ouerdane $ // $Date: 2002/04/16 15:00:53 $ // $Copyright: 2000 Brahms Collaboration // #include "BrRdbmCalibrationsDb.h" #ifndef BRAT_BrException #include "BrException.h" #endif #ifndef WIN32 #include <cstdlib> #include <iostream> #else #include <stdlib.h> #include <iostream.h> #endif //____________________________________________________________________ ClassImp(BrRdbmCalibrationsDb); //____________________________________________________________________ BrRdbmCalibrationsDb::BrRdbmCalibrationsDb(const Char_t* name, const Char_t* title) { // Normal constructor. Singleton. fImplementation = new BrRdbmDb(name, title); } //____________________________________________________________________ BrCalibrationsDb* BrRdbmCalibrationsDb::Instance(void) { // Returns an instance of the Calibration Database connection. // Connection _must_ be constructed somewhere previous to calling // this static method. if (!fgInstance) fgInstance = new BrRdbmCalibrationsDb("",""); return fgInstance; } //____________________________________________________________________ void BrRdbmCalibrationsDb::AddRevision(BrDbRevision* revision) { // Add a Revision to the database. Warns if there exist overlapping // revsisions wiith the same type. if (revision->GetParameterID() <= 0) throw new BrError("BrRdbmCalibrationsDb::AddRevision", "Parameter ID '%d' not valid, giving up", revision->GetParameterID()); if (revision->GetTypeID() < -1) throw new BrError("BrRdbmCalibrationsDb::AddRevision", "Revision Type ID unknown, giving up"); if (!revision->GetArray() || revision->GetEntries() < 1) throw new BrError("BrRdbmCalibrationsDb::AddRevision", "Can not add empty revision"); if (strlen(revision->GetComment()) < 16) cerr << "BrRdbmCalibrationsDb::AddRevision: " "Comment string lesss then 16 characters - investigate!" << endl; BrDbQuery* query = BrDbQuery::Select(BrDbRevision::kTableName,0, Form("parameterId = %d AND validEnd > %d " "AND validStart < %d", revision->GetParameterID(), revision->GetValidStart(), revision->GetValidEnd())); TSQLResult* res = Query(query); delete query; if (res->GetRowCount() > 0) { TObjArray* ar = BrDbRevision::MultipleInstance(res); TIter next(ar); BrDbRevision* r = 0; Int_t overlap = 0; while((r = (BrDbRevision*)next())) { if (r->GetTypeID() == revision->GetTypeID()) { overlap++; delete r; } } if (overlap > 0) cerr << "BrRdbmCalibrationsDb::AddRevision: " << "There exists overlapping Revisions in the data base " << "with the same type ID (" << revision->GetTypeID() << ") Beware: you have overwritten an existing revision.n " << "Don't panic, the old revision is NOT removed but the default n" << "one is now the one you have just committed." << endl; delete ar; } delete res; LockTables(BrDbRevision::kTableName); revision->SetDBID(Increment()); Query(revision->Insert()); UnLockTables(); } //____________________________________________________________________ void BrRdbmCalibrationsDb::AddRevisionType(BrDbRevisionType* type) { // Add a Revision Type to the database. Fails if it allready exists // in the database (throws a BrExcpetion) if (strlen(type->GetName()) < 1) throw new BrWarning("BrRdbmCalibrationsDb::AddRevisionType", "Revision Type MUST have a name"); if (strlen(type->GetComment()) < 16) throw new BrWarning("BrRdbmCalibrationsDb::AddRevisionType", "Revision Type MUST have a comment"); BrDbQuery* query = BrDbQuery::Select(BrDbRevisionType::kTableName,0, Form("name LIKE '%s'",type->GetName())); TSQLResult* res = Query(query); delete query; if (res->GetRowCount() > 0) throw new BrWarning("BrRdbmCalibrationsDb::AddRevisionType", "Revision Type %s already exist", type->GetName()); delete res; LockTables(BrDbRevisionType::kTableName); type->SetDBID(Increment()); Query(type->Insert()); UnLockTables(); } //____________________________________________________________________ void BrRdbmCalibrationsDb::AddParameter(BrDbParameter* param) { // Add a parameter to the database. Fails if it allready exists in // the database (throws a BrExcpetion) if (strlen(param->GetName()) < 1) throw new BrWarning("BrRdbmCalibrationsDb::AddParameter", "Parameter MUST have a name"); if (param->GetReferenceID() < 1) throw new BrWarning("BrRdbmCalibrationsDb::AddParameter", "Parameter MUST refer to something"); if (strlen(param->GetTypeName()) < 1) throw new BrWarning("BrRdbmCalibrationsDb::AddParameter", "Parameter MUST have a type"); BrDbQuery* query = BrDbQuery::Select(BrDbParameter::kTableName,0, Form("name='%s' AND referenceId = %d", param->GetName(), param->GetReferenceID())); TSQLResult* res = Query(query); delete query; if (res->GetRowCount() > 0) { throw new BrWarning("BrRdbmCalibrationsDb::AddParameter", "Parameter %s already exist", param->GetName()); } delete res; LockTables(BrDbParameter::kTableName); param->SetDBID(Increment()); Query(param->Insert()); UnLockTables(); } //____________________________________________________________________ TObjArray* BrRdbmCalibrationsDb::GetXParameter(const Char_t* condition) { // Find Parameters that matches condition <condition> return BrDbParameter::MultipleInstance(GetMultiple(BrDbParameter::kTableName, condition)); } //____________________________________________________________________ TObjArray* BrRdbmCalibrationsDb::GetXRevision(const Char_t* condition) { // Find Revisions that matches condition <condition> return BrDbRevision::MultipleInstance(GetMultiple(BrDbRevision::kTableName, condition)); } //____________________________________________________________________ TObjArray* BrRdbmCalibrationsDb::GetXRevisionType(const Char_t* condition) { // Find Revision Types that matches condition <condition> return BrDbRevisionType::MultipleInstance(GetMultiple(BrDbRevisionType::kTableName, condition)); } //____________________________________________________________________ BrDbParameter* BrRdbmCalibrationsDb::GetParameter(const Char_t* condition) { // Find a parameter that matches condition <condition> return BrDbParameter::SingleInstance(GetSingle(BrDbParameter::kTableName, condition)); } //____________________________________________________________________ BrDbParameter* BrRdbmCalibrationsDb::GetParameter(const Char_t* name, Int_t detectorId) { // Find a parameter with name <name> belonging to detector // <detectorId> return GetParameter(Form("name LIKE '%s' AND referenceId = %d", name, detectorId)); } //____________________________________________________________________ BrDbRevision* BrRdbmCalibrationsDb::GetRevision(const Char_t* condition) { // Find a single Revision macthing condition. return BrDbRevision::SingleInstance(GetSingle(BrDbRevision::kTableName, condition)); } //____________________________________________________________________ BrDbRevision* BrRdbmCalibrationsDb::GetRevision(Int_t parameterId, Int_t start, Int_t stop, Int_t typeId, Int_t parameterPolicy) { // Find a single Revision in the database belonging to Parameter // parameterId, inside the time period from start (inclusive) to // stop (exclusive), and having type typeId. stop may be negative, // in which case the database is serached for time preiod containg // start. typeId, may be less then or equal to -1, in which case // only standard Revision Types are searched. If typeId is zero, // any revision will be searched. Default is to only search standard // revisions. TString query(Form("parameterId = %d AND validStart <= %d", parameterId, start)); if (stop < 0) query += Form(" AND validEnd >= %d", start); else query += Form(" AND validEnd >= %d", stop); if (typeId <= -1) query += " AND typeId = -1"; else if (typeId > 0) query += Form(" AND typeId = %d", typeId); query += " ORDER BY date DESC"; TObjArray* ar = GetXRevision(query.Data()); Int_t entries = ar->GetEntries(); if (entries >= 1) // In case we got one or more Revision back, we return that one // the first one, which is the newest because of ordering in // Query. return (BrDbRevision*)ar->At(0); delete ar; if (entries < 1) { // If we _didn't_ get _any_ back, then we should follow the policy // passed: // 0 give up // <0 find a previous valid calibration // >0 find a future valid calibration if (parameterPolicy == 0) throw new BrError("BrRdbmCalibrationsDb::GetRevision", "No Revisions found in [%d,%d], " "giving up because policy is 0", start, stop); else { TString query2(Form("parameterId = %d",parameterId)); if (parameterPolicy > 0) query2 += Form(" AND validStart > %d ORDER BY validStart ASC", stop); if (parameterPolicy < 0) query2 += Form(" AND validEnd < %d ORDER BY validEnd DESC", start); query2 += ", date DESC"; ar = GetXRevision(query2.Data()); entries = ar->GetEntries(); if (entries < 1) { cerr << "Failed Query : n " << query2.Data() << endl; if (parameterPolicy > 0) throw new BrError("BrRdbmCalibrationsDb::GetRevision", "No Revisions found after %d, giving up", stop); if (parameterPolicy < 0) throw new BrError("BrRdbmCalibrationsDb::GetRevision", "No Revisions found before %d, giving up", start); else throw new BrFatal("BrRdbmCalibrationsDb::GetRevision", "Something is _very_ wrong"); } // In case we got one or more Revision back, we return that one // the first one, which is the newest because of ordering in // Query. return (BrDbRevision*)ar->At(0); } } return 0; } //____________________________________________________________________ BrDbRevisionType* BrRdbmCalibrationsDb::GetRevisionType(const Char_t* condition) { // Find a single Revision Type macthing <condition>. return BrDbRevisionType::SingleInstance(GetSingle(BrDbRevisionType::kTableName, condition)); } //____________________________________________________________________ BrDbRevisionType* BrRdbmCalibrationsDb::GetRevisionType(const Char_t* name, const Char_t* inComment) { // Query the database for a single Revision Type. The first argument // is the name of the revision (mandetory), and the second is a // string to search for in the comment field. The second arguement // may by "", NULL, or 0, in which case no query is perfomed on the // comment field. If a second argument is specified, the first // argument is a substring to search for. if (inComment && strlen(inComment) > 0) return GetRevisionType(Form("name LIKE '%%%s%%' AND " "comment LIKE '%%%s%%'", name, inComment)); else return GetRevisionType(Form("name LIKE '%s'", name)); } //____________________________________________________________________ // // EOF // |
||||||
This page automatically generated by script docBrat by Christian Holm |
Copyright ; 2002 BRAHMS Collaboration
<brahmlib@rcf.rhic.bnl.gov>
|