Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:07

0001 #include <stdexcept>
0002 #include <cstdlib>
0003 #include <string>
0004 #include <cstring>
0005 #include "OnlineDB/Oracle/interface/Oracle.h"
0006 
0007 #include "OnlineDB/EcalCondDB/interface/ODFEDAQConfig.h"
0008 
0009 using namespace std;
0010 using namespace oracle::occi;
0011 
0012 #define MY_NULL -1
0013 #define SET_INT(statement, paramNum, paramVal) \
0014   if (paramVal != MY_NULL) {                   \
0015     statement->setInt(paramNum, paramVal);     \
0016   } else {                                     \
0017     statement->setNull(paramNum, OCCINUMBER);  \
0018   }
0019 #define SET_STRING(statement, paramNum, paramVal) \
0020   if (!paramVal.empty()) {                        \
0021     statement->setString(paramNum, paramVal);     \
0022   } else {                                        \
0023     statement->setNull(paramNum, OCCICHAR);       \
0024   }
0025 
0026 int getInt(ResultSet* rset, int ipar) { return rset->isNull(ipar) ? MY_NULL : rset->getInt(ipar); }
0027 
0028 ODFEDAQConfig::ODFEDAQConfig() {
0029   m_env = nullptr;
0030   m_conn = nullptr;
0031   m_writeStmt = nullptr;
0032   m_readStmt = nullptr;
0033   m_config_tag = "";
0034   m_ID = 0;
0035   clear();
0036 }
0037 
0038 void ODFEDAQConfig::clear() {
0039   m_del = MY_NULL;
0040   m_wei = MY_NULL;
0041   m_ped = MY_NULL;
0042 
0043   m_bxt = MY_NULL;
0044   m_btt = MY_NULL;
0045   m_tbtt = MY_NULL;
0046   m_tbxt = MY_NULL;
0047 
0048   m_version = 0;
0049   m_com = "";
0050 }
0051 
0052 ODFEDAQConfig::~ODFEDAQConfig() {}
0053 
0054 int ODFEDAQConfig::fetchNextId() noexcept(false) {
0055   int result = 0;
0056   try {
0057     this->checkConnection();
0058 
0059     m_readStmt = m_conn->createStatement();
0060     m_readStmt->setSQL("select fe_daq_conDfig_sq.nextVal from dual");
0061     ResultSet* rset = m_readStmt->executeQuery();
0062     while (rset->next()) {
0063       result = rset->getInt(1);
0064     }
0065     m_conn->terminateStatement(m_readStmt);
0066     return result;
0067 
0068   } catch (SQLException& e) {
0069     throw(std::runtime_error(std::string("ODFEDAQConfig::fetchNextId():  ") + e.getMessage()));
0070   }
0071 }
0072 
0073 void ODFEDAQConfig::prepareWrite() noexcept(false) {
0074   this->checkConnection();
0075   int next_id = fetchNextId();
0076 
0077   try {
0078     m_writeStmt = m_conn->createStatement();
0079     m_writeStmt->setSQL(
0080         "INSERT INTO FE_DAQ_CONFIG ( config_id, tag, version, ped_id, "
0081         " del_id, wei_id,bxt_id, btt_id, tr_bxt_id, tr_btt_id, user_comment ) "
0082         "VALUES ( :1, :2, :3, :4, :5, :6, :7 ,:8, :9, :10, :11 )");
0083 
0084     m_writeStmt->setInt(1, next_id);
0085     m_ID = next_id;
0086 
0087   } catch (SQLException& e) {
0088     throw(std::runtime_error(std::string("ODFEDAQConfig::prepareWrite():  ") + e.getMessage()));
0089   }
0090 }
0091 
0092 void ODFEDAQConfig::setParameters(const std::map<string, string>& my_keys_map) {
0093   // parses the result of the XML parser that is a map of
0094   // string string with variable name variable value
0095 
0096   for (std::map<std::string, std::string>::const_iterator ci = my_keys_map.begin(); ci != my_keys_map.end(); ci++) {
0097     if (ci->first == "VERSION")
0098       setVersion(atoi(ci->second.c_str()));
0099     if (ci->first == "PED_ID")
0100       setPedestalId(atoi(ci->second.c_str()));
0101     if (ci->first == "DEL_ID")
0102       setDelayId(atoi(ci->second.c_str()));
0103     if (ci->first == "WEI_ID")
0104       setWeightId(atoi(ci->second.c_str()));
0105 
0106     if (ci->first == "BXT_ID")
0107       setBadXtId(atoi(ci->second.c_str()));
0108     if (ci->first == "BTT_ID")
0109       setBadTTId(atoi(ci->second.c_str()));
0110     if (ci->first == "TRIG_BXT_ID")
0111       setTriggerBadXtId(atoi(ci->second.c_str()));
0112     if (ci->first == "TRIG_BTT_ID")
0113       setTriggerBadTTId(atoi(ci->second.c_str()));
0114 
0115     if (ci->first == "COMMENT" || ci->first == "USER_COMMENT")
0116       setComment(ci->second);
0117   }
0118 }
0119 
0120 void ODFEDAQConfig::writeDB() noexcept(false) {
0121   this->checkConnection();
0122   this->checkPrepare();
0123 
0124   try {
0125     // number 1 is the id
0126     m_writeStmt->setString(2, this->getConfigTag());
0127     m_writeStmt->setInt(3, this->getVersion());
0128     SET_INT(m_writeStmt, 4, this->getPedestalId());
0129     SET_INT(m_writeStmt, 5, this->getDelayId());
0130     SET_INT(m_writeStmt, 6, this->getWeightId());
0131     SET_INT(m_writeStmt, 7, this->getBadXtId());
0132     SET_INT(m_writeStmt, 8, this->getBadTTId());
0133     SET_INT(m_writeStmt, 9, this->getTriggerBadXtId());
0134     SET_INT(m_writeStmt, 10, this->getTriggerBadTTId());
0135 
0136     m_writeStmt->setString(11, this->getComment());
0137 
0138     m_writeStmt->executeUpdate();
0139 
0140   } catch (SQLException& e) {
0141     throw(std::runtime_error(std::string("ODFEDAQConfig::writeDB():  ") + e.getMessage()));
0142   }
0143   // Now get the ID
0144   if (!this->fetchID()) {
0145     throw(std::runtime_error("ODFEDAQConfig::writeDB:  Failed to write"));
0146   }
0147 }
0148 
0149 void ODFEDAQConfig::fetchData(ODFEDAQConfig* result) noexcept(false) {
0150   this->checkConnection();
0151   result->clear();
0152   if (result->getId() == 0 && (result->getConfigTag().empty())) {
0153     throw(std::runtime_error("ODFEDAQConfig::fetchData(): no Id defined for this ODFEDAQConfig "));
0154   }
0155 
0156   m_readStmt = m_conn->createStatement();
0157   if (!result->getConfigTag().empty() && result->getVersion() == 0) {
0158     int new_version = 0;
0159     std::cout << "using new method : retrieving last version for this tag " << endl;
0160     try {
0161       this->checkConnection();
0162 
0163       m_readStmt->setSQL("select max(version) from " + getTable() + " where tag=:tag ");
0164       m_readStmt->setString(1, result->getConfigTag());
0165       std::cout << "Getting last ver" << std::endl << std::flush;
0166       ResultSet* rset = m_readStmt->executeQuery();
0167       while (rset->next()) {
0168         new_version = rset->getInt(1);
0169       }
0170       m_conn->terminateStatement(m_readStmt);
0171 
0172       //      m_readStmt = m_conn->createStatement();
0173 
0174       result->setVersion(new_version);
0175 
0176     } catch (SQLException& e) {
0177       throw(std::runtime_error(std::string("ODFEDAQConfig::fetchData():  ") + e.getMessage()));
0178     }
0179   }
0180 
0181   try {
0182     m_readStmt->setSQL("SELECT * FROM " + getTable() + " where ( config_id = :1 or (tag=:2 AND version=:3 ) )");
0183     m_readStmt->setInt(1, result->getId());
0184     m_readStmt->setString(2, result->getConfigTag());
0185     m_readStmt->setInt(3, result->getVersion());
0186     ResultSet* rset = m_readStmt->executeQuery();
0187 
0188     rset->next();
0189 
0190     // 1 is the id and 2 is the config tag and 3 is the version
0191 
0192     result->setId(rset->getInt(1));
0193     result->setConfigTag(rset->getString(2));
0194     result->setVersion(rset->getInt(3));
0195 
0196     result->setPedestalId(getInt(rset, 4));
0197     result->setDelayId(getInt(rset, 5));
0198     result->setWeightId(getInt(rset, 6));
0199     result->setBadXtId(getInt(rset, 7));
0200     result->setBadTTId(getInt(rset, 8));
0201     result->setTriggerBadXtId(getInt(rset, 9));
0202     result->setTriggerBadTTId(getInt(rset, 10));
0203     result->setComment(rset->getString(11));
0204 
0205   } catch (SQLException& e) {
0206     throw(std::runtime_error(std::string("ODFEDAQConfig::fetchData():  ") + e.getMessage()));
0207   }
0208 }
0209 
0210 int ODFEDAQConfig::fetchID() noexcept(false) {
0211   // Return from memory if available
0212   if (m_ID != 0) {
0213     return m_ID;
0214   }
0215 
0216   this->checkConnection();
0217 
0218   try {
0219     Statement* stmt = m_conn->createStatement();
0220     stmt->setSQL("SELECT config_id FROM " + getTable() + "WHERE  tag=:1 and version=:2 ");
0221 
0222     stmt->setString(1, getConfigTag());
0223     stmt->setInt(2, getVersion());
0224 
0225     ResultSet* rset = stmt->executeQuery();
0226 
0227     if (rset->next()) {
0228       m_ID = rset->getInt(1);
0229     } else {
0230       m_ID = 0;
0231     }
0232     m_conn->terminateStatement(stmt);
0233   } catch (SQLException& e) {
0234     throw(std::runtime_error(std::string("ODFEDAQConfig::fetchID:  ") + e.getMessage()));
0235   }
0236 
0237   return m_ID;
0238 }