Jpp  master_rocky-37-gf0c5bc59d
the software that should make you happy
JDBToolkit.hh
Go to the documentation of this file.
1 #ifndef __JDB_JDBTOOLKIT__
2 #define __JDB_JDBTOOLKIT__
3 
4 #include <string>
5 #include <vector>
6 #include <map>
7 #include <algorithm>
8 
9 #include "JLang/JException.hh"
10 #include "JLang/JLangToolkit.hh"
11 #include "JLang/JPredicate.hh"
12 
13 #include "JDB/JDB.hh"
14 #include "JDB/JDetectors.hh"
15 #include "JDB/JCLBID.hh"
16 #include "JDB/JUPI.hh"
17 #include "JDB/JUPI_t.hh"
18 #include "JDB/JSelector.hh"
20 
21 
22 /**
23  * \author mdejong
24  */
25 
26 namespace JDATABASE {};
27 namespace JPP { using namespace JDATABASE; }
28 
29 /**
30  * Auxiliary classes and methods for database I/O.
31  */
32 namespace JDATABASE {
33 
36  using JLANG::JType;
37 
38 
39  /**
40  * Auxiliary class for mapping serial number and object identifier of detectors.
41  */
43  public std::vector<JDetectors>
44  {
45  /**
46  * Initialise.
47  */
48  void initialise()
49  {
50  ResultSet& rs = JDB::get()->StreamDS(getTable<JDetectors>(), JSelector());
51 
52  for (JDetectors parameters; rs >> parameters; ) {
53  this->push_back(parameters);
54  }
55 
56  rs.Close();
57  }
58 
59 
60  /**
61  * Get detector.
62  *
63  * \param detid object identifier
64  * \return detector
65  */
66  const JDetectors& operator[](const std::string& detid)
67  {
68  using namespace std;
69  using namespace JPP;
70 
71  if (this->empty()) {
72  this->initialise();
73  }
74 
75  const_iterator p = find_if(this->begin(), this->end(), make_predicate(&JDetectors::OID, detid));
76 
77  if (p != this->end())
78  return *p;
79  else
80  THROW(JDatabaseException, "Invalid detector identifier " << detid);
81  }
82 
83 
84  /**
85  * Get detector.
86  *
87  * \param id serial number
88  * \return detector
89  */
90  const JDetectors& operator[](const int id)
91  {
92  using namespace std;
93  using namespace JPP;
94 
95  if (this->empty()) {
96  this->initialise();
97  }
98 
99  const_iterator p = find_if(this->begin(), this->end(), make_predicate(&JDetectors::SERIALNUMBER, id));
100 
101  if (p != this->end())
102  return *p;
103  else
104  THROW(JDatabaseException, "Invalid detector identifier " << id);
105  }
106 
107 
108  /**
109  * Get detector serial number.
110  *
111  * \param detid object identifier
112  * \return serial number
113  */
114  int operator()(const std::string& detid)
115  {
116  return (*this)[detid].SERIALNUMBER;
117  }
118 
119 
120  /**
121  * Get detector identifier.
122  *
123  * \param id serial number
124  * \return object identifier
125  */
126  inline std::string operator()(const int id)
127  {
128  return (*this)[id].OID;
129  }
130 
131 
132  /**
133  * Get serial number according given data type.
134  *
135  * \param detid object identifier or serial number
136  * \param type data type
137  * \return object identifier
138  */
139  int operator()(const std::string& detid, const JType<int>& type)
140  {
141  using namespace JPP;
142 
143  if (is_integer(detid))
144  return to_value<int>(detid);
145  else
146  return (*this)(detid);
147  }
148 
149 
150  /**
151  * Get detector identifier according given data type.
152  *
153  * \param detid object identifier or serial number
154  * \param type data type
155  * \return object identifier
156  */
157  std::string operator()(const std::string& detid, const JType<std::string>& type)
158  {
159  using namespace JPP;
160 
161  if (is_integer(detid))
162  return (*this)(to_value<int>(detid));
163  else
164  return detid;
165  }
166 
167 
168  /**
169  * Helper object.
170  */
172  };
173 
174 
175  /**
176  * Auxiliary class for mapping %UPI of central-logic board to module identifier.
177  */
178  struct JCLBIDHelper :
179  public std::vector<JCLBID>
180  {
181  /**
182  * Initialise.
183  */
184  void initialise()
185  {
186  ResultSet& rs = JDB::get()->StreamDS(getTable<JCLBID>(), getSelector<JCLBID>());
187 
188  for (JCLBID parameters; rs >> parameters; ) {
189  this->push_back(parameters);
190  }
191 
192  rs.Close();
193  }
194 
195 
196  /**
197  * Get module identifier.
198  *
199  * \param upi %UPI
200  * \return module identifier
201  */
202  int operator()(const JUPI_t& upi)
203  {
204  using namespace std;
205  using namespace JPP;
206 
207  if (this->empty()) {
208  this->initialise();
209  }
210 
211  const_iterator p = find_if(this->begin(), this->end(), make_predicate(&JCLBID::CLBUPI, upi));
212 
213  if (p != this->end())
214  return p->CLBID;
215  else
216  THROW(JDatabaseException, "Invalid UPI " << upi);
217  }
218 
219 
220  /**
221  * Get %UPI.
222  *
223  * \param id module identifier
224  * \return %UPI
225  */
226  JUPI_t operator()(const int id)
227  {
228  using namespace std;
229  using namespace JPP;
230 
231  if (this->empty()) {
232  this->initialise();
233  }
234 
235  const_iterator p = find_if(this->begin(), this->end(), make_predicate(&JCLBID::CLBID, id));
236 
237  if (p != this->end())
238  return p->CLBUPI;
239  else
240  THROW(JDatabaseException, "Invalid module identifier " << id);
241  }
242 
243 
244  /**
245  * Helper object.
246  */
248  };
249 
250 
251  /**
252  * Auxiliary class for mapping %PBS and serial number of product to %UPI.
253  */
254  struct JUPIHelper :
255  public std::map<JPBS_t, std::map<int, JUPI_t> >
256  {
257  /**
258  * Initialise.
259  *
260  * \param pbs %PBS
261  */
262  void initialise(const JPBS_t& pbs)
263  {
264  ResultSet& rs = JDB::get()->StreamDS(getTable<JUPI>(), getSelector<JUPI>(pbs));
265 
266  for (JUPI parameters; rs >> parameters; ) {
267 
268  const JUPI_t upi(parameters.PBS,
269  parameters.VARIANT,
270  parameters.VERSION,
271  parameters.SERIALNUMBER);
272 
273  (*this)[pbs][parameters.SERIALNUMBER] = upi;
274  }
275 
276  rs.Close();
277  }
278 
279 
280  /**
281  * Get %UPI.
282  *
283  * \param pbs %PBS
284  * \param number serial number
285  * \return %UPI
286  */
287  JUPI_t operator()(const JPBS_t& pbs, const int number)
288  {
289  const_iterator p = this->find(pbs);
290 
291  if (p == this->end()) {
292 
293  initialise(pbs);
294 
295  p = this->find(pbs);
296  }
297 
298  if (p != this->end()) {
299 
300  typename mapped_type::const_iterator q = p->second.find(number);
301 
302  if (q != p->second.end()) {
303 
304  return q->second;
305 
306  } else {
307 
308  THROW(JValueOutOfRange, "Invalid serial number " << number);
309  }
310 
311  } else {
312 
313  THROW(JDatabaseException, "Invalid PBS " << pbs);
314  }
315  }
316 
317 
318  /**
319  * Helper object.
320  */
322  };
323 
324 
325  /**
326  * Wrapper data structure for initialisation of fuction objects.
327  *
328  * The fuction objects will expand on the fly making a corresponding query to the database.\n
329  * To avoid making a query within another query, a given function object can <em>a priori</em>
330  * be initialised using the static method JDBToolkit::initialise.
331  */
332  struct JDBToolkit {
333  /**
334  * Initialise.
335  *
336  * \param helper helper object
337  * \param args values
338  */
339  template<class JHelper_t, class ...Args>
340  static void initialise(JHelper_t& helper, const Args& ...args)
341  {
342  helper.initialise(args...);
343  }
344 
345 
346  /**
347  * Initialise.
348  *
349  * \param pF helper function
350  * \param args values
351  */
352  template<class ...Args>
353  static void initialise(JDetectorsHelper& (*pF)(), const Args& ...args)
354  {
356  }
357 
358 
359  /**
360  * Initialise.
361  *
362  * \param pF helper function
363  * \param args values
364  */
365  template<class JHelper_t, class ...Args>
366  static void initialise(JHelper_t& (*pF)(), const Args& ...args)
367  {
368  JHelper_t::helper.initialise(args...);
369  }
370  };
371 
372 
373  /**
374  * Auxiliary function for helper object initialisation.
375  *
376  * \return helper object
377  */
379  {
381  }
382 
383 
384  /**
385  * Get detector identifier or serial number, depending on template data type.
386  *
387  * \param detid object identifier or serial number
388  * \return object identifier or serial number
389  */
390  template<class T>
391  inline T getDetector(const std::string& detid)
392  {
393  return JDetectorsHelper::helper(detid, JType<T>());
394  }
395 
396 
397  /**
398  * Get detector serial number.
399  *
400  * \param detid object identifier
401  * \return serial number
402  */
403  inline int getDetector(const std::string& detid)
404  {
405  return JDetectorsHelper::helper(detid);
406  }
407 
408 
409  /**
410  * Get detector identifier.
411  *
412  * \param id serial number
413  * \return object identifier
414  */
415  inline std::string getDetector(const int id)
416  {
417  return JDetectorsHelper::helper(id);
418  }
419 
420 
421  /**
422  * Auxiliary function for helper object initialisation.
423  *
424  * \return helper
425  */
427  {
428  return JCLBIDHelper::helper;
429  }
430 
431 
432  /**
433  * Get module identifier.
434  *
435  * \param upi %UPI
436  * \return module identifier
437  */
438  inline int getCLBID(const JUPI_t& upi)
439  {
440  return JCLBIDHelper::helper(upi);
441  }
442 
443 
444  /**
445  * Auxiliary function for helper object initialisation.
446  *
447  * \return helper
448  */
450  {
451  return JCLBIDHelper::helper;
452  }
453 
454 
455  /**
456  * Get %UPI.
457  *
458  * \param id module identifier
459  * \return %UPI
460  */
461  inline JUPI_t getCLBUPI(const int id)
462  {
463  return JCLBIDHelper::helper(id);
464  }
465 
466 
467  /**
468  * Auxiliary function for helper object initialisation.
469  *
470  * \return helper
471  */
472  inline JUPIHelper& getUPI()
473  {
474  return JUPIHelper::helper;
475  }
476 
477 
478  /**
479  * Get %UPI.
480  *
481  * \param pbs %PBS
482  * \param number serial number
483  * \return %UPI
484  */
485  inline JUPI_t getUPI(const JPBS_t& pbs, const int number)
486  {
487  return JUPIHelper::helper(pbs, number);
488  }
489 }
490 
491 #endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
static JDB & get()
Get connection to database.
Definition: JDB.hh:234
Auxiliary class for specifying selection of database data.
Database exception.
Definition: JException.hh:684
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:180
Auxiliary classes and methods for database I/O.
Definition: JAHRS.hh:14
std::string getDetector(const int id)
Get detector identifier.
Definition: JDBToolkit.hh:415
JUPI_t getCLBUPI(const int id)
Get UPI.
Definition: JDBToolkit.hh:461
JUPI_t getUPI(const JPBS_t &pbs, const int number)
Get UPI.
Definition: JDBToolkit.hh:485
int getCLBID(const JUPI_t &upi)
Get module identifier.
Definition: JDBToolkit.hh:438
bool is_integer(const std::string &buffer)
Check if string is an integer.
Definition: JLangToolkit.hh:58
JPredicate< JResult_t T::*, JComparison::eq > make_predicate(JResult_t T::*member, const JResult_t value)
Helper method to create predicate for data member.
Definition: JPredicate.hh:128
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JSTDTypes.hh:14
Auxiliary class for mapping UPI of central-logic board to module identifier.
Definition: JDBToolkit.hh:180
JUPI_t operator()(const int id)
Get UPI.
Definition: JDBToolkit.hh:226
int operator()(const JUPI_t &upi)
Get module identifier.
Definition: JDBToolkit.hh:202
void initialise()
Initialise.
Definition: JDBToolkit.hh:184
static JCLBIDHelper helper
Helper object.
Definition: JDBToolkit.hh:247
JUPI_t CLBUPI
Definition: JCLBID.hh:24
Wrapper data structure for initialisation of fuction objects.
Definition: JDBToolkit.hh:332
static void initialise(JHelper_t &helper, const Args &...args)
Initialise.
Definition: JDBToolkit.hh:340
static void initialise(JHelper_t &(*pF)(), const Args &...args)
Initialise.
Definition: JDBToolkit.hh:366
static void initialise(JDetectorsHelper &(*pF)(), const Args &...args)
Initialise.
Definition: JDBToolkit.hh:353
Auxiliary class for mapping serial number and object identifier of detectors.
Definition: JDBToolkit.hh:44
std::string operator()(const std::string &detid, const JType< std::string > &type)
Get detector identifier according given data type.
Definition: JDBToolkit.hh:157
static JDetectorsHelper helper
Helper object.
Definition: JDBToolkit.hh:171
int operator()(const std::string &detid, const JType< int > &type)
Get serial number according given data type.
Definition: JDBToolkit.hh:139
std::string operator()(const int id)
Get detector identifier.
Definition: JDBToolkit.hh:126
int operator()(const std::string &detid)
Get detector serial number.
Definition: JDBToolkit.hh:114
void initialise()
Initialise.
Definition: JDBToolkit.hh:48
const JDetectors & operator[](const std::string &detid)
Get detector.
Definition: JDBToolkit.hh:66
const JDetectors & operator[](const int id)
Get detector.
Definition: JDBToolkit.hh:90
Product breakdown structure (PBS).
Definition: JPBS_t.hh:29
Auxiliary class for mapping PBS and serial number of product to UPI.
Definition: JDBToolkit.hh:256
JUPI_t operator()(const JPBS_t &pbs, const int number)
Get UPI.
Definition: JDBToolkit.hh:287
static JUPIHelper helper
Helper object.
Definition: JDBToolkit.hh:321
void initialise(const JPBS_t &pbs)
Initialise.
Definition: JDBToolkit.hh:262
Universal product identifier (UPI).
Definition: JUPI_t.hh:32
Template definition for getting table specific selector.
Auxiliary class for a type holder.
Definition: JType.hh:19