Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:09

0001 #include <stdexcept>
0002 #include <string>
0003 #include <cstring>
0004 #include "OnlineDB/Oracle/interface/Oracle.h"
0005 #include "OnlineDB/EcalCondDB/interface/DateHandler.h"
0006 
0007 #include "OnlineDB/EcalCondDB/interface/ODCond2ConfInfo.h"
0008 
0009 using namespace std;
0010 using namespace oracle::occi;
0011 
0012 ODCond2ConfInfo::ODCond2ConfInfo() {
0013   m_env = nullptr;
0014   m_conn = nullptr;
0015   m_writeStmt = nullptr;
0016   m_readStmt = nullptr;
0017   m_config_tag = "";
0018   m_ID = 0;
0019   clear();
0020 }
0021 
0022 void ODCond2ConfInfo::clear() {
0023   m_type = "";
0024   m_loc = "";
0025   m_run = 0;
0026   m_desc = "";
0027   m_rec_time = Tm();
0028   m_db_time = Tm();
0029   m_typ_id = 0;
0030   m_loc_id = 0;
0031 }
0032 
0033 ODCond2ConfInfo::~ODCond2ConfInfo() {}
0034 
0035 int ODCond2ConfInfo::fetchNextId() noexcept(false) {
0036   int result = 0;
0037   try {
0038     this->checkConnection();
0039 
0040     m_readStmt = m_conn->createStatement();
0041     m_readStmt->setSQL("select COND2CONF_INFO_SQ.NextVal from DUAL ");
0042     ResultSet* rset = m_readStmt->executeQuery();
0043     while (rset->next()) {
0044       result = rset->getInt(1);
0045     }
0046     result++;
0047     m_conn->terminateStatement(m_readStmt);
0048     return result;
0049 
0050   } catch (SQLException& e) {
0051     throw(std::runtime_error(std::string("ODCond2ConfInfo::fetchNextId():  ") + e.getMessage()));
0052   }
0053 }
0054 
0055 void ODCond2ConfInfo::fetchParents() noexcept(false) {
0056   if (m_typ_id == 0) {
0057     if (!getType().empty()) {
0058       try {
0059         this->checkConnection();
0060         m_readStmt = m_conn->createStatement();
0061         m_readStmt->setSQL("select def_id from COND2CONF_TYPE_DEF where rec_type=" + getType());
0062         ResultSet* rset = m_readStmt->executeQuery();
0063         while (rset->next()) {
0064           m_typ_id = rset->getInt(1);
0065         }
0066         m_conn->terminateStatement(m_readStmt);
0067 
0068       } catch (SQLException& e) {
0069         throw(std::runtime_error(std::string("ODCond2ConfInfo::fetchParents():  ") + e.getMessage()));
0070       }
0071     }
0072   }
0073   if (m_loc_id == 0) {
0074     if (!getLocation().empty()) {
0075       try {
0076         this->checkConnection();
0077         m_readStmt = m_conn->createStatement();
0078         m_readStmt->setSQL("select def_id from location_def where location=" + getLocation());
0079         ResultSet* rset = m_readStmt->executeQuery();
0080         while (rset->next()) {
0081           m_loc_id = rset->getInt(1);
0082         }
0083         m_conn->terminateStatement(m_readStmt);
0084       } catch (SQLException& e) {
0085         throw(std::runtime_error(std::string("ODCond2ConfInfo::fetchParents():  ") + e.getMessage()));
0086       }
0087     }
0088   }
0089 }
0090 
0091 void ODCond2ConfInfo::prepareWrite() noexcept(false) {
0092   this->checkConnection();
0093 
0094   int next_id = 0;
0095   if (getId() == 0) {
0096     next_id = fetchNextId();
0097   }
0098 
0099   fetchParents();
0100   try {
0101     m_writeStmt = m_conn->createStatement();
0102     m_writeStmt->setSQL("INSERT INTO " + getTable() +
0103                         " ( rec_id, rec_type_id, rec_date, "
0104                         "location_id, run_number, short_desc ) "
0105                         " VALUES ( :1, :2, :3 , :4, :5, :6 ) ");
0106 
0107     m_writeStmt->setInt(1, next_id);
0108     m_writeStmt->setInt(3, m_typ_id);
0109     m_writeStmt->setInt(4, m_loc_id);
0110 
0111     m_ID = next_id;
0112 
0113   } catch (SQLException& e) {
0114     throw(std::runtime_error(std::string("ODCond2ConfInfo::prepareWrite():  ") + e.getMessage()));
0115   }
0116 }
0117 
0118 void ODCond2ConfInfo::setParameters(const std::map<string, string>& my_keys_map) {
0119   // parses the result of the XML parser that is a map of
0120   // string string with variable name variable value
0121 
0122   for (std::map<std::string, std::string>::const_iterator ci = my_keys_map.begin(); ci != my_keys_map.end(); ci++) {
0123     //    if(ci->first==  "TAG") setConfigTag(ci->second);
0124   }
0125   std::cout << "method not yet implemented" << std::endl;
0126 }
0127 
0128 void ODCond2ConfInfo::writeDB() noexcept(false) {
0129   this->checkConnection();
0130   this->checkPrepare();
0131 
0132   DateHandler dh(m_env, m_conn);
0133   if (m_rec_time.isNull()) {
0134     int very_old_time = 0;
0135     m_rec_time = Tm(very_old_time);
0136   }
0137 
0138   try {
0139     m_writeStmt->setDate(3, dh.tmToDate(this->m_rec_time));
0140     m_writeStmt->setInt(5, this->getRunNumber());
0141     m_writeStmt->setString(6, this->getDescription());
0142 
0143     m_writeStmt->executeUpdate();
0144 
0145   } catch (SQLException& e) {
0146     throw(std::runtime_error(std::string("ODCond2ConfInfo::writeDB():  ") + e.getMessage()));
0147   }
0148   // Now get the ID
0149   if (!this->fetchID()) {
0150     throw(std::runtime_error("ODCond2ConfInfo::writeDB:  Failed to write"));
0151   }
0152 }
0153 
0154 void ODCond2ConfInfo::fetchData(ODCond2ConfInfo* result) noexcept(false) {
0155   this->checkConnection();
0156   result->clear();
0157 
0158   DateHandler dh(m_env, m_conn);
0159 
0160   if (result->getId() == 0) {
0161     throw(std::runtime_error("ODCond2ConfInfo::fetchData(): no Id defined for this ODCond2ConfInfo "));
0162   }
0163 
0164   try {
0165     m_readStmt->setSQL(
0166         "SELECT rec_id, REC_TYPE, rec_date, location, "
0167         "run_number, short_desc, db_timestamp FROM " +
0168         getTable() +
0169         " , COND2CONF_TYPE_DEF , location_def "
0170         " where  rec_id = :1  AND COND2CONF_TYPE_DEF.def_id=" +
0171         getTable() + ".REC_TYPE_ID AND location_def.def_id=LOCATION_ID ");
0172     m_readStmt->setInt(1, result->getId());
0173     ResultSet* rset = m_readStmt->executeQuery();
0174 
0175     rset->next();
0176 
0177     // 1 is the id and 2 is the config tag and 3 is the version
0178 
0179     //    result->setId(rset->getInt(1));
0180 
0181     result->setType(rset->getString(2));
0182     Date startDate = rset->getDate(3);
0183     result->setLocation(rset->getString(4));
0184     result->setRunNumber(rset->getInt(5));
0185     result->setDescription(rset->getString(6));
0186     Date endDate = rset->getDate(7);
0187 
0188     m_rec_time = dh.dateToTm(startDate);
0189     m_db_time = dh.dateToTm(endDate);
0190 
0191   } catch (SQLException& e) {
0192     throw(std::runtime_error(std::string("ODCond2ConfInfo::fetchData():  ") + e.getMessage()));
0193   }
0194 }
0195 
0196 int ODCond2ConfInfo::fetchID() noexcept(false) {
0197   // Return from memory if available
0198   if (m_ID != 0) {
0199     return m_ID;
0200   }
0201 
0202   this->checkConnection();
0203 
0204   fetchParents();
0205 
0206   try {
0207     Statement* stmt = m_conn->createStatement();
0208     stmt->setSQL("SELECT rec_id FROM " + getTable() +
0209                  "WHERE  rec_type_id=:1 and (run_number=:2 or short_desc=:3 ) order by rec_id DESC ");
0210 
0211     stmt->setInt(1, m_typ_id);
0212     stmt->setInt(2, getRunNumber());
0213     stmt->setString(3, getDescription());
0214 
0215     ResultSet* rset = stmt->executeQuery();
0216 
0217     if (rset->next()) {
0218       m_ID = rset->getInt(1);
0219     } else {
0220       m_ID = 0;
0221     }
0222     m_conn->terminateStatement(stmt);
0223   } catch (SQLException& e) {
0224     throw(std::runtime_error(std::string("ODCond2ConfInfo::fetchID:  ") + e.getMessage()));
0225   }
0226 
0227   return m_ID;
0228 }