PostgreSQL Source Code git master
Loading...
Searching...
No Matches
xml.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * xml.c
4 * XML data type support.
5 *
6 *
7 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
9 *
10 * src/backend/utils/adt/xml.c
11 *
12 *-------------------------------------------------------------------------
13 */
14
15/*
16 * Generally, XML type support is only available when libxml use was
17 * configured during the build. But even if that is not done, the
18 * type and all the functions are available, but most of them will
19 * fail. For one thing, this avoids having to manage variant catalog
20 * installations. But it also has nice effects such as that you can
21 * dump a database containing XML type data even if the server is not
22 * linked with libxml. Thus, make sure xml_out() works even if nothing
23 * else does.
24 */
25
26/*
27 * Notes on memory management:
28 *
29 * Sometimes libxml allocates global structures in the hope that it can reuse
30 * them later on. This makes it impractical to change the xmlMemSetup
31 * functions on-the-fly; that is likely to lead to trying to pfree() chunks
32 * allocated with malloc() or vice versa. Since libxml might be used by
33 * loadable modules, eg libperl, our only safe choices are to change the
34 * functions at postmaster/backend launch or not at all. Since we'd rather
35 * not activate libxml in sessions that might never use it, the latter choice
36 * is the preferred one. However, for debugging purposes it can be awfully
37 * handy to constrain libxml's allocations to be done in a specific palloc
38 * context, where they're easy to track. Therefore there is code here that
39 * can be enabled in debug builds to redirect libxml's allocations into a
40 * special context LibxmlContext. It's not recommended to turn this on in
41 * a production build because of the possibility of bad interactions with
42 * external modules.
43 */
44/* #define USE_LIBXMLCONTEXT */
45
46#include "postgres.h"
47
48#ifdef USE_LIBXML
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>
61
62/*
63 * We used to check for xmlStructuredErrorContext via a configure test; but
64 * that doesn't work on Windows, so instead use this grottier method of
65 * testing the library version number.
66 */
67#if LIBXML_VERSION >= 20704
68#define HAVE_XMLSTRUCTUREDERRORCONTEXT 1
69#endif
70
71/*
72 * libxml2 2.12 decided to insert "const" into the error handler API.
73 */
74#if LIBXML_VERSION >= 21200
75#define PgXmlErrorPtr const xmlError *
76#else
77#define PgXmlErrorPtr xmlErrorPtr
78#endif
79
80#endif /* USE_LIBXML */
81
82#include "access/htup_details.h"
83#include "access/table.h"
84#include "catalog/namespace.h"
85#include "catalog/pg_class.h"
86#include "catalog/pg_type.h"
87#include "executor/spi.h"
88#include "executor/tablefunc.h"
89#include "fmgr.h"
90#include "lib/stringinfo.h"
91#include "libpq/pqformat.h"
92#include "mb/pg_wchar.h"
93#include "miscadmin.h"
94#include "nodes/execnodes.h"
95#include "nodes/miscnodes.h"
96#include "nodes/nodeFuncs.h"
97#include "utils/array.h"
98#include "utils/builtins.h"
99#include "utils/date.h"
100#include "utils/datetime.h"
101#include "utils/lsyscache.h"
102#include "utils/rel.h"
103#include "utils/syscache.h"
104#include "utils/xml.h"
105
106
107/* GUC variables */
110
111#ifdef USE_LIBXML
112
113/* random number to identify PgXmlErrorContext */
114#define ERRCXT_MAGIC 68275028
115
117{
118 int magic;
119 /* strictness argument passed to pg_xml_init */
121 /* current error status and accumulated message, if any */
122 bool err_occurred;
124 /* previous libxml error handling state (saved by pg_xml_init) */
126 void *saved_errcxt;
127 /* previous libxml entity handler (saved by pg_xml_init) */
129};
130
131static xmlParserInputPtr xmlPgEntityLoader(const char *URL, const char *ID,
132 xmlParserCtxtPtr ctxt);
133static void xml_errsave(Node *escontext, PgXmlErrorContext *errcxt,
134 int sqlcode, const char *msg);
135static void xml_errorHandler(void *data, PgXmlErrorPtr error);
136static int errdetail_for_xml_code(int code);
139
140#ifdef USE_LIBXMLCONTEXT
141
143
144static void xml_memory_init(void);
145static void *xml_palloc(size_t size);
146static void *xml_repalloc(void *ptr, size_t size);
147static void xml_pfree(void *ptr);
148static char *xml_pstrdup(const char *string);
149#endif /* USE_LIBXMLCONTEXT */
150
151static xmlChar *xml_text2xmlChar(text *in);
152static int parse_xml_decl(const xmlChar *str, size_t *lenp,
153 xmlChar **version, xmlChar **encoding, int *standalone);
154static bool print_xml_decl(StringInfo buf, const xmlChar *version,
156static bool xml_doctype_in_content(const xmlChar *str);
161 Node *escontext);
164 ArrayBuildState *astate,
166static xmlChar *pg_xmlCharStrndup(const char *str, size_t len);
167#endif /* USE_LIBXML */
168
169static void xmldata_root_element_start(StringInfo result, const char *eltname,
170 const char *xmlschema, const char *targetns,
171 bool top_level);
172static void xmldata_root_element_end(StringInfo result, const char *eltname);
173static StringInfo query_to_xml_internal(const char *query, char *tablename,
174 const char *xmlschema, bool nulls, bool tableforest,
175 const char *targetns, bool top_level);
176static const char *map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid,
177 bool nulls, bool tableforest, const char *targetns);
179 List *relid_list, bool nulls,
180 bool tableforest, const char *targetns);
182 bool nulls, bool tableforest,
183 const char *targetns);
184static const char *map_sql_type_to_xml_name(Oid typeoid, int typmod);
186static const char *map_sql_type_to_xmlschema_type(Oid typeoid, int typmod);
188 char *tablename, bool nulls, bool tableforest,
189 const char *targetns, bool top_level);
190
191/* XMLTABLE support */
192#ifdef USE_LIBXML
193/* random number to identify XmlTableContext */
194#define XMLTABLE_CONTEXT_MAGIC 46922182
195typedef struct XmlTableBuilderData
196{
197 int magic;
198 int natts;
199 long int row_count;
201 xmlParserCtxtPtr ctxt;
208#endif
209
210static void XmlTableInitOpaque(struct TableFuncScanState *state, int natts);
212static void XmlTableSetNamespace(struct TableFuncScanState *state, const char *name,
213 const char *uri);
214static void XmlTableSetRowFilter(struct TableFuncScanState *state, const char *path);
216 const char *path, int colnum);
217static bool XmlTableFetchRow(struct TableFuncScanState *state);
218static Datum XmlTableGetValue(struct TableFuncScanState *state, int colnum,
219 Oid typid, int32 typmod, bool *isnull);
221
223{
225 .SetDocument = XmlTableSetDocument,
226 .SetNamespace = XmlTableSetNamespace,
227 .SetRowFilter = XmlTableSetRowFilter,
228 .SetColumnFilter = XmlTableSetColumnFilter,
229 .FetchRow = XmlTableFetchRow,
230 .GetValue = XmlTableGetValue,
231 .DestroyOpaque = XmlTableDestroyOpaque
232};
233
234#define NO_XML_SUPPORT() \
235 ereport(ERROR, \
236 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
237 errmsg("unsupported XML feature"), \
238 errdetail("This functionality requires the server to be built with libxml support.")))
239
240
241/* from SQL/XML:2008 section 4.9 */
242#define NAMESPACE_XSD "http://www.w3.org/2001/XMLSchema"
243#define NAMESPACE_XSI "http://www.w3.org/2001/XMLSchema-instance"
244#define NAMESPACE_SQLXML "http://standards.iso.org/iso/9075/2003/sqlxml"
245
246
247#ifdef USE_LIBXML
248
249static int
251{
252 int encoding = pg_char_to_encoding((const char *) encoding_name);
253
254 if (encoding < 0)
257 errmsg("invalid encoding name \"%s\"",
258 (const char *) encoding_name)));
259 return encoding;
260}
261#endif
262
263
264/*
265 * xml_in uses a plain C string to VARDATA conversion, so for the time being
266 * we use the conversion function for the text datatype.
267 *
268 * This is only acceptable so long as xmltype and text use the same
269 * representation.
270 */
271Datum
273{
274#ifdef USE_LIBXML
275 char *s = PG_GETARG_CSTRING(0);
278
279 /* Build the result object. */
281
282 /*
283 * Parse the data to check if it is well-formed XML data.
284 *
285 * Note: we don't need to worry about whether a soft error is detected.
286 */
288 NULL, NULL, fcinfo->context);
289 if (doc != NULL)
291
293#else
295 return 0;
296#endif
297}
298
299
300#define PG_XML_DEFAULT_VERSION "1.0"
301
302
303/*
304 * xml_out_internal uses a plain VARDATA to C string conversion, so for the
305 * time being we use the conversion function for the text datatype.
306 *
307 * This is only acceptable so long as xmltype and text use the same
308 * representation.
309 */
310static char *
312{
313 char *str = text_to_cstring((text *) x);
314
315#ifdef USE_LIBXML
316 size_t len = strlen(str);
317 xmlChar *version;
318 int standalone;
319 int res_code;
320
322 &len, &version, NULL, &standalone)) == 0)
323 {
325
327
329 {
330 /*
331 * If we are not going to produce an XML declaration, eat a single
332 * newline in the original string to prevent empty first lines in
333 * the output.
334 */
335 if (*(str + len) == '\n')
336 len += 1;
337 }
339
340 pfree(str);
341
342 return buf.data;
343 }
344
347 errmsg_internal("could not parse XML declaration in stored value"),
349#endif
350 return str;
351}
352
353
354Datum
356{
358
359 /*
360 * xml_out removes the encoding property in all cases. This is because we
361 * cannot control from here whether the datum will be converted to a
362 * different client encoding, so we'd do more harm than good by including
363 * it.
364 */
366}
367
368
369Datum
371{
372#ifdef USE_LIBXML
375 const char *input;
376 char *str;
377 char *newstr;
378 int nbytes;
381 int encoding;
382
383 /*
384 * Read the data in raw format. We don't know yet what the encoding is, as
385 * that information is embedded in the xml declaration; so we have to
386 * parse that before converting to server encoding.
387 */
388 nbytes = buf->len - buf->cursor;
389 input = pq_getmsgbytes(buf, nbytes);
390
391 /*
392 * We need a null-terminated string to pass to parse_xml_decl(). Rather
393 * than make a separate copy, make the temporary result one byte bigger
394 * than it needs to be.
395 */
396 result = palloc(nbytes + 1 + VARHDRSZ);
397 SET_VARSIZE(result, nbytes + VARHDRSZ);
398 memcpy(VARDATA(result), input, nbytes);
399 str = VARDATA(result);
400 str[nbytes] = '\0';
401
403
404 /*
405 * If encoding wasn't explicitly specified in the XML header, treat it as
406 * UTF-8, as that's the default in XML. This is different from xml_in(),
407 * where the input has to go through the normal client to server encoding
408 * conversion.
409 */
411
412 /*
413 * Parse the data to check if it is well-formed XML data. Assume that
414 * xml_parse will throw ERROR if not.
415 */
418
419 /* Now that we know what we're dealing with, convert to server encoding */
421
422 if (newstr != str)
423 {
424 pfree(result);
426 pfree(newstr);
427 }
428
430#else
432 return 0;
433#endif
434}
435
436
437Datum
439{
441 char *outval;
443
444 /*
445 * xml_out_internal doesn't convert the encoding, it just prints the right
446 * declaration. pq_sendtext will do the conversion.
447 */
449
452 pfree(outval);
454}
455
456
457#ifdef USE_LIBXML
458static void
460{
462}
463#endif
464
465
466static xmltype *
471
472
473static xmltype *
474cstring_to_xmltype(const char *string)
475{
476 return (xmltype *) cstring_to_text(string);
477}
478
479
480#ifdef USE_LIBXML
481static xmltype *
483{
484 return (xmltype *) cstring_to_text_with_len((const char *) xmlBufferContent(buf),
486}
487#endif
488
489
490Datum
492{
493#ifdef USE_LIBXML
495 char *argdata = VARDATA_ANY(arg);
498 int i;
499
500 /* check for "--" in string or "-" at the end */
501 for (i = 1; i < len; i++)
502 {
503 if (argdata[i] == '-' && argdata[i - 1] == '-')
506 errmsg("invalid XML comment")));
507 }
508 if (len > 0 && argdata[len - 1] == '-')
511 errmsg("invalid XML comment")));
512
514 appendStringInfoString(&buf, "<!--");
517
519#else
521 return 0;
522#endif
523}
524
525
526Datum
528{
529#ifdef USE_LIBXML
531 text *result;
532 xmlChar *volatile xmlbuf = NULL;
534
535 /* First we gotta spin up some error handling. */
537
538 PG_TRY();
539 {
541
542 if (xmlbuf == NULL || xmlerrcxt->err_occurred)
544 "could not allocate xmlChar");
545
546 result = cstring_to_text_with_len((const char *) xmlbuf,
548 }
549 PG_CATCH();
550 {
551 if (xmlbuf)
553
554 pg_xml_done(xmlerrcxt, true);
555 PG_RE_THROW();
556 }
557 PG_END_TRY();
558
560 pg_xml_done(xmlerrcxt, false);
561
563#else
565 return 0;
566#endif /* not USE_LIBXML */
567}
568
569
570/*
571 * TODO: xmlconcat needs to merge the notations and unparsed entities
572 * of the argument values. Not very important in practice, though.
573 */
574xmltype *
576{
577#ifdef USE_LIBXML
578 int global_standalone = 1;
580 bool global_version_no_value = false;
582 ListCell *v;
583
585 foreach(v, args)
586 {
588 size_t len;
589 xmlChar *version;
590 int standalone;
591 char *str;
592
593 len = VARSIZE(x) - VARHDRSZ;
594 str = text_to_cstring((text *) x);
595
596 parse_xml_decl((xmlChar *) str, &len, &version, NULL, &standalone);
597
598 if (standalone == 0 && global_standalone == 1)
600 if (standalone < 0)
602
603 if (!version)
605 else if (!global_version)
606 global_version = version;
607 else if (xmlStrcmp(version, global_version) != 0)
609
611 pfree(str);
612 }
613
615 {
616 StringInfoData buf2;
617
618 initStringInfo(&buf2);
619
620 print_xml_decl(&buf2,
622 0,
624
625 appendBinaryStringInfo(&buf2, buf.data, buf.len);
626 buf = buf2;
627 }
628
629 return stringinfo_to_xmltype(&buf);
630#else
632 return NULL;
633#endif
634}
635
636
637/*
638 * XMLAGG support
639 */
640Datum
642{
643 if (PG_ARGISNULL(0))
644 {
645 if (PG_ARGISNULL(1))
647 else
649 }
650 else if (PG_ARGISNULL(1))
652 else
654 PG_GETARG_XML_P(1))));
655}
656
657
658Datum
660{
662
663 PG_RETURN_XML_P(xmlparse(data, xmloption, true, fcinfo->context));
664}
665
666
667Datum
669{
671
672 /* It's actually binary compatible. */
674}
675
676
677text *
679{
680#ifdef USE_LIBXML
681 text *volatile result;
685 volatile xmlBufferPtr buf = NULL;
686 volatile xmlSaveCtxtPtr ctxt = NULL;
688 PgXmlErrorContext *volatile xmlerrcxt = NULL;
689#endif
690
691 if (xmloption_arg != XMLOPTION_DOCUMENT && !indent)
692 {
693 /*
694 * We don't actually need to do anything, so just return the
695 * binary-compatible input. For backwards-compatibility reasons,
696 * allow such cases to succeed even without USE_LIBXML.
697 */
698 return (text *) data;
699 }
700
701#ifdef USE_LIBXML
702
703 /*
704 * Parse the input according to the xmloption.
705 *
706 * preserve_whitespace is set to false in case we are indenting, otherwise
707 * libxml2 will fail to indent elements that have whitespace between them.
708 */
711 (Node *) &escontext);
712 if (doc == NULL || escontext.error_occurred)
713 {
714 if (doc)
716 /* A soft error must be failure to conform to XMLOPTION_DOCUMENT */
719 errmsg("not an XML document")));
720 }
721
722 /* If we weren't asked to indent, we're done. */
723 if (!indent)
724 {
726 return (text *) data;
727 }
728
729 /*
730 * Otherwise, we gotta spin up some error handling. Unlike most other
731 * routines in this module, we already have a libxml "doc" structure to
732 * free, so we need to call pg_xml_init() inside the PG_TRY and be
733 * prepared for it to fail (typically due to palloc OOM).
734 */
735 PG_TRY();
736 {
737 size_t decl_len = 0;
738
740
741 /* The serialized data will go into this buffer. */
743
744 if (buf == NULL || xmlerrcxt->err_occurred)
746 "could not allocate xmlBuffer");
747
748 /* Detect whether there's an XML declaration */
750
751 /*
752 * Emit declaration only if the input had one. Note: some versions of
753 * xmlSaveToBuffer leak memory if a non-null encoding argument is
754 * passed, so don't do that. We don't want any encoding conversion
755 * anyway.
756 */
757 if (decl_len == 0)
758 ctxt = xmlSaveToBuffer(buf, NULL,
760 else
761 ctxt = xmlSaveToBuffer(buf, NULL,
763
764 if (ctxt == NULL || xmlerrcxt->err_occurred)
766 "could not allocate xmlSaveCtxt");
767
769 {
770 /* If it's a document, saving is easy. */
771 if (xmlSaveDoc(ctxt, doc) == -1 || xmlerrcxt->err_occurred)
773 "could not save document to xmlBuffer");
774 }
775 else if (content_nodes != NULL)
776 {
777 /*
778 * Deal with the case where we have non-singly-rooted XML.
779 * libxml's dump functions don't work well for that without help.
780 * We build a fake root node that serves as a container for the
781 * content nodes, and then iterate over the nodes.
782 */
786
787 root = xmlNewNode(NULL, (const xmlChar *) "content-root");
788 if (root == NULL || xmlerrcxt->err_occurred)
790 "could not allocate xml node");
791
792 /*
793 * This attaches root to doc, so we need not free it separately...
794 * but instead, we have to free the old root if there was one.
795 */
797 if (oldroot != NULL)
799
801 xmlerrcxt->err_occurred)
803 "could not append xml node list");
804
805 /*
806 * We use this node to insert newlines in the dump. Note: in at
807 * least some libxml versions, xmlNewDocText would not attach the
808 * node to the document even if we passed it. Therefore, manage
809 * freeing of this node manually, and pass NULL here to make sure
810 * there's not a dangling link.
811 */
812 newline = xmlNewDocText(NULL, (const xmlChar *) "\n");
813 if (newline == NULL || xmlerrcxt->err_occurred)
815 "could not allocate xml node");
816
817 for (xmlNodePtr node = root->children; node; node = node->next)
818 {
819 /* insert newlines between nodes */
820 if (node->type != XML_TEXT_NODE && node->prev != NULL)
821 {
822 if (xmlSaveTree(ctxt, newline) == -1 || xmlerrcxt->err_occurred)
823 {
826 "could not save newline to xmlBuffer");
827 }
828 }
829
830 if (xmlSaveTree(ctxt, node) == -1 || xmlerrcxt->err_occurred)
831 {
834 "could not save content to xmlBuffer");
835 }
836 }
837
839 }
840
841 if (xmlSaveClose(ctxt) == -1 || xmlerrcxt->err_occurred)
842 {
843 ctxt = NULL; /* don't try to close it again */
845 "could not close xmlSaveCtxtPtr");
846 }
847
848 /*
849 * xmlDocContentDumpOutput may add a trailing newline, so remove that.
850 */
852 {
853 const char *str = (const char *) xmlBufferContent(buf);
854 int len = xmlBufferLength(buf);
855
856 while (len > 0 && (str[len - 1] == '\n' ||
857 str[len - 1] == '\r'))
858 len--;
859
861 }
862 else
864 }
865 PG_CATCH();
866 {
867 if (ctxt)
868 xmlSaveClose(ctxt);
869 if (buf)
872
873 if (xmlerrcxt)
874 pg_xml_done(xmlerrcxt, true);
875
876 PG_RE_THROW();
877 }
878 PG_END_TRY();
879
882
883 pg_xml_done(xmlerrcxt, false);
884
885 return result;
886#else
888 return NULL;
889#endif
890}
891
892
893xmltype *
895 const Datum *named_argvalue, const bool *named_argnull,
896 const Datum *argvalue, const bool *argnull)
897{
898#ifdef USE_LIBXML
902 int i;
903 ListCell *arg;
904 ListCell *narg;
906 volatile xmlBufferPtr buf = NULL;
907 volatile xmlTextWriterPtr writer = NULL;
908
909 /*
910 * All arguments are already evaluated, and their values are passed in the
911 * named_argvalue/named_argnull or argvalue/argnull arrays. This avoids
912 * issues if one of the arguments involves a call to some other function
913 * or subsystem that wants to use libxml on its own terms. We examine the
914 * original XmlExpr to identify the numbers and types of the arguments.
915 */
917 i = 0;
918 foreach(arg, xexpr->named_args)
919 {
920 Expr *e = (Expr *) lfirst(arg);
921 char *str;
922
923 if (named_argnull[i])
924 str = NULL;
925 else
926 str = map_sql_value_to_xml_value(named_argvalue[i],
927 exprType((Node *) e),
928 false);
930 i++;
931 }
932
934 i = 0;
935 foreach(arg, xexpr->args)
936 {
937 Expr *e = (Expr *) lfirst(arg);
938 char *str;
939
940 /* here we can just forget NULL elements immediately */
941 if (!argnull[i])
942 {
943 str = map_sql_value_to_xml_value(argvalue[i],
944 exprType((Node *) e),
945 true);
947 }
948 i++;
949 }
950
952
953 PG_TRY();
954 {
956 if (buf == NULL || xmlerrcxt->err_occurred)
958 "could not allocate xmlBuffer");
960 if (writer == NULL || xmlerrcxt->err_occurred)
962 "could not allocate xmlTextWriter");
963
964 if (xmlTextWriterStartElement(writer, (xmlChar *) xexpr->name) < 0 ||
965 xmlerrcxt->err_occurred)
967 "could not start xml element");
968
969 forboth(arg, named_arg_strings, narg, xexpr->arg_names)
970 {
971 char *str = (char *) lfirst(arg);
972 char *argname = strVal(lfirst(narg));
973
974 if (str)
975 {
977 (xmlChar *) argname,
978 (xmlChar *) str) < 0 ||
979 xmlerrcxt->err_occurred)
981 "could not write xml attribute");
982 }
983 }
984
985 foreach(arg, arg_strings)
986 {
987 char *str = (char *) lfirst(arg);
988
989 if (xmlTextWriterWriteRaw(writer, (xmlChar *) str) < 0 ||
990 xmlerrcxt->err_occurred)
992 "could not write raw xml text");
993 }
994
996 xmlerrcxt->err_occurred)
998 "could not end xml element");
999
1000 /* we MUST do this now to flush data out to the buffer ... */
1002 writer = NULL;
1003
1005 }
1006 PG_CATCH();
1007 {
1008 if (writer)
1010 if (buf)
1012
1013 pg_xml_done(xmlerrcxt, true);
1014
1015 PG_RE_THROW();
1016 }
1017 PG_END_TRY();
1018
1020
1021 pg_xml_done(xmlerrcxt, false);
1022
1023 return result;
1024#else
1026 return NULL;
1027#endif
1028}
1029
1030
1031xmltype *
1033{
1034#ifdef USE_LIBXML
1035 xmlDocPtr doc;
1036
1038 GetDatabaseEncoding(), NULL, NULL, escontext);
1039 if (doc)
1040 xmlFreeDoc(doc);
1041
1042 if (SOFT_ERROR_OCCURRED(escontext))
1043 return NULL;
1044
1045 return (xmltype *) data;
1046#else
1048 return NULL;
1049#endif
1050}
1051
1052
1053xmltype *
1054xmlpi(const char *target, text *arg, bool arg_is_null, bool *result_is_null)
1055{
1056#ifdef USE_LIBXML
1057 xmltype *result;
1059
1060 if (pg_strcasecmp(target, "xml") == 0)
1061 ereport(ERROR,
1063 errmsg("invalid XML processing instruction"),
1064 errdetail("XML processing instruction target name cannot be \"%s\".", target)));
1065
1066 /*
1067 * Following the SQL standard, the null check comes after the syntax check
1068 * above.
1069 */
1071 if (*result_is_null)
1072 return NULL;
1073
1075
1076 appendStringInfo(&buf, "<?%s", target);
1077
1078 if (arg != NULL)
1079 {
1080 char *string;
1081
1082 string = text_to_cstring(arg);
1083 if (strstr(string, "?>") != NULL)
1084 ereport(ERROR,
1086 errmsg("invalid XML processing instruction"),
1087 errdetail("XML processing instruction cannot contain \"?>\".")));
1088
1090 appendStringInfoString(&buf, string + strspn(string, " "));
1091 pfree(string);
1092 }
1094
1096 pfree(buf.data);
1097 return result;
1098#else
1100 return NULL;
1101#endif
1102}
1103
1104
1105xmltype *
1107{
1108#ifdef USE_LIBXML
1109 char *str;
1110 size_t len;
1112 int orig_standalone;
1114
1115 len = VARSIZE(data) - VARHDRSZ;
1117
1119
1120 if (version)
1121 orig_version = xml_text2xmlChar(version);
1122 else
1124
1125 switch (standalone)
1126 {
1127 case XML_STANDALONE_YES:
1128 orig_standalone = 1;
1129 break;
1130 case XML_STANDALONE_NO:
1131 orig_standalone = 0;
1132 break;
1134 orig_standalone = -1;
1135 break;
1137 /* leave original value */
1138 break;
1139 }
1140
1144
1145 return stringinfo_to_xmltype(&buf);
1146#else
1148 return NULL;
1149#endif
1150}
1151
1152
1153/*
1154 * Validate document (given as string) against DTD (given as external link)
1155 *
1156 * This has been removed because it is a security hole: unprivileged users
1157 * should not be able to use Postgres to fetch arbitrary external files,
1158 * which unfortunately is exactly what libxml is willing to do with the DTD
1159 * parameter.
1160 */
1161Datum
1163{
1164 ereport(ERROR,
1166 errmsg("xmlvalidate is not implemented")));
1167 return 0;
1168}
1169
1170
1171bool
1173{
1174#ifdef USE_LIBXML
1175 xmlDocPtr doc;
1177
1178 /*
1179 * We'll report "true" if no soft error is reported by xml_parse().
1180 */
1182 GetDatabaseEncoding(), NULL, NULL, (Node *) &escontext);
1183 if (doc)
1184 xmlFreeDoc(doc);
1185
1186 return !escontext.error_occurred;
1187#else /* not USE_LIBXML */
1189 return false;
1190#endif /* not USE_LIBXML */
1191}
1192
1193
1194#ifdef USE_LIBXML
1195
1196/*
1197 * pg_xml_init_library --- set up for use of libxml
1198 *
1199 * This should be called by each function that is about to use libxml
1200 * facilities but doesn't require error handling. It initializes libxml
1201 * and verifies compatibility with the loaded libxml version. These are
1202 * once-per-session activities.
1203 *
1204 * TODO: xmlChar is utf8-char, make proper tuning (initdb with enc!=utf8 and
1205 * check)
1206 */
1207void
1209{
1210 static bool first_time = true;
1211
1212 if (first_time)
1213 {
1214 /* Stuff we need do only once per session */
1215
1216 /*
1217 * Currently, we have no pure UTF-8 support for internals -- check if
1218 * we can work.
1219 */
1220 if (sizeof(char) != sizeof(xmlChar))
1221 ereport(ERROR,
1222 (errmsg("could not initialize XML library"),
1223 errdetail("libxml2 has incompatible char type: sizeof(char)=%zu, sizeof(xmlChar)=%zu.",
1224 sizeof(char), sizeof(xmlChar))));
1225
1226#ifdef USE_LIBXMLCONTEXT
1227 /* Set up libxml's memory allocation our way */
1229#endif
1230
1231 /* Check library compatibility */
1233
1234 first_time = false;
1235 }
1236}
1237
1238/*
1239 * pg_xml_init --- set up for use of libxml and register an error handler
1240 *
1241 * This should be called by each function that is about to use libxml
1242 * facilities and requires error handling. It initializes libxml with
1243 * pg_xml_init_library() and establishes our libxml error handler.
1244 *
1245 * strictness determines which errors are reported and which are ignored.
1246 *
1247 * Calls to this function MUST be followed by a PG_TRY block that guarantees
1248 * that pg_xml_done() is called during either normal or error exit.
1249 *
1250 * This is exported for use by contrib/xml2, as well as other code that might
1251 * wish to share use of this module's libxml error handler.
1252 */
1255{
1257 void *new_errcxt;
1258
1259 /* Do one-time setup if needed */
1261
1262 /* Create error handling context structure */
1264 errcxt->magic = ERRCXT_MAGIC;
1265 errcxt->strictness = strictness;
1266 errcxt->err_occurred = false;
1267 initStringInfo(&errcxt->err_buf);
1268
1269 /*
1270 * Save original error handler and install ours. libxml originally didn't
1271 * distinguish between the contexts for generic and for structured error
1272 * handlers. If we're using an old libxml version, we must thus save the
1273 * generic error context, even though we're using a structured error
1274 * handler.
1275 */
1276 errcxt->saved_errfunc = xmlStructuredError;
1277
1278#ifdef HAVE_XMLSTRUCTUREDERRORCONTEXT
1279 errcxt->saved_errcxt = xmlStructuredErrorContext;
1280#else
1281 errcxt->saved_errcxt = xmlGenericErrorContext;
1282#endif
1283
1285
1286 /*
1287 * Verify that xmlSetStructuredErrorFunc set the context variable we
1288 * expected it to. If not, the error context pointer we just saved is not
1289 * the correct thing to restore, and since that leaves us without a way to
1290 * restore the context in pg_xml_done, we must fail.
1291 *
1292 * The only known situation in which this test fails is if we compile with
1293 * headers from a libxml2 that doesn't track the structured error context
1294 * separately (< 2.7.4), but at runtime use a version that does, or vice
1295 * versa. The libxml2 authors did not treat that change as constituting
1296 * an ABI break, so the LIBXML_TEST_VERSION test in pg_xml_init_library
1297 * fails to protect us from this.
1298 */
1299
1300#ifdef HAVE_XMLSTRUCTUREDERRORCONTEXT
1302#else
1304#endif
1305
1306 if (new_errcxt != errcxt)
1307 ereport(ERROR,
1309 errmsg("could not set up XML error handler"),
1310 errhint("This probably indicates that the version of libxml2"
1311 " being used is not compatible with the libxml2"
1312 " header files that PostgreSQL was built with.")));
1313
1314 /*
1315 * Also, install an entity loader to prevent unwanted fetches of external
1316 * files and URLs.
1317 */
1318 errcxt->saved_entityfunc = xmlGetExternalEntityLoader();
1320
1321 return errcxt;
1322}
1323
1324
1325/*
1326 * pg_xml_done --- restore previous libxml error handling
1327 *
1328 * Resets libxml's global error-handling state to what it was before
1329 * pg_xml_init() was called.
1330 *
1331 * This routine verifies that all pending errors have been dealt with
1332 * (in assert-enabled builds, anyway).
1333 */
1334void
1336{
1337 void *cur_errcxt;
1338
1339 /* An assert seems like enough protection here */
1340 Assert(errcxt->magic == ERRCXT_MAGIC);
1341
1342 /*
1343 * In a normal exit, there should be no un-handled libxml errors. But we
1344 * shouldn't try to enforce this during error recovery, since the longjmp
1345 * could have been thrown before xml_ereport had a chance to run.
1346 */
1347 Assert(!errcxt->err_occurred || isError);
1348
1349 /*
1350 * Check that libxml's global state is correct, warn if not. This is a
1351 * real test and not an Assert because it has a higher probability of
1352 * happening.
1353 */
1354#ifdef HAVE_XMLSTRUCTUREDERRORCONTEXT
1356#else
1358#endif
1359
1360 if (cur_errcxt != errcxt)
1361 elog(WARNING, "libxml error handling state is out of sync with xml.c");
1362
1363 /* Restore the saved handlers */
1364 xmlSetStructuredErrorFunc(errcxt->saved_errcxt, errcxt->saved_errfunc);
1365 xmlSetExternalEntityLoader(errcxt->saved_entityfunc);
1366
1367 /*
1368 * Mark the struct as invalid, just in case somebody somehow manages to
1369 * call xml_errorHandler or xml_ereport with it.
1370 */
1371 errcxt->magic = 0;
1372
1373 /* Release memory */
1374 pfree(errcxt->err_buf.data);
1375 pfree(errcxt);
1376}
1377
1378
1379/*
1380 * pg_xml_error_occurred() --- test the error flag
1381 */
1382bool
1384{
1385 return errcxt->err_occurred;
1386}
1387
1388
1389/*
1390 * SQL/XML allows storing "XML documents" or "XML content". "XML
1391 * documents" are specified by the XML specification and are parsed
1392 * easily by libxml. "XML content" is specified by SQL/XML as the
1393 * production "XMLDecl? content". But libxml can only parse the
1394 * "content" part, so we have to parse the XML declaration ourselves
1395 * to complete this.
1396 */
1397
1398#define CHECK_XML_SPACE(p) \
1399 do { \
1400 if (!xmlIsBlank_ch(*(p))) \
1401 return XML_ERR_SPACE_REQUIRED; \
1402 } while (0)
1403
1404#define SKIP_XML_SPACE(p) \
1405 while (xmlIsBlank_ch(*(p))) (p)++
1406
1407/* Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender */
1408/* Beware of multiple evaluations of argument! */
1409#define PG_XMLISNAMECHAR(c) \
1410 (xmlIsBaseChar_ch(c) || xmlIsIdeographicQ(c) \
1411 || xmlIsDigit_ch(c) \
1412 || c == '.' || c == '-' || c == '_' || c == ':' \
1413 || xmlIsCombiningQ(c) \
1414 || xmlIsExtender_ch(c))
1415
1416/* pnstrdup, but deal with xmlChar not char; len is measured in xmlChars */
1417static xmlChar *
1418xml_pnstrdup(const xmlChar *str, size_t len)
1419{
1420 xmlChar *result;
1421
1422 result = (xmlChar *) palloc((len + 1) * sizeof(xmlChar));
1423 memcpy(result, str, len * sizeof(xmlChar));
1424 result[len] = 0;
1425 return result;
1426}
1427
1428/* Ditto, except input is char* */
1429static xmlChar *
1430pg_xmlCharStrndup(const char *str, size_t len)
1431{
1432 xmlChar *result;
1433
1434 result = (xmlChar *) palloc((len + 1) * sizeof(xmlChar));
1435 memcpy(result, str, len);
1436 result[len] = '\0';
1437
1438 return result;
1439}
1440
1441/*
1442 * Copy xmlChar string to PostgreSQL-owned memory, freeing the input.
1443 *
1444 * The input xmlChar is freed regardless of success of the copy.
1445 */
1446static char *
1448{
1449 char *result;
1450
1451 if (str)
1452 {
1453 PG_TRY();
1454 {
1455 result = pstrdup((char *) str);
1456 }
1457 PG_FINALLY();
1458 {
1459 xmlFree(str);
1460 }
1461 PG_END_TRY();
1462 }
1463 else
1464 result = NULL;
1465
1466 return result;
1467}
1468
1469/*
1470 * str is the null-terminated input string. Remaining arguments are
1471 * output arguments; each can be NULL if value is not wanted.
1472 * version and encoding are returned as locally-palloc'd strings.
1473 * Result is 0 if OK, an error code if not.
1474 */
1475static int
1476parse_xml_decl(const xmlChar *str, size_t *lenp,
1477 xmlChar **version, xmlChar **encoding, int *standalone)
1478{
1479 const xmlChar *p;
1480 const xmlChar *save_p;
1481 size_t len;
1482 int utf8char;
1483 int utf8len;
1484
1485 /*
1486 * Only initialize libxml. We don't need error handling here, but we do
1487 * need to make sure libxml is initialized before calling any of its
1488 * functions. Note that this is safe (and a no-op) if caller has already
1489 * done pg_xml_init().
1490 */
1492
1493 /* Initialize output arguments to "not present" */
1494 if (version)
1495 *version = NULL;
1496 if (encoding)
1497 *encoding = NULL;
1498 if (standalone)
1499 *standalone = -1;
1500
1501 p = str;
1502
1503 if (xmlStrncmp(p, (xmlChar *) "<?xml", 5) != 0)
1504 goto finished;
1505
1506 /*
1507 * If next char is a name char, it's a PI like <?xml-stylesheet ...?>
1508 * rather than an XMLDecl, so we have done what we came to do and found no
1509 * XMLDecl.
1510 *
1511 * We need an input length value for xmlGetUTF8Char, but there's no need
1512 * to count the whole document size, so use strnlen not strlen.
1513 */
1514 utf8len = strnlen((const char *) (p + 5), MAX_MULTIBYTE_CHAR_LEN);
1515 utf8char = xmlGetUTF8Char(p + 5, &utf8len);
1517 goto finished;
1518
1519 p += 5;
1520
1521 /* version */
1522 CHECK_XML_SPACE(p);
1523 SKIP_XML_SPACE(p);
1524 if (xmlStrncmp(p, (xmlChar *) "version", 7) != 0)
1526 p += 7;
1527 SKIP_XML_SPACE(p);
1528 if (*p != '=')
1530 p += 1;
1531 SKIP_XML_SPACE(p);
1532
1533 if (*p == '\'' || *p == '"')
1534 {
1535 const xmlChar *q;
1536
1537 q = xmlStrchr(p + 1, *p);
1538 if (!q)
1540
1541 if (version)
1542 *version = xml_pnstrdup(p + 1, q - p - 1);
1543 p = q + 1;
1544 }
1545 else
1547
1548 /* encoding */
1549 save_p = p;
1550 SKIP_XML_SPACE(p);
1551 if (xmlStrncmp(p, (xmlChar *) "encoding", 8) == 0)
1552 {
1554 p += 8;
1555 SKIP_XML_SPACE(p);
1556 if (*p != '=')
1558 p += 1;
1559 SKIP_XML_SPACE(p);
1560
1561 if (*p == '\'' || *p == '"')
1562 {
1563 const xmlChar *q;
1564
1565 q = xmlStrchr(p + 1, *p);
1566 if (!q)
1568
1569 if (encoding)
1570 *encoding = xml_pnstrdup(p + 1, q - p - 1);
1571 p = q + 1;
1572 }
1573 else
1575 }
1576 else
1577 {
1578 p = save_p;
1579 }
1580
1581 /* standalone */
1582 save_p = p;
1583 SKIP_XML_SPACE(p);
1584 if (xmlStrncmp(p, (xmlChar *) "standalone", 10) == 0)
1585 {
1587 p += 10;
1588 SKIP_XML_SPACE(p);
1589 if (*p != '=')
1591 p += 1;
1592 SKIP_XML_SPACE(p);
1593 if (xmlStrncmp(p, (xmlChar *) "'yes'", 5) == 0 ||
1594 xmlStrncmp(p, (xmlChar *) "\"yes\"", 5) == 0)
1595 {
1596 if (standalone)
1597 *standalone = 1;
1598 p += 5;
1599 }
1600 else if (xmlStrncmp(p, (xmlChar *) "'no'", 4) == 0 ||
1601 xmlStrncmp(p, (xmlChar *) "\"no\"", 4) == 0)
1602 {
1603 if (standalone)
1604 *standalone = 0;
1605 p += 4;
1606 }
1607 else
1609 }
1610 else
1611 {
1612 p = save_p;
1613 }
1614
1615 SKIP_XML_SPACE(p);
1616 if (xmlStrncmp(p, (xmlChar *) "?>", 2) != 0)
1618 p += 2;
1619
1620finished:
1621 len = p - str;
1622
1623 for (p = str; p < str + len; p++)
1624 if (*p > 127)
1625 return XML_ERR_INVALID_CHAR;
1626
1627 if (lenp)
1628 *lenp = len;
1629
1630 return XML_ERR_OK;
1631}
1632
1633
1634/*
1635 * Write an XML declaration. On output, we adjust the XML declaration
1636 * as follows. (These rules are the moral equivalent of the clause
1637 * "Serialization of an XML value" in the SQL standard.)
1638 *
1639 * We try to avoid generating an XML declaration if possible. This is
1640 * so that you don't get trivial things like xml '<foo/>' resulting in
1641 * '<?xml version="1.0"?><foo/>', which would surely be annoying. We
1642 * must provide a declaration if the standalone property is specified
1643 * or if we include an encoding declaration. If we have a
1644 * declaration, we must specify a version (XML requires this).
1645 * Otherwise we only make a declaration if the version is not "1.0",
1646 * which is the default version specified in SQL:2003.
1647 */
1648static bool
1649print_xml_decl(StringInfo buf, const xmlChar *version,
1651{
1652 if ((version && strcmp((const char *) version, PG_XML_DEFAULT_VERSION) != 0)
1653 || (encoding && encoding != PG_UTF8)
1654 || standalone != -1)
1655 {
1656 appendStringInfoString(buf, "<?xml");
1657
1658 if (version)
1659 appendStringInfo(buf, " version=\"%s\"", version);
1660 else
1661 appendStringInfo(buf, " version=\"%s\"", PG_XML_DEFAULT_VERSION);
1662
1663 if (encoding && encoding != PG_UTF8)
1664 {
1665 /*
1666 * XXX might be useful to convert this to IANA names (ISO-8859-1
1667 * instead of LATIN1 etc.); needs field experience
1668 */
1669 appendStringInfo(buf, " encoding=\"%s\"",
1671 }
1672
1673 if (standalone == 1)
1674 appendStringInfoString(buf, " standalone=\"yes\"");
1675 else if (standalone == 0)
1676 appendStringInfoString(buf, " standalone=\"no\"");
1678
1679 return true;
1680 }
1681 else
1682 return false;
1683}
1684
1685/*
1686 * Test whether an input that is to be parsed as CONTENT contains a DTD.
1687 *
1688 * The SQL/XML:2003 definition of CONTENT ("XMLDecl? content") is not
1689 * satisfied by a document with a DTD, which is a bit of a wart, as it means
1690 * the CONTENT type is not a proper superset of DOCUMENT. SQL/XML:2006 and
1691 * later fix that, by redefining content with reference to the "more
1692 * permissive" Document Node of the XQuery/XPath Data Model, such that any
1693 * DOCUMENT value is indeed also a CONTENT value. That definition is more
1694 * useful, as CONTENT becomes usable for parsing input of unknown form (think
1695 * pg_restore).
1696 *
1697 * As used below in parse_xml when parsing for CONTENT, libxml does not give
1698 * us the 2006+ behavior, but only the 2003; it will choke if the input has
1699 * a DTD. But we can provide the 2006+ definition of CONTENT easily enough,
1700 * by detecting this case first and simply doing the parse as DOCUMENT.
1701 *
1702 * A DTD can be found arbitrarily far in, but that would be a contrived case;
1703 * it will ordinarily start within a few dozen characters. The only things
1704 * that can precede it are an XMLDecl (here, the caller will have called
1705 * parse_xml_decl already), whitespace, comments, and processing instructions.
1706 * This function need only return true if it sees a valid sequence of such
1707 * things leading to <!DOCTYPE. It can simply return false in any other
1708 * cases, including malformed input; that will mean the input gets parsed as
1709 * CONTENT as originally planned, with libxml reporting any errors.
1710 *
1711 * This is only to be called from xml_parse, when pg_xml_init has already
1712 * been called. The input is already in UTF8 encoding.
1713 */
1714static bool
1716{
1717 const xmlChar *p = str;
1718
1719 for (;;)
1720 {
1721 const xmlChar *e;
1722
1723 SKIP_XML_SPACE(p);
1724 if (*p != '<')
1725 return false;
1726 p++;
1727
1728 if (*p == '!')
1729 {
1730 p++;
1731
1732 /* if we see <!DOCTYPE, we can return true */
1733 if (xmlStrncmp(p, (xmlChar *) "DOCTYPE", 7) == 0)
1734 return true;
1735
1736 /* otherwise, if it's not a comment, fail */
1737 if (xmlStrncmp(p, (xmlChar *) "--", 2) != 0)
1738 return false;
1739 /* find end of comment: find -- and a > must follow */
1740 p = xmlStrstr(p + 2, (xmlChar *) "--");
1741 if (!p || p[2] != '>')
1742 return false;
1743 /* advance over comment, and keep scanning */
1744 p += 3;
1745 continue;
1746 }
1747
1748 /* otherwise, if it's not a PI <?target something?>, fail */
1749 if (*p != '?')
1750 return false;
1751 p++;
1752
1753 /* find end of PI (the string ?> is forbidden within a PI) */
1754 e = xmlStrstr(p, (xmlChar *) "?>");
1755 if (!e)
1756 return false;
1757
1758 /* advance over PI, keep scanning */
1759 p = e + 2;
1760 }
1761}
1762
1763
1764/*
1765 * Convert a text object to XML internal representation
1766 *
1767 * data is the source data (must not be toasted!), encoding is its encoding,
1768 * and xmloption_arg and preserve_whitespace are options for the
1769 * transformation.
1770 *
1771 * If parsed_xmloptiontype isn't NULL, *parsed_xmloptiontype is set to the
1772 * XmlOptionType actually used to parse the input (typically the same as
1773 * xmloption_arg, but a DOCTYPE node in the input can force DOCUMENT mode).
1774 *
1775 * If parsed_nodes isn't NULL and we parse in CONTENT mode, the list
1776 * of parsed nodes from the xmlParseBalancedChunkMemory call will be returned
1777 * to *parsed_nodes. (It is caller's responsibility to free that.)
1778 *
1779 * Errors normally result in ereport(ERROR), but if escontext is an
1780 * ErrorSaveContext, then "safe" errors are reported there instead, and the
1781 * caller must check SOFT_ERROR_OCCURRED() to see whether that happened.
1782 *
1783 * Note: it is caller's responsibility to xmlFreeDoc() the result,
1784 * else a permanent memory leak will ensue! But note the result could
1785 * be NULL after a soft error.
1786 *
1787 * TODO maybe libxml2's xmlreader is better? (do not construct DOM,
1788 * yet do not use SAX - see xmlreader.c)
1789 */
1790static xmlDocPtr
1792 bool preserve_whitespace, int encoding,
1794 Node *escontext)
1795{
1796 int32 len;
1797 xmlChar *string;
1800 volatile xmlParserCtxtPtr ctxt = NULL;
1801 volatile xmlDocPtr doc = NULL;
1802 volatile int save_keep_blanks = -1;
1803
1804 /*
1805 * This step looks annoyingly redundant, but we must do it to have a
1806 * null-terminated string in case encoding conversion isn't required.
1807 */
1808 len = VARSIZE_ANY_EXHDR(data); /* will be useful later */
1809 string = xml_text2xmlChar(data);
1810
1811 /*
1812 * If the data isn't UTF8, we must translate before giving it to libxml.
1813 *
1814 * XXX ideally, we'd catch any encoding conversion failure and return a
1815 * soft error. However, failure to convert to UTF8 should be pretty darn
1816 * rare, so for now this is left undone.
1817 */
1819 len,
1820 encoding,
1821 PG_UTF8);
1822
1823 /* Start up libxml and its parser */
1825
1826 /* Use a TRY block to ensure we clean up correctly */
1827 PG_TRY();
1828 {
1829 bool parse_as_document = false;
1830 int res_code;
1831 size_t count = 0;
1832 xmlChar *version = NULL;
1833 int standalone = 0;
1834
1835 /* Any errors here are reported as hard ereport's */
1836 xmlInitParser();
1837
1838 /* Decide whether to parse as document or content */
1840 parse_as_document = true;
1841 else
1842 {
1843 /* Parse and skip over the XML declaration, if any */
1845 &count, &version, NULL, &standalone);
1846 if (res_code != 0)
1847 {
1848 errsave(escontext,
1850 errmsg_internal("invalid XML content: invalid XML declaration"),
1852 goto fail;
1853 }
1854
1855 /* Is there a DOCTYPE element? */
1857 parse_as_document = true;
1858 }
1859
1860 /* initialize output parameters */
1864 if (parsed_nodes != NULL)
1865 *parsed_nodes = NULL;
1866
1868 {
1869 int options;
1870
1871 /* set up parser context used by xmlCtxtReadDoc */
1872 ctxt = xmlNewParserCtxt();
1873 if (ctxt == NULL || xmlerrcxt->err_occurred)
1875 "could not allocate parser context");
1876
1877 /*
1878 * Select parse options.
1879 *
1880 * Note that here we try to apply DTD defaults (XML_PARSE_DTDATTR)
1881 * according to SQL/XML:2008 GR 10.16.7.d: 'Default values defined
1882 * by internal DTD are applied'. As for external DTDs, we try to
1883 * support them too (see SQL/XML:2008 GR 10.16.7.e), but that
1884 * doesn't really happen because xmlPgEntityLoader prevents it.
1885 */
1888
1890 NULL, /* no URL */
1891 "UTF-8",
1892 options);
1893
1894 if (doc == NULL || xmlerrcxt->err_occurred)
1895 {
1896 /* Use original option to decide which error code to report */
1898 xml_errsave(escontext, xmlerrcxt,
1900 "invalid XML document");
1901 else
1902 xml_errsave(escontext, xmlerrcxt,
1904 "invalid XML content");
1905 goto fail;
1906 }
1907 }
1908 else
1909 {
1910 /* set up document that xmlParseBalancedChunkMemory will add to */
1911 doc = xmlNewDoc(version);
1912 if (doc == NULL || xmlerrcxt->err_occurred)
1914 "could not allocate XML document");
1915
1916 Assert(doc->encoding == NULL);
1917 doc->encoding = xmlStrdup((const xmlChar *) "UTF-8");
1918 if (doc->encoding == NULL || xmlerrcxt->err_occurred)
1920 "could not allocate XML document");
1921 doc->standalone = standalone;
1922
1923 /* set parse options --- have to do this the ugly way */
1925
1926 /* allow empty content */
1927 if (*(utf8string + count))
1928 {
1930 utf8string + count,
1931 parsed_nodes);
1932 if (res_code != 0 || xmlerrcxt->err_occurred)
1933 {
1934 xml_errsave(escontext, xmlerrcxt,
1936 "invalid XML content");
1937 goto fail;
1938 }
1939 }
1940 }
1941
1942fail:
1943 ;
1944 }
1945 PG_CATCH();
1946 {
1947 if (save_keep_blanks != -1)
1949 if (doc != NULL)
1950 xmlFreeDoc(doc);
1951 if (ctxt != NULL)
1952 xmlFreeParserCtxt(ctxt);
1953
1954 pg_xml_done(xmlerrcxt, true);
1955
1956 PG_RE_THROW();
1957 }
1958 PG_END_TRY();
1959
1960 if (save_keep_blanks != -1)
1962
1963 if (ctxt != NULL)
1964 xmlFreeParserCtxt(ctxt);
1965
1966 pg_xml_done(xmlerrcxt, false);
1967
1968 return doc;
1969}
1970
1971
1972/*
1973 * xmlChar<->text conversions
1974 */
1975static xmlChar *
1977{
1978 return (xmlChar *) text_to_cstring(in);
1979}
1980
1981
1982#ifdef USE_LIBXMLCONTEXT
1983
1984/*
1985 * Manage the special context used for all libxml allocations (but only
1986 * in special debug builds; see notes at top of file)
1987 */
1988static void
1989xml_memory_init(void)
1990{
1991 /* Create memory context if not there already */
1992 if (LibxmlContext == NULL)
1994 "Libxml context",
1996
1997 /* Re-establish the callbacks even if already set */
1999}
2000
2001/*
2002 * Wrappers for memory management functions
2003 */
2004static void *
2005xml_palloc(size_t size)
2006{
2007 return MemoryContextAlloc(LibxmlContext, size);
2008}
2009
2010
2011static void *
2012xml_repalloc(void *ptr, size_t size)
2013{
2014 return repalloc(ptr, size);
2015}
2016
2017
2018static void
2019xml_pfree(void *ptr)
2020{
2021 /* At least some parts of libxml assume xmlFree(NULL) is allowed */
2022 if (ptr)
2023 pfree(ptr);
2024}
2025
2026
2027static char *
2028xml_pstrdup(const char *string)
2029{
2030 return MemoryContextStrdup(LibxmlContext, string);
2031}
2032#endif /* USE_LIBXMLCONTEXT */
2033
2034
2035/*
2036 * xmlPgEntityLoader --- entity loader callback function
2037 *
2038 * Silently prevent any external entity URL from being loaded. We don't want
2039 * to throw an error, so instead make the entity appear to expand to an empty
2040 * string.
2041 *
2042 * We would prefer to allow loading entities that exist in the system's
2043 * global XML catalog; but the available libxml2 APIs make that a complex
2044 * and fragile task. For now, just shut down all external access.
2045 */
2046static xmlParserInputPtr
2047xmlPgEntityLoader(const char *URL, const char *ID,
2048 xmlParserCtxtPtr ctxt)
2049{
2050 return xmlNewStringInputStream(ctxt, (const xmlChar *) "");
2051}
2052
2053
2054/*
2055 * xml_ereport --- report an XML-related error
2056 *
2057 * The "msg" is the SQL-level message; some can be adopted from the SQL/XML
2058 * standard. This function adds libxml's native error message, if any, as
2059 * detail.
2060 *
2061 * This is exported for modules that want to share the core libxml error
2062 * handler. Note that pg_xml_init() *must* have been called previously.
2063 */
2064void
2065xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
2066{
2067 char *detail;
2068
2069 /* Defend against someone passing us a bogus context struct */
2070 if (errcxt->magic != ERRCXT_MAGIC)
2071 elog(ERROR, "xml_ereport called with invalid PgXmlErrorContext");
2072
2073 /* Flag that the current libxml error has been reported */
2074 errcxt->err_occurred = false;
2075
2076 /* Include detail only if we have some text from libxml */
2077 if (errcxt->err_buf.len > 0)
2078 detail = errcxt->err_buf.data;
2079 else
2080 detail = NULL;
2081
2082 ereport(level,
2083 (errcode(sqlcode),
2084 errmsg_internal("%s", msg),
2085 detail ? errdetail_internal("%s", detail) : 0));
2086}
2087
2088
2089/*
2090 * xml_errsave --- save an XML-related error
2091 *
2092 * If escontext is an ErrorSaveContext, error details are saved into it,
2093 * and control returns normally.
2094 *
2095 * Otherwise, the error is thrown, so that this is equivalent to
2096 * xml_ereport() with level == ERROR.
2097 *
2098 * This should be used only for errors that we're sure we do not need
2099 * a transaction abort to clean up after.
2100 */
2101static void
2103 int sqlcode, const char *msg)
2104{
2105 char *detail;
2106
2107 /* Defend against someone passing us a bogus context struct */
2108 if (errcxt->magic != ERRCXT_MAGIC)
2109 elog(ERROR, "xml_errsave called with invalid PgXmlErrorContext");
2110
2111 /* Flag that the current libxml error has been reported */
2112 errcxt->err_occurred = false;
2113
2114 /* Include detail only if we have some text from libxml */
2115 if (errcxt->err_buf.len > 0)
2116 detail = errcxt->err_buf.data;
2117 else
2118 detail = NULL;
2119
2120 errsave(escontext,
2121 (errcode(sqlcode),
2122 errmsg_internal("%s", msg),
2123 detail ? errdetail_internal("%s", detail) : 0));
2124}
2125
2126
2127/*
2128 * Error handler for libxml errors and warnings
2129 */
2130static void
2132{
2135 xmlParserInputPtr input = (ctxt != NULL) ? ctxt->input : NULL;
2136 xmlNodePtr node = error->node;
2137 const xmlChar *name = (node != NULL &&
2138 node->type == XML_ELEMENT_NODE) ? node->name : NULL;
2139 int domain = error->domain;
2140 int level = error->level;
2142
2143 /*
2144 * Defend against someone passing us a bogus context struct.
2145 *
2146 * We force a backend exit if this check fails because longjmp'ing out of
2147 * libxml would likely render it unsafe to use further.
2148 */
2149 if (xmlerrcxt->magic != ERRCXT_MAGIC)
2150 elog(FATAL, "xml_errorHandler called with invalid PgXmlErrorContext");
2151
2152 /*----------
2153 * Older libxml versions report some errors differently.
2154 * First, some errors were previously reported as coming from the parser
2155 * domain but are now reported as coming from the namespace domain.
2156 * Second, some warnings were upgraded to errors.
2157 * We attempt to compensate for that here.
2158 *----------
2159 */
2160 switch (error->code)
2161 {
2162 case XML_WAR_NS_URI:
2163 level = XML_ERR_ERROR;
2164 domain = XML_FROM_NAMESPACE;
2165 break;
2166
2169 case XML_WAR_NS_COLUMN:
2172 case XML_NS_ERR_QNAME:
2174 case XML_NS_ERR_EMPTY:
2175 domain = XML_FROM_NAMESPACE;
2176 break;
2177 }
2178
2179 /* Decide whether to act on the error or not */
2180 switch (domain)
2181 {
2182 case XML_FROM_PARSER:
2183
2184 /*
2185 * XML_ERR_NOT_WELL_BALANCED is typically reported after some
2186 * other, more on-point error. Furthermore, libxml2 2.13 reports
2187 * it under a completely different set of rules than prior
2188 * versions. To avoid cross-version behavioral differences,
2189 * suppress it so long as we already logged some error.
2190 */
2191 if (error->code == XML_ERR_NOT_WELL_BALANCED &&
2192 xmlerrcxt->err_occurred)
2193 return;
2195
2196 case XML_FROM_NONE:
2197 case XML_FROM_MEMORY:
2198 case XML_FROM_IO:
2199
2200 /*
2201 * Suppress warnings about undeclared entities. We need to do
2202 * this to avoid problems due to not loading DTD definitions.
2203 */
2204 if (error->code == XML_WAR_UNDECLARED_ENTITY)
2205 return;
2206
2207 /* Otherwise, accept error regardless of the parsing purpose */
2208 break;
2209
2210 default:
2211 /* Ignore error if only doing well-formedness check */
2212 if (xmlerrcxt->strictness == PG_XML_STRICTNESS_WELLFORMED)
2213 return;
2214 break;
2215 }
2216
2217 /* Prepare error message in errorBuf */
2219
2220 if (error->line > 0)
2221 appendStringInfo(&errorBuf, "line %d: ", error->line);
2222 if (name != NULL)
2223 appendStringInfo(&errorBuf, "element %s: ", name);
2224 if (error->message != NULL)
2226 else
2227 appendStringInfoString(&errorBuf, "(no message provided)");
2228
2229 /*
2230 * Append context information to errorBuf.
2231 *
2232 * xmlParserPrintFileContext() uses libxml's "generic" error handler to
2233 * write the context. Since we don't want to duplicate libxml
2234 * functionality here, we set up a generic error handler temporarily.
2235 *
2236 * We use appendStringInfo() directly as libxml's generic error handler.
2237 * This should work because it has essentially the same signature as
2238 * libxml expects, namely (void *ptr, const char *msg, ...).
2239 */
2240 if (input != NULL)
2241 {
2244
2247
2248 /* Add context information to errorBuf */
2250
2252
2253 /* Restore generic error func */
2255 }
2256
2257 /* Get rid of any trailing newlines in errorBuf */
2259
2260 /*
2261 * Legacy error handling mode. err_occurred is never set, we just add the
2262 * message to err_buf. This mode exists because the xml2 contrib module
2263 * uses our error-handling infrastructure, but we don't want to change its
2264 * behaviour since it's deprecated anyway. This is also why we don't
2265 * distinguish between notices, warnings and errors here --- the old-style
2266 * generic error handler wouldn't have done that either.
2267 */
2268 if (xmlerrcxt->strictness == PG_XML_STRICTNESS_LEGACY)
2269 {
2272 errorBuf.len);
2273
2274 pfree(errorBuf.data);
2275 return;
2276 }
2277
2278 /*
2279 * We don't want to ereport() here because that'd probably leave libxml in
2280 * an inconsistent state. Instead, we remember the error and ereport()
2281 * from xml_ereport().
2282 *
2283 * Warnings and notices can be reported immediately since they won't cause
2284 * a longjmp() out of libxml.
2285 */
2286 if (level >= XML_ERR_ERROR)
2287 {
2290 errorBuf.len);
2291
2292 xmlerrcxt->err_occurred = true;
2293 }
2294 else if (level >= XML_ERR_WARNING)
2295 {
2297 (errmsg_internal("%s", errorBuf.data)));
2298 }
2299 else
2300 {
2302 (errmsg_internal("%s", errorBuf.data)));
2303 }
2304
2305 pfree(errorBuf.data);
2306}
2307
2308
2309/*
2310 * Convert libxml error codes into textual errdetail messages.
2311 *
2312 * This should be called within an ereport or errsave invocation,
2313 * just as errdetail would be.
2314 *
2315 * At the moment, we only need to cover those codes that we
2316 * may raise in this file.
2317 */
2318static int
2319errdetail_for_xml_code(int code)
2320{
2321 const char *det;
2322
2323 switch (code)
2324 {
2326 det = gettext_noop("Invalid character value.");
2327 break;
2329 det = gettext_noop("Space required.");
2330 break;
2332 det = gettext_noop("standalone accepts only 'yes' or 'no'.");
2333 break;
2335 det = gettext_noop("Malformed declaration: missing version.");
2336 break;
2338 det = gettext_noop("Missing encoding in text declaration.");
2339 break;
2341 det = gettext_noop("Parsing XML declaration: '?>' expected.");
2342 break;
2343 default:
2344 det = gettext_noop("Unrecognized libxml error code: %d.");
2345 break;
2346 }
2347
2348 return errdetail(det, code);
2349}
2350
2351
2352/*
2353 * Remove all trailing newlines from a StringInfo string
2354 */
2355static void
2357{
2358 while (str->len > 0 && str->data[str->len - 1] == '\n')
2359 str->data[--str->len] = '\0';
2360}
2361
2362
2363/*
2364 * Append a newline after removing any existing trailing newlines
2365 */
2366static void
2368{
2370 if (str->len > 0)
2372}
2373
2374
2375/*
2376 * Convert one char in the current server encoding to a Unicode codepoint.
2377 */
2378static pg_wchar
2379sqlchar_to_unicode(const char *s)
2380{
2381 char *utf8string;
2382 pg_wchar ret[2]; /* need space for trailing zero */
2383
2385
2388
2389 if (utf8string != s)
2391
2392 return ret[0];
2393}
2394
2395
2396static bool
2398{
2399 /* (Letter | '_' | ':') */
2400 return (xmlIsBaseCharQ(c) || xmlIsIdeographicQ(c)
2401 || c == '_' || c == ':');
2402}
2403
2404
2405static bool
2407{
2408 /* Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender */
2409 return (xmlIsBaseCharQ(c) || xmlIsIdeographicQ(c)
2410 || xmlIsDigitQ(c)
2411 || c == '.' || c == '-' || c == '_' || c == ':'
2412 || xmlIsCombiningQ(c)
2413 || xmlIsExtenderQ(c));
2414}
2415#endif /* USE_LIBXML */
2416
2417
2418/*
2419 * Map SQL identifier to XML name; see SQL/XML:2008 section 9.1.
2420 */
2421char *
2423 bool escape_period)
2424{
2425#ifdef USE_LIBXML
2427 const char *p;
2428
2429 /*
2430 * SQL/XML doesn't make use of this case anywhere, so it's probably a
2431 * mistake.
2432 */
2434
2436
2437 for (p = ident; *p; p += pg_mblen_cstr(p))
2438 {
2439 if (*p == ':' && (p == ident || fully_escaped))
2440 appendStringInfoString(&buf, "_x003A_");
2441 else if (*p == '_' && *(p + 1) == 'x')
2442 appendStringInfoString(&buf, "_x005F_");
2443 else if (fully_escaped && p == ident &&
2444 pg_strncasecmp(p, "xml", 3) == 0)
2445 {
2446 if (*p == 'x')
2447 appendStringInfoString(&buf, "_x0078_");
2448 else
2449 appendStringInfoString(&buf, "_x0058_");
2450 }
2451 else if (escape_period && *p == '.')
2452 appendStringInfoString(&buf, "_x002E_");
2453 else
2454 {
2456
2457 if ((p == ident)
2460 appendStringInfo(&buf, "_x%04X_", (unsigned int) u);
2461 else
2463 }
2464 }
2465
2466 return buf.data;
2467#else /* not USE_LIBXML */
2469 return NULL;
2470#endif /* not USE_LIBXML */
2471}
2472
2473
2474/*
2475 * Map XML name to SQL identifier; see SQL/XML:2008 section 9.3.
2476 */
2477char *
2479{
2481 const char *p;
2482
2484
2485 for (p = name; *p; p += pg_mblen_cstr(p))
2486 {
2487 if (*p == '_' && *(p + 1) == 'x'
2488 && isxdigit((unsigned char) *(p + 2))
2489 && isxdigit((unsigned char) *(p + 3))
2490 && isxdigit((unsigned char) *(p + 4))
2491 && isxdigit((unsigned char) *(p + 5))
2492 && *(p + 6) == '_')
2493 {
2495 unsigned int u;
2496
2497 sscanf(p + 2, "%X", &u);
2498 pg_unicode_to_server(u, (unsigned char *) cbuf);
2500 p += 6;
2501 }
2502 else
2504 }
2505
2506 return buf.data;
2507}
2508
2509/*
2510 * Map SQL value to XML value; see SQL/XML:2008 section 9.8.
2511 *
2512 * When xml_escape_strings is true, then certain characters in string
2513 * values are replaced by entity references (&lt; etc.), as specified
2514 * in SQL/XML:2008 section 9.8 GR 9) a) iii). This is normally what is
2515 * wanted. The false case is mainly useful when the resulting value
2516 * is used with xmlTextWriterWriteAttribute() to write out an
2517 * attribute, because that function does the escaping itself.
2518 */
2519char *
2521{
2523 {
2524 ArrayType *array;
2525 Oid elmtype;
2526 int16 elmlen;
2527 bool elmbyval;
2528 char elmalign;
2529 int num_elems;
2530 Datum *elem_values;
2531 bool *elem_nulls;
2533 int i;
2534
2535 array = DatumGetArrayTypeP(value);
2536 elmtype = ARR_ELEMTYPE(array);
2537 get_typlenbyvalalign(elmtype, &elmlen, &elmbyval, &elmalign);
2538
2540 elmlen, elmbyval, elmalign,
2541 &elem_values, &elem_nulls,
2542 &num_elems);
2543
2545
2546 for (i = 0; i < num_elems; i++)
2547 {
2548 if (elem_nulls[i])
2549 continue;
2550 appendStringInfoString(&buf, "<element>");
2552 map_sql_value_to_xml_value(elem_values[i],
2553 elmtype, true));
2554 appendStringInfoString(&buf, "</element>");
2555 }
2556
2557 pfree(elem_values);
2558 pfree(elem_nulls);
2559
2560 return buf.data;
2561 }
2562 else
2563 {
2564 Oid typeOut;
2565 bool isvarlena;
2566 char *str;
2567
2568 /*
2569 * Flatten domains; the special-case treatments below should apply to,
2570 * eg, domains over boolean not just boolean.
2571 */
2573
2574 /*
2575 * Special XSD formatting for some data types
2576 */
2577 switch (type)
2578 {
2579 case BOOLOID:
2580 if (DatumGetBool(value))
2581 return "true";
2582 else
2583 return "false";
2584
2585 case DATEOID:
2586 {
2587 DateADT date;
2588 struct pg_tm tm;
2589 char buf[MAXDATELEN + 1];
2590
2592 /* XSD doesn't support infinite values */
2593 if (DATE_NOT_FINITE(date))
2594 ereport(ERROR,
2596 errmsg("date out of range"),
2597 errdetail("XML does not support infinite date values.")));
2599 &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
2601
2602 return pstrdup(buf);
2603 }
2604
2605 case TIMESTAMPOID:
2606 {
2608 struct pg_tm tm;
2609 fsec_t fsec;
2610 char buf[MAXDATELEN + 1];
2611
2613
2614 /* XSD doesn't support infinite values */
2616 ereport(ERROR,
2618 errmsg("timestamp out of range"),
2619 errdetail("XML does not support infinite timestamp values.")));
2620 else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
2621 EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
2622 else
2623 ereport(ERROR,
2625 errmsg("timestamp out of range")));
2626
2627 return pstrdup(buf);
2628 }
2629
2630 case TIMESTAMPTZOID:
2631 {
2633 struct pg_tm tm;
2634 int tz;
2635 fsec_t fsec;
2636 const char *tzn = NULL;
2637 char buf[MAXDATELEN + 1];
2638
2640
2641 /* XSD doesn't support infinite values */
2643 ereport(ERROR,
2645 errmsg("timestamp out of range"),
2646 errdetail("XML does not support infinite timestamp values.")));
2647 else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
2648 EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
2649 else
2650 ereport(ERROR,
2652 errmsg("timestamp out of range")));
2653
2654 return pstrdup(buf);
2655 }
2656
2657#ifdef USE_LIBXML
2658 case BYTEAOID:
2659 {
2662 volatile xmlBufferPtr buf = NULL;
2663 volatile xmlTextWriterPtr writer = NULL;
2664 char *result;
2665
2667
2668 PG_TRY();
2669 {
2670 buf = xmlBufferCreate();
2671 if (buf == NULL || xmlerrcxt->err_occurred)
2673 "could not allocate xmlBuffer");
2675 if (writer == NULL || xmlerrcxt->err_occurred)
2677 "could not allocate xmlTextWriter");
2678
2682 else
2685
2686 /* we MUST do this now to flush data out to the buffer */
2688 writer = NULL;
2689
2690 result = pstrdup((const char *) xmlBufferContent(buf));
2691 }
2692 PG_CATCH();
2693 {
2694 if (writer)
2696 if (buf)
2698
2699 pg_xml_done(xmlerrcxt, true);
2700
2701 PG_RE_THROW();
2702 }
2703 PG_END_TRY();
2704
2706
2707 pg_xml_done(xmlerrcxt, false);
2708
2709 return result;
2710 }
2711#endif /* USE_LIBXML */
2712
2713 }
2714
2715 /*
2716 * otherwise, just use the type's native text representation
2717 */
2720
2721 /* ... exactly as-is for XML, and when escaping is not wanted */
2722 if (type == XMLOID || !xml_escape_strings)
2723 return str;
2724
2725 /* otherwise, translate special characters as needed */
2726 return escape_xml(str);
2727 }
2728}
2729
2730
2731/*
2732 * Escape characters in text that have special meanings in XML.
2733 *
2734 * Returns a palloc'd string.
2735 *
2736 * NB: this is intentionally not dependent on libxml.
2737 */
2738char *
2739escape_xml(const char *str)
2740{
2742 const char *p;
2743
2745 for (p = str; *p; p++)
2746 {
2747 switch (*p)
2748 {
2749 case '&':
2750 appendStringInfoString(&buf, "&amp;");
2751 break;
2752 case '<':
2753 appendStringInfoString(&buf, "&lt;");
2754 break;
2755 case '>':
2756 appendStringInfoString(&buf, "&gt;");
2757 break;
2758 case '\r':
2759 appendStringInfoString(&buf, "&#x0d;");
2760 break;
2761 default:
2763 break;
2764 }
2765 }
2766 return buf.data;
2767}
2768
2769
2770static char *
2771_SPI_strdup(const char *s)
2772{
2773 size_t len = strlen(s) + 1;
2774 char *ret = SPI_palloc(len);
2775
2776 memcpy(ret, s, len);
2777 return ret;
2778}
2779
2780
2781/*
2782 * SQL to XML mapping functions
2783 *
2784 * What follows below was at one point intentionally organized so that
2785 * you can read along in the SQL/XML standard. The functions are
2786 * mostly split up the way the clauses lay out in the standards
2787 * document, and the identifiers are also aligned with the standard
2788 * text. Unfortunately, SQL/XML:2006 reordered the clauses
2789 * differently than SQL/XML:2003, so the order below doesn't make much
2790 * sense anymore.
2791 *
2792 * There are many things going on there:
2793 *
2794 * There are two kinds of mappings: Mapping SQL data (table contents)
2795 * to XML documents, and mapping SQL structure (the "schema") to XML
2796 * Schema. And there are functions that do both at the same time.
2797 *
2798 * Then you can map a database, a schema, or a table, each in both
2799 * ways. This breaks down recursively: Mapping a database invokes
2800 * mapping schemas, which invokes mapping tables, which invokes
2801 * mapping rows, which invokes mapping columns, although you can't
2802 * call the last two from the outside. Because of this, there are a
2803 * number of xyz_internal() functions which are to be called both from
2804 * the function manager wrapper and from some upper layer in a
2805 * recursive call.
2806 *
2807 * See the documentation about what the common function arguments
2808 * nulls, tableforest, and targetns mean.
2809 *
2810 * Some style guidelines for XML output: Use double quotes for quoting
2811 * XML attributes. Indent XML elements by two spaces, but remember
2812 * that a lot of code is called recursively at different levels, so
2813 * it's better not to indent rather than create output that indents
2814 * and outdents weirdly. Add newlines to make the output look nice.
2815 */
2816
2817
2818/*
2819 * Visibility of objects for XML mappings; see SQL/XML:2008 section
2820 * 4.10.8.
2821 */
2822
2823/*
2824 * Given a query, which must return type oid as first column, produce
2825 * a list of Oids with the query results.
2826 */
2827static List *
2828query_to_oid_list(const char *query)
2829{
2830 uint64 i;
2831 List *list = NIL;
2832 int spi_result;
2833
2834 spi_result = SPI_execute(query, true, 0);
2836 elog(ERROR, "SPI_execute returned %s for %s",
2838
2839 for (i = 0; i < SPI_processed; i++)
2840 {
2841 Datum oid;
2842 bool isnull;
2843
2846 1,
2847 &isnull);
2848 if (!isnull)
2849 list = lappend_oid(list, DatumGetObjectId(oid));
2850 }
2851
2852 return list;
2853}
2854
2855
2856static List *
2858{
2859 StringInfoData query;
2860
2861 initStringInfo(&query);
2862 appendStringInfo(&query, "SELECT oid FROM pg_catalog.pg_class"
2863 " WHERE relnamespace = %u AND relkind IN ("
2867 " AND pg_catalog.has_table_privilege (oid, 'SELECT')"
2868 " ORDER BY relname;", nspid);
2869
2870 return query_to_oid_list(query.data);
2871}
2872
2873
2874/*
2875 * Including the system schemas is probably not useful for a database
2876 * mapping.
2877 */
2878#define XML_VISIBLE_SCHEMAS_EXCLUDE "(nspname ~ '^pg_' OR nspname = 'information_schema')"
2879
2880#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
2881
2882
2883static List *
2885{
2886 return query_to_oid_list(XML_VISIBLE_SCHEMAS " ORDER BY nspname;");
2887}
2888
2889
2890static List *
2892{
2893 /* At the moment there is no order required here. */
2894 return query_to_oid_list("SELECT oid FROM pg_catalog.pg_class"
2895 " WHERE relkind IN ("
2899 " AND pg_catalog.has_table_privilege(pg_class.oid, 'SELECT')"
2900 " AND relnamespace IN (" XML_VISIBLE_SCHEMAS ");");
2901}
2902
2903
2904/*
2905 * Map SQL table to XML and/or XML Schema document; see SQL/XML:2008
2906 * section 9.11.
2907 */
2908
2909static StringInfo
2911 const char *xmlschema, bool nulls, bool tableforest,
2912 const char *targetns, bool top_level)
2913{
2914 StringInfoData query;
2915
2916 initStringInfo(&query);
2917 appendStringInfo(&query, "SELECT * FROM %s",
2919 ObjectIdGetDatum(relid))));
2920 return query_to_xml_internal(query.data, get_rel_name(relid),
2921 xmlschema, nulls, tableforest,
2922 targetns, top_level);
2923}
2924
2925
2926Datum
2928{
2929 Oid relid = PG_GETARG_OID(0);
2930 bool nulls = PG_GETARG_BOOL(1);
2931 bool tableforest = PG_GETARG_BOOL(2);
2932 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2933
2935 nulls, tableforest,
2936 targetns, true)));
2937}
2938
2939
2940Datum
2942{
2943 char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2944 bool nulls = PG_GETARG_BOOL(1);
2945 bool tableforest = PG_GETARG_BOOL(2);
2946 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2947
2949 NULL, nulls, tableforest,
2950 targetns, true)));
2951}
2952
2953
2954Datum
2956{
2958 int32 count = PG_GETARG_INT32(1);
2959 bool nulls = PG_GETARG_BOOL(2);
2960 bool tableforest = PG_GETARG_BOOL(3);
2961 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(4));
2962
2964 Portal portal;
2965 uint64 i;
2966
2968
2969 if (!tableforest)
2970 {
2973 }
2974
2975 SPI_connect();
2976 portal = SPI_cursor_find(name);
2977 if (portal == NULL)
2978 ereport(ERROR,
2980 errmsg("cursor \"%s\" does not exist", name)));
2981
2982 SPI_cursor_fetch(portal, true, count);
2983 for (i = 0; i < SPI_processed; i++)
2985 tableforest, targetns, true);
2986
2987 SPI_finish();
2988
2989 if (!tableforest)
2991
2993}
2994
2995
2996/*
2997 * Write the start tag of the root element of a data mapping.
2998 *
2999 * top_level means that this is the very top level of the eventual
3000 * output. For example, when the user calls table_to_xml, then a call
3001 * with a table name to this function is the top level. When the user
3002 * calls database_to_xml, then a call with a schema name to this
3003 * function is not the top level. If top_level is false, then the XML
3004 * namespace declarations are omitted, because they supposedly already
3005 * appeared earlier in the output. Repeating them is not wrong, but
3006 * it looks ugly.
3007 */
3008static void
3010 const char *xmlschema, const char *targetns,
3011 bool top_level)
3012{
3013 /* This isn't really wrong but currently makes no sense. */
3014 Assert(top_level || !xmlschema);
3015
3017 if (top_level)
3018 {
3019 appendStringInfoString(result, " xmlns:xsi=\"" NAMESPACE_XSI "\"");
3020 if (strlen(targetns) > 0)
3021 appendStringInfo(result, " xmlns=\"%s\"", targetns);
3022 }
3023 if (xmlschema)
3024 {
3025 /* FIXME: better targets */
3026 if (strlen(targetns) > 0)
3027 appendStringInfo(result, " xsi:schemaLocation=\"%s #\"", targetns);
3028 else
3029 appendStringInfoString(result, " xsi:noNamespaceSchemaLocation=\"#\"");
3030 }
3032}
3033
3034
3035static void
3040
3041
3042static StringInfo
3043query_to_xml_internal(const char *query, char *tablename,
3044 const char *xmlschema, bool nulls, bool tableforest,
3045 const char *targetns, bool top_level)
3046{
3048 char *xmltn;
3049 uint64 i;
3050
3051 if (tablename)
3052 xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
3053 else
3054 xmltn = "table";
3055
3057
3058 SPI_connect();
3059 if (SPI_execute(query, true, 0) != SPI_OK_SELECT)
3060 ereport(ERROR,
3062 errmsg("invalid query")));
3063
3064 if (!tableforest)
3065 {
3067 targetns, top_level);
3069 }
3070
3071 if (xmlschema)
3072 appendStringInfo(result, "%s\n\n", xmlschema);
3073
3074 for (i = 0; i < SPI_processed; i++)
3075 SPI_sql_row_to_xmlelement(i, result, tablename, nulls,
3076 tableforest, targetns, top_level);
3077
3078 if (!tableforest)
3080
3081 SPI_finish();
3082
3083 return result;
3084}
3085
3086
3087Datum
3089{
3090 Oid relid = PG_GETARG_OID(0);
3091 bool nulls = PG_GETARG_BOOL(1);
3092 bool tableforest = PG_GETARG_BOOL(2);
3093 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
3094 const char *result;
3095 Relation rel;
3096
3097 rel = table_open(relid, AccessShareLock);
3098 result = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
3100 table_close(rel, NoLock);
3101
3103}
3104
3105
3106Datum
3108{
3109 char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
3110 bool nulls = PG_GETARG_BOOL(1);
3111 bool tableforest = PG_GETARG_BOOL(2);
3112 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
3113 const char *result;
3115 Portal portal;
3116
3117 SPI_connect();
3118
3119 if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
3120 elog(ERROR, "SPI_prepare(\"%s\") failed", query);
3121
3122 if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
3123 elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
3124
3126 InvalidOid, nulls,
3128 SPI_cursor_close(portal);
3129 SPI_finish();
3130
3132}
3133
3134
3135Datum
3137{
3139 bool nulls = PG_GETARG_BOOL(1);
3140 bool tableforest = PG_GETARG_BOOL(2);
3141 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
3142 const char *xmlschema;
3143 Portal portal;
3144
3145 SPI_connect();
3146 portal = SPI_cursor_find(name);
3147 if (portal == NULL)
3148 ereport(ERROR,
3150 errmsg("cursor \"%s\" does not exist", name)));
3151 if (portal->tupDesc == NULL)
3152 ereport(ERROR,
3154 errmsg("portal \"%s\" does not return tuples", name)));
3155
3157 InvalidOid, nulls,
3159 SPI_finish();
3160
3162}
3163
3164
3165Datum
3167{
3168 Oid relid = PG_GETARG_OID(0);
3169 bool nulls = PG_GETARG_BOOL(1);
3170 bool tableforest = PG_GETARG_BOOL(2);
3171 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
3172 Relation rel;
3173 const char *xmlschema;
3174
3175 rel = table_open(relid, AccessShareLock);
3176 xmlschema = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
3178 table_close(rel, NoLock);
3179
3181 xmlschema, nulls, tableforest,
3182 targetns, true)));
3183}
3184
3185
3186Datum
3188{
3189 char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
3190 bool nulls = PG_GETARG_BOOL(1);
3191 bool tableforest = PG_GETARG_BOOL(2);
3192 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
3193
3194 const char *xmlschema;
3196 Portal portal;
3197
3198 SPI_connect();
3199
3200 if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
3201 elog(ERROR, "SPI_prepare(\"%s\") failed", query);
3202
3203 if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
3204 elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
3205
3207 InvalidOid, nulls, tableforest, targetns));
3208 SPI_cursor_close(portal);
3209 SPI_finish();
3210
3212 xmlschema, nulls, tableforest,
3213 targetns, true)));
3214}
3215
3216
3217/*
3218 * Map SQL schema to XML and/or XML Schema document; see SQL/XML:2008
3219 * sections 9.13, 9.14.
3220 */
3221
3222static StringInfo
3224 bool tableforest, const char *targetns, bool top_level)
3225{
3227 char *xmlsn;
3229 ListCell *cell;
3230
3232 true, false);
3234
3237
3238 if (xmlschema)
3239 appendStringInfo(result, "%s\n\n", xmlschema);
3240
3241 SPI_connect();
3242
3244
3245 foreach(cell, relid_list)
3246 {
3247 Oid relid = lfirst_oid(cell);
3249
3251 targetns, false);
3252
3255 }
3256
3257 SPI_finish();
3258
3260
3261 return result;
3262}
3263
3264
3265Datum
3267{
3269 bool nulls = PG_GETARG_BOOL(1);
3270 bool tableforest = PG_GETARG_BOOL(2);
3271 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
3272
3273 char *schemaname;
3274 Oid nspid;
3275
3276 schemaname = NameStr(*name);
3277 nspid = LookupExplicitNamespace(schemaname, false);
3278
3280 nulls, tableforest, targetns, true)));
3281}
3282
3283
3284/*
3285 * Write the start element of the root element of an XML Schema mapping.
3286 */
3287static void
3289{
3291 "<xsd:schema\n"
3292 " xmlns:xsd=\"" NAMESPACE_XSD "\"");
3293 if (strlen(targetns) > 0)
3295 "\n"
3296 " targetNamespace=\"%s\"\n"
3297 " elementFormDefault=\"qualified\"",
3298 targetns);
3300 ">\n\n");
3301}
3302
3303
3304static void
3309
3310
3311static StringInfo
3312schema_to_xmlschema_internal(const char *schemaname, bool nulls,
3313 bool tableforest, const char *targetns)
3314{
3315 Oid nspid;
3318 ListCell *cell;
3320
3322
3323 nspid = LookupExplicitNamespace(schemaname, false);
3324
3326
3327 SPI_connect();
3328
3330
3331 tupdesc_list = NIL;
3332 foreach(cell, relid_list)
3333 {
3334 Relation rel;
3335
3338 table_close(rel, NoLock);
3339 }
3340
3343
3346 nulls, tableforest, targetns));
3347
3349
3350 SPI_finish();
3351
3352 return result;
3353}
3354
3355
3356Datum
3367
3368
3369Datum
3371{
3373 bool nulls = PG_GETARG_BOOL(1);
3374 bool tableforest = PG_GETARG_BOOL(2);
3375 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
3376 char *schemaname;
3377 Oid nspid;
3379
3380 schemaname = NameStr(*name);
3381 nspid = LookupExplicitNamespace(schemaname, false);
3382
3383 xmlschema = schema_to_xmlschema_internal(schemaname, nulls,
3385
3387 xmlschema->data, nulls,
3388 tableforest, targetns, true)));
3389}
3390
3391
3392/*
3393 * Map SQL database to XML and/or XML Schema document; see SQL/XML:2008
3394 * sections 9.16, 9.17.
3395 */
3396
3397static StringInfo
3398database_to_xml_internal(const char *xmlschema, bool nulls,
3399 bool tableforest, const char *targetns)
3400{
3403 ListCell *cell;
3404 char *xmlcn;
3405
3407 true, false);
3409
3412
3413 if (xmlschema)
3414 appendStringInfo(result, "%s\n\n", xmlschema);
3415
3416 SPI_connect();
3417
3419
3420 foreach(cell, nspid_list)
3421 {
3422 Oid nspid = lfirst_oid(cell);
3424
3426 tableforest, targetns, false);
3427
3430 }
3431
3432 SPI_finish();
3433
3435
3436 return result;
3437}
3438
3439
3440Datum
3450
3451
3452static StringInfo
3493
3494
3495Datum
3505
3506
3507Datum
3520
3521
3522/*
3523 * Map a multi-part SQL name to an XML name; see SQL/XML:2008 section
3524 * 9.2.
3525 */
3526static char *
3527map_multipart_sql_identifier_to_xml_name(const char *a, const char *b, const char *c, const char *d)
3528{
3530
3532
3533 if (a)
3535 map_sql_identifier_to_xml_name(a, true, true));
3536 if (b)
3537 appendStringInfo(&result, ".%s",
3538 map_sql_identifier_to_xml_name(b, true, true));
3539 if (c)
3540 appendStringInfo(&result, ".%s",
3541 map_sql_identifier_to_xml_name(c, true, true));
3542 if (d)
3543 appendStringInfo(&result, ".%s",
3544 map_sql_identifier_to_xml_name(d, true, true));
3545
3546 return result.data;
3547}
3548
3549
3550/*
3551 * Map an SQL table to an XML Schema document; see SQL/XML:2008
3552 * section 9.11.
3553 *
3554 * Map an SQL table to XML Schema data types; see SQL/XML:2008 section
3555 * 9.9.
3556 */
3557static const char *
3558map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls,
3559 bool tableforest, const char *targetns)
3560{
3561 int i;
3562 char *xmltn;
3563 char *tabletypename;
3564 char *rowtypename;
3566
3568
3569 if (OidIsValid(relid))
3570 {
3571 HeapTuple tuple;
3573
3574 tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
3575 if (!HeapTupleIsValid(tuple))
3576 elog(ERROR, "cache lookup failed for relation %u", relid);
3577 reltuple = (Form_pg_class) GETSTRUCT(tuple);
3578
3580 true, false);
3581
3584 get_namespace_name(reltuple->relnamespace),
3585 NameStr(reltuple->relname));
3586
3589 get_namespace_name(reltuple->relnamespace),
3590 NameStr(reltuple->relname));
3591
3592 ReleaseSysCache(tuple);
3593 }
3594 else
3595 {
3596 if (tableforest)
3597 xmltn = "row";
3598 else
3599 xmltn = "table";
3600
3601 tabletypename = "TableType";
3602 rowtypename = "RowType";
3603 }
3604
3606
3609
3611 "<xsd:complexType name=\"%s\">\n"
3612 " <xsd:sequence>\n",
3613 rowtypename);
3614
3615 for (i = 0; i < tupdesc->natts; i++)
3616 {
3617 Form_pg_attribute att = TupleDescAttr(tupdesc, i);
3618
3619 if (att->attisdropped)
3620 continue;
3622 " <xsd:element name=\"%s\" type=\"%s\"%s></xsd:element>\n",
3624 true, false),
3625 map_sql_type_to_xml_name(att->atttypid, -1),
3626 nulls ? " nillable=\"true\"" : " minOccurs=\"0\"");
3627 }
3628
3630 " </xsd:sequence>\n"
3631 "</xsd:complexType>\n\n");
3632
3633 if (!tableforest)
3634 {
3636 "<xsd:complexType name=\"%s\">\n"
3637 " <xsd:sequence>\n"
3638 " <xsd:element name=\"row\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n"
3639 " </xsd:sequence>\n"
3640 "</xsd:complexType>\n\n",
3642
3644 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3646 }
3647 else
3649 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3651
3653
3654 return result.data;
3655}
3656
3657
3658/*
3659 * Map an SQL schema to XML Schema data types; see SQL/XML:2008
3660 * section 9.12.
3661 */
3662static const char *
3664 bool tableforest, const char *targetns)
3665{
3666 char *dbname;
3667 char *nspname;
3668 char *xmlsn;
3669 char *schematypename;
3671 ListCell *cell;
3672
3674 nspname = get_namespace_name(nspid);
3675
3677
3678 xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
3679
3681 dbname,
3682 nspname,
3683 NULL);
3684
3686 "<xsd:complexType name=\"%s\">\n", schematypename);
3687 if (!tableforest)
3689 " <xsd:all>\n");
3690 else
3692 " <xsd:sequence>\n");
3693
3694 foreach(cell, relid_list)
3695 {
3696 Oid relid = lfirst_oid(cell);
3697 char *relname = get_rel_name(relid);
3698 char *xmltn = map_sql_identifier_to_xml_name(relname, true, false);
3699 char *tabletypename = map_multipart_sql_identifier_to_xml_name(tableforest ? "RowType" : "TableType",
3700 dbname,
3701 nspname,
3702 relname);
3703
3704 if (!tableforest)
3706 " <xsd:element name=\"%s\" type=\"%s\"/>\n",
3708 else
3710 " <xsd:element name=\"%s\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n",
3712 }
3713
3714 if (!tableforest)
3716 " </xsd:all>\n");
3717 else
3719 " </xsd:sequence>\n");
3721 "</xsd:complexType>\n\n");
3722
3724 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3726
3727 return result.data;
3728}
3729
3730
3731/*
3732 * Map an SQL catalog to XML Schema data types; see SQL/XML:2008
3733 * section 9.15.
3734 */
3735static const char *
3737 bool tableforest, const char *targetns)
3738{
3739 char *dbname;
3740 char *xmlcn;
3741 char *catalogtypename;
3743 ListCell *cell;
3744
3746
3748
3750
3752 dbname,
3753 NULL,
3754 NULL);
3755
3757 "<xsd:complexType name=\"%s\">\n", catalogtypename);
3759 " <xsd:all>\n");
3760
3761 foreach(cell, nspid_list)
3762 {
3763 Oid nspid = lfirst_oid(cell);
3764 char *nspname = get_namespace_name(nspid);
3765 char *xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
3767 dbname,
3768 nspname,
3769 NULL);
3770
3772 " <xsd:element name=\"%s\" type=\"%s\"/>\n",
3774 }
3775
3777 " </xsd:all>\n");
3779 "</xsd:complexType>\n\n");
3780
3782 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3784
3785 return result.data;
3786}
3787
3788
3789/*
3790 * Map an SQL data type to an XML name; see SQL/XML:2008 section 9.4.
3791 */
3792static const char *
3793map_sql_type_to_xml_name(Oid typeoid, int typmod)
3794{
3796
3798
3799 switch (typeoid)
3800 {
3801 case BPCHAROID:
3802 if (typmod == -1)
3804 else
3805 appendStringInfo(&result, "CHAR_%d", typmod - VARHDRSZ);
3806 break;
3807 case VARCHAROID:
3808 if (typmod == -1)
3809 appendStringInfoString(&result, "VARCHAR");
3810 else
3811 appendStringInfo(&result, "VARCHAR_%d", typmod - VARHDRSZ);
3812 break;
3813 case NUMERICOID:
3814 if (typmod == -1)
3815 appendStringInfoString(&result, "NUMERIC");
3816 else
3817 appendStringInfo(&result, "NUMERIC_%d_%d",
3818 ((typmod - VARHDRSZ) >> 16) & 0xffff,
3819 (typmod - VARHDRSZ) & 0xffff);
3820 break;
3821 case INT4OID:
3822 appendStringInfoString(&result, "INTEGER");
3823 break;
3824 case INT2OID:
3825 appendStringInfoString(&result, "SMALLINT");
3826 break;
3827 case INT8OID:
3828 appendStringInfoString(&result, "BIGINT");
3829 break;
3830 case FLOAT4OID:
3832 break;
3833 case FLOAT8OID:
3834 appendStringInfoString(&result, "DOUBLE");
3835 break;
3836 case BOOLOID:
3837 appendStringInfoString(&result, "BOOLEAN");
3838 break;
3839 case TIMEOID:
3840 if (typmod == -1)
3842 else
3843 appendStringInfo(&result, "TIME_%d", typmod);
3844 break;
3845 case TIMETZOID:
3846 if (typmod == -1)
3847 appendStringInfoString(&result, "TIME_WTZ");
3848 else
3849 appendStringInfo(&result, "TIME_WTZ_%d", typmod);
3850 break;
3851 case TIMESTAMPOID:
3852 if (typmod == -1)
3853 appendStringInfoString(&result, "TIMESTAMP");
3854 else
3855 appendStringInfo(&result, "TIMESTAMP_%d", typmod);
3856 break;
3857 case TIMESTAMPTZOID:
3858 if (typmod == -1)
3859 appendStringInfoString(&result, "TIMESTAMP_WTZ");
3860 else
3861 appendStringInfo(&result, "TIMESTAMP_WTZ_%d", typmod);
3862 break;
3863 case DATEOID:
3865 break;
3866 case XMLOID:
3868 break;
3869 default:
3870 {
3871 HeapTuple tuple;
3873
3874 tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typeoid));
3875 if (!HeapTupleIsValid(tuple))
3876 elog(ERROR, "cache lookup failed for type %u", typeoid);
3877 typtuple = (Form_pg_type) GETSTRUCT(tuple);
3878
3880 map_multipart_sql_identifier_to_xml_name((typtuple->typtype == TYPTYPE_DOMAIN) ? "Domain" : "UDT",
3882 get_namespace_name(typtuple->typnamespace),
3883 NameStr(typtuple->typname)));
3884
3885 ReleaseSysCache(tuple);
3886 }
3887 }
3888
3889 return result.data;
3890}
3891
3892
3893/*
3894 * Map a collection of SQL data types to XML Schema data types; see
3895 * SQL/XML:2008 section 9.7.
3896 */
3897static const char *
3899{
3900 List *uniquetypes = NIL;
3901 int i;
3903 ListCell *cell0;
3904
3905 /* extract all column types used in the set of TupleDescs */
3906 foreach(cell0, tupdesc_list)
3907 {
3908 TupleDesc tupdesc = (TupleDesc) lfirst(cell0);
3909
3910 for (i = 0; i < tupdesc->natts; i++)
3911 {
3912 Form_pg_attribute att = TupleDescAttr(tupdesc, i);
3913
3914 if (att->attisdropped)
3915 continue;
3917 }
3918 }
3919
3920 /* add base types of domains */
3921 foreach(cell0, uniquetypes)
3922 {
3923 Oid typid = lfirst_oid(cell0);
3924 Oid basetypid = getBaseType(typid);
3925
3926 if (basetypid != typid)
3928 }
3929
3930 /* Convert to textual form */
3932
3933 foreach(cell0, uniquetypes)
3934 {
3935 appendStringInfo(&result, "%s\n",
3937 -1));
3938 }
3939
3940 return result.data;
3941}
3942
3943
3944/*
3945 * Map an SQL data type to a named XML Schema data type; see
3946 * SQL/XML:2008 sections 9.5 and 9.6.
3947 *
3948 * (The distinction between 9.5 and 9.6 is basically that 9.6 adds
3949 * a name attribute, which this function does. The name-less version
3950 * 9.5 doesn't appear to be required anywhere.)
3951 */
3952static const char *
3954{
3956 const char *typename = map_sql_type_to_xml_name(typeoid, typmod);
3957
3959
3960 if (typeoid == XMLOID)
3961 {
3963 "<xsd:complexType mixed=\"true\">\n"
3964 " <xsd:sequence>\n"
3965 " <xsd:any name=\"element\" minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>\n"
3966 " </xsd:sequence>\n"
3967 "</xsd:complexType>\n");
3968 }
3969 else
3970 {
3972 "<xsd:simpleType name=\"%s\">\n", typename);
3973
3974 switch (typeoid)
3975 {
3976 case BPCHAROID:
3977 case VARCHAROID:
3978 case TEXTOID:
3980 " <xsd:restriction base=\"xsd:string\">\n");
3981 if (typmod != -1)
3983 " <xsd:maxLength value=\"%d\"/>\n",
3984 typmod - VARHDRSZ);
3985 appendStringInfoString(&result, " </xsd:restriction>\n");
3986 break;
3987
3988 case BYTEAOID:
3990 " <xsd:restriction base=\"xsd:%s\">\n"
3991 " </xsd:restriction>\n",
3992 xmlbinary == XMLBINARY_BASE64 ? "base64Binary" : "hexBinary");
3993 break;
3994
3995 case NUMERICOID:
3996 if (typmod != -1)
3998 " <xsd:restriction base=\"xsd:decimal\">\n"
3999 " <xsd:totalDigits value=\"%d\"/>\n"
4000 " <xsd:fractionDigits value=\"%d\"/>\n"
4001 " </xsd:restriction>\n",
4002 ((typmod - VARHDRSZ) >> 16) & 0xffff,
4003 (typmod - VARHDRSZ) & 0xffff);
4004 break;
4005
4006 case INT2OID:
4008 " <xsd:restriction base=\"xsd:short\">\n"
4009 " <xsd:maxInclusive value=\"%d\"/>\n"
4010 " <xsd:minInclusive value=\"%d\"/>\n"
4011 " </xsd:restriction>\n",
4013 break;
4014
4015 case INT4OID:
4017 " <xsd:restriction base=\"xsd:int\">\n"
4018 " <xsd:maxInclusive value=\"%d\"/>\n"
4019 " <xsd:minInclusive value=\"%d\"/>\n"
4020 " </xsd:restriction>\n",
4021 INT_MAX, INT_MIN);
4022 break;
4023
4024 case INT8OID:
4026 " <xsd:restriction base=\"xsd:long\">\n"
4027 " <xsd:maxInclusive value=\"" INT64_FORMAT "\"/>\n"
4028 " <xsd:minInclusive value=\"" INT64_FORMAT "\"/>\n"
4029 " </xsd:restriction>\n",
4031 PG_INT64_MIN);
4032 break;
4033
4034 case FLOAT4OID:
4036 " <xsd:restriction base=\"xsd:float\"></xsd:restriction>\n");
4037 break;
4038
4039 case FLOAT8OID:
4041 " <xsd:restriction base=\"xsd:double\"></xsd:restriction>\n");
4042 break;
4043
4044 case BOOLOID:
4046 " <xsd:restriction base=\"xsd:boolean\"></xsd:restriction>\n");
4047 break;
4048
4049 case TIMEOID:
4050 case TIMETZOID:
4051 {
4052 const char *tz = (typeoid == TIMETZOID ? "(\\+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
4053
4054 if (typmod == -1)
4056 " <xsd:restriction base=\"xsd:time\">\n"
4057 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}(.\\p{Nd}+)?%s\"/>\n"
4058 " </xsd:restriction>\n", tz);
4059 else if (typmod == 0)
4061 " <xsd:restriction base=\"xsd:time\">\n"
4062 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
4063 " </xsd:restriction>\n", tz);
4064 else
4066 " <xsd:restriction base=\"xsd:time\">\n"
4067 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}.\\p{Nd}{%d}%s\"/>\n"
4068 " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
4069 break;
4070 }
4071
4072 case TIMESTAMPOID:
4073 case TIMESTAMPTZOID:
4074 {
4075 const char *tz = (typeoid == TIMESTAMPTZOID ? "(\\+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
4076
4077 if (typmod == -1)
4079 " <xsd:restriction base=\"xsd:dateTime\">\n"
4080 " <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"
4081 " </xsd:restriction>\n", tz);
4082 else if (typmod == 0)
4084 " <xsd:restriction base=\"xsd:dateTime\">\n"
4085 " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
4086 " </xsd:restriction>\n", tz);
4087 else
4089 " <xsd:restriction base=\"xsd:dateTime\">\n"
4090 " <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"
4091 " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
4092 break;
4093 }
4094
4095 case DATEOID:
4097 " <xsd:restriction base=\"xsd:date\">\n"
4098 " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}\"/>\n"
4099 " </xsd:restriction>\n");
4100 break;
4101
4102 default:
4103 if (get_typtype(typeoid) == TYPTYPE_DOMAIN)
4104 {
4106 int32 base_typmod = -1;
4107
4108 base_typeoid = getBaseTypeAndTypmod(typeoid, &base_typmod);
4109
4111 " <xsd:restriction base=\"%s\"/>\n",
4113 }
4114 break;
4115 }
4116 appendStringInfoString(&result, "</xsd:simpleType>\n");
4117 }
4118
4119 return result.data;
4120}
4121
4122
4123/*
4124 * Map an SQL row to an XML element, taking the row from the active
4125 * SPI cursor. See also SQL/XML:2008 section 9.10.
4126 */
4127static void
4129 bool nulls, bool tableforest,
4130 const char *targetns, bool top_level)
4131{
4132 int i;
4133 char *xmltn;
4134
4135 if (tablename)
4136 xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
4137 else
4138 {
4139 if (tableforest)
4140 xmltn = "row";
4141 else
4142 xmltn = "table";
4143 }
4144
4145 if (tableforest)
4147 else
4148 appendStringInfoString(result, "<row>\n");
4149
4150 for (i = 1; i <= SPI_tuptable->tupdesc->natts; i++)
4151 {
4152 char *colname;
4153 Datum colval;
4154 bool isnull;
4155
4157 true, false);
4160 i,
4161 &isnull);
4162 if (isnull)
4163 {
4164 if (nulls)
4165 appendStringInfo(result, " <%s xsi:nil=\"true\"/>\n", colname);
4166 }
4167 else
4168 appendStringInfo(result, " <%s>%s</%s>\n",
4169 colname,
4172 colname);
4173 }
4174
4175 if (tableforest)
4176 {
4179 }
4180 else
4181 appendStringInfoString(result, "</row>\n\n");
4182}
4183
4184
4185/*
4186 * XPath related functions
4187 */
4188
4189#ifdef USE_LIBXML
4190
4191/*
4192 * Convert XML node to text.
4193 *
4194 * For attribute and text nodes, return the escaped text. For anything else,
4195 * dump the whole subtree.
4196 */
4197static text *
4199{
4200 xmltype *result = NULL;
4201
4202 if (cur->type != XML_ATTRIBUTE_NODE && cur->type != XML_TEXT_NODE)
4203 {
4204 void (*volatile nodefree) (xmlNodePtr) = NULL;
4205 volatile xmlBufferPtr buf = NULL;
4206 volatile xmlNodePtr cur_copy = NULL;
4207
4208 PG_TRY();
4209 {
4210 int bytes;
4211
4212 buf = xmlBufferCreate();
4213 if (buf == NULL || xmlerrcxt->err_occurred)
4215 "could not allocate xmlBuffer");
4216
4217 /*
4218 * Produce a dump of the node that we can serialize. xmlNodeDump
4219 * does that, but the result of that function won't contain
4220 * namespace definitions from ancestor nodes, so we first do a
4221 * xmlCopyNode() which duplicates the node along with its required
4222 * namespace definitions.
4223 *
4224 * Some old libxml2 versions such as 2.7.6 produce partially
4225 * broken XML_DOCUMENT_NODE nodes (unset content field) when
4226 * copying them. xmlNodeDump of such a node works fine, but
4227 * xmlFreeNode crashes; set us up to call xmlFreeDoc instead.
4228 */
4229 cur_copy = xmlCopyNode(cur, 1);
4230 if (cur_copy == NULL || xmlerrcxt->err_occurred)
4232 "could not copy node");
4233 nodefree = (cur_copy->type == XML_DOCUMENT_NODE) ?
4234 (void (*) (xmlNodePtr)) xmlFreeDoc : xmlFreeNode;
4235
4236 bytes = xmlNodeDump(buf, NULL, cur_copy, 0, 0);
4237 if (bytes == -1 || xmlerrcxt->err_occurred)
4239 "could not dump node");
4240
4242 }
4243 PG_FINALLY();
4244 {
4245 if (nodefree)
4247 if (buf)
4249 }
4250 PG_END_TRY();
4251 }
4252 else
4253 {
4254 xmlChar *volatile str = NULL;
4255
4256 PG_TRY();
4257 {
4258 char *escaped;
4259
4261 if (str == NULL || xmlerrcxt->err_occurred)
4263 "could not allocate xmlChar");
4264
4265 /* Here we rely on XML having the same representation as TEXT */
4266 escaped = escape_xml((char *) str);
4267
4269 pfree(escaped);
4270 }
4271 PG_FINALLY();
4272 {
4273 if (str)
4274 xmlFree(str);
4275 }
4276 PG_END_TRY();
4277 }
4278
4279 return result;
4280}
4281
4282/*
4283 * Convert an XML XPath object (the result of evaluating an XPath expression)
4284 * to an array of xml values, which are appended to astate. The function
4285 * result value is the number of elements in the array.
4286 *
4287 * If "astate" is NULL then we don't generate the array value, but we still
4288 * return the number of elements it would have had.
4289 *
4290 * Nodesets are converted to an array containing the nodes' textual
4291 * representations. Primitive values (float, double, string) are converted
4292 * to a single-element array containing the value's string representation.
4293 */
4294static int
4296 ArrayBuildState *astate,
4298{
4299 int result = 0;
4300 Datum datum;
4301 Oid datumtype;
4302 char *result_str;
4303
4304 switch (xpathobj->type)
4305 {
4306 case XPATH_NODESET:
4307 if (xpathobj->nodesetval != NULL)
4308 {
4309 result = xpathobj->nodesetval->nodeNr;
4310 if (astate != NULL)
4311 {
4312 int i;
4313
4314 for (i = 0; i < result; i++)
4315 {
4316 datum = PointerGetDatum(xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[i],
4317 xmlerrcxt));
4318 (void) accumArrayResult(astate, datum, false,
4320 }
4321 }
4322 }
4323 return result;
4324
4325 case XPATH_BOOLEAN:
4326 if (astate == NULL)
4327 return 1;
4328 datum = BoolGetDatum(xpathobj->boolval);
4330 break;
4331
4332 case XPATH_NUMBER:
4333 if (astate == NULL)
4334 return 1;
4335 datum = Float8GetDatum(xpathobj->floatval);
4337 break;
4338
4339 case XPATH_STRING:
4340 if (astate == NULL)
4341 return 1;
4342 datum = CStringGetDatum((char *) xpathobj->stringval);
4344 break;
4345
4346 default:
4347 elog(ERROR, "xpath expression result type %d is unsupported",
4348 xpathobj->type);
4349 return 0; /* keep compiler quiet */
4350 }
4351
4352 /* Common code for scalar-value cases */
4355 (void) accumArrayResult(astate, datum, false,
4357 return 1;
4358}
4359
4360
4361/*
4362 * Common code for xpath() and xmlexists()
4363 *
4364 * Evaluate XPath expression and return number of nodes in res_nitems
4365 * and array of XML values in astate. Either of those pointers can be
4366 * NULL if the corresponding result isn't wanted.
4367 *
4368 * It is up to the user to ensure that the XML passed is in fact
4369 * an XML document - XPath doesn't work easily on fragments without
4370 * a context node being known.
4371 */
4372static void
4374 int *res_nitems, ArrayBuildState *astate)
4375{
4377 volatile xmlParserCtxtPtr ctxt = NULL;
4378 volatile xmlDocPtr doc = NULL;
4379 volatile xmlXPathContextPtr xpathctx = NULL;
4381 volatile xmlXPathObjectPtr xpathobj = NULL;
4382 char *datastr;
4383 int32 len;
4385 xmlChar *string;
4387 size_t xmldecl_len = 0;
4388 int i;
4389 int ndim;
4391 bool *ns_names_uris_nulls;
4392 int ns_count;
4393
4394 /*
4395 * Namespace mappings are passed as text[]. If an empty array is passed
4396 * (ndim = 0, "0-dimensional"), then there are no namespace mappings.
4397 * Else, a 2-dimensional array with length of the second axis being equal
4398 * to 2 should be passed, i.e., every subarray contains 2 elements, the
4399 * first element defining the name, the second one the URI. Example:
4400 * ARRAY[ARRAY['myns', 'http://example.com'], ARRAY['myns2',
4401 * 'http://example2.com']].
4402 */
4403 ndim = namespaces ? ARR_NDIM(namespaces) : 0;
4404 if (ndim != 0)
4405 {
4406 int *dims;
4407
4408 dims = ARR_DIMS(namespaces);
4409
4410 if (ndim != 2 || dims[1] != 2)
4411 ereport(ERROR,
4413 errmsg("invalid array for XML namespace mapping"),
4414 errdetail("The array must be two-dimensional with length of the second axis equal to 2.")));
4415
4416 Assert(ARR_ELEMTYPE(namespaces) == TEXTOID);
4417
4420 &ns_count);
4421
4422 Assert((ns_count % 2) == 0); /* checked above */
4423 ns_count /= 2; /* count pairs only */
4424 }
4425 else
4426 {
4429 ns_count = 0;
4430 }
4431
4432 datastr = VARDATA(data);
4433 len = VARSIZE(data) - VARHDRSZ;
4435 if (xpath_len == 0)
4436 ereport(ERROR,
4438 errmsg("empty XPath expression")));
4439
4440 string = pg_xmlCharStrndup(datastr, len);
4442
4443 /*
4444 * In a UTF8 database, skip any xml declaration, which might assert
4445 * another encoding. Ignore parse_xml_decl() failure, letting
4446 * xmlCtxtReadMemory() report parse errors. Documentation disclaims
4447 * xpath() support for non-ASCII data in non-UTF8 databases, so leave
4448 * those scenarios bug-compatible with historical behavior.
4449 */
4452
4454
4455 PG_TRY();
4456 {
4457 xmlInitParser();
4458
4459 /*
4460 * redundant XML parsing (two parsings for the same value during one
4461 * command execution are possible)
4462 */
4463 ctxt = xmlNewParserCtxt();
4464 if (ctxt == NULL || xmlerrcxt->err_occurred)
4466 "could not allocate parser context");
4467 doc = xmlCtxtReadMemory(ctxt, (char *) string + xmldecl_len,
4468 len - xmldecl_len, NULL, NULL, 0);
4469 if (doc == NULL || xmlerrcxt->err_occurred)
4471 "could not parse XML document");
4473 if (xpathctx == NULL || xmlerrcxt->err_occurred)
4475 "could not allocate XPath context");
4476 xpathctx->node = (xmlNodePtr) doc;
4477
4478 /* register namespaces, if any */
4479 if (ns_count > 0)
4480 {
4481 for (i = 0; i < ns_count; i++)
4482 {
4483 char *ns_name;
4484 char *ns_uri;
4485
4486 if (ns_names_uris_nulls[i * 2] ||
4487 ns_names_uris_nulls[i * 2 + 1])
4488 ereport(ERROR,
4490 errmsg("neither namespace name nor URI may be null")));
4494 (xmlChar *) ns_name,
4495 (xmlChar *) ns_uri) != 0)
4496 ereport(ERROR, /* is this an internal error??? */
4497 (errmsg("could not register XML namespace with name \"%s\" and URI \"%s\"",
4498 ns_name, ns_uri)));
4499 }
4500 }
4501
4502 /*
4503 * Note: here and elsewhere, be careful to use xmlXPathCtxtCompile not
4504 * xmlXPathCompile. In libxml2 2.13.3 and older, the latter function
4505 * fails to defend itself against recursion-to-stack-overflow. See
4506 * https://gitlab.gnome.org/GNOME/libxml2/-/issues/799
4507 */
4509 if (xpathcomp == NULL || xmlerrcxt->err_occurred)
4511 "invalid XPath expression");
4512
4513 /*
4514 * Version 2.6.27 introduces a function named
4515 * xmlXPathCompiledEvalToBoolean, which would be enough for xmlexists,
4516 * but we can derive the existence by whether any nodes are returned,
4517 * thereby preventing a library version upgrade and keeping the code
4518 * the same.
4519 */
4521 if (xpathobj == NULL || xmlerrcxt->err_occurred)
4523 "could not create XPath object");
4524
4525 /*
4526 * Extract the results as requested.
4527 */
4528 if (res_nitems != NULL)
4530 else
4531 (void) xml_xpathobjtoxmlarray(xpathobj, astate, xmlerrcxt);
4532 }
4533 PG_CATCH();
4534 {
4535 if (xpathobj)
4537 if (xpathcomp)
4539 if (xpathctx)
4541 if (doc)
4542 xmlFreeDoc(doc);
4543 if (ctxt)
4544 xmlFreeParserCtxt(ctxt);
4545
4546 pg_xml_done(xmlerrcxt, true);
4547
4548 PG_RE_THROW();
4549 }
4550 PG_END_TRY();
4551
4555 xmlFreeDoc(doc);
4556 xmlFreeParserCtxt(ctxt);
4557
4558 pg_xml_done(xmlerrcxt, false);
4559}
4560#endif /* USE_LIBXML */
4561
4562/*
4563 * Evaluate XPath expression and return array of XML values.
4564 *
4565 * As we have no support of XQuery sequences yet, this function seems
4566 * to be the most useful one (array of XML functions plays a role of
4567 * some kind of substitution for XQuery sequences).
4568 */
4569Datum
4571{
4572#ifdef USE_LIBXML
4575 ArrayType *namespaces = PG_GETARG_ARRAYTYPE_P(2);
4576 ArrayBuildState *astate;
4577
4579 xpath_internal(xpath_expr_text, data, namespaces,
4580 NULL, astate);
4582#else
4584 return 0;
4585#endif
4586}
4587
4588/*
4589 * Determines if the node specified by the supplied XPath exists
4590 * in a given XML document, returning a boolean.
4591 */
4592Datum
4594{
4595#ifdef USE_LIBXML
4598 int res_nitems;
4599
4601 &res_nitems, NULL);
4602
4604#else
4606 return 0;
4607#endif
4608}
4609
4610/*
4611 * Determines if the node specified by the supplied XPath exists
4612 * in a given XML document, returning a boolean. Differs from
4613 * xmlexists as it supports namespaces and is not defined in SQL/XML.
4614 */
4615Datum
4617{
4618#ifdef USE_LIBXML
4621 ArrayType *namespaces = PG_GETARG_ARRAYTYPE_P(2);
4622 int res_nitems;
4623
4624 xpath_internal(xpath_expr_text, data, namespaces,
4625 &res_nitems, NULL);
4626
4628#else
4630 return 0;
4631#endif
4632}
4633
4634/*
4635 * Functions for checking well-formed-ness
4636 */
4637
4638#ifdef USE_LIBXML
4639static bool
4641{
4642 xmlDocPtr doc;
4644
4645 /*
4646 * We'll report "true" if no soft error is reported by xml_parse().
4647 */
4649 GetDatabaseEncoding(), NULL, NULL, (Node *) &escontext);
4650 if (doc)
4651 xmlFreeDoc(doc);
4652
4653 return !escontext.error_occurred;
4654}
4655#endif
4656
4657Datum
4659{
4660#ifdef USE_LIBXML
4662
4664#else
4666 return 0;
4667#endif /* not USE_LIBXML */
4668}
4669
4670Datum
4672{
4673#ifdef USE_LIBXML
4675
4677#else
4679 return 0;
4680#endif /* not USE_LIBXML */
4681}
4682
4683Datum
4685{
4686#ifdef USE_LIBXML
4688
4690#else
4692 return 0;
4693#endif /* not USE_LIBXML */
4694}
4695
4696/*
4697 * support functions for XMLTABLE
4698 *
4699 */
4700#ifdef USE_LIBXML
4701
4702/*
4703 * Returns private data from executor state. Ensure validity by check with
4704 * MAGIC number.
4705 */
4706static inline XmlTableBuilderData *
4708{
4710
4712 elog(ERROR, "%s called with invalid TableFuncScanState", fname);
4713 result = (XmlTableBuilderData *) state->opaque;
4714 if (result->magic != XMLTABLE_CONTEXT_MAGIC)
4715 elog(ERROR, "%s called with invalid TableFuncScanState", fname);
4716
4717 return result;
4718}
4719#endif
4720
4721/*
4722 * XmlTableInitOpaque
4723 * Fill in TableFuncScanState->opaque for XmlTable processor; initialize
4724 * the XML parser.
4725 *
4726 * Note: Because we call pg_xml_init() here and pg_xml_done() in
4727 * XmlTableDestroyOpaque, it is critical for robustness that no other
4728 * executor nodes run until this node is processed to completion. Caller
4729 * must execute this to completion (probably filling a tuplestore to exhaust
4730 * this node in a single pass) instead of using row-per-call mode.
4731 */
4732static void
4734{
4735#ifdef USE_LIBXML
4736 volatile xmlParserCtxtPtr ctxt = NULL;
4739
4742 xtCxt->natts = natts;
4743 xtCxt->xpathscomp = palloc0_array(xmlXPathCompExprPtr, natts);
4744
4746
4747 PG_TRY();
4748 {
4749 xmlInitParser();
4750
4751 ctxt = xmlNewParserCtxt();
4752 if (ctxt == NULL || xmlerrcxt->err_occurred)
4754 "could not allocate parser context");
4755 }
4756 PG_CATCH();
4757 {
4758 if (ctxt != NULL)
4759 xmlFreeParserCtxt(ctxt);
4760
4761 pg_xml_done(xmlerrcxt, true);
4762
4763 PG_RE_THROW();
4764 }
4765 PG_END_TRY();
4766
4767 xtCxt->xmlerrcxt = xmlerrcxt;
4768 xtCxt->ctxt = ctxt;
4769
4770 state->opaque = xtCxt;
4771#else
4773#endif /* not USE_LIBXML */
4774}
4775
4776/*
4777 * XmlTableSetDocument
4778 * Install the input document
4779 */
4780static void
4782{
4783#ifdef USE_LIBXML
4786 char *str;
4787 xmlChar *xstr;
4788 int length;
4789 volatile xmlDocPtr doc = NULL;
4790 volatile xmlXPathContextPtr xpathcxt = NULL;
4791
4792 xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetDocument");
4793
4794 /*
4795 * Use out function for casting to string (remove encoding property). See
4796 * comment in xml_out.
4797 */
4799
4800 length = strlen(str);
4801 xstr = pg_xmlCharStrndup(str, length);
4802
4803 PG_TRY();
4804 {
4805 doc = xmlCtxtReadMemory(xtCxt->ctxt, (char *) xstr, length, NULL, NULL, 0);
4806 if (doc == NULL || xtCxt->xmlerrcxt->err_occurred)
4808 "could not parse XML document");
4810 if (xpathcxt == NULL || xtCxt->xmlerrcxt->err_occurred)
4812 "could not allocate XPath context");
4813 xpathcxt->node = (xmlNodePtr) doc;
4814 }
4815 PG_CATCH();
4816 {
4817 if (xpathcxt != NULL)
4819 if (doc != NULL)
4820 xmlFreeDoc(doc);
4821
4822 PG_RE_THROW();
4823 }
4824 PG_END_TRY();
4825
4826 xtCxt->doc = doc;
4827 xtCxt->xpathcxt = xpathcxt;
4828#else
4830#endif /* not USE_LIBXML */
4831}
4832
4833/*
4834 * XmlTableSetNamespace
4835 * Add a namespace declaration
4836 */
4837static void
4839{
4840#ifdef USE_LIBXML
4842
4843 if (name == NULL)
4844 ereport(ERROR,
4846 errmsg("DEFAULT namespace is not supported")));
4847 xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetNamespace");
4848
4849 if (xmlXPathRegisterNs(xtCxt->xpathcxt,
4853 "could not set XML namespace");
4854#else
4856#endif /* not USE_LIBXML */
4857}
4858
4859/*
4860 * XmlTableSetRowFilter
4861 * Install the row-filter Xpath expression.
4862 */
4863static void
4865{
4866#ifdef USE_LIBXML
4868 xmlChar *xstr;
4869
4870 xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetRowFilter");
4871
4872 if (*path == '\0')
4873 ereport(ERROR,
4875 errmsg("row path filter must not be empty string")));
4876
4877 xstr = pg_xmlCharStrndup(path, strlen(path));
4878
4879 /* We require XmlTableSetDocument to have been done already */
4880 Assert(xtCxt->xpathcxt != NULL);
4881
4882 xtCxt->xpathcomp = xmlXPathCtxtCompile(xtCxt->xpathcxt, xstr);
4883 if (xtCxt->xpathcomp == NULL || xtCxt->xmlerrcxt->err_occurred)
4885 "invalid XPath expression");
4886#else
4888#endif /* not USE_LIBXML */
4889}
4890
4891/*
4892 * XmlTableSetColumnFilter
4893 * Install the column-filter Xpath expression, for the given column.
4894 */
4895static void
4896XmlTableSetColumnFilter(TableFuncScanState *state, const char *path, int colnum)
4897{
4898#ifdef USE_LIBXML
4900 xmlChar *xstr;
4901
4902 Assert(path);
4903
4904 xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetColumnFilter");
4905
4906 if (*path == '\0')
4907 ereport(ERROR,
4909 errmsg("column path filter must not be empty string")));
4910
4911 xstr = pg_xmlCharStrndup(path, strlen(path));
4912
4913 /* We require XmlTableSetDocument to have been done already */
4914 Assert(xtCxt->xpathcxt != NULL);
4915
4916 xtCxt->xpathscomp[colnum] = xmlXPathCtxtCompile(xtCxt->xpathcxt, xstr);
4917 if (xtCxt->xpathscomp[colnum] == NULL || xtCxt->xmlerrcxt->err_occurred)
4919 "invalid XPath expression");
4920#else
4922#endif /* not USE_LIBXML */
4923}
4924
4925/*
4926 * XmlTableFetchRow
4927 * Prepare the next "current" tuple for upcoming GetValue calls.
4928 * Returns false if the row-filter expression returned no more rows.
4929 */
4930static bool
4932{
4933#ifdef USE_LIBXML
4935
4936 xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableFetchRow");
4937
4938 /* Propagate our own error context to libxml2 */
4940
4941 if (xtCxt->xpathobj == NULL)
4942 {
4943 xtCxt->xpathobj = xmlXPathCompiledEval(xtCxt->xpathcomp, xtCxt->xpathcxt);
4944 if (xtCxt->xpathobj == NULL || xtCxt->xmlerrcxt->err_occurred)
4946 "could not create XPath object");
4947
4948 xtCxt->row_count = 0;
4949 }
4950
4951 if (xtCxt->xpathobj->type == XPATH_NODESET)
4952 {
4953 if (xtCxt->xpathobj->nodesetval != NULL)
4954 {
4955 if (xtCxt->row_count++ < xtCxt->xpathobj->nodesetval->nodeNr)
4956 return true;
4957 }
4958 }
4959
4960 return false;
4961#else
4963 return false;
4964#endif /* not USE_LIBXML */
4965}
4966
4967/*
4968 * XmlTableGetValue
4969 * Return the value for column number 'colnum' for the current row. If
4970 * column -1 is requested, return representation of the whole row.
4971 *
4972 * This leaks memory, so be sure to reset often the context in which it's
4973 * called.
4974 */
4975static Datum
4977 Oid typid, int32 typmod, bool *isnull)
4978{
4979#ifdef USE_LIBXML
4980 Datum result = (Datum) 0;
4982 volatile xmlXPathObjectPtr xpathobj = NULL;
4983
4984 xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableGetValue");
4985
4986 Assert(xtCxt->xpathobj &&
4987 xtCxt->xpathobj->type == XPATH_NODESET &&
4988 xtCxt->xpathobj->nodesetval != NULL);
4989
4990 /* Propagate our own error context to libxml2 */
4992
4993 *isnull = false;
4994
4995 Assert(xtCxt->xpathscomp[colnum] != NULL);
4996
4997 PG_TRY();
4998 {
5000 char *cstr = NULL;
5001
5002 /* Set current node as entry point for XPath evaluation */
5003 cur = xtCxt->xpathobj->nodesetval->nodeTab[xtCxt->row_count - 1];
5004 xtCxt->xpathcxt->node = cur;
5005
5006 /* Evaluate column path */
5007 xpathobj = xmlXPathCompiledEval(xtCxt->xpathscomp[colnum], xtCxt->xpathcxt);
5008 if (xpathobj == NULL || xtCxt->xmlerrcxt->err_occurred)
5010 "could not create XPath object");
5011
5012 /*
5013 * There are four possible cases, depending on the number of nodes
5014 * returned by the XPath expression and the type of the target column:
5015 * a) XPath returns no nodes. b) The target type is XML (return all
5016 * as XML). For non-XML return types: c) One node (return content).
5017 * d) Multiple nodes (error).
5018 */
5019 if (xpathobj->type == XPATH_NODESET)
5020 {
5021 int count = 0;
5022
5023 if (xpathobj->nodesetval != NULL)
5024 count = xpathobj->nodesetval->nodeNr;
5025
5026 if (xpathobj->nodesetval == NULL || count == 0)
5027 {
5028 *isnull = true;
5029 }
5030 else
5031 {
5032 if (typid == XMLOID)
5033 {
5034 text *textstr;
5036
5037 /* Concatenate serialized values */
5039 for (int i = 0; i < count; i++)
5040 {
5041 textstr =
5042 xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[i],
5043 xtCxt->xmlerrcxt);
5044
5046 }
5047 cstr = str.data;
5048 }
5049 else
5050 {
5051 xmlChar *str;
5052
5053 if (count > 1)
5054 ereport(ERROR,
5056 errmsg("more than one value returned by column XPath expression")));
5057
5059 cstr = str ? xml_pstrdup_and_free(str) : "";
5060 }
5061 }
5062 }
5063 else if (xpathobj->type == XPATH_STRING)
5064 {
5065 /* Content should be escaped when target will be XML */
5066 if (typid == XMLOID)
5067 cstr = escape_xml((char *) xpathobj->stringval);
5068 else
5069 cstr = (char *) xpathobj->stringval;
5070 }
5071 else if (xpathobj->type == XPATH_BOOLEAN)
5072 {
5073 char typcategory;
5074 bool typispreferred;
5075 xmlChar *str;
5076
5077 /* Allow implicit casting from boolean to numbers */
5079
5082 else
5084
5086 }
5087 else if (xpathobj->type == XPATH_NUMBER)
5088 {
5089 xmlChar *str;
5090
5093 }
5094 else
5095 elog(ERROR, "unexpected XPath object type %u", xpathobj->type);
5096
5097 /*
5098 * By here, either cstr contains the result value, or the isnull flag
5099 * has been set.
5100 */
5101 Assert(cstr || *isnull);
5102
5103 if (!*isnull)
5104 result = InputFunctionCall(&state->in_functions[colnum],
5105 cstr,
5106 state->typioparams[colnum],
5107 typmod);
5108 }
5109 PG_FINALLY();
5110 {
5111 if (xpathobj != NULL)
5113 }
5114 PG_END_TRY();
5115
5116 return result;
5117#else
5119 return 0;
5120#endif /* not USE_LIBXML */
5121}
5122
5123/*
5124 * XmlTableDestroyOpaque
5125 * Release all libxml2 resources
5126 */
5127static void
5129{
5130#ifdef USE_LIBXML
5132
5133 xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableDestroyOpaque");
5134
5135 /* Propagate our own error context to libxml2 */
5137
5138 if (xtCxt->xpathscomp != NULL)
5139 {
5140 int i;
5141
5142 for (i = 0; i < xtCxt->natts; i++)
5143 if (xtCxt->xpathscomp[i] != NULL)
5144 xmlXPathFreeCompExpr(xtCxt->xpathscomp[i]);
5145 }
5146
5147 if (xtCxt->xpathobj != NULL)
5148 xmlXPathFreeObject(xtCxt->xpathobj);
5149 if (xtCxt->xpathcomp != NULL)
5150 xmlXPathFreeCompExpr(xtCxt->xpathcomp);
5151 if (xtCxt->xpathcxt != NULL)
5152 xmlXPathFreeContext(xtCxt->xpathcxt);
5153 if (xtCxt->doc != NULL)
5154 xmlFreeDoc(xtCxt->doc);
5155 if (xtCxt->ctxt != NULL)
5156 xmlFreeParserCtxt(xtCxt->ctxt);
5157
5158 pg_xml_done(xtCxt->xmlerrcxt, true);
5159
5160 /* not valid anymore */
5161 xtCxt->magic = 0;
5162 state->opaque = NULL;
5163
5164#else
5166#endif /* not USE_LIBXML */
5167}
#define ARR_NDIM(a)
Definition array.h:290
#define PG_GETARG_ARRAYTYPE_P(n)
Definition array.h:263
#define DatumGetArrayTypeP(X)
Definition array.h:261
#define ARR_ELEMTYPE(a)
Definition array.h:292
#define ARR_DIMS(a)
Definition array.h:294
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_builtin(const ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
void deconstruct_array(const ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
void j2date(int jd, int *year, int *month, int *day)
Definition datetime.c:322
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition datetime.c:4465
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
Definition datetime.c:4350
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition timestamp.c:1904
#define TextDatumGetCString(d)
Definition builtins.h:99
#define NameStr(name)
Definition c.h:835
#define gettext_noop(x)
Definition c.h:1285
#define INT64_FORMAT
Definition c.h:634
#define VARHDRSZ
Definition c.h:781
#define Assert(condition)
Definition c.h:943
int16_t int16
Definition c.h:619
#define CppAsString2(x)
Definition c.h:506
int32_t int32
Definition c.h:620
#define PG_INT64_MAX
Definition c.h:676
#define PG_INT64_MIN
Definition c.h:675
uint64_t uint64
Definition c.h:625
#define pg_fallthrough
Definition c.h:161
#define OidIsValid(objectId)
Definition c.h:858
uint32 result
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
int nspid
int64 Timestamp
Definition timestamp.h:38
int64 TimestampTz
Definition timestamp.h:39
int32 fsec_t
Definition timestamp.h:41
#define TIMESTAMP_NOT_FINITE(j)
Definition timestamp.h:169
#define POSTGRES_EPOCH_JDATE
Definition timestamp.h:235
#define DATE_NOT_FINITE(j)
Definition date.h:49
int32 DateADT
Definition date.h:21
static DateADT DatumGetDateADT(Datum X)
Definition date.h:60
struct cursor * cur
Definition ecpg.c:29
Datum arg
Definition elog.c:1322
int errcode(int sqlerrcode)
Definition elog.c:874
#define PG_RE_THROW()
Definition elog.h:407
#define errsave(context,...)
Definition elog.h:264
int int errdetail_internal(const char *fmt,...) pg_attribute_printf(1
int errhint(const char *fmt,...) pg_attribute_printf(1
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define FATAL
Definition elog.h:42
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
#define PG_TRY(...)
Definition elog.h:374
#define WARNING
Definition elog.h:37
#define PG_END_TRY(...)
Definition elog.h:399
#define ERROR
Definition elog.h:40
#define PG_CATCH(...)
Definition elog.h:384
#define elog(elevel,...)
Definition elog.h:228
#define NOTICE
Definition elog.h:36
#define PG_FINALLY(...)
Definition elog.h:391
#define ereport(elevel,...)
Definition elog.h:152
#define palloc_object(type)
Definition fe_memutils.h:74
#define palloc0_array(type, count)
Definition fe_memutils.h:77
#define palloc0_object(type)
Definition fe_memutils.h:75
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition fmgr.c:1532
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition fmgr.c:1764
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define PG_RETURN_BYTEA_P(x)
Definition fmgr.h:373
#define DatumGetByteaPP(X)
Definition fmgr.h:292
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define PG_RETURN_CSTRING(x)
Definition fmgr.h:364
#define PG_ARGISNULL(n)
Definition fmgr.h:209
#define DirectFunctionCall1(func, arg1)
Definition fmgr.h:684
#define PG_GETARG_CSTRING(n)
Definition fmgr.h:278
#define PG_RETURN_NULL()
Definition fmgr.h:346
#define PG_GETARG_NAME(n)
Definition fmgr.h:279
#define PG_RETURN_TEXT_P(x)
Definition fmgr.h:374
#define PG_GETARG_INT32(n)
Definition fmgr.h:269
#define PG_GETARG_BOOL(n)
Definition fmgr.h:274
#define PG_RETURN_DATUM(x)
Definition fmgr.h:354
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
Oid MyDatabaseId
Definition globals.c:96
const char * str
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
#define MAXDATELEN
Definition datetime.h:200
#define ident
#define newline
FILE * input
static struct @177 value
static char * encoding
Definition initdb.c:139
int b
Definition isn.c:74
int x
Definition isn.c:75
int a
Definition isn.c:73
int i
Definition isn.c:77
List * lappend(List *list, void *datum)
Definition list.c:339
List * lappend_oid(List *list, Oid datum)
Definition list.c:375
List * list_append_unique_oid(List *list, Oid datum)
Definition list.c:1380
static struct pg_tm tm
Definition localtime.c:104
#define NoLock
Definition lockdefs.h:34
#define AccessShareLock
Definition lockdefs.h:36
char * get_rel_name(Oid relid)
Definition lsyscache.c:2148
char * get_database_name(Oid dbid)
Definition lsyscache.c:1312
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition lsyscache.c:3129
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition lsyscache.c:2491
char get_typtype(Oid typid)
Definition lsyscache.c:2851
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition lsyscache.c:2760
Oid getBaseType(Oid typid)
Definition lsyscache.c:2743
char * get_namespace_name(Oid nspid)
Definition lsyscache.c:3588
void get_type_category_preferred(Oid typid, char *typcategory, bool *typispreferred)
Definition lsyscache.c:2932
#define type_is_array_domain(typid)
Definition lsyscache.h:222
#define PG_UTF8
Definition mbprint.c:43
unsigned int pg_wchar
Definition mbprint.c:31
int GetDatabaseEncoding(void)
Definition mbutils.c:1388
int pg_mblen_cstr(const char *mbstr)
Definition mbutils.c:1045
char * pg_any_to_server(const char *s, int len, int encoding)
Definition mbutils.c:687
unsigned char * pg_do_encoding_conversion(unsigned char *src, int len, int src_encoding, int dest_encoding)
Definition mbutils.c:365
int pg_get_client_encoding(void)
Definition mbutils.c:345
void pg_unicode_to_server(char32_t c, unsigned char *s)
Definition mbutils.c:875
char * pg_server_to_any(const char *s, int len, int encoding)
Definition mbutils.c:760
int pg_encoding_mb2wchar_with_len(int encoding, const char *from, pg_wchar *to, int len)
Definition mbutils.c:1004
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition mcxt.c:1768
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1232
char * pstrdup(const char *in)
Definition mcxt.c:1781
void * repalloc(void *pointer, Size size)
Definition mcxt.c:1632
void pfree(void *pointer)
Definition mcxt.c:1616
MemoryContext TopMemoryContext
Definition mcxt.c:166
void * palloc(Size size)
Definition mcxt.c:1387
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition memutils.h:160
#define USE_XSD_DATES
Definition miscadmin.h:243
#define SOFT_ERROR_OCCURRED(escontext)
Definition miscnodes.h:53
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
Definition namespace.c:3457
Oid exprType(const Node *expr)
Definition nodeFuncs.c:42
#define IsA(nodeptr, _type_)
Definition nodes.h:164
static char * errmsg
FormData_pg_attribute * Form_pg_attribute
#define ERRCODE_DATA_CORRUPTED
NameData relname
Definition pg_class.h:40
FormData_pg_class * Form_pg_class
Definition pg_class.h:160
const void size_t len
const void * data
#define lfirst(lc)
Definition pg_list.h:172
#define NIL
Definition pg_list.h:68
#define forboth(cell1, list1, cell2, list2)
Definition pg_list.h:550
#define list_make1(x1)
Definition pg_list.h:244
#define lfirst_oid(lc)
Definition pg_list.h:174
#define list_make2(x1, x2)
Definition pg_list.h:246
#define plan(x)
Definition pg_regress.c:164
static char buf[DEFAULT_XLOG_SEG_SIZE]
END_CATALOG_STRUCT typedef FormData_pg_type * Form_pg_type
Definition pg_type.h:265
#define MAX_MULTIBYTE_CHAR_LEN
Definition pg_wchar.h:33
pg_enc
Definition pg_wchar.h:75
#define MAX_UNICODE_EQUIVALENT_STRING
Definition pg_wchar.h:182
#define pg_encoding_to_char
Definition pg_wchar.h:483
#define pg_char_to_encoding
Definition pg_wchar.h:482
long date
Definition pgtypes_date.h:9
int64 timestamp
int pg_strcasecmp(const char *s1, const char *s2)
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
static bool DatumGetBool(Datum X)
Definition postgres.h:100
static Datum PointerGetDatum(const void *X)
Definition postgres.h:342
static Oid DatumGetObjectId(Datum X)
Definition postgres.h:242
static Datum BoolGetDatum(bool X)
Definition postgres.h:112
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
static char * DatumGetCString(Datum X)
Definition postgres.h:355
uint64_t Datum
Definition postgres.h:70
static Datum Float8GetDatum(float8 X)
Definition postgres.h:502
static Datum CStringGetDatum(const char *X)
Definition postgres.h:370
#define InvalidOid
unsigned int Oid
void pq_sendtext(StringInfo buf, const char *str, int slen)
Definition pqformat.c:172
void pq_begintypsend(StringInfo buf)
Definition pqformat.c:325
const char * pq_getmsgbytes(StringInfo msg, int datalen)
Definition pqformat.c:507
bytea * pq_endtypsend(StringInfo buf)
Definition pqformat.c:345
char * c
e
static int fb(int x)
char string[11]
XmlOptionType
Definition primnodes.h:1618
@ XMLOPTION_CONTENT
Definition primnodes.h:1620
@ XMLOPTION_DOCUMENT
Definition primnodes.h:1619
tree ctl root
Definition radixtree.h:1857
Datum regclassout(PG_FUNCTION_ARGS)
Definition regproc.c:951
uint64 SPI_processed
Definition spi.c:45
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition spi.c:1309
const char * SPI_result_code_string(int code)
Definition spi.c:1973
SPITupleTable * SPI_tuptable
Definition spi.c:46
Portal SPI_cursor_find(const char *name)
Definition spi.c:1795
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, const Datum *Values, const char *Nulls, bool read_only)
Definition spi.c:1446
int SPI_connect(void)
Definition spi.c:95
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Definition spi.c:1807
int SPI_finish(void)
Definition spi.c:183
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition spi.c:861
void SPI_cursor_close(Portal portal)
Definition spi.c:1863
void * SPI_palloc(Size size)
Definition spi.c:1339
int SPI_execute(const char *src, bool read_only, long tcount)
Definition spi.c:597
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition spi.c:1253
char * SPI_fname(TupleDesc tupdesc, int fnumber)
Definition spi.c:1199
#define SPI_OK_SELECT
Definition spi.h:86
static void error(void)
char * dbname
Definition streamutil.c:49
struct StringInfoData * StringInfo
Definition string.h:15
StringInfo makeStringInfo(void)
Definition stringinfo.c:72
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition stringinfo.c:145
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
Definition stringinfo.c:281
void appendStringInfoString(StringInfo str, const char *s)
Definition stringinfo.c:230
void appendStringInfoChar(StringInfo str, char ch)
Definition stringinfo.c:242
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
#define appendStringInfoCharMacro(str, ch)
Definition stringinfo.h:231
Definition pg_list.h:54
Definition nodes.h:135
TupleDesc tupDesc
Definition portal.h:159
TupleDesc rd_att
Definition rel.h:112
TupleDesc tupdesc
Definition spi.h:25
HeapTuple * vals
Definition spi.h:26
void(* InitOpaque)(TableFuncScanState *state, int natts)
Definition tablefunc.h:54
List * args
Definition primnodes.h:1635
List * named_args
Definition primnodes.h:1631
Definition c.h:830
Definition pgtime.h:35
int tm_mday
Definition pgtime.h:39
int tm_mon
Definition pgtime.h:40
int tm_year
Definition pgtime.h:41
Definition c.h:776
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:265
HeapTuple SearchSysCache1(SysCacheIdentifier cacheId, Datum key1)
Definition syscache.c:221
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition tupdesc.c:242
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:178
struct TupleDescData * TupleDesc
Definition tupdesc.h:163
static Timestamp DatumGetTimestamp(Datum X)
Definition timestamp.h:28
#define strVal(v)
Definition value.h:82
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition varatt.h:472
static Size VARSIZE(const void *PTR)
Definition varatt.h:298
static char * VARDATA(const void *PTR)
Definition varatt.h:305
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486
static void SET_VARSIZE(void *PTR, Size len)
Definition varatt.h:432
static void appendStringInfoText(StringInfo str, const text *t)
Definition varlena.c:3120
text * cstring_to_text_with_len(const char *s, int len)
Definition varlena.c:196
text * cstring_to_text(const char *s)
Definition varlena.c:184
char * text_to_cstring(const text *t)
Definition varlena.c:217
const char * type
const char * name
int pg_encoding_mblen(int encoding, const char *mbstr)
Definition wchar.c:1934
Datum xml_in(PG_FUNCTION_ARGS)
Definition xml.c:272
Datum cursor_to_xmlschema(PG_FUNCTION_ARGS)
Definition xml.c:3136
#define NO_XML_SUPPORT()
Definition xml.c:234
Datum table_to_xml(PG_FUNCTION_ARGS)
Definition xml.c:2927
Datum query_to_xmlschema(PG_FUNCTION_ARGS)
Definition xml.c:3107
Datum database_to_xml(PG_FUNCTION_ARGS)
Definition xml.c:3441
static const char * map_sql_catalog_to_xmlschema_types(List *nspid_list, bool nulls, bool tableforest, const char *targetns)
Definition xml.c:3736
xmltype * xmlroot(xmltype *data, text *version, int standalone)
Definition xml.c:1106
static void XmlTableInitOpaque(struct TableFuncScanState *state, int natts)
Definition xml.c:4733
static const char * map_sql_type_to_xml_name(Oid typeoid, int typmod)
Definition xml.c:3793
static const char * map_sql_type_to_xmlschema_type(Oid typeoid, int typmod)
Definition xml.c:3953
Datum texttoxml(PG_FUNCTION_ARGS)
Definition xml.c:659
static char * xml_out_internal(xmltype *x, pg_enc target_encoding)
Definition xml.c:311
char * map_sql_identifier_to_xml_name(const char *ident, bool fully_escaped, bool escape_period)
Definition xml.c:2422
static void xsd_schema_element_start(StringInfo result, const char *targetns)
Definition xml.c:3288
Datum query_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
Definition xml.c:3187
Datum xmltotext(PG_FUNCTION_ARGS)
Definition xml.c:668
Datum schema_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
Definition xml.c:3370
Datum xmlexists(PG_FUNCTION_ARGS)
Definition xml.c:4593
Datum xmltext(PG_FUNCTION_ARGS)
Definition xml.c:527
#define NAMESPACE_XSI
Definition xml.c:243
static char * map_multipart_sql_identifier_to_xml_name(const char *a, const char *b, const char *c, const char *d)
Definition xml.c:3527
int xmlbinary
Definition xml.c:108
static StringInfo database_to_xmlschema_internal(bool nulls, bool tableforest, const char *targetns)
Definition xml.c:3453
Datum database_to_xmlschema(PG_FUNCTION_ARGS)
Definition xml.c:3496
static List * schema_get_xml_visible_tables(Oid nspid)
Definition xml.c:2857
Datum schema_to_xmlschema(PG_FUNCTION_ARGS)
Definition xml.c:3357
text * xmltotext_with_options(xmltype *data, XmlOptionType xmloption_arg, bool indent)
Definition xml.c:678
static void xmldata_root_element_start(StringInfo result, const char *eltname, const char *xmlschema, const char *targetns, bool top_level)
Definition xml.c:3009
char * map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
Definition xml.c:2520
Datum xml_send(PG_FUNCTION_ARGS)
Definition xml.c:438
static const char * map_sql_schema_to_xmlschema_types(Oid nspid, List *relid_list, bool nulls, bool tableforest, const char *targetns)
Definition xml.c:3663
const TableFuncRoutine XmlTableRoutine
Definition xml.c:222
Datum xmlcomment(PG_FUNCTION_ARGS)
Definition xml.c:491
static void XmlTableSetNamespace(struct TableFuncScanState *state, const char *name, const char *uri)
Definition xml.c:4838
Datum xmlconcat2(PG_FUNCTION_ARGS)
Definition xml.c:641
static void XmlTableSetRowFilter(struct TableFuncScanState *state, const char *path)
Definition xml.c:4864
static List * database_get_xml_visible_schemas(void)
Definition xml.c:2884
static void xmldata_root_element_end(StringInfo result, const char *eltname)
Definition xml.c:3036
xmltype * xmlconcat(List *args)
Definition xml.c:575
static Datum XmlTableGetValue(struct TableFuncScanState *state, int colnum, Oid typid, int32 typmod, bool *isnull)
Definition xml.c:4976
char * escape_xml(const char *str)
Definition xml.c:2739
xmltype * xmlparse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace, Node *escontext)
Definition xml.c:1032
Datum xml_is_well_formed_document(PG_FUNCTION_ARGS)
Definition xml.c:4671
Datum query_to_xml(PG_FUNCTION_ARGS)
Definition xml.c:2941
static StringInfo database_to_xml_internal(const char *xmlschema, bool nulls, bool tableforest, const char *targetns)
Definition xml.c:3398
int xmloption
Definition xml.c:109
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition xml.c:467
Datum xml_is_well_formed_content(PG_FUNCTION_ARGS)
Definition xml.c:4684
#define XML_VISIBLE_SCHEMAS
Definition xml.c:2880
static List * database_get_xml_visible_tables(void)
Definition xml.c:2891
bool xml_is_document(xmltype *arg)
Definition xml.c:1172
static StringInfo schema_to_xmlschema_internal(const char *schemaname, bool nulls, bool tableforest, const char *targetns)
Definition xml.c:3312
Datum table_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
Definition xml.c:3166
Datum xmlvalidate(PG_FUNCTION_ARGS)
Definition xml.c:1162
static xmltype * cstring_to_xmltype(const char *string)
Definition xml.c:474
static List * query_to_oid_list(const char *query)
Definition xml.c:2828
static void XmlTableSetDocument(struct TableFuncScanState *state, Datum value)
Definition xml.c:4781
static void XmlTableDestroyOpaque(struct TableFuncScanState *state)
Definition xml.c:5128
static const char * map_sql_typecoll_to_xmlschema_types(List *tupdesc_list)
Definition xml.c:3898
#define NAMESPACE_XSD
Definition xml.c:242
#define PG_XML_DEFAULT_VERSION
Definition xml.c:300
Datum table_to_xmlschema(PG_FUNCTION_ARGS)
Definition xml.c:3088
static StringInfo query_to_xml_internal(const char *query, char *tablename, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition xml.c:3043
static void SPI_sql_row_to_xmlelement(uint64 rownum, StringInfo result, char *tablename, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition xml.c:4128
static StringInfo schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition xml.c:3223
static StringInfo table_to_xml_internal(Oid relid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition xml.c:2910
Datum schema_to_xml(PG_FUNCTION_ARGS)
Definition xml.c:3266
char * map_xml_name_to_sql_identifier(const char *name)
Definition xml.c:2478
Datum database_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
Definition xml.c:3508
static bool XmlTableFetchRow(struct TableFuncScanState *state)
Definition xml.c:4931
Datum cursor_to_xml(PG_FUNCTION_ARGS)
Definition xml.c:2955
Datum xpath_exists(PG_FUNCTION_ARGS)
Definition xml.c:4616
Datum xml_is_well_formed(PG_FUNCTION_ARGS)
Definition xml.c:4658
static char * _SPI_strdup(const char *s)
Definition xml.c:2771
static void XmlTableSetColumnFilter(struct TableFuncScanState *state, const char *path, int colnum)
Definition xml.c:4896
static const char * map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls, bool tableforest, const char *targetns)
Definition xml.c:3558
Datum xml_out(PG_FUNCTION_ARGS)
Definition xml.c:355
Datum xml_recv(PG_FUNCTION_ARGS)
Definition xml.c:370
xmltype * xmlelement(XmlExpr *xexpr, const Datum *named_argvalue, const bool *named_argnull, const Datum *argvalue, const bool *argnull)
Definition xml.c:894
xmltype * xmlpi(const char *target, text *arg, bool arg_is_null, bool *result_is_null)
Definition xml.c:1054
Datum xpath(PG_FUNCTION_ARGS)
Definition xml.c:4570
static void xsd_schema_element_end(StringInfo result)
Definition xml.c:3305
@ XML_STANDALONE_OMITTED
Definition xml.h:30
@ XML_STANDALONE_NO_VALUE
Definition xml.h:29
@ XML_STANDALONE_YES
Definition xml.h:27
@ XML_STANDALONE_NO
Definition xml.h:28
struct PgXmlErrorContext PgXmlErrorContext
Definition xml.h:48
PgXmlErrorContext * pg_xml_init(PgXmlStrictness strictness)
#define PG_RETURN_XML_P(x)
Definition xml.h:63
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
bool pg_xml_error_occurred(PgXmlErrorContext *errcxt)
static xmltype * DatumGetXmlP(Datum X)
Definition xml.h:51
void pg_xml_done(PgXmlErrorContext *errcxt, bool isError)
#define PG_GETARG_XML_P(n)
Definition xml.h:62
void pg_xml_init_library(void)
@ XMLBINARY_BASE64
Definition xml.h:35
PgXmlStrictness
Definition xml.h:40
@ PG_XML_STRICTNESS_LEGACY
Definition xml.h:41
@ PG_XML_STRICTNESS_ALL
Definition xml.h:44
@ PG_XML_STRICTNESS_WELLFORMED
Definition xml.h:43