sl@0: # 2006 November 08 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. sl@0: # sl@0: # This file tests the various conditions under which an SQLITE_SCHEMA sl@0: # error should be returned. This is a copy of schema.test that sl@0: # has been altered to use sqlite3_prepare_v2 instead of sqlite3_prepare sl@0: # sl@0: # $Id: schema2.test,v 1.3 2007/10/09 08:29:33 danielk1977 Exp $ sl@0: sl@0: #--------------------------------------------------------------------- sl@0: # When any of the following types of SQL statements or actions are sl@0: # executed, all pre-compiled statements are invalidated. An attempt sl@0: # to execute an invalidated statement always returns SQLITE_SCHEMA. sl@0: # sl@0: # CREATE/DROP TABLE...................................schema2-1.* sl@0: # CREATE/DROP VIEW....................................schema2-2.* sl@0: # CREATE/DROP TRIGGER.................................schema2-3.* sl@0: # CREATE/DROP INDEX...................................schema2-4.* sl@0: # DETACH..............................................schema2-5.* sl@0: # Deleting a user-function............................schema2-6.* sl@0: # Deleting a collation sequence.......................schema2-7.* sl@0: # Setting or changing the authorization function......schema2-8.* sl@0: # sl@0: # Test cases schema2-9.* and schema2-10.* test some specific bugs sl@0: # that came up during development. sl@0: # sl@0: # Test cases schema2-11.* test that it is impossible to delete or sl@0: # change a collation sequence or user-function while SQL statements sl@0: # are executing. Adding new collations or functions is allowed. sl@0: # sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: do_test schema2-1.1 { sl@0: set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL] sl@0: execsql { sl@0: CREATE TABLE abc(a, b, c); sl@0: } sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ROW} sl@0: do_test schema2-1.2 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_OK} sl@0: do_test schema2-1.3 { sl@0: set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL] sl@0: execsql { sl@0: DROP TABLE abc; sl@0: } sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_DONE} sl@0: do_test schema2-1.4 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_OK} sl@0: sl@0: sl@0: ifcapable view { sl@0: do_test schema2-2.1 { sl@0: set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL] sl@0: execsql { sl@0: CREATE VIEW v1 AS SELECT * FROM sqlite_master; sl@0: } sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ROW} sl@0: do_test schema2-2.2 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_OK} sl@0: do_test schema2-2.3 { sl@0: set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL] sl@0: execsql { sl@0: DROP VIEW v1; sl@0: } sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_DONE} sl@0: do_test schema2-2.4 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_OK} sl@0: } sl@0: sl@0: ifcapable trigger { sl@0: do_test schema2-3.1 { sl@0: execsql { sl@0: CREATE TABLE abc(a, b, c); sl@0: } sl@0: set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL] sl@0: execsql { sl@0: CREATE TRIGGER abc_trig AFTER INSERT ON abc BEGIN sl@0: SELECT 1, 2, 3; sl@0: END; sl@0: } sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ROW} sl@0: do_test schema2-3.2 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_OK} sl@0: do_test schema2-3.3 { sl@0: set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL] sl@0: execsql { sl@0: DROP TRIGGER abc_trig; sl@0: } sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ROW} sl@0: do_test schema2-3.4 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_OK} sl@0: } sl@0: sl@0: do_test schema2-4.1 { sl@0: catchsql { sl@0: CREATE TABLE abc(a, b, c); sl@0: } sl@0: set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL] sl@0: execsql { sl@0: CREATE INDEX abc_index ON abc(a); sl@0: } sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ROW} sl@0: do_test schema2-4.2 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_OK} sl@0: do_test schema2-4.3 { sl@0: set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL] sl@0: execsql { sl@0: DROP INDEX abc_index; sl@0: } sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ROW} sl@0: do_test schema2-4.4 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_OK} sl@0: sl@0: #--------------------------------------------------------------------- sl@0: # Tests 5.1 to 5.4 check that prepared statements are invalidated when sl@0: # a database is DETACHed (but not when one is ATTACHed). sl@0: # sl@0: ifcapable attach { sl@0: do_test schema2-5.1 { sl@0: set sql {SELECT * FROM abc;} sl@0: set ::STMT [sqlite3_prepare_v2 $::DB $sql -1 TAIL] sl@0: execsql { sl@0: ATTACH 'test2.db' AS aux; sl@0: } sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_DONE} sl@0: do_test schema2-5.2 { sl@0: sqlite3_reset $::STMT sl@0: } {SQLITE_OK} sl@0: do_test schema2-5.3 { sl@0: execsql { sl@0: DETACH aux; sl@0: } sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_DONE} sl@0: do_test schema2-5.4 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_OK} sl@0: } sl@0: sl@0: #--------------------------------------------------------------------- sl@0: # Tests 6.* check that prepared statements are invalidated when sl@0: # a user-function is deleted (but not when one is added). sl@0: do_test schema2-6.1 { sl@0: set sql {SELECT * FROM abc;} sl@0: set ::STMT [sqlite3_prepare_v2 $::DB $sql -1 TAIL] sl@0: db function hello_function {} sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_DONE} sl@0: do_test schema2-6.2 { sl@0: sqlite3_reset $::STMT sl@0: } {SQLITE_OK} sl@0: do_test schema2-6.3 { sl@0: sqlite_delete_function $::DB hello_function sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_DONE} sl@0: do_test schema2-6.4 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_OK} sl@0: sl@0: #--------------------------------------------------------------------- sl@0: # Tests 7.* check that prepared statements are invalidated when sl@0: # a collation sequence is deleted (but not when one is added). sl@0: # sl@0: ifcapable utf16 { sl@0: do_test schema2-7.1 { sl@0: set sql {SELECT * FROM abc;} sl@0: set ::STMT [sqlite3_prepare_v2 $::DB $sql -1 TAIL] sl@0: add_test_collate $::DB 1 1 1 sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_DONE} sl@0: do_test schema2-7.2 { sl@0: sqlite3_reset $::STMT sl@0: } {SQLITE_OK} sl@0: do_test schema2-7.3 { sl@0: add_test_collate $::DB 0 0 0 sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_DONE} sl@0: do_test schema2-7.4 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_OK} sl@0: } sl@0: sl@0: #--------------------------------------------------------------------- sl@0: # Tests 8.1 and 8.2 check that prepared statements are invalidated when sl@0: # the authorization function is set. sl@0: # sl@0: ifcapable auth { sl@0: do_test schema2-8.1 { sl@0: set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL] sl@0: db auth {} sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ROW} sl@0: do_test schema2-8.3 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_OK} sl@0: } sl@0: sl@0: #--------------------------------------------------------------------- sl@0: # schema2-9.1: Test that if a table is dropped by one database connection, sl@0: # other database connections are aware of the schema change. sl@0: # schema2-9.2: Test that if a view is dropped by one database connection, sl@0: # other database connections are aware of the schema change. sl@0: # sl@0: do_test schema2-9.1 { sl@0: sqlite3 db2 test.db sl@0: execsql { sl@0: DROP TABLE abc; sl@0: } db2 sl@0: db2 close sl@0: catchsql { sl@0: SELECT * FROM abc; sl@0: } sl@0: } {1 {no such table: abc}} sl@0: execsql { sl@0: CREATE TABLE abc(a, b, c); sl@0: } sl@0: ifcapable view { sl@0: do_test schema2-9.2 { sl@0: execsql { sl@0: CREATE VIEW abcview AS SELECT * FROM abc; sl@0: } sl@0: sqlite3 db2 test.db sl@0: execsql { sl@0: DROP VIEW abcview; sl@0: } db2 sl@0: db2 close sl@0: catchsql { sl@0: SELECT * FROM abcview; sl@0: } sl@0: } {1 {no such table: abcview}} sl@0: } sl@0: sl@0: #--------------------------------------------------------------------- sl@0: # Test that if a CREATE TABLE statement fails because there are other sl@0: # btree cursors open on the same database file it does not corrupt sl@0: # the sqlite_master table. sl@0: # sl@0: # 2007-05-02: These tests have been overcome by events. Open btree sl@0: # cursors no longer block CREATE TABLE. But there is no reason not sl@0: # to keep the tests in the test suite. sl@0: # sl@0: do_test schema2-10.1 { sl@0: execsql { sl@0: INSERT INTO abc VALUES(1, 2, 3); sl@0: } sl@0: set sql {SELECT * FROM abc} sl@0: set ::STMT [sqlite3_prepare_v2 $::DB $sql -1 TAIL] sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ROW} sl@0: do_test schema2-10.2 { sl@0: catchsql { sl@0: CREATE TABLE t2(a, b, c); sl@0: } sl@0: } {0 {}} sl@0: do_test schema2-10.3 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_OK} sl@0: do_test schema2-10.4 { sl@0: sqlite3 db2 test.db sl@0: execsql { sl@0: SELECT * FROM abc sl@0: } db2 sl@0: } {1 2 3} sl@0: do_test schema2-10.5 { sl@0: db2 close sl@0: } {} sl@0: sl@0: #--------------------------------------------------------------------- sl@0: # Attempting to delete or replace a user-function or collation sequence sl@0: # while there are active statements returns an SQLITE_BUSY error. sl@0: # sl@0: # schema2-11.1 - 11.4: User function. sl@0: # schema2-11.5 - 11.8: Collation sequence. sl@0: # sl@0: do_test schema2-11.1 { sl@0: db function tstfunc {} sl@0: set sql {SELECT * FROM abc} sl@0: set ::STMT [sqlite3_prepare_v2 $::DB $sql -1 TAIL] sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ROW} sl@0: do_test schema2-11.2 { sl@0: sqlite_delete_function $::DB tstfunc sl@0: } {SQLITE_BUSY} sl@0: do_test schema2-11.3 { sl@0: set rc [catch { sl@0: db function tstfunc {} sl@0: } msg] sl@0: list $rc $msg sl@0: } {1 {Unable to delete/modify user-function due to active statements}} sl@0: do_test schema2-11.4 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_OK} sl@0: do_test schema2-11.5 { sl@0: db collate tstcollate {} sl@0: set sql {SELECT * FROM abc} sl@0: set ::STMT [sqlite3_prepare_v2 $::DB $sql -1 TAIL] sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ROW} sl@0: do_test schema2-11.6 { sl@0: sqlite_delete_collation $::DB tstcollate sl@0: } {SQLITE_BUSY} sl@0: do_test schema2-11.7 { sl@0: set rc [catch { sl@0: db collate tstcollate {} sl@0: } msg] sl@0: list $rc $msg sl@0: } {1 {Unable to delete/modify collation sequence due to active statements}} sl@0: do_test schema2-11.8 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_OK} sl@0: sl@0: finish_test