1#include "DatabaseSvc/DatabaseSvc.h"
2#include "DatabaseSvc/SqliteInterface.h"
5 #include "DatabaseSvc/DbInterface.h"
6 #include "GaudiKernel/MsgStream.h"
7 #include "GaudiKernel/SvcFactory.h"
9 #include "DatabaseSvc/MysqlInterface.h"
26 std::ostringstream error;
27 error <<
"There is another IDatabaseSvc registered with name "
29 throw "Error in DatabaseSvc: "+error.str();
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);
55 if ( IID_IDatabaseSvc == riid ) {
57 return StatusCode::SUCCESS;
59 return Service::queryInterface( riid, ppvInterface );
68 StatusCode status = Service::initialize();
69 if ( !status.isSuccess() )
return status;
71 MsgStream log(
msgSvc(), name() );
75 bool use_sqlite =
false;
76 bool use_mysql =
false;
78 std::transform(m_dbType.begin(), m_dbType.end(), m_dbType.begin(), ::toupper);
83 if(m_dbType==
"SQLITE")
86 log << MSG::DEBUG <<
"Using " << m_dbType
87 <<
" interface with options:" << endreq;
92 log << MSG::DEBUG <<
" dbHost " << m_dbHost << endreq;
93 log << MSG::DEBUG <<
" dbUser " << m_dbUser << endreq;
94 log << MSG::DEBUG <<
" dbPasswd " << m_dbPasswd << endreq;
103 log << MSG::DEBUG <<
" dbFilepath " << m_dbFilePath << endreq;
110 log << MSG::FATAL <<
"No valid database type is set. Please choose either MYSQL or SQLITE " << endreq;
111 return StatusCode::FAILURE;
114 if(m_dbReuseConnection)
115 log << MSG::DEBUG <<
"One connection per job is used" << endreq;
117 log << MSG::DEBUG <<
"One connection per query is used" << endreq;
119 if( m_dbReuseConnection )
125 }
catch ( std::exception &e ) {
127 log << MSG::FATAL <<
"Exception in DataSvc initialization:" << endreq;
128 log << MSG::FATAL <<
"*** error message: " << e.what() << endreq;
129 return StatusCode::FAILURE;
131 }
catch (
char* mess) {
132 log << MSG::FATAL <<
"Exception DataSvc initialization caught: " << mess << endreq;
133 return StatusCode::FAILURE;
136 log << MSG::FATAL <<
"UNKNOWN exception in DataSvc session initialization caught" << endreq;
137 return StatusCode::FAILURE;
140 log << MSG::INFO <<
"DatabaseSvc initialized successfully" << endreq;
141 return StatusCode::SUCCESS;
149 MsgStream log(
msgSvc(), name() );
150 StatusCode status = Service::finalize();
151 if ( ! status.isSuccess() ) {
152 log << MSG::ERROR <<
"Unable to finalize the Service" << endreq;
164 log << MSG::INFO <<
"DatabaseSvc finalized successfully" << endreq;
165 return StatusCode::SUCCESS;
193 bool exit_code =
true;
201 catch ( std::exception &e ) {
202 cerr <<
" Exception in DatabaseSvc initialization:" << endl
203 <<
" *** error message: " << e.what() << endl;
207 cerr <<
" Exception DatabaseSvc initialization caught: "
212 cerr <<
"UNKNOWN exception in DatabaseSvc session initialization caught"
236DatabaseSvc::SetDBFilePath(std::string dbFilePath)
238 m_dbFilePath = dbFilePath;
250 MsgStream log(
msgSvc(), name() );
258 int status = dbInterface->
query(dbName, sql, result);
262 log << MSG::ERROR <<
"Query " << sql <<
" failed" << endreq;
264 cout <<
"Query " << sql <<
" failed" << endl;
272 log << MSG::ERROR <<
"Could not execute query " << sql << endreq;
274 cout <<
"Could not execute query " << sql << endl;
279 return result.size();
287 MsgStream log(
msgSvc(), name() );
289 log << MSG::DEBUG <<
"Query database " << dbName <<
" SQL: " << sql << endreq;
293 int status = dbInterface->
query(dbName, sql);
297 log << MSG::ERROR <<
"Query " << sql <<
" failed" << endreq;
299 cerr <<
"Query " << sql <<
" failed" << endl;
307 log << MSG::ERROR <<
"Could not execute query " << sql << endreq;
309 cerr <<
"Could not execute query " << sql << endl;
int query(const std::string &dbName, const std::string &sql)
virtual StatusCode finalize()
DatabaseSvc(const std::string &name, ISvcLocator *sl)
virtual StatusCode queryInterface(const InterfaceID &riid, void **ppvInterface)
virtual StatusCode initialize()
void set_dbpath(std::string path)
void set_host(std::string host)
virtual int disconnect()=0
void set_passwd(std::string passwd)
virtual int query(std::string dbname, std::string query, DatabaseRecordVector &records)=0
void set_reuse_connection(bool flag)
void set_user(std::string user)
static std::string g_serviceInUse