github.com/jdgcs/sqlite3@v1.12.1-0.20210908114423-bc5f96e4dd51/testdata/tcl/bestindex6.test (about) 1 # 2018-09-09 2 # 3 # The author disclaims copyright to this source code. In place of 4 # a legal notice, here is a blessing: 5 # 6 # May you do good and not evil. 7 # May you find forgiveness for yourself and forgive others. 8 # May you share freely, never taking more than you give. 9 # 10 #*********************************************************************** 11 # 12 13 set testdir [file dirname $argv0] 14 source $testdir/tester.tcl 15 set testprefix bestindex6 16 17 ifcapable !vtab { 18 finish_test 19 return 20 } 21 22 register_tcl_module db 23 24 proc vtab_command {src method args} { 25 switch -- $method { 26 xConnect { 27 return [db one {SELECT sql FROM sqlite_master where name = $src}] 28 } 29 30 xBestIndex { 31 set clist [lindex $args 0] 32 set wlist 1 33 34 set iCons 0 35 set ret [list] 36 foreach cons $clist { 37 catch { array unset C } 38 array set C $cons 39 40 if {$C(usable)} { 41 set col [db one { 42 SELECT name FROM pragma_table_info($src) WHERE cid=$C(column) 43 }] 44 switch $C(op) { 45 isnull { 46 lappend wlist "$col IS NULL" 47 lappend ret omit $iCons 48 } 49 eq { 50 lappend wlist "$col = %$iCons%" 51 lappend ret omit $iCons 52 } 53 } 54 } 55 incr iCons 56 } 57 #puts "xBestIndex: $ret" 58 lappend ret idxStr [join $wlist " AND "] 59 return $ret 60 } 61 62 xFilter { 63 foreach {idxnum idxstr aa} $args {} 64 set map [list] 65 for {set iCons 0} {$iCons < [llength $aa]} {incr iCons} { 66 lappend map %$iCons% [lindex $aa $iCons] 67 } 68 set ret [list sql \ 69 "SELECT rowid, * FROM $src WHERE [string map $map $idxstr]" 70 ] 71 # puts "xFilter: $ret" 72 return $ret 73 } 74 75 } 76 77 return {} 78 } 79 80 do_execsql_test 1.0 { 81 CREATE TABLE t1(id int, value text); 82 CREATE TABLE t2(ctx int, id int, value text); 83 84 INSERT INTO t1 VALUES(1,'try'); 85 INSERT INTO t2 VALUES(1,1,'good'); 86 INSERT INTO t2 VALUES(2,2,'evil'); 87 88 CREATE VIRTUAL TABLE vt1 USING tcl(vtab_command t1); 89 CREATE VIRTUAL TABLE vt2 USING tcl(vtab_command t2); 90 } 91 92 do_execsql_test 1.1 { 93 select * from t2 left join t1 on t1.id=t2.ctx where t1.value is null; 94 } {2 2 evil {} {}} 95 96 do_execsql_test 1.2 { 97 select * from vt2 left join vt1 on vt1.id=vt2.ctx where vt1.value is null; 98 } {2 2 evil {} {}} 99 100 unset -nocomplain xxx 101 do_execsql_test 1.3 { 102 select * from vt2 left join vt1 on vt1.id=vt2.ctx where vt1.value is $xxx; 103 } {2 2 evil {} {}} 104 105 do_execsql_test 1.4 { 106 select * from t2 left join vt1 on vt1.id=t2.ctx where vt1.value = 3 107 } {} 108 109 finish_test