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/nodeFuncs.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/datetime.h"
#include "utils/lsyscache.h"
#include "utils/memutils.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)
 
xmltypexmlconcat (List *args)
 
Datum xmlconcat2 (PG_FUNCTION_ARGS)
 
Datum texttoxml (PG_FUNCTION_ARGS)
 
Datum xmltotext (PG_FUNCTION_ARGS)
 
textxmltotext_with_xmloption (xmltype *data, XmlOptionType xmloption_arg)
 
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
 
int xmloption
 
const TableFuncRoutine XmlTableRoutine
 

Macro Definition Documentation

◆ NAMESPACE_SQLXML

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

Definition at line 229 of file xml.c.

◆ NAMESPACE_XSD

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

Definition at line 227 of file xml.c.

◆ NAMESPACE_XSI

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

Definition at line 228 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:1037
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143

Definition at line 219 of file xml.c.

◆ PG_XML_DEFAULT_VERSION

#define PG_XML_DEFAULT_VERSION   "1.0"

Definition at line 281 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 2493 of file xml.c.

◆ XML_VISIBLE_SCHEMAS_EXCLUDE

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

Definition at line 2491 of file xml.c.

Function Documentation

◆ _SPI_strdup()

static char* _SPI_strdup ( const char *  s)
static

Definition at line 2384 of file xml.c.

2385 {
2386  size_t len = strlen(s) + 1;
2387  char *ret = SPI_palloc(len);
2388 
2389  memcpy(ret, s, len);
2390  return ret;
2391 }
const void size_t len
void * SPI_palloc(Size size)
Definition: spi.c:1336

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

454 {
455  return (xmltype *) cstring_to_text(string);
456 }
Definition: c.h:622
text * cstring_to_text(const char *s)
Definition: varlena.c:188

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

2569 {
2571  int32 count = PG_GETARG_INT32(1);
2572  bool nulls = PG_GETARG_BOOL(2);
2573  bool tableforest = PG_GETARG_BOOL(3);
2574  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(4));
2575 
2576  StringInfoData result;
2577  Portal portal;
2578  uint64 i;
2579 
2580  initStringInfo(&result);
2581 
2582  if (!tableforest)
2583  {
2584  xmldata_root_element_start(&result, "table", NULL, targetns, true);
2585  appendStringInfoChar(&result, '\n');
2586  }
2587 
2588  SPI_connect();
2589  portal = SPI_cursor_find(name);
2590  if (portal == NULL)
2591  ereport(ERROR,
2592  (errcode(ERRCODE_UNDEFINED_CURSOR),
2593  errmsg("cursor \"%s\" does not exist", name)));
2594 
2595  SPI_cursor_fetch(portal, true, count);
2596  for (i = 0; i < SPI_processed; i++)
2597  SPI_sql_row_to_xmlelement(i, &result, NULL, nulls,
2598  tableforest, targetns, true);
2599 
2600  SPI_finish();
2601 
2602  if (!tableforest)
2603  xmldata_root_element_end(&result, "table");
2604 
2606 }
signed int int32
Definition: c.h:429
const char * name
Definition: encode.c:561
#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:45
Portal SPI_cursor_find(const char *name)
Definition: spi.c:1792
int SPI_connect(void)
Definition: spi.c:95
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Definition: spi.c:1804
int SPI_finish(void)
Definition: spi.c:183
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
char * text_to_cstring(const text *t)
Definition: varlena.c:221
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:446
static void xmldata_root_element_start(StringInfo result, const char *eltname, const char *xmlschema, const char *targetns, bool top_level)
Definition: xml.c:2622
static void xmldata_root_element_end(StringInfo result, const char *eltname)
Definition: xml.c:2649
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:3737
#define PG_RETURN_XML_P(x)
Definition: xml.h:54

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

2750 {
2752  bool nulls = PG_GETARG_BOOL(1);
2753  bool tableforest = PG_GETARG_BOOL(2);
2754  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2755  const char *xmlschema;
2756  Portal portal;
2757 
2758  SPI_connect();
2759  portal = SPI_cursor_find(name);
2760  if (portal == NULL)
2761  ereport(ERROR,
2762  (errcode(ERRCODE_UNDEFINED_CURSOR),
2763  errmsg("cursor \"%s\" does not exist", name)));
2764 
2765  xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2766  InvalidOid, nulls,
2767  tableforest, targetns));
2768  SPI_finish();
2769 
2770  PG_RETURN_XML_P(cstring_to_xmltype(xmlschema));
2771 }
#define InvalidOid
Definition: postgres_ext.h:36
TupleDesc tupDesc
Definition: portal.h:160
static char * _SPI_strdup(const char *s)
Definition: xml.c:2384
static xmltype * cstring_to_xmltype(const char *string)
Definition: xml.c:453
static const char * map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3167

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

2498 {
2499  return query_to_oid_list(XML_VISIBLE_SCHEMAS " ORDER BY nspname;");
2500 }
static List * query_to_oid_list(const char *query)
Definition: xml.c:2441
#define XML_VISIBLE_SCHEMAS
Definition: xml.c:2493

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

2505 {
2506  /* At the moment there is no order required here. */
2507  return query_to_oid_list("SELECT oid FROM pg_catalog.pg_class"
2508  " WHERE relkind IN ("
2509  CppAsString2(RELKIND_RELATION) ","
2510  CppAsString2(RELKIND_MATVIEW) ","
2511  CppAsString2(RELKIND_VIEW) ")"
2512  " AND pg_catalog.has_table_privilege(pg_class.oid, 'SELECT')"
2513  " AND relnamespace IN (" XML_VISIBLE_SCHEMAS ");");
2514 }
#define CppAsString2(x)
Definition: c.h:289

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

3051 {
3052  bool nulls = PG_GETARG_BOOL(0);
3053  bool tableforest = PG_GETARG_BOOL(1);
3054  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
3055 
3057  tableforest, targetns)));
3058 }
static StringInfo database_to_xml_internal(const char *xmlschema, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3007

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

3118 {
3119  bool nulls = PG_GETARG_BOOL(0);
3120  bool tableforest = PG_GETARG_BOOL(1);
3121  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
3122  StringInfo xmlschema;
3123 
3124  xmlschema = database_to_xmlschema_internal(nulls, tableforest, targetns);
3125 
3127  nulls, tableforest, targetns)));
3128 }
static StringInfo database_to_xmlschema_internal(bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3062

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

3009 {
3010  StringInfo result;
3011  List *nspid_list;
3012  ListCell *cell;
3013  char *xmlcn;
3014 
3016  true, false);
3017  result = makeStringInfo();
3018 
3019  xmldata_root_element_start(result, xmlcn, xmlschema, targetns, true);
3020  appendStringInfoChar(result, '\n');
3021 
3022  if (xmlschema)
3023  appendStringInfo(result, "%s\n\n", xmlschema);
3024 
3025  SPI_connect();
3026 
3027  nspid_list = database_get_xml_visible_schemas();
3028 
3029  foreach(cell, nspid_list)
3030  {
3031  Oid nspid = lfirst_oid(cell);
3032  StringInfo subres;
3033 
3034  subres = schema_to_xml_internal(nspid, NULL, nulls,
3035  tableforest, targetns, false);
3036 
3037  appendBinaryStringInfo(result, subres->data, subres->len);
3038  appendStringInfoChar(result, '\n');
3039  }
3040 
3041  SPI_finish();
3042 
3043  xmldata_root_element_end(result, xmlcn);
3044 
3045  return result;
3046 }
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2994
Oid MyDatabaseId
Definition: globals.c:89
#define lfirst_oid(lc)
Definition: pg_list.h:171
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:91
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227
Definition: pg_list.h:51
char * map_sql_identifier_to_xml_name(const char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:2035
static StringInfo schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2832
static List * database_get_xml_visible_schemas(void)
Definition: xml.c:2497

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

3106 {
3107  bool nulls = PG_GETARG_BOOL(0);
3108  bool tableforest = PG_GETARG_BOOL(1);
3109  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
3110 
3112  tableforest, targetns)));
3113 }

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

