StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StDbServerImpl.cc
1 /***************************************************************************
2  *
3  * $Id: StDbServerImpl.cc,v 1.4 2016/05/24 20:26:48 dmitry Exp $
4  *
5  * Author: R. Jeff Porter
6  ***************************************************************************
7  *
8  * Description: Server class for DB-access
9  *
10  ***************************************************************************
11  *
12  * $Log: StDbServerImpl.cc,v $
13  * Revision 1.4 2016/05/24 20:26:48 dmitry
14  * coverity - unreachable delete loop suppression
15  *
16  * Revision 1.3 2002/01/30 15:40:48 porter
17  * changed limits on flavor tag & made defaults retrieving more readable
18  *
19  * Revision 1.2 2001/02/08 23:23:56 porter
20  * fixed initialization of schemaID in table & fixed some warnings when
21  * compiled with NODEBUG
22  *
23  * Revision 1.1 2001/01/22 18:37:59 porter
24  * Update of code needed in next year running. This update has little
25  * effect on the interface (only 1 method has been changed in the interface).
26  * Code also preserves backwards compatibility so that old versions of
27  * StDbLib can read new table structures.
28  * -Important features:
29  * a. more efficient low-level table structure (see StDbSql.cc)
30  * b. more flexible indexing for new systems (see StDbElememtIndex.cc)
31  * c. environment variable override KEYS for each database
32  * d. StMessage support & clock-time logging diagnostics
33  * -Cosmetic features
34  * e. hid stl behind interfaces (see new *Impl.* files) to again allow rootcint access
35  * f. removed codes that have been obsolete for awhile (e.g. db factories)
36  * & renamed some classes for clarity (e.g. tableQuery became StDataBaseI
37  * and mysqlAccessor became StDbSql)
38  *
39  **************************************************************************/
40 #include "StDbServerImpl.hh"
41 #include "StDbSql.hh"
42 #define __CLASS__ "StDbServerImpl"
43 
45 
46 StDbServerImpl::StDbServerImpl(): StDbServer(), mcurrentDb(0){}
47 
48 StDbServerImpl::StDbServerImpl(const char* name, const char* host, const char* sock, int port): StDbServer(name,host,sock,port), mcurrentDb(0) {};
49 
50 StDbServerImpl::StDbServerImpl(StDbServerImpl& server) : StDbServer(server), mcurrentDb(0){}
51 
52 StDbServerImpl::~StDbServerImpl() { deleteDataBases(); }
53 
55 
56 void
57 StDbServerImpl::addDataBase(StDbType type, StDbDomain domain) {
58 
59  StDataBaseI* db=useDb(type,domain);
60  if(!db){
61  mcurrentDb = new StDbSql(Db,buff,type,domain);
62  if(!Db.IsConnected())Db.Connect(mhostName,muserName,mpword,mcurrentDb->printDbName(),mportNumber);
63  mcurrentDb->use();
64  mdataBases.push_back(mcurrentDb);
65  }
66 }
68 
69 void
70 StDbServerImpl::addDataBase(const char* type, const char* domain) {
71 
72  if(!useDb(type,domain)){
73  mcurrentDb = new StDbSql(Db,buff,type,domain);
74  if(!Db.IsConnected())Db.Connect(mhostName,muserName,mpword,mcurrentDb->printDbName(),mportNumber);
75  mcurrentDb->use();
76  mdataBases.push_back(mcurrentDb);
77  }
78 }
80 
82 StDbServerImpl::useDb(StDbType type, StDbDomain domain) {
83 
84 if(mcurrentDb &&
85  mcurrentDb->getDbType()==type &&
86  mcurrentDb->getDbDomain()==domain) return mcurrentDb;
87 
88  StDataBaseI* db=0; // return null if not found
89  for(DbList::iterator itr = mdataBases.begin();
90  itr != mdataBases.end(); ++itr){
91  if(type==(*itr)->getDbType() && domain==(*itr)->getDbDomain()){
92  mcurrentDb=*itr;
93  mcurrentDb->use();
94  db=mcurrentDb;
95  break;
96  }
97  }
98  return db;
99 }
101 
103 StDbServerImpl::useDb(const char* type, const char* domain) {
104 
105 if( mcurrentDb &&
106  mcurrentDb->checkDbType(type) &&
107  mcurrentDb->checkDbDomain(domain)) return mcurrentDb;
108 
109  StDataBaseI* db=0;
110  for(DbList::iterator itr = mdataBases.begin();
111  itr != mdataBases.end(); ++itr){
112  if((*itr)->checkDbType(type) && (*itr)->checkDbDomain(domain)){
113  mcurrentDb=*itr;
114  mcurrentDb->use();
115  db=mcurrentDb;
116  break;
117  }
118  }
119  return db;
120 }
121 
123 StDataBaseI*
124 StDbServerImpl::useDb(){
125 
126  if(!mcurrentDb){
127  mcurrentDb= new StDbSql(Db,buff,dbServer,dbStar);
128  mcurrentDb->use();
129  mdataBases.push_back(mcurrentDb);
130  }
131 
132  if(Db.IsConnected() ||
133  Db.Connect(mhostName,muserName,mpword,mcurrentDb->printDbName(),mportNumber)) return mcurrentDb;
134 
135  return (StDataBaseI*) NULL;
136 }
138 
139 void
140 StDbServerImpl::deleteDataBases() {
141 
142  for( auto &it : mdataBases ) delete it;
143  mdataBases.clear();
144 
145 }
147 
148 bool StDbServerImpl::isConnected() { return Db.IsConnected(); }
149 void StDbServerImpl::closeConnection(){ Db.Close(); }
150 
151 void StDbServerImpl::setTimeLogging(bool isTLog){ Db.mlogTime=isTLog; }
152 double StDbServerImpl::getQueryTimes() {return Db.mqueryLog.getTotalTimes();};
153 double StDbServerImpl::getSocketTimes(){return Db.msocketLog.getTotalTimes();};
154 double StDbServerImpl::getConnectTimes(){return Db.mconnectLog.getTotalTimes();};
155 
156 #undef __CLASS__