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:687

References PG_DETOAST_DATUM.

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

◆ escape_xml()

char* escape_xml ( const char *  str)

Definition at line 2674 of file xml.c.

2675 {
2677  const char *p;
2678 
2679  initStringInfo(&buf);
2680  for (p = str; *p; p++)
2681  {
2682  switch (*p)
2683  {
2684  case '&':
2685  appendStringInfoString(&buf, "&");
2686  break;
2687  case '<':
2688  appendStringInfoString(&buf, "&lt;");
2689  break;
2690  case '>':
2691  appendStringInfoString(&buf, "&gt;");
2692  break;
2693  case '\r':
2694  appendStringInfoString(&buf, "&#x0d;");
2695  break;
2696  default:
2698  break;
2699  }
2700  }
2701  return buf.data;
2702 }
const char * str
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 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 2357 of file xml.c.

2359 {
2360 #ifdef USE_LIBXML
2362  const char *p;
2363 
2364  /*
2365  * SQL/XML doesn't make use of this case anywhere, so it's probably a
2366  * mistake.
2367  */
2368  Assert(fully_escaped || !escape_period);
2369 
2370  initStringInfo(&buf);
2371 
2372  for (p = ident; *p; p += pg_mblen(p))
2373  {
2374  if (*p == ':' && (p == ident || fully_escaped))
2375  appendStringInfoString(&buf, "_x003A_");
2376  else if (*p == '_' && *(p + 1) == 'x')
2377  appendStringInfoString(&buf, "_x005F_");
2378  else if (fully_escaped && p == ident &&
2379  pg_strncasecmp(p, "xml", 3) == 0)
2380  {
2381  if (*p == 'x')
2382  appendStringInfoString(&buf, "_x0078_");
2383  else
2384  appendStringInfoString(&buf, "_x0058_");
2385  }
2386  else if (escape_period && *p == '.')
2387  appendStringInfoString(&buf, "_x002E_");
2388  else
2389  {
2390  pg_wchar u = sqlchar_to_unicode(p);
2391 
2392  if ((p == ident)
2393  ? !is_valid_xml_namefirst(u)
2394  : !is_valid_xml_namechar(u))
2395  appendStringInfo(&buf, "_x%04X_", (unsigned int) u);
2396  else
2398  }
2399  }
2400 
2401  return buf.data;
2402 #else /* not USE_LIBXML */
2403  NO_XML_SUPPORT();
2404  return NULL;
2405 #endif /* not USE_LIBXML */
2406 }
#define Assert(condition)
Definition: c.h:858
#define ident
Definition: indent_codes.h:47
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 2455 of file xml.c.

