PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
xml.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/dbcommands.h"
#include "executor/executor.h"
#include "executor/spi.h"
#include "fmgr.h"
#include "lib/stringinfo.h"
#include "libpq/pqformat.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "nodes/execnodes.h"
#include "nodes/nodeFuncs.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/datetime.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/xml.h"
Include dependency graph for xml.c:

Go to the source code of this file.

Macros

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

Functions

static 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)
 
Datum xml_in (PG_FUNCTION_ARGS)
 
static char * xml_out_internal (xmltype *x, pg_enc target_encoding)
 
Datum xml_out (PG_FUNCTION_ARGS)
 
Datum xml_recv (PG_FUNCTION_ARGS)
 
Datum xml_send (PG_FUNCTION_ARGS)
 
static xmltypestringinfo_to_xmltype (StringInfo buf)
 
static xmltypecstring_to_xmltype (const char *string)
 
Datum xmlcomment (PG_FUNCTION_ARGS)
 
xmltypexmlconcat (List *args)
 
Datum xmlconcat2 (PG_FUNCTION_ARGS)
 
Datum texttoxml (PG_FUNCTION_ARGS)
 
Datum xmltotext (PG_FUNCTION_ARGS)
 
textxmltotext_with_xmloption (xmltype *data, XmlOptionType xmloption_arg)
 
xmltypexmlelement (XmlExprState *xmlExpr, ExprContext *econtext)
 
xmltypexmlparse (text *data, XmlOptionType xmloption_arg, bool preserve_whitespace)
 
xmltypexmlpi (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 (char *ident, bool fully_escaped, bool escape_period)
 
static char * unicode_to_sqlchar (pg_wchar c)
 
char * map_xml_name_to_sql_identifier (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)
 
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)
 
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 (char *a, char *b, char *c, char *d)
 
Datum xpath (PG_FUNCTION_ARGS)
 
Datum xmlexists (PG_FUNCTION_ARGS)
 
Datum xpath_exists (PG_FUNCTION_ARGS)
 
Datum xml_is_well_formed (PG_FUNCTION_ARGS)
 
Datum xml_is_well_formed_document (PG_FUNCTION_ARGS)
 
Datum xml_is_well_formed_content (PG_FUNCTION_ARGS)
 

Variables

int xmlbinary
 
int xmloption
 

Macro Definition Documentation

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

Definition at line 179 of file xml.c.

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

Definition at line 177 of file xml.c.

Referenced by xsd_schema_element_start().

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

Definition at line 178 of file xml.c.

Referenced by xmldata_root_element_start().

#define NO_XML_SUPPORT ( )
Value:
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
errmsg("unsupported XML feature"), \
errdetail("This functionality requires the server to be built with libxml support."), \
errhint("You need to rebuild PostgreSQL using --with-libxml.")))
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797

Definition at line 168 of file xml.c.

Referenced by map_sql_identifier_to_xml_name(), xml_in(), xml_is_document(), xml_is_well_formed(), xml_is_well_formed_content(), xml_is_well_formed_document(), xml_recv(), xmlcomment(), xmlconcat(), xmlelement(), xmlexists(), xmlparse(), xmlpi(), xmlroot(), xpath(), and xpath_exists().

#define PG_XML_DEFAULT_VERSION   "1.0"

Definition at line 231 of file xml.c.

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

Referenced by database_get_xml_visible_schemas(), and database_get_xml_visible_tables().

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

Definition at line 2299 of file xml.c.

Function Documentation

static char* _SPI_strdup ( const char *  s)
static

Definition at line 2202 of file xml.c.

References SPI_palloc().

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

2203 {
2204  size_t len = strlen(s) + 1;
2205  char *ret = SPI_palloc(len);
2206 
2207  memcpy(ret, s, len);
2208  return ret;
2209 }
void * SPI_palloc(Size size)
Definition: spi.c:921
static xmltype* cstring_to_xmltype ( const char *  string)
static

Definition at line 403 of file xml.c.

References cstring_to_text().

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

404 {
405  return (xmltype *) cstring_to_text(string);
406 }
text * cstring_to_text(const char *s)
Definition: varlena.c:151
Definition: c.h:434
Datum cursor_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 2370 of file xml.c.

References ereport, errcode(), errmsg(), ERROR, i, initStringInfo(), NULL, 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(), and text_to_cstring().

2371 {
2374  bool nulls = PG_GETARG_BOOL(2);
2375  bool tableforest = PG_GETARG_BOOL(3);
2376  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(4));
2377 
2378  StringInfoData result;
2379  Portal portal;
2380  uint64 i;
2381 
2382  initStringInfo(&result);
2383 
2384  SPI_connect();
2385  portal = SPI_cursor_find(name);
2386  if (portal == NULL)
2387  ereport(ERROR,
2388  (errcode(ERRCODE_UNDEFINED_CURSOR),
2389  errmsg("cursor \"%s\" does not exist", name)));
2390 
2391  SPI_cursor_fetch(portal, true, count);
2392  for (i = 0; i < SPI_processed; i++)
2393  SPI_sql_row_to_xmlelement(i, &result, NULL, nulls,
2394  tableforest, targetns, true);
2395 
2396  SPI_finish();
2397 
2399 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
int SPI_connect(void)
Definition: spi.c:84
int SPI_finish(void)
Definition: spi.c:147
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:230
uint64 SPI_processed
Definition: spi.c:39
signed int int32
Definition: c.h:253
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
Portal SPI_cursor_find(const char *name)
Definition: spi.c:1331
#define ERROR
Definition: elog.h:43
#define PG_RETURN_XML_P(x)
Definition: xml.h:53
#define ereport(elevel, rest)
Definition: elog.h:122
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
#define NULL
Definition: c.h:226
const char * name
Definition: encode.c:521
char * text_to_cstring(const text *t)
Definition: varlena.c:184
int errmsg(const char *fmt,...)
Definition: elog.c:797
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:396
int i
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Definition: spi.c:1343
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:3527
Datum cursor_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2542 of file xml.c.

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

2543 {
2545  bool nulls = PG_GETARG_BOOL(1);
2546  bool tableforest = PG_GETARG_BOOL(2);
2547  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2548  const char *xmlschema;
2549  Portal portal;
2550 
2551  SPI_connect();
2552  portal = SPI_cursor_find(name);
2553  if (portal == NULL)
2554  ereport(ERROR,
2555  (errcode(ERRCODE_UNDEFINED_CURSOR),
2556  errmsg("cursor \"%s\" does not exist", name)));
2557 
2558  xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2559  InvalidOid, nulls,
2560  tableforest, targetns));
2561  SPI_finish();
2562 
2563  PG_RETURN_XML_P(cstring_to_xmltype(xmlschema));
2564 }
int SPI_connect(void)
Definition: spi.c:84
int SPI_finish(void)
Definition: spi.c:147
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:230
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
Portal SPI_cursor_find(const char *name)
Definition: spi.c:1331
#define ERROR
Definition: elog.h:43
static char * _SPI_strdup(const char *s)
Definition: xml.c:2202
#define PG_RETURN_XML_P(x)
Definition: xml.h:53
static const char * map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:2960
#define ereport(elevel, rest)
Definition: elog.h:122
TupleDesc tupDesc
Definition: portal.h:153
#define InvalidOid
Definition: postgres_ext.h:36
static xmltype * cstring_to_xmltype(const char *string)
Definition: xml.c:403
#define NULL
Definition: c.h:226
const char * name
Definition: encode.c:521
char * text_to_cstring(const text *t)
Definition: varlena.c:184
int errmsg(const char *fmt,...)
Definition: elog.c:797
static List* database_get_xml_visible_schemas ( void  )
static

Definition at line 2305 of file xml.c.

References query_to_oid_list(), and XML_VISIBLE_SCHEMAS.

Referenced by database_to_xml_internal(), and database_to_xmlschema_internal().

2306 {
2307  return query_to_oid_list(XML_VISIBLE_SCHEMAS " ORDER BY nspname;");
2308 }
static List * query_to_oid_list(const char *query)
Definition: xml.c:2259
#define XML_VISIBLE_SCHEMAS
Definition: xml.c:2301
static List* database_get_xml_visible_tables ( void  )
static

Definition at line 2312 of file xml.c.

References query_to_oid_list(), and XML_VISIBLE_SCHEMAS.

Referenced by database_to_xmlschema_internal().

2313 {
2314  /* At the moment there is no order required here. */
2315  return query_to_oid_list("SELECT oid FROM pg_catalog.pg_class WHERE relkind IN ('r', 'm', 'v') AND pg_catalog.has_table_privilege (pg_class.oid, 'SELECT') AND relnamespace IN (" XML_VISIBLE_SCHEMAS ");");
2316 }
static List * query_to_oid_list(const char *query)
Definition: xml.c:2259
#define XML_VISIBLE_SCHEMAS
Definition: xml.c:2301
Datum database_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 2843 of file xml.c.

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

