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 
7 #include "KM3NeTDBClient.h"
8 
9 #include "JSystem/JStat.hh"
10 
11 
12 /**
13  * \author mdejong
14  */
15 
16 namespace KM3NETDB {
17 
18  using KM3NeT::DB::DBException;
19  using KM3NeT::DB::Server;
20  using KM3NeT::DB::Client;
21  using KM3NeT::DB::ResultSet;
22  using KM3NeT::DB::Selector;
23 
24  /**
25  * Cookie prefix.
26  */
27  static const char PREFIX_COOKIE = '_';
28 
29 
30  /**
31  * Auxiliary class for connection to data base.
32  */
33  class JDB :
34  public std::shared_ptr<Client>
35  {
36  public:
37  /**
38  * Get connection to database.
39  *
40  * \return database connection
41  */
42  static JDB& get()
43  {
44  static JDB db;
45 
46  return db;
47  }
48 
49 
50  /**
51  * Reset connection to database.
52  */
53  static void reset()
54  {
55  JDB& db = JDB::get();
56 
57  if (db.get() != NULL) {
58  db->Close();
59  db. reset();
60  }
61  }
62 
63 
64  /**
65  * Reset connection to database.
66  *
67  * \param usr user name
68  * \param pwd pass word
69  */
70  static void reset(const std::string& usr, const std::string& pwd)
71  {
72  static_cast<std::shared_ptr<Client>&>(JDB::get()) = Client::Create(Server::Default, usr.c_str(), pwd.c_str());
73  }
74 
75 
76  /**
77  * Reset connection to database.
78  *
79  * \param cookie persistent cookie
80  */
81  static void reset(const std::string& cookie)
82  {
83  using namespace std;
84  using namespace JPP;
85 
86  string buffer = cookie;
87 
88  if (getFileStatus(cookie.c_str())) {
89 
90  ifstream in(cookie.c_str());
91 
92  getline(in, buffer);
93 
94  string::size_type pos = buffer.find(PREFIX_COOKIE);
95 
96  if (pos != string::npos) {
97  buffer.erase(0,pos);
98  }
99 
100  in.close();
101  }
102 
103  static_cast<std::shared_ptr<Client>&>(JDB::get()) = Client::Create(Server::Default, buffer.c_str());
104  }
105 
106 
107  private:
108  /**
109  * Default constructor.
110  */
111  JDB()
112  {}
113 
114 
115  JDB(const JDB&);
116  JDB& operator=(const JDB&);
117  };
118 }
119 
120 #endif
Auxiliary class for connection to data base.
Definition: JDB.hh:33
static void reset(const std::string &usr, const std::string &pwd)
Reset connection to database.
Definition: JDB.hh:70
static JDB & get()
Get connection to database.
Definition: JDB.hh:42
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:468
JDB()
Default constructor.
Definition: JDB.hh:111
static JStat getFileStatus
Function object for file status.
Definition: JStat.hh:173
JDB & operator=(const JDB &)
static void reset()
Reset connection to database.
Definition: JDB.hh:53
static void reset(const std::string &cookie)
Reset connection to database.
Definition: JDB.hh:81
File status.
static const char PREFIX_COOKIE
Cookie prefix.
Definition: JDB.hh:27