summaryrefslogtreecommitdiff
path: root/libmysqlpp/my-column.cpp
blob: c2054394d43d77de969be5c799eb38f3eef52199 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "my-column.h"
#include "my-error.h"
#include "my-selectcommand.h"
#include <boost/date_time/gregorian/gregorian_types.hpp>
#include <cstring>

MySQL::ColumnBase::ColumnBase(const char * name, unsigned int i) : DB::Column(name, i), is_null(false), length(0) { }

bool
MySQL::ColumnBase::isNull() const
{
	return is_null;
}

MySQL::StringColumn::StringColumn(const char * name, unsigned int field, MYSQL_BIND * b, unsigned int len) :
	ColumnBase(name, field), value(len)
{
	b->is_null = &is_null;
	b->buffer_type = MYSQL_TYPE_STRING;
	b->is_unsigned = false;
	b->buffer = value.data();
	b->buffer_length = len;
	b->length = &length;
}

void
MySQL::StringColumn::apply(DB::HandleField & h) const
{
	if (is_null) {
		h.null();
	}
	else {
		h.string({value.data(), 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 = nullptr;
	b->buffer_length = 0;
}

void
MySQL::NullColumn::apply(DB::HandleField & h) const
{
	h.null();
}

namespace MySQL {
	template<class T, enum_field_types MT>
	Column<T, MT>::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 = false;
		b->buffer = &value;
		b->buffer_length = sizeof(T);
	}

	template<>
	void
	Column<int64_t, MYSQL_TYPE_LONGLONG>::apply(DB::HandleField & h) const
	{
		if (is_null) {
			h.null();
		}
		else {
			h.integer(value);
		}
	}
	template<>
	void
	Column<double, MYSQL_TYPE_DOUBLE>::apply(DB::HandleField & h) const
	{
		if (is_null) {
			h.null();
		}
		else {
			h.floatingpoint(value);
		}
	}
	template<>
	void
	Column<MYSQL_TIME, MYSQL_TYPE_DATETIME>::apply(DB::HandleField & h) const
	{
		if (is_null) {
			h.null();
		}
		else {
			h.timestamp(boost::posix_time::ptime(boost::gregorian::date(value.year, value.month, value.day),
					boost::posix_time::time_duration(value.hour, value.minute, value.second)
							+ boost::posix_time::microseconds(value.second_part)));
		}
	}
	template<>
	void
	Column<MYSQL_TIME, MYSQL_TYPE_TIME>::apply(DB::HandleField & h) const
	{
		if (is_null) {
			h.null();
		}
		else {
			h.interval(boost::posix_time::time_duration(value.hour, value.minute, value.second)
					+ boost::posix_time::microseconds(value.second_part));
		}
	}

	template class Column<int64_t, MYSQL_TYPE_LONGLONG>;
	template class Column<double, MYSQL_TYPE_DOUBLE>;
	template class Column<MYSQL_TIME, MYSQL_TYPE_DATETIME>;
	template class Column<MYSQL_TIME, MYSQL_TYPE_TIME>;
}