Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "OnlineDB/EcalCondDB/interface/LMFLmrSubIOV.h"
0002 
0003 #include <ctime>
0004 #include <string>
0005 
0006 void LMFLmrSubIOV::init() {
0007   m_className = "LMFLmrSubIOV";
0008 
0009   m_lmfIOV = 0;
0010   m_t[0] = Tm();
0011   m_t[1] = Tm();
0012   m_t[2] = Tm();
0013 }
0014 
0015 LMFLmrSubIOV::LMFLmrSubIOV() { init(); }
0016 
0017 LMFLmrSubIOV::LMFLmrSubIOV(EcalDBConnection *c) : LMFUnique(c) { init(); }
0018 
0019 LMFLmrSubIOV::LMFLmrSubIOV(oracle::occi::Environment *env, oracle::occi::Connection *conn) : LMFUnique(env, conn) {
0020   init();
0021 }
0022 
0023 LMFLmrSubIOV::~LMFLmrSubIOV() {}
0024 
0025 LMFLmrSubIOV &LMFLmrSubIOV::setLMFIOV(const LMFIOV &iov) {
0026   if (m_debug) {
0027     std::cout << "[LMFLmrSubIOV] Setting IOV_ID as " << iov.getID() << std::endl << std::flush;
0028   }
0029   m_lmfIOV = iov.getID();
0030   return *this;
0031 }
0032 
0033 LMFLmrSubIOV &LMFLmrSubIOV::setTimes(Tm *t) {
0034   m_t[0] = t[0];
0035   m_t[1] = t[1];
0036   m_t[2] = t[2];
0037   return *this;
0038 }
0039 
0040 LMFLmrSubIOV &LMFLmrSubIOV::setTimes(const Tm &t1, const Tm &t2, const Tm &t3) {
0041   m_t[0] = t1;
0042   m_t[1] = t2;
0043   m_t[2] = t3;
0044   return *this;
0045 }
0046 
0047 std::string LMFLmrSubIOV::fetchIdSql(Statement *stmt) {
0048   if (!m_lmfIOV) {
0049     if (m_debug) {
0050       std::cout << m_className << ": LMFIOV not set" << std::endl;
0051     }
0052     return "";
0053   }
0054 
0055   std::string sql =
0056       "SELECT LMR_SUB_IOV_ID FROM "
0057       "CMS_ECAL_LASER_COND.LMF_LMR_SUB_IOV "
0058       "WHERE "
0059       "IOV_ID  = :1 AND "
0060       "T1      = :2 AND "
0061       "T2      = :3 AND "
0062       "T3      = :4";
0063   stmt->setSQL(sql);
0064   stmt->setInt(1, m_lmfIOV);
0065   DateHandler dh(m_env, m_conn);
0066   for (int i = 0; i < 3; i++) {
0067     oracle::occi::Date t = dh.tmToDate(m_t[i]);
0068     stmt->setDate(i + 2, t);
0069   }
0070   return sql;
0071 }
0072 
0073 std::string LMFLmrSubIOV::setByIDSql(Statement *stmt, int id) {
0074   std::string sql =
0075       "SELECT IOV_ID, T1, T2, T3 FROM "
0076       "CMS_ECAL_LASER_COND.LMF_LMR_SUB_IOV "
0077       "WHERE LMR_SUB_IOV_ID = :1";
0078   stmt->setSQL(sql);
0079   stmt->setInt(1, id);
0080   return sql;
0081 }
0082 
0083 std::string LMFLmrSubIOV::writeDBSql(Statement *stmt) {
0084   // Validate the data, use infinity-till convention
0085   DateHandler dh(m_env, m_conn);
0086 
0087   for (int i = 0; i < 3; i++) {
0088     if (m_t[i].isNull()) {
0089       m_t[i] = dh.getPlusInfTm();
0090     }
0091   }
0092 
0093   if (m_lmfIOV == 0) {
0094     throw(std::runtime_error(m_className + "::writeDB: LMFIOV not set"));
0095   }
0096   std::string sp = sequencePostfix(m_t[0]);
0097   std::string sql =
0098       "INSERT INTO LMF_LMR_SUB_IOV (LMR_SUB_IOV_ID, "
0099       "IOV_ID, T1, T2, T3) "
0100       "VALUES (LMF_LMR_SUB_IOV_ID_" +
0101       sp + "_SQ.NextVal, :1, :2, :3, :4)";
0102   stmt->setSQL(sql);
0103   stmt->setInt(1, m_lmfIOV);
0104   for (int i = 0; i < 3; i++) {
0105     stmt->setDate(i + 2, dh.tmToDate(m_t[i]));
0106   }
0107   return sql;
0108 }
0109 
0110 void LMFLmrSubIOV::getParameters(ResultSet *rset) noexcept(false) {
0111   m_lmfIOV = rset->getInt(1);
0112   for (int i = 0; i < 3; i++) {
0113     oracle::occi::Date t = rset->getDate(i + 2);
0114 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
0115     m_t[i].setToString(t.toText("YYYY-MM-DD HH24:MI:SS"));
0116 #else
0117     int year = 0;
0118     unsigned int month = 0;
0119     unsigned int day = 0;
0120     unsigned int hour = 0;
0121     unsigned int min = 0;
0122     unsigned int seconds = 0;
0123     t.getDate(year, month, day, hour, min, seconds);
0124     const std::tm tt = {.tm_sec = static_cast<int>(seconds),
0125                         .tm_min = static_cast<int>(min),
0126                         .tm_hour = static_cast<int>(hour),
0127                         .tm_mday = static_cast<int>(day),
0128                         .tm_mon = static_cast<int>(month),
0129                         .tm_year = year - 1900,
0130                         .tm_wday = 0,
0131                         .tm_yday = 0,
0132                         .tm_isdst = 0,
0133                         .tm_gmtoff = 0,
0134                         .tm_zone = nullptr};
0135     char tt_str[30] = {0};
0136     if (std::strftime(tt_str, sizeof(tt_str), "%F %T", &tt)) {
0137       m_t[i].setToString(std::string(tt_str));
0138     } else {
0139       throw std::runtime_error("LMFLmrSubIOV::writeDBSql: failed to generate the date string");
0140     }
0141 #endif
0142   }
0143 }
0144 
0145 std::list<int> LMFLmrSubIOV::getIOVIDsLaterThan(const Tm &t) noexcept(false) {
0146   Tm tinf;
0147   tinf.setToString("9999-12-31 23:59:59");
0148   return getIOVIDsLaterThan(t, tinf, 0);
0149 }
0150 
0151 std::list<int> LMFLmrSubIOV::getIOVIDsLaterThan(const Tm &t, int howmany) noexcept(false) {
0152   Tm tinf;
0153   tinf.setToString("9999-12-31 23:59:59");
0154   return getIOVIDsLaterThan(t, tinf, howmany);
0155 }
0156 
0157 std::list<int> LMFLmrSubIOV::getIOVIDsLaterThan(const Tm &tmin, const Tm &tmax) noexcept(false) {
0158   return getIOVIDsLaterThan(tmin, tmax, 0);
0159 }
0160 
0161 std::list<int> LMFLmrSubIOV::getIOVIDsLaterThan(const Tm &tmin, const Tm &tmax, int howMany) noexcept(false) {
0162   Tm tinf;
0163   tinf.setToString("9999-12-31 23:59:59");
0164   std::string sql =
0165       "SELECT * FROM (SELECT LMR_SUB_IOV_ID "
0166       "FROM CMS_ECAL_LASER_COND.LMF_LMR_SUB_IOV WHERE T3 > :1 ";
0167   if (tmax != tinf) {
0168     sql += "AND T3 < :2 ORDER BY T3 ASC) ";
0169     if (howMany > 0) {
0170       sql += "WHERE ROWNUM <= :3";
0171     }
0172   } else {
0173     sql += "ORDER BY T3 ASC) ";
0174     if (howMany > 0) {
0175       sql += "WHERE ROWNUM <= :2";
0176     }
0177   }
0178   if (m_debug) {
0179     std::cout << "Executing query: " << std::endl << sql << std::endl;
0180   }
0181   std::list<int> ret;
0182   if (m_conn != nullptr) {
0183     try {
0184       DateHandler dh(m_env, m_conn);
0185       Statement *stmt = m_conn->createStatement();
0186       stmt->setPrefetchRowCount(10000);
0187       stmt->setSQL(sql);
0188       stmt->setDate(1, dh.tmToDate(tmin));
0189       if (tmax != tinf) {
0190         stmt->setDate(2, dh.tmToDate(tmax));
0191         if (howMany > 0) {
0192           stmt->setInt(3, howMany);
0193         }
0194       } else {
0195         if (howMany > 0) {
0196           stmt->setInt(2, howMany);
0197         }
0198       }
0199       ResultSet *rset = stmt->executeQuery();
0200       int row = 1;
0201       while (rset->next() != 0) {
0202         if (m_debug) {
0203           std::cout << "Getting row " << row++ << std::endl;
0204         }
0205         ret.push_back(rset->getInt(1));
0206       }
0207       stmt->setPrefetchRowCount(0);
0208       m_conn->terminateStatement(stmt);
0209     } catch (oracle::occi::SQLException &e) {
0210 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
0211       throw(std::runtime_error(m_className + "::getLmrSubIOVLaterThan: " + e.getMessage()));
0212 #else
0213       throw(
0214           std::runtime_error(m_className + "::getLmrSubIOVLaterThan: error code " + std::to_string(e.getErrorCode())));
0215 #endif
0216     }
0217   } else {
0218     throw(std::runtime_error(m_className + "::getLmrSubIOVLaterThan: " + "Connection not set"));
0219   }
0220   if (m_debug) {
0221     std::cout << "Sorting..." << std::flush;
0222   }
0223   ret.sort();
0224   if (m_debug) {
0225     std::cout << "Done!" << std::endl << std::flush;
0226   }
0227   return ret;
0228 }