CMS 3D CMS Logo

LMFUnique.cc
Go to the documentation of this file.
2 #include <iomanip>
3 
4 using namespace std;
5 using namespace oracle::occi;
6 
8 
10  std::string ts = t.str();
11  return ts.substr(2, 2);
12 }
13 
15  // check if this key exists
16  std::map<std::string, std::string>::const_iterator i = m_stringFields.find(key);
17  if (i != m_stringFields.end()) {
18  // the key exist: check if it changed: reset the ID of the object
19  if (i->second != value) {
20  m_stringFields[key] = value;
21  m_ID = 0;
22  }
23  } else {
24  // create this key and reset the ID of the object
25  m_stringFields[key] = value;
26  m_ID = 0;
27  }
28  return *this;
29 }
30 
32  // check if this key exists
33  std::map<std::string, int>::const_iterator i = m_intFields.find(key);
34  if (i != m_intFields.end()) {
35  // the key exist: check if it changed: reset the ID of the object
36  if (i->second != value) {
37  m_intFields[key] = value;
38  m_ID = 0;
39  }
40  } else {
41  // create this key and reset the ID of the object
42  m_intFields[key] = value;
43  m_ID = 0;
44  }
45  return *this;
46 }
47 
49  std::map<std::string, LMFUnique*>::const_iterator i = m_foreignKeys.find(name);
50  if (i != m_foreignKeys.end()) {
51  if (i->second != u) {
52  m_foreignKeys[name] = u;
53  m_ID = 0;
54  }
55  } else {
56  m_foreignKeys[name] = u;
57  m_ID = 0;
58  }
59 }
60 
61 std::list<std::unique_ptr<LMFUnique>> LMFUnique::fetchAll() const noexcept(false) {
62  /*
63  Returns a list of pointers to DB objects
64  */
65  std::list<std::unique_ptr<LMFUnique>> l;
66  this->checkConnection();
67 
68  try {
69  Statement* stmt = m_conn->createStatement();
70  std::string sql = fetchAllSql(stmt);
71  if (!sql.empty()) {
72  if (m_debug) {
73  cout << m_className + ": Query " + sql << endl;
74  }
75  ResultSet* rset = stmt->executeQuery();
76  while (rset->next() != 0) {
78  if (m_debug) {
79  o->debug();
80  }
81  if (o != nullptr) {
82  o->setByID(rset->getInt(1));
83  if (m_debug) {
84  o->dump();
85  }
86  try {
87  l.emplace_back(o);
88  } catch (std::exception& e) {
89  throw(std::runtime_error(m_className + "::fetchAll: " + e.what()));
90  }
91  }
92  }
93  }
94  m_conn->terminateStatement(stmt);
95  } catch (SQLException& e) {
96  throw(std::runtime_error(m_className + "::fetchAll: " + e.getMessage()));
97  }
98  if (m_debug) {
99  cout << m_className << ": list size = " << l.size() << endl;
100  }
101  return l;
102 }
103 
104 void LMFUnique::dump() const { dump(0); }
105 
106 void LMFUnique::dump(int n) const {
107  /*
108  This method is used to dump the content of an object
109  Indent data if the object is contained inside another object
110  */
111  std::string m_indent = "";
112  std::string m_trail = "";
113  m_trail.resize(70 - 31 - n * 2, '#');
114  m_indent.resize(n * 2, ' ');
115  m_indent += "|";
116  // start of object
117  cout << m_indent << "#################" << setw(15) << m_className << " " << m_trail << endl;
118  cout << m_indent << "Address: " << this << endl;
119  cout << m_indent << "Connection params : " << m_env << ", " << m_conn << endl;
120  // object ID in the DB
121  cout << m_indent << "ID" << setw(18) << ": " << m_ID;
122  if (m_ID == 0) {
123  cout << " *** NULL ID ***";
124  }
125  if (!isValid()) {
126  cout << " INVALID ***";
127  }
128  cout << endl;
129  // iterate over string fields
130  std::map<std::string, std::string>::const_iterator is = m_stringFields.begin();
131  std::map<std::string, std::string>::const_iterator es = m_stringFields.end();
132  while (is != es) {
133  std::string key = is->first;
134  cout << m_indent << key << setw(20 - key.length()) << ": " << is->second << endl;
135  is++;
136  }
137  // iterate over integer fields
138  std::map<std::string, int>::const_iterator ii = m_intFields.begin();
139  std::map<std::string, int>::const_iterator ei = m_intFields.end();
140  while (ii != ei) {
141  std::string key = ii->first;
142  cout << m_indent << key << setw(20 - key.length()) << ": " << ii->second << endl;
143  ii++;
144  }
145  cout << m_indent << "#################" << setw(15) << m_className << " " << m_trail << endl;
146  // iterate over foreign keys
147  std::map<std::string, LMFUnique*>::const_iterator ik = m_foreignKeys.begin();
148  std::map<std::string, LMFUnique*>::const_iterator ek = m_foreignKeys.end();
149  m_indent.clear();
150  m_indent.resize((n + 1) * 2, ' ');
151  while (ik != ek) {
152  cout << m_indent << "Foreign Key: " << ik->first << endl;
153  ik->second->dump(n + 1);
154  ik++;
155  }
156 }
157 
159  fetchID();
160  bool ret = false;
161  if (m_ID > 0) {
162  ret = true;
163  }
164  return ret;
165 }
166 
168  /* this method should setup a Statement to select the unique IDs of the
169  objects to return */
170  return "";
171 }
172 
174  /* this method should return a pointer to a newly created object */
175  return nullptr;
176 }
177 
179  std::string rs = "";
180  std::map<std::string, std::string>::const_iterator i = m_stringFields.find(s);
181  if (i != m_stringFields.end()) {
182  rs = i->second;
183  }
184  return rs;
185 }
186 
188  // this should be better defined
189  int ret = 0;
190  std::map<std::string, int>::const_iterator i = m_intFields.find(s);
191  if (i != m_intFields.end()) {
192  ret = i->second;
193  }
194  return ret;
195 }
196 
197 int LMFUnique::fetchID() noexcept(false) {
198  /*
199  This method fetch the ID of the object from the database according
200  to the given specifications.
201 
202  It is assumed that there is only one object in the database with the
203  given specifications. In case more than one object can be retrieved
204  this method throws an exception.
205 
206  Since not all the specifications can define completely the object
207  itself, at the end, we setup the object based on its ID.
208  */
209  // Return tag from memory if available
210  if (m_ID) {
211  return m_ID;
212  }
213 
214  this->checkConnection();
215 
216  // fetch this ID
217  try {
218  Statement* stmt = m_conn->createStatement();
219  // prepare the sql query
220  std::string sql = fetchIdSql(stmt);
221  if (!sql.empty()) {
222  if (m_debug) {
223  cout << m_className + ": Query " + sql << endl;
224  }
225 
226  ResultSet* rset = stmt->executeQuery();
227  if (rset->next() != 0) {
228  m_ID = rset->getInt(1);
229  } else {
230  m_ID = 0;
231  }
232  if (m_debug) {
233  cout << m_className + ": ID set to " << m_ID << endl;
234  }
235  int n = rset->getNumArrayRows();
236  if (m_debug) {
237  cout << m_className + ": Returned " << n << " rows" << endl;
238  }
239  if (n > 1) {
240  throw(std::runtime_error(m_className + "::fetchID: too many rows returned " + "executing query " + sql));
241  m_ID = 0;
242  }
243  }
244  m_conn->terminateStatement(stmt);
245  } catch (SQLException& e) {
246  throw(std::runtime_error(m_className + "::fetchID: " + e.getMessage()));
247  }
248  // given the ID of this object setup it completely
249  if (m_ID > 0) {
250  setByID(m_ID);
251  }
252  // if foreignKeys are there, set these objects too
253  map<string, LMFUnique*>::iterator i = m_foreignKeys.begin();
254  map<string, LMFUnique*>::iterator e = m_foreignKeys.end();
255  while (i != e) {
256  if (i->second->getID() == 0) {
257  i->second->fetchID();
258  }
259  i++;
260  }
261  if (m_debug) {
262  cout << m_className << ": fetchID:: returning " << m_ID << endl;
263  }
264  return m_ID;
265 }
266 
267 void LMFUnique::setByID(int id) noexcept(false) {
268  /*
269  Given the ID of an object setup it
270  */
271  if (m_debug) {
272  cout << m_className << ": Setting this object as ID = " << id << endl;
273  }
274  this->checkConnection();
275  try {
276  Statement* stmt = m_conn->createStatement();
277  std::string sql = setByIDSql(stmt, id);
278  if (sql.empty()) {
279  throw(std::runtime_error(m_className + "::setByID: [empty sql])"));
280  }
281  if (m_debug) {
282  cout << m_className + ": " + sql << endl;
283  }
284 
285  ResultSet* rset = stmt->executeQuery();
286  if (rset->next() != 0) {
287  // setup the concrete object
288  getParameters(rset);
289  m_ID = id;
290  if (m_debug) {
291  cout << m_className + ": Setting done. ID set to " << m_ID << endl;
292  }
293  } else {
294  throw(std::runtime_error(m_className + "::setByID: Given id is not in the database"));
295  }
296  m_conn->terminateStatement(stmt);
297  } catch (SQLException& e) {
298  throw(std::runtime_error(m_className + "::setByID: " + e.getMessage()));
299  }
300 }
301 
303  std::map<std::string, LMFUnique*>::const_iterator i = m_foreignKeys.begin();
304  std::map<std::string, LMFUnique*>::const_iterator e = m_foreignKeys.end();
305  int count = 0;
306  while (i != e) {
307  if (i->second->getID() == 0) {
308  i->second->writeDB();
309  count++;
310  }
311  i++;
312  }
313  return count;
314 }
315 
316 int LMFUnique::writeDB() noexcept(false) {
317  clock_t start = 0;
318  clock_t end = 0;
319  if (_profiling) {
320  start = clock();
321  }
322  // write the associated objects first (foreign keys must exist before use)
323  writeForeignKeys();
324  // see if this data is already in the DB
325  if (!(this->fetchID())) {
326  // check the connectioin
327  this->checkConnection();
328 
329  // write new tag to the DB
330  std::string sql = "";
331  try {
332  Statement* stmt = m_conn->createStatement();
333 
334  sql = writeDBSql(stmt);
335  if (!sql.empty()) {
336  if (m_debug) {
337  cout << m_className + ": " + sql << endl;
338  }
339  stmt->executeUpdate();
340  }
341  m_conn->commit();
342  m_conn->terminateStatement(stmt);
343  } catch (SQLException& e) {
344  debug();
345  dump();
346  throw(std::runtime_error(m_className + "::writeDB: " + e.getMessage() + " while executing query " + sql));
347  }
348  // now get the id
349  if (this->fetchID() == 0) {
350  throw(std::runtime_error(m_className + "::writeDB: Failed to write"));
351  }
352  }
353  if (_profiling) {
354  end = clock();
355  if (m_debug) {
356  std::cout << m_className << ":: Spent time in writeDB:" << ((double)(end - start)) / CLOCKS_PER_SEC << " s"
357  << endl;
358  }
359  }
360  return m_ID;
361 }
runTheMatrix.ret
ret
prodAgent to be discontinued
Definition: runTheMatrix.py:542
mps_fire.i
i
Definition: mps_fire.py:428
start
Definition: start.py:1
createObject
static void * createObject(dd4hep::Detector &description, int, char **)
Definition: DDCMSDetElementCreator.cc:377
funct::false
false
Definition: Factorize.h:29
LMFUnique::setByID
void setByID(int id) noexcept(false) override
Definition: LMFUnique.cc:267
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
gather_cfg.cout
cout
Definition: gather_cfg.py:144
LMFUnique::setInt
LMFUnique & setInt(std::string key, int value)
Definition: LMFUnique.cc:31
LMFUnique::writeDB
virtual int writeDB() noexcept(false)
Definition: LMFUnique.cc:316
LMFUnique::getInt
int getInt(std::string fieldname) const
Definition: LMFUnique.cc:187
watchdog.const
const
Definition: watchdog.py:83
EcalTangentSkim_cfg.o
o
Definition: EcalTangentSkim_cfg.py:42
LMFUnique::writeForeignKeys
virtual int writeForeignKeys() noexcept(false)
Definition: LMFUnique.cc:302
alignCSCRings.s
s
Definition: alignCSCRings.py:92
debug
#define debug
Definition: HDRShower.cc:19
LMFUnique::dump
virtual void dump() const
Definition: LMFUnique.cc:104
oracle::occi
Definition: ConnectionManager.h:7
LMFUnique::createObject
virtual LMFUnique * createObject() const
Definition: LMFUnique.cc:173
mps_fire.end
end
Definition: mps_fire.py:242
LMFUnique.h
submitPVResolutionJobs.count
count
Definition: submitPVResolutionJobs.py:352
sistrip::SpyUtilities::isValid
const bool isValid(const Frame &aFrame, const FrameQuality &aQuality, const uint16_t aExpectedPos)
Definition: SiStripSpyUtilities.cc:124
Tm
Definition: Tm.h:13
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
cppFunctionSkipper.exception
exception
Definition: cppFunctionSkipper.py:10
LMFUnique::fetchID
int fetchID() noexcept(false) override
Definition: LMFUnique.cc:197
LMFUnique::attach
void attach(std::string name, LMFUnique *u)
Definition: LMFUnique.cc:48
FrontierConditions_GlobalTag_cff.dump
dump
Definition: FrontierConditions_GlobalTag_cff.py:12
LMFUnique::getString
std::string getString(std::string fieldname) const
Definition: LMFUnique.cc:178
LMFUnique::~LMFUnique
~LMFUnique() override
Definition: LMFUnique.cc:7
value
Definition: value.py:1
cmsLHEtoEOSManager.l
l
Definition: cmsLHEtoEOSManager.py:204
LMFUnique::fetchAll
virtual std::list< std::unique_ptr< LMFUnique > > fetchAll() const noexcept(false)
Definition: LMFUnique.cc:61
std
Definition: JetResolutionObject.h:76
triggerObjects_cff.id
id
Definition: triggerObjects_cff.py:29
relativeConstraints.value
value
Definition: relativeConstraints.py:53
LMFUnique::Statement
oracle::occi::Statement Statement
Definition: LMFUnique.h:24
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
LMFUnique::exists
virtual bool exists()
Definition: LMFUnique.cc:158
LMFUnique::setString
LMFUnique & setString(std::string key, std::string value)
Definition: LMFUnique.cc:14
LMFUnique::sequencePostfix
std::string sequencePostfix(const Tm &t)
Definition: LMFUnique.cc:9
LMFUnique
Definition: LMFUnique.h:21
submitPVValidationJobs.t
string t
Definition: submitPVValidationJobs.py:644
crabWrapper.key
key
Definition: crabWrapper.py:19
cuy.ii
ii
Definition: cuy.py:590
LMFUnique::fetchAllSql
virtual std::string fetchAllSql(Statement *stmt) const
Definition: LMFUnique.cc:167
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37
LMFUnique::ResultSet
oracle::occi::ResultSet ResultSet
Definition: LMFUnique.h:23