Cppgres
Build Postgres extensions in C++
Loading...
Searching...
No Matches
record.hpp
1#pragma once
2
3#include "imports.h"
4#include "name.hpp"
5#include "syscache.hpp"
6#include "type.hpp"
7#include "types.hpp"
8
9#include <algorithm>
10#include <memory>
11#include <ranges>
12#include <vector>
13
14namespace cppgres {
15
22
30 : tupdesc(([&]() {
32 auto res = ffi_guard{::CreateTemplateTupleDesc}(nattrs);
33 return res;
34 }())),
35 blessed(false), owned(true) {
36 for (int i = 0; i < nattrs; i++) {
37 operator[](i).attcollation = InvalidOid;
38 operator[](i).attisdropped = false;
39 }
40 }
47 tuple_descriptor(TupleDesc tupdesc, bool blessed = true)
48 : tupdesc(tupdesc), blessed(blessed), owned(false) {}
49
54 : tuple_descriptor([&]() {
56 if ((*syscache).typtype != TYPTYPE_COMPOSITE) {
57 throw std::invalid_argument("not a composite type");
58 }
59 return ffi_guard{::lookup_rowtype_tupdesc_copy}(t.oid, (*syscache).typtypmod);
60 }()) {}
61
68 : tupdesc(ffi_guard{::CreateTupleDescCopyConstr}(other.populate_compact_attribute())),
69 blessed(other.blessed), owned(other.owned) {}
70
75 : tupdesc(other.tupdesc), blessed(other.blessed), owned(other.owned) {}
76
83 tupdesc = ffi_guard{::CreateTupleDescCopyConstr}(other.populate_compact_attribute());
84 blessed = other.blessed;
85 return *this;
86 }
87
92 if (this != &other) {
93 tupdesc = other.tupdesc;
94 blessed = other.blessed;
95 owned = other.owned;
96 }
97 return *this;
98 }
99
103 int attributes() const noexcept { return tupdesc->natts; }
104
110 ::FormData_pg_attribute &operator[](int n) const {
111 check_bounds(n);
112 return *TupleDescAttr(tupdesc, n);
113 }
114
115 type get_type(int n) const {
116 auto &att = operator[](n);
117 return {att.atttypid};
118 }
119
128 void set_type(int n, const type &type) {
129 check_not_blessed();
131 auto &att = operator[](n);
132 att.atttypid = (*typ).oid;
133 att.attcollation = (*typ).typcollation;
134 att.attlen = (*typ).typlen;
135 att.attstorage = (*typ).typstorage;
136 att.attalign = (*typ).typalign;
137 att.atttypmod = (*typ).typtypmod;
138 att.attbyval = (*typ).typbyval;
139 }
140
141 std::string_view get_name(int n) {
142 auto &att = operator[](n);
143 return NameStr(att.attname);
144 }
145
154 void set_name(int n, const name &name) {
155 check_not_blessed();
156 auto &att = operator[](n);
157 att.attname = name;
158 }
159
165 operator TupleDesc() {
166 populate_compact_attribute();
167 if (!blessed) {
168 tupdesc = ffi_guard{::BlessTupleDesc}(tupdesc);
169 blessed = true;
170 }
171 return tupdesc;
172 }
173
174#if PG_MAJORVERSION_NUM > 16
181 bool equal_row_types(const tuple_descriptor &other) {
182 return ffi_guard{::equalRowTypes}(tupdesc, other.tupdesc);
183 }
184#endif
185
192 bool equal_types(const tuple_descriptor &other) {
193 if (tupdesc->natts != other.tupdesc->natts)
194 return false;
195 if (tupdesc->tdtypeid != other.tupdesc->tdtypeid)
196 return false;
197
198 for (int i = 0; i < other.attributes(); i++) {
199 FormData_pg_attribute &att1 = operator[](i);
200 FormData_pg_attribute &att2 = other[i];
201
202 if (att1.atttypid != att2.atttypid || att1.atttypmod != att2.atttypmod ||
203 att1.attcollation != att2.attcollation || att1.attisdropped != att2.attisdropped ||
204 att1.attlen != att2.attlen || att1.attalign != att2.attalign) {
205 return false;
206 }
207 }
208
209 return true;
210 }
211
217 bool operator==(const tuple_descriptor &other) {
218 return ffi_guard{::equalTupleDescs}(tupdesc, other.tupdesc);
219 }
220
224 bool is_blessed() const noexcept { return blessed; }
225
226 operator TupleDesc() const { return tupdesc; }
227
228private:
229 inline void check_bounds(int n) const {
230 if (n + 1 > tupdesc->natts || n < 0) {
231 throw std::out_of_range(cppgres::fmt::format(
232 "attribute index {} is out of bounds for the tuple descriptor with the size of {}", n,
233 tupdesc->natts));
234 }
235 }
236 inline void check_not_blessed() const {
237 if (blessed) {
238 throw std::runtime_error("tuple_descriptor already blessed");
239 }
240 }
241
242 TupleDesc populate_compact_attribute() const {
243#if PG_MAJORVERSION_NUM >= 18
244 for (int i = 0; i < tupdesc->natts; i++) {
245 ffi_guard{::populate_compact_attribute}(tupdesc, i);
246 }
247#endif
248 return tupdesc;
249 }
250
251 TupleDesc tupdesc;
252 bool blessed;
253 bool owned;
254};
255
256static_assert(std::copy_constructible<tuple_descriptor>);
257static_assert(std::move_constructible<tuple_descriptor>);
258static_assert(std::is_copy_assignable_v<tuple_descriptor>);
259
266struct record {
267
268 friend struct datum_conversion<record>;
269
270 record(HeapTupleHeader heap_tuple, abstract_memory_context &ctx)
271 : tupdesc(/* FIXME: can we use the non-copy version with refcounting? */ ffi_guard{
272 ::lookup_rowtype_tupdesc_copy}(HeapTupleHeaderGetTypeId(heap_tuple),
273 HeapTupleHeaderGetTypMod(heap_tuple))),
274 tuple(ctx.template alloc<HeapTupleData>()) {
275#if PG_MAJORVERSION_NUM < 18
276 tuple->t_len = HeapTupleHeaderGetDatumLength(tupdesc.operator TupleDesc());
277#else
278 tuple->t_len = HeapTupleHeaderGetDatumLength(heap_tuple);
279#endif
280 tuple->t_data = heap_tuple;
281 }
282 record(HeapTupleHeader heap_tuple, abstract_memory_context &&ctx) : record(heap_tuple, ctx) {}
283
284 template <std::input_iterator Iter>
286 record(tuple_descriptor &tupdesc, Iter begin, Iter end)
287 : tupdesc(tupdesc), tuple([&]() {
288 std::vector<::Datum> values;
289 std::vector<bool> null_flags;
290 for (auto it = begin; it != end; ++it) {
291 auto nd = into_nullable_datum(*it);
292 values.push_back(nd.is_null() ? ::Datum(0) : nd);
293 null_flags.push_back(nd.is_null());
294 }
295 auto nulls = std::make_unique<bool[]>(null_flags.size());
296 std::ranges::copy(null_flags, nulls.get());
297 return ffi_guard{::heap_form_tuple}(this->tupdesc, values.data(), nulls.get());
298 }()) {}
299
300 template <convertible_into_nullable_datum... D>
301 record(tuple_descriptor &tupdesc, D &&...args)
302 : tupdesc(tupdesc), tuple([&]() {
303 std::array<nullable_datum, sizeof...(D)> datums = {
304 cppgres::into_nullable_datum(std::move(args))...};
305 std::array<bool, sizeof...(D)> nulls;
306 std::ranges::copy(datums | std::views::transform([](auto &v) { return v.is_null(); }),
307 nulls.begin());
308 std::array<::Datum, sizeof...(D)> values;
309 std::ranges::copy(
310 datums | std::views::transform([](auto &v) { return v.is_null() ? ::Datum(0) : v; }),
311 values.begin());
312 return ffi_guard{::heap_form_tuple}(this->tupdesc, values.data(), nulls.data());
313 }()) {}
314
318 int attributes() const { return tupdesc.operator TupleDesc()->natts; }
319
325 type attribute_type(int n) const {
326 check_bounds(n);
327 return {.oid = TupleDescAttr(tupdesc.operator TupleDesc(), n)->atttypid};
328 }
329
335 std::string_view attribute_name(int n) const {
336 check_bounds(n);
337 return {NameStr(TupleDescAttr(tupdesc.operator TupleDesc(), n)->attname)};
338 }
339
346 bool isnull;
347 check_bounds(n);
348 auto _heap_getattr = ffi_guard{
349#if PG_MAJORVERSION_NUM < 15
350 // Handle the fact that it is a macro
351 [](::HeapTuple tup, int attnum, ::TupleDesc tupleDesc, bool *isnull) {
352 return heap_getattr(tup, attnum, tupleDesc, isnull);
353 }
354#else
355 ::heap_getattr
356#endif
357 };
358 datum d(_heap_getattr(tuple, n + 1, tupdesc.operator TupleDesc(), &isnull));
359 return isnull ? nullable_datum() : nullable_datum(d);
360 }
361
367 nullable_datum operator[](std::string_view name) {
368 for (int i = 0; i < attributes(); i++) {
369 if (attribute_name(i) == name) {
370 return get_attribute(i);
371 }
372 }
373 throw std::out_of_range(cppgres::fmt::format("no attribute by the name of {}", name));
374 }
375
382
383 operator HeapTuple() const noexcept { return tuple; }
384 operator TupleDesc() const noexcept { return tupdesc; }
385
389 tuple_descriptor get_tuple_descriptor() const noexcept { return tupdesc; }
390
391 record(const record &other) : tupdesc(other.tupdesc), tuple(other.tuple) {}
392 record(record &&other) noexcept : tupdesc(std::move(other.tupdesc)), tuple(other.tuple) {}
393 record &operator=(const record &other) {
394 tupdesc = other.tupdesc;
395 tuple = other.tuple;
396 return *this;
397 }
398
399 record &operator=(record &&other) noexcept {
400 if (this != &other) {
401 tupdesc = std::move(other.tupdesc);
402 tuple = other.tuple;
403 }
404 return *this;
405 }
406
407private:
408 inline void check_bounds(int n) const {
409 if (n + 1 > attributes() || n < 0) {
410 throw std::out_of_range(cppgres::fmt::format(
411 "attribute index {} is out of bounds for record with the size of {}", n, attributes()));
412 }
413 }
414
415 tuple_descriptor tupdesc;
416 HeapTuple tuple;
417};
418static_assert(std::copy_constructible<record>);
419static_assert(std::move_constructible<record>);
420static_assert(std::is_copy_assignable_v<record>);
421
422template <> struct datum_conversion<record> : default_datum_conversion<record> {
423 static record from_datum(const datum &d, oid, std::optional<memory_context> ctx) {
424 return {reinterpret_cast<HeapTupleHeader>(ffi_guard{::pg_detoast_datum}(
425 reinterpret_cast<struct ::varlena *>(d.operator const ::Datum &()))),
426 ctx.has_value() ? ctx.value() : memory_context()};
427 }
428
429 static datum into_datum(const record &t) { return datum(PointerGetDatum(t.tuple)); }
430};
431
432template <> struct type_traits<record> {
433 bool is(const type &t) {
434 if (t.oid == RECORDOID)
435 return true;
436 // Check if it is a composite type and therefore can be coerced to a record
437 syscache<Form_pg_type, oid> cache(t.oid);
438 return (*cache).typtype == 'c';
439 }
440 constexpr type type_for() { return {.oid = RECORDOID}; }
441};
442
443template <typename T>
444concept composite_type = requires {
445 { T::composite_type() } -> std::same_as<type>;
446};
447
448template <composite_type T> struct datum_conversion<T> : default_datum_conversion<T> {
449 static T from_datum(const datum &d, oid oid_, std::optional<memory_context> ctx) {
450 if (oid_ != T::composite_type().oid) {
451 throw std::runtime_error(fmt::format("invalid type: expected composite type {} got {}",
452 T::composite_type().name(), type{.oid = oid_}.name()));
453 }
454 auto mctx = ctx.has_value() ? ctx.value() : memory_context();
455 record rec{reinterpret_cast<HeapTupleHeader>(ffi_guard{::pg_detoast_datum}(
456 reinterpret_cast<struct ::varlena *>(d.operator const ::Datum &()))),
457 mctx};
458 return [&]<std::size_t... Is>(std::index_sequence<Is...>) {
459 return T{([&] {
460 return from_nullable_datum<utils::tuple_element_t<Is, T>>(rec.get_attribute(Is),
461 rec.attribute_type(Is).oid, mctx);
462 }())...};
463 }(std::make_index_sequence<utils::tuple_size_v<T>>{});
464 }
465
466 static datum into_datum(const T &t) {
467 auto res = [&]<std::size_t... Is>(std::index_sequence<Is...>) {
468 tuple_descriptor td(T::composite_type());
469 record rec{td, utils::get<Is>(t)...};
470 return datum(ffi_guard{::heap_copy_tuple_as_datum}(rec, rec));
471 }(std::make_index_sequence<utils::tuple_size_v<T>>{});
472 return res;
473 }
474};
475template <composite_type T> struct type_traits<T> {
476 bool is(const type &t) {
477 if (t == T::composite_type())
478 return true;
479 // Check if it is a composite type and therefore can be coerced to a record
480 syscache<Form_pg_type, oid> cache(t.oid);
481 return (*cache).typtype == 'c';
482 }
483 constexpr type type_for() { return T::composite_type(); }
484};
485
486} // namespace cppgres
Definition: record.hpp:444
Definition: memory.hpp:14
A trait to convert from and into a cppgres::datum.
Definition: datum.hpp:114
static T from_datum(const datum &, const oid, std::optional< memory_context > context=std::nullopt)=delete
Convert from a datum.
static datum into_datum(const T &d)=delete
Convert datum into a type.
Definition: datum.hpp:39
Definition: datum.hpp:146
Definition: guard.hpp:19
Heap tuple convenience wrapper.
Definition: heap_tuple.hpp:11
Definition: memory.hpp:240
Definition: memory.hpp:103
Definition: name.hpp:7
Definition: datum.hpp:60
Definition: datum.hpp:17
Runtime-typed value of record type.
Definition: record.hpp:266
nullable_datum operator[](std::string_view name)
Get attribute by name.
Definition: record.hpp:367
tuple_descriptor get_tuple_descriptor() const noexcept
Returns tuple descriptor.
Definition: record.hpp:389
std::string_view attribute_name(int n) const
Name of attribute using a 0-based index.
Definition: record.hpp:335
nullable_datum get_attribute(int n)
Get attribute value (datum) using a 0-based index.
Definition: record.hpp:345
nullable_datum operator[](int n)
Get attribute by 0-based index.
Definition: record.hpp:381
int attributes() const
Number of attributes in the record.
Definition: record.hpp:318
type attribute_type(int n) const
Type of attribute using a 0-based index.
Definition: record.hpp:325
Definition: syscache.hpp:27
Tuple descriptor operator.
Definition: record.hpp:21
tuple_descriptor(tuple_descriptor &&other) noexcept
Move constructor.
Definition: record.hpp:74
bool equal_types(const tuple_descriptor &other)
Determines whether two tuple descriptors have equal row types.
Definition: record.hpp:192
void set_name(int n, const name &name)
Set attribute name.
Definition: record.hpp:154
tuple_descriptor(tuple_descriptor &other)
Copy constructor.
Definition: record.hpp:67
::FormData_pg_attribute & operator[](int n) const
Get a reference to Form_pg_attribute
Definition: record.hpp:110
tuple_descriptor & operator=(tuple_descriptor &&other) noexcept
Move assignment.
Definition: record.hpp:91
tuple_descriptor(TupleDesc tupdesc, bool blessed=true)
Create a tuple descriptor for a given TupleDesc
Definition: record.hpp:47
tuple_descriptor(int nattrs, memory_context ctx=memory_context())
Create a tuple descriptor for a given number of attributes.
Definition: record.hpp:29
bool operator==(const tuple_descriptor &other)
Compare two TupleDesc structures for logical equality.
Definition: record.hpp:217
int attributes() const noexcept
Number of attributes.
Definition: record.hpp:103
tuple_descriptor(type t)
Create a tuple description for a given composite type.
Definition: record.hpp:53
bool is_blessed() const noexcept
Returns true if the tuple descriptor is blessed.
Definition: record.hpp:224
void set_type(int n, const type &type)
Set attribute type.
Definition: record.hpp:128
tuple_descriptor & operator=(const tuple_descriptor &other)
Copy assignment.
Definition: record.hpp:82
Definition: type.hpp:42
Postgres type.
Definition: type.hpp:21
Definition: type.hpp:105