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. The
12 # focus of this file is the "swarmvtab" extension
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
17 set testprefix swarmvtab3
25 load_static_extension db unionvtab
27 set nFile $sqlite_open_file_count
30 CREATE TEMP TABLE swarm(id, tbl, minval, maxval);
33 # Set up 100 databases with filenames "remote_test.dbN", where N is between
36 for {set i 0} {$i < 100} {incr i} {
37 set file remote_test.db$i
42 CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
43 INSERT INTO t1 VALUES($i, $i);
47 INSERT INTO swarm VALUES($i, 't1', $i, $i);
49 set ::dbcache(test.db$i) 0
53 proc missing_db {filename} {
54 set remote "remote_$filename"
56 file copy $remote $filename
58 db func missing_db missing_db
60 proc openclose_db {filename bClose} {
62 incr ::dbcache($filename) -1
64 incr ::dbcache($filename) 1
66 if {$::dbcache($filename)==0} {
70 db func openclose_db openclose_db
72 proc check_dbcache {} {
74 for {set i 0} {$i<100} {incr i} {
75 set exists [file exists test.db$i]
76 if {$exists!=($::dbcache(test.db$i)!=0)} {
77 error "inconsistent ::dbcache and disk ($i) - $exists"
84 foreach {tn nMaxOpen cvt} {
86 CREATE VIRTUAL TABLE temp.s USING swarmvtab(
87 'SELECT :prefix || id, tbl, minval, minval FROM swarm',
90 openclose=openclose_db,
96 CREATE VIRTUAL TABLE temp.s USING swarmvtab(
97 'SELECT :prefix || id, tbl, minval, minval FROM swarm',
99 missing = 'missing_db',
100 openclose=[openclose_db],
106 CREATE VIRTUAL TABLE temp.s USING swarmvtab(
107 'SELECT :prefix||''.''||:suffix||id, tbl, minval, minval FROM swarm',
108 :prefix=test, :suffix=db,
109 missing = 'missing_db',
110 openclose=[openclose_db],
116 execsql { DROP TABLE IF EXISTS s }
118 do_execsql_test 1.$tn.1 $cvt
120 do_execsql_test 1.$tn.2 {
121 SELECT b FROM s WHERE a<10;
122 } {0 1 2 3 4 5 6 7 8 9}
124 do_test 1.$tn.3 { check_dbcache } $nMaxOpen
126 do_execsql_test 1.$tn.4 {
127 SELECT b FROM s WHERE (b%10)=0;
128 } {0 10 20 30 40 50 60 70 80 90}
130 do_test 1.$tn.5 { check_dbcache } $nMaxOpen
133 execsql { DROP TABLE IF EXISTS s }
134 for {set i 0} {$i < 100} {incr i} {
135 forcedelete remote_test.db$i
138 #----------------------------------------------------------------------------
140 do_execsql_test 2.0 {
141 DROP TABLE IF EXISTS swarm;
142 CREATE TEMP TABLE swarm(file, tbl, minval, maxval, ctx);
145 catch { array unset ::dbcache }
147 # Set up 100 databases with filenames "remote_test.dbN", where N is a
148 # random integer between 0 and 1,000,000
151 for {set i 0} {$i < 100} {incr i} {
153 set ctx [expr abs(int(rand() *1000000))]
154 if {[info exists ::dbcache($ctx)]==0} break
157 set file test_remote.db$ctx
159 forcedelete test.db$i
162 CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
163 INSERT INTO t1 VALUES($i, $i);
167 INSERT INTO swarm VALUES('test.db' || $i, 't1', $i, $i, $file)
169 set ::dbcache(test.db$i) 0
173 proc missing_db {filename ctx} {
174 file copy $ctx $filename
176 db func missing_db missing_db
178 proc openclose_db {filename ctx bClose} {
180 incr ::dbcache($filename) -1
182 incr ::dbcache($filename) 1
184 if {$::dbcache($filename)==0} {
185 forcedelete $filename
188 db func openclose_db openclose_db
190 proc check_dbcache {} {
192 foreach k [array names ::dbcache] {
193 set exists [file exists $k]
194 if {$exists!=($::dbcache($k)!=0)} {
195 error "inconsistent ::dbcache and disk ($k) - $exists"
202 foreach {tn nMaxOpen cvt} {
204 CREATE VIRTUAL TABLE temp.s USING swarmvtab(
205 'SELECT file, tbl, minval, minval, ctx FROM swarm',
207 openclose=openclose_db,
212 execsql { DROP TABLE IF EXISTS s }
214 do_execsql_test 3.$tn.1 $cvt
216 do_execsql_test 3.$tn.2 {
217 SELECT b FROM s WHERE a<10;
218 } {0 1 2 3 4 5 6 7 8 9}
220 do_test 3.$tn.3 { check_dbcache } $nMaxOpen
222 do_execsql_test 3.$tn.4 {
223 SELECT b FROM s WHERE (b%10)=0;
224 } {0 10 20 30 40 50 60 70 80 90}
226 do_test 3.$tn.5 { check_dbcache } $nMaxOpen
230 forcedelete {*}[glob test.db*]
231 forcedelete {*}[glob test_remote.db*]