ClickHouse/libs/libmysqlxx/src/Connection.cpp
2014-08-12 13:35:15 +04:00

116 lines
2.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <mysqlxx/Connection.h>
#include <mysqlxx/Exception.h>
namespace mysqlxx
{
Connection::Connection()
{
is_connected = false;
/// Инициализация библиотеки.
LibrarySingleton::instance();
}
Connection::Connection(
const char* db,
const char* server,
const char* user,
const char* password,
unsigned port,
unsigned timeout,
unsigned rw_timeout)
{
is_connected = false;
connect(db, server, user, password, port, timeout, rw_timeout);
}
Connection::~Connection()
{
disconnect();
my_thread_end();
}
void Connection::connect(const char* db,
const char* server,
const char* user,
const char* password,
unsigned port,
unsigned timeout,
unsigned rw_timeout)
{
if (is_connected)
disconnect();
/// Инициализация библиотеки.
LibrarySingleton::instance();
if (!mysql_init(&driver))
throw ConnectionFailed(errorMessage(&driver), mysql_errno(&driver));
/// Установим таймауты
if (mysql_options(&driver, MYSQL_OPT_CONNECT_TIMEOUT, reinterpret_cast<const char *>(&timeout)))
throw ConnectionFailed(errorMessage(&driver), mysql_errno(&driver));
if (mysql_options(&driver, MYSQL_OPT_READ_TIMEOUT, reinterpret_cast<const char *>(&rw_timeout)))
throw ConnectionFailed(errorMessage(&driver), mysql_errno(&driver));
if (mysql_options(&driver, MYSQL_OPT_WRITE_TIMEOUT, reinterpret_cast<const char *>(&rw_timeout)))
throw ConnectionFailed(errorMessage(&driver), mysql_errno(&driver));
/** Включаем возможность использовать запрос LOAD DATA LOCAL INFILE с серверами,
* которые были скомпилированы без опции --enable-local-infile.
*/
if (mysql_options(&driver, MYSQL_OPT_LOCAL_INFILE, nullptr))
throw ConnectionFailed(errorMessage(&driver), mysql_errno(&driver));
if (!mysql_real_connect(&driver, server, user, password, db, port, nullptr, driver.client_flag))
throw ConnectionFailed(errorMessage(&driver), mysql_errno(&driver));
/// Установим кодировки по умолчанию - UTF-8.
if (mysql_set_character_set(&driver, "UTF8"))
throw ConnectionFailed(errorMessage(&driver), mysql_errno(&driver));
/// Установим автоматический реконнект
my_bool reconnect = true;
if (mysql_options(&driver, MYSQL_OPT_RECONNECT, reinterpret_cast<const char *>(&reconnect)))
throw ConnectionFailed(errorMessage(&driver), mysql_errno(&driver));
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)
{
return Query(this, str);
}
MYSQL * Connection::getDriver()
{
return &driver;
}
}