2004-03-05 06:24:50 +03:00
|
|
|
/* Parser interface for DOM-based parser (libxml) rather than
|
|
|
|
stream-based SAX-type parser */
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
#include "fmgr.h"
|
|
|
|
#include "executor/spi.h"
|
|
|
|
#include "funcapi.h"
|
|
|
|
#include "miscadmin.h"
|
|
|
|
#include "lib/stringinfo.h"
|
|
|
|
|
|
|
|
/* libxml includes */
|
|
|
|
|
|
|
|
#include <libxml/xpath.h>
|
|
|
|
#include <libxml/tree.h>
|
|
|
|
#include <libxml/xmlmemory.h>
|
|
|
|
#include <libxml/xmlerror.h>
|
|
|
|
#include <libxml/parserInternals.h>
|
|
|
|
|
|
|
|
/* declarations */
|
|
|
|
|
|
|
|
static void *pgxml_palloc(size_t size);
|
|
|
|
static void *pgxml_repalloc(void *ptr, size_t size);
|
|
|
|
static void pgxml_pfree(void *ptr);
|
|
|
|
static char *pgxml_pstrdup(const char *string);
|
2004-08-29 09:07:03 +04:00
|
|
|
static void pgxml_errorHandler(void *ctxt, const char *msg,...);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
void elog_error(int level, char *explain, int force);
|
|
|
|
void pgxml_parser_init(void);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
static xmlChar *pgxmlNodeSetToText(xmlNodeSetPtr nodeset,
|
|
|
|
xmlChar * toptagname, xmlChar * septagname,
|
|
|
|
xmlChar * plainsep);
|
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
text *pgxml_result_to_text(xmlXPathObjectPtr res, xmlChar * toptag,
|
|
|
|
xmlChar * septag, xmlChar * plainsep);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
xmlChar *pgxml_texttoxmlchar(text *textstring);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
static xmlXPathObjectPtr pgxml_xpath(text *document, xmlChar * xpath);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
|
2004-03-07 23:41:27 +03:00
|
|
|
Datum xml_valid(PG_FUNCTION_ARGS);
|
2004-12-03 01:21:12 +03:00
|
|
|
Datum xml_encode_special_chars(PG_FUNCTION_ARGS);
|
2004-08-29 09:07:03 +04:00
|
|
|
Datum xpath_nodeset(PG_FUNCTION_ARGS);
|
2004-03-05 06:24:50 +03:00
|
|
|
Datum xpath_string(PG_FUNCTION_ARGS);
|
|
|
|
Datum xpath_number(PG_FUNCTION_ARGS);
|
2004-08-29 09:07:03 +04:00
|
|
|
Datum xpath_bool(PG_FUNCTION_ARGS);
|
|
|
|
Datum xpath_list(PG_FUNCTION_ARGS);
|
|
|
|
Datum xpath_table(PG_FUNCTION_ARGS);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
/* Global variables */
|
2004-08-29 09:07:03 +04:00
|
|
|
char *errbuf; /* per line error buffer */
|
|
|
|
char *pgxml_errorMsg = NULL; /* overall error message */
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
/* Convenience macros */
|
|
|
|
|
|
|
|
#define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp)))
|
|
|
|
#define GET_STR(textp) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp)))
|
|
|
|
|
|
|
|
#define ERRBUF_SIZE 200
|
|
|
|
|
|
|
|
/* memory handling passthrough functions (e.g. palloc, pstrdup are
|
|
|
|
currently macros, and the others might become so...) */
|
|
|
|
|
|
|
|
static void *
|
|
|
|
pgxml_palloc(size_t size)
|
|
|
|
{
|
|
|
|
/* elog(DEBUG1,"Alloc %d in CMC %x",size,CurrentMemoryContext); */
|
|
|
|
return palloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
pgxml_repalloc(void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
/* elog(DEBUG1,"ReAlloc in CMC %x",CurrentMemoryContext);*/
|
|
|
|
return repalloc(ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pgxml_pfree(void *ptr)
|
|
|
|
{
|
|
|
|
/* elog(DEBUG1,"Free in CMC %x",CurrentMemoryContext); */
|
2004-10-13 05:26:42 +04:00
|
|
|
pfree(ptr);
|
2004-03-05 06:24:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
pgxml_pstrdup(const char *string)
|
|
|
|
{
|
|
|
|
return pstrdup(string);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The error handling function. This formats an error message and sets
|
|
|
|
* a flag - an ereport will be issued prior to return
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
2004-08-29 09:07:03 +04:00
|
|
|
pgxml_errorHandler(void *ctxt, const char *msg,...)
|
2004-03-05 06:24:50 +03:00
|
|
|
{
|
2004-08-29 09:07:03 +04:00
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, msg);
|
|
|
|
vsnprintf(errbuf, ERRBUF_SIZE, msg, args);
|
|
|
|
va_end(args);
|
|
|
|
/* Now copy the argument across */
|
|
|
|
if (pgxml_errorMsg == NULL)
|
|
|
|
pgxml_errorMsg = pstrdup(errbuf);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int32 xsize = strlen(pgxml_errorMsg);
|
|
|
|
|
|
|
|
pgxml_errorMsg = repalloc(pgxml_errorMsg,
|
|
|
|
(size_t) (xsize + strlen(errbuf) + 1));
|
|
|
|
strncpy(&pgxml_errorMsg[xsize - 1], errbuf, strlen(errbuf));
|
|
|
|
pgxml_errorMsg[xsize + strlen(errbuf) - 1] = '\0';
|
|
|
|
|
|
|
|
}
|
|
|
|
memset(errbuf, 0, ERRBUF_SIZE);
|
2004-03-05 06:24:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This function reports the current message at the level specified */
|
2004-08-29 09:07:03 +04:00
|
|
|
void
|
|
|
|
elog_error(int level, char *explain, int force)
|
2004-03-05 06:24:50 +03:00
|
|
|
{
|
2004-08-29 09:07:03 +04:00
|
|
|
if (force || (pgxml_errorMsg != NULL))
|
2004-03-05 06:24:50 +03:00
|
|
|
{
|
2004-08-29 09:07:03 +04:00
|
|
|
if (pgxml_errorMsg == NULL)
|
|
|
|
{
|
|
|
|
ereport(level, (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
|
|
|
|
errmsg(explain)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ereport(level, (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
|
|
|
|
errmsg("%s:%s", explain, pgxml_errorMsg)));
|
|
|
|
pfree(pgxml_errorMsg);
|
|
|
|
}
|
2004-03-05 06:24:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pgxml_parser_init()
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This code could also set parser settings from user-supplied info.
|
|
|
|
* Quite how these settings are made is another matter :)
|
|
|
|
*/
|
|
|
|
|
|
|
|
xmlMemSetup(pgxml_pfree, pgxml_palloc, pgxml_repalloc, pgxml_pstrdup);
|
|
|
|
xmlInitParser();
|
|
|
|
|
|
|
|
xmlSetGenericErrorFunc(NULL, pgxml_errorHandler);
|
|
|
|
|
|
|
|
xmlSubstituteEntitiesDefault(1);
|
|
|
|
xmlLoadExtDtdDefaultValue = 1;
|
|
|
|
|
|
|
|
pgxml_errorMsg = NULL;
|
|
|
|
|
|
|
|
errbuf = palloc(200);
|
2004-08-29 09:07:03 +04:00
|
|
|
memset(errbuf, 0, 200);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Returns true if document is well-formed */
|
|
|
|
|
2004-03-07 23:41:27 +03:00
|
|
|
PG_FUNCTION_INFO_V1(xml_valid);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
Datum
|
2004-03-07 23:41:27 +03:00
|
|
|
xml_valid(PG_FUNCTION_ARGS)
|
2004-03-05 06:24:50 +03:00
|
|
|
{
|
2004-03-07 23:41:27 +03:00
|
|
|
/* called as xml_valid(document) */
|
2004-03-05 06:24:50 +03:00
|
|
|
xmlDocPtr doctree;
|
|
|
|
text *t = PG_GETARG_TEXT_P(0); /* document buffer */
|
|
|
|
int32 docsize = VARSIZE(t) - VARHDRSZ;
|
|
|
|
|
|
|
|
pgxml_parser_init();
|
|
|
|
|
|
|
|
doctree = xmlParseMemory((char *) VARDATA(t), docsize);
|
|
|
|
if (doctree == NULL)
|
|
|
|
{
|
|
|
|
xmlCleanupParser();
|
|
|
|
PG_RETURN_BOOL(false); /* i.e. not well-formed */
|
|
|
|
}
|
|
|
|
xmlCleanupParser();
|
|
|
|
xmlFreeDoc(doctree);
|
|
|
|
PG_RETURN_BOOL(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-03 01:21:12 +03:00
|
|
|
/* Encodes special characters (<, >, &, " and \r) as XML entities */
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(xml_encode_special_chars);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
xml_encode_special_chars(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2005-10-15 06:49:52 +04:00
|
|
|
text *tin = PG_GETARG_TEXT_P(0);
|
|
|
|
text *tout;
|
|
|
|
int32 ressize;
|
|
|
|
xmlChar *ts,
|
|
|
|
*tt;
|
2004-12-03 01:21:12 +03:00
|
|
|
|
|
|
|
ts = pgxml_texttoxmlchar(tin);
|
|
|
|
|
|
|
|
tt = xmlEncodeSpecialChars(NULL, ts);
|
|
|
|
|
|
|
|
pfree(ts);
|
|
|
|
|
|
|
|
ressize = strlen(tt);
|
|
|
|
tout = (text *) palloc(ressize + VARHDRSZ);
|
|
|
|
memcpy(VARDATA(tout), tt, ressize);
|
|
|
|
VARATT_SIZEP(tout) = ressize + VARHDRSZ;
|
|
|
|
|
|
|
|
xmlFree(tt);
|
|
|
|
|
|
|
|
PG_RETURN_TEXT_P(tout);
|
|
|
|
}
|
|
|
|
|
2004-03-05 06:24:50 +03:00
|
|
|
static xmlChar
|
|
|
|
*
|
|
|
|
pgxmlNodeSetToText(xmlNodeSetPtr nodeset,
|
|
|
|
xmlChar * toptagname,
|
|
|
|
xmlChar * septagname,
|
2004-08-29 09:07:03 +04:00
|
|
|
xmlChar * plainsep)
|
2004-03-05 06:24:50 +03:00
|
|
|
{
|
|
|
|
/* Function translates a nodeset into a text representation */
|
|
|
|
|
|
|
|
/*
|
2005-10-15 06:49:52 +04:00
|
|
|
* iterates over each node in the set and calls xmlNodeDump to write it to
|
|
|
|
* an xmlBuffer -from which an xmlChar * string is returned.
|
2004-03-05 06:24:50 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* each representation is surrounded by <tagname> ... </tagname> */
|
2004-08-29 09:07:03 +04:00
|
|
|
|
|
|
|
/*
|
2005-10-15 06:49:52 +04:00
|
|
|
* plainsep is an ordinary (not tag) seperator - if used, then nodes are
|
|
|
|
* cast to string as output method
|
2004-08-29 09:07:03 +04:00
|
|
|
*/
|
|
|
|
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
xmlBufferPtr buf;
|
|
|
|
xmlChar *result;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
buf = xmlBufferCreate();
|
|
|
|
|
|
|
|
if ((toptagname != NULL) && (xmlStrlen(toptagname) > 0))
|
|
|
|
{
|
|
|
|
xmlBufferWriteChar(buf, "<");
|
|
|
|
xmlBufferWriteCHAR(buf, toptagname);
|
|
|
|
xmlBufferWriteChar(buf, ">");
|
|
|
|
}
|
|
|
|
if (nodeset != NULL)
|
|
|
|
{
|
|
|
|
for (i = 0; i < nodeset->nodeNr; i++)
|
|
|
|
{
|
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
if (plainsep != NULL)
|
2004-03-05 06:24:50 +03:00
|
|
|
{
|
2004-08-29 09:07:03 +04:00
|
|
|
xmlBufferWriteCHAR(buf,
|
2005-10-15 06:49:52 +04:00
|
|
|
xmlXPathCastNodeToString(nodeset->nodeTab[i]));
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
/* If this isn't the last entry, write the plain sep. */
|
|
|
|
if (i < (nodeset->nodeNr) - 1)
|
|
|
|
xmlBufferWriteChar(buf, plainsep);
|
|
|
|
}
|
|
|
|
else
|
2004-03-05 06:24:50 +03:00
|
|
|
{
|
2004-08-29 09:07:03 +04:00
|
|
|
|
|
|
|
|
|
|
|
if ((septagname != NULL) && (xmlStrlen(septagname) > 0))
|
|
|
|
{
|
|
|
|
xmlBufferWriteChar(buf, "<");
|
|
|
|
xmlBufferWriteCHAR(buf, septagname);
|
|
|
|
xmlBufferWriteChar(buf, ">");
|
|
|
|
}
|
|
|
|
xmlNodeDump(buf,
|
|
|
|
nodeset->nodeTab[i]->doc,
|
|
|
|
nodeset->nodeTab[i],
|
|
|
|
1, 0);
|
|
|
|
|
|
|
|
if ((septagname != NULL) && (xmlStrlen(septagname) > 0))
|
|
|
|
{
|
|
|
|
xmlBufferWriteChar(buf, "</");
|
|
|
|
xmlBufferWriteCHAR(buf, septagname);
|
|
|
|
xmlBufferWriteChar(buf, ">");
|
|
|
|
}
|
2004-03-05 06:24:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((toptagname != NULL) && (xmlStrlen(toptagname) > 0))
|
|
|
|
{
|
|
|
|
xmlBufferWriteChar(buf, "</");
|
|
|
|
xmlBufferWriteCHAR(buf, toptagname);
|
|
|
|
xmlBufferWriteChar(buf, ">");
|
|
|
|
}
|
|
|
|
result = xmlStrdup(buf->content);
|
|
|
|
xmlBufferFree(buf);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Translate a PostgreSQL "varlena" -i.e. a variable length parameter
|
|
|
|
* into the libxml2 representation
|
|
|
|
*/
|
|
|
|
|
|
|
|
xmlChar *
|
|
|
|
pgxml_texttoxmlchar(text *textstring)
|
|
|
|
{
|
|
|
|
xmlChar *res;
|
|
|
|
int32 txsize;
|
|
|
|
|
|
|
|
txsize = VARSIZE(textstring) - VARHDRSZ;
|
|
|
|
res = (xmlChar *) palloc(txsize + 1);
|
|
|
|
memcpy((char *) res, VARDATA(textstring), txsize);
|
|
|
|
res[txsize] = '\0';
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Public visible XPath functions */
|
|
|
|
|
|
|
|
/* This is a "raw" xpath function. Check that it returns child elements
|
|
|
|
* properly
|
|
|
|
*/
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(xpath_nodeset);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
xpath_nodeset(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 09:07:03 +04:00
|
|
|
xmlChar *xpath,
|
|
|
|
*toptag,
|
|
|
|
*septag;
|
|
|
|
int32 pathsize;
|
|
|
|
text
|
|
|
|
*xpathsupp,
|
|
|
|
*xpres;
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
/* PG_GETARG_TEXT_P(0) is document buffer */
|
|
|
|
xpathsupp = PG_GETARG_TEXT_P(1); /* XPath expression */
|
|
|
|
|
|
|
|
toptag = pgxml_texttoxmlchar(PG_GETARG_TEXT_P(2));
|
|
|
|
septag = pgxml_texttoxmlchar(PG_GETARG_TEXT_P(3));
|
|
|
|
|
|
|
|
pathsize = VARSIZE(xpathsupp) - VARHDRSZ;
|
|
|
|
|
|
|
|
xpath = pgxml_texttoxmlchar(xpathsupp);
|
|
|
|
|
|
|
|
xpres = pgxml_result_to_text(
|
2004-08-29 09:07:03 +04:00
|
|
|
pgxml_xpath(PG_GETARG_TEXT_P(0), xpath),
|
|
|
|
toptag, septag, NULL);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
/* xmlCleanupParser(); done by result_to_text routine */
|
2004-10-13 05:26:42 +04:00
|
|
|
pfree(xpath);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
if (xpres == NULL)
|
|
|
|
PG_RETURN_NULL();
|
2004-03-05 06:24:50 +03:00
|
|
|
PG_RETURN_TEXT_P(xpres);
|
|
|
|
}
|
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
/* The following function is almost identical, but returns the elements in */
|
|
|
|
/* a list. */
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(xpath_list);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
xpath_list(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 09:07:03 +04:00
|
|
|
xmlChar *xpath,
|
|
|
|
*plainsep;
|
|
|
|
int32 pathsize;
|
|
|
|
text
|
|
|
|
*xpathsupp,
|
|
|
|
*xpres;
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
/* PG_GETARG_TEXT_P(0) is document buffer */
|
|
|
|
xpathsupp = PG_GETARG_TEXT_P(1); /* XPath expression */
|
|
|
|
|
|
|
|
plainsep = pgxml_texttoxmlchar(PG_GETARG_TEXT_P(2));
|
|
|
|
|
|
|
|
pathsize = VARSIZE(xpathsupp) - VARHDRSZ;
|
|
|
|
|
|
|
|
xpath = pgxml_texttoxmlchar(xpathsupp);
|
|
|
|
|
|
|
|
xpres = pgxml_result_to_text(
|
2004-08-29 09:07:03 +04:00
|
|
|
pgxml_xpath(PG_GETARG_TEXT_P(0), xpath),
|
|
|
|
NULL, NULL, plainsep);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
/* xmlCleanupParser(); done by result_to_text routine */
|
2004-10-13 05:26:42 +04:00
|
|
|
pfree(xpath);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
if (xpres == NULL)
|
|
|
|
PG_RETURN_NULL();
|
2004-03-05 06:24:50 +03:00
|
|
|
PG_RETURN_TEXT_P(xpres);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(xpath_string);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
xpath_string(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 09:07:03 +04:00
|
|
|
xmlChar *xpath;
|
|
|
|
int32 pathsize;
|
|
|
|
text
|
|
|
|
*xpathsupp,
|
|
|
|
*xpres;
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
/* PG_GETARG_TEXT_P(0) is document buffer */
|
|
|
|
xpathsupp = PG_GETARG_TEXT_P(1); /* XPath expression */
|
|
|
|
|
|
|
|
pathsize = VARSIZE(xpathsupp) - VARHDRSZ;
|
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
/*
|
2005-10-15 06:49:52 +04:00
|
|
|
* We encapsulate the supplied path with "string()" = 8 chars + 1 for NUL
|
|
|
|
* at end
|
2004-08-29 09:07:03 +04:00
|
|
|
*/
|
2004-03-05 06:24:50 +03:00
|
|
|
/* We could try casting to string using the libxml function? */
|
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
xpath = (xmlChar *) palloc(pathsize + 9);
|
|
|
|
memcpy((char *) (xpath + 7), VARDATA(xpathsupp), pathsize);
|
|
|
|
strncpy((char *) xpath, "string(", 7);
|
|
|
|
xpath[pathsize + 7] = ')';
|
|
|
|
xpath[pathsize + 8] = '\0';
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
xpres = pgxml_result_to_text(
|
2004-08-29 09:07:03 +04:00
|
|
|
pgxml_xpath(PG_GETARG_TEXT_P(0), xpath),
|
|
|
|
NULL, NULL, NULL);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
xmlCleanupParser();
|
2004-10-13 05:26:42 +04:00
|
|
|
pfree(xpath);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
if (xpres == NULL)
|
|
|
|
PG_RETURN_NULL();
|
2004-03-05 06:24:50 +03:00
|
|
|
PG_RETURN_TEXT_P(xpres);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(xpath_number);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
xpath_number(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 09:07:03 +04:00
|
|
|
xmlChar *xpath;
|
|
|
|
int32 pathsize;
|
|
|
|
text
|
|
|
|
*xpathsupp;
|
|
|
|
|
|
|
|
float4 fRes;
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
xmlXPathObjectPtr res;
|
|
|
|
|
|
|
|
/* PG_GETARG_TEXT_P(0) is document buffer */
|
|
|
|
xpathsupp = PG_GETARG_TEXT_P(1); /* XPath expression */
|
|
|
|
|
|
|
|
pathsize = VARSIZE(xpathsupp) - VARHDRSZ;
|
|
|
|
|
|
|
|
xpath = pgxml_texttoxmlchar(xpathsupp);
|
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
res = pgxml_xpath(PG_GETARG_TEXT_P(0), xpath);
|
2004-10-13 05:26:42 +04:00
|
|
|
pfree(xpath);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
if (res == NULL)
|
2004-08-29 09:07:03 +04:00
|
|
|
{
|
|
|
|
xmlCleanupParser();
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
}
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
fRes = xmlXPathCastToNumber(res);
|
|
|
|
xmlCleanupParser();
|
|
|
|
if (xmlXPathIsNaN(fRes))
|
2004-08-29 09:07:03 +04:00
|
|
|
PG_RETURN_NULL();
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
PG_RETURN_FLOAT4(fRes);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(xpath_bool);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
xpath_bool(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 09:07:03 +04:00
|
|
|
xmlChar *xpath;
|
|
|
|
int32 pathsize;
|
|
|
|
text
|
|
|
|
*xpathsupp;
|
|
|
|
|
|
|
|
int bRes;
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
xmlXPathObjectPtr res;
|
|
|
|
|
|
|
|
/* PG_GETARG_TEXT_P(0) is document buffer */
|
|
|
|
xpathsupp = PG_GETARG_TEXT_P(1); /* XPath expression */
|
|
|
|
|
|
|
|
pathsize = VARSIZE(xpathsupp) - VARHDRSZ;
|
|
|
|
|
|
|
|
xpath = pgxml_texttoxmlchar(xpathsupp);
|
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
res = pgxml_xpath(PG_GETARG_TEXT_P(0), xpath);
|
2004-10-13 05:26:42 +04:00
|
|
|
pfree(xpath);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
if (res == NULL)
|
2004-08-29 09:07:03 +04:00
|
|
|
{
|
|
|
|
xmlCleanupParser();
|
|
|
|
PG_RETURN_BOOL(false);
|
|
|
|
}
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
bRes = xmlXPathCastToBoolean(res);
|
|
|
|
xmlCleanupParser();
|
|
|
|
PG_RETURN_BOOL(bRes);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Core function to evaluate XPath query */
|
|
|
|
|
|
|
|
xmlXPathObjectPtr
|
2004-08-29 09:07:03 +04:00
|
|
|
pgxml_xpath(text *document, xmlChar * xpath)
|
|
|
|
{
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
xmlDocPtr doctree;
|
|
|
|
xmlXPathContextPtr ctxt;
|
|
|
|
xmlXPathObjectPtr res;
|
|
|
|
|
|
|
|
xmlXPathCompExprPtr comppath;
|
|
|
|
|
|
|
|
int32 docsize;
|
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
|
2004-03-05 06:24:50 +03:00
|
|
|
docsize = VARSIZE(document) - VARHDRSZ;
|
|
|
|
|
|
|
|
pgxml_parser_init();
|
|
|
|
|
|
|
|
doctree = xmlParseMemory((char *) VARDATA(document), docsize);
|
|
|
|
if (doctree == NULL)
|
2004-08-29 09:07:03 +04:00
|
|
|
{ /* not well-formed */
|
2004-03-05 06:24:50 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxt = xmlXPathNewContext(doctree);
|
|
|
|
ctxt->node = xmlDocGetRootElement(doctree);
|
|
|
|
|
|
|
|
|
|
|
|
/* compile the path */
|
|
|
|
comppath = xmlXPathCompile(xpath);
|
|
|
|
if (comppath == NULL)
|
|
|
|
{
|
|
|
|
xmlCleanupParser();
|
|
|
|
xmlFreeDoc(doctree);
|
2004-08-29 09:07:03 +04:00
|
|
|
elog_error(ERROR, "XPath Syntax Error", 1);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
return NULL;
|
2004-03-05 06:24:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now evaluate the path expression. */
|
|
|
|
res = xmlXPathCompiledEval(comppath, ctxt);
|
|
|
|
xmlXPathFreeCompExpr(comppath);
|
|
|
|
|
|
|
|
if (res == NULL)
|
|
|
|
{
|
2004-08-29 09:07:03 +04:00
|
|
|
xmlXPathFreeContext(ctxt);
|
|
|
|
/* xmlCleanupParser(); */
|
2004-03-05 06:24:50 +03:00
|
|
|
xmlFreeDoc(doctree);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* xmlFreeDoc(doctree); */
|
|
|
|
return res;
|
2004-08-29 09:07:03 +04:00
|
|
|
}
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
text
|
|
|
|
*
|
|
|
|
pgxml_result_to_text(xmlXPathObjectPtr res,
|
|
|
|
xmlChar * toptag,
|
|
|
|
xmlChar * septag,
|
|
|
|
xmlChar * plainsep)
|
2004-03-05 06:24:50 +03:00
|
|
|
{
|
2004-08-29 09:07:03 +04:00
|
|
|
xmlChar *xpresstr;
|
|
|
|
int32 ressize;
|
|
|
|
text *xpres;
|
|
|
|
|
|
|
|
if (res == NULL)
|
2005-01-09 20:40:40 +03:00
|
|
|
{
|
|
|
|
xmlCleanupParser();
|
2004-08-29 09:07:03 +04:00
|
|
|
return NULL;
|
2005-01-09 20:40:40 +03:00
|
|
|
}
|
2004-03-05 06:24:50 +03:00
|
|
|
switch (res->type)
|
|
|
|
{
|
|
|
|
case XPATH_NODESET:
|
|
|
|
xpresstr = pgxmlNodeSetToText(res->nodesetval,
|
2004-08-29 09:07:03 +04:00
|
|
|
toptag,
|
|
|
|
septag, plainsep);
|
2004-03-05 06:24:50 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case XPATH_STRING:
|
|
|
|
xpresstr = xmlStrdup(res->stringval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
elog(NOTICE, "Unsupported XQuery result: %d", res->type);
|
|
|
|
xpresstr = xmlStrdup("<unsupported/>");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Now convert this result back to text */
|
|
|
|
ressize = strlen(xpresstr);
|
|
|
|
xpres = (text *) palloc(ressize + VARHDRSZ);
|
|
|
|
memcpy(VARDATA(xpres), xpresstr, ressize);
|
|
|
|
VARATT_SIZEP(xpres) = ressize + VARHDRSZ;
|
|
|
|
|
|
|
|
/* Free various storage */
|
|
|
|
xmlCleanupParser();
|
|
|
|
/* xmlFreeDoc(doctree); -- will die at end of tuple anyway */
|
|
|
|
|
|
|
|
xmlFree(xpresstr);
|
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
elog_error(ERROR, "XPath error", 0);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
|
|
|
|
return xpres;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* xpath_table is a table function. It needs some tidying (as do the
|
|
|
|
* other functions here!
|
|
|
|
*/
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(xpath_table);
|
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
Datum
|
|
|
|
xpath_table(PG_FUNCTION_ARGS)
|
2004-03-05 06:24:50 +03:00
|
|
|
{
|
|
|
|
/* SPI (input tuple) support */
|
2004-08-29 09:07:03 +04:00
|
|
|
SPITupleTable *tuptable;
|
|
|
|
HeapTuple spi_tuple;
|
|
|
|
TupleDesc spi_tupdesc;
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
/* Output tuple (tuplestore) support */
|
2004-08-29 09:07:03 +04:00
|
|
|
Tuplestorestate *tupstore = NULL;
|
|
|
|
TupleDesc ret_tupdesc;
|
|
|
|
HeapTuple ret_tuple;
|
|
|
|
|
|
|
|
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
|
|
|
|
AttInMetadata *attinmeta;
|
|
|
|
MemoryContext per_query_ctx;
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
|
|
|
|
/* Function parameters */
|
|
|
|
char *pkeyfield = GET_STR(PG_GETARG_TEXT_P(0));
|
|
|
|
char *xmlfield = GET_STR(PG_GETARG_TEXT_P(1));
|
|
|
|
char *relname = GET_STR(PG_GETARG_TEXT_P(2));
|
|
|
|
char *xpathset = GET_STR(PG_GETARG_TEXT_P(3));
|
|
|
|
char *condition = GET_STR(PG_GETARG_TEXT_P(4));
|
|
|
|
|
|
|
|
char **values;
|
|
|
|
xmlChar **xpaths;
|
|
|
|
xmlChar *pos;
|
|
|
|
xmlChar *pathsep = "|";
|
|
|
|
|
|
|
|
int numpaths;
|
|
|
|
int ret;
|
|
|
|
int proc;
|
|
|
|
int i;
|
|
|
|
int j;
|
2005-10-15 06:49:52 +04:00
|
|
|
int rownr; /* For issuing multiple rows from one original
|
|
|
|
* document */
|
2004-08-29 09:07:03 +04:00
|
|
|
int had_values; /* To determine end of nodeset results */
|
|
|
|
|
|
|
|
StringInfo querysql;
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2005-07-09 05:53:22 +04:00
|
|
|
/* We only have a valid tuple description in table function mode */
|
|
|
|
if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("set-valued function called in context that cannot accept a set")));
|
2004-08-29 09:07:03 +04:00
|
|
|
if (rsinfo->expectedDesc == NULL)
|
2005-07-09 05:53:22 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("xpath_table must be called as a table function")));
|
2004-08-29 09:07:03 +04:00
|
|
|
|
2005-07-09 05:53:22 +04:00
|
|
|
/*
|
|
|
|
* We want to materialise because it means that we don't have to carry
|
|
|
|
* libxml2 parser state between invocations of this function
|
|
|
|
*/
|
|
|
|
if (!(rsinfo->allowedModes & SFRM_Materialize))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
2005-10-15 06:49:52 +04:00
|
|
|
errmsg("xpath_table requires Materialize mode, but it is not "
|
|
|
|
"allowed in this context")));
|
2005-07-09 05:53:22 +04:00
|
|
|
|
2005-10-15 06:49:52 +04:00
|
|
|
/*
|
|
|
|
* The tuplestore must exist in a higher context than this function call
|
|
|
|
* (per_query_ctx is used)
|
2005-07-09 05:53:22 +04:00
|
|
|
*/
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
|
|
|
|
oldcontext = MemoryContextSwitchTo(per_query_ctx);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2005-10-15 06:49:52 +04:00
|
|
|
/*
|
|
|
|
* Create the tuplestore - work_mem is the max in-memory size before a
|
2005-07-09 05:53:22 +04:00
|
|
|
* file is created on disk to hold it.
|
|
|
|
*/
|
2004-08-29 09:07:03 +04:00
|
|
|
tupstore = tuplestore_begin_heap(true, false, work_mem);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
MemoryContextSwitchTo(oldcontext);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
/* get the requested return tuple description */
|
|
|
|
ret_tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
/*
|
2005-10-15 06:49:52 +04:00
|
|
|
* At the moment we assume that the returned attributes make sense for the
|
|
|
|
* XPath specififed (i.e. we trust the caller). It's not fatal if they get
|
|
|
|
* it wrong - the input function for the column type will raise an error
|
|
|
|
* if the path result can't be converted into the correct binary
|
|
|
|
* representation.
|
2004-08-29 09:07:03 +04:00
|
|
|
*/
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
attinmeta = TupleDescGetAttInMetadata(ret_tupdesc);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
/* Set return mode and allocate value space. */
|
|
|
|
rsinfo->returnMode = SFRM_Materialize;
|
|
|
|
rsinfo->setDesc = ret_tupdesc;
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
values = (char **) palloc(ret_tupdesc->natts * sizeof(char *));
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
xpaths = (xmlChar **) palloc(ret_tupdesc->natts * sizeof(xmlChar *));
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
/* Split XPaths. xpathset is a writable CString. */
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
/* Note that we stop splitting once we've done all needed for tupdesc */
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
numpaths = 0;
|
|
|
|
pos = xpathset;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
xpaths[numpaths] = pos;
|
|
|
|
pos = strstr(pos, pathsep);
|
|
|
|
if (pos != NULL)
|
|
|
|
{
|
|
|
|
*pos = '\0';
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
numpaths++;
|
|
|
|
} while ((pos != NULL) && (numpaths < (ret_tupdesc->natts - 1)));
|
|
|
|
|
|
|
|
/* Now build query */
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
querysql = makeStringInfo();
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
/* Build initial sql statement */
|
|
|
|
appendStringInfo(querysql, "SELECT %s, %s FROM %s WHERE %s",
|
|
|
|
pkeyfield,
|
|
|
|
xmlfield,
|
|
|
|
relname,
|
|
|
|
condition
|
|
|
|
);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
if ((ret = SPI_connect()) < 0)
|
|
|
|
elog(ERROR, "xpath_table: SPI_connect returned %d", ret);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
if ((ret = SPI_exec(querysql->data, 0)) != SPI_OK_SELECT)
|
|
|
|
elog(ERROR, "xpath_table: SPI execution failed for query %s", querysql->data);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
proc = SPI_processed;
|
|
|
|
/* elog(DEBUG1,"xpath_table: SPI returned %d rows",proc); */
|
|
|
|
tuptable = SPI_tuptable;
|
|
|
|
spi_tupdesc = tuptable->tupdesc;
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
/* Switch out of SPI context */
|
2004-08-29 09:07:03 +04:00
|
|
|
MemoryContextSwitchTo(oldcontext);
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
|
|
|
|
/* Check that SPI returned correct result. If you put a comma into one of
|
|
|
|
* the function parameters, this will catch it when the SPI query returns
|
2004-08-29 09:07:03 +04:00
|
|
|
* e.g. 3 columns.
|
2004-03-05 06:24:50 +03:00
|
|
|
*/
|
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
if (spi_tupdesc->natts != 2)
|
|
|
|
{
|
|
|
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("Expression returning multiple columns is not valid in parameter list"),
|
|
|
|
errdetail("Expected two columns in SPI result, got %d", spi_tupdesc->natts)));
|
|
|
|
}
|
2004-03-05 06:24:50 +03:00
|
|
|
|
|
|
|
/* Setup the parser. Beware that this must happen in the same context as the
|
|
|
|
* cleanup - which means that any error from here on must do cleanup to
|
|
|
|
* ensure that the entity table doesn't get freed by being out of context.
|
|
|
|
*/
|
2004-08-29 09:07:03 +04:00
|
|
|
pgxml_parser_init();
|
|
|
|
|
|
|
|
/* For each row i.e. document returned from SPI */
|
|
|
|
for (i = 0; i < proc; i++)
|
|
|
|
{
|
|
|
|
char *pkey;
|
|
|
|
char *xmldoc;
|
|
|
|
|
|
|
|
xmlDocPtr doctree;
|
|
|
|
xmlXPathContextPtr ctxt;
|
|
|
|
xmlXPathObjectPtr res;
|
|
|
|
xmlChar *resstr;
|
|
|
|
|
|
|
|
|
|
|
|
xmlXPathCompExprPtr comppath;
|
|
|
|
|
|
|
|
/* Extract the row data as C Strings */
|
|
|
|
|
|
|
|
spi_tuple = tuptable->vals[i];
|
|
|
|
pkey = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
|
|
|
|
xmldoc = SPI_getvalue(spi_tuple, spi_tupdesc, 2);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2005-10-15 06:49:52 +04:00
|
|
|
* Clear the values array, so that not-well-formed documents return
|
|
|
|
* NULL in all columns.
|
2004-08-29 09:07:03 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Note that this also means that spare columns will be NULL. */
|
|
|
|
for (j = 0; j < ret_tupdesc->natts; j++)
|
|
|
|
values[j] = NULL;
|
|
|
|
|
|
|
|
/* Insert primary key */
|
|
|
|
values[0] = pkey;
|
|
|
|
|
|
|
|
/* Parse the document */
|
|
|
|
doctree = xmlParseMemory(xmldoc, strlen(xmldoc));
|
|
|
|
|
|
|
|
if (doctree == NULL)
|
2005-10-15 06:49:52 +04:00
|
|
|
{ /* not well-formed, so output all-NULL tuple */
|
2004-08-29 09:07:03 +04:00
|
|
|
|
|
|
|
ret_tuple = BuildTupleFromCStrings(attinmeta, values);
|
|
|
|
oldcontext = MemoryContextSwitchTo(per_query_ctx);
|
|
|
|
tuplestore_puttuple(tupstore, ret_tuple);
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
heap_freetuple(ret_tuple);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* New loop here - we have to deal with nodeset results */
|
|
|
|
rownr = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Now evaluate the set of xpaths. */
|
|
|
|
had_values = 0;
|
|
|
|
for (j = 0; j < numpaths; j++)
|
|
|
|
{
|
|
|
|
|
|
|
|
ctxt = xmlXPathNewContext(doctree);
|
|
|
|
ctxt->node = xmlDocGetRootElement(doctree);
|
|
|
|
xmlSetGenericErrorFunc(ctxt, pgxml_errorHandler);
|
|
|
|
|
|
|
|
/* compile the path */
|
|
|
|
comppath = xmlXPathCompile(xpaths[j]);
|
|
|
|
if (comppath == NULL)
|
|
|
|
{
|
|
|
|
xmlCleanupParser();
|
|
|
|
xmlFreeDoc(doctree);
|
|
|
|
|
|
|
|
elog_error(ERROR, "XPath Syntax Error", 1);
|
|
|
|
|
|
|
|
PG_RETURN_NULL(); /* Keep compiler happy */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now evaluate the path expression. */
|
|
|
|
res = xmlXPathCompiledEval(comppath, ctxt);
|
|
|
|
xmlXPathFreeCompExpr(comppath);
|
|
|
|
|
|
|
|
if (res != NULL)
|
|
|
|
{
|
|
|
|
switch (res->type)
|
|
|
|
{
|
|
|
|
case XPATH_NODESET:
|
|
|
|
/* We see if this nodeset has enough nodes */
|
|
|
|
if ((res->nodesetval != NULL) && (rownr < res->nodesetval->nodeNr))
|
|
|
|
{
|
|
|
|
resstr =
|
|
|
|
xmlXPathCastNodeToString(res->nodesetval->nodeTab[rownr]);
|
|
|
|
had_values = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
resstr = NULL;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XPATH_STRING:
|
|
|
|
resstr = xmlStrdup(res->stringval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
elog(NOTICE, "Unsupported XQuery result: %d", res->type);
|
|
|
|
resstr = xmlStrdup("<unsupported/>");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert this into the appropriate column in the
|
|
|
|
* result tuple.
|
|
|
|
*/
|
|
|
|
values[j + 1] = resstr;
|
|
|
|
}
|
|
|
|
xmlXPathFreeContext(ctxt);
|
|
|
|
}
|
|
|
|
/* Now add the tuple to the output, if there is one. */
|
|
|
|
if (had_values)
|
|
|
|
{
|
|
|
|
ret_tuple = BuildTupleFromCStrings(attinmeta, values);
|
|
|
|
oldcontext = MemoryContextSwitchTo(per_query_ctx);
|
|
|
|
tuplestore_puttuple(tupstore, ret_tuple);
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
heap_freetuple(ret_tuple);
|
|
|
|
}
|
|
|
|
|
|
|
|
rownr++;
|
|
|
|
|
|
|
|
} while (had_values);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlFreeDoc(doctree);
|
|
|
|
|
|
|
|
pfree(pkey);
|
|
|
|
pfree(xmldoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlCleanupParser();
|
2004-03-05 06:24:50 +03:00
|
|
|
/* Needed to flag completeness in 7.3.1. 7.4 defines it as a no-op. */
|
2004-08-29 09:07:03 +04:00
|
|
|
tuplestore_donestoring(tupstore);
|
|
|
|
|
|
|
|
SPI_finish();
|
|
|
|
|
|
|
|
rsinfo->setResult = tupstore;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SFRM_Materialize mode expects us to return a NULL Datum. The actual
|
2005-10-15 06:49:52 +04:00
|
|
|
* tuples are in our tuplestore and passed back through rsinfo->setResult.
|
|
|
|
* rsinfo->setDesc is set to the tuple description that we actually used
|
|
|
|
* to build our tuples with, so the caller can verify we did what it was
|
|
|
|
* expecting.
|
2004-08-29 09:07:03 +04:00
|
|
|
*/
|
|
|
|
return (Datum) 0;
|
|
|
|
|
2004-03-05 06:24:50 +03:00
|
|
|
}
|