2844 {
2845  bool nulls = PG_GETARG_BOOL(0);
2846  bool tableforest = PG_GETARG_BOOL(1);
2847  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
2848 
2850  tableforest, targetns)));
2851 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:230
static StringInfo database_to_xml_internal(const char *xmlschema, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:2800
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define PG_RETURN_XML_P(x)
Definition: xml.h:53
#define NULL
Definition: c.h:226
char * text_to_cstring(const text *t)
Definition: varlena.c:184
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:396
Datum database_to_xml_and_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2910 of file xml.c.

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

2911 {
2912  bool nulls = PG_GETARG_BOOL(0);
2913  bool tableforest = PG_GETARG_BOOL(1);
2914  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
2915  StringInfo xmlschema;
2916 
2917  xmlschema = database_to_xmlschema_internal(nulls, tableforest, targetns);
2918 
2920  nulls, tableforest, targetns)));
2921 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:230
static StringInfo database_to_xml_internal(const char *xmlschema, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:2800
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define PG_RETURN_XML_P(x)
Definition: xml.h:53
static StringInfo database_to_xmlschema_internal(bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:2855
char * text_to_cstring(const text *t)
Definition: varlena.c:184
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:396
static StringInfo database_to_xml_internal ( const char *  xmlschema,
bool  nulls,
bool  tableforest,
const char *  targetns 
)
static

Definition at line 2800 of file xml.c.

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), StringInfoData::data, database_get_xml_visible_schemas(), get_database_name(), lfirst_oid, makeStringInfo(), map_sql_identifier_to_xml_name(), MyDatabaseId, NULL, 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().

2802 {
2803  StringInfo result;
2804  List *nspid_list;
2805  ListCell *cell;
2806  char *xmlcn;
2807 
2809  true, false);
2810  result = makeStringInfo();
2811 
2812  xmldata_root_element_start(result, xmlcn, xmlschema, targetns, true);
2813  appendStringInfoChar(result, '\n');
2814 
2815  if (xmlschema)
2816  appendStringInfo(result, "%s\n\n", xmlschema);
2817 
2818  SPI_connect();
2819 
2820  nspid_list = database_get_xml_visible_schemas();
2821 
2822  foreach(cell, nspid_list)
2823  {
2824  Oid nspid = lfirst_oid(cell);
2825  StringInfo subres;
2826 
2827  subres = schema_to_xml_internal(nspid, NULL, nulls,
2828  tableforest, targetns, false);
2829 
2830  appendStringInfoString(result, subres->data);
2831  appendStringInfoChar(result, '\n');
2832  }
2833 
2834  SPI_finish();
2835 
2836  xmldata_root_element_end(result, xmlcn);
2837 
2838  return result;
2839 }
static void xmldata_root_element_start(StringInfo result, const char *eltname, const char *xmlschema, const char *targetns, bool top_level)
Definition: xml.c:2415
static StringInfo schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2625
int SPI_connect(void)
Definition: spi.c:84
int SPI_finish(void)
Definition: spi.c:147
StringInfo makeStringInfo(void)
Definition: stringinfo.c:29
unsigned int Oid
Definition: postgres_ext.h:31
char * map_sql_identifier_to_xml_name(char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:1835
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2049
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
static List * database_get_xml_visible_schemas(void)
Definition: xml.c:2305
Oid MyDatabaseId
Definition: globals.c:76
#define NULL
Definition: c.h:226
static void xmldata_root_element_end(StringInfo result, const char *eltname)
Definition: xml.c:2442
Definition: pg_list.h:45
#define lfirst_oid(lc)
Definition: pg_list.h:108
Datum database_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2898 of file xml.c.

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

2899 {
2900  bool nulls = PG_GETARG_BOOL(0);
2901  bool tableforest = PG_GETARG_BOOL(1);
2902  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
2903 
2905  tableforest, targetns)));
2906 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:230
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define PG_RETURN_XML_P(x)
Definition: xml.h:53
static StringInfo database_to_xmlschema_internal(bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:2855
char * text_to_cstring(const text *t)
Definition: varlena.c:184
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:396
static StringInfo database_to_xmlschema_internal ( bool  nulls,
bool  tableforest,
const char *  targetns 
)
static

Definition at line 2855 of file xml.c.

References AccessShareLock, appendStringInfoString(), CreateTupleDescCopy(), database_get_xml_visible_schemas(), database_get_xml_visible_tables(), heap_close, heap_open(), 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(), xsd_schema_element_end(), and xsd_schema_element_start().

Referenced by database_to_xml_and_xmlschema(), and database_to_xmlschema().

2857 {
2858  List *relid_list;
2859  List *nspid_list;
2860  List *tupdesc_list;
2861  ListCell *cell;
2862  StringInfo result;
2863 
2864  result = makeStringInfo();
2865 
2866  xsd_schema_element_start(result, targetns);
2867 
2868  SPI_connect();
2869 
2870  relid_list = database_get_xml_visible_tables();
2871  nspid_list = database_get_xml_visible_schemas();
2872 
2873  tupdesc_list = NIL;
2874  foreach(cell, relid_list)
2875  {
2876  Relation rel;
2877 
2878  rel = heap_open(lfirst_oid(cell), AccessShareLock);
2879  tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2880  heap_close(rel, NoLock);
2881  }
2882 
2883  appendStringInfoString(result,
2884  map_sql_typecoll_to_xmlschema_types(tupdesc_list));
2885 
2886  appendStringInfoString(result,
2887  map_sql_catalog_to_xmlschema_types(nspid_list, nulls, tableforest, targetns));
2888 
2889  xsd_schema_element_end(result);
2890 
2891  SPI_finish();
2892 
2893  return result;
2894 }
static void xsd_schema_element_start(StringInfo result, const char *targetns)
Definition: xml.c:2690
#define NIL
Definition: pg_list.h:69
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:141
int SPI_connect(void)
Definition: spi.c:84
int SPI_finish(void)
Definition: spi.c:147
StringInfo makeStringInfo(void)
Definition: stringinfo.c:29
#define AccessShareLock
Definition: lockdefs.h:36
static const char * map_sql_catalog_to_xmlschema_types(List *nspid_list, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3136
#define heap_close(r, l)
Definition: heapam.h:97
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
#define NoLock
Definition: lockdefs.h:34
List * lappend(List *list, void *datum)
Definition: list.c:128
static const char * map_sql_typecoll_to_xmlschema_types(List *tupdesc_list)
Definition: xml.c:3298
static List * database_get_xml_visible_schemas(void)
Definition: xml.c:2305
static void xsd_schema_element_end(StringInfo result)
Definition: xml.c:2707
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
TupleDesc rd_att
Definition: rel.h:114
Definition: pg_list.h:45
#define lfirst_oid(lc)
Definition: pg_list.h:108
static List * database_get_xml_visible_tables(void)
Definition: xml.c:2312
char* escape_xml ( const char *  str)

Definition at line 2170 of file xml.c.

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

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

2171 {
2173  const char *p;
2174 
2175  initStringInfo(&buf);
2176  for (p = str; *p; p++)
2177  {
2178  switch (*p)
2179  {
2180  case '&':
2181  appendStringInfoString(&buf, "&amp;");
2182  break;
2183  case '<':
2184  appendStringInfoString(&buf, "&lt;");
2185  break;
2186  case '>':
2187  appendStringInfoString(&buf, "&gt;");
2188  break;
2189  case '\r':
2190  appendStringInfoString(&buf, "&#x0d;");
2191  break;
2192  default:
2193  appendStringInfoCharMacro(&buf, *p);
2194  break;
2195  }
2196  }
2197  return buf.data;
2198 }
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:135
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:65
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
static char* map_multipart_sql_identifier_to_xml_name ( char *  a,
char *  b,
char *  c,
char *  d 
)
static

Definition at line 2929 of file xml.c.

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

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

2930 {
2931  StringInfoData result;
2932 
2933  initStringInfo(&result);
2934 
2935  if (a)
2936  appendStringInfoString(&result,
2937  map_sql_identifier_to_xml_name(a, true, true));
2938  if (b)
2939  appendStringInfo(&result, ".%s",
2940  map_sql_identifier_to_xml_name(b, true, true));
2941  if (c)
2942  appendStringInfo(&result, ".%s",
2943  map_sql_identifier_to_xml_name(c, true, true));
2944  if (d)
2945  appendStringInfo(&result, ".%s",
2946  map_sql_identifier_to_xml_name(d, true, true));
2947 
2948  return result.data;
2949 }
char * map_sql_identifier_to_xml_name(char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:1835
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
char * c
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
static const char * map_sql_catalog_to_xmlschema_types ( List nspid_list,
bool  nulls,
bool  tableforest,
const char *  targetns 
)
static

Definition at line 3136 of file xml.c.

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

Referenced by database_to_xmlschema_internal().

3138 {
3139  char *dbname;
3140  char *xmlcn;
3141  char *catalogtypename;
3142  StringInfoData result;
3143  ListCell *cell;
3144 
3145  dbname = get_database_name(MyDatabaseId);
3146 
3147  initStringInfo(&result);
3148 
3149  xmlcn = map_sql_identifier_to_xml_name(dbname, true, false);
3150 
3151  catalogtypename = map_multipart_sql_identifier_to_xml_name("CatalogType",
3152  dbname,
3153  NULL,
3154  NULL);
3155 
3156  appendStringInfo(&result,
3157  "<xsd:complexType name=\"%s\">\n", catalogtypename);
3158  appendStringInfoString(&result,
3159  " <xsd:all>\n");
3160 
3161  foreach(cell, nspid_list)
3162  {
3163  Oid nspid = lfirst_oid(cell);
3164  char *nspname = get_namespace_name(nspid);
3165  char *xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
3166  char *schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
3167  dbname,
3168  nspname,
3169  NULL);
3170 
3171  appendStringInfo(&result,
3172  " <xsd:element name=\"%s\" type=\"%s\"/>\n",
3173  xmlsn, schematypename);
3174  }
3175 
3176  appendStringInfoString(&result,
3177  " </xsd:all>\n");
3178  appendStringInfoString(&result,
3179  "</xsd:complexType>\n\n");
3180 
3181  appendStringInfo(&result,
3182  "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3183  xmlcn, catalogtypename);
3184 
3185  return result.data;
3186 }
unsigned int Oid
Definition: postgres_ext.h:31
static char * map_multipart_sql_identifier_to_xml_name(char *a, char *b, char *c, char *d)
Definition: xml.c:2929
char * map_sql_identifier_to_xml_name(char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:1835
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2049
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3006
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
Oid MyDatabaseId
Definition: globals.c:76
#define NULL
Definition: c.h:226
char * dbname
Definition: streamutil.c:41
#define lfirst_oid(lc)
Definition: pg_list.h:108
char* map_sql_identifier_to_xml_name ( char *  ident,
bool  fully_escaped,
bool  escape_period 
)

Definition at line 1835 of file xml.c.

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

1837 {
1838 #ifdef USE_LIBXML
1840  char *p;
1841 
1842  /*
1843  * SQL/XML doesn't make use of this case anywhere, so it's probably a
1844  * mistake.
1845  */
1846  Assert(fully_escaped || !escape_period);
1847 
1848  initStringInfo(&buf);
1849 
1850  for (p = ident; *p; p += pg_mblen(p))
1851  {
1852  if (*p == ':' && (p == ident || fully_escaped))
1853  appendStringInfoString(&buf, "_x003A_");
1854  else if (*p == '_' && *(p + 1) == 'x')
1855  appendStringInfoString(&buf, "_x005F_");
1856  else if (fully_escaped && p == ident &&
1857  pg_strncasecmp(p, "xml", 3) == 0)
1858  {
1859  if (*p == 'x')
1860  appendStringInfoString(&buf, "_x0078_");
1861  else
1862  appendStringInfoString(&buf, "_x0058_");
1863  }
1864  else if (escape_period && *p == '.')
1865  appendStringInfoString(&buf, "_x002E_");
1866  else
1867  {
1868  pg_wchar u = sqlchar_to_unicode(p);
1869 
1870  if ((p == ident)
1871  ? !is_valid_xml_namefirst(u)
1872  : !is_valid_xml_namechar(u))
1873  appendStringInfo(&buf, "_x%04X_", (unsigned int) u);
1874  else
1875  appendBinaryStringInfo(&buf, p, pg_mblen(p));
1876  }
1877  }
1878 
1879  return buf.data;
1880 #else /* not USE_LIBXML */
1881  NO_XML_SUPPORT();
1882  return NULL;
1883 #endif /* not USE_LIBXML */
1884 }
#define NO_XML_SUPPORT()
Definition: xml.c:168
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:65
unsigned int pg_wchar
Definition: mbprint.c:31
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
int pg_mblen(const char *mbstr)
Definition: mbutils.c:771
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:240
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 3063 of file xml.c.

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

Referenced by schema_to_xmlschema_internal().

3065 {
3066  char *dbname;
3067  char *nspname;
3068  char *xmlsn;
3069  char *schematypename;
3070  StringInfoData result;
3071  ListCell *cell;
3072 
3073  dbname = get_database_name(MyDatabaseId);
3074  nspname = get_namespace_name(nspid);
3075 
3076  initStringInfo(&result);
3077 
3078  xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
3079 
3080  schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
3081  dbname,
3082  nspname,
3083  NULL);
3084 
3085  appendStringInfo(&result,
3086  "<xsd:complexType name=\"%s\">\n", schematypename);
3087  if (!tableforest)
3088  appendStringInfoString(&result,
3089  " <xsd:all>\n");
3090  else
3091  appendStringInfoString(&result,
3092  " <xsd:sequence>\n");
3093 
3094  foreach(cell, relid_list)
3095  {
3096  Oid relid = lfirst_oid(cell);
3097  char *relname = get_rel_name(relid);
3098  char *xmltn = map_sql_identifier_to_xml_name(relname, true, false);
3099  char *tabletypename = map_multipart_sql_identifier_to_xml_name(tableforest ? "RowType" : "TableType",
3100  dbname,
3101  nspname,
3102  relname);
3103 
3104  if (!tableforest)
3105  appendStringInfo(&result,
3106  " <xsd:element name=\"%s\" type=\"%s\"/>\n",
3107  xmltn, tabletypename);
3108  else
3109  appendStringInfo(&result,
3110  " <xsd:element name=\"%s\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n",
3111  xmltn, tabletypename);
3112  }
3113 
3114  if (!tableforest)
3115  appendStringInfoString(&result,
3116  " </xsd:all>\n");
3117  else
3118  appendStringInfoString(&result,
3119  " </xsd:sequence>\n");
3120  appendStringInfoString(&result,
3121  "</xsd:complexType>\n\n");
3122 
3123  appendStringInfo(&result,
3124  "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3125  xmlsn, schematypename);
3126 
3127  return result.data;
3128 }
unsigned int Oid
Definition: postgres_ext.h:31
static char * map_multipart_sql_identifier_to_xml_name(char *a, char *b, char *c, char *d)
Definition: xml.c:2929
char * map_sql_identifier_to_xml_name(char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:1835
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2049
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3006
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
Oid MyDatabaseId
Definition: globals.c:76
#define NULL
Definition: c.h:226
char * dbname
Definition: streamutil.c:41
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1694
#define lfirst_oid(lc)
Definition: pg_list.h:108
static const char * map_sql_table_to_xmlschema ( TupleDesc  tupdesc,
Oid  relid,
bool  nulls,
bool  tableforest,
const char *  targetns 
)
static

Definition at line 2960 of file xml.c.

References appendStringInfo(), appendStringInfoString(), tupleDesc::attrs, 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, tupleDesc::natts, ObjectIdGetDatum, OidIsValid, ReleaseSysCache(), RELOID, SearchSysCache1, 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().

2962 {
2963  int i;
2964  char *xmltn;
2965  char *tabletypename;
2966  char *rowtypename;
2967  StringInfoData result;
2968 
2969  initStringInfo(&result);
2970 
2971  if (OidIsValid(relid))
2972  {
2973  HeapTuple tuple;
2974  Form_pg_class reltuple;
2975 
2976  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
2977  if (!HeapTupleIsValid(tuple))
2978  elog(ERROR, "cache lookup failed for relation %u", relid);
2979  reltuple = (Form_pg_class) GETSTRUCT(tuple);
2980 
2981  xmltn = map_sql_identifier_to_xml_name(NameStr(reltuple->relname),
2982  true, false);
2983 
2984  tabletypename = map_multipart_sql_identifier_to_xml_name("TableType",
2986  get_namespace_name(reltuple->relnamespace),
2987  NameStr(reltuple->relname));
2988 
2989  rowtypename = map_multipart_sql_identifier_to_xml_name("RowType",
2991  get_namespace_name(reltuple->relnamespace),
2992  NameStr(reltuple->relname));
2993 
2994  ReleaseSysCache(tuple);
2995  }
2996  else
2997  {
2998  if (tableforest)
2999  xmltn = "row";
3000  else
3001  xmltn = "table";
3002 
3003  tabletypename = "TableType";
3004  rowtypename = "RowType";
3005  }
3006 
3007  xsd_schema_element_start(&result, targetns);
3008 
3009  appendStringInfoString(&result,
3011 
3012  appendStringInfo(&result,
3013  "<xsd:complexType name=\"%s\">\n"
3014  " <xsd:sequence>\n",
3015  rowtypename);
3016 
3017  for (i = 0; i < tupdesc->natts; i++)
3018  {
3019  if (tupdesc->attrs[i]->attisdropped)
3020  continue;
3021  appendStringInfo(&result,
3022  " <xsd:element name=\"%s\" type=\"%s\"%s></xsd:element>\n",
3023  map_sql_identifier_to_xml_name(NameStr(tupdesc->attrs[i]->attname),
3024  true, false),
3025  map_sql_type_to_xml_name(tupdesc->attrs[i]->atttypid, -1),
3026  nulls ? " nillable=\"true\"" : " minOccurs=\"0\"");
3027  }
3028 
3029  appendStringInfoString(&result,
3030  " </xsd:sequence>\n"
3031  "</xsd:complexType>\n\n");
3032 
3033  if (!tableforest)
3034  {
3035  appendStringInfo(&result,
3036  "<xsd:complexType name=\"%s\">\n"
3037  " <xsd:sequence>\n"
3038  " <xsd:element name=\"row\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n"
3039  " </xsd:sequence>\n"
3040  "</xsd:complexType>\n\n",
3041  tabletypename, rowtypename);
3042 
3043  appendStringInfo(&result,
3044  "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3045  xmltn, tabletypename);
3046  }
3047  else
3048  appendStringInfo(&result,
3049  "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3050  xmltn, rowtypename);
3051 
3052  xsd_schema_element_end(&result);
3053 
3054  return result.data;
3055 }
static void xsd_schema_element_start(StringInfo result, const char *targetns)
Definition: xml.c:2690
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Form_pg_attribute * attrs
Definition: tupdesc.h:74
static char * map_multipart_sql_identifier_to_xml_name(char *a, char *b, char *c, char *d)
Definition: xml.c:2929
#define OidIsValid(objectId)
Definition: c.h:533
int natts
Definition: tupdesc.h:73
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:149
#define list_make1(x1)
Definition: pg_list.h:133
char * map_sql_identifier_to_xml_name(char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:1835
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2049
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3006
static const char * map_sql_typecoll_to_xmlschema_types(List *tupdesc_list)
Definition: xml.c:3298
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
static const char * map_sql_type_to_xml_name(Oid typeoid, int typmod)
Definition: xml.c:3193
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1083
static void xsd_schema_element_end(StringInfo result)
Definition: xml.c:2707
Oid MyDatabaseId
Definition: globals.c:76
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
FormData_pg_class * Form_pg_class
Definition: pg_class.h:95
int i
#define NameStr(name)
Definition: c.h:494
#define elog
Definition: elog.h:219
static const char * map_sql_type_to_xml_name ( Oid  typeoid,
int  typmod 
)
static

Definition at line 3193 of file xml.c.

References appendStringInfo(), appendStringInfoString(), BOOLOID, BPCHAROID, StringInfoData::data, DATEOID, elog, ERROR, FLOAT4OID, FLOAT8OID, get_database_name(), get_namespace_name(), GETSTRUCT, HeapTupleIsValid, initStringInfo(), INT2OID, INT4OID, INT8OID, map_multipart_sql_identifier_to_xml_name(), MyDatabaseId, NameStr, NUMERICOID, ObjectIdGetDatum, ReleaseSysCache(), SearchSysCache1, TIMEOID, TIMESTAMPOID, TIMESTAMPTZOID, TIMETZOID, TYPEOID, TYPTYPE_DOMAIN, VARCHAROID, VARHDRSZ, and XMLOID.

Referenced by map_sql_table_to_xmlschema(), and map_sql_type_to_xmlschema_type().

3194 {
3195  StringInfoData result;
3196 
3197  initStringInfo(&result);
3198 
3199  switch (typeoid)
3200  {
3201  case BPCHAROID:
3202  if (typmod == -1)
3203  appendStringInfoString(&result, "CHAR");
3204  else
3205  appendStringInfo(&result, "CHAR_%d", typmod - VARHDRSZ);
3206  break;
3207  case VARCHAROID:
3208  if (typmod == -1)
3209  appendStringInfoString(&result, "VARCHAR");
3210  else
3211  appendStringInfo(&result, "VARCHAR_%d", typmod - VARHDRSZ);
3212  break;
3213  case NUMERICOID:
3214  if (typmod == -1)
3215  appendStringInfoString(&result, "NUMERIC");
3216  else
3217  appendStringInfo(&result, "NUMERIC_%d_%d",
3218  ((typmod - VARHDRSZ) >> 16) & 0xffff,
3219  (typmod - VARHDRSZ) & 0xffff);
3220  break;
3221  case INT4OID:
3222  appendStringInfoString(&result, "INTEGER");
3223  break;
3224  case INT2OID:
3225  appendStringInfoString(&result, "SMALLINT");
3226  break;
3227  case INT8OID:
3228  appendStringInfoString(&result, "BIGINT");
3229  break;
3230  case FLOAT4OID:
3231  appendStringInfoString(&result, "REAL");
3232  break;
3233  case FLOAT8OID:
3234  appendStringInfoString(&result, "DOUBLE");
3235  break;
3236  case BOOLOID:
3237  appendStringInfoString(&result, "BOOLEAN");
3238  break;
3239  case TIMEOID:
3240  if (typmod == -1)
3241  appendStringInfoString(&result, "TIME");
3242  else
3243  appendStringInfo(&result, "TIME_%d", typmod);
3244  break;
3245  case TIMETZOID:
3246  if (typmod == -1)
3247  appendStringInfoString(&result, "TIME_WTZ");
3248  else
3249  appendStringInfo(&result, "TIME_WTZ_%d", typmod);
3250  break;
3251  case TIMESTAMPOID:
3252  if (typmod == -1)
3253  appendStringInfoString(&result, "TIMESTAMP");
3254  else
3255  appendStringInfo(&result, "TIMESTAMP_%d", typmod);
3256  break;
3257  case TIMESTAMPTZOID:
3258  if (typmod == -1)
3259  appendStringInfoString(&result, "TIMESTAMP_WTZ");
3260  else
3261  appendStringInfo(&result, "TIMESTAMP_WTZ_%d", typmod);
3262  break;
3263  case DATEOID:
3264  appendStringInfoString(&result, "DATE");
3265  break;
3266  case XMLOID:
3267  appendStringInfoString(&result, "XML");
3268  break;
3269  default:
3270  {
3271  HeapTuple tuple;
3272  Form_pg_type typtuple;
3273 
3274  tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typeoid));
3275  if (!HeapTupleIsValid(tuple))
3276  elog(ERROR, "cache lookup failed for type %u", typeoid);
3277  typtuple = (Form_pg_type) GETSTRUCT(tuple);
3278 
3279  appendStringInfoString(&result,
3280  map_multipart_sql_identifier_to_xml_name((typtuple->typtype == TYPTYPE_DOMAIN) ? "Domain" : "UDT",
3282  get_namespace_name(typtuple->typnamespace),
3283  NameStr(typtuple->typname)));
3284 
3285  ReleaseSysCache(tuple);
3286  }
3287  }
3288 
3289  return result.data;
3290 }
#define TIMESTAMPTZOID
Definition: pg_type.h:513
#define TIMEOID
Definition: pg_type.h:502
#define TYPTYPE_DOMAIN
Definition: pg_type.h:710
#define BPCHAROID
Definition: pg_type.h:492
#define DATEOID
Definition: pg_type.h:499
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define NUMERICOID
Definition: pg_type.h:542
#define VARHDRSZ
Definition: c.h:440
#define INT4OID
Definition: pg_type.h:316
FormData_pg_type * Form_pg_type
Definition: pg_type.h:233
static char * map_multipart_sql_identifier_to_xml_name(char *a, char *b, char *c, char *d)
Definition: xml.c:2929
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:149
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define TIMESTAMPOID
Definition: pg_type.h:507
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2049
#define INT2OID
Definition: pg_type.h:308
#define XMLOID
Definition: pg_type.h:359
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3006
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
#define VARCHAROID
Definition: pg_type.h:495
#define FLOAT4OID
Definition: pg_type.h:408
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1083
Oid MyDatabaseId
Definition: globals.c:76
#define INT8OID
Definition: pg_type.h:304
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define TIMETZOID
Definition: pg_type.h:524
#define FLOAT8OID
Definition: pg_type.h:411
#define BOOLOID
Definition: pg_type.h:288
#define NameStr(name)
Definition: c.h:494
#define elog
Definition: elog.h:219
static const char * map_sql_type_to_xmlschema_type ( Oid  typeoid,
int  typmod 
)
static

Definition at line 3352 of file xml.c.

References appendStringInfo(), appendStringInfoString(), BOOLOID, BPCHAROID, BYTEAOID, StringInfoData::data, DATEOID, FLOAT4OID, FLOAT8OID, get_typtype(), getBaseTypeAndTypmod(), initStringInfo(), INT2OID, INT4OID, INT64_FORMAT, INT8OID, map_sql_type_to_xml_name(), NUMERICOID, TEXTOID, TIMEOID, TIMESTAMPOID, TIMESTAMPTZOID, TIMETZOID, TYPTYPE_DOMAIN, VARCHAROID, VARHDRSZ, xmlbinary, XMLBINARY_BASE64, and XMLOID.

Referenced by map_sql_typecoll_to_xmlschema_types().

3353 {
3354  StringInfoData result;
3355  const char *typename = map_sql_type_to_xml_name(typeoid, typmod);
3356 
3357  initStringInfo(&result);
3358 
3359  if (typeoid == XMLOID)
3360  {
3361  appendStringInfoString(&result,
3362  "<xsd:complexType mixed=\"true\">\n"
3363  " <xsd:sequence>\n"
3364  " <xsd:any name=\"element\" minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>\n"
3365  " </xsd:sequence>\n"
3366  "</xsd:complexType>\n");
3367  }
3368  else
3369  {
3370  appendStringInfo(&result,
3371  "<xsd:simpleType name=\"%s\">\n", typename);
3372 
3373  switch (typeoid)
3374  {
3375  case BPCHAROID:
3376  case VARCHAROID:
3377  case TEXTOID:
3378  appendStringInfo(&result,
3379  " <xsd:restriction base=\"xsd:string\">\n");
3380  if (typmod != -1)
3381  appendStringInfo(&result,
3382  " <xsd:maxLength value=\"%d\"/>\n",
3383  typmod - VARHDRSZ);
3384  appendStringInfoString(&result, " </xsd:restriction>\n");
3385  break;
3386 
3387  case BYTEAOID:
3388  appendStringInfo(&result,
3389  " <xsd:restriction base=\"xsd:%s\">\n"
3390  " </xsd:restriction>\n",
3391  xmlbinary == XMLBINARY_BASE64 ? "base64Binary" : "hexBinary");
3392  break;
3393 
3394  case NUMERICOID:
3395  if (typmod != -1)
3396  appendStringInfo(&result,
3397  " <xsd:restriction base=\"xsd:decimal\">\n"
3398  " <xsd:totalDigits value=\"%d\"/>\n"
3399  " <xsd:fractionDigits value=\"%d\"/>\n"
3400  " </xsd:restriction>\n",
3401  ((typmod - VARHDRSZ) >> 16) & 0xffff,
3402  (typmod - VARHDRSZ) & 0xffff);
3403  break;
3404 
3405  case INT2OID:
3406  appendStringInfo(&result,
3407  " <xsd:restriction base=\"xsd:short\">\n"
3408  " <xsd:maxInclusive value=\"%d\"/>\n"
3409  " <xsd:minInclusive value=\"%d\"/>\n"
3410  " </xsd:restriction>\n",
3411  SHRT_MAX, SHRT_MIN);
3412  break;
3413 
3414  case INT4OID:
3415  appendStringInfo(&result,
3416  " <xsd:restriction base=\"xsd:int\">\n"
3417  " <xsd:maxInclusive value=\"%d\"/>\n"
3418  " <xsd:minInclusive value=\"%d\"/>\n"
3419  " </xsd:restriction>\n",
3420  INT_MAX, INT_MIN);
3421  break;
3422 
3423  case INT8OID:
3424  appendStringInfo(&result,
3425  " <xsd:restriction base=\"xsd:long\">\n"
3426  " <xsd:maxInclusive value=\"" INT64_FORMAT "\"/>\n"
3427  " <xsd:minInclusive value=\"" INT64_FORMAT "\"/>\n"
3428  " </xsd:restriction>\n",
3429  (((uint64) 1) << (sizeof(int64) * 8 - 1)) - 1,
3430  (((uint64) 1) << (sizeof(int64) * 8 - 1)));
3431  break;
3432 
3433  case FLOAT4OID:
3434  appendStringInfoString(&result,
3435  " <xsd:restriction base=\"xsd:float\"></xsd:restriction>\n");
3436  break;
3437 
3438  case FLOAT8OID:
3439  appendStringInfoString(&result,
3440  " <xsd:restriction base=\"xsd:double\"></xsd:restriction>\n");
3441  break;
3442 
3443  case BOOLOID:
3444  appendStringInfoString(&result,
3445  " <xsd:restriction base=\"xsd:boolean\"></xsd:restriction>\n");
3446  break;
3447 
3448  case TIMEOID:
3449  case TIMETZOID:
3450  {
3451  const char *tz = (typeoid == TIMETZOID ? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
3452 
3453  if (typmod == -1)
3454  appendStringInfo(&result,
3455  " <xsd:restriction base=\"xsd:time\">\n"
3456  " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}(.\\p{Nd}+)?%s\"/>\n"
3457  " </xsd:restriction>\n", tz);
3458  else if (typmod == 0)
3459  appendStringInfo(&result,
3460  " <xsd:restriction base=\"xsd:time\">\n"
3461  " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3462  " </xsd:restriction>\n", tz);
3463  else
3464  appendStringInfo(&result,
3465  " <xsd:restriction base=\"xsd:time\">\n"
3466  " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}.\\p{Nd}{%d}%s\"/>\n"
3467  " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
3468  break;
3469  }
3470 
3471  case TIMESTAMPOID:
3472  case TIMESTAMPTZOID:
3473  {
3474  const char *tz = (typeoid == TIMESTAMPTZOID ? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
3475 
3476  if (typmod == -1)
3477  appendStringInfo(&result,
3478  " <xsd:restriction base=\"xsd:dateTime\">\n"
3479  " <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"
3480  " </xsd:restriction>\n", tz);
3481  else if (typmod == 0)
3482  appendStringInfo(&result,
3483  " <xsd:restriction base=\"xsd:dateTime\">\n"
3484  " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3485  " </xsd:restriction>\n", tz);
3486  else
3487  appendStringInfo(&result,
3488  " <xsd:restriction base=\"xsd:dateTime\">\n"
3489  " <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"
3490  " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
3491  break;
3492  }
3493 
3494  case DATEOID:
3495  appendStringInfoString(&result,
3496  " <xsd:restriction base=\"xsd:date\">\n"
3497  " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}\"/>\n"
3498  " </xsd:restriction>\n");
3499  break;
3500 
3501  default:
3502  if (get_typtype(typeoid) == TYPTYPE_DOMAIN)
3503  {
3504  Oid base_typeoid;
3505  int32 base_typmod = -1;
3506 
3507  base_typeoid = getBaseTypeAndTypmod(typeoid, &base_typmod);
3508 
3509  appendStringInfo(&result,
3510  " <xsd:restriction base=\"%s\"/>\n",
3511  map_sql_type_to_xml_name(base_typeoid, base_typmod));
3512  }
3513  break;
3514  }
3515  appendStringInfoString(&result, "</xsd:simpleType>\n");
3516  }
3517 
3518  return result.data;
3519 }
#define TIMESTAMPTZOID
Definition: pg_type.h:513
#define TIMEOID
Definition: pg_type.h:502
#define TYPTYPE_DOMAIN
Definition: pg_type.h:710
#define BPCHAROID
Definition: pg_type.h:492
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2256
#define DATEOID
Definition: pg_type.h:499
#define TEXTOID
Definition: pg_type.h:324
#define NUMERICOID
Definition: pg_type.h:542
#define VARHDRSZ
Definition: c.h:440
#define INT4OID
Definition: pg_type.h:316
char get_typtype(Oid typid)
Definition: lsyscache.c:2347
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:253
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define TIMESTAMPOID
Definition: pg_type.h:507
#define INT2OID
Definition: pg_type.h:308
#define XMLOID
Definition: pg_type.h:359
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
static const char * map_sql_type_to_xml_name(Oid typeoid, int typmod)
Definition: xml.c:3193
#define VARCHAROID
Definition: pg_type.h:495
#define FLOAT4OID
Definition: pg_type.h:408
#define INT8OID
Definition: pg_type.h:304
#define TIMETZOID
Definition: pg_type.h:524
#define FLOAT8OID
Definition: pg_type.h:411
#define BOOLOID
Definition: pg_type.h:288
int xmlbinary
Definition: xml.c:95
#define INT64_FORMAT
Definition: c.h:312
#define BYTEAOID
Definition: pg_type.h:292
static const char * map_sql_typecoll_to_xmlschema_types ( List tupdesc_list)
static

Definition at line 3298 of file xml.c.

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

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

3299 {
3300  List *uniquetypes = NIL;
3301  int i;
3302  StringInfoData result;
3303  ListCell *cell0;
3304 
3305  /* extract all column types used in the set of TupleDescs */
3306  foreach(cell0, tupdesc_list)
3307  {
3308  TupleDesc tupdesc = (TupleDesc) lfirst(cell0);
3309 
3310  for (i = 0; i < tupdesc->natts; i++)
3311  {
3312  if (tupdesc->attrs[i]->attisdropped)
3313  continue;
3314  uniquetypes = list_append_unique_oid(uniquetypes,
3315  tupdesc->attrs[i]->atttypid);
3316  }
3317  }
3318 
3319  /* add base types of domains */
3320  foreach(cell0, uniquetypes)
3321  {
3322  Oid typid = lfirst_oid(cell0);
3323  Oid basetypid = getBaseType(typid);
3324 
3325  if (basetypid != typid)
3326  uniquetypes = list_append_unique_oid(uniquetypes, basetypid);
3327  }
3328 
3329  /* Convert to textual form */
3330  initStringInfo(&result);
3331 
3332  foreach(cell0, uniquetypes)
3333  {
3334  appendStringInfo(&result, "%s\n",
3336  -1));
3337  }
3338 
3339  return result.data;
3340 }
#define NIL
Definition: pg_list.h:69
List * list_append_unique_oid(List *list, Oid datum)
Definition: list.c:999
Form_pg_attribute * attrs
Definition: tupdesc.h:74
unsigned int Oid
Definition: postgres_ext.h:31
int natts
Definition: tupdesc.h:73
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
struct tupleDesc * TupleDesc
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
#define lfirst(lc)
Definition: pg_list.h:106
int i
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2239
static const char * map_sql_type_to_xmlschema_type(Oid typeoid, int typmod)
Definition: xml.c:3352
Definition: pg_list.h:45
#define lfirst_oid(lc)
Definition: pg_list.h:108
char* map_sql_value_to_xml_value ( Datum  value,
Oid  type,
bool  xml_escape_strings 
)

Definition at line 1951 of file xml.c.

References appendStringInfoString(), ARR_ELEMTYPE, BOOLOID, buf, BYTEAOID, StringInfoData::data, DATE_NOT_FINITE, DATEOID, 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, NULL, 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(), timestamp2tm(), TIMESTAMP_NOT_FINITE, TIMESTAMPOID, TIMESTAMPTZOID, pg_tm::tm_mday, pg_tm::tm_mon, pg_tm::tm_year, type_is_array_domain, USE_XSD_DATES, VARDATA_ANY, VARSIZE_ANY_EXHDR, xml_ereport(), xmlbinary, XMLBINARY_BASE64, and XMLOID.

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

1952 {
1953  if (type_is_array_domain(type))
1954  {
1955  ArrayType *array;
1956  Oid elmtype;
1957  int16 elmlen;
1958  bool elmbyval;
1959  char elmalign;
1960  int num_elems;
1961  Datum *elem_values;
1962  bool *elem_nulls;
1964  int i;
1965 
1966  array = DatumGetArrayTypeP(value);
1967  elmtype = ARR_ELEMTYPE(array);
1968  get_typlenbyvalalign(elmtype, &elmlen, &elmbyval, &elmalign);
1969 
1970  deconstruct_array(array, elmtype,
1971  elmlen, elmbyval, elmalign,
1972  &elem_values, &elem_nulls,
1973  &num_elems);
1974 
1975  initStringInfo(&buf);
1976 
1977  for (i = 0; i < num_elems; i++)
1978  {
1979  if (elem_nulls[i])
1980  continue;
1981  appendStringInfoString(&buf, "<element>");
1983  map_sql_value_to_xml_value(elem_values[i],
1984  elmtype, true));
1985  appendStringInfoString(&buf, "</element>");
1986  }
1987 
1988  pfree(elem_values);
1989  pfree(elem_nulls);
1990 
1991  return buf.data;
1992  }
1993  else
1994  {
1995  Oid typeOut;
1996  bool isvarlena;
1997  char *str;
1998 
1999  /*
2000  * Flatten domains; the special-case treatments below should apply to,
2001  * eg, domains over boolean not just boolean.
2002  */
2003  type = getBaseType(type);
2004 
2005  /*
2006  * Special XSD formatting for some data types
2007  */
2008  switch (type)
2009  {
2010  case BOOLOID:
2011  if (DatumGetBool(value))
2012  return "true";
2013  else
2014  return "false";
2015 
2016  case DATEOID:
2017  {
2018  DateADT date;
2019  struct pg_tm tm;
2020  char buf[MAXDATELEN + 1];
2021 
2022  date = DatumGetDateADT(value);
2023  /* XSD doesn't support infinite values */
2024  if (DATE_NOT_FINITE(date))
2025  ereport(ERROR,
2026  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2027  errmsg("date out of range"),
2028  errdetail("XML does not support infinite date values.")));
2030  &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
2032 
2033  return pstrdup(buf);
2034  }
2035 
2036  case TIMESTAMPOID:
2037  {
2039  struct pg_tm tm;
2040  fsec_t fsec;
2041  char buf[MAXDATELEN + 1];
2042 
2043  timestamp = DatumGetTimestamp(value);
2044 
2045  /* XSD doesn't support infinite values */
2046  if (TIMESTAMP_NOT_FINITE(timestamp))
2047  ereport(ERROR,
2048  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2049  errmsg("timestamp out of range"),
2050  errdetail("XML does not support infinite timestamp values.")));
2051  else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
2052  EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
2053  else
2054  ereport(ERROR,
2055  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2056  errmsg("timestamp out of range")));
2057 
2058  return pstrdup(buf);
2059  }
2060 
2061  case TIMESTAMPTZOID:
2062  {
2064  struct pg_tm tm;
2065  int tz;
2066  fsec_t fsec;
2067  const char *tzn = NULL;
2068  char buf[MAXDATELEN + 1];
2069 
2070  timestamp = DatumGetTimestamp(value);
2071 
2072  /* XSD doesn't support infinite values */
2073  if (TIMESTAMP_NOT_FINITE(timestamp))
2074  ereport(ERROR,
2075  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2076  errmsg("timestamp out of range"),
2077  errdetail("XML does not support infinite timestamp values.")));
2078  else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
2079  EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
2080  else
2081  ereport(ERROR,
2082  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2083  errmsg("timestamp out of range")));
2084 
2085  return pstrdup(buf);
2086  }
2087 
2088 #ifdef USE_LIBXML
2089  case BYTEAOID:
2090  {
2091  bytea *bstr = DatumGetByteaPP(value);
2092  PgXmlErrorContext *xmlerrcxt;
2093  volatile xmlBufferPtr buf = NULL;
2094  volatile xmlTextWriterPtr writer = NULL;
2095  char *result;
2096 
2097  xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
2098 
2099  PG_TRY();
2100  {
2101  buf = xmlBufferCreate();
2102  if (buf == NULL || xmlerrcxt->err_occurred)
2103  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
2104  "could not allocate xmlBuffer");
2105  writer = xmlNewTextWriterMemory(buf, 0);
2106  if (writer == NULL || xmlerrcxt->err_occurred)
2107  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
2108  "could not allocate xmlTextWriter");
2109 
2110  if (xmlbinary == XMLBINARY_BASE64)
2111  xmlTextWriterWriteBase64(writer, VARDATA_ANY(bstr),
2112  0, VARSIZE_ANY_EXHDR(bstr));
2113  else
2114  xmlTextWriterWriteBinHex(writer, VARDATA_ANY(bstr),
2115  0, VARSIZE_ANY_EXHDR(bstr));
2116 
2117  /* we MUST do this now to flush data out to the buffer */
2118  xmlFreeTextWriter(writer);
2119  writer = NULL;
2120 
2121  result = pstrdup((const char *) xmlBufferContent(buf));
2122  }
2123  PG_CATCH();
2124  {
2125  if (writer)
2126  xmlFreeTextWriter(writer);
2127  if (buf)
2128  xmlBufferFree(buf);
2129 
2130  pg_xml_done(xmlerrcxt, true);
2131 
2132  PG_RE_THROW();
2133  }
2134  PG_END_TRY();
2135 
2136  xmlBufferFree(buf);
2137 
2138  pg_xml_done(xmlerrcxt, false);
2139 
2140  return result;
2141  }
2142 #endif /* USE_LIBXML */
2143 
2144  }
2145 
2146  /*
2147  * otherwise, just use the type's native text representation
2148  */
2149  getTypeOutputInfo(type, &typeOut, &isvarlena);
2150  str = OidOutputFunctionCall(typeOut, value);
2151 
2152  /* ... exactly as-is for XML, and when escaping is not wanted */
2153  if (type == XMLOID || !xml_escape_strings)
2154  return str;
2155 
2156  /* otherwise, translate special characters as needed */
2157  return escape_xml(str);
2158  }
2159 }
#define MAXDATELEN
Definition: datetime.h:203
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
Definition: datetime.c:3990
#define TIMESTAMPTZOID
Definition: pg_type.h:513
signed short int16
Definition: c.h:252
static struct @76 value
#define DATEOID
Definition: pg_type.h:499
#define DatumGetDateADT(X)
Definition: date.h:73
#define type_is_array_domain(typid)
Definition: lsyscache.h:165
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2600
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
double fsec_t
Definition: timestamp.h:53
int32 DateADT
Definition: date.h:22
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:1989
char * pstrdup(const char *in)
Definition: mcxt.c:1165
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1950
struct PgXmlErrorContext PgXmlErrorContext
Definition: xml.h:47
int errcode(int sqlerrcode)
Definition: elog.c:575
#define DatumGetByteaPP(X)
Definition: fmgr.h:247
long date
Definition: pgtypes_date.h:8
Definition: pgtime.h:25
unsigned int Oid
Definition: postgres_ext.h:31
static struct pg_tm tm
Definition: localtime.c:103
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:144
double TimestampTz
Definition: timestamp.h:51
void pfree(void *pointer)
Definition: mcxt.c:992
#define TIMESTAMPOID
Definition: pg_type.h:507
#define ERROR
Definition: elog.h:43
#define DATE_NOT_FINITE(j)
Definition: date.h:46
#define XMLOID
Definition: pg_type.h:359
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
int tm_mday
Definition: pgtime.h:30
static char * buf
Definition: pg_test_fsync.c:65
int tm_mon
Definition: pgtime.h:31
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define DatumGetBool(X)
Definition: postgres.h:401
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:4105
void pg_xml_done(PgXmlErrorContext *errcxt, bool isError)
#define ereport(elevel, rest)
Definition: elog.h:122
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:322
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
double Timestamp
Definition: timestamp.h:50
uintptr_t Datum
Definition: postgres.h:374
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:226
#define PG_RE_THROW()
Definition: elog.h:314
#define BOOLOID
Definition: pg_type.h:288
double timestamp
int xmlbinary
Definition: xml.c:95
#define BYTEAOID
Definition: pg_type.h:292
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
#define USE_XSD_DATES
Definition: miscadmin.h:213
int tm_year
Definition: pgtime.h:32
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:2006
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
Definition: c.h:434
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:185
char * escape_xml(const char *str)
Definition: xml.c:2170
#define PG_TRY()
Definition: elog.h:284
char * map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
Definition: xml.c:1951
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2239
#define ARR_ELEMTYPE(a)
Definition: array.h:273
PgXmlErrorContext * pg_xml_init(PgXmlStrictness strictness)
#define PG_END_TRY()
Definition: elog.h:300
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
#define DatumGetTimestamp(X)
Definition: timestamp.h:47
#define DatumGetArrayTypeP(X)
Definition: array.h:242
char* map_xml_name_to_sql_identifier ( char *  name)

Definition at line 1911 of file xml.c.

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

Referenced by get_rule_expr().

1912 {
1914  char *p;
1915 
1916  initStringInfo(&buf);
1917 
1918  for (p = name; *p; p += pg_mblen(p))
1919  {
1920  if (*p == '_' && *(p + 1) == 'x'
1921  && isxdigit((unsigned char) *(p + 2))
1922  && isxdigit((unsigned char) *(p + 3))
1923  && isxdigit((unsigned char) *(p + 4))
1924  && isxdigit((unsigned char) *(p + 5))
1925  && *(p + 6) == '_')
1926  {
1927  unsigned int u;
1928 
1929  sscanf(p + 2, "%X", &u);
1931  p += 6;
1932  }
1933  else
1934  appendBinaryStringInfo(&buf, p, pg_mblen(p));
1935  }
1936 
1937  return buf.data;
1938 }
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:65
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
int pg_mblen(const char *mbstr)
Definition: mbutils.c:771
const char * name
Definition: encode.c:521
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:240
static char * unicode_to_sqlchar(pg_wchar c)
Definition: xml.c:1891
static List* query_to_oid_list ( const char *  query)
static

Definition at line 2259 of file xml.c.

References DatumGetObjectId, i, lappend_oid(), sort-test::list, NIL, SPI_execute(), SPI_getbinval(), SPI_processed, 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().

2260 {
2261  uint64 i;
2262  List *list = NIL;
2263 
2264  SPI_execute(query, true, 0);
2265 
2266  for (i = 0; i < SPI_processed; i++)
2267  {
2268  Datum oid;
2269  bool isnull;
2270 
2271  oid = SPI_getbinval(SPI_tuptable->vals[i],
2273  1,
2274  &isnull);
2275  if (!isnull)
2276  list = lappend_oid(list, DatumGetObjectId(oid));
2277  }
2278 
2279  return list;
2280 }
#define NIL
Definition: pg_list.h:69
#define DatumGetObjectId(X)
Definition: postgres.h:508
SPITupleTable * SPI_tuptable
Definition: spi.c:41
HeapTuple * vals
Definition: spi.h:27
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
uint64 SPI_processed
Definition: spi.c:39
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition: spi.c:835
uintptr_t Datum
Definition: postgres.h:374
TupleDesc tupdesc
Definition: spi.h:26
tuple list
Definition: sort-test.py:11
int i
Definition: pg_list.h:45
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:303
Datum query_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 2356 of file xml.c.

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

2357 {
2358  char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2359  bool nulls = PG_GETARG_BOOL(1);
2360  bool tableforest = PG_GETARG_BOOL(2);
2361  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2362 
2364  NULL, nulls, tableforest,
2365  targetns, true)));
2366 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:230
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define PG_RETURN_XML_P(x)
Definition: xml.h:53
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:2449
#define NULL
Definition: c.h:226
char * text_to_cstring(const text *t)
Definition: varlena.c:184
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:396
Datum query_to_xml_and_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2589 of file xml.c.

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

2590 {
2591  char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2592  bool nulls = PG_GETARG_BOOL(1);
2593  bool tableforest = PG_GETARG_BOOL(2);
2594  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2595 
2596  const char *xmlschema;
2597  SPIPlanPtr plan;
2598  Portal portal;
2599 
2600  SPI_connect();
2601 
2602  if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
2603  elog(ERROR, "SPI_prepare(\"%s\") failed", query);
2604 
2605  if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
2606  elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
2607 
2608  xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2609  InvalidOid, nulls, tableforest, targetns));
2610  SPI_cursor_close(portal);
2611  SPI_finish();
2612 
2614  xmlschema, nulls, tableforest,
2615  targetns, true)));
2616 }
int SPI_connect(void)
Definition: spi.c:84
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:481
int SPI_finish(void)
Definition: spi.c:147
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1028
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:230
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define ERROR
Definition: elog.h:43
static char * _SPI_strdup(const char *s)
Definition: xml.c:2202
#define PG_RETURN_XML_P(x)
Definition: xml.h:53
static const char * map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:2960
TupleDesc tupDesc
Definition: portal.h:153
#define InvalidOid
Definition: postgres_ext.h:36
static StringInfo query_to_xml_internal(const char *query, char *tablename, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2449
#define NULL
Definition: c.h:226
void SPI_cursor_close(Portal portal)
Definition: spi.c:1399
char * text_to_cstring(const text *t)
Definition: varlena.c:184
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:396
#define elog
Definition: elog.h:219
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 2449 of file xml.c.

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

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

2452 {
2453  StringInfo result;
2454  char *xmltn;
2455  uint64 i;
2456 
2457  if (tablename)
2458  xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
2459  else
2460  xmltn = "table";
2461 
2462  result = makeStringInfo();
2463 
2464  SPI_connect();
2465  if (SPI_execute(query, true, 0) != SPI_OK_SELECT)
2466  ereport(ERROR,
2467  (errcode(ERRCODE_DATA_EXCEPTION),
2468  errmsg("invalid query")));
2469 
2470  if (!tableforest)
2471  {
2472  xmldata_root_element_start(result, xmltn, xmlschema,
2473  targetns, top_level);
2474  appendStringInfoChar(result, '\n');
2475  }
2476 
2477  if (xmlschema)
2478  appendStringInfo(result, "%s\n\n", xmlschema);
2479 
2480  for (i = 0; i < SPI_processed; i++)
2481  SPI_sql_row_to_xmlelement(i, result, tablename, nulls,
2482  tableforest, targetns, top_level);
2483 
2484  if (!tableforest)
2485  xmldata_root_element_end(result, xmltn);
2486 
2487  SPI_finish();
2488 
2489  return result;
2490 }
static void xmldata_root_element_start(StringInfo result, const char *eltname, const char *xmlschema, const char *targetns, bool top_level)
Definition: xml.c:2415
int SPI_connect(void)
Definition: spi.c:84
int SPI_finish(void)
Definition: spi.c:147
StringInfo makeStringInfo(void)
Definition: stringinfo.c:29
int errcode(int sqlerrcode)
Definition: elog.c:575
uint64 SPI_processed
Definition: spi.c:39
char * map_sql_identifier_to_xml_name(char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:1835
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
#define SPI_OK_SELECT
Definition: spi.h:51
static void xmldata_root_element_end(StringInfo result, const char *eltname)
Definition: xml.c:2442
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
static void SPI_sql_row_to_xmlelement(uint64 rownum, StringInfo result, char *tablename, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:3527
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:303
Datum query_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2513 of file xml.c.

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

2514 {
2515  char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2516  bool nulls = PG_GETARG_BOOL(1);
2517  bool tableforest = PG_GETARG_BOOL(2);
2518  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2519  const char *result;
2520  SPIPlanPtr plan;
2521  Portal portal;
2522 
2523  SPI_connect();
2524 
2525  if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
2526  elog(ERROR, "SPI_prepare(\"%s\") failed", query);
2527 
2528  if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
2529  elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
2530 
2532  InvalidOid, nulls,
2533  tableforest, targetns));
2534  SPI_cursor_close(portal);
2535  SPI_finish();
2536 
2538 }
int SPI_connect(void)
Definition: spi.c:84
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:481
int SPI_finish(void)
Definition: spi.c:147
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1028
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:230
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define ERROR
Definition: elog.h:43
static char * _SPI_strdup(const char *s)
Definition: xml.c:2202
#define PG_RETURN_XML_P(x)
Definition: xml.h:53
static const char * map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:2960
TupleDesc tupDesc
Definition: portal.h:153
#define InvalidOid
Definition: postgres_ext.h:36
static xmltype * cstring_to_xmltype(const char *string)
Definition: xml.c:403
#define NULL
Definition: c.h:226
void SPI_cursor_close(Portal portal)
Definition: spi.c:1399
char * text_to_cstring(const text *t)
Definition: varlena.c:184
#define elog
Definition: elog.h:219
static List* schema_get_xml_visible_tables ( Oid  nspid)
static

Definition at line 2284 of file xml.c.

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

Referenced by schema_to_xml_internal(), and schema_to_xmlschema_internal().

2285 {
2286  StringInfoData query;
2287 
2288  initStringInfo(&query);
2289  appendStringInfo(&query, "SELECT oid FROM pg_catalog.pg_class WHERE relnamespace = %u AND relkind IN ('r', 'm', 'v') AND pg_catalog.has_table_privilege (oid, 'SELECT') ORDER BY relname;", nspid);
2290 
2291  return query_to_oid_list(query.data);
2292 }
static List * query_to_oid_list(const char *query)
Definition: xml.c:2259
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
Datum schema_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 2668 of file xml.c.

References LookupExplicitNamespace(), NameStr, NULL, 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().

2669 {
2670  Name name = PG_GETARG_NAME(0);
2671  bool nulls = PG_GETARG_BOOL(1);
2672  bool tableforest = PG_GETARG_BOOL(2);
2673  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2674 
2675  char *schemaname;
2676  Oid nspid;
2677 
2678  schemaname = NameStr(*name);
2679  nspid = LookupExplicitNamespace(schemaname, false);
2680 
2682  nulls, tableforest, targetns, true)));
2683 }
static StringInfo schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2625
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
Definition: namespace.c:2687
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:230
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
Definition: c.h:488
#define PG_RETURN_XML_P(x)
Definition: xml.h:53
#define NULL
Definition: c.h:226
const char * name
Definition: encode.c:521
char * text_to_cstring(const text *t)
Definition: varlena.c:184
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:396
#define NameStr(name)
Definition: c.h:494
#define PG_GETARG_NAME(n)
Definition: fmgr.h:234
Datum schema_to_xml_and_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2772 of file xml.c.

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

