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

◆ NAMESPACE_XSD

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

Definition at line 228 of file xml.c.

Referenced by xsd_schema_element_start().

◆ NAMESPACE_XSI

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

Definition at line 229 of file xml.c.

Referenced by xmldata_root_element_start().

◆ 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."), \
errhint("You need to rebuild PostgreSQL using --with-libxml.")))
int errhint(const char *fmt,...)
Definition: elog.c:1071
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:957
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824

Definition at line 219 of file xml.c.

Referenced by map_sql_identifier_to_xml_name(), xml_in(), xml_is_document(), xml_is_well_formed(), xml_is_well_formed_content(), xml_is_well_formed_document(), xml_recv(), xmlcomment(), xmlconcat(), xmlelement(), xmlexists(), xmlparse(), xmlpi(), xmlroot(), XmlTableDestroyOpaque(), XmlTableFetchRow(), XmlTableGetValue(), XmlTableInitOpaque(), XmlTableSetColumnFilter(), XmlTableSetDocument(), XmlTableSetNamespace(), XmlTableSetRowFilter(), xpath(), and xpath_exists().

◆ PG_XML_DEFAULT_VERSION

#define PG_XML_DEFAULT_VERSION   "1.0"

Definition at line 282 of file xml.c.

Referenced by xml_is_document().

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

Referenced by database_get_xml_visible_schemas(), and database_get_xml_visible_tables().

◆ XML_VISIBLE_SCHEMAS_EXCLUDE

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

Definition at line 2492 of file xml.c.

Function Documentation

◆ _SPI_strdup()

static char* _SPI_strdup ( const char *  s)
static

Definition at line 2385 of file xml.c.

References SPI_palloc().

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

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

◆ cstring_to_xmltype()

static xmltype* cstring_to_xmltype ( const char *  string)
static

Definition at line 454 of file xml.c.

References cstring_to_text(), and cstring_to_text_with_len().

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

455 {
456  return (xmltype *) cstring_to_text(string);
457 }
text * cstring_to_text(const char *s)
Definition: varlena.c:171
Definition: c.h:555

◆ cursor_to_xml()

Datum cursor_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 2569 of file xml.c.

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

2570 {
2573  bool nulls = PG_GETARG_BOOL(2);
2574  bool tableforest = PG_GETARG_BOOL(3);
2575  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(4));
2576 
2577  StringInfoData result;
2578  Portal portal;
2579  uint64 i;
2580 
2581  initStringInfo(&result);
2582 
2583  if (!tableforest)
2584  {
2585  xmldata_root_element_start(&result, "table", NULL, targetns, true);
2586  appendStringInfoChar(&result, '\n');
2587  }
2588 
2589  SPI_connect();
2590  portal = SPI_cursor_find(name);
2591  if (portal == NULL)
2592  ereport(ERROR,
2593  (errcode(ERRCODE_UNDEFINED_CURSOR),
2594  errmsg("cursor \"%s\" does not exist", name)));
2595 
2596  SPI_cursor_fetch(portal, true, count);
2597  for (i = 0; i < SPI_processed; i++)
2598  SPI_sql_row_to_xmlelement(i, &result, NULL, nulls,
2599  tableforest, targetns, true);
2600 
2601  SPI_finish();
2602 
2603  if (!tableforest)
2604  xmldata_root_element_end(&result, "table");
2605 
2607 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
static void xmldata_root_element_start(StringInfo result, const char *eltname, const char *xmlschema, const char *targetns, bool top_level)
Definition: xml.c:2623
int SPI_connect(void)
Definition: spi.c:89
int SPI_finish(void)
Definition: spi.c:176
int errcode(int sqlerrcode)
Definition: elog.c:610
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
uint64 SPI_processed
Definition: spi.c:45
signed int int32
Definition: c.h:355
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
Portal SPI_cursor_find(const char *name)
Definition: spi.c:1521
#define ERROR
Definition: elog.h:43
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define ereport(elevel,...)
Definition: elog.h:144
static void xmldata_root_element_end(StringInfo result, const char *eltname)
Definition: xml.c:2650
const char * name
Definition: encode.c:521
char * text_to_cstring(const text *t)
Definition: varlena.c:204
int errmsg(const char *fmt,...)
Definition: elog.c:824
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:447
int i
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Definition: spi.c:1533
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:3738

◆ cursor_to_xmlschema()

Datum cursor_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2750 of file xml.c.

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.

2751 {
2753  bool nulls = PG_GETARG_BOOL(1);
2754  bool tableforest = PG_GETARG_BOOL(2);
2755  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2756  const char *xmlschema;
2757  Portal portal;
2758 
2759  SPI_connect();
2760  portal = SPI_cursor_find(name);
2761  if (portal == NULL)
2762  ereport(ERROR,
2763  (errcode(ERRCODE_UNDEFINED_CURSOR),
2764  errmsg("cursor \"%s\" does not exist", name)));
2765 
2766  xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2767  InvalidOid, nulls,
2768  tableforest, targetns));
2769  SPI_finish();
2770 
2771  PG_RETURN_XML_P(cstring_to_xmltype(xmlschema));
2772 }
int SPI_connect(void)
Definition: spi.c:89
int SPI_finish(void)
Definition: spi.c:176
int errcode(int sqlerrcode)
Definition: elog.c:610
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
Portal SPI_cursor_find(const char *name)
Definition: spi.c:1521
#define ERROR
Definition: elog.h:43
static char * _SPI_strdup(const char *s)
Definition: xml.c:2385
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
static const char * map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3168
TupleDesc tupDesc
Definition: portal.h:159
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:144
static xmltype * cstring_to_xmltype(const char *string)
Definition: xml.c:454
const char * name
Definition: encode.c:521
char * text_to_cstring(const text *t)
Definition: varlena.c:204
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ database_get_xml_visible_schemas()

static List* database_get_xml_visible_schemas ( void  )
static

Definition at line 2498 of file xml.c.

References query_to_oid_list(), and XML_VISIBLE_SCHEMAS.

Referenced by database_to_xml_internal(), and database_to_xmlschema_internal().

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

◆ database_get_xml_visible_tables()

static List* database_get_xml_visible_tables ( void  )
static

Definition at line 2505 of file xml.c.

References CppAsString2, query_to_oid_list(), and XML_VISIBLE_SCHEMAS.

Referenced by database_to_xmlschema_internal().

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

◆ database_to_xml()

Datum database_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 3051 of file xml.c.

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

