OSDN Git Service

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