1 /*-------------------------------------------------------------------------
4 * XML data type support.
7 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.71 2008/03/25 22:42:44 tgl Exp $
12 *-------------------------------------------------------------------------
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
27 * Notes on memory management:
29 * Via callbacks, libxml is told to use palloc and friends for memory
30 * management, within a context that we reset at transaction end (and also at
31 * subtransaction abort) to prevent memory leaks. Resetting at transaction or
32 * subtransaction abort is necessary since we might have thrown a longjmp
33 * while some data structures were not linked from anywhere persistent.
34 * Resetting at transaction commit might not be necessary, but seems a good
35 * idea to forestall long-term leaks.
37 * Sometimes libxml allocates global structures in the hope that it can reuse
38 * them later on. Therefore, before resetting LibxmlContext, we must tell
39 * libxml to discard any global data it has. The libxml API documentation is
40 * not very good about specifying this, but for now we assume that
41 * xmlCleanupParser() will get rid of anything we need to worry about.
43 * We use palloc --- which will throw a longjmp on error --- for allocation
44 * callbacks that officially should act like malloc, ie, return NULL on
45 * out-of-memory. This is a bit risky since there is a chance of leaving
46 * persistent libxml data structures in an inconsistent partially-constructed
47 * state, perhaps leading to crash in xmlCleanupParser(). However, as of
48 * early 2008 it is *known* that libxml can crash on out-of-memory due to
49 * inadequate checks for NULL returns, so this behavior seems the lesser
56 #include <libxml/chvalid.h>
57 #include <libxml/parser.h>
58 #include <libxml/tree.h>
59 #include <libxml/uri.h>
60 #include <libxml/xmlerror.h>
61 #include <libxml/xmlwriter.h>
62 #include <libxml/xpath.h>
63 #include <libxml/xpathInternals.h>
64 #endif /* USE_LIBXML */
66 #include "catalog/namespace.h"
67 #include "catalog/pg_type.h"
68 #include "commands/dbcommands.h"
69 #include "executor/executor.h"
70 #include "executor/spi.h"
72 #include "lib/stringinfo.h"
73 #include "libpq/pqformat.h"
74 #include "mb/pg_wchar.h"
75 #include "miscadmin.h"
76 #include "nodes/execnodes.h"
77 #include "parser/parse_expr.h"
78 #include "utils/array.h"
79 #include "utils/builtins.h"
80 #include "utils/date.h"
81 #include "utils/datetime.h"
82 #include "utils/lsyscache.h"
83 #include "utils/memutils.h"
84 #include "access/tupmacs.h"
85 #include "utils/xml.h"
89 XmlBinaryType xmlbinary;
90 XmlOptionType xmloption;
94 static StringInfo xml_err_buf = NULL;
95 static MemoryContext LibxmlContext = NULL;
97 static void xml_init(void);
98 static void xml_memory_init(void);
99 static void xml_memory_cleanup(void);
100 static void *xml_palloc(size_t size);
101 static void *xml_repalloc(void *ptr, size_t size);
102 static void xml_pfree(void *ptr);
103 static char *xml_pstrdup(const char *string);
104 static void xml_ereport(int level, int sqlcode, const char *msg);
105 static void xml_errorHandler(void *ctxt, const char *msg,...);
106 static void xml_ereport_by_code(int level, int sqlcode,
107 const char *msg, int errcode);
108 static xmlChar *xml_text2xmlChar(text *in);
109 static int parse_xml_decl(const xmlChar * str, size_t *lenp,
110 xmlChar ** version, xmlChar ** encoding, int *standalone);
111 static bool print_xml_decl(StringInfo buf, const xmlChar * version,
112 pg_enc encoding, int standalone);
113 static xmlDocPtr xml_parse(text *data, XmlOptionType xmloption_arg,
114 bool preserve_whitespace, xmlChar * encoding);
115 static text *xml_xmlnodetoxmltype(xmlNodePtr cur);
116 #endif /* USE_LIBXML */
118 static StringInfo query_to_xml_internal(const char *query, char *tablename,
119 const char *xmlschema, bool nulls, bool tableforest,
120 const char *targetns, bool top_level);
121 static const char *map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid,
122 bool nulls, bool tableforest, const char *targetns);
123 static const char *map_sql_schema_to_xmlschema_types(Oid nspid,
124 List *relid_list, bool nulls,
125 bool tableforest, const char *targetns);
126 static const char *map_sql_catalog_to_xmlschema_types(List *nspid_list,
127 bool nulls, bool tableforest,
128 const char *targetns);
129 static const char *map_sql_type_to_xml_name(Oid typeoid, int typmod);
130 static const char *map_sql_typecoll_to_xmlschema_types(List *tupdesc_list);
131 static const char *map_sql_type_to_xmlschema_type(Oid typeoid, int typmod);
132 static void SPI_sql_row_to_xmlelement(int rownum, StringInfo result,
133 char *tablename, bool nulls, bool tableforest,
134 const char *targetns, bool top_level);
136 #define NO_XML_SUPPORT() \
138 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
139 errmsg("unsupported XML feature"), \
140 errdetail("This functionality requires the server to be built with libxml support."), \
141 errhint("You need to rebuild PostgreSQL using --with-libxml.")))
144 /* from SQL/XML:2003 section 4.7 */
145 #define NAMESPACE_XSD "http://www.w3.org/2001/XMLSchema"
146 #define NAMESPACE_XSI "http://www.w3.org/2001/XMLSchema-instance"
147 #define NAMESPACE_SQLXML "http://standards.iso.org/iso/9075/2003/sqlxml"
153 xmlChar_to_encoding(xmlChar * encoding_name)
155 int encoding = pg_char_to_encoding((char *) encoding_name);
159 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
160 errmsg("invalid encoding name \"%s\"",
161 (char *) encoding_name)));
168 * xml_in uses a plain C string to VARDATA conversion, so for the time being
169 * we use the conversion function for the text datatype.
171 * This is only acceptable so long as xmltype and text use the same
175 xml_in(PG_FUNCTION_ARGS)
178 char *s = PG_GETARG_CSTRING(0);
182 vardata = (xmltype *) cstring_to_text(s);
185 * Parse the data to check if it is well-formed XML data. Assume that
186 * ERROR occurred if parsing failed.
188 doc = xml_parse(vardata, xmloption, true, NULL);
191 PG_RETURN_XML_P(vardata);
199 #define PG_XML_DEFAULT_VERSION "1.0"
203 * xml_out_internal uses a plain VARDATA to C string conversion, so for the
204 * time being we use the conversion function for the text datatype.
206 * This is only acceptable so long as xmltype and text use the same
210 xml_out_internal(xmltype *x, pg_enc target_encoding)
222 str = text_to_cstring((text *) x);
226 if ((res_code = parse_xml_decl((xmlChar *) str,
227 &len, &version, &encoding, &standalone)) == 0)
231 initStringInfo(&buf);
233 if (!print_xml_decl(&buf, version, target_encoding, standalone))
236 * If we are not going to produce an XML declaration, eat a single
237 * newline in the original string to prevent empty first lines in
240 if (*(str + len) == '\n')
243 appendStringInfoString(&buf, str + len);
248 xml_ereport_by_code(WARNING, ERRCODE_INTERNAL_ERROR,
249 "could not parse XML declaration in stored value",
257 xml_out(PG_FUNCTION_ARGS)
259 xmltype *x = PG_GETARG_XML_P(0);
262 * xml_out removes the encoding property in all cases. This is because we
263 * cannot control from here whether the datum will be converted to a
264 * different client encoding, so we'd do more harm than good by including
267 PG_RETURN_CSTRING(xml_out_internal(x, 0));
272 xml_recv(PG_FUNCTION_ARGS)
275 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
281 xmlChar *encoding = NULL;
284 * Read the data in raw format. We don't know yet what the encoding is, as
285 * that information is embedded in the xml declaration; so we have to
286 * parse that before converting to server encoding.
288 nbytes = buf->len - buf->cursor;
289 str = (char *) pq_getmsgbytes(buf, nbytes);
292 * We need a null-terminated string to pass to parse_xml_decl(). Rather
293 * than make a separate copy, make the temporary result one byte bigger
294 * than it needs to be.
296 result = palloc(nbytes + 1 + VARHDRSZ);
297 SET_VARSIZE(result, nbytes + VARHDRSZ);
298 memcpy(VARDATA(result), str, nbytes);
299 str = VARDATA(result);
302 parse_xml_decl((xmlChar *) str, NULL, NULL, &encoding, NULL);
305 * Parse the data to check if it is well-formed XML data. Assume that
306 * xml_parse will throw ERROR if not.
308 doc = xml_parse(result, xmloption, true, encoding);
311 /* Now that we know what we're dealing with, convert to server encoding */
312 newstr = (char *) pg_do_encoding_conversion((unsigned char *) str,
315 xmlChar_to_encoding(encoding) :
317 GetDatabaseEncoding());
323 nbytes = strlen(newstr);
325 result = palloc(nbytes + VARHDRSZ);
326 SET_VARSIZE(result, nbytes + VARHDRSZ);
327 memcpy(VARDATA(result), newstr, nbytes);
332 PG_RETURN_XML_P(result);
341 xml_send(PG_FUNCTION_ARGS)
343 xmltype *x = PG_GETARG_XML_P(0);
348 * xml_out_internal doesn't convert the encoding, it just prints the right
349 * declaration. pq_sendtext will do the conversion.
351 outval = xml_out_internal(x, pg_get_client_encoding());
353 pq_begintypsend(&buf);
354 pq_sendtext(&buf, outval, strlen(outval));
356 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
362 appendStringInfoText(StringInfo str, const text *t)
364 appendBinaryStringInfo(str, VARDATA(t), VARSIZE(t) - VARHDRSZ);
370 stringinfo_to_xmltype(StringInfo buf)
375 len = buf->len + VARHDRSZ;
376 result = palloc(len);
377 SET_VARSIZE(result, len);
378 memcpy(VARDATA(result), buf->data, buf->len);
385 cstring_to_xmltype(const char *string)
390 len = strlen(string) + VARHDRSZ;
391 result = palloc(len);
392 SET_VARSIZE(result, len);
393 memcpy(VARDATA(result), string, len - VARHDRSZ);
401 xmlBuffer_to_xmltype(xmlBufferPtr buf)
406 len = xmlBufferLength(buf) + VARHDRSZ;
407 result = palloc(len);
408 SET_VARSIZE(result, len);
409 memcpy(VARDATA(result), xmlBufferContent(buf), len - VARHDRSZ);
417 xmlcomment(PG_FUNCTION_ARGS)
420 text *arg = PG_GETARG_TEXT_P(0);
421 char *argdata = VARDATA(arg);
422 int len = VARSIZE(arg) - VARHDRSZ;
426 /* check for "--" in string or "-" at the end */
427 for (i = 1; i < len; i++)
429 if (argdata[i] == '-' && argdata[i - 1] == '-')
431 (errcode(ERRCODE_INVALID_XML_COMMENT),
432 errmsg("invalid XML comment")));
434 if (len > 0 && argdata[len - 1] == '-')
436 (errcode(ERRCODE_INVALID_XML_COMMENT),
437 errmsg("invalid XML comment")));
439 initStringInfo(&buf);
440 appendStringInfo(&buf, "<!--");
441 appendStringInfoText(&buf, arg);
442 appendStringInfo(&buf, "-->");
444 PG_RETURN_XML_P(stringinfo_to_xmltype(&buf));
454 * TODO: xmlconcat needs to merge the notations and unparsed entities
455 * of the argument values. Not very important in practice, though.
458 xmlconcat(List *args)
461 int global_standalone = 1;
462 xmlChar *global_version = NULL;
463 bool global_version_no_value = false;
467 initStringInfo(&buf);
470 xmltype *x = DatumGetXmlP(PointerGetDatum(lfirst(v)));
476 len = VARSIZE(x) - VARHDRSZ;
477 str = palloc(len + 1);
478 memcpy(str, VARDATA(x), len);
481 parse_xml_decl((xmlChar *) str, &len, &version, NULL, &standalone);
483 if (standalone == 0 && global_standalone == 1)
484 global_standalone = 0;
486 global_standalone = -1;
489 global_version_no_value = true;
490 else if (!global_version)
491 global_version = xmlStrdup(version);
492 else if (xmlStrcmp(version, global_version) != 0)
493 global_version_no_value = true;
495 appendStringInfoString(&buf, str + len);
499 if (!global_version_no_value || global_standalone >= 0)
503 initStringInfo(&buf2);
505 print_xml_decl(&buf2,
506 (!global_version_no_value) ? global_version : NULL,
510 appendStringInfoString(&buf2, buf.data);
514 return stringinfo_to_xmltype(&buf);
526 xmlconcat2(PG_FUNCTION_ARGS)
533 PG_RETURN_XML_P(PG_GETARG_XML_P(1));
535 else if (PG_ARGISNULL(1))
536 PG_RETURN_XML_P(PG_GETARG_XML_P(0));
538 PG_RETURN_XML_P(xmlconcat(list_make2(PG_GETARG_XML_P(0),
539 PG_GETARG_XML_P(1))));
544 texttoxml(PG_FUNCTION_ARGS)
546 text *data = PG_GETARG_TEXT_P(0);
548 PG_RETURN_XML_P(xmlparse(data, xmloption, true));
553 xmltotext(PG_FUNCTION_ARGS)
555 xmltype *data = PG_GETARG_XML_P(0);
557 /* It's actually binary compatible. */
558 PG_RETURN_TEXT_P((text *) data);
563 xmltotext_with_xmloption(xmltype *data, XmlOptionType xmloption_arg)
565 if (xmloption_arg == XMLOPTION_DOCUMENT && !xml_is_document(data))
567 (errcode(ERRCODE_NOT_AN_XML_DOCUMENT),
568 errmsg("not an XML document")));
570 /* It's actually binary compatible, save for the above check. */
571 return (text *) data;
576 xmlelement(XmlExprState *xmlExpr, ExprContext *econtext)
579 XmlExpr *xexpr = (XmlExpr *) xmlExpr->xprstate.expr;
581 List *named_arg_strings;
587 xmlTextWriterPtr writer;
590 * We first evaluate all the arguments, then start up libxml and create
591 * the result. This avoids issues if one of the arguments involves a call
592 * to some other function or subsystem that wants to use libxml on its own
595 named_arg_strings = NIL;
597 foreach(arg, xmlExpr->named_args)
599 ExprState *e = (ExprState *) lfirst(arg);
604 value = ExecEvalExpr(e, econtext, &isnull, NULL);
608 str = OutputFunctionCall(&xmlExpr->named_outfuncs[i], value);
609 named_arg_strings = lappend(named_arg_strings, str);
614 foreach(arg, xmlExpr->args)
616 ExprState *e = (ExprState *) lfirst(arg);
621 value = ExecEvalExpr(e, econtext, &isnull, NULL);
622 /* here we can just forget NULL elements immediately */
625 str = map_sql_value_to_xml_value(value,
626 exprType((Node *) e->expr));
627 arg_strings = lappend(arg_strings, str);
631 /* now safe to run libxml */
634 buf = xmlBufferCreate();
635 writer = xmlNewTextWriterMemory(buf, 0);
637 xmlTextWriterStartElement(writer, (xmlChar *) xexpr->name);
639 forboth(arg, named_arg_strings, narg, xexpr->arg_names)
641 char *str = (char *) lfirst(arg);
642 char *argname = strVal(lfirst(narg));
646 xmlTextWriterWriteAttribute(writer,
653 foreach(arg, arg_strings)
655 char *str = (char *) lfirst(arg);
657 xmlTextWriterWriteRaw(writer, (xmlChar *) str);
660 xmlTextWriterEndElement(writer);
661 xmlFreeTextWriter(writer);
663 result = xmlBuffer_to_xmltype(buf);
675 xmlparse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace)
680 doc = xml_parse(data, xmloption_arg, preserve_whitespace, NULL);
683 return (xmltype *) data;
692 xmlpi(char *target, text *arg, bool arg_is_null, bool *result_is_null)
698 if (pg_strcasecmp(target, "xml") == 0)
700 (errcode(ERRCODE_SYNTAX_ERROR), /* really */
701 errmsg("invalid XML processing instruction"),
702 errdetail("XML processing instruction target name cannot be \"%s\".", target)));
705 * Following the SQL standard, the null check comes after the syntax check
708 *result_is_null = arg_is_null;
712 initStringInfo(&buf);
714 appendStringInfo(&buf, "<?%s", target);
720 string = text_to_cstring(arg);
721 if (strstr(string, "?>") != NULL)
723 (errcode(ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION),
724 errmsg("invalid XML processing instruction"),
725 errdetail("XML processing instruction cannot contain \"?>\".")));
727 appendStringInfoChar(&buf, ' ');
728 appendStringInfoString(&buf, string + strspn(string, " "));
731 appendStringInfoString(&buf, "?>");
733 result = stringinfo_to_xmltype(&buf);
744 xmlroot(xmltype *data, text *version, int standalone)
749 xmlChar *orig_version;
753 len = VARSIZE(data) - VARHDRSZ;
754 str = palloc(len + 1);
755 memcpy(str, VARDATA(data), len);
758 parse_xml_decl((xmlChar *) str, &len, &orig_version, NULL, &orig_standalone);
761 orig_version = xml_text2xmlChar(version);
767 case XML_STANDALONE_YES:
770 case XML_STANDALONE_NO:
773 case XML_STANDALONE_NO_VALUE:
774 orig_standalone = -1;
776 case XML_STANDALONE_OMITTED:
777 /* leave original value */
781 initStringInfo(&buf);
782 print_xml_decl(&buf, orig_version, 0, orig_standalone);
783 appendStringInfoString(&buf, str + len);
785 return stringinfo_to_xmltype(&buf);
794 * Validate document (given as string) against DTD (given as external link)
796 * This has been removed because it is a security hole: unprivileged users
797 * should not be able to use Postgres to fetch arbitrary external files,
798 * which unfortunately is exactly what libxml is willing to do with the DTD
802 xmlvalidate(PG_FUNCTION_ARGS)
805 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
806 errmsg("xmlvalidate is not implemented")));
812 xml_is_document(xmltype *arg)
816 xmlDocPtr doc = NULL;
817 MemoryContext ccxt = CurrentMemoryContext;
821 doc = xml_parse((text *) arg, XMLOPTION_DOCUMENT, true, NULL);
829 ecxt = MemoryContextSwitchTo(ccxt);
830 errdata = CopyErrorData();
831 if (errdata->sqlerrcode == ERRCODE_INVALID_XML_DOCUMENT)
838 MemoryContextSwitchTo(ecxt);
848 #else /* not USE_LIBXML */
851 #endif /* not USE_LIBXML */
856 * xml cleanup function for transaction end. This is also called on
857 * subtransaction abort; see notes at top of file for rationale.
863 xml_memory_cleanup();
871 * Set up for use of libxml --- this should be called by each function that
872 * is about to use libxml facilities.
874 * TODO: xmlChar is utf8-char, make proper tuning (initdb with enc!=utf8 and
880 static bool first_time = true;
884 /* Stuff we need do only once per session */
885 MemoryContext oldcontext;
888 * Currently, we have no pure UTF-8 support for internals -- check if
891 if (sizeof(char) != sizeof(xmlChar))
893 (errmsg("could not initialize XML library"),
894 errdetail("libxml2 has incompatible char type: sizeof(char)=%u, sizeof(xmlChar)=%u.",
895 (int) sizeof(char), (int) sizeof(xmlChar))));
897 /* create error buffer in permanent context */
898 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
899 xml_err_buf = makeStringInfo();
900 MemoryContextSwitchTo(oldcontext);
902 /* Now that xml_err_buf exists, safe to call xml_errorHandler */
903 xmlSetGenericErrorFunc(NULL, xml_errorHandler);
905 /* Set up memory allocation our way, too */
908 /* Check library compatibility */
915 /* Reset pre-existing buffer to empty */
916 Assert(xml_err_buf != NULL);
917 resetStringInfo(xml_err_buf);
920 * We re-establish the callback functions every time. This makes it
921 * safe for other subsystems (PL/Perl, say) to also use libxml with
922 * their own callbacks ... so long as they likewise set up the
923 * callbacks on every use. It's cheap enough to not be worth worrying
926 xmlSetGenericErrorFunc(NULL, xml_errorHandler);
933 * SQL/XML allows storing "XML documents" or "XML content". "XML
934 * documents" are specified by the XML specification and are parsed
935 * easily by libxml. "XML content" is specified by SQL/XML as the
936 * production "XMLDecl? content". But libxml can only parse the
937 * "content" part, so we have to parse the XML declaration ourselves
941 #define CHECK_XML_SPACE(p) \
943 if (!xmlIsBlank_ch(*(p))) \
944 return XML_ERR_SPACE_REQUIRED; \
947 #define SKIP_XML_SPACE(p) \
948 while (xmlIsBlank_ch(*(p))) (p)++
950 /* Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender */
951 /* Beware of multiple evaluations of argument! */
952 #define PG_XMLISNAMECHAR(c) \
953 (xmlIsBaseChar_ch(c) || xmlIsIdeographicQ(c) \
954 || xmlIsDigit_ch(c) \
955 || c == '.' || c == '-' || c == '_' || c == ':' \
956 || xmlIsCombiningQ(c) \
957 || xmlIsExtender_ch(c))
960 parse_xml_decl(const xmlChar * str, size_t *lenp,
961 xmlChar ** version, xmlChar ** encoding, int *standalone)
964 const xmlChar *save_p;
980 if (xmlStrncmp(p, (xmlChar *) "<?xml", 5) != 0)
983 /* if next char is name char, it's a PI like <?xml-stylesheet ...?> */
984 utf8len = strlen((const char *) (p + 5));
985 utf8char = xmlGetUTF8Char(p + 5, &utf8len);
986 if (PG_XMLISNAMECHAR(utf8char))
994 if (xmlStrncmp(p, (xmlChar *) "version", 7) != 0)
995 return XML_ERR_VERSION_MISSING;
999 return XML_ERR_VERSION_MISSING;
1003 if (*p == '\'' || *p == '"')
1007 q = xmlStrchr(p + 1, *p);
1009 return XML_ERR_VERSION_MISSING;
1012 *version = xmlStrndup(p + 1, q - p - 1);
1016 return XML_ERR_VERSION_MISSING;
1021 if (xmlStrncmp(p, (xmlChar *) "encoding", 8) == 0)
1023 CHECK_XML_SPACE(save_p);
1027 return XML_ERR_MISSING_ENCODING;
1031 if (*p == '\'' || *p == '"')
1035 q = xmlStrchr(p + 1, *p);
1037 return XML_ERR_MISSING_ENCODING;
1040 *encoding = xmlStrndup(p + 1, q - p - 1);
1044 return XML_ERR_MISSING_ENCODING;
1054 if (xmlStrncmp(p, (xmlChar *) "standalone", 10) == 0)
1056 CHECK_XML_SPACE(save_p);
1060 return XML_ERR_STANDALONE_VALUE;
1063 if (xmlStrncmp(p, (xmlChar *) "'yes'", 5) == 0 ||
1064 xmlStrncmp(p, (xmlChar *) "\"yes\"", 5) == 0)
1069 else if (xmlStrncmp(p, (xmlChar *) "'no'", 4) == 0 ||
1070 xmlStrncmp(p, (xmlChar *) "\"no\"", 4) == 0)
1076 return XML_ERR_STANDALONE_VALUE;
1084 if (xmlStrncmp(p, (xmlChar *) "?>", 2) != 0)
1085 return XML_ERR_XMLDECL_NOT_FINISHED;
1091 for (p = str; p < str + len; p++)
1093 return XML_ERR_INVALID_CHAR;
1103 * Write an XML declaration. On output, we adjust the XML declaration
1104 * as follows. (These rules are the moral equivalent of the clause
1105 * "Serialization of an XML value" in the SQL standard.)
1107 * We try to avoid generating an XML declaration if possible. This is
1108 * so that you don't get trivial things like xml '<foo/>' resulting in
1109 * '<?xml version="1.0"?><foo/>', which would surely be annoying. We
1110 * must provide a declaration if the standalone property is specified
1111 * or if we include an encoding declaration. If we have a
1112 * declaration, we must specify a version (XML requires this).
1113 * Otherwise we only make a declaration if the version is not "1.0",
1114 * which is the default version specified in SQL:2003.
1117 print_xml_decl(StringInfo buf, const xmlChar * version,
1118 pg_enc encoding, int standalone)
1122 if ((version && strcmp((char *) version, PG_XML_DEFAULT_VERSION) != 0)
1123 || (encoding && encoding != PG_UTF8)
1124 || standalone != -1)
1126 appendStringInfoString(buf, "<?xml");
1129 appendStringInfo(buf, " version=\"%s\"", version);
1131 appendStringInfo(buf, " version=\"%s\"", PG_XML_DEFAULT_VERSION);
1133 if (encoding && encoding != PG_UTF8)
1136 * XXX might be useful to convert this to IANA names (ISO-8859-1
1137 * instead of LATIN1 etc.); needs field experience
1139 appendStringInfo(buf, " encoding=\"%s\"",
1140 pg_encoding_to_char(encoding));
1143 if (standalone == 1)
1144 appendStringInfoString(buf, " standalone=\"yes\"");
1145 else if (standalone == 0)
1146 appendStringInfoString(buf, " standalone=\"no\"");
1147 appendStringInfoString(buf, "?>");
1157 * Convert a C string to XML internal representation
1159 * TODO maybe, libxml2's xmlreader is better? (do not construct DOM,
1160 * yet do not use SAX - see xmlreader.c)
1163 xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace,
1168 xmlChar *utf8string;
1169 xmlParserCtxtPtr ctxt;
1172 len = VARSIZE(data) - VARHDRSZ; /* will be useful later */
1173 string = xml_text2xmlChar(data);
1175 utf8string = pg_do_encoding_conversion(string,
1178 xmlChar_to_encoding(encoding) :
1179 GetDatabaseEncoding(),
1184 ctxt = xmlNewParserCtxt();
1186 xml_ereport(ERROR, ERRCODE_OUT_OF_MEMORY,
1187 "could not allocate parser context");
1189 if (xmloption_arg == XMLOPTION_DOCUMENT)
1192 * Note, that here we try to apply DTD defaults
1193 * (XML_PARSE_DTDATTR) according to SQL/XML:10.16.7.d: 'Default
1194 * values defined by internal DTD are applied'. As for external
1195 * DTDs, we try to support them too, (see SQL/XML:10.16.7.e)
1197 doc = xmlCtxtReadDoc(ctxt, utf8string,
1200 XML_PARSE_NOENT | XML_PARSE_DTDATTR
1201 | (preserve_whitespace ? 0 : XML_PARSE_NOBLANKS));
1203 xml_ereport(ERROR, ERRCODE_INVALID_XML_DOCUMENT,
1204 "invalid XML document");
1210 xmlChar *version = NULL;
1211 int standalone = -1;
1213 doc = xmlNewDoc(NULL);
1215 res_code = parse_xml_decl(utf8string,
1216 &count, &version, NULL, &standalone);
1218 xml_ereport_by_code(ERROR, ERRCODE_INVALID_XML_CONTENT,
1219 "invalid XML content: invalid XML declaration",
1222 res_code = xmlParseBalancedChunkMemory(doc, NULL, NULL, 0,
1223 utf8string + count, NULL);
1225 xml_ereport(ERROR, ERRCODE_INVALID_XML_CONTENT,
1226 "invalid XML content");
1228 doc->version = xmlStrdup(version);
1229 doc->encoding = xmlStrdup((xmlChar *) "UTF-8");
1230 doc->standalone = standalone;
1233 xmlFreeParserCtxt(ctxt);
1240 * xmlChar<->text convertions
1243 xml_text2xmlChar(text *in)
1245 int32 len = VARSIZE(in) - VARHDRSZ;
1248 res = palloc(len + 1);
1249 memcpy(res, VARDATA(in), len);
1257 * Manage the special context used for all libxml allocations
1260 xml_memory_init(void)
1263 * Create memory context if not there already. We make it a child of
1264 * TopMemoryContext, even though our current policy is that it doesn't
1265 * survive past transaction end, because we want to be really really
1266 * sure it doesn't go away before we've called xmlCleanupParser().
1268 if (LibxmlContext == NULL)
1269 LibxmlContext = AllocSetContextCreate(TopMemoryContext,
1271 ALLOCSET_DEFAULT_MINSIZE,
1272 ALLOCSET_DEFAULT_INITSIZE,
1273 ALLOCSET_DEFAULT_MAXSIZE);
1275 /* Re-establish the callbacks even if already set */
1276 xmlMemSetup(xml_pfree, xml_palloc, xml_repalloc, xml_pstrdup);
1280 xml_memory_cleanup(void)
1282 if (LibxmlContext != NULL)
1284 /* Give libxml a chance to clean up dangling pointers */
1287 /* And flush the context */
1288 MemoryContextDelete(LibxmlContext);
1289 LibxmlContext = NULL;
1294 * Wrappers for memory management functions
1297 xml_palloc(size_t size)
1299 return MemoryContextAlloc(LibxmlContext, size);
1304 xml_repalloc(void *ptr, size_t size)
1306 return repalloc(ptr, size);
1311 xml_pfree(void *ptr)
1318 xml_pstrdup(const char *string)
1320 return MemoryContextStrdup(LibxmlContext, string);
1325 * Wrapper for "ereport" function for XML-related errors. The "msg"
1326 * is the SQL-level message; some can be adopted from the SQL/XML
1327 * standard. This function adds libxml's native error messages, if
1331 xml_ereport(int level, int sqlcode, const char *msg)
1335 if (xml_err_buf->len > 0)
1337 detail = pstrdup(xml_err_buf->data);
1338 resetStringInfo(xml_err_buf);
1343 /* libxml error messages end in '\n'; get rid of it */
1348 len = strlen(detail);
1349 if (len > 0 && detail[len - 1] == '\n')
1350 detail[len - 1] = '\0';
1355 errdetail("%s", detail)));
1361 errmsg("%s", msg)));
1367 * Error handler for libxml error messages
1370 xml_errorHandler(void *ctxt, const char *msg,...)
1372 /* Append the formatted text to xml_err_buf */
1378 /* Try to format the data. */
1379 va_start(args, msg);
1380 success = appendStringInfoVA(xml_err_buf, msg, args);
1386 /* Double the buffer size and try again. */
1387 enlargeStringInfo(xml_err_buf, xml_err_buf->maxlen);
1393 * Wrapper for "ereport" function for XML-related errors. The "msg"
1394 * is the SQL-level message; some can be adopted from the SQL/XML
1395 * standard. This function uses "code" to create a textual detail
1396 * message. At the moment, we only need to cover those codes that we
1397 * may raise in this file.
1400 xml_ereport_by_code(int level, int sqlcode,
1401 const char *msg, int code)
1407 case XML_ERR_INVALID_CHAR:
1408 det = gettext_noop("Invalid character value.");
1410 case XML_ERR_SPACE_REQUIRED:
1411 det = gettext_noop("Space required.");
1413 case XML_ERR_STANDALONE_VALUE:
1414 det = gettext_noop("standalone accepts only 'yes' or 'no'.");
1416 case XML_ERR_VERSION_MISSING:
1417 det = gettext_noop("Malformed declaration: missing version.");
1419 case XML_ERR_MISSING_ENCODING:
1420 det = gettext_noop("Missing encoding in text declaration.");
1422 case XML_ERR_XMLDECL_NOT_FINISHED:
1423 det = gettext_noop("Parsing XML declaration: '?>' expected.");
1426 det = gettext_noop("Unrecognized libxml error code: %d.");
1433 errdetail(det, code)));
1438 * Convert one char in the current server encoding to a Unicode codepoint.
1441 sqlchar_to_unicode(char *s)
1444 pg_wchar ret[2]; /* need space for trailing zero */
1446 utf8string = (char *) pg_do_encoding_conversion((unsigned char *) s,
1448 GetDatabaseEncoding(),
1451 pg_encoding_mb2wchar_with_len(PG_UTF8, utf8string, ret, pg_mblen(s));
1458 is_valid_xml_namefirst(pg_wchar c)
1460 /* (Letter | '_' | ':') */
1461 return (xmlIsBaseCharQ(c) || xmlIsIdeographicQ(c)
1462 || c == '_' || c == ':');
1467 is_valid_xml_namechar(pg_wchar c)
1469 /* Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender */
1470 return (xmlIsBaseCharQ(c) || xmlIsIdeographicQ(c)
1472 || c == '.' || c == '-' || c == '_' || c == ':'
1473 || xmlIsCombiningQ(c)
1474 || xmlIsExtenderQ(c));
1476 #endif /* USE_LIBXML */
1480 * Map SQL identifier to XML name; see SQL/XML:2003 section 9.1.
1483 map_sql_identifier_to_xml_name(char *ident, bool fully_escaped,
1491 * SQL/XML doesn't make use of this case anywhere, so it's probably a
1494 Assert(fully_escaped || !escape_period);
1496 initStringInfo(&buf);
1498 for (p = ident; *p; p += pg_mblen(p))
1500 if (*p == ':' && (p == ident || fully_escaped))
1501 appendStringInfo(&buf, "_x003A_");
1502 else if (*p == '_' && *(p + 1) == 'x')
1503 appendStringInfo(&buf, "_x005F_");
1504 else if (fully_escaped && p == ident &&
1505 pg_strncasecmp(p, "xml", 3) == 0)
1508 appendStringInfo(&buf, "_x0078_");
1510 appendStringInfo(&buf, "_x0058_");
1512 else if (escape_period && *p == '.')
1513 appendStringInfo(&buf, "_x002E_");
1516 pg_wchar u = sqlchar_to_unicode(p);
1519 ? !is_valid_xml_namefirst(u)
1520 : !is_valid_xml_namechar(u))
1521 appendStringInfo(&buf, "_x%04X_", (unsigned int) u);
1523 appendBinaryStringInfo(&buf, p, pg_mblen(p));
1528 #else /* not USE_LIBXML */
1531 #endif /* not USE_LIBXML */
1536 * Map a Unicode codepoint into the current server encoding.
1539 unicode_to_sqlchar(pg_wchar c)
1541 static unsigned char utf8string[5]; /* need trailing zero */
1547 else if (c <= 0x7FF)
1549 utf8string[0] = 0xC0 | ((c >> 6) & 0x1F);
1550 utf8string[1] = 0x80 | (c & 0x3F);
1552 else if (c <= 0xFFFF)
1554 utf8string[0] = 0xE0 | ((c >> 12) & 0x0F);
1555 utf8string[1] = 0x80 | ((c >> 6) & 0x3F);
1556 utf8string[2] = 0x80 | (c & 0x3F);
1560 utf8string[0] = 0xF0 | ((c >> 18) & 0x07);
1561 utf8string[1] = 0x80 | ((c >> 12) & 0x3F);
1562 utf8string[2] = 0x80 | ((c >> 6) & 0x3F);
1563 utf8string[3] = 0x80 | (c & 0x3F);
1566 return (char *) pg_do_encoding_conversion(utf8string,
1567 pg_mblen((char *) utf8string),
1569 GetDatabaseEncoding());
1574 * Map XML name to SQL identifier; see SQL/XML:2003 section 9.17.
1577 map_xml_name_to_sql_identifier(char *name)
1582 initStringInfo(&buf);
1584 for (p = name; *p; p += pg_mblen(p))
1586 if (*p == '_' && *(p + 1) == 'x'
1587 && isxdigit((unsigned char) *(p + 2))
1588 && isxdigit((unsigned char) *(p + 3))
1589 && isxdigit((unsigned char) *(p + 4))
1590 && isxdigit((unsigned char) *(p + 5))
1595 sscanf(p + 2, "%X", &u);
1596 appendStringInfoString(&buf, unicode_to_sqlchar(u));
1600 appendBinaryStringInfo(&buf, p, pg_mblen(p));
1607 * Map SQL value to XML value; see SQL/XML:2003 section 9.16.
1610 map_sql_value_to_xml_value(Datum value, Oid type)
1614 initStringInfo(&buf);
1616 if (type_is_array(type))
1628 array = DatumGetArrayTypeP(value);
1629 elmtype = ARR_ELEMTYPE(array);
1630 get_typlenbyvalalign(elmtype, &elmlen, &elmbyval, &elmalign);
1632 deconstruct_array(array, elmtype,
1633 elmlen, elmbyval, elmalign,
1634 &elem_values, &elem_nulls,
1637 for (i = 0; i < num_elems; i++)
1641 appendStringInfoString(&buf, "<element>");
1642 appendStringInfoString(&buf,
1643 map_sql_value_to_xml_value(elem_values[i],
1645 appendStringInfoString(&buf, "</element>");
1659 * Special XSD formatting for some data types
1664 if (DatumGetBool(value))
1673 char buf[MAXDATELEN + 1];
1675 date = DatumGetDateADT(value);
1676 j2date(date + POSTGRES_EPOCH_JDATE,
1677 &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
1678 EncodeDateOnly(&tm, USE_XSD_DATES, buf);
1680 return pstrdup(buf);
1685 Timestamp timestamp;
1689 char buf[MAXDATELEN + 1];
1691 timestamp = DatumGetTimestamp(value);
1693 /* XSD doesn't support infinite values */
1694 if (TIMESTAMP_NOT_FINITE(timestamp))
1696 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1697 errmsg("timestamp out of range")));
1698 else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
1699 EncodeDateTime(&tm, fsec, NULL, &tzn, USE_XSD_DATES, buf);
1702 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1703 errmsg("timestamp out of range")));
1705 return pstrdup(buf);
1708 case TIMESTAMPTZOID:
1710 TimestampTz timestamp;
1715 char buf[MAXDATELEN + 1];
1717 timestamp = DatumGetTimestamp(value);
1719 /* XSD doesn't support infinite values */
1720 if (TIMESTAMP_NOT_FINITE(timestamp))
1722 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1723 errmsg("timestamp out of range")));
1724 else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
1725 EncodeDateTime(&tm, fsec, &tz, &tzn, USE_XSD_DATES, buf);
1728 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1729 errmsg("timestamp out of range")));
1731 return pstrdup(buf);
1735 getTypeOutputInfo(type, &typeOut, &isvarlena);
1736 str = OidOutputFunctionCall(typeOut, value);
1742 if (type == BYTEAOID)
1745 xmlTextWriterPtr writer;
1750 buf = xmlBufferCreate();
1751 writer = xmlNewTextWriterMemory(buf, 0);
1753 if (xmlbinary == XMLBINARY_BASE64)
1754 xmlTextWriterWriteBase64(writer, VARDATA(value), 0, VARSIZE(value) - VARHDRSZ);
1756 xmlTextWriterWriteBinHex(writer, VARDATA(value), 0, VARSIZE(value) - VARHDRSZ);
1758 xmlFreeTextWriter(writer);
1759 result = pstrdup((const char *) xmlBufferContent(buf));
1763 #endif /* USE_LIBXML */
1765 for (p = str; *p; p += pg_mblen(p))
1770 appendStringInfo(&buf, "&");
1773 appendStringInfo(&buf, "<");
1776 appendStringInfo(&buf, ">");
1779 appendStringInfo(&buf, "
");
1782 appendBinaryStringInfo(&buf, p, pg_mblen(p));
1793 _SPI_strdup(const char *s)
1795 size_t len = strlen(s) + 1;
1796 char *ret = SPI_palloc(len);
1798 memcpy(ret, s, len);
1804 * SQL to XML mapping functions
1806 * What follows below is intentionally organized so that you can read
1807 * along in the SQL/XML:2003 standard. The functions are mostly split
1808 * up and ordered they way the clauses lay out in the standards
1809 * document, and the identifiers are also aligned with the standard
1810 * text. (SQL/XML:2006 appears to be ordered differently,
1813 * There are many things going on there:
1815 * There are two kinds of mappings: Mapping SQL data (table contents)
1816 * to XML documents, and mapping SQL structure (the "schema") to XML
1817 * Schema. And there are functions that do both at the same time.
1819 * Then you can map a database, a schema, or a table, each in both
1820 * ways. This breaks down recursively: Mapping a database invokes
1821 * mapping schemas, which invokes mapping tables, which invokes
1822 * mapping rows, which invokes mapping columns, although you can't
1823 * call the last two from the outside. Because of this, there are a
1824 * number of xyz_internal() functions which are to be called both from
1825 * the function manager wrapper and from some upper layer in a
1828 * See the documentation about what the common function arguments
1829 * nulls, tableforest, and targetns mean.
1831 * Some style guidelines for XML output: Use double quotes for quoting
1832 * XML attributes. Indent XML elements by two spaces, but remember
1833 * that a lot of code is called recursively at different levels, so
1834 * it's better not to indent rather than create output that indents
1835 * and outdents weirdly. Add newlines to make the output look nice.
1840 * Visibility of objects for XML mappings; see SQL/XML:2003 section
1845 * Given a query, which must return type oid as first column, produce
1846 * a list of Oids with the query results.
1849 query_to_oid_list(const char *query)
1854 SPI_execute(query, true, 0);
1856 for (i = 0; i < SPI_processed; i++)
1861 oid = SPI_getbinval(SPI_tuptable->vals[i],
1862 SPI_tuptable->tupdesc,
1866 list = lappend_oid(list, DatumGetObjectId(oid));
1874 schema_get_xml_visible_tables(Oid nspid)
1876 StringInfoData query;
1878 initStringInfo(&query);
1879 appendStringInfo(&query, "SELECT oid FROM pg_catalog.pg_class WHERE relnamespace = %u AND relkind IN ('r', 'v') AND pg_catalog.has_table_privilege (oid, 'SELECT') ORDER BY relname;", nspid);
1881 return query_to_oid_list(query.data);
1886 * Including the system schemas is probably not useful for a database
1889 #define XML_VISIBLE_SCHEMAS_EXCLUDE "(nspname ~ '^pg_' OR nspname = 'information_schema')"
1891 #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
1895 database_get_xml_visible_schemas(void)
1897 return query_to_oid_list(XML_VISIBLE_SCHEMAS " ORDER BY nspname;");
1902 database_get_xml_visible_tables(void)
1904 /* At the moment there is no order required here. */
1905 return query_to_oid_list("SELECT oid FROM pg_catalog.pg_class WHERE relkind IN ('r', 'v') AND pg_catalog.has_table_privilege (pg_class.oid, 'SELECT') AND relnamespace IN (" XML_VISIBLE_SCHEMAS ");");
1910 * Map SQL table to XML and/or XML Schema document; see SQL/XML:2003
1915 table_to_xml_internal(Oid relid,
1916 const char *xmlschema, bool nulls, bool tableforest,
1917 const char *targetns, bool top_level)
1919 StringInfoData query;
1921 initStringInfo(&query);
1922 appendStringInfo(&query, "SELECT * FROM %s",
1923 DatumGetCString(DirectFunctionCall1(regclassout,
1924 ObjectIdGetDatum(relid))));
1925 return query_to_xml_internal(query.data, get_rel_name(relid),
1926 xmlschema, nulls, tableforest,
1927 targetns, top_level);
1932 table_to_xml(PG_FUNCTION_ARGS)
1934 Oid relid = PG_GETARG_OID(0);
1935 bool nulls = PG_GETARG_BOOL(1);
1936 bool tableforest = PG_GETARG_BOOL(2);
1937 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
1939 PG_RETURN_XML_P(stringinfo_to_xmltype(table_to_xml_internal(relid, NULL,
1946 query_to_xml(PG_FUNCTION_ARGS)
1948 char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
1949 bool nulls = PG_GETARG_BOOL(1);
1950 bool tableforest = PG_GETARG_BOOL(2);
1951 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
1953 PG_RETURN_XML_P(stringinfo_to_xmltype(query_to_xml_internal(query, NULL,
1954 NULL, nulls, tableforest,
1960 cursor_to_xml(PG_FUNCTION_ARGS)
1962 char *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
1963 int32 count = PG_GETARG_INT32(1);
1964 bool nulls = PG_GETARG_BOOL(2);
1965 bool tableforest = PG_GETARG_BOOL(3);
1966 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(4));
1968 StringInfoData result;
1972 initStringInfo(&result);
1975 portal = SPI_cursor_find(name);
1978 (errcode(ERRCODE_UNDEFINED_CURSOR),
1979 errmsg("cursor \"%s\" does not exist", name)));
1981 SPI_cursor_fetch(portal, true, count);
1982 for (i = 0; i < SPI_processed; i++)
1983 SPI_sql_row_to_xmlelement(i, &result, NULL, nulls,
1984 tableforest, targetns, true);
1988 PG_RETURN_XML_P(stringinfo_to_xmltype(&result));
1993 * Write the start tag of the root element of a data mapping.
1995 * top_level means that this is the very top level of the eventual
1996 * output. For example, when the user calls table_to_xml, then a call
1997 * with a table name to this function is the top level. When the user
1998 * calls database_to_xml, then a call with a schema name to this
1999 * function is not the top level. If top_level is false, then the XML
2000 * namespace declarations are omitted, because they supposedly already
2001 * appeared earlier in the output. Repeating them is not wrong, but
2005 xmldata_root_element_start(StringInfo result, const char *eltname,
2006 const char *xmlschema, const char *targetns,
2009 /* This isn't really wrong but currently makes no sense. */
2010 Assert(top_level || !xmlschema);
2012 appendStringInfo(result, "<%s", eltname);
2015 appendStringInfoString(result, " xmlns:xsi=\"" NAMESPACE_XSI "\"");
2016 if (strlen(targetns) > 0)
2017 appendStringInfo(result, " xmlns=\"%s\"", targetns);
2021 /* FIXME: better targets */
2022 if (strlen(targetns) > 0)
2023 appendStringInfo(result, " xsi:schemaLocation=\"%s #\"", targetns);
2025 appendStringInfo(result, " xsi:noNamespaceSchemaLocation=\"#\"");
2027 appendStringInfo(result, ">\n\n");
2032 xmldata_root_element_end(StringInfo result, const char *eltname)
2034 appendStringInfo(result, "</%s>\n", eltname);
2039 query_to_xml_internal(const char *query, char *tablename,
2040 const char *xmlschema, bool nulls, bool tableforest,
2041 const char *targetns, bool top_level)
2048 xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
2052 result = makeStringInfo();
2055 if (SPI_execute(query, true, 0) != SPI_OK_SELECT)
2057 (errcode(ERRCODE_DATA_EXCEPTION),
2058 errmsg("invalid query")));
2061 xmldata_root_element_start(result, xmltn, xmlschema,
2062 targetns, top_level);
2065 appendStringInfo(result, "%s\n\n", xmlschema);
2067 for (i = 0; i < SPI_processed; i++)
2068 SPI_sql_row_to_xmlelement(i, result, tablename, nulls,
2069 tableforest, targetns, top_level);
2072 xmldata_root_element_end(result, xmltn);
2081 table_to_xmlschema(PG_FUNCTION_ARGS)
2083 Oid relid = PG_GETARG_OID(0);
2084 bool nulls = PG_GETARG_BOOL(1);
2085 bool tableforest = PG_GETARG_BOOL(2);
2086 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2090 rel = heap_open(relid, AccessShareLock);
2091 result = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2092 tableforest, targetns);
2093 heap_close(rel, NoLock);
2095 PG_RETURN_XML_P(cstring_to_xmltype(result));
2100 query_to_xmlschema(PG_FUNCTION_ARGS)
2102 char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2103 bool nulls = PG_GETARG_BOOL(1);
2104 bool tableforest = PG_GETARG_BOOL(2);
2105 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2112 if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
2113 elog(ERROR, "SPI_prepare(\"%s\") failed", query);
2115 if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
2116 elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
2118 result = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2120 tableforest, targetns));
2121 SPI_cursor_close(portal);
2124 PG_RETURN_XML_P(cstring_to_xmltype(result));
2129 cursor_to_xmlschema(PG_FUNCTION_ARGS)
2131 char *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
2132 bool nulls = PG_GETARG_BOOL(1);
2133 bool tableforest = PG_GETARG_BOOL(2);
2134 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2135 const char *xmlschema;
2139 portal = SPI_cursor_find(name);
2142 (errcode(ERRCODE_UNDEFINED_CURSOR),
2143 errmsg("cursor \"%s\" does not exist", name)));
2145 xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2147 tableforest, targetns));
2150 PG_RETURN_XML_P(cstring_to_xmltype(xmlschema));
2155 table_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2157 Oid relid = PG_GETARG_OID(0);
2158 bool nulls = PG_GETARG_BOOL(1);
2159 bool tableforest = PG_GETARG_BOOL(2);
2160 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2162 const char *xmlschema;
2164 rel = heap_open(relid, AccessShareLock);
2165 xmlschema = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2166 tableforest, targetns);
2167 heap_close(rel, NoLock);
2169 PG_RETURN_XML_P(stringinfo_to_xmltype(table_to_xml_internal(relid,
2170 xmlschema, nulls, tableforest,
2176 query_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2178 char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2179 bool nulls = PG_GETARG_BOOL(1);
2180 bool tableforest = PG_GETARG_BOOL(2);
2181 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2183 const char *xmlschema;
2189 if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
2190 elog(ERROR, "SPI_prepare(\"%s\") failed", query);
2192 if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
2193 elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
2195 xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2196 InvalidOid, nulls, tableforest, targetns));
2197 SPI_cursor_close(portal);
2200 PG_RETURN_XML_P(stringinfo_to_xmltype(query_to_xml_internal(query, NULL,
2201 xmlschema, nulls, tableforest,
2207 * Map SQL schema to XML and/or XML Schema document; see SQL/XML:2003
2212 schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls,
2213 bool tableforest, const char *targetns, bool top_level)
2220 xmlsn = map_sql_identifier_to_xml_name(get_namespace_name(nspid),
2222 result = makeStringInfo();
2224 xmldata_root_element_start(result, xmlsn, xmlschema, targetns, top_level);
2227 appendStringInfo(result, "%s\n\n", xmlschema);
2231 relid_list = schema_get_xml_visible_tables(nspid);
2235 foreach(cell, relid_list)
2237 Oid relid = lfirst_oid(cell);
2240 subres = table_to_xml_internal(relid, NULL, nulls, tableforest,
2243 appendStringInfoString(result, subres->data);
2244 appendStringInfoChar(result, '\n');
2250 xmldata_root_element_end(result, xmlsn);
2257 schema_to_xml(PG_FUNCTION_ARGS)
2259 Name name = PG_GETARG_NAME(0);
2260 bool nulls = PG_GETARG_BOOL(1);
2261 bool tableforest = PG_GETARG_BOOL(2);
2262 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2267 schemaname = NameStr(*name);
2268 nspid = LookupExplicitNamespace(schemaname);
2270 PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xml_internal(nspid, NULL,
2271 nulls, tableforest, targetns, true)));
2276 * Write the start element of the root element of an XML Schema mapping.
2279 xsd_schema_element_start(StringInfo result, const char *targetns)
2281 appendStringInfoString(result,
2283 " xmlns:xsd=\"" NAMESPACE_XSD "\"");
2284 if (strlen(targetns) > 0)
2285 appendStringInfo(result,
2287 " targetNamespace=\"%s\"\n"
2288 " elementFormDefault=\"qualified\"",
2290 appendStringInfoString(result,
2296 xsd_schema_element_end(StringInfo result)
2298 appendStringInfoString(result, "</xsd:schema>");
2303 schema_to_xmlschema_internal(const char *schemaname, bool nulls,
2304 bool tableforest, const char *targetns)
2312 result = makeStringInfo();
2314 nspid = LookupExplicitNamespace(schemaname);
2316 xsd_schema_element_start(result, targetns);
2320 relid_list = schema_get_xml_visible_tables(nspid);
2323 foreach(cell, relid_list)
2327 rel = heap_open(lfirst_oid(cell), AccessShareLock);
2328 tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2329 heap_close(rel, NoLock);
2332 appendStringInfoString(result,
2333 map_sql_typecoll_to_xmlschema_types(tupdesc_list));
2335 appendStringInfoString(result,
2336 map_sql_schema_to_xmlschema_types(nspid, relid_list,
2337 nulls, tableforest, targetns));
2339 xsd_schema_element_end(result);
2348 schema_to_xmlschema(PG_FUNCTION_ARGS)
2350 Name name = PG_GETARG_NAME(0);
2351 bool nulls = PG_GETARG_BOOL(1);
2352 bool tableforest = PG_GETARG_BOOL(2);
2353 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2355 PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xmlschema_internal(NameStr(*name),
2356 nulls, tableforest, targetns)));
2361 schema_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2363 Name name = PG_GETARG_NAME(0);
2364 bool nulls = PG_GETARG_BOOL(1);
2365 bool tableforest = PG_GETARG_BOOL(2);
2366 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2369 StringInfo xmlschema;
2371 schemaname = NameStr(*name);
2372 nspid = LookupExplicitNamespace(schemaname);
2374 xmlschema = schema_to_xmlschema_internal(schemaname, nulls,
2375 tableforest, targetns);
2377 PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xml_internal(nspid,
2378 xmlschema->data, nulls,
2379 tableforest, targetns, true)));
2384 * Map SQL database to XML and/or XML Schema document; see SQL/XML:2003
2389 database_to_xml_internal(const char *xmlschema, bool nulls,
2390 bool tableforest, const char *targetns)
2397 xmlcn = map_sql_identifier_to_xml_name(get_database_name(MyDatabaseId),
2399 result = makeStringInfo();
2401 xmldata_root_element_start(result, xmlcn, xmlschema, targetns, true);
2404 appendStringInfo(result, "%s\n\n", xmlschema);
2408 nspid_list = database_get_xml_visible_schemas();
2412 foreach(cell, nspid_list)
2414 Oid nspid = lfirst_oid(cell);
2417 subres = schema_to_xml_internal(nspid, NULL, nulls,
2418 tableforest, targetns, false);
2420 appendStringInfoString(result, subres->data);
2421 appendStringInfoChar(result, '\n');
2427 xmldata_root_element_end(result, xmlcn);
2434 database_to_xml(PG_FUNCTION_ARGS)
2436 bool nulls = PG_GETARG_BOOL(0);
2437 bool tableforest = PG_GETARG_BOOL(1);
2438 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
2440 PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xml_internal(NULL, nulls,
2441 tableforest, targetns)));
2446 database_to_xmlschema_internal(bool nulls, bool tableforest,
2447 const char *targetns)
2455 result = makeStringInfo();
2457 xsd_schema_element_start(result, targetns);
2461 relid_list = database_get_xml_visible_tables();
2462 nspid_list = database_get_xml_visible_schemas();
2465 foreach(cell, relid_list)
2469 rel = heap_open(lfirst_oid(cell), AccessShareLock);
2470 tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2471 heap_close(rel, NoLock);
2474 appendStringInfoString(result,
2475 map_sql_typecoll_to_xmlschema_types(tupdesc_list));
2477 appendStringInfoString(result,
2478 map_sql_catalog_to_xmlschema_types(nspid_list, nulls, tableforest, targetns));
2480 xsd_schema_element_end(result);
2489 database_to_xmlschema(PG_FUNCTION_ARGS)
2491 bool nulls = PG_GETARG_BOOL(0);
2492 bool tableforest = PG_GETARG_BOOL(1);
2493 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
2495 PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xmlschema_internal(nulls,
2496 tableforest, targetns)));
2501 database_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2503 bool nulls = PG_GETARG_BOOL(0);
2504 bool tableforest = PG_GETARG_BOOL(1);
2505 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
2506 StringInfo xmlschema;
2508 xmlschema = database_to_xmlschema_internal(nulls, tableforest, targetns);
2510 PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xml_internal(xmlschema->data,
2511 nulls, tableforest, targetns)));
2516 * Map a multi-part SQL name to an XML name; see SQL/XML:2003 section
2520 map_multipart_sql_identifier_to_xml_name(char *a, char *b, char *c, char *d)
2522 StringInfoData result;
2524 initStringInfo(&result);
2527 appendStringInfo(&result, "%s",
2528 map_sql_identifier_to_xml_name(a, true, true));
2530 appendStringInfo(&result, ".%s",
2531 map_sql_identifier_to_xml_name(b, true, true));
2533 appendStringInfo(&result, ".%s",
2534 map_sql_identifier_to_xml_name(c, true, true));
2536 appendStringInfo(&result, ".%s",
2537 map_sql_identifier_to_xml_name(d, true, true));
2544 * Map an SQL table to an XML Schema document; see SQL/XML:2003
2547 * Map an SQL table to XML Schema data types; see SQL/XML:2003 section
2551 map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls,
2552 bool tableforest, const char *targetns)
2556 char *tabletypename;
2558 StringInfoData result;
2560 initStringInfo(&result);
2562 if (OidIsValid(relid))
2565 Form_pg_class reltuple;
2567 tuple = SearchSysCache(RELOID,
2568 ObjectIdGetDatum(relid),
2570 if (!HeapTupleIsValid(tuple))
2571 elog(ERROR, "cache lookup failed for relation %u", relid);
2572 reltuple = (Form_pg_class) GETSTRUCT(tuple);
2574 xmltn = map_sql_identifier_to_xml_name(NameStr(reltuple->relname),
2577 tabletypename = map_multipart_sql_identifier_to_xml_name("TableType",
2578 get_database_name(MyDatabaseId),
2579 get_namespace_name(reltuple->relnamespace),
2580 NameStr(reltuple->relname));
2582 rowtypename = map_multipart_sql_identifier_to_xml_name("RowType",
2583 get_database_name(MyDatabaseId),
2584 get_namespace_name(reltuple->relnamespace),
2585 NameStr(reltuple->relname));
2587 ReleaseSysCache(tuple);
2596 tabletypename = "TableType";
2597 rowtypename = "RowType";
2600 xsd_schema_element_start(&result, targetns);
2602 appendStringInfoString(&result,
2603 map_sql_typecoll_to_xmlschema_types(list_make1(tupdesc)));
2605 appendStringInfo(&result,
2606 "<xsd:complexType name=\"%s\">\n"
2607 " <xsd:sequence>\n",
2610 for (i = 0; i < tupdesc->natts; i++)
2611 appendStringInfo(&result,
2612 " <xsd:element name=\"%s\" type=\"%s\"%s></xsd:element>\n",
2613 map_sql_identifier_to_xml_name(NameStr(tupdesc->attrs[i]->attname),
2615 map_sql_type_to_xml_name(tupdesc->attrs[i]->atttypid, -1),
2616 nulls ? " nillable=\"true\"" : " minOccurs=\"0\"");
2618 appendStringInfoString(&result,
2619 " </xsd:sequence>\n"
2620 "</xsd:complexType>\n\n");
2624 appendStringInfo(&result,
2625 "<xsd:complexType name=\"%s\">\n"
2627 " <xsd:element name=\"row\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n"
2628 " </xsd:sequence>\n"
2629 "</xsd:complexType>\n\n",
2630 tabletypename, rowtypename);
2632 appendStringInfo(&result,
2633 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2634 xmltn, tabletypename);
2637 appendStringInfo(&result,
2638 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2639 xmltn, rowtypename);
2641 xsd_schema_element_end(&result);
2648 * Map an SQL schema to XML Schema data types; see SQL/XML section
2652 map_sql_schema_to_xmlschema_types(Oid nspid, List *relid_list, bool nulls,
2653 bool tableforest, const char *targetns)
2658 char *schematypename;
2659 StringInfoData result;
2662 dbname = get_database_name(MyDatabaseId);
2663 nspname = get_namespace_name(nspid);
2665 initStringInfo(&result);
2667 xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
2669 schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
2674 appendStringInfo(&result,
2675 "<xsd:complexType name=\"%s\">\n", schematypename);
2677 appendStringInfoString(&result,
2680 appendStringInfoString(&result,
2681 " <xsd:sequence>\n");
2683 foreach(cell, relid_list)
2685 Oid relid = lfirst_oid(cell);
2686 char *relname = get_rel_name(relid);
2687 char *xmltn = map_sql_identifier_to_xml_name(relname, true, false);
2688 char *tabletypename = map_multipart_sql_identifier_to_xml_name(tableforest ? "RowType" : "TableType",
2694 appendStringInfo(&result,
2695 " <xsd:element name=\"%s\" type=\"%s\"/>\n",
2696 xmltn, tabletypename);
2698 appendStringInfo(&result,
2699 " <xsd:element name=\"%s\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n",
2700 xmltn, tabletypename);
2704 appendStringInfoString(&result,
2707 appendStringInfoString(&result,
2708 " </xsd:sequence>\n");
2709 appendStringInfoString(&result,
2710 "</xsd:complexType>\n\n");
2712 appendStringInfo(&result,
2713 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2714 xmlsn, schematypename);
2721 * Map an SQL catalog to XML Schema data types; see SQL/XML section
2725 map_sql_catalog_to_xmlschema_types(List *nspid_list, bool nulls,
2726 bool tableforest, const char *targetns)
2730 char *catalogtypename;
2731 StringInfoData result;
2734 dbname = get_database_name(MyDatabaseId);
2736 initStringInfo(&result);
2738 xmlcn = map_sql_identifier_to_xml_name(dbname, true, false);
2740 catalogtypename = map_multipart_sql_identifier_to_xml_name("CatalogType",
2745 appendStringInfo(&result,
2746 "<xsd:complexType name=\"%s\">\n", catalogtypename);
2747 appendStringInfoString(&result,
2750 foreach(cell, nspid_list)
2752 Oid nspid = lfirst_oid(cell);
2753 char *nspname = get_namespace_name(nspid);
2754 char *xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
2755 char *schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
2760 appendStringInfo(&result,
2761 " <xsd:element name=\"%s\" type=\"%s\"/>\n",
2762 xmlsn, schematypename);
2765 appendStringInfoString(&result,
2767 appendStringInfoString(&result,
2768 "</xsd:complexType>\n\n");
2770 appendStringInfo(&result,
2771 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2772 xmlcn, catalogtypename);
2779 * Map an SQL data type to an XML name; see SQL/XML:2003 section 9.9.
2782 map_sql_type_to_xml_name(Oid typeoid, int typmod)
2784 StringInfoData result;
2786 initStringInfo(&result);
2792 appendStringInfo(&result, "CHAR");
2794 appendStringInfo(&result, "CHAR_%d", typmod - VARHDRSZ);
2798 appendStringInfo(&result, "VARCHAR");
2800 appendStringInfo(&result, "VARCHAR_%d", typmod - VARHDRSZ);
2804 appendStringInfo(&result, "NUMERIC");
2806 appendStringInfo(&result, "NUMERIC_%d_%d",
2807 ((typmod - VARHDRSZ) >> 16) & 0xffff,
2808 (typmod - VARHDRSZ) & 0xffff);
2811 appendStringInfo(&result, "INTEGER");
2814 appendStringInfo(&result, "SMALLINT");
2817 appendStringInfo(&result, "BIGINT");
2820 appendStringInfo(&result, "REAL");
2823 appendStringInfo(&result, "DOUBLE");
2826 appendStringInfo(&result, "BOOLEAN");
2830 appendStringInfo(&result, "TIME");
2832 appendStringInfo(&result, "TIME_%d", typmod);
2836 appendStringInfo(&result, "TIME_WTZ");
2838 appendStringInfo(&result, "TIME_WTZ_%d", typmod);
2842 appendStringInfo(&result, "TIMESTAMP");
2844 appendStringInfo(&result, "TIMESTAMP_%d", typmod);
2846 case TIMESTAMPTZOID:
2848 appendStringInfo(&result, "TIMESTAMP_WTZ");
2850 appendStringInfo(&result, "TIMESTAMP_WTZ_%d", typmod);
2853 appendStringInfo(&result, "DATE");
2856 appendStringInfo(&result, "XML");
2861 Form_pg_type typtuple;
2863 tuple = SearchSysCache(TYPEOID,
2864 ObjectIdGetDatum(typeoid),
2866 if (!HeapTupleIsValid(tuple))
2867 elog(ERROR, "cache lookup failed for type %u", typeoid);
2868 typtuple = (Form_pg_type) GETSTRUCT(tuple);
2870 appendStringInfoString(&result,
2871 map_multipart_sql_identifier_to_xml_name((typtuple->typtype == TYPTYPE_DOMAIN) ? "Domain" : "UDT",
2872 get_database_name(MyDatabaseId),
2873 get_namespace_name(typtuple->typnamespace),
2874 NameStr(typtuple->typname)));
2876 ReleaseSysCache(tuple);
2885 * Map a collection of SQL data types to XML Schema data types; see
2886 * SQL/XML:2002 section 9.10.
2889 map_sql_typecoll_to_xmlschema_types(List *tupdesc_list)
2891 List *uniquetypes = NIL;
2893 StringInfoData result;
2896 /* extract all column types used in the set of TupleDescs */
2897 foreach(cell0, tupdesc_list)
2899 TupleDesc tupdesc = (TupleDesc) lfirst(cell0);
2901 for (i = 0; i < tupdesc->natts; i++)
2903 if (tupdesc->attrs[i]->attisdropped)
2905 uniquetypes = list_append_unique_oid(uniquetypes,
2906 tupdesc->attrs[i]->atttypid);
2910 /* add base types of domains */
2911 foreach(cell0, uniquetypes)
2913 Oid typid = lfirst_oid(cell0);
2914 Oid basetypid = getBaseType(typid);
2916 if (basetypid != typid)
2917 uniquetypes = list_append_unique_oid(uniquetypes, basetypid);
2920 /* Convert to textual form */
2921 initStringInfo(&result);
2923 foreach(cell0, uniquetypes)
2925 appendStringInfo(&result, "%s\n",
2926 map_sql_type_to_xmlschema_type(lfirst_oid(cell0),
2935 * Map an SQL data type to a named XML Schema data type; see SQL/XML
2936 * sections 9.11 and 9.15.
2938 * (The distinction between 9.11 and 9.15 is basically that 9.15 adds
2939 * a name attribute, which this function does. The name-less version
2940 * 9.11 doesn't appear to be required anywhere.)
2943 map_sql_type_to_xmlschema_type(Oid typeoid, int typmod)
2945 StringInfoData result;
2946 const char *typename = map_sql_type_to_xml_name(typeoid, typmod);
2948 initStringInfo(&result);
2950 if (typeoid == XMLOID)
2952 appendStringInfo(&result,
2953 "<xsd:complexType mixed=\"true\">\n"
2955 " <xsd:any name=\"element\" minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>\n"
2956 " </xsd:sequence>\n"
2957 "</xsd:complexType>\n");
2961 appendStringInfo(&result,
2962 "<xsd:simpleType name=\"%s\">\n", typename);
2969 appendStringInfo(&result,
2970 " <xsd:restriction base=\"xsd:string\">\n");
2972 appendStringInfo(&result,
2973 " <xsd:maxLength value=\"%d\"/>\n",
2975 appendStringInfo(&result,
2976 " </xsd:restriction>\n");
2980 appendStringInfo(&result,
2981 " <xsd:restriction base=\"xsd:%s\">\n"
2982 " </xsd:restriction>\n",
2983 xmlbinary == XMLBINARY_BASE64 ? "base64Binary" : "hexBinary");
2988 appendStringInfo(&result,
2989 " <xsd:restriction base=\"xsd:decimal\">\n"
2990 " <xsd:totalDigits value=\"%d\"/>\n"
2991 " <xsd:fractionDigits value=\"%d\"/>\n"
2992 " </xsd:restriction>\n",
2993 ((typmod - VARHDRSZ) >> 16) & 0xffff,
2994 (typmod - VARHDRSZ) & 0xffff);
2998 appendStringInfo(&result,
2999 " <xsd:restriction base=\"xsd:short\">\n"
3000 " <xsd:maxInclusive value=\"%d\"/>\n"
3001 " <xsd:minInclusive value=\"%d\"/>\n"
3002 " </xsd:restriction>\n",
3003 SHRT_MAX, SHRT_MIN);
3007 appendStringInfo(&result,
3008 " <xsd:restriction base=\"xsd:int\">\n"
3009 " <xsd:maxInclusive value=\"%d\"/>\n"
3010 " <xsd:minInclusive value=\"%d\"/>\n"
3011 " </xsd:restriction>\n",
3016 appendStringInfo(&result,
3017 " <xsd:restriction base=\"xsd:long\">\n"
3018 " <xsd:maxInclusive value=\"" INT64_FORMAT "\"/>\n"
3019 " <xsd:minInclusive value=\"" INT64_FORMAT "\"/>\n"
3020 " </xsd:restriction>\n",
3021 (((uint64) 1) << (sizeof(int64) * 8 - 1)) - 1,
3022 (((uint64) 1) << (sizeof(int64) * 8 - 1)));
3026 appendStringInfo(&result,
3027 " <xsd:restriction base=\"xsd:float\"></xsd:restriction>\n");
3031 appendStringInfo(&result,
3032 " <xsd:restriction base=\"xsd:double\"></xsd:restriction>\n");
3036 appendStringInfo(&result,
3037 " <xsd:restriction base=\"xsd:boolean\"></xsd:restriction>\n");
3043 const char *tz = (typeoid == TIMETZOID ? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
3046 appendStringInfo(&result,
3047 " <xsd:restriction base=\"xsd:time\">\n"
3048 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}(.\\p{Nd}+)?%s\"/>\n"
3049 " </xsd:restriction>\n", tz);
3050 else if (typmod == 0)
3051 appendStringInfo(&result,
3052 " <xsd:restriction base=\"xsd:time\">\n"
3053 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3054 " </xsd:restriction>\n", tz);
3056 appendStringInfo(&result,
3057 " <xsd:restriction base=\"xsd:time\">\n"
3058 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}.\\p{Nd}{%d}%s\"/>\n"
3059 " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
3064 case TIMESTAMPTZOID:
3066 const char *tz = (typeoid == TIMESTAMPTZOID ? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
3069 appendStringInfo(&result,
3070 " <xsd:restriction base=\"xsd:dateTime\">\n"
3071 " <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"
3072 " </xsd:restriction>\n", tz);
3073 else if (typmod == 0)
3074 appendStringInfo(&result,
3075 " <xsd:restriction base=\"xsd:dateTime\">\n"
3076 " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3077 " </xsd:restriction>\n", tz);
3079 appendStringInfo(&result,
3080 " <xsd:restriction base=\"xsd:dateTime\">\n"
3081 " <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"
3082 " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
3087 appendStringInfo(&result,
3088 " <xsd:restriction base=\"xsd:date\">\n"
3089 " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}\"/>\n"
3090 " </xsd:restriction>\n");
3094 if (get_typtype(typeoid) == TYPTYPE_DOMAIN)
3097 int32 base_typmod = -1;
3099 base_typeoid = getBaseTypeAndTypmod(typeoid, &base_typmod);
3101 appendStringInfo(&result,
3102 " <xsd:restriction base=\"%s\"/>\n",
3103 map_sql_type_to_xml_name(base_typeoid, base_typmod));
3107 appendStringInfo(&result,
3108 "</xsd:simpleType>\n");
3116 * Map an SQL row to an XML element, taking the row from the active
3117 * SPI cursor. See also SQL/XML:2003 section 9.12.
3120 SPI_sql_row_to_xmlelement(int rownum, StringInfo result, char *tablename,
3121 bool nulls, bool tableforest,
3122 const char *targetns, bool top_level)
3128 xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
3138 xmldata_root_element_start(result, xmltn, NULL, targetns, top_level);
3140 appendStringInfoString(result, "<row>\n");
3142 for (i = 1; i <= SPI_tuptable->tupdesc->natts; i++)
3148 colname = map_sql_identifier_to_xml_name(SPI_fname(SPI_tuptable->tupdesc, i),
3150 colval = SPI_getbinval(SPI_tuptable->vals[rownum],
3151 SPI_tuptable->tupdesc,
3157 appendStringInfo(result, " <%s xsi:nil=\"true\"/>\n", colname);
3160 appendStringInfo(result, " <%s>%s</%s>\n",
3162 map_sql_value_to_xml_value(colval,
3163 SPI_gettypeid(SPI_tuptable->tupdesc, i)),
3169 xmldata_root_element_end(result, xmltn);
3170 appendStringInfoChar(result, '\n');
3173 appendStringInfoString(result, "</row>\n\n");
3178 * XPath related functions
3183 * Convert XML node to text (dump subtree in case of element,
3184 * return value otherwise)
3187 xml_xmlnodetoxmltype(xmlNodePtr cur)
3194 if (cur->type == XML_ELEMENT_NODE)
3196 buf = xmlBufferCreate();
3197 xmlNodeDump(buf, NULL, cur, 0, 1);
3198 result = xmlBuffer_to_xmltype(buf);
3203 str = xmlXPathCastNodeToString(cur);
3204 len = strlen((char *) str);
3205 result = (xmltype *) palloc(len + VARHDRSZ);
3206 SET_VARSIZE(result, len + VARHDRSZ);
3207 memcpy(VARDATA(result), str, len);
3216 * Evaluate XPath expression and return array of XML values.
3218 * As we have no support of XQuery sequences yet, this function seems
3219 * to be the most useful one (array of XML functions plays a role of
3220 * some kind of substitution for XQuery sequences).
3222 * Workaround here: we parse XML data in different way to allow XPath for
3223 * fragments (see "XPath for fragment" TODO comment inside).
3226 xpath(PG_FUNCTION_ARGS)
3229 text *xpath_expr_text = PG_GETARG_TEXT_P(0);
3230 xmltype *data = PG_GETARG_XML_P(1);
3231 ArrayType *namespaces = PG_GETARG_ARRAYTYPE_P(2);
3232 ArrayBuildState *astate = NULL;
3233 xmlParserCtxtPtr ctxt;
3235 xmlXPathContextPtr xpathctx;
3236 xmlXPathCompExprPtr xpathcomp;
3237 xmlXPathObjectPtr xpathobj;
3242 xmlChar *xpath_expr;
3246 Datum *ns_names_uris;
3247 bool *ns_names_uris_nulls;
3251 * Namespace mappings are passed as text[]. If an empty array is passed
3252 * (ndim = 0, "0-dimensional"), then there are no namespace mappings.
3253 * Else, a 2-dimensional array with length of the second axis being equal
3254 * to 2 should be passed, i.e., every subarray contains 2 elements, the
3255 * first element defining the name, the second one the URI. Example:
3256 * ARRAY[ARRAY['myns', 'http://example.com'], ARRAY['myns2',
3257 * 'http://example2.com']].
3259 ndim = ARR_NDIM(namespaces);
3264 dims = ARR_DIMS(namespaces);
3266 if (ndim != 2 || dims[1] != 2)
3268 (errcode(ERRCODE_DATA_EXCEPTION),
3269 errmsg("invalid array for XML namespace mapping"),
3270 errdetail("The array must be two-dimensional with length of the second axis equal to 2.")));
3272 Assert(ARR_ELEMTYPE(namespaces) == TEXTOID);
3274 deconstruct_array(namespaces, TEXTOID, -1, false, 'i',
3275 &ns_names_uris, &ns_names_uris_nulls,
3278 Assert((ns_count % 2) == 0); /* checked above */
3279 ns_count /= 2; /* count pairs only */
3283 ns_names_uris = NULL;
3284 ns_names_uris_nulls = NULL;
3288 datastr = VARDATA(data);
3289 len = VARSIZE(data) - VARHDRSZ;
3290 xpath_len = VARSIZE(xpath_expr_text) - VARHDRSZ;
3293 (errcode(ERRCODE_DATA_EXCEPTION),
3294 errmsg("empty XPath expression")));
3299 * To handle both documents and fragments, regardless of the fact whether
3300 * the XML datum has a single root (XML well-formedness), we wrap the XML
3301 * datum in a dummy element (<x>...</x>) and extend the XPath expression
3302 * accordingly. To do it, throw away the XML prolog, if any.
3305 xmlStrncmp((xmlChar *) datastr, (xmlChar *) "<?xml", 5) == 0)
3309 !(datastr[i - 1] == '?' && datastr[i] == '>'))
3313 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3314 "could not parse XML data");
3322 string = (xmlChar *) palloc((len + 8) * sizeof(xmlChar));
3323 memcpy(string, "<x>", 3);
3324 memcpy(string + 3, datastr, len);
3325 memcpy(string + 3 + len, "</x>", 5);
3328 xpath_expr = (xmlChar *) palloc((xpath_len + 3) * sizeof(xmlChar));
3329 memcpy(xpath_expr, "/x", 2);
3330 memcpy(xpath_expr + 2, VARDATA(xpath_expr_text), xpath_len);
3331 xpath_expr[xpath_len + 2] = '\0';
3337 * redundant XML parsing (two parsings for the same value during one
3338 * command execution are possible)
3340 ctxt = xmlNewParserCtxt();
3342 xml_ereport(ERROR, ERRCODE_OUT_OF_MEMORY,
3343 "could not allocate parser context");
3344 doc = xmlCtxtReadMemory(ctxt, (char *) string, len, NULL, NULL, 0);
3346 xml_ereport(ERROR, ERRCODE_INVALID_XML_DOCUMENT,
3347 "could not parse XML data");
3348 xpathctx = xmlXPathNewContext(doc);
3349 if (xpathctx == NULL)
3350 xml_ereport(ERROR, ERRCODE_OUT_OF_MEMORY,
3351 "could not allocate XPath context");
3352 xpathctx->node = xmlDocGetRootElement(doc);
3353 if (xpathctx->node == NULL)
3354 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3355 "could not find root XML element");
3357 /* register namespaces, if any */
3360 for (i = 0; i < ns_count; i++)
3365 if (ns_names_uris_nulls[i * 2] ||
3366 ns_names_uris_nulls[i * 2 + 1])
3368 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3369 errmsg("neither namespace name nor URI may be null")));
3370 ns_name = TextDatumGetCString(ns_names_uris[i * 2]);
3371 ns_uri = TextDatumGetCString(ns_names_uris[i * 2 + 1]);
3372 if (xmlXPathRegisterNs(xpathctx,
3373 (xmlChar *) ns_name,
3374 (xmlChar *) ns_uri) != 0)
3375 ereport(ERROR, /* is this an internal error??? */
3376 (errmsg("could not register XML namespace with name \"%s\" and URI \"%s\"",
3381 xpathcomp = xmlXPathCompile(xpath_expr);
3382 if (xpathcomp == NULL) /* TODO: show proper XPath error details */
3383 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3384 "invalid XPath expression");
3386 xpathobj = xmlXPathCompiledEval(xpathcomp, xpathctx);
3387 if (xpathobj == NULL) /* TODO: reason? */
3389 (errmsg("could not create XPath object")));
3391 xmlXPathFreeCompExpr(xpathcomp);
3393 /* return empty array in cases when nothing is found */
3394 if (xpathobj->nodesetval == NULL)
3397 res_nitems = xpathobj->nodesetval->nodeNr;
3401 for (i = 0; i < xpathobj->nodesetval->nodeNr; i++)
3404 bool elemisnull = false;
3406 elem = PointerGetDatum(xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[i]));
3407 astate = accumArrayResult(astate, elem,
3409 CurrentMemoryContext);
3413 xmlXPathFreeObject(xpathobj);
3414 xmlXPathFreeContext(xpathctx);
3416 xmlFreeParserCtxt(ctxt);
3418 if (res_nitems == 0)
3419 PG_RETURN_ARRAYTYPE_P(construct_empty_array(XMLOID));
3421 PG_RETURN_ARRAYTYPE_P(makeArrayResult(astate, CurrentMemoryContext));