proper quoting for function and result column names
authorMarko Kreen <markokr@gmail.com>
Thu, 8 Nov 2007 07:59:21 +0000 (07:59 +0000)
committerMarko Kreen <markokr@gmail.com>
Thu, 8 Nov 2007 07:59:21 +0000 (07:59 +0000)
src/function.c
src/plproxy.h
src/result.c
src/type.c

index 024abc169382c90739664d0865a3fe5d6837c5b2..8a4ea3cec53f86a097388712492d65a24d7280fd 100644 (file)
@@ -188,7 +188,8 @@ fn_delete(ProxyFunction *func, bool in_cache)
 static void
 fn_set_name(ProxyFunction *func, HeapTuple proc_tuple)
 {
-       char            namebuf[NAMEDATALEN * 2 + 3];
+       /* 2 names, size can double, "" + . + "" + NUL */
+       char            namebuf[NAMEDATALEN * 4 + 2 + 1 + 2 + 1];
        Form_pg_proc proc_struct;
        Form_pg_namespace ns_struct;
        HeapTuple       ns_tup;
@@ -203,8 +204,9 @@ fn_set_name(ProxyFunction *func, HeapTuple proc_tuple)
                plproxy_error(func, "Cannot find namespace %u", nsoid);
        ns_struct = (Form_pg_namespace) GETSTRUCT(ns_tup);
 
-       sprintf(namebuf, "%s.%s", NameStr(ns_struct->nspname),
-                       NameStr(proc_struct->proname));
+       snprintf(namebuf, sizeof(namebuf), "%s.%s",
+                       quote_identifier(NameStr(ns_struct->nspname)),
+                       quote_identifier(NameStr(proc_struct->proname)));
        func->name = plproxy_func_strdup(func, namebuf);
 
        ReleaseSysCache(ns_tup);
index 9d5ca1fe11e3ac135246686bcb5e67e10ee1725f..4e5d51ef76bccc1a9a4bb5a7afe0d58353c0df07 100644 (file)
@@ -187,7 +187,7 @@ typedef struct ProxyComposite
 {
        TupleDesc       tupdesc;                /* Return tuple descriptor */
        ProxyType **type_list;          /* Column type info */
-       char      **name_list;          /* Column names */
+       char      **name_list;          /* Quoted column names */
        unsigned        use_binary:1;   /* True if all columns support binary recv */
 } ProxyComposite;
 
@@ -213,7 +213,7 @@ typedef struct ProxyQuery
  */
 typedef struct ProxyFunction
 {
-       const char *name;                       /* Fully-quelified function name */
+       const char *name;                       /* Fully-qualified and quoted function name */
        Oid                     oid;                    /* Function OID */
        MemoryContext ctx;                      /* Where runtime allocations should happen */
 
index a3cfee16b6ccdaeff0e0e45c159f346c81c0996f..38ef3eb73e0add50f8f2289f3a8ceb79164cb62b 100644 (file)
@@ -33,7 +33,7 @@ name_matches(ProxyFunction *func, const char *aname, PGresult *res, int col)
 
        if (fname == NULL)
                plproxy_error(func, "Unnamed result column %d", col + 1);
-       if (strcasecmp(aname, fname) == 0)
+       if (strcmp(aname, fname) == 0)
                return true;
        return false;
 }
@@ -65,6 +65,7 @@ map_results(ProxyFunction *func, PGresult *res)
 
        for (i = 0; i < natts; i++)
        {
+               /* ->name_list has quoted names, take unquoted from ->tupdesc */
                a = func->ret_composite->tupdesc->attrs[i];
                aname = NameStr(a->attname);
 
index bb401f91c4bc845c1de877cbaab509d18d16d788..a1122476e149824771ed5ae149d40ce18652f33e 100644 (file)
@@ -86,7 +86,7 @@ plproxy_composite_info(ProxyFunction *func, TupleDesc tupdesc)
                if (a->attisdropped)
                        plproxy_error(func, "dropped attrs not supported");
 
-               name = NameStr(a->attname);
+               name = quote_identifier(NameStr(a->attname));
                ret->name_list[i] = plproxy_func_strdup(func, name);
 
                type = plproxy_find_type_info(func, a->atttypid, 0);
@@ -157,7 +157,7 @@ plproxy_find_type_info(ProxyFunction *func, Oid oid, bool for_send)
                                t_nsp;
        Form_pg_type s_type;
        Form_pg_namespace s_nsp;
-       char            namebuf[NAMEDATALEN * 2 + 3];
+       char            namebuf[NAMEDATALEN * 4 + 2 + 1 + 2 + 1];
        Oid                     nsoid;
 
        /* fetch pg_type row */
@@ -175,12 +175,14 @@ plproxy_find_type_info(ProxyFunction *func, Oid oid, bool for_send)
                if (!HeapTupleIsValid(t_nsp))
                        plproxy_error(func, "cache lookup failed for namespace %u", nsoid);
                s_nsp = (Form_pg_namespace) GETSTRUCT(t_nsp);
-               sprintf(namebuf, "%s.%s", NameStr(s_nsp->nspname), NameStr(s_type->typname));
+               snprintf(namebuf, sizeof(namebuf), "%s.%s",
+                               quote_identifier(NameStr(s_nsp->nspname)),
+                               quote_identifier(NameStr(s_type->typname)));
                ReleaseSysCache(t_nsp);
        }
        else
        {
-               sprintf(namebuf, "%s", NameStr(s_type->typname));
+               snprintf(namebuf, sizeof(namebuf), "%s", quote_identifier(NameStr(s_type->typname)));
        }
 
        /* sanity check */