-
Notifications
You must be signed in to change notification settings - Fork 16
/
schema.go
73 lines (59 loc) · 1.88 KB
/
schema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
/*
#cgo linux LDFLAGS: -Wl,-unresolved-symbols=ignore-all
#cgo darwin LDFLAGS: -Wl,-undefined,dynamic_lookup
#include "fdw_helpers.h"
*/
import "C"
import (
"log"
"unsafe"
"github.com/turbot/go-kit/helpers"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-postgres-fdw/sql"
)
func SchemaToSql(schema map[string]*proto.TableSchema, stmt *C.ImportForeignSchemaStmt, serverOid C.Oid) *C.List {
var commands *C.List
server := C.GetForeignServer(serverOid)
if server == nil {
return nil
}
serverName := C.GoString(server.servername)
localSchema := C.GoString(stmt.local_schema)
// first figure out which tables we want
var tables []string
// iterate over table list
if stmt.table_list != nil {
for it := C.list_head(stmt.table_list); it != nil; it = C.lnext(stmt.table_list, it) {
var rv *C.RangeVar = C.cellGetRangeVar(it)
t := C.GoString(rv.relname)
tables = append(tables, t)
}
}
log.Printf("[TRACE] SchemaToSql: tables %v\n", tables)
// TODO we do not handle any options currently
for table, tableSchema := range schema {
if stmt.list_type == C.FDW_IMPORT_SCHEMA_LIMIT_TO {
log.Printf("[TRACE] list_type is FDW_IMPORT_SCHEMA_LIMIT_TO: %v", tables)
if !helpers.StringSliceContains(tables, table) {
log.Printf("[TRACE] Skipping table %s", table)
continue
}
} else if stmt.list_type == C.FDW_IMPORT_SCHEMA_EXCEPT {
log.Printf("[TRACE] list_type is FDW_IMPORT_SCHEMA_EXCEPT: %v", tables)
if helpers.StringSliceContains(tables, table) {
log.Printf("[TRACE] Skipping table %s", table)
continue
}
}
log.Printf("[TRACE] Import table %s", table)
sql, err := sql.GetSQLForTable(table, tableSchema, localSchema, serverName)
if err != nil {
FdwError(err)
return nil
}
log.Printf("[TRACE] SQL %s", sql)
commands = C.lappend(commands, unsafe.Pointer(C.CString(sql)))
}
return commands
}