CGEM BOSS 6.6.5.f
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 queryInterface (const InterfaceID &riid, void **ppvInterface)
 
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)
 
 DatabaseSvc (const std::string &name, ISvcLocator *sl)
 
virtual ~DatabaseSvc ()
 
virtual StatusCode queryInterface (const InterfaceID &riid, void **ppvInterface)
 
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)
 
- Public Member Functions inherited from IDatabaseSvc
virtual ~IDatabaseSvc ()
 
virtual int query (const std::string &dbName, const std::string &sql, DatabaseRecordVector &res)=0
 
virtual ~IDatabaseSvc ()
 
virtual int query (const std::string &dbName, const std::string &sql, DatabaseRecordVector &res)=0
 

Friends

class SvcFactory< DatabaseSvc >
 

Additional Inherited Members

- Static Public Member Functions inherited from IDatabaseSvc
static const InterfaceID & interfaceID ()
 
static const std::string & serviceInUse ()
 
static const InterfaceID & interfaceID ()
 
static const std::string & serviceInUse ()
 
- Static Protected Attributes inherited from IDatabaseSvc
static std::string g_serviceInUse = ""
 

Detailed Description

Constructor & Destructor Documentation

◆ DatabaseSvc() [1/2]

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

Definition at line 22 of file DatabaseSvc.cxx.

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

◆ ~DatabaseSvc() [1/2]

DatabaseSvc::~DatabaseSvc ( )
virtual

Definition at line 44 of file DatabaseSvc.cxx.

45{
46 if (dbInterface)
47 delete dbInterface;
48}

◆ DatabaseSvc() [2/2]

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

◆ ~DatabaseSvc() [2/2]

virtual DatabaseSvc::~DatabaseSvc ( )
virtual

Member Function Documentation

◆ finalize() [1/2]

StatusCode DatabaseSvc::finalize ( )
virtual

Definition at line 147 of file DatabaseSvc.cxx.

148{
149 MsgStream log( msgSvc(), name() );
150 StatusCode status = Service::finalize();
151 if ( ! status.isSuccess() ) {
152 log << MSG::ERROR << "Unable to finalize the Service" << endreq;
153 return status;
154 }
155
156 if(dbInterface)
157 {
158 if(dbInterface->is_connected())
159 dbInterface->disconnect();
160 delete dbInterface;
161 dbInterface = NULL;
162 }
163
164 log << MSG::INFO << "DatabaseSvc finalized successfully" << endreq;
165 return StatusCode::SUCCESS;
166}
virtual int disconnect()=0

◆ finalize() [2/2]

virtual StatusCode DatabaseSvc::finalize ( )
virtual

◆ initialize() [1/2]

StatusCode DatabaseSvc::initialize ( )
virtual

Definition at line 66 of file DatabaseSvc.cxx.

67{
68 StatusCode status = Service::initialize();
69 if ( !status.isSuccess() ) return status;
70
71 MsgStream log( msgSvc(), 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")
81 use_mysql = true;
82
83 if(m_dbType=="SQLITE")
84 use_sqlite = true;
85
86 log << MSG::DEBUG << "Using " << m_dbType
87 << " interface with options:" << endreq;
88
89 try {
90 if(use_mysql)
91 {
92 log << MSG::DEBUG << " dbHost " << m_dbHost << endreq;
93 log << MSG::DEBUG << " dbUser " << m_dbUser << endreq;
94 log << MSG::DEBUG << " dbPasswd " << m_dbPasswd << endreq;
95
96 dbInterface = new MysqlInterface();
97 dbInterface->set_host(m_dbHost);
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 << endreq;
104
105 dbInterface = new SqliteInterface();
106 dbInterface->set_dbpath(m_dbFilePath);
107 }
108 else
109 {
110 log << MSG::FATAL << "No valid database type is set. Please choose either MYSQL or SQLITE " << endreq;
111 return StatusCode::FAILURE;
112 }
113
114 if(m_dbReuseConnection)
115 log << MSG::DEBUG << "One connection per job is used" << endreq;
116 else
117 log << MSG::DEBUG << "One connection per query is used" << endreq;
118
119 if( m_dbReuseConnection )
120 {
121 dbInterface->set_reuse_connection(true);
122 dbInterface->connect();
123 }
124
125 } catch ( std::exception &e ) {
126
127 log << MSG::FATAL << "Exception in DataSvc initialization:" << endreq;
128 log << MSG::FATAL << "*** error message: " << e.what() << endreq;
129 return StatusCode::FAILURE;
130
131 } catch (char* mess) {
132 log << MSG::FATAL << "Exception DataSvc initialization caught: " << mess << endreq;
133 return StatusCode::FAILURE;
134 }
135 catch (...) {
136 log << MSG::FATAL << "UNKNOWN exception in DataSvc session initialization caught" << endreq;
137 return StatusCode::FAILURE;
138 }
139
140 log << MSG::INFO << "DatabaseSvc initialized successfully" << endreq;
141 return StatusCode::SUCCESS;
142}
virtual int connect()=0

◆ initialize() [2/2]

virtual StatusCode DatabaseSvc::initialize ( )
virtual

◆ query() [1/4]

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

Definition at line 284 of file DatabaseSvc.cxx.

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

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

◆ query() [2/4]

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

◆ query() [3/4]

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

Implements IDatabaseSvc.

Definition at line 246 of file DatabaseSvc.cxx.

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

◆ query() [4/4]

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

Implements IDatabaseSvc.

◆ queryInterface() [1/2]

StatusCode DatabaseSvc::queryInterface ( const InterfaceID &  riid,
void **  ppvInterface 
)
virtual

Definition at line 53 of file DatabaseSvc.cxx.

54{
55 if ( IID_IDatabaseSvc == riid ) {
56 *ppvInterface = static_cast< IDatabaseSvc* >( this );
57 return StatusCode::SUCCESS;
58 } else {
59 return Service::queryInterface( riid, ppvInterface );
60 }
61}

◆ queryInterface() [2/2]

virtual StatusCode DatabaseSvc::queryInterface ( const InterfaceID &  riid,
void **  ppvInterface 
)
virtual

Friends And Related Function Documentation

◆ SvcFactory< DatabaseSvc >


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