| 1 |
# 2023-10-26 |
| 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 |
|
| 14 |
set testdir [file dirname $argv0] |
| 15 |
source $testdir/tester.tcl |
| 16 |
set testprefix bestindexB |
| 17 |
|
| 18 |
ifcapable !vtab { |
| 19 |
finish_test |
| 20 |
return |
| 21 |
} |
| 22 |
|
| 23 |
register_tcl_module db |
| 24 |
|
| 25 |
proc vtab_command {method args} { |
| 26 |
switch -- $method { |
| 27 |
xConnect { |
| 28 |
return "CREATE TABLE t1(a, b, c)" |
| 29 |
} |
| 30 |
|
| 31 |
xBestIndex { |
| 32 |
set hdl [lindex $args 0] |
| 33 |
set clist [$hdl constraints] |
| 34 |
set orderby [$hdl orderby] |
| 35 |
|
| 36 |
if {[info exists ::xbestindex_sql]} { |
| 37 |
# explain_i $::xbestindex_sql |
| 38 |
set ::xbestindex_res [ execsql $::xbestindex_sql ] |
| 39 |
} |
| 40 |
|
| 41 |
return "cost 1000000 rows 1000000 idxnum 0 idxstr hello" |
| 42 |
} |
| 43 |
|
| 44 |
xFilter { |
| 45 |
return "sql {SELECT 0, 1, 2, 3}" |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
return {} |
| 50 |
} |
| 51 |
|
| 52 |
do_execsql_test 1.0 { |
| 53 |
CREATE VIRTUAL TABLE x1 USING tcl(vtab_command); |
| 54 |
CREATE TABLE y1(a, b); |
| 55 |
CREATE TABLE y2(a, b); |
| 56 |
} {} |
| 57 |
|
| 58 |
do_execsql_test 1.1 { |
| 59 |
SELECT * FROM x1 |
| 60 |
} {1 2 3} |
| 61 |
|
| 62 |
do_execsql_test 1.2 { |
| 63 |
INSERT INTO y1 VALUES(1, 2) RETURNING rowid; |
| 64 |
} {1} |
| 65 |
|
| 66 |
do_execsql_test 1.3 { |
| 67 |
CREATE TRIGGER y1tr BEFORE INSERT ON y1 BEGIN |
| 68 |
SELECT * FROM x1; |
| 69 |
END; |
| 70 |
INSERT INTO y1 VALUES(3, 4) RETURNING rowid; |
| 71 |
} {2} |
| 72 |
|
| 73 |
|
| 74 |
# This time, rig the xBestIndex() method of the vtab to invoke an SQL |
| 75 |
# statement that uses RETURNING. |
| 76 |
set ::xbestindex_sql { |
| 77 |
INSERT INTO y2 VALUES(NULL, NULL) RETURNING rowid; |
| 78 |
} |
| 79 |
do_execsql_test 1.4 { |
| 80 |
INSERT INTO y1 VALUES(5, 6) RETURNING rowid; |
| 81 |
} {3} |
| 82 |
|
| 83 |
do_test 1.5 { |
| 84 |
set ::xbestindex_res |
| 85 |
} {1} |
| 86 |
|
| 87 |
finish_test |