BOSS 7.1.0
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}
#define NULL

◆ 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 233 of file db_mysql.cpp.

234{
235 Free_Result();
236 Close();
237}
void Free_Result(void)
Definition: db_mysql.cpp:213
void Close(void)
Definition: db_mysql.cpp:223

Member Function Documentation

◆ Close()

void db_mysql::Close ( void  )

Definition at line 223 of file db_mysql.cpp.

224{
225 if(m_bConnect)
226 {
227 Free_Result();
228 mysql_close(m_Mysql);
229 m_bConnect = false;
230 }
231}

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 for(int i=0;i<10;i++)
56 {
57 cout<<"Reconnect ..."<<endl;
58 sleep(1);
59 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))
60 {
61 cout<<"Reconnect success"<<endl;
62 m_bConnect = true;
63 return true;
64 }
65 cout<<"Reconnect fail"<<endl;
66 }
67 mysql_close(m_Mysql);
68 return false;
69 }
70 }
71 else
72 {
73 cout<<"You are reconnecting!"<<endl;
74 return false;
75 }
76 m_bConnect = true;
77 return true;
78}
const double b
Definition: slope.cxx:9

Referenced by main(), and WriteLumDB().

◆ Execute()

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

Definition at line 96 of file db_mysql.cpp.

97{
98 if((!m_bConnect)||(!m_bSelectDB))
99 {
100 cout<<"You executing query before connection or select db!"<<endl;
101 return false;
102 }
103 if(!query.empty())
104 {
105 m_strQuery=query;
106 }
107 if(m_strQuery.empty())
108 {
109 cout<<"Your haven't set query string!"<<endl;
110 return false;
111 }
112 else
113 {
114 Free_Result();
115 if(mysql_real_query(m_Mysql,m_strQuery.c_str(),m_strQuery.size()))
116 {
117 cout<<"Execute query error!"<<endl;
118 return false;
119 }
120 }
121//zhaohs
122 Field_num=mysql_field_count(m_Mysql);
123 if(Field_num==0){
124 m_result=false;
125 }
126 else {m_result=true;}
127//zhaohs
128 m_Res = mysql_store_result(m_Mysql);
129 m_bExecute = true;
130 return true;
131}
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 213 of file db_mysql.cpp.

214{
215 if(m_bExecute)
216 {
217 mysql_free_result(m_Res);
218 m_bExecute = false;
219 m_bGetRow = false;
220 }
221}

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

◆ GetField()

string db_mysql::GetField ( unsigned int  n)

Definition at line 160 of file db_mysql.cpp.

161{
162
163 if(!m_bGetRow)
164 {
165 cout<<"You must get a row before getfield value!"<<endl;
166 return "";
167 }
168 if(m_Row[n]!=NULL){ return (string)m_Row[n];}
169 else return "";
170}
const Int_t n

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

◆ GetField_name()

string db_mysql::GetField_name ( unsigned int  n)

Definition at line 183 of file db_mysql.cpp.

184{
185 if(!m_result)
186 {
187 cout<<"error:no select!"<<endl;
188 return "";
189 }
190 m_field = mysql_fetch_field_direct(m_Res, n);
191 return string(m_field->name);
192
193
194// num_fields = mysql_num_fields(result);
195// m_field = mysql_fetch_fields(m_Res);
196 // std::cout<<m_field[n].name<<std::endl;
197// return m_field[n].name;
198
199
200}

◆ GetRow()

bool db_mysql::GetRow ( my_ulonglong  row = NEXT_ROW)

Definition at line 133 of file db_mysql.cpp.

134{
135 if(!m_bExecute)
136 {
137 cout<<"You must execute query before get row!"<<endl;
138 return false;
139 }
140 if(row!=NEXT_ROW)
141 {
142 if(row>=mysql_num_rows(m_Res)||row<0)
143 {
144// cout<<"Select row error! Row number must be set in range!"<<endl;
145 return false;
146 }
147 mysql_data_seek(m_Res,row);
148 }
149 m_Row=mysql_fetch_row(m_Res);
150 if(m_Row==NULL)
151 {
152 cout<<"fetch_row error!"<<endl;
153 return false;
154 }
155
156 m_bGetRow = true;
157 return true;
158}
#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 203 of file db_mysql.cpp.

204{
205 if(!m_bExecute)
206 {
207 cout<<"You are get number of rows before execute!"<<endl;
208 return 0;
209 }
210 return mysql_num_rows(m_Res);
211}

◆ SelectDB()

bool db_mysql::SelectDB ( string  db)

Definition at line 79 of file db_mysql.cpp.

80{
81 if(!m_bConnect)
82 {
83 cout<<"You must connect before select db!";
84 return false;
85 }
86 if(mysql_select_db(m_Mysql,db.c_str()))
87 {
88 cout<<"Select database error!"<<endl;
89 return false;
90 }
91 m_strDB = db;
92 m_bSelectDB = true;
93 return true;
94}

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: