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 catch { array unset ctx_used }
152 for {set i 0} {$i < 100} {incr i} {
154 set ctx [expr abs(int(rand() *1000000))]
155 if {[info exists ctx_used($ctx)]==0} break
159 set file test_remote.db$ctx
161 forcedelete test.db$i
164 CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
165 INSERT INTO t1 VALUES($i, $i);
169 INSERT INTO swarm VALUES('test.db' || $i, 't1', $i, $i, $file)
171 set ::dbcache(test.db$i) 0
175 proc missing_db {filename ctx} {
176 file copy $ctx $filename
178 db func missing_db missing_db
180 proc openclose_db {filename ctx bClose} {
182 incr ::dbcache($filename) -1
184 incr ::dbcache($filename) 1
186 if {$::dbcache($filename)==0} {
187 forcedelete $filename
190 db func openclose_db openclose_db
192 proc check_dbcache {} {
194 foreach k [array names ::dbcache] {
195 set exists [file exists $k]
196 if {$exists!=($::dbcache($k)!=0)} {
197 error "inconsistent ::dbcache and disk ($k) - $exists"
204 foreach {tn nMaxOpen cvt} {
206 CREATE VIRTUAL TABLE temp.s USING swarmvtab(
207 'SELECT file, tbl, minval, minval, ctx FROM swarm',
209 openclose=openclose_db,
214 execsql { DROP TABLE IF EXISTS s }
216 do_execsql_test 3.$tn.1 $cvt
218 do_execsql_test 3.$tn.2 {
219 SELECT b FROM s WHERE a<10;
220 } {0 1 2 3 4 5 6 7 8 9}
222 do_test 3.$tn.3 { check_dbcache } $nMaxOpen
224 do_execsql_test 3.$tn.4 {
225 SELECT b FROM s WHERE (b%10)=0;
226 } {0 10 20 30 40 50 60 70 80 90}
228 do_test 3.$tn.5 { check_dbcache } $nMaxOpen
232 forcedelete {*}[glob test.db*]
233 forcedelete {*}[glob test_remote.db*]