mysqlxx: development.

This commit is contained in:
Alexey Milovidov 2011-03-09 19:18:01 +00:00
parent bced684a34
commit 6635e81bc1
10 changed files with 192 additions and 190 deletions

View File

@ -3,6 +3,8 @@
#include <boost/noncopyable.hpp>
#include <Poco/Util/Application.h>
#include <mysqlxx/Query.h>
@ -21,7 +23,19 @@ public:
const char* password = 0,
unsigned int port = 0);
Connection(const std::string & config_name);
Connection(const std::string & config_name)
{
is_connected = false;
Poco::Util::LayeredConfiguration & cfg = Poco::Util::Application::instance().config();
std::string db = cfg.getString(config_name + ".db");
std::string server = cfg.getString(config_name + ".host");
std::string user = cfg.getString(config_name + ".user");
std::string password = cfg.getString(config_name + ".password");
unsigned port = cfg.getInt(config_name + ".port");
connect(db.c_str(), server.c_str(), user.c_str(), password.c_str(), port);
}
virtual ~Connection();

View File

@ -0,0 +1,94 @@
#ifndef MYSQLXX_DATE_H
#define MYSQLXX_DATE_H
#include <string>
#include <Yandex/DateLUT.h>
#include <mysqlxx/Exception.h>
namespace mysqlxx
{
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;
}
bool operator!= (const Date & other) const
{
return !(*this == other);
}
};
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();
}
}
#endif

View File

@ -0,0 +1,81 @@
#ifndef MYSQLXX_NULL_H
#define MYSQLXX_NULL_H
#include <mysqlxx/Exception.h>
namespace mysqlxx
{
namespace detail
{
struct NullTypeHelper
{
int x;
};
}
typedef int detail::NullTypeHelper::* NullType;
const NullType null = 0;
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;
}
operator const T & () const
{
if (is_null)
throw Exception("Value is NULL");
return data;
}
Null<T> & operator= (const T & data_) { is_null = false; data = data_; return *this; }
Null<T> & operator= (const Null<T> & other) { is_null = other.is_null; data = other.data; return *this; }
Null<T> & operator= (const NullType other) { is_null = true; data = T(); return *this; }
bool isNull() const { return is_null; }
bool operator< (const Null<T> & other) const
{
return is_null < other.is_null
|| (is_null == other.is_null && data < other.data);
}
bool operator< (const NullType other) const { return false; }
bool operator== (const Null<T> & other) const
{
return is_null == other.is_null && data == other.data;
}
bool operator== (const NullType other) const { return is_null; }
bool operator!= (const Null<T> & other) const
{
return !(*this == other);
}
bool operator!= (const NullType other) const { return !is_null; }
};
}
#endif

View File

@ -3,9 +3,6 @@
#include <string>
#include <mysql/mysql.h>
#include <Yandex/DateLUT.h>
#include <mysqlxx/Exception.h>
namespace mysqlxx
@ -25,146 +22,6 @@ typedef time_t sql_timestamp;
typedef time_t sql_date;
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;
}
bool operator!= (const Date & other) const
{
return !(*this == other);
}
};
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;
}
operator const T & () const
{
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() const { return is_null; }
bool operator< (const Null<T> & other) const
{
return is_null < other.is_null
|| (is_null == other.is_null && data < other.data);
}
bool operator< (const NullType other) const { return false; }
bool operator== (const Null<T> & other) const
{
return is_null == other.is_null && data == other.data;
}
bool operator== (const NullType other) const { return is_null; }
bool operator!= (const Null<T> & other) const
{
return !(*this == other);
}
bool operator!= (const NullType other) const { return !is_null; }
};
}
#endif

View File

@ -5,6 +5,8 @@
#include <mysqlxx/Transaction.h>
#include <mysqlxx/Manip.h>
#include <mysqlxx/Pool.h>
#include <mysqlxx/Date.h>
#include <mysqlxx/Null.h>
/** mysqlxx - чрезвычайно простая библиотека для замены библиотеки mysql++.
*

View File

@ -1,5 +1,3 @@
#include <Poco/Util/Application.h>
#include <mysqlxx/Connection.h>
#include <mysqlxx/Exception.h>
@ -23,20 +21,6 @@ Connection::Connection(
connect(db, server, user, password, port);
}
Connection::Connection(const std::string & config_name)
{
is_connected = false;
Poco::Util::LayeredConfiguration & cfg = Poco::Util::Application::instance().config();
std::string db = cfg.getString(config_name + ".db");
std::string server = cfg.getString(config_name + ".host");
std::string user = cfg.getString(config_name + ".user");
std::string password = cfg.getString(config_name + ".password");
unsigned port = cfg.getInt(config_name + ".port");
connect(db.c_str(), server.c_str(), user.c_str(), password.c_str(), port);
}
Connection::~Connection()
{
disconnect();

View File

@ -1,9 +0,0 @@
#include <typeinfo>
#include <mysqlxx/Exception.h>
namespace mysqlxx
{
}

View File

@ -1,6 +0,0 @@
#include <mysqlxx/Row.h>
namespace mysqlxx
{
}

View File

@ -1,7 +0,0 @@
#include <mysqlxx/String.h>
namespace mysqlxx
{
}

View File

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