mirror of
https://github.com/postgres/postgres.git
synced 2025-11-10 17:42:29 +03:00
Add E'' syntax so eventually normal strings can treat backslashes
literally.
Add GUC variables:
"escape_string_warning" - warn about backslashes in non-E strings
"escape_string_syntax" - supports E'' syntax?
"standard_compliant_strings" - treats backslashes literally in ''
Update code to use E'' when escapes are used.
This commit is contained in:
@@ -24,7 +24,7 @@
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/parser/scan.l,v 1.125 2005/06/15 16:28:06 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/parser/scan.l,v 1.126 2005/06/26 03:03:38 momjian Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -48,7 +48,9 @@
|
||||
extern YYSTYPE yylval;
|
||||
|
||||
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 */
|
||||
static bool warn_on_first_escape;
|
||||
bool escape_string_warning;
|
||||
|
||||
/*
|
||||
* literalbuf is used to accumulate literal values when multiple rules
|
||||
@@ -64,6 +66,7 @@ static int literalalloc; /* current allocated buffer size */
|
||||
static void addlit(char *ytext, int yleng);
|
||||
static void addlitchar(unsigned char ychar);
|
||||
static char *litbufdup(void);
|
||||
static void check_escape_warning(void);
|
||||
|
||||
/*
|
||||
* When we parse a token that requires multiple lexer rules to process,
|
||||
@@ -185,6 +188,10 @@ xhinside [^']*
|
||||
/* National character */
|
||||
xnstart [nN]{quote}
|
||||
|
||||
/* Quote string does not warn about escapes */
|
||||
xestart [eE]{quote}
|
||||
xeinside [^']*
|
||||
|
||||
/* Extended quote
|
||||
* xqdouble implements embedded quote, ''''
|
||||
*/
|
||||
@@ -410,6 +417,13 @@ other .
|
||||
}
|
||||
|
||||
{xqstart} {
|
||||
warn_on_first_escape = true;
|
||||
token_start = yytext;
|
||||
BEGIN(xq);
|
||||
startlit();
|
||||
}
|
||||
{xestart} {
|
||||
warn_on_first_escape = false;
|
||||
token_start = yytext;
|
||||
BEGIN(xq);
|
||||
startlit();
|
||||
@@ -428,14 +442,36 @@ other .
|
||||
addlit(yytext, yyleng);
|
||||
}
|
||||
<xq>{xqescape} {
|
||||
if (yytext[1] == '\'')
|
||||
{
|
||||
if (warn_on_first_escape && escape_string_warning)
|
||||
ereport(WARNING,
|
||||
(errcode(ERRCODE_INVALID_USE_OF_ESCAPE_CHARACTER),
|
||||
errmsg("Invalid use of \\' in a normal string"),
|
||||
errhint("Use '' to place quotes in strings, or use the escape string syntax (E'').")));
|
||||
}
|
||||
else if (yytext[1] == '\\')
|
||||
{
|
||||
if (warn_on_first_escape && escape_string_warning)
|
||||
ereport(WARNING,
|
||||
(errcode(ERRCODE_INVALID_USE_OF_ESCAPE_CHARACTER),
|
||||
errmsg("Invalid use of \\\\ in a normal string"),
|
||||
errhint("Use the escape string syntax for backslashes, e.g. E'\\\\'.")));
|
||||
}
|
||||
else
|
||||
check_escape_warning();
|
||||
addlitchar(unescape_single_char(yytext[1]));
|
||||
}
|
||||
<xq>{xqoctesc} {
|
||||
unsigned char c = strtoul(yytext+1, NULL, 8);
|
||||
|
||||
check_escape_warning();
|
||||
addlitchar(c);
|
||||
}
|
||||
<xq>{xqhexesc} {
|
||||
unsigned char c = strtoul(yytext+2, NULL, 16);
|
||||
|
||||
check_escape_warning();
|
||||
addlitchar(c);
|
||||
}
|
||||
<xq>{quotecontinue} {
|
||||
@@ -810,3 +846,14 @@ unescape_single_char(unsigned char c)
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
check_escape_warning(void)
|
||||
{
|
||||
if (warn_on_first_escape && escape_string_warning)
|
||||
ereport(WARNING,
|
||||
(errcode(ERRCODE_INVALID_USE_OF_ESCAPE_CHARACTER),
|
||||
errmsg("Invalid use of escapes in an ordinary string"),
|
||||
errhint("Use the escape string syntax for escapes, e.g. E'\\r\\n'.")));
|
||||
warn_on_first_escape = false; /* warn only once per string */
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
* Written by Peter Eisentraut <peter_e@gmx.net>.
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.268 2005/06/17 22:32:47 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.269 2005/06/26 03:03:41 momjian Exp $
|
||||
*
|
||||
*--------------------------------------------------------------------
|
||||
*/
|
||||
@@ -189,7 +189,9 @@ static int max_function_args;
|
||||
static int max_index_keys;
|
||||
static int max_identifier_length;
|
||||
static int block_size;
|
||||
static bool integer_datetimes;
|
||||
static bool integer_datetimes;
|
||||
static bool escape_string_syntax;
|
||||
static bool standard_compliant_strings;
|
||||
|
||||
/* should be static, but commands/variable.c needs to get at it */
|
||||
char *session_authorization_string;
|
||||
@@ -873,6 +875,35 @@ static struct config_bool ConfigureNamesBool[] =
|
||||
false, NULL, NULL
|
||||
},
|
||||
|
||||
{
|
||||
{"escape_string_warning", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
|
||||
gettext_noop("Warn about backslash escapes in ordinary, non-escape-syntax strings."),
|
||||
NULL
|
||||
},
|
||||
&escape_string_warning,
|
||||
false, NULL, NULL
|
||||
},
|
||||
|
||||
{
|
||||
{"escape_string_syntax", PGC_INTERNAL, PRESET_OPTIONS,
|
||||
gettext_noop("Escape string syntax (E'') is supported."),
|
||||
NULL,
|
||||
GUC_REPORT | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
|
||||
},
|
||||
&escape_string_syntax,
|
||||
true, NULL, NULL
|
||||
},
|
||||
|
||||
{
|
||||
{"standard_compliant_strings", PGC_INTERNAL, PRESET_OPTIONS,
|
||||
gettext_noop("'' strings treat backslashes literally."),
|
||||
NULL,
|
||||
GUC_REPORT | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
|
||||
},
|
||||
&standard_compliant_strings,
|
||||
false, NULL, NULL
|
||||
},
|
||||
|
||||
/* End-of-list marker */
|
||||
{
|
||||
{NULL, 0, 0, NULL, NULL}, NULL, false, NULL, NULL
|
||||
|
||||
@@ -330,6 +330,7 @@
|
||||
#regex_flavor = advanced # advanced, extended, or basic
|
||||
#sql_inheritance = true
|
||||
#default_with_oids = false
|
||||
#escape_string_warning = false
|
||||
|
||||
# - Other Platforms & Clients -
|
||||
|
||||
|
||||
Reference in New Issue
Block a user