summaryrefslogtreecommitdiff
path: root/libodbcpp/column.cpp
blob: 9ff6d8dc9918102359acc6199dab8d29017bc737 (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
#include <sqlext.h>
#include <stdio.h>
#include <stdlib.h>
#include "column.h"
#include "command.h"
#include "selectcommand.h"
#include "error.h"

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

ODBC::Column::~Column()
{
}

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

bool
ODBC::CharArrayColumn::resize()
{
	if (bindLen >= SQLLEN(data.size())) {
		data.resize(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::rebind(DB::Command * cmd, unsigned int idx) const
{
	meAsAParam()->paramCmd = dynamic_cast<ODBC::Command *>(cmd);
	meAsAParam()->paramIdx = idx;
	meAsAParam()->bind();
}

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

ODBC::TimeStampColumn::operator struct tm() const
{
	struct tm t;
	t << data;
	return t;
}
void operator << (SQL_TIMESTAMP_STRUCT & target, const struct tm & src)
{
	target.year = src.tm_year + 1900;
	target.month = src.tm_mon + 1;
	target.day = src.tm_mday;
	target.hour = src.tm_hour;
	target.minute = src.tm_min;
	target.second = src.tm_sec;
	target.fraction = 0;
}
void operator << (struct tm & target, const SQL_TIMESTAMP_STRUCT & src)
{
	target.tm_year = src.year - 1900;
	target.tm_mon = src.month - 1;
	target.tm_mday = src.day;
	target.tm_hour = src.hour;
	target.tm_min = src.minute;
	target.tm_sec = src.second;
}

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.front(), bindLen);
}
void
ODBC::TimeStampColumn::apply(DB::HandleField & h) const
{
	if (isNull()) return h.null();
	h.timestamp(*this);
}