Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change import_foreign_schema() signature. #315

Merged
merged 2 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions supabase-wrappers/src/import_foreign_schema.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
use pg_sys::AsPgCStr;
use pg_sys::{AsPgCStr, Oid};
use pgrx::pg_sys::panic::ErrorReport;
use pgrx::{debug2, prelude::*, PgList};
use std::marker::PhantomData;

use crate::instance;
use crate::options::options_to_hashmap;
use crate::prelude::ForeignDataWrapper;

// Fdw private state for import_foreign_schema
struct FdwState<E: Into<ErrorReport>, W: ForeignDataWrapper<E>> {
// foreign data wrapper instance
instance: W,
_phantom: PhantomData<E>,
}

impl<E: Into<ErrorReport>, W: ForeignDataWrapper<E>> FdwState<E, W> {
unsafe fn new(foreignserverid: Oid) -> Self {
Self {
instance: instance::create_fdw_instance_from_server_id(foreignserverid),
_phantom: PhantomData,
}
}
}

#[repr(u32)]
#[derive(Debug, Clone)]
pub enum ListType {
Expand All @@ -30,10 +48,10 @@ pub(super) extern "C" fn import_foreign_schema<E: Into<ErrorReport>, W: ForeignD
) -> *mut pg_sys::List {
debug2!("---> import_foreign_schema");

let import_foreign_schema_stmt: ImportForeignSchemaStmt;
let create_stmts: Vec<String>;

unsafe {
import_foreign_schema_stmt = ImportForeignSchemaStmt {
let import_foreign_schema_stmt = ImportForeignSchemaStmt {
server_name: std::ffi::CStr::from_ptr((*stmt).server_name)
.to_str()
.unwrap()
Expand Down Expand Up @@ -76,11 +94,16 @@ pub(super) extern "C" fn import_foreign_schema<E: Into<ErrorReport>, W: ForeignD
},

options: options_to_hashmap((*stmt).options).unwrap(),
}
};

let mut state = FdwState::<E, W>::new(server_oid);
create_stmts = state
.instance
.import_foreign_schema(import_foreign_schema_stmt);
}

let mut ret: PgList<i8> = PgList::new();
for command in W::import_foreign_schema(import_foreign_schema_stmt, server_oid) {
for command in create_stmts {
ret.push(command.as_pg_cstr());
}

Expand Down
23 changes: 18 additions & 5 deletions supabase-wrappers/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@ use crate::prelude::*;
use pgrx::pg_sys::panic::ErrorReport;
use pgrx::prelude::*;

// create a fdw instance
pub(super) unsafe fn create_fdw_instance<E: Into<ErrorReport>, W: ForeignDataWrapper<E>>(
ftable_id: pg_sys::Oid,
// create a fdw instance from its id
pub(super) unsafe fn create_fdw_instance_from_server_id<
remilapeyre marked this conversation as resolved.
Show resolved Hide resolved
E: Into<ErrorReport>,
W: ForeignDataWrapper<E>,
>(
fserver_id: pg_sys::Oid,
) -> W {
let ftable = pg_sys::GetForeignTable(ftable_id);
let fserver = pg_sys::GetForeignServer((*ftable).serverid);
let fserver = pg_sys::GetForeignServer(fserver_id);
let fserver_opts = options_to_hashmap((*fserver).options).report_unwrap();
let wrapper = W::new(&fserver_opts);
wrapper.report_unwrap()
}

// create a fdw instance from a foreign table id
pub(super) unsafe fn create_fdw_instance_from_table_id<
E: Into<ErrorReport>,
W: ForeignDataWrapper<E>,
>(
ftable_id: pg_sys::Oid,
) -> W {
let ftable = pg_sys::GetForeignTable(ftable_id);
create_fdw_instance_from_server_id((*ftable).serverid)
}
2 changes: 1 addition & 1 deletion supabase-wrappers/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,8 +628,8 @@ pub trait ForeignDataWrapper<E: Into<ErrorReport>> {
///
/// [See more details](https://www.postgresql.org/docs/current/fdw-callbacks.html#FDW-CALLBACKS-IMPORT).
fn import_foreign_schema(
&mut self,
_stmt: crate::import_foreign_schema::ImportForeignSchemaStmt,
_server_oid: pg_sys::Oid,
) -> Vec<String> {
Vec::new()
}
Expand Down
2 changes: 1 addition & 1 deletion supabase-wrappers/src/modify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct FdwModifyState<E: Into<ErrorReport>, W: ForeignDataWrapper<E>> {
impl<E: Into<ErrorReport>, W: ForeignDataWrapper<E>> FdwModifyState<E, W> {
unsafe fn new(foreigntableid: Oid, tmp_ctx: PgMemoryContexts) -> Self {
Self {
instance: instance::create_fdw_instance(foreigntableid),
instance: instance::create_fdw_instance_from_table_id(foreigntableid),
rowid_name: String::default(),
rowid_attno: 0,
rowid_typid: Oid::INVALID,
Expand Down
2 changes: 1 addition & 1 deletion supabase-wrappers/src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct FdwState<E: Into<ErrorReport>, W: ForeignDataWrapper<E>> {
impl<E: Into<ErrorReport>, W: ForeignDataWrapper<E>> FdwState<E, W> {
unsafe fn new(foreigntableid: Oid, tmp_ctx: PgMemoryContexts) -> Self {
Self {
instance: instance::create_fdw_instance(foreigntableid),
instance: instance::create_fdw_instance_from_table_id(foreigntableid),
quals: Vec::new(),
tgts: Vec::new(),
sorts: Vec::new(),
Expand Down
Loading