PostgreSQL Source Code  git master
xml.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/table.h"
#include "catalog/namespace.h"
#include "catalog/pg_class.h"
#include "catalog/pg_type.h"
#include "commands/dbcommands.h"
#include "executor/spi.h"
#include "executor/tablefunc.h"
#include "fmgr.h"
#include "lib/stringinfo.h"
#include "libpq/pqformat.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "nodes/execnodes.h"
#include "nodes/miscnodes.h"
#include "nodes/nodeFuncs.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/datetime.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/xml.h"
Include dependency graph for xml.c:

Go to the source code of this file.

Macros

#define NO_XML_SUPPORT()
 
#define NAMESPACE_XSD   "http://www.w3.org/2001/XMLSchema"
 
#define NAMESPACE_XSI   "http://www.w3.org/2001/XMLSchema-instance"
 
#define NAMESPACE_SQLXML   "http://standards.iso.org/iso/9075/2003/sqlxml"
 
#define PG_XML_DEFAULT_VERSION   "1.0"
 
#define XML_VISIBLE_SCHEMAS_EXCLUDE   "(nspname ~ '^pg_' OR nspname = 'information_schema')"
 
#define XML_VISIBLE_SCHEMAS   "SELECT oid FROM pg_catalog.pg_namespace WHERE pg_catalog.has_schema_privilege (oid, 'USAGE') AND NOT " XML_VISIBLE_SCHEMAS_EXCLUDE
 

Functions

static void xmldata_root_element_start (StringInfo result, const char *eltname, const char *xmlschema, const char *targetns, bool top_level)
 
static void xmldata_root_element_end (StringInfo result, const char *eltname)
 
static StringInfo query_to_xml_internal (const char *query, char *tablename, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
 
static const char * map_sql_table_to_xmlschema (TupleDesc tupdesc, Oid relid, bool nulls, bool tableforest, const char *targetns)
 
static const char * map_sql_schema_to_xmlschema_types (Oid nspid, List *relid_list, bool nulls, bool tableforest, const char *targetns)
 
static const char * map_sql_catalog_to_xmlschema_types (List *nspid_list, bool nulls, bool tableforest, const char *targetns)
 
static const char * map_sql_type_to_xml_name (Oid typeoid, int typmod)
 
static const char * map_sql_typecoll_to_xmlschema_types (List *tupdesc_list)
 
static const char * map_sql_type_to_xmlschema_type (Oid typeoid, int typmod)
 
static void SPI_sql_row_to_xmlelement (uint64 rownum, StringInfo result, char *tablename, bool nulls, bool tableforest, const char *targetns, bool top_level)
 
static void XmlTableInitOpaque (struct TableFuncScanState *state, int natts)
 
static void XmlTableSetDocument (struct TableFuncScanState *state, Datum value)
 
static void XmlTableSetNamespace (struct TableFuncScanState *state, const char *name, const char *uri)
 
static void XmlTableSetRowFilter (struct TableFuncScanState *state, const char *path)
 
static void XmlTableSetColumnFilter (struct TableFuncScanState *state, const char *path, int colnum)
 
static bool XmlTableFetchRow (struct TableFuncScanState *state)
 
static Datum XmlTableGetValue (struct TableFuncScanState *state, int colnum, Oid typid, int32 typmod, bool *isnull)
 
static void XmlTableDestroyOpaque (struct TableFuncScanState *state)
 
Datum xml_in (PG_FUNCTION_ARGS)
 
static char * xml_out_internal (xmltype *x, pg_enc target_encoding)
 
Datum xml_out (PG_FUNCTION_ARGS)
 
Datum xml_recv (PG_FUNCTION_ARGS)
 
Datum xml_send (PG_FUNCTION_ARGS)
 
static xmltypestringinfo_to_xmltype (StringInfo buf)
 
static xmltypecstring_to_xmltype (const char *string)
 
Datum xmlcomment (PG_FUNCTION_ARGS)
 
Datum xmltext (PG_FUNCTION_ARGS)
 
xmltypexmlconcat (List *args)
 
Datum xmlconcat2 (PG_FUNCTION_ARGS)
 
Datum texttoxml (PG_FUNCTION_ARGS)
 
Datum xmltotext (PG_FUNCTION_ARGS)
 
textxmltotext_with_options (xmltype *data, XmlOptionType xmloption_arg, bool indent)
 
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)
 
Datum xmlvalidate (PG_FUNCTION_ARGS)
 
bool xml_is_document (xmltype *arg)
 
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)
 
char * escape_xml (const char *str)
 
static char * _SPI_strdup (const char *s)
 
static Listquery_to_oid_list (const char *query)
 
static Listschema_get_xml_visible_tables (Oid nspid)
 
static Listdatabase_get_xml_visible_schemas (void)
 
static Listdatabase_get_xml_visible_tables (void)
 
static StringInfo table_to_xml_internal (Oid relid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
 
Datum table_to_xml (PG_FUNCTION_ARGS)
 
Datum query_to_xml (PG_FUNCTION_ARGS)
 
Datum cursor_to_xml (PG_FUNCTION_ARGS)
 
Datum table_to_xmlschema (PG_FUNCTION_ARGS)
 
Datum query_to_xmlschema (PG_FUNCTION_ARGS)
 
Datum cursor_to_xmlschema (PG_FUNCTION_ARGS)
 
Datum table_to_xml_and_xmlschema (PG_FUNCTION_ARGS)
 
Datum query_to_xml_and_xmlschema (PG_FUNCTION_ARGS)
 
static StringInfo schema_to_xml_internal (Oid nspid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
 
Datum schema_to_xml (PG_FUNCTION_ARGS)
 
static void xsd_schema_element_start (StringInfo result, const char *targetns)
 
static void xsd_schema_element_end (StringInfo result)
 
static StringInfo schema_to_xmlschema_internal (const char *schemaname, bool nulls, bool tableforest, const char *targetns)
 
Datum schema_to_xmlschema (PG_FUNCTION_ARGS)
 
Datum schema_to_xml_and_xmlschema (PG_FUNCTION_ARGS)
 
static StringInfo database_to_xml_internal (const char *xmlschema, bool nulls, bool tableforest, const char *targetns)
 
Datum database_to_xml (PG_FUNCTION_ARGS)
 
static StringInfo database_to_xmlschema_internal (bool nulls, bool tableforest, const char *targetns)
 
Datum database_to_xmlschema (PG_FUNCTION_ARGS)
 
Datum database_to_xml_and_xmlschema (PG_FUNCTION_ARGS)
 
static char * map_multipart_sql_identifier_to_xml_name (const char *a, const char *b, const char *c, const char *d)
 
Datum xpath (PG_FUNCTION_ARGS)
 
Datum xmlexists (PG_FUNCTION_ARGS)
 
Datum xpath_exists (PG_FUNCTION_ARGS)
 
Datum xml_is_well_formed (PG_FUNCTION_ARGS)
 
Datum xml_is_well_formed_document (PG_FUNCTION_ARGS)
 
Datum xml_is_well_formed_content (PG_FUNCTION_ARGS)
 

Variables

int xmlbinary = XMLBINARY_BASE64
 
int xmloption = XMLOPTION_CONTENT
 
const TableFuncRoutine XmlTableRoutine
 

Macro Definition Documentation

◆ NAMESPACE_SQLXML

#define NAMESPACE_SQLXML   "http://standards.iso.org/iso/9075/2003/sqlxml"

Definition at line 245 of file xml.c.

◆ NAMESPACE_XSD

#define NAMESPACE_XSD   "http://www.w3.org/2001/XMLSchema"

Definition at line 243 of file xml.c.

◆ NAMESPACE_XSI

#define NAMESPACE_XSI   "http://www.w3.org/2001/XMLSchema-instance"

Definition at line 244 of file xml.c.

◆ NO_XML_SUPPORT

#define NO_XML_SUPPORT ( )
Value:
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
errmsg("unsupported XML feature"), \
errdetail("This functionality requires the server to be built with libxml support.")))
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 ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149

Definition at line 235 of file xml.c.

◆ PG_XML_DEFAULT_VERSION

#define PG_XML_DEFAULT_VERSION   "1.0"

Definition at line 301 of file xml.c.

◆ XML_VISIBLE_SCHEMAS

#define XML_VISIBLE_SCHEMAS   "SELECT oid FROM pg_catalog.pg_namespace WHERE pg_catalog.has_schema_privilege (oid, 'USAGE') AND NOT " XML_VISIBLE_SCHEMAS_EXCLUDE

Definition at line 2815 of file xml.c.

◆ XML_VISIBLE_SCHEMAS_EXCLUDE

#define XML_VISIBLE_SCHEMAS_EXCLUDE   "(nspname ~ '^pg_' OR nspname = 'information_schema')"

Definition at line 2813 of file xml.c.

Function Documentation

◆ _SPI_strdup()

static char* _SPI_strdup ( const char *  s)
static

Definition at line 2706 of file xml.c.

2707 {
2708  size_t len = strlen(s) + 1;
2709  char *ret = SPI_palloc(len);
2710 
2711  memcpy(ret, s, len);
2712  return ret;
2713 }
const void size_t len
void * SPI_palloc(Size size)
Definition: spi.c:1335

References len, and SPI_palloc().

Referenced by cursor_to_xmlschema(), query_to_xml_and_xmlschema(), and query_to_xmlschema().

◆ cstring_to_xmltype()

static xmltype* cstring_to_xmltype ( const char *  string)
static

Definition at line 474 of file xml.c.

475 {
476  return (xmltype *) cstring_to_text(string);
477 }
Definition: c.h:687
text * cstring_to_text(const char *s)
Definition: varlena.c:184

References cstring_to_text().

Referenced by cursor_to_xmlschema(), query_to_xmlschema(), and table_to_xmlschema().

◆ cursor_to_xml()

Datum cursor_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 2890 of file xml.c.

2891 {
2893  int32 count = PG_GETARG_INT32(1);
2894  bool nulls = PG_GETARG_BOOL(2);
2895  bool tableforest = PG_GETARG_BOOL(3);
2896  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(4));
2897 
2898  StringInfoData result;
2899  Portal portal;
2900  uint64 i;
2901 
2902  initStringInfo(&result);
2903 
2904  if (!tableforest)
2905  {
2906  xmldata_root_element_start(&result, "table", NULL, targetns, true);
2907  appendStringInfoChar(&result, '\n');
2908  }
2909 
2910  SPI_connect();
2911  portal = SPI_cursor_find(name);
2912  if (portal == NULL)
2913  ereport(ERROR,
2914  (errcode(ERRCODE_UNDEFINED_CURSOR),
2915  errmsg("cursor \"%s\" does not exist", name)));
2916 
2917  SPI_cursor_fetch(portal, true, count);
2918  for (i = 0; i < SPI_processed; i++)
2919  SPI_sql_row_to_xmlelement(i, &result, NULL, nulls,
2920  tableforest, targetns, true);
2921 
2922  SPI_finish();
2923 
2924  if (!tableforest)
2925  xmldata_root_element_end(&result, "table");
2926 
2928 }
signed int int32
Definition: c.h:494
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
int i
Definition: isn.c:73
uint64 SPI_processed
Definition: spi.c:44
Portal SPI_cursor_find(const char *name)
Definition: spi.c:1791
int SPI_connect(void)
Definition: spi.c:94
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Definition: spi.c:1803
int SPI_finish(void)
Definition: spi.c:182
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:194
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
char * text_to_cstring(const text *t)
Definition: varlena.c:217
const char * name
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:467
static void xmldata_root_element_start(StringInfo result, const char *eltname, const char *xmlschema, const char *targetns, bool top_level)
Definition: xml.c:2944
static void xmldata_root_element_end(StringInfo result, const char *eltname)
Definition: xml.c:2971
static void SPI_sql_row_to_xmlelement(uint64 rownum, StringInfo result, char *tablename, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:4063
#define PG_RETURN_XML_P(x)
Definition: xml.h:63

References appendStringInfoChar(), ereport, errcode(), errmsg(), ERROR, i, initStringInfo(), name, PG_GETARG_BOOL, PG_GETARG_INT32, PG_GETARG_TEXT_PP, PG_RETURN_XML_P, SPI_connect(), SPI_cursor_fetch(), SPI_cursor_find(), SPI_finish(), SPI_processed, SPI_sql_row_to_xmlelement(), stringinfo_to_xmltype(), text_to_cstring(), xmldata_root_element_end(), and xmldata_root_element_start().

◆ cursor_to_xmlschema()

Datum cursor_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 3071 of file xml.c.

3072 {
3074  bool nulls = PG_GETARG_BOOL(1);
3075  bool tableforest = PG_GETARG_BOOL(2);
3076  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
3077  const char *xmlschema;
3078  Portal portal;
3079 
3080  SPI_connect();
3081  portal = SPI_cursor_find(name);
3082  if (portal == NULL)
3083  ereport(ERROR,
3084  (errcode(ERRCODE_UNDEFINED_CURSOR),
3085  errmsg("cursor \"%s\" does not exist", name)));
3086  if (portal->tupDesc == NULL)
3087  ereport(ERROR,
3088  (errcode(ERRCODE_INVALID_CURSOR_STATE),
3089  errmsg("portal \"%s\" does not return tuples", name)));
3090 
3091  xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
3092  InvalidOid, nulls,
3093  tableforest, targetns));
3094  SPI_finish();
3095 
3096  PG_RETURN_XML_P(cstring_to_xmltype(xmlschema));
3097 }
#define InvalidOid
Definition: postgres_ext.h:36
TupleDesc tupDesc
Definition: portal.h:160
static char * _SPI_strdup(const char *s)
Definition: xml.c:2706
static xmltype * cstring_to_xmltype(const char *string)
Definition: xml.c:474
static const char * map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3493

