CMS 3D CMS Logo

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