Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

MysqlInterface Class Reference

#include <MysqlInterface.h>

Inheritance diagram for MysqlInterface:

DbInterface DbInterface List of all members.

Public Member Functions

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

Protected Member Functions

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

Protected Attributes

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

Private Attributes

MYSQLm_conn
MYSQLm_conn

Constructor & Destructor Documentation

MysqlInterface::MysqlInterface  ) 
 

00008 {}

MysqlInterface::~MysqlInterface  ) 
 

00010 {}

MysqlInterface::MysqlInterface  ) 
 

MysqlInterface::~MysqlInterface  ) 
 


Member Function Documentation

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

int MysqlInterface::connect  )  [virtual]
 

Implements DbInterface.

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

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

int MysqlInterface::connect  )  [virtual]
 

Implements DbInterface.

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

int MysqlInterface::disconnect  )  [virtual]
 

Implements DbInterface.

int MysqlInterface::disconnect  )  [virtual]
 

Implements DbInterface.

00218 {
00219   if (m_conn)
00220     {
00221       mysql_close(m_conn);
00222       delete m_conn;
00223       m_conn = NULL;
00224     }
00225   m_isConnected = false;
00226   return 0;
00227 }

bool DbInterface::is_connected  )  [inline, inherited]
 

00027 { return m_isConnected; }  

bool DbInterface::is_connected  )  [inline, inherited]
 

00027 { return m_isConnected; }  

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

Implements DbInterface.

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

Implements DbInterface.

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

Implements DbInterface.

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

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

Implements DbInterface.

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

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

Implements DbInterface.

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

Implements DbInterface.

00056 {
00057   int ret = mysql_select_db(m_conn, dbname.c_str());
00058   if ( ret != 0 )
00059     throw std::exception();
00060   return 0;
00061 }

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

00032 {m_dbPath=path;};

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

00032 {m_dbPath=path;};

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

00029 {m_dbHost=host;};

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

00029 {m_dbHost=host;};

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

00031 {m_dbPasswd=passwd;};

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

00031 {m_dbPasswd=passwd;};

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

00033 { m_reuseConnection=flag;};

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

00033 { m_reuseConnection=flag;};

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

00030 {m_dbUser=user;};

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

00030 {m_dbUser=user;};


Member Data Documentation

MYSQL* MysqlInterface::m_conn [private]
 

MYSQL* MysqlInterface::m_conn [private]
 

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

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

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

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

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

bool DbInterface::m_isConnected [protected, inherited]
 

bool DbInterface::m_reuseConnection [protected, inherited]
 


The documentation for this class was generated from the following files:
Generated on Wed Feb 2 16:37:36 2011 for BOSS6.5.5 by  doxygen 1.3.9.1