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