-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoption.c
401 lines (354 loc) · 10.8 KB
/
option.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*-------------------------------------------------------------------------
*
* option.c
* FDW option handling for treasuredata_fdw
*
* Portions Copyright (c) 2016, Mitsunori Komatsu
*
* IDENTIFICATION
* option.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "treasuredata_fdw.h"
#include "access/reloptions.h"
#include "catalog/pg_foreign_server.h"
#include "catalog/pg_foreign_table.h"
#include "catalog/pg_user_mapping.h"
#include "commands/defrem.h"
#include "commands/extension.h"
#include "utils/builtins.h"
extern Datum treasuredata_fdw_validator(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(treasuredata_fdw_validator);
/*
* Describes the valid options for objects that this wrapper uses.
*/
typedef struct PgFdwOption
{
const char *keyword;
Oid optcontext; /* OID of catalog in which option may appear */
// bool is_libpq_opt; /* true if it's used in libpq */
} PgFdwOption;
static const struct PgFdwOption valid_options[] =
{
{"endpoint", ForeignTableRelationId},
{"query_engine", ForeignTableRelationId},
{"apikey", ForeignTableRelationId},
{"database", ForeignTableRelationId},
{"table", ForeignTableRelationId},
{"query", ForeignTableRelationId},
{"query_download_dir", ForeignTableRelationId},
{"import_file_size", ForeignTableRelationId},
{"atomic_import", ForeignTableRelationId},
/* Sentinel */
{NULL, InvalidOid}
};
/*
* Check if the option is valid.
*/
static void
validate_option(DefElem *def, Oid context)
{
const struct PgFdwOption *opt;
bool is_valid = false;
for (opt = valid_options; opt->keyword; opt++)
{
if (context == opt->optcontext && strcmp(opt->keyword, def->defname) == 0)
is_valid = true;
}
if (!is_valid)
{
StringInfoData buf;
/*
* Unknown option specified, complain about it. Provide a hint
* with list of valid options for the object.
*/
initStringInfo(&buf);
for (opt = valid_options; opt->keyword; opt++)
{
if (context == opt->optcontext)
appendStringInfo(&buf, "%s%s", (buf.len > 0) ? ", " : "",
opt->keyword);
}
ereport(ERROR,
(errcode(ERRCODE_FDW_INVALID_OPTION_NAME),
errmsg("invalid option \"%s\"", def->defname),
buf.len > 0
? errhint("Valid options in this context are: %s", buf.data)
: errhint("There are no valid options in this context.")));
}
}
static int my_strcasecmp(const char *s1, const char *s2)
{
const unsigned char *p1 = (const unsigned char *) s1;
const unsigned char *p2 = (const unsigned char *) s2;
int result;
if (p1 == p2)
return 0;
while ((result = tolower(*p1) - tolower(*p2++)) == 0)
if (*p1++ == '\0')
break;
return result;
}
Datum
treasuredata_fdw_validator(PG_FUNCTION_ARGS)
{
List *options_list = untransformRelOptions(PG_GETARG_DATUM(0));
Oid catalog = PG_GETARG_OID(1);
char *endpoint = NULL;
char *query_engine = NULL;
char *apikey = NULL;
char *database = NULL;
char *table = NULL;
char *query = NULL;
char *query_download_dir = NULL;
char *import_file_size = NULL;
char *atomic_import = NULL;
ListCell *cell;
/*
* Check that only options supported by treasuredata_fdw, and allowed for the
* current object type, are given.
*/
foreach(cell, options_list)
{
DefElem *def = (DefElem *) lfirst(cell);
/*
* Check if the option is valid with looking up Option.
*/
validate_option(def, catalog);
if (strcmp(def->defname, "endpoint") == 0)
{
if (endpoint)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("conflicting or redundant options")));
/*
* Option value can be obtained by useing defGetXXX() function:
* typically defGetString(), defGetNumeric(), defGetBoolean() or
* defGetInt64().
*
* See commands/defrem.h for more information about defGetXXX()
* functions.
*/
endpoint = defGetString(def);
}
else if (strcmp(def->defname, "query_engine") == 0)
{
if (query_engine)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("conflicting or redundant options")));
query_engine = defGetString(def);
}
else if (strcmp(def->defname, "apikey") == 0)
{
if (apikey)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("conflicting or redundant options")));
apikey = defGetString(def);
}
else if (strcmp(def->defname, "database") == 0)
{
if (database)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("conflicting or redundant options")));
database = defGetString(def);
}
else if (strcmp(def->defname, "table") == 0)
{
if (table)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("conflicting or redundant options")));
table = defGetString(def);
}
else if (strcmp(def->defname, "query") == 0)
{
if (query)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("conflicting or redundant options")));
query = defGetString(def);
}
else if (strcmp(def->defname, "query_download_dir") == 0)
{
if (query_download_dir)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("conflicting or redundant options")));
query_download_dir = defGetString(def);
}
else if (strcmp(def->defname, "import_file_size") == 0)
{
const char *nptr = defGetString(def);
char *end_ptr;
long size;
if (import_file_size)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("conflicting or redundant options")));
size = strtoul(nptr, &end_ptr, 10);
if (nptr == end_ptr || size < 0)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("import_file_size must be unsigned integer")));
import_file_size = (char *) nptr;
}
else if (strcmp(def->defname, "atomic_import") == 0)
{
if (atomic_import)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("conflicting or redundant options")));
atomic_import = defGetString(def);
if (my_strcasecmp(atomic_import, "true") != 0 &&
my_strcasecmp(atomic_import, "false") != 0)
{
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("'atomic_import' should be boolean")));
}
}
}
if (catalog == ForeignTableRelationId)
{
if (query_engine == NULL)
ereport(ERROR,
(errcode(ERRCODE_FDW_DYNAMIC_PARAMETER_VALUE_NEEDED),
errmsg("query_engine is required for treasuredata_fdw foreign tables")));
if (apikey == NULL)
ereport(ERROR,
(errcode(ERRCODE_FDW_DYNAMIC_PARAMETER_VALUE_NEEDED),
errmsg("apikey is required for treasuredata_fdw foreign tables")));
if (database == NULL)
ereport(ERROR,
(errcode(ERRCODE_FDW_DYNAMIC_PARAMETER_VALUE_NEEDED),
errmsg("database is required for treasuredata_fdw foreign tables")));
if (table == NULL && query == NULL)
ereport(ERROR,
(errcode(ERRCODE_FDW_DYNAMIC_PARAMETER_VALUE_NEEDED),
errmsg("table or query is required for treasuredata_fdw foreign tables")));
if (table != NULL && query != NULL)
ereport(ERROR,
(errcode(ERRCODE_FDW_DYNAMIC_PARAMETER_VALUE_NEEDED),
errmsg("both table and query can't be specified with treasuredata_fdw foreign tables")));
}
PG_RETURN_VOID();
}
void
ExtractFdwOptions(ForeignTable *table, TdFdwOption *fdw_option)
{
ForeignServer *server;
ForeignDataWrapper *wrapper;
List *options;
ListCell *cell;
server = GetForeignServer(table->serverid);
wrapper = GetForeignDataWrapper(server->fdwid);
options = NIL;
options = list_concat(options, wrapper->options);
options = list_concat(options, server->options);
options = list_concat(options, table->options);
fdw_option->endpoint = NULL;
fdw_option->query_engine = NULL;
fdw_option->apikey = NULL;
fdw_option->database = NULL;
fdw_option->table = NULL;
fdw_option->query = NULL;
fdw_option->query_download_dir = NULL;
fdw_option->import_file_size = 128 * 1024 * 1024;
fdw_option->atomic_import = false;
foreach(cell, options)
{
DefElem *def = (DefElem *) lfirst(cell);
if (strcmp(def->defname, "endpoint") == 0)
{
fdw_option->endpoint = defGetString(def);
}
else if (strcmp(def->defname, "query_engine") == 0)
{
fdw_option->query_engine = defGetString(def);
}
else if (strcmp(def->defname, "apikey") == 0)
{
fdw_option->apikey = defGetString(def);
}
else if (strcmp(def->defname, "database") == 0)
{
fdw_option->database = defGetString(def);
}
else if (strcmp(def->defname, "table") == 0)
{
fdw_option->table = defGetString(def);
}
else if (strcmp(def->defname, "query") == 0)
{
fdw_option->query = defGetString(def);
}
else if (strcmp(def->defname, "query_download_dir") == 0)
{
fdw_option->query_download_dir = defGetString(def);
}
else if (strcmp(def->defname, "import_file_size") == 0)
{
const char *nptr = defGetString(def);
char *end_ptr;
long size = strtoul(nptr, &end_ptr, 10);
/* Maybe we don't need to check here since the value is already validated, but just in case... */
if (nptr != end_ptr && size >= 0)
{
fdw_option->import_file_size = size;
}
}
else if (strcmp(def->defname, "atomic_import") == 0)
{
if (my_strcasecmp(defGetString(def), "true") == 0)
{
fdw_option->atomic_import = true;
}
else if (my_strcasecmp(defGetString(def), "false") == 0)
{
fdw_option->atomic_import = false;
}
else
{
elog(ERROR, "treasuredata_fdw: atomic_import should be boolean");
}
}
}
/*
* Check required option(s) here.
*/
if (fdw_option->query_engine == NULL)
{
elog(ERROR, "treasuredata_fdw: query_engine is required for treasuredata_fdw foreign tables");
}
if (fdw_option->apikey == NULL)
{
elog(ERROR, "treasuredata_fdw: apikey is required for treasuredata_fdw foreign tables");
}
if (fdw_option->database == NULL)
{
elog(ERROR, "treasuredata_fdw: database is required for treasuredata_fdw foreign tables");
}
if (fdw_option->table == NULL && fdw_option->query == NULL)
{
elog(ERROR, "treasuredata_fdw: table or query is required for treasuredata_fdw foreign tables");
}
if (fdw_option->table != NULL && fdw_option->query != NULL)
{
elog(ERROR, "treasuredata_fdw: both table and query can't be specified with treasuredata_fdw foreign tables");
}
elog(DEBUG1, "treasuredata_fdw: endpoint=%s, query_engine=%s, apikey.len=%ld, database=%s, table=%s, query=%s, query_download_dir=%s, import_file_size=%ld, atomic_import=%d",
fdw_option->endpoint,
fdw_option->query_engine,
strlen(fdw_option->apikey),
fdw_option->database,
fdw_option->table,
fdw_option->query,
fdw_option->query_download_dir,
fdw_option->import_file_size,
fdw_option->atomic_import);
}