BOSS 6.6.4.p03
BESIII Offline Software System
Loading...
Searching...
No Matches
MysqlInterface Class Reference

#include <MysqlInterface.h>

+ Inheritance diagram for MysqlInterface:

Public Member Functions

 MysqlInterface ()
 
 ~MysqlInterface ()
 
int connect ()
 
int select_db (std::string dbname)
 
int query (std::string dbname, std::string query)
 
int query (std::string dbname, std::string query, DatabaseRecordVector &records)
 
int disconnect ()
 
- Public Member Functions inherited from DbInterface
 DbInterface ()
 
virtual ~DbInterface ()
 
virtual int connect ()=0
 
virtual int select_db (std::string dbname)=0
 
virtual int query (std::string dbname, std::string query, DatabaseRecordVector &records)=0
 
virtual int query (std::string dbname, std::string query)=0
 
virtual int disconnect ()=0
 
bool is_connected ()
 
void set_host (std::string host)
 
void set_user (std::string user)
 
void set_passwd (std::string passwd)
 
void set_dbpath (std::string path)
 
void set_reuse_connection (bool flag)
 

Protected Member Functions

int connect (std::string host, std::string user, std::string passwd)
 

Additional Inherited Members

- Protected Attributes inherited from DbInterface
bool m_isConnected
 
bool m_reuseConnection
 
std::string m_dbName
 
std::string m_dbHost
 
std::string m_dbUser
 
std::string m_dbPasswd
 
std::string m_dbPath
 

Detailed Description

Definition at line 7 of file MysqlInterface.h.

Constructor & Destructor Documentation

◆ MysqlInterface()

MysqlInterface::MysqlInterface ( )

Definition at line 9 of file MysqlInterface.cxx.

9{}

◆ ~MysqlInterface()

MysqlInterface::~MysqlInterface ( )

Definition at line 11 of file MysqlInterface.cxx.

11{}

Member Function Documentation

◆ connect() [1/2]

int MysqlInterface::connect ( )
virtual

Implements DbInterface.

Definition at line 13 of file MysqlInterface.cxx.

14{
17 return 0;
18}
std::string m_dbUser
Definition: DbInterface.h:42
bool m_reuseConnection
Definition: DbInterface.h:37
std::string m_dbPasswd
Definition: DbInterface.h:43
std::string m_dbHost
Definition: DbInterface.h:41

Referenced by connect(), query(), and select_db().

◆ connect() [2/2]

int MysqlInterface::connect ( std::string  host,
std::string  user,
std::string  passwd 
)
protected

Definition at line 20 of file MysqlInterface.cxx.

21{
22 try {
23 m_conn = new MYSQL;
24 mysql_init(m_conn);
25 MYSQL* ret = 0;
26 int iattempt = 0;
27 for(iattempt=0;iattempt<3;iattempt++)
28 {
29 ret = mysql_real_connect(m_conn, host.c_str(), user.c_str(), passwd.c_str(),m_dbName.c_str(),3306,NULL,0);
30 if (ret != 0) { // Everything is fine. Put out an info message
31 std::cout << "DatabaseSvc: Connected to MySQL database" << std::endl;
32 break;
33 }
34 else {
35 std::cout << "Couldn't connect to MySQL database. Trying again." << std::endl;
36 sleep(1);
37 }
38 }
39
40 if(ret == 0 && iattempt == 2)
41 throw (char*)mysql_error(m_conn);
42
43 } catch ( std::exception &e ) {
44
45 cerr << "Error in MySQL session initialization!" << endl;
46 cerr << "*** std::exception caught:" << endl;
47 cerr << "*** error message:" << e.what() << endl;
48 return -1;
49
50 } catch (...) { return -1; }
51
52 m_isConnected = true;
53 return 0;
54}
struct st_mysql MYSQL
std::string m_dbName
Definition: DbInterface.h:39
bool m_isConnected
Definition: DbInterface.h:36

◆ disconnect()

int MysqlInterface::disconnect ( )
virtual

Implements DbInterface.

Definition at line 228 of file MysqlInterface.cxx.

229{
230 if (m_conn)
231 {
232 mysql_close(m_conn);
233 delete m_conn;
234 m_conn = NULL;
235 }
236 m_isConnected = false;
237 return 0;
238}

Referenced by query(), and select_db().

◆ query() [1/2]

int MysqlInterface::query ( std::string  dbname,
std::string  query 
)
virtual

Implements DbInterface.

Definition at line 74 of file MysqlInterface.cxx.

