danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame^] | 1 | # 2004 Jun 27 |
| 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. |
| 12 | # |
| 13 | # This file tests the various conditions under which an SQLITE_SCHEMA |
| 14 | # error should be returned. |
| 15 | # |
| 16 | # $Id: schema.test,v 1.1 2005/01/24 10:26:00 danielk1977 Exp $ |
| 17 | |
| 18 | set testdir [file dirname $argv0] |
| 19 | source $testdir/tester.tcl |
| 20 | |
| 21 | # schema-1.1: Test that if a table is dropped by one database connection, |
| 22 | # other database connections are aware of the schema change. |
| 23 | # schema-1.2: Test that if a view is dropped by one database connection, |
| 24 | # other database connections are aware of the schema change. |
| 25 | # |
| 26 | do_test schema-1.1 { |
| 27 | execsql { |
| 28 | CREATE TABLE abc(a, b, c); |
| 29 | } |
| 30 | sqlite3 db2 test.db |
| 31 | execsql { |
| 32 | DROP TABLE abc; |
| 33 | } db2 |
| 34 | catchsql { |
| 35 | SELECT * FROM abc; |
| 36 | } |
| 37 | } {1 {no such table: abc}} |
| 38 | do_test schema-1.2 { |
| 39 | execsql { |
| 40 | CREATE TABLE abc(a, b, c); |
| 41 | CREATE VIEW abcview AS SELECT * FROM abc; |
| 42 | } |
| 43 | sqlite3 db2 test.db |
| 44 | execsql { |
| 45 | DROP VIEW abcview; |
| 46 | } db2 |
| 47 | db2 close |
| 48 | catchsql { |
| 49 | SELECT * FROM abcview; |
| 50 | } |
| 51 | } {1 {no such table: abcview}} |
| 52 | |
| 53 | # Tests 2.1 to 2.4 check that prepared statements are invalidated when |
| 54 | # a collation sequence is deleted (but not when one is added). |
| 55 | # |
| 56 | do_test schema-2.1 { |
| 57 | set sql {SELECT * FROM abc;} |
| 58 | set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL] |
| 59 | add_test_collate $::DB 1 1 1 |
| 60 | sqlite3_step $::STMT |
| 61 | } {SQLITE_DONE} |
| 62 | do_test schema-2.2 { |
| 63 | sqlite3_reset $::STMT |
| 64 | } {SQLITE_OK} |
| 65 | do_test schema-2.3 { |
| 66 | add_test_collate $::DB 0 0 0 |
| 67 | sqlite3_step $::STMT |
| 68 | } {SQLITE_ERROR} |
| 69 | do_test schema-2.4 { |
| 70 | sqlite3_finalize $::STMT |
| 71 | } {SQLITE_SCHEMA} |
| 72 | |
| 73 | # Tests 3.1 to 3.4 check that prepared statements are invalidated when |
| 74 | # a database is DETACHed (but not when one is ATTACHed). |
| 75 | # |
| 76 | do_test schema-3.1 { |
| 77 | set sql {SELECT * FROM abc;} |
| 78 | set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL] |
| 79 | execsql { |
| 80 | ATTACH 'test2.db' AS aux; |
| 81 | } |
| 82 | sqlite3_step $::STMT |
| 83 | } {SQLITE_DONE} |
| 84 | do_test schema-3.2 { |
| 85 | sqlite3_reset $::STMT |
| 86 | } {SQLITE_OK} |
| 87 | do_test schema-3.3 { |
| 88 | execsql { |
| 89 | DETACH aux; |
| 90 | } |
| 91 | sqlite3_step $::STMT |
| 92 | } {SQLITE_ERROR} |
| 93 | do_test schema-3.4 { |
| 94 | sqlite3_finalize $::STMT |
| 95 | } {SQLITE_SCHEMA} |
| 96 | |
| 97 | |
| 98 | # Test that if a CREATE TABLE statement fails because there are other |
| 99 | # btree cursors open on the same database file it does not corrupt |
| 100 | # the sqlite_master table. |
| 101 | # |
| 102 | do_test schema-4.1 { |
| 103 | execsql { |
| 104 | INSERT INTO abc VALUES(1, 2, 3); |
| 105 | } |
| 106 | set sql {SELECT * FROM abc} |
| 107 | set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL] |
| 108 | sqlite3_step $::STMT |
| 109 | } {SQLITE_ROW} |
| 110 | do_test schema-4.2 { |
| 111 | catchsql { |
| 112 | CREATE TABLE t2(a, b, c); |
| 113 | } |
| 114 | } {1 {database table is locked}} |
| 115 | do_test schema-4.3 { |
| 116 | sqlite3_finalize $::STMT |
| 117 | } {SQLITE_OK} |
| 118 | do_test schema-4.4 { |
| 119 | sqlite3 db2 test.db |
| 120 | execsql { |
| 121 | SELECT * FROM abc |
| 122 | } db2 |
| 123 | } {1 2 3} |
| 124 | do_test schema-4.5 { |
| 125 | db2 close |
| 126 | } {} |
| 127 | |
| 128 | finish_test |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |