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 1 of file xml.h.

◆ xmltype

typedef struct varlena xmltype

Definition at line 1 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:674

References PG_DETOAST_DATUM.

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

◆ escape_xml()

char* escape_xml ( const char *  str)

Definition at line 2632 of file xml.c.

2633 {
2635  const char *p;
2636 
2637  initStringInfo(&buf);
2638  for (p = str; *p; p++)
2639  {
2640  switch (*p)
2641  {
2642  case '&':
2643  appendStringInfoString(&buf, "&");
2644  break;
2645  case '<':
2646  appendStringInfoString(&buf, "&lt;");
2647  break;
2648  case '>':
2649  appendStringInfoString(&buf, "&gt;");
2650  break;
2651  case '\r':
2652  appendStringInfoString(&buf, "&#x0d;");
2653  break;
2654  default:
2656  break;
2657  }
2658  }
2659  return buf.data;
2660 }
static char * buf
Definition: pg_test_fsync.c:73
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:182
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:204

References appendStringInfoCharMacro, appendStringInfoString(), buf, initStringInfo(), and generate_unaccent_rules::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 2315 of file xml.c.

2317 {
2318 #ifdef USE_LIBXML
2320  const char *p;
2321 
2322  /*
2323  * SQL/XML doesn't make use of this case anywhere, so it's probably a
2324  * mistake.
2325  */
2326  Assert(fully_escaped || !escape_period);
2327 
2328  initStringInfo(&buf);
2329 
2330  for (p = ident; *p; p += pg_mblen(p))
2331  {
2332  if (*p == ':' && (p == ident || fully_escaped))
2333  appendStringInfoString(&buf, "_x003A_");
2334  else if (*p == '_' && *(p + 1) == 'x')
2335  appendStringInfoString(&buf, "_x005F_");
2336  else if (fully_escaped && p == ident &&
2337  pg_strncasecmp(p, "xml", 3) == 0)
2338  {
2339  if (*p == 'x')
2340  appendStringInfoString(&buf, "_x0078_");
2341  else
2342  appendStringInfoString(&buf, "_x0058_");
2343  }
2344  else if (escape_period && *p == '.')
2345  appendStringInfoString(&buf, "_x002E_");
2346  else
2347  {
2348  pg_wchar u = sqlchar_to_unicode(p);
2349 
2350  if ((p == ident)
2351  ? !is_valid_xml_namefirst(u)
2352  : !is_valid_xml_namechar(u))
2353  appendStringInfo(&buf, "_x%04X_", (unsigned int) u);
2354  else
2356  }
2357  }
2358 
2359  return buf.data;
2360 #else /* not USE_LIBXML */
2361  NO_XML_SUPPORT();
2362  return NULL;
2363 #endif /* not USE_LIBXML */
2364 }
#define ident
Definition: indent_codes.h:47
Assert(fmt[strlen(fmt) - 1] !='\n')
unsigned int pg_wchar
Definition: mbprint.c:31
int pg_mblen(const char *mbstr)
Definition: mbutils.c:1023
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:97
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
Definition: stringinfo.c:233
#define NO_XML_SUPPORT()
Definition: xml.c:235

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 2413 of file xml.c.

