# 2001 September 15
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    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.
#
#***********************************************************************
# This file implements regression tests for SQLite library. The focus
# of this file is modifications made to tables while SELECT queries are
# active on the tables. Using this capability in a program is tricky
# because results can be difficult to predict, but can be useful.
#
# $Id: lock4.test,v 1.1 2004/11/17 16:41:29 danielk1977 Exp $
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl

do_test lock4-1.0 {
  execsql {
    CREATE TABLE t1(a, b);
    INSERT INTO t1 VALUES(1, 2);
  }
} {}

# Check that we can INSERT into a table while doing a SELECT on it.
do_test lock4-1.1 {
  db eval {SELECT * FROM t1} {
    if {$a<5} {
      execsql "INSERT INTO t1 VALUES($a+1, ($a+1)*2)"
    }
  }
} {}
do_test lock4-1.2 {
  execsql {
    SELECT * FROM t1
  }
} {1 2 2 4 3 6 4 8 5 10}

# Check that we can UPDATE a table while doing a SELECT on it.
do_test lock4-1.3 {
  db eval {SELECT * FROM t1 WHERE (a%2)=0} {
    execsql "UPDATE t1 SET b = b/2 WHERE a = $a"
  }
} {}
do_test lock4-1.4 {
  execsql {
    SELECT * FROM t1
  }
} {1 2 2 2 3 6 4 4 5 10}

# Check that we can DELETE from a table while doing a SELECT on it.
do_test lock4-1.5 {
  db eval {SELECT * FROM t1 WHERE (a%2)=0} {
    execsql "DELETE FROM t1 WHERE a = $a"
  }
} {}
do_test lock4-1.6 {
  execsql {
    SELECT * FROM t1
  }
} {1 2 3 6 5 10}

# Check what happens when a row is deleted while a cursor is still using
# the row (because of a SELECT that does a join).
do_test lock4-2.0 {
  execsql {
    CREATE TABLE t2(c);
    INSERT INTO t2 VALUES('one');
    INSERT INTO t2 VALUES('two');
  }
} {}
do_test lock4-2.1 {
  set res [list]
  db eval {SELECT a, b, c FROM t1, t2} {
    lappend res $a $b $c
    if {0==[string compare $c one]} {
      execsql "DELETE FROM t1 WHERE a = $a"
    }
  }
  set res
} {1 2 one 1 2 two 3 6 one 3 6 two 5 10 one 5 10 two}
do_test lock4-2.2 {
  execsql {
    SELECT * FROM t1;
  }
} {}

# do_test lock4-2.3 {
#   execsql "
#     INSERT INTO t1 VALUES('[string repeat 1 750]', '[string repeat 2 750]')
#   "
# } {}
# do_test lock4-2.4 {
#   set res [list]
#   db eval {SELECT a, b, c FROM t1, t2} {
#     lappend res $a $b $c
#     if {0==[string compare $c one]} {
#       execsql "DELETE FROM t1 WHERE a = '$a'"
#     }
#   }
#   set res
# } [list \
#     [string repeat 1 750] [string repeat 2 750] one \
#     [string repeat 1 750] [string repeat 2 750] two
#   ]
# do_test lock4-2.5 {
#   execsql {
#     SELECT * FROM t1;
#   }
# } {}

finish_test

