BOSS 7.0.6
BESIII Offline Software System
Loading...
Searching...
No Matches
db_mysql Class Reference

#include <db_mysql.h>

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
 

Detailed Description

Definition at line 9 of file db_mysql.h.

Constructor & Destructor Documentation

◆ db_mysql() [1/2]

db_mysql::db_mysql ( )

Definition at line 3 of file db_mysql.cpp.

4 :m_Mysql(NULL)
5 ,m_Res(NULL)
6 ,m_bConnect(false)
7 ,m_bSelectDB(false)
8 ,m_bExecute(false)
9 ,m_bGetRow(false)
10 ,m_bInit(false)
11{
12 ;
13}

◆ db_mysql() [2/2]

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

Definition at line 14 of file db_mysql.cpp.

15 :m_Mysql(NULL)
16 ,m_Res(NULL)
17 ,m_bConnect(false)
18 ,m_bSelectDB(false)
19 ,m_bExecute(false)
20 ,m_bGetRow(false)
21{
22 Init(host,user,passwd,db);
23}
void Init(string host, string user, string passwd, string db="")
Definition: db_mysql.cpp:25

◆ ~db_mysql()

db_mysql::~db_mysql ( )

Definition at line 221 of file db_mysql.cpp.

222{
223 Free_Result();
224 Close();
225}
void Free_Result(void)
Definition: db_mysql.cpp:201
void Close(void)
Definition: db_mysql.cpp:211

Member Function Documentation

◆ Close()

void db_mysql::Close ( void  )

Definition at line 211 of file db_mysql.cpp.

212{
213 if(m_bConnect)
214 {
215 Free_Result();
216 mysql_close(m_Mysql);
217 m_bConnect = false;
218 }
219}

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

◆ Connect()

bool db_mysql::Connect ( void  )

Definition at line 41 of file db_mysql.cpp.

42{
43 if(!m_bConnect)
44 {
45 my_bool b = 0;
46 if(!(m_Mysql=mysql_init(NULL)))
47 {
48 cout<<"Init mysql error!!"<<endl;
49 return false;
50 }
51// mysql_options(m_Mysql,MYSQL_REPORT_DATA_TRUNCATION,&b);
52 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))
53 {
54 cout<<"Connect error!!"<<endl;
55 mysql_close(m_Mysql);
56 return false;
57 }
58 }
59 else
60 {
61 cout<<"You are reconnecting!"<<endl;
62 return false;
63 }
64 m_bConnect = true;
65 return true;
66}
const double b
Definition: slope.cxx:9

Referenced by main(), and WriteLumDB().

◆ Execute()

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

Definition at line 84 of file db_mysql.cpp.

85{
86 if((!m_bConnect)||(!m_bSelectDB))
87 {
88 cout<<"You executing query before connection or select db!"<<endl;
89 return false;
90 }
91 if(!query.empty())
92 {
93 m_strQuery=query;
94 }
95 if(m_strQuery.empty())
96 {
97 cout<<"Your haven't set query string!"<<endl;
98 return false;
99 }
100 else
101 {
102 Free_Result();
103 if(mysql_real_query(m_Mysql,m_strQuery.c_str(),m_strQuery.size()))
104 {
105 cout<<"Execute query error!"<<endl;
106 return false;
107 }
108 }
109//zhaohs
110 Field_num=mysql_field_count(m_Mysql);
111 if(Field_num==0){
112 m_result=false;
113 }
114 else {m_result=true;}
115//zhaohs
116 m_Res = mysql_store_result(m_Mysql);
117 m_bExecute = true;
118 return true;
119}
unsigned int Field_num
Definition: db_mysql.h:36

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

◆ Free_Result()

void db_mysql::Free_Result ( void  )

Definition at line 201 of file db_mysql.cpp.

202{
203 if(m_bExecute)
204 {
205 mysql_free_result(m_Res);
206 m_bExecute = false;
207 m_bGetRow = false;
208 }
209}

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

◆ GetField()

string db_mysql::GetField ( unsigned int  n)

Definition at line 148 of file db_mysql.cpp.

149{
150
151 if(!m_bGetRow)
152 {
153 cout<<"You must get a row before getfield value!"<<endl;
154 return "";
155 }
156 if(m_Row[n]!=NULL){ return (string)m_Row[n];}
157 else return "";
158}

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

◆ GetField_name()

string db_mysql::GetField_name ( unsigned int  n)

Definition at line 171 of file db_mysql.cpp.

172{
173 if(!m_result)
174 {
175 cout<<"error:no select!"<<endl;
176 return "";
177 }
178 m_field = mysql_fetch_field_direct(m_Res, n);
179 return string(m_field->name);
180
181
182// num_fields = mysql_num_fields(result);
183// m_field = mysql_fetch_fields(m_Res);
184 // std::cout<<m_field[n].name<<std::endl;
185// return m_field[n].name;
186
187
188}

◆ GetRow()

bool db_mysql::GetRow ( my_ulonglong  row = NEXT_ROW)

Definition at line 121 of file db_mysql.cpp.

122{
123 if(!m_bExecute)
124 {
125 cout<<"You must execute query before get row!"<<endl;
126 return false;
127 }
128 if(row!=NEXT_ROW)
129 {
130 if(row>=mysql_num_rows(m_Res)||row<0)
131 {
132// cout<<"Select row error! Row number must be set in range!"<<endl;
133 return false;
134 }
135 mysql_data_seek(m_Res,row);
136 }
137 m_Row=mysql_fetch_row(m_Res);
138 if(m_Row==NULL)
139 {
140 cout<<"fetch_row error!"<<endl;
141 return false;
142 }
143
144 m_bGetRow = true;
145 return true;
146}
#define NEXT_ROW
Definition: db_mysql.h:8

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

◆ Init()

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

Definition at line 25 of file db_mysql.cpp.

26{
27 m_strHost = host;
28 m_strUser = user;
29 m_strPasswd = passwd;
30 m_strDB = db;
31 m_bInit = true;
32 if(!db.empty())
33 {
34 m_bSelectDB = true;
35 }
36}

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

◆ Num_Rows()

unsigned long db_mysql::Num_Rows ( void  )

Definition at line 191 of file db_mysql.cpp.

192{
193 if(!m_bExecute)
194 {
195 cout<<"You are get number of rows before execute!"<<endl;
196 return 0;
197 }
198 return mysql_num_rows(m_Res);
199}

◆ SelectDB()

bool db_mysql::SelectDB ( string  db)

Definition at line 67 of file db_mysql.cpp.

68{
69 if(!m_bConnect)
70 {
71 cout<<"You must connect before select db!";
72 return false;
73 }
74 if(mysql_select_db(m_Mysql,db.c_str()))
75 {
76 cout<<"Select database error!"<<endl;
77 return false;
78 }
79 m_strDB = db;
80 m_bSelectDB = true;
81 return true;
82}

Referenced by main(), and WriteLumDB().

◆ SetQuery()

void db_mysql::SetQuery ( string  query)

Definition at line 37 of file db_mysql.cpp.

38{
39 m_strQuery=query;
40}

Member Data Documentation

◆ Field_num

unsigned int db_mysql::Field_num

Definition at line 36 of file db_mysql.h.

Referenced by Execute().


The documentation for this class was generated from the following files: