StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StDbManagerImpl.hh
1 /***************************************************************************
2  *
3  * $Id: StDbManagerImpl.hh,v 1.10 2016/05/25 20:17:51 dmitry Exp $
4  *
5  * Author: R. Jeff Porter
6  ***************************************************************************
7  *
8  * Description: Manages access to Servers and passes Query-by-Table to db
9  * --> Now the Implementation of the virtual StDbManager class
10  *
11  ***************************************************************************
12  *
13  * $Log: StDbManagerImpl.hh,v $
14  * Revision 1.10 2016/05/25 20:17:51 dmitry
15  * coverity - uninit ctor
16  *
17  * Revision 1.9 2012/06/11 14:33:47 fisyak
18  * std namespace
19  *
20  * Revision 1.8 2011/02/10 17:30:42 dmitry
21  * added an option to blacklist domains
22  *
23  * Revision 1.7 2007/08/20 18:21:29 deph
24  * New Version of Load Balancer
25  *
26  * Revision 1.6 2007/01/09 16:27:40 deph
27  * Updates for load balancing "added 1)write privilege 2)xml comments 3)camelCase notation
28  *
29  * Revision 1.5 2006/11/16 21:50:40 deph
30  * additional files needed for db load balancing
31  *
32  * Revision 1.4 2006/08/17 02:58:57 deph
33  * updated load balancer - removing hard-coded nodes from API to xml
34  *
35  * Revision 1.3 2003/09/16 22:44:17 porter
36  * got rid of all ostrstream objects; replaced with ostringstream+string.
37  * modified rules.make and added file stdb_streams.h for standalone compilation
38  *
39  * Revision 1.2 2003/09/02 17:57:49 perev
40  * gcc 3.2 updates + WarnOff
41  *
42  * Revision 1.1 2001/01/22 18:37:56 porter
43  * Update of code needed in next year running. This update has little
44  * effect on the interface (only 1 method has been changed in the interface).
45  * Code also preserves backwards compatibility so that old versions of
46  * StDbLib can read new table structures.
47  * -Important features:
48  * a. more efficient low-level table structure (see StDbSql.cc)
49  * b. more flexible indexing for new systems (see StDbElememtIndex.cc)
50  * c. environment variable override KEYS for each database
51  * d. StMessage support & clock-time logging diagnostics
52  * -Cosmetic features
53  * e. hid stl behind interfaces (see new *Impl.* files) to again allow rootcint access
54  * f. removed codes that have been obsolete for awhile (e.g. db factories)
55  * & renamed some classes for clarity (e.g. tableQuery became StDataBaseI
56  * and mysqlAccessor became StDbSql)
57  *
58  **************************************************************************/
59 #ifndef STDBMANAGERIMPL_HH
60 #define STDBMANAGERIMPL_HH
61 
62 #include "StDbManager.hh"
63 #include "parseXmlString.hh"
64 #include "stdb_streams.h"
65 #include "StDbTime.h"
66 #include "StDataBaseI.hh"
67 #include "StDbTableFactory.hh"
68 #include "StDbMessService.hh"
69 #include "StDbConfigNode.hh"
70 #include "StDbTable.h"
71 #include "StDbLogger.hh"
72 #include "StDbServiceBroker.h"
73 
74 /********* helper classes (c-structs) *************/
75 class dbType{
76 public:
77  StDbType type;
78  char name[64];
79  dbType(StDbType atype,const char* aname): type(atype) {
80  if(aname)strcpy(name,aname);};
81 };
82 class dbDomain{
83 public:
84  StDbDomain domain;
85  char name[64];
86  dbDomain(StDbDomain adom,const char* aname): domain(adom) {
87  if(aname)strcpy(name,aname);};
88 };
89 
90 #define MAX_ENV_VARS 50
91 class dbEnvList {
92 public:
93  dbEnvList(): num(0) {
94  for ( int i = 0; i < MAX_ENV_VARS; i++) {
95  envVar[i] = 0; envDef[i] = 0;
96  }
97  };
98  ~dbEnvList() {
99  for(int i=0;i<num;i++){
100  delete [] envVar[i];
101  delete [] envDef[i];
102  }
103  }
104  int num;
105  char* envVar[MAX_ENV_VARS];
106  char* envDef[MAX_ENV_VARS];
107 };
108 /*************** end helper class definitions ****************/
109 
110 #include <list>
111 #include <set>
112 #ifdef ST_NO_TEMPLATE_DEF_ARGS
113 typedef list<dbType*,allocator<dbType*> > dbTypes;
114 typedef list<dbDomain*,allocator<dbDomain*> > dbDomains;
115 typedef list<StDbServer*,allocator<StDbServer*> > ServerList;
116 typedef set<std::string,allocator<std::string> > dbDomainBlacklist;
117 #else
118 #if !defined(ST_NO_NAMESPACES)
119 using std::list;
120 #endif
121 typedef list<dbType*> dbTypes;
122 typedef list<dbDomain*> dbDomains;
123 typedef list<StDbServer*> ServerList;
124 typedef std::set<std::string> dbDomainBlacklist;
125 #endif
126 
127 
128 
129 class StDbManagerImpl : public StDbManager {
130 
131  friend class StDbManager;
132 
133 private:
134 
135  dbTypes mTypes;
136  dbDomains mDomains;
137  StDbType dbTypeFree;
138  StDbDomain dbDomainFree;
139  ServerList mservers;
140  dbDomainBlacklist mBlacklist;
141  parseXmlString mparser;
142 
143  bool mhasServerList;
144  bool mhasDefaultServer;
145  StDbTime mcheckTime;
146  StDbTime mstoreTime;
147  StDbTableFactory* mfactory;
148  StDbLogger mnodeLog;
149  StDbLogger mdataLog;
150 
151 protected:
152 
153  // protected constructor
154  StDbManagerImpl();
155 
156 public:
157 
158  virtual ~StDbManagerImpl();
159 
160  virtual void turnOffTimeLogging();
161  // Factory class for Container of StDbTables..
162  virtual StDbConfigNode* initConfig(const char* databaseName);
163  virtual StDbConfigNode* initConfig(const char* databaseName,
164  const char* configName, int opt=0);
165  virtual StDbConfigNode* initConfig(StDbType type, StDbDomain domain);
166  virtual StDbConfigNode* initConfig(StDbType type, StDbDomain domain,
167  const char* configName, int opt=0);
168  virtual StDbConfigNode* initConfig(StDbType type, StDbDomain domain,
169  unsigned int requestTime, int opt=0);
170  virtual StDbConfigNode* initConfig(const char* databaseName,
171  unsigned int requestTime, int opt=0);
172 
173  virtual char* getConfigNodeName(StDbType type, StDbDomain domain);
174  virtual char* getExternalVersion(StDbType type,StDbDomain domain);
175  virtual dbEnvList* getEnvList(const char* name);
176 
177  virtual StDbTable* newDbTable(const char* dbName,const char* tabName);
178  virtual StDbTable* newDbTable(StDbNode* node);
179 
180  // find databases & servers by various methods
181  virtual StDataBaseI* findDb(StDbType type, StDbDomain domain);
182  virtual StDataBaseI* findDb(const char* dbType, const char* dbDomain);
183  virtual StDataBaseI* findDb(const char* databaseName);
184 
185  virtual StDbServer* findServer(StDbType type, StDbDomain domain);
186  virtual StDbServer* findServer(StDbNode* node);
187  virtual StDbServer* findServer(const char* dbType, const char* dbDomain);
188  virtual StDbServer* findServer(const char* databaseName);
189  virtual StDbServer* findDefaultServer();
190 
191  // helper functions to map enumeration to type
192  virtual char* getDbTypeName(StDbType type);
193  virtual char* getDbDomainName(StDbDomain domain);
194  virtual StDbType getDbType(const char* typeName);
195  virtual StDbDomain getDbDomain(const char* domainName);
196  virtual char* getDbName(const char* typeName,
197  const char* domainName);
198  virtual char* printDbName(StDbType type, StDbDomain domain);
199 
200  virtual void blacklistDbDomain(const char* domainName);
201 
202  // time stamp methods
203  virtual void setRequestTime(unsigned int time);
204  virtual void setRequestTime(const char* time);
205  virtual unsigned int getUnixRequestTime();
206  virtual char* getDateRequestTime();
207  virtual unsigned int getUnixCheckTime();
208  virtual char* getDateCheckTime();
209  virtual void setStoreTime(unsigned int time);
210  virtual void setStoreTime(const char* time);
211  virtual unsigned int getUnixStoreTime();
212  virtual char* getDateStoreTime();
213 
214  // find the dbType & dbDomain for a database=>dbname
215  virtual bool getDataBaseInfo(const char* dbname,
216  char*& type, char*& domain);
217  virtual bool getDataBaseInfo(const char* dbname,
218  StDbType& type, StDbDomain& domain);
219 
220  // fetch & store methods for data
221  virtual bool IsValid(StDbTable* table);
222  virtual bool fetchDbTable(StDbTable* table);
223  virtual bool fetchDbTable(StDbTable* table, char* whereClause);
224  virtual bool fetchAllTables(StDbConfigNode* node);
225  virtual bool storeDbTable(StDbTable* table, bool commitWhenDone=true);
226  virtual bool storeAllTables(StDbConfigNode* node, bool commitWhenDone=true);
227  virtual int storeConfig(StDbConfigNode* node, int currentID,
228  int& configID, bool commitWhenDone=true);
229 
230  // transaction methods
231  // nodes are node entries in the "Nodes" table
232  // tables are real data entries in a data+dataIndex table
233  virtual bool rollBackAllTables(StDbConfigNode* node);
234  virtual bool rollBackAllNodes(StDbConfigNode* node);
235  virtual bool rollBack(StDbNode* node);
236  virtual bool rollBack(StDbTable* table);
237  virtual bool commitAllTables(StDbConfigNode* node);
238  virtual bool commitAllNodes(StDbConfigNode* node);
239 
240  virtual void closeAllConnections();
241  virtual void closeAllConnections(StDbConfigNode* node);
242  virtual void closeConnection(StDbNode* node);
243 
244  virtual void printTimeStats();
245 
246 
247  short xmlInputSource;
248 
249  // MLK added for load balacing:
250 #ifndef NoXmlTreeReader
251  StDbServiceBroker* myServiceBroker;
252 
253 #endif
254 
255 protected:
256  // initializers for standard types & domains
257  virtual void initTypes();
258  virtual void initDomains();
259  virtual void addDbType(StDbType type, const char* typeName);
260  virtual void addDbDomain(StDbDomain domain, const char* domainName);
261 
262  // assigns an available user type or domain to a user supplied name
263  virtual StDbType newDbType(const char* typeName);
264  virtual StDbDomain newDbDomain(const char* domainName);
265 
266  virtual void deleteServers();
267  virtual void deleteTypes();
268  virtual void deleteDomains();
269 
270  // These lookup up ServerInfo from XML files
271  virtual void lookUpServers();
272  virtual void findServersXml(ifstream& is);
273  virtual char* findServerString(ifstream& is);
274  virtual char* getNextName(char*& name);
275 
276  // ensure both unix & date timestamp are in-sinc.
277  void updateDateTime(StDbTime& t);
278  void updateUnixTime(StDbTime& t);
279 
280 
281 };
282 
283 #endif
284 
285 
286 
287 
288 
289 
290 
291 
292 
virtual void closeAllConnections()
node commits
virtual bool commitAllNodes(StDbConfigNode *node)
table commits
virtual bool rollBack(StDbNode *node)
for node data
virtual bool commitAllTables(StDbConfigNode *node)
for table data
virtual void lookUpServers()
virtual bool rollBackAllNodes(StDbConfigNode *node)
for table data