DatabaseSvc Class Reference

#include <DatabaseSvc.h>

Inheritance diagram for DatabaseSvc:

IDatabaseSvc List of all members.

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)

Static Public Member Functions

static const InterfaceID & interfaceID ()
static const std::stringserviceInUse ()

Static Protected Attributes

static std::string g_serviceInUse = ""

Private Attributes

DbInterfacedbInterface
std::string m_dbName
std::string m_dbHost
std::string m_dbUser
std::string m_dbPasswd
std::string m_dbFilePath
std::string m_dbType
bool m_dbReuseConnection

Friends

class SvcFactory< DatabaseSvc >

Detailed Description

Definition at line 14 of file DatabaseSvc.h.


Constructor & Destructor Documentation

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

Definition at line 22 of file DatabaseSvc.cxx.

References ers::error, IDatabaseSvc::g_serviceInUse, m_dbFilePath, m_dbHost, m_dbPasswd, m_dbReuseConnection, m_dbType, and m_dbUser.

00022                                                                  : Service( name, sl )
00023 {
00024   // Set the name of this service
00025   if ( IDatabaseSvc::g_serviceInUse != "" ) {
00026     std::ostringstream error;
00027     error << "There is another IDatabaseSvc registered with name "
00028           << IDatabaseSvc::g_serviceInUse << std::ends;
00029     throw "Error in DatabaseSvc: "+error.str();
00030   }
00031   IDatabaseSvc::g_serviceInUse = "DatabaseSvc";
00032 
00033   // declare properties
00034   declareProperty("Host",m_dbHost="");
00035   declareProperty("User",m_dbUser="guest");
00036   declareProperty("Passwd",m_dbPasswd="guestpass");
00037   declareProperty("SqliteDbPath",m_dbFilePath="");
00038   declareProperty("DbType",m_dbType="SQLITE");
00039   declareProperty("ReuseConnection",m_dbReuseConnection=false);
00040 }

DatabaseSvc::~DatabaseSvc (  )  [virtual]

Definition at line 44 of file DatabaseSvc.cxx.

References dbInterface.

00045 {
00046   if (dbInterface)
00047     delete dbInterface;
00048 }


Member Function Documentation

StatusCode DatabaseSvc::finalize (  )  [virtual]

Definition at line 147 of file DatabaseSvc.cxx.

References dbInterface, DbInterface::disconnect(), calibUtil::ERROR, Bes_Common::INFO, DbInterface::is_connected(), and msgSvc().

00148 {
00149   MsgStream log( msgSvc(), name() );
00150   StatusCode status = Service::finalize();
00151   if ( ! status.isSuccess() ) {
00152     log << MSG::ERROR << "Unable to finalize the Service" << endreq;
00153     return status;
00154   }
00155 
00156   if(dbInterface)
00157     {
00158       if(dbInterface->is_connected())
00159         dbInterface->disconnect();
00160       delete dbInterface;
00161       dbInterface = NULL;
00162     }
00163 
00164   log << MSG::INFO << "DatabaseSvc finalized successfully" << endreq;
00165   return StatusCode::SUCCESS;
00166 }

StatusCode DatabaseSvc::initialize (  )  [virtual]

Definition at line 66 of file DatabaseSvc.cxx.

References DbInterface::connect(), dbInterface, Bes_Common::DEBUG, Bes_Common::FATAL, IDatabaseSvc::g_serviceInUse, Bes_Common::INFO, m_dbFilePath, m_dbHost, m_dbPasswd, m_dbReuseConnection, m_dbType, m_dbUser, msgSvc(), DbInterface::set_dbpath(), DbInterface::set_host(), DbInterface::set_passwd(), DbInterface::set_reuse_connection(), and DbInterface::set_user().

