1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
|
/**
* @file xpath.c
* SQLite extension module to select parts of XML documents
* using libxml2 XPath and SQLite's virtual table mechanism.
*
* 2013 March 15
*
* The author disclaims copyright to this source code.
* In place of a legal notice, here is a blessing:
*
* May you do good and not evil.
* May you find forgiveness for yourself and forgive others.
* May you share freely, never taking more than you give.
*
********************************************************************
*/
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#endif
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#ifdef WITH_XSLT
#include <libxslt/xslt.h>
#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>
#endif
#ifdef STANDALONE
#include <sqlite3.h>
#else
#include <sqlite3ext.h>
static SQLITE_EXTENSION_INIT1
#endif
/**
* @typedef XDOC
* @struct XDOC
* Structure to cache XML document.
*/
typedef struct XDOC {
xmlDocPtr doc; /**< XML document. */
int refcnt; /**< Reference counter. */
} XDOC;
/**
* @typedef XMOD
* @struct XMOD
* Structure holding per module/database data.
*/
typedef struct XMOD {
int refcnt; /**< Reference counter. */
sqlite3_mutex *mutex; /**< DOC table mutex. */
int sdoc; /**< Size of docs array. */
int ndoc; /**< Number of used entries in docs array. */
XDOC *docs; /**< Array of modules's DOCs. */
} XMOD;
static int initialized = 0;
static XMOD *xmod = 0;
/**
* @typedef XTAB
* @struct XTAB
* Structure to describe virtual table.
*/
typedef struct XTAB {
sqlite3_vtab vtab; /**< SQLite virtual table. */
sqlite3 *db; /**< Open database. */
XMOD *xm; /**< Module data. */
struct XCSR *xc; /**< Current cursor. */
int sdoc; /**< Size of idocs array. */
int ndoc; /**< Number of used entries in idocs array. */
int *idocs; /**< Indexes in module-wide DOC table. */
} XTAB;
/**
* @typedef XEXP
* @struct XEXP
* Structure to describe XPath expression.
*/
typedef struct XEXP {
struct XEXP *next; /**< Next item. */
struct XEXP *prev; /**< Previous item. */
xmlDocPtr doc; /**< Current XML document. */
xmlXPathContextPtr pctx; /**< Current XPath context. */
xmlXPathObjectPtr pobj; /**< Current XPath objects. */
xmlNodePtr parent; /**< Current parent node or NULL. */
int pos; /**< Position within XPath expr. */
int conv; /**< Conversion: string/boolean/number. */
char expr[1]; /**< XPath expression text. */
} XEXP;
/**
* @typedef XCSR
* @struct XCSR
* Structure to describe virtual table cursor.
*/
typedef struct XCSR {
sqlite3_vtab_cursor cursor; /**< SQLite virtual table cursor */
int pos; /**< Current index. */
int nexpr; /**< Number of XPath expr. */
XEXP *first; /**< First XPath expr. */
XEXP *last; /**< Last XPath expr. */
} XCSR;
/**
* Connect to virtual table.
* @param db SQLite database pointer
* @param aux user specific pointer
* @param argc argument count
* @param argv argument vector
* @param vtabp pointer receiving virtual table pointer
* @param errp pointer receiving error messag
* @result SQLite error code
*
* Argument vector contains:
*
* argv[0] - module name<br>
* argv[1] - database name<br>
* argv[2] - table name (virtual table)<br>
*/
static int
xpath_connect(sqlite3* db, void *aux, int argc, const char * const *argv,
sqlite3_vtab **vtabp, char **errp)
{
int rc = SQLITE_ERROR;
XTAB *xt;
xt = sqlite3_malloc(sizeof (XTAB));
if (!xt) {
nomem:
*errp = sqlite3_mprintf("out of memory");
return rc;
}
memset(xt, 0, sizeof (XTAB));
xt->db = db;
xt->xm = (XMOD *) aux;
xt->xc = 0;
xt->sdoc = 128;
xt->ndoc = 0;
xt->idocs = sqlite3_malloc(xt->sdoc * sizeof (int));
if (!xt->idocs) {
sqlite3_free(xt);
goto nomem;
}
rc = sqlite3_declare_vtab(db,
"CREATE TABLE x("
" DOCID INTEGER PRIMARY KEY,"
" XML HIDDEN BLOB,"
" PATH HIDDEN TEXT,"
" OPTIONS HIDDEN INTEGER,"
" ENCODING HIDDEN TEXT,"
" BASEURL HIDDEN TEXT,"
" XMLDUMP HIDDEN TEXT"
")");
if (rc != SQLITE_OK) {
sqlite3_free(xt->idocs);
sqlite3_free(xt);
*errp = sqlite3_mprintf("table definition failed (error %d)", rc);
return rc;
}
*vtabp = &xt->vtab;
*errp = 0;
return SQLITE_OK;
}
/**
* Create virtual table.
* @param db SQLite database pointer
* @param aux user specific pointer
* @param argc argument count
* @param argv argument vector
* @param vtabp pointer receiving virtual table pointer
* @param errp pointer receiving error messag
* @result SQLite error code
*/
static int
xpath_create(sqlite3* db, void *aux, int argc,
const char *const *argv,
sqlite3_vtab **vtabp, char **errp)
{
return xpath_connect(db, aux, argc, argv, vtabp, errp);
}
/**
* Disconnect virtual table.
* @param vtab virtual table pointer
* @result always SQLITE_OK
*/
static int
xpath_disconnect(sqlite3_vtab *vtab)
{
XTAB *xt = (XTAB *) vtab;
XMOD *xm = xt->xm;
int i, n;
if (xm->mutex) {
sqlite3_mutex_enter(xm->mutex);
for (i = 0; xm->docs && (i < xt->ndoc); i++) {
n = xt->idocs[i];
if ((n >= 0) && (n < xm->sdoc)) {
xmlDocPtr doc = xm->docs[n].doc;
if (doc) {
xm->docs[n].refcnt -= 1;
if (xm->docs[n].refcnt <= 0) {
xm->docs[n].doc = 0;
xm->docs[n].refcnt = 0;
xm->ndoc--;
xmlFreeDoc(doc);
}
}
}
}
sqlite3_mutex_leave(xm->mutex);
}
sqlite3_free(xt->idocs);
sqlite3_free(xt);
return SQLITE_OK;
}
/**
* Destroy virtual table.
* @param vtab virtual table pointer
* @result always SQLITE_OK
*/
static int
xpath_destroy(sqlite3_vtab *vtab)
{
return xpath_disconnect(vtab);
}
/**
* Determines information for filter function according to constraints.
* @param vtab virtual table
* @param info index/constraint information
* @result SQLite error code
*/
static int
xpath_bestindex(sqlite3_vtab *vtab, sqlite3_index_info *info)
{
return SQLITE_OK;
}
/**
* Open virtual table and return cursor.
* @param vtab virtual table pointer
* @param cursorp pointer receiving cursor pointer
* @result SQLite error code
*/
static int
xpath_open(sqlite3_vtab *vtab, sqlite3_vtab_cursor **cursorp)
{
XCSR *xc = sqlite3_malloc(sizeof (XCSR));
if (!xc) {
return SQLITE_ERROR;
}
xc->cursor.pVtab = vtab;
xc->pos = -1;
xc->nexpr = 0;
xc->first = xc->last = 0;
*cursorp = &xc->cursor;
return SQLITE_OK;
}
/**
* Close virtual table cursor.
* @param cursor cursor pointer
* @result SQLite error code
*/
static int
xpath_close(sqlite3_vtab_cursor *cursor)
{
XCSR *xc = (XCSR *) cursor;
XEXP *xp = xc->first, *next;
XTAB *xt = (XTAB *) xc->cursor.pVtab;
while (xp) {
next = xp->next;
if (xp->pobj) {
xmlXPathFreeObject(xp->pobj);
}
if (xp->pctx) {
xmlXPathFreeContext(xp->pctx);
}
sqlite3_free(xp);
xp = next;
}
if (xt->xc == xc) {
xt->xc = 0;
}
sqlite3_free(xc);
return SQLITE_OK;
}
/**
* Retrieve next row from virtual table cursor.
* @param cursor virtual table cursor
* @result SQLite error code
*/
static int
xpath_next(sqlite3_vtab_cursor *cursor)
{
XCSR *xc = (XCSR *) cursor;
XTAB *xt = (XTAB *) xc->cursor.pVtab;
XEXP *xp;
if (xc->pos < xt->ndoc) {
int ninc = 0;
if ((xc->pos >= 0) && xc->nexpr) {
int newpos;
xmlNodePtr node, parent = 0;
xp = xc->first;
while (xp) {
if (xp->pobj) {
if (xp == xc->first) {
parent = xp->parent;
} else if (parent != xp->parent) {
break;
}
}
xp = xp->next;
}
if (parent && !xp) {
int pchg = 0;
xp = xc->first;
while (xp) {
if (xp->pobj && (xp->pobj->type == XPATH_NODESET) &&
xp->pobj->nodesetval) {
newpos = xp->pos + 1;
if (newpos < xp->pobj->nodesetval->nodeNr) {
node = xp->pobj->nodesetval->nodeTab[newpos];
if (node->parent != xp->parent) {
pchg++;
}
} else {
pchg++;
}
}
xp = xp->next;
}
if ((pchg != 0) && (pchg != xc->nexpr)) {
xp = xc->first;
while (xp) {
if (xp->pobj && (xp->pobj->type == XPATH_NODESET) &&
xp->pobj->nodesetval) {
newpos = xp->pos + 1;
if (newpos < xp->pobj->nodesetval->nodeNr) {
node = xp->pobj->nodesetval->nodeTab[newpos];
if (node->parent == xp->parent) {
xp->pos = newpos;
ninc++;
}
} else {
xp->pos = xp->pobj->nodesetval->nodeNr;
ninc++;
}
}
xp = xp->next;
}
}
}
if (!ninc) {
xp = xc->first;
while (xp) {
if (xp->pobj && (xp->pobj->type == XPATH_NODESET) &&
xp->pobj->nodesetval) {
newpos = xp->pos + 1;
if (newpos < xp->pobj->nodesetval->nodeNr) {
xp->pos = newpos;
ninc++;
} else {
xp->pos = xp->pobj->nodesetval->nodeNr;
}
}
xp = xp->next;
}
}
}
if (!ninc) {
xc->pos++;
xp = xc->first;
while (xp) {
xp->pos = -1;
xp->parent = 0;
xp = xp->next;
}
}
}
return SQLITE_OK;
}
/**
* Filter function for virtual table.
* @param cursor virtual table cursor
* @param idxNum not used
* @param idxStr nod used
* @param argc number arguments (not used)
* @param argv argument (nothing or RHS of filter expression, not used)
* @result SQLite error code
*/
static int
xpath_filter(sqlite3_vtab_cursor *cursor, int idxNum,
const char *idxStr, int argc, sqlite3_value **argv)
{
XCSR *xc = (XCSR *) cursor;
XTAB *xt = (XTAB *) xc->cursor.pVtab;
xc->pos = -1;
xt->xc = xc;
return xpath_next(cursor);
}
/**
* Return end of table state of virtual table cursor.
* @param cursor virtual table cursor
* @result true/false
*/
static int
xpath_eof(sqlite3_vtab_cursor *cursor)
{
XCSR *xc = (XCSR *) cursor;
XTAB *xt = (XTAB *) xc->cursor.pVtab;
return xc->pos >= xt->ndoc;
}
/**
* Return column data of virtual table.
* @param cursor virtual table cursor
* @param ctx SQLite function context
* @param n column index
* @result SQLite error code
*/
static int
xpath_column(sqlite3_vtab_cursor *cursor, sqlite3_context *ctx, int n)
{
XCSR *xc = (XCSR *) cursor;
XTAB *xt = (XTAB *) xc->cursor.pVtab;
XMOD *xm = (XMOD *) xt->xm;
if ((xc->pos < 0) || (xc->pos >= xt->ndoc)) {
sqlite3_result_error(ctx, "column out of bounds", -1);
return SQLITE_ERROR;
}
if (n == 0) {
n = xt->idocs[xc->pos];
if (xm->docs[n].doc) {
sqlite3_result_int(ctx, n + 1);
return SQLITE_OK;
}
} else if (n == 6) {
n = xt->idocs[xc->pos];
if (xm->docs[n].doc) {
xmlChar *dump = 0;
int dump_len = 0;
xmlDocDumpFormatMemoryEnc(xm->docs[n].doc, &dump,
&dump_len, "utf-8", 1);
if (dump) {
sqlite3_result_text(ctx, (char *) dump, dump_len,
SQLITE_TRANSIENT);
xmlFree(dump);
return SQLITE_OK;
}
}
}
sqlite3_result_null(ctx);
return SQLITE_OK;
}
/**
* Return current rowid of virtual table cursor.
* @param cursor virtual table cursor
* @param rowidp value buffer to receive current rowid
* @result SQLite error code
*/
static int
xpath_rowid(sqlite3_vtab_cursor *cursor, sqlite3_int64 *rowidp)
{
XCSR *xc = (XCSR *) cursor;
XTAB *xt = (XTAB *) xc->cursor.pVtab;
XMOD *xm = (XMOD *) xt->xm;
int n = xt->idocs[xc->pos];
if (xm->docs[n].doc) {
*rowidp = (sqlite3_int64) (n + 1);
return SQLITE_OK;
}
return SQLITE_ERROR;
}
/**
* Insert/delete row into/from virtual table.
* @param vtab virtual table pointer
* @param argc number of arguments
* @param argv argument vector
* @param rowidp value buffer to receive rowid
* @result SQLite error code
*
* Examples:
*
* CREATE VIRTUAL TABLE X USING xpath();<br>
* INSERT INTO X(XML) VALUES('xml-string ...');<br>
* INSERT INTO X(PATH,OPTIONS) VALUES(<url>,0);<br>
* DELETE FROM X WHERE DOCID=<docid>;<br>
*
* Virtual table columns:
*
* DOCID - document identifier and ROWID<br>
* XML - XML string<br>
* PATH - pathname or URL<br>
* OPTIONS - parser options, see libxml's XML_PARSE_* defines<br>
* ENCODING - optional document encoding, default UTF-8<br>
* BASEURL - optional base URL when XML string given<br>
* XMLDUMP - output column, XML dump of document tree<br>
*
* All columns except DOCID are hidden. UPDATE on the virtual table
* is not supported. Default parser options are XML_PARSE_NOERROR,
* XML_PARSE_NOWARNING, and XML_PARSE_NONET.
*/
static int
xpath_update(sqlite3_vtab *vtab, int argc, sqlite3_value **argv,
sqlite3_int64 *rowidp)
{
int n = -1, rc = SQLITE_ERROR;
XTAB *xt = (XTAB *) vtab;
XMOD *xm = (XMOD *) xt->xm;
xmlDocPtr doc = 0, docToFree = 0;
if (argc == 1) {
/* DELETE */
int i, k = -1;
n = sqlite3_value_int(argv[0]);
for (i = 0; i < xt->ndoc; i++) {
if ((n - 1) == xt->idocs[i]) {
k = xt->idocs[i];
memmove(xt->idocs + i, xt->idocs + i + 1,
(xt->ndoc - (i + 1)) * sizeof (int));
xt->ndoc--;
break;
}
}
if ((k >= 0) && xm->mutex) {
n = k;
doc = xm->docs[n].doc;
}
rc = SQLITE_OK;
} else if ((argc > 1) && (sqlite3_value_type(argv[0]) == SQLITE_NULL)) {
/* INSERT */
int i, docid;
int opts = (XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET);
char *enc = 0;
if (sqlite3_value_type(argv[1]) != SQLITE_NULL) {
if (vtab->zErrMsg) {
sqlite3_free(vtab->zErrMsg);
}
vtab->zErrMsg = sqlite3_mprintf("ROWID must be NULL");
rc = SQLITE_CONSTRAINT;
goto done;
}
if (sqlite3_value_type(argv[2]) != SQLITE_NULL) {
docid = sqlite3_value_int(argv[2]);
if ((sqlite3_value_type(argv[3]) != SQLITE_NULL) ||
(sqlite3_value_type(argv[4]) != SQLITE_NULL)) {
if (vtab->zErrMsg) {
sqlite3_free(vtab->zErrMsg);
}
vtab->zErrMsg = sqlite3_mprintf("XML and PATH must be NULL");
rc = SQLITE_CONSTRAINT;
goto done;
}
sqlite3_mutex_enter(xm->mutex);
for (i = 0; xm->docs && (i < xt->ndoc); i++) {
if ((docid - 1) == xt->idocs[i]) {
sqlite3_mutex_leave(xm->mutex);
if (vtab->zErrMsg) {
sqlite3_free(vtab->zErrMsg);
}
vtab->zErrMsg = sqlite3_mprintf("constraint violation");
rc = SQLITE_CONSTRAINT;
goto done;
}
}
if ((docid > 0) && (docid <= xm->sdoc)) {
doc = xm->docs[docid - 1].doc;
if (doc) {
xm->docs[docid - 1].refcnt++;
}
}
sqlite3_mutex_leave(xm->mutex);
if (!doc) {
if (vtab->zErrMsg) {
sqlite3_free(vtab->zErrMsg);
}
vtab->zErrMsg = sqlite3_mprintf("invalid DOCID");
goto done;
}
goto havedoc;
}
if (((sqlite3_value_type(argv[3]) == SQLITE_NULL) &&
(sqlite3_value_type(argv[4]) == SQLITE_NULL)) ||
((sqlite3_value_type(argv[3]) != SQLITE_NULL) &&
(sqlite3_value_type(argv[4]) != SQLITE_NULL))) {
if (vtab->zErrMsg) {
sqlite3_free(vtab->zErrMsg);
}
vtab->zErrMsg = sqlite3_mprintf("specify one of XML or PATH");
rc = SQLITE_CONSTRAINT;
goto done;
}
if (sqlite3_value_type(argv[5]) != SQLITE_NULL) {
opts = sqlite3_value_int(argv[5]);
}
if (sqlite3_value_type(argv[6]) != SQLITE_NULL) {
enc = (char *) sqlite3_value_text(argv[6]);
}
if (sqlite3_value_type(argv[4]) != SQLITE_NULL) {
doc = xmlReadFile((char *) sqlite3_value_text(argv[4]), enc, opts);
} else {
char *url = 0;
if (sqlite3_value_type(argv[7]) != SQLITE_NULL) {
url = (char *) sqlite3_value_text(argv[7]);
}
doc = xmlReadMemory(sqlite3_value_blob(argv[3]),
sqlite3_value_bytes(argv[3]),
url ? url : "", enc, opts);
}
if (!doc) {
if (vtab->zErrMsg) {
sqlite3_free(vtab->zErrMsg);
}
vtab->zErrMsg = sqlite3_mprintf("read error");
goto done;
}
docToFree = doc;
havedoc:
if (xt->ndoc >= xt->sdoc) {
int *idocs = sqlite3_realloc(xt->idocs, xt->sdoc +
128 * sizeof (int));
if (!idocs) {
goto nomem;
}
xt->idocs = idocs;
xt->sdoc += 128;
}
if (!xm->mutex) {
goto nomem;
}
sqlite3_mutex_enter(xm->mutex);
if (xm->ndoc >= xt->sdoc) {
XDOC *docs = sqlite3_realloc(xm->docs, xt->sdoc +
128 * sizeof (XDOC));
if (!docs) {
sqlite3_mutex_leave(xm->mutex);
goto nomem;
}
xm->docs = docs;
docs += xt->sdoc;
memset(docs, 0, 128 * sizeof (XDOC));
xt->sdoc += 128;
}
for (i = 0; i < xm->sdoc; i++) {
if (!xm->docs[i].doc) {
xm->docs[i].doc = doc;
xm->docs[i].refcnt = 1;
xm->ndoc++;
xt->idocs[xt->ndoc++] = i;
*rowidp = (sqlite3_int64) (i + 1);
doc = docToFree = 0;
rc = SQLITE_OK;
break;
}
}
sqlite3_mutex_leave(xm->mutex);
} else {
/* UPDATE */
if (vtab->zErrMsg) {
sqlite3_free(vtab->zErrMsg);
}
vtab->zErrMsg = sqlite3_mprintf("UPDATE not supported");
}
done:
if (docToFree) {
xmlFreeDoc(docToFree);
} else if (doc && (n >= 0)) {
sqlite3_mutex_enter(xm->mutex);
xm->docs[n].refcnt -= 1;
if (xm->docs[n].refcnt <= 0) {
xm->docs[n].doc = 0;
xm->docs[n].refcnt = 0;
xm->ndoc--;
xmlFreeDoc(doc);
}
sqlite3_mutex_leave(xm->mutex);
}
return rc;
nomem:
if (vtab->zErrMsg) {
sqlite3_free(vtab->zErrMsg);
}
vtab->zErrMsg = sqlite3_mprintf("out of memory");
rc = SQLITE_NOMEM;
goto done;
}
/**
* Common XPath select function for virtual table.
* @param ctx SQLite function context
* @param conv conversion (0=string, 1=boolean, 2=number)
* @param argc number of arguments
* @param argv argument vector
*
* Examples:
*
* CREATE VIRTUAL TABLE X USING xpath();<br>
* INSERT INTO X(XML) VALUES('xml-string ...');<br>
* SELECT xpath_string(docid, '//book/title') FROM X;<br>
* SELECT xpath_number(docid, '//book/price') FROM X;<br>
*
* The RHS of the xpath_* functions should be a constant string.
*/
static void
xpath_vfunc_common(sqlite3_context *ctx, int conv, int argc,
sqlite3_value **argv)
{
XTAB *xt = (XTAB *) sqlite3_user_data(ctx);
XMOD *xm = xt->xm;
XCSR *xc = xt->xc;
XEXP *xp;
xmlXPathContextPtr pctx = 0;
xmlXPathObjectPtr pobj = 0;
int n;
char *p;
if ((argc < 2) || !sqlite3_value_text(argv[1])) {
sqlite3_result_error(ctx, "wrong arguments", -1);
goto done;
}
if (!xc) {
sqlite3_result_error(ctx, "not in virtual table context", -1);
goto done;
}
if ((xc->pos < 0) || (xc->pos >= xt->ndoc)) {
sqlite3_result_error(ctx, "cursor out of bounds", -1);
goto done;
}
n = xt->idocs[xc->pos];
if (!xm->docs[n].doc) {
sqlite3_result_error(ctx, "no docid", -1);
goto done;
}
p = (char *) sqlite3_value_text(argv[1]);
if (!p || !p[0]) {
sqlite3_result_error(ctx, "no or empty XPath expression", -1);
goto done;
}
xp = xc->first;
while (xp) {
if (!strcmp(p, xp->expr)) {
break;
}
xp = xp->next;
}
if (!xp) {
xp = sqlite3_malloc(sizeof (XEXP) + strlen(p));
if (!xp) {
sqlite3_result_error(ctx, "out of memory", -1);
goto done;
}
xp->next = xp->prev = 0;
strcpy(xp->expr, p);
pctx = xmlXPathNewContext(xm->docs[n].doc);
if (!pctx) {
sqlite3_free(xp);
sqlite3_result_error(ctx, "out of memory", -1);
goto done;
}
pobj = xmlXPathEvalExpression((xmlChar *) xp->expr, pctx);
if (!pobj) {
sqlite3_free(xp);
sqlite3_result_error(ctx, "bad XPath expression", -1);
goto done;
}
xp->doc = xm->docs[n].doc;
xp->pctx = pctx;
xp->pobj = pobj;
xp->parent = 0;
xp->pos = -1;
xp->conv = conv;
pctx = 0;
pobj = 0;
xc->nexpr++;
if (xc->first) {
xc->last->next = xp;
xp->prev = xc->last;
xc->last = xp;
} else {
xc->first = xc->last = xp;
}
} else if (xm->docs[n].doc != xp->doc) {
if (xp->pobj) {
xmlXPathFreeObject(xp->pobj);
xp->pobj = 0;
}
if (xp->pctx) {
xmlXPathFreeContext(xp->pctx);
xp->pctx = 0;
}
xp->doc = xm->docs[n].doc;
xp->parent = 0;
xp->pos = -1;
if (xp->doc) {
pctx = xmlXPathNewContext(xm->docs[n].doc);
if (!pctx) {
sqlite3_result_error(ctx, "out of memory", -1);
goto done;
}
pobj = xmlXPathEvalExpression((xmlChar *) xp->expr, pctx);
if (!pobj) {
sqlite3_result_error(ctx, "bad XPath expression", -1);
goto done;
}
xp->pctx = pctx;
xp->pobj = pobj;
pctx = 0;
pobj = 0;
}
}
if (xp->pos < 0) {
xp->pos = 0;
}
if (!xp->pobj) {
xp->parent = 0;
sqlite3_result_null(ctx);
goto done;
}
if ((xp->pobj->type == XPATH_NODESET) && xp->pobj->nodesetval) {
if ((xp->pos < 0) || (xp->pos >= xp->pobj->nodesetval->nodeNr)) {
xp->parent = 0;
sqlite3_result_null(ctx);
} else {
xmlNodePtr node = xp->pobj->nodesetval->nodeTab[xp->pos];
xmlBufferPtr buf = 0;
xp->parent = node->parent;
if (node) {
switch (xp->conv) {
case 1:
p = (char *) xmlXPathCastNodeToString(node);
n = xmlXPathCastStringToBoolean((xmlChar *) p);
sqlite3_result_int(ctx, n);
if (p) {
xmlFree(p);
}
break;
case 2:
sqlite3_result_double(ctx,
xmlXPathCastNodeToNumber(node));
break;
case 3:
buf = xmlBufferCreate();
if (!buf) {
sqlite3_result_error(ctx, "out of memory", -1);
goto done;
}
xmlNodeDump(buf, xp->doc, node, 0, 0);
sqlite3_result_text(ctx, (char *) xmlBufferContent(buf),
xmlBufferLength(buf),
SQLITE_TRANSIENT);
xmlBufferFree(buf);
break;
default:
p = (char *) xmlXPathCastNodeToString(node);
sqlite3_result_text(ctx, p, -1, SQLITE_TRANSIENT);
if (p) {
xmlFree(p);
}
break;
}
} else {
sqlite3_result_null(ctx);
}
}
} else {
xp->parent = 0;
switch (xp->conv) {
case 1:
sqlite3_result_int(ctx, xmlXPathCastToBoolean(xp->pobj));
break;
case 2:
sqlite3_result_double(ctx, xmlXPathCastToNumber(xp->pobj));
break;
default:
p = (char *) xmlXPathCastToString(xp->pobj);
sqlite3_result_text(ctx, p, -1, SQLITE_TRANSIENT);
if (p) {
xmlFree(p);
}
break;
}
}
done:
if (pobj) {
xmlXPathFreeObject(pobj);
}
if (pctx) {
xmlXPathFreeContext(pctx);
}
}
/**
* XPath select function returning string value from virtual table.
* @param ctx SQLite function context
* @param argc number of arguments
* @param argv argument vector
*/
static void
xpath_vfunc_string(sqlite3_context *ctx, int argc, sqlite3_value **argv)
{
return xpath_vfunc_common(ctx, 0, argc, argv);
}
/**
* XPath select function returning boolean value from virtual table.
* @param ctx SQLite function context
* @param argc number of arguments
* @param argv argument vector
*/
static void
xpath_vfunc_boolean(sqlite3_context *ctx, int argc, sqlite3_value **argv)
{
return xpath_vfunc_common(ctx, 1, argc, argv);
}
/**
* XPath select function returning number from virtual table.
* @param ctx SQLite function context
* @param argc number of arguments
* @param argv argument vector
*/
static void
xpath_vfunc_number(sqlite3_context *ctx, int argc, sqlite3_value **argv)
{
return xpath_vfunc_common(ctx, 2, argc, argv);
}
/**
* XPath select function returning XML from virtual table.
* @param ctx SQLite function context
* @param argc number of arguments
* @param argv argument vector
*/
static void
xpath_vfunc_xml(sqlite3_context *ctx, int argc, sqlite3_value **argv)
{
return xpath_vfunc_common(ctx, 3, argc, argv);
}
/**
* Find overloaded function on virtual table.
* @param vtab virtual table
* @param nargs number arguments
* @param name function name
* @param pfunc pointer to function (value return)
* @param parg pointer to function's argument (value return)
* @result 0 or 1
*/
static int
xpath_findfunc(sqlite3_vtab *vtab, int nargs, const char *name,
void (**pfunc)(sqlite3_context *, int, sqlite3_value **),
void **parg)
{
if (nargs != 2) {
return 0;
}
if (!strcmp(name, "xpath_string")) {
*pfunc = xpath_vfunc_string;
*parg = vtab;
return 1;
}
if (!strcmp(name, "xpath_boolean")) {
*pfunc = xpath_vfunc_boolean;
*parg = vtab;
return 1;
}
if (!strcmp(name, "xpath_number")) {
*pfunc = xpath_vfunc_number;
*parg = vtab;
return 1;
}
if (!strcmp(name, "xpath_xml")) {
*pfunc = xpath_vfunc_xml;
*parg = vtab;
return 1;
}
return 0;
}
#if (SQLITE_VERSION_NUMBER > 3004000)
/**
* Rename virtual table.
* @param newname new name for table
* @result SQLite error code
*/
static int
xpath_rename(sqlite3_vtab *vtab, const char *newname)
{
return SQLITE_OK;
}
#endif
/**
* SQLite module descriptor.
*/
static sqlite3_module xpath_mod = {
1, /* iVersion */
xpath_create, /* xCreate */
xpath_connect, /* xConnect */
xpath_bestindex, /* xBestIndex */
xpath_disconnect, /* xDisconnect */
xpath_destroy, /* xDestroy */
xpath_open, /* xOpen */
xpath_close, /* xClose */
xpath_filter, /* xFilter */
xpath_next, /* xNext */
xpath_eof, /* xEof */
xpath_column, /* xColumn */
xpath_rowid, /* xRowid */
xpath_update, /* xUpdate */
0, /* xBegin */
0, /* xSync */
0, /* xCommit */
0, /* xRollback */
xpath_findfunc, /* xFindFunction */
#if (SQLITE_VERSION_NUMBER > 3004000)
xpath_rename, /* xRename */
#endif
};
/**
* Common XPath select function.
* @param ctx SQLite function context
* @param conv conversion (0=string, 1=boolean, 2=number)
* @param argc number of arguments
* @param argv argument vector
*
* Examples:
*
* SELECT xpath_string(<docid>, '//book/title');<br>
* SELECT xpath_number(<xml-string>, '//book/price',
* <options;>, <encoding>,
* <base-url>);<br>
*
* The <docid> argument is the DOCID value of a row
* in a virtual table. Otherwise a string containing an
* XML document is expected. The optional arguments are<br>
* <options> - parser options, see libxml's XML_PARSE_* defines<br>
* <encoding> - encoding of the XML document<br>
* <base-url> - base URL of the XML document<br>
*/
static void
xpath_func_common(sqlite3_context *ctx, int conv,
int argc, sqlite3_value **argv)
{
xmlDocPtr doc = 0, docToFree = 0;
xmlXPathContextPtr pctx = 0;
xmlXPathObjectPtr pobj = 0;
XMOD *xm = (XMOD *) sqlite3_user_data(ctx);
int index = 0;
char *p;
if (argc < 2) {
sqlite3_result_null(ctx);
goto done;
}
if (sqlite3_value_type(argv[0]) == SQLITE_INTEGER) {
index = sqlite3_value_int(argv[0]);
if (!xm->mutex) {
sqlite3_result_error(ctx, "init error", -1);
goto done;
}
sqlite3_mutex_enter(xm->mutex);
if ((index <= 0) || (index > xm->sdoc) || !xm->docs[index - 1].doc) {
sqlite3_mutex_leave(xm->mutex);
sqlite3_result_error(ctx, "invalid DOCID", -1);
goto done;
}
doc = xm->docs[index - 1].doc;
xm->docs[index - 1].refcnt += 1;
sqlite3_mutex_leave(xm->mutex);
} else {
int opts = (XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET);
char *enc = 0, *url = 0;
p = (char *) sqlite3_value_blob(argv[0]);
if (!p) {
sqlite3_result_null(ctx);
return;
}
if ((argc > 2) && (sqlite3_value_type(argv[2]) != SQLITE_NULL)) {
opts = sqlite3_value_int(argv[2]);
}
if ((argc > 3) && (sqlite3_value_type(argv[3]) != SQLITE_NULL)) {
enc = (char *) sqlite3_value_text(argv[3]);
}
if ((argc > 4) && (sqlite3_value_type(argv[4]) != SQLITE_NULL)) {
url = (char *) sqlite3_value_text(argv[4]);
}
doc = xmlReadMemory(p, sqlite3_value_bytes(argv[0]),
url ? url : "", enc, opts);
docToFree = doc;
if (!doc) {
sqlite3_result_error(ctx, "read error", -1);
goto done;
}
}
p = (char *) sqlite3_value_text(argv[1]);
if (!p) {
sqlite3_result_null(ctx);
goto done;
}
pctx = xmlXPathNewContext(doc);
if (!pctx) {
sqlite3_result_error(ctx, "out of memory", -1);
goto done;
}
pobj = xmlXPathEvalExpression((xmlChar *) p, pctx);
if (!pobj) {
sqlite3_result_error(ctx, "bad XPath expression", -1);
goto done;
}
switch (conv) {
case 1:
sqlite3_result_int(ctx, xmlXPathCastToBoolean(pobj));
break;
case 2:
sqlite3_result_double(ctx, xmlXPathCastToNumber(pobj));
break;
case 3:
if ((pobj->type == XPATH_NODESET) && pobj->nodesetval &&
(pobj->nodesetval->nodeNr)) {
xmlNodePtr node = pobj->nodesetval->nodeTab[0];
xmlBufferPtr buf = 0;
buf = xmlBufferCreate();
if (!buf) {
sqlite3_result_error(ctx, "out of memory", -1);
goto done;
}
xmlNodeDump(buf, doc, node, 0, 0);
sqlite3_result_text(ctx, (char *) xmlBufferContent(buf),
xmlBufferLength(buf), SQLITE_TRANSIENT);
xmlBufferFree(buf);
} else {
sqlite3_result_null(ctx);
}
break;
default:
p = (char *) xmlXPathCastToString(pobj);
sqlite3_result_text(ctx, p, -1, SQLITE_TRANSIENT);
if (p) {
xmlFree(p);
}
break;
}
done:
if (pobj) {
xmlXPathFreeObject(pobj);
}
if (pctx) {
xmlXPathFreeContext(pctx);
}
if (docToFree) {
xmlFreeDoc(docToFree);
} else if (doc) {
if (xm->mutex) {
sqlite3_mutex_enter(xm->mutex);
if (xm->docs && index) {
xm->docs[index - 1].refcnt -= 1;
if (xm->docs[index - 1].refcnt <= 0) {
docToFree = doc;
xm->docs[index - 1].refcnt = 0;
xm->docs[index - 1].doc = 0;
}
}
sqlite3_mutex_leave(xm->mutex);
if (docToFree) {
xmlFreeDoc(docToFree);
}
}
}
}
/**
* XPath select function returning string value.
* @param ctx SQLite function context
* @param argc number of arguments
* @param argv argument vector
*/
static void
xpath_func_string(sqlite3_context *ctx, int argc, sqlite3_value **argv)
{
xpath_func_common(ctx, 0, argc, argv);
}
/**
* XPath select function returning boolean value.
* @param ctx SQLite function context
* @param argc number of arguments
* @param argv argument vector
*/
static void
xpath_func_boolean(sqlite3_context *ctx, int argc, sqlite3_value **argv)
{
xpath_func_common(ctx, 1, argc, argv);
}
/**
* XPath select function returning number.
* @param ctx SQLite function context
* @param argc number of arguments
* @param argv argument vector
*/
static void
xpath_func_number(sqlite3_context *ctx, int argc, sqlite3_value **argv)
{
xpath_func_common(ctx, 2, argc, argv);
}
/**
* XPath select function returning XML.
* @param ctx SQLite function context
* @param argc number of arguments
* @param argv argument vector
*/
static void
xpath_func_xml(sqlite3_context *ctx, int argc, sqlite3_value **argv)
{
xpath_func_common(ctx, 3, argc, argv);
}
/**
* Function to dump XML document.
* @param ctx SQLite function context
* @param argc number of arguments
* @param argv argument vector
*
* Examples:
*
* SELECT xml_dump(<docid>, <encoding> <fmt>)<br>
*
* The <docid> argument is the DOCID value of a row
* in a virtual table. The <encoding> argument is
* the optional encoding (default UTF-8). The <fmt>
* argument is the optional formatting flag for the
* xmlDocDumpFormatMemoryEnc() libxml2 function.
*/
static void
xpath_func_dump(sqlite3_context *ctx, int argc, sqlite3_value **argv)
{
XMOD *xm = (XMOD *) sqlite3_user_data(ctx);
int index = 0, dump_len = 0, fmt = 1;
xmlChar *dump = 0;
char *enc = "utf-8";
if (argc < 1) {
sqlite3_result_null(ctx);
return;
}
index = sqlite3_value_int(argv[0]);
if (argc > 1) {
enc = (char *) sqlite3_value_text(argv[1]);
if (!enc) {
enc = "utf-8";
}
}
if (argc > 2) {
fmt = sqlite3_value_int(argv[2]);
}
if (!xm->mutex) {
sqlite3_result_error(ctx, "init error", -1);
return;
}
sqlite3_mutex_enter(xm->mutex);
if ((index <= 0) || (index > xm->sdoc) || !xm->docs[index - 1].doc) {
sqlite3_mutex_leave(xm->mutex);
sqlite3_result_error(ctx, "invalid DOCID", -1);
return;
}
xmlDocDumpFormatMemoryEnc(xm->docs[index - 1].doc, &dump, &dump_len,
enc, fmt);
if (dump) {
sqlite3_result_text(ctx, (char *) dump, dump_len, SQLITE_TRANSIENT);
xmlFree(dump);
}
sqlite3_mutex_leave(xm->mutex);
}
#ifdef WITH_XSLT
/**
* Function to transform XML document using XSLT stylesheet.
* @param ctx SQLite function context
* @param argc number of arguments
* @param argv argument vector
*
* Examples:
*
* SELECT xslt_transform(<docid>, <stylesheet>, ...)<br>
* SELECT xslt_transform(<xml-string>, <stylesheet>,
* <options;>, <encoding>,
* <base-url>, ...);<br>
*
* The <docid> argument is the DOCID value of a row
* in a virtual table. <stylesheet> is the stylesheet
* to apply on that document. When the transformation succeeded,
* the transformed document replaces the original document.<br>
* In the second form an XML string is transformed with the
* same parameters as in the xpath_string() SQLite function.<br>
* The ellipsis optional arguments are the string parameters for
* the XSLT transformation.
*/
static void
xpath_func_transform(sqlite3_context *ctx, int argc, sqlite3_value **argv)
{
xmlDocPtr doc = 0, docToFree = 0, res = 0;
xsltStylesheetPtr cur = 0;
XMOD *xm = (XMOD *) sqlite3_user_data(ctx);
int index = 0, nparams = 0, param0, i;
char *p;
const char **params = 0;
if (argc < 2) {
sqlite3_result_null(ctx);
goto done;
}
if (sqlite3_value_type(argv[0]) == SQLITE_INTEGER) {
index = sqlite3_value_int(argv[0]);
if (!xm->mutex) {
sqlite3_result_error(ctx, "init error", -1);
goto done;
}
sqlite3_mutex_enter(xm->mutex);
if ((index <= 0) || (index > xm->sdoc) || !xm->docs[index - 1].doc) {
sqlite3_mutex_leave(xm->mutex);
sqlite3_result_error(ctx, "invalid DOCID", -1);
goto done;
}
doc = xm->docs[index - 1].doc;
xm->docs[index - 1].refcnt += 1;
sqlite3_mutex_leave(xm->mutex);
param0 = 2;
nparams = argc - 2;
} else {
int opts = (XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET);
char *enc = 0, *url = 0;
p = (char *) sqlite3_value_blob(argv[0]);
if (!p) {
sqlite3_result_null(ctx);
return;
}
if ((argc > 2) && (sqlite3_value_type(argv[2]) != SQLITE_NULL)) {
opts = sqlite3_value_int(argv[2]);
}
if ((argc > 3) && (sqlite3_value_type(argv[3]) != SQLITE_NULL)) {
enc = (char *) sqlite3_value_text(argv[3]);
}
if ((argc > 4) && (sqlite3_value_type(argv[4]) != SQLITE_NULL)) {
url = (char *) sqlite3_value_text(argv[4]);
}
doc = xmlReadMemory(p, sqlite3_value_bytes(argv[0]),
url ? url : "", enc, opts);
docToFree = doc;
if (!doc) {
sqlite3_result_error(ctx, "read error", -1);
goto done;
}
param0 = 5;
nparams = argc - 5;
}
p = (char *) sqlite3_value_text(argv[1]);
if (!p) {
sqlite3_result_null(ctx);
goto done;
}
cur = xsltParseStylesheetFile((xmlChar *) p);
if (!cur) {
sqlite3_result_error(ctx, "read error on stylesheet", -1);
goto done;
}
if (nparams <= 0) {
nparams = 1;
} else {
nparams++;
}
params = sqlite3_malloc(nparams * sizeof (char *));
if (!params) {
sqlite3_result_error(ctx, "out of memory", -1);
goto done;
}
for (i = 0; i < (argc - param0); i++) {
params[i] = (const char *) sqlite3_value_text(argv[i + param0]);
if (!params[i]) {
params[i] = "";
}
}
params[i] = 0;
res = xsltApplyStylesheet(cur, doc, params);
if (!res) {
sqlite3_result_error(ctx, "transformation failed", -1);
goto done;
}
if (docToFree) {
xmlChar *str = 0;
xmlFreeDoc(docToFree);
docToFree = res;
i = 0;
xsltSaveResultToString(&str, &i, res, cur);
if (str) {
sqlite3_result_text(ctx, (char *) str, i, SQLITE_TRANSIENT);
xmlFree(str);
} else {
sqlite3_result_null(ctx);
}
}
done:
if (params) {
sqlite3_free(params);
}
if (cur) {
xsltFreeStylesheet(cur);
}
if (docToFree) {
xmlFreeDoc(docToFree);
} else if (doc) {
if (xm->mutex) {
sqlite3_mutex_enter(xm->mutex);
if (xm->docs && index) {
docToFree = doc;
xm->docs[index - 1].doc = 0;
xmlFreeDoc(docToFree);
docToFree = 0;
xm->docs[index - 1].refcnt -= 1;
xm->docs[index - 1].doc = res;
if (xm->docs[index - 1].refcnt <= 0) {
docToFree = res;
xm->docs[index - 1].refcnt = 0;
xm->docs[index - 1].doc = 0;
}
}
sqlite3_mutex_leave(xm->mutex);
if (docToFree) {
xmlFreeDoc(docToFree);
}
}
}
}
#endif
/**
* Module finalizer.
* @param aux pointer to module data
* @result SQLite error code
*/
static void
xpath_fini(void *aux)
{
XMOD *xm = (XMOD *) aux;
XDOC *docs;
int i, n, cleanup = 0;
sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
if (!mutex) {
return;
}
sqlite3_mutex_enter(mutex);
if (initialized) {
xm->refcnt--;
if (xm->refcnt <= 0) {
xmod = 0;
initialized = 0;
cleanup = 1;
}
} else {
cleanup = 1;
}
sqlite3_mutex_leave(mutex);
if (cleanup) {
sqlite3_mutex_enter(xm->mutex);
mutex = xm->mutex;
xm->mutex = 0;
docs = xm->docs;
n = xm->ndoc;
xm->docs = 0;
xm->sdoc = xm->ndoc = 0;
sqlite3_mutex_leave(mutex);
sqlite3_mutex_free(mutex);
for (i = 0; i < n; i++) {
if (docs->refcnt <= 0) {
xmlFreeDoc(docs->doc);
docs->doc = 0;
}
}
sqlite3_free(docs);
sqlite3_free(xm);
}
}
/**
* Module initializer creating SQLite module and functions.
* @param db database pointer
* @result SQLite error code
*/
#ifndef STANDALONE
static
#endif
int
xpath_init(sqlite3 *db)
{
XMOD *xm;
int rc;
sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
if (!mutex) {
return SQLITE_NOMEM;
}
sqlite3_mutex_enter(mutex);
if (!initialized) {
xm = sqlite3_malloc(sizeof (XMOD));
if (!xm) {
sqlite3_mutex_leave(mutex);
return SQLITE_NOMEM;
}
xm->refcnt = 1;
xm->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
if (!xm->mutex) {
sqlite3_mutex_leave(mutex);
sqlite3_free(xm);
return SQLITE_NOMEM;
}
xm->sdoc = 128;
xm->ndoc = 0;
xm->docs = sqlite3_malloc(xm->sdoc * sizeof (XDOC));
if (!xm->docs) {
sqlite3_mutex_leave(mutex);
sqlite3_mutex_free(xm->mutex);
sqlite3_free(xm);
return SQLITE_NOMEM;
}
memset(xm->docs, 0, xm->sdoc * sizeof (XDOC));
xmod = xm;
initialized = 1;
} else {
xm = xmod;
xm->refcnt++;
}
sqlite3_mutex_leave(mutex);
sqlite3_create_function(db, "xpath_string", -1, SQLITE_UTF8,
(void *) xm, xpath_func_string, 0, 0);
sqlite3_create_function(db, "xpath_boolean", -1, SQLITE_UTF8,
(void *) xm, xpath_func_boolean, 0, 0);
sqlite3_create_function(db, "xpath_number", -1, SQLITE_UTF8,
(void *) xm, xpath_func_number, 0, 0);
sqlite3_create_function(db, "xpath_xml", -1, SQLITE_UTF8,
(void *) xm, xpath_func_xml, 0, 0);
sqlite3_create_function(db, "xml_dump", -1, SQLITE_UTF8,
(void *) xm, xpath_func_dump, 0, 0);
#ifdef WITH_XSLT
sqlite3_create_function(db, "xslt_transform", -1, SQLITE_UTF8,
(void *) xm, xpath_func_transform, 0, 0);
#endif
rc = sqlite3_create_module_v2(db, "xpath", &xpath_mod,
(void *) xm, xpath_fini);
if (rc != SQLITE_OK) {
sqlite3_create_function(db, "xpath_string", -1, SQLITE_UTF8,
(void *) xm, 0, 0, 0);
sqlite3_create_function(db, "xpath_boolean", -1, SQLITE_UTF8,
(void *) xm, 0, 0, 0);
sqlite3_create_function(db, "xpath_number", -1, SQLITE_UTF8,
(void *) xm, 0, 0, 0);
sqlite3_create_function(db, "xpath_xml", -1, SQLITE_UTF8,
(void *) xm, 0, 0, 0);
sqlite3_create_function(db, "xml_dump", -1, SQLITE_UTF8,
(void *) xm, 0, 0, 0);
#ifdef WITH_XSLT
sqlite3_create_function(db, "xslt_transform", -1, SQLITE_UTF8,
(void *) xm, 0, 0, 0);
#endif
xpath_fini(xm);
}
return rc;
}
#ifndef STANDALONE
/**
* Initializer for SQLite extension load mechanism.
* @param db SQLite database pointer
* @param errmsg pointer receiving error message
* @param api SQLite API routines
* @result SQLite error code
*/
int
sqlite3_extension_init(sqlite3 *db, char **errmsg,
const sqlite3_api_routines *api)
{
SQLITE_EXTENSION_INIT2(api);
return xpath_init(db);
}
#endif
|