CMS 3D CMS Logo

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

Generated on Tue Jun 9 17:40:49 2009 for CMSSW by  doxygen 1.5.4