drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1 | # 2001 September 15 |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 5 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 6 | # May you do good and not evil. |
| 7 | # May you find forgiveness for yourself and forgive others. |
| 8 | # May you share freely, never taking more than you give. |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 9 | # |
| 10 | #*********************************************************************** |
| 11 | # This file implements some common TCL routines used for regression |
| 12 | # testing the SQLite library |
| 13 | # |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame^] | 14 | # $Id: tester.tcl,v 1.116 2008/04/17 19:14:02 drh Exp $ |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 15 | |
drh | 8359d8c | 2008-03-29 11:00:54 +0000 | [diff] [blame] | 16 | # |
| 17 | # What for user input before continuing. This gives an opportunity |
| 18 | # to connect profiling tools to the process. |
| 19 | # |
| 20 | for {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 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 28 | |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 29 | set tcl_precision 15 |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 30 | set sqlite_pending_byte 0x0010000 |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 31 | |
drh | 3a7fb7c | 2007-08-10 16:41:08 +0000 | [diff] [blame] | 32 | # |
| 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 | # |
| 39 | if {![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 | } |
| 50 | sqlite3_soft_heap_limit $soft_limit |
| 51 | |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 52 | # |
| 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 | # |
| 59 | for {set i 0} {$i<[llength $argv]} {incr i} { |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 60 | if {[lindex $argv $i] eq "--malloctrace"} { |
| 61 | set argv [lreplace $argv $i $i] |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 62 | sqlite3_memdebug_backtrace 10 |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 63 | sqlite3_memdebug_log start |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 64 | set tester_do_malloctrace 1 |
| 65 | } |
| 66 | } |
| 67 | for {set i 0} {$i<[llength $argv]} {incr i} { |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 68 | if {[regexp {^--backtrace=(\d+)$} [lindex $argv $i] all value]} { |
| 69 | sqlite3_memdebug_backtrace $value |
| 70 | set argv [lreplace $argv $i $i] |
| 71 | } |
| 72 | } |
| 73 | |
danielk1977 | 1e21fd5 | 2008-04-10 17:27:38 +0000 | [diff] [blame] | 74 | |
| 75 | proc ostrace_call {zCall nClick zFile i32 i64} { |
| 76 | set s "INSERT INTO ostrace VALUES( '$zCall', $nClick, '$zFile', $i32, $i64);" |
| 77 | puts $::ostrace_fd $s |
| 78 | } |
| 79 | |
| 80 | for {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 |
| 91 | } |
| 92 | set argv [lreplace $argv $i $i] |
| 93 | } |
| 94 | } |
| 95 | |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 96 | # |
| 97 | # Check the command-line arguments to set the maximum number of |
| 98 | # errors tolerated before halting. |
| 99 | # |
| 100 | if {![info exists maxErr]} { |
| 101 | set maxErr 1000 |
| 102 | } |
| 103 | for {set i 0} {$i<[llength $argv]} {incr i} { |
| 104 | if {[regexp {^--maxerror=(\d+)$} [lindex $argv $i] all maxErr]} { |
| 105 | set argv [lreplace $argv $i $i] |
| 106 | } |
| 107 | } |
| 108 | #puts "Max error = $maxErr" |
| 109 | |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 110 | |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 111 | # Use the pager codec if it is available |
| 112 | # |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 113 | if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} { |
| 114 | rename sqlite3 sqlite_orig |
| 115 | proc sqlite3 {args} { |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 116 | if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} { |
| 117 | lappend args -key {xyzzy} |
| 118 | } |
| 119 | uplevel 1 sqlite_orig $args |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 124 | # Create a test database |
| 125 | # |
drh | 254cba2 | 2001-09-20 01:44:42 +0000 | [diff] [blame] | 126 | catch {db close} |
| 127 | file delete -force test.db |
| 128 | file delete -force test.db-journal |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 129 | sqlite3 db ./test.db |
| 130 | set ::DB [sqlite3_connection_pointer db] |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 131 | if {[info exists ::SETUP_SQL]} { |
| 132 | db eval $::SETUP_SQL |
| 133 | } |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 134 | |
| 135 | # Abort early if this script has been run before. |
| 136 | # |
| 137 | if {[info exists nTest]} return |
| 138 | |
| 139 | # Set the test counters to zero |
| 140 | # |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 141 | set nErr 0 |
| 142 | set nTest 0 |
drh | 767c200 | 2000-10-19 14:10:08 +0000 | [diff] [blame] | 143 | set skip_test 0 |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 144 | set failList {} |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 145 | set omitList {} |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 146 | if {![info exists speedTest]} { |
| 147 | set speedTest 0 |
| 148 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 149 | |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 150 | # Record the fact that a sequence of tests were omitted. |
| 151 | # |
| 152 | proc omit_test {name reason} { |
| 153 | global omitList |
| 154 | lappend omitList [list $name $reason] |
| 155 | } |
| 156 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 157 | # Invoke the do_test procedure to run a single test |
| 158 | # |
| 159 | proc do_test {name cmd expected} { |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 160 | global argv nErr nTest skip_test maxErr |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 161 | sqlite3_memdebug_settitle $name |
drh | 767c200 | 2000-10-19 14:10:08 +0000 | [diff] [blame] | 162 | if {$skip_test} { |
| 163 | set skip_test 0 |
| 164 | return |
| 165 | } |
| 166 | if {[llength $argv]==0} { |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 167 | set go 1 |
| 168 | } else { |
| 169 | set go 0 |
| 170 | foreach pattern $argv { |
| 171 | if {[string match $pattern $name]} { |
| 172 | set go 1 |
| 173 | break |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | if {!$go} return |
| 178 | incr nTest |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 179 | puts -nonewline $name... |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 180 | flush stdout |
| 181 | if {[catch {uplevel #0 "$cmd;\n"} result]} { |
| 182 | puts "\nError: $result" |
| 183 | incr nErr |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 184 | lappend ::failList $name |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 185 | if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 186 | } elseif {[string compare $result $expected]} { |
| 187 | puts "\nExpected: \[$expected\]\n Got: \[$result\]" |
| 188 | incr nErr |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 189 | lappend ::failList $name |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 190 | if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 191 | } else { |
| 192 | puts " Ok" |
| 193 | } |
drh | 6558db8 | 2007-04-13 03:23:21 +0000 | [diff] [blame] | 194 | flush stdout |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 195 | } |
| 196 | |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 197 | # Run an SQL script. |
| 198 | # Return the number of microseconds per statement. |
| 199 | # |
drh | 3590f15 | 2006-11-23 21:09:10 +0000 | [diff] [blame] | 200 | proc speed_trial {name numstmt units sql} { |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 201 | puts -nonewline [format {%-21.21s } $name...] |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 202 | flush stdout |
| 203 | set speed [time {sqlite3_exec_nr db $sql}] |
| 204 | set tm [lindex $speed 0] |
danielk1977 | 0ee26d9 | 2008-02-08 18:25:29 +0000 | [diff] [blame] | 205 | if {$tm == 0} { |
| 206 | set rate [format %20s "many"] |
| 207 | } else { |
| 208 | set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]] |
| 209 | } |
drh | 3590f15 | 2006-11-23 21:09:10 +0000 | [diff] [blame] | 210 | set u2 $units/s |
danielk1977 | 0ee26d9 | 2008-02-08 18:25:29 +0000 | [diff] [blame] | 211 | puts [format {%12d uS %s %s} $tm $rate $u2] |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 212 | global total_time |
| 213 | set total_time [expr {$total_time+$tm}] |
| 214 | } |
drh | 45c236d | 2008-03-22 01:08:00 +0000 | [diff] [blame] | 215 | proc speed_trial_tcl {name numstmt units script} { |
| 216 | puts -nonewline [format {%-21.21s } $name...] |
| 217 | flush stdout |
| 218 | set speed [time {eval $script}] |
| 219 | set tm [lindex $speed 0] |
| 220 | if {$tm == 0} { |
| 221 | set rate [format %20s "many"] |
| 222 | } else { |
| 223 | set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]] |
| 224 | } |
| 225 | set u2 $units/s |
| 226 | puts [format {%12d uS %s %s} $tm $rate $u2] |
| 227 | global total_time |
| 228 | set total_time [expr {$total_time+$tm}] |
| 229 | } |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 230 | proc speed_trial_init {name} { |
| 231 | global total_time |
| 232 | set total_time 0 |
| 233 | } |
| 234 | proc speed_trial_summary {name} { |
| 235 | global total_time |
| 236 | puts [format {%-21.21s %12d uS TOTAL} $name $total_time] |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 237 | } |
| 238 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 239 | # Run this routine last |
| 240 | # |
| 241 | proc finish_test {} { |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 242 | finalize_testing |
| 243 | } |
| 244 | proc finalize_testing {} { |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 245 | global nTest nErr sqlite_open_file_count omitList |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 246 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 247 | catch {db close} |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 248 | catch {db2 close} |
| 249 | catch {db3 close} |
| 250 | |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 251 | vfs_unlink_test |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 252 | sqlite3 db {} |
danielk1977 | cbb8496 | 2006-01-17 16:10:13 +0000 | [diff] [blame] | 253 | # sqlite3_clear_tsd_memdebug |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 254 | db close |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 255 | sqlite3_reset_auto_extension |
drh | 3a7fb7c | 2007-08-10 16:41:08 +0000 | [diff] [blame] | 256 | set heaplimit [sqlite3_soft_heap_limit] |
| 257 | if {$heaplimit!=$::soft_limit} { |
| 258 | puts "soft-heap-limit changed by this script\ |
| 259 | from $::soft_limit to $heaplimit" |
| 260 | } elseif {$heaplimit!="" && $heaplimit>0} { |
| 261 | puts "soft-heap-limit set to $heaplimit" |
| 262 | } |
| 263 | sqlite3_soft_heap_limit 0 |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 264 | incr nTest |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 265 | puts "$nErr errors out of $nTest tests" |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 266 | if {$nErr>0} { |
| 267 | puts "Failures on these tests: $::failList" |
| 268 | } |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 269 | if {[llength $omitList]>0} { |
| 270 | puts "Omitted test cases:" |
| 271 | set prec {} |
| 272 | foreach {rec} [lsort $omitList] { |
| 273 | if {$rec==$prec} continue |
| 274 | set prec $rec |
| 275 | puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]] |
| 276 | } |
| 277 | } |
drh | 80788d8 | 2006-09-02 14:50:23 +0000 | [diff] [blame] | 278 | if {$nErr>0 && ![working_64bit_int]} { |
| 279 | puts "******************************************************************" |
| 280 | puts "N.B.: The version of TCL that you used to build this test harness" |
| 281 | puts "is defective in that it does not support 64-bit integers. Some or" |
| 282 | puts "all of the test failures above might be a result from this defect" |
| 283 | puts "in your TCL build." |
| 284 | puts "******************************************************************" |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 285 | } |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 286 | if {$sqlite_open_file_count} { |
| 287 | puts "$sqlite_open_file_count files were left open" |
| 288 | incr nErr |
| 289 | } |
danielk1977 | 1e21fd5 | 2008-04-10 17:27:38 +0000 | [diff] [blame] | 290 | if {[info exists ::tester_do_ostrace]} { |
| 291 | puts "Writing ostrace.sql..." |
| 292 | set fd $::ostrace_fd |
| 293 | |
| 294 | puts -nonewline $fd "CREATE TABLE ossummary" |
| 295 | puts $fd "(method TEXT, clicks INTEGER, count INTEGER);" |
| 296 | foreach row [sqlite3_instvfs report ostrace] { |
| 297 | foreach {method count clicks} $row break |
| 298 | puts $fd "INSERT INTO ossummary VALUES('$method', $clicks, $count);" |
| 299 | } |
| 300 | puts $fd "COMMIT;" |
| 301 | close $fd |
| 302 | sqlite3_instvfs destroy ostrace |
| 303 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 304 | if {[sqlite3_memory_used]>0} { |
| 305 | puts "Unfreed memory: [sqlite3_memory_used] bytes" |
| 306 | incr nErr |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 307 | ifcapable memdebug||mem5||(mem3&&debug) { |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 308 | puts "Writing unfreed memory log to \"./memleak.txt\"" |
| 309 | sqlite3_memdebug_dump ./memleak.txt |
| 310 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 311 | } else { |
| 312 | puts "All memory allocations freed - no leaks" |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 313 | ifcapable memdebug||mem5 { |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 314 | sqlite3_memdebug_dump ./memusage.txt |
| 315 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 316 | } |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 317 | puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes" |
| 318 | puts "Current memory usage: [sqlite3_memory_highwater] bytes" |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 319 | if {[info commands sqlite3_memdebug_malloc_count] ne ""} { |
| 320 | puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls" |
| 321 | } |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 322 | if {[info exists ::tester_do_malloctrace]} { |
| 323 | puts "Writing mallocs.sql..." |
| 324 | memdebug_log_sql |
| 325 | sqlite3_memdebug_log stop |
| 326 | sqlite3_memdebug_log clear |
danielk1977 | dbdc4d4 | 2008-03-28 07:42:53 +0000 | [diff] [blame] | 327 | |
| 328 | if {[sqlite3_memory_used]>0} { |
| 329 | puts "Writing leaks.sql..." |
| 330 | sqlite3_memdebug_log sync |
| 331 | memdebug_log_sql leaks.sql |
| 332 | } |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 333 | } |
drh | d9910fe | 2006-10-04 11:55:49 +0000 | [diff] [blame] | 334 | foreach f [glob -nocomplain test.db-*-journal] { |
| 335 | file delete -force $f |
| 336 | } |
| 337 | foreach f [glob -nocomplain test.db-mj*] { |
| 338 | file delete -force $f |
| 339 | } |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 340 | exit [expr {$nErr>0}] |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 341 | } |
| 342 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 343 | # A procedure to execute SQL |
| 344 | # |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 345 | proc execsql {sql {db db}} { |
drh | acbcdc4 | 2001-01-22 00:31:53 +0000 | [diff] [blame] | 346 | # puts "SQL = $sql" |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame] | 347 | uplevel [list $db eval $sql] |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 348 | } |
drh | 3aadb2e | 2000-05-31 17:59:25 +0000 | [diff] [blame] | 349 | |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 350 | # Execute SQL and catch exceptions. |
| 351 | # |
| 352 | proc catchsql {sql {db db}} { |
| 353 | # puts "SQL = $sql" |
| 354 | set r [catch {$db eval $sql} msg] |
| 355 | lappend r $msg |
| 356 | return $r |
| 357 | } |
| 358 | |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 359 | # Do an VDBE code dump on the SQL given |
| 360 | # |
| 361 | proc explain {sql {db db}} { |
| 362 | puts "" |
danielk1977 | 287fb61 | 2008-01-04 19:10:28 +0000 | [diff] [blame] | 363 | puts "addr opcode p1 p2 p3 p4 p5 #" |
| 364 | puts "---- ------------ ------ ------ ------ --------------- -- -" |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 365 | $db eval "explain $sql" {} { |
danielk1977 | 287fb61 | 2008-01-04 19:10:28 +0000 | [diff] [blame] | 366 | puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \ |
| 367 | $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment |
| 368 | ] |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame^] | 372 | # Show the VDBE program for an SQL statement but omit the Trace |
| 373 | # opcode at the beginning. This procedure can be used to prove |
| 374 | # that different SQL statements generate exactly the same VDBE code. |
| 375 | # |
| 376 | proc explain_no_trace {sql} { |
| 377 | set tr [db eval "EXPLAIN $sql"] |
| 378 | return [lrange $tr 7 end] |
| 379 | } |
| 380 | |
drh | 3aadb2e | 2000-05-31 17:59:25 +0000 | [diff] [blame] | 381 | # Another procedure to execute SQL. This one includes the field |
| 382 | # names in the returned list. |
| 383 | # |
| 384 | proc execsql2 {sql} { |
| 385 | set result {} |
| 386 | db eval $sql data { |
| 387 | foreach f $data(*) { |
| 388 | lappend result $f $data($f) |
| 389 | } |
| 390 | } |
| 391 | return $result |
| 392 | } |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 393 | |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 394 | # Use the non-callback API to execute multiple SQL statements |
| 395 | # |
| 396 | proc stepsql {dbptr sql} { |
| 397 | set sql [string trim $sql] |
| 398 | set r 0 |
| 399 | while {[string length $sql]>0} { |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 400 | if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} { |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 401 | return [list 1 $vm] |
| 402 | } |
| 403 | set sql [string trim $sqltail] |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 404 | # while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} { |
| 405 | # foreach v $VAL {lappend r $v} |
| 406 | # } |
| 407 | while {[sqlite3_step $vm]=="SQLITE_ROW"} { |
| 408 | for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} { |
| 409 | lappend r [sqlite3_column_text $vm $i] |
| 410 | } |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 411 | } |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 412 | if {[catch {sqlite3_finalize $vm} errmsg]} { |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 413 | return [list 1 $errmsg] |
| 414 | } |
| 415 | } |
| 416 | return $r |
| 417 | } |
| 418 | |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 419 | # Delete a file or directory |
| 420 | # |
| 421 | proc forcedelete {filename} { |
| 422 | if {[catch {file delete -force $filename}]} { |
| 423 | exec rm -rf $filename |
| 424 | } |
| 425 | } |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 426 | |
| 427 | # Do an integrity check of the entire database |
| 428 | # |
| 429 | proc integrity_check {name} { |
drh | 27d258a | 2004-10-30 20:23:09 +0000 | [diff] [blame] | 430 | ifcapable integrityck { |
| 431 | do_test $name { |
| 432 | execsql {PRAGMA integrity_check} |
| 433 | } {ok} |
| 434 | } |
| 435 | } |
| 436 | |
danielk1977 | db4e886 | 2008-01-16 08:24:46 +0000 | [diff] [blame] | 437 | proc fix_ifcapable_expr {expr} { |
| 438 | set ret "" |
| 439 | set state 0 |
| 440 | for {set i 0} {$i < [string length $expr]} {incr i} { |
| 441 | set char [string range $expr $i $i] |
| 442 | set newstate [expr {[string is alnum $char] || $char eq "_"}] |
| 443 | if {$newstate && !$state} { |
| 444 | append ret {$::sqlite_options(} |
| 445 | } |
| 446 | if {!$newstate && $state} { |
| 447 | append ret ) |
| 448 | } |
| 449 | append ret $char |
| 450 | set state $newstate |
| 451 | } |
| 452 | if {$state} {append ret )} |
| 453 | return $ret |
| 454 | } |
| 455 | |
drh | 27d258a | 2004-10-30 20:23:09 +0000 | [diff] [blame] | 456 | # Evaluate a boolean expression of capabilities. If true, execute the |
| 457 | # code. Omit the code if false. |
| 458 | # |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 459 | proc ifcapable {expr code {else ""} {elsecode ""}} { |
danielk1977 | db4e886 | 2008-01-16 08:24:46 +0000 | [diff] [blame] | 460 | #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2 |
| 461 | set e2 [fix_ifcapable_expr $expr] |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 462 | if ($e2) { |
| 463 | set c [catch {uplevel 1 $code} r] |
| 464 | } else { |
| 465 | set c [catch {uplevel 1 $elsecode} r] |
| 466 | } |
| 467 | return -code $c $r |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 468 | } |
danielk1977 | 45901d6 | 2004-11-10 15:27:38 +0000 | [diff] [blame] | 469 | |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 470 | # This proc execs a seperate process that crashes midway through executing |
| 471 | # the SQL script $sql on database test.db. |
| 472 | # |
| 473 | # The crash occurs during a sync() of file $crashfile. When the crash |
| 474 | # occurs a random subset of all unsynced writes made by the process are |
| 475 | # written into the files on disk. Argument $crashdelay indicates the |
| 476 | # number of file syncs to wait before crashing. |
| 477 | # |
| 478 | # The return value is a list of two elements. The first element is a |
| 479 | # boolean, indicating whether or not the process actually crashed or |
| 480 | # reported some other error. The second element in the returned list is the |
| 481 | # error message. This is "child process exited abnormally" if the crash |
| 482 | # occured. |
| 483 | # |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 484 | # crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 485 | # |
| 486 | proc crashsql {args} { |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 487 | if {$::tcl_platform(platform)!="unix"} { |
| 488 | error "crashsql should only be used on unix" |
| 489 | } |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 490 | |
| 491 | set blocksize "" |
| 492 | set crashdelay 1 |
drh | cb1f0f6 | 2008-01-08 15:18:52 +0000 | [diff] [blame] | 493 | set prngseed 0 |
drh | f858740 | 2008-01-08 16:03:49 +0000 | [diff] [blame] | 494 | set tclbody {} |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 495 | set crashfile "" |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 496 | set dc "" |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 497 | set sql [lindex $args end] |
| 498 | |
| 499 | for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} { |
| 500 | set z [lindex $args $ii] |
| 501 | set n [string length $z] |
| 502 | set z2 [lindex $args [expr $ii+1]] |
| 503 | |
| 504 | if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \ |
drh | cb1f0f6 | 2008-01-08 15:18:52 +0000 | [diff] [blame] | 505 | elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \ |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 506 | elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \ |
drh | f858740 | 2008-01-08 16:03:49 +0000 | [diff] [blame] | 507 | elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 508 | elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \ |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 509 | elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \ |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 510 | else { error "Unrecognized option: $z" } |
| 511 | } |
| 512 | |
| 513 | if {$crashfile eq ""} { |
| 514 | error "Compulsory option -file missing" |
| 515 | } |
| 516 | |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 517 | set cfile [file join [pwd] $crashfile] |
| 518 | |
| 519 | set f [open crash.tcl w] |
danielk1977 | ca0c897 | 2007-09-01 09:02:53 +0000 | [diff] [blame] | 520 | puts $f "sqlite3_crash_enable 1" |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 521 | puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile" |
danielk1977 | 933bbd6 | 2007-03-17 07:22:42 +0000 | [diff] [blame] | 522 | puts $f "set sqlite_pending_byte $::sqlite_pending_byte" |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 523 | puts $f "sqlite3 db test.db -vfs crash" |
danielk1977 | 933bbd6 | 2007-03-17 07:22:42 +0000 | [diff] [blame] | 524 | |
| 525 | # This block sets the cache size of the main database to 10 |
| 526 | # pages. This is done in case the build is configured to omit |
| 527 | # "PRAGMA cache_size". |
| 528 | puts $f {db eval {SELECT * FROM sqlite_master;}} |
| 529 | puts $f {set bt [btree_from_db db]} |
| 530 | puts $f {btree_set_cache_size $bt 10} |
drh | cb1f0f6 | 2008-01-08 15:18:52 +0000 | [diff] [blame] | 531 | if {$prngseed} { |
| 532 | set seed [expr {$prngseed%10007+1}] |
| 533 | # puts seed=$seed |
| 534 | puts $f "db eval {SELECT randomblob($seed)}" |
| 535 | } |
danielk1977 | 933bbd6 | 2007-03-17 07:22:42 +0000 | [diff] [blame] | 536 | |
drh | f858740 | 2008-01-08 16:03:49 +0000 | [diff] [blame] | 537 | if {[string length $tclbody]>0} { |
| 538 | puts $f $tclbody |
| 539 | } |
| 540 | if {[string length $sql]>0} { |
| 541 | puts $f "db eval {" |
| 542 | puts $f "$sql" |
| 543 | puts $f "}" |
| 544 | } |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 545 | close $f |
| 546 | |
| 547 | set r [catch { |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 548 | exec [info nameofexec] crash.tcl >@stdout |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 549 | } msg] |
| 550 | lappend r $msg |
| 551 | } |
| 552 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 553 | # Usage: do_ioerr_test <test number> <options...> |
| 554 | # |
| 555 | # This proc is used to implement test cases that check that IO errors |
| 556 | # are correctly handled. The first argument, <test number>, is an integer |
| 557 | # used to name the tests executed by this proc. Options are as follows: |
| 558 | # |
| 559 | # -tclprep TCL script to run to prepare test. |
| 560 | # -sqlprep SQL script to run to prepare test. |
| 561 | # -tclbody TCL script to run with IO error simulation. |
| 562 | # -sqlbody TCL script to run with IO error simulation. |
| 563 | # -exclude List of 'N' values not to test. |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 564 | # -erc Use extended result codes |
drh | d5eb79e | 2007-03-15 12:17:42 +0000 | [diff] [blame] | 565 | # -persist Make simulated I/O errors persistent |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 566 | # -start Value of 'N' to begin with (default 1) |
| 567 | # |
| 568 | # -cksum Boolean. If true, test that the database does |
| 569 | # not change during the execution of the test case. |
| 570 | # |
| 571 | proc do_ioerr_test {testname args} { |
| 572 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 573 | set ::ioerropts(-start) 1 |
| 574 | set ::ioerropts(-cksum) 0 |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 575 | set ::ioerropts(-erc) 0 |
drh | c2ee76c | 2007-01-04 14:58:14 +0000 | [diff] [blame] | 576 | set ::ioerropts(-count) 100000000 |
drh | d5eb79e | 2007-03-15 12:17:42 +0000 | [diff] [blame] | 577 | set ::ioerropts(-persist) 1 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 578 | array set ::ioerropts $args |
| 579 | |
| 580 | set ::go 1 |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 581 | #reset_prng_state |
| 582 | save_prng_state |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 583 | for {set n $::ioerropts(-start)} {$::go} {incr n} { |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 584 | set ::TN $n |
drh | c2ee76c | 2007-01-04 14:58:14 +0000 | [diff] [blame] | 585 | incr ::ioerropts(-count) -1 |
| 586 | if {$::ioerropts(-count)<0} break |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 587 | |
| 588 | # Skip this IO error if it was specified with the "-exclude" option. |
| 589 | if {[info exists ::ioerropts(-exclude)]} { |
| 590 | if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue |
| 591 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 592 | restore_prng_state |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 593 | |
| 594 | # Delete the files test.db and test2.db, then execute the TCL and |
| 595 | # SQL (in that order) to prepare for the test case. |
| 596 | do_test $testname.$n.1 { |
| 597 | set ::sqlite_io_error_pending 0 |
| 598 | catch {db close} |
| 599 | catch {file delete -force test.db} |
| 600 | catch {file delete -force test.db-journal} |
| 601 | catch {file delete -force test2.db} |
| 602 | catch {file delete -force test2.db-journal} |
drh | a34c62d | 2006-01-06 22:11:20 +0000 | [diff] [blame] | 603 | set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db] |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 604 | sqlite3_extended_result_codes $::DB $::ioerropts(-erc) |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 605 | if {[info exists ::ioerropts(-tclprep)]} { |
| 606 | eval $::ioerropts(-tclprep) |
| 607 | } |
| 608 | if {[info exists ::ioerropts(-sqlprep)]} { |
| 609 | execsql $::ioerropts(-sqlprep) |
| 610 | } |
| 611 | expr 0 |
| 612 | } {0} |
| 613 | |
| 614 | # Read the 'checksum' of the database. |
| 615 | if {$::ioerropts(-cksum)} { |
| 616 | set checksum [cksum] |
| 617 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 618 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 619 | # Set the Nth IO error to fail. |
| 620 | do_test $testname.$n.2 [subst { |
drh | d5eb79e | 2007-03-15 12:17:42 +0000 | [diff] [blame] | 621 | set ::sqlite_io_error_persist $::ioerropts(-persist) |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 622 | set ::sqlite_io_error_pending $n |
| 623 | }] $n |
| 624 | |
| 625 | # Create a single TCL script from the TCL and SQL specified |
| 626 | # as the body of the test. |
| 627 | set ::ioerrorbody {} |
| 628 | if {[info exists ::ioerropts(-tclbody)]} { |
| 629 | append ::ioerrorbody "$::ioerropts(-tclbody)\n" |
| 630 | } |
| 631 | if {[info exists ::ioerropts(-sqlbody)]} { |
| 632 | append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}" |
| 633 | } |
| 634 | |
| 635 | # Execute the TCL Script created in the above block. If |
| 636 | # there are at least N IO operations performed by SQLite as |
| 637 | # a result of the script, the Nth will fail. |
| 638 | do_test $testname.$n.3 { |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 639 | set ::sqlite_io_error_hit 0 |
| 640 | set ::sqlite_io_error_hardhit 0 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 641 | set r [catch $::ioerrorbody msg] |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 642 | set ::errseen $r |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 643 | set rc [sqlite3_errcode $::DB] |
| 644 | if {$::ioerropts(-erc)} { |
drh | 5f7b5bf | 2007-04-19 12:30:54 +0000 | [diff] [blame] | 645 | # If we are in extended result code mode, make sure all of the |
| 646 | # IOERRs we get back really do have their extended code values. |
| 647 | # If an extended result code is returned, the sqlite3_errcode |
| 648 | # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn |
| 649 | # where nnnn is a number |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 650 | if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} { |
| 651 | return $rc |
| 652 | } |
| 653 | } else { |
drh | 5f7b5bf | 2007-04-19 12:30:54 +0000 | [diff] [blame] | 654 | # If we are not in extended result code mode, make sure no |
| 655 | # extended error codes are returned. |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 656 | if {[regexp {\+\d} $rc]} { |
| 657 | return $rc |
| 658 | } |
| 659 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 660 | # The test repeats as long as $::go is non-zero. $::go starts out |
| 661 | # as 1. When a test runs to completion without hitting an I/O |
| 662 | # error, that means there is no point in continuing with this test |
| 663 | # case so set $::go to zero. |
| 664 | # |
| 665 | if {$::sqlite_io_error_pending>0} { |
| 666 | set ::go 0 |
| 667 | set q 0 |
| 668 | set ::sqlite_io_error_pending 0 |
| 669 | } else { |
| 670 | set q 1 |
| 671 | } |
| 672 | |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 673 | set s [expr $::sqlite_io_error_hit==0] |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 674 | if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} { |
| 675 | set r 1 |
| 676 | } |
danielk1977 | f2fa831 | 2006-01-24 13:09:33 +0000 | [diff] [blame] | 677 | set ::sqlite_io_error_hit 0 |
drh | 5f7b5bf | 2007-04-19 12:30:54 +0000 | [diff] [blame] | 678 | |
| 679 | # One of two things must have happened. either |
| 680 | # 1. We never hit the IO error and the SQL returned OK |
| 681 | # 2. An IO error was hit and the SQL failed |
| 682 | # |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 683 | expr { ($s && !$r && !$q) || (!$s && $r && $q) } |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 684 | } {1} |
| 685 | |
| 686 | # If an IO error occured, then the checksum of the database should |
| 687 | # be the same as before the script that caused the IO error was run. |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 688 | if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} { |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 689 | do_test $testname.$n.4 { |
| 690 | catch {db close} |
drh | a34c62d | 2006-01-06 22:11:20 +0000 | [diff] [blame] | 691 | set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db] |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 692 | cksum |
| 693 | } $checksum |
| 694 | } |
| 695 | |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 696 | set ::sqlite_io_error_hardhit 0 |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 697 | set ::sqlite_io_error_pending 0 |
danielk1977 | 105afed | 2005-05-26 15:20:53 +0000 | [diff] [blame] | 698 | if {[info exists ::ioerropts(-cleanup)]} { |
| 699 | catch $::ioerropts(-cleanup) |
| 700 | } |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 701 | } |
| 702 | set ::sqlite_io_error_pending 0 |
drh | 6d54da0 | 2007-03-25 19:08:46 +0000 | [diff] [blame] | 703 | set ::sqlite_io_error_persist 0 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 704 | unset ::ioerropts |
| 705 | } |
| 706 | |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 707 | # Return a checksum based on the contents of the main database associated |
| 708 | # with connection $db |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 709 | # |
| 710 | proc cksum {{db db}} { |
| 711 | set txt [$db eval { |
| 712 | SELECT name, type, sql FROM sqlite_master order by name |
| 713 | }]\n |
| 714 | foreach tbl [$db eval { |
| 715 | SELECT name FROM sqlite_master WHERE type='table' order by name |
| 716 | }] { |
| 717 | append txt [$db eval "SELECT * FROM $tbl"]\n |
| 718 | } |
| 719 | foreach prag {default_synchronous default_cache_size} { |
| 720 | append txt $prag-[$db eval "PRAGMA $prag"]\n |
| 721 | } |
| 722 | set cksum [string length $txt]-[md5 $txt] |
| 723 | # puts $cksum-[file size test.db] |
| 724 | return $cksum |
| 725 | } |
| 726 | |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 727 | # Generate a checksum based on the contents of the main and temp tables |
| 728 | # database $db. If the checksum of two databases is the same, and the |
| 729 | # integrity-check passes for both, the two databases are identical. |
| 730 | # |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 731 | proc allcksum {{db db}} { |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 732 | set ret [list] |
| 733 | ifcapable tempdb { |
| 734 | set sql { |
| 735 | SELECT name FROM sqlite_master WHERE type = 'table' UNION |
| 736 | SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION |
| 737 | SELECT 'sqlite_master' UNION |
| 738 | SELECT 'sqlite_temp_master' ORDER BY 1 |
| 739 | } |
| 740 | } else { |
| 741 | set sql { |
| 742 | SELECT name FROM sqlite_master WHERE type = 'table' UNION |
| 743 | SELECT 'sqlite_master' ORDER BY 1 |
| 744 | } |
| 745 | } |
| 746 | set tbllist [$db eval $sql] |
| 747 | set txt {} |
| 748 | foreach tbl $tbllist { |
| 749 | append txt [$db eval "SELECT * FROM $tbl"] |
| 750 | } |
| 751 | foreach prag {default_cache_size} { |
| 752 | append txt $prag-[$db eval "PRAGMA $prag"]\n |
| 753 | } |
| 754 | # puts txt=$txt |
| 755 | return [md5 $txt] |
| 756 | } |
| 757 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 758 | proc memdebug_log_sql {{filename mallocs.sql}} { |
| 759 | |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 760 | set data [sqlite3_memdebug_log dump] |
| 761 | set nFrame [expr [llength [lindex $data 0]]-2] |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 762 | if {$nFrame < 0} { return "" } |
| 763 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 764 | set database temp |
| 765 | |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 766 | set tbl "CREATE TABLE ${database}.malloc(nCall, nByte" |
| 767 | for {set ii 1} {$ii <= $nFrame} {incr ii} { |
| 768 | append tbl ", f${ii}" |
| 769 | } |
| 770 | append tbl ");\n" |
| 771 | |
| 772 | set sql "" |
| 773 | foreach e $data { |
| 774 | append sql "INSERT INTO ${database}.malloc VALUES([join $e ,]);\n" |
| 775 | foreach f [lrange $e 2 end] { |
| 776 | set frames($f) 1 |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n" |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 781 | set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n" |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 782 | |
| 783 | foreach f [array names frames] { |
| 784 | set addr [format %x $f] |
| 785 | set cmd "addr2line -e [info nameofexec] $addr" |
| 786 | set line [eval exec $cmd] |
| 787 | append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n" |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 788 | |
| 789 | set file [lindex [split $line :] 0] |
| 790 | set files($file) 1 |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 791 | } |
| 792 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 793 | foreach f [array names files] { |
| 794 | set contents "" |
| 795 | catch { |
| 796 | set fd [open $f] |
| 797 | set contents [read $fd] |
| 798 | close $fd |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 799 | } |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 800 | set contents [string map {' ''} $contents] |
| 801 | append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n" |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 802 | } |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 803 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 804 | set fd [open $filename w] |
| 805 | puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;" |
| 806 | close $fd |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 807 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 808 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 809 | # Copy file $from into $to. This is used because some versions of |
| 810 | # TCL for windows (notably the 8.4.1 binary package shipped with the |
| 811 | # current mingw release) have a broken "file copy" command. |
| 812 | # |
| 813 | proc copy_file {from to} { |
| 814 | if {$::tcl_platform(platform)=="unix"} { |
| 815 | file copy -force $from $to |
| 816 | } else { |
| 817 | set f [open $from] |
| 818 | fconfigure $f -translation binary |
| 819 | set t [open $to w] |
| 820 | fconfigure $t -translation binary |
| 821 | puts -nonewline $t [read $f [file size $from]] |
| 822 | close $t |
| 823 | close $f |
| 824 | } |
| 825 | } |
| 826 | |
danielk1977 | 45901d6 | 2004-11-10 15:27:38 +0000 | [diff] [blame] | 827 | # If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set |
| 828 | # to non-zero, then set the global variable $AUTOVACUUM to 1. |
| 829 | set AUTOVACUUM $sqlite_options(default_autovacuum) |