| 1 |
# 2020-01-29 |
| 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 bestindexA |
| 16 |
|
| 17 |
ifcapable !vtab { |
| 18 |
finish_test |
| 19 |
return |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
proc vtab_command {method args} { |
| 24 |
switch -- $method { |
| 25 |
xConnect { |
| 26 |
return "CREATE TABLE x(a, b, c)" |
| 27 |
} |
| 28 |
|
| 29 |
xBestIndex { |
| 30 |
set hdl [lindex $args 0] |
| 31 |
set clist [$hdl constraints] |
| 32 |
foreach c $clist { |
| 33 |
array set C $c |
| 34 |
lappend ::vtab_constraints [list $C(op) $C(column)] |
| 35 |
} |
| 36 |
return [list] |
| 37 |
} |
| 38 |
|
| 39 |
xFilter { |
| 40 |
return "" |
| 41 |
} |
| 42 |
|
| 43 |
xFindFunction { |
| 44 |
foreach {nArg name} $args {} |
| 45 |
if {$nArg==2 && $name=="even"} { |
| 46 |
return 152 |
| 47 |
} |
| 48 |
return 0 |
| 49 |
} |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
return {} |
| 54 |
} |
| 55 |
|
| 56 |
register_tcl_module db |
| 57 |
do_execsql_test 1.0 { |
| 58 |
CREATE VIRTUAL TABLE t1 USING tcl(vtab_command); |
| 59 |
} |
| 60 |
|
| 61 |
proc do_xbestindex_test {tn sql res} { |
| 62 |
set script [subst { |
| 63 |
execsql "$sql" |
| 64 |
set ::vtab_constraints |
| 65 |
}] |
| 66 |
|
| 67 |
uplevel [list do_test $tn $script [list {*}$res]] |
| 68 |
set ::vtab_constraints [list] |
| 69 |
} |
| 70 |
|
| 71 |
do_xbestindex_test 1.1 { |
| 72 |
SELECT * FROM t1 WHERE a=? |
| 73 |
} { |
| 74 |
{eq 0} |
| 75 |
} |
| 76 |
|
| 77 |
do_xbestindex_test 1.2 { |
| 78 |
SELECT * FROM t1 WHERE a=? LIMIT 10 |
| 79 |
} { |
| 80 |
{eq 0} |
| 81 |
{limit 0} |
| 82 |
} |
| 83 |
|
| 84 |
do_xbestindex_test 1.3 { |
| 85 |
SELECT * FROM t1 WHERE a=? AND (b+1)=? LIMIT 10 |
| 86 |
} { |
| 87 |
{eq 0} |
| 88 |
} |
| 89 |
|
| 90 |
proc error_function {args} { error "not a function!" } |
| 91 |
db function even error_function |
| 92 |
|
| 93 |
do_xbestindex_test 1.4 { |
| 94 |
SELECT * FROM t1 WHERE even(a, ?) |
| 95 |
} { |
| 96 |
{152 0} |
| 97 |
} |
| 98 |
|
| 99 |
do_xbestindex_test 1.5 { |
| 100 |
SELECT * FROM t1 WHERE b=10 AND even(a, ?) |
| 101 |
} { |
| 102 |
{eq 1} |
| 103 |
{152 0} |
| 104 |
} |
| 105 |
|
| 106 |
do_xbestindex_test 1.6 { |
| 107 |
SELECT * FROM t1 WHERE b=10 LIMIT 10 |
| 108 |
} { |
| 109 |
{eq 1} |
| 110 |
{limit 0} |
| 111 |
} |
| 112 |
|
| 113 |
do_xbestindex_test 1.7 { |
| 114 |
SELECT * FROM t1 WHERE even(b,?) LIMIT 10 |
| 115 |
} { |
| 116 |
{152 1} |
| 117 |
{limit 0} |
| 118 |
} |
| 119 |
|
| 120 |
do_xbestindex_test 1.8 { |
| 121 |
SELECT * FROM t1 WHERE b!=? LIMIT 10 |
| 122 |
} { |
| 123 |
{ne 1} |
| 124 |
{limit 0} |
| 125 |
} |
| 126 |
|
| 127 |
do_xbestindex_test 1.9 { |
| 128 |
SELECT * FROM t1 WHERE ?=a LIMIT 10 |
| 129 |
} { |
| 130 |
{eq 0} |
| 131 |
{limit 0} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
finish_test |