3064 {
3065  List *relid_list;
3066  List *nspid_list;
3067  List *tupdesc_list;
3068  ListCell *cell;
3069  StringInfo result;
3070 
3071  result = makeStringInfo();
3072 
3073  xsd_schema_element_start(result, targetns);
3074 
3075  SPI_connect();
3076 
3077  relid_list = database_get_xml_visible_tables();
3078  nspid_list = database_get_xml_visible_schemas();
3079 
3080  tupdesc_list = NIL;
3081  foreach(cell, relid_list)
3082  {
3083  Relation rel;
3084 
3085  rel = table_open(lfirst_oid(cell), AccessShareLock);
3086  tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
3087  table_close(rel, NoLock);
3088  }
3089 
3090  appendStringInfoString(result,
3091  map_sql_typecoll_to_xmlschema_types(tupdesc_list));
3092 
3093  appendStringInfoString(result,
3094  map_sql_catalog_to_xmlschema_types(nspid_list, nulls, tableforest, targetns));
3095 
3096  xsd_schema_element_end(result);
3097 
3098  SPI_finish();
3099 
3100  return result;
3101 }
List * lappend(List *list, void *datum)
Definition: list.c:336
#define NoLock
Definition: lockdefs.h:34
#define AccessShareLock
Definition: lockdefs.h:36
#define NIL
Definition: pg_list.h:65
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
TupleDesc rd_att
Definition: rel.h:110
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:111
static List * database_get_xml_visible_tables(void)
Definition: xml.c:2504
static const char * map_sql_catalog_to_xmlschema_types(List *nspid_list, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3345
static void xsd_schema_element_start(StringInfo result, const char *targetns)
Definition: xml.c:2897
static const char * map_sql_typecoll_to_xmlschema_types(List *tupdesc_list)
Definition: xml.c:3507
static void xsd_schema_element_end(StringInfo result)
Definition: xml.c:2914

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

2353 {
2355  const char *p;
2356 
2357  initStringInfo(&buf);
2358  for (p = str; *p; p++)
2359  {
2360  switch (*p)
2361  {
2362  case '&':
2363  appendStringInfoString(&buf, "&amp;");
2364  break;
2365  case '<':
2366  appendStringInfoString(&buf, "&lt;");
2367  break;
2368  case '>':
2369  appendStringInfoString(&buf, "&gt;");
2370  break;
2371  case '\r':
2372  appendStringInfoString(&buf, "&#x0d;");
2373  break;
2374  default:
2376  break;
2377  }
2378  }
2379  return buf.data;
2380 }
static char * buf
Definition: pg_test_fsync.c:67
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:128

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

3137 {
3138  StringInfoData result;
3139 
3140  initStringInfo(&result);
3141 
3142  if (a)
3143  appendStringInfoString(&result,
3144  map_sql_identifier_to_xml_name(a, true, true));
3145  if (b)
3146  appendStringInfo(&result, ".%s",
3147  map_sql_identifier_to_xml_name(b, true, true));
3148  if (c)
3149  appendStringInfo(&result, ".%s",
3150  map_sql_identifier_to_xml_name(c, true, true));
3151  if (d)
3152  appendStringInfo(&result, ".%s",
3153  map_sql_identifier_to_xml_name(d, true, true));
3154 
3155  return result.data;
3156 }
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 3345 of file xml.c.

3347 {
3348  char *dbname;
3349  char *xmlcn;
3350  char *catalogtypename;
3351  StringInfoData result;
3352  ListCell *cell;
3353 
3355 
3356  initStringInfo(&result);
3357 
3358  xmlcn = map_sql_identifier_to_xml_name(dbname, true, false);
3359 
3360  catalogtypename = map_multipart_sql_identifier_to_xml_name("CatalogType",
3361  dbname,
3362  NULL,
3363  NULL);
3364 
3365  appendStringInfo(&result,
3366  "<xsd:complexType name=\"%s\">\n", catalogtypename);
3367  appendStringInfoString(&result,
3368  " <xsd:all>\n");
3369 
3370  foreach(cell, nspid_list)
3371  {
3372  Oid nspid = lfirst_oid(cell);
3373  char *nspname = get_namespace_name(nspid);
3374  char *xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
3375  char *schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
3376  dbname,
3377  nspname,
3378  NULL);
3379 
3380  appendStringInfo(&result,
3381  " <xsd:element name=\"%s\" type=\"%s\"/>\n",
3382  xmlsn, schematypename);
3383  }
3384 
3385  appendStringInfoString(&result,
3386  " </xsd:all>\n");
3387  appendStringInfoString(&result,
3388  "</xsd:complexType>\n\n");
3389 
3390  appendStringInfo(&result,
3391  "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3392  xmlcn, catalogtypename);
3393 
3394  return result.data;
3395 }
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3326
char * dbname
Definition: streamutil.c:51
static char * map_multipart_sql_identifier_to_xml_name(const char *a, const char *b, const char *c, const char *d)
Definition: xml.c:3136

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

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

2037 {
2038 #ifdef USE_LIBXML
2040  const char *p;
2041 
2042  /*
2043  * SQL/XML doesn't make use of this case anywhere, so it's probably a
2044  * mistake.
2045  */
2046  Assert(fully_escaped || !escape_period);
2047 
2048  initStringInfo(&buf);
2049 
2050  for (p = ident; *p; p += pg_mblen(p))
2051  {
2052  if (*p == ':' && (p == ident || fully_escaped))
2053  appendStringInfoString(&buf, "_x003A_");
2054  else if (*p == '_' && *(p + 1) == 'x')
2055  appendStringInfoString(&buf, "_x005F_");
2056  else if (fully_escaped && p == ident &&
2057  pg_strncasecmp(p, "xml", 3) == 0)
2058  {
2059  if (*p == 'x')
2060  appendStringInfoString(&buf, "_x0078_");
2061  else
2062  appendStringInfoString(&buf, "_x0058_");
2063  }
2064  else if (escape_period && *p == '.')
2065  appendStringInfoString(&buf, "_x002E_");
2066  else
2067  {
2068  pg_wchar u = sqlchar_to_unicode(p);
2069 
2070  if ((p == ident)
2071  ? !is_valid_xml_namefirst(u)
2072  : !is_valid_xml_namechar(u))
2073  appendStringInfo(&buf, "_x%04X_", (unsigned int) u);
2074  else
2076  }
2077  }
2078 
2079  return buf.data;
2080 #else /* not USE_LIBXML */
2081  NO_XML_SUPPORT();
2082  return NULL;
2083 #endif /* not USE_LIBXML */
2084 }
Assert(fmt[strlen(fmt) - 1] !='\n')
unsigned int pg_wchar
Definition: mbprint.c:31
int pg_mblen(const char *mbstr)
Definition: mbutils.c:966
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
#define NO_XML_SUPPORT()
Definition: xml.c:219

References appendBinaryStringInfo(), appendStringInfo(), appendStringInfoString(), Assert(), buf, 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 3272 of file xml.c.

3274 {
3275  char *dbname;
3276  char *nspname;
3277  char *xmlsn;
3278  char *schematypename;
3279  StringInfoData result;
3280  ListCell *cell;
3281 
3283  nspname = get_namespace_name(nspid);
3284 
3285  initStringInfo(&result);
3286 
3287  xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
3288 
3289  schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
3290  dbname,
3291  nspname,
3292  NULL);
3293 
3294  appendStringInfo(&result,
3295  "<xsd:complexType name=\"%s\">\n", schematypename);
3296  if (!tableforest)
3297  appendStringInfoString(&result,
3298  " <xsd:all>\n");
3299  else
3300  appendStringInfoString(&result,
3301  " <xsd:sequence>\n");
3302 
3303  foreach(cell, relid_list)
3304  {
3305  Oid relid = lfirst_oid(cell);
3306  char *relname = get_rel_name(relid);
3307  char *xmltn = map_sql_identifier_to_xml_name(relname, true, false);
3308  char *tabletypename = map_multipart_sql_identifier_to_xml_name(tableforest ? "RowType" : "TableType",
3309  dbname,
3310  nspname,
3311  relname);
3312 
3313  if (!tableforest)
3314  appendStringInfo(&result,
3315  " <xsd:element name=\"%s\" type=\"%s\"/>\n",
3316  xmltn, tabletypename);
3317  else
3318  appendStringInfo(&result,
3319  " <xsd:element name=\"%s\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n",
3320  xmltn, tabletypename);
3321  }
3322 
3323  if (!tableforest)
3324  appendStringInfoString(&result,
3325  " </xsd:all>\n");
3326  else
3327  appendStringInfoString(&result,
3328  " </xsd:sequence>\n");
3329  appendStringInfoString(&result,
3330  "</xsd:complexType>\n\n");
3331 
3332  appendStringInfo(&result,
3333  "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3334  xmlsn, schematypename);
3335 
3336  return result.data;
3337 }
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1909
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, 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 3167 of file xml.c.

3169 {
3170  int i;
3171  char *xmltn;
3172  char *tabletypename;
3173  char *rowtypename;
3174  StringInfoData result;
3175 
3176  initStringInfo(&result);
3177 
3178  if (OidIsValid(relid))
3179  {
3180  HeapTuple tuple;
3181  Form_pg_class reltuple;
3182 
3183  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
3184  if (!HeapTupleIsValid(tuple))
3185  elog(ERROR, "cache lookup failed for relation %u", relid);
3186  reltuple = (Form_pg_class) GETSTRUCT(tuple);
3187 
3188  xmltn = map_sql_identifier_to_xml_name(NameStr(reltuple->relname),
3189  true, false);
3190 
3191  tabletypename = map_multipart_sql_identifier_to_xml_name("TableType",
3193  get_namespace_name(reltuple->relnamespace),
3194  NameStr(reltuple->relname));
3195 
3196  rowtypename = map_multipart_sql_identifier_to_xml_name("RowType",
3198  get_namespace_name(reltuple->relnamespace),
3199  NameStr(reltuple->relname));
3200 
3201  ReleaseSysCache(tuple);
3202  }
3203  else
3204  {
3205  if (tableforest)
3206  xmltn = "row";
3207  else
3208  xmltn = "table";
3209 
3210  tabletypename = "TableType";
3211  rowtypename = "RowType";
3212  }
3213 
3214  xsd_schema_element_start(&result, targetns);
3215 
3216  appendStringInfoString(&result,
3218 
3219  appendStringInfo(&result,
3220  "<xsd:complexType name=\"%s\">\n"
3221  " <xsd:sequence>\n",
3222  rowtypename);
3223 
3224  for (i = 0; i < tupdesc->natts; i++)
3225  {
3226  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
3227 
3228  if (att->attisdropped)
3229  continue;
3230  appendStringInfo(&result,
3231  " <xsd:element name=\"%s\" type=\"%s\"%s></xsd:element>\n",
3233  true, false),
3234  map_sql_type_to_xml_name(att->atttypid, -1),
3235  nulls ? " nillable=\"true\"" : " minOccurs=\"0\"");
3236  }
3237 
3238  appendStringInfoString(&result,
3239  " </xsd:sequence>\n"
3240  "</xsd:complexType>\n\n");
3241 
3242  if (!tableforest)
3243  {
3244  appendStringInfo(&result,
3245  "<xsd:complexType name=\"%s\">\n"
3246  " <xsd:sequence>\n"
3247  " <xsd:element name=\"row\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n"
3248  " </xsd:sequence>\n"
3249  "</xsd:complexType>\n\n",
3250  tabletypename, rowtypename);
3251 
3252  appendStringInfo(&result,
3253  "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3254  xmltn, tabletypename);
3255  }
3256  else
3257  appendStringInfo(&result,
3258  "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3259  xmltn, rowtypename);
3260 
3261  xsd_schema_element_end(&result);
3262 
3263  return result.data;
3264 }
#define NameStr(name)
Definition: c.h:681
#define OidIsValid(objectId)
Definition: c.h:710
#define elog(elevel,...)
Definition: elog.h:218
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:649
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
#define list_make1(x1)
Definition: pg_list.h:206
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1221
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1173
@ RELOID
Definition: syscache.h:89
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static const char * map_sql_type_to_xml_name(Oid typeoid, int typmod)
Definition: xml.c:3402

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(), RELOID, 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 3402 of file xml.c.

3403 {
3404  StringInfoData result;
3405 
3406  initStringInfo(&result);
3407 
3408  switch (typeoid)
3409  {
3410  case BPCHAROID:
3411  if (typmod == -1)
3412  appendStringInfoString(&result, "CHAR");
3413  else
3414  appendStringInfo(&result, "CHAR_%d", typmod - VARHDRSZ);
3415  break;
3416  case VARCHAROID:
3417  if (typmod == -1)
3418  appendStringInfoString(&result, "VARCHAR");
3419  else
3420  appendStringInfo(&result, "VARCHAR_%d", typmod - VARHDRSZ);
3421  break;
3422  case NUMERICOID:
3423  if (typmod == -1)
3424  appendStringInfoString(&result, "NUMERIC");
3425  else
3426  appendStringInfo(&result, "NUMERIC_%d_%d",
3427  ((typmod - VARHDRSZ) >> 16) & 0xffff,
3428  (typmod - VARHDRSZ) & 0xffff);
3429  break;
3430  case INT4OID:
3431  appendStringInfoString(&result, "INTEGER");
3432  break;
3433  case INT2OID:
3434  appendStringInfoString(&result, "SMALLINT");
3435  break;
3436  case INT8OID:
3437  appendStringInfoString(&result, "BIGINT");
3438  break;
3439  case FLOAT4OID:
3440  appendStringInfoString(&result, "REAL");
3441  break;
3442  case FLOAT8OID:
3443  appendStringInfoString(&result, "DOUBLE");
3444  break;
3445  case BOOLOID:
3446  appendStringInfoString(&result, "BOOLEAN");
3447  break;
3448  case TIMEOID:
3449  if (typmod == -1)
3450  appendStringInfoString(&result, "TIME");
3451  else
3452  appendStringInfo(&result, "TIME_%d", typmod);
3453  break;
3454  case TIMETZOID:
3455  if (typmod == -1)
3456  appendStringInfoString(&result, "TIME_WTZ");
3457  else
3458  appendStringInfo(&result, "TIME_WTZ_%d", typmod);
3459  break;
3460  case TIMESTAMPOID:
3461  if (typmod == -1)
3462  appendStringInfoString(&result, "TIMESTAMP");
3463  else
3464  appendStringInfo(&result, "TIMESTAMP_%d", typmod);
3465  break;
3466  case TIMESTAMPTZOID:
3467  if (typmod == -1)
3468  appendStringInfoString(&result, "TIMESTAMP_WTZ");
3469  else
3470  appendStringInfo(&result, "TIMESTAMP_WTZ_%d", typmod);
3471  break;
3472  case DATEOID:
3473  appendStringInfoString(&result, "DATE");
3474  break;
3475  case XMLOID:
3476  appendStringInfoString(&result, "XML");
3477  break;
3478  default:
3479  {
3480  HeapTuple tuple;
3481  Form_pg_type typtuple;
3482 
3483  tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typeoid));
3484  if (!HeapTupleIsValid(tuple))
3485  elog(ERROR, "cache lookup failed for type %u", typeoid);
3486  typtuple = (Form_pg_type) GETSTRUCT(tuple);
3487 
3488  appendStringInfoString(&result,
3489  map_multipart_sql_identifier_to_xml_name((typtuple->typtype == TYPTYPE_DOMAIN) ? "Domain" : "UDT",
3491  get_namespace_name(typtuple->typnamespace),
3492  NameStr(typtuple->typname)));
3493 
3494  ReleaseSysCache(tuple);
3495  }
3496  }
3497 
3498  return result.data;
3499 }
#define VARHDRSZ
Definition: c.h:627
FormData_pg_type * Form_pg_type
Definition: pg_type.h:261
@ TYPEOID
Definition: syscache.h:114

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(), TYPEOID, 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 3562 of file xml.c.

3563 {
3564  StringInfoData result;
3565  const char *typename = map_sql_type_to_xml_name(typeoid, typmod);
3566 
3567  initStringInfo(&result);
3568 
3569  if (typeoid == XMLOID)
3570  {
3571  appendStringInfoString(&result,
3572  "<xsd:complexType mixed=\"true\">\n"
3573  " <xsd:sequence>\n"
3574  " <xsd:any name=\"element\" minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>\n"
3575  " </xsd:sequence>\n"
3576  "</xsd:complexType>\n");
3577  }
3578  else
3579  {
3580  appendStringInfo(&result,
3581  "<xsd:simpleType name=\"%s\">\n", typename);
3582 
3583  switch (typeoid)
3584  {
3585  case BPCHAROID:
3586  case VARCHAROID:
3587  case TEXTOID:
3588  appendStringInfoString(&result,
3589  " <xsd:restriction base=\"xsd:string\">\n");
3590  if (typmod != -1)
3591  appendStringInfo(&result,
3592  " <xsd:maxLength value=\"%d\"/>\n",
3593  typmod - VARHDRSZ);
3594  appendStringInfoString(&result, " </xsd:restriction>\n");
3595  break;
3596 
3597  case BYTEAOID:
3598  appendStringInfo(&result,
3599  " <xsd:restriction base=\"xsd:%s\">\n"
3600  " </xsd:restriction>\n",
3601  xmlbinary == XMLBINARY_BASE64 ? "base64Binary" : "hexBinary");
3602  break;
3603 
3604  case NUMERICOID:
3605  if (typmod != -1)
3606  appendStringInfo(&result,
3607  " <xsd:restriction base=\"xsd:decimal\">\n"
3608  " <xsd:totalDigits value=\"%d\"/>\n"
3609  " <xsd:fractionDigits value=\"%d\"/>\n"
3610  " </xsd:restriction>\n",
3611  ((typmod - VARHDRSZ) >> 16) & 0xffff,
3612  (typmod - VARHDRSZ) & 0xffff);
3613  break;
3614 
3615  case INT2OID:
3616  appendStringInfo(&result,
3617  " <xsd:restriction base=\"xsd:short\">\n"
3618  " <xsd:maxInclusive value=\"%d\"/>\n"
3619  " <xsd:minInclusive value=\"%d\"/>\n"
3620  " </xsd:restriction>\n",
3621  SHRT_MAX, SHRT_MIN);
3622  break;
3623 
3624  case INT4OID:
3625  appendStringInfo(&result,
3626  " <xsd:restriction base=\"xsd:int\">\n"
3627  " <xsd:maxInclusive value=\"%d\"/>\n"
3628  " <xsd:minInclusive value=\"%d\"/>\n"
3629  " </xsd:restriction>\n",
3630  INT_MAX, INT_MIN);
3631  break;
3632 
3633  case INT8OID:
3634  appendStringInfo(&result,
3635  " <xsd:restriction base=\"xsd:long\">\n"
3636  " <xsd:maxInclusive value=\"" INT64_FORMAT "\"/>\n"
3637  " <xsd:minInclusive value=\"" INT64_FORMAT "\"/>\n"
3638  " </xsd:restriction>\n",
3639  PG_INT64_MAX,
3640  PG_INT64_MIN);
3641  break;
3642 
3643  case FLOAT4OID:
3644  appendStringInfoString(&result,
3645  " <xsd:restriction base=\"xsd:float\"></xsd:restriction>\n");
3646  break;
3647 
3648  case FLOAT8OID:
3649  appendStringInfoString(&result,
3650  " <xsd:restriction base=\"xsd:double\"></xsd:restriction>\n");
3651  break;
3652 
3653  case BOOLOID:
3654  appendStringInfoString(&result,
3655  " <xsd:restriction base=\"xsd:boolean\"></xsd:restriction>\n");
3656  break;
3657 
3658  case TIMEOID:
3659  case TIMETZOID:
3660  {
3661  const char *tz = (typeoid == TIMETZOID ? "(\\+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
3662 
3663  if (typmod == -1)
3664  appendStringInfo(&result,
3665  " <xsd:restriction base=\"xsd:time\">\n"
3666  " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}(.\\p{Nd}+)?%s\"/>\n"
3667  " </xsd:restriction>\n", tz);
3668  else if (typmod == 0)
3669  appendStringInfo(&result,
3670  " <xsd:restriction base=\"xsd:time\">\n"
3671  " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3672  " </xsd:restriction>\n", tz);
3673  else
3674  appendStringInfo(&result,
3675  " <xsd:restriction base=\"xsd:time\">\n"
3676  " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}.\\p{Nd}{%d}%s\"/>\n"
3677  " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
3678  break;
3679  }
3680 
3681  case TIMESTAMPOID:
3682  case TIMESTAMPTZOID:
3683  {
3684  const char *tz = (typeoid == TIMESTAMPTZOID ? "(\\+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
3685 
3686  if (typmod == -1)
3687  appendStringInfo(&result,
3688  " <xsd:restriction base=\"xsd:dateTime\">\n"
3689  " <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"
3690  " </xsd:restriction>\n", tz);
3691  else if (typmod == 0)
3692  appendStringInfo(&result,
3693  " <xsd:restriction base=\"xsd:dateTime\">\n"
3694  " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3695  " </xsd:restriction>\n", tz);
3696  else
3697  appendStringInfo(&result,
3698  " <xsd:restriction base=\"xsd:dateTime\">\n"
3699  " <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"
3700  " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
3701  break;
3702  }
3703 
3704  case DATEOID:
3705  appendStringInfoString(&result,
3706  " <xsd:restriction base=\"xsd:date\">\n"
3707  " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}\"/>\n"
3708  " </xsd:restriction>\n");
3709  break;
3710 
3711  default:
3712  if (get_typtype(typeoid) == TYPTYPE_DOMAIN)
3713  {
3714  Oid base_typeoid;
3715  int32 base_typmod = -1;
3716 
3717  base_typeoid = getBaseTypeAndTypmod(typeoid, &base_typmod);
3718 
3719  appendStringInfo(&result,
3720  " <xsd:restriction base=\"%s\"/>\n",
3721  map_sql_type_to_xml_name(base_typeoid, base_typmod));
3722  }
3723  break;
3724  }
3725  appendStringInfoString(&result, "</xsd:simpleType>\n");
3726  }
3727 
3728  return result.data;
3729 }
#define INT64_FORMAT
Definition: c.h:483
#define PG_INT64_MAX
Definition: c.h:527
#define PG_INT64_MIN
Definition: c.h:526
char get_typtype(Oid typid)
Definition: lsyscache.c:2586
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2495
int xmlbinary
Definition: xml.c:97
@ 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 3507 of file xml.c.

3508 {
3509  List *uniquetypes = NIL;
3510  int i;
3511  StringInfoData result;
3512  ListCell *cell0;
3513 
3514  /* extract all column types used in the set of TupleDescs */
3515  foreach(cell0, tupdesc_list)
3516  {
3517  TupleDesc tupdesc = (TupleDesc) lfirst(cell0);
3518 
3519  for (i = 0; i < tupdesc->natts; i++)
3520  {
3521  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
3522 
3523  if (att->attisdropped)
3524  continue;
3525  uniquetypes = list_append_unique_oid(uniquetypes, att->atttypid);
3526  }
3527  }
3528 
3529  /* add base types of domains */
3530  foreach(cell0, uniquetypes)
3531  {
3532  Oid typid = lfirst_oid(cell0);
3533  Oid basetypid = getBaseType(typid);
3534 
3535  if (basetypid != typid)
3536  uniquetypes = list_append_unique_oid(uniquetypes, basetypid);
3537  }
3538 
3539  /* Convert to textual form */
3540  initStringInfo(&result);
3541 
3542  foreach(cell0, uniquetypes)
3543  {
3544  appendStringInfo(&result, "%s\n",
3546  -1));
3547  }
3548 
3549  return result.data;
3550 }
List * list_append_unique_oid(List *list, Oid datum)
Definition: list.c:1339
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2478
#define lfirst(lc)
Definition: pg_list.h:169
struct TupleDescData * TupleDesc
Definition: tupdesc.h:89
static const char * map_sql_type_to_xmlschema_type(Oid typeoid, int typmod)
Definition: xml.c:3562

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

2134 {
2136  {
2137  ArrayType *array;
2138  Oid elmtype;
2139  int16 elmlen;
2140  bool elmbyval;
2141  char elmalign;
2142  int num_elems;
2143  Datum *elem_values;
2144  bool *elem_nulls;
2146  int i;
2147 
2148  array = DatumGetArrayTypeP(value);
2149  elmtype = ARR_ELEMTYPE(array);
2150  get_typlenbyvalalign(elmtype, &elmlen, &elmbyval, &elmalign);
2151 
2152  deconstruct_array(array, elmtype,
2153  elmlen, elmbyval, elmalign,
2154  &elem_values, &elem_nulls,
2155  &num_elems);
2156 
2157  initStringInfo(&buf);
2158 
2159  for (i = 0; i < num_elems; i++)
2160  {
2161  if (elem_nulls[i])
2162  continue;
2163  appendStringInfoString(&buf, "<element>");
2165  map_sql_value_to_xml_value(elem_values[i],
2166  elmtype, true));
2167  appendStringInfoString(&buf, "</element>");
2168  }
2169 
2170  pfree(elem_values);
2171  pfree(elem_nulls);
2172 
2173  return buf.data;
2174  }
2175  else
2176  {
2177  Oid typeOut;
2178  bool isvarlena;
2179  char *str;
2180 
2181  /*
2182  * Flatten domains; the special-case treatments below should apply to,
2183  * eg, domains over boolean not just boolean.
2184  */
2185  type = getBaseType(type);
2186 
2187  /*
2188  * Special XSD formatting for some data types
2189  */
2190  switch (type)
2191  {
2192  case BOOLOID:
2193  if (DatumGetBool(value))
2194  return "true";
2195  else
2196  return "false";
2197 
2198  case DATEOID:
2199  {
2200  DateADT date;
2201  struct pg_tm tm;
2202  char buf[MAXDATELEN + 1];
2203 
2205  /* XSD doesn't support infinite values */
2206  if (DATE_NOT_FINITE(date))
2207  ereport(ERROR,
2208  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2209  errmsg("date out of range"),
2210  errdetail("XML does not support infinite date values.")));
2212  &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
2214 
2215  return pstrdup(buf);
2216  }
2217 
2218  case TIMESTAMPOID:
2219  {
2221  struct pg_tm tm;
2222  fsec_t fsec;
2223  char buf[MAXDATELEN + 1];
2224 
2226 
2227  /* XSD doesn't support infinite values */
2229  ereport(ERROR,
2230  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2231  errmsg("timestamp out of range"),
2232  errdetail("XML does not support infinite timestamp values.")));
2233  else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
2234  EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
2235  else
2236  ereport(ERROR,
2237  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2238  errmsg("timestamp out of range")));
2239 
2240  return pstrdup(buf);
2241  }
2242 
2243  case TIMESTAMPTZOID:
2244  {
2246  struct pg_tm tm;
2247  int tz;
2248  fsec_t fsec;
2249  const char *tzn = NULL;
2250  char buf[MAXDATELEN + 1];
2251 
2253 
2254  /* XSD doesn't support infinite values */
2256  ereport(ERROR,
2257  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2258  errmsg("timestamp out of range"),
2259  errdetail("XML does not support infinite timestamp values.")));
2260  else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
2261  EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
2262  else
2263  ereport(ERROR,
2264  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2265  errmsg("timestamp out of range")));
2266 
2267  return pstrdup(buf);
2268  }
2269 
2270 #ifdef USE_LIBXML
2271  case BYTEAOID:
2272  {
2273  bytea *bstr = DatumGetByteaPP(value);
2274  PgXmlErrorContext *xmlerrcxt;
2275  volatile xmlBufferPtr buf = NULL;
2276  volatile xmlTextWriterPtr writer = NULL;
2277  char *result;
2278 
2279  xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
2280 
2281  PG_TRY();
2282  {
2283  buf = xmlBufferCreate();
2284  if (buf == NULL || xmlerrcxt->err_occurred)
2285  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
2286  "could not allocate xmlBuffer");
2287  writer = xmlNewTextWriterMemory(buf, 0);
2288  if (writer == NULL || xmlerrcxt->err_occurred)
2289  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
2290  "could not allocate xmlTextWriter");
2291 
2292  if (xmlbinary == XMLBINARY_BASE64)
2293  xmlTextWriterWriteBase64(writer, VARDATA_ANY(bstr),
2294  0, VARSIZE_ANY_EXHDR(bstr));
2295  else
2296  xmlTextWriterWriteBinHex(writer, VARDATA_ANY(bstr),
2297  0, VARSIZE_ANY_EXHDR(bstr));
2298 
2299  /* we MUST do this now to flush data out to the buffer */
2300  xmlFreeTextWriter(writer);
2301  writer = NULL;
2302 
2303  result = pstrdup((const char *) xmlBufferContent(buf));
2304  }
2305  PG_CATCH();
2306  {
2307  if (writer)
2308  xmlFreeTextWriter(writer);
2309  if (buf)
2310  xmlBufferFree(buf);
2311 
2312  pg_xml_done(xmlerrcxt, true);
2313 
2314  PG_RE_THROW();
2315  }
2316  PG_END_TRY();
2317 
2318  xmlBufferFree(buf);
2319 
2320  pg_xml_done(xmlerrcxt, false);
2321 
2322  return result;
2323  }
2324 #endif /* USE_LIBXML */
2325 
2326  }
2327 
2328  /*
2329  * otherwise, just use the type's native text representation
2330  */
2331  getTypeOutputInfo(type, &typeOut, &isvarlena);
2332  str = OidOutputFunctionCall(typeOut, value);
2333 
2334  /* ... exactly as-is for XML, and when escaping is not wanted */
2335  if (type == XMLOID || !xml_escape_strings)
2336  return str;
2337 
2338  /* otherwise, translate special characters as needed */
2339  return escape_xml(str);
2340  }
2341 }
#define DatumGetArrayTypeP(X)
Definition: array.h:254
#define ARR_ELEMTYPE(a)
Definition: array.h:285
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3491
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:309
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:4258
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
Definition: datetime.c:4143
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1816
signed short int16
Definition: c.h:428
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:161
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:202
#define DATE_NOT_FINITE(j)
Definition: date.h:43
int32 DateADT
Definition: date.h:23
#define DatumGetDateADT(X)
Definition: date.h:53
#define PG_RE_THROW()
Definition: elog.h:340
#define PG_END_TRY()
Definition: elog.h:324
#define PG_TRY()
Definition: elog.h:299
#define PG_CATCH()
Definition: elog.h:309
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1639
#define DatumGetByteaPP(X)
Definition: fmgr.h:291
#define MAXDATELEN
Definition: datetime.h:201
static struct @151 value
static struct pg_tm tm
Definition: localtime.c:102
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2864
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2228
#define type_is_array_domain(typid)
Definition: lsyscache.h:204
char * pstrdup(const char *in)
Definition: mcxt.c:1305
void pfree(void *pointer)
Definition: mcxt.c:1175
#define USE_XSD_DATES
Definition: miscadmin.h:233
long date
Definition: pgtypes_date.h:9
int64 timestamp
uintptr_t Datum
Definition: postgres.h:411
#define DatumGetBool(X)
Definition: postgres.h:437
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
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
#define DatumGetTimestamp(X)
Definition: timestamp.h:27
char * escape_xml(const char *str)
Definition: xml.c:2352
char * map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
Definition: xml.c:2133
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(), generate_unaccent_rules::str, timestamp2tm(), TIMESTAMP_NOT_FINITE, tm, pg_tm::tm_mday, pg_tm::tm_mon, pg_tm::tm_year, generate_unaccent_rules::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 2091 of file xml.c.

