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