Jpp  17.3.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JDB.hh
Go to the documentation of this file.
1 #ifndef __JDB_JDB__
2 #define __JDB_JDB__
3 
4 #include <string>
5 #include <istream>
6 #include <ostream>
7 #include <fstream>
8 #include <stdlib.h>
9 #include <vector>
10 #include <map>
11 #include <algorithm>
12 #include <memory>
13 
14 #include "dbclient/KM3NeTDBClient.h"
15 
16 #include "JSystem/JStat.hh"
17 #include "JLang/JManip.hh"
18 #include "JSon/JSon.hh"
19 
20 #include "JDB/JDBReader.hh"
21 
22 
23 /**
24  * \author mdejong
25  */
26 namespace JDATABASE {}
27 namespace JPP { using namespace JDATABASE; }
28 
29 namespace JDATABASE {
30 
31  using KM3NeT::DB::DBException;
32  using KM3NeT::DB::Server;
33  using KM3NeT::DB::Client;
34  using KM3NeT::DB::ResultSet;
35  using KM3NeT::DB::Selector;
37  using JSON::json;
38 
39 
40  /**
41  * Wrapper class for server name.
42  *
43  * By reading the name of the server, this class will automatically set the server in class JDB.
44  */
45  struct JServer :
46  public std::string
47  {
48  /**
49  * Default constructor.
50  */
52  {}
53 
54 
55  /**
56  * Constructor.
57  *
58  * \param server server name
59  */
60  JServer(const char* server) :
61  std::string(server)
62  {}
63  };
64 
65 
66  /**
67  * Named list of available database servers.
68  */
70  { "Default", Server::Default },
71  { "Test", Server::Test },
72  { "Lyon", Server::Lyon },
73  { "Lyon2", Server::Lyon2 },
74  { "Napoli", Server::Napoli }
75  };
76 
77 
78  /**
79  * Get server by name.
80  *
81  * \param server server name
82  * \return server
83  */
84  inline const Server& getServer(const std::string& server)
85  {
86  for (const auto& element : LIST_OF_SERVERS) {
87  if (element.first == server) {
88  return element.second;
89  }
90  }
91 
92  THROW(JDatabaseException, "Invalid server name " << server);
93  }
94 
95 
96  /**
97  * Get list of names of available database servers.
98  *
99  * \return server names
100  */
102  {
103  std::vector<JServer> buffer;
104 
105  const char* const server = getenv("DATABASE_SERVER");
106 
107  if (server != NULL) {
108  buffer.push_back(server);
109  }
110 
111  for (const auto& element : LIST_OF_SERVERS) {
112  if (std::find(buffer.begin(), buffer.end(), element.first) == buffer.end()) {
113  buffer.push_back(element.first);
114  }
115  }
116 
117  return buffer;
118  }
119 
120 
121  /**
122  * Get default server.
123  *
124  * The default server is defined by:
125  * -# named server from environment variable DATABASE_SERVER; else
126  * -# first server in LIST_OF_SERVERS if not empty; else
127  * -# default server Server::Default;
128  *
129  * \return server
130  */
131  inline const Server& getServer()
132  {
133  const char* const server = getenv("DATABASE_SERVER");
134 
135  if (server != NULL)
136  return getServer(server);
137  else if (!LIST_OF_SERVERS.empty())
138  return LIST_OF_SERVERS[0].second;
139  else
140  return Server::Default;
141  }
142 
143 
144  /**
145  * Get private cookie.
146  *
147  * \return cookie
148  */
149  inline const char* getPrivateCookie()
150  {
151  const char* home = getenv("HOME");
152 
153  return MAKE_CSTRING((home != NULL ? home : ".") << "/" << ".km3netdb_cookie");
154  }
155 
156 
157  /**
158  * Get public cookie.
159  *
160  * \return cookie
161  */
162  inline const char* getPublicCookie()
163  {
164  return getenv("DBCOOKIE");
165  }
166 
167 
168  /**
169  * Cookie prefix.
170  */
171  static const char PREFIX_COOKIE = '_';
172 
173 
174  /**
175  * This string is prepended to every parameter in the database output for the corresponding process.
176  */
177  static const std::string PREFIX_DATAFILTER = "DAQ_triggerParameters";
178  static const std::string PREFIX_ADF = "DAQ_ADF";
179 
180 
181  /**
182  * Auxiliary class for connection to data base.
183  */
184  class JDB :
185  public std::shared_ptr<Client>
186  {
187  private:
188  /**
189  * Get server.
190  *
191  * \return server
192  */
193  static inline Server& get_server()
194  {
195  static Server server = JDATABASE::getServer();
196 
197  return server;
198  }
199 
200  public:
201  /**
202  * Get server.
203  *
204  * \return server
205  */
206  static inline const Server& getServer()
207  {
208  return get_server();
209  }
210 
211 
212  /**
213  * Set server.
214  *
215  * \param server server
216  */
217  static inline void setServer(const Server& server)
218  {
219  get_server() = server;
220  }
221 
222 
223  /**
224  * Get connection to database.
225  *
226  * \return database connection
227  */
228  static JDB& get()
229  {
230  static JDB db;
231 
232  return db;
233  }
234 
235 
236  /**
237  * Reset connection to database.
238  */
239  static void reset()
240  {
241  JDB& db = JDB::get();
242 
243  if (db.get() != NULL) {
244  db->Close();
245  static_cast<std::shared_ptr<Client>&>(db).reset();
246  }
247  }
248 
249 
250  /**
251  * Reset connection to database.
252  *
253  * \param usr user name
254  * \param pwd pass word
255  */
256  static void reset(const std::string& usr,
257  const std::string& pwd)
258  {
259  static_cast<std::shared_ptr<Client>&>(JDB::get()) = Client::Create(getServer(), usr.c_str(), pwd.c_str());
260  }
261 
262 
263  /**
264  * Reset connection to database.
265  *
266  * \param cookie persistent cookie
267  */
268  static void reset(const std::string& cookie)
269  {
270  using namespace std;
271  using namespace JPP;
272 
273  string buffer = cookie;
274 
275  if (getFileStatus(cookie.c_str())) {
276 
277  ifstream in(cookie.c_str());
278 
279  getline(in, buffer);
280 
281  in.close();
282  }
283 
284  const string::size_type pos = buffer.find(PREFIX_COOKIE);
285 
286  if (pos != string::npos) {
287  buffer.erase(0, pos);
288  }
289 
290  static_cast<std::shared_ptr<Client>&>(JDB::get()) = Client::Create(getServer(), buffer.c_str());
291  }
292 
293 
294  /**
295  * Reset connection to database.
296  *
297  * An attempt to make a connection to the database is made in the following order,
298  * using:
299  * -# user name and password, if neither empty;
300  * -# given cookie, if not empty;
301  * -# private cookie file (created with JCookie.sh);
302  * -# public cookie (defined by environment variable DBCOOKIE);
303  *
304  * \param usr user name
305  * \param pwd password
306  * \param cookie persistent cookie
307  */
308  static void reset(const std::string& usr,
309  const std::string& pwd,
310  const std::string& cookie)
311  {
312  using namespace std;
313  using namespace JPP;
314 
315  if (usr != "" && pwd != "")
316  JDB::reset(usr, pwd);
317  else if (cookie != "")
318  JDB::reset(cookie);
319  else if (getFileStatus(getPrivateCookie()))
321  else if (getPublicCookie() != NULL)
323  else
324  THROW(JDatabaseException, "Missing user name / password or cookie file.");
325  }
326 
327 
328  private:
329  /**
330  * Default constructor.
331  */
332  JDB()
333  {}
334 
335 
336  JDB(const JDB&);
337  JDB& operator=(const JDB&);
338  };
339 
340 
341  /**
342  * Read server name from input stream.
343  *
344  * This operation will accordingly set the database server, if possible.
345  *
346  * \param in input stream
347  * \param server server
348  * \return input stream
349  */
350  inline std::istream& operator>>(std::istream& in, JServer& server)
351  {
352  if (in >> static_cast<std::string&>(server)) {
353  JDB::setServer(getServer(server));
354  }
355 
356  return in;
357  }
358 
359 
360  /**
361  * Get table name.
362  *
363  * \return table name
364  */
365  template<class JTable_t>
366  inline const char* getTable()
367  {
368  return JTable_t::getName();
369  }
370 
371 
372  /**
373  * Get column name.
374  *
375  * \param data_member data member
376  * \return column name
377  */
378  template<class JTable_t, class JType_t>
379  inline const char* getColumn(JType_t JTable_t::*data_member)
380  {
381  JTable_t* pc = NULL;
382  TClass* rc = dynamic_cast<TClass*>(TDictionary::GetDictionary(typeid(JTable_t)));
383 
384  if (rc != NULL) {
385 
386  for( std::unique_ptr<TIterator> i(rc->GetListOfDataMembers()->MakeIterator()); const TDataMember* p = (const TDataMember*) i->Next(); ) {
387 
388  if (p->GetOffset() == (char*) &(pc->*data_member) - (char*) pc) {
389  return p->GetName();
390  }
391  }
392  }
393 
394  return NULL;
395  }
396 
397 
398  /**
399  * Get column names.
400  *
401  * \return column names
402  */
403  template<class JTable_t>
405  {
406  using namespace JPP;
407 
409 
410  TClass* rc = dynamic_cast<TClass*>(TDictionary::GetDictionary(typeid(JTable_t)));
411 
412  if (rc != NULL) {
413 
414  for (std::unique_ptr<TIterator> i(rc->GetListOfDataMembers()->MakeIterator()); const TDataMember* p = (const TDataMember*) i->Next(); ) {
415  if (JRootClass::is_class(*p)) {
416  buffer.push_back(p->GetName());
417  }
418  }
419  }
420 
421  return buffer;
422  }
423 
424 
425  /**
426  * Get result set.
427  *
428  * \param query query / table name
429  * \return result set
430  */
431  inline ResultSet& getResultSet(const std::string& query)
432  {
433  return JDB::get()->StreamDS(query.c_str(), std::vector<Selector>());
434  }
435 
436 
437  /**
438  * Get result set.
439  *
440  * \param query query / table name
441  * \param selection selection
442  * \return result set
443  */
444  inline ResultSet& getResultSet(const std::string& query, const std::vector<Selector>& selection)
445  {
446  return JDB::get()->StreamDS(query.c_str(), selection);
447  }
448 
449 
450  /**
451  * Get result set.
452  *
453  * \param js json
454  * \param query query / table name
455  */
456  inline void to_json(json& js, const std::string& query)
457  {
458  *(JDB::get()->APIv2Select(query.c_str(), std::vector<Selector>())) >> js;
459  }
460 
461 
462  /**
463  * Get result set.
464  *
465  * \param js json
466  * \param query query / table name
467  * \param selection selection
468  */
469  inline void to_json(json& js, const std::string& query, const std::vector<Selector>& selection)
470  {
471  *(JDB::get()->APIv2Select(query.c_str(), selection)) >> js;
472  }
473 }
474 
475 #endif
static void reset()
Reset connection to database.
Definition: JDB.hh:239
static void reset(const std::string &cookie)
Reset connection to database.
Definition: JDB.hh:268
JServer(const char *server)
Constructor.
Definition: JDB.hh:60
const char * getTable()
Get table name.
Definition: JDB.hh:366
static std::vector< std::pair< JServer, Server > > LIST_OF_SERVERS
Named list of available database servers.
Definition: JDB.hh:69
static const Server & getServer()
Get server.
Definition: JDB.hh:206
Database exception.
Definition: JException.hh:666
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
JServer()
Default constructor.
Definition: JDB.hh:51
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:136
static const std::string PREFIX_ADF
Definition: JDB.hh:178
const char * getColumn(JType_t JTable_t::*data_member)
Get column name.
Definition: JDB.hh:379
static void setServer(const Server &server)
Set server.
Definition: JDB.hh:217
const Server & getServer(const std::string &server)
Get server by name.
Definition: JDB.hh:84
const char * getPrivateCookie()
Get private cookie.
Definition: JDB.hh:149
std::vector< std::string > getColumns()
Get column names.
Definition: JDB.hh:404
then awk string
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:478
static const char PREFIX_COOKIE
Cookie prefix.
Definition: JDB.hh:171
I/O manipulators.
static JStat getFileStatus
Function object for file status.
Definition: JStat.hh:173
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition: JHead.hh:1756
static const std::string PREFIX_DATAFILTER
This string is prepended to every parameter in the database output for the corresponding process...
Definition: JDB.hh:177
static void reset(const std::string &usr, const std::string &pwd, const std::string &cookie)
Reset connection to database.
Definition: JDB.hh:308
const char * getPublicCookie()
Get public cookie.
Definition: JDB.hh:162
Auxiliary class for connection to data base.
Definition: JDB.hh:184
nlohmann::json json
ResultSet & getResultSet(const std::string &query)
Get result set.
Definition: JDB.hh:431
std::vector< JServer > getServernames()
Get list of names of available database servers.
Definition: JDB.hh:101
Wrapper class for server name.
Definition: JDB.hh:45
static void reset(const std::string &usr, const std::string &pwd)
Reset connection to database.
Definition: JDB.hh:256
JDB()
Default constructor.
Definition: JDB.hh:332
const char * getName()
Get ROOT name of given data type.
Definition: JRootToolkit.hh:57
void to_json(json &js, const std::string &query)
Get result set.
Definition: JDB.hh:456
static Server & get_server()
Get server.
Definition: JDB.hh:193
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
static bool is_class(const char *const name)
Check name of class against ROOT classes.
Definition: JRootClass.hh:50
JDB & operator=(const JDB &)
File status.
static JDB & get()
Get connection to database.
Definition: JDB.hh:228