CMS 3D CMS Logo

FEConfigSlidingInfo.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 {
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 
27  m_iov_id=0;
28 }
29 
30 
31 
33 {
34 }
35 
36 
37 
39 
40  int result=0;
41  try {
42  this->checkConnection();
43 
44  m_readStmt = m_conn->createStatement();
45  m_readStmt->setSQL("select FE_CONFIG_SLI_SQ.NextVal from DUAL ");
46  ResultSet* rset = m_readStmt->executeQuery();
47  while (rset->next ()){
48  result= rset->getInt(1);
49  }
50  result++;
51  m_conn->terminateStatement(m_readStmt);
52  return result;
53 
54  } catch (SQLException &e) {
55  throw(std::runtime_error(std::string("FEConfigSlidingInfo::fetchNextId(): ")+getOraMessage(&e)));
56  }
57 
58 }
59 
62 {
63  this->checkConnection();
64 
65  int next_id=0;
66  if(getId()==0){
67  next_id=fetchNextId();
68  }
69 
70  try {
71  m_writeStmt = m_conn->createStatement();
72  m_writeStmt->setSQL("INSERT INTO "+getTable()+" ( sli_conf_id, tag, version, iov_id) "
73  " VALUES ( :1, :2, :3 , :4) " );
74 
75  m_writeStmt->setInt(1, next_id);
76  m_ID=next_id;
77 
78  } catch (SQLException &e) {
79  throw(std::runtime_error(std::string("FEConfigSlidingInfo::prepareWrite(): ")+getOraMessage(&e)));
80  }
81 
82 }
83 
84 void FEConfigSlidingInfo::setParameters(const std::map<string,string>& my_keys_map){
85 
86  // parses the result of the XML parser that is a map of
87  // string string with variable name variable value
88 
89  for( std::map<std::string, std::string >::const_iterator ci=
90  my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
91 
92  if(ci->first== "VERSION") setVersion(atoi(ci->second.c_str()) );
93  if(ci->first== "TAG") setConfigTag(ci->second);
94  if(ci->first== "IOV_ID") setIOVId(atoi(ci->second.c_str()) );
95 
96  }
97 
98 }
99 
101  noexcept(false)
102 {
103  this->checkConnection();
104  this->checkPrepare();
105 
106  try {
107 
108  // number 1 is the id
109  m_writeStmt->setString(2, this->getConfigTag());
110  m_writeStmt->setInt(3, this->getVersion());
111  m_writeStmt->setInt(4, this->getIOVId());
112 
113  m_writeStmt->executeUpdate();
114 
115 
116  } catch (SQLException &e) {
117  throw(std::runtime_error(std::string("FEConfigSlidingInfo::writeDB(): ")+getOraMessage(&e)));
118  }
119  // Now get the ID
120  if (!this->fetchID()) {
121  throw(std::runtime_error("FEConfigSlidingInfo::writeDB: Failed to write"));
122  }
123 
124 
125 }
126 
127 
129  noexcept(false)
130 {
131  this->checkConnection();
132  result->clear();
133  if(result->getId()==0 && (result->getConfigTag().empty()) ){
134  throw(std::runtime_error("FEConfigSlidingInfo::fetchData(): no Id defined for this FEConfigSlidingInfo "));
135  }
136 
137  try {
138  DateHandler dh(m_env, m_conn);
139 
140  m_readStmt->setSQL("SELECT * FROM " + getTable() +
141  " where ( sli_conf_id= :1 or (tag=:2 AND version=:3 ) )" );
142  m_readStmt->setInt(1, result->getId());
143  m_readStmt->setString(2, result->getConfigTag());
144  m_readStmt->setInt(3, result->getVersion());
145  ResultSet* rset = m_readStmt->executeQuery();
146 
147  rset->next();
148 
149  // 1 is the id and 2 is the config tag and 3 is the version
150 
151  result->setId(rset->getInt(1));
152  result->setConfigTag(getOraString(rset,2));
153  result->setVersion(rset->getInt(3));
154  result->setIOVId(rset->getInt(4));
155  Date dbdate = rset->getDate(5);
156  result->setDBTime( dh.dateToTm( dbdate ));
157 
158  } catch (SQLException &e) {
159  throw(std::runtime_error(std::string("FEConfigSlidingInfo::fetchData(): ")+getOraMessage(&e)));
160  }
161 }
162 
164  noexcept(false)
165 {
166  this->checkConnection();
167  result->clear();
168  try {
169  DateHandler dh(m_env, m_conn);
170 
171  m_readStmt->setSQL("SELECT * FROM " + getTable() +
172  " where sli_conf_id = ( select max( sli_conf_id) from "+ getTable() +" ) " );
173  ResultSet* rset = m_readStmt->executeQuery();
174 
175  rset->next();
176 
177  result->setId(rset->getInt(1));
178  result->setConfigTag(getOraString(rset,2));
179  result->setVersion(rset->getInt(3));
180  result->setIOVId(rset->getInt(4));
181  Date dbdate = rset->getDate(5);
182  result->setDBTime( dh.dateToTm( dbdate ));
183 
184  } catch (SQLException &e) {
185  throw(std::runtime_error(std::string("FEConfigSlidingInfo::fetchData(): ")+getOraMessage(&e)));
186  }
187 }
188 
190 {
191  // Return from memory if available
192  if (m_ID!=0) {
193  return m_ID;
194  }
195 
196  this->checkConnection();
197 
198  try {
199  Statement* stmt = m_conn->createStatement();
200  stmt->setSQL("SELECT sli_conf_id FROM "+ getTable()+
201  " WHERE tag=:1 and version=:2 " );
202 
203  stmt->setString(1, getConfigTag() );
204  stmt->setInt(2, getVersion() );
205 
206  ResultSet* rset = stmt->executeQuery();
207 
208  if (rset->next()) {
209  m_ID = rset->getInt(1);
210  } else {
211  m_ID = 0;
212  }
213  m_conn->terminateStatement(stmt);
214  } catch (SQLException &e) {
215  throw(std::runtime_error(std::string("FEConfigSlidingInfo::fetchID: ")+getOraMessage(&e)));
216  }
217 
218  return m_ID;
219 }
220 
221 
222 
224  noexcept(false)
225 {
226  this->checkConnection();
227 
228  DateHandler dh(m_env, m_conn);
229 
230  try {
231  Statement* stmt = m_conn->createStatement();
232 
233  stmt->setSQL("SELECT * FROM " + getTable() +" WHERE sli_conf_id = :1");
234  stmt->setInt(1, id);
235 
236  ResultSet* rset = stmt->executeQuery();
237  if (rset->next()) {
238  this->setId(rset->getInt(1));
239  this->setConfigTag(getOraString(rset,2));
240  this->setVersion(rset->getInt(3));
241  this->setIOVId(rset->getInt(4));
242  Date dbdate = rset->getDate(5);
243  this->setDBTime( dh.dateToTm( dbdate ));
244  } else {
245  throw(std::runtime_error("FEConfigSlidingInfo::setByID: Given config_id is not in the database"));
246  }
247 
248  m_conn->terminateStatement(stmt);
249  } catch (SQLException &e) {
250  throw(std::runtime_error(std::string("FEConfigSlidingInfo::setByID: ")+getOraMessage(&e)));
251  }
252 }
253 
254 
255 
static unsigned int getId()
int fetchNextId() noexcept(false)
#define noexcept
void fetchLastData(FEConfigSlidingInfo *result) noexcept(false)
void clear(CLHEP::HepGenMatrix &m)
Helper function: Reset all elements of a matrix to 0.
Definition: matutil.cc:167
void setParameters(const std::map< std::string, std::string > &my_keys_map)
int fetchID() noexcept(false)
oracle::occi::Statement Statement
Definition: IODConfig.h:23
void fetchData(FEConfigSlidingInfo *result) noexcept(false)
void setByID(int id) noexcept(false)
oracle::occi::SQLException SQLException
Definition: IODConfig.h:22
void writeDB() noexcept(false)
void prepareWrite() noexcept(false) override
dh
Definition: cuy.py:353
Tm dateToTm(oracle::occi::Date &date) const
Definition: DateHandler.cc:31