00067 {
00068   StatusCode status = Service::initialize();
00069   if ( !status.isSuccess() ) return status;
00070 
00071   MsgStream log( msgSvc(), name() );
00072   IDatabaseSvc::g_serviceInUse = name();
00073   setProperties();
00074 
00075   bool use_sqlite = false;
00076   bool use_mysql = false;
00077 
00078   std::transform(m_dbType.begin(), m_dbType.end(), m_dbType.begin(), ::toupper);
00079 
00080   if(m_dbType=="MYSQL")
00081     use_mysql = true;
00082 
00083   if(m_dbType=="SQLITE")
00084     use_sqlite = true;
00085 
00086   log << MSG::DEBUG << "Using " << m_dbType
00087       << " interface with options:" << endreq;
00088 
00089   try {
00090     if(use_mysql)
00091       {
00092         log << MSG::DEBUG << "  dbHost " << m_dbHost << endreq;
00093         log << MSG::DEBUG << "  dbUser " << m_dbUser << endreq;
00094         log << MSG::DEBUG << "  dbPasswd " << m_dbPasswd << endreq;
00095 
00096         dbInterface = new MysqlInterface();
00097         dbInterface->set_host(m_dbHost);
00098         dbInterface->set_user(m_dbUser);
00099         dbInterface->set_passwd(m_dbPasswd);
00100       }
00101     else if(use_sqlite)
00102       {
00103         log << MSG::DEBUG << "  dbFilepath " << m_dbFilePath << endreq;
00104 
00105         dbInterface = new SqliteInterface();
00106         dbInterface->set_dbpath(m_dbFilePath);
00107       }
00108     else
00109       {
00110         log << MSG::FATAL << "No valid database type is set. Please choose either MYSQL or SQLITE " << endreq;
00111         return StatusCode::FAILURE;
00112       }
00113 
00114     if(m_dbReuseConnection)
00115       log << MSG::DEBUG << "One connection per job is used" << endreq;
00116     else
00117       log << MSG::DEBUG << "One connection per query is used" << endreq;
00118 
00119     if( m_dbReuseConnection )
00120       {
00121         dbInterface->set_reuse_connection(true);
00122         dbInterface->connect();
00123       }
00124 
00125   } catch ( std::exception &e ) {
00126 
00127     log << MSG::FATAL << "Exception in DataSvc initialization:" << endreq;
00128     log << MSG::FATAL << "***  error message: " << e.what()         << endreq;
00129     return StatusCode::FAILURE;
00130 
00131   } catch (char* mess) {
00132     log << MSG::FATAL << "Exception DataSvc initialization caught: " << mess << endreq;
00133     return StatusCode::FAILURE;
00134   }
00135   catch (...) {
00136     log << MSG::FATAL << "UNKNOWN exception in DataSvc session initialization caught" << endreq;
00137     return StatusCode::FAILURE;
00138   }
00139 
00140   log << MSG::INFO << "DatabaseSvc initialized successfully" << endreq;
00141   return StatusCode::SUCCESS;
00142 }

static const InterfaceID& IDatabaseSvc::interfaceID (  )  [inline, static, inherited]

Definition at line 22 of file IDatabaseSvc.h.

References IID_IDatabaseSvc().

00022 { return IID_IDatabaseSvc; }

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

Implements IDatabaseSvc.

Definition at line 246 of file DatabaseSvc.cxx.

References DatabaseRecordVector::clear(), dbInterface, calibUtil::ERROR, msgSvc(), and DbInterface::query().

00248 {
00249 #ifndef BEAN
00250   MsgStream log( msgSvc(), name() );
00251 
00252   //maqm log << MSG::DEBUG << "Query database " << dbName << " SQL: " << sql << endreq;
00253 #endif
00254 
00255   result.clear();
00256 
00257   try{
00258     int status = dbInterface->query(dbName, sql, result);
00259     if (status<0)
00260       {
00261 #ifndef BEAN
00262         log << MSG::ERROR << "Query " << sql << " failed" << endreq;
00263 #else
00264         cout << "Query " << sql << " failed" << endl;
00265 #endif
00266         return -1;
00267       }
00268   }
00269   catch(...)
00270     {
00271 #ifndef BEAN
00272       log << MSG::ERROR << "Could not execute query " << sql << endreq;
00273 #else
00274       cout << "Could not execute query " << sql << endl;
00275 #endif
00276       return -1;
00277     }
00278 
00279   return result.size();
00280 }

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

