/search.css" rel="stylesheet" type="text/css"/> /search.js">
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

Public Member Functions | Static Public Member Functions | Private Attributes
DatabaseSvc Class Reference

#include <DatabaseSvc.h>

Inheritance diagram for DatabaseSvc:
Inheritance graph
[legend]
Collaboration diagram for DatabaseSvc:
Collaboration graph
[legend]

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, DatabaseRecordVector &res)

Static Public Member Functions

static const InterfaceID & interfaceID ()

Private Attributes

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

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.

                                                                 : Service( name, sl ) 
{
  // declare properties
  declareProperty("dbHost",m_dbHost="dyb1.ihep.ac.cn",
                  "The host of offline database");
  declareProperty("dbName",m_dbName="offline_db");
  declareProperty("dbUser",m_dbUser="");
  declareProperty("dbPswd",m_dbPswd="");

}
DatabaseSvc::~DatabaseSvc ( ) [virtual]

Definition at line 23 of file DatabaseSvc.cc.

{
  if (m_conn)
    {
      mysql_close(m_conn);
      delete m_conn;
      m_conn = NULL;
    }
}

Member Function Documentation

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

Definition at line 34 of file DatabaseSvc.cc.

{
  if ( IID_IDatabaseSvc == riid ) {
    *ppvInterface = static_cast< IDatabaseSvc* >( this );
    return StatusCode::SUCCESS;
  } else {
    return Service::queryInterface( riid, ppvInterface );
  }
}
StatusCode DatabaseSvc::initialize ( ) [virtual]

Definition at line 47 of file DatabaseSvc.cc.

{

  // Initialize the base class
  StatusCode status = this->Service::initialize();
  if ( !status.isSuccess() ) return status;

  // Now we can get a handle to the MessageSvc
  MsgStream log( msgSvc(), name() );
  log << MSG::INFO 
      << "DatabaseSvc specific initialization starting" << endreq;  

  // Get properties from the JobOptionsSvc
  status = setProperties();
  if ( !status.isSuccess() ) {
    log << MSG::ERROR << "Could not set jobOptions properties" << endreq;
    return status;
  }
  log << MSG::DEBUG << "Properties were read from jobOptions" << endreq;


  const char* dbconf = getenv("DBCONF");
  if( dbconf ){
      log << MSG::INFO << "Using DybPython.DBConf.Export to setup envvars for DBCONF : \"" << dbconf << "\"" <<  endreq ;
      TPython::Exec( "from DybPython import DBConf ; DBConf.Export() " );
  }


  char* host = getenv("DYB_DB_HOST");
  if (host){ 
    m_dbHost = std::string(host);
  }
  else{
    log << MSG::WARNING << "Enviroment variable ${DYB_DB_HOST} is not set."<<endreq;
    log << MSG::WARNING << "Use the default database host: "<< m_dbHost << endreq;
  } 
  log << MSG::DEBUG << "Environment variable ${DYB_DB_HOST}: "<<m_dbHost << endreq;

  char* name = getenv("DYB_DB_NAME");
  if (name){
    m_dbName = std::string(name);
  }
  else{
    log << MSG::WARNING << "Enviroment variable ${DYB_DB_NAME} is not set."<<endreq;
    log << MSG::WARNING << "Use the default database name: "<< m_dbName << endreq;
  }
  log << MSG::DEBUG << "Environment variable ${DYB_DB_HOST}: "<<m_dbName << endreq;

  char* user = getenv("DYB_DB_USER");
  if (user){
    m_dbUser = std::string(user);
  }
  else{
    log << MSG::ERROR << "Enviroment variable ${DYB_DB_USER} is not set."<<endreq;
  }

  char* pswd = getenv("DYB_DB_PSWD");
  if (pswd){
    m_dbPswd = std::string(pswd);
  }
  else{
    log << MSG::ERROR << "Enviroment variable ${DYB_DB_PSWD} is not set."<<endreq;
  }


  if( dbconf ){
     setenv("DYB_DB_PSWD","dbconf_censored",1);
  }


  // Now initialize the MysqlSession
  try {
    m_conn = new MYSQL;
    mysql_init(m_conn);
    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);
    if (ret != 0) {  // Everything is fine.  Put out an info message
      log << MSG::INFO << "Connected to MySQL database: " << m_dbHost
          <<"/"<<m_dbName<<endreq;
    }   
    else {      
      throw (char*)mysql_error(m_conn);
    }

  } catch ( std::exception &e ) {

    log << MSG::FATAL << "Error in MySQL session initialization!" << endreq;
    log << MSG::FATAL << "*** std::exception caught:"              << endreq;
    log << MSG::FATAL << "***  error message:" << e.what()         << endreq;
    return StatusCode::FAILURE;    
  
  } catch (char* mess) {
  
    log << MSG::FATAL << "Exception in MySQL session initialization caught: " << mess << endreq;  
    log << MSG::FATAL << "Check jobOptions properties dbHost, dbUser, dbPswd" << endreq;  
    //log << MSG::FATAL << "Check if environmental variable $DYB_DB_HOST is set." << endreq;  
    return StatusCode::FAILURE;    
  }
  catch (...) {
    log << MSG::FATAL << "UNKNOWN exception in MySQL session initialization caught" << endreq;
    return StatusCode::FAILURE;
  }

  // Initialization completed
  log << MSG::INFO << "DatabaseSvc initialized successfully" << endreq;
  return StatusCode::SUCCESS;
}
StatusCode DatabaseSvc::finalize ( ) [virtual]

Definition at line 157 of file DatabaseSvc.cc.

{
  MsgStream log( msgSvc(), name() ); 
  
  // Finalize the base class
  StatusCode status = Service::finalize();
  if ( ! status.isSuccess() ) {
    log << MSG::ERROR << "Unable to finalize the Service" << endreq;
    return status;
  }

  mysql_close(m_conn);
  delete m_conn;
  m_conn = NULL;
  
  // Finalization completed
  log << MSG::INFO << "DatabaseSvc finalized successfully" << endreq;
  return StatusCode::SUCCESS;
}
int DatabaseSvc::query ( const std::string &  dbName,
const std::string &  sql,
DatabaseRecordVector res 
) [virtual]

Implements IDatabaseSvc.

Definition at line 178 of file DatabaseSvc.cc.

{
  MsgStream log( msgSvc(), name() ); 

  log << MSG::DEBUG << "Query database " << dbName << " SQL: " << sql << endreq; 

  // clear vector
  res.clear();
 
  try{
    // check database name
    if(m_dbName != dbName)
      {
        m_dbName = dbName;
        int ret = mysql_select_db(m_conn, m_dbName.c_str());
        if ( ret != 0 )
          throw std::exception();
      }
    
    int status = mysql_real_query(m_conn, sql.c_str(), sql.length());
    if (status)
      {
        log << MSG::ERROR << "Query " << sql << " failed: " << mysql_error(m_conn) << endreq;
      }     
 
    MYSQL_RES* result = mysql_store_result(m_conn);
  
    if(result)
      {
        int num_fields = mysql_num_fields(result);
    
        if(num_fields>0)
          {
            MYSQL_FIELD *fields;
            fields = mysql_fetch_fields(result);
            
            MYSQL_ROW row;
            while ((row = mysql_fetch_row(result)))
              {
                DatabaseRecord dbrec;
                int field;
                for(field = 0; field<num_fields; field++)
                  {
                    if(row[field]!=0)
                      {
                        dbrec[fields[field].name]=row[field];
                      }
                    else 
                      {
                        dbrec[fields[field].name]="NULL";
                      }
                  }
                res.push_back(dbrec);
              }
          }
        mysql_free_result(result);
        return res.size();
      }
  } 
  catch(...)
    {
      log << MSG::ERROR << "Could not execute query: " << mysql_error(m_conn)  << endreq;  
      return 0;
    }
  
  return 0;
}
static const InterfaceID& IDatabaseSvc::interfaceID ( ) [inline, static, inherited]

Definition at line 19 of file IDatabaseSvc.h.

{ 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 Fri May 16 2014 09:53:53 for DatabaseSvc by doxygen 1.7.4