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_class.h"
#include "catalog/pg_type.h"
#include "commands/dbcommands.h"
#include "executor/spi.h"
#include "executor/tablefunc.h"
#include "fmgr.h"
#include "lib/stringinfo.h"
#include "libpq/pqformat.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "nodes/execnodes.h"
#include "nodes/nodeFuncs.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/datetime.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/xml.h"
Include dependency graph for xml.c:

Go to the source code of this file.

Macros

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

Functions

static void xmldata_root_element_start (StringInfo result, const char *eltname, const char *xmlschema, const char *targetns, bool top_level)
 
static void xmldata_root_element_end (StringInfo result, const char *eltname)
 
static StringInfo query_to_xml_internal (const char *query, char *tablename, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
 
static const char * map_sql_table_to_xmlschema (TupleDesc tupdesc, Oid relid, bool nulls, bool tableforest, const char *targetns)
 
static const char * map_sql_schema_to_xmlschema_types (Oid nspid, List *relid_list, bool nulls, bool tableforest, const char *targetns)
 
static const char * map_sql_catalog_to_xmlschema_types (List *nspid_list, bool nulls, bool tableforest, const char *targetns)
 
static const char * map_sql_type_to_xml_name (Oid typeoid, int typmod)
 
static const char * map_sql_typecoll_to_xmlschema_types (List *tupdesc_list)
 
static const char * map_sql_type_to_xmlschema_type (Oid typeoid, int typmod)
 
static void SPI_sql_row_to_xmlelement (uint64 rownum, StringInfo result, char *tablename, bool nulls, bool tableforest, const char *targetns, bool top_level)
 
static void XmlTableInitOpaque (struct TableFuncScanState *state, int natts)
 
static void XmlTableSetDocument (struct TableFuncScanState *state, Datum value)
 
static void XmlTableSetNamespace (struct TableFuncScanState *state, char *name, char *uri)
 
static void XmlTableSetRowFilter (struct TableFuncScanState *state, char *path)
 
static void XmlTableSetColumnFilter (struct TableFuncScanState *state, char *path, int colnum)
 
static bool XmlTableFetchRow (struct TableFuncScanState *state)
 
static Datum XmlTableGetValue (struct TableFuncScanState *state, int colnum, Oid typid, int32 typmod, bool *isnull)
 
static void XmlTableDestroyOpaque (struct TableFuncScanState *state)
 
Datum xml_in (PG_FUNCTION_ARGS)
 
static char * xml_out_internal (xmltype *x, pg_enc target_encoding)
 
Datum xml_out (PG_FUNCTION_ARGS)
 
Datum xml_recv (PG_FUNCTION_ARGS)
 
Datum xml_send (PG_FUNCTION_ARGS)
 
static xmltypestringinfo_to_xmltype (StringInfo buf)
 
static xmltypecstring_to_xmltype (const char *string)
 
Datum xmlcomment (PG_FUNCTION_ARGS)
 
xmltypexmlconcat (List *args)
 
Datum xmlconcat2 (PG_FUNCTION_ARGS)
 
Datum texttoxml (PG_FUNCTION_ARGS)
 
Datum xmltotext (PG_FUNCTION_ARGS)
 
textxmltotext_with_xmloption (xmltype *data, XmlOptionType xmloption_arg)
 
xmltypexmlelement (XmlExpr *xexpr, Datum *named_argvalue, bool *named_argnull, Datum *argvalue, bool *argnull)
 
xmltypexmlparse (text *data, XmlOptionType xmloption_arg, bool preserve_whitespace)
 
xmltypexmlpi (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)
 
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
 
const TableFuncRoutine XmlTableRoutine
 

Macro Definition Documentation

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

Definition at line 228 of file xml.c.

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

Definition at line 226 of file xml.c.

Referenced by xsd_schema_element_start().

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

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

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

#define PG_XML_DEFAULT_VERSION   "1.0"

Definition at line 280 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 2369 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 2367 of file xml.c.

Function Documentation

static char* _SPI_strdup ( const char *  s)
static

Definition at line 2264 of file xml.c.

References SPI_palloc().

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

2265 {
2266  size_t len = strlen(s) + 1;
2267  char *ret = SPI_palloc(len);
2268 
2269  memcpy(ret, s, len);
2270  return ret;
2271 }
void * SPI_palloc(Size size)
Definition: spi.c:930
static xmltype* cstring_to_xmltype ( const char *  string)
static

Definition at line 452 of file xml.c.

References cstring_to_text().

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

453 {
454  return (xmltype *) cstring_to_text(string);
455 }
text * cstring_to_text(const char *s)
Definition: varlena.c:149
Definition: c.h:433
Datum cursor_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 2444 of file xml.c.

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

2445 {
2448  bool nulls = PG_GETARG_BOOL(2);
2449  bool tableforest = PG_GETARG_BOOL(3);
2450  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(4));
2451 
2452  StringInfoData result;
2453  Portal portal;
2454  uint64 i;
2455 
2456  initStringInfo(&result);
2457 
2458  if (!tableforest)
2459  {
2460  xmldata_root_element_start(&result, "table", NULL, targetns, true);
2461  appendStringInfoChar(&result, '\n');
2462  }
2463 
2464  SPI_connect();
2465  portal = SPI_cursor_find(name);
2466  if (portal == NULL)
2467  ereport(ERROR,
2468  (errcode(ERRCODE_UNDEFINED_CURSOR),
2469  errmsg("cursor \"%s\" does not exist", name)));
2470 
2471  SPI_cursor_fetch(portal, true, count);
2472  for (i = 0; i < SPI_processed; i++)
2473  SPI_sql_row_to_xmlelement(i, &result, NULL, nulls,
2474  tableforest, targetns, true);
2475 
2476  SPI_finish();
2477 
2478  if (!tableforest)
2479  xmldata_root_element_end(&result, "table");
2480 
2482 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
static void xmldata_root_element_start(StringInfo result, const char *eltname, const char *xmlschema, const char *targetns, bool top_level)
Definition: xml.c:2498
int SPI_connect(void)
Definition: spi.c:84
int SPI_finish(void)
Definition: spi.c:149
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
uint64 SPI_processed
Definition: spi.c:39
signed int int32
Definition: c.h:246
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
Portal SPI_cursor_find(const char *name)
Definition: spi.c:1343
#define ERROR
Definition: elog.h:43
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
#define ereport(elevel, rest)
Definition: elog.h:122
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:169
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
static void xmldata_root_element_end(StringInfo result, const char *eltname)
Definition: xml.c:2525
const char * name
Definition: encode.c:521
char * text_to_cstring(const text *t)
Definition: varlena.c:182
int errmsg(const char *fmt,...)
Definition: elog.c:797
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:445
int i
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Definition: spi.c:1355
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:3613
Datum cursor_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2625 of file xml.c.

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

2626 {
2628  bool nulls = PG_GETARG_BOOL(1);
2629  bool tableforest = PG_GETARG_BOOL(2);
2630  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2631  const char *xmlschema;
2632  Portal portal;
2633 
2634  SPI_connect();
2635  portal = SPI_cursor_find(name);
2636  if (portal == NULL)
2637  ereport(ERROR,
2638  (errcode(ERRCODE_UNDEFINED_CURSOR),
2639  errmsg("cursor \"%s\" does not exist", name)));
2640 
2641  xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2642  InvalidOid, nulls,
2643  tableforest, targetns));
2644  SPI_finish();
2645 
2646  PG_RETURN_XML_P(cstring_to_xmltype(xmlschema));
2647 }
int SPI_connect(void)
Definition: spi.c:84
int SPI_finish(void)
Definition: spi.c:149
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
Portal SPI_cursor_find(const char *name)
Definition: spi.c:1343
#define ERROR
Definition: elog.h:43
static char * _SPI_strdup(const char *s)
Definition: xml.c:2264
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
static const char * map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3043
#define ereport(elevel, rest)
Definition: elog.h:122
TupleDesc tupDesc
Definition: portal.h:155
#define InvalidOid
Definition: postgres_ext.h:36
static xmltype * cstring_to_xmltype(const char *string)
Definition: xml.c:452
const char * name
Definition: encode.c:521
char * text_to_cstring(const text *t)
Definition: varlena.c:182
int errmsg(const char *fmt,...)
Definition: elog.c:797
static List* database_get_xml_visible_schemas ( void  )
static

Definition at line 2373 of file xml.c.

References query_to_oid_list(), and XML_VISIBLE_SCHEMAS.

Referenced by database_to_xml_internal(), and database_to_xmlschema_internal().

2374 {
2375  return query_to_oid_list(XML_VISIBLE_SCHEMAS " ORDER BY nspname;");
2376 }
static List * query_to_oid_list(const char *query)
Definition: xml.c:2321
#define XML_VISIBLE_SCHEMAS
Definition: xml.c:2369
static List* database_get_xml_visible_tables ( void  )
static

Definition at line 2380 of file xml.c.

References CppAsString2, query_to_oid_list(), RELKIND_MATVIEW, RELKIND_RELATION, RELKIND_VIEW, and XML_VISIBLE_SCHEMAS.

Referenced by database_to_xmlschema_internal().

2381 {
2382  /* At the moment there is no order required here. */
2383  return query_to_oid_list("SELECT oid FROM pg_catalog.pg_class"
2384  " WHERE relkind IN ("
2388  " AND pg_catalog.has_table_privilege(pg_class.oid, 'SELECT')"
2389  " AND relnamespace IN (" XML_VISIBLE_SCHEMAS ");");
2390 }
static List * query_to_oid_list(const char *query)
Definition: xml.c:2321
#define RELKIND_MATVIEW
Definition: pg_class.h:165
#define XML_VISIBLE_SCHEMAS
Definition: xml.c:2369
#define CppAsString2(x)
Definition: c.h:162
#define RELKIND_VIEW
Definition: pg_class.h:164
#define RELKIND_RELATION
Definition: pg_class.h:160
Datum database_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 2926 of file xml.c.

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

2927 {
2928  bool nulls = PG_GETARG_BOOL(0);
2929  bool tableforest = PG_GETARG_BOOL(1);
2930  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
2931 
2933  tableforest, targetns)));
2934 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
static StringInfo database_to_xml_internal(const char *xmlschema, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:2883
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
char * text_to_cstring(const text *t)
Definition: varlena.c:182
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:445
Datum database_to_xml_and_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2993 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().

2994 {
2995  bool nulls = PG_GETARG_BOOL(0);
2996  bool tableforest = PG_GETARG_BOOL(1);
2997  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
2998  StringInfo xmlschema;
2999 
3000  xmlschema = database_to_xmlschema_internal(nulls, tableforest, targetns);
3001 
3003  nulls, tableforest, targetns)));
3004 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
static StringInfo database_to_xml_internal(const char *xmlschema, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:2883
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
static StringInfo database_to_xmlschema_internal(bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:2938
char * text_to_cstring(const text *t)
Definition: varlena.c:182
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:445
static StringInfo database_to_xml_internal ( const char *  xmlschema,
bool  nulls,
bool  tableforest,
const char *  targetns 
)
static

Definition at line 2883 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, 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().

2885 {
2886  StringInfo result;
2887  List *nspid_list;
2888  ListCell *cell;
2889  char *xmlcn;
2890 
2892  true, false);
2893  result = makeStringInfo();
2894 
2895  xmldata_root_element_start(result, xmlcn, xmlschema, targetns, true);
2896  appendStringInfoChar(result, '\n');
2897 
2898  if (xmlschema)
2899  appendStringInfo(result, "%s\n\n", xmlschema);
2900 
2901  SPI_connect();
2902 
2903  nspid_list = database_get_xml_visible_schemas();
2904 
2905  foreach(cell, nspid_list)
2906  {
2907  Oid nspid = lfirst_oid(cell);
2908  StringInfo subres;
2909 
2910  subres = schema_to_xml_internal(nspid, NULL, nulls,
2911  tableforest, targetns, false);
2912 
2913  appendStringInfoString(result, subres->data);
2914  appendStringInfoChar(result, '\n');
2915  }
2916 
2917  SPI_finish();
2918 
2919  xmldata_root_element_end(result, xmlcn);
2920 
2921  return result;
2922 }
static void xmldata_root_element_start(StringInfo result, const char *eltname, const char *xmlschema, const char *targetns, bool top_level)
Definition: xml.c:2498
static StringInfo schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2708
int SPI_connect(void)
Definition: spi.c:84
int SPI_finish(void)
Definition: spi.c:149
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
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:1897
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2056
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:169
static List * database_get_xml_visible_schemas(void)
Definition: xml.c:2373
Oid MyDatabaseId
Definition: globals.c:77
static void xmldata_root_element_end(StringInfo result, const char *eltname)
Definition: xml.c:2525
Definition: pg_list.h:45
#define lfirst_oid(lc)
Definition: pg_list.h:108
Datum database_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2981 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().

2982 {
2983  bool nulls = PG_GETARG_BOOL(0);
2984  bool tableforest = PG_GETARG_BOOL(1);
2985  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
2986 
2988  tableforest, targetns)));
2989 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
static StringInfo database_to_xmlschema_internal(bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:2938
char * text_to_cstring(const text *t)
Definition: varlena.c:182
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:445
static StringInfo database_to_xmlschema_internal ( bool  nulls,
bool  tableforest,
const char *  targetns 
)
static

Definition at line 2938 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().

2940 {
2941  List *relid_list;
2942  List *nspid_list;
2943  List *tupdesc_list;
2944  ListCell *cell;
2945  StringInfo result;
2946 
2947  result = makeStringInfo();
2948 
2949  xsd_schema_element_start(result, targetns);
2950 
2951  SPI_connect();
2952 
2953  relid_list = database_get_xml_visible_tables();
2954  nspid_list = database_get_xml_visible_schemas();
2955 
2956  tupdesc_list = NIL;
2957  foreach(cell, relid_list)
2958  {
2959  Relation rel;
2960 
2961  rel = heap_open(lfirst_oid(cell), AccessShareLock);
2962  tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2963  heap_close(rel, NoLock);
2964  }
2965 
2966  appendStringInfoString(result,
2967  map_sql_typecoll_to_xmlschema_types(tupdesc_list));
2968 
2969  appendStringInfoString(result,
2970  map_sql_catalog_to_xmlschema_types(nspid_list, nulls, tableforest, targetns));
2971 
2972  xsd_schema_element_end(result);
2973 
2974  SPI_finish();
2975 
2976  return result;
2977 }
static void xsd_schema_element_start(StringInfo result, const char *targetns)
Definition: xml.c:2773
#define NIL
Definition: pg_list.h:69
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:102
int SPI_connect(void)
Definition: spi.c:84
int SPI_finish(void)
Definition: spi.c:149
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
#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:3221
#define heap_close(r, l)
Definition: heapam.h:97
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
#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:3383
static List * database_get_xml_visible_schemas(void)
Definition: xml.c:2373
static void xsd_schema_element_end(StringInfo result)
Definition: xml.c:2790
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1290
TupleDesc rd_att
Definition: rel.h:115
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:2380
char* escape_xml ( const char *  str)

Definition at line 2232 of file xml.c.

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

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

2233 {
2235  const char *p;
2236 
2237  initStringInfo(&buf);
2238  for (p = str; *p; p++)
2239  {
2240  switch (*p)
2241  {
2242  case '&':
2243  appendStringInfoString(&buf, "&amp;");
2244  break;
2245  case '<':
2246  appendStringInfoString(&buf, "&lt;");
2247  break;
2248  case '>':
2249  appendStringInfoString(&buf, "&gt;");
2250  break;
2251  case '\r':
2252  appendStringInfoString(&buf, "&#x0d;");
2253  break;
2254  default:
2255  appendStringInfoCharMacro(&buf, *p);
2256  break;
2257  }
2258  }
2259  return buf.data;
2260 }
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:127
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
static char * buf
Definition: pg_test_fsync.c:67
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
static char* map_multipart_sql_identifier_to_xml_name ( char *  a,
char *  b,
char *  c,
char *  d 
)
static

Definition at line 3012 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().

3013 {
3014  StringInfoData result;
3015 
3016  initStringInfo(&result);
3017 
3018  if (a)
3019  appendStringInfoString(&result,
3020  map_sql_identifier_to_xml_name(a, true, true));
3021  if (b)
3022  appendStringInfo(&result, ".%s",
3023  map_sql_identifier_to_xml_name(b, true, true));
3024  if (c)
3025  appendStringInfo(&result, ".%s",
3026  map_sql_identifier_to_xml_name(c, true, true));
3027  if (d)
3028  appendStringInfo(&result, ".%s",
3029  map_sql_identifier_to_xml_name(d, true, true));
3030 
3031  return result.data;
3032 }
char * map_sql_identifier_to_xml_name(char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:1897
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
char * c
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
static const char * map_sql_catalog_to_xmlschema_types ( List nspid_list,
bool  nulls,
bool  tableforest,
const char *  targetns 
)
static

Definition at line 3221 of file xml.c.

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

Referenced by database_to_xmlschema_internal().

3223 {
3224  char *dbname;
3225  char *xmlcn;
3226  char *catalogtypename;
3227  StringInfoData result;
3228  ListCell *cell;
3229 
3230  dbname = get_database_name(MyDatabaseId);
3231 
3232  initStringInfo(&result);
3233 
3234  xmlcn = map_sql_identifier_to_xml_name(dbname, true, false);
3235 
3236  catalogtypename = map_multipart_sql_identifier_to_xml_name("CatalogType",
3237  dbname,
3238  NULL,
3239  NULL);
3240 
3241  appendStringInfo(&result,
3242  "<xsd:complexType name=\"%s\">\n", catalogtypename);
3243  appendStringInfoString(&result,
3244  " <xsd:all>\n");
3245 
3246  foreach(cell, nspid_list)
3247  {
3248  Oid nspid = lfirst_oid(cell);
3249  char *nspname = get_namespace_name(nspid);
3250  char *xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
3251  char *schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
3252  dbname,
3253  nspname,
3254  NULL);
3255 
3256  appendStringInfo(&result,
3257  " <xsd:element name=\"%s\" type=\"%s\"/>\n",
3258  xmlsn, schematypename);
3259  }
3260 
3261  appendStringInfoString(&result,
3262  " </xsd:all>\n");
3263  appendStringInfoString(&result,
3264  "</xsd:complexType>\n\n");
3265 
3266  appendStringInfo(&result,
3267  "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3268  xmlcn, catalogtypename);
3269 
3270  return result.data;
3271 }
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:3012
char * map_sql_identifier_to_xml_name(char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:1897
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2056
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3033
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
Oid MyDatabaseId
Definition: globals.c:77
char * dbname
Definition: streamutil.c:42
#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 1897 of file xml.c.

References appendBinaryStringInfo(), appendStringInfo(), appendStringInfoString(), Assert, buf, StringInfoData::data, initStringInfo(), NO_XML_SUPPORT, pg_mblen(), and pg_strncasecmp().

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

1899 {
1900 #ifdef USE_LIBXML
1902  char *p;
1903 
1904  /*
1905  * SQL/XML doesn't make use of this case anywhere, so it's probably a
1906  * mistake.
1907  */
1908  Assert(fully_escaped || !escape_period);
1909 
1910  initStringInfo(&buf);
1911 
1912  for (p = ident; *p; p += pg_mblen(p))
1913  {
1914  if (*p == ':' && (p == ident || fully_escaped))
1915  appendStringInfoString(&buf, "_x003A_");
1916  else if (*p == '_' && *(p + 1) == 'x')
1917  appendStringInfoString(&buf, "_x005F_");
1918  else if (fully_escaped && p == ident &&
1919  pg_strncasecmp(p, "xml", 3) == 0)
1920  {
1921  if (*p == 'x')
1922  appendStringInfoString(&buf, "_x0078_");
1923  else
1924  appendStringInfoString(&buf, "_x0058_");
1925  }
1926  else if (escape_period && *p == '.')
1927  appendStringInfoString(&buf, "_x002E_");
1928  else
1929  {
1930  pg_wchar u = sqlchar_to_unicode(p);
1931 
1932  if ((p == ident)
1933  ? !is_valid_xml_namefirst(u)
1934  : !is_valid_xml_namechar(u))
1935  appendStringInfo(&buf, "_x%04X_", (unsigned int) u);
1936  else
1937  appendBinaryStringInfo(&buf, p, pg_mblen(p));
1938  }
1939  }
1940 
1941  return buf.data;
1942 #else /* not USE_LIBXML */
1943  NO_XML_SUPPORT();
1944  return NULL;
1945 #endif /* not USE_LIBXML */
1946 }
#define NO_XML_SUPPORT()
Definition: xml.c:217
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:78
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
static char * buf
Definition: pg_test_fsync.c:67
unsigned int pg_wchar
Definition: mbprint.c:31
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define Assert(condition)
Definition: c.h:681
int pg_mblen(const char *mbstr)
Definition: mbutils.c:760
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208
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 3148 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(), and MyDatabaseId.

Referenced by schema_to_xmlschema_internal().

3150 {
3151  char *dbname;
3152  char *nspname;
3153  char *xmlsn;
3154  char *schematypename;
3155  StringInfoData result;
3156  ListCell *cell;
3157 
3158  dbname = get_database_name(MyDatabaseId);
3159  nspname = get_namespace_name(nspid);
3160 
3161  initStringInfo(&result);
3162 
3163  xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
3164 
3165  schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
3166  dbname,
3167  nspname,
3168  NULL);
3169 
3170  appendStringInfo(&result,
3171  "<xsd:complexType name=\"%s\">\n", schematypename);
3172  if (!tableforest)
3173  appendStringInfoString(&result,
3174  " <xsd:all>\n");
3175  else
3176  appendStringInfoString(&result,
3177  " <xsd:sequence>\n");
3178 
3179  foreach(cell, relid_list)
3180  {
3181  Oid relid = lfirst_oid(cell);
3182  char *relname = get_rel_name(relid);
3183  char *xmltn = map_sql_identifier_to_xml_name(relname, true, false);
3184  char *tabletypename = map_multipart_sql_identifier_to_xml_name(tableforest ? "RowType" : "TableType",
3185  dbname,
3186  nspname,
3187  relname);
3188 
3189  if (!tableforest)
3190  appendStringInfo(&result,
3191  " <xsd:element name=\"%s\" type=\"%s\"/>\n",
3192  xmltn, tabletypename);
3193  else
3194  appendStringInfo(&result,
3195  " <xsd:element name=\"%s\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n",
3196  xmltn, tabletypename);
3197  }
3198 
3199  if (!tableforest)
3200  appendStringInfoString(&result,
3201  " </xsd:all>\n");
3202  else
3203  appendStringInfoString(&result,
3204  " </xsd:sequence>\n");
3205  appendStringInfoString(&result,
3206  "</xsd:complexType>\n\n");
3207 
3208  appendStringInfo(&result,
3209  "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3210  xmlsn, schematypename);
3211 
3212  return result.data;
3213 }
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:3012
char * map_sql_identifier_to_xml_name(char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:1897
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2056
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3033
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
Oid MyDatabaseId
Definition: globals.c:77
char * dbname
Definition: streamutil.c:42
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1726
#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 3043 of file xml.c.

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

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

3045 {
3046  int i;
3047  char *xmltn;
3048  char *tabletypename;
3049  char *rowtypename;
3050  StringInfoData result;
3051 
3052  initStringInfo(&result);
3053 
3054  if (OidIsValid(relid))
3055  {
3056  HeapTuple tuple;
3057  Form_pg_class reltuple;
3058 
3059  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
3060  if (!HeapTupleIsValid(tuple))
3061  elog(ERROR, "cache lookup failed for relation %u", relid);
3062  reltuple = (Form_pg_class) GETSTRUCT(tuple);
3063 
3064  xmltn = map_sql_identifier_to_xml_name(NameStr(reltuple->relname),
3065  true, false);
3066 
3067  tabletypename = map_multipart_sql_identifier_to_xml_name("TableType",
3069  get_namespace_name(reltuple->relnamespace),
3070  NameStr(reltuple->relname));
3071 
3072  rowtypename = map_multipart_sql_identifier_to_xml_name("RowType",
3074  get_namespace_name(reltuple->relnamespace),
3075  NameStr(reltuple->relname));
3076 
3077  ReleaseSysCache(tuple);
3078  }
3079  else
3080  {
3081  if (tableforest)
3082  xmltn = "row";
3083  else
3084  xmltn = "table";
3085 
3086  tabletypename = "TableType";
3087  rowtypename = "RowType";
3088  }
3089 
3090  xsd_schema_element_start(&result, targetns);
3091 
3092  appendStringInfoString(&result,
3094 
3095  appendStringInfo(&result,
3096  "<xsd:complexType name=\"%s\">\n"
3097  " <xsd:sequence>\n",
3098  rowtypename);
3099 
3100  for (i = 0; i < tupdesc->natts; i++)
3101  {
3102  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
3103 
3104  if (att->attisdropped)
3105  continue;
3106  appendStringInfo(&result,
3107  " <xsd:element name=\"%s\" type=\"%s\"%s></xsd:element>\n",
3109  true, false),
3110  map_sql_type_to_xml_name(att->atttypid, -1),
3111  nulls ? " nillable=\"true\"" : " minOccurs=\"0\"");
3112  }
3113 
3114  appendStringInfoString(&result,
3115  " </xsd:sequence>\n"
3116  "</xsd:complexType>\n\n");
3117 
3118  if (!tableforest)
3119  {
3120  appendStringInfo(&result,
3121  "<xsd:complexType name=\"%s\">\n"
3122  " <xsd:sequence>\n"
3123  " <xsd:element name=\"row\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n"
3124  " </xsd:sequence>\n"
3125  "</xsd:complexType>\n\n",
3126  tabletypename, rowtypename);
3127 
3128  appendStringInfo(&result,
3129  "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3130  xmltn, tabletypename);
3131  }
3132  else
3133  appendStringInfo(&result,
3134  "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
3135  xmltn, rowtypename);
3136 
3137  xsd_schema_element_end(&result);
3138 
3139  return result.data;
3140 }
static void xsd_schema_element_start(StringInfo result, const char *targetns)
Definition: xml.c:2773
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
static char * map_multipart_sql_identifier_to_xml_name(char *a, char *b, char *c, char *d)
Definition: xml.c:3012
#define OidIsValid(objectId)
Definition: c.h:532
int natts
Definition: tupdesc.h:73
#define list_make1(x1)
Definition: pg_list.h:139
char * map_sql_identifier_to_xml_name(char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:1897
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2056
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3033
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
static const char * map_sql_typecoll_to_xmlschema_types(List *tupdesc_list)
Definition: xml.c:3383
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
static const char * map_sql_type_to_xml_name(Oid typeoid, int typmod)
Definition: xml.c:3278
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
static void xsd_schema_element_end(StringInfo result)
Definition: xml.c:2790
Oid MyDatabaseId
Definition: globals.c:77
#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:493
#define elog
Definition: elog.h:219
static const char * map_sql_type_to_xml_name ( Oid  typeoid,
int  typmod 
)
static

Definition at line 3278 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().

3279 {
3280  StringInfoData result;
3281 
3282  initStringInfo(&result);
3283 
3284  switch (typeoid)
3285  {
3286  case BPCHAROID:
3287  if (typmod == -1)
3288  appendStringInfoString(&result, "CHAR");
3289  else
3290  appendStringInfo(&result, "CHAR_%d", typmod - VARHDRSZ);
3291  break;
3292  case VARCHAROID:
3293  if (typmod == -1)
3294  appendStringInfoString(&result, "VARCHAR");
3295  else
3296  appendStringInfo(&result, "VARCHAR_%d", typmod - VARHDRSZ);
3297  break;
3298  case NUMERICOID:
3299  if (typmod == -1)
3300  appendStringInfoString(&result, "NUMERIC");
3301  else
3302  appendStringInfo(&result, "NUMERIC_%d_%d",
3303  ((typmod - VARHDRSZ) >> 16) & 0xffff,
3304  (typmod - VARHDRSZ) & 0xffff);
3305  break;
3306  case INT4OID:
3307  appendStringInfoString(&result, "INTEGER");
3308  break;
3309  case INT2OID:
3310  appendStringInfoString(&result, "SMALLINT");
3311  break;
3312  case INT8OID:
3313  appendStringInfoString(&result, "BIGINT");
3314  break;
3315  case FLOAT4OID:
3316  appendStringInfoString(&result, "REAL");
3317  break;
3318  case FLOAT8OID:
3319  appendStringInfoString(&result, "DOUBLE");
3320  break;
3321  case BOOLOID:
3322  appendStringInfoString(&result, "BOOLEAN");
3323  break;
3324  case TIMEOID:
3325  if (typmod == -1)
3326  appendStringInfoString(&result, "TIME");
3327  else
3328  appendStringInfo(&result, "TIME_%d", typmod);
3329  break;
3330  case TIMETZOID:
3331  if (typmod == -1)
3332  appendStringInfoString(&result, "TIME_WTZ");
3333  else
3334  appendStringInfo(&result, "TIME_WTZ_%d", typmod);
3335  break;
3336  case TIMESTAMPOID:
3337  if (typmod == -1)
3338  appendStringInfoString(&result, "TIMESTAMP");
3339  else
3340  appendStringInfo(&result, "TIMESTAMP_%d", typmod);
3341  break;
3342  case TIMESTAMPTZOID:
3343  if (typmod == -1)
3344  appendStringInfoString(&result, "TIMESTAMP_WTZ");
3345  else
3346  appendStringInfo(&result, "TIMESTAMP_WTZ_%d", typmod);
3347  break;
3348  case DATEOID:
3349  appendStringInfoString(&result, "DATE");
3350  break;
3351  case XMLOID:
3352  appendStringInfoString(&result, "XML");
3353  break;
3354  default:
3355  {
3356  HeapTuple tuple;
3357  Form_pg_type typtuple;
3358 
3359  tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typeoid));
3360  if (!HeapTupleIsValid(tuple))
3361  elog(ERROR, "cache lookup failed for type %u", typeoid);
3362  typtuple = (Form_pg_type) GETSTRUCT(tuple);
3363 
3364  appendStringInfoString(&result,
3365  map_multipart_sql_identifier_to_xml_name((typtuple->typtype == TYPTYPE_DOMAIN) ? "Domain" : "UDT",
3367  get_namespace_name(typtuple->typnamespace),
3368  NameStr(typtuple->typname)));
3369 
3370  ReleaseSysCache(tuple);
3371  }
3372  }
3373 
3374  return result.data;
3375 }
#define TIMESTAMPTZOID
Definition: pg_type.h:525
#define TIMEOID
Definition: pg_type.h:514
#define TYPTYPE_DOMAIN
Definition: pg_type.h:722
#define BPCHAROID
Definition: pg_type.h:504
#define DATEOID
Definition: pg_type.h:511
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define NUMERICOID
Definition: pg_type.h:554
#define VARHDRSZ
Definition: c.h:439
#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:3012
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define TIMESTAMPOID
Definition: pg_type.h:519
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2056
#define INT2OID
Definition: pg_type.h:308
#define XMLOID
Definition: pg_type.h:359
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3033
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
#define VARCHAROID
Definition: pg_type.h:507
#define FLOAT4OID
Definition: pg_type.h:416
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
Oid MyDatabaseId
Definition: globals.c:77
#define INT8OID
Definition: pg_type.h:304
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define TIMETZOID
Definition: pg_type.h:536
#define FLOAT8OID
Definition: pg_type.h:419
#define BOOLOID
Definition: pg_type.h:288
#define NameStr(name)
Definition: c.h:493
#define elog
Definition: elog.h:219
static const char * map_sql_type_to_xmlschema_type ( Oid  typeoid,
int  typmod 
)
static

Definition at line 3438 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().

3439 {
3440  StringInfoData result;
3441  const char *typename = map_sql_type_to_xml_name(typeoid, typmod);
3442 
3443  initStringInfo(&result);
3444 
3445  if (typeoid == XMLOID)
3446  {
3447  appendStringInfoString(&result,
3448  "<xsd:complexType mixed=\"true\">\n"
3449  " <xsd:sequence>\n"
3450  " <xsd:any name=\"element\" minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>\n"
3451  " </xsd:sequence>\n"
3452  "</xsd:complexType>\n");
3453  }
3454  else
3455  {
3456  appendStringInfo(&result,
3457  "<xsd:simpleType name=\"%s\">\n", typename);
3458 
3459  switch (typeoid)
3460  {
3461  case BPCHAROID:
3462  case VARCHAROID:
3463  case TEXTOID:
3464  appendStringInfoString(&result,
3465  " <xsd:restriction base=\"xsd:string\">\n");
3466  if (typmod != -1)
3467  appendStringInfo(&result,
3468  " <xsd:maxLength value=\"%d\"/>\n",
3469  typmod - VARHDRSZ);
3470  appendStringInfoString(&result, " </xsd:restriction>\n");
3471  break;
3472 
3473  case BYTEAOID:
3474  appendStringInfo(&result,
3475  " <xsd:restriction base=\"xsd:%s\">\n"
3476  " </xsd:restriction>\n",
3477  xmlbinary == XMLBINARY_BASE64 ? "base64Binary" : "hexBinary");
3478  break;
3479 
3480  case NUMERICOID:
3481  if (typmod != -1)
3482  appendStringInfo(&result,
3483  " <xsd:restriction base=\"xsd:decimal\">\n"
3484  " <xsd:totalDigits value=\"%d\"/>\n"
3485  " <xsd:fractionDigits value=\"%d\"/>\n"
3486  " </xsd:restriction>\n",
3487  ((typmod - VARHDRSZ) >> 16) & 0xffff,
3488  (typmod - VARHDRSZ) & 0xffff);
3489  break;
3490 
3491  case INT2OID:
3492  appendStringInfo(&result,
3493  " <xsd:restriction base=\"xsd:short\">\n"
3494  " <xsd:maxInclusive value=\"%d\"/>\n"
3495  " <xsd:minInclusive value=\"%d\"/>\n"
3496  " </xsd:restriction>\n",
3497  SHRT_MAX, SHRT_MIN);
3498  break;
3499 
3500  case INT4OID:
3501  appendStringInfo(&result,
3502  " <xsd:restriction base=\"xsd:int\">\n"
3503  " <xsd:maxInclusive value=\"%d\"/>\n"
3504  " <xsd:minInclusive value=\"%d\"/>\n"
3505  " </xsd:restriction>\n",
3506  INT_MAX, INT_MIN);
3507  break;
3508 
3509  case INT8OID:
3510  appendStringInfo(&result,
3511  " <xsd:restriction base=\"xsd:long\">\n"
3512  " <xsd:maxInclusive value=\"" INT64_FORMAT "\"/>\n"
3513  " <xsd:minInclusive value=\"" INT64_FORMAT "\"/>\n"
3514  " </xsd:restriction>\n",
3515  (((uint64) 1) << (sizeof(int64) * 8 - 1)) - 1,
3516  (((uint64) 1) << (sizeof(int64) * 8 - 1)));
3517  break;
3518 
3519  case FLOAT4OID:
3520  appendStringInfoString(&result,
3521  " <xsd:restriction base=\"xsd:float\"></xsd:restriction>\n");
3522  break;
3523 
3524  case FLOAT8OID:
3525  appendStringInfoString(&result,
3526  " <xsd:restriction base=\"xsd:double\"></xsd:restriction>\n");
3527  break;
3528 
3529  case BOOLOID:
3530  appendStringInfoString(&result,
3531  " <xsd:restriction base=\"xsd:boolean\"></xsd:restriction>\n");
3532  break;
3533 
3534  case TIMEOID:
3535  case TIMETZOID:
3536  {
3537  const char *tz = (typeoid == TIMETZOID ? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
3538 
3539  if (typmod == -1)
3540  appendStringInfo(&result,
3541  " <xsd:restriction base=\"xsd:time\">\n"
3542  " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}(.\\p{Nd}+)?%s\"/>\n"
3543  " </xsd:restriction>\n", tz);
3544  else if (typmod == 0)
3545  appendStringInfo(&result,
3546  " <xsd:restriction base=\"xsd:time\">\n"
3547  " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3548  " </xsd:restriction>\n", tz);
3549  else
3550  appendStringInfo(&result,
3551  " <xsd:restriction base=\"xsd:time\">\n"
3552  " <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}.\\p{Nd}{%d}%s\"/>\n"
3553  " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
3554  break;
3555  }
3556 
3557  case TIMESTAMPOID:
3558  case TIMESTAMPTZOID:
3559  {
3560  const char *tz = (typeoid == TIMESTAMPTZOID ? "(+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
3561 
3562  if (typmod == -1)
3563  appendStringInfo(&result,
3564  " <xsd:restriction base=\"xsd:dateTime\">\n"
3565  " <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"
3566  " </xsd:restriction>\n", tz);
3567  else if (typmod == 0)
3568  appendStringInfo(&result,
3569  " <xsd:restriction base=\"xsd:dateTime\">\n"
3570  " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
3571  " </xsd:restriction>\n", tz);
3572  else
3573  appendStringInfo(&result,
3574  " <xsd:restriction base=\"xsd:dateTime\">\n"
3575  " <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"
3576  " </xsd:restriction>\n", typmod - VARHDRSZ, tz);
3577  break;
3578  }
3579 
3580  case DATEOID:
3581  appendStringInfoString(&result,
3582  " <xsd:restriction base=\"xsd:date\">\n"
3583  " <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}\"/>\n"
3584  " </xsd:restriction>\n");
3585  break;
3586 
3587  default:
3588  if (get_typtype(typeoid) == TYPTYPE_DOMAIN)
3589  {
3590  Oid base_typeoid;
3591  int32 base_typmod = -1;
3592 
3593  base_typeoid = getBaseTypeAndTypmod(typeoid, &base_typmod);
3594 
3595  appendStringInfo(&result,
3596  " <xsd:restriction base=\"%s\"/>\n",
3597  map_sql_type_to_xml_name(base_typeoid, base_typmod));
3598  }
3599  break;
3600  }
3601  appendStringInfoString(&result, "</xsd:simpleType>\n");
3602  }
3603 
3604  return result.data;
3605 }
#define TIMESTAMPTZOID
Definition: pg_type.h:525
#define TIMEOID
Definition: pg_type.h:514
#define TYPTYPE_DOMAIN
Definition: pg_type.h:722
#define BPCHAROID
Definition: pg_type.h:504
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2288
#define DATEOID
Definition: pg_type.h:511
#define TEXTOID
Definition: pg_type.h:324
#define NUMERICOID
Definition: pg_type.h:554
#define VARHDRSZ
Definition: c.h:439
#define INT4OID
Definition: pg_type.h:316
char get_typtype(Oid typid)
Definition: lsyscache.c:2379
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:246
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define TIMESTAMPOID
Definition: pg_type.h:519
#define INT2OID
Definition: pg_type.h:308
#define XMLOID
Definition: pg_type.h:359
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
static const char * map_sql_type_to_xml_name(Oid typeoid, int typmod)
Definition: xml.c:3278
#define VARCHAROID
Definition: pg_type.h:507
#define FLOAT4OID
Definition: pg_type.h:416
#define INT8OID
Definition: pg_type.h:304
#define TIMETZOID
Definition: pg_type.h:536
#define FLOAT8OID
Definition: pg_type.h:419
#define BOOLOID
Definition: pg_type.h:288
int xmlbinary
Definition: xml.c:96
#define INT64_FORMAT
Definition: c.h:300
#define BYTEAOID
Definition: pg_type.h:292
static const char * map_sql_typecoll_to_xmlschema_types ( List tupdesc_list)
static

Definition at line 3383 of file xml.c.

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

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

3384 {
3385  List *uniquetypes = NIL;
3386  int i;
3387  StringInfoData result;
3388  ListCell *cell0;
3389 
3390  /* extract all column types used in the set of TupleDescs */
3391  foreach(cell0, tupdesc_list)
3392  {
3393  TupleDesc tupdesc = (TupleDesc) lfirst(cell0);
3394 
3395  for (i = 0; i < tupdesc->natts; i++)
3396  {
3397  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
3398 
3399  if (att->attisdropped)
3400  continue;
3401  uniquetypes = list_append_unique_oid(uniquetypes, att->atttypid);
3402  }
3403  }
3404 
3405  /* add base types of domains */
3406  foreach(cell0, uniquetypes)
3407  {
3408  Oid typid = lfirst_oid(cell0);
3409  Oid basetypid = getBaseType(typid);
3410 
3411  if (basetypid != typid)
3412  uniquetypes = list_append_unique_oid(uniquetypes, basetypid);
3413  }
3414 
3415  /* Convert to textual form */
3416  initStringInfo(&result);
3417 
3418  foreach(cell0, uniquetypes)
3419  {
3420  appendStringInfo(&result, "%s\n",
3422  -1));
3423  }
3424 
3425  return result.data;
3426 }
#define NIL
Definition: pg_list.h:69
List * list_append_unique_oid(List *list, Oid datum)
Definition: list.c:999
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
unsigned int Oid
Definition: postgres_ext.h:31
int natts
Definition: tupdesc.h:73
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
struct tupleDesc * TupleDesc
#define lfirst(lc)
Definition: pg_list.h:106
int i
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2271
static const char * map_sql_type_to_xmlschema_type(Oid typeoid, int typmod)
Definition: xml.c:3438
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 2013 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, 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 ExecEvalXmlExpr(), map_sql_value_to_xml_value(), SPI_sql_row_to_xmlelement(), and xmlelement().

2014 {
2015  if (type_is_array_domain(type))
2016  {
2017  ArrayType *array;
2018  Oid elmtype;
2019  int16 elmlen;
2020  bool elmbyval;
2021  char elmalign;
2022  int num_elems;
2023  Datum *elem_values;
2024  bool *elem_nulls;
2026  int i;
2027 
2028  array = DatumGetArrayTypeP(value);
2029  elmtype = ARR_ELEMTYPE(array);
2030  get_typlenbyvalalign(elmtype, &elmlen, &elmbyval, &elmalign);
2031 
2032  deconstruct_array(array, elmtype,
2033  elmlen, elmbyval, elmalign,
2034  &elem_values, &elem_nulls,
2035  &num_elems);
2036 
2037  initStringInfo(&buf);
2038 
2039  for (i = 0; i < num_elems; i++)
2040  {
2041  if (elem_nulls[i])
2042  continue;
2043  appendStringInfoString(&buf, "<element>");
2045  map_sql_value_to_xml_value(elem_values[i],
2046  elmtype, true));
2047  appendStringInfoString(&buf, "</element>");
2048  }
2049 
2050  pfree(elem_values);
2051  pfree(elem_nulls);
2052 
2053  return buf.data;
2054  }
2055  else
2056  {
2057  Oid typeOut;
2058  bool isvarlena;
2059  char *str;
2060 
2061  /*
2062  * Flatten domains; the special-case treatments below should apply to,
2063  * eg, domains over boolean not just boolean.
2064  */
2065  type = getBaseType(type);
2066 
2067  /*
2068  * Special XSD formatting for some data types
2069  */
2070  switch (type)
2071  {
2072  case BOOLOID:
2073  if (DatumGetBool(value))
2074  return "true";
2075  else
2076  return "false";
2077 
2078  case DATEOID:
2079  {
2080  DateADT date;
2081  struct pg_tm tm;
2082  char buf[MAXDATELEN + 1];
2083 
2084  date = DatumGetDateADT(value);
2085  /* XSD doesn't support infinite values */
2086  if (DATE_NOT_FINITE(date))
2087  ereport(ERROR,
2088  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2089  errmsg("date out of range"),
2090  errdetail("XML does not support infinite date values.")));
2092  &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
2094 
2095  return pstrdup(buf);
2096  }
2097 
2098  case TIMESTAMPOID:
2099  {
2101  struct pg_tm tm;
2102  fsec_t fsec;
2103  char buf[MAXDATELEN + 1];
2104 
2105  timestamp = DatumGetTimestamp(value);
2106 
2107  /* XSD doesn't support infinite values */
2108  if (TIMESTAMP_NOT_FINITE(timestamp))
2109  ereport(ERROR,
2110  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2111  errmsg("timestamp out of range"),
2112  errdetail("XML does not support infinite timestamp values.")));
2113  else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
2114  EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
2115  else
2116  ereport(ERROR,
2117  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2118  errmsg("timestamp out of range")));
2119 
2120  return pstrdup(buf);
2121  }
2122 
2123  case TIMESTAMPTZOID:
2124  {
2126  struct pg_tm tm;
2127  int tz;
2128  fsec_t fsec;
2129  const char *tzn = NULL;
2130  char buf[MAXDATELEN + 1];
2131 
2132  timestamp = DatumGetTimestamp(value);
2133 
2134  /* XSD doesn't support infinite values */
2135  if (TIMESTAMP_NOT_FINITE(timestamp))
2136  ereport(ERROR,
2137  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2138  errmsg("timestamp out of range"),
2139  errdetail("XML does not support infinite timestamp values.")));
2140  else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
2141  EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
2142  else
2143  ereport(ERROR,
2144  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2145  errmsg("timestamp out of range")));
2146 
2147  return pstrdup(buf);
2148  }
2149 
2150 #ifdef USE_LIBXML
2151  case BYTEAOID:
2152  {
2153  bytea *bstr = DatumGetByteaPP(value);
2154  PgXmlErrorContext *xmlerrcxt;
2155  volatile xmlBufferPtr buf = NULL;
2156  volatile xmlTextWriterPtr writer = NULL;
2157  char *result;
2158 
2159  xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
2160 
2161  PG_TRY();
2162  {
2163  buf = xmlBufferCreate();
2164  if (buf == NULL || xmlerrcxt->err_occurred)
2165  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
2166  "could not allocate xmlBuffer");
2167  writer = xmlNewTextWriterMemory(buf, 0);
2168  if (writer == NULL || xmlerrcxt->err_occurred)
2169  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
2170  "could not allocate xmlTextWriter");
2171 
2172  if (xmlbinary == XMLBINARY_BASE64)
2173  xmlTextWriterWriteBase64(writer, VARDATA_ANY(bstr),
2174  0, VARSIZE_ANY_EXHDR(bstr));
2175  else
2176  xmlTextWriterWriteBinHex(writer, VARDATA_ANY(bstr),
2177  0, VARSIZE_ANY_EXHDR(bstr));
2178 
2179  /* we MUST do this now to flush data out to the buffer */
2180  xmlFreeTextWriter(writer);
2181  writer = NULL;
2182 
2183  result = pstrdup((const char *) xmlBufferContent(buf));
2184  }
2185  PG_CATCH();
2186  {
2187  if (writer)
2188  xmlFreeTextWriter(writer);
2189  if (buf)
2190  xmlBufferFree(buf);
2191 
2192  pg_xml_done(xmlerrcxt, true);
2193 
2194  PG_RE_THROW();
2195  }
2196  PG_END_TRY();
2197 
2198  xmlBufferFree(buf);
2199 
2200  pg_xml_done(xmlerrcxt, false);
2201 
2202  return result;
2203  }
2204 #endif /* USE_LIBXML */
2205 
2206  }
2207 
2208  /*
2209  * otherwise, just use the type's native text representation
2210  */
2211  getTypeOutputInfo(type, &typeOut, &isvarlena);
2212  str = OidOutputFunctionCall(typeOut, value);
2213 
2214  /* ... exactly as-is for XML, and when escaping is not wanted */
2215  if (type == XMLOID || !xml_escape_strings)
2216  return str;
2217 
2218  /* otherwise, translate special characters as needed */
2219  return escape_xml(str);
2220  }
2221 }
#define MAXDATELEN
Definition: datetime.h:203
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
Definition: datetime.c:3884
#define TIMESTAMPTZOID
Definition: pg_type.h:525
signed short int16
Definition: c.h:245
#define DATEOID
Definition: pg_type.h:511
#define DatumGetDateADT(X)
Definition: date.h:52
#define type_is_array_domain(typid)
Definition: lsyscache.h:182
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2632
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
int32 DateADT
Definition: date.h:22
int64 timestamp
int64 TimestampTz
Definition: timestamp.h:39
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2021
char * pstrdup(const char *in)
Definition: mcxt.c:1076
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1758
struct PgXmlErrorContext PgXmlErrorContext
Definition: xml.h:48
int errcode(int sqlerrcode)
Definition: elog.c:575
#define DatumGetByteaPP(X)
Definition: fmgr.h:255
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:107
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
void pfree(void *pointer)
Definition: mcxt.c:949
#define TIMESTAMPOID
Definition: pg_type.h:519
#define ERROR
Definition: elog.h:43
#define DATE_NOT_FINITE(j)
Definition: date.h:42
static struct @121 value
#define XMLOID
Definition: pg_type.h:359
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
int tm_mday
Definition: pgtime.h:30
static char * buf
Definition: pg_test_fsync.c:67
int tm_mon
Definition: pgtime.h:31
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define DatumGetBool(X)
Definition: postgres.h:399
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:3999
int32 fsec_t
Definition: timestamp.h:41
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:317
int64 Timestamp
Definition: timestamp.h:38
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
uintptr_t Datum
Definition: postgres.h:372
#define PG_CATCH()
Definition: elog.h:293
#define PG_RE_THROW()
Definition: elog.h:314
#define BOOLOID
Definition: pg_type.h:288
int xmlbinary
Definition: xml.c:96
#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:3449
#define USE_XSD_DATES
Definition: miscadmin.h:214
int tm_year
Definition: pgtime.h:32
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1742
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
Definition: c.h:433
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
char * escape_xml(const char *str)
Definition: xml.c:2232
#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:2013
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2271
#define ARR_ELEMTYPE(a)
Definition: array.h:277
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:27
#define DatumGetArrayTypeP(X)
Definition: array.h:246
char* map_xml_name_to_sql_identifier ( char *  name)

Definition at line 1973 of file xml.c.

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

Referenced by get_rule_expr().

1974 {
1976  char *p;
1977 
1978  initStringInfo(&buf);
1979 
1980  for (p = name; *p; p += pg_mblen(p))
1981  {
1982  if (*p == '_' && *(p + 1) == 'x'
1983  && isxdigit((unsigned char) *(p + 2))
1984  && isxdigit((unsigned char) *(p + 3))
1985  && isxdigit((unsigned char) *(p + 4))
1986  && isxdigit((unsigned char) *(p + 5))
1987  && *(p + 6) == '_')
1988  {
1989  unsigned int u;
1990 
1991  sscanf(p + 2, "%X", &u);
1993  p += 6;
1994  }
1995  else
1996  appendBinaryStringInfo(&buf, p, pg_mblen(p));
1997  }
1998 
1999  return buf.data;
2000 }
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
static char * buf
Definition: pg_test_fsync.c:67
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
int pg_mblen(const char *mbstr)
Definition: mbutils.c:760
const char * name
Definition: encode.c:521
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208
static char * unicode_to_sqlchar(pg_wchar c)
Definition: xml.c:1953
static List* query_to_oid_list ( const char *  query)
static

Definition at line 2321 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().

2322 {
2323  uint64 i;
2324  List *list = NIL;
2325 
2326  SPI_execute(query, true, 0);
2327 
2328  for (i = 0; i < SPI_processed; i++)
2329  {
2330  Datum oid;
2331  bool isnull;
2332 
2333  oid = SPI_getbinval(SPI_tuptable->vals[i],
2335  1,
2336  &isnull);
2337  if (!isnull)
2338  list = lappend_oid(list, DatumGetObjectId(oid));
2339  }
2340 
2341  return list;
2342 }
#define NIL
Definition: pg_list.h:69
#define DatumGetObjectId(X)
Definition: postgres.h:506
SPITupleTable * SPI_tuptable
Definition: spi.c:41
HeapTuple * vals
Definition: spi.h:28
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:844
uintptr_t Datum
Definition: postgres.h:372
TupleDesc tupdesc
Definition: spi.h:27
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:310
Datum query_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 2430 of file xml.c.

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

2431 {
2432  char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2433  bool nulls = PG_GETARG_BOOL(1);
2434  bool tableforest = PG_GETARG_BOOL(2);
2435  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2436 
2438  NULL, nulls, tableforest,
2439  targetns, true)));
2440 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
static StringInfo query_to_xml_internal(const char *query, char *tablename, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2532
char * text_to_cstring(const text *t)
Definition: varlena.c:182
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:445
Datum query_to_xml_and_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2672 of file xml.c.

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

2673 {
2674  char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2675  bool nulls = PG_GETARG_BOOL(1);
2676  bool tableforest = PG_GETARG_BOOL(2);
2677  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2678 
2679  const char *xmlschema;
2680  SPIPlanPtr plan;
2681  Portal portal;
2682 
2683  SPI_connect();
2684 
2685  if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
2686  elog(ERROR, "SPI_prepare(\"%s\") failed", query);
2687 
2688  if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
2689  elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
2690 
2691  xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
2692  InvalidOid, nulls, tableforest, targetns));
2693  SPI_cursor_close(portal);
2694  SPI_finish();
2695 
2697  xmlschema, nulls, tableforest,
2698  targetns, true)));
2699 }
int SPI_connect(void)
Definition: spi.c:84
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:488
int SPI_finish(void)
Definition: spi.c:149
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1037
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define ERROR
Definition: elog.h:43
static char * _SPI_strdup(const char *s)
Definition: xml.c:2264
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
static const char * map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3043
TupleDesc tupDesc
Definition: portal.h:155
#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:2532
void SPI_cursor_close(Portal portal)
Definition: spi.c:1411
char * text_to_cstring(const text *t)
Definition: varlena.c:182
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:445
#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 2532 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().

2535 {
2536  StringInfo result;
2537  char *xmltn;
2538  uint64 i;
2539 
2540  if (tablename)
2541  xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
2542  else
2543  xmltn = "table";
2544 
2545  result = makeStringInfo();
2546 
2547  SPI_connect();
2548  if (SPI_execute(query, true, 0) != SPI_OK_SELECT)
2549  ereport(ERROR,
2550  (errcode(ERRCODE_DATA_EXCEPTION),
2551  errmsg("invalid query")));
2552 
2553  if (!tableforest)
2554  {
2555  xmldata_root_element_start(result, xmltn, xmlschema,
2556  targetns, top_level);
2557  appendStringInfoChar(result, '\n');
2558  }
2559 
2560  if (xmlschema)
2561  appendStringInfo(result, "%s\n\n", xmlschema);
2562 
2563  for (i = 0; i < SPI_processed; i++)
2564  SPI_sql_row_to_xmlelement(i, result, tablename, nulls,
2565  tableforest, targetns, top_level);
2566 
2567  if (!tableforest)
2568  xmldata_root_element_end(result, xmltn);
2569 
2570  SPI_finish();
2571 
2572  return result;
2573 }
static void xmldata_root_element_start(StringInfo result, const char *eltname, const char *xmlschema, const char *targetns, bool top_level)
Definition: xml.c:2498
int SPI_connect(void)
Definition: spi.c:84
int SPI_finish(void)
Definition: spi.c:149
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
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:1897
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:169
#define SPI_OK_SELECT
Definition: spi.h:54
static void xmldata_root_element_end(StringInfo result, const char *eltname)
Definition: xml.c:2525
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:3613
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:310
Datum query_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2596 of file xml.c.

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

2597 {
2598  char *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
2599  bool nulls = PG_GETARG_BOOL(1);
2600  bool tableforest = PG_GETARG_BOOL(2);
2601  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2602  const char *result;
2603  SPIPlanPtr plan;
2604  Portal portal;
2605 
2606  SPI_connect();
2607 
2608  if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
2609  elog(ERROR, "SPI_prepare(\"%s\") failed", query);
2610 
2611  if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
2612  elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
2613 
2615  InvalidOid, nulls,
2616  tableforest, targetns));
2617  SPI_cursor_close(portal);
2618  SPI_finish();
2619 
2621 }
int SPI_connect(void)
Definition: spi.c:84
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:488
int SPI_finish(void)
Definition: spi.c:149
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1037
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define ERROR
Definition: elog.h:43
static char * _SPI_strdup(const char *s)
Definition: xml.c:2264
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
static const char * map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3043
TupleDesc tupDesc
Definition: portal.h:155
#define InvalidOid
Definition: postgres_ext.h:36
static xmltype * cstring_to_xmltype(const char *string)
Definition: xml.c:452
void SPI_cursor_close(Portal portal)
Definition: spi.c:1411
char * text_to_cstring(const text *t)
Definition: varlena.c:182
#define elog
Definition: elog.h:219
static List* schema_get_xml_visible_tables ( Oid  nspid)
static

Definition at line 2346 of file xml.c.

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

Referenced by schema_to_xml_internal(), and schema_to_xmlschema_internal().

2347 {
2348  StringInfoData query;
2349 
2350  initStringInfo(&query);
2351  appendStringInfo(&query, "SELECT oid FROM pg_catalog.pg_class"
2352  " WHERE relnamespace = %u AND relkind IN ("
2356  " AND pg_catalog.has_table_privilege (oid, 'SELECT')"
2357  " ORDER BY relname;", nspid);
2358 
2359  return query_to_oid_list(query.data);
2360 }
static List * query_to_oid_list(const char *query)
Definition: xml.c:2321
#define RELKIND_MATVIEW
Definition: pg_class.h:165
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define CppAsString2(x)
Definition: c.h:162
#define RELKIND_VIEW
Definition: pg_class.h:164
#define RELKIND_RELATION
Definition: pg_class.h:160
Datum schema_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 2751 of file xml.c.

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

2752 {
2753  Name name = PG_GETARG_NAME(0);
2754  bool nulls = PG_GETARG_BOOL(1);
2755  bool tableforest = PG_GETARG_BOOL(2);
2756  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2757 
2758  char *schemaname;
2759  Oid nspid;
2760 
2761  schemaname = NameStr(*name);
2762  nspid = LookupExplicitNamespace(schemaname, false);
2763 
2765  nulls, tableforest, targetns, true)));
2766 }
static StringInfo schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2708
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
Definition: namespace.c:2853
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
Definition: c.h:487
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
const char * name
Definition: encode.c:521
char * text_to_cstring(const text *t)
Definition: varlena.c:182
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:445
#define NameStr(name)
Definition: c.h:493
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
Datum schema_to_xml_and_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2855 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().

2856 {
2857  Name name = PG_GETARG_NAME(0);
2858  bool nulls = PG_GETARG_BOOL(1);
2859  bool tableforest = PG_GETARG_BOOL(2);
2860  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2861  char *schemaname;
2862  Oid nspid;
2863  StringInfo xmlschema;
2864 
2865  schemaname = NameStr(*name);
2866  nspid = LookupExplicitNamespace(schemaname, false);
2867 
2868  xmlschema = schema_to_xmlschema_internal(schemaname, nulls,
2869  tableforest, targetns);
2870 
2872  xmlschema->data, nulls,
2873  tableforest, targetns, true)));
2874 }
static StringInfo schema_to_xmlschema_internal(const char *schemaname, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:2797
static StringInfo schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2708
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
Definition: namespace.c:2853
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
Definition: c.h:487
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
const char * name
Definition: encode.c:521
char * text_to_cstring(const text *t)
Definition: varlena.c:182
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:445
#define NameStr(name)
Definition: c.h:493
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
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 2708 of file xml.c.

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

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

2710 {
2711  StringInfo result;
2712  char *xmlsn;
2713  List *relid_list;
2714  ListCell *cell;
2715 
2717  true, false);
2718  result = makeStringInfo();
2719 
2720  xmldata_root_element_start(result, xmlsn, xmlschema, targetns, top_level);
2721  appendStringInfoChar(result, '\n');
2722 
2723  if (xmlschema)
2724  appendStringInfo(result, "%s\n\n", xmlschema);
2725 
2726  SPI_connect();
2727 
2728  relid_list = schema_get_xml_visible_tables(nspid);
2729 
2730  foreach(cell, relid_list)
2731  {
2732  Oid relid = lfirst_oid(cell);
2733  StringInfo subres;
2734 
2735  subres = table_to_xml_internal(relid, NULL, nulls, tableforest,
2736  targetns, false);
2737 
2738  appendStringInfoString(result, subres->data);
2739  appendStringInfoChar(result, '\n');
2740  }
2741 
2742  SPI_finish();
2743 
2744  xmldata_root_element_end(result, xmlsn);
2745 
2746  return result;
2747 }
static void xmldata_root_element_start(StringInfo result, const char *eltname, const char *xmlschema, const char *targetns, bool top_level)
Definition: xml.c:2498
static List * schema_get_xml_visible_tables(Oid nspid)
Definition: xml.c:2346
int SPI_connect(void)
Definition: spi.c:84
int SPI_finish(void)
Definition: spi.c:149
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
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:1897
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3033
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:169
static StringInfo table_to_xml_internal(Oid relid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2399
static void xmldata_root_element_end(StringInfo result, const char *eltname)
Definition: xml.c:2525
Definition: pg_list.h:45
#define lfirst_oid(lc)
Definition: pg_list.h:108
Datum schema_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2842 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().

2843 {
2844  Name name = PG_GETARG_NAME(0);
2845  bool nulls = PG_GETARG_BOOL(1);
2846  bool tableforest = PG_GETARG_BOOL(2);
2847  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2848 
2850  nulls, tableforest, targetns)));
2851 }
static StringInfo schema_to_xmlschema_internal(const char *schemaname, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:2797
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
Definition: c.h:487
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
const char * name
Definition: encode.c:521
char * text_to_cstring(const text *t)
Definition: varlena.c:182
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:445
#define NameStr(name)
Definition: c.h:493
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
static StringInfo schema_to_xmlschema_internal ( const char *  schemaname,
bool  nulls,
bool  tableforest,
const char *  targetns 
)
static

Definition at line 2797 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().

2799 {
2800  Oid nspid;
2801  List *relid_list;
2802  List *tupdesc_list;
2803  ListCell *cell;
2804  StringInfo result;
2805 
2806  result = makeStringInfo();
2807 
2808  nspid = LookupExplicitNamespace(schemaname, false);
2809 
2810  xsd_schema_element_start(result, targetns);
2811 
2812  SPI_connect();
2813 
2814  relid_list = schema_get_xml_visible_tables(nspid);
2815 
2816  tupdesc_list = NIL;
2817  foreach(cell, relid_list)
2818  {
2819  Relation rel;
2820 
2821  rel = heap_open(lfirst_oid(cell), AccessShareLock);
2822  tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
2823  heap_close(rel, NoLock);
2824  }
2825 
2826  appendStringInfoString(result,
2827  map_sql_typecoll_to_xmlschema_types(tupdesc_list));
2828 
2829  appendStringInfoString(result,
2830  map_sql_schema_to_xmlschema_types(nspid, relid_list,
2831  nulls, tableforest, targetns));
2832 
2833  xsd_schema_element_end(result);
2834 
2835  SPI_finish();
2836 
2837  return result;
2838 }
static void xsd_schema_element_start(StringInfo result, const char *targetns)
Definition: xml.c:2773
#define NIL
Definition: pg_list.h:69
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:102
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
Definition: namespace.c:2853
static List * schema_get_xml_visible_tables(Oid nspid)
Definition: xml.c:2346
int SPI_connect(void)
Definition: spi.c:84
int SPI_finish(void)
Definition: spi.c:149
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
#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:157
#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:3383
static void xsd_schema_element_end(StringInfo result)
Definition: xml.c:2790
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1290
TupleDesc rd_att
Definition: rel.h:115
static const char * map_sql_schema_to_xmlschema_types(Oid nspid, List *relid_list, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3148
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 3613 of file xml.c.

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

Referenced by cursor_to_xml(), and query_to_xml_internal().

3616 {
3617  int i;
3618  char *xmltn;
3619 
3620  if (tablename)
3621  xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
3622  else
3623  {
3624  if (tableforest)
3625  xmltn = "row";
3626  else
3627  xmltn = "table";
3628  }
3629 
3630  if (tableforest)
3631  xmldata_root_element_start(result, xmltn, NULL, targetns, top_level);
3632  else
3633  appendStringInfoString(result, "<row>\n");
3634 
3635  for (i = 1; i <= SPI_tuptable->tupdesc->natts; i++)
3636  {
3637  char *colname;
3638  Datum colval;
3639  bool isnull;
3640 
3642  true, false);
3643  colval = SPI_getbinval(SPI_tuptable->vals[rownum],
3645  i,
3646  &isnull);
3647  if (isnull)
3648  {
3649  if (nulls)
3650  appendStringInfo(result, " <%s xsi:nil=\"true\"/>\n", colname);
3651  }
3652  else
3653  appendStringInfo(result, " <%s>%s</%s>\n",
3654  colname,
3656  SPI_gettypeid(SPI_tuptable->tupdesc, i), true),
3657  colname);
3658  }
3659 
3660  if (tableforest)
3661  {
3662  xmldata_root_element_end(result, xmltn);
3663  appendStringInfoChar(result, '\n');
3664  }
3665  else
3666  appendStringInfoString(result, "</row>\n\n");
3667 }
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition: spi.c:900
static void xmldata_root_element_start(StringInfo result, const char *eltname, const char *xmlschema, const char *targetns, bool top_level)
Definition: xml.c:2498
SPITupleTable * SPI_tuptable
Definition: spi.c:41
HeapTuple * vals
Definition: spi.h:28
int natts
Definition: tupdesc.h:73
char * SPI_fname(TupleDesc tupdesc, int fnumber)
Definition: spi.c:790
char * map_sql_identifier_to_xml_name(char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:1897
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition: spi.c:844
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:169
uintptr_t Datum
Definition: postgres.h:372
TupleDesc tupdesc
Definition: spi.h:27
static void xmldata_root_element_end(StringInfo result, const char *eltname)
Definition: xml.c:2525
int i
char * map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
Definition: xml.c:2013
Datum table_to_xml ( PG_FUNCTION_ARGS  )

Definition at line 2416 of file xml.c.

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

2417 {
2418  Oid relid = PG_GETARG_OID(0);
2419  bool nulls = PG_GETARG_BOOL(1);
2420  bool tableforest = PG_GETARG_BOOL(2);
2421  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2422 
2424  nulls, tableforest,
2425  targetns, true)));
2426 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
static StringInfo table_to_xml_internal(Oid relid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2399
char * text_to_cstring(const text *t)
Definition: varlena.c:182
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:445
Datum table_to_xml_and_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2651 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().

2652 {
2653  Oid relid = PG_GETARG_OID(0);
2654  bool nulls = PG_GETARG_BOOL(1);
2655  bool tableforest = PG_GETARG_BOOL(2);
2656  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2657  Relation rel;
2658  const char *xmlschema;
2659 
2660  rel = heap_open(relid, AccessShareLock);
2661  xmlschema = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2662  tableforest, targetns);
2663  heap_close(rel, NoLock);
2664 
2666  xmlschema, nulls, tableforest,
2667  targetns, true)));
2668 }
#define AccessShareLock
Definition: lockdefs.h:36
#define heap_close(r, l)
Definition: heapam.h:97
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define NoLock
Definition: lockdefs.h:34
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
static const char * map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3043
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1290
TupleDesc rd_att
Definition: rel.h:115
static StringInfo table_to_xml_internal(Oid relid, const char *xmlschema, bool nulls, bool tableforest, const char *targetns, bool top_level)
Definition: xml.c:2399
char * text_to_cstring(const text *t)
Definition: varlena.c:182
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:445
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 2399 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().

2402 {
2403  StringInfoData query;
2404 
2405  initStringInfo(&query);
2406  appendStringInfo(&query, "SELECT * FROM %s",
2408  ObjectIdGetDatum(relid))));
2409  return query_to_xml_internal(query.data, get_rel_name(relid),
2410  xmlschema, nulls, tableforest,
2411  targetns, top_level);
2412 }
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:585
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define DatumGetCString(X)
Definition: postgres.h:572
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
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:2532
Datum regclassout(PG_FUNCTION_ARGS)
Definition: regproc.c:972
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1726
Datum table_to_xmlschema ( PG_FUNCTION_ARGS  )

Definition at line 2577 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().

2578 {
2579  Oid relid = PG_GETARG_OID(0);
2580  bool nulls = PG_GETARG_BOOL(1);
2581  bool tableforest = PG_GETARG_BOOL(2);
2582  const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
2583  const char *result;
2584  Relation rel;
2585 
2586  rel = heap_open(relid, AccessShareLock);
2587  result = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
2588  tableforest, targetns);
2589  heap_close(rel, NoLock);
2590 
2592 }
#define AccessShareLock
Definition: lockdefs.h:36
#define heap_close(r, l)
Definition: heapam.h:97
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define NoLock
Definition: lockdefs.h:34
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
static const char * map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls, bool tableforest, const char *targetns)
Definition: xml.c:3043
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1290
TupleDesc rd_att
Definition: rel.h:115
static xmltype * cstring_to_xmltype(const char *string)
Definition: xml.c:452
char * text_to_cstring(const text *t)
Definition: varlena.c:182
Datum texttoxml ( PG_FUNCTION_ARGS  )

Definition at line 594 of file xml.c.

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

595 {
596  text *data = PG_GETARG_TEXT_PP(0);
597 
598  PG_RETURN_XML_P(xmlparse(data, xmloption, true));
599 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
int xmloption
Definition: xml.c:97
Definition: c.h:433
xmltype * xmlparse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace)
Definition: xml.c:750
static char* unicode_to_sqlchar ( pg_wchar  c)
static

Definition at line 1953 of file xml.c.

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

Referenced by map_xml_name_to_sql_identifier().

1954 {
1955  char utf8string[8]; /* need room for trailing zero */
1956  char *result;
1957 
1958  memset(utf8string, 0, sizeof(utf8string));
1959  unicode_to_utf8(c, (unsigned char *) utf8string);
1960 
1961  result = pg_any_to_server(utf8string, strlen(utf8string), PG_UTF8);
1962  /* if pg_any_to_server didn't strdup, we must */
1963  if (result == utf8string)
1964  result = pstrdup(result);
1965  return result;
1966 }
unsigned char * unicode_to_utf8(pg_wchar c, unsigned char *utf8string)
Definition: wchar.c:475
char * pstrdup(const char *in)
Definition: mcxt.c:1076
char * c
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:561
Datum xml_in ( PG_FUNCTION_ARGS  )

Definition at line 256 of file xml.c.

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

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

Definition at line 886 of file xml.c.

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

Referenced by ExecEvalXmlExpr(), and xmltotext_with_xmloption().

887 {
888 #ifdef USE_LIBXML
889  bool result;
890  volatile xmlDocPtr doc = NULL;
892 
893  /* We want to catch ereport(INVALID_XML_DOCUMENT) and return false */
894  PG_TRY();
895  {
896  doc = xml_parse((text *) arg, XMLOPTION_DOCUMENT, true,
898  result = true;
899  }
900  PG_CATCH();
901  {
902  ErrorData *errdata;
903  MemoryContext ecxt;
904 
905  ecxt = MemoryContextSwitchTo(ccxt);
906  errdata = CopyErrorData();
907  if (errdata->sqlerrcode == ERRCODE_INVALID_XML_DOCUMENT)
908  {
909  FlushErrorState();
910  result = false;
911  }
912  else
913  {
914  MemoryContextSwitchTo(ecxt);
915  PG_RE_THROW();
916  }
917  }
918  PG_END_TRY();
919 
920  if (doc)
921  xmlFreeDoc(doc);
922 
923  return result;
924 #else /* not USE_LIBXML */
925  NO_XML_SUPPORT();
926  return false;
927 #endif /* not USE_LIBXML */
928 }
#define NO_XML_SUPPORT()
Definition: xml.c:217
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:1004
#define PG_CATCH()
Definition: elog.h:293
#define PG_RE_THROW()
Definition: elog.h:314
Definition: c.h:433
#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 4112 of file xml.c.

References NO_XML_SUPPORT, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and xmloption.

4113 {
4114 #ifdef USE_LIBXML
4115  text *data = PG_GETARG_TEXT_PP(0);
4116 
4117  PG_RETURN_BOOL(wellformed_xml(data, xmloption));
4118 #else
4119  NO_XML_SUPPORT();
4120  return 0;
4121 #endif /* not USE_LIBXML */
4122 }
#define NO_XML_SUPPORT()
Definition: xml.c:217
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
int xmloption
Definition: xml.c:97
Definition: c.h:433
Datum xml_is_well_formed_content ( PG_FUNCTION_ARGS  )

Definition at line 4138 of file xml.c.

References NO_XML_SUPPORT, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and XMLOPTION_CONTENT.

4139 {
4140 #ifdef USE_LIBXML
4141  text *data = PG_GETARG_TEXT_PP(0);
4142 
4143  PG_RETURN_BOOL(wellformed_xml(data, XMLOPTION_CONTENT));
4144 #else
4145  NO_XML_SUPPORT();
4146  return 0;
4147 #endif /* not USE_LIBXML */
4148 }
#define NO_XML_SUPPORT()
Definition: xml.c:217
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Definition: c.h:433
Datum xml_is_well_formed_document ( PG_FUNCTION_ARGS  )

Definition at line 4125 of file xml.c.

References NO_XML_SUPPORT, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and XMLOPTION_DOCUMENT.

4126 {
4127 #ifdef USE_LIBXML
4128  text *data = PG_GETARG_TEXT_PP(0);
4129 
4130  PG_RETURN_BOOL(wellformed_xml(data, XMLOPTION_DOCUMENT));
4131 #else
4132  NO_XML_SUPPORT();
4133  return 0;
4134 #endif /* not USE_LIBXML */
4135 }
#define NO_XML_SUPPORT()
Definition: xml.c:217
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Definition: c.h:433
Datum xml_out ( PG_FUNCTION_ARGS  )

Definition at line 334 of file xml.c.

References PG_GETARG_XML_P, PG_RETURN_CSTRING, and xml_out_internal().

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

Definition at line 291 of file xml.c.

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

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

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

Definition at line 349 of file xml.c.

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

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

Definition at line 416 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().

417 {
418  xmltype *x = PG_GETARG_XML_P(0);
419  char *outval;
421 
422  /*
423  * xml_out_internal doesn't convert the encoding, it just prints the right
424  * declaration. pq_sendtext will do the conversion.
425  */
427 
428  pq_begintypsend(&buf);
429  pq_sendtext(&buf, outval, strlen(outval));
430  pfree(outval);
432 }
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
void pq_sendtext(StringInfo buf, const char *str, int slen)
Definition: pqformat.c:174
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
void pfree(void *pointer)
Definition: mcxt.c:949
static char * buf
Definition: pg_test_fsync.c:67
#define PG_GETARG_XML_P(n)
Definition: xml.h:53
int pg_get_client_encoding(void)
Definition: mbutils.c:306
static char * xml_out_internal(xmltype *x, pg_enc target_encoding)
Definition: xml.c:291
Definition: c.h:433
Datum xmlcomment ( PG_FUNCTION_ARGS  )

Definition at line 469 of file xml.c.

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

470 {
471 #ifdef USE_LIBXML
472  text *arg = PG_GETARG_TEXT_PP(0);
473  char *argdata = VARDATA_ANY(arg);
474  int len = VARSIZE_ANY_EXHDR(arg);
476  int i;
477 
478  /* check for "--" in string or "-" at the end */
479  for (i = 1; i < len; i++)
480  {
481  if (argdata[i] == '-' && argdata[i - 1] == '-')
482  ereport(ERROR,
483  (errcode(ERRCODE_INVALID_XML_COMMENT),
484  errmsg("invalid XML comment")));
485  }
486  if (len > 0 && argdata[len - 1] == '-')
487  ereport(ERROR,
488  (errcode(ERRCODE_INVALID_XML_COMMENT),
489  errmsg("invalid XML comment")));
490 
491  initStringInfo(&buf);
492  appendStringInfoString(&buf, "<!--");
493  appendStringInfoText(&buf, arg);
494  appendStringInfoString(&buf, "-->");
495 
497 #else
498  NO_XML_SUPPORT();
499  return 0;
500 #endif
501 }
#define NO_XML_SUPPORT()
Definition: xml.c:217
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define ERROR
Definition: elog.h:43
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:3677
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
static char * buf
Definition: pg_test_fsync.c:67
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
#define ereport(elevel, rest)
Definition: elog.h:122
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
int errmsg(const char *fmt,...)
Definition: elog.c:797
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:445
int i
void * arg
Definition: c.h:433
xmltype* xmlconcat ( List args)

Definition at line 510 of file xml.c.

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

Referenced by ExecEvalXmlExpr(), and xmlconcat2().

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

Definition at line 576 of file xml.c.

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

577 {
578  if (PG_ARGISNULL(0))
579  {
580  if (PG_ARGISNULL(1))
581  PG_RETURN_NULL();
582  else
584  }
585  else if (PG_ARGISNULL(1))
587  else
589  PG_GETARG_XML_P(1))));
590 }
#define list_make2(x1, x2)
Definition: pg_list.h:140
xmltype * xmlconcat(List *args)
Definition: xml.c:510
#define PG_RETURN_XML_P(x)
Definition: xml.h:54
#define PG_GETARG_XML_P(n)
Definition: xml.h:53
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define PG_RETURN_NULL()
Definition: fmgr.h:305
static void xmldata_root_element_end ( StringInfo  result,
const char *  eltname 
)
static

Definition at line 2525 of file xml.c.

References appendStringInfo().

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

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

Definition at line 2498 of file xml.c.

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

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

2501 {
2502  /* This isn't really wrong but currently makes no sense. */
2503  Assert(top_level || !xmlschema);
2504 
2505  appendStringInfo(result, "<%s", eltname);
2506  if (top_level)
2507  {
2508  appendStringInfoString(result, " xmlns:xsi=\"" NAMESPACE_XSI "\"");
2509  if (strlen(targetns) > 0)
2510  appendStringInfo(result, " xmlns=\"%s\"", targetns);
2511  }
2512  if (xmlschema)
2513  {
2514  /* FIXME: better targets */
2515  if (strlen(targetns) > 0)
2516  appendStringInfo(result, " xsi:schemaLocation=\"%s #\"", targetns);
2517  else
2518  appendStringInfoString(result, " xsi:noNamespaceSchemaLocation=\"#\"");
2519  }
2520  appendStringInfoString(result, ">\n");
2521 }
#define NAMESPACE_XSI
Definition: xml.c:227
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
#define Assert(condition)
Definition: c.h:681
xmltype* xmlelement ( XmlExpr xexpr,
Datum named_argvalue,
bool named_argnull,
Datum argvalue,
bool argnull 
)

Definition at line 626 of file xml.c.

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

Referenced by ExecEvalXmlExpr().

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

References NO_XML_SUPPORT, PG_GETARG_TEXT_PP, PG_GETARG_XML_P, and PG_RETURN_BOOL.

4040 {
4041 #ifdef USE_LIBXML
4042  text *xpath_expr_text = PG_GETARG_TEXT_PP(0);
4043  xmltype *data = PG_GETARG_XML_P(1);
4044  int res_nitems;
4045 
4046  xpath_internal(xpath_expr_text, data, NULL,
4047  &res_nitems, NULL);
4048 
4049  PG_RETURN_BOOL(res_nitems > 0);
4050 #else
4051  NO_XML_SUPPORT();
4052  return 0;
4053 #endif
4054 }
#define NO_XML_SUPPORT()
Definition: xml.c:217
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_GETARG_XML_P(n)
Definition: xml.h:53
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Definition: c.h:433
xmltype* xmlparse ( text data,
XmlOptionType  xmloption_arg,
bool  preserve_whitespace 
)

Definition at line 750 of file xml.c.

References GetDatabaseEncoding(), and NO_XML_SUPPORT.

Referenced by ExecEvalXmlExpr(), and texttoxml().

751 {
752 #ifdef USE_LIBXML
753  xmlDocPtr doc;
754 
755  doc = xml_parse(data, xmloption_arg, preserve_whitespace,
757  xmlFreeDoc(doc);
758 
759  return (xmltype *) data;
760 #else
761  NO_XML_SUPPORT();
762  return NULL;
763 #endif
764 }
#define NO_XML_SUPPORT()
Definition: xml.c:217
int GetDatabaseEncoding(void)
Definition: mbutils.c:1004
Definition: c.h:433
xmltype* xmlpi ( char *  target,
text arg,
bool  arg_is_null,
bool result_is_null 
)

Definition at line 768 of file xml.c.

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

Referenced by ExecEvalXmlExpr().

769 {
770 #ifdef USE_LIBXML
771  xmltype *result;
773 
774  if (pg_strcasecmp(target, "xml") == 0)
775  ereport(ERROR,
776  (errcode(ERRCODE_SYNTAX_ERROR), /* really */
777  errmsg("invalid XML processing instruction"),
778  errdetail("XML processing instruction target name cannot be \"%s\".", target)));
779 
780  /*
781  * Following the SQL standard, the null check comes after the syntax check
782  * above.
783  */
784  *result_is_null = arg_is_null;
785  if (*result_is_null)
786  return NULL;
787 
788  initStringInfo(&buf);
789 
790  appendStringInfo(&buf, "<?%s", target);
791 
792  if (arg != NULL)
793  {
794  char *string;
795 
796  string = text_to_cstring(arg);
797  if (strstr(string, "?>") != NULL)
798  ereport(ERROR,
799  (errcode(ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION),
800  errmsg("invalid XML processing instruction"),
801  errdetail("XML processing instruction cannot contain \"?>\".")));
802 
803  appendStringInfoChar(&buf, ' ');
804  appendStringInfoString(&buf, string + strspn(string, " "));
805  pfree(string);
806  }
807  appendStringInfoString(&buf, "?>");
808 
809  result = stringinfo_to_xmltype(&buf);
810  pfree(buf.data);
811  return result;
812 #else
813  NO_XML_SUPPORT();
814  return NULL;
815 #endif
816 }
#define NO_XML_SUPPORT()
Definition: xml.c:217
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:949
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define ERROR
Definition: elog.h:43
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
static char * buf
Definition: pg_test_fsync.c:67
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:169
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
char * text_to_cstring(const text *t)
Definition: varlena.c:182
int errmsg(const char *fmt,...)
Definition: elog.c:797
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:445
Definition: c.h:433
xmltype* xmlroot ( xmltype data,
text version,
int  standalone 
)

Definition at line 820 of file xml.c.

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

Referenced by ExecEvalXmlExpr().

821 {
822 #ifdef USE_LIBXML
823  char *str;
824  size_t len;
825  xmlChar *orig_version;
826  int orig_standalone;
828 
829  len = VARSIZE(data) - VARHDRSZ;
830  str = text_to_cstring((text *) data);
831 
832  parse_xml_decl((xmlChar *) str, &len, &orig_version, NULL, &orig_standalone);
833 
834  if (version)
835  orig_version = xml_text2xmlChar(version);
836  else
837  orig_version = NULL;
838 
839  switch (standalone)
840  {
841  case XML_STANDALONE_YES:
842  orig_standalone = 1;
843  break;
844  case XML_STANDALONE_NO:
845  orig_standalone = 0;
846  break;
848  orig_standalone = -1;
849  break;
851  /* leave original value */
852  break;
853  }
854 
855  initStringInfo(&buf);
856  print_xml_decl(&buf, orig_version, 0, orig_standalone);
857  appendStringInfoString(&buf, str + len);
858 
859  return stringinfo_to_xmltype(&buf);
860 #else
861  NO_XML_SUPPORT();
862  return NULL;
863 #endif
864 }
#define NO_XML_SUPPORT()
Definition: xml.c:217
#define VARSIZE(PTR)
Definition: postgres.h:304
#define VARHDRSZ
Definition: c.h:439
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
static char * buf
Definition: pg_test_fsync.c:67
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
char * text_to_cstring(const text *t)
Definition: varlena.c:182
static xmltype * stringinfo_to_xmltype(StringInfo buf)
Definition: xml.c:445
Definition: c.h:433
static void XmlTableDestroyOpaque ( struct TableFuncScanState state)
static

Definition at line 4599 of file xml.c.

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

4600 {
4601 #ifdef USE_LIBXML
4602  XmlTableBuilderData *xtCxt;
4603 
4604  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableDestroyOpaque");
4605 
4606  /* Propagate context related error context to libxml2 */
4607  xmlSetStructuredErrorFunc((void *) xtCxt->xmlerrcxt, xml_errorHandler);
4608 
4609  if (xtCxt->xpathscomp != NULL)
4610  {
4611  int i;
4612 
4613  for (i = 0; i < xtCxt->natts; i++)
4614  if (xtCxt->xpathscomp[i] != NULL)
4615  xmlXPathFreeCompExpr(xtCxt->xpathscomp[i]);
4616  }
4617 
4618  if (xtCxt->xpathobj != NULL)
4619  xmlXPathFreeObject(xtCxt->xpathobj);
4620  if (xtCxt->xpathcomp != NULL)
4621  xmlXPathFreeCompExpr(xtCxt->xpathcomp);
4622  if (xtCxt->xpathcxt != NULL)
4623  xmlXPathFreeContext(xtCxt->xpathcxt);
4624  if (xtCxt->doc != NULL)
4625  xmlFreeDoc(xtCxt->doc);
4626  if (xtCxt->ctxt != NULL)
4627  xmlFreeParserCtxt(xtCxt->ctxt);
4628 
4629  pg_xml_done(xtCxt->xmlerrcxt, true);
4630 
4631  /* not valid anymore */
4632  xtCxt->magic = 0;
4633  state->opaque = NULL;
4634 
4635 #else
4636  NO_XML_SUPPORT();
4637 #endif /* not USE_LIBXML */
4638 }
#define NO_XML_SUPPORT()
Definition: xml.c:217
void pg_xml_done(PgXmlErrorContext *errcxt, bool isError)
int i
static bool XmlTableFetchRow ( struct TableFuncScanState state)
static

Definition at line 4382 of file xml.c.

References ERROR, NO_XML_SUPPORT, and xml_ereport().

4383 {
4384 #ifdef USE_LIBXML
4385  XmlTableBuilderData *xtCxt;
4386 
4387  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableFetchRow");
4388 
4389  /*
4390  * XmlTable returns table - set of composite values. The error context, is
4391  * used for producement more values, between two calls, there can be
4392  * created and used another libxml2 error context. It is libxml2 global
4393  * value, so it should be refreshed any time before any libxml2 usage,
4394  * that is finished by returning some value.
4395  */
4396  xmlSetStructuredErrorFunc((void *) xtCxt->xmlerrcxt, xml_errorHandler);
4397 
4398  if (xtCxt->xpathobj == NULL)
4399  {
4400  xtCxt->xpathobj = xmlXPathCompiledEval(xtCxt->xpathcomp, xtCxt->xpathcxt);
4401  if (xtCxt->xpathobj == NULL || xtCxt->xmlerrcxt->err_occurred)
4402  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
4403  "could not create XPath object");
4404 
4405  xtCxt->row_count = 0;
4406  }
4407 
4408  if (xtCxt->xpathobj->type == XPATH_NODESET)
4409  {
4410  if (xtCxt->xpathobj->nodesetval != NULL)
4411  {
4412  if (xtCxt->row_count++ < xtCxt->xpathobj->nodesetval->nodeNr)
4413  return true;
4414  }
4415  }
4416 
4417  return false;
4418 #else
4419  NO_XML_SUPPORT();
4420  return false;
4421 #endif /* not USE_LIBXML */
4422 }
#define NO_XML_SUPPORT()
Definition: xml.c:217
#define ERROR
Definition: elog.h:43
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
static Datum XmlTableGetValue ( struct TableFuncScanState state,
int  colnum,
Oid  typid,
int32  typmod,
bool isnull 
)
static

Definition at line 4433 of file xml.c.

References appendStringInfoText(), Assert, cur, StringInfoData::data, elog, ereport, errcode(), errmsg(), ERROR, i, TableFuncScanState::in_functions, initStringInfo(), InputFunctionCall(), NO_XML_SUPPORT, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, pstrdup(), text_to_cstring(), TableFuncScanState::typioparams, xml_ereport(), and XMLOID.

4435 {
4436 #ifdef USE_LIBXML
4437  XmlTableBuilderData *xtCxt;
4438  Datum result = (Datum) 0;
4439  xmlNodePtr cur;
4440  char *cstr = NULL;
4441  volatile xmlXPathObjectPtr xpathobj = NULL;
4442 
4443  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableGetValue");
4444 
4445  Assert(xtCxt->xpathobj &&
4446  xtCxt->xpathobj->type == XPATH_NODESET &&
4447  xtCxt->xpathobj->nodesetval != NULL);
4448 
4449  /* Propagate context related error context to libxml2 */
4450  xmlSetStructuredErrorFunc((void *) xtCxt->xmlerrcxt, xml_errorHandler);
4451 
4452  *isnull = false;
4453 
4454  cur = xtCxt->xpathobj->nodesetval->nodeTab[xtCxt->row_count - 1];
4455 
4456  Assert(xtCxt->xpathscomp[colnum] != NULL);
4457 
4458  PG_TRY();
4459  {
4460  /* Set current node as entry point for XPath evaluation */
4461  xtCxt->xpathcxt->node = cur;
4462 
4463  /* Evaluate column path */
4464  xpathobj = xmlXPathCompiledEval(xtCxt->xpathscomp[colnum], xtCxt->xpathcxt);
4465  if (xpathobj == NULL || xtCxt->xmlerrcxt->err_occurred)
4466  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
4467  "could not create XPath object");
4468 
4469  /*
4470  * There are four possible cases, depending on the number of nodes
4471  * returned by the XPath expression and the type of the target column:
4472  * a) XPath returns no nodes. b) One node is returned, and column is
4473  * of type XML. c) One node, column type other than XML. d) Multiple
4474  * nodes are returned.
4475  */
4476  if (xpathobj->type == XPATH_NODESET)
4477  {
4478  int count = 0;
4479 
4480  if (xpathobj->nodesetval != NULL)
4481  count = xpathobj->nodesetval->nodeNr;
4482 
4483  if (xpathobj->nodesetval == NULL || count == 0)
4484  {
4485  *isnull = true;
4486  }
4487  else if (count == 1 && typid == XMLOID)
4488  {
4489  text *textstr;
4490 
4491  /* simple case, result is one value */
4492  textstr = xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[0],
4493  xtCxt->xmlerrcxt);
4494  cstr = text_to_cstring(textstr);
4495  }
4496  else if (count == 1)
4497  {
4498  xmlChar *str;
4499 
4500  str = xmlNodeListGetString(xtCxt->doc,
4501  xpathobj->nodesetval->nodeTab[0]->xmlChildrenNode,
4502  1);
4503 
4504  if (str != NULL)
4505  {
4506  PG_TRY();
4507  {
4508  cstr = pstrdup((char *) str);
4509  }
4510  PG_CATCH();
4511  {
4512  xmlFree(str);
4513  PG_RE_THROW();
4514  }
4515  PG_END_TRY();
4516  xmlFree(str);
4517  }
4518  else
4519  {
4520  /*
4521  * This line ensure mapping of empty tags to PostgreSQL
4522  * value. Usually we would to map a empty tag to empty
4523  * string. But this mapping can create empty string when
4524  * user doesn't expect it - when empty tag is enforced by
4525  * libxml2 - when user uses a text() function for example.
4526  */
4527  cstr = "";
4528  }
4529  }
4530  else
4531  {
4532  StringInfoData str;
4533  int i;
4534 
4535  Assert(count > 1);
4536 
4537  /*
4538  * When evaluating the XPath expression returns multiple
4539  * nodes, the result is the concatenation of them all. The
4540  * target type must be XML.
4541  */
4542  if (typid != XMLOID)
4543  ereport(ERROR,
4544  (errcode(ERRCODE_CARDINALITY_VIOLATION),
4545  errmsg("more than one value returned by column XPath expression")));
4546 
4547  /* Concatenate serialized values */
4548  initStringInfo(&str);
4549  for (i = 0; i < count; i++)
4550  {
4551  appendStringInfoText(&str,
4552  xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[i],
4553  xtCxt->xmlerrcxt));
4554  }
4555  cstr = str.data;
4556  }
4557  }
4558  else if (xpathobj->type == XPATH_STRING)
4559  {
4560  cstr = (char *) xpathobj->stringval;
4561  }
4562  else
4563  elog(ERROR, "unexpected XPath object type %u", xpathobj->type);
4564 
4565  /*
4566  * By here, either cstr contains the result value, or the isnull flag
4567  * has been set.
4568  */
4569  Assert(cstr || *isnull);
4570 
4571  if (!*isnull)
4572  result = InputFunctionCall(&state->in_functions[colnum],
4573  cstr,
4574  state->typioparams[colnum],
4575  typmod);
4576  }
4577  PG_CATCH();
4578  {
4579  if (xpathobj != NULL)
4580  xmlXPathFreeObject(xpathobj);
4581  PG_RE_THROW();
4582  }
4583  PG_END_TRY();
4584 
4585  xmlXPathFreeObject(xpathobj);
4586 
4587  return result;
4588 #else
4589  NO_XML_SUPPORT();
4590  return 0;
4591 #endif /* not USE_LIBXML */
4592 }
#define NO_XML_SUPPORT()
Definition: xml.c:217
char * pstrdup(const char *in)
Definition: mcxt.c:1076
struct cursor * cur
Definition: ecpg.c:28
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:3677
#define XMLOID
Definition: pg_type.h:359
#define ereport(elevel, rest)
Definition: elog.h:122
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
uintptr_t Datum
Definition: postgres.h:372
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1618
#define PG_CATCH()
Definition: elog.h:293
#define Assert(condition)
Definition: c.h:681
FmgrInfo * in_functions
Definition: execnodes.h:1474
#define PG_RE_THROW()
Definition: elog.h:314
char * text_to_cstring(const text *t)
Definition: varlena.c:182
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
Definition: c.h:433
#define elog
Definition: elog.h:219
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
static void XmlTableInitOpaque ( struct TableFuncScanState state,
int  natts 
)
static

Definition at line 4187 of file xml.c.

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

4188 {
4189 #ifdef USE_LIBXML
4190  volatile xmlParserCtxtPtr ctxt = NULL;
4191  XmlTableBuilderData *xtCxt;
4192  PgXmlErrorContext *xmlerrcxt;
4193 
4194  xtCxt = palloc0(sizeof(XmlTableBuilderData));
4195  xtCxt->magic = XMLTABLE_CONTEXT_MAGIC;
4196  xtCxt->natts = natts;
4197  xtCxt->xpathscomp = palloc0(sizeof(xmlXPathCompExprPtr) * natts);
4198 
4199  xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
4200 
4201  PG_TRY();
4202  {
4203  xmlInitParser();
4204 
4205  ctxt = xmlNewParserCtxt();
4206  if (ctxt == NULL || xmlerrcxt->err_occurred)
4207  xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
4208  "could not allocate parser context");
4209  }
4210  PG_CATCH();
4211  {
4212  if (ctxt != NULL)
4213  xmlFreeParserCtxt(ctxt);
4214 
4215  pg_xml_done(xmlerrcxt, true);
4216 
4217  PG_RE_THROW();
4218  }
4219  PG_END_TRY();
4220 
4221  xtCxt->xmlerrcxt = xmlerrcxt;
4222  xtCxt->ctxt = ctxt;
4223 
4224  state->opaque = xtCxt;
4225 #else
4226  NO_XML_SUPPORT();
4227 #endif /* not USE_LIBXML */
4228 }
#define NO_XML_SUPPORT()
Definition: xml.c:217
struct PgXmlErrorContext PgXmlErrorContext
Definition: xml.h:48
#define ERROR
Definition: elog.h:43
void pg_xml_done(PgXmlErrorContext *errcxt, bool isError)
void * palloc0(Size size)
Definition: mcxt.c:877
#define PG_CATCH()
Definition: elog.h:293
#define PG_RE_THROW()
Definition: elog.h:314
#define PG_TRY()
Definition: elog.h:284
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)
static void XmlTableSetColumnFilter ( struct TableFuncScanState state,
char *  path,
int  colnum 
)
static

Definition at line 4350 of file xml.c.

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

4351 {
4352 #ifdef USE_LIBXML
4353  XmlTableBuilderData *xtCxt;
4354  xmlChar *xstr;
4355 
4356  AssertArg(PointerIsValid(path));
4357 
4358  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetColumnFilter");
4359 
4360  if (*path == '\0')
4361  ereport(ERROR,
4362  (errcode(ERRCODE_DATA_EXCEPTION),
4363  errmsg("column path filter must not be empty string")));
4364 
4365  xstr = pg_xmlCharStrndup(path, strlen(path));
4366 
4367  xtCxt->xpathscomp[colnum] = xmlXPathCompile(xstr);
4368  if (xtCxt->xpathscomp[colnum] == NULL || xtCxt->xmlerrcxt->err_occurred)
4369  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_DATA_EXCEPTION,
4370  "invalid XPath expression");
4371 #else
4372  NO_XML_SUPPORT();
4373 #endif /* not USE_LIBXML */
4374 }
#define NO_XML_SUPPORT()
Definition: xml.c:217
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define AssertArg(condition)
Definition: c.h:683
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PointerIsValid(pointer)
Definition: c.h:520
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
static void XmlTableSetDocument ( struct TableFuncScanState state,
Datum  value 
)
static

Definition at line 4235 of file xml.c.

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

4236 {
4237 #ifdef USE_LIBXML
4238  XmlTableBuilderData *xtCxt;
4239  xmltype *xmlval = DatumGetXmlP(value);
4240  char *str;
4241  xmlChar *xstr;
4242  int length;
4243  volatile xmlDocPtr doc = NULL;
4244  volatile xmlXPathContextPtr xpathcxt = NULL;
4245 
4246  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetDocument");
4247 
4248  /*
4249  * Use out function for casting to string (remove encoding property). See
4250  * comment in xml_out.
4251  */
4252  str = xml_out_internal(xmlval, 0);
4253 
4254  length = strlen(str);
4255  xstr = pg_xmlCharStrndup(str, length);
4256 
4257  PG_TRY();
4258  {
4259  doc = xmlCtxtReadMemory(xtCxt->ctxt, (char *) xstr, length, NULL, NULL, 0);
4260  if (doc == NULL || xtCxt->xmlerrcxt->err_occurred)
4261  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INVALID_XML_DOCUMENT,
4262  "could not parse XML document");
4263  xpathcxt = xmlXPathNewContext(doc);
4264  if (xpathcxt == NULL || xtCxt->xmlerrcxt->err_occurred)
4265  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
4266  "could not allocate XPath context");
4267  xpathcxt->node = xmlDocGetRootElement(doc);
4268  if (xpathcxt->node == NULL || xtCxt->xmlerrcxt->err_occurred)
4269  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
4270  "could not find root XML element");
4271  }
4272  PG_CATCH();
4273  {
4274  if (xpathcxt != NULL)
4275  xmlXPathFreeContext(xpathcxt);
4276  if (doc != NULL)
4277  xmlFreeDoc(doc);
4278 
4279  PG_RE_THROW();
4280  }
4281  PG_END_TRY();
4282 
4283  xtCxt->doc = doc;
4284  xtCxt->xpathcxt = xpathcxt;
4285 #else
4286  NO_XML_SUPPORT();
4287 #endif /* not USE_LIBXML */
4288 }
int length(const List *list)
Definition: list.c:1271
#define NO_XML_SUPPORT()
Definition: xml.c:217
#define DatumGetXmlP(X)
Definition: xml.h:50
#define ERROR
Definition: elog.h:43
static struct @121 value
static char * xml_out_internal(xmltype *x, pg_enc target_encoding)
Definition: xml.c:291
#define PG_CATCH()
Definition: elog.h:293
#define PG_RE_THROW()
Definition: elog.h:314
Definition: c.h:433
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
static void XmlTableSetNamespace ( struct TableFuncScanState state,
char *  name,
char *  uri 
)
static

Definition at line 4295 of file xml.c.

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

4296 {
4297 #ifdef USE_LIBXML
4298  XmlTableBuilderData *xtCxt;
4299 
4300  if (name == NULL)
4301  ereport(ERROR,
4302  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4303  errmsg("DEFAULT namespace is not supported")));
4304  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetNamespace");
4305 
4306  if (xmlXPathRegisterNs(xtCxt->xpathcxt,
4307  pg_xmlCharStrndup(name, strlen(name)),
4308  pg_xmlCharStrndup(uri, strlen(uri))))
4309  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_DATA_EXCEPTION,
4310  "could not set XML namespace");
4311 #else
4312  NO_XML_SUPPORT();
4313 #endif /* not USE_LIBXML */
4314 }
#define NO_XML_SUPPORT()
Definition: xml.c:217
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:797
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
static void XmlTableSetRowFilter ( struct TableFuncScanState state,
char *  path 
)
static

Definition at line 4321 of file xml.c.

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

4322 {
4323 #ifdef USE_LIBXML
4324  XmlTableBuilderData *xtCxt;
4325  xmlChar *xstr;
4326 
4327  xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetRowFilter");
4328 
4329  if (*path == '\0')
4330  ereport(ERROR,
4331  (errcode(ERRCODE_DATA_EXCEPTION),
4332  errmsg("row path filter must not be empty string")));
4333 
4334  xstr = pg_xmlCharStrndup(path, strlen(path));
4335 
4336  xtCxt->xpathcomp = xmlXPathCompile(xstr);
4337  if (xtCxt->xpathcomp == NULL || xtCxt->xmlerrcxt->err_occurred)
4338  xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_SYNTAX_ERROR,
4339  "invalid XPath expression");
4340 #else
4341  NO_XML_SUPPORT();
4342 #endif /* not USE_LIBXML */
4343 }
#define NO_XML_SUPPORT()
Definition: xml.c:217
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
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
Datum xmltotext ( PG_FUNCTION_ARGS  )

Definition at line 603 of file xml.c.

References PG_GETARG_XML_P, and PG_RETURN_TEXT_P.

604 {
605  xmltype *data = PG_GETARG_XML_P(0);
606 
607  /* It's actually binary compatible. */
608  PG_RETURN_TEXT_P((text *) data);
609 }
#define PG_GETARG_XML_P(n)
Definition: xml.h:53
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
Definition: c.h:433
text* xmltotext_with_xmloption ( xmltype data,
XmlOptionType  xmloption_arg 
)

Definition at line 613 of file xml.c.

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

Referenced by ExecEvalXmlExpr().

614 {
615  if (xmloption_arg == XMLOPTION_DOCUMENT && !xml_is_document(data))
616  ereport(ERROR,
617  (errcode(ERRCODE_NOT_AN_XML_DOCUMENT),
618  errmsg("not an XML document")));
619 
620  /* It's actually binary compatible, save for the above check. */
621  return (text *) data;
622 }
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:886
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:433
Datum xmlvalidate ( PG_FUNCTION_ARGS  )

Definition at line 876 of file xml.c.

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

877 {
878  ereport(ERROR,
879  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
880  errmsg("xmlvalidate is not implemented")));
881  return 0;
882 }
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 4016 of file xml.c.

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

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

4017 {
4018 #ifdef USE_LIBXML
4019  text *xpath_expr_text = PG_GETARG_TEXT_PP(0);
4020  xmltype *data = PG_GETARG_XML_P(1);
4021  ArrayType *namespaces = PG_GETARG_ARRAYTYPE_P(2);
4022  ArrayBuildState *astate;
4023 
4024  astate = initArrayResult(XMLOID, CurrentMemoryContext, true);
4025  xpath_internal(xpath_expr_text, data, namespaces,
4026  NULL, astate);
4028 #else
4029  NO_XML_SUPPORT();
4030  return 0;
4031 #endif
4032 }
#define NO_XML_SUPPORT()
Definition: xml.c:217
ArrayBuildState * initArrayResult(Oid element_type, MemoryContext rcontext, bool subcontext)
Definition: arrayfuncs.c:5003
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
#define XMLOID
Definition: pg_type.h:359
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:250
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5106
#define PG_GETARG_XML_P(n)
Definition: xml.h:53
Definition: c.h:433
Datum xpath_exists ( PG_FUNCTION_ARGS  )

Definition at line 4062 of file xml.c.

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

4063 {
4064 #ifdef USE_LIBXML
4065  text *xpath_expr_text = PG_GETARG_TEXT_PP(0);
4066  xmltype *data = PG_GETARG_XML_P(1);
4067  ArrayType *namespaces = PG_GETARG_ARRAYTYPE_P(2);
4068  int res_nitems;
4069 
4070  xpath_internal(xpath_expr_text, data, namespaces,
4071  &res_nitems, NULL);
4072 
4073  PG_RETURN_BOOL(res_nitems > 0);
4074 #else
4075  NO_XML_SUPPORT();
4076  return 0;
4077 #endif
4078 }
#define NO_XML_SUPPORT()
Definition: xml.c:217
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
#define PG_GETARG_XML_P(n)
Definition: xml.h:53
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Definition: c.h:433
static void xsd_schema_element_end ( StringInfo  result)
static

Definition at line 2790 of file xml.c.

References appendStringInfoString().

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

2791 {
2792  appendStringInfoString(result, "</xsd:schema>");
2793 }
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
static void xsd_schema_element_start ( StringInfo  result,
const char *  targetns 
)
static

Definition at line 2773 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().

2774 {
2775  appendStringInfoString(result,
2776  "<xsd:schema\n"
2777  " xmlns:xsd=\"" NAMESPACE_XSD "\"");
2778  if (strlen(targetns) > 0)
2779  appendStringInfo(result,
2780  "\n"
2781  " targetNamespace=\"%s\"\n"
2782  " elementFormDefault=\"qualified\"",
2783  targetns);
2784  appendStringInfoString(result,
2785  ">\n\n");
2786 }
#define NAMESPACE_XSD
Definition: xml.c:226
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157

Variable Documentation

int xmlbinary

Definition at line 96 of file xml.c.

Referenced by map_sql_type_to_xmlschema_type(), and map_sql_value_to_xml_value().

const TableFuncRoutine XmlTableRoutine
Initial value:
=
{
}
static void XmlTableInitOpaque(struct TableFuncScanState *state, int natts)
Definition: xml.c:4187
static bool XmlTableFetchRow(struct TableFuncScanState *state)
Definition: xml.c:4382
static void XmlTableSetDocument(struct TableFuncScanState *state, Datum value)
Definition: xml.c:4235
static Datum XmlTableGetValue(struct TableFuncScanState *state, int colnum, Oid typid, int32 typmod, bool *isnull)
Definition: xml.c:4433
static void XmlTableSetRowFilter(struct TableFuncScanState *state, char *path)
Definition: xml.c:4321
static void XmlTableDestroyOpaque(struct TableFuncScanState *state)
Definition: xml.c:4599
static void XmlTableSetNamespace(struct TableFuncScanState *state, char *name, char *uri)
Definition: xml.c:4295
static void XmlTableSetColumnFilter(struct TableFuncScanState *state, char *path, int colnum)
Definition: xml.c:4350

Definition at line 205 of file xml.c.

Referenced by ExecInitTableFuncScan().