BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
db_mysql.cpp
Go to the documentation of this file.
1#include "db_mysql.h"
2#include <unistd.h>
3
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}
14db_mysql::db_mysql( string host, string user, string passwd, string db )
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}
23
24void db_mysql::Init( string host, string user, string passwd, string db ) {
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}
32void db_mysql::SetQuery( string query ) { m_strQuery = query; }
33bool db_mysql::Connect( void ) {
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}
72bool db_mysql::SelectDB( string db ) {
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}
87
88bool db_mysql::Execute( string query ) {
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}
118
119bool db_mysql::GetRow( my_ulonglong row ) {
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}
145
146string db_mysql::GetField( unsigned int n ) {
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}
156//// gaizao
157/*string db_mysql::GetField(string name)
158{
159
160 if(!m_bGetRow)
161 {cout<<"You must get a row before getfield value!"<<endl;
162 return "";
163 }
164 if(m_Row[name]!=NULL){ return (string)m_Row[name];}
165 else return "";
166}*/
167///// myself---zhaohs
168string db_mysql::GetField_name( unsigned int n ) {
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}
182
183////
184unsigned long db_mysql::Num_Rows( void ) {
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}
192
194 if ( m_bExecute )
195 {
196 mysql_free_result( m_Res );
197 m_bExecute = false;
198 m_bGetRow = false;
199 }
200}
201
202void db_mysql::Close( void ) {
203 if ( m_bConnect )
204 {
205 Free_Result();
206 mysql_close( m_Mysql );
207 m_bConnect = false;
208 }
209}
210
212 Free_Result();
213 Close();
214}
const Int_t n
void Free_Result(void)
Definition db_mysql.cpp:193
bool SelectDB(string db)
Definition db_mysql.cpp:72
bool GetRow(my_ulonglong row=NEXT_ROW)
Definition db_mysql.cpp:119
string GetField_name(unsigned int n)
Definition db_mysql.cpp:168
unsigned long Num_Rows(void)
Definition db_mysql.cpp:184
void SetQuery(string query)
Definition db_mysql.cpp:32
void Close(void)
Definition db_mysql.cpp:202
void Init(string host, string user, string passwd, string db="")
Definition db_mysql.cpp:24
bool Connect(void)
Definition db_mysql.cpp:33
bool Execute(string query="")
Definition db_mysql.cpp:88
unsigned int Field_num
Definition db_mysql.h:34
string GetField(unsigned int n)
Definition db_mysql.cpp:146
#define NEXT_ROW
Definition db_mysql.h:8