Enable standard_conforming_strings to be turned on.
authorBruce Momjian
Mon, 6 Mar 2006 19:49:20 +0000 (19:49 +0000)
committerBruce Momjian
Mon, 6 Mar 2006 19:49:20 +0000 (19:49 +0000)
Kevin Grittner

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/common.h
src/bin/psql/psqlscan.l
src/include/utils/guc.h
src/interfaces/ecpg/preproc/pgc.l
src/test/regress/expected/strings.out
src/test/regress/sql/strings.sql

index 77a742e0450bac42c9a64e33c3457b38e0a02350..e255df3e1321ac07310a5b2821ee6b0ed68aaa79 100644 (file)
@@ -1,5 +1,5 @@
 
 
   Server Configuration
@@ -3737,10 +3737,28 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir'
        
        
         Escape string syntax (E'...') should be used for
-        escapes, because in future versions of
-        PostgreSQL ordinary strings will have
+        backslash escape sequences, because ordinary strings have
         the standard-conforming behavior of treating backslashes
-        literally.
+        literally when the standard-conforming-strings
+        option is set on.
+       
+      
+     
+
+     
+      standard_conforming_strings (boolean)
+      stringsescape
+      
+       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.
        
       
      
@@ -3992,28 +4010,6 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir'
       
      
 
-     
-      standard_conforming_strings (boolean)
-      stringsescape
-      
-       standard_conforming_strings configuration parameter
-      
-      
-       
-        Reports whether ordinary string literals
-        ('...') treat backslashes literally, as specified in
-        the SQL standard.  The value is currently always off,
-        indicating that backslashes are treated as escapes.  It is planned
-        that this will change to on in a future
-        PostgreSQL release when string literal
-        syntax changes to meet 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.
-       
-      
-     
-
     
    
 
index 10193bcff592c1794a748c1d3b8a65d5813d3739..e277920ee2037224309a6084ba8a9e27d1bf0a0f 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.130 2006/03/05 15:58:34 momjian Exp $
+ *   $PostgreSQL: pgsql/src/backend/parser/scan.l,v 1.131 2006/03/06 19:49:20 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -51,13 +51,14 @@ static int      xcdepth = 0;    /* depth of nesting in slash-star comments */
 static char    *dolqstart;      /* current $foo$ quote start string */
 
 /*
- * GUC variable.  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;
  * as such, changing its value 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;
 
 static bool        warn_on_first_escape;
 
@@ -77,6 +78,7 @@ static void addlitchar(unsigned char ychar);
 static char *litbufdup(void);
 static int pg_err_position(void);
 static void check_escape_warning(void);
+static void check_string_escape_warning(unsigned char ychar);
 
 /*
  * When we parse a token that requires multiple lexer rules to process,
@@ -119,7 +121,8 @@ static unsigned char unescape_single_char(unsigned char c);
  *   extended C-style comments
  *   delimited identifiers (double-quoted identifiers)
  *   hexadecimal numeric string
- *   quoted strings
+ *   standard quoted strings
+ *   extended quoted strings (support backslash escape sequences)
  *   $foo$ quoted strings
  */
 
@@ -127,6 +130,7 @@ static unsigned char unescape_single_char(unsigned char c);
 %x xc
 %x xd
 %x xh
+%x xe
 %x xq
 %x xdolq
 