3052 {
3053  bool nulls = PG_GETARG_BOOL(0);
3054  bool tableforest = PG_GETARG_BOOL(1);
3055  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
3056 
3058  tableforest, targetns)));
3059 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
static StringInfo database_to_xml_internal(const char *xmlschema, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3008
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
char * text_to_cstring(const text *t)
Definition: varlena.c:204
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:447

◆ database_to_xml_and_xmlschema()

Datum database_to_xml_and_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 3118 of file xml.c.

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

3119 {
3120  bool nulls = PG_GETARG_BOOL(0);
3121  bool tableforest = PG_GETARG_BOOL(1);
3122  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
3123  StringInfo xmlschema;
3124 
3125  xmlschema = database_to_xmlschema_internal(nulls, tableforest, targetns);
3126 
3128  nulls, tableforest, targetns)));
3129 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
static StringInfo database_to_xml_internal(const char *xmlschema, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3008
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
static StringInfo database_to_xmlschema_internal(bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3063
char * text_to_cstring(const text *t)
Definition: varlena.c:204
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:447

◆ database_to_xml_internal()

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

Definition at line 3008 of file xml.c.

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

3010 {
3011  StringInfo result;
3012  List *nspid_list;
3013  ListCell *cell;
3014  char *xmlcn;
3015 
3017  true, false);
3018  result = makeStringInfo();
3019 
3020  xmldata_root_element_start(result, xmlcn, xmlschema, targetns, true);
3021  appendStringInfoChar(result, '\n');
3022 
3023  if (xmlschema)
3024  appendStringInfo(result, "%s\n\n", xmlschema);
3025 
3026  SPI_connect();
3027 
3028  nspid_list = database_get_xml_visible_schemas();
3029 
3030  foreach(cell, nspid_list)
3031  {
3032  Oid nspid = lfirst_oid(cell);
3033  StringInfo subres;
3034 
3035  subres = schema_to_xml_internal(nspid, NULL, nulls,
3036  tableforest, targetns, false);
3037 
3038  appendBinaryStringInfo(result, subres->data, subres->len);
3039  appendStringInfoChar(result, '\n');
3040  }
3041 
3042  SPI_finish();
3043 
3044  xmldata_root_element_end(result, xmlcn);
3045 
3046  return result;
3047 }
static void xmldata_root_element_start(StringInfo result, const char *eltname, const char *xmlschema, const char *targetns, bool top_level)
Definition: xml.c:2623
static StringInfo schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2833
int SPI_connect(void)
Definition: spi.c:89
int SPI_finish(void)
Definition: spi.c:176
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
unsigned int Oid
Definition: postgres_ext.h:31
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2155
char * map_sql_identifier_to_xml_name(const char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:2036
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
static List * database_get_xml_visible_schemas(void)
Definition: xml.c:2498
Oid MyDatabaseId
Definition: globals.c:85
static void xmldata_root_element_end(StringInfo result, const char *eltname)
Definition: xml.c:2650
Definition: pg_list.h:50
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227
#define lfirst_oid(lc)
Definition: pg_list.h:192

◆ database_to_xmlschema()

Datum database_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 3106 of file xml.c.

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

3107 {
3108  bool nulls = PG_GETARG_BOOL(0);
3109  bool tableforest = PG_GETARG_BOOL(1);
3110  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
3111 
3113  tableforest, targetns)));
3114 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
static StringInfo database_to_xmlschema_internal(bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3063
char * text_to_cstring(const text *t)
Definition: varlena.c:204
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:447

◆ database_to_xmlschema_internal()

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

Definition at line 3063 of file xml.c.

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

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

◆ escape_xml()

char* escape_xml ( const char *  str)

Definition at line 2353 of file xml.c.

References appendStringInfoCharMacro, appendStringInfoString(), buf, StringInfoData::data, and initStringInfo().

Referenced by ExplainProperty(), ExplainPropertyList(), map_sql_value_to_xml_value(), SPI_sql_row_to_xmlelement(), and XmlTableGetValue().

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

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

References appendStringInfo(), appendStringInfoString(), 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().

3138 {
3139  StringInfoData result;
3140 
3141  initStringInfo(&result);
3142 
3143  if (a)
3144  appendStringInfoString(&result,
3145  map_sql_identifier_to_xml_name(a, true, true));
3146  if (b)
3147  appendStringInfo(&result, ".%s",
3148  map_sql_identifier_to_xml_name(b, true, true));
3149  if (c)
3150  appendStringInfo(&result, ".%s",
3151  map_sql_identifier_to_xml_name(c, true, true));
3152  if (d)
3153  appendStringInfo(&result, ".%s",
3154  map_sql_identifier_to_xml_name(d, true, true));
3155 
3156  return result.data;
3157 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
char * c
char * map_sql_identifier_to_xml_name(const char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:2036
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59

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

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

3348 {
3349  char *dbname;
3350  char *xmlcn;
3351  char *catalogtypename;
3352  StringInfoData result;
3353  ListCell *cell;
3354 
3355  dbname = get_database_name(MyDatabaseId);
3356 
3357  initStringInfo(&result);
3358 
3359  xmlcn = map_sql_identifier_to_xml_name(dbname, true, false);
3360 
3361  catalogtypename = map_multipart_sql_identifier_to_xml_name("CatalogType",
3362  dbname,
3363  NULL,
3364  NULL);
3365 
3366  appendStringInfo(&result,
3367  "<xsd:complexType name=\"%s\">\n", catalogtypename);
3368  appendStringInfoString(&result,
3369  " <xsd:all>\n");
3370 
3371  foreach(cell, nspid_list)
3372  {
3373  Oid nspid = lfirst_oid(cell);
3374  char *nspname = get_namespace_name(nspid);
3375  char *xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
3376  char *schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
3377  dbname,
3378  nspname,
3379  NULL);
3380 
3381  appendStringInfo(&result,
3382  " <xsd:element name=\"%s\" type=\"%s\"/>\n",
3383  xmlsn, schematypename);
3384  }
3385 
3386  appendStringInfoString(&result,
3387  " </xsd:all>\n");
3388  appendStringInfoString(&result,
3389  "</xsd:complexType>\n\n");
3390 
3391  appendStringInfo(&result,
3392  "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3393  xmlcn, catalogtypename);
3394 
3395  return result.data;
3396 }
unsigned int Oid
Definition: postgres_ext.h:31
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2155
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3120
char * map_sql_identifier_to_xml_name(const char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:2036
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
Oid MyDatabaseId
Definition: globals.c:85
static char * map_multipart_sql_identifier_to_xml_name(const char *a, const char *b, const char *c, const char *d)
Definition: xml.c:3137
char * dbname
Definition: streamutil.c:50
#define lfirst_oid(lc)
Definition: pg_list.h:192

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

References appendBinaryStringInfo(), appendStringInfo(), appendStringInfoString(), Assert, buf, StringInfoData::data, 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().

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

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

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

3275 {
3276  char *dbname;
3277  char *nspname;
3278  char *xmlsn;
3279  char *schematypename;
3280  StringInfoData result;
3281  ListCell *cell;
3282 
3283  dbname = get_database_name(MyDatabaseId);
3284  nspname = get_namespace_name(nspid);
3285 
3286  initStringInfo(&result);
3287 
3288  xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
3289 
3290  schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
3291  dbname,
3292  nspname,
3293  NULL);
3294 
3295  appendStringInfo(&result,
3296  "<xsd:complexType name=\"%s\">\n", schematypename);
3297  if (!tableforest)
3298  appendStringInfoString(&result,
3299  " <xsd:all>\n");
3300  else
3301  appendStringInfoString(&result,
3302  " <xsd:sequence>\n");
3303 
3304  foreach(cell, relid_list)
3305  {
3306  Oid relid = lfirst_oid(cell);
3307  char *relname = get_rel_name(relid);
3308  char *xmltn = map_sql_identifier_to_xml_name(relname, true, false);
3309  char *tabletypename = map_multipart_sql_identifier_to_xml_name(tableforest ? "RowType" : "TableType",
3310  dbname,
3311  nspname,
3312  relname);
3313 
3314  if (!tableforest)
3315  appendStringInfo(&result,
3316  " <xsd:element name=\"%s\" type=\"%s\"/>\n",
3317  xmltn, tabletypename);
3318  else
3319  appendStringInfo(&result,
3320  " <xsd:element name=\"%s\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n",
3321  xmltn, tabletypename);
3322  }
3323 
3324  if (!tableforest)
3325  appendStringInfoString(&result,
3326  " </xsd:all>\n");
3327  else
3328  appendStringInfoString(&result,
3329  " </xsd:sequence>\n");
3330  appendStringInfoString(&result,
3331  "</xsd:complexType>\n\n");
3332 
3333  appendStringInfo(&result,
3334  "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3335  xmlsn, schematypename);
3336 
3337  return result.data;
3338 }
NameData relname
Definition: pg_class.h:38
unsigned int Oid
Definition: postgres_ext.h:31
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2155
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3120
char * map_sql_identifier_to_xml_name(const char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:2036
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
Oid MyDatabaseId
Definition: globals.c:85
static char * map_multipart_sql_identifier_to_xml_name(const char *a, const char *b, const char *c, const char *d)
Definition: xml.c:3137
char * dbname
Definition: streamutil.c:50
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1756
#define lfirst_oid(lc)
Definition: pg_list.h:192

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

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

3170 {
3171  int i;
3172  char *xmltn;
3173  char *tabletypename;
3174  char *rowtypename;
3175  StringInfoData result;
3176 
3177  initStringInfo(&result);
3178 
3179  if (OidIsValid(relid))
3180  {
3181  HeapTuple tuple;
3182  Form_pg_class reltuple;
3183 
3184  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
3185  if (!HeapTupleIsValid(tuple))
3186  elog(ERROR, "cache lookup failed for relation %u", relid);
3187  reltuple = (Form_pg_class) GETSTRUCT(tuple);
3188 
3189  xmltn = map_sql_identifier_to_xml_name(NameStr(reltuple->relname),
3190  true, false);
3191 
3192  tabletypename = map_multipart_sql_identifier_to_xml_name("TableType",
3194  get_namespace_name(reltuple->relnamespace),
3195  NameStr(reltuple->relname));
3196 
3197  rowtypename = map_multipart_sql_identifier_to_xml_name("RowType",
3199  get_namespace_name(reltuple->relnamespace),
3200  NameStr(reltuple->relname));
3201 
3202  ReleaseSysCache(tuple);
3203  }
3204  else
3205  {
3206  if (tableforest)
3207  xmltn = "row";
3208  else
3209  xmltn = "table";
3210 
3211  tabletypename = "TableType";
3212  rowtypename = "RowType";
3213  }
3214 
3215  xsd_schema_element_start(&result, targetns);
3216 
3217  appendStringInfoString(&result,
3219 
3220  appendStringInfo(&result,
3221  "<xsd:complexType name=\"%s\">\n"
3222  " <xsd:sequence>\n",
3223  rowtypename);
3224 
3225  for (i = 0; i < tupdesc->natts; i++)
3226  {
3227  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
3228 
3229  if (att->attisdropped)
3230  continue;
3231  appendStringInfo(&result,
3232  " <xsd:element name=\"%s\" type=\"%s\"%s></xsd:element>\n",
3234  true, false),
3235  map_sql_type_to_xml_name(att->atttypid, -1),
3236  nulls ? " nillable=\"true\"" : " minOccurs=\"0\"");
3237  }
3238 
3239  appendStringInfoString(&result,
3240  " </xsd:sequence>\n"
3241  "</xsd:complexType>\n\n");
3242 
3243  if (!tableforest)
3244  {
3245  appendStringInfo(&result,
3246  "<xsd:complexType name=\"%s\">\n"
3247  " <xsd:sequence>\n"
3248  " <xsd:element name=\"row\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n"
3249  " </xsd:sequence>\n"
3250  "</xsd:complexType>\n\n",
3251  tabletypename, rowtypename);
3252 
3253  appendStringInfo(&result,
3254  "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3255  xmltn, tabletypename);
3256  }
3257  else
3258  appendStringInfo(&result,
3259  "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3260  xmltn, rowtypename);
3261 
3262  xsd_schema_element_end(&result);
3263 
3264  return result.data;
3265 }
static void xsd_schema_element_start(StringInfo result, const char *targetns)
Definition: xml.c:2898
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define OidIsValid(objectId)
Definition: c.h:644
#define list_make1(x1)
Definition: pg_list.h:227
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2155
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3120
char * map_sql_identifier_to_xml_name(const char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:2036
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
static const char * map_sql_typecoll_to_xmlschema_types(List *tupdesc_list)
Definition: xml.c:3508
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
static const char * map_sql_type_to_xml_name(Oid typeoid, int typmod)
Definition: xml.c:3403
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
static void xsd_schema_element_end(StringInfo result)
Definition: xml.c:2915
Oid MyDatabaseId
Definition: globals.c:85
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static char * map_multipart_sql_identifier_to_xml_name(const char *a, const char *b, const char *c, const char *d)
Definition: xml.c:3137
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
#define elog(elevel,...)
Definition: elog.h:214
int i
#define NameStr(name)
Definition: c.h:615

◆ map_sql_type_to_xml_name()

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

Definition at line 3403 of file xml.c.

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

3404 {
3405  StringInfoData result;
3406 
3407  initStringInfo(&result);
3408 
3409  switch (typeoid)
3410  {
3411  case BPCHAROID:
3412  if (typmod == -1)
3413  appendStringInfoString(&result, "CHAR");
3414  else
3415  appendStringInfo(&result, "CHAR_%d", typmod - VARHDRSZ);
3416  break;
3417  case VARCHAROID:
3418  if (typmod == -1)
3419  appendStringInfoString(&result, "VARCHAR");
3420  else
3421  appendStringInfo(&result, "VARCHAR_%d", typmod - VARHDRSZ);
3422  break;
3423  case NUMERICOID:
3424  if (typmod == -1)
3425  appendStringInfoString(&result, "NUMERIC");
3426  else
3427  appendStringInfo(&result, "NUMERIC_%d_%d",
3428  ((typmod - VARHDRSZ) >> 16) & 0xffff,
3429  (typmod - VARHDRSZ) & 0xffff);
3430  break;
3431  case INT4OID:
3432  appendStringInfoString(&result, "INTEGER");
3433  break;
3434  case INT2OID:
3435  appendStringInfoString(&result, "SMALLINT");
3436  break;
3437  case INT8OID:
3438  appendStringInfoString(&result, "BIGINT");
3439  break;
3440  case FLOAT4OID:
3441  appendStringInfoString(&result, "REAL");
3442  break;
3443  case FLOAT8OID:
3444  appendStringInfoString(&result, "DOUBLE");
3445  break;
3446  case BOOLOID:
3447  appendStringInfoString(&result, "BOOLEAN");
3448  break;
3449  case TIMEOID:
3450  if (typmod == -1)
3451  appendStringInfoString(&result, "TIME");
3452  else
3453  appendStringInfo(&result, "TIME_%d", typmod);
3454  break;
3455  case TIMETZOID:
3456  if (typmod == -1)
3457  appendStringInfoString(&result, "TIME_WTZ");
3458  else
3459  appendStringInfo(&result, "TIME_WTZ_%d", typmod);
3460  break;
3461  case TIMESTAMPOID:
3462  if (typmod == -1)
3463  appendStringInfoString(&result, "TIMESTAMP");
3464  else
3465  appendStringInfo(&result, "TIMESTAMP_%d", typmod);
3466  break;
3467  case TIMESTAMPTZOID:
3468  if (typmod == -1)
3469  appendStringInfoString(&result, "TIMESTAMP_WTZ");
3470  else
3471  appendStringInfo(&result, "TIMESTAMP_WTZ_%d", typmod);
3472  break;
3473  case DATEOID:
3474  appendStringInfoString(&result, "DATE");
3475  break;
3476  case XMLOID:
3477  appendStringInfoString(&result, "XML");
3478  break;
3479  default:
3480  {
3481  HeapTuple tuple;
3482  Form_pg_type typtuple;
3483 
3484  tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typeoid));
3485  if (!HeapTupleIsValid(tuple))
3486  elog(ERROR, "cache lookup failed for type %u", typeoid);
3487  typtuple = (Form_pg_type) GETSTRUCT(tuple);
3488 
3489  appendStringInfoString(&result,
3490  map_multipart_sql_identifier_to_xml_name((typtuple->typtype == TYPTYPE_DOMAIN) ? "Domain" : "UDT",
3492  get_namespace_name(typtuple->typnamespace),
3493  NameStr(typtuple->typname)));
3494 
3495  ReleaseSysCache(tuple);
3496  }
3497  }
3498 
3499  return result.data;
3500 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define VARHDRSZ
Definition: c.h:561
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2155
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3120
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
Oid MyDatabaseId
Definition: globals.c:85
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static char * map_multipart_sql_identifier_to_xml_name(const char *a, const char *b, const char *c, const char *d)
Definition: xml.c:3137
FormData_pg_type * Form_pg_type
Definition: pg_type.h:255
#define elog(elevel,...)
Definition: elog.h:214
#define NameStr(name)
Definition: c.h:615

◆ map_sql_type_to_xmlschema_type()

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

Definition at line 3563 of file xml.c.

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

Referenced by map_sql_typecoll_to_xmlschema_types().

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

◆ map_sql_typecoll_to_xmlschema_types()

static const char * map_sql_typecoll_to_xmlschema_types ( List tupdesc_list)
static

Definition at line 3508 of file xml.c.

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

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

◆ map_sql_value_to_xml_value()

char* map_sql_value_to_xml_value ( Datum  value,
Oid  type,
bool  xml_escape_strings 
)

Definition at line 2134 of file xml.c.

References appendStringInfoString(), ARR_ELEMTYPE, buf, StringInfoData::data, 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(), map_sql_value_to_xml_value(), 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, pg_tm::tm_mday, pg_tm::tm_mon, pg_tm::tm_year, type_is_array_domain, USE_XSD_DATES, VARDATA_ANY, VARSIZE_ANY_EXHDR, xml_ereport(), xmlbinary, and XMLBINARY_BASE64.

Referenced by ExecEvalXmlExpr(), map_sql_value_to_xml_value(), SPI_sql_row_to_xmlelement(), and xmlelement().

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

◆ map_xml_name_to_sql_identifier()

char* map_xml_name_to_sql_identifier ( const char *  name)

Definition at line 2092 of file xml.c.

References appendBinaryStringInfo(), appendStringInfoString(), buf, StringInfoData::data, initStringInfo(), MAX_UNICODE_EQUIVALENT_STRING, pg_mblen(), and pg_unicode_to_server().

Referenced by get_rule_expr().

2093 {
2095  const char *p;
2096 
2097  initStringInfo(&buf);
2098 
2099  for (p = name; *p; p += pg_mblen(p))
2100  {
2101  if (*p == '_' && *(p + 1) == 'x'
2102  && isxdigit((unsigned char) *(p + 2))
2103  && isxdigit((unsigned char) *(p + 3))
2104  && isxdigit((unsigned char) *(p + 4))
2105  && isxdigit((unsigned char) *(p + 5))
2106  && *(p + 6) == '_')
2107  {
2108  char cbuf[MAX_UNICODE_EQUIVALENT_STRING + 1];
2109  unsigned int u;
2110 
2111  sscanf(p + 2, "%X", &u);
2112  pg_unicode_to_server(u, (unsigned char *) cbuf);
2113  appendStringInfoString(&buf, cbuf);
2114  p += 6;
2115  }
2116  else
2117  appendBinaryStringInfo(&buf, p, pg_mblen(p));
2118  }
2119 
2120  return buf.data;
2121 }
#define MAX_UNICODE_EQUIVALENT_STRING
Definition: pg_wchar.h:325
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:67
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
void pg_unicode_to_server(pg_wchar c, unsigned char *s)
Definition: mbutils.c:806
int pg_mblen(const char *mbstr)
Definition: mbutils.c:907
const char * name
Definition: encode.c:521
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227

◆ query_to_oid_list()

static List* query_to_oid_list ( const char *  query)
static

Definition at line 2442 of file xml.c.

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

2443 {
2444  uint64 i;
2445  List *list = NIL;
2446  int spi_result;
2447 
2448  spi_result = SPI_execute(query, true, 0);
2449  if (spi_result != SPI_OK_SELECT)
2450  elog(ERROR, "SPI_execute returned %s for %s",
2451  SPI_result_code_string(spi_result), query);
2452 
2453  for (i = 0; i < SPI_processed; i++)
2454  {
2455  Datum oid;
2456  bool isnull;
2457 
2458  oid = SPI_getbinval(SPI_tuptable->vals[i],
2460  1,
2461  &isnull);
2462  if (!isnull)
2463  list = lappend_oid(list, DatumGetObjectId(oid));
2464  }
2465 
2466  return list;
2467 }
#define NIL
Definition: pg_list.h:65
#define DatumGetObjectId(X)
Definition: postgres.h:500
SPITupleTable * SPI_tuptable
Definition: spi.c:46
HeapTuple * vals
Definition: spi.h:26
List * lappend_oid(List *list, Oid datum)
Definition: list.c:358
uint64 SPI_processed
Definition: spi.c:45
#define ERROR
Definition: elog.h:43
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition: spi.c:1028
const char * SPI_result_code_string(int code)
Definition: spi.c:1699
uintptr_t Datum
Definition: postgres.h:367
TupleDesc tupdesc
Definition: spi.h:25
#define SPI_OK_SELECT
Definition: spi.h:57
#define elog(elevel,...)
Definition: elog.h:214
int i
Definition: pg_list.h:50
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:496

◆ query_to_xml()

Datum query_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 2555 of file xml.c.

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

2556 {
2557  char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2558  bool nulls = PG_GETARG_BOOL(1);
2559  bool tableforest = PG_GETARG_BOOL(2);
2560  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2561 
2563  NULL, nulls, tableforest,
2564  targetns, true)));
2565 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
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:2657
char * text_to_cstring(const text *t)
Definition: varlena.c:204
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:447

◆ query_to_xml_and_xmlschema()

Datum query_to_xml_and_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2797 of file xml.c.

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.

2798 {
2799  char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2800  bool nulls = PG_GETARG_BOOL(1);
2801  bool tableforest = PG_GETARG_BOOL(2);
2802  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2803 
2804  const char *xmlschema;
2805  SPIPlanPtr plan;
2806  Portal portal;
2807 
2808  SPI_connect();
2809 
2810  if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
2811  elog(ERROR, "SPI_prepare(\"%s\") failed", query);
2812 
2813  if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
2814  elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
2815 
2816  xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2817  InvalidOid, nulls, tableforest, targetns));
2818  SPI_cursor_close(portal);
2819  SPI_finish();
2820 
2822  xmlschema, nulls, tableforest,
2823  targetns, true)));
2824 }
int SPI_connect(void)
Definition: spi.c:89
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:674
int SPI_finish(void)
Definition: spi.c:176
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1221
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define ERROR
Definition: elog.h:43
static char * _SPI_strdup(const char *s)
Definition: xml.c:2385
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
static const char * map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3168
TupleDesc tupDesc
Definition: portal.h:159
#define InvalidOid
Definition: postgres_ext.h:36
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:2657
void SPI_cursor_close(Portal portal)
Definition: spi.c:1589
char * text_to_cstring(const text *t)
Definition: varlena.c:204
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:447
#define elog(elevel,...)
Definition: elog.h:214

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

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

