CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
FEConfigTimingInfo.cc
Go to the documentation of this file.
1 #include <stdexcept>
2 #include <string>
3 #include <cstring>
5 #include <cstdlib>
9 
10 using namespace std;
11 using namespace oracle::occi;
12 
14  m_env = nullptr;
15  m_conn = nullptr;
16  m_writeStmt = nullptr;
17  m_readStmt = nullptr;
18  m_config_tag = "";
19  m_version = 0;
20  m_ID = 0;
21  clear();
22 }
23 
25 
27 
29  int result = 0;
30  try {
31  this->checkConnection();
32 
33  m_readStmt = m_conn->createStatement();
34  m_readStmt->setSQL("select FE_CONFIG_TIM_SQ.NextVal from DUAL ");
35  ResultSet* rset = m_readStmt->executeQuery();
36  while (rset->next()) {
37  result = rset->getInt(1);
38  }
39  result++;
40  m_conn->terminateStatement(m_readStmt);
41  return result;
42 
43  } catch (SQLException& e) {
44  throw(std::runtime_error(std::string("FEConfigTimingInfo::fetchNextId(): ") + e.getMessage()));
45  }
46 }
47 
49  this->checkConnection();
50 
51  int next_id = 0;
52  if (getId() == 0) {
53  next_id = fetchNextId();
54  }
55 
56  try {
57  m_writeStmt = m_conn->createStatement();
58  m_writeStmt->setSQL("INSERT INTO " + getTable() +
59  " ( tim_conf_id, tag, version) "
60  " VALUES ( :1, :2, :3 ) ");
61 
62  m_writeStmt->setInt(1, next_id);
63  m_ID = next_id;
64 
65  } catch (SQLException& e) {
66  throw(std::runtime_error(std::string("FEConfigTimingInfo::prepareWrite(): ") + e.getMessage()));
67  }
68 }
69 
70 void FEConfigTimingInfo::setParameters(const std::map<string, string>& my_keys_map) {
71  // parses the result of the XML parser that is a map of
72  // string string with variable name variable value
73 
74  for (std::map<std::string, std::string>::const_iterator ci = my_keys_map.begin(); ci != my_keys_map.end(); ci++) {
75  if (ci->first == "VERSION")
76  setVersion(atoi(ci->second.c_str()));
77  if (ci->first == "TAG")
78  setConfigTag(ci->second);
79  }
80 }
81 
83  this->checkConnection();
84  this->checkPrepare();
85 
86  try {
87  // number 1 is the id
88  m_writeStmt->setString(2, this->getConfigTag());
89  m_writeStmt->setInt(3, this->getVersion());
90 
91  m_writeStmt->executeUpdate();
92 
93  } catch (SQLException& e) {
94  throw(std::runtime_error(std::string("FEConfigTimingInfo::writeDB(): ") + e.getMessage()));
95  }
96  // Now get the ID
97  if (!this->fetchID()) {
98  throw(std::runtime_error("FEConfigTimingInfo::writeDB: Failed to write"));
99  }
100 }
101 
103  this->checkConnection();
104  result->clear();
105  if (result->getId() == 0 && (result->getConfigTag().empty())) {
106  throw(std::runtime_error("FEConfigTimingInfo::fetchData(): no Id defined for this FEConfigTimingInfo "));
107  }
108 
109  try {
110  DateHandler dh(m_env, m_conn);
111 
112  m_readStmt->setSQL("SELECT * FROM " + getTable() + " where ( tim_conf_id= :1 or (tag=:2 AND version=:3 ) )");
113  m_readStmt->setInt(1, result->getId());
114  m_readStmt->setString(2, result->getConfigTag());
115  m_readStmt->setInt(3, result->getVersion());
116  ResultSet* rset = m_readStmt->executeQuery();
117 
118  rset->next();
119 
120  // 1 is the id and 2 is the config tag and 3 is the version
121 
122  result->setId(rset->getInt(1));
123  result->setConfigTag(rset->getString(2));
124  result->setVersion(rset->getInt(3));
125  Date dbdate = rset->getDate(4);
126  result->setDBTime(dh.dateToTm(dbdate));
127 
128  } catch (SQLException& e) {
129  throw(std::runtime_error(std::string("FEConfigTimingInfo::fetchData(): ") + e.getMessage()));
130  }
131 }
132 
134  this->checkConnection();
135  result->clear();
136  try {
137  DateHandler dh(m_env, m_conn);
138 
139  m_readStmt->setSQL("SELECT * FROM " + getTable() + " where tim_conf_id = ( select max( tim_conf_id) from " +
140  getTable() + " ) ");
141  ResultSet* rset = m_readStmt->executeQuery();
142 
143  rset->next();
144 
145  result->setId(rset->getInt(1));
146  result->setConfigTag(rset->getString(2));
147  result->setVersion(rset->getInt(3));
148  Date dbdate = rset->getDate(4);
149  result->setDBTime(dh.dateToTm(dbdate));
150 
151  } catch (SQLException& e) {
152  throw(std::runtime_error(std::string("FEConfigTimingInfo::fetchData(): ") + e.getMessage()));
153  }
154 }
155 
157  // Return from memory if available
158  if (m_ID != 0) {
159  return m_ID;
160  }
161 
162  this->checkConnection();
163 
164  try {
165  Statement* stmt = m_conn->createStatement();
166  stmt->setSQL("SELECT tim_conf_id FROM " + getTable() + " WHERE tag=:1 and version=:2 ");
167 
168  stmt->setString(1, getConfigTag());
169  stmt->setInt(2, getVersion());
170 
171  ResultSet* rset = stmt->executeQuery();
172 
173  if (rset->next()) {
174  m_ID = rset->getInt(1);
175  } else {
176  m_ID = 0;
177  }
178  m_conn->terminateStatement(stmt);
179  } catch (SQLException& e) {
180  throw(std::runtime_error(std::string("FEConfigTimingInfo::fetchID: ") + e.getMessage()));
181  }
182 
183  return m_ID;
184 }
185 
186 void FEConfigTimingInfo::setByID(int id) noexcept(false) {
187  this->checkConnection();
188 
189  DateHandler dh(m_env, m_conn);
190 
191  try {
192  Statement* stmt = m_conn->createStatement();
193 
194  stmt->setSQL("SELECT * FROM " + getTable() + " WHERE tim_conf_id = :1");
195  stmt->setInt(1, id);
196 
197  ResultSet* rset = stmt->executeQuery();
198  if (rset->next()) {
199  this->setId(rset->getInt(1));
200  this->setConfigTag(rset->getString(2));
201  this->setVersion(rset->getInt(3));
202  Date dbdate = rset->getDate(4);
203  this->setDBTime(dh.dateToTm(dbdate));
204  } else {
205  throw(std::runtime_error("FEConfigTimingInfo::setByID: Given tim_conf_id is not in the database"));
206  }
207 
208  m_conn->terminateStatement(stmt);
209  } catch (SQLException& e) {
210  throw(std::runtime_error(std::string("FEConfigTimingInfo::setByID: ") + e.getMessage()));
211  }
212 }
void fetchLastData(FEConfigTimingInfo *result) noexcept(false)
~FEConfigTimingInfo() override
void prepareWrite() noexcept(false) override
void writeDB() noexcept(false)
void setByID(int id) noexcept(false)
oracle::occi::Statement Statement
Definition: IODConfig.h:21
static unsigned int getId()
void fetchData(FEConfigTimingInfo *result) noexcept(false)
int fetchID() noexcept(false)
oracle::occi::SQLException SQLException
Definition: IODConfig.h:20
int fetchNextId() noexcept(false)
void setParameters(const std::map< std::string, std::string > &my_keys_map)
void clear(EGIsoObj &c)
Definition: egamma.h:82
dh
Definition: cuy.py:354