Code review for standard_conforming_strings patch. Fix it so it does not
authorTom Lane
Thu, 11 May 2006 19:15:36 +0000 (19:15 +0000)
committerTom Lane
Thu, 11 May 2006 19:15:36 +0000 (19:15 +0000)
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.

doc/src/sgml/config.sgml
src/backend/parser/scan.l
src/backend/utils/misc/guc.c
src/backend/utils/misc/postgresql.conf.sample
src/bin/psql/common.c
src/bin/psql/psqlscan.l
src/include/parser/gramparse.h
src/include/utils/guc.h
src/interfaces/ecpg/preproc/pgc.l
src/test/regress/expected/strings.out

index 933ca3811dc26f3f2810d9c85df33e7b28e698e0..a2e6d46ca6ae02e50bb460717a78cadd518e5372 100644 (file)
@@ -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.
+       
+      
+     
+
      
     
 
index 0ac8d346a7bd13194a81c0e3b59d7ff6b68fc8e2..78a888e02afd718f98b03fa7d466c49d8b603edc 100644 (file)
@@ -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]));
index c2c1318b4cb4361576355d0aefe1b9be8263e231..87df3fdbacdc40bc691aef8e8b1c1e64e3729a7e 100644 (file)
@@ -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"
index 84c9f7958483512877c2d4bde928326893f28aac..dac59378a076c5a7dabd8365ebfd878d836fadcf 100644 (file)
                    #   warning
                    #   error
                    #   panic(off)
-                
+
 #log_min_duration_statement = -1   # -1 is disabled, 0 logs all statements
                    # and their durations, in milliseconds.
 
 #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
 
index ec0acc156e7f80356ead2c719c25f0ea9db750f1..ba8e403dd2a6558f948aa3edc59d81b578516cc9 100644 (file)
@@ -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)
index 6c04aaf30baba68ea259399c550c6ffc37923527..4a344baf6b20c25ff5fee579df7f7acd8ca814cc 100644 (file)
@@ -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}  {
index 13af69116cd3bc34a0b8bacf7eed2cda0084dcbb..8430ad8c8e80ecb1dccfa512dc79bc60e68081ec 100644 (file)
@@ -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 $
  *
  *-------------------------------------------------------------------------
  */
  */
 #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);
index d60f5061b26ad67a7e2b53dc9f3b8e4a99d1ebf7..182a762cc029b8e59714a26fa982c22effcbb07a 100644 (file)
@@ -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;
index 67f2517d21f9ed59a101271951ca5b733c2e2278..a835a1da6969dfa847ad1ae1d4d12dfa9a22bf14 100644 (file)
@@ -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}   {
+               warn_on_first_escape = false;
+               token_start = yytext;
+               state_before = YYSTATE;
+               BEGIN(xe);
                startlit();
            }
-<C,SQL>{xqstart}   {
+<SQL>{xqstart} {
                warn_on_first_escape = true;
                token_start = yytext;
                state_before = YYSTATE;
@@ -416,7 +427,7 @@ cppline         {space}*#(.*\\{space})*.*{newline}
                    BEGIN(xe);
                startlit();
            }
-<C,SQL>{xestart}   {
+<SQL>{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);
index 0c9b88ee8f0ee6d62b1a94ed2fee30cbac5ba014..20081f699b68a73857ea84331bc8b48028996a84 100644 (file)
@@ -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 | \\