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.
13 # This file tests the various conditions under which an SQLITE_SCHEMA
14 # error should be returned. This is a copy of schema.test that
15 # has been altered to use sqlite3_prepare_v2 instead of sqlite3_prepare
17 # $Id: schema2.test,v 1.4 2009/02/04 17:40:58 drh Exp $
19 #---------------------------------------------------------------------
20 # When any of the following types of SQL statements or actions are
21 # executed, all pre-compiled statements are invalidated. An attempt
22 # to execute an invalidated statement always returns SQLITE_SCHEMA.
24 # CREATE/DROP TABLE...................................schema2-1.*
25 # CREATE/DROP VIEW....................................schema2-2.*
26 # CREATE/DROP TRIGGER.................................schema2-3.*
27 # CREATE/DROP INDEX...................................schema2-4.*
28 # DETACH..............................................schema2-5.*
29 # Deleting a user-function............................schema2-6.*
30 # Deleting a collation sequence.......................schema2-7.*
31 # Setting or changing the authorization function......schema2-8.*
33 # Test cases schema2-9.* and schema2-10.* test some specific bugs
34 # that came up during development.
36 # Test cases schema2-11.* test that it is impossible to delete or
37 # change a collation sequence or user-function while SQL statements
38 # are executing. Adding new collations or functions is allowed.
41 set testdir [file dirname $argv0]
42 source $testdir/tester.tcl
45 set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL]
47 CREATE TABLE abc(a, b, c);
52 sqlite3_finalize $::STMT
55 set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL]
62 sqlite3_finalize $::STMT
68 set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL]
70 CREATE VIEW v1 AS SELECT * FROM sqlite_master;
75 sqlite3_finalize $::STMT
78 set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL]
85 sqlite3_finalize $::STMT
92 CREATE TABLE abc(a, b, c);
94 set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL]
96 CREATE TRIGGER abc_trig AFTER INSERT ON abc BEGIN
102 do_test schema2-3.2 {
103 sqlite3_finalize $::STMT
105 do_test schema2-3.3 {
106 set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL]
108 DROP TRIGGER abc_trig;
112 do_test schema2-3.4 {
113 sqlite3_finalize $::STMT
117 do_test schema2-4.1 {
119 CREATE TABLE abc(a, b, c);
121 set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL]
123 CREATE INDEX abc_index ON abc(a);
127 do_test schema2-4.2 {
128 sqlite3_finalize $::STMT
130 do_test schema2-4.3 {
131 set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL]
133 DROP INDEX abc_index;
137 do_test schema2-4.4 {
138 sqlite3_finalize $::STMT
141 #---------------------------------------------------------------------
142 # Tests 5.1 to 5.4 check that prepared statements are invalidated when
143 # a database is DETACHed (but not when one is ATTACHed).
146 do_test schema2-5.1 {
147 set sql {SELECT * FROM abc;}
148 set ::STMT [sqlite3_prepare_v2 $::DB $sql -1 TAIL]
150 ATTACH 'test2.db' AS aux;
154 do_test schema2-5.2 {
155 sqlite3_reset $::STMT
157 do_test schema2-5.3 {
163 do_test schema2-5.4 {
164 sqlite3_finalize $::STMT
168 #---------------------------------------------------------------------
169 # Tests 6.* check that prepared statements are invalidated when
170 # a user-function is deleted (but not when one is added).
171 do_test schema2-6.1 {
172 set sql {SELECT * FROM abc;}
173 set ::STMT [sqlite3_prepare_v2 $::DB $sql -1 TAIL]
174 db function hello_function {}
177 do_test schema2-6.2 {
178 sqlite3_reset $::STMT
180 do_test schema2-6.3 {
181 sqlite_delete_function $::DB hello_function
184 do_test schema2-6.4 {
185 sqlite3_finalize $::STMT
188 #---------------------------------------------------------------------
189 # Tests 7.* check that prepared statements are invalidated when
190 # a collation sequence is deleted (but not when one is added).
193 do_test schema2-7.1 {
194 set sql {SELECT * FROM abc;}
195 set ::STMT [sqlite3_prepare_v2 $::DB $sql -1 TAIL]
196 add_test_collate $::DB 1 1 1
199 do_test schema2-7.2 {
200 sqlite3_reset $::STMT
202 do_test schema2-7.3 {
203 add_test_collate $::DB 0 0 0
206 do_test schema2-7.4 {
207 sqlite3_finalize $::STMT
211 #---------------------------------------------------------------------
212 # Tests 8.1 and 8.2 check that prepared statements are invalidated when
213 # the authorization function is set.
216 do_test schema2-8.1 {
217 set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL]
221 do_test schema2-8.3 {
222 sqlite3_finalize $::STMT
226 #---------------------------------------------------------------------
227 # schema2-9.1: Test that if a table is dropped by one database connection,
228 # other database connections are aware of the schema change.
229 # schema2-9.2: Test that if a view is dropped by one database connection,
230 # other database connections are aware of the schema change.
232 do_test schema2-9.1 {
241 } {1 {no such table: abc}}
243 CREATE TABLE abc(a, b, c);
246 do_test schema2-9.2 {
248 CREATE VIEW abcview AS SELECT * FROM abc;
256 SELECT * FROM abcview;
258 } {1 {no such table: abcview}}
261 #---------------------------------------------------------------------
262 # Test that if a CREATE TABLE statement fails because there are other
263 # btree cursors open on the same database file it does not corrupt
264 # the sqlite_master table.
266 # 2007-05-02: These tests have been overcome by events. Open btree
267 # cursors no longer block CREATE TABLE. But there is no reason not
268 # to keep the tests in the test suite.
270 do_test schema2-10.1 {
272 INSERT INTO abc VALUES(1, 2, 3);
274 set sql {SELECT * FROM abc}
275 set ::STMT [sqlite3_prepare_v2 $::DB $sql -1 TAIL]
278 do_test schema2-10.2 {
280 CREATE TABLE t2(a, b, c);
283 do_test schema2-10.3 {
284 sqlite3_finalize $::STMT
286 do_test schema2-10.4 {
292 do_test schema2-10.5 {
296 #---------------------------------------------------------------------
297 # Attempting to delete or replace a user-function or collation sequence
298 # while there are active statements returns an SQLITE_BUSY error.
300 # schema2-11.1 - 11.4: User function.
301 # schema2-11.5 - 11.8: Collation sequence.
303 do_test schema2-11.1 {
304 db function tstfunc {}
305 set sql {SELECT * FROM abc}
306 set ::STMT [sqlite3_prepare_v2 $::DB $sql -1 TAIL]
309 do_test schema2-11.2 {
310 sqlite_delete_function $::DB tstfunc
312 do_test schema2-11.3 {
314 db function tstfunc {}
317 } {1 {unable to delete/modify user-function due to active statements}}
318 do_test schema2-11.4 {
319 sqlite3_finalize $::STMT
321 do_test schema2-11.5 {
322 db collate tstcollate {}
323 set sql {SELECT * FROM abc}
324 set ::STMT [sqlite3_prepare_v2 $::DB $sql -1 TAIL]
327 do_test schema2-11.6 {
328 sqlite_delete_collation $::DB tstcollate
330 do_test schema2-11.7 {
332 db collate tstcollate {}
335 } {1 {unable to delete/modify collation sequence due to active statements}}
336 do_test schema2-11.8 {
337 sqlite3_finalize $::STMT