2092 {
2094  const char *p;
2095 
2096  initStringInfo(&buf);
2097 
2098  for (p = name; *p; p += pg_mblen(p))
2099  {
2100  if (*p == '_' && *(p + 1) == 'x'
2101  && isxdigit((unsigned char) *(p + 2))
2102  && isxdigit((unsigned char) *(p + 3))
2103  && isxdigit((unsigned char) *(p + 4))
2104  && isxdigit((unsigned char) *(p + 5))
2105  && *(p + 6) == '_')
2106  {
2107  char cbuf[MAX_UNICODE_EQUIVALENT_STRING + 1];
2108  unsigned int u;
2109 
2110  sscanf(p + 2, "%X", &u);
2111  pg_unicode_to_server(u, (unsigned char *) cbuf);
2112  appendStringInfoString(&buf, cbuf);
2113  p += 6;
2114  }
2115  else
2117  }
2118 
2119  return buf.data;
2120 }
void pg_unicode_to_server(pg_wchar c, unsigned char *s)
Definition: mbutils.c:864
#define MAX_UNICODE_EQUIVALENT_STRING
Definition: pg_wchar.h:327

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

2442 {
2443  uint64 i;
2444  List *list = NIL;
2445  int spi_result;
2446 
2447  spi_result = SPI_execute(query, true, 0);
2448  if (spi_result != SPI_OK_SELECT)
2449  elog(ERROR, "SPI_execute returned %s for %s",
2450  SPI_result_code_string(spi_result), query);
2451 
2452  for (i = 0; i < SPI_processed; i++)
2453  {
2454  Datum oid;
2455  bool isnull;
2456 
2457  oid = SPI_getbinval(SPI_tuptable->vals[i],
2459  1,
2460  &isnull);
2461  if (!isnull)
2463  }
2464 
2465  return list;
2466 }
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
#define DatumGetObjectId(X)
Definition: postgres.h:544
SPITupleTable * SPI_tuptable
Definition: spi.c:46
const char * SPI_result_code_string(int code)
Definition: spi.c:1970
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:594
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition: spi.c:1250
#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 2554 of file xml.c.

2555 {
2556  char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2557  bool nulls = PG_GETARG_BOOL(1);
2558  bool tableforest = PG_GETARG_BOOL(2);
2559  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2560 
2562  NULL, nulls, tableforest,
2563  targetns, true)));
2564 }
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:2656

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

2797 {
2798  char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2799  bool nulls = PG_GETARG_BOOL(1);
2800  bool tableforest = PG_GETARG_BOOL(2);
2801  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2802 
2803  const char *xmlschema;
2804  SPIPlanPtr plan;
2805  Portal portal;
2806 
2807  SPI_connect();
2808 
2809  if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
2810  elog(ERROR, "SPI_prepare(\"%s\") failed", query);
2811 
2812  if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
2813  elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
2814 
2815  xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2816  InvalidOid, nulls, tableforest, targetns));
2817  SPI_cursor_close(portal);
2818  SPI_finish();
2819 
2821  xmlschema, nulls, tableforest,
2822  targetns, true)));
2823 }
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1443
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:858
void SPI_cursor_close(Portal portal)
Definition: spi.c:1860

References _SPI_strdup(), elog, ERROR, InvalidOid, map_sql_table_to_xmlschema(), PG_GETARG_BOOL, PG_GETARG_TEXT_PP, PG_RETURN_XML_P, 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 2656 of file xml.c.

2659 {
2660  StringInfo result;
2661  char *xmltn;
2662  uint64 i;
2663 
2664  if (tablename)
2665  xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
2666  else
2667  xmltn = "table";
2668 
2669  result = makeStringInfo();
2670 
2671  SPI_connect();
2672  if (SPI_execute(query, true, 0) != SPI_OK_SELECT)
2673  ereport(ERROR,
2674  (errcode(ERRCODE_DATA_EXCEPTION),
2675  errmsg("invalid query")));
2676 
2677  if (!tableforest)
2678  {
2679  xmldata_root_element_start(result, xmltn, xmlschema,
2680  targetns, top_level);
2681  appendStringInfoChar(result, '\n');
2682  }
2683 
2684  if (xmlschema)
2685  appendStringInfo(result, "%s\n\n", xmlschema);
2686 
2687  for (i = 0; i < SPI_processed; i++)
2688  SPI_sql_row_to_xmlelement(i, result, tablename, nulls,
2689  tableforest, targetns, top_level);
2690 
2691  if (!tableforest)
2692  xmldata_root_element_end(result, xmltn);
2693 
2694  SPI_finish();
2695 
2696  return result;
2697 }

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

2721 {
2722  char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2723  bool nulls = PG_GETARG_BOOL(1);
2724  bool tableforest = PG_GETARG_BOOL(2);
2725  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2726  const char *result;
2727  SPIPlanPtr plan;
2728  Portal portal;
2729 
2730  SPI_connect();
2731 
2732  if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
2733  elog(ERROR, "SPI_prepare(\"%s\") failed", query);
2734 
2735  if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
2736  elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
2737 
2739  InvalidOid, nulls,
2740  tableforest, targetns));
2741  SPI_cursor_close(portal);
2742  SPI_finish();
2743 
2745 }

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

2471 {
2472  StringInfoData query;
2473 
2474  initStringInfo(&query);
2475  appendStringInfo(&query, "SELECT oid FROM pg_catalog.pg_class"
2476  " WHERE relnamespace = %u AND relkind IN ("
2477  CppAsString2(RELKIND_RELATION) ","
2478  CppAsString2(RELKIND_MATVIEW) ","
2479  CppAsString2(RELKIND_VIEW) ")"
2480  " AND pg_catalog.has_table_privilege (oid, 'SELECT')"
2481  " ORDER BY relname;", nspid);
2482 
2483  return query_to_oid_list(query.data);
2484 }

References appendStringInfo(), CppAsString2, StringInfoData::data, initStringInfo(), 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 2875 of file xml.c.

2876 {
2877  Name name = PG_GETARG_NAME(0);
2878  bool nulls = PG_GETARG_BOOL(1);
2879  bool tableforest = PG_GETARG_BOOL(2);
2880  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2881 
2882  char *schemaname;
2883  Oid nspid;
2884 
2885  schemaname = NameStr(*name);
2886  nspid = LookupExplicitNamespace(schemaname, false);
2887 
2889  nulls, tableforest, targetns, true)));
2890 }
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
Definition: namespace.c:2939
Definition: c.h:676

References LookupExplicitNamespace(), name, NameStr, 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 2979 of file xml.c.

