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

Go to the source code of this file.

Macros

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

Functions

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

Variables

int xmlbinary = XMLBINARY_BASE64
 
int xmloption = XMLOPTION_CONTENT
 
const TableFuncRoutine XmlTableRoutine
 

Macro Definition Documentation

◆ NAMESPACE_SQLXML

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

Definition at line 245 of file xml.c.

◆ NAMESPACE_XSD

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

Definition at line 243 of file xml.c.

◆ NAMESPACE_XSI

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

Definition at line 244 of file xml.c.

◆ NO_XML_SUPPORT

#define NO_XML_SUPPORT ( )
Value:
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
errmsg("unsupported XML feature"), \
errdetail("This functionality requires the server to be built with libxml support.")))
int errdetail(const char *fmt,...)
Definition: elog.c:1204
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149

Definition at line 235 of file xml.c.

◆ PG_XML_DEFAULT_VERSION

#define PG_XML_DEFAULT_VERSION   "1.0"

Definition at line 301 of file xml.c.

◆ XML_VISIBLE_SCHEMAS

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

Definition at line 2849 of file xml.c.

◆ XML_VISIBLE_SCHEMAS_EXCLUDE

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

Definition at line 2847 of file xml.c.

Function Documentation

◆ _SPI_strdup()

static char * _SPI_strdup ( const char *  s)
static

Definition at line 2740 of file xml.c.

2741{
2742 size_t len = strlen(s) + 1;
2743 char *ret = SPI_palloc(len);
2744
2745 memcpy(ret, s, len);
2746 return ret;
2747}
const void size_t len
void * SPI_palloc(Size size)
Definition: spi.c:1338

References len, and SPI_palloc().

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

◆ cstring_to_xmltype()

static xmltype * cstring_to_xmltype ( const char *  string)
static

Definition at line 474 of file xml.c.

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

References cstring_to_text().

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

◆ cursor_to_xml()

Datum cursor_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 2924 of file xml.c.

2925{
2927 int32 count = PG_GETARG_INT32(1);
2928 bool nulls = PG_GETARG_BOOL(2);
2929 bool tableforest = PG_GETARG_BOOL(3);
2930 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(4));
2931
2932 StringInfoData result;
2933 Portal portal;
2934 uint64 i;
2935
2936 initStringInfo(&result);
2937
2938 if (!tableforest)
2939 {
2940 xmldata_root_element_start(&result, "table", NULL, targetns, true);
2941 appendStringInfoChar(&result, '\n');
2942 }
2943
2944 SPI_connect();
2945 portal = SPI_cursor_find(name);
2946 if (portal == NULL)
2947 ereport(ERROR,
2948 (errcode(ERRCODE_UNDEFINED_CURSOR),
2949 errmsg("cursor \"%s\" does not exist", name)));
2950
2951 SPI_cursor_fetch(portal, true, count);
2952 for (i = 0; i < SPI_processed; i++)
2953 SPI_sql_row_to_xmlelement(i, &result, NULL, nulls,
2954 tableforest, targetns, true);
2955
2956 SPI_finish();
2957
2958 if (!tableforest)
2959 xmldata_root_element_end(&result, "table");
2960
2962}
int32_t int32
Definition: c.h:498
uint64_t uint64
Definition: c.h:503
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
int i
Definition: isn.c:77
uint64 SPI_processed
Definition: spi.c:44
Portal SPI_cursor_find(const char *name)
Definition: spi.c:1794
int SPI_connect(void)
Definition: spi.c:94
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Definition: spi.c:1806
int SPI_finish(void)
Definition: spi.c:182
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:242
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97
char * text_to_cstring(const text *t)
Definition: varlena.c:225
const char * name
static void xmldata_root_element_start(StringInfo result, const char *eltname, const char *xmlschema, const char *targetns, bool top_level)
Definition: xml.c:2978
static void xmldata_root_element_end(StringInfo result, const char *eltname)
Definition: xml.c:3005
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:467
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:4097
#define PG_RETURN_XML_P(x)
Definition: xml.h:63

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

◆ cursor_to_xmlschema()

Datum cursor_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 3105 of file xml.c.

3106{
3108 bool nulls = PG_GETARG_BOOL(1);
3109 bool tableforest = PG_GETARG_BOOL(2);
3110 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
3111 const char *xmlschema;
3112 Portal portal;
3113
3114 SPI_connect();
3115 portal = SPI_cursor_find(name);
3116 if (portal == NULL)
3117 ereport(ERROR,
3118 (errcode(ERRCODE_UNDEFINED_CURSOR),
3119 errmsg("cursor \"%s\" does not exist", name)));
3120 if (portal->tupDesc == NULL)
3121 ereport(ERROR,
3122 (errcode(ERRCODE_INVALID_CURSOR_STATE),
3123 errmsg("portal \"%s\" does not return tuples", name)));
3124
3126 InvalidOid, nulls,
3127 tableforest, targetns));
3128 SPI_finish();
3129
3131}
#define InvalidOid
Definition: postgres_ext.h:35
TupleDesc tupDesc
Definition: portal.h:159
static xmltype * cstring_to_xmltype(const char *string)
Definition: xml.c:474
static char * _SPI_strdup(const char *s)
Definition: xml.c:2740
static const char * map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3527

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

◆ database_get_xml_visible_schemas()

static List * database_get_xml_visible_schemas ( void  )
static

Definition at line 2853 of file xml.c.

2854{
2855 return query_to_oid_list(XML_VISIBLE_SCHEMAS " ORDER BY nspname;");
2856}
#define XML_VISIBLE_SCHEMAS
Definition: xml.c:2849
static List * query_to_oid_list(const char *query)
Definition: xml.c:2797

References query_to_oid_list(), and XML_VISIBLE_SCHEMAS.

Referenced by database_to_xml_internal(), and database_to_xmlschema_internal().

◆ database_get_xml_visible_tables()

static List * database_get_xml_visible_tables ( void  )
static

Definition at line 2860 of file xml.c.

2861{
2862 /* At the moment there is no order required here. */
2863 return query_to_oid_list("SELECT oid FROM pg_catalog.pg_class"
2864 " WHERE relkind IN ("
2865 CppAsString2(RELKIND_RELATION) ","
2866 CppAsString2(RELKIND_MATVIEW) ","
2867 CppAsString2(RELKIND_VIEW) ")"
2868 " AND pg_catalog.has_table_privilege(pg_class.oid, 'SELECT')"
2869 " AND relnamespace IN (" XML_VISIBLE_SCHEMAS ");");
2870}
#define CppAsString2(x)
Definition: c.h:363

References CppAsString2, query_to_oid_list(), and XML_VISIBLE_SCHEMAS.

Referenced by database_to_xmlschema_internal().

◆ database_to_xml()

Datum database_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 3410 of file xml.c.

3411{
3412 bool nulls = PG_GETARG_BOOL(0);
3413 bool tableforest = PG_GETARG_BOOL(1);
3414 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
3415
3417 tableforest, targetns)));
3418}
static StringInfo database_to_xml_internal(const char *xmlschema, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3367

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

◆ database_to_xml_and_xmlschema()

Datum database_to_xml_and_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 3477 of file xml.c.

3478{
3479 bool nulls = PG_GETARG_BOOL(0);
3480 bool tableforest = PG_GETARG_BOOL(1);
3481 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
3482 StringInfo xmlschema;
3483
3484 xmlschema = database_to_xmlschema_internal(nulls, tableforest, targetns);
3485
3487 nulls, tableforest, targetns)));
3488}
static StringInfo database_to_xmlschema_internal(bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3422

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

◆ database_to_xml_internal()

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

Definition at line 3367 of file xml.c.

3369{
3370 StringInfo result;
3371 List *nspid_list;
3372 ListCell *cell;
3373 char *xmlcn;
3374
3376 true, false);
3377 result = makeStringInfo();
3378
3379 xmldata_root_element_start(result, xmlcn, xmlschema, targetns, true);
3380 appendStringInfoChar(result, '\n');
3381
3382 if (xmlschema)
3383 appendStringInfo(result, "%s\n\n", xmlschema);
3384
3385 SPI_connect();
3386
3387 nspid_list = database_get_xml_visible_schemas();
3388
3389 foreach(cell, nspid_list)
3390 {
3391 Oid nspid = lfirst_oid(cell);
3392 StringInfo subres;
3393
3394 subres = schema_to_xml_internal(nspid, NULL, nulls,
3395 tableforest, targetns, false);
3396
3397 appendBinaryStringInfo(result, subres->data, subres->len);
3398 appendStringInfoChar(result, '\n');
3399 }
3400
3401 SPI_finish();
3402
3403 xmldata_root_element_end(result, xmlcn);
3404
3405 return result;
3406}
int nspid
char * get_database_name(Oid dbid)
Definition: dbcommands.c:3188
Oid MyDatabaseId
Definition: globals.c:94
#define lfirst_oid(lc)
Definition: pg_list.h:174
unsigned int Oid
Definition: postgres_ext.h:30
StringInfo makeStringInfo(void)
Definition: stringinfo.c:72
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:145
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
Definition: stringinfo.c:281
Definition: pg_list.h:54
char * map_sql_identifier_to_xml_name(const char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:2391
static List * database_get_xml_visible_schemas(void)
Definition: xml.c:2853
static StringInfo schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:3192

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

Referenced by database_to_xml(), and database_to_xml_and_xmlschema().

◆ database_to_xmlschema()

Datum database_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 3465 of file xml.c.

3466{
3467 bool nulls = PG_GETARG_BOOL(0);
3468 bool tableforest = PG_GETARG_BOOL(1);
3469 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
3470
3472 tableforest, targetns)));
3473}

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

◆ database_to_xmlschema_internal()

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

Definition at line 3422 of file xml.c.

3424{
3425 List *relid_list;
3426 List *nspid_list;
3427 List *tupdesc_list;
3428 ListCell *cell;
3429 StringInfo result;
3430
3431 result = makeStringInfo();
3432
3433 xsd_schema_element_start(result, targetns);
3434
3435 SPI_connect();
3436
3437 relid_list = database_get_xml_visible_tables();
3438 nspid_list = database_get_xml_visible_schemas();
3439
3440 tupdesc_list = NIL;
3441 foreach(cell, relid_list)
3442 {
3443 Relation rel;
3444
3446 tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
3447 table_close(rel, NoLock);
3448 }
3449
3452
3454 map_sql_catalog_to_xmlschema_types(nspid_list, nulls, tableforest, targetns));
3455
3456 xsd_schema_element_end(result);
3457
3458 SPI_finish();
3459
3460 return result;
3461}
List * lappend(List *list, void *datum)
Definition: list.c:339
#define NoLock
Definition: lockdefs.h:34
#define AccessShareLock
Definition: lockdefs.h:36
#define NIL
Definition: pg_list.h:68
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:230
TupleDesc rd_att
Definition: rel.h:112
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:245
static const char * map_sql_catalog_to_xmlschema_types(List *nspid_list, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3705
static void xsd_schema_element_start(StringInfo result, const char *targetns)
Definition: xml.c:3257
static List * database_get_xml_visible_tables(void)
Definition: xml.c:2860
static const char * map_sql_typecoll_to_xmlschema_types(List *tupdesc_list)
Definition: xml.c:3867
static void xsd_schema_element_end(StringInfo result)
Definition: xml.c:3274

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

Referenced by database_to_xml_and_xmlschema(), and database_to_xmlschema().

◆ escape_xml()

char * escape_xml ( const char *  str)

Definition at line 2708 of file xml.c.

2709{
2711 const char *p;
2712
2714 for (p = str; *p; p++)
2715 {
2716 switch (*p)
2717 {
2718 case '&':
2719 appendStringInfoString(&buf, "&amp;");
2720 break;
2721 case '<':
2722 appendStringInfoString(&buf, "&lt;");
2723 break;
2724 case '>':
2725 appendStringInfoString(&buf, "&gt;");
2726 break;
2727 case '\r':
2728 appendStringInfoString(&buf, "&#x0d;");
2729 break;
2730 default:
2732 break;
2733 }
2734 }
2735 return buf.data;
2736}
const char * str
static char * buf
Definition: pg_test_fsync.c:72
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:231

References appendStringInfoCharMacro, appendStringInfoString(), buf, initStringInfo(), and str.

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

◆ map_multipart_sql_identifier_to_xml_name()

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

Definition at line 3496 of file xml.c.

3497{
3498 StringInfoData result;
3499
3500 initStringInfo(&result);
3501
3502 if (a)
3503 appendStringInfoString(&result,
3504 map_sql_identifier_to_xml_name(a, true, true));
3505 if (b)
3506 appendStringInfo(&result, ".%s",
3507 map_sql_identifier_to_xml_name(b, true, true));
3508 if (c)
3509 appendStringInfo(&result, ".%s",
3510 map_sql_identifier_to_xml_name(c, true, true));
3511 if (d)
3512 appendStringInfo(&result, ".%s",
3513 map_sql_identifier_to_xml_name(d, true, true));
3514
3515 return result.data;
3516}
int b
Definition: isn.c:74
int a
Definition: isn.c:73
char * c

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

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

◆ map_sql_catalog_to_xmlschema_types()

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

Definition at line 3705 of file xml.c.

3707{
3708 char *dbname;
3709 char *xmlcn;
3710 char *catalogtypename;
3711 StringInfoData result;
3712 ListCell *cell;
3713
3715
3716 initStringInfo(&result);
3717
3718 xmlcn = map_sql_identifier_to_xml_name(dbname, true, false);
3719
3720 catalogtypename = map_multipart_sql_identifier_to_xml_name("CatalogType",
3721 dbname,
3722 NULL,
3723 NULL);
3724
3725 appendStringInfo(&result,
3726 "<xsd:complexType name=\"%s\">\n", catalogtypename);
3727 appendStringInfoString(&result,
3728 " <xsd:all>\n");
3729
3730 foreach(cell, nspid_list)
3731 {
3732 Oid nspid = lfirst_oid(cell);
3733 char *nspname = get_namespace_name(nspid);
3734 char *xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
3735 char *schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
3736 dbname,
3737 nspname,
3738 NULL);
3739
3740 appendStringInfo(&result,
3741 " <xsd:element name=\"%s\" type=\"%s\"/>\n",
3742 xmlsn, schematypename);
3743 }
3744
3745 appendStringInfoString(&result,
3746 " </xsd:all>\n");
3747 appendStringInfoString(&result,
3748 "</xsd:complexType>\n\n");
3749
3750 appendStringInfo(&result,
3751 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3752 xmlcn, catalogtypename);
3753
3754 return result.data;
3755}
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3506
char * dbname
Definition: streamutil.c:49
static char * map_multipart_sql_identifier_to_xml_name(const char *a, const char *b, const char *c, const char *d)
Definition: xml.c:3496

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

Referenced by database_to_xmlschema_internal().

◆ map_sql_identifier_to_xml_name()

char * map_sql_identifier_to_xml_name ( const char *  ident,
bool  fully_escaped,
bool  escape_period 
)

Definition at line 2391 of file xml.c.

2393{
2394#ifdef USE_LIBXML
2396 const char *p;
2397
2398 /*
2399 * SQL/XML doesn't make use of this case anywhere, so it's probably a
2400 * mistake.
2401 */
2402 Assert(fully_escaped || !escape_period);
2403
2405
2406 for (p = ident; *p; p += pg_mblen(p))
2407 {
2408 if (*p == ':' && (p == ident || fully_escaped))
2409 appendStringInfoString(&buf, "_x003A_");
2410 else if (*p == '_' && *(p + 1) == 'x')
2411 appendStringInfoString(&buf, "_x005F_");
2412 else if (fully_escaped && p == ident &&
2413 pg_strncasecmp(p, "xml", 3) == 0)
2414 {
2415 if (*p == 'x')
2416 appendStringInfoString(&buf, "_x0078_");
2417 else
2418 appendStringInfoString(&buf, "_x0058_");
2419 }
2420 else if (escape_period && *p == '.')
2421 appendStringInfoString(&buf, "_x002E_");
2422 else
2423 {
2424 pg_wchar u = sqlchar_to_unicode(p);
2425
2426 if ((p == ident)
2427 ? !is_valid_xml_namefirst(u)
2428 : !is_valid_xml_namechar(u))
2429 appendStringInfo(&buf, "_x%04X_", (unsigned int) u);
2430 else
2432 }
2433 }
2434
2435 return buf.data;
2436#else /* not USE_LIBXML */
2438 return NULL;
2439#endif /* not USE_LIBXML */
2440}
Assert(PointerIsAligned(start, uint64))
#define ident
Definition: indent_codes.h:47
unsigned int pg_wchar
Definition: mbprint.c:31
int pg_mblen(const char *mbstr)
Definition: mbutils.c:1024
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
#define NO_XML_SUPPORT()
Definition: xml.c:235

References appendBinaryStringInfo(), appendStringInfo(), appendStringInfoString(), Assert(), buf, ident, initStringInfo(), NO_XML_SUPPORT, pg_mblen(), and pg_strncasecmp().

Referenced by database_to_xml_internal(), map_multipart_sql_identifier_to_xml_name(), map_sql_catalog_to_xmlschema_types(), map_sql_schema_to_xmlschema_types(), map_sql_table_to_xmlschema(), query_to_xml_internal(), schema_to_xml_internal(), SPI_sql_row_to_xmlelement(), and transformXmlExpr().

◆ map_sql_schema_to_xmlschema_types()

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

Definition at line 3632 of file xml.c.

3634{
3635 char *dbname;
3636 char *nspname;
3637 char *xmlsn;
3638 char *schematypename;
3639 StringInfoData result;
3640 ListCell *cell;
3641
3643 nspname = get_namespace_name(nspid);
3644
3645 initStringInfo(&result);
3646
3647 xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
3648
3649 schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
3650 dbname,
3651 nspname,
3652 NULL);
3653
3654 appendStringInfo(&result,
3655 "<xsd:complexType name=\"%s\">\n", schematypename);
3656 if (!tableforest)
3657 appendStringInfoString(&result,
3658 " <xsd:all>\n");
3659 else
3660 appendStringInfoString(&result,
3661 " <xsd:sequence>\n");
3662
3663 foreach(cell, relid_list)
3664 {
3665 Oid relid = lfirst_oid(cell);
3666 char *relname = get_rel_name(relid);
3667 char *xmltn = map_sql_identifier_to_xml_name(relname, true, false);
3668 char *tabletypename = map_multipart_sql_identifier_to_xml_name(tableforest ? "RowType" : "TableType",
3669 dbname,
3670 nspname,
3671 relname);
3672
3673 if (!tableforest)
3674 appendStringInfo(&result,
3675 " <xsd:element name=\"%s\" type=\"%s\"/>\n",
3676 xmltn, tabletypename);
3677 else
3678 appendStringInfo(&result,
3679 " <xsd:element name=\"%s\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n",
3680 xmltn, tabletypename);
3681 }
3682
3683 if (!tableforest)
3684 appendStringInfoString(&result,
3685 " </xsd:all>\n");
3686 else
3687 appendStringInfoString(&result,
3688 " </xsd:sequence>\n");
3689 appendStringInfoString(&result,
3690 "</xsd:complexType>\n\n");
3691
3692 appendStringInfo(&result,
3693 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3694 xmlsn, schematypename);
3695
3696 return result.data;
3697}
char * get_rel_name(Oid relid)
Definition: lsyscache.c:2068
NameData relname
Definition: pg_class.h:38

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

Referenced by schema_to_xmlschema_internal().

◆ map_sql_table_to_xmlschema()

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

Definition at line 3527 of file xml.c.

