1 /*-------------------------------------------------------------------------
4 * XML data type support.
7 * Portions Copyright (c) 1996-2007, 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.59 2007/11/20 23:14:41 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 * Note on memory management: Via callbacks, libxml is told to use
28 * palloc and friends for memory management. Sometimes, libxml
29 * allocates global structures in the hope that it can reuse them
30 * later on, but if "later" is much later, the memory context
31 * management of PostgreSQL will have blown those structures away
32 * without telling libxml about it. Therefore, it is important to
33 * call xmlCleanupParser() or perhaps some other cleanup function
34 * after using such functions, for example something from
35 * libxml/parser.h or libxml/xmlsave.h. Unfortunately, you cannot
36 * readily tell from the API documentation when that happens, so
37 * careful evaluation is necessary when introducing new libxml APIs
44 #include <libxml/chvalid.h>
45 #include <libxml/parser.h>
46 #include <libxml/tree.h>
47 #include <libxml/uri.h>
48 #include <libxml/xmlerror.h>
49 #include <libxml/xmlwriter.h>
50 #include <libxml/xpath.h>
51 #include <libxml/xpathInternals.h>
52 #endif /* USE_LIBXML */
54 #include "catalog/namespace.h"
55 #include "catalog/pg_type.h"
56 #include "commands/dbcommands.h"
57 #include "executor/executor.h"
58 #include "executor/spi.h"
60 #include "lib/stringinfo.h"
61 #include "libpq/pqformat.h"
62 #include "mb/pg_wchar.h"
63 #include "miscadmin.h"
64 #include "nodes/execnodes.h"
65 #include "parser/parse_expr.h"
66 #include "utils/array.h"
67 #include "utils/builtins.h"
68 #include "utils/date.h"
69 #include "utils/datetime.h"
70 #include "utils/lsyscache.h"
71 #include "utils/memutils.h"
72 #include "access/tupmacs.h"
73 #include "utils/xml.h"
77 XmlBinaryType xmlbinary;
78 XmlOptionType xmloption;
82 static StringInfo xml_err_buf = NULL;
84 static void xml_init(void);
85 static void *xml_palloc(size_t size);
86 static void *xml_repalloc(void *ptr, size_t size);
87 static void xml_pfree(void *ptr);
88 static char *xml_pstrdup(const char *string);
89 static void xml_ereport(int level, int sqlcode, const char *msg);
90 static void xml_errorHandler(void *ctxt, const char *msg,...);
91 static void xml_ereport_by_code(int level, int sqlcode,
92 const char *msg, int errcode);
93 static xmlChar *xml_text2xmlChar(text *in);
94 static int parse_xml_decl(const xmlChar * str, size_t *lenp,
95 xmlChar ** version, xmlChar ** encoding, int *standalone);
96 static bool print_xml_decl(StringInfo buf, const xmlChar * version,
97 pg_enc encoding, int standalone);
98 static xmlDocPtr xml_parse(text *data, XmlOptionType xmloption_arg,
99 bool preserve_whitespace, xmlChar * encoding);
100 static text *xml_xmlnodetoxmltype(xmlNodePtr cur);
101 #endif /* USE_LIBXML */
103 static StringInfo query_to_xml_internal(const char *query, char *tablename,
104 const char *xmlschema, bool nulls, bool tableforest,
105 const char *targetns, bool top_level);
106 static const char *map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid,
107 bool nulls, bool tableforest, const char *targetns);
108 static const char *map_sql_schema_to_xmlschema_types(Oid nspid,
109 List *relid_list, bool nulls,
110 bool tableforest, const char *targetns);
111 static const char *map_sql_catalog_to_xmlschema_types(List *nspid_list,
112 bool nulls, bool tableforest,
113 const char *targetns);
114 static const char *map_sql_type_to_xml_name(Oid typeoid, int typmod);
115 static const char *map_sql_typecoll_to_xmlschema_types(List *tupdesc_list);
116 static const char *map_sql_type_to_xmlschema_type(Oid typeoid, int typmod);
117 static void SPI_sql_row_to_xmlelement(int rownum, StringInfo result,
118 char *tablename, bool nulls, bool tableforest,
119 const char *targetns, bool top_level);
121 #define NO_XML_SUPPORT() \
123 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
124 errmsg("unsupported XML feature"), \
125 errdetail("This functionality requires the server to be built with libxml support."), \
126 errhint("You need to rebuild PostgreSQL using --with-libxml.")))
129 #define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str))
130 #define _textout(x) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(x)))
133 /* from SQL/XML:2003 section 4.7 */
134 #define NAMESPACE_XSD "http://www.w3.org/2001/XMLSchema"
135 #define NAMESPACE_XSI "http://www.w3.org/2001/XMLSchema-instance"
136 #define NAMESPACE_SQLXML "http://standards.iso.org/iso/9075/2003/sqlxml"
142 xmlChar_to_encoding(xmlChar * encoding_name)
144 int encoding = pg_char_to_encoding((char *) encoding_name);
148 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
149 errmsg("invalid encoding name \"%s\"",
150 (char *) encoding_name)));
157 xml_in(PG_FUNCTION_ARGS)
160 char *s = PG_GETARG_CSTRING(0);
166 vardata = palloc(len + VARHDRSZ);
167 SET_VARSIZE(vardata, len + VARHDRSZ);
168 memcpy(VARDATA(vardata), s, len);
171 * Parse the data to check if it is well-formed XML data. Assume that
172 * ERROR occurred if parsing failed.
174 doc = xml_parse(vardata, xmloption, true, NULL);
177 PG_RETURN_XML_P(vardata);
185 #define PG_XML_DEFAULT_VERSION "1.0"
189 xml_out_internal(xmltype *x, pg_enc target_encoding)
201 len = VARSIZE(x) - VARHDRSZ;
202 str = palloc(len + 1);
203 memcpy(str, VARDATA(x), len);
207 if ((res_code = parse_xml_decl((xmlChar *) str,
208 &len, &version, &encoding, &standalone)) == 0)
212 initStringInfo(&buf);
214 if (!print_xml_decl(&buf, version, target_encoding, standalone))
217 * If we are not going to produce an XML declaration, eat a single
218 * newline in the original string to prevent empty first lines in
221 if (*(str + len) == '\n')
224 appendStringInfoString(&buf, str + len);
229 xml_ereport_by_code(WARNING, ERRCODE_INTERNAL_ERROR,
230 "could not parse XML declaration in stored value",
238 xml_out(PG_FUNCTION_ARGS)
240 xmltype *x = PG_GETARG_XML_P(0);
243 * xml_out removes the encoding property in all cases. This is because we
244 * cannot control from here whether the datum will be converted to a
245 * different client encoding, so we'd do more harm than good by including
248 PG_RETURN_CSTRING(xml_out_internal(x, 0));
253 xml_recv(PG_FUNCTION_ARGS)
256 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
262 xmlChar *encoding = NULL;
265 * Read the data in raw format. We don't know yet what the encoding is, as
266 * that information is embedded in the xml declaration; so we have to
267 * parse that before converting to server encoding.
269 nbytes = buf->len - buf->cursor;
270 str = (char *) pq_getmsgbytes(buf, nbytes);
273 * We need a null-terminated string to pass to parse_xml_decl(). Rather
274 * than make a separate copy, make the temporary result one byte bigger
275 * than it needs to be.
277 result = palloc(nbytes + 1 + VARHDRSZ);
278 SET_VARSIZE(result, nbytes + VARHDRSZ);
279 memcpy(VARDATA(result), str, nbytes);
280 str = VARDATA(result);
283 parse_xml_decl((xmlChar *) str, NULL, NULL, &encoding, NULL);
286 * Parse the data to check if it is well-formed XML data. Assume that
287 * xml_parse will throw ERROR if not.
289 doc = xml_parse(result, xmloption, true, encoding);
292 /* Now that we know what we're dealing with, convert to server encoding */
293 newstr = (char *) pg_do_encoding_conversion((unsigned char *) str,
296 xmlChar_to_encoding(encoding) :
298 GetDatabaseEncoding());
304 nbytes = strlen(newstr);
306 result = palloc(nbytes + VARHDRSZ);
307 SET_VARSIZE(result, nbytes + VARHDRSZ);
308 memcpy(VARDATA(result), newstr, nbytes);
313 PG_RETURN_XML_P(result);
322 xml_send(PG_FUNCTION_ARGS)
324 xmltype *x = PG_GETARG_XML_P(0);
329 * xml_out_internal doesn't convert the encoding, it just prints the right
330 * declaration. pq_sendtext will do the conversion.
332 outval = xml_out_internal(x, pg_get_client_encoding());
334 pq_begintypsend(&buf);
335 pq_sendtext(&buf, outval, strlen(outval));
337 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
343 appendStringInfoText(StringInfo str, const text *t)
345 appendBinaryStringInfo(str, VARDATA(t), VARSIZE(t) - VARHDRSZ);
351 stringinfo_to_xmltype(StringInfo buf)
356 len = buf->len + VARHDRSZ;
357 result = palloc(len);
358 SET_VARSIZE(result, len);
359 memcpy(VARDATA(result), buf->data, buf->len);
366 cstring_to_xmltype(const char *string)
371 len = strlen(string) + VARHDRSZ;
372 result = palloc(len);
373 SET_VARSIZE(result, len);
374 memcpy(VARDATA(result), string, len - VARHDRSZ);
382 xmlBuffer_to_xmltype(xmlBufferPtr buf)
387 len = xmlBufferLength(buf) + VARHDRSZ;
388 result = palloc(len);
389 SET_VARSIZE(result, len);
390 memcpy(VARDATA(result), xmlBufferContent(buf), len - VARHDRSZ);
398 xmlcomment(PG_FUNCTION_ARGS)
401 text *arg = PG_GETARG_TEXT_P(0);
402 char *argdata = VARDATA(arg);
403 int len = VARSIZE(arg) - VARHDRSZ;
407 /* check for "--" in string or "-" at the end */
408 for (i = 1; i < len; i++)
410 if (argdata[i] == '-' && argdata[i - 1] == '-')
412 (errcode(ERRCODE_INVALID_XML_COMMENT),
413 errmsg("invalid XML comment")));
415 if (len > 0 && argdata[len - 1] == '-')
417 (errcode(ERRCODE_INVALID_XML_COMMENT),
418 errmsg("invalid XML comment")));
420 initStringInfo(&buf);
421 appendStringInfo(&buf, "<!--");
422 appendStringInfoText(&buf, arg);
423 appendStringInfo(&buf, "-->");
425 PG_RETURN_XML_P(stringinfo_to_xmltype(&buf));
435 * TODO: xmlconcat needs to merge the notations and unparsed entities
436 * of the argument values. Not very important in practice, though.
439 xmlconcat(List *args)
442 int global_standalone = 1;
443 xmlChar *global_version = NULL;
444 bool global_version_no_value = false;
448 initStringInfo(&buf);
451 xmltype *x = DatumGetXmlP(PointerGetDatum(lfirst(v)));
457 len = VARSIZE(x) - VARHDRSZ;
458 str = palloc(len + 1);
459 memcpy(str, VARDATA(x), len);
462 parse_xml_decl((xmlChar *) str, &len, &version, NULL, &standalone);
464 if (standalone == 0 && global_standalone == 1)
465 global_standalone = 0;
467 global_standalone = -1;
470 global_version_no_value = true;
471 else if (!global_version)
472 global_version = xmlStrdup(version);
473 else if (xmlStrcmp(version, global_version) != 0)
474 global_version_no_value = true;
476 appendStringInfoString(&buf, str + len);
480 if (!global_version_no_value || global_standalone >= 0)
484 initStringInfo(&buf2);
486 print_xml_decl(&buf2,
487 (!global_version_no_value) ? global_version : NULL,
491 appendStringInfoString(&buf2, buf.data);
495 return stringinfo_to_xmltype(&buf);
507 xmlconcat2(PG_FUNCTION_ARGS)
514 PG_RETURN_XML_P(PG_GETARG_XML_P(1));
516 else if (PG_ARGISNULL(1))
517 PG_RETURN_XML_P(PG_GETARG_XML_P(0));
519 PG_RETURN_XML_P(xmlconcat(list_make2(PG_GETARG_XML_P(0),
520 PG_GETARG_XML_P(1))));
525 texttoxml(PG_FUNCTION_ARGS)
527 text *data = PG_GETARG_TEXT_P(0);
529 PG_RETURN_XML_P(xmlparse(data, xmloption, true));
534 xmltotext(PG_FUNCTION_ARGS)
536 xmltype *data = PG_GETARG_XML_P(0);
538 PG_RETURN_TEXT_P(xmltotext_with_xmloption(data, xmloption));
543 xmltotext_with_xmloption(xmltype *data, XmlOptionType xmloption_arg)
545 if (xmloption_arg == XMLOPTION_DOCUMENT && !xml_is_document(data))
547 (errcode(ERRCODE_NOT_AN_XML_DOCUMENT),
548 errmsg("not an XML document")));
550 /* It's actually binary compatible, save for the above check. */
551 return (text *) data;
556 xmlelement(XmlExprState *xmlExpr, ExprContext *econtext)
559 XmlExpr *xexpr = (XmlExpr *) xmlExpr->xprstate.expr;
561 List *named_arg_strings;
567 xmlTextWriterPtr writer;
570 * We first evaluate all the arguments, then start up libxml and create
571 * the result. This avoids issues if one of the arguments involves a call
572 * to some other function or subsystem that wants to use libxml on its own
575 named_arg_strings = NIL;
577 foreach(arg, xmlExpr->named_args)
579 ExprState *e = (ExprState *) lfirst(arg);
584 value = ExecEvalExpr(e, econtext, &isnull, NULL);
588 str = OutputFunctionCall(&xmlExpr->named_outfuncs[i], value);
589 named_arg_strings = lappend(named_arg_strings, str);
594 foreach(arg, xmlExpr->args)
596 ExprState *e = (ExprState *) lfirst(arg);
601 value = ExecEvalExpr(e, econtext, &isnull, NULL);
602 /* here we can just forget NULL elements immediately */
605 str = map_sql_value_to_xml_value(value,
606 exprType((Node *) e->expr));
607 arg_strings = lappend(arg_strings, str);
611 /* now safe to run libxml */
614 buf = xmlBufferCreate();
615 writer = xmlNewTextWriterMemory(buf, 0);
617 xmlTextWriterStartElement(writer, (xmlChar *) xexpr->name);
619 forboth(arg, named_arg_strings, narg, xexpr->arg_names)
621 char *str = (char *) lfirst(arg);
622 char *argname = strVal(lfirst(narg));
626 xmlTextWriterWriteAttribute(writer,
633 foreach(arg, arg_strings)
635 char *str = (char *) lfirst(arg);
637 xmlTextWriterWriteRaw(writer, (xmlChar *) str);
640 xmlTextWriterEndElement(writer);
641 xmlFreeTextWriter(writer);
643 result = xmlBuffer_to_xmltype(buf);
655 xmlparse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace)
660 doc = xml_parse(data, xmloption_arg, preserve_whitespace, NULL);
663 return (xmltype *) data;
672 xmlpi(char *target, text *arg, bool arg_is_null, bool *result_is_null)
678 if (pg_strcasecmp(target, "xml") == 0)
680 (errcode(ERRCODE_SYNTAX_ERROR), /* really */
681 errmsg("invalid XML processing instruction"),
682 errdetail("XML processing instruction target name cannot be \"%s\".", target)));
685 * Following the SQL standard, the null check comes after the syntax check
688 *result_is_null = arg_is_null;
692 initStringInfo(&buf);
694 appendStringInfo(&buf, "<?%s", target);
700 string = _textout(arg);
701 if (strstr(string, "?>") != NULL)
703 (errcode(ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION),
704 errmsg("invalid XML processing instruction"),
705 errdetail("XML processing instruction cannot contain \"?>\".")));
707 appendStringInfoChar(&buf, ' ');
708 appendStringInfoString(&buf, string + strspn(string, " "));
711 appendStringInfoString(&buf, "?>");
713 result = stringinfo_to_xmltype(&buf);
724 xmlroot(xmltype *data, text *version, int standalone)
729 xmlChar *orig_version;
733 len = VARSIZE(data) - VARHDRSZ;
734 str = palloc(len + 1);
735 memcpy(str, VARDATA(data), len);
738 parse_xml_decl((xmlChar *) str, &len, &orig_version, NULL, &orig_standalone);
741 orig_version = xml_text2xmlChar(version);
747 case XML_STANDALONE_YES:
750 case XML_STANDALONE_NO:
753 case XML_STANDALONE_NO_VALUE:
754 orig_standalone = -1;
756 case XML_STANDALONE_OMITTED:
757 /* leave original value */
761 initStringInfo(&buf);
762 print_xml_decl(&buf, orig_version, 0, orig_standalone);
763 appendStringInfoString(&buf, str + len);
765 return stringinfo_to_xmltype(&buf);
774 * Validate document (given as string) against DTD (given as external link)
775 * TODO !!! use text instead of cstring for second arg
776 * TODO allow passing DTD as a string value (not only as an URI)
777 * TODO redesign (see comment with '!!!' below)
780 xmlvalidate(PG_FUNCTION_ARGS)
783 text *data = PG_GETARG_TEXT_P(0);
784 text *dtdOrUri = PG_GETARG_TEXT_P(1);
786 xmlParserCtxtPtr ctxt = NULL;
787 xmlDocPtr doc = NULL;
788 xmlDtdPtr dtd = NULL;
792 /* We use a PG_TRY block to ensure libxml parser is cleaned up on error */
796 ctxt = xmlNewParserCtxt();
798 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
799 "could not allocate parser context");
801 doc = xmlCtxtReadMemory(ctxt, (char *) VARDATA(data),
802 VARSIZE(data) - VARHDRSZ,
805 xml_ereport(ERROR, ERRCODE_INVALID_XML_DOCUMENT,
806 "could not parse XML data");
809 uri = xmlCreateURI();
810 elog(NOTICE, "dtd - %s", dtdOrUri);
811 dtd = palloc(sizeof(xmlDtdPtr));
812 uri = xmlParseURI(dtdOrUri);
814 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
815 "not implemented yet... (TODO)");
818 dtd = xmlParseDTD(NULL, xml_text2xmlChar(dtdOrUri));
821 xml_ereport(ERROR, ERRCODE_INVALID_XML_DOCUMENT,
822 "could not load DTD");
824 if (xmlValidateDtd(xmlNewValidCtxt(), doc, dtd) == 1)
828 xml_ereport(NOTICE, ERRCODE_INVALID_XML_DOCUMENT,
829 "validation against DTD failed");
843 xmlFreeParserCtxt(ctxt);
858 xmlFreeParserCtxt(ctxt);
865 PG_RETURN_BOOL(result);
866 #else /* not USE_LIBXML */
869 #endif /* not USE_LIBXML */
874 xml_is_document(xmltype *arg)
878 xmlDocPtr doc = NULL;
879 MemoryContext ccxt = CurrentMemoryContext;
883 doc = xml_parse((text *) arg, XMLOPTION_DOCUMENT, true, NULL);
891 ecxt = MemoryContextSwitchTo(ccxt);
892 errdata = CopyErrorData();
893 if (errdata->sqlerrcode == ERRCODE_INVALID_XML_DOCUMENT)
900 MemoryContextSwitchTo(ecxt);
910 #else /* not USE_LIBXML */
913 #endif /* not USE_LIBXML */
920 * Set up for use of libxml --- this should be called by each function that
921 * is about to use libxml facilities.
923 * TODO: xmlChar is utf8-char, make proper tuning (initdb with enc!=utf8 and
929 static bool first_time = true;
933 /* Stuff we need do only once per session */
934 MemoryContext oldcontext;
937 * Currently, we have no pure UTF-8 support for internals -- check if
940 if (sizeof(char) != sizeof(xmlChar))
942 (errmsg("could not initialize XML library"),
943 errdetail("libxml2 has incompatible char type: sizeof(char)=%u, sizeof(xmlChar)=%u.",
944 (int) sizeof(char), (int) sizeof(xmlChar))));
946 /* create error buffer in permanent context */
947 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
948 xml_err_buf = makeStringInfo();
949 MemoryContextSwitchTo(oldcontext);
951 /* Now that xml_err_buf exists, safe to call xml_errorHandler */
952 xmlSetGenericErrorFunc(NULL, xml_errorHandler);
954 /* Set up memory allocation our way, too */
955 xmlMemSetup(xml_pfree, xml_palloc, xml_repalloc, xml_pstrdup);
957 /* Check library compatibility */
964 /* Reset pre-existing buffer to empty */
965 Assert(xml_err_buf != NULL);
966 resetStringInfo(xml_err_buf);
969 * We re-establish the callback functions every time. This makes it
970 * safe for other subsystems (PL/Perl, say) to also use libxml with
971 * their own callbacks ... so long as they likewise set up the
972 * callbacks on every use. It's cheap enough to not be worth worrying
975 xmlSetGenericErrorFunc(NULL, xml_errorHandler);
976 xmlMemSetup(xml_pfree, xml_palloc, xml_repalloc, xml_pstrdup);
982 * SQL/XML allows storing "XML documents" or "XML content". "XML
983 * documents" are specified by the XML specification and are parsed
984 * easily by libxml. "XML content" is specified by SQL/XML as the
985 * production "XMLDecl? content". But libxml can only parse the
986 * "content" part, so we have to parse the XML declaration ourselves
990 #define CHECK_XML_SPACE(p) \
992 if (!xmlIsBlank_ch(*(p))) \
993 return XML_ERR_SPACE_REQUIRED; \
996 #define SKIP_XML_SPACE(p) \
997 while (xmlIsBlank_ch(*(p))) (p)++
999 /* Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender */
1000 /* Beware of multiple evaluations of argument! */
1001 #define PG_XMLISNAMECHAR(c) \
1002 (xmlIsBaseChar_ch(c) || xmlIsIdeographicQ(c) \
1003 || xmlIsDigit_ch(c) \
1004 || c == '.' || c == '-' || c == '_' || c == ':' \
1005 || xmlIsCombiningQ(c) \
1006 || xmlIsExtender_ch(c))
1009 parse_xml_decl(const xmlChar * str, size_t *lenp,
1010 xmlChar ** version, xmlChar ** encoding, int *standalone)
1013 const xmlChar *save_p;
1029 if (xmlStrncmp(p, (xmlChar *) "<?xml", 5) != 0)
1032 /* if next char is name char, it's a PI like <?xml-stylesheet ...?> */
1033 utf8len = strlen((const char *) (p + 5));
1034 utf8char = xmlGetUTF8Char(p + 5, &utf8len);
1035 if (PG_XMLISNAMECHAR(utf8char))
1043 if (xmlStrncmp(p, (xmlChar *) "version", 7) != 0)
1044 return XML_ERR_VERSION_MISSING;
1048 return XML_ERR_VERSION_MISSING;
1052 if (*p == '\'' || *p == '"')
1056 q = xmlStrchr(p + 1, *p);
1058 return XML_ERR_VERSION_MISSING;
1061 *version = xmlStrndup(p + 1, q - p - 1);
1065 return XML_ERR_VERSION_MISSING;
1070 if (xmlStrncmp(p, (xmlChar *) "encoding", 8) == 0)
1072 CHECK_XML_SPACE(save_p);
1076 return XML_ERR_MISSING_ENCODING;
1080 if (*p == '\'' || *p == '"')
1084 q = xmlStrchr(p + 1, *p);
1086 return XML_ERR_MISSING_ENCODING;
1089 *encoding = xmlStrndup(p + 1, q - p - 1);
1093 return XML_ERR_MISSING_ENCODING;
1103 if (xmlStrncmp(p, (xmlChar *) "standalone", 10) == 0)
1105 CHECK_XML_SPACE(save_p);
1109 return XML_ERR_STANDALONE_VALUE;
1112 if (xmlStrncmp(p, (xmlChar *) "'yes'", 5) == 0 ||
1113 xmlStrncmp(p, (xmlChar *) "\"yes\"", 5) == 0)
1118 else if (xmlStrncmp(p, (xmlChar *) "'no'", 4) == 0 ||
1119 xmlStrncmp(p, (xmlChar *) "\"no\"", 4) == 0)
1125 return XML_ERR_STANDALONE_VALUE;
1133 if (xmlStrncmp(p, (xmlChar *) "?>", 2) != 0)
1134 return XML_ERR_XMLDECL_NOT_FINISHED;
1140 for (p = str; p < str + len; p++)
1142 return XML_ERR_INVALID_CHAR;
1152 * Write an XML declaration. On output, we adjust the XML declaration
1153 * as follows. (These rules are the moral equivalent of the clause
1154 * "Serialization of an XML value" in the SQL standard.)
1156 * We try to avoid generating an XML declaration if possible. This is
1157 * so that you don't get trivial things like xml '<foo/>' resulting in
1158 * '<?xml version="1.0"?><foo/>', which would surely be annoying. We
1159 * must provide a declaration if the standalone property is specified
1160 * or if we include an encoding declaration. If we have a
1161 * declaration, we must specify a version (XML requires this).
1162 * Otherwise we only make a declaration if the version is not "1.0",
1163 * which is the default version specified in SQL:2003.
1166 print_xml_decl(StringInfo buf, const xmlChar * version,
1167 pg_enc encoding, int standalone)
1171 if ((version && strcmp((char *) version, PG_XML_DEFAULT_VERSION) != 0)
1172 || (encoding && encoding != PG_UTF8)
1173 || standalone != -1)
1175 appendStringInfoString(buf, "<?xml");
1178 appendStringInfo(buf, " version=\"%s\"", version);
1180 appendStringInfo(buf, " version=\"%s\"", PG_XML_DEFAULT_VERSION);
1182 if (encoding && encoding != PG_UTF8)
1185 * XXX might be useful to convert this to IANA names (ISO-8859-1
1186 * instead of LATIN1 etc.); needs field experience
1188 appendStringInfo(buf, " encoding=\"%s\"",
1189 pg_encoding_to_char(encoding));
1192 if (standalone == 1)
1193 appendStringInfoString(buf, " standalone=\"yes\"");
1194 else if (standalone == 0)
1195 appendStringInfoString(buf, " standalone=\"no\"");
1196 appendStringInfoString(buf, "?>");
1206 * Convert a C string to XML internal representation
1208 * TODO maybe, libxml2's xmlreader is better? (do not construct DOM,
1209 * yet do not use SAX - see xml_reader.c)
1212 xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace,
1217 xmlChar *utf8string;
1218 xmlParserCtxtPtr ctxt = NULL;
1219 xmlDocPtr doc = NULL;
1221 len = VARSIZE(data) - VARHDRSZ; /* will be useful later */
1222 string = xml_text2xmlChar(data);
1224 utf8string = pg_do_encoding_conversion(string,
1227 xmlChar_to_encoding(encoding) :
1228 GetDatabaseEncoding(),
1233 /* We use a PG_TRY block to ensure libxml parser is cleaned up on error */
1237 ctxt = xmlNewParserCtxt();
1239 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
1240 "could not allocate parser context");
1242 if (xmloption_arg == XMLOPTION_DOCUMENT)
1245 * Note, that here we try to apply DTD defaults
1246 * (XML_PARSE_DTDATTR) according to SQL/XML:10.16.7.d: 'Default
1247 * valies defined by internal DTD are applied'. As for external
1248 * DTDs, we try to support them too, (see SQL/XML:10.16.7.e)
1250 doc = xmlCtxtReadDoc(ctxt, utf8string,
1253 XML_PARSE_NOENT | XML_PARSE_DTDATTR
1254 | (preserve_whitespace ? 0 : XML_PARSE_NOBLANKS));
1256 xml_ereport(ERROR, ERRCODE_INVALID_XML_DOCUMENT,
1257 "invalid XML document");
1263 xmlChar *version = NULL;
1264 int standalone = -1;
1266 doc = xmlNewDoc(NULL);
1268 res_code = parse_xml_decl(utf8string, &count, &version, NULL, &standalone);
1270 xml_ereport_by_code(ERROR, ERRCODE_INVALID_XML_CONTENT,
1271 "invalid XML content: invalid XML declaration", res_code);
1273 res_code = xmlParseBalancedChunkMemory(doc, NULL, NULL, 0, utf8string + count, NULL);
1275 xml_ereport(ERROR, ERRCODE_INVALID_XML_CONTENT,
1276 "invalid XML content");
1278 doc->version = xmlStrdup(version);
1279 doc->encoding = xmlStrdup((xmlChar *) "UTF-8");
1280 doc->standalone = standalone;
1284 xmlFreeParserCtxt(ctxt);
1293 xmlFreeParserCtxt(ctxt);
1305 * xmlChar<->text convertions
1308 xml_text2xmlChar(text *in)
1310 int32 len = VARSIZE(in) - VARHDRSZ;
1313 res = palloc(len + 1);
1314 memcpy(res, VARDATA(in), len);
1322 * Wrappers for memory management functions
1325 xml_palloc(size_t size)
1327 return palloc(size);
1332 xml_repalloc(void *ptr, size_t size)
1334 return repalloc(ptr, size);
1339 xml_pfree(void *ptr)
1346 xml_pstrdup(const char *string)
1348 return pstrdup(string);
1353 * Wrapper for "ereport" function for XML-related errors. The "msg"
1354 * is the SQL-level message; some can be adopted from the SQL/XML
1355 * standard. This function adds libxml's native error messages, if
1359 xml_ereport(int level, int sqlcode, const char *msg)
1363 if (xml_err_buf->len > 0)
1365 detail = pstrdup(xml_err_buf->data);
1366 resetStringInfo(xml_err_buf);
1371 /* libxml error messages end in '\n'; get rid of it */
1376 len = strlen(detail);
1377 if (len > 0 && detail[len - 1] == '\n')
1378 detail[len - 1] = '\0';
1383 errdetail("%s", detail)));
1389 errmsg("%s", msg)));
1395 * Error handler for libxml error messages
1398 xml_errorHandler(void *ctxt, const char *msg,...)
1400 /* Append the formatted text to xml_err_buf */
1406 /* Try to format the data. */
1407 va_start(args, msg);
1408 success = appendStringInfoVA(xml_err_buf, msg, args);
1414 /* Double the buffer size and try again. */
1415 enlargeStringInfo(xml_err_buf, xml_err_buf->maxlen);
1421 * Wrapper for "ereport" function for XML-related errors. The "msg"
1422 * is the SQL-level message; some can be adopted from the SQL/XML
1423 * standard. This function uses "code" to create a textual detail
1424 * message. At the moment, we only need to cover those codes that we
1425 * may raise in this file.
1428 xml_ereport_by_code(int level, int sqlcode,
1429 const char *msg, int code)
1435 case XML_ERR_INVALID_CHAR:
1436 det = "Invalid character value";
1438 case XML_ERR_SPACE_REQUIRED:
1439 det = "Space required";
1441 case XML_ERR_STANDALONE_VALUE:
1442 det = "standalone accepts only 'yes' or 'no'";
1444 case XML_ERR_VERSION_MISSING:
1445 det = "Malformed declaration expecting version";
1447 case XML_ERR_MISSING_ENCODING:
1448 det = "Missing encoding in text declaration";
1450 case XML_ERR_XMLDECL_NOT_FINISHED:
1451 det = "Parsing XML declaration: '?>' expected";
1454 det = "Unrecognized libxml error code: %d";
1461 errdetail(det, code)));
1466 * Convert one char in the current server encoding to a Unicode codepoint.
1469 sqlchar_to_unicode(char *s)
1472 pg_wchar ret[2]; /* need space for trailing zero */
1474 utf8string = (char *) pg_do_encoding_conversion((unsigned char *) s,
1476 GetDatabaseEncoding(),
1479 pg_encoding_mb2wchar_with_len(PG_UTF8, utf8string, ret, pg_mblen(s));
1486 is_valid_xml_namefirst(pg_wchar c)
1488 /* (Letter | '_' | ':') */
1489 return (xmlIsBaseCharQ(c) || xmlIsIdeographicQ(c)
1490 || c == '_' || c == ':');
1495 is_valid_xml_namechar(pg_wchar c)
1497 /* Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender */
1498 return (xmlIsBaseCharQ(c) || xmlIsIdeographicQ(c)
1500 || c == '.' || c == '-' || c == '_' || c == ':'
1501 || xmlIsCombiningQ(c)
1502 || xmlIsExtenderQ(c));
1504 #endif /* USE_LIBXML */
1508 * Map SQL identifier to XML name; see SQL/XML:2003 section 9.1.
1511 map_sql_identifier_to_xml_name(char *ident, bool fully_escaped,
1519 * SQL/XML doesn't make use of this case anywhere, so it's probably a
1522 Assert(fully_escaped || !escape_period);
1524 initStringInfo(&buf);
1526 for (p = ident; *p; p += pg_mblen(p))
1528 if (*p == ':' && (p == ident || fully_escaped))
1529 appendStringInfo(&buf, "_x003A_");
1530 else if (*p == '_' && *(p + 1) == 'x')
1531 appendStringInfo(&buf, "_x005F_");
1532 else if (fully_escaped && p == ident &&
1533 pg_strncasecmp(p, "xml", 3) == 0)
1536 appendStringInfo(&buf, "_x0078_");
1538 appendStringInfo(&buf, "_x0058_");
1540 else if (escape_period && *p == '.')
1541 appendStringInfo(&buf, "_x002E_");
1544 pg_wchar u = sqlchar_to_unicode(p);
1547 ? !is_valid_xml_namefirst(u)
1548 : !is_valid_xml_namechar(u))
1549 appendStringInfo(&buf, "_x%04X_", (unsigned int) u);
1551 appendBinaryStringInfo(&buf, p, pg_mblen(p));
1556 #else /* not USE_LIBXML */
1559 #endif /* not USE_LIBXML */
1564 * Map a Unicode codepoint into the current server encoding.
1567 unicode_to_sqlchar(pg_wchar c)
1569 static unsigned char utf8string[5]; /* need trailing zero */
1575 else if (c <= 0x7FF)
1577 utf8string[0] = 0xC0 | ((c >> 6) & 0x1F);
1578 utf8string[1] = 0x80 | (c & 0x3F);
1580 else if (c <= 0xFFFF)
1582 utf8string[0] = 0xE0 | ((c >> 12) & 0x0F);
1583 utf8string[1] = 0x80 | ((c >> 6) & 0x3F);
1584 utf8string[2] = 0x80 | (c & 0x3F);
1588 utf8string[0] = 0xF0 | ((c >> 18) & 0x07);
1589 utf8string[1] = 0x80 | ((c >> 12) & 0x3F);
1590 utf8string[2] = 0x80 | ((c >> 6) & 0x3F);
1591 utf8string[3] = 0x80 | (c & 0x3F);
1594 return (char *) pg_do_encoding_conversion(utf8string,
1595 pg_mblen((char *) utf8string),
1597 GetDatabaseEncoding());
1602 * Map XML name to SQL identifier; see SQL/XML:2003 section 9.17.
1605 map_xml_name_to_sql_identifier(char *name)
1610 initStringInfo(&buf);
1612 for (p = name; *p; p += pg_mblen(p))
1614 if (*p == '_' && *(p + 1) == 'x'
1615 && isxdigit((unsigned char) *(p + 2))
1616 && isxdigit((unsigned char) *(p + 3))
1617 && isxdigit((unsigned char) *(p + 4))
1618 && isxdigit((unsigned char) *(p + 5))
1623 sscanf(p + 2, "%X", &u);
1624 appendStringInfoString(&buf, unicode_to_sqlchar(u));
1628 appendBinaryStringInfo(&buf, p, pg_mblen(p));
1635 * Map SQL value to XML value; see SQL/XML:2003 section 9.16.
1638 map_sql_value_to_xml_value(Datum value, Oid type)
1642 initStringInfo(&buf);
1644 if (type_is_array(type))
1656 array = DatumGetArrayTypeP(value);
1657 elmtype = ARR_ELEMTYPE(array);
1658 get_typlenbyvalalign(elmtype, &elmlen, &elmbyval, &elmalign);
1660 deconstruct_array(array, elmtype,
1661 elmlen, elmbyval, elmalign,
1662 &elem_values, &elem_nulls,
1665 for (i = 0; i < num_elems; i++)
1669 appendStringInfoString(&buf, "<element>");
1670 appendStringInfoString(&buf,
1671 map_sql_value_to_xml_value(elem_values[i],
1673 appendStringInfoString(&buf, "</element>");
1687 * Special XSD formatting for some data types
1692 if (DatumGetBool(value))
1701 char buf[MAXDATELEN + 1];
1703 date = DatumGetDateADT(value);
1704 j2date(date + POSTGRES_EPOCH_JDATE,
1705 &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
1706 EncodeDateOnly(&tm, USE_XSD_DATES, buf);
1708 return pstrdup(buf);
1713 Timestamp timestamp;
1717 char buf[MAXDATELEN + 1];
1719 timestamp = DatumGetTimestamp(value);
1721 /* XSD doesn't support infinite values */
1722 if (TIMESTAMP_NOT_FINITE(timestamp))
1724 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1725 errmsg("timestamp out of range")));
1726 else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
1727 EncodeDateTime(&tm, fsec, NULL, &tzn, USE_XSD_DATES, buf);
1730 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1731 errmsg("timestamp out of range")));
1733 return pstrdup(buf);
1736 case TIMESTAMPTZOID:
1738 TimestampTz timestamp;
1743 char buf[MAXDATELEN + 1];
1745 timestamp = DatumGetTimestamp(value);
1747 /* XSD doesn't support infinite values */
1748 if (TIMESTAMP_NOT_FINITE(timestamp))
1750 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1751 errmsg("timestamp out of range")));
1752 else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
1753 EncodeDateTime(&tm, fsec, &tz, &tzn, USE_XSD_DATES, buf);
1756 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1757 errmsg("timestamp out of range")));
1759 return pstrdup(buf);
1763 getTypeOutputInfo(type, &typeOut, &isvarlena);
1764 str = OidOutputFunctionCall(typeOut, value);
1770 if (type == BYTEAOID)
1773 xmlTextWriterPtr writer;
1778 buf = xmlBufferCreate();
1779 writer = xmlNewTextWriterMemory(buf, 0);
1781 if (xmlbinary == XMLBINARY_BASE64)
1782 xmlTextWriterWriteBase64(writer, VARDATA(value), 0, VARSIZE(value) - VARHDRSZ);
1784 xmlTextWriterWriteBinHex(writer, VARDATA(value), 0, VARSIZE(value) - VARHDRSZ);
1786 xmlFreeTextWriter(writer);
1787 result = pstrdup((const char *) xmlBufferContent(buf));
1791 #endif /* USE_LIBXML */
1793 for (p = str; *p; p += pg_mblen(p))
1798 appendStringInfo(&buf, "&");
1801 appendStringInfo(&buf, "<");
1804 appendStringInfo(&buf, ">");
1807 appendStringInfo(&buf, "
");
1810 appendBinaryStringInfo(&buf, p, pg_mblen(p));
1821 _SPI_strdup(const char *s)
1823 char *ret = SPI_palloc(strlen(s) + 1);
1831 * SQL to XML mapping functions
1833 * What follows below is intentionally organized so that you can read
1834 * along in the SQL/XML:2003 standard. The functions are mostly split
1835 * up and ordered they way the clauses lay out in the standards
1836 * document, and the identifiers are also aligned with the standard
1837 * text. (SQL/XML:2006 appears to be ordered differently,
1840 * There are many things going on there:
1842 * There are two kinds of mappings: Mapping SQL data (table contents)
1843 * to XML documents, and mapping SQL structure (the "schema") to XML
1844 * Schema. And there are functions that do both at the same time.
1846 * Then you can map a database, a schema, or a table, each in both
1847 * ways. This breaks down recursively: Mapping a database invokes
1848 * mapping schemas, which invokes mapping tables, which invokes
1849 * mapping rows, which invokes mapping columns, although you can't
1850 * call the last two from the outside. Because of this, there are a
1851 * number of xyz_internal() functions which are to be called both from
1852 * the function manager wrapper and from some upper layer in a
1855 * See the documentation about what the common function arguments
1856 * nulls, tableforest, and targetns mean.
1858 * Some style guidelines for XML output: Use double quotes for quoting
1859 * XML attributes. Indent XML elements by two spaces, but remember
1860 * that a lot of code is called recursively at different levels, so
1861 * it's better not to indent rather than create output that indents
1862 * and outdents weirdly. Add newlines to make the output look nice.
1867 * Visibility of objects for XML mappings; see SQL/XML:2003 section
1872 * Given a query, which must return type oid as first column, produce
1873 * a list of Oids with the query results.
1876 query_to_oid_list(const char *query)
1881 SPI_execute(query, true, 0);
1883 for (i = 0; i < SPI_processed; i++)
1888 oid = SPI_getbinval(SPI_tuptable->vals[i],
1889 SPI_tuptable->tupdesc,
1893 list = lappend_oid(list, DatumGetObjectId(oid));
1901 schema_get_xml_visible_tables(Oid nspid)
1903 StringInfoData query;
1905 initStringInfo(&query);
1906 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);
1908 return query_to_oid_list(query.data);
1913 * Including the system schemas is probably not useful for a database
1916 #define XML_VISIBLE_SCHEMAS_EXCLUDE "(nspname ~ '^pg_' OR nspname = 'information_schema')"
1918 #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
1922 database_get_xml_visible_schemas(void)
1924 return query_to_oid_list(XML_VISIBLE_SCHEMAS " ORDER BY nspname;");
1929 database_get_xml_visible_tables(void)
1931 /* At the moment there is no order required here. */
1932 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 ");");
1937 * Map SQL table to XML and/or XML Schema document; see SQL/XML:2003
1942 table_to_xml_internal(Oid relid,
1943 const char *xmlschema, bool nulls, bool tableforest,
1944 const char *targetns, bool top_level)
1946 StringInfoData query;
1948 initStringInfo(&query);
1949 appendStringInfo(&query, "SELECT * FROM %s",
1950 DatumGetCString(DirectFunctionCall1(regclassout,
1951 ObjectIdGetDatum(relid))));
1952 return query_to_xml_internal(query.data, get_rel_name(relid),
1953 xmlschema, nulls, tableforest,
1954 targetns, top_level);
1959 table_to_xml(PG_FUNCTION_ARGS)
1961 Oid relid = PG_GETARG_OID(0);
1962 bool nulls = PG_GETARG_BOOL(1);
1963 bool tableforest = PG_GETARG_BOOL(2);
1964 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
1966 PG_RETURN_XML_P(stringinfo_to_xmltype(table_to_xml_internal(relid, NULL,
1973 query_to_xml(PG_FUNCTION_ARGS)
1975 char *query = _textout(PG_GETARG_TEXT_P(0));
1976 bool nulls = PG_GETARG_BOOL(1);
1977 bool tableforest = PG_GETARG_BOOL(2);
1978 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
1980 PG_RETURN_XML_P(stringinfo_to_xmltype(query_to_xml_internal(query, NULL,
1981 NULL, nulls, tableforest,
1987 cursor_to_xml(PG_FUNCTION_ARGS)
1989 char *name = _textout(PG_GETARG_TEXT_P(0));
1990 int32 count = PG_GETARG_INT32(1);
1991 bool nulls = PG_GETARG_BOOL(2);
1992 bool tableforest = PG_GETARG_BOOL(3);
1993 const char *targetns = _textout(PG_GETARG_TEXT_P(4));
1995 StringInfoData result;
1999 initStringInfo(&result);
2002 portal = SPI_cursor_find(name);
2005 (errcode(ERRCODE_UNDEFINED_CURSOR),
2006 errmsg("cursor \"%s\" does not exist", name)));
2008 SPI_cursor_fetch(portal, true, count);
2009 for (i = 0; i < SPI_processed; i++)
2010 SPI_sql_row_to_xmlelement(i, &result, NULL, nulls,
2011 tableforest, targetns, true);
2015 PG_RETURN_XML_P(stringinfo_to_xmltype(&result));
2020 * Write the start tag of the root element of a data mapping.
2022 * top_level means that this is the very top level of the eventual
2023 * output. For example, when the user calls table_to_xml, then a call
2024 * with a table name to this function is the top level. When the user
2025 * calls database_to_xml, then a call with a schema name to this
2026 * function is not the top level. If top_level is false, then the XML
2027 * namespace declarations are omitted, because they supposedly already
2028 * appeared earlier in the output. Repeating them is not wrong, but
2032 xmldata_root_element_start(StringInfo result, const char *eltname,
2033 const char *xmlschema, const char *targetns,
2036 /* This isn't really wrong but currently makes no sense. */
2037 Assert(top_level || !xmlschema);
2039 appendStringInfo(result, "<%s", eltname);
2042 appendStringInfoString(result, " xmlns:xsi=\"" NAMESPACE_XSI "\"");
2043 if (strlen(targetns) > 0)
2044 appendStringInfo(result, " xmlns=\"%s\"", targetns);
2048 /* FIXME: better targets */
2049 if (strlen(targetns) > 0)
2050 appendStringInfo(result, " xsi:schemaLocation=\"%s #\"", targetns);
2052 appendStringInfo(result, " xsi:noNamespaceSchemaLocation=\"#\"");
2054 appendStringInfo(result, ">\n\n");
2059 xmldata_root_element_end(StringInfo result, const char *eltname)
2061 appendStringInfo(result, "</%s>\n", eltname);
2066 query_to_xml_internal(const char *query, char *tablename,
2067 const char *xmlschema, bool nulls, bool tableforest,
2068 const char *targetns, bool top_level)
2075 xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
2079 result = makeStringInfo();
2082 if (SPI_execute(query, true, 0) != SPI_OK_SELECT)
2084 (errcode(ERRCODE_DATA_EXCEPTION),
2085 errmsg("invalid query")));
2088 xmldata_root_element_start(result, xmltn, xmlschema,
2089 targetns, top_level);
2092 appendStringInfo(result, "%s\n\n", xmlschema);
2094 for (i = 0; i < SPI_processed; i++)
2095 SPI_sql_row_to_xmlelement(i, result, tablename, nulls,
2096 tableforest, targetns, top_level);
2099 xmldata_root_element_end(result, xmltn);
2108 table_to_xmlschema(PG_FUNCTION_ARGS)
2110 Oid relid = PG_GETARG_OID(0);
2111 bool nulls = PG_GETARG_BOOL(1);
2112 bool tableforest = PG_GETARG_BOOL(2);
2113 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2117 rel = heap_open(relid, AccessShareLock);
2118 result = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2119 tableforest, targetns);
2120 heap_close(rel, NoLock);
2122 PG_RETURN_XML_P(cstring_to_xmltype(result));
2127 query_to_xmlschema(PG_FUNCTION_ARGS)
2129 char *query = _textout(PG_GETARG_TEXT_P(0));
2130 bool nulls = PG_GETARG_BOOL(1);
2131 bool tableforest = PG_GETARG_BOOL(2);
2132 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2138 plan = SPI_prepare(query, 0, NULL);
2139 portal = SPI_cursor_open(NULL, plan, NULL, NULL, true);
2140 result = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2142 tableforest, targetns));
2143 SPI_cursor_close(portal);
2146 PG_RETURN_XML_P(cstring_to_xmltype(result));
2151 cursor_to_xmlschema(PG_FUNCTION_ARGS)
2153 char *name = _textout(PG_GETARG_TEXT_P(0));
2154 bool nulls = PG_GETARG_BOOL(1);
2155 bool tableforest = PG_GETARG_BOOL(2);
2156 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2157 const char *xmlschema;
2161 portal = SPI_cursor_find(name);
2164 (errcode(ERRCODE_UNDEFINED_CURSOR),
2165 errmsg("cursor \"%s\" does not exist", name)));
2167 xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2169 tableforest, targetns));
2172 PG_RETURN_XML_P(cstring_to_xmltype(xmlschema));
2177 table_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2179 Oid relid = PG_GETARG_OID(0);
2180 bool nulls = PG_GETARG_BOOL(1);
2181 bool tableforest = PG_GETARG_BOOL(2);
2182 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2184 const char *xmlschema;
2186 rel = heap_open(relid, AccessShareLock);
2187 xmlschema = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2188 tableforest, targetns);
2189 heap_close(rel, NoLock);
2191 PG_RETURN_XML_P(stringinfo_to_xmltype(table_to_xml_internal(relid,
2192 xmlschema, nulls, tableforest,
2198 query_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2200 char *query = _textout(PG_GETARG_TEXT_P(0));
2201 bool nulls = PG_GETARG_BOOL(1);
2202 bool tableforest = PG_GETARG_BOOL(2);
2203 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2205 const char *xmlschema;
2210 plan = SPI_prepare(query, 0, NULL);
2211 portal = SPI_cursor_open(NULL, plan, NULL, NULL, true);
2212 xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2213 InvalidOid, nulls, tableforest, targetns));
2214 SPI_cursor_close(portal);
2217 PG_RETURN_XML_P(stringinfo_to_xmltype(query_to_xml_internal(query, NULL,
2218 xmlschema, nulls, tableforest,
2224 * Map SQL schema to XML and/or XML Schema document; see SQL/XML:2003
2229 schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls,
2230 bool tableforest, const char *targetns, bool top_level)
2237 xmlsn = map_sql_identifier_to_xml_name(get_namespace_name(nspid),
2239 result = makeStringInfo();
2241 xmldata_root_element_start(result, xmlsn, xmlschema, targetns, top_level);
2244 appendStringInfo(result, "%s\n\n", xmlschema);
2248 relid_list = schema_get_xml_visible_tables(nspid);
2252 foreach(cell, relid_list)
2254 Oid relid = lfirst_oid(cell);
2257 subres = table_to_xml_internal(relid, NULL, nulls, tableforest,
2260 appendStringInfoString(result, subres->data);
2261 appendStringInfoChar(result, '\n');
2267 xmldata_root_element_end(result, xmlsn);
2274 schema_to_xml(PG_FUNCTION_ARGS)
2276 Name name = PG_GETARG_NAME(0);
2277 bool nulls = PG_GETARG_BOOL(1);
2278 bool tableforest = PG_GETARG_BOOL(2);
2279 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2284 schemaname = NameStr(*name);
2285 nspid = LookupExplicitNamespace(schemaname);
2287 PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xml_internal(nspid, NULL,
2288 nulls, tableforest, targetns, true)));
2293 * Write the start element of the root element of an XML Schema mapping.
2296 xsd_schema_element_start(StringInfo result, const char *targetns)
2298 appendStringInfoString(result,
2300 " xmlns:xsd=\"" NAMESPACE_XSD "\"");
2301 if (strlen(targetns) > 0)
2302 appendStringInfo(result,
2304 " targetNamespace=\"%s\"\n"
2305 " elementFormDefault=\"qualified\"",
2307 appendStringInfoString(result,
2313 xsd_schema_element_end(StringInfo result)
2315 appendStringInfoString(result, "</xsd:schema>");
2320 schema_to_xmlschema_internal(const char *schemaname, bool nulls,
2321 bool tableforest, const char *targetns)
2329 result = makeStringInfo();
2331 nspid = LookupExplicitNamespace(schemaname);
2333 xsd_schema_element_start(result, targetns);
2337 relid_list = schema_get_xml_visible_tables(nspid);
2340 foreach(cell, relid_list)
2344 rel = heap_open(lfirst_oid(cell), AccessShareLock);
2345 tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2346 heap_close(rel, NoLock);
2349 appendStringInfoString(result,
2350 map_sql_typecoll_to_xmlschema_types(tupdesc_list));
2352 appendStringInfoString(result,
2353 map_sql_schema_to_xmlschema_types(nspid, relid_list,
2354 nulls, tableforest, targetns));
2356 xsd_schema_element_end(result);
2365 schema_to_xmlschema(PG_FUNCTION_ARGS)
2367 Name name = PG_GETARG_NAME(0);
2368 bool nulls = PG_GETARG_BOOL(1);
2369 bool tableforest = PG_GETARG_BOOL(2);
2370 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2372 PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xmlschema_internal(NameStr(*name),
2373 nulls, tableforest, targetns)));
2378 schema_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2380 Name name = PG_GETARG_NAME(0);
2381 bool nulls = PG_GETARG_BOOL(1);
2382 bool tableforest = PG_GETARG_BOOL(2);
2383 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2386 StringInfo xmlschema;
2388 schemaname = NameStr(*name);
2389 nspid = LookupExplicitNamespace(schemaname);
2391 xmlschema = schema_to_xmlschema_internal(schemaname, nulls,
2392 tableforest, targetns);
2394 PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xml_internal(nspid,
2395 xmlschema->data, nulls,
2396 tableforest, targetns, true)));
2401 * Map SQL database to XML and/or XML Schema document; see SQL/XML:2003
2406 database_to_xml_internal(const char *xmlschema, bool nulls,
2407 bool tableforest, const char *targetns)
2414 xmlcn = map_sql_identifier_to_xml_name(get_database_name(MyDatabaseId),
2416 result = makeStringInfo();
2418 xmldata_root_element_start(result, xmlcn, xmlschema, targetns, true);
2421 appendStringInfo(result, "%s\n\n", xmlschema);
2425 nspid_list = database_get_xml_visible_schemas();
2429 foreach(cell, nspid_list)
2431 Oid nspid = lfirst_oid(cell);
2434 subres = schema_to_xml_internal(nspid, NULL, nulls,
2435 tableforest, targetns, false);
2437 appendStringInfoString(result, subres->data);
2438 appendStringInfoChar(result, '\n');
2444 xmldata_root_element_end(result, xmlcn);
2451 database_to_xml(PG_FUNCTION_ARGS)
2453 bool nulls = PG_GETARG_BOOL(0);
2454 bool tableforest = PG_GETARG_BOOL(1);
2455 const char *targetns = _textout(PG_GETARG_TEXT_P(2));
2457 PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xml_internal(NULL, nulls,
2458 tableforest, targetns)));
2463 database_to_xmlschema_internal(bool nulls, bool tableforest,
2464 const char *targetns)
2472 result = makeStringInfo();
2474 xsd_schema_element_start(result, targetns);
2478 relid_list = database_get_xml_visible_tables();
2479 nspid_list = database_get_xml_visible_schemas();
2482 foreach(cell, relid_list)
2486 rel = heap_open(lfirst_oid(cell), AccessShareLock);
2487 tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2488 heap_close(rel, NoLock);
2491 appendStringInfoString(result,
2492 map_sql_typecoll_to_xmlschema_types(tupdesc_list));
2494 appendStringInfoString(result,
2495 map_sql_catalog_to_xmlschema_types(nspid_list, nulls, tableforest, targetns));
2497 xsd_schema_element_end(result);
2506 database_to_xmlschema(PG_FUNCTION_ARGS)
2508 bool nulls = PG_GETARG_BOOL(0);
2509 bool tableforest = PG_GETARG_BOOL(1);
2510 const char *targetns = _textout(PG_GETARG_TEXT_P(2));
2512 PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xmlschema_internal(nulls,
2513 tableforest, targetns)));
2518 database_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2520 bool nulls = PG_GETARG_BOOL(0);
2521 bool tableforest = PG_GETARG_BOOL(1);
2522 const char *targetns = _textout(PG_GETARG_TEXT_P(2));
2523 StringInfo xmlschema;
2525 xmlschema = database_to_xmlschema_internal(nulls, tableforest, targetns);
2527 PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xml_internal(xmlschema->data,
2528 nulls, tableforest, targetns)));
2533 * Map a multi-part SQL name to an XML name; see SQL/XML:2003 section
2537 map_multipart_sql_identifier_to_xml_name(char *a, char *b, char *c, char *d)
2539 StringInfoData result;
2541 initStringInfo(&result);
2544 appendStringInfo(&result, "%s",
2545 map_sql_identifier_to_xml_name(a, true, true));
2547 appendStringInfo(&result, ".%s",
2548 map_sql_identifier_to_xml_name(b, true, true));
2550 appendStringInfo(&result, ".%s",
2551 map_sql_identifier_to_xml_name(c, true, true));
2553 appendStringInfo(&result, ".%s",
2554 map_sql_identifier_to_xml_name(d, true, true));
2561 * Map an SQL table to an XML Schema document; see SQL/XML:2003
2564 * Map an SQL table to XML Schema data types; see SQL/XML:2003 section
2568 map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls,
2569 bool tableforest, const char *targetns)
2573 char *tabletypename;
2575 StringInfoData result;
2577 initStringInfo(&result);
2579 if (OidIsValid(relid))
2582 Form_pg_class reltuple;
2584 tuple = SearchSysCache(RELOID,
2585 ObjectIdGetDatum(relid),
2587 if (!HeapTupleIsValid(tuple))
2588 elog(ERROR, "cache lookup failed for relation %u", relid);
2589 reltuple = (Form_pg_class) GETSTRUCT(tuple);
2591 xmltn = map_sql_identifier_to_xml_name(NameStr(reltuple->relname),
2594 tabletypename = map_multipart_sql_identifier_to_xml_name("TableType",
2595 get_database_name(MyDatabaseId),
2596 get_namespace_name(reltuple->relnamespace),
2597 NameStr(reltuple->relname));
2599 rowtypename = map_multipart_sql_identifier_to_xml_name("RowType",
2600 get_database_name(MyDatabaseId),
2601 get_namespace_name(reltuple->relnamespace),
2602 NameStr(reltuple->relname));
2604 ReleaseSysCache(tuple);
2613 tabletypename = "TableType";
2614 rowtypename = "RowType";
2617 xsd_schema_element_start(&result, targetns);
2619 appendStringInfoString(&result,
2620 map_sql_typecoll_to_xmlschema_types(list_make1(tupdesc)));
2622 appendStringInfo(&result,
2623 "<xsd:complexType name=\"%s\">\n"
2624 " <xsd:sequence>\n",
2627 for (i = 0; i < tupdesc->natts; i++)
2628 appendStringInfo(&result,
2629 " <xsd:element name=\"%s\" type=\"%s\"%s></xsd:element>\n",
2630 map_sql_identifier_to_xml_name(NameStr(tupdesc->attrs[i]->attname),
2632 map_sql_type_to_xml_name(tupdesc->attrs[i]->atttypid, -1),
2633 nulls ? " nillable=\"true\"" : " minOccurs=\"0\"");
2635 appendStringInfoString(&result,
2636 " </xsd:sequence>\n"
2637 "</xsd:complexType>\n\n");
2641 appendStringInfo(&result,
2642 "<xsd:complexType name=\"%s\">\n"
2644 " <xsd:element name=\"row\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n"
2645 " </xsd:sequence>\n"
2646 "</xsd:complexType>\n\n",
2647 tabletypename, rowtypename);
2649 appendStringInfo(&result,
2650 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2651 xmltn, tabletypename);
2654 appendStringInfo(&result,
2655 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2656 xmltn, rowtypename);
2658 xsd_schema_element_end(&result);
2665 * Map an SQL schema to XML Schema data types; see SQL/XML section
2669 map_sql_schema_to_xmlschema_types(Oid nspid, List *relid_list, bool nulls,
2670 bool tableforest, const char *targetns)
2675 char *schematypename;
2676 StringInfoData result;
2679 dbname = get_database_name(MyDatabaseId);
2680 nspname = get_namespace_name(nspid);
2682 initStringInfo(&result);
2684 xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
2686 schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
2691 appendStringInfo(&result,
2692 "<xsd:complexType name=\"%s\">\n", schematypename);
2694 appendStringInfoString(&result,
2697 appendStringInfoString(&result,
2698 " <xsd:sequence>\n");
2700 foreach(cell, relid_list)
2702 Oid relid = lfirst_oid(cell);
2703 char *relname = get_rel_name(relid);
2704 char *xmltn = map_sql_identifier_to_xml_name(relname, true, false);
2705 char *tabletypename = map_multipart_sql_identifier_to_xml_name(tableforest ? "RowType" : "TableType",
2711 appendStringInfo(&result,
2712 " <xsd:element name=\"%s\" type=\"%s\"/>\n",
2713 xmltn, tabletypename);
2715 appendStringInfo(&result,
2716 " <xsd:element name=\"%s\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n",
2717 xmltn, tabletypename);
2721 appendStringInfoString(&result,
2724 appendStringInfoString(&result,
2725 " </xsd:sequence>\n");
2726 appendStringInfoString(&result,
2727 "</xsd:complexType>\n\n");
2729 appendStringInfo(&result,
2730 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2731 xmlsn, schematypename);
2738 * Map an SQL catalog to XML Schema data types; see SQL/XML section
2742 map_sql_catalog_to_xmlschema_types(List *nspid_list, bool nulls,
2743 bool tableforest, const char *targetns)
2747 char *catalogtypename;
2748 StringInfoData result;
2751 dbname = get_database_name(MyDatabaseId);
2753 initStringInfo(&result);
2755 xmlcn = map_sql_identifier_to_xml_name(dbname, true, false);
2757 catalogtypename = map_multipart_sql_identifier_to_xml_name("CatalogType",
2762 appendStringInfo(&result,
2763 "<xsd:complexType name=\"%s\">\n", catalogtypename);
2764 appendStringInfoString(&result,
2767 foreach(cell, nspid_list)
2769 Oid nspid = lfirst_oid(cell);
2770 char *nspname = get_namespace_name(nspid);
2771 char *xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
2772 char *schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
2777 appendStringInfo(&result,
2778 " <xsd:element name=\"%s\" type=\"%s\"/>\n",
2779 xmlsn, schematypename);
2782 appendStringInfoString(&result,
2784 appendStringInfoString(&result,
2785 "</xsd:complexType>\n\n");
2787 appendStringInfo(&result,
2788 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2789 xmlcn, catalogtypename);
2796 * Map an SQL data type to an XML name; see SQL/XML:2003 section 9.9.
2799 map_sql_type_to_xml_name(Oid typeoid, int typmod)
2801 StringInfoData result;
2803 initStringInfo(&result);
2809 appendStringInfo(&result, "CHAR");
2811 appendStringInfo(&result, "CHAR_%d", typmod - VARHDRSZ);
2815 appendStringInfo(&result, "VARCHAR");
2817 appendStringInfo(&result, "VARCHAR_%d", typmod - VARHDRSZ);
2821 appendStringInfo(&result, "NUMERIC");
2823 appendStringInfo(&result, "NUMERIC_%d_%d",
2824 ((typmod - VARHDRSZ) >> 16) & 0xffff,
2825 (typmod - VARHDRSZ) & 0xffff);
2828 appendStringInfo(&result, "INTEGER");
2831 appendStringInfo(&result, "SMALLINT");
2834 appendStringInfo(&result, "BIGINT");
2837 appendStringInfo(&result, "REAL");
2840 appendStringInfo(&result, "DOUBLE");
2843 appendStringInfo(&result, "BOOLEAN");
2847 appendStringInfo(&result, "TIME");
2849 appendStringInfo(&result, "TIME_%d", typmod);
2853 appendStringInfo(&result, "TIME_WTZ");
2855 appendStringInfo(&result, "TIME_WTZ_%d", typmod);
2859 appendStringInfo(&result, "TIMESTAMP");
2861 appendStringInfo(&result, "TIMESTAMP_%d", typmod);
2863 case TIMESTAMPTZOID:
2865 appendStringInfo(&result, "TIMESTAMP_WTZ");
2867 appendStringInfo(&result, "TIMESTAMP_WTZ_%d", typmod);
2870 appendStringInfo(&result, "DATE");
2873 appendStringInfo(&result, "XML");
2878 Form_pg_type typtuple;
2880 tuple = SearchSysCache(TYPEOID,
2881 ObjectIdGetDatum(typeoid),
2883 if (!HeapTupleIsValid(tuple))
2884 elog(ERROR, "cache lookup failed for type %u", typeoid);
2885 typtuple = (Form_pg_type) GETSTRUCT(tuple);
2887 appendStringInfoString(&result,
2888 map_multipart_sql_identifier_to_xml_name((typtuple->typtype == TYPTYPE_DOMAIN) ? "Domain" : "UDT",
2889 get_database_name(MyDatabaseId),
2890 get_namespace_name(typtuple->typnamespace),
2891 NameStr(typtuple->typname)));
2893 ReleaseSysCache(tuple);
2902 * Map a collection of SQL data types to XML Schema data types; see
2903 * SQL/XML:2002 section 9.10.
2906 map_sql_typecoll_to_xmlschema_types(List *tupdesc_list)
2908 List *uniquetypes = NIL;
2910 StringInfoData result;
2913 /* extract all column types used in the set of TupleDescs */
2914 foreach(cell0, tupdesc_list)
2916 TupleDesc tupdesc = (TupleDesc) lfirst(cell0);
2918 for (i = 0; i < tupdesc->natts; i++)
2920 if (tupdesc->attrs[i]->attisdropped)
2922 uniquetypes = list_append_unique_oid(uniquetypes,
2923 tupdesc->attrs[i]->atttypid);
2927 /* add base types of domains */
2928 foreach(cell0, uniquetypes)
2930 Oid typid = lfirst_oid(cell0);
2931 Oid basetypid = getBaseType(typid);
2933 if (basetypid != typid)
2934 uniquetypes = list_append_unique_oid(uniquetypes, basetypid);
2937 /* Convert to textual form */
2938 initStringInfo(&result);
2940 foreach(cell0, uniquetypes)
2942 appendStringInfo(&result, "%s\n",
2943 map_sql_type_to_xmlschema_type(lfirst_oid(cell0),
2952 * Map an SQL data type to a named XML Schema data type; see SQL/XML
2953 * sections 9.11 and 9.15.
2955 * (The distinction between 9.11 and 9.15 is basically that 9.15 adds
2956 * a name attribute, which this function does. The name-less version
2957 * 9.11 doesn't appear to be required anywhere.)
2960 map_sql_type_to_xmlschema_type(Oid typeoid, int typmod)
2962 StringInfoData result;
2963 const char *typename = map_sql_type_to_xml_name(typeoid, typmod);
2965 initStringInfo(&result);
2967 if (typeoid == XMLOID)
2969 appendStringInfo(&result,
2970 "<xsd:complexType mixed=\"true\">\n"
2972 " <xsd:any name=\"element\" minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>\n"
2973 " </xsd:sequence>\n"
2974 "</xsd:complexType>\n");
2978 appendStringInfo(&result,
2979 "<xsd:simpleType name=\"%s\">\n", typename);
2987 appendStringInfo(&result,
2988 " <xsd:restriction base=\"xsd:string\">\n"
2989 " <xsd:maxLength value=\"%d\"/>\n"
2990 " </xsd:restriction>\n",
2995 appendStringInfo(&result,
2996 " <xsd:restriction base=\"xsd:%s\">\n"
2997 " </xsd:restriction>\n",
2998 xmlbinary == XMLBINARY_BASE64 ? "base64Binary" : "hexBinary");
3002 appendStringInfo(&result,
3003 " <xsd:restriction base=\"xsd:decimal\">\n"
3004 " <xsd:totalDigits value=\"%d\"/>\n"
3005 " <xsd:fractionDigits value=\"%d\"/>\n"
3006 " </xsd:restriction>\n",
3007 ((typmod - VARHDRSZ) >> 16) & 0xffff,
3008 (typmod - VARHDRSZ) & 0xffff);
3012 appendStringInfo(&result,
3013 " <xsd:restriction base=\"xsd:short\">\n"
3014 " <xsd:maxInclusive value=\"%d\"/>\n"
3015 " <xsd:minInclusive value=\"%d\"/>\n"
3016 " </xsd:restriction>\n",
3017 SHRT_MAX, SHRT_MIN);
3021 appendStringInfo(&result,
3022 " <xsd:restriction base='xsd:int'>\n"
3023 " <xsd:maxInclusive value=\"%d\"/>\n"
3024 " <xsd:minInclusive value=\"%d\"/>\n"
3025 " </xsd:restriction>\n",
3030 appendStringInfo(&result,
3031 " <xsd:restriction base=\"xsd:long\">\n"
3032 " <xsd:maxInclusive value=\"" INT64_FORMAT "\"/>\n"
3033 " <xsd:minInclusive value=\"" INT64_FORMAT "\"/>\n"
3034 " </xsd:restriction>\n",
3035 (((uint64) 1) << (sizeof(int64) * 8 - 1)) - 1,
3036 (((uint64) 1) << (sizeof(int64) * 8 - 1)));
3040 appendStringInfo(&result,
3041 " <xsd:restriction base=\"xsd:float\"></xsd:restriction>\n");
3045 appendStringInfo(&result,
3046 " <xsd:restriction base=\"xsd:double\"></xsd:restriction>\n");
3050 appendStringInfo(&result,
3051 " <xsd:restriction base=\"xsd:boolean\"></xsd:restriction>\n");
3057 const char *tz = (typeoid == TIMETZOID ? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
3060 appendStringInfo(&result,
3061 " <xsd:restriction base=\"xsd:time\">\n"
3062 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}(.\\p{Nd}+)?%s\"/>\n"
3063 " </xsd:restriction>\n", tz);
3064 else if (typmod == 0)
3065 appendStringInfo(&result,
3066 " <xsd:restriction base=\"xsd:time\">\n"
3067 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3068 " </xsd:restriction>\n", tz);
3070 appendStringInfo(&result,
3071 " <xsd:restriction base=\"xsd:time\">\n"
3072 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}.\\p{Nd}{%d}%s\"/>\n"
3073 " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
3078 case TIMESTAMPTZOID:
3080 const char *tz = (typeoid == TIMESTAMPTZOID ? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
3083 appendStringInfo(&result,
3084 " <xsd:restriction base=\"xsd:dateTime\">\n"
3085 " <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"
3086 " </xsd:restriction>\n", tz);
3087 else if (typmod == 0)
3088 appendStringInfo(&result,
3089 " <xsd:restriction base=\"xsd:dateTime\">\n"
3090 " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3091 " </xsd:restriction>\n", tz);
3093 appendStringInfo(&result,
3094 " <xsd:restriction base=\"xsd:dateTime\">\n"
3095 " <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"
3096 " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
3101 appendStringInfo(&result,
3102 " <xsd:restriction base=\"xsd:date\">\n"
3103 " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}\"/>\n"
3104 " </xsd:restriction>\n");
3108 if (get_typtype(typeoid) == TYPTYPE_DOMAIN)
3111 int32 base_typmod = -1;
3113 base_typeoid = getBaseTypeAndTypmod(typeoid, &base_typmod);
3115 appendStringInfo(&result,
3116 " <xsd:restriction base=\"%s\"/>\n",
3117 map_sql_type_to_xml_name(base_typeoid, base_typmod));
3121 appendStringInfo(&result,
3122 "</xsd:simpleType>\n");
3130 * Map an SQL row to an XML element, taking the row from the active
3131 * SPI cursor. See also SQL/XML:2003 section 9.12.
3134 SPI_sql_row_to_xmlelement(int rownum, StringInfo result, char *tablename,
3135 bool nulls, bool tableforest,
3136 const char *targetns, bool top_level)
3142 xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
3152 xmldata_root_element_start(result, xmltn, NULL, targetns, top_level);
3154 appendStringInfoString(result, "<row>\n");
3156 for (i = 1; i <= SPI_tuptable->tupdesc->natts; i++)
3162 colname = map_sql_identifier_to_xml_name(SPI_fname(SPI_tuptable->tupdesc, i),
3164 colval = SPI_getbinval(SPI_tuptable->vals[rownum],
3165 SPI_tuptable->tupdesc,
3171 appendStringInfo(result, " <%s xsi:nil='true'/>\n", colname);
3174 appendStringInfo(result, " <%s>%s</%s>\n",
3176 map_sql_value_to_xml_value(colval,
3177 SPI_gettypeid(SPI_tuptable->tupdesc, i)),
3183 xmldata_root_element_end(result, xmltn);
3184 appendStringInfoChar(result, '\n');
3187 appendStringInfoString(result, "</row>\n\n");
3192 * XPath related functions
3197 * Convert XML node to text (dump subtree in case of element,
3198 * return value otherwise)
3201 xml_xmlnodetoxmltype(xmlNodePtr cur)
3208 if (cur->type == XML_ELEMENT_NODE)
3210 buf = xmlBufferCreate();
3211 xmlNodeDump(buf, NULL, cur, 0, 1);
3212 result = xmlBuffer_to_xmltype(buf);
3217 str = xmlXPathCastNodeToString(cur);
3218 len = strlen((char *) str);
3219 result = (text *) palloc(len + VARHDRSZ);
3220 SET_VARSIZE(result, len + VARHDRSZ);
3221 memcpy(VARDATA(result), str, len);
3230 * Evaluate XPath expression and return array of XML values.
3232 * As we have no support of XQuery sequences yet, this function seems
3233 * to be the most useful one (array of XML functions plays a role of
3234 * some kind of substitution for XQuery sequences).
3236 * Workaround here: we parse XML data in different way to allow XPath for
3237 * fragments (see "XPath for fragment" TODO comment inside).
3240 xpath(PG_FUNCTION_ARGS)
3243 text *xpath_expr_text = PG_GETARG_TEXT_P(0);
3244 xmltype *data = PG_GETARG_XML_P(1);
3245 ArrayType *namespaces = PG_GETARG_ARRAYTYPE_P(2);
3246 ArrayBuildState *astate = NULL;
3247 xmlParserCtxtPtr ctxt = NULL;
3248 xmlDocPtr doc = NULL;
3249 xmlXPathContextPtr xpathctx = NULL;
3250 xmlXPathCompExprPtr xpathcomp = NULL;
3251 xmlXPathObjectPtr xpathobj = NULL;
3256 xmlChar *xpath_expr;
3260 Datum *ns_names_uris;
3261 bool *ns_names_uris_nulls;
3265 * Namespace mappings are passed as text[]. If an empty array is passed
3266 * (ndim = 0, "0-dimensional"), then there are no namespace mappings.
3267 * Else, a 2-dimensional array with length of the second axis being equal
3268 * to 2 should be passed, i.e., every subarray contains 2 elements, the
3269 * first element defining the name, the second one the URI. Example:
3270 * ARRAY[ARRAY['myns', 'http://example.com'], ARRAY['myns2',
3271 * 'http://example2.com']].
3273 ndim = ARR_NDIM(namespaces);
3278 dims = ARR_DIMS(namespaces);
3280 if (ndim != 2 || dims[1] != 2)
3282 (errcode(ERRCODE_DATA_EXCEPTION),
3283 errmsg("invalid array for XML namespace mapping"),
3284 errdetail("The array must be two-dimensional with length of the second axis equal to 2.")));
3286 Assert(ARR_ELEMTYPE(namespaces) == TEXTOID);
3288 deconstruct_array(namespaces, TEXTOID, -1, false, 'i',
3289 &ns_names_uris, &ns_names_uris_nulls,
3292 Assert((ns_count % 2) == 0); /* checked above */
3293 ns_count /= 2; /* count pairs only */
3297 ns_names_uris = NULL;
3298 ns_names_uris_nulls = NULL;
3302 datastr = VARDATA(data);
3303 len = VARSIZE(data) - VARHDRSZ;
3304 xpath_len = VARSIZE(xpath_expr_text) - VARHDRSZ;
3307 (errcode(ERRCODE_DATA_EXCEPTION),
3308 errmsg("empty XPath expression")));
3313 * To handle both documents and fragments, regardless of the fact whether
3314 * the XML datum has a single root (XML well-formedness), we wrap the XML
3315 * datum in a dummy element (<x>...</x>) and extend the XPath expression
3316 * accordingly. To do it, throw away the XML prolog, if any.
3319 xmlStrncmp((xmlChar *) datastr, (xmlChar *) "<?xml", 5) == 0)
3323 !(datastr[i - 1] == '?' && datastr[i] == '>'))
3327 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3328 "could not parse XML data");
3336 string = (xmlChar *) palloc((len + 8) * sizeof(xmlChar));
3337 memcpy(string, "<x>", 3);
3338 memcpy(string + 3, datastr, len);
3339 memcpy(string + 3 + len, "</x>", 5);
3342 xpath_expr = (xmlChar *) palloc((xpath_len + 3) * sizeof(xmlChar));
3343 memcpy(xpath_expr, "/x", 2);
3344 memcpy(xpath_expr + 2, VARDATA(xpath_expr_text), xpath_len);
3345 xpath_expr[xpath_len + 2] = '\0';
3348 /* We use a PG_TRY block to ensure libxml parser is cleaned up on error */
3354 * redundant XML parsing (two parsings for the same value during one
3355 * command execution are possible)
3357 ctxt = xmlNewParserCtxt();
3359 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3360 "could not allocate parser context");
3361 doc = xmlCtxtReadMemory(ctxt, (char *) string, len, NULL, NULL, 0);
3363 xml_ereport(ERROR, ERRCODE_INVALID_XML_DOCUMENT,
3364 "could not parse XML data");
3365 xpathctx = xmlXPathNewContext(doc);
3366 if (xpathctx == NULL)
3367 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3368 "could not allocate XPath context");
3369 xpathctx->node = xmlDocGetRootElement(doc);
3370 if (xpathctx->node == NULL)
3371 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3372 "could not find root XML element");
3374 /* register namespaces, if any */
3377 for (i = 0; i < ns_count; i++)
3382 if (ns_names_uris_nulls[i * 2] ||
3383 ns_names_uris_nulls[i * 2 + 1])
3385 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3386 errmsg("neither namespace name nor URI may be null")));
3387 ns_name = _textout(ns_names_uris[i * 2]);
3388 ns_uri = _textout(ns_names_uris[i * 2 + 1]);
3389 if (xmlXPathRegisterNs(xpathctx,
3390 (xmlChar *) ns_name,
3391 (xmlChar *) ns_uri) != 0)
3392 ereport(ERROR, /* is this an internal error??? */
3393 (errmsg("could not register XML namespace with name \"%s\" and URI \"%s\"",
3398 xpathcomp = xmlXPathCompile(xpath_expr);
3399 if (xpathcomp == NULL) /* TODO: show proper XPath error details */
3400 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3401 "invalid XPath expression");
3403 xpathobj = xmlXPathCompiledEval(xpathcomp, xpathctx);
3404 if (xpathobj == NULL) /* TODO: reason? */
3406 (errmsg("could not create XPath object")));
3408 xmlXPathFreeCompExpr(xpathcomp);
3411 /* return empty array in cases when nothing is found */
3412 if (xpathobj->nodesetval == NULL)
3415 res_nitems = xpathobj->nodesetval->nodeNr;
3418 for (i = 0; i < xpathobj->nodesetval->nodeNr; i++)
3421 bool elemisnull = false;
3423 elem = PointerGetDatum(xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[i]));
3424 astate = accumArrayResult(astate, elem,
3426 CurrentMemoryContext);
3429 xmlXPathFreeObject(xpathobj);
3431 xmlXPathFreeContext(xpathctx);
3435 xmlFreeParserCtxt(ctxt);
3442 xmlXPathFreeCompExpr(xpathcomp);
3444 xmlXPathFreeObject(xpathobj);
3446 xmlXPathFreeContext(xpathctx);
3450 xmlFreeParserCtxt(ctxt);
3457 if (res_nitems == 0)
3458 PG_RETURN_ARRAYTYPE_P(construct_empty_array(XMLOID));
3460 PG_RETURN_ARRAYTYPE_P(makeArrayResult(astate, CurrentMemoryContext));