| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

DatabaseSvc Class Reference

#include <DatabaseSvc.h>

Inheritance diagram for DatabaseSvc:

[legend]
Collaboration diagram for DatabaseSvc:
[legend]
List of all members.

Public Types

 SUCCESS
 NO_INTERFACE
 VERSMISMATCH
 LAST_ERROR
enum  Status
 SUCCESS
 NO_INTERFACE
 VERSMISMATCH
 LAST_ERROR
enum  Status

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, DatabaseRecordVector &res)
virtual unsigned long addRef ()=0
virtual unsigned long release ()=0
virtual unsigned long addRef ()
virtual unsigned long release ()
virtual const std::string & name () const
virtual const InterfaceIDtype () const
virtual StatusCode configure ()
virtual StatusCode start ()
virtual StatusCode stop ()
virtual StatusCode terminate ()
virtual Gaudi::StateMachine::State FSMState () const
virtual Gaudi::StateMachine::State targetFSMState () const
virtual StatusCode reinitialize ()
virtual StatusCode restart ()
virtual StatusCode sysInitialize ()
virtual StatusCode sysStart ()
virtual StatusCode sysStop ()
virtual StatusCode sysFinalize ()
virtual StatusCode sysReinitialize ()
virtual StatusCode sysRestart ()
virtual StatusCode setProperty (const Property &p)
virtual StatusCode setProperty (const std::string &s)
virtual StatusCode setProperty (const std::string &n, const std::string &v)
StatusCode setProperty (const std::string &name, const TYPE &value)
virtual StatusCode getProperty (Property *p) const
virtual const PropertygetProperty (const std::string &name) const
virtual StatusCode getProperty (const std::string &n, std::string &v) const
virtual const std::vector<
Property * > & 
getProperties () const
ISvcLocatorserviceLocator () const
IMessageSvcmsgSvc ()
IMessageSvcmsgSvc () const
IMessageSvcmessageService ()
IMessageSvcmessageService () const
StatusCode setProperties ()
StatusCode service (const std::string &name, T *&psvc, bool createIf=true) const
StatusCode service (const std::string &svcType, const std::string &svcName, T *&psvc) const
PropertydeclareProperty (const std::string &name, T &property, const std::string &doc="none") const
PropertydeclareRemoteProperty (const std::string &name, IProperty *rsvc, const std::string &rname="") const
IAuditorSvcauditorSvc () const

Static Public Member Functions

static const InterfaceIDinterfaceID ()
static const InterfaceIDinterfaceID ()

Public Attributes

 SUCCESS
 NO_INTERFACE
 VERSMISMATCH
 LAST_ERROR

Protected Member Functions

int outputLevel () const

Protected Attributes

IntegerProperty m_outputLevel
Gaudi::StateMachine::State m_state
Gaudi::StateMachine::State m_targetState
IMessageSvcm_messageSvc

Private Attributes

std::string m_dbHost
std::string m_dbName
std::string m_dbUser
std::string m_dbPswd
MYSQL * m_conn

Friends

friend class ServiceManager

Detailed Description

Definition at line 10 of file DatabaseSvc.h.


Constructor & Destructor Documentation

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

Definition at line 12 of file DatabaseSvc.cc.

00012                                                                  : Service( name, sl ) 
00013 {
00014   // declare properties
00015   declareProperty("dbHost",m_dbHost="dyb1.ihep.ac.cn",
00016                   "The host of offline database");
00017   declareProperty("dbName",m_dbName="offline_db");
00018   declareProperty("dbUser",m_dbUser="");
00019   declareProperty("dbPswd",m_dbPswd="");
00020 
00021 }

DatabaseSvc::~DatabaseSvc (  )  [virtual]

Definition at line 23 of file DatabaseSvc.cc.

00024 {
00025   if (m_conn)
00026     {
00027       mysql_close(m_conn);
00028       delete m_conn;
00029       m_conn = NULL;
00030     }
00031 }


Member Function Documentation

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

Reimplemented from Service.

Definition at line 34 of file DatabaseSvc.cc.

00035 {
00036   if ( IID_IDatabaseSvc == riid ) {
00037     *ppvInterface = static_cast< IDatabaseSvc* >( this );
00038     return StatusCode::SUCCESS;
00039   } else {
00040     return Service::queryInterface( riid, ppvInterface );
00041   }
00042 }

