CMS 3D CMS Logo

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