BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
MysqlInterface.cxx
Go to the documentation of this file.
1#include "MysqlInterface.h"
2
3#include <cstring>
4#include <iostream>
5#include <mysql.h>
6#include <unistd.h>
7
8using namespace std;
9
11
13
18
19int MysqlInterface::connect( std::string host, std::string user, std::string passwd,
20 int port ) {
21 try
22 {
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(),
30 m_dbName.c_str(), port, NULL, 0 );
31 if ( ret != 0 )
32 { // Everything is fine. Put out an info message
33 std::cout << "DatabaseSvc: Connected to MySQL database" << std::endl;
34 break;
35 }
36 else
37 {
38 std::cout << "Couldn't connect to MySQL database. Trying again." << std::endl;
39 sleep( 1 );
40 }
41 }
42
43 if ( ret == 0 && iattempt == 2 ) throw (char*)mysql_error( m_conn );
44
45 } catch ( std::exception& e )
46 {
47
48 cerr << "Error in MySQL session initialization!" << endl;
49 cerr << "*** std::exception caught:" << endl;
50 cerr << "*** error message:" << e.what() << endl;
51 return -1;
52
53 } catch ( ... )
54 { return -1; }
55
56 m_isConnected = true;
57 return 0;
58}
59
60int MysqlInterface::select_db( std::string dbname ) {
61 int ret = mysql_select_db( m_conn, dbname.c_str() );
62 if ( ret != 0 )
63 {
64 disconnect();
65 // Try to re-connect
66 sleep( 5 );
68 if ( ret == 0 ) { ret = mysql_select_db( m_conn, dbname.c_str() ); }
69 if ( ret != 0 ) { throw std::exception(); }
70 }
71 return 0;
72}
73
74int MysqlInterface::query( std::string dbname, std::string sql ) {
76
77 try
78 {
79 // check database name
80 if ( m_dbName != dbname )
81 {
82 m_dbName = dbname;
84 }
85
86 int status = mysql_real_query( m_conn, sql.c_str(), sql.length() );
87 if ( status )
88 {
89 // if(mysql_errno(m_conn)==2006){
90 // std::cerr << "MySQL error 2006: MySQL server has gone away"<< std::endl;
91 std::cerr << "MySQL error: MySQL server has gone away" << std::endl;
92 disconnect();
93 // Try to re-connect
94 sleep( 5 );
96 if ( ret == 0 )
97 {
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 } catch ( ... )
111 {
112 cerr << "Could not execute query: " << mysql_error( m_conn ) << endl;
113 return -1;
114 }
115
117
118 return 0;
119}
120
121int MysqlInterface::query( std::string dbname, std::string sql,
122 DatabaseRecordVector& records ) {
124
125 records.clear();
126
127 try
128 {
129 // check database name
130 if ( m_dbName != dbname )
131 {
132 m_dbName = dbname;
134 }
135
136 int status = mysql_real_query( m_conn, sql.c_str(), sql.length() );
137 if ( status )
138 {
139 // if(mysql_errno(m_conn)==2006){
140 // std::cerr << "MySQL error 2006: MySQL server has gone away"<< std::endl;
141 std::cerr << "MySQL error: MySQL server has gone away" << std::endl;
142 disconnect();
143 // Try to re-connect
144 sleep( 5 );
146 if ( ret == 0 )
147 {
148 std::cout << "Connected to MySQL database " << std::endl;
150 status = mysql_real_query( m_conn, sql.c_str(), sql.length() );
151 }
152 // }
153
154 if ( status )
155 {
156 cerr << "Query " << sql << " failed: " << mysql_error( m_conn ) << endl;
157 return -1;
158 }
159 }
160
161 MYSQL_RES* result = mysql_store_result( m_conn );
162
163 if ( result )
164 {
165 int num_fields = mysql_num_fields( result );
166
167 if ( num_fields > 0 )
168 {
169 MYSQL_FIELD* fields;
170 fields = mysql_fetch_fields( result );
171
172 MYSQL_ROW row;
173 while ( ( row = mysql_fetch_row( result ) ) )
174 {
175 unsigned long* lengths;
176 lengths = mysql_fetch_lengths( result );
177 DatabaseRecord* dbrec = new DatabaseRecord;
178 int field;
179 for ( field = 0; field < num_fields; field++ )
180 {
181 if ( row[field] != 0 )
182 {
183 unsigned long field_len = lengths[field];
184 char* new_record;
185 if ( fields[field].type == FIELD_TYPE_BLOB )
186 {
187 new_record = new char[field_len];
188 memcpy( new_record, row[field], field_len );
189 }
190 else // strings
191 {
192 new_record = new char[field_len + 1];
193 strcpy( new_record, row[field] );
194 }
195
196 ( *dbrec )[fields[field].name] = new_record;
197 }
198 else { ( *dbrec )[fields[field].name] = nullptr; }
199 }
200 records.push_back( dbrec );
201 }
202 }
203 mysql_free_result( result );
205
206 return records.size();
207 }
208 } catch ( ... )
209 {
210 cerr << "Could not execute query: " << mysql_error( m_conn ) << endl;
212 return -1;
213 }
214
216
217 return 0;
218}
219
221 if ( m_conn )
222 {
223 mysql_close( m_conn );
224 delete m_conn;
225 m_conn = NULL;
226 }
227 m_isConnected = false;
228 return 0;
229}
std::string m_dbUser
Definition DbInterface.h:44
std::string m_dbName
Definition DbInterface.h:40
bool m_reuseConnection
Definition DbInterface.h:38
bool m_isConnected
Definition DbInterface.h:37
std::string m_dbPasswd
Definition DbInterface.h:45
std::string m_dbHost
Definition DbInterface.h:42
int query(std::string dbname, std::string query)
int select_db(std::string dbname)