Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf862c42

Browse files
committed
Use dollar-quoting for function bodies, unless disabled with
--disable-dollar-quoting.Andrew Dunstan
1 parent48b2802 commitf862c42

File tree

4 files changed

+121
-28
lines changed

4 files changed

+121
-28
lines changed

‎doc/src/sgml/ref/pg_dump.sgml

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/pg_dump.sgml,v 1.68 2003/12/01 22:07:58 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/pg_dump.sgml,v 1.69 2004/03/23 22:06:08 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -420,36 +420,34 @@ PostgreSQL documentation
420420
</varlistentry>
421421

422422
<varlistentry>
423-
<term><option>-Xuse-set-session-authorization</></term>
424-
<term><option>--use-set-session-authorization</></term>
423+
<term><option>-Xdisable-dollar-quoting</></term>
424+
<term><option>--disable-dollar-quoting</></term>
425425
<listitem>
426426
<para>
427-
This option is obsolete but still accepted for backwards
428-
compatibility.
429-
<application>pg_dump</application> now always behaves in the
430-
way formerly selected by this option.
427+
This option disables the use of dollar quoting for function bodies,
428+
and forces them to be quoted using SQL standard string syntax.
431429
</para>
432-
</listitem>
433-
</varlistentry>
430+
</listitem>
431+
</varlistentry>
434432

435433
<varlistentry>
436434
<term><option>-X disable-triggers</></term>
437435
<term><option>--disable-triggers</></term>
438436
<listitem>
439437
<para>
440438
This option is only relevant when creating a data-only dump.
441-
It instructs <application>pg_dump</application> to include commands
442-
to temporarily disable triggers on the target tables while
443-
the data is reloaded. Use this if you have referential
444-
integrity checks or other triggers on the tables that you
445-
do not want to invoke during data reload.
439+
It instructs <application>pg_dump</application> to include commands
440+
to temporarily disable triggers on the target tables while
441+
the data is reloaded. Use this if you have referential
442+
integrity checks or other triggers on the tables that you
443+
do not want to invoke during data reload.
446444
</para>
447445

448446
<para>
449447
Presently, the commands emitted for <option>--disable-triggers</>
450-
must be done as superuser. So, you should also specify
451-
a superuser name with <option>-S</>, or preferably be careful to
452-
start the resulting script as a superuser.
448+
must be done as superuser. So, you should also specify
449+
a superuser name with <option>-S</>, or preferably be careful to
450+
start the resulting script as a superuser.
453451
</para>
454452

455453
<para>
@@ -460,14 +458,27 @@ PostgreSQL documentation
460458
</listitem>
461459
</varlistentry>
462460

461+
<varlistentry>
462+
<term><option>-X use-set-session-authorization</></term>
463+
<term><option>--use-set-session-authorization</></term>
464+
<listitem>
465+
<para>
466+
This option is obsolete but still accepted for backwards
467+
compatibility.
468+
<application>pg_dump</application> now always behaves in the
469+
way formerly selected by this option.
470+
</para>
471+
</listitem>
472+
</varlistentry>
473+
463474
<varlistentry>
464475
<term><option>-Z <replaceable class="parameter">0..9</replaceable></option></term>
465476
<term><option>--compress=<replaceable class="parameter">0..9</replaceable></option></term>
466477
<listitem>
467478
<para>
468-
Specify the compression level to use in archive formats that
469-
support compression. (Currently only the custom archive
470-
format supports compression.)
479+
Specify the compression level to use in archive formats that
480+
support compression. (Currently only the custom archive
481+
format supports compression.)
471482
</para>
472483
</listitem>
473484
</varlistentry>

‎src/bin/pg_dump/dumputils.c

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2003, 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.11 2004/01/07 00:44:21 tgl Exp $
10+
* $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.c,v 1.12 2004/03/23 22:06:08 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -142,6 +142,65 @@ appendStringLiteral(PQExpBuffer buf, const char *str, bool escapeAll)
142142
}
143143

144144

145+
/*
146+
* Convert a string value to a dollar quoted literal and append it to
147+
* the given buffer. If the dqprefix parameter is not NULL then the
148+
* dollar quote delimiter will begin with that (after the opening $).
149+
*
150+
* No escaping is done at all on str, in compliance with the rules
151+
* for parsing dollar quoted strings.
152+
*/
153+
void
154+
appendStringLiteralDQ(PQExpBufferbuf,constchar*str,constchar*dqprefix)
155+
{
156+
staticconstcharsuffixes[]="_XXXXXXX";
157+
intnextchar=0;
158+
PQExpBufferdelimBuf=createPQExpBuffer();
159+
160+
/* start with $ + dqprefix if not NULL */
161+
appendPQExpBufferChar(delimBuf,'$');
162+
if (dqprefix)
163+
appendPQExpBuffer(delimBuf,dqprefix);
164+
165+
/*
166+
* Make sure we choose a delimiter which (without the trailing $)
167+
* is not present in the string being quoted. We don't check with the
168+
* trailing $ because a string ending in $foo must not be quoted with
169+
* $foo$.
170+
*/
171+
while (strstr(str,delimBuf->data)!=NULL)
172+
{
173+
appendPQExpBufferChar(delimBuf,suffixes[nextchar++]);
174+
nextchar %=sizeof(suffixes)-1;
175+
}
176+
177+
/* add trailing $ */
178+
appendPQExpBufferChar(delimBuf,'$');
179+
180+
/* quote it and we are all done */
181+
appendPQExpBufferStr(buf,delimBuf->data);
182+
appendPQExpBufferStr(buf,str);
183+
appendPQExpBufferStr(buf,delimBuf->data);
184+
185+
destroyPQExpBuffer(delimBuf);
186+
}
187+
188+
189+
/*
190+
* Use dollar quoting if the string to be quoted contains ' or \,
191+
* otherwise use standard quoting.
192+
*/
193+
void
194+
appendStringLiteralDQOpt(PQExpBufferbuf,constchar*str,
195+
boolescapeAll,constchar*dqprefix)
196+
{
197+
if (strchr(str,'\'')==NULL&&strchr(str,'\\')==NULL)
198+
appendStringLiteral(buf,str,escapeAll);
199+
else
200+
appendStringLiteralDQ(buf,str,dqprefix);
201+
}
202+
203+
145204
/*
146205
* Convert backend's version string into a number.
147206
*/

