QwAnalysis
QwDatabase.h
Go to the documentation of this file.
1 /*!
2  * \file QwDatabase.h
3  * \brief A class for handling connections to the Qweak database.
4  *
5  * \author Damon Spayde
6  * \date 2010-01-07
7  */
8 
9 #ifndef QWDATABASE_HH
10 #define QWDATABASE_HH
11 
12 // System headers
13 #include <map>
14 #include <vector>
15 #include <string>
16 #include <typeinfo>
17 
18 // Third Party Headers
19 #include <mysql++.h>
20 #include "ssqls.h"
21 
22 
23 // ROOT headers
24 #include "TString.h"
25 
26 // Qweak headers
27 #include "QwTypes.h"
28 #include "QwLog.h"
29 #include "QwColor.h"
30 #include "QwOptions.h"
31 
32 // Forward declarations
33 
34 /**
35  * \class QwDatabase
36  * \ingroup QwAnalysis
37  * \brief A database interface class
38  *
39  * This class provides the connection to the Qweak database to other objects
40  * in the Qweak analyzer. A static global object gQwDatabase is used to
41  * provide these services.
42  *
43  */
44 
45 class QwDatabase: private mysqlpp::Connection {
46  public:
47 
48  QwDatabase(const string &major = "00", const string &minor = "00", const string &point = "0000"); //!< Simple constructor
49  QwDatabase(QwOptions &options, const string &major = "00", const string &minor = "00", const string &point = "0000"); //!< Constructor with QwOptions object
50 
51  virtual ~QwDatabase(); //!< Destructor
52 
53  void SetAccessLevel(string accesslevel); //!< Sets the access level flag based on string labels: "off", "ro", "rw".
54 
57 
58  Bool_t Connect(); //!< Open a connection to the database using the predefined parameters.
59  void Disconnect() {disconnect();}; //<! Close an open database connection
60  Bool_t Connected() { return connected(); }
61  const string GetServerVersion() {return server_version();}; //<! Get database server version
62  static void DefineOptions(QwOptions& options); //!< Defines available class options for QwOptions
63  void ProcessOptions(QwOptions &options); //!< Processes the options contained in the QwOptions object.
64  void ProcessOptions(const TString& dbname, const TString& username, const TString& passwd, const TString& dbhost="localhost", const Int_t dbport = 0, const TString& accesslevel = "ro"); //!< Processes database options
65 
66  mysqlpp::Query Query(const char *qstr=0 ) {return query(qstr);} //<! Generate a query to the database.
67  mysqlpp::Query Query(const std::string &qstr) {return query(qstr);} //<! Generate a query to the database.
68 
69 
70  const string GetVersion(); //! Return a full version string for the DB schema
71  const string GetVersionMajor() {return fVersionMajor;} //<! fVersionMajor getter
72  const string GetVersionMinor() {return fVersionMinor;} //<! fVersionMinor getter
73  const string GetVersionPoint() {return fVersionPoint;} //<! fVersionPoint getter
74  const string GetValidVersion(); //<! Return a full required version string.
75 
76  void PrintServerInfo(); //<! Print Server Information
77 
78  private:
80  private:
81 
82  Bool_t ValidateConnection(); //!< Checks that given connection parameters result in a valid connection
83  bool StoreDBVersion(); //!< Retrieve database schema version information from database
84 
85  // Do not allow compiler to automatically generated these functions
86  QwDatabase(const QwDatabase& rhs); //!< Copy Constructor (not implemented)
87  QwDatabase& operator= (const QwDatabase& rhs); //!< Assignment operator (not implemented)
88 // QwDatabase* operator& (); //!< Address-of operator (not implemented)
89 // const QwDatabase* operator& () const; //!< Address-of operator (not implemented)
90 
91  EQwDBAccessLevel fAccessLevel; //!< Access level of the database instance
92 
93  string fDatabase; //!< Name of database to connect to
94  string fDBServer; //!< Name of server carrying DB to connect to
95  string fDBUsername; //!< Name of account to connect to DB server with
96  string fDBPassword; //!< DB account password
97  UInt_t fDBPortNumber; //!< Port number to connect to on server (mysql default port is 3306)
98  Bool_t fValidConnection; //!< True if a valid connection was established using defined connection information
99 
100  string fVersionMajor; //!< Major version number of current DB schema
101  string fVersionMinor; //!< Minor version number of current DB schema
102  string fVersionPoint; //!< Point version number of current DB schema
103  const string kValidVersionMajor;
104  const string kValidVersionMinor;
105  const string kValidVersionPoint;
106 
107 };
108 
109 #endif
UInt_t fDBPortNumber
Port number to connect to on server (mysql default port is 3306)
Definition: QwDatabase.h:97
Bool_t ValidateConnection()
Checks that given connection parameters result in a valid connection.
Definition: QwDatabase.cc:84
QwDatabase(const string &major="00", const string &minor="00", const string &point="0000")
Simple constructor.
const string kValidVersionPoint
Definition: QwDatabase.h:105
void Disconnect()
Definition: QwDatabase.h:59
Bool_t Connect()
Open a connection to the database using the predefined parameters.
Definition: QwDatabase.cc:175
void ProcessOptions(QwOptions &options)
Processes the options contained in the QwOptions object.
Definition: QwDatabase.cc:225
Bool_t Connected()
Definition: QwDatabase.h:60
An options class.
Definition: QwOptions.h:133
QwDatabase & operator=(const QwDatabase &rhs)
Assignment operator (not implemented)
A database interface class.
Definition: QwDatabase.h:45
string fDBServer
Name of server carrying DB to connect to.
Definition: QwDatabase.h:94
const string GetValidVersion()
Definition: QwDatabase.cc:309
const string GetVersionMinor()
Definition: QwDatabase.h:72
virtual ~QwDatabase()
Destructor.
Definition: QwDatabase.cc:72
void SetAccessLevel(string accesslevel)
Sets the access level flag based on string labels: &quot;off&quot;, &quot;ro&quot;, &quot;rw&quot;.
Definition: QwDatabase.cc:264
string fVersionMajor
Major version number of current DB schema.
Definition: QwDatabase.h:100
const string GetVersion()
Definition: QwDatabase.cc:304
EQwDBAccessLevel fAccessLevel
Access level of the database instance.
Definition: QwDatabase.h:91
const string kValidVersionMajor
Definition: QwDatabase.h:103
const string GetVersionMajor()
Return a full version string for the DB schema.
Definition: QwDatabase.h:71
Bool_t AllowsWriteAccess()
Definition: QwDatabase.h:56
string fDBPassword
DB account password.
Definition: QwDatabase.h:96
A logfile class, based on an identical class in the Hermes analyzer.
const string kValidVersionMinor
Definition: QwDatabase.h:104
Bool_t AllowsReadAccess()
Definition: QwDatabase.h:55
Bool_t fValidConnection
True if a valid connection was established using defined connection information.
Definition: QwDatabase.h:98
string fVersionPoint
Point version number of current DB schema.
Definition: QwDatabase.h:102
mysqlpp::Query Query(const char *qstr=0)
Definition: QwDatabase.h:66
string fDBUsername
Name of account to connect to DB server with.
Definition: QwDatabase.h:95
const string GetVersionPoint()
Definition: QwDatabase.h:73
void PrintServerInfo()
Definition: QwDatabase.cc:283
string fVersionMinor
Minor version number of current DB schema.
Definition: QwDatabase.h:101
string fDatabase
Name of database to connect to.
Definition: QwDatabase.h:93
An options class which parses command line, config file and environment.
const string GetServerVersion()
Definition: QwDatabase.h:61
mysqlpp::Query Query(const std::string &qstr)
Definition: QwDatabase.h:67
static void DefineOptions(QwOptions &options)
Defines available class options for QwOptions.
Definition: QwDatabase.cc:209
bool StoreDBVersion()
Retrieve database schema version information from database.
Definition: QwDatabase.cc:318