blob: 44798a70988ae82cb821a02f71be0627aa400632 [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} {
81 if {[lindex $argv $i] eq "--ossummary" || [lindex $argv $i] eq "--ostrace"} {
82 sqlite3_instvfs create -default ostrace
83 set tester_do_ostrace 1
84 set ostrace_fd [open ostrace.sql w]
85 puts $ostrace_fd "BEGIN;"
86 if {[lindex $argv $i] eq "--ostrace"} {
87 set s "CREATE TABLE ostrace"
88 append s "(method TEXT, clicks INT, file TEXT, i32 INT, i64 INT);"
89 puts $ostrace_fd $s
90 sqlite3_instvfs configure ostrace ostrace_call
danielk1977374177e2008-05-08 15:58:06 +000091 sqlite3_instvfs configure ostrace ostrace_call
danielk19771e21fd52008-04-10 17:27:38 +000092 }
93 set argv [lreplace $argv $i $i]
94 }
danielk1977374177e2008-05-08 15:58:06 +000095 if {[lindex $argv $i] eq "--binarylog"} {
96 set tester_do_binarylog 1
danielk1977374177e2008-05-08 15:58:06 +000097 set argv [lreplace $argv $i $i]
98 }
danielk19771e21fd52008-04-10 17:27:38 +000099}
100
drh6a288a32008-01-07 19:20:24 +0000101#
102# Check the command-line arguments to set the maximum number of
103# errors tolerated before halting.
104#
105if {![info exists maxErr]} {
106 set maxErr 1000
107}
108for {set i 0} {$i<[llength $argv]} {incr i} {
109 if {[regexp {^--maxerror=(\d+)$} [lindex $argv $i] all maxErr]} {
110 set argv [lreplace $argv $i $i]
111 }
112}
113#puts "Max error = $maxErr"
114
drh4a50aac2007-08-23 02:47:53 +0000115
drh9eb9e262004-02-11 02:18:05 +0000116# Use the pager codec if it is available
117#
drhef4ac8f2004-06-19 00:16:31 +0000118if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} {
119 rename sqlite3 sqlite_orig
120 proc sqlite3 {args} {
drh9eb9e262004-02-11 02:18:05 +0000121 if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} {
122 lappend args -key {xyzzy}
123 }
124 uplevel 1 sqlite_orig $args
125 }
126}
127
128
drhbec2bf42000-05-29 23:48:22 +0000129# Create a test database
130#
danielk1977f7b9d662008-06-23 18:49:43 +0000131if {![info exists nTest]} {
132 sqlite3_shutdown
133 install_malloc_faultsim 1
134 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000135 autoinstall_test_functions
danielk1977185eac92008-07-12 15:55:54 +0000136 if {[info exists tester_do_binarylog]} {
137 sqlite3_instvfs binarylog -default binarylog ostrace.bin
138 sqlite3_instvfs marker binarylog "$argv0 $argv"
139 }
danielk1977f7b9d662008-06-23 18:49:43 +0000140}
danielk197703ba3fa2009-01-09 10:49:14 +0000141
142proc reset_db {} {
143 catch {db close}
144 file delete -force test.db
145 file delete -force test.db-journal
dand3f8f942010-04-13 11:35:01 +0000146 file delete -force test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000147 sqlite3 db ./test.db
148 set ::DB [sqlite3_connection_pointer db]
149 if {[info exists ::SETUP_SQL]} {
150 db eval $::SETUP_SQL
151 }
drhcd61c282002-03-06 22:01:34 +0000152}
danielk197703ba3fa2009-01-09 10:49:14 +0000153reset_db
drhbec2bf42000-05-29 23:48:22 +0000154
155# Abort early if this script has been run before.
156#
157if {[info exists nTest]} return
158
159# Set the test counters to zero
160#
drh348784e2000-05-29 20:41:49 +0000161set nErr 0
162set nTest 0
drh767c2002000-10-19 14:10:08 +0000163set skip_test 0
drha1b351a2001-09-14 16:42:12 +0000164set failList {}
drh521cc842008-04-15 02:36:33 +0000165set omitList {}
drhb62c3352006-11-23 09:39:16 +0000166if {![info exists speedTest]} {
167 set speedTest 0
168}
drh348784e2000-05-29 20:41:49 +0000169
drh521cc842008-04-15 02:36:33 +0000170# Record the fact that a sequence of tests were omitted.
171#
172proc omit_test {name reason} {
173 global omitList
174 lappend omitList [list $name $reason]
175}
176
drh348784e2000-05-29 20:41:49 +0000177# Invoke the do_test procedure to run a single test
178#
179proc do_test {name cmd expected} {
drhd3d39e92004-05-20 22:16:29 +0000180 global argv nErr nTest skip_test maxErr
drh4a50aac2007-08-23 02:47:53 +0000181 sqlite3_memdebug_settitle $name
danielk197706fb0402008-05-08 16:51:11 +0000182 if {[info exists ::tester_do_binarylog]} {
danielk1977374177e2008-05-08 15:58:06 +0000183 sqlite3_instvfs marker binarylog "Start of $name"
184 }
drh767c2002000-10-19 14:10:08 +0000185 if {$skip_test} {
186 set skip_test 0
187 return
188 }
189 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +0000190 set go 1
191 } else {
192 set go 0
193 foreach pattern $argv {
194 if {[string match $pattern $name]} {
195 set go 1
196 break
197 }
198 }
199 }
200 if {!$go} return
201 incr nTest
drh5edc3122001-09-13 21:53:09 +0000202 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000203 flush stdout
204 if {[catch {uplevel #0 "$cmd;\n"} result]} {
205 puts "\nError: $result"
206 incr nErr
drha1b351a2001-09-14 16:42:12 +0000207 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000208 if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000209 } elseif {[string compare $result $expected]} {
210 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
211 incr nErr
drha1b351a2001-09-14 16:42:12 +0000212 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000213 if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000214 } else {
215 puts " Ok"
216 }
drh6558db82007-04-13 03:23:21 +0000217 flush stdout
danielk197706fb0402008-05-08 16:51:11 +0000218 if {[info exists ::tester_do_binarylog]} {
danielk1977374177e2008-05-08 15:58:06 +0000219 sqlite3_instvfs marker binarylog "End of $name"
220 }
drh348784e2000-05-29 20:41:49 +0000221}
222
drhb62c3352006-11-23 09:39:16 +0000223# Run an SQL script.
224# Return the number of microseconds per statement.
225#
drh3590f152006-11-23 21:09:10 +0000226proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000227 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000228 flush stdout
229 set speed [time {sqlite3_exec_nr db $sql}]
230 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000231 if {$tm == 0} {
232 set rate [format %20s "many"]
233 } else {
234 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
235 }
drh3590f152006-11-23 21:09:10 +0000236 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000237 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000238 global total_time
239 set total_time [expr {$total_time+$tm}]
240}
drh45c236d2008-03-22 01:08:00 +0000241proc speed_trial_tcl {name numstmt units script} {
242 puts -nonewline [format {%-21.21s } $name...]
243 flush stdout
244 set speed [time {eval $script}]
245 set tm [lindex $speed 0]
246 if {$tm == 0} {
247 set rate [format %20s "many"]
248 } else {
249 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
250 }
251 set u2 $units/s
252 puts [format {%12d uS %s %s} $tm $rate $u2]
253 global total_time
254 set total_time [expr {$total_time+$tm}]
255}
drhdd924312007-03-31 22:34:16 +0000256proc speed_trial_init {name} {
257 global total_time
258 set total_time 0
259}
260proc speed_trial_summary {name} {
261 global total_time
262 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000263}
264
drh348784e2000-05-29 20:41:49 +0000265# Run this routine last
266#
267proc finish_test {} {
drha1b351a2001-09-14 16:42:12 +0000268 finalize_testing
269}
270proc finalize_testing {} {
drh521cc842008-04-15 02:36:33 +0000271 global nTest nErr sqlite_open_file_count omitList
danielk19772e588c72005-12-09 14:25:08 +0000272
drh6e142f52000-06-08 13:36:40 +0000273 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000274 catch {db2 close}
275 catch {db3 close}
276
drh9bc54492007-10-23 14:49:59 +0000277 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000278 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000279 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000280 db close
drh984bfaa2008-03-19 16:08:53 +0000281 sqlite3_reset_auto_extension
drh3a7fb7c2007-08-10 16:41:08 +0000282 set heaplimit [sqlite3_soft_heap_limit]
283 if {$heaplimit!=$::soft_limit} {
284 puts "soft-heap-limit changed by this script\
285 from $::soft_limit to $heaplimit"
286 } elseif {$heaplimit!="" && $heaplimit>0} {
287 puts "soft-heap-limit set to $heaplimit"
288 }
289 sqlite3_soft_heap_limit 0
drhb4bc7052006-01-11 23:40:33 +0000290 incr nTest
drh348784e2000-05-29 20:41:49 +0000291 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000292 if {$nErr>0} {
293 puts "Failures on these tests: $::failList"
294 }
danielk1977ee8b7992009-03-26 17:13:06 +0000295 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000296 if {[llength $omitList]>0} {
297 puts "Omitted test cases:"
298 set prec {}
299 foreach {rec} [lsort $omitList] {
300 if {$rec==$prec} continue
301 set prec $rec
302 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
303 }
304 }
drh80788d82006-09-02 14:50:23 +0000305 if {$nErr>0 && ![working_64bit_int]} {
306 puts "******************************************************************"
307 puts "N.B.: The version of TCL that you used to build this test harness"
308 puts "is defective in that it does not support 64-bit integers. Some or"
309 puts "all of the test failures above might be a result from this defect"
310 puts "in your TCL build."
311 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000312 }
danielk1977374177e2008-05-08 15:58:06 +0000313 if {[info exists ::tester_do_binarylog]} {
314 sqlite3_instvfs destroy binarylog
315 }
drh94e92032003-02-16 22:21:32 +0000316 if {$sqlite_open_file_count} {
317 puts "$sqlite_open_file_count files were left open"
318 incr nErr
319 }
danielk19771e21fd52008-04-10 17:27:38 +0000320 if {[info exists ::tester_do_ostrace]} {
321 puts "Writing ostrace.sql..."
322 set fd $::ostrace_fd
323
324 puts -nonewline $fd "CREATE TABLE ossummary"
325 puts $fd "(method TEXT, clicks INTEGER, count INTEGER);"
326 foreach row [sqlite3_instvfs report ostrace] {
327 foreach {method count clicks} $row break
328 puts $fd "INSERT INTO ossummary VALUES('$method', $clicks, $count);"
329 }
330 puts $fd "COMMIT;"
331 close $fd
332 sqlite3_instvfs destroy ostrace
333 }
drhf3a65f72007-08-22 20:18:21 +0000334 if {[sqlite3_memory_used]>0} {
335 puts "Unfreed memory: [sqlite3_memory_used] bytes"
336 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000337 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000338 puts "Writing unfreed memory log to \"./memleak.txt\""
339 sqlite3_memdebug_dump ./memleak.txt
340 }
drhf3a65f72007-08-22 20:18:21 +0000341 } else {
342 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000343 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000344 sqlite3_memdebug_dump ./memusage.txt
345 }
drhf3a65f72007-08-22 20:18:21 +0000346 }
drhf7141992008-06-19 00:16:08 +0000347 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000348 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
349 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000350 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
351 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
352 }
danielk197735754ac2008-03-21 17:29:37 +0000353 if {[info exists ::tester_do_malloctrace]} {
354 puts "Writing mallocs.sql..."
355 memdebug_log_sql
356 sqlite3_memdebug_log stop
357 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000358
359 if {[sqlite3_memory_used]>0} {
360 puts "Writing leaks.sql..."
361 sqlite3_memdebug_log sync
362 memdebug_log_sql leaks.sql
363 }
danielk197735754ac2008-03-21 17:29:37 +0000364 }
drhd9910fe2006-10-04 11:55:49 +0000365 foreach f [glob -nocomplain test.db-*-journal] {
366 file delete -force $f
367 }
368 foreach f [glob -nocomplain test.db-mj*] {
369 file delete -force $f
370 }
drhdb25e382001-03-15 18:21:22 +0000371 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000372}
373
drhf7141992008-06-19 00:16:08 +0000374# Display memory statistics for analysis and debugging purposes.
375#
376proc show_memstats {} {
377 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000378 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
379 set val [format {now %10d max %10d max-size %10d} \
380 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000381 puts "Memory used: $val"
382 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000383 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
384 set val [format {now %10d max %10d max-size %10d} \
385 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000386 puts "Page-cache used: $val"
387 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
388 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
389 puts "Page-cache overflow: $val"
390 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
391 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
392 puts "Scratch memory used: $val"
393 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +0000394 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
395 set val [format {now %10d max %10d max-size %10d} \
396 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000397 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +0000398 ifcapable yytrackmaxstackdepth {
399 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
400 set val [format { max %10d} [lindex $x 2]]
401 puts "Parser stack depth: $val"
402 }
drhf7141992008-06-19 00:16:08 +0000403}
404
drh348784e2000-05-29 20:41:49 +0000405# A procedure to execute SQL
406#
drhc4a3c772001-04-04 11:48:57 +0000407proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000408 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000409 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000410}
drh3aadb2e2000-05-31 17:59:25 +0000411
drhadbca9c2001-09-27 15:11:53 +0000412# Execute SQL and catch exceptions.
413#
414proc catchsql {sql {db db}} {
415 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +0000416 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +0000417 lappend r $msg
418 return $r
419}
420
drh04096482001-11-09 22:41:44 +0000421# Do an VDBE code dump on the SQL given
422#
423proc explain {sql {db db}} {
424 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000425 puts "addr opcode p1 p2 p3 p4 p5 #"
426 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000427 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000428 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
429 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
430 ]
drh04096482001-11-09 22:41:44 +0000431 }
432}
433
drhdafc0ce2008-04-17 19:14:02 +0000434# Show the VDBE program for an SQL statement but omit the Trace
435# opcode at the beginning. This procedure can be used to prove
436# that different SQL statements generate exactly the same VDBE code.
437#
438proc explain_no_trace {sql} {
439 set tr [db eval "EXPLAIN $sql"]
440 return [lrange $tr 7 end]
441}
442
drh3aadb2e2000-05-31 17:59:25 +0000443# Another procedure to execute SQL. This one includes the field
444# names in the returned list.
445#
446proc execsql2 {sql} {
447 set result {}
448 db eval $sql data {
449 foreach f $data(*) {
450 lappend result $f $data($f)
451 }
452 }
453 return $result
454}
drh17a68932001-01-31 13:28:08 +0000455
drhdde85d92003-03-01 19:45:34 +0000456# Use the non-callback API to execute multiple SQL statements
457#
458proc stepsql {dbptr sql} {
459 set sql [string trim $sql]
460 set r 0
461 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000462 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000463 return [list 1 $vm]
464 }
465 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000466# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
467# foreach v $VAL {lappend r $v}
468# }
469 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
470 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
471 lappend r [sqlite3_column_text $vm $i]
472 }
drhdde85d92003-03-01 19:45:34 +0000473 }
danielk1977106bb232004-05-21 10:08:53 +0000474 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000475 return [list 1 $errmsg]
476 }
477 }
478 return $r
479}
480
drh17a68932001-01-31 13:28:08 +0000481# Delete a file or directory
482#
483proc forcedelete {filename} {
484 if {[catch {file delete -force $filename}]} {
485 exec rm -rf $filename
486 }
487}
drh21504322002-06-25 13:16:02 +0000488
489# Do an integrity check of the entire database
490#
danielk197704103022009-02-03 16:51:24 +0000491proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +0000492 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +0000493 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +0000494 }
495}
496
danielk1977db4e8862008-01-16 08:24:46 +0000497proc fix_ifcapable_expr {expr} {
498 set ret ""
499 set state 0
500 for {set i 0} {$i < [string length $expr]} {incr i} {
501 set char [string range $expr $i $i]
502 set newstate [expr {[string is alnum $char] || $char eq "_"}]
503 if {$newstate && !$state} {
504 append ret {$::sqlite_options(}
505 }
506 if {!$newstate && $state} {
507 append ret )
508 }
509 append ret $char
510 set state $newstate
511 }
512 if {$state} {append ret )}
513 return $ret
514}
515
drh27d258a2004-10-30 20:23:09 +0000516# Evaluate a boolean expression of capabilities. If true, execute the
517# code. Omit the code if false.
518#
danielk19773e8c37e2005-01-21 03:12:14 +0000519proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000520 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
521 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000522 if ($e2) {
523 set c [catch {uplevel 1 $code} r]
524 } else {
525 set c [catch {uplevel 1 $elsecode} r]
526 }
527 return -code $c $r
drh21504322002-06-25 13:16:02 +0000528}
danielk197745901d62004-11-10 15:27:38 +0000529
danielk1977aca790a2005-01-13 11:07:52 +0000530# This proc execs a seperate process that crashes midway through executing
531# the SQL script $sql on database test.db.
532#
533# The crash occurs during a sync() of file $crashfile. When the crash
534# occurs a random subset of all unsynced writes made by the process are
535# written into the files on disk. Argument $crashdelay indicates the
536# number of file syncs to wait before crashing.
537#
538# The return value is a list of two elements. The first element is a
539# boolean, indicating whether or not the process actually crashed or
540# reported some other error. The second element in the returned list is the
541# error message. This is "child process exited abnormally" if the crash
542# occured.
543#
danielk1977f8940ae2007-08-23 11:07:10 +0000544# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000545#
546proc crashsql {args} {
danielk1977aca790a2005-01-13 11:07:52 +0000547 if {$::tcl_platform(platform)!="unix"} {
548 error "crashsql should only be used on unix"
549 }
danielk197759a33f92007-03-17 10:26:59 +0000550
551 set blocksize ""
552 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000553 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000554 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000555 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000556 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000557 set sql [lindex $args end]
558
559 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
560 set z [lindex $args $ii]
561 set n [string length $z]
562 set z2 [lindex $args [expr $ii+1]]
563
564 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000565 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000566 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000567 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000568 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000569 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000570 else { error "Unrecognized option: $z" }
571 }
572
573 if {$crashfile eq ""} {
574 error "Compulsory option -file missing"
575 }
576
danielk1977aca790a2005-01-13 11:07:52 +0000577 set cfile [file join [pwd] $crashfile]
578
579 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000580 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000581 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +0000582 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000583 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000584
585 # This block sets the cache size of the main database to 10
586 # pages. This is done in case the build is configured to omit
587 # "PRAGMA cache_size".
588 puts $f {db eval {SELECT * FROM sqlite_master;}}
589 puts $f {set bt [btree_from_db db]}
590 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000591 if {$prngseed} {
592 set seed [expr {$prngseed%10007+1}]
593 # puts seed=$seed
594 puts $f "db eval {SELECT randomblob($seed)}"
595 }
danielk1977933bbd62007-03-17 07:22:42 +0000596
drhf8587402008-01-08 16:03:49 +0000597 if {[string length $tclbody]>0} {
598 puts $f $tclbody
599 }
600 if {[string length $sql]>0} {
601 puts $f "db eval {"
602 puts $f "$sql"
603 puts $f "}"
604 }
danielk1977aca790a2005-01-13 11:07:52 +0000605 close $f
606
607 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000608 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000609 } msg]
610 lappend r $msg
611}
612
danielk197732554c12005-01-22 03:39:39 +0000613# Usage: do_ioerr_test <test number> <options...>
614#
615# This proc is used to implement test cases that check that IO errors
616# are correctly handled. The first argument, <test number>, is an integer
617# used to name the tests executed by this proc. Options are as follows:
618#
619# -tclprep TCL script to run to prepare test.
620# -sqlprep SQL script to run to prepare test.
621# -tclbody TCL script to run with IO error simulation.
622# -sqlbody TCL script to run with IO error simulation.
623# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000624# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000625# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000626# -start Value of 'N' to begin with (default 1)
627#
628# -cksum Boolean. If true, test that the database does
629# not change during the execution of the test case.
630#
631proc do_ioerr_test {testname args} {
632
danielk197732554c12005-01-22 03:39:39 +0000633 set ::ioerropts(-start) 1
634 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000635 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000636 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000637 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +0000638 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +0000639 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +0000640 array set ::ioerropts $args
641
danielk197747cd39c2008-05-12 12:41:15 +0000642 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
643 # a couple of obscure IO errors that do not return them.
644 set ::ioerropts(-erc) 0
645
danielk197732554c12005-01-22 03:39:39 +0000646 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000647 #reset_prng_state
648 save_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +0000649 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000650 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000651 incr ::ioerropts(-count) -1
652 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000653
654 # Skip this IO error if it was specified with the "-exclude" option.
655 if {[info exists ::ioerropts(-exclude)]} {
656 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
657 }
danielk1977861f7452008-06-05 11:39:11 +0000658 if {$::ioerropts(-restoreprng)} {
659 restore_prng_state
660 }
danielk197732554c12005-01-22 03:39:39 +0000661
662 # Delete the files test.db and test2.db, then execute the TCL and
663 # SQL (in that order) to prepare for the test case.
664 do_test $testname.$n.1 {
665 set ::sqlite_io_error_pending 0
666 catch {db close}
aswiftaebf4132008-11-21 00:10:35 +0000667 catch {db2 close}
danielk197732554c12005-01-22 03:39:39 +0000668 catch {file delete -force test.db}
669 catch {file delete -force test.db-journal}
670 catch {file delete -force test2.db}
671 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000672 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000673 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000674 if {[info exists ::ioerropts(-tclprep)]} {
675 eval $::ioerropts(-tclprep)
676 }
677 if {[info exists ::ioerropts(-sqlprep)]} {
678 execsql $::ioerropts(-sqlprep)
679 }
680 expr 0
681 } {0}
682
683 # Read the 'checksum' of the database.
684 if {$::ioerropts(-cksum)} {
685 set checksum [cksum]
686 }
drh93aed5a2008-01-16 17:46:38 +0000687
danielk197732554c12005-01-22 03:39:39 +0000688 # Set the Nth IO error to fail.
689 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000690 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000691 set ::sqlite_io_error_pending $n
692 }] $n
693
694 # Create a single TCL script from the TCL and SQL specified
695 # as the body of the test.
696 set ::ioerrorbody {}
697 if {[info exists ::ioerropts(-tclbody)]} {
698 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
699 }
700 if {[info exists ::ioerropts(-sqlbody)]} {
701 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
702 }
703
704 # Execute the TCL Script created in the above block. If
705 # there are at least N IO operations performed by SQLite as
706 # a result of the script, the Nth will fail.
707 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +0000708 set ::sqlite_io_error_hit 0
709 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +0000710 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +0000711 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +0000712 set rc [sqlite3_errcode $::DB]
713 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000714 # If we are in extended result code mode, make sure all of the
715 # IOERRs we get back really do have their extended code values.
716 # If an extended result code is returned, the sqlite3_errcode
717 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
718 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000719 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
720 return $rc
721 }
722 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000723 # If we are not in extended result code mode, make sure no
724 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000725 if {[regexp {\+\d} $rc]} {
726 return $rc
727 }
728 }
drh93aed5a2008-01-16 17:46:38 +0000729 # The test repeats as long as $::go is non-zero. $::go starts out
730 # as 1. When a test runs to completion without hitting an I/O
731 # error, that means there is no point in continuing with this test
732 # case so set $::go to zero.
733 #
734 if {$::sqlite_io_error_pending>0} {
735 set ::go 0
736 set q 0
737 set ::sqlite_io_error_pending 0
738 } else {
739 set q 1
740 }
741
drhc9ac5ca2005-11-04 22:03:30 +0000742 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +0000743 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
744 set r 1
745 }
danielk1977f2fa8312006-01-24 13:09:33 +0000746 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000747
748 # One of two things must have happened. either
749 # 1. We never hit the IO error and the SQL returned OK
750 # 2. An IO error was hit and the SQL failed
751 #
drh93aed5a2008-01-16 17:46:38 +0000752 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +0000753 } {1}
754
danielk197780daec62008-05-12 10:57:02 +0000755 set ::sqlite_io_error_hit 0
756 set ::sqlite_io_error_pending 0
757
danielk197752b472a2008-05-05 16:23:55 +0000758 # Check that no page references were leaked. There should be
759 # a single reference if there is still an active transaction,
760 # or zero otherwise.
761 #
danielk197781620542008-06-07 05:19:37 +0000762 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
763 # locks are established on database files (i.e. if the error
764 # occurs while attempting to detect a hot-journal file), then
765 # there may 0 page references and an active transaction according
766 # to [sqlite3_get_autocommit].
767 #
danielk19774abd5442008-05-05 15:26:50 +0000768 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +0000769 do_test $testname.$n.4 {
770 set bt [btree_from_db db]
771 db_enter db
772 array set stats [btree_pager_stats $bt]
773 db_leave db
danielk197781620542008-06-07 05:19:37 +0000774 set nRef $stats(ref)
775 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
776 } {1}
danielk19774abd5442008-05-05 15:26:50 +0000777 }
778
danielk197752b472a2008-05-05 16:23:55 +0000779 # If there is an open database handle and no open transaction,
780 # and the pager is not running in exclusive-locking mode,
781 # check that the pager is in "unlocked" state. Theoretically,
782 # if a call to xUnlock() failed due to an IO error the underlying
783 # file may still be locked.
784 #
785 ifcapable pragma {
786 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +0000787 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +0000788 && [db one {pragma locking_mode}] eq "normal"
789 && [sqlite3_get_autocommit db]
790 } {
791 do_test $testname.$n.5 {
792 set bt [btree_from_db db]
793 db_enter db
794 array set stats [btree_pager_stats $bt]
795 db_leave db
796 set stats(state)
797 } 0
798 }
799 }
800
danielk197732554c12005-01-22 03:39:39 +0000801 # If an IO error occured, then the checksum of the database should
802 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +0000803 #
drh1aa5af12008-03-07 19:51:14 +0000804 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +0000805 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +0000806 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +0000807 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +0000808 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000809 cksum
810 } $checksum
811 }
812
drh1aa5af12008-03-07 19:51:14 +0000813 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +0000814 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000815 if {[info exists ::ioerropts(-cleanup)]} {
816 catch $::ioerropts(-cleanup)
817 }
danielk197732554c12005-01-22 03:39:39 +0000818 }
819 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000820 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000821 unset ::ioerropts
822}
823
drh93aed5a2008-01-16 17:46:38 +0000824# Return a checksum based on the contents of the main database associated
825# with connection $db
danielk197732554c12005-01-22 03:39:39 +0000826#
827proc cksum {{db db}} {
828 set txt [$db eval {
829 SELECT name, type, sql FROM sqlite_master order by name
830 }]\n
831 foreach tbl [$db eval {
832 SELECT name FROM sqlite_master WHERE type='table' order by name
833 }] {
834 append txt [$db eval "SELECT * FROM $tbl"]\n
835 }
836 foreach prag {default_synchronous default_cache_size} {
837 append txt $prag-[$db eval "PRAGMA $prag"]\n
838 }
839 set cksum [string length $txt]-[md5 $txt]
840 # puts $cksum-[file size test.db]
841 return $cksum
842}
843
drh93aed5a2008-01-16 17:46:38 +0000844# Generate a checksum based on the contents of the main and temp tables
845# database $db. If the checksum of two databases is the same, and the
846# integrity-check passes for both, the two databases are identical.
847#
drh1db639c2008-01-17 02:36:28 +0000848proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +0000849 set ret [list]
850 ifcapable tempdb {
851 set sql {
852 SELECT name FROM sqlite_master WHERE type = 'table' UNION
853 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
854 SELECT 'sqlite_master' UNION
855 SELECT 'sqlite_temp_master' ORDER BY 1
856 }
857 } else {
858 set sql {
859 SELECT name FROM sqlite_master WHERE type = 'table' UNION
860 SELECT 'sqlite_master' ORDER BY 1
861 }
862 }
863 set tbllist [$db eval $sql]
864 set txt {}
865 foreach tbl $tbllist {
866 append txt [$db eval "SELECT * FROM $tbl"]
867 }
868 foreach prag {default_cache_size} {
869 append txt $prag-[$db eval "PRAGMA $prag"]\n
870 }
871 # puts txt=$txt
872 return [md5 $txt]
873}
874
drhdc2c4912009-02-04 22:46:47 +0000875# Generate a checksum based on the contents of a single database with
876# a database connection. The name of the database is $dbname.
877# Examples of $dbname are "temp" or "main".
878#
879proc dbcksum {db dbname} {
880 if {$dbname=="temp"} {
881 set master sqlite_temp_master
882 } else {
883 set master $dbname.sqlite_master
884 }
885 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
886 set txt [$db eval "SELECT * FROM $master"]\n
887 foreach tab $alltab {
888 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
889 }
890 return [md5 $txt]
891}
892
danielk197735754ac2008-03-21 17:29:37 +0000893proc memdebug_log_sql {{filename mallocs.sql}} {
894
danielk19776f332c12008-03-21 14:22:44 +0000895 set data [sqlite3_memdebug_log dump]
896 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +0000897 if {$nFrame < 0} { return "" }
898
danielk197735754ac2008-03-21 17:29:37 +0000899 set database temp
900
danielk19776ab3a2e2009-02-19 14:39:25 +0000901 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +0000902
903 set sql ""
904 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +0000905 set nCall [lindex $e 0]
906 set nByte [lindex $e 1]
907 set lStack [lrange $e 2 end]
908 append sql "INSERT INTO ${database}.malloc VALUES"
909 append sql "('test', $nCall, $nByte, '$lStack');\n"
910 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +0000911 set frames($f) 1
912 }
913 }
914
915 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +0000916 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +0000917
918 foreach f [array names frames] {
919 set addr [format %x $f]
920 set cmd "addr2line -e [info nameofexec] $addr"
921 set line [eval exec $cmd]
922 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +0000923
924 set file [lindex [split $line :] 0]
925 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +0000926 }
927
danielk197735754ac2008-03-21 17:29:37 +0000928 foreach f [array names files] {
929 set contents ""
930 catch {
931 set fd [open $f]
932 set contents [read $fd]
933 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000934 }
danielk197735754ac2008-03-21 17:29:37 +0000935 set contents [string map {' ''} $contents]
936 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +0000937 }
danielk19776f332c12008-03-21 14:22:44 +0000938
danielk197735754ac2008-03-21 17:29:37 +0000939 set fd [open $filename w]
940 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
941 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000942}
drh93aed5a2008-01-16 17:46:38 +0000943
danielk197732554c12005-01-22 03:39:39 +0000944# Copy file $from into $to. This is used because some versions of
945# TCL for windows (notably the 8.4.1 binary package shipped with the
946# current mingw release) have a broken "file copy" command.
947#
948proc copy_file {from to} {
949 if {$::tcl_platform(platform)=="unix"} {
950 file copy -force $from $to
951 } else {
952 set f [open $from]
953 fconfigure $f -translation binary
954 set t [open $to w]
955 fconfigure $t -translation binary
956 puts -nonewline $t [read $f [file size $from]]
957 close $t
958 close $f
959 }
960}
961
danf5894502009-10-07 18:41:19 +0000962# Drop all tables in database [db]
963proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +0000964 ifcapable trigger&&foreignkey {
965 set pk [$db one "PRAGMA foreign_keys"]
966 $db eval "PRAGMA foreign_keys = OFF"
967 }
drh9a6ffc82010-02-15 18:03:20 +0000968 foreach {idx name file} [db eval {PRAGMA database_list}] {
969 if {$idx==1} {
970 set master sqlite_temp_master
971 } else {
972 set master $name.sqlite_master
973 }
974 foreach {t type} [$db eval "
975 SELECT name, type FROM $master
976 WHERE type IN('table', 'view') AND name NOT like 'sqlite_%'
977 "] {
978 $db eval "DROP $type $t"
979 }
danf5894502009-10-07 18:41:19 +0000980 }
shaneh4e7b32f2009-12-17 22:12:51 +0000981 ifcapable trigger&&foreignkey {
982 $db eval "PRAGMA foreign_keys = $pk"
983 }
danf5894502009-10-07 18:41:19 +0000984}
985
986
danielk197745901d62004-11-10 15:27:38 +0000987# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
988# to non-zero, then set the global variable $AUTOVACUUM to 1.
989set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +0000990
991source $testdir/thread_common.tcl