db_mysql Class Reference

#include <db_mysql.h>

List of all members.

Public Member Functions

 db_mysql ()
 db_mysql (string host, string user, string passwd, string db="")
 ~db_mysql ()
void Init (string host, string user, string passwd, string db="")
void SetQuery (string query)
bool Connect (void)
bool SelectDB (string db)
bool Execute (string query="")
unsigned long Num_Rows (void)
bool GetRow (my_ulonglong row=NEXT_ROW)
string GetField (unsigned int n)
string GetField_name (unsigned int n)
void Free_Result (void)
void Close (void)

Public Attributes

unsigned int Field_num

Private Attributes

MYSQLm_Mysql
MYSQL_RESm_Res
MYSQL_ROW m_Row
MYSQL_FIELD * m_field
string m_strHost
string m_strUser
string m_strPasswd
string m_strDB
string m_strQuery
bool m_bInit
bool m_bConnect
bool m_bSelectDB
bool m_bExecute
bool m_bGetRow
bool m_result


Detailed Description

Definition at line 9 of file db_mysql.h.


Constructor & Destructor Documentation

db_mysql::db_mysql (  ) 

Definition at line 3 of file db_mysql.cpp.

00004         :m_Mysql(NULL)
00005         ,m_Res(NULL)
00006         ,m_bConnect(false)
00007         ,m_bSelectDB(false)
00008         ,m_bExecute(false)
00009         ,m_bGetRow(false)
00010         ,m_bInit(false)
00011 {
00012         ;       
00013 }

db_mysql::db_mysql ( string  host,
string  user,
string  passwd,
string  db = "" 
)

Definition at line 14 of file db_mysql.cpp.

References Init().

00015         :m_Mysql(NULL)
00016         ,m_Res(NULL)
00017         ,m_bConnect(false)
00018         ,m_bSelectDB(false)
00019         ,m_bExecute(false)
00020         ,m_bGetRow(false)
00021 {
00022         Init(host,user,passwd,db);
00023 }       

db_mysql::~db_mysql (  ) 

Definition at line 221 of file db_mysql.cpp.

References Close(), and Free_Result().

00222 {
00223         Free_Result();
00224         Close();
00225 }


Member Function Documentation

void db_mysql::Close ( void   ) 

Definition at line 211 of file db_mysql.cpp.

References Free_Result(), m_bConnect, and m_Mysql.

Referenced by main(), WriteLumDB(), and ~db_mysql().

00212 {
00213         if(m_bConnect)
00214         {
00215                 Free_Result();
00216                 mysql_close(m_Mysql);
00217                 m_bConnect = false;
00218         }
00219 }

bool db_mysql::Connect ( void   ) 

Definition at line 41 of file db_mysql.cpp.

References m_bConnect, m_Mysql, m_strDB, m_strHost, m_strPasswd, and m_strUser.

Referenced by main(), and WriteLumDB().

00042 {
00043         if(!m_bConnect)
00044         {
00045                 my_bool b = 0;
00046                 if(!(m_Mysql=mysql_init(NULL)))
00047                 {
00048                         cout<<"Init mysql error!!"<<endl;
00049                         return false; 
00050                 }
00051 //              mysql_options(m_Mysql,MYSQL_REPORT_DATA_TRUNCATION,&b);
00052                 if(!mysql_real_connect(m_Mysql,m_strHost.c_str(),m_strUser.c_str(),m_strPasswd.c_str(),m_strDB.c_str(),0,NULL,0))
00053                 {
00054                         cout<<"Connect error!!"<<endl;
00055                         mysql_close(m_Mysql);
00056                         return false;
00057                 }
00058         }
00059         else
00060         {
00061                 cout<<"You are reconnecting!"<<endl;
00062                 return false;   
00063         }
00064         m_bConnect = true;
00065         return true;
00066 }

bool db_mysql::Execute ( string  query = ""  ) 

Definition at line 84 of file db_mysql.cpp.

References Field_num, Free_Result(), m_bConnect, m_bExecute, m_bSelectDB, m_Mysql, m_Res, m_result, and m_strQuery.

