CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/OnlineDB/EcalCondDB/src/ODBadXTInfo.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/ODBadXTInfo.h"
00007 
00008 using namespace std;
00009 using namespace oracle::occi;
00010 
00011 ODBadXTInfo::ODBadXTInfo()
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    m_version=0;
00020    clear();   
00021 }
00022 
00023 
00024 void ODBadXTInfo::clear(){
00025 
00026 }
00027 
00028 
00029 
00030 ODBadXTInfo::~ODBadXTInfo()
00031 {
00032 }
00033 
00034 
00035 
00036 int ODBadXTInfo::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(std::runtime_error("ODBadXTInfo::fetchNextId():  "+e.getMessage()));
00054   }
00055 
00056 }
00057 
00058 void ODBadXTInfo::prepareWrite()
00059   throw(std::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(std::runtime_error("ODBadXTInfo::prepareWrite():  "+e.getMessage()));
00078   }
00079 
00080 }
00081 
00082 void ODBadXTInfo::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 ODBadXTInfo::writeDB()
00098   throw(std::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(std::runtime_error("ODBadXTInfo::writeDB():  "+e.getMessage()));
00114   }
00115   // Now get the ID
00116   if (!this->fetchID()) {
00117     throw(std::runtime_error("ODBadXTInfo::writeDB:  Failed to write"));
00118   } else {
00119     int old_version=this->getVersion();
00120     m_readStmt = m_conn->createStatement(); 
00121     this->fetchData (this);
00122     m_conn->terminateStatement(m_readStmt);
00123     if(this->getVersion()!=old_version) std::cout << "ODBadXTInfo>>WARNING version is "<< getVersion()<< endl; 
00124   }
00125 
00126 
00127 }
00128 
00129 
00130 void ODBadXTInfo::fetchData(ODBadXTInfo * result)
00131   throw(std::runtime_error)
00132 {
00133   this->checkConnection();
00134   result->clear();
00135   if(result->getId()==0 && (result->getConfigTag()=="") ){
00136     throw(std::runtime_error("ODBadXTInfo::fetchData(): no Id defined for this ODBadXTInfo "));
00137   }
00138 
00139 
00140 
00141   try {
00142     if(result->getId()!=0) { 
00143       m_readStmt->setSQL("SELECT * FROM " + getTable() +   
00144                          " where  rec_id = :1 ");
00145       m_readStmt->setInt(1, result->getId());
00146     } else if (result->getConfigTag()!="") {
00147       m_readStmt->setSQL("SELECT * FROM " + getTable() +   
00148                          " where  tag=:1 AND version=:2 " );
00149       m_readStmt->setString(1, result->getConfigTag());
00150       m_readStmt->setInt(2, result->getVersion());
00151     } else {
00152       // we should never pass here 
00153       throw(std::runtime_error("ODBadXTInfo::fetchData(): no Id defined for this record "));
00154     }
00155 
00156 
00157     ResultSet* rset = m_readStmt->executeQuery();
00158 
00159     rset->next();
00160 
00161     // 1 is the id and 2 is the config tag and 3 is the version
00162 
00163     result->setId(rset->getInt(1));
00164     result->setConfigTag(rset->getString(2));
00165     result->setVersion(rset->getInt(3));
00166 
00167   } catch (SQLException &e) {
00168     throw(std::runtime_error("ODBadXTInfo::fetchData():  "+e.getMessage()));
00169   }
00170 }
00171 
00172 int ODBadXTInfo::fetchID()    throw(std::runtime_error)
00173 {
00174   // Return from memory if available
00175   if (m_ID!=0) {
00176     return m_ID;
00177   }
00178 
00179   this->checkConnection();
00180 
00181   try {
00182     Statement* stmt = m_conn->createStatement();
00183     stmt->setSQL("SELECT rec_id FROM "+ getTable()+
00184                  "WHERE  tag=:1 and version=:2 " );
00185 
00186     stmt->setString(1, getConfigTag() );
00187     stmt->setInt(2, getVersion() );
00188 
00189     ResultSet* rset = stmt->executeQuery();
00190 
00191     if (rset->next()) {
00192       m_ID = rset->getInt(1);
00193     } else {
00194       m_ID = 0;
00195     }
00196     m_conn->terminateStatement(stmt);
00197   } catch (SQLException &e) {
00198     throw(std::runtime_error("ODBadXTInfo::fetchID:  "+e.getMessage()));
00199   }
00200 
00201   return m_ID;
00202 }