PostgreSQL Source Code git master
Loading...
Searching...
No Matches
xpath.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "executor/spi.h"
#include "fmgr.h"
#include "funcapi.h"
#include "lib/stringinfo.h"
#include "utils/builtins.h"
#include "utils/tuplestore.h"
#include "utils/xml.h"
#include <libxml/xpath.h>
#include <libxml/tree.h>
#include <libxml/xmlmemory.h>
#include <libxml/xmlerror.h>
#include <libxml/parserInternals.h>
Include dependency graph for xpath.c:

Go to the source code of this file.

Data Structures

struct  xpath_workspace
 

Functions

 PG_MODULE_MAGIC_EXT (.name="xml2",.version=PG_VERSION)
 
PgXmlErrorContextpgxml_parser_init (PgXmlStrictness strictness)
 
static xmlCharpgxmlNodeSetToText (xmlNodeSetPtr nodeset, xmlChar *toptagname, xmlChar *septagname, xmlChar *plainsep)
 
static textpgxml_result_to_text (xmlXPathObjectPtr res, xmlChar *toptag, xmlChar *septag, xmlChar *plainsep)
 
static xmlCharpgxml_texttoxmlchar (text *textstring)
 
static xpath_workspacepgxml_xpath (text *document, xmlChar *xpath, PgXmlErrorContext *xmlerrcxt)
 
static void cleanup_workspace (xpath_workspace *workspace)
 
 PG_FUNCTION_INFO_V1 (xml_encode_special_chars)
 
