6 #include "GaudiKernel/MsgStream.h"
7 #include "GaudiKernel/SvcFactory.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(
"Port", m_dbPort=3306);
38 declareProperty(
"SqliteDbPath",m_dbFilePath=
"");
39 declareProperty(
"DbType",m_dbType=
"SQLITE");
40 declareProperty(
"ReuseConnection",m_dbReuseConnection=
false);
56 if ( IID_IDatabaseSvc == riid ) {
58 return StatusCode::SUCCESS;
60 return Service::queryInterface( riid, ppvInterface );
69 StatusCode status = Service::initialize();
70 if ( !status.isSuccess() )
return status;
72 MsgStream log(
msgSvc(), name() );
76 bool use_sqlite =
false;
77 bool use_mysql =
false;
79 std::transform(m_dbType.begin(), m_dbType.end(), m_dbType.begin(), ::toupper);
84 if(m_dbType==
"SQLITE")
87 log << MSG::DEBUG <<
"Using " << m_dbType
88 <<
" interface with options:" << endreq;
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;
106 log << MSG::DEBUG <<
" dbFilepath " << m_dbFilePath << endreq;
113 log << MSG::FATAL <<
"No valid database type is set. Please choose either MYSQL or SQLITE " << endreq;
114 return StatusCode::FAILURE;
117 if(m_dbReuseConnection)
118 log << MSG::DEBUG <<
"One connection per job is used" << endreq;
120 log << MSG::DEBUG <<
"One connection per query is used" << endreq;
122 if( m_dbReuseConnection )
128 }
catch ( std::exception &e ) {
130 log << MSG::FATAL <<
"Exception in DataSvc initialization:" << endreq;
131 log << MSG::FATAL <<
"*** error message: " << e.what() << endreq;
132 return StatusCode::FAILURE;
134 }
catch (
char* mess) {
135 log << MSG::FATAL <<
"Exception DataSvc initialization caught: " << mess << endreq;
136 return StatusCode::FAILURE;
139 log << MSG::FATAL <<
"UNKNOWN exception in DataSvc session initialization caught" << endreq;
140 return StatusCode::FAILURE;
143 log << MSG::INFO <<
"DatabaseSvc initialized successfully" << endreq;
144 return StatusCode::SUCCESS;
152 MsgStream log(
msgSvc(), name() );
153 StatusCode status = Service::finalize();
154 if ( ! status.isSuccess() ) {
155 log << MSG::ERROR <<
"Unable to finalize the Service" << endreq;
167 log << MSG::INFO <<
"DatabaseSvc finalized successfully" << endreq;
168 return StatusCode::SUCCESS;
196 bool exit_code =
true;
204 catch ( std::exception &e ) {
205 cerr <<
" Exception in DatabaseSvc initialization:" << endl
206 <<
" *** error message: " << e.what() << endl;
210 cerr <<
" Exception DatabaseSvc initialization caught: "
215 cerr <<
"UNKNOWN exception in DatabaseSvc session initialization caught"
239DatabaseSvc::SetDBFilePath(std::string dbFilePath)
241 m_dbFilePath = dbFilePath;
253 MsgStream log(
msgSvc(), name() );
261 int status = dbInterface->
query(dbName, sql, result);
265 log << MSG::ERROR <<
"Query " << sql <<
" failed" << endreq;
267 cout <<
"Query " << sql <<
" failed" << endl;
275 log << MSG::ERROR <<
"Could not execute query " << sql << endreq;
277 cout <<
"Could not execute query " << sql << endl;
282 return result.size();
290 MsgStream log(
msgSvc(), name() );
292 log << MSG::DEBUG <<
"Query database " << dbName <<
" SQL: " << sql << endreq;
296 int status = dbInterface->
query(dbName, sql);
300 log << MSG::ERROR <<
"Query " << sql <<
" failed" << endreq;
302 cerr <<
"Query " << sql <<
" failed" << endl;
310 log << MSG::ERROR <<
"Could not execute query " << sql << endreq;
312 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