3529{
3530 int i;
3531 char *xmltn;
3532 char *tabletypename;
3533 char *rowtypename;
3534 StringInfoData result;
3535
3536 initStringInfo(&result);
3537
3538 if (OidIsValid(relid))
3539 {
3540 HeapTuple tuple;
3541 Form_pg_class reltuple;
3542
3543 tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
3544 if (!HeapTupleIsValid(tuple))
3545 elog(ERROR, "cache lookup failed for relation %u", relid);
3546 reltuple = (Form_pg_class) GETSTRUCT(tuple);
3547
3548 xmltn = map_sql_identifier_to_xml_name(NameStr(reltuple->relname),
3549 true, false);
3550
3551 tabletypename = map_multipart_sql_identifier_to_xml_name("TableType",
3553 get_namespace_name(reltuple->relnamespace),
3554 NameStr(reltuple->relname));
3555
3556 rowtypename = map_multipart_sql_identifier_to_xml_name("RowType",
3558 get_namespace_name(reltuple->relnamespace),
3559 NameStr(reltuple->relname));
3560
3561 ReleaseSysCache(tuple);
3562 }
3563 else
3564 {
3565 if (tableforest)
3566 xmltn = "row";
3567 else
3568 xmltn = "table";
3569
3570 tabletypename = "TableType";
3571 rowtypename = "RowType";
3572 }
3573
3574 xsd_schema_element_start(&result, targetns);
3575
3576 appendStringInfoString(&result,
3578
3579 appendStringInfo(&result,
3580 "<xsd:complexType name=\"%s\">\n"
3581 " <xsd:sequence>\n",
3582 rowtypename);
3583
3584 for (i = 0; i < tupdesc->natts; i++)
3585 {
3586 Form_pg_attribute att = TupleDescAttr(tupdesc, i);
3587
3588 if (att->attisdropped)
3589 continue;
3590 appendStringInfo(&result,
3591 " <xsd:element name=\"%s\" type=\"%s\"%s></xsd:element>\n",
3593 true, false),
3594 map_sql_type_to_xml_name(att->atttypid, -1),
3595 nulls ? " nillable=\"true\"" : " minOccurs=\"0\"");
3596 }
3597
3598 appendStringInfoString(&result,
3599 " </xsd:sequence>\n"
3600 "</xsd:complexType>\n\n");
3601
3602 if (!tableforest)
3603 {
3604 appendStringInfo(&result,
3605 "<xsd:complexType name=\"%s\">\n"
3606 " <xsd:sequence>\n"
3607 " <xsd:element name=\"row\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n"
3608 " </xsd:sequence>\n"
3609 "</xsd:complexType>\n\n",
3610 tabletypename, rowtypename);
3611
3612 appendStringInfo(&result,
3613 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3614 xmltn, tabletypename);
3615 }
3616 else
3617 appendStringInfo(&result,
3618 "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3619 xmltn, rowtypename);
3620
3621 xsd_schema_element_end(&result);
3622
3623 return result.data;
3624}
#define NameStr(name)
Definition: c.h:717
#define OidIsValid(objectId)
Definition: c.h:746
#define elog(elevel,...)
Definition: elog.h:225
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:202
FormData_pg_class * Form_pg_class
Definition: pg_class.h:156
#define list_make1(x1)
Definition: pg_list.h:212
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:160
static const char * map_sql_type_to_xml_name(Oid typeoid, int typmod)
Definition: xml.c:3762

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

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

◆ map_sql_type_to_xml_name()

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

Definition at line 3762 of file xml.c.

3763{
3764 StringInfoData result;
3765
3766 initStringInfo(&result);
3767
3768 switch (typeoid)
3769 {
3770 case BPCHAROID:
3771 if (typmod == -1)
3772 appendStringInfoString(&result, "CHAR");
3773 else
3774 appendStringInfo(&result, "CHAR_%d", typmod - VARHDRSZ);
3775 break;
3776 case VARCHAROID:
3777 if (typmod == -1)
3778 appendStringInfoString(&result, "VARCHAR");
3779 else
3780 appendStringInfo(&result, "VARCHAR_%d", typmod - VARHDRSZ);
3781 break;
3782 case NUMERICOID:
3783 if (typmod == -1)
3784 appendStringInfoString(&result, "NUMERIC");
3785 else
3786 appendStringInfo(&result, "NUMERIC_%d_%d",
3787 ((typmod - VARHDRSZ) >> 16) & 0xffff,
3788 (typmod - VARHDRSZ) & 0xffff);
3789 break;
3790 case INT4OID:
3791 appendStringInfoString(&result, "INTEGER");
3792 break;
3793 case INT2OID:
3794 appendStringInfoString(&result, "SMALLINT");
3795 break;
3796 case INT8OID:
3797 appendStringInfoString(&result, "BIGINT");
3798 break;
3799 case FLOAT4OID:
3800 appendStringInfoString(&result, "REAL");
3801 break;
3802 case FLOAT8OID:
3803 appendStringInfoString(&result, "DOUBLE");
3804 break;
3805 case BOOLOID:
3806 appendStringInfoString(&result, "BOOLEAN");
3807 break;
3808 case TIMEOID:
3809 if (typmod == -1)
3810 appendStringInfoString(&result, "TIME");
3811 else
3812 appendStringInfo(&result, "TIME_%d", typmod);
3813 break;
3814 case TIMETZOID:
3815 if (typmod == -1)
3816 appendStringInfoString(&result, "TIME_WTZ");
3817 else
3818 appendStringInfo(&result, "TIME_WTZ_%d", typmod);
3819 break;
3820 case TIMESTAMPOID:
3821 if (typmod == -1)
3822 appendStringInfoString(&result, "TIMESTAMP");
3823 else
3824 appendStringInfo(&result, "TIMESTAMP_%d", typmod);
3825 break;
3826 case TIMESTAMPTZOID:
3827 if (typmod == -1)
3828 appendStringInfoString(&result, "TIMESTAMP_WTZ");
3829 else
3830 appendStringInfo(&result, "TIMESTAMP_WTZ_%d", typmod);
3831 break;
3832 case DATEOID:
3833 appendStringInfoString(&result, "DATE");
3834 break;
3835 case XMLOID:
3836 appendStringInfoString(&result, "XML");
3837 break;
3838 default:
3839 {
3840 HeapTuple tuple;
3841 Form_pg_type typtuple;
3842
3843 tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typeoid));
3844 if (!HeapTupleIsValid(tuple))
3845 elog(ERROR, "cache lookup failed for type %u", typeoid);
3846 typtuple = (Form_pg_type) GETSTRUCT(tuple);
3847
3848 appendStringInfoString(&result,
3849 map_multipart_sql_identifier_to_xml_name((typtuple->typtype == TYPTYPE_DOMAIN) ? "Domain" : "UDT",
3851 get_namespace_name(typtuple->typnamespace),
3852 NameStr(typtuple->typname)));
3853
3854 ReleaseSysCache(tuple);
3855 }
3856 }
3857
3858 return result.data;
3859}
#define VARHDRSZ
Definition: c.h:663
FormData_pg_type * Form_pg_type
Definition: pg_type.h:261

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

Referenced by map_sql_table_to_xmlschema(), and map_sql_type_to_xmlschema_type().

◆ map_sql_type_to_xmlschema_type()

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

Definition at line 3922 of file xml.c.