Datum xml_encode_special_chars (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (xpath_nodeset)
 
Datum xpath_nodeset (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (xpath_list)
 
Datum xpath_list (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (xpath_string)
 
Datum xpath_string (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (xpath_number)
 
Datum xpath_number (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (xpath_bool)
 
Datum xpath_bool (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (xpath_table)
 
Datum xpath_table (PG_FUNCTION_ARGS)
 

Function Documentation

◆ cleanup_workspace()

static void cleanup_workspace ( xpath_workspace workspace)
static

Definition at line 564 of file xpath.c.

565{
566 if (workspace->res)
567 xmlXPathFreeObject(workspace->res);
568 workspace->res = NULL;
569 if (workspace->ctxt)
570 xmlXPathFreeContext(workspace->ctxt);
571 workspace->ctxt = NULL;
572 if (workspace->doctree)
573 xmlFreeDoc(workspace->doctree);
574 workspace->doctree = NULL;
575}
static int fb(int x)
xmlXPathContextPtr ctxt
Definition xpath.c:40
xmlDocPtr doctree
Definition xpath.c:39
xmlXPathObjectPtr res
Definition xpath.c:41

References xpath_workspace::ctxt, xpath_workspace::doctree, fb(), and xpath_workspace::res.

Referenced by pgxml_xpath(), xpath_bool(), xpath_list(), xpath_nodeset(), xpath_number(), and xpath_string().

◆ PG_FUNCTION_INFO_V1() [1/7]

PG_FUNCTION_INFO_V1 ( xml_encode_special_chars  )

◆ PG_FUNCTION_INFO_V1() [2/7]

PG_FUNCTION_INFO_V1 ( xpath_bool  )

◆ PG_FUNCTION_INFO_V1() [3/7]

PG_FUNCTION_INFO_V1 ( xpath_list  )

◆ PG_FUNCTION_INFO_V1() [4/7]

PG_FUNCTION_INFO_V1 ( xpath_nodeset  )

◆ PG_FUNCTION_INFO_V1() [5/7]

PG_FUNCTION_INFO_V1 ( xpath_number  )

◆ PG_FUNCTION_INFO_V1() [6/7]

PG_FUNCTION_INFO_V1 ( xpath_string  )

◆ PG_FUNCTION_INFO_V1() [7/7]

PG_FUNCTION_INFO_V1 ( xpath_table  )

◆ PG_MODULE_MAGIC_EXT()

PG_MODULE_MAGIC_EXT ( name = "xml2",
version = PG_VERSION 
)

◆ pgxml_parser_init()

PgXmlErrorContext * pgxml_parser_init ( PgXmlStrictness  strictness)

Definition at line 68 of file xpath.c.

69{
71
72 /* Set up error handling (we share the core's error handler) */
74
75 /* Note: we're assuming an elog cannot be thrown by the following calls */
76
77 /* Initialize libxml */
79
80 return xmlerrcxt;
81}
struct PgXmlErrorContext PgXmlErrorContext
Definition xml.h:48
PgXmlErrorContext * pg_xml_init(PgXmlStrictness strictness)

References fb(), and pg_xml_init().

Referenced by xpath_bool(), xpath_list(), xpath_nodeset(), xpath_number(), xpath_string(), xpath_table(), and xslt_process().

◆ pgxml_result_to_text()

static text * pgxml_result_to_text ( xmlXPathObjectPtr  res,
xmlChar toptag,
xmlChar septag,
xmlChar plainsep 
)
static

Definition at line 578 of file xpath.c.

582{
583 xmlChar *volatile xpresstr = NULL;
584 text *volatile xpres = NULL;
586
587 if (res == NULL)
588 return NULL;
589
590 /* spin some error handling */
592
593 PG_TRY();
594 {
595 switch (res->type)
596 {
597 case XPATH_NODESET:
598 xpresstr = pgxmlNodeSetToText(res->nodesetval,
599 toptag,
601 break;
602
603 case XPATH_STRING:
604 xpresstr = xmlStrdup(res->stringval);
607 "could not allocate result");
608 break;
609
610 default:
611 elog(NOTICE, "unsupported XQuery result: %d", res->type);
612 xpresstr = xmlStrdup((const xmlChar *) "<unsupported/>");
615 "could not allocate result");
616 }
617
618 /* Now convert this result back to text */
619 xpres = cstring_to_text((char *) xpresstr);
620 }
621 PG_CATCH();
622 {
623 if (xpresstr != NULL)
625
626 pg_xml_done(xmlerrcxt, true);
627
628 PG_RE_THROW();
629 }
630 PG_END_TRY();
631
632 /* Free various storage */
634
635 pg_xml_done(xmlerrcxt, false);
636
637 return xpres;
638}
#define PG_RE_THROW()
Definition elog.h:407
#define PG_TRY(...)
Definition elog.h:374
#define PG_END_TRY(...)
Definition elog.h:399
#define ERROR
Definition elog.h:40
#define PG_CATCH(...)
Definition elog.h:384
#define elog(elevel,...)
Definition elog.h:228
#define NOTICE
Definition elog.h:36
Definition c.h:835
text * cstring_to_text(const char *s)
Definition varlena.c:184
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
bool pg_xml_error_occurred(PgXmlErrorContext *errcxt)
void pg_xml_done(PgXmlErrorContext *errcxt, bool isError)
@ PG_XML_STRICTNESS_ALL
Definition xml.h:44
static xmlChar * pgxmlNodeSetToText(xmlNodeSetPtr nodeset, xmlChar *toptagname, xmlChar *septagname, xmlChar *plainsep)
Definition xpath.c:143

References cstring_to_text(), elog, ERROR, fb(), NOTICE, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, pg_xml_done(), pg_xml_error_occurred(), pg_xml_init(), PG_XML_STRICTNESS_ALL, pgxmlNodeSetToText(), and xml_ereport().

Referenced by xpath_list(), xpath_nodeset(), and xpath_string().

◆ pgxml_texttoxmlchar()

static xmlChar * pgxml_texttoxmlchar ( text textstring)
static

Definition at line 266 of file xpath.c.

267{
269}
char * text_to_cstring(const text *t)
Definition varlena.c:217

References fb(), and text_to_cstring().

Referenced by xml_encode_special_chars(), xpath_bool(), xpath_list(), xpath_nodeset(), and xpath_number().

◆ pgxml_xpath()

static xpath_workspace * pgxml_xpath ( text document,
xmlChar xpath,
PgXmlErrorContext xmlerrcxt 
)
static

Definition at line 512 of file xpath.c.

513{
517
518 workspace->doctree = NULL;
519 workspace->ctxt = NULL;
520 workspace->res = NULL;
521
522 PG_TRY();
523 {
524 workspace->doctree = xmlReadMemory((char *) VARDATA_ANY(document),
525 docsize, NULL, NULL,
527 if (workspace->doctree != NULL)
528 {
529 workspace->ctxt = xmlXPathNewContext(workspace->doctree);
530 if (workspace->ctxt == NULL)
532 "could not allocate XPath context");
533
534 workspace->ctxt->node = xmlDocGetRootElement(workspace->doctree);
535
536 /* compile the path */
537 comppath = xmlXPathCtxtCompile(workspace->ctxt, xpath);
540 "XPath Syntax Error");
541
542 /* Now evaluate the path expression. */
543 workspace->res = xmlXPathCompiledEval(comppath, workspace->ctxt);
544
546 comppath = NULL;
547 }
548 }
549 PG_CATCH();
550 {
551 if (comppath != NULL)
553 cleanup_workspace(workspace);
554
555 PG_RE_THROW();
556 }
557 PG_END_TRY();
558
559 return workspace;
560}
int32_t int32
Definition c.h:679
#define palloc0_object(type)
Definition fe_memutils.h:90
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition varatt.h:472
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486
Datum xpath(PG_FUNCTION_ARGS)
Definition xml.c:4572
static void cleanup_workspace(xpath_workspace *workspace)
Definition xpath.c:564

References cleanup_workspace(), xpath_workspace::ctxt, xpath_workspace::doctree, ERROR, fb(), palloc0_object, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, pg_xml_error_occurred(), xpath_workspace::res, VARDATA_ANY(), VARSIZE_ANY_EXHDR(), xml_ereport(), and xpath().

Referenced by xpath_bool(), xpath_list(), xpath_nodeset(), xpath_number(), and xpath_string().

◆ pgxmlNodeSetToText()

static xmlChar * pgxmlNodeSetToText ( xmlNodeSetPtr  nodeset,
xmlChar toptagname,
xmlChar septagname,
xmlChar plainsep 
)
static

Definition at line 143 of file xpath.c.

147{
148 volatile xmlBufferPtr buf = NULL;
149 xmlChar *volatile result = NULL;
150 xmlChar *volatile str = NULL;
152
153 /* spin up some error handling */
155
156 PG_TRY();
157 {
159
162 "could not allocate xmlBuffer");
163
164 if ((toptagname != NULL) && (xmlStrlen(toptagname) > 0))
165 {
169 }
170 if (nodeset != NULL)
171 {
172 for (int i = 0; i < nodeset->nodeNr; i++)
173 {
174 if (plainsep != NULL)
175 {
179 "could not allocate node text");
180
182 xmlFree(str);
183 str = NULL;
184
185 /* If this isn't the last entry, write the plain sep. */
186 if (i < (nodeset->nodeNr) - 1)
188 }
189 else
190 {
191 xmlNodePtr node = nodeset->nodeTab[i];
192
193 if ((septagname != NULL) && (xmlStrlen(septagname) > 0))
194 {
198 }
199
200 /*
201 * XML_NAMESPACE_DECL nodes are xmlNs structs, that cannot
202 * be processed by xmlNodeDump().
203 */
204 if (node->type == XML_NAMESPACE_DECL)
205 {
209 "could not allocate node text");
211 xmlFree(str);
212 str = NULL;
213 }
214 else
215 xmlNodeDump(buf, node->doc, node, 1, 0);
216
217 if ((septagname != NULL) && (xmlStrlen(septagname) > 0))
218 {
219 xmlBufferWriteChar(buf, "</");
222 }
223 }
224 }
225 }
226
227 if ((toptagname != NULL) && (xmlStrlen(toptagname) > 0))
228 {
229 xmlBufferWriteChar(buf, "</");
232 }
233
237 "could not allocate result");
238 }
239 PG_CATCH();
240 {
241 if (result)
243 if (str)
244 xmlFree(str);
245 if (buf)
247
248 pg_xml_done(xmlerrcxt, true);
249
250 PG_RE_THROW();
251 }
252 PG_END_TRY();
253
255 pg_xml_done(xmlerrcxt, false);
256
257 return result;
258}
uint32 result
const char * str
int i
Definition isn.c:77
static char buf[DEFAULT_XLOG_SEG_SIZE]

References buf, ERROR, fb(), i, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, pg_xml_done(), pg_xml_error_occurred(), pg_xml_init(), PG_XML_STRICTNESS_ALL, result, str, and xml_ereport().

Referenced by pgxml_result_to_text().

◆ xml_encode_special_chars()

Datum xml_encode_special_chars ( PG_FUNCTION_ARGS  )

Definition at line 89 of file xpath.c.

90{
92 text *volatile tout = NULL;
93 xmlChar *volatile tt = NULL;
95
97
98 PG_TRY();
99 {
100 xmlChar *ts;
101
103
107 "could not allocate xmlChar");
108 pfree(ts);
109
110 tout = cstring_to_text((char *) tt);
111 }
112 PG_CATCH();
113 {
114 if (tt != NULL)
115 xmlFree(tt);
116
117 pg_xml_done(xmlerrcxt, true);
118
119 PG_RE_THROW();
120 }
121 PG_END_TRY();
122
123 if (tt != NULL)
124 xmlFree(tt);
125
126 pg_xml_done(xmlerrcxt, false);
127
129}
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define PG_RETURN_TEXT_P(x)
Definition fmgr.h:374
void pfree(void *pointer)
Definition mcxt.c:1619
static xmlChar * pgxml_texttoxmlchar(text *textstring)
Definition xpath.c:266

