Commit d807c7ef authored by Peter Eisentraut's avatar Peter Eisentraut

Some fine-tuning of xmlpi in corner cases:

- correct error codes
- do syntax checks in correct order
- strip leading spaces of argument
parent de9aa5a7
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/executor/execQual.c,v 1.203 2007/01/05 22:19:27 momjian Exp $ * $PostgreSQL: pgsql/src/backend/executor/execQual.c,v 1.204 2007/01/07 22:49:55 petere Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -2803,15 +2803,17 @@ ExecEvalXml(XmlExprState *xmlExpr, ExprContext *econtext, ...@@ -2803,15 +2803,17 @@ ExecEvalXml(XmlExprState *xmlExpr, ExprContext *econtext,
e = (ExprState *) linitial(xmlExpr->args); e = (ExprState *) linitial(xmlExpr->args);
value = ExecEvalExpr(e, econtext, &isnull, NULL); value = ExecEvalExpr(e, econtext, &isnull, NULL);
if (isnull) if (isnull)
return (Datum) 0; arg = NULL;
else
arg = DatumGetTextP(value); arg = DatumGetTextP(value);
} }
else else
{
arg = NULL; arg = NULL;
isnull = false;
}
*isNull = false; return PointerGetDatum(xmlpi(xexpr->name, arg, isnull, isNull));
return PointerGetDatum(xmlpi(xexpr->name, arg));
} }
break; break;
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.12 2007/01/07 00:13:55 petere Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.13 2007/01/07 22:49:56 petere Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -257,7 +257,7 @@ xmlparse(text *data, bool is_document, bool preserve_whitespace) ...@@ -257,7 +257,7 @@ xmlparse(text *data, bool is_document, bool preserve_whitespace)
xmltype * xmltype *
xmlpi(char *target, text *arg) xmlpi(char *target, text *arg, bool arg_is_null, bool *result_is_null)
{ {
#ifdef USE_LIBXML #ifdef USE_LIBXML
xmltype *result; xmltype *result;
...@@ -265,10 +265,18 @@ xmlpi(char *target, text *arg) ...@@ -265,10 +265,18 @@ xmlpi(char *target, text *arg)
if (pg_strncasecmp(target, "xml", 3) == 0) if (pg_strncasecmp(target, "xml", 3) == 0)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION), (errcode(ERRCODE_SYNTAX_ERROR), /* really */
errmsg("invalid XML processing instruction"), errmsg("invalid XML processing instruction"),
errdetail("XML processing instruction target name cannot start with \"xml\"."))); errdetail("XML processing instruction target name cannot start with \"xml\".")));
/*
* Following the SQL standard, the null check comes after the
* syntax check above.
*/
*result_is_null = arg_is_null;
if (*result_is_null)
return NULL;
initStringInfo(&buf); initStringInfo(&buf);
appendStringInfo(&buf, "<?%s", target); appendStringInfo(&buf, "<?%s", target);
...@@ -286,7 +294,7 @@ xmlpi(char *target, text *arg) ...@@ -286,7 +294,7 @@ xmlpi(char *target, text *arg)
errdetail("XML processing instruction cannot contain \"?>\"."))); errdetail("XML processing instruction cannot contain \"?>\".")));
appendStringInfoChar(&buf, ' '); appendStringInfoChar(&buf, ' ');
appendStringInfoString(&buf, string); appendStringInfoString(&buf, string + strspn(string, " "));
pfree(string); pfree(string);
} }
appendStringInfoString(&buf, "?>"); appendStringInfoString(&buf, "?>");
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/include/utils/xml.h,v 1.6 2007/01/05 22:20:00 momjian Exp $ * $PostgreSQL: pgsql/src/include/utils/xml.h,v 1.7 2007/01/07 22:49:56 petere Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -33,7 +33,7 @@ extern Datum texttoxml(PG_FUNCTION_ARGS); ...@@ -33,7 +33,7 @@ extern Datum texttoxml(PG_FUNCTION_ARGS);
extern Datum xmlvalidate(PG_FUNCTION_ARGS); extern Datum xmlvalidate(PG_FUNCTION_ARGS);
extern xmltype *xmlparse(text *data, bool is_doc, bool preserve_whitespace); extern xmltype *xmlparse(text *data, bool is_doc, bool preserve_whitespace);
extern xmltype *xmlpi(char *target, text *arg); extern xmltype *xmlpi(char *target, text *arg, bool arg_is_null, bool *result_is_null);
extern xmltype *xmlroot(xmltype *data, text *version, int standalone); extern xmltype *xmlroot(xmltype *data, text *version, int standalone);
extern char *map_sql_identifier_to_xml_name(char *ident, bool fully_escaped); extern char *map_sql_identifier_to_xml_name(char *ident, bool fully_escaped);
......
...@@ -124,6 +124,21 @@ SELECT xmlpi(name foo, 'bar'); ...@@ -124,6 +124,21 @@ SELECT xmlpi(name foo, 'bar');
SELECT xmlpi(name foo, 'in?>valid'); SELECT xmlpi(name foo, 'in?>valid');
ERROR: invalid XML processing instruction ERROR: invalid XML processing instruction
DETAIL: XML processing instruction cannot contain "?>". DETAIL: XML processing instruction cannot contain "?>".
SELECT xmlpi(name foo, null);
xmlpi
-------
(1 row)
SELECT xmlpi(name xmlstuff, null);
ERROR: invalid XML processing instruction
DETAIL: XML processing instruction target name cannot start with "xml".
SELECT xmlpi(name foo, ' bar');
xmlpi
-------------
<?foo bar?>
(1 row)
SELECT xmlroot(xml '<foo/>', version no value, standalone no value); SELECT xmlroot(xml '<foo/>', version no value, standalone no value);
xmlroot xmlroot
----------------------- -----------------------
......
...@@ -62,6 +62,12 @@ SELECT xmlpi(name foo, 'bar'); ...@@ -62,6 +62,12 @@ SELECT xmlpi(name foo, 'bar');
ERROR: no XML support in this installation ERROR: no XML support in this installation
SELECT xmlpi(name foo, 'in?>valid'); SELECT xmlpi(name foo, 'in?>valid');
ERROR: no XML support in this installation ERROR: no XML support in this installation
SELECT xmlpi(name foo, null);
ERROR: no XML support in this installation
SELECT xmlpi(name xmlstuff, null);
ERROR: no XML support in this installation
SELECT xmlpi(name foo, ' bar');
ERROR: no XML support in this installation
SELECT xmlroot(xml '<foo/>', version no value, standalone no value); SELECT xmlroot(xml '<foo/>', version no value, standalone no value);
ERROR: no XML support in this installation ERROR: no XML support in this installation
SELECT xmlroot(xml '<foo/>', version '2.0'); SELECT xmlroot(xml '<foo/>', version '2.0');
......
...@@ -51,6 +51,9 @@ SELECT xmlpi(name foo); ...@@ -51,6 +51,9 @@ SELECT xmlpi(name foo);
SELECT xmlpi(name xmlstuff); SELECT xmlpi(name xmlstuff);
SELECT xmlpi(name foo, 'bar'); SELECT xmlpi(name foo, 'bar');
SELECT xmlpi(name foo, 'in?>valid'); SELECT xmlpi(name foo, 'in?>valid');
SELECT xmlpi(name foo, null);
SELECT xmlpi(name xmlstuff, null);
SELECT xmlpi(name foo, ' bar');
SELECT xmlroot(xml '<foo/>', version no value, standalone no value); SELECT xmlroot(xml '<foo/>', version no value, standalone no value);
SELECT xmlroot(xml '<foo/>', version '2.0'); SELECT xmlroot(xml '<foo/>', version '2.0');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment