29 auto res =
ffi_guard{::CreateTemplateTupleDesc}(nattrs);
32 blessed(
false), owned(
true) {
33 for (
int i = 0; i < nattrs; i++) {
45 : tupdesc(tupdesc), blessed(blessed), owned(false) {}
53 if ((*syscache).typtype != TYPTYPE_COMPOSITE) {
54 throw std::invalid_argument(
"not a composite type");
56 return ffi_guard{::lookup_rowtype_tupdesc_copy}(t.oid, (*syscache).typtypmod);
65 : tupdesc(
ffi_guard{::CreateTupleDescCopyConstr}(other.populate_compact_attribute())),
66 blessed(other.blessed), owned(other.owned) {}
72 : tupdesc(other.tupdesc), blessed(other.blessed), owned(other.owned) {}
80 tupdesc =
ffi_guard{::CreateTupleDescCopyConstr}(other.populate_compact_attribute());
81 blessed = other.blessed;
90 tupdesc = other.tupdesc;
91 blessed = other.blessed;
109 return *TupleDescAttr(tupdesc, n);
112 type get_type(
int n)
const {
114 return {att.atttypid};
129 att.atttypid = (*typ).oid;
130 att.attcollation = (*typ).typcollation;
131 att.attlen = (*typ).typlen;
132 att.attstorage = (*typ).typstorage;
133 att.attalign = (*typ).typalign;
134 att.atttypmod = (*typ).typtypmod;
135 att.attbyval = (*typ).typbyval;
138 std::string_view get_name(
int n) {
140 return NameStr(att.attname);
162 operator TupleDesc() {
163 populate_compact_attribute();
165 tupdesc =
ffi_guard{::BlessTupleDesc}(tupdesc);
171#if PG_MAJORVERSION_NUM > 16
179 return ffi_guard{::equalRowTypes}(tupdesc, other.tupdesc);
190 if (tupdesc->natts != other.tupdesc->natts)
192 if (tupdesc->tdtypeid != other.tupdesc->tdtypeid)
195 for (
int i = 0; i < other.
attributes(); i++) {
197 FormData_pg_attribute &att2 = other[i];
199 if (att1.atttypid != att2.atttypid || att1.atttypmod != att2.atttypmod ||
200 att1.attcollation != att2.attcollation || att1.attisdropped != att2.attisdropped ||
201 att1.attlen != att2.attlen || att1.attalign != att2.attalign) {
215 return ffi_guard{::equalTupleDescs}(tupdesc, other.tupdesc);
223 operator TupleDesc()
const {
return tupdesc; }
226 inline void check_bounds(
int n)
const {
227 if (n + 1 > tupdesc->natts || n < 0) {
228 throw std::out_of_range(cppgres::fmt::format(
229 "attribute index {} is out of bounds for the tuple descriptor with the size of {}", n,
233 inline void check_not_blessed()
const {
235 throw std::runtime_error(
"tuple_descriptor already blessed");
239 TupleDesc populate_compact_attribute()
const {
240#if PG_MAJORVERSION_NUM >= 18
241 for (
int i = 0; i < tupdesc->natts; i++) {
242 ffi_guard{::populate_compact_attribute}(tupdesc, i);
253static_assert(std::copy_constructible<tuple_descriptor>);
254static_assert(std::move_constructible<tuple_descriptor>);
255static_assert(std::is_copy_assignable_v<tuple_descriptor>);
269 ::lookup_rowtype_tupdesc_copy}(HeapTupleHeaderGetTypeId(
heap_tuple),
271 tuple(ctx.template alloc<HeapTupleData>()) {
272#if PG_MAJORVERSION_NUM < 18
273 tuple->t_len = HeapTupleHeaderGetDatumLength(tupdesc.operator TupleDesc());
275 tuple->t_len = HeapTupleHeaderGetDatumLength(
heap_tuple);
281 template <std::input_iterator Iter>
284 : tupdesc(tupdesc), tuple([&]() {
285 std::vector<::Datum> values;
286 std::vector<uint8_t> nulls;
287 for (
auto it = begin; it != end; ++it) {
288 auto nd = into_nullable_datum(*it);
289 values.push_back(nd.is_null() ? ::Datum(0) : nd);
290 nulls.push_back(nd.is_null() ? 1 : 0);
292 return ffi_guard{::heap_form_tuple}(this->tupdesc, values.data(),
293 reinterpret_cast<bool *
>(nulls.data()));
298 : tupdesc(tupdesc), tuple([&]() {
300 cppgres::into_nullable_datum(std::move(args))...};
301 std::array<bool,
sizeof...(D)> nulls;
302 std::ranges::copy(datums | std::views::transform([](
auto &v) {
return v.is_null(); }),
304 std::array<::Datum,
sizeof...(D)> values;
306 datums | std::views::transform([](
auto &v) {
return v.is_null() ? ::Datum(0) : v; }),
308 return ffi_guard{::heap_form_tuple}(this->tupdesc, values.data(), nulls.data());
314 int attributes()
const {
return tupdesc.operator TupleDesc()->natts; }
323 return {.oid = TupleDescAttr(tupdesc.operator TupleDesc(), n)->atttypid};
333 return {NameStr(TupleDescAttr(tupdesc.operator TupleDesc(), n)->attname)};
345#if PG_MAJORVERSION_NUM < 15
347 [](::HeapTuple tup,
int attnum, ::TupleDesc tupleDesc,
bool *isnull) {
348 return heap_getattr(tup, attnum, tupleDesc, isnull);
354 datum d(_heap_getattr(tuple, n + 1, tupdesc.operator TupleDesc(), &isnull));
369 throw std::out_of_range(cppgres::fmt::format(
"no attribute by the name of {}",
name));
379 operator HeapTuple() const noexcept {
return tuple; }
380 operator TupleDesc() const noexcept {
return tupdesc; }
387 record(
const record &other) : tupdesc(other.tupdesc), tuple(other.tuple) {}
388 record(record &&other) noexcept : tupdesc(std::move(other.tupdesc)), tuple(other.tuple) {}
389 record &operator=(
const record &other) {
390 tupdesc = other.tupdesc;
395 record &operator=(record &&other)
noexcept {
396 if (
this != &other) {
397 tupdesc = std::move(other.tupdesc);
404 inline void check_bounds(
int n)
const {
406 throw std::out_of_range(cppgres::fmt::format(
407 "attribute index {} is out of bounds for record with the size of {}", n,
attributes()));
411 tuple_descriptor tupdesc;
414static_assert(std::copy_constructible<record>);
415static_assert(std::move_constructible<record>);
416static_assert(std::is_copy_assignable_v<record>);
420 return {
reinterpret_cast<HeapTupleHeader
>(
ffi_guard{::pg_detoast_datum}(
421 reinterpret_cast<struct ::
varlena *
>(d.operator const ::Datum &()))),
429 bool is(
const type &t) {
430 if (t.oid == RECORDOID)
434 return (*cache).typtype ==
'c';
436 constexpr type type_for() {
return {.oid = RECORDOID}; }
441 { T::composite_type() } -> std::same_as<type>;
446 if (oid_ != T::composite_type().
oid) {
447 throw std::runtime_error(fmt::format(
"invalid type: expected composite type {} got {}",
448 T::composite_type().
name(),
type{.oid = oid_}.name()));
451 record rec{
reinterpret_cast<HeapTupleHeader
>(
ffi_guard{::pg_detoast_datum}(
452 reinterpret_cast<struct ::
varlena *
>(d.operator const ::Datum &()))),
454 return [&]<std::size_t... Is>(std::index_sequence<Is...>) {
456 return from_nullable_datum<utils::tuple_element_t<Is, T>>(rec.get_attribute(Is),
457 rec.attribute_type(Is).oid, mctx);
459 }(std::make_index_sequence<utils::tuple_size_v<T>>{});
463 auto res = [&]<std::size_t... Is>(std::index_sequence<Is...>) {
465 record rec{td, utils::get<Is>(t)...};
467 }(std::make_index_sequence<utils::tuple_size_v<T>>{});
472 bool is(
const type &t) {
473 if (t == T::composite_type())
477 return (*cache).typtype ==
'c';
479 constexpr type type_for() {
return T::composite_type(); }
Definition: record.hpp:440
Definition: datum.hpp:213
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:146
Heap tuple convenience wrapper.
Definition: heap_tuple.hpp:11
Definition: memory.hpp:217
Definition: memory.hpp:85
Runtime-typed value of record type.
Definition: record.hpp:263
nullable_datum operator[](std::string_view name)
Get attribute by name.
Definition: record.hpp:363
tuple_descriptor get_tuple_descriptor() const noexcept
Returns tuple descriptor.
Definition: record.hpp:385
std::string_view attribute_name(int n) const
Name of attribute using a 0-based index.
Definition: record.hpp:331
nullable_datum get_attribute(int n)
Get attribute value (datum) using a 0-based index.
Definition: record.hpp:341
nullable_datum operator[](int n)
Get attribute by 0-based index.
Definition: record.hpp:377
int attributes() const
Number of attributes in the record.
Definition: record.hpp:314
type attribute_type(int n) const
Type of attribute using a 0-based index.
Definition: record.hpp:321
Definition: syscache.hpp:27
Tuple descriptor operator.
Definition: record.hpp:18
tuple_descriptor(tuple_descriptor &&other) noexcept
Move constructor.
Definition: record.hpp:71
bool equal_types(const tuple_descriptor &other)
Determines whether two tuple descriptors have equal row types.
Definition: record.hpp:189
void set_name(int n, const name &name)
Set attribute name.
Definition: record.hpp:151
tuple_descriptor(tuple_descriptor &other)
Copy constructor.
Definition: record.hpp:64
::FormData_pg_attribute & operator[](int n) const
Get a reference to Form_pg_attribute
Definition: record.hpp:107
tuple_descriptor & operator=(tuple_descriptor &&other) noexcept
Move assignment.
Definition: record.hpp:88
tuple_descriptor(TupleDesc tupdesc, bool blessed=true)
Create a tuple descriptor for a given TupleDesc
Definition: record.hpp:44
tuple_descriptor(int nattrs, memory_context ctx=memory_context())
Create a tuple descriptor for a given number of attributes.
Definition: record.hpp:26
bool operator==(const tuple_descriptor &other)
Compare two TupleDesc structures for logical equality.
Definition: record.hpp:214
int attributes() const noexcept
Number of attributes.
Definition: record.hpp:100
tuple_descriptor(type t)
Create a tuple description for a given composite type.
Definition: record.hpp:50
bool is_blessed() const noexcept
Returns true if the tuple descriptor is blessed.
Definition: record.hpp:221
void set_type(int n, const type &type)
Set attribute type.
Definition: record.hpp:125
tuple_descriptor & operator=(const tuple_descriptor &other)
Copy assignment.
Definition: record.hpp:79
Postgres type.
Definition: type.hpp:20