mysqlxx: added server address to exception messages [#METR-9068].

This commit is contained in:
Alexey Milovidov 2013-10-26 02:50:23 +00:00
parent 8fd6963212
commit fa7a666a96
3 changed files with 20 additions and 14 deletions

View File

@ -1,6 +1,6 @@
#ifndef MYSQLXX_EXCEPTION_H #pragma once
#define MYSQLXX_EXCEPTION_H
#include <sstream>
#include <mysql/mysql.h> #include <mysql/mysql.h>
#include <Poco/Exception.h> #include <Poco/Exception.h>
@ -48,22 +48,28 @@ struct CannotParseValue : public Exception
}; };
inline std::string errorMessage(MYSQL * driver)
{
std::stringstream res;
res << mysql_error(driver) << " (" << driver->host << ":" << driver->port << ")";
return res.str();
}
/// Для внутренних нужд библиотеки. /// Для внутренних нужд библиотеки.
inline void checkError(MYSQL * driver) inline void checkError(MYSQL * driver)
{ {
unsigned num = mysql_errno(driver); unsigned num = mysql_errno(driver);
if (num) if (num)
throw Exception(mysql_error(driver), num); throw Exception(errorMessage(driver), num);
} }
/// Для внутренних нужд библиотеки. /// Для внутренних нужд библиотеки.
inline void onError(MYSQL * driver) inline void onError(MYSQL * driver)
{ {
throw Exception(mysql_error(driver), mysql_errno(driver)); throw Exception(errorMessage(driver), mysql_errno(driver));
} }
} }
#endif

View File

@ -48,30 +48,30 @@ void Connection::connect(const char* db,
LibrarySingleton::instance(); LibrarySingleton::instance();
if (!mysql_init(&driver)) if (!mysql_init(&driver))
throw ConnectionFailed(mysql_error(&driver), mysql_errno(&driver)); throw ConnectionFailed(errorMessage(&driver), mysql_errno(&driver));
/// Установим таймауты /// Установим таймауты
if (mysql_options(&driver, MYSQL_OPT_CONNECT_TIMEOUT, reinterpret_cast<const char *>(&timeout))) if (mysql_options(&driver, MYSQL_OPT_CONNECT_TIMEOUT, reinterpret_cast<const char *>(&timeout)))
throw ConnectionFailed(mysql_error(&driver), mysql_errno(&driver)); throw ConnectionFailed(errorMessage(&driver), mysql_errno(&driver));
if (mysql_options(&driver, MYSQL_OPT_READ_TIMEOUT, reinterpret_cast<const char *>(&rw_timeout))) if (mysql_options(&driver, MYSQL_OPT_READ_TIMEOUT, reinterpret_cast<const char *>(&rw_timeout)))
throw ConnectionFailed(mysql_error(&driver), mysql_errno(&driver)); throw ConnectionFailed(errorMessage(&driver), mysql_errno(&driver));
if (mysql_options(&driver, MYSQL_OPT_WRITE_TIMEOUT, reinterpret_cast<const char *>(&rw_timeout))) if (mysql_options(&driver, MYSQL_OPT_WRITE_TIMEOUT, reinterpret_cast<const char *>(&rw_timeout)))
throw ConnectionFailed(mysql_error(&driver), mysql_errno(&driver)); throw ConnectionFailed(errorMessage(&driver), mysql_errno(&driver));
/** Включаем возможность использовать запрос LOAD DATA LOCAL INFILE с серверами, /** Включаем возможность использовать запрос LOAD DATA LOCAL INFILE с серверами,
* которые были скомпилированы без опции --enable-local-infile. * которые были скомпилированы без опции --enable-local-infile.
*/ */
if (mysql_options(&driver, MYSQL_OPT_LOCAL_INFILE, NULL)) if (mysql_options(&driver, MYSQL_OPT_LOCAL_INFILE, NULL))
throw ConnectionFailed(mysql_error(&driver), mysql_errno(&driver)); throw ConnectionFailed(errorMessage(&driver), mysql_errno(&driver));
if (!mysql_real_connect(&driver, server, user, password, db, port, NULL, driver.client_flag)) if (!mysql_real_connect(&driver, server, user, password, db, port, NULL, driver.client_flag))
throw ConnectionFailed(mysql_error(&driver), mysql_errno(&driver)); throw ConnectionFailed(errorMessage(&driver), mysql_errno(&driver));
/// Установим кодировки по-умолчанию - UTF-8. /// Установим кодировки по-умолчанию - UTF-8.
if (mysql_set_character_set(&driver, "UTF8")) if (mysql_set_character_set(&driver, "UTF8"))
throw ConnectionFailed(mysql_error(&driver), mysql_errno(&driver)); throw ConnectionFailed(errorMessage(&driver), mysql_errno(&driver));
is_connected = true; is_connected = true;
} }

View File

@ -59,7 +59,7 @@ void Query::executeImpl()
{ {
std::string query_string = query_buf.str(); std::string query_string = query_buf.str();
if (mysql_real_query(conn->getDriver(), query_string.data(), query_string.size())) if (mysql_real_query(conn->getDriver(), query_string.data(), query_string.size()))
throw BadQuery(mysql_error(conn->getDriver()), mysql_errno(conn->getDriver())); throw BadQuery(errorMessage(conn->getDriver()), mysql_errno(conn->getDriver()));
} }
UseQueryResult Query::use() UseQueryResult Query::use()