CMS 3D CMS Logo

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