drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1 | # 2001 September 15 |
drh | 87c40e8 | 2001-07-23 14:33:02 +0000 | [diff] [blame] | 2 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
drh | 87c40e8 | 2001-07-23 14:33:02 +0000 | [diff] [blame] | 5 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | 87c40e8 | 2001-07-23 14:33:02 +0000 | [diff] [blame] | 9 | # |
| 10 | #*********************************************************************** |
| 11 | # This file implements regression tests for SQLite library. The |
| 12 | # focus of this file is the ability to specify table and column names |
| 13 | # as quoted strings. |
| 14 | # |
drh | b556ce1 | 2007-04-25 11:32:30 +0000 | [diff] [blame] | 15 | # $Id: quote.test,v 1.7 2007/04/25 11:32:30 drh Exp $ |
drh | 87c40e8 | 2001-07-23 14:33:02 +0000 | [diff] [blame] | 16 | |
| 17 | set testdir [file dirname $argv0] |
| 18 | source $testdir/tester.tcl |
dan | 0d92571 | 2019-05-20 17:14:25 +0000 | [diff] [blame] | 19 | set testprefix quote |
drh | 87c40e8 | 2001-07-23 14:33:02 +0000 | [diff] [blame] | 20 | |
| 21 | # Create a table with a strange name and with strange column names. |
| 22 | # |
| 23 | do_test quote-1.0 { |
drh | 3d94662 | 2005-08-13 18:15:42 +0000 | [diff] [blame] | 24 | catchsql {CREATE TABLE '@abc' ( '#xyz' int, '!pqr' text );} |
drh | 87c40e8 | 2001-07-23 14:33:02 +0000 | [diff] [blame] | 25 | } {0 {}} |
| 26 | |
| 27 | # Insert, update and query the table. |
| 28 | # |
| 29 | do_test quote-1.1 { |
drh | 3d94662 | 2005-08-13 18:15:42 +0000 | [diff] [blame] | 30 | catchsql {INSERT INTO '@abc' VALUES(5,'hello')} |
drh | 87c40e8 | 2001-07-23 14:33:02 +0000 | [diff] [blame] | 31 | } {0 {}} |
drh | 3d94662 | 2005-08-13 18:15:42 +0000 | [diff] [blame] | 32 | do_test quote-1.2.1 { |
| 33 | catchsql {SELECT * FROM '@abc'} |
| 34 | } {0 {5 hello}} |
| 35 | do_test quote-1.2.2 { |
| 36 | catchsql {SELECT * FROM [@abc]} ;# SqlServer compatibility |
| 37 | } {0 {5 hello}} |
| 38 | do_test quote-1.2.3 { |
| 39 | catchsql {SELECT * FROM `@abc`} ;# MySQL compatibility |
drh | 87c40e8 | 2001-07-23 14:33:02 +0000 | [diff] [blame] | 40 | } {0 {5 hello}} |
| 41 | do_test quote-1.3 { |
drh | 3d94662 | 2005-08-13 18:15:42 +0000 | [diff] [blame] | 42 | catchsql { |
| 43 | SELECT '@abc'.'!pqr', '@abc'.'#xyz'+5 FROM '@abc' |
| 44 | } |
drh | 87c40e8 | 2001-07-23 14:33:02 +0000 | [diff] [blame] | 45 | } {0 {hello 10}} |
drh | 2398937 | 2002-05-21 13:43:04 +0000 | [diff] [blame] | 46 | do_test quote-1.3.1 { |
| 47 | catchsql { |
| 48 | SELECT '!pqr', '#xyz'+5 FROM '@abc' |
| 49 | } |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 50 | } {0 {!pqr 5}} |
drh | 2398937 | 2002-05-21 13:43:04 +0000 | [diff] [blame] | 51 | do_test quote-1.3.2 { |
| 52 | catchsql { |
| 53 | SELECT "!pqr", "#xyz"+5 FROM '@abc' |
| 54 | } |
| 55 | } {0 {hello 10}} |
drh | 3d94662 | 2005-08-13 18:15:42 +0000 | [diff] [blame] | 56 | do_test quote-1.3.3 { |
| 57 | catchsql { |
| 58 | SELECT [!pqr], `#xyz`+5 FROM '@abc' |
| 59 | } |
| 60 | } {0 {hello 10}} |
drh | b556ce1 | 2007-04-25 11:32:30 +0000 | [diff] [blame] | 61 | do_test quote-1.3.4 { |
drh | 2398937 | 2002-05-21 13:43:04 +0000 | [diff] [blame] | 62 | set r [catch { |
| 63 | execsql {SELECT '@abc'.'!pqr', '@abc'.'#xyz'+5 FROM '@abc'} |
| 64 | } msg ] |
| 65 | lappend r $msg |
| 66 | } {0 {hello 10}} |
drh | 87c40e8 | 2001-07-23 14:33:02 +0000 | [diff] [blame] | 67 | do_test quote-1.4 { |
| 68 | set r [catch { |
| 69 | execsql {UPDATE '@abc' SET '#xyz'=11} |
| 70 | } msg ] |
| 71 | lappend r $msg |
| 72 | } {0 {}} |
| 73 | do_test quote-1.5 { |
| 74 | set r [catch { |
| 75 | execsql {SELECT '@abc'.'!pqr', '@abc'.'#xyz'+5 FROM '@abc'} |
| 76 | } msg ] |
| 77 | lappend r $msg |
| 78 | } {0 {hello 16}} |
| 79 | |
| 80 | # Drop the table with the strange name. |
| 81 | # |
| 82 | do_test quote-1.6 { |
| 83 | set r [catch { |
| 84 | execsql {DROP TABLE '@abc'} |
| 85 | } msg ] |
| 86 | lappend r $msg |
| 87 | } {0 {}} |
dan | 0d92571 | 2019-05-20 17:14:25 +0000 | [diff] [blame] | 88 | |
| 89 | #------------------------------------------------------------------------- |
| 90 | # Check that it is not possible to use double-quotes for a string |
| 91 | # constant in a CHECK constraint or CREATE INDEX statement. However, |
| 92 | # SQLite can load such a schema from disk. |
| 93 | # |
| 94 | reset_db |
drh | 4b50da9 | 2019-07-02 12:23:09 +0000 | [diff] [blame] | 95 | sqlite3_db_config db SQLITE_DBCONFIG_DQS_DDL 0 |
| 96 | sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1 |
dan | 0d92571 | 2019-05-20 17:14:25 +0000 | [diff] [blame] | 97 | do_execsql_test 2.0 { |
| 98 | CREATE TABLE t1(x, y, z); |
| 99 | } |
| 100 | foreach {tn sql errname} { |
| 101 | 1 { CREATE TABLE xyz(a, b, c CHECK (c!="null") ) } null |
| 102 | 2 { CREATE INDEX i2 ON t1(x, y, z||"abc") } abc |
| 103 | 3 { CREATE INDEX i3 ON t1("w") } w |
| 104 | 4 { CREATE INDEX i4 ON t1(x) WHERE z="w" } w |
| 105 | } { |
| 106 | do_catchsql_test 2.1.$tn $sql [list 1 "no such column: $errname"] |
| 107 | } |
| 108 | |
| 109 | do_execsql_test 2.2 { |
| 110 | PRAGMA writable_schema = 1; |
| 111 | CREATE TABLE xyz(a, b, c CHECK (c!="null") ); |
| 112 | CREATE INDEX i2 ON t1(x, y, z||"abc"); |
drh | 44d4413 | 2021-03-14 19:55:40 +0000 | [diff] [blame] | 113 | CREATE INDEX i3 ON t1("w"||""); |
dan | 0d92571 | 2019-05-20 17:14:25 +0000 | [diff] [blame] | 114 | CREATE INDEX i4 ON t1(x) WHERE z="w"; |
| 115 | } |
| 116 | |
| 117 | db close |
| 118 | sqlite3 db test.db |
| 119 | |
| 120 | do_execsql_test 2.3.1 { |
| 121 | INSERT INTO xyz VALUES(1, 2, 3); |
| 122 | } |
| 123 | do_catchsql_test 2.3.2 { |
| 124 | INSERT INTO xyz VALUES(1, 2, 'null'); |
drh | 92e21ef | 2020-08-27 18:36:30 +0000 | [diff] [blame] | 125 | } {1 {CHECK constraint failed: c!="null"}} |
dan | 0d92571 | 2019-05-20 17:14:25 +0000 | [diff] [blame] | 126 | |
| 127 | do_execsql_test 2.4 { |
| 128 | INSERT INTO t1 VALUES(1, 2, 3); |
| 129 | INSERT INTO t1 VALUES(4, 5, 'w'); |
| 130 | SELECT * FROM t1 WHERE z='w'; |
| 131 | } {4 5 w} |
| 132 | do_execsql_test 2.5 { |
| 133 | SELECT sql FROM sqlite_master; |
| 134 | } { |
| 135 | {CREATE TABLE t1(x, y, z)} |
| 136 | {CREATE TABLE xyz(a, b, c CHECK (c!="null") )} |
| 137 | {CREATE INDEX i2 ON t1(x, y, z||"abc")} |
drh | 44d4413 | 2021-03-14 19:55:40 +0000 | [diff] [blame] | 138 | {CREATE INDEX i3 ON t1("w"||"")} |
dan | 0d92571 | 2019-05-20 17:14:25 +0000 | [diff] [blame] | 139 | {CREATE INDEX i4 ON t1(x) WHERE z="w"} |
| 140 | } |
| 141 | |
drh | 44d4413 | 2021-03-14 19:55:40 +0000 | [diff] [blame] | 142 | # 2021-03-13 |
| 143 | # ticket 1c24a659e6d7f3a1 |
| 144 | reset_db |
| 145 | do_catchsql_test 3.0 { |
| 146 | CREATE TABLE t1(a,b); |
| 147 | CREATE INDEX x1 on t1("b"); |
| 148 | ALTER TABLE t1 DROP COLUMN b; |
| 149 | } {1 {error in index x1 after drop column: no such column: b}} |
| 150 | do_catchsql_test 3.1 { |
| 151 | DROP TABLE t1; |
| 152 | CREATE TABLE t1(a,"b"); |
| 153 | CREATE INDEX x1 on t1("b"); |
| 154 | ALTER TABLE t1 DROP COLUMN b; |
| 155 | } {1 {error in index x1 after drop column: no such column: b}} |
| 156 | do_catchsql_test 3.2 { |
| 157 | DROP TABLE t1; |
| 158 | CREATE TABLE t1(a,'b'); |
| 159 | CREATE INDEX x1 on t1("b"); |
| 160 | ALTER TABLE t1 DROP COLUMN b; |
| 161 | } {1 {error in index x1 after drop column: no such column: b}} |
| 162 | do_catchsql_test 3.3 { |
| 163 | DROP TABLE t1; |
| 164 | CREATE TABLE t1(a,"b"); |
| 165 | CREATE INDEX x1 on t1('b'); |
| 166 | ALTER TABLE t1 DROP COLUMN b; |
| 167 | } {1 {error in index x1 after drop column: no such column: b}} |
| 168 | do_catchsql_test 3.4 { |
| 169 | DROP TABLE t1; |
| 170 | CREATE TABLE t1(a, b, c); |
| 171 | CREATE INDEX x1 ON t1("a"||"b"); |
| 172 | INSERT INTO t1 VALUES(1,2,3),(1,4,5); |
| 173 | ALTER TABLE t1 DROP COLUMN b; |
| 174 | } {1 {error in index x1 after drop column: no such column: b}} |
drh | 0e8075a | 2021-03-14 20:17:06 +0000 | [diff] [blame] | 175 | do_catchsql_test 3.5 { |
| 176 | DROP TABLE t1; |
| 177 | CREATE TABLE t1(a, b, c); |
| 178 | CREATE INDEX x1 ON t1("a"||"x"); |
| 179 | INSERT INTO t1 VALUES(1,2,3),(1,4,5); |
| 180 | ALTER TABLE t1 DROP COLUMN b; |
| 181 | } {0 {}} |
drh | 87c40e8 | 2001-07-23 14:33:02 +0000 | [diff] [blame] | 182 | |
| 183 | finish_test |