3923{
3924 StringInfoData result;
3925 const char *typename = map_sql_type_to_xml_name(typeoid, typmod);
3926
3927 initStringInfo(&result);
3928
3929 if (typeoid == XMLOID)
3930 {
3931 appendStringInfoString(&result,
3932 "<xsd:complexType mixed=\"true\">\n"
3933 " <xsd:sequence>\n"
3934 " <xsd:any name=\"element\" minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>\n"
3935 " </xsd:sequence>\n"
3936 "</xsd:complexType>\n");
3937 }
3938 else
3939 {
3940 appendStringInfo(&result,
3941 "<xsd:simpleType name=\"%s\">\n", typename);
3942
3943 switch (typeoid)
3944 {
3945 case BPCHAROID:
3946 case VARCHAROID:
3947 case TEXTOID:
3948 appendStringInfoString(&result,
3949 " <xsd:restriction base=\"xsd:string\">\n");
3950 if (typmod != -1)
3951 appendStringInfo(&result,
3952 " <xsd:maxLength value=\"%d\"/>\n",
3953 typmod - VARHDRSZ);
3954 appendStringInfoString(&result, " </xsd:restriction>\n");
3955 break;
3956
3957 case BYTEAOID:
3958 appendStringInfo(&result,
3959 " <xsd:restriction base=\"xsd:%s\">\n"
3960 " </xsd:restriction>\n",
3961 xmlbinary == XMLBINARY_BASE64 ? "base64Binary" : "hexBinary");
3962 break;
3963
3964 case NUMERICOID:
3965 if (typmod != -1)
3966 appendStringInfo(&result,
3967 " <xsd:restriction base=\"xsd:decimal\">\n"
3968 " <xsd:totalDigits value=\"%d\"/>\n"
3969 " <xsd:fractionDigits value=\"%d\"/>\n"
3970 " </xsd:restriction>\n",
3971 ((typmod - VARHDRSZ) >> 16) & 0xffff,
3972 (typmod - VARHDRSZ) & 0xffff);
3973 break;
3974
3975 case INT2OID:
3976 appendStringInfo(&result,
3977 " <xsd:restriction base=\"xsd:short\">\n"
3978 " <xsd:maxInclusive value=\"%d\"/>\n"
3979 " <xsd:minInclusive value=\"%d\"/>\n"
3980 " </xsd:restriction>\n",
3981 SHRT_MAX, SHRT_MIN);
3982 break;
3983
3984 case INT4OID:
3985 appendStringInfo(&result,
3986 " <xsd:restriction base=\"xsd:int\">\n"
3987 " <xsd:maxInclusive value=\"%d\"/>\n"
3988 " <xsd:minInclusive value=\"%d\"/>\n"
3989 " </xsd:restriction>\n",
3990 INT_MAX, INT_MIN);
3991 break;
3992
3993 case INT8OID:
3994 appendStringInfo(&result,
3995 " <xsd:restriction base=\"xsd:long\">\n"
3996 " <xsd:maxInclusive value=\"" INT64_FORMAT "\"/>\n"
3997 " <xsd:minInclusive value=\"" INT64_FORMAT "\"/>\n"
3998 " </xsd:restriction>\n",
4000 PG_INT64_MIN);
4001 break;
4002
4003 case FLOAT4OID:
4004 appendStringInfoString(&result,
4005 " <xsd:restriction base=\"xsd:float\"></xsd:restriction>\n");
4006 break;
4007
4008 case FLOAT8OID:
4009 appendStringInfoString(&result,
4010 " <xsd:restriction base=\"xsd:double\"></xsd:restriction>\n");
4011 break;
4012
4013 case BOOLOID:
4014 appendStringInfoString(&result,
4015 " <xsd:restriction base=\"xsd:boolean\"></xsd:restriction>\n");
4016 break;
4017
4018 case TIMEOID:
4019 case TIMETZOID:
4020 {
4021 const char *tz = (typeoid == TIMETZOID ? "(\\+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
4022
4023 if (typmod == -1)
4024 appendStringInfo(&result,
4025 " <xsd:restriction base=\"xsd:time\">\n"
4026 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}(.\\p{Nd}+)?%s\"/>\n"
4027 " </xsd:restriction>\n", tz);
4028 else if (typmod == 0)
4029 appendStringInfo(&result,
4030 " <xsd:restriction base=\"xsd:time\">\n"
4031 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
4032 " </xsd:restriction>\n", tz);
4033 else
4034 appendStringInfo(&result,
4035 " <xsd:restriction base=\"xsd:time\">\n"
4036 " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}.\\p{Nd}{%d}%s\"/>\n"
4037 " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
4038 break;
4039 }
4040
4041 case TIMESTAMPOID:
4042 case TIMESTAMPTZOID:
4043 {
4044 const char *tz = (typeoid == TIMESTAMPTZOID ? "(\\+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
4045
4046 if (typmod == -1)
4047 appendStringInfo(&result,
4048 " <xsd:restriction base=\"xsd:dateTime\">\n"
4049 " <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"
4050 " </xsd:restriction>\n", tz);
4051 else if (typmod == 0)
4052 appendStringInfo(&result,
4053 " <xsd:restriction base=\"xsd:dateTime\">\n"
4054 " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
4055 " </xsd:restriction>\n", tz);
4056 else
4057 appendStringInfo(&result,
4058 " <xsd:restriction base=\"xsd:dateTime\">\n"
4059 " <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"
4060 " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
4061 break;
4062 }
4063
4064 case DATEOID:
4065 appendStringInfoString(&result,
4066 " <xsd:restriction base=\"xsd:date\">\n"
4067 " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}\"/>\n"
4068 " </xsd:restriction>\n");
4069 break;
4070
4071 default:
4072 if (get_typtype(typeoid) == TYPTYPE_DOMAIN)
4073 {
4074 Oid base_typeoid;
4075 int32 base_typmod = -1;
4076
4077 base_typeoid = getBaseTypeAndTypmod(typeoid, &base_typmod);
4078
4079 appendStringInfo(&result,
4080 " <xsd:restriction base=\"%s\"/>\n",
4081 map_sql_type_to_xml_name(base_typeoid, base_typmod));
4082 }
4083 break;
4084 }
4085 appendStringInfoString(&result, "</xsd:simpleType>\n");
4086 }
4087
4088 return result.data;
4089}
#define INT64_FORMAT
Definition: c.h:520
#define PG_INT64_MAX
Definition: c.h:563
#define PG_INT64_MIN
Definition: c.h:562
char get_typtype(Oid typid)
Definition: lsyscache.c:2769
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2678
int xmlbinary
Definition: xml.c:109
@ XMLBINARY_BASE64
Definition: xml.h:35

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

Referenced by map_sql_typecoll_to_xmlschema_types().

◆ map_sql_typecoll_to_xmlschema_types()

static const char * map_sql_typecoll_to_xmlschema_types ( List tupdesc_list)
static

Definition at line 3867 of file xml.c.

3868{
3869 List *uniquetypes = NIL;
3870 int i;
3871 StringInfoData result;
3872 ListCell *cell0;
3873
3874 /* extract all column types used in the set of TupleDescs */
3875 foreach(cell0, tupdesc_list)
3876 {
3877 TupleDesc tupdesc = (TupleDesc) lfirst(cell0);
3878
3879 for (i = 0; i < tupdesc->natts; i++)
3880 {
3881 Form_pg_attribute att = TupleDescAttr(tupdesc, i);
3882
3883 if (att->attisdropped)
3884 continue;
3885 uniquetypes = list_append_unique_oid(uniquetypes, att->atttypid);
3886 }
3887 }
3888
3889 /* add base types of domains */
3890 foreach(cell0, uniquetypes)
3891 {
3892 Oid typid = lfirst_oid(cell0);
3893 Oid basetypid = getBaseType(typid);
3894
3895 if (basetypid != typid)
3896 uniquetypes = list_append_unique_oid(uniquetypes, basetypid);
3897 }
3898
3899 /* Convert to textual form */
3900 initStringInfo(&result);
3901
3902 foreach(cell0, uniquetypes)
3903 {
3904 appendStringInfo(&result, "%s\n",
3906 -1));
3907 }
3908
3909 return result.data;
3910}
List * list_append_unique_oid(List *list, Oid datum)
Definition: list.c:1380
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2661
#define lfirst(lc)
Definition: pg_list.h:172
struct TupleDescData * TupleDesc
Definition: tupdesc.h:145
static const char * map_sql_type_to_xmlschema_type(Oid typeoid, int typmod)
Definition: xml.c:3922

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

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

◆ map_sql_value_to_xml_value()

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

Definition at line 2489 of file xml.c.

2490{
2492 {
2493 ArrayType *array;
2494 Oid elmtype;
2495 int16 elmlen;
2496 bool elmbyval;
2497 char elmalign;
2498 int num_elems;
2499 Datum *elem_values;
2500 bool *elem_nulls;
2502 int i;
2503
2504 array = DatumGetArrayTypeP(value);
2505 elmtype = ARR_ELEMTYPE(array);
2506 get_typlenbyvalalign(elmtype, &elmlen, &elmbyval, &elmalign);
2507
2508 deconstruct_array(array, elmtype,
2509 elmlen, elmbyval, elmalign,
2510 &elem_values, &elem_nulls,
2511 &num_elems);
2512
2514
2515 for (i = 0; i < num_elems; i++)
2516 {
2517 if (elem_nulls[i])
2518 continue;
2519 appendStringInfoString(&buf, "<element>");
2521 map_sql_value_to_xml_value(elem_values[i],
2522 elmtype, true));
2523 appendStringInfoString(&buf, "</element>");
2524 }
2525
2526 pfree(elem_values);
2527 pfree(elem_nulls);
2528
2529 return buf.data;
2530 }
2531 else
2532 {
2533 Oid typeOut;
2534 bool isvarlena;
2535 char *str;
2536
2537 /*
2538 * Flatten domains; the special-case treatments below should apply to,
2539 * eg, domains over boolean not just boolean.
2540 */
2542
2543 /*
2544 * Special XSD formatting for some data types
2545 */
2546 switch (type)
2547 {
2548 case BOOLOID:
2549 if (DatumGetBool(value))
2550 return "true";
2551 else
2552 return "false";
2553
2554 case DATEOID:
2555 {
2556 DateADT date;
2557 struct pg_tm tm;
2558 char buf[MAXDATELEN + 1];
2559
2561 /* XSD doesn't support infinite values */
2562 if (DATE_NOT_FINITE(date))
2563 ereport(ERROR,
2564 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2565 errmsg("date out of range"),
2566 errdetail("XML does not support infinite date values.")));
2568 &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
2570
2571 return pstrdup(buf);
2572 }
2573
2574 case TIMESTAMPOID:
2575 {
2577 struct pg_tm tm;
2578 fsec_t fsec;
2579 char buf[MAXDATELEN + 1];
2580
2582
2583 /* XSD doesn't support infinite values */
2585 ereport(ERROR,
2586 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2587 errmsg("timestamp out of range"),
2588 errdetail("XML does not support infinite timestamp values.")));
2589 else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
2590 EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
2591 else
2592 ereport(ERROR,
2593 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2594 errmsg("timestamp out of range")));
2595
2596 return pstrdup(buf);
2597 }
2598
2599 case TIMESTAMPTZOID:
2600 {
2602 struct pg_tm tm;
2603 int tz;
2604 fsec_t fsec;
2605 const char *tzn = NULL;
2606 char buf[MAXDATELEN + 1];
2607
2609
2610 /* XSD doesn't support infinite values */
2612 ereport(ERROR,
2613 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2614 errmsg("timestamp out of range"),
2615 errdetail("XML does not support infinite timestamp values.")));
2616 else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
2617 EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
2618 else
2619 ereport(ERROR,
2620 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2621 errmsg("timestamp out of range")));
2622
2623 return pstrdup(buf);
2624 }
2625
2626#ifdef USE_LIBXML
2627 case BYTEAOID:
2628 {
2629 bytea *bstr = DatumGetByteaPP(value);
2630 PgXmlErrorContext *xmlerrcxt;
2631 volatile xmlBufferPtr buf = NULL;
2632 volatile xmlTextWriterPtr writer = NULL;
2633 char *result;
2634
2636
2637 PG_TRY();
2638 {
2639 buf = xmlBufferCreate();
2640 if (buf == NULL || xmlerrcxt->err_occurred)
2641 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
2642 "could not allocate xmlBuffer");
2643 writer = xmlNewTextWriterMemory(buf, 0);
2644 if (writer == NULL || xmlerrcxt->err_occurred)
2645 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
2646 "could not allocate xmlTextWriter");
2647
2649 xmlTextWriterWriteBase64(writer, VARDATA_ANY(bstr),
2650 0, VARSIZE_ANY_EXHDR(bstr));
2651 else
2652 xmlTextWriterWriteBinHex(writer, VARDATA_ANY(bstr),
2653 0, VARSIZE_ANY_EXHDR(bstr));
2654
2655 /* we MUST do this now to flush data out to the buffer */
2656 xmlFreeTextWriter(writer);
2657 writer = NULL;
2658
2659 result = pstrdup((const char *) xmlBufferContent(buf));
2660 }
2661 PG_CATCH();
2662 {
2663 if (writer)
2664 xmlFreeTextWriter(writer);
2665 if (buf)
2666 xmlBufferFree(buf);
2667
2668 pg_xml_done(xmlerrcxt, true);
2669
2670 PG_RE_THROW();
2671 }
2672 PG_END_TRY();
2673
2674 xmlBufferFree(buf);
2675
2676 pg_xml_done(xmlerrcxt, false);
2677
2678 return result;
2679 }
2680#endif /* USE_LIBXML */
2681
2682 }
2683
2684 /*
2685 * otherwise, just use the type's native text representation
2686 */
2687 getTypeOutputInfo(type, &typeOut, &isvarlena);
2688 str = OidOutputFunctionCall(typeOut, value);
2689
2690 /* ... exactly as-is for XML, and when escaping is not wanted */
2691 if (type == XMLOID || !xml_escape_strings)
2692 return str;
2693
2694 /* otherwise, translate special characters as needed */
2695 return escape_xml(str);
2696 }
2697}
#define DatumGetArrayTypeP(X)
Definition: array.h:261
#define ARR_ELEMTYPE(a)
Definition: array.h:292
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3631
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:321
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:4458
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
Definition: datetime.c:4343
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1910
int16_t int16
Definition: c.h:497
int64 Timestamp
Definition: timestamp.h:38
int64 TimestampTz
Definition: timestamp.h:39
int32 fsec_t
Definition: timestamp.h:41
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:169
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:235
#define DATE_NOT_FINITE(j)
Definition: date.h:43
int32 DateADT
Definition: date.h:23
static DateADT DatumGetDateADT(Datum X)
Definition: date.h:54
#define PG_RE_THROW()
Definition: elog.h:404
#define PG_TRY(...)
Definition: elog.h:371
#define PG_END_TRY(...)
Definition: elog.h:396
#define PG_CATCH(...)
Definition: elog.h:381
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1763
#define DatumGetByteaPP(X)
Definition: fmgr.h:291
#define MAXDATELEN
Definition: datetime.h:200
static struct @165 value
static struct pg_tm tm
Definition: localtime.c:104
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:3047
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2411
#define type_is_array_domain(typid)
Definition: lsyscache.h:216
char * pstrdup(const char *in)
Definition: mcxt.c:1703
void pfree(void *pointer)
Definition: mcxt.c:1528
#define USE_XSD_DATES
Definition: miscadmin.h:239
long date
Definition: pgtypes_date.h:9
int64 timestamp
static bool DatumGetBool(Datum X)
Definition: postgres.h:95
uintptr_t Datum
Definition: postgres.h:69
Definition: pgtime.h:35
int tm_mday
Definition: pgtime.h:39
int tm_mon
Definition: pgtime.h:40
int tm_year
Definition: pgtime.h:41
static Timestamp DatumGetTimestamp(Datum X)
Definition: timestamp.h:28
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317
const char * type
char * map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
Definition: xml.c:2489
char * escape_xml(const char *str)
Definition: xml.c:2708
struct PgXmlErrorContext PgXmlErrorContext
Definition: xml.h:48
PgXmlErrorContext * pg_xml_init(PgXmlStrictness strictness)
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
void pg_xml_done(PgXmlErrorContext *errcxt, bool isError)
@ PG_XML_STRICTNESS_ALL
Definition: xml.h:44

References appendStringInfoString(), ARR_ELEMTYPE, buf, DATE_NOT_FINITE, DatumGetArrayTypeP, DatumGetBool(), DatumGetByteaPP, DatumGetDateADT(), DatumGetTimestamp(), deconstruct_array(), EncodeDateOnly(), EncodeDateTime(), ereport, errcode(), errdetail(), errmsg(), ERROR, escape_xml(), get_typlenbyvalalign(), getBaseType(), getTypeOutputInfo(), i, initStringInfo(), j2date(), 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(), str, timestamp2tm(), TIMESTAMP_NOT_FINITE, tm, pg_tm::tm_mday, pg_tm::tm_mon, pg_tm::tm_year, type, type_is_array_domain, USE_XSD_DATES, value, VARDATA_ANY, VARSIZE_ANY_EXHDR, xml_ereport(), xmlbinary, and XMLBINARY_BASE64.

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

◆ map_xml_name_to_sql_identifier()

char * map_xml_name_to_sql_identifier ( const char *  name)

Definition at line 2447 of file xml.c.

2448{
2450 const char *p;
2451
2453
2454 for (p = name; *p; p += pg_mblen(p))
2455 {
2456 if (*p == '_' && *(p + 1) == 'x'
2457 && isxdigit((unsigned char) *(p + 2))
2458 && isxdigit((unsigned char) *(p + 3))
2459 && isxdigit((unsigned char) *(p + 4))
2460 && isxdigit((unsigned char) *(p + 5))
2461 && *(p + 6) == '_')
2462 {
2463 char cbuf[MAX_UNICODE_EQUIVALENT_STRING + 1];
2464 unsigned int u;
2465
2466 sscanf(p + 2, "%X", &u);
2467 pg_unicode_to_server(u, (unsigned char *) cbuf);
2469 p += 6;
2470 }
2471 else
2473 }
2474
2475 return buf.data;
2476}
void pg_unicode_to_server(pg_wchar c, unsigned char *s)
Definition: mbutils.c:865
#define MAX_UNICODE_EQUIVALENT_STRING
Definition: pg_wchar.h:329

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

Referenced by get_rule_expr().

◆ query_to_oid_list()

static List * query_to_oid_list ( const char *  query)
static

Definition at line 2797 of file xml.c.

2798{
2799 uint64 i;
2800 List *list = NIL;
2801 int spi_result;
2802
2803 spi_result = SPI_execute(query, true, 0);
2804 if (spi_result != SPI_OK_SELECT)
2805 elog(ERROR, "SPI_execute returned %s for %s",
2806 SPI_result_code_string(spi_result), query);
2807
2808 for (i = 0; i < SPI_processed; i++)
2809 {
2810 Datum oid;
2811 bool isnull;
2812
2815 1,
2816 &isnull);
2817 if (!isnull)
2819 }
2820
2821 return list;
2822}
List * lappend_oid(List *list, Oid datum)
Definition: list.c:375
static Oid DatumGetObjectId(Datum X)
Definition: postgres.h:247
const char * SPI_result_code_string(int code)
Definition: spi.c:1972
SPITupleTable * SPI_tuptable
Definition: spi.c:45
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:596
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition: spi.c:1252
#define SPI_OK_SELECT
Definition: spi.h:86
TupleDesc tupdesc
Definition: spi.h:25
HeapTuple * vals
Definition: spi.h:26

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

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