References cstring_to_text(), ERROR, fb(), pfree(), PG_CATCH, PG_END_TRY, PG_GETARG_TEXT_PP, PG_RE_THROW, PG_RETURN_TEXT_P, PG_TRY, pg_xml_done(), pg_xml_error_occurred(), pg_xml_init(), PG_XML_STRICTNESS_ALL, pgxml_texttoxmlchar(), and xml_ereport().

◆ xpath_bool()

Datum xpath_bool ( PG_FUNCTION_ARGS  )

Definition at line 469 of file xpath.c.

470{
472 text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */
473 xmlChar *xpath;
474 volatile int bRes = 0;
475 xpath_workspace *volatile workspace = NULL;
477
480
481 PG_TRY();
482 {
483 workspace = pgxml_xpath(document, xpath, xmlerrcxt);
484 pfree(xpath);
485
486 if (workspace->res == NULL)
487 bRes = 0;
488 else
489 bRes = xmlXPathCastToBoolean(workspace->res);
490 }
491 PG_CATCH();
492 {
493 if (workspace)
494 cleanup_workspace(workspace);
495
496 pg_xml_done(xmlerrcxt, true);
497 PG_RE_THROW();
498 }
499 PG_END_TRY();
500
501 cleanup_workspace(workspace);
502 pg_xml_done(xmlerrcxt, false);
503
505}
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
@ PG_XML_STRICTNESS_LEGACY
Definition xml.h:41
PgXmlErrorContext * pgxml_parser_init(PgXmlStrictness strictness)
Definition xpath.c:68
static xpath_workspace * pgxml_xpath(text *document, xmlChar *xpath, PgXmlErrorContext *xmlerrcxt)
Definition xpath.c:512

References cleanup_workspace(), fb(), pfree(), PG_CATCH, PG_END_TRY, PG_GETARG_TEXT_PP, PG_RE_THROW, PG_RETURN_BOOL, PG_TRY, pg_xml_done(), PG_XML_STRICTNESS_LEGACY, pgxml_parser_init(), pgxml_texttoxmlchar(), pgxml_xpath(), xpath_workspace::res, and xpath().

◆ xpath_list()

Datum xpath_list ( PG_FUNCTION_ARGS  )

Definition at line 326 of file xpath.c.

327{
329 text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */
331 xmlChar *xpath;
332 text *volatile xpres = NULL;
333 xpath_workspace *volatile workspace = NULL;
335
338
339 PG_TRY();
340 {
341 workspace = pgxml_xpath(document, xpath, xmlerrcxt);
343 }
344 PG_CATCH();
345 {
346 if (workspace)
347 cleanup_workspace(workspace);
348
349 pg_xml_done(xmlerrcxt, true);
350 PG_RE_THROW();
351 }
352 PG_END_TRY();
353
354 cleanup_workspace(workspace);
355 pg_xml_done(xmlerrcxt, false);
356
357 pfree(xpath);
358
359 if (xpres == NULL)
362}
#define PG_RETURN_NULL()
Definition fmgr.h:346
static text * pgxml_result_to_text(xmlXPathObjectPtr res, xmlChar *toptag, xmlChar *septag, xmlChar *plainsep)
Definition xpath.c:578

References cleanup_workspace(), fb(), pfree(), PG_CATCH, PG_END_TRY, PG_GETARG_TEXT_PP, PG_RE_THROW, PG_RETURN_NULL, PG_RETURN_TEXT_P, PG_TRY, pg_xml_done(), PG_XML_STRICTNESS_LEGACY, pgxml_parser_init(), pgxml_result_to_text(), pgxml_texttoxmlchar(), pgxml_xpath(), xpath_workspace::res, and xpath().

◆ xpath_nodeset()

◆ xpath_number()

Datum xpath_number ( PG_FUNCTION_ARGS  )

Definition at line 423 of file xpath.c.

424{
426 text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */
427 xmlChar *xpath;
428 volatile float4 fRes = 0.0;
429 volatile bool isNull = false;
430 xpath_workspace *volatile workspace = NULL;
432
435
436 PG_TRY();
437 {
438 workspace = pgxml_xpath(document, xpath, xmlerrcxt);
439 pfree(xpath);
440
441 if (workspace->res == NULL)
442 isNull = true;
443 else
444 fRes = xmlXPathCastToNumber(workspace->res);
445 }
446 PG_CATCH();
447 {
448 if (workspace)
449 cleanup_workspace(workspace);
450
451 pg_xml_done(xmlerrcxt, true);
452 PG_RE_THROW();
453 }
454 PG_END_TRY();
455
456 cleanup_workspace(workspace);
457 pg_xml_done(xmlerrcxt, false);
458
459 if (isNull || xmlXPathIsNaN(fRes))
461
463}
float float4
Definition c.h:772
#define PG_RETURN_FLOAT4(x)
Definition fmgr.h:368

References cleanup_workspace(), fb(), pfree(), PG_CATCH, PG_END_TRY, PG_GETARG_TEXT_PP, PG_RE_THROW, PG_RETURN_FLOAT4, PG_RETURN_NULL, PG_TRY, pg_xml_done(), PG_XML_STRICTNESS_LEGACY, pgxml_parser_init(), pgxml_texttoxmlchar(), pgxml_xpath(), xpath_workspace::res, and xpath().

◆ xpath_string()

Datum xpath_string ( PG_FUNCTION_ARGS  )

Definition at line 368 of file xpath.c.

369{
371 text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */
372 xmlChar *xpath;
374 text *volatile xpres = NULL;
375 xpath_workspace *volatile workspace = NULL;
377
379
380 /*
381 * We encapsulate the supplied path with "string()" = 8 chars + 1 for NUL
382 * at end
383 */
384 /* We could try casting to string using the libxml function? */
385
386 xpath = (xmlChar *) palloc(pathsize + 9);
387 memcpy(xpath, "string(", 7);
389 xpath[pathsize + 7] = ')';
390 xpath[pathsize + 8] = '\0';
391
393
394 PG_TRY();
395 {
396 workspace = pgxml_xpath(document, xpath, xmlerrcxt);
397 xpres = pgxml_result_to_text(workspace->res, NULL, NULL, NULL);
398 }
399 PG_CATCH();
400 {
401 if (workspace)
402 cleanup_workspace(workspace);
403
404 pg_xml_done(xmlerrcxt, true);
405 PG_RE_THROW();
406 }
407 PG_END_TRY();
408
409 cleanup_workspace(workspace);
410 pg_xml_done(xmlerrcxt, false);
411
412 pfree(xpath);
413
414 if (xpres == NULL)
417}
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
void * palloc(Size size)
Definition mcxt.c:1390

References cleanup_workspace(), fb(), memcpy(), palloc(), pfree(), PG_CATCH, PG_END_TRY, PG_GETARG_TEXT_PP, PG_RE_THROW, PG_RETURN_NULL, PG_RETURN_TEXT_P, PG_TRY, pg_xml_done(), PG_XML_STRICTNESS_LEGACY, pgxml_parser_init(), pgxml_result_to_text(), pgxml_xpath(), xpath_workspace::res, VARDATA_ANY(), VARSIZE_ANY_EXHDR(), and xpath().

◆ xpath_table()

Datum xpath_table ( PG_FUNCTION_ARGS  )

Definition at line 647 of file xpath.c.

