CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/OnlineDB/EcalCondDB/src/ODFEWeightsInfo.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/ODFEWeightsInfo.h"
00008 
00009 using namespace std;
00010 using namespace oracle::occi;
00011 
00012 ODFEWeightsInfo::ODFEWeightsInfo()
00013 {
00014   m_env = NULL;
00015   m_conn = NULL;
00016   m_writeStmt = NULL;
00017   m_readStmt = NULL;
00018   m_config_tag="";
00019   m_ID=0;
00020   m_version=0;
00021   clear();   
00022 }
00023 
00024 
00025 void ODFEWeightsInfo::clear(){
00026 
00027 }
00028 
00029 
00030 
00031 ODFEWeightsInfo::~ODFEWeightsInfo()
00032 {
00033 }
00034 
00035 
00036 
00037 int ODFEWeightsInfo::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     return result; 
00052 
00053   } catch (SQLException &e) {
00054     throw(std::runtime_error("ODFEWeightsInfo::fetchNextId():  "+e.getMessage()));
00055   }
00056 
00057 }
00058 
00059 void ODFEWeightsInfo::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()+" ( rec_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("ODFEWeightsInfo::prepareWrite():  "+e.getMessage()));
00079   }
00080 
00081 }
00082 
00083 void ODFEWeightsInfo::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 ODFEWeightsInfo::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     m_writeStmt->executeUpdate();
00110 
00111 
00112   } catch (SQLException &e) {
00113     throw(std::runtime_error("ODFEWeightsInfo::writeDB():  "+e.getMessage()));
00114   }
00115 
00116   // Now get the ID
00117   if (!this->fetchID()) {
00118     throw(std::runtime_error("ODFEWeightsInfo::writeDB:  Failed to write"));
00119   } else {
00120     int old_version=this->getVersion();
00121     m_readStmt = m_conn->createStatement(); 
00122     this->fetchData (this);
00123     m_conn->terminateStatement(m_readStmt);
00124     if(this->getVersion()!=old_version) std::cout << "ODFEWeightsInfo>>WARNING version is "<< getVersion()<< endl; 
00125   }
00126 
00127 
00128 }
00129 
00130 
00131 void ODFEWeightsInfo::fetchData(ODFEWeightsInfo * result)
00132   throw(std::runtime_error)
00133 {
00134   this->checkConnection();
00135   result->clear();
00136   if(result->getId()==0 && (result->getConfigTag()=="") ){
00137     throw(std::runtime_error("ODFEWeightsInfo::fetchData(): no Id defined for this ODFEWeightsInfo "));
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("ODFEWeightsInfo::fetchData(): no Id defined for this ODFEDelaysInfo "));
00154     }
00155 
00156     ResultSet* rset = m_readStmt->executeQuery();
00157 
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("ODFEWeightsInfo::fetchData():  "+e.getMessage()));
00168   }
00169 }
00170 
00171 int ODFEWeightsInfo::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("ODFEWeightsInfo::fetchID:  "+e.getMessage()));
00198   }
00199 
00200   return m_ID;
00201 }