Fix oversight in previous error-reporting patch; mustn't pfree path string
[PostgreSQL.git] / src / backend / utils / adt / xml.c
blobadebee6a478f484f358766cc94afa21bdf4b2ac1
1 /*-------------------------------------------------------------------------
3 * xml.c
4 * XML data type support.
7 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $PostgreSQL$
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
23 * else does.
27 * Notes on memory management:
29 * Via callbacks, libxml is told to use palloc and friends for memory
30 * management, within a context that we reset at transaction end (and also at
31 * subtransaction abort) to prevent memory leaks. Resetting at transaction or
32 * subtransaction abort is necessary since we might have thrown a longjmp
33 * while some data structures were not linked from anywhere persistent.
34 * Resetting at transaction commit might not be necessary, but seems a good
35 * idea to forestall long-term leaks.
37 * Sometimes libxml allocates global structures in the hope that it can reuse
38 * them later on. Therefore, before resetting LibxmlContext, we must tell
39 * libxml to discard any global data it has. The libxml API documentation is
40 * not very good about specifying this, but for now we assume that
41 * xmlCleanupParser() will get rid of anything we need to worry about.
43 * We use palloc --- which will throw a longjmp on error --- for allocation
44 * callbacks that officially should act like malloc, ie, return NULL on
45 * out-of-memory. This is a bit risky since there is a chance of leaving
46 * persistent libxml data structures in an inconsistent partially-constructed
47 * state, perhaps leading to crash in xmlCleanupParser(). However, as of
48 * early 2008 it is *known* that libxml can crash on out-of-memory due to
49 * inadequate checks for NULL returns, so this behavior seems the lesser
50 * of two evils.
53 #include "postgres.h"
55 #ifdef USE_LIBXML
56 #include <libxml/chvalid.h>
57 #include <libxml/parser.h>
58 #include <libxml/tree.h>
59 #include <libxml/uri.h>
60 #include <libxml/xmlerror.h>
61 #include <libxml/xmlwriter.h>
62 #include <libxml/xpath.h>
63 #include <libxml/xpathInternals.h>
64 #endif /* USE_LIBXML */
66 #include "catalog/namespace.h"
67 #include "catalog/pg_type.h"
68 #include "commands/dbcommands.h"
69 #include "executor/executor.h"
70 #include "executor/spi.h"
71 #include "fmgr.h"
72 #include "lib/stringinfo.h"
73 #include "libpq/pqformat.h"
74 #include "mb/pg_wchar.h"
75 #include "miscadmin.h"
76 #include "nodes/execnodes.h"
77 #include "nodes/nodeFuncs.h"
78 #include "utils/array.h"
79 #include "utils/builtins.h"
80 #include "utils/date.h"
81 #include "utils/datetime.h"
82 #include "utils/lsyscache.h"
83 #include "utils/memutils.h"
84 #include "utils/xml.h"
87 /* GUC variables */
88 int xmlbinary;
89 int xmloption;
91 #ifdef USE_LIBXML
93 static StringInfo xml_err_buf = NULL;
94 static MemoryContext LibxmlContext = NULL;
96 static void xml_init(void);
97 static void xml_memory_init(void);
98 static void xml_memory_cleanup(void);
99 static void *xml_palloc(size_t size);
100 static void *xml_repalloc(void *ptr, size_t size);
101 static void xml_pfree(void *ptr);
102 static char *xml_pstrdup(const char *string);
103 static void xml_ereport(int level, int sqlcode, const char *msg);
104 static void xml_errorHandler(void *ctxt, const char *msg,...);
105 static void xml_ereport_by_code(int level, int sqlcode,
106 const char *msg, int errcode);
107 static xmlChar *xml_text2xmlChar(text *in);
108 static int parse_xml_decl(const xmlChar * str, size_t *lenp,
109 xmlChar ** version, xmlChar ** encoding, int *standalone);
110 static bool print_xml_decl(StringInfo buf, const xmlChar * version,
111 pg_enc encoding, int standalone);
112 static xmlDocPtr xml_parse(text *data, XmlOptionType xmloption_arg,
113 bool preserve_whitespace, xmlChar * encoding);
114 static text *xml_xmlnodetoxmltype(xmlNodePtr cur);
115 #endif /* USE_LIBXML */
117 static StringInfo query_to_xml_internal(const char *query, char *tablename,
118 const char *xmlschema, bool nulls, bool tableforest,
119 const char *targetns, bool top_level);
120 static const char *map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid,
121 bool nulls, bool tableforest, const char *targetns);
122 static const char *map_sql_schema_to_xmlschema_types(Oid nspid,
123 List *relid_list, bool nulls,
124 bool tableforest, const char *targetns);
125 static const char *map_sql_catalog_to_xmlschema_types(List *nspid_list,
126 bool nulls, bool tableforest,
127 const char *targetns);
128 static const char *map_sql_type_to_xml_name(Oid typeoid, int typmod);
129 static const char *map_sql_typecoll_to_xmlschema_types(List *tupdesc_list);
130 static const char *map_sql_type_to_xmlschema_type(Oid typeoid, int typmod);
131 static void SPI_sql_row_to_xmlelement(int rownum, StringInfo result,
132 char *tablename, bool nulls, bool tableforest,
133 const char *targetns, bool top_level);
135 #define NO_XML_SUPPORT() \
136 ereport(ERROR, \
137 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
138 errmsg("unsupported XML feature"), \
139 errdetail("This functionality requires the server to be built with libxml support."), \
140 errhint("You need to rebuild PostgreSQL using --with-libxml.")))
143 /* from SQL/XML:2003 section 4.7 */
144 #define NAMESPACE_XSD "http://www.w3.org/2001/XMLSchema"
145 #define NAMESPACE_XSI "http://www.w3.org/2001/XMLSchema-instance"
146 #define NAMESPACE_SQLXML "http://standards.iso.org/iso/9075/2003/sqlxml"
149 #ifdef USE_LIBXML
151 static int
152 xmlChar_to_encoding(xmlChar * encoding_name)
154 int encoding = pg_char_to_encoding((char *) encoding_name);
156 if (encoding < 0)
157 ereport(ERROR,
158 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
159 errmsg("invalid encoding name \"%s\"",
160 (char *) encoding_name)));
161 return encoding;
163 #endif
167 * xml_in uses a plain C string to VARDATA conversion, so for the time being
168 * we use the conversion function for the text datatype.
170 * This is only acceptable so long as xmltype and text use the same
171 * representation.
173 Datum
174 xml_in(PG_FUNCTION_ARGS)
176 #ifdef USE_LIBXML
177 char *s = PG_GETARG_CSTRING(0);
178 xmltype *vardata;
179 xmlDocPtr doc;
181 vardata = (xmltype *) cstring_to_text(s);
184 * Parse the data to check if it is well-formed XML data. Assume that
185 * ERROR occurred if parsing failed.
187 doc = xml_parse(vardata, xmloption, true, NULL);
188 xmlFreeDoc(doc);
190 PG_RETURN_XML_P(vardata);
191 #else
192 NO_XML_SUPPORT();
193 return 0;
194 #endif
198 #define PG_XML_DEFAULT_VERSION "1.0"
202 * xml_out_internal uses a plain VARDATA to C string conversion, so for the
203 * time being we use the conversion function for the text datatype.
205 * This is only acceptable so long as xmltype and text use the same
206 * representation.
208 static char *
209 xml_out_internal(xmltype *x, pg_enc target_encoding)
211 char *str = text_to_cstring((text *) x);
213 #ifdef USE_LIBXML
214 size_t len = strlen(str);
215 xmlChar *version;
216 int standalone;
217 int res_code;
219 if ((res_code = parse_xml_decl((xmlChar *) str,
220 &len, &version, NULL, &standalone)) == 0)
222 StringInfoData buf;
224 initStringInfo(&buf);
226 if (!print_xml_decl(&buf, version, target_encoding, standalone))
229 * If we are not going to produce an XML declaration, eat a single
230 * newline in the original string to prevent empty first lines in
231 * the output.
233 if (*(str + len) == '\n')
234 len += 1;
236 appendStringInfoString(&buf, str + len);
238 if (version)
239 xmlFree(version);
240 pfree(str);
242 return buf.data;
245 xml_ereport_by_code(WARNING, ERRCODE_INTERNAL_ERROR,
246 "could not parse XML declaration in stored value",
247 res_code);
248 #endif
249 return str;
253 Datum
254 xml_out(PG_FUNCTION_ARGS)
256 xmltype *x = PG_GETARG_XML_P(0);
259 * xml_out removes the encoding property in all cases. This is because we
260 * cannot control from here whether the datum will be converted to a
261 * different client encoding, so we'd do more harm than good by including
262 * it.
264 PG_RETURN_CSTRING(xml_out_internal(x, 0));
268 Datum
269 xml_recv(PG_FUNCTION_ARGS)
271 #ifdef USE_LIBXML
272 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
273 xmltype *result;
274 char *str;
275 char *newstr;
276 int nbytes;
277 xmlDocPtr doc;
278 xmlChar *encoding = NULL;
281 * Read the data in raw format. We don't know yet what the encoding is, as
282 * that information is embedded in the xml declaration; so we have to
283 * parse that before converting to server encoding.
285 nbytes = buf->len - buf->cursor;
286 str = (char *) pq_getmsgbytes(buf, nbytes);
289 * We need a null-terminated string to pass to parse_xml_decl(). Rather
290 * than make a separate copy, make the temporary result one byte bigger
291 * than it needs to be.
293 result = palloc(nbytes + 1 + VARHDRSZ);
294 SET_VARSIZE(result, nbytes + VARHDRSZ);
295 memcpy(VARDATA(result), str, nbytes);
296 str = VARDATA(result);
297 str[nbytes] = '\0';
299 parse_xml_decl((xmlChar *) str, NULL, NULL, &encoding, NULL);
302 * Parse the data to check if it is well-formed XML data. Assume that
303 * xml_parse will throw ERROR if not.
305 doc = xml_parse(result, xmloption, true, encoding);
306 xmlFreeDoc(doc);
308 /* Now that we know what we're dealing with, convert to server encoding */
309 newstr = (char *) pg_do_encoding_conversion((unsigned char *) str,
310 nbytes,
311 encoding ?
312 xmlChar_to_encoding(encoding) :
313 PG_UTF8,
314 GetDatabaseEncoding());
316 if (newstr != str)
318 pfree(result);
319 result = (xmltype *) cstring_to_text(newstr);
320 pfree(newstr);
323 PG_RETURN_XML_P(result);
324 #else
325 NO_XML_SUPPORT();
326 return 0;
327 #endif
331 Datum
332 xml_send(PG_FUNCTION_ARGS)
334 xmltype *x = PG_GETARG_XML_P(0);
335 char *outval;
336 StringInfoData buf;
339 * xml_out_internal doesn't convert the encoding, it just prints the right
340 * declaration. pq_sendtext will do the conversion.
342 outval = xml_out_internal(x, pg_get_client_encoding());
344 pq_begintypsend(&buf);
345 pq_sendtext(&buf, outval, strlen(outval));
346 pfree(outval);
347 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
351 #ifdef USE_LIBXML
352 static void
353 appendStringInfoText(StringInfo str, const text *t)
355 appendBinaryStringInfo(str, VARDATA(t), VARSIZE(t) - VARHDRSZ);
357 #endif
360 static xmltype *
361 stringinfo_to_xmltype(StringInfo buf)
363 return (xmltype *) cstring_to_text_with_len(buf->data, buf->len);
367 static xmltype *
368 cstring_to_xmltype(const char *string)
370 return (xmltype *) cstring_to_text(string);
374 #ifdef USE_LIBXML
375 static xmltype *
376 xmlBuffer_to_xmltype(xmlBufferPtr buf)
378 return (xmltype *) cstring_to_text_with_len((char *) xmlBufferContent(buf),
379 xmlBufferLength(buf));
381 #endif
384 Datum
385 xmlcomment(PG_FUNCTION_ARGS)
387 #ifdef USE_LIBXML
388 text *arg = PG_GETARG_TEXT_P(0);
389 char *argdata = VARDATA(arg);
390 int len = VARSIZE(arg) - VARHDRSZ;
391 StringInfoData buf;
392 int i;
394 /* check for "--" in string or "-" at the end */
395 for (i = 1; i < len; i++)
397 if (argdata[i] == '-' && argdata[i - 1] == '-')
398 ereport(ERROR,
399 (errcode(ERRCODE_INVALID_XML_COMMENT),
400 errmsg("invalid XML comment")));
402 if (len > 0 && argdata[len - 1] == '-')
403 ereport(ERROR,
404 (errcode(ERRCODE_INVALID_XML_COMMENT),
405 errmsg("invalid XML comment")));
407 initStringInfo(&buf);
408 appendStringInfo(&buf, "<!--");
409 appendStringInfoText(&buf, arg);
410 appendStringInfo(&buf, "-->");
412 PG_RETURN_XML_P(stringinfo_to_xmltype(&buf));
413 #else
414 NO_XML_SUPPORT();
415 return 0;
416 #endif
422 * TODO: xmlconcat needs to merge the notations and unparsed entities
423 * of the argument values. Not very important in practice, though.
425 xmltype *
426 xmlconcat(List *args)
428 #ifdef USE_LIBXML
429 int global_standalone = 1;
430 xmlChar *global_version = NULL;
431 bool global_version_no_value = false;
432 StringInfoData buf;
433 ListCell *v;
435 initStringInfo(&buf);
436 foreach(v, args)
438 xmltype *x = DatumGetXmlP(PointerGetDatum(lfirst(v)));
439 size_t len;
440 xmlChar *version;
441 int standalone;
442 char *str;
444 len = VARSIZE(x) - VARHDRSZ;
445 str = text_to_cstring((text *) x);
447 parse_xml_decl((xmlChar *) str, &len, &version, NULL, &standalone);
449 if (standalone == 0 && global_standalone == 1)
450 global_standalone = 0;
451 if (standalone < 0)
452 global_standalone = -1;
454 if (!version)
455 global_version_no_value = true;
456 else if (!global_version)
457 global_version = xmlStrdup(version);
458 else if (xmlStrcmp(version, global_version) != 0)
459 global_version_no_value = true;
461 appendStringInfoString(&buf, str + len);
462 pfree(str);
465 if (!global_version_no_value || global_standalone >= 0)
467 StringInfoData buf2;
469 initStringInfo(&buf2);
471 print_xml_decl(&buf2,
472 (!global_version_no_value) ? global_version : NULL,
474 global_standalone);
476 appendStringInfoString(&buf2, buf.data);
477 buf = buf2;
480 return stringinfo_to_xmltype(&buf);
481 #else
482 NO_XML_SUPPORT();
483 return NULL;
484 #endif
489 * XMLAGG support
491 Datum
492 xmlconcat2(PG_FUNCTION_ARGS)
494 if (PG_ARGISNULL(0))
496 if (PG_ARGISNULL(1))
497 PG_RETURN_NULL();
498 else
499 PG_RETURN_XML_P(PG_GETARG_XML_P(1));
501 else if (PG_ARGISNULL(1))
502 PG_RETURN_XML_P(PG_GETARG_XML_P(0));
503 else
504 PG_RETURN_XML_P(xmlconcat(list_make2(PG_GETARG_XML_P(0),
505 PG_GETARG_XML_P(1))));
509 Datum
510 texttoxml(PG_FUNCTION_ARGS)
512 text *data = PG_GETARG_TEXT_P(0);
514 PG_RETURN_XML_P(xmlparse(data, xmloption, true));
518 Datum
519 xmltotext(PG_FUNCTION_ARGS)
521 xmltype *data = PG_GETARG_XML_P(0);
523 /* It's actually binary compatible. */
524 PG_RETURN_TEXT_P((text *) data);
528 text *
529 xmltotext_with_xmloption(xmltype *data, XmlOptionType xmloption_arg)
531 if (xmloption_arg == XMLOPTION_DOCUMENT && !xml_is_document(data))
532 ereport(ERROR,
533 (errcode(ERRCODE_NOT_AN_XML_DOCUMENT),
534 errmsg("not an XML document")));
536 /* It's actually binary compatible, save for the above check. */
537 return (text *) data;
541 xmltype *
542 xmlelement(XmlExprState *xmlExpr, ExprContext *econtext)
544 #ifdef USE_LIBXML
545 XmlExpr *xexpr = (XmlExpr *) xmlExpr->xprstate.expr;
546 xmltype *result;
547 List *named_arg_strings;
548 List *arg_strings;
549 int i;
550 ListCell *arg;
551 ListCell *narg;
552 xmlBufferPtr buf;
553 xmlTextWriterPtr writer;
556 * We first evaluate all the arguments, then start up libxml and create
557 * the result. This avoids issues if one of the arguments involves a call
558 * to some other function or subsystem that wants to use libxml on its own
559 * terms.
561 named_arg_strings = NIL;
562 i = 0;
563 foreach(arg, xmlExpr->named_args)
565 ExprState *e = (ExprState *) lfirst(arg);
566 Datum value;
567 bool isnull;
568 char *str;
570 value = ExecEvalExpr(e, econtext, &isnull, NULL);
571 if (isnull)
572 str = NULL;
573 else
574 str = OutputFunctionCall(&xmlExpr->named_outfuncs[i], value);
575 named_arg_strings = lappend(named_arg_strings, str);
576 i++;
579 arg_strings = NIL;
580 foreach(arg, xmlExpr->args)
582 ExprState *e = (ExprState *) lfirst(arg);
583 Datum value;
584 bool isnull;
585 char *str;
587 value = ExecEvalExpr(e, econtext, &isnull, NULL);
588 /* here we can just forget NULL elements immediately */
589 if (!isnull)
591 str = map_sql_value_to_xml_value(value,
592 exprType((Node *) e->expr));
593 arg_strings = lappend(arg_strings, str);
597 /* now safe to run libxml */
598 xml_init();
600 buf = xmlBufferCreate();
601 writer = xmlNewTextWriterMemory(buf, 0);
603 xmlTextWriterStartElement(writer, (xmlChar *) xexpr->name);
605 forboth(arg, named_arg_strings, narg, xexpr->arg_names)
607 char *str = (char *) lfirst(arg);
608 char *argname = strVal(lfirst(narg));
610 if (str)
612 xmlTextWriterWriteAttribute(writer,
613 (xmlChar *) argname,
614 (xmlChar *) str);
615 pfree(str);
619 foreach(arg, arg_strings)
621 char *str = (char *) lfirst(arg);
623 xmlTextWriterWriteRaw(writer, (xmlChar *) str);
626 xmlTextWriterEndElement(writer);
627 xmlFreeTextWriter(writer);
629 result = xmlBuffer_to_xmltype(buf);
630 xmlBufferFree(buf);
632 return result;
633 #else
634 NO_XML_SUPPORT();
635 return NULL;
636 #endif
640 xmltype *
641 xmlparse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace)
643 #ifdef USE_LIBXML
644 xmlDocPtr doc;
646 doc = xml_parse(data, xmloption_arg, preserve_whitespace, NULL);
647 xmlFreeDoc(doc);
649 return (xmltype *) data;
650 #else
651 NO_XML_SUPPORT();
652 return NULL;
653 #endif
657 xmltype *
658 xmlpi(char *target, text *arg, bool arg_is_null, bool *result_is_null)
660 #ifdef USE_LIBXML
661 xmltype *result;
662 StringInfoData buf;
664 if (pg_strcasecmp(target, "xml") == 0)
665 ereport(ERROR,
666 (errcode(ERRCODE_SYNTAX_ERROR), /* really */
667 errmsg("invalid XML processing instruction"),
668 errdetail("XML processing instruction target name cannot be \"%s\".", target)));
671 * Following the SQL standard, the null check comes after the syntax check
672 * above.
674 *result_is_null = arg_is_null;
675 if (*result_is_null)
676 return NULL;
678 initStringInfo(&buf);
680 appendStringInfo(&buf, "<?%s", target);
682 if (arg != NULL)
684 char *string;
686 string = text_to_cstring(arg);
687 if (strstr(string, "?>") != NULL)
688 ereport(ERROR,
689 (errcode(ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION),
690 errmsg("invalid XML processing instruction"),
691 errdetail("XML processing instruction cannot contain \"?>\".")));
693 appendStringInfoChar(&buf, ' ');
694 appendStringInfoString(&buf, string + strspn(string, " "));
695 pfree(string);
697 appendStringInfoString(&buf, "?>");
699 result = stringinfo_to_xmltype(&buf);
700 pfree(buf.data);
701 return result;
702 #else
703 NO_XML_SUPPORT();
704 return NULL;
705 #endif
709 xmltype *
710 xmlroot(xmltype *data, text *version, int standalone)
712 #ifdef USE_LIBXML
713 char *str;
714 size_t len;
715 xmlChar *orig_version;
716 int orig_standalone;
717 StringInfoData buf;
719 len = VARSIZE(data) - VARHDRSZ;
720 str = text_to_cstring((text *) data);
722 parse_xml_decl((xmlChar *) str, &len, &orig_version, NULL, &orig_standalone);
724 if (version)
725 orig_version = xml_text2xmlChar(version);
726 else
727 orig_version = NULL;
729 switch (standalone)
731 case XML_STANDALONE_YES:
732 orig_standalone = 1;
733 break;
734 case XML_STANDALONE_NO:
735 orig_standalone = 0;
736 break;
737 case XML_STANDALONE_NO_VALUE:
738 orig_standalone = -1;
739 break;
740 case XML_STANDALONE_OMITTED:
741 /* leave original value */
742 break;
745 initStringInfo(&buf);
746 print_xml_decl(&buf, orig_version, 0, orig_standalone);
747 appendStringInfoString(&buf, str + len);
749 return stringinfo_to_xmltype(&buf);
750 #else
751 NO_XML_SUPPORT();
752 return NULL;
753 #endif
758 * Validate document (given as string) against DTD (given as external link)
760 * This has been removed because it is a security hole: unprivileged users
761 * should not be able to use Postgres to fetch arbitrary external files,
762 * which unfortunately is exactly what libxml is willing to do with the DTD
763 * parameter.
765 Datum
766 xmlvalidate(PG_FUNCTION_ARGS)
768 ereport(ERROR,
769 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
770 errmsg("xmlvalidate is not implemented")));
771 return 0;
775 bool
776 xml_is_document(xmltype *arg)
778 #ifdef USE_LIBXML
779 bool result;
780 xmlDocPtr doc = NULL;
781 MemoryContext ccxt = CurrentMemoryContext;
783 PG_TRY();
785 doc = xml_parse((text *) arg, XMLOPTION_DOCUMENT, true, NULL);
786 result = true;
788 PG_CATCH();
790 ErrorData *errdata;
791 MemoryContext ecxt;
793 ecxt = MemoryContextSwitchTo(ccxt);
794 errdata = CopyErrorData();
795 if (errdata->sqlerrcode == ERRCODE_INVALID_XML_DOCUMENT)
797 FlushErrorState();
798 result = false;
800 else
802 MemoryContextSwitchTo(ecxt);
803 PG_RE_THROW();
806 PG_END_TRY();
808 if (doc)
809 xmlFreeDoc(doc);
811 return result;
812 #else /* not USE_LIBXML */
813 NO_XML_SUPPORT();
814 return false;
815 #endif /* not USE_LIBXML */
820 * xml cleanup function for transaction end. This is also called on
821 * subtransaction abort; see notes at top of file for rationale.
823 void
824 AtEOXact_xml(void)
826 #ifdef USE_LIBXML
827 xml_memory_cleanup();
828 #endif
832 #ifdef USE_LIBXML
835 * Set up for use of libxml --- this should be called by each function that
836 * is about to use libxml facilities.
838 * TODO: xmlChar is utf8-char, make proper tuning (initdb with enc!=utf8 and
839 * check)
841 static void
842 xml_init(void)
844 static bool first_time = true;
846 if (first_time)
848 /* Stuff we need do only once per session */
849 MemoryContext oldcontext;
852 * Currently, we have no pure UTF-8 support for internals -- check if
853 * we can work.
855 if (sizeof(char) != sizeof(xmlChar))
856 ereport(ERROR,
857 (errmsg("could not initialize XML library"),
858 errdetail("libxml2 has incompatible char type: sizeof(char)=%u, sizeof(xmlChar)=%u.",
859 (int) sizeof(char), (int) sizeof(xmlChar))));
861 /* create error buffer in permanent context */
862 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
863 xml_err_buf = makeStringInfo();
864 MemoryContextSwitchTo(oldcontext);
866 /* Now that xml_err_buf exists, safe to call xml_errorHandler */
867 xmlSetGenericErrorFunc(NULL, xml_errorHandler);
869 /* Set up memory allocation our way, too */
870 xml_memory_init();
872 /* Check library compatibility */
873 LIBXML_TEST_VERSION;
875 first_time = false;
877 else
879 /* Reset pre-existing buffer to empty */
880 Assert(xml_err_buf != NULL);
881 resetStringInfo(xml_err_buf);
884 * We re-establish the callback functions every time. This makes it
885 * safe for other subsystems (PL/Perl, say) to also use libxml with
886 * their own callbacks ... so long as they likewise set up the
887 * callbacks on every use. It's cheap enough to not be worth worrying
888 * about, anyway.
890 xmlSetGenericErrorFunc(NULL, xml_errorHandler);
891 xml_memory_init();
897 * SQL/XML allows storing "XML documents" or "XML content". "XML
898 * documents" are specified by the XML specification and are parsed
899 * easily by libxml. "XML content" is specified by SQL/XML as the
900 * production "XMLDecl? content". But libxml can only parse the
901 * "content" part, so we have to parse the XML declaration ourselves
902 * to complete this.
905 #define CHECK_XML_SPACE(p) \
906 do { \
907 if (!xmlIsBlank_ch(*(p))) \
908 return XML_ERR_SPACE_REQUIRED; \
909 } while (0)
911 #define SKIP_XML_SPACE(p) \
912 while (xmlIsBlank_ch(*(p))) (p)++
914 /* Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender */
915 /* Beware of multiple evaluations of argument! */
916 #define PG_XMLISNAMECHAR(c) \
917 (xmlIsBaseChar_ch(c) || xmlIsIdeographicQ(c) \
918 || xmlIsDigit_ch(c) \
919 || c == '.' || c == '-' || c == '_' || c == ':' \
920 || xmlIsCombiningQ(c) \
921 || xmlIsExtender_ch(c))
923 static int
924 parse_xml_decl(const xmlChar * str, size_t *lenp,
925 xmlChar ** version, xmlChar ** encoding, int *standalone)
927 const xmlChar *p;
928 const xmlChar *save_p;
929 size_t len;
930 int utf8char;
931 int utf8len;
933 xml_init();
935 if (version)
936 *version = NULL;
937 if (encoding)
938 *encoding = NULL;
939 if (standalone)
940 *standalone = -1;
942 p = str;
944 if (xmlStrncmp(p, (xmlChar *) "<?xml", 5) != 0)
945 goto finished;
947 /* if next char is name char, it's a PI like <?xml-stylesheet ...?> */
948 utf8len = strlen((const char *) (p + 5));
949 utf8char = xmlGetUTF8Char(p + 5, &utf8len);
950 if (PG_XMLISNAMECHAR(utf8char))
951 goto finished;
953 p += 5;
955 /* version */
956 CHECK_XML_SPACE(p);
957 SKIP_XML_SPACE(p);
958 if (xmlStrncmp(p, (xmlChar *) "version", 7) != 0)
959 return XML_ERR_VERSION_MISSING;
960 p += 7;
961 SKIP_XML_SPACE(p);
962 if (*p != '=')
963 return XML_ERR_VERSION_MISSING;
964 p += 1;
965 SKIP_XML_SPACE(p);
967 if (*p == '\'' || *p == '"')
969 const xmlChar *q;
971 q = xmlStrchr(p + 1, *p);
972 if (!q)
973 return XML_ERR_VERSION_MISSING;
975 if (version)
976 *version = xmlStrndup(p + 1, q - p - 1);
977 p = q + 1;
979 else
980 return XML_ERR_VERSION_MISSING;
982 /* encoding */
983 save_p = p;
984 SKIP_XML_SPACE(p);
985 if (xmlStrncmp(p, (xmlChar *) "encoding", 8) == 0)
987 CHECK_XML_SPACE(save_p);
988 p += 8;
989 SKIP_XML_SPACE(p);
990 if (*p != '=')
991 return XML_ERR_MISSING_ENCODING;
992 p += 1;
993 SKIP_XML_SPACE(p);
995 if (*p == '\'' || *p == '"')
997 const xmlChar *q;
999 q = xmlStrchr(p + 1, *p);
1000 if (!q)
1001 return XML_ERR_MISSING_ENCODING;
1003 if (encoding)
1004 *encoding = xmlStrndup(p + 1, q - p - 1);
1005 p = q + 1;
1007 else
1008 return XML_ERR_MISSING_ENCODING;
1010 else
1012 p = save_p;
1015 /* standalone */
1016 save_p = p;
1017 SKIP_XML_SPACE(p);
1018 if (xmlStrncmp(p, (xmlChar *) "standalone", 10) == 0)
1020 CHECK_XML_SPACE(save_p);
1021 p += 10;
1022 SKIP_XML_SPACE(p);
1023 if (*p != '=')
1024 return XML_ERR_STANDALONE_VALUE;
1025 p += 1;
1026 SKIP_XML_SPACE(p);
1027 if (xmlStrncmp(p, (xmlChar *) "'yes'", 5) == 0 ||
1028 xmlStrncmp(p, (xmlChar *) "\"yes\"", 5) == 0)
1030 *standalone = 1;
1031 p += 5;
1033 else if (xmlStrncmp(p, (xmlChar *) "'no'", 4) == 0 ||
1034 xmlStrncmp(p, (xmlChar *) "\"no\"", 4) == 0)
1036 *standalone = 0;
1037 p += 4;
1039 else
1040 return XML_ERR_STANDALONE_VALUE;
1042 else
1044 p = save_p;
1047 SKIP_XML_SPACE(p);
1048 if (xmlStrncmp(p, (xmlChar *) "?>", 2) != 0)
1049 return XML_ERR_XMLDECL_NOT_FINISHED;
1050 p += 2;
1052 finished:
1053 len = p - str;
1055 for (p = str; p < str + len; p++)
1056 if (*p > 127)
1057 return XML_ERR_INVALID_CHAR;
1059 if (lenp)
1060 *lenp = len;
1062 return XML_ERR_OK;
1067 * Write an XML declaration. On output, we adjust the XML declaration
1068 * as follows. (These rules are the moral equivalent of the clause
1069 * "Serialization of an XML value" in the SQL standard.)
1071 * We try to avoid generating an XML declaration if possible. This is
1072 * so that you don't get trivial things like xml '<foo/>' resulting in
1073 * '<?xml version="1.0"?><foo/>', which would surely be annoying. We
1074 * must provide a declaration if the standalone property is specified
1075 * or if we include an encoding declaration. If we have a
1076 * declaration, we must specify a version (XML requires this).
1077 * Otherwise we only make a declaration if the version is not "1.0",
1078 * which is the default version specified in SQL:2003.
1080 static bool
1081 print_xml_decl(StringInfo buf, const xmlChar * version,
1082 pg_enc encoding, int standalone)
1084 xml_init();
1086 if ((version && strcmp((char *) version, PG_XML_DEFAULT_VERSION) != 0)
1087 || (encoding && encoding != PG_UTF8)
1088 || standalone != -1)
1090 appendStringInfoString(buf, "<?xml");
1092 if (version)
1093 appendStringInfo(buf, " version=\"%s\"", version);
1094 else
1095 appendStringInfo(buf, " version=\"%s\"", PG_XML_DEFAULT_VERSION);
1097 if (encoding && encoding != PG_UTF8)
1100 * XXX might be useful to convert this to IANA names (ISO-8859-1
1101 * instead of LATIN1 etc.); needs field experience
1103 appendStringInfo(buf, " encoding=\"%s\"",
1104 pg_encoding_to_char(encoding));
1107 if (standalone == 1)
1108 appendStringInfoString(buf, " standalone=\"yes\"");
1109 else if (standalone == 0)
1110 appendStringInfoString(buf, " standalone=\"no\"");
1111 appendStringInfoString(buf, "?>");
1113 return true;
1115 else
1116 return false;
1121 * Convert a C string to XML internal representation
1123 * TODO maybe, libxml2's xmlreader is better? (do not construct DOM,
1124 * yet do not use SAX - see xmlreader.c)
1126 static xmlDocPtr
1127 xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace,
1128 xmlChar * encoding)
1130 int32 len;
1131 xmlChar *string;
1132 xmlChar *utf8string;
1133 xmlParserCtxtPtr ctxt;
1134 xmlDocPtr doc;
1136 len = VARSIZE(data) - VARHDRSZ; /* will be useful later */
1137 string = xml_text2xmlChar(data);
1139 utf8string = pg_do_encoding_conversion(string,
1140 len,
1141 encoding ?
1142 xmlChar_to_encoding(encoding) :
1143 GetDatabaseEncoding(),
1144 PG_UTF8);
1146 xml_init();
1147 xmlInitParser();
1148 ctxt = xmlNewParserCtxt();
1149 if (ctxt == NULL)
1150 xml_ereport(ERROR, ERRCODE_OUT_OF_MEMORY,
1151 "could not allocate parser context");
1153 if (xmloption_arg == XMLOPTION_DOCUMENT)
1156 * Note, that here we try to apply DTD defaults
1157 * (XML_PARSE_DTDATTR) according to SQL/XML:10.16.7.d: 'Default
1158 * values defined by internal DTD are applied'. As for external
1159 * DTDs, we try to support them too, (see SQL/XML:10.16.7.e)
1161 doc = xmlCtxtReadDoc(ctxt, utf8string,
1162 NULL,
1163 "UTF-8",
1164 XML_PARSE_NOENT | XML_PARSE_DTDATTR
1165 | (preserve_whitespace ? 0 : XML_PARSE_NOBLANKS));
1166 if (doc == NULL)
1167 xml_ereport(ERROR, ERRCODE_INVALID_XML_DOCUMENT,
1168 "invalid XML document");
1170 else
1172 int res_code;
1173 size_t count;
1174 xmlChar *version = NULL;
1175 int standalone = -1;
1177 doc = xmlNewDoc(NULL);
1179 res_code = parse_xml_decl(utf8string,
1180 &count, &version, NULL, &standalone);
1181 if (res_code != 0)
1182 xml_ereport_by_code(ERROR, ERRCODE_INVALID_XML_CONTENT,
1183 "invalid XML content: invalid XML declaration",
1184 res_code);
1186 res_code = xmlParseBalancedChunkMemory(doc, NULL, NULL, 0,
1187 utf8string + count, NULL);
1188 if (res_code != 0)
1189 xml_ereport(ERROR, ERRCODE_INVALID_XML_CONTENT,
1190 "invalid XML content");
1192 doc->version = xmlStrdup(version);
1193 doc->encoding = xmlStrdup((xmlChar *) "UTF-8");
1194 doc->standalone = standalone;
1197 xmlFreeParserCtxt(ctxt);
1199 return doc;
1204 * xmlChar<->text conversions
1206 static xmlChar *
1207 xml_text2xmlChar(text *in)
1209 return (xmlChar *) text_to_cstring(in);
1214 * Manage the special context used for all libxml allocations
1216 static void
1217 xml_memory_init(void)
1220 * Create memory context if not there already. We make it a child of
1221 * TopMemoryContext, even though our current policy is that it doesn't
1222 * survive past transaction end, because we want to be really really
1223 * sure it doesn't go away before we've called xmlCleanupParser().
1225 if (LibxmlContext == NULL)
1226 LibxmlContext = AllocSetContextCreate(TopMemoryContext,
1227 "LibxmlContext",
1228 ALLOCSET_DEFAULT_MINSIZE,
1229 ALLOCSET_DEFAULT_INITSIZE,
1230 ALLOCSET_DEFAULT_MAXSIZE);
1232 /* Re-establish the callbacks even if already set */
1233 xmlMemSetup(xml_pfree, xml_palloc, xml_repalloc, xml_pstrdup);
1236 static void
1237 xml_memory_cleanup(void)
1239 if (LibxmlContext != NULL)
1241 /* Give libxml a chance to clean up dangling pointers */
1242 xmlCleanupParser();
1244 /* And flush the context */
1245 MemoryContextDelete(LibxmlContext);
1246 LibxmlContext = NULL;
1251 * Wrappers for memory management functions
1253 static void *
1254 xml_palloc(size_t size)
1256 return MemoryContextAlloc(LibxmlContext, size);
1260 static void *
1261 xml_repalloc(void *ptr, size_t size)
1263 return repalloc(ptr, size);
1267 static void
1268 xml_pfree(void *ptr)
1270 pfree(ptr);
1274 static char *
1275 xml_pstrdup(const char *string)
1277 return MemoryContextStrdup(LibxmlContext, string);
1282 * Wrapper for "ereport" function for XML-related errors. The "msg"
1283 * is the SQL-level message; some can be adopted from the SQL/XML
1284 * standard. This function adds libxml's native error messages, if
1285 * any, as detail.
1287 static void
1288 xml_ereport(int level, int sqlcode, const char *msg)
1290 char *detail;
1292 if (xml_err_buf->len > 0)
1294 detail = pstrdup(xml_err_buf->data);
1295 resetStringInfo(xml_err_buf);
1297 else
1298 detail = NULL;
1300 /* libxml error messages end in '\n'; get rid of it */
1301 if (detail)
1303 size_t len;
1305 len = strlen(detail);
1306 if (len > 0 && detail[len - 1] == '\n')
1307 detail[len - 1] = '\0';
1309 ereport(level,
1310 (errcode(sqlcode),
1311 errmsg("%s", msg),
1312 errdetail("%s", detail)));
1314 else
1316 ereport(level,
1317 (errcode(sqlcode),
1318 errmsg("%s", msg)));
1324 * Error handler for libxml error messages
1326 static void
1327 xml_errorHandler(void *ctxt, const char *msg,...)
1329 /* Append the formatted text to xml_err_buf */
1330 for (;;)
1332 va_list args;
1333 bool success;
1335 /* Try to format the data. */
1336 va_start(args, msg);
1337 success = appendStringInfoVA(xml_err_buf, msg, args);
1338 va_end(args);
1340 if (success)
1341 break;
1343 /* Double the buffer size and try again. */
1344 enlargeStringInfo(xml_err_buf, xml_err_buf->maxlen);
1350 * Wrapper for "ereport" function for XML-related errors. The "msg"
1351 * is the SQL-level message; some can be adopted from the SQL/XML
1352 * standard. This function uses "code" to create a textual detail
1353 * message. At the moment, we only need to cover those codes that we
1354 * may raise in this file.
1356 static void
1357 xml_ereport_by_code(int level, int sqlcode,
1358 const char *msg, int code)
1360 const char *det;
1362 switch (code)
1364 case XML_ERR_INVALID_CHAR:
1365 det = gettext_noop("Invalid character value.");
1366 break;
1367 case XML_ERR_SPACE_REQUIRED:
1368 det = gettext_noop("Space required.");
1369 break;
1370 case XML_ERR_STANDALONE_VALUE:
1371 det = gettext_noop("standalone accepts only 'yes' or 'no'.");
1372 break;
1373 case XML_ERR_VERSION_MISSING:
1374 det = gettext_noop("Malformed declaration: missing version.");
1375 break;
1376 case XML_ERR_MISSING_ENCODING:
1377 det = gettext_noop("Missing encoding in text declaration.");
1378 break;
1379 case XML_ERR_XMLDECL_NOT_FINISHED:
1380 det = gettext_noop("Parsing XML declaration: '?>' expected.");
1381 break;
1382 default:
1383 det = gettext_noop("Unrecognized libxml error code: %d.");
1384 break;
1387 ereport(level,
1388 (errcode(sqlcode),
1389 errmsg("%s", msg),
1390 errdetail(det, code)));
1395 * Convert one char in the current server encoding to a Unicode codepoint.
1397 static pg_wchar
1398 sqlchar_to_unicode(char *s)
1400 char *utf8string;
1401 pg_wchar ret[2]; /* need space for trailing zero */
1403 utf8string = (char *) pg_do_encoding_conversion((unsigned char *) s,
1404 pg_mblen(s),
1405 GetDatabaseEncoding(),
1406 PG_UTF8);
1408 pg_encoding_mb2wchar_with_len(PG_UTF8, utf8string, ret,
1409 pg_encoding_mblen(PG_UTF8, utf8string));
1411 if (utf8string != s)
1412 pfree(utf8string);
1414 return ret[0];
1418 static bool
1419 is_valid_xml_namefirst(pg_wchar c)
1421 /* (Letter | '_' | ':') */
1422 return (xmlIsBaseCharQ(c) || xmlIsIdeographicQ(c)
1423 || c == '_' || c == ':');
1427 static bool
1428 is_valid_xml_namechar(pg_wchar c)
1430 /* Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender */
1431 return (xmlIsBaseCharQ(c) || xmlIsIdeographicQ(c)
1432 || xmlIsDigitQ(c)
1433 || c == '.' || c == '-' || c == '_' || c == ':'
1434 || xmlIsCombiningQ(c)
1435 || xmlIsExtenderQ(c));
1437 #endif /* USE_LIBXML */
1441 * Map SQL identifier to XML name; see SQL/XML:2003 section 9.1.
1443 char *
1444 map_sql_identifier_to_xml_name(char *ident, bool fully_escaped,
1445 bool escape_period)
1447 #ifdef USE_LIBXML
1448 StringInfoData buf;
1449 char *p;
1452 * SQL/XML doesn't make use of this case anywhere, so it's probably a
1453 * mistake.
1455 Assert(fully_escaped || !escape_period);
1457 initStringInfo(&buf);
1459 for (p = ident; *p; p += pg_mblen(p))
1461 if (*p == ':' && (p == ident || fully_escaped))
1462 appendStringInfo(&buf, "_x003A_");
1463 else if (*p == '_' && *(p + 1) == 'x')
1464 appendStringInfo(&buf, "_x005F_");
1465 else if (fully_escaped && p == ident &&
1466 pg_strncasecmp(p, "xml", 3) == 0)
1468 if (*p == 'x')
1469 appendStringInfo(&buf, "_x0078_");
1470 else
1471 appendStringInfo(&buf, "_x0058_");
1473 else if (escape_period && *p == '.')
1474 appendStringInfo(&buf, "_x002E_");
1475 else
1477 pg_wchar u = sqlchar_to_unicode(p);
1479 if ((p == ident)
1480 ? !is_valid_xml_namefirst(u)
1481 : !is_valid_xml_namechar(u))
1482 appendStringInfo(&buf, "_x%04X_", (unsigned int) u);
1483 else
1484 appendBinaryStringInfo(&buf, p, pg_mblen(p));
1488 return buf.data;
1489 #else /* not USE_LIBXML */
1490 NO_XML_SUPPORT();
1491 return NULL;
1492 #endif /* not USE_LIBXML */
1497 * Map a Unicode codepoint into the current server encoding.
1499 static char *
1500 unicode_to_sqlchar(pg_wchar c)
1502 unsigned char utf8string[5]; /* need room for trailing zero */
1503 char *result;
1505 memset(utf8string, 0, sizeof(utf8string));
1506 unicode_to_utf8(c, utf8string);
1508 result = (char *) pg_do_encoding_conversion(utf8string,
1509 pg_encoding_mblen(PG_UTF8,
1510 (char *) utf8string),
1511 PG_UTF8,
1512 GetDatabaseEncoding());
1513 /* if pg_do_encoding_conversion didn't strdup, we must */
1514 if (result == (char *) utf8string)
1515 result = pstrdup(result);
1516 return result;
1521 * Map XML name to SQL identifier; see SQL/XML:2003 section 9.17.
1523 char *
1524 map_xml_name_to_sql_identifier(char *name)
1526 StringInfoData buf;
1527 char *p;
1529 initStringInfo(&buf);
1531 for (p = name; *p; p += pg_mblen(p))
1533 if (*p == '_' && *(p + 1) == 'x'
1534 && isxdigit((unsigned char) *(p + 2))
1535 && isxdigit((unsigned char) *(p + 3))
1536 && isxdigit((unsigned char) *(p + 4))
1537 && isxdigit((unsigned char) *(p + 5))
1538 && *(p + 6) == '_')
1540 unsigned int u;
1542 sscanf(p + 2, "%X", &u);
1543 appendStringInfoString(&buf, unicode_to_sqlchar(u));
1544 p += 6;
1546 else
1547 appendBinaryStringInfo(&buf, p, pg_mblen(p));
1550 return buf.data;
1554 * Map SQL value to XML value; see SQL/XML:2003 section 9.16.
1556 char *
1557 map_sql_value_to_xml_value(Datum value, Oid type)
1559 StringInfoData buf;
1561 if (type_is_array(type))
1563 ArrayType *array;
1564 Oid elmtype;
1565 int16 elmlen;
1566 bool elmbyval;
1567 char elmalign;
1568 int num_elems;
1569 Datum *elem_values;
1570 bool *elem_nulls;
1571 int i;
1573 array = DatumGetArrayTypeP(value);
1574 elmtype = ARR_ELEMTYPE(array);
1575 get_typlenbyvalalign(elmtype, &elmlen, &elmbyval, &elmalign);
1577 deconstruct_array(array, elmtype,
1578 elmlen, elmbyval, elmalign,
1579 &elem_values, &elem_nulls,
1580 &num_elems);
1582 initStringInfo(&buf);
1584 for (i = 0; i < num_elems; i++)
1586 if (elem_nulls[i])
1587 continue;
1588 appendStringInfoString(&buf, "<element>");
1589 appendStringInfoString(&buf,
1590 map_sql_value_to_xml_value(elem_values[i],
1591 elmtype));
1592 appendStringInfoString(&buf, "</element>");
1595 pfree(elem_values);
1596 pfree(elem_nulls);
1598 return buf.data;
1600 else
1602 Oid typeOut;
1603 bool isvarlena;
1604 char *p,
1605 *str;
1608 * Special XSD formatting for some data types
1610 switch (type)
1612 case BOOLOID:
1613 if (DatumGetBool(value))
1614 return "true";
1615 else
1616 return "false";
1618 case DATEOID:
1620 DateADT date;
1621 struct pg_tm tm;
1622 char buf[MAXDATELEN + 1];
1624 date = DatumGetDateADT(value);
1625 /* XSD doesn't support infinite values */
1626 if (DATE_NOT_FINITE(date))
1627 ereport(ERROR,
1628 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1629 errmsg("date out of range")));
1630 j2date(date + POSTGRES_EPOCH_JDATE,
1631 &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
1632 EncodeDateOnly(&tm, USE_XSD_DATES, buf);
1634 return pstrdup(buf);
1637 case TIMESTAMPOID:
1639 Timestamp timestamp;
1640 struct pg_tm tm;
1641 fsec_t fsec;
1642 char *tzn = NULL;
1643 char buf[MAXDATELEN + 1];
1645 timestamp = DatumGetTimestamp(value);
1647 /* XSD doesn't support infinite values */
1648 if (TIMESTAMP_NOT_FINITE(timestamp))
1649 ereport(ERROR,
1650 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1651 errmsg("timestamp out of range")));
1652 else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
1653 EncodeDateTime(&tm, fsec, NULL, &tzn, USE_XSD_DATES, buf);
1654 else
1655 ereport(ERROR,
1656 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1657 errmsg("timestamp out of range")));
1659 return pstrdup(buf);
1662 case TIMESTAMPTZOID:
1664 TimestampTz timestamp;
1665 struct pg_tm tm;
1666 int tz;
1667 fsec_t fsec;
1668 char *tzn = NULL;
1669 char buf[MAXDATELEN + 1];
1671 timestamp = DatumGetTimestamp(value);
1673 /* XSD doesn't support infinite values */
1674 if (TIMESTAMP_NOT_FINITE(timestamp))
1675 ereport(ERROR,
1676 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1677 errmsg("timestamp out of range")));
1678 else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
1679 EncodeDateTime(&tm, fsec, &tz, &tzn, USE_XSD_DATES, buf);
1680 else
1681 ereport(ERROR,
1682 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1683 errmsg("timestamp out of range")));
1685 return pstrdup(buf);
1688 #ifdef USE_LIBXML
1689 case BYTEAOID:
1691 bytea *bstr = DatumGetByteaPP(value);
1692 xmlBufferPtr buf;
1693 xmlTextWriterPtr writer;
1694 char *result;
1696 xml_init();
1698 buf = xmlBufferCreate();
1699 writer = xmlNewTextWriterMemory(buf, 0);
1701 if (xmlbinary == XMLBINARY_BASE64)
1702 xmlTextWriterWriteBase64(writer, VARDATA_ANY(bstr),
1703 0, VARSIZE_ANY_EXHDR(bstr));
1704 else
1705 xmlTextWriterWriteBinHex(writer, VARDATA_ANY(bstr),
1706 0, VARSIZE_ANY_EXHDR(bstr));
1708 xmlFreeTextWriter(writer);
1709 result = pstrdup((const char *) xmlBufferContent(buf));
1710 xmlBufferFree(buf);
1711 return result;
1713 #endif /* USE_LIBXML */
1718 * otherwise, just use the type's native text representation
1720 getTypeOutputInfo(type, &typeOut, &isvarlena);
1721 str = OidOutputFunctionCall(typeOut, value);
1723 /* ... exactly as-is for XML */
1724 if (type == XMLOID)
1725 return str;
1727 /* otherwise, translate special characters as needed */
1728 initStringInfo(&buf);
1730 for (p = str; *p; p++)
1732 switch (*p)
1734 case '&':
1735 appendStringInfoString(&buf, "&amp;");
1736 break;
1737 case '<':
1738 appendStringInfoString(&buf, "&lt;");
1739 break;
1740 case '>':
1741 appendStringInfoString(&buf, "&gt;");
1742 break;
1743 case '\r':
1744 appendStringInfoString(&buf, "&#x0d;");
1745 break;
1746 default:
1747 appendStringInfoCharMacro(&buf, *p);
1748 break;
1752 return buf.data;
1757 static char *
1758 _SPI_strdup(const char *s)
1760 size_t len = strlen(s) + 1;
1761 char *ret = SPI_palloc(len);
1763 memcpy(ret, s, len);
1764 return ret;
1769 * SQL to XML mapping functions
1771 * What follows below is intentionally organized so that you can read
1772 * along in the SQL/XML:2003 standard. The functions are mostly split
1773 * up and ordered they way the clauses lay out in the standards
1774 * document, and the identifiers are also aligned with the standard
1775 * text. (SQL/XML:2006 appears to be ordered differently,
1776 * unfortunately.)
1778 * There are many things going on there:
1780 * There are two kinds of mappings: Mapping SQL data (table contents)
1781 * to XML documents, and mapping SQL structure (the "schema") to XML
1782 * Schema. And there are functions that do both at the same time.
1784 * Then you can map a database, a schema, or a table, each in both
1785 * ways. This breaks down recursively: Mapping a database invokes
1786 * mapping schemas, which invokes mapping tables, which invokes
1787 * mapping rows, which invokes mapping columns, although you can't
1788 * call the last two from the outside. Because of this, there are a
1789 * number of xyz_internal() functions which are to be called both from
1790 * the function manager wrapper and from some upper layer in a
1791 * recursive call.
1793 * See the documentation about what the common function arguments
1794 * nulls, tableforest, and targetns mean.
1796 * Some style guidelines for XML output: Use double quotes for quoting
1797 * XML attributes. Indent XML elements by two spaces, but remember
1798 * that a lot of code is called recursively at different levels, so
1799 * it's better not to indent rather than create output that indents
1800 * and outdents weirdly. Add newlines to make the output look nice.
1805 * Visibility of objects for XML mappings; see SQL/XML:2003 section
1806 * 4.8.5.
1810 * Given a query, which must return type oid as first column, produce
1811 * a list of Oids with the query results.
1813 static List *
1814 query_to_oid_list(const char *query)
1816 int i;
1817 List *list = NIL;
1819 SPI_execute(query, true, 0);
1821 for (i = 0; i < SPI_processed; i++)
1823 Datum oid;
1824 bool isnull;
1826 oid = SPI_getbinval(SPI_tuptable->vals[i],
1827 SPI_tuptable->tupdesc,
1829 &isnull);
1830 if (!isnull)
1831 list = lappend_oid(list, DatumGetObjectId(oid));
1834 return list;
1838 static List *
1839 schema_get_xml_visible_tables(Oid nspid)
1841 StringInfoData query;
1843 initStringInfo(&query);
1844 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);
1846 return query_to_oid_list(query.data);
1851 * Including the system schemas is probably not useful for a database
1852 * mapping.
1854 #define XML_VISIBLE_SCHEMAS_EXCLUDE "(nspname ~ '^pg_' OR nspname = 'information_schema')"
1856 #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
1859 static List *
1860 database_get_xml_visible_schemas(void)
1862 return query_to_oid_list(XML_VISIBLE_SCHEMAS " ORDER BY nspname;");
1866 static List *
1867 database_get_xml_visible_tables(void)
1869 /* At the moment there is no order required here. */
1870 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 ");");
1875 * Map SQL table to XML and/or XML Schema document; see SQL/XML:2003
1876 * section 9.3.
1879 static StringInfo
1880 table_to_xml_internal(Oid relid,
1881 const char *xmlschema, bool nulls, bool tableforest,
1882 const char *targetns, bool top_level)
1884 StringInfoData query;
1886 initStringInfo(&query);
1887 appendStringInfo(&query, "SELECT * FROM %s",
1888 DatumGetCString(DirectFunctionCall1(regclassout,
1889 ObjectIdGetDatum(relid))));
1890 return query_to_xml_internal(query.data, get_rel_name(relid),
1891 xmlschema, nulls, tableforest,
1892 targetns, top_level);
1896 Datum
1897 table_to_xml(PG_FUNCTION_ARGS)
1899 Oid relid = PG_GETARG_OID(0);
1900 bool nulls = PG_GETARG_BOOL(1);
1901 bool tableforest = PG_GETARG_BOOL(2);
1902 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
1904 PG_RETURN_XML_P(stringinfo_to_xmltype(table_to_xml_internal(relid, NULL,
1905 nulls, tableforest,
1906 targetns, true)));
1910 Datum
1911 query_to_xml(PG_FUNCTION_ARGS)
1913 char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
1914 bool nulls = PG_GETARG_BOOL(1);
1915 bool tableforest = PG_GETARG_BOOL(2);
1916 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
1918 PG_RETURN_XML_P(stringinfo_to_xmltype(query_to_xml_internal(query, NULL,
1919 NULL, nulls, tableforest,
1920 targetns, true)));
1924 Datum
1925 cursor_to_xml(PG_FUNCTION_ARGS)
1927 char *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
1928 int32 count = PG_GETARG_INT32(1);
1929 bool nulls = PG_GETARG_BOOL(2);
1930 bool tableforest = PG_GETARG_BOOL(3);
1931 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(4));
1933 StringInfoData result;
1934 Portal portal;
1935 int i;
1937 initStringInfo(&result);
1939 SPI_connect();
1940 portal = SPI_cursor_find(name);
1941 if (portal == NULL)
1942 ereport(ERROR,
1943 (errcode(ERRCODE_UNDEFINED_CURSOR),
1944 errmsg("cursor \"%s\" does not exist", name)));
1946 SPI_cursor_fetch(portal, true, count);
1947 for (i = 0; i < SPI_processed; i++)
1948 SPI_sql_row_to_xmlelement(i, &result, NULL, nulls,
1949 tableforest, targetns, true);
1951 SPI_finish();
1953 PG_RETURN_XML_P(stringinfo_to_xmltype(&result));
1958 * Write the start tag of the root element of a data mapping.
1960 * top_level means that this is the very top level of the eventual
1961 * output. For example, when the user calls table_to_xml, then a call
1962 * with a table name to this function is the top level. When the user
1963 * calls database_to_xml, then a call with a schema name to this
1964 * function is not the top level. If top_level is false, then the XML
1965 * namespace declarations are omitted, because they supposedly already
1966 * appeared earlier in the output. Repeating them is not wrong, but
1967 * it looks ugly.
1969 static void
1970 xmldata_root_element_start(StringInfo result, const char *eltname,
1971 const char *xmlschema, const char *targetns,
1972 bool top_level)
1974 /* This isn't really wrong but currently makes no sense. */
1975 Assert(top_level || !xmlschema);
1977 appendStringInfo(result, "<%s", eltname);
1978 if (top_level)
1980 appendStringInfoString(result, " xmlns:xsi=\"" NAMESPACE_XSI "\"");
1981 if (strlen(targetns) > 0)
1982 appendStringInfo(result, " xmlns=\"%s\"", targetns);
1984 if (xmlschema)
1986 /* FIXME: better targets */
1987 if (strlen(targetns) > 0)
1988 appendStringInfo(result, " xsi:schemaLocation=\"%s #\"", targetns);
1989 else
1990 appendStringInfo(result, " xsi:noNamespaceSchemaLocation=\"#\"");
1992 appendStringInfo(result, ">\n\n");
1996 static void
1997 xmldata_root_element_end(StringInfo result, const char *eltname)
1999 appendStringInfo(result, "</%s>\n", eltname);
2003 static StringInfo
2004 query_to_xml_internal(const char *query, char *tablename,
2005 const char *xmlschema, bool nulls, bool tableforest,
2006 const char *targetns, bool top_level)
2008 StringInfo result;
2009 char *xmltn;
2010 int i;
2012 if (tablename)
2013 xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
2014 else
2015 xmltn = "table";
2017 result = makeStringInfo();
2019 SPI_connect();
2020 if (SPI_execute(query, true, 0) != SPI_OK_SELECT)
2021 ereport(ERROR,
2022 (errcode(ERRCODE_DATA_EXCEPTION),
2023 errmsg("invalid query")));
2025 if (!tableforest)
2026 xmldata_root_element_start(result, xmltn, xmlschema,
2027 targetns, top_level);
2029 if (xmlschema)
2030 appendStringInfo(result, "%s\n\n", xmlschema);
2032 for (i = 0; i < SPI_processed; i++)
2033 SPI_sql_row_to_xmlelement(i, result, tablename, nulls,
2034 tableforest, targetns, top_level);
2036 if (!tableforest)
2037 xmldata_root_element_end(result, xmltn);
2039 SPI_finish();
2041 return result;
2045 Datum
2046 table_to_xmlschema(PG_FUNCTION_ARGS)
2048 Oid relid = PG_GETARG_OID(0);
2049 bool nulls = PG_GETARG_BOOL(1);
2050 bool tableforest = PG_GETARG_BOOL(2);
2051 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2052 const char *result;
2053 Relation rel;
2055 rel = heap_open(relid, AccessShareLock);
2056 result = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2057 tableforest, targetns);
2058 heap_close(rel, NoLock);
2060 PG_RETURN_XML_P(cstring_to_xmltype(result));
2064 Datum
2065 query_to_xmlschema(PG_FUNCTION_ARGS)
2067 char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2068 bool nulls = PG_GETARG_BOOL(1);
2069 bool tableforest = PG_GETARG_BOOL(2);
2070 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2071 const char *result;
2072 SPIPlanPtr plan;
2073 Portal portal;
2075 SPI_connect();
2077 if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
2078 elog(ERROR, "SPI_prepare(\"%s\") failed", query);
2080 if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
2081 elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
2083 result = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2084 InvalidOid, nulls,
2085 tableforest, targetns));
2086 SPI_cursor_close(portal);
2087 SPI_finish();
2089 PG_RETURN_XML_P(cstring_to_xmltype(result));
2093 Datum
2094 cursor_to_xmlschema(PG_FUNCTION_ARGS)
2096 char *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
2097 bool nulls = PG_GETARG_BOOL(1);
2098 bool tableforest = PG_GETARG_BOOL(2);
2099 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2100 const char *xmlschema;
2101 Portal portal;
2103 SPI_connect();
2104 portal = SPI_cursor_find(name);
2105 if (portal == NULL)
2106 ereport(ERROR,
2107 (errcode(ERRCODE_UNDEFINED_CURSOR),
2108 errmsg("cursor \"%s\" does not exist", name)));
2110 xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2111 InvalidOid, nulls,
2112 tableforest, targetns));
2113 SPI_finish();
2115 PG_RETURN_XML_P(cstring_to_xmltype(xmlschema));
2119 Datum
2120 table_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2122 Oid relid = PG_GETARG_OID(0);
2123 bool nulls = PG_GETARG_BOOL(1);
2124 bool tableforest = PG_GETARG_BOOL(2);
2125 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2126 Relation rel;
2127 const char *xmlschema;
2129 rel = heap_open(relid, AccessShareLock);
2130 xmlschema = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2131 tableforest, targetns);
2132 heap_close(rel, NoLock);
2134 PG_RETURN_XML_P(stringinfo_to_xmltype(table_to_xml_internal(relid,
2135 xmlschema, nulls, tableforest,
2136 targetns, true)));
2140 Datum
2141 query_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2143 char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2144 bool nulls = PG_GETARG_BOOL(1);
2145 bool tableforest = PG_GETARG_BOOL(2);
2146 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2148 const char *xmlschema;
2149 SPIPlanPtr plan;
2150 Portal portal;
2152 SPI_connect();
2154 if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
2155 elog(ERROR, "SPI_prepare(\"%s\") failed", query);
2157 if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
2158 elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
2160 xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2161 InvalidOid, nulls, tableforest, targetns));
2162 SPI_cursor_close(portal);
2163 SPI_finish();
2165 PG_RETURN_XML_P(stringinfo_to_xmltype(query_to_xml_internal(query, NULL,
2166 xmlschema, nulls, tableforest,
2167 targetns, true)));
2172 * Map SQL schema to XML and/or XML Schema document; see SQL/XML:2003
2173 * section 9.4.
2176 static StringInfo
2177 schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls,
2178 bool tableforest, const char *targetns, bool top_level)
2180 StringInfo result;
2181 char *xmlsn;
2182 List *relid_list;
2183 ListCell *cell;
2185 xmlsn = map_sql_identifier_to_xml_name(get_namespace_name(nspid),
2186 true, false);
2187 result = makeStringInfo();
2189 xmldata_root_element_start(result, xmlsn, xmlschema, targetns, top_level);
2191 if (xmlschema)
2192 appendStringInfo(result, "%s\n\n", xmlschema);
2194 SPI_connect();
2196 relid_list = schema_get_xml_visible_tables(nspid);
2198 SPI_push();
2200 foreach(cell, relid_list)
2202 Oid relid = lfirst_oid(cell);
2203 StringInfo subres;
2205 subres = table_to_xml_internal(relid, NULL, nulls, tableforest,
2206 targetns, false);
2208 appendStringInfoString(result, subres->data);
2209 appendStringInfoChar(result, '\n');
2212 SPI_pop();
2213 SPI_finish();
2215 xmldata_root_element_end(result, xmlsn);
2217 return result;
2221 Datum
2222 schema_to_xml(PG_FUNCTION_ARGS)
2224 Name name = PG_GETARG_NAME(0);
2225 bool nulls = PG_GETARG_BOOL(1);
2226 bool tableforest = PG_GETARG_BOOL(2);
2227 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2229 char *schemaname;
2230 Oid nspid;
2232 schemaname = NameStr(*name);
2233 nspid = LookupExplicitNamespace(schemaname);
2235 PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xml_internal(nspid, NULL,
2236 nulls, tableforest, targetns, true)));
2241 * Write the start element of the root element of an XML Schema mapping.
2243 static void
2244 xsd_schema_element_start(StringInfo result, const char *targetns)
2246 appendStringInfoString(result,
2247 "<xsd:schema\n"
2248 " xmlns:xsd=\"" NAMESPACE_XSD "\"");
2249 if (strlen(targetns) > 0)
2250 appendStringInfo(result,
2251 "\n"
2252 " targetNamespace=\"%s\"\n"
2253 " elementFormDefault=\"qualified\"",
2254 targetns);
2255 appendStringInfoString(result,
2256 ">\n\n");
2260 static void
2261 xsd_schema_element_end(StringInfo result)
2263 appendStringInfoString(result, "</xsd:schema>");
2267 static StringInfo
2268 schema_to_xmlschema_internal(const char *schemaname, bool nulls,
2269 bool tableforest, const char *targetns)
2271 Oid nspid;
2272 List *relid_list;
2273 List *tupdesc_list;
2274 ListCell *cell;
2275 StringInfo result;
2277 result = makeStringInfo();
2279 nspid = LookupExplicitNamespace(schemaname);
2281 xsd_schema_element_start(result, targetns);
2283 SPI_connect();
2285 relid_list = schema_get_xml_visible_tables(nspid);
2287 tupdesc_list = NIL;
2288 foreach(cell, relid_list)
2290 Relation rel;
2292 rel = heap_open(lfirst_oid(cell), AccessShareLock);
2293 tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2294 heap_close(rel, NoLock);
2297 appendStringInfoString(result,
2298 map_sql_typecoll_to_xmlschema_types(tupdesc_list));
2300 appendStringInfoString(result,
2301 map_sql_schema_to_xmlschema_types(nspid, relid_list,
2302 nulls, tableforest, targetns));
2304 xsd_schema_element_end(result);
2306 SPI_finish();
2308 return result;
2312 Datum
2313 schema_to_xmlschema(PG_FUNCTION_ARGS)
2315 Name name = PG_GETARG_NAME(0);
2316 bool nulls = PG_GETARG_BOOL(1);
2317 bool tableforest = PG_GETARG_BOOL(2);
2318 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2320 PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xmlschema_internal(NameStr(*name),
2321 nulls, tableforest, targetns)));
2325 Datum
2326 schema_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2328 Name name = PG_GETARG_NAME(0);
2329 bool nulls = PG_GETARG_BOOL(1);
2330 bool tableforest = PG_GETARG_BOOL(2);
2331 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2332 char *schemaname;
2333 Oid nspid;
2334 StringInfo xmlschema;
2336 schemaname = NameStr(*name);
2337 nspid = LookupExplicitNamespace(schemaname);
2339 xmlschema = schema_to_xmlschema_internal(schemaname, nulls,
2340 tableforest, targetns);
2342 PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xml_internal(nspid,
2343 xmlschema->data, nulls,
2344 tableforest, targetns, true)));
2349 * Map SQL database to XML and/or XML Schema document; see SQL/XML:2003
2350 * section 9.5.
2353 static StringInfo
2354 database_to_xml_internal(const char *xmlschema, bool nulls,
2355 bool tableforest, const char *targetns)
2357 StringInfo result;
2358 List *nspid_list;
2359 ListCell *cell;
2360 char *xmlcn;
2362 xmlcn = map_sql_identifier_to_xml_name(get_database_name(MyDatabaseId),
2363 true, false);
2364 result = makeStringInfo();
2366 xmldata_root_element_start(result, xmlcn, xmlschema, targetns, true);
2368 if (xmlschema)
2369 appendStringInfo(result, "%s\n\n", xmlschema);
2371 SPI_connect();
2373 nspid_list = database_get_xml_visible_schemas();
2375 SPI_push();
2377 foreach(cell, nspid_list)
2379 Oid nspid = lfirst_oid(cell);
2380 StringInfo subres;
2382 subres = schema_to_xml_internal(nspid, NULL, nulls,
2383 tableforest, targetns, false);
2385 appendStringInfoString(result, subres->data);
2386 appendStringInfoChar(result, '\n');
2389 SPI_pop();
2390 SPI_finish();
2392 xmldata_root_element_end(result, xmlcn);
2394 return result;
2398 Datum
2399 database_to_xml(PG_FUNCTION_ARGS)
2401 bool nulls = PG_GETARG_BOOL(0);
2402 bool tableforest = PG_GETARG_BOOL(1);
2403 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
2405 PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xml_internal(NULL, nulls,
2406 tableforest, targetns)));
2410 static StringInfo
2411 database_to_xmlschema_internal(bool nulls, bool tableforest,
2412 const char *targetns)
2414 List *relid_list;
2415 List *nspid_list;
2416 List *tupdesc_list;
2417 ListCell *cell;
2418 StringInfo result;
2420 result = makeStringInfo();
2422 xsd_schema_element_start(result, targetns);
2424 SPI_connect();
2426 relid_list = database_get_xml_visible_tables();
2427 nspid_list = database_get_xml_visible_schemas();
2429 tupdesc_list = NIL;
2430 foreach(cell, relid_list)
2432 Relation rel;
2434 rel = heap_open(lfirst_oid(cell), AccessShareLock);
2435 tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2436 heap_close(rel, NoLock);
2439 appendStringInfoString(result,
2440 map_sql_typecoll_to_xmlschema_types(tupdesc_list));
2442 appendStringInfoString(result,
2443 map_sql_catalog_to_xmlschema_types(nspid_list, nulls, tableforest, targetns));
2445 xsd_schema_element_end(result);
2447 SPI_finish();
2449 return result;
2453 Datum
2454 database_to_xmlschema(PG_FUNCTION_ARGS)
2456 bool nulls = PG_GETARG_BOOL(0);
2457 bool tableforest = PG_GETARG_BOOL(1);
2458 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
2460 PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xmlschema_internal(nulls,
2461 tableforest, targetns)));
2465 Datum
2466 database_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2468 bool nulls = PG_GETARG_BOOL(0);
2469 bool tableforest = PG_GETARG_BOOL(1);
2470 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
2471 StringInfo xmlschema;
2473 xmlschema = database_to_xmlschema_internal(nulls, tableforest, targetns);
2475 PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xml_internal(xmlschema->data,
2476 nulls, tableforest, targetns)));
2481 * Map a multi-part SQL name to an XML name; see SQL/XML:2003 section
2482 * 9.2.
2484 static char *
2485 map_multipart_sql_identifier_to_xml_name(char *a, char *b, char *c, char *d)
2487 StringInfoData result;
2489 initStringInfo(&result);
2491 if (a)
2492 appendStringInfo(&result, "%s",
2493 map_sql_identifier_to_xml_name(a, true, true));
2494 if (b)
2495 appendStringInfo(&result, ".%s",
2496 map_sql_identifier_to_xml_name(b, true, true));
2497 if (c)
2498 appendStringInfo(&result, ".%s",
2499 map_sql_identifier_to_xml_name(c, true, true));
2500 if (d)
2501 appendStringInfo(&result, ".%s",
2502 map_sql_identifier_to_xml_name(d, true, true));
2504 return result.data;
2509 * Map an SQL table to an XML Schema document; see SQL/XML:2003
2510 * section 9.3.
2512 * Map an SQL table to XML Schema data types; see SQL/XML:2003 section
2513 * 9.6.
2515 static const char *
2516 map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls,
2517 bool tableforest, const char *targetns)
2519 int i;
2520 char *xmltn;
2521 char *tabletypename;
2522 char *rowtypename;
2523 StringInfoData result;
2525 initStringInfo(&result);
2527 if (OidIsValid(relid))
2529 HeapTuple tuple;
2530 Form_pg_class reltuple;
2532 tuple = SearchSysCache(RELOID,
2533 ObjectIdGetDatum(relid),
2534 0, 0, 0);
2535 if (!HeapTupleIsValid(tuple))
2536 elog(ERROR, "cache lookup failed for relation %u", relid);
2537 reltuple = (Form_pg_class) GETSTRUCT(tuple);
2539 xmltn = map_sql_identifier_to_xml_name(NameStr(reltuple->relname),
2540 true, false);
2542 tabletypename = map_multipart_sql_identifier_to_xml_name("TableType",
2543 get_database_name(MyDatabaseId),
2544 get_namespace_name(reltuple->relnamespace),
2545 NameStr(reltuple->relname));
2547 rowtypename = map_multipart_sql_identifier_to_xml_name("RowType",
2548 get_database_name(MyDatabaseId),
2549 get_namespace_name(reltuple->relnamespace),
2550 NameStr(reltuple->relname));
2552 ReleaseSysCache(tuple);
2554 else
2556 if (tableforest)
2557 xmltn = "row";
2558 else
2559 xmltn = "table";
2561 tabletypename = "TableType";
2562 rowtypename = "RowType";
2565 xsd_schema_element_start(&result, targetns);
2567 appendStringInfoString(&result,
2568 map_sql_typecoll_to_xmlschema_types(list_make1(tupdesc)));
2570 appendStringInfo(&result,
2571 "<xsd:complexType name=\"%s\">\n"
2572 " <xsd:sequence>\n",
2573 rowtypename);
2575 for (i = 0; i < tupdesc->natts; i++)
2576 appendStringInfo(&result,
2577 " <xsd:element name=\"%s\" type=\"%s\"%s></xsd:element>\n",
2578 map_sql_identifier_to_xml_name(NameStr(tupdesc->attrs[i]->attname),
2579 true, false),
2580 map_sql_type_to_xml_name(tupdesc->attrs[i]->atttypid, -1),
2581 nulls ? " nillable=\"true\"" : " minOccurs=\"0\"");
2583 appendStringInfoString(&result,
2584 " </xsd:sequence>\n"
2585 "</xsd:complexType>\n\n");
2587 if (!tableforest)
2589 appendStringInfo(&result,
2590 "<xsd:complexType name=\"%s\">\n"
2591 " <xsd:sequence>\n"
2592 " <xsd:element name=\"row\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n"
2593 " </xsd:sequence>\n"
2594 "</xsd:complexType>\n\n",
2595 tabletypename, rowtypename);
2597 appendStringInfo(&result,
2598 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2599 xmltn, tabletypename);
2601 else
2602 appendStringInfo(&result,
2603 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2604 xmltn, rowtypename);
2606 xsd_schema_element_end(&result);
2608 return result.data;
2613 * Map an SQL schema to XML Schema data types; see SQL/XML section
2614 * 9.7.
2616 static const char *
2617 map_sql_schema_to_xmlschema_types(Oid nspid, List *relid_list, bool nulls,
2618 bool tableforest, const char *targetns)
2620 char *dbname;
2621 char *nspname;
2622 char *xmlsn;
2623 char *schematypename;
2624 StringInfoData result;
2625 ListCell *cell;
2627 dbname = get_database_name(MyDatabaseId);
2628 nspname = get_namespace_name(nspid);
2630 initStringInfo(&result);
2632 xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
2634 schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
2635 dbname,
2636 nspname,
2637 NULL);
2639 appendStringInfo(&result,
2640 "<xsd:complexType name=\"%s\">\n", schematypename);
2641 if (!tableforest)
2642 appendStringInfoString(&result,
2643 " <xsd:all>\n");
2644 else
2645 appendStringInfoString(&result,
2646 " <xsd:sequence>\n");
2648 foreach(cell, relid_list)
2650 Oid relid = lfirst_oid(cell);
2651 char *relname = get_rel_name(relid);
2652 char *xmltn = map_sql_identifier_to_xml_name(relname, true, false);
2653 char *tabletypename = map_multipart_sql_identifier_to_xml_name(tableforest ? "RowType" : "TableType",
2654 dbname,
2655 nspname,
2656 relname);
2658 if (!tableforest)
2659 appendStringInfo(&result,
2660 " <xsd:element name=\"%s\" type=\"%s\"/>\n",
2661 xmltn, tabletypename);
2662 else
2663 appendStringInfo(&result,
2664 " <xsd:element name=\"%s\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n",
2665 xmltn, tabletypename);
2668 if (!tableforest)
2669 appendStringInfoString(&result,
2670 " </xsd:all>\n");
2671 else
2672 appendStringInfoString(&result,
2673 " </xsd:sequence>\n");
2674 appendStringInfoString(&result,
2675 "</xsd:complexType>\n\n");
2677 appendStringInfo(&result,
2678 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2679 xmlsn, schematypename);
2681 return result.data;
2686 * Map an SQL catalog to XML Schema data types; see SQL/XML section
2687 * 9.8.
2689 static const char *
2690 map_sql_catalog_to_xmlschema_types(List *nspid_list, bool nulls,
2691 bool tableforest, const char *targetns)
2693 char *dbname;
2694 char *xmlcn;
2695 char *catalogtypename;
2696 StringInfoData result;
2697 ListCell *cell;
2699 dbname = get_database_name(MyDatabaseId);
2701 initStringInfo(&result);
2703 xmlcn = map_sql_identifier_to_xml_name(dbname, true, false);
2705 catalogtypename = map_multipart_sql_identifier_to_xml_name("CatalogType",
2706 dbname,
2707 NULL,
2708 NULL);
2710 appendStringInfo(&result,
2711 "<xsd:complexType name=\"%s\">\n", catalogtypename);
2712 appendStringInfoString(&result,
2713 " <xsd:all>\n");
2715 foreach(cell, nspid_list)
2717 Oid nspid = lfirst_oid(cell);
2718 char *nspname = get_namespace_name(nspid);
2719 char *xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
2720 char *schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
2721 dbname,
2722 nspname,
2723 NULL);
2725 appendStringInfo(&result,
2726 " <xsd:element name=\"%s\" type=\"%s\"/>\n",
2727 xmlsn, schematypename);
2730 appendStringInfoString(&result,
2731 " </xsd:all>\n");
2732 appendStringInfoString(&result,
2733 "</xsd:complexType>\n\n");
2735 appendStringInfo(&result,
2736 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2737 xmlcn, catalogtypename);
2739 return result.data;
2744 * Map an SQL data type to an XML name; see SQL/XML:2003 section 9.9.
2746 static const char *
2747 map_sql_type_to_xml_name(Oid typeoid, int typmod)
2749 StringInfoData result;
2751 initStringInfo(&result);
2753 switch (typeoid)
2755 case BPCHAROID:
2756 if (typmod == -1)
2757 appendStringInfo(&result, "CHAR");
2758 else
2759 appendStringInfo(&result, "CHAR_%d", typmod - VARHDRSZ);
2760 break;
2761 case VARCHAROID:
2762 if (typmod == -1)
2763 appendStringInfo(&result, "VARCHAR");
2764 else
2765 appendStringInfo(&result, "VARCHAR_%d", typmod - VARHDRSZ);
2766 break;
2767 case NUMERICOID:
2768 if (typmod == -1)
2769 appendStringInfo(&result, "NUMERIC");
2770 else
2771 appendStringInfo(&result, "NUMERIC_%d_%d",
2772 ((typmod - VARHDRSZ) >> 16) & 0xffff,
2773 (typmod - VARHDRSZ) & 0xffff);
2774 break;
2775 case INT4OID:
2776 appendStringInfo(&result, "INTEGER");
2777 break;
2778 case INT2OID:
2779 appendStringInfo(&result, "SMALLINT");
2780 break;
2781 case INT8OID:
2782 appendStringInfo(&result, "BIGINT");
2783 break;
2784 case FLOAT4OID:
2785 appendStringInfo(&result, "REAL");
2786 break;
2787 case FLOAT8OID:
2788 appendStringInfo(&result, "DOUBLE");
2789 break;
2790 case BOOLOID:
2791 appendStringInfo(&result, "BOOLEAN");
2792 break;
2793 case TIMEOID:
2794 if (typmod == -1)
2795 appendStringInfo(&result, "TIME");
2796 else
2797 appendStringInfo(&result, "TIME_%d", typmod);
2798 break;
2799 case TIMETZOID:
2800 if (typmod == -1)
2801 appendStringInfo(&result, "TIME_WTZ");
2802 else
2803 appendStringInfo(&result, "TIME_WTZ_%d", typmod);
2804 break;
2805 case TIMESTAMPOID:
2806 if (typmod == -1)
2807 appendStringInfo(&result, "TIMESTAMP");
2808 else
2809 appendStringInfo(&result, "TIMESTAMP_%d", typmod);
2810 break;
2811 case TIMESTAMPTZOID:
2812 if (typmod == -1)
2813 appendStringInfo(&result, "TIMESTAMP_WTZ");
2814 else
2815 appendStringInfo(&result, "TIMESTAMP_WTZ_%d", typmod);
2816 break;
2817 case DATEOID:
2818 appendStringInfo(&result, "DATE");
2819 break;
2820 case XMLOID:
2821 appendStringInfo(&result, "XML");
2822 break;
2823 default:
2825 HeapTuple tuple;
2826 Form_pg_type typtuple;
2828 tuple = SearchSysCache(TYPEOID,
2829 ObjectIdGetDatum(typeoid),
2830 0, 0, 0);
2831 if (!HeapTupleIsValid(tuple))
2832 elog(ERROR, "cache lookup failed for type %u", typeoid);
2833 typtuple = (Form_pg_type) GETSTRUCT(tuple);
2835 appendStringInfoString(&result,
2836 map_multipart_sql_identifier_to_xml_name((typtuple->typtype == TYPTYPE_DOMAIN) ? "Domain" : "UDT",
2837 get_database_name(MyDatabaseId),
2838 get_namespace_name(typtuple->typnamespace),
2839 NameStr(typtuple->typname)));
2841 ReleaseSysCache(tuple);
2845 return result.data;
2850 * Map a collection of SQL data types to XML Schema data types; see
2851 * SQL/XML:2002 section 9.10.
2853 static const char *
2854 map_sql_typecoll_to_xmlschema_types(List *tupdesc_list)
2856 List *uniquetypes = NIL;
2857 int i;
2858 StringInfoData result;
2859 ListCell *cell0;
2861 /* extract all column types used in the set of TupleDescs */
2862 foreach(cell0, tupdesc_list)
2864 TupleDesc tupdesc = (TupleDesc) lfirst(cell0);
2866 for (i = 0; i < tupdesc->natts; i++)
2868 if (tupdesc->attrs[i]->attisdropped)
2869 continue;
2870 uniquetypes = list_append_unique_oid(uniquetypes,
2871 tupdesc->attrs[i]->atttypid);
2875 /* add base types of domains */
2876 foreach(cell0, uniquetypes)
2878 Oid typid = lfirst_oid(cell0);
2879 Oid basetypid = getBaseType(typid);
2881 if (basetypid != typid)
2882 uniquetypes = list_append_unique_oid(uniquetypes, basetypid);
2885 /* Convert to textual form */
2886 initStringInfo(&result);
2888 foreach(cell0, uniquetypes)
2890 appendStringInfo(&result, "%s\n",
2891 map_sql_type_to_xmlschema_type(lfirst_oid(cell0),
2892 -1));
2895 return result.data;
2900 * Map an SQL data type to a named XML Schema data type; see SQL/XML
2901 * sections 9.11 and 9.15.
2903 * (The distinction between 9.11 and 9.15 is basically that 9.15 adds
2904 * a name attribute, which this function does. The name-less version
2905 * 9.11 doesn't appear to be required anywhere.)
2907 static const char *
2908 map_sql_type_to_xmlschema_type(Oid typeoid, int typmod)
2910 StringInfoData result;
2911 const char *typename = map_sql_type_to_xml_name(typeoid, typmod);
2913 initStringInfo(&result);
2915 if (typeoid == XMLOID)
2917 appendStringInfo(&result,
2918 "<xsd:complexType mixed=\"true\">\n"
2919 " <xsd:sequence>\n"
2920 " <xsd:any name=\"element\" minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>\n"
2921 " </xsd:sequence>\n"
2922 "</xsd:complexType>\n");
2924 else
2926 appendStringInfo(&result,
2927 "<xsd:simpleType name=\"%s\">\n", typename);
2929 switch (typeoid)
2931 case BPCHAROID:
2932 case VARCHAROID:
2933 case TEXTOID:
2934 appendStringInfo(&result,
2935 " <xsd:restriction base=\"xsd:string\">\n");
2936 if (typmod != -1)
2937 appendStringInfo(&result,
2938 " <xsd:maxLength value=\"%d\"/>\n",
2939 typmod - VARHDRSZ);
2940 appendStringInfo(&result,
2941 " </xsd:restriction>\n");
2942 break;
2944 case BYTEAOID:
2945 appendStringInfo(&result,
2946 " <xsd:restriction base=\"xsd:%s\">\n"
2947 " </xsd:restriction>\n",
2948 xmlbinary == XMLBINARY_BASE64 ? "base64Binary" : "hexBinary");
2949 break;
2951 case NUMERICOID:
2952 if (typmod != -1)
2953 appendStringInfo(&result,
2954 " <xsd:restriction base=\"xsd:decimal\">\n"
2955 " <xsd:totalDigits value=\"%d\"/>\n"
2956 " <xsd:fractionDigits value=\"%d\"/>\n"
2957 " </xsd:restriction>\n",
2958 ((typmod - VARHDRSZ) >> 16) & 0xffff,
2959 (typmod - VARHDRSZ) & 0xffff);
2960 break;
2962 case INT2OID:
2963 appendStringInfo(&result,
2964 " <xsd:restriction base=\"xsd:short\">\n"
2965 " <xsd:maxInclusive value=\"%d\"/>\n"
2966 " <xsd:minInclusive value=\"%d\"/>\n"
2967 " </xsd:restriction>\n",
2968 SHRT_MAX, SHRT_MIN);
2969 break;
2971 case INT4OID:
2972 appendStringInfo(&result,
2973 " <xsd:restriction base=\"xsd:int\">\n"
2974 " <xsd:maxInclusive value=\"%d\"/>\n"
2975 " <xsd:minInclusive value=\"%d\"/>\n"
2976 " </xsd:restriction>\n",
2977 INT_MAX, INT_MIN);
2978 break;
2980 case INT8OID:
2981 appendStringInfo(&result,
2982 " <xsd:restriction base=\"xsd:long\">\n"
2983 " <xsd:maxInclusive value=\"" INT64_FORMAT "\"/>\n"
2984 " <xsd:minInclusive value=\"" INT64_FORMAT "\"/>\n"
2985 " </xsd:restriction>\n",
2986 (((uint64) 1) << (sizeof(int64) * 8 - 1)) - 1,
2987 (((uint64) 1) << (sizeof(int64) * 8 - 1)));
2988 break;
2990 case FLOAT4OID:
2991 appendStringInfo(&result,
2992 " <xsd:restriction base=\"xsd:float\"></xsd:restriction>\n");
2993 break;
2995 case FLOAT8OID:
2996 appendStringInfo(&result,
2997 " <xsd:restriction base=\"xsd:double\"></xsd:restriction>\n");
2998 break;
3000 case BOOLOID:
3001 appendStringInfo(&result,
3002 " <xsd:restriction base=\"xsd:boolean\"></xsd:restriction>\n");
3003 break;
3005 case TIMEOID:
3006 case TIMETZOID:
3008 const char *tz = (typeoid == TIMETZOID ? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
3010 if (typmod == -1)
3011 appendStringInfo(&result,
3012 " <xsd:restriction base=\"xsd:time\">\n"
3013 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}(.\\p{Nd}+)?%s\"/>\n"
3014 " </xsd:restriction>\n", tz);
3015 else if (typmod == 0)
3016 appendStringInfo(&result,
3017 " <xsd:restriction base=\"xsd:time\">\n"
3018 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3019 " </xsd:restriction>\n", tz);
3020 else
3021 appendStringInfo(&result,
3022 " <xsd:restriction base=\"xsd:time\">\n"
3023 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}.\\p{Nd}{%d}%s\"/>\n"
3024 " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
3025 break;
3028 case TIMESTAMPOID:
3029 case TIMESTAMPTZOID:
3031 const char *tz = (typeoid == TIMESTAMPTZOID ? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
3033 if (typmod == -1)
3034 appendStringInfo(&result,
3035 " <xsd:restriction base=\"xsd:dateTime\">\n"
3036 " <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"
3037 " </xsd:restriction>\n", tz);
3038 else if (typmod == 0)
3039 appendStringInfo(&result,
3040 " <xsd:restriction base=\"xsd:dateTime\">\n"
3041 " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3042 " </xsd:restriction>\n", tz);
3043 else
3044 appendStringInfo(&result,
3045 " <xsd:restriction base=\"xsd:dateTime\">\n"
3046 " <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"
3047 " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
3048 break;
3051 case DATEOID:
3052 appendStringInfo(&result,
3053 " <xsd:restriction base=\"xsd:date\">\n"
3054 " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}\"/>\n"
3055 " </xsd:restriction>\n");
3056 break;
3058 default:
3059 if (get_typtype(typeoid) == TYPTYPE_DOMAIN)
3061 Oid base_typeoid;
3062 int32 base_typmod = -1;
3064 base_typeoid = getBaseTypeAndTypmod(typeoid, &base_typmod);
3066 appendStringInfo(&result,
3067 " <xsd:restriction base=\"%s\"/>\n",
3068 map_sql_type_to_xml_name(base_typeoid, base_typmod));
3070 break;
3072 appendStringInfo(&result,
3073 "</xsd:simpleType>\n");
3076 return result.data;
3081 * Map an SQL row to an XML element, taking the row from the active
3082 * SPI cursor. See also SQL/XML:2003 section 9.12.
3084 static void
3085 SPI_sql_row_to_xmlelement(int rownum, StringInfo result, char *tablename,
3086 bool nulls, bool tableforest,
3087 const char *targetns, bool top_level)
3089 int i;
3090 char *xmltn;
3092 if (tablename)
3093 xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
3094 else
3096 if (tableforest)
3097 xmltn = "row";
3098 else
3099 xmltn = "table";
3102 if (tableforest)
3103 xmldata_root_element_start(result, xmltn, NULL, targetns, top_level);
3104 else
3105 appendStringInfoString(result, "<row>\n");
3107 for (i = 1; i <= SPI_tuptable->tupdesc->natts; i++)
3109 char *colname;
3110 Datum colval;
3111 bool isnull;
3113 colname = map_sql_identifier_to_xml_name(SPI_fname(SPI_tuptable->tupdesc, i),
3114 true, false);
3115 colval = SPI_getbinval(SPI_tuptable->vals[rownum],
3116 SPI_tuptable->tupdesc,
3118 &isnull);
3119 if (isnull)
3121 if (nulls)
3122 appendStringInfo(result, " <%s xsi:nil=\"true\"/>\n", colname);
3124 else
3125 appendStringInfo(result, " <%s>%s</%s>\n",
3126 colname,
3127 map_sql_value_to_xml_value(colval,
3128 SPI_gettypeid(SPI_tuptable->tupdesc, i)),
3129 colname);
3132 if (tableforest)
3134 xmldata_root_element_end(result, xmltn);
3135 appendStringInfoChar(result, '\n');
3137 else
3138 appendStringInfoString(result, "</row>\n\n");
3143 * XPath related functions
3146 #ifdef USE_LIBXML
3148 * Convert XML node to text (dump subtree in case of element,
3149 * return value otherwise)
3151 static text *
3152 xml_xmlnodetoxmltype(xmlNodePtr cur)
3154 xmlChar *str;
3155 xmltype *result;
3156 xmlBufferPtr buf;
3158 if (cur->type == XML_ELEMENT_NODE)
3160 buf = xmlBufferCreate();
3161 xmlNodeDump(buf, NULL, cur, 0, 1);
3162 result = xmlBuffer_to_xmltype(buf);
3163 xmlBufferFree(buf);
3165 else
3167 str = xmlXPathCastNodeToString(cur);
3168 result = (xmltype *) cstring_to_text((char *) str);
3169 xmlFree(str);
3172 return result;
3174 #endif
3178 * Evaluate XPath expression and return array of XML values.
3180 * As we have no support of XQuery sequences yet, this function seems
3181 * to be the most useful one (array of XML functions plays a role of
3182 * some kind of substitution for XQuery sequences).
3184 * Workaround here: we parse XML data in different way to allow XPath for
3185 * fragments (see "XPath for fragment" TODO comment inside).
3187 Datum
3188 xpath(PG_FUNCTION_ARGS)
3190 #ifdef USE_LIBXML
3191 text *xpath_expr_text = PG_GETARG_TEXT_P(0);
3192 xmltype *data = PG_GETARG_XML_P(1);
3193 ArrayType *namespaces = PG_GETARG_ARRAYTYPE_P(2);
3194 ArrayBuildState *astate = NULL;
3195 xmlParserCtxtPtr ctxt;
3196 xmlDocPtr doc;
3197 xmlXPathContextPtr xpathctx;
3198 xmlXPathCompExprPtr xpathcomp;
3199 xmlXPathObjectPtr xpathobj;
3200 char *datastr;
3201 int32 len;
3202 int32 xpath_len;
3203 xmlChar *string;
3204 xmlChar *xpath_expr;
3205 int i;
3206 int res_nitems;
3207 int ndim;
3208 Datum *ns_names_uris;
3209 bool *ns_names_uris_nulls;
3210 int ns_count;
3213 * Namespace mappings are passed as text[]. If an empty array is passed
3214 * (ndim = 0, "0-dimensional"), then there are no namespace mappings.
3215 * Else, a 2-dimensional array with length of the second axis being equal
3216 * to 2 should be passed, i.e., every subarray contains 2 elements, the
3217 * first element defining the name, the second one the URI. Example:
3218 * ARRAY[ARRAY['myns', 'http://example.com'], ARRAY['myns2',
3219 * 'http://example2.com']].
3221 ndim = ARR_NDIM(namespaces);
3222 if (ndim != 0)
3224 int *dims;
3226 dims = ARR_DIMS(namespaces);
3228 if (ndim != 2 || dims[1] != 2)
3229 ereport(ERROR,
3230 (errcode(ERRCODE_DATA_EXCEPTION),
3231 errmsg("invalid array for XML namespace mapping"),
3232 errdetail("The array must be two-dimensional with length of the second axis equal to 2.")));
3234 Assert(ARR_ELEMTYPE(namespaces) == TEXTOID);
3236 deconstruct_array(namespaces, TEXTOID, -1, false, 'i',
3237 &ns_names_uris, &ns_names_uris_nulls,
3238 &ns_count);
3240 Assert((ns_count % 2) == 0); /* checked above */
3241 ns_count /= 2; /* count pairs only */
3243 else
3245 ns_names_uris = NULL;
3246 ns_names_uris_nulls = NULL;
3247 ns_count = 0;
3250 datastr = VARDATA(data);
3251 len = VARSIZE(data) - VARHDRSZ;
3252 xpath_len = VARSIZE(xpath_expr_text) - VARHDRSZ;
3253 if (xpath_len == 0)
3254 ereport(ERROR,
3255 (errcode(ERRCODE_DATA_EXCEPTION),
3256 errmsg("empty XPath expression")));
3258 xml_init();
3261 * To handle both documents and fragments, regardless of the fact whether
3262 * the XML datum has a single root (XML well-formedness), we wrap the XML
3263 * datum in a dummy element (<x>...</x>) and extend the XPath expression
3264 * accordingly. To do it, throw away the XML prolog, if any.
3266 if (len >= 5 &&
3267 xmlStrncmp((xmlChar *) datastr, (xmlChar *) "<?xml", 5) == 0)
3269 i = 5;
3270 while (i < len &&
3271 !(datastr[i - 1] == '?' && datastr[i] == '>'))
3272 i++;
3274 if (i == len)
3275 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3276 "could not parse XML data");
3278 ++i;
3280 datastr += i;
3281 len -= i;
3284 string = (xmlChar *) palloc((len + 8) * sizeof(xmlChar));
3285 memcpy(string, "<x>", 3);
3286 memcpy(string + 3, datastr, len);
3287 memcpy(string + 3 + len, "</x>", 5);
3288 len += 7;
3290 xpath_expr = (xmlChar *) palloc((xpath_len + 3) * sizeof(xmlChar));
3291 memcpy(xpath_expr, "/x", 2);
3292 memcpy(xpath_expr + 2, VARDATA(xpath_expr_text), xpath_len);
3293 xpath_expr[xpath_len + 2] = '\0';
3294 xpath_len += 2;
3296 xmlInitParser();
3299 * redundant XML parsing (two parsings for the same value during one
3300 * command execution are possible)
3302 ctxt = xmlNewParserCtxt();
3303 if (ctxt == NULL)
3304 xml_ereport(ERROR, ERRCODE_OUT_OF_MEMORY,
3305 "could not allocate parser context");
3306 doc = xmlCtxtReadMemory(ctxt, (char *) string, len, NULL, NULL, 0);
3307 if (doc == NULL)
3308 xml_ereport(ERROR, ERRCODE_INVALID_XML_DOCUMENT,
3309 "could not parse XML data");
3310 xpathctx = xmlXPathNewContext(doc);
3311 if (xpathctx == NULL)
3312 xml_ereport(ERROR, ERRCODE_OUT_OF_MEMORY,
3313 "could not allocate XPath context");
3314 xpathctx->node = xmlDocGetRootElement(doc);
3315 if (xpathctx->node == NULL)
3316 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3317 "could not find root XML element");
3319 /* register namespaces, if any */
3320 if (ns_count > 0)
3322 for (i = 0; i < ns_count; i++)
3324 char *ns_name;
3325 char *ns_uri;
3327 if (ns_names_uris_nulls[i * 2] ||
3328 ns_names_uris_nulls[i * 2 + 1])
3329 ereport(ERROR,
3330 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3331 errmsg("neither namespace name nor URI may be null")));
3332 ns_name = TextDatumGetCString(ns_names_uris[i * 2]);
3333 ns_uri = TextDatumGetCString(ns_names_uris[i * 2 + 1]);
3334 if (xmlXPathRegisterNs(xpathctx,
3335 (xmlChar *) ns_name,
3336 (xmlChar *) ns_uri) != 0)
3337 ereport(ERROR, /* is this an internal error??? */
3338 (errmsg("could not register XML namespace with name \"%s\" and URI \"%s\"",
3339 ns_name, ns_uri)));
3343 xpathcomp = xmlXPathCompile(xpath_expr);
3344 if (xpathcomp == NULL) /* TODO: show proper XPath error details */
3345 xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3346 "invalid XPath expression");
3348 xpathobj = xmlXPathCompiledEval(xpathcomp, xpathctx);
3349 if (xpathobj == NULL) /* TODO: reason? */
3350 ereport(ERROR,
3351 (errmsg("could not create XPath object")));
3353 xmlXPathFreeCompExpr(xpathcomp);
3355 /* return empty array in cases when nothing is found */
3356 if (xpathobj->nodesetval == NULL)
3357 res_nitems = 0;
3358 else
3359 res_nitems = xpathobj->nodesetval->nodeNr;
3361 if (res_nitems)
3363 for (i = 0; i < xpathobj->nodesetval->nodeNr; i++)
3365 Datum elem;
3366 bool elemisnull = false;
3368 elem = PointerGetDatum(xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[i]));
3369 astate = accumArrayResult(astate, elem,
3370 elemisnull, XMLOID,
3371 CurrentMemoryContext);
3375 xmlXPathFreeObject(xpathobj);
3376 xmlXPathFreeContext(xpathctx);
3377 xmlFreeDoc(doc);
3378 xmlFreeParserCtxt(ctxt);
3380 if (res_nitems == 0)
3381 PG_RETURN_ARRAYTYPE_P(construct_empty_array(XMLOID));
3382 else
3383 PG_RETURN_ARRAYTYPE_P(makeArrayResult(astate, CurrentMemoryContext));
3384 #else
3385 NO_XML_SUPPORT();
3386 return 0;
3387 #endif