1 /*-------------------------------------------------------------------------
4 * XML data type support.
7 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 *-------------------------------------------------------------------------
16 * Generally, XML type support is only available when libxml use was
17 * configured during the build. But even if that is not done, the
18 * type and all the functions are available, but most of them will
19 * fail. For one thing, this avoids having to manage variant catalog
20 * installations. But it also has nice effects such as that you can
21 * dump a database containing XML type data even if the server is not
22 * linked with libxml. Thus, make sure xml_out() works even if nothing
27 * Notes on memory management:
29 * Via callbacks, libxml is told to use palloc and friends for memory
30 * management, within a context that we reset at transaction end (and also at
31 * subtransaction abort) to prevent memory leaks. Resetting at transaction or
32 * subtransaction abort is necessary since we might have thrown a longjmp
33 * while some data structures were not linked from anywhere persistent.
34 * Resetting at transaction commit might not be necessary, but seems a good
35 * idea to forestall long-term leaks.
37 * Sometimes libxml allocates global structures in the hope that it can reuse
38 * them later on. Therefore, before resetting LibxmlContext, we must tell
39 * libxml to discard any global data it has. The libxml API documentation is
40 * not very good about specifying this, but for now we assume that
41 * xmlCleanupParser() will get rid of anything we need to worry about.
43 * We use palloc --- which will throw a longjmp on error --- for allocation
44 * callbacks that officially should act like malloc, ie, return NULL on
45 * out-of-memory. This is a bit risky since there is a chance of leaving
46 * persistent libxml data structures in an inconsistent partially-constructed
47 * state, perhaps leading to crash in xmlCleanupParser(). However, as of
48 * early 2008 it is *known* that libxml can crash on out-of-memory due to
49 * inadequate checks for NULL returns, so this behavior seems the lesser
56 #include <libxml/chvalid.h>
57 #include <libxml/parser.h>
58 #include <libxml/tree.h>
59 #include <libxml/uri.h>
60 #include <libxml/xmlerror.h>
61 #include <libxml/xmlwriter.h>
62 #include <libxml/xpath.h>
63 #include <libxml/xpathInternals.h>
64 #endif /* USE_LIBXML */
66 #include "catalog/namespace.h"
67 #include "catalog/pg_type.h"
68 #include "commands/dbcommands.h"
69 #include "executor/executor.h"
70 #include "executor/spi.h"
72 #include "lib/stringinfo.h"
73 #include "libpq/pqformat.h"
74 #include "mb/pg_wchar.h"
75 #include "miscadmin.h"
76 #include "nodes/execnodes.h"
77 #include "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"
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() \
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"
152 xmlChar_to_encoding(xmlChar
* encoding_name
)
154 int encoding
= pg_char_to_encoding((char *) encoding_name
);
158 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
159 errmsg("invalid encoding name \"%s\"",
160 (char *) encoding_name
)));
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
174 xml_in(PG_FUNCTION_ARGS
)
177 char *s
= PG_GETARG_CSTRING(0);
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
);
190 PG_RETURN_XML_P(vardata
);
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
209 xml_out_internal(xmltype
*x
, pg_enc target_encoding
)
211 char *str
= text_to_cstring((text
*) x
);
214 size_t len
= strlen(str
);
219 if ((res_code
= parse_xml_decl((xmlChar
*) str
,
220 &len
, &version
, NULL
, &standalone
)) == 0)
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
233 if (*(str
+ len
) == '\n')
236 appendStringInfoString(&buf
, str
+ len
);
245 xml_ereport_by_code(WARNING
, ERRCODE_INTERNAL_ERROR
,
246 "could not parse XML declaration in stored value",
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
264 PG_RETURN_CSTRING(xml_out_internal(x
, 0));
269 xml_recv(PG_FUNCTION_ARGS
)
272 StringInfo buf
= (StringInfo
) PG_GETARG_POINTER(0);
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
);
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
);
308 /* Now that we know what we're dealing with, convert to server encoding */
309 newstr
= (char *) pg_do_encoding_conversion((unsigned char *) str
,
312 xmlChar_to_encoding(encoding
) :
314 GetDatabaseEncoding());
319 result
= (xmltype
*) cstring_to_text(newstr
);
323 PG_RETURN_XML_P(result
);
332 xml_send(PG_FUNCTION_ARGS
)
334 xmltype
*x
= PG_GETARG_XML_P(0);
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
));
347 PG_RETURN_BYTEA_P(pq_endtypsend(&buf
));
353 appendStringInfoText(StringInfo str
, const text
*t
)
355 appendBinaryStringInfo(str
, VARDATA(t
), VARSIZE(t
) - VARHDRSZ
);
361 stringinfo_to_xmltype(StringInfo buf
)
363 return (xmltype
*) cstring_to_text_with_len(buf
->data
, buf
->len
);
368 cstring_to_xmltype(const char *string
)
370 return (xmltype
*) cstring_to_text(string
);
376 xmlBuffer_to_xmltype(xmlBufferPtr buf
)
378 return (xmltype
*) cstring_to_text_with_len((char *) xmlBufferContent(buf
),
379 xmlBufferLength(buf
));
385 xmlcomment(PG_FUNCTION_ARGS
)
388 text
*arg
= PG_GETARG_TEXT_P(0);
389 char *argdata
= VARDATA(arg
);
390 int len
= VARSIZE(arg
) - VARHDRSZ
;
394 /* check for "--" in string or "-" at the end */
395 for (i
= 1; i
< len
; i
++)
397 if (argdata
[i
] == '-' && argdata
[i
- 1] == '-')
399 (errcode(ERRCODE_INVALID_XML_COMMENT
),
400 errmsg("invalid XML comment")));
402 if (len
> 0 && argdata
[len
- 1] == '-')
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
));
422 * TODO: xmlconcat needs to merge the notations and unparsed entities
423 * of the argument values. Not very important in practice, though.
426 xmlconcat(List
*args
)
429 int global_standalone
= 1;
430 xmlChar
*global_version
= NULL
;
431 bool global_version_no_value
= false;
435 initStringInfo(&buf
);
438 xmltype
*x
= DatumGetXmlP(PointerGetDatum(lfirst(v
)));
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;
452 global_standalone
= -1;
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
);
465 if (!global_version_no_value
|| global_standalone
>= 0)
469 initStringInfo(&buf2
);
471 print_xml_decl(&buf2
,
472 (!global_version_no_value
) ? global_version
: NULL
,
476 appendStringInfoString(&buf2
, buf
.data
);
480 return stringinfo_to_xmltype(&buf
);
492 xmlconcat2(PG_FUNCTION_ARGS
)
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));
504 PG_RETURN_XML_P(xmlconcat(list_make2(PG_GETARG_XML_P(0),
505 PG_GETARG_XML_P(1))));
510 texttoxml(PG_FUNCTION_ARGS
)
512 text
*data
= PG_GETARG_TEXT_P(0);
514 PG_RETURN_XML_P(xmlparse(data
, xmloption
, true));
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
);
529 xmltotext_with_xmloption(xmltype
*data
, XmlOptionType xmloption_arg
)
531 if (xmloption_arg
== XMLOPTION_DOCUMENT
&& !xml_is_document(data
))
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
;
542 xmlelement(XmlExprState
*xmlExpr
, ExprContext
*econtext
)
545 XmlExpr
*xexpr
= (XmlExpr
*) xmlExpr
->xprstate
.expr
;
547 List
*named_arg_strings
;
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
561 named_arg_strings
= NIL
;
563 foreach(arg
, xmlExpr
->named_args
)
565 ExprState
*e
= (ExprState
*) lfirst(arg
);
570 value
= ExecEvalExpr(e
, econtext
, &isnull
, NULL
);
574 str
= OutputFunctionCall(&xmlExpr
->named_outfuncs
[i
], value
);
575 named_arg_strings
= lappend(named_arg_strings
, str
);
580 foreach(arg
, xmlExpr
->args
)
582 ExprState
*e
= (ExprState
*) lfirst(arg
);
587 value
= ExecEvalExpr(e
, econtext
, &isnull
, NULL
);
588 /* here we can just forget NULL elements immediately */
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 */
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
));
612 xmlTextWriterWriteAttribute(writer
,
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
);
641 xmlparse(text
*data
, XmlOptionType xmloption_arg
, bool preserve_whitespace
)
646 doc
= xml_parse(data
, xmloption_arg
, preserve_whitespace
, NULL
);
649 return (xmltype
*) data
;
658 xmlpi(char *target
, text
*arg
, bool arg_is_null
, bool *result_is_null
)
664 if (pg_strcasecmp(target
, "xml") == 0)
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
674 *result_is_null
= arg_is_null
;
678 initStringInfo(&buf
);
680 appendStringInfo(&buf
, "<?%s", target
);
686 string
= text_to_cstring(arg
);
687 if (strstr(string
, "?>") != NULL
)
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
, " "));
697 appendStringInfoString(&buf
, "?>");
699 result
= stringinfo_to_xmltype(&buf
);
710 xmlroot(xmltype
*data
, text
*version
, int standalone
)
715 xmlChar
*orig_version
;
719 len
= VARSIZE(data
) - VARHDRSZ
;
720 str
= text_to_cstring((text
*) data
);
722 parse_xml_decl((xmlChar
*) str
, &len
, &orig_version
, NULL
, &orig_standalone
);
725 orig_version
= xml_text2xmlChar(version
);
731 case XML_STANDALONE_YES
:
734 case XML_STANDALONE_NO
:
737 case XML_STANDALONE_NO_VALUE
:
738 orig_standalone
= -1;
740 case XML_STANDALONE_OMITTED
:
741 /* leave original value */
745 initStringInfo(&buf
);
746 print_xml_decl(&buf
, orig_version
, 0, orig_standalone
);
747 appendStringInfoString(&buf
, str
+ len
);
749 return stringinfo_to_xmltype(&buf
);
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
766 xmlvalidate(PG_FUNCTION_ARGS
)
769 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
770 errmsg("xmlvalidate is not implemented")));
776 xml_is_document(xmltype
*arg
)
780 xmlDocPtr doc
= NULL
;
781 MemoryContext ccxt
= CurrentMemoryContext
;
785 doc
= xml_parse((text
*) arg
, XMLOPTION_DOCUMENT
, true, NULL
);
793 ecxt
= MemoryContextSwitchTo(ccxt
);
794 errdata
= CopyErrorData();
795 if (errdata
->sqlerrcode
== ERRCODE_INVALID_XML_DOCUMENT
)
802 MemoryContextSwitchTo(ecxt
);
812 #else /* not USE_LIBXML */
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.
827 xml_memory_cleanup();
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
844 static bool first_time
= true;
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
855 if (sizeof(char) != sizeof(xmlChar
))
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 */
872 /* Check library compatibility */
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
890 xmlSetGenericErrorFunc(NULL
, xml_errorHandler
);
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
905 #define CHECK_XML_SPACE(p) \
907 if (!xmlIsBlank_ch(*(p))) \
908 return XML_ERR_SPACE_REQUIRED; \
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))
924 parse_xml_decl(const xmlChar
* str
, size_t *lenp
,
925 xmlChar
** version
, xmlChar
** encoding
, int *standalone
)
928 const xmlChar
*save_p
;
944 if (xmlStrncmp(p
, (xmlChar
*) "<?xml", 5) != 0)
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
))
958 if (xmlStrncmp(p
, (xmlChar
*) "version", 7) != 0)
959 return XML_ERR_VERSION_MISSING
;
963 return XML_ERR_VERSION_MISSING
;
967 if (*p
== '\'' || *p
== '"')
971 q
= xmlStrchr(p
+ 1, *p
);
973 return XML_ERR_VERSION_MISSING
;
976 *version
= xmlStrndup(p
+ 1, q
- p
- 1);
980 return XML_ERR_VERSION_MISSING
;
985 if (xmlStrncmp(p
, (xmlChar
*) "encoding", 8) == 0)
987 CHECK_XML_SPACE(save_p
);
991 return XML_ERR_MISSING_ENCODING
;
995 if (*p
== '\'' || *p
== '"')
999 q
= xmlStrchr(p
+ 1, *p
);
1001 return XML_ERR_MISSING_ENCODING
;
1004 *encoding
= xmlStrndup(p
+ 1, q
- p
- 1);
1008 return XML_ERR_MISSING_ENCODING
;
1018 if (xmlStrncmp(p
, (xmlChar
*) "standalone", 10) == 0)
1020 CHECK_XML_SPACE(save_p
);
1024 return XML_ERR_STANDALONE_VALUE
;
1027 if (xmlStrncmp(p
, (xmlChar
*) "'yes'", 5) == 0 ||
1028 xmlStrncmp(p
, (xmlChar
*) "\"yes\"", 5) == 0)
1033 else if (xmlStrncmp(p
, (xmlChar
*) "'no'", 4) == 0 ||
1034 xmlStrncmp(p
, (xmlChar
*) "\"no\"", 4) == 0)
1040 return XML_ERR_STANDALONE_VALUE
;
1048 if (xmlStrncmp(p
, (xmlChar
*) "?>", 2) != 0)
1049 return XML_ERR_XMLDECL_NOT_FINISHED
;
1055 for (p
= str
; p
< str
+ len
; p
++)
1057 return XML_ERR_INVALID_CHAR
;
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.
1081 print_xml_decl(StringInfo buf
, const xmlChar
* version
,
1082 pg_enc encoding
, int standalone
)
1086 if ((version
&& strcmp((char *) version
, PG_XML_DEFAULT_VERSION
) != 0)
1087 || (encoding
&& encoding
!= PG_UTF8
)
1088 || standalone
!= -1)
1090 appendStringInfoString(buf
, "<?xml");
1093 appendStringInfo(buf
, " version=\"%s\"", version
);
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
, "?>");
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)
1127 xml_parse(text
*data
, XmlOptionType xmloption_arg
, bool preserve_whitespace
,
1132 xmlChar
*utf8string
;
1133 xmlParserCtxtPtr ctxt
;
1136 len
= VARSIZE(data
) - VARHDRSZ
; /* will be useful later */
1137 string
= xml_text2xmlChar(data
);
1139 utf8string
= pg_do_encoding_conversion(string
,
1142 xmlChar_to_encoding(encoding
) :
1143 GetDatabaseEncoding(),
1148 ctxt
= xmlNewParserCtxt();
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
,
1164 XML_PARSE_NOENT
| XML_PARSE_DTDATTR
1165 | (preserve_whitespace
? 0 : XML_PARSE_NOBLANKS
));
1167 xml_ereport(ERROR
, ERRCODE_INVALID_XML_DOCUMENT
,
1168 "invalid XML document");
1174 xmlChar
*version
= NULL
;
1175 int standalone
= -1;
1177 doc
= xmlNewDoc(NULL
);
1179 res_code
= parse_xml_decl(utf8string
,
1180 &count
, &version
, NULL
, &standalone
);
1182 xml_ereport_by_code(ERROR
, ERRCODE_INVALID_XML_CONTENT
,
1183 "invalid XML content: invalid XML declaration",
1186 res_code
= xmlParseBalancedChunkMemory(doc
, NULL
, NULL
, 0,
1187 utf8string
+ count
, NULL
);
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
);
1204 * xmlChar<->text conversions
1207 xml_text2xmlChar(text
*in
)
1209 return (xmlChar
*) text_to_cstring(in
);
1214 * Manage the special context used for all libxml allocations
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
,
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
);
1237 xml_memory_cleanup(void)
1239 if (LibxmlContext
!= NULL
)
1241 /* Give libxml a chance to clean up dangling pointers */
1244 /* And flush the context */
1245 MemoryContextDelete(LibxmlContext
);
1246 LibxmlContext
= NULL
;
1251 * Wrappers for memory management functions
1254 xml_palloc(size_t size
)
1256 return MemoryContextAlloc(LibxmlContext
, size
);
1261 xml_repalloc(void *ptr
, size_t size
)
1263 return repalloc(ptr
, size
);
1268 xml_pfree(void *ptr
)
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
1288 xml_ereport(int level
, int sqlcode
, const char *msg
)
1292 if (xml_err_buf
->len
> 0)
1294 detail
= pstrdup(xml_err_buf
->data
);
1295 resetStringInfo(xml_err_buf
);
1300 /* libxml error messages end in '\n'; get rid of it */
1305 len
= strlen(detail
);
1306 if (len
> 0 && detail
[len
- 1] == '\n')
1307 detail
[len
- 1] = '\0';
1312 errdetail("%s", detail
)));
1318 errmsg("%s", msg
)));
1324 * Error handler for libxml error messages
1327 xml_errorHandler(void *ctxt
, const char *msg
,...)
1329 /* Append the formatted text to xml_err_buf */
1335 /* Try to format the data. */
1336 va_start(args
, msg
);
1337 success
= appendStringInfoVA(xml_err_buf
, msg
, args
);
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.
1357 xml_ereport_by_code(int level
, int sqlcode
,
1358 const char *msg
, int code
)
1364 case XML_ERR_INVALID_CHAR
:
1365 det
= gettext_noop("Invalid character value.");
1367 case XML_ERR_SPACE_REQUIRED
:
1368 det
= gettext_noop("Space required.");
1370 case XML_ERR_STANDALONE_VALUE
:
1371 det
= gettext_noop("standalone accepts only 'yes' or 'no'.");
1373 case XML_ERR_VERSION_MISSING
:
1374 det
= gettext_noop("Malformed declaration: missing version.");
1376 case XML_ERR_MISSING_ENCODING
:
1377 det
= gettext_noop("Missing encoding in text declaration.");
1379 case XML_ERR_XMLDECL_NOT_FINISHED
:
1380 det
= gettext_noop("Parsing XML declaration: '?>' expected.");
1383 det
= gettext_noop("Unrecognized libxml error code: %d.");
1390 errdetail(det
, code
)));
1395 * Convert one char in the current server encoding to a Unicode codepoint.
1398 sqlchar_to_unicode(char *s
)
1401 pg_wchar ret
[2]; /* need space for trailing zero */
1403 utf8string
= (char *) pg_do_encoding_conversion((unsigned char *) s
,
1405 GetDatabaseEncoding(),
1408 pg_encoding_mb2wchar_with_len(PG_UTF8
, utf8string
, ret
,
1409 pg_encoding_mblen(PG_UTF8
, utf8string
));
1411 if (utf8string
!= s
)
1419 is_valid_xml_namefirst(pg_wchar c
)
1421 /* (Letter | '_' | ':') */
1422 return (xmlIsBaseCharQ(c
) || xmlIsIdeographicQ(c
)
1423 || c
== '_' || c
== ':');
1428 is_valid_xml_namechar(pg_wchar c
)
1430 /* Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender */
1431 return (xmlIsBaseCharQ(c
) || xmlIsIdeographicQ(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.
1444 map_sql_identifier_to_xml_name(char *ident
, bool fully_escaped
,
1452 * SQL/XML doesn't make use of this case anywhere, so it's probably a
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)
1469 appendStringInfo(&buf
, "_x0078_");
1471 appendStringInfo(&buf
, "_x0058_");
1473 else if (escape_period
&& *p
== '.')
1474 appendStringInfo(&buf
, "_x002E_");
1477 pg_wchar u
= sqlchar_to_unicode(p
);
1480 ? !is_valid_xml_namefirst(u
)
1481 : !is_valid_xml_namechar(u
))
1482 appendStringInfo(&buf
, "_x%04X_", (unsigned int) u
);
1484 appendBinaryStringInfo(&buf
, p
, pg_mblen(p
));
1489 #else /* not USE_LIBXML */
1492 #endif /* not USE_LIBXML */
1497 * Map a Unicode codepoint into the current server encoding.
1500 unicode_to_sqlchar(pg_wchar c
)
1502 unsigned char utf8string
[5]; /* need room for trailing zero */
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
),
1512 GetDatabaseEncoding());
1513 /* if pg_do_encoding_conversion didn't strdup, we must */
1514 if (result
== (char *) utf8string
)
1515 result
= pstrdup(result
);
1521 * Map XML name to SQL identifier; see SQL/XML:2003 section 9.17.
1524 map_xml_name_to_sql_identifier(char *name
)
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))
1542 sscanf(p
+ 2, "%X", &u
);
1543 appendStringInfoString(&buf
, unicode_to_sqlchar(u
));
1547 appendBinaryStringInfo(&buf
, p
, pg_mblen(p
));
1554 * Map SQL value to XML value; see SQL/XML:2003 section 9.16.
1557 map_sql_value_to_xml_value(Datum value
, Oid type
)
1561 if (type_is_array(type
))
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
,
1582 initStringInfo(&buf
);
1584 for (i
= 0; i
< num_elems
; i
++)
1588 appendStringInfoString(&buf
, "<element>");
1589 appendStringInfoString(&buf
,
1590 map_sql_value_to_xml_value(elem_values
[i
],
1592 appendStringInfoString(&buf
, "</element>");
1608 * Special XSD formatting for some data types
1613 if (DatumGetBool(value
))
1622 char buf
[MAXDATELEN
+ 1];
1624 date
= DatumGetDateADT(value
);
1625 /* XSD doesn't support infinite values */
1626 if (DATE_NOT_FINITE(date
))
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
);
1639 Timestamp timestamp
;
1643 char buf
[MAXDATELEN
+ 1];
1645 timestamp
= DatumGetTimestamp(value
);
1647 /* XSD doesn't support infinite values */
1648 if (TIMESTAMP_NOT_FINITE(timestamp
))
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
);
1656 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE
),
1657 errmsg("timestamp out of range")));
1659 return pstrdup(buf
);
1662 case TIMESTAMPTZOID
:
1664 TimestampTz timestamp
;
1669 char buf
[MAXDATELEN
+ 1];
1671 timestamp
= DatumGetTimestamp(value
);
1673 /* XSD doesn't support infinite values */
1674 if (TIMESTAMP_NOT_FINITE(timestamp
))
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
);
1682 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE
),
1683 errmsg("timestamp out of range")));
1685 return pstrdup(buf
);
1691 bytea
*bstr
= DatumGetByteaPP(value
);
1693 xmlTextWriterPtr writer
;
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
));
1705 xmlTextWriterWriteBinHex(writer
, VARDATA_ANY(bstr
),
1706 0, VARSIZE_ANY_EXHDR(bstr
));
1708 xmlFreeTextWriter(writer
);
1709 result
= pstrdup((const char *) xmlBufferContent(buf
));
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 */
1727 /* otherwise, translate special characters as needed */
1728 initStringInfo(&buf
);
1730 for (p
= str
; *p
; p
++)
1735 appendStringInfoString(&buf
, "&");
1738 appendStringInfoString(&buf
, "<");
1741 appendStringInfoString(&buf
, ">");
1744 appendStringInfoString(&buf
, "
");
1747 appendStringInfoCharMacro(&buf
, *p
);
1758 _SPI_strdup(const char *s
)
1760 size_t len
= strlen(s
) + 1;
1761 char *ret
= SPI_palloc(len
);
1763 memcpy(ret
, s
, len
);
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,
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
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
1810 * Given a query, which must return type oid as first column, produce
1811 * a list of Oids with the query results.
1814 query_to_oid_list(const char *query
)
1819 SPI_execute(query
, true, 0);
1821 for (i
= 0; i
< SPI_processed
; i
++)
1826 oid
= SPI_getbinval(SPI_tuptable
->vals
[i
],
1827 SPI_tuptable
->tupdesc
,
1831 list
= lappend_oid(list
, DatumGetObjectId(oid
));
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
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
1860 database_get_xml_visible_schemas(void)
1862 return query_to_oid_list(XML_VISIBLE_SCHEMAS
" ORDER BY nspname;");
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
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
);
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
,
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
,
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
;
1937 initStringInfo(&result
);
1940 portal
= SPI_cursor_find(name
);
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);
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
1970 xmldata_root_element_start(StringInfo result
, const char *eltname
,
1971 const char *xmlschema
, const char *targetns
,
1974 /* This isn't really wrong but currently makes no sense. */
1975 Assert(top_level
|| !xmlschema
);
1977 appendStringInfo(result
, "<%s", eltname
);
1980 appendStringInfoString(result
, " xmlns:xsi=\"" NAMESPACE_XSI
"\"");
1981 if (strlen(targetns
) > 0)
1982 appendStringInfo(result
, " xmlns=\"%s\"", targetns
);
1986 /* FIXME: better targets */
1987 if (strlen(targetns
) > 0)
1988 appendStringInfo(result
, " xsi:schemaLocation=\"%s #\"", targetns
);
1990 appendStringInfo(result
, " xsi:noNamespaceSchemaLocation=\"#\"");
1992 appendStringInfo(result
, ">\n\n");
1997 xmldata_root_element_end(StringInfo result
, const char *eltname
)
1999 appendStringInfo(result
, "</%s>\n", eltname
);
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
)
2013 xmltn
= map_sql_identifier_to_xml_name(tablename
, true, false);
2017 result
= makeStringInfo();
2020 if (SPI_execute(query
, true, 0) != SPI_OK_SELECT
)
2022 (errcode(ERRCODE_DATA_EXCEPTION
),
2023 errmsg("invalid query")));
2026 xmldata_root_element_start(result
, xmltn
, xmlschema
,
2027 targetns
, top_level
);
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
);
2037 xmldata_root_element_end(result
, xmltn
);
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));
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
));
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));
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
,
2085 tableforest
, targetns
));
2086 SPI_cursor_close(portal
);
2089 PG_RETURN_XML_P(cstring_to_xmltype(result
));
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
;
2104 portal
= SPI_cursor_find(name
);
2107 (errcode(ERRCODE_UNDEFINED_CURSOR
),
2108 errmsg("cursor \"%s\" does not exist", name
)));
2110 xmlschema
= _SPI_strdup(map_sql_table_to_xmlschema(portal
->tupDesc
,
2112 tableforest
, targetns
));
2115 PG_RETURN_XML_P(cstring_to_xmltype(xmlschema
));
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));
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
,
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
;
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
);
2165 PG_RETURN_XML_P(stringinfo_to_xmltype(query_to_xml_internal(query
, NULL
,
2166 xmlschema
, nulls
, tableforest
,
2172 * Map SQL schema to XML and/or XML Schema document; see SQL/XML:2003
2177 schema_to_xml_internal(Oid nspid
, const char *xmlschema
, bool nulls
,
2178 bool tableforest
, const char *targetns
, bool top_level
)
2185 xmlsn
= map_sql_identifier_to_xml_name(get_namespace_name(nspid
),
2187 result
= makeStringInfo();
2189 xmldata_root_element_start(result
, xmlsn
, xmlschema
, targetns
, top_level
);
2192 appendStringInfo(result
, "%s\n\n", xmlschema
);
2196 relid_list
= schema_get_xml_visible_tables(nspid
);
2200 foreach(cell
, relid_list
)
2202 Oid relid
= lfirst_oid(cell
);
2205 subres
= table_to_xml_internal(relid
, NULL
, nulls
, tableforest
,
2208 appendStringInfoString(result
, subres
->data
);
2209 appendStringInfoChar(result
, '\n');
2215 xmldata_root_element_end(result
, xmlsn
);
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));
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.
2244 xsd_schema_element_start(StringInfo result
, const char *targetns
)
2246 appendStringInfoString(result
,
2248 " xmlns:xsd=\"" NAMESPACE_XSD
"\"");
2249 if (strlen(targetns
) > 0)
2250 appendStringInfo(result
,
2252 " targetNamespace=\"%s\"\n"
2253 " elementFormDefault=\"qualified\"",
2255 appendStringInfoString(result
,
2261 xsd_schema_element_end(StringInfo result
)
2263 appendStringInfoString(result
, "</xsd:schema>");
2268 schema_to_xmlschema_internal(const char *schemaname
, bool nulls
,
2269 bool tableforest
, const char *targetns
)
2277 result
= makeStringInfo();
2279 nspid
= LookupExplicitNamespace(schemaname
);
2281 xsd_schema_element_start(result
, targetns
);
2285 relid_list
= schema_get_xml_visible_tables(nspid
);
2288 foreach(cell
, relid_list
)
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
);
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
)));
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));
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
2354 database_to_xml_internal(const char *xmlschema
, bool nulls
,
2355 bool tableforest
, const char *targetns
)
2362 xmlcn
= map_sql_identifier_to_xml_name(get_database_name(MyDatabaseId
),
2364 result
= makeStringInfo();
2366 xmldata_root_element_start(result
, xmlcn
, xmlschema
, targetns
, true);
2369 appendStringInfo(result
, "%s\n\n", xmlschema
);
2373 nspid_list
= database_get_xml_visible_schemas();
2377 foreach(cell
, nspid_list
)
2379 Oid nspid
= lfirst_oid(cell
);
2382 subres
= schema_to_xml_internal(nspid
, NULL
, nulls
,
2383 tableforest
, targetns
, false);
2385 appendStringInfoString(result
, subres
->data
);
2386 appendStringInfoChar(result
, '\n');
2392 xmldata_root_element_end(result
, xmlcn
);
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
)));
2411 database_to_xmlschema_internal(bool nulls
, bool tableforest
,
2412 const char *targetns
)
2420 result
= makeStringInfo();
2422 xsd_schema_element_start(result
, targetns
);
2426 relid_list
= database_get_xml_visible_tables();
2427 nspid_list
= database_get_xml_visible_schemas();
2430 foreach(cell
, relid_list
)
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
);
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
)));
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
2485 map_multipart_sql_identifier_to_xml_name(char *a
, char *b
, char *c
, char *d
)
2487 StringInfoData result
;
2489 initStringInfo(&result
);
2492 appendStringInfo(&result
, "%s",
2493 map_sql_identifier_to_xml_name(a
, true, true));
2495 appendStringInfo(&result
, ".%s",
2496 map_sql_identifier_to_xml_name(b
, true, true));
2498 appendStringInfo(&result
, ".%s",
2499 map_sql_identifier_to_xml_name(c
, true, true));
2501 appendStringInfo(&result
, ".%s",
2502 map_sql_identifier_to_xml_name(d
, true, true));
2509 * Map an SQL table to an XML Schema document; see SQL/XML:2003
2512 * Map an SQL table to XML Schema data types; see SQL/XML:2003 section
2516 map_sql_table_to_xmlschema(TupleDesc tupdesc
, Oid relid
, bool nulls
,
2517 bool tableforest
, const char *targetns
)
2521 char *tabletypename
;
2523 StringInfoData result
;
2525 initStringInfo(&result
);
2527 if (OidIsValid(relid
))
2530 Form_pg_class reltuple
;
2532 tuple
= SearchSysCache(RELOID
,
2533 ObjectIdGetDatum(relid
),
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
),
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
);
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",
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
),
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");
2589 appendStringInfo(&result
,
2590 "<xsd:complexType name=\"%s\">\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
);
2602 appendStringInfo(&result
,
2603 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2604 xmltn
, rowtypename
);
2606 xsd_schema_element_end(&result
);
2613 * Map an SQL schema to XML Schema data types; see SQL/XML section
2617 map_sql_schema_to_xmlschema_types(Oid nspid
, List
*relid_list
, bool nulls
,
2618 bool tableforest
, const char *targetns
)
2623 char *schematypename
;
2624 StringInfoData result
;
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",
2639 appendStringInfo(&result
,
2640 "<xsd:complexType name=\"%s\">\n", schematypename
);
2642 appendStringInfoString(&result
,
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",
2659 appendStringInfo(&result
,
2660 " <xsd:element name=\"%s\" type=\"%s\"/>\n",
2661 xmltn
, tabletypename
);
2663 appendStringInfo(&result
,
2664 " <xsd:element name=\"%s\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n",
2665 xmltn
, tabletypename
);
2669 appendStringInfoString(&result
,
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
);
2686 * Map an SQL catalog to XML Schema data types; see SQL/XML section
2690 map_sql_catalog_to_xmlschema_types(List
*nspid_list
, bool nulls
,
2691 bool tableforest
, const char *targetns
)
2695 char *catalogtypename
;
2696 StringInfoData result
;
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",
2710 appendStringInfo(&result
,
2711 "<xsd:complexType name=\"%s\">\n", catalogtypename
);
2712 appendStringInfoString(&result
,
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",
2725 appendStringInfo(&result
,
2726 " <xsd:element name=\"%s\" type=\"%s\"/>\n",
2727 xmlsn
, schematypename
);
2730 appendStringInfoString(&result
,
2732 appendStringInfoString(&result
,
2733 "</xsd:complexType>\n\n");
2735 appendStringInfo(&result
,
2736 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2737 xmlcn
, catalogtypename
);
2744 * Map an SQL data type to an XML name; see SQL/XML:2003 section 9.9.
2747 map_sql_type_to_xml_name(Oid typeoid
, int typmod
)
2749 StringInfoData result
;
2751 initStringInfo(&result
);
2757 appendStringInfo(&result
, "CHAR");
2759 appendStringInfo(&result
, "CHAR_%d", typmod
- VARHDRSZ
);
2763 appendStringInfo(&result
, "VARCHAR");
2765 appendStringInfo(&result
, "VARCHAR_%d", typmod
- VARHDRSZ
);
2769 appendStringInfo(&result
, "NUMERIC");
2771 appendStringInfo(&result
, "NUMERIC_%d_%d",
2772 ((typmod
- VARHDRSZ
) >> 16) & 0xffff,
2773 (typmod
- VARHDRSZ
) & 0xffff);
2776 appendStringInfo(&result
, "INTEGER");
2779 appendStringInfo(&result
, "SMALLINT");
2782 appendStringInfo(&result
, "BIGINT");
2785 appendStringInfo(&result
, "REAL");
2788 appendStringInfo(&result
, "DOUBLE");
2791 appendStringInfo(&result
, "BOOLEAN");
2795 appendStringInfo(&result
, "TIME");
2797 appendStringInfo(&result
, "TIME_%d", typmod
);
2801 appendStringInfo(&result
, "TIME_WTZ");
2803 appendStringInfo(&result
, "TIME_WTZ_%d", typmod
);
2807 appendStringInfo(&result
, "TIMESTAMP");
2809 appendStringInfo(&result
, "TIMESTAMP_%d", typmod
);
2811 case TIMESTAMPTZOID
:
2813 appendStringInfo(&result
, "TIMESTAMP_WTZ");
2815 appendStringInfo(&result
, "TIMESTAMP_WTZ_%d", typmod
);
2818 appendStringInfo(&result
, "DATE");
2821 appendStringInfo(&result
, "XML");
2826 Form_pg_type typtuple
;
2828 tuple
= SearchSysCache(TYPEOID
,
2829 ObjectIdGetDatum(typeoid
),
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
);
2850 * Map a collection of SQL data types to XML Schema data types; see
2851 * SQL/XML:2002 section 9.10.
2854 map_sql_typecoll_to_xmlschema_types(List
*tupdesc_list
)
2856 List
*uniquetypes
= NIL
;
2858 StringInfoData result
;
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
)
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
),
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.)
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"
2920 " <xsd:any name=\"element\" minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>\n"
2921 " </xsd:sequence>\n"
2922 "</xsd:complexType>\n");
2926 appendStringInfo(&result
,
2927 "<xsd:simpleType name=\"%s\">\n", typename
);
2934 appendStringInfo(&result
,
2935 " <xsd:restriction base=\"xsd:string\">\n");
2937 appendStringInfo(&result
,
2938 " <xsd:maxLength value=\"%d\"/>\n",
2940 appendStringInfo(&result
,
2941 " </xsd:restriction>\n");
2945 appendStringInfo(&result
,
2946 " <xsd:restriction base=\"xsd:%s\">\n"
2947 " </xsd:restriction>\n",
2948 xmlbinary
== XMLBINARY_BASE64
? "base64Binary" : "hexBinary");
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);
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
);
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",
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)));
2991 appendStringInfo(&result
,
2992 " <xsd:restriction base=\"xsd:float\"></xsd:restriction>\n");
2996 appendStringInfo(&result
,
2997 " <xsd:restriction base=\"xsd:double\"></xsd:restriction>\n");
3001 appendStringInfo(&result
,
3002 " <xsd:restriction base=\"xsd:boolean\"></xsd:restriction>\n");
3008 const char *tz
= (typeoid
== TIMETZOID
? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
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
);
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
);
3029 case TIMESTAMPTZOID
:
3031 const char *tz
= (typeoid
== TIMESTAMPTZOID
? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
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
);
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
);
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");
3059 if (get_typtype(typeoid
) == TYPTYPE_DOMAIN
)
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
));
3072 appendStringInfo(&result
,
3073 "</xsd:simpleType>\n");
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.
3085 SPI_sql_row_to_xmlelement(int rownum
, StringInfo result
, char *tablename
,
3086 bool nulls
, bool tableforest
,
3087 const char *targetns
, bool top_level
)
3093 xmltn
= map_sql_identifier_to_xml_name(tablename
, true, false);
3103 xmldata_root_element_start(result
, xmltn
, NULL
, targetns
, top_level
);
3105 appendStringInfoString(result
, "<row>\n");
3107 for (i
= 1; i
<= SPI_tuptable
->tupdesc
->natts
; i
++)
3113 colname
= map_sql_identifier_to_xml_name(SPI_fname(SPI_tuptable
->tupdesc
, i
),
3115 colval
= SPI_getbinval(SPI_tuptable
->vals
[rownum
],
3116 SPI_tuptable
->tupdesc
,
3122 appendStringInfo(result
, " <%s xsi:nil=\"true\"/>\n", colname
);
3125 appendStringInfo(result
, " <%s>%s</%s>\n",
3127 map_sql_value_to_xml_value(colval
,
3128 SPI_gettypeid(SPI_tuptable
->tupdesc
, i
)),
3134 xmldata_root_element_end(result
, xmltn
);
3135 appendStringInfoChar(result
, '\n');
3138 appendStringInfoString(result
, "</row>\n\n");
3143 * XPath related functions
3148 * Convert XML node to text (dump subtree in case of element,
3149 * return value otherwise)
3152 xml_xmlnodetoxmltype(xmlNodePtr cur
)
3158 if (cur
->type
== XML_ELEMENT_NODE
)
3160 buf
= xmlBufferCreate();
3161 xmlNodeDump(buf
, NULL
, cur
, 0, 1);
3162 result
= xmlBuffer_to_xmltype(buf
);
3167 str
= xmlXPathCastNodeToString(cur
);
3168 result
= (xmltype
*) cstring_to_text((char *) str
);
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).
3188 xpath(PG_FUNCTION_ARGS
)
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
;
3197 xmlXPathContextPtr xpathctx
;
3198 xmlXPathCompExprPtr xpathcomp
;
3199 xmlXPathObjectPtr xpathobj
;
3204 xmlChar
*xpath_expr
;
3208 Datum
*ns_names_uris
;
3209 bool *ns_names_uris_nulls
;
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
);
3226 dims
= ARR_DIMS(namespaces
);
3228 if (ndim
!= 2 || dims
[1] != 2)
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
,
3240 Assert((ns_count
% 2) == 0); /* checked above */
3241 ns_count
/= 2; /* count pairs only */
3245 ns_names_uris
= NULL
;
3246 ns_names_uris_nulls
= NULL
;
3250 datastr
= VARDATA(data
);
3251 len
= VARSIZE(data
) - VARHDRSZ
;
3252 xpath_len
= VARSIZE(xpath_expr_text
) - VARHDRSZ
;
3255 (errcode(ERRCODE_DATA_EXCEPTION
),
3256 errmsg("empty XPath expression")));
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.
3267 xmlStrncmp((xmlChar
*) datastr
, (xmlChar
*) "<?xml", 5) == 0)
3271 !(datastr
[i
- 1] == '?' && datastr
[i
] == '>'))
3275 xml_ereport(ERROR
, ERRCODE_INTERNAL_ERROR
,
3276 "could not parse XML data");
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);
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';
3299 * redundant XML parsing (two parsings for the same value during one
3300 * command execution are possible)
3302 ctxt
= xmlNewParserCtxt();
3304 xml_ereport(ERROR
, ERRCODE_OUT_OF_MEMORY
,
3305 "could not allocate parser context");
3306 doc
= xmlCtxtReadMemory(ctxt
, (char *) string
, len
, NULL
, NULL
, 0);
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 */
3322 for (i
= 0; i
< ns_count
; i
++)
3327 if (ns_names_uris_nulls
[i
* 2] ||
3328 ns_names_uris_nulls
[i
* 2 + 1])
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\"",
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? */
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
)
3359 res_nitems
= xpathobj
->nodesetval
->nodeNr
;
3363 for (i
= 0; i
< xpathobj
->nodesetval
->nodeNr
; i
++)
3366 bool elemisnull
= false;
3368 elem
= PointerGetDatum(xml_xmlnodetoxmltype(xpathobj
->nodesetval
->nodeTab
[i
]));
3369 astate
= accumArrayResult(astate
, elem
,
3371 CurrentMemoryContext
);
3375 xmlXPathFreeObject(xpathobj
);
3376 xmlXPathFreeContext(xpathctx
);
3378 xmlFreeParserCtxt(ctxt
);
3380 if (res_nitems
== 0)
3381 PG_RETURN_ARRAYTYPE_P(construct_empty_array(XMLOID
));
3383 PG_RETURN_ARRAYTYPE_P(makeArrayResult(astate
, CurrentMemoryContext
));