2456 {
2458  {
2459  ArrayType *array;
2460  Oid elmtype;
2461  int16 elmlen;
2462  bool elmbyval;
2463  char elmalign;
2464  int num_elems;
2465  Datum *elem_values;
2466  bool *elem_nulls;
2468  int i;
2469 
2470  array = DatumGetArrayTypeP(value);
2471  elmtype = ARR_ELEMTYPE(array);
2472  get_typlenbyvalalign(elmtype, &elmlen, &elmbyval, &elmalign);
2473 
2474  deconstruct_array(array, elmtype,
2475  elmlen, elmbyval, elmalign,
2476  &elem_values, &elem_nulls,
2477  &num_elems);
2478 
2479  initStringInfo(&buf);
2480 
2481  for (i = 0; i < num_elems; i++)
2482  {
2483  if (elem_nulls[i])
2484  continue;
2485  appendStringInfoString(&buf, "<element>");
2487  map_sql_value_to_xml_value(elem_values[i],
2488  elmtype, true));
2489  appendStringInfoString(&buf, "</element>");
2490  }
2491 
2492  pfree(elem_values);
2493  pfree(elem_nulls);
2494 
2495  return buf.data;
2496  }
2497  else
2498  {
2499  Oid typeOut;
2500  bool isvarlena;
2501  char *str;
2502 
2503  /*
2504  * Flatten domains; the special-case treatments below should apply to,
2505  * eg, domains over boolean not just boolean.
2506  */
2507  type = getBaseType(type);
2508 
2509  /*
2510  * Special XSD formatting for some data types
2511  */
2512  switch (type)
2513  {
2514  case BOOLOID:
2515  if (DatumGetBool(value))
2516  return "true";
2517  else
2518  return "false";
2519 
2520  case DATEOID:
2521  {
2522  DateADT date;
2523  struct pg_tm tm;
2524  char buf[MAXDATELEN + 1];
2525 
2527  /* XSD doesn't support infinite values */
2528  if (DATE_NOT_FINITE(date))
2529  ereport(ERROR,
2530  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2531  errmsg("date out of range"),
2532  errdetail("XML does not support infinite date values.")));
2534  &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
2536 
2537  return pstrdup(buf);
2538  }
2539 
2540  case TIMESTAMPOID:
2541  {
2543  struct pg_tm tm;
2544  fsec_t fsec;
2545  char buf[MAXDATELEN + 1];
2546 
2548 
2549  /* XSD doesn't support infinite values */
2551  ereport(ERROR,
2552  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2553  errmsg("timestamp out of range"),
2554  errdetail("XML does not support infinite timestamp values.")));
2555  else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
2556  EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
2557  else
2558  ereport(ERROR,
2559  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2560  errmsg("timestamp out of range")));
2561 
2562  return pstrdup(buf);
2563  }
2564 
2565  case TIMESTAMPTZOID:
2566  {
2568  struct pg_tm tm;
2569  int tz;
2570  fsec_t fsec;
2571  const char *tzn = NULL;
2572  char buf[MAXDATELEN + 1];
2573 
2575 
2576  /* XSD doesn't support infinite values */
2578  ereport(ERROR,
2579  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2580  errmsg("timestamp out of range"),
2581  errdetail("XML does not support infinite timestamp values.")));
2582  else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
2583  EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
2584  else
2585  ereport(ERROR,
2586  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2587  errmsg("timestamp out of range")));
2588 
2589  return pstrdup(buf);
2590  }
2591 
2592 #ifdef USE_LIBXML
2593  case BYTEAOID:
2594  {
2595  bytea *bstr = DatumGetByteaPP(value);
2596  PgXmlErrorContext *xmlerrcxt;
2597  volatile xmlBufferPtr buf = NULL;
2598  volatile xmlTextWriterPtr writer = NULL;
2599  char *result;
2600 
2601  xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
2602 
2603  PG_TRY();
2604  {
2605  buf = xmlBufferCreate();
2606  if (buf == NULL || xmlerrcxt->err_occurred)
2607  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
2608  "could not allocate xmlBuffer");
2609  writer = xmlNewTextWriterMemory(buf, 0);
2610  if (writer == NULL || xmlerrcxt->err_occurred)
2611  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
2612  "could not allocate xmlTextWriter");
2613 
2614  if (xmlbinary == XMLBINARY_BASE64)
2615  xmlTextWriterWriteBase64(writer, VARDATA_ANY(bstr),
2616  0, VARSIZE_ANY_EXHDR(bstr));
2617  else
2618  xmlTextWriterWriteBinHex(writer, VARDATA_ANY(bstr),
2619  0, VARSIZE_ANY_EXHDR(bstr));
2620 
2621  /* we MUST do this now to flush data out to the buffer */
2622  xmlFreeTextWriter(writer);
2623  writer = NULL;
2624 
2625  result = pstrdup((const char *) xmlBufferContent(buf));
2626  }
2627  PG_CATCH();
2628  {
2629  if (writer)
2630  xmlFreeTextWriter(writer);
2631  if (buf)
2632  xmlBufferFree(buf);
2633 
2634  pg_xml_done(xmlerrcxt, true);
2635 
2636  PG_RE_THROW();
2637  }
2638  PG_END_TRY();
2639 
2640  xmlBufferFree(buf);
2641 
2642  pg_xml_done(xmlerrcxt, false);
2643 
2644  return result;
2645  }
2646 #endif /* USE_LIBXML */
2647 
2648  }
2649 
2650  /*
2651  * otherwise, just use the type's native text representation
2652  */
2653  getTypeOutputInfo(type, &typeOut, &isvarlena);
2654  str = OidOutputFunctionCall(typeOut, value);
2655 
2656  /* ... exactly as-is for XML, and when escaping is not wanted */
2657  if (type == XMLOID || !xml_escape_strings)
2658  return str;
2659 
2660  /* otherwise, translate special characters as needed */
2661  return escape_xml(str);
2662  }
2663 }
#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:3619
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:4342
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
Definition: datetime.c:4227
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1902
signed short int16
Definition: c.h:493
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:1203
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#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 @155 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:2907
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2271
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2521
#define type_is_array_domain(typid)
Definition: lsyscache.h:211
char * pstrdup(const char *in)
Definition: mcxt.c:1696
void pfree(void *pointer)
Definition: mcxt.c:1521
#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:2674
char * map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
Definition: xml.c:2455
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(), 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 2413 of file xml.c.

2414 {
2416  const char *p;
2417 
2418  initStringInfo(&buf);
2419 
2420  for (p = name; *p; p += pg_mblen(p))
2421  {
2422  if (*p == '_' && *(p + 1) == 'x'
2423  && isxdigit((unsigned char) *(p + 2))
2424  && isxdigit((unsigned char) *(p + 3))
2425  && isxdigit((unsigned char) *(p + 4))
2426  && isxdigit((unsigned char) *(p + 5))
2427  && *(p + 6) == '_')
2428  {
2429  char cbuf[MAX_UNICODE_EQUIVALENT_STRING + 1];
2430  unsigned int u;
2431 
2432  sscanf(p + 2, "%X", &u);
2433  pg_unicode_to_server(u, (unsigned char *) cbuf);
2434  appendStringInfoString(&buf, cbuf);
2435  p += 6;
2436  }
2437  else
2439  }
2440 
2441  return buf.data;
2442 }
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:1592
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:692
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(), 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:1608
List * named_args
Definition: primnodes.h:1604
#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 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, 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  xmlAddChildList(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:1591
tree ctl root
Definition: radixtree.h:1886

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, root, 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().