2414 {
2416  {
2417  ArrayType *array;
2418  Oid elmtype;
2419  int16 elmlen;
2420  bool elmbyval;
2421  char elmalign;
2422  int num_elems;
2423  Datum *elem_values;
2424  bool *elem_nulls;
2426  int i;
2427 
2428  array = DatumGetArrayTypeP(value);
2429  elmtype = ARR_ELEMTYPE(array);
2430  get_typlenbyvalalign(elmtype, &elmlen, &elmbyval, &elmalign);
2431 
2432  deconstruct_array(array, elmtype,
2433  elmlen, elmbyval, elmalign,
2434  &elem_values, &elem_nulls,
2435  &num_elems);
2436 
2437  initStringInfo(&buf);
2438 
2439  for (i = 0; i < num_elems; i++)
2440  {
2441  if (elem_nulls[i])
2442  continue;
2443  appendStringInfoString(&buf, "<element>");
2445  map_sql_value_to_xml_value(elem_values[i],
2446  elmtype, true));
2447  appendStringInfoString(&buf, "</element>");
2448  }
2449 
2450  pfree(elem_values);
2451  pfree(elem_nulls);
2452 
2453  return buf.data;
2454  }
2455  else
2456  {
2457  Oid typeOut;
2458  bool isvarlena;
2459  char *str;
2460 
2461  /*
2462  * Flatten domains; the special-case treatments below should apply to,
2463  * eg, domains over boolean not just boolean.
2464  */
2465  type = getBaseType(type);
2466 
2467  /*
2468  * Special XSD formatting for some data types
2469  */
2470  switch (type)
2471  {
2472  case BOOLOID:
2473  if (DatumGetBool(value))
2474  return "true";
2475  else
2476  return "false";
2477 
2478  case DATEOID:
2479  {
2480  DateADT date;
2481  struct pg_tm tm;
2482  char buf[MAXDATELEN + 1];
2483 
2485  /* XSD doesn't support infinite values */
2486  if (DATE_NOT_FINITE(date))
2487  ereport(ERROR,
2488  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2489  errmsg("date out of range"),
2490  errdetail("XML does not support infinite date values.")));
2492  &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
2494 
2495  return pstrdup(buf);
2496  }
2497 
2498  case TIMESTAMPOID:
2499  {
2501  struct pg_tm tm;
2502  fsec_t fsec;
2503  char buf[MAXDATELEN + 1];
2504 
2506 
2507  /* XSD doesn't support infinite values */
2509  ereport(ERROR,
2510  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2511  errmsg("timestamp out of range"),
2512  errdetail("XML does not support infinite timestamp values.")));
2513  else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
2514  EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
2515  else
2516  ereport(ERROR,
2517  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2518  errmsg("timestamp out of range")));
2519 
2520  return pstrdup(buf);
2521  }
2522 
2523  case TIMESTAMPTZOID:
2524  {
2526  struct pg_tm tm;
2527  int tz;
2528  fsec_t fsec;
2529  const char *tzn = NULL;
2530  char buf[MAXDATELEN + 1];
2531 
2533 
2534  /* XSD doesn't support infinite values */
2536  ereport(ERROR,
2537  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2538  errmsg("timestamp out of range"),
2539  errdetail("XML does not support infinite timestamp values.")));
2540  else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
2541  EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
2542  else
2543  ereport(ERROR,
2544  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2545  errmsg("timestamp out of range")));
2546 
2547  return pstrdup(buf);
2548  }
2549 
2550 #ifdef USE_LIBXML
2551  case BYTEAOID:
2552  {
2553  bytea *bstr = DatumGetByteaPP(value);
2554  PgXmlErrorContext *xmlerrcxt;
2555  volatile xmlBufferPtr buf = NULL;
2556  volatile xmlTextWriterPtr writer = NULL;
2557  char *result;
2558 
2559  xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
2560 
2561  PG_TRY();
2562  {
2563  buf = xmlBufferCreate();
2564  if (buf == NULL || xmlerrcxt->err_occurred)
2565  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
2566  "could not allocate xmlBuffer");
2567  writer = xmlNewTextWriterMemory(buf, 0);
2568  if (writer == NULL || xmlerrcxt->err_occurred)
2569  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
2570  "could not allocate xmlTextWriter");
2571 
2572  if (xmlbinary == XMLBINARY_BASE64)
2573  xmlTextWriterWriteBase64(writer, VARDATA_ANY(bstr),
2574  0, VARSIZE_ANY_EXHDR(bstr));
2575  else
2576  xmlTextWriterWriteBinHex(writer, VARDATA_ANY(bstr),
2577  0, VARSIZE_ANY_EXHDR(bstr));
2578 
2579  /* we MUST do this now to flush data out to the buffer */
2580  xmlFreeTextWriter(writer);
2581  writer = NULL;
2582 
2583  result = pstrdup((const char *) xmlBufferContent(buf));
2584  }
2585  PG_CATCH();
2586  {
2587  if (writer)
2588  xmlFreeTextWriter(writer);
2589  if (buf)
2590  xmlBufferFree(buf);
2591 
2592  pg_xml_done(xmlerrcxt, true);
2593 
2594  PG_RE_THROW();
2595  }
2596  PG_END_TRY();
2597 
2598  xmlBufferFree(buf);
2599 
2600  pg_xml_done(xmlerrcxt, false);
2601 
2602  return result;
2603  }
2604 #endif /* USE_LIBXML */
2605 
2606  }
2607 
2608  /*
2609  * otherwise, just use the type's native text representation
2610  */
2611  getTypeOutputInfo(type, &typeOut, &isvarlena);
2612  str = OidOutputFunctionCall(typeOut, value);
2613 
2614  /* ... exactly as-is for XML, and when escaping is not wanted */
2615  if (type == XMLOID || !xml_escape_strings)
2616  return str;
2617 
2618  /* otherwise, translate special characters as needed */
2619  return escape_xml(str);
2620  }
2621 }
#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:3612
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:311
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:4331
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
Definition: datetime.c:4216
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1901
signed short int16
Definition: c.h:480
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:1205
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define PG_RE_THROW()
Definition: elog.h:411
#define PG_TRY(...)
Definition: elog.h:370
#define PG_END_TRY(...)
Definition: elog.h:395
#define ERROR
Definition: elog.h:39
#define PG_CATCH(...)
Definition: elog.h:380
#define ereport(elevel,...)
Definition: elog.h:149
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1763
#define DatumGetByteaPP(X)
Definition: fmgr.h:291
#define MAXDATELEN
Definition: datetime.h:200
static struct @150 value
int i
Definition: isn.c:73
static struct pg_tm tm
Definition: localtime.c:104
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2863
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2227
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2477
#define type_is_array_domain(typid)
Definition: lsyscache.h:208
char * pstrdup(const char *in)
Definition: mcxt.c:1683
void pfree(void *pointer)
Definition: mcxt.c:1508
#define USE_XSD_DATES
Definition: miscadmin.h:238
long date
Definition: pgtypes_date.h:9
int64 timestamp
static bool DatumGetBool(Datum X)
Definition: postgres.h:90
uintptr_t Datum
Definition: postgres.h:64
unsigned int Oid
Definition: postgres_ext.h:31
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
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317
const char * type
int xmlbinary
Definition: xml.c:109
char * escape_xml(const char *str)
Definition: xml.c:2632
char * map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
Definition: xml.c:2413
struct PgXmlErrorContext PgXmlErrorContext
Definition: xml.h:48
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
void pg_xml_done(PgXmlErrorContext *errcxt, bool isError)
PgXmlErrorContext * pg_xml_init(PgXmlStrictness strictness)

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(), 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(), generate_unaccent_rules::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(), 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 2371 of file xml.c.

