BOSS 7.1.2
BESIII Offline Software System
Loading...
Searching...
No Matches
DatabaseSvc Class Reference

#include <DatabaseSvc.h>

+ Inheritance diagram for DatabaseSvc:

Public Member Functions

 DatabaseSvc (const std::string &name, ISvcLocator *sl)
 
virtual ~DatabaseSvc ()
 
virtual StatusCode initialize ()
 
virtual StatusCode finalize ()
 
int query (const std::string &dbName, const std::string &sql)
 
int query (const std::string &dbName, const std::string &sql, DatabaseRecordVector &res)
 

Friends

class SvcFactory< DatabaseSvc >
 

Detailed Description

Definition at line 13 of file DatabaseSvc.h.

Constructor & Destructor Documentation

◆ DatabaseSvc()

DatabaseSvc::DatabaseSvc ( const std::string & name,
ISvcLocator * sl )

Definition at line 23 of file DatabaseSvc.cxx.

23 : base_class( name, sl )
24{
25 // Set the name of this service
26 if ( IDatabaseSvc::g_serviceInUse != "" ) {
27 std::ostringstream error;
28 error << "There is another IDatabaseSvc registered with name "
29 << IDatabaseSvc::g_serviceInUse << std::ends;
30 throw "Error in DatabaseSvc: "+error.str();
31 }
32 IDatabaseSvc::g_serviceInUse = "DatabaseSvc";
33
34 // declare properties
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);
42}
static std::string g_serviceInUse
@ error
Definition Core.h:24

◆ ~DatabaseSvc()

DatabaseSvc::~DatabaseSvc ( )
virtual

Definition at line 46 of file DatabaseSvc.cxx.

47{
48 if (dbInterface)
49 delete dbInterface;
50}

Member Function Documentation

◆ finalize()

StatusCode DatabaseSvc::finalize ( )
virtual

Definition at line 151 of file DatabaseSvc.cxx.

152{
153 MsgStream log( msgSvc(), name() );
154 StatusCode status = Service::finalize();
155 if ( ! status.isSuccess() ) {
156 log << MSG::ERROR << "Unable to finalize the Service" << endreq;
157 return status;
158 }
159
160 if(dbInterface)
161 {
162 if(dbInterface->is_connected())
163 dbInterface->disconnect();
164 delete dbInterface;
165 dbInterface = NULL;
166 }
167
168 log << MSG::INFO << "DatabaseSvc finalized successfully" << endreq;
169 return StatusCode::SUCCESS;
170}
IMessageSvc * msgSvc()
#define NULL
bool is_connected()
Definition DbInterface.h:27
virtual int disconnect()=0

◆ initialize()

StatusCode DatabaseSvc::initialize ( )
virtual

Definition at line 68 of file DatabaseSvc.cxx.

