Skip to content

Commit

Permalink
Pass user mappings to new()
Browse files Browse the repository at this point in the history
  • Loading branch information
rebasedming authored and kysshsy committed Jan 12, 2025
1 parent d6fe279 commit 8535900
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
30 changes: 29 additions & 1 deletion supabase-wrappers/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::ffi::CStr;
use crate::prelude::*;
use pgrx::pg_sys::panic::ErrorReport;
use pgrx::prelude::*;
use std::collections::HashMap;
use std::panic::{catch_unwind, AssertUnwindSafe};

pub struct ForeignServer {
pub server_name: String,
Expand Down Expand Up @@ -54,5 +56,31 @@ pub(super) unsafe fn create_fdw_instance_from_table_id<
ftable_id: pg_sys::Oid,
) -> W {
let ftable = pg_sys::GetForeignTable(ftable_id);
create_fdw_instance_from_server_id((*ftable).serverid)
let fserver = pg_sys::GetForeignServer((*ftable).serverid);
let fserver_opts = options_to_hashmap((*fserver).options).report_unwrap();
let user_id = pg_sys::GetUserId();

let user_mapping_exists = !pg_sys::SearchSysCache2(
pg_sys::SysCacheIdentifier_USERMAPPINGUSERSERVER as i32,
pg_sys::Datum::from(user_id),
pg_sys::Datum::from((*fserver).serverid),
)
.is_null();
let public_mapping_exists = !pg_sys::SearchSysCache2(
pg_sys::SysCacheIdentifier_USERMAPPINGUSERSERVER as i32,
pg_sys::Datum::from(pg_sys::InvalidOid),
pg_sys::Datum::from((*fserver).serverid),
)
.is_null();

let user_mapping_opts = match user_mapping_exists || public_mapping_exists {
true => {
let user_mapping = pg_sys::GetUserMapping(user_id, (*fserver).serverid);
options_to_hashmap((*user_mapping).options).report_unwrap()
}
false => HashMap::new(),
};

let wrapper = W::new(&fserver_opts, &user_mapping_opts);
wrapper.report_unwrap()
}
5 changes: 4 additions & 1 deletion supabase-wrappers/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,10 @@ pub trait ForeignDataWrapper<E: Into<ErrorReport>> {
/// You can do any initalization in this function, like saving connection
/// info or API url in an variable, but don't do heavy works like database
/// connection or API call.
fn new(server: ForeignServer) -> Result<Self, E>
fn new(
server_options: &HashMap<String, String>,
user_mapping_options: &HashMap<String, String>,
) -> Result<Self, E>
where
Self: Sized;

Expand Down

0 comments on commit 8535900

Please sign in to comment.