CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/OnlineDB/EcalCondDB/src/ODFEPedestalOffsetInfo.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/ODFEPedestalOffsetInfo.h"
00008 
00009 using namespace std;
00010 using namespace oracle::occi;
00011 
00012 ODFEPedestalOffsetInfo::ODFEPedestalOffsetInfo()
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 ODFEPedestalOffsetInfo::clear(){
00026 
00027 }
00028 
00029 
00030 
00031 ODFEPedestalOffsetInfo::~ODFEPedestalOffsetInfo()
00032 {
00033 }
00034 
00035 
00036 
00037 int ODFEPedestalOffsetInfo::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("ODFEPedestalOffsetInfo::fetchNextId():  "+e.getMessage()));
00055   }
00056 
00057 }
00058 
00059 void ODFEPedestalOffsetInfo::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("ODFEPedestalOffsetInfo::prepareWrite():  "+e.getMessage()));
00079   }
00080 
00081 }
00082 
00083 void ODFEPedestalOffsetInfo::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 ODFEPedestalOffsetInfo::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 
00110     m_writeStmt->executeUpdate();
00111 
00112 
00113   } catch (SQLException &e) {
00114     throw(std::runtime_error("ODFEPedestalOffsetInfo::writeDB():  "+e.getMessage()));
00115   }
00116 
00117 
00118   // Now get the ID
00119   if (!this->fetchID()) {
00120     throw(std::runtime_error("ODFEPedestalOffsetInfo::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 << "ODFEPedestalOffsetInfo>>WARNING version is "<< getVersion()<< endl; 
00127   }
00128 
00129 
00130 
00131 }
00132 
00133 
00134 void ODFEPedestalOffsetInfo::fetchData(ODFEPedestalOffsetInfo * result)
00135   throw(std::runtime_error)
00136 {
00137   this->checkConnection();
00138   result->clear();
00139   if(result->getId()==0 && (result->getConfigTag()=="") ){
00140     throw(std::runtime_error("ODFEPedestalOffsetInfo::fetchData(): no Id defined for this ODFEPedestalOffsetInfo "));
00141   }
00142 
00143   try {
00144     if(result->getId()!=0) { 
00145       m_readStmt->setSQL("SELECT * FROM " + getTable() +   
00146                          " where  rec_id = :1 ");
00147       m_readStmt->setInt(1, result->getId());
00148     } else if (result->getConfigTag()!="") {
00149       m_readStmt->setSQL("SELECT * FROM " + getTable() +   
00150                          " where  tag=:1 AND version=:2 " );
00151       m_readStmt->setString(1, result->getConfigTag());
00152       m_readStmt->setInt(2, result->getVersion());
00153     } else {
00154       // we should never pass here 
00155       throw(std::runtime_error("ODFEPedestalOffsetInfo::fetchData(): no Id defined for this record "));
00156     }
00157 
00158     ResultSet* rset = m_readStmt->executeQuery();
00159 
00160     rset->next();
00161 
00162     // 1 is the id and 2 is the config tag and 3 is the version
00163 
00164     result->setId(rset->getInt(1));
00165     result->setConfigTag(rset->getString(2));
00166     result->setVersion(rset->getInt(3));
00167 
00168   } catch (SQLException &e) {
00169     throw(std::runtime_error("ODFEPedestalOffsetInfo::fetchData():  "+e.getMessage()));
00170   }
00171 }
00172 
00173 void ODFEPedestalOffsetInfo::fetchLastData(ODFEPedestalOffsetInfo * result)
00174   throw(std::runtime_error)
00175 {
00176   this->checkConnection();
00177   result->clear();
00178   try {
00179 
00180     m_readStmt->setSQL("SELECT * FROM " + getTable() +   
00181                        " where  rec_id = ( select max(rec_id) from "+ getTable() +" ) " );
00182     ResultSet* rset = m_readStmt->executeQuery();
00183 
00184     rset->next();
00185 
00186     result->setId(rset->getInt(1));
00187     result->setConfigTag(rset->getString(2));
00188     result->setVersion(rset->getInt(3));
00189 
00190   } catch (SQLException &e) {
00191     throw(std::runtime_error("ODFEPedestalOffsetInfo::fetchData():  "+e.getMessage()));
00192   }
00193 }
00194 
00195 int ODFEPedestalOffsetInfo::fetchID()    throw(std::runtime_error)
00196 {
00197   // Return from memory if available
00198   if (m_ID!=0) {
00199     return m_ID;
00200   }
00201 
00202   this->checkConnection();
00203 
00204   try {
00205     Statement* stmt = m_conn->createStatement();
00206     stmt->setSQL("SELECT rec_id FROM "+ getTable()+
00207                  "WHERE  tag=:1 and version=:2 " );
00208 
00209     stmt->setString(1, getConfigTag() );
00210     stmt->setInt(2, getVersion() );
00211 
00212     ResultSet* rset = stmt->executeQuery();
00213 
00214     if (rset->next()) {
00215       m_ID = rset->getInt(1);
00216     } else {
00217       m_ID = 0;
00218     }
00219     m_conn->terminateStatement(stmt);
00220   } catch (SQLException &e) {
00221     throw(std::runtime_error("ODFEPedestalOffsetInfo::fetchID:  "+e.getMessage()));
00222   }
00223 
00224   return m_ID;
00225 }