SQLite extension module to select parts of XML documents using libxml2 XPath and SQLite's virtual table mechanism. More...
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <sqlite3ext.h>
Go to the source code of this file.
Data Structures | |
struct | XDOC |
Structure to cache XML document. More... | |
struct | XMOD |
Structure holding per module/database data. More... | |
struct | XTAB |
Structure to describe virtual table. More... | |
struct | XEXP |
Structure to describe XPath expression. More... | |
struct | XCSR |
Structure to describe virtual table cursor. More... | |
Typedefs | |
typedef struct XDOC | XDOC |
typedef struct XMOD | XMOD |
typedef struct XTAB | XTAB |
typedef struct XEXP | XEXP |
typedef struct XCSR | XCSR |
Functions | |
static int | xpath_connect (sqlite3 *db, void *aux, int argc, const char *const *argv, sqlite3_vtab **vtabp, char **errp) |
Connect to virtual table. | |
static int | xpath_create (sqlite3 *db, void *aux, int argc, const char *const *argv, sqlite3_vtab **vtabp, char **errp) |
Create virtual table. | |
static int | xpath_disconnect (sqlite3_vtab *vtab) |
Disconnect virtual table. | |
static int | xpath_destroy (sqlite3_vtab *vtab) |
Destroy virtual table. | |
static int | xpath_bestindex (sqlite3_vtab *vtab, sqlite3_index_info *info) |
Determines information for filter function according to constraints. | |
static int | xpath_open (sqlite3_vtab *vtab, sqlite3_vtab_cursor **cursorp) |
Open virtual table and return cursor. | |
static int | xpath_close (sqlite3_vtab_cursor *cursor) |
Close virtual table cursor. | |
static int | xpath_next (sqlite3_vtab_cursor *cursor) |
Retrieve next row from virtual table cursor. | |
static int | xpath_filter (sqlite3_vtab_cursor *cursor, int idxNum, const char *idxStr, int argc, sqlite3_value **argv) |
Filter function for virtual table. | |
static int | xpath_eof (sqlite3_vtab_cursor *cursor) |
Return end of table state of virtual table cursor. | |
static int | xpath_column (sqlite3_vtab_cursor *cursor, sqlite3_context *ctx, int n) |
Return column data of virtual table. | |
static int | xpath_rowid (sqlite3_vtab_cursor *cursor, sqlite3_int64 *rowidp) |
Return current rowid of virtual table cursor. | |
static int | xpath_update (sqlite3_vtab *vtab, int argc, sqlite3_value **argv, sqlite3_int64 *rowidp) |
Insert/delete row into/from virtual table. | |
static void | xpath_vfunc_common (sqlite3_context *ctx, int conv, int argc, sqlite3_value **argv) |
Common XPath select function for virtual table. | |
static void | xpath_vfunc_string (sqlite3_context *ctx, int argc, sqlite3_value **argv) |
XPath select function returning string value from virtual table. | |
static void | xpath_vfunc_boolean (sqlite3_context *ctx, int argc, sqlite3_value **argv) |
XPath select function returning boolean value from virtual table. | |
static void | xpath_vfunc_number (sqlite3_context *ctx, int argc, sqlite3_value **argv) |
XPath select function returning number from virtual table. | |
static void | xpath_vfunc_xml (sqlite3_context *ctx, int argc, sqlite3_value **argv) |
XPath select function returning XML from virtual table. | |
static int | xpath_findfunc (sqlite3_vtab *vtab, int nargs, const char *name, void(**pfunc)(sqlite3_context *, int, sqlite3_value **), void **parg) |
Find overloaded function on virtual table. | |
static void | xpath_func_common (sqlite3_context *ctx, int conv, int argc, sqlite3_value **argv) |
Common XPath select function. | |
static void | xpath_func_string (sqlite3_context *ctx, int argc, sqlite3_value **argv) |
XPath select function returning string value. | |
static void | xpath_func_boolean (sqlite3_context *ctx, int argc, sqlite3_value **argv) |
XPath select function returning boolean value. | |
static void | xpath_func_number (sqlite3_context *ctx, int argc, sqlite3_value **argv) |
XPath select function returning number. | |
static void | xpath_func_xml (sqlite3_context *ctx, int argc, sqlite3_value **argv) |
XPath select function returning XML. | |
static void | xpath_func_dump (sqlite3_context *ctx, int argc, sqlite3_value **argv) |
Function to dump XML document. | |
static void | xpath_fini (void *aux) |
Module finalizer. | |
static int | xpath_init (sqlite3 *db) |
Module initializer creating SQLite module and functions. | |
int | sqlite3_extension_init (sqlite3 *db, char **errmsg, const sqlite3_api_routines *api) |
Initializer for SQLite extension load mechanism. | |
Variables | |
static int | initialized = 0 |
static XMOD * | xmod = 0 |
static sqlite3_module | xpath_mod |
SQLite module descriptor. | |
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.
Definition in file xpath.c.
typedef struct XCSR XCSR |
typedef struct XDOC XDOC |
typedef struct XEXP XEXP |
typedef struct XMOD XMOD |
typedef struct XTAB XTAB |
int sqlite3_extension_init | ( | sqlite3 * | db, |
char ** | errmsg, | ||
const sqlite3_api_routines * | api ) |
Initializer for SQLite extension load mechanism.
db | SQLite database pointer |
errmsg | pointer receiving error message |
api | SQLite API routines |
Definition at line 1658 of file xpath.c.
References xpath_init().
|
static |
|
static |
Close virtual table cursor.
cursor | cursor pointer |
Definition at line 290 of file xpath.c.
References XCSR::cursor, XCSR::first, XEXP::next, XEXP::pctx, XEXP::pobj, and XTAB::xc.
|
static |
Return column data of virtual table.
cursor | virtual table cursor |
ctx | SQLite function context |
n | column index |
Definition at line 461 of file xpath.c.
References XCSR::cursor, XDOC::doc, XMOD::docs, XTAB::idocs, XTAB::ndoc, XCSR::pos, and XTAB::xm.
|
static |
Connect to virtual table.
db | SQLite database pointer |
aux | user specific pointer |
argc | argument count |
argv | argument vector |
vtabp | pointer receiving virtual table pointer |
errp | pointer receiving error messag |
Argument vector contains:
argv[0] - module name
argv[1] - database name
argv[2] - table name (virtual table)
Definition at line 135 of file xpath.c.
References XTAB::db, XTAB::idocs, XTAB::ndoc, nomem(), XTAB::sdoc, XTAB::vtab, XTAB::xc, and XTAB::xm.
Referenced by xpath_create().
|
static |
Create virtual table.
db | SQLite database pointer |
aux | user specific pointer |
argc | argument count |
argv | argument vector |
vtabp | pointer receiving virtual table pointer |
errp | pointer receiving error messag |
Definition at line 191 of file xpath.c.
References xpath_connect().
|
static |
Destroy virtual table.
vtab | virtual table pointer |
Definition at line 242 of file xpath.c.
References xpath_disconnect().
|
static |
Disconnect virtual table.
vtab | virtual table pointer |
Definition at line 205 of file xpath.c.
References XDOC::doc, XMOD::docs, XTAB::idocs, XMOD::mutex, XMOD::ndoc, XTAB::ndoc, XDOC::refcnt, and XTAB::xm.
Referenced by xpath_destroy().
|
static |
Return end of table state of virtual table cursor.
cursor | virtual table cursor |
Definition at line 444 of file xpath.c.
References XCSR::cursor, XTAB::ndoc, and XCSR::pos.
|
static |
Filter function for virtual table.
cursor | virtual table cursor |
idxNum | not used |
idxStr | nod used |
argc | number arguments (not used) |
argv | argument (nothing or RHS of filter expression, not used) |
Definition at line 426 of file xpath.c.
References XCSR::cursor, XCSR::pos, XTAB::xc, and xpath_next().
|
static |
Find overloaded function on virtual table.
vtab | virtual table |
nargs | number arguments |
name | function name |
pfunc | pointer to function (value return) |
parg | pointer to function's argument (value return) |
Definition at line 1008 of file xpath.c.
References xpath_vfunc_boolean(), xpath_vfunc_number(), xpath_vfunc_string(), and xpath_vfunc_xml().
|
static |
Module finalizer.
aux | pointer to module data |
Definition at line 1518 of file xpath.c.
References XDOC::doc, XMOD::docs, initialized, XMOD::mutex, XMOD::ndoc, XDOC::refcnt, XMOD::refcnt, XMOD::sdoc, and xmod.
Referenced by xpath_init().
|
static |
XPath select function returning boolean value.
ctx | SQLite function context |
argc | number of arguments |
argv | argument vector |
Definition at line 1259 of file xpath.c.
References xpath_func_common().
Referenced by xpath_init().
|
static |
Common XPath select function.
ctx | SQLite function context |
conv | conversion (0=string, 1=boolean, 2=number) |
argc | number of arguments |
argv | argument vector |
Examples:
SELECT xpath_string(<docid>, '//book/title');
SELECT xpath_number(<xml-string>, '//book/price', <options;>, <encoding>, <base-url>);
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
<options> - parser options, see libxml's XML_PARSE_* defines
<encoding> - encoding of the XML document
<base-url> - base URL of the XML document
Definition at line 1106 of file xpath.c.
References XDOC::doc, XMOD::docs, XMOD::mutex, XDOC::refcnt, and XMOD::sdoc.
Referenced by xpath_func_boolean(), xpath_func_number(), xpath_func_string(), and xpath_func_xml().
|
static |
Function to dump XML document.
ctx | SQLite function context |
argc | number of arguments |
argv | argument vector |
Examples:
SELECT xml_dump(<docid>, <encoding> <fmt>)
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.
Definition at line 1308 of file xpath.c.
References XDOC::doc, XMOD::docs, XMOD::mutex, and XMOD::sdoc.
Referenced by xpath_init().
|
static |
XPath select function returning number.
ctx | SQLite function context |
argc | number of arguments |
argv | argument vector |
Definition at line 1272 of file xpath.c.
References xpath_func_common().
Referenced by xpath_init().
|
static |
XPath select function returning string value.
ctx | SQLite function context |
argc | number of arguments |
argv | argument vector |
Definition at line 1246 of file xpath.c.
References xpath_func_common().
Referenced by xpath_init().
|
static |
XPath select function returning XML.
ctx | SQLite function context |
argc | number of arguments |
argv | argument vector |
Definition at line 1285 of file xpath.c.
References xpath_func_common().
Referenced by xpath_init().
|
static |
Module initializer creating SQLite module and functions.
db | database pointer |
Definition at line 1571 of file xpath.c.
References XMOD::docs, initialized, XMOD::mutex, XMOD::ndoc, XMOD::refcnt, XMOD::sdoc, xmod, xpath_fini(), xpath_func_boolean(), xpath_func_dump(), xpath_func_number(), xpath_func_string(), xpath_func_xml(), and xpath_mod.
Referenced by sqlite3_extension_init().
|
static |
Retrieve next row from virtual table cursor.
cursor | virtual table cursor |
Definition at line 321 of file xpath.c.
References XCSR::cursor, XCSR::first, XTAB::ndoc, XCSR::nexpr, XEXP::next, XEXP::parent, XEXP::pobj, XCSR::pos, and XEXP::pos.
Referenced by xpath_filter().
|
static |
Open virtual table and return cursor.
vtab | virtual table pointer |
cursorp | pointer receiving cursor pointer |
Definition at line 268 of file xpath.c.
References XCSR::cursor, XCSR::first, XCSR::last, XCSR::nexpr, and XCSR::pos.
|
static |
Return current rowid of virtual table cursor.
cursor | virtual table cursor |
rowidp | value buffer to receive current rowid |
Definition at line 505 of file xpath.c.
References XCSR::cursor, XDOC::doc, XMOD::docs, XTAB::idocs, XCSR::pos, and XTAB::xm.
|
static |
Insert/delete row into/from virtual table.
vtab | virtual table pointer |
argc | number of arguments |
argv | argument vector |
rowidp | value buffer to receive rowid |
Examples:
CREATE VIRTUAL TABLE X USING xpath();
INSERT INTO X(XML) VALUES('xml-string ...');
INSERT INTO X(PATH,OPTIONS) VALUES(<url>,0);
DELETE FROM X WHERE DOCID=<docid>;
Virtual table columns:
DOCID - document identifier and ROWID
XML - XML string
PATH - pathname or URL
OPTIONS - parser options, see libxml's XML_PARSE_* defines
ENCODING - optional document encoding, default UTF-8
BASEURL - optional base URL when XML string given
XMLDUMP - output column, XML dump of document tree
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.
Definition at line 550 of file xpath.c.
References XDOC::doc, XMOD::docs, XTAB::idocs, XMOD::mutex, XMOD::ndoc, XTAB::ndoc, nomem(), XDOC::refcnt, XMOD::sdoc, XTAB::sdoc, and XTAB::xm.
|
static |
XPath select function returning boolean value from virtual table.
ctx | SQLite function context |
argc | number of arguments |
argv | argument vector |
Definition at line 966 of file xpath.c.
References xpath_vfunc_common().
Referenced by xpath_findfunc().
|
static |
Common XPath select function for virtual table.
ctx | SQLite function context |
conv | conversion (0=string, 1=boolean, 2=number) |
argc | number of arguments |
argv | argument vector |
Examples:
CREATE VIRTUAL TABLE X USING xpath();
INSERT INTO X(XML) VALUES('xml-string ...');
SELECT xpath_string(docid, '//book/title') FROM X;
SELECT xpath_number(docid, '//book/price') FROM X;
The RHS of the xpath_* functions should be a constant string.
Definition at line 757 of file xpath.c.
References XEXP::conv, XDOC::doc, XEXP::doc, XMOD::docs, XEXP::expr, XCSR::first, XTAB::idocs, XCSR::last, XTAB::ndoc, XCSR::nexpr, XEXP::next, XEXP::parent, XEXP::pctx, XEXP::pobj, XCSR::pos, XEXP::pos, XEXP::prev, XTAB::xc, and XTAB::xm.
Referenced by xpath_vfunc_boolean(), xpath_vfunc_number(), xpath_vfunc_string(), and xpath_vfunc_xml().
|
static |
XPath select function returning number from virtual table.
ctx | SQLite function context |
argc | number of arguments |
argv | argument vector |
Definition at line 979 of file xpath.c.
References xpath_vfunc_common().
Referenced by xpath_findfunc().
|
static |
XPath select function returning string value from virtual table.
ctx | SQLite function context |
argc | number of arguments |
argv | argument vector |
Definition at line 953 of file xpath.c.
References xpath_vfunc_common().
Referenced by xpath_findfunc().
|
static |
XPath select function returning XML from virtual table.
ctx | SQLite function context |
argc | number of arguments |
argv | argument vector |
Definition at line 992 of file xpath.c.
References xpath_vfunc_common().
Referenced by xpath_findfunc().
|
static |
Definition at line 66 of file xpath.c.
Referenced by xpath_fini(), and xpath_init().
|
static |
Definition at line 67 of file xpath.c.
Referenced by xpath_fini(), and xpath_init().
|
static |
SQLite module descriptor.
Definition at line 1058 of file xpath.c.
Referenced by xpath_init().