BOSS 8.0.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 4 of file db_mysql.cpp.

5 : m_Mysql( NULL )
6 , m_Res( NULL )
7 , m_bConnect( false )
8 , m_bSelectDB( false )
9 , m_bExecute( false )
10 , m_bGetRow( false )
11 , m_bInit( false ) {
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 Init( host, user, passwd, db );
22}
void Init(string host, string user, string passwd, string db="")
Definition db_mysql.cpp:24

◆ ~db_mysql()

db_mysql::~db_mysql ( )

Definition at line 211 of file db_mysql.cpp.

211 {
212 Free_Result();
213 Close();
214}
void Free_Result(void)
Definition db_mysql.cpp:193
void Close(void)
Definition db_mysql.cpp:202

Member Function Documentation

◆ Close()

void db_mysql::Close ( void )

Definition at line 202 of file db_mysql.cpp.

202 {
203 if ( m_bConnect )
204 {
205 Free_Result();
206 mysql_close( m_Mysql );
207 m_bConnect = false;
208 }
209}

Referenced by ~db_mysql().

◆ Connect()

bool db_mysql::Connect ( void )

Definition at line 33 of file db_mysql.cpp.

33 {
34 if ( !m_bConnect )
35 {
36 my_bool b = 0;
37 if ( !( m_Mysql = mysql_init( NULL ) ) )
38 {
39 cout << "Init mysql error!!" << endl;
40 return false;
41 }
42 // mysql_options(m_Mysql,MYSQL_REPORT_DATA_TRUNCATION,&b);
43 if ( !mysql_real_connect( m_Mysql, m_strHost.c_str(), m_strUser.c_str(),
44 m_strPasswd.c_str(), m_strDB.c_str(), 0, NULL, 0 ) )
45 {
46 cout << "Connect error!!" << endl;
47 for ( int i = 0; i < 10; i++ )
48 {
49 cout << "Reconnect ..." << endl;
50 sleep( 1 );
51 if ( mysql_real_connect( m_Mysql, m_strHost.c_str(), m_strUser.c_str(),
52 m_strPasswd.c_str(), m_strDB.c_str(), 0, NULL, 0 ) )
53 {
54 cout << "Reconnect success" << endl;
55 m_bConnect = true;
56 return true;
57 }
58 cout << "Reconnect fail" << endl;
59 }
60 mysql_close( m_Mysql );
61 return false;
62 }
63 }
64 else
65 {
66 cout << "You are reconnecting!" << endl;
67 return false;
68 }
69 m_bConnect = true;
70 return true;
71}

◆ Execute()

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

Definition at line 88 of file db_mysql.cpp.

88 {
89 if ( ( !m_bConnect ) || ( !m_bSelectDB ) )
90 {
91 cout << "You executing query before connection or select db!" << endl;
92 return false;
93 }
94 if ( !query.empty() ) { m_strQuery = query; }
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 ) { m_result = false; }
112 else { m_result = true; }
113 // zhaohs
114 m_Res = mysql_store_result( m_Mysql );
115 m_bExecute = true;
116 return true;
117}
unsigned int Field_num
Definition db_mysql.h:34

◆ Free_Result()

void db_mysql::Free_Result ( void )

Definition at line 193 of file db_mysql.cpp.

193 {
194 if ( m_bExecute )
195 {
196 mysql_free_result( m_Res );
197 m_bExecute = false;
198 m_bGetRow = false;
199 }
200}

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

◆ GetField()

string db_mysql::GetField ( unsigned int n)

Definition at line 146 of file db_mysql.cpp.

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

◆ GetField_name()

string db_mysql::GetField_name ( unsigned int n)

Definition at line 168 of file db_mysql.cpp.

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

◆ GetRow()

bool db_mysql::GetRow ( my_ulonglong row = NEXT_ROW)

Definition at line 119 of file db_mysql.cpp.

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

◆ Init()

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

Definition at line 24 of file db_mysql.cpp.

24 {
25 m_strHost = host;
26 m_strUser = user;
27 m_strPasswd = passwd;
28 m_strDB = db;
29 m_bInit = true;
30 if ( !db.empty() ) { m_bSelectDB = true; }
31}

Referenced by db_mysql().

◆ Num_Rows()

unsigned long db_mysql::Num_Rows ( void )

Definition at line 184 of file db_mysql.cpp.

184 {
185 if ( !m_bExecute )
186 {
187 cout << "You are get number of rows before execute!" << endl;
188 return 0;
189 }
190 return mysql_num_rows( m_Res );
191}

◆ SelectDB()

bool db_mysql::SelectDB ( string db)

Definition at line 72 of file db_mysql.cpp.

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

◆ SetQuery()

void db_mysql::SetQuery ( string query)

Definition at line 32 of file db_mysql.cpp.

32{ m_strQuery = query; }

Member Data Documentation

◆ Field_num

unsigned int db_mysql::Field_num

Definition at line 34 of file db_mysql.h.

Referenced by Execute().


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