648{
649 /* Function parameters */
654 char *condition = text_to_cstring(PG_GETARG_TEXT_PP(4));
655
656 /* SPI (input tuple) support */
657 SPITupleTable *tuptable;
660
661
662 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
663 AttInMetadata *attinmeta;
664
665 char **values;
666 xmlChar **xpaths;
667 char *pos;
668 const char *pathsep = "|";
669
670 int numpaths;
671 int ret;
672 uint64 proc;
673 int j;
674 int rownr; /* For issuing multiple rows from one original
675 * document */
676 bool had_values; /* To determine end of nodeset results */
679 volatile xmlDocPtr doctree = NULL;
680 xmlXPathContextPtr volatile ctxt = NULL;
681 xmlXPathObjectPtr volatile res = NULL;
683 xmlChar *volatile resstr = NULL;
684
686
687 /* must have at least one output column (for the pkey) */
688 if (rsinfo->setDesc->natts < 1)
691 errmsg("xpath_table must have at least one output column")));
692
693 /*
694 * At the moment we assume that the returned attributes make sense for the
695 * XPath specified (i.e. we trust the caller). It's not fatal if they get
696 * it wrong - the input function for the column type will raise an error
697 * if the path result can't be converted into the correct binary
698 * representation.
699 */
700
701 attinmeta = TupleDescGetAttInMetadata(rsinfo->setDesc);
702
703 values = (char **) palloc0(rsinfo->setDesc->natts * sizeof(char *));
704 xpaths = (xmlChar **) palloc(rsinfo->setDesc->natts * sizeof(xmlChar *));
705
706 /*
707 * Split XPaths. xpathset is a writable CString.
708 *
709 * Note that we stop splitting once we've done all needed for tupdesc
710 */
711 numpaths = 0;
712 pos = xpathset;
713 while (numpaths < (rsinfo->setDesc->natts - 1))
714 {
715 xpaths[numpaths++] = (xmlChar *) pos;
716 pos = strstr(pos, pathsep);
717 if (pos != NULL)
718 {
719 *pos = '\0';
720 pos++;
721 }
722 else
723 break;
724 }
725
726 /* Now build query */
728
729 /* Build initial sql statement */
730 appendStringInfo(&query_buf, "SELECT %s, %s FROM %s WHERE %s",
731 pkeyfield,
732 xmlfield,
733 relname,
734 condition);
735
736 SPI_connect();
737
738 if ((ret = SPI_exec(query_buf.data, 0)) != SPI_OK_SELECT)
739 elog(ERROR, "xpath_table: SPI execution failed for query %s",
740 query_buf.data);
741
742 proc = SPI_processed;
743 tuptable = SPI_tuptable;
744 spi_tupdesc = tuptable->tupdesc;
745
746 /*
747 * Check that SPI returned correct result. If you put a comma into one of
748 * the function parameters, this will catch it when the SPI query returns
749 * e.g. 3 columns.
750 */
751 if (spi_tupdesc->natts != 2)
752 {
754 errmsg("expression returning multiple columns is not valid in parameter list"),
755 errdetail("Expected two columns in SPI result, got %d.", spi_tupdesc->natts)));
756 }
757
758 /*
759 * Setup the parser. This should happen after we are done evaluating the
760 * query, in case it calls functions that set up libxml differently.
761 */
763
764 PG_TRY();
765 {
766 /* For each row i.e. document returned from SPI */
767 uint64 i;
768
769 for (i = 0; i < proc; i++)
770 {
771 char *pkey;
772 char *xmldoc;
774
775 /* Extract the row data as C Strings */
776 spi_tuple = tuptable->vals[i];
779
780 /*
781 * Clear the values array, so that not-well-formed documents
782 * return NULL in all columns. Note that this also means that
783 * spare columns will be NULL.
784 */
785 for (j = 0; j < rsinfo->setDesc->natts; j++)
786 values[j] = NULL;
787
788 /* Insert primary key */
789 values[0] = pkey;
790
791 /* Parse the document */
792 if (xmldoc)
793 doctree = xmlReadMemory(xmldoc, strlen(xmldoc),
794 NULL, NULL,
796 else /* treat NULL as not well-formed */
797 doctree = NULL;
798
799 if (doctree == NULL)
800 {
801 /* not well-formed, so output all-NULL tuple */
805 }
806 else
807 {
808 /* New loop here - we have to deal with nodeset results */
809 rownr = 0;
810
811 do
812 {
813 /* Now evaluate the set of xpaths. */
814 had_values = false;
815 for (j = 0; j < numpaths; j++)
816 {
817 ctxt = NULL;
818 res = NULL;
819 comppath = NULL;
820 resstr = NULL;
821
822 ctxt = xmlXPathNewContext(doctree);
823 if (ctxt == NULL || pg_xml_error_occurred(xmlerrcxt))
826 "could not allocate XPath context");
827
828 ctxt->node = xmlDocGetRootElement(doctree);
829
830 /* compile the path */
835 "XPath Syntax Error");
836
837 /* Now evaluate the path expression. */
838 res = xmlXPathCompiledEval(comppath, ctxt);
840 comppath = NULL;
841
842 if (res != NULL)
843 {
844 switch (res->type)
845 {
846 case XPATH_NODESET:
847 /* We see if this nodeset has enough nodes */
848 if (res->nodesetval != NULL &&
850 {
851 resstr = xmlXPathCastNodeToString(res->nodesetval->nodeTab[rownr]);
855 "could not allocate result");
856 had_values = true;
857 }
858 else
859 resstr = NULL;
860
861 break;
862
863 case XPATH_STRING:
864 resstr = xmlStrdup(res->stringval);
868 "could not allocate result");
869 break;
870
871 default:
872 elog(NOTICE, "unsupported XQuery result: %d", res->type);
873 resstr = xmlStrdup((const xmlChar *) "<unsupported/>");
877 "could not allocate result");
878 }
879
880 /*
881 * Insert this into the appropriate column in the
882 * result tuple.
883 */
884 values[j + 1] = (char *) resstr;
885 resstr = NULL;
886 }
887
888 if (res != NULL)
889 {
891 res = NULL;
892 }
894 ctxt = NULL;
895 }
896
897 /* Now add the tuple to the output, if there is one. */
898 if (had_values)
899 {
903 }
904
905 /* BuildTupleFromCStrings() has copied the values. */
906 for (j = 1; j < rsinfo->setDesc->natts; j++)
907 {
908 if (values[j] != NULL)
909 {
910 xmlFree((xmlChar *) values[j]);
911 values[j] = NULL;
912 }
913 }
914
915 rownr++;
916 } while (had_values);
917 }
918
919 if (doctree != NULL)
920 xmlFreeDoc(doctree);
921 doctree = NULL;
922
923 if (pkey)
924 pfree(pkey);
925 if (xmldoc)
926 pfree(xmldoc);
927 }
928 }
929 PG_CATCH();
930 {
931 if (resstr != NULL)
933 for (j = 1; j < rsinfo->setDesc->natts; j++)
934 {
935 if (values[j] != NULL)
936 xmlFree((xmlChar *) values[j]);
937 }
938 if (res != NULL)
940 if (comppath != NULL)
942 if (ctxt != NULL)
944 if (doctree != NULL)
945 xmlFreeDoc(doctree);
946
947 pg_xml_done(xmlerrcxt, true);
948
949 PG_RE_THROW();
950 }
951 PG_END_TRY();
952
953 if (doctree != NULL)
954 xmlFreeDoc(doctree);
955
956 pg_xml_done(xmlerrcxt, false);
957
958 SPI_finish();
959
960 /*
961 * SFRM_Materialize mode expects us to return a NULL Datum. The actual
962 * tuples are in our tuplestore and passed back through rsinfo->setResult.
963 * rsinfo->setDesc is set to the tuple description that we actually used
964 * to build our tuples with, so the caller can verify we did what it was
965 * expecting.
966 */
967 return (Datum) 0;
968}
static Datum values[MAXATTR]
Definition bootstrap.c:190
uint64_t uint64
Definition c.h:684
int errcode(int sqlerrcode)
Definition elog.c:875
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define ereport(elevel,...)
Definition elog.h:152
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
void InitMaterializedSRF(FunctionCallInfo fcinfo, uint32 flags)
Definition funcapi.c:76
#define MAT_SRF_USE_EXPECTED_DESC
Definition funcapi.h:296
void heap_freetuple(HeapTuple htup)
Definition heaptuple.c:1372
int j
Definition isn.c:78
void * palloc0(Size size)
Definition mcxt.c:1420
static char * errmsg
NameData relname
Definition pg_class.h:40
uint64_t Datum
Definition postgres.h:70
uint64 SPI_processed
Definition spi.c:45
SPITupleTable * SPI_tuptable
Definition spi.c:46
int SPI_connect(void)
Definition spi.c:95
int SPI_finish(void)
Definition spi.c:183
int SPI_exec(const char *src, long tcount)
Definition spi.c:631
char * SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
Definition spi.c:1221
#define SPI_OK_SELECT
Definition spi.h:86
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition stringinfo.c:145
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
TupleDesc tupdesc
Definition spi.h:25
void tuplestore_puttuple(Tuplestorestate *state, HeapTuple tuple)
Definition tuplestore.c:765

References appendStringInfo(), BuildTupleFromCStrings(), elog, ereport, errcode(), errdetail(), errmsg, ERROR, fb(), heap_freetuple(), i, InitMaterializedSRF(), initStringInfo(), j, MAT_SRF_USE_EXPECTED_DESC, NOTICE, palloc(), palloc0(), pfree(), PG_CATCH, PG_END_TRY, PG_GETARG_TEXT_PP, PG_RE_THROW, PG_TRY, pg_xml_done(), pg_xml_error_occurred(), PG_XML_STRICTNESS_LEGACY, pgxml_parser_init(), relname, SPI_connect(), SPI_exec(), SPI_finish(), SPI_getvalue(), SPI_OK_SELECT, SPI_processed, SPI_tuptable, text_to_cstring(), SPITupleTable::tupdesc, TupleDescGetAttInMetadata(), tuplestore_puttuple(), SPITupleTable::vals, values, and xml_ereport().