wasCSharpSQLite – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 # 2006 July 25
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 # 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.
14 #
15 # $Id: vtab7.test,v 1.4 2007/12/04 16:54:53 drh Exp $
16  
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
19  
20 ifcapable !vtab {
21 finish_test
22 return
23 }
24  
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:
30 #
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 # -------------------------------------------------------
37 #
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.
42 #
43 register_echo_module [sqlite3_connection_pointer db]
44 #trace add variable ::echo_module write echo_module_trace
45 trace variable ::echo_module w echo_module_trace
46  
47 # This Tcl proc is invoked whenever the ::echo_module variable is written.
48 #
49 proc echo_module_trace {args} {
50 # Filter out writes to ::echo_module that are not xSync, xCommit or
51 # xRollback callbacks.
52 if {[llength $::echo_module] < 2} return
53 set x [lindex $::echo_module end-1]
54 if {$x ne "xSync" && $x ne "xCommit" && $x ne "xRollback"} return
55  
56 regexp {^echo.(.*).$} [lindex $::echo_module end] dummy tablename
57 # puts "Ladies and gentlemen, an $x on $tablename!"
58  
59 if {[info exists ::callbacks($x,$tablename)]} {
60 eval $::callbacks($x,$tablename)
61 }
62 }
63  
64 # The following tests, vtab7-1.*, test that the trace callback on
65 # ::echo_module is providing the expected tcl callbacks.
66 do_test vtab7-1.1 {
67 execsql {
68 CREATE TABLE abc(a, b, c);
69 CREATE VIRTUAL TABLE abc2 USING echo(abc);
70 }
71 } {}
72  
73 do_test vtab7-1.2 {
74 set ::callbacks(xSync,abc) {incr ::counter}
75 set ::counter 0
76 execsql {
77 INSERT INTO abc2 VALUES(1, 2, 3);
78 }
79 set ::counter
80 } {1}
81  
82 # Write to an existing database table from within an xSync callback.
83 do_test vtab7-2.1 {
84 set ::callbacks(xSync,abc) {
85 execsql {INSERT INTO log VALUES('xSync');}
86 }
87 execsql {
88 CREATE TABLE log(msg);
89 INSERT INTO abc2 VALUES(4, 5, 6);
90 SELECT * FROM log;
91 }
92 } {xSync}
93 do_test vtab7-2.3 {
94 execsql {
95 INSERT INTO abc2 VALUES(4, 5, 6);
96 SELECT * FROM log;
97 }
98 } {xSync xSync}
99 do_test vtab7-2.4 {
100 execsql {
101 INSERT INTO abc2 VALUES(4, 5, 6);
102 SELECT * FROM log;
103 }
104 } {xSync xSync xSync}
105  
106 # Create a database table from within xSync callback.
107 do_test vtab7-2.5 {
108 set ::callbacks(xSync,abc) {
109 execsql { CREATE TABLE newtab(d, e, f); }
110 }
111 execsql {
112 INSERT INTO abc2 VALUES(1, 2, 3);
113 SELECT name FROM sqlite_master ORDER BY name;
114 }
115 } {abc abc2 log newtab}
116  
117 # Drop a database table from within xSync callback.
118 # This is not allowed. Tables cannot be dropped while
119 # any other statement is active.
120 #
121 do_test vtab7-2.6 {
122 set ::callbacks(xSync,abc) {
123 set ::rc [catchsql { DROP TABLE newtab }]
124 }
125 execsql {
126 INSERT INTO abc2 VALUES(1, 2, 3);
127 SELECT name FROM sqlite_master ORDER BY name;
128 }
129 } {abc abc2 log newtab}
130 do_test vtab7-2.6.1 {
131 set ::rc
132 } {1 {database table is locked}}
133 execsql {DROP TABLE newtab}
134  
135 # Write to an attached database from xSync().
136 ifcapable attach {
137 do_test vtab7-3.1 {
138 file delete -force test2.db
139 file delete -force test2.db-journal
140 execsql {
141 ATTACH 'test2.db' AS db2;
142 CREATE TABLE db2.stuff(description, shape, color);
143 }
144 set ::callbacks(xSync,abc) {
145 execsql { INSERT INTO db2.stuff VALUES('abc', 'square', 'green'); }
146 }
147 execsql {
148 INSERT INTO abc2 VALUES(1, 2, 3);
149 SELECT * from stuff;
150 }
151 } {abc square green}
152 }
153  
154 # UPDATE: The next test passes, but leaks memory. So leave it out.
155 #
156 # The following tests test that writing to the database from within
157 # the xCommit callback causes a misuse error.
158 # do_test vtab7-4.1 {
159 # unset -nocomplain ::callbacks(xSync,abc)
160 # set ::callbacks(xCommit,abc) {
161 # execsql { INSERT INTO log VALUES('hello') }
162 # }
163 # catchsql {
164 # INSERT INTO abc2 VALUES(1, 2, 3);
165 # }
166 # } {1 {library routine called out of sequence}}
167  
168 # These tests, vtab7-4.*, test that an SQLITE_LOCKED error is returned
169 # if an attempt to write to a virtual module table or create a new
170 # virtual table from within an xSync() callback.
171 do_test vtab7-4.1 {
172 execsql {
173 CREATE TABLE def(d, e, f);
174 CREATE VIRTUAL TABLE def2 USING echo(def);
175 }
176 set ::callbacks(xSync,abc) {
177 set ::error [catchsql { INSERT INTO def2 VALUES(1, 2, 3) }]
178 }
179 execsql {
180 INSERT INTO abc2 VALUES(1, 2, 3);
181 }
182 set ::error
183 } {1 {database table is locked}}
184 do_test vtab7-4.2 {
185 set ::callbacks(xSync,abc) {
186 set ::error [catchsql { CREATE VIRTUAL TABLE def3 USING echo(def) }]
187 }
188 execsql {
189 INSERT INTO abc2 VALUES(1, 2, 3);
190 }
191 set ::error
192 } {1 {database table is locked}}
193  
194 do_test vtab7-4.3 {
195 set ::callbacks(xSync,abc) {
196 set ::error [catchsql { DROP TABLE def2 }]
197 }
198 execsql {
199 INSERT INTO abc2 VALUES(1, 2, 3);
200 SELECT name FROM sqlite_master ORDER BY name;
201 }
202 set ::error
203 } {1 {database table is locked}}
204  
205 #trace remove variable ::echo_module write echo_module_trace
206 trace vdelete ::echo_module w echo_module_trace
207 unset -nocomplain ::callbacks
208  
209 finish_test