CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/OnlineDB/EcalCondDB/src/ODScanConfig.cc

Go to the documentation of this file.
00001 #include <stdexcept>
00002 #include <cstdlib>
00003 #include <string>
00004 #include "OnlineDB/Oracle/interface/Oracle.h"
00005 
00006 #include "OnlineDB/EcalCondDB/interface/ODScanConfig.h"
00007 
00008 using namespace std;
00009 using namespace oracle::occi;
00010 
00011 ODScanConfig::ODScanConfig()
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 
00024 ODScanConfig::~ODScanConfig()
00025 {
00026 }
00027 
00028 void ODScanConfig::clear(){
00029 
00030   m_type_id=0;
00031   m_type="";
00032   m_from_val=0;
00033   m_to_val=0;
00034   m_step=0;
00035 
00036 }
00037 
00038 int ODScanConfig::fetchNextId()  throw(std::runtime_error) {
00039 
00040   int result=0;
00041   try {
00042     this->checkConnection();
00043 
00044     m_readStmt = m_conn->createStatement(); 
00045     m_readStmt->setSQL("select ecal_scan_config_sq.NextVal from dual");
00046     ResultSet* rset = m_readStmt->executeQuery();
00047     while (rset->next ()){
00048       result= rset->getInt(1);
00049     }
00050     m_conn->terminateStatement(m_readStmt);
00051     return result; 
00052 
00053   } catch (SQLException &e) {
00054     throw(std::runtime_error("ODScanConfig::fetchNextId():  "+e.getMessage()));
00055   }
00056 
00057 }
00058 
00059 
00060 void ODScanConfig::setParameters(std::map<string,string> my_keys_map){
00061   
00062   // parses the result of the XML parser that is a map of 
00063   // string string with variable name variable value 
00064   
00065   for( std::map<std::string, std::string >::iterator ci=
00066          my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
00067     
00068     if(ci->first==  "SCAN_ID") setConfigTag(ci->second);
00069     if(ci->first==  "TYPE_ID") setTypeId(atoi(ci->second.c_str()) );
00070     if(ci->first==  "TYPE" || ci->first== "SCAN_TYPE") setScanType(ci->second.c_str() );
00071     if(ci->first==  "FROM" ||ci->first==  "FROM_VAL" ) setFromVal(atoi(ci->second.c_str() ));
00072     if(ci->first==  "TO" ||ci->first==  "TO_VAL" ) setToVal(atoi(ci->second.c_str() ));
00073     if(ci->first==  "STEP") setStep(atoi(ci->second.c_str() ));
00074       
00075   }
00076   
00077 }
00078 
00079 void ODScanConfig::prepareWrite()
00080   throw(std::runtime_error)
00081 {
00082   this->checkConnection();
00083   int next_id=fetchNextId();
00084 
00085   try {
00086     m_writeStmt = m_conn->createStatement();
00087     m_writeStmt->setSQL("INSERT INTO ECAL_scan_dat ( scan_id, scan_tag ,"
00088                         "   type_id, scan_type , FROM_VAL , TO_VAL, STEP )"
00089                         " VALUES ( :1, :2, :3, :4, :5, :6, :7)");
00090     m_writeStmt->setInt(1, next_id);
00091     m_ID=next_id;
00092 
00093   } catch (SQLException &e) {
00094     throw(std::runtime_error("ODScanConfig::prepareWrite():  "+e.getMessage()));
00095   }
00096 }
00097 
00098 void ODScanConfig::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 
00109     m_writeStmt->setInt(3, this->getTypeId());
00110     m_writeStmt->setString(4,  this->getScanType() );
00111     m_writeStmt->setInt(5, this->getFromVal() );
00112     m_writeStmt->setInt(6, this->getToVal() );
00113     m_writeStmt->setInt(7, this->getStep() );
00114  
00115     m_writeStmt->executeUpdate();
00116 
00117 
00118   } catch (SQLException &e) {
00119     throw(std::runtime_error("ODScanConfig::writeDB():  "+e.getMessage()));
00120   }
00121   // Now get the ID
00122   if (!this->fetchID()) {
00123     throw(std::runtime_error("ODScanConfig::writeDB:  Failed to write"));
00124   }
00125 
00126 }
00127 
00128 
00129 
00130 
00131 void ODScanConfig::fetchData(ODScanConfig * 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("ODScanConfig::fetchData(): no Id defined for this ODScanConfig "));
00138   }
00139 
00140   try {
00141 
00142     m_readStmt->setSQL("SELECT * "
00143                        "FROM ECAL_SCAN_DAT "
00144                        " where (scan_id = :1  or scan_tag=:2 )" );
00145      m_readStmt->setInt(1, result->getId());
00146     m_readStmt->setString(2, result->getConfigTag());
00147 
00148     ResultSet* rset = m_readStmt->executeQuery();
00149 
00150     rset->next();
00151 
00152     // id 1 is the scan_id 
00153     result->setId(rset->getInt(1));
00154     result->setConfigTag(rset->getString(2));
00155     result->setTypeId(           rset->getInt(3) );
00156     result->setScanType(        rset->getString(4) );
00157     result->setFromVal(            rset->getInt(5) );
00158     result->setToVal(              rset->getInt(6) );
00159     result->setStep(              rset->getInt(7) );
00160 
00161   } catch (SQLException &e) {
00162     throw(std::runtime_error("ODScanConfig::fetchData():  "+e.getMessage()));
00163   }
00164 }
00165 
00166 int ODScanConfig::fetchID()    throw(std::runtime_error)
00167 {
00168   // Return from memory if available
00169   if (m_ID!=0) {
00170     return m_ID;
00171   }
00172 
00173   this->checkConnection();
00174 
00175   try {
00176     Statement* stmt = m_conn->createStatement();
00177     stmt->setSQL("SELECT scan_id FROM ecal_scan_dat "
00178                  "WHERE scan_tag=:scan_tag ");
00179     
00180     stmt->setString(1, getConfigTag() );
00181 
00182     ResultSet* rset = stmt->executeQuery();
00183 
00184     if (rset->next()) {
00185       m_ID = rset->getInt(1);
00186     } else {
00187       m_ID = 0;
00188     }
00189     m_conn->terminateStatement(stmt);
00190   } catch (SQLException &e) {
00191     throw(std::runtime_error("ODScanConfig::fetchID:  "+e.getMessage()));
00192   }
00193 
00194   return m_ID;
00195 }