OSDN Git Service

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