Skip to content

Commit 071d133

Browse files
committed
Fix cursor_to_xml in tableforest false mode
It only produced <row> elements but no wrapping <table> 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 <thomas.munro@enterprisedb.com>
1 parent 855f0e9 commit 071d133

File tree

4 files changed

+71
-15
lines changed

4 files changed

+71
-15
lines changed

src/backend/utils/adt/xml.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ static int xml_xpathobjtoxmlarray(xmlXPathObjectPtr xpathobj,
147147
PgXmlErrorContext *xmlerrcxt);
148148
#endif /* USE_LIBXML */
149149

150+
static void xmldata_root_element_start(StringInfo result, const char *eltname,
151+
const char *xmlschema, const char *targetns,
152+
bool top_level);
153+
static void xmldata_root_element_end(StringInfo result, const char *eltname);
150154
static StringInfo query_to_xml_internal(const char *query, char *tablename,
151155
const char *xmlschema, bool nulls, bool tableforest,
152156
const char *targetns, bool top_level);
@@ -2381,6 +2385,12 @@ cursor_to_xml(PG_FUNCTION_ARGS)
23812385

23822386
initStringInfo(&result);
23832387

2388+
if (!tableforest)
2389+
{
2390+
xmldata_root_element_start(&result, "table", NULL, targetns, true);
2391+
appendStringInfoChar(&result, '\n');
2392+
}
2393+
23842394
SPI_connect();
23852395
portal = SPI_cursor_find(name);
23862396
if (portal == NULL)
@@ -2395,6 +2405,9 @@ cursor_to_xml(PG_FUNCTION_ARGS)
23952405

23962406
SPI_finish();
23972407

2408+
if (!tableforest)
2409+
xmldata_root_element_end(&result, "table");
2410+
23982411
PG_RETURN_XML_P(stringinfo_to_xmltype(&result));
23992412
}
24002413

src/test/regress/expected/xmlmap.out

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -696,20 +696,58 @@ SELECT cursor_to_xml('xc'::refcursor, 5, false, true, '');
696696

697697
(1 row)
698698

699-
MOVE FIRST IN xc;
699+
SELECT cursor_to_xmlschema('xc'::refcursor, false, true, '');
700+
cursor_to_xmlschema
701+
----------------------------------------------------------------------------------------------
702+
<xsd:schema +
703+
xmlns:xsd="http://www.w3.org/2001/XMLSchema"> +
704+
+
705+
<xsd:simpleType name="INTEGER"> +
706+
<xsd:restriction base="xsd:int"> +
707+
<xsd:maxInclusive value="2147483647"/> +
708+
<xsd:minInclusive value="-2147483648"/> +
709+
</xsd:restriction> +
710+
</xsd:simpleType> +
711+
+
712+
<xsd:simpleType name="UDT.regression.pg_catalog.text"> +
713+
<xsd:restriction base="xsd:string"> +
714+
</xsd:restriction> +
715+
</xsd:simpleType> +
716+
+
717+
<xsd:complexType name="RowType"> +
718+
<xsd:sequence> +
719+
<xsd:element name="a" type="INTEGER" minOccurs="0"></xsd:element> +
720+
<xsd:element name="b" type="UDT.regression.pg_catalog.text" minOccurs="0"></xsd:element>+
721+
</xsd:sequence> +
722+
</xsd:complexType> +
723+
+
724+
<xsd:element name="row" type="RowType"/> +
725+
+
726+
</xsd:schema>
727+
(1 row)
728+
729+
MOVE BACKWARD ALL IN xc;
700730
SELECT cursor_to_xml('xc'::refcursor, 5, true, false, '');
701-
cursor_to_xml
702-
---------------
703-
<row> +
704-
<a>1</a> +
705-
<b>one</b> +
706-
</row> +
707-
+
708-
<row> +
709-
<a>2</a> +
710-
<b>two</b> +
711-
</row> +
712-
+
731+
cursor_to_xml
732+
---------------------------------------------------------------
733+
<table xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">+
734+
+
735+
<row> +
736+
<a>-1</a> +
737+
<b xsi:nil="true"/> +
738+
</row> +
739+
+
740+
<row> +
741+
<a>1</a> +
742+
<b>one</b> +
743+
</row> +
744+
+
745+
<row> +
746+
<a>2</a> +
747+
<b>two</b> +
748+
</row> +
749+
+
750+
</table> +
713751

714752
(1 row)
715753

src/test/regress/expected/xmlmap_1.out

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ SELECT cursor_to_xml('xc'::refcursor, 5, false, true, '');
7878
ERROR: unsupported XML feature
7979
DETAIL: This functionality requires the server to be built with libxml support.
8080
HINT: You need to rebuild PostgreSQL using --with-libxml.
81-
MOVE FIRST IN xc;
81+
SELECT cursor_to_xmlschema('xc'::refcursor, false, true, '');
82+
ERROR: unsupported XML feature
83+
DETAIL: This functionality requires the server to be built with libxml support.
84+
HINT: You need to rebuild PostgreSQL using --with-libxml.
85+
MOVE BACKWARD ALL IN xc;
8286
SELECT cursor_to_xml('xc'::refcursor, 5, true, false, '');
8387
ERROR: unsupported XML feature
8488
DETAIL: This functionality requires the server to be built with libxml support.

src/test/regress/sql/xmlmap.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ SELECT query_to_xml_and_xmlschema('SELECT * FROM testxmlschema.test1', true, tru
3030

3131
DECLARE xc CURSOR WITH HOLD FOR SELECT * FROM testxmlschema.test1 ORDER BY 1, 2;
3232
SELECT cursor_to_xml('xc'::refcursor, 5, false, true, '');
33-
MOVE FIRST IN xc;
33+
SELECT cursor_to_xmlschema('xc'::refcursor, false, true, '');
34+
MOVE BACKWARD ALL IN xc;
3435
SELECT cursor_to_xml('xc'::refcursor, 5, true, false, '');
3536
SELECT cursor_to_xmlschema('xc'::refcursor, true, false, '');
3637

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