2660 {
2661  StringInfo result;
2662  char *xmltn;
2663  uint64 i;
2664 
2665  if (tablename)
2666  xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
2667  else
2668  xmltn = "table";
2669 
2670  result = makeStringInfo();
2671 
2672  SPI_connect();
2673  if (SPI_execute(query, true, 0) != SPI_OK_SELECT)
2674  ereport(ERROR,
2675  (errcode(ERRCODE_DATA_EXCEPTION),
2676  errmsg("invalid query")));
2677 
2678  if (!tableforest)
2679  {
2680  xmldata_root_element_start(result, xmltn, xmlschema,
2681  targetns, top_level);
2682  appendStringInfoChar(result, '\n');
2683  }
2684 
2685  if (xmlschema)
2686  appendStringInfo(result, "%s\n\n", xmlschema);
2687 
2688  for (i = 0; i < SPI_processed; i++)
2689  SPI_sql_row_to_xmlelement(i, result, tablename, nulls,
2690  tableforest, targetns, top_level);
2691 
2692  if (!tableforest)
2693  xmldata_root_element_end(result, xmltn);
2694 
2695  SPI_finish();
2696 
2697  return result;
2698 }
static void xmldata_root_element_start(StringInfo result, const char *eltname, const char *xmlschema, const char *targetns, bool top_level)
Definition: xml.c:2623
int SPI_connect(void)
Definition: spi.c:89
int SPI_finish(void)
Definition: spi.c:176
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
int errcode(int sqlerrcode)
Definition: elog.c:610
uint64 SPI_processed
Definition: spi.c:45
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ERROR
Definition: elog.h:43
char * map_sql_identifier_to_xml_name(const char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:2036
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
#define SPI_OK_SELECT
Definition: spi.h:57
#define ereport(elevel,...)
Definition: elog.h:144
static void xmldata_root_element_end(StringInfo result, const char *eltname)
Definition: xml.c:2650
int errmsg(const char *fmt,...)
Definition: elog.c:824
int i
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:3738
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:496

◆ query_to_xmlschema()

Datum query_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2721 of file xml.c.

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.

2722 {
2723  char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2724  bool nulls = PG_GETARG_BOOL(1);
2725  bool tableforest = PG_GETARG_BOOL(2);
2726  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2727  const char *result;
2728  SPIPlanPtr plan;
2729  Portal portal;
2730 
2731  SPI_connect();
2732 
2733  if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
2734  elog(ERROR, "SPI_prepare(\"%s\") failed", query);
2735 
2736  if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
2737  elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
2738 
2740  InvalidOid, nulls,
2741  tableforest, targetns));
2742  SPI_cursor_close(portal);
2743  SPI_finish();
2744 
2746 }
int SPI_connect(void)
Definition: spi.c:89
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:674
int SPI_finish(void)
Definition: spi.c:176
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1221
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define ERROR
Definition: elog.h:43
static char * _SPI_strdup(const char *s)
Definition: xml.c:2385
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
static const char * map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3168
TupleDesc tupDesc
Definition: portal.h:159
#define InvalidOid
Definition: postgres_ext.h:36
static xmltype * cstring_to_xmltype(const char *string)
Definition: xml.c:454
void SPI_cursor_close(Portal portal)
Definition: spi.c:1589
char * text_to_cstring(const text *t)
Definition: varlena.c:204
#define elog(elevel,...)
Definition: elog.h:214

