Jpp test-rotations-old
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
14#include "dbclient/KM3NeTDBClient.h"
15
16#include "JSystem/JStat.hh"
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 if (getFileStatus(cookie.c_str())) {
283
284 ifstream in(cookie.c_str());
285
286 getline(in, buffer);
287
288 in.close();
289 }
290
291 const string::size_type pos = buffer.find(PREFIX_COOKIE);
292
293 if (pos != string::npos) {
294 buffer.erase(0, pos);
295 }
296
297 static_cast<std::shared_ptr<Client>&>(JDB::get()) = Client::Create(getServer(), buffer.c_str());
298 }
299
300
301 /**
302 * Reset connection to database.
303 *
304 * An attempt to make a connection to the database is made in the following order,
305 * using:
306 * -# user name and password, if neither empty;
307 * -# given cookie, if not empty;
308 * -# private cookie file (created with JCookie.sh);
309 * -# public cookie (defined by environment variable DBCOOKIE);
310 *
311 * \param usr user name
312 * \param pwd password
313 * \param cookie persistent cookie
314 */
315 static void reset(const std::string& usr,
316 const std::string& pwd,
317 const std::string& cookie)
318 {
319 using namespace std;
320 using namespace JPP;
321
322 if (usr != "" && pwd != "")
323 JDB::reset(usr, pwd);
324 else if (cookie != "")
325 JDB::reset(cookie);
326 else if (getFileStatus(getPrivateCookie()))
328 else if (getPublicCookie() != NULL)
330 else
331 THROW(JDatabaseException, "Missing user name / password or cookie file.");
332 }
333
334
335 private:
336 /**
337 * Default constructor.
338 */
340 {}
341
342
343 JDB(const JDB&);
344 JDB& operator=(const JDB&);
345 };
346
347
348 /**
349 * Read server name from input stream.
350 *
351 * This operation will accordingly set the database server, if possible.
352 *
353 * \param in input stream
354 * \param server server
355 * \return input stream
356 */
357 inline std::istream& operator>>(std::istream& in, JServer& server)
358 {
359 if (in >> static_cast<std::string&>(server)) {
360 JDB::setServer(getServer(server));
361 }
362
363 return in;
364 }
365
366
367 /**
368 * Get table name.
369 *
370 * \return table name
371 */
372 template<class JTable_t>
373 inline const char* getTable()
374 {
375 return JTable_t::getName();
376 }
377
378
379 /**
380 * Get column name.
381 *
382 * \param data_member data member
383 * \return column name
384 */
385 template<class JTable_t, class JType_t>
386 inline const char* getColumn(JType_t JTable_t::*data_member)
387 {
388 JTable_t* pc = NULL;
389 TClass* rc = dynamic_cast<TClass*>(TDictionary::GetDictionary(typeid(JTable_t)));
390
391 if (rc != NULL) {
392
393 for( std::unique_ptr<TIterator> i(rc->GetListOfDataMembers()->MakeIterator()); const TDataMember* p = (const TDataMember*) i->Next(); ) {
394
395 if (p->GetOffset() == (char*) &(pc->*data_member) - (char*) pc) {
396 return p->GetName();
397 }
398 }
399 }
400
401 return NULL;
402 }
403
404
405 /**
406 * Get column names.
407 *
408 * \return column names
409 */
410 template<class JTable_t>
412 {
413 using namespace JPP;
414
416
417 TClass* rc = dynamic_cast<TClass*>(TDictionary::GetDictionary(typeid(JTable_t)));
418
419 if (rc != NULL) {
420
421 for (std::unique_ptr<TIterator> i(rc->GetListOfDataMembers()->MakeIterator()); const TDataMember* p = (const TDataMember*) i->Next(); ) {
422 if (JRoot::is_class(*p)) {
423 buffer.push_back(p->GetName());
424 }
425 }
426 }
427
428 return buffer;
429 }
430
431
432 /**
433 * Get result set.
434 *
435 * \param query query / table name
436 * \return result set
437 */
438 inline ResultSet& getResultSet(const std::string& query)
439 {
440 return JDB::get()->StreamDS(query.c_str(), std::vector<Selector>());
441 }
442
443
444 /**
445 * Get result set.
446 *
447 * \param query query / table name
448 * \param selection selection
449 * \return result set
450 */
451 inline ResultSet& getResultSet(const std::string& query, const std::vector<Selector>& selection)
452 {
453 return JDB::get()->StreamDS(query.c_str(), selection);
454 }
455
456
457 /**
458 * Get json.
459 *
460 * \param js json
461 * \param query query / table name
462 */
463 inline void get_json(json& js, const std::string& query)
464 {
465 *(JDB::get()->APIv2Select(APIV2_VERSION.toString().c_str(), query.c_str(), std::vector<Selector>())) >> js;
466 }
467
468
469 /**
470 * Get json.
471 *
472 * \param js json
473 * \param query query / table name
474 * \param selection selection
475 */
476 inline void get_json(json& js, const std::string& query, const std::vector<Selector>& selection)
477 {
478 *(JDB::get()->APIv2Select(APIV2_VERSION.toString().c_str(), query.c_str(), selection)) >> js;
479 }
480}
481
482#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:72
File status.
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:339
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:315
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:438
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:373
const Server & getServer()
Get default server.
Definition JDB.hh:138
std::vector< std::string > getColumns()
Get column names.
Definition JDB.hh:411
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:463
const char * getColumn(JType_t JTable_t::*data_member)
Get column name.
Definition JDB.hh:386
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.