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.61 2007/11/27 12:21:05 petere 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 /* It's actually binary compatible. */
539 return (text *) data;
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 create
572 * the result. This avoids issues if one of the arguments involves a call
573 * to some other function or subsystem that wants to use libxml on its own
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_strcasecmp(target, "xml") == 0)
681 (errcode(ERRCODE_SYNTAX_ERROR), /* really */
682 errmsg("invalid XML processing instruction"),
683 errdetail("XML processing instruction target name cannot be \"%s\".", target)));
686 * Following the SQL standard, the null check comes after the syntax check
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 if
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 worrying
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)++
1000 /* Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender */
1001 /* Beware of multiple evaluations of argument! */
1002 #define PG_XMLISNAMECHAR(c) \
1003 (xmlIsBaseChar_ch(c) || xmlIsIdeographicQ(c) \
1004 || xmlIsDigit_ch(c) \
1005 || c == '.' || c == '-' || c == '_' || c == ':' \
1006 || xmlIsCombiningQ(c) \
1007 || xmlIsExtender_ch(c))
1010 parse_xml_decl(const xmlChar * str, size_t *lenp,
1011 xmlChar ** version, xmlChar ** encoding, int *standalone)
1014 const xmlChar *save_p;
1030 if (xmlStrncmp(p, (xmlChar *) "<?xml", 5) != 0)
1033 /* if next char is name char, it's a PI like <?xml-stylesheet ...?> */
1034 utf8len = strlen((const char *) (p + 5));
1035 utf8char = xmlGetUTF8Char(p + 5, &utf8len);
1036 if (PG_XMLISNAMECHAR(utf8char))
1044 if (xmlStrncmp(p, (xmlChar *) "version", 7) != 0)
1045 return XML_ERR_VERSION_MISSING;
1049 return XML_ERR_VERSION_MISSING;
1053 if (*p == '\'' || *p == '"')
1057 q = xmlStrchr(p + 1, *p);
1059 return XML_ERR_VERSION_MISSING;
1062 *version = xmlStrndup(p + 1, q - p - 1);
1066 return XML_ERR_VERSION_MISSING;
1071 if (xmlStrncmp(p, (xmlChar *) "encoding", 8) == 0)
1073 CHECK_XML_SPACE(save_p);
1077 return XML_ERR_MISSING_ENCODING;
1081 if (*p == '\'' || *p == '"')
1085 q = xmlStrchr(p + 1, *p);
1087 return XML_ERR_MISSING_ENCODING;
1090 *encoding = xmlStrndup(p + 1, q - p - 1);
1094 return XML_ERR_MISSING_ENCODING;
1104 if (xmlStrncmp(p, (xmlChar *) "standalone", 10) == 0)
1106 CHECK_XML_SPACE(save_p);
1110 return XML_ERR_STANDALONE_VALUE;
1113 if (xmlStrncmp(p, (xmlChar *) "'yes'", 5) == 0 ||
1114 xmlStrncmp(p, (xmlChar *) "\"yes\"", 5) == 0)
1119 else if (xmlStrncmp(p, (xmlChar *) "'no'", 4) == 0 ||
1120 xmlStrncmp(p, (xmlChar *) "\"no\"", 4) == 0)
1126 return XML_ERR_STANDALONE_VALUE;
1134 if (xmlStrncmp(p, (xmlChar *) "?>", 2) != 0)
1135 return XML_ERR_XMLDECL_NOT_FINISHED;
1141 for (p = str; p < str + len; p++)
1143 return XML_ERR_INVALID_CHAR;
1153 * Write an XML declaration. On output, we adjust the XML declaration
1154 * as follows. (These rules are the moral equivalent of the clause
1155 * "Serialization of an XML value" in the SQL standard.)
1157 * We try to avoid generating an XML declaration if possible. This is
1158 * so that you don't get trivial things like xml '<foo/>' resulting in
1159 * '<?xml version="1.0"?><foo/>', which would surely be annoying. We
1160 * must provide a declaration if the standalone property is specified
1161 * or if we include an encoding declaration. If we have a
1162 * declaration, we must specify a version (XML requires this).
1163 * Otherwise we only make a declaration if the version is not "1.0",
1164 * which is the default version specified in SQL:2003.
1167 print_xml_decl(StringInfo buf, const xmlChar * version,
1168 pg_enc encoding, int standalone)
1172 if ((version && strcmp((char *) version, PG_XML_DEFAULT_VERSION) != 0)
1173 || (encoding && encoding != PG_UTF8)
1174 || standalone != -1)
1176 appendStringInfoString(buf, "<?xml");
1179 appendStringInfo(buf, " version=\"%s\"", version);
1181 appendStringInfo(buf, " version=\"%s\"", PG_XML_DEFAULT_VERSION);
1183 if (encoding && encoding != PG_UTF8)
1186 * XXX might be useful to convert this to IANA names (ISO-8859-1
1187 * instead of LATIN1 etc.); needs field experience
1189 appendStringInfo(buf, " encoding=\"%s\"",
1190 pg_encoding_to_char(encoding));
1193 if (standalone == 1)
1194 appendStringInfoString(buf, " standalone=\"yes\"");
1195 else if (standalone == 0)
1196 appendStringInfoString(buf, " standalone=\"no\"");
1197 appendStringInfoString(buf, "?>");
1207 * Convert a C string to XML internal representation
1209 * TODO maybe, libxml2's xmlreader is better? (do not construct DOM,
1210 * yet do not use SAX - see xml_reader.c)
1213 xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace,
1218 xmlChar *utf8string;
1219 xmlParserCtxtPtr ctxt = NULL;
1220 xmlDocPtr doc = NULL;
1222 len = VARSIZE(data) - VARHDRSZ; /* will be useful later */
1223 string = xml_text2xmlChar(data);
1225 utf8string = pg_do_encoding_conversion(string,
1228 xmlChar_to_encoding(encoding) :
1229 GetDatabaseEncoding(),
1234 /* We use a PG_TRY block to ensure libxml parser is cleaned up on error */
1238 ctxt = xmlNewParserCtxt();
1240 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
1241 "could not allocate parser context");
1243 if (xmloption_arg == XMLOPTION_DOCUMENT)
1246 * Note, that here we try to apply DTD defaults
1247 * (XML_PARSE_DTDATTR) according to SQL/XML:10.16.7.d: 'Default
1248 * valies defined by internal DTD are applied'. As for external
1249 * DTDs, we try to support them too, (see SQL/XML:10.16.7.e)
1251 doc = xmlCtxtReadDoc(ctxt, utf8string,
1254 XML_PARSE_NOENT | XML_PARSE_DTDATTR
1255 | (preserve_whitespace ? 0 : XML_PARSE_NOBLANKS));
1257 xml_ereport(ERROR, ERRCODE_INVALID_XML_DOCUMENT,
1258 "invalid XML document");
1264 xmlChar *version = NULL;
1265 int standalone = -1;
1267 doc = xmlNewDoc(NULL);
1269 res_code = parse_xml_decl(utf8string, &count, &version, NULL, &standalone);
1271 xml_ereport_by_code(ERROR, ERRCODE_INVALID_XML_CONTENT,
1272 "invalid XML content: invalid XML declaration", res_code);
1274 res_code = xmlParseBalancedChunkMemory(doc, NULL, NULL, 0, utf8string + count, NULL);
1276 xml_ereport(ERROR, ERRCODE_INVALID_XML_CONTENT,
1277 "invalid XML content");
1279 doc->version = xmlStrdup(version);
1280 doc->encoding = xmlStrdup((xmlChar *) "UTF-8");
1281 doc->standalone = standalone;
1285 xmlFreeParserCtxt(ctxt);
1294 xmlFreeParserCtxt(ctxt);
1306 * xmlChar<->text convertions
1309 xml_text2xmlChar(text *in)
1311 int32 len = VARSIZE(in) - VARHDRSZ;
1314 res = palloc(len + 1);
1315 memcpy(res, VARDATA(in), len);
1323 * Wrappers for memory management functions
1326 xml_palloc(size_t size)
1328 return palloc(size);
1333 xml_repalloc(void *ptr, size_t size)
1335 return repalloc(ptr, size);
1340 xml_pfree(void *ptr)
1347 xml_pstrdup(const char *string)
1349 return pstrdup(string);
1354 * Wrapper for "ereport" function for XML-related errors. The "msg"
1355 * is the SQL-level message; some can be adopted from the SQL/XML
1356 * standard. This function adds libxml's native error messages, if
1360 xml_ereport(int level, int sqlcode, const char *msg)
1364 if (xml_err_buf->len > 0)
1366 detail = pstrdup(xml_err_buf->data);
1367 resetStringInfo(xml_err_buf);
1372 /* libxml error messages end in '\n'; get rid of it */
1377 len = strlen(detail);
1378 if (len > 0 && detail[len - 1] == '\n')
1379 detail[len - 1] = '\0';
1384 errdetail("%s", detail)));
1390 errmsg("%s", msg)));
1396 * Error handler for libxml error messages
1399 xml_errorHandler(void *ctxt, const char *msg,...)
1401 /* Append the formatted text to xml_err_buf */
1407 /* Try to format the data. */
1408 va_start(args, msg);
1409 success = appendStringInfoVA(xml_err_buf, msg, args);
1415 /* Double the buffer size and try again. */
1416 enlargeStringInfo(xml_err_buf, xml_err_buf->maxlen);
1422 * Wrapper for "ereport" function for XML-related errors. The "msg"
1423 * is the SQL-level message; some can be adopted from the SQL/XML
1424 * standard. This function uses "code" to create a textual detail
1425 * message. At the moment, we only need to cover those codes that we
1426 * may raise in this file.
1429 xml_ereport_by_code(int level, int sqlcode,
1430 const char *msg, int code)
1436 case XML_ERR_INVALID_CHAR:
1437 det = "Invalid character value";
1439 case XML_ERR_SPACE_REQUIRED:
1440 det = "Space required";
1442 case XML_ERR_STANDALONE_VALUE:
1443 det = "standalone accepts only 'yes' or 'no'";
1445 case XML_ERR_VERSION_MISSING:
1446 det = "Malformed declaration expecting version";
1448 case XML_ERR_MISSING_ENCODING:
1449 det = "Missing encoding in text declaration";
1451 case XML_ERR_XMLDECL_NOT_FINISHED:
1452 det = "Parsing XML declaration: '?>' expected";
1455 det = "Unrecognized libxml error code: %d";
1462 errdetail(det, code)));
1467 * Convert one char in the current server encoding to a Unicode codepoint.
1470 sqlchar_to_unicode(char *s)
1473 pg_wchar ret[2]; /* need space for trailing zero */
1475 utf8string = (char *) pg_do_encoding_conversion((unsigned char *) s,
1477 GetDatabaseEncoding(),
1480 pg_encoding_mb2wchar_with_len(PG_UTF8, utf8string, ret, pg_mblen(s));
1487 is_valid_xml_namefirst(pg_wchar c)
1489 /* (Letter | '_' | ':') */
1490 return (xmlIsBaseCharQ(c) || xmlIsIdeographicQ(c)
1491 || c == '_' || c == ':');
1496 is_valid_xml_namechar(pg_wchar c)
1498 /* Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender */
1499 return (xmlIsBaseCharQ(c) || xmlIsIdeographicQ(c)
1501 || c == '.' || c == '-' || c == '_' || c == ':'
1502 || xmlIsCombiningQ(c)
1503 || xmlIsExtenderQ(c));
1505 #endif /* USE_LIBXML */
1509 * Map SQL identifier to XML name; see SQL/XML:2003 section 9.1.
1512 map_sql_identifier_to_xml_name(char *ident, bool fully_escaped,
1520 * SQL/XML doesn't make use of this case anywhere, so it's probably a
1523 Assert(fully_escaped || !escape_period);
1525 initStringInfo(&buf);
1527 for (p = ident; *p; p += pg_mblen(p))
1529 if (*p == ':' && (p == ident || fully_escaped))
1530 appendStringInfo(&buf, "_x003A_");
1531 else if (*p == '_' && *(p + 1) == 'x')
1532 appendStringInfo(&buf, "_x005F_");
1533 else if (fully_escaped && p == ident &&
1534 pg_strncasecmp(p, "xml", 3) == 0)
1537 appendStringInfo(&buf, "_x0078_");
1539 appendStringInfo(&buf, "_x0058_");
1541 else if (escape_period && *p == '.')
1542 appendStringInfo(&buf, "_x002E_");
1545 pg_wchar u = sqlchar_to_unicode(p);
1548 ? !is_valid_xml_namefirst(u)
1549 : !is_valid_xml_namechar(u))
1550 appendStringInfo(&buf, "_x%04X_", (unsigned int) u);
1552 appendBinaryStringInfo(&buf, p, pg_mblen(p));
1557 #else /* not USE_LIBXML */
1560 #endif /* not USE_LIBXML */
1565 * Map a Unicode codepoint into the current server encoding.
1568 unicode_to_sqlchar(pg_wchar c)
1570 static unsigned char utf8string[5]; /* need trailing zero */
1576 else if (c <= 0x7FF)
1578 utf8string[0] = 0xC0 | ((c >> 6) & 0x1F);
1579 utf8string[1] = 0x80 | (c & 0x3F);
1581 else if (c <= 0xFFFF)
1583 utf8string[0] = 0xE0 | ((c >> 12) & 0x0F);
1584 utf8string[1] = 0x80 | ((c >> 6) & 0x3F);
1585 utf8string[2] = 0x80 | (c & 0x3F);
1589 utf8string[0] = 0xF0 | ((c >> 18) & 0x07);
1590 utf8string[1] = 0x80 | ((c >> 12) & 0x3F);
1591 utf8string[2] = 0x80 | ((c >> 6) & 0x3F);
1592 utf8string[3] = 0x80 | (c & 0x3F);
1595 return (char *) pg_do_encoding_conversion(utf8string,
1596 pg_mblen((char *) utf8string),
1598 GetDatabaseEncoding());
1603 * Map XML name to SQL identifier; see SQL/XML:2003 section 9.17.
1606 map_xml_name_to_sql_identifier(char *name)
1611 initStringInfo(&buf);
1613 for (p = name; *p; p += pg_mblen(p))
1615 if (*p == '_' && *(p + 1) == 'x'
1616 && isxdigit((unsigned char) *(p + 2))
1617 && isxdigit((unsigned char) *(p + 3))
1618 && isxdigit((unsigned char) *(p + 4))
1619 && isxdigit((unsigned char) *(p + 5))
1624 sscanf(p + 2, "%X", &u);
1625 appendStringInfoString(&buf, unicode_to_sqlchar(u));
1629 appendBinaryStringInfo(&buf, p, pg_mblen(p));
1636 * Map SQL value to XML value; see SQL/XML:2003 section 9.16.
1639 map_sql_value_to_xml_value(Datum value, Oid type)
1643 initStringInfo(&buf);
1645 if (type_is_array(type))
1657 array = DatumGetArrayTypeP(value);
1658 elmtype = ARR_ELEMTYPE(array);
1659 get_typlenbyvalalign(elmtype, &elmlen, &elmbyval, &elmalign);
1661 deconstruct_array(array, elmtype,
1662 elmlen, elmbyval, elmalign,
1663 &elem_values, &elem_nulls,
1666 for (i = 0; i < num_elems; i++)
1670 appendStringInfoString(&buf, "<element>");
1671 appendStringInfoString(&buf,
1672 map_sql_value_to_xml_value(elem_values[i],
1674 appendStringInfoString(&buf, "</element>");
1688 * Special XSD formatting for some data types
1693 if (DatumGetBool(value))
1702 char buf[MAXDATELEN + 1];
1704 date = DatumGetDateADT(value);
1705 j2date(date + POSTGRES_EPOCH_JDATE,
1706 &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
1707 EncodeDateOnly(&tm, USE_XSD_DATES, buf);
1709 return pstrdup(buf);
1714 Timestamp timestamp;
1718 char buf[MAXDATELEN + 1];
1720 timestamp = DatumGetTimestamp(value);
1722 /* XSD doesn't support infinite values */
1723 if (TIMESTAMP_NOT_FINITE(timestamp))
1725 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1726 errmsg("timestamp out of range")));
1727 else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
1728 EncodeDateTime(&tm, fsec, NULL, &tzn, USE_XSD_DATES, buf);
1731 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1732 errmsg("timestamp out of range")));
1734 return pstrdup(buf);
1737 case TIMESTAMPTZOID:
1739 TimestampTz timestamp;
1744 char buf[MAXDATELEN + 1];
1746 timestamp = DatumGetTimestamp(value);
1748 /* XSD doesn't support infinite values */
1749 if (TIMESTAMP_NOT_FINITE(timestamp))
1751 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1752 errmsg("timestamp out of range")));
1753 else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
1754 EncodeDateTime(&tm, fsec, &tz, &tzn, USE_XSD_DATES, buf);
1757 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1758 errmsg("timestamp out of range")));
1760 return pstrdup(buf);
1764 getTypeOutputInfo(type, &typeOut, &isvarlena);
1765 str = OidOutputFunctionCall(typeOut, value);
1771 if (type == BYTEAOID)
1774 xmlTextWriterPtr writer;
1779 buf = xmlBufferCreate();
1780 writer = xmlNewTextWriterMemory(buf, 0);
1782 if (xmlbinary == XMLBINARY_BASE64)
1783 xmlTextWriterWriteBase64(writer, VARDATA(value), 0, VARSIZE(value) - VARHDRSZ);
1785 xmlTextWriterWriteBinHex(writer, VARDATA(value), 0, VARSIZE(value) - VARHDRSZ);
1787 xmlFreeTextWriter(writer);
1788 result = pstrdup((const char *) xmlBufferContent(buf));
1792 #endif /* USE_LIBXML */
1794 for (p = str; *p; p += pg_mblen(p))
1799 appendStringInfo(&buf, "&");
1802 appendStringInfo(&buf, "<");
1805 appendStringInfo(&buf, ">");
1808 appendStringInfo(&buf, "
");
1811 appendBinaryStringInfo(&buf, p, pg_mblen(p));
1822 _SPI_strdup(const char *s)
1824 char *ret = SPI_palloc(strlen(s) + 1);
1832 * SQL to XML mapping functions
1834 * What follows below is intentionally organized so that you can read
1835 * along in the SQL/XML:2003 standard. The functions are mostly split
1836 * up and ordered they way the clauses lay out in the standards
1837 * document, and the identifiers are also aligned with the standard
1838 * text. (SQL/XML:2006 appears to be ordered differently,
1841 * There are many things going on there:
1843 * There are two kinds of mappings: Mapping SQL data (table contents)
1844 * to XML documents, and mapping SQL structure (the "schema") to XML
1845 * Schema. And there are functions that do both at the same time.
1847 * Then you can map a database, a schema, or a table, each in both
1848 * ways. This breaks down recursively: Mapping a database invokes
1849 * mapping schemas, which invokes mapping tables, which invokes
1850 * mapping rows, which invokes mapping columns, although you can't
1851 * call the last two from the outside. Because of this, there are a
1852 * number of xyz_internal() functions which are to be called both from
1853 * the function manager wrapper and from some upper layer in a
1856 * See the documentation about what the common function arguments
1857 * nulls, tableforest, and targetns mean.
1859 * Some style guidelines for XML output: Use double quotes for quoting
1860 * XML attributes. Indent XML elements by two spaces, but remember
1861 * that a lot of code is called recursively at different levels, so
1862 * it's better not to indent rather than create output that indents
1863 * and outdents weirdly. Add newlines to make the output look nice.
1868 * Visibility of objects for XML mappings; see SQL/XML:2003 section
1873 * Given a query, which must return type oid as first column, produce
1874 * a list of Oids with the query results.
1877 query_to_oid_list(const char *query)
1882 SPI_execute(query, true, 0);
1884 for (i = 0; i < SPI_processed; i++)
1889 oid = SPI_getbinval(SPI_tuptable->vals[i],
1890 SPI_tuptable->tupdesc,
1894 list = lappend_oid(list, DatumGetObjectId(oid));
1902 schema_get_xml_visible_tables(Oid nspid)
1904 StringInfoData query;
1906 initStringInfo(&query);
1907 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);
1909 return query_to_oid_list(query.data);
1914 * Including the system schemas is probably not useful for a database
1917 #define XML_VISIBLE_SCHEMAS_EXCLUDE "(nspname ~ '^pg_' OR nspname = 'information_schema')"
1919 #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
1923 database_get_xml_visible_schemas(void)
1925 return query_to_oid_list(XML_VISIBLE_SCHEMAS " ORDER BY nspname;");
1930 database_get_xml_visible_tables(void)
1932 /* At the moment there is no order required here. */
1933 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 ");");
1938 * Map SQL table to XML and/or XML Schema document; see SQL/XML:2003
1943 table_to_xml_internal(Oid relid,
1944 const char *xmlschema, bool nulls, bool tableforest,
1945 const char *targetns, bool top_level)
1947 StringInfoData query;
1949 initStringInfo(&query);
1950 appendStringInfo(&query, "SELECT * FROM %s",
1951 DatumGetCString(DirectFunctionCall1(regclassout,
1952 ObjectIdGetDatum(relid))));
1953 return query_to_xml_internal(query.data, get_rel_name(relid),
1954 xmlschema, nulls, tableforest,
1955 targetns, top_level);
1960 table_to_xml(PG_FUNCTION_ARGS)
1962 Oid relid = PG_GETARG_OID(0);
1963 bool nulls = PG_GETARG_BOOL(1);
1964 bool tableforest = PG_GETARG_BOOL(2);
1965 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
1967 PG_RETURN_XML_P(stringinfo_to_xmltype(table_to_xml_internal(relid, NULL,
1974 query_to_xml(PG_FUNCTION_ARGS)
1976 char *query = _textout(PG_GETARG_TEXT_P(0));
1977 bool nulls = PG_GETARG_BOOL(1);
1978 bool tableforest = PG_GETARG_BOOL(2);
1979 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
1981 PG_RETURN_XML_P(stringinfo_to_xmltype(query_to_xml_internal(query, NULL,
1982 NULL, nulls, tableforest,
1988 cursor_to_xml(PG_FUNCTION_ARGS)
1990 char *name = _textout(PG_GETARG_TEXT_P(0));
1991 int32 count = PG_GETARG_INT32(1);
1992 bool nulls = PG_GETARG_BOOL(2);
1993 bool tableforest = PG_GETARG_BOOL(3);
1994 const char *targetns = _textout(PG_GETARG_TEXT_P(4));
1996 StringInfoData result;
2000 initStringInfo(&result);
2003 portal = SPI_cursor_find(name);
2006 (errcode(ERRCODE_UNDEFINED_CURSOR),
2007 errmsg("cursor \"%s\" does not exist", name)));
2009 SPI_cursor_fetch(portal, true, count);
2010 for (i = 0; i < SPI_processed; i++)
2011 SPI_sql_row_to_xmlelement(i, &result, NULL, nulls,
2012 tableforest, targetns, true);
2016 PG_RETURN_XML_P(stringinfo_to_xmltype(&result));
2021 * Write the start tag of the root element of a data mapping.
2023 * top_level means that this is the very top level of the eventual
2024 * output. For example, when the user calls table_to_xml, then a call
2025 * with a table name to this function is the top level. When the user
2026 * calls database_to_xml, then a call with a schema name to this
2027 * function is not the top level. If top_level is false, then the XML
2028 * namespace declarations are omitted, because they supposedly already
2029 * appeared earlier in the output. Repeating them is not wrong, but
2033 xmldata_root_element_start(StringInfo result, const char *eltname,
2034 const char *xmlschema, const char *targetns,
2037 /* This isn't really wrong but currently makes no sense. */
2038 Assert(top_level || !xmlschema);
2040 appendStringInfo(result, "<%s", eltname);
2043 appendStringInfoString(result, " xmlns:xsi=\"" NAMESPACE_XSI "\"");
2044 if (strlen(targetns) > 0)
2045 appendStringInfo(result, " xmlns=\"%s\"", targetns);
2049 /* FIXME: better targets */
2050 if (strlen(targetns) > 0)
2051 appendStringInfo(result, " xsi:schemaLocation=\"%s #\"", targetns);
2053 appendStringInfo(result, " xsi:noNamespaceSchemaLocation=\"#\"");
2055 appendStringInfo(result, ">\n\n");
2060 xmldata_root_element_end(StringInfo result, const char *eltname)
2062 appendStringInfo(result, "</%s>\n", eltname);
2067 query_to_xml_internal(const char *query, char *tablename,
2068 const char *xmlschema, bool nulls, bool tableforest,
2069 const char *targetns, bool top_level)
2076 xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
2080 result = makeStringInfo();
2083 if (SPI_execute(query, true, 0) != SPI_OK_SELECT)
2085 (errcode(ERRCODE_DATA_EXCEPTION),
2086 errmsg("invalid query")));
2089 xmldata_root_element_start(result, xmltn, xmlschema,
2090 targetns, top_level);
2093 appendStringInfo(result, "%s\n\n", xmlschema);
2095 for (i = 0; i < SPI_processed; i++)
2096 SPI_sql_row_to_xmlelement(i, result, tablename, nulls,
2097 tableforest, targetns, top_level);
2100 xmldata_root_element_end(result, xmltn);
2109 table_to_xmlschema(PG_FUNCTION_ARGS)
2111 Oid relid = PG_GETARG_OID(0);
2112 bool nulls = PG_GETARG_BOOL(1);
2113 bool tableforest = PG_GETARG_BOOL(2);
2114 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2118 rel = heap_open(relid, AccessShareLock);
2119 result = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2120 tableforest, targetns);
2121 heap_close(rel, NoLock);
2123 PG_RETURN_XML_P(cstring_to_xmltype(result));
2128 query_to_xmlschema(PG_FUNCTION_ARGS)
2130 char *query = _textout(PG_GETARG_TEXT_P(0));
2131 bool nulls = PG_GETARG_BOOL(1);
2132 bool tableforest = PG_GETARG_BOOL(2);
2133 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2139 plan = SPI_prepare(query, 0, NULL);
2140 portal = SPI_cursor_open(NULL, plan, NULL, NULL, true);
2141 result = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2143 tableforest, targetns));
2144 SPI_cursor_close(portal);
2147 PG_RETURN_XML_P(cstring_to_xmltype(result));
2152 cursor_to_xmlschema(PG_FUNCTION_ARGS)
2154 char *name = _textout(PG_GETARG_TEXT_P(0));
2155 bool nulls = PG_GETARG_BOOL(1);
2156 bool tableforest = PG_GETARG_BOOL(2);
2157 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2158 const char *xmlschema;
2162 portal = SPI_cursor_find(name);
2165 (errcode(ERRCODE_UNDEFINED_CURSOR),
2166 errmsg("cursor \"%s\" does not exist", name)));
2168 xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2170 tableforest, targetns));
2173 PG_RETURN_XML_P(cstring_to_xmltype(xmlschema));
2178 table_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2180 Oid relid = PG_GETARG_OID(0);
2181 bool nulls = PG_GETARG_BOOL(1);
2182 bool tableforest = PG_GETARG_BOOL(2);
2183 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2185 const char *xmlschema;
2187 rel = heap_open(relid, AccessShareLock);
2188 xmlschema = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2189 tableforest, targetns);
2190 heap_close(rel, NoLock);
2192 PG_RETURN_XML_P(stringinfo_to_xmltype(table_to_xml_internal(relid,
2193 xmlschema, nulls, tableforest,
2199 query_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2201 char *query = _textout(PG_GETARG_TEXT_P(0));
2202 bool nulls = PG_GETARG_BOOL(1);
2203 bool tableforest = PG_GETARG_BOOL(2);
2204 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2206 const char *xmlschema;
2211 plan = SPI_prepare(query, 0, NULL);
2212 portal = SPI_cursor_open(NULL, plan, NULL, NULL, true);
2213 xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2214 InvalidOid, nulls, tableforest, targetns));
2215 SPI_cursor_close(portal);
2218 PG_RETURN_XML_P(stringinfo_to_xmltype(query_to_xml_internal(query, NULL,
2219 xmlschema, nulls, tableforest,
2225 * Map SQL schema to XML and/or XML Schema document; see SQL/XML:2003
2230 schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls,
2231 bool tableforest, const char *targetns, bool top_level)
2238 xmlsn = map_sql_identifier_to_xml_name(get_namespace_name(nspid),
2240 result = makeStringInfo();
2242 xmldata_root_element_start(result, xmlsn, xmlschema, targetns, top_level);
2245 appendStringInfo(result, "%s\n\n", xmlschema);
2249 relid_list = schema_get_xml_visible_tables(nspid);
2253 foreach(cell, relid_list)
2255 Oid relid = lfirst_oid(cell);
2258 subres = table_to_xml_internal(relid, NULL, nulls, tableforest,
2261 appendStringInfoString(result, subres->data);
2262 appendStringInfoChar(result, '\n');
2268 xmldata_root_element_end(result, xmlsn);
2275 schema_to_xml(PG_FUNCTION_ARGS)
2277 Name name = PG_GETARG_NAME(0);
2278 bool nulls = PG_GETARG_BOOL(1);
2279 bool tableforest = PG_GETARG_BOOL(2);
2280 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2285 schemaname = NameStr(*name);
2286 nspid = LookupExplicitNamespace(schemaname);
2288 PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xml_internal(nspid, NULL,
2289 nulls, tableforest, targetns, true)));
2294 * Write the start element of the root element of an XML Schema mapping.
2297 xsd_schema_element_start(StringInfo result, const char *targetns)
2299 appendStringInfoString(result,
2301 " xmlns:xsd=\"" NAMESPACE_XSD "\"");
2302 if (strlen(targetns) > 0)
2303 appendStringInfo(result,
2305 " targetNamespace=\"%s\"\n"
2306 " elementFormDefault=\"qualified\"",
2308 appendStringInfoString(result,
2314 xsd_schema_element_end(StringInfo result)
2316 appendStringInfoString(result, "</xsd:schema>");
2321 schema_to_xmlschema_internal(const char *schemaname, bool nulls,
2322 bool tableforest, const char *targetns)
2330 result = makeStringInfo();
2332 nspid = LookupExplicitNamespace(schemaname);
2334 xsd_schema_element_start(result, targetns);
2338 relid_list = schema_get_xml_visible_tables(nspid);
2341 foreach(cell, relid_list)
2345 rel = heap_open(lfirst_oid(cell), AccessShareLock);
2346 tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2347 heap_close(rel, NoLock);
2350 appendStringInfoString(result,
2351 map_sql_typecoll_to_xmlschema_types(tupdesc_list));
2353 appendStringInfoString(result,
2354 map_sql_schema_to_xmlschema_types(nspid, relid_list,
2355 nulls, tableforest, targetns));
2357 xsd_schema_element_end(result);
2366 schema_to_xmlschema(PG_FUNCTION_ARGS)
2368 Name name = PG_GETARG_NAME(0);
2369 bool nulls = PG_GETARG_BOOL(1);
2370 bool tableforest = PG_GETARG_BOOL(2);
2371 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2373 PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xmlschema_internal(NameStr(*name),
2374 nulls, tableforest, targetns)));
2379 schema_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2381 Name name = PG_GETARG_NAME(0);
2382 bool nulls = PG_GETARG_BOOL(1);
2383 bool tableforest = PG_GETARG_BOOL(2);
2384 const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2387 StringInfo xmlschema;
2389 schemaname = NameStr(*name);
2390 nspid = LookupExplicitNamespace(schemaname);
2392 xmlschema = schema_to_xmlschema_internal(schemaname, nulls,
2393 tableforest, targetns);
2395 PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xml_internal(nspid,
2396 xmlschema->data, nulls,
2397 tableforest, targetns, true)));
2402 * Map SQL database to XML and/or XML Schema document; see SQL/XML:2003
2407 database_to_xml_internal(const char *xmlschema, bool nulls,
2408 bool tableforest, const char *targetns)
2415 xmlcn = map_sql_identifier_to_xml_name(get_database_name(MyDatabaseId),
2417 result = makeStringInfo();
2419 xmldata_root_element_start(result, xmlcn, xmlschema, targetns, true);
2422 appendStringInfo(result, "%s\n\n", xmlschema);
2426 nspid_list = database_get_xml_visible_schemas();
2430 foreach(cell, nspid_list)
2432 Oid nspid = lfirst_oid(cell);
2435 subres = schema_to_xml_internal(nspid, NULL, nulls,
2436 tableforest, targetns, false);
2438 appendStringInfoString(result, subres->data);
2439 appendStringInfoChar(result, '\n');
2445 xmldata_root_element_end(result, xmlcn);
2452 database_to_xml(PG_FUNCTION_ARGS)
2454 bool nulls = PG_GETARG_BOOL(0);
2455 bool tableforest = PG_GETARG_BOOL(1);
2456 const char *targetns = _textout(PG_GETARG_TEXT_P(2));
2458 PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xml_internal(NULL, nulls,
2459 tableforest, targetns)));
2464 database_to_xmlschema_internal(bool nulls, bool tableforest,
2465 const char *targetns)
2473 result = makeStringInfo();
2475 xsd_schema_element_start(result, targetns);
2479 relid_list = database_get_xml_visible_tables();
2480 nspid_list = database_get_xml_visible_schemas();
2483 foreach(cell, relid_list)
2487 rel = heap_open(lfirst_oid(cell), AccessShareLock);
2488 tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2489 heap_close(rel, NoLock);
2492 appendStringInfoString(result,
2493 map_sql_typecoll_to_xmlschema_types(tupdesc_list));
2495 appendStringInfoString(result,
2496 map_sql_catalog_to_xmlschema_types(nspid_list, nulls, tableforest, targetns));
2498 xsd_schema_element_end(result);
2507 database_to_xmlschema(PG_FUNCTION_ARGS)
2509 bool nulls = PG_GETARG_BOOL(0);
2510 bool tableforest = PG_GETARG_BOOL(1);
2511 const char *targetns = _textout(PG_GETARG_TEXT_P(2));
2513 PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xmlschema_internal(nulls,
2514 tableforest, targetns)));
2519 database_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2521 bool nulls = PG_GETARG_BOOL(0);
2522 bool tableforest = PG_GETARG_BOOL(1);
2523 const char *targetns = _textout(PG_GETARG_TEXT_P(2));
2524 StringInfo xmlschema;
2526 xmlschema = database_to_xmlschema_internal(nulls, tableforest, targetns);
2528 PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xml_internal(xmlschema->data,
2529 nulls, tableforest, targetns)));
2534 * Map a multi-part SQL name to an XML name; see SQL/XML:2003 section
2538 map_multipart_sql_identifier_to_xml_name(char *a, char *b, char *c, char *d)
2540 StringInfoData result;
2542 initStringInfo(&result);
2545 appendStringInfo(&result, "%s",
2546 map_sql_identifier_to_xml_name(a, true, true));
2548 appendStringInfo(&result, ".%s",
2549 map_sql_identifier_to_xml_name(b, true, true));
2551 appendStringInfo(&result, ".%s",
2552 map_sql_identifier_to_xml_name(c, true, true));
2554 appendStringInfo(&result, ".%s",
2555 map_sql_identifier_to_xml_name(d, true, true));
2562 * Map an SQL table to an XML Schema document; see SQL/XML:2003
2565 * Map an SQL table to XML Schema data types; see SQL/XML:2003 section
2569 map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls,
2570 bool tableforest, const char *targetns)
2574 char *tabletypename;
2576 StringInfoData result;
2578 initStringInfo(&result);
2580 if (OidIsValid(relid))
2583 Form_pg_class reltuple;
2585 tuple = SearchSysCache(RELOID,
2586 ObjectIdGetDatum(relid),
2588 if (!HeapTupleIsValid(tuple))
2589 elog(ERROR, "cache lookup failed for relation %u", relid);
2590 reltuple = (Form_pg_class) GETSTRUCT(tuple);
2592 xmltn = map_sql_identifier_to_xml_name(NameStr(reltuple->relname),
2595 tabletypename = map_multipart_sql_identifier_to_xml_name("TableType",
2596 get_database_name(MyDatabaseId),
2597 get_namespace_name(reltuple->relnamespace),
2598 NameStr(reltuple->relname));
2600 rowtypename = map_multipart_sql_identifier_to_xml_name("RowType",
2601 get_database_name(MyDatabaseId),
2602 get_namespace_name(reltuple->relnamespace),
2603 NameStr(reltuple->relname));
2605 ReleaseSysCache(tuple);
2614 tabletypename = "TableType";
2615 rowtypename = "RowType";
2618 xsd_schema_element_start(&result, targetns);
2620 appendStringInfoString(&result,
2621 map_sql_typecoll_to_xmlschema_types(list_make1(tupdesc)));
2623 appendStringInfo(&result,
2624 "<xsd:complexType name=\"%s\">\n"
2625 " <xsd:sequence>\n",
2628 for (i = 0; i < tupdesc->natts; i++)
2629 appendStringInfo(&result,
2630 " <xsd:element name=\"%s\" type=\"%s\"%s></xsd:element>\n",
2631 map_sql_identifier_to_xml_name(NameStr(tupdesc->attrs[i]->attname),
2633 map_sql_type_to_xml_name(tupdesc->attrs[i]->atttypid, -1),
2634 nulls ? " nillable=\"true\"" : " minOccurs=\"0\"");
2636 appendStringInfoString(&result,
2637 " </xsd:sequence>\n"
2638 "</xsd:complexType>\n\n");
2642 appendStringInfo(&result,
2643 "<xsd:complexType name=\"%s\">\n"
2645 " <xsd:element name=\"row\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n"
2646 " </xsd:sequence>\n"
2647 "</xsd:complexType>\n\n",
2648 tabletypename, rowtypename);
2650 appendStringInfo(&result,
2651 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2652 xmltn, tabletypename);
2655 appendStringInfo(&result,
2656 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2657 xmltn, rowtypename);
2659 xsd_schema_element_end(&result);
2666 * Map an SQL schema to XML Schema data types; see SQL/XML section
2670 map_sql_schema_to_xmlschema_types(Oid nspid, List *relid_list, bool nulls,
2671 bool tableforest, const char *targetns)
2676 char *schematypename;
2677 StringInfoData result;
2680 dbname = get_database_name(MyDatabaseId);
2681 nspname = get_namespace_name(nspid);
2683 initStringInfo(&result);
2685 xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
2687 schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
2692 appendStringInfo(&result,
2693 "<xsd:complexType name=\"%s\">\n", schematypename);
2695 appendStringInfoString(&result,
2698 appendStringInfoString(&result,
2699 " <xsd:sequence>\n");
2701 foreach(cell, relid_list)
2703 Oid relid = lfirst_oid(cell);
2704 char *relname = get_rel_name(relid);
2705 char *xmltn = map_sql_identifier_to_xml_name(relname, true, false);
2706 char *tabletypename = map_multipart_sql_identifier_to_xml_name(tableforest ? "RowType" : "TableType",
2712 appendStringInfo(&result,
2713 " <xsd:element name=\"%s\" type=\"%s\"/>\n",
2714 xmltn, tabletypename);
2716 appendStringInfo(&result,
2717 " <xsd:element name=\"%s\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n",
2718 xmltn, tabletypename);
2722 appendStringInfoString(&result,
2725 appendStringInfoString(&result,
2726 " </xsd:sequence>\n");
2727 appendStringInfoString(&result,
2728 "</xsd:complexType>\n\n");
2730 appendStringInfo(&result,
2731 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2732 xmlsn, schematypename);
2739 * Map an SQL catalog to XML Schema data types; see SQL/XML section
2743 map_sql_catalog_to_xmlschema_types(List *nspid_list, bool nulls,
2744 bool tableforest, const char *targetns)
2748 char *catalogtypename;
2749 StringInfoData result;
2752 dbname = get_database_name(MyDatabaseId);
2754 initStringInfo(&result);
2756 xmlcn = map_sql_identifier_to_xml_name(dbname, true, false);
2758 catalogtypename = map_multipart_sql_identifier_to_xml_name("CatalogType",
2763 appendStringInfo(&result,
2764 "<xsd:complexType name=\"%s\">\n", catalogtypename);
2765 appendStringInfoString(&result,
2768 foreach(cell, nspid_list)
2770 Oid nspid = lfirst_oid(cell);
2771 char *nspname = get_namespace_name(nspid);
2772 char *xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
2773 char *schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
2778 appendStringInfo(&result,
2779 " <xsd:element name=\"%s\" type=\"%s\"/>\n",
2780 xmlsn, schematypename);
2783 appendStringInfoString(&result,
2785 appendStringInfoString(&result,
2786 "</xsd:complexType>\n\n");
2788 appendStringInfo(&result,
2789 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2790 xmlcn, catalogtypename);
2797 * Map an SQL data type to an XML name; see SQL/XML:2003 section 9.9.
2800 map_sql_type_to_xml_name(Oid typeoid, int typmod)
2802 StringInfoData result;
2804 initStringInfo(&result);
2810 appendStringInfo(&result, "CHAR");
2812 appendStringInfo(&result, "CHAR_%d", typmod - VARHDRSZ);
2816 appendStringInfo(&result, "VARCHAR");
2818 appendStringInfo(&result, "VARCHAR_%d", typmod - VARHDRSZ);
2822 appendStringInfo(&result, "NUMERIC");
2824 appendStringInfo(&result, "NUMERIC_%d_%d",
2825 ((typmod - VARHDRSZ) >> 16) & 0xffff,
2826 (typmod - VARHDRSZ) & 0xffff);
2829 appendStringInfo(&result, "INTEGER");
2832 appendStringInfo(&result, "SMALLINT");
2835 appendStringInfo(&result, "BIGINT");
2838 appendStringInfo(&result, "REAL");
2841 appendStringInfo(&result, "DOUBLE");
2844 appendStringInfo(&result, "BOOLEAN");
2848 appendStringInfo(&result, "TIME");
2850 appendStringInfo(&result, "TIME_%d", typmod);
2854 appendStringInfo(&result, "TIME_WTZ");
2856 appendStringInfo(&result, "TIME_WTZ_%d", typmod);
2860 appendStringInfo(&result, "TIMESTAMP");
2862 appendStringInfo(&result, "TIMESTAMP_%d", typmod);
2864 case TIMESTAMPTZOID:
2866 appendStringInfo(&result, "TIMESTAMP_WTZ");
2868 appendStringInfo(&result, "TIMESTAMP_WTZ_%d", typmod);
2871 appendStringInfo(&result, "DATE");
2874 appendStringInfo(&result, "XML");
2879 Form_pg_type typtuple;
2881 tuple = SearchSysCache(TYPEOID,
2882 ObjectIdGetDatum(typeoid),
2884 if (!HeapTupleIsValid(tuple))
2885 elog(ERROR, "cache lookup failed for type %u", typeoid);
2886 typtuple = (Form_pg_type) GETSTRUCT(tuple);
2888 appendStringInfoString(&result,
2889 map_multipart_sql_identifier_to_xml_name((typtuple->typtype == TYPTYPE_DOMAIN) ? "Domain" : "UDT",
2890 get_database_name(MyDatabaseId),
2891 get_namespace_name(typtuple->typnamespace),
2892 NameStr(typtuple->typname)));
2894 ReleaseSysCache(tuple);
2903 * Map a collection of SQL data types to XML Schema data types; see
2904 * SQL/XML:2002 section 9.10.
2907 map_sql_typecoll_to_xmlschema_types(List *tupdesc_list)
2909 List *uniquetypes = NIL;
2911 StringInfoData result;
2914 /* extract all column types used in the set of TupleDescs */
2915 foreach(cell0, tupdesc_list)
2917 TupleDesc tupdesc = (TupleDesc) lfirst(cell0);
2919 for (i = 0; i < tupdesc->natts; i++)
2921 if (tupdesc->attrs[i]->attisdropped)
2923 uniquetypes = list_append_unique_oid(uniquetypes,
2924 tupdesc->attrs[i]->atttypid);
2928 /* add base types of domains */
2929 foreach(cell0, uniquetypes)
2931 Oid typid = lfirst_oid(cell0);
2932 Oid basetypid = getBaseType(typid);
2934 if (basetypid != typid)
2935 uniquetypes = list_append_unique_oid(uniquetypes, basetypid);
2938 /* Convert to textual form */
2939 initStringInfo(&result);
2941 foreach(cell0, uniquetypes)
2943 appendStringInfo(&result, "%s\n",
2944 map_sql_type_to_xmlschema_type(lfirst_oid(cell0),
2953 * Map an SQL data type to a named XML Schema data type; see SQL/XML
2954 * sections 9.11 and 9.15.
2956 * (The distinction between 9.11 and 9.15 is basically that 9.15 adds
2957 * a name attribute, which this function does. The name-less version
2958 * 9.11 doesn't appear to be required anywhere.)
2961 map_sql_type_to_xmlschema_type(Oid typeoid, int typmod)
2963 StringInfoData result;
2964 const char *typename = map_sql_type_to_xml_name(typeoid, typmod);
2966 initStringInfo(&result);
2968 if (typeoid == XMLOID)
2970 appendStringInfo(&result,
2971 "<xsd:complexType mixed=\"true\">\n"
2973 " <xsd:any name=\"element\" minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>\n"
2974 " </xsd:sequence>\n"
2975 "</xsd:complexType>\n");
2979 appendStringInfo(&result,
2980 "<xsd:simpleType name=\"%s\">\n", typename);
2988 appendStringInfo(&result,
2989 " <xsd:restriction base=\"xsd:string\">\n"
2990 " <xsd:maxLength value=\"%d\"/>\n"
2991 " </xsd:restriction>\n",
2996 appendStringInfo(&result,
2997 " <xsd:restriction base=\"xsd:%s\">\n"
2998 " </xsd:restriction>\n",
2999 xmlbinary == XMLBINARY_BASE64 ? "base64Binary" : "hexBinary");
3003 appendStringInfo(&result,
3004 " <xsd:restriction base=\"xsd:decimal\">\n"
3005 " <xsd:totalDigits value=\"%d\"/>\n"
3006 " <xsd:fractionDigits value=\"%d\"/>\n"
3007 " </xsd:restriction>\n",
3008 ((typmod - VARHDRSZ) >> 16) & 0xffff,
3009 (typmod - VARHDRSZ) & 0xffff);
3013 appendStringInfo(&result,
3014 " <xsd:restriction base=\"xsd:short\">\n"
3015 " <xsd:maxInclusive value=\"%d\"/>\n"
3016 " <xsd:minInclusive value=\"%d\"/>\n"
3017 " </xsd:restriction>\n",
3018 SHRT_MAX, SHRT_MIN);
3022 appendStringInfo(&result,
3023 " <xsd:restriction base=\"xsd:int\">\n"
3024 " <xsd:maxInclusive value=\"%d\"/>\n"
3025 " <xsd:minInclusive value=\"%d\"/>\n"
3026 " </xsd:restriction>\n",
3031 appendStringInfo(&result,
3032 " <xsd:restriction base=\"xsd:long\">\n"
3033 " <xsd:maxInclusive value=\"" INT64_FORMAT "\"/>\n"
3034 " <xsd:minInclusive value=\"" INT64_FORMAT "\"/>\n"
3035 " </xsd:restriction>\n",
3036 (((uint64) 1) << (sizeof(int64) * 8 - 1)) - 1,
3037 (((uint64) 1) << (sizeof(int64) * 8 - 1)));
3041 appendStringInfo(&result,
3042 " <xsd:restriction base=\"xsd:float\"></xsd:restriction>\n");
3046 appendStringInfo(&result,
3047 " <xsd:restriction base=\"xsd:double\"></xsd:restriction>\n");
3051 appendStringInfo(&result,
3052 " <xsd:restriction base=\"xsd:boolean\"></xsd:restriction>\n");
3058 const char *tz = (typeoid == TIMETZOID ? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
3061 appendStringInfo(&result,
3062 " <xsd:restriction base=\"xsd:time\">\n"
3063 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}(.\\p{Nd}+)?%s\"/>\n"
3064 " </xsd:restriction>\n", tz);
3065 else if (typmod == 0)
3066 appendStringInfo(&result,
3067 " <xsd:restriction base=\"xsd:time\">\n"
3068 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3069 " </xsd:restriction>\n", tz);
3071 appendStringInfo(&result,
3072 " <xsd:restriction base=\"xsd:time\">\n"
3073 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}.\\p{Nd}{%d}%s\"/>\n"
3074 " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
3079 case TIMESTAMPTZOID:
3081 const char *tz = (typeoid == TIMESTAMPTZOID ? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
3084 appendStringInfo(&result,
3085 " <xsd:restriction base=\"xsd:dateTime\">\n"
3086 " <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"
3087 " </xsd:restriction>\n", tz);
3088 else if (typmod == 0)
3089 appendStringInfo(&result,
3090 " <xsd:restriction base=\"xsd:dateTime\">\n"
3091 " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3092 " </xsd:restriction>\n", tz);
3094 appendStringInfo(&result,
3095 " <xsd:restriction base=\"xsd:dateTime\">\n"
3096 " <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"
3097 " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
3102 appendStringInfo(&result,
3103 " <xsd:restriction base=\"xsd:date\">\n"
3104 " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}\"/>\n"
3105 " </xsd:restriction>\n");
3109 if (get_typtype(typeoid) == TYPTYPE_DOMAIN)
3112 int32 base_typmod = -1;
3114 base_typeoid = getBaseTypeAndTypmod(typeoid, &base_typmod);
3116 appendStringInfo(&result,
3117 " <xsd:restriction base=\"%s\"/>\n",
3118 map_sql_type_to_xml_name(base_typeoid, base_typmod));
3122 appendStringInfo(&result,
3123 "</xsd:simpleType>\n");
3131 * Map an SQL row to an XML element, taking the row from the active
3132 * SPI cursor. See also SQL/XML:2003 section 9.12.
3135 SPI_sql_row_to_xmlelement(int rownum, StringInfo result, char *tablename,
3136 bool nulls, bool tableforest,
3137 const char *targetns, bool top_level)
3143 xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
3153 xmldata_root_element_start(result, xmltn, NULL, targetns, top_level);
3155 appendStringInfoString(result, "<row>\n");
3157 for (i = 1; i <= SPI_tuptable->tupdesc->natts; i++)
3163 colname = map_sql_identifier_to_xml_name(SPI_fname(SPI_tuptable->tupdesc, i),
3165 colval = SPI_getbinval(SPI_tuptable->vals[rownum],
3166 SPI_tuptable->tupdesc,
3172 appendStringInfo(result, " <%s xsi:nil=\"true\"/>\n", colname);
3175 appendStringInfo(result, " <%s>%s</%s>\n",
3177 map_sql_value_to_xml_value(colval,
3178 SPI_gettypeid(SPI_tuptable->tupdesc, i)),
3184 xmldata_root_element_end(result, xmltn);
3185 appendStringInfoChar(result, '\n');
3188 appendStringInfoString(result, "</row>\n\n");
3193 * XPath related functions
3198 * Convert XML node to text (dump subtree in case of element,
3199 * return value otherwise)
3202 xml_xmlnodetoxmltype(xmlNodePtr cur)
3209 if (cur->type == XML_ELEMENT_NODE)
3211 buf = xmlBufferCreate();
3212 xmlNodeDump(buf, NULL, cur, 0, 1);
3213 result = xmlBuffer_to_xmltype(buf);
3218 str = xmlXPathCastNodeToString(cur);
3219 len = strlen((char *) str);
3220 result = (text *) palloc(len + VARHDRSZ);
3221 SET_VARSIZE(result, len + VARHDRSZ);
3222 memcpy(VARDATA(result), str, len);
3231 * Evaluate XPath expression and return array of XML values.
3233 * As we have no support of XQuery sequences yet, this function seems
3234 * to be the most useful one (array of XML functions plays a role of
3235 * some kind of substitution for XQuery sequences).
3237 * Workaround here: we parse XML data in different way to allow XPath for
3238 * fragments (see "XPath for fragment" TODO comment inside).
3241 xpath(PG_FUNCTION_ARGS)
3244 text *xpath_expr_text = PG_GETARG_TEXT_P(0);
3245 xmltype *data = PG_GETARG_XML_P(1);
3246 ArrayType *namespaces = PG_GETARG_ARRAYTYPE_P(2);
3247 ArrayBuildState *astate = NULL;
3248 xmlParserCtxtPtr ctxt = NULL;
3249 xmlDocPtr doc = NULL;
3250 xmlXPathContextPtr xpathctx = NULL;
3251 xmlXPathCompExprPtr xpathcomp = NULL;
3252 xmlXPathObjectPtr xpathobj = NULL;
3257 xmlChar *xpath_expr;
3261 Datum *ns_names_uris;
3262 bool *ns_names_uris_nulls;
3266 * Namespace mappings are passed as text[]. If an empty array is passed
3267 * (ndim = 0, "0-dimensional"), then there are no namespace mappings.
3268 * Else, a 2-dimensional array with length of the second axis being equal
3269 * to 2 should be passed, i.e., every subarray contains 2 elements, the
3270 * first element defining the name, the second one the URI. Example:
3271 * ARRAY[ARRAY['myns', 'http://example.com'], ARRAY['myns2',
3272 * 'http://example2.com']].
3274 ndim = ARR_NDIM(namespaces);
3279 dims = ARR_DIMS(namespaces);
3281 if (ndim != 2 || dims[1] != 2)
3283 (errcode(ERRCODE_DATA_EXCEPTION),
3284 errmsg("invalid array for XML namespace mapping"),
3285 errdetail("The array must be two-dimensional with length of the second axis equal to 2.")));
3287 Assert(ARR_ELEMTYPE(namespaces) == TEXTOID);
3289 deconstruct_array(namespaces, TEXTOID, -1, false, 'i',
3290 &ns_names_uris, &ns_names_uris_nulls,
3293 Assert((ns_count % 2) == 0); /* checked above */
3294 ns_count /= 2; /* count pairs only */
3298 ns_names_uris = NULL;
3299 ns_names_uris_nulls = NULL;
3303 datastr = VARDATA(data);
3304 len = VARSIZE(data) - VARHDRSZ;
3305 xpath_len = VARSIZE(xpath_expr_text) - VARHDRSZ;
3308 (errcode(ERRCODE_DATA_EXCEPTION),
3309 errmsg("empty XPath expression")));
3314 * To handle both documents and fragments, regardless of the fact whether
3315 * the XML datum has a single root (XML well-formedness), we wrap the XML
3316 * datum in a dummy element (<x>...</x>) and extend the XPath expression
3317 * accordingly. To do it, throw away the XML prolog, if any.
3320 xmlStrncmp((xmlChar *) datastr, (xmlChar *) "<?xml", 5) == 0)
3324 !(datastr[i - 1] == '?' && datastr[i] == '>'))
3328 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3329 "could not parse XML data");
3337 string = (xmlChar *) palloc((len + 8) * sizeof(xmlChar));
3338 memcpy(string, "<x>", 3);
3339 memcpy(string + 3, datastr, len);
3340 memcpy(string + 3 + len, "</x>", 5);
3343 xpath_expr = (xmlChar *) palloc((xpath_len + 3) * sizeof(xmlChar));
3344 memcpy(xpath_expr, "/x", 2);
3345 memcpy(xpath_expr + 2, VARDATA(xpath_expr_text), xpath_len);
3346 xpath_expr[xpath_len + 2] = '\0';
3349 /* We use a PG_TRY block to ensure libxml parser is cleaned up on error */
3355 * redundant XML parsing (two parsings for the same value during one
3356 * command execution are possible)
3358 ctxt = xmlNewParserCtxt();
3360 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3361 "could not allocate parser context");
3362 doc = xmlCtxtReadMemory(ctxt, (char *) string, len, NULL, NULL, 0);
3364 xml_ereport(ERROR, ERRCODE_INVALID_XML_DOCUMENT,
3365 "could not parse XML data");
3366 xpathctx = xmlXPathNewContext(doc);
3367 if (xpathctx == NULL)
3368 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3369 "could not allocate XPath context");
3370 xpathctx->node = xmlDocGetRootElement(doc);
3371 if (xpathctx->node == NULL)
3372 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3373 "could not find root XML element");
3375 /* register namespaces, if any */
3378 for (i = 0; i < ns_count; i++)
3383 if (ns_names_uris_nulls[i * 2] ||
3384 ns_names_uris_nulls[i * 2 + 1])
3386 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3387 errmsg("neither namespace name nor URI may be null")));
3388 ns_name = _textout(ns_names_uris[i * 2]);
3389 ns_uri = _textout(ns_names_uris[i * 2 + 1]);
3390 if (xmlXPathRegisterNs(xpathctx,
3391 (xmlChar *) ns_name,
3392 (xmlChar *) ns_uri) != 0)
3393 ereport(ERROR, /* is this an internal error??? */
3394 (errmsg("could not register XML namespace with name \"%s\" and URI \"%s\"",
3399 xpathcomp = xmlXPathCompile(xpath_expr);
3400 if (xpathcomp == NULL) /* TODO: show proper XPath error details */
3401 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3402 "invalid XPath expression");
3404 xpathobj = xmlXPathCompiledEval(xpathcomp, xpathctx);
3405 if (xpathobj == NULL) /* TODO: reason? */
3407 (errmsg("could not create XPath object")));
3409 xmlXPathFreeCompExpr(xpathcomp);
3412 /* return empty array in cases when nothing is found */
3413 if (xpathobj->nodesetval == NULL)
3416 res_nitems = xpathobj->nodesetval->nodeNr;
3419 for (i = 0; i < xpathobj->nodesetval->nodeNr; i++)
3422 bool elemisnull = false;
3424 elem = PointerGetDatum(xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[i]));
3425 astate = accumArrayResult(astate, elem,
3427 CurrentMemoryContext);
3430 xmlXPathFreeObject(xpathobj);
3432 xmlXPathFreeContext(xpathctx);
3436 xmlFreeParserCtxt(ctxt);
3443 xmlXPathFreeCompExpr(xpathcomp);
3445 xmlXPathFreeObject(xpathobj);
3447 xmlXPathFreeContext(xpathctx);
3451 xmlFreeParserCtxt(ctxt);
3458 if (res_nitems == 0)
3459 PG_RETURN_ARRAYTYPE_P(construct_empty_array(XMLOID));
3461 PG_RETURN_ARRAYTYPE_P(makeArrayResult(astate, CurrentMemoryContext));