◆ schema_get_xml_visible_tables()

static List* schema_get_xml_visible_tables ( Oid  nspid)
static

Definition at line 2471 of file xml.c.

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

Referenced by schema_to_xml_internal(), and schema_to_xmlschema_internal().

2472 {
2473  StringInfoData query;
2474 
2475  initStringInfo(&query);
2476  appendStringInfo(&query, "SELECT oid FROM pg_catalog.pg_class"
2477  " WHERE relnamespace = %u AND relkind IN ("
2478  CppAsString2(RELKIND_RELATION) ","
2479  CppAsString2(RELKIND_MATVIEW) ","
2480  CppAsString2(RELKIND_VIEW) ")"
2481  " AND pg_catalog.has_table_privilege (oid, 'SELECT')"
2482  " ORDER BY relname;", nspid);
2483 
2484  return query_to_oid_list(query.data);
2485 }
static List * query_to_oid_list(const char *query)
Definition: xml.c:2442
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define CppAsString2(x)
Definition: c.h:222

◆ schema_to_xml()

Datum schema_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 2876 of file xml.c.

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

2877 {
2878  Name name = PG_GETARG_NAME(0);
2879  bool nulls = PG_GETARG_BOOL(1);
2880  bool tableforest = PG_GETARG_BOOL(2);
2881  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2882 
2883  char *schemaname;
2884  Oid nspid;
2885 
2886  schemaname = NameStr(*name);
2887  nspid = LookupExplicitNamespace(schemaname, false);
2888 
2890  nulls, tableforest, targetns, true)));
2891 }
static StringInfo schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2833
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
Definition: namespace.c:2893
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
Definition: c.h:609
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
const char * name
Definition: encode.c:521
char * text_to_cstring(const text *t)
Definition: varlena.c:204
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:447
#define NameStr(name)
Definition: c.h:615
#define PG_GETARG_NAME(n)
Definition: fmgr.h:273

◆ schema_to_xml_and_xmlschema()

Datum schema_to_xml_and_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2980 of file xml.c.

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

2981 {
2982  Name name = PG_GETARG_NAME(0);
2983  bool nulls = PG_GETARG_BOOL(1);
2984  bool tableforest = PG_GETARG_BOOL(2);
2985  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2986  char *schemaname;
2987  Oid nspid;
2988  StringInfo xmlschema;
2989 
2990  schemaname = NameStr(*name);
2991  nspid = LookupExplicitNamespace(schemaname, false);
2992 
2993  xmlschema = schema_to_xmlschema_internal(schemaname, nulls,
2994  tableforest, targetns);
2995 
2997  xmlschema->data, nulls,
2998  tableforest, targetns, true)));
2999 }
static StringInfo schema_to_xmlschema_internal(const char *schemaname, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:2922
static StringInfo schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2833
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
Definition: namespace.c:2893
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
Definition: c.h:609
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
const char * name
Definition: encode.c:521
char * text_to_cstring(const text *t)
Definition: varlena.c:204
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:447
#define NameStr(name)
Definition: c.h:615
#define PG_GETARG_NAME(n)
Definition: fmgr.h:273

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

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

2835 {
2836  StringInfo result;
2837  char *xmlsn;
2838  List *relid_list;
2839  ListCell *cell;
2840 
2842  true, false);
2843  result = makeStringInfo();
2844 
2845  xmldata_root_element_start(result, xmlsn, xmlschema, targetns, top_level);
2846  appendStringInfoChar(result, '\n');
2847 
2848  if (xmlschema)
2849  appendStringInfo(result, "%s\n\n", xmlschema);
2850 
2851  SPI_connect();
2852 
2853  relid_list = schema_get_xml_visible_tables(nspid);
2854 
2855  foreach(cell, relid_list)
2856  {
2857  Oid relid = lfirst_oid(cell);
2858  StringInfo subres;
2859 
2860  subres = table_to_xml_internal(relid, NULL, nulls, tableforest,
2861  targetns, false);
2862 
2863  appendBinaryStringInfo(result, subres->data, subres->len);
2864  appendStringInfoChar(result, '\n');
2865  }
2866 
2867  SPI_finish();
2868 
2869  xmldata_root_element_end(result, xmlsn);
2870 
2871  return result;
2872 }
static void xmldata_root_element_start(StringInfo result, const char *eltname, const char *xmlschema, const char *targetns, bool top_level)
Definition: xml.c:2623
static List * schema_get_xml_visible_tables(Oid nspid)
Definition: xml.c:2471
int SPI_connect(void)
Definition: spi.c:89
int SPI_finish(void)
Definition: spi.c:176
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
unsigned int Oid
Definition: postgres_ext.h:31
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3120
char * map_sql_identifier_to_xml_name(const char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:2036
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
static StringInfo table_to_xml_internal(Oid relid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2524
static void xmldata_root_element_end(StringInfo result, const char *eltname)
Definition: xml.c:2650
Definition: pg_list.h:50
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227
#define lfirst_oid(lc)
Definition: pg_list.h:192

◆ schema_to_xmlschema()

Datum schema_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2967 of file xml.c.

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

2968 {
2969  Name name = PG_GETARG_NAME(0);
2970  bool nulls = PG_GETARG_BOOL(1);
2971  bool tableforest = PG_GETARG_BOOL(2);
2972  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2973 
2975  nulls, tableforest, targetns)));
2976 }
static StringInfo schema_to_xmlschema_internal(const char *schemaname, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:2922
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
Definition: c.h:609
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
const char * name
Definition: encode.c:521
char * text_to_cstring(const text *t)
Definition: varlena.c:204
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:447
#define NameStr(name)
Definition: c.h:615
#define PG_GETARG_NAME(n)
Definition: fmgr.h:273

◆ schema_to_xmlschema_internal()

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

Definition at line 2922 of file xml.c.

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

2924 {
2925  Oid nspid;
2926  List *relid_list;
2927  List *tupdesc_list;
2928  ListCell *cell;
2929  StringInfo result;
2930 
2931  result = makeStringInfo();
2932 
2933  nspid = LookupExplicitNamespace(schemaname, false);
2934 
2935  xsd_schema_element_start(result, targetns);
2936 
2937  SPI_connect();
2938 
2939  relid_list = schema_get_xml_visible_tables(nspid);
2940 
2941  tupdesc_list = NIL;
2942  foreach(cell, relid_list)
2943  {
2944  Relation rel;
2945 
2946  rel = table_open(lfirst_oid(cell), AccessShareLock);
2947  tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2948  table_close(rel, NoLock);
2949  }
2950 
2951  appendStringInfoString(result,
2952  map_sql_typecoll_to_xmlschema_types(tupdesc_list));
2953 
2954  appendStringInfoString(result,
2955  map_sql_schema_to_xmlschema_types(nspid, relid_list,
2956  nulls, tableforest, targetns));
2957 
2958  xsd_schema_element_end(result);
2959 
2960  SPI_finish();
2961 
2962  return result;
2963 }
static void xsd_schema_element_start(StringInfo result, const char *targetns)
Definition: xml.c:2898
#define NIL
Definition: pg_list.h:65
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:110
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
Definition: namespace.c:2893
static List * schema_get_xml_visible_tables(Oid nspid)
Definition: xml.c:2471
int SPI_connect(void)
Definition: spi.c:89
int SPI_finish(void)
Definition: spi.c:176
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
#define AccessShareLock
Definition: lockdefs.h:36
unsigned int Oid
Definition: postgres_ext.h:31
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
#define NoLock
Definition: lockdefs.h:34
List * lappend(List *list, void *datum)
Definition: list.c:322
static const char * map_sql_typecoll_to_xmlschema_types(List *tupdesc_list)
Definition: xml.c:3508
static void xsd_schema_element_end(StringInfo result)
Definition: xml.c:2915
TupleDesc rd_att
Definition: rel.h:90
static const char * map_sql_schema_to_xmlschema_types(Oid nspid, List *relid_list, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3273
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50
#define lfirst_oid(lc)
Definition: pg_list.h:192

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

References accumArrayResult(), appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), ARR_DIMS, ARR_ELEMTYPE, ARR_NDIM, Assert, BoolGetDatum, generate_unaccent_rules::bytes(), cstring_to_text(), cstring_to_xmltype(), CStringGetDatum, cur, CurrentMemoryContext, deconstruct_array(), elog, ereport, errcode(), errdetail(), errmsg(), ERROR, escape_xml(), Float8GetDatum(), GetDatabaseEncoding(), i, map_sql_identifier_to_xml_name(), map_sql_value_to_xml_value(), TupleDescData::natts, pfree(), PG_CATCH, PG_END_TRY, PG_FINALLY, PG_RE_THROW, PG_TRY, PG_UTF8, pg_xml_done(), pg_xml_init(), PG_XML_STRICTNESS_ALL, PointerGetDatum, SPI_fname(), SPI_getbinval(), SPI_gettypeid(), SPI_tuptable, generate_unaccent_rules::str, TextDatumGetCString, SPITupleTable::tupdesc, SPITupleTable::vals, VARDATA, VARDATA_ANY, VARHDRSZ, VARSIZE, VARSIZE_ANY_EXHDR, xml_ereport(), xmldata_root_element_end(), and xmldata_root_element_start().

Referenced by cursor_to_xml(), and query_to_xml_internal().

3741 {
3742  int i;
3743  char *xmltn;
3744 
3745  if (tablename)
3746  xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
3747  else
3748  {
3749  if (tableforest)
3750  xmltn = "row";
3751  else
3752  xmltn = "table";
3753  }
3754 
3755  if (tableforest)
3756  xmldata_root_element_start(result, xmltn, NULL, targetns, top_level);
3757  else
3758  appendStringInfoString(result, "<row>\n");
3759 
3760  for (i = 1; i <= SPI_tuptable->tupdesc->natts; i++)
3761  {
3762  char *colname;
3763  Datum colval;
3764  bool isnull;
3765 
3767  true, false);
3768  colval = SPI_getbinval(SPI_tuptable->vals[rownum],
3770  i,
3771  &isnull);
3772  if (isnull)
3773  {
3774  if (nulls)
3775  appendStringInfo(result, " <%s xsi:nil=\"true\"/>\n", colname);
3776  }
3777  else
3778  appendStringInfo(result, " <%s>%s</%s>\n",
3779  colname,
3781  SPI_gettypeid(SPI_tuptable->tupdesc, i), true),
3782  colname);
3783  }
3784 
3785  if (tableforest)
3786  {
3787  xmldata_root_element_end(result, xmltn);
3788  appendStringInfoChar(result, '\n');
3789  }
3790  else
3791  appendStringInfoString(result, "</row>\n\n");
3792 }
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1084
static void xmldata_root_element_start(StringInfo result, const char *eltname, const char *xmlschema, const char *targetns, bool top_level)
Definition: xml.c:2623
SPITupleTable * SPI_tuptable
Definition: spi.c:46
HeapTuple * vals
Definition: spi.h:26
char * SPI_fname(TupleDesc tupdesc, int fnumber)
Definition: spi.c:974
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition: spi.c:1028
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
char * map_sql_identifier_to_xml_name(const char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:2036
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
uintptr_t Datum
Definition: postgres.h:367
TupleDesc tupdesc
Definition: spi.h:25
static void xmldata_root_element_end(StringInfo result, const char *eltname)
Definition: xml.c:2650
int i
char * map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
Definition: xml.c:2134

◆ stringinfo_to_xmltype()

◆ table_to_xml()

Datum table_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 2541 of file xml.c.

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

2542 {
2543  Oid relid = PG_GETARG_OID(0);
2544  bool nulls = PG_GETARG_BOOL(1);
2545  bool tableforest = PG_GETARG_BOOL(2);
2546  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2547 
2549  nulls, tableforest,
2550  targetns, true)));
2551 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
static StringInfo table_to_xml_internal(Oid relid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2524
char * text_to_cstring(const text *t)
Definition: varlena.c:204
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:447

◆ table_to_xml_and_xmlschema()

Datum table_to_xml_and_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2776 of file xml.c.

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

2777 {
2778  Oid relid = PG_GETARG_OID(0);
2779  bool nulls = PG_GETARG_BOOL(1);
2780  bool tableforest = PG_GETARG_BOOL(2);
2781  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2782  Relation rel;
2783  const char *xmlschema;
2784 
2785  rel = table_open(relid, AccessShareLock);
2786  xmlschema = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2787  tableforest, targetns);
2788  table_close(rel, NoLock);
2789 
2791  xmlschema, nulls, tableforest,
2792  targetns, true)));
2793 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define AccessShareLock
Definition: lockdefs.h:36
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define NoLock
Definition: lockdefs.h:34
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
static const char * map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3168
TupleDesc rd_att
Definition: rel.h:90
static StringInfo table_to_xml_internal(Oid relid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2524
char * text_to_cstring(const text *t)
Definition: varlena.c:204
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:447
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

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

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

2527 {
2528  StringInfoData query;
2529 
2530  initStringInfo(&query);
2531  appendStringInfo(&query, "SELECT * FROM %s",
2533  ObjectIdGetDatum(relid))));
2534  return query_to_xml_internal(query.data, get_rel_name(relid),
2535  xmlschema, nulls, tableforest,
2536  targetns, top_level);
2537 }
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:615
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define DatumGetCString(X)
Definition: postgres.h:566
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
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:2657
Datum regclassout(PG_FUNCTION_ARGS)
Definition: regproc.c:973
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1756

◆ table_to_xmlschema()

Datum table_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2702 of file xml.c.

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

2703 {
2704  Oid relid = PG_GETARG_OID(0);
2705  bool nulls = PG_GETARG_BOOL(1);
2706  bool tableforest = PG_GETARG_BOOL(2);
2707  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2708  const char *result;
2709  Relation rel;
2710 
2711  rel = table_open(relid, AccessShareLock);
2712  result = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2713  tableforest, targetns);
2714  table_close(rel, NoLock);
2715 
2717 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define AccessShareLock
Definition: lockdefs.h:36
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define NoLock
Definition: lockdefs.h:34
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
static const char * map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3168
TupleDesc rd_att
Definition: rel.h:90
static xmltype * cstring_to_xmltype(const char *string)
Definition: xml.c:454
char * text_to_cstring(const text *t)
Definition: varlena.c:204
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ texttoxml()

Datum texttoxml ( PG_FUNCTION_ARGS  )

Definition at line 596 of file xml.c.

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

597 {
598  text *data = PG_GETARG_TEXT_PP(0);
599 
600  PG_RETURN_XML_P(xmlparse(data, xmloption, true));
601 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
int xmloption
Definition: xml.c:98
Definition: c.h:555
xmltype * xmlparse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace)
Definition: xml.c:752

◆ xml_in()

Datum xml_in ( PG_FUNCTION_ARGS  )

Definition at line 258 of file xml.c.

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

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

◆ xml_is_document()

bool xml_is_document ( xmltype arg)

Definition at line 888 of file xml.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, appendBinaryStringInfo(), appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), Assert, CopyErrorData(), CurrentMemoryContext, StringInfoData::data, elog, encoding, ereport, errcode(), errdetail(), errdetail_internal(), errhint(), errmsg(), errmsg_internal(), ERROR, error(), FATAL, FlushErrorState(), GetDatabaseEncoding(), gettext_noop, initStringInfo(), StringInfoData::len, makeStringInfo(), MAX_MULTIBYTE_CHAR_LEN, MemoryContextAlloc(), MemoryContextStrdup(), MemoryContextSwitchTo(), name, NO_XML_SUPPORT, NOTICE, palloc(), pfree(), PG_CATCH, pg_do_encoding_conversion(), pg_encoding_mb2wchar_with_len(), pg_encoding_mblen(), pg_encoding_to_char(), PG_END_TRY, PG_FINALLY, pg_mblen(), PG_RE_THROW, pg_server_to_any(), PG_TRY, PG_UTF8, PG_XML_DEFAULT_VERSION, pg_xml_done(), pg_xml_error_occurred(), pg_xml_init(), pg_xml_init_library(), PG_XML_STRICTNESS_LEGACY, PG_XML_STRICTNESS_WELLFORMED, pstrdup(), repalloc(), ErrorData::sqlerrcode, generate_unaccent_rules::str, strnlen(), text_to_cstring(), TopMemoryContext, VARSIZE_ANY_EXHDR, WARNING, xml_ereport(), and XMLOPTION_DOCUMENT.

Referenced by ExecEvalXmlExpr(), and xmltotext_with_xmloption().

889 {
890 #ifdef USE_LIBXML
891  bool result;
892  volatile xmlDocPtr doc = NULL;
894 
895  /* We want to catch ereport(INVALID_XML_DOCUMENT) and return false */
896  PG_TRY();
897  {
898  doc = xml_parse((text *) arg, XMLOPTION_DOCUMENT, true,
900  result = true;
901  }
902  PG_CATCH();
903  {
904  ErrorData *errdata;
905  MemoryContext ecxt;
906 
907  ecxt = MemoryContextSwitchTo(ccxt);
908  errdata = CopyErrorData();
909  if (errdata->sqlerrcode == ERRCODE_INVALID_XML_DOCUMENT)
910  {
911  FlushErrorState();
912  result = false;
913  }
914  else
915  {
916  MemoryContextSwitchTo(ecxt);
917  PG_RE_THROW();
918  }
919  }
920  PG_END_TRY();
921 
922  if (doc)
923  xmlFreeDoc(doc);
924 
925  return result;
926 #else /* not USE_LIBXML */
927  NO_XML_SUPPORT();
928  return false;
929 #endif /* not USE_LIBXML */
930 }
#define NO_XML_SUPPORT()
Definition: xml.c:219
int sqlerrcode
Definition: elog.h:364
ErrorData * CopyErrorData(void)
Definition: elog.c:1474
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void FlushErrorState(void)
Definition: elog.c:1568
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
int GetDatabaseEncoding(void)
Definition: mbutils.c:1151
#define PG_CATCH()
Definition: elog.h:305
#define PG_RE_THROW()
Definition: elog.h:336
Definition: c.h:555
#define PG_TRY()
Definition: elog.h:295
#define PG_END_TRY()
Definition: elog.h:320

◆ xml_is_well_formed()

Datum xml_is_well_formed ( PG_FUNCTION_ARGS  )

Definition at line 4263 of file xml.c.

References NO_XML_SUPPORT, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and xmloption.

4264 {
4265 #ifdef USE_LIBXML
4266  text *data = PG_GETARG_TEXT_PP(0);
4267 
4268  PG_RETURN_BOOL(wellformed_xml(data, xmloption));
4269 #else
4270  NO_XML_SUPPORT();
4271  return 0;
4272 #endif /* not USE_LIBXML */
4273 }
#define NO_XML_SUPPORT()
Definition: xml.c:219
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
int xmloption
Definition: xml.c:98
Definition: c.h:555

◆ xml_is_well_formed_content()

Datum xml_is_well_formed_content ( PG_FUNCTION_ARGS  )

Definition at line 4289 of file xml.c.

References elog, ERROR, IsA, NO_XML_SUPPORT, TableFuncScanState::opaque, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and XMLOPTION_CONTENT.

4290 {
4291 #ifdef USE_LIBXML
4292  text *data = PG_GETARG_TEXT_PP(0);
4293 
4294  PG_RETURN_BOOL(wellformed_xml(data, XMLOPTION_CONTENT));
4295 #else
4296  NO_XML_SUPPORT();
4297  return 0;
4298 #endif /* not USE_LIBXML */
4299 }
#define NO_XML_SUPPORT()
Definition: xml.c:219
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
Definition: c.h:555

◆ xml_is_well_formed_document()

Datum xml_is_well_formed_document ( PG_FUNCTION_ARGS  )

Definition at line 4276 of file xml.c.

References NO_XML_SUPPORT, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and XMLOPTION_DOCUMENT.

4277 {
4278 #ifdef USE_LIBXML
4279  text *data = PG_GETARG_TEXT_PP(0);
4280 
4281  PG_RETURN_BOOL(wellformed_xml(data, XMLOPTION_DOCUMENT));
4282 #else
4283  NO_XML_SUPPORT();
4284  return 0;
4285 #endif /* not USE_LIBXML */
4286 }
#define NO_XML_SUPPORT()
Definition: xml.c:219
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
Definition: c.h:555

◆ xml_out()

Datum xml_out ( PG_FUNCTION_ARGS  )

Definition at line 336 of file xml.c.

References PG_GETARG_XML_P, PG_RETURN_CSTRING, and xml_out_internal().

337 {
338  xmltype *x = PG_GETARG_XML_P(0);
339 
340  /*
341  * xml_out removes the encoding property in all cases. This is because we
342  * cannot control from here whether the datum will be converted to a
343  * different client encoding, so we'd do more harm than good by including
344  * it.
345  */
347 }
#define PG_GETARG_XML_P(n)
Definition: xml.h:53
static char * xml_out_internal(xmltype *x, pg_enc target_encoding)
Definition: xml.c:293
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352
Definition: c.h:555

◆ xml_out_internal()

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

Definition at line 293 of file xml.c.

References appendStringInfoString(), buf, StringInfoData::data, initStringInfo(), pfree(), generate_unaccent_rules::str, text_to_cstring(), and WARNING.

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

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

◆ xml_recv()

Datum xml_recv ( PG_FUNCTION_ARGS  )

Definition at line 351 of file xml.c.

References cstring_to_text(), StringInfoData::cursor, encoding, StringInfoData::len, 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.

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

◆ xml_send()

Datum xml_send ( PG_FUNCTION_ARGS  )

Definition at line 418 of file xml.c.

References appendBinaryStringInfo(), appendStringInfoText(), buf, pfree(), pg_get_client_encoding(), PG_GETARG_XML_P, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendtext(), VARDATA_ANY, VARSIZE_ANY_EXHDR, and xml_out_internal().

419 {
420  xmltype *x = PG_GETARG_XML_P(0);
421  char *outval;
423 
424  /*
425  * xml_out_internal doesn't convert the encoding, it just prints the right
426  * declaration. pq_sendtext will do the conversion.
427  */
429 
430  pq_begintypsend(&buf);
431  pq_sendtext(&buf, outval, strlen(outval));
432  pfree(outval);
434 }
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
void pq_sendtext(StringInfo buf, const char *str, int slen)
Definition: pqformat.c:174
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:360
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
void pfree(void *pointer)
Definition: mcxt.c:1056
static char * buf
Definition: pg_test_fsync.c:67
#define PG_GETARG_XML_P(n)
Definition: xml.h:53
int pg_get_client_encoding(void)
Definition: mbutils.c:336
static char * xml_out_internal(xmltype *x, pg_enc target_encoding)
Definition: xml.c:293
Definition: c.h:555

◆ xmlcomment()

Datum xmlcomment ( PG_FUNCTION_ARGS  )

Definition at line 471 of file xml.c.

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

472 {
473 #ifdef USE_LIBXML
474  text *arg = PG_GETARG_TEXT_PP(0);
475  char *argdata = VARDATA_ANY(arg);
476  int len = VARSIZE_ANY_EXHDR(arg);
478  int i;
479 
480  /* check for "--" in string or "-" at the end */
481  for (i = 1; i < len; i++)
482  {
483  if (argdata[i] == '-' && argdata[i - 1] == '-')
484  ereport(ERROR,
485  (errcode(ERRCODE_INVALID_XML_COMMENT),
486  errmsg("invalid XML comment")));
487  }
488  if (len > 0 && argdata[len - 1] == '-')
489  ereport(ERROR,
490  (errcode(ERRCODE_INVALID_XML_COMMENT),
491  errmsg("invalid XML comment")));
492 
493  initStringInfo(&buf);
494  appendStringInfoString(&buf, "<!--");
495  appendStringInfoText(&buf, arg);
496  appendStringInfoString(&buf, "-->");
497 
499 #else
500  NO_XML_SUPPORT();
501  return 0;
502 #endif
503 }
#define NO_XML_SUPPORT()
Definition: xml.c:219
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
int errcode(int sqlerrcode)
Definition: elog.c:610
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define ERROR
Definition: elog.h:43
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:4197
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:67
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define ereport(elevel,...)
Definition: elog.h:144
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int errmsg(const char *fmt,...)
Definition: elog.c:824
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:447
int i
void * arg
Definition: c.h:555

◆ xmlconcat()

xmltype* xmlconcat ( List args)

Definition at line 512 of file xml.c.

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

Referenced by ExecEvalXmlExpr(), and xmlconcat2().

513 {
514 #ifdef USE_LIBXML
515  int global_standalone = 1;
516  xmlChar *global_version = NULL;
517  bool global_version_no_value = false;
519  ListCell *v;
520 
521  initStringInfo(&buf);
522  foreach(v, args)
523  {
525  size_t len;
526  xmlChar *version;
527  int standalone;
528  char *str;
529 
530  len = VARSIZE(x) - VARHDRSZ;
531  str = text_to_cstring((text *) x);
532 
533  parse_xml_decl((xmlChar *) str, &len, &version, NULL, &standalone);
534 
535  if (standalone == 0 && global_standalone == 1)
536  global_standalone = 0;
537  if (standalone < 0)
538  global_standalone = -1;
539 
540  if (!version)
541  global_version_no_value = true;
542  else if (!global_version)
543  global_version = version;
544  else if (xmlStrcmp(version, global_version) != 0)
545  global_version_no_value = true;
546 
547  appendStringInfoString(&buf, str + len);
548  pfree(str);
549  }
550 
551  if (!global_version_no_value || global_standalone >= 0)
552  {
553  StringInfoData buf2;
554 
555  initStringInfo(&buf2);
556 
557  print_xml_decl(&buf2,
558  (!global_version_no_value) ? global_version : NULL,
559  0,
560  global_standalone);
561 
562  appendBinaryStringInfo(&buf2, buf.data, buf.len);
563  buf = buf2;
564  }
565 
566  return stringinfo_to_xmltype(&buf);
567 #else
568  NO_XML_SUPPORT();
569  return NULL;
570 #endif
571 }
#define NO_XML_SUPPORT()
Definition: xml.c:219
#define VARSIZE(PTR)
Definition: postgres.h:303
#define PointerGetDatum(X)
Definition: postgres.h:556
#define VARHDRSZ
Definition: c.h:561
#define DatumGetXmlP(X)
Definition: xml.h:50
void pfree(void *pointer)
Definition: mcxt.c:1056
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:67
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define lfirst(lc)
Definition: pg_list.h:190
char * text_to_cstring(const text *t)
Definition: varlena.c:204
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:447
Definition: c.h:555
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227

◆ xmlconcat2()

Datum xmlconcat2 ( PG_FUNCTION_ARGS  )

Definition at line 578 of file xml.c.

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

579 {
580  if (PG_ARGISNULL(0))
581  {
582  if (PG_ARGISNULL(1))
583  PG_RETURN_NULL();
584  else
586  }
587  else if (PG_ARGISNULL(1))
589  else
591  PG_GETARG_XML_P(1))));
592 }
#define list_make2(x1, x2)
Definition: pg_list.h:229
xmltype * xmlconcat(List *args)
Definition: xml.c:512
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
#define PG_GETARG_XML_P(n)
Definition: xml.h:53
#define PG_ARGISNULL(n)
Definition: fmgr.h:204
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ xmldata_root_element_end()

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

Definition at line 2650 of file xml.c.

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

