Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include <stdexcept>
0002 #include <string>
0003 #include <cstring>
0004 #include <cstdlib>
0005 
0006 #include "OnlineDB/Oracle/interface/Oracle.h"
0007 
0008 #include "OnlineDB/EcalCondDB/interface/ODCCSConfig.h"
0009 
0010 #include <limits>
0011 
0012 #define MY_NULL 16777215  // numeric_limits<int>::quiet_NaN()
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 using namespace std;
0027 using namespace oracle::occi;
0028 
0029 ODCCSConfig::ODCCSConfig() {
0030   m_env = nullptr;
0031   m_conn = nullptr;
0032   m_writeStmt = nullptr;
0033   m_readStmt = nullptr;
0034   m_config_tag = "";
0035   m_ID = 0;
0036   clear();
0037 }
0038 
0039 void ODCCSConfig::clear() {
0040   m_daccal = MY_NULL;
0041   m_delay = MY_NULL;
0042   m_gain = "";
0043   m_memgain = "";
0044   m_offset_high = MY_NULL;
0045   m_offset_low = MY_NULL;
0046   m_offset_mid = MY_NULL;
0047   m_trg_mode = "";
0048   m_trg_filter = "";
0049   m_bgo = "";
0050   m_tts_mask = MY_NULL;
0051   m_daq = MY_NULL;
0052   m_trg = MY_NULL;
0053   m_bc0 = MY_NULL;
0054   m_bc0_delay = MY_NULL;
0055   m_te_delay = MY_NULL;
0056 }
0057 
0058 ODCCSConfig::~ODCCSConfig() {}
0059 
0060 int ODCCSConfig::fetchNextId() noexcept(false) {
0061   int result = 0;
0062   try {
0063     this->checkConnection();
0064 
0065     m_readStmt = m_conn->createStatement();
0066     m_readStmt->setSQL("select ecal_CCS_config_sq.NextVal from dual");
0067     ResultSet* rset = m_readStmt->executeQuery();
0068     while (rset->next()) {
0069       result = rset->getInt(1);
0070     }
0071     m_conn->terminateStatement(m_readStmt);
0072     return result;
0073 
0074   } catch (SQLException& e) {
0075     throw(std::runtime_error(std::string("ODCCSConfig::fetchNextId():  ") + e.getMessage()));
0076   }
0077 }
0078 
0079 void ODCCSConfig::prepareWrite() noexcept(false) {
0080   this->checkConnection();
0081   int next_id = fetchNextId();
0082 
0083   try {
0084     m_writeStmt = m_conn->createStatement();
0085     m_writeStmt->setSQL(
0086         "INSERT INTO ECAL_CCS_CONFIGURATION ( ccs_configuration_id, ccs_tag ,"
0087         " daccal, delay, gain, memgain, offset_high,offset_low,offset_mid, trg_mode, trg_filter, "
0088         " clock, BGO_SOURCE, TTS_MASK, DAQ_BCID_PRESET , TRIG_BCID_PRESET, BC0_COUNTER, BC0_DELAY, TE_DELAY ) "
0089         "VALUES (  "
0090         " :ccs_configuration_id, :ccs_tag,  :daccal, :delay, :gain, :memgain, :offset_high,:offset_low,"
0091         " :offset_mid, :trg_mode, :trg_filter, :clock, :BGO_SOURCE, :TTS_MASK, "
0092         " :DAQ_BCID_PRESET , :TRIG_BCID_PRESET, :BC0_COUNTER, :BC0_DELAY, :TE_DELAY) ");
0093 
0094     m_writeStmt->setInt(1, next_id);
0095     m_ID = next_id;
0096 
0097   } catch (SQLException& e) {
0098     throw(std::runtime_error(std::string("ODCCSConfig::prepareWrite():  ") + e.getMessage()));
0099   }
0100 }
0101 
0102 void ODCCSConfig::setParameters(const std::map<string, string>& my_keys_map) {
0103   // parses the result of the XML parser that is a map of
0104   // string string with variable name variable value
0105 
0106   for (std::map<std::string, std::string>::const_iterator ci = my_keys_map.begin(); ci != my_keys_map.end(); ci++) {
0107     if (ci->first == "CCS_CONFIGURATION_ID")
0108       setConfigTag(ci->second);
0109     else if (ci->first == "DACCAL")
0110       setDaccal(atoi(ci->second.c_str()));
0111     else if (ci->first == "GAIN")
0112       setGain(ci->second);
0113     else if (ci->first == "MEMGAIN")
0114       setMemGain(ci->second);
0115     else if (ci->first == "OFFSET_HIGH")
0116       setOffsetHigh(atoi(ci->second.c_str()));
0117     else if (ci->first == "OFFSET_LOW")
0118       setOffsetLow(atoi(ci->second.c_str()));
0119     else if (ci->first == "OFFSET_MID")
0120       setOffsetMid(atoi(ci->second.c_str()));
0121     else if (ci->first == "TRG_MODE")
0122       setTrgMode(ci->second);
0123     else if (ci->first == "TRG_FILTER")
0124       setTrgFilter(ci->second);
0125     else if (ci->first == "CLOCK")
0126       setClock(atoi(ci->second.c_str()));
0127     else if (ci->first == "BGO_SOURCE")
0128       setBGOSource(ci->second);
0129     else if (ci->first == "TTS_MASK")
0130       setTTSMask(atoi(ci->second.c_str()));
0131     else if (ci->first == "DAQ_BCID_PRESET")
0132       setDAQBCIDPreset(atoi(ci->second.c_str()));
0133     else if (ci->first == "TRIG_BCID_PRESET")
0134       setTrgBCIDPreset(atoi(ci->second.c_str()));
0135     else if (ci->first == "BC0_COUNTER")
0136       setBC0Counter(atoi(ci->second.c_str()));
0137     else if (ci->first == "BC0_DELAY")
0138       setBC0Delay(atoi(ci->second.c_str()));
0139     else if (ci->first == "TE_DELAY")
0140       setTEDelay(atoi(ci->second.c_str()));
0141     else if (ci->first == "DELAY")
0142       setDelay(atoi(ci->second.c_str()));
0143   }
0144 }
0145 
0146 void ODCCSConfig::writeDB() noexcept(false) {
0147   this->checkConnection();
0148   this->checkPrepare();
0149 
0150   try {
0151     // number 1 is the id
0152     // m_writeStmt->setString(2, this->getConfigTag());
0153     // m_writeStmt->setInt(3, this->getDaccal());
0154     // m_writeStmt->setInt(4, this->getDelay());
0155     // m_writeStmt->setString(5, this->getGain());
0156     // m_writeStmt->setString(6, this->getMemGain());
0157     // m_writeStmt->setInt(7, this->getOffsetHigh());
0158     // m_writeStmt->setInt(8, this->getOffsetLow());
0159     // m_writeStmt->setInt(9, this->getOffsetMid());
0160     // m_writeStmt->setString(10, this->getTrgMode() );
0161     // m_writeStmt->setString(11, this->getTrgFilter() );
0162     // m_writeStmt->setInt(  12, this->getClock() );
0163     // m_writeStmt->setString(13, this->getBGOSource() );
0164     // m_writeStmt->setInt(14, this->getTTSMask() );
0165     // m_writeStmt->setInt(15, this->getDAQBCIDPreset() );
0166     // m_writeStmt->setInt(16, this->getTrgBCIDPreset() );
0167     // m_writeStmt->setInt(17, this->getBC0Counter() );
0168 
0169     SET_STRING(m_writeStmt, 2, this->getConfigTag());
0170     SET_INT(m_writeStmt, 3, this->getDaccal());
0171     SET_INT(m_writeStmt, 4, this->getDelay());
0172     SET_STRING(m_writeStmt, 5, this->getGain());
0173     SET_STRING(m_writeStmt, 6, this->getMemGain());
0174     SET_INT(m_writeStmt, 7, this->getOffsetHigh());
0175     SET_INT(m_writeStmt, 8, this->getOffsetLow());
0176     SET_INT(m_writeStmt, 9, this->getOffsetMid());
0177     SET_STRING(m_writeStmt, 10, this->getTrgMode());
0178     SET_STRING(m_writeStmt, 11, this->getTrgFilter());
0179     SET_INT(m_writeStmt, 12, this->getClock());
0180     SET_STRING(m_writeStmt, 13, this->getBGOSource());
0181     SET_INT(m_writeStmt, 14, this->getTTSMask());
0182     SET_INT(m_writeStmt, 15, this->getDAQBCIDPreset());
0183     SET_INT(m_writeStmt, 16, this->getTrgBCIDPreset());
0184     SET_INT(m_writeStmt, 17, this->getBC0Counter());
0185     SET_INT(m_writeStmt, 18, this->getBC0Delay());
0186     SET_INT(m_writeStmt, 19, this->getTEDelay());
0187 
0188     m_writeStmt->executeUpdate();
0189 
0190   } catch (SQLException& e) {
0191     throw(std::runtime_error(std::string("ODCCSConfig::writeDB():  ") + e.getMessage()));
0192   }
0193   // Now get the ID
0194   if (!this->fetchID()) {
0195     throw(std::runtime_error("ODCCSConfig::writeDB:  Failed to write"));
0196   }
0197 }
0198 
0199 void ODCCSConfig::fetchData(ODCCSConfig* result) noexcept(false) {
0200   this->checkConnection();
0201   result->clear();
0202   if (result->getId() == 0 && (result->getConfigTag().empty())) {
0203     throw(std::runtime_error("ODCCSConfig::fetchData(): no Id defined for this ODCCSConfig "));
0204   }
0205 
0206   try {
0207     m_readStmt->setSQL(
0208         "SELECT * "
0209         "FROM ECAL_CCS_CONFIGURATION  "
0210         " where ( CCS_configuration_id = :1 or CCS_tag=:2 )");
0211     m_readStmt->setInt(1, result->getId());
0212     m_readStmt->setString(2, result->getConfigTag());
0213     ResultSet* rset = m_readStmt->executeQuery();
0214 
0215     rset->next();
0216 
0217     // 1 is the id and 2 is the config tag
0218 
0219     result->setId(rset->getInt(1));
0220     result->setConfigTag(rset->getString(2));
0221 
0222     result->setDaccal(rset->getInt(3));
0223     result->setDelay(rset->getInt(4));
0224     result->setGain(rset->getString(5));
0225     result->setMemGain(rset->getString(6));
0226     result->setOffsetHigh(rset->getInt(7));
0227     result->setOffsetLow(rset->getInt(8));
0228     result->setOffsetMid(rset->getInt(9));
0229     result->setTrgMode(rset->getString(10));
0230     result->setTrgFilter(rset->getString(11));
0231     result->setClock(rset->getInt(12));
0232     result->setBGOSource(rset->getString(13));
0233     result->setTTSMask(rset->getInt(14));
0234     result->setDAQBCIDPreset(rset->getInt(15));
0235     result->setTrgBCIDPreset(rset->getInt(16));
0236     result->setBC0Counter(rset->getInt(17));
0237 
0238   } catch (SQLException& e) {
0239     throw(std::runtime_error(std::string("ODCCSConfig::fetchData():  ") + e.getMessage()));
0240   }
0241 }
0242 
0243 int ODCCSConfig::fetchID() noexcept(false) {
0244   // Return from memory if available
0245   if (m_ID != 0) {
0246     return m_ID;
0247   }
0248 
0249   this->checkConnection();
0250 
0251   try {
0252     Statement* stmt = m_conn->createStatement();
0253     stmt->setSQL(
0254         "SELECT ccs_configuration_id FROM ecal_ccs_configuration "
0255         "WHERE  ccs_tag=:ccs_tag ");
0256 
0257     stmt->setString(1, getConfigTag());
0258 
0259     ResultSet* rset = stmt->executeQuery();
0260 
0261     if (rset->next()) {
0262       m_ID = rset->getInt(1);
0263     } else {
0264       m_ID = 0;
0265     }
0266     m_conn->terminateStatement(stmt);
0267   } catch (SQLException& e) {
0268     throw(std::runtime_error(std::string("ODCCSConfig::fetchID:  ") + e.getMessage()));
0269   }
0270 
0271   return m_ID;
0272 }