Backported bug fix for #2956.
authorMichael Meskes <[email protected]>
Tue, 27 Feb 2007 13:26:59 +0000 (13:26 +0000)
committerMichael Meskes <[email protected]>
Tue, 27 Feb 2007 13:26:59 +0000 (13:26 +0000)
src/interfaces/ecpg/ecpglib/execute.c

index 4f70f7eda034a10dd41f00f2ae451d79481c7b82..a08afafadd770daaf94c15a2003142ca57466566 100644 (file)
@@ -1,4 +1,4 @@
-/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.38.4.11 2007/02/06 09:42:08 meskes Exp $ */
+/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.38.4.12 2007/02/27 13:26:59 meskes Exp $ */
 
 /*
  * The aim is to get a simpler inteface to the database routines.
 static char *
 quote_postgres(char *arg, int lineno)
 {
-   char       *res = (char *) ECPGalloc(2 * strlen(arg) + 3, lineno);
-   int         i,
-               ri = 0;
+   char    *res;
+   int error;
+   size_t  length;
+   size_t  escaped_len;
+   size_t  buffer_len;
 
+   /*
+    * if quote is false we just need to store things in a descriptor they
+    * will be quoted once they are inserted in a statement
+    */
+   length = strlen(arg);
+   buffer_len = 2 * length + 1;
+   res = (char *) ECPGalloc(buffer_len + 2, lineno);
    if (!res)
        return (res);
 
-   res[ri++] = '\'';
-
-   for (i = 0; arg[i]; i++, ri++)
+   error = 0;
+   escaped_len = PQescapeString(res+1, arg, buffer_len);
+   if (error)
    {
-       switch (arg[i])
-       {
-           case '\'':
-               res[ri++] = '\'';
-               break;
-           case '\\':
-               res[ri++] = '\\';
-               break;
-           default:
-               ;
-       }
-       res[ri] = arg[i];
+       ECPGfree(res);
+       return NULL;
    }
 
-   res[ri++] = '\'';
-   res[ri] = '\0';
+   res[0] = res[escaped_len+1] = '\'';
+   res[escaped_len+2] = '\0';
 
    ECPGfree(arg);
    return res;