-
Notifications
You must be signed in to change notification settings - Fork 0
/
douban_fdw.c
88 lines (75 loc) · 2.69 KB
/
douban_fdw.c
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*-------------------------------------------------------------------------
*
* douban_fdw.c
* register for the douban foreign-data wrapper.
*-------------------------------------------------------------------------
*/
#include <sys/stat.h>
#include <unistd.h>
#include "libdbango.h" /* auto-generated by cgo */
PG_MODULE_MAGIC;
/*
* SQL functions
*/
extern Datum douban_fdw_handler(PG_FUNCTION_ARGS);
extern Datum douban_fdw_validator(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(douban_fdw_handler);
PG_FUNCTION_INFO_V1(douban_fdw_validator);
/*
* Foreign-data wrapper handler function
*/
Datum
douban_fdw_handler(PG_FUNCTION_ARGS)
{
FdwRoutine *fdwroutine = makeNode(FdwRoutine);
fdwroutine->GetForeignRelSize = doubanGetForeignRelSize;
fdwroutine->GetForeignPaths = doubanGetForeignPaths;
fdwroutine->GetForeignPlan = doubanGetForeignPlan;
fdwroutine->ExplainForeignScan = doubanExplainForeignScan;
fdwroutine->BeginForeignScan = doubanBeginForeignScan;
fdwroutine->IterateForeignScan = doubanIterateForeignScan;
fdwroutine->ReScanForeignScan = doubanReScanForeignScan;
fdwroutine->EndForeignScan = doubanEndForeignScan;
fdwroutine->AnalyzeForeignTable = doubanAnalyzeForeignTable;
PG_RETURN_POINTER(fdwroutine);
}
/*
* Validate the generic options given to a FOREIGN DATA WRAPPER, SERVER
* USER MAPPING or FOREIGN TABLE that uses douban_fdw.
*/
Datum
douban_fdw_validator(PG_FUNCTION_ARGS)
{
List *options_list = untransformRelOptions(PG_GETARG_DATUM(0));
Oid catalog = PG_GETARG_OID(1);
ListCell *cell;
int kind = 0;
if (GetDatabaseEncoding() != 0x06) /* in order to avoid including pg_wchar.h, use the actual value of "PG_UTF8" */
{
ereport(ERROR,
(errcode(ERRCODE_FDW_UNABLE_TO_CREATE_EXECUTION),
errmsg("douban_fdw cannot run on an database with the encoding \"%s\"",
GetDatabaseEncodingName())));
}
foreach(cell, options_list)
{
DefElem *def = (DefElem *) lfirst(cell);
kind = checkOptionName(def->defname, catalog);
switch (kind)
{
case 0: /* "rank_name" */
if (!checkRankName(defGetString(def)))
{
ereport(ERROR,
(errcode(ERRCODE_FDW_TABLE_NOT_FOUND),
errmsg("specified rank name \"%s\" does not exist", defGetString(def))));
}
break;
default:
ereport(ERROR,
(errcode(ERRCODE_FDW_INVALID_OPTION_NAME),
errmsg("invalid option \"%s\" for the object %d", def->defname, catalog)));
}
}
PG_RETURN_VOID();
}