LCOV - code coverage report
Current view: top level - monetdb5/modules/atoms - batxml.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 319 899 35.5 %
Date: 2021-10-13 02:24:04 Functions: 14 22 63.6 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : /*
      10             :  *  M.L. Kersten
      11             :  *  XML multiplexes
      12             :  * SQL/XML requires a handful of instructions.
      13             :  * The collection of routines provided here are map operations
      14             :  * for the atom xml primitives.
      15             :  *
      16             :  * In line with the batcalc module, we assume that if two bat operands
      17             :  * are provided that they are aligned.
      18             :  *
      19             :  * The implementation is focussed on functionality. At a later stage
      20             :  * we may postpone string contstruction until it is really needed.
      21             :  */
      22             : 
      23             : 
      24             : 
      25             : #include "monetdb_config.h"
      26             : #include "gdk.h"
      27             : #include <ctype.h>
      28             : #include <string.h>
      29             : #ifdef HAVE_LIBXML
      30             : #include <libxml/parser.h>
      31             : #endif
      32             : #include "mal_interpreter.h"
      33             : #include "mal_function.h"
      34             : #include "xml.h"
      35             : 
      36             : #include "mel.h"
      37             : 
      38             : #ifdef HAVE_LIBXML
      39             : 
      40             : #define prepareResult(X,Y,tpe,Z,free)                                                           \
      41             :         do {                                                                                                                    \
      42             :                 (X) = COLnew((Y)->hseqbase, (tpe), BATcount(Y), TRANSIENT);  \
      43             :                 if ((X) == NULL) {                                                                                      \
      44             :                         BBPunfix((Y)->batCacheid);                                                           \
      45             :                         free;                                                                                                   \
      46             :                         throw(MAL, "xml." Z, SQLSTATE(HY013) MAL_MALLOC_FAIL);        \
      47             :                 }                                                                                                                       \
      48             :                 (X)->tsorted =  false;                                                                               \
      49             :                 (X)->trevsorted =  false;                                                                    \
      50             :                 (X)->tnonil = true;                                                                                  \
      51             :         } while (0)
      52             : 
      53             : #define finalizeResult(X,Y,Z)                                   \
      54             :         do {                                                                            \
      55             :                 BATsetcount((Y), (Y)->batCount);             \
      56             :                 *(X) = (Y)->batCacheid;                                      \
      57             :                 BBPkeepref(*(X));                                               \
      58             :                 BBPunfix((Z)->batCacheid);                           \
      59             :         } while (0)
      60             : 
      61             : static str
      62          12 : BATXMLxml2str(bat *ret, const bat *bid)
      63             : {
      64             :         BAT *b, *bn;
      65             :         BUN p, q;
      66             :         BATiter bi;
      67             : 
      68          12 :         if ((b = BATdescriptor(*bid)) == NULL)
      69           0 :                 throw(MAL, "xml.str", INTERNAL_BAT_ACCESS);
      70          12 :         prepareResult(bn, b, TYPE_str, "str", (void) 0);
      71          12 :         bi = bat_iterator(b);
      72          36 :         BATloop(b, p, q) {
      73          24 :                 const char *t = (const char *) BUNtvar(bi, p);
      74             : 
      75          24 :                 if (strNil(t)) {
      76           0 :                         if (bunfastapp_nocheckVAR(bn, t) != GDK_SUCCEED)
      77           0 :                                 goto bunins_failed;
      78           0 :                         bn->tnonil = false;
      79             :                 } else {
      80          24 :                         assert(*t == 'A' || *t == 'C' || *t == 'D');
      81          24 :                         if (bunfastapp_nocheckVAR(bn, t + 1) != GDK_SUCCEED)
      82           0 :                                 goto bunins_failed;
      83             :                 }
      84             :         }
      85          12 :         bat_iterator_end(&bi);
      86          12 :         finalizeResult(ret, bn, b);
      87          12 :         return MAL_SUCCEED;
      88           0 :   bunins_failed:
      89           0 :         bat_iterator_end(&bi);
      90           0 :         BBPunfix(b->batCacheid);
      91           0 :         BBPunfix(bn->batCacheid);
      92           0 :         throw(MAL, "xml.str", OPERATION_FAILED " during bulk coercion");
      93             : }
      94             : 
      95             : static str
      96           0 : BATXMLxmltext(bat *ret, const bat *bid)
      97             : {
      98             :         BAT *b, *bn;
      99             :         BUN p, q;
     100             :         BATiter bi;
     101             :         size_t size = 0;
     102             :         str buf = NULL;
     103             :         xmlDocPtr doc = NULL;
     104             :         xmlNodePtr elem;
     105             :         str content = NULL;
     106             :         const char *err = OPERATION_FAILED;
     107             : 
     108           0 :         if ((b = BATdescriptor(*bid)) == NULL)
     109           0 :                 throw(MAL, "xml.text", INTERNAL_BAT_ACCESS);
     110           0 :         prepareResult(bn, b, TYPE_str, "text", (void) 0);
     111           0 :         bi = bat_iterator(b);
     112           0 :         BATloop(b, p, q) {
     113           0 :                 const char *t = (const char *) BUNtvar(bi, p);
     114             :                 size_t len;
     115             : 
     116           0 :                 if (strNil(t)) {
     117           0 :                         if (bunfastapp_nocheckVAR(bn, t) != GDK_SUCCEED)
     118           0 :                                 goto bunins_failed;
     119           0 :                         bn->tnonil = false;
     120           0 :                         continue;
     121             :                 }
     122           0 :                 len = strlen(t);
     123           0 :                 switch (*t) {
     124           0 :                 case 'D': {
     125           0 :                         xmlDocPtr d = xmlParseMemory(t + 1, (int) (len - 1));
     126           0 :                         elem = xmlDocGetRootElement(d);
     127           0 :                         content = (str) xmlNodeGetContent(elem);
     128           0 :                         xmlFreeDoc(d);
     129           0 :                         if (content == NULL) {
     130             :                                 err = SQLSTATE(HY013) MAL_MALLOC_FAIL;
     131           0 :                                 goto bunins_failed;
     132             :                         }
     133             :                         break;
     134             :                 }
     135           0 :                 case 'C':
     136           0 :                         if (doc == NULL)
     137           0 :                                 doc = xmlParseMemory("<doc/>", 6);
     138           0 :                         xmlParseInNodeContext(xmlDocGetRootElement(doc), t + 1, (int) (len - 1), 0, &elem);
     139           0 :                         content = (str) xmlNodeGetContent(elem);
     140           0 :                         xmlFreeNodeList(elem);
     141           0 :                         if (content == NULL) {
     142             :                                 err = SQLSTATE(HY013) MAL_MALLOC_FAIL;
     143           0 :                                 goto bunins_failed;
     144             :                         }
     145             :                         break;
     146           0 :                 case 'A': {
     147             :                         str s;
     148             : 
     149           0 :                         if (buf == NULL || size < len) {
     150           0 :                                 size = len + 128;
     151           0 :                                 if (buf != NULL)
     152           0 :                                         GDKfree(buf);
     153           0 :                                 buf = GDKmalloc(size);
     154           0 :                                 if (buf == NULL) {
     155             :                                         err = SQLSTATE(HY013) MAL_MALLOC_FAIL;
     156           0 :                                         goto bunins_failed;
     157             :                                 }
     158             :                         }
     159             :                         s = buf;
     160           0 :                         t++;
     161           0 :                         while (*t) {
     162           0 :                                 if (*t == '"' || *t == '\'') {
     163           0 :                                         char q = *t++;
     164             : 
     165           0 :                                         s += XMLunquotestring(&t, q, s);
     166             :                                 }
     167           0 :                                 t++;
     168             :                         }
     169           0 :                         *s = 0;
     170           0 :                         break;
     171             :                 }
     172           0 :                 default:
     173           0 :                         assert(*t == 'A' || *t == 'C' || *t == 'D');
     174           0 :                         if (bunfastapp_nocheckVAR(bn, str_nil) != GDK_SUCCEED)
     175           0 :                                 goto bunins_failed;
     176           0 :                         bn->tnonil = false;
     177           0 :                         continue;
     178             :                 }
     179           0 :                 assert(content != NULL || buf != NULL);
     180           0 :                 if (bunfastapp_nocheckVAR(bn, content != NULL ? content : buf) != GDK_SUCCEED)
     181           0 :                         goto bunins_failed;
     182           0 :                 if (content != NULL)
     183           0 :                         GDKfree(content);
     184             :                 content = NULL;
     185             :         }
     186           0 :         bat_iterator_end(&bi);
     187           0 :         finalizeResult(ret, bn, b);
     188           0 :         if (buf != NULL)
     189           0 :                 GDKfree(buf);
     190           0 :         if (doc != NULL)
     191           0 :                 xmlFreeDoc(doc);
     192             :         return MAL_SUCCEED;
     193             :   bunins_failed:
     194           0 :         bat_iterator_end(&bi);
     195           0 :         BBPunfix(b->batCacheid);
     196           0 :         BBPunfix(bn->batCacheid);
     197           0 :         if (buf != NULL)
     198           0 :                 GDKfree(buf);
     199           0 :         if (doc != NULL)
     200           0 :                 xmlFreeDoc(doc);
     201           0 :         if (content != NULL)
     202           0 :                 GDKfree(content);
     203           0 :         throw(MAL, "xml.text", "%s", err);
     204             : }
     205             : 
     206             : /*
     207             :  * The core of the activity is str2xml, where the actual strings
     208             :  * are constructed.
     209             :  * To avoid repetitive copying we make sure that the garbage
     210             :  * collector does not remove the xml intermediates.
     211             :  * This way, we know that as long as the xml-variables are not
     212             :  * reused, the complete structure of the xml document(s) are available.
     213             :  * We merely have to collect the pieces.
     214             :  * [FOR LATER, FIRST GO FOR THE EASY IMPLEMENTATION]
     215             :  * XML values are represented by strings already.
     216             :  */
     217             : static str
     218          49 : BATXMLstr2xml(bat *ret, const bat *bid)
     219             : {
     220             :         BAT *b, *bn;
     221             :         BUN p, q;
     222             :         size_t size = BUFSIZ;
     223             :         str buf;
     224             :         const char *err= OPERATION_FAILED;
     225             :         BATiter bi;
     226             : 
     227          49 :         buf = GDKmalloc(size);
     228          49 :         if (buf == NULL)
     229           0 :                 throw(MAL,"xml.str2xml", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     230          49 :         if ((b = BATdescriptor(*bid)) == NULL) {
     231           0 :                 GDKfree(buf);
     232           0 :                 throw(MAL, "xml.xml", INTERNAL_BAT_ACCESS);
     233             :         }
     234          49 :         prepareResult(bn, b, TYPE_xml, "xml", GDKfree(buf));
     235          49 :         bi = bat_iterator(b);
     236         141 :         BATloop(b, p, q) {
     237          92 :                 const char *t = (const char *) BUNtvar(bi, p);
     238             :                 size_t len;
     239             : 
     240          92 :                 if (strNil(t)) {
     241           0 :                         if (bunfastapp_nocheckVAR(bn, str_nil) != GDK_SUCCEED)
     242           0 :                                 goto bunins_failed;
     243           0 :                         bn->tnonil = false;
     244           0 :                         continue;
     245             :                 }
     246             : 
     247          92 :                 len = strlen(t) * 6 + 1;
     248          92 :                 if (size < len) {
     249           0 :                         size = len + 128;
     250           0 :                         GDKfree(buf);
     251           0 :                         buf = GDKmalloc(size);
     252           0 :                         if (buf == NULL) {
     253             :                                 err = SQLSTATE(HY013) MAL_MALLOC_FAIL;
     254           0 :                                 goto bunins_failed;
     255             :                         }
     256             :                 }
     257          92 :                 buf[0] = 'C';
     258          92 :                 XMLquotestring(t, buf + 1, size - 1);
     259          92 :                 if (bunfastapp_nocheckVAR(bn, buf) != GDK_SUCCEED)
     260           0 :                         goto bunins_failed;
     261             :         }
     262          49 :         bat_iterator_end(&bi);
     263          49 :         GDKfree(buf);
     264          49 :         finalizeResult(ret, bn, b);
     265          49 :         return MAL_SUCCEED;
     266           0 :   bunins_failed:
     267           0 :         bat_iterator_end(&bi);
     268           0 :         BBPunfix(b->batCacheid);
     269           0 :         BBPunfix(bn->batCacheid);
     270           0 :         if (buf != NULL)
     271           0 :                 GDKfree(buf);
     272           0 :         throw(MAL, "xml.xml", "%s", err);
     273             : }
     274             : 
     275             : static str
     276           0 : BATXMLdocument(bat *ret, const bat *bid)
     277             : {
     278             :         BAT *b, *bn;
     279             :         BUN p, q;
     280             :         BATiter bi;
     281             :         size_t size = BUFSIZ;
     282           0 :         str buf = GDKmalloc(size);
     283             :         const char *err = OPERATION_FAILED;
     284             : 
     285           0 :         if (buf == NULL)
     286           0 :                 throw(MAL,"xml.document", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     287           0 :         if ((b = BATdescriptor(*bid)) == NULL) {
     288           0 :                 GDKfree(buf);
     289           0 :                 throw(MAL, "xml.document", INTERNAL_BAT_ACCESS);
     290             :         }
     291           0 :         prepareResult(bn, b, TYPE_xml, "document", GDKfree(buf));
     292           0 :         bi = bat_iterator(b);
     293           0 :         BATloop(b, p, q) {
     294           0 :                 const char *t = (const char *) BUNtvar(bi, p);
     295             :                 xmlDocPtr doc;
     296             :                 int len;
     297             :                 xmlChar *s;
     298             : 
     299           0 :                 if (strNil(t)) {
     300           0 :                         if (bunfastapp_nocheckVAR(bn, str_nil) != GDK_SUCCEED)
     301           0 :                                 goto bunins_failed;
     302           0 :                         bn->tnonil = false;
     303           0 :                         continue;
     304             :                 }
     305           0 :                 len = (int) strlen(t);
     306           0 :                 doc = xmlParseMemory(t, len);
     307           0 :                 if (doc == NULL) {
     308             :                         err = OPERATION_FAILED XML_PARSE_ERROR;
     309           0 :                         goto bunins_failed;
     310             :                 }
     311           0 :                 xmlDocDumpMemory(doc, &s, &len);
     312           0 :                 xmlFreeDoc(doc);
     313           0 :                 if ((size_t) len + 2 >= size) {
     314           0 :                         GDKfree(buf);
     315           0 :                         size = (size_t) len + 128;
     316           0 :                         buf = GDKmalloc(size);
     317           0 :                         if (buf == NULL) {
     318             :                                 err= MAL_MALLOC_FAIL;
     319           0 :                                 goto bunins_failed;
     320             :                         }
     321             :                 }
     322           0 :                 buf[0] = 'D';
     323           0 :                 strcpy(buf + 1, (char *) s);
     324           0 :                 if (bunfastapp_nocheckVAR(bn, buf) != GDK_SUCCEED)
     325           0 :                         goto bunins_failed;
     326             :         }
     327           0 :         bat_iterator_end(&bi);
     328           0 :         GDKfree(buf);
     329           0 :         finalizeResult(ret, bn, b);
     330           0 :         return MAL_SUCCEED;
     331             :   bunins_failed:
     332           0 :         bat_iterator_end(&bi);
     333           0 :         GDKfree(buf);
     334           0 :         BBPunfix(b->batCacheid);
     335           0 :         BBPunfix(bn->batCacheid);
     336           0 :         throw(MAL, "xml.document", "%s", err);
     337             : }
     338             : 
     339             : static str
     340           0 : BATXMLcontent(bat *ret, const bat *bid)
     341             : {
     342             :         BAT *b, *bn;
     343             :         BUN p, q;
     344             :         BATiter bi;
     345             :         xmlDocPtr doc;
     346             :         xmlNodePtr root;
     347             :         size_t size = BUFSIZ;
     348           0 :         str buf = GDKmalloc(size);
     349             :         const char *err = OPERATION_FAILED;
     350             :         xmlBufferPtr xbuf;
     351             : 
     352           0 :         if (buf == NULL)
     353           0 :                 throw(MAL,"xml.content", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     354           0 :         if ((b = BATdescriptor(*bid)) == NULL) {
     355           0 :                 GDKfree(buf);
     356           0 :                 throw(MAL, "xml.content", INTERNAL_BAT_ACCESS);
     357             :         }
     358           0 :         doc = xmlParseMemory("<doc/>", 6);
     359           0 :         root = xmlDocGetRootElement(doc);
     360           0 :         prepareResult(bn, b, TYPE_xml, "content", GDKfree(buf));
     361           0 :         bi = bat_iterator(b);
     362           0 :         xbuf = xmlBufferCreate();
     363           0 :         BATloop(b, p, q) {
     364           0 :                 const char *t = (const char *) BUNtvar(bi, p);
     365             :                 size_t len;
     366             :                 xmlNodePtr elem;
     367             :                 xmlParserErrors xerr;
     368             :                 const xmlChar *s;
     369             : 
     370           0 :                 if (strNil(t)) {
     371           0 :                         if (bunfastapp_nocheckVAR(bn, str_nil) != GDK_SUCCEED)
     372           0 :                                 goto bunins_failed;
     373           0 :                         bn->tnonil = false;
     374           0 :                         continue;
     375             :                 }
     376           0 :                 len = strlen(t);
     377           0 :                 xerr = xmlParseInNodeContext(root, t, (int) len, 0, &elem);
     378           0 :                 if (xerr != XML_ERR_OK) {
     379             :                         err = XML_PARSE_ERROR;
     380           0 :                         goto bunins_failed;
     381             :                 }
     382           0 :                 xmlNodeDump(xbuf, doc, elem, 0, 0);
     383           0 :                 s = xmlBufferContent(xbuf);
     384           0 :                 len = strlen((const char *) s);
     385           0 :                 if (len + 2 >= size) {
     386           0 :                         GDKfree(buf);
     387           0 :                         size = len + 128;
     388           0 :                         buf = GDKmalloc(size);
     389           0 :                         if (buf == NULL) {
     390             :                                 err = SQLSTATE(HY013) MAL_MALLOC_FAIL;
     391           0 :                                 goto bunins_failed;
     392             :                         }
     393             :                 }
     394           0 :                 buf[0] = 'C';
     395           0 :                 strcpy(buf + 1, (const char *) s);
     396           0 :                 if (bunfastapp_nocheckVAR(bn, buf) != GDK_SUCCEED)
     397           0 :                         goto bunins_failed;
     398           0 :                 xmlBufferEmpty(xbuf);
     399           0 :                 xmlFreeNodeList(elem);
     400             :         }
     401           0 :         bat_iterator_end(&bi);
     402           0 :         xmlBufferFree(xbuf);
     403           0 :         xmlFreeDoc(doc);
     404           0 :         GDKfree(buf);
     405           0 :         finalizeResult(ret, bn, b);
     406           0 :         return MAL_SUCCEED;
     407             :   bunins_failed:
     408           0 :         bat_iterator_end(&bi);
     409           0 :         xmlBufferFree(xbuf);
     410           0 :         xmlFreeDoc(doc);
     411           0 :         if (buf != NULL)
     412           0 :                 GDKfree(buf);
     413           0 :         BBPunfix(b->batCacheid);
     414           0 :         BBPunfix(bn->batCacheid);
     415           0 :         throw(MAL, "xml.document", "%s", err);
     416             : }
     417             : 
     418             : static str
     419           0 : BATXMLisdocument(bat *ret, const bat *bid)
     420             : {
     421             :         BAT *b, *bn;
     422             :         BUN p, q;
     423             :         BATiter bi;
     424             : 
     425           0 :         if ((b = BATdescriptor(*bid)) == NULL)
     426           0 :                 throw(MAL, "xml.isdocument", INTERNAL_BAT_ACCESS);
     427           0 :         prepareResult(bn, b, TYPE_bit, "isdocument", (void) 0);
     428           0 :         bi = bat_iterator(b);
     429           0 :         BATloop(b, p, q) {
     430           0 :                 const char *t = (const char *) BUNtvar(bi, p);
     431             :                 xmlDocPtr doc;
     432             :                 bit val;
     433             : 
     434           0 :                 if (strNil(t)) {
     435           0 :                         val = bit_nil;
     436           0 :                         bn->tnonil = false;
     437             :                 } else {
     438           0 :                         doc = xmlParseMemory(t, (int) strlen(t));
     439           0 :                         if (doc == NULL) {
     440             :                                 val = 0;
     441             :                         } else {
     442           0 :                                 xmlFreeDoc(doc);
     443             :                                 val = 1;
     444             :                         }
     445             :                 }
     446           0 :                 if (bunfastappTYPE(bit, bn, &val) != GDK_SUCCEED) {
     447           0 :                         bat_iterator_end(&bi);
     448           0 :                         BBPunfix(b->batCacheid);
     449           0 :                         BBPunfix(bn->batCacheid);
     450           0 :                         throw(MAL, "xml.isdocument",
     451             :                                   OPERATION_FAILED " During bulk processing");
     452             :                 }
     453             :         }
     454           0 :         bat_iterator_end(&bi);
     455           0 :         finalizeResult(ret, bn, b);
     456           0 :         return MAL_SUCCEED;
     457             : }
     458             : 
     459             : /*
     460             :  * The standard supports specific mappings for
     461             :  * NULL values,i.e. {null,absent,empty,nil,niloncontent)
     462             :  * in the context of an element and forest construction.
     463             :  * The standard should be studied in more detail, because
     464             :  * the syntax(rules) seem ambiguous.
     465             :  * It applies to all components of an element or their
     466             :  * concatenation.
     467             :  *
     468             :  * For the time being, the variaton on XMLtag seems the
     469             :  * most reasonable interpretation.
     470             :  */
     471             : static str
     472           0 : BATXMLoptions(bat *ret, const char * const *name, const char * const *options, const bat *bid)
     473             : {
     474             :         BAT *b, *bn;
     475             :         BUN p, q;
     476           0 :         str buf = GDKmalloc(BUFSIZ);
     477           0 :         str val = GDKmalloc(BUFSIZ);
     478           0 :         size_t size = BUFSIZ, len = strlen(*name);
     479           0 :         BATiter bi =  (BATiter) { .b = NULL };
     480             :         const char *err = OPERATION_FAILED " During bulk options analysis";
     481             : 
     482           0 :         if (val == NULL || buf == NULL) {
     483           0 :                 if (val != NULL)
     484           0 :                         GDKfree(val);
     485           0 :                 if (buf != NULL)
     486           0 :                         GDKfree(buf);
     487           0 :                 throw(MAL, "batxml.options", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     488             :         }
     489           0 :         if ((b = BATdescriptor(*bid)) == NULL) {
     490           0 :                 GDKfree(val);
     491           0 :                 GDKfree(buf);
     492           0 :                 throw(MAL, "xml.options", INTERNAL_BAT_ACCESS);
     493             :         }
     494           0 :         prepareResult(bn, b, TYPE_xml, "options", GDKfree(val); GDKfree(buf));
     495             : 
     496           0 :         if (strcmp(*options, "absent") == 0)
     497           0 :                 buf[0] = 0;
     498           0 :         else if (strcmp(*options, "empty") == 0)
     499           0 :                 snprintf(buf, size, "<%s></%s>", *name, *name);
     500           0 :         else if (strcmp(*options, "null") == 0)
     501           0 :                 snprintf(buf, size, "null");
     502           0 :         else if (strcmp(*options, "nil") == 0)
     503           0 :                 snprintf(buf, size, "nil");
     504             :         else {
     505             :                 /*if(strcmp(*options,"niloncontent")==0) */
     506             :                 err = SQLSTATE(0A000) PROGRAM_NYI;
     507           0 :                 goto bunins_failed;
     508             :         }
     509             : 
     510           0 :         snprintf(val, size, "<%s>", *name);
     511           0 :         bi = bat_iterator(b);
     512           0 :         BATloop(b, p, q) {
     513           0 :                 const char *t = (const char *) BUNtvar(bi, p);
     514             : 
     515           0 :                 if (strNil(t)) {
     516           0 :                         if (bunfastapp_nocheckVAR(bn, buf) != GDK_SUCCEED)
     517           0 :                                 goto bunins_failed1;
     518             :                 } else {
     519           0 :                         if (strlen(t) > size - 2 * len - 6) {
     520             :                                 char *tmp;
     521           0 :                                 size += strlen(t);
     522           0 :                                 tmp = (char *) GDKrealloc(val, size + strlen(t));
     523           0 :                                 if (tmp == NULL) {
     524             :                                         err = SQLSTATE(HY013) MAL_MALLOC_FAIL;
     525           0 :                                         goto bunins_failed1;
     526             :                                 }
     527             :                                 val = tmp;
     528             :                         }
     529           0 :                         snprintf(val + len + 2, size - len, "%s</%s>", t, *name);
     530           0 :                         if (bunfastapp_nocheckVAR(bn, val) != GDK_SUCCEED)
     531           0 :                                 goto bunins_failed1;
     532             :                 }
     533             :         }
     534           0 :         bat_iterator_end(&bi);
     535           0 :         GDKfree(val);
     536           0 :         GDKfree(buf);
     537           0 :         finalizeResult(ret, bn, b);
     538           0 :         return MAL_SUCCEED;
     539           0 : bunins_failed1:
     540           0 :         bat_iterator_end(&bi);
     541           0 : bunins_failed:
     542           0 :         BBPunfix(b->batCacheid);
     543           0 :         BBPunfix(bn->batCacheid);
     544             :         if (buf != NULL)
     545           0 :                 GDKfree(buf);
     546           0 :         if (val != NULL)
     547           0 :                 GDKfree(val);
     548           0 :         throw(MAL, "batxml.options", "%s", err);
     549             : }
     550             : 
     551             : static str
     552           1 : BATXMLcomment(bat *ret, const bat *bid)
     553             : {
     554             :         BAT *b, *bn;
     555             :         BUN p, q;
     556             :         size_t size = BUFSIZ;
     557           1 :         str buf = GDKmalloc(size);
     558             :         BATiter bi;
     559             :         const char *err= OPERATION_FAILED;
     560             : 
     561           1 :         if (buf == NULL)
     562           0 :                 throw(MAL, "xml.comment", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     563           1 :         if ((b = BATdescriptor(*bid)) == NULL) {
     564           0 :                 GDKfree(buf);
     565           0 :                 throw(MAL, "xml.comment", INTERNAL_BAT_ACCESS);
     566             :         }
     567           1 :         prepareResult(bn, b, TYPE_xml, "comment", GDKfree(buf));
     568           1 :         bi = bat_iterator(b);
     569           3 :         BATloop(b, p, q) {
     570           2 :                 const char *t = (const char *) BUNtvar(bi, p);
     571             :                 size_t len;
     572             : 
     573           2 :                 if (strNil(t)) {
     574           0 :                         if (bunfastapp_nocheckVAR(bn, str_nil) != GDK_SUCCEED)
     575           0 :                                 goto bunins_failed;
     576           0 :                         bn->tnonil = false;
     577           0 :                         continue;
     578             :                 }
     579           2 :                 if (strstr(t, "--") != NULL) {
     580             :                         err = XML_COMMENT_ERROR;
     581           0 :                         goto bunins_failed;
     582             :                 }
     583           2 :                 len = strlen(t);
     584           2 :                 if (len + 9 >= size) {
     585             :                         /* make sure there is enough space */
     586           0 :                         size = len + 128;
     587             :                         /* free/malloc so we don't copy */
     588           0 :                         GDKfree(buf);
     589           0 :                         buf = GDKmalloc(size);
     590           0 :                         if (buf == NULL) {
     591             :                                 err = SQLSTATE(HY013) MAL_MALLOC_FAIL;
     592           0 :                                 goto bunins_failed;
     593             :                         }
     594             :                 }
     595           2 :                 snprintf(buf, size, "C<!--%s-->", t);
     596           2 :                 if (bunfastapp_nocheckVAR(bn, buf) != GDK_SUCCEED)
     597           0 :                         goto bunins_failed;
     598             :         }
     599           1 :         bat_iterator_end(&bi);
     600           1 :         GDKfree(buf);
     601           1 :         finalizeResult(ret, bn, b);
     602           1 :         return MAL_SUCCEED;
     603           0 :   bunins_failed:
     604           0 :         bat_iterator_end(&bi);
     605           0 :         BBPunfix(b->batCacheid);
     606           0 :         BBPunfix(bn->batCacheid);
     607           0 :         if (buf != NULL)
     608           0 :                 GDKfree(buf);
     609           0 :         throw(MAL, "xml.comment", "%s", err);
     610             : }
     611             : 
     612             : static str
     613           0 : BATXMLparse(bat *ret, const char * const *doccont, const bat *bid, const char * const *option)
     614             : {
     615             :         (void) option;
     616           0 :         if (strcmp(*doccont, "content") == 0)
     617           0 :                 return BATXMLcontent(ret, bid);
     618           0 :         if (strcmp(*doccont, "document") == 0)
     619           0 :                 return BATXMLdocument(ret, bid);
     620           0 :         throw(MAL, "xml.parse", ILLEGAL_ARGUMENT " <document> or <content> expected");
     621             : }
     622             : 
     623             : static str
     624           0 : BATXMLpi(bat *ret, const char * const *target, const bat *bid)
     625             : {
     626             :         BAT *b, *bn;
     627             :         BUN p, q;
     628             :         size_t size = BUFSIZ;
     629             :         str buf;
     630             :         BATiter bi;
     631             :         size_t tgtlen;
     632             :         const char *err = OPERATION_FAILED;
     633             : 
     634           0 :         if (strNil(*target))
     635           0 :                 throw(MAL, "xml.pi", XML_PI_ERROR);
     636           0 :         buf = GDKmalloc(size);
     637           0 :         if (buf == NULL)
     638           0 :                 throw(MAL, "xml.pi", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     639             : 
     640           0 :         tgtlen = strlen(*target) + 6;
     641           0 :         if ((b = BATdescriptor(*bid)) == NULL) {
     642           0 :                 GDKfree(buf);
     643           0 :                 throw(MAL, "xml.pi", INTERNAL_BAT_ACCESS);
     644             :         }
     645           0 :         prepareResult(bn, b, TYPE_xml, "pi", GDKfree(buf));
     646           0 :         bi = bat_iterator(b);
     647           0 :         BATloop(b, p, q) {
     648           0 :                 const char *t = (const char *) BUNtvar(bi, p);
     649             :                 size_t len;
     650             : 
     651             :                 len = tgtlen;
     652           0 :                 if (!strNil(t))
     653           0 :                         len += strlen(t) * 6 + 1;
     654           0 :                 if (len >= size) {
     655             :                         /* make sure there is enough space */
     656           0 :                         size = len + 128;
     657             :                         /* free/malloc so we don't copy */
     658           0 :                         GDKfree(buf);
     659           0 :                         buf = GDKmalloc(size);
     660           0 :                         if (buf == NULL) {
     661             :                                 err = SQLSTATE(HY013) MAL_MALLOC_FAIL;
     662           0 :                                 goto bunins_failed;
     663             :                         }
     664             :                 }
     665           0 :                 if (strNil(t))
     666           0 :                         snprintf(buf, size, "C<?%s?>", *target);
     667             :                 else {
     668           0 :                         int n = snprintf(buf, size, "C<?%s ", *target);
     669           0 :                         size_t m = XMLquotestring(t, buf + n, size - n);
     670           0 :                         strcpy(buf + n + m, "?>");
     671             :                 }
     672           0 :                 if (bunfastapp_nocheckVAR(bn, buf) != GDK_SUCCEED)
     673           0 :                         goto bunins_failed;
     674             :         }
     675           0 :         bat_iterator_end(&bi);
     676           0 :         GDKfree(buf);
     677           0 :         finalizeResult(ret, bn, b);
     678           0 :         return MAL_SUCCEED;
     679           0 :   bunins_failed:
     680           0 :         bat_iterator_end(&bi);
     681           0 :         BBPunfix(b->batCacheid);
     682           0 :         BBPunfix(bn->batCacheid);
     683           0 :         if (buf != NULL)
     684           0 :                 GDKfree(buf);
     685           0 :         throw(MAL, "xml.pi", "%s", err);
     686             : }
     687             : 
     688             : static str
     689           1 : BATXMLroot(bat *ret, const bat *bid, const char * const *version, const char * const *standalone)
     690             : {
     691             :         BAT *b, *bn;
     692             :         BUN p, q;
     693             :         size_t size = BUFSIZ;
     694             :         str buf;
     695             :         BATiter bi;
     696             :         size_t hdrlen;
     697             :         const char *err = OPERATION_FAILED;
     698             : 
     699             :         hdrlen = 8;
     700           2 :         if (!strNil(*version) && **version) {
     701           1 :                 if (strcmp(*version, "1.0") != 0 && strcmp(*version, "1.1") != 0)
     702           0 :                         throw(MAL, "xml.root", XML_VERSION_ERROR);
     703           1 :                 hdrlen += 11 + strlen(*version);  /* strlen(" version=\"\"") */
     704             :         }
     705           2 :         if (!strNil(*standalone) && **standalone) {
     706           1 :                 if (strcmp(*standalone, "yes") != 0 && strcmp(*standalone, "no") != 0)
     707           0 :                         throw(MAL, "xml.root", XML_STANDALONE_ERROR "illegal XML standalone value");
     708           1 :                 hdrlen += 14 + strlen(*standalone);  /* strlen(" standalone=\"\"") */
     709             :         }
     710           1 :         buf = GDKmalloc(size);
     711           1 :         if (buf == NULL)
     712           0 :                 throw(MAL, "xml.root", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     713           1 :         if ((b = BATdescriptor(*bid)) == NULL) {
     714           0 :                 GDKfree(buf);
     715           0 :                 throw(MAL, "xml.pi", INTERNAL_BAT_ACCESS);
     716             :         }
     717           1 :         prepareResult(bn, b, TYPE_xml, "pi", GDKfree(buf));
     718           1 :         bi = bat_iterator(b);
     719           3 :         BATloop(b, p, q) {
     720           2 :                 const char *t = (const char *) BUNtvar(bi, p);
     721             :                 size_t len, i;
     722             :                 bit isdoc;
     723             : 
     724             :                 len = hdrlen;
     725           2 :                 if (!strNil(t))
     726           2 :                         len += strlen(t);
     727           2 :                 if (len >= size) {
     728             :                         /* make sure there is enough space */
     729           0 :                         size = len + 128;
     730             :                         /* free/malloc so we don't copy */
     731           0 :                         GDKfree(buf);
     732           0 :                         buf = GDKmalloc(size);
     733           0 :                         if (buf == NULL) {
     734             :                                 err = SQLSTATE(HY013) MAL_MALLOC_FAIL;
     735           0 :                                 goto bunins_failed;
     736             :                         }
     737             :                 }
     738           2 :                 if (strNil(t)) {
     739           0 :                         strcpy(buf, str_nil);
     740           0 :                         bn->tnonil = false;
     741             :                 } else {
     742           2 :                         strcpy(buf, "D<?xml");
     743           2 :                         i = strlen(buf);
     744           4 :                         if (!strNil(*version) && **version)
     745           2 :                                 i += snprintf(buf + i, len - i, " version=\"%s\"", *version);
     746           4 :                         if (!strNil(*standalone) && **standalone)
     747           2 :                                 i += snprintf(buf + i, len - i, " standalone=\"%s\"", *standalone);
     748           2 :                         snprintf(buf + i, len - i, "?>%s", t + 1);
     749           2 :                         buf++;
     750           2 :                         XMLisdocument(&isdoc, &buf); /* check well-formedness */
     751           2 :                         buf--;
     752           2 :                         if (!isdoc) {
     753             :                                 err = XML_NOT_WELL_FORMED;
     754           0 :                                 goto bunins_failed;
     755             :                         }
     756             :                 }
     757           2 :                 if (bunfastapp_nocheckVAR(bn, buf) != GDK_SUCCEED)
     758           0 :                         goto bunins_failed;
     759             :         }
     760           1 :         bat_iterator_end(&bi);
     761           1 :         GDKfree(buf);
     762           1 :         finalizeResult(ret, bn, b);
     763           1 :         return MAL_SUCCEED;
     764             :   bunins_failed:
     765           0 :         bat_iterator_end(&bi);
     766           0 :         BBPunfix(b->batCacheid);
     767           0 :         BBPunfix(bn->batCacheid);
     768           0 :         if (buf != NULL)
     769           0 :                 GDKfree(buf);
     770           0 :         throw(MAL, "xml.root", "%s", err);
     771             : }
     772             : 
     773             : static str
     774          10 : BATXMLattribute(bat *ret, const char * const *name, const bat *bid)
     775             : {
     776             :         BAT *b, *bn;
     777             :         BUN p, q;
     778             :         size_t size = BUFSIZ;
     779             :         str buf;
     780             :         BATiter bi;
     781             :         size_t attrlen;
     782             :         const char *err = OPERATION_FAILED;
     783             : 
     784          20 :         if (strNil(*name))
     785           0 :                 throw(MAL, "xml.attribute", XML_ATTRIBUTE_ERROR);
     786          10 :         if (xmlValidateName((xmlChar *) *name, 0) != 0)
     787           0 :                 throw(MAL, "xml.attribute", XML_ATTRIBUTE_INVALID);
     788          10 :         attrlen = strlen(*name) + 5;
     789          10 :         buf = GDKmalloc(size);
     790          10 :         if (buf == NULL)
     791           0 :                 throw(MAL, "xml.attribute", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     792          10 :         if ((b = BATdescriptor(*bid)) == NULL) {
     793           0 :                 GDKfree(buf);
     794           0 :                 throw(MAL, "xml.attribute", INTERNAL_BAT_ACCESS);
     795             :         }
     796          10 :         prepareResult(bn, b, TYPE_xml, "attribute", GDKfree(buf));
     797          10 :         bi = bat_iterator(b);
     798          34 :         BATloop(b, p, q) {
     799          24 :                 const char *t = (const char *) BUNtvar(bi, p);
     800             :                 size_t len;
     801             : 
     802             :                 len = attrlen;
     803          24 :                 if (!strNil(t))
     804          24 :                         len += strlen(t) * 6 + 1;
     805          24 :                 if (len >= size) {
     806             :                         /* make sure there is enough space */
     807           0 :                         size = len + 128;
     808             :                         /* free/malloc so we don't copy */
     809           0 :                         GDKfree(buf);
     810           0 :                         buf = GDKmalloc(size);
     811           0 :                         if (buf == NULL) {
     812             :                                 err = SQLSTATE(HY013) MAL_MALLOC_FAIL;
     813           0 :                                 goto bunins_failed;
     814             :                         }
     815             :                 }
     816          24 :                 if (strNil(t)) {
     817           0 :                         strcpy(buf, str_nil);
     818           0 :                         bn->tnonil = false;
     819             :                 } else {
     820          24 :                         int n = snprintf(buf, size, "A%s = \"", *name);
     821          24 :                         size_t m = XMLquotestring(t, buf + n, size - n);
     822          24 :                         strcpy(buf + n + m, "\"");
     823             :                 }
     824          24 :                 if (bunfastapp_nocheckVAR(bn, buf) != GDK_SUCCEED)
     825           0 :                         goto bunins_failed;
     826             :         }
     827          10 :         bat_iterator_end(&bi);
     828          10 :         GDKfree(buf);
     829          10 :         finalizeResult(ret, bn, b);
     830          10 :         return MAL_SUCCEED;
     831           0 :   bunins_failed:
     832           0 :         bat_iterator_end(&bi);
     833           0 :         BBPunfix(b->batCacheid);
     834           0 :         BBPunfix(bn->batCacheid);
     835           0 :         if (buf != NULL)
     836           0 :                 GDKfree(buf);
     837           0 :         throw(MAL, "xml.attribute", "%s", err);
     838             : }
     839             : 
     840             : static str
     841          57 : BATXMLelement(bat *ret, const char * const *name, xml *nspace, xml *attr, const bat *bid)
     842             : {
     843             :         BAT *b, *bn;
     844             :         BUN p, q;
     845             :         size_t size = BUFSIZ;
     846             :         str buf;
     847             :         BATiter bi;
     848             :         size_t elemlen, namelen;
     849             :         const char *err = OPERATION_FAILED;
     850             : 
     851         114 :         if (strNil(*name))
     852           0 :                 throw(MAL, "xml.element", XML_NO_ELEMENT);
     853          57 :         if (xmlValidateName((xmlChar *) *name, 0) != 0)
     854           0 :                 throw(MAL, "xml.element", XML_ATTRIBUTE_INVALID);
     855         106 :         if (nspace && !strNil(*nspace) && **nspace)
     856           0 :                 throw(MAL, "xml.element", XML_NO_NAMESPACE);
     857          57 :         namelen = strlen(*name);
     858          57 :         elemlen = namelen + 5;
     859         106 :         if (nspace && !strNil(*nspace)) {
     860           0 :                 if (**nspace != 'A')
     861           0 :                         throw(MAL, "xml.element", XML_ILLEGAL_NAMESPACE);
     862           0 :                 elemlen += strlen(*nspace);     /* " " + nspace (nspace contains initial 'A' which is replaced by space) */
     863             :         }
     864         106 :         if (attr && !strNil(*attr)) {
     865           0 :                 if (**attr != 'A')
     866           0 :                         throw(MAL, "xml.element", XML_ILLEGAL_ATTRIBUTE);
     867           0 :                 elemlen += strlen(*attr);       /* " " + attr (attr contains initial 'A' which is replaced by space) */
     868             :         }
     869          57 :         buf = GDKmalloc(size);
     870          57 :         if (buf == NULL)
     871           0 :                 throw(MAL, "xml.attribute", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     872          57 :         if ((b = BATdescriptor(*bid)) == NULL) {
     873           0 :                 GDKfree(buf);
     874           0 :                 throw(MAL, "xml.element", INTERNAL_BAT_ACCESS);
     875             :         }
     876          57 :         prepareResult(bn, b, TYPE_xml, "element", GDKfree(buf));
     877          57 :         bi = bat_iterator(b);
     878         170 :         BATloop(b, p, q) {
     879         113 :                 const char *t = (const char *) BUNtvar(bi, p);
     880             :                 size_t len;
     881             : 
     882             :                 len = elemlen;
     883         113 :                 if (!strNil(t)) {
     884         111 :                         if (*t != 'C') {
     885             :                                 err = XML_ILLEGAL_CONTENT;
     886           0 :                                 goto bunins_failed;
     887             :                         }
     888         111 :                         len += strlen(t + 1) + namelen + 2;  /* extra "<", ">", and name ("/" already counted) */
     889             :                 }
     890         113 :                 if (len >= size) {
     891             :                         /* make sure there is enough space */
     892           0 :                         size = len + 128;
     893             :                         /* free/malloc so we don't copy */
     894           0 :                         GDKfree(buf);
     895           0 :                         buf = GDKmalloc(size);
     896           0 :                         if (buf == NULL) {
     897             :                                 err = SQLSTATE(HY013) MAL_MALLOC_FAIL;
     898           0 :                                 goto bunins_failed;
     899             :                         }
     900             :                 }
     901         115 :                 if (strNil(t) && (!attr || strNil(*attr))) {
     902           2 :                         strcpy(buf, str_nil);
     903           2 :                         bn->tnonil = false;
     904             :                 } else {
     905         111 :                         int i = snprintf(buf, size, "C<%s", *name);
     906         204 :                         if (nspace && !strNil(*nspace))
     907           0 :                                 i += snprintf(buf + i, size - i, " %s", *nspace + 1);
     908         204 :                         if (attr && !strNil(*attr))
     909           0 :                                 i += snprintf(buf + i, size - i, " %s", *attr + 1);
     910         111 :                         if (!strNil(t))
     911         111 :                                 i += snprintf(buf + i, size - i, ">%s</%s>", t + 1, *name);
     912             :                         else
     913           0 :                                 i += snprintf(buf + i, size - i, "/>");
     914             :                 }
     915         113 :                 if (bunfastapp_nocheckVAR(bn, buf) != GDK_SUCCEED)
     916           0 :                         goto bunins_failed;
     917             :         }
     918          57 :         bat_iterator_end(&bi);
     919          57 :         GDKfree(buf);
     920          57 :         finalizeResult(ret, bn, b);
     921          57 :         return MAL_SUCCEED;
     922           0 :   bunins_failed:
     923           0 :         bat_iterator_end(&bi);
     924           0 :         BBPunfix(b->batCacheid);
     925           0 :         BBPunfix(bn->batCacheid);
     926           0 :         if (buf != NULL)
     927           0 :                 GDKfree(buf);
     928           0 :         throw(MAL, "xml.element", "%s", err);
     929             : }
     930             : 
     931             : static str
     932           8 : BATXMLelementSmall(bat *ret, const char * const *name, const bat *bid)
     933             : {
     934           8 :         return BATXMLelement(ret, name, NULL, NULL, bid);
     935             : }
     936             : 
     937             : static str
     938           1 : BATXMLforest(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     939             : {
     940           1 :         bat *ret = getArgReference_bat(stk, pci, 0);
     941             :         BAT *bn;
     942             :         BATiter *bi;
     943             :         BUN *p, *q;
     944             :         str buf;
     945             :         int i;
     946             :         size_t offset, len, size = BUFSIZ;
     947             :         const char *err = OPERATION_FAILED;
     948             : 
     949             :         (void) mb;
     950             :         (void) cntxt;
     951           1 :         buf = GDKmalloc(size);
     952           1 :         bi = GDKmalloc(sizeof(BATiter) * pci->argc);
     953           1 :         p = GDKmalloc(sizeof(BUN) * pci->argc);
     954           1 :         q = GDKmalloc(sizeof(BUN) * pci->argc);
     955           1 :         if (buf == NULL || bi == NULL || p == NULL || q == NULL) {
     956           0 :                 if (buf)
     957           0 :                         GDKfree(buf);
     958           0 :                 if (bi)
     959           0 :                         GDKfree(bi);
     960           0 :                 if (p)
     961           0 :                         GDKfree(p);
     962           0 :                 if (q)
     963           0 :                         GDKfree(q);
     964           0 :                 throw(MAL, "xml.forest", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     965             :         }
     966             : 
     967             :         /* collect the admin for the xml elements */
     968           3 :         for (i = pci->retc; i < pci->argc; i++) {
     969           2 :                 BAT *b = BATdescriptor(*getArgReference_bat(stk, pci, i));
     970           2 :                 if (b == NULL)
     971             :                         break;
     972           2 :                 bi[i] = bat_iterator(b);
     973           2 :                 p[i] = 0;
     974           2 :                 q[i] = BUNlast(bi[i].b);
     975             :         }
     976             :         /* check for errors */
     977           1 :         if (i != pci->argc) {
     978           0 :                 for (i--; i >= pci->retc; i--)
     979           0 :                         if (bi[i].b) {
     980             :                                 BAT *b = bi[i].b;
     981           0 :                                 bat_iterator_end(&bi[i]);
     982           0 :                                 BBPunfix(b->batCacheid);
     983             :                         }
     984           0 :                 GDKfree(bi);
     985           0 :                 GDKfree(p);
     986           0 :                 GDKfree(q);
     987           0 :                 GDKfree(buf);
     988           0 :                 throw(MAL, "xml.forest", INTERNAL_BAT_ACCESS);
     989             :         }
     990             : 
     991           1 :         prepareResult(bn, bi[pci->retc].b, TYPE_xml, "forest",
     992             :                                   for (i = pci->retc; i < pci->argc; i++) {BAT *b = bi[i].b; bat_iterator_end(&bi[i]); BBPunfix(b->batCacheid);}
     993             :                                   GDKfree(bi); GDKfree(p); GDKfree(q); GDKfree(buf));
     994             : 
     995           3 :         while (p[pci->retc] < q[pci->retc]) {
     996             :                 const char *t;
     997             : 
     998             :                 /* fetch the elements */
     999             :                 offset = 0;
    1000           2 :                 strcpy(buf, str_nil);
    1001           6 :                 for (i = pci->retc; i < pci->argc; i++) {
    1002             :                         int n;
    1003             : 
    1004           4 :                         t = (const char *) BUNtvar(bi[i], p[i]);
    1005           4 :                         if (strNil(t))
    1006           0 :                                 continue;
    1007             : 
    1008           4 :                         if ((len = strlen(t)) >= size - offset) {
    1009             :                                 char *tmp;
    1010           0 :                                 size += len + 128;
    1011           0 :                                 tmp = GDKrealloc(buf, size);
    1012           0 :                                 if (tmp == NULL) {
    1013             :                                         err = SQLSTATE(HY013) MAL_MALLOC_FAIL;
    1014           0 :                                         goto bunins_failed;
    1015             :                                 }
    1016             :                                 buf = tmp;
    1017             :                         }
    1018           4 :                         if (offset == 0)
    1019           2 :                                 n = snprintf(buf, size, "%s", t);
    1020           2 :                         else if (buf[0] != *t) {
    1021             :                                 err = "incompatible values in forest";
    1022           0 :                                 goto bunins_failed;
    1023           2 :                         } else if (buf[0] == 'A')
    1024           0 :                                 n = snprintf(buf + offset, size - offset, " %s", t + 1);
    1025           2 :                         else if (buf[0] == 'C')
    1026           2 :                                 n = snprintf(buf + offset, size - offset, "%s", t + 1);
    1027             :                         else {
    1028             :                                 err = "can only combine attributes and element content";
    1029           0 :                                 goto bunins_failed;
    1030             :                         }
    1031           4 :                         offset += n;
    1032             :                 }
    1033           2 :                 if (bunfastapp_nocheckVAR(bn, buf) != GDK_SUCCEED)
    1034           0 :                         goto bunins_failed;
    1035           2 :                 if (offset == 0)
    1036           0 :                         bn->tnonil = false;
    1037             : 
    1038           6 :                 for (i = pci->retc; i < pci->argc; i++)
    1039           4 :                         if (bi[i].b)
    1040           4 :                                 p[i]++;
    1041             :         }
    1042           1 :         GDKfree(buf);
    1043           1 :         BATsetcount(bn, bn->batCount);
    1044           1 :         *ret = bn->batCacheid;
    1045           1 :         BBPkeepref(*ret);
    1046           3 :         for (i = pci->retc; i < pci->argc; i++) {
    1047           2 :                 BAT *b = bi[i].b;
    1048           2 :                 if (b) {
    1049           2 :                         bat_iterator_end(&bi[i]);
    1050           2 :                         BBPunfix(b->batCacheid);
    1051             :                 }
    1052             :         }
    1053           1 :         GDKfree(bi);
    1054           1 :         GDKfree(p);
    1055           1 :         GDKfree(q);
    1056           1 :         return MAL_SUCCEED;
    1057           0 : bunins_failed:
    1058           0 :         for (i = pci->retc; i < pci->argc; i++) {
    1059           0 :                 BAT *b = bi[i].b;
    1060           0 :                 if (b) {
    1061           0 :                         bat_iterator_end(&bi[i]);
    1062           0 :                         BBPunfix(b->batCacheid);
    1063             :                 }
    1064             :         }
    1065           0 :         BBPunfix(bn->batCacheid);
    1066           0 :         if (buf != NULL)
    1067           0 :                 GDKfree(buf);
    1068           0 :         GDKfree(bi);
    1069           0 :         GDKfree(p);
    1070           0 :         GDKfree(q);
    1071           0 :         throw(MAL, "xml.forest", "%s", err);
    1072             : }
    1073             : 
    1074             : static str
    1075          33 : BATXMLconcat(bat *ret, const bat *bid, const bat *rid)
    1076             : {
    1077             :         BAT *b, *r = 0, *bn;
    1078             :         BUN p, q, rp = 0;
    1079             :         size_t len, size = BUFSIZ;
    1080          33 :         str buf = GDKmalloc(size);
    1081             :         BATiter bi, ri;
    1082             :         const char *err = OPERATION_FAILED;
    1083             : 
    1084          33 :         if (buf == NULL)
    1085           0 :                 throw(MAL, "xml.concat", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1086          33 :         b = BATdescriptor(*bid);
    1087          33 :         r = BATdescriptor(*rid);
    1088          33 :         if (b == NULL || r == NULL) {
    1089           0 :                 GDKfree(buf);
    1090           0 :                 if (b)
    1091           0 :                         BBPunfix(b->batCacheid);
    1092           0 :                 if (r)
    1093           0 :                         BBPunfix(r->batCacheid);
    1094           0 :                 throw(MAL, "xml.concat", INTERNAL_BAT_ACCESS);
    1095             :         }
    1096             :         p = 0;
    1097          33 :         q = BUNlast(b);
    1098             :         rp = 0;
    1099             : 
    1100          33 :         prepareResult(bn, b, TYPE_xml, "concat",
    1101             :                                   GDKfree(buf); BBPunfix(r->batCacheid));
    1102             : 
    1103          33 :         bi = bat_iterator(b);
    1104          33 :         ri = bat_iterator(r);
    1105          94 :         while (p < q) {
    1106          61 :                 const char *t = (const char *) BUNtvar(bi, p);
    1107          61 :                 const char *v = (const char *) BUNtvar(ri, rp);
    1108             : 
    1109          61 :                 len = strlen(t) + strlen(v) + 1;
    1110             : 
    1111          61 :                 if (len >= size) {
    1112           0 :                         GDKfree(buf);
    1113           0 :                         size = len + 128;
    1114           0 :                         buf = GDKmalloc(size);
    1115           0 :                         if (buf == NULL) {
    1116             :                                 err= MAL_MALLOC_FAIL;
    1117           0 :                                 goto bunins_failed;
    1118             :                         }
    1119             :                 }
    1120          61 :                 if (strNil(t)) {
    1121           0 :                         if (strNil(v)) {
    1122           0 :                                 strcpy(buf, str_nil);
    1123           0 :                                 bn->tnonil = false;
    1124             :                         } else
    1125           0 :                                 strcpy(buf, v);
    1126             :                 } else {
    1127          61 :                         if (strNil(v))
    1128           2 :                            strcpy(buf, t);
    1129          59 :                         else if (*t != *v) {
    1130             :                                 err = "arguments not compatible";
    1131           0 :                                 goto bunins_failed;
    1132          59 :                         } else if (*t == 'A')
    1133           2 :                                 snprintf(buf, size, "A%s %s", t + 1, v + 1);
    1134          57 :                         else if (*t == 'C')
    1135          57 :                                 snprintf(buf, size, "C%s%s", t + 1, v + 1);
    1136             :                         else {
    1137             :                                 err = "can only concatenate attributes and element content";
    1138           0 :                                 goto bunins_failed;
    1139             :                         }
    1140             :                 }
    1141          61 :                 if (bunfastapp_nocheckVAR(bn, buf) != GDK_SUCCEED)
    1142           0 :                         goto bunins_failed;
    1143          61 :                 rp++;
    1144             :                 p++;
    1145             :         }
    1146          33 :         bat_iterator_end(&bi);
    1147          33 :         bat_iterator_end(&ri);
    1148          33 :         GDKfree(buf);
    1149          33 :         finalizeResult(ret, bn, b);
    1150          33 :         return MAL_SUCCEED;
    1151           0 :   bunins_failed:
    1152           0 :         bat_iterator_end(&bi);
    1153           0 :         bat_iterator_end(&ri);
    1154           0 :         BBPunfix(r->batCacheid);
    1155           0 :         BBPunfix(b->batCacheid);
    1156           0 :         BBPunfix(bn->batCacheid);
    1157           0 :         if (buf != NULL)
    1158           0 :                 GDKfree(buf);
    1159           0 :         throw(MAL, "xml.concat", "%s", err);
    1160             : }
    1161             : 
    1162             : static str
    1163           2 : BATXMLgroup(xml *ret, const bat *bid)
    1164             : {
    1165             :         BAT *b;
    1166             :         BUN p, q;
    1167             :         const char *t;
    1168             :         size_t len, size = BUFSIZ, offset;
    1169           2 :         str buf = GDKmalloc(size);
    1170             :         BATiter bi;
    1171             :         const char *err = NULL;
    1172             : 
    1173           2 :         if (buf == NULL)
    1174           0 :                 throw(MAL, "xml.aggr", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1175           2 :         if ((b = BATdescriptor(*bid)) == NULL) {
    1176           0 :                 GDKfree(buf);
    1177           0 :                 throw(MAL, "xml.aggr", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    1178             :         }
    1179             : 
    1180           2 :         strcpy(buf, str_nil);
    1181             :         offset = 0;
    1182           2 :         bi = bat_iterator(b);
    1183           5 :         BATloop(b, p, q) {
    1184             :                 int n;
    1185             : 
    1186           3 :                 t = (const char *) BUNtvar(bi, p);
    1187             : 
    1188           3 :                 if (strNil(t))
    1189           0 :                         continue;
    1190           3 :                 len = strlen(t) + 1;
    1191           3 :                 if (len >= size - offset) {
    1192             :                         char *tmp;
    1193           0 :                         size += len + 128;
    1194           0 :                         tmp = GDKrealloc(buf, size);
    1195           0 :                         if (tmp == NULL) {
    1196             :                                 err= MAL_MALLOC_FAIL;
    1197           0 :                                 goto failed;
    1198             :                         }
    1199             :                         buf = tmp;
    1200             :                 }
    1201           3 :                 if (offset == 0)
    1202           1 :                         n = snprintf(buf, size, "%s", t);
    1203           2 :                 else if (buf[0] != *t) {
    1204             :                         err = "incompatible values in group";
    1205           0 :                         goto failed;
    1206           2 :                 } else if (buf[0] == 'A')
    1207           0 :                         n = snprintf(buf + offset, size - offset, " %s", t + 1);
    1208           2 :                 else if (buf[0] == 'C')
    1209           2 :                         n = snprintf(buf + offset, size - offset, "%s", t + 1);
    1210             :                 else {
    1211             :                         err = "can only group attributes and element content";
    1212           0 :                         goto failed;
    1213             :                 }
    1214           3 :                 offset += n;
    1215             :         }
    1216           2 :         bat_iterator_end(&bi);
    1217           2 :         BBPunfix(b->batCacheid);
    1218           2 :         *ret = buf;
    1219           2 :         return MAL_SUCCEED;
    1220           0 :   failed:
    1221           0 :         bat_iterator_end(&bi);
    1222           0 :         BBPunfix(b->batCacheid);
    1223           0 :         if (buf != NULL)
    1224           0 :                 GDKfree(buf);
    1225           0 :         throw(MAL, "xml.aggr", "%s", err);
    1226             : }
    1227             : 
    1228             : static const char *
    1229           2 : BATxmlaggr(BAT **bnp, BAT *b, BAT *g, BAT *e, BAT *s, int skip_nils)
    1230             : {
    1231           2 :         BAT *bn = NULL, *t1, *t2 = NULL;
    1232           2 :         BATiter bi =  (BATiter) { .b = NULL };
    1233             :         oid min, max;
    1234             :         BUN ngrp;
    1235             :         BUN nils = 0;
    1236             :         BUN ncand;
    1237             :         struct canditer ci;
    1238             :         int isnil;
    1239             :         const char *v;
    1240             :         const oid *grps, *map;
    1241             :         oid mapoff = 0;
    1242             :         oid prev;
    1243             :         BUN p, q;
    1244             :         int freeb = 0, freeg = 0;
    1245             :         char *buf = NULL;
    1246             :         size_t buflen, maxlen, len;
    1247             :         const char *err;
    1248             :         char *tmp;
    1249             : 
    1250           2 :         if ((err = BATgroupaggrinit(b, g, e, s, &min, &max, &ngrp, &ci, &ncand)) != NULL) {
    1251             :                 return err;
    1252             :         }
    1253           2 :         assert(b->ttype == TYPE_xml);
    1254           2 :         if (BATcount(b) == 0 || ngrp == 0) {
    1255           0 :                 bn = BATconstant(ngrp == 0 ? 0 : min, TYPE_xml, ATOMnilptr(TYPE_xml), ngrp, TRANSIENT);
    1256           0 :                 if (bn == NULL)
    1257             :                         return MAL_MALLOC_FAIL;
    1258           0 :                 *bnp = bn;
    1259           0 :                 return NULL;
    1260             :         }
    1261           2 :         if (s) {
    1262           0 :                 b = BATproject(s, b);
    1263           0 :                 if (b == NULL) {
    1264             :                         err = "internal project failed";
    1265           0 :                         goto out;
    1266             :                 }
    1267             :                 freeb = 1;
    1268           0 :                 if (g) {
    1269           0 :                         g = BATproject(s, g);
    1270           0 :                         if (g == NULL) {
    1271             :                                 err = "internal project failed";
    1272           0 :                                 goto out;
    1273             :                         }
    1274             :                         freeg = 1;
    1275             :                 }
    1276             :         }
    1277           2 :         if (g && BATtdense(g)) {
    1278             :                 /* singleton groups: return group ID's (g's tail) and original
    1279             :                  * values from b */
    1280           0 :                 bn = VIEWcreate(g->tseqbase, b);
    1281           0 :                 goto out;
    1282             :         }
    1283             : 
    1284             :         maxlen = BUFSIZ;
    1285           2 :         if ((buf = GDKmalloc(maxlen)) == NULL) {
    1286             :                 err = SQLSTATE(HY013) MAL_MALLOC_FAIL;
    1287           0 :                 goto out;
    1288             :         }
    1289             :         buflen = 0;
    1290           2 :         bn = COLnew(min, TYPE_xml, ngrp, TRANSIENT);
    1291           2 :         if (bn == NULL) {
    1292             :                 err = SQLSTATE(HY013) MAL_MALLOC_FAIL;
    1293           0 :                 goto out;
    1294             :         }
    1295           2 :         bi = bat_iterator(b);
    1296           2 :         if (g) {
    1297             :                 /* stable sort g */
    1298           2 :                 if (BATsort(&t1, &t2, NULL, g, NULL, NULL, false, false, true) != GDK_SUCCEED) {
    1299           0 :                         BBPreclaim(bn);
    1300             :                         bn = NULL;
    1301             :                         err = GDK_EXCEPTION;
    1302           0 :                         goto out1;
    1303             :                 }
    1304           2 :                 if (freeg)
    1305           0 :                         BBPunfix(g->batCacheid);
    1306           2 :                 g = t1;
    1307             :                 freeg = 1;
    1308           2 :                 if (t2->ttype == TYPE_void) {
    1309             :                         map = NULL;
    1310           2 :                         mapoff = b->tseqbase;
    1311             :                 } else {
    1312           0 :                         map = (const oid *) Tloc(t2, 0);
    1313             :                 }
    1314           2 :                 grps = (const oid *) Tloc(g, 0);
    1315           2 :                 prev = grps[0];
    1316             :                 isnil = 0;
    1317           9 :                 for (p = 0, q = BATcount(g); p <= q; p++) {
    1318           9 :                         if (p == q || grps[p] != prev) {
    1319           5 :                                 while (BATcount(bn) < prev - min) {
    1320           0 :                                         if (bunfastapp_nocheckVAR(bn, str_nil) != GDK_SUCCEED)
    1321           0 :                                                 goto bunins_failed;
    1322           0 :                                         nils++;
    1323             :                                 }
    1324           5 :                                 if (bunfastapp_nocheckVAR(bn, buf) != GDK_SUCCEED)
    1325           0 :                                         goto bunins_failed;
    1326           5 :                                 nils += strNil(buf);
    1327           5 :                                 strncpy(buf, str_nil, maxlen);
    1328             :                                 buflen = 0;
    1329           5 :                                 if (p == q)
    1330             :                                         break;
    1331           3 :                                 prev = grps[p];
    1332             :                                 isnil = 0;
    1333             :                         }
    1334           7 :                         if (isnil)
    1335           0 :                                 continue;
    1336           7 :                         v = (const char *) BUNtvar(bi, (map ? (BUN) map[p] : p + mapoff));
    1337           7 :                         if (strNil(v)) {
    1338           0 :                                 if (skip_nils)
    1339           0 :                                         continue;
    1340           0 :                                 strncpy(buf, str_nil, buflen);
    1341             :                                 isnil = 1;
    1342             :                         } else {
    1343           7 :                                 len = strlen(v);
    1344           7 :                                 if (len >= maxlen - buflen) {
    1345           0 :                                         maxlen += len + BUFSIZ;
    1346           0 :                                         tmp = GDKrealloc(buf, maxlen);
    1347           0 :                                         if (tmp == NULL) {
    1348             :                                                 err = SQLSTATE(HY013) MAL_MALLOC_FAIL;
    1349           0 :                                                 goto bunins_failed;
    1350             :                                         }
    1351             :                                         buf = tmp;
    1352             :                                 }
    1353           7 :                                 if (buflen == 0) {
    1354           5 :                                         strncpy(buf, v, maxlen);
    1355           5 :                                         buflen += len;
    1356           2 :                                 } else if (buf[0] != v[0]) {
    1357             :                                         err = "incompatible values in group";
    1358           0 :                                         goto bunins_failed;
    1359           2 :                                 } else if (buf[0] == 'A') {
    1360           0 :                                         snprintf(buf + buflen, maxlen - buflen, " %s", v + 1);
    1361           0 :                                         buflen += len;
    1362           2 :                                 } else if (buf[0] == 'C') {
    1363           2 :                                         snprintf(buf + buflen, maxlen - buflen, "%s", v + 1);
    1364           2 :                                         buflen += len - 1;
    1365             :                                 } else {
    1366             :                                         err = "can only group attributes and element content";
    1367           0 :                                         goto bunins_failed;
    1368             :                                 }
    1369             :                         }
    1370             :                 }
    1371           2 :                 BBPunfix(t2->batCacheid);
    1372           2 :                 t2 = NULL;
    1373             :         } else {
    1374           0 :                 for (p = 0, q = p + BATcount(b); p < q; p++) {
    1375           0 :                         v = (const char *) BUNtvar(bi, p);
    1376           0 :                         if (strNil(v)) {
    1377           0 :                                 if (skip_nils)
    1378           0 :                                         continue;
    1379           0 :                                 strncpy(buf, str_nil, buflen);
    1380             :                                 nils++;
    1381           0 :                                 break;
    1382             :                         }
    1383           0 :                         len = strlen(v);
    1384           0 :                         if (len >= maxlen - buflen) {
    1385           0 :                                 maxlen += len + BUFSIZ;
    1386           0 :                                 tmp = GDKrealloc(buf, maxlen);
    1387           0 :                                 if (tmp == NULL) {
    1388             :                                         err = SQLSTATE(HY013) MAL_MALLOC_FAIL;
    1389           0 :                                         goto bunins_failed;
    1390             :                                 }
    1391             :                                 buf = tmp;
    1392             :                         }
    1393           0 :                         if (buflen == 0) {
    1394           0 :                                 strncpy(buf, v, maxlen);
    1395           0 :                                 buflen += len;
    1396           0 :                         } else if (buf[0] != v[0]) {
    1397             :                                 err = "incompatible values in group";
    1398           0 :                                 goto bunins_failed;
    1399           0 :                         } else if (buf[0] == 'A') {
    1400           0 :                                 snprintf(buf + buflen, maxlen - buflen, " %s", v + 1);
    1401           0 :                                 buflen += len;
    1402           0 :                         } else if (buf[0] == 'C') {
    1403           0 :                                 snprintf(buf + buflen, maxlen - buflen, "%s", v + 1);
    1404           0 :                                 buflen += len - 1;
    1405             :                         } else {
    1406             :                                 err = "can only group attributes and element content";
    1407           0 :                                 goto bunins_failed;
    1408             :                         }
    1409             :                 }
    1410           0 :                 if (bunfastapp_nocheckVAR(bn, buf) != GDK_SUCCEED)
    1411           0 :                         goto bunins_failed;
    1412             :         }
    1413           2 :         bn->theap->dirty = true;
    1414           2 :         bn->tnil = nils != 0;
    1415           2 :         bn->tnonil = nils == 0;
    1416           2 :         bn->tsorted = BATcount(bn) <= 1;
    1417           2 :         bn->trevsorted = BATcount(bn) <= 1;
    1418           2 :         bn->tkey = BATcount(bn) <= 1;
    1419             : 
    1420           2 :   out1:
    1421           2 :         bat_iterator_end(&bi);
    1422           2 :   out:
    1423           2 :         if (t2)
    1424           0 :                 BBPunfix(t2->batCacheid);
    1425           2 :         if (freeb && b)
    1426           0 :                 BBPunfix(b->batCacheid);
    1427           2 :         if (freeg && g)
    1428           2 :                 BBPunfix(g->batCacheid);
    1429           2 :         if (buf)
    1430           2 :                 GDKfree(buf);
    1431           2 :         *bnp = bn;
    1432           2 :         return err;
    1433             : 
    1434           0 :   bunins_failed:
    1435           0 :         bat_iterator_end(&bi);
    1436           0 :         BBPreclaim(bn);
    1437             :         bn = NULL;
    1438           0 :         if (err == NULL)
    1439             :                 err = SQLSTATE(HY013) MAL_MALLOC_FAIL;  /* insertion into result BAT failed */
    1440           0 :         goto out;
    1441             : }
    1442             : 
    1443             : static str
    1444           2 : AGGRsubxmlcand(bat *retval, const bat *bid, const bat *gid, const bat *eid, const bat *sid, const bit *skip_nils)
    1445             : {
    1446           2 :         BAT *b, *g, *e, *s, *bn = NULL;
    1447             :         const char *err;
    1448             : 
    1449           2 :         b = BATdescriptor(*bid);
    1450           2 :         g = gid ? BATdescriptor(*gid) : NULL;
    1451           2 :         e = eid ? BATdescriptor(*eid) : NULL;
    1452           2 :         if (b == NULL || (gid != NULL && g == NULL) || (eid != NULL && e == NULL)) {
    1453           0 :                 if (b)
    1454           0 :                         BBPunfix(b->batCacheid);
    1455           0 :                 if (g)
    1456           0 :                         BBPunfix(g->batCacheid);
    1457           0 :                 if (e)
    1458           0 :                         BBPunfix(e->batCacheid);
    1459           0 :                 throw(MAL, "aggr.subxml", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    1460             :         }
    1461           2 :         if (sid && !is_bat_nil(*sid)) {
    1462           0 :                 s = BATdescriptor(*sid);
    1463           0 :                 if (s == NULL) {
    1464           0 :                         BBPunfix(b->batCacheid);
    1465           0 :                         if (g)
    1466           0 :                                 BBPunfix(g->batCacheid);
    1467           0 :                         if (e)
    1468           0 :                                 BBPunfix(e->batCacheid);
    1469           0 :                         throw(MAL, "aggr.subxml", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    1470             :                 }
    1471             :         } else {
    1472             :                 s = NULL;
    1473             :         }
    1474           2 :         err = BATxmlaggr(&bn, b, g, e, s, *skip_nils);
    1475           2 :         BBPunfix(b->batCacheid);
    1476           2 :         if (g)
    1477           2 :                 BBPunfix(g->batCacheid);
    1478           2 :         if (e)
    1479           2 :                 BBPunfix(e->batCacheid);
    1480           2 :         if (s)
    1481           0 :                 BBPunfix(s->batCacheid);
    1482           2 :         if (err != NULL)
    1483           0 :                 throw(MAL, "aggr.subxml", "%s", err);
    1484             : 
    1485           2 :         *retval = bn->batCacheid;
    1486           2 :         BBPkeepref(bn->batCacheid);
    1487           2 :         return MAL_SUCCEED;
    1488             : }
    1489             : 
    1490             : static str
    1491           2 : AGGRsubxml(bat *retval, const bat *bid, const bat *gid, const bat *eid, const bit *skip_nils)
    1492             : {
    1493           2 :         return AGGRsubxmlcand(retval, bid, gid, eid, NULL, skip_nils);
    1494             : }
    1495             : 
    1496             : static str
    1497           0 : BATXMLxquery(bat *ret, const bat *bid, const char * const *expr)
    1498             : {
    1499             :         (void) ret;
    1500             :         (void) bid;
    1501             :         (void) expr;
    1502             :         /* use external library to solve this */
    1503           0 :         throw(MAL, "xml.xquery", SQLSTATE(0A000) PROGRAM_NYI);
    1504             : }
    1505             : 
    1506             : #else
    1507             : 
    1508             : #define NO_LIBXML_FATAL "batxml: MonetDB was built without libxml, but what you are trying to do requires it."
    1509             : 
    1510             : static str BATXMLxml2str(bat *ret, const bat *bid) {
    1511             :         (void) ret;
    1512             :         (void) bid;
    1513             :         return createException(MAL, "batxml.xml2str", SQLSTATE(HY005) NO_LIBXML_FATAL);
    1514             : }
    1515             : static str BATXMLxmltext(bat *ret, const bat *bid) {
    1516             :         (void) ret;
    1517             :         (void) bid;
    1518             :         return createException(MAL, "batxml.xmltext", SQLSTATE(HY005) NO_LIBXML_FATAL);
    1519             : }
    1520             : static str BATXMLstr2xml(bat *ret, const bat *bid) {
    1521             :         (void) ret;
    1522             :         (void) bid;
    1523             :         return createException(MAL, "batxml.str2xml", SQLSTATE(HY005) NO_LIBXML_FATAL);
    1524             : }
    1525             : static str BATXMLdocument(bat *ret, const bat *bid) {
    1526             :         (void) ret;
    1527             :         (void) bid;
    1528             :         return createException(MAL, "batxml.document", SQLSTATE(HY005) NO_LIBXML_FATAL);
    1529             : }
    1530             : static str BATXMLcontent(bat *ret, const bat *bid) {
    1531             :         (void) ret;
    1532             :         (void) bid;
    1533             :         return createException(MAL, "batxml.content", SQLSTATE(HY005) NO_LIBXML_FATAL);
    1534             : }
    1535             : static str BATXMLisdocument(bat *ret, const bat *bid) {
    1536             :         (void) ret;
    1537             :         (void) bid;
    1538             :         return createException(MAL, "batxml.isdocument", SQLSTATE(HY005) NO_LIBXML_FATAL);
    1539             : }
    1540             : static str BATXMLelementSmall(bat *ret, const char * const *name, const bat *bid) {
    1541             :         (void) ret;
    1542             :         (void) name;
    1543             :         (void) bid;
    1544             :         return createException(MAL, "batxml.elementSmall", SQLSTATE(HY005) NO_LIBXML_FATAL);
    1545             : }
    1546             : static str BATXMLoptions(bat *ret, const char * const *name, const char * const *options, const bat *bid) {
    1547             :         (void) ret;
    1548             :         (void) name;
    1549             :         (void) options;
    1550             :         (void) bid;
    1551             :         return createException(MAL, "batxml.options", SQLSTATE(HY005) NO_LIBXML_FATAL);
    1552             : }
    1553             : static str BATXMLcomment(bat *ret, const bat *bid) {
    1554             :         (void) ret;
    1555             :         (void) bid;
    1556             :         return createException(MAL, "batxml.comment", SQLSTATE(HY005) NO_LIBXML_FATAL);
    1557             : }
    1558             : static str BATXMLparse(bat *ret, const char * const *doccont, const bat *bid, const char * const *option) {
    1559             :         (void) ret;
    1560             :         (void) doccont;
    1561             :         (void) bid;
    1562             :         (void) option;
    1563             :         return createException(MAL, "batxml.parse", SQLSTATE(HY005) NO_LIBXML_FATAL);
    1564             : }
    1565             : static str BATXMLxquery(bat *ret, const bat *bid, const char * const *expr) {
    1566             :         (void) ret;
    1567             :         (void) bid;
    1568             :         (void) expr;
    1569             :         return createException(MAL, "batxml.xquery", SQLSTATE(HY005) NO_LIBXML_FATAL);
    1570             : }
    1571             : static str BATXMLpi(bat *ret, const char * const *tgt, const bat *bid) {
    1572             :         (void) ret;
    1573             :         (void) tgt;
    1574             :         (void) bid;
    1575             :         return createException(MAL, "batxml.pi", SQLSTATE(HY005) NO_LIBXML_FATAL);
    1576             : }
    1577             : static str BATXMLroot(bat *ret, const bat *bid, const char * const *version, const char * const *standalone) {
    1578             :         (void) ret;
    1579             :         (void) bid;
    1580             :         (void) version;
    1581             :         (void) standalone;
    1582             :         return createException(MAL, "batxml.root", SQLSTATE(HY005) NO_LIBXML_FATAL);
    1583             : }
    1584             : static str BATXMLattribute(bat *ret, const char * const *name, const bat *bid) {
    1585             :         (void) ret;
    1586             :         (void) name;
    1587             :         (void) bid;
    1588             :         return createException(MAL, "batxml.attribute", SQLSTATE(HY005) NO_LIBXML_FATAL);
    1589             : }
    1590             : static str BATXMLelement(bat *ret, const char * const *name, xml *ns, xml *attr, const bat *bid) {
    1591             :         (void) ret;
    1592             :         (void) name;
    1593             :         (void) ns;
    1594             :         (void) attr;
    1595             :         (void) bid;
    1596             :         return createException(MAL, "batxml.element", SQLSTATE(HY005) NO_LIBXML_FATAL);
    1597             : }
    1598             : static str BATXMLconcat(bat *ret, const bat *bid, const bat *rid) {
    1599             :         (void) ret;
    1600             :         (void) bid;
    1601             :         (void) rid;
    1602             :         return createException(MAL, "batxml.concat", SQLSTATE(HY005) NO_LIBXML_FATAL);
    1603             : }
    1604             : static str BATXMLforest(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr p) {
    1605             :         (void) cntxt;
    1606             :         (void) mb;
    1607             :         (void) stk;
    1608             :         (void) p;
    1609             :         return createException(MAL, "batxml.forest", SQLSTATE(HY005) NO_LIBXML_FATAL);
    1610             : }
    1611             : static str BATXMLgroup(xml *ret, const bat *bid) {
    1612             :         (void) ret;
    1613             :         (void) bid;
    1614             :         return createException(MAL, "batxml.group", SQLSTATE(HY005) NO_LIBXML_FATAL);
    1615             : }
    1616             : static str AGGRsubxmlcand(bat *retval, const bat *bid, const bat *gid, const bat *eid, const bat *sid, const bit *skip_nils) {
    1617             :         (void) retval;
    1618             :         (void) bid;
    1619             :         (void) gid;
    1620             :         (void) eid;
    1621             :         (void) sid;
    1622             :         (void) skip_nils;
    1623             :         return createException(MAL, "batxml.subxmlcand", SQLSTATE(HY005) NO_LIBXML_FATAL);
    1624             : }
    1625             : static str AGGRsubxml(bat *retval, const bat *bid, const bat *gid, const bat *eid, const bit *skip_nils) {
    1626             :         (void) retval;
    1627             :         (void) bid;
    1628             :         (void) gid;
    1629             :         (void) eid;
    1630             :         (void) skip_nils;
    1631             :         return createException(MAL, "batxml.subxml", SQLSTATE(HY005) NO_LIBXML_FATAL);
    1632             : }
    1633             : 
    1634             : #endif /* HAVE_LIBXML */
    1635             : 
    1636             : #include "mel.h"
    1637             : mel_func batxml_init_funcs[] = {
    1638             :  command("batxml", "xml", BATXMLstr2xml, false, "Cast the string to an xml compliant string.", args(1,2, batarg("",xml),batarg("src",str))),
    1639             :  command("batxml", "str", BATXMLxml2str, false, "Cast the xml to a string.", args(1,2, batarg("",str),batarg("src",xml))),
    1640             :  command("batxml", "document", BATXMLdocument, false, "Parse the string as an XML document.", args(1,2, batarg("",xml),batarg("src",str))),
    1641             :  command("batxml", "content", BATXMLcontent, false, "Parse the string as XML element content.", args(1,2, batarg("",xml),batarg("src",str))),
    1642             :  command("batxml", "comment", BATXMLcomment, false, "Create an XML comment element.", args(1,2, batarg("",xml),batarg("val",str))),
    1643             :  command("batxml", "parse", BATXMLparse, false, "Parse the XML document or element string values.", args(1,4, batarg("",xml),arg("doccont",str),batarg("val",str),arg("option",str))),
    1644             :  command("batxml", "serialize", BATXMLxml2str, false, "Serialize the XML object to a string.", args(1,2, batarg("",str),batarg("val",xml))),
    1645             :  command("batxml", "text", BATXMLxmltext, false, "Serialize the XML object to a string.", args(1,2, batarg("",str),batarg("val",xml))),
    1646             :  command("batxml", "xquery", BATXMLxquery, false, "Execute the XQuery against the elements.", args(1,3, batarg("",xml),batarg("val",str),arg("expr",str))),
    1647             :  command("batxml", "pi", BATXMLpi, false, "Construct a processing instruction.", args(1,3, batarg("",xml),arg("target",str),batarg("val",xml))),
    1648             :  command("batxml", "attribute", BATXMLattribute, false, "Construct an attribute value pair.", args(1,3, batarg("",xml),arg("name",str),batarg("val",str))),
    1649             :  command("batxml", "element", BATXMLelementSmall, false, "The basic building block for XML elements are namespaces, attributes and a sequence of XML elements. The name space and the attributes may be left unspecified.", args(1,3, batarg("",xml),arg("name",str),batarg("s",xml))),
    1650             :  command("batxml", "options", BATXMLoptions, false, "Create the components including NULL conversions.", args(1,4, batarg("",xml),arg("tag",str),arg("option",str),batarg("left",xml))),
    1651             :  command("batxml", "element", BATXMLelement, false, "The basic building block for XML elements are namespaces, attributes and a sequence of XML elements. The name space and the attributes may be left unspecified(=nil).", args(1,5, batarg("",xml),arg("name",str),arg("ns",xml),arg("attr",xml),batarg("s",xml))),
    1652             :  command("batxml", "concat", BATXMLconcat, false, "Concatenate the XML values.", args(1,3, batarg("",xml),batarg("left",xml),batarg("right",xml))),
    1653             :  pattern("batxml", "forest", BATXMLforest, false, "Construct an element list.", args(1,2, batarg("",xml),batvararg("val",xml))),
    1654             :  command("batxml", "root", BATXMLroot, false, "Contruct the root nodes.", args(1,4, batarg("",xml),batarg("val",xml),arg("version",str),arg("standalone",str))),
    1655             :  command("batxml", "isdocument", BATXMLisdocument, false, "Validate the string as a XML document.", args(1,2, batarg("",bit),batarg("val",str))),
    1656             :  command("xml", "aggr", BATXMLgroup, false, "Aggregate the XML values.", args(1,2, arg("",xml),batarg("val",xml))),
    1657             :  command("xml", "subaggr", AGGRsubxml, false, "Grouped aggregation of XML values.", args(1,5, batarg("",xml),batarg("val",xml),batarg("g",oid),batargany("e",1),arg("skip_nils",bit))),
    1658             :  command("xml", "subaggr", AGGRsubxmlcand, false, "Grouped aggregation of XML values with candidates list.", args(1,6, batarg("",xml),batarg("val",xml),batarg("g",oid),batargany("e",1),batarg("s",oid),arg("skip_nils",bit))),
    1659             :  command("batcalc", "xml", BATXMLstr2xml, false, "", args(1,2, batarg("",xml),batarg("src",str))),
    1660             :  { .imp=NULL }
    1661             : };
    1662             : #include "mal_import.h"
    1663             : #ifdef _MSC_VER
    1664             : #undef read
    1665             : #pragma section(".CRT$XCU",read)
    1666             : #endif
    1667         259 : LIB_STARTUP_FUNC(init_batxml_mal)
    1668         259 : { mal_module("batxml", NULL, batxml_init_funcs); }

Generated by: LCOV version 1.14