summaryrefslogtreecommitdiff
path: root/libodbcpp/odbc-column.cpp
blob: 7c01bd362a0b15b56dc2e2ef9f9cb5a229f1daaa (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
#include "odbc-column.h"
#include "column.h"
#include "odbc-error.h"
#include "odbc-param_fwd.h"
#include "odbc-selectcommand.h"
#include <boost/date_time/gregorian_calendar.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/time.hpp>
#include <cstdio>

namespace Glib {
	class ustring;
}

ODBC::Column::Column(SelectCommand * sc, const Glib::ustring & s, unsigned int i) : DB::Column(s, i), selectCmd(sc)
{
	bindLen = 0;
}

bool
ODBC::Column::resize()
{
	return false;
}

bool
ODBC::CharArrayColumn::resize()
{
	if (bindLen >= SQLLEN(data.size())) {
		data.resize(static_cast<std::size_t>(bindLen + 1));
		Column::bind();
		if (paramCmd) {
			paramBound = false;
			Param::bind();
		}
		return true;
	}
	return false;
}

bool
ODBC::Column::isNull() const
{
	return (bindLen == SQL_NULL_DATA);
}

void
ODBC::Column::bind()
{
	RETCODE rc = SQLBindCol(selectCmd->hStmt, static_cast<SQLUSMALLINT>(colNo + 1), ctype(), rwDataAddress(),
			static_cast<SQLLEN>(size()), &bindLen);
	if (!SQL_SUCCEEDED(rc)) {
		throw Error(rc, SQL_HANDLE_STMT, selectCmd->hStmt);
	}
}

void
ODBC::SignedIntegerColumn::apply(DB::HandleField & h) const
{
	if (isNull()) {
		return h.null();
	}
	h.integer(data);
}
void
ODBC::FloatingPointColumn::apply(DB::HandleField & h) const
{
	if (isNull()) {
		return h.null();
	}
	h.floatingpoint(data);
}
void
ODBC::CharArrayColumn::apply(DB::HandleField & h) const
{
	if (isNull()) {
		return h.null();
	}
	h.string({data.data(), static_cast<std::size_t>(bindLen)});
}
void
ODBC::TimeStampColumn::apply(DB::HandleField & h) const
{
	if (isNull()) {
		return h.null();
	}
	h.timestamp(boost::posix_time::ptime(
			boost::gregorian::date(static_cast<unsigned short int>(data.year),
					static_cast<unsigned short int>(data.month), static_cast<unsigned short int>(data.day)),
			boost::posix_time::time_duration(data.hour, data.minute, data.second, data.fraction)));
}
void
ODBC::IntervalColumn::apply(DB::HandleField & h) const
{
	if (isNull()) {
		return h.null();
	}
	auto dur = boost::posix_time::time_duration((24 * data.intval.day_second.day) + data.intval.day_second.hour,
			data.intval.day_second.minute, data.intval.day_second.second, data.intval.day_second.fraction);
	h.interval(data.interval_sign ? -dur : dur);
}