@@ -200,6 +204,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}
 
 /* Extended quote
  * xqdouble implements embedded quote, ''''
@@ -207,9 +215,7 @@ xestart         [eE]{quote}
 xqstart            {quote}
 xqdouble       {quote}{quote}
 xqinside       [^\\']+
-xqescape       [\\][^0-7]
-xqoctesc       [\\][0-7]{1,3}
-xqhexesc       [\\]x[0-9A-Fa-f]{1,2}
+xqbackslash    [\\]
 
 /* $foo$ style quotes ("dollar quoting")
  * The quoted string starts with $foo$ where "foo" is an optional string
@@ -428,73 +434,62 @@ other         .
 {xqstart}      {
                    warn_on_first_escape = true;
                    token_start = yytext;
-                   BEGIN(xq);
+                   if (standard_conforming_strings)
+                       BEGIN(xq);
+                   else
+                       BEGIN(xe);
                    startlit();
                }
 {xestart}      {
                    warn_on_first_escape = false;
                    token_start = yytext;
-                   BEGIN(xq);
+                   BEGIN(xe);
                    startlit();
                }
->{quotestop}    |
-{quotefail} {
+,xe>{quotestop} |
+,xe>{quotefail} {
                    yyless(1);
                    BEGIN(INITIAL);
                    yylval.str = litbufdup();
                    return SCONST;
                }
-{xqdouble}  {
+,xe>{xqdouble}  {
                    addlitchar('\'');
                }
 {xqinside}  {
                    addlit(yytext, yyleng);
                }
-{xqescape}  {
-                   if (yytext[1] == '\'')
-                   {
-                       if (warn_on_first_escape && escape_string_warning)
-                           ereport(WARNING,
-                                   (errcode(ERRCODE_NONSTANDARD_USE_OF_ESCAPE_CHARACTER),
-                                    errmsg("nonstandard use of \\' in a string literal"),
-                                    errhint("Use '' to write quotes in strings, or use the escape string syntax (E'...')."),
-                                    errposition(pg_err_position())));
-                       warn_on_first_escape = false;   /* warn only once per string */
-                   }
-                   else if (yytext[1] == '\\')
-                   {
-                       if (warn_on_first_escape && escape_string_warning)
-                           ereport(WARNING,
-                                   (errcode(ERRCODE_NONSTANDARD_USE_OF_ESCAPE_CHARACTER),
-                                    errmsg("nonstandard use of \\\\ in a string literal"),
-                                    errhint("Use the escape string syntax for backslashes, e.g., E'\\\\'."),
-                                    errposition(pg_err_position())));
-                       warn_on_first_escape = false;   /* warn only once per string */
-                   }
-                   else
-                       check_escape_warning();
+{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]));
                }
