BOSS 8.0.0
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.

14 {
16 return 0;
17}
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 19 of file MysqlInterface.cxx.

20 {
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}
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 220 of file MysqlInterface.cxx.

220 {
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}

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 74 of file MysqlInterface.cxx.

74 {
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}
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 121 of file MysqlInterface.cxx.

122 {
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}

◆ select_db()

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

Implements DbInterface.

Definition at line 60 of file MysqlInterface.cxx.

60 {
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}

Referenced by query(), and query().


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