25 #include <mysql/errmsg.h>
33 g_logger.
fatal(
"Failed to initialize MySQL connection handle.");
36 my_bool reconnect =
true;
37 mysql_options(
m_handle, MYSQL_OPT_RECONNECT, &reconnect);
47 const std::string& db,
uint16 port,
const std::string& unix_socket)
55 unix_socket.empty() ? NULL : unix_socket.c_str(), 0)) {
65 unsigned int error = mysql_errno(
m_handle);
68 if(error == CR_SOCKET_CREATE_ERROR ||
69 error == CR_CONNECTION_ERROR ||
70 error == CR_CONN_HOST_ERROR ||
71 error == CR_IPSOCK_ERROR ||
72 error == CR_UNKNOWN_HOST ||
73 error == CR_SERVER_GONE_ERROR ||
74 error == CR_SERVER_LOST ||
75 error == CR_SERVER_HANDSHAKE_ERR) {
76 g_logger.
error(
"MYSQL connection lost, trying to reconnect...");
81 bool connected = (mysql_ping(
m_handle) == 0);
122 while(mysql_real_query(
m_handle, query.c_str(), query.length()) != 0) {
134 MYSQL_RES* res = mysql_store_result(
m_handle);
137 mysql_free_result(res);
138 }
else if(mysql_errno(
m_handle) != 0) {
151 MYSQL_RES* res = mysql_store_result(
m_handle);
160 else if(mysql_errno(
m_handle) != 0) {
184 return std::string();
187 char* output =
new char[length * 2 + 1];
188 mysql_real_escape_string(
m_handle, output, s, length);
190 std::string res =
"'";
200 RowNames_t::iterator it =
m_names.find(s);
202 return m_row[it->second] ? atoi(
m_row[it->second]) : 0;
210 RowNames_t::iterator it =
m_names.find(s);
212 return m_row[it->second] ? atoll(
m_row[it->second]) : 0;
220 RowNames_t::iterator it =
m_names.find(s);
222 return m_row[it->second] ? std::string(
m_row[it->second]) : std::string();
225 return std::string();
231 RowNames_t::iterator it =
m_names.find(s);
237 if(!
m_row[it->second])
240 size = mysql_fetch_lengths(
m_result)[it->second];
241 return m_row[it->second];
247 g_logger.
fatal(
"[MySQLResult::free] Error: trying to free already freed result");
258 return (
m_row != NULL);
274 while((field = mysql_fetch_field(
m_result))) {