References _SPI_strdup(), cstring_to_xmltype(), ereport, errcode(), errmsg(), ERROR, InvalidOid, map_sql_table_to_xmlschema(), name, PG_GETARG_BOOL, PG_GETARG_TEXT_PP, PG_RETURN_XML_P, SPI_connect(), SPI_cursor_find(), SPI_finish(), text_to_cstring(), and PortalData::tupDesc.

◆ database_get_xml_visible_schemas()

static List* database_get_xml_visible_schemas ( void  )
static

Definition at line 2819 of file xml.c.

2820 {
2821  return query_to_oid_list(XML_VISIBLE_SCHEMAS " ORDER BY nspname;");
2822 }
static List * query_to_oid_list(const char *query)
Definition: xml.c:2763
#define XML_VISIBLE_SCHEMAS
Definition: xml.c:2815

References query_to_oid_list(), and XML_VISIBLE_SCHEMAS.

Referenced by database_to_xml_internal(), and database_to_xmlschema_internal().

◆ database_get_xml_visible_tables()

static List* database_get_xml_visible_tables ( void  )
static

Definition at line 2826 of file xml.c.

2827 {
2828  /* At the moment there is no order required here. */
2829  return query_to_oid_list("SELECT oid FROM pg_catalog.pg_class"
2830  " WHERE relkind IN ("
2831  CppAsString2(RELKIND_RELATION) ","
2832  CppAsString2(RELKIND_MATVIEW) ","
2833  CppAsString2(RELKIND_VIEW) ")"
2834  " AND pg_catalog.has_table_privilege(pg_class.oid, 'SELECT')"
2835  " AND relnamespace IN (" XML_VISIBLE_SCHEMAS ");");
2836 }
#define CppAsString2(x)
Definition: c.h:327

References CppAsString2, query_to_oid_list(), and XML_VISIBLE_SCHEMAS.

Referenced by database_to_xmlschema_internal().

◆ database_to_xml()

Datum database_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 3376 of file xml.c.

3377 {
3378  bool nulls = PG_GETARG_BOOL(0);
3379  bool tableforest = PG_GETARG_BOOL(1);
3380  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
3381 
3383  tableforest, targetns)));
3384 }
static StringInfo database_to_xml_internal(const char *xmlschema, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3333

References database_to_xml_internal(), PG_GETARG_BOOL, PG_GETARG_TEXT_PP, PG_RETURN_XML_P, stringinfo_to_xmltype(), and text_to_cstring().

◆ database_to_xml_and_xmlschema()

Datum database_to_xml_and_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 3443 of file xml.c.

3444 {
3445  bool nulls = PG_GETARG_BOOL(0);
3446  bool tableforest = PG_GETARG_BOOL(1);
3447  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
3448  StringInfo xmlschema;
3449 
3450  xmlschema = database_to_xmlschema_internal(nulls, tableforest, targetns);
3451 
3453  nulls, tableforest, targetns)));
3454 }
static StringInfo database_to_xmlschema_internal(bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3388

References StringInfoData::data, database_to_xml_internal(), database_to_xmlschema_internal(), PG_GETARG_BOOL, PG_GETARG_TEXT_PP, PG_RETURN_XML_P, stringinfo_to_xmltype(), and text_to_cstring().

◆ database_to_xml_internal()

static StringInfo database_to_xml_internal ( const char *  xmlschema,
bool  nulls,
bool  tableforest,
const char *  targetns 
)
static

Definition at line 3333 of file xml.c.

3335 {
3336  StringInfo result;
3337  List *nspid_list;
3338  ListCell *cell;
3339  char *xmlcn;
3340 
3342  true, false);
3343  result = makeStringInfo();
3344 
3345  xmldata_root_element_start(result, xmlcn, xmlschema, targetns, true);
3346  appendStringInfoChar(result, '\n');
3347 
3348  if (xmlschema)
3349  appendStringInfo(result, "%s\n\n", xmlschema);
3350 
3351  SPI_connect();
3352 
3353  nspid_list = database_get_xml_visible_schemas();
3354 
3355  foreach(cell, nspid_list)
3356  {
3357  Oid nspid = lfirst_oid(cell);
3358  StringInfo subres;
3359 
3360  subres = schema_to_xml_internal(nspid, NULL, nulls,
3361  tableforest, targetns, false);
3362 
3363  appendBinaryStringInfo(result, subres->data, subres->len);
3364  appendStringInfoChar(result, '\n');
3365  }
3366 
3367  SPI_finish();
3368 
3369  xmldata_root_element_end(result, xmlcn);
3370 
3371  return result;
3372 }
int nspid
char * get_database_name(Oid dbid)
Definition: dbcommands.c:3166
Oid MyDatabaseId
Definition: globals.c:92
#define lfirst_oid(lc)
Definition: pg_list.h:174
unsigned int Oid
Definition: postgres_ext.h:31
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:97
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
Definition: stringinfo.c:233
Definition: pg_list.h:54
char * map_sql_identifier_to_xml_name(const char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:2357
static StringInfo schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:3158
static List * database_get_xml_visible_schemas(void)
Definition: xml.c:2819

References appendBinaryStringInfo(), appendStringInfo(), appendStringInfoChar(), StringInfoData::data, database_get_xml_visible_schemas(), get_database_name(), StringInfoData::len, lfirst_oid, makeStringInfo(), map_sql_identifier_to_xml_name(), MyDatabaseId, nspid, schema_to_xml_internal(), SPI_connect(), SPI_finish(), xmldata_root_element_end(), and xmldata_root_element_start().

Referenced by database_to_xml(), and database_to_xml_and_xmlschema().

◆ database_to_xmlschema()

Datum database_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 3431 of file xml.c.

3432 {
3433  bool nulls = PG_GETARG_BOOL(0);
3434  bool tableforest = PG_GETARG_BOOL(1);
3435  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
3436 
3438  tableforest, targetns)));
3439 }

References database_to_xmlschema_internal(), PG_GETARG_BOOL, PG_GETARG_TEXT_PP, PG_RETURN_XML_P, stringinfo_to_xmltype(), and text_to_cstring().

◆ database_to_xmlschema_internal()

static StringInfo database_to_xmlschema_internal ( bool  nulls,
bool  tableforest,
const char *  targetns 
)
static

Definition at line 3388 of file xml.c.

3390 {
3391  List *relid_list;
3392  List *nspid_list;
3393  List *tupdesc_list;
3394  ListCell *cell;
3395  StringInfo result;
3396 
3397  result = makeStringInfo();
3398 
3399  xsd_schema_element_start(result, targetns);
3400 
3401  SPI_connect();
3402 
3403  relid_list = database_get_xml_visible_tables();
3404  nspid_list = database_get_xml_visible_schemas();
3405 
3406  tupdesc_list = NIL;
3407  foreach(cell, relid_list)
3408  {
3409  Relation rel;
3410 
3411  rel = table_open(lfirst_oid(cell), AccessShareLock);
3412  tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
3413  table_close(rel, NoLock);
3414  }
3415 
3416  appendStringInfoString(result,
3417  map_sql_typecoll_to_xmlschema_types(tupdesc_list));
3418 
3419  appendStringInfoString(result,
3420  map_sql_catalog_to_xmlschema_types(nspid_list, nulls, tableforest, targetns));
3421 
3422  xsd_schema_element_end(result);
3423 
3424  SPI_finish();
3425 
3426  return result;
3427 }
List * lappend(List *list, void *datum)
Definition: list.c:339
#define NoLock
Definition: lockdefs.h:34
#define AccessShareLock
Definition: lockdefs.h:36
#define NIL
Definition: pg_list.h:68
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:182
TupleDesc rd_att
Definition: rel.h:112
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:133
static List * database_get_xml_visible_tables(void)
Definition: xml.c:2826
static const char * map_sql_catalog_to_xmlschema_types(List *nspid_list, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3671
static void xsd_schema_element_start(StringInfo result, const char *targetns)
Definition: xml.c:3223
static const char * map_sql_typecoll_to_xmlschema_types(List *tupdesc_list)
Definition: xml.c:3833
static void xsd_schema_element_end(StringInfo result)
Definition: xml.c:3240

References AccessShareLock, appendStringInfoString(), CreateTupleDescCopy(), database_get_xml_visible_schemas(), database_get_xml_visible_tables(), lappend(), lfirst_oid, makeStringInfo(), map_sql_catalog_to_xmlschema_types(), map_sql_typecoll_to_xmlschema_types(), NIL, NoLock, RelationData::rd_att, SPI_connect(), SPI_finish(), table_close(), table_open(), xsd_schema_element_end(), and xsd_schema_element_start().

Referenced by database_to_xml_and_xmlschema(), and database_to_xmlschema().

◆ 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, "&amp;");
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
#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_multipart_sql_identifier_to_xml_name()

static char* map_multipart_sql_identifier_to_xml_name ( const char *  a,
const char *  b,
const char *  c,
const char *  d 
)
static

Definition at line 3462 of file xml.c.

3463 {
3464  StringInfoData result;
3465 
3466  initStringInfo(&result);
3467 
3468  if (a)
3469  appendStringInfoString(&result,
3470  map_sql_identifier_to_xml_name(a, true, true));
3471  if (b)
3472  appendStringInfo(&result, ".%s",
3473  map_sql_identifier_to_xml_name(b, true, true));
3474  if (c)
3475  appendStringInfo(&result, ".%s",
3476  map_sql_identifier_to_xml_name(c, true, true));
3477  if (d)
3478  appendStringInfo(&result, ".%s",
3479  map_sql_identifier_to_xml_name(d, true, true));
3480 
3481  return result.data;
3482 }
int b
Definition: isn.c:70
int a
Definition: isn.c:69
char * c

References a, appendStringInfo(), appendStringInfoString(), b, StringInfoData::data, initStringInfo(), and map_sql_identifier_to_xml_name().

Referenced by map_sql_catalog_to_xmlschema_types(), map_sql_schema_to_xmlschema_types(), map_sql_table_to_xmlschema(), and map_sql_type_to_xml_name().

◆ map_sql_catalog_to_xmlschema_types()

static const char * map_sql_catalog_to_xmlschema_types ( List nspid_list,
bool  nulls,
bool  tableforest,
const char *  targetns 
)
static

Definition at line 3671 of file xml.c.

3673 {
3674  char *dbname;
3675  char *xmlcn;
3676  char *catalogtypename;
3677  StringInfoData result;
3678  ListCell *cell;
3679 
3681 
3682  initStringInfo(&result);
3683 
3684  xmlcn = map_sql_identifier_to_xml_name(dbname, true, false);
3685 
3686  catalogtypename = map_multipart_sql_identifier_to_xml_name("CatalogType",
3687  dbname,
3688  NULL,
3689  NULL);
3690 
3691  appendStringInfo(&result,
3692  "<xsd:complexType name=\"%s\">\n", catalogtypename);
3693  appendStringInfoString(&result,
3694  " <xsd:all>\n");
3695 
3696  foreach(cell, nspid_list)
3697  {
3698  Oid nspid = lfirst_oid(cell);
3699  char *nspname = get_namespace_name(nspid);
3700  char *xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
3701  char *schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
3702  dbname,
3703  nspname,
3704  NULL);
3705 
3706  appendStringInfo(&result,
3707  " <xsd:element name=\"%s\" type=\"%s\"/>\n",
3708  xmlsn, schematypename);
3709  }
3710 
3711  appendStringInfoString(&result,
3712  " </xsd:all>\n");
3713  appendStringInfoString(&result,
3714  "</xsd:complexType>\n\n");
3715 
3716  appendStringInfo(&result,
3717  "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3718  xmlcn, catalogtypename);
3719 
3720  return result.data;
3721 }
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3366
char * dbname
Definition: streamutil.c:52
static char * map_multipart_sql_identifier_to_xml_name(const char *a, const char *b, const char *c, const char *d)
Definition: xml.c:3462

References appendStringInfo(), appendStringInfoString(), StringInfoData::data, dbname, get_database_name(), get_namespace_name(), initStringInfo(), lfirst_oid, map_multipart_sql_identifier_to_xml_name(), map_sql_identifier_to_xml_name(), MyDatabaseId, and nspid.

Referenced by database_to_xmlschema_internal().

◆ 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
#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_schema_to_xmlschema_types()

static const char * map_sql_schema_to_xmlschema_types ( Oid  nspid,
List relid_list,
bool  nulls,
bool  tableforest,
const char *  targetns 
)
static

Definition at line 3598 of file xml.c.

3600 {
3601  char *dbname;
3602  char *nspname;
3603  char *xmlsn;
3604  char *schematypename;
3605  StringInfoData result;
3606  ListCell *cell;
3607 
3609  nspname = get_namespace_name(nspid);
3610 
3611  initStringInfo(&result);
3612 
3613  xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
3614 
3615  schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
3616  dbname,
3617  nspname,
3618  NULL);
3619 
3620  appendStringInfo(&result,
3621  "<xsd:complexType name=\"%s\">\n", schematypename);
3622  if (!tableforest)
3623  appendStringInfoString(&result,
3624  " <xsd:all>\n");
3625  else
3626  appendStringInfoString(&result,
3627  " <xsd:sequence>\n");
3628 
3629  foreach(cell, relid_list)
3630  {
3631  Oid relid = lfirst_oid(cell);
3632  char *relname = get_rel_name(relid);
3633  char *xmltn = map_sql_identifier_to_xml_name(relname, true, false);
3634  char *tabletypename = map_multipart_sql_identifier_to_xml_name(tableforest ? "RowType" : "TableType",
3635  dbname,
3636  nspname,
3637  relname);
3638 
3639  if (!tableforest)
3640  appendStringInfo(&result,
3641  " <xsd:element name=\"%s\" type=\"%s\"/>\n",
3642  xmltn, tabletypename);
3643  else
3644  appendStringInfo(&result,
3645  " <xsd:element name=\"%s\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n",
3646  xmltn, tabletypename);
3647  }
3648 
3649  if (!tableforest)
3650  appendStringInfoString(&result,
3651  " </xsd:all>\n");
3652  else
3653  appendStringInfoString(&result,
3654  " </xsd:sequence>\n");
3655  appendStringInfoString(&result,
3656  "</xsd:complexType>\n\n");
3657 
3658  appendStringInfo(&result,
3659  "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3660  xmlsn, schematypename);
3661 
3662  return result.data;
3663 }
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1928
NameData relname
Definition: pg_class.h:38

References appendStringInfo(), appendStringInfoString(), StringInfoData::data, dbname, get_database_name(), get_namespace_name(), get_rel_name(), initStringInfo(), lfirst_oid, map_multipart_sql_identifier_to_xml_name(), map_sql_identifier_to_xml_name(), MyDatabaseId, nspid, and relname.

Referenced by schema_to_xmlschema_internal().

◆ map_sql_table_to_xmlschema()

static const char * map_sql_table_to_xmlschema ( TupleDesc  tupdesc,
Oid  relid,
bool  nulls,
bool  tableforest,
const char *  targetns 
)
static

Definition at line 3493 of file xml.c.

3495 {
3496  int i;
3497  char *xmltn;
3498  char *tabletypename;
3499  char *rowtypename;
3500  StringInfoData result;
3501 
3502  initStringInfo(&result);
3503 
3504  if (OidIsValid(relid))
3505  {
3506  HeapTuple tuple;
3507  Form_pg_class reltuple;
3508 
3509  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
3510  if (!HeapTupleIsValid(tuple))
3511  elog(ERROR, "cache lookup failed for relation %u", relid);
3512  reltuple = (Form_pg_class) GETSTRUCT(tuple);
3513 
3514  xmltn = map_sql_identifier_to_xml_name(NameStr(reltuple->relname),
3515  true, false);
3516 
3517  tabletypename = map_multipart_sql_identifier_to_xml_name("TableType",
3519  get_namespace_name(reltuple->relnamespace),
3520  NameStr(reltuple->relname));
3521 
3522  rowtypename = map_multipart_sql_identifier_to_xml_name("RowType",
3524  get_namespace_name(reltuple->relnamespace),
3525  NameStr(reltuple->relname));
3526 
3527  ReleaseSysCache(tuple);
3528  }
3529  else
3530  {
3531  if (tableforest)
3532  xmltn = "row";
3533  else
3534  xmltn = "table";
3535 
3536  tabletypename = "TableType";
3537  rowtypename = "RowType";
3538  }
3539 
3540  xsd_schema_element_start(&result, targetns);
3541 
3542  appendStringInfoString(&result,
3544 
3545  appendStringInfo(&result,
3546  "<xsd:complexType name=\"%s\">\n"
3547  " <xsd:sequence>\n",
3548  rowtypename);
3549 
3550  for (i = 0; i < tupdesc->natts; i++)
3551  {
3552  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
3553 
3554  if (att->attisdropped)
3555  continue;
3556  appendStringInfo(&result,
3557  " <xsd:element name=\"%s\" type=\"%s\"%s></xsd:element>\n",
3559  true, false),
3560  map_sql_type_to_xml_name(att->atttypid, -1),
3561  nulls ? " nillable=\"true\"" : " minOccurs=\"0\"");
3562  }
3563 
3564  appendStringInfoString(&result,
3565  " </xsd:sequence>\n"
3566  "</xsd:complexType>\n\n");
3567 
3568  if (!tableforest)
3569  {
3570  appendStringInfo(&result,
3571  "<xsd:complexType name=\"%s\">\n"
3572  " <xsd:sequence>\n"
3573  " <xsd:element name=\"row\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n"
3574  " </xsd:sequence>\n"
3575  "</xsd:complexType>\n\n",
3576  tabletypename, rowtypename);
3577 
3578  appendStringInfo(&result,
3579  "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3580  xmltn, tabletypename);
3581  }
3582  else
3583  appendStringInfo(&result,
3584  "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3585  xmltn, rowtypename);
3586 
3587  xsd_schema_element_end(&result);
3588 
3589  return result.data;
3590 }
#define NameStr(name)
Definition: c.h:746
#define OidIsValid(objectId)
Definition: c.h:775
#define elog(elevel,...)
Definition: elog.h:224
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
#define list_make1(x1)
Definition: pg_list.h:212
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:266
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:218
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static const char * map_sql_type_to_xml_name(Oid typeoid, int typmod)
Definition: xml.c:3728

References appendStringInfo(), appendStringInfoString(), StringInfoData::data, elog, ERROR, get_database_name(), get_namespace_name(), GETSTRUCT, HeapTupleIsValid, i, initStringInfo(), list_make1, map_multipart_sql_identifier_to_xml_name(), map_sql_identifier_to_xml_name(), map_sql_type_to_xml_name(), map_sql_typecoll_to_xmlschema_types(), MyDatabaseId, NameStr, TupleDescData::natts, ObjectIdGetDatum(), OidIsValid, ReleaseSysCache(), SearchSysCache1(), TupleDescAttr, xsd_schema_element_end(), and xsd_schema_element_start().

Referenced by cursor_to_xmlschema(), query_to_xml_and_xmlschema(), query_to_xmlschema(), table_to_xml_and_xmlschema(), and table_to_xmlschema().

◆ map_sql_type_to_xml_name()

static const char * map_sql_type_to_xml_name ( Oid  typeoid,
int  typmod 
)
static

Definition at line 3728 of file xml.c.

3729 {
3730  StringInfoData result;
3731 
3732  initStringInfo(&result);
3733 
3734  switch (typeoid)
3735  {
3736  case BPCHAROID:
3737  if (typmod == -1)
3738  appendStringInfoString(&result, "CHAR");
3739  else
3740  appendStringInfo(&result, "CHAR_%d", typmod - VARHDRSZ);
3741  break;
3742  case VARCHAROID:
3743  if (typmod == -1)
3744  appendStringInfoString(&result, "VARCHAR");
3745  else
3746  appendStringInfo(&result, "VARCHAR_%d", typmod - VARHDRSZ);
3747  break;
3748  case NUMERICOID:
3749  if (typmod == -1)
3750  appendStringInfoString(&result, "NUMERIC");
3751  else
3752  appendStringInfo(&result, "NUMERIC_%d_%d",
3753  ((typmod - VARHDRSZ) >> 16) & 0xffff,
3754  (typmod - VARHDRSZ) & 0xffff);
3755  break;
3756  case INT4OID:
3757  appendStringInfoString(&result, "INTEGER");
3758  break;
3759  case INT2OID:
3760  appendStringInfoString(&result, "SMALLINT");
3761  break;
3762  case INT8OID:
3763  appendStringInfoString(&result, "BIGINT");
3764  break;
3765  case FLOAT4OID:
3766  appendStringInfoString(&result, "REAL");
3767  break;
3768  case FLOAT8OID:
3769  appendStringInfoString(&result, "DOUBLE");
3770  break;
3771  case BOOLOID:
3772  appendStringInfoString(&result, "BOOLEAN");
3773  break;
3774  case TIMEOID:
3775  if (typmod == -1)
3776  appendStringInfoString(&result, "TIME");
3777  else
3778  appendStringInfo(&result, "TIME_%d", typmod);
3779  break;
3780  case TIMETZOID:
3781  if (typmod == -1)
3782  appendStringInfoString(&result, "TIME_WTZ");
3783  else
3784  appendStringInfo(&result, "TIME_WTZ_%d", typmod);
3785  break;
3786  case TIMESTAMPOID:
3787  if (typmod == -1)
3788  appendStringInfoString(&result, "TIMESTAMP");
3789  else
3790  appendStringInfo(&result, "TIMESTAMP_%d", typmod);
3791  break;
3792  case TIMESTAMPTZOID:
3793  if (typmod == -1)
3794  appendStringInfoString(&result, "TIMESTAMP_WTZ");
3795  else
3796  appendStringInfo(&result, "TIMESTAMP_WTZ_%d", typmod);
3797  break;
3798  case DATEOID:
3799  appendStringInfoString(&result, "DATE");
3800  break;
3801  case XMLOID:
3802  appendStringInfoString(&result, "XML");
3803  break;
3804  default:
3805  {
3806  HeapTuple tuple;
3807  Form_pg_type typtuple;
3808 
3809  tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typeoid));
3810  if (!HeapTupleIsValid(tuple))
3811  elog(ERROR, "cache lookup failed for type %u", typeoid);
3812  typtuple = (Form_pg_type) GETSTRUCT(tuple);
3813 
3814  appendStringInfoString(&result,
3815  map_multipart_sql_identifier_to_xml_name((typtuple->typtype == TYPTYPE_DOMAIN) ? "Domain" : "UDT",
3817  get_namespace_name(typtuple->typnamespace),
3818  NameStr(typtuple->typname)));
3819 
3820  ReleaseSysCache(tuple);
3821  }
3822  }
3823 
3824  return result.data;
3825 }
#define VARHDRSZ
Definition: c.h:692
FormData_pg_type * Form_pg_type
Definition: pg_type.h:261

References appendStringInfo(), appendStringInfoString(), StringInfoData::data, elog, ERROR, get_database_name(), get_namespace_name(), GETSTRUCT, HeapTupleIsValid, initStringInfo(), map_multipart_sql_identifier_to_xml_name(), MyDatabaseId, NameStr, ObjectIdGetDatum(), ReleaseSysCache(), SearchSysCache1(), and VARHDRSZ.

Referenced by map_sql_table_to_xmlschema(), and map_sql_type_to_xmlschema_type().

◆ map_sql_type_to_xmlschema_type()

static const char * map_sql_type_to_xmlschema_type ( Oid  typeoid,
int  typmod 
)
static

Definition at line 3888 of file xml.c.

3889 {
3890  StringInfoData result;
3891  const char *typename = map_sql_type_to_xml_name(typeoid, typmod);
3892 
3893  initStringInfo(&result);
3894 
3895  if (typeoid == XMLOID)
3896  {
3897  appendStringInfoString(&result,
3898  "<xsd:complexType mixed=\"true\">\n"
3899  " <xsd:sequence>\n"
3900  " <xsd:any name=\"element\" minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>\n"
3901  " </xsd:sequence>\n"
3902  "</xsd:complexType>\n");
3903  }
3904  else
3905  {
3906  appendStringInfo(&result,
3907  "<xsd:simpleType name=\"%s\">\n", typename);
3908 
3909  switch (typeoid)
3910  {
3911  case BPCHAROID:
3912  case VARCHAROID:
3913  case TEXTOID:
3914  appendStringInfoString(&result,
3915  " <xsd:restriction base=\"xsd:string\">\n");
3916  if (typmod != -1)
3917  appendStringInfo(&result,
3918  " <xsd:maxLength value=\"%d\"/>\n",
3919  typmod - VARHDRSZ);
3920  appendStringInfoString(&result, " </xsd:restriction>\n");
3921  break;
3922 
3923  case BYTEAOID:
3924  appendStringInfo(&result,
3925  " <xsd:restriction base=\"xsd:%s\">\n"
3926  " </xsd:restriction>\n",
3927  xmlbinary == XMLBINARY_BASE64 ? "base64Binary" : "hexBinary");
3928  break;
3929 
3930  case NUMERICOID:
3931  if (typmod != -1)
3932  appendStringInfo(&result,
3933  " <xsd:restriction base=\"xsd:decimal\">\n"
3934  " <xsd:totalDigits value=\"%d\"/>\n"
3935  " <xsd:fractionDigits value=\"%d\"/>\n"
3936  " </xsd:restriction>\n",
3937  ((typmod - VARHDRSZ) >> 16) & 0xffff,
3938  (typmod - VARHDRSZ) & 0xffff);
3939  break;
3940 
3941  case INT2OID:
3942  appendStringInfo(&result,
3943  " <xsd:restriction base=\"xsd:short\">\n"
3944  " <xsd:maxInclusive value=\"%d\"/>\n"
3945  " <xsd:minInclusive value=\"%d\"/>\n"
3946  " </xsd:restriction>\n",
3947  SHRT_MAX, SHRT_MIN);
3948  break;
3949 
3950  case INT4OID:
3951  appendStringInfo(&result,
3952  " <xsd:restriction base=\"xsd:int\">\n"
3953  " <xsd:maxInclusive value=\"%d\"/>\n"
3954  " <xsd:minInclusive value=\"%d\"/>\n"
3955  " </xsd:restriction>\n",
3956  INT_MAX, INT_MIN);
3957  break;
3958 
3959  case INT8OID:
3960  appendStringInfo(&result,
3961  " <xsd:restriction base=\"xsd:long\">\n"
3962  " <xsd:maxInclusive value=\"" INT64_FORMAT "\"/>\n"
3963  " <xsd:minInclusive value=\"" INT64_FORMAT "\"/>\n"
3964  " </xsd:restriction>\n",
3965  PG_INT64_MAX,
3966  PG_INT64_MIN);
3967  break;
3968 
3969  case FLOAT4OID:
3970  appendStringInfoString(&result,
3971  " <xsd:restriction base=\"xsd:float\"></xsd:restriction>\n");
3972  break;
3973 
3974  case FLOAT8OID:
3975  appendStringInfoString(&result,
3976  " <xsd:restriction base=\"xsd:double\"></xsd:restriction>\n");
3977  break;
3978 
3979  case BOOLOID:
3980  appendStringInfoString(&result,
3981  " <xsd:restriction base=\"xsd:boolean\"></xsd:restriction>\n");
3982  break;
3983 
3984  case TIMEOID:
3985  case TIMETZOID:
3986  {
3987  const char *tz = (typeoid == TIMETZOID ? "(\\+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
3988 
3989  if (typmod == -1)
3990  appendStringInfo(&result,
3991  " <xsd:restriction base=\"xsd:time\">\n"
3992  " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}(.\\p{Nd}+)?%s\"/>\n"
3993  " </xsd:restriction>\n", tz);
3994  else if (typmod == 0)
3995  appendStringInfo(&result,
3996  " <xsd:restriction base=\"xsd:time\">\n"
3997  " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3998  " </xsd:restriction>\n", tz);
3999  else
4000  appendStringInfo(&result,
4001  " <xsd:restriction base=\"xsd:time\">\n"
4002  " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}.\\p{Nd}{%d}%s\"/>\n"
4003  " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
4004  break;
4005  }
4006 
4007  case TIMESTAMPOID:
4008  case TIMESTAMPTZOID:
4009  {
4010  const char *tz = (typeoid == TIMESTAMPTZOID ? "(\\+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
4011 
4012  if (typmod == -1)
4013  appendStringInfo(&result,
4014  " <xsd:restriction base=\"xsd:dateTime\">\n"
4015  " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}(.\\p{Nd}+)?%s\"/>\n"
4016  " </xsd:restriction>\n", tz);
4017  else if (typmod == 0)
4018  appendStringInfo(&result,
4019  " <xsd:restriction base=\"xsd:dateTime\">\n"
4020  " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
4021  " </xsd:restriction>\n", tz);
4022  else
4023  appendStringInfo(&result,
4024  " <xsd:restriction base=\"xsd:dateTime\">\n"
4025  " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}.\\p{Nd}{%d}%s\"/>\n"
4026  " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
4027  break;
4028  }
4029 
4030  case DATEOID:
4031  appendStringInfoString(&result,
4032  " <xsd:restriction base=\"xsd:date\">\n"
4033  " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}\"/>\n"
4034  " </xsd:restriction>\n");
4035  break;
4036 
4037  default:
4038  if (get_typtype(typeoid) == TYPTYPE_DOMAIN)
4039  {
4040  Oid base_typeoid;
4041  int32 base_typmod = -1;
4042 
4043  base_typeoid = getBaseTypeAndTypmod(typeoid, &base_typmod);
4044 
4045  appendStringInfo(&result,
4046  " <xsd:restriction base=\"%s\"/>\n",
4047  map_sql_type_to_xml_name(base_typeoid, base_typmod));
4048  }
4049  break;
4050  }
4051  appendStringInfoString(&result, "</xsd:simpleType>\n");
4052  }
4053 
4054  return result.data;
4055 }
#define INT64_FORMAT
Definition: c.h:548
#define PG_INT64_MAX
Definition: c.h:592
#define PG_INT64_MIN
Definition: c.h:591
char get_typtype(Oid typid)
Definition: lsyscache.c:2629
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2538
int xmlbinary
Definition: xml.c:109
@ XMLBINARY_BASE64
Definition: xml.h:35

References appendStringInfo(), appendStringInfoString(), StringInfoData::data, get_typtype(), getBaseTypeAndTypmod(), initStringInfo(), INT64_FORMAT, map_sql_type_to_xml_name(), PG_INT64_MAX, PG_INT64_MIN, VARHDRSZ, xmlbinary, and XMLBINARY_BASE64.

Referenced by map_sql_typecoll_to_xmlschema_types().

◆ map_sql_typecoll_to_xmlschema_types()

static const char * map_sql_typecoll_to_xmlschema_types ( List tupdesc_list)
static

Definition at line 3833 of file xml.c.

3834 {
3835  List *uniquetypes = NIL;
3836  int i;
3837  StringInfoData result;
3838  ListCell *cell0;
3839 
3840  /* extract all column types used in the set of TupleDescs */
3841  foreach(cell0, tupdesc_list)
3842  {
3843  TupleDesc tupdesc = (TupleDesc) lfirst(cell0);
3844 
3845  for (i = 0; i < tupdesc->natts; i++)
3846  {
3847  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
3848 
3849  if (att->attisdropped)
3850  continue;
3851  uniquetypes = list_append_unique_oid(uniquetypes, att->atttypid);
3852  }
3853  }
3854 
3855  /* add base types of domains */
3856  foreach(cell0, uniquetypes)
3857  {
3858  Oid typid = lfirst_oid(cell0);
3859  Oid basetypid = getBaseType(typid);
3860 
3861  if (basetypid != typid)
3862  uniquetypes = list_append_unique_oid(uniquetypes, basetypid);
3863  }
3864 
3865  /* Convert to textual form */
3866  initStringInfo(&result);
3867 
3868  foreach(cell0, uniquetypes)
3869  {
3870  appendStringInfo(&result, "%s\n",
3872  -1));
3873  }
3874 
3875  return result.data;
3876 }
List * list_append_unique_oid(List *list, Oid datum)
Definition: list.c:1380
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2521
#define lfirst(lc)
Definition: pg_list.h:172
struct TupleDescData * TupleDesc
Definition: tupdesc.h:89
static const char * map_sql_type_to_xmlschema_type(Oid typeoid, int typmod)
Definition: xml.c:3888

References appendStringInfo(), StringInfoData::data, getBaseType(), i, initStringInfo(), lfirst, lfirst_oid, list_append_unique_oid(), map_sql_type_to_xmlschema_type(), TupleDescData::natts, NIL, and TupleDescAttr.

Referenced by database_to_xmlschema_internal(), map_sql_table_to_xmlschema(), and schema_to_xmlschema_internal().

◆ 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
#define PG_RE_THROW()
Definition: elog.h:411
#define PG_TRY(...)
Definition: elog.h:370
#define PG_END_TRY(...)
Definition: elog.h:395
#define PG_CATCH(...)
Definition: elog.h:380
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
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
#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
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
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)
@ PG_XML_STRICTNESS_ALL
Definition: xml.h:44

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

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

Referenced by get_rule_expr().

◆ query_to_oid_list()

static List* query_to_oid_list ( const char *  query)
static

Definition at line 2763 of file xml.c.

2764 {
2765  uint64 i;
2766  List *list = NIL;
2767  int spi_result;
2768 
2769  spi_result = SPI_execute(query, true, 0);
2770  if (spi_result != SPI_OK_SELECT)
2771  elog(ERROR, "SPI_execute returned %s for %s",
2772  SPI_result_code_string(spi_result), query);
2773 
2774  for (i = 0; i < SPI_processed; i++)
2775  {
2776  Datum oid;
2777  bool isnull;
2778 
2779  oid = SPI_getbinval(SPI_tuptable->vals[i],
2781  1,
2782  &isnull);
2783  if (!isnull)
2785  }
2786 
2787  return list;
2788 }
List * lappend_oid(List *list, Oid datum)
Definition: list.c:375
static Oid DatumGetObjectId(Datum X)
Definition: postgres.h:242
SPITupleTable * SPI_tuptable
Definition: spi.c:45
const char * SPI_result_code_string(int code)
Definition: spi.c:1969
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:593
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition: spi.c:1249
#define SPI_OK_SELECT
Definition: spi.h:86
TupleDesc tupdesc
Definition: spi.h:25
HeapTuple * vals
Definition: spi.h:26

References DatumGetObjectId(), elog, ERROR, i, lappend_oid(), sort-test::list, NIL, SPI_execute(), SPI_getbinval(), SPI_OK_SELECT, SPI_processed, SPI_result_code_string(), SPI_tuptable, SPITupleTable::tupdesc, and SPITupleTable::vals.

Referenced by database_get_xml_visible_schemas(), database_get_xml_visible_tables(), and schema_get_xml_visible_tables().

◆ query_to_xml()

Datum query_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 2876 of file xml.c.

2877 {
2878  char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2879  bool nulls = PG_GETARG_BOOL(1);
2880  bool tableforest = PG_GETARG_BOOL(2);
2881  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2882 
2884  NULL, nulls, tableforest,
2885  targetns, true)));
2886 }
static StringInfo query_to_xml_internal(const char *query, char *tablename, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2978

References PG_GETARG_BOOL, PG_GETARG_TEXT_PP, PG_RETURN_XML_P, query_to_xml_internal(), stringinfo_to_xmltype(), and text_to_cstring().

◆ query_to_xml_and_xmlschema()

Datum query_to_xml_and_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 3122 of file xml.c.

3123 {
3124  char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
3125  bool nulls = PG_GETARG_BOOL(1);
3126  bool tableforest = PG_GETARG_BOOL(2);
3127  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
3128 
3129  const char *xmlschema;
3130  SPIPlanPtr plan;
3131  Portal portal;
3132 
3133  SPI_connect();
3134 
3135  if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
3136  elog(ERROR, "SPI_prepare(\"%s\") failed", query);
3137 
3138  if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
3139  elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
3140 
3141  xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
3142  InvalidOid, nulls, tableforest, targetns));
3143  SPI_cursor_close(portal);
3144  SPI_finish();
3145 
3147  xmlschema, nulls, tableforest,
3148  targetns, true)));
3149 }
#define plan(x)
Definition: pg_regress.c:162
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1442
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:857
void SPI_cursor_close(Portal portal)
Definition: spi.c:1859

References _SPI_strdup(), elog, ERROR, InvalidOid, map_sql_table_to_xmlschema(), PG_GETARG_BOOL, PG_GETARG_TEXT_PP, PG_RETURN_XML_P, plan, query_to_xml_internal(), SPI_connect(), SPI_cursor_close(), SPI_cursor_open(), SPI_finish(), SPI_prepare(), stringinfo_to_xmltype(), text_to_cstring(), and PortalData::tupDesc.

◆ query_to_xml_internal()

static StringInfo query_to_xml_internal ( const char *  query,
char *  tablename,
const char *  xmlschema,
bool  nulls,
bool  tableforest,
const char *  targetns,
bool  top_level 
)
static

Definition at line 2978 of file xml.c.

2981 {
2982  StringInfo result;
2983  char *xmltn;
2984  uint64 i;
2985 
2986  if (tablename)
2987  xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
2988  else
2989  xmltn = "table";
2990 
2991  result = makeStringInfo();
2992 
2993  SPI_connect();
2994  if (SPI_execute(query, true, 0) != SPI_OK_SELECT)
2995  ereport(ERROR,
2996  (errcode(ERRCODE_DATA_EXCEPTION),
2997  errmsg("invalid query")));
2998 
2999  if (!tableforest)
3000  {
3001  xmldata_root_element_start(result, xmltn, xmlschema,
3002  targetns, top_level);
3003  appendStringInfoChar(result, '\n');
3004  }
3005 
3006  if (xmlschema)
3007  appendStringInfo(result, "%s\n\n", xmlschema);
3008 
3009  for (i = 0; i < SPI_processed; i++)
3010  SPI_sql_row_to_xmlelement(i, result, tablename, nulls,
3011  tableforest, targetns, top_level);
3012 
3013  if (!tableforest)
3014  xmldata_root_element_end(result, xmltn);
3015 
3016  SPI_finish();
3017 
3018  return result;
3019 }

References appendStringInfo(), appendStringInfoChar(), ereport, errcode(), errmsg(), ERROR, i, makeStringInfo(), map_sql_identifier_to_xml_name(), SPI_connect(), SPI_execute(), SPI_finish(), SPI_OK_SELECT, SPI_processed, SPI_sql_row_to_xmlelement(), xmldata_root_element_end(), and xmldata_root_element_start().

Referenced by query_to_xml(), query_to_xml_and_xmlschema(), and table_to_xml_internal().

◆ query_to_xmlschema()

Datum query_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 3042 of file xml.c.

3043 {
3044  char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
3045  bool nulls = PG_GETARG_BOOL(1);
3046  bool tableforest = PG_GETARG_BOOL(2);
3047  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
3048  const char *result;
3049  SPIPlanPtr plan;
3050  Portal portal;
3051 
3052  SPI_connect();
3053 
3054  if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
3055  elog(ERROR, "SPI_prepare(\"%s\") failed", query);
3056 
3057  if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
3058  elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
3059 
3061  InvalidOid, nulls,
3062  tableforest, targetns));
3063  SPI_cursor_close(portal);
3064  SPI_finish();
3065 
3067 }

References _SPI_strdup(), cstring_to_xmltype(), elog, ERROR, InvalidOid, map_sql_table_to_xmlschema(), PG_GETARG_BOOL, PG_GETARG_TEXT_PP, PG_RETURN_XML_P, plan, SPI_connect(), SPI_cursor_close(), SPI_cursor_open(), SPI_finish(), SPI_prepare(), text_to_cstring(), and PortalData::tupDesc.

◆ schema_get_xml_visible_tables()

static List* schema_get_xml_visible_tables ( Oid  nspid)
static

Definition at line 2792 of file xml.c.

2793 {
2794  StringInfoData query;
2795 
2796  initStringInfo(&query);
2797  appendStringInfo(&query, "SELECT oid FROM pg_catalog.pg_class"
2798  " WHERE relnamespace = %u AND relkind IN ("
2799  CppAsString2(RELKIND_RELATION) ","
2800  CppAsString2(RELKIND_MATVIEW) ","
2801  CppAsString2(RELKIND_VIEW) ")"
2802  " AND pg_catalog.has_table_privilege (oid, 'SELECT')"
2803  " ORDER BY relname;", nspid);
2804 
2805  return query_to_oid_list(query.data);
2806 }

References appendStringInfo(), CppAsString2, StringInfoData::data, initStringInfo(), nspid, and query_to_oid_list().

Referenced by schema_to_xml_internal(), and schema_to_xmlschema_internal().

◆ schema_to_xml()

Datum schema_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 3201 of file xml.c.

3202 {
3203  Name name = PG_GETARG_NAME(0);
3204  bool nulls = PG_GETARG_BOOL(1);
3205  bool tableforest = PG_GETARG_BOOL(2);
3206  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
3207 
3208  char *schemaname;
3209  Oid nspid;
3210 
3211  schemaname = NameStr(*name);
3212  nspid = LookupExplicitNamespace(schemaname, false);
3213 
3215  nulls, tableforest, targetns, true)));
3216 }
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
Definition: namespace.c:3370
Definition: c.h:741

References LookupExplicitNamespace(), name, NameStr, nspid, PG_GETARG_BOOL, PG_GETARG_NAME, PG_GETARG_TEXT_PP, PG_RETURN_XML_P, schema_to_xml_internal(), stringinfo_to_xmltype(), and text_to_cstring().

◆ schema_to_xml_and_xmlschema()

Datum schema_to_xml_and_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 3305 of file xml.c.

3306 {
3307  Name name = PG_GETARG_NAME(0);
3308  bool nulls = PG_GETARG_BOOL(1);
3309  bool tableforest = PG_GETARG_BOOL(2);
3310  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
3311  char *schemaname;
3312  Oid nspid;
3313  StringInfo xmlschema;
3314 
3315  schemaname = NameStr(*name);
3316  nspid = LookupExplicitNamespace(schemaname, false);
3317 
3318  xmlschema = schema_to_xmlschema_internal(schemaname, nulls,
3319  tableforest, targetns);
3320 
3322  xmlschema->data, nulls,
3323  tableforest, targetns, true)));
3324 }
static StringInfo schema_to_xmlschema_internal(const char *schemaname, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3247

References StringInfoData::data, LookupExplicitNamespace(), name, NameStr, nspid, PG_GETARG_BOOL, PG_GETARG_NAME, PG_GETARG_TEXT_PP, PG_RETURN_XML_P, schema_to_xml_internal(), schema_to_xmlschema_internal(), stringinfo_to_xmltype(), and text_to_cstring().

◆ schema_to_xml_internal()

static StringInfo schema_to_xml_internal ( Oid  nspid,
const char *  xmlschema,
bool  nulls,
bool  tableforest,
const char *  targetns,
bool  top_level 
)
static

Definition at line 3158 of file xml.c.

3160 {
3161  StringInfo result;
3162  char *xmlsn;
3163  List *relid_list;
3164  ListCell *cell;
3165 
3167  true, false);
3168  result = makeStringInfo();
3169 
3170  xmldata_root_element_start(result, xmlsn, xmlschema, targetns, top_level);
3171  appendStringInfoChar(result, '\n');
3172 
3173  if (xmlschema)
3174  appendStringInfo(result, "%s\n\n", xmlschema);
3175 
3176  SPI_connect();
3177 
3178  relid_list = schema_get_xml_visible_tables(nspid);
3179 
3180  foreach(cell, relid_list)
3181  {
3182  Oid relid = lfirst_oid(cell);
3183  StringInfo subres;
3184 
3185  subres = table_to_xml_internal(relid, NULL, nulls, tableforest,
3186  targetns, false);
3187 
3188  appendBinaryStringInfo(result, subres->data, subres->len);
3189  appendStringInfoChar(result, '\n');
3190  }
3191 
3192  SPI_finish();
3193 
3194  xmldata_root_element_end(result, xmlsn);
3195 
3196  return result;
3197 }
static List * schema_get_xml_visible_tables(Oid nspid)
Definition: xml.c:2792
static StringInfo table_to_xml_internal(Oid relid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2845

References appendBinaryStringInfo(), appendStringInfo(), appendStringInfoChar(), StringInfoData::data, get_namespace_name(), StringInfoData::len, lfirst_oid, makeStringInfo(), map_sql_identifier_to_xml_name(), nspid, schema_get_xml_visible_tables(), SPI_connect(), SPI_finish(), table_to_xml_internal(), xmldata_root_element_end(), and xmldata_root_element_start().

Referenced by database_to_xml_internal(), schema_to_xml(), and schema_to_xml_and_xmlschema().

◆ schema_to_xmlschema()

Datum schema_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 3292 of file xml.c.

3293 {
3294  Name name = PG_GETARG_NAME(0);
3295  bool nulls = PG_GETARG_BOOL(1);
3296  bool tableforest = PG_GETARG_BOOL(2);
3297  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
3298 
3300  nulls, tableforest, targetns)));
3301 }

References name, NameStr, PG_GETARG_BOOL, PG_GETARG_NAME, PG_GETARG_TEXT_PP, PG_RETURN_XML_P, schema_to_xmlschema_internal(), stringinfo_to_xmltype(), and text_to_cstring().

◆ schema_to_xmlschema_internal()

static StringInfo schema_to_xmlschema_internal ( const char *  schemaname,
bool  nulls,
bool  tableforest,
const char *  targetns 
)
static

Definition at line 3247 of file xml.c.

3249 {
3250  Oid nspid;
3251  List *relid_list;
3252  List *tupdesc_list;
3253  ListCell *cell;
3254  StringInfo result;
3255 
3256  result = makeStringInfo();
3257 
3258  nspid = LookupExplicitNamespace(schemaname, false);
3259 
3260  xsd_schema_element_start(result, targetns);
3261 
3262  SPI_connect();
3263 
3264  relid_list = schema_get_xml_visible_tables(nspid);
3265 
3266  tupdesc_list = NIL;
3267  foreach(cell, relid_list)
3268  {
3269  Relation rel;
3270 
3271  rel = table_open(lfirst_oid(cell), AccessShareLock);
3272  tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
3273  table_close(rel, NoLock);
3274  }
3275 
3276  appendStringInfoString(result,
3277  map_sql_typecoll_to_xmlschema_types(tupdesc_list));
3278 
3279  appendStringInfoString(result,
3281  nulls, tableforest, targetns));
3282 
3283  xsd_schema_element_end(result);
3284 
3285  SPI_finish();
3286 
3287  return result;
3288 }
static const char * map_sql_schema_to_xmlschema_types(Oid nspid, List *relid_list, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3598

References AccessShareLock, appendStringInfoString(), CreateTupleDescCopy(), lappend(), lfirst_oid, LookupExplicitNamespace(), makeStringInfo(), map_sql_schema_to_xmlschema_types(), map_sql_typecoll_to_xmlschema_types(), NIL, NoLock, nspid, RelationData::rd_att, schema_get_xml_visible_tables(), SPI_connect(), SPI_finish(), table_close(), table_open(), xsd_schema_element_end(), and xsd_schema_element_start().

Referenced by schema_to_xml_and_xmlschema(), and schema_to_xmlschema().

◆ SPI_sql_row_to_xmlelement()

static void SPI_sql_row_to_xmlelement ( uint64  rownum,
StringInfo  result,
char *  tablename,
bool  nulls,
bool  tableforest,
const char *  targetns,
bool  top_level 
)
static

Definition at line 4063 of file xml.c.

4066 {
4067  int i;
4068  char *xmltn;
4069 
4070  if (tablename)
4071  xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
4072  else
4073  {
4074  if (tableforest)
4075  xmltn = "row";
4076  else
4077  xmltn = "table";
4078  }
4079 
4080  if (tableforest)
4081  xmldata_root_element_start(result, xmltn, NULL, targetns, top_level);
4082  else
4083  appendStringInfoString(result, "<row>\n");
4084 
4085  for (i = 1; i <= SPI_tuptable->tupdesc->natts; i++)
4086  {
4087  char *colname;
4088  Datum colval;
4089  bool isnull;
4090 
4092  true, false);
4093  colval = SPI_getbinval(SPI_tuptable->vals[rownum],
4095  i,
4096  &isnull);
4097  if (isnull)
4098  {
4099  if (nulls)
4100  appendStringInfo(result, " <%s xsi:nil=\"true\"/>\n", colname);
4101  }
4102  else
4103  appendStringInfo(result, " <%s>%s</%s>\n",
4104  colname,
4106  SPI_gettypeid(SPI_tuptable->tupdesc, i), true),
4107  colname);
4108  }
4109 
4110  if (tableforest)
4111  {
4112  xmldata_root_element_end(result, xmltn);
4113  appendStringInfoChar(result, '\n');
4114  }
4115  else
4116  appendStringInfoString(result, "</row>\n\n");
4117 }
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1305
char * SPI_fname(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1195

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), i, map_sql_identifier_to_xml_name(), map_sql_value_to_xml_value(), TupleDescData::natts, SPI_fname(), SPI_getbinval(), SPI_gettypeid(), SPI_tuptable, SPITupleTable::tupdesc, SPITupleTable::vals, xmldata_root_element_end(), and xmldata_root_element_start().

Referenced by cursor_to_xml(), and query_to_xml_internal().

◆ stringinfo_to_xmltype()

◆ table_to_xml()

Datum table_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 2862 of file xml.c.

2863 {
2864  Oid relid = PG_GETARG_OID(0);
2865  bool nulls = PG_GETARG_BOOL(1);
2866  bool tableforest = PG_GETARG_BOOL(2);
2867  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2868 
2870  nulls, tableforest,
2871  targetns, true)));
2872 }
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

References PG_GETARG_BOOL, PG_GETARG_OID, PG_GETARG_TEXT_PP, PG_RETURN_XML_P, stringinfo_to_xmltype(), table_to_xml_internal(), and text_to_cstring().

◆ table_to_xml_and_xmlschema()

Datum table_to_xml_and_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 3101 of file xml.c.

3102 {
3103  Oid relid = PG_GETARG_OID(0);
3104  bool nulls = PG_GETARG_BOOL(1);
3105  bool tableforest = PG_GETARG_BOOL(2);
3106  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
3107  Relation rel;
3108  const char *xmlschema;
3109 
3110  rel = table_open(relid, AccessShareLock);
3111  xmlschema = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
3112  tableforest, targetns);
3113  table_close(rel, NoLock);
3114 
3116  xmlschema, nulls, tableforest,
3117  targetns, true)));
3118 }

References AccessShareLock, map_sql_table_to_xmlschema(), NoLock, PG_GETARG_BOOL, PG_GETARG_OID, PG_GETARG_TEXT_PP, PG_RETURN_XML_P, RelationData::rd_att, stringinfo_to_xmltype(), table_close(), table_open(), table_to_xml_internal(), and text_to_cstring().

◆ table_to_xml_internal()

static StringInfo table_to_xml_internal ( Oid  relid,
const char *  xmlschema,
bool  nulls,
bool  tableforest,
const char *  targetns,
bool  top_level 
)
static

Definition at line 2845 of file xml.c.

2848 {
2849  StringInfoData query;
2850 
2851  initStringInfo(&query);
2852  appendStringInfo(&query, "SELECT * FROM %s",
2854  ObjectIdGetDatum(relid))));
2855  return query_to_xml_internal(query.data, get_rel_name(relid),
2856  xmlschema, nulls, tableforest,
2857  targetns, top_level);
2858 }
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:642
static char * DatumGetCString(Datum X)
Definition: postgres.h:335
Datum regclassout(PG_FUNCTION_ARGS)
Definition: regproc.c:943

References appendStringInfo(), StringInfoData::data, DatumGetCString(), DirectFunctionCall1, get_rel_name(), initStringInfo(), ObjectIdGetDatum(), query_to_xml_internal(), and regclassout().

Referenced by schema_to_xml_internal(), table_to_xml(), and table_to_xml_and_xmlschema().

◆ table_to_xmlschema()

Datum table_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 3023 of file xml.c.

3024 {
3025  Oid relid = PG_GETARG_OID(0);
3026  bool nulls = PG_GETARG_BOOL(1);
3027  bool tableforest = PG_GETARG_BOOL(2);
3028  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
3029  const char *result;
3030  Relation rel;
3031 
3032  rel = table_open(relid, AccessShareLock);
3033  result = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
3034  tableforest, targetns);
3035  table_close(rel, NoLock);
3036 
3038 }

References AccessShareLock, cstring_to_xmltype(), map_sql_table_to_xmlschema(), NoLock, PG_GETARG_BOOL, PG_GETARG_OID, PG_GETARG_TEXT_PP, PG_RETURN_XML_P, RelationData::rd_att, table_close(), table_open(), and text_to_cstring().

◆ texttoxml()

Datum texttoxml ( PG_FUNCTION_ARGS  )

Definition at line 637 of file xml.c.

638 {
640 
642 }
const void * data
xmltype * xmlparse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace)
Definition: xml.c:960
int xmloption
Definition: xml.c:110

References data, PG_GETARG_TEXT_PP, PG_RETURN_XML_P, xmloption, and xmlparse().

◆ xml_in()

Datum xml_in ( PG_FUNCTION_ARGS  )

Definition at line 273 of file xml.c.

274 {
275 #ifdef USE_LIBXML
276  char *s = PG_GETARG_CSTRING(0);
277  xmltype *vardata;
278  xmlDocPtr doc;
279 
280  /* Build the result object. */
281  vardata = (xmltype *) cstring_to_text(s);
282 
283  /*
284  * Parse the data to check if it is well-formed XML data.
285  *
286  * Note: we don't need to worry about whether a soft error is detected.
287  */
288  doc = xml_parse(vardata, xmloption, true, GetDatabaseEncoding(),
289  NULL, NULL, fcinfo->context);
290  if (doc != NULL)
291  xmlFreeDoc(doc);
292 
293  PG_RETURN_XML_P(vardata);
294 #else
295  NO_XML_SUPPORT();
296  return 0;
297 #endif
298 }
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
int GetDatabaseEncoding(void)
Definition: mbutils.c:1261

References cstring_to_text(), GetDatabaseEncoding(), NO_XML_SUPPORT, PG_GETARG_CSTRING, PG_RETURN_XML_P, and xmloption.

◆ 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 }
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().

◆ xml_is_well_formed()

Datum xml_is_well_formed ( PG_FUNCTION_ARGS  )

Definition at line 4580 of file xml.c.

4581 {
4582 #ifdef USE_LIBXML
4583  text *data = PG_GETARG_TEXT_PP(0);
4584 
4585  PG_RETURN_BOOL(wellformed_xml(data, xmloption));
4586 #else
4587  NO_XML_SUPPORT();
4588  return 0;
4589 #endif /* not USE_LIBXML */
4590 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

References data, NO_XML_SUPPORT, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and xmloption.

◆ xml_is_well_formed_content()

Datum xml_is_well_formed_content ( PG_FUNCTION_ARGS  )

Definition at line 4606 of file xml.c.

4607 {
4608 #ifdef USE_LIBXML
4609  text *data = PG_GETARG_TEXT_PP(0);
4610 
4611  PG_RETURN_BOOL(wellformed_xml(data, XMLOPTION_CONTENT));
4612 #else
4613  NO_XML_SUPPORT();
4614  return 0;
4615 #endif /* not USE_LIBXML */
4616 }
@ XMLOPTION_CONTENT
Definition: primnodes.h:1593

References data, NO_XML_SUPPORT, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and XMLOPTION_CONTENT.

◆ xml_is_well_formed_document()

Datum xml_is_well_formed_document ( PG_FUNCTION_ARGS  )

Definition at line 4593 of file xml.c.

4594 {
4595 #ifdef USE_LIBXML
4596  text *data = PG_GETARG_TEXT_PP(0);
4597 
4598  PG_RETURN_BOOL(wellformed_xml(data, XMLOPTION_DOCUMENT));
4599 #else
4600  NO_XML_SUPPORT();
4601  return 0;
4602 #endif /* not USE_LIBXML */
4603 }

References data, NO_XML_SUPPORT, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and XMLOPTION_DOCUMENT.

◆ xml_out()

Datum xml_out ( PG_FUNCTION_ARGS  )

Definition at line 356 of file xml.c.

357 {
358  xmltype *x = PG_GETARG_XML_P(0);
359 
360  /*
361  * xml_out removes the encoding property in all cases. This is because we
362  * cannot control from here whether the datum will be converted to a
363  * different client encoding, so we'd do more harm than good by including
364  * it.
365  */
367 }
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
int x
Definition: isn.c:71
static char * xml_out_internal(xmltype *x, pg_enc target_encoding)
Definition: xml.c:312
#define PG_GETARG_XML_P(n)
Definition: xml.h:62

References PG_GETARG_XML_P, PG_RETURN_CSTRING, x, and xml_out_internal().

◆ xml_out_internal()

static char* xml_out_internal ( xmltype x,
pg_enc  target_encoding 
)
static

Definition at line 312 of file xml.c.

313 {
314  char *str = text_to_cstring((text *) x);
315 
316 #ifdef USE_LIBXML
317  size_t len = strlen(str);
318  xmlChar *version;
319  int standalone;
320  int res_code;
321 
322  if ((res_code = parse_xml_decl((xmlChar *) str,
323  &len, &version, NULL, &standalone)) == 0)
324  {
326 
328 
329  if (!print_xml_decl(&buf, version, target_encoding, standalone))
330  {
331  /*
332  * If we are not going to produce an XML declaration, eat a single
333  * newline in the original string to prevent empty first lines in
334  * the output.
335  */
336  if (*(str + len) == '\n')
337  len += 1;
338  }
340 
341  pfree(str);
342 
343  return buf.data;
344  }
345 
347  errcode(ERRCODE_INTERNAL_ERROR),
348  errmsg_internal("could not parse XML declaration in stored value"),
349  errdetail_for_xml_code(res_code));
350 #endif
351  return str;
352 }
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
#define WARNING
Definition: elog.h:36

References appendStringInfoString(), buf, ereport, errcode(), errmsg_internal(), initStringInfo(), len, pfree(), str, text_to_cstring(), WARNING, and x.

Referenced by xml_out(), xml_send(), and XmlTableSetDocument().

◆ xml_recv()

Datum xml_recv ( PG_FUNCTION_ARGS  )

Definition at line 371 of file xml.c.

372 {
373 #ifdef USE_LIBXML
375  xmltype *result;
376  char *str;
377  char *newstr;
378  int nbytes;
379  xmlDocPtr doc;
380  xmlChar *encodingStr = NULL;
381  int encoding;
382 
383  /*
384  * Read the data in raw format. We don't know yet what the encoding is, as
385  * that information is embedded in the xml declaration; so we have to
386  * parse that before converting to server encoding.
387  */
388  nbytes = buf->len - buf->cursor;
389  str = (char *) pq_getmsgbytes(buf, nbytes);
390 
391  /*
392  * We need a null-terminated string to pass to parse_xml_decl(). Rather
393  * than make a separate copy, make the temporary result one byte bigger
394  * than it needs to be.
395  */
396  result = palloc(nbytes + 1 + VARHDRSZ);
397  SET_VARSIZE(result, nbytes + VARHDRSZ);
398  memcpy(VARDATA(result), str, nbytes);
399  str = VARDATA(result);
400  str[nbytes] = '\0';
401 
402  parse_xml_decl((const xmlChar *) str, NULL, NULL, &encodingStr, NULL);
403 
404  /*
405  * If encoding wasn't explicitly specified in the XML header, treat it as
406  * UTF-8, as that's the default in XML. This is different from xml_in(),
407  * where the input has to go through the normal client to server encoding
408  * conversion.
409  */
410  encoding = encodingStr ? xmlChar_to_encoding(encodingStr) : PG_UTF8;
411 
412  /*
413  * Parse the data to check if it is well-formed XML data. Assume that
414  * xml_parse will throw ERROR if not.
415  */
416  doc = xml_parse(result, xmloption, true, encoding, NULL, NULL, NULL);
417  xmlFreeDoc(doc);
418 
419  /* Now that we know what we're dealing with, convert to server encoding */
420  newstr = pg_any_to_server(str, nbytes, encoding);
421 
422  if (newstr != str)
423  {
424  pfree(result);
425  result = (xmltype *) cstring_to_text(newstr);
426  pfree(newstr);
427  }
428 
429  PG_RETURN_XML_P(result);
430 #else
431  NO_XML_SUPPORT();
432  return 0;
433 #endif
434 }
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:676
void * palloc(Size size)
Definition: mcxt.c:1317
int32 encoding
Definition: pg_database.h:41
@ PG_UTF8
Definition: pg_wchar.h:232
const char * pq_getmsgbytes(StringInfo msg, int datalen)
Definition: pqformat.c:508
StringInfoData * StringInfo
Definition: stringinfo.h:54
#define VARDATA(PTR)
Definition: varatt.h:278
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305

References buf, cstring_to_text(), encoding, NO_XML_SUPPORT, palloc(), pfree(), pg_any_to_server(), PG_GETARG_POINTER, PG_RETURN_XML_P, PG_UTF8, pq_getmsgbytes(), SET_VARSIZE, str, VARDATA, VARHDRSZ, and xmloption.

◆ xml_send()

Datum xml_send ( PG_FUNCTION_ARGS  )

Definition at line 438 of file xml.c.

439 {
440  xmltype *x = PG_GETARG_XML_P(0);
441  char *outval;
443 
444  /*
445  * xml_out_internal doesn't convert the encoding, it just prints the right
446  * declaration. pq_sendtext will do the conversion.
447  */
449 
451  pq_sendtext(&buf, outval, strlen(outval));
452  pfree(outval);
454 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
int pg_get_client_encoding(void)
Definition: mbutils.c:336
void pq_sendtext(StringInfo buf, const char *str, int slen)
Definition: pqformat.c:172
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:326
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:346

References buf, pfree(), pg_get_client_encoding(), PG_GETARG_XML_P, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendtext(), x, and xml_out_internal().

◆ xmlcomment()

Datum xmlcomment ( PG_FUNCTION_ARGS  )

Definition at line 491 of file xml.c.

492 {
493 #ifdef USE_LIBXML
494  text *arg = PG_GETARG_TEXT_PP(0);
495  char *argdata = VARDATA_ANY(arg);
496  int len = VARSIZE_ANY_EXHDR(arg);
498  int i;
499 
500  /* check for "--" in string or "-" at the end */
501  for (i = 1; i < len; i++)
502  {
503  if (argdata[i] == '-' && argdata[i - 1] == '-')
504  ereport(ERROR,
505  (errcode(ERRCODE_INVALID_XML_COMMENT),
506  errmsg("invalid XML comment")));
507  }
508  if (len > 0 && argdata[len - 1] == '-')
509  ereport(ERROR,
510  (errcode(ERRCODE_INVALID_XML_COMMENT),
511  errmsg("invalid XML comment")));
512 
514  appendStringInfoString(&buf, "<!--");
516  appendStringInfoString(&buf, "-->");
517 
519 #else
520  NO_XML_SUPPORT();
521  return 0;
522 #endif
523 }
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:3982

References appendStringInfoString(), appendStringInfoText(), arg, buf, ereport, errcode(), errmsg(), ERROR, i, initStringInfo(), len, NO_XML_SUPPORT, PG_GETARG_TEXT_PP, PG_RETURN_XML_P, stringinfo_to_xmltype(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ 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 }
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
#define VARSIZE(PTR)
Definition: varatt.h:279
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().

◆ xmlconcat2()

Datum xmlconcat2 ( PG_FUNCTION_ARGS  )

Definition at line 619 of file xml.c.

620 {
621  if (PG_ARGISNULL(0))
622  {
623  if (PG_ARGISNULL(1))
624  PG_RETURN_NULL();
625  else
627  }
628  else if (PG_ARGISNULL(1))
630  else
632  PG_GETARG_XML_P(1))));
633 }
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define list_make2(x1, x2)
Definition: pg_list.h:214
xmltype * xmlconcat(List *args)
Definition: xml.c:553

References list_make2, PG_ARGISNULL, PG_GETARG_XML_P, PG_RETURN_NULL, PG_RETURN_XML_P, and xmlconcat().

◆ xmldata_root_element_end()

