ClickHouse/libs/libmysqlxx/src/Connection.cpp

81 lines
1.3 KiB
C++
Raw Normal View History

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-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
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
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
}
}