CMS 3D CMS Logo

CaliIOV.cc
Go to the documentation of this file.
1 #include <stdexcept>
3 
8 
9 using namespace std;
10 using namespace oracle::occi;
11 
13  m_conn = nullptr;
14  m_ID = 0;
15  m_since = Tm();
16  m_till = Tm();
17 }
18 
20 
21 void CaliIOV::setSince(const Tm& since) {
22  if (since != m_since) {
23  m_ID = 0;
24  m_since = since;
25  }
26 }
27 
28 Tm CaliIOV::getSince() const { return m_since; }
29 
30 void CaliIOV::setTill(const Tm& till) {
31  if (till != m_till) {
32  m_ID = 0;
33  m_till = till;
34  }
35 }
36 
37 Tm CaliIOV::getTill() const { return m_till; }
38 
40  if (tag != m_caliTag) {
41  m_ID = 0;
42  m_caliTag = tag;
43  }
44 }
45 
46 CaliTag CaliIOV::getCaliTag() const { return m_caliTag; }
47 
49  // Return from memory if available
50  if (m_ID) {
51  return m_ID;
52  }
53 
54  this->checkConnection();
55 
56  m_caliTag.setConnection(m_env, m_conn);
57  int tagID = m_caliTag.fetchID();
58  if (!tagID) {
59  return 0;
60  }
61 
62  DateHandler dh(m_env, m_conn);
63 
64  if (m_till.isNull()) {
65  m_till = dh.getPlusInfTm();
66  }
67 
68  try {
69  Statement* stmt = m_conn->createStatement();
70  stmt->setSQL(
71  "SELECT iov_id FROM cali_iov "
72  "WHERE tag_id = :tag_id AND "
73  "since = :since AND "
74  "till = :till");
75  stmt->setInt(1, tagID);
76  stmt->setDate(2, dh.tmToDate(m_since));
77  stmt->setDate(3, dh.tmToDate(m_till));
78 
79  ResultSet* rset = stmt->executeQuery();
80 
81  if (rset->next()) {
82  m_ID = rset->getInt(1);
83  } else {
84  m_ID = 0;
85  }
86  m_conn->terminateStatement(stmt);
87  } catch (SQLException& e) {
88  throw(std::runtime_error("CaliIOV::fetchID: " + e.getMessage()));
89  }
90 
91  return m_ID;
92 }
93 
94 void CaliIOV::setByID(int id) noexcept(false) {
95  this->checkConnection();
96 
97  DateHandler dh(m_env, m_conn);
98 
99  try {
100  Statement* stmt = m_conn->createStatement();
101 
102  stmt->setSQL("SELECT tag_id, since, till FROM cali_iov WHERE iov_id = :1");
103  stmt->setInt(1, id);
104 
105  ResultSet* rset = stmt->executeQuery();
106  if (rset->next()) {
107  int tagID = rset->getInt(1);
108  Date since = rset->getDate(2);
109  Date till = rset->getDate(3);
110 
111  m_since = dh.dateToTm(since);
112  m_till = dh.dateToTm(till);
113 
114  m_caliTag.setConnection(m_env, m_conn);
115  m_caliTag.setByID(tagID);
116  m_ID = id;
117  } else {
118  throw(std::runtime_error("CaliTag::setByID: Given tag_id is not in the database"));
119  }
120 
121  m_conn->terminateStatement(stmt);
122  } catch (SQLException& e) {
123  throw(std::runtime_error("CaliTag::setByID: " + e.getMessage()));
124  }
125 }
126 
128  this->checkConnection();
129 
130  // Check if this IOV has already been written
131  if (this->fetchID()) {
132  return m_ID;
133  }
134 
135  m_caliTag.setConnection(m_env, m_conn);
136  int tagID = m_caliTag.writeDB();
137 
138  // Validate the data, use infinity-till convention
139  DateHandler dh(m_env, m_conn);
140 
141  if (m_since.isNull()) {
142  throw(std::runtime_error("CaliIOV::writeDB: Must setSince before writing"));
143  }
144 
145  if (m_till.isNull()) {
146  m_till = dh.getPlusInfTm();
147  }
148 
149  try {
150  Statement* stmt = m_conn->createStatement();
151 
152  stmt->setSQL(
153  "INSERT INTO cali_iov (iov_id, tag_id, since, till) "
154  "VALUES (cali_iov_sq.NextVal, :1, :2, :3)");
155  stmt->setInt(1, tagID);
156  stmt->setDate(2, dh.tmToDate(m_since));
157  stmt->setDate(3, dh.tmToDate(m_till));
158 
159  stmt->executeUpdate();
160 
161  m_conn->terminateStatement(stmt);
162  } catch (SQLException& e) {
163  throw(std::runtime_error("CaliIOV::writeDB: " + e.getMessage()));
164  }
165 
166  // Now get the ID
167  if (!this->fetchID()) {
168  throw(std::runtime_error("CaliIOV::writeDB: Failed to write"));
169  }
170 
171  return m_ID;
172 }
173 
174 void CaliIOV::setByTm(CaliTag* tag, const Tm& eventTm) noexcept(false) {
175  this->checkConnection();
176 
177  tag->setConnection(m_env, m_conn);
178  int tagID = tag->fetchID();
179 
180  if (!tagID) {
181  throw(std::runtime_error("CaliIOV::setByTm: Given CaliTag does not exist in the DB"));
182  }
183 
184  DateHandler dh(m_env, m_conn);
185 
186  Date eventDate = dh.tmToDate(eventTm);
187 
188  try {
189  Statement* stmt = m_conn->createStatement();
190 
191  stmt->setSQL(
192  "SELECT iov_id, since, till FROM cali_iov "
193  "WHERE tag_id = :1 AND since <= :2 AND till > :3");
194  stmt->setInt(1, tagID);
195  stmt->setDate(2, eventDate);
196  stmt->setDate(3, eventDate);
197 
198  ResultSet* rset = stmt->executeQuery();
199  if (rset->next()) {
200  m_caliTag = *tag;
201 
202  m_ID = rset->getInt(1);
203  Date sinceDate = rset->getDate(2);
204  Date tillDate = rset->getDate(3);
205 
206  m_since = dh.dateToTm(sinceDate);
207  m_till = dh.dateToTm(tillDate);
208  } else {
209  throw(std::runtime_error("CaliIOV::setByTm: Given subrun is not in the database"));
210  }
211 
212  m_conn->terminateStatement(stmt);
213  } catch (SQLException& e) {
214  throw(std::runtime_error("CaliIOV::setByTm: " + e.getMessage()));
215  }
216 }
Tm getSince() const
Definition: CaliIOV.cc:28
int writeDB() noexcept(false)
Definition: CaliIOV.cc:127
void setSince(const Tm &since)
Definition: CaliIOV.cc:21
void setByID(int id) noexcept(false) override
Definition: CaliIOV.cc:94
CaliIOV()
Definition: CaliIOV.cc:12
CaliTag getCaliTag() const
Definition: CaliIOV.cc:46
oracle::occi::Date tmToDate(const Tm &inTm) const
Definition: DateHandler.cc:19
~CaliIOV() override
Definition: CaliIOV.cc:19
Tm getPlusInfTm() const
Definition: DateHandler.h:13
#define noexcept
void setCaliTag(const CaliTag &tag)
Definition: CaliIOV.cc:39
void setByTm(CaliTag *tag, const Tm &time) noexcept(false)
Definition: CaliIOV.cc:174
Tm getTill() const
Definition: CaliIOV.cc:37
int fetchID() noexcept(false) override
Definition: CaliIOV.cc:48
dh
Definition: cuy.py:355
Tm dateToTm(oracle::occi::Date &date) const
Definition: DateHandler.cc:28
Definition: Tm.h:13
void setTill(const Tm &till)
Definition: CaliIOV.cc:30