2372 {
2374  const char *p;
2375 
2376  initStringInfo(&buf);
2377 
2378  for (p = name; *p; p += pg_mblen(p))
2379  {
2380  if (*p == '_' && *(p + 1) == 'x'
2381  && isxdigit((unsigned char) *(p + 2))
2382  && isxdigit((unsigned char) *(p + 3))
2383  && isxdigit((unsigned char) *(p + 4))
2384  && isxdigit((unsigned char) *(p + 5))
2385  && *(p + 6) == '_')
2386  {
2387  char cbuf[MAX_UNICODE_EQUIVALENT_STRING + 1];
2388  unsigned int u;
2389 
2390  sscanf(p + 2, "%X", &u);
2391  pg_unicode_to_server(u, (unsigned char *) cbuf);
2392  appendStringInfoString(&buf, cbuf);
2393  p += 6;
2394  }
2395  else
2397  }
2398 
2399  return buf.data;
2400 }
void pg_unicode_to_server(pg_wchar c, unsigned char *s)
Definition: mbutils.c:864
#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()

bool pg_xml_error_occurred ( PgXmlErrorContext errcxt)

◆ 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 1096 of file xml.c.

1097 {
1098 #ifdef USE_LIBXML
1099  xmlDocPtr doc;
1100  ErrorSaveContext escontext = {T_ErrorSaveContext};
1101 
1102  /*
1103  * We'll report "true" if no soft error is reported by xml_parse().
1104  */
1105  doc = xml_parse((text *) arg, XMLOPTION_DOCUMENT, true,
1106  GetDatabaseEncoding(), NULL, NULL, (Node *) &escontext);
1107  if (doc)
1108  xmlFreeDoc(doc);
1109 
1110  return !escontext.error_occurred;
1111 #else /* not USE_LIBXML */
1112  NO_XML_SUPPORT();
1113  return false;
1114 #endif /* not USE_LIBXML */
1115 }
int GetDatabaseEncoding(void)
Definition: mbutils.c:1261
void * arg
@ XMLOPTION_DOCUMENT
Definition: primnodes.h:1548
bool error_occurred
Definition: miscnodes.h:46
Definition: nodes.h:129

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

