CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/OnlineDB/EcalCondDB/src/ODFEDAQConfig.cc

Go to the documentation of this file.
00001 #include <stdexcept>
00002 #include <cstdlib>
00003 #include <string>
00004 #include <string.h>
00005 #include "OnlineDB/Oracle/interface/Oracle.h"
00006 
00007 #include "OnlineDB/EcalCondDB/interface/ODFEDAQConfig.h"
00008 
00009 using namespace std;
00010 using namespace oracle::occi;
00011 
00012 #define MY_NULL -1
00013 #define SET_INT( statement, paramNum, paramVal ) if( paramVal != MY_NULL ) { statement->setInt(paramNum, paramVal);  } else { statement->setNull(paramNum,OCCINUMBER); }
00014 #define SET_STRING( statement, paramNum, paramVal ) if( ! paramVal.empty() ) { statement->setString(paramNum, paramVal); } else { statement->setNull(paramNum,OCCICHAR); }
00015 
00016 int getInt(ResultSet * rset, int ipar )
00017 {
00018   return  rset->isNull(ipar) ? MY_NULL : rset->getInt(ipar) ;
00019 }
00020 
00021 ODFEDAQConfig::ODFEDAQConfig()
00022 {
00023   m_env = NULL;
00024   m_conn = NULL;
00025   m_writeStmt = NULL;
00026   m_readStmt = NULL;
00027   m_config_tag="";
00028    m_ID=0;
00029    clear();   
00030 }
00031 
00032 
00033 void ODFEDAQConfig::clear(){
00034    m_del=MY_NULL;
00035    m_wei=MY_NULL;
00036    m_ped=MY_NULL;
00037 
00038    m_bxt =MY_NULL;
00039    m_btt =MY_NULL;
00040    m_tbtt=MY_NULL;
00041    m_tbxt=MY_NULL;
00042 
00043    m_version=0;
00044    m_com="";
00045 }
00046 
00047 
00048 
00049 ODFEDAQConfig::~ODFEDAQConfig()
00050 {
00051 }
00052 
00053 
00054 
00055 int ODFEDAQConfig::fetchNextId()  throw(std::runtime_error) {
00056 
00057   int result=0;
00058   try {
00059     this->checkConnection();
00060 
00061     m_readStmt = m_conn->createStatement(); 
00062     m_readStmt->setSQL("select fe_daq_conDfig_sq.nextVal from dual");
00063     ResultSet* rset = m_readStmt->executeQuery();
00064     while (rset->next ()){
00065       result= rset->getInt(1);
00066     }
00067     m_conn->terminateStatement(m_readStmt);
00068     return result; 
00069 
00070   } catch (SQLException &e) {
00071     throw(std::runtime_error("ODFEDAQConfig::fetchNextId():  "+e.getMessage()));
00072   }
00073 
00074 }
00075 
00076 void ODFEDAQConfig::prepareWrite()
00077   throw(std::runtime_error)
00078 {
00079   this->checkConnection();
00080   int next_id=fetchNextId();
00081 
00082   try {
00083     m_writeStmt = m_conn->createStatement();
00084     m_writeStmt->setSQL("INSERT INTO FE_DAQ_CONFIG ( config_id, tag, version, ped_id, " 
00085                         " del_id, wei_id,bxt_id, btt_id, tr_bxt_id, tr_btt_id, user_comment ) "
00086                         "VALUES ( :1, :2, :3, :4, :5, :6, :7 ,:8, :9, :10, :11 )" );
00087 
00088     m_writeStmt->setInt(1, next_id);
00089     m_ID=next_id;
00090 
00091   } catch (SQLException &e) {
00092     throw(std::runtime_error("ODFEDAQConfig::prepareWrite():  "+e.getMessage()));
00093   }
00094 
00095 }
00096 
00097 void ODFEDAQConfig::setParameters(std::map<string,string> my_keys_map){
00098   
00099   // parses the result of the XML parser that is a map of 
00100   // string string with variable name variable value 
00101   
00102   for( std::map<std::string, std::string >::iterator ci=
00103          my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
00104     
00105     if(ci->first==  "VERSION") setVersion(atoi(ci->second.c_str()) );
00106     if(ci->first==  "PED_ID") setPedestalId(atoi(ci->second.c_str()) );
00107     if(ci->first==  "DEL_ID") setDelayId(atoi(ci->second.c_str()));
00108     if(ci->first==  "WEI_ID") setWeightId(atoi(ci->second.c_str()));
00109 
00110     if(ci->first==  "BXT_ID") setBadXtId(atoi(ci->second.c_str()));
00111     if(ci->first==  "BTT_ID") setBadTTId(atoi(ci->second.c_str()));
00112     if(ci->first==  "TRIG_BXT_ID") setTriggerBadXtId(atoi(ci->second.c_str()));
00113     if(ci->first==  "TRIG_BTT_ID") setTriggerBadTTId(atoi(ci->second.c_str()));
00114 
00115     if(ci->first==  "COMMENT" || ci->first==  "USER_COMMENT") setComment(ci->second);
00116     
00117   }
00118   
00119 }
00120 
00121 void ODFEDAQConfig::writeDB()
00122   throw(std::runtime_error)
00123 {
00124   this->checkConnection();
00125   this->checkPrepare();
00126 
00127   try {
00128 
00129     // number 1 is the id 
00130     m_writeStmt->setString(2, this->getConfigTag());
00131     m_writeStmt->setInt(3, this->getVersion());
00132     SET_INT(m_writeStmt,4, this->getPedestalId());
00133     SET_INT(m_writeStmt,5, this->getDelayId());
00134     SET_INT(m_writeStmt,6, this->getWeightId());
00135     SET_INT(m_writeStmt,7, this->getBadXtId());
00136     SET_INT(m_writeStmt,8, this->getBadTTId());
00137     SET_INT(m_writeStmt,9, this->getTriggerBadXtId());
00138     SET_INT(m_writeStmt,10,this->getTriggerBadTTId());
00139 
00140     m_writeStmt->setString(11, this->getComment());
00141 
00142     m_writeStmt->executeUpdate();
00143 
00144 
00145   } catch (SQLException &e) {
00146     throw(std::runtime_error("ODFEDAQConfig::writeDB():  "+e.getMessage()));
00147   }
00148   // Now get the ID
00149   if (!this->fetchID()) {
00150     throw(std::runtime_error("ODFEDAQConfig::writeDB:  Failed to write"));
00151   }
00152 
00153 
00154 }
00155 
00156 
00157 void ODFEDAQConfig::fetchData(ODFEDAQConfig * result)
00158   throw(std::runtime_error)
00159 {
00160   this->checkConnection();
00161   result->clear();
00162   if(result->getId()==0 && (result->getConfigTag()=="") ){
00163     throw(std::runtime_error("ODFEDAQConfig::fetchData(): no Id defined for this ODFEDAQConfig "));
00164   }
00165 
00166   m_readStmt = m_conn->createStatement(); 
00167   if(result->getConfigTag()!="" && result->getVersion() ==0  ){
00168     int new_version=0;
00169     std::cout<< "using new method : retrieving last version for this tag "<<endl;
00170     try {
00171       this->checkConnection();
00172 
00173       m_readStmt->setSQL("select max(version) from "+getTable()+" where tag=:tag " );
00174       m_readStmt->setString(1, result->getConfigTag());
00175       std::cout << "Getting last ver" << std::endl << std::flush; 
00176      ResultSet* rset = m_readStmt->executeQuery();
00177       while (rset->next ()){
00178         new_version= rset->getInt(1);
00179       }
00180       m_conn->terminateStatement(m_readStmt);
00181 
00182       //      m_readStmt = m_conn->createStatement(); 
00183       
00184       result->setVersion(new_version);
00185       
00186     } catch (SQLException &e) {
00187       throw(std::runtime_error("ODFEDAQConfig::fetchData():  "+e.getMessage()));
00188     }
00189     
00190     
00191     
00192   }
00193 
00194   try {
00195 
00196     m_readStmt->setSQL("SELECT * FROM " + getTable() +   
00197                        " where ( config_id = :1 or (tag=:2 AND version=:3 ) )" );
00198     m_readStmt->setInt(1, result->getId());
00199     m_readStmt->setString(2, result->getConfigTag());
00200     m_readStmt->setInt(3, result->getVersion());
00201     ResultSet* rset = m_readStmt->executeQuery();
00202 
00203     rset->next();
00204 
00205     // 1 is the id and 2 is the config tag and 3 is the version
00206 
00207     result->setId(rset->getInt(1));
00208     result->setConfigTag(rset->getString(2));
00209     result->setVersion(rset->getInt(3));
00210 
00211     result->setPedestalId(       getInt(rset,4) );
00212     result->setDelayId(          getInt(rset,5) );
00213     result->setWeightId(         getInt(rset,6) );
00214     result->setBadXtId(          getInt(rset,7) );
00215     result->setBadTTId(          getInt(rset,8) );
00216     result->setTriggerBadXtId(   getInt(rset,9) );
00217     result->setTriggerBadTTId(   getInt(rset,10) );
00218     result->setComment(          rset->getString(11) );
00219 
00220   } catch (SQLException &e) {
00221     throw(std::runtime_error("ODFEDAQConfig::fetchData():  "+e.getMessage()));
00222   }
00223 }
00224 
00225 int ODFEDAQConfig::fetchID()    throw(std::runtime_error)
00226 {
00227   // Return from memory if available
00228   if (m_ID!=0) {
00229     return m_ID;
00230   }
00231 
00232   this->checkConnection();
00233 
00234   try {
00235     Statement* stmt = m_conn->createStatement();
00236     stmt->setSQL("SELECT config_id FROM "+ getTable()+
00237                  "WHERE  tag=:1 and version=:2 " );
00238 
00239     stmt->setString(1, getConfigTag() );
00240     stmt->setInt(2, getVersion() );
00241 
00242     ResultSet* rset = stmt->executeQuery();
00243 
00244     if (rset->next()) {
00245       m_ID = rset->getInt(1);
00246     } else {
00247       m_ID = 0;
00248     }
00249     m_conn->terminateStatement(stmt);
00250   } catch (SQLException &e) {
00251     throw(std::runtime_error("ODFEDAQConfig::fetchID:  "+e.getMessage()));
00252   }
00253 
00254   return m_ID;
00255 }