Code review for standard_conforming_strings patch. Fix it so it does not

throw warnings for 100%-SQL-standard constructs, clean up some minor
infelicities, try to un-break ecpg to the best of my ability.  (It's not clear
how ecpg is going to find out the setting of standard_conforming_strings,
though.)  I think pg_dump still needs work, too.
This commit is contained in:
Tom Lane 2006-05-11 19:15:36 +00:00
parent 3fdeb189e9
commit 637028afe1
10 changed files with 80 additions and 89 deletions

View File

@ -1,4 +1,4 @@
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.57 2006/05/02 18:07:51 tgl Exp $ --> <!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.58 2006/05/11 19:15:35 tgl Exp $ -->
<chapter Id="runtime-config"> <chapter Id="runtime-config">
<title>Server Configuration</title> <title>Server Configuration</title>
@ -3734,32 +3734,15 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir'
<para> <para>
When on, a warning is issued if a backslash (<literal>\</>) When on, a warning is issued if a backslash (<literal>\</>)
appears in an ordinary string literal (<literal>'...'</> appears in an ordinary string literal (<literal>'...'</>
syntax). The default is <literal>on</>. syntax) and <varname>standard_conforming_strings</varname> is off.
The default is <literal>on</>.
</para> </para>
<para> <para>
Escape string syntax (<literal>E'...'</>) should be used for Applications that wish to use backslash as escape should be
backslash escape sequences, because ordinary strings have modified to use escape string syntax (<literal>E'...'</>),
the standard-conforming behavior of treating backslashes because the default behavior of ordinary strings will change
literally when the <literal>standard-conforming-strings</> in a future release for SQL compatibility. This variable can
option is set <literal>on</>. be enabled to help detect applications that will break.
</para>
</listitem>
</varlistentry>
<varlistentry id="guc-standard-conforming-strings" xreflabel="standard_conforming_strings">
<term><varname>standard_conforming_strings</varname> (<type>boolean</type>)</term>
<indexterm><primary>strings</><secondary>standard conforming</></>
<indexterm>
<primary><varname>standard_conforming_strings</> configuration parameter</primary>
</indexterm>
<listitem>
<para>
Controls whether ordinary string literals
(<literal>'...'</>) treat backslashes literally, as specified in
the SQL standard. Applications may check this
parameter to determine how string literals will be processed.
The presence of this parameter can also be taken as an indication
that the escape string syntax (<literal>E'...'</>) is supported.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
@ -3799,6 +3782,32 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir'
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry id="guc-standard-conforming-strings" xreflabel="standard_conforming_strings">
<term><varname>standard_conforming_strings</varname> (<type>boolean</type>)</term>
<indexterm><primary>strings</><secondary>standard conforming</></>
<indexterm>
<primary><varname>standard_conforming_strings</> configuration parameter</primary>
</indexterm>
<listitem>
<para>
This controls whether ordinary string literals
(<literal>'...'</>) treat backslashes literally, as specified in
the SQL standard.
The default is currently <literal>off</>, causing
<productname>PostgreSQL</productname> to have its historical
behavior of treating backslashes as escape characters.
The default will change to <literal>on</> in a future release
to improve compatibility with the standard.
Applications may check this
parameter to determine how string literals will be processed.
The presence of this parameter can also be taken as an indication
that the escape string syntax (<literal>E'...'</>) is supported.
Escape string syntax should be used if an application desires
backslashes to be treated as escape characters.
</para>
</listitem>
</varlistentry>
</variablelist> </variablelist>
</sect2> </sect2>

View File

