2001-09-16 04:13:26 +04:00
|
|
|
# 2001 September 15
|
2000-05-30 00:41:49 +04:00
|
|
|
#
|
2001-09-16 04:13:26 +04:00
|
|
|
# The author disclaims copyright to this source code. In place of
|
|
|
|
# a legal notice, here is a blessing:
|
2000-05-30 00:41:49 +04:00
|
|
|
#
|
2001-09-16 04:13:26 +04:00
|
|
|
# May you do good and not evil.
|
|
|
|
# May you find forgiveness for yourself and forgive others.
|
|
|
|
# May you share freely, never taking more than you give.
|
2000-05-30 00:41:49 +04:00
|
|
|
#
|
|
|
|
#***********************************************************************
|
|
|
|
# This file implements some common TCL routines used for regression
|
|
|
|
# testing the SQLite library
|
|
|
|
#
|
2009-04-09 05:23:49 +04:00
|
|
|
# $Id: tester.tcl,v 1.143 2009/04/09 01:23:49 drh Exp $
|
2001-04-06 20:13:42 +04:00
|
|
|
|
2008-03-29 14:00:54 +03:00
|
|
|
#
|
|
|
|
# What for user input before continuing. This gives an opportunity
|
|
|
|
# to connect profiling tools to the process.
|
|
|
|
#
|
|
|
|
for {set i 0} {$i<[llength $argv]} {incr i} {
|
|
|
|
if {[regexp {^-+pause$} [lindex $argv $i] all value]} {
|
|
|
|
puts -nonewline "Press RETURN to begin..."
|
|
|
|
flush stdout
|
|
|
|
gets stdin
|
|
|
|
set argv [lreplace $argv $i $i]
|
|
|
|
}
|
|
|
|
}
|
2000-05-30 00:41:49 +04:00
|
|
|
|
2004-08-20 22:34:20 +04:00
|
|
|
set tcl_precision 15
|
2009-02-05 19:31:45 +03:00
|
|
|
sqlite3_test_control_pending_byte 0x0010000
|
2004-08-20 22:34:20 +04:00
|
|
|
|
2007-08-10 20:41:08 +04:00
|
|
|
#
|
|
|
|
# Check the command-line arguments for a default soft-heap-limit.
|
|
|
|
# Store this default value in the global variable ::soft_limit and
|
|
|
|
# update the soft-heap-limit each time this script is run. In that
|
|
|
|
# way if an individual test file changes the soft-heap-limit, it
|
|
|
|
# will be reset at the start of the next test file.
|
|
|
|
#
|
|
|
|
if {![info exists soft_limit]} {
|
|
|
|
set soft_limit 0
|
|
|
|
for {set i 0} {$i<[llength $argv]} {incr i} {
|
|
|
|
if {[regexp {^--soft-heap-limit=(.+)$} [lindex $argv $i] all value]} {
|
|
|
|
if {$value!="off"} {
|
|
|
|
set soft_limit $value
|
|
|
|
}
|
|
|
|
set argv [lreplace $argv $i $i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sqlite3_soft_heap_limit $soft_limit
|
|
|
|
|
2007-08-23 06:47:53 +04:00
|
|
|
#
|
|
|
|
# Check the command-line arguments to set the memory debugger
|
|
|
|
# backtrace depth.
|
|
|
|
#
|
|
|
|
# See the sqlite3_memdebug_backtrace() function in mem2.c or
|
|
|
|
# test_malloc.c for additional information.
|
|
|
|
#
|
2008-03-21 20:29:37 +03:00
|
|
|
for {set i 0} {$i<[llength $argv]} {incr i} {
|
|
|
|
if {[lindex $argv $i] eq "--malloctrace"} {
|
|
|
|
set argv [lreplace $argv $i $i]
|
2008-03-28 18:44:09 +03:00
|
|
|
sqlite3_memdebug_backtrace 10
|
2008-03-21 20:29:37 +03:00
|
|
|
sqlite3_memdebug_log start
|
|
|
|
set tester_do_malloctrace 1
|
|
|
|
}
|
|
|
|
}
|
2007-08-23 06:47:53 +04:00
|
|
|
for {set i 0} {$i<[llength $argv]} {incr i} {
|
|
|
|
if {[regexp {^--backtrace=(\d+)$} [lindex $argv $i] all value]} {
|
|
|
|
sqlite3_memdebug_backtrace $value
|
|
|
|
set argv [lreplace $argv $i $i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-10 21:27:38 +04:00
|
|
|
|
|
|
|
proc ostrace_call {zCall nClick zFile i32 i64} {
|
2008-05-12 11:42:20 +04:00
|
|
|
set s "INSERT INTO ostrace VALUES('$zCall', $nClick, '$zFile', $i32, $i64);"
|
2008-04-10 21:27:38 +04:00
|
|
|
puts $::ostrace_fd $s
|
|
|
|
}
|
|
|
|
|
|
|
|
for {set i 0} {$i<[llength $argv]} {incr i} {
|
|
|
|
if {[lindex $argv $i] eq "--ossummary" || [lindex $argv $i] eq "--ostrace"} {
|
|
|
|
sqlite3_instvfs create -default ostrace
|
|
|
|
set tester_do_ostrace 1
|
|
|
|
set ostrace_fd [open ostrace.sql w]
|
|
|
|
puts $ostrace_fd "BEGIN;"
|
|
|
|
if {[lindex $argv $i] eq "--ostrace"} {
|
|
|
|
set s "CREATE TABLE ostrace"
|
|
|
|
append s "(method TEXT, clicks INT, file TEXT, i32 INT, i64 INT);"
|
|
|
|
puts $ostrace_fd $s
|
|
|
|
sqlite3_instvfs configure ostrace ostrace_call
|
2008-05-08 19:58:06 +04:00
|
|
|
sqlite3_instvfs configure ostrace ostrace_call
|
2008-04-10 21:27:38 +04:00
|
|
|
}
|
|
|
|
set argv [lreplace $argv $i $i]
|
|
|
|
}
|
2008-05-08 19:58:06 +04:00
|
|
|
if {[lindex $argv $i] eq "--binarylog"} {
|
|
|
|
set tester_do_binarylog 1
|
|
|
|
set argv [lreplace $argv $i $i]
|
|
|
|
}
|
2008-04-10 21:27:38 +04:00
|
|
|
}
|
|
|
|
|
2008-01-07 22:20:24 +03:00
|
|
|
#
|
|
|
|
# Check the command-line arguments to set the maximum number of
|
|
|
|
# errors tolerated before halting.
|
|
|
|
#
|
|
|
|
if {![info exists maxErr]} {
|
|
|
|
set maxErr 1000
|
|
|
|
}
|
|
|
|
for {set i 0} {$i<[llength $argv]} {incr i} {
|
|
|
|
if {[regexp {^--maxerror=(\d+)$} [lindex $argv $i] all maxErr]} {
|
|
|
|
set argv [lreplace $argv $i $i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#puts "Max error = $maxErr"
|
|
|
|
|
2007-08-23 06:47:53 +04:00
|
|
|
|
2004-02-11 05:18:05 +03:00
|
|
|
# Use the pager codec if it is available
|
|
|
|
#
|
2004-06-19 04:16:31 +04:00
|
|
|
if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} {
|
|
|
|
rename sqlite3 sqlite_orig
|
|
|
|
proc sqlite3 {args} {
|
2004-02-11 05:18:05 +03:00
|
|
|
if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} {
|
|
|
|
lappend args -key {xyzzy}
|
|
|
|
}
|
|
|
|
uplevel 1 sqlite_orig $args
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-05-30 03:48:22 +04:00
|
|
|
# Create a test database
|
|
|
|
#
|
2008-06-23 22:49:43 +04:00
|
|
|
if {![info exists nTest]} {
|
|
|
|
sqlite3_shutdown
|
|
|
|
install_malloc_faultsim 1
|
|
|
|
sqlite3_initialize
|
2009-04-09 05:23:49 +04:00
|
|
|
autoinstall_test_functions
|
2008-07-12 19:55:54 +04:00
|
|
|
if {[info exists tester_do_binarylog]} {
|
|
|
|
sqlite3_instvfs binarylog -default binarylog ostrace.bin
|
|
|
|
sqlite3_instvfs marker binarylog "$argv0 $argv"
|
|
|
|
}
|
2008-06-23 22:49:43 +04:00
|
|
|
}
|
2009-01-09 13:49:14 +03:00
|
|
|
|
|
|
|
proc reset_db {} {
|
|
|
|
catch {db close}
|
|
|
|
file delete -force test.db
|
|
|
|
file delete -force test.db-journal
|
|
|
|
sqlite3 db ./test.db
|
|
|
|
set ::DB [sqlite3_connection_pointer db]
|
|
|
|
if {[info exists ::SETUP_SQL]} {
|
|
|
|
db eval $::SETUP_SQL
|
|
|
|
}
|
2002-03-07 01:01:34 +03:00
|
|
|
}
|
2009-01-09 13:49:14 +03:00
|
|
|
reset_db
|
2000-05-30 03:48:22 +04:00
|
|
|
|
|
|
|
# Abort early if this script has been run before.
|
|
|
|
#
|
|
|
|
if {[info exists nTest]} return
|
|
|
|
|
|
|
|
# Set the test counters to zero
|
|
|
|
#
|
2000-05-30 00:41:49 +04:00
|
|
|
set nErr 0
|
|
|
|
set nTest 0
|
2000-10-19 18:10:08 +04:00
|
|
|
set skip_test 0
|
2001-09-14 20:42:12 +04:00
|
|
|
set failList {}
|
2008-04-15 06:36:33 +04:00
|
|
|
set omitList {}
|
2006-11-23 12:39:16 +03:00
|
|
|
if {![info exists speedTest]} {
|
|
|
|
set speedTest 0
|
|
|
|
}
|
2000-05-30 00:41:49 +04:00
|
|
|
|
2008-04-15 06:36:33 +04:00
|
|
|
# Record the fact that a sequence of tests were omitted.
|
|
|
|
#
|
|
|
|
proc omit_test {name reason} {
|
|
|
|
global omitList
|
|
|
|
lappend omitList [list $name $reason]
|
|
|
|
}
|
|
|
|
|
2000-05-30 00:41:49 +04:00
|
|
|
# Invoke the do_test procedure to run a single test
|
|
|
|
#
|
|
|
|
proc do_test {name cmd expected} {
|
2004-05-21 02:16:29 +04:00
|
|
|
global argv nErr nTest skip_test maxErr
|
2007-08-23 06:47:53 +04:00
|
|
|
sqlite3_memdebug_settitle $name
|
2008-05-08 20:51:11 +04:00
|
|
|
if {[info exists ::tester_do_binarylog]} {
|
2008-05-08 19:58:06 +04:00
|
|
|
sqlite3_instvfs marker binarylog "Start of $name"
|
|
|
|
}
|
2000-10-19 18:10:08 +04:00
|
|
|
if {$skip_test} {
|
|
|
|
set skip_test 0
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if {[llength $argv]==0} {
|
2000-05-30 00:41:49 +04:00
|
|
|
set go 1
|
|
|
|
} else {
|
|
|
|
set go 0
|
|
|
|
foreach pattern $argv {
|
|
|
|
if {[string match $pattern $name]} {
|
|
|
|
set go 1
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if {!$go} return
|
|
|
|
incr nTest
|
2001-09-14 01:53:09 +04:00
|
|
|
puts -nonewline $name...
|
2000-05-30 00:41:49 +04:00
|
|
|
flush stdout
|
|
|
|
if {[catch {uplevel #0 "$cmd;\n"} result]} {
|
|
|
|
puts "\nError: $result"
|
|
|
|
incr nErr
|
2001-09-14 20:42:12 +04:00
|
|
|
lappend ::failList $name
|
2004-05-21 02:16:29 +04:00
|
|
|
if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing}
|
2000-05-30 00:41:49 +04:00
|
|
|
} elseif {[string compare $result $expected]} {
|
|
|
|
puts "\nExpected: \[$expected\]\n Got: \[$result\]"
|
|
|
|
incr nErr
|
2001-09-14 20:42:12 +04:00
|
|
|
lappend ::failList $name
|
2004-05-21 02:16:29 +04:00
|
|
|
if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing}
|
2000-05-30 00:41:49 +04:00
|
|
|
} else {
|
|
|
|
puts " Ok"
|
|
|
|
}
|
2007-04-13 07:23:21 +04:00
|
|
|
flush stdout
|
2008-05-08 20:51:11 +04:00
|
|
|
if {[info exists ::tester_do_binarylog]} {
|
2008-05-08 19:58:06 +04:00
|
|
|
sqlite3_instvfs marker binarylog "End of $name"
|
|
|
|
}
|
2000-05-30 00:41:49 +04:00
|
|
|
}
|
|
|
|
|
2006-11-23 12:39:16 +03:00
|
|
|
# Run an SQL script.
|
|
|
|
# Return the number of microseconds per statement.
|
|
|
|
#
|
2006-11-24 00:09:10 +03:00
|
|
|
proc speed_trial {name numstmt units sql} {
|
2007-04-01 02:34:16 +04:00
|
|
|
puts -nonewline [format {%-21.21s } $name...]
|
2006-11-23 12:39:16 +03:00
|
|
|
flush stdout
|
|
|
|
set speed [time {sqlite3_exec_nr db $sql}]
|
|
|
|
set tm [lindex $speed 0]
|
2008-02-08 21:25:29 +03:00
|
|
|
if {$tm == 0} {
|
|
|
|
set rate [format %20s "many"]
|
|
|
|
} else {
|
|
|
|
set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
|
|
|
|
}
|
2006-11-24 00:09:10 +03:00
|
|
|
set u2 $units/s
|
2008-02-08 21:25:29 +03:00
|
|
|
puts [format {%12d uS %s %s} $tm $rate $u2]
|
2007-04-01 02:34:16 +04:00
|
|
|
global total_time
|
|
|
|
set total_time [expr {$total_time+$tm}]
|
|
|
|
}
|
2008-03-22 04:08:00 +03:00
|
|
|
proc speed_trial_tcl {name numstmt units script} {
|
|
|
|
puts -nonewline [format {%-21.21s } $name...]
|
|
|
|
flush stdout
|
|
|
|
set speed [time {eval $script}]
|
|
|
|
set tm [lindex $speed 0]
|
|
|
|
if {$tm == 0} {
|
|
|
|
set rate [format %20s "many"]
|
|
|
|
} else {
|
|
|
|
set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
|
|
|
|
}
|
|
|
|
set u2 $units/s
|
|
|
|
puts [format {%12d uS %s %s} $tm $rate $u2]
|
|
|
|
global total_time
|
|
|
|
set total_time [expr {$total_time+$tm}]
|
|
|
|
}
|
2007-04-01 02:34:16 +04:00
|
|
|
proc speed_trial_init {name} {
|
|
|
|
global total_time
|
|
|
|
set total_time 0
|
|
|
|
}
|
|
|
|
proc speed_trial_summary {name} {
|
|
|
|
global total_time
|
|
|
|
puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
|
2006-11-23 12:39:16 +03:00
|
|
|
}
|
|
|
|
|
2000-05-30 00:41:49 +04:00
|
|
|
# Run this routine last
|
|
|
|
#
|
|
|
|
proc finish_test {} {
|
2001-09-14 20:42:12 +04:00
|
|
|
finalize_testing
|
|
|
|
}
|
|
|
|
proc finalize_testing {} {
|
2008-04-15 06:36:33 +04:00
|
|
|
global nTest nErr sqlite_open_file_count omitList
|
2005-12-09 17:25:08 +03:00
|
|
|
|
2000-06-08 17:36:40 +04:00
|
|
|
catch {db close}
|
2005-12-09 17:25:08 +03:00
|
|
|
catch {db2 close}
|
|
|
|
catch {db3 close}
|
|
|
|
|
2007-10-23 18:49:59 +04:00
|
|
|
vfs_unlink_test
|
2006-01-12 02:40:33 +03:00
|
|
|
sqlite3 db {}
|
2006-01-17 19:10:13 +03:00
|
|
|
# sqlite3_clear_tsd_memdebug
|
2006-01-12 02:40:33 +03:00
|
|
|
db close
|
2008-03-19 19:08:53 +03:00
|
|
|
sqlite3_reset_auto_extension
|
2007-08-10 20:41:08 +04:00
|
|
|
set heaplimit [sqlite3_soft_heap_limit]
|
|
|
|
if {$heaplimit!=$::soft_limit} {
|
|
|
|
puts "soft-heap-limit changed by this script\
|
|
|
|
from $::soft_limit to $heaplimit"
|
|
|
|
} elseif {$heaplimit!="" && $heaplimit>0} {
|
|
|
|
puts "soft-heap-limit set to $heaplimit"
|
|
|
|
}
|
|
|
|
sqlite3_soft_heap_limit 0
|
2006-01-12 02:40:33 +03:00
|
|
|
incr nTest
|
2000-05-30 00:41:49 +04:00
|
|
|
puts "$nErr errors out of $nTest tests"
|
2007-08-23 00:18:21 +04:00
|
|
|
if {$nErr>0} {
|
|
|
|
puts "Failures on these tests: $::failList"
|
|
|
|
}
|
2009-03-26 20:13:06 +03:00
|
|
|
run_thread_tests 1
|
2008-04-15 06:36:33 +04:00
|
|
|
if {[llength $omitList]>0} {
|
|
|
|
puts "Omitted test cases:"
|
|
|
|
set prec {}
|
|
|
|
foreach {rec} [lsort $omitList] {
|
|
|
|
if {$rec==$prec} continue
|
|
|
|
set prec $rec
|
|
|
|
puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
|
|
|
|
}
|
|
|
|
}
|
2006-09-02 18:50:23 +04:00
|
|
|
if {$nErr>0 && ![working_64bit_int]} {
|
|
|
|
puts "******************************************************************"
|
|
|
|
puts "N.B.: The version of TCL that you used to build this test harness"
|
|
|
|
puts "is defective in that it does not support 64-bit integers. Some or"
|
|
|
|
puts "all of the test failures above might be a result from this defect"
|
|
|
|
puts "in your TCL build."
|
|
|
|
puts "******************************************************************"
|
2001-03-15 21:21:22 +03:00
|
|
|
}
|
2008-05-08 19:58:06 +04:00
|
|
|
if {[info exists ::tester_do_binarylog]} {
|
|
|
|
sqlite3_instvfs destroy binarylog
|
|
|
|
}
|
2003-02-17 01:21:32 +03:00
|
|
|
if {$sqlite_open_file_count} {
|
|
|
|
puts "$sqlite_open_file_count files were left open"
|
|
|
|
incr nErr
|
|
|
|
}
|
2008-04-10 21:27:38 +04:00
|
|
|
if {[info exists ::tester_do_ostrace]} {
|
|
|
|
puts "Writing ostrace.sql..."
|
|
|
|
set fd $::ostrace_fd
|
|
|
|
|
|
|
|
puts -nonewline $fd "CREATE TABLE ossummary"
|
|
|
|
puts $fd "(method TEXT, clicks INTEGER, count INTEGER);"
|
|
|
|
foreach row [sqlite3_instvfs report ostrace] {
|
|
|
|
foreach {method count clicks} $row break
|
|
|
|
puts $fd "INSERT INTO ossummary VALUES('$method', $clicks, $count);"
|
|
|
|
}
|
|
|
|
puts $fd "COMMIT;"
|
|
|
|
close $fd
|
|
|
|
sqlite3_instvfs destroy ostrace
|
|
|
|
}
|
2007-08-23 00:18:21 +04:00
|
|
|
if {[sqlite3_memory_used]>0} {
|
|
|
|
puts "Unfreed memory: [sqlite3_memory_used] bytes"
|
|
|
|
incr nErr
|
2008-02-16 19:21:45 +03:00
|
|
|
ifcapable memdebug||mem5||(mem3&&debug) {
|
2007-08-23 06:47:53 +04:00
|
|
|
puts "Writing unfreed memory log to \"./memleak.txt\""
|
|
|
|
sqlite3_memdebug_dump ./memleak.txt
|
|
|
|
}
|
2007-08-23 00:18:21 +04:00
|
|
|
} else {
|
|
|
|
puts "All memory allocations freed - no leaks"
|
2008-02-16 19:21:45 +03:00
|
|
|
ifcapable memdebug||mem5 {
|
2007-10-15 23:34:32 +04:00
|
|
|
sqlite3_memdebug_dump ./memusage.txt
|
|
|
|
}
|
2007-08-23 00:18:21 +04:00
|
|
|
}
|
2008-06-19 04:16:08 +04:00
|
|
|
show_memstats
|
2008-01-19 23:11:25 +03:00
|
|
|
puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
|
|
|
|
puts "Current memory usage: [sqlite3_memory_highwater] bytes"
|
2008-02-13 21:25:27 +03:00
|
|
|
if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
|
|
|
|
puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
|
|
|
|
}
|
2008-03-21 20:29:37 +03:00
|
|
|
if {[info exists ::tester_do_malloctrace]} {
|
|
|
|
puts "Writing mallocs.sql..."
|
|
|
|
memdebug_log_sql
|
|
|
|
sqlite3_memdebug_log stop
|
|
|
|
sqlite3_memdebug_log clear
|
2008-03-28 10:42:53 +03:00
|
|
|
|
|
|
|
if {[sqlite3_memory_used]>0} {
|
|
|
|
puts "Writing leaks.sql..."
|
|
|
|
sqlite3_memdebug_log sync
|
|
|
|
memdebug_log_sql leaks.sql
|
|
|
|
}
|
2008-03-21 20:29:37 +03:00
|
|
|
}
|
2006-10-04 15:55:49 +04:00
|
|
|
foreach f [glob -nocomplain test.db-*-journal] {
|
|
|
|
file delete -force $f
|
|
|
|
}
|
|
|
|
foreach f [glob -nocomplain test.db-mj*] {
|
|
|
|
file delete -force $f
|
|
|
|
}
|
2001-03-15 21:21:22 +03:00
|
|
|
exit [expr {$nErr>0}]
|
2000-05-30 00:41:49 +04:00
|
|
|
}
|
|
|
|
|
2008-06-19 04:16:08 +04:00
|
|
|
# Display memory statistics for analysis and debugging purposes.
|
|
|
|
#
|
|
|
|
proc show_memstats {} {
|
|
|
|
set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
|
2008-08-05 21:53:22 +04:00
|
|
|
set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
|
|
|
|
set val [format {now %10d max %10d max-size %10d} \
|
|
|
|
[lindex $x 1] [lindex $x 2] [lindex $y 2]]
|
2008-06-19 04:16:08 +04:00
|
|
|
puts "Memory used: $val"
|
|
|
|
set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
|
2008-08-05 21:53:22 +04:00
|
|
|
set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
|
|
|
|
set val [format {now %10d max %10d max-size %10d} \
|
|
|
|
[lindex $x 1] [lindex $x 2] [lindex $y 2]]
|
2008-06-19 04:16:08 +04:00
|
|
|
puts "Page-cache used: $val"
|
|
|
|
set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
|
|
|
|
set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
|
|
|
|
puts "Page-cache overflow: $val"
|
|
|
|
set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
|
|
|
|
set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
|
|
|
|
puts "Scratch memory used: $val"
|
|
|
|
set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
|
2008-08-05 21:53:22 +04:00
|
|
|
set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
|
|
|
|
set val [format {now %10d max %10d max-size %10d} \
|
|
|
|
[lindex $x 1] [lindex $x 2] [lindex $y 2]]
|
2008-06-19 04:16:08 +04:00
|
|
|
puts "Scratch overflow: $val"
|
2008-07-25 19:39:03 +04:00
|
|
|
ifcapable yytrackmaxstackdepth {
|
|
|
|
set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
|
|
|
|
set val [format { max %10d} [lindex $x 2]]
|
|
|
|
puts "Parser stack depth: $val"
|
|
|
|
}
|
2008-06-19 04:16:08 +04:00
|
|
|
}
|
|
|
|
|
2000-05-30 00:41:49 +04:00
|
|
|
# A procedure to execute SQL
|
|
|
|
#
|
2001-04-04 15:48:57 +04:00
|
|
|
proc execsql {sql {db db}} {
|
2001-01-22 03:31:53 +03:00
|
|
|
# puts "SQL = $sql"
|
2006-01-17 12:35:01 +03:00
|
|
|
uplevel [list $db eval $sql]
|
2000-05-30 00:41:49 +04:00
|
|
|
}
|
2000-05-31 21:59:25 +04:00
|
|
|
|
2001-09-27 19:11:53 +04:00
|
|
|
# Execute SQL and catch exceptions.
|
|
|
|
#
|
|
|
|
proc catchsql {sql {db db}} {
|
|
|
|
# puts "SQL = $sql"
|
2009-11-28 15:40:32 +03:00
|
|
|
set r [catch [list uplevel [list $db eval $sql]] msg]
|
2001-09-27 19:11:53 +04:00
|
|
|
lappend r $msg
|
|
|
|
return $r
|
|
|
|
}
|
|
|
|
|
2001-11-10 01:41:44 +03:00
|
|
|
# Do an VDBE code dump on the SQL given
|
|
|
|
#
|
|
|
|
proc explain {sql {db db}} {
|
|
|
|
puts ""
|
2008-01-04 22:10:28 +03:00
|
|
|
puts "addr opcode p1 p2 p3 p4 p5 #"
|
|
|
|
puts "---- ------------ ------ ------ ------ --------------- -- -"
|
2001-11-10 01:41:44 +03:00
|
|
|
$db eval "explain $sql" {} {
|
2008-01-04 22:10:28 +03:00
|
|
|
puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
|
|
|
|
$addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
|
|
|
|
]
|
2001-11-10 01:41:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-17 23:14:02 +04:00
|
|
|
# Show the VDBE program for an SQL statement but omit the Trace
|
|
|
|
# opcode at the beginning. This procedure can be used to prove
|
|
|
|
# that different SQL statements generate exactly the same VDBE code.
|
|
|
|
#
|
|
|
|
proc explain_no_trace {sql} {
|
|
|
|
set tr [db eval "EXPLAIN $sql"]
|
|
|
|
return [lrange $tr 7 end]
|
|
|
|
}
|
|
|
|
|
2000-05-31 21:59:25 +04:00
|
|
|
# Another procedure to execute SQL. This one includes the field
|
|
|
|
# names in the returned list.
|
|
|
|
#
|
|
|
|
proc execsql2 {sql} {
|
|
|
|
set result {}
|
|
|
|
db eval $sql data {
|
|
|
|
foreach f $data(*) {
|
|
|
|
lappend result $f $data($f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result
|
|
|
|
}
|
2001-01-31 16:28:08 +03:00
|
|
|
|
2003-03-01 22:45:34 +03:00
|
|
|
# Use the non-callback API to execute multiple SQL statements
|
|
|
|
#
|
|
|
|
proc stepsql {dbptr sql} {
|
|
|
|
set sql [string trim $sql]
|
|
|
|
set r 0
|
|
|
|
while {[string length $sql]>0} {
|
2004-05-21 05:47:26 +04:00
|
|
|
if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
|
2003-03-01 22:45:34 +03:00
|
|
|
return [list 1 $vm]
|
|
|
|
}
|
|
|
|
set sql [string trim $sqltail]
|
2004-06-15 06:44:18 +04:00
|
|
|
# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
|
|
|
|
# foreach v $VAL {lappend r $v}
|
|
|
|
# }
|
|
|
|
while {[sqlite3_step $vm]=="SQLITE_ROW"} {
|
|
|
|
for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
|
|
|
|
lappend r [sqlite3_column_text $vm $i]
|
|
|
|
}
|
2003-03-01 22:45:34 +03:00
|
|
|
}
|
2004-05-21 14:08:53 +04:00
|
|
|
if {[catch {sqlite3_finalize $vm} errmsg]} {
|
2003-03-01 22:45:34 +03:00
|
|
|
return [list 1 $errmsg]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $r
|
|
|
|
}
|
|
|
|
|
2001-01-31 16:28:08 +03:00
|
|
|
# Delete a file or directory
|
|
|
|
#
|
|
|
|
proc forcedelete {filename} {
|
|
|
|
if {[catch {file delete -force $filename}]} {
|
|
|
|
exec rm -rf $filename
|
|
|
|
}
|
|
|
|
}
|
2002-06-25 17:16:02 +04:00
|
|
|
|
|
|
|
# Do an integrity check of the entire database
|
|
|
|
#
|
2009-02-03 19:51:24 +03:00
|
|
|
proc integrity_check {name {db db}} {
|
2004-10-31 00:23:09 +04:00
|
|
|
ifcapable integrityck {
|
2009-02-03 19:51:24 +03:00
|
|
|
do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
|
2004-10-31 00:23:09 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-16 11:24:46 +03:00
|
|
|
proc fix_ifcapable_expr {expr} {
|
|
|
|
set ret ""
|
|
|
|
set state 0
|
|
|
|
for {set i 0} {$i < [string length $expr]} {incr i} {
|
|
|
|
set char [string range $expr $i $i]
|
|
|
|
set newstate [expr {[string is alnum $char] || $char eq "_"}]
|
|
|
|
if {$newstate && !$state} {
|
|
|
|
append ret {$::sqlite_options(}
|
|
|
|
}
|
|
|
|
if {!$newstate && $state} {
|
|
|
|
append ret )
|
|
|
|
}
|
|
|
|
append ret $char
|
|
|
|
set state $newstate
|
|
|
|
}
|
|
|
|
if {$state} {append ret )}
|
|
|
|
return $ret
|
|
|
|
}
|
|
|
|
|
2004-10-31 00:23:09 +04:00
|
|
|
# Evaluate a boolean expression of capabilities. If true, execute the
|
|
|
|
# code. Omit the code if false.
|
|
|
|
#
|
2005-01-21 06:12:14 +03:00
|
|
|
proc ifcapable {expr code {else ""} {elsecode ""}} {
|
2008-01-16 11:24:46 +03:00
|
|
|
#regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
|
|
|
|
set e2 [fix_ifcapable_expr $expr]
|
2005-01-21 06:12:14 +03:00
|
|
|
if ($e2) {
|
|
|
|
set c [catch {uplevel 1 $code} r]
|
|
|
|
} else {
|
|
|
|
set c [catch {uplevel 1 $elsecode} r]
|
|
|
|
}
|
|
|
|
return -code $c $r
|
2002-06-25 17:16:02 +04:00
|
|
|
}
|
2004-11-10 18:27:38 +03:00
|
|
|
|
2005-01-13 14:07:52 +03:00
|
|
|
# This proc execs a seperate process that crashes midway through executing
|
|
|
|
# the SQL script $sql on database test.db.
|
|
|
|
#
|
|
|
|
# The crash occurs during a sync() of file $crashfile. When the crash
|
|
|
|
# occurs a random subset of all unsynced writes made by the process are
|
|
|
|
# written into the files on disk. Argument $crashdelay indicates the
|
|
|
|
# number of file syncs to wait before crashing.
|
|
|
|
#
|
|
|
|
# The return value is a list of two elements. The first element is a
|
|
|
|
# boolean, indicating whether or not the process actually crashed or
|
|
|
|
# reported some other error. The second element in the returned list is the
|
|
|
|
# error message. This is "child process exited abnormally" if the crash
|
|
|
|
# occured.
|
|
|
|
#
|
2007-08-23 15:07:10 +04:00
|
|
|
# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
|
2007-03-17 13:26:59 +03:00
|
|
|
#
|
|
|
|
proc crashsql {args} {
|
2005-01-13 14:07:52 +03:00
|
|
|
if {$::tcl_platform(platform)!="unix"} {
|
|
|
|
error "crashsql should only be used on unix"
|
|
|
|
}
|
2007-03-17 13:26:59 +03:00
|
|
|
|
|
|
|
set blocksize ""
|
|
|
|
set crashdelay 1
|
2008-01-08 18:18:52 +03:00
|
|
|
set prngseed 0
|
2008-01-08 19:03:49 +03:00
|
|
|
set tclbody {}
|
2007-03-17 13:26:59 +03:00
|
|
|
set crashfile ""
|
2007-08-23 15:07:10 +04:00
|
|
|
set dc ""
|
2007-03-17 13:26:59 +03:00
|
|
|
set sql [lindex $args end]
|
|
|
|
|
|
|
|
for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
|
|
|
|
set z [lindex $args $ii]
|
|
|
|
set n [string length $z]
|
|
|
|
set z2 [lindex $args [expr $ii+1]]
|
|
|
|
|
|
|
|
if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
|
2008-01-08 18:18:52 +03:00
|
|
|
elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
|
2007-03-17 13:26:59 +03:00
|
|
|
elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
|
2008-01-08 19:03:49 +03:00
|
|
|
elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
|
2007-08-20 18:23:44 +04:00
|
|
|
elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
|
2007-08-23 15:07:10 +04:00
|
|
|
elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
|
2007-03-17 13:26:59 +03:00
|
|
|
else { error "Unrecognized option: $z" }
|
|
|
|
}
|
|
|
|
|
|
|
|
if {$crashfile eq ""} {
|
|
|
|
error "Compulsory option -file missing"
|
|
|
|
}
|
|
|
|
|
2005-01-13 14:07:52 +03:00
|
|
|
set cfile [file join [pwd] $crashfile]
|
|
|
|
|
|
|
|
set f [open crash.tcl w]
|
2007-09-01 13:02:53 +04:00
|
|
|
puts $f "sqlite3_crash_enable 1"
|
2007-08-23 15:07:10 +04:00
|
|
|
puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
|
2009-02-05 19:31:45 +03:00
|
|
|
puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
|
2007-09-01 10:51:27 +04:00
|
|
|
puts $f "sqlite3 db test.db -vfs crash"
|
2007-03-17 10:22:42 +03:00
|
|
|
|
|
|
|
# This block sets the cache size of the main database to 10
|
|
|
|
# pages. This is done in case the build is configured to omit
|
|
|
|
# "PRAGMA cache_size".
|
|
|
|
puts $f {db eval {SELECT * FROM sqlite_master;}}
|
|
|
|
puts $f {set bt [btree_from_db db]}
|
|
|
|
puts $f {btree_set_cache_size $bt 10}
|
2008-01-08 18:18:52 +03:00
|
|
|
if {$prngseed} {
|
|
|
|
set seed [expr {$prngseed%10007+1}]
|
|
|
|
# puts seed=$seed
|
|
|
|
puts $f "db eval {SELECT randomblob($seed)}"
|
|
|
|
}
|
2007-03-17 10:22:42 +03:00
|
|
|
|
2008-01-08 19:03:49 +03:00
|
|
|
if {[string length $tclbody]>0} {
|
|
|
|
puts $f $tclbody
|
|
|
|
}
|
|
|
|
if {[string length $sql]>0} {
|
|
|
|
puts $f "db eval {"
|
|
|
|
puts $f "$sql"
|
|
|
|
puts $f "}"
|
|
|
|
}
|
2005-01-13 14:07:52 +03:00
|
|
|
close $f
|
|
|
|
|
|
|
|
set r [catch {
|
2005-11-26 03:25:00 +03:00
|
|
|
exec [info nameofexec] crash.tcl >@stdout
|
2005-01-13 14:07:52 +03:00
|
|
|
} msg]
|
|
|
|
lappend r $msg
|
|
|
|
}
|
|
|
|
|
2005-01-22 06:39:39 +03:00
|
|
|
# Usage: do_ioerr_test <test number> <options...>
|
|
|
|
#
|
|
|
|
# This proc is used to implement test cases that check that IO errors
|
|
|
|
# are correctly handled. The first argument, <test number>, is an integer
|
|
|
|
# used to name the tests executed by this proc. Options are as follows:
|
|
|
|
#
|
|
|
|
# -tclprep TCL script to run to prepare test.
|
|
|
|
# -sqlprep SQL script to run to prepare test.
|
|
|
|
# -tclbody TCL script to run with IO error simulation.
|
|
|
|
# -sqlbody TCL script to run with IO error simulation.
|
|
|
|
# -exclude List of 'N' values not to test.
|
2006-09-15 11:28:50 +04:00
|
|
|
# -erc Use extended result codes
|
2007-03-15 15:17:42 +03:00
|
|
|
# -persist Make simulated I/O errors persistent
|
2005-01-22 06:39:39 +03:00
|
|
|
# -start Value of 'N' to begin with (default 1)
|
|
|
|
#
|
|
|
|
# -cksum Boolean. If true, test that the database does
|
|
|
|
# not change during the execution of the test case.
|
|
|
|
#
|
|
|
|
proc do_ioerr_test {testname args} {
|
|
|
|
|
|
|
|
set ::ioerropts(-start) 1
|
|
|
|
set ::ioerropts(-cksum) 0
|
2006-09-15 11:28:50 +04:00
|
|
|
set ::ioerropts(-erc) 0
|
2007-01-04 17:58:14 +03:00
|
|
|
set ::ioerropts(-count) 100000000
|
2007-03-15 15:17:42 +03:00
|
|
|
set ::ioerropts(-persist) 1
|
2008-05-05 19:26:50 +04:00
|
|
|
set ::ioerropts(-ckrefcount) 0
|
2008-06-05 15:39:11 +04:00
|
|
|
set ::ioerropts(-restoreprng) 1
|
2005-01-22 06:39:39 +03:00
|
|
|
array set ::ioerropts $args
|
|
|
|
|
2008-05-12 16:41:15 +04:00
|
|
|
# TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
|
|
|
|
# a couple of obscure IO errors that do not return them.
|
|
|
|
set ::ioerropts(-erc) 0
|
|
|
|
|
2005-01-22 06:39:39 +03:00
|
|
|
set ::go 1
|
2008-01-16 20:46:38 +03:00
|
|
|
#reset_prng_state
|
|
|
|
save_prng_state
|
2009-04-06 21:50:03 +04:00
|
|
|
for {set n $::ioerropts(-start)} {$::go} {incr n} {
|
2007-05-04 16:05:56 +04:00
|
|
|
set ::TN $n
|
2007-01-04 17:58:14 +03:00
|
|
|
incr ::ioerropts(-count) -1
|
|
|
|
if {$::ioerropts(-count)<0} break
|
2005-01-22 06:39:39 +03:00
|
|
|
|
|
|
|
# Skip this IO error if it was specified with the "-exclude" option.
|
|
|
|
if {[info exists ::ioerropts(-exclude)]} {
|
|
|
|
if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
|
|
|
|
}
|
2008-06-05 15:39:11 +04:00
|
|
|
if {$::ioerropts(-restoreprng)} {
|
|
|
|
restore_prng_state
|
|
|
|
}
|
2005-01-22 06:39:39 +03:00
|
|
|
|
|
|
|
# Delete the files test.db and test2.db, then execute the TCL and
|
|
|
|
# SQL (in that order) to prepare for the test case.
|
|
|
|
do_test $testname.$n.1 {
|
|
|
|
set ::sqlite_io_error_pending 0
|
|
|
|
catch {db close}
|
2008-11-21 03:10:35 +03:00
|
|
|
catch {db2 close}
|
2005-01-22 06:39:39 +03:00
|
|
|
catch {file delete -force test.db}
|
|
|
|
catch {file delete -force test.db-journal}
|
|
|
|
catch {file delete -force test2.db}
|
|
|
|
catch {file delete -force test2.db-journal}
|
2006-01-07 01:11:20 +03:00
|
|
|
set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
|
2006-09-15 11:28:50 +04:00
|
|
|
sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
|
2005-01-22 06:39:39 +03:00
|
|
|
if {[info exists ::ioerropts(-tclprep)]} {
|
|
|
|
eval $::ioerropts(-tclprep)
|
|
|
|
}
|
|
|
|
if {[info exists ::ioerropts(-sqlprep)]} {
|
|
|
|
execsql $::ioerropts(-sqlprep)
|
|
|
|
}
|
|
|
|
expr 0
|
|
|
|
} {0}
|
|
|
|
|
|
|
|
# Read the 'checksum' of the database.
|
|
|
|
if {$::ioerropts(-cksum)} {
|
|
|
|
set checksum [cksum]
|
|
|
|
}
|
2008-01-16 20:46:38 +03:00
|
|
|
|
2005-01-22 06:39:39 +03:00
|
|
|
# Set the Nth IO error to fail.
|
|
|
|
do_test $testname.$n.2 [subst {
|
2007-03-15 15:17:42 +03:00
|
|
|
set ::sqlite_io_error_persist $::ioerropts(-persist)
|
2005-01-22 06:39:39 +03:00
|
|
|
set ::sqlite_io_error_pending $n
|
|
|
|
}] $n
|
|
|
|
|
|
|
|
# Create a single TCL script from the TCL and SQL specified
|
|
|
|
# as the body of the test.
|
|
|
|
set ::ioerrorbody {}
|
|
|
|
if {[info exists ::ioerropts(-tclbody)]} {
|
|
|
|
append ::ioerrorbody "$::ioerropts(-tclbody)\n"
|
|
|
|
}
|
|
|
|
if {[info exists ::ioerropts(-sqlbody)]} {
|
|
|
|
append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Execute the TCL Script created in the above block. If
|
|
|
|
# there are at least N IO operations performed by SQLite as
|
|
|
|
# a result of the script, the Nth will fail.
|
|
|
|
do_test $testname.$n.3 {
|
2008-03-07 22:51:14 +03:00
|
|
|
set ::sqlite_io_error_hit 0
|
|
|
|
set ::sqlite_io_error_hardhit 0
|
2005-01-22 06:39:39 +03:00
|
|
|
set r [catch $::ioerrorbody msg]
|
2008-03-07 22:51:14 +03:00
|
|
|
set ::errseen $r
|
2006-09-15 16:29:16 +04:00
|
|
|
set rc [sqlite3_errcode $::DB]
|
|
|
|
if {$::ioerropts(-erc)} {
|
2007-04-19 16:30:54 +04:00
|
|
|
# If we are in extended result code mode, make sure all of the
|
|
|
|
# IOERRs we get back really do have their extended code values.
|
|
|
|
# If an extended result code is returned, the sqlite3_errcode
|
|
|
|
# TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
|
|
|
|
# where nnnn is a number
|
2006-09-15 16:29:16 +04:00
|
|
|
if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
|
|
|
|
return $rc
|
|
|
|
}
|
|
|
|
} else {
|
2007-04-19 16:30:54 +04:00
|
|
|
# If we are not in extended result code mode, make sure no
|
|
|
|
# extended error codes are returned.
|
2006-09-15 16:29:16 +04:00
|
|
|
if {[regexp {\+\d} $rc]} {
|
|
|
|
return $rc
|
|
|
|
}
|
|
|
|
}
|
2008-01-16 20:46:38 +03:00
|
|
|
# The test repeats as long as $::go is non-zero. $::go starts out
|
|
|
|
# as 1. When a test runs to completion without hitting an I/O
|
|
|
|
# error, that means there is no point in continuing with this test
|
|
|
|
# case so set $::go to zero.
|
|
|
|
#
|
|
|
|
if {$::sqlite_io_error_pending>0} {
|
|
|
|
set ::go 0
|
|
|
|
set q 0
|
|
|
|
set ::sqlite_io_error_pending 0
|
|
|
|
} else {
|
|
|
|
set q 1
|
|
|
|
}
|
|
|
|
|
2005-11-05 01:03:30 +03:00
|
|
|
set s [expr $::sqlite_io_error_hit==0]
|
2008-03-07 22:51:14 +03:00
|
|
|
if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
|
|
|
|
set r 1
|
|
|
|
}
|
2006-01-24 16:09:33 +03:00
|
|
|
set ::sqlite_io_error_hit 0
|
2007-04-19 16:30:54 +04:00
|
|
|
|
|
|
|
# One of two things must have happened. either
|
|
|
|
# 1. We never hit the IO error and the SQL returned OK
|
|
|
|
# 2. An IO error was hit and the SQL failed
|
|
|
|
#
|
2008-01-16 20:46:38 +03:00
|
|
|
expr { ($s && !$r && !$q) || (!$s && $r && $q) }
|
2005-01-22 06:39:39 +03:00
|
|
|
} {1}
|
|
|
|
|
2008-05-12 14:57:02 +04:00
|
|
|
set ::sqlite_io_error_hit 0
|
|
|
|
set ::sqlite_io_error_pending 0
|
|
|
|
|
2008-05-05 20:23:55 +04:00
|
|
|
# Check that no page references were leaked. There should be
|
|
|
|
# a single reference if there is still an active transaction,
|
|
|
|
# or zero otherwise.
|
|
|
|
#
|
2008-06-07 09:19:37 +04:00
|
|
|
# UPDATE: If the IO error occurs after a 'BEGIN' but before any
|
|
|
|
# locks are established on database files (i.e. if the error
|
|
|
|
# occurs while attempting to detect a hot-journal file), then
|
|
|
|
# there may 0 page references and an active transaction according
|
|
|
|
# to [sqlite3_get_autocommit].
|
|
|
|
#
|
2008-05-05 19:26:50 +04:00
|
|
|
if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
|
|
|
|
do_test $testname.$n.4 {
|
|
|
|
set bt [btree_from_db db]
|
|
|
|
db_enter db
|
|
|
|
array set stats [btree_pager_stats $bt]
|
|
|
|
db_leave db
|
2008-06-07 09:19:37 +04:00
|
|
|
set nRef $stats(ref)
|
|
|
|
expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
|
|
|
|
} {1}
|
2008-05-05 19:26:50 +04:00
|
|
|
}
|
|
|
|
|
2008-05-05 20:23:55 +04:00
|
|
|
# If there is an open database handle and no open transaction,
|
|
|
|
# and the pager is not running in exclusive-locking mode,
|
|
|
|
# check that the pager is in "unlocked" state. Theoretically,
|
|
|
|
# if a call to xUnlock() failed due to an IO error the underlying
|
|
|
|
# file may still be locked.
|
|
|
|
#
|
|
|
|
ifcapable pragma {
|
|
|
|
if { [info commands db] ne ""
|
2008-05-05 21:14:53 +04:00
|
|
|
&& $::ioerropts(-ckrefcount)
|
2008-05-05 20:23:55 +04:00
|
|
|
&& [db one {pragma locking_mode}] eq "normal"
|
|
|
|
&& [sqlite3_get_autocommit db]
|
|
|
|
} {
|
|
|
|
do_test $testname.$n.5 {
|
|
|
|
set bt [btree_from_db db]
|
|
|
|
db_enter db
|
|
|
|
array set stats [btree_pager_stats $bt]
|
|
|
|
db_leave db
|
|
|
|
set stats(state)
|
|
|
|
} 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-22 06:39:39 +03:00
|
|
|
# If an IO error occured, then the checksum of the database should
|
|
|
|
# be the same as before the script that caused the IO error was run.
|
2008-05-05 20:23:55 +04:00
|
|
|
#
|
2008-03-07 22:51:14 +03:00
|
|
|
if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
|
2008-05-05 21:14:53 +04:00
|
|
|
do_test $testname.$n.6 {
|
2005-01-22 06:39:39 +03:00
|
|
|
catch {db close}
|
2008-07-08 16:07:32 +04:00
|
|
|
catch {db2 close}
|
2006-01-07 01:11:20 +03:00
|
|
|
set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
|
2005-01-22 06:39:39 +03:00
|
|
|
cksum
|
|
|
|
} $checksum
|
|
|
|
}
|
|
|
|
|
2008-03-07 22:51:14 +03:00
|
|
|
set ::sqlite_io_error_hardhit 0
|
2006-01-21 15:08:54 +03:00
|
|
|
set ::sqlite_io_error_pending 0
|
2005-05-26 19:20:53 +04:00
|
|
|
if {[info exists ::ioerropts(-cleanup)]} {
|
|
|
|
catch $::ioerropts(-cleanup)
|
|
|
|
}
|
2005-01-22 06:39:39 +03:00
|
|
|
}
|
|
|
|
set ::sqlite_io_error_pending 0
|
2007-03-25 23:08:46 +04:00
|
|
|
set ::sqlite_io_error_persist 0
|
2005-01-22 06:39:39 +03:00
|
|
|
unset ::ioerropts
|
|
|
|
}
|
|
|
|
|
2008-01-16 20:46:38 +03:00
|
|
|
# Return a checksum based on the contents of the main database associated
|
|
|
|
# with connection $db
|
2005-01-22 06:39:39 +03:00
|
|
|
#
|
|
|
|
proc cksum {{db db}} {
|
|
|
|
set txt [$db eval {
|
|
|
|
SELECT name, type, sql FROM sqlite_master order by name
|
|
|
|
}]\n
|
|
|
|
foreach tbl [$db eval {
|
|
|
|
SELECT name FROM sqlite_master WHERE type='table' order by name
|
|
|
|
}] {
|
|
|
|
append txt [$db eval "SELECT * FROM $tbl"]\n
|
|
|
|
}
|
|
|
|
foreach prag {default_synchronous default_cache_size} {
|
|
|
|
append txt $prag-[$db eval "PRAGMA $prag"]\n
|
|
|
|
}
|
|
|
|
set cksum [string length $txt]-[md5 $txt]
|
|
|
|
# puts $cksum-[file size test.db]
|
|
|
|
return $cksum
|
|
|
|
}
|
|
|
|
|
2008-01-16 20:46:38 +03:00
|
|
|
# Generate a checksum based on the contents of the main and temp tables
|
|
|
|
# database $db. If the checksum of two databases is the same, and the
|
|
|
|
# integrity-check passes for both, the two databases are identical.
|
|
|
|
#
|
2008-01-17 05:36:28 +03:00
|
|
|
proc allcksum {{db db}} {
|
2008-01-16 20:46:38 +03:00
|
|
|
set ret [list]
|
|
|
|
ifcapable tempdb {
|
|
|
|
set sql {
|
|
|
|
SELECT name FROM sqlite_master WHERE type = 'table' UNION
|
|
|
|
SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
|
|
|
|
SELECT 'sqlite_master' UNION
|
|
|
|
SELECT 'sqlite_temp_master' ORDER BY 1
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
set sql {
|
|
|
|
SELECT name FROM sqlite_master WHERE type = 'table' UNION
|
|
|
|
SELECT 'sqlite_master' ORDER BY 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set tbllist [$db eval $sql]
|
|
|
|
set txt {}
|
|
|
|
foreach tbl $tbllist {
|
|
|
|
append txt [$db eval "SELECT * FROM $tbl"]
|
|
|
|
}
|
|
|
|
foreach prag {default_cache_size} {
|
|
|
|
append txt $prag-[$db eval "PRAGMA $prag"]\n
|
|
|
|
}
|
|
|
|
# puts txt=$txt
|
|
|
|
return [md5 $txt]
|
2009-02-05 01:46:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Generate a checksum based on the contents of a single database with
|
|
|
|
# a database connection. The name of the database is $dbname.
|
|
|
|
# Examples of $dbname are "temp" or "main".
|
|
|
|
#
|
|
|
|
proc dbcksum {db dbname} {
|
|
|
|
if {$dbname=="temp"} {
|
|
|
|
set master sqlite_temp_master
|
|
|
|
} else {
|
|
|
|
set master $dbname.sqlite_master
|
|
|
|
}
|
|
|
|
set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
|
|
|
|
set txt [$db eval "SELECT * FROM $master"]\n
|
|
|
|
foreach tab $alltab {
|
|
|
|
append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
|
|
|
|
}
|
|
|
|
return [md5 $txt]
|
2008-01-16 20:46:38 +03:00
|
|
|
}
|
|
|
|
|
2008-03-21 20:29:37 +03:00
|
|
|
proc memdebug_log_sql {{filename mallocs.sql}} {
|
|
|
|
|
2008-03-21 17:22:44 +03:00
|
|
|
set data [sqlite3_memdebug_log dump]
|
|
|
|
set nFrame [expr [llength [lindex $data 0]]-2]
|
|
|
|
if {$nFrame < 0} { return "" }
|
|
|
|
|
2008-03-21 20:29:37 +03:00
|
|
|
set database temp
|
|
|
|
|
2009-02-19 17:39:25 +03:00
|
|
|
set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
|
2008-03-21 17:22:44 +03:00
|
|
|
|
|
|
|
set sql ""
|
|
|
|
foreach e $data {
|
2009-02-19 17:39:25 +03:00
|
|
|
set nCall [lindex $e 0]
|
|
|
|
set nByte [lindex $e 1]
|
|
|
|
set lStack [lrange $e 2 end]
|
|
|
|
append sql "INSERT INTO ${database}.malloc VALUES"
|
|
|
|
append sql "('test', $nCall, $nByte, '$lStack');\n"
|
|
|
|
foreach f $lStack {
|
2008-03-21 17:22:44 +03:00
|
|
|
set frames($f) 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
|
2008-03-21 20:29:37 +03:00
|
|
|
set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
|
2008-03-21 17:22:44 +03:00
|
|
|
|
|
|
|
foreach f [array names frames] {
|
|
|
|
set addr [format %x $f]
|
|
|
|
set cmd "addr2line -e [info nameofexec] $addr"
|
|
|
|
set line [eval exec $cmd]
|
|
|
|
append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
|
|
|
|
|
2008-03-21 20:29:37 +03:00
|
|
|
set file [lindex [split $line :] 0]
|
|
|
|
set files($file) 1
|
2008-03-21 17:22:44 +03:00
|
|
|
}
|
|
|
|
|
2008-03-21 20:29:37 +03:00
|
|
|
foreach f [array names files] {
|
|
|
|
set contents ""
|
|
|
|
catch {
|
|
|
|
set fd [open $f]
|
|
|
|
set contents [read $fd]
|
|
|
|
close $fd
|
2008-03-21 17:22:44 +03:00
|
|
|
}
|
2008-03-21 20:29:37 +03:00
|
|
|
set contents [string map {' ''} $contents]
|
|
|
|
append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
|
2008-03-21 17:22:44 +03:00
|
|
|
}
|
|
|
|
|
2008-03-21 20:29:37 +03:00
|
|
|
set fd [open $filename w]
|
|
|
|
puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
|
|
|
|
close $fd
|
2008-03-21 17:22:44 +03:00
|
|
|
}
|
2008-01-16 20:46:38 +03:00
|
|
|
|
2005-01-22 06:39:39 +03:00
|
|
|
# Copy file $from into $to. This is used because some versions of
|
|
|
|
# TCL for windows (notably the 8.4.1 binary package shipped with the
|
|
|
|
# current mingw release) have a broken "file copy" command.
|
|
|
|
#
|
|
|
|
proc copy_file {from to} {
|
|
|
|
if {$::tcl_platform(platform)=="unix"} {
|
|
|
|
file copy -force $from $to
|
|
|
|
} else {
|
|
|
|
set f [open $from]
|
|
|
|
fconfigure $f -translation binary
|
|
|
|
set t [open $to w]
|
|
|
|
fconfigure $t -translation binary
|
|
|
|
puts -nonewline $t [read $f [file size $from]]
|
|
|
|
close $t
|
|
|
|
close $f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-07 22:41:19 +04:00
|
|
|
# Drop all tables in database [db]
|
|
|
|
proc drop_all_tables {{db db}} {
|
|
|
|
set pk [$db one "PRAGMA foreign_keys"]
|
|
|
|
$db eval "PRAGMA foreign_keys = OFF"
|
|
|
|
foreach {t type} [$db eval {
|
|
|
|
SELECT name, type FROM sqlite_master
|
|
|
|
WHERE type IN('table', 'view') AND name NOT like 'sqlite_%'
|
|
|
|
}] {
|
|
|
|
$db eval "DROP $type $t"
|
|
|
|
}
|
|
|
|
$db eval " PRAGMA foreign_keys = $pk "
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-10 18:27:38 +03:00
|
|
|
# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
|
|
|
|
# to non-zero, then set the global variable $AUTOVACUUM to 1.
|
|
|
|
set AUTOVACUUM $sqlite_options(default_autovacuum)
|
2009-03-26 20:13:06 +03:00
|
|
|
|
|
|
|
source $testdir/thread_common.tcl
|