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 xpath_workspace *pgxml_xpath(text *document, xmlChar *xpath,
55 : PgXmlErrorContext *xmlerrcxt);
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 0 : text *volatile tout = NULL;
92 0 : xmlChar *volatile tt = NULL;
93 : PgXmlErrorContext *xmlerrcxt;
94 :
95 0 : xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
96 :
97 0 : PG_TRY();
98 : {
99 : xmlChar *ts;
100 :
101 0 : ts = pgxml_texttoxmlchar(tin);
102 :
103 0 : tt = xmlEncodeSpecialChars(NULL, ts);
104 0 : if (tt == NULL || pg_xml_error_occurred(xmlerrcxt))
105 0 : xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
106 : "could not allocate xmlChar");
107 0 : pfree(ts);
108 :
109 0 : tout = cstring_to_text((char *) tt);
110 : }
111 0 : PG_CATCH();
112 : {
113 0 : if (tt != NULL)
114 0 : xmlFree(tt);
115 :
116 0 : pg_xml_done(xmlerrcxt, true);
117 :
118 0 : PG_RE_THROW();
119 : }
120 0 : PG_END_TRY();
121 :
122 0 : if (tt != NULL)
123 0 : xmlFree(tt);
124 :
125 0 : pg_xml_done(xmlerrcxt, false);
126 :
127 0 : PG_RETURN_TEXT_P(tout);
128 : }
129 :
130 : /*
131 : * Function translates a nodeset into a text representation
132 : *
133 : * iterates over each node in the set and calls xmlNodeDump to write it to
134 : * an xmlBuffer -from which an xmlChar * string is returned.
135 : *
136 : * each representation is surrounded by <tagname> ... </tagname>
137 : *
138 : * plainsep is an ordinary (not tag) separator - if used, then nodes are
139 : * cast to string as output method
140 : */
141 : static xmlChar *
142 10 : pgxmlNodeSetToText(xmlNodeSetPtr nodeset,
143 : xmlChar *toptagname,
144 : xmlChar *septagname,
145 : xmlChar *plainsep)
146 : {
147 10 : volatile xmlBufferPtr buf = NULL;
148 10 : xmlChar *volatile result = NULL;
149 : PgXmlErrorContext *xmlerrcxt;
150 :
151 : /* spin up some error handling */
152 10 : xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
153 :
154 10 : PG_TRY();
155 : {
156 10 : buf = xmlBufferCreate();
157 :
158 10 : if (buf == NULL || pg_xml_error_occurred(xmlerrcxt))
159 0 : xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
160 : "could not allocate xmlBuffer");
161 :
162 10 : if ((toptagname != NULL) && (xmlStrlen(toptagname) > 0))
163 : {
164 2 : xmlBufferWriteChar(buf, "<");
165 2 : xmlBufferWriteCHAR(buf, toptagname);
166 2 : xmlBufferWriteChar(buf, ">");
167 : }
168 10 : if (nodeset != NULL)
169 : {
170 30 : for (int i = 0; i < nodeset->nodeNr; i++)
171 : {
172 20 : if (plainsep != NULL)
173 : {
174 8 : xmlBufferWriteCHAR(buf,
175 8 : xmlXPathCastNodeToString(nodeset->nodeTab[i]));
176 :
177 : /* If this isn't the last entry, write the plain sep. */
178 8 : if (i < (nodeset->nodeNr) - 1)
179 4 : xmlBufferWriteChar(buf, (char *) plainsep);
180 : }
181 : else
182 : {
183 12 : if ((septagname != NULL) && (xmlStrlen(septagname) > 0))
184 : {
185 8 : xmlBufferWriteChar(buf, "<");
186 8 : xmlBufferWriteCHAR(buf, septagname);
187 8 : xmlBufferWriteChar(buf, ">");
188 : }
189 12 : xmlNodeDump(buf,
190 12 : nodeset->nodeTab[i]->doc,
191 12 : nodeset->nodeTab[i],
192 : 1, 0);
193 :
194 12 : if ((septagname != NULL) && (xmlStrlen(septagname) > 0))
195 : {
196 8 : xmlBufferWriteChar(buf, "</");
197 8 : xmlBufferWriteCHAR(buf, septagname);
198 8 : xmlBufferWriteChar(buf, ">");
199 : }
200 : }
201 : }
202 : }
203 :
204 10 : if ((toptagname != NULL) && (xmlStrlen(toptagname) > 0))
205 : {
206 2 : xmlBufferWriteChar(buf, "</");
207 2 : xmlBufferWriteCHAR(buf, toptagname);
208 2 : xmlBufferWriteChar(buf, ">");
209 : }
210 :
211 10 : result = xmlStrdup(xmlBufferContent(buf));
212 10 : if (result == NULL || pg_xml_error_occurred(xmlerrcxt))
213 0 : xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
214 : "could not allocate result");
215 : }
216 0 : PG_CATCH();
217 : {
218 0 : if (buf)
219 0 : xmlBufferFree(buf);
220 :
221 0 : pg_xml_done(xmlerrcxt, true);
222 :
223 0 : PG_RE_THROW();
224 : }
225 10 : PG_END_TRY();
226 :
227 10 : xmlBufferFree(buf);
228 10 : pg_xml_done(xmlerrcxt, false);
229 :
230 10 : return result;
231 : }
232 :
233 :
234 : /* Translate a PostgreSQL "varlena" -i.e. a variable length parameter
235 : * into the libxml2 representation
236 : */
237 : static xmlChar *
238 26 : pgxml_texttoxmlchar(text *textstring)
239 : {
240 26 : return (xmlChar *) text_to_cstring(textstring);
241 : }
242 :
243 : /* Publicly visible XPath functions */
244 :
245 : /*
246 : * This is a "raw" xpath function. Check that it returns child elements
247 : * properly
248 : */
249 4 : PG_FUNCTION_INFO_V1(xpath_nodeset);
250 :
251 : Datum
252 6 : xpath_nodeset(PG_FUNCTION_ARGS)
253 : {
254 6 : text *document = PG_GETARG_TEXT_PP(0);
255 6 : text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */
256 6 : xmlChar *toptag = pgxml_texttoxmlchar(PG_GETARG_TEXT_PP(2));
257 6 : xmlChar *septag = pgxml_texttoxmlchar(PG_GETARG_TEXT_PP(3));
258 : xmlChar *xpath;
259 6 : text *volatile xpres = NULL;
260 6 : xpath_workspace *volatile workspace = NULL;
261 : PgXmlErrorContext *xmlerrcxt;
262 :
263 6 : xpath = pgxml_texttoxmlchar(xpathsupp);
264 6 : xmlerrcxt = pgxml_parser_init(PG_XML_STRICTNESS_LEGACY);
265 :
266 6 : PG_TRY();
267 : {
268 6 : workspace = pgxml_xpath(document, xpath, xmlerrcxt);
269 6 : xpres = pgxml_result_to_text(workspace->res, toptag, septag, NULL);
270 : }
271 0 : PG_CATCH();
272 : {
273 0 : if (workspace)
274 0 : cleanup_workspace(workspace);
275 :
276 0 : pg_xml_done(xmlerrcxt, true);
277 0 : PG_RE_THROW();
278 : }
279 6 : PG_END_TRY();
280 :
281 6 : cleanup_workspace(workspace);
282 6 : pg_xml_done(xmlerrcxt, false);
283 :
284 6 : pfree(xpath);
285 :
286 6 : if (xpres == NULL)
287 0 : PG_RETURN_NULL();
288 6 : PG_RETURN_TEXT_P(xpres);
289 : }
290 :
291 : /*
292 : * The following function is almost identical, but returns the elements in
293 : * a list.
294 : */
295 4 : PG_FUNCTION_INFO_V1(xpath_list);
296 :
297 : Datum
298 4 : xpath_list(PG_FUNCTION_ARGS)
299 : {
300 4 : text *document = PG_GETARG_TEXT_PP(0);
301 4 : text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */
302 4 : xmlChar *plainsep = pgxml_texttoxmlchar(PG_GETARG_TEXT_PP(2));
303 : xmlChar *xpath;
304 4 : text *volatile xpres = NULL;
305 4 : xpath_workspace *volatile workspace = NULL;
306 : PgXmlErrorContext *xmlerrcxt;
307 :
308 4 : xpath = pgxml_texttoxmlchar(xpathsupp);
309 4 : xmlerrcxt = pgxml_parser_init(PG_XML_STRICTNESS_LEGACY);
310 :
311 4 : PG_TRY();
312 : {
313 4 : workspace = pgxml_xpath(document, xpath, xmlerrcxt);
314 4 : xpres = pgxml_result_to_text(workspace->res, NULL, NULL, plainsep);
315 : }
316 0 : PG_CATCH();
317 : {
318 0 : if (workspace)
319 0 : cleanup_workspace(workspace);
320 :
321 0 : pg_xml_done(xmlerrcxt, true);
322 0 : PG_RE_THROW();
323 : }
324 4 : PG_END_TRY();
325 :
326 4 : cleanup_workspace(workspace);
327 4 : pg_xml_done(xmlerrcxt, false);
328 :
329 4 : pfree(xpath);
330 :
331 4 : if (xpres == NULL)
332 0 : PG_RETURN_NULL();
333 4 : PG_RETURN_TEXT_P(xpres);
334 : }
335 :
336 :
337 4 : PG_FUNCTION_INFO_V1(xpath_string);
338 :
339 : Datum
340 2 : xpath_string(PG_FUNCTION_ARGS)
341 : {
342 2 : text *document = PG_GETARG_TEXT_PP(0);
343 2 : text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */
344 : xmlChar *xpath;
345 : int32 pathsize;
346 2 : text *volatile xpres = NULL;
347 2 : xpath_workspace *volatile workspace = NULL;
348 : PgXmlErrorContext *xmlerrcxt;
349 :
350 2 : pathsize = VARSIZE_ANY_EXHDR(xpathsupp);
351 :
352 : /*
353 : * We encapsulate the supplied path with "string()" = 8 chars + 1 for NUL
354 : * at end
355 : */
356 : /* We could try casting to string using the libxml function? */
357 :
358 2 : xpath = (xmlChar *) palloc(pathsize + 9);
359 2 : memcpy(xpath, "string(", 7);
360 2 : memcpy(xpath + 7, VARDATA_ANY(xpathsupp), pathsize);
361 2 : xpath[pathsize + 7] = ')';
362 2 : xpath[pathsize + 8] = '\0';
363 :
364 2 : xmlerrcxt = pgxml_parser_init(PG_XML_STRICTNESS_LEGACY);
365 :
366 2 : PG_TRY();
367 : {
368 2 : workspace = pgxml_xpath(document, xpath, xmlerrcxt);
369 2 : xpres = pgxml_result_to_text(workspace->res, NULL, NULL, NULL);
370 : }
371 0 : PG_CATCH();
372 : {
373 0 : if (workspace)
374 0 : cleanup_workspace(workspace);
375 :
376 0 : pg_xml_done(xmlerrcxt, true);
377 0 : PG_RE_THROW();
378 : }
379 2 : PG_END_TRY();
380 :
381 2 : cleanup_workspace(workspace);
382 2 : pg_xml_done(xmlerrcxt, false);
383 :
384 2 : pfree(xpath);
385 :
386 2 : if (xpres == NULL)
387 2 : PG_RETURN_NULL();
388 0 : PG_RETURN_TEXT_P(xpres);
389 : }
390 :
391 :
392 2 : PG_FUNCTION_INFO_V1(xpath_number);
393 :
394 : Datum
395 0 : xpath_number(PG_FUNCTION_ARGS)
396 : {
397 0 : text *document = PG_GETARG_TEXT_PP(0);
398 0 : text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */
399 : xmlChar *xpath;
400 0 : volatile float4 fRes = 0.0;
401 0 : volatile bool isNull = false;
402 0 : xpath_workspace *volatile workspace = NULL;
403 : PgXmlErrorContext *xmlerrcxt;
404 :
405 0 : xpath = pgxml_texttoxmlchar(xpathsupp);
406 0 : xmlerrcxt = pgxml_parser_init(PG_XML_STRICTNESS_LEGACY);
407 :
408 0 : PG_TRY();
409 : {
410 0 : workspace = pgxml_xpath(document, xpath, xmlerrcxt);
411 0 : pfree(xpath);
412 :
413 0 : if (workspace->res == NULL)
414 0 : isNull = true;
415 : else
416 0 : fRes = xmlXPathCastToNumber(workspace->res);
417 : }
418 0 : PG_CATCH();
419 : {
420 0 : if (workspace)
421 0 : cleanup_workspace(workspace);
422 :
423 0 : pg_xml_done(xmlerrcxt, true);
424 0 : PG_RE_THROW();
425 : }
426 0 : PG_END_TRY();
427 :
428 0 : cleanup_workspace(workspace);
429 0 : pg_xml_done(xmlerrcxt, false);
430 :
431 0 : if (isNull || xmlXPathIsNaN(fRes))
432 0 : PG_RETURN_NULL();
433 :
434 0 : PG_RETURN_FLOAT4(fRes);
435 : }
436 :
437 :
438 2 : PG_FUNCTION_INFO_V1(xpath_bool);
439 :
440 : Datum
441 0 : xpath_bool(PG_FUNCTION_ARGS)
442 : {
443 0 : text *document = PG_GETARG_TEXT_PP(0);
444 0 : text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */
445 : xmlChar *xpath;
446 0 : volatile int bRes = 0;
447 0 : xpath_workspace *volatile workspace = NULL;
448 : PgXmlErrorContext *xmlerrcxt;
449 :
450 0 : xpath = pgxml_texttoxmlchar(xpathsupp);
451 0 : xmlerrcxt = pgxml_parser_init(PG_XML_STRICTNESS_LEGACY);
452 :
453 0 : PG_TRY();
454 : {
455 0 : workspace = pgxml_xpath(document, xpath, xmlerrcxt);
456 0 : pfree(xpath);
457 :
458 0 : if (workspace->res == NULL)
459 0 : bRes = 0;
460 : else
461 0 : bRes = xmlXPathCastToBoolean(workspace->res);
462 : }
463 0 : PG_CATCH();
464 : {
465 0 : if (workspace)
466 0 : cleanup_workspace(workspace);
467 :
468 0 : pg_xml_done(xmlerrcxt, true);
469 0 : PG_RE_THROW();
470 : }
471 0 : PG_END_TRY();
472 :
473 0 : cleanup_workspace(workspace);
474 0 : pg_xml_done(xmlerrcxt, false);
475 :
476 0 : PG_RETURN_BOOL(bRes);
477 : }
478 :
479 :
480 :
481 : /* Core function to evaluate XPath query */
482 :
483 : static xpath_workspace *
484 12 : pgxml_xpath(text *document, xmlChar *xpath, PgXmlErrorContext *xmlerrcxt)
485 : {
486 12 : int32 docsize = VARSIZE_ANY_EXHDR(document);
487 : xmlXPathCompExprPtr comppath;
488 : xpath_workspace *workspace = (xpath_workspace *)
489 12 : palloc0(sizeof(xpath_workspace));
490 :
491 12 : workspace->doctree = NULL;
492 12 : workspace->ctxt = NULL;
493 12 : workspace->res = NULL;
494 :
495 12 : workspace->doctree = xmlReadMemory((char *) VARDATA_ANY(document),
496 : docsize, NULL, NULL,
497 : XML_PARSE_NOENT);
498 12 : if (workspace->doctree != NULL)
499 : {
500 10 : workspace->ctxt = xmlXPathNewContext(workspace->doctree);
501 10 : workspace->ctxt->node = xmlDocGetRootElement(workspace->doctree);
502 :
503 : /* compile the path */
504 10 : comppath = xmlXPathCtxtCompile(workspace->ctxt, xpath);
505 10 : if (comppath == NULL || pg_xml_error_occurred(xmlerrcxt))
506 0 : xml_ereport(xmlerrcxt, ERROR, ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
507 : "XPath Syntax Error");
508 :
509 : /* Now evaluate the path expression. */
510 10 : workspace->res = xmlXPathCompiledEval(comppath, workspace->ctxt);
511 :
512 10 : xmlXPathFreeCompExpr(comppath);
513 : }
514 :
515 12 : return workspace;
516 : }
517 :
518 : /* Clean up after processing the result of pgxml_xpath() */
519 : static void
520 12 : cleanup_workspace(xpath_workspace *workspace)
521 : {
522 12 : if (workspace->res)
523 10 : xmlXPathFreeObject(workspace->res);
524 12 : workspace->res = NULL;
525 12 : if (workspace->ctxt)
526 10 : xmlXPathFreeContext(workspace->ctxt);
527 12 : workspace->ctxt = NULL;
528 12 : if (workspace->doctree)
529 10 : xmlFreeDoc(workspace->doctree);
530 12 : workspace->doctree = NULL;
531 12 : }
532 :
533 : static text *
534 12 : pgxml_result_to_text(xmlXPathObjectPtr res,
535 : xmlChar *toptag,
536 : xmlChar *septag,
537 : xmlChar *plainsep)
538 : {
539 12 : xmlChar *volatile xpresstr = NULL;
540 12 : text *volatile xpres = NULL;
541 : PgXmlErrorContext *xmlerrcxt;
542 :
543 12 : if (res == NULL)
544 2 : return NULL;
545 :
546 : /* spin some error handling */
547 10 : xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
548 :
549 10 : PG_TRY();
550 : {
551 10 : switch (res->type)
552 : {
553 10 : case XPATH_NODESET:
554 10 : xpresstr = pgxmlNodeSetToText(res->nodesetval,
555 : toptag,
556 : septag, plainsep);
557 10 : break;
558 :
559 0 : case XPATH_STRING:
560 0 : xpresstr = xmlStrdup(res->stringval);
561 0 : if (xpresstr == NULL || pg_xml_error_occurred(xmlerrcxt))
562 0 : xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
563 : "could not allocate result");
564 0 : break;
565 :
566 0 : default:
567 0 : elog(NOTICE, "unsupported XQuery result: %d", res->type);
568 0 : xpresstr = xmlStrdup((const xmlChar *) "<unsupported/>");
569 0 : if (xpresstr == NULL || pg_xml_error_occurred(xmlerrcxt))
570 0 : xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
571 : "could not allocate result");
572 : }
573 :
574 : /* Now convert this result back to text */
575 10 : xpres = cstring_to_text((char *) xpresstr);
576 : }
577 0 : PG_CATCH();
578 : {
579 0 : if (xpresstr != NULL)
580 0 : xmlFree(xpresstr);
581 :
582 0 : pg_xml_done(xmlerrcxt, true);
583 :
584 0 : PG_RE_THROW();
585 : }
586 10 : PG_END_TRY();
587 :
588 : /* Free various storage */
589 10 : xmlFree(xpresstr);
590 :
591 10 : pg_xml_done(xmlerrcxt, false);
592 :
593 10 : return xpres;
594 : }
595 :
596 : /*
597 : * xpath_table is a table function. It needs some tidying (as do the
598 : * other functions here!
599 : */
600 4 : PG_FUNCTION_INFO_V1(xpath_table);
601 :
602 : Datum
603 10 : xpath_table(PG_FUNCTION_ARGS)
604 : {
605 : /* Function parameters */
606 10 : char *pkeyfield = text_to_cstring(PG_GETARG_TEXT_PP(0));
607 10 : char *xmlfield = text_to_cstring(PG_GETARG_TEXT_PP(1));
608 10 : char *relname = text_to_cstring(PG_GETARG_TEXT_PP(2));
609 10 : char *xpathset = text_to_cstring(PG_GETARG_TEXT_PP(3));
610 10 : char *condition = text_to_cstring(PG_GETARG_TEXT_PP(4));
611 :
612 : /* SPI (input tuple) support */
613 : SPITupleTable *tuptable;
614 : HeapTuple spi_tuple;
615 : TupleDesc spi_tupdesc;
616 :
617 :
618 10 : ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
619 : AttInMetadata *attinmeta;
620 :
621 : char **values;
622 : xmlChar **xpaths;
623 : char *pos;
624 10 : const char *pathsep = "|";
625 :
626 : int numpaths;
627 : int ret;
628 : uint64 proc;
629 : int j;
630 : int rownr; /* For issuing multiple rows from one original
631 : * document */
632 : bool had_values; /* To determine end of nodeset results */
633 : StringInfoData query_buf;
634 : PgXmlErrorContext *xmlerrcxt;
635 10 : volatile xmlDocPtr doctree = NULL;
636 :
637 10 : InitMaterializedSRF(fcinfo, MAT_SRF_USE_EXPECTED_DESC);
638 :
639 : /* must have at least one output column (for the pkey) */
640 10 : if (rsinfo->setDesc->natts < 1)
641 0 : ereport(ERROR,
642 : (errcode(ERRCODE_SYNTAX_ERROR),
643 : errmsg("xpath_table must have at least one output column")));
644 :
645 : /*
646 : * At the moment we assume that the returned attributes make sense for the
647 : * XPath specified (i.e. we trust the caller). It's not fatal if they get
648 : * it wrong - the input function for the column type will raise an error
649 : * if the path result can't be converted into the correct binary
650 : * representation.
651 : */
652 :
653 10 : attinmeta = TupleDescGetAttInMetadata(rsinfo->setDesc);
654 :
655 10 : values = (char **) palloc(rsinfo->setDesc->natts * sizeof(char *));
656 10 : xpaths = (xmlChar **) palloc(rsinfo->setDesc->natts * sizeof(xmlChar *));
657 :
658 : /*
659 : * Split XPaths. xpathset is a writable CString.
660 : *
661 : * Note that we stop splitting once we've done all needed for tupdesc
662 : */
663 10 : numpaths = 0;
664 10 : pos = xpathset;
665 14 : while (numpaths < (rsinfo->setDesc->natts - 1))
666 : {
667 10 : xpaths[numpaths++] = (xmlChar *) pos;
668 10 : pos = strstr(pos, pathsep);
669 10 : if (pos != NULL)
670 : {
671 4 : *pos = '\0';
672 4 : pos++;
673 : }
674 : else
675 6 : break;
676 : }
677 :
678 : /* Now build query */
679 10 : initStringInfo(&query_buf);
680 :
681 : /* Build initial sql statement */
682 10 : appendStringInfo(&query_buf, "SELECT %s, %s FROM %s WHERE %s",
683 : pkeyfield,
684 : xmlfield,
685 : relname,
686 : condition);
687 :
688 10 : SPI_connect();
689 :
690 10 : if ((ret = SPI_exec(query_buf.data, 0)) != SPI_OK_SELECT)
691 0 : elog(ERROR, "xpath_table: SPI execution failed for query %s",
692 : query_buf.data);
693 :
694 10 : proc = SPI_processed;
695 10 : tuptable = SPI_tuptable;
696 10 : spi_tupdesc = tuptable->tupdesc;
697 :
698 : /*
699 : * Check that SPI returned correct result. If you put a comma into one of
700 : * the function parameters, this will catch it when the SPI query returns
701 : * e.g. 3 columns.
702 : */
703 10 : if (spi_tupdesc->natts != 2)
704 : {
705 0 : ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
706 : errmsg("expression returning multiple columns is not valid in parameter list"),
707 : errdetail("Expected two columns in SPI result, got %d.", spi_tupdesc->natts)));
708 : }
709 :
710 : /*
711 : * Setup the parser. This should happen after we are done evaluating the
712 : * query, in case it calls functions that set up libxml differently.
713 : */
714 10 : xmlerrcxt = pgxml_parser_init(PG_XML_STRICTNESS_LEGACY);
715 :
716 10 : PG_TRY();
717 : {
718 : /* For each row i.e. document returned from SPI */
719 : uint64 i;
720 :
721 20 : for (i = 0; i < proc; i++)
722 : {
723 : char *pkey;
724 : char *xmldoc;
725 : xmlXPathContextPtr ctxt;
726 : xmlXPathObjectPtr res;
727 : xmlChar *resstr;
728 : xmlXPathCompExprPtr comppath;
729 : HeapTuple ret_tuple;
730 :
731 : /* Extract the row data as C Strings */
732 10 : spi_tuple = tuptable->vals[i];
733 10 : pkey = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
734 10 : xmldoc = SPI_getvalue(spi_tuple, spi_tupdesc, 2);
735 :
736 : /*
737 : * Clear the values array, so that not-well-formed documents
738 : * return NULL in all columns. Note that this also means that
739 : * spare columns will be NULL.
740 : */
741 30 : for (j = 0; j < rsinfo->setDesc->natts; j++)
742 20 : values[j] = NULL;
743 :
744 : /* Insert primary key */
745 10 : values[0] = pkey;
746 :
747 : /* Parse the document */
748 10 : if (xmldoc)
749 10 : doctree = xmlReadMemory(xmldoc, strlen(xmldoc),
750 : NULL, NULL,
751 : XML_PARSE_NOENT);
752 : else /* treat NULL as not well-formed */
753 0 : doctree = NULL;
754 :
755 10 : if (doctree == NULL)
756 : {
757 : /* not well-formed, so output all-NULL tuple */
758 0 : ret_tuple = BuildTupleFromCStrings(attinmeta, values);
759 0 : tuplestore_puttuple(rsinfo->setResult, ret_tuple);
760 0 : heap_freetuple(ret_tuple);
761 : }
762 : else
763 : {
764 : /* New loop here - we have to deal with nodeset results */
765 10 : rownr = 0;
766 :
767 : do
768 : {
769 : /* Now evaluate the set of xpaths. */
770 16 : had_values = false;
771 36 : for (j = 0; j < numpaths; j++)
772 : {
773 20 : ctxt = xmlXPathNewContext(doctree);
774 20 : if (ctxt == NULL || pg_xml_error_occurred(xmlerrcxt))
775 0 : xml_ereport(xmlerrcxt,
776 : ERROR, ERRCODE_OUT_OF_MEMORY,
777 : "could not allocate XPath context");
778 :
779 20 : ctxt->node = xmlDocGetRootElement(doctree);
780 :
781 : /* compile the path */
782 20 : comppath = xmlXPathCtxtCompile(ctxt, xpaths[j]);
783 20 : if (comppath == NULL || pg_xml_error_occurred(xmlerrcxt))
784 0 : xml_ereport(xmlerrcxt, ERROR,
785 : ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
786 : "XPath Syntax Error");
787 :
788 : /* Now evaluate the path expression. */
789 20 : res = xmlXPathCompiledEval(comppath, ctxt);
790 20 : xmlXPathFreeCompExpr(comppath);
791 :
792 20 : if (res != NULL)
793 : {
794 20 : switch (res->type)
795 : {
796 20 : case XPATH_NODESET:
797 : /* We see if this nodeset has enough nodes */
798 20 : if (res->nodesetval != NULL &&
799 20 : rownr < res->nodesetval->nodeNr)
800 : {
801 8 : resstr = xmlXPathCastNodeToString(res->nodesetval->nodeTab[rownr]);
802 8 : if (resstr == NULL || pg_xml_error_occurred(xmlerrcxt))
803 0 : xml_ereport(xmlerrcxt,
804 : ERROR, ERRCODE_OUT_OF_MEMORY,
805 : "could not allocate result");
806 8 : had_values = true;
807 : }
808 : else
809 12 : resstr = NULL;
810 :
811 20 : break;
812 :
813 0 : case XPATH_STRING:
814 0 : resstr = xmlStrdup(res->stringval);
815 0 : if (resstr == NULL || pg_xml_error_occurred(xmlerrcxt))
816 0 : xml_ereport(xmlerrcxt,
817 : ERROR, ERRCODE_OUT_OF_MEMORY,
818 : "could not allocate result");
819 0 : break;
820 :
821 0 : default:
822 0 : elog(NOTICE, "unsupported XQuery result: %d", res->type);
823 0 : resstr = xmlStrdup((const xmlChar *) "<unsupported/>");
824 0 : if (resstr == NULL || pg_xml_error_occurred(xmlerrcxt))
825 0 : xml_ereport(xmlerrcxt,
826 : ERROR, ERRCODE_OUT_OF_MEMORY,
827 : "could not allocate result");
828 : }
829 :
830 : /*
831 : * Insert this into the appropriate column in the
832 : * result tuple.
833 : */
834 20 : values[j + 1] = (char *) resstr;
835 : }
836 20 : xmlXPathFreeContext(ctxt);
837 : }
838 :
839 : /* Now add the tuple to the output, if there is one. */
840 16 : if (had_values)
841 : {
842 6 : ret_tuple = BuildTupleFromCStrings(attinmeta, values);
843 6 : tuplestore_puttuple(rsinfo->setResult, ret_tuple);
844 6 : heap_freetuple(ret_tuple);
845 : }
846 :
847 16 : rownr++;
848 16 : } while (had_values);
849 : }
850 :
851 10 : if (doctree != NULL)
852 10 : xmlFreeDoc(doctree);
853 10 : doctree = NULL;
854 :
855 10 : if (pkey)
856 10 : pfree(pkey);
857 10 : if (xmldoc)
858 10 : pfree(xmldoc);
859 : }
860 : }
861 0 : PG_CATCH();
862 : {
863 0 : if (doctree != NULL)
864 0 : xmlFreeDoc(doctree);
865 :
866 0 : pg_xml_done(xmlerrcxt, true);
867 :
868 0 : PG_RE_THROW();
869 : }
870 10 : PG_END_TRY();
871 :
872 10 : if (doctree != NULL)
873 0 : xmlFreeDoc(doctree);
874 :
875 10 : pg_xml_done(xmlerrcxt, false);
876 :
877 10 : SPI_finish();
878 :
879 : /*
880 : * SFRM_Materialize mode expects us to return a NULL Datum. The actual
881 : * tuples are in our tuplestore and passed back through rsinfo->setResult.
882 : * rsinfo->setDesc is set to the tuple description that we actually used
883 : * to build our tuples with, so the caller can verify we did what it was
884 : * expecting.
885 : */
886 10 : return (Datum) 0;
887 : }
|