69{
70 StatusCode status = Service::initialize();
71 if ( !status.isSuccess() ) return status;
72
73 MsgStream log( msgSvc(), name() );
75 setProperties();
76
77 bool use_sqlite = false;
78 bool use_mysql = false;
79
80 std::transform(m_dbType.begin(), m_dbType.end(), m_dbType.begin(), ::toupper);
81
82 if(m_dbType=="MYSQL")
83 use_mysql = true;
84
85 if(m_dbType=="SQLITE")
86 use_sqlite = true;
87
88 log << MSG::DEBUG << "Using " << m_dbType
89 << " interface with options:" << endreq;
90
91 try {
92 if(use_mysql)
93 {
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;
98
99 dbInterface = new MysqlInterface();
100 dbInterface->set_host(m_dbHost);
101 dbInterface->set_port(m_dbPort);
102 dbInterface->set_user(m_dbUser);
103 dbInterface->set_passwd(m_dbPasswd);
104 }
105 else if(use_sqlite)
106 {
107 log << MSG::DEBUG << " dbFilepath " << m_dbFilePath << endreq;
108
109 dbInterface = new SqliteInterface();
110 dbInterface->set_dbpath(m_dbFilePath);
111 }
112 else
113 {
114 log << MSG::FATAL << "No valid database type is set. Please choose either MYSQL or SQLITE " << endreq;
115 return StatusCode::FAILURE;
116 }
117
118 if(m_dbReuseConnection)
119 log << MSG::DEBUG << "One connection per job is used" << endreq;
120 else
121 log << MSG::DEBUG << "One connection per query is used" << endreq;
122
123 if( m_dbReuseConnection )
124 {
125 dbInterface->set_reuse_connection(true);
126 dbInterface->connect();
127 }
128
129 } catch ( std::exception &e ) {
130
131 log << MSG::FATAL << "Exception in DataSvc initialization:" << endreq;
132 log << MSG::FATAL << "*** error message: " << e.what() << endreq;
133 return StatusCode::FAILURE;
134
135 } catch (char* mess) {
136 log << MSG::FATAL << "Exception DataSvc initialization caught: " << mess << endreq;
137 return StatusCode::FAILURE;
138 }
139 catch (...) {
140 log << MSG::FATAL << "UNKNOWN exception in DataSvc session initialization caught" << endreq;
141 return StatusCode::FAILURE;
142 }
143
144 log << MSG::INFO << "DatabaseSvc initialized successfully" << endreq;
145 return StatusCode::SUCCESS;
146}
void set_dbpath(std::string path)
Definition DbInterface.h:32
void set_host(std::string host)
Definition DbInterface.h:29
void set_passwd(std::string passwd)
Definition DbInterface.h:31
void set_port(int port)
Definition DbInterface.h:33
void set_reuse_connection(bool flag)
Definition DbInterface.h:34
virtual int connect()=0
void set_user(std::string user)
Definition DbInterface.h:30

◆ query() [1/2]

int DatabaseSvc::query ( const std::string & dbName,
const std::string & sql )

Definition at line 288 of file DatabaseSvc.cxx.

289{
290#ifndef BEAN
291 MsgStream log( msgSvc(), name() );
292
293 log << MSG::DEBUG << "Query database " << dbName << " SQL: " << sql << endreq;
294#endif
295
296 try{
297 int status = dbInterface->query(dbName, sql);
298 if (status<0)
299 {
300#ifndef BEAN
301 log << MSG::ERROR << "Query " << sql << " failed" << endreq;
302#else
303 cerr << "Query " << sql << " failed" << endl;
304#endif
305 return -1;
306 }
307 }
308 catch(...)
309 {
310#ifndef BEAN
311 log << MSG::ERROR << "Could not execute query " << sql << endreq;
312#else
313 cerr << "Could not execute query " << sql << endl;
314#endif
315 return -1;
316 }
317
318 return 0;
319}
virtual int query(std::string dbname, std::string query, DatabaseRecordVector &records)=0

Referenced by FieldDBUtil::ConnectionDB::getReadSC_MagnetInfo().

◆ query() [2/2]

int DatabaseSvc::query ( const std::string & dbName,
const std::string & sql,
DatabaseRecordVector & res )

Definition at line 250 of file DatabaseSvc.cxx.

252{
253#ifndef BEAN
254 MsgStream log( msgSvc(), name() );
255
256 //maqm log << MSG::DEBUG << "Query database " << dbName << " SQL: " << sql << endreq;
257#endif
258
259 result.clear();
260
261 try{
262 int status = dbInterface->query(dbName, sql, result);
263 if (status<0)
264 {
265#ifndef BEAN
266 log << MSG::ERROR << "Query " << sql << " failed" << endreq;
267#else
268 cout << "Query " << sql << " failed" << endl;
269#endif
270 return -1;
271 }
272 }
273 catch(...)
274 {
275#ifndef BEAN
276 log << MSG::ERROR << "Could not execute query " << sql << endreq;
277#else
278 cout << "Could not execute query " << sql << endl;
279#endif
280 return -1;
281 }
282
283 return result.size();
284}

Friends And Related Symbol Documentation

◆ SvcFactory< DatabaseSvc >

friend class SvcFactory< DatabaseSvc >
friend

Definition at line 1 of file DatabaseSvc.h.


The documentation for this class was generated from the following files: