#include "FairDbTutPar.h" #include "FairDbLogFormat.h" #include "FairDbLogService.h" #include "FairDbOutTableBuffer.h" // for FairDbOutRowStream #include "FairDbStatement.h" // for FairDbStatement #include "FairParamList.h" // for FairParamList #include "Riosfwd.h" // for ostream #include "TString.h" // for TString #include // for exit #include // for auto_ptr, etc #include // for vector, vector<>::iterator using namespace std; ClassImp(FairDbTutPar); #include "FairDbReader.tpl" template class FairDbReader; #include "FairDbWriter.tpl" template class FairDbWriter; FairDbTutPar::FairDbTutPar(const char* name, const char* title, const char* context, Bool_t own) : FairParGenericSet(name,title,context, own), fTopPitch(0.), fTopAnchor(0.), fTopNrFE(0), fFeType(""), fDbEntry(0), // Set the default Db Entry to the first slot fParam_Writer(NULL), // Writer Meta-Class for SQL IO fParam_Reader(NULL), // Reader Meta-Class for SQL IO fMultConn(FairDbTableInterfaceStore::Instance().fConnectionPool) { } FairDbTutPar::~FairDbTutPar() { if (fParam_Writer) { delete fParam_Writer; fParam_Writer=NULL; } if (fParam_Reader) { delete fParam_Reader; fParam_Reader=NULL; } } void FairDbTutPar::putParams(FairParamList* list) { std::cout<<"-I- FairDbTutPar::putParams() called"<add("top_pitch", fTopPitch); list->add("top_anchor", fTopAnchor); list->add("nr_fe_top", fTopNrFE); list->add("fe_Type", (Text_t*) &fFeType); } Bool_t FairDbTutPar::getParams(FairParamList* list) { if (!list) { return kFALSE; } if (!list->fill("top_pitch", &fTopPitch)) { return kFALSE; } if (!list->fill("top_anchor", &fTopAnchor)) { return kFALSE; } if (!list->fill("nr_fe_top", &fTopNrFE)) { return kFALSE; } Text_t feName[80]; if (!list->fill("fe_Type",feName,80)) { return kFALSE; } fFeType = feName; return kTRUE; } void FairDbTutPar::clear() { fTopPitch=fTopAnchor=0.0; fTopNrFE=0; fFeType=""; if (fParam_Writer) { fParam_Writer->Reset(); } if (fParam_Reader) { fParam_Reader->Reset(); } } string FairDbTutPar::GetTableDefinition(const char* Name) { string sql("create table "); if ( Name ) { sql += Name; } else { sql += "FAIRDBTUTPAR"; } sql += "( SEQNO INT NOT NULL,"; sql += " ROW_ID INT NOT NULL,"; sql += " TOPPITCH DOUBLE,"; sql += " TOPANCHOR DOUBLE,"; sql += " TOPNRFE INT,"; sql += " FETYPE TEXT,"; sql += " primary key(SEQNO,ROW_ID))"; return sql; } void FairDbTutPar::Fill(FairDbResultPool& res_in, const FairDbValRecord* valrec) { res_in >> fTopPitch >> fTopAnchor >> fTopNrFE >> fFeType; } void FairDbTutPar::Store(FairDbOutTableBuffer& res_out, const FairDbValRecord* valrec) const { res_out << fTopPitch << fTopAnchor << fTopNrFE << fFeType; } void FairDbTutPar::fill(UInt_t rid) { // Get Reader Meta Class fParam_Reader=GetParamReader(); // Define a Context ValTimeStamp ts(rid); ValCondition context(Detector::kGfi,DataType::kData,ts); // Activate reading for this Context fParam_Reader->Activate(context, GetVersion()); // By default use the latest row entry // (Other rows would correspond to outdated versions) Int_t numRows = fParam_Reader->GetNumRows(); if ( numRows > 1 ) { numRows = 1; } for (int i = 0; i < numRows; ++i) { FairDbTutPar* cgd = (FairDbTutPar*) fParam_Reader->GetRow(i); if (!cgd) { continue; } fTopPitch = cgd->GetTopPitch(); fTopAnchor = cgd->GetTopAnchor(); fTopNrFE = cgd->GetNrTopFE(); fFeType = cgd->GetFeType(); } } void FairDbTutPar::store(UInt_t rid) { // Boolean IO test variable Bool_t fail= kFALSE; // Create a unique statement on choosen DB entry auto_ptr stmtDbn(fMultConn->CreateStatement(GetDbEntry())); if ( ! stmtDbn.get() ) { cout << "-E- FairDbTutPar::Store() Cannot create statement for Database_id: " << GetDbEntry() << "\n Please check the FAIRDB_TSQL_* environment. Quitting ... " << endl; exit(1); } // Check if for this DB entry the table already exists. // If not call the corresponding Table Definition Function std::vector sql_cmds; TString atr(GetName()); atr.ToUpper(); if (! fMultConn->GetConnection(GetDbEntry())->TableExists("FAIRDBTUTPAR") ) { sql_cmds.push_back(FairDb::GetValDefinition("FAIRDBTUTPAR").Data()); sql_cmds.push_back(FairDbTutPar::GetTableDefinition()); } // Packed SQL commands executed internally via SQL processor std::vector::iterator itr(sql_cmds.begin()), itrEnd(sql_cmds.end()); while( itr != itrEnd ) { std::string& sql_cmd(*itr++); stmtDbn->Commit(sql_cmd.c_str()); if ( stmtDbn->PrintExceptions() ) { fail = true; cout << "-E- FairDbTutPar::Store() ******* Error Executing SQL commands *********** " << endl; } } // Refresh list of tables in connected database // for the choosen DB entry fMultConn->GetConnection(GetDbEntry())->SetTableExists(); // Writer Meta-Class Instance fParam_Writer = GetParamWriter(); // Activate Writer Meta-Class with the proper // Validity Time Interval (run_id encapsulated) // Writer Activate() arguments list // // // Validity Interval, ValInterval // Composition , Int_t set via cont. factory // Version , Int_t set via cont. factory // DbEntry , Int_t set via cont. factory // LogTitle , std::string set via cont. factory fParam_Writer->Activate(GetValInterval(rid),GetComboNo(), GetVersion(),GetDbEntry(),GetLogTitle()); // Object to Table mapping *fParam_Writer<< (*this); // Check for eventual IO problems if ( !fParam_Writer->Close() ) { fail = true; cout << "-E- FairDbTutPar::Store() ******** Cannot do IO on class: " << GetName() << endl; } } void FairDbTutPar::Print() { std::cout<<" Tutorial SQL Parameters:"<