Referenced by ReadFromDB(), ReWriteBeamParToDb(), WriteBeamParToDb(), WriteLumDB(), and WriteLumTauDB().

00085 {
00086         if((!m_bConnect)||(!m_bSelectDB))
00087         {
00088                 cout<<"You executing query before connection or select db!"<<endl;
00089                 return false;
00090         }
00091         if(!query.empty())
00092         {
00093                 m_strQuery=query;
00094         }
00095         if(m_strQuery.empty())
00096         {
00097                 cout<<"Your haven't set query string!"<<endl;
00098                 return false;   
00099         }
00100         else
00101         {
00102                 Free_Result();
00103                 if(mysql_real_query(m_Mysql,m_strQuery.c_str(),m_strQuery.size()))
00104                 {
00105                         cout<<"Execute query error!"<<endl;
00106                         return false;   
00107                 }       
00108         }
00109 //zhaohs
00110         Field_num=mysql_field_count(m_Mysql);
00111         if(Field_num==0){
00112         m_result=false;
00113         }
00114         else {m_result=true;}
00115 //zhaohs
00116         m_Res = mysql_store_result(m_Mysql);
00117         m_bExecute = true;
00118         return true;
00119 }

void db_mysql::Free_Result ( void   ) 

Definition at line 201 of file db_mysql.cpp.

References m_bExecute, m_bGetRow, and m_Res.

Referenced by Close(), Execute(), and ~db_mysql().

00202 {
00203         if(m_bExecute)
00204         {
00205                 mysql_free_result(m_Res);
00206                 m_bExecute = false;
00207                 m_bGetRow = false;      
00208         }
00209 }

string db_mysql::GetField ( unsigned int  n  ) 

Definition at line 148 of file db_mysql.cpp.

References m_bGetRow, m_Row, and deljobs::string.

Referenced by ReadFromDB(), ReWriteBeamParToDb(), and WriteLumDB().

00149 {
00150         
00151         if(!m_bGetRow)
00152         {
00153                 cout<<"You must get a row before getfield value!"<<endl;
00154                 return "";
00155         }
00156         if(m_Row[n]!=NULL){ return (string)m_Row[n];}
00157         else return "";
00158 }

string db_mysql::GetField_name ( unsigned int  n  ) 

Definition at line 171 of file db_mysql.cpp.

References m_field, m_Res, m_result, and deljobs::string.

00172 {
00173         if(!m_result)
00174                 {
00175                  cout<<"error:no select!"<<endl;
00176                 return "";
00177                 }
00178         m_field = mysql_fetch_field_direct(m_Res, n);
00179         return string(m_field->name);
00180 
00181 
00182 //      num_fields = mysql_num_fields(result);
00183 //      m_field = mysql_fetch_fields(m_Res);
00184   //      std::cout<<m_field[n].name<<std::endl;
00185 //      return m_field[n].name;
00186 
00187            
00188 }

bool db_mysql::GetRow ( my_ulonglong  row = NEXT_ROW  ) 

Definition at line 121 of file db_mysql.cpp.

References m_bExecute, m_bGetRow, m_Res, m_Row, and NEXT_ROW.

Referenced by ReadFromDB(), ReWriteBeamParToDb(), and WriteLumDB().

00122 {
00123         if(!m_bExecute)
00124         {
00125                 cout<<"You must execute query before get row!"<<endl;
00126                 return false;
00127         }
00128         if(row!=NEXT_ROW)
00129         {
00130                 if(row>=mysql_num_rows(m_Res)||row<0)
00131                 {
00132 //                      cout<<"Select row error! Row number must be set in range!"<<endl;
00133                         return false;
00134                 }
00135                 mysql_data_seek(m_Res,row);     
00136         }
00137         m_Row=mysql_fetch_row(m_Res);
00138         if(m_Row==NULL)
00139         {
00140                 cout<<"fetch_row error!"<<endl; 
00141                 return false;
00142         }
00143         
00144         m_bGetRow = true;
00145         return true;
00146 }

void db_mysql::Init ( string  host,
string  user,
string  passwd,
string  db = "" 
)

Definition at line 25 of file db_mysql.cpp.

