CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/OnlineDB/EcalCondDB/src/FEConfigTimingInfo.cc

Go to the documentation of this file.
00001 #include <stdexcept>
00002 #include <string>
00003 #include <string.h>
00004 #include "OnlineDB/Oracle/interface/Oracle.h"
00005 #include <cstdlib>
00006 #include "OnlineDB/EcalCondDB/interface/FEConfigTimingInfo.h"
00007 #include "OnlineDB/EcalCondDB/interface/Tm.h"
00008 #include "OnlineDB/EcalCondDB/interface/DateHandler.h"
00009 
00010 using namespace std;
00011 using namespace oracle::occi;
00012 
00013 FEConfigTimingInfo::FEConfigTimingInfo()
00014 {
00015   m_env = NULL;
00016   m_conn = NULL;
00017   m_writeStmt = NULL;
00018   m_readStmt = NULL;
00019   m_config_tag="";
00020   m_version=0;
00021   m_ID=0;
00022   clear();   
00023 }
00024 
00025 
00026 void FEConfigTimingInfo::clear(){
00027 }
00028 
00029 
00030 
00031 FEConfigTimingInfo::~FEConfigTimingInfo()
00032 {
00033 }
00034 
00035 
00036 
00037 int FEConfigTimingInfo::fetchNextId()  throw(std::runtime_error) {
00038 
00039   int result=0;
00040   try {
00041     this->checkConnection();
00042 
00043     m_readStmt = m_conn->createStatement(); 
00044     m_readStmt->setSQL("select FE_CONFIG_TIM_SQ.NextVal from DUAL ");
00045     ResultSet* rset = m_readStmt->executeQuery();
00046     while (rset->next ()){
00047       result= rset->getInt(1);
00048     }
00049     result++;
00050     m_conn->terminateStatement(m_readStmt);
00051     return result; 
00052 
00053   } catch (SQLException &e) {
00054     throw(std::runtime_error("FEConfigTimingInfo::fetchNextId():  "+e.getMessage()));
00055   }
00056 
00057 }
00058 
00059 void FEConfigTimingInfo::prepareWrite()
00060   throw(std::runtime_error)
00061 {
00062   this->checkConnection();
00063 
00064   int next_id=0;
00065   if(getId()==0){
00066     next_id=fetchNextId();
00067   }
00068 
00069   try {
00070     m_writeStmt = m_conn->createStatement();
00071     m_writeStmt->setSQL("INSERT INTO "+getTable()+" ( tim_conf_id, tag, version) " 
00072                         " VALUES ( :1, :2, :3 ) " );
00073 
00074     m_writeStmt->setInt(1, next_id);
00075     m_ID=next_id;
00076 
00077   } catch (SQLException &e) {
00078     throw(std::runtime_error("FEConfigTimingInfo::prepareWrite():  "+e.getMessage()));
00079   }
00080 
00081 }
00082 
00083 void FEConfigTimingInfo::setParameters(std::map<string,string> my_keys_map){
00084   
00085   // parses the result of the XML parser that is a map of 
00086   // string string with variable name variable value 
00087   
00088   for( std::map<std::string, std::string >::iterator ci=
00089          my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
00090     
00091     if(ci->first==  "VERSION") setVersion(atoi(ci->second.c_str()) );
00092     if(ci->first==  "TAG") setConfigTag(ci->second);
00093     
00094   }
00095   
00096 }
00097 
00098 void FEConfigTimingInfo::writeDB()
00099   throw(std::runtime_error)
00100 {
00101   this->checkConnection();
00102   this->checkPrepare();
00103 
00104   try {
00105 
00106     // number 1 is the id 
00107     m_writeStmt->setString(2, this->getConfigTag());
00108     m_writeStmt->setInt(3, this->getVersion());
00109 
00110     m_writeStmt->executeUpdate();
00111 
00112 
00113   } catch (SQLException &e) {
00114     throw(std::runtime_error("FEConfigTimingInfo::writeDB():  "+e.getMessage()));
00115   }
00116   // Now get the ID
00117   if (!this->fetchID()) {
00118     throw(std::runtime_error("FEConfigTimingInfo::writeDB:  Failed to write"));
00119   }
00120 
00121 
00122 }
00123 
00124 
00125 void FEConfigTimingInfo::fetchData(FEConfigTimingInfo * result)
00126   throw(std::runtime_error)
00127 {
00128   this->checkConnection();
00129   result->clear();
00130   if(result->getId()==0 && (result->getConfigTag()=="") ){
00131     throw(std::runtime_error("FEConfigTimingInfo::fetchData(): no Id defined for this FEConfigTimingInfo "));
00132   }
00133 
00134   try {
00135     DateHandler dh(m_env, m_conn);
00136 
00137     m_readStmt->setSQL("SELECT * FROM " + getTable() +   
00138                        " where ( tim_conf_id= :1 or (tag=:2 AND version=:3 ) )" );
00139     m_readStmt->setInt(1, result->getId());
00140     m_readStmt->setString(2, result->getConfigTag());
00141     m_readStmt->setInt(3, result->getVersion());
00142     ResultSet* rset = m_readStmt->executeQuery();
00143 
00144     rset->next();
00145 
00146     // 1 is the id and 2 is the config tag and 3 is the version
00147 
00148     result->setId(rset->getInt(1));
00149     result->setConfigTag(rset->getString(2));
00150     result->setVersion(rset->getInt(3));
00151     Date dbdate = rset->getDate(4);
00152     result->setDBTime( dh.dateToTm( dbdate ));
00153 
00154   } catch (SQLException &e) {
00155     throw(std::runtime_error("FEConfigTimingInfo::fetchData():  "+e.getMessage()));
00156   }
00157 }
00158 
00159 void FEConfigTimingInfo::fetchLastData(FEConfigTimingInfo * result)
00160   throw(std::runtime_error)
00161 {
00162   this->checkConnection();
00163   result->clear();
00164   try {
00165     DateHandler dh(m_env, m_conn);
00166 
00167     m_readStmt->setSQL("SELECT * FROM " + getTable() +   
00168                        " where   tim_conf_id = ( select max( tim_conf_id) from "+ getTable() +" ) " );
00169     ResultSet* rset = m_readStmt->executeQuery();
00170 
00171     rset->next();
00172 
00173     result->setId(rset->getInt(1));
00174     result->setConfigTag(rset->getString(2));
00175     result->setVersion(rset->getInt(3));
00176     Date dbdate = rset->getDate(4);
00177     result->setDBTime( dh.dateToTm( dbdate ));
00178 
00179   } catch (SQLException &e) {
00180     throw(std::runtime_error("FEConfigTimingInfo::fetchData():  "+e.getMessage()));
00181   }
00182 }
00183 
00184 int FEConfigTimingInfo::fetchID()    throw(std::runtime_error)
00185 {
00186   // Return from memory if available
00187   if (m_ID!=0) {
00188     return m_ID;
00189   }
00190 
00191   this->checkConnection();
00192 
00193   try {
00194     Statement* stmt = m_conn->createStatement();
00195     stmt->setSQL("SELECT tim_conf_id FROM "+ getTable()+
00196                  " WHERE  tag=:1 and version=:2 " );
00197 
00198     stmt->setString(1, getConfigTag() );
00199     stmt->setInt(2, getVersion() );
00200 
00201     ResultSet* rset = stmt->executeQuery();
00202 
00203     if (rset->next()) {
00204       m_ID = rset->getInt(1);
00205     } else {
00206       m_ID = 0;
00207     }
00208     m_conn->terminateStatement(stmt);
00209   } catch (SQLException &e) {
00210     throw(std::runtime_error("FEConfigTimingInfo::fetchID:  "+e.getMessage()));
00211   }
00212 
00213   return m_ID;
00214 }
00215 
00216 
00217 
00218 void FEConfigTimingInfo::setByID(int id) 
00219   throw(std::runtime_error)
00220 {
00221    this->checkConnection();
00222 
00223    DateHandler dh(m_env, m_conn);
00224 
00225    try {
00226      Statement* stmt = m_conn->createStatement();
00227 
00228      stmt->setSQL("SELECT * FROM "+ getTable()+" WHERE tim_conf_id = :1");
00229      stmt->setInt(1, id);
00230      
00231      ResultSet* rset = stmt->executeQuery();
00232      if (rset->next()) {
00233        this->setId(rset->getInt(1));
00234        this->setConfigTag(rset->getString(2));
00235        this->setVersion(rset->getInt(3));
00236        Date dbdate = rset->getDate(4);
00237        this->setDBTime( dh.dateToTm( dbdate ));
00238      } else {
00239        throw(std::runtime_error("FEConfigTimingInfo::setByID:  Given tim_conf_id is not in the database"));
00240      }
00241      
00242      m_conn->terminateStatement(stmt);
00243    } catch (SQLException &e) {
00244      throw(std::runtime_error("FEConfigTimingInfo::setByID:  "+e.getMessage()));
00245    }
00246 }
00247 
00248 
00249