StatusCode DatabaseSvc::initialize (  )  [virtual]

Reimplemented from Service.

Definition at line 47 of file DatabaseSvc.cc.

00048 {
00049 
00050   // Initialize the base class
00051   StatusCode status = this->Service::initialize();
00052   if ( !status.isSuccess() ) return status;
00053 
00054   // Now we can get a handle to the MessageSvc
00055   MsgStream log( msgSvc(), name() );
00056   log << MSG::INFO 
00057       << "DatabaseSvc specific initialization starting" << endreq;  
00058 
00059   // Get properties from the JobOptionsSvc
00060   status = setProperties();
00061   if ( !status.isSuccess() ) {
00062     log << MSG::ERROR << "Could not set jobOptions properties" << endreq;
00063     return status;
00064   }
00065   log << MSG::DEBUG << "Properties were read from jobOptions" << endreq;
00066 
00067 
00068   const char* dbconf = getenv("DBCONF");
00069   if( dbconf ){
00070       log << MSG::INFO << "Using DybPython.DBConf.Export to setup envvars for DBCONF : \"" << dbconf << "\"" <<  endreq ;
00071       TPython::Exec( "from DybPython import DBConf ; DBConf.Export() " );
00072   }
00073 
00074 
00075   char* host = getenv("DYB_DB_HOST");
00076   if (host){ 
00077     m_dbHost = std::string(host);
00078   }
00079   else{
00080     log << MSG::WARNING << "Enviroment variable ${DYB_DB_HOST} is not set."<<endreq;
00081     log << MSG::WARNING << "Use the default database host: "<< m_dbHost << endreq;
00082   } 
00083   log << MSG::DEBUG << "Environment variable ${DYB_DB_HOST}: "<<m_dbHost << endreq;
00084 
00085   char* name = getenv("DYB_DB_NAME");
00086   if (name){
00087     m_dbName = std::string(name);
00088   }
00089   else{
00090     log << MSG::WARNING << "Enviroment variable ${DYB_DB_NAME} is not set."<<endreq;
00091     log << MSG::WARNING << "Use the default database name: "<< m_dbName << endreq;
00092   }
00093   log << MSG::DEBUG << "Environment variable ${DYB_DB_HOST}: "<<m_dbName << endreq;
00094 
00095   char* user = getenv("DYB_DB_USER");
00096   if (user){
00097     m_dbUser = std::string(user);
00098   }
00099   else{
00100     log << MSG::ERROR << "Enviroment variable ${DYB_DB_USER} is not set."<<endreq;
00101   }
00102 
00103   char* pswd = getenv("DYB_DB_PSWD");
00104   if (pswd){
00105     m_dbPswd = std::string(pswd);
00106   }
00107   else{
00108     log << MSG::ERROR << "Enviroment variable ${DYB_DB_PSWD} is not set."<<endreq;
00109   }
00110 
00111 
00112   if( dbconf ){
00113      setenv("DYB_DB_PSWD","dbconf_censored",1);
00114   }
00115 
00116 
00117   // Now initialize the MysqlSession
00118   try {
00119     m_conn = new MYSQL;
00120     mysql_init(m_conn);
00121     MYSQL* ret = mysql_real_connect(m_conn, m_dbHost.c_str(), m_dbUser.c_str(), m_dbPswd.c_str(),m_dbName.c_str(),3306,NULL,0);
00122     if (ret != 0) {  // Everything is fine.  Put out an info message
00123       log << MSG::INFO << "Connected to MySQL database: " << m_dbHost
00124           <<"/"<<m_dbName<<endreq;
00125     }   
00126     else {      
00127       throw (char*)mysql_error(m_conn);
00128     }
00129 
00130   } catch ( std::exception &e ) {
00131 
00132     log << MSG::FATAL << "Error in MySQL session initialization!" << endreq;
00133     log << MSG::FATAL << "*** std::exception caught:"              << endreq;
00134     log << MSG::FATAL << "***  error message:" << e.what()         << endreq;
00135     return StatusCode::FAILURE;    
00136   
00137   } catch (char* mess) {
00138   
00139     log << MSG::FATAL << "Exception in MySQL session initialization caught: " << mess << endreq;  
00140     log << MSG::FATAL << "Check jobOptions properties dbHost, dbUser, dbPswd" << endreq;  
00141     //log << MSG::FATAL << "Check if environmental variable $DYB_DB_HOST is set." << endreq;  
00142     return StatusCode::FAILURE;    
00143   }
00144   catch (...) {
00145     log << MSG::FATAL << "UNKNOWN exception in MySQL session initialization caught" << endreq;
00146     return StatusCode::FAILURE;
00147   }
00148 
00149   // Initialization completed
00150   log << MSG::INFO << "DatabaseSvc initialized successfully" << endreq;
00151   return StatusCode::SUCCESS;
00152 }