Referenced by ExecEvalXmlExpr().

◆ xmlconcat()

xmltype* xmlconcat ( List args)

Definition at line 553 of file xml.c.

554 {
555 #ifdef USE_LIBXML
556  int global_standalone = 1;
557  xmlChar *global_version = NULL;
558  bool global_version_no_value = false;
560  ListCell *v;
561 
563  foreach(v, args)
564  {
566  size_t len;
567  xmlChar *version;
568  int standalone;
569  char *str;
570 
571  len = VARSIZE(x) - VARHDRSZ;
572  str = text_to_cstring((text *) x);
573 
574  parse_xml_decl((xmlChar *) str, &len, &version, NULL, &standalone);
575 
576  if (standalone == 0 && global_standalone == 1)
577  global_standalone = 0;
578  if (standalone < 0)
579  global_standalone = -1;
580 
581  if (!version)
582  global_version_no_value = true;
583  else if (!global_version)
584  global_version = version;
585  else if (xmlStrcmp(version, global_version) != 0)
586  global_version_no_value = true;
587 
589  pfree(str);
590  }
591 
592  if (!global_version_no_value || global_standalone >= 0)
593  {
594  StringInfoData buf2;
595 
596  initStringInfo(&buf2);
597 
598  print_xml_decl(&buf2,
599  (!global_version_no_value) ? global_version : NULL,
600  0,
601  global_standalone);
602 
603  appendBinaryStringInfo(&buf2, buf.data, buf.len);
604  buf = buf2;
605  }
606 
607  return stringinfo_to_xmltype(&buf);
608 #else
609  NO_XML_SUPPORT();
610  return NULL;
611 #endif
612 }
#define VARHDRSZ
Definition: c.h:679
int x
Definition: isn.c:71
const void size_t len
#define lfirst(lc)
Definition: pg_list.h:172
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
#define VARSIZE(PTR)
Definition: varatt.h:279
char * text_to_cstring(const text *t)
Definition: varlena.c:217
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:467
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(), generate_unaccent_rules::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 836 of file xml.c.