References m_bInit, m_bSelectDB, m_strDB, m_strHost, m_strPasswd, and m_strUser.

Referenced by db_mysql(), main(), and WriteLumDB().

00026 {
00027         m_strHost = host;
00028         m_strUser = user;
00029         m_strPasswd = passwd;
00030         m_strDB = db;
00031         m_bInit = true;
00032         if(!db.empty())
00033         {
00034                 m_bSelectDB = true;
00035         }
00036 }

unsigned long db_mysql::Num_Rows ( void   ) 

Definition at line 191 of file db_mysql.cpp.

References m_bExecute, and m_Res.

00192 {
00193         if(!m_bExecute)
00194         {
00195                 cout<<"You are get number of rows before execute!"<<endl;
00196                 return 0;
00197         }
00198         return mysql_num_rows(m_Res);
00199 }

bool db_mysql::SelectDB ( string  db  ) 

Definition at line 67 of file db_mysql.cpp.

References m_bConnect, m_bSelectDB, m_Mysql, and m_strDB.

Referenced by main(), and WriteLumDB().

00068 {
00069         if(!m_bConnect)
00070         {
00071                 cout<<"You must connect before select db!";
00072                 return false;
00073         }
00074         if(mysql_select_db(m_Mysql,db.c_str()))
00075         {
00076                 cout<<"Select database error!"<<endl;
00077                 return false;
00078         }
00079         m_strDB = db;
00080         m_bSelectDB = true;
00081         return true;
00082 }

void db_mysql::SetQuery ( string  query  ) 

Definition at line 37 of file db_mysql.cpp.

References m_strQuery.

00038 {
00039         m_strQuery=query;
00040 }


Member Data Documentation

unsigned int db_mysql::Field_num

Definition at line 36 of file db_mysql.h.

Referenced by Execute().

bool db_mysql::m_bConnect [private]

Definition at line 25 of file db_mysql.h.

Referenced by Close(), Connect(), Execute(), and SelectDB().

bool db_mysql::m_bExecute [private]

Definition at line 27 of file db_mysql.h.

Referenced by Execute(), Free_Result(), GetRow(), and Num_Rows().

bool db_mysql::m_bGetRow [private]

Definition at line 28 of file db_mysql.h.

Referenced by Free_Result(), GetField(), and GetRow().

bool db_mysql::m_bInit [private]

Definition at line 24 of file db_mysql.h.

Referenced by Init().

bool db_mysql::m_bSelectDB [private]

Definition at line 26 of file db_mysql.h.

Referenced by Execute(), Init(), and SelectDB().

MYSQL_FIELD* db_mysql::m_field [private]

Definition at line 15 of file db_mysql.h.

Referenced by GetField_name().

MYSQL* db_mysql::m_Mysql [private]

Definition at line 12 of file db_mysql.h.

Referenced by Close(), Connect(), Execute(), and SelectDB().

MYSQL_RES* db_mysql::m_Res [private]

Definition at line 13 of file db_mysql.h.

Referenced by Execute(), Free_Result(), GetField_name(), GetRow(), and Num_Rows().

bool db_mysql::m_result [private]

Definition at line 29 of file db_mysql.h.

Referenced by Execute(), and GetField_name().

MYSQL_ROW db_mysql::m_Row [private]

Definition at line 14 of file db_mysql.h.

Referenced by GetField(), and GetRow().

string db_mysql::m_strDB [private]

Definition at line 20 of file db_mysql.h.

Referenced by Connect(), Init(), and SelectDB().

string db_mysql::m_strHost [private]

Definition at line 17 of file db_mysql.h.

Referenced by Connect(), and Init().

string db_mysql::m_strPasswd [private]

Definition at line 19 of file db_mysql.h.

Referenced by Connect(), and Init().

string db_mysql::m_strQuery [private]

Definition at line 21 of file db_mysql.h.

Referenced by Execute(), and SetQuery().

string db_mysql::m_strUser [private]

Definition at line 18 of file db_mysql.h.

Referenced by Connect(), and Init().


Generated on Tue Nov 29 23:18:10 2016 for BOSS_7.0.2 by  doxygen 1.4.7