Jpp
 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 <fstream>
6 #include <stdlib.h>
7 #include <vector>
8 
9 #include "dbclient/KM3NeTDBClient.h"
10 
11 #include "JSystem/JStat.hh"
12 #include "JLang/JManip.hh"
13 
14 #include "JDB/JDBReader.hh"
15 
16 
17 /**
18  * \author mdejong
19  */
20 namespace JDATABASE {}
21 namespace JPP { using namespace JDATABASE; }
22 
23 namespace JDATABASE {
24 
25  using KM3NeT::DB::DBException;
26  using KM3NeT::DB::Server;
27  using KM3NeT::DB::Client;
28  using KM3NeT::DB::ResultSet;
29  using KM3NeT::DB::Selector;
31 
32  /**
33  * Get private cookie.
34  *
35  * \return cookie
36  */
37  inline const char* getPrivateCookie()
38  {
39  const char* home = getenv("HOME");
40 
41  return MAKE_CSTRING((home != NULL ? home : ".") << "/" << ".km3netdb_cookie");
42  }
43 
44 
45  /**
46  * Get public cookie.
47  *
48  * \return cookie
49  */
50  inline const char* getPublicCookie()
51  {
52  return getenv("DBCOOKIE");
53  }
54 
55 
56  /**
57  * Cookie prefix.
58  */
59  static const char PREFIX_COOKIE = '_';
60 
61 
62  /**
63  * This string is prepended to every parameter in the database output for the corresponding process.
64  */
65  static const std::string PREFIX_DATAFILTER = "DAQ_triggerParameters";
66  static const std::string PREFIX_ADF = "DAQ_ADF";
67 
68 
69  /**
70  * Auxiliary class for connection to data base.
71  */
72  class JDB :
73  public std::shared_ptr<Client>
74  {
75  public:
76  /**
77  * Get connection to database.
78  *
79  * \return database connection
80  */
81  static JDB& get()
82  {
83  static JDB db;
84 
85  return db;
86  }
87 
88 
89  /**
90  * Reset connection to database.
91  */
92  static void reset()
93  {
94  JDB& db = JDB::get();
95 
96  if (db.get() != NULL) {
97  db->Close();
98  static_cast<std::shared_ptr<Client>&>(db).reset();
99  }
100  }
101 
102 
103  /**
104  * Reset connection to database.
105  *
106  * \param usr user name
107  * \param pwd pass word
108  */
109  static void reset(const std::string& usr,
110  const std::string& pwd)
111  {
112  static_cast<std::shared_ptr<Client>&>(JDB::get()) = Client::Create(Server::Default, usr.c_str(), pwd.c_str());
113  }
114 
115 
116  /**
117  * Reset connection to database.
118  *
119  * \param cookie persistent cookie
120  */
121  static void reset(const std::string& cookie)
122  {
123  using namespace std;
124  using namespace JPP;
125 
126  string buffer = cookie;
127 
128  if (getFileStatus(cookie.c_str())) {
129 
130  ifstream in(cookie.c_str());
131 
132  getline(in, buffer);
133 
134  in.close();
135  }
136 
137  const string::size_type pos = buffer.find(PREFIX_COOKIE);
138 
139  if (pos != string::npos) {
140  buffer.erase(0, pos);
141  }
142 
143  static_cast<std::shared_ptr<Client>&>(JDB::get()) = Client::Create(Server::Default, buffer.c_str());
144  }
145 
146 
147  /**
148  * Reset connection to database.
149  *
150  * An attempt to make a connection to the database is made in the following order,
151  * using:
152  * -# user name and password, if neither empty;
153  * -# given cookie, if not empty;
154  * -# private cookie file (created with JCookie.sh);
155  * -# public cookie (defined by environment variable DBCOOKIE);
156  *
157  * \param usr user name
158  * \param pwd password
159  * \param cookie persistent cookie
160  */
161  static void reset(const std::string& usr,
162  const std::string& pwd,
163  const std::string& cookie)
164  {
165  using namespace std;
166  using namespace JPP;
167 
168  if (usr != "" && pwd != "")
169  JDB::reset(usr, pwd);
170  else if (cookie != "")
171  JDB::reset(cookie);
172  else if (getFileStatus(getPrivateCookie()))
174  else if (getPublicCookie() != NULL)
176  else
177  THROW(JDatabaseException, "Missing user name / password or cookie file.");
178  }
179 
180 
181  private:
182  /**
183  * Default constructor.
184  */
185  JDB()
186  {}
187 
188 
189  JDB(const JDB&);
190  JDB& operator=(const JDB&);
191  };
192 
193 
194  /**
195  * Get table name.
196  *
197  * \return table name
198  */
199  template<class JTable_t>
200  inline const char* getTable()
201  {
202  return JTable_t::getName();
203  }
204 
205 
206  /**
207  * Get column name.
208  *
209  * \param data_member data member
210  * \return column name
211  */
212  template<class JTable_t, class JType_t>
213  inline const char* getColumn(JType_t JTable_t::*data_member)
214  {
215  JTable_t* pc = NULL;
216  TClass* rc = dynamic_cast<TClass*>(TDictionary::GetDictionary(typeid(JTable_t)));
217 
218  if (rc != NULL) {
219 
220  TIterator* i = rc->GetListOfDataMembers()->MakeIterator();
221 
222  for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) {
223 
224  if (p->GetOffset() == (char*) &(pc->*data_member) - (char*) pc) {
225  return p->GetName();
226  }
227  }
228  }
229 
230  return NULL;
231  }
232 
233 
234  /**
235  * Get column names.
236  *
237  * \return column names
238  */
239  template<class JTable_t>
241  {
242  using namespace JPP;
243 
245 
246  TClass* rc = dynamic_cast<TClass*>(TDictionary::GetDictionary(typeid(JTable_t)));
247 
248  if (rc != NULL) {
249 
250  TIterator* i = rc->GetListOfDataMembers()->MakeIterator();
251 
252  for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) {
253  if (JRootClass::is_class(*p)) {
254  buffer.push_back(p->GetName());
255  }
256  }
257  }
258 
259  return buffer;
260  }
261 
262 
263  /**
264  * Get result set.
265  *
266  * \param query query / table name
267  * \return result set
268  */
269  inline ResultSet& getResultSet(const std::string& query)
270  {
271  return JDB::get()->StreamDS(query.c_str(), std::vector<Selector>());
272  }
273 
274 
275  /**
276  * Get result set.
277  *
278  * \param query query / table name
279  * \param selection selection
280  * \return result set
281  */
282  inline ResultSet& getResultSet(const std::string& query, const std::vector<Selector>& selection)
283  {
284  return JDB::get()->StreamDS(query.c_str(), selection);
285  }
286 }
287 
288 #endif
static void reset()
Reset connection to database.
Definition: JDB.hh:92
static void reset(const std::string &cookie)
Reset connection to database.
Definition: JDB.hh:121
const char * getTable()
Get table name.
Definition: JDB.hh:200
Database exception.
Definition: JException.hh:648
static bool is_class(const char *const class_name)
Check class name against ROOT class names.
Definition: JRootClass.hh:48
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:151
static const std::string PREFIX_ADF
Definition: JDB.hh:66
const char * getColumn(JType_t JTable_t::*data_member)
Get column name.
Definition: JDB.hh:213
const char * getPrivateCookie()
Get private cookie.
Definition: JDB.hh:37
std::vector< std::string > getColumns()
Get column names.
Definition: JDB.hh:240
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:59
I/O manipulators.
static JStat getFileStatus
Function object for file status.
Definition: JStat.hh:173
static const std::string PREFIX_DATAFILTER
This string is prepended to every parameter in the database output for the corresponding process...
Definition: JDB.hh:65
static void reset(const std::string &usr, const std::string &pwd, const std::string &cookie)
Reset connection to database.
Definition: JDB.hh:161
const char * getPublicCookie()
Get public cookie.
Definition: JDB.hh:50
Auxiliary class for connection to data base.
Definition: JDB.hh:72
ResultSet & getResultSet(const std::string &query)
Get result set.
Definition: JDB.hh:269
static void reset(const std::string &usr, const std::string &pwd)
Reset connection to database.
Definition: JDB.hh:109
JDB()
Default constructor.
Definition: JDB.hh:185
const char * getName()
Get ROOT name of given data type.
Definition: JRootToolkit.hh:54
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 typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:36
JDB & operator=(const JDB &)
File status.
static JDB & get()
Get connection to database.
Definition: JDB.hh:81