Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

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  XCSR
 Structure to describe virtual table cursor. More...

struct  XDOC
 Structure to cache XML document. More...

struct  XEXP
 Structure to describe XPath expression. More...

struct  XMOD
 Structure holding per module/database data. More...

struct  XTAB
 Structure to describe virtual table. More...


Typedefs

typedef XDOC XDOC
typedef XMOD XMOD
typedef XTAB XTAB
typedef XEXP XEXP
typedef XCSR XCSR

Functions

int xpath_connect (sqlite3 *db, void *aux, int argc, const char *const *argv, sqlite3_vtab **vtabp, char **errp)
 Connect to virtual table.

int xpath_create (sqlite3 *db, void *aux, int argc, const char *const *argv, sqlite3_vtab **vtabp, char **errp)
 Create virtual table.

int xpath_disconnect (sqlite3_vtab *vtab)
 Disconnect virtual table.

int xpath_destroy (sqlite3_vtab *vtab)
 Destroy virtual table.

int xpath_bestindex (sqlite3_vtab *vtab, sqlite3_index_info *info)
 Determines information for filter function according to constraints.

int xpath_open (sqlite3_vtab *vtab, sqlite3_vtab_cursor **cursorp)
 Open virtual table and return cursor.

int xpath_close (sqlite3_vtab_cursor *cursor)
 Close virtual table cursor.

int xpath_next (sqlite3_vtab_cursor *cursor)
 Retrieve next row from virtual table cursor.

int xpath_filter (sqlite3_vtab_cursor *cursor, int idxNum, const char *idxStr, int argc, sqlite3_value **argv)
 Filter function for virtual table.

int xpath_eof (sqlite3_vtab_cursor *cursor)
 Return end of table state of virtual table cursor.

int xpath_column (sqlite3_vtab_cursor *cursor, sqlite3_context *ctx, int n)
 Return column data of virtual table.

int xpath_rowid (sqlite3_vtab_cursor *cursor, sqlite3_int64 *rowidp)
 Return current rowid of virtual table cursor.

int xpath_update (sqlite3_vtab *vtab, int argc, sqlite3_value **argv, sqlite3_int64 *rowidp)
 Insert/delete row into/from virtual table.

void xpath_vfunc_common (sqlite3_context *ctx, int conv, int argc, sqlite3_value **argv)
 Common XPath select function for virtual table.

void xpath_vfunc_string (sqlite3_context *ctx, int argc, sqlite3_value **argv)
 XPath select function returning string value from virtual table.

void xpath_vfunc_boolean (sqlite3_context *ctx, int argc, sqlite3_value **argv)
 XPath select function returning boolean value from virtual table.

void xpath_vfunc_number (sqlite3_context *ctx, int argc, sqlite3_value **argv)
 XPath select function returning number from virtual table.

void xpath_vfunc_xml (sqlite3_context *ctx, int argc, sqlite3_value **argv)
 XPath select function returning XML from virtual table.

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.

void xpath_func_common (sqlite3_context *ctx, int conv, int argc, sqlite3_value **argv)
 Common XPath select function.

void xpath_func_string (sqlite3_context *ctx, int argc, sqlite3_value **argv)
 XPath select function returning string value.

void xpath_func_boolean (sqlite3_context *ctx, int argc, sqlite3_value **argv)
 XPath select function returning boolean value.

void xpath_func_number (sqlite3_context *ctx, int argc, sqlite3_value **argv)
 XPath select function returning number.

void xpath_func_xml (sqlite3_context *ctx, int argc, sqlite3_value **argv)
 XPath select function returning XML.

void xpath_func_dump (sqlite3_context *ctx, int argc, sqlite3_value **argv)
 Function to dump XML document.

void xpath_fini (void *aux)
 Module finalizer.

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

int initialized = 0
XMODxmod = 0
sqlite3_module xpath_mod
 SQLite module descriptor.


Typedef Documentation

typedef struct XCSR XCSR
 

typedef struct XDOC XDOC
 

typedef struct XEXP XEXP
 

typedef struct XMOD XMOD
 

typedef struct XTAB XTAB
 


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

Referenced by impexp_init().

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.

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.

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, XDOC::doc, XMOD::docs, XTAB::idocs, XTAB::ndoc, XCSR::pos, and XTAB::xm.

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, XTAB::sdoc, XTAB::vtab, XTAB::xc, and XTAB::xm.

Referenced by xpath_create().

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

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

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

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.

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

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

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

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

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

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

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

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

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

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(), xpath_func_xml(), and xpath_mod.

Referenced by sqlite3_extension_init().

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::next, XEXP::parent, XEXP::pobj, XEXP::pos, and XCSR::pos.

Referenced by xpath_filter().

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.

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, XDOC::doc, XMOD::docs, XTAB::idocs, XCSR::pos, and XTAB::xm.

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, XDOC::refcnt, XTAB::sdoc, XMOD::sdoc, and XTAB::xm.

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

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

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

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

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]
 

Initial value:

 {
    1,                  
    xpath_create,       
    xpath_connect,      
    xpath_bestindex,    
    xpath_disconnect,   
    xpath_destroy,      
    xpath_open,         
    xpath_close,        
    xpath_filter,       
    xpath_next,         
    xpath_eof,          
    xpath_column,       
    xpath_rowid,        
    xpath_update,       
    0,                  
    0,                  
    0,                  
    0,                  
    xpath_findfunc,     



}
SQLite module descriptor.

Definition at line 1058 of file xpath.c.

Referenced by xpath_init().


Generated on 23 Oct 2023 by doxygen.
Contact: chw@ch-werner.de