diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index 933ca3811dc..a2e6d46ca6a 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -1,4 +1,4 @@ - + Server Configuration @@ -3734,32 +3734,15 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir' When on, a warning is issued if a backslash (\) appears in an ordinary string literal ('...' - syntax). The default is on. + syntax) and standard_conforming_strings is off. + The default is on. - Escape string syntax (E'...') should be used for - backslash escape sequences, because ordinary strings have - the standard-conforming behavior of treating backslashes - literally when the standard-conforming-strings - option is set on. - - - - - - standard_conforming_strings (boolean) - stringsstandard conforming - - standard_conforming_strings configuration parameter - - - - Controls whether ordinary string literals - ('...') 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 (E'...') is supported. + Applications that wish to use backslash as escape should be + modified to use escape string syntax (E'...'), + because the default behavior of ordinary strings will change + in a future release for SQL compatibility. This variable can + be enabled to help detect applications that will break. @@ -3799,6 +3782,32 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir' + + standard_conforming_strings (boolean) + stringsstandard conforming + + standard_conforming_strings configuration parameter + + + + This controls whether ordinary string literals + ('...') treat backslashes literally, as specified in + the SQL standard. + The default is currently off, causing + PostgreSQL to have its historical + behavior of treating backslashes as escape characters. + The default will change to 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 (E'...') is supported. + Escape string syntax should be used if an application desires + backslashes to be treated as escape characters. + + + + diff --git a/src/backend/parser/scan.l b/src/backend/parser/scan.l index 0ac8d346a7b..78a888e02af 100644 --- a/src/backend/parser/scan.l +++ b/src/backend/parser/scan.l @@ -24,7 +24,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * 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 * 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 * to SQL-standard string syntax is complete. */ -bool escape_string_warning; -bool standard_conforming_strings; +bool escape_string_warning = true; +bool standard_conforming_strings = false; static bool warn_on_first_escape; @@ -211,8 +211,7 @@ xehexesc [\\]x[0-9A-Fa-f]{1,2} */ xqstart {quote} xqdouble {quote}{quote} -xqinside [^\\']+ -xqbackslash [\\] +xqinside [^']+ /* $foo$ style quotes ("dollar quoting") * The quoted string starts with $foo$ where "foo" is an optional string @@ -443,7 +442,7 @@ other . yylval.str = litbufdup(); return SCONST; } -{xqdouble} { +{xqdouble} { addlitchar('\''); } {xqinside} { @@ -452,10 +451,6 @@ other . {xeinside} { addlit(yytext, yyleng); } -{xqbackslash} { - check_string_escape_warning(yytext[1]); - addlitchar('\\'); - } {xeescape} { check_string_escape_warning(yytext[1]); addlitchar(unescape_single_char(yytext[1])); diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index c2c1318b4cb..87df3fdbacd 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -10,7 +10,7 @@ * Written by Peter Eisentraut . * * 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/paths.h" #include "optimizer/planmain.h" +#include "parser/gramparse.h" #include "parser/parse_expr.h" #include "parser/parse_relation.h" #include "parser/scansup.h" diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample index 84c9f795848..dac59378a07 100644 --- a/src/backend/utils/misc/postgresql.conf.sample +++ b/src/backend/utils/misc/postgresql.conf.sample @@ -279,7 +279,7 @@ # warning # error # panic(off) - + #log_min_duration_statement = -1 # -1 is disabled, 0 logs all statements # and their durations, in milliseconds. @@ -415,8 +415,8 @@ #add_missing_from = off #array_nulls = on #default_with_oids = off -escape_string_warning = on # warn about backslashes in string literals -#standard_conforming_strings = off # SQL standard string literal processing +#escape_string_warning = on +#standard_conforming_strings = off #regex_flavor = advanced # advanced, extended, or basic #sql_inheritance = on diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index ec0acc156e7..ba8e403dd2a 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -3,7 +3,7 @@ * * 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 "common.h" @@ -1101,8 +1101,8 @@ is_superuser(void) /* * Test if the current session uses standard string literals. * - * Note: this will correctly detect the setting only with a protocol-3.0 - * or newer backend; otherwise it will always say "false". + * Note: With a pre-protocol-3.0 connection this will always say "false", + * which should be the right answer. */ bool standard_strings(void) diff --git a/src/bin/psql/psqlscan.l b/src/bin/psql/psqlscan.l index 6c04aaf30ba..4a344baf6b2 100644 --- a/src/bin/psql/psqlscan.l +++ b/src/bin/psql/psqlscan.l @@ -33,7 +33,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * 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 $ * *------------------------------------------------------------------------- */ @@ -467,7 +467,7 @@ other . BEGIN(INITIAL); ECHO; } -{xqdouble} { +{xqdouble} { ECHO; } {xqinside} { diff --git a/src/include/parser/gramparse.h b/src/include/parser/gramparse.h index 13af69116cd..8430ad8c8e8 100644 --- a/src/include/parser/gramparse.h +++ b/src/include/parser/gramparse.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group * 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 +/* 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 */ extern void scanner_init(const char *str); extern void scanner_finish(void); diff --git a/src/include/utils/guc.h b/src/include/utils/guc.h index d60f5061b26..182a762cc02 100644 --- a/src/include/utils/guc.h +++ b/src/include/utils/guc.h @@ -7,7 +7,7 @@ * Copyright (c) 2000-2006, PostgreSQL Global Development Group * Written by Peter Eisentraut . * - * $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 @@ -120,8 +120,6 @@ extern bool SQL_inheritance; extern bool Australian_timezones; 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_messages; diff --git a/src/interfaces/ecpg/preproc/pgc.l b/src/interfaces/ecpg/preproc/pgc.l index 67f2517d21f..a835a1da696 100644 --- a/src/interfaces/ecpg/preproc/pgc.l +++ b/src/interfaces/ecpg/preproc/pgc.l @@ -12,7 +12,7 @@ * * * 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 char *dolqstart; /* current $foo$ quote start string */ -bool escape_string_warning; -bool standard_conforming_strings; +static bool escape_string_warning; +static bool standard_conforming_strings; static bool warn_on_first_escape; /* @@ -128,10 +128,10 @@ xnstart [nN]{quote} /* Quoted string that allows backslash escapes */ xestart [eE]{quote} -xeinside [^\\']+ -xeescape [\\][^0-7] -xeoctesc [\\][0-7]{1,3} -xehexesc [\\]x[0-9A-Fa-f]{1,2} +xeinside [^\\']+ +xeescape [\\][^0-7] +xeoctesc [\\][0-7]{1,3} +xehexesc [\\]x[0-9A-Fa-f]{1,2} /* C version of hex number */ xch 0[xX][0-9A-Fa-f]* @@ -141,8 +141,7 @@ xch 0[xX][0-9A-Fa-f]* */ xqstart {quote} xqdouble {quote}{quote} -xqinside [^\\']+ -xqbackslash [\\] +xqinside [^']+ /* $foo$ style quotes ("dollar quoting") * The quoted string starts with $foo$ where "foo" is an optional string @@ -402,11 +401,23 @@ cppline {space}*#(.*\\{space})*.*{newline} /* National character. * Transfer it as-is to the backend. */ + warn_on_first_escape = true; token_start = yytext; - BEGIN(xq); + state_before = YYSTATE; + if (standard_conforming_strings) + BEGIN(xq); + else + BEGIN(xe); startlit(); } -{xqstart} { +{xqstart} { + warn_on_first_escape = false; + token_start = yytext; + state_before = YYSTATE; + BEGIN(xe); + startlit(); + } +{xqstart} { warn_on_first_escape = true; token_start = yytext; state_before = YYSTATE; @@ -416,7 +427,7 @@ cppline {space}*#(.*\\{space})*.*{newline} BEGIN(xe); startlit(); } -{xestart} { +{xestart} { warn_on_first_escape = false; token_start = yytext; state_before = YYSTATE; @@ -433,10 +444,6 @@ cppline {space}*#(.*\\{space})*.*{newline} {xqdouble} { addlitchar('\''); } {xqinside} { addlit(yytext, yyleng); } {xeinside} { addlit(yytext, yyleng); } -{xqbackslash} { - check_escape_warning(); - addlitchar('\\'); - } {xeescape} { check_escape_warning(); addlit(yytext, yyleng); diff --git a/src/test/regress/expected/strings.out b/src/test/regress/expected/strings.out index 0c9b88ee8f0..20081f699b6 100644 --- a/src/test/regress/expected/strings.out +++ b/src/test/regress/expected/strings.out @@ -927,30 +927,6 @@ show standard_conforming_strings; (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; -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 -------+--------+---------+-------+--------+---- a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\