Add an extra test for the change on this branch.

FossilOrigin-Name: d2d28251566d2a0ec1a07fe5b8ed047136840bfd
This commit is contained in:
dan 2016-03-02 16:13:53 +00:00
commit 512c09b0bd
4 changed files with 249 additions and 23 deletions

@ -1,5 +1,5 @@
C Allow\sthe\sleft-hand\sside\sof\sIN\soperators\son\svirtual\stables\sto\shave\sthe\naConstraintUsage[].omit\sflag\sclear.
D 2016-03-02T03:28:07.978
C Add\san\sextra\stest\sfor\sthe\schange\son\sthis\sbranch.
D 2016-03-02T16:13:53.809
F Makefile.in e335453db0b16da00c884ad51bb56d1c091a74de
F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
F Makefile.msc dbd4621ecc585c2ef0c2aa0874698c54675754f1
@ -371,7 +371,7 @@ F src/test9.c bea1e8cf52aa93695487badedd6e1886c321ea60
F src/test_async.c 21e11293a2f72080eda70e1124e9102044531cd8
F src/test_autoext.c dea8a01a7153b9adc97bd26161e4226329546e12
F src/test_backup.c 2e6e6a081870150f20c526a2e9d0d29cda47d803
F src/test_bestindex.c cd2eb53d7abf082665bca161e6762c4ba395f17a
F src/test_bestindex.c a711473c79e812e4082fbf045ed6c18c99c16058
F src/test_blob.c b2551a9b5573232db5f66f292307c37067937239
F src/test_btree.c 2e9978eca99a9a4bfa8cae949efb00886860a64f
F src/test_config.c 0dee90328e3dedf8ba002ee94b6a7e7ea7726fe4
@ -493,7 +493,7 @@ F test/backup_malloc.test 7162d604ec2b4683c4b3799a48657fb8b5e2d450
F test/badutf.test d5360fc31f643d37a973ab0d8b4fb85799c3169f
F test/badutf2.test f5bc7f2d280670ecd79b9cf4f0f1760c607fe51f
F test/bc_common.tcl 3eda41ef9cda7d5f6c205462c96228b301da4191
F test/bestindex1.test 41c763428e403663b3e15b2acba7f07b40b22592
F test/bestindex1.test e228fe1e3794dbe20271481164e000d695abcd24
F test/between.test 34d375fb5ce1ae283ffe82b6b233e9f38e84fc6c
F test/bigfile.test aa74f4e5db51c8e54a1d9de9fa65d01d1eb20b59
F test/bigfile2.test 1b489a3a39ae90c7f027b79110d6b4e1dbc71bfc
@ -1453,10 +1453,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 3d9daa929c0abe6dc01e800ef343b0eef2f0c76a
R c059a6ee769a0309e913921e43679d8c
T *branch * vtab-IN-opt
T *sym-vtab-IN-opt *
T -sym-trunk *
U drh
Z f36b44043e8c4337262495e2c87317b9
P 1622623cbbfc4325c53d731aba78ca9c382ec612 3c15a9bf45cd7dae2fbd99123b8dd75ce278d6e4
R bdc2b3d0e5cbd463ccc4765d0aeeef84
U dan
Z d9e37a89daaed1ba1547f52769e0ebb4

@ -1 +1 @@
1622623cbbfc4325c53d731aba78ca9c382ec612
d2d28251566d2a0ec1a07fe5b8ed047136840bfd

@ -28,8 +28,9 @@
** CREATE VIRTUAL TABLE x1 USING tcl(tcl_command);
**
** The command [tcl_command] is invoked when the table is first created (or
** connected) and when the xBestIndex() method is invoked. When it is created
** (or connected), it is invoked as follows:
** connected), when the xBestIndex() method is invoked and when the xFilter()
** method is called. When it is created (or connected), it is invoked as
** follows:
**
** tcl_command xConnect
**
@ -67,10 +68,27 @@
** "cost" (value of estimatedCost field)
** "rows" (value of estimatedRows field)
** "use" (index of used constraint in aConstraint[])
** "omit" (like "use", but also sets omit flag)
** "idxnum" (value of idxNum field)
** "idxstr" (value of idxStr field)
**
** Refer to code below for further details.
**
** When SQLite calls the xFilter() method, this module invokes the following
** Tcl script:
**
** tcl_command xFilter IDXNUM IDXSTR ARGLIST
**
** IDXNUM and IDXSTR are the values of the idxNum and idxStr parameters
** passed to xFilter. ARGLIST is a Tcl list containing each of the arguments
** passed to xFilter in text form.
**
** As with xBestIndex(), the return value of the script is interpreted as a
** list of key-value pairs. There is currently only one key defined - "sql".
** The value must be the full text of an SQL statement that returns the data
** for the current scan. The leftmost column returned by the SELECT is assumed
** to contain the rowid. Other columns must follow, in order from left to
** right.
*/
@ -89,11 +107,13 @@ struct tcl_vtab {
sqlite3_vtab base;
Tcl_Interp *interp;
Tcl_Obj *pCmd;
sqlite3 *db;
};
/* A tcl cursor object */
struct tcl_cursor {
sqlite3_vtab_cursor base;
sqlite3_stmt *pStmt; /* Read data from here */
};
/*
@ -132,6 +152,7 @@ static int tclConnect(
pTab->pCmd = Tcl_NewStringObj(zCmd, -1);
pTab->interp = interp;
pTab->db = db;
Tcl_IncrRefCount(pTab->pCmd);
pScript = Tcl_DuplicateObj(pTab->pCmd);
@ -180,11 +201,29 @@ static int tclOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
*/
static int tclClose(sqlite3_vtab_cursor *cur){
tcl_cursor *pCur = (tcl_cursor *)cur;
sqlite3_free(pCur);
if( pCur ){
sqlite3_finalize(pCur->pStmt);
sqlite3_free(pCur);
}
return SQLITE_OK;
}
static int tclNext(sqlite3_vtab_cursor *cur){
static int tclNext(sqlite3_vtab_cursor *pVtabCursor){
tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
tcl_vtab *pTab = (tcl_vtab*)(pVtabCursor->pVtab);
if( pCsr->pStmt ){
tcl_vtab *pTab = (tcl_vtab*)(pVtabCursor->pVtab);
int rc = sqlite3_step(pCsr->pStmt);
if( rc!=SQLITE_ROW ){
const char *zErr;
rc = sqlite3_finalize(pCsr->pStmt);
pCsr->pStmt = 0;
if( rc!=SQLITE_OK ){
zErr = sqlite3_errmsg(pTab->db);
pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
}
}
}
return SQLITE_OK;
}
@ -193,19 +232,104 @@ static int tclFilter(
int idxNum, const char *idxStr,
int argc, sqlite3_value **argv
){
tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
tcl_vtab *pTab = (tcl_vtab*)(pVtabCursor->pVtab);
Tcl_Interp *interp = pTab->interp;
Tcl_Obj *pScript;
Tcl_Obj *pArg;
int ii;
int rc;
pScript = Tcl_DuplicateObj(pTab->pCmd);
Tcl_IncrRefCount(pScript);
Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("xFilter", -1));
Tcl_ListObjAppendElement(interp, pScript, Tcl_NewIntObj(idxNum));
if( idxStr ){
Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj(idxStr, -1));
}else{
Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("", -1));
}
pArg = Tcl_NewObj();
Tcl_IncrRefCount(pArg);
for(ii=0; ii<argc; ii++){
const char *zVal = (const char*)sqlite3_value_text(argv[ii]);
Tcl_Obj *pVal;
if( zVal==0 ){
pVal = Tcl_NewObj();
}else{
pVal = Tcl_NewStringObj(zVal, -1);
}
Tcl_ListObjAppendElement(interp, pArg, pVal);
}
Tcl_ListObjAppendElement(interp, pScript, pArg);
Tcl_DecrRefCount(pArg);
rc = Tcl_EvalObjEx(interp, pScript, TCL_EVAL_GLOBAL);
if( rc!=TCL_OK ){
const char *zErr = Tcl_GetStringResult(interp);
rc = SQLITE_ERROR;
pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
}else{
/* Analyze the scripts return value. The return value should be a tcl
** list object with an even number of elements. The first element of each
** pair must be one of:
**
** "sql" (SQL statement to return data)
*/
Tcl_Obj *pRes = Tcl_GetObjResult(interp);
Tcl_Obj **apElem = 0;
int nElem;
rc = Tcl_ListObjGetElements(interp, pRes, &nElem, &apElem);
if( rc!=TCL_OK ){
const char *zErr = Tcl_GetStringResult(interp);
rc = SQLITE_ERROR;
pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr);
}else{
int iArgv = 1;
for(ii=0; rc==SQLITE_OK && ii<nElem; ii+=2){
const char *zCmd = Tcl_GetString(apElem[ii]);
Tcl_Obj *p = apElem[ii+1];
if( sqlite3_stricmp("sql", zCmd)==0 ){
const char *zSql = Tcl_GetString(p);
rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
if( rc!=SQLITE_OK ){
const char *zErr = sqlite3_errmsg(pTab->db);
pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %s", zErr);
}
}else{
rc = SQLITE_ERROR;
pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %s", zCmd);
}
}
}
}
if( rc==SQLITE_OK ){
rc = tclNext(pVtabCursor);
}
return rc;
}
static int tclColumn(
sqlite3_vtab_cursor *pVtabCursor,
sqlite3_context *ctx,
int i
){
tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pStmt, i+1));
return SQLITE_OK;
}
static int tclColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
static int tclRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
*pRowid = sqlite3_column_int64(pCsr->pStmt, 0);
return SQLITE_OK;
}
static int tclRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
return SQLITE_OK;
}
static int tclEof(sqlite3_vtab_cursor *cur){
return 1;
static int tclEof(sqlite3_vtab_cursor *pVtabCursor){
tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor;
return (pCsr->pStmt==0);
}
static int tclBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){

@ -57,4 +57,109 @@ do_eqp_test 1.2 {
0 0 0 {EXECUTE LIST SUBQUERY 1}
}
#-------------------------------------------------------------------------
#
reset_db
register_tcl_module db
# Parameter $mode may be one of:
#
# "omit" - Implement filtering. Set the omit flag.
# "use" - Implement filtering. Use the constraint, but do not set omit.
# "use2" - Do not implement filtering. Use the constraint anyway.
#
#
proc t1_vtab {mode method args} {
switch -- $method {
xConnect {
return "CREATE TABLE t1(a, b)"
}
xBestIndex {
set SQL_FILTER {SELECT * FROM t1x WHERE a='%1%'}
set SQL_SCAN {SELECT * FROM t1x}
set clist [lindex $args 0]
set idx 0
for {set idx 0} {$idx < [llength $clist]} {incr idx} {
array unset C
array set C [lindex $clist $idx]
if {$C(column)==0 && $C(op)=="eq" && $C(usable)} {
switch -- $mode {
"omit" {
return [list omit $idx rows 10 cost 10 idxstr $SQL_FILTER]
}
"use" {
return [list use $idx rows 10 cost 10 idxstr $SQL_FILTER]
}
"use2" {
return [list use $idx rows 10 cost 10 idxstr $SQL_SCAN]
}
default {
error "Bad mode - $mode"
}
}
}
}
return [list idxstr {SELECT * FROM t1x}]
}
xFilter {
set map [list %1% [lindex $args 2 0]]
set sql [string map $map [lindex $args 1]]
return [list sql $sql]
}
}
return {}
}
do_execsql_test 2.1 {
CREATE TABLE t1x(i INTEGER PRIMARY KEY, a, b);
INSERT INTO t1x VALUES(1, 'one', 1);
INSERT INTO t1x VALUES(2, 'two', 2);
INSERT INTO t1x VALUES(3, 'three', 3);
INSERT INTO t1x VALUES(4, 'four', 4);
}
foreach {tn mode} {
1 use 2 omit 3 use2
} {
do_execsql_test 2.2.$mode.1 "
DROP TABLE IF EXISTS t1;
CREATE VIRTUAL TABLE t1 USING tcl(t1_vtab $mode);
"
do_execsql_test 2.2.$mode.2 {SELECT * FROM t1} {one 1 two 2 three 3 four 4}
do_execsql_test 2.2.$mode.3 {SELECT rowid FROM t1} {1 2 3 4}
do_execsql_test 2.2.$mode.4 {SELECT rowid FROM t1 WHERE a='two'} {2}
do_execsql_test 2.2.$mode.5 {
SELECT rowid FROM t1 WHERE a IN ('one', 'four') ORDER BY +rowid
} {1 4}
set plan(use) {
0 0 0 {SCAN TABLE t1 VIRTUAL TABLE INDEX 0:SELECT * FROM t1x WHERE a='%1%'}
0 0 0 {EXECUTE LIST SUBQUERY 1}
0 0 0 {USE TEMP B-TREE FOR ORDER BY}
}
set plan(omit) {
0 0 0 {SCAN TABLE t1 VIRTUAL TABLE INDEX 0:SELECT * FROM t1x WHERE a='%1%'}
0 0 0 {EXECUTE LIST SUBQUERY 1}
0 0 0 {USE TEMP B-TREE FOR ORDER BY}
}
set plan(use2) {
0 0 0 {SCAN TABLE t1 VIRTUAL TABLE INDEX 0:SELECT * FROM t1x}
0 0 0 {EXECUTE LIST SUBQUERY 1}
0 0 0 {USE TEMP B-TREE FOR ORDER BY}
}
do_eqp_test 2.2.$mode.6 {
SELECT rowid FROM t1 WHERE a IN ('one', 'four') ORDER BY +rowid
} $plan($mode)
}
finish_test