OSDN Git Service

Fix bogus length calculation that could lead to crash if the string
[pg-rex/syncrep.git] / src / backend / utils / adt / xml.c
1 /*-------------------------------------------------------------------------
2  *
3  * xml.c
4  *        XML data type support.
5  *
6  *
7  * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.59 2007/11/20 23:14:41 tgl Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14
15 /*
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.
24  */
25
26 /*
27  * Note on memory management: Via callbacks, libxml is told to use
28  * palloc and friends for memory management.  Sometimes, libxml
29  * allocates global structures in the hope that it can reuse them
30  * later on, but if "later" is much later, the memory context
31  * management of PostgreSQL will have blown those structures away
32  * without telling libxml about it.  Therefore, it is important to
33  * call xmlCleanupParser() or perhaps some other cleanup function
34  * after using such functions, for example something from
35  * libxml/parser.h or libxml/xmlsave.h.  Unfortunately, you cannot
36  * readily tell from the API documentation when that happens, so
37  * careful evaluation is necessary when introducing new libxml APIs
38  * here.
39  */
40
41 #include "postgres.h"
42
43 #ifdef USE_LIBXML
44 #include <libxml/chvalid.h>
45 #include <libxml/parser.h>
46 #include <libxml/tree.h>
47 #include <libxml/uri.h>
48 #include <libxml/xmlerror.h>
49 #include <libxml/xmlwriter.h>
50 #include <libxml/xpath.h>
51 #include <libxml/xpathInternals.h>
52 #endif   /* USE_LIBXML */
53
54 #include "catalog/namespace.h"
55 #include "catalog/pg_type.h"
56 #include "commands/dbcommands.h"
57 #include "executor/executor.h"
58 #include "executor/spi.h"
59 #include "fmgr.h"
60 #include "lib/stringinfo.h"
61 #include "libpq/pqformat.h"
62 #include "mb/pg_wchar.h"
63 #include "miscadmin.h"
64 #include "nodes/execnodes.h"
65 #include "parser/parse_expr.h"
66 #include "utils/array.h"
67 #include "utils/builtins.h"
68 #include "utils/date.h"
69 #include "utils/datetime.h"
70 #include "utils/lsyscache.h"
71 #include "utils/memutils.h"
72 #include "access/tupmacs.h"
73 #include "utils/xml.h"
74
75
76 /* GUC variables */
77 XmlBinaryType xmlbinary;
78 XmlOptionType xmloption;
79
80 #ifdef USE_LIBXML
81
82 static StringInfo xml_err_buf = NULL;
83
84 static void xml_init(void);
85 static void *xml_palloc(size_t size);
86 static void *xml_repalloc(void *ptr, size_t size);
87 static void xml_pfree(void *ptr);
88 static char *xml_pstrdup(const char *string);
89 static void xml_ereport(int level, int sqlcode, const char *msg);
90 static void xml_errorHandler(void *ctxt, const char *msg,...);
91 static void xml_ereport_by_code(int level, int sqlcode,
92                                         const char *msg, int errcode);
93 static xmlChar *xml_text2xmlChar(text *in);
94 static int parse_xml_decl(const xmlChar * str, size_t *lenp,
95                            xmlChar ** version, xmlChar ** encoding, int *standalone);
96 static bool print_xml_decl(StringInfo buf, const xmlChar * version,
97                            pg_enc encoding, int standalone);
98 static xmlDocPtr xml_parse(text *data, XmlOptionType xmloption_arg,
99                   bool preserve_whitespace, xmlChar * encoding);
100 static text *xml_xmlnodetoxmltype(xmlNodePtr cur);
101 #endif   /* USE_LIBXML */
102
103 static StringInfo query_to_xml_internal(const char *query, char *tablename,
104                                           const char *xmlschema, bool nulls, bool tableforest,
105                                           const char *targetns, bool top_level);
106 static const char *map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid,
107                                                  bool nulls, bool tableforest, const char *targetns);
108 static const char *map_sql_schema_to_xmlschema_types(Oid nspid,
109                                                                   List *relid_list, bool nulls,
110                                                                   bool tableforest, const char *targetns);
111 static const char *map_sql_catalog_to_xmlschema_types(List *nspid_list,
112                                                                    bool nulls, bool tableforest,
113                                                                    const char *targetns);
114 static const char *map_sql_type_to_xml_name(Oid typeoid, int typmod);
115 static const char *map_sql_typecoll_to_xmlschema_types(List *tupdesc_list);
116 static const char *map_sql_type_to_xmlschema_type(Oid typeoid, int typmod);
117 static void SPI_sql_row_to_xmlelement(int rownum, StringInfo result,
118                                                   char *tablename, bool nulls, bool tableforest,
119                                                   const char *targetns, bool top_level);
120
121 #define NO_XML_SUPPORT() \
122         ereport(ERROR, \
123                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
124                          errmsg("unsupported XML feature"), \
125                          errdetail("This functionality requires the server to be built with libxml support."), \
126                          errhint("You need to rebuild PostgreSQL using --with-libxml.")))
127
128
129 #define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str))
130 #define _textout(x) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(x)))
131
132
133 /* from SQL/XML:2003 section 4.7 */
134 #define NAMESPACE_XSD "http://www.w3.org/2001/XMLSchema"
135 #define NAMESPACE_XSI "http://www.w3.org/2001/XMLSchema-instance"
136 #define NAMESPACE_SQLXML "http://standards.iso.org/iso/9075/2003/sqlxml"
137
138
139 #ifdef USE_LIBXML
140
141 static int
142 xmlChar_to_encoding(xmlChar * encoding_name)
143 {
144         int                     encoding = pg_char_to_encoding((char *) encoding_name);
145
146         if (encoding < 0)
147                 ereport(ERROR,
148                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
149                                  errmsg("invalid encoding name \"%s\"",
150                                                 (char *) encoding_name)));
151         return encoding;
152 }
153 #endif
154
155
156 Datum
157 xml_in(PG_FUNCTION_ARGS)
158 {
159 #ifdef USE_LIBXML
160         char       *s = PG_GETARG_CSTRING(0);
161         size_t          len;
162         xmltype    *vardata;
163         xmlDocPtr       doc;
164
165         len = strlen(s);
166         vardata = palloc(len + VARHDRSZ);
167         SET_VARSIZE(vardata, len + VARHDRSZ);
168         memcpy(VARDATA(vardata), s, len);
169
170         /*
171          * Parse the data to check if it is well-formed XML data.  Assume that
172          * ERROR occurred if parsing failed.
173          */
174         doc = xml_parse(vardata, xmloption, true, NULL);
175         xmlFreeDoc(doc);
176
177         PG_RETURN_XML_P(vardata);
178 #else
179         NO_XML_SUPPORT();
180         return 0;
181 #endif
182 }
183
184
185 #define PG_XML_DEFAULT_VERSION "1.0"
186
187
188 static char *
189 xml_out_internal(xmltype *x, pg_enc target_encoding)
190 {
191         char       *str;
192         size_t          len;
193
194 #ifdef USE_LIBXML
195         xmlChar    *version;
196         xmlChar    *encoding;
197         int                     standalone;
198         int                     res_code;
199 #endif
200
201         len = VARSIZE(x) - VARHDRSZ;
202         str = palloc(len + 1);
203         memcpy(str, VARDATA(x), len);
204         str[len] = '\0';
205
206 #ifdef USE_LIBXML
207         if ((res_code = parse_xml_decl((xmlChar *) str,
208                                                            &len, &version, &encoding, &standalone)) == 0)
209         {
210                 StringInfoData buf;
211
212                 initStringInfo(&buf);
213
214                 if (!print_xml_decl(&buf, version, target_encoding, standalone))
215                 {
216                         /*
217                          * If we are not going to produce an XML declaration, eat a single
218                          * newline in the original string to prevent empty first lines in
219                          * the output.
220                          */
221                         if (*(str + len) == '\n')
222                                 len += 1;
223                 }
224                 appendStringInfoString(&buf, str + len);
225
226                 return buf.data;
227         }
228
229         xml_ereport_by_code(WARNING, ERRCODE_INTERNAL_ERROR,
230                                                 "could not parse XML declaration in stored value",
231                                                 res_code);
232 #endif
233         return str;
234 }
235
236
237 Datum
238 xml_out(PG_FUNCTION_ARGS)
239 {
240         xmltype    *x = PG_GETARG_XML_P(0);
241
242         /*
243          * xml_out removes the encoding property in all cases.  This is because we
244          * cannot control from here whether the datum will be converted to a
245          * different client encoding, so we'd do more harm than good by including
246          * it.
247          */
248         PG_RETURN_CSTRING(xml_out_internal(x, 0));
249 }
250
251
252 Datum
253 xml_recv(PG_FUNCTION_ARGS)
254 {
255 #ifdef USE_LIBXML
256         StringInfo      buf = (StringInfo) PG_GETARG_POINTER(0);
257         xmltype    *result;
258         char       *str;
259         char       *newstr;
260         int                     nbytes;
261         xmlDocPtr       doc;
262         xmlChar    *encoding = NULL;
263
264         /*
265          * Read the data in raw format. We don't know yet what the encoding is, as
266          * that information is embedded in the xml declaration; so we have to
267          * parse that before converting to server encoding.
268          */
269         nbytes = buf->len - buf->cursor;
270         str = (char *) pq_getmsgbytes(buf, nbytes);
271
272         /*
273          * We need a null-terminated string to pass to parse_xml_decl().  Rather
274          * than make a separate copy, make the temporary result one byte bigger
275          * than it needs to be.
276          */
277         result = palloc(nbytes + 1 + VARHDRSZ);
278         SET_VARSIZE(result, nbytes + VARHDRSZ);
279         memcpy(VARDATA(result), str, nbytes);
280         str = VARDATA(result);
281         str[nbytes] = '\0';
282
283         parse_xml_decl((xmlChar *) str, NULL, NULL, &encoding, NULL);
284
285         /*
286          * Parse the data to check if it is well-formed XML data.  Assume that
287          * xml_parse will throw ERROR if not.
288          */
289         doc = xml_parse(result, xmloption, true, encoding);
290         xmlFreeDoc(doc);
291
292         /* Now that we know what we're dealing with, convert to server encoding */
293         newstr = (char *) pg_do_encoding_conversion((unsigned char *) str,
294                                                                                                 nbytes,
295                                                                                                 encoding ?
296                                                                                           xmlChar_to_encoding(encoding) :
297                                                                                                 PG_UTF8,
298                                                                                                 GetDatabaseEncoding());
299
300         if (newstr != str)
301         {
302                 pfree(result);
303
304                 nbytes = strlen(newstr);
305
306                 result = palloc(nbytes + VARHDRSZ);
307                 SET_VARSIZE(result, nbytes + VARHDRSZ);
308                 memcpy(VARDATA(result), newstr, nbytes);
309
310                 pfree(newstr);
311         }
312
313         PG_RETURN_XML_P(result);
314 #else
315         NO_XML_SUPPORT();
316         return 0;
317 #endif
318 }
319
320
321 Datum
322 xml_send(PG_FUNCTION_ARGS)
323 {
324         xmltype    *x = PG_GETARG_XML_P(0);
325         char       *outval;
326         StringInfoData buf;
327
328         /*
329          * xml_out_internal doesn't convert the encoding, it just prints the right
330          * declaration. pq_sendtext will do the conversion.
331          */
332         outval = xml_out_internal(x, pg_get_client_encoding());
333
334         pq_begintypsend(&buf);
335         pq_sendtext(&buf, outval, strlen(outval));
336         pfree(outval);
337         PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
338 }
339
340
341 #ifdef USE_LIBXML
342 static void
343 appendStringInfoText(StringInfo str, const text *t)
344 {
345         appendBinaryStringInfo(str, VARDATA(t), VARSIZE(t) - VARHDRSZ);
346 }
347 #endif
348
349
350 static xmltype *
351 stringinfo_to_xmltype(StringInfo buf)
352 {
353         int32           len;
354         xmltype    *result;
355
356         len = buf->len + VARHDRSZ;
357         result = palloc(len);
358         SET_VARSIZE(result, len);
359         memcpy(VARDATA(result), buf->data, buf->len);
360
361         return result;
362 }
363
364
365 static xmltype *
366 cstring_to_xmltype(const char *string)
367 {
368         int32           len;
369         xmltype    *result;
370
371         len = strlen(string) + VARHDRSZ;
372         result = palloc(len);
373         SET_VARSIZE(result, len);
374         memcpy(VARDATA(result), string, len - VARHDRSZ);
375
376         return result;
377 }
378
379
380 #ifdef USE_LIBXML
381 static xmltype *
382 xmlBuffer_to_xmltype(xmlBufferPtr buf)
383 {
384         int32           len;
385         xmltype    *result;
386
387         len = xmlBufferLength(buf) + VARHDRSZ;
388         result = palloc(len);
389         SET_VARSIZE(result, len);
390         memcpy(VARDATA(result), xmlBufferContent(buf), len - VARHDRSZ);
391
392         return result;
393 }
394 #endif
395
396
397 Datum
398 xmlcomment(PG_FUNCTION_ARGS)
399 {
400 #ifdef USE_LIBXML
401         text       *arg = PG_GETARG_TEXT_P(0);
402         char       *argdata = VARDATA(arg);
403         int                     len = VARSIZE(arg) - VARHDRSZ;
404         StringInfoData buf;
405         int                     i;
406
407         /* check for "--" in string or "-" at the end */
408         for (i = 1; i < len; i++)
409         {
410                 if (argdata[i] == '-' && argdata[i - 1] == '-')
411                         ereport(ERROR,
412                                         (errcode(ERRCODE_INVALID_XML_COMMENT),
413                                          errmsg("invalid XML comment")));
414         }
415         if (len > 0 && argdata[len - 1] == '-')
416                 ereport(ERROR,
417                                 (errcode(ERRCODE_INVALID_XML_COMMENT),
418                                  errmsg("invalid XML comment")));
419
420         initStringInfo(&buf);
421         appendStringInfo(&buf, "<!--");
422         appendStringInfoText(&buf, arg);
423         appendStringInfo(&buf, "-->");
424
425         PG_RETURN_XML_P(stringinfo_to_xmltype(&buf));
426 #else
427         NO_XML_SUPPORT();
428         return 0;
429 #endif
430 }
431
432
433
434 /*
435  * TODO: xmlconcat needs to merge the notations and unparsed entities
436  * of the argument values.      Not very important in practice, though.
437  */
438 xmltype *
439 xmlconcat(List *args)
440 {
441 #ifdef USE_LIBXML
442         int                     global_standalone = 1;
443         xmlChar    *global_version = NULL;
444         bool            global_version_no_value = false;
445         StringInfoData buf;
446         ListCell   *v;
447
448         initStringInfo(&buf);
449         foreach(v, args)
450         {
451                 xmltype    *x = DatumGetXmlP(PointerGetDatum(lfirst(v)));
452                 size_t          len;
453                 xmlChar    *version;
454                 int                     standalone;
455                 char       *str;
456
457                 len = VARSIZE(x) - VARHDRSZ;
458                 str = palloc(len + 1);
459                 memcpy(str, VARDATA(x), len);
460                 str[len] = '\0';
461
462                 parse_xml_decl((xmlChar *) str, &len, &version, NULL, &standalone);
463
464                 if (standalone == 0 && global_standalone == 1)
465                         global_standalone = 0;
466                 if (standalone < 0)
467                         global_standalone = -1;
468
469                 if (!version)
470                         global_version_no_value = true;
471                 else if (!global_version)
472                         global_version = xmlStrdup(version);
473                 else if (xmlStrcmp(version, global_version) != 0)
474                         global_version_no_value = true;
475
476                 appendStringInfoString(&buf, str + len);
477                 pfree(str);
478         }
479
480         if (!global_version_no_value || global_standalone >= 0)
481         {
482                 StringInfoData buf2;
483
484                 initStringInfo(&buf2);
485
486                 print_xml_decl(&buf2,
487                                            (!global_version_no_value) ? global_version : NULL,
488                                            0,
489                                            global_standalone);
490
491                 appendStringInfoString(&buf2, buf.data);
492                 buf = buf2;
493         }
494
495         return stringinfo_to_xmltype(&buf);
496 #else
497         NO_XML_SUPPORT();
498         return NULL;
499 #endif
500 }
501
502
503 /*
504  * XMLAGG support
505  */
506 Datum
507 xmlconcat2(PG_FUNCTION_ARGS)
508 {
509         if (PG_ARGISNULL(0))
510         {
511                 if (PG_ARGISNULL(1))
512                         PG_RETURN_NULL();
513                 else
514                         PG_RETURN_XML_P(PG_GETARG_XML_P(1));
515         }
516         else if (PG_ARGISNULL(1))
517                 PG_RETURN_XML_P(PG_GETARG_XML_P(0));
518         else
519                 PG_RETURN_XML_P(xmlconcat(list_make2(PG_GETARG_XML_P(0),
520                                                                                          PG_GETARG_XML_P(1))));
521 }
522
523
524 Datum
525 texttoxml(PG_FUNCTION_ARGS)
526 {
527         text       *data = PG_GETARG_TEXT_P(0);
528
529         PG_RETURN_XML_P(xmlparse(data, xmloption, true));
530 }
531
532
533 Datum
534 xmltotext(PG_FUNCTION_ARGS)
535 {
536         xmltype    *data = PG_GETARG_XML_P(0);
537
538         PG_RETURN_TEXT_P(xmltotext_with_xmloption(data, xmloption));
539 }
540
541
542 text *
543 xmltotext_with_xmloption(xmltype *data, XmlOptionType xmloption_arg)
544 {
545         if (xmloption_arg == XMLOPTION_DOCUMENT && !xml_is_document(data))
546                 ereport(ERROR,
547                                 (errcode(ERRCODE_NOT_AN_XML_DOCUMENT),
548                                  errmsg("not an XML document")));
549
550         /* It's actually binary compatible, save for the above check. */
551         return (text *) data;
552 }
553
554
555 xmltype *
556 xmlelement(XmlExprState *xmlExpr, ExprContext *econtext)
557 {
558 #ifdef USE_LIBXML
559         XmlExpr    *xexpr = (XmlExpr *) xmlExpr->xprstate.expr;
560         xmltype    *result;
561         List       *named_arg_strings;
562         List       *arg_strings;
563         int                     i;
564         ListCell   *arg;
565         ListCell   *narg;
566         xmlBufferPtr buf;
567         xmlTextWriterPtr writer;
568
569         /*
570          * We first evaluate all the arguments, then start up libxml and create
571          * the result.  This avoids issues if one of the arguments involves a call
572          * to some other function or subsystem that wants to use libxml on its own
573          * terms.
574          */
575         named_arg_strings = NIL;
576         i = 0;
577         foreach(arg, xmlExpr->named_args)
578         {
579                 ExprState  *e = (ExprState *) lfirst(arg);
580                 Datum           value;
581                 bool            isnull;
582                 char       *str;
583
584                 value = ExecEvalExpr(e, econtext, &isnull, NULL);
585                 if (isnull)
586                         str = NULL;
587                 else
588                         str = OutputFunctionCall(&xmlExpr->named_outfuncs[i], value);
589                 named_arg_strings = lappend(named_arg_strings, str);
590                 i++;
591         }
592
593         arg_strings = NIL;
594         foreach(arg, xmlExpr->args)
595         {
596                 ExprState  *e = (ExprState *) lfirst(arg);
597                 Datum           value;
598                 bool            isnull;
599                 char       *str;
600
601                 value = ExecEvalExpr(e, econtext, &isnull, NULL);
602                 /* here we can just forget NULL elements immediately */
603                 if (!isnull)
604                 {
605                         str = map_sql_value_to_xml_value(value,
606                                                                                          exprType((Node *) e->expr));
607                         arg_strings = lappend(arg_strings, str);
608                 }
609         }
610
611         /* now safe to run libxml */
612         xml_init();
613
614         buf = xmlBufferCreate();
615         writer = xmlNewTextWriterMemory(buf, 0);
616
617         xmlTextWriterStartElement(writer, (xmlChar *) xexpr->name);
618
619         forboth(arg, named_arg_strings, narg, xexpr->arg_names)
620         {
621                 char       *str = (char *) lfirst(arg);
622                 char       *argname = strVal(lfirst(narg));
623
624                 if (str)
625                 {
626                         xmlTextWriterWriteAttribute(writer,
627                                                                                 (xmlChar *) argname,
628                                                                                 (xmlChar *) str);
629                         pfree(str);
630                 }
631         }
632
633         foreach(arg, arg_strings)
634         {
635                 char       *str = (char *) lfirst(arg);
636
637                 xmlTextWriterWriteRaw(writer, (xmlChar *) str);
638         }
639
640         xmlTextWriterEndElement(writer);
641         xmlFreeTextWriter(writer);
642
643         result = xmlBuffer_to_xmltype(buf);
644         xmlBufferFree(buf);
645
646         return result;
647 #else
648         NO_XML_SUPPORT();
649         return NULL;
650 #endif
651 }
652
653
654 xmltype *
655 xmlparse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace)
656 {
657 #ifdef USE_LIBXML
658         xmlDocPtr       doc;
659
660         doc = xml_parse(data, xmloption_arg, preserve_whitespace, NULL);
661         xmlFreeDoc(doc);
662
663         return (xmltype *) data;
664 #else
665         NO_XML_SUPPORT();
666         return NULL;
667 #endif
668 }
669
670
671 xmltype *
672 xmlpi(char *target, text *arg, bool arg_is_null, bool *result_is_null)
673 {
674 #ifdef USE_LIBXML
675         xmltype    *result;
676         StringInfoData buf;
677
678         if (pg_strcasecmp(target, "xml") == 0)
679                 ereport(ERROR,
680                                 (errcode(ERRCODE_SYNTAX_ERROR), /* really */
681                                  errmsg("invalid XML processing instruction"),
682                                  errdetail("XML processing instruction target name cannot be \"%s\".", target)));
683
684         /*
685          * Following the SQL standard, the null check comes after the syntax check
686          * above.
687          */
688         *result_is_null = arg_is_null;
689         if (*result_is_null)
690                 return NULL;
691
692         initStringInfo(&buf);
693
694         appendStringInfo(&buf, "<?%s", target);
695
696         if (arg != NULL)
697         {
698                 char       *string;
699
700                 string = _textout(arg);
701                 if (strstr(string, "?>") != NULL)
702                         ereport(ERROR,
703                                         (errcode(ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION),
704                                          errmsg("invalid XML processing instruction"),
705                         errdetail("XML processing instruction cannot contain \"?>\".")));
706
707                 appendStringInfoChar(&buf, ' ');
708                 appendStringInfoString(&buf, string + strspn(string, " "));
709                 pfree(string);
710         }
711         appendStringInfoString(&buf, "?>");
712
713         result = stringinfo_to_xmltype(&buf);
714         pfree(buf.data);
715         return result;
716 #else
717         NO_XML_SUPPORT();
718         return NULL;
719 #endif
720 }
721
722
723 xmltype *
724 xmlroot(xmltype *data, text *version, int standalone)
725 {
726 #ifdef USE_LIBXML
727         char       *str;
728         size_t          len;
729         xmlChar    *orig_version;
730         int                     orig_standalone;
731         StringInfoData buf;
732
733         len = VARSIZE(data) - VARHDRSZ;
734         str = palloc(len + 1);
735         memcpy(str, VARDATA(data), len);
736         str[len] = '\0';
737
738         parse_xml_decl((xmlChar *) str, &len, &orig_version, NULL, &orig_standalone);
739
740         if (version)
741                 orig_version = xml_text2xmlChar(version);
742         else
743                 orig_version = NULL;
744
745         switch (standalone)
746         {
747                 case XML_STANDALONE_YES:
748                         orig_standalone = 1;
749                         break;
750                 case XML_STANDALONE_NO:
751                         orig_standalone = 0;
752                         break;
753                 case XML_STANDALONE_NO_VALUE:
754                         orig_standalone = -1;
755                         break;
756                 case XML_STANDALONE_OMITTED:
757                         /* leave original value */
758                         break;
759         }
760
761         initStringInfo(&buf);
762         print_xml_decl(&buf, orig_version, 0, orig_standalone);
763         appendStringInfoString(&buf, str + len);
764
765         return stringinfo_to_xmltype(&buf);
766 #else
767         NO_XML_SUPPORT();
768         return NULL;
769 #endif
770 }
771
772
773 /*
774  * Validate document (given as string) against DTD (given as external link)
775  * TODO !!! use text instead of cstring for second arg
776  * TODO allow passing DTD as a string value (not only as an URI)
777  * TODO redesign (see comment with '!!!' below)
778  */
779 Datum
780 xmlvalidate(PG_FUNCTION_ARGS)
781 {
782 #ifdef USE_LIBXML
783         text       *data = PG_GETARG_TEXT_P(0);
784         text       *dtdOrUri = PG_GETARG_TEXT_P(1);
785         bool            result = false;
786         xmlParserCtxtPtr ctxt = NULL;
787         xmlDocPtr       doc = NULL;
788         xmlDtdPtr       dtd = NULL;
789
790         xml_init();
791
792         /* We use a PG_TRY block to ensure libxml parser is cleaned up on error */
793         PG_TRY();
794         {
795                 xmlInitParser();
796                 ctxt = xmlNewParserCtxt();
797                 if (ctxt == NULL)
798                         xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
799                                                 "could not allocate parser context");
800
801                 doc = xmlCtxtReadMemory(ctxt, (char *) VARDATA(data),
802                                                                 VARSIZE(data) - VARHDRSZ,
803                                                                 NULL, NULL, 0);
804                 if (doc == NULL)
805                         xml_ereport(ERROR, ERRCODE_INVALID_XML_DOCUMENT,
806                                                 "could not parse XML data");
807
808 #if 0
809                 uri = xmlCreateURI();
810                 elog(NOTICE, "dtd - %s", dtdOrUri);
811                 dtd = palloc(sizeof(xmlDtdPtr));
812                 uri = xmlParseURI(dtdOrUri);
813                 if (uri == NULL)
814                         xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
815                                                 "not implemented yet... (TODO)");
816                 else
817 #endif
818                         dtd = xmlParseDTD(NULL, xml_text2xmlChar(dtdOrUri));
819
820                 if (dtd == NULL)
821                         xml_ereport(ERROR, ERRCODE_INVALID_XML_DOCUMENT,
822                                                 "could not load DTD");
823
824                 if (xmlValidateDtd(xmlNewValidCtxt(), doc, dtd) == 1)
825                         result = true;
826
827                 if (!result)
828                         xml_ereport(NOTICE, ERRCODE_INVALID_XML_DOCUMENT,
829                                                 "validation against DTD failed");
830
831 #if 0
832                 if (uri)
833                         xmlFreeURI(uri);
834                 uri = NULL;
835 #endif
836                 if (dtd)
837                         xmlFreeDtd(dtd);
838                 dtd = NULL;
839                 if (doc)
840                         xmlFreeDoc(doc);
841                 doc = NULL;
842                 if (ctxt)
843                         xmlFreeParserCtxt(ctxt);
844                 ctxt = NULL;
845                 xmlCleanupParser();
846         }
847         PG_CATCH();
848         {
849 #if 0
850                 if (uri)
851                         xmlFreeURI(uri);
852 #endif
853                 if (dtd)
854                         xmlFreeDtd(dtd);
855                 if (doc)
856                         xmlFreeDoc(doc);
857                 if (ctxt)
858                         xmlFreeParserCtxt(ctxt);
859                 xmlCleanupParser();
860
861                 PG_RE_THROW();
862         }
863         PG_END_TRY();
864
865         PG_RETURN_BOOL(result);
866 #else                                                   /* not USE_LIBXML */
867         NO_XML_SUPPORT();
868         return 0;
869 #endif   /* not USE_LIBXML */
870 }
871
872
873 bool
874 xml_is_document(xmltype *arg)
875 {
876 #ifdef USE_LIBXML
877         bool            result;
878         xmlDocPtr       doc = NULL;
879         MemoryContext ccxt = CurrentMemoryContext;
880
881         PG_TRY();
882         {
883                 doc = xml_parse((text *) arg, XMLOPTION_DOCUMENT, true, NULL);
884                 result = true;
885         }
886         PG_CATCH();
887         {
888                 ErrorData  *errdata;
889                 MemoryContext ecxt;
890
891                 ecxt = MemoryContextSwitchTo(ccxt);
892                 errdata = CopyErrorData();
893                 if (errdata->sqlerrcode == ERRCODE_INVALID_XML_DOCUMENT)
894                 {
895                         FlushErrorState();
896                         result = false;
897                 }
898                 else
899                 {
900                         MemoryContextSwitchTo(ecxt);
901                         PG_RE_THROW();
902                 }
903         }
904         PG_END_TRY();
905
906         if (doc)
907                 xmlFreeDoc(doc);
908
909         return result;
910 #else                                                   /* not USE_LIBXML */
911         NO_XML_SUPPORT();
912         return false;
913 #endif   /* not USE_LIBXML */
914 }
915
916
917 #ifdef USE_LIBXML
918
919 /*
920  * Set up for use of libxml --- this should be called by each function that
921  * is about to use libxml facilities.
922  *
923  * TODO: xmlChar is utf8-char, make proper tuning (initdb with enc!=utf8 and
924  * check)
925  */
926 static void
927 xml_init(void)
928 {
929         static bool first_time = true;
930
931         if (first_time)
932         {
933                 /* Stuff we need do only once per session */
934                 MemoryContext oldcontext;
935
936                 /*
937                  * Currently, we have no pure UTF-8 support for internals -- check if
938                  * we can work.
939                  */
940                 if (sizeof(char) != sizeof(xmlChar))
941                         ereport(ERROR,
942                                         (errmsg("could not initialize XML library"),
943                                          errdetail("libxml2 has incompatible char type: sizeof(char)=%u, sizeof(xmlChar)=%u.",
944                                                            (int) sizeof(char), (int) sizeof(xmlChar))));
945
946                 /* create error buffer in permanent context */
947                 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
948                 xml_err_buf = makeStringInfo();
949                 MemoryContextSwitchTo(oldcontext);
950
951                 /* Now that xml_err_buf exists, safe to call xml_errorHandler */
952                 xmlSetGenericErrorFunc(NULL, xml_errorHandler);
953
954                 /* Set up memory allocation our way, too */
955                 xmlMemSetup(xml_pfree, xml_palloc, xml_repalloc, xml_pstrdup);
956
957                 /* Check library compatibility */
958                 LIBXML_TEST_VERSION;
959
960                 first_time = false;
961         }
962         else
963         {
964                 /* Reset pre-existing buffer to empty */
965                 Assert(xml_err_buf != NULL);
966                 resetStringInfo(xml_err_buf);
967
968                 /*
969                  * We re-establish the callback functions every time.  This makes it
970                  * safe for other subsystems (PL/Perl, say) to also use libxml with
971                  * their own callbacks ... so long as they likewise set up the
972                  * callbacks on every use.      It's cheap enough to not be worth worrying
973                  * about, anyway.
974                  */
975                 xmlSetGenericErrorFunc(NULL, xml_errorHandler);
976                 xmlMemSetup(xml_pfree, xml_palloc, xml_repalloc, xml_pstrdup);
977         }
978 }
979
980
981 /*
982  * SQL/XML allows storing "XML documents" or "XML content".  "XML
983  * documents" are specified by the XML specification and are parsed
984  * easily by libxml.  "XML content" is specified by SQL/XML as the
985  * production "XMLDecl? content".  But libxml can only parse the
986  * "content" part, so we have to parse the XML declaration ourselves
987  * to complete this.
988  */
989
990 #define CHECK_XML_SPACE(p) \
991         do { \
992                 if (!xmlIsBlank_ch(*(p))) \
993                         return XML_ERR_SPACE_REQUIRED; \
994         } while (0)
995
996 #define SKIP_XML_SPACE(p) \
997         while (xmlIsBlank_ch(*(p))) (p)++
998
999 /* Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender */
1000 /* Beware of multiple evaluations of argument! */
1001 #define PG_XMLISNAMECHAR(c) \
1002         (xmlIsBaseChar_ch(c) || xmlIsIdeographicQ(c) \
1003                         || xmlIsDigit_ch(c) \
1004                         || c == '.' || c == '-' || c == '_' || c == ':' \
1005                         || xmlIsCombiningQ(c) \
1006                         || xmlIsExtender_ch(c))
1007
1008 static int
1009 parse_xml_decl(const xmlChar * str, size_t *lenp,
1010                            xmlChar ** version, xmlChar ** encoding, int *standalone)
1011 {
1012         const xmlChar *p;
1013         const xmlChar *save_p;
1014         size_t          len;
1015         int                     utf8char;
1016         int                     utf8len;
1017
1018         xml_init();
1019
1020         if (version)
1021                 *version = NULL;
1022         if (encoding)
1023                 *encoding = NULL;
1024         if (standalone)
1025                 *standalone = -1;
1026
1027         p = str;
1028
1029         if (xmlStrncmp(p, (xmlChar *) "<?xml", 5) != 0)
1030                 goto finished;
1031
1032         /* if next char is name char, it's a PI like <?xml-stylesheet ...?> */
1033         utf8len = strlen((const char *) (p + 5));
1034         utf8char = xmlGetUTF8Char(p + 5, &utf8len);
1035         if (PG_XMLISNAMECHAR(utf8char))
1036                 goto finished;
1037
1038         p += 5;
1039
1040         /* version */
1041         CHECK_XML_SPACE(p);
1042         SKIP_XML_SPACE(p);
1043         if (xmlStrncmp(p, (xmlChar *) "version", 7) != 0)
1044                 return XML_ERR_VERSION_MISSING;
1045         p += 7;
1046         SKIP_XML_SPACE(p);
1047         if (*p != '=')
1048                 return XML_ERR_VERSION_MISSING;
1049         p += 1;
1050         SKIP_XML_SPACE(p);
1051
1052         if (*p == '\'' || *p == '"')
1053         {
1054                 const xmlChar *q;
1055
1056                 q = xmlStrchr(p + 1, *p);
1057                 if (!q)
1058                         return XML_ERR_VERSION_MISSING;
1059
1060                 if (version)
1061                         *version = xmlStrndup(p + 1, q - p - 1);
1062                 p = q + 1;
1063         }
1064         else
1065                 return XML_ERR_VERSION_MISSING;
1066
1067         /* encoding */
1068         save_p = p;
1069         SKIP_XML_SPACE(p);
1070         if (xmlStrncmp(p, (xmlChar *) "encoding", 8) == 0)
1071         {
1072                 CHECK_XML_SPACE(save_p);
1073                 p += 8;
1074                 SKIP_XML_SPACE(p);
1075                 if (*p != '=')
1076                         return XML_ERR_MISSING_ENCODING;
1077                 p += 1;
1078                 SKIP_XML_SPACE(p);
1079
1080                 if (*p == '\'' || *p == '"')
1081                 {
1082                         const xmlChar *q;
1083
1084                         q = xmlStrchr(p + 1, *p);
1085                         if (!q)
1086                                 return XML_ERR_MISSING_ENCODING;
1087
1088                         if (encoding)
1089                                 *encoding = xmlStrndup(p + 1, q - p - 1);
1090                         p = q + 1;
1091                 }
1092                 else
1093                         return XML_ERR_MISSING_ENCODING;
1094         }
1095         else
1096         {
1097                 p = save_p;
1098         }
1099
1100         /* standalone */
1101         save_p = p;
1102         SKIP_XML_SPACE(p);
1103         if (xmlStrncmp(p, (xmlChar *) "standalone", 10) == 0)
1104         {
1105                 CHECK_XML_SPACE(save_p);
1106                 p += 10;
1107                 SKIP_XML_SPACE(p);
1108                 if (*p != '=')
1109                         return XML_ERR_STANDALONE_VALUE;
1110                 p += 1;
1111                 SKIP_XML_SPACE(p);
1112                 if (xmlStrncmp(p, (xmlChar *) "'yes'", 5) == 0 ||
1113                         xmlStrncmp(p, (xmlChar *) "\"yes\"", 5) == 0)
1114                 {
1115                         *standalone = 1;
1116                         p += 5;
1117                 }
1118                 else if (xmlStrncmp(p, (xmlChar *) "'no'", 4) == 0 ||
1119                                  xmlStrncmp(p, (xmlChar *) "\"no\"", 4) == 0)
1120                 {
1121                         *standalone = 0;
1122                         p += 4;
1123                 }
1124                 else
1125                         return XML_ERR_STANDALONE_VALUE;
1126         }
1127         else
1128         {
1129                 p = save_p;
1130         }
1131
1132         SKIP_XML_SPACE(p);
1133         if (xmlStrncmp(p, (xmlChar *) "?>", 2) != 0)
1134                 return XML_ERR_XMLDECL_NOT_FINISHED;
1135         p += 2;
1136
1137 finished:
1138         len = p - str;
1139
1140         for (p = str; p < str + len; p++)
1141                 if (*p > 127)
1142                         return XML_ERR_INVALID_CHAR;
1143
1144         if (lenp)
1145                 *lenp = len;
1146
1147         return XML_ERR_OK;
1148 }
1149
1150
1151 /*
1152  * Write an XML declaration.  On output, we adjust the XML declaration
1153  * as follows.  (These rules are the moral equivalent of the clause
1154  * "Serialization of an XML value" in the SQL standard.)
1155  *
1156  * We try to avoid generating an XML declaration if possible.  This is
1157  * so that you don't get trivial things like xml '<foo/>' resulting in
1158  * '<?xml version="1.0"?><foo/>', which would surely be annoying.  We
1159  * must provide a declaration if the standalone property is specified
1160  * or if we include an encoding declaration.  If we have a
1161  * declaration, we must specify a version (XML requires this).
1162  * Otherwise we only make a declaration if the version is not "1.0",
1163  * which is the default version specified in SQL:2003.
1164  */
1165 static bool
1166 print_xml_decl(StringInfo buf, const xmlChar * version,
1167                            pg_enc encoding, int standalone)
1168 {
1169         xml_init();
1170
1171         if ((version && strcmp((char *) version, PG_XML_DEFAULT_VERSION) != 0)
1172                 || (encoding && encoding != PG_UTF8)
1173                 || standalone != -1)
1174         {
1175                 appendStringInfoString(buf, "<?xml");
1176
1177                 if (version)
1178                         appendStringInfo(buf, " version=\"%s\"", version);
1179                 else
1180                         appendStringInfo(buf, " version=\"%s\"", PG_XML_DEFAULT_VERSION);
1181
1182                 if (encoding && encoding != PG_UTF8)
1183                 {
1184                         /*
1185                          * XXX might be useful to convert this to IANA names (ISO-8859-1
1186                          * instead of LATIN1 etc.); needs field experience
1187                          */
1188                         appendStringInfo(buf, " encoding=\"%s\"",
1189                                                          pg_encoding_to_char(encoding));
1190                 }
1191
1192                 if (standalone == 1)
1193                         appendStringInfoString(buf, " standalone=\"yes\"");
1194                 else if (standalone == 0)
1195                         appendStringInfoString(buf, " standalone=\"no\"");
1196                 appendStringInfoString(buf, "?>");
1197
1198                 return true;
1199         }
1200         else
1201                 return false;
1202 }
1203
1204
1205 /*
1206  * Convert a C string to XML internal representation
1207  *
1208  * TODO maybe, libxml2's xmlreader is better? (do not construct DOM,
1209  * yet do not use SAX - see xml_reader.c)
1210  */
1211 static xmlDocPtr
1212 xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace,
1213                   xmlChar * encoding)
1214 {
1215         int32           len;
1216         xmlChar    *string;
1217         xmlChar    *utf8string;
1218         xmlParserCtxtPtr ctxt = NULL;
1219         xmlDocPtr       doc = NULL;
1220
1221         len = VARSIZE(data) - VARHDRSZ;         /* will be useful later */
1222         string = xml_text2xmlChar(data);
1223
1224         utf8string = pg_do_encoding_conversion(string,
1225                                                                                    len,
1226                                                                                    encoding ?
1227                                                                                    xmlChar_to_encoding(encoding) :
1228                                                                                    GetDatabaseEncoding(),
1229                                                                                    PG_UTF8);
1230
1231         xml_init();
1232
1233         /* We use a PG_TRY block to ensure libxml parser is cleaned up on error */
1234         PG_TRY();
1235         {
1236                 xmlInitParser();
1237                 ctxt = xmlNewParserCtxt();
1238                 if (ctxt == NULL)
1239                         xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
1240                                                 "could not allocate parser context");
1241
1242                 if (xmloption_arg == XMLOPTION_DOCUMENT)
1243                 {
1244                         /*
1245                          * Note, that here we try to apply DTD defaults
1246                          * (XML_PARSE_DTDATTR) according to SQL/XML:10.16.7.d: 'Default
1247                          * valies defined by internal DTD are applied'. As for external
1248                          * DTDs, we try to support them too, (see SQL/XML:10.16.7.e)
1249                          */
1250                         doc = xmlCtxtReadDoc(ctxt, utf8string,
1251                                                                  NULL,
1252                                                                  "UTF-8",
1253                                                                  XML_PARSE_NOENT | XML_PARSE_DTDATTR
1254                                                    | (preserve_whitespace ? 0 : XML_PARSE_NOBLANKS));
1255                         if (doc == NULL)
1256                                 xml_ereport(ERROR, ERRCODE_INVALID_XML_DOCUMENT,
1257                                                         "invalid XML document");
1258                 }
1259                 else
1260                 {
1261                         int                     res_code;
1262                         size_t          count;
1263                         xmlChar    *version = NULL;
1264                         int                     standalone = -1;
1265
1266                         doc = xmlNewDoc(NULL);
1267
1268                         res_code = parse_xml_decl(utf8string, &count, &version, NULL, &standalone);
1269                         if (res_code != 0)
1270                                 xml_ereport_by_code(ERROR, ERRCODE_INVALID_XML_CONTENT,
1271                                    "invalid XML content: invalid XML declaration", res_code);
1272
1273                         res_code = xmlParseBalancedChunkMemory(doc, NULL, NULL, 0, utf8string + count, NULL);
1274                         if (res_code != 0)
1275                                 xml_ereport(ERROR, ERRCODE_INVALID_XML_CONTENT,
1276                                                         "invalid XML content");
1277
1278                         doc->version = xmlStrdup(version);
1279                         doc->encoding = xmlStrdup((xmlChar *) "UTF-8");
1280                         doc->standalone = standalone;
1281                 }
1282
1283                 if (ctxt)
1284                         xmlFreeParserCtxt(ctxt);
1285                 ctxt = NULL;
1286                 xmlCleanupParser();
1287         }
1288         PG_CATCH();
1289         {
1290                 if (doc)
1291                         xmlFreeDoc(doc);
1292                 if (ctxt)
1293                         xmlFreeParserCtxt(ctxt);
1294                 xmlCleanupParser();
1295
1296                 PG_RE_THROW();
1297         }
1298         PG_END_TRY();
1299
1300         return doc;
1301 }
1302
1303
1304 /*
1305  * xmlChar<->text convertions
1306  */
1307 static xmlChar *
1308 xml_text2xmlChar(text *in)
1309 {
1310         int32           len = VARSIZE(in) - VARHDRSZ;
1311         xmlChar    *res;
1312
1313         res = palloc(len + 1);
1314         memcpy(res, VARDATA(in), len);
1315         res[len] = '\0';
1316
1317         return (res);
1318 }
1319
1320
1321 /*
1322  * Wrappers for memory management functions
1323  */
1324 static void *
1325 xml_palloc(size_t size)
1326 {
1327         return palloc(size);
1328 }
1329
1330
1331 static void *
1332 xml_repalloc(void *ptr, size_t size)
1333 {
1334         return repalloc(ptr, size);
1335 }
1336
1337
1338 static void
1339 xml_pfree(void *ptr)
1340 {
1341         pfree(ptr);
1342 }
1343
1344
1345 static char *
1346 xml_pstrdup(const char *string)
1347 {
1348         return pstrdup(string);
1349 }
1350
1351
1352 /*
1353  * Wrapper for "ereport" function for XML-related errors.  The "msg"
1354  * is the SQL-level message; some can be adopted from the SQL/XML
1355  * standard.  This function adds libxml's native error messages, if
1356  * any, as detail.
1357  */
1358 static void
1359 xml_ereport(int level, int sqlcode, const char *msg)
1360 {
1361         char       *detail;
1362
1363         if (xml_err_buf->len > 0)
1364         {
1365                 detail = pstrdup(xml_err_buf->data);
1366                 resetStringInfo(xml_err_buf);
1367         }
1368         else
1369                 detail = NULL;
1370
1371         /* libxml error messages end in '\n'; get rid of it */
1372         if (detail)
1373         {
1374                 size_t          len;
1375
1376                 len = strlen(detail);
1377                 if (len > 0 && detail[len - 1] == '\n')
1378                         detail[len - 1] = '\0';
1379
1380                 ereport(level,
1381                                 (errcode(sqlcode),
1382                                  errmsg("%s", msg),
1383                                  errdetail("%s", detail)));
1384         }
1385         else
1386         {
1387                 ereport(level,
1388                                 (errcode(sqlcode),
1389                                  errmsg("%s", msg)));
1390         }
1391 }
1392
1393
1394 /*
1395  * Error handler for libxml error messages
1396  */
1397 static void
1398 xml_errorHandler(void *ctxt, const char *msg,...)
1399 {
1400         /* Append the formatted text to xml_err_buf */
1401         for (;;)
1402         {
1403                 va_list         args;
1404                 bool            success;
1405
1406                 /* Try to format the data. */
1407                 va_start(args, msg);
1408                 success = appendStringInfoVA(xml_err_buf, msg, args);
1409                 va_end(args);
1410
1411                 if (success)
1412                         break;
1413
1414                 /* Double the buffer size and try again. */
1415                 enlargeStringInfo(xml_err_buf, xml_err_buf->maxlen);
1416         }
1417 }
1418
1419
1420 /*
1421  * Wrapper for "ereport" function for XML-related errors.  The "msg"
1422  * is the SQL-level message; some can be adopted from the SQL/XML
1423  * standard.  This function uses "code" to create a textual detail
1424  * message.  At the moment, we only need to cover those codes that we
1425  * may raise in this file.
1426  */
1427 static void
1428 xml_ereport_by_code(int level, int sqlcode,
1429                                         const char *msg, int code)
1430 {
1431         const char *det;
1432
1433         switch (code)
1434         {
1435                 case XML_ERR_INVALID_CHAR:
1436                         det = "Invalid character value";
1437                         break;
1438                 case XML_ERR_SPACE_REQUIRED:
1439                         det = "Space required";
1440                         break;
1441                 case XML_ERR_STANDALONE_VALUE:
1442                         det = "standalone accepts only 'yes' or 'no'";
1443                         break;
1444                 case XML_ERR_VERSION_MISSING:
1445                         det = "Malformed declaration expecting version";
1446                         break;
1447                 case XML_ERR_MISSING_ENCODING:
1448                         det = "Missing encoding in text declaration";
1449                         break;
1450                 case XML_ERR_XMLDECL_NOT_FINISHED:
1451                         det = "Parsing XML declaration: '?>' expected";
1452                         break;
1453                 default:
1454                         det = "Unrecognized libxml error code: %d";
1455                         break;
1456         }
1457
1458         ereport(level,
1459                         (errcode(sqlcode),
1460                          errmsg("%s", msg),
1461                          errdetail(det, code)));
1462 }
1463
1464
1465 /*
1466  * Convert one char in the current server encoding to a Unicode codepoint.
1467  */
1468 static pg_wchar
1469 sqlchar_to_unicode(char *s)
1470 {
1471         char       *utf8string;
1472         pg_wchar        ret[2];                 /* need space for trailing zero */
1473
1474         utf8string = (char *) pg_do_encoding_conversion((unsigned char *) s,
1475                                                                                                         pg_mblen(s),
1476                                                                                                         GetDatabaseEncoding(),
1477                                                                                                         PG_UTF8);
1478
1479         pg_encoding_mb2wchar_with_len(PG_UTF8, utf8string, ret, pg_mblen(s));
1480
1481         return ret[0];
1482 }
1483
1484
1485 static bool
1486 is_valid_xml_namefirst(pg_wchar c)
1487 {
1488         /* (Letter | '_' | ':') */
1489         return (xmlIsBaseCharQ(c) || xmlIsIdeographicQ(c)
1490                         || c == '_' || c == ':');
1491 }
1492
1493
1494 static bool
1495 is_valid_xml_namechar(pg_wchar c)
1496 {
1497         /* Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender */
1498         return (xmlIsBaseCharQ(c) || xmlIsIdeographicQ(c)
1499                         || xmlIsDigitQ(c)
1500                         || c == '.' || c == '-' || c == '_' || c == ':'
1501                         || xmlIsCombiningQ(c)
1502                         || xmlIsExtenderQ(c));
1503 }
1504 #endif   /* USE_LIBXML */
1505
1506
1507 /*
1508  * Map SQL identifier to XML name; see SQL/XML:2003 section 9.1.
1509  */
1510 char *
1511 map_sql_identifier_to_xml_name(char *ident, bool fully_escaped,
1512                                                            bool escape_period)
1513 {
1514 #ifdef USE_LIBXML
1515         StringInfoData buf;
1516         char       *p;
1517
1518         /*
1519          * SQL/XML doesn't make use of this case anywhere, so it's probably a
1520          * mistake.
1521          */
1522         Assert(fully_escaped || !escape_period);
1523
1524         initStringInfo(&buf);
1525
1526         for (p = ident; *p; p += pg_mblen(p))
1527         {
1528                 if (*p == ':' && (p == ident || fully_escaped))
1529                         appendStringInfo(&buf, "_x003A_");
1530                 else if (*p == '_' && *(p + 1) == 'x')
1531                         appendStringInfo(&buf, "_x005F_");
1532                 else if (fully_escaped && p == ident &&
1533                                  pg_strncasecmp(p, "xml", 3) == 0)
1534                 {
1535                         if (*p == 'x')
1536                                 appendStringInfo(&buf, "_x0078_");
1537                         else
1538                                 appendStringInfo(&buf, "_x0058_");
1539                 }
1540                 else if (escape_period && *p == '.')
1541                         appendStringInfo(&buf, "_x002E_");
1542                 else
1543                 {
1544                         pg_wchar        u = sqlchar_to_unicode(p);
1545
1546                         if ((p == ident)
1547                                 ? !is_valid_xml_namefirst(u)
1548                                 : !is_valid_xml_namechar(u))
1549                                 appendStringInfo(&buf, "_x%04X_", (unsigned int) u);
1550                         else
1551                                 appendBinaryStringInfo(&buf, p, pg_mblen(p));
1552                 }
1553         }
1554
1555         return buf.data;
1556 #else                                                   /* not USE_LIBXML */
1557         NO_XML_SUPPORT();
1558         return NULL;
1559 #endif   /* not USE_LIBXML */
1560 }
1561
1562
1563 /*
1564  * Map a Unicode codepoint into the current server encoding.
1565  */
1566 static char *
1567 unicode_to_sqlchar(pg_wchar c)
1568 {
1569         static unsigned char utf8string[5]; /* need trailing zero */
1570
1571         if (c <= 0x7F)
1572         {
1573                 utf8string[0] = c;
1574         }
1575         else if (c <= 0x7FF)
1576         {
1577                 utf8string[0] = 0xC0 | ((c >> 6) & 0x1F);
1578                 utf8string[1] = 0x80 | (c & 0x3F);
1579         }
1580         else if (c <= 0xFFFF)
1581         {
1582                 utf8string[0] = 0xE0 | ((c >> 12) & 0x0F);
1583                 utf8string[1] = 0x80 | ((c >> 6) & 0x3F);
1584                 utf8string[2] = 0x80 | (c & 0x3F);
1585         }
1586         else
1587         {
1588                 utf8string[0] = 0xF0 | ((c >> 18) & 0x07);
1589                 utf8string[1] = 0x80 | ((c >> 12) & 0x3F);
1590                 utf8string[2] = 0x80 | ((c >> 6) & 0x3F);
1591                 utf8string[3] = 0x80 | (c & 0x3F);
1592         }
1593
1594         return (char *) pg_do_encoding_conversion(utf8string,
1595                                                                                           pg_mblen((char *) utf8string),
1596                                                                                           PG_UTF8,
1597                                                                                           GetDatabaseEncoding());
1598 }
1599
1600
1601 /*
1602  * Map XML name to SQL identifier; see SQL/XML:2003 section 9.17.
1603  */
1604 char *
1605 map_xml_name_to_sql_identifier(char *name)
1606 {
1607         StringInfoData buf;
1608         char       *p;
1609
1610         initStringInfo(&buf);
1611
1612         for (p = name; *p; p += pg_mblen(p))
1613         {
1614                 if (*p == '_' && *(p + 1) == 'x'
1615                         && isxdigit((unsigned char) *(p + 2))
1616                         && isxdigit((unsigned char) *(p + 3))
1617                         && isxdigit((unsigned char) *(p + 4))
1618                         && isxdigit((unsigned char) *(p + 5))
1619                         && *(p + 6) == '_')
1620                 {
1621                         unsigned int u;
1622
1623                         sscanf(p + 2, "%X", &u);
1624                         appendStringInfoString(&buf, unicode_to_sqlchar(u));
1625                         p += 6;
1626                 }
1627                 else
1628                         appendBinaryStringInfo(&buf, p, pg_mblen(p));
1629         }
1630
1631         return buf.data;
1632 }
1633
1634 /*
1635  * Map SQL value to XML value; see SQL/XML:2003 section 9.16.
1636  */
1637 char *
1638 map_sql_value_to_xml_value(Datum value, Oid type)
1639 {
1640         StringInfoData buf;
1641
1642         initStringInfo(&buf);
1643
1644         if (type_is_array(type))
1645         {
1646                 ArrayType  *array;
1647                 Oid                     elmtype;
1648                 int16           elmlen;
1649                 bool            elmbyval;
1650                 char            elmalign;
1651                 int                     num_elems;
1652                 Datum      *elem_values;
1653                 bool       *elem_nulls;
1654                 int                     i;
1655
1656                 array = DatumGetArrayTypeP(value);
1657                 elmtype = ARR_ELEMTYPE(array);
1658                 get_typlenbyvalalign(elmtype, &elmlen, &elmbyval, &elmalign);
1659
1660                 deconstruct_array(array, elmtype,
1661                                                   elmlen, elmbyval, elmalign,
1662                                                   &elem_values, &elem_nulls,
1663                                                   &num_elems);
1664
1665                 for (i = 0; i < num_elems; i++)
1666                 {
1667                         if (elem_nulls[i])
1668                                 continue;
1669                         appendStringInfoString(&buf, "<element>");
1670                         appendStringInfoString(&buf,
1671                                                                    map_sql_value_to_xml_value(elem_values[i],
1672                                                                                                                           elmtype));
1673                         appendStringInfoString(&buf, "</element>");
1674                 }
1675
1676                 pfree(elem_values);
1677                 pfree(elem_nulls);
1678         }
1679         else
1680         {
1681                 Oid                     typeOut;
1682                 bool            isvarlena;
1683                 char       *p,
1684                                    *str;
1685
1686                 /*
1687                  * Special XSD formatting for some data types
1688                  */
1689                 switch (type)
1690                 {
1691                         case BOOLOID:
1692                                 if (DatumGetBool(value))
1693                                         return "true";
1694                                 else
1695                                         return "false";
1696
1697                         case DATEOID:
1698                                 {
1699                                         DateADT         date;
1700                                         struct pg_tm tm;
1701                                         char            buf[MAXDATELEN + 1];
1702
1703                                         date = DatumGetDateADT(value);
1704                                         j2date(date + POSTGRES_EPOCH_JDATE,
1705                                                    &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
1706                                         EncodeDateOnly(&tm, USE_XSD_DATES, buf);
1707
1708                                         return pstrdup(buf);
1709                                 }
1710
1711                         case TIMESTAMPOID:
1712                                 {
1713                                         Timestamp       timestamp;
1714                                         struct pg_tm tm;
1715                                         fsec_t          fsec;
1716                                         char       *tzn = NULL;
1717                                         char            buf[MAXDATELEN + 1];
1718
1719                                         timestamp = DatumGetTimestamp(value);
1720
1721                                         /* XSD doesn't support infinite values */
1722                                         if (TIMESTAMP_NOT_FINITE(timestamp))
1723                                                 ereport(ERROR,
1724                                                                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1725                                                                  errmsg("timestamp out of range")));
1726                                         else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
1727                                                 EncodeDateTime(&tm, fsec, NULL, &tzn, USE_XSD_DATES, buf);
1728                                         else
1729                                                 ereport(ERROR,
1730                                                                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1731                                                                  errmsg("timestamp out of range")));
1732
1733                                         return pstrdup(buf);
1734                                 }
1735
1736                         case TIMESTAMPTZOID:
1737                                 {
1738                                         TimestampTz timestamp;
1739                                         struct pg_tm tm;
1740                                         int                     tz;
1741                                         fsec_t          fsec;
1742                                         char       *tzn = NULL;
1743                                         char            buf[MAXDATELEN + 1];
1744
1745                                         timestamp = DatumGetTimestamp(value);
1746
1747                                         /* XSD doesn't support infinite values */
1748                                         if (TIMESTAMP_NOT_FINITE(timestamp))
1749                                                 ereport(ERROR,
1750                                                                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1751                                                                  errmsg("timestamp out of range")));
1752                                         else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
1753                                                 EncodeDateTime(&tm, fsec, &tz, &tzn, USE_XSD_DATES, buf);
1754                                         else
1755                                                 ereport(ERROR,
1756                                                                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1757                                                                  errmsg("timestamp out of range")));
1758
1759                                         return pstrdup(buf);
1760                                 }
1761                 }
1762
1763                 getTypeOutputInfo(type, &typeOut, &isvarlena);
1764                 str = OidOutputFunctionCall(typeOut, value);
1765
1766                 if (type == XMLOID)
1767                         return str;
1768
1769 #ifdef USE_LIBXML
1770                 if (type == BYTEAOID)
1771                 {
1772                         xmlBufferPtr buf;
1773                         xmlTextWriterPtr writer;
1774                         char       *result;
1775
1776                         xml_init();
1777
1778                         buf = xmlBufferCreate();
1779                         writer = xmlNewTextWriterMemory(buf, 0);
1780
1781                         if (xmlbinary == XMLBINARY_BASE64)
1782                                 xmlTextWriterWriteBase64(writer, VARDATA(value), 0, VARSIZE(value) - VARHDRSZ);
1783                         else
1784                                 xmlTextWriterWriteBinHex(writer, VARDATA(value), 0, VARSIZE(value) - VARHDRSZ);
1785
1786                         xmlFreeTextWriter(writer);
1787                         result = pstrdup((const char *) xmlBufferContent(buf));
1788                         xmlBufferFree(buf);
1789                         return result;
1790                 }
1791 #endif   /* USE_LIBXML */
1792
1793                 for (p = str; *p; p += pg_mblen(p))
1794                 {
1795                         switch (*p)
1796                         {
1797                                 case '&':
1798                                         appendStringInfo(&buf, "&amp;");
1799                                         break;
1800                                 case '<':
1801                                         appendStringInfo(&buf, "&lt;");
1802                                         break;
1803                                 case '>':
1804                                         appendStringInfo(&buf, "&gt;");
1805                                         break;
1806                                 case '\r':
1807                                         appendStringInfo(&buf, "&#x0d;");
1808                                         break;
1809                                 default:
1810                                         appendBinaryStringInfo(&buf, p, pg_mblen(p));
1811                                         break;
1812                         }
1813                 }
1814         }
1815
1816         return buf.data;
1817 }
1818
1819
1820 static char *
1821 _SPI_strdup(const char *s)
1822 {
1823         char       *ret = SPI_palloc(strlen(s) + 1);
1824
1825         strcpy(ret, s);
1826         return ret;
1827 }
1828
1829
1830 /*
1831  * SQL to XML mapping functions
1832  *
1833  * What follows below is intentionally organized so that you can read
1834  * along in the SQL/XML:2003 standard.  The functions are mostly split
1835  * up and ordered they way the clauses lay out in the standards
1836  * document, and the identifiers are also aligned with the standard
1837  * text.  (SQL/XML:2006 appears to be ordered differently,
1838  * unfortunately.)
1839  *
1840  * There are many things going on there:
1841  *
1842  * There are two kinds of mappings: Mapping SQL data (table contents)
1843  * to XML documents, and mapping SQL structure (the "schema") to XML
1844  * Schema.      And there are functions that do both at the same time.
1845  *
1846  * Then you can map a database, a schema, or a table, each in both
1847  * ways.  This breaks down recursively: Mapping a database invokes
1848  * mapping schemas, which invokes mapping tables, which invokes
1849  * mapping rows, which invokes mapping columns, although you can't
1850  * call the last two from the outside.  Because of this, there are a
1851  * number of xyz_internal() functions which are to be called both from
1852  * the function manager wrapper and from some upper layer in a
1853  * recursive call.
1854  *
1855  * See the documentation about what the common function arguments
1856  * nulls, tableforest, and targetns mean.
1857  *
1858  * Some style guidelines for XML output: Use double quotes for quoting
1859  * XML attributes.      Indent XML elements by two spaces, but remember
1860  * that a lot of code is called recursively at different levels, so
1861  * it's better not to indent rather than create output that indents
1862  * and outdents weirdly.  Add newlines to make the output look nice.
1863  */
1864
1865
1866 /*
1867  * Visibility of objects for XML mappings; see SQL/XML:2003 section
1868  * 4.8.5.
1869  */
1870
1871 /*
1872  * Given a query, which must return type oid as first column, produce
1873  * a list of Oids with the query results.
1874  */
1875 static List *
1876 query_to_oid_list(const char *query)
1877 {
1878         int                     i;
1879         List       *list = NIL;
1880
1881         SPI_execute(query, true, 0);
1882
1883         for (i = 0; i < SPI_processed; i++)
1884         {
1885                 Datum           oid;
1886                 bool            isnull;
1887
1888                 oid = SPI_getbinval(SPI_tuptable->vals[i],
1889                                                         SPI_tuptable->tupdesc,
1890                                                         1,
1891                                                         &isnull);
1892                 if (!isnull)
1893                         list = lappend_oid(list, DatumGetObjectId(oid));
1894         }
1895
1896         return list;
1897 }
1898
1899
1900 static List *
1901 schema_get_xml_visible_tables(Oid nspid)
1902 {
1903         StringInfoData query;
1904
1905         initStringInfo(&query);
1906         appendStringInfo(&query, "SELECT oid FROM pg_catalog.pg_class WHERE relnamespace = %u AND relkind IN ('r', 'v') AND pg_catalog.has_table_privilege (oid, 'SELECT') ORDER BY relname;", nspid);
1907
1908         return query_to_oid_list(query.data);
1909 }
1910
1911
1912 /*
1913  * Including the system schemas is probably not useful for a database
1914  * mapping.
1915  */
1916 #define XML_VISIBLE_SCHEMAS_EXCLUDE "(nspname ~ '^pg_' OR nspname = 'information_schema')"
1917
1918 #define XML_VISIBLE_SCHEMAS "SELECT oid FROM pg_catalog.pg_namespace WHERE pg_catalog.has_schema_privilege (oid, 'USAGE') AND NOT " XML_VISIBLE_SCHEMAS_EXCLUDE
1919
1920
1921 static List *
1922 database_get_xml_visible_schemas(void)
1923 {
1924         return query_to_oid_list(XML_VISIBLE_SCHEMAS " ORDER BY nspname;");
1925 }
1926
1927
1928 static List *
1929 database_get_xml_visible_tables(void)
1930 {
1931         /* At the moment there is no order required here. */
1932         return query_to_oid_list("SELECT oid FROM pg_catalog.pg_class WHERE relkind IN ('r', 'v') AND pg_catalog.has_table_privilege (pg_class.oid, 'SELECT') AND relnamespace IN (" XML_VISIBLE_SCHEMAS ");");
1933 }
1934
1935
1936 /*
1937  * Map SQL table to XML and/or XML Schema document; see SQL/XML:2003
1938  * section 9.3.
1939  */
1940
1941 static StringInfo
1942 table_to_xml_internal(Oid relid,
1943                                           const char *xmlschema, bool nulls, bool tableforest,
1944                                           const char *targetns, bool top_level)
1945 {
1946         StringInfoData query;
1947
1948         initStringInfo(&query);
1949         appendStringInfo(&query, "SELECT * FROM %s",
1950                                          DatumGetCString(DirectFunctionCall1(regclassout,
1951                                                                                                   ObjectIdGetDatum(relid))));
1952         return query_to_xml_internal(query.data, get_rel_name(relid),
1953                                                                  xmlschema, nulls, tableforest,
1954                                                                  targetns, top_level);
1955 }
1956
1957
1958 Datum
1959 table_to_xml(PG_FUNCTION_ARGS)
1960 {
1961         Oid                     relid = PG_GETARG_OID(0);
1962         bool            nulls = PG_GETARG_BOOL(1);
1963         bool            tableforest = PG_GETARG_BOOL(2);
1964         const char *targetns = _textout(PG_GETARG_TEXT_P(3));
1965
1966         PG_RETURN_XML_P(stringinfo_to_xmltype(table_to_xml_internal(relid, NULL,
1967                                                                                                                   nulls, tableforest,
1968                                                                                                                    targetns, true)));
1969 }
1970
1971
1972 Datum
1973 query_to_xml(PG_FUNCTION_ARGS)
1974 {
1975         char       *query = _textout(PG_GETARG_TEXT_P(0));
1976         bool            nulls = PG_GETARG_BOOL(1);
1977         bool            tableforest = PG_GETARG_BOOL(2);
1978         const char *targetns = _textout(PG_GETARG_TEXT_P(3));
1979
1980         PG_RETURN_XML_P(stringinfo_to_xmltype(query_to_xml_internal(query, NULL,
1981                                                                                                         NULL, nulls, tableforest,
1982                                                                                                                    targetns, true)));
1983 }
1984
1985
1986 Datum
1987 cursor_to_xml(PG_FUNCTION_ARGS)
1988 {
1989         char       *name = _textout(PG_GETARG_TEXT_P(0));
1990         int32           count = PG_GETARG_INT32(1);
1991         bool            nulls = PG_GETARG_BOOL(2);
1992         bool            tableforest = PG_GETARG_BOOL(3);
1993         const char *targetns = _textout(PG_GETARG_TEXT_P(4));
1994
1995         StringInfoData result;
1996         Portal          portal;
1997         int                     i;
1998
1999         initStringInfo(&result);
2000
2001         SPI_connect();
2002         portal = SPI_cursor_find(name);
2003         if (portal == NULL)
2004                 ereport(ERROR,
2005                                 (errcode(ERRCODE_UNDEFINED_CURSOR),
2006                                  errmsg("cursor \"%s\" does not exist", name)));
2007
2008         SPI_cursor_fetch(portal, true, count);
2009         for (i = 0; i < SPI_processed; i++)
2010                 SPI_sql_row_to_xmlelement(i, &result, NULL, nulls,
2011                                                                   tableforest, targetns, true);
2012
2013         SPI_finish();
2014
2015         PG_RETURN_XML_P(stringinfo_to_xmltype(&result));
2016 }
2017
2018
2019 /*
2020  * Write the start tag of the root element of a data mapping.
2021  *
2022  * top_level means that this is the very top level of the eventual
2023  * output.      For example, when the user calls table_to_xml, then a call
2024  * with a table name to this function is the top level.  When the user
2025  * calls database_to_xml, then a call with a schema name to this
2026  * function is not the top level.  If top_level is false, then the XML
2027  * namespace declarations are omitted, because they supposedly already
2028  * appeared earlier in the output.      Repeating them is not wrong, but
2029  * it looks ugly.
2030  */
2031 static void
2032 xmldata_root_element_start(StringInfo result, const char *eltname,
2033                                                    const char *xmlschema, const char *targetns,
2034                                                    bool top_level)
2035 {
2036         /* This isn't really wrong but currently makes no sense. */
2037         Assert(top_level || !xmlschema);
2038
2039         appendStringInfo(result, "<%s", eltname);
2040         if (top_level)
2041         {
2042                 appendStringInfoString(result, " xmlns:xsi=\"" NAMESPACE_XSI "\"");
2043                 if (strlen(targetns) > 0)
2044                         appendStringInfo(result, " xmlns=\"%s\"", targetns);
2045         }
2046         if (xmlschema)
2047         {
2048                 /* FIXME: better targets */
2049                 if (strlen(targetns) > 0)
2050                         appendStringInfo(result, " xsi:schemaLocation=\"%s #\"", targetns);
2051                 else
2052                         appendStringInfo(result, " xsi:noNamespaceSchemaLocation=\"#\"");
2053         }
2054         appendStringInfo(result, ">\n\n");
2055 }
2056
2057
2058 static void
2059 xmldata_root_element_end(StringInfo result, const char *eltname)
2060 {
2061         appendStringInfo(result, "</%s>\n", eltname);
2062 }
2063
2064
2065 static StringInfo
2066 query_to_xml_internal(const char *query, char *tablename,
2067                                           const char *xmlschema, bool nulls, bool tableforest,
2068                                           const char *targetns, bool top_level)
2069 {
2070         StringInfo      result;
2071         char       *xmltn;
2072         int                     i;
2073
2074         if (tablename)
2075                 xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
2076         else
2077                 xmltn = "table";
2078
2079         result = makeStringInfo();
2080
2081         SPI_connect();
2082         if (SPI_execute(query, true, 0) != SPI_OK_SELECT)
2083                 ereport(ERROR,
2084                                 (errcode(ERRCODE_DATA_EXCEPTION),
2085                                  errmsg("invalid query")));
2086
2087         if (!tableforest)
2088                 xmldata_root_element_start(result, xmltn, xmlschema,
2089                                                                    targetns, top_level);
2090
2091         if (xmlschema)
2092                 appendStringInfo(result, "%s\n\n", xmlschema);
2093
2094         for (i = 0; i < SPI_processed; i++)
2095                 SPI_sql_row_to_xmlelement(i, result, tablename, nulls,
2096                                                                   tableforest, targetns, top_level);
2097
2098         if (!tableforest)
2099                 xmldata_root_element_end(result, xmltn);
2100
2101         SPI_finish();
2102
2103         return result;
2104 }
2105
2106
2107 Datum
2108 table_to_xmlschema(PG_FUNCTION_ARGS)
2109 {
2110         Oid                     relid = PG_GETARG_OID(0);
2111         bool            nulls = PG_GETARG_BOOL(1);
2112         bool            tableforest = PG_GETARG_BOOL(2);
2113         const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2114         const char *result;
2115         Relation        rel;
2116
2117         rel = heap_open(relid, AccessShareLock);
2118         result = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2119                                                                                 tableforest, targetns);
2120         heap_close(rel, NoLock);
2121
2122         PG_RETURN_XML_P(cstring_to_xmltype(result));
2123 }
2124
2125
2126 Datum
2127 query_to_xmlschema(PG_FUNCTION_ARGS)
2128 {
2129         char       *query = _textout(PG_GETARG_TEXT_P(0));
2130         bool            nulls = PG_GETARG_BOOL(1);
2131         bool            tableforest = PG_GETARG_BOOL(2);
2132         const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2133         const char *result;
2134         SPIPlanPtr      plan;
2135         Portal          portal;
2136
2137         SPI_connect();
2138         plan = SPI_prepare(query, 0, NULL);
2139         portal = SPI_cursor_open(NULL, plan, NULL, NULL, true);
2140         result = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2141                                                                                                         InvalidOid, nulls,
2142                                                                                                         tableforest, targetns));
2143         SPI_cursor_close(portal);
2144         SPI_finish();
2145
2146         PG_RETURN_XML_P(cstring_to_xmltype(result));
2147 }
2148
2149
2150 Datum
2151 cursor_to_xmlschema(PG_FUNCTION_ARGS)
2152 {
2153         char       *name = _textout(PG_GETARG_TEXT_P(0));
2154         bool            nulls = PG_GETARG_BOOL(1);
2155         bool            tableforest = PG_GETARG_BOOL(2);
2156         const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2157         const char *xmlschema;
2158         Portal          portal;
2159
2160         SPI_connect();
2161         portal = SPI_cursor_find(name);
2162         if (portal == NULL)
2163                 ereport(ERROR,
2164                                 (errcode(ERRCODE_UNDEFINED_CURSOR),
2165                                  errmsg("cursor \"%s\" does not exist", name)));
2166
2167         xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2168                                                                                                            InvalidOid, nulls,
2169                                                                                                          tableforest, targetns));
2170         SPI_finish();
2171
2172         PG_RETURN_XML_P(cstring_to_xmltype(xmlschema));
2173 }
2174
2175
2176 Datum
2177 table_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2178 {
2179         Oid                     relid = PG_GETARG_OID(0);
2180         bool            nulls = PG_GETARG_BOOL(1);
2181         bool            tableforest = PG_GETARG_BOOL(2);
2182         const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2183         Relation        rel;
2184         const char *xmlschema;
2185
2186         rel = heap_open(relid, AccessShareLock);
2187         xmlschema = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2188                                                                                    tableforest, targetns);
2189         heap_close(rel, NoLock);
2190
2191         PG_RETURN_XML_P(stringinfo_to_xmltype(table_to_xml_internal(relid,
2192                                                                                            xmlschema, nulls, tableforest,
2193                                                                                                                    targetns, true)));
2194 }
2195
2196
2197 Datum
2198 query_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2199 {
2200         char       *query = _textout(PG_GETARG_TEXT_P(0));
2201         bool            nulls = PG_GETARG_BOOL(1);
2202         bool            tableforest = PG_GETARG_BOOL(2);
2203         const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2204
2205         const char *xmlschema;
2206         SPIPlanPtr      plan;
2207         Portal          portal;
2208
2209         SPI_connect();
2210         plan = SPI_prepare(query, 0, NULL);
2211         portal = SPI_cursor_open(NULL, plan, NULL, NULL, true);
2212         xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2213                                                                   InvalidOid, nulls, tableforest, targetns));
2214         SPI_cursor_close(portal);
2215         SPI_finish();
2216
2217         PG_RETURN_XML_P(stringinfo_to_xmltype(query_to_xml_internal(query, NULL,
2218                                                                                            xmlschema, nulls, tableforest,
2219                                                                                                                    targetns, true)));
2220 }
2221
2222
2223 /*
2224  * Map SQL schema to XML and/or XML Schema document; see SQL/XML:2003
2225  * section 9.4.
2226  */
2227
2228 static StringInfo
2229 schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls,
2230                                            bool tableforest, const char *targetns, bool top_level)
2231 {
2232         StringInfo      result;
2233         char       *xmlsn;
2234         List       *relid_list;
2235         ListCell   *cell;
2236
2237         xmlsn = map_sql_identifier_to_xml_name(get_namespace_name(nspid),
2238                                                                                    true, false);
2239         result = makeStringInfo();
2240
2241         xmldata_root_element_start(result, xmlsn, xmlschema, targetns, top_level);
2242
2243         if (xmlschema)
2244                 appendStringInfo(result, "%s\n\n", xmlschema);
2245
2246         SPI_connect();
2247
2248         relid_list = schema_get_xml_visible_tables(nspid);
2249
2250         SPI_push();
2251
2252         foreach(cell, relid_list)
2253         {
2254                 Oid                     relid = lfirst_oid(cell);
2255                 StringInfo      subres;
2256
2257                 subres = table_to_xml_internal(relid, NULL, nulls, tableforest,
2258                                                                            targetns, false);
2259
2260                 appendStringInfoString(result, subres->data);
2261                 appendStringInfoChar(result, '\n');
2262         }
2263
2264         SPI_pop();
2265         SPI_finish();
2266
2267         xmldata_root_element_end(result, xmlsn);
2268
2269         return result;
2270 }
2271
2272
2273 Datum
2274 schema_to_xml(PG_FUNCTION_ARGS)
2275 {
2276         Name            name = PG_GETARG_NAME(0);
2277         bool            nulls = PG_GETARG_BOOL(1);
2278         bool            tableforest = PG_GETARG_BOOL(2);
2279         const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2280
2281         char       *schemaname;
2282         Oid                     nspid;
2283
2284         schemaname = NameStr(*name);
2285         nspid = LookupExplicitNamespace(schemaname);
2286
2287         PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xml_internal(nspid, NULL,
2288                                                                            nulls, tableforest, targetns, true)));
2289 }
2290
2291
2292 /*
2293  * Write the start element of the root element of an XML Schema mapping.
2294  */
2295 static void
2296 xsd_schema_element_start(StringInfo result, const char *targetns)
2297 {
2298         appendStringInfoString(result,
2299                                                    "<xsd:schema\n"
2300                                                    "    xmlns:xsd=\"" NAMESPACE_XSD "\"");
2301         if (strlen(targetns) > 0)
2302                 appendStringInfo(result,
2303                                                  "\n"
2304                                                  "    targetNamespace=\"%s\"\n"
2305                                                  "    elementFormDefault=\"qualified\"",
2306                                                  targetns);
2307         appendStringInfoString(result,
2308                                                    ">\n\n");
2309 }
2310
2311
2312 static void
2313 xsd_schema_element_end(StringInfo result)
2314 {
2315         appendStringInfoString(result, "</xsd:schema>");
2316 }
2317
2318
2319 static StringInfo
2320 schema_to_xmlschema_internal(const char *schemaname, bool nulls,
2321                                                          bool tableforest, const char *targetns)
2322 {
2323         Oid                     nspid;
2324         List       *relid_list;
2325         List       *tupdesc_list;
2326         ListCell   *cell;
2327         StringInfo      result;
2328
2329         result = makeStringInfo();
2330
2331         nspid = LookupExplicitNamespace(schemaname);
2332
2333         xsd_schema_element_start(result, targetns);
2334
2335         SPI_connect();
2336
2337         relid_list = schema_get_xml_visible_tables(nspid);
2338
2339         tupdesc_list = NIL;
2340         foreach(cell, relid_list)
2341         {
2342                 Relation        rel;
2343
2344                 rel = heap_open(lfirst_oid(cell), AccessShareLock);
2345                 tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2346                 heap_close(rel, NoLock);
2347         }
2348
2349         appendStringInfoString(result,
2350                                                    map_sql_typecoll_to_xmlschema_types(tupdesc_list));
2351
2352         appendStringInfoString(result,
2353                                                  map_sql_schema_to_xmlschema_types(nspid, relid_list,
2354                                                                                           nulls, tableforest, targetns));
2355
2356         xsd_schema_element_end(result);
2357
2358         SPI_finish();
2359
2360         return result;
2361 }
2362
2363
2364 Datum
2365 schema_to_xmlschema(PG_FUNCTION_ARGS)
2366 {
2367         Name            name = PG_GETARG_NAME(0);
2368         bool            nulls = PG_GETARG_BOOL(1);
2369         bool            tableforest = PG_GETARG_BOOL(2);
2370         const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2371
2372         PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xmlschema_internal(NameStr(*name),
2373                                                                                          nulls, tableforest, targetns)));
2374 }
2375
2376
2377 Datum
2378 schema_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2379 {
2380         Name            name = PG_GETARG_NAME(0);
2381         bool            nulls = PG_GETARG_BOOL(1);
2382         bool            tableforest = PG_GETARG_BOOL(2);
2383         const char *targetns = _textout(PG_GETARG_TEXT_P(3));
2384         char       *schemaname;
2385         Oid                     nspid;
2386         StringInfo      xmlschema;
2387
2388         schemaname = NameStr(*name);
2389         nspid = LookupExplicitNamespace(schemaname);
2390
2391         xmlschema = schema_to_xmlschema_internal(schemaname, nulls,
2392                                                                                          tableforest, targetns);
2393
2394         PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xml_internal(nspid,
2395                                                                                                           xmlschema->data, nulls,
2396                                                                                           tableforest, targetns, true)));
2397 }
2398
2399
2400 /*
2401  * Map SQL database to XML and/or XML Schema document; see SQL/XML:2003
2402  * section 9.5.
2403  */
2404
2405 static StringInfo
2406 database_to_xml_internal(const char *xmlschema, bool nulls,
2407                                                  bool tableforest, const char *targetns)
2408 {
2409         StringInfo      result;
2410         List       *nspid_list;
2411         ListCell   *cell;
2412         char       *xmlcn;
2413
2414         xmlcn = map_sql_identifier_to_xml_name(get_database_name(MyDatabaseId),
2415                                                                                    true, false);
2416         result = makeStringInfo();
2417
2418         xmldata_root_element_start(result, xmlcn, xmlschema, targetns, true);
2419
2420         if (xmlschema)
2421                 appendStringInfo(result, "%s\n\n", xmlschema);
2422
2423         SPI_connect();
2424
2425         nspid_list = database_get_xml_visible_schemas();
2426
2427         SPI_push();
2428
2429         foreach(cell, nspid_list)
2430         {
2431                 Oid                     nspid = lfirst_oid(cell);
2432                 StringInfo      subres;
2433
2434                 subres = schema_to_xml_internal(nspid, NULL, nulls,
2435                                                                                 tableforest, targetns, false);
2436
2437                 appendStringInfoString(result, subres->data);
2438                 appendStringInfoChar(result, '\n');
2439         }
2440
2441         SPI_pop();
2442         SPI_finish();
2443
2444         xmldata_root_element_end(result, xmlcn);
2445
2446         return result;
2447 }
2448
2449
2450 Datum
2451 database_to_xml(PG_FUNCTION_ARGS)
2452 {
2453         bool            nulls = PG_GETARG_BOOL(0);
2454         bool            tableforest = PG_GETARG_BOOL(1);
2455         const char *targetns = _textout(PG_GETARG_TEXT_P(2));
2456
2457         PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xml_internal(NULL, nulls,
2458                                                                                                         tableforest, targetns)));
2459 }
2460
2461
2462 static StringInfo
2463 database_to_xmlschema_internal(bool nulls, bool tableforest,
2464                                                            const char *targetns)
2465 {
2466         List       *relid_list;
2467         List       *nspid_list;
2468         List       *tupdesc_list;
2469         ListCell   *cell;
2470         StringInfo      result;
2471
2472         result = makeStringInfo();
2473
2474         xsd_schema_element_start(result, targetns);
2475
2476         SPI_connect();
2477
2478         relid_list = database_get_xml_visible_tables();
2479         nspid_list = database_get_xml_visible_schemas();
2480
2481         tupdesc_list = NIL;
2482         foreach(cell, relid_list)
2483         {
2484                 Relation        rel;
2485
2486                 rel = heap_open(lfirst_oid(cell), AccessShareLock);
2487                 tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2488                 heap_close(rel, NoLock);
2489         }
2490
2491         appendStringInfoString(result,
2492                                                    map_sql_typecoll_to_xmlschema_types(tupdesc_list));
2493
2494         appendStringInfoString(result,
2495                                                    map_sql_catalog_to_xmlschema_types(nspid_list, nulls, tableforest, targetns));
2496
2497         xsd_schema_element_end(result);
2498
2499         SPI_finish();
2500
2501         return result;
2502 }
2503
2504
2505 Datum
2506 database_to_xmlschema(PG_FUNCTION_ARGS)
2507 {
2508         bool            nulls = PG_GETARG_BOOL(0);
2509         bool            tableforest = PG_GETARG_BOOL(1);
2510         const char *targetns = _textout(PG_GETARG_TEXT_P(2));
2511
2512         PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xmlschema_internal(nulls,
2513                                                                                                         tableforest, targetns)));
2514 }
2515
2516
2517 Datum
2518 database_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
2519 {
2520         bool            nulls = PG_GETARG_BOOL(0);
2521         bool            tableforest = PG_GETARG_BOOL(1);
2522         const char *targetns = _textout(PG_GETARG_TEXT_P(2));
2523         StringInfo      xmlschema;
2524
2525         xmlschema = database_to_xmlschema_internal(nulls, tableforest, targetns);
2526
2527         PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xml_internal(xmlschema->data,
2528                                                                                          nulls, tableforest, targetns)));
2529 }
2530
2531
2532 /*
2533  * Map a multi-part SQL name to an XML name; see SQL/XML:2003 section
2534  * 9.2.
2535  */
2536 static char *
2537 map_multipart_sql_identifier_to_xml_name(char *a, char *b, char *c, char *d)
2538 {
2539         StringInfoData result;
2540
2541         initStringInfo(&result);
2542
2543         if (a)
2544                 appendStringInfo(&result, "%s",
2545                                                  map_sql_identifier_to_xml_name(a, true, true));
2546         if (b)
2547                 appendStringInfo(&result, ".%s",
2548                                                  map_sql_identifier_to_xml_name(b, true, true));
2549         if (c)
2550                 appendStringInfo(&result, ".%s",
2551                                                  map_sql_identifier_to_xml_name(c, true, true));
2552         if (d)
2553                 appendStringInfo(&result, ".%s",
2554                                                  map_sql_identifier_to_xml_name(d, true, true));
2555
2556         return result.data;
2557 }
2558
2559
2560 /*
2561  * Map an SQL table to an XML Schema document; see SQL/XML:2003
2562  * section 9.3.
2563  *
2564  * Map an SQL table to XML Schema data types; see SQL/XML:2003 section
2565  * 9.6.
2566  */
2567 static const char *
2568 map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls,
2569                                                    bool tableforest, const char *targetns)
2570 {
2571         int                     i;
2572         char       *xmltn;
2573         char       *tabletypename;
2574         char       *rowtypename;
2575         StringInfoData result;
2576
2577         initStringInfo(&result);
2578
2579         if (OidIsValid(relid))
2580         {
2581                 HeapTuple       tuple;
2582                 Form_pg_class reltuple;
2583
2584                 tuple = SearchSysCache(RELOID,
2585                                                            ObjectIdGetDatum(relid),
2586                                                            0, 0, 0);
2587                 if (!HeapTupleIsValid(tuple))
2588                         elog(ERROR, "cache lookup failed for relation %u", relid);
2589                 reltuple = (Form_pg_class) GETSTRUCT(tuple);
2590
2591                 xmltn = map_sql_identifier_to_xml_name(NameStr(reltuple->relname),
2592                                                                                            true, false);
2593
2594                 tabletypename = map_multipart_sql_identifier_to_xml_name("TableType",
2595                                                                                          get_database_name(MyDatabaseId),
2596                                                                   get_namespace_name(reltuple->relnamespace),
2597                                                                                                  NameStr(reltuple->relname));
2598
2599                 rowtypename = map_multipart_sql_identifier_to_xml_name("RowType",
2600                                                                                          get_database_name(MyDatabaseId),
2601                                                                   get_namespace_name(reltuple->relnamespace),
2602                                                                                                  NameStr(reltuple->relname));
2603
2604                 ReleaseSysCache(tuple);
2605         }
2606         else
2607         {
2608                 if (tableforest)
2609                         xmltn = "row";
2610                 else
2611                         xmltn = "table";
2612
2613                 tabletypename = "TableType";
2614                 rowtypename = "RowType";
2615         }
2616
2617         xsd_schema_element_start(&result, targetns);
2618
2619         appendStringInfoString(&result,
2620                                    map_sql_typecoll_to_xmlschema_types(list_make1(tupdesc)));
2621
2622         appendStringInfo(&result,
2623                                          "<xsd:complexType name=\"%s\">\n"
2624                                          "  <xsd:sequence>\n",
2625                                          rowtypename);
2626
2627         for (i = 0; i < tupdesc->natts; i++)
2628                 appendStringInfo(&result,
2629                            "    <xsd:element name=\"%s\" type=\"%s\"%s></xsd:element>\n",
2630                   map_sql_identifier_to_xml_name(NameStr(tupdesc->attrs[i]->attname),
2631                                                                                  true, false),
2632                                    map_sql_type_to_xml_name(tupdesc->attrs[i]->atttypid, -1),
2633                                                  nulls ? " nillable=\"true\"" : " minOccurs=\"0\"");
2634
2635         appendStringInfoString(&result,
2636                                                    "  </xsd:sequence>\n"
2637                                                    "</xsd:complexType>\n\n");
2638
2639         if (!tableforest)
2640         {
2641                 appendStringInfo(&result,
2642                                                  "<xsd:complexType name=\"%s\">\n"
2643                                                  "  <xsd:sequence>\n"
2644                                                  "    <xsd:element name=\"row\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n"
2645                                                  "  </xsd:sequence>\n"
2646                                                  "</xsd:complexType>\n\n",
2647                                                  tabletypename, rowtypename);
2648
2649                 appendStringInfo(&result,
2650                                                  "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2651                                                  xmltn, tabletypename);
2652         }
2653         else
2654                 appendStringInfo(&result,
2655                                                  "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2656                                                  xmltn, rowtypename);
2657
2658         xsd_schema_element_end(&result);
2659
2660         return result.data;
2661 }
2662
2663
2664 /*
2665  * Map an SQL schema to XML Schema data types; see SQL/XML section
2666  * 9.7.
2667  */
2668 static const char *
2669 map_sql_schema_to_xmlschema_types(Oid nspid, List *relid_list, bool nulls,
2670                                                                   bool tableforest, const char *targetns)
2671 {
2672         char       *dbname;
2673         char       *nspname;
2674         char       *xmlsn;
2675         char       *schematypename;
2676         StringInfoData result;
2677         ListCell   *cell;
2678
2679         dbname = get_database_name(MyDatabaseId);
2680         nspname = get_namespace_name(nspid);
2681
2682         initStringInfo(&result);
2683
2684         xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
2685
2686         schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
2687                                                                                                                           dbname,
2688                                                                                                                           nspname,
2689                                                                                                                           NULL);
2690
2691         appendStringInfo(&result,
2692                                          "<xsd:complexType name=\"%s\">\n", schematypename);
2693         if (!tableforest)
2694                 appendStringInfoString(&result,
2695                                                            "  <xsd:all>\n");
2696         else
2697                 appendStringInfoString(&result,
2698                                                            "  <xsd:sequence>\n");
2699
2700         foreach(cell, relid_list)
2701         {
2702                 Oid                     relid = lfirst_oid(cell);
2703                 char       *relname = get_rel_name(relid);
2704                 char       *xmltn = map_sql_identifier_to_xml_name(relname, true, false);
2705                 char       *tabletypename = map_multipart_sql_identifier_to_xml_name(tableforest ? "RowType" : "TableType",
2706                                                                                                                                           dbname,
2707                                                                                                                                          nspname,
2708                                                                                                                                         relname);
2709
2710                 if (!tableforest)
2711                         appendStringInfo(&result,
2712                                                          "    <xsd:element name=\"%s\" type=\"%s\"/>\n",
2713                                                          xmltn, tabletypename);
2714                 else
2715                         appendStringInfo(&result,
2716                                                          "    <xsd:element name=\"%s\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n",
2717                                                          xmltn, tabletypename);
2718         }
2719
2720         if (!tableforest)
2721                 appendStringInfoString(&result,
2722                                                            "  </xsd:all>\n");
2723         else
2724                 appendStringInfoString(&result,
2725                                                            "  </xsd:sequence>\n");
2726         appendStringInfoString(&result,
2727                                                    "</xsd:complexType>\n\n");
2728
2729         appendStringInfo(&result,
2730                                          "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2731                                          xmlsn, schematypename);
2732
2733         return result.data;
2734 }
2735
2736
2737 /*
2738  * Map an SQL catalog to XML Schema data types; see SQL/XML section
2739  * 9.8.
2740  */
2741 static const char *
2742 map_sql_catalog_to_xmlschema_types(List *nspid_list, bool nulls,
2743                                                                    bool tableforest, const char *targetns)
2744 {
2745         char       *dbname;
2746         char       *xmlcn;
2747         char       *catalogtypename;
2748         StringInfoData result;
2749         ListCell   *cell;
2750
2751         dbname = get_database_name(MyDatabaseId);
2752
2753         initStringInfo(&result);
2754
2755         xmlcn = map_sql_identifier_to_xml_name(dbname, true, false);
2756
2757         catalogtypename = map_multipart_sql_identifier_to_xml_name("CatalogType",
2758                                                                                                                            dbname,
2759                                                                                                                            NULL,
2760                                                                                                                            NULL);
2761
2762         appendStringInfo(&result,
2763                                          "<xsd:complexType name=\"%s\">\n", catalogtypename);
2764         appendStringInfoString(&result,
2765                                                    "  <xsd:all>\n");
2766
2767         foreach(cell, nspid_list)
2768         {
2769                 Oid                     nspid = lfirst_oid(cell);
2770                 char       *nspname = get_namespace_name(nspid);
2771                 char       *xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
2772                 char       *schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
2773                                                                                                                                           dbname,
2774                                                                                                                                          nspname,
2775                                                                                                                                            NULL);
2776
2777                 appendStringInfo(&result,
2778                                                  "    <xsd:element name=\"%s\" type=\"%s\"/>\n",
2779                                                  xmlsn, schematypename);
2780         }
2781
2782         appendStringInfoString(&result,
2783                                                    "  </xsd:all>\n");
2784         appendStringInfoString(&result,
2785                                                    "</xsd:complexType>\n\n");
2786
2787         appendStringInfo(&result,
2788                                          "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
2789                                          xmlcn, catalogtypename);
2790
2791         return result.data;
2792 }
2793
2794
2795 /*
2796  * Map an SQL data type to an XML name; see SQL/XML:2003 section 9.9.
2797  */
2798 static const char *
2799 map_sql_type_to_xml_name(Oid typeoid, int typmod)
2800 {
2801         StringInfoData result;
2802
2803         initStringInfo(&result);
2804
2805         switch (typeoid)
2806         {
2807                 case BPCHAROID:
2808                         if (typmod == -1)
2809                                 appendStringInfo(&result, "CHAR");
2810                         else
2811                                 appendStringInfo(&result, "CHAR_%d", typmod - VARHDRSZ);
2812                         break;
2813                 case VARCHAROID:
2814                         if (typmod == -1)
2815                                 appendStringInfo(&result, "VARCHAR");
2816                         else
2817                                 appendStringInfo(&result, "VARCHAR_%d", typmod - VARHDRSZ);
2818                         break;
2819                 case NUMERICOID:
2820                         if (typmod == -1)
2821                                 appendStringInfo(&result, "NUMERIC");
2822                         else
2823                                 appendStringInfo(&result, "NUMERIC_%d_%d",
2824                                                                  ((typmod - VARHDRSZ) >> 16) & 0xffff,
2825                                                                  (typmod - VARHDRSZ) & 0xffff);
2826                         break;
2827                 case INT4OID:
2828                         appendStringInfo(&result, "INTEGER");
2829                         break;
2830                 case INT2OID:
2831                         appendStringInfo(&result, "SMALLINT");
2832                         break;
2833                 case INT8OID:
2834                         appendStringInfo(&result, "BIGINT");
2835                         break;
2836                 case FLOAT4OID:
2837                         appendStringInfo(&result, "REAL");
2838                         break;
2839                 case FLOAT8OID:
2840                         appendStringInfo(&result, "DOUBLE");
2841                         break;
2842                 case BOOLOID:
2843                         appendStringInfo(&result, "BOOLEAN");
2844                         break;
2845                 case TIMEOID:
2846                         if (typmod == -1)
2847                                 appendStringInfo(&result, "TIME");
2848                         else
2849                                 appendStringInfo(&result, "TIME_%d", typmod);
2850                         break;
2851                 case TIMETZOID:
2852                         if (typmod == -1)
2853                                 appendStringInfo(&result, "TIME_WTZ");
2854                         else
2855                                 appendStringInfo(&result, "TIME_WTZ_%d", typmod);
2856                         break;
2857                 case TIMESTAMPOID:
2858                         if (typmod == -1)
2859                                 appendStringInfo(&result, "TIMESTAMP");
2860                         else
2861                                 appendStringInfo(&result, "TIMESTAMP_%d", typmod);
2862                         break;
2863                 case TIMESTAMPTZOID:
2864                         if (typmod == -1)
2865                                 appendStringInfo(&result, "TIMESTAMP_WTZ");
2866                         else
2867                                 appendStringInfo(&result, "TIMESTAMP_WTZ_%d", typmod);
2868                         break;
2869                 case DATEOID:
2870                         appendStringInfo(&result, "DATE");
2871                         break;
2872                 case XMLOID:
2873                         appendStringInfo(&result, "XML");
2874                         break;
2875                 default:
2876                         {
2877                                 HeapTuple       tuple;
2878                                 Form_pg_type typtuple;
2879
2880                                 tuple = SearchSysCache(TYPEOID,
2881                                                                            ObjectIdGetDatum(typeoid),
2882                                                                            0, 0, 0);
2883                                 if (!HeapTupleIsValid(tuple))
2884                                         elog(ERROR, "cache lookup failed for type %u", typeoid);
2885                                 typtuple = (Form_pg_type) GETSTRUCT(tuple);
2886
2887                                 appendStringInfoString(&result,
2888                                                                            map_multipart_sql_identifier_to_xml_name((typtuple->typtype == TYPTYPE_DOMAIN) ? "Domain" : "UDT",
2889                                                                                          get_database_name(MyDatabaseId),
2890                                                                   get_namespace_name(typtuple->typnamespace),
2891                                                                                                 NameStr(typtuple->typname)));
2892
2893                                 ReleaseSysCache(tuple);
2894                         }
2895         }
2896
2897         return result.data;
2898 }
2899
2900
2901 /*
2902  * Map a collection of SQL data types to XML Schema data types; see
2903  * SQL/XML:2002 section 9.10.
2904  */
2905 static const char *
2906 map_sql_typecoll_to_xmlschema_types(List *tupdesc_list)
2907 {
2908         List       *uniquetypes = NIL;
2909         int                     i;
2910         StringInfoData result;
2911         ListCell   *cell0;
2912
2913         /* extract all column types used in the set of TupleDescs */
2914         foreach(cell0, tupdesc_list)
2915         {
2916                 TupleDesc       tupdesc = (TupleDesc) lfirst(cell0);
2917
2918                 for (i = 0; i < tupdesc->natts; i++)
2919                 {
2920                         if (tupdesc->attrs[i]->attisdropped)
2921                                 continue;
2922                         uniquetypes = list_append_unique_oid(uniquetypes,
2923                                                                                                  tupdesc->attrs[i]->atttypid);
2924                 }
2925         }
2926
2927         /* add base types of domains */
2928         foreach(cell0, uniquetypes)
2929         {
2930                 Oid                     typid = lfirst_oid(cell0);
2931                 Oid                     basetypid = getBaseType(typid);
2932
2933                 if (basetypid != typid)
2934                         uniquetypes = list_append_unique_oid(uniquetypes, basetypid);
2935         }
2936
2937         /* Convert to textual form */
2938         initStringInfo(&result);
2939
2940         foreach(cell0, uniquetypes)
2941         {
2942                 appendStringInfo(&result, "%s\n",
2943                                                  map_sql_type_to_xmlschema_type(lfirst_oid(cell0),
2944                                                                                                                 -1));
2945         }
2946
2947         return result.data;
2948 }
2949
2950
2951 /*
2952  * Map an SQL data type to a named XML Schema data type; see SQL/XML
2953  * sections 9.11 and 9.15.
2954  *
2955  * (The distinction between 9.11 and 9.15 is basically that 9.15 adds
2956  * a name attribute, which this function does.  The name-less version
2957  * 9.11 doesn't appear to be required anywhere.)
2958  */
2959 static const char *
2960 map_sql_type_to_xmlschema_type(Oid typeoid, int typmod)
2961 {
2962         StringInfoData result;
2963         const char *typename = map_sql_type_to_xml_name(typeoid, typmod);
2964
2965         initStringInfo(&result);
2966
2967         if (typeoid == XMLOID)
2968         {
2969                 appendStringInfo(&result,
2970                                                  "<xsd:complexType mixed=\"true\">\n"
2971                                                  "  <xsd:sequence>\n"
2972                                                  "    <xsd:any name=\"element\" minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>\n"
2973                                                  "  </xsd:sequence>\n"
2974                                                  "</xsd:complexType>\n");
2975         }
2976         else
2977         {
2978                 appendStringInfo(&result,
2979                                                  "<xsd:simpleType name=\"%s\">\n", typename);
2980
2981                 switch (typeoid)
2982                 {
2983                         case BPCHAROID:
2984                         case VARCHAROID:
2985                         case TEXTOID:
2986                                 if (typmod != -1)
2987                                         appendStringInfo(&result,
2988                                                                   "  <xsd:restriction base=\"xsd:string\">\n"
2989                                                                          "    <xsd:maxLength value=\"%d\"/>\n"
2990                                                                          "  </xsd:restriction>\n",
2991                                                                          typmod - VARHDRSZ);
2992                                 break;
2993
2994                         case BYTEAOID:
2995                                 appendStringInfo(&result,
2996                                                                  "  <xsd:restriction base=\"xsd:%s\">\n"
2997                                                                  "  </xsd:restriction>\n",
2998                                 xmlbinary == XMLBINARY_BASE64 ? "base64Binary" : "hexBinary");
2999
3000                         case NUMERICOID:
3001                                 if (typmod != -1)
3002                                         appendStringInfo(&result,
3003                                                                  "  <xsd:restriction base=\"xsd:decimal\">\n"
3004                                                                          "    <xsd:totalDigits value=\"%d\"/>\n"
3005                                                                    "    <xsd:fractionDigits value=\"%d\"/>\n"
3006                                                                          "  </xsd:restriction>\n",
3007                                                                          ((typmod - VARHDRSZ) >> 16) & 0xffff,
3008                                                                          (typmod - VARHDRSZ) & 0xffff);
3009                                 break;
3010
3011                         case INT2OID:
3012                                 appendStringInfo(&result,
3013                                                                  "  <xsd:restriction base=\"xsd:short\">\n"
3014                                                                  "    <xsd:maxInclusive value=\"%d\"/>\n"
3015                                                                  "    <xsd:minInclusive value=\"%d\"/>\n"
3016                                                                  "  </xsd:restriction>\n",
3017                                                                  SHRT_MAX, SHRT_MIN);
3018                                 break;
3019
3020                         case INT4OID:
3021                                 appendStringInfo(&result,
3022                                                                  "  <xsd:restriction base='xsd:int'>\n"
3023                                                                  "    <xsd:maxInclusive value=\"%d\"/>\n"
3024                                                                  "    <xsd:minInclusive value=\"%d\"/>\n"
3025                                                                  "  </xsd:restriction>\n",
3026                                                                  INT_MAX, INT_MIN);
3027                                 break;
3028
3029                         case INT8OID:
3030                                 appendStringInfo(&result,
3031                                                                  "  <xsd:restriction base=\"xsd:long\">\n"
3032                                            "    <xsd:maxInclusive value=\"" INT64_FORMAT "\"/>\n"
3033                                            "    <xsd:minInclusive value=\"" INT64_FORMAT "\"/>\n"
3034                                                                  "  </xsd:restriction>\n",
3035                                                            (((uint64) 1) << (sizeof(int64) * 8 - 1)) - 1,
3036                                                                  (((uint64) 1) << (sizeof(int64) * 8 - 1)));
3037                                 break;
3038
3039                         case FLOAT4OID:
3040                                 appendStringInfo(&result,
3041                                 "  <xsd:restriction base=\"xsd:float\"></xsd:restriction>\n");
3042                                 break;
3043
3044                         case FLOAT8OID:
3045                                 appendStringInfo(&result,
3046                                                                  "  <xsd:restriction base=\"xsd:double\"></xsd:restriction>\n");
3047                                 break;
3048
3049                         case BOOLOID:
3050                                 appendStringInfo(&result,
3051                                                                  "  <xsd:restriction base=\"xsd:boolean\"></xsd:restriction>\n");
3052                                 break;
3053
3054                         case TIMEOID:
3055                         case TIMETZOID:
3056                                 {
3057                                         const char *tz = (typeoid == TIMETZOID ? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
3058
3059                                         if (typmod == -1)
3060                                                 appendStringInfo(&result,
3061                                                                         "  <xsd:restriction base=\"xsd:time\">\n"
3062                                                                                  "    <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}(.\\p{Nd}+)?%s\"/>\n"
3063                                                                                  "  </xsd:restriction>\n", tz);
3064                                         else if (typmod == 0)
3065                                                 appendStringInfo(&result,
3066                                                                         "  <xsd:restriction base=\"xsd:time\">\n"
3067                                                                                  "    <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3068                                                                                  "  </xsd:restriction>\n", tz);
3069                                         else
3070                                                 appendStringInfo(&result,
3071                                                                         "  <xsd:restriction base=\"xsd:time\">\n"
3072                                                                                  "    <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}.\\p{Nd}{%d}%s\"/>\n"
3073                                                         "  </xsd:restriction>\n", typmod - VARHDRSZ, tz);
3074                                         break;
3075                                 }
3076
3077                         case TIMESTAMPOID:
3078                         case TIMESTAMPTZOID:
3079                                 {
3080                                         const char *tz = (typeoid == TIMESTAMPTZOID ? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
3081
3082                                         if (typmod == -1)
3083                                                 appendStringInfo(&result,
3084                                                                 "  <xsd:restriction base=\"xsd:dateTime\">\n"
3085                                                                                  "    <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}(.\\p{Nd}+)?%s\"/>\n"
3086                                                                                  "  </xsd:restriction>\n", tz);
3087                                         else if (typmod == 0)
3088                                                 appendStringInfo(&result,
3089                                                                 "  <xsd:restriction base=\"xsd:dateTime\">\n"
3090                                                                                  "    <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3091                                                                                  "  </xsd:restriction>\n", tz);
3092                                         else
3093                                                 appendStringInfo(&result,
3094                                                                 "  <xsd:restriction base=\"xsd:dateTime\">\n"
3095                                                                                  "    <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}.\\p{Nd}{%d}%s\"/>\n"
3096                                                         "  </xsd:restriction>\n", typmod - VARHDRSZ, tz);
3097                                         break;
3098                                 }
3099
3100                         case DATEOID:
3101                                 appendStringInfo(&result,
3102                                                                  "  <xsd:restriction base=\"xsd:date\">\n"
3103                                                                  "    <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}\"/>\n"
3104                                                                  "  </xsd:restriction>\n");
3105                                 break;
3106
3107                         default:
3108                                 if (get_typtype(typeoid) == TYPTYPE_DOMAIN)
3109                                 {
3110                                         Oid                     base_typeoid;
3111                                         int32           base_typmod = -1;
3112
3113                                         base_typeoid = getBaseTypeAndTypmod(typeoid, &base_typmod);
3114
3115                                         appendStringInfo(&result,
3116                                                                          "  <xsd:restriction base=\"%s\"/>\n",
3117                                                 map_sql_type_to_xml_name(base_typeoid, base_typmod));
3118                                 }
3119                                 break;
3120                 }
3121                 appendStringInfo(&result,
3122                                                  "</xsd:simpleType>\n");
3123         }
3124
3125         return result.data;
3126 }
3127
3128
3129 /*
3130  * Map an SQL row to an XML element, taking the row from the active
3131  * SPI cursor.  See also SQL/XML:2003 section 9.12.
3132  */
3133 static void
3134 SPI_sql_row_to_xmlelement(int rownum, StringInfo result, char *tablename,
3135                                                   bool nulls, bool tableforest,
3136                                                   const char *targetns, bool top_level)
3137 {
3138         int                     i;
3139         char       *xmltn;
3140
3141         if (tablename)
3142                 xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
3143         else
3144         {
3145                 if (tableforest)
3146                         xmltn = "row";
3147                 else
3148                         xmltn = "table";
3149         }
3150
3151         if (tableforest)
3152                 xmldata_root_element_start(result, xmltn, NULL, targetns, top_level);
3153         else
3154                 appendStringInfoString(result, "<row>\n");
3155
3156         for (i = 1; i <= SPI_tuptable->tupdesc->natts; i++)
3157         {
3158                 char       *colname;
3159                 Datum           colval;
3160                 bool            isnull;
3161
3162                 colname = map_sql_identifier_to_xml_name(SPI_fname(SPI_tuptable->tupdesc, i),
3163                                                                                                  true, false);
3164                 colval = SPI_getbinval(SPI_tuptable->vals[rownum],
3165                                                            SPI_tuptable->tupdesc,
3166                                                            i,
3167                                                            &isnull);
3168                 if (isnull)
3169                 {
3170                         if (nulls)
3171                                 appendStringInfo(result, "  <%s xsi:nil='true'/>\n", colname);
3172                 }
3173                 else
3174                         appendStringInfo(result, "  <%s>%s</%s>\n",
3175                                                          colname,
3176                                                          map_sql_value_to_xml_value(colval,
3177                                                                         SPI_gettypeid(SPI_tuptable->tupdesc, i)),
3178                                                          colname);
3179         }
3180
3181         if (tableforest)
3182         {
3183                 xmldata_root_element_end(result, xmltn);
3184                 appendStringInfoChar(result, '\n');
3185         }
3186         else
3187                 appendStringInfoString(result, "</row>\n\n");
3188 }
3189
3190
3191 /*
3192  * XPath related functions
3193  */
3194
3195 #ifdef USE_LIBXML
3196 /*
3197  * Convert XML node to text (dump subtree in case of element,
3198  * return value otherwise)
3199  */
3200 static text *
3201 xml_xmlnodetoxmltype(xmlNodePtr cur)
3202 {
3203         xmlChar    *str;
3204         xmltype    *result;
3205         size_t          len;
3206         xmlBufferPtr buf;
3207
3208         if (cur->type == XML_ELEMENT_NODE)
3209         {
3210                 buf = xmlBufferCreate();
3211                 xmlNodeDump(buf, NULL, cur, 0, 1);
3212                 result = xmlBuffer_to_xmltype(buf);
3213                 xmlBufferFree(buf);
3214         }
3215         else
3216         {
3217                 str = xmlXPathCastNodeToString(cur);
3218                 len = strlen((char *) str);
3219                 result = (text *) palloc(len + VARHDRSZ);
3220                 SET_VARSIZE(result, len + VARHDRSZ);
3221                 memcpy(VARDATA(result), str, len);
3222         }
3223
3224         return result;
3225 }
3226 #endif
3227
3228
3229 /*
3230  * Evaluate XPath expression and return array of XML values.
3231  *
3232  * As we have no support of XQuery sequences yet, this function seems
3233  * to be the most useful one (array of XML functions plays a role of
3234  * some kind of substitution for XQuery sequences).
3235  *
3236  * Workaround here: we parse XML data in different way to allow XPath for
3237  * fragments (see "XPath for fragment" TODO comment inside).
3238  */
3239 Datum
3240 xpath(PG_FUNCTION_ARGS)
3241 {
3242 #ifdef USE_LIBXML
3243         text       *xpath_expr_text = PG_GETARG_TEXT_P(0);
3244         xmltype    *data = PG_GETARG_XML_P(1);
3245         ArrayType  *namespaces = PG_GETARG_ARRAYTYPE_P(2);
3246         ArrayBuildState *astate = NULL;
3247         xmlParserCtxtPtr ctxt = NULL;
3248         xmlDocPtr       doc = NULL;
3249         xmlXPathContextPtr xpathctx = NULL;
3250         xmlXPathCompExprPtr xpathcomp = NULL;
3251         xmlXPathObjectPtr xpathobj = NULL;
3252         char       *datastr;
3253         int32           len;
3254         int32           xpath_len;
3255         xmlChar    *string;
3256         xmlChar    *xpath_expr;
3257         int                     i;
3258         int                     res_nitems;
3259         int                     ndim;
3260         Datum      *ns_names_uris;
3261         bool       *ns_names_uris_nulls;
3262         int                     ns_count;
3263
3264         /*
3265          * Namespace mappings are passed as text[].  If an empty array is passed
3266          * (ndim = 0, "0-dimensional"), then there are no namespace mappings.
3267          * Else, a 2-dimensional array with length of the second axis being equal
3268          * to 2 should be passed, i.e., every subarray contains 2 elements, the
3269          * first element defining the name, the second one the URI.  Example:
3270          * ARRAY[ARRAY['myns', 'http://example.com'], ARRAY['myns2',
3271          * 'http://example2.com']].
3272          */
3273         ndim = ARR_NDIM(namespaces);
3274         if (ndim != 0)
3275         {
3276                 int                *dims;
3277
3278                 dims = ARR_DIMS(namespaces);
3279
3280                 if (ndim != 2 || dims[1] != 2)
3281                         ereport(ERROR,
3282                                         (errcode(ERRCODE_DATA_EXCEPTION),
3283                                          errmsg("invalid array for XML namespace mapping"),
3284                                          errdetail("The array must be two-dimensional with length of the second axis equal to 2.")));
3285
3286                 Assert(ARR_ELEMTYPE(namespaces) == TEXTOID);
3287
3288                 deconstruct_array(namespaces, TEXTOID, -1, false, 'i',
3289                                                   &ns_names_uris, &ns_names_uris_nulls,
3290                                                   &ns_count);
3291
3292                 Assert((ns_count % 2) == 0);    /* checked above */
3293                 ns_count /= 2;                  /* count pairs only */
3294         }
3295         else
3296         {
3297                 ns_names_uris = NULL;
3298                 ns_names_uris_nulls = NULL;
3299                 ns_count = 0;
3300         }
3301
3302         datastr = VARDATA(data);
3303         len = VARSIZE(data) - VARHDRSZ;
3304         xpath_len = VARSIZE(xpath_expr_text) - VARHDRSZ;
3305         if (xpath_len == 0)
3306                 ereport(ERROR,
3307                                 (errcode(ERRCODE_DATA_EXCEPTION),
3308                                  errmsg("empty XPath expression")));
3309
3310         xml_init();
3311
3312         /*
3313          * To handle both documents and fragments, regardless of the fact whether
3314          * the XML datum has a single root (XML well-formedness), we wrap the XML
3315          * datum in a dummy element (<x>...</x>) and extend the XPath expression
3316          * accordingly.  To do it, throw away the XML prolog, if any.
3317          */
3318         if (len >= 5 &&
3319                 xmlStrncmp((xmlChar *) datastr, (xmlChar *) "<?xml", 5) == 0)
3320         {
3321                 i = 5;
3322                 while (i < len &&
3323                            !(datastr[i - 1] == '?' && datastr[i] == '>'))
3324                         i++;
3325
3326                 if (i == len)
3327                         xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3328                                                 "could not parse XML data");
3329
3330                 ++i;
3331
3332                 datastr += i;
3333                 len -= i;
3334         }
3335
3336         string = (xmlChar *) palloc((len + 8) * sizeof(xmlChar));
3337         memcpy(string, "<x>", 3);
3338         memcpy(string + 3, datastr, len);
3339         memcpy(string + 3 + len, "</x>", 5);
3340         len += 7;
3341
3342         xpath_expr = (xmlChar *) palloc((xpath_len + 3) * sizeof(xmlChar));
3343         memcpy(xpath_expr, "/x", 2);
3344         memcpy(xpath_expr + 2, VARDATA(xpath_expr_text), xpath_len);
3345         xpath_expr[xpath_len + 2] = '\0';
3346         xpath_len += 2;
3347
3348         /* We use a PG_TRY block to ensure libxml parser is cleaned up on error */
3349         PG_TRY();
3350         {
3351                 xmlInitParser();
3352
3353                 /*
3354                  * redundant XML parsing (two parsings for the same value during one
3355                  * command execution are possible)
3356                  */
3357                 ctxt = xmlNewParserCtxt();
3358                 if (ctxt == NULL)
3359                         xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3360                                                 "could not allocate parser context");
3361                 doc = xmlCtxtReadMemory(ctxt, (char *) string, len, NULL, NULL, 0);
3362                 if (doc == NULL)
3363                         xml_ereport(ERROR, ERRCODE_INVALID_XML_DOCUMENT,
3364                                                 "could not parse XML data");
3365                 xpathctx = xmlXPathNewContext(doc);
3366                 if (xpathctx == NULL)
3367                         xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3368                                                 "could not allocate XPath context");
3369                 xpathctx->node = xmlDocGetRootElement(doc);
3370                 if (xpathctx->node == NULL)
3371                         xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3372                                                 "could not find root XML element");
3373
3374                 /* register namespaces, if any */
3375                 if (ns_count > 0)
3376                 {
3377                         for (i = 0; i < ns_count; i++)
3378                         {
3379                                 char       *ns_name;
3380                                 char       *ns_uri;
3381
3382                                 if (ns_names_uris_nulls[i * 2] ||
3383                                         ns_names_uris_nulls[i * 2 + 1])
3384                                         ereport(ERROR,
3385                                                         (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3386                                           errmsg("neither namespace name nor URI may be null")));
3387                                 ns_name = _textout(ns_names_uris[i * 2]);
3388                                 ns_uri = _textout(ns_names_uris[i * 2 + 1]);
3389                                 if (xmlXPathRegisterNs(xpathctx,
3390                                                                            (xmlChar *) ns_name,
3391                                                                            (xmlChar *) ns_uri) != 0)
3392                                         ereport(ERROR,          /* is this an internal error??? */
3393                                                         (errmsg("could not register XML namespace with name \"%s\" and URI \"%s\"",
3394                                                                         ns_name, ns_uri)));
3395                         }
3396                 }
3397
3398                 xpathcomp = xmlXPathCompile(xpath_expr);
3399                 if (xpathcomp == NULL)  /* TODO: show proper XPath error details */
3400                         xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
3401                                                 "invalid XPath expression");
3402
3403                 xpathobj = xmlXPathCompiledEval(xpathcomp, xpathctx);
3404                 if (xpathobj == NULL)   /* TODO: reason? */
3405                         ereport(ERROR,
3406                                         (errmsg("could not create XPath object")));
3407
3408                 xmlXPathFreeCompExpr(xpathcomp);
3409                 xpathcomp = NULL;
3410
3411                 /* return empty array in cases when nothing is found */
3412                 if (xpathobj->nodesetval == NULL)
3413                         res_nitems = 0;
3414                 else
3415                         res_nitems = xpathobj->nodesetval->nodeNr;
3416
3417                 if (res_nitems)
3418                         for (i = 0; i < xpathobj->nodesetval->nodeNr; i++)
3419                         {
3420                                 Datum           elem;
3421                                 bool            elemisnull = false;
3422
3423                                 elem = PointerGetDatum(xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[i]));
3424                                 astate = accumArrayResult(astate, elem,
3425                                                                                   elemisnull, XMLOID,
3426                                                                                   CurrentMemoryContext);
3427                         }
3428
3429                 xmlXPathFreeObject(xpathobj);
3430                 xpathobj = NULL;
3431                 xmlXPathFreeContext(xpathctx);
3432                 xpathctx = NULL;
3433                 xmlFreeDoc(doc);
3434                 doc = NULL;
3435                 xmlFreeParserCtxt(ctxt);
3436                 ctxt = NULL;
3437                 xmlCleanupParser();
3438         }
3439         PG_CATCH();
3440         {
3441                 if (xpathcomp)
3442                         xmlXPathFreeCompExpr(xpathcomp);
3443                 if (xpathobj)
3444                         xmlXPathFreeObject(xpathobj);
3445                 if (xpathctx)
3446                         xmlXPathFreeContext(xpathctx);
3447                 if (doc)
3448                         xmlFreeDoc(doc);
3449                 if (ctxt)
3450                         xmlFreeParserCtxt(ctxt);
3451                 xmlCleanupParser();
3452
3453                 PG_RE_THROW();
3454         }
3455         PG_END_TRY();
3456
3457         if (res_nitems == 0)
3458                 PG_RETURN_ARRAYTYPE_P(construct_empty_array(XMLOID));
3459         else
3460                 PG_RETURN_ARRAYTYPE_P(makeArrayResult(astate, CurrentMemoryContext));
3461 #else
3462         NO_XML_SUPPORT();
3463         return 0;
3464 #endif
3465 }