Add code to pg_dump to use E'' strings when backslashes are used in dump
authorBruce Momjian
Fri, 1 Jul 2005 21:03:25 +0000 (21:03 +0000)
committerBruce Momjian
Fri, 1 Jul 2005 21:03:25 +0000 (21:03 +0000)
files.

src/bin/pg_dump/dumputils.c
src/bin/pg_dump/pg_backup_db.c
src/bin/pg_dump/pg_dump.c

index f3cece132d1b86e1e9f95edfba455fc1ebac4c03..b1622d55aec20c40c50f4a76d5769a60bb7aae5c 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.c,v 1.17 2005/04/30 08:08:51 neilc Exp $
+ * $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.c,v 1.18 2005/07/01 21:03:25 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -111,6 +111,27 @@ fmtId(const char *rawid)
 void
 appendStringLiteral(PQExpBuffer buf, const char *str, bool escapeAll)
 {
+   bool has_escapes = false;
+   const char *str2 = str;
+
+   while (*str2)
+   {
+       char        ch = *str2++;
+
+       if (ch == '\\' ||
+           ((unsigned char) ch < (unsigned char) ' ' &&
+            (escapeAll ||
+             (ch != '\t' && ch != '\n' && ch != '\v' &&
+              ch != '\f' && ch != '\r'))))
+       {
+           has_escapes = true;
+           break;
+       }
+   }
+   
+   if (has_escapes)
+       appendPQExpBufferChar(buf, 'E');
+   
    appendPQExpBufferChar(buf, '\'');
    while (*str)
    {
@@ -122,9 +143,9 @@ appendStringLiteral(PQExpBuffer buf, const char *str, bool escapeAll)
            appendPQExpBufferChar(buf, ch);
        }
        else if ((unsigned char) ch < (unsigned char) ' ' &&
-                (escapeAll
-                 || (ch != '\t' && ch != '\n' && ch != '\v' && ch != '\f' && ch != '\r')
-                 ))
+                (escapeAll ||
+                 (ch != '\t' && ch != '\n' && ch != '\v' &&
+                  ch != '\f' && ch != '\r')))
        {
            /*
             * generate octal escape for control chars other than
index af50cb9e9da2efc3201469acec1f1a8bfefffe34..047a1aa9182c6d7adaa4303191f4518294149723 100644 (file)
@@ -5,7 +5,7 @@
  * Implements the basic DB functions used by the archiver.
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.62 2005/06/21 20:45:44 tgl Exp $
+ *   $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.63 2005/07/01 21:03:25 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -597,7 +597,6 @@ _sendSQLLine(ArchiveHandle *AH, char *qry, char *eos)
                    }
                    else
                    {
-
                        if (qry[pos] == '\\')
                        {
                            if (AH->sqlparse.lastChar == '\\')
index d40b01a266d7077a661fd55396f2658dd3ec479c..7708083f3507eeb14bb5b1dce6c11377012e1923 100644 (file)
@@ -12,7 +12,7 @@
  * by PostgreSQL
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.411 2005/06/30 03:02:56 tgl Exp $
+ *   $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.412 2005/07/01 21:03:25 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -7767,8 +7767,9 @@ dumpTrigger(Archive *fout, TriggerInfo *tginfo)
    p = tginfo->tgargs;
    for (findx = 0; findx < tginfo->tgnargs; findx++)
    {
-       const char *s = p;
+       const char *s = p, *s2 = p;
 
+       /* Set 'p' to end of arg string. marked by '\000' */
        for (;;)
        {
            p = strchr(p, '\\');
@@ -7781,20 +7782,29 @@ dumpTrigger(Archive *fout, TriggerInfo *tginfo)
                exit_nicely();
            }
            p++;
-           if (*p == '\\')
+           if (*p == '\\')     /* is it '\\'? */
            {
                p++;
                continue;
            }
-           if (p[0] == '0' && p[1] == '0' && p[2] == '0')
+           if (p[0] == '0' && p[1] == '0' && p[2] == '0')  /* is it '\000'? */
                break;
        }
        p--;
+
+       /* do we need E''? */
+       while (s2 < p)
+           if (*s2++ == '\\')
+           {
+               appendPQExpBufferChar(query, 'E');
+               break;
+           }
+
        appendPQExpBufferChar(query, '\'');
        while (s < p)
        {
            if (*s == '\'')
-               appendPQExpBufferChar(query, '\\');
+               appendPQExpBufferChar(query, '\'');
            appendPQExpBufferChar(query, *s++);
        }
        appendPQExpBufferChar(query, '\'');