CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/OnlineDB/EcalCondDB/src/ODVfeToRejectInfo.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/ODVfeToRejectInfo.h"
00007 
00008 using namespace std;
00009 using namespace oracle::occi;
00010 
00011 ODVfeToRejectInfo::ODVfeToRejectInfo()
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 ODVfeToRejectInfo::clear(){
00025 
00026 }
00027 
00028 
00029 
00030 ODVfeToRejectInfo::~ODVfeToRejectInfo()
00031 {
00032 }
00033 
00034 
00035 
00036 int ODVfeToRejectInfo::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("ODVfeToRejectInfo::fetchNextId():  "+e.getMessage()));
00054   }
00055 
00056 }
00057 
00058 void ODVfeToRejectInfo::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("ODVfeToRejectInfo::prepareWrite():  "+e.getMessage()));
00078   }
00079 
00080 }
00081 
00082 void ODVfeToRejectInfo::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 ODVfeToRejectInfo::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("ODVfeToRejectInfo::writeDB():  "+e.getMessage()));
00114   }
00115   // Now get the ID
00116   if (!this->fetchID()) {
00117     throw(std::runtime_error("ODVfeToRejectInfo::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 << "ODVfeToRejectInfo>>WARNING version is "<< getVersion()<< endl; 
00124   }
00125 
00126 
00127 }
00128 
00129 
00130 void ODVfeToRejectInfo::fetchData(ODVfeToRejectInfo * 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("ODVfeToRejectInfo::fetchData(): no Id defined for this ODVfeToRejectInfo "));
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 
00148       if(result->getVersion() !=0){
00149         m_readStmt->setSQL("SELECT * FROM " + getTable() +
00150                      " WHERE tag = :tag "
00151                      " and version = :version " );
00152         m_readStmt->setString(1, result->getConfigTag());
00153         m_readStmt->setInt(2, result->getVersion());
00154       } else {
00155         // always select the last inserted one with a given tag
00156         m_readStmt->setSQL("SELECT * FROM " + getTable() +
00157                      " WHERE tag = :1 and version= (select max(version) from "+getTable() +" where tag=:2) " );
00158         m_readStmt->setString(1, result->getConfigTag());
00159         m_readStmt->setString(2, result->getConfigTag());
00160       }
00161 
00162     } else {
00163       // we should never pass here 
00164       throw(std::runtime_error("ODVfeToRejectInfo::fetchData(): no Id defined for this record "));
00165     }
00166 
00167 
00168     ResultSet* rset = m_readStmt->executeQuery();
00169 
00170     rset->next();
00171 
00172     // 1 is the id and 2 is the config tag and 3 is the version
00173 
00174     result->setId(rset->getInt(1));
00175     result->setConfigTag(rset->getString(2));
00176     result->setVersion(rset->getInt(3));
00177 
00178   } catch (SQLException &e) {
00179     throw(std::runtime_error("ODVfeToRejectInfo::fetchData():  "+e.getMessage()));
00180   }
00181 }
00182 
00183 int ODVfeToRejectInfo::fetchID()    throw(std::runtime_error)
00184 {
00185   // Return from memory if available
00186   if (m_ID!=0) {
00187     return m_ID;
00188   }
00189 
00190   this->checkConnection();
00191 
00192   try {
00193     Statement* stmt = m_conn->createStatement();
00194     stmt->setSQL("SELECT rec_id FROM "+ getTable()+
00195                  " WHERE  tag=:1 and version=:2 " );
00196 
00197     stmt->setString(1, getConfigTag() );
00198     stmt->setInt(2, getVersion() );
00199 
00200     ResultSet* rset = stmt->executeQuery();
00201 
00202     if (rset->next()) {
00203       m_ID = rset->getInt(1);
00204     } else {
00205       m_ID = 0;
00206     }
00207     m_conn->terminateStatement(stmt);
00208   } catch (SQLException &e) {
00209     throw(std::runtime_error("ODVfeToRejectInfo::fetchID:  "+e.getMessage()));
00210   }
00211 
00212   return m_ID;
00213 }