os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/progress.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
# 2001 September 15
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#***********************************************************************
sl@0
    11
# This file implements regression tests for SQLite library.  The
sl@0
    12
# focus of this file is testing the 'progress callback'.
sl@0
    13
#
sl@0
    14
# $Id: progress.test,v 1.8 2007/06/15 14:53:53 danielk1977 Exp $
sl@0
    15
sl@0
    16
set testdir [file dirname $argv0]
sl@0
    17
source $testdir/tester.tcl
sl@0
    18
sl@0
    19
# If the progress callback is not available in this build, skip this
sl@0
    20
# whole file.
sl@0
    21
ifcapable !progress {
sl@0
    22
  finish_test
sl@0
    23
  return
sl@0
    24
}
sl@0
    25
sl@0
    26
# Build some test data
sl@0
    27
#
sl@0
    28
execsql {
sl@0
    29
  BEGIN;
sl@0
    30
  CREATE TABLE t1(a);
sl@0
    31
  INSERT INTO t1 VALUES(1);
sl@0
    32
  INSERT INTO t1 VALUES(2);
sl@0
    33
  INSERT INTO t1 VALUES(3);
sl@0
    34
  INSERT INTO t1 VALUES(4);
sl@0
    35
  INSERT INTO t1 VALUES(5);
sl@0
    36
  INSERT INTO t1 VALUES(6);
sl@0
    37
  INSERT INTO t1 VALUES(7);
sl@0
    38
  INSERT INTO t1 VALUES(8);
sl@0
    39
  INSERT INTO t1 VALUES(9);
sl@0
    40
  INSERT INTO t1 VALUES(10);
sl@0
    41
  COMMIT;
sl@0
    42
}
sl@0
    43
sl@0
    44
sl@0
    45
# Test that the progress callback is invoked.
sl@0
    46
do_test progress-1.0 {
sl@0
    47
  set counter 0
sl@0
    48
  db progress 1 "[namespace code {incr counter}] ; expr 0"
sl@0
    49
  execsql {
sl@0
    50
    SELECT * FROM t1
sl@0
    51
  }
sl@0
    52
  expr $counter > 1
sl@0
    53
} 1
sl@0
    54
do_test progress-1.0.1 {
sl@0
    55
  db progress
sl@0
    56
} {::namespace inscope :: {incr counter} ; expr 0}
sl@0
    57
do_test progress-1.0.2 {
sl@0
    58
  set v [catch {db progress xyz bogus} msg]
sl@0
    59
  lappend v $msg
sl@0
    60
} {1 {expected integer but got "xyz"}}
sl@0
    61
sl@0
    62
# Test that the query is abandoned when the progress callback returns non-zero
sl@0
    63
do_test progress-1.1 {
sl@0
    64
  set counter 0
sl@0
    65
  db progress 1 "[namespace code {incr counter}] ; expr 1"
sl@0
    66
  set rc [catch {execsql {
sl@0
    67
    SELECT * FROM t1
sl@0
    68
  }}]
sl@0
    69
  list $counter $rc
sl@0
    70
} {1 1}
sl@0
    71
sl@0
    72
# Test that the query is rolled back when the progress callback returns
sl@0
    73
# non-zero.
sl@0
    74
do_test progress-1.2 {
sl@0
    75
sl@0
    76
  # This figures out how many opcodes it takes to copy 5 extra rows into t1.
sl@0
    77
  db progress 1 "[namespace code {incr five_rows}] ; expr 0"
sl@0
    78
  set five_rows 0
sl@0
    79
  execsql {
sl@0
    80
    INSERT INTO t1 SELECT a+10 FROM t1 WHERE a < 6
sl@0
    81
  }
sl@0
    82
  db progress 0 ""
sl@0
    83
  execsql {
sl@0
    84
    DELETE FROM t1 WHERE a > 10
sl@0
    85
  }
sl@0
    86
sl@0
    87
  # Now set up the progress callback to abandon the query after the number of
sl@0
    88
  # opcodes to copy 5 rows. That way, when we try to copy 6 rows, we know
sl@0
    89
  # some data will have been inserted into the table by the time the progress
sl@0
    90
  # callback abandons the query.
sl@0
    91
  db progress $five_rows "expr 1"
sl@0
    92
  catchsql {
sl@0
    93
    INSERT INTO t1 SELECT a+10 FROM t1 WHERE a < 9
sl@0
    94
  }
sl@0
    95
  execsql {
sl@0
    96
    SELECT count(*) FROM t1
sl@0
    97
  }
sl@0
    98
} 10
sl@0
    99
