CMS 3D CMS Logo

LMFLmrSubIOV.cc
Go to the documentation of this file.
2 
3 #include <ctime>
4 #include <string>
5 
7  m_className = "LMFLmrSubIOV";
8 
9  m_lmfIOV = 0;
10  m_t[0] = Tm();
11  m_t[1] = Tm();
12  m_t[2] = Tm();
13 }
14 
16  init();
17 }
18 
20  init();
21 }
22 
23 LMFLmrSubIOV::LMFLmrSubIOV(oracle::occi::Environment* env,
24  oracle::occi::Connection* conn): LMFUnique(env, conn)
25 {
26  init();
27 }
28 
30 }
31 
33  if (m_debug) {
34  std::cout << "[LMFLmrSubIOV] Setting IOV_ID as " << iov.getID()
35  << std::endl << std::flush;
36  }
37  m_lmfIOV = iov.getID();
38  return *this;
39 }
40 
42  m_t[0] = t[0];
43  m_t[1] = t[1];
44  m_t[2] = t[2];
45  return *this;
46 }
47 
48 LMFLmrSubIOV& LMFLmrSubIOV::setTimes(const Tm& t1, const Tm& t2, const Tm& t3) {
49  m_t[0] = t1;
50  m_t[1] = t2;
51  m_t[2] = t3;
52  return *this;
53 }
54 
56  if (!m_lmfIOV) {
57  if (m_debug) {
58  std::cout << m_className << ": LMFIOV not set" << std::endl;
59  }
60  return "";
61  }
62 
63  std::string sql = "SELECT LMR_SUB_IOV_ID FROM "
64  "CMS_ECAL_LASER_COND.LMF_LMR_SUB_IOV "
65  "WHERE "
66  "IOV_ID = :1 AND "
67  "T1 = :2 AND "
68  "T2 = :3 AND "
69  "T3 = :4";
70  stmt->setSQL(sql);
71  stmt->setInt(1, m_lmfIOV);
73  for (int i = 0; i < 3; i++) {
74  oracle::occi::Date t = dh.tmToDate(m_t[i]);
75  stmt->setDate(i + 2, t);
76  }
77  return sql;
78 
79 }
80 
82  std::string sql = "SELECT IOV_ID, T1, T2, T3 FROM "
83  "CMS_ECAL_LASER_COND.LMF_LMR_SUB_IOV "
84  "WHERE LMR_SUB_IOV_ID = :1";
85  stmt->setSQL(sql);
86  stmt->setInt(1, id);
87  return sql;
88 }
89 
91  // Validate the data, use infinity-till convention
93 
94  for (int i = 0; i < 3; i++) {
95  if (m_t[i].isNull()) {
96  m_t[i] = dh.getPlusInfTm();
97  }
98  }
99 
100  if (m_lmfIOV == 0) {
101  throw(std::runtime_error(m_className + "::writeDB: LMFIOV not set"));
102  }
104  std::string sql = "INSERT INTO LMF_LMR_SUB_IOV (LMR_SUB_IOV_ID, "
105  "IOV_ID, T1, T2, T3) "
106  "VALUES (LMF_LMR_SUB_IOV_ID_" + sp + "_SQ.NextVal, :1, :2, :3, :4)";
107  stmt->setSQL(sql);
108  stmt->setInt(1, m_lmfIOV);
109  for (int i = 0; i < 3; i++) {
110  stmt->setDate(i + 2, dh.tmToDate(m_t[i]));
111  }
112  return sql;
113 }
114 
116  m_lmfIOV = rset->getInt(1);
117  for (int i = 0; i < 3; i++) {
118  oracle::occi::Date t = rset->getDate(i + 2);
119 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
120  m_t[i].setToString(t.toText("YYYY-MM-DD HH24:MI:SS"));
121 #else
122  int year = 0;
123  unsigned int month = 0;
124  unsigned int day = 0;
125  unsigned int hour = 0;
126  unsigned int min = 0;
127  unsigned int seconds = 0;
128  t.getDate(year, month, day, hour, min, seconds);
129  const std::tm tt = {
130  .tm_sec = static_cast<int>(seconds),
131  .tm_min = static_cast<int>(min),
132  .tm_hour = static_cast<int>(hour),
133  .tm_mday = static_cast<int>(day),
134  .tm_mon = static_cast<int>(month),
135  .tm_year = year - 1900
136  };
137  char tt_str[30] = { 0 };
138  if (std::strftime(tt_str, sizeof(tt_str), "%F %T", &tt)) {
139  m_t[i].setToString(std::string(tt_str));
140  } else {
141  throw std::runtime_error("LMFLmrSubIOV::writeDBSql: failed to generate the date string");
142  }
143 #endif
144  }
145 }
146 
147 std::list<int> LMFLmrSubIOV::getIOVIDsLaterThan(const Tm &t)
148  noexcept(false) {
149  Tm tinf;
150  tinf.setToString("9999-12-31 23:59:59");
151  return getIOVIDsLaterThan(t, tinf, 0);
152 }
153 
154 std::list<int> LMFLmrSubIOV::getIOVIDsLaterThan(const Tm &t, int howmany)
155  noexcept(false) {
156  Tm tinf;
157  tinf.setToString("9999-12-31 23:59:59");
158  return getIOVIDsLaterThan(t, tinf, howmany);
159 }
160 
161 std::list<int> LMFLmrSubIOV::getIOVIDsLaterThan(const Tm &tmin,
162  const Tm &tmax)
163  noexcept(false) {
164  return getIOVIDsLaterThan(tmin, tmax, 0);
165 }
166 
167 std::list<int> LMFLmrSubIOV::getIOVIDsLaterThan(const Tm &tmin, const Tm &tmax,
168  int howMany)
169  noexcept(false) {
170  Tm tinf;
171  tinf.setToString("9999-12-31 23:59:59");
172  std::string sql = "SELECT * FROM (SELECT LMR_SUB_IOV_ID "
173  "FROM CMS_ECAL_LASER_COND.LMF_LMR_SUB_IOV WHERE T3 > :1 ";
174  if (tmax != tinf) {
175  sql += "AND T3 < :2 ORDER BY T3 ASC) ";
176  if (howMany > 0) {
177  sql += "WHERE ROWNUM <= :3";
178  }
179  } else {
180  sql += "ORDER BY T3 ASC) ";
181  if (howMany > 0) {
182  sql += "WHERE ROWNUM <= :2";
183  }
184  }
185  if (m_debug) {
186  std::cout << "Executing query: " << std::endl << sql << std::endl;
187  }
188  std::list<int> ret;
189  if (m_conn != nullptr) {
190  try {
192  Statement *stmt = m_conn->createStatement();
193  stmt->setPrefetchRowCount(10000);
194  stmt->setSQL(sql);
195  stmt->setDate(1, dh.tmToDate(tmin));
196  if (tmax != tinf) {
197  stmt->setDate(2, dh.tmToDate(tmax));
198  if (howMany > 0) {
199  stmt->setInt(3, howMany);
200  }
201  } else {
202  if (howMany > 0) {
203  stmt->setInt(2, howMany);
204  }
205  }
206  ResultSet *rset = stmt->executeQuery();
207  int row = 1;
208  while (rset->next() != 0) {
209  if (m_debug) {
210  std::cout << "Getting row " << row++ << std::endl;
211  }
212  ret.push_back(rset->getInt(1));
213  }
214  stmt->setPrefetchRowCount(0);
215  m_conn->terminateStatement(stmt);
216  }
217  catch (oracle::occi::SQLException &e) {
218 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
219  throw(std::runtime_error(m_className + "::getLmrSubIOVLaterThan: " +
220  e.getMessage()));
221 #else
222  throw(std::runtime_error(m_className + "::getLmrSubIOVLaterThan: error code " +
223  std::to_string(e.getErrorCode())));
224 #endif
225  }
226  } else {
227  throw(std::runtime_error(m_className + "::getLmrSubIOVLaterThan: " +
228  "Connection not set"));
229  }
230  if (m_debug) {
231  std::cout << "Sorting..." << std::flush;
232  }
233  ret.sort();
234  if (m_debug) {
235  std::cout << "Done!" << std::endl << std::flush;
236  }
237  return ret;
238 }
239 
240 
std::string m_className
Definition: LMFUnique.h:98
std::list< int > getIOVIDsLaterThan(const Tm &t) noexcept(false)
oracle::occi::Environment * m_env
Definition: IDBObject.h:38
oracle::occi::Connection * m_conn
Definition: IDBObject.h:39
int getID() const
Definition: LMFUnique.h:52
double seconds()
std::string sequencePostfix(const Tm &t)
Definition: LMFUnique.cc:10
oracle::occi::ResultSet ResultSet
Definition: LMFUnique.h:19
std::string setByIDSql(Statement *stmt, int id) override
Definition: LMFLmrSubIOV.cc:81
LMFLmrSubIOV & setTimes(const Tm &t1, const Tm &t2, const Tm &t3)
Definition: LMFLmrSubIOV.cc:48
oracle::occi::Statement Statement
Definition: LMFUnique.h:20
std::string writeDBSql(Statement *stmt) override
Definition: LMFLmrSubIOV.cc:90
oracle::occi::Date tmToDate(const Tm &inTm) const
Definition: DateHandler.cc:20
T min(T a, T b)
Definition: MathUtil.h:58
Tm getPlusInfTm() const
Definition: DateHandler.h:15
Definition: LMFIOV.h:17
#define noexcept
static const double tmax[3]
void setToString(const std::string s) noexcept(false)
Definition: Tm.cc:193
def day(string)
Definition: getRunInfo.py:11
char m_debug
Definition: LMFUnique.h:99
dh
Definition: cuy.py:355
Definition: Tm.h:13
std::string fetchIdSql(Statement *stmt) override
Definition: LMFLmrSubIOV.cc:55
void getParameters(ResultSet *rset) override
LMFLmrSubIOV & setLMFIOV(const LMFIOV &iov)
Definition: LMFLmrSubIOV.cc:32
~LMFLmrSubIOV() override
Definition: LMFLmrSubIOV.cc:29