1# 2009 July 2
2#
3# The author disclaims copyright to this source code.  In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
11#
12# $Id: sharedlock.test,v 1.1 2009/07/02 17:21:58 danielk1977 Exp $
13
14set testdir [file dirname $argv0]
15source $testdir/tester.tcl
16set testprefix sharedlock
17db close
18
19ifcapable !shared_cache {
20  finish_test
21  return
22}
23
24set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
25sqlite3 db  test.db
26sqlite3 db2 test.db
27
28do_test sharedlock-1.1 {
29  execsql {
30    CREATE TABLE t1(a, b);
31    INSERT INTO t1 VALUES(1, 'one');
32    INSERT INTO t1 VALUES(2, 'two');
33  }
34} {}
35
36do_test sharedlock-1.2 {
37  set res [list]
38  db eval { SELECT * FROM t1 ORDER BY rowid } {
39    lappend res $a $b
40    if {$a == 1} { catch { db  eval "INSERT INTO t1 VALUES(3, 'three')" } }
41
42    # This should fail. Connection [db] has a read-lock on t1, which should
43    # prevent connection [db2] from obtaining the write-lock it needs to
44    # modify t1. At one point there was a bug causing the previous INSERT
45    # to drop the read-lock belonging to [db].
46    if {$a == 2} { catch { db2 eval "INSERT INTO t1 VALUES(4, 'four')"  } }
47  }
48  set res
49} {1 one 2 two 3 three}
50
51
52#-------------------------------------------------------------------------
53# Test that a write-lock is taken on a table when its entire contents
54# are deleted using the OP_Clear optimization.
55#
56foreach {tn delete_sql} {
57  1 { DELETE FROM t2 WHERE 1 }
58  2 { DELETE FROM t2 }
59} {
60  do_execsql_test 2.1 {
61    DROP TABLE IF EXISTS t2;
62    CREATE TABLE t2(x, y);
63    INSERT INTO t2 VALUES(1, 2);
64    INSERT INTO t2 VALUES(3, 4);
65  }
66
67  do_test 2.2 { execsql { SELECT * FROM t2 } db2 } {1 2 3 4}
68
69  do_execsql_test 2.3 " BEGIN; $delete_sql; "
70
71  do_test 2.4 {
72    catchsql { SELECT * FROM t2 } db2
73  } {1 {database table is locked: t2}}
74
75  do_execsql_test 2.5 COMMIT
76}
77
78
79db close
80db2 close
81sqlite3_enable_shared_cache $::enable_shared_cache
82finish_test
83