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

References PG_DETOAST_DATUM.

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

◆ escape_xml()

char* escape_xml ( const char *  str)

Definition at line 2635 of file xml.c.

2636 {
2638  const char *p;
2639 
2640  initStringInfo(&buf);
2641  for (p = str; *p; p++)
2642  {
2643  switch (*p)
2644  {
2645  case '&':
2646  appendStringInfoString(&buf, "&");
2647  break;
2648  case '<':
2649  appendStringInfoString(&buf, "&lt;");
2650  break;
2651  case '>':
2652  appendStringInfoString(&buf, "&gt;");
2653  break;
2654  case '\r':
2655  appendStringInfoString(&buf, "&#x0d;");
2656  break;
2657  default:
2659  break;
2660  }
2661  }
2662  return buf.data;
2663 }
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:203

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

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

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

2417 {
2419  {
2420  ArrayType *array;
2421  Oid elmtype;
2422  int16 elmlen;
2423  bool elmbyval;
2424  char elmalign;
2425  int num_elems;
2426  Datum *elem_values;
2427  bool *elem_nulls;
2429  int i;
2430 
2431  array = DatumGetArrayTypeP(value);
2432  elmtype = ARR_ELEMTYPE(array);
2433  get_typlenbyvalalign(elmtype, &elmlen, &elmbyval, &elmalign);
2434 
2435  deconstruct_array(array, elmtype,
2436  elmlen, elmbyval, elmalign,
2437  &elem_values, &elem_nulls,
2438  &num_elems);
2439 
2440  initStringInfo(&buf);
2441 
2442  for (i = 0; i < num_elems; i++)
2443  {
2444  if (elem_nulls[i])
2445  continue;
2446  appendStringInfoString(&buf, "<element>");
2448  map_sql_value_to_xml_value(elem_values[i],
2449  elmtype, true));
2450  appendStringInfoString(&buf, "</element>");
2451  }
2452 
2453  pfree(elem_values);
2454  pfree(elem_nulls);
2455 
2456  return buf.data;
2457  }
2458  else
2459  {
2460  Oid typeOut;
2461  bool isvarlena;
2462  char *str;
2463 
2464  /*
2465  * Flatten domains; the special-case treatments below should apply to,
2466  * eg, domains over boolean not just boolean.
2467  */
2468  type = getBaseType(type);
2469 
2470  /*
2471  * Special XSD formatting for some data types
2472  */
2473  switch (type)
2474  {
2475  case BOOLOID:
2476  if (DatumGetBool(value))
2477  return "true";
2478  else
2479  return "false";
2480 
2481  case DATEOID:
2482  {
2483  DateADT date;
2484  struct pg_tm tm;
2485  char buf[MAXDATELEN + 1];
2486 
2488  /* XSD doesn't support infinite values */
2489  if (DATE_NOT_FINITE(date))
2490  ereport(ERROR,
2491  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2492  errmsg("date out of range"),
2493  errdetail("XML does not support infinite date values.")));
2495  &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
2497 
2498  return pstrdup(buf);
2499  }
2500 
2501  case TIMESTAMPOID:
2502  {
2504  struct pg_tm tm;
2505  fsec_t fsec;
2506  char buf[MAXDATELEN + 1];
2507 
2509 
2510  /* XSD doesn't support infinite values */
2512  ereport(ERROR,
2513  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2514  errmsg("timestamp out of range"),
2515  errdetail("XML does not support infinite timestamp values.")));
2516  else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
2517  EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
2518  else
2519  ereport(ERROR,
2520  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2521  errmsg("timestamp out of range")));
2522 
2523  return pstrdup(buf);
2524  }
2525 
2526  case TIMESTAMPTZOID:
2527  {
2529  struct pg_tm tm;
2530  int tz;
2531  fsec_t fsec;
2532  const char *tzn = NULL;
2533  char buf[MAXDATELEN + 1];
2534 
2536 
2537  /* XSD doesn't support infinite values */
2539  ereport(ERROR,
2540  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2541  errmsg("timestamp out of range"),
2542  errdetail("XML does not support infinite timestamp values.")));
2543  else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
2544  EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
2545  else
2546  ereport(ERROR,
2547  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2548  errmsg("timestamp out of range")));
2549 
2550  return pstrdup(buf);
2551  }
2552 
2553 #ifdef USE_LIBXML
2554  case BYTEAOID:
2555  {
2556  bytea *bstr = DatumGetByteaPP(value);
2557  PgXmlErrorContext *xmlerrcxt;
2558  volatile xmlBufferPtr buf = NULL;
2559  volatile xmlTextWriterPtr writer = NULL;
2560  char *result;
2561 
2562  xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
2563 
2564  PG_TRY();
2565  {
2566  buf = xmlBufferCreate();
2567  if (buf == NULL || xmlerrcxt->err_occurred)
2568  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
2569  "could not allocate xmlBuffer");
2570  writer = xmlNewTextWriterMemory(buf, 0);
2571  if (writer == NULL || xmlerrcxt->err_occurred)
2572  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
2573  "could not allocate xmlTextWriter");
2574 
2575  if (xmlbinary == XMLBINARY_BASE64)
2576  xmlTextWriterWriteBase64(writer, VARDATA_ANY(bstr),
2577  0, VARSIZE_ANY_EXHDR(bstr));
2578  else
2579  xmlTextWriterWriteBinHex(writer, VARDATA_ANY(bstr),
2580  0, VARSIZE_ANY_EXHDR(bstr));
2581 
2582  /* we MUST do this now to flush data out to the buffer */
2583  xmlFreeTextWriter(writer);
2584  writer = NULL;
2585 
2586  result = pstrdup((const char *) xmlBufferContent(buf));
2587  }
2588  PG_CATCH();
2589  {
2590  if (writer)
2591  xmlFreeTextWriter(writer);
2592  if (buf)
2593  xmlBufferFree(buf);
2594 
2595  pg_xml_done(xmlerrcxt, true);
2596 
2597  PG_RE_THROW();
2598  }
2599  PG_END_TRY();
2600 
2601  xmlBufferFree(buf);
2602 
2603  pg_xml_done(xmlerrcxt, false);
2604 
2605  return result;
2606  }
2607 #endif /* USE_LIBXML */
2608 
2609  }
2610 
2611  /*
2612  * otherwise, just use the type's native text representation
2613  */
2614  getTypeOutputInfo(type, &typeOut, &isvarlena);
2615  str = OidOutputFunctionCall(typeOut, value);
2616 
2617  /* ... exactly as-is for XML, and when escaping is not wanted */
2618  if (type == XMLOID || !xml_escape_strings)
2619  return str;
2620 
2621  /* otherwise, translate special characters as needed */
2622  return escape_xml(str);
2623  }
2624 }
#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:3613
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:313
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:4333
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
Definition: datetime.c:4218
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:482
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:1208
int errcode(int sqlerrcode)
Definition: elog.c:860
int errmsg(const char *fmt,...)
Definition: elog.c:1075
#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 @148 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:2862
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2226
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2476
#define type_is_array_domain(typid)
Definition: lsyscache.h:208
char * pstrdup(const char *in)
Definition: mcxt.c:1619
void pfree(void *pointer)
Definition: mcxt.c:1431
#define USE_XSD_DATES
Definition: miscadmin.h:236
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:110
char * escape_xml(const char *str)
Definition: xml.c:2635
char * map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
Definition: xml.c:2416
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 2374 of file xml.c.

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

