Skip to content

Instantly share code, notes, and snippets.

@xavery
Created February 2, 2017 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xavery/976981b84d3bba90ef6bb480adc7a16d to your computer and use it in GitHub Desktop.
Save xavery/976981b84d3bba90ef6bb480adc7a16d to your computer and use it in GitHub Desktop.
A demonstration of differences between = NULL and IS NULL queries in SQLite virtual tables
/*
** 2009 November 10
**
** 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.
**
*************************************************************************
**
** This file implements a read-only VIRTUAL TABLE that contains the
** content of a C-language array of integer values. See the corresponding
** header file for full details.
*/
#include <sqlite3.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
typedef struct sqlite3_intarray sqlite3_intarray;
/*
** Definition of the sqlite3_intarray object.
**
** The internal representation of an intarray object is subject
** to change, is not externally visible, and should be used by
** the implementation of intarray only. This object is opaque
** to users.
*/
struct sqlite3_intarray {
int n; /* Number of elements in the array */
sqlite3_int64 *a; /* Contents of the array */
void (*xFree)(void*); /* Function used to free a[] */
};
/* Objects used internally by the virtual table implementation */
typedef struct intarray_vtab intarray_vtab;
typedef struct intarray_cursor intarray_cursor;
/* An intarray table object */
struct intarray_vtab {
sqlite3_vtab base; /* Base class */
sqlite3_intarray *pContent; /* Content of the integer array */
};
/* An intarray cursor object */
struct intarray_cursor {
sqlite3_vtab_cursor base; /* Base class */
int i; /* Current cursor position */
};
/*
** None of this works unless we have virtual tables.
*/
#ifndef SQLITE_OMIT_VIRTUALTABLE
/*
** Free an sqlite3_intarray object.
*/
static void intarrayFree(sqlite3_intarray *p){
if( p->xFree ){
p->xFree(p->a);
}
sqlite3_free(p);
}
/*
** Table destructor for the intarray module.
*/
static int intarrayDestroy(sqlite3_vtab *p){
intarray_vtab *pVtab = (intarray_vtab*)p;
sqlite3_free(pVtab);
return 0;
}
/*
** Table constructor for the intarray module.
*/
static int intarrayCreate(
sqlite3 *db, /* Database where module is created */
void *pAux, /* clientdata for the module */
int argc, /* Number of arguments */
const char *const*argv, /* Value for all arguments */
sqlite3_vtab **ppVtab, /* Write the new virtual table object here */
char **pzErr /* Put error message text here */
){
int rc = SQLITE_NOMEM;
intarray_vtab *pVtab = sqlite3_malloc64(sizeof(intarray_vtab));
if( pVtab ){
memset(pVtab, 0, sizeof(intarray_vtab));
pVtab->pContent = (sqlite3_intarray*)pAux;
rc = sqlite3_declare_vtab(db, "CREATE TABLE x(value INTEGER PRIMARY KEY)");
}
*ppVtab = (sqlite3_vtab *)pVtab;
return rc;
}
/*
** Open a new cursor on the intarray table.
*/
static int intarrayOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
int rc = SQLITE_NOMEM;
intarray_cursor *pCur;
pCur = sqlite3_malloc64(sizeof(intarray_cursor));
if( pCur ){
memset(pCur, 0, sizeof(intarray_cursor));
*ppCursor = (sqlite3_vtab_cursor *)pCur;
rc = SQLITE_OK;
}
return rc;
}
/*
** Close a intarray table cursor.
*/
static int intarrayClose(sqlite3_vtab_cursor *cur){
intarray_cursor *pCur = (intarray_cursor *)cur;
sqlite3_free(pCur);
return SQLITE_OK;
}
/*
** Retrieve a column of data.
*/
static int intarrayColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
intarray_cursor *pCur = (intarray_cursor*)cur;
intarray_vtab *pVtab = (intarray_vtab*)cur->pVtab;
if( pCur->i>=0 && pCur->i<pVtab->pContent->n ){
sqlite3_result_int64(ctx, pVtab->pContent->a[pCur->i]);
}
return SQLITE_OK;
}
/*
** Retrieve the current rowid.
*/
static int intarrayRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
intarray_cursor *pCur = (intarray_cursor *)cur;
*pRowid = pCur->i;
return SQLITE_OK;
}
static int intarrayEof(sqlite3_vtab_cursor *cur){
intarray_cursor *pCur = (intarray_cursor *)cur;
intarray_vtab *pVtab = (intarray_vtab *)cur->pVtab;
return pCur->i>=pVtab->pContent->n;
}
/*
** Advance the cursor to the next row.
*/
static int intarrayNext(sqlite3_vtab_cursor *cur){
intarray_cursor *pCur = (intarray_cursor *)cur;
pCur->i++;
return SQLITE_OK;
}
/*
** Reset a intarray table cursor.
*/
static int intarrayFilter(
sqlite3_vtab_cursor *pVtabCursor,
int idxNum, const char *idxStr,
int argc, sqlite3_value **argv
){
intarray_cursor *pCur = (intarray_cursor *)pVtabCursor;
pCur->i = 0;
fprintf(stderr, "Filter : argc = %d\n", argc);
for(int i = 0 ; i < argc ; ++i) {
fprintf(stderr, "argv[%d] = %s\n", i, sqlite3_value_text(argv[i]));
}
return SQLITE_OK;
}
/*
** Analyse the WHERE condition.
*/
static int intarrayBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
fprintf(stderr, "BestIndex : nConstraint = %d\n", pIdxInfo->nConstraint);
int argvIdx = 1;
for(int i = 0 ; i < pIdxInfo->nConstraint ; ++i) {
pIdxInfo->aConstraintUsage[i].argvIndex = argvIdx++;
}
return SQLITE_OK;
}
/*
** A virtual table module that merely echos method calls into TCL
** variables.
*/
static sqlite3_module intarrayModule = {
0, /* iVersion */
intarrayCreate, /* xCreate - create a new virtual table */
intarrayCreate, /* xConnect - connect to an existing vtab */
intarrayBestIndex, /* xBestIndex - find the best query index */
intarrayDestroy, /* xDisconnect - disconnect a vtab */
intarrayDestroy, /* xDestroy - destroy a vtab */
intarrayOpen, /* xOpen - open a cursor */
intarrayClose, /* xClose - close a cursor */
intarrayFilter, /* xFilter - configure scan constraints */
intarrayNext, /* xNext - advance a cursor */
intarrayEof, /* xEof */
intarrayColumn, /* xColumn - read data */
intarrayRowid, /* xRowid - read data */
0, /* xUpdate */
0, /* xBegin */
0, /* xSync */
0, /* xCommit */
0, /* xRollback */
0, /* xFindMethod */
0, /* xRename */
};
#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
/*
** Invoke this routine to create a specific instance of an intarray object.
** The new intarray object is returned by the 3rd parameter.
**
** Each intarray object corresponds to a virtual table in the TEMP table
** with a name of zName.
**
** Destroy the intarray object by dropping the virtual table. If not done
** explicitly by the application, the virtual table will be dropped implicitly
** by the system when the database connection is closed.
*/
SQLITE_API int sqlite3_intarray_create(
sqlite3 *db,
const char *zName,
sqlite3_intarray **ppReturn
){
int rc = SQLITE_OK;
#ifndef SQLITE_OMIT_VIRTUALTABLE
sqlite3_intarray *p;
*ppReturn = p = sqlite3_malloc64( sizeof(*p) );
if( p==0 ){
return SQLITE_NOMEM;
}
memset(p, 0, sizeof(*p));
rc = sqlite3_create_module_v2(db, zName, &intarrayModule, p,
(void(*)(void*))intarrayFree);
if( rc==SQLITE_OK ){
char *zSql;
zSql = sqlite3_mprintf("CREATE VIRTUAL TABLE temp.%Q USING %Q",
zName, zName);
rc = sqlite3_exec(db, zSql, 0, 0, 0);
sqlite3_free(zSql);
}
#endif
return rc;
}
/*
** Bind a new array array of integers to a specific intarray object.
**
** The array of integers bound must be unchanged for the duration of
** any query against the corresponding virtual table. If the integer
** array does change or is deallocated undefined behavior will result.
*/
SQLITE_API int sqlite3_intarray_bind(
sqlite3_intarray *pIntArray, /* The intarray object to bind to */
int nElements, /* Number of elements in the intarray */
sqlite3_int64 *aElements, /* Content of the intarray */
void (*xFree)(void*) /* How to dispose of the intarray when done */
){
if( pIntArray->xFree ){
pIntArray->xFree(pIntArray->a);
}
pIntArray->n = nElements;
pIntArray->a = aElements;
pIntArray->xFree = xFree;
return SQLITE_OK;
}
int main(void) {
sqlite3* db;
int rv = sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE, 0);
assert(rv == SQLITE_OK);
sqlite3_intarray* intarray;
rv = sqlite3_intarray_create(db, "a", &intarray);
assert(rv == SQLITE_OK);
sqlite3_int64 contents[] = { 1, 2, 3, 4 };
rv = sqlite3_intarray_bind(intarray, sizeof(contents)/sizeof(*contents),
contents, 0);
assert(rv == SQLITE_OK);
rv = sqlite3_exec(db, "SELECT 1 FROM a WHERE value = 123", 0, 0, 0);
assert(rv == SQLITE_OK);
rv = sqlite3_exec(db, "SELECT 1 FROM a WHERE value = NULL", 0, 0, 0);
assert(rv == SQLITE_OK);
rv = sqlite3_exec(db, "SELECT 1 FROM a WHERE value IS NULL", 0, 0, 0);
assert(rv == SQLITE_OK);
rv = sqlite3_exec(db, "SELECT 1 FROM a WHERE value IS NOT NULL", 0, 0, 0);
assert(rv == SQLITE_OK);
rv = sqlite3_exec(db, "SELECT 1 FROM a WHERE typeof(value) = 'null'", 0, 0, 0);
assert(rv == SQLITE_OK);
sqlite3_close_v2(db);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment