Thanks to visit codestin.com
Credit goes to doxygen.postgresql.org

PostgreSQL Source Code git master
xml.h File Reference
#include "executor/tablefunc.h"
#include "fmgr.h"
#include "nodes/execnodes.h"
#include "nodes/primnodes.h"
Include dependency graph for xml.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define PG_GETARG_XML_P(n)   DatumGetXmlP(PG_GETARG_DATUM(n))
 
#define PG_RETURN_XML_P(x)   PG_RETURN_POINTER(x)
 

Typedefs

typedef struct varlena xmltype
 
typedef struct PgXmlErrorContext PgXmlErrorContext
 

Enumerations

enum  XmlStandaloneType { XML_STANDALONE_YES , XML_STANDALONE_NO , XML_STANDALONE_NO_VALUE , XML_STANDALONE_OMITTED }
 
enum  XmlBinaryType { XMLBINARY_BASE64 , XMLBINARY_HEX }
 
enum  PgXmlStrictness { PG_XML_STRICTNESS_LEGACY , PG_XML_STRICTNESS_WELLFORMED , PG_XML_STRICTNESS_ALL }
 

Functions

static xmltypeDatumGetXmlP (Datum X)
 
static Datum XmlPGetDatum (const xmltype *X)
 
void pg_xml_init_library (void)
 
PgXmlErrorContextpg_xml_init (PgXmlStrictness strictness)
 
void pg_xml_done (PgXmlErrorContext *errcxt, bool isError)
 
bool pg_xml_error_occurred (PgXmlErrorContext *errcxt)
 
void xml_ereport (PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
 
xmltypexmlconcat (List *args)
 
xmltypexmlelement (XmlExpr *xexpr, Datum *named_argvalue, bool *named_argnull, Datum *argvalue, bool *argnull)
 
xmltypexmlparse (text *data, XmlOptionType xmloption_arg, bool preserve_whitespace)
 
xmltypexmlpi (const char *target, text *arg, bool arg_is_null, bool *result_is_null)
 
xmltypexmlroot (xmltype *data, text *version, int standalone)
 
bool xml_is_document (xmltype *arg)
 
textxmltotext_with_options (xmltype *data, XmlOptionType xmloption_arg, bool indent)
 
char * escape_xml (const char *str)
 
char * map_sql_identifier_to_xml_name (const char *ident, bool fully_escaped, bool escape_period)
 
char * map_xml_name_to_sql_identifier (const char *name)
 
char * map_sql_value_to_xml_value (Datum value, Oid type, bool xml_escape_strings)
 

Variables

PGDLLIMPORT int xmlbinary
 
PGDLLIMPORT int xmloption
 
PGDLLIMPORT const TableFuncRoutine XmlTableRoutine
 

Macro Definition Documentation

◆ PG_GETARG_XML_P

#define PG_GETARG_XML_P (   n)    DatumGetXmlP(PG_GETARG_DATUM(n))

Definition at line 62 of file xml.h.

◆ PG_RETURN_XML_P

#define PG_RETURN_XML_P (   x)    PG_RETURN_POINTER(x)

Definition at line 63 of file xml.h.

Typedef Documentation

◆ PgXmlErrorContext

Definition at line 48 of file xml.h.

◆ xmltype

typedef struct varlena xmltype

Definition at line 23 of file xml.h.

Enumeration Type Documentation

◆ PgXmlStrictness

Enumerator
PG_XML_STRICTNESS_LEGACY 
PG_XML_STRICTNESS_WELLFORMED 
PG_XML_STRICTNESS_ALL 

Definition at line 39 of file xml.h.

40{
41 PG_XML_STRICTNESS_LEGACY, /* ignore errors unless function result
42 * indicates error condition */
43 PG_XML_STRICTNESS_WELLFORMED, /* ignore non-parser messages */
44 PG_XML_STRICTNESS_ALL, /* report all notices/warnings/errors */
PgXmlStrictness
Definition: xml.h:40
@ PG_XML_STRICTNESS_LEGACY
Definition: xml.h:41
@ PG_XML_STRICTNESS_ALL
Definition: xml.h:44
@ PG_XML_STRICTNESS_WELLFORMED
Definition: xml.h:43

◆ XmlBinaryType

Enumerator
XMLBINARY_BASE64 
XMLBINARY_HEX 

Definition at line 33 of file xml.h.

34{
XmlBinaryType
Definition: xml.h:34
@ XMLBINARY_BASE64
Definition: xml.h:35
@ XMLBINARY_HEX
Definition: xml.h:36

◆ XmlStandaloneType

Enumerator
XML_STANDALONE_YES 
XML_STANDALONE_NO 
XML_STANDALONE_NO_VALUE 
XML_STANDALONE_OMITTED 

Definition at line 25 of file xml.h.

26{
XmlStandaloneType
Definition: xml.h:26
@ XML_STANDALONE_OMITTED
Definition: xml.h:30
@ XML_STANDALONE_NO_VALUE
Definition: xml.h:29
@ XML_STANDALONE_YES
Definition: xml.h:27
@ XML_STANDALONE_NO
Definition: xml.h:28

Function Documentation

◆ DatumGetXmlP()

static xmltype * DatumGetXmlP ( Datum  X)
inlinestatic

Definition at line 51 of file xml.h.

52{
53 return (xmltype *) PG_DETOAST_DATUM(X);
54}
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:240
Definition: c.h:693

References PG_DETOAST_DATUM.

Referenced by ExecEvalXmlExpr(), xmlconcat(), and XmlTableSetDocument().

◆ escape_xml()

char * escape_xml ( const char *  str)

Definition at line 2735 of file xml.c.

2736{
2738 const char *p;
2739
2741 for (p = str; *p; p++)
2742 {
2743 switch (*p)
2744 {
2745 case '&':
2746 appendStringInfoString(&buf, "&");
2747 break;
2748 case '<':
2749 appendStringInfoString(&buf, "&lt;");
2750 break;
2751 case '>':
2752 appendStringInfoString(&buf, "&gt;");
2753 break;
2754 case '\r':
2755 appendStringInfoString(&buf, "&#x0d;");
2756 break;
2757 default:
2759 break;
2760 }
2761 }
2762 return buf.data;
2763}
const char * str
static char * buf
Definition: pg_test_fsync.c:72
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:230
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:231

References appendStringInfoCharMacro, appendStringInfoString(), buf, initStringInfo(), and str.

Referenced by ExplainProperty(), ExplainPropertyList(), map_sql_value_to_xml_value(), and XmlTableGetValue().

◆ map_sql_identifier_to_xml_name()

char * map_sql_identifier_to_xml_name ( const char *  ident,
bool  fully_escaped,
bool  escape_period 
)

Definition at line 2418 of file xml.c.

2420{
2421#ifdef USE_LIBXML
2423 const char *p;
2424
2425 /*
2426 * SQL/XML doesn't make use of this case anywhere, so it's probably a
2427 * mistake.
2428 */
2429 Assert(fully_escaped || !escape_period);
2430
2432
2433 for (p = ident; *p; p += pg_mblen(p))
2434 {
2435 if (*p == ':' && (p == ident || fully_escaped))
2436 appendStringInfoString(&buf, "_x003A_");
2437 else if (*p == '_' && *(p + 1) == 'x')
2438 appendStringInfoString(&buf, "_x005F_");
2439 else if (fully_escaped && p == ident &&
2440 pg_strncasecmp(p, "xml", 3) == 0)
2441 {
2442 if (*p == 'x')
2443 appendStringInfoString(&buf, "_x0078_");
2444 else
2445 appendStringInfoString(&buf, "_x0058_");
2446 }
2447 else if (escape_period && *p == '.')
2448 appendStringInfoString(&buf, "_x002E_");
2449 else
2450 {
2451 pg_wchar u = sqlchar_to_unicode(p);
2452
2453 if ((p == ident)
2454 ? !is_valid_xml_namefirst(u)
2455 : !is_valid_xml_namechar(u))
2456 appendStringInfo(&buf, "_x%04X_", (unsigned int) u);
2457 else
2459 }
2460 }
2461
2462 return buf.data;
2463#else /* not USE_LIBXML */
2465 return NULL;
2466#endif /* not USE_LIBXML */
2467}
Assert(PointerIsAligned(start, uint64))
#define ident
Definition: indent_codes.h:47
unsigned int pg_wchar
Definition: mbprint.c:31
int pg_mblen(const char *mbstr)
Definition: mbutils.c:1024
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:145
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
Definition: stringinfo.c:281
#define NO_XML_SUPPORT()
Definition: xml.c:234

References appendBinaryStringInfo(), appendStringInfo(), appendStringInfoString(), Assert(), buf, ident, initStringInfo(), NO_XML_SUPPORT, pg_mblen(), and pg_strncasecmp().

Referenced by database_to_xml_internal(), map_multipart_sql_identifier_to_xml_name(), map_sql_catalog_to_xmlschema_types(), map_sql_schema_to_xmlschema_types(), map_sql_table_to_xmlschema(), query_to_xml_internal(), schema_to_xml_internal(), SPI_sql_row_to_xmlelement(), and transformXmlExpr().

◆ map_sql_value_to_xml_value()

char * map_sql_value_to_xml_value ( Datum  value,
Oid  type,
bool  xml_escape_strings 
)

Definition at line 2516 of file xml.c.

2517{
2519 {
2520 ArrayType *array;
2521 Oid elmtype;
2522 int16 elmlen;
2523 bool elmbyval;
2524 char elmalign;
2525 int num_elems;
2526 Datum *elem_values;
2527 bool *elem_nulls;
2529 int i;
2530
2531 array = DatumGetArrayTypeP(value);
2532 elmtype = ARR_ELEMTYPE(array);
2533 get_typlenbyvalalign(elmtype, &elmlen, &elmbyval, &elmalign);
2534
2535 deconstruct_array(array, elmtype,
2536 elmlen, elmbyval, elmalign,
2537 &elem_values, &elem_nulls,
2538 &num_elems);
2539
2541
2542 for (i = 0; i < num_elems; i++)
2543 {
2544 if (elem_nulls[i])
2545 continue;
2546 appendStringInfoString(&buf, "<element>");
2548 map_sql_value_to_xml_value(elem_values[i],
2549 elmtype, true));
2550 appendStringInfoString(&buf, "</element>");
2551 }
2552
2553 pfree(elem_values);
2554 pfree(elem_nulls);
2555
2556 return buf.data;
2557 }
2558 else
2559 {
2560 Oid typeOut;
2561 bool isvarlena;
2562 char *str;
2563
2564 /*
2565 * Flatten domains; the special-case treatments below should apply to,
2566 * eg, domains over boolean not just boolean.
2567 */
2569
2570 /*
2571 * Special XSD formatting for some data types
2572 */
2573 switch (type)
2574 {
2575 case BOOLOID:
2576 if (DatumGetBool(value))
2577 return "true";
2578 else
2579 return "false";
2580
2581 case DATEOID:
2582 {
2583 DateADT date;
2584 struct pg_tm tm;
2585 char buf[MAXDATELEN + 1];
2586
2588 /* XSD doesn't support infinite values */
2589 if (DATE_NOT_FINITE(date))
2590 ereport(ERROR,
2591 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2592 errmsg("date out of range"),
2593 errdetail("XML does not support infinite date values.")));
2595 &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
2597
2598 return pstrdup(buf);
2599 }
2600
2601 case TIMESTAMPOID:
2602 {
2604 struct pg_tm tm;
2605 fsec_t fsec;
2606 char buf[MAXDATELEN + 1];
2607
2609
2610 /* XSD doesn't support infinite values */
2612 ereport(ERROR,
2613 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2614 errmsg("timestamp out of range"),
2615 errdetail("XML does not support infinite timestamp values.")));
2616 else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
2617 EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
2618 else
2619 ereport(ERROR,
2620 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2621 errmsg("timestamp out of range")));
2622
2623 return pstrdup(buf);
2624 }
2625
2626 case TIMESTAMPTZOID:
2627 {
2629 struct pg_tm tm;
2630 int tz;
2631 fsec_t fsec;
2632 const char *tzn = NULL;
2633 char buf[MAXDATELEN + 1];
2634
2636
2637 /* XSD doesn't support infinite values */
2639 ereport(ERROR,
2640 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2641 errmsg("timestamp out of range"),
2642 errdetail("XML does not support infinite timestamp values.")));
2643 else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
2644 EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
2645 else
2646 ereport(ERROR,
2647 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2648 errmsg("timestamp out of range")));
2649
2650 return pstrdup(buf);
2651 }
2652
2653#ifdef USE_LIBXML
2654 case BYTEAOID:
2655 {
2656 bytea *bstr = DatumGetByteaPP(value);
2657 PgXmlErrorContext *xmlerrcxt;
2658 volatile xmlBufferPtr buf = NULL;
2659 volatile xmlTextWriterPtr writer = NULL;
2660 char *result;
2661
2663
2664 PG_TRY();
2665 {
2666 buf = xmlBufferCreate();
2667 if (buf == NULL || xmlerrcxt->err_occurred)
2668 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
2669 "could not allocate xmlBuffer");
2670 writer = xmlNewTextWriterMemory(buf, 0);
2671 if (writer == NULL || xmlerrcxt->err_occurred)
2672 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
2673 "could not allocate xmlTextWriter");
2674
2676 xmlTextWriterWriteBase64(writer, VARDATA_ANY(bstr),
2677 0, VARSIZE_ANY_EXHDR(bstr));
2678 else
2679 xmlTextWriterWriteBinHex(writer, VARDATA_ANY(bstr),
2680 0, VARSIZE_ANY_EXHDR(bstr));
2681
2682 /* we MUST do this now to flush data out to the buffer */
2683 xmlFreeTextWriter(writer);
2684 writer = NULL;
2685
2686 result = pstrdup((const char *) xmlBufferContent(buf));
2687 }
2688 PG_CATCH();
2689 {
2690 if (writer)
2691 xmlFreeTextWriter(writer);
2692 if (buf)
2693 xmlBufferFree(buf);
2694
2695 pg_xml_done(xmlerrcxt, true);
2696
2697 PG_RE_THROW();
2698 }
2699 PG_END_TRY();
2700
2701 xmlBufferFree(buf);
2702
2703 pg_xml_done(xmlerrcxt, false);
2704
2705 return result;
2706 }
2707#endif /* USE_LIBXML */
2708
2709 }
2710
2711 /*
2712 * otherwise, just use the type's native text representation
2713 */
2714 getTypeOutputInfo(type, &typeOut, &isvarlena);
2715 str = OidOutputFunctionCall(typeOut, value);
2716
2717 /* ... exactly as-is for XML, and when escaping is not wanted */
2718 if (type == XMLOID || !xml_escape_strings)
2719 return str;
2720
2721 /* otherwise, translate special characters as needed */
2722 return escape_xml(str);
2723 }
2724}
#define DatumGetArrayTypeP(X)
Definition: array.h:261
#define ARR_ELEMTYPE(a)
Definition: array.h:292
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3631
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:321
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:4464
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
Definition: datetime.c:4349
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1910
int16_t int16
Definition: c.h:534
int64 Timestamp
Definition: timestamp.h:38
int64 TimestampTz
Definition: timestamp.h:39
int32 fsec_t
Definition: timestamp.h:41
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:169
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:235
#define DATE_NOT_FINITE(j)
Definition: date.h:43
int32 DateADT
Definition: date.h:23
static DateADT DatumGetDateADT(Datum X)
Definition: date.h:54
int errdetail(const char *fmt,...)
Definition: elog.c:1207
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define PG_RE_THROW()
Definition: elog.h:405
#define PG_TRY(...)
Definition: elog.h:372
#define PG_END_TRY(...)
Definition: elog.h:397
#define ERROR
Definition: elog.h:39
#define PG_CATCH(...)
Definition: elog.h:382
#define ereport(elevel,...)
Definition: elog.h:150
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1762
#define DatumGetByteaPP(X)
Definition: fmgr.h:291
#define MAXDATELEN
Definition: datetime.h:200
static struct @166 value
int i
Definition: isn.c:77
static struct pg_tm tm
Definition: localtime.c:104
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:3074
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2438
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2688
#define type_is_array_domain(typid)
Definition: lsyscache.h:216
char * pstrdup(const char *in)
Definition: mcxt.c:1759
void pfree(void *pointer)
Definition: mcxt.c:1594
#define USE_XSD_DATES
Definition: miscadmin.h:239
long date
Definition: pgtypes_date.h:9
int64 timestamp
static bool DatumGetBool(Datum X)
Definition: postgres.h:100
uint64_t Datum
Definition: postgres.h:70
unsigned int Oid
Definition: postgres_ext.h:32
Definition: pgtime.h:35
int tm_mday
Definition: pgtime.h:39
int tm_mon
Definition: pgtime.h:40
int tm_year
Definition: pgtime.h:41
static Timestamp DatumGetTimestamp(Datum X)
Definition: timestamp.h:28
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition: varatt.h:472
static char * VARDATA_ANY(const void *PTR)
Definition: varatt.h:486
const char * type
int xmlbinary
Definition: xml.c:108
char * map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
Definition: xml.c:2516
char * escape_xml(const char *str)
Definition: xml.c:2735
struct PgXmlErrorContext PgXmlErrorContext
Definition: xml.h:48
PgXmlErrorContext * pg_xml_init(PgXmlStrictness strictness)
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
void pg_xml_done(PgXmlErrorContext *errcxt, bool isError)

References appendStringInfoString(), ARR_ELEMTYPE, buf, DATE_NOT_FINITE, DatumGetArrayTypeP, DatumGetBool(), DatumGetByteaPP, DatumGetDateADT(), DatumGetTimestamp(), deconstruct_array(), EncodeDateOnly(), EncodeDateTime(), ereport, errcode(), errdetail(), errmsg(), ERROR, escape_xml(), get_typlenbyvalalign(), getBaseType(), getTypeOutputInfo(), i, initStringInfo(), j2date(), map_sql_value_to_xml_value(), MAXDATELEN, OidOutputFunctionCall(), pfree(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, pg_xml_done(), pg_xml_init(), PG_XML_STRICTNESS_ALL, POSTGRES_EPOCH_JDATE, pstrdup(), str, timestamp2tm(), TIMESTAMP_NOT_FINITE, tm, pg_tm::tm_mday, pg_tm::tm_mon, pg_tm::tm_year, type, type_is_array_domain, USE_XSD_DATES, value, VARDATA_ANY(), VARSIZE_ANY_EXHDR(), xml_ereport(), xmlbinary, and XMLBINARY_BASE64.

Referenced by ExecEvalXmlExpr(), map_sql_value_to_xml_value(), SPI_sql_row_to_xmlelement(), and xmlelement().

◆ map_xml_name_to_sql_identifier()

char * map_xml_name_to_sql_identifier ( const char *  name)

Definition at line 2474 of file xml.c.

2475{
2477 const char *p;
2478
2480
2481 for (p = name; *p; p += pg_mblen(p))
2482 {
2483 if (*p == '_' && *(p + 1) == 'x'
2484 && isxdigit((unsigned char) *(p + 2))
2485 && isxdigit((unsigned char) *(p + 3))
2486 && isxdigit((unsigned char) *(p + 4))
2487 && isxdigit((unsigned char) *(p + 5))
2488 && *(p + 6) == '_')
2489 {
2490 char cbuf[MAX_UNICODE_EQUIVALENT_STRING + 1];
2491 unsigned int u;
2492
2493 sscanf(p + 2, "%X", &u);
2494 pg_unicode_to_server(u, (unsigned char *) cbuf);
2496 p += 6;
2497 }
2498 else
2500 }
2501
2502 return buf.data;
2503}
void pg_unicode_to_server(pg_wchar c, unsigned char *s)
Definition: mbutils.c:865
#define MAX_UNICODE_EQUIVALENT_STRING
Definition: pg_wchar.h:329
const char * name

References appendBinaryStringInfo(), appendStringInfoString(), buf, initStringInfo(), MAX_UNICODE_EQUIVALENT_STRING, name, pg_mblen(), and pg_unicode_to_server().

Referenced by get_rule_expr().

◆ pg_xml_done()

◆ pg_xml_error_occurred()

◆ pg_xml_init()

◆ pg_xml_init_library()

void pg_xml_init_library ( void  )

◆ xml_ereport()

◆ xml_is_document()

bool xml_is_document ( xmltype arg)

Definition at line 1167 of file xml.c.

1168{
1169#ifdef USE_LIBXML
1170 xmlDocPtr doc;
1171 ErrorSaveContext escontext = {T_ErrorSaveContext};
1172
1173 /*
1174 * We'll report "true" if no soft error is reported by xml_parse().
1175 */
1176 doc = xml_parse((text *) arg, XMLOPTION_DOCUMENT, true,
1177 GetDatabaseEncoding(), NULL, NULL, (Node *) &escontext);
1178 if (doc)
1179 xmlFreeDoc(doc);
1180
1181 return !escontext.error_occurred;
1182#else /* not USE_LIBXML */
1184 return false;
1185#endif /* not USE_LIBXML */
1186}
int GetDatabaseEncoding(void)
Definition: mbutils.c:1262
void * arg
@ XMLOPTION_DOCUMENT
Definition: primnodes.h:1603
bool error_occurred
Definition: miscnodes.h:47
Definition: nodes.h:135

References arg, ErrorSaveContext::error_occurred, GetDatabaseEncoding(), NO_XML_SUPPORT, and XMLOPTION_DOCUMENT.

Referenced by ExecEvalXmlExpr().

◆ xmlconcat()

xmltype * xmlconcat ( List args)

Definition at line 574 of file xml.c.

575{
576#ifdef USE_LIBXML
577 int global_standalone = 1;
578 xmlChar *global_version = NULL;
579 bool global_version_no_value = false;
581 ListCell *v;
582
584 foreach(v, args)
585 {
587 size_t len;
588 xmlChar *version;
589 int standalone;
590 char *str;
591
592 len = VARSIZE(x) - VARHDRSZ;
593 str = text_to_cstring((text *) x);
594
595 parse_xml_decl((xmlChar *) str, &len, &version, NULL, &standalone);
596
597 if (standalone == 0 && global_standalone == 1)
598 global_standalone = 0;
599 if (standalone < 0)
600 global_standalone = -1;
601
602 if (!version)
603 global_version_no_value = true;
604 else if (!global_version)
605 global_version = version;
606 else if (xmlStrcmp(version, global_version) != 0)
607 global_version_no_value = true;
608
610 pfree(str);
611 }
612
613 if (!global_version_no_value || global_standalone >= 0)
614 {
615 StringInfoData buf2;
616
617 initStringInfo(&buf2);
618
619 print_xml_decl(&buf2,
620 (!global_version_no_value) ? global_version : NULL,
621 0,
622 global_standalone);
623
624 appendBinaryStringInfo(&buf2, buf.data, buf.len);
625 buf = buf2;
626 }
627
628 return stringinfo_to_xmltype(&buf);
629#else
631 return NULL;
632#endif
633}
#define VARHDRSZ
Definition: c.h:698
int x
Definition: isn.c:75
const void size_t len
#define lfirst(lc)
Definition: pg_list.h:172
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:332
static Size VARSIZE(const void *PTR)
Definition: varatt.h:298
char * text_to_cstring(const text *t)
Definition: varlena.c:214
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:466
static xmltype * DatumGetXmlP(Datum X)
Definition: xml.h:51

References appendBinaryStringInfo(), appendStringInfoString(), generate_unaccent_rules::args, buf, DatumGetXmlP(), initStringInfo(), len, lfirst, NO_XML_SUPPORT, pfree(), PointerGetDatum(), str, stringinfo_to_xmltype(), text_to_cstring(), VARHDRSZ, VARSIZE(), and x.

Referenced by ExecEvalXmlExpr(), and xmlconcat2().

◆ xmlelement()

xmltype * xmlelement ( XmlExpr xexpr,
Datum named_argvalue,
bool *  named_argnull,
Datum argvalue,
bool *  argnull 
)

Definition at line 893 of file xml.c.

896{
897#ifdef USE_LIBXML
898 xmltype *result;
899 List *named_arg_strings;
900 List *arg_strings;
901 int i;
902 ListCell *arg;
903 ListCell *narg;
904 PgXmlErrorContext *xmlerrcxt;
905 volatile xmlBufferPtr buf = NULL;
906 volatile xmlTextWriterPtr writer = NULL;
907
908 /*
909 * All arguments are already evaluated, and their values are passed in the
910 * named_argvalue/named_argnull or argvalue/argnull arrays. This avoids
911 * issues if one of the arguments involves a call to some other function
912 * or subsystem that wants to use libxml on its own terms. We examine the
913 * original XmlExpr to identify the numbers and types of the arguments.
914 */
915 named_arg_strings = NIL;
916 i = 0;
917 foreach(arg, xexpr->named_args)
918 {
919 Expr *e = (Expr *) lfirst(arg);
920 char *str;
921
922 if (named_argnull[i])
923 str = NULL;
924 else
925 str = map_sql_value_to_xml_value(named_argvalue[i],
926 exprType((Node *) e),
927 false);
928 named_arg_strings = lappend(named_arg_strings, str);
929 i++;
930 }
931
932 arg_strings = NIL;
933 i = 0;
934 foreach(arg, xexpr->args)
935 {
936 Expr *e = (Expr *) lfirst(arg);
937 char *str;
938
939 /* here we can just forget NULL elements immediately */
940 if (!argnull[i])
941 {
942 str = map_sql_value_to_xml_value(argvalue[i],
943 exprType((Node *) e),
944 true);
945 arg_strings = lappend(arg_strings, str);
946 }
947 i++;
948 }
949
951
952 PG_TRY();
953 {
954 buf = xmlBufferCreate();
955 if (buf == NULL || xmlerrcxt->err_occurred)
956 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
957 "could not allocate xmlBuffer");
958 writer = xmlNewTextWriterMemory(buf, 0);
959 if (writer == NULL || xmlerrcxt->err_occurred)
960 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
961 "could not allocate xmlTextWriter");
962
963 if (xmlTextWriterStartElement(writer, (xmlChar *) xexpr->name) < 0 ||
964 xmlerrcxt->err_occurred)
965 xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
966 "could not start xml element");
967
968 forboth(arg, named_arg_strings, narg, xexpr->arg_names)
969 {
970 char *str = (char *) lfirst(arg);
971 char *argname = strVal(lfirst(narg));
972
973 if (str)
974 {
975 if (xmlTextWriterWriteAttribute(writer,
976 (xmlChar *) argname,
977 (xmlChar *) str) < 0 ||
978 xmlerrcxt->err_occurred)
979 xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
980 "could not write xml attribute");
981 }
982 }
983
984 foreach(arg, arg_strings)
985 {
986 char *str = (char *) lfirst(arg);
987
988 if (xmlTextWriterWriteRaw(writer, (xmlChar *) str) < 0 ||
989 xmlerrcxt->err_occurred)
990 xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
991 "could not write raw xml text");
992 }
993
994 if (xmlTextWriterEndElement(writer) < 0 ||
995 xmlerrcxt->err_occurred)
996 xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
997 "could not end xml element");
998
999 /* we MUST do this now to flush data out to the buffer ... */
1000 xmlFreeTextWriter(writer);
1001 writer = NULL;
1002
1003 result = xmlBuffer_to_xmltype(buf);
1004 }
1005 PG_CATCH();
1006 {
1007 if (writer)
1008 xmlFreeTextWriter(writer);
1009 if (buf)
1010 xmlBufferFree(buf);
1011
1012 pg_xml_done(xmlerrcxt, true);
1013
1014 PG_RE_THROW();
1015 }
1016 PG_END_TRY();
1017
1018 xmlBufferFree(buf);
1019
1020 pg_xml_done(xmlerrcxt, false);
1021
1022 return result;
1023#else
1025 return NULL;
1026#endif
1027}
List * lappend(List *list, void *datum)
Definition: list.c:339
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
#define NIL
Definition: pg_list.h:68
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
e
Definition: preproc-init.c:82
Definition: pg_list.h:54
List * args
Definition: primnodes.h:1619
List * named_args
Definition: primnodes.h:1615
#define strVal(v)
Definition: value.h:82

References arg, XmlExpr::args, buf, ERROR, exprType(), forboth, i, lappend(), lfirst, map_sql_value_to_xml_value(), XmlExpr::named_args, NIL, NO_XML_SUPPORT, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, pg_xml_done(), pg_xml_init(), PG_XML_STRICTNESS_ALL, str, strVal, and xml_ereport().

Referenced by ExecEvalXmlExpr().

◆ xmlparse()

xmltype * xmlparse ( text data,
XmlOptionType  xmloption_arg,
bool  preserve_whitespace 
)

Definition at line 1031 of file xml.c.

1032{
1033#ifdef USE_LIBXML
1034 xmlDocPtr doc;
1035
1036 doc = xml_parse(data, xmloption_arg, preserve_whitespace,
1037 GetDatabaseEncoding(), NULL, NULL, NULL);
1038 xmlFreeDoc(doc);
1039
1040 return (xmltype *) data;
1041#else
1043 return NULL;
1044#endif
1045}
const void * data

References data, GetDatabaseEncoding(), and NO_XML_SUPPORT.

Referenced by ExecEvalXmlExpr(), and texttoxml().

◆ XmlPGetDatum()

static Datum XmlPGetDatum ( const xmltype X)
inlinestatic

Definition at line 57 of file xml.h.

