Skip to content

Commit

Permalink
Add user_mapping_options
Browse files Browse the repository at this point in the history
  • Loading branch information
rebasedming authored and kysshsy committed Jan 12, 2025
1 parent 974c33d commit ecf3fa8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
23 changes: 1 addition & 22 deletions supabase-wrappers/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,7 @@ pub(super) unsafe fn create_fdw_instance_from_table_id<
let ftable = pg_sys::GetForeignTable(ftable_id);
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 user_mapping_opts = user_mapping_options(fserver);

let wrapper = W::new(fserver_opts, user_mapping_opts);
wrapper.report_unwrap()
Expand Down
26 changes: 26 additions & 0 deletions supabase-wrappers/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::collections::HashMap;
use std::ffi::CStr;
use thiserror::Error;

use super::utils::ReportableError;

#[derive(Error, Debug)]
pub enum OptionsError {
#[error("required option `{0}` is not specified")]
Expand Down Expand Up @@ -117,3 +119,27 @@ pub unsafe fn options_to_hashmap(
}
Ok(ret)
}

pub unsafe fn user_mapping_options(fserver: *mut pg_sys::ForeignServer) -> HashMap<String, String> {
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();

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(),
}
}

0 comments on commit ecf3fa8

Please sign in to comment.