CMS 3D CMS Logo

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