forked from EnterpriseDB/mongo_fdw
-
Notifications
You must be signed in to change notification settings - Fork 4
/
option.c
300 lines (247 loc) · 8.65 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
/*-------------------------------------------------------------------------
*
* option.c
* FDW option handling for mongo_fdw
*
* Portions Copyright (c) 2012-2014, PostgreSQL Global Development Group
* Portions Copyright (c) 2004-2022, EnterpriseDB Corporation.
* Portions Copyright (c) 2012–2014 Citus Data, Inc.
* Portions Copyright (c) 2021, TOSHIBA CORPORATION
*
* IDENTIFICATION
* option.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "miscadmin.h"
#if PG_VERSION_NUM >= 160000
#include "utils/varlena.h"
#endif
#include "mongo_wrapper.h"
/*
* Validate the generic options given to a FOREIGN DATA WRAPPER, SERVER,
* USER MAPPING or FOREIGN TABLE that uses postgres_fdw.
*
* Raise an ERROR if the option or its value is considered invalid.
*/
extern Datum mongo_fdw_validator(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(mongo_fdw_validator);
/*
* mongo_fdw_validator
* Validates options given to one of the following commands:
* foreign data wrapper, server, user mapping, or foreign table.
*
* This function errors out if the given option name or its value is considered
* invalid.
*/
Datum
mongo_fdw_validator(PG_FUNCTION_ARGS)
{
Datum optionArray = PG_GETARG_DATUM(0);
Oid optionContextId = PG_GETARG_OID(1);
List *optionList = untransformRelOptions(optionArray);
ListCell *optionCell;
foreach(optionCell, optionList)
{
DefElem *optionDef = (DefElem *) lfirst(optionCell);
char *optionName = optionDef->defname;
bool optionValid = false;
int32 optionIndex;
for (optionIndex = 0; optionIndex < ValidOptionCount; optionIndex++)
{
const MongoValidOption *validOption;
validOption = &(ValidOptionArray[optionIndex]);
if ((optionContextId == validOption->optionContextId) &&
(strncmp(optionName, validOption->optionName, NAMEDATALEN) == 0))
{
optionValid = true;
break;
}
}
/* If invalid option, display an informative error message */
if (!optionValid)
{
#if PG_VERSION_NUM >= 160000
/*
* Unknown option specified, complain about it. Provide a hint
* with a valid option that looks similar, if there is one.
*/
const char *closest_match;
ClosestMatchState match_state;
bool has_valid_options = false;
initClosestMatch(&match_state, optionName, 4);
for (optionIndex = 0; optionIndex < ValidOptionCount; optionIndex++)
{
const MongoValidOption *validOption;
validOption = &(ValidOptionArray[optionIndex]);
if (optionContextId == validOption->optionContextId)
{
has_valid_options = true;
updateClosestMatch(&match_state, validOption->optionName);
}
}
closest_match = getClosestMatch(&match_state);
ereport(ERROR,
(errcode(ERRCODE_FDW_INVALID_OPTION_NAME),
errmsg("invalid option \"%s\"", optionName),
has_valid_options ? closest_match ?
errhint("Perhaps you meant the option \"%s\".",
closest_match) : 0 :
errhint("There are no valid options in this context.")));
#else
StringInfo optionNamesString;
optionNamesString = mongo_option_names_string(optionContextId);
ereport(ERROR,
(errcode(ERRCODE_FDW_INVALID_OPTION_NAME),
errmsg("invalid option \"%s\"", optionName),
errhint("Valid options in this context are: %s.",
optionNamesString->data)));
#endif
}
/* If port option is given, error out if its value isn't an integer */
if (strncmp(optionName, OPTION_NAME_PORT, NAMEDATALEN) == 0)
{
long port;
char *intString = defGetString(optionDef);
char *endp;
port = strtol(intString, &endp, 10);
if (port < 0 || port > USHRT_MAX)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("port value \"%s\" is out of range for type %s",
intString, "unsigned short")));
}
else if (strcmp(optionName, OPTION_NAME_USE_REMOTE_ESTIMATE) == 0
|| strcmp(optionName, OPTION_NAME_ENABLE_JOIN_PUSHDOWN) == 0
#ifdef META_DRIVER
|| strcmp(optionName, OPTION_NAME_WEAK_CERT) == 0 ||
strcmp(optionName, OPTION_NAME_SSL) == 0
#endif
)
{
/* These accept only boolean values */
(void) defGetBoolean(optionDef);
}
}
PG_RETURN_VOID();
}
/*
* mongo_option_names_string
* Finds all options that are valid for the current context, and
* concatenates these option names in a comma separated string.
*/
StringInfo
mongo_option_names_string(Oid currentContextId)
{
StringInfo optionNamesString = makeStringInfo();
bool firstOptionPrinted = false;
int32 optionIndex;
for (optionIndex = 0; optionIndex < ValidOptionCount; optionIndex++)
{
const MongoValidOption *validOption;
validOption = &(ValidOptionArray[optionIndex]);
/* If option belongs to current context, append option name */
if (currentContextId == validOption->optionContextId)
{
if (firstOptionPrinted)
appendStringInfoString(optionNamesString, ", ");
appendStringInfoString(optionNamesString, validOption->optionName);
firstOptionPrinted = true;
}
}
return optionNamesString;
}
/*
* mongo_get_options
* Returns the option values to be used when connecting to and querying
* MongoDB.
*
* To resolve these values, the function checks the foreign table's options,
* and if not present, falls back to default values.
*/
MongoFdwOptions *
mongo_get_options(Oid foreignTableId, Oid userid)
{
ForeignTable *foreignTable;
ForeignServer *foreignServer;
UserMapping *mapping;
List *optionList = NIL;
MongoFdwOptions *options;
ListCell *lc;
foreignTable = GetForeignTable(foreignTableId);
foreignServer = GetForeignServer(foreignTable->serverid);
mapping = GetUserMapping(userid, foreignTable->serverid);
optionList = mongo_list_concat(optionList, foreignServer->options);
optionList = mongo_list_concat(optionList, foreignTable->options);
optionList = mongo_list_concat(optionList, mapping->options);
options = (MongoFdwOptions *) palloc0(sizeof(MongoFdwOptions));
options->use_remote_estimate = false;
options->enable_join_pushdown = true;
#ifdef META_DRIVER
options->ssl = false;
options->weak_cert_validation = false;
#endif
/* Loop through the options */
foreach(lc, optionList)
{
DefElem *def = (DefElem *) lfirst(lc);
#ifdef META_DRIVER
if (strcmp(def->defname, OPTION_NAME_READ_PREFERENCE) == 0)
options->readPreference = defGetString(def);
else if (strcmp(def->defname, OPTION_NAME_AUTHENTICATION_DATABASE) == 0)
options->authenticationDatabase = defGetString(def);
else if (strcmp(def->defname, OPTION_NAME_REPLICA_SET) == 0)
options->replicaSet = defGetString(def);
else if (strcmp(def->defname, OPTION_NAME_SSL) == 0)
options->ssl = defGetBoolean(def);
else if (strcmp(def->defname, OPTION_NAME_PEM_FILE) == 0)
options->pem_file = defGetString(def);
else if (strcmp(def->defname, OPTION_NAME_PEM_PWD) == 0)
options->pem_pwd = defGetString(def);
else if (strcmp(def->defname, OPTION_NAME_CA_FILE) == 0)
options->ca_file = defGetString(def);
else if (strcmp(def->defname, OPTION_NAME_CA_DIR) == 0)
options->ca_dir = defGetString(def);
else if (strcmp(def->defname, OPTION_NAME_CRL_FILE) == 0)
options->crl_file = defGetString(def);
else if (strcmp(def->defname, OPTION_NAME_WEAK_CERT) == 0)
options->weak_cert_validation = defGetBoolean(def);
else /* This is for continuation */
#endif
if (strcmp(def->defname, OPTION_NAME_ADDRESS) == 0)
options->svr_address = defGetString(def);
else if (strcmp(def->defname, OPTION_NAME_PORT) == 0)
options->svr_port = atoi(defGetString(def));
else if (strcmp(def->defname, OPTION_NAME_DATABASE) == 0)
options->svr_database = defGetString(def);
else if (strcmp(def->defname, OPTION_NAME_COLLECTION) == 0)
options->collectionName = defGetString(def);
else if (strcmp(def->defname, OPTION_NAME_COLUMN_NAME) == 0)
options->column_name = defGetString(def);
else if (strcmp(def->defname, OPTION_NAME_USERNAME) == 0)
options->svr_username = defGetString(def);
else if (strcmp(def->defname, OPTION_NAME_PASSWORD) == 0)
options->svr_password = defGetString(def);
else if (strcmp(def->defname, OPTION_NAME_USE_REMOTE_ESTIMATE) == 0)
options->use_remote_estimate = defGetBoolean(def);
else if (strcmp(def->defname, OPTION_NAME_ENABLE_JOIN_PUSHDOWN) == 0)
options->enable_join_pushdown = defGetBoolean(def);
}
/* Default values, if required */
if (!options->svr_address)
options->svr_address = DEFAULT_IP_ADDRESS;
if (!options->svr_port)
options->svr_port = DEFAULT_PORT_NUMBER;
if (!options->svr_database)
options->svr_database = DEFAULT_DATABASE_NAME;
if (!options->collectionName)
options->collectionName= get_rel_name(foreignTableId);
return options;
}
void
mongo_free_options(MongoFdwOptions *options)
{
if (options)
pfree(options);
}