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.51 2007/11/06 03:06:28 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);
102 #endif /* USE_LIBXML */
104 static StringInfo query_to_xml_internal(const char *query, char *tablename,
105 const char *xmlschema, bool nulls, bool tableforest,
106 const char *targetns, bool top_level);
107 static const char *map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid,
108 bool nulls, bool tableforest, const char *targetns);
109 static const char *map_sql_schema_to_xmlschema_types(Oid nspid,
110 List *relid_list, bool nulls,
111 bool tableforest, const char *targetns);
112 static const char *map_sql_catalog_to_xmlschema_types(List *nspid_list,
113 bool nulls, bool tableforest,
114 const char *targetns);
115 static const char * map_sql_type_to_xml_name(Oid typeoid, int typmod);
116 static const char * map_sql_typecoll_to_xmlschema_types(List *tupdesc_list);
117 static const char * map_sql_type_to_xmlschema_type(Oid typeoid, int typmod);
118 static void SPI_sql_row_to_xmlelement(int rownum, StringInfo result,
119 char *tablename, bool nulls, bool tableforest,
120 const char *targetns, bool top_level);
122 #define NO_XML_SUPPORT() \
124 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
125 errmsg("unsupported XML feature"), \
126 errdetail("This functionality requires libxml support."), \
127 errhint("You need to re-compile PostgreSQL using --with-libxml.")))
130 #define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str))
131 #define _textout(x) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(x)))
134 /* from SQL/XML:2003 section 4.7 */
135 #define NAMESPACE_XSD "http://www.w3.org/2001/XMLSchema"
136 #define NAMESPACE_XSI "http://www.w3.org/2001/XMLSchema-instance"
137 #define NAMESPACE_SQLXML "http://standards.iso.org/iso/9075/2003/sqlxml"
143 xmlChar_to_encoding(xmlChar *encoding_name)
145 int encoding = pg_char_to_encoding((char *) encoding_name);
149 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
150 errmsg("invalid encoding name \"%s\"",
151 (char *) encoding_name)));
159 xml_in(PG_FUNCTION_ARGS)
162 char *s = PG_GETARG_CSTRING(0);
168 vardata = palloc(len + VARHDRSZ);
169 SET_VARSIZE(vardata, len + VARHDRSZ);
170 memcpy(VARDATA(vardata), s, len);
173 * Parse the data to check if it is well-formed XML data. Assume
174 * that ERROR occurred if parsing failed.
176 doc = xml_parse(vardata, xmloption, true, NULL);
179 PG_RETURN_XML_P(vardata);
187 #define PG_XML_DEFAULT_VERSION "1.0"
191 xml_out_internal(xmltype *x, pg_enc target_encoding)
202 len = VARSIZE(x) - VARHDRSZ;
203 str = palloc(len + 1);
204 memcpy(str, VARDATA(x), len);
208 if ((res_code = parse_xml_decl((xmlChar *) str,
209 &len, &version, &encoding, &standalone)) == 0)
213 initStringInfo(&buf);
215 if (!print_xml_decl(&buf, version, target_encoding, standalone))
218 * If we are not going to produce an XML declaration, eat
219 * a single newline in the original string to prevent
220 * empty first lines in the output.
222 if (*(str + len) == '\n')
225 appendStringInfoString(&buf, str + len);
230 xml_ereport_by_code(WARNING, ERRCODE_INTERNAL_ERROR,
231 "could not parse XML declaration in stored value",
239 xml_out(PG_FUNCTION_ARGS)
241 xmltype *x = PG_GETARG_XML_P(0);
244 * xml_out removes the encoding property in all cases. This is
245 * because we cannot control from here whether the datum will be
246 * converted to a different client encoding, so we'd do more harm
247 * than good by including it.
249 PG_RETURN_CSTRING(xml_out_internal(x, 0));
254 xml_recv(PG_FUNCTION_ARGS)
257 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
263 xmlChar *encoding = NULL;
266 * Read the data in raw format. We don't know yet what the encoding
267 * is, as that information is embedded in the xml declaration; so we
268 * have to parse that before converting to server encoding.
270 nbytes = buf->len - buf->cursor;
271 str = (char *) pq_getmsgbytes(buf, nbytes);
274 * We need a null-terminated string to pass to parse_xml_decl(). Rather
275 * than make a separate copy, make the temporary result one byte bigger
276 * than it needs to be.
278 result = palloc(nbytes + 1 + VARHDRSZ);
279 SET_VARSIZE(result, nbytes + VARHDRSZ);
280 memcpy(VARDATA(result), str, nbytes);
281 str = VARDATA(result);
284 parse_xml_decl((xmlChar *) str, NULL, NULL, &encoding, NULL);
287 * Parse the data to check if it is well-formed XML data. Assume
288 * that xml_parse will throw ERROR if not.
290 doc = xml_parse(result, xmloption, true, encoding);
293 /* Now that we know what we're dealing with, convert to server encoding */
294 newstr = (char *) pg_do_encoding_conversion((unsigned char *) str,
297 xmlChar_to_encoding(encoding) :
299 GetDatabaseEncoding());
305 nbytes = strlen(newstr);
307 result = palloc(nbytes + VARHDRSZ);
308 SET_VARSIZE(result, nbytes + VARHDRSZ);
309 memcpy(VARDATA(result), newstr, nbytes);
314 PG_RETURN_XML_P(result);
323 xml_send(PG_FUNCTION_ARGS)
325 xmltype *x = PG_GETARG_XML_P(0);
330 * xml_out_internal doesn't convert the encoding, it just prints
331 * the right declaration. pq_sendtext will do the conversion.
333 outval = xml_out_internal(x, pg_get_client_encoding());
335 pq_begintypsend(&buf);
336 pq_sendtext(&buf, outval, strlen(outval));
338 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
344 appendStringInfoText(StringInfo str, const text *t)
346 appendBinaryStringInfo(str, VARDATA(t), VARSIZE(t) - VARHDRSZ);
352 stringinfo_to_xmltype(StringInfo buf)
357 len = buf->len + VARHDRSZ;
358 result = palloc(len);
359 SET_VARSIZE(result, len);
360 memcpy(VARDATA(result), buf->data, buf->len);
367 cstring_to_xmltype(const char *string)
372 len = strlen(string) + VARHDRSZ;
373 result = palloc(len);
374 SET_VARSIZE(result, len);
375 memcpy(VARDATA(result), string, len - VARHDRSZ);
383 xmlBuffer_to_xmltype(xmlBufferPtr buf)
388 len = xmlBufferLength(buf) + VARHDRSZ;
389 result = palloc(len);
390 SET_VARSIZE(result, len);
391 memcpy(VARDATA(result), xmlBufferContent(buf), len - VARHDRSZ);
399 xmlcomment(PG_FUNCTION_ARGS)
402 text *arg = PG_GETARG_TEXT_P(0);
403 char *argdata = VARDATA(arg);
404 int len = VARSIZE(arg) - VARHDRSZ;
408 /* check for "--" in string or "-" at the end */
409 for (i = 1; i < len; i++)
411 if (argdata[i] == '-' && argdata[i - 1] == '-')
413 (errcode(ERRCODE_INVALID_XML_COMMENT),
414 errmsg("invalid XML comment")));
416 if (len > 0 && argdata[len - 1] == '-')
418 (errcode(ERRCODE_INVALID_XML_COMMENT),
419 errmsg("invalid XML comment")));
421 initStringInfo(&buf);
422 appendStringInfo(&buf, "<!--");
423 appendStringInfoText(&buf, arg);
424 appendStringInfo(&buf, "-->");
426 PG_RETURN_XML_P(stringinfo_to_xmltype(&buf));
436 * TODO: xmlconcat needs to merge the notations and unparsed entities
437 * of the argument values. Not very important in practice, though.
440 xmlconcat(List *args)
443 int global_standalone = 1;
444 xmlChar *global_version = NULL;
445 bool global_version_no_value = false;
449 initStringInfo(&buf);
452 xmltype *x = DatumGetXmlP(PointerGetDatum(lfirst(v)));
458 len = VARSIZE(x) - VARHDRSZ;
459 str = palloc(len + 1);
460 memcpy(str, VARDATA(x), len);
463 parse_xml_decl((xmlChar *) str, &len, &version, NULL, &standalone);
465 if (standalone == 0 && global_standalone == 1)
466 global_standalone = 0;
468 global_standalone = -1;
471 global_version_no_value = true;
472 else if (!global_version)
473 global_version = xmlStrdup(version);
474 else if (xmlStrcmp(version, global_version) != 0)
475 global_version_no_value = true;
477 appendStringInfoString(&buf, str + len);
481 if (!global_version_no_value || global_standalone >= 0)
485 initStringInfo(&buf2);
487 print_xml_decl(&buf2,
488 (!global_version_no_value) ? global_version : NULL,
492 appendStringInfoString(&buf2, buf.data);
496 return stringinfo_to_xmltype(&buf);
508 xmlconcat2(PG_FUNCTION_ARGS)
515 PG_RETURN_XML_P(PG_GETARG_XML_P(1));
517 else if (PG_ARGISNULL(1))
518 PG_RETURN_XML_P(PG_GETARG_XML_P(0));
520 PG_RETURN_XML_P(xmlconcat(list_make2(PG_GETARG_XML_P(0),
521 PG_GETARG_XML_P(1))));
526 texttoxml(PG_FUNCTION_ARGS)
528 text *data = PG_GETARG_TEXT_P(0);
530 PG_RETURN_XML_P(xmlparse(data, xmloption, true));
535 xmltotext(PG_FUNCTION_ARGS)
537 xmltype *data = PG_GETARG_XML_P(0);
539 PG_RETURN_TEXT_P(xmltotext_with_xmloption(data, xmloption));
544 xmltotext_with_xmloption(xmltype *data, XmlOptionType xmloption_arg)
546 if (xmloption_arg == XMLOPTION_DOCUMENT && !xml_is_document(data))
548 (errcode(ERRCODE_NOT_AN_XML_DOCUMENT),
549 errmsg("not an XML document")));
551 /* It's actually binary compatible, save for the above check. */
552 return (text *) data;
557 xmlelement(XmlExprState *xmlExpr, ExprContext *econtext)
560 XmlExpr *xexpr = (XmlExpr *) xmlExpr->xprstate.expr;
562 List *named_arg_strings;
568 xmlTextWriterPtr writer;
571 * We first evaluate all the arguments, then start up libxml and
572 * create the result. This avoids issues if one of the arguments
573 * involves a call to some other function or subsystem that wants to use
574 * libxml on its own terms.
576 named_arg_strings = NIL;
578 foreach(arg, xmlExpr->named_args)
580 ExprState *e = (ExprState *) lfirst(arg);
585 value = ExecEvalExpr(e, econtext, &isnull, NULL);
589 str = OutputFunctionCall(&xmlExpr->named_outfuncs[i], value);
590 named_arg_strings = lappend(named_arg_strings, str);
595 foreach(arg, xmlExpr->args)
597 ExprState *e = (ExprState *) lfirst(arg);
602 value = ExecEvalExpr(e, econtext, &isnull, NULL);
603 /* here we can just forget NULL elements immediately */
606 str = map_sql_value_to_xml_value(value,
607 exprType((Node *) e->expr));
608 arg_strings = lappend(arg_strings, str);
612 /* now safe to run libxml */
615 buf = xmlBufferCreate();
616 writer = xmlNewTextWriterMemory(buf, 0);
618 xmlTextWriterStartElement(writer, (xmlChar *) xexpr->name);
620 forboth(arg, named_arg_strings, narg, xexpr->arg_names)
622 char *str = (char *) lfirst(arg);
623 char *argname = strVal(lfirst(narg));
627 xmlTextWriterWriteAttribute(writer,
634 foreach(arg, arg_strings)
636 char *str = (char *) lfirst(arg);
638 xmlTextWriterWriteRaw(writer, (xmlChar *) str);
641 xmlTextWriterEndElement(writer);
642 xmlFreeTextWriter(writer);
644 result = xmlBuffer_to_xmltype(buf);
656 xmlparse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace)
661 doc = xml_parse(data, xmloption_arg, preserve_whitespace, NULL);
664 return (xmltype *) data;
673 xmlpi(char *target, text *arg, bool arg_is_null, bool *result_is_null)
679 if (pg_strncasecmp(target, "xml", 3) == 0)
681 (errcode(ERRCODE_SYNTAX_ERROR), /* really */
682 errmsg("invalid XML processing instruction"),
683 errdetail("XML processing instruction target name cannot start with \"xml\".")));
686 * Following the SQL standard, the null check comes after the
687 * syntax check above.
689 *result_is_null = arg_is_null;
693 initStringInfo(&buf);
695 appendStringInfo(&buf, "<?%s", target);
701 string = _textout(arg);
702 if (strstr(string, "?>") != NULL)
704 (errcode(ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION),
705 errmsg("invalid XML processing instruction"),
706 errdetail("XML processing instruction cannot contain \"?>\".")));
708 appendStringInfoChar(&buf, ' ');
709 appendStringInfoString(&buf, string + strspn(string, " "));
712 appendStringInfoString(&buf, "?>");
714 result = stringinfo_to_xmltype(&buf);
725 xmlroot(xmltype *data, text *version, int standalone)
730 xmlChar *orig_version;
734 len = VARSIZE(data) - VARHDRSZ;
735 str = palloc(len + 1);
736 memcpy(str, VARDATA(data), len);
739 parse_xml_decl((xmlChar *) str, &len, &orig_version, NULL, &orig_standalone);
742 orig_version = xml_text2xmlChar(version);
748 case XML_STANDALONE_YES:
751 case XML_STANDALONE_NO:
754 case XML_STANDALONE_NO_VALUE:
755 orig_standalone = -1;
757 case XML_STANDALONE_OMITTED:
758 /* leave original value */
762 initStringInfo(&buf);
763 print_xml_decl(&buf, orig_version, 0, orig_standalone);
764 appendStringInfoString(&buf, str + len);
766 return stringinfo_to_xmltype(&buf);
775 * Validate document (given as string) against DTD (given as external link)
776 * TODO !!! use text instead of cstring for second arg
777 * TODO allow passing DTD as a string value (not only as an URI)
778 * TODO redesign (see comment with '!!!' below)
781 xmlvalidate(PG_FUNCTION_ARGS)
784 text *data = PG_GETARG_TEXT_P(0);
785 text *dtdOrUri = PG_GETARG_TEXT_P(1);
787 xmlParserCtxtPtr ctxt = NULL;
788 xmlDocPtr doc = NULL;
789 xmlDtdPtr dtd = NULL;
793 /* We use a PG_TRY block to ensure libxml parser is cleaned up on error */
797 ctxt = xmlNewParserCtxt();
799 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
800 "could not allocate parser context");
802 doc = xmlCtxtReadMemory(ctxt, (char *) VARDATA(data),
803 VARSIZE(data) - VARHDRSZ,
806 xml_ereport(ERROR, ERRCODE_INVALID_XML_DOCUMENT,
807 "could not parse XML data");
810 uri = xmlCreateURI();
811 elog(NOTICE, "dtd - %s", dtdOrUri);
812 dtd = palloc(sizeof(xmlDtdPtr));
813 uri = xmlParseURI(dtdOrUri);
815 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
816 "not implemented yet... (TODO)");
819 dtd = xmlParseDTD(NULL, xml_text2xmlChar(dtdOrUri));
822 xml_ereport(ERROR, ERRCODE_INVALID_XML_DOCUMENT,
823 "could not load DTD");
825 if (xmlValidateDtd(xmlNewValidCtxt(), doc, dtd) == 1)
829 xml_ereport(NOTICE, ERRCODE_INVALID_XML_DOCUMENT,
830 "validation against DTD failed");
844 xmlFreeParserCtxt(ctxt);
859 xmlFreeParserCtxt(ctxt);
866 PG_RETURN_BOOL(result);
867 #else /* not USE_LIBXML */
870 #endif /* not USE_LIBXML */
875 xml_is_document(xmltype *arg)
879 xmlDocPtr doc = NULL;
880 MemoryContext ccxt = CurrentMemoryContext;
884 doc = xml_parse((text *) arg, XMLOPTION_DOCUMENT, true, NULL);
892 ecxt = MemoryContextSwitchTo(ccxt);
893 errdata = CopyErrorData();
894 if (errdata->sqlerrcode == ERRCODE_INVALID_XML_DOCUMENT)
901 MemoryContextSwitchTo(ecxt);
911 #else /* not USE_LIBXML */
914 #endif /* not USE_LIBXML */
921 * Set up for use of libxml --- this should be called by each function that
922 * is about to use libxml facilities.
924 * TODO: xmlChar is utf8-char, make proper tuning (initdb with enc!=utf8 and
930 static bool first_time = true;
934 /* Stuff we need do only once per session */
935 MemoryContext oldcontext;
938 * Currently, we have no pure UTF-8 support for internals -- check
941 if (sizeof(char) != sizeof(xmlChar))
943 (errmsg("could not initialize XML library"),
944 errdetail("libxml2 has incompatible char type: sizeof(char)=%u, sizeof(xmlChar)=%u.",
945 (int) sizeof(char), (int) sizeof(xmlChar))));
947 /* create error buffer in permanent context */
948 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
949 xml_err_buf = makeStringInfo();
950 MemoryContextSwitchTo(oldcontext);
952 /* Now that xml_err_buf exists, safe to call xml_errorHandler */
953 xmlSetGenericErrorFunc(NULL, xml_errorHandler);
955 /* Set up memory allocation our way, too */
956 xmlMemSetup(xml_pfree, xml_palloc, xml_repalloc, xml_pstrdup);
958 /* Check library compatibility */
965 /* Reset pre-existing buffer to empty */
966 Assert(xml_err_buf != NULL);
967 resetStringInfo(xml_err_buf);
970 * We re-establish the callback functions every time. This makes it
971 * safe for other subsystems (PL/Perl, say) to also use libxml with
972 * their own callbacks ... so long as they likewise set up the
973 * callbacks on every use. It's cheap enough to not be worth
974 * worrying about, anyway.
976 xmlSetGenericErrorFunc(NULL, xml_errorHandler);
977 xmlMemSetup(xml_pfree, xml_palloc, xml_repalloc, xml_pstrdup);
983 * SQL/XML allows storing "XML documents" or "XML content". "XML
984 * documents" are specified by the XML specification and are parsed
985 * easily by libxml. "XML content" is specified by SQL/XML as the
986 * production "XMLDecl? content". But libxml can only parse the
987 * "content" part, so we have to parse the XML declaration ourselves
991 #define CHECK_XML_SPACE(p) \
993 if (!xmlIsBlank_ch(*(p))) \
994 return XML_ERR_SPACE_REQUIRED; \
997 #define SKIP_XML_SPACE(p) \
998 while (xmlIsBlank_ch(*(p))) (p)++
1001 parse_xml_decl(const xmlChar *str,size_t *lenp,
1002 xmlChar **version, xmlChar **encoding, int *standalone)
1005 const xmlChar *save_p;
1019 if (xmlStrncmp(p, (xmlChar *)"<?xml", 5) != 0)
1027 if (xmlStrncmp(p, (xmlChar *)"version", 7) != 0)
1028 return XML_ERR_VERSION_MISSING;
1032 return XML_ERR_VERSION_MISSING;
1036 if (*p == '\'' || *p == '"')
1040 q = xmlStrchr(p + 1, *p);
1042 return XML_ERR_VERSION_MISSING;
1045 *version = xmlStrndup(p + 1, q - p - 1);
1049 return XML_ERR_VERSION_MISSING;
1054 if (xmlStrncmp(p, (xmlChar *)"encoding", 8) == 0)
1056 CHECK_XML_SPACE(save_p);
1060 return XML_ERR_MISSING_ENCODING;
1064 if (*p == '\'' || *p == '"')
1068 q = xmlStrchr(p + 1, *p);
1070 return XML_ERR_MISSING_ENCODING;
1073 *encoding = xmlStrndup(p + 1, q - p - 1);
1077 return XML_ERR_MISSING_ENCODING;
1087 if (xmlStrncmp(p, (xmlChar *)"standalone", 10) == 0)
1089 CHECK_XML_SPACE(save_p);
1093 return XML_ERR_STANDALONE_VALUE;
1096 if (xmlStrncmp(p, (xmlChar *)"'yes'", 5) == 0 || xmlStrncmp(p, (xmlChar *)"\"yes\"", 5) == 0)
1101 else if (xmlStrncmp(p, (xmlChar *)"'no'", 4) == 0 || xmlStrncmp(p, (xmlChar *)"\"no\"", 4) == 0)
1107 return XML_ERR_STANDALONE_VALUE;
1115 if (xmlStrncmp(p, (xmlChar *)"?>", 2) != 0)
1116 return XML_ERR_XMLDECL_NOT_FINISHED;
1122 for (p = str; p < str + len; p++)
1124 return XML_ERR_INVALID_CHAR;
1134 * Write an XML declaration. On output, we adjust the XML declaration
1135 * as follows. (These rules are the moral equivalent of the clause
1136 * "Serialization of an XML value" in the SQL standard.)
1138 * We try to avoid generating an XML declaration if possible. This is
1139 * so that you don't get trivial things like xml '<foo/>' resulting in
1140 * '<?xml version="1.0"?><foo/>', which would surely be annoying. We
1141 * must provide a declaration if the standalone property is specified
1142 * or if we include an encoding declaration. If we have a
1143 * declaration, we must specify a version (XML requires this).
1144 * Otherwise we only make a declaration if the version is not "1.0",
1145 * which is the default version specified in SQL:2003.
1148 print_xml_decl(StringInfo buf, const xmlChar *version,
1149 pg_enc encoding, int standalone)
1153 if ((version && strcmp((char *) version, PG_XML_DEFAULT_VERSION) != 0)
1154 || (encoding && encoding != PG_UTF8)
1155 || standalone != -1)
1157 appendStringInfoString(buf, "<?xml");
1160 appendStringInfo(buf, " version=\"%s\"", version);
1162 appendStringInfo(buf, " version=\"%s\"", PG_XML_DEFAULT_VERSION);
1164 if (encoding && encoding != PG_UTF8)
1167 * XXX might be useful to convert this to IANA names
1168 * (ISO-8859-1 instead of LATIN1 etc.); needs field experience
1170 appendStringInfo(buf, " encoding=\"%s\"",
1171 pg_encoding_to_char(encoding));
1174 if (standalone == 1)
1175 appendStringInfoString(buf, " standalone=\"yes\"");
1176 else if (standalone == 0)
1177 appendStringInfoString(buf, " standalone=\"no\"");
1178 appendStringInfoString(buf, "?>");
1188 * Convert a C string to XML internal representation
1190 * TODO maybe, libxml2's xmlreader is better? (do not construct DOM,
1191 * yet do not use SAX - see xml_reader.c)
1194 xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace,
1199 xmlChar *utf8string;
1200 xmlParserCtxtPtr ctxt = NULL;
1201 xmlDocPtr doc = NULL;
1203 len = VARSIZE(data) - VARHDRSZ; /* will be useful later */
1204 string = xml_text2xmlChar(data);
1206 utf8string = pg_do_encoding_conversion(string,
1209 xmlChar_to_encoding(encoding) :
1210 GetDatabaseEncoding(),
1215 /* We use a PG_TRY block to ensure libxml parser is cleaned up on error */
1219 ctxt = xmlNewParserCtxt();
1221 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
1222 "could not allocate parser context");
1224 if (xmloption_arg == XMLOPTION_DOCUMENT)
1227 * Note, that here we try to apply DTD defaults
1228 * (XML_PARSE_DTDATTR) according to SQL/XML:10.16.7.d:
1229 * 'Default valies defined by internal DTD are applied'.
1230 * As for external DTDs, we try to support them too, (see
1231 * SQL/XML:10.16.7.e)
1233 doc = xmlCtxtReadDoc(ctxt, utf8string,
1236 XML_PARSE_NOENT | XML_PARSE_DTDATTR
1237 | (preserve_whitespace ? 0 : XML_PARSE_NOBLANKS));
1239 xml_ereport(ERROR, ERRCODE_INVALID_XML_DOCUMENT,
1240 "invalid XML document");
1246 xmlChar *version = NULL;
1247 int standalone = -1;
1249 doc = xmlNewDoc(NULL);
1251 res_code = parse_xml_decl(utf8string, &count, &version, NULL, &standalone);
1253 xml_ereport_by_code(ERROR, ERRCODE_INVALID_XML_CONTENT,
1254 "invalid XML content: invalid XML declaration", res_code);
1256 res_code = xmlParseBalancedChunkMemory(doc, NULL, NULL, 0, utf8string + count, NULL);
1258 xml_ereport(ERROR, ERRCODE_INVALID_XML_CONTENT,
1259 "invalid XML content");
1261 doc->version = xmlStrdup(version);
1262 doc->encoding = xmlStrdup((xmlChar *) "UTF-8");
1263 doc->standalone = standalone;
1267 xmlFreeParserCtxt(ctxt);
1276 xmlFreeParserCtxt(ctxt);
1288 * xmlChar<->text convertions
1291 xml_text2xmlChar(text *in)
1293 int32 len = VARSIZE(in) - VARHDRSZ;
1296 res = palloc(len + 1);
1297 memcpy(res, VARDATA(in), len);
1305 * Wrappers for memory management functions
1308 xml_palloc(size_t size)
1310 return palloc(size);
1315 xml_repalloc(void *ptr, size_t size)
1317 return repalloc(ptr, size);
1322 xml_pfree(void *ptr)
1329 xml_pstrdup(const char *string)
1331 return pstrdup(string);
1336 * Wrapper for "ereport" function for XML-related errors. The "msg"
1337 * is the SQL-level message; some can be adopted from the SQL/XML
1338 * standard. This function adds libxml's native error messages, if
1342 xml_ereport(int level, int sqlcode, const char *msg)
1346 if (xml_err_buf->len > 0)
1348 detail = pstrdup(xml_err_buf->data);
1349 resetStringInfo(xml_err_buf);
1354 /* libxml error messages end in '\n'; get rid of it */
1359 len = strlen(detail);
1360 if (len > 0 && detail[len-1] == '\n')
1361 detail[len-1] = '\0';
1366 errdetail("%s", detail)));
1372 errmsg("%s", msg)));
1378 * Error handler for libxml error messages
1381 xml_errorHandler(void *ctxt, const char *msg,...)
1383 /* Append the formatted text to xml_err_buf */
1389 /* Try to format the data. */
1390 va_start(args, msg);
1391 success = appendStringInfoVA(xml_err_buf, msg, args);
1397 /* Double the buffer size and try again. */
1398 enlargeStringInfo(xml_err_buf, xml_err_buf->maxlen);
1404 * Wrapper for "ereport" function for XML-related errors. The "msg"
1405 * is the SQL-level message; some can be adopted from the SQL/XML
1406 * standard. This function uses "code" to create a textual detail
1407 * message. At the moment, we only need to cover those codes that we
1408 * may raise in this file.
1411 xml_ereport_by_code(int level, int sqlcode,
1412 const char *msg, int code)
1418 case XML_ERR_INVALID_CHAR:
1419 det = "Invalid character value";
1421 case XML_ERR_SPACE_REQUIRED:
1422 det = "Space required";
1424 case XML_ERR_STANDALONE_VALUE:
1425 det = "standalone accepts only 'yes' or 'no'";
1427 case XML_ERR_VERSION_MISSING:
1428 det = "Malformed declaration expecting version";
1430 case XML_ERR_MISSING_ENCODING:
1431 det = "Missing encoding in text declaration";
1433 case XML_ERR_XMLDECL_NOT_FINISHED:
1434 det = "Parsing XML declaration: '?>' expected";
1437 det = "Unrecognized libxml error code: %d";
1444 errdetail(det, code)));
1449 * Convert one char in the current server encoding to a Unicode codepoint.
1452 sqlchar_to_unicode(char *s)
1455 pg_wchar ret[2]; /* need space for trailing zero */
1457 utf8string = (char *) pg_do_encoding_conversion((unsigned char *) s,
1459 GetDatabaseEncoding(),
1462 pg_encoding_mb2wchar_with_len(PG_UTF8, utf8string, ret, pg_mblen(s));
1469 is_valid_xml_namefirst(pg_wchar c)
1471 /* (Letter | '_' | ':') */
1472 return (xmlIsBaseCharQ(c) || xmlIsIdeographicQ(c)
1473 || c == '_' || c == ':');
1478 is_valid_xml_namechar(pg_wchar c)
1480 /* Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender */
1481 return (xmlIsBaseCharQ(c) || xmlIsIdeographicQ(c)
1483 || c == '.' || c == '-' || c == '_' || c == ':'
1484 || xmlIsCombiningQ(c)
1485 || xmlIsExtenderQ(c));
1487 #endif /* USE_LIBXML */
1491 * Map SQL identifier to XML name; see SQL/XML:2003 section 9.1.
1494 map_sql_identifier_to_xml_name(char *ident, bool fully_escaped,
1502 * SQL/XML doesn't make use of this case anywhere, so it's
1503 * probably a mistake.
1505 Assert(fully_escaped || !escape_period);
1507 initStringInfo(&buf);
1509 for (p = ident; *p; p += pg_mblen(p))
1511 if (*p == ':' && (p == ident || fully_escaped))
1512 appendStringInfo(&buf, "_x003A_");
1513 else if (*p == '_' && *(p+1) == 'x')
1514 appendStringInfo(&buf, "_x005F_");
1515 else if (fully_escaped && p == ident &&
1516 pg_strncasecmp(p, "xml", 3) == 0)
1519 appendStringInfo(&buf, "_x0078_");
1521 appendStringInfo(&buf, "_x0058_");
1523 else if (escape_period && *p == '.')
1524 appendStringInfo(&buf, "_x002E_");
1527 pg_wchar u = sqlchar_to_unicode(p);
1530 ? !is_valid_xml_namefirst(u)
1531 : !is_valid_xml_namechar(u))
1532 appendStringInfo(&buf, "_x%04X_", (unsigned int) u);
1534 appendBinaryStringInfo(&buf, p, pg_mblen(p));
1539 #else /* not USE_LIBXML */
1542 #endif /* not USE_LIBXML */
1547 * Map a Unicode codepoint into the current server encoding.
1550 unicode_to_sqlchar(pg_wchar c)
1552 static unsigned char utf8string[5]; /* need trailing zero */
1558 else if (c <= 0x7FF)
1560 utf8string[0] = 0xC0 | ((c >> 6) & 0x1F);
1561 utf8string[1] = 0x80 | (c & 0x3F);
1563 else if (c <= 0xFFFF)
1565 utf8string[0] = 0xE0 | ((c >> 12) & 0x0F);
1566 utf8string[1] = 0x80 | ((c >> 6) & 0x3F);
1567 utf8string[2] = 0x80 | (c & 0x3F);
1571 utf8string[0] = 0xF0 | ((c >> 18) & 0x07);
1572 utf8string[1] = 0x80 | ((c >> 12) & 0x3F);
1573 utf8string[2] = 0x80 | ((c >> 6) & 0x3F);
1574 utf8string[3] = 0x80 | (c & 0x3F);
1577 return (char *) pg_do_encoding_conversion(utf8string,
1578 pg_mblen((char *) utf8string),
1580 GetDatabaseEncoding());
1585 * Map XML name to SQL identifier; see SQL/XML:2003 section 9.17.
1588 map_xml_name_to_sql_identifier(char *name)
1593 initStringInfo(&buf);
1595 for (p = name; *p; p += pg_mblen(p))
1597 if (*p == '_' && *(p+1) == 'x'
1598 && isxdigit((unsigned char) *(p+2))
1599 && isxdigit((unsigned char) *(p+3))
1600 && isxdigit((unsigned char) *(p+4))
1601 && isxdigit((unsigned char) *(p+5))
1606 sscanf(p + 2, "%X", &u);
1607 appendStringInfoString(&buf, unicode_to_sqlchar(u));
1611 appendBinaryStringInfo(&buf, p, pg_mblen(p));
1618 * Map SQL value to XML value; see SQL/XML:2003 section 9.16.
1621 map_sql_value_to_xml_value(Datum value, Oid type)
1625 initStringInfo(&buf);
1627 if (type_is_array(type))
1639 array = DatumGetArrayTypeP(value);
1640 elmtype = ARR_ELEMTYPE(array);
1641 get_typlenbyvalalign(elmtype, &elmlen, &elmbyval, &elmalign);
1643 deconstruct_array(array, elmtype,
1644 elmlen, elmbyval, elmalign,
1645 &elem_values, &elem_nulls,
1648 for (i = 0; i < num_elems; i++)
1652 appendStringInfoString(&buf, "<element>");
1653 appendStringInfoString(&buf,
1654 map_sql_value_to_xml_value(elem_values[i],
1656 appendStringInfoString(&buf, "</element>");
1669 * Special XSD formatting for some data types
1674 if (DatumGetBool(value))
1683 char buf[MAXDATELEN + 1];
1685 date = DatumGetDateADT(value);
1686 j2date(date + POSTGRES_EPOCH_JDATE,
1687 &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
1688 EncodeDateOnly(&tm, USE_XSD_DATES, buf);
1690 return pstrdup(buf);
1695 Timestamp timestamp;
1699 char buf[MAXDATELEN + 1];
1701 timestamp = DatumGetTimestamp(value);
1703 /* XSD doesn't support infinite values */
1704 if (TIMESTAMP_NOT_FINITE(timestamp))
1706 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1707 errmsg("timestamp out of range")));
1708 else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
1709 EncodeDateTime(&tm, fsec, NULL, &tzn, USE_XSD_DATES, buf);
1712 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1713 errmsg("timestamp out of range")));
1715 return pstrdup(buf);
1718 case TIMESTAMPTZOID:
1720 TimestampTz timestamp;
1725 char buf[MAXDATELEN + 1];
1727 timestamp = DatumGetTimestamp(value);
1729 /* XSD doesn't support infinite values */
1730 if (TIMESTAMP_NOT_FINITE(timestamp))
1732 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1733 errmsg("timestamp out of range")));
1734 else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
1735 EncodeDateTime(&tm, fsec, &tz, &tzn, USE_XSD_DATES, buf);
1738 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1739 errmsg("timestamp out of range")));
1741 return pstrdup(buf);
1745 getTypeOutputInfo(type, &typeOut, &isvarlena);
1746 str = OidOutputFunctionCall(typeOut, value);
1752 if (type == BYTEAOID)
1755 xmlTextWriterPtr writer;
1760 buf = xmlBufferCreate();
1761 writer = xmlNewTextWriterMemory(buf, 0);
1763 if (xmlbinary == XMLBINARY_BASE64)
1764 xmlTextWriterWriteBase64(writer, VARDATA(value), 0, VARSIZE(value) - VARHDRSZ);
1766 xmlTextWriterWriteBinHex(writer, VARDATA(value), 0, VARSIZE(value) - VARHDRSZ);
1768 xmlFreeTextWriter(writer);
1769 result = pstrdup((const char *) xmlBufferContent(buf));
1773 #endif /* USE_LIBXML */
1775 for (p = str; *p; p += pg_mblen(p))
1780 appendStringInfo(&buf, "&");
1783 appendStringInfo(&buf, "<");
1786 appendStringInfo(&buf, ">");
1789 appendStringInfo(&buf, "
");
1792 appendBinaryStringInfo(&buf, p, pg_mblen(p));
1803 _SPI_strdup(const char *s)
1805 char *ret = SPI_palloc(strlen(s) + 1);
1812 * SQL to XML mapping functions
1814 * What follows below is intentionally organized so that you can read
1815 * along in the SQL/XML:2003 standard. The functions are mostly split
1816 * up and ordered they way the clauses lay out in the standards
1817 * document, and the identifiers are also aligned with the standard
1818 * text. (SQL/XML:2006 appears to be ordered differently,
1821 * There are many things going on there:
1823 * There are two kinds of mappings: Mapping SQL data (table contents)
1824 * to XML documents, and mapping SQL structure (the "schema") to XML
1825 * Schema. And there are functions that do both at the same time.
1827 * Then you can map a database, a schema, or a table, each in both
1828 * ways. This breaks down recursively: Mapping a database invokes
1829 * mapping schemas, which invokes mapping tables, which invokes
1830 * mapping rows, which invokes mapping columns, although you can't
1831 * call the last two from the outside. Because of this, there are a
1832 * number of xyz_internal() functions which are to be called both from
1833 * the function manager wrapper and from some upper layer in a
1836 * See the documentation about what the common function arguments
1837 * nulls, tableforest, and targetns mean.
1839 * Some style guidelines for XML output: Use double quotes for quoting
1840 * XML attributes. Indent XML elements by two spaces, but remember
1841 * that a lot of code is called recursively at different levels, so
1842 * it's better not to indent rather than create output that indents
1843 * and outdents weirdly. Add newlines to make the output look nice.
1848 * Visibility of objects for XML mappings; see SQL/XML:2003 section
1853 * Given a query, which must return type oid as first column, produce
1854 * a list of Oids with the query results.
1857 query_to_oid_list(const char *query)
1862 SPI_execute(query, true, 0);
1864 for (i = 0; i < SPI_processed; i++)
1869 oid = SPI_getbinval(SPI_tuptable->vals[i],
1870 SPI_tuptable->tupdesc,
1874 list = lappend_oid(list, DatumGetObjectId(oid));
1882 schema_get_xml_visible_tables(Oid nspid)
1884 StringInfoData query;
1886 initStringInfo(&query);
1887 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);
1889 return query_to_oid_list(query.data);
1894 * Including the system schemas is probably not useful for a database
1897 #define XML_VISIBLE_SCHEMAS_EXCLUDE "(nspname ~ '^pg_' OR nspname = 'information_schema')"
1899 #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
1903 database_get_xml_visible_schemas(void)
1905 return query_to_oid_list(XML_VISIBLE_SCHEMAS " ORDER BY nspname;");
1910 database_get_xml_visible_tables(void)
1912 /* At the moment there is no order required here. */
1913 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 ");");
1918 * Map SQL table to XML and/or XML Schema document; see SQL/XML:2003
1923 table_to_xml_internal(Oid relid,
1924 const char *xmlschema, bool nulls, bool tableforest,
1925 const char *targetns, bool top_level)
1927 StringInfoData query;
1929 initStringInfo(&query);
1930 appendStringInfo(&query, "SELECT * FROM %s",
1931 DatumGetCString(DirectFunctionCall1(regclassout,
1932 ObjectIdGetDatum(relid))));
1933 return query_to_xml_internal(query.data, get_rel_name(relid),
1934 xmlschema, nulls, tableforest,
1935 targetns, top_level);
1940 table_to_xml(PG_FUNCTION_ARGS)
1942 Oid relid = PG_GETARG_OID(0);
1943 bool nulls = PG_GETARG_BOOL(1);
1944 bool tableforest = PG_GETARG_BOOL(2);
1945 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
1947 PG_RETURN_XML_P(stringinfo_to_xmltype(table_to_xml_internal(relid, NULL,
1954 query_to_xml(PG_FUNCTION_ARGS)
1956 char *query = _textout(PG_GETARG_TEXT_P(0));
1957 bool nulls = PG_GETARG_BOOL(1);
1958 bool tableforest = PG_GETARG_BOOL(2);
1959 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
1961 PG_RETURN_XML_P(stringinfo_to_xmltype(query_to_xml_internal(query, NULL,
1962 NULL, nulls, tableforest,
1968 cursor_to_xml(PG_FUNCTION_ARGS)
1970 char *name = _textout(PG_GETARG_TEXT_P(0));
1971 int32 count = PG_GETARG_INT32(1);
1972 bool nulls = PG_GETARG_BOOL(2);
1973 bool tableforest = PG_GETARG_BOOL(3);
1974 const char *targetns = _textout(PG_GETARG_TEXT_P(4));
1976 StringInfoData result;
1980 initStringInfo(&result);
1983 portal = SPI_cursor_find(name);
1986 (errcode(ERRCODE_UNDEFINED_CURSOR),
1987 errmsg("cursor \"%s\" does not exist", name)));
1989 SPI_cursor_fetch(portal, true, count);
1990 for (i = 0; i < SPI_processed; i++)
1991 SPI_sql_row_to_xmlelement(i, &result, NULL, nulls,
1992 tableforest, targetns, true);
1996 PG_RETURN_XML_P(stringinfo_to_xmltype(&result));
2001 * Write the start tag of the root element of a data mapping.
2003 * top_level means that this is the very top level of the eventual
2004 * output. For example, when the user calls table_to_xml, then a call
2005 * with a table name to this function is the top level. When the user
2006 * calls database_to_xml, then a call with a schema name to this
2007 * function is not the top level. If top_level is false, then the XML
2008 * namespace declarations are omitted, because they supposedly already
2009 * appeared earlier in the output. Repeating them is not wrong, but
2013 xmldata_root_element_start(StringInfo result, const char *eltname,
2014 const char *xmlschema, const char *targetns,
2017 /* This isn't really wrong but currently makes no sense. */
2018 Assert(top_level || !xmlschema);
2020 appendStringInfo(result, "<%s", eltname);
2023 appendStringInfoString(result, " xmlns:xsi=\"" NAMESPACE_XSI "\"");
2024 if (strlen(targetns) > 0)
2025 appendStringInfo(result, " xmlns=\"%s\"", targetns);
2029 /* FIXME: better targets */
2030 if (strlen(targetns) > 0)
2031 appendStringInfo(result, " xsi:schemaLocation=\"%s #\"", targetns);
2033 appendStringInfo(result, " xsi:noNamespaceSchemaLocation=\"#\"");
2035 appendStringInfo(result, ">\n\n");
2040 xmldata_root_element_end(StringInfo result, const char *eltname)
2042 appendStringInfo(result, "</%s>\n", eltname);
2047 query_to_xml_internal(const char *query, char *tablename,
2048 const char *xmlschema, bool nulls, bool tableforest,
2049 const char *targetns, bool top_level)
2056 xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
2060 result = makeStringInfo();
2063 if (SPI_execute(query, true, 0) != SPI_OK_SELECT)
2065 (errcode(ERRCODE_DATA_EXCEPTION),
2066 errmsg("invalid query")));
2069 xmldata_root_element_start(result, xmltn, xmlschema,
2070 targetns, top_level);
2073 appendStringInfo(result, "%s\n\n", xmlschema);
2075 for(i = 0; i < SPI_processed; i++)
2076 SPI_sql_row_to_xmlelement(i, result, tablename, nulls,
2077 tableforest, targetns, top_level);
2080 xmldata_root_element_end(result, xmltn);
2089 table_to_xmlschema(PG_FUNCTION_ARGS)
2091 Oid relid = PG_GETARG_OID(0);
2092 bool nulls = PG_GETARG_BOOL(1);
2093 bool tableforest = PG_GETARG_BOOL(2);
2094 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2098 rel = heap_open(relid, AccessShareLock);
2099 result = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2100 tableforest, targetns);
2101 heap_close(rel, NoLock);
2103 PG_RETURN_XML_P(cstring_to_xmltype(result));
2108 query_to_xmlschema(PG_FUNCTION_ARGS)
2110 char *query = _textout(PG_GETARG_TEXT_P(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));
2119 plan = SPI_prepare(query, 0, NULL);
2120 portal = SPI_cursor_open(NULL, plan, NULL, NULL, true);
2121 result = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2123 tableforest, targetns));
2124 SPI_cursor_close(portal);
2127 PG_RETURN_XML_P(cstring_to_xmltype(result));
2132 cursor_to_xmlschema(PG_FUNCTION_ARGS)
2134 char *name = _textout(PG_GETARG_TEXT_P(0));
2135 bool nulls = PG_GETARG_BOOL(1);
2136 bool tableforest = PG_GETARG_BOOL(2);
2137 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2138 const char *xmlschema;
2142 portal = SPI_cursor_find(name);
2145 (errcode(ERRCODE_UNDEFINED_CURSOR),
2146 errmsg("cursor \"%s\" does not exist", name)));
2148 xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2150 tableforest, targetns));
2153 PG_RETURN_XML_P(cstring_to_xmltype(xmlschema));
2158 table_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2160 Oid relid = PG_GETARG_OID(0);
2161 bool nulls = PG_GETARG_BOOL(1);
2162 bool tableforest = PG_GETARG_BOOL(2);
2163 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2165 const char *xmlschema;
2167 rel = heap_open(relid, AccessShareLock);
2168 xmlschema = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2169 tableforest, targetns);
2170 heap_close(rel, NoLock);
2172 PG_RETURN_XML_P(stringinfo_to_xmltype(table_to_xml_internal(relid,
2173 xmlschema, nulls, tableforest,
2179 query_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2181 char *query = _textout(PG_GETARG_TEXT_P(0));
2182 bool nulls = PG_GETARG_BOOL(1);
2183 bool tableforest = PG_GETARG_BOOL(2);
2184 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2186 const char *xmlschema;
2191 plan = SPI_prepare(query, 0, NULL);
2192 portal = SPI_cursor_open(NULL, plan, NULL, NULL, true);
2193 xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2194 InvalidOid, nulls, tableforest, targetns));
2195 SPI_cursor_close(portal);
2198 PG_RETURN_XML_P(stringinfo_to_xmltype(query_to_xml_internal(query, NULL,
2199 xmlschema, nulls, tableforest,
2205 * Map SQL schema to XML and/or XML Schema document; see SQL/XML:2003
2210 schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls,
2211 bool tableforest, const char *targetns, bool top_level)
2218 xmlsn = map_sql_identifier_to_xml_name(get_namespace_name(nspid),
2220 result = makeStringInfo();
2222 xmldata_root_element_start(result, xmlsn, xmlschema, targetns, top_level);
2225 appendStringInfo(result, "%s\n\n", xmlschema);
2229 relid_list = schema_get_xml_visible_tables(nspid);
2233 foreach(cell, relid_list)
2235 Oid relid = lfirst_oid(cell);
2238 subres = table_to_xml_internal(relid, NULL, nulls, tableforest,
2241 appendStringInfoString(result, subres->data);
2242 appendStringInfoChar(result, '\n');
2248 xmldata_root_element_end(result, xmlsn);
2255 schema_to_xml(PG_FUNCTION_ARGS)
2257 Name name = PG_GETARG_NAME(0);
2258 bool nulls = PG_GETARG_BOOL(1);
2259 bool tableforest = PG_GETARG_BOOL(2);
2260 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2265 schemaname = NameStr(*name);
2266 nspid = LookupExplicitNamespace(schemaname);
2268 PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xml_internal(nspid, NULL,
2269 nulls, tableforest, targetns, true)));
2274 * Write the start element of the root element of an XML Schema mapping.
2277 xsd_schema_element_start(StringInfo result, const char *targetns)
2279 appendStringInfoString(result,
2281 " xmlns:xsd=\"" NAMESPACE_XSD "\"");
2282 if (strlen(targetns) > 0)
2283 appendStringInfo(result,
2285 " targetNamespace=\"%s\"\n"
2286 " elementFormDefault=\"qualified\"",
2288 appendStringInfoString(result,
2294 xsd_schema_element_end(StringInfo result)
2296 appendStringInfoString(result, "</xsd:schema>");
2301 schema_to_xmlschema_internal(const char *schemaname, bool nulls,
2302 bool tableforest, const char *targetns)
2310 result = makeStringInfo();
2312 nspid = LookupExplicitNamespace(schemaname);
2314 xsd_schema_element_start(result, targetns);
2318 relid_list = schema_get_xml_visible_tables(nspid);
2321 foreach (cell, relid_list)
2325 rel = heap_open(lfirst_oid(cell), AccessShareLock);
2326 tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2327 heap_close(rel, NoLock);
2330 appendStringInfoString(result,
2331 map_sql_typecoll_to_xmlschema_types(tupdesc_list));
2333 appendStringInfoString(result,
2334 map_sql_schema_to_xmlschema_types(nspid, relid_list,
2335 nulls, tableforest, targetns));
2337 xsd_schema_element_end(result);
2346 schema_to_xmlschema(PG_FUNCTION_ARGS)
2348 Name name = PG_GETARG_NAME(0);
2349 bool nulls = PG_GETARG_BOOL(1);
2350 bool tableforest = PG_GETARG_BOOL(2);
2351 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2353 PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xmlschema_internal(NameStr(*name),
2354 nulls, tableforest, targetns)));
2359 schema_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2361 Name name = PG_GETARG_NAME(0);
2362 bool nulls = PG_GETARG_BOOL(1);
2363 bool tableforest = PG_GETARG_BOOL(2);
2364 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2367 StringInfo xmlschema;
2369 schemaname = NameStr(*name);
2370 nspid = LookupExplicitNamespace(schemaname);
2372 xmlschema = schema_to_xmlschema_internal(schemaname, nulls,
2373 tableforest, targetns);
2375 PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xml_internal(nspid,
2376 xmlschema->data, nulls,
2377 tableforest, targetns, true)));
2382 * Map SQL database to XML and/or XML Schema document; see SQL/XML:2003
2387 database_to_xml_internal(const char *xmlschema, bool nulls,
2388 bool tableforest, const char *targetns)
2395 xmlcn = map_sql_identifier_to_xml_name(get_database_name(MyDatabaseId),
2397 result = makeStringInfo();
2399 xmldata_root_element_start(result, xmlcn, xmlschema, targetns, true);
2402 appendStringInfo(result, "%s\n\n", xmlschema);
2406 nspid_list = database_get_xml_visible_schemas();
2410 foreach(cell, nspid_list)
2412 Oid nspid = lfirst_oid(cell);
2415 subres = schema_to_xml_internal(nspid, NULL, nulls,
2416 tableforest, targetns, false);
2418 appendStringInfoString(result, subres->data);
2419 appendStringInfoChar(result, '\n');
2425 xmldata_root_element_end(result, xmlcn);
2432 database_to_xml(PG_FUNCTION_ARGS)
2434 bool nulls = PG_GETARG_BOOL(0);
2435 bool tableforest = PG_GETARG_BOOL(1);
2436 const char *targetns = _textout(PG_GETARG_TEXT_P(2));
2438 PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xml_internal(NULL, nulls,
2439 tableforest, targetns)));
2444 database_to_xmlschema_internal(bool nulls, bool tableforest,
2445 const char *targetns)
2453 result = makeStringInfo();
2455 xsd_schema_element_start(result, targetns);
2459 relid_list = database_get_xml_visible_tables();
2460 nspid_list = database_get_xml_visible_schemas();
2463 foreach (cell, relid_list)
2467 rel = heap_open(lfirst_oid(cell), AccessShareLock);
2468 tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2469 heap_close(rel, NoLock);
2472 appendStringInfoString(result,
2473 map_sql_typecoll_to_xmlschema_types(tupdesc_list));
2475 appendStringInfoString(result,
2476 map_sql_catalog_to_xmlschema_types(nspid_list, nulls, tableforest, targetns));
2478 xsd_schema_element_end(result);
2487 database_to_xmlschema(PG_FUNCTION_ARGS)
2489 bool nulls = PG_GETARG_BOOL(0);
2490 bool tableforest = PG_GETARG_BOOL(1);
2491 const char *targetns = _textout(PG_GETARG_TEXT_P(2));
2493 PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xmlschema_internal(nulls,
2494 tableforest, targetns)));
2499 database_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2501 bool nulls = PG_GETARG_BOOL(0);
2502 bool tableforest = PG_GETARG_BOOL(1);
2503 const char *targetns = _textout(PG_GETARG_TEXT_P(2));
2504 StringInfo xmlschema;
2506 xmlschema = database_to_xmlschema_internal(nulls, tableforest, targetns);
2508 PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xml_internal(xmlschema->data,
2509 nulls, tableforest, targetns)));
2514 * Map a multi-part SQL name to an XML name; see SQL/XML:2003 section
2518 map_multipart_sql_identifier_to_xml_name(char *a, char *b, char *c, char *d)
2520 StringInfoData result;
2522 initStringInfo(&result);
2525 appendStringInfo(&result, "%s",
2526 map_sql_identifier_to_xml_name(a, true, true));
2528 appendStringInfo(&result, ".%s",
2529 map_sql_identifier_to_xml_name(b, true, true));
2531 appendStringInfo(&result, ".%s",
2532 map_sql_identifier_to_xml_name(c, true, true));
2534 appendStringInfo(&result, ".%s",
2535 map_sql_identifier_to_xml_name(d, true, true));
2542 * Map an SQL table to an XML Schema document; see SQL/XML:2003
2545 * Map an SQL table to XML Schema data types; see SQL/XML:2003 section
2549 map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls,
2550 bool tableforest, const char *targetns)
2554 char *tabletypename;
2556 StringInfoData result;
2558 initStringInfo(&result);
2560 if (OidIsValid(relid))
2563 Form_pg_class reltuple;
2565 tuple = SearchSysCache(RELOID,
2566 ObjectIdGetDatum(relid),
2568 if (!HeapTupleIsValid(tuple))
2569 elog(ERROR, "cache lookup failed for relation %u", relid);
2570 reltuple = (Form_pg_class) GETSTRUCT(tuple);
2572 xmltn = map_sql_identifier_to_xml_name(NameStr(reltuple->relname),
2575 tabletypename = map_multipart_sql_identifier_to_xml_name("TableType",
2576 get_database_name(MyDatabaseId),
2577 get_namespace_name(reltuple->relnamespace),
2578 NameStr(reltuple->relname));
2580 rowtypename = map_multipart_sql_identifier_to_xml_name("RowType",
2581 get_database_name(MyDatabaseId),
2582 get_namespace_name(reltuple->relnamespace),
2583 NameStr(reltuple->relname));
2585 ReleaseSysCache(tuple);
2594 tabletypename = "TableType";
2595 rowtypename = "RowType";
2598 xsd_schema_element_start(&result, targetns);
2600 appendStringInfoString(&result,
2601 map_sql_typecoll_to_xmlschema_types(list_make1(tupdesc)));
2603 appendStringInfo(&result,
2604 "<xsd:complexType name=\"%s\">\n"
2605 " <xsd:sequence>\n",
2608 for (i = 0; i < tupdesc->natts; i++)
2609 appendStringInfo(&result,
2610 " <xsd:element name=\"%s\" type=\"%s\"%s></xsd:element>\n",
2611 map_sql_identifier_to_xml_name(NameStr(tupdesc->attrs[i]->attname),
2613 map_sql_type_to_xml_name(tupdesc->attrs[i]->atttypid, -1),
2614 nulls ? " nillable=\"true\"" : " minOccurs=\"0\"");
2616 appendStringInfoString(&result,
2617 " </xsd:sequence>\n"
2618 "</xsd:complexType>\n\n");
2622 appendStringInfo(&result,
2623 "<xsd:complexType name=\"%s\">\n"
2625 " <xsd:element name=\"row\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n"
2626 " </xsd:sequence>\n"
2627 "</xsd:complexType>\n\n",
2628 tabletypename, rowtypename);
2630 appendStringInfo(&result,
2631 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2632 xmltn, tabletypename);
2635 appendStringInfo(&result,
2636 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2637 xmltn, rowtypename);
2639 xsd_schema_element_end(&result);
2646 * Map an SQL schema to XML Schema data types; see SQL/XML section
2650 map_sql_schema_to_xmlschema_types(Oid nspid, List *relid_list, bool nulls,
2651 bool tableforest, const char *targetns)
2656 char *schematypename;
2657 StringInfoData result;
2660 dbname = get_database_name(MyDatabaseId);
2661 nspname = get_namespace_name(nspid);
2663 initStringInfo(&result);
2665 xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
2667 schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
2672 appendStringInfo(&result,
2673 "<xsd:complexType name=\"%s\">\n", schematypename);
2675 appendStringInfoString(&result,
2678 appendStringInfoString(&result,
2679 " <xsd:sequence>\n");
2681 foreach (cell, relid_list)
2683 Oid relid = lfirst_oid(cell);
2684 char *relname = get_rel_name(relid);
2685 char *xmltn = map_sql_identifier_to_xml_name(relname, true, false);
2686 char *tabletypename = map_multipart_sql_identifier_to_xml_name(tableforest ? "RowType" : "TableType",
2692 appendStringInfo(&result,
2693 " <xsd:element name=\"%s\" type=\"%s\" />\n",
2694 xmltn, tabletypename);
2696 appendStringInfo(&result,
2697 " <xsd:element name=\"%s\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\" />\n",
2698 xmltn, tabletypename);
2702 appendStringInfoString(&result,
2705 appendStringInfoString(&result,
2706 " </xsd:sequence>\n");
2707 appendStringInfoString(&result,
2708 "</xsd:complexType>\n\n");
2710 appendStringInfo(&result,
2711 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2712 xmlsn, schematypename);
2719 * Map an SQL catalog to XML Schema data types; see SQL/XML section
2723 map_sql_catalog_to_xmlschema_types(List *nspid_list, bool nulls,
2724 bool tableforest, const char *targetns)
2728 char *catalogtypename;
2729 StringInfoData result;
2732 dbname = get_database_name(MyDatabaseId);
2734 initStringInfo(&result);
2736 xmlcn = map_sql_identifier_to_xml_name(dbname, true, false);
2738 catalogtypename = map_multipart_sql_identifier_to_xml_name("CatalogType",
2743 appendStringInfo(&result,
2744 "<xsd:complexType name=\"%s\">\n", catalogtypename);
2745 appendStringInfoString(&result,
2748 foreach (cell, nspid_list)
2750 Oid nspid = lfirst_oid(cell);
2751 char *nspname = get_namespace_name(nspid);
2752 char *xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
2753 char *schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
2758 appendStringInfo(&result,
2759 " <xsd:element name=\"%s\" type=\"%s\" />\n",
2760 xmlsn, schematypename);
2763 appendStringInfoString(&result,
2765 appendStringInfoString(&result,
2766 "</xsd:complexType>\n\n");
2768 appendStringInfo(&result,
2769 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2770 xmlcn, catalogtypename);
2777 * Map an SQL data type to an XML name; see SQL/XML:2003 section 9.9.
2780 map_sql_type_to_xml_name(Oid typeoid, int typmod)
2782 StringInfoData result;
2784 initStringInfo(&result);
2790 appendStringInfo(&result, "CHAR");
2792 appendStringInfo(&result, "CHAR_%d", typmod - VARHDRSZ);
2796 appendStringInfo(&result, "VARCHAR");
2798 appendStringInfo(&result, "VARCHAR_%d", typmod - VARHDRSZ);
2802 appendStringInfo(&result, "NUMERIC");
2804 appendStringInfo(&result, "NUMERIC_%d_%d",
2805 ((typmod - VARHDRSZ) >> 16) & 0xffff,
2806 (typmod - VARHDRSZ) & 0xffff);
2809 appendStringInfo(&result, "INTEGER");
2812 appendStringInfo(&result, "SMALLINT");
2815 appendStringInfo(&result, "BIGINT");
2818 appendStringInfo(&result, "REAL");
2821 appendStringInfo(&result, "DOUBLE");
2824 appendStringInfo(&result, "BOOLEAN");
2828 appendStringInfo(&result, "TIME");
2830 appendStringInfo(&result, "TIME_%d", typmod);
2834 appendStringInfo(&result, "TIME_WTZ");
2836 appendStringInfo(&result, "TIME_WTZ_%d", typmod);
2840 appendStringInfo(&result, "TIMESTAMP");
2842 appendStringInfo(&result, "TIMESTAMP_%d", typmod);
2844 case TIMESTAMPTZOID:
2846 appendStringInfo(&result, "TIMESTAMP_WTZ");
2848 appendStringInfo(&result, "TIMESTAMP_WTZ_%d", typmod);
2851 appendStringInfo(&result, "DATE");
2854 appendStringInfo(&result, "XML");
2859 Form_pg_type typtuple;
2861 tuple = SearchSysCache(TYPEOID,
2862 ObjectIdGetDatum(typeoid),
2864 if (!HeapTupleIsValid(tuple))
2865 elog(ERROR, "cache lookup failed for type %u", typeoid);
2866 typtuple = (Form_pg_type) GETSTRUCT(tuple);
2868 appendStringInfoString(&result,
2869 map_multipart_sql_identifier_to_xml_name((typtuple->typtype == TYPTYPE_DOMAIN) ? "Domain" : "UDT",
2870 get_database_name(MyDatabaseId),
2871 get_namespace_name(typtuple->typnamespace),
2872 NameStr(typtuple->typname)));
2874 ReleaseSysCache(tuple);
2883 * Map a collection of SQL data types to XML Schema data types; see
2884 * SQL/XML:2002 section 9.10.
2887 map_sql_typecoll_to_xmlschema_types(List *tupdesc_list)
2889 List *uniquetypes = NIL;
2891 StringInfoData result;
2894 /* extract all column types used in the set of TupleDescs */
2895 foreach(cell0, tupdesc_list)
2897 TupleDesc tupdesc = (TupleDesc) lfirst(cell0);
2899 for (i = 0; i < tupdesc->natts; i++)
2901 if (tupdesc->attrs[i]->attisdropped)
2903 uniquetypes = list_append_unique_oid(uniquetypes,
2904 tupdesc->attrs[i]->atttypid);
2908 /* add base types of domains */
2909 foreach(cell0, uniquetypes)
2911 Oid typid = lfirst_oid(cell0);
2912 Oid basetypid = getBaseType(typid);
2914 if (basetypid != typid)
2915 uniquetypes = list_append_unique_oid(uniquetypes, basetypid);
2918 /* Convert to textual form */
2919 initStringInfo(&result);
2921 foreach(cell0, uniquetypes)
2923 appendStringInfo(&result, "%s\n",
2924 map_sql_type_to_xmlschema_type(lfirst_oid(cell0),
2933 * Map an SQL data type to a named XML Schema data type; see SQL/XML
2934 * sections 9.11 and 9.15.
2936 * (The distinction between 9.11 and 9.15 is basically that 9.15 adds
2937 * a name attribute, which this function does. The name-less version
2938 * 9.11 doesn't appear to be required anywhere.)
2941 map_sql_type_to_xmlschema_type(Oid typeoid, int typmod)
2943 StringInfoData result;
2944 const char *typename = map_sql_type_to_xml_name(typeoid, typmod);
2946 initStringInfo(&result);
2948 if (typeoid == XMLOID)
2950 appendStringInfo(&result,
2951 "<xsd:complexType mixed=\"true\">\n"
2953 " <xsd:any name=\"element\" minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>\n"
2954 " </xsd:sequence>\n"
2955 "</xsd:complexType>\n");
2959 appendStringInfo(&result,
2960 "<xsd:simpleType name=\"%s\">\n", typename);
2968 appendStringInfo(&result,
2969 " <xsd:restriction base=\"xsd:string\">\n"
2970 " <xsd:maxLength value=\"%d\"/>\n"
2971 " </xsd:restriction>\n",
2976 appendStringInfo(&result,
2977 " <xsd:restriction base=\"xsd:%s\">\n"
2978 " </xsd:restriction>\n",
2979 xmlbinary == XMLBINARY_BASE64 ? "base64Binary" : "hexBinary");
2983 appendStringInfo(&result,
2984 " <xsd:restriction base=\"xsd:decimal\">\n"
2985 " <xsd:totalDigits value=\"%d\"/>\n"
2986 " <xsd:fractionDigits value=\"%d\"/>\n"
2987 " </xsd:restriction>\n",
2988 ((typmod - VARHDRSZ) >> 16) & 0xffff,
2989 (typmod - VARHDRSZ) & 0xffff);
2993 appendStringInfo(&result,
2994 " <xsd:restriction base=\"xsd:short\">\n"
2995 " <xsd:maxInclusive value=\"%d\"/>\n"
2996 " <xsd:minInclusive value=\"%d\"/>\n"
2997 " </xsd:restriction>\n",
2998 SHRT_MAX, SHRT_MIN);
3002 appendStringInfo(&result,
3003 " <xsd:restriction base='xsd:int'>\n"
3004 " <xsd:maxInclusive value=\"%d\"/>\n"
3005 " <xsd:minInclusive value=\"%d\"/>\n"
3006 " </xsd:restriction>\n",
3011 appendStringInfo(&result,
3012 " <xsd:restriction base=\"xsd:long\">\n"
3013 " <xsd:maxInclusive value=\"" INT64_FORMAT "\"/>\n"
3014 " <xsd:minInclusive value=\"" INT64_FORMAT "\"/>\n"
3015 " </xsd:restriction>\n",
3016 (((uint64) 1) << (sizeof(int64) * 8 - 1)) - 1,
3017 (((uint64) 1) << (sizeof(int64) * 8 - 1)));
3021 appendStringInfo(&result,
3022 " <xsd:restriction base=\"xsd:float\"></xsd:restriction>\n");
3026 appendStringInfo(&result,
3027 " <xsd:restriction base=\"xsd:double\"></xsd:restriction>\n");
3031 appendStringInfo(&result,
3032 " <xsd:restriction base=\"xsd:boolean\"></xsd:restriction>\n");
3038 const char *tz = (typeoid == TIMETZOID ? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
3041 appendStringInfo(&result,
3042 " <xsd:restriction base=\"xsd:time\">\n"
3043 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}(.\\p{Nd}+)?%s\"/>\n"
3044 " </xsd:restriction>\n", tz);
3045 else if (typmod == 0)
3046 appendStringInfo(&result,
3047 " <xsd:restriction base=\"xsd:time\">\n"
3048 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3049 " </xsd:restriction>\n", tz);
3051 appendStringInfo(&result,
3052 " <xsd:restriction base=\"xsd:time\">\n"
3053 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}.\\p{Nd}{%d}%s\"/>\n"
3054 " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
3059 case TIMESTAMPTZOID:
3061 const char *tz = (typeoid == TIMESTAMPTZOID ? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
3064 appendStringInfo(&result,
3065 " <xsd:restriction base=\"xsd:dateTime\">\n"
3066 " <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"
3067 " </xsd:restriction>\n", tz);
3068 else if (typmod == 0)
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}%s\"/>\n"
3072 " </xsd:restriction>\n", tz);
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}.\\p{Nd}{%d}%s\"/>\n"
3077 " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
3082 appendStringInfo(&result,
3083 " <xsd:restriction base=\"xsd:date\">\n"
3084 " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}\"/>\n"
3085 " </xsd:restriction>\n");
3089 if (get_typtype(typeoid) == TYPTYPE_DOMAIN)
3092 int32 base_typmod = -1;
3094 base_typeoid = getBaseTypeAndTypmod(typeoid, &base_typmod);
3096 appendStringInfo(&result,
3097 " <xsd:restriction base=\"%s\">\n",
3098 map_sql_type_to_xml_name(base_typeoid, base_typmod));
3102 appendStringInfo(&result,
3103 "</xsd:simpleType>\n");
3111 * Map an SQL row to an XML element, taking the row from the active
3112 * SPI cursor. See also SQL/XML:2003 section 9.12.
3115 SPI_sql_row_to_xmlelement(int rownum, StringInfo result, char *tablename,
3116 bool nulls, bool tableforest,
3117 const char *targetns, bool top_level)
3123 xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
3133 xmldata_root_element_start(result, xmltn, NULL, targetns, top_level);
3135 appendStringInfoString(result, "<row>\n");
3137 for(i = 1; i <= SPI_tuptable->tupdesc->natts; i++)
3143 colname = map_sql_identifier_to_xml_name(SPI_fname(SPI_tuptable->tupdesc, i),
3145 colval = SPI_getbinval(SPI_tuptable->vals[rownum],
3146 SPI_tuptable->tupdesc,
3152 appendStringInfo(result, " <%s xsi:nil='true'/>\n", colname);
3155 appendStringInfo(result, " <%s>%s</%s>\n",
3157 map_sql_value_to_xml_value(colval,
3158 SPI_gettypeid(SPI_tuptable->tupdesc, i)),
3164 xmldata_root_element_end(result, xmltn);
3165 appendStringInfoChar(result, '\n');
3168 appendStringInfoString(result, "</row>\n\n");
3173 * XPath related functions
3178 * Convert XML node to text (dump subtree in case of element,
3179 * return value otherwise)
3182 xml_xmlnodetoxmltype(xmlNodePtr cur)
3189 if (cur->type == XML_ELEMENT_NODE)
3191 buf = xmlBufferCreate();
3192 xmlNodeDump(buf, NULL, cur, 0, 1);
3193 result = xmlBuffer_to_xmltype(buf);
3198 str = xmlXPathCastNodeToString(cur);
3199 len = strlen((char *) str);
3200 result = (text *) palloc(len + VARHDRSZ);
3201 SET_VARSIZE(result, len + VARHDRSZ);
3202 memcpy(VARDATA(result), str, len);
3211 * Evaluate XPath expression and return array of XML values.
3213 * As we have no support of XQuery sequences yet, this function seems
3214 * to be the most useful one (array of XML functions plays a role of
3215 * some kind of substitution for XQuery sequences).
3217 * Workaround here: we parse XML data in different way to allow XPath for
3218 * fragments (see "XPath for fragment" TODO comment inside).
3221 xpath(PG_FUNCTION_ARGS)
3224 text *xpath_expr_text = PG_GETARG_TEXT_P(0);
3225 xmltype *data = PG_GETARG_XML_P(1);
3226 ArrayType *namespaces = PG_GETARG_ARRAYTYPE_P(2);
3227 ArrayBuildState *astate = NULL;
3228 xmlParserCtxtPtr ctxt = NULL;
3229 xmlDocPtr doc = NULL;
3230 xmlXPathContextPtr xpathctx = NULL;
3231 xmlXPathCompExprPtr xpathcomp = NULL;
3232 xmlXPathObjectPtr xpathobj = NULL;
3237 xmlChar *xpath_expr;
3241 Datum *ns_names_uris;
3242 bool *ns_names_uris_nulls;
3246 * Namespace mappings are passed as text[]. If an empty array is
3247 * passed (ndim = 0, "0-dimensional"), then there are no namespace
3248 * mappings. Else, a 2-dimensional array with length of the
3249 * second axis being equal to 2 should be passed, i.e., every
3250 * subarray contains 2 elements, the first element defining the
3251 * name, the second one the URI. Example: ARRAY[ARRAY['myns',
3252 * 'http://example.com'], ARRAY['myns2', 'http://example2.com']].
3254 ndim = ARR_NDIM(namespaces);
3259 dims = ARR_DIMS(namespaces);
3261 if (ndim != 2 || dims[1] != 2)
3263 (errcode(ERRCODE_DATA_EXCEPTION),
3264 errmsg("invalid array for XML namespace mapping"),
3265 errdetail("The array must be two-dimensional with length of the second axis equal to 2.")));
3267 Assert(ARR_ELEMTYPE(namespaces) == TEXTOID);
3269 deconstruct_array(namespaces, TEXTOID, -1, false, 'i',
3270 &ns_names_uris, &ns_names_uris_nulls,
3273 Assert((ns_count % 2) == 0); /* checked above */
3274 ns_count /= 2; /* count pairs only */
3278 ns_names_uris = NULL;
3279 ns_names_uris_nulls = NULL;
3283 datastr = VARDATA(data);
3284 len = VARSIZE(data) - VARHDRSZ;
3285 xpath_len = VARSIZE(xpath_expr_text) - VARHDRSZ;
3288 (errcode(ERRCODE_DATA_EXCEPTION),
3289 errmsg("empty XPath expression")));
3292 * To handle both documents and fragments, regardless of the fact
3293 * whether the XML datum has a single root (XML well-formedness),
3294 * we wrap the XML datum in a dummy element (<x>...</x>) and
3295 * extend the XPath expression accordingly. To do it, throw away
3296 * the XML prolog, if any.
3299 xmlStrncmp((xmlChar *) datastr, (xmlChar *) "<?xml", 5) == 0)
3303 !(datastr[i - 1] == '?' && datastr[i] == '>'))
3307 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3308 "could not parse XML data");
3311 string = xmlStrncatNew((xmlChar *) "<x>",
3312 (xmlChar *) datastr + i, len - i);
3315 string = xmlStrncatNew((xmlChar *) "<x>",
3316 (xmlChar *) datastr, len);
3318 string = xmlStrncat(string, (xmlChar *) "</x>", 5);
3320 xpath_expr = xmlStrncatNew((xmlChar *) "/x",
3321 (xmlChar *) VARDATA(xpath_expr_text), xpath_len);
3326 /* We use a PG_TRY block to ensure libxml parser is cleaned up on error */
3331 * redundant XML parsing (two parsings for the same value
3332 * during one command execution are possible)
3334 ctxt = xmlNewParserCtxt();
3336 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3337 "could not allocate parser context");
3338 doc = xmlCtxtReadMemory(ctxt, (char *) string, len, NULL, NULL, 0);
3340 xml_ereport(ERROR, ERRCODE_INVALID_XML_DOCUMENT,
3341 "could not parse XML data");
3342 xpathctx = xmlXPathNewContext(doc);
3343 if (xpathctx == NULL)
3344 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3345 "could not allocate XPath context");
3346 xpathctx->node = xmlDocGetRootElement(doc);
3347 if (xpathctx->node == NULL)
3348 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3349 "could not find root XML element");
3351 /* register namespaces, if any */
3354 for (i = 0; i < ns_count; i++)
3359 if (ns_names_uris_nulls[i * 2] ||
3360 ns_names_uris_nulls[i * 2 + 1])
3362 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3363 errmsg("neither namespace name nor URI may be null")));
3364 ns_name = _textout(ns_names_uris[i * 2]);
3365 ns_uri = _textout(ns_names_uris[i * 2 + 1]);
3366 if (xmlXPathRegisterNs(xpathctx,
3367 (xmlChar *) ns_name,
3368 (xmlChar *) ns_uri) != 0)
3369 ereport(ERROR, /* is this an internal error??? */
3370 (errmsg("could not register XML namespace with name \"%s\" and URI \"%s\"",
3375 xpathcomp = xmlXPathCompile(xpath_expr);
3376 if (xpathcomp == NULL) /* TODO: show proper XPath error details */
3377 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3378 "invalid XPath expression");
3380 xpathobj = xmlXPathCompiledEval(xpathcomp, xpathctx);
3381 if (xpathobj == NULL) /* TODO: reason? */
3383 (errmsg("could not create XPath object")));
3385 xmlXPathFreeCompExpr(xpathcomp);
3388 /* return empty array in cases when nothing is found */
3389 if (xpathobj->nodesetval == NULL)
3392 res_nitems = xpathobj->nodesetval->nodeNr;
3395 for (i = 0; i < xpathobj->nodesetval->nodeNr; i++)
3398 bool elemisnull = false;
3399 elem = PointerGetDatum(xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[i]));
3400 astate = accumArrayResult(astate, elem,
3402 CurrentMemoryContext);
3405 xmlXPathFreeObject(xpathobj);
3407 xmlXPathFreeContext(xpathctx);
3411 xmlFreeParserCtxt(ctxt);
3418 xmlXPathFreeCompExpr(xpathcomp);
3420 xmlXPathFreeObject(xpathobj);
3422 xmlXPathFreeContext(xpathctx);
3426 xmlFreeParserCtxt(ctxt);
3433 if (res_nitems == 0)
3434 PG_RETURN_ARRAYTYPE_P(construct_empty_array(XMLOID));
3436 PG_RETURN_ARRAYTYPE_P(makeArrayResult(astate, CurrentMemoryContext));