From: Shigeru Hanada Date: Thu, 24 Feb 2011 11:32:55 +0000 (+0900) Subject: Use is_conninfo_option() to determine which option should be used to X-Git-Url: http://git.postgresql.org/gitweb/static/gitweb.js?a=commitdiff_plain;h=732a622acaf6e4c873e9cebc51ffd473a2167d2d;p=users%2Fhanada%2Fpostgres.git Use is_conninfo_option() to determine which option should be used to connect remote PostgreSQL server. --- diff --git a/contrib/postgresql_fdw/postgresql_fdw.c b/contrib/postgresql_fdw/postgresql_fdw.c index fd8a4d5ed3..89b5790123 100644 --- a/contrib/postgresql_fdw/postgresql_fdw.c +++ b/contrib/postgresql_fdw/postgresql_fdw.c @@ -918,25 +918,13 @@ connect_pg_server(ForeignServer *server, UserMapping *user) for (i = 0, j = 0; all_keywords[i]; i++) { - StringInfoData conninfo; - PQconninfoOption *opt; - - initStringInfo(&conninfo); - appendStringInfo(&conninfo, "%s=%s", all_keywords[i], all_values[i]); - opt = PQconninfoParse(conninfo.data, NULL); - /* Use only valid libpq connection options. */ - if (opt == NULL) - { - PQconninfoFree(opt); + if (!is_conninfo_option(all_keywords[i])) continue; - } + keywords[j] = all_keywords[i]; values[j] = all_values[i]; j++; - - PQconninfoFree(opt); - pfree(conninfo.data); } keywords[j] = values[j] = NULL; pfree(all_keywords); diff --git a/src/backend/foreign/foreign.c b/src/backend/foreign/foreign.c index 37bbceee25..a675155d6a 100644 --- a/src/backend/foreign/foreign.c +++ b/src/backend/foreign/foreign.c @@ -444,10 +444,11 @@ pg_options_to_table(PG_FUNCTION_ARGS) /* * Describes the valid options for postgresql FDW, server, and user mapping. */ -struct ConnectionOption +struct FdwOption { - const char *optname; + const char *optname; /* name of the option */ Oid optcontext; /* Oid of catalog in which option may appear */ + bool is_conninfo; /* T if the option is a libpq conninfo option */ }; /* @@ -455,22 +456,26 @@ struct ConnectionOption * * The list is small - don't bother with bsearch if it stays so. */ -static struct ConnectionOption libpq_conninfo_options[] = { - {"authtype", ForeignServerRelationId}, - {"service", ForeignServerRelationId}, - {"user", UserMappingRelationId}, - {"password", UserMappingRelationId}, - {"connect_timeout", ForeignServerRelationId}, - {"dbname", ForeignServerRelationId}, - {"host", ForeignServerRelationId}, - {"hostaddr", ForeignServerRelationId}, - {"port", ForeignServerRelationId}, - {"tty", ForeignServerRelationId}, - {"options", ForeignServerRelationId}, - {"requiressl", ForeignServerRelationId}, - {"sslmode", ForeignServerRelationId}, - {"gsslib", ForeignServerRelationId}, - {NULL, InvalidOid} +static struct FdwOption postgresql_fdw_options[] = { + /* libpq connection options */ + {"authtype", ForeignServerRelationId, true}, + {"service", ForeignServerRelationId, true}, + {"user", UserMappingRelationId, true}, + {"password", UserMappingRelationId, true}, + {"connect_timeout", ForeignServerRelationId, true}, + {"dbname", ForeignServerRelationId, true}, + {"host", ForeignServerRelationId, true}, + {"hostaddr", ForeignServerRelationId, true}, + {"port", ForeignServerRelationId, true}, + {"tty", ForeignServerRelationId, true}, + {"options", ForeignServerRelationId, true}, + {"requiressl", ForeignServerRelationId, true}, + {"sslmode", ForeignServerRelationId, true}, + {"gsslib", ForeignServerRelationId, true}, + /* catalog options */ + {"nspname", ForeignTableRelationId, false}, + {"relname", ForeignTableRelationId, false}, + {NULL, InvalidOid, false} }; @@ -479,41 +484,28 @@ static struct ConnectionOption libpq_conninfo_options[] = { * context is the Oid of the catalog the option came from, or 0 if we * don't care. */ -static bool -is_conninfo_option(const char *option, Oid context) +bool +is_conninfo_option(const char *option) { - struct ConnectionOption *opt; + struct FdwOption *opt; - for (opt = libpq_conninfo_options; opt->optname; opt++) - if (context == opt->optcontext && strcmp(opt->optname, option) == 0) + for (opt = postgresql_fdw_options; opt->optname; opt++) + if (strcmp(opt->optname, option) == 0) return true; return false; } - -/* - * Valid options for postgresql_fdw objects. - * - * The list is small - don't bother with bsearch if it stays so. - */ -static struct ConnectionOption fdw_options[] = { - {"relname", ForeignTableRelationId}, - {"nspname", ForeignTableRelationId}, - {NULL, InvalidOid} -}; - - /* - * Check if the provided option is one of fdw options. + * Check if the provided option is one of postgresql_fdw options. * context is the Oid of the catalog the option came from, or 0 if we * don't care. */ static bool -is_fdw_option(const char *option, Oid context) +is_postgresql_fdw_option(const char *option, Oid context) { - struct ConnectionOption *opt; + struct FdwOption *opt; - for (opt = fdw_options; opt->optname; opt++) + for (opt = postgresql_fdw_options; opt->optname; opt++) if (context == opt->optcontext && strcmp(opt->optname, option) == 0) return true; return false; @@ -540,10 +532,9 @@ postgresql_fdw_validator(PG_FUNCTION_ARGS) { DefElem *def = lfirst(cell); - if (!is_conninfo_option(def->defname, catalog) && - !is_fdw_option(def->defname, catalog)) + if (!is_postgresql_fdw_option(def->defname, catalog)) { - struct ConnectionOption *opt; + struct FdwOption *opt; StringInfoData buf; /* @@ -551,7 +542,7 @@ postgresql_fdw_validator(PG_FUNCTION_ARGS) * with list of valid options for the object. */ initStringInfo(&buf); - for (opt = libpq_conninfo_options; opt->optname; opt++) + for (opt = postgresql_fdw_options; opt->optname; opt++) if (catalog == opt->optcontext) appendStringInfo(&buf, "%s%s", (buf.len > 0) ? ", " : "", opt->optname); diff --git a/src/include/foreign/foreign.h b/src/include/foreign/foreign.h index d676f3fce7..551bff6292 100644 --- a/src/include/foreign/foreign.h +++ b/src/include/foreign/foreign.h @@ -77,5 +77,6 @@ extern ForeignDataWrapper *GetForeignDataWrapperByName(const char *name, bool missing_ok); extern Oid GetForeignDataWrapperOidByName(const char *name, bool missing_ok); extern ForeignTable *GetForeignTable(Oid relid); +bool is_conninfo_option(const char *option); #endif /* FOREIGN_H */