CMS 3D CMS Logo

MODRunIOV.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 = NULL;
15  m_ID = 0;
16  m_runIOV = RunIOV();
17  m_subRunNum = 0;
18  m_subRunStart = Tm();
19  m_subRunEnd = Tm();
20 }
21 
22 
23 
25 {
26 }
27 
28 
29 void MODRunIOV::setID(int id)
30 {
31  m_ID = id;
32 }
33 
34 
35 
36 void MODRunIOV::setRunIOV(const RunIOV& iov)
37 {
38  if (iov != m_runIOV) {
39  m_ID = 0;
40  m_runIOV = iov;
41  }
42 }
43 
45 {
46  return m_runIOV;
47 }
48 
49 
51 {
52  if (subrun != m_subRunNum) {
53  m_ID = 0;
54  m_subRunNum = subrun;
55  }
56 }
57 
58 
59 
61 {
62  return m_subRunNum;
63 }
64 
65 
66 
68 {
69  if (start != m_subRunStart) {
70  m_ID = 0;
71  m_subRunStart = start;
72  }
73 }
74 
75 
76 
78 {
79  return m_subRunStart;
80 }
81 
82 
83 
85 {
86  if (end != m_subRunEnd) {
87  m_ID = 0;
88  m_subRunEnd = end;
89  }
90 }
91 
92 
93 
95 {
96  return m_subRunEnd;
97 }
98 
99 
100 
102  noexcept(false)
103 {
104  // Return from memory if available
105  if (m_ID) {
106  return m_ID;
107  }
108 
109  this->checkConnection();
110 
111  // fetch the parent IDs
112  int runIOVID;
113  this->fetchParentIDs(&runIOVID);
114 
115  if (!runIOVID) {
116  return 0;
117  }
118 
119  DateHandler dh(m_env, m_conn);
120 
121  if (m_subRunEnd.isNull()) {
122  m_subRunEnd = dh.getPlusInfTm();
123  }
124 
125  try {
126  Statement* stmt = m_conn->createStatement();
127  stmt->setSQL("SELECT iov_id FROM OD_run_iov "
128  "WHERE "
129  "run_iov_id = :1 AND "
130  "subrun_num = :2 AND "
131  "subrun_start = :3 AND "
132  "subrun_end = :4");
133 
134  stmt->setInt(1, runIOVID);
135  stmt->setInt(2, m_subRunNum);
136  stmt->setDate(3, dh.tmToDate(m_subRunStart));
137  stmt->setDate(4, dh.tmToDate(m_subRunEnd));
138 
139  ResultSet* rset = stmt->executeQuery();
140 
141  if (rset->next()) {
142  m_ID = rset->getInt(1);
143  } else {
144  m_ID = 0;
145  }
146  m_conn->terminateStatement(stmt);
147  } catch (SQLException &e) {
148  throw(std::runtime_error("MODRunIOV::fetchID: "+e.getMessage()));
149  }
150 
151  return m_ID;
152 }
153 
154 
155 
156 void MODRunIOV::setByID(int id)
157  noexcept(false)
158 {
159  this->checkConnection();
160 
161  DateHandler dh(m_env, m_conn);
162 
163  try {
164  Statement* stmt = m_conn->createStatement();
165 
166  stmt->setSQL("SELECT run_iov_id, subrun_num, subrun_start, subrun_end FROM OD_run_iov WHERE iov_id = :1");
167  stmt->setInt(1, id);
168 
169  ResultSet* rset = stmt->executeQuery();
170  if (rset->next()) {
171  int runIOVID = rset->getInt(1);
172  m_subRunNum = rset->getInt(2);
173  Date startDate = rset->getDate(3);
174  Date endDate = rset->getDate(4);
175 
176  m_subRunStart = dh.dateToTm( startDate );
177  m_subRunEnd = dh.dateToTm( endDate );
178 
179  m_runIOV.setConnection(m_env, m_conn);
180  m_runIOV.setByID(runIOVID);
181 
182  m_ID = id;
183  } else {
184  throw(std::runtime_error("MODRunIOV::setByID: Given id is not in the database"));
185  }
186 
187  m_conn->terminateStatement(stmt);
188  } catch (SQLException &e) {
189  throw(std::runtime_error("MODRunIOV::setByID: "+e.getMessage()));
190  }
191 }
192 
193 
194 
196  noexcept(false)
197 {
198  this->checkConnection();
199 
200  // Check if this IOV has already been written
201  if (this->fetchID()) {
202  return m_ID;
203  }
204 
205  // fetch Parent IDs
206  int runIOVID;
207  this->fetchParentIDs(&runIOVID);
208 
209  // Validate the data, use infinity-till convention
210  DateHandler dh(m_env, m_conn);
211 
212  if (m_subRunStart.isNull()) {
213  throw(std::runtime_error("MODRunIOV::writeDB: Must setSubRunStart before writing"));
214  }
215 
216  if (m_subRunEnd.isNull()) {
217  m_subRunEnd = dh.getPlusInfTm();
218  }
219 
220  try {
221  Statement* stmt = m_conn->createStatement();
222 
223  stmt->setSQL("INSERT INTO od_run_iov (iov_id, run_iov_id, subrun_num, subrun_start, subrun_end) "
224  "VALUES (OD_run_iov_sq.NextVal, :1, :2, :3, :4)");
225  stmt->setInt(1, runIOVID);
226  stmt->setInt(2, m_subRunNum);
227  stmt->setDate(3, dh.tmToDate(m_subRunStart));
228  stmt->setDate(4, dh.tmToDate(m_subRunEnd));
229 
230  stmt->executeUpdate();
231 
232  m_conn->terminateStatement(stmt);
233  } catch (SQLException &e) {
234  throw(std::runtime_error("MODRunIOV::writeDB: "+e.getMessage()));
235  }
236 
237  // Now get the ID
238  if (!this->fetchID()) {
239  throw(std::runtime_error("MODRunIOV::writeDB: Failed to write"));
240  }
241 
242  return m_ID;
243 }
244 
245 
246 
247 void MODRunIOV::fetchParentIDs( int* runIOVID)
248  noexcept(false)
249 {
250  // get the RunIOV
251  m_runIOV.setConnection(m_env, m_conn);
252  *runIOVID = m_runIOV.fetchID();
253 
254  if (! *runIOVID) {
255  throw(std::runtime_error("MODRunIOV: Given RunIOV does not exist in DB"));
256  }
257 
258 }
259 
260 
261 
262 void MODRunIOV::setByRun( RunIOV* runiov, subrun_t subrun)
263  noexcept(false)
264 {
265  this->checkConnection();
266 
267  runiov->setConnection(m_env, m_conn);
268  int runIOVID = runiov->fetchID();
269 
270  if (!runIOVID) {
271  throw(std::runtime_error("MODRunIOV::setByRun: Given RunIOV does not exist in DB"));
272  }
273 
274  DateHandler dh(m_env, m_conn);
275 
276  try {
277  Statement* stmt = m_conn->createStatement();
278 
279  stmt->setSQL("SELECT iov_id, subrun_start, subrun_end FROM OD_run_iov "
280  "WHERE run_iov_id = :1 AND subrun_num = :2");
281  stmt->setInt(1, runIOVID);
282  stmt->setInt(2, subrun);
283 
284  ResultSet* rset = stmt->executeQuery();
285  if (rset->next()) {
286  m_runIOV = *runiov;
287  m_subRunNum = subrun;
288 
289  m_ID = rset->getInt(1);
290  Date startDate = rset->getDate(2);
291  Date endDate = rset->getDate(3);
292 
293  m_subRunStart = dh.dateToTm( startDate );
294  m_subRunEnd = dh.dateToTm( endDate );
295  } else {
296  throw(std::runtime_error("MODRunIOV::setByRun: Given subrun is not in the database"));
297  }
298 
299  m_conn->terminateStatement(stmt);
300  } catch (SQLException &e) {
301  throw(std::runtime_error("MODRunIOV::setByRun: "+e.getMessage()));
302  }
303 
304 }
Definition: start.py:1
int run_t
Definition: CaliIOV.h:11
void setRunIOV(const RunIOV &iov)
Definition: MODRunIOV.cc:36
int writeDB() noexcept(false)
Definition: MODRunIOV.cc:195
Tm getSubRunStart() const
Definition: MODRunIOV.cc:77
#define noexcept
#define NULL
Definition: scimark2.h:8
~MODRunIOV()
Definition: MODRunIOV.cc:24
oracle::occi::SQLException SQLException
Definition: HcalDbOmds.cc:27
void setSubRunEnd(const Tm &end)
Definition: MODRunIOV.cc:84
int fetchID() noexcept(false)
Definition: MODRunIOV.cc:101
run_t getSubRunNumber() const
Definition: MODRunIOV.cc:60
void setByID(int id) noexcept(false)
Definition: MODRunIOV.cc:156
int subrun_t
Definition: MODRunIOV.h:11
#define end
Definition: vmac.h:37
oracle::occi::Date tmToDate(const Tm &inTm) const
Definition: DateHandler.cc:20
void setSubRunStart(const Tm &start)
Definition: MODRunIOV.cc:67
Tm getPlusInfTm() const
Definition: DateHandler.h:15
Tm getSubRunEnd() const
Definition: MODRunIOV.cc:94
void fetchParentIDs(int *runIOVID) noexcept(false)
Definition: MODRunIOV.cc:247
void setSubRunNumber(subrun_t subrun)
Definition: MODRunIOV.cc:50
oracle::occi::ResultSet ResultSet
Definition: HcalDbOmds.cc:26
void setByRun(RunIOV *runiov, subrun_t subrun) noexcept(false)
Definition: MODRunIOV.cc:262
RunIOV getRunIOV()
Definition: MODRunIOV.cc:44
void setID(int id)
Definition: MODRunIOV.cc:29
dh
Definition: cuy.py:353
Definition: RunIOV.h:13
Tm dateToTm(oracle::occi::Date &date) const
Definition: DateHandler.cc:31
Definition: Tm.h:13