Skip to content

Commit

Permalink
add ::new API for catalog wrappers
Browse files Browse the repository at this point in the history
Signed-off-by: usamoi <[email protected]>
  • Loading branch information
usamoi committed Aug 17, 2024
1 parent ef1c8d5 commit f70b053
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions pgrx/src/pg_catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,14 @@ macro_rules! define_column {
paste::paste! {
impl [<$table:camel>]<'_> {
$(#[$column_meta])*
/// # Panics
///
/// This function panics if the catalog wrapper is created by `Self::new` but there is no `cache_id` provided.
pub fn $column(&self) -> Option<$column_type> {
unsafe {
get_attr::<$column_type>(self.inner, self.cache_id, pg_sys::[<Anum_ $table _ $column>])
if let Some(cache_id) = self.cache_id {
unsafe { get_attr::<$column_type>(self.inner, cache_id, pg_sys::[<Anum_ $table _ $column>]) }
} else {
panic!("`cache_id` is not provided")
}
}
}
Expand All @@ -227,10 +232,15 @@ macro_rules! define_column {
paste::paste! {
impl [<$table:camel>]<'_> {
$(#[$column_meta])*
/// # Panics
///
/// This function panics if the catalog wrapper is created by `Self::new` but there is no `cache_id` provided.
pub fn $column(&self) -> $column_type {
unsafe {
get_attr::<$column_type>(self.inner, self.cache_id, pg_sys::[<Anum_ $table _ $column>])
}.unwrap()
if let Some(cache_id) = self.cache_id {
unsafe { get_attr::<$column_type>(self.inner, cache_id, pg_sys::[<Anum_ $table _ $column>]).unwrap() }
} else {
panic!("`cache_id` is not provided")
}
}
}
}
Expand All @@ -247,7 +257,21 @@ macro_rules! define_catalog {
pub struct [<$table:camel>]<'a> {
inner: &'a pg_sys::HeapTupleData,
#[allow(dead_code)]
cache_id: i32,
cache_id: Option<i32>,
}

impl<'a> [<$table:camel>]<'a> {
/// Create a catalog wrapper by a heap tuple.
///
/// # Safety
///
/// If `cache_id` is provided, `cache_id` must be an ID for a `syscache` for this catalog.
pub unsafe fn new(inner: &'a pg_sys::HeapTupleData, cache_id: Option<i32>) -> Self {
Self {
inner,
cache_id,
}
}
}

#[doc = concat!("The search result for pg_catalog.", stringify!($table), ".")]
Expand All @@ -266,7 +290,7 @@ macro_rules! define_catalog {
unsafe {
Some([<$table:camel>] {
inner: self.inner?.as_ref(),
cache_id: self.cache_id,
cache_id: Some(self.cache_id),
})
}
}
Expand Down Expand Up @@ -304,7 +328,7 @@ macro_rules! define_catalog {
unsafe {
Some([<$table:camel>] {
inner: syscache_get(self.inner, i)?,
cache_id: self.cache_id
cache_id: Some(self.cache_id)
})
}
}
Expand Down

0 comments on commit f70b053

Please sign in to comment.