OSDN Git Service

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