xml.c 81.5 KB
Newer Older
1 2 3 4 5 6
/*-------------------------------------------------------------------------
 *
 * xml.c
 *	  XML data type support.
 *
 *
7
 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
8 9
 * Portions Copyright (c) 1994, Regents of the University of California
 *
10
 * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.73 2008/05/04 16:42:41 tgl Exp $
11 12 13 14 15 16 17 18 19 20 21
 *
 *-------------------------------------------------------------------------
 */

/*
 * Generally, XML type support is only available when libxml use was
 * configured during the build.  But even if that is not done, the
 * type and all the functions are available, but most of them will
 * fail.  For one thing, this avoids having to manage variant catalog
 * installations.  But it also has nice effects such as that you can
 * dump a database containing XML type data even if the server is not
Bruce Momjian's avatar
Bruce Momjian committed
22
 * linked with libxml.	Thus, make sure xml_out() works even if nothing
23
 * else does.
24 25
 */

26
/*
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
 * Notes on memory management:
 *
 * Via callbacks, libxml is told to use palloc and friends for memory
 * management, within a context that we reset at transaction end (and also at
 * subtransaction abort) to prevent memory leaks.  Resetting at transaction or
 * subtransaction abort is necessary since we might have thrown a longjmp
 * while some data structures were not linked from anywhere persistent.
 * Resetting at transaction commit might not be necessary, but seems a good
 * idea to forestall long-term leaks.
 *
 * Sometimes libxml allocates global structures in the hope that it can reuse
 * them later on.  Therefore, before resetting LibxmlContext, we must tell
 * libxml to discard any global data it has.  The libxml API documentation is
 * not very good about specifying this, but for now we assume that
 * xmlCleanupParser() will get rid of anything we need to worry about.
 *
 * We use palloc --- which will throw a longjmp on error --- for allocation
 * callbacks that officially should act like malloc, ie, return NULL on
 * out-of-memory.  This is a bit risky since there is a chance of leaving
 * persistent libxml data structures in an inconsistent partially-constructed
 * state, perhaps leading to crash in xmlCleanupParser().  However, as of
 * early 2008 it is *known* that libxml can crash on out-of-memory due to
 * inadequate checks for NULL returns, so this behavior seems the lesser
 * of two evils.
51 52
 */

53 54 55 56 57 58 59 60
#include "postgres.h"

#ifdef USE_LIBXML
#include <libxml/chvalid.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/uri.h>
#include <libxml/xmlerror.h>
61
#include <libxml/xmlwriter.h>
62 63
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
Bruce Momjian's avatar
Bruce Momjian committed
64
#endif   /* USE_LIBXML */
65

66
#include "catalog/namespace.h"
67
#include "catalog/pg_type.h"
68
#include "commands/dbcommands.h"
69
#include "executor/executor.h"
70
#include "executor/spi.h"
71
#include "fmgr.h"
72
#include "lib/stringinfo.h"
73
#include "libpq/pqformat.h"
74
#include "mb/pg_wchar.h"
75
#include "miscadmin.h"
76
#include "nodes/execnodes.h"
77 78
#include "parser/parse_expr.h"
#include "utils/array.h"
79
#include "utils/builtins.h"
80 81
#include "utils/date.h"
#include "utils/datetime.h"
82
#include "utils/lsyscache.h"
83
#include "utils/memutils.h"
84
#include "access/tupmacs.h"
85 86 87
#include "utils/xml.h"


88
/* GUC variables */
89 90
int xmlbinary;
int xmloption;
91

92 93
#ifdef USE_LIBXML

94
static StringInfo xml_err_buf = NULL;
95
static MemoryContext LibxmlContext = NULL;
96

Bruce Momjian's avatar
Bruce Momjian committed
97
static void xml_init(void);
98 99
static void xml_memory_init(void);
static void xml_memory_cleanup(void);
Bruce Momjian's avatar
Bruce Momjian committed
100 101 102 103 104 105 106 107
static void *xml_palloc(size_t size);
static void *xml_repalloc(void *ptr, size_t size);
static void xml_pfree(void *ptr);
static char *xml_pstrdup(const char *string);
static void xml_ereport(int level, int sqlcode, const char *msg);
static void xml_errorHandler(void *ctxt, const char *msg,...);
static void xml_ereport_by_code(int level, int sqlcode,
					const char *msg, int errcode);
108
static xmlChar *xml_text2xmlChar(text *in);
Bruce Momjian's avatar
Bruce Momjian committed
109 110 111 112
static int parse_xml_decl(const xmlChar * str, size_t *lenp,
			   xmlChar ** version, xmlChar ** encoding, int *standalone);
static bool print_xml_decl(StringInfo buf, const xmlChar * version,
			   pg_enc encoding, int standalone);
Tom Lane's avatar
Tom Lane committed
113
static xmlDocPtr xml_parse(text *data, XmlOptionType xmloption_arg,
Bruce Momjian's avatar
Bruce Momjian committed
114 115 116
		  bool preserve_whitespace, xmlChar * encoding);
static text *xml_xmlnodetoxmltype(xmlNodePtr cur);
#endif   /* USE_LIBXML */
117

Tom Lane's avatar
Tom Lane committed
118 119 120 121
static StringInfo query_to_xml_internal(const char *query, char *tablename,
					  const char *xmlschema, bool nulls, bool tableforest,
					  const char *targetns, bool top_level);
static const char *map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid,
Bruce Momjian's avatar
Bruce Momjian committed
122
						 bool nulls, bool tableforest, const char *targetns);
Tom Lane's avatar
Tom Lane committed
123 124 125 126 127 128
static const char *map_sql_schema_to_xmlschema_types(Oid nspid,
								  List *relid_list, bool nulls,
								  bool tableforest, const char *targetns);
static const char *map_sql_catalog_to_xmlschema_types(List *nspid_list,
								   bool nulls, bool tableforest,
								   const char *targetns);
Bruce Momjian's avatar
Bruce Momjian committed
129 130 131
static const char *map_sql_type_to_xml_name(Oid typeoid, int typmod);
static const char *map_sql_typecoll_to_xmlschema_types(List *tupdesc_list);
static const char *map_sql_type_to_xmlschema_type(Oid typeoid, int typmod);
Tom Lane's avatar
Tom Lane committed
132 133 134
static void SPI_sql_row_to_xmlelement(int rownum, StringInfo result,
						  char *tablename, bool nulls, bool tableforest,
						  const char *targetns, bool top_level);
135

136 137 138
#define NO_XML_SUPPORT() \
	ereport(ERROR, \
			(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
139
			 errmsg("unsupported XML feature"), \
140
			 errdetail("This functionality requires the server to be built with libxml support."), \
Peter Eisentraut's avatar
Peter Eisentraut committed
141
			 errhint("You need to rebuild PostgreSQL using --with-libxml.")))
142 143


144 145 146 147 148 149
/* from SQL/XML:2003 section 4.7 */
#define NAMESPACE_XSD "http://www.w3.org/2001/XMLSchema"
#define NAMESPACE_XSI "http://www.w3.org/2001/XMLSchema-instance"
#define NAMESPACE_SQLXML "http://standards.iso.org/iso/9075/2003/sqlxml"


150 151 152
#ifdef USE_LIBXML

static int
Bruce Momjian's avatar
Bruce Momjian committed
153
xmlChar_to_encoding(xmlChar * encoding_name)
154
{
Bruce Momjian's avatar
Bruce Momjian committed
155
	int			encoding = pg_char_to_encoding((char *) encoding_name);
156 157 158 159 160 161 162 163 164 165 166

	if (encoding < 0)
		ereport(ERROR,
				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
				 errmsg("invalid encoding name \"%s\"",
						(char *) encoding_name)));
	return encoding;
}
#endif


167 168 169 170 171 172 173
/*
 * xml_in uses a plain C string to VARDATA conversion, so for the time being
 * we use the conversion function for the text datatype.
 *
 * This is only acceptable so long as xmltype and text use the same
 * representation.
 */
174 175 176 177
Datum
xml_in(PG_FUNCTION_ARGS)
{
#ifdef USE_LIBXML
Bruce Momjian's avatar
Bruce Momjian committed
178 179 180
	char	   *s = PG_GETARG_CSTRING(0);
	xmltype    *vardata;
	xmlDocPtr	doc;
181

182
	vardata = (xmltype *) cstring_to_text(s);
183 184

	/*
Bruce Momjian's avatar
Bruce Momjian committed
185 186
	 * Parse the data to check if it is well-formed XML data.  Assume that
	 * ERROR occurred if parsing failed.
187
	 */
188
	doc = xml_parse(vardata, xmloption, true, NULL);
189
	xmlFreeDoc(doc);
190 191 192 193 194 195 196 197 198

	PG_RETURN_XML_P(vardata);
#else
	NO_XML_SUPPORT();
	return 0;
#endif
}


199 200 201
#define PG_XML_DEFAULT_VERSION "1.0"


202 203 204 205 206 207 208
/*
 * xml_out_internal uses a plain VARDATA to C string conversion, so for the
 * time being we use the conversion function for the text datatype.
 *
 * This is only acceptable so long as xmltype and text use the same
 * representation.
 */
209
static char *
210
xml_out_internal(xmltype *x, pg_enc target_encoding)
211
{
Bruce Momjian's avatar
Bruce Momjian committed
212
	char	   *str;
213
	size_t		len;
Bruce Momjian's avatar
Bruce Momjian committed
214

215
#ifdef USE_LIBXML
Bruce Momjian's avatar
Bruce Momjian committed
216 217
	xmlChar    *version;
	xmlChar    *encoding;
218 219 220 221
	int			standalone;
	int			res_code;
#endif

222 223
	str = text_to_cstring((text *) x);
	len = strlen(str);
224 225

#ifdef USE_LIBXML
Tom Lane's avatar
Tom Lane committed
226
	if ((res_code = parse_xml_decl((xmlChar *) str,
Bruce Momjian's avatar
Bruce Momjian committed
227
							   &len, &version, &encoding, &standalone)) == 0)
228 229 230 231 232
	{
		StringInfoData buf;

		initStringInfo(&buf);

233
		if (!print_xml_decl(&buf, version, target_encoding, standalone))
234 235
		{
			/*
Bruce Momjian's avatar
Bruce Momjian committed
236 237 238
			 * If we are not going to produce an XML declaration, eat a single
			 * newline in the original string to prevent empty first lines in
			 * the output.
239 240 241 242 243 244 245 246 247 248
			 */
			if (*(str + len) == '\n')
				len += 1;
		}
		appendStringInfoString(&buf, str + len);

		return buf.data;
	}

	xml_ereport_by_code(WARNING, ERRCODE_INTERNAL_ERROR,
249 250
						"could not parse XML declaration in stored value",
						res_code);
251 252 253 254 255
#endif
	return str;
}


256 257 258
Datum
xml_out(PG_FUNCTION_ARGS)
{
Bruce Momjian's avatar
Bruce Momjian committed
259
	xmltype    *x = PG_GETARG_XML_P(0);
260

261
	/*
Bruce Momjian's avatar
Bruce Momjian committed
262 263 264 265
	 * xml_out removes the encoding property in all cases.	This is because we
	 * cannot control from here whether the datum will be converted to a
	 * different client encoding, so we'd do more harm than good by including
	 * it.
266 267
	 */
	PG_RETURN_CSTRING(xml_out_internal(x, 0));
268 269 270
}


271 272 273 274 275
Datum
xml_recv(PG_FUNCTION_ARGS)
{
#ifdef USE_LIBXML
	StringInfo	buf = (StringInfo) PG_GETARG_POINTER(0);
Bruce Momjian's avatar
Bruce Momjian committed
276
	xmltype    *result;
277
	char	   *str;
278
	char	   *newstr;
279
	int			nbytes;
280
	xmlDocPtr	doc;
Bruce Momjian's avatar
Bruce Momjian committed
281
	xmlChar    *encoding = NULL;
282

283
	/*
Bruce Momjian's avatar
Bruce Momjian committed
284 285 286
	 * Read the data in raw format. We don't know yet what the encoding is, as
	 * that information is embedded in the xml declaration; so we have to
	 * parse that before converting to server encoding.
287 288 289
	 */
	nbytes = buf->len - buf->cursor;
	str = (char *) pq_getmsgbytes(buf, nbytes);
290

291 292 293 294 295 296
	/*
	 * We need a null-terminated string to pass to parse_xml_decl().  Rather
	 * than make a separate copy, make the temporary result one byte bigger
	 * than it needs to be.
	 */
	result = palloc(nbytes + 1 + VARHDRSZ);
297
	SET_VARSIZE(result, nbytes + VARHDRSZ);
298
	memcpy(VARDATA(result), str, nbytes);
299 300
	str = VARDATA(result);
	str[nbytes] = '\0';
301 302

	parse_xml_decl((xmlChar *) str, NULL, NULL, &encoding, NULL);
303 304

	/*
Bruce Momjian's avatar
Bruce Momjian committed
305 306
	 * Parse the data to check if it is well-formed XML data.  Assume that
	 * xml_parse will throw ERROR if not.
307
	 */
308
	doc = xml_parse(result, xmloption, true, encoding);
309
	xmlFreeDoc(doc);
310

311
	/* Now that we know what we're dealing with, convert to server encoding */
312 313
	newstr = (char *) pg_do_encoding_conversion((unsigned char *) str,
												nbytes,
314
												encoding ?
Bruce Momjian's avatar
Bruce Momjian committed
315
											  xmlChar_to_encoding(encoding) :
316
												PG_UTF8,
317 318 319 320
												GetDatabaseEncoding());

	if (newstr != str)
	{
321
		pfree(result);
322
		result = (xmltype *) cstring_to_text(newstr);
323
		pfree(newstr);
324 325
	}

326 327 328 329 330 331 332 333 334 335 336
	PG_RETURN_XML_P(result);
#else
	NO_XML_SUPPORT();
	return 0;
#endif
}


Datum
xml_send(PG_FUNCTION_ARGS)
{
Bruce Momjian's avatar
Bruce Momjian committed
337
	xmltype    *x = PG_GETARG_XML_P(0);
338
	char	   *outval;
339
	StringInfoData buf;
Bruce Momjian's avatar
Bruce Momjian committed
340

341
	/*
Bruce Momjian's avatar
Bruce Momjian committed
342 343
	 * xml_out_internal doesn't convert the encoding, it just prints the right
	 * declaration. pq_sendtext will do the conversion.
344 345
	 */
	outval = xml_out_internal(x, pg_get_client_encoding());
346 347

	pq_begintypsend(&buf);
348 349
	pq_sendtext(&buf, outval, strlen(outval));
	pfree(outval);
350 351 352 353
	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}


354 355 356 357 358 359
#ifdef USE_LIBXML
static void
appendStringInfoText(StringInfo str, const text *t)
{
	appendBinaryStringInfo(str, VARDATA(t), VARSIZE(t) - VARHDRSZ);
}
360
#endif
361 362 363 364 365


static xmltype *
stringinfo_to_xmltype(StringInfo buf)
{
366
	return (xmltype *) cstring_to_text_with_len(buf->data, buf->len);
367
}
368 369


370 371 372
static xmltype *
cstring_to_xmltype(const char *string)
{
373
	return (xmltype *) cstring_to_text(string);
374 375 376
}


377
#ifdef USE_LIBXML
378 379 380
static xmltype *
xmlBuffer_to_xmltype(xmlBufferPtr buf)
{
381 382
	return (xmltype *) cstring_to_text_with_len((char *) xmlBufferContent(buf),
												xmlBufferLength(buf));
383
}
384 385 386 387 388 389 390
#endif


Datum
xmlcomment(PG_FUNCTION_ARGS)
{
#ifdef USE_LIBXML
Bruce Momjian's avatar
Bruce Momjian committed
391 392 393
	text	   *arg = PG_GETARG_TEXT_P(0);
	char	   *argdata = VARDATA(arg);
	int			len = VARSIZE(arg) - VARHDRSZ;
394
	StringInfoData buf;
Bruce Momjian's avatar
Bruce Momjian committed
395
	int			i;
396 397 398

	/* check for "--" in string or "-" at the end */
	for (i = 1; i < len; i++)
Tom Lane's avatar
Tom Lane committed
399 400 401 402 403 404 405 406 407 408
	{
		if (argdata[i] == '-' && argdata[i - 1] == '-')
			ereport(ERROR,
					(errcode(ERRCODE_INVALID_XML_COMMENT),
					 errmsg("invalid XML comment")));
	}
	if (len > 0 && argdata[len - 1] == '-')
		ereport(ERROR,
				(errcode(ERRCODE_INVALID_XML_COMMENT),
				 errmsg("invalid XML comment")));
409 410 411 412 413 414 415 416 417 418 419 420 421 422

	initStringInfo(&buf);
	appendStringInfo(&buf, "<!--");
	appendStringInfoText(&buf, arg);
	appendStringInfo(&buf, "-->");

	PG_RETURN_XML_P(stringinfo_to_xmltype(&buf));
#else
	NO_XML_SUPPORT();
	return 0;
#endif
}


423 424 425

/*
 * TODO: xmlconcat needs to merge the notations and unparsed entities
Bruce Momjian's avatar
Bruce Momjian committed
426
 * of the argument values.	Not very important in practice, though.
427 428 429 430 431 432
 */
xmltype *
xmlconcat(List *args)
{
#ifdef USE_LIBXML
	int			global_standalone = 1;
Bruce Momjian's avatar
Bruce Momjian committed
433
	xmlChar    *global_version = NULL;
434
	bool		global_version_no_value = false;
Tom Lane's avatar
Tom Lane committed
435 436
	StringInfoData buf;
	ListCell   *v;
437 438 439 440

	initStringInfo(&buf);
	foreach(v, args)
	{
Bruce Momjian's avatar
Bruce Momjian committed
441
		xmltype    *x = DatumGetXmlP(PointerGetDatum(lfirst(v)));
442
		size_t		len;
Bruce Momjian's avatar
Bruce Momjian committed
443
		xmlChar    *version;
444 445 446 447
		int			standalone;
		char	   *str;

		len = VARSIZE(x) - VARHDRSZ;
448
		str = text_to_cstring((text *) x);
449 450 451 452 453 454 455 456

		parse_xml_decl((xmlChar *) str, &len, &version, NULL, &standalone);

		if (standalone == 0 && global_standalone == 1)
			global_standalone = 0;
		if (standalone < 0)
			global_standalone = -1;

457 458 459
		if (!version)
			global_version_no_value = true;
		else if (!global_version)
460
			global_version = xmlStrdup(version);
461
		else if (xmlStrcmp(version, global_version) != 0)
462 463 464 465 466 467 468 469 470 471 472 473
			global_version_no_value = true;

		appendStringInfoString(&buf, str + len);
		pfree(str);
	}

	if (!global_version_no_value || global_standalone >= 0)
	{
		StringInfoData buf2;

		initStringInfo(&buf2);

474
		print_xml_decl(&buf2,
Tom Lane's avatar
Tom Lane committed
475
					   (!global_version_no_value) ? global_version : NULL,
476 477
					   0,
					   global_standalone);
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506

		appendStringInfoString(&buf2, buf.data);
		buf = buf2;
	}

	return stringinfo_to_xmltype(&buf);
#else
	NO_XML_SUPPORT();
	return NULL;
#endif
}


/*
 * XMLAGG support
 */
Datum
xmlconcat2(PG_FUNCTION_ARGS)
{
	if (PG_ARGISNULL(0))
	{
		if (PG_ARGISNULL(1))
			PG_RETURN_NULL();
		else
			PG_RETURN_XML_P(PG_GETARG_XML_P(1));
	}
	else if (PG_ARGISNULL(1))
		PG_RETURN_XML_P(PG_GETARG_XML_P(0));
	else
Tom Lane's avatar
Tom Lane committed
507 508
		PG_RETURN_XML_P(xmlconcat(list_make2(PG_GETARG_XML_P(0),
											 PG_GETARG_XML_P(1))));
509 510 511
}


512
Datum
513
texttoxml(PG_FUNCTION_ARGS)
514
{
515
	text	   *data = PG_GETARG_TEXT_P(0);
516

517 518 519 520 521 522 523
	PG_RETURN_XML_P(xmlparse(data, xmloption, true));
}


Datum
xmltotext(PG_FUNCTION_ARGS)
{
Bruce Momjian's avatar
Bruce Momjian committed
524
	xmltype    *data = PG_GETARG_XML_P(0);
525

526
	/* It's actually binary compatible. */
Tom Lane's avatar
Tom Lane committed
527
	PG_RETURN_TEXT_P((text *) data);
528 529 530 531
}


text *
532
xmltotext_with_xmloption(xmltype *data, XmlOptionType xmloption_arg)
533 534 535 536 537 538 539 540
{
	if (xmloption_arg == XMLOPTION_DOCUMENT && !xml_is_document(data))
		ereport(ERROR,
				(errcode(ERRCODE_NOT_AN_XML_DOCUMENT),
				 errmsg("not an XML document")));

	/* It's actually binary compatible, save for the above check. */
	return (text *) data;
541
}
542 543


544
xmltype *
545
xmlelement(XmlExprState *xmlExpr, ExprContext *econtext)
546 547
{
#ifdef USE_LIBXML
Bruce Momjian's avatar
Bruce Momjian committed
548 549
	XmlExpr    *xexpr = (XmlExpr *) xmlExpr->xprstate.expr;
	xmltype    *result;
550 551
	List	   *named_arg_strings;
	List	   *arg_strings;
552 553 554 555 556 557
	int			i;
	ListCell   *arg;
	ListCell   *narg;
	xmlBufferPtr buf;
	xmlTextWriterPtr writer;

558
	/*
Bruce Momjian's avatar
Bruce Momjian committed
559 560 561 562
	 * We first evaluate all the arguments, then start up libxml and create
	 * the result.	This avoids issues if one of the arguments involves a call
	 * to some other function or subsystem that wants to use libxml on its own
	 * terms.
563 564
	 */
	named_arg_strings = NIL;
565
	i = 0;
566
	foreach(arg, xmlExpr->named_args)
567
	{
Bruce Momjian's avatar
Bruce Momjian committed
568
		ExprState  *e = (ExprState *) lfirst(arg);
569 570 571
		Datum		value;
		bool		isnull;
		char	   *str;
572 573

		value = ExecEvalExpr(e, econtext, &isnull, NULL);
574 575 576
		if (isnull)
			str = NULL;
		else
577
			str = OutputFunctionCall(&xmlExpr->named_outfuncs[i], value);
578
		named_arg_strings = lappend(named_arg_strings, str);
579 580 581
		i++;
	}

582
	arg_strings = NIL;
583 584
	foreach(arg, xmlExpr->args)
	{
Bruce Momjian's avatar
Bruce Momjian committed
585
		ExprState  *e = (ExprState *) lfirst(arg);
586 587 588
		Datum		value;
		bool		isnull;
		char	   *str;
589 590

		value = ExecEvalExpr(e, econtext, &isnull, NULL);
591
		/* here we can just forget NULL elements immediately */
592
		if (!isnull)
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
		{
			str = map_sql_value_to_xml_value(value,
											 exprType((Node *) e->expr));
			arg_strings = lappend(arg_strings, str);
		}
	}

	/* now safe to run libxml */
	xml_init();

	buf = xmlBufferCreate();
	writer = xmlNewTextWriterMemory(buf, 0);

	xmlTextWriterStartElement(writer, (xmlChar *) xexpr->name);

	forboth(arg, named_arg_strings, narg, xexpr->arg_names)
	{
Bruce Momjian's avatar
Bruce Momjian committed
610 611
		char	   *str = (char *) lfirst(arg);
		char	   *argname = strVal(lfirst(narg));
612 613 614 615 616 617 618 619 620 621 622 623

		if (str)
		{
			xmlTextWriterWriteAttribute(writer,
										(xmlChar *) argname,
										(xmlChar *) str);
			pfree(str);
		}
	}

	foreach(arg, arg_strings)
	{
Bruce Momjian's avatar
Bruce Momjian committed
624
		char	   *str = (char *) lfirst(arg);
625 626

		xmlTextWriterWriteRaw(writer, (xmlChar *) str);
627 628 629 630 631 632 633
	}

	xmlTextWriterEndElement(writer);
	xmlFreeTextWriter(writer);

	result = xmlBuffer_to_xmltype(buf);
	xmlBufferFree(buf);
634

635 636 637 638 639 640 641 642
	return result;
#else
	NO_XML_SUPPORT();
	return NULL;
#endif
}


643
xmltype *
644
xmlparse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace)
645 646
{
#ifdef USE_LIBXML
647 648
	xmlDocPtr	doc;

649
	doc = xml_parse(data, xmloption_arg, preserve_whitespace, NULL);
650
	xmlFreeDoc(doc);
651

652
	return (xmltype *) data;
653 654
#else
	NO_XML_SUPPORT();
655
	return NULL;
656 657 658 659
#endif
}


660
xmltype *
661
xmlpi(char *target, text *arg, bool arg_is_null, bool *result_is_null)
662 663
{
#ifdef USE_LIBXML
Bruce Momjian's avatar
Bruce Momjian committed
664
	xmltype    *result;
665 666
	StringInfoData buf;

667
	if (pg_strcasecmp(target, "xml") == 0)
668
		ereport(ERROR,
Bruce Momjian's avatar
Bruce Momjian committed
669
				(errcode(ERRCODE_SYNTAX_ERROR), /* really */
670
				 errmsg("invalid XML processing instruction"),
671
				 errdetail("XML processing instruction target name cannot be \"%s\".", target)));
672

673
	/*
Bruce Momjian's avatar
Bruce Momjian committed
674 675
	 * Following the SQL standard, the null check comes after the syntax check
	 * above.
676 677 678
	 */
	*result_is_null = arg_is_null;
	if (*result_is_null)
Bruce Momjian's avatar
Bruce Momjian committed
679
		return NULL;
680

681 682
	initStringInfo(&buf);

683 684 685
	appendStringInfo(&buf, "<?%s", target);

	if (arg != NULL)
686
	{
Bruce Momjian's avatar
Bruce Momjian committed
687
		char	   *string;
688

689
		string = text_to_cstring(arg);
690
		if (strstr(string, "?>") != NULL)
Bruce Momjian's avatar
Bruce Momjian committed
691 692 693 694
			ereport(ERROR,
					(errcode(ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION),
					 errmsg("invalid XML processing instruction"),
			errdetail("XML processing instruction cannot contain \"?>\".")));
695

696
		appendStringInfoChar(&buf, ' ');
697
		appendStringInfoString(&buf, string + strspn(string, " "));
698
		pfree(string);
699 700 701
	}
	appendStringInfoString(&buf, "?>");

702 703 704
	result = stringinfo_to_xmltype(&buf);
	pfree(buf.data);
	return result;
705 706
#else
	NO_XML_SUPPORT();
707
	return NULL;
708 709 710 711
#endif
}


712
xmltype *
713
xmlroot(xmltype *data, text *version, int standalone)
714 715
{
#ifdef USE_LIBXML
716 717
	char	   *str;
	size_t		len;
Bruce Momjian's avatar
Bruce Momjian committed
718
	xmlChar    *orig_version;
719 720
	int			orig_standalone;
	StringInfoData buf;
721

722
	len = VARSIZE(data) - VARHDRSZ;
723
	str = text_to_cstring((text *) data);
724 725

	parse_xml_decl((xmlChar *) str, &len, &orig_version, NULL, &orig_standalone);
726

727
	if (version)
728
		orig_version = xml_text2xmlChar(version);
729
	else
730
		orig_version = NULL;
731 732

	switch (standalone)
733
	{
734 735 736 737 738
		case XML_STANDALONE_YES:
			orig_standalone = 1;
			break;
		case XML_STANDALONE_NO:
			orig_standalone = 0;
739
			break;
740 741
		case XML_STANDALONE_NO_VALUE:
			orig_standalone = -1;
742
			break;
743 744
		case XML_STANDALONE_OMITTED:
			/* leave original value */
745
			break;
746 747
	}

748 749 750
	initStringInfo(&buf);
	print_xml_decl(&buf, orig_version, 0, orig_standalone);
	appendStringInfoString(&buf, str + len);
751

752
	return stringinfo_to_xmltype(&buf);
753 754
#else
	NO_XML_SUPPORT();
755
	return NULL;
756 757 758 759 760 761
#endif
}


/*
 * Validate document (given as string) against DTD (given as external link)
762 763 764 765 766
 *
 * This has been removed because it is a security hole: unprivileged users
 * should not be able to use Postgres to fetch arbitrary external files,
 * which unfortunately is exactly what libxml is willing to do with the DTD
 * parameter.
767 768 769 770
 */
Datum
xmlvalidate(PG_FUNCTION_ARGS)
{
771 772 773
	ereport(ERROR,
			(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
			 errmsg("xmlvalidate is not implemented")));
774 775 776 777
	return 0;
}


778
bool
779
xml_is_document(xmltype *arg)
780 781 782 783 784 785 786 787
{
#ifdef USE_LIBXML
	bool		result;
	xmlDocPtr	doc = NULL;
	MemoryContext ccxt = CurrentMemoryContext;

	PG_TRY();
	{
788
		doc = xml_parse((text *) arg, XMLOPTION_DOCUMENT, true, NULL);
789 790 791 792
		result = true;
	}
	PG_CATCH();
	{
Bruce Momjian's avatar
Bruce Momjian committed
793
		ErrorData  *errdata;
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
		MemoryContext ecxt;

		ecxt = MemoryContextSwitchTo(ccxt);
		errdata = CopyErrorData();
		if (errdata->sqlerrcode == ERRCODE_INVALID_XML_DOCUMENT)
		{
			FlushErrorState();
			result = false;
		}
		else
		{
			MemoryContextSwitchTo(ecxt);
			PG_RE_THROW();
		}
	}
	PG_END_TRY();

	if (doc)
		xmlFreeDoc(doc);

	return result;
Bruce Momjian's avatar
Bruce Momjian committed
815
#else							/* not USE_LIBXML */
816 817
	NO_XML_SUPPORT();
	return false;
Bruce Momjian's avatar
Bruce Momjian committed
818
#endif   /* not USE_LIBXML */
819 820 821
}


822 823 824 825 826 827 828 829 830 831 832 833 834
/*
 * xml cleanup function for transaction end.  This is also called on
 * subtransaction abort; see notes at top of file for rationale.
 */
void
AtEOXact_xml(void)
{
#ifdef USE_LIBXML
	xml_memory_cleanup();
#endif
}


835 836 837
#ifdef USE_LIBXML

/*
838 839 840 841 842
 * Set up for use of libxml --- this should be called by each function that
 * is about to use libxml facilities.
 *
 * TODO: xmlChar is utf8-char, make proper tuning (initdb with enc!=utf8 and
 * check)
843 844 845 846
 */
static void
xml_init(void)
{
847
	static bool first_time = true;
848

849
	if (first_time)
850
	{
851
		/* Stuff we need do only once per session */
852 853
		MemoryContext oldcontext;

854
		/*
Bruce Momjian's avatar
Bruce Momjian committed
855 856
		 * Currently, we have no pure UTF-8 support for internals -- check if
		 * we can work.
857 858 859 860 861 862 863 864
		 */
		if (sizeof(char) != sizeof(xmlChar))
			ereport(ERROR,
					(errmsg("could not initialize XML library"),
					 errdetail("libxml2 has incompatible char type: sizeof(char)=%u, sizeof(xmlChar)=%u.",
							   (int) sizeof(char), (int) sizeof(xmlChar))));

		/* create error buffer in permanent context */
865 866 867
		oldcontext = MemoryContextSwitchTo(TopMemoryContext);
		xml_err_buf = makeStringInfo();
		MemoryContextSwitchTo(oldcontext);
868 869 870 871 872

		/* Now that xml_err_buf exists, safe to call xml_errorHandler */
		xmlSetGenericErrorFunc(NULL, xml_errorHandler);

		/* Set up memory allocation our way, too */
873
		xml_memory_init();
874 875 876 877 878

		/* Check library compatibility */
		LIBXML_TEST_VERSION;

		first_time = false;
879 880 881 882
	}
	else
	{
		/* Reset pre-existing buffer to empty */
883
		Assert(xml_err_buf != NULL);
884
		resetStringInfo(xml_err_buf);
885

886 887 888 889
		/*
		 * We re-establish the callback functions every time.  This makes it
		 * safe for other subsystems (PL/Perl, say) to also use libxml with
		 * their own callbacks ... so long as they likewise set up the
Bruce Momjian's avatar
Bruce Momjian committed
890 891
		 * callbacks on every use.	It's cheap enough to not be worth worrying
		 * about, anyway.
892 893
		 */
		xmlSetGenericErrorFunc(NULL, xml_errorHandler);
894
		xml_memory_init();
895
	}
896 897 898
}


899 900 901 902 903 904 905 906 907
/*
 * SQL/XML allows storing "XML documents" or "XML content".  "XML
 * documents" are specified by the XML specification and are parsed
 * easily by libxml.  "XML content" is specified by SQL/XML as the
 * production "XMLDecl? content".  But libxml can only parse the
 * "content" part, so we have to parse the XML declaration ourselves
 * to complete this.
 */

Tom Lane's avatar
Tom Lane committed
908 909 910 911 912 913 914 915
#define CHECK_XML_SPACE(p) \
	do { \
		if (!xmlIsBlank_ch(*(p))) \
			return XML_ERR_SPACE_REQUIRED; \
	} while (0)

#define SKIP_XML_SPACE(p) \
	while (xmlIsBlank_ch(*(p))) (p)++
916

917
/* Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender */
918 919
/* Beware of multiple evaluations of argument! */
#define PG_XMLISNAMECHAR(c) \
920 921 922 923 924 925
	(xmlIsBaseChar_ch(c) || xmlIsIdeographicQ(c) \
			|| xmlIsDigit_ch(c) \
			|| c == '.' || c == '-' || c == '_' || c == ':' \
			|| xmlIsCombiningQ(c) \
			|| xmlIsExtender_ch(c))

926
static int
Bruce Momjian's avatar
Bruce Momjian committed
927 928
parse_xml_decl(const xmlChar * str, size_t *lenp,
			   xmlChar ** version, xmlChar ** encoding, int *standalone)
929 930 931
{
	const xmlChar *p;
	const xmlChar *save_p;
932
	size_t		len;
933
	int			utf8char;
934
	int			utf8len;
935

Tom Lane's avatar
Tom Lane committed
936
	xml_init();
937

938 939 940 941 942 943 944
	if (version)
		*version = NULL;
	if (encoding)
		*encoding = NULL;
	if (standalone)
		*standalone = -1;

Tom Lane's avatar
Tom Lane committed
945 946
	p = str;

Bruce Momjian's avatar
Bruce Momjian committed
947
	if (xmlStrncmp(p, (xmlChar *) "<?xml", 5) != 0)
948 949
		goto finished;

950
	/* if next char is name char, it's a PI like <?xml-stylesheet ...?> */
Bruce Momjian's avatar
Bruce Momjian committed
951 952
	utf8len = strlen((const char *) (p + 5));
	utf8char = xmlGetUTF8Char(p + 5, &utf8len);
953
	if (PG_XMLISNAMECHAR(utf8char))
954 955
		goto finished;

956 957 958 959 960
	p += 5;

	/* version */
	CHECK_XML_SPACE(p);
	SKIP_XML_SPACE(p);
Bruce Momjian's avatar
Bruce Momjian committed
961
	if (xmlStrncmp(p, (xmlChar *) "version", 7) != 0)
962 963 964 965 966 967 968
		return XML_ERR_VERSION_MISSING;
	p += 7;
	SKIP_XML_SPACE(p);
	if (*p != '=')
		return XML_ERR_VERSION_MISSING;
	p += 1;
	SKIP_XML_SPACE(p);
969 970 971 972 973 974 975 976 977 978 979 980 981 982

	if (*p == '\'' || *p == '"')
	{
		const xmlChar *q;

		q = xmlStrchr(p + 1, *p);
		if (!q)
			return XML_ERR_VERSION_MISSING;

		if (version)
			*version = xmlStrndup(p + 1, q - p - 1);
		p = q + 1;
	}
	else
983 984 985 986 987
		return XML_ERR_VERSION_MISSING;

	/* encoding */
	save_p = p;
	SKIP_XML_SPACE(p);
Bruce Momjian's avatar
Bruce Momjian committed
988
	if (xmlStrncmp(p, (xmlChar *) "encoding", 8) == 0)
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
	{
		CHECK_XML_SPACE(save_p);
		p += 8;
		SKIP_XML_SPACE(p);
		if (*p != '=')
			return XML_ERR_MISSING_ENCODING;
		p += 1;
		SKIP_XML_SPACE(p);

		if (*p == '\'' || *p == '"')
		{
			const xmlChar *q;

			q = xmlStrchr(p + 1, *p);
			if (!q)
				return XML_ERR_MISSING_ENCODING;

1006
			if (encoding)
Bruce Momjian's avatar
Bruce Momjian committed
1007
				*encoding = xmlStrndup(p + 1, q - p - 1);
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
			p = q + 1;
		}
		else
			return XML_ERR_MISSING_ENCODING;
	}
	else
	{
		p = save_p;
	}

	/* standalone */
	save_p = p;
	SKIP_XML_SPACE(p);
Bruce Momjian's avatar
Bruce Momjian committed
1021
	if (xmlStrncmp(p, (xmlChar *) "standalone", 10) == 0)
1022 1023 1024 1025 1026 1027 1028 1029
	{
		CHECK_XML_SPACE(save_p);
		p += 10;
		SKIP_XML_SPACE(p);
		if (*p != '=')
			return XML_ERR_STANDALONE_VALUE;
		p += 1;
		SKIP_XML_SPACE(p);
1030 1031
		if (xmlStrncmp(p, (xmlChar *) "'yes'", 5) == 0 ||
			xmlStrncmp(p, (xmlChar *) "\"yes\"", 5) == 0)
1032 1033 1034 1035
		{
			*standalone = 1;
			p += 5;
		}
1036 1037
		else if (xmlStrncmp(p, (xmlChar *) "'no'", 4) == 0 ||
				 xmlStrncmp(p, (xmlChar *) "\"no\"", 4) == 0)
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
		{
			*standalone = 0;
			p += 4;
		}
		else
			return XML_ERR_STANDALONE_VALUE;
	}
	else
	{
		p = save_p;
	}

	SKIP_XML_SPACE(p);
Bruce Momjian's avatar
Bruce Momjian committed
1051
	if (xmlStrncmp(p, (xmlChar *) "?>", 2) != 0)
1052 1053 1054 1055
		return XML_ERR_XMLDECL_NOT_FINISHED;
	p += 2;

finished:
1056 1057 1058 1059 1060 1061 1062 1063 1064
	len = p - str;

	for (p = str; p < str + len; p++)
		if (*p > 127)
			return XML_ERR_INVALID_CHAR;

	if (lenp)
		*lenp = len;

1065 1066 1067 1068
	return XML_ERR_OK;
}


1069 1070
/*
 * Write an XML declaration.  On output, we adjust the XML declaration
Bruce Momjian's avatar
Bruce Momjian committed
1071
 * as follows.	(These rules are the moral equivalent of the clause
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
 * "Serialization of an XML value" in the SQL standard.)
 *
 * We try to avoid generating an XML declaration if possible.  This is
 * so that you don't get trivial things like xml '<foo/>' resulting in
 * '<?xml version="1.0"?><foo/>', which would surely be annoying.  We
 * must provide a declaration if the standalone property is specified
 * or if we include an encoding declaration.  If we have a
 * declaration, we must specify a version (XML requires this).
 * Otherwise we only make a declaration if the version is not "1.0",
 * which is the default version specified in SQL:2003.
 */
static bool
Bruce Momjian's avatar
Bruce Momjian committed
1084
print_xml_decl(StringInfo buf, const xmlChar * version,
Tom Lane's avatar
Tom Lane committed
1085
			   pg_enc encoding, int standalone)
1086
{
Tom Lane's avatar
Tom Lane committed
1087 1088
	xml_init();

1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
	if ((version && strcmp((char *) version, PG_XML_DEFAULT_VERSION) != 0)
		|| (encoding && encoding != PG_UTF8)
		|| standalone != -1)
	{
		appendStringInfoString(buf, "<?xml");

		if (version)
			appendStringInfo(buf, " version=\"%s\"", version);
		else
			appendStringInfo(buf, " version=\"%s\"", PG_XML_DEFAULT_VERSION);

		if (encoding && encoding != PG_UTF8)
Tom Lane's avatar
Tom Lane committed
1101
		{
1102
			/*
Bruce Momjian's avatar
Bruce Momjian committed
1103 1104
			 * XXX might be useful to convert this to IANA names (ISO-8859-1
			 * instead of LATIN1 etc.); needs field experience
1105
			 */
Tom Lane's avatar
Tom Lane committed
1106 1107 1108
			appendStringInfo(buf, " encoding=\"%s\"",
							 pg_encoding_to_char(encoding));
		}
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122

		if (standalone == 1)
			appendStringInfoString(buf, " standalone=\"yes\"");
		else if (standalone == 0)
			appendStringInfoString(buf, " standalone=\"no\"");
		appendStringInfoString(buf, "?>");

		return true;
	}
	else
		return false;
}


1123 1124
/*
 * Convert a C string to XML internal representation
1125
 *
Tom Lane's avatar
Tom Lane committed
1126
 * TODO maybe, libxml2's xmlreader is better? (do not construct DOM,
1127
 * yet do not use SAX - see xmlreader.c)
1128 1129
 */
static xmlDocPtr
Tom Lane's avatar
Tom Lane committed
1130
xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace,
Bruce Momjian's avatar
Bruce Momjian committed
1131
		  xmlChar * encoding)
1132
{
Bruce Momjian's avatar
Bruce Momjian committed
1133 1134 1135
	int32		len;
	xmlChar    *string;
	xmlChar    *utf8string;
1136 1137
	xmlParserCtxtPtr ctxt;
	xmlDocPtr	doc;
1138

Bruce Momjian's avatar
Bruce Momjian committed
1139
	len = VARSIZE(data) - VARHDRSZ;		/* will be useful later */
1140
	string = xml_text2xmlChar(data);
1141

1142 1143
	utf8string = pg_do_encoding_conversion(string,
										   len,
1144 1145 1146
										   encoding ?
										   xmlChar_to_encoding(encoding) :
										   GetDatabaseEncoding(),
1147 1148
										   PG_UTF8);

1149
	xml_init();
1150 1151 1152 1153 1154
	xmlInitParser();
	ctxt = xmlNewParserCtxt();
	if (ctxt == NULL)
		xml_ereport(ERROR, ERRCODE_OUT_OF_MEMORY,
					"could not allocate parser context");
1155

1156
	if (xmloption_arg == XMLOPTION_DOCUMENT)
1157
	{
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
		/*
		 * Note, that here we try to apply DTD defaults
		 * (XML_PARSE_DTDATTR) according to SQL/XML:10.16.7.d: 'Default
		 * values defined by internal DTD are applied'. As for external
		 * DTDs, we try to support them too, (see SQL/XML:10.16.7.e)
		 */
		doc = xmlCtxtReadDoc(ctxt, utf8string,
							 NULL,
							 "UTF-8",
							 XML_PARSE_NOENT | XML_PARSE_DTDATTR
							 | (preserve_whitespace ? 0 : XML_PARSE_NOBLANKS));
		if (doc == NULL)
			xml_ereport(ERROR, ERRCODE_INVALID_XML_DOCUMENT,
						"invalid XML document");
1172
	}
1173
	else
1174
	{
1175 1176 1177 1178
		int			res_code;
		size_t		count;
		xmlChar    *version = NULL;
		int			standalone = -1;
1179

1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
		doc = xmlNewDoc(NULL);

		res_code = parse_xml_decl(utf8string,
								  &count, &version, NULL, &standalone);
		if (res_code != 0)
			xml_ereport_by_code(ERROR, ERRCODE_INVALID_XML_CONTENT,
								"invalid XML content: invalid XML declaration",
								res_code);

		res_code = xmlParseBalancedChunkMemory(doc, NULL, NULL, 0,
											   utf8string + count, NULL);
		if (res_code != 0)
			xml_ereport(ERROR, ERRCODE_INVALID_XML_CONTENT,
						"invalid XML content");

		doc->version = xmlStrdup(version);
		doc->encoding = xmlStrdup((xmlChar *) "UTF-8");
		doc->standalone = standalone;
1198
	}
1199 1200

	xmlFreeParserCtxt(ctxt);
1201

1202 1203 1204 1205
	return doc;
}


1206
/*
1207
 * xmlChar<->text conversions
1208 1209 1210 1211
 */
static xmlChar *
xml_text2xmlChar(text *in)
{
1212
	return (xmlChar *) text_to_cstring(in);
1213 1214 1215
}


1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
/*
 * Manage the special context used for all libxml allocations
 */
static void
xml_memory_init(void)
{
	/*
	 * Create memory context if not there already.  We make it a child of
	 * TopMemoryContext, even though our current policy is that it doesn't
	 * survive past transaction end, because we want to be really really
	 * sure it doesn't go away before we've called xmlCleanupParser().
	 */
	if (LibxmlContext == NULL)
		LibxmlContext = AllocSetContextCreate(TopMemoryContext,
											  "LibxmlContext",
											  ALLOCSET_DEFAULT_MINSIZE,
											  ALLOCSET_DEFAULT_INITSIZE,
											  ALLOCSET_DEFAULT_MAXSIZE);

	/* Re-establish the callbacks even if already set */
	xmlMemSetup(xml_pfree, xml_palloc, xml_repalloc, xml_pstrdup);
}

static void
xml_memory_cleanup(void)
{
	if (LibxmlContext != NULL)
	{
		/* Give libxml a chance to clean up dangling pointers */
		xmlCleanupParser();

		/* And flush the context */
		MemoryContextDelete(LibxmlContext);
		LibxmlContext = NULL;
	}
}

1253 1254
/*
 * Wrappers for memory management functions
1255 1256 1257 1258
 */
static void *
xml_palloc(size_t size)
{
1259
	return MemoryContextAlloc(LibxmlContext, size);
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
}


static void *
xml_repalloc(void *ptr, size_t size)
{
	return repalloc(ptr, size);
}


static void
xml_pfree(void *ptr)
{
	pfree(ptr);
}


static char *
xml_pstrdup(const char *string)
{
1280
	return MemoryContextStrdup(LibxmlContext, string);
1281 1282 1283 1284
}


/*
1285 1286 1287 1288
 * Wrapper for "ereport" function for XML-related errors.  The "msg"
 * is the SQL-level message; some can be adopted from the SQL/XML
 * standard.  This function adds libxml's native error messages, if
 * any, as detail.
1289 1290
 */
static void
Tom Lane's avatar
Tom Lane committed
1291
xml_ereport(int level, int sqlcode, const char *msg)
1292
{
Bruce Momjian's avatar
Bruce Momjian committed
1293
	char	   *detail;
1294

1295
	if (xml_err_buf->len > 0)
1296
	{
1297
		detail = pstrdup(xml_err_buf->data);
1298
		resetStringInfo(xml_err_buf);
1299
	}
1300 1301
	else
		detail = NULL;
1302

1303 1304
	/* libxml error messages end in '\n'; get rid of it */
	if (detail)
1305
	{
Bruce Momjian's avatar
Bruce Momjian committed
1306
		size_t		len;
1307 1308

		len = strlen(detail);
Bruce Momjian's avatar
Bruce Momjian committed
1309 1310
		if (len > 0 && detail[len - 1] == '\n')
			detail[len - 1] = '\0';
1311

1312 1313
		ereport(level,
				(errcode(sqlcode),
1314 1315
				 errmsg("%s", msg),
				 errdetail("%s", detail)));
1316 1317 1318
	}
	else
	{
1319 1320
		ereport(level,
				(errcode(sqlcode),
1321
				 errmsg("%s", msg)));
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
	}
}


/*
 * Error handler for libxml error messages
 */
static void
xml_errorHandler(void *ctxt, const char *msg,...)
{
1332 1333
	/* Append the formatted text to xml_err_buf */
	for (;;)
1334
	{
1335 1336 1337 1338 1339 1340 1341
		va_list		args;
		bool		success;

		/* Try to format the data. */
		va_start(args, msg);
		success = appendStringInfoVA(xml_err_buf, msg, args);
		va_end(args);
1342

1343 1344 1345 1346 1347
		if (success)
			break;

		/* Double the buffer size and try again. */
		enlargeStringInfo(xml_err_buf, xml_err_buf->maxlen);
1348 1349 1350 1351 1352
	}
}


/*
1353 1354 1355 1356 1357
 * Wrapper for "ereport" function for XML-related errors.  The "msg"
 * is the SQL-level message; some can be adopted from the SQL/XML
 * standard.  This function uses "code" to create a textual detail
 * message.  At the moment, we only need to cover those codes that we
 * may raise in this file.
1358 1359
 */
static void
1360 1361
xml_ereport_by_code(int level, int sqlcode,
					const char *msg, int code)
1362
{
Bruce Momjian's avatar
Bruce Momjian committed
1363
	const char *det;
1364

Bruce Momjian's avatar
Bruce Momjian committed
1365
	switch (code)
1366
	{
1367
		case XML_ERR_INVALID_CHAR:
1368
			det = gettext_noop("Invalid character value.");
1369 1370
			break;
		case XML_ERR_SPACE_REQUIRED:
1371
			det = gettext_noop("Space required.");
1372 1373
			break;
		case XML_ERR_STANDALONE_VALUE:
1374
			det = gettext_noop("standalone accepts only 'yes' or 'no'.");
1375 1376
			break;
		case XML_ERR_VERSION_MISSING:
1377
			det = gettext_noop("Malformed declaration: missing version.");
1378 1379
			break;
		case XML_ERR_MISSING_ENCODING:
1380
			det = gettext_noop("Missing encoding in text declaration.");
1381 1382
			break;
		case XML_ERR_XMLDECL_NOT_FINISHED:
1383
			det = gettext_noop("Parsing XML declaration: '?>' expected.");
1384
			break;
Bruce Momjian's avatar
Bruce Momjian committed
1385
		default:
1386
			det = gettext_noop("Unrecognized libxml error code: %d.");
1387
			break;
1388
	}
1389

1390 1391 1392 1393
	ereport(level,
			(errcode(sqlcode),
			 errmsg("%s", msg),
			 errdetail(det, code)));
1394 1395 1396 1397
}


/*
1398
 * Convert one char in the current server encoding to a Unicode codepoint.
1399 1400
 */
static pg_wchar
1401
sqlchar_to_unicode(char *s)
1402
{
Bruce Momjian's avatar
Bruce Momjian committed
1403 1404
	char	   *utf8string;
	pg_wchar	ret[2];			/* need space for trailing zero */
1405 1406 1407 1408 1409

	utf8string = (char *) pg_do_encoding_conversion((unsigned char *) s,
													pg_mblen(s),
													GetDatabaseEncoding(),
													PG_UTF8);
1410

1411
	pg_encoding_mb2wchar_with_len(PG_UTF8, utf8string, ret, pg_mblen(s));
1412

1413
	return ret[0];
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
}


static bool
is_valid_xml_namefirst(pg_wchar c)
{
	/* (Letter | '_' | ':') */
	return (xmlIsBaseCharQ(c) || xmlIsIdeographicQ(c)
			|| c == '_' || c == ':');
}


static bool
is_valid_xml_namechar(pg_wchar c)
{
	/* Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender */
	return (xmlIsBaseCharQ(c) || xmlIsIdeographicQ(c)
			|| xmlIsDigitQ(c)
			|| c == '.' || c == '-' || c == '_' || c == ':'
			|| xmlIsCombiningQ(c)
			|| xmlIsExtenderQ(c));
}
Bruce Momjian's avatar
Bruce Momjian committed
1436
#endif   /* USE_LIBXML */
1437 1438 1439 1440 1441 1442


/*
 * Map SQL identifier to XML name; see SQL/XML:2003 section 9.1.
 */
char *
Tom Lane's avatar
Tom Lane committed
1443 1444
map_sql_identifier_to_xml_name(char *ident, bool fully_escaped,
							   bool escape_period)
1445
{
1446 1447
#ifdef USE_LIBXML
	StringInfoData buf;
Bruce Momjian's avatar
Bruce Momjian committed
1448
	char	   *p;
1449

1450
	/*
Bruce Momjian's avatar
Bruce Momjian committed
1451 1452
	 * SQL/XML doesn't make use of this case anywhere, so it's probably a
	 * mistake.
1453 1454 1455
	 */
	Assert(fully_escaped || !escape_period);

1456 1457 1458 1459 1460 1461
	initStringInfo(&buf);

	for (p = ident; *p; p += pg_mblen(p))
	{
		if (*p == ':' && (p == ident || fully_escaped))
			appendStringInfo(&buf, "_x003A_");
Bruce Momjian's avatar
Bruce Momjian committed
1462
		else if (*p == '_' && *(p + 1) == 'x')
1463
			appendStringInfo(&buf, "_x005F_");
1464 1465
		else if (fully_escaped && p == ident &&
				 pg_strncasecmp(p, "xml", 3) == 0)
1466 1467 1468 1469 1470 1471
		{
			if (*p == 'x')
				appendStringInfo(&buf, "_x0078_");
			else
				appendStringInfo(&buf, "_x0058_");
		}
1472 1473
		else if (escape_period && *p == '.')
			appendStringInfo(&buf, "_x002E_");
1474 1475
		else
		{
Bruce Momjian's avatar
Bruce Momjian committed
1476
			pg_wchar	u = sqlchar_to_unicode(p);
1477

1478 1479 1480 1481
			if ((p == ident)
				? !is_valid_xml_namefirst(u)
				: !is_valid_xml_namechar(u))
				appendStringInfo(&buf, "_x%04X_", (unsigned int) u);
1482 1483 1484 1485 1486 1487
			else
				appendBinaryStringInfo(&buf, p, pg_mblen(p));
		}
	}

	return buf.data;
Bruce Momjian's avatar
Bruce Momjian committed
1488
#else							/* not USE_LIBXML */
1489 1490
	NO_XML_SUPPORT();
	return NULL;
Bruce Momjian's avatar
Bruce Momjian committed
1491
#endif   /* not USE_LIBXML */
1492
}
1493 1494 1495


/*
1496 1497 1498 1499 1500
 * Map a Unicode codepoint into the current server encoding.
 */
static char *
unicode_to_sqlchar(pg_wchar c)
{
Bruce Momjian's avatar
Bruce Momjian committed
1501
	static unsigned char utf8string[5]; /* need trailing zero */
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534

	if (c <= 0x7F)
	{
		utf8string[0] = c;
	}
	else if (c <= 0x7FF)
	{
		utf8string[0] = 0xC0 | ((c >> 6) & 0x1F);
		utf8string[1] = 0x80 | (c & 0x3F);
	}
	else if (c <= 0xFFFF)
	{
		utf8string[0] = 0xE0 | ((c >> 12) & 0x0F);
		utf8string[1] = 0x80 | ((c >> 6) & 0x3F);
		utf8string[2] = 0x80 | (c & 0x3F);
	}
	else
	{
		utf8string[0] = 0xF0 | ((c >> 18) & 0x07);
		utf8string[1] = 0x80 | ((c >> 12) & 0x3F);
		utf8string[2] = 0x80 | ((c >> 6) & 0x3F);
		utf8string[3] = 0x80 | (c & 0x3F);
	}

	return (char *) pg_do_encoding_conversion(utf8string,
											  pg_mblen((char *) utf8string),
											  PG_UTF8,
											  GetDatabaseEncoding());
}


/*
 * Map XML name to SQL identifier; see SQL/XML:2003 section 9.17.
1535 1536 1537 1538 1539
 */
char *
map_xml_name_to_sql_identifier(char *name)
{
	StringInfoData buf;
Bruce Momjian's avatar
Bruce Momjian committed
1540
	char	   *p;
1541 1542 1543 1544 1545

	initStringInfo(&buf);

	for (p = name; *p; p += pg_mblen(p))
	{
Bruce Momjian's avatar
Bruce Momjian committed
1546 1547 1548 1549 1550 1551
		if (*p == '_' && *(p + 1) == 'x'
			&& isxdigit((unsigned char) *(p + 2))
			&& isxdigit((unsigned char) *(p + 3))
			&& isxdigit((unsigned char) *(p + 4))
			&& isxdigit((unsigned char) *(p + 5))
			&& *(p + 6) == '_')
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
		{
			unsigned int u;

			sscanf(p + 2, "%X", &u);
			appendStringInfoString(&buf, unicode_to_sqlchar(u));
			p += 6;
		}
		else
			appendBinaryStringInfo(&buf, p, pg_mblen(p));
	}

	return buf.data;
}
1565 1566 1567 1568

/*
 * Map SQL value to XML value; see SQL/XML:2003 section 9.16.
 */
