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