summaryrefslogtreecommitdiff
path: root/libpqpp/pq-binarycolumn.cpp
blob: 761bf0bbb1e818c3ebac2cad6d0e4c2a7d7b6cfc (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
#include "pq-binarycolumn.h"
#include "column.h"
#include "dbTypes.h"
#include "pq-column.h"
#include <bit>
#include <cstdint>
#include <endian.h>
#include <error.h>
#include <server/catalog/pg_type_d.h>

PQ::BinaryColumn::BinaryColumn(const PQ::SelectBase * s, unsigned int f) : PQ::Column(s, f) { }

template<std::integral T>
inline T
PQ::BinaryColumn::valueAs() const
{
	T v {};
	std::memcpy(&v, value(), sizeof(T));
	if constexpr (std::endian::native != std::endian::big && sizeof(T) > 1) {
		return std::byteswap(v);
	}
	return v;
}

void
PQ::BinaryColumn::apply(DB::HandleField & h) const
{
	if (isNull()) {
		h.null();
		return;
	}
	switch (oid) {
		case CHAROID:
		case VARCHAROID:
		case TEXTOID:
		case XMLOID:
			h.string({value(), length()});
			break;
		case BOOLOID:
			h.boolean(valueAs<bool>());
			break;
		case INT2OID:
			h.integer(valueAs<int16_t>());
			break;
		case INT4OID:
			h.integer(valueAs<int32_t>());
			break;
		case INT8OID:
			h.integer(valueAs<int64_t>());
			break;
		case BYTEAOID:
			h.blob(DB::Blob(value(), length()));
			break;
		default:
			throw DB::ColumnTypeNotSupported();
	}
}