sl@0
   100
# Test that an active transaction remains active and not rolled back 
sl@0
   101
# after the progress query abandons a query. 
sl@0
   102
#
sl@0
   103
# UPDATE: It is now recognised that this is a sure route to database
sl@0
   104
# corruption. So the transaction is rolled back.
sl@0
   105
do_test progress-1.3 {
sl@0
   106
sl@0
   107
  db progress 0 ""
sl@0
   108
  execsql BEGIN
sl@0
   109
  execsql {
sl@0
   110
    INSERT INTO t1 VALUES(11)
sl@0
   111
  }
sl@0
   112
  db progress 1 "expr 1"
sl@0
   113
  catchsql {
sl@0
   114
    INSERT INTO t1 VALUES(12)
sl@0
   115
  }
sl@0
   116
  db progress 0 ""
sl@0
   117
  catchsql COMMIT
sl@0
   118
} {1 {cannot commit - no transaction is active}}
sl@0
   119
do_test progress-1.3.1 {
sl@0
   120
  execsql {
sl@0
   121
    SELECT count(*) FROM t1
sl@0
   122
  }
sl@0
   123
} 10
sl@0
   124
sl@0
   125
# Check that a value of 0 for N means no progress callback
sl@0
   126
do_test progress-1.4 {
sl@0
   127
  set counter 0
sl@0
   128
  db progress 0 "[namespace code {incr counter}] ; expr 0"
sl@0
   129
  execsql {
sl@0
   130
    SELECT * FROM t1;
sl@0
   131
  }
sl@0
   132
  set counter
sl@0
   133
} 0
sl@0
   134
sl@0
   135
db progress 0 ""
sl@0
   136
sl@0
   137
# Make sure other queries can be run from within the progress
sl@0
   138
# handler.  Ticket #1827
sl@0
   139
#
sl@0
   140
do_test progress-1.5 {
sl@0
   141
  set rx 0
sl@0
   142
  proc set_rx {args} {
sl@0
   143
    db progress 0 {}
sl@0
   144
    set ::rx [db eval {SELECT count(*) FROM t1}]
sl@0
   145
    return [expr 0]
sl@0
   146
  }
sl@0
   147
  db progress 10 set_rx
sl@0
   148
  db eval {
sl@0
   149
    SELECT sum(a) FROM t1
sl@0
   150
  }
sl@0
   151
} {55}
sl@0
   152
do_test progress-1.6 {
sl@0
   153
  set ::rx
sl@0
   154
} {10}
sl@0
   155
sl@0
   156
# Check that abandoning a query using the progress handler does
sl@0
   157
# not cause other queries to abort. Ticket #2415.
sl@0
   158
do_test progress-1.7 {
sl@0
   159
  execsql {
sl@0
   160
    CREATE TABLE abc(a, b, c);
sl@0
   161
    INSERT INTO abc VALUES(1, 2, 3);
sl@0
   162
    INSERT INTO abc VALUES(4, 5, 6);
sl@0
   163
    INSERT INTO abc VALUES(7, 8, 9);
sl@0
   164
  }
sl@0
   165
sl@0
   166
  set ::res [list]
sl@0
   167
  db eval {SELECT a, b, c FROM abc} {
sl@0
   168
    lappend ::res $a $b $c
sl@0
   169
    db progress 10 "expr 1"
sl@0
   170
    catch {db eval {SELECT a, b, c FROM abc} { }} msg
sl@0
   171
    lappend ::res $msg
sl@0
   172
  }
sl@0
   173
sl@0
   174
  set ::res
sl@0
   175
} {1 2 3 interrupted 4 5 6 interrupted 7 8 9 interrupted}
sl@0
   176
sl@0
   177
finish_test