StatusCode DatabaseSvc::finalize (  )  [virtual]

Reimplemented from Service.

Definition at line 157 of file DatabaseSvc.cc.

00158 {
00159   MsgStream log( msgSvc(), name() ); 
00160   
00161   // Finalize the base class
00162   StatusCode status = Service::finalize();
00163   if ( ! status.isSuccess() ) {
00164     log << MSG::ERROR << "Unable to finalize the Service" << endreq;
00165     return status;
00166   }
00167 
00168   mysql_close(m_conn);
00169   delete m_conn;
00170   m_conn = NULL;
00171   
00172   // Finalization completed
00173   log << MSG::INFO << "DatabaseSvc finalized successfully" << endreq;
00174   return StatusCode::SUCCESS;
00175 }

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

Implements IDatabaseSvc.

Definition at line 178 of file DatabaseSvc.cc.

00179 {
00180   MsgStream log( msgSvc(), name() ); 
00181 
00182   log << MSG::DEBUG << "Query database " << dbName << " SQL: " << sql << endreq; 
00183 
00184   // clear vector
00185   res.clear();
00186  
00187   try{
00188     // check database name
00189     if(m_dbName != dbName)
00190       {
00191         m_dbName = dbName;
00192         int ret = mysql_select_db(m_conn, m_dbName.c_str());
00193         if ( ret != 0 )
00194           throw std::exception();
00195       }
00196     
00197     int status = mysql_real_query(m_conn, sql.c_str(), sql.length());
00198     if (status)
00199       {
00200         log << MSG::ERROR << "Query " << sql << " failed: " << mysql_error(m_conn) << endreq;
00201       }     
00202  
00203     MYSQL_RES* result = mysql_store_result(m_conn);
00204   
00205     if(result)
00206       {
00207         int num_fields = mysql_num_fields(result);
00208     
00209         if(num_fields>0)
00210           {
00211             MYSQL_FIELD *fields;
00212             fields = mysql_fetch_fields(result);
00213             
00214             MYSQL_ROW row;
00215             while ((row = mysql_fetch_row(result)))
00216               {
00217                 DatabaseRecord dbrec;
00218                 int field;
00219                 for(field = 0; field<num_fields; field++)
00220                   {
00221                     if(row[field]!=0)
00222                       {
00223                         dbrec[fields[field].name]=row[field];
00224                       }
00225                     else 
00226                       {
00227                         dbrec[fields[field].name]="NULL";
00228                       }
00229                   }
00230                 res.push_back(dbrec);
00231               }
00232           }
00233         mysql_free_result(result);
00234         return res.size();
00235       }
00236   } 
00237   catch(...)
00238     {
00239       log << MSG::ERROR << "Could not execute query: " << mysql_error(m_conn)  << endreq;  
00240       return 0;
00241     }
00242   
00243   return 0;
00244 }

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

Reimplemented from IInterface.

Definition at line 19 of file IDatabaseSvc.h.

00019 { return IID_IDatabaseSvc; }


Member Data Documentation

std::string DatabaseSvc::m_dbHost [private]

Definition at line 38 of file DatabaseSvc.h.

std::string DatabaseSvc::m_dbName [private]

Definition at line 39 of file DatabaseSvc.h.

std::string DatabaseSvc::m_dbUser [private]

Definition at line 40 of file DatabaseSvc.h.

std::string DatabaseSvc::m_dbPswd [private]

Definition at line 41 of file DatabaseSvc.h.

MYSQL* DatabaseSvc::m_conn [private]

Definition at line 44 of file DatabaseSvc.h.


The documentation for this class was generated from the following files:
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Mon Apr 11 20:09:43 2011 for DatabaseSvc by doxygen 1.4.7