2980 {
2981  Name name = PG_GETARG_NAME(0);
2982  bool nulls = PG_GETARG_BOOL(1);
2983  bool tableforest = PG_GETARG_BOOL(2);
2984  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2985  char *schemaname;
2986  Oid nspid;
2987  StringInfo xmlschema;
2988 
2989  schemaname = NameStr(*name);
2990  nspid = LookupExplicitNamespace(schemaname, false);
2991 
2992  xmlschema = schema_to_xmlschema_internal(schemaname, nulls,
2993  tableforest, targetns);
2994 
2996  xmlschema->data, nulls,
2997  tableforest, targetns, true)));
2998 }
static StringInfo schema_to_xmlschema_internal(const char *schemaname, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:2921

References StringInfoData::data, LookupExplicitNamespace(), name, NameStr, 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 2832 of file xml.c.

2834 {
2835  StringInfo result;
2836  char *xmlsn;
2837  List *relid_list;
2838  ListCell *cell;
2839 
2841  true, false);
2842  result = makeStringInfo();
2843 
2844  xmldata_root_element_start(result, xmlsn, xmlschema, targetns, top_level);
2845  appendStringInfoChar(result, '\n');
2846 
2847  if (xmlschema)
2848  appendStringInfo(result, "%s\n\n", xmlschema);
2849 
2850  SPI_connect();
2851 
2852  relid_list = schema_get_xml_visible_tables(nspid);
2853 
2854  foreach(cell, relid_list)
2855  {
2856  Oid relid = lfirst_oid(cell);
2857  StringInfo subres;
2858 
2859  subres = table_to_xml_internal(relid, NULL, nulls, tableforest,
2860  targetns, false);
2861 
2862  appendBinaryStringInfo(result, subres->data, subres->len);
2863  appendStringInfoChar(result, '\n');
2864  }
2865 
2866  SPI_finish();
2867 
2868  xmldata_root_element_end(result, xmlsn);
2869 
2870  return result;
2871 }
static List * schema_get_xml_visible_tables(Oid nspid)
Definition: xml.c:2470
static StringInfo table_to_xml_internal(Oid relid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2523

References appendBinaryStringInfo(), appendStringInfo(), appendStringInfoChar(), StringInfoData::data, get_namespace_name(), StringInfoData::len, lfirst_oid, makeStringInfo(), map_sql_identifier_to_xml_name(), 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 2966 of file xml.c.

2967 {
2968  Name name = PG_GETARG_NAME(0);
2969  bool nulls = PG_GETARG_BOOL(1);
2970  bool tableforest = PG_GETARG_BOOL(2);
2971  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2972 
2974  nulls, tableforest, targetns)));
2975 }

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

2923 {
2924  Oid nspid;
2925  List *relid_list;
2926  List *tupdesc_list;
2927  ListCell *cell;
2928  StringInfo result;
2929 
2930  result = makeStringInfo();
2931 
2932  nspid = LookupExplicitNamespace(schemaname, false);
2933 
2934  xsd_schema_element_start(result, targetns);
2935 
2936  SPI_connect();
2937 
2938  relid_list = schema_get_xml_visible_tables(nspid);
2939 
2940  tupdesc_list = NIL;
2941  foreach(cell, relid_list)
2942  {
2943  Relation rel;
2944 
2945  rel = table_open(lfirst_oid(cell), AccessShareLock);
2946  tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2947  table_close(rel, NoLock);
2948  }
2949 
2950  appendStringInfoString(result,
2951  map_sql_typecoll_to_xmlschema_types(tupdesc_list));
2952 
2953  appendStringInfoString(result,
2954  map_sql_schema_to_xmlschema_types(nspid, relid_list,
2955  nulls, tableforest, targetns));
2956 
2957  xsd_schema_element_end(result);
2958 
2959  SPI_finish();
2960 
2961  return result;
2962 }
static const char * map_sql_schema_to_xmlschema_types(Oid nspid, List *relid_list, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3272

References AccessShareLock, appendStringInfoString(), CreateTupleDescCopy(), lappend(), lfirst_oid, LookupExplicitNamespace(), makeStringInfo(), map_sql_schema_to_xmlschema_types(), map_sql_typecoll_to_xmlschema_types(), NIL, NoLock, 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 3737 of file xml.c.

3740 {
3741  int i;
3742  char *xmltn;
3743 
3744  if (tablename)
3745  xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
3746  else
3747  {
3748  if (tableforest)
3749  xmltn = "row";
3750  else
3751  xmltn = "table";
3752  }
3753 
3754  if (tableforest)
3755  xmldata_root_element_start(result, xmltn, NULL, targetns, top_level);
3756  else
3757  appendStringInfoString(result, "<row>\n");
3758 
3759  for (i = 1; i <= SPI_tuptable->tupdesc->natts; i++)
3760  {
3761  char *colname;
3762  Datum colval;
3763  bool isnull;
3764 
3766  true, false);
3767  colval = SPI_getbinval(SPI_tuptable->vals[rownum],
3769  i,
3770  &isnull);
3771  if (isnull)
3772  {
3773  if (nulls)
3774  appendStringInfo(result, " <%s xsi:nil=\"true\"/>\n", colname);
3775  }
3776  else
3777  appendStringInfo(result, " <%s>%s</%s>\n",
3778  colname,
3780  SPI_gettypeid(SPI_tuptable->tupdesc, i), true),
3781  colname);
3782  }
3783 
3784  if (tableforest)
3785  {
3786  xmldata_root_element_end(result, xmltn);
3787  appendStringInfoChar(result, '\n');
3788  }
3789  else
3790  appendStringInfoString(result, "</row>\n\n");
3791 }
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1306
char * SPI_fname(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1196

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

2541 {
2542  Oid relid = PG_GETARG_OID(0);
2543  bool nulls = PG_GETARG_BOOL(1);
2544  bool tableforest = PG_GETARG_BOOL(2);
2545  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2546 
2548  nulls, tableforest,
2549  targetns, true)));
2550 }
#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 2775 of file xml.c.

2776 {
2777  Oid relid = PG_GETARG_OID(0);
2778  bool nulls = PG_GETARG_BOOL(1);
2779  bool tableforest = PG_GETARG_BOOL(2);
2780  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2781  Relation rel;
2782  const char *xmlschema;
2783 
2784  rel = table_open(relid, AccessShareLock);
2785  xmlschema = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2786  tableforest, targetns);
2787  table_close(rel, NoLock);
2788 
2790  xmlschema, nulls, tableforest,
2791  targetns, true)));
2792 }

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

2526 {
2527  StringInfoData query;
2528 
2529  initStringInfo(&query);
2530  appendStringInfo(&query, "SELECT * FROM %s",
2532  ObjectIdGetDatum(relid))));
2533  return query_to_xml_internal(query.data, get_rel_name(relid),
2534  xmlschema, nulls, tableforest,
2535  targetns, top_level);
2536 }
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:631
#define DatumGetCString(X)
Definition: postgres.h:610
Datum regclassout(PG_FUNCTION_ARGS)
Definition: regproc.c:1010

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

2702 {
2703  Oid relid = PG_GETARG_OID(0);
2704  bool nulls = PG_GETARG_BOOL(1);
2705  bool tableforest = PG_GETARG_BOOL(2);
2706  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2707  const char *result;
2708  Relation rel;
2709 
2710  rel = table_open(relid, AccessShareLock);
2711  result = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2712  tableforest, targetns);
2713  table_close(rel, NoLock);
2714 
2716 }

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

596 {
598 
600 }
const void * data
xmltype * xmlparse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace)
Definition: xml.c:751
int xmloption
Definition: xml.c:98

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

◆ xml_in()

Datum xml_in ( PG_FUNCTION_ARGS  )

Definition at line 257 of file xml.c.

258 {
259 #ifdef USE_LIBXML
260  char *s = PG_GETARG_CSTRING(0);
261  xmltype *vardata;
262  xmlDocPtr doc;
263 
264  vardata = (xmltype *) cstring_to_text(s);
265 
266  /*
267  * Parse the data to check if it is well-formed XML data. Assume that
268  * ERROR occurred if parsing failed.
269  */
270  doc = xml_parse(vardata, xmloption, true, GetDatabaseEncoding());
271  xmlFreeDoc(doc);
272 
273  PG_RETURN_XML_P(vardata);
274 #else
275  NO_XML_SUPPORT();
276  return 0;
277 #endif
278 }
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
int GetDatabaseEncoding(void)
Definition: mbutils.c:1210

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

888 {
889 #ifdef USE_LIBXML
890  bool result;
891  volatile xmlDocPtr doc = NULL;
893 
894  /* We want to catch ereport(INVALID_XML_DOCUMENT) and return false */
895  PG_TRY();
896  {
897  doc = xml_parse((text *) arg, XMLOPTION_DOCUMENT, true,
899  result = true;
900  }
901  PG_CATCH();
902  {
903  ErrorData *errdata;
904  MemoryContext ecxt;
905 
906  ecxt = MemoryContextSwitchTo(ccxt);
907  errdata = CopyErrorData();
908  if (errdata->sqlerrcode == ERRCODE_INVALID_XML_DOCUMENT)
909  {
910  FlushErrorState();
911  result = false;
912  }
913  else
914  {
915  MemoryContextSwitchTo(ecxt);
916  PG_RE_THROW();
917  }
918  }
919  PG_END_TRY();
920 
921  if (doc)
922  xmlFreeDoc(doc);
923 
924  return result;
925 #else /* not USE_LIBXML */
926  NO_XML_SUPPORT();
927  return false;
928 #endif /* not USE_LIBXML */
929 }
void FlushErrorState(void)
Definition: elog.c:1649
ErrorData * CopyErrorData(void)
Definition: elog.c:1555
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * arg
@ XMLOPTION_DOCUMENT
Definition: primnodes.h:1227
int sqlerrcode
Definition: elog.h:367

References arg, CopyErrorData(), CurrentMemoryContext, FlushErrorState(), GetDatabaseEncoding(), MemoryContextSwitchTo(), NO_XML_SUPPORT, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, ErrorData::sqlerrcode, and XMLOPTION_DOCUMENT.

Referenced by ExecEvalXmlExpr(), and xmltotext_with_xmloption().

◆ xml_is_well_formed()

Datum xml_is_well_formed ( PG_FUNCTION_ARGS  )

Definition at line 4262 of file xml.c.

4263 {
4264 #ifdef USE_LIBXML
4265  text *data = PG_GETARG_TEXT_PP(0);
4266 
4267  PG_RETURN_BOOL(wellformed_xml(data, xmloption));
4268 #else
4269  NO_XML_SUPPORT();
4270  return 0;
4271 #endif /* not USE_LIBXML */
4272 }
#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 4288 of file xml.c.

4289 {
4290 #ifdef USE_LIBXML
4291  text *data = PG_GETARG_TEXT_PP(0);
4292 
4293  PG_RETURN_BOOL(wellformed_xml(data, XMLOPTION_CONTENT));
4294 #else
4295  NO_XML_SUPPORT();
4296  return 0;
4297 #endif /* not USE_LIBXML */
4298 }
@ XMLOPTION_CONTENT
Definition: primnodes.h:1228

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

4276 {
4277 #ifdef USE_LIBXML
4278  text *data = PG_GETARG_TEXT_PP(0);
4279 
4280  PG_RETURN_BOOL(wellformed_xml(data, XMLOPTION_DOCUMENT));
4281 #else
4282  NO_XML_SUPPORT();
4283  return 0;
4284 #endif /* not USE_LIBXML */
4285 }

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

336 {
337  xmltype *x = PG_GETARG_XML_P(0);
338 
339  /*
340  * xml_out removes the encoding property in all cases. This is because we
341  * cannot control from here whether the datum will be converted to a
342  * different client encoding, so we'd do more harm than good by including
343  * it.
344  */
346 }
#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:292
#define PG_GETARG_XML_P(n)
Definition: xml.h:53

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

293 {
294  char *str = text_to_cstring((text *) x);
295 
296 #ifdef USE_LIBXML
297  size_t len = strlen(str);
298  xmlChar *version;
299  int standalone;
300  int res_code;
301 
302  if ((res_code = parse_xml_decl((xmlChar *) str,
303  &len, &version, NULL, &standalone)) == 0)
304  {
306 
308 
309  if (!print_xml_decl(&buf, version, target_encoding, standalone))
310  {
311  /*
312  * If we are not going to produce an XML declaration, eat a single
313  * newline in the original string to prevent empty first lines in
314  * the output.
315  */
316  if (*(str + len) == '\n')
317  len += 1;
318  }
320 
321  pfree(str);
322 
323  return buf.data;
324  }
325 
326  xml_ereport_by_code(WARNING, ERRCODE_INTERNAL_ERROR,
327  "could not parse XML declaration in stored value",
328  res_code);
329 #endif
330  return str;
331 }
#define WARNING
Definition: elog.h:30

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

351 {
352 #ifdef USE_LIBXML
354  xmltype *result;
355  char *str;
356  char *newstr;
357  int nbytes;
358  xmlDocPtr doc;
359  xmlChar *encodingStr = NULL;
360  int encoding;
361 
362  /*
363  * Read the data in raw format. We don't know yet what the encoding is, as
364  * that information is embedded in the xml declaration; so we have to
365  * parse that before converting to server encoding.
366  */
367  nbytes = buf->len - buf->cursor;
368  str = (char *) pq_getmsgbytes(buf, nbytes);
369 
370  /*
371  * We need a null-terminated string to pass to parse_xml_decl(). Rather
372  * than make a separate copy, make the temporary result one byte bigger
373  * than it needs to be.
374  */
375  result = palloc(nbytes + 1 + VARHDRSZ);
376  SET_VARSIZE(result, nbytes + VARHDRSZ);
377  memcpy(VARDATA(result), str, nbytes);
378  str = VARDATA(result);
379  str[nbytes] = '\0';
380 
381  parse_xml_decl((const xmlChar *) str, NULL, NULL, &encodingStr, NULL);
382 
383  /*
384  * If encoding wasn't explicitly specified in the XML header, treat it as
385  * UTF-8, as that's the default in XML. This is different from xml_in(),
386  * where the input has to go through the normal client to server encoding
387  * conversion.
388  */
389  encoding = encodingStr ? xmlChar_to_encoding(encodingStr) : PG_UTF8;
390 
391  /*
392  * Parse the data to check if it is well-formed XML data. Assume that
393  * xml_parse will throw ERROR if not.
394  */
395  doc = xml_parse(result, xmloption, true, encoding);
396  xmlFreeDoc(doc);
397 
398  /* Now that we know what we're dealing with, convert to server encoding */
399  newstr = pg_any_to_server(str, nbytes, encoding);
400 
401  if (newstr != str)
402  {
403  pfree(result);
404  result = (xmltype *) cstring_to_text(newstr);
405  pfree(newstr);
406  }
407 
408  PG_RETURN_XML_P(result);
409 #else
410  NO_XML_SUPPORT();
411  return 0;
412 #endif
413 }
#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:1068
int32 encoding
Definition: pg_database.h:41
@ PG_UTF8
Definition: pg_wchar.h:230
#define VARDATA(PTR)
Definition: postgres.h:315
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
const char * pq_getmsgbytes(StringInfo msg, int datalen)
Definition: pqformat.c:510
StringInfoData * StringInfo
Definition: stringinfo.h:44

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, generate_unaccent_rules::str, VARDATA, VARHDRSZ, and xmloption.

◆ xml_send()

Datum xml_send ( PG_FUNCTION_ARGS  )

Definition at line 417 of file xml.c.

418 {
419  xmltype *x = PG_GETARG_XML_P(0);
420  char *outval;
422 
423  /*
424  * xml_out_internal doesn't convert the encoding, it just prints the right
425  * declaration. pq_sendtext will do the conversion.
426  */
428 
430  pq_sendtext(&buf, outval, strlen(outval));
431  pfree(outval);
433 }
#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:174
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348

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

471 {
472 #ifdef USE_LIBXML
473  text *arg = PG_GETARG_TEXT_PP(0);
474  char *argdata = VARDATA_ANY(arg);
475  int len = VARSIZE_ANY_EXHDR(arg);
477  int i;
478 
479  /* check for "--" in string or "-" at the end */
480  for (i = 1; i < len; i++)
481  {
482  if (argdata[i] == '-' && argdata[i - 1] == '-')
483  ereport(ERROR,
484  (errcode(ERRCODE_INVALID_XML_COMMENT),
485  errmsg("invalid XML comment")));
486  }
487  if (len > 0 && argdata[len - 1] == '-')
488  ereport(ERROR,
489  (errcode(ERRCODE_INVALID_XML_COMMENT),
490  errmsg("invalid XML comment")));
491 
493  appendStringInfoString(&buf, "<!--");
495  appendStringInfoString(&buf, "-->");
496 
498 #else
499  NO_XML_SUPPORT();
500  return 0;
501 #endif
502 }
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:4240

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

512 {
513 #ifdef USE_LIBXML
514  int global_standalone = 1;
515  xmlChar *global_version = NULL;
516  bool global_version_no_value = false;
518  ListCell *v;
519 
521  foreach(v, args)
522  {
524  size_t len;
525  xmlChar *version;
526  int standalone;
527  char *str;
528 
529  len = VARSIZE(x) - VARHDRSZ;
530  str = text_to_cstring((text *) x);
531 
532  parse_xml_decl((xmlChar *) str, &len, &version, NULL, &standalone);
533 
534  if (standalone == 0 && global_standalone == 1)
535  global_standalone = 0;
536  if (standalone < 0)
537  global_standalone = -1;
538 
539  if (!version)
540  global_version_no_value = true;
541  else if (!global_version)
542  global_version = version;
543  else if (xmlStrcmp(version, global_version) != 0)
544  global_version_no_value = true;
545 
547  pfree(str);
548  }
549 
550  if (!global_version_no_value || global_standalone >= 0)
551  {
552  StringInfoData buf2;
553 
554  initStringInfo(&buf2);
555 
556  print_xml_decl(&buf2,
557  (!global_version_no_value) ? global_version : NULL,
558  0,
559  global_standalone);
560 
561  appendBinaryStringInfo(&buf2, buf.data, buf.len);
562  buf = buf2;
563  }
564 
565  return stringinfo_to_xmltype(&buf);
566 #else
567  NO_XML_SUPPORT();
568  return NULL;
569 #endif
570 }
#define VARSIZE(PTR)
Definition: postgres.h:316
#define PointerGetDatum(X)
Definition: postgres.h:600
#define DatumGetXmlP(X)
Definition: xml.h:50

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

Referenced by ExecEvalXmlExpr(), and xmlconcat2().

◆ xmlconcat2()

Datum xmlconcat2 ( PG_FUNCTION_ARGS  )

Definition at line 577 of file xml.c.

578 {
579  if (PG_ARGISNULL(0))
580  {
581  if (PG_ARGISNULL(1))
582  PG_RETURN_NULL();
583  else
585  }
586  else if (PG_ARGISNULL(1))
588  else
590  PG_GETARG_XML_P(1))));
591 }
#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:208
xmltype * xmlconcat(List *args)
Definition: xml.c:511

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

2650 {
2651  appendStringInfo(result, "</%s>\n", eltname);
2652 }

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

2625 {
2626  /* This isn't really wrong but currently makes no sense. */
2627  Assert(top_level || !xmlschema);
2628 
2629  appendStringInfo(result, "<%s", eltname);
2630  if (top_level)
2631  {
2632  appendStringInfoString(result, " xmlns:xsi=\"" NAMESPACE_XSI "\"");
2633  if (strlen(targetns) > 0)
2634  appendStringInfo(result, " xmlns=\"%s\"", targetns);
2635  }
2636  if (xmlschema)
2637  {
2638  /* FIXME: better targets */
2639  if (strlen(targetns) > 0)
2640  appendStringInfo(result, " xsi:schemaLocation=\"%s #\"", targetns);
2641  else
2642  appendStringInfoString(result, " xsi:noNamespaceSchemaLocation=\"#\"");
2643  }
2644  appendStringInfoString(result, ">\n");
2645 }
#define NAMESPACE_XSI
Definition: xml.c:228

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

630 {
631 #ifdef USE_LIBXML
632  xmltype *result;
633  List *named_arg_strings;
634  List *arg_strings;
635  int i;
636  ListCell *arg;
637  ListCell *narg;
638  PgXmlErrorContext *xmlerrcxt;
639  volatile xmlBufferPtr buf = NULL;
640  volatile xmlTextWriterPtr writer = NULL;
641 
642  /*
643  * All arguments are already evaluated, and their values are passed in the
644  * named_argvalue/named_argnull or argvalue/argnull arrays. This avoids
645  * issues if one of the arguments involves a call to some other function
646  * or subsystem that wants to use libxml on its own terms. We examine the
647  * original XmlExpr to identify the numbers and types of the arguments.
648  */
649  named_arg_strings = NIL;
650  i = 0;
651  foreach(arg, xexpr->named_args)
652  {
653  Expr *e = (Expr *) lfirst(arg);
654  char *str;
655 
656  if (named_argnull[i])
657  str = NULL;
658  else
659  str = map_sql_value_to_xml_value(named_argvalue[i],
660  exprType((Node *) e),
661  false);
662  named_arg_strings = lappend(named_arg_strings, str);
663  i++;
664  }
665 
666  arg_strings = NIL;
667  i = 0;
668  foreach(arg, xexpr->args)
669  {
670  Expr *e = (Expr *) lfirst(arg);
671  char *str;
672 
673  /* here we can just forget NULL elements immediately */
674  if (!argnull[i])
675  {
676  str = map_sql_value_to_xml_value(argvalue[i],
677  exprType((Node *) e),
678  true);
679  arg_strings = lappend(arg_strings, str);
680  }
681  i++;
682  }
683 
684  xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
685 
686  PG_TRY();
687  {
688  buf = xmlBufferCreate();
689  if (buf == NULL || xmlerrcxt->err_occurred)
690  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
691  "could not allocate xmlBuffer");
692  writer = xmlNewTextWriterMemory(buf, 0);
693  if (writer == NULL || xmlerrcxt->err_occurred)
694  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
695  "could not allocate xmlTextWriter");
696 
697  xmlTextWriterStartElement(writer, (xmlChar *) xexpr->name);
698 
699  forboth(arg, named_arg_strings, narg, xexpr->arg_names)
700  {
701  char *str = (char *) lfirst(arg);
702  char *argname = strVal(lfirst(narg));
703 
704  if (str)
705  xmlTextWriterWriteAttribute(writer,
706  (xmlChar *) argname,
707  (xmlChar *) str);
708  }
709 
710  foreach(arg, arg_strings)
711  {
712  char *str = (char *) lfirst(arg);
713 
714  xmlTextWriterWriteRaw(writer, (xmlChar *) str);
715  }
716 
717  xmlTextWriterEndElement(writer);
718 
719  /* we MUST do this now to flush data out to the buffer ... */
720  xmlFreeTextWriter(writer);
721  writer = NULL;
722 
723  result = xmlBuffer_to_xmltype(buf);
724  }
725  PG_CATCH();
726  {
727  if (writer)
728  xmlFreeTextWriter(writer);
729  if (buf)
730  xmlBufferFree(buf);
731 
732  pg_xml_done(xmlerrcxt, true);
733 
734  PG_RE_THROW();
735  }
736  PG_END_TRY();
737 
738  xmlBufferFree(buf);
739 
740  pg_xml_done(xmlerrcxt, false);
741 
742  return result;
743 #else
744  NO_XML_SUPPORT();
745  return NULL;
746 #endif
747 }
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
e
Definition: preproc-init.c:82
Definition: nodes.h:574
List * args
Definition: primnodes.h:1238
char * name
Definition: primnodes.h:1235
List * arg_names
Definition: primnodes.h:1237
List * named_args
Definition: primnodes.h:1236
#define strVal(v)
Definition: value.h:72

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

