Skip to content

Commit 6f863b4

Browse files
Iam-WenYijiaoshuntian
authored andcommitted
OSPP 2024: Add xml functions (IvorySQL#687)
* Mapping issue IvorySQL#695 Add xml functions * Add xml functions * Update ora_xml_functions.c * Delete contrib/ivorysql_ora/ivorysql_ora--1.0.sql * Add test * Update ora_gram.y * update test * update test * update * update * Update for OSPP 2024 * Update ora_xml_functions.out * Update ora_xml_functions.c * Update code --------- Co-authored-by: Wen Yi <wen-yi@qq.com>
1 parent 73e14be commit 6f863b4

7 files changed

Lines changed: 109 additions & 0 deletions

File tree

contrib/ivorysql_ora/expected/ora_xml_functions.out

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,21 @@ SELECT insertchildxmlafter(data, 'soapenv:Envelope/soapenv:Body/web:BBB','typ:FF
793793
</soapenv:Envelope>
794794
(1 row)
795795

796+
--
797+
-- xmlisvalid
798+
--
799+
SELECT xmlisvalid(XMLType('<a/>'));
800+
xmlisvalid
801+
------------
802+
1
803+
(1 row)
804+
805+
SELECT xmlisvalid(XMLType('<a>'));
806+
xmlisvalid
807+
------------
808+
0
809+
(1 row)
810+
796811
drop table inaf;
797812
drop table xmltest;
798813
drop table xmlnstest;

contrib/ivorysql_ora/sql/ora_xml_functions.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,14 @@ SELECT insertchildxmlafter(XMLType('<a><b>100</b><b>200</b></a>'), '/a/b[1]', 'b
254254

255255
SELECT insertchildxmlafter(data, 'soapenv:Envelope/soapenv:Body/web:BBB','typ:FFF', XMLType('<typ:IVY xmlns:typ="http://www.def.com">Ivory</typ:IVY>'), 'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:typ="http://www.def.com" xmlns:web="http://www.abc.com"') from xmlnstest where id = 1;
256256

257+
--
258+
-- xmlisvalid
259+
--
260+
261+
SELECT xmlisvalid(XMLType('<a/>'));
262+
263+
SELECT xmlisvalid(XMLType('<a>'));
264+
257265
drop table inaf;
258266
drop table xmltest;
259267
drop table xmlnstest;

contrib/ivorysql_ora/src/xml_functions/ora_xml_functions.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ PG_FUNCTION_INFO_V1(ivy_insertchildxmlbefore);
363363
PG_FUNCTION_INFO_V1(ivy_insertchildxmlbefore2);
364364
PG_FUNCTION_INFO_V1(ivy_insertchildxmlafter);
365365
PG_FUNCTION_INFO_V1(ivy_insertchildxmlafter2);
366+
PG_FUNCTION_INFO_V1(ivy_xmlisvalid);
366367
/*
367368
* ----------------------------------------------------------------------------
368369
* Helper Function definition
@@ -2372,3 +2373,37 @@ updatexml(List *args)
23722373
return NULL;
23732374
#endif
23742375
}
2376+
2377+
Datum ivy_xmlisvalid(PG_FUNCTION_ARGS)
2378+
{
2379+
#ifdef USE_LIBXML
2380+
if (fcinfo->args[0].isnull)
2381+
{
2382+
PG_RETURN_UINT32(1);
2383+
}
2384+
else
2385+
{
2386+
text *data = PG_GETARG_TEXT_PP(0);
2387+
char *datastr = VARDATA(data);
2388+
int32 len = VARSIZE(data) - VARHDRSZ;
2389+
xmlDocPtr doc = NULL;
2390+
if (len <= 0) /* Avoid crash */
2391+
{
2392+
PG_RETURN_BOOL(1);
2393+
}
2394+
doc = xmlReadMemory((const char *)datastr, len, NULL, NULL, XML_PARSE_NOBLANKS);
2395+
if (doc)
2396+
{
2397+
xmlFreeDoc(doc);
2398+
PG_RETURN_UINT32(1);
2399+
}
2400+
else
2401+
{
2402+
PG_RETURN_UINT32(0);
2403+
}
2404+
}
2405+
#else
2406+
NO_XML_SUPPORT();
2407+
return NULL;
2408+
#endif
2409+
}

contrib/ivorysql_ora/src/xml_functions/xml_functions--1.0.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,11 @@ CREATE FUNCTION sys.INSERTCHILDXMLAFTER(sys.xmltype, text, text, sys.xmltype, te
165165
RETURNS sys.xmltype
166166
AS 'MODULE_PATHNAME','ivy_insertchildxmlafter2'
167167
LANGUAGE C IMMUTABLE;
168+
169+
--
170+
-- XMLISVALID
171+
--
172+
CREATE OR REPLACE FUNCTION sys.XMLISVALID(sys.xmltype)
173+
RETURNS INTEGER
174+
AS 'MODULE_PATHNAME','ivy_xmlisvalid'
175+
LANGUAGE C IMMUTABLE;

src/backend/oracle_parser/ora_gram.y

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17868,6 +17868,22 @@ func_expr_common_subexpr:
1786817868
{
1786917869
$$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, $8, @1);
1787017870
}
17871+
| XMLELEMENT '(' ColLabel ')'
17872+
{
17873+
$$ = makeXmlExpr(IS_XMLELEMENT, $3, NIL, NIL, @1);
17874+
}
17875+
| XMLELEMENT '(' ColLabel ',' xml_attributes ')'
17876+
{
17877+
$$ = makeXmlExpr(IS_XMLELEMENT, $3, $5, NIL, @1);
17878+
}
17879+
| XMLELEMENT '(' ColLabel ',' expr_list ')'
17880+
{
17881+
$$ = makeXmlExpr(IS_XMLELEMENT, $3, NIL, $5, @1);
17882+
}
17883+
| XMLELEMENT '(' ColLabel ',' xml_attributes ',' expr_list ')'
17884+
{
17885+
$$ = makeXmlExpr(IS_XMLELEMENT, $3, $5, $7, @1);
17886+
}
1787117887
| XMLEXISTS '(' c_expr xmlexists_argument ')'
1787217888
{
1787317889
/* xmlexists(A PASSING [BY REF] B [BY REF]) is

src/oracle_test/regress/expected/xml.out

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,29 @@ SELECT xmlelement(name foo, xmlattributes('<>&"''' as funny, xml 'b<a/>r' as fun
224224
<foo funny="&lt;&gt;&amp;&quot;'" funnier="b&lt;a/&gt;r"/>
225225
(1 row)
226226

227+
SELECT xmlelement(foo, xmlattributes(true as bar));
228+
xmlelement
229+
-------------------
230+
<foo bar="true"/>
231+
(1 row)
232+
233+
SELECT xmlelement(foo, xmlattributes('2009-04-09 00:24:37'::timestamp as bar));
234+
xmlelement
235+
-----------------------------------------
236+
<foo bar="2009-04-09 00:24:37.000000"/>
237+
(1 row)
238+
239+
SELECT xmlelement(foo, xmlattributes('infinity'::timestamp as bar));
240+
ERROR: invalid value "infi" for "YYYY"
241+
LINE 1: SELECT xmlelement(foo, xmlattributes('infinity'::timestamp a...
242+
^
243+
DETAIL: Value must be an integer.
244+
SELECT xmlelement(foo, xmlattributes('<>&"''' as funny, xml 'b<a/>r' as funnier));
245+
xmlelement
246+
------------------------------------------------------------
247+
<foo funny="&lt;&gt;&amp;&quot;'" funnier="b&lt;a/&gt;r"/>
248+
(1 row)
249+
227250
SELECT xmlparse(content '');
228251
xmlparse
229252
----------

src/oracle_test/regress/sql/xml.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ SELECT xmlelement(name foo, xmlattributes('2009-04-09 00:24:37'::timestamp as ba
6767
SELECT xmlelement(name foo, xmlattributes('infinity'::timestamp as bar));
6868
SELECT xmlelement(name foo, xmlattributes('<>&"''' as funny, xml 'b<a/>r' as funnier));
6969

70+
SELECT xmlelement(foo, xmlattributes(true as bar));
71+
SELECT xmlelement(foo, xmlattributes('2009-04-09 00:24:37'::timestamp as bar));
72+
SELECT xmlelement(foo, xmlattributes('infinity'::timestamp as bar));
73+
SELECT xmlelement(foo, xmlattributes('<>&"''' as funny, xml 'b<a/>r' as funnier));
7074

7175
SELECT xmlparse(content '');
7276
SELECT xmlparse(content ' ');

0 commit comments

Comments
 (0)