From 522efc62b09255d1c4f676529e637fa0b0514860 Mon Sep 17 00:00:00 2001 From: drh Date: Tue, 10 Nov 2009 17:24:37 +0000 Subject: [PATCH] Initial implementation and test cases for the "intarray" virtual table. FossilOrigin-Name: 0e7d29253f093d4125ede3cfc478e4bf09aef987 --- Makefile.in | 1 + main.mk | 1 + manifest | 31 ++-- manifest.uuid | 2 +- src/tclsqlite.c | 2 + src/test_intarray.c | 375 ++++++++++++++++++++++++++++++++++++++++++++ src/test_intarray.h | 114 ++++++++++++++ test/intarray.test | 62 ++++++++ 8 files changed, 578 insertions(+), 10 deletions(-) create mode 100644 src/test_intarray.c create mode 100644 src/test_intarray.h create mode 100644 test/intarray.test diff --git a/Makefile.in b/Makefile.in index 15bada28e3..542f6bae3c 100644 --- a/Makefile.in +++ b/Makefile.in @@ -383,6 +383,7 @@ TESTSRC = \ $(TOP)/src/test_func.c \ $(TOP)/src/test_hexio.c \ $(TOP)/src/test_init.c \ + $(TOP)/src/test_intarray.c \ $(TOP)/src/test_journal.c \ $(TOP)/src/test_malloc.c \ $(TOP)/src/test_mutex.c \ diff --git a/main.mk b/main.mk index 5e1d1540db..e4fcaade05 100644 --- a/main.mk +++ b/main.mk @@ -231,6 +231,7 @@ TESTSRC = \ $(TOP)/src/test_func.c \ $(TOP)/src/test_hexio.c \ $(TOP)/src/test_init.c \ + $(TOP)/src/test_intarray.c \ $(TOP)/src/test_journal.c \ $(TOP)/src/test_malloc.c \ $(TOP)/src/test_mutex.c \ diff --git a/manifest b/manifest index 2cbae61693..fb20da70b7 100644 --- a/manifest +++ b/manifest @@ -1,7 +1,10 @@ -C Fix\sfor\sdatbase\sname\sbeing\signored\swith\sthe\s-batch\soption\sin\sthe\sCLI.\s\sTicket\s[aeff892c57]. -D 2009-11-10T17:07:31 +-----BEGIN PGP SIGNED MESSAGE----- +Hash: SHA1 + +C Initial\simplementation\sand\stest\scases\sfor\sthe\s"intarray"\svirtual\stable. +D 2009-11-10T17:24:37 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 -F Makefile.in b24a5d5f12bd6035d120c15f5e78e5d60ee4dd28 +F Makefile.in 53f3dfa49f28ab5b80cb083fb7c9051e596bcfa1 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 F Makefile.vxworks 10010ddbf52e2503c7c49c7c0b7c7a096f8638a6 F README cd04a36fbc7ea56932a4052d7d0b7f09f27c33d6 @@ -86,7 +89,7 @@ F ext/rtree/tkt3363.test 2bf324f7908084a5f463de3109db9c6e607feb1b F ext/rtree/viewrtree.tcl eea6224b3553599ae665b239bd827e182b466024 F install-sh 9d4de14ab9fb0facae2f48780b874848cbf2f895 F ltmain.sh 3ff0879076df340d2e23ae905484d8c15d5fdea8 -F main.mk d588eab5d51b0bfe924a1cccdfdd2cbb4cbe40b4 +F main.mk 0320def78eed84285c273f3d84dd20d8f26a0139 F mkdll.sh 7d09b23c05d56532e9d44a50868eb4b12ff4f74a F mkextu.sh 416f9b7089d80e5590a29692c9d9280a10dbad9f F mkextw.sh 4123480947681d9b434a5e7b1ee08135abe409ac @@ -168,7 +171,7 @@ F src/sqliteInt.h 55d26b83d16107804693982028c8b3a075f5a836 F src/sqliteLimit.h 3afab2291762b5d09ae20c18feb8e9fa935a60a6 F src/status.c e651be6b30d397d86384c6867bc016e4913bcac7 F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e -F src/tclsqlite.c 21432a361808b6c3d405710cdd22fa1b16f7b84f +F src/tclsqlite.c 223746524139e1e02b7a2b61c05b5a690f91a0ff F src/test1.c db4d8fd2849ab9aca0f27fd3773b8d68d078cf86 F src/test2.c b6b43413d495addd039a88b87d65c839f86b18cb F src/test3.c f17eeaf8114205844d76f4e69bab27ea341087af @@ -187,6 +190,8 @@ F src/test_devsym.c de3c9af2bb9a8b1e44525c449e4ec3f88e3d4110 F src/test_func.c 1c94388a23d4a9e7cd62ec79d612d1bae2451fa2 F src/test_hexio.c 1c0f4238c6fb36c890ce7c07d9c8e1cecedad9ad F src/test_init.c 5d624ffd0409d424cf9adbfe1f056b200270077c +F src/test_intarray.c 52862866211f2f8813471e6ac24f17afe1ddf2b7 +F src/test_intarray.h 489edb9068bb926583445cb02589344961054207 F src/test_journal.c adc0ce3840ed19b49feb1d583b2212f560ef7866 F src/test_loadext.c df586c27176e3c2cb2e099c78da67bf14379a56e F src/test_malloc.c f777d15df756bea0e98271932464ac5d882e66fe @@ -421,6 +426,7 @@ F test/insert2.test 4f3a04d168c728ed5ec2c88842e772606c7ce435 F test/insert3.test 1b7db95a03ad9c5013fdf7d6722b6cd66ee55e30 F test/insert4.test 6e382eaf7295a4463e6f29ea20fcd8e63d097eeb F test/insert5.test 1f93cbe9742110119133d7e8e3ccfe6d7c249766 +F test/intarray.test 829a72b2bf7329c969703788e4355f1d9870116a F test/interrupt.test 42e7cf98646fd9cb4a3b131a93ed3c50b9e149f1 F test/intpkey.test 537669fd535f62632ca64828e435b9e54e8d677f F test/io.test e7bd58edb4e2131a8ecd81b4b00af3ee5c79d464 @@ -763,7 +769,14 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f -P f6c045f649036958078cb15cd9d5453680c82b0c -R 1f800c450b12aea8b2bafa846301d578 -U shaneh -Z 90330abd89db4a61b87a266140e2ee99 +P 8097c64acfc74c913bc6d7d631bc1423c90df93f +R 8b070e35abc0273e79586bc251b3aec5 +U drh +Z 28d3772e2493768aa7f3896b180a9b39 +-----BEGIN PGP SIGNATURE----- +Version: GnuPG v1.4.6 (GNU/Linux) + +iD8DBQFK+aHXoxKgR168RlERAm2qAJ958rJsRjQOpNvspuJfXRgPM0fPugCghSop +UuOC37hwMLT7++0J6JCbgaM= +=j9AA +-----END PGP SIGNATURE----- diff --git a/manifest.uuid b/manifest.uuid index 72b465c54d..992255f59d 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -8097c64acfc74c913bc6d7d631bc1423c90df93f \ No newline at end of file +0e7d29253f093d4125ede3cfc478e4bf09aef987 \ No newline at end of file diff --git a/src/tclsqlite.c b/src/tclsqlite.c index 604e937952..9834779788 100644 --- a/src/tclsqlite.c +++ b/src/tclsqlite.c @@ -3488,6 +3488,7 @@ int TCLSH_MAIN(int argc, char **argv){ extern int SqlitetestOnefile_Init(); extern int SqlitetestOsinst_Init(Tcl_Interp*); extern int Sqlitetestbackup_Init(Tcl_Interp*); + extern int Sqlitetestintarray_Init(Tcl_Interp*); Sqliteconfig_Init(interp); Sqlitetest1_Init(interp); @@ -3512,6 +3513,7 @@ int TCLSH_MAIN(int argc, char **argv){ SqlitetestOnefile_Init(interp); SqlitetestOsinst_Init(interp); Sqlitetestbackup_Init(interp); + Sqlitetestintarray_Init(interp); #ifdef SQLITE_SSE Sqlitetestsse_Init(interp); diff --git a/src/test_intarray.c b/src/test_intarray.c new file mode 100644 index 0000000000..4abe654bc1 --- /dev/null +++ b/src/test_intarray.c @@ -0,0 +1,375 @@ +/* +** 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 "test_intarray.h" +#include +#include + + +/* +** 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; + +/* A intarray table object */ +struct intarray_vtab { + sqlite3_vtab base; /* Base class */ + sqlite3_intarray *pContent; /* Content of the integer array */ +}; + +/* A 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_malloc(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_malloc(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->ipContent->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; + return SQLITE_OK; +} + +/* +** Analyse the WHERE condition. +*/ +static int intarrayBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ + 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. +*/ +int sqlite3_intarray_create( + sqlite3 *db, + const char *zName, + sqlite3_intarray **ppReturn +){ + int rc; + sqlite3_intarray *p; + + *ppReturn = p = sqlite3_malloc( 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); + } + 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. +*/ +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; +} + + +/***************************************************************************** +** Everything below is interface for testing this module. +*/ +#ifdef SQLITE_TEST +#include + +/* +** Routines to encode and decode pointers +*/ +extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb); +extern int sqlite3TestTextToPtr(const char*); +extern int sqlite3TestMakePointerStr(Tcl_Interp*, char *zPtr, void*); +extern const char *sqlite3TestErrorName(int); + +/* +** sqlite3_intarray_create DB NAME +** +** Invoke the sqlite3_intarray_create interface. A string that becomes +** the first parameter to sqlite3_intarray_bind. +*/ +static int test_intarray_create( + ClientData clientData, /* Not used */ + Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ + int objc, /* Number of arguments */ + Tcl_Obj *CONST objv[] /* Command arguments */ +){ + sqlite3 *db; + const char *zName; + sqlite3_intarray *pArray; + int rc; + char zPtr[100]; + + if( objc!=3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB"); + return TCL_ERROR; + } + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + zName = Tcl_GetString(objv[2]); + rc = sqlite3_intarray_create(db, zName, &pArray); + if( rc!=SQLITE_OK ){ + assert( pArray==0 ); + Tcl_AppendResult(interp, sqlite3TestErrorName(rc), (char*)0); + return TCL_ERROR; + } + sqlite3TestMakePointerStr(interp, zPtr, pArray); + Tcl_AppendResult(interp, zPtr, (char*)0); + return TCL_OK; +} + +/* +** sqlite3_intarray_bind INTARRAY ?VALUE ...? +** +** Invoke the sqlite3_intarray_bind interface on the given array of integers. +*/ +static int test_intarray_bind( + ClientData clientData, /* Not used */ + Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ + int objc, /* Number of arguments */ + Tcl_Obj *CONST objv[] /* Command arguments */ +){ + sqlite3_intarray *pArray; + int rc; + int i, n; + sqlite3_int64 *a; + + if( objc<2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "INTARRAY"); + return TCL_ERROR; + } + pArray = (sqlite3_intarray*)sqlite3TestTextToPtr(Tcl_GetString(objv[1])); + n = objc - 2; + a = sqlite3_malloc( sizeof(a[0])*n ); + if( a==0 ){ + Tcl_AppendResult(interp, "SQLITE_NOMEM", (char*)0); + return TCL_ERROR; + } + for(i=0; i