Referenced by ExecEvalXmlExpr().

◆ xmlexists()

Datum xmlexists ( PG_FUNCTION_ARGS  )

Definition at line 4189 of file xml.c.

4190 {
4191 #ifdef USE_LIBXML
4192  text *xpath_expr_text = PG_GETARG_TEXT_PP(0);
4194  int res_nitems;
4195 
4196  xpath_internal(xpath_expr_text, data, NULL,
4197  &res_nitems, NULL);
4198 
4199  PG_RETURN_BOOL(res_nitems > 0);
4200 #else
4201  NO_XML_SUPPORT();
4202  return 0;
4203 #endif
4204 }

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

752 {
753 #ifdef USE_LIBXML
754  xmlDocPtr doc;
755 
756  doc = xml_parse(data, xmloption_arg, preserve_whitespace,
758  xmlFreeDoc(doc);
759 
760  return (xmltype *) data;
761 #else
762  NO_XML_SUPPORT();
763  return NULL;
764 #endif
765 }

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

770 {
771 #ifdef USE_LIBXML
772  xmltype *result;
774 
775  if (pg_strcasecmp(target, "xml") == 0)
776  ereport(ERROR,
777  (errcode(ERRCODE_SYNTAX_ERROR), /* really */
778  errmsg("invalid XML processing instruction"),
779  errdetail("XML processing instruction target name cannot be \"%s\".", target)));
780 
781  /*
782  * Following the SQL standard, the null check comes after the syntax check
783  * above.
784  */
785  *result_is_null = arg_is_null;
786  if (*result_is_null)
787  return NULL;
788 
790 
791  appendStringInfo(&buf, "<?%s", target);
792 
793  if (arg != NULL)
794  {
795  char *string;
796 
797  string = text_to_cstring(arg);
798  if (strstr(string, "?>") != NULL)
799  ereport(ERROR,
800  (errcode(ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION),
801  errmsg("invalid XML processing instruction"),
802  errdetail("XML processing instruction cannot contain \"?>\".")));
803 
804  appendStringInfoChar(&buf, ' ');
805  appendStringInfoString(&buf, string + strspn(string, " "));
806  pfree(string);
807  }
808  appendStringInfoString(&buf, "?>");
809 
810  result = stringinfo_to_xmltype(&buf);
811  pfree(buf.data);
812  return result;
813 #else
814  NO_XML_SUPPORT();
815  return NULL;
816 #endif
817 }
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
char string[11]
Definition: preproc-type.c:46

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

822 {
823 #ifdef USE_LIBXML
824  char *str;
825  size_t len;
826  xmlChar *orig_version;
827  int orig_standalone;
829 
830  len = VARSIZE(data) - VARHDRSZ;
831  str = text_to_cstring((text *) data);
832 
833  parse_xml_decl((xmlChar *) str, &len, &orig_version, NULL, &orig_standalone);
834 
835  if (version)
836  orig_version = xml_text2xmlChar(version);
837  else
838  orig_version = NULL;
839 
840  switch (standalone)
841  {
842  case XML_STANDALONE_YES:
843  orig_standalone = 1;
844  break;
845  case XML_STANDALONE_NO:
846  orig_standalone = 0;
847  break;
849  orig_standalone = -1;
850  break;
852  /* leave original value */
853  break;
854  }
855 
857  print_xml_decl(&buf, orig_version, 0, orig_standalone);
859 
860  return stringinfo_to_xmltype(&buf);
861 #else
862  NO_XML_SUPPORT();
863  return NULL;
864 #endif
865 }
@ 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, generate_unaccent_rules::str, stringinfo_to_xmltype(), text_to_cstring(), VARHDRSZ, VARSIZE, XML_STANDALONE_NO, XML_STANDALONE_NO_VALUE, XML_STANDALONE_OMITTED, and XML_STANDALONE_YES.

Referenced by ExecEvalXmlExpr().

◆ XmlTableDestroyOpaque()

static void XmlTableDestroyOpaque ( struct TableFuncScanState state)
static

Definition at line 4726 of file xml.c.

4727 {
4728 #ifdef USE_LIBXML
4729  XmlTableBuilderData *xtCxt;
4730 
4731  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableDestroyOpaque");
4732 
4733  /* Propagate our own error context to libxml2 */
4734  xmlSetStructuredErrorFunc((void *) xtCxt->xmlerrcxt, xml_errorHandler);
4735 
4736  if (xtCxt->xpathscomp != NULL)
4737  {
4738  int i;
4739 
4740  for (i = 0; i < xtCxt->natts; i++)
4741  if (xtCxt->xpathscomp[i] != NULL)
4742  xmlXPathFreeCompExpr(xtCxt->xpathscomp[i]);
4743  }
4744 
4745  if (xtCxt->xpathobj != NULL)
4746  xmlXPathFreeObject(xtCxt->xpathobj);
4747  if (xtCxt->xpathcomp != NULL)
4748  xmlXPathFreeCompExpr(xtCxt->xpathcomp);
4749  if (xtCxt->xpathcxt != NULL)
4750  xmlXPathFreeContext(xtCxt->xpathcxt);
4751  if (xtCxt->doc != NULL)
4752  xmlFreeDoc(xtCxt->doc);
4753  if (xtCxt->ctxt != NULL)
4754  xmlFreeParserCtxt(xtCxt->ctxt);
4755 
4756  pg_xml_done(xtCxt->xmlerrcxt, true);
4757 
4758  /* not valid anymore */
4759  xtCxt->magic = 0;
4760  state->opaque = NULL;
4761 
4762 #else
4763  NO_XML_SUPPORT();
4764 #endif /* not USE_LIBXML */
4765 }
Definition: regguts.h:318

References i, NO_XML_SUPPORT, and pg_xml_done().

◆ XmlTableFetchRow()

static bool XmlTableFetchRow ( struct TableFuncScanState state)
static

Definition at line 4529 of file xml.c.

4530 {
4531 #ifdef USE_LIBXML
4532  XmlTableBuilderData *xtCxt;
4533 
4534  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableFetchRow");
4535 
4536  /* Propagate our own error context to libxml2 */
4537  xmlSetStructuredErrorFunc((void *) xtCxt->xmlerrcxt, xml_errorHandler);
4538 
4539  if (xtCxt->xpathobj == NULL)
4540  {
4541  xtCxt->xpathobj = xmlXPathCompiledEval(xtCxt->xpathcomp, xtCxt->xpathcxt);
4542  if (xtCxt->xpathobj == NULL || xtCxt->xmlerrcxt->err_occurred)
4543  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
4544  "could not create XPath object");
4545 
4546  xtCxt->row_count = 0;
4547  }
4548 
4549  if (xtCxt->xpathobj->type == XPATH_NODESET)
4550  {
4551  if (xtCxt->xpathobj->nodesetval != NULL)
4552  {
4553  if (xtCxt->row_count++ < xtCxt->xpathobj->nodesetval->nodeNr)
4554  return true;
4555  }
4556  }
4557 
4558  return false;
4559 #else
4560  NO_XML_SUPPORT();
4561  return false;
4562 #endif /* not USE_LIBXML */
4563 }

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

4576 {
4577 #ifdef USE_LIBXML
4578  XmlTableBuilderData *xtCxt;
4579  Datum result = (Datum) 0;
4580  xmlNodePtr cur;
4581  char *cstr = NULL;
4582  volatile xmlXPathObjectPtr xpathobj = NULL;
4583 
4584  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableGetValue");
4585 
4586  Assert(xtCxt->xpathobj &&
4587  xtCxt->xpathobj->type == XPATH_NODESET &&
4588  xtCxt->xpathobj->nodesetval != NULL);
4589 
4590  /* Propagate our own error context to libxml2 */
4591  xmlSetStructuredErrorFunc((void *) xtCxt->xmlerrcxt, xml_errorHandler);
4592 
4593  *isnull = false;
4594 
4595  cur = xtCxt->xpathobj->nodesetval->nodeTab[xtCxt->row_count - 1];
4596 
4597  Assert(xtCxt->xpathscomp[colnum] != NULL);
4598 
4599  PG_TRY();
4600  {
4601  /* Set current node as entry point for XPath evaluation */
4602  xtCxt->xpathcxt->node = cur;
4603 
4604  /* Evaluate column path */
4605  xpathobj = xmlXPathCompiledEval(xtCxt->xpathscomp[colnum], xtCxt->xpathcxt);
4606  if (xpathobj == NULL || xtCxt->xmlerrcxt->err_occurred)
4607  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
4608  "could not create XPath object");
4609 
4610  /*
4611  * There are four possible cases, depending on the number of nodes
4612  * returned by the XPath expression and the type of the target column:
4613  * a) XPath returns no nodes. b) The target type is XML (return all
4614  * as XML). For non-XML return types: c) One node (return content).
4615  * d) Multiple nodes (error).
4616  */
4617  if (xpathobj->type == XPATH_NODESET)
4618  {
4619  int count = 0;
4620 
4621  if (xpathobj->nodesetval != NULL)
4622  count = xpathobj->nodesetval->nodeNr;
4623 
4624  if (xpathobj->nodesetval == NULL || count == 0)
4625  {
4626  *isnull = true;
4627  }
4628  else
4629  {
4630  if (typid == XMLOID)
4631  {
4632  text *textstr;
4634 
4635  /* Concatenate serialized values */
4636  initStringInfo(&str);
4637  for (int i = 0; i < count; i++)
4638  {
4639  textstr =
4640  xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[i],
4641  xtCxt->xmlerrcxt);
4642 
4643  appendStringInfoText(&str, textstr);
4644  }
4645  cstr = str.data;
4646  }
4647  else
4648  {
4649  xmlChar *str;
4650 
4651  if (count > 1)
4652  ereport(ERROR,
4653  (errcode(ERRCODE_CARDINALITY_VIOLATION),
4654  errmsg("more than one value returned by column XPath expression")));
4655 
4656  str = xmlXPathCastNodeSetToString(xpathobj->nodesetval);
4657  cstr = str ? xml_pstrdup_and_free(str) : "";
4658  }
4659  }
4660  }
4661  else if (xpathobj->type == XPATH_STRING)
4662  {
4663  /* Content should be escaped when target will be XML */
4664  if (typid == XMLOID)
4665  cstr = escape_xml((char *) xpathobj->stringval);
4666  else
4667  cstr = (char *) xpathobj->stringval;
4668  }
4669  else if (xpathobj->type == XPATH_BOOLEAN)
4670  {
4671  char typcategory;
4672  bool typispreferred;
4673  xmlChar *str;
4674 
4675  /* Allow implicit casting from boolean to numbers */
4676  get_type_category_preferred(typid, &typcategory, &typispreferred);
4677 
4678  if (typcategory != TYPCATEGORY_NUMERIC)
4679  str = xmlXPathCastBooleanToString(xpathobj->boolval);
4680  else
4681  str = xmlXPathCastNumberToString(xmlXPathCastBooleanToNumber(xpathobj->boolval));
4682 
4683  cstr = xml_pstrdup_and_free(str);
4684  }
4685  else if (xpathobj->type == XPATH_NUMBER)
4686  {
4687  xmlChar *str;
4688 
4689  str = xmlXPathCastNumberToString(xpathobj->floatval);
4690  cstr = xml_pstrdup_and_free(str);
4691  }
4692  else
4693  elog(ERROR, "unexpected XPath object type %u", xpathobj->type);
4694 
4695  /*
4696  * By here, either cstr contains the result value, or the isnull flag
4697  * has been set.
4698  */
4699  Assert(cstr || *isnull);
4700 
4701  if (!*isnull)
4702  result = InputFunctionCall(&state->in_functions[colnum],
4703  cstr,
4704  state->typioparams[colnum],
4705  typmod);
4706  }
4707  PG_FINALLY();
4708  {
4709  if (xpathobj != NULL)
4710  xmlXPathFreeObject(xpathobj);
4711  }
4712  PG_END_TRY();
4713 
4714  return result;
4715 #else
4716  NO_XML_SUPPORT();
4717  return 0;
4718 #endif /* not USE_LIBXML */
4719 }
struct cursor * cur
Definition: ecpg.c:28
#define PG_FINALLY()
Definition: elog.h:316
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1515
void get_type_category_preferred(Oid typid, char *typcategory, bool *typispreferred)
Definition: lsyscache.c:2667

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, generate_unaccent_rules::str, and xml_ereport().