1569
char *
1570 1571 1572 1573 1574 1575
map_sql_value_to_xml_value(Datum value, Oid type)
{
	StringInfoData buf;

	initStringInfo(&buf);

1576
	if (type_is_array(type))
1577
	{
Bruce Momjian's avatar
Bruce Momjian committed
1578 1579 1580 1581 1582
		ArrayType  *array;
		Oid			elmtype;
		int16		elmlen;
		bool		elmbyval;
		char		elmalign;
Tom Lane's avatar
Tom Lane committed
1583 1584 1585
		int			num_elems;
		Datum	   *elem_values;
		bool	   *elem_nulls;
Bruce Momjian's avatar
Bruce Momjian committed
1586
		int			i;
1587 1588 1589 1590 1591

		array = DatumGetArrayTypeP(value);
		elmtype = ARR_ELEMTYPE(array);
		get_typlenbyvalalign(elmtype, &elmlen, &elmbyval, &elmalign);

Tom Lane's avatar
Tom Lane committed
1592 1593 1594 1595
		deconstruct_array(array, elmtype,
						  elmlen, elmbyval, elmalign,
						  &elem_values, &elem_nulls,
						  &num_elems);
1596

Tom Lane's avatar
Tom Lane committed
1597 1598 1599 1600
		for (i = 0; i < num_elems; i++)
		{
			if (elem_nulls[i])
				continue;
1601
			appendStringInfoString(&buf, "<element>");
Tom Lane's avatar
Tom Lane committed
1602 1603 1604
			appendStringInfoString(&buf,
								   map_sql_value_to_xml_value(elem_values[i],
															  elmtype));
1605 1606
			appendStringInfoString(&buf, "</element>");
		}
Tom Lane's avatar
Tom Lane committed
1607 1608 1609

		pfree(elem_values);
		pfree(elem_nulls);
1610 1611 1612
	}
	else
	{
Bruce Momjian's avatar
Bruce Momjian committed
1613 1614 1615 1616
		Oid			typeOut;
		bool		isvarlena;
		char	   *p,
				   *str;
1617

1618 1619 1620 1621
		/*
		 * Special XSD formatting for some data types
		 */
		switch (type)
1622
		{
1623 1624 1625 1626 1627 1628 1629
			case BOOLOID:
				if (DatumGetBool(value))
					return "true";
				else
					return "false";

			case DATEOID:
Bruce Momjian's avatar
Bruce Momjian committed
1630 1631 1632 1633
				{
					DateADT		date;
					struct pg_tm tm;
					char		buf[MAXDATELEN + 1];
1634

Bruce Momjian's avatar
Bruce Momjian committed
1635 1636 1637 1638
					date = DatumGetDateADT(value);
					j2date(date + POSTGRES_EPOCH_JDATE,
						   &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
					EncodeDateOnly(&tm, USE_XSD_DATES, buf);
1639

Bruce Momjian's avatar
Bruce Momjian committed
1640 1641
					return pstrdup(buf);
				}
1642 1643

			case TIMESTAMPOID:
Bruce Momjian's avatar
Bruce Momjian committed
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
				{
					Timestamp	timestamp;
					struct pg_tm tm;
					fsec_t		fsec;
					char	   *tzn = NULL;
					char		buf[MAXDATELEN + 1];

					timestamp = DatumGetTimestamp(value);

					/* XSD doesn't support infinite values */
					if (TIMESTAMP_NOT_FINITE(timestamp))
						ereport(ERROR,
								(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
								 errmsg("timestamp out of range")));
					else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
						EncodeDateTime(&tm, fsec, NULL, &tzn, USE_XSD_DATES, buf);
					else
						ereport(ERROR,
								(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
								 errmsg("timestamp out of range")));

					return pstrdup(buf);
				}
1667 1668

			case TIMESTAMPTZOID:
Bruce Momjian's avatar
Bruce Momjian committed
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692
				{
					TimestampTz timestamp;
					struct pg_tm tm;
					int			tz;
					fsec_t		fsec;
					char	   *tzn = NULL;
					char		buf[MAXDATELEN + 1];

					timestamp = DatumGetTimestamp(value);

					/* XSD doesn't support infinite values */
					if (TIMESTAMP_NOT_FINITE(timestamp))
						ereport(ERROR,
								(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
								 errmsg("timestamp out of range")));
					else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
						EncodeDateTime(&tm, fsec, &tz, &tzn, USE_XSD_DATES, buf);
					else
						ereport(ERROR,
								(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
								 errmsg("timestamp out of range")));

					return pstrdup(buf);
				}
1693 1694
		}

1695 1696 1697 1698 1699 1700
		getTypeOutputInfo(type, &typeOut, &isvarlena);
		str = OidOutputFunctionCall(typeOut, value);

		if (type == XMLOID)
			return str;

1701 1702 1703 1704 1705
#ifdef USE_LIBXML
		if (type == BYTEAOID)
		{
			xmlBufferPtr buf;
			xmlTextWriterPtr writer;
Bruce Momjian's avatar
Bruce Momjian committed
1706
			char	   *result;
1707

Tom Lane's avatar
Tom Lane committed
1708 1709
			xml_init();

1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722
			buf = xmlBufferCreate();
			writer = xmlNewTextWriterMemory(buf, 0);

			if (xmlbinary == XMLBINARY_BASE64)
				xmlTextWriterWriteBase64(writer, VARDATA(value), 0, VARSIZE(value) - VARHDRSZ);
			else
				xmlTextWriterWriteBinHex(writer, VARDATA(value), 0, VARSIZE(value) - VARHDRSZ);

			xmlFreeTextWriter(writer);
			result = pstrdup((const char *) xmlBufferContent(buf));
			xmlBufferFree(buf);
			return result;
		}
Bruce Momjian's avatar
Bruce Momjian committed
1723
#endif   /* USE_LIBXML */
1724

1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
		for (p = str; *p; p += pg_mblen(p))
		{
			switch (*p)
			{
				case '&':
					appendStringInfo(&buf, "&amp;");
					break;
				case '<':
					appendStringInfo(&buf, "&lt;");
					break;
				case '>':
					appendStringInfo(&buf, "&gt;");
					break;
				case '\r':
					appendStringInfo(&buf, "&#x0d;");
					break;
				default:
					appendBinaryStringInfo(&buf, p, pg_mblen(p));
					break;
			}
		}
	}

	return buf.data;
}
1750 1751 1752 1753 1754


static char *
_SPI_strdup(const char *s)
{
1755 1756
	size_t		len = strlen(s) + 1;
	char	   *ret = SPI_palloc(len);
Bruce Momjian's avatar
Bruce Momjian committed
1757

1758
	memcpy(ret, s, len);
1759 1760 1761 1762
	return ret;
}


1763 1764 1765 1766
/*
 * SQL to XML mapping functions
 *
 * What follows below is intentionally organized so that you can read
Bruce Momjian's avatar
Bruce Momjian committed
1767
 * along in the SQL/XML:2003 standard.	The functions are mostly split
1768 1769 1770 1771 1772 1773 1774 1775 1776
 * up and ordered they way the clauses lay out in the standards
 * document, and the identifiers are also aligned with the standard
 * text.  (SQL/XML:2006 appears to be ordered differently,
 * unfortunately.)
 *
 * There are many things going on there:
 *
 * There are two kinds of mappings: Mapping SQL data (table contents)
 * to XML documents, and mapping SQL structure (the "schema") to XML
Bruce Momjian's avatar
Bruce Momjian committed
1777
 * Schema.	And there are functions that do both at the same time.
1778 1779 1780 1781 1782
 *
 * Then you can map a database, a schema, or a table, each in both
 * ways.  This breaks down recursively: Mapping a database invokes
 * mapping schemas, which invokes mapping tables, which invokes
 * mapping rows, which invokes mapping columns, although you can't
Bruce Momjian's avatar
Bruce Momjian committed
1783
 * call the last two from the outside.	Because of this, there are a
1784 1785 1786 1787 1788 1789 1790 1791
 * number of xyz_internal() functions which are to be called both from
 * the function manager wrapper and from some upper layer in a
 * recursive call.
 *
 * See the documentation about what the common function arguments
 * nulls, tableforest, and targetns mean.
 *
 * Some style guidelines for XML output: Use double quotes for quoting
Bruce Momjian's avatar
Bruce Momjian committed
1792
 * XML attributes.	Indent XML elements by two spaces, but remember
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817
 * that a lot of code is called recursively at different levels, so
 * it's better not to indent rather than create output that indents
 * and outdents weirdly.  Add newlines to make the output look nice.
 */


/*
 * Visibility of objects for XML mappings; see SQL/XML:2003 section
 * 4.8.5.
 */

/*
 * Given a query, which must return type oid as first column, produce
 * a list of Oids with the query results.
 */
static List *
query_to_oid_list(const char *query)
{
	int			i;
	List	   *list = NIL;

	SPI_execute(query, true, 0);

	for (i = 0; i < SPI_processed; i++)
	{
Bruce Momjian's avatar
Bruce Momjian committed
1818 1819
		Datum		oid;
		bool		isnull;
1820

Tom Lane's avatar
Tom Lane committed
1821 1822 1823 1824 1825 1826
		oid = SPI_getbinval(SPI_tuptable->vals[i],
							SPI_tuptable->tupdesc,
							1,
							&isnull);
		if (!isnull)
			list = lappend_oid(list, DatumGetObjectId(oid));
1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
	}

	return list;
}


static List *
schema_get_xml_visible_tables(Oid nspid)
{
	StringInfoData query;

	initStringInfo(&query);
1839
	appendStringInfo(&query, "SELECT oid FROM pg_catalog.pg_class WHERE relnamespace = %u AND relkind IN ('r', 'v') AND pg_catalog.has_table_privilege (oid, 'SELECT') ORDER BY relname;", nspid);
1840 1841 1842 1843 1844

	return query_to_oid_list(query.data);
}


Bruce Momjian's avatar
Bruce Momjian committed
1845
/*
1846 1847 1848
 * Including the system schemas is probably not useful for a database
 * mapping.
 */
Tom Lane's avatar
Tom Lane committed
1849
#define XML_VISIBLE_SCHEMAS_EXCLUDE "(nspname ~ '^pg_' OR nspname = 'information_schema')"
1850

Tom Lane's avatar
Tom Lane committed
1851
#define XML_VISIBLE_SCHEMAS "SELECT oid FROM pg_catalog.pg_namespace WHERE pg_catalog.has_schema_privilege (oid, 'USAGE') AND NOT " XML_VISIBLE_SCHEMAS_EXCLUDE
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864


static List *
database_get_xml_visible_schemas(void)
{
	return query_to_oid_list(XML_VISIBLE_SCHEMAS " ORDER BY nspname;");
}


static List *
database_get_xml_visible_tables(void)
{
	/* At the moment there is no order required here. */
1865
	return query_to_oid_list("SELECT oid FROM pg_catalog.pg_class WHERE relkind IN ('r', 'v') AND pg_catalog.has_table_privilege (pg_class.oid, 'SELECT') AND relnamespace IN (" XML_VISIBLE_SCHEMAS ");");
1866 1867 1868
}


1869 1870 1871 1872 1873
/*
 * Map SQL table to XML and/or XML Schema document; see SQL/XML:2003
 * section 9.3.
 */

1874
static StringInfo
Tom Lane's avatar
Tom Lane committed
1875 1876 1877
table_to_xml_internal(Oid relid,
					  const char *xmlschema, bool nulls, bool tableforest,
					  const char *targetns, bool top_level)
1878 1879 1880 1881
{
	StringInfoData query;

	initStringInfo(&query);
Tom Lane's avatar
Tom Lane committed
1882 1883
	appendStringInfo(&query, "SELECT * FROM %s",
					 DatumGetCString(DirectFunctionCall1(regclassout,
Bruce Momjian's avatar
Bruce Momjian committed
1884
												  ObjectIdGetDatum(relid))));
Tom Lane's avatar
Tom Lane committed
1885 1886 1887
	return query_to_xml_internal(query.data, get_rel_name(relid),
								 xmlschema, nulls, tableforest,
								 targetns, top_level);
1888 1889 1890
}


1891 1892 1893 1894 1895 1896
Datum
table_to_xml(PG_FUNCTION_ARGS)
{
	Oid			relid = PG_GETARG_OID(0);
	bool		nulls = PG_GETARG_BOOL(1);
	bool		tableforest = PG_GETARG_BOOL(2);
1897
	const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
1898

Tom Lane's avatar
Tom Lane committed
1899
	PG_RETURN_XML_P(stringinfo_to_xmltype(table_to_xml_internal(relid, NULL,
Bruce Momjian's avatar
Bruce Momjian committed
1900 1901
														  nulls, tableforest,
														   targetns, true)));
1902 1903 1904 1905 1906 1907
}


Datum
query_to_xml(PG_FUNCTION_ARGS)
{
1908
	char	   *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
1909 1910
	bool		nulls = PG_GETARG_BOOL(1);
	bool		tableforest = PG_GETARG_BOOL(2);
1911
	const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
1912

Tom Lane's avatar
Tom Lane committed
1913 1914
	PG_RETURN_XML_P(stringinfo_to_xmltype(query_to_xml_internal(query, NULL,
													NULL, nulls, tableforest,
Bruce Momjian's avatar
Bruce Momjian committed
1915
														   targetns, true)));
1916 1917 1918 1919 1920 1921
}


Datum
cursor_to_xml(PG_FUNCTION_ARGS)
{
1922
	char	   *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
1923 1924 1925
	int32		count = PG_GETARG_INT32(1);
	bool		nulls = PG_GETARG_BOOL(2);
	bool		tableforest = PG_GETARG_BOOL(3);
1926
	const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(4));
1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942

	StringInfoData result;
	Portal		portal;
	int			i;

	initStringInfo(&result);

	SPI_connect();
	portal = SPI_cursor_find(name);
	if (portal == NULL)
		ereport(ERROR,
				(errcode(ERRCODE_UNDEFINED_CURSOR),
				 errmsg("cursor \"%s\" does not exist", name)));

	SPI_cursor_fetch(portal, true, count);
	for (i = 0; i < SPI_processed; i++)
Tom Lane's avatar
Tom Lane committed
1943 1944
		SPI_sql_row_to_xmlelement(i, &result, NULL, nulls,
								  tableforest, targetns, true);
1945 1946 1947 1948 1949 1950 1951

	SPI_finish();

	PG_RETURN_XML_P(stringinfo_to_xmltype(&result));
}


1952 1953 1954 1955
/*
 * Write the start tag of the root element of a data mapping.
 *
 * top_level means that this is the very top level of the eventual
Bruce Momjian's avatar
Bruce Momjian committed
1956
 * output.	For example, when the user calls table_to_xml, then a call
1957 1958 1959 1960
 * with a table name to this function is the top level.  When the user
 * calls database_to_xml, then a call with a schema name to this
 * function is not the top level.  If top_level is false, then the XML
 * namespace declarations are omitted, because they supposedly already
Bruce Momjian's avatar
Bruce Momjian committed
1961
 * appeared earlier in the output.	Repeating them is not wrong, but
1962
 * it looks ugly.
Tom Lane's avatar
Tom Lane committed
1963
 */
1964
static void
Tom Lane's avatar
Tom Lane committed
1965 1966 1967
xmldata_root_element_start(StringInfo result, const char *eltname,
						   const char *xmlschema, const char *targetns,
						   bool top_level)
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
{
	/* This isn't really wrong but currently makes no sense. */
	Assert(top_level || !xmlschema);

	appendStringInfo(result, "<%s", eltname);
	if (top_level)
	{
		appendStringInfoString(result, " xmlns:xsi=\"" NAMESPACE_XSI "\"");
		if (strlen(targetns) > 0)
			appendStringInfo(result, " xmlns=\"%s\"", targetns);
	}
	if (xmlschema)
	{
		/* FIXME: better targets */
		if (strlen(targetns) > 0)
			appendStringInfo(result, " xsi:schemaLocation=\"%s #\"", targetns);
		else
			appendStringInfo(result, " xsi:noNamespaceSchemaLocation=\"#\"");
	}
	appendStringInfo(result, ">\n\n");
}


static void
xmldata_root_element_end(StringInfo result, const char *eltname)
{
	appendStringInfo(result, "</%s>\n", eltname);
}


1998
static StringInfo
Tom Lane's avatar
Tom Lane committed
1999 2000 2001
query_to_xml_internal(const char *query, char *tablename,
					  const char *xmlschema, bool nulls, bool tableforest,
					  const char *targetns, bool top_level)
2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
{
	StringInfo	result;
	char	   *xmltn;
	int			i;

	if (tablename)
		xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
	else
		xmltn = "table";

	result = makeStringInfo();

	SPI_connect();
	if (SPI_execute(query, true, 0) != SPI_OK_SELECT)
		ereport(ERROR,
				(errcode(ERRCODE_DATA_EXCEPTION),
				 errmsg("invalid query")));

	if (!tableforest)
Tom Lane's avatar
Tom Lane committed
2021 2022
		xmldata_root_element_start(result, xmltn, xmlschema,
								   targetns, top_level);
2023 2024 2025 2026

	if (xmlschema)
		appendStringInfo(result, "%s\n\n", xmlschema);

Bruce Momjian's avatar
Bruce Momjian committed
2027
	for (i = 0; i < SPI_processed; i++)
Tom Lane's avatar
Tom Lane committed
2028 2029
		SPI_sql_row_to_xmlelement(i, result, tablename, nulls,
								  tableforest, targetns, top_level);
2030 2031

	if (!tableforest)
2032
		xmldata_root_element_end(result, xmltn);
2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045

	SPI_finish();

	return result;
}


Datum
table_to_xmlschema(PG_FUNCTION_ARGS)
{
	Oid			relid = PG_GETARG_OID(0);
	bool		nulls = PG_GETARG_BOOL(1);
	bool		tableforest = PG_GETARG_BOOL(2);
2046
	const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2047
	const char *result;
Bruce Momjian's avatar
Bruce Momjian committed
2048
	Relation	rel;
2049 2050

	rel = heap_open(relid, AccessShareLock);
Tom Lane's avatar
Tom Lane committed
2051 2052
	result = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
										tableforest, targetns);
2053 2054 2055 2056 2057 2058 2059 2060 2061
	heap_close(rel, NoLock);

	PG_RETURN_XML_P(cstring_to_xmltype(result));
}


Datum
query_to_xmlschema(PG_FUNCTION_ARGS)
{
2062
	char	   *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2063 2064
	bool		nulls = PG_GETARG_BOOL(1);
	bool		tableforest = PG_GETARG_BOOL(2);
2065
	const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2066
	const char *result;
2067
	SPIPlanPtr	plan;
2068 2069 2070
	Portal		portal;

	SPI_connect();
2071 2072 2073 2074 2075 2076 2077

	if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
		elog(ERROR, "SPI_prepare(\"%s\") failed", query);

	if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
		elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);

Tom Lane's avatar
Tom Lane committed
2078 2079 2080
	result = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
													InvalidOid, nulls,
													tableforest, targetns));
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090
	SPI_cursor_close(portal);
	SPI_finish();

	PG_RETURN_XML_P(cstring_to_xmltype(result));
}


Datum
cursor_to_xmlschema(PG_FUNCTION_ARGS)
{
2091
	char	   *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
2092 2093
	bool		nulls = PG_GETARG_BOOL(1);
	bool		tableforest = PG_GETARG_BOOL(2);
2094
	const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104
	const char *xmlschema;
	Portal		portal;

	SPI_connect();
	portal = SPI_cursor_find(name);
	if (portal == NULL)
		ereport(ERROR,
				(errcode(ERRCODE_UNDEFINED_CURSOR),
				 errmsg("cursor \"%s\" does not exist", name)));

Tom Lane's avatar
Tom Lane committed
2105 2106
	xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
													   InvalidOid, nulls,
Bruce Momjian's avatar
Bruce Momjian committed
2107
													 tableforest, targetns));
2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119
	SPI_finish();

	PG_RETURN_XML_P(cstring_to_xmltype(xmlschema));
}


Datum
table_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
{
	Oid			relid = PG_GETARG_OID(0);
	bool		nulls = PG_GETARG_BOOL(1);
	bool		tableforest = PG_GETARG_BOOL(2);
2120
	const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2121 2122 2123 2124
	Relation	rel;
	const char *xmlschema;

	rel = heap_open(relid, AccessShareLock);
Tom Lane's avatar
Tom Lane committed
2125 2126
	xmlschema = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
										   tableforest, targetns);
2127 2128
	heap_close(rel, NoLock);

Tom Lane's avatar
Tom Lane committed
2129
	PG_RETURN_XML_P(stringinfo_to_xmltype(table_to_xml_internal(relid,
Bruce Momjian's avatar
Bruce Momjian committed
2130 2131
											   xmlschema, nulls, tableforest,
														   targetns, true)));
2132 2133 2134 2135 2136 2137
}


Datum
query_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
{
2138
	char	   *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2139 2140
	bool		nulls = PG_GETARG_BOOL(1);
	bool		tableforest = PG_GETARG_BOOL(2);
2141
	const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2142 2143

	const char *xmlschema;
2144
	SPIPlanPtr	plan;
2145 2146 2147
	Portal		portal;

	SPI_connect();
2148 2149 2150 2151 2152 2153 2154

	if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
		elog(ERROR, "SPI_prepare(\"%s\") failed", query);

	if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
		elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);

Tom Lane's avatar
Tom Lane committed
2155
	xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
Bruce Momjian's avatar
Bruce Momjian committed
2156
								  InvalidOid, nulls, tableforest, targetns));
2157 2158 2159
	SPI_cursor_close(portal);
	SPI_finish();

Tom Lane's avatar
Tom Lane committed
2160
	PG_RETURN_XML_P(stringinfo_to_xmltype(query_to_xml_internal(query, NULL,
Bruce Momjian's avatar
Bruce Momjian committed
2161 2162
											   xmlschema, nulls, tableforest,
														   targetns, true)));
2163 2164 2165 2166 2167 2168 2169 2170 2171
}


/*
 * Map SQL schema to XML and/or XML Schema document; see SQL/XML:2003
 * section 9.4.
 */

static StringInfo
Tom Lane's avatar
Tom Lane committed
2172 2173
schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls,
					   bool tableforest, const char *targetns, bool top_level)