839 {
840 #ifdef USE_LIBXML
841  xmltype *result;
842  List *named_arg_strings;
843  List *arg_strings;
844  int i;
845  ListCell *arg;
846  ListCell *narg;
847  PgXmlErrorContext *xmlerrcxt;
848  volatile xmlBufferPtr buf = NULL;
849  volatile xmlTextWriterPtr writer = NULL;
850 
851  /*
852  * All arguments are already evaluated, and their values are passed in the
853  * named_argvalue/named_argnull or argvalue/argnull arrays. This avoids
854  * issues if one of the arguments involves a call to some other function
855  * or subsystem that wants to use libxml on its own terms. We examine the
856  * original XmlExpr to identify the numbers and types of the arguments.
857  */
858  named_arg_strings = NIL;
859  i = 0;
860  foreach(arg, xexpr->named_args)
861  {
862  Expr *e = (Expr *) lfirst(arg);
863  char *str;
864 
865  if (named_argnull[i])
866  str = NULL;
867  else
868  str = map_sql_value_to_xml_value(named_argvalue[i],
869  exprType((Node *) e),
870  false);
871  named_arg_strings = lappend(named_arg_strings, str);
872  i++;
873  }
874 
875  arg_strings = NIL;
876  i = 0;
877  foreach(arg, xexpr->args)
878  {
879  Expr *e = (Expr *) lfirst(arg);
880  char *str;
881 
882  /* here we can just forget NULL elements immediately */
883  if (!argnull[i])
884  {
885  str = map_sql_value_to_xml_value(argvalue[i],
886  exprType((Node *) e),
887  true);
888  arg_strings = lappend(arg_strings, str);
889  }
890  i++;
891  }
892 
893  xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
894 
895  PG_TRY();
896  {
897  buf = xmlBufferCreate();
898  if (buf == NULL || xmlerrcxt->err_occurred)
899  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
900  "could not allocate xmlBuffer");
901  writer = xmlNewTextWriterMemory(buf, 0);
902  if (writer == NULL || xmlerrcxt->err_occurred)
903  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
904  "could not allocate xmlTextWriter");
905 
906  xmlTextWriterStartElement(writer, (xmlChar *) xexpr->name);
907 
908  forboth(arg, named_arg_strings, narg, xexpr->arg_names)
909  {
910  char *str = (char *) lfirst(arg);
911  char *argname = strVal(lfirst(narg));
912 
913  if (str)
914  xmlTextWriterWriteAttribute(writer,
915  (xmlChar *) argname,
916  (xmlChar *) str);
917  }
918 
919  foreach(arg, arg_strings)
920  {
921  char *str = (char *) lfirst(arg);
922 
923  xmlTextWriterWriteRaw(writer, (xmlChar *) str);
924  }
925 
926  xmlTextWriterEndElement(writer);
927 
928  /* we MUST do this now to flush data out to the buffer ... */
929  xmlFreeTextWriter(writer);
930  writer = NULL;
931 
932  result = xmlBuffer_to_xmltype(buf);
933  }
934  PG_CATCH();
935  {
936  if (writer)
937  xmlFreeTextWriter(writer);
938  if (buf)
939  xmlBufferFree(buf);
940 
941  pg_xml_done(xmlerrcxt, true);
942 
943  PG_RE_THROW();
944  }
945  PG_END_TRY();
946 
947  xmlBufferFree(buf);
948 
949  pg_xml_done(xmlerrcxt, false);
950 
951  return result;
952 #else
953  NO_XML_SUPPORT();
954  return NULL;
955 #endif
956 }
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:1564
List * named_args
Definition: primnodes.h:1560
#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, generate_unaccent_rules::str, strVal, and xml_ereport().

Referenced by ExecEvalXmlExpr().

◆ xmlparse()

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

Definition at line 960 of file xml.c.

961 {
962 #ifdef USE_LIBXML
963  xmlDocPtr doc;
964 
965  doc = xml_parse(data, xmloption_arg, preserve_whitespace,
966  GetDatabaseEncoding(), NULL, NULL, NULL);
967  xmlFreeDoc(doc);
968 
969  return (xmltype *) data;
970 #else
971  NO_XML_SUPPORT();
972  return NULL;
973 #endif
974 }
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 978 of file xml.c.

979 {
980 #ifdef USE_LIBXML
981  xmltype *result;
983 
984  if (pg_strcasecmp(target, "xml") == 0)
985  ereport(ERROR,
986  (errcode(ERRCODE_SYNTAX_ERROR), /* really */
987  errmsg("invalid XML processing instruction"),
988  errdetail("XML processing instruction target name cannot be \"%s\".", target)));
989 
990  /*
991  * Following the SQL standard, the null check comes after the syntax check
992  * above.
993  */
994  *result_is_null = arg_is_null;
995  if (*result_is_null)
996  return NULL;
997 
999 
1000  appendStringInfo(&buf, "<?%s", target);
1001 
1002  if (arg != NULL)
1003  {
1004  char *string;
1005 
1006  string = text_to_cstring(arg);
1007  if (strstr(string, "?>") != NULL)
1008  ereport(ERROR,
1009  (errcode(ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION),
1010  errmsg("invalid XML processing instruction"),
1011  errdetail("XML processing instruction cannot contain \"?>\".")));
1012 
1013  appendStringInfoChar(&buf, ' ');
1014  appendStringInfoString(&buf, string + strspn(string, " "));
1015  pfree(string);
1016  }
1017  appendStringInfoString(&buf, "?>");
1018 
1019  result = stringinfo_to_xmltype(&buf);
1020  pfree(buf.data);
1021  return result;
1022 #else
1023  NO_XML_SUPPORT();
1024  return NULL;
1025 #endif
1026 }
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:194

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 1030 of file xml.c.

