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