BOSS 8.0.0
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)
 ~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)
std::string & serviceInUse ()

Detailed Description

Definition at line 12 of file DatabaseSvc.h.

Constructor & Destructor Documentation

◆ DatabaseSvc()

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

Definition at line 23 of file DatabaseSvc.cxx.

24 : base_class( name, sl ), m_serviceInuse( "" ), dbInterface( nullptr ) {
25 // Set the name of this service
26 if ( m_serviceInuse != "" )
27 {
28 std::ostringstream error;
29 error << "There is another IDatabaseSvc registered with name " << m_serviceInuse
30 << std::ends;
31 throw "Error in DatabaseSvc: " + error.str();
32 }
33 m_serviceInuse = "DatabaseSvc";
34
35 // declare properties
36 declareProperty( "Host", m_dbHost = "" );
37 declareProperty( "User", m_dbUser = "guest" );
38 declareProperty( "Passwd", m_dbPasswd = "guestpass" );
39 declareProperty( "Port", m_dbPort = 3306 );
40 declareProperty( "SqliteDbPath", m_dbFilePath = "" );
41 declareProperty( "DbType", m_dbType = "SQLITE" );
42 declareProperty( "ReuseConnection", m_dbReuseConnection = false );
43}

Referenced by DatabaseSvc().

◆ ~DatabaseSvc()

DatabaseSvc::~DatabaseSvc ( )

Definition at line 47 of file DatabaseSvc.cxx.

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

Member Function Documentation

◆ finalize()

StatusCode DatabaseSvc::finalize ( )
virtual

Definition at line 146 of file DatabaseSvc.cxx.

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

◆ initialize()

StatusCode DatabaseSvc::initialize ( )
virtual

Definition at line 66 of file DatabaseSvc.cxx.

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

◆ query() [1/2]

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

Definition at line 267 of file DatabaseSvc.cxx.

267 {
268#ifndef BEAN
269 MsgStream log( msgSvc(), name() );
270
271 log << MSG::DEBUG << "Query database " << dbName << " SQL: " << sql << endmsg;
272#endif
273
274 try
275 {
276 int status = dbInterface->query( dbName, sql );
277 if ( status < 0 )
278 {
279#ifndef BEAN
280 log << MSG::ERROR << "Query " << sql << " failed" << endmsg;
281#else
282 cerr << "Query " << sql << " failed" << endl;
283#endif
284 return -1;
285 }
286 } catch ( ... )
287 {
288#ifndef BEAN
289 log << MSG::ERROR << "Could not execute query " << sql << endmsg;
290#else
291 cerr << "Could not execute query " << sql << endl;
292#endif
293 return -1;
294 }
295
296 return 0;
297}

◆ query() [2/2]

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

Definition at line 230 of file DatabaseSvc.cxx.

231 {
232#ifndef BEAN
233 MsgStream log( msgSvc(), name() );
234
235 // maqm log << MSG::DEBUG << "Query database " << dbName << " SQL: " << sql << endmsg;
236#endif
237
238 result.clear();
239
240 try
241 {
242 int status = dbInterface->query( dbName, sql, result ); // MysqlInterface::query
243 if ( status < 0 )
244 {
245#ifndef BEAN
246 log << MSG::ERROR << "Query " << sql << " failed" << endmsg;
247#else
248 cout << "Query " << sql << " failed" << endl;
249#endif
250 return -1;
251 }
252 } catch ( ... )
253 {
254#ifndef BEAN
255 log << MSG::ERROR << "Could not execute query " << sql << endmsg;
256#else
257 cout << "Could not execute query " << sql << endl;
258#endif
259 return -1;
260 }
261
262 return result.size();
263}

◆ serviceInUse()

std::string & DatabaseSvc::serviceInUse ( )
inline

Definition at line 55 of file DatabaseSvc.h.

55{ return m_serviceInuse; }

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