1522efc62Sdrh /*
2522efc62Sdrh ** 2009 November 10
3522efc62Sdrh **
4522efc62Sdrh ** The author disclaims copyright to this source code. In place of
5522efc62Sdrh ** a legal notice, here is a blessing:
6522efc62Sdrh **
7522efc62Sdrh ** May you do good and not evil.
8522efc62Sdrh ** May you find forgiveness for yourself and forgive others.
9522efc62Sdrh ** May you share freely, never taking more than you give.
10522efc62Sdrh **
11522efc62Sdrh *************************************************************************
12522efc62Sdrh **
13522efc62Sdrh ** This file implements a read-only VIRTUAL TABLE that contains the
14522efc62Sdrh ** content of a C-language array of integer values. See the corresponding
15522efc62Sdrh ** header file for full details.
16*f8181eaaSdrh **
17*f8181eaaSdrh ** This virtual table is used for internal testing of SQLite only. It is
18*f8181eaaSdrh ** not recommended for use in production. For a similar virtual table that
19*f8181eaaSdrh ** is production-ready, see the "carray" virtual table over in ext/misc.
20522efc62Sdrh */
21522efc62Sdrh #include "test_intarray.h"
22522efc62Sdrh #include <string.h>
23522efc62Sdrh #include <assert.h>
24522efc62Sdrh
25522efc62Sdrh
26522efc62Sdrh /*
27522efc62Sdrh ** Definition of the sqlite3_intarray object.
28522efc62Sdrh **
29522efc62Sdrh ** The internal representation of an intarray object is subject
30522efc62Sdrh ** to change, is not externally visible, and should be used by
31522efc62Sdrh ** the implementation of intarray only. This object is opaque
32522efc62Sdrh ** to users.
33522efc62Sdrh */
34522efc62Sdrh struct sqlite3_intarray {
35522efc62Sdrh int n; /* Number of elements in the array */
36522efc62Sdrh sqlite3_int64 *a; /* Contents of the array */
37522efc62Sdrh void (*xFree)(void*); /* Function used to free a[] */
38522efc62Sdrh };
39522efc62Sdrh
40522efc62Sdrh /* Objects used internally by the virtual table implementation */
41522efc62Sdrh typedef struct intarray_vtab intarray_vtab;
42522efc62Sdrh typedef struct intarray_cursor intarray_cursor;
43522efc62Sdrh
4460ec914cSpeter.d.reid /* An intarray table object */
45522efc62Sdrh struct intarray_vtab {
46522efc62Sdrh sqlite3_vtab base; /* Base class */
47522efc62Sdrh sqlite3_intarray *pContent; /* Content of the integer array */
48522efc62Sdrh };
49522efc62Sdrh
5060ec914cSpeter.d.reid /* An intarray cursor object */
51522efc62Sdrh struct intarray_cursor {
52522efc62Sdrh sqlite3_vtab_cursor base; /* Base class */
53522efc62Sdrh int i; /* Current cursor position */
54522efc62Sdrh };
55522efc62Sdrh
56522efc62Sdrh /*
57522efc62Sdrh ** None of this works unless we have virtual tables.
58522efc62Sdrh */
59522efc62Sdrh #ifndef SQLITE_OMIT_VIRTUALTABLE
60522efc62Sdrh
61522efc62Sdrh /*
6263b38789Sdrh ** Free an sqlite3_intarray object.
63522efc62Sdrh */
intarrayFree(sqlite3_intarray * p)64522efc62Sdrh static void intarrayFree(sqlite3_intarray *p){
65522efc62Sdrh if( p->xFree ){
66522efc62Sdrh p->xFree(p->a);
67522efc62Sdrh }
68522efc62Sdrh sqlite3_free(p);
69522efc62Sdrh }
70522efc62Sdrh
71522efc62Sdrh /*
72522efc62Sdrh ** Table destructor for the intarray module.
73522efc62Sdrh */
intarrayDestroy(sqlite3_vtab * p)74522efc62Sdrh static int intarrayDestroy(sqlite3_vtab *p){
75522efc62Sdrh intarray_vtab *pVtab = (intarray_vtab*)p;
76522efc62Sdrh sqlite3_free(pVtab);
77522efc62Sdrh return 0;
78522efc62Sdrh }
79522efc62Sdrh
80522efc62Sdrh /*
81522efc62Sdrh ** Table constructor for the intarray module.
82522efc62Sdrh */
intarrayCreate(sqlite3 * db,void * pAux,int argc,const char * const * argv,sqlite3_vtab ** ppVtab,char ** pzErr)83522efc62Sdrh static int intarrayCreate(
84522efc62Sdrh sqlite3 *db, /* Database where module is created */
85522efc62Sdrh void *pAux, /* clientdata for the module */
86522efc62Sdrh int argc, /* Number of arguments */
87522efc62Sdrh const char *const*argv, /* Value for all arguments */
88522efc62Sdrh sqlite3_vtab **ppVtab, /* Write the new virtual table object here */
89522efc62Sdrh char **pzErr /* Put error message text here */
90522efc62Sdrh ){
91522efc62Sdrh int rc = SQLITE_NOMEM;
92f3cdcdccSdrh intarray_vtab *pVtab = sqlite3_malloc64(sizeof(intarray_vtab));
93522efc62Sdrh
94522efc62Sdrh if( pVtab ){
95522efc62Sdrh memset(pVtab, 0, sizeof(intarray_vtab));
96522efc62Sdrh pVtab->pContent = (sqlite3_intarray*)pAux;
97522efc62Sdrh rc = sqlite3_declare_vtab(db, "CREATE TABLE x(value INTEGER PRIMARY KEY)");
98522efc62Sdrh }
99522efc62Sdrh *ppVtab = (sqlite3_vtab *)pVtab;
100522efc62Sdrh return rc;
101522efc62Sdrh }
102522efc62Sdrh
103522efc62Sdrh /*
104522efc62Sdrh ** Open a new cursor on the intarray table.
105522efc62Sdrh */
intarrayOpen(sqlite3_vtab * pVTab,sqlite3_vtab_cursor ** ppCursor)106522efc62Sdrh static int intarrayOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
107522efc62Sdrh int rc = SQLITE_NOMEM;
108522efc62Sdrh intarray_cursor *pCur;
109f3cdcdccSdrh pCur = sqlite3_malloc64(sizeof(intarray_cursor));
110522efc62Sdrh if( pCur ){
111522efc62Sdrh memset(pCur, 0, sizeof(intarray_cursor));
112522efc62Sdrh *ppCursor = (sqlite3_vtab_cursor *)pCur;
113522efc62Sdrh rc = SQLITE_OK;
114522efc62Sdrh }
115522efc62Sdrh return rc;
116522efc62Sdrh }
117522efc62Sdrh
118522efc62Sdrh /*
119522efc62Sdrh ** Close a intarray table cursor.
120522efc62Sdrh */
intarrayClose(sqlite3_vtab_cursor * cur)121522efc62Sdrh static int intarrayClose(sqlite3_vtab_cursor *cur){
122522efc62Sdrh intarray_cursor *pCur = (intarray_cursor *)cur;
123522efc62Sdrh sqlite3_free(pCur);
124522efc62Sdrh return SQLITE_OK;
125522efc62Sdrh }
126522efc62Sdrh
127522efc62Sdrh /*
128522efc62Sdrh ** Retrieve a column of data.
129522efc62Sdrh */
intarrayColumn(sqlite3_vtab_cursor * cur,sqlite3_context * ctx,int i)130522efc62Sdrh static int intarrayColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
131522efc62Sdrh intarray_cursor *pCur = (intarray_cursor*)cur;
132522efc62Sdrh intarray_vtab *pVtab = (intarray_vtab*)cur->pVtab;
133522efc62Sdrh if( pCur->i>=0 && pCur->i<pVtab->pContent->n ){
134522efc62Sdrh sqlite3_result_int64(ctx, pVtab->pContent->a[pCur->i]);
135522efc62Sdrh }
136522efc62Sdrh return SQLITE_OK;
137522efc62Sdrh }
138522efc62Sdrh
139522efc62Sdrh /*
140522efc62Sdrh ** Retrieve the current rowid.
141522efc62Sdrh */
intarrayRowid(sqlite3_vtab_cursor * cur,sqlite_int64 * pRowid)142522efc62Sdrh static int intarrayRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
143522efc62Sdrh intarray_cursor *pCur = (intarray_cursor *)cur;
144522efc62Sdrh *pRowid = pCur->i;
145522efc62Sdrh return SQLITE_OK;
146522efc62Sdrh }
147522efc62Sdrh
intarrayEof(sqlite3_vtab_cursor * cur)148522efc62Sdrh static int intarrayEof(sqlite3_vtab_cursor *cur){
149522efc62Sdrh intarray_cursor *pCur = (intarray_cursor *)cur;
150522efc62Sdrh intarray_vtab *pVtab = (intarray_vtab *)cur->pVtab;
151522efc62Sdrh return pCur->i>=pVtab->pContent->n;
152522efc62Sdrh }
153522efc62Sdrh
154522efc62Sdrh /*
155522efc62Sdrh ** Advance the cursor to the next row.
156522efc62Sdrh */
intarrayNext(sqlite3_vtab_cursor * cur)157522efc62Sdrh static int intarrayNext(sqlite3_vtab_cursor *cur){
158522efc62Sdrh intarray_cursor *pCur = (intarray_cursor *)cur;
159522efc62Sdrh pCur->i++;
160522efc62Sdrh return SQLITE_OK;
161522efc62Sdrh }
162522efc62Sdrh
163522efc62Sdrh /*
164522efc62Sdrh ** Reset a intarray table cursor.
165522efc62Sdrh */
intarrayFilter(sqlite3_vtab_cursor * pVtabCursor,int idxNum,const char * idxStr,int argc,sqlite3_value ** argv)166522efc62Sdrh static int intarrayFilter(
167522efc62Sdrh sqlite3_vtab_cursor *pVtabCursor,
168522efc62Sdrh int idxNum, const char *idxStr,
169522efc62Sdrh int argc, sqlite3_value **argv
170522efc62Sdrh ){
171522efc62Sdrh intarray_cursor *pCur = (intarray_cursor *)pVtabCursor;
172522efc62Sdrh pCur->i = 0;
173522efc62Sdrh return SQLITE_OK;
174522efc62Sdrh }
175522efc62Sdrh
176522efc62Sdrh /*
177522efc62Sdrh ** Analyse the WHERE condition.
178522efc62Sdrh */
intarrayBestIndex(sqlite3_vtab * tab,sqlite3_index_info * pIdxInfo)179522efc62Sdrh static int intarrayBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
180522efc62Sdrh return SQLITE_OK;
181522efc62Sdrh }
182522efc62Sdrh
183522efc62Sdrh /*
184522efc62Sdrh ** A virtual table module that merely echos method calls into TCL
185522efc62Sdrh ** variables.
186522efc62Sdrh */
187522efc62Sdrh static sqlite3_module intarrayModule = {
188522efc62Sdrh 0, /* iVersion */
189522efc62Sdrh intarrayCreate, /* xCreate - create a new virtual table */
190522efc62Sdrh intarrayCreate, /* xConnect - connect to an existing vtab */
191522efc62Sdrh intarrayBestIndex, /* xBestIndex - find the best query index */
192522efc62Sdrh intarrayDestroy, /* xDisconnect - disconnect a vtab */
193522efc62Sdrh intarrayDestroy, /* xDestroy - destroy a vtab */
194522efc62Sdrh intarrayOpen, /* xOpen - open a cursor */
195522efc62Sdrh intarrayClose, /* xClose - close a cursor */
196522efc62Sdrh intarrayFilter, /* xFilter - configure scan constraints */
197522efc62Sdrh intarrayNext, /* xNext - advance a cursor */
198522efc62Sdrh intarrayEof, /* xEof */
199522efc62Sdrh intarrayColumn, /* xColumn - read data */
200522efc62Sdrh intarrayRowid, /* xRowid - read data */
201522efc62Sdrh 0, /* xUpdate */
202522efc62Sdrh 0, /* xBegin */
203522efc62Sdrh 0, /* xSync */
204522efc62Sdrh 0, /* xCommit */
205522efc62Sdrh 0, /* xRollback */
206522efc62Sdrh 0, /* xFindMethod */
207522efc62Sdrh 0, /* xRename */
208522efc62Sdrh };
209522efc62Sdrh
210522efc62Sdrh #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
211522efc62Sdrh
212522efc62Sdrh /*
213522efc62Sdrh ** Invoke this routine to create a specific instance of an intarray object.
214522efc62Sdrh ** The new intarray object is returned by the 3rd parameter.
215522efc62Sdrh **
216522efc62Sdrh ** Each intarray object corresponds to a virtual table in the TEMP table
217522efc62Sdrh ** with a name of zName.
218522efc62Sdrh **
219522efc62Sdrh ** Destroy the intarray object by dropping the virtual table. If not done
220522efc62Sdrh ** explicitly by the application, the virtual table will be dropped implicitly
221522efc62Sdrh ** by the system when the database connection is closed.
222522efc62Sdrh */
sqlite3_intarray_create(sqlite3 * db,const char * zName,sqlite3_intarray ** ppReturn)223fb90841aSdrh SQLITE_API int sqlite3_intarray_create(
224522efc62Sdrh sqlite3 *db,
225522efc62Sdrh const char *zName,
226522efc62Sdrh sqlite3_intarray **ppReturn
227522efc62Sdrh ){
2285c03f30aSdrh int rc = SQLITE_OK;
2295c03f30aSdrh #ifndef SQLITE_OMIT_VIRTUALTABLE
230522efc62Sdrh sqlite3_intarray *p;
231522efc62Sdrh
232f3cdcdccSdrh *ppReturn = p = sqlite3_malloc64( sizeof(*p) );
233522efc62Sdrh if( p==0 ){
234522efc62Sdrh return SQLITE_NOMEM;
235522efc62Sdrh }
236522efc62Sdrh memset(p, 0, sizeof(*p));
237522efc62Sdrh rc = sqlite3_create_module_v2(db, zName, &intarrayModule, p,
238522efc62Sdrh (void(*)(void*))intarrayFree);
239522efc62Sdrh if( rc==SQLITE_OK ){
240522efc62Sdrh char *zSql;
241522efc62Sdrh zSql = sqlite3_mprintf("CREATE VIRTUAL TABLE temp.%Q USING %Q",
242522efc62Sdrh zName, zName);
243522efc62Sdrh rc = sqlite3_exec(db, zSql, 0, 0, 0);
244522efc62Sdrh sqlite3_free(zSql);
245522efc62Sdrh }
2465c03f30aSdrh #endif
247522efc62Sdrh return rc;
248522efc62Sdrh }
249522efc62Sdrh
250522efc62Sdrh /*
251522efc62Sdrh ** Bind a new array array of integers to a specific intarray object.
252522efc62Sdrh **
253522efc62Sdrh ** The array of integers bound must be unchanged for the duration of
254522efc62Sdrh ** any query against the corresponding virtual table. If the integer
255522efc62Sdrh ** array does change or is deallocated undefined behavior will result.
256522efc62Sdrh */
sqlite3_intarray_bind(sqlite3_intarray * pIntArray,int nElements,sqlite3_int64 * aElements,void (* xFree)(void *))257fb90841aSdrh SQLITE_API int sqlite3_intarray_bind(
258522efc62Sdrh sqlite3_intarray *pIntArray, /* The intarray object to bind to */
259522efc62Sdrh int nElements, /* Number of elements in the intarray */
260522efc62Sdrh sqlite3_int64 *aElements, /* Content of the intarray */
261522efc62Sdrh void (*xFree)(void*) /* How to dispose of the intarray when done */
262522efc62Sdrh ){
263522efc62Sdrh if( pIntArray->xFree ){
264522efc62Sdrh pIntArray->xFree(pIntArray->a);
265522efc62Sdrh }
266522efc62Sdrh pIntArray->n = nElements;
267522efc62Sdrh pIntArray->a = aElements;
268522efc62Sdrh pIntArray->xFree = xFree;
269522efc62Sdrh return SQLITE_OK;
270522efc62Sdrh }
271522efc62Sdrh
272522efc62Sdrh
273522efc62Sdrh /*****************************************************************************
274522efc62Sdrh ** Everything below is interface for testing this module.
275522efc62Sdrh */
276522efc62Sdrh #ifdef SQLITE_TEST
27752b1dbb5Smistachkin #if defined(INCLUDE_SQLITE_TCL_H)
27852b1dbb5Smistachkin # include "sqlite_tcl.h"
27952b1dbb5Smistachkin #else
28052b1dbb5Smistachkin # include "tcl.h"
2817617e4a8Smistachkin # ifndef SQLITE_TCLAPI
2827617e4a8Smistachkin # define SQLITE_TCLAPI
2837617e4a8Smistachkin # endif
28452b1dbb5Smistachkin #endif
285522efc62Sdrh
286522efc62Sdrh /*
287522efc62Sdrh ** Routines to encode and decode pointers
288522efc62Sdrh */
289522efc62Sdrh extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
290d8267b89Sdrh extern void *sqlite3TestTextToPtr(const char*);
291522efc62Sdrh extern int sqlite3TestMakePointerStr(Tcl_Interp*, char *zPtr, void*);
292e84d8d32Smistachkin extern const char *sqlite3ErrName(int);
293522efc62Sdrh
294522efc62Sdrh /*
295522efc62Sdrh ** sqlite3_intarray_create DB NAME
296522efc62Sdrh **
297522efc62Sdrh ** Invoke the sqlite3_intarray_create interface. A string that becomes
298522efc62Sdrh ** the first parameter to sqlite3_intarray_bind.
299522efc62Sdrh */
test_intarray_create(ClientData clientData,Tcl_Interp * interp,int objc,Tcl_Obj * CONST objv[])3007617e4a8Smistachkin static int SQLITE_TCLAPI test_intarray_create(
301522efc62Sdrh ClientData clientData, /* Not used */
302522efc62Sdrh Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
303522efc62Sdrh int objc, /* Number of arguments */
304522efc62Sdrh Tcl_Obj *CONST objv[] /* Command arguments */
305522efc62Sdrh ){
306522efc62Sdrh sqlite3 *db;
307522efc62Sdrh const char *zName;
308522efc62Sdrh sqlite3_intarray *pArray;
3095c03f30aSdrh int rc = SQLITE_OK;
310522efc62Sdrh char zPtr[100];
311522efc62Sdrh
312522efc62Sdrh if( objc!=3 ){
313522efc62Sdrh Tcl_WrongNumArgs(interp, 1, objv, "DB");
314522efc62Sdrh return TCL_ERROR;
315522efc62Sdrh }
316522efc62Sdrh if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
317522efc62Sdrh zName = Tcl_GetString(objv[2]);
3185c03f30aSdrh #ifndef SQLITE_OMIT_VIRTUALTABLE
319522efc62Sdrh rc = sqlite3_intarray_create(db, zName, &pArray);
3205c03f30aSdrh #endif
321522efc62Sdrh if( rc!=SQLITE_OK ){
322e84d8d32Smistachkin Tcl_AppendResult(interp, sqlite3ErrName(rc), (char*)0);
323522efc62Sdrh return TCL_ERROR;
324522efc62Sdrh }
325522efc62Sdrh sqlite3TestMakePointerStr(interp, zPtr, pArray);
326522efc62Sdrh Tcl_AppendResult(interp, zPtr, (char*)0);
327522efc62Sdrh return TCL_OK;
328522efc62Sdrh }
329522efc62Sdrh
330522efc62Sdrh /*
331522efc62Sdrh ** sqlite3_intarray_bind INTARRAY ?VALUE ...?
332522efc62Sdrh **
333522efc62Sdrh ** Invoke the sqlite3_intarray_bind interface on the given array of integers.
334522efc62Sdrh */
test_intarray_bind(ClientData clientData,Tcl_Interp * interp,int objc,Tcl_Obj * CONST objv[])3357617e4a8Smistachkin static int SQLITE_TCLAPI test_intarray_bind(
336522efc62Sdrh ClientData clientData, /* Not used */
337522efc62Sdrh Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
338522efc62Sdrh int objc, /* Number of arguments */
339522efc62Sdrh Tcl_Obj *CONST objv[] /* Command arguments */
340522efc62Sdrh ){
341522efc62Sdrh sqlite3_intarray *pArray;
3425c03f30aSdrh int rc = SQLITE_OK;
343522efc62Sdrh int i, n;
344522efc62Sdrh sqlite3_int64 *a;
345522efc62Sdrh
346522efc62Sdrh if( objc<2 ){
347522efc62Sdrh Tcl_WrongNumArgs(interp, 1, objv, "INTARRAY");
348522efc62Sdrh return TCL_ERROR;
349522efc62Sdrh }
350522efc62Sdrh pArray = (sqlite3_intarray*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
351522efc62Sdrh n = objc - 2;
3525c03f30aSdrh #ifndef SQLITE_OMIT_VIRTUALTABLE
353f3cdcdccSdrh a = sqlite3_malloc64( sizeof(a[0])*n );
354522efc62Sdrh if( a==0 ){
355522efc62Sdrh Tcl_AppendResult(interp, "SQLITE_NOMEM", (char*)0);
356522efc62Sdrh return TCL_ERROR;
357522efc62Sdrh }
358522efc62Sdrh for(i=0; i<n; i++){
359b3f787f4Sdrh Tcl_WideInt x = 0;
360b3f787f4Sdrh Tcl_GetWideIntFromObj(0, objv[i+2], &x);
361b3f787f4Sdrh a[i] = x;
362522efc62Sdrh }
363522efc62Sdrh rc = sqlite3_intarray_bind(pArray, n, a, sqlite3_free);
364522efc62Sdrh if( rc!=SQLITE_OK ){
365e84d8d32Smistachkin Tcl_AppendResult(interp, sqlite3ErrName(rc), (char*)0);
366522efc62Sdrh return TCL_ERROR;
367522efc62Sdrh }
3685c03f30aSdrh #endif
369522efc62Sdrh return TCL_OK;
370522efc62Sdrh }
371522efc62Sdrh
372522efc62Sdrh /*
373522efc62Sdrh ** Register commands with the TCL interpreter.
374522efc62Sdrh */
Sqlitetestintarray_Init(Tcl_Interp * interp)375522efc62Sdrh int Sqlitetestintarray_Init(Tcl_Interp *interp){
376522efc62Sdrh static struct {
377522efc62Sdrh char *zName;
378522efc62Sdrh Tcl_ObjCmdProc *xProc;
379522efc62Sdrh void *clientData;
380522efc62Sdrh } aObjCmd[] = {
381522efc62Sdrh { "sqlite3_intarray_create", test_intarray_create, 0 },
382522efc62Sdrh { "sqlite3_intarray_bind", test_intarray_bind, 0 },
383522efc62Sdrh };
384522efc62Sdrh int i;
385522efc62Sdrh for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
386522efc62Sdrh Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
387522efc62Sdrh aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
388522efc62Sdrh }
389522efc62Sdrh return TCL_OK;
390522efc62Sdrh }
391522efc62Sdrh
392522efc62Sdrh #endif /* SQLITE_TEST */
393