2174 2175 2176 2177 2178 2179
{
	StringInfo	result;
	char	   *xmlsn;
	List	   *relid_list;
	ListCell   *cell;

Tom Lane's avatar
Tom Lane committed
2180 2181
	xmlsn = map_sql_identifier_to_xml_name(get_namespace_name(nspid),
										   true, false);
2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196
	result = makeStringInfo();

	xmldata_root_element_start(result, xmlsn, xmlschema, targetns, top_level);

	if (xmlschema)
		appendStringInfo(result, "%s\n\n", xmlschema);

	SPI_connect();

	relid_list = schema_get_xml_visible_tables(nspid);

	SPI_push();

	foreach(cell, relid_list)
	{
Bruce Momjian's avatar
Bruce Momjian committed
2197 2198
		Oid			relid = lfirst_oid(cell);
		StringInfo	subres;
2199

Tom Lane's avatar
Tom Lane committed
2200 2201
		subres = table_to_xml_internal(relid, NULL, nulls, tableforest,
									   targetns, false);
2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221

		appendStringInfoString(result, subres->data);
		appendStringInfoChar(result, '\n');
	}

	SPI_pop();
	SPI_finish();

	xmldata_root_element_end(result, xmlsn);

	return result;
}


Datum
schema_to_xml(PG_FUNCTION_ARGS)
{
	Name		name = PG_GETARG_NAME(0);
	bool		nulls = PG_GETARG_BOOL(1);
	bool		tableforest = PG_GETARG_BOOL(2);
2222
	const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2223 2224 2225 2226 2227 2228 2229

	char	   *schemaname;
	Oid			nspid;

	schemaname = NameStr(*name);
	nspid = LookupExplicitNamespace(schemaname);

Tom Lane's avatar
Tom Lane committed
2230
	PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xml_internal(nspid, NULL,
Bruce Momjian's avatar
Bruce Momjian committed
2231
									   nulls, tableforest, targetns, true)));
2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257
}


/*
 * Write the start element of the root element of an XML Schema mapping.
 */
static void
xsd_schema_element_start(StringInfo result, const char *targetns)
{
	appendStringInfoString(result,
						   "<xsd:schema\n"
						   "    xmlns:xsd=\"" NAMESPACE_XSD "\"");
	if (strlen(targetns) > 0)
		appendStringInfo(result,
						 "\n"
						 "    targetNamespace=\"%s\"\n"
						 "    elementFormDefault=\"qualified\"",
						 targetns);
	appendStringInfoString(result,
						   ">\n\n");
}


static void
xsd_schema_element_end(StringInfo result)
{
Tom Lane's avatar
Tom Lane committed
2258
	appendStringInfoString(result, "</xsd:schema>");
2259 2260 2261 2262
}


static StringInfo
Tom Lane's avatar
Tom Lane committed
2263 2264
schema_to_xmlschema_internal(const char *schemaname, bool nulls,
							 bool tableforest, const char *targetns)
2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282
{
	Oid			nspid;
	List	   *relid_list;
	List	   *tupdesc_list;
	ListCell   *cell;
	StringInfo	result;

	result = makeStringInfo();

	nspid = LookupExplicitNamespace(schemaname);

	xsd_schema_element_start(result, targetns);

	SPI_connect();

	relid_list = schema_get_xml_visible_tables(nspid);

	tupdesc_list = NIL;
Bruce Momjian's avatar
Bruce Momjian committed
2283
	foreach(cell, relid_list)
2284
	{
Bruce Momjian's avatar
Bruce Momjian committed
2285
		Relation	rel;
2286 2287

		rel = heap_open(lfirst_oid(cell), AccessShareLock);
Tom Lane's avatar
Tom Lane committed
2288
		tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2289 2290 2291 2292 2293 2294 2295
		heap_close(rel, NoLock);
	}

	appendStringInfoString(result,
						   map_sql_typecoll_to_xmlschema_types(tupdesc_list));

	appendStringInfoString(result,
Bruce Momjian's avatar
Bruce Momjian committed
2296 2297
						 map_sql_schema_to_xmlschema_types(nspid, relid_list,
											  nulls, tableforest, targetns));
2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312

	xsd_schema_element_end(result);

	SPI_finish();

	return result;
}


Datum
schema_to_xmlschema(PG_FUNCTION_ARGS)
{
	Name		name = PG_GETARG_NAME(0);
	bool		nulls = PG_GETARG_BOOL(1);
	bool		tableforest = PG_GETARG_BOOL(2);
2313
	const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2314

Tom Lane's avatar
Tom Lane committed
2315
	PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xmlschema_internal(NameStr(*name),
Bruce Momjian's avatar
Bruce Momjian committed
2316
											 nulls, tableforest, targetns)));
2317 2318 2319 2320 2321 2322 2323 2324 2325
}


Datum
schema_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
{
	Name		name = PG_GETARG_NAME(0);
	bool		nulls = PG_GETARG_BOOL(1);
	bool		tableforest = PG_GETARG_BOOL(2);
2326
	const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2327 2328 2329 2330 2331 2332 2333
	char	   *schemaname;
	Oid			nspid;
	StringInfo	xmlschema;

	schemaname = NameStr(*name);
	nspid = LookupExplicitNamespace(schemaname);

Tom Lane's avatar
Tom Lane committed
2334 2335
	xmlschema = schema_to_xmlschema_internal(schemaname, nulls,
											 tableforest, targetns);
2336

Tom Lane's avatar
Tom Lane committed
2337
	PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xml_internal(nspid,
Bruce Momjian's avatar
Bruce Momjian committed
2338 2339
													  xmlschema->data, nulls,
											  tableforest, targetns, true)));
2340 2341 2342 2343 2344 2345 2346 2347 2348
}


/*
 * Map SQL database to XML and/or XML Schema document; see SQL/XML:2003
 * section 9.5.
 */

static StringInfo
Tom Lane's avatar
Tom Lane committed
2349 2350
database_to_xml_internal(const char *xmlschema, bool nulls,
						 bool tableforest, const char *targetns)
2351 2352 2353 2354 2355 2356
{
	StringInfo	result;
	List	   *nspid_list;
	ListCell   *cell;
	char	   *xmlcn;

Tom Lane's avatar
Tom Lane committed
2357 2358
	xmlcn = map_sql_identifier_to_xml_name(get_database_name(MyDatabaseId),
										   true, false);
2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373
	result = makeStringInfo();

	xmldata_root_element_start(result, xmlcn, xmlschema, targetns, true);

	if (xmlschema)
		appendStringInfo(result, "%s\n\n", xmlschema);

	SPI_connect();

	nspid_list = database_get_xml_visible_schemas();

	SPI_push();

	foreach(cell, nspid_list)
	{
Bruce Momjian's avatar
Bruce Momjian committed
2374 2375
		Oid			nspid = lfirst_oid(cell);
		StringInfo	subres;
2376

Tom Lane's avatar
Tom Lane committed
2377 2378
		subres = schema_to_xml_internal(nspid, NULL, nulls,
										tableforest, targetns, false);
2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397

		appendStringInfoString(result, subres->data);
		appendStringInfoChar(result, '\n');
	}

	SPI_pop();
	SPI_finish();

	xmldata_root_element_end(result, xmlcn);

	return result;
}


Datum
database_to_xml(PG_FUNCTION_ARGS)
{
	bool		nulls = PG_GETARG_BOOL(0);
	bool		tableforest = PG_GETARG_BOOL(1);
2398
	const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
2399

Tom Lane's avatar
Tom Lane committed
2400
	PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xml_internal(NULL, nulls,
Bruce Momjian's avatar
Bruce Momjian committed
2401
													tableforest, targetns)));
2402 2403 2404 2405
}


static StringInfo
Tom Lane's avatar
Tom Lane committed
2406 2407
database_to_xmlschema_internal(bool nulls, bool tableforest,
							   const char *targetns)
2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424
{
	List	   *relid_list;
	List	   *nspid_list;
	List	   *tupdesc_list;
	ListCell   *cell;
	StringInfo	result;

	result = makeStringInfo();

	xsd_schema_element_start(result, targetns);

	SPI_connect();

	relid_list = database_get_xml_visible_tables();
	nspid_list = database_get_xml_visible_schemas();

	tupdesc_list = NIL;
Bruce Momjian's avatar
Bruce Momjian committed
2425
	foreach(cell, relid_list)
2426
	{
Bruce Momjian's avatar
Bruce Momjian committed
2427
		Relation	rel;
2428 2429

		rel = heap_open(lfirst_oid(cell), AccessShareLock);
Tom Lane's avatar
Tom Lane committed
2430
		tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452
		heap_close(rel, NoLock);
	}

	appendStringInfoString(result,
						   map_sql_typecoll_to_xmlschema_types(tupdesc_list));

	appendStringInfoString(result,
						   map_sql_catalog_to_xmlschema_types(nspid_list, nulls, tableforest, targetns));

	xsd_schema_element_end(result);

	SPI_finish();

	return result;
}


Datum
database_to_xmlschema(PG_FUNCTION_ARGS)
{
	bool		nulls = PG_GETARG_BOOL(0);
	bool		tableforest = PG_GETARG_BOOL(1);
2453
	const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
2454

Tom Lane's avatar
Tom Lane committed
2455
	PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xmlschema_internal(nulls,
Bruce Momjian's avatar
Bruce Momjian committed
2456
													tableforest, targetns)));
2457 2458 2459 2460 2461 2462 2463 2464
}


Datum
database_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
{
	bool		nulls = PG_GETARG_BOOL(0);
	bool		tableforest = PG_GETARG_BOOL(1);
2465
	const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
2466 2467 2468 2469
	StringInfo	xmlschema;

	xmlschema = database_to_xmlschema_internal(nulls, tableforest, targetns);

Tom Lane's avatar
Tom Lane committed
2470
	PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xml_internal(xmlschema->data,
Bruce Momjian's avatar
Bruce Momjian committed
2471
											 nulls, tableforest, targetns)));
2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486
}


/*
 * Map a multi-part SQL name to an XML name; see SQL/XML:2003 section
 * 9.2.
 */
static char *
map_multipart_sql_identifier_to_xml_name(char *a, char *b, char *c, char *d)
{
	StringInfoData result;

	initStringInfo(&result);

	if (a)
Tom Lane's avatar
Tom Lane committed
2487 2488
		appendStringInfo(&result, "%s",
						 map_sql_identifier_to_xml_name(a, true, true));
2489
	if (b)
Tom Lane's avatar
Tom Lane committed
2490 2491
		appendStringInfo(&result, ".%s",
						 map_sql_identifier_to_xml_name(b, true, true));
2492
	if (c)
Tom Lane's avatar
Tom Lane committed
2493 2494
		appendStringInfo(&result, ".%s",
						 map_sql_identifier_to_xml_name(c, true, true));
2495
	if (d)
Tom Lane's avatar
Tom Lane committed
2496 2497
		appendStringInfo(&result, ".%s",
						 map_sql_identifier_to_xml_name(d, true, true));
2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510

	return result.data;
}


/*
 * Map an SQL table to an XML Schema document; see SQL/XML:2003
 * section 9.3.
 *
 * Map an SQL table to XML Schema data types; see SQL/XML:2003 section
 * 9.6.
 */
static const char *
Tom Lane's avatar
Tom Lane committed
2511 2512
map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls,
						   bool tableforest, const char *targetns)
2513 2514 2515 2516 2517 2518 2519 2520 2521
{
	int			i;
	char	   *xmltn;
	char	   *tabletypename;
	char	   *rowtypename;
	StringInfoData result;

	initStringInfo(&result);

Tom Lane's avatar
Tom Lane committed
2522
	if (OidIsValid(relid))
2523
	{
Bruce Momjian's avatar
Bruce Momjian committed
2524
		HeapTuple	tuple;
Tom Lane's avatar
Tom Lane committed
2525 2526 2527 2528 2529 2530 2531 2532
		Form_pg_class reltuple;

		tuple = SearchSysCache(RELOID,
							   ObjectIdGetDatum(relid),
							   0, 0, 0);
		if (!HeapTupleIsValid(tuple))
			elog(ERROR, "cache lookup failed for relation %u", relid);
		reltuple = (Form_pg_class) GETSTRUCT(tuple);
2533

Tom Lane's avatar
Tom Lane committed
2534 2535
		xmltn = map_sql_identifier_to_xml_name(NameStr(reltuple->relname),
											   true, false);
2536 2537

		tabletypename = map_multipart_sql_identifier_to_xml_name("TableType",
Bruce Momjian's avatar
Bruce Momjian committed
2538 2539 2540
											 get_database_name(MyDatabaseId),
								  get_namespace_name(reltuple->relnamespace),
												 NameStr(reltuple->relname));
2541 2542

		rowtypename = map_multipart_sql_identifier_to_xml_name("RowType",
Bruce Momjian's avatar
Bruce Momjian committed
2543 2544 2545
											 get_database_name(MyDatabaseId),
								  get_namespace_name(reltuple->relnamespace),
												 NameStr(reltuple->relname));
2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559

		ReleaseSysCache(tuple);
	}
	else
	{
		if (tableforest)
			xmltn = "row";
		else
			xmltn = "table";

		tabletypename = "TableType";
		rowtypename = "RowType";
	}

2560
	xsd_schema_element_start(&result, targetns);
2561 2562

	appendStringInfoString(&result,
Bruce Momjian's avatar
Bruce Momjian committed
2563
				   map_sql_typecoll_to_xmlschema_types(list_make1(tupdesc)));
2564 2565 2566 2567 2568 2569 2570 2571

	appendStringInfo(&result,
					 "<xsd:complexType name=\"%s\">\n"
					 "  <xsd:sequence>\n",
					 rowtypename);

	for (i = 0; i < tupdesc->natts; i++)
		appendStringInfo(&result,
Bruce Momjian's avatar
Bruce Momjian committed
2572 2573 2574 2575
			   "    <xsd:element name=\"%s\" type=\"%s\"%s></xsd:element>\n",
		  map_sql_identifier_to_xml_name(NameStr(tupdesc->attrs[i]->attname),
										 true, false),
				   map_sql_type_to_xml_name(tupdesc->attrs[i]->atttypid, -1),
2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600
						 nulls ? " nillable=\"true\"" : " minOccurs=\"0\"");

	appendStringInfoString(&result,
						   "  </xsd:sequence>\n"
						   "</xsd:complexType>\n\n");

	if (!tableforest)
	{
		appendStringInfo(&result,
						 "<xsd:complexType name=\"%s\">\n"
						 "  <xsd:sequence>\n"
						 "    <xsd:element name=\"row\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n"
						 "  </xsd:sequence>\n"
						 "</xsd:complexType>\n\n",
						 tabletypename, rowtypename);

		appendStringInfo(&result,
						 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
						 xmltn, tabletypename);
	}
	else
		appendStringInfo(&result,
						 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
						 xmltn, rowtypename);

2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611
	xsd_schema_element_end(&result);

	return result.data;
}


/*
 * Map an SQL schema to XML Schema data types; see SQL/XML section
 * 9.7.
 */
static const char *
Tom Lane's avatar
Tom Lane committed
2612 2613
map_sql_schema_to_xmlschema_types(Oid nspid, List *relid_list, bool nulls,
								  bool tableforest, const char *targetns)
2614
{
Tom Lane's avatar
Tom Lane committed
2615 2616
	char	   *dbname;
	char	   *nspname;
2617 2618 2619 2620 2621
	char	   *xmlsn;
	char	   *schematypename;
	StringInfoData result;
	ListCell   *cell;

Tom Lane's avatar
Tom Lane committed
2622 2623 2624
	dbname = get_database_name(MyDatabaseId);
	nspname = get_namespace_name(nspid);

2625 2626
	initStringInfo(&result);

Tom Lane's avatar
Tom Lane committed
2627
	xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
2628 2629

	schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
Tom Lane's avatar
Tom Lane committed
2630 2631
															  dbname,
															  nspname,
2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642
															  NULL);

	appendStringInfo(&result,
					 "<xsd:complexType name=\"%s\">\n", schematypename);
	if (!tableforest)
		appendStringInfoString(&result,
							   "  <xsd:all>\n");
	else
		appendStringInfoString(&result,
							   "  <xsd:sequence>\n");

Bruce Momjian's avatar
Bruce Momjian committed
2643
	foreach(cell, relid_list)
