3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. Specifically,
12 # it tests that the GLOB, LIKE and REGEXP operators are correctly exposed
13 # to virtual table implementations.
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
25 register_echo_module db
28 CREATE TABLE t6(a, b TEXT);
29 CREATE INDEX i6 ON t6(b, a);
30 CREATE VIRTUAL TABLE e6 USING echo(t6);
33 foreach {tn sql expect} {
34 1 "SELECT * FROM e6 WHERE b LIKE '8abc'" {
35 xBestIndex {SELECT rowid, a, b FROM 't6' WHERE b like ?}
36 xFilter {SELECT rowid, a, b FROM 't6' WHERE b like ?} 8abc
39 2 "SELECT * FROM e6 WHERE b GLOB '8abc'" {
40 xBestIndex {SELECT rowid, a, b FROM 't6' WHERE b glob ?}
41 xFilter {SELECT rowid, a, b FROM 't6' WHERE b glob ?} 8abc
52 #--------------------------------------------------------------------------
54 register_tclvar_module db
57 CREATE VIRTUAL TABLE vars USING tclvar;
58 SELECT name, arrayname, value FROM vars WHERE name = 'xyz';
71 db func glob -argcount 2 gfunc
77 db func like -argcount 2 lfunc
83 db func regexp -argcount 2 rfunc
89 foreach ::tclvar_set_omit {0 1} {
90 foreach {tn expr res cnt} {
91 1 {value GLOB 'aban*'} {x3 abandon x4 abandonint} 2
92 2 {value LIKE '%ac%'} {x1 aback x7 backbone x8 backarrow} 300
93 3 {value REGEXP '^......$'} {x5 babble x6 baboon x9 castle} 30000
97 if {$::tclvar_set_omit} {set cnt 0}
99 do_test 2.$tclvar_set_omit.$tn.1 {
100 execsql "SELECT name, value FROM vars WHERE name MATCH 'x*' AND $expr"
103 do_test 2.$tclvar_set_omit.$tn.2 {
109 #-------------------------------------------------------------------------
111 if {$tcl_platform(platform)=="windows"} {
112 set drive [string range [pwd] 0 1]
113 set ::env(fstreeDrive) $drive
115 if {$tcl_platform(platform)!="windows" || \
116 [regexp -nocase -- {^[A-Z]:} $drive]} {
118 register_fs_module db
119 do_execsql_test 3.0 {
120 SELECT name FROM fsdir WHERE dir = '.' AND name = 'test.db';
121 SELECT name FROM fsdir WHERE dir = '.' AND name = '.'
124 proc sort_files { names {nocase false} } {
125 if {$nocase && $::tcl_platform(platform) eq "windows"} {
126 return [lsort -nocase $names]
128 return [lsort $names]
132 proc list_root_files {} {
133 if {$::tcl_platform(platform) eq "windows"} {
134 set res [list]; set dir $::env(fstreeDrive)/; set names [list]
135 eval lappend names [glob -nocomplain -directory $dir -- *]
136 foreach name $names {
137 if {[string index [file tail $name] 0] eq "."} continue
138 if {[file attributes $name -hidden]} continue
139 if {[file attributes $name -system]} continue
142 return [sort_files $res true]
144 return [sort_files [string map {/ {}} [glob -nocomplain -- /*]]]
148 proc list_files { pattern } {
149 if {$::tcl_platform(platform) eq "windows"} {
150 set res [list]; set names [list]
151 eval lappend names [glob -nocomplain -- $pattern]
152 foreach name $names {
153 if {[string index [file tail $name] 0] eq "."} continue
154 if {[file attributes $name -hidden]} continue
155 if {[file attributes $name -system]} continue
158 return [sort_files $res]
160 return [sort_files [glob -nocomplain -- $pattern]]
164 # Read the first 5 entries from the root directory. Except, ignore
165 # files that contain the "$" character in their names as these are
166 # special files on some Windows platforms.
169 set root_files [list_root_files]
170 foreach p $root_files {
171 if {$::tcl_platform(platform) eq "windows"} {
172 if {![regexp {\$} $p]} {lappend res $p}
177 set num_root_files [llength $root_files]
179 sort_files [execsql {
180 SELECT path FROM fstree WHERE path NOT GLOB '*\$*' LIMIT $num_root_files;
182 } [sort_files $res true]
184 # Read all entries in the current directory.
186 proc contents {pattern} {
188 foreach f [list_files $pattern] {
190 if {[file isdir $f]} {
191 set res [concat $res [contents "$f/*"]]
197 set res [contents $pwd]
198 do_execsql_test 3.2 {
199 SELECT path FROM fstree WHERE path GLOB $pwd ORDER BY 1
202 # Add some sub-directories and files to the current directory.
205 catch { file delete -force subdir }
210 set dir [file dirname $path]
211 catch { file mkdir $dir }
212 set fd [open $path w]
213 puts -nonewline $fd [string repeat 1 $sz]
219 if {![string match {*[_%]*} $pwd]} {
220 do_execsql_test 3.5 {
221 SELECT path, size FROM fstree
222 WHERE path GLOB $pwd || '/subdir/*' ORDER BY 1
224 "$pwd/subdir/x1.txt" 143 \
225 "$pwd/subdir/x2.txt" 153 \
227 do_execsql_test 3.6 {
228 SELECT path, size FROM fstree
229 WHERE path LIKE $pwd || '/subdir/%' ORDER BY 1
231 "$pwd/subdir/x1.txt" 143 \
232 "$pwd/subdir/x2.txt" 153 \
234 do_execsql_test 3.7 {
235 SELECT sum(size) FROM fstree WHERE path LIKE $pwd || '/subdir/%'
237 do_execsql_test 3.8 {
238 SELECT size FROM fstree WHERE path = $pwd || '/subdir/x1.txt'