-q>{xqoctesc}  {
+e>{xeoctesc}  {
                    unsigned char c = strtoul(yytext+1, NULL, 8);
 
                    check_escape_warning();
                    addlitchar(c);
                }
-q>{xqhexesc}  {
+e>{xehexesc}  {
                    unsigned char c = strtoul(yytext+2, NULL, 16);
 
                    check_escape_warning();
                    addlitchar(c);
                }
-{quotecontinue} {
+,xe>{quotecontinue} {
                    /* ignore */
                }
-q>.          {
+e>.          {
                    /* This is only needed for \ just before EOF */
                    addlitchar(yytext[0]);
                }
-><>        { yyerror("unterminated quoted string"); }
+,xe><>     { yyerror("unterminated quoted string"); }
 
 {dolqdelim}        {
                    token_start = yytext;
@@ -875,6 +870,33 @@ unescape_single_char(unsigned char c)
    }
 }
 
+static void
+check_string_escape_warning(unsigned char ychar)
+{
+   if (ychar == '\'')
+   {
+       if (warn_on_first_escape && escape_string_warning)
+           ereport(WARNING,
+                   (errcode(ERRCODE_NONSTANDARD_USE_OF_ESCAPE_CHARACTER),
+                    errmsg("nonstandard use of \\' in a string literal"),
+                    errhint("Use '' to write quotes in strings, or use the escape string syntax (E'...')."),
+                    errposition(pg_err_position())));
+       warn_on_first_escape = false;   /* warn only once per string */
+   }
+   else if (ychar == '\\')
+   {
+       if (warn_on_first_escape && escape_string_warning)
+           ereport(WARNING,
+                   (errcode(ERRCODE_NONSTANDARD_USE_OF_ESCAPE_CHARACTER),
+                    errmsg("nonstandard use of \\\\ in a string literal"),
+                    errhint("Use the escape string syntax for backslashes, e.g., E'\\\\'."),
+                    errposition(pg_err_position())));
+       warn_on_first_escape = false;   /* warn only once per string */
+   }
+   else
+       check_escape_warning();
+}
+
 static void
 check_escape_warning(void)
 {
index 67eb67144f022c7af573b628eaddcf2c3b8eed82..16180247bfcaaf7d800b2a67430d3084fc57c3de 100644 (file)
@@ -10,7 +10,7 @@
  * Written by Peter Eisentraut .
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.312 2006/03/05 15:58:49 momjian Exp $
+ *   $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.313 2006/03/06 19:49:20 momjian Exp $
  *
  *--------------------------------------------------------------------
  */
@@ -224,7 +224,6 @@ static int  max_index_keys;
 static int max_identifier_length;
 static int block_size;
 static bool integer_datetimes;
-static bool standard_conforming_strings;
 
 /* should be static, but commands/variable.c needs to get at these */
 char      *role_string;
@@ -974,10 +973,10 @@ static struct config_bool ConfigureNamesBool[] =
    },
 
    {
-       {"standard_conforming_strings", PGC_INTERNAL, PRESET_OPTIONS,
+       {"standard_conforming_strings", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
            gettext_noop("'...' strings treat backslashes literally."),
            NULL,
-           GUC_REPORT | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
+           GUC_REPORT
        },
        &standard_conforming_strings,
        false, NULL, NULL
index 731f9ff6c2e4098d5e0bee4bd976887d8eb9c771..071839cfd8986ccc141fd5d491626d057f46e928 100644 (file)
 #add_missing_from = off
 #array_nulls = on
 #default_with_oids = off
-#escape_string_warning = off
+#escape_string_warning = off       # warn about backslashes in string literals
+#standard_conforming_strings = off # SQL standard string literal processing
 #regex_flavor = advanced       # advanced, extended, or basic
 #sql_inheritance = on
 
index ee1333e1ddc204ed58d98e548b56e5be59926494..07f4d22c07087d19904c0227c0e56b6e5ea75a0e 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2006, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.114 2006/03/05 15:58:51 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.115 2006/03/06 19:49:20 momjian Exp $
  */
 #include "postgres_fe.h"
 #include "common.h"
@@ -1328,6 +1328,29 @@ 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".
+ */
+bool
+standard_strings(void)
+{
+   const char *val;
+
+   if (!pset.db)
+       return false;
+
+   val = PQparameterStatus(pset.db, "standard_conforming_strings");
+
+   if (val && strcmp(val, "on") == 0)
+       return true;
+
+   return false;
+}
+
+
 /*
  * Return the session user of the current connection.
  *
index 70bb10d67007fa6f0b07f807cf32d6577f159ba6..b12ccdec575a90429c29a84c342dae48362e9f95 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2006, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/common.h,v 1.46 2006/03/05 15:58:51 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/common.h,v 1.47 2006/03/06 19:49:20 momjian Exp $
  */
 #ifndef COMMON_H
 #define COMMON_H
@@ -57,6 +57,7 @@ extern PGresult *PSQLexec(const char *query, bool start_xact);
 extern bool SendQuery(const char *query);
 
 extern bool is_superuser(void);
+extern bool standard_strings(void);
 extern const char *session_username(void);
 
 extern char *expand_tilde(char **filename);
index 2341527ccbf08d5b2c515f29b8283abd4dc79e74..6c04aaf30baba68ea259399c550c6ffc37923527 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.16 2006/03/05 15:58:52 momjian Exp $
+ *   $PostgreSQL: pgsql/src/bin/psql/psqlscan.l,v 1.17 2006/03/06 19:49:20 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -154,7 +154,8 @@ static void emit(const char *txt, int len);
  *   extended C-style comments
  *   delimited identifiers (double-quoted identifiers)
  *   hexadecimal numeric string
- *   quoted strings
+ *   standard quoted strings
+ *   extended quoted strings (support backslash escape sequences)
  *   $foo$ quoted strings
  */
 
@@ -162,6 +163,7 @@ static void emit(const char *txt, int len);
 %x xc
 %x xd
 %x xh
+%x xe
 %x xq
 %x xdolq
 /* Additional exclusive states for psql only: lex backslash commands */
@@ -244,16 +246,17 @@ 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}
 
 /* Extended quote
  * xqdouble implements embedded quote, ''''
  */
 xqstart            {quote}
 xqdouble       {quote}{quote}
-xqinside       [^\\']+
-xqescape       [\\][^0-7]
-xqoctesc       [\\][0-7]{1,3}
-xqhexesc       [\\]x[0-9A-Fa-f]{1,2}
+xqinside       [^']+
 
 /* $foo$ style quotes ("dollar quoting")
  * The quoted string starts with $foo$ where "foo" is an optional string
@@ -448,38 +451,44 @@ other         .
                }
 
 {xqstart}      {
-                   BEGIN(xq);
+                   if (standard_strings())
+                       BEGIN(xq);
+                   else
+                       BEGIN(xe);
                    ECHO;
                }
 {xestart}      {
-                   BEGIN(xq);
+                   BEGIN(xe);
                    ECHO;
                }
->{quotestop}    |
-{quotefail} {
+,xe>{quotestop} |
+,xe>{quotefail} {
                    yyless(1);
                    BEGIN(INITIAL);
                    ECHO;
                }
-{xqdouble}  {
+,xe>{xqdouble}  {
                    ECHO;
                }
 {xqinside}  {
                    ECHO;
                }
-{xqescape}  {
+{xeinside}  {
+                   ECHO;
+               }
+{xeescape}  {
                    ECHO;
                }
-q>{xqoctesc}  {
+e>{xeoctesc}  {
                    ECHO;
                }
-q>{xqhexesc}  {
+e>{xehexesc}  {
                    ECHO;
                }
-{quotecontinue} {
+,xe>{quotecontinue} {
                    ECHO;
                }
-q>.          {
+e>.          {
                    /* This is only needed for \ just before EOF */
                    ECHO;
                }
@@ -858,13 +867,13 @@ other         .
 "\\r"          { appendPQExpBufferChar(output_buf, '\r'); }
 "\\f"          { appendPQExpBufferChar(output_buf, '\f'); }
 
-{xqoctesc}     {
+{xeoctesc}     {
                    /* octal case */
                    appendPQExpBufferChar(output_buf,
                                          (char) strtol(yytext + 1, NULL, 8));
                }
 
-{xqhexesc}     {
+{xehexesc}     {
                    /* hex case */
                    appendPQExpBufferChar(output_buf,
                                          (char) strtol(yytext + 2, NULL, 16));
@@ -1128,6 +1137,10 @@ psql_scan(PsqlScanState state,
                    result = PSCAN_INCOMPLETE;
                    *prompt = PROMPT_SINGLEQUOTE;
                    break;
+               case xe:
+                   result = PSCAN_INCOMPLETE;
+                   *prompt = PROMPT_SINGLEQUOTE;
+                   break;
                case xdolq:
                    result = PSCAN_INCOMPLETE;
                    *prompt = PROMPT_DOLLARQUOTE;
index a55a89b90d8db1ccb65bbf91e13a6efa45c0f5e4..6e444756f8b9d22111cf22f54dc0f2958ef742d6 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.65 2006/03/06 18:38:11 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/utils/guc.h,v 1.66 2006/03/06 19:49:20 momjian Exp $
  *--------------------------------------------------------------------
  */
 #ifndef GUC_H
@@ -121,6 +121,7 @@ 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 7e6237c0510aa15e1e06104dd4dd5b56074186b9..67f2517d21f9ed59a101271951ca5b733c2e2278 100644 (file)
@@ -12,7 +12,7 @@
  *
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/pgc.l,v 1.144 2006/03/05 15:59:08 momjian Exp $
+ *   $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/pgc.l,v 1.145 2006/03/06 19:49:20 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -30,7 +30,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;
-static bool        warn_on_first_escape;
+bool           standard_conforming_strings;
+static bool    warn_on_first_escape;
 
 /*
  * literalbuf is used to accumulate literal values when multiple rules
@@ -96,7 +97,8 @@ static struct _if_value
  *  extended C-style comments - thomas 1997-07-12
  *  delimited identifiers (double-quoted identifiers) - thomas 1997-10-27
  *  hexadecimal numeric string - thomas 1997-11-16
- *  quoted strings - thomas 1997-07-30
+ *  standard quoted strings - thomas 1997-07-30
+ *  extended quoted strings (support backslash escape sequences)
  *   $foo$ quoted strings
  */
 
@@ -105,6 +107,7 @@ static struct _if_value
 %x xd
 %x xdc
 %x xh
+%x xe
 %x xq
 %x xdolq
 %x xpre
@@ -125,6 +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}
 
 /* C version of hex number */
 xch                0[xX][0-9A-Fa-f]*
@@ -135,9 +142,7 @@ xch             0[xX][0-9A-Fa-f]*
 xqstart            {quote}
 xqdouble       {quote}{quote}
 xqinside       [^\\']+
-xqescape       [\\][^0-7]
-xqoctesc       [\\][0-7]{1,3}
-xqhexesc       [\\]x[0-9A-Fa-f]{1,2}
+xqbackslash    [\\]
 
 /* $foo$ style quotes ("dollar quoting")
  * The quoted string starts with $foo$ where "foo" is an optional string
@@ -405,43 +410,51 @@ cppline           {space}*#(.*\\{space})*.*{newline}
                warn_on_first_escape = true;
                token_start = yytext;
                state_before = YYSTATE;
-               BEGIN(xq);
+               if (standard_conforming_strings)
+                   BEGIN(xq);
+               else
+                   BEGIN(xe);
                startlit();
            }
 {xestart}   {
                warn_on_first_escape = false;
                token_start = yytext;
                state_before = YYSTATE;
-               BEGIN(xq);
+               BEGIN(xe);
                startlit();
            }
-{quotestop} |
->{quotefail}        {
+,xe>{quotestop} |
+,xe>{quotefail} {
                yyless(1);
                BEGIN(state_before);
                yylval.str = mm_strdup(literalbuf);
                return SCONST;
            }
->{xqdouble}     { addlitchar('\''); }
+,xe>{xqdouble}      { addlitchar('\''); }
 {xqinside}     { addlit(yytext, yyleng); }
-{xqescape}     { 
+{xeinside}     { addlit(yytext, yyleng); }
+{xqbackslash} {
+                   check_escape_warning();
+                   addlitchar('\\');
+                }
+{xeescape}     { 
                check_escape_warning();
                addlit(yytext, yyleng);
            }
-q>{xqoctesc}     { 
+e>{xeoctesc}     { 
                check_escape_warning();
                addlit(yytext, yyleng);
            }
-q>{xqhexesc}     { 
+e>{xehexesc}     { 
                check_escape_warning();
                addlit(yytext, yyleng);
            }
->{quotecontinue}    { /* ignore */ }
-q>{other}        {
+,xe>{quotecontinue} { /* ignore */ }
+e>.                   {
               /* This is only needed for \ just before EOF */
               addlitchar(yytext[0]);
            }
-><>        { mmerror(PARSE_ERROR, ET_FATAL, "Unterminated quoted string"); }
+,xe><>     { mmerror(PARSE_ERROR, ET_FATAL, "Unterminated quoted string"); }
 {dolqfailed}  {
                /* throw back all but the initial "$" */
                yyless(1);
index d05ce5629972303b80f210ca923431081cb732de..6faf3c69ad542b0fc35df7adc31d4050e9c94afe 100644 (file)
@@ -193,13 +193,13 @@ SELECT SUBSTRING('abcdefg' FROM 'b(.*)f') AS "cde";
 (1 row)
 
 -- PostgreSQL extension to allow using back reference in replace string;
-SELECT regexp_replace('1112223333', '(\\d{3})(\\d{3})(\\d{4})', '(\\1) \\2-\\3');
+SELECT regexp_replace('1112223333', E'(\\d{3})(\\d{3})(\\d{4})', E'(\\1) \\2-\\3');
  regexp_replace 
 ----------------
  (111) 222-3333
 (1 row)
 
-SELECT regexp_replace('AAA   BBB   CCC   ', '\\s+', ' ', 'g');
+SELECT regexp_replace('AAA   BBB   CCC   ', E'\\s+', ' ', 'g');
  regexp_replace 
 ----------------
  AAA BBB CCC 
@@ -895,3 +895,86 @@ select md5('12345678901234567890123456789012345678901234567890123456789012345678
  t
 (1 row)
 
+--
+-- test behavior of escape_string_warning and standard_conforming_strings options
+--
+set escape_string_warning = off;
+set standard_conforming_strings = off;
+show escape_string_warning;
+ escape_string_warning 
+-----------------------
+ off
+(1 row)
+
+show standard_conforming_strings;
+ standard_conforming_strings 
+-----------------------------
+ off
+(1 row)
+
+set escape_string_warning = on;
+set standard_conforming_strings = on;
+show escape_string_warning;
+ escape_string_warning 
+-----------------------
+ on
+(1 row)
+
+show standard_conforming_strings;
+ standard_conforming_strings 
+-----------------------------
+ on
+(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 at character 8
+HINT:  Use the escape string syntax for escapes, e.g., E'\r\n'.
+WARNING:  nonstandard use of escape in a string literal at character 23
+HINT:  Use the escape string syntax for escapes, e.g., E'\r\n'.
+WARNING:  nonstandard use of escape in a string literal at character 40
+HINT:  Use the escape string syntax for escapes, e.g., E'\r\n'.
+WARNING:  nonstandard use of escape in a string literal at character 59
+HINT:  Use the escape string syntax for escapes, e.g., E'\r\n'.
+WARNING:  nonstandard use of escape in a string literal at character 76
+HINT:  Use the escape string syntax for escapes, e.g., E'\r\n'.
+WARNING:  nonstandard use of escape in a string literal at character 93
+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 | \\
+(1 row)
+
+set standard_conforming_strings = off;
+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 \\ in a string literal at character 8
+HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
+WARNING:  nonstandard use of \\ in a string literal at character 24
+HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
+WARNING:  nonstandard use of \\ in a string literal at character 42
+HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
+WARNING:  nonstandard use of \\ in a string literal at character 62
+HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
+WARNING:  nonstandard use of \\ in a string literal at character 80
+HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
+WARNING:  nonstandard use of \\ in a string literal at character 98
+HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
+  f1   |   f2   |   f3    |  f4   |   f5   | f6 
+-------+--------+---------+-------+--------+----
+ a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\
+(1 row)
+
+set escape_string_warning = off;
+set standard_conforming_strings = on;
+select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\'   as f4, 'ab\''cd' as f5, '\\' as f6;
+  f1   |   f2   |   f3    |  f4   |   f5   | f6 
+-------+--------+---------+-------+--------+----
+ a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\
+(1 row)
+
+set standard_conforming_strings = off;
+select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\'   as f4, 'ab\\\'cd' as f5, '\\\\' as f6;
+  f1   |   f2   |   f3    |  f4   |   f5   | f6 
+-------+--------+---------+-------+--------+----
+ a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\
+(1 row)
+
index 620aabe8ae67dba0ec53d5e3796f636de027f585..570d9a27f46060b8334019fde77b92fa2f0dec2f 100644 (file)
@@ -81,8 +81,8 @@ SELECT SUBSTRING('abcdefg' FROM 'c.e') AS "cde";
 SELECT SUBSTRING('abcdefg' FROM 'b(.*)f') AS "cde";
 
 -- PostgreSQL extension to allow using back reference in replace string;
-SELECT regexp_replace('1112223333', '(\\d{3})(\\d{3})(\\d{4})', '(\\1) \\2-\\3');
-SELECT regexp_replace('AAA   BBB   CCC   ', '\\s+', ' ', 'g');
+SELECT regexp_replace('1112223333', E'(\\d{3})(\\d{3})(\\d{4})', E'(\\1) \\2-\\3');
+SELECT regexp_replace('AAA   BBB   CCC   ', E'\\s+', ' ', 'g');
 SELECT regexp_replace('AAA', '^|$', 'Z', 'g');
 SELECT regexp_replace('AAA aaa', 'A+', 'Z', 'gi');
 -- invalid option of REGEXP_REPLACE
@@ -352,3 +352,33 @@ select md5('abcdefghijklmnopqrstuvwxyz'::bytea) = 'c3fcd3d76192e4007dfb496cca67e
 select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'::bytea) = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
 
 select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890'::bytea) = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";
+
+--
+-- test behavior of escape_string_warning and standard_conforming_strings options
+--
+set escape_string_warning = off;
+set standard_conforming_strings = off;
+
+show escape_string_warning;
+show standard_conforming_strings;
+
+set escape_string_warning = on;
+set standard_conforming_strings = on;
+
+show escape_string_warning;
+show standard_conforming_strings;
+
+select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\'   as f4, 'ab\''cd' as f5, '\\' as f6;
+
+set standard_conforming_strings = off;
+
+select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\'   as f4, 'ab\\\'cd' as f5, '\\\\' as f6;
+
+set escape_string_warning = off;
+set standard_conforming_strings = on;
+
+select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\'   as f4, 'ab\''cd' as f5, '\\' as f6;
+
+set standard_conforming_strings = off;
+
+select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\'   as f4, 'ab\\\'cd' as f5, '\\\\' as f6;