Skip to content

Commit 9440656

Browse files
authored
Merge pull request #315 from remilapeyre/import-foreign-schema
Change import_foreign_schema() signature.
2 parents 37f9bea + d0603c9 commit 9440656

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed

supabase-wrappers/src/import_foreign_schema.rs

+28-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
1-
use pg_sys::AsPgCStr;
1+
use pg_sys::{AsPgCStr, Oid};
22
use pgrx::pg_sys::panic::ErrorReport;
33
use pgrx::{debug2, prelude::*, PgList};
4+
use std::marker::PhantomData;
45

6+
use crate::instance;
57
use crate::options::options_to_hashmap;
68
use crate::prelude::ForeignDataWrapper;
79

10+
// Fdw private state for import_foreign_schema
11+
struct FdwState<E: Into<ErrorReport>, W: ForeignDataWrapper<E>> {
12+
// foreign data wrapper instance
13+
instance: W,
14+
_phantom: PhantomData<E>,
15+
}
16+
17+
impl<E: Into<ErrorReport>, W: ForeignDataWrapper<E>> FdwState<E, W> {
18+
unsafe fn new(foreignserverid: Oid) -> Self {
19+
Self {
20+
instance: instance::create_fdw_instance_from_server_id(foreignserverid),
21+
_phantom: PhantomData,
22+
}
23+
}
24+
}
25+
826
#[repr(u32)]
927
#[derive(Debug, Clone)]
1028
pub enum ListType {
@@ -30,10 +48,10 @@ pub(super) extern "C" fn import_foreign_schema<E: Into<ErrorReport>, W: ForeignD
3048
) -> *mut pg_sys::List {
3149
debug2!("---> import_foreign_schema");
3250

33-
let import_foreign_schema_stmt: ImportForeignSchemaStmt;
51+
let create_stmts: Vec<String>;
3452

3553
unsafe {
36-
import_foreign_schema_stmt = ImportForeignSchemaStmt {
54+
let import_foreign_schema_stmt = ImportForeignSchemaStmt {
3755
server_name: std::ffi::CStr::from_ptr((*stmt).server_name)
3856
.to_str()
3957
.unwrap()
@@ -76,11 +94,16 @@ pub(super) extern "C" fn import_foreign_schema<E: Into<ErrorReport>, W: ForeignD
7694
},
7795

7896
options: options_to_hashmap((*stmt).options).unwrap(),
79-
}
97+
};
98+
99+
let mut state = FdwState::<E, W>::new(server_oid);
100+
create_stmts = state
101+
.instance
102+
.import_foreign_schema(import_foreign_schema_stmt);
80103
}
81104

82105
let mut ret: PgList<i8> = PgList::new();
83-
for command in W::import_foreign_schema(import_foreign_schema_stmt, server_oid) {
106+
for command in create_stmts {
84107
ret.push(command.as_pg_cstr());
85108
}
86109

supabase-wrappers/src/instance.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,26 @@ use crate::prelude::*;
22
use pgrx::pg_sys::panic::ErrorReport;
33
use pgrx::prelude::*;
44

5-
// create a fdw instance
6-
pub(super) unsafe fn create_fdw_instance<E: Into<ErrorReport>, W: ForeignDataWrapper<E>>(
7-
ftable_id: pg_sys::Oid,
5+
// create a fdw instance from its id
6+
pub(super) unsafe fn create_fdw_instance_from_server_id<
7+
E: Into<ErrorReport>,
8+
W: ForeignDataWrapper<E>,
9+
>(
10+
fserver_id: pg_sys::Oid,
811
) -> W {
9-
let ftable = pg_sys::GetForeignTable(ftable_id);
10-
let fserver = pg_sys::GetForeignServer((*ftable).serverid);
12+
let fserver = pg_sys::GetForeignServer(fserver_id);
1113
let fserver_opts = options_to_hashmap((*fserver).options).report_unwrap();
1214
let wrapper = W::new(&fserver_opts);
1315
wrapper.report_unwrap()
1416
}
17+
18+
// create a fdw instance from a foreign table id
19+
pub(super) unsafe fn create_fdw_instance_from_table_id<
20+
E: Into<ErrorReport>,
21+
W: ForeignDataWrapper<E>,
22+
>(
23+
ftable_id: pg_sys::Oid,
24+
) -> W {
25+
let ftable = pg_sys::GetForeignTable(ftable_id);
26+
create_fdw_instance_from_server_id((*ftable).serverid)
27+
}

supabase-wrappers/src/interface.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,8 @@ pub trait ForeignDataWrapper<E: Into<ErrorReport>> {
628628
///
629629
/// [See more details](https://www.postgresql.org/docs/current/fdw-callbacks.html#FDW-CALLBACKS-IMPORT).
630630
fn import_foreign_schema(
631+
&mut self,
631632
_stmt: crate::import_foreign_schema::ImportForeignSchemaStmt,
632-
_server_oid: pg_sys::Oid,
633633
) -> Vec<String> {
634634
Vec::new()
635635
}

supabase-wrappers/src/modify.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct FdwModifyState<E: Into<ErrorReport>, W: ForeignDataWrapper<E>> {
4040
impl<E: Into<ErrorReport>, W: ForeignDataWrapper<E>> FdwModifyState<E, W> {
4141
unsafe fn new(foreigntableid: Oid, tmp_ctx: PgMemoryContexts) -> Self {
4242
Self {
43-
instance: instance::create_fdw_instance(foreigntableid),
43+
instance: instance::create_fdw_instance_from_table_id(foreigntableid),
4444
rowid_name: String::default(),
4545
rowid_attno: 0,
4646
rowid_typid: Oid::INVALID,

supabase-wrappers/src/scan.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ struct FdwState<E: Into<ErrorReport>, W: ForeignDataWrapper<E>> {
5555
impl<E: Into<ErrorReport>, W: ForeignDataWrapper<E>> FdwState<E, W> {
5656
unsafe fn new(foreigntableid: Oid, tmp_ctx: PgMemoryContexts) -> Self {
5757
Self {
58-
instance: instance::create_fdw_instance(foreigntableid),
58+
instance: instance::create_fdw_instance_from_table_id(foreigntableid),
5959
quals: Vec::new(),
6060
tgts: Vec::new(),
6161
sorts: Vec::new(),

0 commit comments

Comments
 (0)