1098 {
1099 #ifdef USE_LIBXML
1100  xmlDocPtr doc;
1101  ErrorSaveContext escontext = {T_ErrorSaveContext};
1102 
1103  /*
1104  * We'll report "true" if no soft error is reported by xml_parse().
1105  */
1106  doc = xml_parse((text *) arg, XMLOPTION_DOCUMENT, true,
1107  GetDatabaseEncoding(), NULL, NULL, (Node *) &escontext);
1108  if (doc)
1109  xmlFreeDoc(doc);
1110 
1111  return !escontext.error_occurred;
1112 #else /* not USE_LIBXML */
1113  NO_XML_SUPPORT();
1114  return false;
1115 #endif /* not USE_LIBXML */
1116 }
int GetDatabaseEncoding(void)
Definition: mbutils.c:1268
void * arg
@ XMLOPTION_DOCUMENT
Definition: primnodes.h:1527
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 554 of file xml.c.

555 {
556 #ifdef USE_LIBXML
557  int global_standalone = 1;
558  xmlChar *global_version = NULL;
559  bool global_version_no_value = false;
561  ListCell *v;
562 
564  foreach(v, args)
565  {
567  size_t len;
568  xmlChar *version;
569  int standalone;
570  char *str;
571 
572  len = VARSIZE(x) - VARHDRSZ;
573  str = text_to_cstring((text *) x);
574 
575  parse_xml_decl((xmlChar *) str, &len, &version, NULL, &standalone);
576 
577  if (standalone == 0 && global_standalone == 1)
578  global_standalone = 0;
579  if (standalone < 0)
580  global_standalone = -1;
581 
582  if (!version)
583  global_version_no_value = true;
584  else if (!global_version)
585  global_version = version;
586  else if (xmlStrcmp(version, global_version) != 0)
587  global_version_no_value = true;
588 
590  pfree(str);
591  }
592 
593  if (!global_version_no_value || global_standalone >= 0)
594  {
595  StringInfoData buf2;
596 
597  initStringInfo(&buf2);
598 
599  print_xml_decl(&buf2,
600  (!global_version_no_value) ? global_version : NULL,
601  0,
602  global_standalone);
603 
604  appendBinaryStringInfo(&buf2, buf.data, buf.len);
605  buf = buf2;
606  }
607 
608  return stringinfo_to_xmltype(&buf);
609 #else
610  NO_XML_SUPPORT();
611  return NULL;
612 #endif
613 }
#define VARHDRSZ
Definition: c.h:681
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:468
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 837 of file xml.c.

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

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

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

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

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

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

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 110 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 111 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 224 of file xml.c.

Referenced by ExecInitTableFuncScan().