blob: 87fe3bc7b94a80160856d4ce00537367f35fc35d [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
danc1a60c52010-06-07 14:28:16 +000016#-------------------------------------------------------------------------
17# The commands provided by the code in this file to help with creating
18# test cases are as follows:
drh8359d8c2008-03-29 11:00:54 +000019#
danc1a60c52010-06-07 14:28:16 +000020# Commands to manipulate the db and the file-system at a high level:
drh8359d8c2008-03-29 11:00:54 +000021#
danc1a60c52010-06-07 14:28:16 +000022# copy_file FROM TO
23# drop_all_table ?DB?
24# forcedelete FILENAME
25#
26# Test the capability of the SQLite version built into the interpreter to
27# determine if a specific test can be run:
28#
29# ifcapable EXPR
30#
31# Calulate checksums based on database contents:
32#
33# dbcksum DB DBNAME
34# allcksum ?DB?
35# cksum ?DB?
36#
37# Commands to execute/explain SQL statements:
38#
39# stepsql DB SQL
40# execsql2 SQL
41# explain_no_trace SQL
42# explain SQL ?DB?
43# catchsql SQL ?DB?
44# execsql SQL ?DB?
45#
46# Commands to run test cases:
47#
48# do_ioerr_test TESTNAME ARGS...
49# crashsql ARGS...
50# integrity_check TESTNAME ?DB?
51# do_test TESTNAME SCRIPT EXPECTED
52#
53# Commands providing a lower level interface to the test counters:
54#
55# set_test_counter COUNTER ?VALUE?
56# omit_test TESTNAME REASON
57# fail_test TESTNAME
58# incr_ntest
59#
60# Command run at the end of each test file:
61#
62# finish_test
63#
64# Commands to help create test files that run with the "WAL" permutation:
65#
66# wal_is_wal_mode
67# wal_set_journal_mode ?DB?
68# wal_check_journal_mode TESTNAME?DB?
69#
drh348784e2000-05-29 20:41:49 +000070
danc1a60c52010-06-07 14:28:16 +000071# Set the precision of FP arithmatic used by the interpreter. And
72# configure SQLite to take database file locks on the page that begins
73# 64KB into the database file instead of the one 1GB in. This means
74# the code that handles that special case can be tested without creating
75# very large database files.
76#
drh92febd92004-08-20 18:34:20 +000077set tcl_precision 15
drhc7a3bb92009-02-05 16:31:45 +000078sqlite3_test_control_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000079
danc1a60c52010-06-07 14:28:16 +000080
81# If the pager codec is available, create a wrapper for the [sqlite3]
82# command that appends "-key {xyzzy}" to the command line. i.e. this:
drh3a7fb7c2007-08-10 16:41:08 +000083#
danc1a60c52010-06-07 14:28:16 +000084# sqlite3 db test.db
drh4a50aac2007-08-23 02:47:53 +000085#
danc1a60c52010-06-07 14:28:16 +000086# becomes
drh4a50aac2007-08-23 02:47:53 +000087#
danc1a60c52010-06-07 14:28:16 +000088# sqlite3 db test.db -key {xyzzy}
drh9eb9e262004-02-11 02:18:05 +000089#
drhef4ac8f2004-06-19 00:16:31 +000090if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} {
91 rename sqlite3 sqlite_orig
92 proc sqlite3 {args} {
drh9eb9e262004-02-11 02:18:05 +000093 if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} {
94 lappend args -key {xyzzy}
95 }
96 uplevel 1 sqlite_orig $args
97 }
98}
99
danc1a60c52010-06-07 14:28:16 +0000100# The following block only runs the first time this file is sourced.
drhbec2bf42000-05-29 23:48:22 +0000101#
danc1a60c52010-06-07 14:28:16 +0000102if {[info exists cmdlinearg]==0} {
103
104 # Parse any options specified in the $argv array. This script accepts the
105 # following options:
106 #
107 # --pause
108 # --soft-heap-limit=NN
109 # --maxerror=NN
110 # --malloctrace=N
111 # --backtrace=N
112 # --binarylog=N
113 #
114 set cmdlinearg(soft-heap-limit) 0
115 set cmdlinearg(maxerror) 1000
116 set cmdlinearg(malloctrace) 0
117 set cmdlinearg(backtrace) 10
118 set cmdlinearg(binarylog) 0
119
120 set leftover [list]
121 foreach a $argv {
122 switch -regexp -- $a {
123 {^-+pause$} {
124 # Wait for user input before continuing. This is to give the user an
125 # opportunity to connect profiling tools to the process.
126 puts -nonewline "Press RETURN to begin..."
127 flush stdout
128 gets stdin
129 }
130 {^-+soft-heap-limit=.+$} {
131 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
132 }
133 {^-+maxerror=.+$} {
134 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
135 }
136 {^-+malloctrace=.+$} {
137 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
138 if {$cmdlinearg(malloctrace)} {
139 sqlite3_memdebug_log start
140 }
141 }
142 {^-+backtrace=.+$} {
143 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
144 }
145 sqlite3_memdebug_backtrace $value
146 {^-+binarylog=.+$} {
147 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
148 }
149 default {
150 lappend leftover $a
151 }
152 }
153 }
154 set argv $leftover
155
danielk1977f7b9d662008-06-23 18:49:43 +0000156 sqlite3_shutdown
157 install_malloc_faultsim 1
158 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000159 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000160
161 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000162 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000163 }
164
165 # Set the backtrace depth, if malloc tracing is enabled.
166 #
167 if {$cmdlinearg(malloctrace)} {
168 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000169 }
danielk1977f7b9d662008-06-23 18:49:43 +0000170}
danielk197703ba3fa2009-01-09 10:49:14 +0000171
danc1a60c52010-06-07 14:28:16 +0000172# Update the soft-heap-limit each time this script is run. In that
173# way if an individual test file changes the soft-heap-limit, it
174# will be reset at the start of the next test file.
175#
176sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
177
178# Create a test database
179#
danielk197703ba3fa2009-01-09 10:49:14 +0000180proc reset_db {} {
181 catch {db close}
182 file delete -force test.db
183 file delete -force test.db-journal
dand3f8f942010-04-13 11:35:01 +0000184 file delete -force test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000185 sqlite3 db ./test.db
186 set ::DB [sqlite3_connection_pointer db]
187 if {[info exists ::SETUP_SQL]} {
188 db eval $::SETUP_SQL
189 }
drhcd61c282002-03-06 22:01:34 +0000190}
danielk197703ba3fa2009-01-09 10:49:14 +0000191reset_db
drhbec2bf42000-05-29 23:48:22 +0000192
193# Abort early if this script has been run before.
194#
danc1a60c52010-06-07 14:28:16 +0000195if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000196
danc1a60c52010-06-07 14:28:16 +0000197# Initialize the test counters and set up commands to access them.
198# Or, if this is a slave interpreter, set up aliases to write the
199# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000200#
danc1a60c52010-06-07 14:28:16 +0000201if {0==[info exists ::SLAVE]} {
202 set TC(errors) 0
203 set TC(count) 0
204 set TC(fail_list) [list]
205 set TC(omit_list) [list]
206
207 proc set_test_counter {counter args} {
208 if {[llength $args]} {
209 set ::TC($counter) [lindex $args 0]
210 }
211 set ::TC($counter)
212 }
drhb62c3352006-11-23 09:39:16 +0000213}
drh348784e2000-05-29 20:41:49 +0000214
drh521cc842008-04-15 02:36:33 +0000215# Record the fact that a sequence of tests were omitted.
216#
217proc omit_test {name reason} {
danc1a60c52010-06-07 14:28:16 +0000218 set omitList [set_test_counter omit_list]
drh521cc842008-04-15 02:36:33 +0000219 lappend omitList [list $name $reason]
danc1a60c52010-06-07 14:28:16 +0000220 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000221}
222
danc1a60c52010-06-07 14:28:16 +0000223# Record the fact that a test failed.
224#
225proc fail_test {name} {
226 set f [set_test_counter fail_list]
227 lappend f $name
228 set_test_counter fail_list $f
229 set_test_counter errors [expr [set_test_counter errors] + 1]
230
231 set nFail [set_test_counter errors]
232 if {$nFail>=$::cmdlinearg(maxerror)} {
233 puts "*** Giving up..."
234 finalize_testing
235 }
236}
237
238# Increment the number of tests run
239#
240proc incr_ntest {} {
241 set_test_counter count [expr [set_test_counter count] + 1]
242}
243
244
drh348784e2000-05-29 20:41:49 +0000245# Invoke the do_test procedure to run a single test
246#
247proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000248
249 global argv cmdlinearg
250
drh4a50aac2007-08-23 02:47:53 +0000251 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000252
drh767c2002000-10-19 14:10:08 +0000253 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +0000254 set go 1
255 } else {
256 set go 0
257 foreach pattern $argv {
258 if {[string match $pattern $name]} {
259 set go 1
260 break
261 }
262 }
263 }
264 if {!$go} return
danc1a60c52010-06-07 14:28:16 +0000265 incr_ntest
drh5edc3122001-09-13 21:53:09 +0000266 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000267 flush stdout
268 if {[catch {uplevel #0 "$cmd;\n"} result]} {
269 puts "\nError: $result"
danc1a60c52010-06-07 14:28:16 +0000270 fail_test $name
drh348784e2000-05-29 20:41:49 +0000271 } elseif {[string compare $result $expected]} {
272 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
danc1a60c52010-06-07 14:28:16 +0000273 fail_test $name
drh348784e2000-05-29 20:41:49 +0000274 } else {
275 puts " Ok"
276 }
drh6558db82007-04-13 03:23:21 +0000277 flush stdout
drh348784e2000-05-29 20:41:49 +0000278}
279
drhb62c3352006-11-23 09:39:16 +0000280# Run an SQL script.
281# Return the number of microseconds per statement.
282#
drh3590f152006-11-23 21:09:10 +0000283proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000284 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000285 flush stdout
286 set speed [time {sqlite3_exec_nr db $sql}]
287 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000288 if {$tm == 0} {
289 set rate [format %20s "many"]
290 } else {
291 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
292 }
drh3590f152006-11-23 21:09:10 +0000293 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000294 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000295 global total_time
296 set total_time [expr {$total_time+$tm}]
297}
drh45c236d2008-03-22 01:08:00 +0000298proc speed_trial_tcl {name numstmt units script} {
299 puts -nonewline [format {%-21.21s } $name...]
300 flush stdout
301 set speed [time {eval $script}]
302 set tm [lindex $speed 0]
303 if {$tm == 0} {
304 set rate [format %20s "many"]
305 } else {
306 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
307 }
308 set u2 $units/s
309 puts [format {%12d uS %s %s} $tm $rate $u2]
310 global total_time
311 set total_time [expr {$total_time+$tm}]
312}
drhdd924312007-03-31 22:34:16 +0000313proc speed_trial_init {name} {
314 global total_time
315 set total_time 0
316}
317proc speed_trial_summary {name} {
318 global total_time
319 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000320}
321
drh348784e2000-05-29 20:41:49 +0000322# Run this routine last
323#
324proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000325 catch {db close}
326 catch {db2 close}
327 catch {db3 close}
328 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +0000329}
330proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +0000331 global sqlite_open_file_count
332
333 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +0000334
drh6e142f52000-06-08 13:36:40 +0000335 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000336 catch {db2 close}
337 catch {db3 close}
338
drh9bc54492007-10-23 14:49:59 +0000339 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000340 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000341 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000342 db close
drh984bfaa2008-03-19 16:08:53 +0000343 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +0000344
drh3a7fb7c2007-08-10 16:41:08 +0000345 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +0000346 set nTest [incr_ntest]
347 set nErr [set_test_counter errors]
348
drh348784e2000-05-29 20:41:49 +0000349 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000350 if {$nErr>0} {
danc1a60c52010-06-07 14:28:16 +0000351 puts "Failures on these tests: [set_test_counter fail_list]"
drhf3a65f72007-08-22 20:18:21 +0000352 }
danielk1977ee8b7992009-03-26 17:13:06 +0000353 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000354 if {[llength $omitList]>0} {
355 puts "Omitted test cases:"
356 set prec {}
357 foreach {rec} [lsort $omitList] {
358 if {$rec==$prec} continue
359 set prec $rec
360 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
361 }
362 }
drh80788d82006-09-02 14:50:23 +0000363 if {$nErr>0 && ![working_64bit_int]} {
364 puts "******************************************************************"
365 puts "N.B.: The version of TCL that you used to build this test harness"
366 puts "is defective in that it does not support 64-bit integers. Some or"
367 puts "all of the test failures above might be a result from this defect"
368 puts "in your TCL build."
369 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000370 }
danc1a60c52010-06-07 14:28:16 +0000371 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000372 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000373 }
drh94e92032003-02-16 22:21:32 +0000374 if {$sqlite_open_file_count} {
375 puts "$sqlite_open_file_count files were left open"
376 incr nErr
377 }
drhf3a65f72007-08-22 20:18:21 +0000378 if {[sqlite3_memory_used]>0} {
379 puts "Unfreed memory: [sqlite3_memory_used] bytes"
380 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000381 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000382 puts "Writing unfreed memory log to \"./memleak.txt\""
383 sqlite3_memdebug_dump ./memleak.txt
384 }
drhf3a65f72007-08-22 20:18:21 +0000385 } else {
386 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000387 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000388 sqlite3_memdebug_dump ./memusage.txt
389 }
drhf3a65f72007-08-22 20:18:21 +0000390 }
drhf7141992008-06-19 00:16:08 +0000391 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000392 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
393 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000394 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
395 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
396 }
danc1a60c52010-06-07 14:28:16 +0000397 if {$::cmdlinearg(malloctrace)} {
danielk197735754ac2008-03-21 17:29:37 +0000398 puts "Writing mallocs.sql..."
399 memdebug_log_sql
400 sqlite3_memdebug_log stop
401 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000402
403 if {[sqlite3_memory_used]>0} {
404 puts "Writing leaks.sql..."
405 sqlite3_memdebug_log sync
406 memdebug_log_sql leaks.sql
407 }
danielk197735754ac2008-03-21 17:29:37 +0000408 }
drhd9910fe2006-10-04 11:55:49 +0000409 foreach f [glob -nocomplain test.db-*-journal] {
410 file delete -force $f
411 }
412 foreach f [glob -nocomplain test.db-mj*] {
413 file delete -force $f
414 }
drhdb25e382001-03-15 18:21:22 +0000415 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000416}
417
drhf7141992008-06-19 00:16:08 +0000418# Display memory statistics for analysis and debugging purposes.
419#
420proc show_memstats {} {
421 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000422 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
423 set val [format {now %10d max %10d max-size %10d} \
424 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000425 puts "Memory used: $val"
426 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000427 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
428 set val [format {now %10d max %10d max-size %10d} \
429 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000430 puts "Page-cache used: $val"
431 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
432 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
433 puts "Page-cache overflow: $val"
434 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
435 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
436 puts "Scratch memory used: $val"
437 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +0000438 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
439 set val [format {now %10d max %10d max-size %10d} \
440 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000441 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +0000442 ifcapable yytrackmaxstackdepth {
443 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
444 set val [format { max %10d} [lindex $x 2]]
445 puts "Parser stack depth: $val"
446 }
drhf7141992008-06-19 00:16:08 +0000447}
448
drh348784e2000-05-29 20:41:49 +0000449# A procedure to execute SQL
450#
drhc4a3c772001-04-04 11:48:57 +0000451proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000452 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000453 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000454}
drh3aadb2e2000-05-31 17:59:25 +0000455
drhadbca9c2001-09-27 15:11:53 +0000456# Execute SQL and catch exceptions.
457#
458proc catchsql {sql {db db}} {
459 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +0000460 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +0000461 lappend r $msg
462 return $r
463}
464
drh04096482001-11-09 22:41:44 +0000465# Do an VDBE code dump on the SQL given
466#
467proc explain {sql {db db}} {
468 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000469 puts "addr opcode p1 p2 p3 p4 p5 #"
470 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000471 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000472 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
473 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
474 ]
drh04096482001-11-09 22:41:44 +0000475 }
476}
477
drhdafc0ce2008-04-17 19:14:02 +0000478# Show the VDBE program for an SQL statement but omit the Trace
479# opcode at the beginning. This procedure can be used to prove
480# that different SQL statements generate exactly the same VDBE code.
481#
482proc explain_no_trace {sql} {
483 set tr [db eval "EXPLAIN $sql"]
484 return [lrange $tr 7 end]
485}
486
drh3aadb2e2000-05-31 17:59:25 +0000487# Another procedure to execute SQL. This one includes the field
488# names in the returned list.
489#
490proc execsql2 {sql} {
491 set result {}
492 db eval $sql data {
493 foreach f $data(*) {
494 lappend result $f $data($f)
495 }
496 }
497 return $result
498}
drh17a68932001-01-31 13:28:08 +0000499
drhdde85d92003-03-01 19:45:34 +0000500# Use the non-callback API to execute multiple SQL statements
501#
502proc stepsql {dbptr sql} {
503 set sql [string trim $sql]
504 set r 0
505 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000506 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000507 return [list 1 $vm]
508 }
509 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000510# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
511# foreach v $VAL {lappend r $v}
512# }
513 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
514 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
515 lappend r [sqlite3_column_text $vm $i]
516 }
drhdde85d92003-03-01 19:45:34 +0000517 }
danielk1977106bb232004-05-21 10:08:53 +0000518 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000519 return [list 1 $errmsg]
520 }
521 }
522 return $r
523}
524
drh17a68932001-01-31 13:28:08 +0000525# Delete a file or directory
526#
527proc forcedelete {filename} {
528 if {[catch {file delete -force $filename}]} {
529 exec rm -rf $filename
530 }
531}
drh21504322002-06-25 13:16:02 +0000532
533# Do an integrity check of the entire database
534#
danielk197704103022009-02-03 16:51:24 +0000535proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +0000536 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +0000537 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +0000538 }
539}
540
danielk1977db4e8862008-01-16 08:24:46 +0000541proc fix_ifcapable_expr {expr} {
542 set ret ""
543 set state 0
544 for {set i 0} {$i < [string length $expr]} {incr i} {
545 set char [string range $expr $i $i]
546 set newstate [expr {[string is alnum $char] || $char eq "_"}]
547 if {$newstate && !$state} {
548 append ret {$::sqlite_options(}
549 }
550 if {!$newstate && $state} {
551 append ret )
552 }
553 append ret $char
554 set state $newstate
555 }
556 if {$state} {append ret )}
557 return $ret
558}
559
drh27d258a2004-10-30 20:23:09 +0000560# Evaluate a boolean expression of capabilities. If true, execute the
561# code. Omit the code if false.
562#
danielk19773e8c37e2005-01-21 03:12:14 +0000563proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000564 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
565 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000566 if ($e2) {
567 set c [catch {uplevel 1 $code} r]
568 } else {
569 set c [catch {uplevel 1 $elsecode} r]
570 }
571 return -code $c $r
drh21504322002-06-25 13:16:02 +0000572}
danielk197745901d62004-11-10 15:27:38 +0000573
danielk1977aca790a2005-01-13 11:07:52 +0000574# This proc execs a seperate process that crashes midway through executing
575# the SQL script $sql on database test.db.
576#
577# The crash occurs during a sync() of file $crashfile. When the crash
578# occurs a random subset of all unsynced writes made by the process are
579# written into the files on disk. Argument $crashdelay indicates the
580# number of file syncs to wait before crashing.
581#
582# The return value is a list of two elements. The first element is a
583# boolean, indicating whether or not the process actually crashed or
584# reported some other error. The second element in the returned list is the
585# error message. This is "child process exited abnormally" if the crash
586# occured.
587#
danielk1977f8940ae2007-08-23 11:07:10 +0000588# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000589#
590proc crashsql {args} {
danielk1977aca790a2005-01-13 11:07:52 +0000591 if {$::tcl_platform(platform)!="unix"} {
592 error "crashsql should only be used on unix"
593 }
danielk197759a33f92007-03-17 10:26:59 +0000594
595 set blocksize ""
596 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000597 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000598 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000599 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000600 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000601 set sql [lindex $args end]
602
603 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
604 set z [lindex $args $ii]
605 set n [string length $z]
606 set z2 [lindex $args [expr $ii+1]]
607
608 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000609 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000610 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000611 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000612 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000613 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000614 else { error "Unrecognized option: $z" }
615 }
616
617 if {$crashfile eq ""} {
618 error "Compulsory option -file missing"
619 }
620
danielk1977aca790a2005-01-13 11:07:52 +0000621 set cfile [file join [pwd] $crashfile]
622
623 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000624 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000625 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +0000626 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000627 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000628
629 # This block sets the cache size of the main database to 10
630 # pages. This is done in case the build is configured to omit
631 # "PRAGMA cache_size".
632 puts $f {db eval {SELECT * FROM sqlite_master;}}
633 puts $f {set bt [btree_from_db db]}
634 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000635 if {$prngseed} {
636 set seed [expr {$prngseed%10007+1}]
637 # puts seed=$seed
638 puts $f "db eval {SELECT randomblob($seed)}"
639 }
danielk1977933bbd62007-03-17 07:22:42 +0000640
drhf8587402008-01-08 16:03:49 +0000641 if {[string length $tclbody]>0} {
642 puts $f $tclbody
643 }
644 if {[string length $sql]>0} {
645 puts $f "db eval {"
646 puts $f "$sql"
647 puts $f "}"
648 }
danielk1977aca790a2005-01-13 11:07:52 +0000649 close $f
650
651 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000652 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000653 } msg]
654 lappend r $msg
655}
656
danielk197732554c12005-01-22 03:39:39 +0000657# Usage: do_ioerr_test <test number> <options...>
658#
659# This proc is used to implement test cases that check that IO errors
660# are correctly handled. The first argument, <test number>, is an integer
661# used to name the tests executed by this proc. Options are as follows:
662#
663# -tclprep TCL script to run to prepare test.
664# -sqlprep SQL script to run to prepare test.
665# -tclbody TCL script to run with IO error simulation.
666# -sqlbody TCL script to run with IO error simulation.
667# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000668# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000669# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000670# -start Value of 'N' to begin with (default 1)
671#
672# -cksum Boolean. If true, test that the database does
673# not change during the execution of the test case.
674#
675proc do_ioerr_test {testname args} {
676
danielk197732554c12005-01-22 03:39:39 +0000677 set ::ioerropts(-start) 1
678 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000679 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000680 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000681 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +0000682 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +0000683 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +0000684 array set ::ioerropts $args
685
danielk197747cd39c2008-05-12 12:41:15 +0000686 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
687 # a couple of obscure IO errors that do not return them.
688 set ::ioerropts(-erc) 0
689
danielk197732554c12005-01-22 03:39:39 +0000690 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000691 #reset_prng_state
692 save_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +0000693 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000694 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000695 incr ::ioerropts(-count) -1
696 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000697
698 # Skip this IO error if it was specified with the "-exclude" option.
699 if {[info exists ::ioerropts(-exclude)]} {
700 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
701 }
danielk1977861f7452008-06-05 11:39:11 +0000702 if {$::ioerropts(-restoreprng)} {
703 restore_prng_state
704 }
danielk197732554c12005-01-22 03:39:39 +0000705
706 # Delete the files test.db and test2.db, then execute the TCL and
707 # SQL (in that order) to prepare for the test case.
708 do_test $testname.$n.1 {
709 set ::sqlite_io_error_pending 0
710 catch {db close}
aswiftaebf4132008-11-21 00:10:35 +0000711 catch {db2 close}
danielk197732554c12005-01-22 03:39:39 +0000712 catch {file delete -force test.db}
713 catch {file delete -force test.db-journal}
714 catch {file delete -force test2.db}
715 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000716 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000717 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000718 if {[info exists ::ioerropts(-tclprep)]} {
719 eval $::ioerropts(-tclprep)
720 }
721 if {[info exists ::ioerropts(-sqlprep)]} {
722 execsql $::ioerropts(-sqlprep)
723 }
724 expr 0
725 } {0}
726
727 # Read the 'checksum' of the database.
728 if {$::ioerropts(-cksum)} {
729 set checksum [cksum]
730 }
drh93aed5a2008-01-16 17:46:38 +0000731
danielk197732554c12005-01-22 03:39:39 +0000732 # Set the Nth IO error to fail.
733 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000734 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000735 set ::sqlite_io_error_pending $n
736 }] $n
737
738 # Create a single TCL script from the TCL and SQL specified
739 # as the body of the test.
740 set ::ioerrorbody {}
741 if {[info exists ::ioerropts(-tclbody)]} {
742 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
743 }
744 if {[info exists ::ioerropts(-sqlbody)]} {
745 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
746 }
747
748 # Execute the TCL Script created in the above block. If
749 # there are at least N IO operations performed by SQLite as
750 # a result of the script, the Nth will fail.
751 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +0000752 set ::sqlite_io_error_hit 0
753 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +0000754 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +0000755 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +0000756 set rc [sqlite3_errcode $::DB]
757 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000758 # If we are in extended result code mode, make sure all of the
759 # IOERRs we get back really do have their extended code values.
760 # If an extended result code is returned, the sqlite3_errcode
761 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
762 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000763 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
764 return $rc
765 }
766 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000767 # If we are not in extended result code mode, make sure no
768 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000769 if {[regexp {\+\d} $rc]} {
770 return $rc
771 }
772 }
drh93aed5a2008-01-16 17:46:38 +0000773 # The test repeats as long as $::go is non-zero. $::go starts out
774 # as 1. When a test runs to completion without hitting an I/O
775 # error, that means there is no point in continuing with this test
776 # case so set $::go to zero.
777 #
778 if {$::sqlite_io_error_pending>0} {
779 set ::go 0
780 set q 0
781 set ::sqlite_io_error_pending 0
782 } else {
783 set q 1
784 }
785
drhc9ac5ca2005-11-04 22:03:30 +0000786 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +0000787 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
788 set r 1
789 }
danielk1977f2fa8312006-01-24 13:09:33 +0000790 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000791
792 # One of two things must have happened. either
793 # 1. We never hit the IO error and the SQL returned OK
794 # 2. An IO error was hit and the SQL failed
795 #
dand41a29a2010-05-06 15:56:28 +0000796 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +0000797 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +0000798 } {1}
799
danielk197780daec62008-05-12 10:57:02 +0000800 set ::sqlite_io_error_hit 0
801 set ::sqlite_io_error_pending 0
802
danielk197752b472a2008-05-05 16:23:55 +0000803 # Check that no page references were leaked. There should be
804 # a single reference if there is still an active transaction,
805 # or zero otherwise.
806 #
danielk197781620542008-06-07 05:19:37 +0000807 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
808 # locks are established on database files (i.e. if the error
809 # occurs while attempting to detect a hot-journal file), then
810 # there may 0 page references and an active transaction according
811 # to [sqlite3_get_autocommit].
812 #
danielk19774abd5442008-05-05 15:26:50 +0000813 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +0000814 do_test $testname.$n.4 {
815 set bt [btree_from_db db]
816 db_enter db
817 array set stats [btree_pager_stats $bt]
818 db_leave db
danielk197781620542008-06-07 05:19:37 +0000819 set nRef $stats(ref)
820 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
821 } {1}
danielk19774abd5442008-05-05 15:26:50 +0000822 }
823
danielk197752b472a2008-05-05 16:23:55 +0000824 # If there is an open database handle and no open transaction,
825 # and the pager is not running in exclusive-locking mode,
826 # check that the pager is in "unlocked" state. Theoretically,
827 # if a call to xUnlock() failed due to an IO error the underlying
828 # file may still be locked.
829 #
830 ifcapable pragma {
831 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +0000832 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +0000833 && [db one {pragma locking_mode}] eq "normal"
834 && [sqlite3_get_autocommit db]
835 } {
836 do_test $testname.$n.5 {
837 set bt [btree_from_db db]
838 db_enter db
839 array set stats [btree_pager_stats $bt]
840 db_leave db
841 set stats(state)
842 } 0
843 }
844 }
845
danielk197732554c12005-01-22 03:39:39 +0000846 # If an IO error occured, then the checksum of the database should
847 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +0000848 #
drh1aa5af12008-03-07 19:51:14 +0000849 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +0000850 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +0000851 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +0000852 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +0000853 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000854 cksum
855 } $checksum
856 }
857
drh1aa5af12008-03-07 19:51:14 +0000858 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +0000859 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000860 if {[info exists ::ioerropts(-cleanup)]} {
861 catch $::ioerropts(-cleanup)
862 }
danielk197732554c12005-01-22 03:39:39 +0000863 }
864 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000865 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000866 unset ::ioerropts
867}
868
drh93aed5a2008-01-16 17:46:38 +0000869# Return a checksum based on the contents of the main database associated
870# with connection $db
danielk197732554c12005-01-22 03:39:39 +0000871#
872proc cksum {{db db}} {
873 set txt [$db eval {
874 SELECT name, type, sql FROM sqlite_master order by name
875 }]\n
876 foreach tbl [$db eval {
877 SELECT name FROM sqlite_master WHERE type='table' order by name
878 }] {
879 append txt [$db eval "SELECT * FROM $tbl"]\n
880 }
881 foreach prag {default_synchronous default_cache_size} {
882 append txt $prag-[$db eval "PRAGMA $prag"]\n
883 }
884 set cksum [string length $txt]-[md5 $txt]
885 # puts $cksum-[file size test.db]
886 return $cksum
887}
888
drh93aed5a2008-01-16 17:46:38 +0000889# Generate a checksum based on the contents of the main and temp tables
890# database $db. If the checksum of two databases is the same, and the
891# integrity-check passes for both, the two databases are identical.
892#
drh1db639c2008-01-17 02:36:28 +0000893proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +0000894 set ret [list]
895 ifcapable tempdb {
896 set sql {
897 SELECT name FROM sqlite_master WHERE type = 'table' UNION
898 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
899 SELECT 'sqlite_master' UNION
900 SELECT 'sqlite_temp_master' ORDER BY 1
901 }
902 } else {
903 set sql {
904 SELECT name FROM sqlite_master WHERE type = 'table' UNION
905 SELECT 'sqlite_master' ORDER BY 1
906 }
907 }
908 set tbllist [$db eval $sql]
909 set txt {}
910 foreach tbl $tbllist {
911 append txt [$db eval "SELECT * FROM $tbl"]
912 }
913 foreach prag {default_cache_size} {
914 append txt $prag-[$db eval "PRAGMA $prag"]\n
915 }
916 # puts txt=$txt
917 return [md5 $txt]
918}
919
drhdc2c4912009-02-04 22:46:47 +0000920# Generate a checksum based on the contents of a single database with
921# a database connection. The name of the database is $dbname.
922# Examples of $dbname are "temp" or "main".
923#
924proc dbcksum {db dbname} {
925 if {$dbname=="temp"} {
926 set master sqlite_temp_master
927 } else {
928 set master $dbname.sqlite_master
929 }
930 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
931 set txt [$db eval "SELECT * FROM $master"]\n
932 foreach tab $alltab {
933 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
934 }
935 return [md5 $txt]
936}
937
danielk197735754ac2008-03-21 17:29:37 +0000938proc memdebug_log_sql {{filename mallocs.sql}} {
939
danielk19776f332c12008-03-21 14:22:44 +0000940 set data [sqlite3_memdebug_log dump]
941 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +0000942 if {$nFrame < 0} { return "" }
943
danielk197735754ac2008-03-21 17:29:37 +0000944 set database temp
945
danielk19776ab3a2e2009-02-19 14:39:25 +0000946 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +0000947
948 set sql ""
949 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +0000950 set nCall [lindex $e 0]
951 set nByte [lindex $e 1]
952 set lStack [lrange $e 2 end]
953 append sql "INSERT INTO ${database}.malloc VALUES"
954 append sql "('test', $nCall, $nByte, '$lStack');\n"
955 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +0000956 set frames($f) 1
957 }
958 }
959
960 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +0000961 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +0000962
963 foreach f [array names frames] {
964 set addr [format %x $f]
965 set cmd "addr2line -e [info nameofexec] $addr"
966 set line [eval exec $cmd]
967 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +0000968
969 set file [lindex [split $line :] 0]
970 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +0000971 }
972
danielk197735754ac2008-03-21 17:29:37 +0000973 foreach f [array names files] {
974 set contents ""
975 catch {
976 set fd [open $f]
977 set contents [read $fd]
978 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000979 }
danielk197735754ac2008-03-21 17:29:37 +0000980 set contents [string map {' ''} $contents]
981 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +0000982 }
danielk19776f332c12008-03-21 14:22:44 +0000983
danielk197735754ac2008-03-21 17:29:37 +0000984 set fd [open $filename w]
985 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
986 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000987}
drh93aed5a2008-01-16 17:46:38 +0000988
danielk197732554c12005-01-22 03:39:39 +0000989# Copy file $from into $to. This is used because some versions of
990# TCL for windows (notably the 8.4.1 binary package shipped with the
991# current mingw release) have a broken "file copy" command.
992#
993proc copy_file {from to} {
994 if {$::tcl_platform(platform)=="unix"} {
995 file copy -force $from $to
996 } else {
997 set f [open $from]
998 fconfigure $f -translation binary
999 set t [open $to w]
1000 fconfigure $t -translation binary
1001 puts -nonewline $t [read $f [file size $from]]
1002 close $t
1003 close $f
1004 }
1005}
1006
danf5894502009-10-07 18:41:19 +00001007# Drop all tables in database [db]
1008proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001009 ifcapable trigger&&foreignkey {
1010 set pk [$db one "PRAGMA foreign_keys"]
1011 $db eval "PRAGMA foreign_keys = OFF"
1012 }
drh9a6ffc82010-02-15 18:03:20 +00001013 foreach {idx name file} [db eval {PRAGMA database_list}] {
1014 if {$idx==1} {
1015 set master sqlite_temp_master
1016 } else {
1017 set master $name.sqlite_master
1018 }
1019 foreach {t type} [$db eval "
1020 SELECT name, type FROM $master
1021 WHERE type IN('table', 'view') AND name NOT like 'sqlite_%'
1022 "] {
1023 $db eval "DROP $type $t"
1024 }
danf5894502009-10-07 18:41:19 +00001025 }
shaneh4e7b32f2009-12-17 22:12:51 +00001026 ifcapable trigger&&foreignkey {
1027 $db eval "PRAGMA foreign_keys = $pk"
1028 }
danf5894502009-10-07 18:41:19 +00001029}
1030
dan71cb5182010-04-26 12:39:03 +00001031#-------------------------------------------------------------------------
1032# If a test script is executed with global variable
1033# $::permutations_test_prefix set to "wal", then the tests are run
1034# in WAL mode. Otherwise, they should be run in rollback mode. The
1035# following Tcl procs are used to make this less intrusive:
1036#
1037# wal_set_journal_mode ?DB?
1038#
1039# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1040# connection handle DB. Otherwise, this command is a no-op.
1041#
1042# wal_check_journal_mode TESTNAME ?DB?
1043#
1044# If running a WAL test, execute a tests case that fails if the main
1045# database for connection handle DB is not currently a WAL database.
1046# Otherwise (if not running a WAL permutation) this is a no-op.
1047#
1048# wal_is_wal_mode
1049#
1050# Returns true if this test should be run in WAL mode. False otherwise.
1051#
1052proc wal_is_wal_mode {} {
1053 expr { [catch {set ::permutations_test_prefix} v]==0 && $v == "wal" }
1054}
1055proc wal_set_journal_mode {{db db}} {
1056 if { [wal_is_wal_mode] } {
1057 $db eval "PRAGMA journal_mode = WAL"
1058 }
1059}
1060proc wal_check_journal_mode {testname {db db}} {
1061 if { [wal_is_wal_mode] } {
1062 $db eval { SELECT * FROM sqlite_master }
1063 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1064 }
1065}
1066
danc1a60c52010-06-07 14:28:16 +00001067#-------------------------------------------------------------------------
1068#
1069proc slave_test_script {script} {
1070
1071 # Create the interpreter used to run the test script.
1072 interp create tinterp
1073
1074 # Populate some global variables that tester.tcl expects to see.
1075 foreach {var value} [list \
1076 ::argv0 $::argv0 \
1077 ::argv {} \
1078 ::SLAVE 1 \
1079 ] {
1080 interp eval tinterp [list set $var $value]
1081 }
1082
1083 # The alias used to access the global test counters.
1084 tinterp alias set_test_counter set_test_counter
1085
1086 # Set up the ::cmdlinearg array in the slave.
1087 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1088
1089 # Load the various test interfaces implemented in C.
1090 load_testfixture_extensions tinterp
1091
1092 # Run the test script.
1093 interp eval tinterp $script
1094
1095 # Delete the interpreter used to run the test script.
1096 interp delete tinterp
1097}
1098
1099proc slave_test_file {file} {
1100 set zFile [file join $::testdir $file]
1101 set time [time {
1102 slave_test_script [list source $zFile]
1103 }]
1104 puts "time $file [lrange $time 0 1]"
1105}
1106
danf5894502009-10-07 18:41:19 +00001107
danielk197745901d62004-11-10 15:27:38 +00001108# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
1109# to non-zero, then set the global variable $AUTOVACUUM to 1.
1110set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00001111
1112source $testdir/thread_common.tcl