BOSS 7.0.5
BESIII Offline Software System
Loading...
Searching...
No Matches
MysqlInterface.cxx
Go to the documentation of this file.
1#include "DatabaseSvc/MysqlInterface.h"
2
3#include <iostream>
4#include <mysql.h>
5#include <cstring>
6
7using namespace std;
8
10
12
14{
17 return 0;
18}
19
20int MysqlInterface::connect(std::string host, std::string user, std::string passwd, int port)
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(),port,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}
55
56int MysqlInterface::select_db(std::string dbname)
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}
73
74int MysqlInterface::query(std::string dbname, std::string sql)
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}
122
123int MysqlInterface::query(std::string dbname, std::string sql, DatabaseRecordVector& records)
124{
127
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}
228
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}
int query(std::string dbname, std::string query)
int select_db(std::string dbname)