CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
FEConfigMainInfo.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_env = nullptr;
14  m_conn = nullptr;
15  m_writeStmt = nullptr;
16  m_readStmt = nullptr;
17 
18  m_ID = 0;
19  m_version = 0;
20  clear();
21 }
22 
24 
26  m_description = "";
27  m_ped_id = 0;
28  m_lin_id = 0;
29  m_lut_id = 0;
30  m_sli_id = 0;
31  m_fgr_id = 0;
32  m_wei_id = 0;
33  m_bxt_id = 0;
34  m_btt_id = 0;
35  m_tim_id = 0;
36  m_spi_id = 0;
37  m_bst_id = 0;
38  m_coke_id = 0;
39  m_wei2_id = 0;
40 
41  m_db_time = Tm();
42 }
44  int result = 0;
45  try {
46  this->checkConnection();
47 
48  m_readStmt = m_conn->createStatement();
49  m_readStmt->setSQL("select fe_config_main_sq.NextVal from dual");
50  ResultSet* rset = m_readStmt->executeQuery();
51  while (rset->next()) {
52  result = rset->getInt(1);
53  }
54  m_conn->terminateStatement(m_readStmt);
55  return result;
56 
57  } catch (SQLException& e) {
58  throw cms::Exception("SQLException") << "FEConfigMainInfo::fetchNextId(): " << e.getMessage();
59  }
60 }
61 
63  // Return from memory if available
64  if (m_ID > 0) {
65  return m_ID;
66  }
67 
68  this->checkConnection();
69 
70  DateHandler dh(m_env, m_conn);
71 
72  std::cout << " tag/version " << getConfigTag() << "/" << getVersion() << std::endl;
73 
74  try {
75  Statement* stmt = m_conn->createStatement();
76  if (m_version != 0) {
77  stmt->setSQL(
78  "SELECT conf_id from FE_CONFIG_MAIN "
79  "WHERE tag = :tag "
80  " and version = :version ");
81  stmt->setString(1, m_config_tag);
82  stmt->setInt(2, m_version);
83  std::cout << " using query with version " << endl;
84  } else {
85  // always select the last inserted one with a given tag
86  stmt->setSQL(
87  "SELECT conf_id from FE_CONFIG_MAIN "
88  "WHERE tag = :1 and version= (select max(version) from FE_CONFIG_MAIN where tag=:2) ");
89  stmt->setString(1, m_config_tag);
90  stmt->setString(2, m_config_tag);
91  std::cout << " using query WITHOUT version " << endl;
92  }
93 
94  ResultSet* rset = stmt->executeQuery();
95 
96  if (rset->next()) {
97  m_ID = rset->getInt(1);
98  } else {
99  m_ID = 0;
100  }
101  std::cout << m_ID << endl;
102  m_conn->terminateStatement(stmt);
103  } catch (SQLException& e) {
104  throw cms::Exception("SQLException") << "FEConfigMainInfo::fetchID: " << e.getMessage();
105  }
106  setByID(m_ID);
107  return m_ID;
108 }
109 
111  this->checkConnection();
112 
113  int next_id = fetchNextId();
114 
115  try {
116  m_writeStmt = m_conn->createStatement();
117  m_writeStmt->setSQL(
118  "INSERT INTO fe_config_main (conf_id, ped_conf_id, lin_conf_id, lut_conf_id, fgr_conf_id, sli_conf_id, "
119  "wei_conf_id, spi_conf_id, tim_conf_id, bxt_conf_id, btt_conf_id, bst_conf_id, coke_conf_id, wei2_conf_id, "
120  "tag, version, description) "
121  " VALUES (:1, :2, :3 , :4, :5, :6 ,:7, :8, :9, :10, :11, :12, :13, :14, :15, :16, :17 )");
122 
123  m_writeStmt->setInt(1, next_id);
124  m_ID = next_id;
125 
126  } catch (SQLException& e) {
127  throw cms::Exception("SQLException") << "FEConfigMainInfo::prepareWrite(): " << e.getMessage();
128  }
129 }
130 
132  this->checkConnection();
133  this->checkPrepare();
134 
135  // Validate the data, use infinity-till convention
136  DateHandler dh(m_env, m_conn);
137 
138  try {
139  m_writeStmt->setInt(2, this->getPedId());
140  m_writeStmt->setInt(3, this->getLinId());
141  m_writeStmt->setInt(4, this->getLUTId());
142  m_writeStmt->setInt(5, this->getFgrId());
143  m_writeStmt->setInt(6, this->getSliId());
144  m_writeStmt->setInt(7, this->getWeiId());
145  m_writeStmt->setInt(8, this->getSpiId());
146  m_writeStmt->setInt(9, this->getTimId());
147  m_writeStmt->setInt(10, this->getBxtId());
148  m_writeStmt->setInt(11, this->getBttId());
149  m_writeStmt->setInt(12, this->getBstId());
150  m_writeStmt->setInt(13, this->getCokeId());
151  m_writeStmt->setInt(14, this->getWei2Id());
152  m_writeStmt->setString(15, this->getConfigTag());
153  m_writeStmt->setInt(16, this->getVersion());
154  m_writeStmt->setString(17, this->getDescription());
155  m_writeStmt->executeUpdate();
156 
157  } catch (SQLException& e) {
158  throw cms::Exception("SQLException") << "FEConfigMainInfo::writeDB: " << e.getMessage();
159  }
160  // Now get the ID
161  if (!this->fetchID()) {
162  throw(std::runtime_error("FEConfigMainInfo::writeDB: Failed to write"));
163  }
164  setByID(m_ID);
165 
166  cout << "FEConfigMainInfo::writeDB>> done inserting FEConfigMainInfo with id=" << m_ID << endl;
167 }
168 
170  this->checkConnection();
171 
172  DateHandler dh(m_env, m_conn);
173 
174  try {
175  Statement* stmt = m_conn->createStatement();
176  stmt->setSQL("SELECT max(conf_id) FROM fe_config_main ");
177  ResultSet* rset = stmt->executeQuery();
178 
179  if (rset->next()) {
180  m_ID = rset->getInt(1);
181  } else {
182  m_ID = 0;
183  }
184  m_conn->terminateStatement(stmt);
185  } catch (SQLException& e) {
186  throw cms::Exception("SQLException") << "ODRunConfigInfo::fetchIDLast: " << e.getMessage();
187  }
188 
189  setByID(m_ID);
190  return m_ID;
191 }
192 
193 void FEConfigMainInfo::setByID(int id) noexcept(false) {
194  this->checkConnection();
195 
196  DateHandler dh(m_env, m_conn);
197 
198  cout << "FEConfigMainInfo::setByID called for id " << id << endl;
199 
200  try {
201  Statement* stmt = m_conn->createStatement();
202 
203  stmt->setSQL(
204  "SELECT conf_id, ped_conf_id, lin_conf_id, lut_conf_id, fgr_conf_id, sli_conf_id, wei_conf_id, spi_conf_id, "
205  "tim_conf_id, bxt_conf_id, btt_conf_id, bst_conf_id, coke_conf_id, wei2_conf_id, tag, version, description, "
206  "db_timestamp FROM FE_CONFIG_MAIN WHERE conf_id = :1 ");
207  stmt->setInt(1, id);
208 
209  ResultSet* rset = stmt->executeQuery();
210  if (rset->next()) {
211  setId(rset->getInt(1));
212  setPedId(rset->getInt(2));
213  setLinId(rset->getInt(3));
214  setLUTId(rset->getInt(4));
215  setFgrId(rset->getInt(5));
216  setSliId(rset->getInt(6));
217  setWeiId(rset->getInt(7));
218  setSpiId(rset->getInt(8));
219  setTimId(rset->getInt(9));
220  setBxtId(rset->getInt(10));
221  setBttId(rset->getInt(11));
222  setBstId(rset->getInt(12));
223  setCokeId(rset->getInt(13));
224  setWei2Id(rset->getInt(14));
225  setConfigTag(rset->getString(15));
226  setVersion(rset->getInt(16));
227  setDescription(rset->getString(17));
228  Date dbdate = rset->getDate(18);
229  setDBTime(dh.dateToTm(dbdate));
230  m_ID = id;
231  } else {
232  throw(std::runtime_error("FEConfigMainInfo::setByID: Given cycle_id is not in the database"));
233  }
234  m_conn->terminateStatement(stmt);
235  } catch (SQLException& e) {
236  throw cms::Exception("SQLException") << "FEConfigMainInfo::setByID: " << e.getMessage();
237  }
238 }
239 
241  std::cout << " ### 1 getId from FEConfigMainInfo = " << result->getId() << std::endl;
242  std::cout << " tag/version " << result->getConfigTag() << "/" << result->getVersion() << std::endl;
243 
244  this->checkConnection();
245  DateHandler dh(m_env, m_conn);
246  // result->clear();
247 
248  int idid = 0;
249 
250  if (result->getId() == 0) {
251  //throw(std::runtime_error("FEConfigMainInfo::fetchData(): no Id defined for this FEConfigMainInfo "));
252  idid = result->fetchID();
253  result->setId(idid);
254  }
255 
256  try {
257  m_readStmt->setSQL(
258  "SELECT conf_id, ped_conf_id, lin_conf_id, lut_conf_id, fgr_conf_id, sli_conf_id, wei_conf_id, spi_conf_id, "
259  "tim_conf_id, bxt_conf_id, btt_conf_id, bst_conf_id, coke_conf_id, wei2_conf_id, tag, version, description, "
260  "db_timestamp FROM FE_CONFIG_MAIN WHERE conf_id = :1 ");
261 
262  std::cout << " ### 2 getId from FEConfigMainInfo = " << result->getId() << std::endl;
263 
264  // good m_readStmt->setInt(1, result->getId());
265  m_readStmt->setInt(1, result->getId());
266  ResultSet* rset = m_readStmt->executeQuery();
267 
268  rset->next();
269 
270  result->setId(rset->getInt(1));
271 
272  setPedId(rset->getInt(2));
273  setLinId(rset->getInt(3));
274  setLUTId(rset->getInt(4));
275  setFgrId(rset->getInt(5));
276  setSliId(rset->getInt(6));
277  setWeiId(rset->getInt(7));
278  setSpiId(rset->getInt(8));
279  setTimId(rset->getInt(9));
280  setBxtId(rset->getInt(10));
281  setBttId(rset->getInt(11));
282  setBstId(rset->getInt(12));
283  setCokeId(rset->getInt(13));
284  setWei2Id(rset->getInt(14));
285 
286  result->setConfigTag(rset->getString(15));
287  result->setVersion(rset->getInt(16));
288  result->setDescription(rset->getString(17));
289  Date dbdate = rset->getDate(18);
290  result->setDBTime(dh.dateToTm(dbdate));
291 
292  } catch (SQLException& e) {
293  throw cms::Exception("SQLException") << "FEConfigMainInfo::fetchData(): " << e.getMessage();
294  }
295 }
296 
298  try {
299  prepareWrite();
300  writeDB();
301  m_conn->commit();
302  terminateWriteStatement();
303  } catch (std::runtime_error& e) {
304  m_conn->rollback();
305  throw cms::Exception("RuntimeError") << e.what();
306  }
307 }
void setByID(int id) noexcept(false)
uint16_t *__restrict__ id
int fetchNextId() noexcept(false)
activeDets clear()
int fetchID() noexcept(false)
void prepareWrite() noexcept(false) override
void writeDB() noexcept(false)
tuple result
Definition: mps_fire.py:311
void insertConfig() noexcept(false)
void fetchData(FEConfigMainInfo *result) noexcept(false)
oracle::occi::Statement Statement
Definition: IODConfig.h:21
oracle::occi::SQLException SQLException
Definition: IODConfig.h:20
tuple cout
Definition: gather_cfg.py:144
Tm dateToTm(oracle::occi::Date &date) const
Definition: DateHandler.cc:28
int fetchIDLast() noexcept(false)
Definition: Tm.h:13
~FEConfigMainInfo() override
tuple dh
Definition: cuy.py:354