58{
59 return PointerGetDatum(X);
60}

References PointerGetDatum().

◆ xmlpi()

xmltype * xmlpi ( const char *  target,
text arg,
bool  arg_is_null,
bool *  result_is_null 
)

Definition at line 1049 of file xml.c.

1050{
1051#ifdef USE_LIBXML
1052 xmltype *result;
1054
1055 if (pg_strcasecmp(target, "xml") == 0)
1056 ereport(ERROR,
1057 (errcode(ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION),
1058 errmsg("invalid XML processing instruction"),
1059 errdetail("XML processing instruction target name cannot be \"%s\".", target)));
1060
1061 /*
1062 * Following the SQL standard, the null check comes after the syntax check
1063 * above.
1064 */
1065 *result_is_null = arg_is_null;
1066 if (*result_is_null)
1067 return NULL;
1068
1070
1071 appendStringInfo(&buf, "<?%s", target);
1072
1073 if (arg != NULL)
1074 {
1075 char *string;
1076
1077 string = text_to_cstring(arg);
1078 if (strstr(string, "?>") != NULL)
1079 ereport(ERROR,
1080 (errcode(ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION),
1081 errmsg("invalid XML processing instruction"),
1082 errdetail("XML processing instruction cannot contain \"?>\".")));
1083
1085 appendStringInfoString(&buf, string + strspn(string, " "));
1086 pfree(string);
1087 }
1089
1090 result = stringinfo_to_xmltype(&buf);
1091 pfree(buf.data);
1092 return result;
1093#else
1095 return NULL;
1096#endif
1097}
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
char string[11]
Definition: preproc-type.c:52
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:242

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), arg, buf, ereport, errcode(), errdetail(), errmsg(), ERROR, initStringInfo(), NO_XML_SUPPORT, pfree(), pg_strcasecmp(), stringinfo_to_xmltype(), and text_to_cstring().

