32 auto res =
ffi_guard{::CreateTemplateTupleDesc}(nattrs);
35 blessed(
false), owned(
true) {
36 for (
int i = 0; i < nattrs; i++) {
39#if PG_MAJORVERSION_NUM < 18
55 : tupdesc(tupdesc), blessed(blessed), owned(false) {}
63 if ((*syscache).typtype != TYPTYPE_COMPOSITE) {
64 throw std::invalid_argument(
"not a composite type");
66 return ffi_guard{::lookup_rowtype_tupdesc_copy}(t.oid, (*syscache).typtypmod);
75 : tupdesc(
ffi_guard{::CreateTupleDescCopyConstr}(other.populate_compact_attribute())),
76 blessed(other.blessed), owned(other.owned) {}
82 : tupdesc(other.tupdesc), blessed(other.blessed), owned(other.owned) {}
90 tupdesc =
ffi_guard{::CreateTupleDescCopyConstr}(other.populate_compact_attribute());
91 blessed = other.blessed;
100 tupdesc = other.tupdesc;
101 blessed = other.blessed;
119 return *TupleDescAttr(tupdesc, n);
122 type get_type(
int n)
const {
124 return {att.atttypid};
139 att.atttypid = (*typ).oid;
140 att.attcollation = (*typ).typcollation;
141 att.attlen = (*typ).typlen;
142 att.attstorage = (*typ).typstorage;
143 att.attalign = (*typ).typalign;
144 att.atttypmod = (*typ).typtypmod;
145 att.attbyval = (*typ).typbyval;
148 std::string_view get_name(
int n) {
150 return NameStr(att.attname);
172 operator TupleDesc() {
173 populate_compact_attribute();
175 tupdesc =
ffi_guard{::BlessTupleDesc}(tupdesc);
181#if PG_MAJORVERSION_NUM > 16
189 return ffi_guard{::equalRowTypes}(tupdesc, other.tupdesc);
200 if (tupdesc->natts != other.tupdesc->natts)
202 if (tupdesc->tdtypeid != other.tupdesc->tdtypeid)
205 for (
int i = 0; i < other.
attributes(); i++) {
207 FormData_pg_attribute &att2 = other[i];
209 if (att1.atttypid != att2.atttypid || att1.atttypmod != att2.atttypmod ||
210 att1.attcollation != att2.attcollation || att1.attisdropped != att2.attisdropped ||
211 att1.attlen != att2.attlen || att1.attalign != att2.attalign) {
225 return ffi_guard{::equalTupleDescs}(tupdesc, other.tupdesc);
233 operator TupleDesc()
const {
return tupdesc; }
236 inline void check_bounds(
int n)
const {
237 if (n + 1 > tupdesc->natts || n < 0) {
238 throw std::out_of_range(cppgres::fmt::format(
239 "attribute index {} is out of bounds for the tuple descriptor with the size of {}", n,
243 inline void check_not_blessed()
const {
245 throw std::runtime_error(
"tuple_descriptor already blessed");
249 TupleDesc populate_compact_attribute()
const {
250#if PG_MAJORVERSION_NUM >= 18
251 for (
int i = 0; i < tupdesc->natts; i++) {
252 ffi_guard{::populate_compact_attribute}(tupdesc, i);
263static_assert(std::copy_constructible<tuple_descriptor>);
264static_assert(std::move_constructible<tuple_descriptor>);
265static_assert(std::is_copy_assignable_v<tuple_descriptor>);
279 ::lookup_rowtype_tupdesc_copy}(HeapTupleHeaderGetTypeId(
heap_tuple),
281 tuple(ctx.template alloc<HeapTupleData>()) {
282#if PG_MAJORVERSION_NUM < 18
283 tuple->t_len = HeapTupleHeaderGetDatumLength(tupdesc.operator TupleDesc());
285 tuple->t_len = HeapTupleHeaderGetDatumLength(
heap_tuple);
291 template <std::input_iterator Iter>
294 : tupdesc(tupdesc), tuple([&]() {
295 std::vector<::Datum> values;
296 std::vector<bool> null_flags;
297 for (
auto it = begin; it != end; ++it) {
298 auto nd = into_nullable_datum(*it);
299 values.push_back(nd.is_null() ? ::Datum(0) : nd);
300 null_flags.push_back(nd.is_null());
302 auto nulls = std::make_unique<bool[]>(null_flags.size());
303 std::ranges::copy(null_flags, nulls.get());
304 return ffi_guard{::heap_form_tuple}(this->tupdesc, values.data(), nulls.get());
309 : tupdesc(tupdesc), tuple([&]() {
311 cppgres::into_nullable_datum(std::move(args))...};
312 std::array<bool,
sizeof...(D)> nulls;
313 std::ranges::copy(datums | std::views::transform([](
auto &v) {
return v.is_null(); }),
315 std::array<::Datum,
sizeof...(D)> values;
317 datums | std::views::transform([](
auto &v) {
return v.is_null() ? ::Datum(0) : v; }),
319 return ffi_guard{::heap_form_tuple}(this->tupdesc, values.data(), nulls.data());
325 int attributes()
const {
return tupdesc.operator TupleDesc()->natts; }
334 return {.oid = TupleDescAttr(tupdesc.operator TupleDesc(), n)->atttypid};
344 return {NameStr(TupleDescAttr(tupdesc.operator TupleDesc(), n)->attname)};
356#if PG_MAJORVERSION_NUM < 15
358 [](::HeapTuple tup,
int attnum, ::TupleDesc tupleDesc,
bool *isnull) {
359 return heap_getattr(tup, attnum, tupleDesc, isnull);
365 datum d(_heap_getattr(tuple, n + 1, tupdesc.operator TupleDesc(), &isnull));
380 throw std::out_of_range(cppgres::fmt::format(
"no attribute by the name of {}",
name));
390 operator HeapTuple() const noexcept {
return tuple; }
391 operator TupleDesc() const noexcept {
return tupdesc; }
398 record(
const record &other) : tupdesc(other.tupdesc), tuple(other.tuple) {}
399 record(record &&other) noexcept : tupdesc(std::move(other.tupdesc)), tuple(other.tuple) {}
400 record &operator=(
const record &other) {
401 tupdesc = other.tupdesc;
406 record &operator=(record &&other)
noexcept {
407 if (
this != &other) {
408 tupdesc = std::move(other.tupdesc);
415 inline void check_bounds(
int n)
const {
417 throw std::out_of_range(cppgres::fmt::format(
418 "attribute index {} is out of bounds for record with the size of {}", n,
attributes()));
422 tuple_descriptor tupdesc;
425static_assert(std::copy_constructible<record>);
426static_assert(std::move_constructible<record>);
427static_assert(std::is_copy_assignable_v<record>);
431 return {
reinterpret_cast<HeapTupleHeader
>(
ffi_guard{::pg_detoast_datum}(
432 reinterpret_cast<struct ::
varlena *
>(d.operator const ::Datum &()))),
440 bool is(
const type &t) {
441 if (t.oid == RECORDOID)
445 return (*cache).typtype ==
'c';
447 constexpr type type_for() {
return {.oid = RECORDOID}; }
452 { T::composite_type() } -> std::same_as<type>;
457 if (oid_ != T::composite_type().
oid) {
458 throw std::runtime_error(fmt::format(
"invalid type: expected composite type {} got {}",
459 T::composite_type().
name(),
type{.oid = oid_}.name()));
462 record rec{
reinterpret_cast<HeapTupleHeader
>(
ffi_guard{::pg_detoast_datum}(
463 reinterpret_cast<struct ::
varlena *
>(d.operator const ::Datum &()))),
465 return [&]<std::size_t... Is>(std::index_sequence<Is...>) {
467 return from_nullable_datum<utils::tuple_element_t<Is, T>>(rec.get_attribute(Is),
468 rec.attribute_type(Is).oid, mctx);
470 }(std::make_index_sequence<utils::tuple_size_v<T>>{});
474 auto res = [&]<std::size_t... Is>(std::index_sequence<Is...>) {
476 record rec{td, utils::get<Is>(t)...};
478 }(std::make_index_sequence<utils::tuple_size_v<T>>{});
483 bool is(
const type &t) {
484 if (t == T::composite_type())
488 return (*cache).typtype ==
'c';
490 constexpr type type_for() {
return T::composite_type(); }
Definition: record.hpp:451
Definition: datum.hpp:213
Definition: memory.hpp:15
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:146
Heap tuple convenience wrapper.
Definition: heap_tuple.hpp:11
Definition: memory.hpp:276
Definition: memory.hpp:139
Runtime-typed value of record type.
Definition: record.hpp:273
nullable_datum operator[](std::string_view name)
Get attribute by name.
Definition: record.hpp:374
tuple_descriptor get_tuple_descriptor() const noexcept
Returns tuple descriptor.
Definition: record.hpp:396
std::string_view attribute_name(int n) const
Name of attribute using a 0-based index.
Definition: record.hpp:342
nullable_datum get_attribute(int n)
Get attribute value (datum) using a 0-based index.
Definition: record.hpp:352
nullable_datum operator[](int n)
Get attribute by 0-based index.
Definition: record.hpp:388
int attributes() const
Number of attributes in the record.
Definition: record.hpp:325
type attribute_type(int n) const
Type of attribute using a 0-based index.
Definition: record.hpp:332
Definition: syscache.hpp:27
Tuple descriptor operator.
Definition: record.hpp:21
tuple_descriptor(tuple_descriptor &&other) noexcept
Move constructor.
Definition: record.hpp:81
bool equal_types(const tuple_descriptor &other)
Determines whether two tuple descriptors have equal row types.
Definition: record.hpp:199
void set_name(int n, const name &name)
Set attribute name.
Definition: record.hpp:161
tuple_descriptor(tuple_descriptor &other)
Copy constructor.
Definition: record.hpp:74
::FormData_pg_attribute & operator[](int n) const
Get a reference to Form_pg_attribute
Definition: record.hpp:117
tuple_descriptor & operator=(tuple_descriptor &&other) noexcept
Move assignment.
Definition: record.hpp:98
tuple_descriptor(TupleDesc tupdesc, bool blessed=true)
Create a tuple descriptor for a given TupleDesc
Definition: record.hpp:54
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:224
int attributes() const noexcept
Number of attributes.
Definition: record.hpp:110
tuple_descriptor(type t)
Create a tuple description for a given composite type.
Definition: record.hpp:60
bool is_blessed() const noexcept
Returns true if the tuple descriptor is blessed.
Definition: record.hpp:231
void set_type(int n, const type &type)
Set attribute type.
Definition: record.hpp:135
tuple_descriptor & operator=(const tuple_descriptor &other)
Copy assignment.
Definition: record.hpp:89
Postgres type.
Definition: type.hpp:22