Definition at line 284 of file DatabaseSvc.cxx.

References dbInterface, Bes_Common::DEBUG, calibUtil::ERROR, msgSvc(), and DbInterface::query().

00285 {
00286 #ifndef BEAN
00287   MsgStream log( msgSvc(), name() );
00288 
00289   log << MSG::DEBUG << "Query database " << dbName << " SQL: " << sql << endreq;
00290 #endif
00291 
00292   try{
00293     int status = dbInterface->query(dbName, sql);
00294     if (status<0)
00295       {
00296 #ifndef BEAN
00297         log << MSG::ERROR << "Query " << sql << " failed" << endreq;
00298 #else
00299         cerr << "Query " << sql << " failed" << endl;
00300 #endif
00301         return -1;
00302       }
00303   }
00304   catch(...)
00305     {
00306 #ifndef BEAN
00307       log << MSG::ERROR << "Could not execute query " << sql << endreq;
00308 #else
00309       cerr << "Could not execute query " << sql << endl;
00310 #endif
00311       return -1;
00312     }
00313 
00314   return 0;
00315 }

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

Definition at line 53 of file DatabaseSvc.cxx.

References IID_IDatabaseSvc().

00054 {
00055   if ( IID_IDatabaseSvc == riid ) {
00056     *ppvInterface = static_cast< IDatabaseSvc* >( this );
00057     return StatusCode::SUCCESS;
00058   } else {
00059     return Service::queryInterface( riid, ppvInterface );
00060   }
00061 }

static const std::string& IDatabaseSvc::serviceInUse (  )  [inline, static, inherited]

Definition at line 23 of file IDatabaseSvc.h.

References IDatabaseSvc::g_serviceInUse.

00023                                          { 
00024     return g_serviceInUse; 
00025   };


Friends And Related Function Documentation

friend class SvcFactory< DatabaseSvc > [friend]

Definition at line 19 of file DatabaseSvc.h.


Member Data Documentation

DbInterface* DatabaseSvc::dbInterface [private]

Definition at line 71 of file DatabaseSvc.h.

Referenced by finalize(), initialize(), query(), and ~DatabaseSvc().

std::string IDatabaseSvc::g_serviceInUse = "" [static, protected, inherited]

Definition at line 31 of file IDatabaseSvc.h.

Referenced by DatabaseSvc(), initialize(), and IDatabaseSvc::serviceInUse().

std::string DatabaseSvc::m_dbFilePath [private]

Definition at line 77 of file DatabaseSvc.h.

Referenced by DatabaseSvc(), and initialize().

std::string DatabaseSvc::m_dbHost [private]

Definition at line 74 of file DatabaseSvc.h.

Referenced by DatabaseSvc(), and initialize().

std::string DatabaseSvc::m_dbName [private]

Definition at line 72 of file DatabaseSvc.h.

std::string DatabaseSvc::m_dbPasswd [private]

Definition at line 76 of file DatabaseSvc.h.

Referenced by DatabaseSvc(), and initialize().

bool DatabaseSvc::m_dbReuseConnection [private]

Definition at line 79 of file DatabaseSvc.h.

Referenced by DatabaseSvc(), and initialize().

std::string DatabaseSvc::m_dbType [private]

Definition at line 78 of file DatabaseSvc.h.

Referenced by DatabaseSvc(), and initialize().

std::string DatabaseSvc::m_dbUser [private]

Definition at line 75 of file DatabaseSvc.h.

Referenced by DatabaseSvc(), and initialize().


Generated on Tue Nov 29 23:18:10 2016 for BOSS_7.0.2 by  doxygen 1.4.7