blob: 8dadb0f59de57776462ab238dae887cf17dbff50 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh348784e2000-05-29 20:41:49 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh348784e2000-05-29 20:41:49 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drh348784e2000-05-29 20:41:49 +00009#
10#***********************************************************************
11# This file implements some common TCL routines used for regression
12# testing the SQLite library
13#
drhbb77b752009-04-09 01:23:49 +000014# $Id: tester.tcl,v 1.143 2009/04/09 01:23:49 drh Exp $
drhfbc3eab2001-04-06 16:13:42 +000015
drh8359d8c2008-03-29 11:00:54 +000016#
17# What for user input before continuing. This gives an opportunity
18# to connect profiling tools to the process.
19#
20for {set i 0} {$i<[llength $argv]} {incr i} {
21 if {[regexp {^-+pause$} [lindex $argv $i] all value]} {
22 puts -nonewline "Press RETURN to begin..."
23 flush stdout
24 gets stdin
25 set argv [lreplace $argv $i $i]
26 }
27}
drh348784e2000-05-29 20:41:49 +000028
drh92febd92004-08-20 18:34:20 +000029set tcl_precision 15
drhc7a3bb92009-02-05 16:31:45 +000030sqlite3_test_control_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000031
drh3a7fb7c2007-08-10 16:41:08 +000032#
33# Check the command-line arguments for a default soft-heap-limit.
34# Store this default value in the global variable ::soft_limit and
35# update the soft-heap-limit each time this script is run. In that
36# way if an individual test file changes the soft-heap-limit, it
37# will be reset at the start of the next test file.
38#
39if {![info exists soft_limit]} {
40 set soft_limit 0
41 for {set i 0} {$i<[llength $argv]} {incr i} {
42 if {[regexp {^--soft-heap-limit=(.+)$} [lindex $argv $i] all value]} {
43 if {$value!="off"} {
44 set soft_limit $value
45 }
46 set argv [lreplace $argv $i $i]
47 }
48 }
49}
50sqlite3_soft_heap_limit $soft_limit
51
drh4a50aac2007-08-23 02:47:53 +000052#
53# Check the command-line arguments to set the memory debugger
54# backtrace depth.
55#
56# See the sqlite3_memdebug_backtrace() function in mem2.c or
57# test_malloc.c for additional information.
58#
59for {set i 0} {$i<[llength $argv]} {incr i} {
danielk197735754ac2008-03-21 17:29:37 +000060 if {[lindex $argv $i] eq "--malloctrace"} {
61 set argv [lreplace $argv $i $i]
danielk19775f096132008-03-28 15:44:09 +000062 sqlite3_memdebug_backtrace 10
danielk197735754ac2008-03-21 17:29:37 +000063 sqlite3_memdebug_log start
danielk197735754ac2008-03-21 17:29:37 +000064 set tester_do_malloctrace 1
65 }
66}
67for {set i 0} {$i<[llength $argv]} {incr i} {
drh4a50aac2007-08-23 02:47:53 +000068 if {[regexp {^--backtrace=(\d+)$} [lindex $argv $i] all value]} {
69 sqlite3_memdebug_backtrace $value
70 set argv [lreplace $argv $i $i]
71 }
72}
73
danielk19771e21fd52008-04-10 17:27:38 +000074
75proc ostrace_call {zCall nClick zFile i32 i64} {
danielk197727467042008-05-12 07:42:20 +000076 set s "INSERT INTO ostrace VALUES('$zCall', $nClick, '$zFile', $i32, $i64);"
danielk19771e21fd52008-04-10 17:27:38 +000077 puts $::ostrace_fd $s
78}
79
80for {set i 0} {$i<[llength $argv]} {incr i} {
danielk1977374177e2008-05-08 15:58:06 +000081 if {[lindex $argv $i] eq "--binarylog"} {
82 set tester_do_binarylog 1
danielk1977374177e2008-05-08 15:58:06 +000083 set argv [lreplace $argv $i $i]
84 }
danielk19771e21fd52008-04-10 17:27:38 +000085}
86
drh6a288a32008-01-07 19:20:24 +000087#
88# Check the command-line arguments to set the maximum number of
89# errors tolerated before halting.
90#
91if {![info exists maxErr]} {
92 set maxErr 1000
93}
94for {set i 0} {$i<[llength $argv]} {incr i} {
95 if {[regexp {^--maxerror=(\d+)$} [lindex $argv $i] all maxErr]} {
96 set argv [lreplace $argv $i $i]
97 }
98}
99#puts "Max error = $maxErr"
100
drh4a50aac2007-08-23 02:47:53 +0000101
drh9eb9e262004-02-11 02:18:05 +0000102# Use the pager codec if it is available
103#
drhef4ac8f2004-06-19 00:16:31 +0000104if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} {
105 rename sqlite3 sqlite_orig
106 proc sqlite3 {args} {
drh9eb9e262004-02-11 02:18:05 +0000107 if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} {
108 lappend args -key {xyzzy}
109 }
110 uplevel 1 sqlite_orig $args
111 }
112}
113
114
drhbec2bf42000-05-29 23:48:22 +0000115# Create a test database
116#
danielk1977f7b9d662008-06-23 18:49:43 +0000117if {![info exists nTest]} {
118 sqlite3_shutdown
119 install_malloc_faultsim 1
120 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000121 autoinstall_test_functions
danielk1977185eac92008-07-12 15:55:54 +0000122 if {[info exists tester_do_binarylog]} {
danfbefb892010-05-12 19:02:35 +0000123 vfslog new binarylog {} vfslog.bin
124 #sqlite3_instvfs marker binarylog "$argv0 $argv"
danielk1977185eac92008-07-12 15:55:54 +0000125 }
danielk1977f7b9d662008-06-23 18:49:43 +0000126}
danielk197703ba3fa2009-01-09 10:49:14 +0000127
128proc reset_db {} {
129 catch {db close}
130 file delete -force test.db
131 file delete -force test.db-journal
dand3f8f942010-04-13 11:35:01 +0000132 file delete -force test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000133 sqlite3 db ./test.db
134 set ::DB [sqlite3_connection_pointer db]
135 if {[info exists ::SETUP_SQL]} {
136 db eval $::SETUP_SQL
137 }
drhcd61c282002-03-06 22:01:34 +0000138}
danielk197703ba3fa2009-01-09 10:49:14 +0000139reset_db
drhbec2bf42000-05-29 23:48:22 +0000140
141# Abort early if this script has been run before.
142#
143if {[info exists nTest]} return
144
145# Set the test counters to zero
146#
drh348784e2000-05-29 20:41:49 +0000147set nErr 0
148set nTest 0
drh767c2002000-10-19 14:10:08 +0000149set skip_test 0
drha1b351a2001-09-14 16:42:12 +0000150set failList {}
drh521cc842008-04-15 02:36:33 +0000151set omitList {}
drhb62c3352006-11-23 09:39:16 +0000152if {![info exists speedTest]} {
153 set speedTest 0
154}
drh348784e2000-05-29 20:41:49 +0000155
drh521cc842008-04-15 02:36:33 +0000156# Record the fact that a sequence of tests were omitted.
157#
158proc omit_test {name reason} {
159 global omitList
160 lappend omitList [list $name $reason]
161}
162
drh348784e2000-05-29 20:41:49 +0000163# Invoke the do_test procedure to run a single test
164#
165proc do_test {name cmd expected} {
drhd3d39e92004-05-20 22:16:29 +0000166 global argv nErr nTest skip_test maxErr
drh4a50aac2007-08-23 02:47:53 +0000167 sqlite3_memdebug_settitle $name
danielk197706fb0402008-05-08 16:51:11 +0000168 if {[info exists ::tester_do_binarylog]} {
danfbefb892010-05-12 19:02:35 +0000169 #sqlite3_instvfs marker binarylog "Start of $name"
danielk1977374177e2008-05-08 15:58:06 +0000170 }
drh767c2002000-10-19 14:10:08 +0000171 if {$skip_test} {
172 set skip_test 0
173 return
174 }
175 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +0000176 set go 1
177 } else {
178 set go 0
179 foreach pattern $argv {
180 if {[string match $pattern $name]} {
181 set go 1
182 break
183 }
184 }
185 }
186 if {!$go} return
187 incr nTest
drh5edc3122001-09-13 21:53:09 +0000188 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000189 flush stdout
190 if {[catch {uplevel #0 "$cmd;\n"} result]} {
191 puts "\nError: $result"
192 incr nErr
drha1b351a2001-09-14 16:42:12 +0000193 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000194 if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000195 } elseif {[string compare $result $expected]} {
196 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
197 incr nErr
drha1b351a2001-09-14 16:42:12 +0000198 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000199 if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000200 } else {
201 puts " Ok"
202 }
drh6558db82007-04-13 03:23:21 +0000203 flush stdout
danielk197706fb0402008-05-08 16:51:11 +0000204 if {[info exists ::tester_do_binarylog]} {
danfbefb892010-05-12 19:02:35 +0000205 #sqlite3_instvfs marker binarylog "End of $name"
danielk1977374177e2008-05-08 15:58:06 +0000206 }
drh348784e2000-05-29 20:41:49 +0000207}
208
drhb62c3352006-11-23 09:39:16 +0000209# Run an SQL script.
210# Return the number of microseconds per statement.
211#
drh3590f152006-11-23 21:09:10 +0000212proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000213 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000214 flush stdout
215 set speed [time {sqlite3_exec_nr db $sql}]
216 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000217 if {$tm == 0} {
218 set rate [format %20s "many"]
219 } else {
220 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
221 }
drh3590f152006-11-23 21:09:10 +0000222 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000223 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000224 global total_time
225 set total_time [expr {$total_time+$tm}]
226}
drh45c236d2008-03-22 01:08:00 +0000227proc speed_trial_tcl {name numstmt units script} {
228 puts -nonewline [format {%-21.21s } $name...]
229 flush stdout
230 set speed [time {eval $script}]
231 set tm [lindex $speed 0]
232 if {$tm == 0} {
233 set rate [format %20s "many"]
234 } else {
235 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
236 }
237 set u2 $units/s
238 puts [format {%12d uS %s %s} $tm $rate $u2]
239 global total_time
240 set total_time [expr {$total_time+$tm}]
241}
drhdd924312007-03-31 22:34:16 +0000242proc speed_trial_init {name} {
243 global total_time
244 set total_time 0
245}
246proc speed_trial_summary {name} {
247 global total_time
248 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000249}
250
drh348784e2000-05-29 20:41:49 +0000251# Run this routine last
252#
253proc finish_test {} {
drha1b351a2001-09-14 16:42:12 +0000254 finalize_testing
255}
256proc finalize_testing {} {
drh521cc842008-04-15 02:36:33 +0000257 global nTest nErr sqlite_open_file_count omitList
danielk19772e588c72005-12-09 14:25:08 +0000258
drh6e142f52000-06-08 13:36:40 +0000259 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000260 catch {db2 close}
261 catch {db3 close}
262
drh9bc54492007-10-23 14:49:59 +0000263 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000264 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000265 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000266 db close
drh984bfaa2008-03-19 16:08:53 +0000267 sqlite3_reset_auto_extension
drh3a7fb7c2007-08-10 16:41:08 +0000268 set heaplimit [sqlite3_soft_heap_limit]
269 if {$heaplimit!=$::soft_limit} {
270 puts "soft-heap-limit changed by this script\
271 from $::soft_limit to $heaplimit"
272 } elseif {$heaplimit!="" && $heaplimit>0} {
273 puts "soft-heap-limit set to $heaplimit"
274 }
275 sqlite3_soft_heap_limit 0
drhb4bc7052006-01-11 23:40:33 +0000276 incr nTest
drh348784e2000-05-29 20:41:49 +0000277 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000278 if {$nErr>0} {
279 puts "Failures on these tests: $::failList"
280 }
danielk1977ee8b7992009-03-26 17:13:06 +0000281 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000282 if {[llength $omitList]>0} {
283 puts "Omitted test cases:"
284 set prec {}
285 foreach {rec} [lsort $omitList] {
286 if {$rec==$prec} continue
287 set prec $rec
288 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
289 }
290 }
drh80788d82006-09-02 14:50:23 +0000291 if {$nErr>0 && ![working_64bit_int]} {
292 puts "******************************************************************"
293 puts "N.B.: The version of TCL that you used to build this test harness"
294 puts "is defective in that it does not support 64-bit integers. Some or"
295 puts "all of the test failures above might be a result from this defect"
296 puts "in your TCL build."
297 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000298 }
danielk1977374177e2008-05-08 15:58:06 +0000299 if {[info exists ::tester_do_binarylog]} {
danfbefb892010-05-12 19:02:35 +0000300 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000301 }
drh94e92032003-02-16 22:21:32 +0000302 if {$sqlite_open_file_count} {
303 puts "$sqlite_open_file_count files were left open"
304 incr nErr
305 }
drhf3a65f72007-08-22 20:18:21 +0000306 if {[sqlite3_memory_used]>0} {
307 puts "Unfreed memory: [sqlite3_memory_used] bytes"
308 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000309 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000310 puts "Writing unfreed memory log to \"./memleak.txt\""
311 sqlite3_memdebug_dump ./memleak.txt
312 }
drhf3a65f72007-08-22 20:18:21 +0000313 } else {
314 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000315 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000316 sqlite3_memdebug_dump ./memusage.txt
317 }
drhf3a65f72007-08-22 20:18:21 +0000318 }
drhf7141992008-06-19 00:16:08 +0000319 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000320 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
321 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000322 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
323 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
324 }
danielk197735754ac2008-03-21 17:29:37 +0000325 if {[info exists ::tester_do_malloctrace]} {
326 puts "Writing mallocs.sql..."
327 memdebug_log_sql
328 sqlite3_memdebug_log stop
329 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000330
331 if {[sqlite3_memory_used]>0} {
332 puts "Writing leaks.sql..."
333 sqlite3_memdebug_log sync
334 memdebug_log_sql leaks.sql
335 }
danielk197735754ac2008-03-21 17:29:37 +0000336 }
drhd9910fe2006-10-04 11:55:49 +0000337 foreach f [glob -nocomplain test.db-*-journal] {
338 file delete -force $f
339 }
340 foreach f [glob -nocomplain test.db-mj*] {
341 file delete -force $f
342 }
drhdb25e382001-03-15 18:21:22 +0000343 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000344}
345
drhf7141992008-06-19 00:16:08 +0000346# Display memory statistics for analysis and debugging purposes.
347#
348proc show_memstats {} {
349 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000350 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
351 set val [format {now %10d max %10d max-size %10d} \
352 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000353 puts "Memory used: $val"
354 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000355 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
356 set val [format {now %10d max %10d max-size %10d} \
357 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000358 puts "Page-cache used: $val"
359 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
360 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
361 puts "Page-cache overflow: $val"
362 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
363 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
364 puts "Scratch memory used: $val"
365 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +0000366 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
367 set val [format {now %10d max %10d max-size %10d} \
368 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000369 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +0000370 ifcapable yytrackmaxstackdepth {
371 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
372 set val [format { max %10d} [lindex $x 2]]
373 puts "Parser stack depth: $val"
374 }
drhf7141992008-06-19 00:16:08 +0000375}
376
drh348784e2000-05-29 20:41:49 +0000377# A procedure to execute SQL
378#
drhc4a3c772001-04-04 11:48:57 +0000379proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000380 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000381 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000382}
drh3aadb2e2000-05-31 17:59:25 +0000383
drhadbca9c2001-09-27 15:11:53 +0000384# Execute SQL and catch exceptions.
385#
386proc catchsql {sql {db db}} {
387 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +0000388 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +0000389 lappend r $msg
390 return $r
391}
392
drh04096482001-11-09 22:41:44 +0000393# Do an VDBE code dump on the SQL given
394#
395proc explain {sql {db db}} {
396 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000397 puts "addr opcode p1 p2 p3 p4 p5 #"
398 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000399 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000400 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
401 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
402 ]
drh04096482001-11-09 22:41:44 +0000403 }
404}
405
drhdafc0ce2008-04-17 19:14:02 +0000406# Show the VDBE program for an SQL statement but omit the Trace
407# opcode at the beginning. This procedure can be used to prove
408# that different SQL statements generate exactly the same VDBE code.
409#
410proc explain_no_trace {sql} {
411 set tr [db eval "EXPLAIN $sql"]
412 return [lrange $tr 7 end]
413}
414
drh3aadb2e2000-05-31 17:59:25 +0000415# Another procedure to execute SQL. This one includes the field
416# names in the returned list.
417#
418proc execsql2 {sql} {
419 set result {}
420 db eval $sql data {
421 foreach f $data(*) {
422 lappend result $f $data($f)
423 }
424 }
425 return $result
426}
drh17a68932001-01-31 13:28:08 +0000427
drhdde85d92003-03-01 19:45:34 +0000428# Use the non-callback API to execute multiple SQL statements
429#
430proc stepsql {dbptr sql} {
431 set sql [string trim $sql]
432 set r 0
433 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000434 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000435 return [list 1 $vm]
436 }
437 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000438# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
439# foreach v $VAL {lappend r $v}
440# }
441 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
442 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
443 lappend r [sqlite3_column_text $vm $i]
444 }
drhdde85d92003-03-01 19:45:34 +0000445 }
danielk1977106bb232004-05-21 10:08:53 +0000446 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000447 return [list 1 $errmsg]
448 }
449 }
450 return $r
451}
452
drh17a68932001-01-31 13:28:08 +0000453# Delete a file or directory
454#
455proc forcedelete {filename} {
456 if {[catch {file delete -force $filename}]} {
457 exec rm -rf $filename
458 }
459}
drh21504322002-06-25 13:16:02 +0000460
461# Do an integrity check of the entire database
462#
danielk197704103022009-02-03 16:51:24 +0000463proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +0000464 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +0000465 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +0000466 }
467}
468
danielk1977db4e8862008-01-16 08:24:46 +0000469proc fix_ifcapable_expr {expr} {
470 set ret ""
471 set state 0
472 for {set i 0} {$i < [string length $expr]} {incr i} {
473 set char [string range $expr $i $i]
474 set newstate [expr {[string is alnum $char] || $char eq "_"}]
475 if {$newstate && !$state} {
476 append ret {$::sqlite_options(}
477 }
478 if {!$newstate && $state} {
479 append ret )
480 }
481 append ret $char
482 set state $newstate
483 }
484 if {$state} {append ret )}
485 return $ret
486}
487
drh27d258a2004-10-30 20:23:09 +0000488# Evaluate a boolean expression of capabilities. If true, execute the
489# code. Omit the code if false.
490#
danielk19773e8c37e2005-01-21 03:12:14 +0000491proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000492 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
493 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000494 if ($e2) {
495 set c [catch {uplevel 1 $code} r]
496 } else {
497 set c [catch {uplevel 1 $elsecode} r]
498 }
499 return -code $c $r
drh21504322002-06-25 13:16:02 +0000500}
danielk197745901d62004-11-10 15:27:38 +0000501
danielk1977aca790a2005-01-13 11:07:52 +0000502# This proc execs a seperate process that crashes midway through executing
503# the SQL script $sql on database test.db.
504#
505# The crash occurs during a sync() of file $crashfile. When the crash
506# occurs a random subset of all unsynced writes made by the process are
507# written into the files on disk. Argument $crashdelay indicates the
508# number of file syncs to wait before crashing.
509#
510# The return value is a list of two elements. The first element is a
511# boolean, indicating whether or not the process actually crashed or
512# reported some other error. The second element in the returned list is the
513# error message. This is "child process exited abnormally" if the crash
514# occured.
515#
danielk1977f8940ae2007-08-23 11:07:10 +0000516# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000517#
518proc crashsql {args} {
danielk1977aca790a2005-01-13 11:07:52 +0000519 if {$::tcl_platform(platform)!="unix"} {
520 error "crashsql should only be used on unix"
521 }
danielk197759a33f92007-03-17 10:26:59 +0000522
523 set blocksize ""
524 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000525 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000526 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000527 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000528 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000529 set sql [lindex $args end]
530
531 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
532 set z [lindex $args $ii]
533 set n [string length $z]
534 set z2 [lindex $args [expr $ii+1]]
535
536 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000537 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000538 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000539 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000540 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000541 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000542 else { error "Unrecognized option: $z" }
543 }
544
545 if {$crashfile eq ""} {
546 error "Compulsory option -file missing"
547 }
548
danielk1977aca790a2005-01-13 11:07:52 +0000549 set cfile [file join [pwd] $crashfile]
550
551 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000552 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000553 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +0000554 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000555 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000556
557 # This block sets the cache size of the main database to 10
558 # pages. This is done in case the build is configured to omit
559 # "PRAGMA cache_size".
560 puts $f {db eval {SELECT * FROM sqlite_master;}}
561 puts $f {set bt [btree_from_db db]}
562 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000563 if {$prngseed} {
564 set seed [expr {$prngseed%10007+1}]
565 # puts seed=$seed
566 puts $f "db eval {SELECT randomblob($seed)}"
567 }
danielk1977933bbd62007-03-17 07:22:42 +0000568
drhf8587402008-01-08 16:03:49 +0000569 if {[string length $tclbody]>0} {
570 puts $f $tclbody
571 }
572 if {[string length $sql]>0} {
573 puts $f "db eval {"
574 puts $f "$sql"
575 puts $f "}"
576 }
danielk1977aca790a2005-01-13 11:07:52 +0000577 close $f
578
579 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000580 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000581 } msg]
582 lappend r $msg
583}
584
danielk197732554c12005-01-22 03:39:39 +0000585# Usage: do_ioerr_test <test number> <options...>
586#
587# This proc is used to implement test cases that check that IO errors
588# are correctly handled. The first argument, <test number>, is an integer
589# used to name the tests executed by this proc. Options are as follows:
590#
591# -tclprep TCL script to run to prepare test.
592# -sqlprep SQL script to run to prepare test.
593# -tclbody TCL script to run with IO error simulation.
594# -sqlbody TCL script to run with IO error simulation.
595# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000596# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000597# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000598# -start Value of 'N' to begin with (default 1)
599#
600# -cksum Boolean. If true, test that the database does
601# not change during the execution of the test case.
602#
603proc do_ioerr_test {testname args} {
604
danielk197732554c12005-01-22 03:39:39 +0000605 set ::ioerropts(-start) 1
606 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000607 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000608 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000609 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +0000610 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +0000611 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +0000612 array set ::ioerropts $args
613
danielk197747cd39c2008-05-12 12:41:15 +0000614 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
615 # a couple of obscure IO errors that do not return them.
616 set ::ioerropts(-erc) 0
617
danielk197732554c12005-01-22 03:39:39 +0000618 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000619 #reset_prng_state
620 save_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +0000621 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000622 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000623 incr ::ioerropts(-count) -1
624 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000625
626 # Skip this IO error if it was specified with the "-exclude" option.
627 if {[info exists ::ioerropts(-exclude)]} {
628 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
629 }
danielk1977861f7452008-06-05 11:39:11 +0000630 if {$::ioerropts(-restoreprng)} {
631 restore_prng_state
632 }
danielk197732554c12005-01-22 03:39:39 +0000633
634 # Delete the files test.db and test2.db, then execute the TCL and
635 # SQL (in that order) to prepare for the test case.
636 do_test $testname.$n.1 {
637 set ::sqlite_io_error_pending 0
638 catch {db close}
aswiftaebf4132008-11-21 00:10:35 +0000639 catch {db2 close}
danielk197732554c12005-01-22 03:39:39 +0000640 catch {file delete -force test.db}
641 catch {file delete -force test.db-journal}
642 catch {file delete -force test2.db}
643 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000644 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000645 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000646 if {[info exists ::ioerropts(-tclprep)]} {
647 eval $::ioerropts(-tclprep)
648 }
649 if {[info exists ::ioerropts(-sqlprep)]} {
650 execsql $::ioerropts(-sqlprep)
651 }
652 expr 0
653 } {0}
654
655 # Read the 'checksum' of the database.
656 if {$::ioerropts(-cksum)} {
657 set checksum [cksum]
658 }
drh93aed5a2008-01-16 17:46:38 +0000659
danielk197732554c12005-01-22 03:39:39 +0000660 # Set the Nth IO error to fail.
661 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000662 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000663 set ::sqlite_io_error_pending $n
664 }] $n
665
666 # Create a single TCL script from the TCL and SQL specified
667 # as the body of the test.
668 set ::ioerrorbody {}
669 if {[info exists ::ioerropts(-tclbody)]} {
670 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
671 }
672 if {[info exists ::ioerropts(-sqlbody)]} {
673 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
674 }
675
676 # Execute the TCL Script created in the above block. If
677 # there are at least N IO operations performed by SQLite as
678 # a result of the script, the Nth will fail.
679 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +0000680 set ::sqlite_io_error_hit 0
681 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +0000682 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +0000683 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +0000684 set rc [sqlite3_errcode $::DB]
685 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000686 # If we are in extended result code mode, make sure all of the
687 # IOERRs we get back really do have their extended code values.
688 # If an extended result code is returned, the sqlite3_errcode
689 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
690 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000691 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
692 return $rc
693 }
694 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000695 # If we are not in extended result code mode, make sure no
696 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000697 if {[regexp {\+\d} $rc]} {
698 return $rc
699 }
700 }
drh93aed5a2008-01-16 17:46:38 +0000701 # The test repeats as long as $::go is non-zero. $::go starts out
702 # as 1. When a test runs to completion without hitting an I/O
703 # error, that means there is no point in continuing with this test
704 # case so set $::go to zero.
705 #
706 if {$::sqlite_io_error_pending>0} {
707 set ::go 0
708 set q 0
709 set ::sqlite_io_error_pending 0
710 } else {
711 set q 1
712 }
713
drhc9ac5ca2005-11-04 22:03:30 +0000714 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +0000715 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
716 set r 1
717 }
danielk1977f2fa8312006-01-24 13:09:33 +0000718 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000719
720 # One of two things must have happened. either
721 # 1. We never hit the IO error and the SQL returned OK
722 # 2. An IO error was hit and the SQL failed
723 #
dand41a29a2010-05-06 15:56:28 +0000724 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +0000725 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +0000726 } {1}
727
danielk197780daec62008-05-12 10:57:02 +0000728 set ::sqlite_io_error_hit 0
729 set ::sqlite_io_error_pending 0
730
danielk197752b472a2008-05-05 16:23:55 +0000731 # Check that no page references were leaked. There should be
732 # a single reference if there is still an active transaction,
733 # or zero otherwise.
734 #
danielk197781620542008-06-07 05:19:37 +0000735 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
736 # locks are established on database files (i.e. if the error
737 # occurs while attempting to detect a hot-journal file), then
738 # there may 0 page references and an active transaction according
739 # to [sqlite3_get_autocommit].
740 #
danielk19774abd5442008-05-05 15:26:50 +0000741 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +0000742 do_test $testname.$n.4 {
743 set bt [btree_from_db db]
744 db_enter db
745 array set stats [btree_pager_stats $bt]
746 db_leave db
danielk197781620542008-06-07 05:19:37 +0000747 set nRef $stats(ref)
748 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
749 } {1}
danielk19774abd5442008-05-05 15:26:50 +0000750 }
751
danielk197752b472a2008-05-05 16:23:55 +0000752 # If there is an open database handle and no open transaction,
753 # and the pager is not running in exclusive-locking mode,
754 # check that the pager is in "unlocked" state. Theoretically,
755 # if a call to xUnlock() failed due to an IO error the underlying
756 # file may still be locked.
757 #
758 ifcapable pragma {
759 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +0000760 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +0000761 && [db one {pragma locking_mode}] eq "normal"
762 && [sqlite3_get_autocommit db]
763 } {
764 do_test $testname.$n.5 {
765 set bt [btree_from_db db]
766 db_enter db
767 array set stats [btree_pager_stats $bt]
768 db_leave db
769 set stats(state)
770 } 0
771 }
772 }
773
danielk197732554c12005-01-22 03:39:39 +0000774 # If an IO error occured, then the checksum of the database should
775 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +0000776 #
drh1aa5af12008-03-07 19:51:14 +0000777 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +0000778 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +0000779 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +0000780 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +0000781 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000782 cksum
783 } $checksum
784 }
785
drh1aa5af12008-03-07 19:51:14 +0000786 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +0000787 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000788 if {[info exists ::ioerropts(-cleanup)]} {
789 catch $::ioerropts(-cleanup)
790 }
danielk197732554c12005-01-22 03:39:39 +0000791 }
792 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000793 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000794 unset ::ioerropts
795}
796
drh93aed5a2008-01-16 17:46:38 +0000797# Return a checksum based on the contents of the main database associated
798# with connection $db
danielk197732554c12005-01-22 03:39:39 +0000799#
800proc cksum {{db db}} {
801 set txt [$db eval {
802 SELECT name, type, sql FROM sqlite_master order by name
803 }]\n
804 foreach tbl [$db eval {
805 SELECT name FROM sqlite_master WHERE type='table' order by name
806 }] {
807 append txt [$db eval "SELECT * FROM $tbl"]\n
808 }
809 foreach prag {default_synchronous default_cache_size} {
810 append txt $prag-[$db eval "PRAGMA $prag"]\n
811 }
812 set cksum [string length $txt]-[md5 $txt]
813 # puts $cksum-[file size test.db]
814 return $cksum
815}
816
drh93aed5a2008-01-16 17:46:38 +0000817# Generate a checksum based on the contents of the main and temp tables
818# database $db. If the checksum of two databases is the same, and the
819# integrity-check passes for both, the two databases are identical.
820#
drh1db639c2008-01-17 02:36:28 +0000821proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +0000822 set ret [list]
823 ifcapable tempdb {
824 set sql {
825 SELECT name FROM sqlite_master WHERE type = 'table' UNION
826 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
827 SELECT 'sqlite_master' UNION
828 SELECT 'sqlite_temp_master' ORDER BY 1
829 }
830 } else {
831 set sql {
832 SELECT name FROM sqlite_master WHERE type = 'table' UNION
833 SELECT 'sqlite_master' ORDER BY 1
834 }
835 }
836 set tbllist [$db eval $sql]
837 set txt {}
838 foreach tbl $tbllist {
839 append txt [$db eval "SELECT * FROM $tbl"]
840 }
841 foreach prag {default_cache_size} {
842 append txt $prag-[$db eval "PRAGMA $prag"]\n
843 }
844 # puts txt=$txt
845 return [md5 $txt]
846}
847
drhdc2c4912009-02-04 22:46:47 +0000848# Generate a checksum based on the contents of a single database with
849# a database connection. The name of the database is $dbname.
850# Examples of $dbname are "temp" or "main".
851#
852proc dbcksum {db dbname} {
853 if {$dbname=="temp"} {
854 set master sqlite_temp_master
855 } else {
856 set master $dbname.sqlite_master
857 }
858 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
859 set txt [$db eval "SELECT * FROM $master"]\n
860 foreach tab $alltab {
861 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
862 }
863 return [md5 $txt]
864}
865
danielk197735754ac2008-03-21 17:29:37 +0000866proc memdebug_log_sql {{filename mallocs.sql}} {
867
danielk19776f332c12008-03-21 14:22:44 +0000868 set data [sqlite3_memdebug_log dump]
869 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +0000870 if {$nFrame < 0} { return "" }
871
danielk197735754ac2008-03-21 17:29:37 +0000872 set database temp
873
danielk19776ab3a2e2009-02-19 14:39:25 +0000874 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +0000875
876 set sql ""
877 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +0000878 set nCall [lindex $e 0]
879 set nByte [lindex $e 1]
880 set lStack [lrange $e 2 end]
881 append sql "INSERT INTO ${database}.malloc VALUES"
882 append sql "('test', $nCall, $nByte, '$lStack');\n"
883 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +0000884 set frames($f) 1
885 }
886 }
887
888 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +0000889 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +0000890
891 foreach f [array names frames] {
892 set addr [format %x $f]
893 set cmd "addr2line -e [info nameofexec] $addr"
894 set line [eval exec $cmd]
895 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +0000896
897 set file [lindex [split $line :] 0]
898 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +0000899 }
900
danielk197735754ac2008-03-21 17:29:37 +0000901 foreach f [array names files] {
902 set contents ""
903 catch {
904 set fd [open $f]
905 set contents [read $fd]
906 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000907 }
danielk197735754ac2008-03-21 17:29:37 +0000908 set contents [string map {' ''} $contents]
909 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +0000910 }
danielk19776f332c12008-03-21 14:22:44 +0000911
danielk197735754ac2008-03-21 17:29:37 +0000912 set fd [open $filename w]
913 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
914 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000915}
drh93aed5a2008-01-16 17:46:38 +0000916
danielk197732554c12005-01-22 03:39:39 +0000917# Copy file $from into $to. This is used because some versions of
918# TCL for windows (notably the 8.4.1 binary package shipped with the
919# current mingw release) have a broken "file copy" command.
920#
921proc copy_file {from to} {
922 if {$::tcl_platform(platform)=="unix"} {
923 file copy -force $from $to
924 } else {
925 set f [open $from]
926 fconfigure $f -translation binary
927 set t [open $to w]
928 fconfigure $t -translation binary
929 puts -nonewline $t [read $f [file size $from]]
930 close $t
931 close $f
932 }
933}
934
danf5894502009-10-07 18:41:19 +0000935# Drop all tables in database [db]
936proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +0000937 ifcapable trigger&&foreignkey {
938 set pk [$db one "PRAGMA foreign_keys"]
939 $db eval "PRAGMA foreign_keys = OFF"
940 }
drh9a6ffc82010-02-15 18:03:20 +0000941 foreach {idx name file} [db eval {PRAGMA database_list}] {
942 if {$idx==1} {
943 set master sqlite_temp_master
944 } else {
945 set master $name.sqlite_master
946 }
947 foreach {t type} [$db eval "
948 SELECT name, type FROM $master
949 WHERE type IN('table', 'view') AND name NOT like 'sqlite_%'
950 "] {
951 $db eval "DROP $type $t"
952 }
danf5894502009-10-07 18:41:19 +0000953 }
shaneh4e7b32f2009-12-17 22:12:51 +0000954 ifcapable trigger&&foreignkey {
955 $db eval "PRAGMA foreign_keys = $pk"
956 }
danf5894502009-10-07 18:41:19 +0000957}
958
dan71cb5182010-04-26 12:39:03 +0000959#-------------------------------------------------------------------------
960# If a test script is executed with global variable
961# $::permutations_test_prefix set to "wal", then the tests are run
962# in WAL mode. Otherwise, they should be run in rollback mode. The
963# following Tcl procs are used to make this less intrusive:
964#
965# wal_set_journal_mode ?DB?
966#
967# If running a WAL test, execute "PRAGMA journal_mode = wal" using
968# connection handle DB. Otherwise, this command is a no-op.
969#
970# wal_check_journal_mode TESTNAME ?DB?
971#
972# If running a WAL test, execute a tests case that fails if the main
973# database for connection handle DB is not currently a WAL database.
974# Otherwise (if not running a WAL permutation) this is a no-op.
975#
976# wal_is_wal_mode
977#
978# Returns true if this test should be run in WAL mode. False otherwise.
979#
980proc wal_is_wal_mode {} {
981 expr { [catch {set ::permutations_test_prefix} v]==0 && $v == "wal" }
982}
983proc wal_set_journal_mode {{db db}} {
984 if { [wal_is_wal_mode] } {
985 $db eval "PRAGMA journal_mode = WAL"
986 }
987}
988proc wal_check_journal_mode {testname {db db}} {
989 if { [wal_is_wal_mode] } {
990 $db eval { SELECT * FROM sqlite_master }
991 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
992 }
993}
994
danf5894502009-10-07 18:41:19 +0000995
danielk197745901d62004-11-10 15:27:38 +0000996# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
997# to non-zero, then set the global variable $AUTOVACUUM to 1.
998set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +0000999
1000source $testdir/thread_common.tcl