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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use crate::{pg_sys, void_mut_ptr, FromDatum, PgBox, PgRelation};
use std::ops::Deref;
pub struct PgTupleDesc<'a> {
tupdesc: PgBox<pg_sys::TupleDescData>,
parent: Option<&'a PgRelation>,
data: Option<PgBox<pg_sys::HeapTupleData>>,
need_release: bool,
need_pfree: bool,
}
impl<'a> PgTupleDesc<'a> {
pub unsafe fn from_pg<'b>(ptr: pg_sys::TupleDesc) -> PgTupleDesc<'b> {
PgTupleDesc {
tupdesc: PgBox::from_pg(ptr),
parent: None,
data: None,
need_release: true,
need_pfree: false,
}
}
pub fn from_pg_copy<'b>(ptr: pg_sys::TupleDesc) -> PgTupleDesc<'b> {
PgTupleDesc {
tupdesc: PgBox::from_pg(unsafe { pg_sys::CreateTupleDescCopyConstr(ptr) }),
parent: None,
data: None,
need_release: false,
need_pfree: true,
}
}
pub unsafe fn from_pg_is_copy<'b>(ptr: pg_sys::TupleDesc) -> PgTupleDesc<'b> {
PgTupleDesc {
tupdesc: PgBox::from_pg(ptr),
parent: None,
data: None,
need_release: false,
need_pfree: true,
}
}
pub fn from_relation(parent: &PgRelation) -> PgTupleDesc {
PgTupleDesc {
tupdesc: PgBox::from_pg(parent.rd_att),
parent: Some(parent),
data: None,
need_release: false,
need_pfree: false,
}
}
pub unsafe fn from_composite(composite: pg_sys::Datum) -> Self {
let htup_header =
pg_sys::pg_detoast_datum(composite as *mut pg_sys::varlena) as pg_sys::HeapTupleHeader;
let tup_type = crate::heap_tuple_header_get_type_id(htup_header);
let tup_typmod = crate::heap_tuple_header_get_typmod(htup_header);
let tupdesc = pg_sys::lookup_rowtype_tupdesc(tup_type, tup_typmod);
let mut data = PgBox::<pg_sys::HeapTupleData>::alloc();
data.t_len = crate::heap_tuple_header_get_datum_length(htup_header) as u32;
data.t_data = htup_header;
PgTupleDesc {
tupdesc: PgBox::from_pg(tupdesc),
parent: None,
data: Some(data),
need_release: true,
need_pfree: false,
}
}
pub fn parent(&self) -> Option<&PgRelation> {
self.parent
}
pub fn oid(&self) -> pg_sys::Oid {
self.tupdesc.tdtypeid
}
pub fn typmod(&self) -> i32 {
self.tupdesc.tdtypmod
}
pub fn len(&self) -> usize {
self.tupdesc.natts as usize
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn get(&self, i: usize) -> Option<&pg_sys::FormData_pg_attribute> {
if i >= self.len() {
None
} else {
Some(tupdesc_get_attr(&self.tupdesc, i))
}
}
pub fn get_attr<T: FromDatum>(&self, attno: usize) -> Option<T> {
crate::heap_getattr(
self.data
.as_ref()
.expect("no composite data associated with this PgTupleDesc"),
attno + 1,
&self,
)
}
pub fn iter(&self) -> TupleDescIterator {
TupleDescIterator {
tupdesc: self,
curr: 0,
}
}
}
impl<'a> Deref for PgTupleDesc<'a> {
type Target = PgBox<pg_sys::TupleDescData>;
fn deref(&self) -> &Self::Target {
&self.tupdesc
}
}
impl<'a> Drop for PgTupleDesc<'a> {
fn drop(&mut self) {
if self.need_release {
unsafe { release_tupdesc(self.tupdesc.as_ptr()) }
} else if self.need_pfree {
unsafe { pg_sys::pfree(self.tupdesc.as_ptr() as void_mut_ptr) }
}
}
}
pub unsafe fn release_tupdesc(ptr: pg_sys::TupleDesc) {
if (*ptr).tdrefcount >= 0 {
pg_sys::DecrTupleDescRefCount(ptr)
}
}
#[cfg(feature = "pg10")]
#[inline]
fn tupdesc_get_attr(
tupdesc: &PgBox<pg_sys::TupleDescData>,
attno: usize,
) -> &pg_sys::FormData_pg_attribute {
let atts = unsafe { std::slice::from_raw_parts(tupdesc.attrs, tupdesc.natts as usize) };
unsafe { atts[attno].as_ref().unwrap() }
}
#[cfg(any(feature = "pg11", feature = "pg12", feature = "pg13"))]
#[inline]
fn tupdesc_get_attr(
tupdesc: &PgBox<pg_sys::TupleDescData>,
attno: usize,
) -> &pg_sys::FormData_pg_attribute {
let atts = unsafe { tupdesc.attrs.as_slice(tupdesc.natts as usize) };
&atts[attno]
}
pub struct TupleDescIterator<'a> {
tupdesc: &'a PgTupleDesc<'a>,
curr: usize,
}
impl<'a> Iterator for TupleDescIterator<'a> {
type Item = &'a pg_sys::FormData_pg_attribute;
fn next(&mut self) -> Option<Self::Item> {
let result = self.tupdesc.get(self.curr);
self.curr += 1;
result
}
}
pub struct TupleDescDataIntoIterator<'a> {
tupdesc: PgTupleDesc<'a>,
curr: usize,
}
impl<'a> IntoIterator for PgTupleDesc<'a> {
type Item = pg_sys::FormData_pg_attribute;
type IntoIter = TupleDescDataIntoIterator<'a>;
fn into_iter(self) -> Self::IntoIter {
TupleDescDataIntoIterator {
tupdesc: self,
curr: 0,
}
}
}
impl<'a> Iterator for TupleDescDataIntoIterator<'a> {
type Item = pg_sys::FormData_pg_attribute;
fn next(&mut self) -> Option<Self::Item> {
let result = match self.tupdesc.get(self.curr) {
Some(result) => *result,
None => {
return None;
}
};
self.curr += 1;
Some(result)
}
}