]> granicus.if.org Git - postgresql/commitdiff
Fix cursor_to_xml in tableforest false mode
authorPeter Eisentraut <peter_e@gmx.net>
Thu, 4 May 2017 01:25:01 +0000 (21:25 -0400)
committerPeter Eisentraut <peter_e@gmx.net>
Fri, 5 May 2017 01:17:46 +0000 (21:17 -0400)
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>
src/backend/utils/adt/xml.c
src/test/regress/expected/xmlmap.out
src/test/regress/expected/xmlmap_1.out
src/test/regress/sql/xmlmap.sql

index b144920ec65d02d00312b5e2621b6f1d7b6013e3..f5348b34657b7552118d546d34a2338031a606fe 100644 (file)
@@ -147,6 +147,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);
@@ -2381,6 +2385,12 @@ cursor_to_xml(PG_FUNCTION_ARGS)
 
        initStringInfo(&result);
 
+       if (!tableforest)
+       {
+               xmldata_root_element_start(&result, "table", NULL, targetns, true);
+               appendStringInfoChar(&result, '\n');
+       }
+
        SPI_connect();
        portal = SPI_cursor_find(name);
        if (portal == NULL)
@@ -2395,6 +2405,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));
 }
 
index b50396face9d56a0810dfa7649c0e2f597aef5b1..c08f8a0d9c17a466c63a83150ce2baa49445f971 100644 (file)
@@ -696,20 +696,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                                      
+----------------------------------------------------------------------------------------------
+ <xsd:schema                                                                                 +
+     xmlns:xsd="http://www.w3.org/2001/XMLSchema">                                           +
+                                                                                             +
+ <xsd:simpleType name="INTEGER">                                                             +
+   <xsd:restriction base="xsd:int">                                                          +
+     <xsd:maxInclusive value="2147483647"/>                                                  +
+     <xsd:minInclusive value="-2147483648"/>                                                 +
+   </xsd:restriction>                                                                        +
+ </xsd:simpleType>                                                                           +
+                                                                                             +
+ <xsd:simpleType name="UDT.regression.pg_catalog.text">                                      +
+   <xsd:restriction base="xsd:string">                                                       +
+   </xsd:restriction>                                                                        +
+ </xsd:simpleType>                                                                           +
+                                                                                             +
+ <xsd:complexType name="RowType">                                                            +
+   <xsd:sequence>                                                                            +
+     <xsd:element name="a" type="INTEGER" minOccurs="0"></xsd:element>                       +
+     <xsd:element name="b" type="UDT.regression.pg_catalog.text" minOccurs="0"></xsd:element>+
+   </xsd:sequence>                                                                           +
+ </xsd:complexType>                                                                          +
+                                                                                             +
+ <xsd:element name="row" type="RowType"/>                                                    +
+                                                                                             +
+ </xsd:schema>
+(1 row)
+
+MOVE BACKWARD ALL IN xc;
 SELECT cursor_to_xml('xc'::refcursor, 5, true, false, '');
- cursor_to_xml 
----------------
- <row>        +
-   <a>1</a>   +
-   <b>one</b> +
- </row>       +
-              +
- <row>        +
-   <a>2</a>   +
-   <b>two</b> +
- </row>       +
-              +
+                         cursor_to_xml                         
+---------------------------------------------------------------
+ <table xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">+
+                                                              +
+ <row>                                                        +
+   <a>-1</a>                                                  +
+   <b xsi:nil="true"/>                                        +
+ </row>                                                       +
+                                                              +
+ <row>                                                        +
+   <a>1</a>                                                   +
+   <b>one</b>                                                 +
+ </row>                                                       +
+                                                              +
+ <row>                                                        +
+   <a>2</a>                                                   +
+   <b>two</b>                                                 +
+ </row>                                                       +
+                                                              +
+ </table>                                                     +
  
 (1 row)
 
index d67ef6d3e24c3068b8c335156577c3340ba517ac..f6dbf81666aa213d7df92dfab311c12fef0b712d 100644 (file)
@@ -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.
index 8f8d680813975b04d324ad930af9f955f0a8a669..fde1b9eb597f2d94d9122f5db237f4fb0f057c31 100644 (file)
@@ -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, '');