49 #include <libxml/chvalid.h>
50 #include <libxml/entities.h>
51 #include <libxml/parser.h>
52 #include <libxml/parserInternals.h>
53 #include <libxml/tree.h>
54 #include <libxml/uri.h>
55 #include <libxml/xmlerror.h>
56 #include <libxml/xmlsave.h>
57 #include <libxml/xmlversion.h>
58 #include <libxml/xmlwriter.h>
59 #include <libxml/xpath.h>
60 #include <libxml/xpathInternals.h>
67 #if LIBXML_VERSION >= 20704
68 #define HAVE_XMLSTRUCTUREDERRORCONTEXT 1
74 #if LIBXML_VERSION >= 21200
75 #define PgXmlErrorPtr const xmlError *
77 #define PgXmlErrorPtr xmlErrorPtr
115 #define ERRCXT_MAGIC 68275028
126 xmlStructuredErrorFunc saved_errfunc;
129 xmlExternalEntityLoader saved_entityfunc;
132 static xmlParserInputPtr xmlPgEntityLoader(
const char *URL,
const char *ID,
133 xmlParserCtxtPtr ctxt);
135 int sqlcode,
const char *msg);
136 static void xml_errorHandler(
void *
data, PgXmlErrorPtr
error);
137 static int errdetail_for_xml_code(
int code);
141 #ifdef USE_LIBXMLCONTEXT
145 static void xml_memory_init(
void);
146 static void *xml_palloc(
size_t size);
147 static void *xml_repalloc(
void *ptr,
size_t size);
148 static void xml_pfree(
void *ptr);
149 static char *xml_pstrdup(
const char *
string);
152 static xmlChar *xml_text2xmlChar(
text *in);
153 static int parse_xml_decl(
const xmlChar *
str,
size_t *lenp,
154 xmlChar **version, xmlChar **
encoding,
int *standalone);
155 static bool print_xml_decl(
StringInfo buf,
const xmlChar *version,
157 static bool xml_doctype_in_content(
const xmlChar *
str);
159 bool preserve_whitespace,
int encoding,
161 xmlNodePtr *parsed_nodes,
164 static int xml_xpathobjtoxmlarray(xmlXPathObjectPtr xpathobj,
167 static xmlChar *pg_xmlCharStrndup(
const char *
str,
size_t len);
171 const char *xmlschema,
const char *targetns,
175 const char *xmlschema,
bool nulls,
bool tableforest,
176 const char *targetns,
bool top_level);
178 bool nulls,
bool tableforest,
const char *targetns);
180 List *relid_list,
bool nulls,
181 bool tableforest,
const char *targetns);
183 bool nulls,
bool tableforest,
184 const char *targetns);
189 char *tablename,
bool nulls,
bool tableforest,
190 const char *targetns,
bool top_level);
195 #define XMLTABLE_CONTEXT_MAGIC 46922182
196 typedef struct XmlTableBuilderData
202 xmlParserCtxtPtr ctxt;
204 xmlXPathContextPtr xpathcxt;
205 xmlXPathCompExprPtr xpathcomp;
206 xmlXPathObjectPtr xpathobj;
207 xmlXPathCompExprPtr *xpathscomp;
208 } XmlTableBuilderData;
217 const char *path,
int colnum);
220 Oid typid,
int32 typmod,
bool *isnull);
235 #define NO_XML_SUPPORT() \
237 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
238 errmsg("unsupported XML feature"), \
239 errdetail("This functionality requires the server to be built with libxml support.")))
243 #define NAMESPACE_XSD "http://www.w3.org/2001/XMLSchema"
244 #define NAMESPACE_XSI "http://www.w3.org/2001/XMLSchema-instance"
245 #define NAMESPACE_SQLXML "http://standards.iso.org/iso/9075/2003/sqlxml"
251 xmlChar_to_encoding(
const xmlChar *encoding_name)
257 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
258 errmsg(
"invalid encoding name \"%s\"",
259 (
const char *) encoding_name)));
289 NULL, NULL, fcinfo->context);
301 #define PG_XML_DEFAULT_VERSION "1.0"
322 if ((res_code = parse_xml_decl((xmlChar *)
str,
323 &
len, &version, NULL, &standalone)) == 0)
329 if (!print_xml_decl(&
buf, version, target_encoding, standalone))
347 errcode(ERRCODE_INTERNAL_ERROR),
349 errdetail_for_xml_code(res_code));
380 xmlChar *encodingStr = NULL;
388 nbytes =
buf->len -
buf->cursor;
402 parse_xml_decl((
const xmlChar *)
str, NULL, NULL, &encodingStr, NULL);
482 xmlBuffer_to_xmltype(xmlBufferPtr
buf)
485 xmlBufferLength(
buf));
503 if (argdata[
i] ==
'-' && argdata[
i - 1] ==
'-')
505 (
errcode(ERRCODE_INVALID_XML_COMMENT),
506 errmsg(
"invalid XML comment")));
508 if (
len > 0 && argdata[
len - 1] ==
'-')
510 (
errcode(ERRCODE_INVALID_XML_COMMENT),
511 errmsg(
"invalid XML comment")));
532 xmlChar *xmlbuf = NULL;
534 xmlbuf = xmlEncodeSpecialChars(NULL, xml_text2xmlChar(
arg));
556 int global_standalone = 1;
557 xmlChar *global_version = NULL;
558 bool global_version_no_value =
false;
574 parse_xml_decl((xmlChar *)
str, &
len, &version, NULL, &standalone);
576 if (standalone == 0 && global_standalone == 1)
577 global_standalone = 0;
579 global_standalone = -1;
582 global_version_no_value =
true;
583 else if (!global_version)
584 global_version = version;
585 else if (xmlStrcmp(version, global_version) != 0)
586 global_version_no_value =
true;
592 if (!global_version_no_value || global_standalone >= 0)
598 print_xml_decl(&buf2,
599 (!global_version_no_value) ? global_version : NULL,
659 text *
volatile result;
662 xmlNodePtr content_nodes;
663 volatile xmlBufferPtr
buf = NULL;
664 volatile xmlSaveCtxtPtr ctxt = NULL;
682 &parsed_xmloptiontype, &content_nodes,
683 (
Node *) &escontext);
690 (
errcode(ERRCODE_NOT_AN_XML_DOCUMENT),
691 errmsg(
"not an XML document")));
709 buf = xmlBufferCreate();
711 if (
buf == NULL || xmlerrcxt->err_occurred)
713 "could not allocate xmlBuffer");
716 parse_xml_decl(xml_text2xmlChar(
data), &decl_len, NULL, NULL, NULL);
725 ctxt = xmlSaveToBuffer(
buf, NULL,
726 XML_SAVE_NO_DECL | XML_SAVE_FORMAT);
728 ctxt = xmlSaveToBuffer(
buf, NULL,
731 if (ctxt == NULL || xmlerrcxt->err_occurred)
733 "could not allocate xmlSaveCtxt");
738 if (xmlSaveDoc(ctxt, doc) == -1 || xmlerrcxt->err_occurred)
740 "could not save document to xmlBuffer");
742 else if (content_nodes != NULL)
753 root = xmlNewNode(NULL, (
const xmlChar *)
"content-root");
754 if (root == NULL || xmlerrcxt->err_occurred)
756 "could not allocate xml node");
759 xmlDocSetRootElement(doc, root);
760 xmlAddChild(root, content_nodes);
769 newline = xmlNewDocText(NULL, (
const xmlChar *)
"\n");
770 if (
newline == NULL || xmlerrcxt->err_occurred)
772 "could not allocate xml node");
774 for (xmlNodePtr node = root->children; node; node = node->next)
777 if (node->type != XML_TEXT_NODE && node->prev != NULL)
779 if (xmlSaveTree(ctxt,
newline) == -1 || xmlerrcxt->err_occurred)
783 "could not save newline to xmlBuffer");
787 if (xmlSaveTree(ctxt, node) == -1 || xmlerrcxt->err_occurred)
791 "could not save content to xmlBuffer");
798 if (xmlSaveClose(ctxt) == -1 || xmlerrcxt->err_occurred)
802 "could not close xmlSaveCtxtPtr");
805 result = (
text *) xmlBuffer_to_xmltype(
buf);
837 Datum *named_argvalue,
bool *named_argnull,
838 Datum *argvalue,
bool *argnull)
842 List *named_arg_strings;
848 volatile xmlBufferPtr
buf = NULL;
849 volatile xmlTextWriterPtr writer = NULL;
858 named_arg_strings =
NIL;
865 if (named_argnull[
i])
871 named_arg_strings =
lappend(named_arg_strings,
str);
897 buf = xmlBufferCreate();
898 if (
buf == NULL || xmlerrcxt->err_occurred)
900 "could not allocate xmlBuffer");
901 writer = xmlNewTextWriterMemory(
buf, 0);
902 if (writer == NULL || xmlerrcxt->err_occurred)
904 "could not allocate xmlTextWriter");
906 xmlTextWriterStartElement(writer, (xmlChar *) xexpr->name);
908 forboth(
arg, named_arg_strings, narg, xexpr->arg_names)
914 xmlTextWriterWriteAttribute(writer,
919 foreach(
arg, arg_strings)
923 xmlTextWriterWriteRaw(writer, (xmlChar *)
str);
926 xmlTextWriterEndElement(writer);
929 xmlFreeTextWriter(writer);
932 result = xmlBuffer_to_xmltype(
buf);
937 xmlFreeTextWriter(writer);
965 doc = xml_parse(
data, xmloption_arg, preserve_whitespace,
978 xmlpi(
const char *target,
text *
arg,
bool arg_is_null,
bool *result_is_null)
986 (
errcode(ERRCODE_SYNTAX_ERROR),
987 errmsg(
"invalid XML processing instruction"),
988 errdetail(
"XML processing instruction target name cannot be \"%s\".", target)));
994 *result_is_null = arg_is_null;
1007 if (strstr(
string,
"?>") != NULL)
1009 (
errcode(ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION),
1010 errmsg(
"invalid XML processing instruction"),
1011 errdetail(
"XML processing instruction cannot contain \"?>\".")));
1035 xmlChar *orig_version;
1036 int orig_standalone;
1042 parse_xml_decl((xmlChar *)
str, &
len, &orig_version, NULL, &orig_standalone);
1045 orig_version = xml_text2xmlChar(version);
1047 orig_version = NULL;
1052 orig_standalone = 1;
1055 orig_standalone = 0;
1058 orig_standalone = -1;
1066 print_xml_decl(&
buf, orig_version, 0, orig_standalone);
1089 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1090 errmsg(
"xmlvalidate is not implemented")));
1134 static bool first_time =
true;
1144 if (
sizeof(
char) !=
sizeof(xmlChar))
1146 (
errmsg(
"could not initialize XML library"),
1147 errdetail(
"libxml2 has incompatible char type: sizeof(char)=%zu, sizeof(xmlChar)=%zu.",
1148 sizeof(
char),
sizeof(xmlChar))));
1150 #ifdef USE_LIBXMLCONTEXT
1156 LIBXML_TEST_VERSION;
1188 errcxt->magic = ERRCXT_MAGIC;
1189 errcxt->strictness = strictness;
1190 errcxt->err_occurred =
false;
1200 errcxt->saved_errfunc = xmlStructuredError;
1202 #ifdef HAVE_XMLSTRUCTUREDERRORCONTEXT
1203 errcxt->saved_errcxt = xmlStructuredErrorContext;
1205 errcxt->saved_errcxt = xmlGenericErrorContext;
1208 xmlSetStructuredErrorFunc((
void *) errcxt, xml_errorHandler);
1224 #ifdef HAVE_XMLSTRUCTUREDERRORCONTEXT
1225 new_errcxt = xmlStructuredErrorContext;
1227 new_errcxt = xmlGenericErrorContext;
1230 if (new_errcxt != (
void *) errcxt)
1232 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1233 errmsg(
"could not set up XML error handler"),
1234 errhint(
"This probably indicates that the version of libxml2"
1235 " being used is not compatible with the libxml2"
1236 " header files that PostgreSQL was built with.")));
1242 errcxt->saved_entityfunc = xmlGetExternalEntityLoader();
1243 xmlSetExternalEntityLoader(xmlPgEntityLoader);
1264 Assert(errcxt->magic == ERRCXT_MAGIC);
1271 Assert(!errcxt->err_occurred || isError);
1278 #ifdef HAVE_XMLSTRUCTUREDERRORCONTEXT
1279 cur_errcxt = xmlStructuredErrorContext;
1281 cur_errcxt = xmlGenericErrorContext;
1284 if (cur_errcxt != (
void *) errcxt)
1285 elog(
WARNING,
"libxml error handling state is out of sync with xml.c");
1288 xmlSetStructuredErrorFunc(errcxt->saved_errcxt, errcxt->saved_errfunc);
1289 xmlSetExternalEntityLoader(errcxt->saved_entityfunc);
1298 pfree(errcxt->err_buf.data);
1309 return errcxt->err_occurred;
1322 #define CHECK_XML_SPACE(p) \
1324 if (!xmlIsBlank_ch(*(p))) \
1325 return XML_ERR_SPACE_REQUIRED; \
1328 #define SKIP_XML_SPACE(p) \
1329 while (xmlIsBlank_ch(*(p))) (p)++
1333 #define PG_XMLISNAMECHAR(c) \
1334 (xmlIsBaseChar_ch(c) || xmlIsIdeographicQ(c) \
1335 || xmlIsDigit_ch(c) \
1336 || c == '.' || c == '-' || c == '_' || c == ':' \
1337 || xmlIsCombiningQ(c) \
1338 || xmlIsExtender_ch(c))
1342 xml_pnstrdup(
const xmlChar *
str,
size_t len)
1346 result = (xmlChar *)
palloc((
len + 1) *
sizeof(xmlChar));
1347 memcpy(result,
str,
len *
sizeof(xmlChar));
1354 pg_xmlCharStrndup(
const char *
str,
size_t len)
1358 result = (xmlChar *)
palloc((
len + 1) *
sizeof(xmlChar));
1359 memcpy(result,
str,
len);
1371 xml_pstrdup_and_free(xmlChar *
str)
1400 parse_xml_decl(
const xmlChar *
str,
size_t *lenp,
1401 xmlChar **version, xmlChar **
encoding,
int *standalone)
1404 const xmlChar *save_p;
1427 if (xmlStrncmp(p, (xmlChar *)
"<?xml", 5) != 0)
1439 utf8char = xmlGetUTF8Char(p + 5, &utf8len);
1440 if (PG_XMLISNAMECHAR(utf8char))
1448 if (xmlStrncmp(p, (xmlChar *)
"version", 7) != 0)
1449 return XML_ERR_VERSION_MISSING;
1453 return XML_ERR_VERSION_MISSING;
1457 if (*p ==
'\'' || *p ==
'"')
1461 q = xmlStrchr(p + 1, *p);
1463 return XML_ERR_VERSION_MISSING;
1466 *version = xml_pnstrdup(p + 1, q - p - 1);
1470 return XML_ERR_VERSION_MISSING;
1475 if (xmlStrncmp(p, (xmlChar *)
"encoding", 8) == 0)
1477 CHECK_XML_SPACE(save_p);
1481 return XML_ERR_MISSING_ENCODING;
1485 if (*p ==
'\'' || *p ==
'"')
1489 q = xmlStrchr(p + 1, *p);
1491 return XML_ERR_MISSING_ENCODING;
1494 *
encoding = xml_pnstrdup(p + 1, q - p - 1);
1498 return XML_ERR_MISSING_ENCODING;
1508 if (xmlStrncmp(p, (xmlChar *)
"standalone", 10) == 0)
1510 CHECK_XML_SPACE(save_p);
1514 return XML_ERR_STANDALONE_VALUE;
1517 if (xmlStrncmp(p, (xmlChar *)
"'yes'", 5) == 0 ||
1518 xmlStrncmp(p, (xmlChar *)
"\"yes\"", 5) == 0)
1524 else if (xmlStrncmp(p, (xmlChar *)
"'no'", 4) == 0 ||
1525 xmlStrncmp(p, (xmlChar *)
"\"no\"", 4) == 0)
1532 return XML_ERR_STANDALONE_VALUE;
1540 if (xmlStrncmp(p, (xmlChar *)
"?>", 2) != 0)
1541 return XML_ERR_XMLDECL_NOT_FINISHED;
1549 return XML_ERR_INVALID_CHAR;
1578 || standalone != -1)
1597 if (standalone == 1)
1599 else if (standalone == 0)
1639 xml_doctype_in_content(
const xmlChar *
str)
1641 const xmlChar *p =
str;
1657 if (xmlStrncmp(p, (xmlChar *)
"DOCTYPE", 7) == 0)
1661 if (xmlStrncmp(p, (xmlChar *)
"--", 2) != 0)
1664 p = xmlStrstr(p + 2, (xmlChar *)
"--");
1665 if (!p || p[2] !=
'>')
1678 e = xmlStrstr(p, (xmlChar *)
"?>");
1716 bool preserve_whitespace,
int encoding,
1717 XmlOptionType *parsed_xmloptiontype, xmlNodePtr *parsed_nodes,
1722 xmlChar *utf8string;
1724 volatile xmlParserCtxtPtr ctxt = NULL;
1725 volatile xmlDocPtr doc = NULL;
1732 string = xml_text2xmlChar(
data);
1752 bool parse_as_document =
false;
1755 xmlChar *version = NULL;
1761 ctxt = xmlNewParserCtxt();
1762 if (ctxt == NULL || xmlerrcxt->err_occurred)
1764 "could not allocate parser context");
1768 parse_as_document =
true;
1772 res_code = parse_xml_decl(utf8string,
1773 &count, &version, NULL, &standalone);
1777 errcode(ERRCODE_INVALID_XML_CONTENT),
1779 errdetail_for_xml_code(res_code));
1784 if (xml_doctype_in_content(utf8string + count))
1785 parse_as_document =
true;
1789 if (parsed_xmloptiontype != NULL)
1792 if (parsed_nodes != NULL)
1793 *parsed_nodes = NULL;
1795 if (parse_as_document)
1804 doc = xmlCtxtReadDoc(ctxt, utf8string,
1807 XML_PARSE_NOENT | XML_PARSE_DTDATTR
1808 | (preserve_whitespace ? 0 : XML_PARSE_NOBLANKS));
1809 if (doc == NULL || xmlerrcxt->err_occurred)
1813 xml_errsave(escontext, xmlerrcxt,
1814 ERRCODE_INVALID_XML_DOCUMENT,
1815 "invalid XML document");
1817 xml_errsave(escontext, xmlerrcxt,
1818 ERRCODE_INVALID_XML_CONTENT,
1819 "invalid XML content");
1825 doc = xmlNewDoc(version);
1826 if (doc == NULL || xmlerrcxt->err_occurred)
1828 "could not allocate XML document");
1830 Assert(doc->encoding == NULL);
1831 doc->encoding = xmlStrdup((
const xmlChar *)
"UTF-8");
1832 if (doc->encoding == NULL || xmlerrcxt->err_occurred)
1834 "could not allocate XML document");
1835 doc->standalone = standalone;
1838 if (*(utf8string + count))
1840 res_code = xmlParseBalancedChunkMemory(doc, NULL, NULL, 0,
1843 if (res_code != 0 || xmlerrcxt->err_occurred)
1845 xml_errsave(escontext, xmlerrcxt,
1846 ERRCODE_INVALID_XML_CONTENT,
1847 "invalid XML content");
1861 xmlFreeParserCtxt(ctxt);
1869 xmlFreeParserCtxt(ctxt);
1881 xml_text2xmlChar(
text *in)
1887 #ifdef USE_LIBXMLCONTEXT
1894 xml_memory_init(
void)
1897 if (LibxmlContext == NULL)
1903 xmlMemSetup(xml_pfree, xml_palloc, xml_repalloc, xml_pstrdup);
1910 xml_palloc(
size_t size)
1917 xml_repalloc(
void *ptr,
size_t size)
1924 xml_pfree(
void *ptr)
1933 xml_pstrdup(
const char *
string)
1951 static xmlParserInputPtr
1952 xmlPgEntityLoader(
const char *URL,
const char *ID,
1953 xmlParserCtxtPtr ctxt)
1955 return xmlNewStringInputStream(ctxt, (
const xmlChar *)
"");
1975 if (errcxt->magic != ERRCXT_MAGIC)
1976 elog(
ERROR,
"xml_ereport called with invalid PgXmlErrorContext");
1979 errcxt->err_occurred =
false;
1982 if (errcxt->err_buf.len > 0)
1983 detail = errcxt->err_buf.data;
2008 int sqlcode,
const char *msg)
2013 if (errcxt->magic != ERRCXT_MAGIC)
2014 elog(
ERROR,
"xml_errsave called with invalid PgXmlErrorContext");
2017 errcxt->err_occurred =
false;
2020 if (errcxt->err_buf.len > 0)
2021 detail = errcxt->err_buf.data;
2036 xml_errorHandler(
void *
data, PgXmlErrorPtr
error)
2039 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr)
error->ctxt;
2040 xmlParserInputPtr
input = (ctxt != NULL) ? ctxt->input : NULL;
2041 xmlNodePtr node =
error->node;
2042 const xmlChar *
name = (node != NULL &&
2043 node->type == XML_ELEMENT_NODE) ? node->name : NULL;
2044 int domain =
error->domain;
2045 int level =
error->level;
2054 if (xmlerrcxt->magic != ERRCXT_MAGIC)
2055 elog(
FATAL,
"xml_errorHandler called with invalid PgXmlErrorContext");
2065 switch (
error->code)
2067 case XML_WAR_NS_URI:
2068 level = XML_ERR_ERROR;
2069 domain = XML_FROM_NAMESPACE;
2072 case XML_ERR_NS_DECL_ERROR:
2073 case XML_WAR_NS_URI_RELATIVE:
2074 case XML_WAR_NS_COLUMN:
2075 case XML_NS_ERR_XML_NAMESPACE:
2076 case XML_NS_ERR_UNDEFINED_NAMESPACE:
2077 case XML_NS_ERR_QNAME:
2078 case XML_NS_ERR_ATTRIBUTE_REDEFINED:
2079 case XML_NS_ERR_EMPTY:
2080 domain = XML_FROM_NAMESPACE;
2087 case XML_FROM_PARSER:
2089 case XML_FROM_MEMORY:
2096 if (
error->code == XML_WAR_UNDECLARED_ENTITY)
2112 if (
error->line > 0)
2116 if (
error->message != NULL)
2134 xmlGenericErrorFunc errFuncSaved = xmlGenericError;
2135 void *errCtxSaved = xmlGenericErrorContext;
2137 xmlSetGenericErrorFunc((
void *) errorBuf,
2141 appendStringInfoLineSeparator(errorBuf);
2143 xmlParserPrintFileContext(
input);
2146 xmlSetGenericErrorFunc(errCtxSaved, errFuncSaved);
2150 chopStringInfoNewlines(errorBuf);
2162 appendStringInfoLineSeparator(&xmlerrcxt->err_buf);
2178 if (level >= XML_ERR_ERROR)
2180 appendStringInfoLineSeparator(&xmlerrcxt->err_buf);
2184 xmlerrcxt->err_occurred =
true;
2186 else if (level >= XML_ERR_WARNING)
2211 errdetail_for_xml_code(
int code)
2217 case XML_ERR_INVALID_CHAR:
2220 case XML_ERR_SPACE_REQUIRED:
2223 case XML_ERR_STANDALONE_VALUE:
2224 det =
gettext_noop(
"standalone accepts only 'yes' or 'no'.");
2226 case XML_ERR_VERSION_MISSING:
2227 det =
gettext_noop(
"Malformed declaration: missing version.");
2229 case XML_ERR_MISSING_ENCODING:
2230 det =
gettext_noop(
"Missing encoding in text declaration.");
2232 case XML_ERR_XMLDECL_NOT_FINISHED:
2233 det =
gettext_noop(
"Parsing XML declaration: '?>' expected.");
2236 det =
gettext_noop(
"Unrecognized libxml error code: %d.");
2250 while (
str->len > 0 &&
str->data[
str->len - 1] ==
'\n')
2251 str->data[--
str->len] =
'\0';
2261 chopStringInfoNewlines(
str);
2271 sqlchar_to_unicode(
const char *s)
2282 if (utf8string != s)
2293 return (xmlIsBaseCharQ(
c) || xmlIsIdeographicQ(
c)
2294 ||
c ==
'_' ||
c ==
':');
2302 return (xmlIsBaseCharQ(
c) || xmlIsIdeographicQ(
c)
2304 ||
c ==
'.' ||
c ==
'-' ||
c ==
'_' ||
c ==
':'
2305 || xmlIsCombiningQ(
c)
2306 || xmlIsExtenderQ(
c));
2326 Assert(fully_escaped || !escape_period);
2332 if (*p ==
':' && (p ==
ident || fully_escaped))
2334 else if (*p ==
'_' && *(p + 1) ==
'x')
2336 else if (fully_escaped && p ==
ident &&
2344 else if (escape_period && *p ==
'.')
2348 pg_wchar u = sqlchar_to_unicode(p);
2351 ? !is_valid_xml_namefirst(u)
2352 : !is_valid_xml_namechar(u))
2380 if (*p ==
'_' && *(p + 1) ==
'x'
2381 && isxdigit((
unsigned char) *(p + 2))
2382 && isxdigit((
unsigned char) *(p + 3))
2383 && isxdigit((
unsigned char) *(p + 4))
2384 && isxdigit((
unsigned char) *(p + 5))
2390 sscanf(p + 2,
"%X", &u);
2433 elmlen, elmbyval, elmalign,
2434 &elem_values, &elem_nulls,
2439 for (
i = 0;
i < num_elems;
i++)
2488 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2489 errmsg(
"date out of range"),
2490 errdetail(
"XML does not support infinite date values.")));
2510 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2511 errmsg(
"timestamp out of range"),
2512 errdetail(
"XML does not support infinite timestamp values.")));
2517 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2518 errmsg(
"timestamp out of range")));
2523 case TIMESTAMPTZOID:
2529 const char *tzn = NULL;
2537 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2538 errmsg(
"timestamp out of range"),
2539 errdetail(
"XML does not support infinite timestamp values.")));
2544 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2545 errmsg(
"timestamp out of range")));
2555 volatile xmlBufferPtr
buf = NULL;
2556 volatile xmlTextWriterPtr writer = NULL;
2563 buf = xmlBufferCreate();
2564 if (
buf == NULL || xmlerrcxt->err_occurred)
2566 "could not allocate xmlBuffer");
2567 writer = xmlNewTextWriterMemory(
buf, 0);
2568 if (writer == NULL || xmlerrcxt->err_occurred)
2570 "could not allocate xmlTextWriter");
2573 xmlTextWriterWriteBase64(writer,
VARDATA_ANY(bstr),
2576 xmlTextWriterWriteBinHex(writer,
VARDATA_ANY(bstr),
2580 xmlFreeTextWriter(writer);
2583 result =
pstrdup((
const char *) xmlBufferContent(
buf));
2588 xmlFreeTextWriter(writer);
2615 if (
type == XMLOID || !xml_escape_strings)
2638 for (p =
str; *p; p++)
2666 size_t len = strlen(s) + 1;
2669 memcpy(ret, s,
len);
2729 elog(
ERROR,
"SPI_execute returned %s for %s",
2756 " WHERE relnamespace = %u AND relkind IN ("
2760 " AND pg_catalog.has_table_privilege (oid, 'SELECT')"
2761 " ORDER BY relname;",
nspid);
2771 #define XML_VISIBLE_SCHEMAS_EXCLUDE "(nspname ~ '^pg_' OR nspname = 'information_schema')"
2773 #define XML_VISIBLE_SCHEMAS "SELECT oid FROM pg_catalog.pg_namespace WHERE pg_catalog.has_schema_privilege (oid, 'USAGE') AND NOT " XML_VISIBLE_SCHEMAS_EXCLUDE
2788 " WHERE relkind IN ("
2792 " AND pg_catalog.has_table_privilege(pg_class.oid, 'SELECT')"
2804 const char *xmlschema,
bool nulls,
bool tableforest,
2805 const char *targetns,
bool top_level)
2814 xmlschema, nulls, tableforest,
2815 targetns, top_level);
2842 NULL, nulls, tableforest,
2872 (
errcode(ERRCODE_UNDEFINED_CURSOR),
2873 errmsg(
"cursor \"%s\" does not exist",
name)));
2878 tableforest, targetns,
true);
2903 const char *xmlschema,
const char *targetns,
2907 Assert(top_level || !xmlschema);
2913 if (strlen(targetns) > 0)
2919 if (strlen(targetns) > 0)
2937 const char *xmlschema,
bool nulls,
bool tableforest,
2938 const char *targetns,
bool top_level)
2954 (
errcode(ERRCODE_DATA_EXCEPTION),
2955 errmsg(
"invalid query")));
2960 targetns, top_level);
2969 tableforest, targetns, top_level);
2992 tableforest, targetns);
3013 elog(
ERROR,
"SPI_prepare(\"%s\") failed", query);
3016 elog(
ERROR,
"SPI_cursor_open(\"%s\") failed", query);
3020 tableforest, targetns));
3035 const char *xmlschema;
3042 (
errcode(ERRCODE_UNDEFINED_CURSOR),
3043 errmsg(
"cursor \"%s\" does not exist",
name)));
3046 (
errcode(ERRCODE_INVALID_CURSOR_STATE),
3047 errmsg(
"portal \"%s\" does not return tuples",
name)));
3051 tableforest, targetns));
3066 const char *xmlschema;
3070 tableforest, targetns);
3074 xmlschema, nulls, tableforest,
3087 const char *xmlschema;
3094 elog(
ERROR,
"SPI_prepare(\"%s\") failed", query);
3097 elog(
ERROR,
"SPI_cursor_open(\"%s\") failed", query);
3105 xmlschema, nulls, tableforest,
3117 bool tableforest,
const char *targetns,
bool top_level)
3138 foreach(cell, relid_list)
3173 nulls, tableforest, targetns,
true)));
3186 if (strlen(targetns) > 0)
3189 " targetNamespace=\"%s\"\n"
3190 " elementFormDefault=\"qualified\"",
3206 bool tableforest,
const char *targetns)
3225 foreach(cell, relid_list)
3239 nulls, tableforest, targetns));
3258 nulls, tableforest, targetns)));
3277 tableforest, targetns);
3280 xmlschema->
data, nulls,
3281 tableforest, targetns,
true)));
3292 bool tableforest,
const char *targetns)
3313 foreach(cell, nspid_list)
3319 tableforest, targetns,
false);
3341 tableforest, targetns)));
3347 const char *targetns)
3365 foreach(cell, relid_list)
3396 tableforest, targetns)));
3411 nulls, tableforest, targetns)));
3452 bool tableforest,
const char *targetns)
3456 char *tabletypename;
3469 elog(
ERROR,
"cache lookup failed for relation %u", relid);
3494 tabletypename =
"TableType";
3495 rowtypename =
"RowType";
3504 "<xsd:complexType name=\"%s\">\n"
3505 " <xsd:sequence>\n",
3508 for (
i = 0;
i < tupdesc->
natts;
i++)
3512 if (att->attisdropped)
3515 " <xsd:element name=\"%s\" type=\"%s\"%s></xsd:element>\n",
3519 nulls ?
" nillable=\"true\"" :
" minOccurs=\"0\"");
3523 " </xsd:sequence>\n"
3524 "</xsd:complexType>\n\n");
3529 "<xsd:complexType name=\"%s\">\n"
3531 " <xsd:element name=\"row\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n"
3532 " </xsd:sequence>\n"
3533 "</xsd:complexType>\n\n",
3534 tabletypename, rowtypename);
3537 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3538 xmltn, tabletypename);
3542 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3543 xmltn, rowtypename);
3557 bool tableforest,
const char *targetns)
3562 char *schematypename;
3579 "<xsd:complexType name=\"%s\">\n", schematypename);
3585 " <xsd:sequence>\n");
3587 foreach(cell, relid_list)
3599 " <xsd:element name=\"%s\" type=\"%s\"/>\n",
3600 xmltn, tabletypename);
3603 " <xsd:element name=\"%s\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n",
3604 xmltn, tabletypename);
3612 " </xsd:sequence>\n");
3614 "</xsd:complexType>\n\n");
3617 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3618 xmlsn, schematypename);
3630 bool tableforest,
const char *targetns)
3634 char *catalogtypename;
3650 "<xsd:complexType name=\"%s\">\n", catalogtypename);
3654 foreach(cell, nspid_list)
3665 " <xsd:element name=\"%s\" type=\"%s\"/>\n",
3666 xmlsn, schematypename);
3672 "</xsd:complexType>\n\n");
3675 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3676 xmlcn, catalogtypename);
3711 ((typmod -
VARHDRSZ) >> 16) & 0xffff,
3750 case TIMESTAMPTZOID:
3769 elog(
ERROR,
"cache lookup failed for type %u", typeoid);
3799 foreach(cell0, tupdesc_list)
3803 for (
i = 0;
i < tupdesc->
natts;
i++)
3807 if (att->attisdropped)
3814 foreach(cell0, uniquetypes)
3819 if (basetypid != typid)
3826 foreach(cell0, uniquetypes)
3853 if (typeoid == XMLOID)
3856 "<xsd:complexType mixed=\"true\">\n"
3858 " <xsd:any name=\"element\" minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>\n"
3859 " </xsd:sequence>\n"
3860 "</xsd:complexType>\n");
3865 "<xsd:simpleType name=\"%s\">\n",
typename);
3873 " <xsd:restriction base=\"xsd:string\">\n");
3876 " <xsd:maxLength value=\"%d\"/>\n",
3883 " <xsd:restriction base=\"xsd:%s\">\n"
3884 " </xsd:restriction>\n",
3891 " <xsd:restriction base=\"xsd:decimal\">\n"
3892 " <xsd:totalDigits value=\"%d\"/>\n"
3893 " <xsd:fractionDigits value=\"%d\"/>\n"
3894 " </xsd:restriction>\n",
3895 ((typmod -
VARHDRSZ) >> 16) & 0xffff,
3901 " <xsd:restriction base=\"xsd:short\">\n"
3902 " <xsd:maxInclusive value=\"%d\"/>\n"
3903 " <xsd:minInclusive value=\"%d\"/>\n"
3904 " </xsd:restriction>\n",
3905 SHRT_MAX, SHRT_MIN);
3910 " <xsd:restriction base=\"xsd:int\">\n"
3911 " <xsd:maxInclusive value=\"%d\"/>\n"
3912 " <xsd:minInclusive value=\"%d\"/>\n"
3913 " </xsd:restriction>\n",
3919 " <xsd:restriction base=\"xsd:long\">\n"
3922 " </xsd:restriction>\n",
3929 " <xsd:restriction base=\"xsd:float\"></xsd:restriction>\n");
3934 " <xsd:restriction base=\"xsd:double\"></xsd:restriction>\n");
3939 " <xsd:restriction base=\"xsd:boolean\"></xsd:restriction>\n");
3945 const char *tz = (typeoid == TIMETZOID ?
"(\\+|-)\\p{Nd}{2}:\\p{Nd}{2}" :
"");
3949 " <xsd:restriction base=\"xsd:time\">\n"
3950 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}(.\\p{Nd}+)?%s\"/>\n"
3951 " </xsd:restriction>\n", tz);
3952 else if (typmod == 0)
3954 " <xsd:restriction base=\"xsd:time\">\n"
3955 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3956 " </xsd:restriction>\n", tz);
3959 " <xsd:restriction base=\"xsd:time\">\n"
3960 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}.\\p{Nd}{%d}%s\"/>\n"
3961 " </xsd:restriction>\n", typmod -
VARHDRSZ, tz);
3966 case TIMESTAMPTZOID:
3968 const char *tz = (typeoid == TIMESTAMPTZOID ?
"(\\+|-)\\p{Nd}{2}:\\p{Nd}{2}" :
"");
3972 " <xsd:restriction base=\"xsd:dateTime\">\n"
3973 " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}(.\\p{Nd}+)?%s\"/>\n"
3974 " </xsd:restriction>\n", tz);
3975 else if (typmod == 0)
3977 " <xsd:restriction base=\"xsd:dateTime\">\n"
3978 " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3979 " </xsd:restriction>\n", tz);
3982 " <xsd:restriction base=\"xsd:dateTime\">\n"
3983 " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}.\\p{Nd}{%d}%s\"/>\n"
3984 " </xsd:restriction>\n", typmod -
VARHDRSZ, tz);
3990 " <xsd:restriction base=\"xsd:date\">\n"
3991 " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}\"/>\n"
3992 " </xsd:restriction>\n");
3999 int32 base_typmod = -1;
4004 " <xsd:restriction base=\"%s\"/>\n",
4022 bool nulls,
bool tableforest,
4023 const char *targetns,
bool top_level)
4095 if (
cur->type != XML_ATTRIBUTE_NODE &&
cur->type != XML_TEXT_NODE)
4097 void (*
volatile nodefree) (xmlNodePtr) = NULL;
4098 volatile xmlBufferPtr
buf = NULL;
4099 volatile xmlNodePtr cur_copy = NULL;
4105 buf = xmlBufferCreate();
4106 if (
buf == NULL || xmlerrcxt->err_occurred)
4108 "could not allocate xmlBuffer");
4122 cur_copy = xmlCopyNode(
cur, 1);
4123 if (cur_copy == NULL || xmlerrcxt->err_occurred)
4125 "could not copy node");
4126 nodefree = (cur_copy->type == XML_DOCUMENT_NODE) ?
4127 (
void (*) (xmlNodePtr)) xmlFreeDoc : xmlFreeNode;
4129 bytes = xmlNodeDump(
buf, NULL, cur_copy, 0, 0);
4130 if (bytes == -1 || xmlerrcxt->err_occurred)
4132 "could not dump node");
4134 result = xmlBuffer_to_xmltype(
buf);
4149 str = xmlXPathCastNodeToString(
cur);
4181 xml_xpathobjtoxmlarray(xmlXPathObjectPtr xpathobj,
4190 switch (xpathobj->type)
4193 if (xpathobj->nodesetval != NULL)
4195 result = xpathobj->nodesetval->nodeNr;
4200 for (
i = 0;
i < result;
i++)
4202 datum =
PointerGetDatum(xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[
i],
4215 datumtype = BOOLOID;
4222 datumtype = FLOAT8OID;
4229 datumtype = CSTRINGOID;
4233 elog(
ERROR,
"xpath expression result type %d is unsupported",
4263 volatile xmlParserCtxtPtr ctxt = NULL;
4264 volatile xmlDocPtr doc = NULL;
4265 volatile xmlXPathContextPtr xpathctx = NULL;
4266 volatile xmlXPathCompExprPtr xpathcomp = NULL;
4267 volatile xmlXPathObjectPtr xpathobj = NULL;
4272 xmlChar *xpath_expr;
4273 size_t xmldecl_len = 0;
4276 Datum *ns_names_uris;
4277 bool *ns_names_uris_nulls;
4289 ndim = namespaces ?
ARR_NDIM(namespaces) : 0;
4296 if (ndim != 2 || dims[1] != 2)
4298 (
errcode(ERRCODE_DATA_EXCEPTION),
4299 errmsg(
"invalid array for XML namespace mapping"),
4300 errdetail(
"The array must be two-dimensional with length of the second axis equal to 2.")));
4305 &ns_names_uris, &ns_names_uris_nulls,
4308 Assert((ns_count % 2) == 0);
4313 ns_names_uris = NULL;
4314 ns_names_uris_nulls = NULL;
4323 (
errcode(ERRCODE_DATA_EXCEPTION),
4324 errmsg(
"empty XPath expression")));
4326 string = pg_xmlCharStrndup(datastr,
len);
4327 xpath_expr = pg_xmlCharStrndup(
VARDATA_ANY(xpath_expr_text), xpath_len);
4337 parse_xml_decl(
string, &xmldecl_len, NULL, NULL, NULL);
4349 ctxt = xmlNewParserCtxt();
4350 if (ctxt == NULL || xmlerrcxt->err_occurred)
4352 "could not allocate parser context");
4353 doc = xmlCtxtReadMemory(ctxt, (
char *)
string + xmldecl_len,
4354 len - xmldecl_len, NULL, NULL, 0);
4355 if (doc == NULL || xmlerrcxt->err_occurred)
4357 "could not parse XML document");
4358 xpathctx = xmlXPathNewContext(doc);
4359 if (xpathctx == NULL || xmlerrcxt->err_occurred)
4361 "could not allocate XPath context");
4362 xpathctx->node = (xmlNodePtr) doc;
4367 for (
i = 0;
i < ns_count;
i++)
4372 if (ns_names_uris_nulls[
i * 2] ||
4373 ns_names_uris_nulls[
i * 2 + 1])
4375 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4376 errmsg(
"neither namespace name nor URI may be null")));
4379 if (xmlXPathRegisterNs(xpathctx,
4380 (xmlChar *) ns_name,
4381 (xmlChar *) ns_uri) != 0)
4383 (
errmsg(
"could not register XML namespace with name \"%s\" and URI \"%s\"",
4388 xpathcomp = xmlXPathCompile(xpath_expr);
4389 if (xpathcomp == NULL || xmlerrcxt->err_occurred)
4391 "invalid XPath expression");
4400 xpathobj = xmlXPathCompiledEval(xpathcomp, xpathctx);
4401 if (xpathobj == NULL || xmlerrcxt->err_occurred)
4403 "could not create XPath object");
4408 if (res_nitems != NULL)
4409 *res_nitems = xml_xpathobjtoxmlarray(xpathobj, astate, xmlerrcxt);
4411 (
void) xml_xpathobjtoxmlarray(xpathobj, astate, xmlerrcxt);
4416 xmlXPathFreeObject(xpathobj);
4418 xmlXPathFreeCompExpr(xpathcomp);
4420 xmlXPathFreeContext(xpathctx);
4424 xmlFreeParserCtxt(ctxt);
4432 xmlXPathFreeObject(xpathobj);
4433 xmlXPathFreeCompExpr(xpathcomp);
4434 xmlXPathFreeContext(xpathctx);
4436 xmlFreeParserCtxt(ctxt);
4459 xpath_internal(xpath_expr_text,
data, namespaces,
4480 xpath_internal(xpath_expr_text,
data, NULL,
4504 xpath_internal(xpath_expr_text,
data, namespaces,
4528 doc = xml_parse(
data, xmloption_arg,
true,
4586 static inline XmlTableBuilderData *
4589 XmlTableBuilderData *result;
4592 elog(
ERROR,
"%s called with invalid TableFuncScanState", fname);
4593 result = (XmlTableBuilderData *)
state->opaque;
4594 if (result->magic != XMLTABLE_CONTEXT_MAGIC)
4595 elog(
ERROR,
"%s called with invalid TableFuncScanState", fname);
4616 volatile xmlParserCtxtPtr ctxt = NULL;
4617 XmlTableBuilderData *xtCxt;
4620 xtCxt =
palloc0(
sizeof(XmlTableBuilderData));
4621 xtCxt->magic = XMLTABLE_CONTEXT_MAGIC;
4622 xtCxt->natts = natts;
4623 xtCxt->xpathscomp =
palloc0(
sizeof(xmlXPathCompExprPtr) * natts);
4631 ctxt = xmlNewParserCtxt();
4632 if (ctxt == NULL || xmlerrcxt->err_occurred)
4634 "could not allocate parser context");
4639 xmlFreeParserCtxt(ctxt);
4647 xtCxt->xmlerrcxt = xmlerrcxt;
4650 state->opaque = xtCxt;
4664 XmlTableBuilderData *xtCxt;
4669 volatile xmlDocPtr doc = NULL;
4670 volatile xmlXPathContextPtr xpathcxt = NULL;
4672 xtCxt = GetXmlTableBuilderPrivateData(
state,
"XmlTableSetDocument");
4680 length = strlen(
str);
4681 xstr = pg_xmlCharStrndup(
str, length);
4685 doc = xmlCtxtReadMemory(xtCxt->ctxt, (
char *) xstr, length, NULL, NULL, 0);
4686 if (doc == NULL || xtCxt->xmlerrcxt->err_occurred)
4688 "could not parse XML document");
4689 xpathcxt = xmlXPathNewContext(doc);
4690 if (xpathcxt == NULL || xtCxt->xmlerrcxt->err_occurred)
4692 "could not allocate XPath context");
4693 xpathcxt->node = (xmlNodePtr) doc;
4697 if (xpathcxt != NULL)
4698 xmlXPathFreeContext(xpathcxt);
4707 xtCxt->xpathcxt = xpathcxt;
4721 XmlTableBuilderData *xtCxt;
4725 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4726 errmsg(
"DEFAULT namespace is not supported")));
4727 xtCxt = GetXmlTableBuilderPrivateData(
state,
"XmlTableSetNamespace");
4729 if (xmlXPathRegisterNs(xtCxt->xpathcxt,
4730 pg_xmlCharStrndup(
name, strlen(
name)),
4731 pg_xmlCharStrndup(uri, strlen(uri))))
4733 "could not set XML namespace");
4747 XmlTableBuilderData *xtCxt;
4750 xtCxt = GetXmlTableBuilderPrivateData(
state,
"XmlTableSetRowFilter");
4754 (
errcode(ERRCODE_DATA_EXCEPTION),
4755 errmsg(
"row path filter must not be empty string")));
4757 xstr = pg_xmlCharStrndup(path, strlen(path));
4759 xtCxt->xpathcomp = xmlXPathCompile(xstr);
4760 if (xtCxt->xpathcomp == NULL || xtCxt->xmlerrcxt->err_occurred)
4762 "invalid XPath expression");
4776 XmlTableBuilderData *xtCxt;
4781 xtCxt = GetXmlTableBuilderPrivateData(
state,
"XmlTableSetColumnFilter");
4785 (
errcode(ERRCODE_DATA_EXCEPTION),
4786 errmsg(
"column path filter must not be empty string")));
4788 xstr = pg_xmlCharStrndup(path, strlen(path));
4790 xtCxt->xpathscomp[colnum] = xmlXPathCompile(xstr);
4791 if (xtCxt->xpathscomp[colnum] == NULL || xtCxt->xmlerrcxt->err_occurred)
4793 "invalid XPath expression");
4808 XmlTableBuilderData *xtCxt;
4810 xtCxt = GetXmlTableBuilderPrivateData(
state,
"XmlTableFetchRow");
4813 xmlSetStructuredErrorFunc((
void *) xtCxt->xmlerrcxt, xml_errorHandler);
4815 if (xtCxt->xpathobj == NULL)
4817 xtCxt->xpathobj = xmlXPathCompiledEval(xtCxt->xpathcomp, xtCxt->xpathcxt);
4818 if (xtCxt->xpathobj == NULL || xtCxt->xmlerrcxt->err_occurred)
4820 "could not create XPath object");
4822 xtCxt->row_count = 0;
4825 if (xtCxt->xpathobj->type == XPATH_NODESET)
4827 if (xtCxt->xpathobj->nodesetval != NULL)
4829 if (xtCxt->row_count++ < xtCxt->xpathobj->nodesetval->nodeNr)
4851 Oid typid,
int32 typmod,
bool *isnull)
4854 XmlTableBuilderData *xtCxt;
4858 volatile xmlXPathObjectPtr xpathobj = NULL;
4860 xtCxt = GetXmlTableBuilderPrivateData(
state,
"XmlTableGetValue");
4862 Assert(xtCxt->xpathobj &&
4863 xtCxt->xpathobj->type == XPATH_NODESET &&
4864 xtCxt->xpathobj->nodesetval != NULL);
4867 xmlSetStructuredErrorFunc((
void *) xtCxt->xmlerrcxt, xml_errorHandler);
4871 cur = xtCxt->xpathobj->nodesetval->nodeTab[xtCxt->row_count - 1];
4873 Assert(xtCxt->xpathscomp[colnum] != NULL);
4878 xtCxt->xpathcxt->node =
cur;
4881 xpathobj = xmlXPathCompiledEval(xtCxt->xpathscomp[colnum], xtCxt->xpathcxt);
4882 if (xpathobj == NULL || xtCxt->xmlerrcxt->err_occurred)
4884 "could not create XPath object");
4893 if (xpathobj->type == XPATH_NODESET)
4897 if (xpathobj->nodesetval != NULL)
4898 count = xpathobj->nodesetval->nodeNr;
4900 if (xpathobj->nodesetval == NULL || count == 0)
4906 if (typid == XMLOID)
4913 for (
int i = 0;
i < count;
i++)
4916 xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[
i],
4929 (
errcode(ERRCODE_CARDINALITY_VIOLATION),
4930 errmsg(
"more than one value returned by column XPath expression")));
4932 str = xmlXPathCastNodeSetToString(xpathobj->nodesetval);
4933 cstr =
str ? xml_pstrdup_and_free(
str) :
"";
4937 else if (xpathobj->type == XPATH_STRING)
4940 if (typid == XMLOID)
4941 cstr =
escape_xml((
char *) xpathobj->stringval);
4943 cstr = (
char *) xpathobj->stringval;
4945 else if (xpathobj->type == XPATH_BOOLEAN)
4948 bool typispreferred;
4954 if (typcategory != TYPCATEGORY_NUMERIC)
4955 str = xmlXPathCastBooleanToString(xpathobj->boolval);
4957 str = xmlXPathCastNumberToString(xmlXPathCastBooleanToNumber(xpathobj->boolval));
4959 cstr = xml_pstrdup_and_free(
str);
4961 else if (xpathobj->type == XPATH_NUMBER)
4965 str = xmlXPathCastNumberToString(xpathobj->floatval);
4966 cstr = xml_pstrdup_and_free(
str);
4969 elog(
ERROR,
"unexpected XPath object type %u", xpathobj->type);
4980 state->typioparams[colnum],
4985 if (xpathobj != NULL)
4986 xmlXPathFreeObject(xpathobj);
5005 XmlTableBuilderData *xtCxt;
5007 xtCxt = GetXmlTableBuilderPrivateData(
state,
"XmlTableDestroyOpaque");
5010 xmlSetStructuredErrorFunc((
void *) xtCxt->xmlerrcxt, xml_errorHandler);
5012 if (xtCxt->xpathscomp != NULL)
5016 for (
i = 0;
i < xtCxt->natts;
i++)
5017 if (xtCxt->xpathscomp[
i] != NULL)
5018 xmlXPathFreeCompExpr(xtCxt->xpathscomp[
i]);
5021 if (xtCxt->xpathobj != NULL)
5022 xmlXPathFreeObject(xtCxt->xpathobj);
5023 if (xtCxt->xpathcomp != NULL)
5024 xmlXPathFreeCompExpr(xtCxt->xpathcomp);
5025 if (xtCxt->xpathcxt != NULL)
5026 xmlXPathFreeContext(xtCxt->xpathcxt);
5027 if (xtCxt->doc != NULL)
5028 xmlFreeDoc(xtCxt->doc);
5029 if (xtCxt->ctxt != NULL)
5030 xmlFreeParserCtxt(xtCxt->ctxt);
5036 state->opaque = NULL;
#define PG_GETARG_ARRAYTYPE_P(n)
#define DatumGetArrayTypeP(X)
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
ArrayBuildState * initArrayResult(Oid element_type, MemoryContext rcontext, bool subcontext)
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
void deconstruct_array_builtin(ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
void j2date(int jd, int *year, int *month, int *day)
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
#define TextDatumGetCString(d)
#define PointerIsValid(pointer)
#define OidIsValid(objectId)
#define TIMESTAMP_NOT_FINITE(j)
#define POSTGRES_EPOCH_JDATE
#define DATE_NOT_FINITE(j)
static DateADT DatumGetDateADT(Datum X)
char * get_database_name(Oid dbid)
int errmsg_internal(const char *fmt,...)
int errdetail_internal(const char *fmt,...)
int errdetail(const char *fmt,...)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define errsave(context,...)
#define ereport(elevel,...)
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Datum Float8GetDatum(float8 X)
char * OidOutputFunctionCall(Oid functionId, Datum val)
#define PG_GETARG_TEXT_PP(n)
#define PG_RETURN_BYTEA_P(x)
#define DatumGetByteaPP(X)
#define PG_GETARG_POINTER(n)
#define PG_RETURN_CSTRING(x)
#define DirectFunctionCall1(func, arg1)
#define PG_GETARG_CSTRING(n)
#define PG_GETARG_NAME(n)
#define PG_RETURN_TEXT_P(x)
#define PG_GETARG_INT32(n)
#define PG_GETARG_BOOL(n)
#define PG_RETURN_DATUM(x)
#define PG_RETURN_BOOL(x)
#define HeapTupleIsValid(tuple)
if(TABLE==NULL||TABLE_index==NULL)
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
List * lappend_oid(List *list, Oid datum)
List * list_append_unique_oid(List *list, Oid datum)
char * get_namespace_name(Oid nspid)
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
char * get_rel_name(Oid relid)
char get_typtype(Oid typid)
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Oid getBaseType(Oid typid)
void get_type_category_preferred(Oid typid, char *typcategory, bool *typispreferred)
#define type_is_array_domain(typid)
unsigned char * pg_do_encoding_conversion(unsigned char *src, int len, int src_encoding, int dest_encoding)
char * pg_any_to_server(const char *s, int len, int encoding)
int GetDatabaseEncoding(void)
void pg_unicode_to_server(pg_wchar c, unsigned char *s)
int pg_get_client_encoding(void)
int pg_encoding_mb2wchar_with_len(int encoding, const char *from, pg_wchar *to, int len)
char * pg_server_to_any(const char *s, int len, int encoding)
int pg_mblen(const char *mbstr)
char * pstrdup(const char *in)
void pfree(void *pointer)
MemoryContext TopMemoryContext
void * palloc0(Size size)
MemoryContext CurrentMemoryContext
void * repalloc(void *pointer, Size size)
void * MemoryContextAlloc(MemoryContext context, Size size)
char * MemoryContextStrdup(MemoryContext context, const char *string)
#define AllocSetContextCreate
#define ALLOCSET_DEFAULT_SIZES
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
Oid exprType(const Node *expr)
#define IsA(nodeptr, _type_)
FormData_pg_attribute * Form_pg_attribute
FormData_pg_class * Form_pg_class
#define forboth(cell1, list1, cell2, list2)
#define list_make2(x1, x2)
FormData_pg_type * Form_pg_type
#define MAX_MULTIBYTE_CHAR_LEN
#define MAX_UNICODE_EQUIVALENT_STRING
#define pg_encoding_to_char
#define pg_char_to_encoding
int pg_strcasecmp(const char *s1, const char *s2)
size_t strnlen(const char *str, size_t maxlen)
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
static bool DatumGetBool(Datum X)
static Datum PointerGetDatum(const void *X)
static char * DatumGetCString(Datum X)
static Oid DatumGetObjectId(Datum X)
static Datum BoolGetDatum(bool X)
static Datum ObjectIdGetDatum(Oid X)
static Datum CStringGetDatum(const char *X)
Datum regclassout(PG_FUNCTION_ARGS)
static pg_noinline void Size size
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
SPITupleTable * SPI_tuptable
Portal SPI_cursor_find(const char *name)
const char * SPI_result_code_string(int code)
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
void SPI_cursor_close(Portal portal)
void * SPI_palloc(Size size)
char * SPI_fname(TupleDesc tupdesc, int fnumber)
int SPI_execute(const char *src, bool read_only, long tcount)
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
void destroyStringInfo(StringInfo str)
StringInfo makeStringInfo(void)
void appendStringInfo(StringInfo str, const char *fmt,...)
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
void appendStringInfoString(StringInfo str, const char *s)
void appendStringInfoChar(StringInfo str, char ch)
void initStringInfo(StringInfo str)
StringInfoData * StringInfo
#define appendStringInfoCharMacro(str, ch)
void(* InitOpaque)(struct TableFuncScanState *state, int natts)
void ReleaseSysCache(HeapTuple tuple)
HeapTuple SearchSysCache1(int cacheId, Datum key1)
void table_close(Relation relation, LOCKMODE lockmode)
Relation table_open(Oid relationId, LOCKMODE lockmode)
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
struct TupleDescData * TupleDesc
#define TupleDescAttr(tupdesc, i)
static Timestamp DatumGetTimestamp(Datum X)
#define SET_VARSIZE(PTR, len)
#define VARSIZE_ANY_EXHDR(PTR)
static void appendStringInfoText(StringInfo str, const text *t)
char * text_to_cstring(const text *t)
text * cstring_to_text_with_len(const char *s, int len)
text * cstring_to_text(const char *s)
int pg_encoding_mblen(int encoding, const char *mbstr)
Datum xml_in(PG_FUNCTION_ARGS)
Datum cursor_to_xmlschema(PG_FUNCTION_ARGS)
static char * _SPI_strdup(const char *s)
xmltype * xmlconcat(List *args)
Datum table_to_xml(PG_FUNCTION_ARGS)
Datum query_to_xmlschema(PG_FUNCTION_ARGS)
Datum database_to_xml(PG_FUNCTION_ARGS)
static List * database_get_xml_visible_tables(void)
static const char * map_sql_catalog_to_xmlschema_types(List *nspid_list, bool nulls, bool tableforest, const char *targetns)
static char * map_multipart_sql_identifier_to_xml_name(const char *a, const char *b, const char *c, const char *d)
static void XmlTableInitOpaque(struct TableFuncScanState *state, int natts)
static const char * map_sql_type_to_xml_name(Oid typeoid, int typmod)
static const char * map_sql_type_to_xmlschema_type(Oid typeoid, int typmod)
Datum texttoxml(PG_FUNCTION_ARGS)
static void xsd_schema_element_start(StringInfo result, const char *targetns)
Datum query_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
Datum xmltotext(PG_FUNCTION_ARGS)
Datum schema_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
Datum xmlexists(PG_FUNCTION_ARGS)
Datum xmltext(PG_FUNCTION_ARGS)
static List * query_to_oid_list(const char *query)
static char * xml_out_internal(xmltype *x, pg_enc target_encoding)
static xmltype * stringinfo_to_xmltype(StringInfo buf)
static StringInfo database_to_xmlschema_internal(bool nulls, bool tableforest, const char *targetns)
Datum database_to_xmlschema(PG_FUNCTION_ARGS)
Datum schema_to_xmlschema(PG_FUNCTION_ARGS)
static void xmldata_root_element_start(StringInfo result, const char *eltname, const char *xmlschema, const char *targetns, bool top_level)
Datum xml_send(PG_FUNCTION_ARGS)
static const char * map_sql_schema_to_xmlschema_types(Oid nspid, List *relid_list, bool nulls, bool tableforest, const char *targetns)
const TableFuncRoutine XmlTableRoutine
Datum xmlcomment(PG_FUNCTION_ARGS)
static void XmlTableSetNamespace(struct TableFuncScanState *state, const char *name, const char *uri)
Datum xmlconcat2(PG_FUNCTION_ARGS)
static void XmlTableSetRowFilter(struct TableFuncScanState *state, const char *path)
static void xmldata_root_element_end(StringInfo result, const char *eltname)
text * xmltotext_with_options(xmltype *data, XmlOptionType xmloption_arg, bool indent)
xmltype * xmlparse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace)
static Datum XmlTableGetValue(struct TableFuncScanState *state, int colnum, Oid typid, int32 typmod, bool *isnull)
static xmltype * cstring_to_xmltype(const char *string)
Datum xml_is_well_formed_document(PG_FUNCTION_ARGS)
Datum query_to_xml(PG_FUNCTION_ARGS)
char * map_sql_identifier_to_xml_name(const char *ident, bool fully_escaped, bool escape_period)
static StringInfo database_to_xml_internal(const char *xmlschema, bool nulls, bool tableforest, const char *targetns)
Datum xml_is_well_formed_content(PG_FUNCTION_ARGS)
#define XML_VISIBLE_SCHEMAS
bool xml_is_document(xmltype *arg)
static StringInfo schema_to_xmlschema_internal(const char *schemaname, bool nulls, bool tableforest, const char *targetns)
static List * schema_get_xml_visible_tables(Oid nspid)
char * escape_xml(const char *str)
Datum table_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
Datum xmlvalidate(PG_FUNCTION_ARGS)
static void XmlTableSetDocument(struct TableFuncScanState *state, Datum value)
static void XmlTableDestroyOpaque(struct TableFuncScanState *state)
char * map_xml_name_to_sql_identifier(const char *name)
static const char * map_sql_typecoll_to_xmlschema_types(List *tupdesc_list)
xmltype * xmlpi(const char *target, text *arg, bool arg_is_null, bool *result_is_null)
#define PG_XML_DEFAULT_VERSION
Datum table_to_xmlschema(PG_FUNCTION_ARGS)
static StringInfo query_to_xml_internal(const char *query, char *tablename, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
static void SPI_sql_row_to_xmlelement(uint64 rownum, StringInfo result, char *tablename, bool nulls, bool tableforest, const char *targetns, bool top_level)
static StringInfo schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
static StringInfo table_to_xml_internal(Oid relid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Datum schema_to_xml(PG_FUNCTION_ARGS)
Datum database_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
char * map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
static bool XmlTableFetchRow(struct TableFuncScanState *state)
Datum cursor_to_xml(PG_FUNCTION_ARGS)
Datum xpath_exists(PG_FUNCTION_ARGS)
Datum xml_is_well_formed(PG_FUNCTION_ARGS)
xmltype * xmlelement(XmlExpr *xexpr, Datum *named_argvalue, bool *named_argnull, Datum *argvalue, bool *argnull)
static void XmlTableSetColumnFilter(struct TableFuncScanState *state, const char *path, int colnum)
static const char * map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls, bool tableforest, const char *targetns)
Datum xml_out(PG_FUNCTION_ARGS)
Datum xml_recv(PG_FUNCTION_ARGS)
xmltype * xmlroot(xmltype *data, text *version, int standalone)
Datum xpath(PG_FUNCTION_ARGS)
static void xsd_schema_element_end(StringInfo result)
static List * database_get_xml_visible_schemas(void)
@ XML_STANDALONE_NO_VALUE
struct PgXmlErrorContext PgXmlErrorContext
static xmltype * DatumGetXmlP(Datum X)
#define PG_RETURN_XML_P(x)
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)
PgXmlErrorContext * pg_xml_init(PgXmlStrictness strictness)
#define PG_GETARG_XML_P(n)
void pg_xml_init_library(void)
@ PG_XML_STRICTNESS_LEGACY
@ PG_XML_STRICTNESS_WELLFORMED