Class Reference for E1039 Core & Analysis Software
DbSvc.h
Go to the documentation of this file.
1 #ifndef __DB_SVC_H__
2 #define __DB_SVC_H__
3 #include <vector>
4 #include <string>
5 #include <sstream>
6 #include <TSQLServer.h>
7 class TSQLServer;
8 class TSQLStatement;
9 
11 
15 class DbSvc {
16  public:
17  typedef enum { AutoSvr, DB1, DB2, DB3, DB4, LITE, LOCAL } SvrId_t;
18  typedef enum { AutoUsr, Guest, Prod } UsrId_t;
19  class VarList {
20  std::vector<std::string> m_name;
21  std::vector<std::string> m_type;
22  std::vector<bool> m_is_key;
23  public:
24  unsigned int Size() const { return m_name.size(); }
25  void Add(const std::string name, const std::string type, const bool is_key=false);
26  void Get(const int idx, std::string& name, std::string& type, bool& is_key) const;
27  };
28 
29  DbSvc(const SvrId_t svr_id=AutoSvr, const UsrId_t usr_id=AutoUsr, const std::string my_cnf="");
30  explicit DbSvc(const SvrId_t svr_id, std::string dbfile); // this is only for sqlite db
31  ~DbSvc();
32 
33  void UseSchema(const char* name, const bool do_create=false, const bool do_drop=false);
34  void UseSchema(const std::string name, const bool do_create=false, const bool do_drop=false) { UseSchema(name.c_str(), do_create, do_drop); }
35 
36  void DropTable (const char* name);
37  void DropTable (const std::string name) { DropTable(name.c_str()); }
38  bool HasTable(const char* name, const bool exit_on_false=false);
39  bool HasTable(const std::string name, const bool exit_on_false=false) { return HasTable(name.c_str(), exit_on_false); }
40  void CreateTable(const std::string name, const std::vector<std::string> list_var, const std::vector<std::string> list_type, const std::vector<std::string> list_key);
41  void CreateTable(const std::string name, const int n_var, const char** list_var, const char** list_type, const int n_key=0, const char** list_key=0);
42  void CreateTable(const std::string name, const VarList list);
43 
44  TSQLServer* Con() { return m_con; }
45  TSQLStatement* Process(const char* query);
46  TSQLStatement* Process(const std::string query) { return Process(query.c_str()); }
47 
48  private:
49  SvrId_t m_svr_id;
50  UsrId_t m_usr_id;
51  std::string m_svr;
52  std::string m_my_cnf;
53  TSQLServer* m_con;
54 
55  void SelectServer();
56  void SelectUser();
57  void ConnectServer();
58  bool FileExist(const std::string fileName);
59  std::string ExpandEnvironmentals( const std::string& input );
60 };
61 #endif // __DB_SVC_H__
void Get(const int idx, std::string &name, std::string &type, bool &is_key) const
Definition: DbSvc.cc:289
void Add(const std::string name, const std::string type, const bool is_key=false)
Definition: DbSvc.cc:282
unsigned int Size() const
Definition: DbSvc.h:24
Standard interface with SQL database.
Definition: DbSvc.h:15
~DbSvc()
Definition: DbSvc.cc:47
void UseSchema(const std::string name, const bool do_create=false, const bool do_drop=false)
Definition: DbSvc.h:34
DbSvc(const SvrId_t svr_id=AutoSvr, const UsrId_t usr_id=AutoUsr, const std::string my_cnf="")
Definition: DbSvc.cc:19
TSQLStatement * Process(const char *query)
Definition: DbSvc.cc:176
SvrId_t
Definition: DbSvc.h:17
@ LOCAL
Definition: DbSvc.h:17
@ LITE
Definition: DbSvc.h:17
@ DB4
Definition: DbSvc.h:17
@ DB3
Definition: DbSvc.h:17
@ AutoSvr
Definition: DbSvc.h:17
@ DB1
Definition: DbSvc.h:17
@ DB2
Definition: DbSvc.h:17
void DropTable(const char *name)
Definition: DbSvc.cc:82
void UseSchema(const char *name, const bool do_create=false, const bool do_drop=false)
Definition: DbSvc.cc:52
TSQLStatement * Process(const std::string query)
Definition: DbSvc.h:46
void CreateTable(const std::string name, const std::vector< std::string > list_var, const std::vector< std::string > list_type, const std::vector< std::string > list_key)
Definition: DbSvc.cc:102
UsrId_t
Definition: DbSvc.h:18
@ Guest
Definition: DbSvc.h:18
@ Prod
Definition: DbSvc.h:18
@ AutoUsr
Definition: DbSvc.h:18
bool HasTable(const std::string name, const bool exit_on_false=false)
Definition: DbSvc.h:39
void DropTable(const std::string name)
Definition: DbSvc.h:37
TSQLServer * Con()
Definition: DbSvc.h:44
bool HasTable(const char *name, const bool exit_on_false=false)
Definition: DbSvc.cc:92