CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/OnlineDB/EcalCondDB/src/ODFEDelaysInfo.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/ODFEDelaysInfo.h"
00008 
00009 using namespace std;
00010 using namespace oracle::occi;
00011 
00012 ODFEDelaysInfo::ODFEDelaysInfo()
00013 {
00014   m_env = NULL;
00015   m_conn = NULL;
00016   m_writeStmt = NULL;
00017   m_readStmt = NULL;
00018   m_config_tag="";
00019   m_version=0;
00020   m_ID=0;
00021   clear();   
00022 }
00023 
00024 
00025 void ODFEDelaysInfo::clear(){
00026 
00027 }
00028 
00029 
00030 
00031 ODFEDelaysInfo::~ODFEDelaysInfo()
00032 {
00033 }
00034 
00035 
00036 
00037 int ODFEDelaysInfo::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 COND2CONF_INFO_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     std::cout << " the id is going to be : "<< result<< endl;
00052     return result; 
00053 
00054   } catch (SQLException &e) {
00055     throw(std::runtime_error("ODFEDelaysInfo::fetchNextId():  "+e.getMessage()));
00056   }
00057 
00058 }
00059 
00060 void ODFEDelaysInfo::prepareWrite()
00061   throw(std::runtime_error)
00062 {
00063   this->checkConnection();
00064 
00065   int next_id=0;
00066   if(getId()==0){
00067     next_id=fetchNextId();
00068   } 
00069 
00070   try {
00071     m_writeStmt = m_conn->createStatement();
00072     m_writeStmt->setSQL("INSERT INTO "+getTable()+" ( rec_id, tag, version) " 
00073                         " VALUES ( :1, :2, :3 ) " );
00074 
00075     m_writeStmt->setInt(1, next_id);
00076     m_ID=next_id;
00077 
00078   } catch (SQLException &e) {
00079     throw(std::runtime_error("ODFEDelaysInfo::prepareWrite():  "+e.getMessage()));
00080   }
00081 
00082 }
00083 
00084 void ODFEDelaysInfo::setParameters(std::map<string,string> my_keys_map){
00085   
00086   // parses the result of the XML parser that is a map of 
00087   // string string with variable name variable value 
00088   
00089   for( std::map<std::string, std::string >::iterator ci=
00090          my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
00091     
00092     if(ci->first==  "VERSION") setVersion(atoi(ci->second.c_str()) );
00093     if(ci->first==  "TAG") setConfigTag(ci->second);
00094     
00095   }
00096   
00097 }
00098 
00099 void ODFEDelaysInfo::writeDB()
00100   throw(std::runtime_error)
00101 {
00102   this->checkConnection();
00103   this->checkPrepare();
00104 
00105   try {
00106 
00107     // number 1 is the id 
00108     m_writeStmt->setString(2, this->getConfigTag());
00109     m_writeStmt->setInt(3, this->getVersion());
00110 
00111     m_writeStmt->executeUpdate();
00112 
00113 
00114   } catch (SQLException &e) {
00115     throw(std::runtime_error("ODFEDelaysInfo::writeDB():  "+e.getMessage()));
00116   }
00117 
00118   // Now get the ID
00119   if (!this->fetchID()) {
00120     throw(std::runtime_error("ODFEDelaysInfo::writeDB:  Failed to write"));
00121   } else {
00122     int old_version=this->getVersion();
00123     m_readStmt = m_conn->createStatement(); 
00124     this->fetchData (this);
00125     m_conn->terminateStatement(m_readStmt);
00126     if(this->getVersion()!=old_version) std::cout << "ODFEDelaysInfo>>WARNING version is "<< getVersion()<< endl; 
00127   }
00128 }
00129 
00130 
00131 void ODFEDelaysInfo::fetchData(ODFEDelaysInfo * result)
00132   throw(std::runtime_error)
00133 {
00134 
00135   this->checkConnection();
00136 
00137   result->clear();
00138   if(result->getId()==0 && (result->getConfigTag()=="") ){
00139     throw(std::runtime_error("ODFEDelaysInfo::fetchData(): no Id defined for this ODFEDelaysInfo "));
00140   }
00141 
00142   try {
00143     if(result->getId()!=0) { 
00144       m_readStmt->setSQL("SELECT * FROM " + getTable() +   
00145                          " where  rec_id = :1 ");
00146       m_readStmt->setInt(1, result->getId());
00147     } else if (result->getConfigTag()!="") {
00148       m_readStmt->setSQL("SELECT * FROM " + getTable() +   
00149                          " where  tag=:1 AND version=:2 " );
00150       m_readStmt->setString(1, result->getConfigTag());
00151       m_readStmt->setInt(2, result->getVersion());
00152     } else {
00153       // we should never pass here 
00154       throw(std::runtime_error("ODFEDelaysInfo::fetchData(): no Id defined for this ODFEDelaysInfo "));
00155     }
00156 
00157     ResultSet* rset = m_readStmt->executeQuery();
00158     rset->next();
00159 
00160     // 1 is the id and 2 is the config tag and 3 is the version
00161 
00162     result->setId(rset->getInt(1));
00163     result->setConfigTag(rset->getString(2));
00164     result->setVersion(rset->getInt(3));
00165 
00166   } catch (SQLException &e) {
00167     throw(std::runtime_error("ODFEDelaysInfo::fetchData():  "+e.getMessage()));
00168   }
00169 }
00170 
00171 int ODFEDelaysInfo::fetchID()    throw(std::runtime_error)
00172 {
00173   // Return from memory if available
00174   if (m_ID!=0) {
00175     return m_ID;
00176   }
00177 
00178   this->checkConnection();
00179 
00180   try {
00181     Statement* stmt = m_conn->createStatement();
00182     stmt->setSQL("SELECT rec_id FROM "+ getTable()+
00183                  "WHERE  tag=:1 and version=:2 " );
00184 
00185     stmt->setString(1, getConfigTag() );
00186     stmt->setInt(2, getVersion() );
00187 
00188     ResultSet* rset = stmt->executeQuery();
00189 
00190     if (rset->next()) {
00191       m_ID = rset->getInt(1);
00192     } else {
00193       m_ID = 0;
00194     }
00195     m_conn->terminateStatement(stmt);
00196   } catch (SQLException &e) {
00197     throw(std::runtime_error("ODFEDelaysInfo::fetchID:  "+e.getMessage()));
00198   }
00199 
00200   return m_ID;
00201 }