1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
# 2025-09-02
#
# 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.
#
#***********************************************************************
#
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix bestindexE
ifcapable !vtab {
finish_test
return
}
register_tcl_module db
proc pretty_constraint {lCol cons} {
array set C $cons
set ret ""
if {$C(usable)} {
set OP(eq) =
set ret "[lindex $lCol $C(column)]$OP($C(op))?"
}
return $ret
}
proc vtab_command {zName lCol method args} {
switch -- $method {
xConnect {
return "CREATE TABLE $zName ([join $lCol ,]) "
}
xBestIndex {
set hdl [lindex $args 0]
set conslist [$hdl constraints]
set ret [list]
foreach cons $conslist {
set pretty [pretty_constraint $lCol $cons]
if {$pretty != ""} { lappend ret $pretty }
}
lappend ::xBestIndex "$zName: [join $ret { AND }]"
return "cost 1000 rows 1000 idxnum 555"
}
}
return {}
}
proc do_bestindex_test {tn sql lCons} {
set ::xBestIndex [list]
do_execsql_test $tn.1 $sql
uplevel [list do_test $tn.2 [list set ::xBestIndex] [list {*}$lCons]]
}
proc create_vtab {tname clist} {
set cmd [list vtab_command $tname $clist]
execsql "
CREATE VIRTUAL TABLE $tname USING tcl('$cmd')
"
}
do_test 1.0 {
create_vtab x1 {a b c}
} {}
do_bestindex_test 1.1 {
SELECT * FROM x1 WHERE a=?
} {{x1: a=?}}
do_bestindex_test 1.2 {
SELECT * FROM x1 WHERE a=? AND b=?
} {{x1: a=? AND b=?}}
#--------------------------------------------------------------------------
reset_db
register_tcl_module db
do_test 2.0 {
create_vtab Delivery {id customer}
create_vtab ReturnDelivery {id customer}
create_vtab Customer {oid name}
} {}
do_bestindex_test 2.1 {
SELECT Delivery.ID, Customer.Name
FROM Delivery LEFT JOIN
Customer ON Delivery.Customer = Customer.OID
} {
{Delivery: }
{Customer: oid=?}
}
do_bestindex_test 2.2 {
SELECT * FROM
(
SELECT Delivery.ID, Customer.Name
FROM Delivery LEFT JOIN
Customer ON Delivery.Customer = Customer.OID
UNION
SELECT ReturnDelivery.ID, Customer.Name
FROM ReturnDelivery LEFT JOIN
Customer ON ReturnDelivery.Customer = Customer.OID
)
WHERE ID = 1
} {
{Delivery: id=?}
{Customer: oid=?}
{ReturnDelivery: id=?}
{Customer: oid=?}
}
#--------------------------------------------------------------------------
reset_db
register_tcl_module db eponymous_cmd
proc eponymous_cmd {method args} {
switch -- $method {
xConnect {
db eval { SELECT * FROM sqlite_schema }
return "CREATE TABLE t1 (a, b)"
}
xBestIndex {
return "idxnum 555"
}
xFilter {
return [list sql {SELECT 123, 'A', 'B'}]
}
xUpdate {
return 123
}
}
return {}
}
do_execsql_test 3.1.0 {
PRAGMA table_info = tcl
} {
0 a {} 0 {} 0 1 b {} 0 {} 0
}
do_execsql_test 3.1.1 {
SELECT rowid, * FROM tcl
} {123 A B}
do_execsql_test 3.1.2 {
INSERT INTO tcl VALUES('i', 'ii') RETURNING *;
} {i ii}
do_test 3.1.3 {
db last_insert_rowid
} {123}
do_execsql_test 3.1.4 {
CREATE TABLE x1(x);
}
db close
sqlite3 db test.db
register_tcl_module db eponymous_cmd
sqlite3 db2 test.db
# Load the schema into connection [db]
do_execsql_test 3.2.1 { SELECT * FROM x1 }
# Modify the schema on disk
do_execsql_test -db db2 3.2.2 { CREATE TABLE x2(x) }
# Insert with RETURNING trigger on eponymous table.
do_execsql_test 3.2.3 {
INSERT INTO tcl VALUES('i', 'ii') RETURNING *;
} {i ii}
finish_test
|