6 #include "GaudiKernel/MsgStream.h"
7 #include "GaudiKernel/SvcFactory.h"
27 std::ostringstream error;
28 error <<
"There is another IDatabaseSvc registered with name "
30 throw "Error in DatabaseSvc: "+error.str();
35 declareProperty(
"Host",m_dbHost=
"");
36 declareProperty(
"User",m_dbUser=
"guest");
37 declareProperty(
"Passwd",m_dbPasswd=
"guestpass");
38 declareProperty(
"Port", m_dbPort=3306);
39 declareProperty(
"SqliteDbPath",m_dbFilePath=
"");
40 declareProperty(
"DbType",m_dbType=
"SQLITE");
41 declareProperty(
"ReuseConnection",m_dbReuseConnection=
false);
70 StatusCode status = Service::initialize();
71 if ( !status.isSuccess() )
return status;
73 MsgStream log(
msgSvc(), name() );
77 bool use_sqlite =
false;
78 bool use_mysql =
false;
80 std::transform(m_dbType.begin(), m_dbType.end(), m_dbType.begin(), ::toupper);
85 if(m_dbType==
"SQLITE")
88 log << MSG::DEBUG <<
"Using " << m_dbType
89 <<
" interface with options:" << endreq;
94 log << MSG::DEBUG <<
" dbHost " << m_dbHost << endreq;
95 log << MSG::DEBUG <<
" dbPort " << m_dbPort << endreq;
96 log << MSG::DEBUG <<
" dbUser " << m_dbUser << endreq;
97 log << MSG::DEBUG <<
" dbPasswd " << m_dbPasswd << endreq;
107 log << MSG::DEBUG <<
" dbFilepath " << m_dbFilePath << endreq;
114 log << MSG::FATAL <<
"No valid database type is set. Please choose either MYSQL or SQLITE " << endreq;
115 return StatusCode::FAILURE;
118 if(m_dbReuseConnection)
119 log << MSG::DEBUG <<
"One connection per job is used" << endreq;
121 log << MSG::DEBUG <<
"One connection per query is used" << endreq;
123 if( m_dbReuseConnection )
129 }
catch ( std::exception &e ) {
131 log << MSG::FATAL <<
"Exception in DataSvc initialization:" << endreq;
132 log << MSG::FATAL <<
"*** error message: " << e.what() << endreq;
133 return StatusCode::FAILURE;
135 }
catch (
char* mess) {
136 log << MSG::FATAL <<
"Exception DataSvc initialization caught: " << mess << endreq;
137 return StatusCode::FAILURE;
140 log << MSG::FATAL <<
"UNKNOWN exception in DataSvc session initialization caught" << endreq;
141 return StatusCode::FAILURE;
144 log << MSG::INFO <<
"DatabaseSvc initialized successfully" << endreq;
145 return StatusCode::SUCCESS;
153 MsgStream log(
msgSvc(), name() );
154 StatusCode status = Service::finalize();
155 if ( ! status.isSuccess() ) {
156 log << MSG::ERROR <<
"Unable to finalize the Service" << endreq;
168 log << MSG::INFO <<
"DatabaseSvc finalized successfully" << endreq;
169 return StatusCode::SUCCESS;
197 bool exit_code =
true;
205 catch ( std::exception &e ) {
206 cerr <<
" Exception in DatabaseSvc initialization:" << endl
207 <<
" *** error message: " << e.what() << endl;
211 cerr <<
" Exception DatabaseSvc initialization caught: "
216 cerr <<
"UNKNOWN exception in DatabaseSvc session initialization caught"
240DatabaseSvc::SetDBFilePath(std::string dbFilePath)
242 m_dbFilePath = dbFilePath;
254 MsgStream log(
msgSvc(), name() );
262 int status = dbInterface->
query(dbName, sql, result);
266 log << MSG::ERROR <<
"Query " << sql <<
" failed" << endreq;
268 cout <<
"Query " << sql <<
" failed" << endl;
276 log << MSG::ERROR <<
"Could not execute query " << sql << endreq;
278 cout <<
"Could not execute query " << sql << endl;
283 return result.size();
291 MsgStream log(
msgSvc(), name() );
293 log << MSG::DEBUG <<
"Query database " << dbName <<
" SQL: " << sql << endreq;
297 int status = dbInterface->
query(dbName, sql);
301 log << MSG::ERROR <<
"Query " << sql <<
" failed" << endreq;
303 cerr <<
"Query " << sql <<
" failed" << endl;
311 log << MSG::ERROR <<
"Could not execute query " << sql << endreq;
313 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 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