Skip to content

Commit

Permalink
Use seperate trait function when OID needed
Browse files Browse the repository at this point in the history
  • Loading branch information
syvb committed Oct 6, 2022
1 parent 8ea6559 commit a12b8bd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
9 changes: 9 additions & 0 deletions pgx/src/datum/anyarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ impl FromDatum for AnyArray {
datum: pg_sys::Datum,
is_null: bool,
typoid: pg_sys::Oid,
) -> Option<AnyArray> {
FromDatum::from_datum_with_typid(datum, is_null, typoid)
}

#[inline]
unsafe fn from_datum_with_typid(
datum: pg_sys::Datum,
is_null: bool,
typoid: pg_sys::Oid,
) -> Option<AnyArray> {
if is_null {
None
Expand Down
9 changes: 9 additions & 0 deletions pgx/src/datum/anyelement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ impl FromDatum for AnyElement {
datum: pg_sys::Datum,
is_null: bool,
typoid: pg_sys::Oid,
) -> Option<AnyElement> {
FromDatum::from_datum_with_typid(datum, is_null, typoid)
}

#[inline]
unsafe fn from_datum_with_typid(
datum: pg_sys::Datum,
is_null: bool,
typoid: pg_sys::Oid,
) -> Option<AnyElement> {
if is_null {
None
Expand Down
11 changes: 11 additions & 0 deletions pgx/src/datum/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ pub trait FromDatum {
where
Self: Sized;

/// Like `from_datum` but the typoid must have a value.
///
/// ## Safety
///
/// Same caveats as `FromDatum::from_datum(...)`.
unsafe fn from_datum_with_typid(datum: pg_sys::Datum, is_null: bool, typoid: pg_sys::Oid) -> Option<Self>
where
Self: Sized {
FromDatum::from_datum(datum, is_null, typoid)
}

/// Default implementation switched to the specified memory context and then simply calls
/// `FromDatum::from_datum(...)` from within that context.
///
Expand Down
18 changes: 8 additions & 10 deletions pgx/src/fcinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,11 @@ mod pg_10_11 {
let datum = unsafe { fcinfo.as_ref() }.unwrap().arg[num];
let isnull = pg_arg_is_null(fcinfo, num);
unsafe {
let typid = if T::GET_TYPOID {
super::pg_getarg_type(fcinfo, num)
if T::GET_TYPOID {
T::from_datum_with_typid(datum.value, isnull, super::pg_getarg_type(fcinfo, num))
} else {
pg_sys::InvalidOid
};
T::from_datum(datum, isnull, typid)
T::from_datum(datum.value, isnull, pg_sys::InvalidOid)
}
}
}

Expand Down Expand Up @@ -138,12 +137,11 @@ mod pg_12_13_14 {
pub fn pg_getarg<T: FromDatum>(fcinfo: pg_sys::FunctionCallInfo, num: usize) -> Option<T> {
let datum = get_nullable_datum(fcinfo, num);
unsafe {
let typid = if T::GET_TYPOID {
super::pg_getarg_type(fcinfo, num)
if T::GET_TYPOID {
T::from_datum_with_typid(datum.value, datum.isnull, super::pg_getarg_type(fcinfo, num))
} else {
pg_sys::InvalidOid
};
T::from_datum(datum.value, datum.isnull, typid)
T::from_datum(datum.value, datum.isnull, pg_sys::InvalidOid)
}
}
}

Expand Down

0 comments on commit a12b8bd

Please sign in to comment.