forked from EnterpriseDB/mongo_fdw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.c
271 lines (237 loc) · 8.52 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
/*-------------------------------------------------------------------------
*
* option.c
* Foreign-data wrapper for remote MongoDB servers
*
* Portions Copyright (c) 2012-2014, PostgreSQL Global Development Group
*
* Portions Copyright (c) 2004-2014, EnterpriseDB Corporation.
*
* Portions Copyright (c) 2012–2014 Citus Data, Inc.
*
* IDENTIFICATION
* option.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "mongo_wrapper.h"
#include "mongo_fdw.h"
#include "access/reloptions.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
#include "commands/explain.h"
#include "commands/vacuum.h"
#include "foreign/fdwapi.h"
#include "foreign/foreign.h"
#include "nodes/makefuncs.h"
#include "optimizer/cost.h"
#include "optimizer/pathnode.h"
#include "optimizer/plancat.h"
#include "optimizer/planmain.h"
#include "optimizer/restrictinfo.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/hsearch.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/memutils.h"
#include "miscadmin.h"
static char * mongo_get_option_value(Oid foreignTableId, const char *optionName);
/*
* 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 = NULL;
foreach(optionCell, optionList)
{
DefElem *optionDef = (DefElem *) lfirst(optionCell);
char *optionName = optionDef->defname;
bool optionValid = false;
int32 optionIndex = 0;
for (optionIndex = 0; optionIndex < ValidOptionCount; optionIndex++)
{
const MongoValidOption *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)
{
StringInfo 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)));
}
/* if port option is given, error out if its value isn't an integer */
if (strncmp(optionName, OPTION_NAME_PORT, NAMEDATALEN) == 0)
{
char *optionValue = defGetString(optionDef);
int32 portNumber = pg_atoi(optionValue, sizeof(int32), 0);
(void) portNumber;
}
}
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 = 0;
for (optionIndex = 0; optionIndex < ValidOptionCount; optionIndex++)
{
const MongoValidOption *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)
{
MongoFdwOptions *options = NULL;
char *addressName = NULL;
char *portName = NULL;
int32 portNumber = 0;
char *svr_database = NULL;
char *collectionName = NULL;
char *svr_username= NULL;
char *svr_password= NULL;
#ifdef META_DRIVER
char *readPreference = NULL;
char *authenticationDatabase = NULL;
char *replicaSet = NULL;
bool ssl = false;
char *pem_file = NULL;
char *pem_pwd = NULL;
char *ca_file = NULL;
char *ca_dir = NULL;
char *crl_file = NULL;
bool weak_cert_validation = false;
readPreference = mongo_get_option_value(foreignTableId, OPTION_NAME_READ_PREFERENCE);
authenticationDatabase = mongo_get_option_value(foreignTableId, OPTION_NAME_AUTHENTICATION_DATABASE);
replicaSet = mongo_get_option_value(foreignTableId, OPTION_NAME_REPLICA_SET);
ssl = mongo_get_option_value(foreignTableId, OPTION_NAME_SSL);
pem_file = mongo_get_option_value(foreignTableId, OPTION_NAME_PEM_FILE);
pem_pwd = mongo_get_option_value(foreignTableId, OPTION_NAME_PEM_PWD);
ca_file = mongo_get_option_value(foreignTableId, OPTION_NAME_CA_FILE);
ca_dir = mongo_get_option_value(foreignTableId, OPTION_NAME_CA_DIR);
crl_file = mongo_get_option_value(foreignTableId, OPTION_NAME_CRL_FILE);
weak_cert_validation = mongo_get_option_value(foreignTableId, OPTION_NAME_WEAK_CERT);
#endif
addressName = mongo_get_option_value(foreignTableId, OPTION_NAME_ADDRESS);
if (addressName == NULL)
addressName = pstrdup(DEFAULT_IP_ADDRESS);
portName = mongo_get_option_value(foreignTableId, OPTION_NAME_PORT);
if (portName == NULL)
portNumber = DEFAULT_PORT_NUMBER;
else
portNumber = pg_atoi(portName, sizeof(int32), 0);
svr_database = mongo_get_option_value(foreignTableId, OPTION_NAME_DATABASE);
if (svr_database == NULL)
svr_database = pstrdup(DEFAULT_DATABASE_NAME);
collectionName = mongo_get_option_value(foreignTableId, OPTION_NAME_COLLECTION);
if (collectionName == NULL)
collectionName = get_rel_name(foreignTableId);
svr_username = mongo_get_option_value(foreignTableId, OPTION_NAME_USERNAME);
svr_password = mongo_get_option_value(foreignTableId, OPTION_NAME_PASSWORD);
options = (MongoFdwOptions *) palloc0(sizeof(MongoFdwOptions));
options->svr_address = addressName;
options->svr_port = portNumber;
options->svr_database = svr_database;
options->collectionName = collectionName;
options->svr_username = svr_username;
options->svr_password = svr_password;
#ifdef META_DRIVER
options->readPreference = readPreference;
options->authenticationDatabase = authenticationDatabase;
options->replicaSet = replicaSet;
options->ssl = ssl;
options->pem_file = pem_file;
options->pem_pwd = pem_pwd;
options->ca_file = ca_file;
options->ca_dir = ca_dir;
options->crl_file = crl_file;
options->weak_cert_validation = weak_cert_validation;
#endif
return options;
}
void
mongo_free_options(MongoFdwOptions *options)
{
if (options)
{
pfree(options->svr_address);
pfree(options->svr_database);
pfree(options);
}
}
/*
* mongo_get_option_value walks over foreign table and foreign server options, and
* looks for the option with the given name. If found, the function returns the
* option's value.
*/
static char *
mongo_get_option_value(Oid foreignTableId, const char *optionName)
{
ForeignTable *foreignTable = NULL;
ForeignServer *foreignServer = NULL;
List *optionList = NIL;
ListCell *optionCell = NULL;
UserMapping *mapping= NULL;
char *optionValue = NULL;
foreignTable = GetForeignTable(foreignTableId);
foreignServer = GetForeignServer(foreignTable->serverid);
mapping = GetUserMapping(GetUserId(), foreignTable->serverid);
optionList = list_concat(optionList, foreignTable->options);
optionList = list_concat(optionList, foreignServer->options);
optionList = list_concat(optionList, mapping->options);
foreach(optionCell, optionList)
{
DefElem *optionDef = (DefElem *) lfirst(optionCell);
char *optionDefName = optionDef->defname;
if (strncmp(optionDefName, optionName, NAMEDATALEN) == 0)
{
optionValue = defGetString(optionDef);
break;
}
}
return optionValue;
}