75{
78
79 try{
80 // check database name
81 if(m_dbName != dbname)
82 {
83 m_dbName = dbname;
85 }
86
87 int status = mysql_real_query(m_conn, sql.c_str(), sql.length());
88 if (status)
89 {
90 // if(mysql_errno(m_conn)==2006){
91 // std::cerr << "MySQL error 2006: MySQL server has gone away"<< std::endl;
92 std::cerr << "MySQL error: MySQL server has gone away"<< std::endl;
93 disconnect();
94 // Try to re-connect
95 sleep(5);
97 if (ret == 0) {
98 std::cout << "Connected to MySQL database " << std::endl;
100 status = mysql_real_query(m_conn, sql.c_str(), sql.length());
101 }
102 // }
103
104 if(status)
105 {
106 cerr << "Query " << sql << " failed: " << mysql_error(m_conn) << endl;
107 return -1;
108 }
109 }
110 }
111 catch(...)
112 {
113 cerr << "Could not execute query: " << mysql_error(m_conn) << endl;
114 return -1;
115 }
116
118 disconnect();
119
120 return 0;
121}
int select_db(std::string dbname)

◆ query() [2/2]

int MysqlInterface::query ( std::string  dbname,
std::string  query,
DatabaseRecordVector records 
)
virtual

Implements DbInterface.

Definition at line 123 of file MysqlInterface.cxx.

124{
127
128 records.clear();
129
130 try{
131 // check database name
132 if(m_dbName != dbname)
133 {
134 m_dbName = dbname;
136 }
137
138 int status = mysql_real_query(m_conn, sql.c_str(), sql.length());
139 if (status)
140 {
141// if(mysql_errno(m_conn)==2006){
142// std::cerr << "MySQL error 2006: MySQL server has gone away"<< std::endl;
143 std::cerr << "MySQL error: MySQL server has gone away"<< std::endl;
144 disconnect();
145 // Try to re-connect
146 sleep(5);
147 int ret = connect(m_dbHost, m_dbUser, m_dbPasswd);
148 if (ret == 0) {
149 std::cout << "Connected to MySQL database " << std::endl;
151 status = mysql_real_query(m_conn, sql.c_str(), sql.length());
152 }
153 // }
154
155 if(status)
156 {
157 cerr << "Query " << sql << " failed: " << mysql_error(m_conn) << endl;
158 return -1;
159 }
160 }
161
162 MYSQL_RES* result = mysql_store_result(m_conn);
163
164 if(result)
165 {
166 int num_fields = mysql_num_fields(result);
167
168 if(num_fields>0)
169 {
170 MYSQL_FIELD *fields;
171 fields = mysql_fetch_fields(result);
172
173 MYSQL_ROW row;
174 while ((row = mysql_fetch_row(result)))
175 {
176 unsigned long* lengths;
177 lengths = mysql_fetch_lengths(result);
178 DatabaseRecord* dbrec = new DatabaseRecord;
179 int field;
180 for(field = 0; field<num_fields; field++)
181 {
182 if(row[field]!=0)
183 {
184 unsigned long field_len = lengths[field];
185 char* new_record;
186 if(fields[field].type==FIELD_TYPE_BLOB)
187 {
188 new_record = new char[field_len];
189 memcpy(new_record, row[field], field_len);
190 }
191 else // strings
192 {
193 new_record = new char[field_len+1];
194 strcpy(new_record, row[field]);
195 }
196
197 (*dbrec)[fields[field].name]=new_record;
198 }
199 else
200 {
201 (*dbrec)[fields[field].name]='\0';
202 }
203 }
204 records.push_back(dbrec);
205 }
206 }
207 mysql_free_result(result);
209 disconnect();
210
211 return records.size();
212 }
213 }
214 catch(...)
215 {
216 cerr << "Could not execute query: " << mysql_error(m_conn) << endl;
218 disconnect();
219 return -1;
220 }
221
223 disconnect();
224
225 return 0;
226}
struct st_mysql_res MYSQL_RES

◆ select_db()

int MysqlInterface::select_db ( std::string  dbname)
virtual

Implements DbInterface.

Definition at line 56 of file MysqlInterface.cxx.

57{
58 int ret = mysql_select_db(m_conn, dbname.c_str());
59 if ( ret != 0 ) {
60 disconnect();
61 // Try to re-connect
62 sleep(5);
64 if ( ret == 0 ) {
65 ret = mysql_select_db(m_conn, dbname.c_str());
66 }
67 if (ret != 0) {
68 throw std::exception();
69 }
70 }
71 return 0;
72}

Referenced by query().


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