2644
	{
Bruce Momjian's avatar
Bruce Momjian committed
2645 2646 2647 2648 2649 2650 2651
		Oid			relid = lfirst_oid(cell);
		char	   *relname = get_rel_name(relid);
		char	   *xmltn = map_sql_identifier_to_xml_name(relname, true, false);
		char	   *tabletypename = map_multipart_sql_identifier_to_xml_name(tableforest ? "RowType" : "TableType",
																	  dbname,
																	 nspname,
																	relname);
2652 2653 2654

		if (!tableforest)
			appendStringInfo(&result,
2655
							 "    <xsd:element name=\"%s\" type=\"%s\"/>\n",
2656 2657 2658
							 xmltn, tabletypename);
		else
			appendStringInfo(&result,
2659
							 "    <xsd:element name=\"%s\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n",
2660 2661 2662 2663 2664 2665 2666 2667 2668
							 xmltn, tabletypename);
	}

	if (!tableforest)
		appendStringInfoString(&result,
							   "  </xsd:all>\n");
	else
		appendStringInfoString(&result,
							   "  </xsd:sequence>\n");
2669
	appendStringInfoString(&result,
2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684
						   "</xsd:complexType>\n\n");

	appendStringInfo(&result,
					 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
					 xmlsn, schematypename);

	return result.data;
}


/*
 * Map an SQL catalog to XML Schema data types; see SQL/XML section
 * 9.8.
 */
static const char *
Tom Lane's avatar
Tom Lane committed
2685 2686
map_sql_catalog_to_xmlschema_types(List *nspid_list, bool nulls,
								   bool tableforest, const char *targetns)
2687
{
Tom Lane's avatar
Tom Lane committed
2688
	char	   *dbname;
2689 2690 2691 2692 2693
	char	   *xmlcn;
	char	   *catalogtypename;
	StringInfoData result;
	ListCell   *cell;

Tom Lane's avatar
Tom Lane committed
2694 2695
	dbname = get_database_name(MyDatabaseId);

2696 2697
	initStringInfo(&result);

Tom Lane's avatar
Tom Lane committed
2698
	xmlcn = map_sql_identifier_to_xml_name(dbname, true, false);
2699 2700

	catalogtypename = map_multipart_sql_identifier_to_xml_name("CatalogType",
Tom Lane's avatar
Tom Lane committed
2701
															   dbname,
2702 2703 2704 2705 2706 2707 2708 2709
															   NULL,
															   NULL);

	appendStringInfo(&result,
					 "<xsd:complexType name=\"%s\">\n", catalogtypename);
	appendStringInfoString(&result,
						   "  <xsd:all>\n");

Bruce Momjian's avatar
Bruce Momjian committed
2710
	foreach(cell, nspid_list)
2711
	{
Bruce Momjian's avatar
Bruce Momjian committed
2712
		Oid			nspid = lfirst_oid(cell);
Tom Lane's avatar
Tom Lane committed
2713
		char	   *nspname = get_namespace_name(nspid);
Bruce Momjian's avatar
Bruce Momjian committed
2714 2715 2716 2717 2718
		char	   *xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
		char	   *schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
																	  dbname,
																	 nspname,
																	   NULL);
2719 2720

		appendStringInfo(&result,
2721
						 "    <xsd:element name=\"%s\" type=\"%s\"/>\n",
2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732
						 xmlsn, schematypename);
	}

	appendStringInfoString(&result,
						   "  </xsd:all>\n");
	appendStringInfoString(&result,
						   "</xsd:complexType>\n\n");

	appendStringInfo(&result,
					 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
					 xmlcn, catalogtypename);
2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747

	return result.data;
}


/*
 * Map an SQL data type to an XML name; see SQL/XML:2003 section 9.9.
 */
static const char *
map_sql_type_to_xml_name(Oid typeoid, int typmod)
{
	StringInfoData result;

	initStringInfo(&result);

Bruce Momjian's avatar
Bruce Momjian committed
2748
	switch (typeoid)
2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818
	{
		case BPCHAROID:
			if (typmod == -1)
				appendStringInfo(&result, "CHAR");
			else
				appendStringInfo(&result, "CHAR_%d", typmod - VARHDRSZ);
			break;
		case VARCHAROID:
			if (typmod == -1)
				appendStringInfo(&result, "VARCHAR");
			else
				appendStringInfo(&result, "VARCHAR_%d", typmod - VARHDRSZ);
			break;
		case NUMERICOID:
			if (typmod == -1)
				appendStringInfo(&result, "NUMERIC");
			else
				appendStringInfo(&result, "NUMERIC_%d_%d",
								 ((typmod - VARHDRSZ) >> 16) & 0xffff,
								 (typmod - VARHDRSZ) & 0xffff);
			break;
		case INT4OID:
			appendStringInfo(&result, "INTEGER");
			break;
		case INT2OID:
			appendStringInfo(&result, "SMALLINT");
			break;
		case INT8OID:
			appendStringInfo(&result, "BIGINT");
			break;
		case FLOAT4OID:
			appendStringInfo(&result, "REAL");
			break;
		case FLOAT8OID:
			appendStringInfo(&result, "DOUBLE");
			break;
		case BOOLOID:
			appendStringInfo(&result, "BOOLEAN");
			break;
		case TIMEOID:
			if (typmod == -1)
				appendStringInfo(&result, "TIME");
			else
				appendStringInfo(&result, "TIME_%d", typmod);
			break;
		case TIMETZOID:
			if (typmod == -1)
				appendStringInfo(&result, "TIME_WTZ");
			else
				appendStringInfo(&result, "TIME_WTZ_%d", typmod);
			break;
		case TIMESTAMPOID:
			if (typmod == -1)
				appendStringInfo(&result, "TIMESTAMP");
			else
				appendStringInfo(&result, "TIMESTAMP_%d", typmod);
			break;
		case TIMESTAMPTZOID:
			if (typmod == -1)
				appendStringInfo(&result, "TIMESTAMP_WTZ");
			else
				appendStringInfo(&result, "TIMESTAMP_WTZ_%d", typmod);
			break;
		case DATEOID:
			appendStringInfo(&result, "DATE");
			break;
		case XMLOID:
			appendStringInfo(&result, "XML");
			break;
		default:
Bruce Momjian's avatar
Bruce Momjian committed
2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837
			{
				HeapTuple	tuple;
				Form_pg_type typtuple;

				tuple = SearchSysCache(TYPEOID,
									   ObjectIdGetDatum(typeoid),
									   0, 0, 0);
				if (!HeapTupleIsValid(tuple))
					elog(ERROR, "cache lookup failed for type %u", typeoid);
				typtuple = (Form_pg_type) GETSTRUCT(tuple);

				appendStringInfoString(&result,
									   map_multipart_sql_identifier_to_xml_name((typtuple->typtype == TYPTYPE_DOMAIN) ? "Domain" : "UDT",
											 get_database_name(MyDatabaseId),
								  get_namespace_name(typtuple->typnamespace),
												NameStr(typtuple->typname)));

				ReleaseSysCache(tuple);
			}
2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848
	}

	return result.data;
}


/*
 * Map a collection of SQL data types to XML Schema data types; see
 * SQL/XML:2002 section 9.10.
 */
static const char *
2849
map_sql_typecoll_to_xmlschema_types(List *tupdesc_list)
2850
{
2851 2852
	List	   *uniquetypes = NIL;
	int			i;
2853
	StringInfoData result;
2854
	ListCell   *cell0;
2855

2856 2857
	/* extract all column types used in the set of TupleDescs */
	foreach(cell0, tupdesc_list)
2858
	{
Bruce Momjian's avatar
Bruce Momjian committed
2859
		TupleDesc	tupdesc = (TupleDesc) lfirst(cell0);
2860

2861
		for (i = 0; i < tupdesc->natts; i++)
2862
		{
2863
			if (tupdesc->attrs[i]->attisdropped)
2864
				continue;
2865 2866
			uniquetypes = list_append_unique_oid(uniquetypes,
												 tupdesc->attrs[i]->atttypid);
2867
		}
2868 2869 2870
	}

	/* add base types of domains */
2871
	foreach(cell0, uniquetypes)
2872
	{
Bruce Momjian's avatar
Bruce Momjian committed
2873 2874
		Oid			typid = lfirst_oid(cell0);
		Oid			basetypid = getBaseType(typid);
2875

2876 2877
		if (basetypid != typid)
			uniquetypes = list_append_unique_oid(uniquetypes, basetypid);
2878 2879
	}

2880
	/* Convert to textual form */
2881 2882
	initStringInfo(&result);

2883 2884 2885 2886 2887 2888
	foreach(cell0, uniquetypes)
	{
		appendStringInfo(&result, "%s\n",
						 map_sql_type_to_xmlschema_type(lfirst_oid(cell0),
														-1));
	}
2889 2890 2891 2892 2893 2894 2895 2896 2897 2898

	return result.data;
}


/*
 * Map an SQL data type to a named XML Schema data type; see SQL/XML
 * sections 9.11 and 9.15.
 *
 * (The distinction between 9.11 and 9.15 is basically that 9.15 adds
Bruce Momjian's avatar
Bruce Momjian committed
2899
 * a name attribute, which this function does.	The name-less version
2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923
 * 9.11 doesn't appear to be required anywhere.)
 */
