2013-10-26 02:50:23 +00:00
|
|
|
|
#pragma once
|
2011-03-03 19:57:34 +00:00
|
|
|
|
|
2013-10-26 02:50:23 +00:00
|
|
|
|
#include <sstream>
|
2011-03-03 19:57:34 +00:00
|
|
|
|
#include <mysql/mysql.h>
|
|
|
|
|
|
|
|
|
|
#include <Poco/Exception.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace mysqlxx
|
|
|
|
|
{
|
|
|
|
|
|
2011-03-18 20:26:54 +00:00
|
|
|
|
/** Общий класс исключений, которые могут быть выкинуты функциями из библиотеки.
|
|
|
|
|
* Функции code() и errnum() возвращают номер ошибки MySQL. (см. mysqld_error.h)
|
|
|
|
|
*/
|
2011-03-04 20:58:19 +00:00
|
|
|
|
struct Exception : public Poco::Exception
|
|
|
|
|
{
|
|
|
|
|
Exception(const std::string & msg, int code = 0) : Poco::Exception(msg, code) {}
|
2011-07-20 17:30:31 +00:00
|
|
|
|
int errnum() const { return code(); }
|
2011-07-20 17:55:33 +00:00
|
|
|
|
const char * name() const throw() { return "mysqlxx::Exception"; }
|
|
|
|
|
const char * className() const throw() { return "mysqlxx::Exception"; }
|
2011-03-04 20:58:19 +00:00
|
|
|
|
};
|
|
|
|
|
|
2011-03-18 20:26:54 +00:00
|
|
|
|
|
|
|
|
|
/// Не удалось соединиться с сервером.
|
2011-03-04 20:58:19 +00:00
|
|
|
|
struct ConnectionFailed : public Exception
|
|
|
|
|
{
|
|
|
|
|
ConnectionFailed(const std::string & msg, int code = 0) : Exception(msg, code) {}
|
2011-07-20 17:55:33 +00:00
|
|
|
|
const char * name() const throw() { return "mysqlxx::ConnectionFailed"; }
|
|
|
|
|
const char * className() const throw() { return "mysqlxx::ConnectionFailed"; }
|
2011-03-04 20:58:19 +00:00
|
|
|
|
};
|
|
|
|
|
|
2011-03-18 20:26:54 +00:00
|
|
|
|
|
|
|
|
|
/// Запрос содержит ошибку.
|
2011-03-04 20:58:19 +00:00
|
|
|
|
struct BadQuery : public Exception
|
|
|
|
|
{
|
|
|
|
|
BadQuery(const std::string & msg, int code = 0) : Exception(msg, code) {}
|
2011-07-20 17:55:33 +00:00
|
|
|
|
const char * name() const throw() { return "mysqlxx::BadQuery"; }
|
|
|
|
|
const char * className() const throw() { return "mysqlxx::BadQuery"; }
|
2011-03-04 20:58:19 +00:00
|
|
|
|
};
|
2011-03-03 19:57:34 +00:00
|
|
|
|
|
|
|
|
|
|
2011-03-18 20:26:54 +00:00
|
|
|
|
/// Невозможно распарсить значение.
|
|
|
|
|
struct CannotParseValue : public Exception
|
|
|
|
|
{
|
|
|
|
|
CannotParseValue(const std::string & msg, int code = 0) : Exception(msg, code) {}
|
2011-07-20 17:55:33 +00:00
|
|
|
|
const char * name() const throw() { return "mysqlxx::CannotParseValue"; }
|
|
|
|
|
const char * className() const throw() { return "mysqlxx::CannotParseValue"; }
|
2011-03-18 20:26:54 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2013-10-26 02:50:23 +00:00
|
|
|
|
inline std::string errorMessage(MYSQL * driver)
|
|
|
|
|
{
|
|
|
|
|
std::stringstream res;
|
|
|
|
|
res << mysql_error(driver) << " (" << driver->host << ":" << driver->port << ")";
|
|
|
|
|
return res.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-03-18 20:26:54 +00:00
|
|
|
|
/// Для внутренних нужд библиотеки.
|
2011-03-09 20:11:29 +00:00
|
|
|
|
inline void checkError(MYSQL * driver)
|
2011-03-03 19:57:34 +00:00
|
|
|
|
{
|
2011-03-09 20:11:29 +00:00
|
|
|
|
unsigned num = mysql_errno(driver);
|
2011-03-03 19:57:34 +00:00
|
|
|
|
|
|
|
|
|
if (num)
|
2013-10-26 02:50:23 +00:00
|
|
|
|
throw Exception(errorMessage(driver), num);
|
2011-03-03 19:57:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-18 20:26:54 +00:00
|
|
|
|
|
|
|
|
|
/// Для внутренних нужд библиотеки.
|
2011-03-09 20:11:29 +00:00
|
|
|
|
inline void onError(MYSQL * driver)
|
2011-03-03 19:57:34 +00:00
|
|
|
|
{
|
2013-10-26 02:50:23 +00:00
|
|
|
|
throw Exception(errorMessage(driver), mysql_errno(driver));
|
2011-03-03 19:57:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|