Referenced by ExecEvalXmlExpr().

◆ xmlroot()

xmltype * xmlroot ( xmltype data,
text version,
int  standalone 
)

Definition at line 1101 of file xml.c.

1102{
1103#ifdef USE_LIBXML
1104 char *str;
1105 size_t len;
1106 xmlChar *orig_version;
1107 int orig_standalone;
1109
1110 len = VARSIZE(data) - VARHDRSZ;
1112
1113 parse_xml_decl((xmlChar *) str, &len, &orig_version, NULL, &orig_standalone);
1114
1115 if (version)
1116 orig_version = xml_text2xmlChar(version);
1117 else
1118 orig_version = NULL;
1119
1120 switch (standalone)
1121 {
1122 case XML_STANDALONE_YES:
1123 orig_standalone = 1;
1124 break;
1125 case XML_STANDALONE_NO:
1126 orig_standalone = 0;
1127 break;
1129 orig_standalone = -1;
1130 break;
1132 /* leave original value */
1133 break;
1134 }
1135
1137 print_xml_decl(&buf, orig_version, 0, orig_standalone);
1139
1140 return stringinfo_to_xmltype(&buf);
1141#else
1143 return NULL;
1144#endif
1145}

References appendStringInfoString(), buf, data, initStringInfo(), len, NO_XML_SUPPORT, str, stringinfo_to_xmltype(), text_to_cstring(), VARHDRSZ, VARSIZE(), XML_STANDALONE_NO, XML_STANDALONE_NO_VALUE, XML_STANDALONE_OMITTED, and XML_STANDALONE_YES.