@ -24,7 +24,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/parser/scan.l,v 1.133 2006/03/14 22:48:21 tgl Exp $ * $PostgreSQL: pgsql/src/backend/parser/scan.l,v 1.134 2006/05/11 19:15:35 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -51,12 +51,12 @@ static char *dolqstart; /* current $foo$ quote start string */
/* /*
* GUC variables. This is a DIRECT violation of the warning given at the * GUC variables. This is a DIRECT violation of the warning given at the
* head of gram.y, ie flex/bison code must not depend on any GUC variables; * head of gram.y, ie flex/bison code must not depend on any GUC variables;
* as such, changing its value can induce very unintuitive behavior. * as such, changing their values can induce very unintuitive behavior.
* But we shall have to live with it as a short-term thing until the switch * But we shall have to live with it as a short-term thing until the switch
* to SQL-standard string syntax is complete. * to SQL-standard string syntax is complete.
*/ */
bool escape_string_warning; bool escape_string_warning = true;
bool standard_conforming_strings; bool standard_conforming_strings = false;
static bool warn_on_first_escape; static bool warn_on_first_escape;
@ -211,8 +211,7 @@ xehexesc [\\]x[0-9A-Fa-f]{1,2}
*/ */
xqstart {quote} xqstart {quote}
xqdouble {quote}{quote} xqdouble {quote}{quote}
xqinside [^\\']+ xqinside [^']+
xqbackslash [\\]
/* $foo$ style quotes ("dollar quoting") /* $foo$ style quotes ("dollar quoting")
* The quoted string starts with $foo$ where "foo" is an optional string * The quoted string starts with $foo$ where "foo" is an optional string
@ -452,10 +451,6 @@ other .
<xe>{xeinside} { <xe>{xeinside} {
addlit(yytext, yyleng); addlit(yytext, yyleng);
} }
<xq>{xqbackslash} {
check_string_escape_warning(yytext[1]);
addlitchar('\\');
}
<xe>{xeescape} { <xe>{xeescape} {
check_string_escape_warning(yytext[1]); check_string_escape_warning(yytext[1]);
addlitchar(unescape_single_char(yytext[1])); addlitchar(unescape_single_char(yytext[1]));

View File

@ -10,7 +10,7 @@
* Written by Peter Eisentraut <peter_e@gmx.net>. * Written by Peter Eisentraut <peter_e@gmx.net>.
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.318 2006/05/02 11:28:55 teodor Exp $ * $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.319 2006/05/11 19:15:35 tgl Exp $
* *
*-------------------------------------------------------------------- *--------------------------------------------------------------------
*/ */
@ -46,6 +46,7 @@
#include "optimizer/geqo.h" #include "optimizer/geqo.h"
#include "optimizer/paths.h" #include "optimizer/paths.h"
#include "optimizer/planmain.h" #include "optimizer/planmain.h"
#include "parser/gramparse.h"
#include "parser/parse_expr.h" #include "parser/parse_expr.h"
#include "parser/parse_relation.h" #include "parser/parse_relation.h"
#include "parser/scansup.h" #include "parser/scansup.h"

View File

@ -415,8 +415,8 @@
#add_missing_from = off #add_missing_from = off
#array_nulls = on #array_nulls = on
#default_with_oids = off #default_with_oids = off
escape_string_warning = on # warn about backslashes in string literals #escape_string_warning = on
#standard_conforming_strings = off # SQL standard string literal processing #standard_conforming_strings = off
#regex_flavor = advanced # advanced, extended, or basic #regex_flavor = advanced # advanced, extended, or basic
#sql_inheritance = on #sql_inheritance = on

View File

@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2006, PostgreSQL Global Development Group * Copyright (c) 2000-2006, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.116 2006/03/14 22:48:22 tgl Exp $ * $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.117 2006/05/11 19:15:35 tgl Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "common.h" #include "common.h"
@ -1101,8 +1101,8 @@ is_superuser(void)
/* /*
* Test if the current session uses standard string literals. * Test if the current session uses standard string literals.
* *
* Note: this will correctly detect the setting only with a protocol-3.0 * Note: With a pre-protocol-3.0 connection this will always say "false",
* or newer backend; otherwise it will always say "false". * which should be the right answer.
*/ */
bool bool
standard_strings(void) standard_strings(void)

View File

@ -33,7 +33,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/bin/psql/psqlscan.l,v 1.17 2006/03/06 19:49:20 momjian Exp $ * $PostgreSQL: pgsql/src/bin/psql/psqlscan.l,v 1.18 2006/05/11 19:15:35 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/include/parser/gramparse.h,v 1.34 2006/03/14 22:48:22 tgl Exp $ * $PostgreSQL: pgsql/src/include/parser/gramparse.h,v 1.35 2006/05/11 19:15:35 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -27,6 +27,11 @@
*/ */
#define YYLTYPE int #define YYLTYPE int
/* GUC variables in scan.l (every one of these is a bad idea :-() */
extern bool escape_string_warning;
extern bool standard_conforming_strings;
/* from scan.l */ /* from scan.l */
extern void scanner_init(const char *str); extern void scanner_init(const char *str);
extern void scanner_finish(void); extern void scanner_finish(void);

View File

@ -7,7 +7,7 @@
* Copyright (c) 2000-2006, PostgreSQL Global Development Group * Copyright (c) 2000-2006, PostgreSQL Global Development Group
* Written by Peter Eisentraut <peter_e@gmx.net>. * Written by Peter Eisentraut <peter_e@gmx.net>.
* *
* $PostgreSQL: pgsql/src/include/utils/guc.h,v 1.67 2006/03/07 03:01:22 momjian Exp $ * $PostgreSQL: pgsql/src/include/utils/guc.h,v 1.68 2006/05/11 19:15:35 tgl Exp $
*-------------------------------------------------------------------- *--------------------------------------------------------------------
*/ */
#ifndef GUC_H #ifndef GUC_H
@ -120,8 +120,6 @@ extern bool SQL_inheritance;
extern bool Australian_timezones; extern bool Australian_timezones;
extern bool default_with_oids; extern bool default_with_oids;
extern bool escape_string_warning;
extern bool standard_conforming_strings;
extern int log_min_error_statement; extern int log_min_error_statement;
extern int log_min_messages; extern int log_min_messages;

