MysqlInterface Class Reference

#include <MysqlInterface.h>

Inheritance diagram for MysqlInterface:

DbInterface List of all members.

Public Member Functions

 MysqlInterface ()
 ~MysqlInterface ()
int connect ()
int select_db (std::string dbname)
int query (std::string dbname, std::string query)
int query (std::string dbname, std::string query, DatabaseRecordVector &records)
int disconnect ()
bool is_connected ()
void set_host (std::string host)
void set_user (std::string user)
void set_passwd (std::string passwd)
void set_dbpath (std::string path)
void set_reuse_connection (bool flag)

Protected Member Functions

int connect (std::string host, std::string user, std::string passwd)

Protected Attributes

bool m_isConnected
bool m_reuseConnection
std::string m_dbName
std::string m_dbHost
std::string m_dbUser
std::string m_dbPasswd
std::string m_dbPath

Private Attributes

MYSQLm_conn

Detailed Description

Definition at line 7 of file MysqlInterface.h.


Constructor & Destructor Documentation

MysqlInterface::MysqlInterface (  ) 

Definition at line 9 of file MysqlInterface.cxx.

00009 {}

MysqlInterface::~MysqlInterface (  ) 

Definition at line 11 of file MysqlInterface.cxx.

00011 {}


Member Function Documentation

int MysqlInterface::connect ( std::string  host,
std::string  user,
std::string  passwd 
) [protected]

Definition at line 20 of file MysqlInterface.cxx.

References m_conn, DbInterface::m_dbName, and DbInterface::m_isConnected.

00021 {
00022   try {
00023     m_conn = new MYSQL;
00024     mysql_init(m_conn);
00025     MYSQL* ret = 0;
00026     int iattempt = 0;
00027     for(iattempt=0;iattempt<3;iattempt++)
00028     {
00029       ret = mysql_real_connect(m_conn, host.c_str(), user.c_str(), passwd.c_str(),m_dbName.c_str(),3306,NULL,0);
00030       if (ret != 0) {  // Everything is fine.  Put out an info message
00031        std::cout << "DatabaseSvc: Connected to MySQL database" << std::endl;
00032        break;
00033       }   
00034       else {      
00035        std::cout << "Couldn't connect to MySQL database. Trying again." << std::endl;
00036        sleep(1);
00037       }
00038    }
00039 
00040    if(ret == 0 && iattempt == 2)
00041         throw (char*)mysql_error(m_conn);
00042 
00043   } catch ( std::exception &e ) {
00044 
00045     cerr << "Error in MySQL session initialization!" << endl;
00046     cerr << "*** std::exception caught:"              << endl;
00047     cerr << "***  error message:" << e.what()         << endl;
00048     return -1;
00049   
00050   } catch (...) { return -1; }
00051 
00052   m_isConnected = true;
00053   return 0;
00054 }

int MysqlInterface::connect (  )  [virtual]

Implements DbInterface.

Definition at line 13 of file MysqlInterface.cxx.

References DbInterface::m_dbHost, DbInterface::m_dbPasswd, DbInterface::m_dbUser, and DbInterface::m_reuseConnection.

Referenced by query(), and select_db().

00014 {
00015   if( m_reuseConnection )
00016     connect(m_dbHost, m_dbUser, m_dbPasswd);
00017   return 0;
00018 }

int MysqlInterface::disconnect (  )  [virtual]

Implements DbInterface.

Definition at line 228 of file MysqlInterface.cxx.

References m_conn, and DbInterface::m_isConnected.

Referenced by query(), and select_db().

00229 {
00230   if (m_conn)
00231     {
00232       mysql_close(m_conn);
00233       delete m_conn;
00234       m_conn = NULL;
00235     }
00236   m_isConnected = false;
00237   return 0;
00238 }

bool DbInterface::is_connected (  )  [inline, inherited]

Definition at line 27 of file DbInterface.h.

References DbInterface::m_isConnected.

Referenced by DatabaseSvc::finalize().

00027 { return m_isConnected; }  

int MysqlInterface::query ( std::string  dbname,
std::string  query,
DatabaseRecordVector records 
) [virtual]

Implements DbInterface.

Definition at line 123 of file MysqlInterface.cxx.

References DatabaseRecordVector::clear(), connect(), disconnect(), m_conn, DbInterface::m_dbHost, DbInterface::m_dbName, DbInterface::m_dbPasswd, DbInterface::m_dbUser, DbInterface::m_reuseConnection, select_db(), and type.

