From 49286cb2d2987e4d2f6a07f6b9ed46d4de97d9ac Mon Sep 17 00:00:00 2001 From: randomdan Date: Sun, 18 Nov 2012 19:13:16 +0000 Subject: Add a basic MySQL connector, not fully functional, but will suffice for p2tv --- libmysqlpp/column.cpp | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 libmysqlpp/column.cpp (limited to 'libmysqlpp/column.cpp') diff --git a/libmysqlpp/column.cpp b/libmysqlpp/column.cpp new file mode 100644 index 0000000..cd1d811 --- /dev/null +++ b/libmysqlpp/column.cpp @@ -0,0 +1,112 @@ +#include "column.h" +#include "selectcommand.h" +#include "error.h" +#include + +MySQL::ColumnBase::ColumnBase(const char * name, unsigned int i) : + DB::Column(name, i) +{ +} + +bool +MySQL::ColumnBase::isNull() const +{ + return is_null; +} + +void +MySQL::ColumnBase::rebind(DB::Command *, unsigned int) const +{ + throw Error("Not supported"); +} + +MySQL::StringColumn::StringColumn(const char * name, unsigned int field, MYSQL_BIND * b, unsigned int len) : + ColumnBase(name, field), + value(new char[len]) +{ + b->is_null = &is_null; + b->buffer_type = MYSQL_TYPE_STRING; + b->is_unsigned = 0; + b->buffer = value; + b->buffer_length = len; + b->length = &length; +} +MySQL::StringColumn::~StringColumn() +{ + delete[] value; +} +void +MySQL::StringColumn::apply(DB::HandleField & h) const +{ + if (is_null) { + h.null(); + } + else { + h.string(value, length); + } +} +MySQL::NullColumn::NullColumn(const char * name, unsigned int field, MYSQL_BIND * b) : + ColumnBase(name, field) +{ + b->is_null = &is_null; + b->buffer_type = MYSQL_TYPE_NULL; + b->buffer = NULL; + b->buffer_length = 0; +} +void +MySQL::NullColumn::apply(DB::HandleField & h) const +{ + h.null(); +} + +namespace MySQL { + template Column::Column(const char * name, unsigned int field, MYSQL_BIND * b) : + ColumnBase(name, field) + { + b->is_null = &is_null; + b->buffer_type = MT; + b->is_unsigned = 0; + b->buffer = &value; + b->buffer_length = sizeof(T); + } + + template <> void Column::apply(DB::HandleField & h) const + { + if (is_null) { + h.null(); + } + else { + h.integer(value); + } + } + template <> void Column::apply(DB::HandleField & h) const + { + if (is_null) { + h.null(); + } + else { + h.floatingpoint(value); + } + } + template <> void Column::apply(DB::HandleField & h) const + { + if (is_null) { + h.null(); + } + else { + struct tm tm; + memset(&tm, 0, sizeof(tm)); + tm.tm_year = value.year - 1900; + tm.tm_mon = value.month - 1; + tm.tm_mday = value.day; + tm.tm_hour = value.hour; + tm.tm_min = value.minute; + tm.tm_sec = value.second; + h.timestamp(tm); + } + } + + template class Column; + template class Column; + template class Column; +} -- cgit v1.2.3