mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-21 15:12:02 +00:00
mysqlxx: development.
This commit is contained in:
parent
c7b18f8972
commit
7b05734af8
@ -5,12 +5,33 @@
|
||||
|
||||
#include <Poco/Util/Application.h>
|
||||
|
||||
#include <Yandex/singleton.h>
|
||||
|
||||
#include <mysqlxx/Query.h>
|
||||
|
||||
|
||||
namespace mysqlxx
|
||||
{
|
||||
|
||||
|
||||
/// Для корректной инициализации и деинициализации MySQL библиотеки.
|
||||
class LibrarySingleton : public Singleton<LibrarySingleton>
|
||||
{
|
||||
friend class Singleton<LibrarySingleton>;
|
||||
private:
|
||||
LibrarySingleton()
|
||||
{
|
||||
if (mysql_library_init(0, NULL, NULL))
|
||||
throw Exception("Cannot initialize MySQL library.");
|
||||
}
|
||||
|
||||
~LibrarySingleton()
|
||||
{
|
||||
mysql_library_end();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class Connection : private boost::noncopyable
|
||||
{
|
||||
public:
|
||||
|
@ -1,6 +1,7 @@
|
||||
#ifndef MYSQLXX_DATE_H
|
||||
#define MYSQLXX_DATE_H
|
||||
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <Yandex/DateLUT.h>
|
||||
|
||||
@ -10,7 +11,8 @@
|
||||
namespace mysqlxx
|
||||
{
|
||||
|
||||
class Date
|
||||
/// packed - для memcmp
|
||||
class __attribute__ ((__packed__)) Date
|
||||
{
|
||||
private:
|
||||
unsigned short m_year;
|
||||
@ -38,7 +40,7 @@ private:
|
||||
}
|
||||
|
||||
public:
|
||||
Date(time_t time)
|
||||
explicit Date(time_t time)
|
||||
{
|
||||
init(time);
|
||||
}
|
||||
@ -48,7 +50,7 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
Date(const std::string & s)
|
||||
explicit Date(const std::string & s)
|
||||
{
|
||||
init(s.data(), s.size());
|
||||
}
|
||||
@ -63,6 +65,12 @@ public:
|
||||
init(time(0));
|
||||
}
|
||||
|
||||
Date & operator= (time_t time)
|
||||
{
|
||||
init(time);
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator time_t() const
|
||||
{
|
||||
return Yandex::DateLUTSingleton::instance().makeDate(m_year, m_month, m_day);
|
||||
@ -78,14 +86,12 @@ public:
|
||||
|
||||
bool operator< (const Date & other) const
|
||||
{
|
||||
return m_year < other.m_year
|
||||
|| (m_year == other.m_year && m_month < other.m_month)
|
||||
|| (m_year == other.m_year && m_month == other.m_month && m_day < other.m_day);
|
||||
return -1 == memcmp(this, &other, sizeof(*this));
|
||||
}
|
||||
|
||||
bool operator== (const Date & other) const
|
||||
{
|
||||
return m_year == other.m_year && m_month == other.m_month && m_day == other.m_day;
|
||||
return 0 == memcmp(this, &other, sizeof(*this));
|
||||
}
|
||||
|
||||
bool operator!= (const Date & other) const
|
||||
|
@ -4,13 +4,14 @@
|
||||
#include <string>
|
||||
#include <Yandex/DateLUT.h>
|
||||
|
||||
#include <mysqlxx/Exception.h>
|
||||
#include <mysqlxx/Date.h>
|
||||
|
||||
|
||||
namespace mysqlxx
|
||||
{
|
||||
|
||||
class DateTime
|
||||
/// packed - для memcmp
|
||||
class __attribute__ ((__packed__)) DateTime
|
||||
{
|
||||
private:
|
||||
unsigned short m_year;
|
||||
@ -48,7 +49,7 @@ private:
|
||||
}
|
||||
|
||||
public:
|
||||
DateTime(time_t time)
|
||||
explicit DateTime(time_t time)
|
||||
{
|
||||
init(time);
|
||||
}
|
||||
@ -59,7 +60,7 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
DateTime(const std::string & s)
|
||||
explicit DateTime(const std::string & s)
|
||||
{
|
||||
if (s.size() < 19)
|
||||
throw Exception("Cannot parse DateTime: " + s);
|
||||
@ -83,6 +84,12 @@ public:
|
||||
init(data, length);
|
||||
}
|
||||
|
||||
DateTime & operator= (time_t time)
|
||||
{
|
||||
init(time);
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator time_t() const
|
||||
{
|
||||
return Yandex::DateLUTSingleton::instance().makeDateTime(m_year, m_month, m_day, m_hour, m_minute, m_second);
|
||||
@ -102,28 +109,16 @@ public:
|
||||
void minute(unsigned char x) { m_minute = x; }
|
||||
void second(unsigned char x) { m_second = x; }
|
||||
|
||||
bool operator< (const DateTime & other) const
|
||||
Date toDate() const { return Date(m_year, m_month, m_day); }
|
||||
|
||||
bool operator< (const Date & other) const
|
||||
{
|
||||
if (m_year < other.m_year)
|
||||
return true;
|
||||
else if (m_month < other.m_month)
|
||||
return true;
|
||||
else if (m_day < other.m_day)
|
||||
return true;
|
||||
else if (m_hour < other.m_hour)
|
||||
return true;
|
||||
else if (m_minute < other.m_minute)
|
||||
return true;
|
||||
else if (m_second < other.m_second)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
return -1 == memcmp(this, &other, sizeof(*this));
|
||||
}
|
||||
|
||||
bool operator== (const DateTime & other) const
|
||||
bool operator== (const Date & other) const
|
||||
{
|
||||
return m_year == other.m_year && m_month == other.m_month && m_day == other.m_day
|
||||
&& m_hour == other.m_hour && m_minute == other.m_minute && m_second == other.m_second;
|
||||
return 0 == memcmp(this, &other, sizeof(*this));
|
||||
}
|
||||
|
||||
bool operator!= (const DateTime & other) const
|
||||
|
@ -21,7 +21,7 @@ public:
|
||||
void execute();
|
||||
UseQueryResult use();
|
||||
StoreQueryResult store();
|
||||
|
||||
|
||||
UInt64 insertID();
|
||||
|
||||
/// Для совместимости
|
||||
@ -35,6 +35,8 @@ public:
|
||||
private:
|
||||
Connection * conn;
|
||||
std::stringbuf query_buf;
|
||||
|
||||
void executeImpl();
|
||||
};
|
||||
|
||||
|
||||
|
@ -21,7 +21,10 @@ public:
|
||||
unsigned getNumFields() { return num_fields; }
|
||||
MYSQL_RES * getRes() { return res; }
|
||||
|
||||
virtual ~ResultBase() {}
|
||||
virtual ~ResultBase()
|
||||
{
|
||||
mysql_free_result(res);
|
||||
}
|
||||
|
||||
protected:
|
||||
MYSQL_RES * res;
|
||||
|
@ -19,7 +19,8 @@ private:
|
||||
* Взято из mysql++.
|
||||
*/
|
||||
typedef MYSQL_ROW Row::*private_bool_type;
|
||||
|
||||
void this_type_does_not_support_comparisons() const {}
|
||||
|
||||
public:
|
||||
Row() : row(NULL), res(NULL)
|
||||
{
|
||||
@ -64,6 +65,21 @@ private:
|
||||
ResultBase * res;
|
||||
};
|
||||
|
||||
|
||||
template <typename T>
|
||||
bool operator!=(const Row & lhs, const T & rhs)
|
||||
{
|
||||
lhs.this_type_does_not_support_comparisons();
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool operator==(const Row & lhs, const T & rhs)
|
||||
{
|
||||
lhs.this_type_does_not_support_comparisons();
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -14,8 +14,11 @@ class UseQueryResult : public ResultBase
|
||||
{
|
||||
public:
|
||||
UseQueryResult(MYSQL_RES * res_, Connection * conn_);
|
||||
|
||||
Row fetch_row();
|
||||
|
||||
Row fetch();
|
||||
|
||||
/// Для совместимости
|
||||
Row fetch_row() { return fetch(); }
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -35,6 +35,8 @@ void Connection::connect(const char* db,
|
||||
if (is_connected)
|
||||
disconnect();
|
||||
|
||||
LibrarySingleton::instance();
|
||||
|
||||
if (!mysql_init(&driver))
|
||||
throw ConnectionFailed(mysql_error(&driver), mysql_errno(&driver));
|
||||
|
||||
|
@ -42,7 +42,7 @@ void Query::reset()
|
||||
query_buf.str("");
|
||||
}
|
||||
|
||||
void Query::execute()
|
||||
void Query::executeImpl()
|
||||
{
|
||||
std::string query_string = query_buf.str();
|
||||
if (mysql_real_query(conn->getDriver(), query_string.data(), query_string.size()))
|
||||
@ -51,7 +51,7 @@ void Query::execute()
|
||||
|
||||
UseQueryResult Query::use()
|
||||
{
|
||||
execute();
|
||||
executeImpl();
|
||||
MYSQL_RES * res = mysql_use_result(conn->getDriver());
|
||||
if (!res)
|
||||
onError(conn->getDriver());
|
||||
@ -61,7 +61,7 @@ UseQueryResult Query::use()
|
||||
|
||||
StoreQueryResult Query::store()
|
||||
{
|
||||
execute();
|
||||
executeImpl();
|
||||
MYSQL_RES * res = mysql_store_result(conn->getDriver());
|
||||
if (!res)
|
||||
checkError(conn->getDriver());
|
||||
@ -69,6 +69,11 @@ StoreQueryResult Query::store()
|
||||
return StoreQueryResult(res, conn);
|
||||
}
|
||||
|
||||
void Query::execute()
|
||||
{
|
||||
executeImpl();
|
||||
}
|
||||
|
||||
UInt64 Query::insertID()
|
||||
{
|
||||
return mysql_insert_id(conn->getDriver());
|
||||
|
@ -1,3 +1,4 @@
|
||||
#include <mysqlxx/Connection.h>
|
||||
#include <mysqlxx/ResultBase.h>
|
||||
|
||||
#include <iostream>
|
||||
|
@ -9,7 +9,7 @@ UseQueryResult::UseQueryResult(MYSQL_RES * res_, Connection * conn_) : ResultBas
|
||||
{
|
||||
}
|
||||
|
||||
Row UseQueryResult::fetch_row()
|
||||
Row UseQueryResult::fetch()
|
||||
{
|
||||
MYSQL_ROW row = mysql_fetch_row(res);
|
||||
if (!row)
|
||||
|
Loading…
Reference in New Issue
Block a user