Referenced by ExecEvalXmlExpr().

◆ xmltotext_with_options()

text * xmltotext_with_options ( xmltype data,
XmlOptionType  xmloption_arg,
bool  indent 
)

Definition at line 677 of file xml.c.

678{
679#ifdef USE_LIBXML
680 text *volatile result;
681 xmlDocPtr doc;
682 XmlOptionType parsed_xmloptiontype;
683 xmlNodePtr content_nodes;
684 volatile xmlBufferPtr buf = NULL;
685 volatile xmlSaveCtxtPtr ctxt = NULL;
686 ErrorSaveContext escontext = {T_ErrorSaveContext};
687 PgXmlErrorContext *volatile xmlerrcxt = NULL;
688#endif
689
690 if (xmloption_arg != XMLOPTION_DOCUMENT && !indent)
691 {
692 /*
693 * We don't actually need to do anything, so just return the
694 * binary-compatible input. For backwards-compatibility reasons,
695 * allow such cases to succeed even without USE_LIBXML.
696 */
697 return (text *) data;
698 }
699
700#ifdef USE_LIBXML
701
702 /*
703 * Parse the input according to the xmloption.
704 *
705 * preserve_whitespace is set to false in case we are indenting, otherwise
706 * libxml2 will fail to indent elements that have whitespace between them.
707 */
708 doc = xml_parse(data, xmloption_arg, !indent, GetDatabaseEncoding(),
709 &parsed_xmloptiontype, &content_nodes,
710 (Node *) &escontext);
711 if (doc == NULL || escontext.error_occurred)
712 {
713 if (doc)
714 xmlFreeDoc(doc);
715 /* A soft error must be failure to conform to XMLOPTION_DOCUMENT */
717 (errcode(ERRCODE_NOT_AN_XML_DOCUMENT),
718 errmsg("not an XML document")));
719 }
720
721 /* If we weren't asked to indent, we're done. */
722 if (!indent)
723 {
724 xmlFreeDoc(doc);
725 return (text *) data;
726 }
727
728 /*
729 * Otherwise, we gotta spin up some error handling. Unlike most other
730 * routines in this module, we already have a libxml "doc" structure to
731 * free, so we need to call pg_xml_init() inside the PG_TRY and be
732 * prepared for it to fail (typically due to palloc OOM).
733 */
734 PG_TRY();
735 {
736 size_t decl_len = 0;
737
739
740 /* The serialized data will go into this buffer. */
741 buf = xmlBufferCreate();
742
743 if (buf == NULL || xmlerrcxt->err_occurred)
744 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
745 "could not allocate xmlBuffer");
746
747 /* Detect whether there's an XML declaration */
748 parse_xml_decl(xml_text2xmlChar(data), &decl_len, NULL, NULL, NULL);
749
750 /*
751 * Emit declaration only if the input had one. Note: some versions of
752 * xmlSaveToBuffer leak memory if a non-null encoding argument is
753 * passed, so don't do that. We don't want any encoding conversion
754 * anyway.
755 */
756 if (decl_len == 0)
757 ctxt = xmlSaveToBuffer(buf, NULL,
758 XML_SAVE_NO_DECL | XML_SAVE_FORMAT);
759 else
760 ctxt = xmlSaveToBuffer(buf, NULL,
761 XML_SAVE_FORMAT);
762
763 if (ctxt == NULL || xmlerrcxt->err_occurred)
764 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
765 "could not allocate xmlSaveCtxt");
766
767 if (parsed_xmloptiontype == XMLOPTION_DOCUMENT)
768 {
769 /* If it's a document, saving is easy. */
770 if (xmlSaveDoc(ctxt, doc) == -1 || xmlerrcxt->err_occurred)
771 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
772 "could not save document to xmlBuffer");
773 }
774 else if (content_nodes != NULL)
775 {
776 /*
777 * Deal with the case where we have non-singly-rooted XML.
778 * libxml's dump functions don't work well for that without help.
779 * We build a fake root node that serves as a container for the
780 * content nodes, and then iterate over the nodes.
781 */
782 xmlNodePtr root;
783 xmlNodePtr oldroot;
784 xmlNodePtr newline;
785
786 root = xmlNewNode(NULL, (const xmlChar *) "content-root");
787 if (root == NULL || xmlerrcxt->err_occurred)
788 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
789 "could not allocate xml node");
790
791 /*
792 * This attaches root to doc, so we need not free it separately...
793 * but instead, we have to free the old root if there was one.
794 */
795 oldroot = xmlDocSetRootElement(doc, root);
796 if (oldroot != NULL)
797 xmlFreeNode(oldroot);
798
799 if (xmlAddChildList(root, content_nodes) == NULL ||
800 xmlerrcxt->err_occurred)
801 xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
802 "could not append xml node list");
803
804 /*
805 * We use this node to insert newlines in the dump. Note: in at
806 * least some libxml versions, xmlNewDocText would not attach the
807 * node to the document even if we passed it. Therefore, manage
808 * freeing of this node manually, and pass NULL here to make sure
809 * there's not a dangling link.
810 */
811 newline = xmlNewDocText(NULL, (const xmlChar *) "\n");
812 if (newline == NULL || xmlerrcxt->err_occurred)
813 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
814 "could not allocate xml node");
815
816 for (xmlNodePtr node = root->children; node; node = node->next)
817 {
818 /* insert newlines between nodes */
819 if (node->type != XML_TEXT_NODE && node->prev != NULL)
820 {
821 if (xmlSaveTree(ctxt, newline) == -1 || xmlerrcxt->err_occurred)
822 {
823 xmlFreeNode(newline);
824 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
825 "could not save newline to xmlBuffer");
826 }
827 }
828
829 if (xmlSaveTree(ctxt, node) == -1 || xmlerrcxt->err_occurred)
830 {
831 xmlFreeNode(newline);
832 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
833 "could not save content to xmlBuffer");
834 }
835 }
836
837 xmlFreeNode(newline);
838 }
839
840 if (xmlSaveClose(ctxt) == -1 || xmlerrcxt->err_occurred)
841 {
842 ctxt = NULL; /* don't try to close it again */
843 xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
844 "could not close xmlSaveCtxtPtr");
845 }
846
847 /*
848 * xmlDocContentDumpOutput may add a trailing newline, so remove that.
849 */
850 if (xmloption_arg == XMLOPTION_DOCUMENT)
851 {
852 const char *str = (const char *) xmlBufferContent(buf);
853 int len = xmlBufferLength(buf);
854
855 while (len > 0 && (str[len - 1] == '\n' ||
856 str[len - 1] == '\r'))
857 len--;
858
860 }
861 else
862 result = (text *) xmlBuffer_to_xmltype(buf);
863 }
864 PG_CATCH();
865 {
866 if (ctxt)
867 xmlSaveClose(ctxt);
868 if (buf)
869 xmlBufferFree(buf);
870 xmlFreeDoc(doc);
871
872 if (xmlerrcxt)
873 pg_xml_done(xmlerrcxt, true);
874
875 PG_RE_THROW();
876 }
877 PG_END_TRY();
878
879 xmlBufferFree(buf);
880 xmlFreeDoc(doc);
881
882 pg_xml_done(xmlerrcxt, false);
883
884 return result;
885#else
887 return NULL;
888#endif
889}
#define newline
Definition: indent_codes.h:35
XmlOptionType
Definition: primnodes.h:1602
tree ctl root
Definition: radixtree.h:1857
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:193

References buf, cstring_to_text_with_len(), data, ereport, errcode(), errmsg(), ERROR, ErrorSaveContext::error_occurred, GetDatabaseEncoding(), len, newline, NO_XML_SUPPORT, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, pg_xml_done(), pg_xml_init(), PG_XML_STRICTNESS_ALL, root, str, xml_ereport(), and XMLOPTION_DOCUMENT.

Referenced by ExecEvalXmlExpr().

Variable Documentation

◆ xmlbinary

PGDLLIMPORT int xmlbinary
extern

Definition at line 108 of file xml.c.

Referenced by map_sql_type_to_xmlschema_type(), and map_sql_value_to_xml_value().

◆ xmloption

PGDLLIMPORT int xmloption
extern

Definition at line 109 of file xml.c.

Referenced by texttoxml(), xml_in(), xml_is_well_formed(), and xml_recv().

◆ XmlTableRoutine

PGDLLIMPORT const TableFuncRoutine XmlTableRoutine
extern

Definition at line 222 of file xml.c.

Referenced by ExecInitTableFuncScan().