◆ query_to_xml()

Datum query_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 2910 of file xml.c.

2911{
2912 char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2913 bool nulls = PG_GETARG_BOOL(1);
2914 bool tableforest = PG_GETARG_BOOL(2);
2915 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2916
2918 NULL, nulls, tableforest,
2919 targetns, true)));
2920}
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:3012

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

◆ query_to_xml_and_xmlschema()

Datum query_to_xml_and_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 3156 of file xml.c.

3157{
3158 char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
3159 bool nulls = PG_GETARG_BOOL(1);
3160 bool tableforest = PG_GETARG_BOOL(2);
3161 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
3162
3163 const char *xmlschema;
3165 Portal portal;
3166
3167 SPI_connect();
3168
3169 if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
3170 elog(ERROR, "SPI_prepare(\"%s\") failed", query);
3171
3172 if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
3173 elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
3174
3176 InvalidOid, nulls, tableforest, targetns));
3177 SPI_cursor_close(portal);
3178 SPI_finish();
3179
3181 xmlschema, nulls, tableforest,
3182 targetns, true)));
3183}
#define plan(x)
Definition: pg_regress.c:161
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1445
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:860
void SPI_cursor_close(Portal portal)
Definition: spi.c:1862

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

◆ query_to_xml_internal()

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

Definition at line 3012 of file xml.c.

3015{
3016 StringInfo result;
3017 char *xmltn;
3018 uint64 i;
3019
3020 if (tablename)
3021 xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
3022 else
3023 xmltn = "table";
3024
3025 result = makeStringInfo();
3026
3027 SPI_connect();
3028 if (SPI_execute(query, true, 0) != SPI_OK_SELECT)
3029 ereport(ERROR,
3030 (errcode(ERRCODE_DATA_EXCEPTION),
3031 errmsg("invalid query")));
3032
3033 if (!tableforest)
3034 {
3035 xmldata_root_element_start(result, xmltn, xmlschema,
3036 targetns, top_level);
3037 appendStringInfoChar(result, '\n');
3038 }
3039
3040 if (xmlschema)
3041 appendStringInfo(result, "%s\n\n", xmlschema);
3042
3043 for (i = 0; i < SPI_processed; i++)
3044 SPI_sql_row_to_xmlelement(i, result, tablename, nulls,
3045 tableforest, targetns, top_level);
3046
3047 if (!tableforest)
3048 xmldata_root_element_end(result, xmltn);
3049
3050 SPI_finish();
3051
3052 return result;
3053}

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

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

◆ query_to_xmlschema()

Datum query_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 3076 of file xml.c.

3077{
3078 char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
3079 bool nulls = PG_GETARG_BOOL(1);
3080 bool tableforest = PG_GETARG_BOOL(2);
3081 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
3082 const char *result;
3084 Portal portal;
3085
3086 SPI_connect();
3087
3088 if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
3089 elog(ERROR, "SPI_prepare(\"%s\") failed", query);
3090
3091 if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
3092 elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
3093
3095 InvalidOid, nulls,
3096 tableforest, targetns));
3097 SPI_cursor_close(portal);
3098 SPI_finish();
3099
3101}

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

◆ schema_get_xml_visible_tables()

static List * schema_get_xml_visible_tables ( Oid  nspid)
static

Definition at line 2826 of file xml.c.

2827{
2828 StringInfoData query;
2829
2830 initStringInfo(&query);
2831 appendStringInfo(&query, "SELECT oid FROM pg_catalog.pg_class"
2832 " WHERE relnamespace = %u AND relkind IN ("
2833 CppAsString2(RELKIND_RELATION) ","
2834 CppAsString2(RELKIND_MATVIEW) ","
2835 CppAsString2(RELKIND_VIEW) ")"
2836 " AND pg_catalog.has_table_privilege (oid, 'SELECT')"
2837 " ORDER BY relname;", nspid);
2838
2839 return query_to_oid_list(query.data);
2840}

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

Referenced by schema_to_xml_internal(), and schema_to_xmlschema_internal().

◆ schema_to_xml()

Datum schema_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 3235 of file xml.c.

3236{
3238 bool nulls = PG_GETARG_BOOL(1);
3239 bool tableforest = PG_GETARG_BOOL(2);
3240 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
3241
3242 char *schemaname;
3243 Oid nspid;
3244
3245 schemaname = NameStr(*name);
3246 nspid = LookupExplicitNamespace(schemaname, false);
3247
3249 nulls, tableforest, targetns, true)));
3250}
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
Definition: namespace.c:3385
Definition: c.h:712

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

◆ schema_to_xml_and_xmlschema()

Datum schema_to_xml_and_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 3339 of file xml.c.

3340{
3342 bool nulls = PG_GETARG_BOOL(1);
3343 bool tableforest = PG_GETARG_BOOL(2);
3344 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
3345 char *schemaname;
3346 Oid nspid;
3347 StringInfo xmlschema;
3348
3349 schemaname = NameStr(*name);
3350 nspid = LookupExplicitNamespace(schemaname, false);
3351
3352 xmlschema = schema_to_xmlschema_internal(schemaname, nulls,
3353 tableforest, targetns);
3354
3356 xmlschema->data, nulls,
3357 tableforest, targetns, true)));
3358}
static StringInfo schema_to_xmlschema_internal(const char *schemaname, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3281

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

◆ schema_to_xml_internal()

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

Definition at line 3192 of file xml.c.

3194{
3195 StringInfo result;
3196 char *xmlsn;
3197 List *relid_list;
3198 ListCell *cell;
3199
3201 true, false);
3202 result = makeStringInfo();
3203
3204 xmldata_root_element_start(result, xmlsn, xmlschema, targetns, top_level);
3205 appendStringInfoChar(result, '\n');
3206
3207 if (xmlschema)
3208 appendStringInfo(result, "%s\n\n", xmlschema);
3209
3210 SPI_connect();
3211
3213
3214 foreach(cell, relid_list)
3215 {
3216 Oid relid = lfirst_oid(cell);
3217 StringInfo subres;
3218
3219 subres = table_to_xml_internal(relid, NULL, nulls, tableforest,
3220 targetns, false);
3221
3222 appendBinaryStringInfo(result, subres->data, subres->len);
3223 appendStringInfoChar(result, '\n');
3224 }
3225
3226 SPI_finish();
3227
3228 xmldata_root_element_end(result, xmlsn);
3229
3230 return result;
3231}
static List * schema_get_xml_visible_tables(Oid nspid)
Definition: xml.c:2826
static StringInfo table_to_xml_internal(Oid relid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2879

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

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

◆ schema_to_xmlschema()

Datum schema_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 3326 of file xml.c.

3327{
3329 bool nulls = PG_GETARG_BOOL(1);
3330 bool tableforest = PG_GETARG_BOOL(2);
3331 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
3332
3334 nulls, tableforest, targetns)));
3335}

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

◆ schema_to_xmlschema_internal()

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

Definition at line 3281 of file xml.c.

3283{
3284 Oid nspid;
3285 List *relid_list;
3286 List *tupdesc_list;
3287 ListCell *cell;
3288 StringInfo result;
3289
3290 result = makeStringInfo();
3291
3292 nspid = LookupExplicitNamespace(schemaname, false);
3293
3294 xsd_schema_element_start(result, targetns);
3295
3296 SPI_connect();
3297
3299
3300 tupdesc_list = NIL;
3301 foreach(cell, relid_list)
3302 {
3303 Relation rel;
3304
3306 tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
3307 table_close(rel, NoLock);
3308 }
3309
3312
3315 nulls, tableforest, targetns));
3316
3317 xsd_schema_element_end(result);
3318
3319 SPI_finish();
3320
3321 return result;
3322}
static const char * map_sql_schema_to_xmlschema_types(Oid nspid, List *relid_list, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3632

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

Referenced by schema_to_xml_and_xmlschema(), and schema_to_xmlschema().

◆ SPI_sql_row_to_xmlelement()

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

Definition at line 4097 of file xml.c.

4100{
4101 int i;
4102 char *xmltn;
4103
4104 if (tablename)
4105 xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
4106 else
4107 {
4108 if (tableforest)
4109 xmltn = "row";
4110 else
4111 xmltn = "table";
4112 }
4113
4114 if (tableforest)
4115 xmldata_root_element_start(result, xmltn, NULL, targetns, top_level);
4116 else
4117 appendStringInfoString(result, "<row>\n");
4118
4119 for (i = 1; i <= SPI_tuptable->tupdesc->natts; i++)
4120 {
4121 char *colname;
4122 Datum colval;
4123 bool isnull;
4124
4126 true, false);
4127 colval = SPI_getbinval(SPI_tuptable->vals[rownum],
4129 i,
4130 &isnull);
4131 if (isnull)
4132 {
4133 if (nulls)
4134 appendStringInfo(result, " <%s xsi:nil=\"true\"/>\n", colname);
4135 }
4136 else
4137 appendStringInfo(result, " <%s>%s</%s>\n",
4138 colname,
4141 colname);
4142 }
4143
4144 if (tableforest)
4145 {
4146 xmldata_root_element_end(result, xmltn);
4147 appendStringInfoChar(result, '\n');
4148 }
4149 else
4150 appendStringInfoString(result, "</row>\n\n");
4151}
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1308
char * SPI_fname(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1198

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

Referenced by cursor_to_xml(), and query_to_xml_internal().

◆ stringinfo_to_xmltype()

◆ table_to_xml()

Datum table_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 2896 of file xml.c.

2897{
2898 Oid relid = PG_GETARG_OID(0);
2899 bool nulls = PG_GETARG_BOOL(1);
2900 bool tableforest = PG_GETARG_BOOL(2);
2901 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2902
2904 nulls, tableforest,
2905 targetns, true)));
2906}
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

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

◆ table_to_xml_and_xmlschema()

Datum table_to_xml_and_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 3135 of file xml.c.

3136{
3137 Oid relid = PG_GETARG_OID(0);
3138 bool nulls = PG_GETARG_BOOL(1);
3139 bool tableforest = PG_GETARG_BOOL(2);
3140 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
3141 Relation rel;
3142 const char *xmlschema;
3143
3144 rel = table_open(relid, AccessShareLock);
3145 xmlschema = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
3146 tableforest, targetns);
3147 table_close(rel, NoLock);
3148
3150 xmlschema, nulls, tableforest,
3151 targetns, true)));
3152}

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

◆ table_to_xml_internal()

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

Definition at line 2879 of file xml.c.

2882{
2883 StringInfoData query;
2884
2885 initStringInfo(&query);
2886 appendStringInfo(&query, "SELECT * FROM %s",
2888 ObjectIdGetDatum(relid))));
2889 return query_to_xml_internal(query.data, get_rel_name(relid),
2890 xmlschema, nulls, tableforest,
2891 targetns, top_level);
2892}
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:682
static char * DatumGetCString(Datum X)
Definition: postgres.h:340
Datum regclassout(PG_FUNCTION_ARGS)
Definition: regproc.c:943

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

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

◆ table_to_xmlschema()

Datum table_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 3057 of file xml.c.

3058{
3059 Oid relid = PG_GETARG_OID(0);
3060 bool nulls = PG_GETARG_BOOL(1);
3061 bool tableforest = PG_GETARG_BOOL(2);
3062 const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
3063 const char *result;
3064 Relation rel;
3065
3066 rel = table_open(relid, AccessShareLock);
3067 result = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
3068 tableforest, targetns);
3069 table_close(rel, NoLock);
3070
3072}

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

◆ texttoxml()

Datum texttoxml ( PG_FUNCTION_ARGS  )

Definition at line 637 of file xml.c.

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

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

◆ xml_in()

Datum xml_in ( PG_FUNCTION_ARGS  )

Definition at line 273 of file xml.c.

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

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

◆ xml_is_document()

bool xml_is_document ( xmltype arg)

Definition at line 1124 of file xml.c.

1125{
1126#ifdef USE_LIBXML
1127 xmlDocPtr doc;
1128 ErrorSaveContext escontext = {T_ErrorSaveContext};
1129
1130 /*
1131 * We'll report "true" if no soft error is reported by xml_parse().
1132 */
1133 doc = xml_parse((text *) arg, XMLOPTION_DOCUMENT, true,
1134 GetDatabaseEncoding(), NULL, NULL, (Node *) &escontext);
1135 if (doc)
1136 xmlFreeDoc(doc);
1137
1138 return !escontext.error_occurred;
1139#else /* not USE_LIBXML */
1141 return false;
1142#endif /* not USE_LIBXML */
1143}
void * arg
@ XMLOPTION_DOCUMENT
Definition: primnodes.h:1597
bool error_occurred
Definition: miscnodes.h:47
Definition: nodes.h:135

References arg, ErrorSaveContext::error_occurred, GetDatabaseEncoding(), NO_XML_SUPPORT, and XMLOPTION_DOCUMENT.

Referenced by ExecEvalXmlExpr().

◆ xml_is_well_formed()

Datum xml_is_well_formed ( PG_FUNCTION_ARGS  )

Definition at line 4620 of file xml.c.

4621{
4622#ifdef USE_LIBXML
4624
4625 PG_RETURN_BOOL(wellformed_xml(data, xmloption));
4626#else
4628 return 0;
4629#endif /* not USE_LIBXML */
4630}
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

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

◆ xml_is_well_formed_content()

Datum xml_is_well_formed_content ( PG_FUNCTION_ARGS  )

Definition at line 4646 of file xml.c.

4647{
4648#ifdef USE_LIBXML
4650
4651 PG_RETURN_BOOL(wellformed_xml(data, XMLOPTION_CONTENT));
4652#else
4654 return 0;
4655#endif /* not USE_LIBXML */
4656}
@ XMLOPTION_CONTENT
Definition: primnodes.h:1598

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

◆ xml_is_well_formed_document()

Datum xml_is_well_formed_document ( PG_FUNCTION_ARGS  )

Definition at line 4633 of file xml.c.

4634{
4635#ifdef USE_LIBXML
4637
4638 PG_RETURN_BOOL(wellformed_xml(data, XMLOPTION_DOCUMENT));
4639#else
4641 return 0;
4642#endif /* not USE_LIBXML */
4643}

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

◆ xml_out()

Datum xml_out ( PG_FUNCTION_ARGS  )

Definition at line 356 of file xml.c.

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

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

◆ xml_out_internal()

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

Definition at line 312 of file xml.c.

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

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

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

◆ xml_recv()

Datum xml_recv ( PG_FUNCTION_ARGS  )

Definition at line 371 of file xml.c.

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

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

◆ xml_send()

Datum xml_send ( PG_FUNCTION_ARGS  )

Definition at line 438 of file xml.c.

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

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

◆ xmlcomment()

Datum xmlcomment ( PG_FUNCTION_ARGS  )

Definition at line 491 of file xml.c.

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

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

◆ xmlconcat()

xmltype * xmlconcat ( List args)

Definition at line 553 of file xml.c.

554{
555#ifdef USE_LIBXML
556 int global_standalone = 1;
557 xmlChar *global_version = NULL;
558 bool global_version_no_value = false;
560 ListCell *v;
561
563 foreach(v, args)
564 {
566 size_t len;
567 xmlChar *version;
568 int standalone;
569 char *str;
570
571 len = VARSIZE(x) - VARHDRSZ;
572 str = text_to_cstring((text *) x);
573
574 parse_xml_decl((xmlChar *) str, &len, &version, NULL, &standalone);
575
576 if (standalone == 0 && global_standalone == 1)
577 global_standalone = 0;
578 if (standalone < 0)
579 global_standalone = -1;
580
581 if (!version)
582 global_version_no_value = true;
583 else if (!global_version)
584 global_version = version;
585 else if (xmlStrcmp(version, global_version) != 0)
586 global_version_no_value = true;
587
589 pfree(str);
590 }
591
592 if (!global_version_no_value || global_standalone >= 0)
593 {
594 StringInfoData buf2;
595
596 initStringInfo(&buf2);
597
598 print_xml_decl(&buf2,
599 (!global_version_no_value) ? global_version : NULL,
600 0,
601 global_standalone);
602
603 appendBinaryStringInfo(&buf2, buf.data, buf.len);
604 buf = buf2;
605 }
606
607 return stringinfo_to_xmltype(&buf);
608#else
610 return NULL;
611#endif
612}
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
#define VARSIZE(PTR)
Definition: varatt.h:279
static xmltype * DatumGetXmlP(Datum X)
Definition: xml.h:51

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

Referenced by ExecEvalXmlExpr(), and xmlconcat2().

◆ xmlconcat2()

Datum xmlconcat2 ( PG_FUNCTION_ARGS  )

Definition at line 619 of file xml.c.

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

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

◆ xmldata_root_element_end()

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

Definition at line 3005 of file xml.c.

3006{
3007 appendStringInfo(result, "</%s>\n", eltname);
3008}

References appendStringInfo().

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

◆ xmldata_root_element_start()

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

Definition at line 2978 of file xml.c.

2981{
2982 /* This isn't really wrong but currently makes no sense. */
2983 Assert(top_level || !xmlschema);
2984
2985 appendStringInfo(result, "<%s", eltname);
2986 if (top_level)
2987 {
2988 appendStringInfoString(result, " xmlns:xsi=\"" NAMESPACE_XSI "\"");
2989 if (strlen(targetns) > 0)
2990 appendStringInfo(result, " xmlns=\"%s\"", targetns);
2991 }
2992 if (xmlschema)
2993 {
2994 /* FIXME: better targets */
2995 if (strlen(targetns) > 0)
2996 appendStringInfo(result, " xsi:schemaLocation=\"%s #\"", targetns);
2997 else
2998 appendStringInfoString(result, " xsi:noNamespaceSchemaLocation=\"#\"");
2999 }
3000 appendStringInfoString(result, ">\n");
3001}
#define NAMESPACE_XSI
Definition: xml.c:244

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

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

◆ xmlelement()

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

Definition at line 864 of file xml.c.

867{
868#ifdef USE_LIBXML
869 xmltype *result;
870 List *named_arg_strings;
871 List *arg_strings;
872 int i;
873 ListCell *arg;
874 ListCell *narg;
875 PgXmlErrorContext *xmlerrcxt;
876 volatile xmlBufferPtr buf = NULL;
877 volatile xmlTextWriterPtr writer = NULL;
878
879 /*
880 * All arguments are already evaluated, and their values are passed in the
881 * named_argvalue/named_argnull or argvalue/argnull arrays. This avoids
882 * issues if one of the arguments involves a call to some other function
883 * or subsystem that wants to use libxml on its own terms. We examine the
884 * original XmlExpr to identify the numbers and types of the arguments.
885 */
886 named_arg_strings = NIL;
887 i = 0;
888 foreach(arg, xexpr->named_args)
889 {
890 Expr *e = (Expr *) lfirst(arg);
891 char *str;
892
893 if (named_argnull[i])
894 str = NULL;
895 else
896 str = map_sql_value_to_xml_value(named_argvalue[i],
897 exprType((Node *) e),
898 false);
899 named_arg_strings = lappend(named_arg_strings, str);
900 i++;
901 }
902
903 arg_strings = NIL;
904 i = 0;
905 foreach(arg, xexpr->args)
906 {
907 Expr *e = (Expr *) lfirst(arg);
908 char *str;
909
910 /* here we can just forget NULL elements immediately */
911 if (!argnull[i])
912 {
913 str = map_sql_value_to_xml_value(argvalue[i],
914 exprType((Node *) e),
915 true);
916 arg_strings = lappend(arg_strings, str);
917 }
918 i++;
919 }
920
922
923 PG_TRY();
924 {
925 buf = xmlBufferCreate();
926 if (buf == NULL || xmlerrcxt->err_occurred)
927 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
928 "could not allocate xmlBuffer");
929 writer = xmlNewTextWriterMemory(buf, 0);
930 if (writer == NULL || xmlerrcxt->err_occurred)
931 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
932 "could not allocate xmlTextWriter");
933
934 xmlTextWriterStartElement(writer, (xmlChar *) xexpr->name);
935
936 forboth(arg, named_arg_strings, narg, xexpr->arg_names)
937 {
938 char *str = (char *) lfirst(arg);
939 char *argname = strVal(lfirst(narg));
940
941 if (str)
942 xmlTextWriterWriteAttribute(writer,
943 (xmlChar *) argname,
944 (xmlChar *) str);
945 }
946
947 foreach(arg, arg_strings)
948 {
949 char *str = (char *) lfirst(arg);
950
951 xmlTextWriterWriteRaw(writer, (xmlChar *) str);
952 }
953
954 xmlTextWriterEndElement(writer);
955
956 /* we MUST do this now to flush data out to the buffer ... */
957 xmlFreeTextWriter(writer);
958 writer = NULL;
959
960 result = xmlBuffer_to_xmltype(buf);
961 }
962 PG_CATCH();
963 {
964 if (writer)
965 xmlFreeTextWriter(writer);
966 if (buf)
967 xmlBufferFree(buf);
968
969 pg_xml_done(xmlerrcxt, true);
970
971 PG_RE_THROW();
972 }
973 PG_END_TRY();
974
975 xmlBufferFree(buf);
976
977 pg_xml_done(xmlerrcxt, false);
978
979 return result;
980#else
982 return NULL;
983#endif
984}
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
e
Definition: preproc-init.c:82
List * args
Definition: primnodes.h:1613
List * named_args
Definition: primnodes.h:1609
#define strVal(v)
Definition: value.h:82

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

Referenced by ExecEvalXmlExpr().

◆ xmlexists()

Datum xmlexists ( PG_FUNCTION_ARGS  )

Definition at line 4555 of file xml.c.

4556{
4557#ifdef USE_LIBXML
4558 text *xpath_expr_text = PG_GETARG_TEXT_PP(0);
4560 int res_nitems;
4561
4562 xpath_internal(xpath_expr_text, data, NULL,
4563 &res_nitems, NULL);
4564
4565 PG_RETURN_BOOL(res_nitems > 0);
4566#else
4568 return 0;
4569#endif
4570}

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

◆ xmlparse()

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

Definition at line 988 of file xml.c.

989{
990#ifdef USE_LIBXML
991 xmlDocPtr doc;
992
993 doc = xml_parse(data, xmloption_arg, preserve_whitespace,
994 GetDatabaseEncoding(), NULL, NULL, NULL);
995 xmlFreeDoc(doc);
996
997 return (xmltype *) data;
998#else
1000 return NULL;
1001#endif
1002}

References data, GetDatabaseEncoding(), and NO_XML_SUPPORT.

Referenced by ExecEvalXmlExpr(), and texttoxml().

◆ xmlpi()

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

Definition at line 1006 of file xml.c.

1007{
1008#ifdef USE_LIBXML
1009 xmltype *result;
1011
1012 if (pg_strcasecmp(target, "xml") == 0)
1013 ereport(ERROR,
1014 (errcode(ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION),
1015 errmsg("invalid XML processing instruction"),
1016 errdetail("XML processing instruction target name cannot be \"%s\".", target)));
1017
1018 /*
1019 * Following the SQL standard, the null check comes after the syntax check
1020 * above.
1021 */
1022 *result_is_null = arg_is_null;
1023 if (*result_is_null)
1024 return NULL;
1025
1027
1028 appendStringInfo(&buf, "<?%s", target);
1029
1030 if (arg != NULL)
1031 {
1032 char *string;
1033
1034 string = text_to_cstring(arg);
1035 if (strstr(string, "?>") != NULL)
1036 ereport(ERROR,
1037 (errcode(ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION),
1038 errmsg("invalid XML processing instruction"),
1039 errdetail("XML processing instruction cannot contain \"?>\".")));
1040
1042 appendStringInfoString(&buf, string + strspn(string, " "));
1043 pfree(string);
1044 }
1046
1047 result = stringinfo_to_xmltype(&buf);
1048 pfree(buf.data);
1049 return result;
1050#else
1052 return NULL;
1053#endif
1054}
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
char string[11]
Definition: preproc-type.c:52

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

Referenced by ExecEvalXmlExpr().

◆ xmlroot()

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

Definition at line 1058 of file xml.c.

1059{
1060#ifdef USE_LIBXML
1061 char *str;
1062 size_t len;
1063 xmlChar *orig_version;
1064 int orig_standalone;
1066
1067 len = VARSIZE(data) - VARHDRSZ;
1069
1070 parse_xml_decl((xmlChar *) str, &len, &orig_version, NULL, &orig_standalone);
1071
1072 if (version)
1073 orig_version = xml_text2xmlChar(version);
1074 else
1075 orig_version = NULL;
1076
1077 switch (standalone)
1078 {
1079 case XML_STANDALONE_YES:
1080 orig_standalone = 1;
1081 break;
1082 case XML_STANDALONE_NO:
1083 orig_standalone = 0;
1084 break;
1086 orig_standalone = -1;
1087 break;
1089 /* leave original value */
1090 break;
1091 }
1092
1094 print_xml_decl(&buf, orig_version, 0, orig_standalone);
1096
1097 return stringinfo_to_xmltype(&buf);
1098#else
1100 return NULL;
1101#endif
1102}
@ XML_STANDALONE_OMITTED
Definition: xml.h:30
@ XML_STANDALONE_NO_VALUE
Definition: xml.h:29
@ XML_STANDALONE_YES
Definition: xml.h:27
@ XML_STANDALONE_NO
Definition: xml.h:28

References appendStringInfoString(), buf, data, initStringInfo(), len, NO_XML_SUPPORT, str, stringinfo_to_xmltype(), text_to_cstring(), VARHDRSZ, VARSIZE, XML_STANDALONE_NO, XML_STANDALONE_NO_VALUE, XML_STANDALONE_OMITTED, and XML_STANDALONE_YES.

Referenced by ExecEvalXmlExpr().

◆ XmlTableDestroyOpaque()

static void XmlTableDestroyOpaque ( struct TableFuncScanState state)
static

Definition at line 5090 of file xml.c.

5091{
5092#ifdef USE_LIBXML
5093 XmlTableBuilderData *xtCxt;
5094
5095 xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableDestroyOpaque");
5096
5097 /* Propagate our own error context to libxml2 */
5098 xmlSetStructuredErrorFunc(xtCxt->xmlerrcxt, xml_errorHandler);
5099
5100 if (xtCxt->xpathscomp != NULL)
5101 {
5102 int i;
5103
5104 for (i = 0; i < xtCxt->natts; i++)
5105 if (xtCxt->xpathscomp[i] != NULL)
5106 xmlXPathFreeCompExpr(xtCxt->xpathscomp[i]);
5107 }
5108
5109 if (xtCxt->xpathobj != NULL)
5110 xmlXPathFreeObject(xtCxt->xpathobj);
5111 if (xtCxt->xpathcomp != NULL)
5112 xmlXPathFreeCompExpr(xtCxt->xpathcomp);
5113 if (xtCxt->xpathcxt != NULL)
5114 xmlXPathFreeContext(xtCxt->xpathcxt);
5115 if (xtCxt->doc != NULL)
5116 xmlFreeDoc(xtCxt->doc);
5117 if (xtCxt->ctxt != NULL)
5118 xmlFreeParserCtxt(xtCxt->ctxt);
5119
5120 pg_xml_done(xtCxt->xmlerrcxt, true);
5121
5122 /* not valid anymore */
5123 xtCxt->magic = 0;
5124 state->opaque = NULL;
5125
5126#else
5128#endif /* not USE_LIBXML */
5129}
Definition: regguts.h:323

References i, NO_XML_SUPPORT, and pg_xml_done().

◆ XmlTableFetchRow()

static bool XmlTableFetchRow ( struct TableFuncScanState state)
static

Definition at line 4893 of file xml.c.

4894{
4895#ifdef USE_LIBXML
4896 XmlTableBuilderData *xtCxt;
4897
4898 xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableFetchRow");
4899
4900 /* Propagate our own error context to libxml2 */
4901 xmlSetStructuredErrorFunc(xtCxt->xmlerrcxt, xml_errorHandler);
4902
4903 if (xtCxt->xpathobj == NULL)
4904 {
4905 xtCxt->xpathobj = xmlXPathCompiledEval(xtCxt->xpathcomp, xtCxt->xpathcxt);
4906 if (xtCxt->xpathobj == NULL || xtCxt->xmlerrcxt->err_occurred)
4907 xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
4908 "could not create XPath object");
4909
4910 xtCxt->row_count = 0;
4911 }
4912
4913 if (xtCxt->xpathobj->type == XPATH_NODESET)
4914 {
4915 if (xtCxt->xpathobj->nodesetval != NULL)
4916 {
4917 if (xtCxt->row_count++ < xtCxt->xpathobj->nodesetval->nodeNr)
4918 return true;
4919 }
4920 }
4921
4922 return false;
4923#else
4925 return false;
4926#endif /* not USE_LIBXML */
4927}

References ERROR, NO_XML_SUPPORT, and xml_ereport().

◆ XmlTableGetValue()

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

Definition at line 4938 of file xml.c.

4940{
4941#ifdef USE_LIBXML
4942 Datum result = (Datum) 0;
4943 XmlTableBuilderData *xtCxt;
4944 volatile xmlXPathObjectPtr xpathobj = NULL;
4945
4946 xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableGetValue");
4947
4948 Assert(xtCxt->xpathobj &&
4949 xtCxt->xpathobj->type == XPATH_NODESET &&
4950 xtCxt->xpathobj->nodesetval != NULL);
4951
4952 /* Propagate our own error context to libxml2 */
4953 xmlSetStructuredErrorFunc(xtCxt->xmlerrcxt, xml_errorHandler);
4954
4955 *isnull = false;
4956
4957 Assert(xtCxt->xpathscomp[colnum] != NULL);
4958
4959 PG_TRY();
4960 {
4961 xmlNodePtr cur;
4962 char *cstr = NULL;
4963
4964 /* Set current node as entry point for XPath evaluation */
4965 cur = xtCxt->xpathobj->nodesetval->nodeTab[xtCxt->row_count - 1];
4966 xtCxt->xpathcxt->node = cur;
4967
4968 /* Evaluate column path */
4969 xpathobj = xmlXPathCompiledEval(xtCxt->xpathscomp[colnum], xtCxt->xpathcxt);
4970 if (xpathobj == NULL || xtCxt->xmlerrcxt->err_occurred)
4971 xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
4972 "could not create XPath object");
4973
4974 /*
4975 * There are four possible cases, depending on the number of nodes
4976 * returned by the XPath expression and the type of the target column:
4977 * a) XPath returns no nodes. b) The target type is XML (return all
4978 * as XML). For non-XML return types: c) One node (return content).
4979 * d) Multiple nodes (error).
4980 */
4981 if (xpathobj->type == XPATH_NODESET)
4982 {
4983 int count = 0;
4984
4985 if (xpathobj->nodesetval != NULL)
4986 count = xpathobj->nodesetval->nodeNr;
4987
4988 if (xpathobj->nodesetval == NULL || count == 0)
4989 {
4990 *isnull = true;
4991 }
4992 else
4993 {
4994 if (typid == XMLOID)
4995 {
4996 text *textstr;
4998
4999 /* Concatenate serialized values */
5001 for (int i = 0; i < count; i++)
5002 {
5003 textstr =
5004 xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[i],
5005 xtCxt->xmlerrcxt);
5006
5007 appendStringInfoText(&str, textstr);
5008 }
5009 cstr = str.data;
5010 }
5011 else
5012 {
5013 xmlChar *str;
5014
5015 if (count > 1)
5016 ereport(ERROR,
5017 (errcode(ERRCODE_CARDINALITY_VIOLATION),
5018 errmsg("more than one value returned by column XPath expression")));
5019
5020 str = xmlXPathCastNodeSetToString(xpathobj->nodesetval);
5021 cstr = str ? xml_pstrdup_and_free(str) : "";
5022 }
5023 }
5024 }
5025 else if (xpathobj->type == XPATH_STRING)
5026 {
5027 /* Content should be escaped when target will be XML */
5028 if (typid == XMLOID)
5029 cstr = escape_xml((char *) xpathobj->stringval);
5030 else
5031 cstr = (char *) xpathobj->stringval;
5032 }
5033 else if (xpathobj->type == XPATH_BOOLEAN)
5034 {
5035 char typcategory;
5036 bool typispreferred;
5037 xmlChar *str;
5038
5039 /* Allow implicit casting from boolean to numbers */
5040 get_type_category_preferred(typid, &typcategory, &typispreferred);
5041
5042 if (typcategory != TYPCATEGORY_NUMERIC)
5043 str = xmlXPathCastBooleanToString(xpathobj->boolval);
5044 else
5045 str = xmlXPathCastNumberToString(xmlXPathCastBooleanToNumber(xpathobj->boolval));
5046
5047 cstr = xml_pstrdup_and_free(str);
5048 }
5049 else if (xpathobj->type == XPATH_NUMBER)
5050 {
5051 xmlChar *str;
5052
5053 str = xmlXPathCastNumberToString(xpathobj->floatval);
5054 cstr = xml_pstrdup_and_free(str);
5055 }
5056 else
5057 elog(ERROR, "unexpected XPath object type %u", xpathobj->type);
5058
5059 /*
5060 * By here, either cstr contains the result value, or the isnull flag
5061 * has been set.
5062 */
5063 Assert(cstr || *isnull);
5064
5065 if (!*isnull)
5066 result = InputFunctionCall(&state->in_functions[colnum],
5067 cstr,
5068 state->typioparams[colnum],
5069 typmod);
5070 }
5071 PG_FINALLY();
5072 {
5073 if (xpathobj != NULL)
5074 xmlXPathFreeObject(xpathobj);
5075 }
5076 PG_END_TRY();
5077
5078 return result;
5079#else
5081 return 0;
5082#endif /* not USE_LIBXML */
5083}
struct cursor * cur
Definition: ecpg.c:29
#define PG_FINALLY(...)
Definition: elog.h:388
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1530
void get_type_category_preferred(Oid typid, char *typcategory, bool *typispreferred)
Definition: lsyscache.c:2850

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

◆ XmlTableInitOpaque()

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

Definition at line 4695 of file xml.c.

4696{
4697#ifdef USE_LIBXML
4698 volatile xmlParserCtxtPtr ctxt = NULL;
4699 XmlTableBuilderData *xtCxt;
4700 PgXmlErrorContext *xmlerrcxt;
4701
4702 xtCxt = palloc0(sizeof(XmlTableBuilderData));
4703 xtCxt->magic = XMLTABLE_CONTEXT_MAGIC;
4704 xtCxt->natts = natts;
4705 xtCxt->xpathscomp = palloc0(sizeof(xmlXPathCompExprPtr) * natts);
4706
4708
4709 PG_TRY();
4710 {
4711 xmlInitParser();
4712
4713 ctxt = xmlNewParserCtxt();
4714 if (ctxt == NULL || xmlerrcxt->err_occurred)
4715 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
4716 "could not allocate parser context");
4717 }
4718 PG_CATCH();
4719 {
4720 if (ctxt != NULL)
4721 xmlFreeParserCtxt(ctxt);
4722
4723 pg_xml_done(xmlerrcxt, true);
4724
4725 PG_RE_THROW();
4726 }
4727 PG_END_TRY();
4728
4729 xtCxt->xmlerrcxt = xmlerrcxt;
4730 xtCxt->ctxt = ctxt;
4731
4732 state->opaque = xtCxt;
4733#else
4735#endif /* not USE_LIBXML */
4736}
void * palloc0(Size size)
Definition: mcxt.c:1351

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

◆ XmlTableSetColumnFilter()

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

Definition at line 4858 of file xml.c.

4859{
4860#ifdef USE_LIBXML
4861 XmlTableBuilderData *xtCxt;
4862 xmlChar *xstr;
4863
4864 Assert(PointerIsValid(path));
4865
4866 xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetColumnFilter");
4867
4868 if (*path == '\0')
4869 ereport(ERROR,
4870 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_XQUERY),
4871 errmsg("column path filter must not be empty string")));
4872
4873 xstr = pg_xmlCharStrndup(path, strlen(path));
4874
4875 /* We require XmlTableSetDocument to have been done already */
4876 Assert(xtCxt->xpathcxt != NULL);
4877
4878 xtCxt->xpathscomp[colnum] = xmlXPathCtxtCompile(xtCxt->xpathcxt, xstr);
4879 if (xtCxt->xpathscomp[colnum] == NULL || xtCxt->xmlerrcxt->err_occurred)
4880 xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
4881 "invalid XPath expression");
4882#else
4884#endif /* not USE_LIBXML */
4885}
#define PointerIsValid(pointer)
Definition: c.h:734

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

◆ XmlTableSetDocument()

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

Definition at line 4743 of file xml.c.

4744{
4745#ifdef USE_LIBXML
4746 XmlTableBuilderData *xtCxt;
4747 xmltype *xmlval = DatumGetXmlP(value);
4748 char *str;
4749 xmlChar *xstr;
4750 int length;
4751 volatile xmlDocPtr doc = NULL;
4752 volatile xmlXPathContextPtr xpathcxt = NULL;
4753
4754 xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetDocument");
4755
4756 /*
4757 * Use out function for casting to string (remove encoding property). See
4758 * comment in xml_out.
4759 */
4760 str = xml_out_internal(xmlval, 0);
4761
4762 length = strlen(str);
4763 xstr = pg_xmlCharStrndup(str, length);
4764
4765 PG_TRY();
4766 {
4767 doc = xmlCtxtReadMemory(xtCxt->ctxt, (char *) xstr, length, NULL, NULL, 0);
4768 if (doc == NULL || xtCxt->xmlerrcxt->err_occurred)
4769 xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INVALID_XML_DOCUMENT,
4770 "could not parse XML document");
4771 xpathcxt = xmlXPathNewContext(doc);
4772 if (xpathcxt == NULL || xtCxt->xmlerrcxt->err_occurred)
4773 xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
4774 "could not allocate XPath context");
4775 xpathcxt->node = (xmlNodePtr) doc;
4776 }
4777 PG_CATCH();
4778 {
4779 if (xpathcxt != NULL)
4780 xmlXPathFreeContext(xpathcxt);
4781 if (doc != NULL)
4782 xmlFreeDoc(doc);
4783
4784 PG_RE_THROW();
4785 }
4786 PG_END_TRY();
4787
4788 xtCxt->doc = doc;
4789 xtCxt->xpathcxt = xpathcxt;
4790#else
4792#endif /* not USE_LIBXML */
4793}

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

◆ XmlTableSetNamespace()

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

Definition at line 4800 of file xml.c.

4801{
4802#ifdef USE_LIBXML
4803 XmlTableBuilderData *xtCxt;
4804
4805 if (name == NULL)
4806 ereport(ERROR,
4807 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4808 errmsg("DEFAULT namespace is not supported")));
4809 xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetNamespace");
4810
4811 if (xmlXPathRegisterNs(xtCxt->xpathcxt,
4812 pg_xmlCharStrndup(name, strlen(name)),
4813 pg_xmlCharStrndup(uri, strlen(uri))))
4814 xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
4815 "could not set XML namespace");
4816#else
4818#endif /* not USE_LIBXML */
4819}

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

◆ XmlTableSetRowFilter()

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

Definition at line 4826 of file xml.c.

4827{
4828#ifdef USE_LIBXML
4829 XmlTableBuilderData *xtCxt;
4830 xmlChar *xstr;
4831
4832 xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetRowFilter");
4833
4834 if (*path == '\0')
4835 ereport(ERROR,
4836 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_XQUERY),
4837 errmsg("row path filter must not be empty string")));
4838
4839 xstr = pg_xmlCharStrndup(path, strlen(path));
4840
4841 /* We require XmlTableSetDocument to have been done already */
4842 Assert(xtCxt->xpathcxt != NULL);
4843
4844 xtCxt->xpathcomp = xmlXPathCtxtCompile(xtCxt->xpathcxt, xstr);
4845 if (xtCxt->xpathcomp == NULL || xtCxt->xmlerrcxt->err_occurred)
4846 xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
4847 "invalid XPath expression");
4848#else
4850#endif /* not USE_LIBXML */
4851}

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

◆ xmltext()

Datum xmltext ( PG_FUNCTION_ARGS  )

Definition at line 527 of file xml.c.

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

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

◆ xmltotext()

Datum xmltotext ( PG_FUNCTION_ARGS  )

Definition at line 646 of file xml.c.

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

References data, PG_GETARG_XML_P, and PG_RETURN_TEXT_P.

◆ xmltotext_with_options()

text * xmltotext_with_options ( xmltype data,
XmlOptionType  xmloption_arg,
bool  indent 
)

Definition at line 656 of file xml.c.

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

References buf, cstring_to_text_with_len(), data, ereport, errcode(), errmsg(), ERROR, ErrorSaveContext::error_occurred, GetDatabaseEncoding(), len, newline, NO_XML_SUPPORT, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, pg_xml_done(), pg_xml_init(), PG_XML_STRICTNESS_ALL, root, str, xml_ereport(), and XMLOPTION_DOCUMENT.

Referenced by ExecEvalXmlExpr().

◆ xmlvalidate()

Datum xmlvalidate ( PG_FUNCTION_ARGS  )

Definition at line 1114 of file xml.c.

1115{
1116 ereport(ERROR,
1117 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1118 errmsg("xmlvalidate is not implemented")));
1119 return 0;
1120}

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

◆ xpath()

Datum xpath ( PG_FUNCTION_ARGS  )

Definition at line 4532 of file xml.c.

4533{
4534#ifdef USE_LIBXML
4535 text *xpath_expr_text = PG_GETARG_TEXT_PP(0);
4537 ArrayType *namespaces = PG_GETARG_ARRAYTYPE_P(2);
4538 ArrayBuildState *astate;
4539
4540 astate = initArrayResult(XMLOID, CurrentMemoryContext, true);
4541 xpath_internal(xpath_expr_text, data, namespaces,
4542 NULL, astate);
4544#else
4546 return 0;
4547#endif
4548}
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:263
ArrayBuildState * initArrayResult(Oid element_type, MemoryContext rcontext, bool subcontext)
Definition: arrayfuncs.c:5293
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5420
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143

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

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

◆ xpath_exists()

Datum xpath_exists ( PG_FUNCTION_ARGS  )

Definition at line 4578 of file xml.c.

4579{
4580#ifdef USE_LIBXML
4581 text *xpath_expr_text = PG_GETARG_TEXT_PP(0);
4583 ArrayType *namespaces = PG_GETARG_ARRAYTYPE_P(2);
4584 int res_nitems;
4585
4586 xpath_internal(xpath_expr_text, data, namespaces,
4587 &res_nitems, NULL);
4588
4589 PG_RETURN_BOOL(res_nitems > 0);
4590#else
4592 return 0;
4593#endif
4594}

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

◆ xsd_schema_element_end()

static void xsd_schema_element_end ( StringInfo  result)
static

Definition at line 3274 of file xml.c.

3275{
3276 appendStringInfoString(result, "</xsd:schema>");
3277}

References appendStringInfoString().

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

◆ xsd_schema_element_start()

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

Definition at line 3257 of file xml.c.

3258{
3260 "<xsd:schema\n"
3261 " xmlns:xsd=\"" NAMESPACE_XSD "\"");
3262 if (strlen(targetns) > 0)
3263 appendStringInfo(result,
3264 "\n"
3265 " targetNamespace=\"%s\"\n"
3266 " elementFormDefault=\"qualified\"",
3267 targetns);
3269 ">\n\n");
3270}
#define NAMESPACE_XSD
Definition: xml.c:243

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

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

Variable Documentation

◆ xmlbinary

int xmlbinary = XMLBINARY_BASE64

Definition at line 109 of file xml.c.

Referenced by map_sql_type_to_xmlschema_type(), and map_sql_value_to_xml_value().

◆ xmloption

int xmloption = XMLOPTION_CONTENT

Definition at line 110 of file xml.c.

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

◆ XmlTableRoutine

const TableFuncRoutine XmlTableRoutine
Initial value:
=
{
.InitOpaque = XmlTableInitOpaque,
.SetDocument = XmlTableSetDocument,
.SetNamespace = XmlTableSetNamespace,
.SetRowFilter = XmlTableSetRowFilter,
.SetColumnFilter = XmlTableSetColumnFilter,
.FetchRow = XmlTableFetchRow,
.GetValue = XmlTableGetValue,
.DestroyOpaque = XmlTableDestroyOpaque
}
static void XmlTableInitOpaque(struct TableFuncScanState *state, int natts)
Definition: xml.c:4695
static void XmlTableSetNamespace(struct TableFuncScanState *state, const char *name, const char *uri)
Definition: xml.c:4800
static void XmlTableSetRowFilter(struct TableFuncScanState *state, const char *path)
Definition: xml.c:4826
static Datum XmlTableGetValue(struct TableFuncScanState *state, int colnum, Oid typid, int32 typmod, bool *isnull)
Definition: xml.c:4938
static void XmlTableSetDocument(struct TableFuncScanState *state, Datum value)
Definition: xml.c:4743
static void XmlTableDestroyOpaque(struct TableFuncScanState *state)
Definition: xml.c:5090
static bool XmlTableFetchRow(struct TableFuncScanState *state)
Definition: xml.c:4893
static void XmlTableSetColumnFilter(struct TableFuncScanState *state, const char *path, int colnum)
Definition: xml.c:4858

Definition at line 223 of file xml.c.

Referenced by ExecInitTableFuncScan().