static void xmldata_root_element_end ( StringInfo  result,
const char *  eltname 
)
static

Definition at line 2971 of file xml.c.

2972 {
2973  appendStringInfo(result, "</%s>\n", eltname);
2974 }

References appendStringInfo().

Referenced by cursor_to_xml(), database_to_xml_internal(), query_to_xml_internal(), schema_to_xml_internal(), and SPI_sql_row_to_xmlelement().

◆ xmldata_root_element_start()

static void xmldata_root_element_start ( StringInfo  result,
const char *  eltname,
const char *  xmlschema,
const char *  targetns,
bool  top_level 
)
static

Definition at line 2944 of file xml.c.

2947 {
2948  /* This isn't really wrong but currently makes no sense. */
2949  Assert(top_level || !xmlschema);
2950 
2951  appendStringInfo(result, "<%s", eltname);
2952  if (top_level)
2953  {
2954  appendStringInfoString(result, " xmlns:xsi=\"" NAMESPACE_XSI "\"");
2955  if (strlen(targetns) > 0)
2956  appendStringInfo(result, " xmlns=\"%s\"", targetns);
2957  }
2958  if (xmlschema)
2959  {
2960  /* FIXME: better targets */
2961  if (strlen(targetns) > 0)
2962  appendStringInfo(result, " xsi:schemaLocation=\"%s #\"", targetns);
2963  else
2964  appendStringInfoString(result, " xsi:noNamespaceSchemaLocation=\"#\"");
2965  }
2966  appendStringInfoString(result, ">\n");
2967 }
#define NAMESPACE_XSI
Definition: xml.c:244

References appendStringInfo(), appendStringInfoString(), Assert, and NAMESPACE_XSI.

Referenced by cursor_to_xml(), database_to_xml_internal(), query_to_xml_internal(), schema_to_xml_internal(), and SPI_sql_row_to_xmlelement().

◆ 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 }
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
e
Definition: preproc-init.c:82
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().

◆ xmlexists()

Datum xmlexists ( PG_FUNCTION_ARGS  )

Definition at line 4515 of file xml.c.

4516 {
4517 #ifdef USE_LIBXML
4518  text *xpath_expr_text = PG_GETARG_TEXT_PP(0);
4520  int res_nitems;
4521 
4522  xpath_internal(xpath_expr_text, data, NULL,
4523  &res_nitems, NULL);
4524 
4525  PG_RETURN_BOOL(res_nitems > 0);
4526 #else
4527  NO_XML_SUPPORT();
4528  return 0;
4529 #endif
4530 }

References data, NO_XML_SUPPORT, PG_GETARG_TEXT_PP, PG_GETARG_XML_P, and PG_RETURN_BOOL.

◆ 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 }

References data, GetDatabaseEncoding(), and NO_XML_SUPPORT.

Referenced by ExecEvalXmlExpr(), and texttoxml().

◆ 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

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 }
@ 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

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

◆ XmlTableDestroyOpaque()

static void XmlTableDestroyOpaque ( struct TableFuncScanState state)
static

Definition at line 5044 of file xml.c.

5045 {
5046 #ifdef USE_LIBXML
5047  XmlTableBuilderData *xtCxt;
5048 
5049  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableDestroyOpaque");
5050 
5051  /* Propagate our own error context to libxml2 */
5052  xmlSetStructuredErrorFunc((void *) xtCxt->xmlerrcxt, xml_errorHandler);
5053 
5054  if (xtCxt->xpathscomp != NULL)
5055  {
5056  int i;
5057 
5058  for (i = 0; i < xtCxt->natts; i++)
5059  if (xtCxt->xpathscomp[i] != NULL)
5060  xmlXPathFreeCompExpr(xtCxt->xpathscomp[i]);
5061  }
5062 
5063  if (xtCxt->xpathobj != NULL)
5064  xmlXPathFreeObject(xtCxt->xpathobj);
5065  if (xtCxt->xpathcomp != NULL)
5066  xmlXPathFreeCompExpr(xtCxt->xpathcomp);
5067  if (xtCxt->xpathcxt != NULL)
5068  xmlXPathFreeContext(xtCxt->xpathcxt);
5069  if (xtCxt->doc != NULL)
5070  xmlFreeDoc(xtCxt->doc);
5071  if (xtCxt->ctxt != NULL)
5072  xmlFreeParserCtxt(xtCxt->ctxt);
5073 
5074  pg_xml_done(xtCxt->xmlerrcxt, true);
5075 
5076  /* not valid anymore */
5077  xtCxt->magic = 0;
5078  state->opaque = NULL;
5079 
5080 #else
5081  NO_XML_SUPPORT();
5082 #endif /* not USE_LIBXML */
5083 }
Definition: regguts.h:323

References i, NO_XML_SUPPORT, and pg_xml_done().

◆ XmlTableFetchRow()

static bool XmlTableFetchRow ( struct TableFuncScanState state)
static

Definition at line 4847 of file xml.c.

4848 {
4849 #ifdef USE_LIBXML
4850  XmlTableBuilderData *xtCxt;
4851 
4852  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableFetchRow");
4853 
4854  /* Propagate our own error context to libxml2 */
4855  xmlSetStructuredErrorFunc((void *) xtCxt->xmlerrcxt, xml_errorHandler);
4856 
4857  if (xtCxt->xpathobj == NULL)
4858  {
4859  xtCxt->xpathobj = xmlXPathCompiledEval(xtCxt->xpathcomp, xtCxt->xpathcxt);
4860  if (xtCxt->xpathobj == NULL || xtCxt->xmlerrcxt->err_occurred)
4861  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
4862  "could not create XPath object");
4863 
4864  xtCxt->row_count = 0;
4865  }
4866 
4867  if (xtCxt->xpathobj->type == XPATH_NODESET)
4868  {
4869  if (xtCxt->xpathobj->nodesetval != NULL)
4870  {
4871  if (xtCxt->row_count++ < xtCxt->xpathobj->nodesetval->nodeNr)
4872  return true;
4873  }
4874  }
4875 
4876  return false;
4877 #else
4878  NO_XML_SUPPORT();
4879  return false;
4880 #endif /* not USE_LIBXML */
4881 }

References ERROR, NO_XML_SUPPORT, and xml_ereport().

◆ XmlTableGetValue()

static Datum XmlTableGetValue ( struct TableFuncScanState state,
int  colnum,
Oid  typid,
int32  typmod,
bool isnull 
)
static

Definition at line 4892 of file xml.c.

4894 {
4895 #ifdef USE_LIBXML
4896  XmlTableBuilderData *xtCxt;
4897  Datum result = (Datum) 0;
4898  xmlNodePtr cur;
4899  char *cstr = NULL;
4900  volatile xmlXPathObjectPtr xpathobj = NULL;
4901 
4902  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableGetValue");
4903 
4904  Assert(xtCxt->xpathobj &&
4905  xtCxt->xpathobj->type == XPATH_NODESET &&
4906  xtCxt->xpathobj->nodesetval != NULL);
4907 
4908  /* Propagate our own error context to libxml2 */
4909  xmlSetStructuredErrorFunc((void *) xtCxt->xmlerrcxt, xml_errorHandler);
4910 
4911  *isnull = false;
4912 
4913  cur = xtCxt->xpathobj->nodesetval->nodeTab[xtCxt->row_count - 1];
4914 
4915  Assert(xtCxt->xpathscomp[colnum] != NULL);
4916 
4917  PG_TRY();
4918  {
4919  /* Set current node as entry point for XPath evaluation */
4920  xtCxt->xpathcxt->node = cur;
4921 
4922  /* Evaluate column path */
4923  xpathobj = xmlXPathCompiledEval(xtCxt->xpathscomp[colnum], xtCxt->xpathcxt);
4924  if (xpathobj == NULL || xtCxt->xmlerrcxt->err_occurred)
4925  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
4926  "could not create XPath object");
4927 
4928  /*
4929  * There are four possible cases, depending on the number of nodes
4930  * returned by the XPath expression and the type of the target column:
4931  * a) XPath returns no nodes. b) The target type is XML (return all
4932  * as XML). For non-XML return types: c) One node (return content).
4933  * d) Multiple nodes (error).
4934  */
4935  if (xpathobj->type == XPATH_NODESET)
4936  {
4937  int count = 0;
4938 
4939  if (xpathobj->nodesetval != NULL)
4940  count = xpathobj->nodesetval->nodeNr;
4941 
4942  if (xpathobj->nodesetval == NULL || count == 0)
4943  {
4944  *isnull = true;
4945  }
4946  else
4947  {
4948  if (typid == XMLOID)
4949  {
4950  text *textstr;
4952 
4953  /* Concatenate serialized values */
4954  initStringInfo(&str);
4955  for (int i = 0; i < count; i++)
4956  {
4957  textstr =
4958  xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[i],
4959  xtCxt->xmlerrcxt);
4960 
4961  appendStringInfoText(&str, textstr);
4962  }
4963  cstr = str.data;
4964  }
4965  else
4966  {
4967  xmlChar *str;
4968 
4969  if (count > 1)
4970  ereport(ERROR,
4971  (errcode(ERRCODE_CARDINALITY_VIOLATION),
4972  errmsg("more than one value returned by column XPath expression")));
4973 
4974  str = xmlXPathCastNodeSetToString(xpathobj->nodesetval);
4975  cstr = str ? xml_pstrdup_and_free(str) : "";
4976  }
4977  }
4978  }
4979  else if (xpathobj->type == XPATH_STRING)
4980  {
4981  /* Content should be escaped when target will be XML */
4982  if (typid == XMLOID)
4983  cstr = escape_xml((char *) xpathobj->stringval);
4984  else
4985  cstr = (char *) xpathobj->stringval;
4986  }
4987  else if (xpathobj->type == XPATH_BOOLEAN)
4988  {
4989  char typcategory;
4990  bool typispreferred;
4991  xmlChar *str;
4992 
4993  /* Allow implicit casting from boolean to numbers */
4994  get_type_category_preferred(typid, &typcategory, &typispreferred);
4995 
4996  if (typcategory != TYPCATEGORY_NUMERIC)
4997  str = xmlXPathCastBooleanToString(xpathobj->boolval);
4998  else
4999  str = xmlXPathCastNumberToString(xmlXPathCastBooleanToNumber(xpathobj->boolval));
5000 
5001  cstr = xml_pstrdup_and_free(str);
5002  }
5003  else if (xpathobj->type == XPATH_NUMBER)
5004  {
5005  xmlChar *str;
5006 
5007  str = xmlXPathCastNumberToString(xpathobj->floatval);
5008  cstr = xml_pstrdup_and_free(str);
5009  }
5010  else
5011  elog(ERROR, "unexpected XPath object type %u", xpathobj->type);
5012 
5013  /*
5014  * By here, either cstr contains the result value, or the isnull flag
5015  * has been set.
5016  */
5017  Assert(cstr || *isnull);
5018 
5019  if (!*isnull)
5020  result = InputFunctionCall(&state->in_functions[colnum],
5021  cstr,
5022  state->typioparams[colnum],
5023  typmod);
5024  }
5025  PG_FINALLY();
5026  {
5027  if (xpathobj != NULL)
5028  xmlXPathFreeObject(xpathobj);
5029  }
5030  PG_END_TRY();
5031 
5032  return result;
5033 #else
5034  NO_XML_SUPPORT();
5035  return 0;
5036 #endif /* not USE_LIBXML */
5037 }
struct cursor * cur
Definition: ecpg.c:28
#define PG_FINALLY(...)
Definition: elog.h:387
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1530
void get_type_category_preferred(Oid typid, char *typcategory, bool *typispreferred)
Definition: lsyscache.c:2710

References appendStringInfoText(), Assert, cur, elog, ereport, errcode(), errmsg(), ERROR, escape_xml(), get_type_category_preferred(), i, initStringInfo(), InputFunctionCall(), NO_XML_SUPPORT, PG_END_TRY, PG_FINALLY, PG_TRY, str, and xml_ereport().

◆ XmlTableInitOpaque()

static void XmlTableInitOpaque ( struct TableFuncScanState state,
int  natts 
)
static

Definition at line 4655 of file xml.c.

4656 {
4657 #ifdef USE_LIBXML
4658  volatile xmlParserCtxtPtr ctxt = NULL;
4659  XmlTableBuilderData *xtCxt;
4660  PgXmlErrorContext *xmlerrcxt;
4661 
4662  xtCxt = palloc0(sizeof(XmlTableBuilderData));
4663  xtCxt->magic = XMLTABLE_CONTEXT_MAGIC;
4664  xtCxt->natts = natts;
4665  xtCxt->xpathscomp = palloc0(sizeof(xmlXPathCompExprPtr) * natts);
4666 
4667  xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
4668 
4669  PG_TRY();
4670  {
4671  xmlInitParser();
4672 
4673  ctxt = xmlNewParserCtxt();
4674  if (ctxt == NULL || xmlerrcxt->err_occurred)
4675  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
4676  "could not allocate parser context");
4677  }
4678  PG_CATCH();
4679  {
4680  if (ctxt != NULL)
4681  xmlFreeParserCtxt(ctxt);
4682 
4683  pg_xml_done(xmlerrcxt, true);
4684 
4685  PG_RE_THROW();
4686  }
4687  PG_END_TRY();
4688 
4689  xtCxt->xmlerrcxt = xmlerrcxt;
4690  xtCxt->ctxt = ctxt;
4691 
4692  state->opaque = xtCxt;
4693 #else
4694  NO_XML_SUPPORT();
4695 #endif /* not USE_LIBXML */
4696 }
void * palloc0(Size size)
Definition: mcxt.c:1347

