OSDN Git Service

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