View File

@ -12,7 +12,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/pgc.l,v 1.145 2006/03/06 19:49:20 momjian Exp $ * $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/pgc.l,v 1.146 2006/05/11 19:15:36 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -29,8 +29,8 @@ extern YYSTYPE yylval;
static int xcdepth = 0; /* depth of nesting in slash-star comments */ static int xcdepth = 0; /* depth of nesting in slash-star comments */
static char *dolqstart; /* current $foo$ quote start string */ static char *dolqstart; /* current $foo$ quote start string */
bool escape_string_warning; static bool escape_string_warning;
bool standard_conforming_strings; static bool standard_conforming_strings;
static bool warn_on_first_escape; static bool warn_on_first_escape;
/* /*
@ -141,8 +141,7 @@ xch 0[xX][0-9A-Fa-f]*
*/ */
xqstart {quote} xqstart {quote}
xqdouble {quote}{quote} xqdouble {quote}{quote}
xqinside [^\\']+ xqinside [^']+
xqbackslash [\\]
/* $foo$ style quotes ("dollar quoting") /* $foo$ style quotes ("dollar quoting")
* The quoted string starts with $foo$ where "foo" is an optional string * The quoted string starts with $foo$ where "foo" is an optional string
@ -402,11 +401,6 @@ cppline {space}*#(.*\\{space})*.*{newline}
/* National character. /* National character.
* Transfer it as-is to the backend. * Transfer it as-is to the backend.
*/ */
token_start = yytext;
BEGIN(xq);
startlit();
}
<C,SQL>{xqstart} {
warn_on_first_escape = true; warn_on_first_escape = true;
token_start = yytext; token_start = yytext;
state_before = YYSTATE; state_before = YYSTATE;
@ -416,7 +410,24 @@ cppline {space}*#(.*\\{space})*.*{newline}
BEGIN(xe); BEGIN(xe);
startlit(); startlit();
} }
<C,SQL>{xestart} { <C>{xqstart} {
warn_on_first_escape = false;
token_start = yytext;
state_before = YYSTATE;
BEGIN(xe);
startlit();
}
<SQL>{xqstart} {
warn_on_first_escape = true;
token_start = yytext;
state_before = YYSTATE;
if (standard_conforming_strings)
BEGIN(xq);
else
BEGIN(xe);
startlit();
}
<SQL>{xestart} {
warn_on_first_escape = false; warn_on_first_escape = false;
token_start = yytext; token_start = yytext;
state_before = YYSTATE; state_before = YYSTATE;
@ -433,10 +444,6 @@ cppline {space}*#(.*\\{space})*.*{newline}
<xq,xe>{xqdouble} { addlitchar('\''); } <xq,xe>{xqdouble} { addlitchar('\''); }
<xq>{xqinside} { addlit(yytext, yyleng); } <xq>{xqinside} { addlit(yytext, yyleng); }
<xe>{xeinside} { addlit(yytext, yyleng); } <xe>{xeinside} { addlit(yytext, yyleng); }
<xq>{xqbackslash} {
check_escape_warning();
addlitchar('\\');
}
<xe>{xeescape} { <xe>{xeescape} {
check_escape_warning(); check_escape_warning();
addlit(yytext, yyleng); addlit(yytext, yyleng);

View File

@ -927,30 +927,6 @@ show standard_conforming_strings;
(1 row) (1 row)
select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\' as f4, 'ab\''cd' as f5, '\\' as f6; select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\' as f4, 'ab\''cd' as f5, '\\' as f6;
WARNING: nonstandard use of escape in a string literal
LINE 1: select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'a...
^
HINT: Use the escape string syntax for escapes, e.g., E'\r\n'.
WARNING: nonstandard use of escape in a string literal
LINE 1: select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'a...
^
HINT: Use the escape string syntax for escapes, e.g., E'\r\n'.
WARNING: nonstandard use of escape in a string literal
LINE 1: select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'a...
^
HINT: Use the escape string syntax for escapes, e.g., E'\r\n'.
WARNING: nonstandard use of escape in a string literal
LINE 1: ...a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\' ...
^
HINT: Use the escape string syntax for escapes, e.g., E'\r\n'.
WARNING: nonstandard use of escape in a string literal
LINE 1: ...b''cd' as f2, 'a\b''''cd' as f3, 'abcd\' as f4, 'ab\''cd' ...
^
HINT: Use the escape string syntax for escapes, e.g., E'\r\n'.
WARNING: nonstandard use of escape in a string literal
LINE 1: ...b''''cd' as f3, 'abcd\' as f4, 'ab\''cd' as f5, '\\' as f6...
^
HINT: Use the escape string syntax for escapes, e.g., E'\r\n'.
f1 | f2 | f3 | f4 | f5 | f6 f1 | f2 | f3 | f4 | f5 | f6
-------+--------+---------+-------+--------+---- -------+--------+---------+-------+--------+----
a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\ a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\