#include <DatabaseSvc.h>
Inheritance diagram for DatabaseSvc:
Public Member Functions | |
virtual StatusCode | finalize () |
virtual StatusCode | finalize () |
virtual StatusCode | initialize () |
virtual StatusCode | initialize () |
int | query (const std::string &dbName, const std::string &sql, DatabaseRecordVector &res) |
int | query (const std::string &dbName, const std::string &sql) |
int | query (const std::string &dbName, const std::string &sql, DatabaseRecordVector &res) |
int | query (const std::string &dbName, const std::string &sql) |
virtual StatusCode | queryInterface (const InterfaceID &riid, void **ppvInterface) |
virtual StatusCode | queryInterface (const InterfaceID &riid, void **ppvInterface) |
Static Public Member Functions | |
const InterfaceID & | interfaceID () |
const InterfaceID & | interfaceID () |
const std::string & | serviceInUse () |
const std::string & | serviceInUse () |
Protected Member Functions | |
DatabaseSvc (const std::string &name, ISvcLocator *sl) | |
DatabaseSvc (const std::string &name, ISvcLocator *sl) | |
virtual | ~DatabaseSvc () |
virtual | ~DatabaseSvc () |
Static Protected Attributes | |
std::string | g_serviceInUse = "" |
Private Attributes | |
DbInterface * | dbInterface |
DbInterface * | dbInterface |
std::string | m_dbFilePath |
std::string | m_dbHost |
std::string | m_dbName |
std::string | m_dbPasswd |
bool | m_dbReuseConnection |
std::string | m_dbType |
std::string | m_dbUser |
Friends | |
class | SvcFactory<DatabaseSvc> |
|
00016 : Service( name, sl ) 00017 { 00018 // Set the name of this service 00019 if ( IDatabaseSvc::g_serviceInUse != "" ) { 00020 std::ostringstream error; 00021 error << "There is another IDatabaseSvc registered with name " 00022 << IDatabaseSvc::g_serviceInUse << std::ends; 00023 throw "Error in DatabaseSvc: "+error.str(); 00024 } 00025 IDatabaseSvc::g_serviceInUse = "DatabaseSvc"; 00026 00027 // declare properties 00028 declareProperty("Host",m_dbHost=""); 00029 declareProperty("User",m_dbUser="guest"); 00030 declareProperty("Passwd",m_dbPasswd="guestpass"); 00031 declareProperty("SqliteDbPath",m_dbFilePath=""); 00032 declareProperty("DbType",m_dbType="SQLITE"); 00033 declareProperty("ReuseConnection",m_dbReuseConnection=false); 00034 }
|
|
00039 { 00040 if (dbInterface) 00041 delete dbInterface; 00042 }
|
|
|
|
|
|
|
|
00142 { 00143 MsgStream log( msgSvc(), name() ); 00144 StatusCode status = Service::finalize(); 00145 if ( ! status.isSuccess() ) { 00146 log << MSG::ERROR << "Unable to finalize the Service" << endreq; 00147 return status; 00148 } 00149 00150 if(dbInterface) 00151 { 00152 if(dbInterface->is_connected()) 00153 dbInterface->disconnect(); 00154 delete dbInterface; 00155 dbInterface = NULL; 00156 } 00157 00158 log << MSG::INFO << "DatabaseSvc finalized successfully" << endreq; 00159 return StatusCode::SUCCESS; 00160 }
|
|
|
|
00061 { 00062 StatusCode status = Service::initialize(); 00063 if ( !status.isSuccess() ) return status; 00064 00065 MsgStream log( msgSvc(), name() ); 00066 IDatabaseSvc::g_serviceInUse = name(); 00067 setProperties(); 00068 00069 bool use_sqlite = false; 00070 bool use_mysql = false; 00071 00072 std::transform(m_dbType.begin(), m_dbType.end(), m_dbType.begin(), ::toupper); 00073 00074 if(m_dbType=="MYSQL") 00075 use_mysql = true; 00076 00077 if(m_dbType=="SQLITE") 00078 use_sqlite = true; 00079 00080 log << MSG::DEBUG << "Using " << m_dbType 00081 << " interface with options:" << endreq; 00082 00083 try { 00084 if(use_mysql) 00085 { 00086 log << MSG::DEBUG << " dbHost " << m_dbHost << endreq; 00087 log << MSG::DEBUG << " dbUser " << m_dbUser << endreq; 00088 log << MSG::DEBUG << " dbPasswd " << m_dbPasswd << endreq; 00089 00090 dbInterface = new MysqlInterface(); 00091 dbInterface->set_host(m_dbHost); 00092 dbInterface->set_user(m_dbUser); 00093 dbInterface->set_passwd(m_dbPasswd); 00094 } 00095 else if(use_sqlite) 00096 { 00097 log << MSG::DEBUG << " dbFilepath " << m_dbFilePath << endreq; 00098 00099 dbInterface = new SqliteInterface(); 00100 dbInterface->set_dbpath(m_dbFilePath); 00101 } 00102 else 00103 { 00104 log << MSG::FATAL << "No valid database type is set. Please choose either MYSQL or SQLITE " << endreq; 00105 return StatusCode::FAILURE; 00106 } 00107 00108 if(m_dbReuseConnection) 00109 log << MSG::DEBUG << "One connection per job is used" << endreq; 00110 else 00111 log << MSG::DEBUG << "One connection per query is used" << endreq; 00112 00113 if( m_dbReuseConnection ) 00114 { 00115 dbInterface->set_reuse_connection(true); 00116 dbInterface->connect(); 00117 } 00118 00119 } catch ( std::exception &e ) { 00120 00121 log << MSG::FATAL << "Exception in DataSvc initialization:" << endreq; 00122 log << MSG::FATAL << "*** error message: " << e.what() << endreq; 00123 return StatusCode::FAILURE; 00124 00125 } catch (char* mess) { 00126 log << MSG::FATAL << "Exception DataSvc initialization caught: " << mess << endreq; 00127 return StatusCode::FAILURE; 00128 } 00129 catch (...) { 00130 log << MSG::FATAL << "UNKNOWN exception in DataSvc session initialization caught" << endreq; 00131 return StatusCode::FAILURE; 00132 } 00133 00134 log << MSG::INFO << "DatabaseSvc initialized successfully" << endreq; 00135 return StatusCode::SUCCESS; 00136 }
|
|
00022 { return IID_IDatabaseSvc; }
|
|
00022 { return IID_IDatabaseSvc; }
|
|
Implements IDatabaseSvc. |
|
|
|
Implements IDatabaseSvc. 00164 { 00165 MsgStream log( msgSvc(), name() ); 00166 00167 log << MSG::DEBUG << "Query database " << dbName << " SQL: " << sql << endreq; 00168 00169 result.clear(); 00170 00171 try{ 00172 int status = dbInterface->query(dbName, sql, result); 00173 if (status<0) 00174 { 00175 log << MSG::ERROR << "Query " << sql << " failed" << endreq; 00176 return -1; 00177 } 00178 } 00179 catch(...) 00180 { 00181 log << MSG::ERROR << "Could not execute query " << sql << endreq; 00182 return -1; 00183 } 00184 00185 return result.size(); 00186 }
|
|
00189 { 00190 MsgStream log( msgSvc(), name() ); 00191 00192 log << MSG::DEBUG << "Query database " << dbName << " SQL: " << sql << endreq; 00193 00194 try{ 00195 int status = dbInterface->query(dbName, sql); 00196 if (status<0) 00197 { 00198 log << MSG::ERROR << "Query " << sql << " failed" << endreq; 00199 return -1; 00200 } 00201 } 00202 catch(...) 00203 { 00204 log << MSG::ERROR << "Could not execute query " << sql << endreq; 00205 return -1; 00206 } 00207 00208 return 0; 00209 }
|
|
|
|
00048 { 00049 if ( IID_IDatabaseSvc == riid ) { 00050 *ppvInterface = static_cast< IDatabaseSvc* >( this ); 00051 return StatusCode::SUCCESS; 00052 } else { 00053 return Service::queryInterface( riid, ppvInterface ); 00054 } 00055 }
|
|
00023 {
00024 return g_serviceInUse;
00025 };
|
|
00023 {
00024 return g_serviceInUse;
00025 };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|