Jpp
JDB.hh
Go to the documentation of this file.
1 #ifndef __JDB_JDB__
2 #define __JDB_JDB__
3 
4 #include <string>
5 #include <fstream>
6 #include <stdlib.h>
7 #include <vector>
8 
9 #include <TDictionary.h>
10 #include <TClass.h>
11 #include <TDataMember.h>
12 #include <TIterator.h>
13 
14 #include "JSystem/JStat.hh"
15 #include "JLang/JRedirectString.hh"
16 #include "JLang/JNullStream.hh"
17 #include "JLang/JException.hh"
18 #include "JROOT/JRootClass.hh"
19 #include "JROOT/JRootDictionary.hh"
20 #include "JROOT/JRootStreamer.hh"
21 #include "Jeep/JPrint.hh"
22 
23 #include "KM3NeTDBClient.h"
24 
25 
26 /**
27  * \author mdejong
28  */
29 namespace JDATABASE {}
30 namespace JPP { using namespace JDATABASE; }
31 
32 namespace JDATABASE {
33 
34  using KM3NeT::DB::DBException;
35  using KM3NeT::DB::Server;
36  using KM3NeT::DB::Client;
37  using KM3NeT::DB::ResultSet;
38  using KM3NeT::DB::Selector;
40 
41  /**
42  * Get private cookie.
43  *
44  * \return cookie
45  */
46  inline const char* getPrivateCookie()
47  {
48  const char* home = getenv("HOME");
49 
50  return MAKE_CSTRING((home != NULL ? home : ".") << "/" << ".km3netdb_cookie");
51  }
52 
53 
54  /**
55  * Get public cookie.
56  *
57  * \return cookie
58  */
59  inline const char* getPublicCookie()
60  {
61  return getenv("DBCOOKIE");
62  }
63 
64 
65  /**
66  * Cookie prefix.
67  */
68  static const char PREFIX_COOKIE = '_';
69 
70 
71  /**
72  * Special characters.
73  */
74  static const std::string SPECIAL_CHARACTERS = "!@#$%^&*()-+=[]{};:'\"\\|,.<>/?";
75 
76 
77  /**
78  * This string is prepended to every parameter in the database output for the corresponding process.
79  */
80  static const std::string PREFIX_DATAFILTER = "DAQ_triggerParameters";
81  static const std::string PREFIX_ADF = "DAQ_ADF";
82 
83 
84  /**
85  * Auxiliary class for connection to data base.
86  */
87  class JDB :
88  public std::shared_ptr<Client>
89  {
90  public:
91  /**
92  * Get connection to database.
93  *
94  * \return database connection
95  */
96  static JDB& get()
97  {
98  static JDB db;
99 
100  return db;
101  }
102 
103 
104  /**
105  * Reset connection to database.
106  */
107  static void reset()
108  {
109  JDB& db = JDB::get();
110 
111  if (db.get() != NULL) {
112  db->Close();
113  static_cast<std::shared_ptr<Client>&>(db).reset();
114  }
115  }
116 
117 
118  /**
119  * Reset connection to database.
120  *
121  * \param usr user name
122  * \param pwd pass word
123  */
124  static void reset(const std::string& usr,
125  const std::string& pwd)
126  {
127  static_cast<std::shared_ptr<Client>&>(JDB::get()) = Client::Create(Server::Default, usr.c_str(), pwd.c_str());
128  }
129 
130 
131  /**
132  * Reset connection to database.
133  *
134  * \param cookie persistent cookie
135  */
136  static void reset(const std::string& cookie)
137  {
138  using namespace std;
139  using namespace JPP;
140 
141  string buffer = cookie;
142 
143  if (getFileStatus(cookie.c_str())) {
144 
145  ifstream in(cookie.c_str());
146 
147  getline(in, buffer);
148 
149  in.close();
150  }
151 
152  const string::size_type pos = buffer.find(PREFIX_COOKIE);
153 
154  if (pos != string::npos) {
155  buffer.erase(0, pos);
156  }
157 
158  static_cast<std::shared_ptr<Client>&>(JDB::get()) = Client::Create(Server::Default, buffer.c_str());
159  }
160 
161 
162  /**
163  * Reset connection to database.
164  *
165  * An attempt to make a connection to the database is made in the following order,
166  * using:
167  * -# user name and password, if neither empty;
168  * -# given cookie, if not empty;
169  * -# private cookie file (created with JCookie.sh);
170  * -# public cookie (defined by environment variable DBCOOKIE);
171  *
172  * \param usr user name
173  * \param pwd password
174  * \param cookie persistent cookie
175  */
176  static void reset(const std::string& usr,
177  const std::string& pwd,
178  const std::string& cookie)
179  {
180  using namespace std;
181  using namespace JPP;
182 
183  if (usr != "" && pwd != "")
184  JDB::reset(usr, pwd);
185  else if (cookie != "")
186  JDB::reset(cookie);
187  else if (getFileStatus(getPrivateCookie()))
189  else if (getPublicCookie() != NULL)
191  else
192  THROW(JDatabaseException, "Missing user name / password or cookie file.");
193  }
194 
195 
196  private:
197  /**
198  * Default constructor.
199  */
200  JDB()
201  {}
202 
203 
204  JDB(const JDB&);
205  JDB& operator=(const JDB&);
206  };
207 
208 
209  /**
210  * Auxiliary class to read ROOT object from database.
211  *
212  * Note that the list of columns associated to the template parameter is considered to be permanent.\n
213  * If not, the method JDBReader::reset should be called before the next operation.
214  */
215  template<class T>
216  struct JDBReader {
217  /**
218  * Default constructor.
219  */
221  object(),
222  reader(JLANG::null, JLANG::JEquationParameters(), JROOT::JRootDictionary::getInstance())
223  {}
224 
225 
226  /**
227  * Reset.
228  */
229  void reset()
230  {
231  buffer.clear();
232  }
233 
234 
235  /**
236  * Read ROOT object from result set.
237  *
238  * \param rs result set
239  * \return object
240  */
241  const T& operator()(ResultSet& rs)
242  {
243  using namespace std;
244  using namespace JPP;
245 
246  object = T();
247 
248  if (buffer.empty()) {
249 
250  JRootReadableClass cls(object);
251 
252  for (size_t i = 0; i != rs.FieldCount(); ++i) {
253 
254  string parameter = rs.FieldName(i);
255 
256  for (string::const_iterator i = SPECIAL_CHARACTERS.begin(); i != SPECIAL_CHARACTERS.end(); ++i) {
257  for (string::size_type pos; (pos = parameter.find(*i)) != string::npos; ) {
258  parameter.erase(pos);
259  }
260  }
261 
262  buffer.push_back(cls.find(parameter.c_str()));
263  }
264  }
265 
266  for (unsigned int i = 0; i != rs.FieldCount(); ++i) {
267 
268  if (buffer[i].is_valid()) {
269 
270  JRedirectString redirect(reader, rs.GetString(i));
271 
272  reader.getObject(buffer[i]);
273  }
274  }
275 
276  return object;
277  }
278 
279  private:
282 
283  std::vector<JROOT::JRootReadableClass> buffer; // field count -> data member
284  };
285 
286 
287  /**
288  * Read ROOT object from result set.
289  *
290  * \param rs result set
291  * \param object object
292  * \return true if read; else false
293  */
294  template<class T>
295  inline bool operator>>(ResultSet& rs, T& object)
296  {
297  static JDBReader<T> reader;
298 
299  if (rs.Next()) {
300 
301  object = reader(rs);
302 
303  return true;
304  }
305 
306  return false;
307  }
308 
309 
310  /**
311  * Read vector of ROOT objects from result set.
312  *
313  * \param rs result set
314  * \param buffer
315  * \return true if read; else false
316  */
317  template<class JElement_t, class JAllocator_t>
318  inline bool operator>>(ResultSet& rs, std::vector<JElement_t, JAllocator_t>& buffer)
319  {
320  for (JElement_t object; rs >> object; ) {
321  buffer.push_back(object);
322  }
323 
324  return true;
325  }
326 
327 
328  /**
329  * Get table name.
330  *
331  * \return table name
332  */
333  template<class JTable_t>
334  inline const char* getTable()
335  {
336  return JTable_t::getName();
337  }
338 
339 
340  /**
341  * Get column name.
342  *
343  * \param data_member data member
344  * \return column name
345  */
346  template<class JTable_t, class JType_t>
347  inline const char* getColumn(JType_t JTable_t::*data_member)
348  {
349  JTable_t* pc = NULL;
350  TClass* rc = dynamic_cast<TClass*>(TDictionary::GetDictionary(typeid(JTable_t)));
351 
352  if (rc != NULL) {
353 
354  TIterator* i = rc->GetListOfDataMembers()->MakeIterator();
355 
356  for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) {
357 
358  if (p->GetOffset() == (char*) &(pc->*data_member) - (char*) pc) {
359  return p->GetName();
360  }
361  }
362  }
363 
364  return NULL;
365  }
366 
367 
368  /**
369  * Get column names.
370  *
371  * \return column names
372  */
373  template<class JTable_t>
375  {
376  using namespace JPP;
377 
379 
380  TClass* rc = dynamic_cast<TClass*>(TDictionary::GetDictionary(typeid(JTable_t)));
381 
382  if (rc != NULL) {
383 
384  TIterator* i = rc->GetListOfDataMembers()->MakeIterator();
385 
386  for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) {
387  if (JRootClass::is_class(*p)) {
388  buffer.push_back(p->GetName());
389  }
390  }
391  }
392 
393  return buffer;
394  }
395 
396 
397  /**
398  * Get result set.
399  *
400  * \param query query / table name
401  * \return result set
402  */
403  inline ResultSet& getResultSet(const std::string& query)
404  {
405  return JDB::get()->StreamDS(query.c_str(), std::vector<Selector>());
406  }
407 
408 
409  /**
410  * Get result set.
411  *
412  * \param query query / table name
413  * \param selection selection
414  * \return result set
415  */
416  inline ResultSet& getResultSet(const std::string& query, const std::vector<Selector>& selection)
417  {
418  return JDB::get()->StreamDS(query.c_str(), selection);
419  }
420 }
421 
422 #endif
JException.hh
JLANG::JDatabaseException
Database exception.
Definition: JException.hh:648
JROOT
Auxiliary classes and methods for ROOT I/O.
Definition: JAbstractStreamer.hh:13
JDATABASE::getColumns
std::vector< std::string > getColumns()
Get column names.
Definition: JDB.hh:374
JPrint.hh
JLANG::getInstance
T & getInstance(const T &object)
Get static instance from temporary object.
Definition: JObject.hh:73
JDATABASE::getColumn
const char * getColumn(JType_t JTable_t::*data_member)
Get column name.
Definition: JDB.hh:347
JNullStream.hh
JROOT::JRootClass::is_class
static bool is_class(const char *const class_name)
Check class name against ROOT class names.
Definition: JRootClass.hh:47
JAANET::is_valid
bool is_valid(const T &value)
Check validity of given value.
Definition: JHead.hh:823
JDATABASE::JDB::reset
static void reset(const std::string &usr, const std::string &pwd, const std::string &cookie)
Reset connection to database.
Definition: JDB.hh:176
JRootStreamer.hh
std::vector< JROOT::JRootReadableClass >
JLANG::null
static JNullStream null
Null I/O stream.
Definition: JNullStream.hh:51
JDATABASE::JDB::operator=
JDB & operator=(const JDB &)
JDATABASE::JDB::get
static JDB & get()
Get connection to database.
Definition: JDB.hh:96
JDATABASE::getResultSet
ResultSet & getResultSet(const std::string &query, const std::vector< Selector > &selection)
Get result set.
Definition: JDB.hh:416
JDATABASE::operator>>
bool operator>>(ResultSet &rs, std::vector< JElement_t, JAllocator_t > &buffer)
Read vector of ROOT objects from result set.
Definition: JDB.hh:318
JDATABASE::JDB::reset
static void reset(const std::string &usr, const std::string &pwd)
Reset connection to database.
Definition: JDB.hh:124
JDATABASE
Auxiliary classes and methods for database I/O.
Definition: JAHRS.hh:12
JDATABASE::JDBReader::reader
JROOT::JRootReader reader
Definition: JDB.hh:281
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
MAKE_CSTRING
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:708
JDATABASE::JDB::reset
static void reset()
Reset connection to database.
Definition: JDB.hh:107
JDATABASE::JDB::JDB
JDB()
Default constructor.
Definition: JDB.hh:200
JDATABASE::getPrivateCookie
const char * getPrivateCookie()
Get private cookie.
Definition: JDB.hh:46
JRedirectString.hh
JSYSTEM::getFileStatus
static JStat getFileStatus
Function object for file status.
Definition: JStat.hh:173
THROW
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:669
JStat.hh
JDATABASE::JDBReader::JDBReader
JDBReader()
Default constructor.
Definition: JDB.hh:220
JDATABASE::JDB::reset
static void reset(const std::string &cookie)
Reset connection to database.
Definition: JDB.hh:136
JDATABASE::getTable
const char * getTable()
Get table name.
Definition: JDB.hh:334
JRootDictionary.hh
JROOT::JRootReader
Implementation for ASCII input of objects with ROOT dictionary.
Definition: JRootStreamer.hh:52
JDATABASE::JDBReader::operator()
const T & operator()(ResultSet &rs)
Read ROOT object from result set.
Definition: JDB.hh:241
JROOT::getName
const char * getName()
Get ROOT name of given data type.
Definition: JRootToolkit.hh:45
JDATABASE::JDBReader::object
T object
Definition: JDB.hh:280
std
Definition: jaanetDictionary.h:36
JDATABASE::PREFIX_COOKIE
static const char PREFIX_COOKIE
Cookie prefix.
Definition: JDB.hh:68
JDATABASE::PREFIX_ADF
static const std::string PREFIX_ADF
Definition: JDB.hh:81
JDATABASE::JDBReader
Auxiliary class to read ROOT object from database.
Definition: JDB.hh:216
JLANG
Auxiliary classes and methods for language specific functionality.
Definition: JAbstractClass.hh:10
JDATABASE::PREFIX_DATAFILTER
static const std::string PREFIX_DATAFILTER
This string is prepended to every parameter in the database output for the corresponding process.
Definition: JDB.hh:80
JDATABASE::getPublicCookie
const char * getPublicCookie()
Get public cookie.
Definition: JDB.hh:59
JDATABASE::JDB
Auxiliary class for connection to data base.
Definition: JDB.hh:87
JDATABASE::JDBReader::reset
void reset()
Reset.
Definition: JDB.hh:229
JLANG::getline
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:468
JDATABASE::JDBReader::buffer
std::vector< JROOT::JRootReadableClass > buffer
Definition: JDB.hh:283
JDATABASE::SPECIAL_CHARACTERS
static const std::string SPECIAL_CHARACTERS
Special characters.
Definition: JDB.hh:74
JRootClass.hh