References ERROR, NO_XML_SUPPORT, palloc0(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, pg_xml_done(), pg_xml_init(), PG_XML_STRICTNESS_ALL, and xml_ereport().

◆ XmlTableSetColumnFilter()

static void XmlTableSetColumnFilter ( struct TableFuncScanState state,
const char *  path,
int  colnum 
)
static

Definition at line 4815 of file xml.c.

4816 {
4817 #ifdef USE_LIBXML
4818  XmlTableBuilderData *xtCxt;
4819  xmlChar *xstr;
4820 
4821  Assert(PointerIsValid(path));
4822 
4823  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetColumnFilter");
4824 
4825  if (*path == '\0')
4826  ereport(ERROR,
4827  (errcode(ERRCODE_DATA_EXCEPTION),
4828  errmsg("column path filter must not be empty string")));
4829 
4830  xstr = pg_xmlCharStrndup(path, strlen(path));
4831 
4832  xtCxt->xpathscomp[colnum] = xmlXPathCompile(xstr);
4833  if (xtCxt->xpathscomp[colnum] == NULL || xtCxt->xmlerrcxt->err_occurred)
4834  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_DATA_EXCEPTION,
4835  "invalid XPath expression");
4836 #else
4837  NO_XML_SUPPORT();
4838 #endif /* not USE_LIBXML */
4839 }
#define PointerIsValid(pointer)
Definition: c.h:763

References Assert, ereport, errcode(), errmsg(), ERROR, NO_XML_SUPPORT, PointerIsValid, and xml_ereport().

◆ XmlTableSetDocument()

static void XmlTableSetDocument ( struct TableFuncScanState state,
Datum  value 
)
static

Definition at line 4703 of file xml.c.

4704 {
4705 #ifdef USE_LIBXML
4706  XmlTableBuilderData *xtCxt;
4707  xmltype *xmlval = DatumGetXmlP(value);
4708  char *str;
4709  xmlChar *xstr;
4710  int length;
4711  volatile xmlDocPtr doc = NULL;
4712  volatile xmlXPathContextPtr xpathcxt = NULL;
4713 
4714  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetDocument");
4715 
4716  /*
4717  * Use out function for casting to string (remove encoding property). See
4718  * comment in xml_out.
4719  */
4720  str = xml_out_internal(xmlval, 0);
4721 
4722  length = strlen(str);
4723  xstr = pg_xmlCharStrndup(str, length);
4724 
4725  PG_TRY();
4726  {
4727  doc = xmlCtxtReadMemory(xtCxt->ctxt, (char *) xstr, length, NULL, NULL, 0);
4728  if (doc == NULL || xtCxt->xmlerrcxt->err_occurred)
4729  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INVALID_XML_DOCUMENT,
4730  "could not parse XML document");
4731  xpathcxt = xmlXPathNewContext(doc);
4732  if (xpathcxt == NULL || xtCxt->xmlerrcxt->err_occurred)
4733  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
4734  "could not allocate XPath context");
4735  xpathcxt->node = (xmlNodePtr) doc;
4736  }
4737  PG_CATCH();
4738  {
4739  if (xpathcxt != NULL)
4740  xmlXPathFreeContext(xpathcxt);
4741  if (doc != NULL)
4742  xmlFreeDoc(doc);
4743 
4744  PG_RE_THROW();
4745  }
4746  PG_END_TRY();
4747 
4748  xtCxt->doc = doc;
4749  xtCxt->xpathcxt = xpathcxt;
4750 #else
4751  NO_XML_SUPPORT();
4752 #endif /* not USE_LIBXML */
4753 }

References DatumGetXmlP(), ERROR, NO_XML_SUPPORT, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, str, value, xml_ereport(), and xml_out_internal().

◆ XmlTableSetNamespace()

static void XmlTableSetNamespace ( struct TableFuncScanState state,
const char *  name,
const char *  uri 
)
static

Definition at line 4760 of file xml.c.

4761 {
4762 #ifdef USE_LIBXML
4763  XmlTableBuilderData *xtCxt;
4764 
4765  if (name == NULL)
4766  ereport(ERROR,
4767  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4768  errmsg("DEFAULT namespace is not supported")));
4769  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetNamespace");
4770 
4771  if (xmlXPathRegisterNs(xtCxt->xpathcxt,
4772  pg_xmlCharStrndup(name, strlen(name)),
4773  pg_xmlCharStrndup(uri, strlen(uri))))
4774  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_DATA_EXCEPTION,
4775  "could not set XML namespace");
4776 #else
4777  NO_XML_SUPPORT();
4778 #endif /* not USE_LIBXML */
4779 }

References ereport, errcode(), errmsg(), ERROR, name, NO_XML_SUPPORT, and xml_ereport().

◆ XmlTableSetRowFilter()

static void XmlTableSetRowFilter ( struct TableFuncScanState state,
const char *  path 
)
static

Definition at line 4786 of file xml.c.

4787 {
4788 #ifdef USE_LIBXML
4789  XmlTableBuilderData *xtCxt;
4790  xmlChar *xstr;
4791 
4792  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetRowFilter");
4793 
4794  if (*path == '\0')
4795  ereport(ERROR,
4796  (errcode(ERRCODE_DATA_EXCEPTION),
4797  errmsg("row path filter must not be empty string")));
4798 
4799  xstr = pg_xmlCharStrndup(path, strlen(path));
4800 
4801  xtCxt->xpathcomp = xmlXPathCompile(xstr);
4802  if (xtCxt->xpathcomp == NULL || xtCxt->xmlerrcxt->err_occurred)
4803  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_SYNTAX_ERROR,
4804  "invalid XPath expression");
4805 #else
4806  NO_XML_SUPPORT();
4807 #endif /* not USE_LIBXML */
4808 }

References ereport, errcode(), errmsg(), ERROR, NO_XML_SUPPORT, and xml_ereport().

◆ xmltext()

Datum xmltext ( PG_FUNCTION_ARGS  )

Definition at line 527 of file xml.c.

528 {
529 #ifdef USE_LIBXML
530  text *arg = PG_GETARG_TEXT_PP(0);
531  text *result;
532  xmlChar *xmlbuf = NULL;
533 
534  xmlbuf = xmlEncodeSpecialChars(NULL, xml_text2xmlChar(arg));
535 
536  Assert(xmlbuf);
537 
538  result = cstring_to_text_with_len((const char *) xmlbuf, xmlStrlen(xmlbuf));
539  xmlFree(xmlbuf);
540  PG_RETURN_XML_P(result);
541 #else
542  NO_XML_SUPPORT();
543  return 0;
544 #endif /* not USE_LIBXML */
545 }

References arg, Assert, cstring_to_text_with_len(), NO_XML_SUPPORT, PG_GETARG_TEXT_PP, and PG_RETURN_XML_P.

◆ xmltotext()

Datum xmltotext ( PG_FUNCTION_ARGS  )

Definition at line 646 of file xml.c.

647 {
649 
650  /* It's actually binary compatible. */
652 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372

References data, PG_GETARG_XML_P, and PG_RETURN_TEXT_P.

◆ 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().

◆ xmlvalidate()

Datum xmlvalidate ( PG_FUNCTION_ARGS  )

Definition at line 1086 of file xml.c.

1087 {
1088  ereport(ERROR,
1089  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1090  errmsg("xmlvalidate is not implemented")));
1091  return 0;
1092 }

References ereport, errcode(), errmsg(), and ERROR.

◆ xpath()

Datum xpath ( PG_FUNCTION_ARGS  )

Definition at line 4492 of file xml.c.

4493 {
4494 #ifdef USE_LIBXML
4495  text *xpath_expr_text = PG_GETARG_TEXT_PP(0);
4497  ArrayType *namespaces = PG_GETARG_ARRAYTYPE_P(2);
4498  ArrayBuildState *astate;
4499 
4500  astate = initArrayResult(XMLOID, CurrentMemoryContext, true);
4501  xpath_internal(xpath_expr_text, data, namespaces,
4502  NULL, astate);
4504 #else
4505  NO_XML_SUPPORT();
4506  return 0;
4507 #endif
4508 }
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:263
ArrayBuildState * initArrayResult(Oid element_type, MemoryContext rcontext, bool subcontext)
Definition: arrayfuncs.c:5281
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5408
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143

References CurrentMemoryContext, data, initArrayResult(), makeArrayResult(), NO_XML_SUPPORT, PG_GETARG_ARRAYTYPE_P, PG_GETARG_TEXT_PP, PG_GETARG_XML_P, and PG_RETURN_DATUM.

Referenced by pgxml_xpath(), xpath_bool(), xpath_list(), xpath_nodeset(), xpath_number(), and xpath_string().

◆ xpath_exists()

Datum xpath_exists ( PG_FUNCTION_ARGS  )

Definition at line 4538 of file xml.c.

4539 {
4540 #ifdef USE_LIBXML
4541  text *xpath_expr_text = PG_GETARG_TEXT_PP(0);
4543  ArrayType *namespaces = PG_GETARG_ARRAYTYPE_P(2);
4544  int res_nitems;
4545 
4546  xpath_internal(xpath_expr_text, data, namespaces,
4547  &res_nitems, NULL);
4548 
4549  PG_RETURN_BOOL(res_nitems > 0);
4550 #else
4551  NO_XML_SUPPORT();
4552  return 0;
4553 #endif
4554 }

References data, NO_XML_SUPPORT, PG_GETARG_ARRAYTYPE_P, PG_GETARG_TEXT_PP, PG_GETARG_XML_P, and PG_RETURN_BOOL.

◆ xsd_schema_element_end()

static void xsd_schema_element_end ( StringInfo  result)
static

Definition at line 3240 of file xml.c.

3241 {
3242  appendStringInfoString(result, "</xsd:schema>");
3243 }

References appendStringInfoString().

Referenced by database_to_xmlschema_internal(), map_sql_table_to_xmlschema(), and schema_to_xmlschema_internal().

◆ xsd_schema_element_start()

static void xsd_schema_element_start ( StringInfo  result,
const char *  targetns 
)
static

Definition at line 3223 of file xml.c.

3224 {
3225  appendStringInfoString(result,
3226  "<xsd:schema\n"
3227  " xmlns:xsd=\"" NAMESPACE_XSD "\"");
3228  if (strlen(targetns) > 0)
3229  appendStringInfo(result,
3230  "\n"
3231  " targetNamespace=\"%s\"\n"
3232  " elementFormDefault=\"qualified\"",
3233  targetns);
3234  appendStringInfoString(result,
3235  ">\n\n");
3236 }
#define NAMESPACE_XSD
Definition: xml.c:243

References appendStringInfo(), appendStringInfoString(), and NAMESPACE_XSD.

Referenced by database_to_xmlschema_internal(), map_sql_table_to_xmlschema(), and schema_to_xmlschema_internal().

Variable Documentation

◆ xmlbinary

int xmlbinary = XMLBINARY_BASE64

Definition at line 109 of file xml.c.

Referenced by map_sql_type_to_xmlschema_type(), and map_sql_value_to_xml_value().

◆ xmloption

int xmloption = XMLOPTION_CONTENT

Definition at line 110 of file xml.c.

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

◆ XmlTableRoutine

const TableFuncRoutine XmlTableRoutine
Initial value:
=
{
.InitOpaque = XmlTableInitOpaque,
.SetDocument = XmlTableSetDocument,
.SetNamespace = XmlTableSetNamespace,
.SetRowFilter = XmlTableSetRowFilter,
.SetColumnFilter = XmlTableSetColumnFilter,
.FetchRow = XmlTableFetchRow,
.GetValue = XmlTableGetValue,
.DestroyOpaque = XmlTableDestroyOpaque
}
static void XmlTableInitOpaque(struct TableFuncScanState *state, int natts)
Definition: xml.c:4655
static void XmlTableSetNamespace(struct TableFuncScanState *state, const char *name, const char *uri)
Definition: xml.c:4760
static void XmlTableSetRowFilter(struct TableFuncScanState *state, const char *path)
Definition: xml.c:4786
static Datum XmlTableGetValue(struct TableFuncScanState *state, int colnum, Oid typid, int32 typmod, bool *isnull)
Definition: xml.c:4892
static void XmlTableSetDocument(struct TableFuncScanState *state, Datum value)
Definition: xml.c:4703
static void XmlTableDestroyOpaque(struct TableFuncScanState *state)
Definition: xml.c:5044
static bool XmlTableFetchRow(struct TableFuncScanState *state)
Definition: xml.c:4847
static void XmlTableSetColumnFilter(struct TableFuncScanState *state, const char *path, int colnum)
Definition: xml.c:4815

Definition at line 223 of file xml.c.

Referenced by ExecInitTableFuncScan().