2773 {
2774  Name name = PG_GETARG_NAME(0);
2775  bool nulls = PG_GETARG_BOOL(1);
2776  bool tableforest = PG_GETARG_BOOL(2);
2777  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2778  char *schemaname;
2779  Oid nspid;
2780  StringInfo xmlschema;
2781 
2782  schemaname = NameStr(*name);
2783  nspid = LookupExplicitNamespace(schemaname, false);
2784 
2785  xmlschema = schema_to_xmlschema_internal(schemaname, nulls,
2786  tableforest, targetns);
2787 
2789  xmlschema->data, nulls,
2790  tableforest, targetns, true)));
2791 }
static StringInfo schema_to_xmlschema_internal(const char *schemaname, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:2714
static StringInfo schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2625
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
Definition: namespace.c:2687
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:230
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
Definition: c.h:488
#define PG_RETURN_XML_P(x)
Definition: xml.h:53
const char * name
Definition: encode.c:521
char * text_to_cstring(const text *t)
Definition: varlena.c:184
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:396
#define NameStr(name)
Definition: c.h:494
#define PG_GETARG_NAME(n)
Definition: fmgr.h:234
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 2625 of file xml.c.

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), StringInfoData::data, get_namespace_name(), lfirst_oid, makeStringInfo(), map_sql_identifier_to_xml_name(), NULL, 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().

2627 {
2628  StringInfo result;
2629  char *xmlsn;
2630  List *relid_list;
2631  ListCell *cell;
2632 
2634  true, false);
2635  result = makeStringInfo();
2636 
2637  xmldata_root_element_start(result, xmlsn, xmlschema, targetns, top_level);
2638  appendStringInfoChar(result, '\n');
2639 
2640  if (xmlschema)
2641  appendStringInfo(result, "%s\n\n", xmlschema);
2642 
2643  SPI_connect();
2644 
2645  relid_list = schema_get_xml_visible_tables(nspid);
2646 
2647  foreach(cell, relid_list)
2648  {
2649  Oid relid = lfirst_oid(cell);
2650  StringInfo subres;
2651 
2652  subres = table_to_xml_internal(relid, NULL, nulls, tableforest,
2653  targetns, false);
2654 
2655  appendStringInfoString(result, subres->data);
2656  appendStringInfoChar(result, '\n');
2657  }
2658 
2659  SPI_finish();
2660 
2661  xmldata_root_element_end(result, xmlsn);
2662 
2663  return result;
2664 }
static void xmldata_root_element_start(StringInfo result, const char *eltname, const char *xmlschema, const char *targetns, bool top_level)
Definition: xml.c:2415
static List * schema_get_xml_visible_tables(Oid nspid)
Definition: xml.c:2284
int SPI_connect(void)
Definition: spi.c:84
int SPI_finish(void)
Definition: spi.c:147
StringInfo makeStringInfo(void)
Definition: stringinfo.c:29
unsigned int Oid
Definition: postgres_ext.h:31
char * map_sql_identifier_to_xml_name(char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:1835
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3006
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
#define NULL
Definition: c.h:226
static StringInfo table_to_xml_internal(Oid relid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2325
static void xmldata_root_element_end(StringInfo result, const char *eltname)
Definition: xml.c:2442
Definition: pg_list.h:45
#define lfirst_oid(lc)
Definition: pg_list.h:108
Datum schema_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2759 of file xml.c.

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

2760 {
2761  Name name = PG_GETARG_NAME(0);
2762  bool nulls = PG_GETARG_BOOL(1);
2763  bool tableforest = PG_GETARG_BOOL(2);
2764  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2765 
2767  nulls, tableforest, targetns)));
2768 }
static StringInfo schema_to_xmlschema_internal(const char *schemaname, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:2714
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:230
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
Definition: c.h:488
#define PG_RETURN_XML_P(x)
Definition: xml.h:53
const char * name
Definition: encode.c:521
char * text_to_cstring(const text *t)
Definition: varlena.c:184
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:396
#define NameStr(name)
Definition: c.h:494
#define PG_GETARG_NAME(n)
Definition: fmgr.h:234
static StringInfo schema_to_xmlschema_internal ( const char *  schemaname,
bool  nulls,
bool  tableforest,
const char *  targetns 
)
static

Definition at line 2714 of file xml.c.

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

Referenced by schema_to_xml_and_xmlschema(), and schema_to_xmlschema().

2716 {
2717  Oid nspid;
2718  List *relid_list;
2719  List *tupdesc_list;
2720  ListCell *cell;
2721  StringInfo result;
2722 
2723  result = makeStringInfo();
2724 
2725  nspid = LookupExplicitNamespace(schemaname, false);
2726 
2727  xsd_schema_element_start(result, targetns);
2728 
2729  SPI_connect();
2730 
2731  relid_list = schema_get_xml_visible_tables(nspid);
2732 
2733  tupdesc_list = NIL;
2734  foreach(cell, relid_list)
2735  {
2736  Relation rel;
2737 
2738  rel = heap_open(lfirst_oid(cell), AccessShareLock);
2739  tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2740  heap_close(rel, NoLock);
2741  }
2742 
2743  appendStringInfoString(result,
2744  map_sql_typecoll_to_xmlschema_types(tupdesc_list));
2745 
2746  appendStringInfoString(result,
2747  map_sql_schema_to_xmlschema_types(nspid, relid_list,
2748  nulls, tableforest, targetns));
2749 
2750  xsd_schema_element_end(result);
2751 
2752  SPI_finish();
2753 
2754  return result;
2755 }
static void xsd_schema_element_start(StringInfo result, const char *targetns)
Definition: xml.c:2690
#define NIL
Definition: pg_list.h:69
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:141
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
Definition: namespace.c:2687
static List * schema_get_xml_visible_tables(Oid nspid)
Definition: xml.c:2284
int SPI_connect(void)
Definition: spi.c:84
int SPI_finish(void)
Definition: spi.c:147
StringInfo makeStringInfo(void)
Definition: stringinfo.c:29
#define AccessShareLock
Definition: lockdefs.h:36
#define heap_close(r, l)
Definition: heapam.h:97
unsigned int Oid
Definition: postgres_ext.h:31
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
#define NoLock
Definition: lockdefs.h:34
List * lappend(List *list, void *datum)
Definition: list.c:128
static const char * map_sql_typecoll_to_xmlschema_types(List *tupdesc_list)
Definition: xml.c:3298
static void xsd_schema_element_end(StringInfo result)
Definition: xml.c:2707
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
TupleDesc rd_att
Definition: rel.h:114
static const char * map_sql_schema_to_xmlschema_types(Oid nspid, List *relid_list, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3063
Definition: pg_list.h:45
#define lfirst_oid(lc)
Definition: pg_list.h:108
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 3527 of file xml.c.

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), i, map_sql_identifier_to_xml_name(), map_sql_value_to_xml_value(), tupleDesc::natts, NULL, 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().

3530 {
3531  int i;
3532  char *xmltn;
3533 
3534  if (tablename)
3535  xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
3536  else
3537  {
3538  if (tableforest)
3539  xmltn = "row";
3540  else
3541  xmltn = "table";
3542  }
3543 
3544  if (tableforest)
3545  xmldata_root_element_start(result, xmltn, NULL, targetns, top_level);
3546  else
3547  appendStringInfoString(result, "<row>\n");
3548 
3549  for (i = 1; i <= SPI_tuptable->tupdesc->natts; i++)
3550  {
3551  char *colname;
3552  Datum colval;
3553  bool isnull;
3554 
3556  true, false);
3557  colval = SPI_getbinval(SPI_tuptable->vals[rownum],
3559  i,
3560  &isnull);
3561  if (isnull)
3562  {
3563  if (nulls)
3564  appendStringInfo(result, " <%s xsi:nil=\"true\"/>\n", colname);
3565  }
3566  else
3567  appendStringInfo(result, " <%s>%s</%s>\n",
3568  colname,
3570  SPI_gettypeid(SPI_tuptable->tupdesc, i), true),
3571  colname);
3572  }
3573 
3574  if (tableforest)
3575  {
3576  xmldata_root_element_end(result, xmltn);
3577  appendStringInfoChar(result, '\n');
3578  }
3579  else
3580  appendStringInfoString(result, "</row>\n\n");
3581 }
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition: spi.c:891
static void xmldata_root_element_start(StringInfo result, const char *eltname, const char *xmlschema, const char *targetns, bool top_level)
Definition: xml.c:2415
SPITupleTable * SPI_tuptable
Definition: spi.c:41
HeapTuple * vals
Definition: spi.h:27
int natts
Definition: tupdesc.h:73
char * SPI_fname(TupleDesc tupdesc, int fnumber)
Definition: spi.c:781
char * map_sql_identifier_to_xml_name(char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:1835
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition: spi.c:835
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
uintptr_t Datum
Definition: postgres.h:374
TupleDesc tupdesc
Definition: spi.h:26
#define NULL
Definition: c.h:226
static void xmldata_root_element_end(StringInfo result, const char *eltname)
Definition: xml.c:2442
int i
char * map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
Definition: xml.c:1951
Datum table_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 2342 of file xml.c.

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

2343 {
2344  Oid relid = PG_GETARG_OID(0);
2345  bool nulls = PG_GETARG_BOOL(1);
2346  bool tableforest = PG_GETARG_BOOL(2);
2347  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2348 
2350  nulls, tableforest,
2351  targetns, true)));
2352 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:230
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define PG_GETARG_OID(n)
Definition: fmgr.h:231
#define PG_RETURN_XML_P(x)
Definition: xml.h:53
#define NULL
Definition: c.h:226
static StringInfo table_to_xml_internal(Oid relid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2325
char * text_to_cstring(const text *t)
Definition: varlena.c:184
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:396
Datum table_to_xml_and_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2568 of file xml.c.

References AccessShareLock, heap_close, heap_open(), 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_to_xml_internal(), and text_to_cstring().

2569 {
2570  Oid relid = PG_GETARG_OID(0);
2571  bool nulls = PG_GETARG_BOOL(1);
2572  bool tableforest = PG_GETARG_BOOL(2);
2573  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2574  Relation rel;
2575  const char *xmlschema;
2576 
2577  rel = heap_open(relid, AccessShareLock);
2578  xmlschema = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2579  tableforest, targetns);
2580  heap_close(rel, NoLock);
2581 
2583  xmlschema, nulls, tableforest,
2584  targetns, true)));
2585 }
#define AccessShareLock
Definition: lockdefs.h:36
#define heap_close(r, l)
Definition: heapam.h:97
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:230
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define NoLock
Definition: lockdefs.h:34
#define PG_GETARG_OID(n)
Definition: fmgr.h:231
#define PG_RETURN_XML_P(x)
Definition: xml.h:53
static const char * map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:2960
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
TupleDesc rd_att
Definition: rel.h:114
static StringInfo table_to_xml_internal(Oid relid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2325
char * text_to_cstring(const text *t)
Definition: varlena.c:184
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:396
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 2325 of file xml.c.

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

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

2328 {
2329  StringInfoData query;
2330 
2331  initStringInfo(&query);
2332  appendStringInfo(&query, "SELECT * FROM %s",
2334  ObjectIdGetDatum(relid))));
2335  return query_to_xml_internal(query.data, get_rel_name(relid),
2336  xmlschema, nulls, tableforest,
2337  targetns, top_level);
2338 }
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:555
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define DatumGetCString(X)
Definition: postgres.h:574
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
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:2449
Datum regclassout(PG_FUNCTION_ARGS)
Definition: regproc.c:1089
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1694
Datum table_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2494 of file xml.c.

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

2495 {
2496  Oid relid = PG_GETARG_OID(0);
2497  bool nulls = PG_GETARG_BOOL(1);
2498  bool tableforest = PG_GETARG_BOOL(2);
2499  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2500  const char *result;
2501  Relation rel;
2502 
2503  rel = heap_open(relid, AccessShareLock);
2504  result = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2505  tableforest, targetns);
2506  heap_close(rel, NoLock);
2507 
2509 }
#define AccessShareLock
Definition: lockdefs.h:36
#define heap_close(r, l)
Definition: heapam.h:97
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:230
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define NoLock
Definition: lockdefs.h:34
#define PG_GETARG_OID(n)
Definition: fmgr.h:231
#define PG_RETURN_XML_P(x)
Definition: xml.h:53
static const char * map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:2960
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
TupleDesc rd_att
Definition: rel.h:114
static xmltype * cstring_to_xmltype(const char *string)
Definition: xml.c:403
char * text_to_cstring(const text *t)
Definition: varlena.c:184
Datum texttoxml ( PG_FUNCTION_ARGS  )

Definition at line 545 of file xml.c.

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

546 {
547  text *data = PG_GETARG_TEXT_P(0);
548 
549  PG_RETURN_XML_P(xmlparse(data, xmloption, true));
550 }
#define PG_RETURN_XML_P(x)
Definition: xml.h:53
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
int xmloption
Definition: xml.c:96
Definition: c.h:434
xmltype * xmlparse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace)
Definition: xml.c:701
static char* unicode_to_sqlchar ( pg_wchar  c)
static

Definition at line 1891 of file xml.c.

References pg_any_to_server(), PG_UTF8, pstrdup(), and unicode_to_utf8().

Referenced by map_xml_name_to_sql_identifier().

1892 {
1893  char utf8string[8]; /* need room for trailing zero */
1894  char *result;
1895 
1896  memset(utf8string, 0, sizeof(utf8string));
1897  unicode_to_utf8(c, (unsigned char *) utf8string);
1898 
1899  result = pg_any_to_server(utf8string, strlen(utf8string), PG_UTF8);
1900  /* if pg_any_to_server didn't strdup, we must */
1901  if (result == utf8string)
1902  result = pstrdup(result);
1903  return result;
1904 }
unsigned char * unicode_to_utf8(pg_wchar c, unsigned char *utf8string)
Definition: wchar.c:475
char * pstrdup(const char *in)
Definition: mcxt.c:1165
char * c
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:572
Datum xml_in ( PG_FUNCTION_ARGS  )

Definition at line 207 of file xml.c.

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

208 {
209 #ifdef USE_LIBXML
210  char *s = PG_GETARG_CSTRING(0);
211  xmltype *vardata;
212  xmlDocPtr doc;
213 
214  vardata = (xmltype *) cstring_to_text(s);
215 
216  /*
217  * Parse the data to check if it is well-formed XML data. Assume that
218  * ERROR occurred if parsing failed.
219  */
220  doc = xml_parse(vardata, xmloption, true, GetDatabaseEncoding());
221  xmlFreeDoc(doc);
222 
223  PG_RETURN_XML_P(vardata);
224 #else
225  NO_XML_SUPPORT();
226  return 0;
227 #endif
228 }
#define NO_XML_SUPPORT()
Definition: xml.c:168
#define PG_RETURN_XML_P(x)
Definition: xml.h:53
int GetDatabaseEncoding(void)
Definition: mbutils.c:1015
text * cstring_to_text(const char *s)
Definition: varlena.c:151
int xmloption
Definition: xml.c:96
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:233
Definition: c.h:434
bool xml_is_document ( xmltype arg)

Definition at line 837 of file xml.c.

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

Referenced by ExecEvalXml(), and xmltotext_with_xmloption().

838 {
839 #ifdef USE_LIBXML
840  bool result;
841  volatile xmlDocPtr doc = NULL;
843 
844  /* We want to catch ereport(INVALID_XML_DOCUMENT) and return false */
845  PG_TRY();
846  {
847  doc = xml_parse((text *) arg, XMLOPTION_DOCUMENT, true,
849  result = true;
850  }
851  PG_CATCH();
852  {
853  ErrorData *errdata;
854  MemoryContext ecxt;
855 
856  ecxt = MemoryContextSwitchTo(ccxt);
857  errdata = CopyErrorData();
858  if (errdata->sqlerrcode == ERRCODE_INVALID_XML_DOCUMENT)
859  {
860  FlushErrorState();
861  result = false;
862  }
863  else
864  {
865  MemoryContextSwitchTo(ecxt);
866  PG_RE_THROW();
867  }
868  }
869  PG_END_TRY();
870 
871  if (doc)
872  xmlFreeDoc(doc);
873 
874  return result;
875 #else /* not USE_LIBXML */
876  NO_XML_SUPPORT();
877  return false;
878 #endif /* not USE_LIBXML */
879 }
#define NO_XML_SUPPORT()
Definition: xml.c:168
int sqlerrcode
Definition: elog.h:342
ErrorData * CopyErrorData(void)
Definition: elog.c:1497
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void FlushErrorState(void)
Definition: elog.c:1587
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
int GetDatabaseEncoding(void)
Definition: mbutils.c:1015
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:226
#define PG_RE_THROW()
Definition: elog.h:314
Definition: c.h:434
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
Datum xml_is_well_formed ( PG_FUNCTION_ARGS  )

Definition at line 4031 of file xml.c.

References NO_XML_SUPPORT, PG_GETARG_TEXT_P, PG_RETURN_BOOL, and xmloption.

4032 {
4033 #ifdef USE_LIBXML
4034  text *data = PG_GETARG_TEXT_P(0);
4035 
4036  PG_RETURN_BOOL(wellformed_xml(data, xmloption));
4037 #else
4038  NO_XML_SUPPORT();
4039  return 0;
4040 #endif /* not USE_LIBXML */
4041 }
#define NO_XML_SUPPORT()
Definition: xml.c:168
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
int xmloption
Definition: xml.c:96
Definition: c.h:434
Datum xml_is_well_formed_content ( PG_FUNCTION_ARGS  )

Definition at line 4057 of file xml.c.

References NO_XML_SUPPORT, PG_GETARG_TEXT_P, PG_RETURN_BOOL, and XMLOPTION_CONTENT.

4058 {
4059 #ifdef USE_LIBXML
4060  text *data = PG_GETARG_TEXT_P(0);
4061 
4062  PG_RETURN_BOOL(wellformed_xml(data, XMLOPTION_CONTENT));
4063 #else
4064  NO_XML_SUPPORT();
4065  return 0;
4066 #endif /* not USE_LIBXML */
4067 }
#define NO_XML_SUPPORT()
Definition: xml.c:168
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
Definition: c.h:434
Datum xml_is_well_formed_document ( PG_FUNCTION_ARGS  )

Definition at line 4044 of file xml.c.

References NO_XML_SUPPORT, PG_GETARG_TEXT_P, PG_RETURN_BOOL, and XMLOPTION_DOCUMENT.

4045 {
4046 #ifdef USE_LIBXML
4047  text *data = PG_GETARG_TEXT_P(0);
4048 
4049  PG_RETURN_BOOL(wellformed_xml(data, XMLOPTION_DOCUMENT));
4050 #else
4051  NO_XML_SUPPORT();
4052  return 0;
4053 #endif /* not USE_LIBXML */
4054 }
#define NO_XML_SUPPORT()
Definition: xml.c:168
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
Definition: c.h:434
Datum xml_out ( PG_FUNCTION_ARGS  )

Definition at line 285 of file xml.c.

References PG_GETARG_XML_P, PG_RETURN_CSTRING, and xml_out_internal().

286 {
287  xmltype *x = PG_GETARG_XML_P(0);
288 
289  /*
290  * xml_out removes the encoding property in all cases. This is because we
291  * cannot control from here whether the datum will be converted to a
292  * different client encoding, so we'd do more harm than good by including
293  * it.
294  */
296 }
#define PG_GETARG_XML_P(n)
Definition: xml.h:52
static char * xml_out_internal(xmltype *x, pg_enc target_encoding)
Definition: xml.c:242
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:306
Definition: c.h:434
static char* xml_out_internal ( xmltype x,
pg_enc  target_encoding 
)
static

Definition at line 242 of file xml.c.

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

Referenced by xml_out(), and xml_send().

243 {
244  char *str = text_to_cstring((text *) x);
245 
246 #ifdef USE_LIBXML
247  size_t len = strlen(str);
248  xmlChar *version;
249  int standalone;
250  int res_code;
251 
252  if ((res_code = parse_xml_decl((xmlChar *) str,
253  &len, &version, NULL, &standalone)) == 0)
254  {
256 
257  initStringInfo(&buf);
258 
259  if (!print_xml_decl(&buf, version, target_encoding, standalone))
260  {
261  /*
262  * If we are not going to produce an XML declaration, eat a single
263  * newline in the original string to prevent empty first lines in
264  * the output.
265  */
266  if (*(str + len) == '\n')
267  len += 1;
268  }
269  appendStringInfoString(&buf, str + len);
270 
271  pfree(str);
272 
273  return buf.data;
274  }
275 
276  xml_ereport_by_code(WARNING, ERRCODE_INTERNAL_ERROR,
277  "could not parse XML declaration in stored value",
278  res_code);
279 #endif
280  return str;
281 }
void pfree(void *pointer)
Definition: mcxt.c:992
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:65
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
#define WARNING
Definition: elog.h:40
#define NULL
Definition: c.h:226
char * text_to_cstring(const text *t)
Definition: varlena.c:184
Definition: c.h:434
Datum xml_recv ( PG_FUNCTION_ARGS  )

Definition at line 300 of file xml.c.

References buf, cstring_to_text(), StringInfoData::cursor, encoding, StringInfoData::len, NO_XML_SUPPORT, NULL, palloc(), pfree(), pg_any_to_server(), PG_GETARG_POINTER, PG_RETURN_XML_P, PG_UTF8, pq_getmsgbytes(), SET_VARSIZE, VARDATA, VARHDRSZ, and xmloption.

301 {
302 #ifdef USE_LIBXML
304  xmltype *result;
305  char *str;
306  char *newstr;
307  int nbytes;
308  xmlDocPtr doc;
309  xmlChar *encodingStr = NULL;
310  int encoding;
311 
312  /*
313  * Read the data in raw format. We don't know yet what the encoding is, as
314  * that information is embedded in the xml declaration; so we have to
315  * parse that before converting to server encoding.
316  */
317  nbytes = buf->len - buf->cursor;
318  str = (char *) pq_getmsgbytes(buf, nbytes);
319 
320  /*
321  * We need a null-terminated string to pass to parse_xml_decl(). Rather
322  * than make a separate copy, make the temporary result one byte bigger
323  * than it needs to be.
324  */
325  result = palloc(nbytes + 1 + VARHDRSZ);
326  SET_VARSIZE(result, nbytes + VARHDRSZ);
327  memcpy(VARDATA(result), str, nbytes);
328  str = VARDATA(result);
329  str[nbytes] = '\0';
330 
331  parse_xml_decl((const xmlChar *) str, NULL, NULL, &encodingStr, NULL);
332 
333  /*
334  * If encoding wasn't explicitly specified in the XML header, treat it as
335  * UTF-8, as that's the default in XML. This is different from xml_in(),
336  * where the input has to go through the normal client to server encoding
337  * conversion.
338  */
339  encoding = encodingStr ? xmlChar_to_encoding(encodingStr) : PG_UTF8;
340 
341  /*
342  * Parse the data to check if it is well-formed XML data. Assume that
343  * xml_parse will throw ERROR if not.
344  */
345  doc = xml_parse(result, xmloption, true, encoding);
346  xmlFreeDoc(doc);
347 
348  /* Now that we know what we're dealing with, convert to server encoding */
349  newstr = pg_any_to_server(str, nbytes, encoding);
350 
351  if (newstr != str)
352  {
353  pfree(result);
354  result = (xmltype *) cstring_to_text(newstr);
355  pfree(newstr);
356  }
357 
358  PG_RETURN_XML_P(result);
359 #else
360  NO_XML_SUPPORT();
361  return 0;
362 #endif
363 }
#define NO_XML_SUPPORT()
Definition: xml.c:168
#define VARDATA(PTR)
Definition: postgres.h:305
#define VARHDRSZ
Definition: c.h:440
StringInfoData * StringInfo
Definition: stringinfo.h:46
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
const char * pq_getmsgbytes(StringInfo msg, int datalen)
Definition: pqformat.c:550
void pfree(void *pointer)
Definition: mcxt.c:992
static char * buf
Definition: pg_test_fsync.c:65
#define PG_RETURN_XML_P(x)
Definition: xml.h:53
static char * encoding
Definition: initdb.c:121
text * cstring_to_text(const char *s)
Definition: varlena.c:151
#define NULL
Definition: c.h:226
void * palloc(Size size)
Definition: mcxt.c:891
int xmloption
Definition: xml.c:96
Definition: c.h:434
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:330
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:572
Datum xml_send ( PG_FUNCTION_ARGS  )

Definition at line 367 of file xml.c.

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

368 {
369  xmltype *x = PG_GETARG_XML_P(0);
370  char *outval;
372 
373  /*
374  * xml_out_internal doesn't convert the encoding, it just prints the right
375  * declaration. pq_sendtext will do the conversion.
376  */
378 
379  pq_begintypsend(&buf);
380  pq_sendtext(&buf, outval, strlen(outval));
381  pfree(outval);
383 }
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:359
void pq_sendtext(StringInfo buf, const char *str, int slen)
Definition: pqformat.c:163
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:313
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:379
void pfree(void *pointer)
Definition: mcxt.c:992
static char * buf
Definition: pg_test_fsync.c:65
#define PG_GETARG_XML_P(n)
Definition: xml.h:52
int pg_get_client_encoding(void)
Definition: mbutils.c:317
static char * xml_out_internal(xmltype *x, pg_enc target_encoding)
Definition: xml.c:242
Definition: c.h:434
Datum xmlcomment ( PG_FUNCTION_ARGS  )

Definition at line 420 of file xml.c.

References appendStringInfoString(), appendStringInfoText(), arg, buf, ereport, errcode(), errmsg(), ERROR, i, initStringInfo(), NO_XML_SUPPORT, PG_GETARG_TEXT_P, PG_RETURN_XML_P, stringinfo_to_xmltype(), VARDATA, VARHDRSZ, and VARSIZE.

421 {
422 #ifdef USE_LIBXML
423  text *arg = PG_GETARG_TEXT_P(0);
424  char *argdata = VARDATA(arg);
425  int len = VARSIZE(arg) - VARHDRSZ;
427  int i;
428 
429  /* check for "--" in string or "-" at the end */
430  for (i = 1; i < len; i++)
431  {
432  if (argdata[i] == '-' && argdata[i - 1] == '-')
433  ereport(ERROR,
434  (errcode(ERRCODE_INVALID_XML_COMMENT),
435  errmsg("invalid XML comment")));
436  }
437  if (len > 0 && argdata[len - 1] == '-')
438  ereport(ERROR,
439  (errcode(ERRCODE_INVALID_XML_COMMENT),
440  errmsg("invalid XML comment")));
441 
442  initStringInfo(&buf);
443  appendStringInfoString(&buf, "<!--");
444  appendStringInfoText(&buf, arg);
445  appendStringInfoString(&buf, "-->");
446 
448 #else
449  NO_XML_SUPPORT();
450  return 0;
451 #endif
452 }
#define NO_XML_SUPPORT()
Definition: xml.c:168
#define VARDATA(PTR)
Definition: postgres.h:305
#define VARSIZE(PTR)
Definition: postgres.h:306
#define VARHDRSZ
Definition: c.h:440
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:3540
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:65
#define PG_RETURN_XML_P(x)
Definition: xml.h:53
#define ereport(elevel, rest)
Definition: elog.h:122
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:396
int i
void * arg
Definition: c.h:434
xmltype* xmlconcat ( List args)

Definition at line 461 of file xml.c.

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

Referenced by ExecEvalXml(), and xmlconcat2().

462 {
463 #ifdef USE_LIBXML
464  int global_standalone = 1;
465  xmlChar *global_version = NULL;
466  bool global_version_no_value = false;
468  ListCell *v;
469 
470  initStringInfo(&buf);
471  foreach(v, args)
472  {
474  size_t len;
475  xmlChar *version;
476  int standalone;
477  char *str;
478 
479  len = VARSIZE(x) - VARHDRSZ;
480  str = text_to_cstring((text *) x);
481 
482  parse_xml_decl((xmlChar *) str, &len, &version, NULL, &standalone);
483 
484  if (standalone == 0 && global_standalone == 1)
485  global_standalone = 0;
486  if (standalone < 0)
487  global_standalone = -1;
488 
489  if (!version)
490  global_version_no_value = true;
491  else if (!global_version)
492  global_version = version;
493  else if (xmlStrcmp(version, global_version) != 0)
494  global_version_no_value = true;
495 
496  appendStringInfoString(&buf, str + len);
497  pfree(str);
498  }
499 
500  if (!global_version_no_value || global_standalone >= 0)
501  {
502  StringInfoData buf2;
503 
504  initStringInfo(&buf2);
505 
506  print_xml_decl(&buf2,
507  (!global_version_no_value) ? global_version : NULL,
508  0,
509  global_standalone);
510 
511  appendStringInfoString(&buf2, buf.data);
512  buf = buf2;
513  }
514 
515  return stringinfo_to_xmltype(&buf);
516 #else
517  NO_XML_SUPPORT();
518  return NULL;
519 #endif
520 }
#define NO_XML_SUPPORT()
Definition: xml.c:168
#define VARSIZE(PTR)
Definition: postgres.h:306
#define PointerGetDatum(X)
Definition: postgres.h:564
#define VARHDRSZ
Definition: c.h:440
#define DatumGetXmlP(X)
Definition: xml.h:49
void pfree(void *pointer)
Definition: mcxt.c:992
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:65
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
char * text_to_cstring(const text *t)
Definition: varlena.c:184
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:396
Definition: c.h:434
Datum xmlconcat2 ( PG_FUNCTION_ARGS  )

Definition at line 527 of file xml.c.

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

528 {
529  if (PG_ARGISNULL(0))
530  {
531  if (PG_ARGISNULL(1))
532  PG_RETURN_NULL();
533  else
535  }
536  else if (PG_ARGISNULL(1))
538  else
540  PG_GETARG_XML_P(1))));
541 }
#define list_make2(x1, x2)
Definition: pg_list.h:134
xmltype * xmlconcat(List *args)
Definition: xml.c:461
#define PG_RETURN_XML_P(x)
Definition: xml.h:53
#define PG_GETARG_XML_P(n)
Definition: xml.h:52
#define PG_ARGISNULL(n)
Definition: fmgr.h:166
#define PG_RETURN_NULL()
Definition: fmgr.h:289
static void xmldata_root_element_end ( StringInfo  result,
const char *  eltname 
)
static

Definition at line 2442 of file xml.c.

References appendStringInfo().

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

2443 {
2444  appendStringInfo(result, "</%s>\n", eltname);
2445 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
static void xmldata_root_element_start ( StringInfo  result,
const char *  eltname,
const char *  xmlschema,
const char *  targetns,
bool  top_level 
)
static

Definition at line 2415 of file xml.c.

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

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

2418 {
2419  /* This isn't really wrong but currently makes no sense. */
2420  Assert(top_level || !xmlschema);
2421 
2422  appendStringInfo(result, "<%s", eltname);
2423  if (top_level)
2424  {
2425  appendStringInfoString(result, " xmlns:xsi=\"" NAMESPACE_XSI "\"");
2426  if (strlen(targetns) > 0)
2427  appendStringInfo(result, " xmlns=\"%s\"", targetns);
2428  }
2429  if (xmlschema)
2430  {
2431  /* FIXME: better targets */
2432  if (strlen(targetns) > 0)
2433  appendStringInfo(result, " xsi:schemaLocation=\"%s #\"", targetns);
2434  else
2435  appendStringInfoString(result, " xsi:noNamespaceSchemaLocation=\"#\"");
2436  }
2437  appendStringInfoString(result, ">\n");
2438 }
#define NAMESPACE_XSI
Definition: xml.c:178
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
#define Assert(condition)
Definition: c.h:670
xmltype* xmlelement ( XmlExprState xmlExpr,
ExprContext econtext 
)

Definition at line 577 of file xml.c.

References arg, XmlExpr::arg_names, XmlExprState::args, ERROR, ExecEvalExpr, ExprState::expr, exprType(), forboth, i, lappend(), lfirst, map_sql_value_to_xml_value(), XmlExpr::name, XmlExprState::named_args, NIL, NO_XML_SUPPORT, NULL, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, pg_xml_done(), pg_xml_init(), PG_XML_STRICTNESS_ALL, strVal, value, xml_ereport(), and XmlExprState::xprstate.

Referenced by ExecEvalXml().

578 {
579 #ifdef USE_LIBXML
580  XmlExpr *xexpr = (XmlExpr *) xmlExpr->xprstate.expr;
581  xmltype *result;
582  List *named_arg_strings;
583  List *arg_strings;
584  int i;
585  ListCell *arg;
586  ListCell *narg;
587  PgXmlErrorContext *xmlerrcxt;
588  volatile xmlBufferPtr buf = NULL;
589  volatile xmlTextWriterPtr writer = NULL;
590 
591  /*
592  * We first evaluate all the arguments, then start up libxml and create
593  * the result. This avoids issues if one of the arguments involves a call
594  * to some other function or subsystem that wants to use libxml on its own
595  * terms.
596  */
597  named_arg_strings = NIL;
598  i = 0;
599  foreach(arg, xmlExpr->named_args)
600  {
601  ExprState *e = (ExprState *) lfirst(arg);
602  Datum value;
603  bool isnull;
604  char *str;
605 
606  value = ExecEvalExpr(e, econtext, &isnull);
607  if (isnull)
608  str = NULL;
609  else
610  str = map_sql_value_to_xml_value(value, exprType((Node *) e->expr), false);
611  named_arg_strings = lappend(named_arg_strings, str);
612  i++;
613  }
614 
615  arg_strings = NIL;
616  foreach(arg, xmlExpr->args)
617  {
618  ExprState *e = (ExprState *) lfirst(arg);
619  Datum value;
620  bool isnull;
621  char *str;
622 
623  value = ExecEvalExpr(e, econtext, &isnull);
624  /* here we can just forget NULL elements immediately */
625  if (!isnull)
626  {
627  str = map_sql_value_to_xml_value(value,
628  exprType((Node *) e->expr), true);
629  arg_strings = lappend(arg_strings, str);
630  }
631  }
632 
633  /* now safe to run libxml */
634  xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
635 
636  PG_TRY();
637  {
638  buf = xmlBufferCreate();
639  if (buf == NULL || xmlerrcxt->err_occurred)
640  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
641  "could not allocate xmlBuffer");
642  writer = xmlNewTextWriterMemory(buf, 0);
643  if (writer == NULL || xmlerrcxt->err_occurred)
644  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
645  "could not allocate xmlTextWriter");
646 
647  xmlTextWriterStartElement(writer, (xmlChar *) xexpr->name);
648 
649  forboth(arg, named_arg_strings, narg, xexpr->arg_names)
650  {
651  char *str = (char *) lfirst(arg);
652  char *argname = strVal(lfirst(narg));
653 
654  if (str)
655  xmlTextWriterWriteAttribute(writer,
656  (xmlChar *) argname,
657  (xmlChar *) str);
658  }
659 
660  foreach(arg, arg_strings)
661  {
662  char *str = (char *) lfirst(arg);
663 
664  xmlTextWriterWriteRaw(writer, (xmlChar *) str);
665  }
666 
667  xmlTextWriterEndElement(writer);
668 
669  /* we MUST do this now to flush data out to the buffer ... */
670  xmlFreeTextWriter(writer);
671  writer = NULL;
672 
673  result = xmlBuffer_to_xmltype(buf);
674  }
675  PG_CATCH();
676  {
677  if (writer)
678  xmlFreeTextWriter(writer);
679  if (buf)
680  xmlBufferFree(buf);
681 
682  pg_xml_done(xmlerrcxt, true);
683 
684  PG_RE_THROW();
685  }
686  PG_END_TRY();
687 
688  xmlBufferFree(buf);
689 
690  pg_xml_done(xmlerrcxt, false);
691 
692  return result;
693 #else
694  NO_XML_SUPPORT();
695  return NULL;
696 #endif
697 }
ExprState xprstate
Definition: execnodes.h:976
#define NIL
Definition: pg_list.h:69
#define NO_XML_SUPPORT()
Definition: xml.c:168
static struct @76 value
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:174
char * name
Definition: primnodes.h:1119
List * args
Definition: execnodes.h:978
struct PgXmlErrorContext PgXmlErrorContext
Definition: xml.h:47
Definition: nodes.h:508
#define strVal(v)
Definition: value.h:54
List * arg_names
Definition: primnodes.h:1121
#define ExecEvalExpr(expr, econtext, isNull)
Definition: executor.h:73
#define ERROR
Definition: elog.h:43
Expr * expr
Definition: execnodes.h:598
static char * buf
Definition: pg_test_fsync.c:65
void pg_xml_done(PgXmlErrorContext *errcxt, bool isError)
List * lappend(List *list, void *datum)
Definition: list.c:128
uintptr_t Datum
Definition: postgres.h:374
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
List * named_args
Definition: execnodes.h:977
#define PG_RE_THROW()
Definition: elog.h:314
e
Definition: preproc-init.c:82
int i
void * arg
Definition: c.h:434
#define PG_TRY()
Definition: elog.h:284
char * map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
Definition: xml.c:1951
Definition: pg_list.h:45
PgXmlErrorContext * pg_xml_init(PgXmlStrictness strictness)
#define PG_END_TRY()
Definition: elog.h:300
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
Datum xmlexists ( PG_FUNCTION_ARGS  )

Definition at line 3958 of file xml.c.

References NO_XML_SUPPORT, NULL, PG_GETARG_TEXT_P, PG_GETARG_XML_P, and PG_RETURN_BOOL.

3959 {
3960 #ifdef USE_LIBXML
3961  text *xpath_expr_text = PG_GETARG_TEXT_P(0);
3962  xmltype *data = PG_GETARG_XML_P(1);
3963  int res_nitems;
3964 
3965  xpath_internal(xpath_expr_text, data, NULL,
3966  &res_nitems, NULL);
3967 
3968  PG_RETURN_BOOL(res_nitems > 0);
3969 #else
3970  NO_XML_SUPPORT();
3971  return 0;
3972 #endif
3973 }
#define NO_XML_SUPPORT()
Definition: xml.c:168
#define PG_GETARG_XML_P(n)
Definition: xml.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define NULL
Definition: c.h:226
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
Definition: c.h:434
xmltype* xmlparse ( text data,
XmlOptionType  xmloption_arg,
bool  preserve_whitespace 
)

Definition at line 701 of file xml.c.

References GetDatabaseEncoding(), NO_XML_SUPPORT, and NULL.

Referenced by ExecEvalXml(), and texttoxml().

702 {
703 #ifdef USE_LIBXML
704  xmlDocPtr doc;
705 
706  doc = xml_parse(data, xmloption_arg, preserve_whitespace,
708  xmlFreeDoc(doc);
709 
710  return (xmltype *) data;
711 #else
712  NO_XML_SUPPORT();
713  return NULL;
714 #endif
715 }
#define NO_XML_SUPPORT()
Definition: xml.c:168
int GetDatabaseEncoding(void)
Definition: mbutils.c:1015
#define NULL
Definition: c.h:226
Definition: c.h:434
xmltype* xmlpi ( char *  target,
text arg,
bool  arg_is_null,
bool result_is_null 
)

Definition at line 719 of file xml.c.

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

Referenced by ExecEvalXml().

720 {
721 #ifdef USE_LIBXML
722  xmltype *result;
724 
725  if (pg_strcasecmp(target, "xml") == 0)
726  ereport(ERROR,
727  (errcode(ERRCODE_SYNTAX_ERROR), /* really */
728  errmsg("invalid XML processing instruction"),
729  errdetail("XML processing instruction target name cannot be \"%s\".", target)));
730 
731  /*
732  * Following the SQL standard, the null check comes after the syntax check
733  * above.
734  */
735  *result_is_null = arg_is_null;
736  if (*result_is_null)
737  return NULL;
738 
739  initStringInfo(&buf);
740 
741  appendStringInfo(&buf, "<?%s", target);
742 
743  if (arg != NULL)
744  {
745  char *string;
746 
747  string = text_to_cstring(arg);
748  if (strstr(string, "?>") != NULL)
749  ereport(ERROR,
750  (errcode(ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION),
751  errmsg("invalid XML processing instruction"),
752  errdetail("XML processing instruction cannot contain \"?>\".")));
753 
754  appendStringInfoChar(&buf, ' ');
755  appendStringInfoString(&buf, string + strspn(string, " "));
756  pfree(string);
757  }
758  appendStringInfoString(&buf, "?>");
759 
760  result = stringinfo_to_xmltype(&buf);
761  pfree(buf.data);
762  return result;
763 #else
764  NO_XML_SUPPORT();
765  return NULL;
766 #endif
767 }
#define NO_XML_SUPPORT()
Definition: xml.c:168
int errcode(int sqlerrcode)
Definition: elog.c:575
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
void pfree(void *pointer)
Definition: mcxt.c:992
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define ERROR
Definition: elog.h:43
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:65
int errdetail(const char *fmt,...)
Definition: elog.c:873
char string[11]
Definition: preproc-type.c:46
#define ereport(elevel, rest)
Definition: elog.h:122
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
#define NULL
Definition: c.h:226
char * text_to_cstring(const text *t)
Definition: varlena.c:184
int errmsg(const char *fmt,...)
Definition: elog.c:797
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:396
Definition: c.h:434
xmltype* xmlroot ( xmltype data,
text version,
int  standalone 
)

Definition at line 771 of file xml.c.

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

Referenced by ExecEvalXml().

772 {
773 #ifdef USE_LIBXML
774  char *str;
775  size_t len;
776  xmlChar *orig_version;
777  int orig_standalone;
779 
780  len = VARSIZE(data) - VARHDRSZ;
781  str = text_to_cstring((text *) data);
782 
783  parse_xml_decl((xmlChar *) str, &len, &orig_version, NULL, &orig_standalone);
784 
785  if (version)
786  orig_version = xml_text2xmlChar(version);
787  else
788  orig_version = NULL;
789 
790  switch (standalone)
791  {
792  case XML_STANDALONE_YES:
793  orig_standalone = 1;
794  break;
795  case XML_STANDALONE_NO:
796  orig_standalone = 0;
797  break;
799  orig_standalone = -1;
800  break;
802  /* leave original value */
803  break;
804  }
805 
806  initStringInfo(&buf);
807  print_xml_decl(&buf, orig_version, 0, orig_standalone);
808  appendStringInfoString(&buf, str + len);
809 
810  return stringinfo_to_xmltype(&buf);
811 #else
812  NO_XML_SUPPORT();
813  return NULL;
814 #endif
815 }
#define NO_XML_SUPPORT()
Definition: xml.c:168
#define VARSIZE(PTR)
Definition: postgres.h:306
#define VARHDRSZ
Definition: c.h:440
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static char * buf
Definition: pg_test_fsync.c:65
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
#define NULL
Definition: c.h:226
char * text_to_cstring(const text *t)
Definition: varlena.c:184
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:396
Definition: c.h:434
Datum xmltotext ( PG_FUNCTION_ARGS  )

Definition at line 554 of file xml.c.

References PG_GETARG_XML_P, and PG_RETURN_TEXT_P.

555 {
556  xmltype *data = PG_GETARG_XML_P(0);
557 
558  /* It's actually binary compatible. */
559  PG_RETURN_TEXT_P((text *) data);
560 }
#define PG_GETARG_XML_P(n)
Definition: xml.h:52
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
Definition: c.h:434
text* xmltotext_with_xmloption ( xmltype data,
XmlOptionType  xmloption_arg 
)

Definition at line 564 of file xml.c.

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

Referenced by ExecEvalXml().

565 {
566  if (xmloption_arg == XMLOPTION_DOCUMENT && !xml_is_document(data))
567  ereport(ERROR,
568  (errcode(ERRCODE_NOT_AN_XML_DOCUMENT),
569  errmsg("not an XML document")));
570 
571  /* It's actually binary compatible, save for the above check. */
572  return (text *) data;
573 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
bool xml_is_document(xmltype *arg)
Definition: xml.c:837
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:434
Datum xmlvalidate ( PG_FUNCTION_ARGS  )

Definition at line 827 of file xml.c.

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

828 {
829  ereport(ERROR,
830  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
831  errmsg("xmlvalidate is not implemented")));
832  return 0;
833 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum xpath ( PG_FUNCTION_ARGS  )

Definition at line 3935 of file xml.c.

References CurrentMemoryContext, initArrayResult(), makeArrayResult(), NO_XML_SUPPORT, NULL, PG_GETARG_ARRAYTYPE_P, PG_GETARG_TEXT_P, PG_GETARG_XML_P, PG_RETURN_ARRAYTYPE_P, and XMLOID.

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

3936 {
3937 #ifdef USE_LIBXML
3938  text *xpath_expr_text = PG_GETARG_TEXT_P(0);
3939  xmltype *data = PG_GETARG_XML_P(1);
3940  ArrayType *namespaces = PG_GETARG_ARRAYTYPE_P(2);
3941  ArrayBuildState *astate;
3942 
3943  astate = initArrayResult(XMLOID, CurrentMemoryContext, true);
3944  xpath_internal(xpath_expr_text, data, namespaces,
3945  NULL, astate);
3947 #else
3948  NO_XML_SUPPORT();
3949  return 0;
3950 #endif
3951 }
#define NO_XML_SUPPORT()
Definition: xml.c:168
ArrayBuildState * initArrayResult(Oid element_type, MemoryContext rcontext, bool subcontext)
Definition: arrayfuncs.c:4951
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
#define XMLOID
Definition: pg_type.h:359
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:246
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5055
#define PG_GETARG_XML_P(n)
Definition: xml.h:52
#define NULL
Definition: c.h:226
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
Definition: c.h:434
Datum xpath_exists ( PG_FUNCTION_ARGS  )

Definition at line 3981 of file xml.c.

References NO_XML_SUPPORT, NULL, PG_GETARG_ARRAYTYPE_P, PG_GETARG_TEXT_P, PG_GETARG_XML_P, and PG_RETURN_BOOL.

3982 {
3983 #ifdef USE_LIBXML
3984  text *xpath_expr_text = PG_GETARG_TEXT_P(0);
3985  xmltype *data = PG_GETARG_XML_P(1);
3986  ArrayType *namespaces = PG_GETARG_ARRAYTYPE_P(2);
3987  int res_nitems;
3988 
3989  xpath_internal(xpath_expr_text, data, namespaces,
3990  &res_nitems, NULL);
3991 
3992  PG_RETURN_BOOL(res_nitems > 0);
3993 #else
3994  NO_XML_SUPPORT();
3995  return 0;
3996 #endif
3997 }
#define NO_XML_SUPPORT()
Definition: xml.c:168
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
#define PG_GETARG_XML_P(n)
Definition: xml.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define NULL
Definition: c.h:226
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
Definition: c.h:434
static void xsd_schema_element_end ( StringInfo  result)
static

Definition at line 2707 of file xml.c.

References appendStringInfoString().

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

2708 {
2709  appendStringInfoString(result, "</xsd:schema>");
2710 }
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static void xsd_schema_element_start ( StringInfo  result,
const char *  targetns 
)
static

Definition at line 2690 of file xml.c.

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

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

2691 {
2692  appendStringInfoString(result,
2693  "<xsd:schema\n"
2694  " xmlns:xsd=\"" NAMESPACE_XSD "\"");
2695  if (strlen(targetns) > 0)
2696  appendStringInfo(result,
2697  "\n"
2698  " targetNamespace=\"%s\"\n"
2699  " elementFormDefault=\"qualified\"",
2700  targetns);
2701  appendStringInfoString(result,
2702  ">\n\n");
2703 }
#define NAMESPACE_XSD
Definition: xml.c:177
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189

Variable Documentation

int xmlbinary

Definition at line 95 of file xml.c.

Referenced by map_sql_type_to_xmlschema_type(), and map_sql_value_to_xml_value().