Skip to content

Commit 8832f0f

Browse files
committed
De-escape XML names when reverse-compiling XML expressions.
1 parent 9aefd56 commit 8832f0f

File tree

3 files changed

+76
-11
lines changed

3 files changed

+76
-11
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* ruleutils.c - Functions to convert stored expressions/querytrees
33
* back to source text
44
*
5-
* $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.238 2006/12/24 00:29:19 tgl Exp $
5+
* $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.239 2006/12/29 10:50:22 petere Exp $
66
**********************************************************************/
77

88
#include "postgres.h"
@@ -3810,11 +3810,8 @@ get_rule_expr(Node *node, deparse_context *context,
38103810
}
38113811
if (xexpr->name)
38123812
{
3813-
/*
3814-
* XXX need to de-escape the name
3815-
*/
38163813
appendStringInfo(buf, "NAME %s",
3817-
quote_identifier(xexpr->name));
3814+
quote_identifier(map_xml_name_to_sql_identifier(xexpr->name)));
38183815
needcomma = true;
38193816
}
38203817
if (xexpr->named_args)
@@ -3834,11 +3831,8 @@ get_rule_expr(Node *node, deparse_context *context,
38343831
if (needcomma)
38353832
appendStringInfoString(buf, ", ");
38363833
get_rule_expr((Node *) e, context, true);
3837-
/*
3838-
* XXX need to de-escape the name
3839-
*/
38403834
appendStringInfo(buf, " AS %s",
3841-
quote_identifier(argname));
3835+
quote_identifier(map_xml_name_to_sql_identifier(argname)));
38423836
needcomma = true;
38433837
}
38443838
if (xexpr->op != IS_XMLFOREST)

src/backend/utils/adt/xml.c

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.7 2006/12/28 14:28:36 petere Exp $
10+
* $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.8 2006/12/29 10:50:22 petere Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -882,6 +882,42 @@ sqlchar_to_unicode(char *s)
882882
}
883883

884884

885+
static char *
886+
unicode_to_sqlchar(pg_wchar c)
887+
{
888+
char utf8string[4] = { '\0', '\0', '\0', '\0' };
889+
890+
if (c <= 0x7F)
891+
{
892+
utf8string[0] = c;
893+
}
894+
else if (c <= 0x7FF)
895+
{
896+
utf8string[0] = 0xC0 | ((c >> 6) & 0x1F);
897+
utf8string[1] = 0x80 | (c & 0x3F);
898+
}
899+
else if (c <= 0xFFFF)
900+
{
901+
utf8string[0] = 0xE0 | ((c >> 12) & 0x0F);
902+
utf8string[1] = 0x80 | ((c >> 6) & 0x3F);
903+
utf8string[2] = 0x80 | (c & 0x3F);
904+
}
905+
else
906+
{
907+
utf8string[0] = 0xF0 | ((c >> 18) & 0x07);
908+
utf8string[1] = 0x80 | ((c >> 12) & 0x3F);
909+
utf8string[2] = 0x80 | ((c >> 6) & 0x3F);
910+
utf8string[3] = 0x80 | (c & 0x3F);
911+
912+
}
913+
914+
return (char *) pg_do_encoding_conversion((unsigned char *) utf8string,
915+
pg_mblen(utf8string),
916+
PG_UTF8,
917+
GetDatabaseEncoding());
918+
}
919+
920+
885921
static bool
886922
is_valid_xml_namefirst(pg_wchar c)
887923
{
@@ -949,3 +985,37 @@ map_sql_identifier_to_xml_name(char *ident, bool fully_escaped)
949985
return NULL;
950986
#endif /* not USE_LIBXML */
951987
}
988+
989+
990+
/*
991+
* The inverse; see SQL/XML:2003 section 9.17.
992+
*/
993+
char *
994+
map_xml_name_to_sql_identifier(char *name)
995+
{
996+
StringInfoData buf;
997+
char *p;
998+
999+
initStringInfo(&buf);
1000+
1001+
for (p = name; *p; p += pg_mblen(p))
1002+
{
1003+
if (*p == '_' && *(p+1) == 'x'
1004+
&& isxdigit((unsigned char) *(p+2))
1005+
&& isxdigit((unsigned char) *(p+3))
1006+
&& isxdigit((unsigned char) *(p+4))
1007+
&& isxdigit((unsigned char) *(p+5))
1008+
&& *(p+6) == '_')
1009+
{
1010+
unsigned int u;
1011+
1012+
sscanf(p + 2, "%X", &u);
1013+
appendStringInfoString(&buf, unicode_to_sqlchar(u));
1014+
p += 6;
1015+
}
1016+
else
1017+
appendBinaryStringInfo(&buf, p, pg_mblen(p));
1018+
}
1019+
1020+
return buf.data;
1021+
}

src/include/utils/xml.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/utils/xml.h,v 1.4 2006/12/28 14:28:36 petere Exp $
10+
* $PostgreSQL: pgsql/src/include/utils/xml.h,v 1.5 2006/12/29 10:50:22 petere Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -37,5 +37,6 @@ extern xmltype *xmlpi(char *target, text *arg);
3737
extern xmltype *xmlroot(xmltype *data, text *version, int standalone);
3838

3939
extern char *map_sql_identifier_to_xml_name(char *ident, bool fully_escaped);
40+
extern char *map_xml_name_to_sql_identifier(char *name);
4041

4142
#endif /* XML_H */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy