ClickHouse/base/mysqlxx/Connection.cpp

160 lines
4.1 KiB
C++
Raw Normal View History

#if __has_include(<mysql.h>)
#include <mysql.h>
#else
#include <mysql/mysql.h>
#endif
2011-03-03 19:57:34 +00:00
#include <mysqlxx/Connection.h>
#include <mysqlxx/Exception.h>
static inline const char* ifNotEmpty(const char* s)
{
return s && *s ? s : nullptr;
}
2011-03-03 19:57:34 +00:00
namespace mysqlxx
{
LibrarySingleton::LibrarySingleton()
{
if (mysql_library_init(0, nullptr, nullptr))
throw Exception("Cannot initialize MySQL library.");
}
LibrarySingleton::~LibrarySingleton()
{
mysql_library_end();
}
auto & LibrarySingleton::instance()
{
static LibrarySingleton instance;
return instance;
}
2011-03-03 19:57:34 +00:00
Connection::Connection()
: driver(std::make_unique<MYSQL>())
2011-03-03 19:57:34 +00:00
{
/// MySQL library initialization.
LibrarySingleton::instance();
2011-03-03 19:57:34 +00:00
}
Connection::Connection(
const char* db,
const char* server,
const char* user,
const char* password,
unsigned port,
2017-07-14 12:45:32 +00:00
const char * socket,
const char* ssl_ca,
const char* ssl_cert,
const char* ssl_key,
unsigned timeout,
2018-10-01 15:55:21 +00:00
unsigned rw_timeout,
bool enable_local_infile)
: Connection()
2011-03-03 19:57:34 +00:00
{
2018-10-01 15:55:21 +00:00
connect(db, server, user, password, port, socket, ssl_ca, ssl_cert, ssl_key, timeout, rw_timeout, enable_local_infile);
2011-03-03 19:57:34 +00:00
}
Connection::Connection(const std::string & config_name)
: Connection()
{
connect(config_name);
}
2011-03-03 19:57:34 +00:00
Connection::~Connection()
{
disconnect();
mysql_thread_end();
2011-03-03 19:57:34 +00:00
}
void Connection::connect(const char* db,
2017-12-13 19:55:45 +00:00
const char * server,
const char * user,
const char * password,
unsigned port,
2017-07-14 12:45:32 +00:00
const char * socket,
2017-12-13 19:55:45 +00:00
const char * ssl_ca,
const char * ssl_cert,
const char * ssl_key,
unsigned timeout,
2018-10-01 15:55:21 +00:00
unsigned rw_timeout,
bool enable_local_infile)
2011-03-03 19:57:34 +00:00
{
if (is_connected)
disconnect();
2011-03-03 19:57:34 +00:00
if (!mysql_init(driver.get()))
throw ConnectionFailed(errorMessage(driver.get()), mysql_errno(driver.get()));
is_initialized = true;
2011-03-03 19:57:34 +00:00
/// Set timeouts.
if (mysql_options(driver.get(), MYSQL_OPT_CONNECT_TIMEOUT, &timeout))
throw ConnectionFailed(errorMessage(driver.get()), mysql_errno(driver.get()));
2011-05-13 16:58:53 +00:00
if (mysql_options(driver.get(), MYSQL_OPT_READ_TIMEOUT, &rw_timeout))
throw ConnectionFailed(errorMessage(driver.get()), mysql_errno(driver.get()));
if (mysql_options(driver.get(), MYSQL_OPT_WRITE_TIMEOUT, &rw_timeout))
throw ConnectionFailed(errorMessage(driver.get()), mysql_errno(driver.get()));
2018-10-01 15:55:21 +00:00
/// Disable LOAD DATA LOCAL INFILE because it is insecure if necessary.
unsigned enable_local_infile_arg = static_cast<unsigned>(enable_local_infile);
if (mysql_options(driver.get(), MYSQL_OPT_LOCAL_INFILE, &enable_local_infile_arg))
throw ConnectionFailed(errorMessage(driver.get()), mysql_errno(driver.get()));
/// Enables auto-reconnect.
bool reconnect = true;
if (mysql_options(driver.get(), MYSQL_OPT_RECONNECT, reinterpret_cast<const char *>(&reconnect)))
throw ConnectionFailed(errorMessage(driver.get()), mysql_errno(driver.get()));
/// Specifies particular ssl key and certificate if it needs
if (mysql_ssl_set(driver.get(), ifNotEmpty(ssl_key), ifNotEmpty(ssl_cert), ifNotEmpty(ssl_ca), nullptr, nullptr))
throw ConnectionFailed(errorMessage(driver.get()), mysql_errno(driver.get()));
if (!mysql_real_connect(driver.get(), server, user, password, db, port, ifNotEmpty(socket), driver->client_flag))
throw ConnectionFailed(errorMessage(driver.get()), mysql_errno(driver.get()));
2011-03-03 19:57:34 +00:00
/// Sets UTF-8 as default encoding.
if (mysql_set_character_set(driver.get(), "UTF8"))
throw ConnectionFailed(errorMessage(driver.get()), mysql_errno(driver.get()));
2011-03-16 20:33:39 +00:00
is_connected = true;
2011-03-03 19:57:34 +00:00
}
bool Connection::connected() const
{
return is_connected;
2011-03-03 19:57:34 +00:00
}
void Connection::disconnect()
{
if (!is_initialized)
return;
2011-03-03 19:57:34 +00:00
mysql_close(driver.get());
memset(driver.get(), 0, sizeof(*driver));
is_initialized = false;
is_connected = false;
2011-03-03 19:57:34 +00:00
}
bool Connection::ping()
{
return is_connected && !mysql_ping(driver.get());
2011-03-03 19:57:34 +00:00
}
Query Connection::query(const std::string & str)
{
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
{
return driver.get();
2011-03-03 19:57:34 +00:00
}
}