00124 {
00125   if(! m_reuseConnection)
00126     connect(m_dbHost, m_dbUser, m_dbPasswd);
00127 
00128   records.clear();
00129 
00130   try{
00131     // check database name
00132     if(m_dbName != dbname)
00133       {
00134         m_dbName = dbname;
00135         select_db(m_dbName);
00136       }
00137     
00138     int status = mysql_real_query(m_conn, sql.c_str(), sql.length());
00139     if (status)
00140       {
00141 //      if(mysql_errno(m_conn)==2006){ 
00142 //         std::cerr << "MySQL error 2006: MySQL server has gone away"<< std::endl;
00143          std::cerr << "MySQL error: MySQL server has gone away"<< std::endl;
00144          disconnect();
00145          // Try to re-connect
00146          sleep(5);
00147          int ret = connect(m_dbHost, m_dbUser, m_dbPasswd);
00148          if (ret == 0) { 
00149              std::cout << "Connected to MySQL database " << std::endl;
00150              select_db(m_dbName);
00151              status = mysql_real_query(m_conn, sql.c_str(), sql.length());
00152          }
00153        // }
00154         
00155         if(status)
00156         {
00157           cerr << "Query " << sql << " failed: " << mysql_error(m_conn) << endl;
00158           return -1;
00159         }
00160       }     
00161  
00162     MYSQL_RES* result = mysql_store_result(m_conn);
00163 
00164     if(result)
00165       {
00166         int num_fields = mysql_num_fields(result);
00167     
00168         if(num_fields>0)
00169           {
00170             MYSQL_FIELD *fields;
00171             fields = mysql_fetch_fields(result);
00172             
00173             MYSQL_ROW row;
00174             while ((row = mysql_fetch_row(result)))
00175               {
00176                 unsigned long* lengths;
00177                 lengths = mysql_fetch_lengths(result);
00178                 DatabaseRecord* dbrec = new DatabaseRecord;
00179                 int field;
00180                 for(field = 0; field<num_fields; field++)
00181                   {
00182                     if(row[field]!=0)
00183                       {
00184                         unsigned long field_len = lengths[field];
00185                         char* new_record;
00186                         if(fields[field].type==FIELD_TYPE_BLOB)
00187                         { 
00188                           new_record = new char[field_len];
00189                           memcpy(new_record, row[field], field_len);
00190                         }
00191                         else // strings
00192                         {
00193                           new_record = new char[field_len+1];
00194                           strcpy(new_record, row[field]);       
00195                         }
00196                         
00197                         (*dbrec)[fields[field].name]=new_record;
00198                       }
00199                     else 
00200                       {
00201                         (*dbrec)[fields[field].name]='\0';
00202                       }
00203                   }
00204                 records.push_back(dbrec);
00205               }
00206           }
00207         mysql_free_result(result);
00208         if(! m_reuseConnection)
00209           disconnect();
00210         
00211         return records.size();
00212       }
00213   } 
00214   catch(...)
00215     {
00216       cerr << "Could not execute query: " << mysql_error(m_conn)  << endl;
00217       if(! m_reuseConnection)
00218         disconnect();
00219       return -1;
00220     }  
00221 
00222   if(! m_reuseConnection)
00223     disconnect();
00224 
00225   return 0;
00226 }

int MysqlInterface::query ( std::string  dbname,
std::string  query 
) [virtual]

Implements DbInterface.

Definition at line 74 of file MysqlInterface.cxx.

References connect(), disconnect(), m_conn, DbInterface::m_dbHost, DbInterface::m_dbName, DbInterface::m_dbPasswd, DbInterface::m_dbUser, DbInterface::m_reuseConnection, and select_db().

00075 {
00076   if(! m_reuseConnection)
00077     connect(m_dbHost, m_dbUser, m_dbPasswd);
00078 
00079   try{
00080     // check database name
00081     if(m_dbName != dbname)
00082       {
00083         m_dbName = dbname;
00084         select_db(m_dbName);
00085       }
00086     
00087     int status = mysql_real_query(m_conn, sql.c_str(), sql.length());
00088     if (status)
00089       {
00090         //        if(mysql_errno(m_conn)==2006){
00091         //  std::cerr << "MySQL error 2006: MySQL server has gone away"<< std::endl;
00092         std::cerr << "MySQL error: MySQL server has gone away"<< std::endl;
00093         disconnect();
00094         // Try to re-connect
00095         sleep(5);
00096         int ret = connect(m_dbHost, m_dbUser, m_dbPasswd);
00097         if (ret == 0) {
00098           std::cout << "Connected to MySQL database " << std::endl;
00099           select_db(m_dbName);
00100           status = mysql_real_query(m_conn, sql.c_str(), sql.length());
00101         }
00102         //  }
00103 
00104         if(status)
00105         {
00106           cerr << "Query " << sql << " failed: " << mysql_error(m_conn) << endl;
00107           return -1;
00108         }
00109       }
00110   } 
00111   catch(...)
00112     {
00113       cerr << "Could not execute query: " << mysql_error(m_conn)  << endl;
00114       return -1;
00115     }  
00116 
00117   if(! m_reuseConnection)
00118     disconnect();
00119 
00120   return 0;
00121 }

