BOSS 7.1.1
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 ()
 
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_port (int port)
 
void set_reuse_connection (bool flag)
 

Protected Member Functions

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

Additional Inherited Members

- Protected Attributes inherited from DbInterface
bool m_isConnected
 
bool m_reuseConnection
 
std::string m_dbName
 
std::string m_dbHost
 
int m_dbPort
 
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 10 of file MysqlInterface.cxx.

10{}

◆ ~MysqlInterface()

MysqlInterface::~MysqlInterface ( )

Definition at line 12 of file MysqlInterface.cxx.

12{}

Member Function Documentation

◆ connect() [1/2]

int MysqlInterface::connect ( )
virtual

Implements DbInterface.

Definition at line 14 of file MysqlInterface.cxx.

15{
18 return 0;
19}
std::string m_dbUser
Definition DbInterface.h:44
bool m_reuseConnection
Definition DbInterface.h:38
std::string m_dbPasswd
Definition DbInterface.h:45
std::string m_dbHost
Definition DbInterface.h:42

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

◆ connect() [2/2]

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

Definition at line 21 of file MysqlInterface.cxx.

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

◆ disconnect()

int MysqlInterface::disconnect ( )
virtual

Implements DbInterface.

Definition at line 229 of file MysqlInterface.cxx.

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

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

◆ query() [1/2]

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

Implements DbInterface.

Definition at line 75 of file MysqlInterface.cxx.

76{
79
80 try{
81 // check database name
82 if(m_dbName != dbname)
83 {
84 m_dbName = dbname;
86 }
87
88 int status = mysql_real_query(m_conn, sql.c_str(), sql.length());
89 if (status)
90 {
91 // if(mysql_errno(m_conn)==2006){
92 // std::cerr << "MySQL error 2006: MySQL server has gone away"<< std::endl;
93 std::cerr << "MySQL error: MySQL server has gone away"<< std::endl;
94 disconnect();
95 // Try to re-connect
96 sleep(5);
98 if (ret == 0) {
99 std::cout << "Connected to MySQL database " << std::endl;
101 status = mysql_real_query(m_conn, sql.c_str(), sql.length());
102 }
103 // }
104
105 if(status)
106 {
107 cerr << "Query " << sql << " failed: " << mysql_error(m_conn) << endl;
108 return -1;
109 }
110 }
111 }
112 catch(...)
113 {
114 cerr << "Could not execute query: " << mysql_error(m_conn) << endl;
115 return -1;
116 }
117
119 disconnect();
120
121 return 0;
122}
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 124 of file MysqlInterface.cxx.

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

◆ select_db()

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

Implements DbInterface.

Definition at line 57 of file MysqlInterface.cxx.

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

Referenced by query(), and query().


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