CMS 3D CMS Logo

FEConfigOddWeightInfo.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 
25 void FEConfigOddWeightInfo::clear() { m_ngr = 0; }
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_WEIGHT2GROUP_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") << "FEConfigOddWeightInfo::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  " ( wei2_conf_id, tag, number_of_groups) "
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") << "FEConfigOddWeightInfo::prepareWrite(): " << e.getMessage();
68  }
69 }
70 
71 void FEConfigOddWeightInfo::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 == "TAG")
77  setConfigTag(ci->second);
78  if (ci->first == "NUMBER_OF_GROUPS")
79  setNumberOfGroups(atoi(ci->second.c_str()));
80  }
81 }
82 
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->getNumberOfGroups());
91 
92  m_writeStmt->executeUpdate();
93 
94  } catch (SQLException& e) {
95  throw cms::Exception("SQLException") << "FEConfigOddWeightInfo::writeDB(): " << e.getMessage();
96  }
97  // Now get the ID
98  if (!this->fetchID()) {
99  throw(std::runtime_error("FEConfigOddWeightInfo::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("FEConfigOddWeightInfo::fetchData(): no Id defined for this FEConfigOddWeightInfo "));
108  }
109 
110  try {
111  DateHandler dh(m_env, m_conn);
112 
113  m_readStmt->setSQL("SELECT wei2_conf_id, tag, number_of_groups, db_timestamp FROM " + getTable() +
114  " where ( wei2_conf_id= :1 or (tag=:2 ) )");
115  m_readStmt->setInt(1, result->getId());
116  m_readStmt->setString(2, result->getConfigTag());
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->setNumberOfGroups(rset->getInt(3));
126  Date dbdate = rset->getDate(4);
127  result->setDBTime(dh.dateToTm(dbdate));
128 
129  } catch (SQLException& e) {
130  throw cms::Exception("SQLException") << "FEConfigOddWeightInfo::fetchData(): " << e.getMessage();
131  }
132 }
133 
135  this->checkConnection();
136  result->clear();
137  try {
138  DateHandler dh(m_env, m_conn);
139 
140  m_readStmt->setSQL("SELECT wei2_conf_id, tag, number_of_groups, db_timestamp FROM " + getTable() +
141  " where wei2_conf_id = ( select max( wei2_conf_id) from " + getTable() + " ) ");
142  ResultSet* rset = m_readStmt->executeQuery();
143 
144  rset->next();
145 
146  result->setId(rset->getInt(1));
147  result->setConfigTag(rset->getString(2));
148  result->setNumberOfGroups(rset->getInt(3));
149  Date dbdate = rset->getDate(4);
150  result->setDBTime(dh.dateToTm(dbdate));
151 
152  } catch (SQLException& e) {
153  throw cms::Exception("SQLException") << "FEConfigOddWeightInfo::fetchData(): " << e.getMessage();
154  }
155 }
156 
158  // Return from memory if available
159  if (m_ID != 0) {
160  return m_ID;
161  }
162 
163  this->checkConnection();
164 
165  try {
166  Statement* stmt = m_conn->createStatement();
167  stmt->setSQL("SELECT wei2_conf_id FROM " + getTable() + " WHERE tag=:1 ");
168 
169  stmt->setString(1, getConfigTag());
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 cms::Exception("SQLException") << "FEConfigOddWeightInfo::fetchID: " << e.getMessage();
181  }
182 
183  return m_ID;
184 }
185 
186 void FEConfigOddWeightInfo::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 wei2_conf_id, tag, number_of_groups, db_timestamp FROM " + getTable() +
195  " WHERE wei2_conf_id = :1");
196  stmt->setInt(1, id);
197 
198  ResultSet* rset = stmt->executeQuery();
199  if (rset->next()) {
200  this->setId(rset->getInt(1));
201  this->setConfigTag(rset->getString(2));
202  this->setNumberOfGroups(rset->getInt(3));
203  Date dbdate = rset->getDate(4);
204  this->setDBTime(dh.dateToTm(dbdate));
205  } else {
206  throw(std::runtime_error("FEConfigOddWeightInfo::setByID: Given config_id is not in the database"));
207  }
208 
209  m_conn->terminateStatement(stmt);
210  } catch (SQLException& e) {
211  throw cms::Exception("SQLException") << "FEConfigOddWeightInfo::setByID: " << e.getMessage();
212  }
213 }
int fetchNextId() noexcept(false)
void prepareWrite() noexcept(false) override
void writeDB() noexcept(false)
void fetchData(FEConfigOddWeightInfo *result) noexcept(false)
void setByID(int id) noexcept(false)
int fetchID() noexcept(false)
oracle::occi::Statement Statement
Definition: IODConfig.h:21
static unsigned int getId()
void fetchLastData(FEConfigOddWeightInfo *result) noexcept(false)
oracle::occi::SQLException SQLException
Definition: IODConfig.h:20
void clear(HadCaloObj &c)
Definition: data.h:124
void setParameters(const std::map< std::string, std::string > &my_keys_map)
dh
Definition: cuy.py:354