xpath.c File Reference


Detailed Description

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.

#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...

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 XMODxmod = 0
static sqlite3_module xpath_mod
 SQLite module descriptor.


Function Documentation

int sqlite3_extension_init ( sqlite3 *  db,
char **  errmsg,
const sqlite3_api_routines *  api 
)

Initializer for SQLite extension load mechanism.

Parameters:
db SQLite database pointer
errmsg pointer receiving error message
api SQLite API routines
Returns:
SQLite error code

Definition at line 1658 of file xpath.c.

References xpath_init().

static int xpath_bestindex ( sqlite3_vtab *  vtab,
sqlite3_index_info *  info 
) [static]

Determines information for filter function according to constraints.

Parameters:
vtab virtual table
info index/constraint information
Returns:
SQLite error code

Definition at line 255 of file xpath.c.

static int xpath_close ( sqlite3_vtab_cursor *  cursor  )  [static]

Close virtual table cursor.

Parameters:
cursor cursor pointer
Returns:
SQLite error code

Definition at line 290 of file xpath.c.

References XCSR::cursor, XCSR::first, XEXP::next, XEXP::pctx, XEXP::pobj, and XTAB::xc.

static int xpath_column ( sqlite3_vtab_cursor *  cursor,
sqlite3_context *  ctx,
int  n 
) [static]

Return column data of virtual table.

Parameters:
cursor virtual table cursor
ctx SQLite function context
n column index
Returns:
SQLite error code

Definition at line 461 of file xpath.c.

References XCSR::cursor, XTAB::idocs, XTAB::ndoc, XCSR::pos, and XTAB::xm.

static int xpath_connect ( sqlite3 *  db,
void *  aux,
int  argc,
const char *const *  argv,
sqlite3_vtab **  vtabp,
char **  errp 
) [static]

Connect to virtual table.

Parameters:
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
Returns:
SQLite error code
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 int xpath_create ( sqlite3 *  db,
void *  aux,
int  argc,
const char *const *  argv,
sqlite3_vtab **  vtabp,
char **  errp 
) [static]

Create virtual table.

Parameters:
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
Returns:
SQLite error code

Definition at line 191 of file xpath.c.

References xpath_connect().

static int xpath_destroy ( sqlite3_vtab *  vtab  )  [static]

Destroy virtual table.

Parameters:
vtab virtual table pointer
Returns:
always SQLITE_OK

Definition at line 242 of file xpath.c.

References xpath_disconnect().

static int xpath_disconnect ( sqlite3_vtab *  vtab  )  [static]

Disconnect virtual table.

Parameters:
vtab virtual table pointer
Returns:
always SQLITE_OK

Definition at line 205 of file xpath.c.

References XDOC::doc, XMOD::docs, XTAB::idocs, XMOD::mutex, XMOD::ndoc, XTAB::ndoc, XDOC::refcnt, XMOD::sdoc, and XTAB::xm.

Referenced by xpath_destroy().

static int xpath_eof ( sqlite3_vtab_cursor *  cursor  )  [static]

Return end of table state of virtual table cursor.

Parameters:
cursor virtual table cursor
Returns:
true/false

Definition at line 444 of file xpath.c.

References XCSR::cursor, XTAB::ndoc, and XCSR::pos.

static int xpath_filter ( sqlite3_vtab_cursor *  cursor,
int  idxNum,
const char *  idxStr,
int  argc,
sqlite3_value **  argv 
) [static]

Filter function for virtual table.

Parameters:
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)
Returns:
SQLite error code

Definition at line 426 of file xpath.c.

References XCSR::cursor, XCSR::pos, XTAB::xc, and xpath_next().

static int xpath_findfunc ( sqlite3_vtab *  vtab,
int  nargs,
const char *  name,
void(**)(sqlite3_context *, int, sqlite3_value **)  pfunc,
void **  parg 
) [static]

Find overloaded function on virtual table.

Parameters:
vtab virtual table
nargs number arguments
name function name
pfunc pointer to function (value return)
parg pointer to function's argument (value return)
Returns:
0 or 1

Definition at line 1008 of file xpath.c.

References xpath_vfunc_boolean(), xpath_vfunc_number(), xpath_vfunc_string(), and xpath_vfunc_xml().

static void xpath_fini ( void *  aux  )  [static]

Module finalizer.

Parameters:
aux pointer to module data
Returns:
SQLite error code

Definition at line 1518 of file xpath.c.

References XDOC::doc, XMOD::docs, initialized, XMOD::mutex, XMOD::ndoc, XDOC::refcnt, XMOD::refcnt, and XMOD::sdoc.

Referenced by xpath_init().

static void xpath_func_boolean ( sqlite3_context *  ctx,
int  argc,
sqlite3_value **  argv 
) [static]

XPath select function returning boolean value.

Parameters:
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 void xpath_func_common ( sqlite3_context *  ctx,
int  conv,
int  argc,
sqlite3_value **  argv 
) [static]

Common XPath select function.

Parameters:
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;&gt, <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 void xpath_func_dump ( sqlite3_context *  ctx,
int  argc,
sqlite3_value **  argv 
) [static]

Function to dump XML document.

Parameters:
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 void xpath_func_number ( sqlite3_context *  ctx,
int  argc,
sqlite3_value **  argv 
) [static]

XPath select function returning number.

Parameters:
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 void xpath_func_string ( sqlite3_context *  ctx,
int  argc,
sqlite3_value **  argv 
) [static]

XPath select function returning string value.

Parameters:
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 void xpath_func_xml ( sqlite3_context *  ctx,
int  argc,
sqlite3_value **  argv 
) [static]

XPath select function returning XML.

Parameters:
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 int xpath_init ( sqlite3 *  db  )  [static]

Module initializer creating SQLite module and functions.

Parameters:
db database pointer
Returns:
SQLite error code

Definition at line 1571 of file xpath.c.

References XMOD::docs, initialized, XMOD::mutex, XMOD::ndoc, XMOD::refcnt, XMOD::sdoc, xpath_fini(), xpath_func_boolean(), xpath_func_dump(), xpath_func_number(), xpath_func_string(), and xpath_func_xml().

Referenced by sqlite3_extension_init().

static int xpath_next ( sqlite3_vtab_cursor *  cursor  )  [static]

Retrieve next row from virtual table cursor.

Parameters:
cursor virtual table cursor
Returns:
SQLite error code

Definition at line 321 of file xpath.c.

References XCSR::cursor, XCSR::first, XTAB::ndoc, XCSR::nexpr, XEXP::parent, XEXP::pos, and XCSR::pos.

Referenced by xpath_filter().

static int xpath_open ( sqlite3_vtab *  vtab,
sqlite3_vtab_cursor **  cursorp 
) [static]

Open virtual table and return cursor.

Parameters:
vtab virtual table pointer
cursorp pointer receiving cursor pointer
Returns:
SQLite error code

Definition at line 268 of file xpath.c.

References XCSR::cursor, XCSR::first, XCSR::last, XCSR::nexpr, and XCSR::pos.

static int xpath_rowid ( sqlite3_vtab_cursor *  cursor,
sqlite3_int64 *  rowidp 
) [static]

Return current rowid of virtual table cursor.

Parameters:
cursor virtual table cursor
rowidp value buffer to receive current rowid
Returns:
SQLite error code

Definition at line 505 of file xpath.c.

References XCSR::cursor, XTAB::idocs, XCSR::pos, and XTAB::xm.

static int xpath_update ( sqlite3_vtab *  vtab,
int  argc,
sqlite3_value **  argv,
sqlite3_int64 *  rowidp 
) [static]

Insert/delete row into/from virtual table.

Parameters:
vtab virtual table pointer
argc number of arguments
argv argument vector
rowidp value buffer to receive rowid
Returns:
SQLite error code
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 void xpath_vfunc_boolean ( sqlite3_context *  ctx,
int  argc,
sqlite3_value **  argv 
) [static]

XPath select function returning boolean value from virtual table.

Parameters:
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 void xpath_vfunc_common ( sqlite3_context *  ctx,
int  conv,
int  argc,
sqlite3_value **  argv 
) [static]

Common XPath select function for virtual table.

Parameters:
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, XEXP::doc, XDOC::doc, XMOD::docs, XEXP::expr, XCSR::first, XTAB::idocs, XCSR::last, XTAB::ndoc, XCSR::nexpr, XEXP::next, XEXP::parent, XEXP::pctx, XEXP::pobj, XEXP::pos, XCSR::pos, XEXP::prev, XTAB::xc, and XTAB::xm.

Referenced by xpath_vfunc_boolean(), xpath_vfunc_number(), xpath_vfunc_string(), and xpath_vfunc_xml().

static void xpath_vfunc_number ( sqlite3_context *  ctx,
int  argc,
sqlite3_value **  argv 
) [static]

XPath select function returning number from virtual table.

Parameters:
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 void xpath_vfunc_string ( sqlite3_context *  ctx,
int  argc,
sqlite3_value **  argv 
) [static]

XPath select function returning string value from virtual table.

Parameters:
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 void xpath_vfunc_xml ( sqlite3_context *  ctx,
int  argc,
sqlite3_value **  argv 
) [static]

XPath select function returning XML from virtual table.

Parameters:
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().


Variable Documentation

int initialized = 0 [static]

Definition at line 66 of file xpath.c.

Referenced by xpath_fini(), and xpath_init().

XMOD* xmod = 0 [static]

Definition at line 67 of file xpath.c.

sqlite3_module xpath_mod [static]


Generated on 9 Dec 2016 by doxygen.
Contact: chw@ch-werner.de