mysqlxx: development.

This commit is contained in:
Alexey Milovidov 2011-03-05 20:47:46 +00:00
parent 109aefb00e
commit 0dedceaa50
5 changed files with 162 additions and 5 deletions

View File

@ -15,6 +15,7 @@ class Query
public: public:
Query(Connection & conn_, const std::string & query_string); Query(Connection & conn_, const std::string & query_string);
Query(const Query & other); Query(const Query & other);
Query & operator= (const Query & other);
void reset(); void reset();
void execute(); void execute();
@ -31,6 +32,11 @@ public:
return query_stream.str(); return query_stream.str();
} }
std::ostream & ostr()
{
return query_stream;
}
template <typename T> template <typename T>
Query & operator<< (const T & x) Query & operator<< (const T & x)
{ {

View File

@ -8,6 +8,7 @@
#include <string> #include <string>
#include <limits> #include <limits>
#include <Yandex/Common.h>
#include <Yandex/DateLUT.h> #include <Yandex/DateLUT.h>
#include <mysqlxx/Types.h> #include <mysqlxx/Types.h>
@ -198,6 +199,9 @@ public:
{ {
if (unlikely(isNull())) if (unlikely(isNull()))
throw Exception("Value is NULL"); throw Exception("Value is NULL");
if (!checkDateTime())
return getUInt();
Yandex::DateLUTSingleton & date_lut = Yandex::DateLUTSingleton::instance(); Yandex::DateLUTSingleton & date_lut = Yandex::DateLUTSingleton::instance();
@ -208,7 +212,7 @@ public:
m_data[5] * 10 + m_data[6], m_data[5] * 10 + m_data[6],
m_data[8] * 10 + m_data[9]); m_data[8] * 10 + m_data[9]);
} }
else if (m_length == 19) else
{ {
return date_lut.makeDateTime( return date_lut.makeDateTime(
m_data[0] * 1000 + m_data[1] * 100 + m_data[2] * 10 + m_data[3], m_data[0] * 1000 + m_data[1] * 100 + m_data[2] * 10 + m_data[3],
@ -218,8 +222,6 @@ public:
m_data[14] * 10 + m_data[15], m_data[14] * 10 + m_data[15],
m_data[17] * 10 + m_data[18]); m_data[17] * 10 + m_data[18]);
} }
else
throw Exception("Cannot parse DateTime: " + getString());
} }
time_t getDate() const time_t getDate() const
@ -227,6 +229,9 @@ public:
if (unlikely(isNull())) if (unlikely(isNull()))
throw Exception("Value is NULL"); throw Exception("Value is NULL");
if (!checkDateTime())
return getUInt();
Yandex::DateLUTSingleton & date_lut = Yandex::DateLUTSingleton::instance(); Yandex::DateLUTSingleton & date_lut = Yandex::DateLUTSingleton::instance();
if (m_length == 10 || m_length == 19) if (m_length == 10 || m_length == 19)
@ -258,6 +263,9 @@ public:
template <typename T> T get() const; template <typename T> T get() const;
/// Для совместимости
template <typename T> operator T() const { return get<T>(); }
const char * data() const { return m_data; } const char * data() const { return m_data; }
size_t length() const { return m_length; } size_t length() const { return m_length; }
size_t size() const { return m_length; } size_t size() const { return m_length; }
@ -268,7 +276,7 @@ private:
bool checkDateTime() const bool checkDateTime() const
{ {
return m_length >= 10 && m_data[4] == '-' && m_data[7] == '-'; return (m_length == 10 || m_length == 19) && m_data[4] == '-' && m_data[7] == '-';
} }
}; };
@ -289,6 +297,11 @@ template <> inline float String::get<float >() const { return getDouble()
template <> inline double String::get<double >() const { return getDouble(); } template <> inline double String::get<double >() const { return getDouble(); }
template <> inline std::string String::get<std::string >() const { return getString(); } template <> inline std::string String::get<std::string >() const { return getString(); }
template <> inline Yandex::VisitID_t String::get<Yandex::VisitID_t >() const { return Yandex::VisitID_t(getUInt()); }
//template <> inline Date String::get<Date >() const { return getString(); }
template <typename T> inline T String::get() const { return T(*this); }
inline std::ostream & operator<< (std::ostream & ostr, const String & x) inline std::ostream & operator<< (std::ostream & ostr, const String & x)
{ {

View File

@ -2,8 +2,10 @@
#define MYSQLXX_TYPES_H #define MYSQLXX_TYPES_H
#include <string> #include <string>
#include <mysql/mysql.h> #include <mysql/mysql.h>
#include <Yandex/DateLUT.h>
#include <mysqlxx/Exception.h>
namespace mysqlxx namespace mysqlxx
@ -19,9 +21,131 @@ typedef MYSQL_FIELD * MYSQL_FIELDS;
/// Для совместимости с mysql++ /// Для совместимости с mysql++
typedef time_t sql_datetime; typedef time_t sql_datetime;
typedef time_t sql_timestamp;
typedef time_t sql_date; typedef time_t sql_date;
typedef std::string sql_char; typedef std::string sql_char;
class Date
{
private:
unsigned char m_day;
unsigned char m_month;
unsigned short m_year;
public:
Date(time_t time)
{
Yandex::DateLUTSingleton & date_lut = Yandex::DateLUTSingleton::instance();
const Yandex::DateLUT::Values & values = date_lut.getValues(time);
m_day = values.day_of_month;
m_month = values.month;
m_year = values.year;
}
/// Для совместимости
Date(unsigned short year_, unsigned char month_, unsigned char day_)
: m_day(day_), m_month(month_), m_year(year_)
{
}
/// Для совместимости
Date(const std::string & s)
{
if (s.size() < 10)
throw Exception("Cannot parse Date: " + s);
m_year = s[0] * 1000 + s[1] * 100 + s[2] * 10 + s[3];
m_month = s[5] * 10 + s[6];
m_day = s[8] * 10 + s[9];
}
/// Для совместимости
Date() : m_day(1), m_month(1), m_year(2000)
{
}
/// Для совместимости
operator time_t() const
{
return Yandex::DateLUTSingleton::instance().makeDate(m_year, m_month, m_day);
}
unsigned char day() const { return m_day; }
unsigned char month() const { return m_month; }
unsigned short year() const { return m_year; }
void day(unsigned char x) { m_day = x; }
void month(unsigned char x) { m_month = x; }
void year(unsigned short x) { m_year = x; }
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);
}
bool operator== (const Date & other) const
{
return m_year == other.m_year && m_month == other.m_month && m_day == other.m_day;
}
};
inline std::ostream & operator<< (std::ostream & ostr, const Date & date)
{
return ostr << date.year()
<< '-' << (date.month() < 10 ? "0" : "") << date.month()
<< '-' << (date.day() < 10 ? "0" : "") << date.day();
}
struct NullType
{
};
extern NullType null;
template <typename T>
class Null
{
public:
T data;
bool is_null;
Null() : is_null(true) {}
Null(NullType data) : is_null(true) {}
Null(const T & data_) : data(data_), is_null(false) {}
operator T()
{
if (is_null)
throw Exception("Value is NULL");
return data;
}
Null<T> & operator= (const T & data_) { is_null = false; data = data_; return *this; }
bool isNull() { return is_null; }
bool operator< (const Null<T> & other)
{
return is_null < other.is_null
|| (is_null == other.is_null && data < other.data);
}
bool operator< (const NullType other) { return false; }
bool operator== (const Null<T> & other)
{
return is_null == other.is_null && data == other.data;
}
bool operator== (const NullType other) { return is_null; }
};
} }
#endif #endif

View File

@ -15,6 +15,12 @@ Query::Query(const Query & other) : conn(other.conn)
query_stream << other.query_stream.rdbuf(); query_stream << other.query_stream.rdbuf();
} }
Query & Query::operator= (const Query & other)
{
query_stream << other.query_stream.rdbuf();
return *this;
}
void Query::reset() void Query::reset()
{ {
query_stream.str(""); query_stream.str("");

View File

@ -0,0 +1,8 @@
#include <mysqlxx/Types.h>
namespace mysqlxx
{
NullType null = NullType();
}