1
- use pg_sys:: AsPgCStr ;
1
+ use pg_sys:: { AsPgCStr , Oid } ;
2
2
use pgrx:: pg_sys:: panic:: ErrorReport ;
3
3
use pgrx:: { debug2, prelude:: * , PgList } ;
4
+ use std:: marker:: PhantomData ;
4
5
6
+ use crate :: instance;
5
7
use crate :: options:: options_to_hashmap;
6
8
use crate :: prelude:: ForeignDataWrapper ;
7
9
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
+
8
26
#[ repr( u32 ) ]
9
27
#[ derive( Debug , Clone ) ]
10
28
pub enum ListType {
@@ -30,10 +48,10 @@ pub(super) extern "C" fn import_foreign_schema<E: Into<ErrorReport>, W: ForeignD
30
48
) -> * mut pg_sys:: List {
31
49
debug2 ! ( "---> import_foreign_schema" ) ;
32
50
33
- let import_foreign_schema_stmt : ImportForeignSchemaStmt ;
51
+ let create_stmts : Vec < String > ;
34
52
35
53
unsafe {
36
- import_foreign_schema_stmt = ImportForeignSchemaStmt {
54
+ let import_foreign_schema_stmt = ImportForeignSchemaStmt {
37
55
server_name : std:: ffi:: CStr :: from_ptr ( ( * stmt) . server_name )
38
56
. to_str ( )
39
57
. unwrap ( )
@@ -76,11 +94,16 @@ pub(super) extern "C" fn import_foreign_schema<E: Into<ErrorReport>, W: ForeignD
76
94
} ,
77
95
78
96
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) ;
80
103
}
81
104
82
105
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 {
84
107
ret. push ( command. as_pg_cstr ( ) ) ;
85
108
}
86
109
0 commit comments