2011-03-03 19:57:34 +00:00
|
|
|
|
#include <mysqlxx/Connection.h>
|
|
|
|
|
#include <mysqlxx/Exception.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace mysqlxx
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
Connection::Connection()
|
|
|
|
|
{
|
|
|
|
|
is_connected = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Connection::Connection(
|
|
|
|
|
const char* db,
|
|
|
|
|
const char* server,
|
|
|
|
|
const char* user,
|
|
|
|
|
const char* password,
|
|
|
|
|
unsigned int port)
|
|
|
|
|
{
|
|
|
|
|
is_connected = false;
|
|
|
|
|
connect(db, server, user, password, port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Connection::~Connection()
|
|
|
|
|
{
|
|
|
|
|
disconnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Connection::connect(const char* db,
|
|
|
|
|
const char* server,
|
|
|
|
|
const char* user,
|
|
|
|
|
const char* password,
|
|
|
|
|
unsigned int port)
|
|
|
|
|
{
|
|
|
|
|
if (is_connected)
|
|
|
|
|
disconnect();
|
|
|
|
|
|
2011-03-16 20:33:39 +00:00
|
|
|
|
/// Инициализация библиотеки.
|
2011-03-15 20:56:42 +00:00
|
|
|
|
LibrarySingleton::instance();
|
|
|
|
|
|
2011-03-03 19:57:34 +00:00
|
|
|
|
if (!mysql_init(&driver))
|
2011-03-04 20:58:19 +00:00
|
|
|
|
throw ConnectionFailed(mysql_error(&driver), mysql_errno(&driver));
|
2011-03-03 19:57:34 +00:00
|
|
|
|
|
2011-05-13 16:58:53 +00:00
|
|
|
|
/// Установим таймауты
|
|
|
|
|
unsigned int timeout = MYSQLXX_TIMEOUT;
|
2011-06-29 19:59:20 +00:00
|
|
|
|
if (mysql_options(&driver, MYSQL_OPT_CONNECT_TIMEOUT, reinterpret_cast<const char *>(&timeout)))
|
2011-05-13 16:58:53 +00:00
|
|
|
|
throw ConnectionFailed(mysql_error(&driver), mysql_errno(&driver));
|
|
|
|
|
|
2011-08-23 17:46:58 +00:00
|
|
|
|
/** Включаем возможность использовать запрос LOAD DATA LOCAL INFILE с серверами,
|
|
|
|
|
* которые были скомпилированы без опции --enable-local-infile.
|
|
|
|
|
*/
|
|
|
|
|
if (mysql_options(&driver, MYSQL_OPT_LOCAL_INFILE, NULL))
|
|
|
|
|
throw ConnectionFailed(mysql_error(&driver), mysql_errno(&driver));
|
|
|
|
|
|
2011-03-03 19:57:34 +00:00
|
|
|
|
if (!mysql_real_connect(&driver, server, user, password, db, port, NULL, driver.client_flag))
|
2011-03-04 20:58:19 +00:00
|
|
|
|
throw ConnectionFailed(mysql_error(&driver), mysql_errno(&driver));
|
2011-03-03 19:57:34 +00:00
|
|
|
|
|
2011-03-16 20:33:39 +00:00
|
|
|
|
/// Установим кодировки по-умолчанию - UTF-8.
|
|
|
|
|
if (mysql_set_character_set(&driver, "UTF8"))
|
|
|
|
|
throw ConnectionFailed(mysql_error(&driver), mysql_errno(&driver));
|
|
|
|
|
|
2011-03-03 19:57:34 +00:00
|
|
|
|
is_connected = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Connection::connected() const
|
|
|
|
|
{
|
|
|
|
|
return is_connected;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Connection::disconnect()
|
|
|
|
|
{
|
|
|
|
|
if (!is_connected)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
mysql_close(&driver);
|
|
|
|
|
memset(&driver, 0, sizeof(driver));
|
|
|
|
|
is_connected = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Connection::ping()
|
|
|
|
|
{
|
|
|
|
|
return is_connected && !mysql_ping(&driver);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Query Connection::query(const std::string & str)
|
|
|
|
|
{
|
2011-03-09 20:11:29 +00:00
|
|
|
|
return Query(this, str);
|
2011-03-03 19:57:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-09 20:11:29 +00:00
|
|
|
|
MYSQL * Connection::getDriver()
|
2011-03-03 19:57:34 +00:00
|
|
|
|
{
|
2011-03-09 20:11:29 +00:00
|
|
|
|
return &driver;
|
2011-03-03 19:57:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|