Line data Source code
1 : /*
2 : * contrib/xml2/xpath.c
3 : *
4 : * Parser interface for DOM-based parser (libxml) rather than
5 : * stream-based SAX-type parser
6 : */
7 : #include "postgres.h"
8 :
9 : #include "access/htup_details.h"
10 : #include "executor/spi.h"
11 : #include "fmgr.h"
12 : #include "funcapi.h"
13 : #include "lib/stringinfo.h"
14 : #include "utils/builtins.h"
15 : #include "utils/xml.h"
16 :
17 : /* libxml includes */
18 :
19 : #include <libxml/xpath.h>
20 : #include <libxml/tree.h>
21 : #include <libxml/xmlmemory.h>
22 : #include <libxml/xmlerror.h>
23 : #include <libxml/parserInternals.h>
24 :
25 2 : PG_MODULE_MAGIC_EXT(
26 : .name = "xml2",
27 : .version = PG_VERSION
28 : );
29 :
30 : /* exported for use by xslt_proc.c */
31 :
32 : PgXmlErrorContext *pgxml_parser_init(PgXmlStrictness strictness);
33 :
34 : /* workspace for pgxml_xpath() */
35 :
36 : typedef struct
37 : {
38 : xmlDocPtr doctree;
39 : xmlXPathContextPtr ctxt;
40 : xmlXPathObjectPtr res;
41 : } xpath_workspace;
42 :
43 : /* local declarations */
44 :
45 : static xmlChar *pgxmlNodeSetToText(xmlNodeSetPtr nodeset,
46 : xmlChar *toptagname, xmlChar *septagname,
47 : xmlChar *plainsep);
48 :
49 : static text *pgxml_result_to_text(xmlXPathObjectPtr res, xmlChar *toptag,
50 : xmlChar *septag, xmlChar *plainsep);
51 :
52 : static xmlChar *pgxml_texttoxmlchar(text *textstring);
53 :
54 : static xmlXPathObjectPtr pgxml_xpath(text *document, xmlChar *xpath,
55 : xpath_workspace *workspace);
56 :
57 : static void cleanup_workspace(xpath_workspace *workspace);
58 :
59 :
60 : /*
61 : * Initialize for xml parsing.
62 : *
63 : * As with the underlying pg_xml_init function, calls to this MUST be followed
64 : * by a PG_TRY block that guarantees that pg_xml_done is called.
65 : */
66 : PgXmlErrorContext *
67 22 : pgxml_parser_init(PgXmlStrictness strictness)
68 : {
69 : PgXmlErrorContext *xmlerrcxt;
70 :
71 : /* Set up error handling (we share the core's error handler) */
72 22 : xmlerrcxt = pg_xml_init(strictness);
73 :
74 : /* Note: we're assuming an elog cannot be thrown by the following calls */
75 :
76 : /* Initialize libxml */
77 22 : xmlInitParser();
78 :
79 22 : return xmlerrcxt;
80 : }
81 :
82 :
83 : /* Encodes special characters (<, >, &, " and \r) as XML entities */
84 :
85 2 : PG_FUNCTION_INFO_V1(xml_encode_special_chars);
86 :
87 : Datum
88 0 : xml_encode_special_chars(PG_FUNCTION_ARGS)
89 : {
90 0 : text *tin = PG_GETARG_TEXT_PP(0);
91 : text *tout;
92 : xmlChar *ts,
93 : *tt;
94 :
95 0 : ts = pgxml_texttoxmlchar(tin);
96 :
97 0 : tt = xmlEncodeSpecialChars(NULL, ts);
98 :
99 0 : pfree(ts);
100 :
101 0 : tout = cstring_to_text((char *) tt);
102 :
103 0 : xmlFree(tt);
104 :
105 0 : PG_RETURN_TEXT_P(tout);
106 : }
107 :
108 : /*
109 : * Function translates a nodeset into a text representation
110 : *
111 : * iterates over each node in the set and calls xmlNodeDump to write it to
112 : * an xmlBuffer -from which an xmlChar * string is returned.
113 : *
114 : * each representation is surrounded by <tagname> ... </tagname>
115 : *
116 : * plainsep is an ordinary (not tag) separator - if used, then nodes are
117 : * cast to string as output method
118 : */
119 : static xmlChar *
120 10 : pgxmlNodeSetToText(xmlNodeSetPtr nodeset,
121 : xmlChar *toptagname,
122 : xmlChar *septagname,
123 : xmlChar *plainsep)
124 : {
125 : xmlBufferPtr buf;
126 : xmlChar *result;
127 : int i;
128 :
129 10 : buf = xmlBufferCreate();
130 :
131 10 : if ((toptagname != NULL) && (xmlStrlen(toptagname) > 0))
132 : {
133 2 : xmlBufferWriteChar(buf, "<");
134 2 : xmlBufferWriteCHAR(buf, toptagname);
135 2 : xmlBufferWriteChar(buf, ">");
136 : }
137 10 : if (nodeset != NULL)
138 : {
139 30 : for (i = 0; i < nodeset->nodeNr; i++)
140 : {
141 20 : if (plainsep != NULL)
142 : {
143 8 : xmlBufferWriteCHAR(buf,
144 8 : xmlXPathCastNodeToString(nodeset->nodeTab[i]));
145 :
146 : /* If this isn't the last entry, write the plain sep. */
147 8 : if (i < (nodeset->nodeNr) - 1)
148 4 : xmlBufferWriteChar(buf, (char *) plainsep);
149 : }
150 : else
151 : {
152 12 : if ((septagname != NULL) && (xmlStrlen(septagname) > 0))
153 : {
154 8 : xmlBufferWriteChar(buf, "<");
155 8 : xmlBufferWriteCHAR(buf, septagname);
156 8 : xmlBufferWriteChar(buf, ">");
157 : }
158 12 : xmlNodeDump(buf,
159 12 : nodeset->nodeTab[i]->doc,
160 12 : nodeset->nodeTab[i],
161 : 1, 0);
162 :
163 12 : if ((septagname != NULL) && (xmlStrlen(septagname) > 0))
164 : {
165 8 : xmlBufferWriteChar(buf, "</");
166 8 : xmlBufferWriteCHAR(buf, septagname);
167 8 : xmlBufferWriteChar(buf, ">");
168 : }
169 : }
170 : }
171 : }
172 :
173 10 : if ((toptagname != NULL) && (xmlStrlen(toptagname) > 0))
174 : {
175 2 : xmlBufferWriteChar(buf, "</");
176 2 : xmlBufferWriteCHAR(buf, toptagname);
177 2 : xmlBufferWriteChar(buf, ">");
178 : }
179 10 : result = xmlStrdup(buf->content);
180 10 : xmlBufferFree(buf);
181 10 : return result;
182 : }
183 :
184 :
185 : /* Translate a PostgreSQL "varlena" -i.e. a variable length parameter
186 : * into the libxml2 representation
187 : */
188 : static xmlChar *
189 26 : pgxml_texttoxmlchar(text *textstring)
190 : {
191 26 : return (xmlChar *) text_to_cstring(textstring);
192 : }
193 :
194 : /* Publicly visible XPath functions */
195 :
196 : /*
197 : * This is a "raw" xpath function. Check that it returns child elements
198 : * properly
199 : */
200 4 : PG_FUNCTION_INFO_V1(xpath_nodeset);
201 :
202 : Datum
203 6 : xpath_nodeset(PG_FUNCTION_ARGS)
204 : {
205 6 : text *document = PG_GETARG_TEXT_PP(0);
206 6 : text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */
207 6 : xmlChar *toptag = pgxml_texttoxmlchar(PG_GETARG_TEXT_PP(2));
208 6 : xmlChar *septag = pgxml_texttoxmlchar(PG_GETARG_TEXT_PP(3));
209 : xmlChar *xpath;
210 : text *xpres;
211 : xmlXPathObjectPtr res;
212 : xpath_workspace workspace;
213 :
214 6 : xpath = pgxml_texttoxmlchar(xpathsupp);
215 :
216 6 : res = pgxml_xpath(document, xpath, &workspace);
217 :
218 6 : xpres = pgxml_result_to_text(res, toptag, septag, NULL);
219 :
220 6 : cleanup_workspace(&workspace);
221 :
222 6 : pfree(xpath);
223 :
224 6 : if (xpres == NULL)
225 0 : PG_RETURN_NULL();
226 6 : PG_RETURN_TEXT_P(xpres);
227 : }
228 :
229 : /*
230 : * The following function is almost identical, but returns the elements in
231 : * a list.
232 : */
233 4 : PG_FUNCTION_INFO_V1(xpath_list);
234 :
235 : Datum
236 4 : xpath_list(PG_FUNCTION_ARGS)
237 : {
238 4 : text *document = PG_GETARG_TEXT_PP(0);
239 4 : text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */
240 4 : xmlChar *plainsep = pgxml_texttoxmlchar(PG_GETARG_TEXT_PP(2));
241 : xmlChar *xpath;
242 : text *xpres;
243 : xmlXPathObjectPtr res;
244 : xpath_workspace workspace;
245 :
246 4 : xpath = pgxml_texttoxmlchar(xpathsupp);
247 :
248 4 : res = pgxml_xpath(document, xpath, &workspace);
249 :
250 4 : xpres = pgxml_result_to_text(res, NULL, NULL, plainsep);
251 :
252 4 : cleanup_workspace(&workspace);
253 :
254 4 : pfree(xpath);
255 :
256 4 : if (xpres == NULL)
257 0 : PG_RETURN_NULL();
258 4 : PG_RETURN_TEXT_P(xpres);
259 : }
260 :
261 :
262 4 : PG_FUNCTION_INFO_V1(xpath_string);
263 :
264 : Datum
265 2 : xpath_string(PG_FUNCTION_ARGS)
266 : {
267 2 : text *document = PG_GETARG_TEXT_PP(0);
268 2 : text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */
269 : xmlChar *xpath;
270 : int32 pathsize;
271 : text *xpres;
272 : xmlXPathObjectPtr res;
273 : xpath_workspace workspace;
274 :
275 2 : pathsize = VARSIZE_ANY_EXHDR(xpathsupp);
276 :
277 : /*
278 : * We encapsulate the supplied path with "string()" = 8 chars + 1 for NUL
279 : * at end
280 : */
281 : /* We could try casting to string using the libxml function? */
282 :
283 2 : xpath = (xmlChar *) palloc(pathsize + 9);
284 2 : memcpy(xpath, "string(", 7);
285 2 : memcpy(xpath + 7, VARDATA_ANY(xpathsupp), pathsize);
286 2 : xpath[pathsize + 7] = ')';
287 2 : xpath[pathsize + 8] = '\0';
288 :
289 2 : res = pgxml_xpath(document, xpath, &workspace);
290 :
291 2 : xpres = pgxml_result_to_text(res, NULL, NULL, NULL);
292 :
293 2 : cleanup_workspace(&workspace);
294 :
295 2 : pfree(xpath);
296 :
297 2 : if (xpres == NULL)
298 2 : PG_RETURN_NULL();
299 0 : PG_RETURN_TEXT_P(xpres);
300 : }
301 :
302 :
303 2 : PG_FUNCTION_INFO_V1(xpath_number);
304 :
305 : Datum
306 0 : xpath_number(PG_FUNCTION_ARGS)
307 : {
308 0 : text *document = PG_GETARG_TEXT_PP(0);
309 0 : text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */
310 : xmlChar *xpath;
311 : float4 fRes;
312 : xmlXPathObjectPtr res;
313 : xpath_workspace workspace;
314 :
315 0 : xpath = pgxml_texttoxmlchar(xpathsupp);
316 :
317 0 : res = pgxml_xpath(document, xpath, &workspace);
318 :
319 0 : pfree(xpath);
320 :
321 0 : if (res == NULL)
322 0 : PG_RETURN_NULL();
323 :
324 0 : fRes = xmlXPathCastToNumber(res);
325 :
326 0 : cleanup_workspace(&workspace);
327 :
328 0 : if (xmlXPathIsNaN(fRes))
329 0 : PG_RETURN_NULL();
330 :
331 0 : PG_RETURN_FLOAT4(fRes);
332 : }
333 :
334 :
335 2 : PG_FUNCTION_INFO_V1(xpath_bool);
336 :
337 : Datum
338 0 : xpath_bool(PG_FUNCTION_ARGS)
339 : {
340 0 : text *document = PG_GETARG_TEXT_PP(0);
341 0 : text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */
342 : xmlChar *xpath;
343 : int bRes;
344 : xmlXPathObjectPtr res;
345 : xpath_workspace workspace;
346 :
347 0 : xpath = pgxml_texttoxmlchar(xpathsupp);
348 :
349 0 : res = pgxml_xpath(document, xpath, &workspace);
350 :
351 0 : pfree(xpath);
352 :
353 0 : if (res == NULL)
354 0 : PG_RETURN_BOOL(false);
355 :
356 0 : bRes = xmlXPathCastToBoolean(res);
357 :
358 0 : cleanup_workspace(&workspace);
359 :
360 0 : PG_RETURN_BOOL(bRes);
361 : }
362 :
363 :
364 :
365 : /* Core function to evaluate XPath query */
366 :
367 : static xmlXPathObjectPtr
368 12 : pgxml_xpath(text *document, xmlChar *xpath, xpath_workspace *workspace)
369 : {
370 12 : int32 docsize = VARSIZE_ANY_EXHDR(document);
371 : PgXmlErrorContext *xmlerrcxt;
372 : xmlXPathCompExprPtr comppath;
373 :
374 12 : workspace->doctree = NULL;
375 12 : workspace->ctxt = NULL;
376 12 : workspace->res = NULL;
377 :
378 12 : xmlerrcxt = pgxml_parser_init(PG_XML_STRICTNESS_LEGACY);
379 :
380 12 : PG_TRY();
381 : {
382 12 : workspace->doctree = xmlReadMemory((char *) VARDATA_ANY(document),
383 : docsize, NULL, NULL,
384 : XML_PARSE_NOENT);
385 12 : if (workspace->doctree != NULL)
386 : {
387 10 : workspace->ctxt = xmlXPathNewContext(workspace->doctree);
388 10 : workspace->ctxt->node = xmlDocGetRootElement(workspace->doctree);
389 :
390 : /* compile the path */
391 10 : comppath = xmlXPathCtxtCompile(workspace->ctxt, xpath);
392 10 : if (comppath == NULL)
393 0 : xml_ereport(xmlerrcxt, ERROR, ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
394 : "XPath Syntax Error");
395 :
396 : /* Now evaluate the path expression. */
397 10 : workspace->res = xmlXPathCompiledEval(comppath, workspace->ctxt);
398 :
399 10 : xmlXPathFreeCompExpr(comppath);
400 : }
401 : }
402 0 : PG_CATCH();
403 : {
404 0 : cleanup_workspace(workspace);
405 :
406 0 : pg_xml_done(xmlerrcxt, true);
407 :
408 0 : PG_RE_THROW();
409 : }
410 12 : PG_END_TRY();
411 :
412 12 : if (workspace->res == NULL)
413 2 : cleanup_workspace(workspace);
414 :
415 12 : pg_xml_done(xmlerrcxt, false);
416 :
417 12 : return workspace->res;
418 : }
419 :
420 : /* Clean up after processing the result of pgxml_xpath() */
421 : static void
422 14 : cleanup_workspace(xpath_workspace *workspace)
423 : {
424 14 : if (workspace->res)
425 10 : xmlXPathFreeObject(workspace->res);
426 14 : workspace->res = NULL;
427 14 : if (workspace->ctxt)
428 10 : xmlXPathFreeContext(workspace->ctxt);
429 14 : workspace->ctxt = NULL;
430 14 : if (workspace->doctree)
431 10 : xmlFreeDoc(workspace->doctree);
432 14 : workspace->doctree = NULL;
433 14 : }
434 :
435 : static text *
436 12 : pgxml_result_to_text(xmlXPathObjectPtr res,
437 : xmlChar *toptag,
438 : xmlChar *septag,
439 : xmlChar *plainsep)
440 : {
441 : xmlChar *xpresstr;
442 : text *xpres;
443 :
444 12 : if (res == NULL)
445 2 : return NULL;
446 :
447 10 : switch (res->type)
448 : {
449 10 : case XPATH_NODESET:
450 10 : xpresstr = pgxmlNodeSetToText(res->nodesetval,
451 : toptag,
452 : septag, plainsep);
453 10 : break;
454 :
455 0 : case XPATH_STRING:
456 0 : xpresstr = xmlStrdup(res->stringval);
457 0 : break;
458 :
459 0 : default:
460 0 : elog(NOTICE, "unsupported XQuery result: %d", res->type);
461 0 : xpresstr = xmlStrdup((const xmlChar *) "<unsupported/>");
462 : }
463 :
464 : /* Now convert this result back to text */
465 10 : xpres = cstring_to_text((char *) xpresstr);
466 :
467 : /* Free various storage */
468 10 : xmlFree(xpresstr);
469 :
470 10 : return xpres;
471 : }
472 :
473 : /*
474 : * xpath_table is a table function. It needs some tidying (as do the
475 : * other functions here!
476 : */
477 4 : PG_FUNCTION_INFO_V1(xpath_table);
478 :
479 : Datum
480 10 : xpath_table(PG_FUNCTION_ARGS)
481 : {
482 : /* Function parameters */
483 10 : char *pkeyfield = text_to_cstring(PG_GETARG_TEXT_PP(0));
484 10 : char *xmlfield = text_to_cstring(PG_GETARG_TEXT_PP(1));
485 10 : char *relname = text_to_cstring(PG_GETARG_TEXT_PP(2));
486 10 : char *xpathset = text_to_cstring(PG_GETARG_TEXT_PP(3));
487 10 : char *condition = text_to_cstring(PG_GETARG_TEXT_PP(4));
488 :
489 : /* SPI (input tuple) support */
490 : SPITupleTable *tuptable;
491 : HeapTuple spi_tuple;
492 : TupleDesc spi_tupdesc;
493 :
494 :
495 10 : ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
496 : AttInMetadata *attinmeta;
497 :
498 : char **values;
499 : xmlChar **xpaths;
500 : char *pos;
501 10 : const char *pathsep = "|";
502 :
503 : int numpaths;
504 : int ret;
505 : uint64 proc;
506 : int j;
507 : int rownr; /* For issuing multiple rows from one original
508 : * document */
509 : bool had_values; /* To determine end of nodeset results */
510 : StringInfoData query_buf;
511 : PgXmlErrorContext *xmlerrcxt;
512 10 : volatile xmlDocPtr doctree = NULL;
513 :
514 10 : InitMaterializedSRF(fcinfo, MAT_SRF_USE_EXPECTED_DESC);
515 :
516 : /* must have at least one output column (for the pkey) */
517 10 : if (rsinfo->setDesc->natts < 1)
518 0 : ereport(ERROR,
519 : (errcode(ERRCODE_SYNTAX_ERROR),
520 : errmsg("xpath_table must have at least one output column")));
521 :
522 : /*
523 : * At the moment we assume that the returned attributes make sense for the
524 : * XPath specified (i.e. we trust the caller). It's not fatal if they get
525 : * it wrong - the input function for the column type will raise an error
526 : * if the path result can't be converted into the correct binary
527 : * representation.
528 : */
529 :
530 10 : attinmeta = TupleDescGetAttInMetadata(rsinfo->setDesc);
531 :
532 10 : values = (char **) palloc(rsinfo->setDesc->natts * sizeof(char *));
533 10 : xpaths = (xmlChar **) palloc(rsinfo->setDesc->natts * sizeof(xmlChar *));
534 :
535 : /*
536 : * Split XPaths. xpathset is a writable CString.
537 : *
538 : * Note that we stop splitting once we've done all needed for tupdesc
539 : */
540 10 : numpaths = 0;
541 10 : pos = xpathset;
542 14 : while (numpaths < (rsinfo->setDesc->natts - 1))
543 : {
544 10 : xpaths[numpaths++] = (xmlChar *) pos;
545 10 : pos = strstr(pos, pathsep);
546 10 : if (pos != NULL)
547 : {
548 4 : *pos = '\0';
549 4 : pos++;
550 : }
551 : else
552 6 : break;
553 : }
554 :
555 : /* Now build query */
556 10 : initStringInfo(&query_buf);
557 :
558 : /* Build initial sql statement */
559 10 : appendStringInfo(&query_buf, "SELECT %s, %s FROM %s WHERE %s",
560 : pkeyfield,
561 : xmlfield,
562 : relname,
563 : condition);
564 :
565 10 : SPI_connect();
566 :
567 10 : if ((ret = SPI_exec(query_buf.data, 0)) != SPI_OK_SELECT)
568 0 : elog(ERROR, "xpath_table: SPI execution failed for query %s",
569 : query_buf.data);
570 :
571 10 : proc = SPI_processed;
572 10 : tuptable = SPI_tuptable;
573 10 : spi_tupdesc = tuptable->tupdesc;
574 :
575 : /*
576 : * Check that SPI returned correct result. If you put a comma into one of
577 : * the function parameters, this will catch it when the SPI query returns
578 : * e.g. 3 columns.
579 : */
580 10 : if (spi_tupdesc->natts != 2)
581 : {
582 0 : ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
583 : errmsg("expression returning multiple columns is not valid in parameter list"),
584 : errdetail("Expected two columns in SPI result, got %d.", spi_tupdesc->natts)));
585 : }
586 :
587 : /*
588 : * Setup the parser. This should happen after we are done evaluating the
589 : * query, in case it calls functions that set up libxml differently.
590 : */
591 10 : xmlerrcxt = pgxml_parser_init(PG_XML_STRICTNESS_LEGACY);
592 :
593 10 : PG_TRY();
594 : {
595 : /* For each row i.e. document returned from SPI */
596 : uint64 i;
597 :
598 20 : for (i = 0; i < proc; i++)
599 : {
600 : char *pkey;
601 : char *xmldoc;
602 : xmlXPathContextPtr ctxt;
603 : xmlXPathObjectPtr res;
604 : xmlChar *resstr;
605 : xmlXPathCompExprPtr comppath;
606 : HeapTuple ret_tuple;
607 :
608 : /* Extract the row data as C Strings */
609 10 : spi_tuple = tuptable->vals[i];
610 10 : pkey = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
611 10 : xmldoc = SPI_getvalue(spi_tuple, spi_tupdesc, 2);
612 :
613 : /*
614 : * Clear the values array, so that not-well-formed documents
615 : * return NULL in all columns. Note that this also means that
616 : * spare columns will be NULL.
617 : */
618 30 : for (j = 0; j < rsinfo->setDesc->natts; j++)
619 20 : values[j] = NULL;
620 :
621 : /* Insert primary key */
622 10 : values[0] = pkey;
623 :
624 : /* Parse the document */
625 10 : if (xmldoc)
626 10 : doctree = xmlReadMemory(xmldoc, strlen(xmldoc),
627 : NULL, NULL,
628 : XML_PARSE_NOENT);
629 : else /* treat NULL as not well-formed */
630 0 : doctree = NULL;
631 :
632 10 : if (doctree == NULL)
633 : {
634 : /* not well-formed, so output all-NULL tuple */
635 0 : ret_tuple = BuildTupleFromCStrings(attinmeta, values);
636 0 : tuplestore_puttuple(rsinfo->setResult, ret_tuple);
637 0 : heap_freetuple(ret_tuple);
638 : }
639 : else
640 : {
641 : /* New loop here - we have to deal with nodeset results */
642 10 : rownr = 0;
643 :
644 : do
645 : {
646 : /* Now evaluate the set of xpaths. */
647 16 : had_values = false;
648 36 : for (j = 0; j < numpaths; j++)
649 : {
650 20 : ctxt = xmlXPathNewContext(doctree);
651 20 : ctxt->node = xmlDocGetRootElement(doctree);
652 :
653 : /* compile the path */
654 20 : comppath = xmlXPathCtxtCompile(ctxt, xpaths[j]);
655 20 : if (comppath == NULL)
656 0 : xml_ereport(xmlerrcxt, ERROR,
657 : ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
658 : "XPath Syntax Error");
659 :
660 : /* Now evaluate the path expression. */
661 20 : res = xmlXPathCompiledEval(comppath, ctxt);
662 20 : xmlXPathFreeCompExpr(comppath);
663 :
664 20 : if (res != NULL)
665 : {
666 20 : switch (res->type)
667 : {
668 20 : case XPATH_NODESET:
669 : /* We see if this nodeset has enough nodes */
670 20 : if (res->nodesetval != NULL &&
671 20 : rownr < res->nodesetval->nodeNr)
672 : {
673 8 : resstr = xmlXPathCastNodeToString(res->nodesetval->nodeTab[rownr]);
674 8 : had_values = true;
675 : }
676 : else
677 12 : resstr = NULL;
678 :
679 20 : break;
680 :
681 0 : case XPATH_STRING:
682 0 : resstr = xmlStrdup(res->stringval);
683 0 : break;
684 :
685 0 : default:
686 0 : elog(NOTICE, "unsupported XQuery result: %d", res->type);
687 0 : resstr = xmlStrdup((const xmlChar *) "<unsupported/>");
688 : }
689 :
690 : /*
691 : * Insert this into the appropriate column in the
692 : * result tuple.
693 : */
694 20 : values[j + 1] = (char *) resstr;
695 : }
696 20 : xmlXPathFreeContext(ctxt);
697 : }
698 :
699 : /* Now add the tuple to the output, if there is one. */
700 16 : if (had_values)
701 : {
702 6 : ret_tuple = BuildTupleFromCStrings(attinmeta, values);
703 6 : tuplestore_puttuple(rsinfo->setResult, ret_tuple);
704 6 : heap_freetuple(ret_tuple);
705 : }
706 :
707 16 : rownr++;
708 16 : } while (had_values);
709 : }
710 :
711 10 : if (doctree != NULL)
712 10 : xmlFreeDoc(doctree);
713 10 : doctree = NULL;
714 :
715 10 : if (pkey)
716 10 : pfree(pkey);
717 10 : if (xmldoc)
718 10 : pfree(xmldoc);
719 : }
720 : }
721 0 : PG_CATCH();
722 : {
723 0 : if (doctree != NULL)
724 0 : xmlFreeDoc(doctree);
725 :
726 0 : pg_xml_done(xmlerrcxt, true);
727 :
728 0 : PG_RE_THROW();
729 : }
730 10 : PG_END_TRY();
731 :
732 10 : if (doctree != NULL)
733 0 : xmlFreeDoc(doctree);
734 :
735 10 : pg_xml_done(xmlerrcxt, false);
736 :
737 10 : SPI_finish();
738 :
739 : /*
740 : * SFRM_Materialize mode expects us to return a NULL Datum. The actual
741 : * tuples are in our tuplestore and passed back through rsinfo->setResult.
742 : * rsinfo->setDesc is set to the tuple description that we actually used
743 : * to build our tuples with, so the caller can verify we did what it was
744 : * expecting.
745 : */
746 10 : return (Datum) 0;
747 : }
|