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