BOSS 7.1.2
BESIII Offline Software System
Loading...
Searching...
No Matches
DatabaseSvc.cxx
Go to the documentation of this file.
3
4#ifndef BEAN
6 #include "GaudiKernel/MsgStream.h"
7 #include "GaudiKernel/SvcFactory.h"
8
10#else
11 #include <iostream>
12 #include <cstdlib>
13#endif
14
15#include <algorithm>
16
17using namespace std;
18
19#ifndef BEAN
20//----------------------------------------------------------------------------
21DECLARE_COMPONENT(DatabaseSvc)
22
23DatabaseSvc::DatabaseSvc( const std::string& name, ISvcLocator* sl ) : base_class( name, sl )
24{
25 // Set the name of this service
26 if ( IDatabaseSvc::g_serviceInUse != "" ) {
27 std::ostringstream error;
28 error << "There is another IDatabaseSvc registered with name "
29 << IDatabaseSvc::g_serviceInUse << std::ends;
30 throw "Error in DatabaseSvc: "+error.str();
31 }
32 IDatabaseSvc::g_serviceInUse = "DatabaseSvc";
33
34 // declare properties
35 declareProperty("Host",m_dbHost="");
36 declareProperty("User",m_dbUser="guest");
37 declareProperty("Passwd",m_dbPasswd="guestpass");
38 declareProperty("Port", m_dbPort=3306);
39 declareProperty("SqliteDbPath",m_dbFilePath="");
40 declareProperty("DbType",m_dbType="SQLITE");
41 declareProperty("ReuseConnection",m_dbReuseConnection=false);
42}
43
44//----------------------------------------------------------------------------
45
47{
48 if (dbInterface)
49 delete dbInterface;
50}
51
52//----------------------------------------------------------------------------
53
54/*StatusCode
55DatabaseSvc::queryInterface( const InterfaceID& riid, void** ppvInterface )
56{
57 if ( IID_IDatabaseSvc == riid ) {
58 *ppvInterface = static_cast< IDatabaseSvc* >( this );
59 return StatusCode::SUCCESS;
60 } else {
61 return Service::queryInterface( riid, ppvInterface );
62 }
63}
64*/
65//----------------------------------------------------------------------------
66
67StatusCode
69{
70 StatusCode status = Service::initialize();
71 if ( !status.isSuccess() ) return status;
72
73 MsgStream log( msgSvc(), name() );
75 setProperties();
76
77 bool use_sqlite = false;
78 bool use_mysql = false;
79
80 std::transform(m_dbType.begin(), m_dbType.end(), m_dbType.begin(), ::toupper);
81
82 if(m_dbType=="MYSQL")
83 use_mysql = true;
84
85 if(m_dbType=="SQLITE")
86 use_sqlite = true;
87
88 log << MSG::DEBUG << "Using " << m_dbType
89 << " interface with options:" << endreq;
90
91 try {
92 if(use_mysql)
93 {
94 log << MSG::DEBUG << " dbHost " << m_dbHost << endreq;
95 log << MSG::DEBUG << " dbPort " << m_dbPort << endreq;
96 log << MSG::DEBUG << " dbUser " << m_dbUser << endreq;
97 log << MSG::DEBUG << " dbPasswd " << m_dbPasswd << endreq;
98
99 dbInterface = new MysqlInterface();
100 dbInterface->set_host(m_dbHost);
101 dbInterface->set_port(m_dbPort);
102 dbInterface->set_user(m_dbUser);
103 dbInterface->set_passwd(m_dbPasswd);
104 }
105 else if(use_sqlite)
106 {
107 log << MSG::DEBUG << " dbFilepath " << m_dbFilePath << endreq;
108
109 dbInterface = new SqliteInterface();
110 dbInterface->set_dbpath(m_dbFilePath);
111 }
112 else
113 {
114 log << MSG::FATAL << "No valid database type is set. Please choose either MYSQL or SQLITE " << endreq;
115 return StatusCode::FAILURE;
116 }
117
118 if(m_dbReuseConnection)
119 log << MSG::DEBUG << "One connection per job is used" << endreq;
120 else
121 log << MSG::DEBUG << "One connection per query is used" << endreq;
122
123 if( m_dbReuseConnection )
124 {
125 dbInterface->set_reuse_connection(true);
126 dbInterface->connect();
127 }
128
129 } catch ( std::exception &e ) {
130
131 log << MSG::FATAL << "Exception in DataSvc initialization:" << endreq;
132 log << MSG::FATAL << "*** error message: " << e.what() << endreq;
133 return StatusCode::FAILURE;
134
135 } catch (char* mess) {
136 log << MSG::FATAL << "Exception DataSvc initialization caught: " << mess << endreq;
137 return StatusCode::FAILURE;
138 }
139 catch (...) {
140 log << MSG::FATAL << "UNKNOWN exception in DataSvc session initialization caught" << endreq;
141 return StatusCode::FAILURE;
142 }
143
144 log << MSG::INFO << "DatabaseSvc initialized successfully" << endreq;
145 return StatusCode::SUCCESS;
146}
147
148//----------------------------------------------------------------------------
149
150StatusCode
152{
153 MsgStream log( msgSvc(), name() );
154 StatusCode status = Service::finalize();
155 if ( ! status.isSuccess() ) {
156 log << MSG::ERROR << "Unable to finalize the Service" << endreq;
157 return status;
158 }
159
160 if(dbInterface)
161 {
162 if(dbInterface->is_connected())
163 dbInterface->disconnect();
164 delete dbInterface;
165 dbInterface = NULL;
166 }
167
168 log << MSG::INFO << "DatabaseSvc finalized successfully" << endreq;
169 return StatusCode::SUCCESS;
170}
171
172#else
173// -------------------------- BEAN ------------------------------------
174
175DatabaseSvc* DatabaseSvc::m_db = 0;
176
178{
179 // m_dbFilePath="unknown";
180 if( !this->initialize() ) {
181 exit(0);
182 }
183}
184
185//----------------------------------------------------------------------------
186
188{
189 this->finalize();
190}
191
192//----------------------------------------------------------------------------
193
194bool
196{
197 bool exit_code = true;
198
199 try {
200 dbInterface = new SqliteInterface();
201 dbInterface->set_dbpath(m_dbFilePath);
202 dbInterface->set_reuse_connection(true);
203 dbInterface->connect();
204 }
205 catch ( std::exception &e ) {
206 cerr << " Exception in DatabaseSvc initialization:" << endl
207 << " *** error message: " << e.what() << endl;
208 exit_code = false;
209 }
210 catch (char* mess) {
211 cerr << " Exception DatabaseSvc initialization caught: "
212 << mess << endl;
213 exit_code = false;
214 }
215 catch (...) {
216 cerr << "UNKNOWN exception in DatabaseSvc session initialization caught"
217 << endl;
218 exit_code = false;
219 }
220
221 return exit_code;
222}
223
224//----------------------------------------------------------------------------
225
226void
228{
229 if( dbInterface ) {
230 if(dbInterface->is_connected())
231 dbInterface->disconnect();
232 delete dbInterface;
233 dbInterface = 0;
234 }
235}
236
237//----------------------------------------------------------------------------
238
239void
240DatabaseSvc::SetDBFilePath(std::string dbFilePath)
241{
242 m_dbFilePath = dbFilePath;
243 dbInterface->set_dbpath(m_dbFilePath);
244}
245
246#endif
247//----------------------------------------------------------------------------
248
249int
250DatabaseSvc::query(const std::string& dbName, const std::string& sql,
251 DatabaseRecordVector& result)
252{
253#ifndef BEAN
254 MsgStream log( msgSvc(), name() );
255
256 //maqm log << MSG::DEBUG << "Query database " << dbName << " SQL: " << sql << endreq;
257#endif
258
259 result.clear();
260
261 try{
262 int status = dbInterface->query(dbName, sql, result);
263 if (status<0)
264 {
265#ifndef BEAN
266 log << MSG::ERROR << "Query " << sql << " failed" << endreq;
267#else
268 cout << "Query " << sql << " failed" << endl;
269#endif
270 return -1;
271 }
272 }
273 catch(...)
274 {
275#ifndef BEAN
276 log << MSG::ERROR << "Could not execute query " << sql << endreq;
277#else
278 cout << "Could not execute query " << sql << endl;
279#endif
280 return -1;
281 }
282
283 return result.size();
284}
285
286//----------------------------------------------------------------------------
287
288int DatabaseSvc::query(const std::string& dbName, const std::string& sql)
289{
290#ifndef BEAN
291 MsgStream log( msgSvc(), name() );
292
293 log << MSG::DEBUG << "Query database " << dbName << " SQL: " << sql << endreq;
294#endif
295
296 try{
297 int status = dbInterface->query(dbName, sql);
298 if (status<0)
299 {
300#ifndef BEAN
301 log << MSG::ERROR << "Query " << sql << " failed" << endreq;
302#else
303 cerr << "Query " << sql << " failed" << endl;
304#endif
305 return -1;
306 }
307 }
308 catch(...)
309 {
310#ifndef BEAN
311 log << MSG::ERROR << "Could not execute query " << sql << endreq;
312#else
313 cerr << "Could not execute query " << sql << endl;
314#endif
315 return -1;
316 }
317
318 return 0;
319}
320
321//----------------------------------------------------------------------------
IMessageSvc * msgSvc()
#define NULL
int query(const std::string &dbName, const std::string &sql)
virtual StatusCode finalize()
DatabaseSvc(const std::string &name, ISvcLocator *sl)
virtual StatusCode initialize()
virtual ~DatabaseSvc()
void set_dbpath(std::string path)
Definition DbInterface.h:32
void set_host(std::string host)
Definition DbInterface.h:29
bool is_connected()
Definition DbInterface.h:27
virtual int disconnect()=0
void set_passwd(std::string passwd)
Definition DbInterface.h:31
void set_port(int port)
Definition DbInterface.h:33
virtual int query(std::string dbname, std::string query, DatabaseRecordVector &records)=0
void set_reuse_connection(bool flag)
Definition DbInterface.h:34
virtual int connect()=0
void set_user(std::string user)
Definition DbInterface.h:30
static std::string g_serviceInUse