summaryrefslogtreecommitdiff
path: root/libpqpp/column.cpp
blob: 1bd212aaa3bc2e2d8f9d892a74770db3a77a856b (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
#include "column.h"
#include "selectcommand.h"
#include "error.h"
#include <string.h>

PQ::Column::Column(const SelectCommand * s, unsigned int i) :
	DB::Column(PQfname(s->execRes, i), i),
	sc(s),
	oid(PQftype(sc->execRes, colNo))
{
}

bool
PQ::Column::isNull() const
{
	return PQgetisnull(sc->execRes, sc->tuple, colNo);
}

void
PQ::Column::apply(DB::HandleField & h) const
{
	if (isNull()) {
		h.null();
		return;
	}
	struct tm tm;
	switch (oid) {
		case 18: //CHAROID:
		case 25: //TEXTOID:
		case 142: //XMLOID:
			h.string(PQgetvalue(sc->execRes, sc->tuple, colNo), PQgetlength(sc->execRes, sc->tuple, colNo));
			break;
		case 16: //BOOLOID:
			h.integer(PQgetvalue(sc->execRes, sc->tuple, colNo)[0] == 't' ? 1 : 0);
			break;
		case 21: //INT2OID:
		case 23: //INT4OID:
		case 20: //INT8OID:
			h.integer(atol(PQgetvalue(sc->execRes, sc->tuple, colNo)));
			break;
		case 1700: //NUMERICOID:
		case 700: //FLOAT4OID:
		case 701: //FLOAT8OID:
			h.floatingpoint(atof(PQgetvalue(sc->execRes, sc->tuple, colNo)));
			break;
		case 1083: //TIMEOID:
			memset(&tm, 0, sizeof(tm));
			strptime(PQgetvalue(sc->execRes, sc->tuple, colNo), "%T", &tm);
			h.timestamp(tm);
			break;
		case 1082: //DATEOID:
			memset(&tm, 0, sizeof(tm));
			strptime(PQgetvalue(sc->execRes, sc->tuple, colNo), "%F", &tm);
			h.timestamp(tm);
			break;
		case 702: //ABSTIMEOID:
		case 1114: //TIMESTAMPOID:
		case 1184: //TIMESTAMPTZOID:
			strptime(PQgetvalue(sc->execRes, sc->tuple, colNo), "%F %T", &tm);
			h.timestamp(tm);
			break;
		default:
			fprintf(stderr, "Unknown Oid %d\n", oid);
			throw Error("Unknown Oid");
	}
}

void
PQ::Column::rebind(DB::Command *, unsigned int) const
{
	throw Error("Not supported");
}