3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The focus
12 # of this test is reading and writing to the database from within a
13 # virtual table xSync() callback.
15 # $Id: vtab7.test,v 1.4 2007/12/04 16:54:53 drh Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
25 # Register the echo module. Code inside the echo module appends elements
26 # to the global tcl list variable ::echo_module whenever SQLite invokes
27 # certain module callbacks. This includes the xSync(), xCommit() and
28 # xRollback() callbacks. For each of these callback, two elements are
29 # appended to ::echo_module, as follows:
31 # Module method Elements appended to ::echo_module
32 # -------------------------------------------------------
33 # xSync() xSync echo($tablename)
34 # xCommit() xCommit echo($tablename)
35 # xRollback() xRollback echo($tablename)
36 # -------------------------------------------------------
38 # In each case, $tablename is replaced by the name of the real table (not
39 # the echo table). By setting up a tcl trace on the ::echo_module variable,
40 # code in this file arranges for a Tcl script to be executed from within
41 # the echo module xSync() callback.
43 register_echo_module [sqlite3_connection_pointer db]
44 trace add variable ::echo_module write echo_module_trace
46 # This Tcl proc is invoked whenever the ::echo_module variable is written.
48 proc echo_module_trace {args} {
49 # Filter out writes to ::echo_module that are not xSync, xCommit or
50 # xRollback callbacks.
51 if {[llength $::echo_module] < 2} return
52 set x [lindex $::echo_module end-1]
53 if {$x ne "xSync" && $x ne "xCommit" && $x ne "xRollback"} return
55 regexp {^echo.(.*).$} [lindex $::echo_module end] dummy tablename
56 # puts "Ladies and gentlemen, an $x on $tablename!"
58 if {[info exists ::callbacks($x,$tablename)]} {
59 eval $::callbacks($x,$tablename)
63 # The following tests, vtab7-1.*, test that the trace callback on
64 # ::echo_module is providing the expected tcl callbacks.
67 CREATE TABLE abc(a, b, c);
68 CREATE VIRTUAL TABLE abc2 USING echo(abc);
73 set ::callbacks(xSync,abc) {incr ::counter}
76 INSERT INTO abc2 VALUES(1, 2, 3);
81 # Write to an existing database table from within an xSync callback.
83 set ::callbacks(xSync,abc) {
84 execsql {INSERT INTO log VALUES('xSync');}
87 CREATE TABLE log(msg);
88 INSERT INTO abc2 VALUES(4, 5, 6);
94 INSERT INTO abc2 VALUES(4, 5, 6);
100 INSERT INTO abc2 VALUES(4, 5, 6);
103 } {xSync xSync xSync}
105 # Create a database table from within xSync callback.
107 set ::callbacks(xSync,abc) {
108 execsql { CREATE TABLE newtab(d, e, f); }
111 INSERT INTO abc2 VALUES(1, 2, 3);
112 SELECT name FROM sqlite_master ORDER BY name;
114 } {abc abc2 log newtab}
116 # Drop a database table from within xSync callback.
117 # This is not allowed. Tables cannot be dropped while
118 # any other statement is active.
121 set ::callbacks(xSync,abc) {
122 set ::rc [catchsql { DROP TABLE newtab }]
125 INSERT INTO abc2 VALUES(1, 2, 3);
126 SELECT name FROM sqlite_master ORDER BY name;
128 } {abc abc2 log newtab}
129 do_test vtab7-2.6.1 {
131 } {1 {database table is locked}}
132 execsql {DROP TABLE newtab}
134 # Write to an attached database from xSync().
138 forcedelete test2.db-journal
140 ATTACH 'test2.db' AS db2;
141 CREATE TABLE db2.stuff(description, shape, color);
143 set ::callbacks(xSync,abc) {
144 execsql { INSERT INTO db2.stuff VALUES('abc', 'square', 'green'); }
147 INSERT INTO abc2 VALUES(1, 2, 3);
153 # UPDATE: The next test passes, but leaks memory. So leave it out.
155 # The following tests test that writing to the database from within
156 # the xCommit callback causes a misuse error.
157 # do_test vtab7-4.1 {
158 # unset -nocomplain ::callbacks(xSync,abc)
159 # set ::callbacks(xCommit,abc) {
160 # execsql { INSERT INTO log VALUES('hello') }
163 # INSERT INTO abc2 VALUES(1, 2, 3);
165 # } {1 {library routine called out of sequence}}
167 # These tests, vtab7-4.*, test that an SQLITE_LOCKED error is returned
168 # if an attempt to write to a virtual module table or create a new
169 # virtual table from within an xSync() callback.
172 CREATE TABLE def(d, e, f);
173 CREATE VIRTUAL TABLE def2 USING echo(def);
175 set ::callbacks(xSync,abc) {
176 set ::error [catchsql { INSERT INTO def2 VALUES(1, 2, 3) }]
179 INSERT INTO abc2 VALUES(1, 2, 3);
182 } {1 {database table is locked}}
184 set ::callbacks(xSync,abc) {
185 set ::error [catchsql { CREATE VIRTUAL TABLE def3 USING echo(def) }]
188 INSERT INTO abc2 VALUES(1, 2, 3);
191 } {1 {database table is locked}}
194 set ::callbacks(xSync,abc) {
195 set ::error [catchsql { DROP TABLE def2 }]
198 INSERT INTO abc2 VALUES(1, 2, 3);
199 SELECT name FROM sqlite_master ORDER BY name;
202 } {1 {database table is locked}}
204 trace remove variable ::echo_module write echo_module_trace
205 unset -nocomplain ::callbacks