BOSS 7.0.5
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("Port", m_dbPort=3306);
38 declareProperty("SqliteDbPath",m_dbFilePath="");
39 declareProperty("DbType",m_dbType="SQLITE");
40 declareProperty("ReuseConnection",m_dbReuseConnection=false);
41}

◆ ~DatabaseSvc() [1/2]

DatabaseSvc::~DatabaseSvc ( )
virtual

Definition at line 45 of file DatabaseSvc.cxx.

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

◆ 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 150 of file DatabaseSvc.cxx.

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

◆ finalize() [2/2]

virtual StatusCode DatabaseSvc::finalize ( )
virtual

◆ initialize() [1/2]

StatusCode DatabaseSvc::initialize ( )
virtual

Definition at line 67 of file DatabaseSvc.cxx.

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

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

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

◆ 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 54 of file DatabaseSvc.cxx.

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

◆ 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: