From 4841624ab991a90dc84cb48cec9abcfe6f885dbf Mon Sep 17 00:00:00 2001 From: drh Date: Wed, 29 Jun 2016 05:00:30 +0000 Subject: [PATCH 1/5] Add a prototype intarray($PTR,$N) table valued function. FossilOrigin-Name: 233b33382dc70de45f90b6dfdb5785f20b21489e --- Makefile.in | 1 + ext/misc/array.c | 296 ++++++++++++++++++++++++++++++++++++++++++++ manifest | 20 +-- manifest.uuid | 2 +- src/test1.c | 43 +++++++ test/tabfunc01.test | 22 ++++ 6 files changed, 375 insertions(+), 9 deletions(-) create mode 100644 ext/misc/array.c diff --git a/Makefile.in b/Makefile.in index b9f684e617..ff863ec906 100644 --- a/Makefile.in +++ b/Makefile.in @@ -416,6 +416,7 @@ TESTSRC = \ # TESTSRC += \ $(TOP)/ext/misc/amatch.c \ + $(TOP)/ext/misc/array.c \ $(TOP)/ext/misc/closure.c \ $(TOP)/ext/misc/csv.c \ $(TOP)/ext/misc/eval.c \ diff --git a/ext/misc/array.c b/ext/misc/array.c new file mode 100644 index 0000000000..54b249f90a --- /dev/null +++ b/ext/misc/array.c @@ -0,0 +1,296 @@ +/* +** 2016-06-29 +** +** 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 demonstrates how to create a table-valued-function that +** returns the values in a C-language array. +** Examples: +** +** SELECT * FROM intarray($ptr,5) +** +** The query above returns 5 integers contained in a C-language array +** at the address $ptr. $ptr is a pointer to the array of integers that +** has been cast to an integer. +** +** HOW IT WORKS +** +** The intarray "function" is really a virtual table with the +** following schema: +** +** CREATE FUNCTION intarray( +** value, +** pointer HIDDEN, +** count HIDDEN +** ); +*/ +#include "sqlite3ext.h" +SQLITE_EXTENSION_INIT1 +#include +#include + +#ifndef SQLITE_OMIT_VIRTUALTABLE + + +/* intarray_cursor is a subclass of sqlite3_vtab_cursor which will +** serve as the underlying representation of a cursor that scans +** over rows of the result +*/ +typedef struct intarray_cursor intarray_cursor; +struct intarray_cursor { + sqlite3_vtab_cursor base; /* Base class - must be first */ + int isDesc; /* True to count down rather than up */ + sqlite3_int64 iRowid; /* The rowid */ + sqlite3_int64 iPtr; /* Pointer to array of integers */ + sqlite3_int64 iCnt; /* Number of integers in the array */ +}; + +/* +** The intarrayConnect() method is invoked to create a new +** intarray_vtab that describes the intarray virtual table. +** +** Think of this routine as the constructor for intarray_vtab objects. +** +** All this routine needs to do is: +** +** (1) Allocate the intarray_vtab object and initialize all fields. +** +** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the +** result set of queries against intarray will look like. +*/ +static int intarrayConnect( + sqlite3 *db, + void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVtab, + char **pzErr +){ + sqlite3_vtab *pNew; + int rc; + +/* Column numbers */ +#define INTARRAY_COLUMN_VALUE 0 +#define INTARRAY_COLUMN_POINTER 1 +#define INTARRAY_COLUMN_COUNT 2 + + rc = sqlite3_declare_vtab(db, + "CREATE TABLE x(value,pointer hidden,count hidden)"); + if( rc==SQLITE_OK ){ + pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) ); + if( pNew==0 ) return SQLITE_NOMEM; + memset(pNew, 0, sizeof(*pNew)); + } + return rc; +} + +/* +** This method is the destructor for intarray_cursor objects. +*/ +static int intarrayDisconnect(sqlite3_vtab *pVtab){ + sqlite3_free(pVtab); + return SQLITE_OK; +} + +/* +** Constructor for a new intarray_cursor object. +*/ +static int intarrayOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ + intarray_cursor *pCur; + pCur = sqlite3_malloc( sizeof(*pCur) ); + if( pCur==0 ) return SQLITE_NOMEM; + memset(pCur, 0, sizeof(*pCur)); + *ppCursor = &pCur->base; + return SQLITE_OK; +} + +/* +** Destructor for a intarray_cursor. +*/ +static int intarrayClose(sqlite3_vtab_cursor *cur){ + sqlite3_free(cur); + return SQLITE_OK; +} + + +/* +** Advance a intarray_cursor to its next row of output. +*/ +static int intarrayNext(sqlite3_vtab_cursor *cur){ + intarray_cursor *pCur = (intarray_cursor*)cur; + pCur->iRowid++; + return SQLITE_OK; +} + +/* +** Return values of columns for the row at which the intarray_cursor +** is currently pointing. +*/ +static int intarrayColumn( + sqlite3_vtab_cursor *cur, /* The cursor */ + sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ + int i /* Which column to return */ +){ + intarray_cursor *pCur = (intarray_cursor*)cur; + sqlite3_int64 x = 0; + switch( i ){ + case INTARRAY_COLUMN_POINTER: x = pCur->iPtr; break; + case INTARRAY_COLUMN_COUNT: x = pCur->iCnt; break; + default: { + int *p = (int*)pCur->iPtr; + x = (int)p[pCur->iRowid-1]; + break; + } + } + sqlite3_result_int64(ctx, x); + return SQLITE_OK; +} + +/* +** Return the rowid for the current row. In this implementation, the +** rowid is the same as the output value. +*/ +static int intarrayRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ + intarray_cursor *pCur = (intarray_cursor*)cur; + *pRowid = pCur->iRowid; + return SQLITE_OK; +} + +/* +** Return TRUE if the cursor has been moved off of the last +** row of output. +*/ +static int intarrayEof(sqlite3_vtab_cursor *cur){ + intarray_cursor *pCur = (intarray_cursor*)cur; + return pCur->iRowid>=pCur->iCnt; +} + +/* +** This method is called to "rewind" the intarray_cursor object back +** to the first row of output. +*/ +static int intarrayFilter( + sqlite3_vtab_cursor *pVtabCursor, + int idxNum, const char *idxStr, + int argc, sqlite3_value **argv +){ + intarray_cursor *pCur = (intarray_cursor *)pVtabCursor; + int i = 0; + if( idxNum ){ + pCur->iPtr = sqlite3_value_int64(argv[0]); + pCur->iCnt = sqlite3_value_int64(argv[1]); + }else{ + pCur->iPtr = 0; + pCur->iCnt = 0; + } + pCur->iRowid = 1; + return SQLITE_OK; +} + +/* +** SQLite will invoke this method one or more times while planning a query +** that uses the intarray virtual table. This routine needs to create +** a query plan for each invocation and compute an estimated cost for that +** plan. +** +** In this implementation idxNum is used to represent the +** query plan. idxStr is unused. +** +** idxNum is 1 if the pointer= and count= constraints exist and is 0 otherwise. +** If idxNum is 0, then intarray becomes an empty table. +*/ +static int intarrayBestIndex( + sqlite3_vtab *tab, + sqlite3_index_info *pIdxInfo +){ + int i; /* Loop over constraints */ + int idxNum = 0; /* The query plan bitmask */ + int ptrIdx = -1; /* Index of the pointer= constraint, or -1 if none */ + int cntIdx = -1; /* Index of the count= constraint, or -1 if none */ + int nArg = 0; /* Number of arguments that intarrayFilter() expects */ + + const struct sqlite3_index_constraint *pConstraint; + pConstraint = pIdxInfo->aConstraint; + for(i=0; inConstraint; i++, pConstraint++){ + if( pConstraint->usable==0 ) continue; + if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; + switch( pConstraint->iColumn ){ + case INTARRAY_COLUMN_POINTER: + ptrIdx = i; + break; + case INTARRAY_COLUMN_COUNT: + cntIdx = i; + break; + } + } + if( ptrIdx>=0 && cntIdx>=0 ){ + pIdxInfo->aConstraintUsage[ptrIdx].argvIndex = 1; + pIdxInfo->aConstraintUsage[ptrIdx].omit = 1; + pIdxInfo->aConstraintUsage[cntIdx].argvIndex = 2; + pIdxInfo->aConstraintUsage[cntIdx].omit = 1; + pIdxInfo->estimatedCost = (double)1; + pIdxInfo->estimatedRows = (double)100; + pIdxInfo->idxNum = 1; + }else{ + pIdxInfo->estimatedCost = (double)2147483647; + pIdxInfo->estimatedRows = (double)2147483647; + pIdxInfo->idxNum = 0; + } + return SQLITE_OK; +} + +/* +** This following structure defines all the methods for the +** intarray virtual table. +*/ +static sqlite3_module intarrayModule = { + 0, /* iVersion */ + 0, /* xCreate */ + intarrayConnect, /* xConnect */ + intarrayBestIndex, /* xBestIndex */ + intarrayDisconnect, /* xDisconnect */ + 0, /* xDestroy */ + intarrayOpen, /* xOpen - open a cursor */ + intarrayClose, /* xClose - close a cursor */ + intarrayFilter, /* xFilter - configure scan constraints */ + intarrayNext, /* xNext - advance a cursor */ + intarrayEof, /* xEof - check for end of scan */ + intarrayColumn, /* xColumn - read data */ + intarrayRowid, /* xRowid - read data */ + 0, /* xUpdate */ + 0, /* xBegin */ + 0, /* xSync */ + 0, /* xCommit */ + 0, /* xRollback */ + 0, /* xFindMethod */ + 0, /* xRename */ +}; + +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +#ifdef _WIN32 +__declspec(dllexport) +#endif +int sqlite3_array_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ + int rc = SQLITE_OK; + SQLITE_EXTENSION_INIT2(pApi); +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( sqlite3_libversion_number()<3008012 ){ + *pzErrMsg = sqlite3_mprintf( + "intarray() requires SQLite 3.8.12 or later"); + return SQLITE_ERROR; + } + rc = sqlite3_create_module(db, "intarray", &intarrayModule, 0); +#endif + return rc; +} diff --git a/manifest b/manifest index 2dd253cf30..f6356d3145 100644 --- a/manifest +++ b/manifest @@ -1,6 +1,6 @@ -C Prevent\sthe\sWhereLoop.rSetup\scost\sestimate\sfrom\sgoing\snegative\son\scomplex\nqueries. -D 2016-06-26T04:06:28.081 -F Makefile.in bc2b4864a23a4a21c3e26d7b4350f51bab324d45 +C Add\sa\sprototype\sintarray($PTR,$N)\stable\svalued\sfunction. +D 2016-06-29T05:00:30.819 +F Makefile.in 541d493154ec3b0b20b2f1d495ec66f55905191e F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc 50149765ef72f4e652b9a0f1f6462c4784bb9423 F README.md 8ecc12493ff9f820cdea6520a9016001cb2e59b7 @@ -204,6 +204,7 @@ F ext/icu/README.txt d9fbbad0c2f647c3fdf715fc9fd64af53aedfc43 F ext/icu/icu.c 43df9d8ef2fae7a325100ebd713ab089dc829dd7 F ext/icu/sqliteicu.h 728867a802baa5a96de7495e9689a8e01715ef37 F ext/misc/amatch.c 211108e201105e4bb0c076527b8cfd34330fc234 +F ext/misc/array.c 20af0591e6611755dd8a9d1124e9c9a8cf42761f F ext/misc/closure.c 0d2a038df8fbae7f19de42e7c7d71f2e4dc88704 F ext/misc/compress.c 122faa92d25033d6c3f07c39231de074ab3d2e83 F ext/misc/csv.c f01126ba170fd4ef7c752b156568a80c912d4441 @@ -392,7 +393,7 @@ F src/sqliteLimit.h c0373387c287c8d0932510b5547ecde31b5da247 F src/status.c 70912d7be68e9e2dbc4010c93d344af61d4c59ba F src/table.c 5226df15ab9179b9ed558d89575ea0ce37b03fc9 F src/tclsqlite.c 25fbbbb97f76dbfd113153fb63f52d7ecfac5dd0 -F src/test1.c 43b37ab2b7338fd3313e74902f0d6c821eae843b +F src/test1.c 081e4ed40525590406a51f7e7e4cee31cdb5d029 F src/test2.c 5586f43fcd9a1be0830793cf9d354082c261b25b F src/test3.c c75c8af0eadb335236c9e61b51044c58a8f7dd59 F src/test4.c d168f83cc78d02e8d35567bb5630e40dcd85ac1e @@ -1114,7 +1115,7 @@ F test/symlink.test c9ebe7330d228249e447038276bfc8a7b22f4849 F test/sync.test 2f84bdbc2b2df1fcb0220575b4b9f8cea94b7529 F test/syscall.test f59ba4e25f7ba4a4c031026cc2ef8b6e4b4c639c F test/sysfault.test c9f2b0d8d677558f74de750c75e12a5454719d04 -F test/tabfunc01.test f977868fa8bb7beb4b2072883190411653473906 +F test/tabfunc01.test a1976cbc37cbcdd4b4bd1e52d19a173dd62ab9e0 F test/table.test b708f3e5fa2542fa51dfab21fc07b36ea445cb2f F test/tableapi.test 2674633fa95d80da917571ebdd759a14d9819126 F test/tableopts.test dba698ba97251017b7c80d738c198d39ab747930 @@ -1502,7 +1503,10 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 507014e4c7a70cd09410c89c8ed466c8edab39d2 -R ae55d782914cbf2db58c75f82f862316 +P f81050859170c8708a1b296da8dd3ef0dd314a11 +R 609a6d062299b01b515e433d814c02e8 +T *branch * prototype-int-array +T *sym-prototype-int-array * +T -sym-trunk * U drh -Z e21bca843071316b56d3c29d5bf9cc08 +Z 45472ddb0e35914c318f364b2c8020fb diff --git a/manifest.uuid b/manifest.uuid index ee49eb40ac..2ff393eb58 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -f81050859170c8708a1b296da8dd3ef0dd314a11 \ No newline at end of file +233b33382dc70de45f90b6dfdb5785f20b21489e \ No newline at end of file diff --git a/src/test1.c b/src/test1.c index 0f16d62d85..9a73e0e0b5 100644 --- a/src/test1.c +++ b/src/test1.c @@ -3241,6 +3241,46 @@ static int test_bind_int( } +/* +** Usage: sqlite3_bind_intarray STMT N INT ... +** +** Create a C-language array of integers from the arguments. Bind a pointer +** to this array to the NAME parameter of STMT. +*/ +static int test_bind_intarray( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3_stmt *pStmt; + int idx; + int i; + static int *p = 0; + + sqlite3_free(p); + p = 0; + if( objc<4 ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", + Tcl_GetStringFromObj(objv[0], 0), " STMT NAME INT...", 0); + return TCL_ERROR; + } + + if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; + if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; + p = sqlite3_malloc( sizeof(int)*(objc-3) ); + if( p==0 ) return TCL_ERROR; + for(i=0; i Date: Wed, 29 Jun 2016 05:08:01 +0000 Subject: [PATCH 2/5] Another test case for the intarray($PTR,$N) virtual table. FossilOrigin-Name: 06e1fab7527c6b4330a58f8d8873afaf2d67ae86 --- manifest | 15 ++++++--------- manifest.uuid | 2 +- test/tabfunc01.test | 23 +++++++++++++++++++++++ 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/manifest b/manifest index f6356d3145..defdea7bc4 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\sa\sprototype\sintarray($PTR,$N)\stable\svalued\sfunction. -D 2016-06-29T05:00:30.819 +C Another\stest\scase\sfor\sthe\sintarray($PTR,$N)\svirtual\stable. +D 2016-06-29T05:08:01.847 F Makefile.in 541d493154ec3b0b20b2f1d495ec66f55905191e F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc 50149765ef72f4e652b9a0f1f6462c4784bb9423 @@ -1115,7 +1115,7 @@ F test/symlink.test c9ebe7330d228249e447038276bfc8a7b22f4849 F test/sync.test 2f84bdbc2b2df1fcb0220575b4b9f8cea94b7529 F test/syscall.test f59ba4e25f7ba4a4c031026cc2ef8b6e4b4c639c F test/sysfault.test c9f2b0d8d677558f74de750c75e12a5454719d04 -F test/tabfunc01.test a1976cbc37cbcdd4b4bd1e52d19a173dd62ab9e0 +F test/tabfunc01.test bb0ef0fdf5e807c5bc3438c8c4face4c9887971d F test/table.test b708f3e5fa2542fa51dfab21fc07b36ea445cb2f F test/tableapi.test 2674633fa95d80da917571ebdd759a14d9819126 F test/tableopts.test dba698ba97251017b7c80d738c198d39ab747930 @@ -1503,10 +1503,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P f81050859170c8708a1b296da8dd3ef0dd314a11 -R 609a6d062299b01b515e433d814c02e8 -T *branch * prototype-int-array -T *sym-prototype-int-array * -T -sym-trunk * +P 233b33382dc70de45f90b6dfdb5785f20b21489e +R 67bef7ed0b382234904201d45ecddcca U drh -Z 45472ddb0e35914c318f364b2c8020fb +Z bbb146cf4e7f714032732261f87e16f3 diff --git a/manifest.uuid b/manifest.uuid index 2ff393eb58..ba2f320cf9 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -233b33382dc70de45f90b6dfdb5785f20b21489e \ No newline at end of file +06e1fab7527c6b4330a58f8d8873afaf2d67ae86 \ No newline at end of file diff --git a/test/tabfunc01.test b/test/tabfunc01.test index 473f3f385c..d0de104d81 100644 --- a/test/tabfunc01.test +++ b/test/tabfunc01.test @@ -155,6 +155,29 @@ do_test tabfunc01-621 { sqlite3_column_int $VM 0 } 22 sqlite3_finalize $VM + +do_test tabfunc01-650 { + db eval { + DROP TABLE IF EXISTS t6; + CREATE TABLE t6(x INTEGER PRIMARY KEY, y BLOB); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100) + INSERT INTO t6(x,y) SELECT x, randomblob(x) FROM c; + } + set TAIL {} + set VM [sqlite3_prepare db { + SELECT length(y) FROM t6 WHERE x IN (SELECT value FROM intarray(?1,3)); + } -1 TAIL] + string trim $TAIL +} {} +do_test tabfunc01-660 { + sqlite3_bind_intarray $VM 1 11 22 33 44 55 + sqlite3_step $VM +} SQLITE_ROW +do_test tabfunc01-661 { + sqlite3_column_int $VM 0 +} 11 +sqlite3_finalize $VM + catch {sqlite3_bind_intarray} finish_test From 2d05331444e0feb269a4651ba2f8c47f4a5ad6e2 Mon Sep 17 00:00:00 2001 From: drh Date: Wed, 29 Jun 2016 06:19:19 +0000 Subject: [PATCH 3/5] Add the ability to have a table-valued function on the RHS of an IN operator. FossilOrigin-Name: ba1b441b6003808810667d749635fe6b2e8c6165 --- manifest | 14 +++++++------- manifest.uuid | 2 +- src/parse.y | 9 +++++++++ test/tabfunc01.test | 16 ++++++++++++++++ 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/manifest b/manifest index defdea7bc4..3973bf7b86 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Another\stest\scase\sfor\sthe\sintarray($PTR,$N)\svirtual\stable. -D 2016-06-29T05:08:01.847 +C Add\sthe\sability\sto\shave\sa\stable-valued\sfunction\son\sthe\sRHS\sof\san\sIN\soperator. +D 2016-06-29T06:19:19.192 F Makefile.in 541d493154ec3b0b20b2f1d495ec66f55905191e F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc 50149765ef72f4e652b9a0f1f6462c4784bb9423 @@ -372,7 +372,7 @@ F src/os_win.c d4b8faf8896b65818e67070711fdd00d8e620bd6 F src/os_win.h eb7a47aa17b26b77eb97e4823f20a00b8bda12ca F src/pager.c c368634b888b1c8740aea83b36bfd266f2443e60 F src/pager.h 8ab6b6feeee4bc0439bfde7ee59ba99df98b9bc3 -F src/parse.y 01b9f37c4c7009ab56fda98bc7db4c42643cecfe +F src/parse.y 300a30380bfa73c0a0d2e0ae4c4d0cd844d80199 F src/pcache.c 5583c8ade4b05075a60ba953ef471d1c1a9c05df F src/pcache.h 2cedcd8407eb23017d92790b112186886e179490 F src/pcache1.c 7f51d2b541aab57596adf62db2c4bb025d34f04d @@ -1115,7 +1115,7 @@ F test/symlink.test c9ebe7330d228249e447038276bfc8a7b22f4849 F test/sync.test 2f84bdbc2b2df1fcb0220575b4b9f8cea94b7529 F test/syscall.test f59ba4e25f7ba4a4c031026cc2ef8b6e4b4c639c F test/sysfault.test c9f2b0d8d677558f74de750c75e12a5454719d04 -F test/tabfunc01.test bb0ef0fdf5e807c5bc3438c8c4face4c9887971d +F test/tabfunc01.test 32e620a19963c50dcf6958dc5bb352de96d948fb F test/table.test b708f3e5fa2542fa51dfab21fc07b36ea445cb2f F test/tableapi.test 2674633fa95d80da917571ebdd759a14d9819126 F test/tableopts.test dba698ba97251017b7c80d738c198d39ab747930 @@ -1503,7 +1503,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 233b33382dc70de45f90b6dfdb5785f20b21489e -R 67bef7ed0b382234904201d45ecddcca +P 06e1fab7527c6b4330a58f8d8873afaf2d67ae86 +R a95030951795f74126be51a632f80a74 U drh -Z bbb146cf4e7f714032732261f87e16f3 +Z 5d31abe2efd2f25b296efdbc945337ef diff --git a/manifest.uuid b/manifest.uuid index ba2f320cf9..6d65c9b627 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -06e1fab7527c6b4330a58f8d8873afaf2d67ae86 \ No newline at end of file +ba1b441b6003808810667d749635fe6b2e8c6165 \ No newline at end of file diff --git a/src/parse.y b/src/parse.y index 9294a30428..d69bd6841a 100644 --- a/src/parse.y +++ b/src/parse.y @@ -1142,6 +1142,15 @@ expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] { exprNot(pParse, N, &A); A.zEnd = Z.z ? &Z.z[Z.n] : &Y.z[Y.n]; } + expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) LP exprlist(E) RP. [IN] { + SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&Y,&Z); + Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0); + sqlite3SrcListFuncArgs(pParse, pSrc, E); + A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0, 0); + sqlite3PExprAddSelect(pParse, A.pExpr, pSelect); + exprNot(pParse, N, &A); + A.zEnd = Z.z ? &Z.z[Z.n] : &Y.z[Y.n]; + } expr(A) ::= EXISTS(B) LP select(Y) RP(E). { Expr *p; spanSet(&A,&B,&E); /*A-overwrites-B*/ diff --git a/test/tabfunc01.test b/test/tabfunc01.test index d0de104d81..a0aae155bd 100644 --- a/test/tabfunc01.test +++ b/test/tabfunc01.test @@ -178,6 +178,22 @@ do_test tabfunc01-661 { } 11 sqlite3_finalize $VM +do_test tabfunc01-670 { + set TAIL {} + set VM [sqlite3_prepare db { + SELECT length(y) FROM t6 WHERE x IN intarray(?1,3); + } -1 TAIL] + string trim $TAIL +} {} +do_test tabfunc01-671 { + sqlite3_bind_intarray $VM 1 11 22 33 44 55 + sqlite3_step $VM +} SQLITE_ROW +do_test tabfunc01-672 { + sqlite3_column_int $VM 0 +} 11 +sqlite3_finalize $VM + catch {sqlite3_bind_intarray} finish_test From ba637157cad2aca6a4157d280e9a5559c5ed3a2d Mon Sep 17 00:00:00 2001 From: drh Date: Sat, 2 Jul 2016 20:57:06 +0000 Subject: [PATCH 4/5] Fix an off-by-one comparison in the intarray() virtual table. Get the intarray() virtual table tests working using the legacy makefile. FossilOrigin-Name: 7c3d441f2a9f642f3d91dcee854a4d16d298bc34 --- ext/misc/array.c | 7 ++----- main.mk | 1 + manifest | 16 ++++++++-------- manifest.uuid | 2 +- test/tabfunc01.test | 11 +++++++++++ 5 files changed, 23 insertions(+), 14 deletions(-) diff --git a/ext/misc/array.c b/ext/misc/array.c index 54b249f90a..4420dc5178 100644 --- a/ext/misc/array.c +++ b/ext/misc/array.c @@ -25,7 +25,7 @@ ** The intarray "function" is really a virtual table with the ** following schema: ** -** CREATE FUNCTION intarray( +** CREATE TABLE intarray( ** value, ** pointer HIDDEN, ** count HIDDEN @@ -168,7 +168,7 @@ static int intarrayRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ */ static int intarrayEof(sqlite3_vtab_cursor *cur){ intarray_cursor *pCur = (intarray_cursor*)cur; - return pCur->iRowid>=pCur->iCnt; + return pCur->iRowid>pCur->iCnt; } /* @@ -181,7 +181,6 @@ static int intarrayFilter( int argc, sqlite3_value **argv ){ intarray_cursor *pCur = (intarray_cursor *)pVtabCursor; - int i = 0; if( idxNum ){ pCur->iPtr = sqlite3_value_int64(argv[0]); pCur->iCnt = sqlite3_value_int64(argv[1]); @@ -210,10 +209,8 @@ static int intarrayBestIndex( sqlite3_index_info *pIdxInfo ){ int i; /* Loop over constraints */ - int idxNum = 0; /* The query plan bitmask */ int ptrIdx = -1; /* Index of the pointer= constraint, or -1 if none */ int cntIdx = -1; /* Index of the count= constraint, or -1 if none */ - int nArg = 0; /* Number of arguments that intarrayFilter() expects */ const struct sqlite3_index_constraint *pConstraint; pConstraint = pIdxInfo->aConstraint; diff --git a/main.mk b/main.mk index ba710bfa46..9a91625948 100644 --- a/main.mk +++ b/main.mk @@ -324,6 +324,7 @@ TESTSRC = \ # TESTSRC += \ $(TOP)/ext/misc/amatch.c \ + $(TOP)/ext/misc/array.c \ $(TOP)/ext/misc/closure.c \ $(TOP)/ext/misc/csv.c \ $(TOP)/ext/misc/eval.c \ diff --git a/manifest b/manifest index 98382c2ade..f0d5a72bb4 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Merge\sthe\salternative\stable-valued\sfunction\sRHS\sof\sIN\soperator\simplementation\nfrom\strunk. -D 2016-07-02T20:51:31.298 +C Fix\san\soff-by-one\scomparison\sin\sthe\sintarray()\svirtual\stable.\s\sGet\sthe\nintarray()\svirtual\stable\stests\sworking\susing\sthe\slegacy\smakefile. +D 2016-07-02T20:57:06.871 F Makefile.in 541d493154ec3b0b20b2f1d495ec66f55905191e F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc 50149765ef72f4e652b9a0f1f6462c4784bb9423 @@ -204,7 +204,7 @@ F ext/icu/README.txt d9fbbad0c2f647c3fdf715fc9fd64af53aedfc43 F ext/icu/icu.c 43df9d8ef2fae7a325100ebd713ab089dc829dd7 F ext/icu/sqliteicu.h 728867a802baa5a96de7495e9689a8e01715ef37 F ext/misc/amatch.c 211108e201105e4bb0c076527b8cfd34330fc234 -F ext/misc/array.c 20af0591e6611755dd8a9d1124e9c9a8cf42761f +F ext/misc/array.c bce2608f123d35eff81ac73e443b0620bed89ca1 F ext/misc/closure.c 0d2a038df8fbae7f19de42e7c7d71f2e4dc88704 F ext/misc/compress.c 122faa92d25033d6c3f07c39231de074ab3d2e83 F ext/misc/csv.c f01126ba170fd4ef7c752b156568a80c912d4441 @@ -308,7 +308,7 @@ F ext/userauth/userauth.c 5fa3bdb492f481bbc1709fc83c91ebd13460c69e F install-sh 9d4de14ab9fb0facae2f48780b874848cbf2f895 x F ltmain.sh 3ff0879076df340d2e23ae905484d8c15d5fdea8 F magic.txt 8273bf49ba3b0c8559cb2774495390c31fd61c60 -F main.mk 3f669c06db5c4a53ff21dda639247c6310497180 +F main.mk 162bb978db5519dbcaba141be346247c82b7caa8 F mkso.sh fd21c06b063bb16a5d25deea1752c2da6ac3ed83 F mptest/config01.test 3c6adcbc50b991866855f1977ff172eb6d901271 F mptest/config02.test 4415dfe36c48785f751e16e32c20b077c28ae504 @@ -1116,7 +1116,7 @@ F test/symlink.test c9ebe7330d228249e447038276bfc8a7b22f4849 F test/sync.test 2f84bdbc2b2df1fcb0220575b4b9f8cea94b7529 F test/syscall.test f59ba4e25f7ba4a4c031026cc2ef8b6e4b4c639c F test/sysfault.test c9f2b0d8d677558f74de750c75e12a5454719d04 -F test/tabfunc01.test 96e56e22e5be82818d9673e9e993e9f26f80079a +F test/tabfunc01.test ebcfb548b88f874a5ded67e7c85ca0d5ab31f4b5 F test/table.test b708f3e5fa2542fa51dfab21fc07b36ea445cb2f F test/tableapi.test 2674633fa95d80da917571ebdd759a14d9819126 F test/tableopts.test dba698ba97251017b7c80d738c198d39ab747930 @@ -1504,7 +1504,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P ba1b441b6003808810667d749635fe6b2e8c6165 bead151e72215e6ca2a90eb049cfca414dccea04 -R ab2f6bd04c16fa8193c86a3894a34997 +P 507fdbfb54ce377f0d870260b07d71b797843fcf +R 7bb6d7d8a00bd78e137b76d2018230b6 U drh -Z f71a4b07caffe5df9d8bd19d720e6958 +Z f5dbc3921a51d0860e9141544e9a9fdc diff --git a/manifest.uuid b/manifest.uuid index 2e561b3053..1d82fb3553 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -507fdbfb54ce377f0d870260b07d71b797843fcf \ No newline at end of file +7c3d441f2a9f642f3d91dcee854a4d16d298bc34 \ No newline at end of file diff --git a/test/tabfunc01.test b/test/tabfunc01.test index 044851705d..298872e0ef 100644 --- a/test/tabfunc01.test +++ b/test/tabfunc01.test @@ -201,6 +201,17 @@ do_test tabfunc01-671 { do_test tabfunc01-672 { sqlite3_column_int $VM 0 } 11 +do_test tabfunc01-673 { + sqlite3_step $VM + sqlite3_column_int $VM 0 +} 22 +do_test tabfunc01-674 { + sqlite3_step $VM + sqlite3_column_int $VM 0 +} 33 +do_test tabfunc01-675 { + sqlite3_step $VM +} {SQLITE_DONE} sqlite3_finalize $VM catch {sqlite3_bind_intarray} From 2e3f87ae8493817ceaddc316077b49d081e4e9db Mon Sep 17 00:00:00 2001 From: drh Date: Sun, 3 Jul 2016 02:35:47 +0000 Subject: [PATCH 5/5] Change the name of the intarray() extension to carray() and give it an optional third parameter that specifies the datatype as one of 'int32', 'int64', 'double', or 'char*'. 'int32' is the default. FossilOrigin-Name: a204ba99db34b356acb259189158a32d2df25da0 --- Makefile.in | 2 +- Makefile.msc | 1 + ext/misc/array.c | 293 ----------------------------------- ext/misc/carray.c | 369 ++++++++++++++++++++++++++++++++++++++++++++ main.mk | 2 +- manifest | 22 +-- manifest.uuid | 2 +- src/test1.c | 147 +++++++++++++++--- test/tabfunc01.test | 109 ++++++------- 9 files changed, 550 insertions(+), 397 deletions(-) delete mode 100644 ext/misc/array.c create mode 100644 ext/misc/carray.c diff --git a/Makefile.in b/Makefile.in index ff863ec906..fd0ad6b6a7 100644 --- a/Makefile.in +++ b/Makefile.in @@ -416,7 +416,7 @@ TESTSRC = \ # TESTSRC += \ $(TOP)/ext/misc/amatch.c \ - $(TOP)/ext/misc/array.c \ + $(TOP)/ext/misc/carray.c \ $(TOP)/ext/misc/closure.c \ $(TOP)/ext/misc/csv.c \ $(TOP)/ext/misc/eval.c \ diff --git a/Makefile.msc b/Makefile.msc index 93116cd0dc..cd866ce4a4 100644 --- a/Makefile.msc +++ b/Makefile.msc @@ -1292,6 +1292,7 @@ TESTSRC = \ # TESTEXT = \ $(TOP)\ext\misc\amatch.c \ + $(TOP)\ext\misc\carray.c \ $(TOP)\ext\misc\closure.c \ $(TOP)\ext\misc\csv.c \ $(TOP)\ext\misc\eval.c \ diff --git a/ext/misc/array.c b/ext/misc/array.c deleted file mode 100644 index 4420dc5178..0000000000 --- a/ext/misc/array.c +++ /dev/null @@ -1,293 +0,0 @@ -/* -** 2016-06-29 -** -** 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 demonstrates how to create a table-valued-function that -** returns the values in a C-language array. -** Examples: -** -** SELECT * FROM intarray($ptr,5) -** -** The query above returns 5 integers contained in a C-language array -** at the address $ptr. $ptr is a pointer to the array of integers that -** has been cast to an integer. -** -** HOW IT WORKS -** -** The intarray "function" is really a virtual table with the -** following schema: -** -** CREATE TABLE intarray( -** value, -** pointer HIDDEN, -** count HIDDEN -** ); -*/ -#include "sqlite3ext.h" -SQLITE_EXTENSION_INIT1 -#include -#include - -#ifndef SQLITE_OMIT_VIRTUALTABLE - - -/* intarray_cursor is a subclass of sqlite3_vtab_cursor which will -** serve as the underlying representation of a cursor that scans -** over rows of the result -*/ -typedef struct intarray_cursor intarray_cursor; -struct intarray_cursor { - sqlite3_vtab_cursor base; /* Base class - must be first */ - int isDesc; /* True to count down rather than up */ - sqlite3_int64 iRowid; /* The rowid */ - sqlite3_int64 iPtr; /* Pointer to array of integers */ - sqlite3_int64 iCnt; /* Number of integers in the array */ -}; - -/* -** The intarrayConnect() method is invoked to create a new -** intarray_vtab that describes the intarray virtual table. -** -** Think of this routine as the constructor for intarray_vtab objects. -** -** All this routine needs to do is: -** -** (1) Allocate the intarray_vtab object and initialize all fields. -** -** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the -** result set of queries against intarray will look like. -*/ -static int intarrayConnect( - sqlite3 *db, - void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVtab, - char **pzErr -){ - sqlite3_vtab *pNew; - int rc; - -/* Column numbers */ -#define INTARRAY_COLUMN_VALUE 0 -#define INTARRAY_COLUMN_POINTER 1 -#define INTARRAY_COLUMN_COUNT 2 - - rc = sqlite3_declare_vtab(db, - "CREATE TABLE x(value,pointer hidden,count hidden)"); - if( rc==SQLITE_OK ){ - pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) ); - if( pNew==0 ) return SQLITE_NOMEM; - memset(pNew, 0, sizeof(*pNew)); - } - return rc; -} - -/* -** This method is the destructor for intarray_cursor objects. -*/ -static int intarrayDisconnect(sqlite3_vtab *pVtab){ - sqlite3_free(pVtab); - return SQLITE_OK; -} - -/* -** Constructor for a new intarray_cursor object. -*/ -static int intarrayOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ - intarray_cursor *pCur; - pCur = sqlite3_malloc( sizeof(*pCur) ); - if( pCur==0 ) return SQLITE_NOMEM; - memset(pCur, 0, sizeof(*pCur)); - *ppCursor = &pCur->base; - return SQLITE_OK; -} - -/* -** Destructor for a intarray_cursor. -*/ -static int intarrayClose(sqlite3_vtab_cursor *cur){ - sqlite3_free(cur); - return SQLITE_OK; -} - - -/* -** Advance a intarray_cursor to its next row of output. -*/ -static int intarrayNext(sqlite3_vtab_cursor *cur){ - intarray_cursor *pCur = (intarray_cursor*)cur; - pCur->iRowid++; - return SQLITE_OK; -} - -/* -** Return values of columns for the row at which the intarray_cursor -** is currently pointing. -*/ -static int intarrayColumn( - sqlite3_vtab_cursor *cur, /* The cursor */ - sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ - int i /* Which column to return */ -){ - intarray_cursor *pCur = (intarray_cursor*)cur; - sqlite3_int64 x = 0; - switch( i ){ - case INTARRAY_COLUMN_POINTER: x = pCur->iPtr; break; - case INTARRAY_COLUMN_COUNT: x = pCur->iCnt; break; - default: { - int *p = (int*)pCur->iPtr; - x = (int)p[pCur->iRowid-1]; - break; - } - } - sqlite3_result_int64(ctx, x); - return SQLITE_OK; -} - -/* -** Return the rowid for the current row. In this implementation, the -** rowid is the same as the output value. -*/ -static int intarrayRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ - intarray_cursor *pCur = (intarray_cursor*)cur; - *pRowid = pCur->iRowid; - return SQLITE_OK; -} - -/* -** Return TRUE if the cursor has been moved off of the last -** row of output. -*/ -static int intarrayEof(sqlite3_vtab_cursor *cur){ - intarray_cursor *pCur = (intarray_cursor*)cur; - return pCur->iRowid>pCur->iCnt; -} - -/* -** This method is called to "rewind" the intarray_cursor object back -** to the first row of output. -*/ -static int intarrayFilter( - sqlite3_vtab_cursor *pVtabCursor, - int idxNum, const char *idxStr, - int argc, sqlite3_value **argv -){ - intarray_cursor *pCur = (intarray_cursor *)pVtabCursor; - if( idxNum ){ - pCur->iPtr = sqlite3_value_int64(argv[0]); - pCur->iCnt = sqlite3_value_int64(argv[1]); - }else{ - pCur->iPtr = 0; - pCur->iCnt = 0; - } - pCur->iRowid = 1; - return SQLITE_OK; -} - -/* -** SQLite will invoke this method one or more times while planning a query -** that uses the intarray virtual table. This routine needs to create -** a query plan for each invocation and compute an estimated cost for that -** plan. -** -** In this implementation idxNum is used to represent the -** query plan. idxStr is unused. -** -** idxNum is 1 if the pointer= and count= constraints exist and is 0 otherwise. -** If idxNum is 0, then intarray becomes an empty table. -*/ -static int intarrayBestIndex( - sqlite3_vtab *tab, - sqlite3_index_info *pIdxInfo -){ - int i; /* Loop over constraints */ - int ptrIdx = -1; /* Index of the pointer= constraint, or -1 if none */ - int cntIdx = -1; /* Index of the count= constraint, or -1 if none */ - - const struct sqlite3_index_constraint *pConstraint; - pConstraint = pIdxInfo->aConstraint; - for(i=0; inConstraint; i++, pConstraint++){ - if( pConstraint->usable==0 ) continue; - if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; - switch( pConstraint->iColumn ){ - case INTARRAY_COLUMN_POINTER: - ptrIdx = i; - break; - case INTARRAY_COLUMN_COUNT: - cntIdx = i; - break; - } - } - if( ptrIdx>=0 && cntIdx>=0 ){ - pIdxInfo->aConstraintUsage[ptrIdx].argvIndex = 1; - pIdxInfo->aConstraintUsage[ptrIdx].omit = 1; - pIdxInfo->aConstraintUsage[cntIdx].argvIndex = 2; - pIdxInfo->aConstraintUsage[cntIdx].omit = 1; - pIdxInfo->estimatedCost = (double)1; - pIdxInfo->estimatedRows = (double)100; - pIdxInfo->idxNum = 1; - }else{ - pIdxInfo->estimatedCost = (double)2147483647; - pIdxInfo->estimatedRows = (double)2147483647; - pIdxInfo->idxNum = 0; - } - return SQLITE_OK; -} - -/* -** This following structure defines all the methods for the -** intarray virtual table. -*/ -static sqlite3_module intarrayModule = { - 0, /* iVersion */ - 0, /* xCreate */ - intarrayConnect, /* xConnect */ - intarrayBestIndex, /* xBestIndex */ - intarrayDisconnect, /* xDisconnect */ - 0, /* xDestroy */ - intarrayOpen, /* xOpen - open a cursor */ - intarrayClose, /* xClose - close a cursor */ - intarrayFilter, /* xFilter - configure scan constraints */ - intarrayNext, /* xNext - advance a cursor */ - intarrayEof, /* xEof - check for end of scan */ - intarrayColumn, /* xColumn - read data */ - intarrayRowid, /* xRowid - read data */ - 0, /* xUpdate */ - 0, /* xBegin */ - 0, /* xSync */ - 0, /* xCommit */ - 0, /* xRollback */ - 0, /* xFindMethod */ - 0, /* xRename */ -}; - -#endif /* SQLITE_OMIT_VIRTUALTABLE */ - -#ifdef _WIN32 -__declspec(dllexport) -#endif -int sqlite3_array_init( - sqlite3 *db, - char **pzErrMsg, - const sqlite3_api_routines *pApi -){ - int rc = SQLITE_OK; - SQLITE_EXTENSION_INIT2(pApi); -#ifndef SQLITE_OMIT_VIRTUALTABLE - if( sqlite3_libversion_number()<3008012 ){ - *pzErrMsg = sqlite3_mprintf( - "intarray() requires SQLite 3.8.12 or later"); - return SQLITE_ERROR; - } - rc = sqlite3_create_module(db, "intarray", &intarrayModule, 0); -#endif - return rc; -} diff --git a/ext/misc/carray.c b/ext/misc/carray.c new file mode 100644 index 0000000000..70d226a85f --- /dev/null +++ b/ext/misc/carray.c @@ -0,0 +1,369 @@ +/* +** 2016-06-29 +** +** 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 demonstrates how to create a table-valued-function that +** returns the values in a C-language array. +** Examples: +** +** SELECT * FROM array($ptr,5) +** +** The query above returns 5 integers contained in a C-language array +** at the address $ptr. $ptr is a pointer to the array of integers that +** has been cast to an integer. +** +** There is an optional third parameter to determine the datatype of +** the C-language array. Allowed values of the third parameter are +** 'int32', 'int64', 'double', 'char*'. Example: +** +** SELECT * FROM array($ptr,10,'char*'); +** +** HOW IT WORKS +** +** The carray "function" is really a virtual table with the +** following schema: +** +** CREATE TABLE carray( +** value, +** pointer HIDDEN, +** count HIDDEN, +** ctype TEXT HIDDEN +** ); +** +** If the hidden columns "pointer" and "count" are unconstrained, then +** the virtual table has no rows. Otherwise, the virtual table interprets +** the integer value of "pointer" as a pointer to the array and "count" +** as the number of elements in the array. The virtual table steps through +** the array, element by element. +*/ +#include "sqlite3ext.h" +SQLITE_EXTENSION_INIT1 +#include +#include + +#ifndef SQLITE_OMIT_VIRTUALTABLE + +/* +** Allowed datatypes +*/ +#define CARRAY_INT32 0 +#define CARRAY_INT64 1 +#define CARRAY_DOUBLE 2 +#define CARRAY_TEXT 3 + +/* +** Names of types +*/ +static const char *azType[] = { "int32", "int64", "double", "char*" }; + + +/* carray_cursor is a subclass of sqlite3_vtab_cursor which will +** serve as the underlying representation of a cursor that scans +** over rows of the result +*/ +typedef struct carray_cursor carray_cursor; +struct carray_cursor { + sqlite3_vtab_cursor base; /* Base class - must be first */ + sqlite3_int64 iRowid; /* The rowid */ + sqlite3_int64 iPtr; /* Pointer to array of values */ + sqlite3_int64 iCnt; /* Number of integers in the array */ + unsigned char eType; /* One of the CARRAY_type values */ +}; + +/* +** The carrayConnect() method is invoked to create a new +** carray_vtab that describes the carray virtual table. +** +** Think of this routine as the constructor for carray_vtab objects. +** +** All this routine needs to do is: +** +** (1) Allocate the carray_vtab object and initialize all fields. +** +** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the +** result set of queries against carray will look like. +*/ +static int carrayConnect( + sqlite3 *db, + void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVtab, + char **pzErr +){ + sqlite3_vtab *pNew; + int rc; + +/* Column numbers */ +#define CARRAY_COLUMN_VALUE 0 +#define CARRAY_COLUMN_POINTER 1 +#define CARRAY_COLUMN_COUNT 2 +#define CARRAY_COLUMN_CTYPE 3 + + rc = sqlite3_declare_vtab(db, + "CREATE TABLE x(value,pointer hidden,count hidden,ctype hidden)"); + if( rc==SQLITE_OK ){ + pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) ); + if( pNew==0 ) return SQLITE_NOMEM; + memset(pNew, 0, sizeof(*pNew)); + } + return rc; +} + +/* +** This method is the destructor for carray_cursor objects. +*/ +static int carrayDisconnect(sqlite3_vtab *pVtab){ + sqlite3_free(pVtab); + return SQLITE_OK; +} + +/* +** Constructor for a new carray_cursor object. +*/ +static int carrayOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ + carray_cursor *pCur; + pCur = sqlite3_malloc( sizeof(*pCur) ); + if( pCur==0 ) return SQLITE_NOMEM; + memset(pCur, 0, sizeof(*pCur)); + *ppCursor = &pCur->base; + return SQLITE_OK; +} + +/* +** Destructor for a carray_cursor. +*/ +static int carrayClose(sqlite3_vtab_cursor *cur){ + sqlite3_free(cur); + return SQLITE_OK; +} + + +/* +** Advance a carray_cursor to its next row of output. +*/ +static int carrayNext(sqlite3_vtab_cursor *cur){ + carray_cursor *pCur = (carray_cursor*)cur; + pCur->iRowid++; + return SQLITE_OK; +} + +/* +** Return values of columns for the row at which the carray_cursor +** is currently pointing. +*/ +static int carrayColumn( + sqlite3_vtab_cursor *cur, /* The cursor */ + sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ + int i /* Which column to return */ +){ + carray_cursor *pCur = (carray_cursor*)cur; + sqlite3_int64 x = 0; + switch( i ){ + case CARRAY_COLUMN_POINTER: x = pCur->iPtr; break; + case CARRAY_COLUMN_COUNT: x = pCur->iCnt; break; + case CARRAY_COLUMN_CTYPE: { + sqlite3_result_text(ctx, azType[pCur->eType], -1, SQLITE_STATIC); + return SQLITE_OK; + } + default: { + switch( pCur->eType ){ + case CARRAY_INT32: { + int *p = (int*)pCur->iPtr; + sqlite3_result_int(ctx, p[pCur->iRowid-1]); + return SQLITE_OK; + } + case CARRAY_INT64: { + sqlite3_int64 *p = (sqlite3_int64*)pCur->iPtr; + sqlite3_result_int64(ctx, p[pCur->iRowid-1]); + return SQLITE_OK; + } + case CARRAY_DOUBLE: { + double *p = (double*)pCur->iPtr; + sqlite3_result_double(ctx, p[pCur->iRowid-1]); + return SQLITE_OK; + } + case CARRAY_TEXT: { + const char **p = (const char**)pCur->iPtr; + sqlite3_result_text(ctx, p[pCur->iRowid-1], -1, SQLITE_TRANSIENT); + return SQLITE_OK; + } + } + } + } + sqlite3_result_int64(ctx, x); + return SQLITE_OK; +} + +/* +** Return the rowid for the current row. In this implementation, the +** rowid is the same as the output value. +*/ +static int carrayRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ + carray_cursor *pCur = (carray_cursor*)cur; + *pRowid = pCur->iRowid; + return SQLITE_OK; +} + +/* +** Return TRUE if the cursor has been moved off of the last +** row of output. +*/ +static int carrayEof(sqlite3_vtab_cursor *cur){ + carray_cursor *pCur = (carray_cursor*)cur; + return pCur->iRowid>pCur->iCnt; +} + +/* +** This method is called to "rewind" the carray_cursor object back +** to the first row of output. +*/ +static int carrayFilter( + sqlite3_vtab_cursor *pVtabCursor, + int idxNum, const char *idxStr, + int argc, sqlite3_value **argv +){ + carray_cursor *pCur = (carray_cursor *)pVtabCursor; + if( idxNum ){ + pCur->iPtr = sqlite3_value_int64(argv[0]); + pCur->iCnt = sqlite3_value_int64(argv[1]); + if( idxNum<3 ){ + pCur->eType = CARRAY_INT32; + }else{ + int i; + const char *zType = (const char*)sqlite3_value_text(argv[2]); + for(i=0; i=sizeof(azType)/sizeof(azType[0]) ){ + pVtabCursor->pVtab->zErrMsg = sqlite3_mprintf( + "unknown datatype: %Q", zType); + return SQLITE_ERROR; + }else{ + pCur->eType = i; + } + } + }else{ + pCur->iPtr = 0; + pCur->iCnt = 0; + } + pCur->iRowid = 1; + return SQLITE_OK; +} + +/* +** SQLite will invoke this method one or more times while planning a query +** that uses the carray virtual table. This routine needs to create +** a query plan for each invocation and compute an estimated cost for that +** plan. +** +** In this implementation idxNum is used to represent the +** query plan. idxStr is unused. +** +** idxNum is 2 if the pointer= and count= constraints exist, +** 3 if the ctype= constraint also exists, and is 0 otherwise. +** If idxNum is 0, then carray becomes an empty table. +*/ +static int carrayBestIndex( + sqlite3_vtab *tab, + sqlite3_index_info *pIdxInfo +){ + int i; /* Loop over constraints */ + int ptrIdx = -1; /* Index of the pointer= constraint, or -1 if none */ + int cntIdx = -1; /* Index of the count= constraint, or -1 if none */ + int ctypeIdx = -1; /* Index of the ctype= constraint, or -1 if none */ + + const struct sqlite3_index_constraint *pConstraint; + pConstraint = pIdxInfo->aConstraint; + for(i=0; inConstraint; i++, pConstraint++){ + if( pConstraint->usable==0 ) continue; + if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; + switch( pConstraint->iColumn ){ + case CARRAY_COLUMN_POINTER: + ptrIdx = i; + break; + case CARRAY_COLUMN_COUNT: + cntIdx = i; + break; + case CARRAY_COLUMN_CTYPE: + ctypeIdx = i; + break; + } + } + if( ptrIdx>=0 && cntIdx>=0 ){ + pIdxInfo->aConstraintUsage[ptrIdx].argvIndex = 1; + pIdxInfo->aConstraintUsage[ptrIdx].omit = 1; + pIdxInfo->aConstraintUsage[cntIdx].argvIndex = 2; + pIdxInfo->aConstraintUsage[cntIdx].omit = 1; + pIdxInfo->estimatedCost = (double)1; + pIdxInfo->estimatedRows = (double)100; + pIdxInfo->idxNum = 2; + if( ctypeIdx>=0 ){ + pIdxInfo->aConstraintUsage[ctypeIdx].argvIndex = 3; + pIdxInfo->aConstraintUsage[ctypeIdx].omit = 1; + pIdxInfo->idxNum = 3; + } + }else{ + pIdxInfo->estimatedCost = (double)2147483647; + pIdxInfo->estimatedRows = (double)2147483647; + pIdxInfo->idxNum = 0; + } + return SQLITE_OK; +} + +/* +** This following structure defines all the methods for the +** carray virtual table. +*/ +static sqlite3_module carrayModule = { + 0, /* iVersion */ + 0, /* xCreate */ + carrayConnect, /* xConnect */ + carrayBestIndex, /* xBestIndex */ + carrayDisconnect, /* xDisconnect */ + 0, /* xDestroy */ + carrayOpen, /* xOpen - open a cursor */ + carrayClose, /* xClose - close a cursor */ + carrayFilter, /* xFilter - configure scan constraints */ + carrayNext, /* xNext - advance a cursor */ + carrayEof, /* xEof - check for end of scan */ + carrayColumn, /* xColumn - read data */ + carrayRowid, /* xRowid - read data */ + 0, /* xUpdate */ + 0, /* xBegin */ + 0, /* xSync */ + 0, /* xCommit */ + 0, /* xRollback */ + 0, /* xFindMethod */ + 0, /* xRename */ +}; + +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +#ifdef _WIN32 +__declspec(dllexport) +#endif +int sqlite3_carray_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ + int rc = SQLITE_OK; + SQLITE_EXTENSION_INIT2(pApi); +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( sqlite3_libversion_number()<3008012 ){ + *pzErrMsg = sqlite3_mprintf( + "carray() requires SQLite 3.8.12 or later"); + return SQLITE_ERROR; + } + rc = sqlite3_create_module(db, "carray", &carrayModule, 0); +#endif + return rc; +} diff --git a/main.mk b/main.mk index 9a91625948..e3da609750 100644 --- a/main.mk +++ b/main.mk @@ -324,7 +324,7 @@ TESTSRC = \ # TESTSRC += \ $(TOP)/ext/misc/amatch.c \ - $(TOP)/ext/misc/array.c \ + $(TOP)/ext/misc/carray.c \ $(TOP)/ext/misc/closure.c \ $(TOP)/ext/misc/csv.c \ $(TOP)/ext/misc/eval.c \ diff --git a/manifest b/manifest index f0d5a72bb4..4f9231b7f5 100644 --- a/manifest +++ b/manifest @@ -1,8 +1,8 @@ -C Fix\san\soff-by-one\scomparison\sin\sthe\sintarray()\svirtual\stable.\s\sGet\sthe\nintarray()\svirtual\stable\stests\sworking\susing\sthe\slegacy\smakefile. -D 2016-07-02T20:57:06.871 -F Makefile.in 541d493154ec3b0b20b2f1d495ec66f55905191e +C Change\sthe\sname\sof\sthe\sintarray()\sextension\sto\scarray()\sand\sgive\sit\san\noptional\sthird\sparameter\sthat\sspecifies\sthe\sdatatype\sas\sone\sof\s'int32',\n'int64',\s'double',\sor\s'char*'.\s\s'int32'\sis\sthe\sdefault. +D 2016-07-03T02:35:47.798 +F Makefile.in 6c20d44f72d4564f11652b26291a214c8367e5db F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 -F Makefile.msc 50149765ef72f4e652b9a0f1f6462c4784bb9423 +F Makefile.msc d66d0395c38571aab3804f8db0fa20707ae4609a F README.md 8ecc12493ff9f820cdea6520a9016001cb2e59b7 F VERSION cb29eb11e493dd85b3eeec4053c03949bf98478e F aclocal.m4 a5c22d164aff7ed549d53a90fa56d56955281f50 @@ -204,7 +204,7 @@ F ext/icu/README.txt d9fbbad0c2f647c3fdf715fc9fd64af53aedfc43 F ext/icu/icu.c 43df9d8ef2fae7a325100ebd713ab089dc829dd7 F ext/icu/sqliteicu.h 728867a802baa5a96de7495e9689a8e01715ef37 F ext/misc/amatch.c 211108e201105e4bb0c076527b8cfd34330fc234 -F ext/misc/array.c bce2608f123d35eff81ac73e443b0620bed89ca1 +F ext/misc/carray.c f1947c7d5bbce17f8244c9a05baae11d5d68467e w ext/misc/array.c F ext/misc/closure.c 0d2a038df8fbae7f19de42e7c7d71f2e4dc88704 F ext/misc/compress.c 122faa92d25033d6c3f07c39231de074ab3d2e83 F ext/misc/csv.c f01126ba170fd4ef7c752b156568a80c912d4441 @@ -308,7 +308,7 @@ F ext/userauth/userauth.c 5fa3bdb492f481bbc1709fc83c91ebd13460c69e F install-sh 9d4de14ab9fb0facae2f48780b874848cbf2f895 x F ltmain.sh 3ff0879076df340d2e23ae905484d8c15d5fdea8 F magic.txt 8273bf49ba3b0c8559cb2774495390c31fd61c60 -F main.mk 162bb978db5519dbcaba141be346247c82b7caa8 +F main.mk e9d66f1b1c4874221d12d940be3ce5f397c10741 F mkso.sh fd21c06b063bb16a5d25deea1752c2da6ac3ed83 F mptest/config01.test 3c6adcbc50b991866855f1977ff172eb6d901271 F mptest/config02.test 4415dfe36c48785f751e16e32c20b077c28ae504 @@ -393,7 +393,7 @@ F src/sqliteLimit.h c0373387c287c8d0932510b5547ecde31b5da247 F src/status.c 70912d7be68e9e2dbc4010c93d344af61d4c59ba F src/table.c 5226df15ab9179b9ed558d89575ea0ce37b03fc9 F src/tclsqlite.c 25fbbbb97f76dbfd113153fb63f52d7ecfac5dd0 -F src/test1.c 081e4ed40525590406a51f7e7e4cee31cdb5d029 +F src/test1.c 7cbfda741fbfa541ecce305ff2d7c6bb730aa991 F src/test2.c 5586f43fcd9a1be0830793cf9d354082c261b25b F src/test3.c c75c8af0eadb335236c9e61b51044c58a8f7dd59 F src/test4.c d168f83cc78d02e8d35567bb5630e40dcd85ac1e @@ -1116,7 +1116,7 @@ F test/symlink.test c9ebe7330d228249e447038276bfc8a7b22f4849 F test/sync.test 2f84bdbc2b2df1fcb0220575b4b9f8cea94b7529 F test/syscall.test f59ba4e25f7ba4a4c031026cc2ef8b6e4b4c639c F test/sysfault.test c9f2b0d8d677558f74de750c75e12a5454719d04 -F test/tabfunc01.test ebcfb548b88f874a5ded67e7c85ca0d5ab31f4b5 +F test/tabfunc01.test 50a9fb379f9747fd0d40ea6d8fa3a101361bb537 F test/table.test b708f3e5fa2542fa51dfab21fc07b36ea445cb2f F test/tableapi.test 2674633fa95d80da917571ebdd759a14d9819126 F test/tableopts.test dba698ba97251017b7c80d738c198d39ab747930 @@ -1504,7 +1504,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 507fdbfb54ce377f0d870260b07d71b797843fcf -R 7bb6d7d8a00bd78e137b76d2018230b6 +P 7c3d441f2a9f642f3d91dcee854a4d16d298bc34 +R 41f36cbba6be0bbd66a667562505a546 U drh -Z f5dbc3921a51d0860e9141544e9a9fdc +Z 31f2105436c0dcfd40b6904e7b94f7b1 diff --git a/manifest.uuid b/manifest.uuid index 1d82fb3553..869a21e73c 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -7c3d441f2a9f642f3d91dcee854a4d16d298bc34 \ No newline at end of file +a204ba99db34b356acb259189158a32d2df25da0 \ No newline at end of file diff --git a/src/test1.c b/src/test1.c index 9a73e0e0b5..8e8020c043 100644 --- a/src/test1.c +++ b/src/test1.c @@ -3242,41 +3242,137 @@ static int test_bind_int( /* -** Usage: sqlite3_bind_intarray STMT N INT ... +** Usage: intarray_addr INT ... ** -** Create a C-language array of integers from the arguments. Bind a pointer -** to this array to the NAME parameter of STMT. +** Return the address of a C-language array of 32-bit integers. +** +** Space to hold the array is obtained from malloc(). Call this procedure once +** with no arguments in order to release memory. Each call to this procedure +** overwrites the previous array. */ -static int test_bind_intarray( +static int test_intarray_addr( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ - sqlite3_stmt *pStmt; - int idx; int i; static int *p = 0; sqlite3_free(p); p = 0; - if( objc<4 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " STMT NAME INT...", 0); - return TCL_ERROR; - } - - if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; - if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; - p = sqlite3_malloc( sizeof(int)*(objc-3) ); - if( p==0 ) return TCL_ERROR; - for(i=0; i1 ){ + p = sqlite3_malloc( sizeof(p[0])*(objc-1) ); + if( p==0 ) return TCL_ERROR; + for(i=0; i1 ){ + p = sqlite3_malloc( sizeof(p[0])*(objc-1) ); + if( p==0 ) return TCL_ERROR; + for(i=0; i1 ){ + p = sqlite3_malloc( sizeof(p[0])*(objc-1) ); + if( p==0 ) return TCL_ERROR; + for(i=0; i1 ){ + p = sqlite3_malloc( sizeof(p[0])*(objc-1) ); + if( p==0 ) return TCL_ERROR; + for(i=0; i