◆ XmlTableInitOpaque()

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

Definition at line 4337 of file xml.c.

4338 {
4339 #ifdef USE_LIBXML
4340  volatile xmlParserCtxtPtr ctxt = NULL;
4341  XmlTableBuilderData *xtCxt;
4342  PgXmlErrorContext *xmlerrcxt;
4343 
4344  xtCxt = palloc0(sizeof(XmlTableBuilderData));
4345  xtCxt->magic = XMLTABLE_CONTEXT_MAGIC;
4346  xtCxt->natts = natts;
4347  xtCxt->xpathscomp = palloc0(sizeof(xmlXPathCompExprPtr) * natts);
4348 
4349  xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
4350 
4351  PG_TRY();
4352  {
4353  xmlInitParser();
4354 
4355  ctxt = xmlNewParserCtxt();
4356  if (ctxt == NULL || xmlerrcxt->err_occurred)
4357  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
4358  "could not allocate parser context");
4359  }
4360  PG_CATCH();
4361  {
4362  if (ctxt != NULL)
4363  xmlFreeParserCtxt(ctxt);
4364 
4365  pg_xml_done(xmlerrcxt, true);
4366 
4367  PG_RE_THROW();
4368  }
4369  PG_END_TRY();
4370 
4371  xtCxt->xmlerrcxt = xmlerrcxt;
4372  xtCxt->ctxt = ctxt;
4373 
4374  state->opaque = xtCxt;
4375 #else
4376  NO_XML_SUPPORT();
4377 #endif /* not USE_LIBXML */
4378 }
void * palloc0(Size size)
Definition: mcxt.c:1099

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

4498 {
4499 #ifdef USE_LIBXML
4500  XmlTableBuilderData *xtCxt;
4501  xmlChar *xstr;
4502 
4503  AssertArg(PointerIsValid(path));
4504 
4505  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetColumnFilter");
4506 
4507  if (*path == '\0')
4508  ereport(ERROR,
4509  (errcode(ERRCODE_DATA_EXCEPTION),
4510  errmsg("column path filter must not be empty string")));
4511 
4512  xstr = pg_xmlCharStrndup(path, strlen(path));
4513 
4514  xtCxt->xpathscomp[colnum] = xmlXPathCompile(xstr);
4515  if (xtCxt->xpathscomp[colnum] == NULL || xtCxt->xmlerrcxt->err_occurred)
4516  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_DATA_EXCEPTION,
4517  "invalid XPath expression");
4518 #else
4519  NO_XML_SUPPORT();
4520 #endif /* not USE_LIBXML */
4521 }
#define PointerIsValid(pointer)
Definition: c.h:698
#define AssertArg(condition)
Definition: c.h:806

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

◆ XmlTableSetDocument()

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

Definition at line 4385 of file xml.c.

4386 {
4387 #ifdef USE_LIBXML
4388  XmlTableBuilderData *xtCxt;
4389  xmltype *xmlval = DatumGetXmlP(value);
4390  char *str;
4391  xmlChar *xstr;
4392  int length;
4393  volatile xmlDocPtr doc = NULL;
4394  volatile xmlXPathContextPtr xpathcxt = NULL;
4395 
4396  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetDocument");
4397 
4398  /*
4399  * Use out function for casting to string (remove encoding property). See
4400  * comment in xml_out.
4401  */
4402  str = xml_out_internal(xmlval, 0);
4403 
4404  length = strlen(str);
4405  xstr = pg_xmlCharStrndup(str, length);
4406 
4407  PG_TRY();
4408  {
4409  doc = xmlCtxtReadMemory(xtCxt->ctxt, (char *) xstr, length, NULL, NULL, 0);
4410  if (doc == NULL || xtCxt->xmlerrcxt->err_occurred)
4411  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INVALID_XML_DOCUMENT,
4412  "could not parse XML document");
4413  xpathcxt = xmlXPathNewContext(doc);
4414  if (xpathcxt == NULL || xtCxt->xmlerrcxt->err_occurred)
4415  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
4416  "could not allocate XPath context");
4417  xpathcxt->node = (xmlNodePtr) doc;
4418  }
4419  PG_CATCH();
4420  {
4421  if (xpathcxt != NULL)
4422  xmlXPathFreeContext(xpathcxt);
4423  if (doc != NULL)
4424  xmlFreeDoc(doc);
4425 
4426  PG_RE_THROW();
4427  }
4428  PG_END_TRY();
4429 
4430  xtCxt->doc = doc;
4431  xtCxt->xpathcxt = xpathcxt;
4432 #else
4433  NO_XML_SUPPORT();
4434 #endif /* not USE_LIBXML */
4435 }

References DatumGetXmlP, ERROR, NO_XML_SUPPORT, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, generate_unaccent_rules::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 4442 of file xml.c.

4443 {
4444 #ifdef USE_LIBXML
4445  XmlTableBuilderData *xtCxt;
4446 
4447  if (name == NULL)
4448  ereport(ERROR,
4449  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4450  errmsg("DEFAULT namespace is not supported")));
4451  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetNamespace");
4452 
4453  if (xmlXPathRegisterNs(xtCxt->xpathcxt,
4454  pg_xmlCharStrndup(name, strlen(name)),
4455  pg_xmlCharStrndup(uri, strlen(uri))))
4456  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_DATA_EXCEPTION,
4457  "could not set XML namespace");
4458 #else
4459  NO_XML_SUPPORT();
4460 #endif /* not USE_LIBXML */
4461 }

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

4469 {
4470 #ifdef USE_LIBXML
4471  XmlTableBuilderData *xtCxt;
4472  xmlChar *xstr;
4473 
4474  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetRowFilter");
4475 
4476  if (*path == '\0')
4477  ereport(ERROR,
4478  (errcode(ERRCODE_DATA_EXCEPTION),
4479  errmsg("row path filter must not be empty string")));
4480 
4481  xstr = pg_xmlCharStrndup(path, strlen(path));
4482 
4483  xtCxt->xpathcomp = xmlXPathCompile(xstr);
4484  if (xtCxt->xpathcomp == NULL || xtCxt->xmlerrcxt->err_occurred)
4485  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_SYNTAX_ERROR,
4486  "invalid XPath expression");
4487 #else
4488  NO_XML_SUPPORT();
4489 #endif /* not USE_LIBXML */
4490 }

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

◆ xmltotext()

Datum xmltotext ( PG_FUNCTION_ARGS  )

Definition at line 604 of file xml.c.

605 {
607 
608  /* It's actually binary compatible. */
610 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372

References data, PG_GETARG_XML_P, and PG_RETURN_TEXT_P.

◆ xmltotext_with_xmloption()

text* xmltotext_with_xmloption ( xmltype data,
XmlOptionType  xmloption_arg 
)

Definition at line 614 of file xml.c.

615 {
616  if (xmloption_arg == XMLOPTION_DOCUMENT && !xml_is_document(data))
617  ereport(ERROR,
618  (errcode(ERRCODE_NOT_AN_XML_DOCUMENT),
619  errmsg("not an XML document")));
620 
621  /* It's actually binary compatible, save for the above check. */
622  return (text *) data;
623 }
bool xml_is_document(xmltype *arg)
Definition: xml.c:887

References data, ereport, errcode(), errmsg(), ERROR, xml_is_document(), and XMLOPTION_DOCUMENT.

Referenced by ExecEvalXmlExpr().

◆ xmlvalidate()

Datum xmlvalidate ( PG_FUNCTION_ARGS  )

Definition at line 877 of file xml.c.

878 {
879  ereport(ERROR,
880  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
881  errmsg("xmlvalidate is not implemented")));
882  return 0;
883 }

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

◆ xpath()

Datum xpath ( PG_FUNCTION_ARGS  )

Definition at line 4166 of file xml.c.

4167 {
4168 #ifdef USE_LIBXML
4169  text *xpath_expr_text = PG_GETARG_TEXT_PP(0);
4171  ArrayType *namespaces = PG_GETARG_ARRAYTYPE_P(2);
4172  ArrayBuildState *astate;
4173 
4174  astate = initArrayResult(XMLOID, CurrentMemoryContext, true);
4175  xpath_internal(xpath_expr_text, data, namespaces,
4176  NULL, astate);
4178 #else
4179  NO_XML_SUPPORT();
4180  return 0;
4181 #endif
4182 }
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:258
ArrayBuildState * initArrayResult(Oid element_type, MemoryContext rcontext, bool subcontext)
Definition: arrayfuncs.c:5084
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5187

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

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

4213 {
4214 #ifdef USE_LIBXML
4215  text *xpath_expr_text = PG_GETARG_TEXT_PP(0);
4217  ArrayType *namespaces = PG_GETARG_ARRAYTYPE_P(2);
4218  int res_nitems;
4219 
4220  xpath_internal(xpath_expr_text, data, namespaces,
4221  &res_nitems, NULL);
4222 
4223  PG_RETURN_BOOL(res_nitems > 0);
4224 #else
4225  NO_XML_SUPPORT();
4226  return 0;
4227 #endif
4228 }

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

2915 {
2916  appendStringInfoString(result, "</xsd:schema>");
2917 }

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

2898 {
2899  appendStringInfoString(result,
2900  "<xsd:schema\n"
2901  " xmlns:xsd=\"" NAMESPACE_XSD "\"");
2902  if (strlen(targetns) > 0)
2903  appendStringInfo(result,
2904  "\n"
2905  " targetNamespace=\"%s\"\n"
2906  " elementFormDefault=\"qualified\"",
2907  targetns);
2908  appendStringInfoString(result,
2909  ">\n\n");
2910 }
#define NAMESPACE_XSD
Definition: xml.c:227

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

Definition at line 97 of file xml.c.

Referenced by map_sql_type_to_xmlschema_type(), and map_sql_value_to_xml_value().

◆ xmloption

◆ XmlTableRoutine

const TableFuncRoutine XmlTableRoutine
Initial value:
=
{
}
static void XmlTableInitOpaque(struct TableFuncScanState *state, int natts)
Definition: xml.c:4337
static void XmlTableSetNamespace(struct TableFuncScanState *state, const char *name, const char *uri)
Definition: xml.c:4442
static void XmlTableSetRowFilter(struct TableFuncScanState *state, const char *path)
Definition: xml.c:4468
static Datum XmlTableGetValue(struct TableFuncScanState *state, int colnum, Oid typid, int32 typmod, bool *isnull)
Definition: xml.c:4574
static void XmlTableSetDocument(struct TableFuncScanState *state, Datum value)
Definition: xml.c:4385
static void XmlTableDestroyOpaque(struct TableFuncScanState *state)
Definition: xml.c:4726
static bool XmlTableFetchRow(struct TableFuncScanState *state)
Definition: xml.c:4529
static void XmlTableSetColumnFilter(struct TableFuncScanState *state, const char *path, int colnum)
Definition: xml.c:4497

Definition at line 207 of file xml.c.

Referenced by ExecInitTableFuncScan().