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