2651 {
2652  appendStringInfo(result, "</%s>\n", eltname);
2653 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91

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

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

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

◆ xmlelement()

xmltype* xmlelement ( XmlExpr xexpr,
Datum named_argvalue,
bool named_argnull,
Datum argvalue,
bool argnull 
)

Definition at line 628 of file xml.c.

References arg, XmlExpr::arg_names, XmlExpr::args, 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().

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

◆ xmlexists()

Datum xmlexists ( PG_FUNCTION_ARGS  )

Definition at line 4190 of file xml.c.

References NO_XML_SUPPORT, PG_GETARG_TEXT_PP, PG_GETARG_XML_P, and PG_RETURN_BOOL.

4191 {
4192 #ifdef USE_LIBXML
4193  text *xpath_expr_text = PG_GETARG_TEXT_PP(0);
4194  xmltype *data = PG_GETARG_XML_P(1);
4195  int res_nitems;
4196 
4197  xpath_internal(xpath_expr_text, data, NULL,
4198  &res_nitems, NULL);
4199 
4200  PG_RETURN_BOOL(res_nitems > 0);
4201 #else
4202  NO_XML_SUPPORT();
4203  return 0;
4204 #endif
4205 }
#define NO_XML_SUPPORT()
Definition: xml.c:219
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define PG_GETARG_XML_P(n)
Definition: xml.h:53
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
Definition: c.h:555

◆ xmlparse()

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

Definition at line 752 of file xml.c.

References GetDatabaseEncoding(), and NO_XML_SUPPORT.

Referenced by ExecEvalXmlExpr(), and texttoxml().

753 {
754 #ifdef USE_LIBXML
755  xmlDocPtr doc;
756 
757  doc = xml_parse(data, xmloption_arg, preserve_whitespace,
759  xmlFreeDoc(doc);
760 
761  return (xmltype *) data;
762 #else
763  NO_XML_SUPPORT();
764  return NULL;
765 #endif
766 }
#define NO_XML_SUPPORT()
Definition: xml.c:219
int GetDatabaseEncoding(void)
Definition: mbutils.c:1151
Definition: c.h:555

◆ xmlpi()

xmltype* xmlpi ( const char *  target,
text arg,
bool  arg_is_null,
bool result_is_null 
)

Definition at line 770 of file xml.c.

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), buf, StringInfoData::data, ereport, errcode(), errdetail(), errmsg(), ERROR, initStringInfo(), NO_XML_SUPPORT, pfree(), pg_strcasecmp(), stringinfo_to_xmltype(), and text_to_cstring().

Referenced by ExecEvalXmlExpr().

771 {
772 #ifdef USE_LIBXML
773  xmltype *result;
775 
776  if (pg_strcasecmp(target, "xml") == 0)
777  ereport(ERROR,
778  (errcode(ERRCODE_SYNTAX_ERROR), /* really */
779  errmsg("invalid XML processing instruction"),
780  errdetail("XML processing instruction target name cannot be \"%s\".", target)));
781 
782  /*
783  * Following the SQL standard, the null check comes after the syntax check
784  * above.
785  */
786  *result_is_null = arg_is_null;
787  if (*result_is_null)
788  return NULL;
789 
790  initStringInfo(&buf);
791 
792  appendStringInfo(&buf, "<?%s", target);
793 
794  if (arg != NULL)
795  {
796  char *string;
797 
798  string = text_to_cstring(arg);
799  if (strstr(string, "?>") != NULL)
800  ereport(ERROR,
801  (errcode(ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION),
802  errmsg("invalid XML processing instruction"),
803  errdetail("XML processing instruction cannot contain \"?>\".")));
804 
805  appendStringInfoChar(&buf, ' ');
806  appendStringInfoString(&buf, string + strspn(string, " "));
807  pfree(string);
808  }
809  appendStringInfoString(&buf, "?>");
810 
811  result = stringinfo_to_xmltype(&buf);
812  pfree(buf.data);
813  return result;
814 #else
815  NO_XML_SUPPORT();
816  return NULL;
817 #endif
818 }
#define NO_XML_SUPPORT()
Definition: xml.c:219
int errcode(int sqlerrcode)
Definition: elog.c:610
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
void pfree(void *pointer)
Definition: mcxt.c:1056
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ERROR
Definition: elog.h:43
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:67
int errdetail(const char *fmt,...)
Definition: elog.c:957
char string[11]
Definition: preproc-type.c:46
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define ereport(elevel,...)
Definition: elog.h:144
char * text_to_cstring(const text *t)
Definition: varlena.c:204
int errmsg(const char *fmt,...)
Definition: elog.c:824
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:447
Definition: c.h:555

◆ xmlroot()

xmltype* xmlroot ( xmltype data,
text version,
int  standalone 
)

Definition at line 822 of file xml.c.

References appendStringInfoString(), buf, initStringInfo(), 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().

823 {
824 #ifdef USE_LIBXML
825  char *str;
826  size_t len;
827  xmlChar *orig_version;
828  int orig_standalone;
830 
831  len = VARSIZE(data) - VARHDRSZ;
832  str = text_to_cstring((text *) data);
833 
834  parse_xml_decl((xmlChar *) str, &len, &orig_version, NULL, &orig_standalone);
835 
836  if (version)
837  orig_version = xml_text2xmlChar(version);
838  else
839  orig_version = NULL;
840 
841  switch (standalone)
842  {
843  case XML_STANDALONE_YES:
844  orig_standalone = 1;
845  break;
846  case XML_STANDALONE_NO:
847  orig_standalone = 0;
848  break;
850  orig_standalone = -1;
851  break;
853  /* leave original value */
854  break;
855  }
856 
857  initStringInfo(&buf);
858  print_xml_decl(&buf, orig_version, 0, orig_standalone);
859  appendStringInfoString(&buf, str + len);
860 
861  return stringinfo_to_xmltype(&buf);
862 #else
863  NO_XML_SUPPORT();
864  return NULL;
865 #endif
866 }
#define NO_XML_SUPPORT()
Definition: xml.c:219
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:561
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:67
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
char * text_to_cstring(const text *t)
Definition: varlena.c:204
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:447
Definition: c.h:555

◆ XmlTableDestroyOpaque()

static void XmlTableDestroyOpaque ( struct TableFuncScanState state)
static

Definition at line 4733 of file xml.c.

References i, NO_XML_SUPPORT, TableFuncScanState::opaque, and pg_xml_done().

4734 {
4735 #ifdef USE_LIBXML
4736  XmlTableBuilderData *xtCxt;
4737 
4738  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableDestroyOpaque");
4739 
4740  /* Propagate context related error context to libxml2 */
4741  xmlSetStructuredErrorFunc((void *) xtCxt->xmlerrcxt, xml_errorHandler);
4742 
4743  if (xtCxt->xpathscomp != NULL)
4744  {
4745  int i;
4746 
4747  for (i = 0; i < xtCxt->natts; i++)
4748  if (xtCxt->xpathscomp[i] != NULL)
4749  xmlXPathFreeCompExpr(xtCxt->xpathscomp[i]);
4750  }
4751 
4752  if (xtCxt->xpathobj != NULL)
4753  xmlXPathFreeObject(xtCxt->xpathobj);
4754  if (xtCxt->xpathcomp != NULL)
4755  xmlXPathFreeCompExpr(xtCxt->xpathcomp);
4756  if (xtCxt->xpathcxt != NULL)
4757  xmlXPathFreeContext(xtCxt->xpathcxt);
4758  if (xtCxt->doc != NULL)
4759  xmlFreeDoc(xtCxt->doc);
4760  if (xtCxt->ctxt != NULL)
4761  xmlFreeParserCtxt(xtCxt->ctxt);
4762 
4763  pg_xml_done(xtCxt->xmlerrcxt, true);
4764 
4765  /* not valid anymore */
4766  xtCxt->magic = 0;
4767  state->opaque = NULL;
4768 
4769 #else
4770  NO_XML_SUPPORT();
4771 #endif /* not USE_LIBXML */
4772 }
#define NO_XML_SUPPORT()
Definition: xml.c:219
void pg_xml_done(PgXmlErrorContext *errcxt, bool isError)
int i

◆ XmlTableFetchRow()

static bool XmlTableFetchRow ( struct TableFuncScanState state)
static

Definition at line 4530 of file xml.c.

References ERROR, NO_XML_SUPPORT, and xml_ereport().

4531 {
4532 #ifdef USE_LIBXML
4533  XmlTableBuilderData *xtCxt;
4534 
4535  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableFetchRow");
4536 
4537  /*
4538  * XmlTable returns table - set of composite values. The error context, is
4539  * used for producement more values, between two calls, there can be
4540  * created and used another libxml2 error context. It is libxml2 global
4541  * value, so it should be refreshed any time before any libxml2 usage,
4542  * that is finished by returning some value.
4543  */
4544  xmlSetStructuredErrorFunc((void *) xtCxt->xmlerrcxt, xml_errorHandler);
4545 
4546  if (xtCxt->xpathobj == NULL)
4547  {
4548  xtCxt->xpathobj = xmlXPathCompiledEval(xtCxt->xpathcomp, xtCxt->xpathcxt);
4549  if (xtCxt->xpathobj == NULL || xtCxt->xmlerrcxt->err_occurred)
4550  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
4551  "could not create XPath object");
4552 
4553  xtCxt->row_count = 0;
4554  }
4555 
4556  if (xtCxt->xpathobj->type == XPATH_NODESET)
4557  {
4558  if (xtCxt->xpathobj->nodesetval != NULL)
4559  {
4560  if (xtCxt->row_count++ < xtCxt->xpathobj->nodesetval->nodeNr)
4561  return true;
4562  }
4563  }
4564 
4565  return false;
4566 #else
4567  NO_XML_SUPPORT();
4568  return false;
4569 #endif /* not USE_LIBXML */
4570 }
#define NO_XML_SUPPORT()
Definition: xml.c:219
#define ERROR
Definition: elog.h:43
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)

◆ XmlTableGetValue()

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

Definition at line 4581 of file xml.c.

References appendStringInfoText(), Assert, cur, StringInfoData::data, elog, ereport, errcode(), errmsg(), ERROR, escape_xml(), get_type_category_preferred(), i, TableFuncScanState::in_functions, initStringInfo(), InputFunctionCall(), NO_XML_SUPPORT, PG_END_TRY, PG_FINALLY, PG_TRY, generate_unaccent_rules::str, TableFuncScanState::typioparams, and xml_ereport().

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

◆ XmlTableInitOpaque()

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

Definition at line 4338 of file xml.c.

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

