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