‎src/bin/pg_dump/dumputils.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.h,v 1.10 2004/01/07 00:44:21 tgl Exp $
10+
* $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.h,v 1.11 2004/03/23 22:06:08 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -21,6 +21,10 @@
2121
externconstchar*fmtId(constchar*identifier);
2222
externvoidappendStringLiteral(PQExpBufferbuf,constchar*str,
2323
boolescapeAll);
24+
externvoidappendStringLiteralDQ(PQExpBufferbuf,constchar*str,
25+
constchar*dqprefix);
26+
externvoidappendStringLiteralDQOpt(PQExpBufferbuf,constchar*str,
27+
boolescapeAll,constchar*dqprefix);
2428
externintparse_version(constchar*versionString);
2529
externboolparsePGArray(constchar*atext,char***itemarray,int*nitems);
2630
externboolbuildACLCommands(constchar*name,constchar*type,

‎src/bin/pg_dump/pg_dump.c

Lines changed: 25 additions & 6 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.368 2004/03/20 20:09:45 tgl Exp $
15+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.369 2004/03/23 22:06:08 tgl Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -107,6 +107,9 @@ static const CatalogId nilCatalogId = { 0, 0 };
107107
staticNamespaceInfo*g_namespaces;
108108
staticintg_numNamespaces;
109109

110+
/* flag to turn on/off dollar quoting */
111+
staticintdisable_dollar_quoting=0;
112+
110113

111114
staticvoidhelp(constchar*progname);
112115
staticNamespaceInfo*findNamespace(Oidnsoid,Oidobjoid);
@@ -231,8 +234,9 @@ main(int argc, char **argv)
231234
* the following options don't have an equivalent short option
232235
* letter, but are available as '-X long-name'
233236
*/
234-
{"use-set-session-authorization",no_argument,&use_setsessauth,1},
237+
{"disable-dollar-quoting",no_argument,&disable_dollar_quoting,1},
235238
{"disable-triggers",no_argument,&disable_triggers,1},
239+
{"use-set-session-authorization",no_argument,&use_setsessauth,1},
236240

237241
{NULL,0,NULL,0}
238242
};
@@ -385,10 +389,12 @@ main(int argc, char **argv)
385389
* require arguments should use '-X feature=foo'.
386390
*/
387391
case'X':
388-
if (strcmp(optarg,"use-set-session-authorization")==0)
389-
/* no-op, still allowed for compatibility */;
392+
if (strcmp(optarg,"disable-dollar-quoting")==0)
393+
disable_dollar_quoting=1;
390394
elseif (strcmp(optarg,"disable-triggers")==0)
391395
disable_triggers=1;
396+
elseif (strcmp(optarg,"use-set-session-authorization")==0)
397+
/* no-op, still allowed for compatibility */ ;
392398
else
393399
{
394400
fprintf(stderr,
@@ -679,6 +685,8 @@ help(const char *progname)
679685
" plain text format\n"));
680686
printf(_(" -t, --table=TABLE dump the named table only\n"));
681687
printf(_(" -x, --no-privileges do not dump privileges (grant/revoke)\n"));
688+
printf(_(" -X disable-dollar-quoting, --disable-dollar-quoting\n"
689+
" disable dollar quoting, use SQL standard quoting\n"));
682690
printf(_(" -X disable-triggers, --disable-triggers\n"
683691
" disable triggers during data-only restore\n"));
684692

@@ -5076,15 +5084,26 @@ dumpFunc(Archive *fout, FuncInfo *finfo)
50765084
if (strcmp(prosrc,"-")!=0)
50775085
{
50785086
appendPQExpBuffer(asPart,", ");
5079-
appendStringLiteral(asPart,prosrc, false);
5087+
/*
5088+
* where we have bin, use dollar quoting if allowed and src
5089+
* contains quote or backslash; else use regular quoting.
5090+
*/
5091+
if (disable_dollar_quoting)
5092+
appendStringLiteral(asPart,prosrc, false);
5093+
else
5094+
appendStringLiteralDQOpt(asPart,prosrc, false,NULL);
50805095
}
50815096
}
50825097
else
50835098
{
50845099
if (strcmp(prosrc,"-")!=0)
50855100
{
50865101
appendPQExpBuffer(asPart,"AS ");
5087-
appendStringLiteral(asPart,prosrc, false);
5102+
/* with no bin, dollar quote src unconditionally if allowed */
5103+
if (disable_dollar_quoting)
5104+
appendStringLiteral(asPart,prosrc, false);
5105+
else
5106+
appendStringLiteralDQ(asPart,prosrc,NULL);
50885107
}
50895108
}
50905109

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp