Use is_conninfo_option() to determine which option should be used to
authorShigeru Hanada <hanada@metrosystems.co.jp>
Thu, 24 Feb 2011 11:32:55 +0000 (20:32 +0900)
committerShigeru Hanada <hanada@metrosystems.co.jp>
Thu, 24 Feb 2011 11:32:55 +0000 (20:32 +0900)
connect remote PostgreSQL server.

contrib/postgresql_fdw/postgresql_fdw.c
src/backend/foreign/foreign.c
src/include/foreign/foreign.h

index fd8a4d5ed37389da50e59481e923ec24070921f5..89b579012348d6770d2ca0b4a260738a73153f4a 100644 (file)
@@ -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);
index 37bbceee25c75fa44d40b425181a6a16f2f013e7..a675155d6a75c50ff24f8634f14c243f2d527845 100644 (file)
@@ -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);
index d676f3fce7451cbf880eb0c1c063a9250f20cb68..551bff62921bfa7ca35fa25bdd09a103f8cb557a 100644 (file)
@@ -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 */