From a48d47908337ce3a17da37e413f1e2ea658552f9 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Wed, 3 May 2017 21:25:01 -0400 Subject: [PATCH] Fix cursor_to_xml in tableforest false mode It only produced elements but no wrapping element. By contrast, cursor_to_xmlschema produced a schema that is now correct but did not previously match the XML data produced by cursor_to_xml. In passing, also fix a minor misunderstanding about moving cursors in the tests related to this. Reported-by: filip@jirsak.org Based-on-patch-by: Thomas Munro --- src/backend/utils/adt/xml.c | 10 ++++ src/test/regress/expected/xmlmap.out | 64 ++++++++++++++++++++------ src/test/regress/expected/xmlmap_1.out | 6 ++- src/test/regress/sql/xmlmap.sql | 3 +- 4 files changed, 68 insertions(+), 15 deletions(-) diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c index d1122dc580c..df06d7c37a1 100644 --- a/src/backend/utils/adt/xml.c +++ b/src/backend/utils/adt/xml.c @@ -146,6 +146,10 @@ static int xml_xpathobjtoxmlarray(xmlXPathObjectPtr xpathobj, PgXmlErrorContext *xmlerrcxt); #endif /* USE_LIBXML */ +static void xmldata_root_element_start(StringInfo result, const char *eltname, + const char *xmlschema, const char *targetns, + bool top_level); +static void xmldata_root_element_end(StringInfo result, const char *eltname); static StringInfo query_to_xml_internal(const char *query, char *tablename, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level); @@ -2380,6 +2384,9 @@ cursor_to_xml(PG_FUNCTION_ARGS) initStringInfo(&result); + if (!tableforest) + xmldata_root_element_start(&result, "table", NULL, targetns, true); + SPI_connect(); portal = SPI_cursor_find(name); if (portal == NULL) @@ -2394,6 +2401,9 @@ cursor_to_xml(PG_FUNCTION_ARGS) SPI_finish(); + if (!tableforest) + xmldata_root_element_end(&result, "table"); + PG_RETURN_XML_P(stringinfo_to_xmltype(&result)); } diff --git a/src/test/regress/expected/xmlmap.out b/src/test/regress/expected/xmlmap.out index cf46c9ffb58..b72761d7f89 100644 --- a/src/test/regress/expected/xmlmap.out +++ b/src/test/regress/expected/xmlmap.out @@ -714,20 +714,58 @@ SELECT cursor_to_xml('xc'::refcursor, 5, false, true, ''); (1 row) -MOVE FIRST IN xc; +SELECT cursor_to_xmlschema('xc'::refcursor, false, true, ''); + cursor_to_xmlschema +---------------------------------------------------------------------------------------------- + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +(1 row) + +MOVE BACKWARD ALL IN xc; SELECT cursor_to_xml('xc'::refcursor, 5, true, false, ''); - cursor_to_xml ---------------- - + - 1 + - one + - + - + - + - 2 + - two + - + - + + cursor_to_xml +--------------------------------------------------------------- +
+ + + + + + -1 + + + + + + + + + + 1 + + one + + + + + + + + 2 + + two + + + + + +
+ (1 row) diff --git a/src/test/regress/expected/xmlmap_1.out b/src/test/regress/expected/xmlmap_1.out index 932122f6be0..e0dd23439e3 100644 --- a/src/test/regress/expected/xmlmap_1.out +++ b/src/test/regress/expected/xmlmap_1.out @@ -78,7 +78,11 @@ SELECT cursor_to_xml('xc'::refcursor, 5, false, true, ''); ERROR: unsupported XML feature DETAIL: This functionality requires the server to be built with libxml support. HINT: You need to rebuild PostgreSQL using --with-libxml. -MOVE FIRST IN xc; +SELECT cursor_to_xmlschema('xc'::refcursor, false, true, ''); +ERROR: unsupported XML feature +DETAIL: This functionality requires the server to be built with libxml support. +HINT: You need to rebuild PostgreSQL using --with-libxml. +MOVE BACKWARD ALL IN xc; SELECT cursor_to_xml('xc'::refcursor, 5, true, false, ''); ERROR: unsupported XML feature DETAIL: This functionality requires the server to be built with libxml support. diff --git a/src/test/regress/sql/xmlmap.sql b/src/test/regress/sql/xmlmap.sql index df1f98046d3..d2203039ced 100644 --- a/src/test/regress/sql/xmlmap.sql +++ b/src/test/regress/sql/xmlmap.sql @@ -30,7 +30,8 @@ SELECT query_to_xml_and_xmlschema('SELECT * FROM testxmlschema.test1', true, tru DECLARE xc CURSOR WITH HOLD FOR SELECT * FROM testxmlschema.test1 ORDER BY 1, 2; SELECT cursor_to_xml('xc'::refcursor, 5, false, true, ''); -MOVE FIRST IN xc; +SELECT cursor_to_xmlschema('xc'::refcursor, false, true, ''); +MOVE BACKWARD ALL IN xc; SELECT cursor_to_xml('xc'::refcursor, 5, true, false, ''); SELECT cursor_to_xmlschema('xc'::refcursor, true, false, ''); -- 2.39.5