int MysqlInterface::select_db ( std::string  dbname  )  [virtual]

Implements DbInterface.

Definition at line 56 of file MysqlInterface.cxx.

References connect(), disconnect(), m_conn, DbInterface::m_dbHost, DbInterface::m_dbPasswd, and DbInterface::m_dbUser.

Referenced by query().

00057 {
00058   int ret = mysql_select_db(m_conn, dbname.c_str());
00059   if ( ret != 0 ) {
00060       disconnect();
00061       // Try to re-connect
00062       sleep(5);
00063       ret = connect(m_dbHost, m_dbUser, m_dbPasswd);
00064       if ( ret == 0 ) {
00065           ret = mysql_select_db(m_conn, dbname.c_str());
00066       }
00067       if (ret != 0) {
00068           throw std::exception();
00069       }
00070   }
00071   return 0;
00072 }

void DbInterface::set_dbpath ( std::string  path  )  [inline, inherited]

Definition at line 32 of file DbInterface.h.

References DbInterface::m_dbPath.

Referenced by DatabaseSvc::initialize().

00032 {m_dbPath=path;};

void DbInterface::set_host ( std::string  host  )  [inline, inherited]

Definition at line 29 of file DbInterface.h.

References DbInterface::m_dbHost.

Referenced by DatabaseSvc::initialize().

00029 {m_dbHost=host;};

void DbInterface::set_passwd ( std::string  passwd  )  [inline, inherited]

Definition at line 31 of file DbInterface.h.

References DbInterface::m_dbPasswd.

Referenced by DatabaseSvc::initialize().

00031 {m_dbPasswd=passwd;};

void DbInterface::set_reuse_connection ( bool  flag  )  [inline, inherited]

Definition at line 33 of file DbInterface.h.

References DbInterface::m_reuseConnection.

Referenced by DatabaseSvc::initialize().

void DbInterface::set_user ( std::string  user  )  [inline, inherited]

Definition at line 30 of file DbInterface.h.

References DbInterface::m_dbUser.

Referenced by DatabaseSvc::initialize().

00030 {m_dbUser=user;};


Member Data Documentation

MYSQL* MysqlInterface::m_conn [private]

Definition at line 23 of file MysqlInterface.h.

Referenced by connect(), disconnect(), query(), and select_db().

std::string DbInterface::m_dbHost [protected, inherited]

Definition at line 41 of file DbInterface.h.

Referenced by connect(), query(), select_db(), and DbInterface::set_host().

std::string DbInterface::m_dbName [protected, inherited]

Definition at line 39 of file DbInterface.h.

Referenced by connect(), DbInterface::DbInterface(), SqliteInterface::disconnect(), query(), and SqliteInterface::select_db().

std::string DbInterface::m_dbPasswd [protected, inherited]

Definition at line 43 of file DbInterface.h.

Referenced by connect(), query(), select_db(), and DbInterface::set_passwd().

std::string DbInterface::m_dbPath [protected, inherited]

Definition at line 44 of file DbInterface.h.

Referenced by SqliteInterface::select_db(), and DbInterface::set_dbpath().

std::string DbInterface::m_dbUser [protected, inherited]

Definition at line 42 of file DbInterface.h.

Referenced by connect(), query(), select_db(), and DbInterface::set_user().

bool DbInterface::m_isConnected [protected, inherited]

Definition at line 33 of file DbInterface.h.

Referenced by SqliteInterface::connect(), connect(), DbInterface::DbInterface(), SqliteInterface::disconnect(), disconnect(), DbInterface::is_connected(), and SqliteInterface::select_db().

bool DbInterface::m_reuseConnection [protected, inherited]

Definition at line 37 of file DbInterface.h.

Referenced by connect(), DbInterface::DbInterface(), query(), and DbInterface::set_reuse_connection().


Generated on Tue Nov 29 23:20:33 2016 for BOSS_7.0.2 by  doxygen 1.4.7