1031 {
1032 #ifdef USE_LIBXML
1033  char *str;
1034  size_t len;
1035  xmlChar *orig_version;
1036  int orig_standalone;
1038 
1039  len = VARSIZE(data) - VARHDRSZ;
1040  str = text_to_cstring((text *) data);
1041 
1042  parse_xml_decl((xmlChar *) str, &len, &orig_version, NULL, &orig_standalone);
1043 
1044  if (version)
1045  orig_version = xml_text2xmlChar(version);
1046  else
1047  orig_version = NULL;
1048 
1049  switch (standalone)
1050  {
1051  case XML_STANDALONE_YES:
1052  orig_standalone = 1;
1053  break;
1054  case XML_STANDALONE_NO:
1055  orig_standalone = 0;
1056  break;
1058  orig_standalone = -1;
1059  break;
1061  /* leave original value */
1062  break;
1063  }
1064 
1065  initStringInfo(&buf);
1066  print_xml_decl(&buf, orig_version, 0, orig_standalone);
1068 
1069  return stringinfo_to_xmltype(&buf);
1070 #else
1071  NO_XML_SUPPORT();
1072  return NULL;
1073 #endif
1074 }

References appendStringInfoString(), buf, data, initStringInfo(), len, NO_XML_SUPPORT, generate_unaccent_rules::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 656 of file xml.c.

657 {
658 #ifdef USE_LIBXML
659  text *volatile result;
660  xmlDocPtr doc;
661  XmlOptionType parsed_xmloptiontype;
662  xmlNodePtr content_nodes;
663  volatile xmlBufferPtr buf = NULL;
664  volatile xmlSaveCtxtPtr ctxt = NULL;
665  ErrorSaveContext escontext = {T_ErrorSaveContext};
666  PgXmlErrorContext *xmlerrcxt;
667 #endif
668 
669  if (xmloption_arg != XMLOPTION_DOCUMENT && !indent)
670  {
671  /*
672  * We don't actually need to do anything, so just return the
673  * binary-compatible input. For backwards-compatibility reasons,
674  * allow such cases to succeed even without USE_LIBXML.
675  */
676  return (text *) data;
677  }
678 
679 #ifdef USE_LIBXML
680  /* Parse the input according to the xmloption */
681  doc = xml_parse(data, xmloption_arg, true, GetDatabaseEncoding(),
682  &parsed_xmloptiontype, &content_nodes,
683  (Node *) &escontext);
684  if (doc == NULL || escontext.error_occurred)
685  {
686  if (doc)
687  xmlFreeDoc(doc);
688  /* A soft error must be failure to conform to XMLOPTION_DOCUMENT */
689  ereport(ERROR,
690  (errcode(ERRCODE_NOT_AN_XML_DOCUMENT),
691  errmsg("not an XML document")));
692  }
693 
694  /* If we weren't asked to indent, we're done. */
695  if (!indent)
696  {
697  xmlFreeDoc(doc);
698  return (text *) data;
699  }
700 
701  /* Otherwise, we gotta spin up some error handling. */
702  xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
703 
704  PG_TRY();
705  {
706  size_t decl_len = 0;
707 
708  /* The serialized data will go into this buffer. */
709  buf = xmlBufferCreate();
710 
711  if (buf == NULL || xmlerrcxt->err_occurred)
712  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
713  "could not allocate xmlBuffer");
714 
715  /* Detect whether there's an XML declaration */
716  parse_xml_decl(xml_text2xmlChar(data), &decl_len, NULL, NULL, NULL);
717 
718  /*
719  * Emit declaration only if the input had one. Note: some versions of
720  * xmlSaveToBuffer leak memory if a non-null encoding argument is
721  * passed, so don't do that. We don't want any encoding conversion
722  * anyway.
723  */
724  if (decl_len == 0)
725  ctxt = xmlSaveToBuffer(buf, NULL,
726  XML_SAVE_NO_DECL | XML_SAVE_FORMAT);
727  else
728  ctxt = xmlSaveToBuffer(buf, NULL,
729  XML_SAVE_FORMAT);
730 
731  if (ctxt == NULL || xmlerrcxt->err_occurred)
732  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
733  "could not allocate xmlSaveCtxt");
734 
735  if (parsed_xmloptiontype == XMLOPTION_DOCUMENT)
736  {
737  /* If it's a document, saving is easy. */
738  if (xmlSaveDoc(ctxt, doc) == -1 || xmlerrcxt->err_occurred)
739  xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
740  "could not save document to xmlBuffer");
741  }
742  else if (content_nodes != NULL)
743  {
744  /*
745  * Deal with the case where we have non-singly-rooted XML.
746  * libxml's dump functions don't work well for that without help.
747  * We build a fake root node that serves as a container for the
748  * content nodes, and then iterate over the nodes.
749  */
750  xmlNodePtr root;
751  xmlNodePtr newline;
752 
753  root = xmlNewNode(NULL, (const xmlChar *) "content-root");
754  if (root == NULL || xmlerrcxt->err_occurred)
755  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
756  "could not allocate xml node");
757 
758  /* This attaches root to doc, so we need not free it separately. */
759  xmlDocSetRootElement(doc, root);
760  xmlAddChild(root, content_nodes);
761 
762  /*
763  * We use this node to insert newlines in the dump. Note: in at
764  * least some libxml versions, xmlNewDocText would not attach the
765  * node to the document even if we passed it. Therefore, manage
766  * freeing of this node manually, and pass NULL here to make sure
767  * there's not a dangling link.
768  */
769  newline = xmlNewDocText(NULL, (const xmlChar *) "\n");
770  if (newline == NULL || xmlerrcxt->err_occurred)
771  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
772  "could not allocate xml node");
773 
774  for (xmlNodePtr node = root->children; node; node = node->next)
775  {
776  /* insert newlines between nodes */
777  if (node->type != XML_TEXT_NODE && node->prev != NULL)
778  {
779  if (xmlSaveTree(ctxt, newline) == -1 || xmlerrcxt->err_occurred)
780  {
781  xmlFreeNode(newline);
782  xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
783  "could not save newline to xmlBuffer");
784  }
785  }
786 
787  if (xmlSaveTree(ctxt, node) == -1 || xmlerrcxt->err_occurred)
788  {
789  xmlFreeNode(newline);
790  xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
791  "could not save content to xmlBuffer");
792  }
793  }
794 
795  xmlFreeNode(newline);
796  }
797 
798  if (xmlSaveClose(ctxt) == -1 || xmlerrcxt->err_occurred)
799  {
800  ctxt = NULL; /* don't try to close it again */
801  xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
802  "could not close xmlSaveCtxtPtr");
803  }
804 
805  result = (text *) xmlBuffer_to_xmltype(buf);
806  }
807  PG_CATCH();
808  {
809  if (ctxt)
810  xmlSaveClose(ctxt);
811  if (buf)
812  xmlBufferFree(buf);
813  if (doc)
814  xmlFreeDoc(doc);
815 
816  pg_xml_done(xmlerrcxt, true);
817 
818  PG_RE_THROW();
819  }
820  PG_END_TRY();
821 
822  xmlBufferFree(buf);
823  xmlFreeDoc(doc);
824 
825  pg_xml_done(xmlerrcxt, false);
826 
827  return result;
828 #else
829  NO_XML_SUPPORT();
830  return NULL;
831 #endif
832 }
#define newline
Definition: indent_codes.h:35
XmlOptionType
Definition: primnodes.h:1547

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

Referenced by ExecEvalXmlExpr().

Variable Documentation

◆ xmlbinary

PGDLLIMPORT int xmlbinary
extern

Definition at line 109 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 110 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 223 of file xml.c.

Referenced by ExecInitTableFuncScan().