static const char *
map_sql_type_to_xmlschema_type(Oid typeoid, int typmod)
{
	StringInfoData result;
	const char *typename = map_sql_type_to_xml_name(typeoid, typmod);

	initStringInfo(&result);

	if (typeoid == XMLOID)
	{
		appendStringInfo(&result,
						 "<xsd:complexType mixed=\"true\">\n"
						 "  <xsd:sequence>\n"
						 "    <xsd:any name=\"element\" minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>\n"
						 "  </xsd:sequence>\n"
						 "</xsd:complexType>\n");
	}
	else
	{
		appendStringInfo(&result,
						 "<xsd:simpleType name=\"%s\">\n", typename);

Bruce Momjian's avatar
Bruce Momjian committed
2924
		switch (typeoid)
2925 2926 2927 2928
		{
			case BPCHAROID:
			case VARCHAROID:
			case TEXTOID:
2929 2930
				appendStringInfo(&result,
								 "  <xsd:restriction base=\"xsd:string\">\n");
2931 2932
				if (typmod != -1)
					appendStringInfo(&result,
2933
									 "    <xsd:maxLength value=\"%d\"/>\n",
2934
									 typmod - VARHDRSZ);
2935 2936
				appendStringInfo(&result,
								 "  </xsd:restriction>\n");
2937 2938 2939 2940 2941 2942
				break;

			case BYTEAOID:
				appendStringInfo(&result,
								 "  <xsd:restriction base=\"xsd:%s\">\n"
								 "  </xsd:restriction>\n",
Bruce Momjian's avatar
Bruce Momjian committed
2943
				xmlbinary == XMLBINARY_BASE64 ? "base64Binary" : "hexBinary");
2944
				break;
2945 2946 2947 2948

			case NUMERICOID:
				if (typmod != -1)
					appendStringInfo(&result,
Bruce Momjian's avatar
Bruce Momjian committed
2949
								 "  <xsd:restriction base=\"xsd:decimal\">\n"
2950
									 "    <xsd:totalDigits value=\"%d\"/>\n"
Bruce Momjian's avatar
Bruce Momjian committed
2951
								   "    <xsd:fractionDigits value=\"%d\"/>\n"
2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967
									 "  </xsd:restriction>\n",
									 ((typmod - VARHDRSZ) >> 16) & 0xffff,
									 (typmod - VARHDRSZ) & 0xffff);
				break;

			case INT2OID:
				appendStringInfo(&result,
								 "  <xsd:restriction base=\"xsd:short\">\n"
								 "    <xsd:maxInclusive value=\"%d\"/>\n"
								 "    <xsd:minInclusive value=\"%d\"/>\n"
								 "  </xsd:restriction>\n",
								 SHRT_MAX, SHRT_MIN);
				break;

			case INT4OID:
				appendStringInfo(&result,
2968
								 "  <xsd:restriction base=\"xsd:int\">\n"
2969 2970 2971 2972 2973 2974 2975 2976 2977
								 "    <xsd:maxInclusive value=\"%d\"/>\n"
								 "    <xsd:minInclusive value=\"%d\"/>\n"
								 "  </xsd:restriction>\n",
								 INT_MAX, INT_MIN);
				break;

			case INT8OID:
				appendStringInfo(&result,
								 "  <xsd:restriction base=\"xsd:long\">\n"
Bruce Momjian's avatar
Bruce Momjian committed
2978 2979
					   "    <xsd:maxInclusive value=\"" INT64_FORMAT "\"/>\n"
					   "    <xsd:minInclusive value=\"" INT64_FORMAT "\"/>\n"
2980
								 "  </xsd:restriction>\n",
Bruce Momjian's avatar
Bruce Momjian committed
2981
							   (((uint64) 1) << (sizeof(int64) * 8 - 1)) - 1,
2982
								 (((uint64) 1) << (sizeof(int64) * 8 - 1)));
2983 2984 2985 2986
				break;

			case FLOAT4OID:
				appendStringInfo(&result,
Bruce Momjian's avatar
Bruce Momjian committed
2987
				"  <xsd:restriction base=\"xsd:float\"></xsd:restriction>\n");
2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001
				break;

			case FLOAT8OID:
				appendStringInfo(&result,
								 "  <xsd:restriction base=\"xsd:double\"></xsd:restriction>\n");
				break;

			case BOOLOID:
				appendStringInfo(&result,
								 "  <xsd:restriction base=\"xsd:boolean\"></xsd:restriction>\n");
				break;

			case TIMEOID:
			case TIMETZOID:
Bruce Momjian's avatar
Bruce Momjian committed
3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021
				{
					const char *tz = (typeoid == TIMETZOID ? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");

					if (typmod == -1)
						appendStringInfo(&result,
									"  <xsd:restriction base=\"xsd:time\">\n"
										 "    <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}(.\\p{Nd}+)?%s\"/>\n"
										 "  </xsd:restriction>\n", tz);
					else if (typmod == 0)
						appendStringInfo(&result,
									"  <xsd:restriction base=\"xsd:time\">\n"
										 "    <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
										 "  </xsd:restriction>\n", tz);
					else
						appendStringInfo(&result,
									"  <xsd:restriction base=\"xsd:time\">\n"
										 "    <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}.\\p{Nd}{%d}%s\"/>\n"
							"  </xsd:restriction>\n", typmod - VARHDRSZ, tz);
					break;
				}
3022 3023 3024

			case TIMESTAMPOID:
			case TIMESTAMPTZOID:
Bruce Momjian's avatar
Bruce Momjian committed
3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044
				{
					const char *tz = (typeoid == TIMESTAMPTZOID ? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");

					if (typmod == -1)
						appendStringInfo(&result,
								"  <xsd:restriction base=\"xsd:dateTime\">\n"
										 "    <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"
										 "  </xsd:restriction>\n", tz);
					else if (typmod == 0)
						appendStringInfo(&result,
								"  <xsd:restriction base=\"xsd:dateTime\">\n"
										 "    <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
										 "  </xsd:restriction>\n", tz);
					else
						appendStringInfo(&result,
								"  <xsd:restriction base=\"xsd:dateTime\">\n"
										 "    <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"
							"  </xsd:restriction>\n", typmod - VARHDRSZ, tz);
					break;
				}
3045 3046 3047 3048 3049 3050

			case DATEOID:
				appendStringInfo(&result,
								 "  <xsd:restriction base=\"xsd:date\">\n"
								 "    <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}\"/>\n"
								 "  </xsd:restriction>\n");
Tom Lane's avatar
Tom Lane committed
3051
				break;
3052 3053

			default:
3054
				if (get_typtype(typeoid) == TYPTYPE_DOMAIN)
3055
				{
Bruce Momjian's avatar
Bruce Momjian committed
3056 3057
					Oid			base_typeoid;
					int32		base_typmod = -1;
3058 3059 3060 3061

					base_typeoid = getBaseTypeAndTypmod(typeoid, &base_typmod);

					appendStringInfo(&result,
3062
									 "  <xsd:restriction base=\"%s\"/>\n",
Bruce Momjian's avatar
Bruce Momjian committed
3063
						map_sql_type_to_xml_name(base_typeoid, base_typmod));
3064
				}
Tom Lane's avatar
Tom Lane committed
3065
				break;
3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076
		}
		appendStringInfo(&result,
						 "</xsd:simpleType>\n");
	}

	return result.data;
}


/*
 * Map an SQL row to an XML element, taking the row from the active
Bruce Momjian's avatar
Bruce Momjian committed
3077
 * SPI cursor.	See also SQL/XML:2003 section 9.12.
3078 3079
 */
static void
Tom Lane's avatar
Tom Lane committed
3080 3081 3082
SPI_sql_row_to_xmlelement(int rownum, StringInfo result, char *tablename,
						  bool nulls, bool tableforest,
						  const char *targetns, bool top_level)
3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097
{
	int			i;
	char	   *xmltn;

	if (tablename)
		xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
	else
	{
		if (tableforest)
			xmltn = "row";
		else
			xmltn = "table";
	}

	if (tableforest)
3098
		xmldata_root_element_start(result, xmltn, NULL, targetns, top_level);
3099 3100 3101
	else
		appendStringInfoString(result, "<row>\n");

Bruce Momjian's avatar
Bruce Momjian committed
3102
	for (i = 1; i <= SPI_tuptable->tupdesc->natts; i++)
3103
	{
Bruce Momjian's avatar
Bruce Momjian committed
3104 3105 3106
		char	   *colname;
		Datum		colval;
		bool		isnull;
3107

Tom Lane's avatar
Tom Lane committed
3108 3109 3110 3111 3112 3113
		colname = map_sql_identifier_to_xml_name(SPI_fname(SPI_tuptable->tupdesc, i),
												 true, false);
		colval = SPI_getbinval(SPI_tuptable->vals[rownum],
							   SPI_tuptable->tupdesc,
							   i,
							   &isnull);
3114 3115 3116
		if (isnull)
		{
			if (nulls)
3117
				appendStringInfo(result, "  <%s xsi:nil=\"true\"/>\n", colname);
3118 3119 3120
		}
		else
			appendStringInfo(result, "  <%s>%s</%s>\n",
Tom Lane's avatar
Tom Lane committed
3121 3122
							 colname,
							 map_sql_value_to_xml_value(colval,
Bruce Momjian's avatar
Bruce Momjian committed
3123
									SPI_gettypeid(SPI_tuptable->tupdesc, i)),
3124 3125 3126 3127
							 colname);
	}

	if (tableforest)
3128 3129 3130 3131
	{
		xmldata_root_element_end(result, xmltn);
		appendStringInfoChar(result, '\n');
	}
3132 3133 3134
	else
		appendStringInfoString(result, "</row>\n\n");
}
3135 3136 3137 3138 3139 3140 3141


/*
 * XPath related functions
 */

#ifdef USE_LIBXML
Bruce Momjian's avatar
Bruce Momjian committed
3142
/*
Tom Lane's avatar
Tom Lane committed
3143 3144
 * Convert XML node to text (dump subtree in case of element,
 * return value otherwise)
3145
 */
Tom Lane's avatar
Tom Lane committed
3146
static text *
3147
xml_xmlnodetoxmltype(xmlNodePtr cur)
3148
{
Bruce Momjian's avatar
Bruce Momjian committed
3149 3150 3151 3152
	xmlChar    *str;
	xmltype    *result;
	xmlBufferPtr buf;

3153 3154 3155 3156 3157 3158 3159 3160 3161 3162
	if (cur->type == XML_ELEMENT_NODE)
	{
		buf = xmlBufferCreate();
		xmlNodeDump(buf, NULL, cur, 0, 1);
		result = xmlBuffer_to_xmltype(buf);
		xmlBufferFree(buf);
	}
	else
	{
		str = xmlXPathCastNodeToString(cur);
3163
		result = (xmltype *) cstring_to_text((char *) str);
3164
	}
Bruce Momjian's avatar
Bruce Momjian committed
3165

3166 3167 3168 3169
	return result;
}
#endif

Peter Eisentraut's avatar
Peter Eisentraut committed
3170

3171 3172
/*
 * Evaluate XPath expression and return array of XML values.
Tom Lane's avatar
Tom Lane committed
3173 3174
 *
 * As we have no support of XQuery sequences yet, this function seems
3175
 * to be the most useful one (array of XML functions plays a role of
Tom Lane's avatar
Tom Lane committed
3176
 * some kind of substitution for XQuery sequences).
Peter Eisentraut's avatar
Peter Eisentraut committed
3177
 *
3178 3179 3180 3181
 * Workaround here: we parse XML data in different way to allow XPath for
 * fragments (see "XPath for fragment" TODO comment inside).
 */
Datum
Peter Eisentraut's avatar
Peter Eisentraut committed
3182
xpath(PG_FUNCTION_ARGS)
3183 3184
{
#ifdef USE_LIBXML
Peter Eisentraut's avatar
Peter Eisentraut committed
3185
	text	   *xpath_expr_text = PG_GETARG_TEXT_P(0);
Bruce Momjian's avatar
Bruce Momjian committed
3186
	xmltype    *data = PG_GETARG_XML_P(1);
Peter Eisentraut's avatar
Peter Eisentraut committed
3187
	ArrayType  *namespaces = PG_GETARG_ARRAYTYPE_P(2);
Bruce Momjian's avatar
Bruce Momjian committed
3188
	ArrayBuildState *astate = NULL;
3189 3190 3191 3192 3193
	xmlParserCtxtPtr ctxt;
	xmlDocPtr	doc;
	xmlXPathContextPtr xpathctx;
	xmlXPathCompExprPtr xpathcomp;
	xmlXPathObjectPtr xpathobj;
Tom Lane's avatar
Tom Lane committed
3194
	char	   *datastr;
Peter Eisentraut's avatar
Peter Eisentraut committed
3195 3196
	int32		len;
	int32		xpath_len;
Bruce Momjian's avatar
Bruce Momjian committed
3197 3198
	xmlChar    *string;
	xmlChar    *xpath_expr;
Peter Eisentraut's avatar
Peter Eisentraut committed
3199 3200 3201
	int			i;
	int			res_nitems;
	int			ndim;
Tom Lane's avatar
Tom Lane committed
3202 3203
	Datum	   *ns_names_uris;
	bool	   *ns_names_uris_nulls;
Peter Eisentraut's avatar
Peter Eisentraut committed
3204 3205 3206
	int			ns_count;

	/*
Bruce Momjian's avatar
Bruce Momjian committed
3207 3208 3209 3210 3211 3212 3213
	 * Namespace mappings are passed as text[].  If an empty array is passed
	 * (ndim = 0, "0-dimensional"), then there are no namespace mappings.
	 * Else, a 2-dimensional array with length of the second axis being equal
	 * to 2 should be passed, i.e., every subarray contains 2 elements, the
	 * first element defining the name, the second one the URI.  Example:
	 * ARRAY[ARRAY['myns', 'http://example.com'], ARRAY['myns2',
	 * 'http://example2.com']].
3214
	 */
Peter Eisentraut's avatar
Peter Eisentraut committed
3215 3216
	ndim = ARR_NDIM(namespaces);
	if (ndim != 0)
3217
	{
Peter Eisentraut's avatar
Peter Eisentraut committed
3218 3219
		int		   *dims;

3220
		dims = ARR_DIMS(namespaces);
Peter Eisentraut's avatar
Peter Eisentraut committed
3221 3222

		if (ndim != 2 || dims[1] != 2)
Tom Lane's avatar
Tom Lane committed
3223 3224 3225 3226
			ereport(ERROR,
					(errcode(ERRCODE_DATA_EXCEPTION),
					 errmsg("invalid array for XML namespace mapping"),
					 errdetail("The array must be two-dimensional with length of the second axis equal to 2.")));
Peter Eisentraut's avatar
Peter Eisentraut committed
3227

3228
		Assert(ARR_ELEMTYPE(namespaces) == TEXTOID);
Peter Eisentraut's avatar
Peter Eisentraut committed
3229

Tom Lane's avatar
Tom Lane committed
3230 3231 3232
		deconstruct_array(namespaces, TEXTOID, -1, false, 'i',
						  &ns_names_uris, &ns_names_uris_nulls,
						  &ns_count);
Peter Eisentraut's avatar
Peter Eisentraut committed
3233

Bruce Momjian's avatar
Bruce Momjian committed
3234
		Assert((ns_count % 2) == 0);	/* checked above */
Tom Lane's avatar
Tom Lane committed
3235
		ns_count /= 2;			/* count pairs only */
3236
	}
Peter Eisentraut's avatar
Peter Eisentraut committed
3237 3238
	else
	{
Tom Lane's avatar
Tom Lane committed
3239 3240
		ns_names_uris = NULL;
		ns_names_uris_nulls = NULL;
Peter Eisentraut's avatar
Peter Eisentraut committed
3241 3242 3243
		ns_count = 0;
	}

Tom Lane's avatar
Tom Lane committed
3244
	datastr = VARDATA(data);
3245 3246 3247
	len = VARSIZE(data) - VARHDRSZ;
	xpath_len = VARSIZE(xpath_expr_text) - VARHDRSZ;
	if (xpath_len == 0)
Tom Lane's avatar
Tom Lane committed
3248 3249 3250
		ereport(ERROR,
				(errcode(ERRCODE_DATA_EXCEPTION),
				 errmsg("empty XPath expression")));
Peter Eisentraut's avatar
Peter Eisentraut committed
3251

3252 3253
	xml_init();

Peter Eisentraut's avatar
Peter Eisentraut committed
3254
	/*
Bruce Momjian's avatar
Bruce Momjian committed
3255 3256 3257 3258
	 * To handle both documents and fragments, regardless of the fact whether
	 * the XML datum has a single root (XML well-formedness), we wrap the XML
	 * datum in a dummy element (<x>...</x>) and extend the XPath expression
	 * accordingly.  To do it, throw away the XML prolog, if any.
Peter Eisentraut's avatar
Peter Eisentraut committed
3259
	 */
Tom Lane's avatar
Tom Lane committed
3260 3261
	if (len >= 5 &&
		xmlStrncmp((xmlChar *) datastr, (xmlChar *) "<?xml", 5) == 0)
3262
	{
Peter Eisentraut's avatar
Peter Eisentraut committed
3263
		i = 5;
Tom Lane's avatar
Tom Lane committed
3264 3265
		while (i < len &&
			   !(datastr[i - 1] == '?' && datastr[i] == '>'))
Peter Eisentraut's avatar
Peter Eisentraut committed
3266 3267 3268 3269 3270 3271 3272
			i++;

		if (i == len)
			xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
						"could not parse XML data");

		++i;
3273 3274 3275

		datastr += i;
		len -= i;
3276
	}
Peter Eisentraut's avatar
Peter Eisentraut committed
3277

3278 3279 3280 3281
	string = (xmlChar *) palloc((len + 8) * sizeof(xmlChar));
	memcpy(string, "<x>", 3);
	memcpy(string + 3, datastr, len);
	memcpy(string + 3 + len, "</x>", 5);
Peter Eisentraut's avatar
Peter Eisentraut committed
3282 3283
	len += 7;

3284 3285 3286 3287 3288
	xpath_expr = (xmlChar *) palloc((xpath_len + 3) * sizeof(xmlChar));
	memcpy(xpath_expr, "/x", 2);
	memcpy(xpath_expr + 2, VARDATA(xpath_expr_text), xpath_len);
	xpath_expr[xpath_len + 2] = '\0';
	xpath_len += 2;
3289

3290
	xmlInitParser();
Bruce Momjian's avatar
Bruce Momjian committed
3291

3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316
	/*
	 * redundant XML parsing (two parsings for the same value during one
	 * command execution are possible)
	 */
	ctxt = xmlNewParserCtxt();
	if (ctxt == NULL)
		xml_ereport(ERROR, ERRCODE_OUT_OF_MEMORY,
					"could not allocate parser context");
	doc = xmlCtxtReadMemory(ctxt, (char *) string, len, NULL, NULL, 0);
	if (doc == NULL)
		xml_ereport(ERROR, ERRCODE_INVALID_XML_DOCUMENT,
					"could not parse XML data");
	xpathctx = xmlXPathNewContext(doc);
	if (xpathctx == NULL)
		xml_ereport(ERROR, ERRCODE_OUT_OF_MEMORY,
					"could not allocate XPath context");
	xpathctx->node = xmlDocGetRootElement(doc);
	if (xpathctx->node == NULL)
		xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
					"could not find root XML element");

	/* register namespaces, if any */
	if (ns_count > 0)
	{
		for (i = 0; i < ns_count; i++)
Tom Lane's avatar
Tom Lane committed
3317
		{
3318 3319 3320 3321 3322 3323 3324 3325
			char	   *ns_name;
			char	   *ns_uri;

			if (ns_names_uris_nulls[i * 2] ||
				ns_names_uris_nulls[i * 2 + 1])
				ereport(ERROR,
						(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
						 errmsg("neither namespace name nor URI may be null")));
3326 3327
			ns_name = TextDatumGetCString(ns_names_uris[i * 2]);
			ns_uri = TextDatumGetCString(ns_names_uris[i * 2 + 1]);
3328 3329 3330 3331 3332 3333
			if (xmlXPathRegisterNs(xpathctx,
								   (xmlChar *) ns_name,
								   (xmlChar *) ns_uri) != 0)
				ereport(ERROR,		/* is this an internal error??? */
						(errmsg("could not register XML namespace with name \"%s\" and URI \"%s\"",
								ns_name, ns_uri)));
Tom Lane's avatar
Tom Lane committed
3334
		}
3335
	}
Peter Eisentraut's avatar
Peter Eisentraut committed
3336

3337 3338 3339 3340
	xpathcomp = xmlXPathCompile(xpath_expr);
	if (xpathcomp == NULL)	/* TODO: show proper XPath error details */
		xml_ereport(ERROR, ERRCODE_INTERNAL_ERROR,
					"invalid XPath expression");
3341

3342 3343 3344 3345
	xpathobj = xmlXPathCompiledEval(xpathcomp, xpathctx);
	if (xpathobj == NULL)	/* TODO: reason? */
		ereport(ERROR,
				(errmsg("could not create XPath object")));
Peter Eisentraut's avatar
Peter Eisentraut committed
3346

3347
	xmlXPathFreeCompExpr(xpathcomp);
Bruce Momjian's avatar
Bruce Momjian committed
3348

3349 3350 3351 3352 3353
	/* return empty array in cases when nothing is found */
	if (xpathobj->nodesetval == NULL)
		res_nitems = 0;
	else
		res_nitems = xpathobj->nodesetval->nodeNr;
Peter Eisentraut's avatar
Peter Eisentraut committed
3354

3355
	if (res_nitems)
3356
	{
3357 3358 3359 3360
		for (i = 0; i < xpathobj->nodesetval->nodeNr; i++)
		{
			Datum		elem;
			bool		elemisnull = false;
3361

3362 3363 3364 3365 3366
			elem = PointerGetDatum(xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[i]));
			astate = accumArrayResult(astate, elem,
									  elemisnull, XMLOID,
									  CurrentMemoryContext);
		}
3367
	}
3368 3369 3370 3371 3372

	xmlXPathFreeObject(xpathobj);
	xmlXPathFreeContext(xpathctx);
	xmlFreeDoc(doc);
	xmlFreeParserCtxt(ctxt);
Peter Eisentraut's avatar
Peter Eisentraut committed
3373 3374 3375

	if (res_nitems == 0)
		PG_RETURN_ARRAYTYPE_P(construct_empty_array(XMLOID));
3376 3377 3378 3379 3380 3381 3382
	else
		PG_RETURN_ARRAYTYPE_P(makeArrayResult(astate, CurrentMemoryContext));
#else
	NO_XML_SUPPORT();
	return 0;
#endif
}