sl@0: # 2001 September 15 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. The sl@0: # focus of this file is testing the 'progress callback'. sl@0: # sl@0: # $Id: progress.test,v 1.8 2007/06/15 14:53:53 danielk1977 Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: # If the progress callback is not available in this build, skip this sl@0: # whole file. sl@0: ifcapable !progress { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: # Build some test data sl@0: # sl@0: execsql { sl@0: BEGIN; sl@0: CREATE TABLE t1(a); sl@0: INSERT INTO t1 VALUES(1); sl@0: INSERT INTO t1 VALUES(2); sl@0: INSERT INTO t1 VALUES(3); sl@0: INSERT INTO t1 VALUES(4); sl@0: INSERT INTO t1 VALUES(5); sl@0: INSERT INTO t1 VALUES(6); sl@0: INSERT INTO t1 VALUES(7); sl@0: INSERT INTO t1 VALUES(8); sl@0: INSERT INTO t1 VALUES(9); sl@0: INSERT INTO t1 VALUES(10); sl@0: COMMIT; sl@0: } sl@0: sl@0: sl@0: # Test that the progress callback is invoked. sl@0: do_test progress-1.0 { sl@0: set counter 0 sl@0: db progress 1 "[namespace code {incr counter}] ; expr 0" sl@0: execsql { sl@0: SELECT * FROM t1 sl@0: } sl@0: expr $counter > 1 sl@0: } 1 sl@0: do_test progress-1.0.1 { sl@0: db progress sl@0: } {::namespace inscope :: {incr counter} ; expr 0} sl@0: do_test progress-1.0.2 { sl@0: set v [catch {db progress xyz bogus} msg] sl@0: lappend v $msg sl@0: } {1 {expected integer but got "xyz"}} sl@0: sl@0: # Test that the query is abandoned when the progress callback returns non-zero sl@0: do_test progress-1.1 { sl@0: set counter 0 sl@0: db progress 1 "[namespace code {incr counter}] ; expr 1" sl@0: set rc [catch {execsql { sl@0: SELECT * FROM t1 sl@0: }}] sl@0: list $counter $rc sl@0: } {1 1} sl@0: sl@0: # Test that the query is rolled back when the progress callback returns sl@0: # non-zero. sl@0: do_test progress-1.2 { sl@0: sl@0: # This figures out how many opcodes it takes to copy 5 extra rows into t1. sl@0: db progress 1 "[namespace code {incr five_rows}] ; expr 0" sl@0: set five_rows 0 sl@0: execsql { sl@0: INSERT INTO t1 SELECT a+10 FROM t1 WHERE a < 6 sl@0: } sl@0: db progress 0 "" sl@0: execsql { sl@0: DELETE FROM t1 WHERE a > 10 sl@0: } sl@0: sl@0: # Now set up the progress callback to abandon the query after the number of sl@0: # opcodes to copy 5 rows. That way, when we try to copy 6 rows, we know sl@0: # some data will have been inserted into the table by the time the progress sl@0: # callback abandons the query. sl@0: db progress $five_rows "expr 1" sl@0: catchsql { sl@0: INSERT INTO t1 SELECT a+10 FROM t1 WHERE a < 9 sl@0: } sl@0: execsql { sl@0: SELECT count(*) FROM t1 sl@0: } sl@0: } 10 sl@0: sl@0: # Test that an active transaction remains active and not rolled back sl@0: # after the progress query abandons a query. sl@0: # sl@0: # UPDATE: It is now recognised that this is a sure route to database sl@0: # corruption. So the transaction is rolled back. sl@0: do_test progress-1.3 { sl@0: sl@0: db progress 0 "" sl@0: execsql BEGIN sl@0: execsql { sl@0: INSERT INTO t1 VALUES(11) sl@0: } sl@0: db progress 1 "expr 1" sl@0: catchsql { sl@0: INSERT INTO t1 VALUES(12) sl@0: } sl@0: db progress 0 "" sl@0: catchsql COMMIT sl@0: } {1 {cannot commit - no transaction is active}} sl@0: do_test progress-1.3.1 { sl@0: execsql { sl@0: SELECT count(*) FROM t1 sl@0: } sl@0: } 10 sl@0: sl@0: # Check that a value of 0 for N means no progress callback sl@0: do_test progress-1.4 { sl@0: set counter 0 sl@0: db progress 0 "[namespace code {incr counter}] ; expr 0" sl@0: execsql { sl@0: SELECT * FROM t1; sl@0: } sl@0: set counter sl@0: } 0 sl@0: sl@0: db progress 0 "" sl@0: sl@0: # Make sure other queries can be run from within the progress sl@0: # handler. Ticket #1827 sl@0: # sl@0: do_test progress-1.5 { sl@0: set rx 0 sl@0: proc set_rx {args} { sl@0: db progress 0 {} sl@0: set ::rx [db eval {SELECT count(*) FROM t1}] sl@0: return [expr 0] sl@0: } sl@0: db progress 10 set_rx sl@0: db eval { sl@0: SELECT sum(a) FROM t1 sl@0: } sl@0: } {55} sl@0: do_test progress-1.6 { sl@0: set ::rx sl@0: } {10} sl@0: sl@0: # Check that abandoning a query using the progress handler does sl@0: # not cause other queries to abort. Ticket #2415. sl@0: do_test progress-1.7 { sl@0: execsql { sl@0: CREATE TABLE abc(a, b, c); sl@0: INSERT INTO abc VALUES(1, 2, 3); sl@0: INSERT INTO abc VALUES(4, 5, 6); sl@0: INSERT INTO abc VALUES(7, 8, 9); sl@0: } sl@0: sl@0: set ::res [list] sl@0: db eval {SELECT a, b, c FROM abc} { sl@0: lappend ::res $a $b $c sl@0: db progress 10 "expr 1" sl@0: catch {db eval {SELECT a, b, c FROM abc} { }} msg sl@0: lappend ::res $msg sl@0: } sl@0: sl@0: set ::res sl@0: } {1 2 3 interrupted 4 5 6 interrupted 7 8 9 interrupted} sl@0: sl@0: finish_test