4339 {
4340 #ifdef USE_LIBXML
4341  volatile xmlParserCtxtPtr ctxt = NULL;
4342  XmlTableBuilderData *xtCxt;
4343  PgXmlErrorContext *xmlerrcxt;
4344 
4345  xtCxt = palloc0(sizeof(XmlTableBuilderData));
4346  xtCxt->magic = XMLTABLE_CONTEXT_MAGIC;
4347  xtCxt->natts = natts;
4348  xtCxt->xpathscomp = palloc0(sizeof(xmlXPathCompExprPtr) * natts);
4349 
4350  xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
4351 
4352  PG_TRY();
4353  {
4354  xmlInitParser();
4355 
4356  ctxt = xmlNewParserCtxt();
4357  if (ctxt == NULL || xmlerrcxt->err_occurred)
4358  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
4359  "could not allocate parser context");
4360  }
4361  PG_CATCH();
4362  {
4363  if (ctxt != NULL)
4364  xmlFreeParserCtxt(ctxt);
4365 
4366  pg_xml_done(xmlerrcxt, true);
4367 
4368  PG_RE_THROW();
4369  }
4370  PG_END_TRY();
4371 
4372  xtCxt->xmlerrcxt = xmlerrcxt;
4373  xtCxt->ctxt = ctxt;
4374 
4375  state->opaque = xtCxt;
4376 #else
4377  NO_XML_SUPPORT();
4378 #endif /* not USE_LIBXML */
4379 }
#define NO_XML_SUPPORT()
Definition: xml.c:219
struct PgXmlErrorContext PgXmlErrorContext
Definition: xml.h:48
#define ERROR
Definition: elog.h:43
void pg_xml_done(PgXmlErrorContext *errcxt, bool isError)
void * palloc0(Size size)
Definition: mcxt.c:980
#define PG_CATCH()
Definition: elog.h:305
#define PG_RE_THROW()
Definition: elog.h:336
#define PG_TRY()
Definition: elog.h:295
PgXmlErrorContext * pg_xml_init(PgXmlStrictness strictness)
#define PG_END_TRY()
Definition: elog.h:320
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)

◆ XmlTableSetColumnFilter()

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

Definition at line 4498 of file xml.c.

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

4499 {
4500 #ifdef USE_LIBXML
4501  XmlTableBuilderData *xtCxt;
4502  xmlChar *xstr;
4503 
4504  AssertArg(PointerIsValid(path));
4505 
4506  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetColumnFilter");
4507 
4508  if (*path == '\0')
4509  ereport(ERROR,
4510  (errcode(ERRCODE_DATA_EXCEPTION),
4511  errmsg("column path filter must not be empty string")));
4512 
4513  xstr = pg_xmlCharStrndup(path, strlen(path));
4514 
4515  xtCxt->xpathscomp[colnum] = xmlXPathCompile(xstr);
4516  if (xtCxt->xpathscomp[colnum] == NULL || xtCxt->xmlerrcxt->err_occurred)
4517  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_DATA_EXCEPTION,
4518  "invalid XPath expression");
4519 #else
4520  NO_XML_SUPPORT();
4521 #endif /* not USE_LIBXML */
4522 }
#define NO_XML_SUPPORT()
Definition: xml.c:219
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
#define AssertArg(condition)
Definition: c.h:740
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define PointerIsValid(pointer)
Definition: c.h:632
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)

◆ XmlTableSetDocument()

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

Definition at line 4386 of file xml.c.

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

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

◆ XmlTableSetNamespace()

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

Definition at line 4443 of file xml.c.

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

4444 {
4445 #ifdef USE_LIBXML
4446  XmlTableBuilderData *xtCxt;
4447 
4448  if (name == NULL)
4449  ereport(ERROR,
4450  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4451  errmsg("DEFAULT namespace is not supported")));
4452  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetNamespace");
4453 
4454  if (xmlXPathRegisterNs(xtCxt->xpathcxt,
4455  pg_xmlCharStrndup(name, strlen(name)),
4456  pg_xmlCharStrndup(uri, strlen(uri))))
4457  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_DATA_EXCEPTION,
4458  "could not set XML namespace");
4459 #else
4460  NO_XML_SUPPORT();
4461 #endif /* not USE_LIBXML */
4462 }
#define NO_XML_SUPPORT()
Definition: xml.c:219
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:144
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:824
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)

◆ XmlTableSetRowFilter()

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

Definition at line 4469 of file xml.c.

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

4470 {
4471 #ifdef USE_LIBXML
4472  XmlTableBuilderData *xtCxt;
4473  xmlChar *xstr;
4474 
4475  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetRowFilter");
4476 
4477  if (*path == '\0')
4478  ereport(ERROR,
4479  (errcode(ERRCODE_DATA_EXCEPTION),
4480  errmsg("row path filter must not be empty string")));
4481 
4482  xstr = pg_xmlCharStrndup(path, strlen(path));
4483 
4484  xtCxt->xpathcomp = xmlXPathCompile(xstr);
4485  if (xtCxt->xpathcomp == NULL || xtCxt->xmlerrcxt->err_occurred)
4486  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_SYNTAX_ERROR,
4487  "invalid XPath expression");
4488 #else
4489  NO_XML_SUPPORT();
4490 #endif /* not USE_LIBXML */
4491 }
#define NO_XML_SUPPORT()
Definition: xml.c:219
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)

◆ xmltotext()

Datum xmltotext ( PG_FUNCTION_ARGS  )

Definition at line 605 of file xml.c.

References PG_GETARG_XML_P, and PG_RETURN_TEXT_P.

606 {
607  xmltype *data = PG_GETARG_XML_P(0);
608 
609  /* It's actually binary compatible. */
610  PG_RETURN_TEXT_P((text *) data);
611 }
#define PG_GETARG_XML_P(n)
Definition: xml.h:53
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
Definition: c.h:555

◆ xmltotext_with_xmloption()

text* xmltotext_with_xmloption ( xmltype data,
XmlOptionType  xmloption_arg 
)

Definition at line 615 of file xml.c.

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

Referenced by ExecEvalXmlExpr().

616 {
617  if (xmloption_arg == XMLOPTION_DOCUMENT && !xml_is_document(data))
618  ereport(ERROR,
619  (errcode(ERRCODE_NOT_AN_XML_DOCUMENT),
620  errmsg("not an XML document")));
621 
622  /* It's actually binary compatible, save for the above check. */
623  return (text *) data;
624 }
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
bool xml_is_document(xmltype *arg)
Definition: xml.c:888
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
Definition: c.h:555

◆ xmlvalidate()

Datum xmlvalidate ( PG_FUNCTION_ARGS  )

Definition at line 878 of file xml.c.

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

879 {
880  ereport(ERROR,
881  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
882  errmsg("xmlvalidate is not implemented")));
883  return 0;
884 }
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ xpath()

Datum xpath ( PG_FUNCTION_ARGS  )

Definition at line 4167 of file xml.c.

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

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

4168 {
4169 #ifdef USE_LIBXML
4170  text *xpath_expr_text = PG_GETARG_TEXT_PP(0);
4171  xmltype *data = PG_GETARG_XML_P(1);
4172  ArrayType *namespaces = PG_GETARG_ARRAYTYPE_P(2);
4173  ArrayBuildState *astate;
4174 
4175  astate = initArrayResult(XMLOID, CurrentMemoryContext, true);
4176  xpath_internal(xpath_expr_text, data, namespaces,
4177  NULL, astate);
4179 #else
4180  NO_XML_SUPPORT();
4181  return 0;
4182 #endif
4183 }
#define NO_XML_SUPPORT()
Definition: xml.c:219
ArrayBuildState * initArrayResult(Oid element_type, MemoryContext rcontext, bool subcontext)
Definition: arrayfuncs.c:5014
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:253
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5117
#define PG_GETARG_XML_P(n)
Definition: xml.h:53
Definition: c.h:555

◆ xpath_exists()

Datum xpath_exists ( PG_FUNCTION_ARGS  )

Definition at line 4213 of file xml.c.

References FlushErrorState(), GetDatabaseEncoding(), NO_XML_SUPPORT, PG_CATCH, PG_END_TRY, PG_GETARG_ARRAYTYPE_P, PG_GETARG_TEXT_PP, PG_GETARG_XML_P, PG_RETURN_BOOL, and PG_TRY.

4214 {
4215 #ifdef USE_LIBXML
4216  text *xpath_expr_text = PG_GETARG_TEXT_PP(0);
4217  xmltype *data = PG_GETARG_XML_P(1);
4218  ArrayType *namespaces = PG_GETARG_ARRAYTYPE_P(2);
4219  int res_nitems;
4220 
4221  xpath_internal(xpath_expr_text, data, namespaces,
4222  &res_nitems, NULL);
4223 
4224  PG_RETURN_BOOL(res_nitems > 0);
4225 #else
4226  NO_XML_SUPPORT();
4227  return 0;
4228 #endif
4229 }
#define NO_XML_SUPPORT()
Definition: xml.c:219
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
#define PG_GETARG_XML_P(n)
Definition: xml.h:53
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
Definition: c.h:555

◆ xsd_schema_element_end()

static void xsd_schema_element_end ( StringInfo  result)
static

Definition at line 2915 of file xml.c.

References appendStringInfoString().

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

2916 {
2917  appendStringInfoString(result, "</xsd:schema>");
2918 }
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176

◆ xsd_schema_element_start()

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

Definition at line 2898 of file xml.c.

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

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

2899 {
2900  appendStringInfoString(result,
2901  "<xsd:schema\n"
2902  " xmlns:xsd=\"" NAMESPACE_XSD "\"");
2903  if (strlen(targetns) > 0)
2904  appendStringInfo(result,
2905  "\n"
2906  " targetNamespace=\"%s\"\n"
2907  " elementFormDefault=\"qualified\"",
2908  targetns);
2909  appendStringInfoString(result,
2910  ">\n\n");
2911 }
#define NAMESPACE_XSD
Definition: xml.c:228
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176

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:4338
static bool XmlTableFetchRow(struct TableFuncScanState *state)
Definition: xml.c:4530
static void XmlTableSetNamespace(struct TableFuncScanState *state, const char *name, const char *uri)
Definition: xml.c:4443
static void XmlTableSetDocument(struct TableFuncScanState *state, Datum value)
Definition: xml.c:4386
static void XmlTableSetColumnFilter(struct TableFuncScanState *state, const char *path, int colnum)
Definition: xml.c:4498
static Datum XmlTableGetValue(struct TableFuncScanState *state, int colnum, Oid typid, int32 typmod, bool *isnull)
Definition: xml.c:4581
static void XmlTableSetRowFilter(struct TableFuncScanState *state, const char *path)
Definition: xml.c:4469
static void XmlTableDestroyOpaque(struct TableFuncScanState *state)
Definition: xml.c:4733

Definition at line 207 of file xml.c.

Referenced by ExecInitTableFuncScan().