Skip to content

Commit a4708eb

Browse files
authored
DatumWithOid: Add new_from_datum(Option<Datum>, Oid) and null_oid(Oid) (pgcentralfoundation#1999)
This allows the creation of dynamically typed `DatumWithOid` values where the type isn't known at compile time. Fixes pgcentralfoundation#1993
1 parent 6f59539 commit a4708eb

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

pgrx/src/datum/mod.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,22 @@ impl<'src> DatumWithOid<'src> {
171171
///
172172
/// [`Oid`]: pg_sys::Oid
173173
pub unsafe fn new<T: IntoDatum>(value: T, oid: pg_sys::Oid) -> Self {
174-
Self { datum: value.into_datum().map(|d| Datum(d, PhantomData::default())), oid }
174+
Self::new_from_datum(value.into_datum().map(|d| Datum(d, PhantomData::default())), oid)
175+
}
176+
177+
/// Construct a `DatumWithOid` given an optional [`Datum`] and [`Oid`].
178+
///
179+
/// SQL NULL is represented by passing `None` for `datum`.
180+
///
181+
/// [`Datum`]: crate::datum::Datum
182+
/// [`Oid`]: pg_sys::Oid
183+
pub unsafe fn new_from_datum(datum: Option<Datum<'src>>, oid: pg_sys::Oid) -> Self {
184+
Self { datum, oid }
185+
}
186+
187+
/// Constructs a `DatumWithOid` representing SQL NULL
188+
pub fn null_oid(oid: pg_sys::Oid) -> Self {
189+
Self { datum: None, oid }
175190
}
176191

177192
/// Construct a `DatumWithOid` containing a null value for type `T`.

0 commit comments

Comments
 (0)