xpath.c File Reference

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

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.

Typedef Documentation

◆ XCSR

typedef struct XCSR XCSR

◆ XDOC

typedef struct XDOC XDOC

◆ XEXP

typedef struct XEXP XEXP

◆ XMOD

typedef struct XMOD XMOD

◆ XTAB

typedef struct XTAB XTAB

Function Documentation

◆ sqlite3_extension_init()

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

Initializer for SQLite extension load mechanism.

Parameters
dbSQLite database pointer
errmsgpointer receiving error message
apiSQLite API routines
Returns
SQLite error code

Definition at line 1658 of file xpath.c.

References xpath_init().

◆ xpath_bestindex()

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

Determines information for filter function according to constraints.

Parameters
vtabvirtual table
infoindex/constraint information
Returns
SQLite error code

Definition at line 255 of file xpath.c.

◆ xpath_close()

static int xpath_close ( sqlite3_vtab_cursor * cursor)
static

Close virtual table cursor.

Parameters
cursorcursor 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.

◆ xpath_column()

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

Return column data of virtual table.

Parameters
cursorvirtual table cursor
ctxSQLite function context
ncolumn index
Returns
SQLite error code

Definition at line 461 of file xpath.c.

References XCSR::cursor, XDOC::doc, XMOD::docs, XTAB::idocs, XTAB::ndoc, XCSR::pos, and XTAB::xm.

◆ xpath_connect()

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
dbSQLite database pointer
auxuser specific pointer
argcargument count
argvargument vector
vtabppointer receiving virtual table pointer
errppointer 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().

◆ 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
dbSQLite database pointer
auxuser specific pointer
argcargument count
argvargument vector
vtabppointer receiving virtual table pointer
errppointer receiving error messag
Returns
SQLite error code

Definition at line 191 of file xpath.c.

References xpath_connect().

◆ xpath_destroy()

static int xpath_destroy ( sqlite3_vtab * vtab)
static

Destroy virtual table.

Parameters
vtabvirtual table pointer
Returns
always SQLITE_OK

Definition at line 242 of file xpath.c.

References xpath_disconnect().

◆ xpath_disconnect()

static int xpath_disconnect ( sqlite3_vtab * vtab)
static

Disconnect virtual table.

Parameters
vtabvirtual 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, and XTAB::xm.

Referenced by xpath_destroy().

◆ xpath_eof()

static int xpath_eof ( sqlite3_vtab_cursor * cursor)
static

Return end of table state of virtual table cursor.

Parameters
cursorvirtual table cursor
Returns
true/false

Definition at line 444 of file xpath.c.

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

◆ xpath_filter()

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
cursorvirtual table cursor
idxNumnot used
idxStrnod used
argcnumber arguments (not used)
argvargument (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().

◆ xpath_findfunc()

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

Find overloaded function on virtual table.

Parameters
vtabvirtual table
nargsnumber arguments
namefunction name
pfuncpointer to function (value return)
pargpointer 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().

◆ xpath_fini()

static void xpath_fini ( void * aux)
static

Module finalizer.

Parameters
auxpointer 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, XMOD::sdoc, and xmod.

Referenced by xpath_init().

◆ xpath_func_boolean()

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

XPath select function returning boolean value.

Parameters
ctxSQLite function context
argcnumber of arguments
argvargument vector

Definition at line 1259 of file xpath.c.

References xpath_func_common().

Referenced by xpath_init().

◆ xpath_func_common()

static void xpath_func_common ( sqlite3_context * ctx,
int conv,
int argc,
sqlite3_value ** argv )
static

Common XPath select function.

Parameters
ctxSQLite function context
convconversion (0=string, 1=boolean, 2=number)
argcnumber of arguments
argvargument 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().

◆ xpath_func_dump()

static void xpath_func_dump ( sqlite3_context * ctx,
int argc,
sqlite3_value ** argv )
static

Function to dump XML document.

Parameters
ctxSQLite function context
argcnumber of arguments
argvargument 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().

◆ xpath_func_number()

static void xpath_func_number ( sqlite3_context * ctx,
int argc,
sqlite3_value ** argv )
static

XPath select function returning number.

Parameters
ctxSQLite function context
argcnumber of arguments
argvargument vector

Definition at line 1272 of file xpath.c.

References xpath_func_common().

Referenced by xpath_init().

◆ xpath_func_string()

static void xpath_func_string ( sqlite3_context * ctx,
int argc,
sqlite3_value ** argv )
static

XPath select function returning string value.

Parameters
ctxSQLite function context
argcnumber of arguments
argvargument vector

Definition at line 1246 of file xpath.c.

References xpath_func_common().

Referenced by xpath_init().

◆ xpath_func_xml()

static void xpath_func_xml ( sqlite3_context * ctx,
int argc,
sqlite3_value ** argv )
static

XPath select function returning XML.

Parameters
ctxSQLite function context
argcnumber of arguments
argvargument vector

Definition at line 1285 of file xpath.c.

References xpath_func_common().

Referenced by xpath_init().

◆ xpath_init()

static int xpath_init ( sqlite3 * db)
static

Module initializer creating SQLite module and functions.

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

◆ xpath_next()

static int xpath_next ( sqlite3_vtab_cursor * cursor)
static

Retrieve next row from virtual table cursor.

Parameters
cursorvirtual table cursor
Returns
SQLite error code

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

◆ xpath_open()

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

Open virtual table and return cursor.

Parameters
vtabvirtual table pointer
cursorppointer 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.

◆ xpath_rowid()

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

Return current rowid of virtual table cursor.

Parameters
cursorvirtual table cursor
rowidpvalue buffer to receive current rowid
Returns
SQLite error code

Definition at line 505 of file xpath.c.

References XCSR::cursor, XDOC::doc, XMOD::docs, XTAB::idocs, XCSR::pos, and XTAB::xm.

◆ xpath_update()

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

Insert/delete row into/from virtual table.

Parameters
vtabvirtual table pointer
argcnumber of arguments
argvargument vector
rowidpvalue 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.

◆ xpath_vfunc_boolean()

static void xpath_vfunc_boolean ( sqlite3_context * ctx,
int argc,
sqlite3_value ** argv )
static

XPath select function returning boolean value from virtual table.

Parameters
ctxSQLite function context
argcnumber of arguments
argvargument vector

Definition at line 966 of file xpath.c.

References xpath_vfunc_common().

Referenced by xpath_findfunc().

◆ xpath_vfunc_common()

static void xpath_vfunc_common ( sqlite3_context * ctx,
int conv,
int argc,
sqlite3_value ** argv )
static

Common XPath select function for virtual table.

Parameters
ctxSQLite function context
convconversion (0=string, 1=boolean, 2=number)
argcnumber of arguments
argvargument 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().

◆ xpath_vfunc_number()

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

XPath select function returning number from virtual table.

Parameters
ctxSQLite function context
argcnumber of arguments
argvargument vector

Definition at line 979 of file xpath.c.

References xpath_vfunc_common().

Referenced by xpath_findfunc().

◆ xpath_vfunc_string()

static void xpath_vfunc_string ( sqlite3_context * ctx,
int argc,
sqlite3_value ** argv )
static

XPath select function returning string value from virtual table.

Parameters
ctxSQLite function context
argcnumber of arguments
argvargument vector

Definition at line 953 of file xpath.c.

References xpath_vfunc_common().

Referenced by xpath_findfunc().

◆ xpath_vfunc_xml()

static void xpath_vfunc_xml ( sqlite3_context * ctx,
int argc,
sqlite3_value ** argv )
static

XPath select function returning XML from virtual table.

Parameters
ctxSQLite function context
argcnumber of arguments
argvargument vector

Definition at line 992 of file xpath.c.

References xpath_vfunc_common().

Referenced by xpath_findfunc().

Variable Documentation

◆ initialized

int initialized = 0
static

Definition at line 66 of file xpath.c.

Referenced by xpath_fini(), and xpath_init().

◆ xmod

XMOD* xmod = 0
static

Definition at line 67 of file xpath.c.

Referenced by xpath_fini(), and xpath_init().

◆ xpath_mod

sqlite3_module xpath_mod
static
Initial value:
= {
1,
0,
0,
0,
0,
}
static int xpath_rowid(sqlite3_vtab_cursor *cursor, sqlite3_int64 *rowidp)
Return current rowid of virtual table cursor.
Definition xpath.c:505
static int xpath_next(sqlite3_vtab_cursor *cursor)
Retrieve next row from virtual table cursor.
Definition xpath.c:321
static int xpath_filter(sqlite3_vtab_cursor *cursor, int idxNum, const char *idxStr, int argc, sqlite3_value **argv)
Filter function for virtual table.
Definition xpath.c:426
static int xpath_destroy(sqlite3_vtab *vtab)
Destroy virtual table.
Definition xpath.c:242
static int xpath_close(sqlite3_vtab_cursor *cursor)
Close virtual table cursor.
Definition xpath.c:290
static int xpath_bestindex(sqlite3_vtab *vtab, sqlite3_index_info *info)
Determines information for filter function according to constraints.
Definition xpath.c:255
static int xpath_disconnect(sqlite3_vtab *vtab)
Disconnect virtual table.
Definition xpath.c:205
static int xpath_column(sqlite3_vtab_cursor *cursor, sqlite3_context *ctx, int n)
Return column data of virtual table.
Definition xpath.c:461
static int xpath_create(sqlite3 *db, void *aux, int argc, const char *const *argv, sqlite3_vtab **vtabp, char **errp)
Create virtual table.
Definition xpath.c:191
static int xpath_eof(sqlite3_vtab_cursor *cursor)
Return end of table state of virtual table cursor.
Definition xpath.c:444
static int xpath_connect(sqlite3 *db, void *aux, int argc, const char *const *argv, sqlite3_vtab **vtabp, char **errp)
Connect to virtual table.
Definition xpath.c:135
static int xpath_update(sqlite3_vtab *vtab, int argc, sqlite3_value **argv, sqlite3_int64 *rowidp)
Insert/delete row into/from virtual table.
Definition xpath.c:550
static int xpath_open(sqlite3_vtab *vtab, sqlite3_vtab_cursor **cursorp)
Open virtual table and return cursor.
Definition xpath.c:268
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.
Definition xpath.c:1008

SQLite module descriptor.

Definition at line 1058 of file xpath.c.

Referenced by xpath_init().


Generated on Sun Nov 26 2023 by doxygen.
Contact: chw@ch-werner.de