Skip to content

Commit 539bc9f

Browse files
committed
Add code to pg_dump to use E'' strings when backslashes are used in dump
files.
1 parent 975368e commit 539bc9f

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

src/bin/pg_dump/dumputils.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.c,v 1.17 2005/04/30 08:08:51 neilc Exp $
10+
* $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.c,v 1.18 2005/07/01 21:03:25 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -111,6 +111,27 @@ fmtId(const char *rawid)
111111
void
112112
appendStringLiteral(PQExpBuffer buf, const char *str, bool escapeAll)
113113
{
114+
bool has_escapes = false;
115+
const char *str2 = str;
116+
117+
while (*str2)
118+
{
119+
char ch = *str2++;
120+
121+
if (ch == '\\' ||
122+
((unsigned char) ch < (unsigned char) ' ' &&
123+
(escapeAll ||
124+
(ch != '\t' && ch != '\n' && ch != '\v' &&
125+
ch != '\f' && ch != '\r'))))
126+
{
127+
has_escapes = true;
128+
break;
129+
}
130+
}
131+
132+
if (has_escapes)
133+
appendPQExpBufferChar(buf, 'E');
134+
114135
appendPQExpBufferChar(buf, '\'');
115136
while (*str)
116137
{
@@ -122,9 +143,9 @@ appendStringLiteral(PQExpBuffer buf, const char *str, bool escapeAll)
122143
appendPQExpBufferChar(buf, ch);
123144
}
124145
else if ((unsigned char) ch < (unsigned char) ' ' &&
125-
(escapeAll
126-
|| (ch != '\t' && ch != '\n' && ch != '\v' && ch != '\f' && ch != '\r')
127-
))
146+
(escapeAll ||
147+
(ch != '\t' && ch != '\n' && ch != '\v' &&
148+
ch != '\f' && ch != '\r')))
128149
{
129150
/*
130151
* generate octal escape for control chars other than

src/bin/pg_dump/pg_backup_db.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Implements the basic DB functions used by the archiver.
66
*
77
* IDENTIFICATION
8-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.62 2005/06/21 20:45:44 tgl Exp $
8+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.63 2005/07/01 21:03:25 momjian Exp $
99
*
1010
*-------------------------------------------------------------------------
1111
*/
@@ -597,7 +597,6 @@ _sendSQLLine(ArchiveHandle *AH, char *qry, char *eos)
597597
}
598598
else
599599
{
600-
601600
if (qry[pos] == '\\')
602601
{
603602
if (AH->sqlparse.lastChar == '\\')

src/bin/pg_dump/pg_dump.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* by PostgreSQL
1313
*
1414
* IDENTIFICATION
15-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.411 2005/06/30 03:02:56 tgl Exp $
15+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.412 2005/07/01 21:03:25 momjian Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -7767,8 +7767,9 @@ dumpTrigger(Archive *fout, TriggerInfo *tginfo)
77677767
p = tginfo->tgargs;
77687768
for (findx = 0; findx < tginfo->tgnargs; findx++)
77697769
{
7770-
const char *s = p;
7770+
const char *s = p, *s2 = p;
77717771

7772+
/* Set 'p' to end of arg string. marked by '\000' */
77727773
for (;;)
77737774
{
77747775
p = strchr(p, '\\');
@@ -7781,20 +7782,29 @@ dumpTrigger(Archive *fout, TriggerInfo *tginfo)
77817782
exit_nicely();
77827783
}
77837784
p++;
7784-
if (*p == '\\')
7785+
if (*p == '\\') /* is it '\\'? */
77857786
{
77867787
p++;
77877788
continue;
77887789
}
7789-
if (p[0] == '0' && p[1] == '0' && p[2] == '0')
7790+
if (p[0] == '0' && p[1] == '0' && p[2] == '0') /* is it '\000'? */
77907791
break;
77917792
}
77927793
p--;
7794+
7795+
/* do we need E''? */
7796+
while (s2 < p)
7797+
if (*s2++ == '\\')
7798+
{
7799+
appendPQExpBufferChar(query, 'E');
7800+
break;
7801+
}
7802+
77937803
appendPQExpBufferChar(query, '\'');
77947804
while (s < p)
77957805
{
77967806
if (*s == '\'')
7797-
appendPQExpBufferChar(query, '\\');
7807+
appendPQExpBufferChar(query, '\'');
77987808
appendPQExpBufferChar(query, *s++);
77997809
}
78007810
appendPQExpBufferChar(query, '\'');

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy