drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 1 | # 2002 March 6 |
| 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 implements tests for the PRAGMA command. |
| 14 | # |
danielk1977 | 838cce4 | 2009-01-12 14:01:45 +0000 | [diff] [blame] | 15 | # $Id: pragma.test,v 1.73 2009/01/12 14:01:45 danielk1977 Exp $ |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 16 | |
| 17 | set testdir [file dirname $argv0] |
| 18 | source $testdir/tester.tcl |
dan | 5885e76 | 2012-07-16 10:06:12 +0000 | [diff] [blame] | 19 | set testprefix pragma |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 20 | |
dan | 68928b6 | 2010-06-22 13:46:43 +0000 | [diff] [blame] | 21 | # Do not use a codec for tests in this file, as the database file is |
| 22 | # manipulated directly using tcl scripts (using the [hexio_write] command). |
| 23 | # |
| 24 | do_not_use_codec |
| 25 | |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 26 | # Test organization: |
| 27 | # |
| 28 | # pragma-1.*: Test cache_size, default_cache_size and synchronous on main db. |
| 29 | # pragma-2.*: Test synchronous on attached db. |
| 30 | # pragma-3.*: Test detection of table/index inconsistency by integrity_check. |
| 31 | # pragma-4.*: Test cache_size and default_cache_size on attached db. |
| 32 | # pragma-5.*: Test that pragma synchronous may not be used inside of a |
| 33 | # transaction. |
danielk1977 | cc6bd38 | 2005-01-10 02:48:49 +0000 | [diff] [blame] | 34 | # pragma-6.*: Test schema-query pragmas. |
| 35 | # pragma-7.*: Miscellaneous tests. |
| 36 | # pragma-8.*: Test user_version and schema_version pragmas. |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 37 | # pragma-9.*: Test temp_store and temp_store_directory. |
danielk1977 | cc6bd38 | 2005-01-10 02:48:49 +0000 | [diff] [blame] | 38 | # pragma-10.*: Test the count_changes pragma in the presence of triggers. |
danielk1977 | 48af65a | 2005-02-09 03:20:37 +0000 | [diff] [blame] | 39 | # pragma-11.*: Test the collation_list pragma. |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 40 | # pragma-14.*: Test the page_count pragma. |
danielk1977 | 8cf6c55 | 2008-06-23 16:53:46 +0000 | [diff] [blame] | 41 | # pragma-15.*: Test that the value set using the cache_size pragma is not |
| 42 | # reset when the schema is reloaded. |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 43 | # pragma-16.*: Test proxy locking |
mistachkin | a112d14 | 2012-03-14 00:44:01 +0000 | [diff] [blame] | 44 | # pragma-20.*: Test data_store_directory. |
dan | 5885e76 | 2012-07-16 10:06:12 +0000 | [diff] [blame] | 45 | # pragma-22.*: Test that "PRAGMA [db].integrity_check" respects the "db" |
| 46 | # directive - if it is present. |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 47 | # |
| 48 | |
drh | 05a8298 | 2006-03-19 13:00:25 +0000 | [diff] [blame] | 49 | ifcapable !pragma { |
| 50 | finish_test |
| 51 | return |
| 52 | } |
| 53 | |
drh | c228be5 | 2015-01-31 02:00:01 +0000 | [diff] [blame] | 54 | # Capture the output of a pragma in a TEMP table. |
| 55 | # |
| 56 | proc capture_pragma {db tabname sql} { |
| 57 | $db eval "DROP TABLE IF EXISTS temp.$tabname" |
| 58 | set once 1 |
| 59 | $db eval $sql x { |
| 60 | if {$once} { |
| 61 | set once 0 |
| 62 | set ins "INSERT INTO $tabname VALUES" |
| 63 | set crtab "CREATE TEMP TABLE $tabname " |
| 64 | set sep "(" |
| 65 | foreach col $x(*) { |
| 66 | append ins ${sep}\$x($col) |
| 67 | append crtab ${sep}\"$col\" |
| 68 | set sep , |
| 69 | } |
| 70 | append ins ) |
| 71 | append crtab ) |
| 72 | $db eval $crtab |
| 73 | } |
| 74 | $db eval $ins |
| 75 | } |
| 76 | } |
| 77 | |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 78 | # Delete the preexisting database to avoid the special setup |
| 79 | # that the "all.test" script does. |
| 80 | # |
| 81 | db close |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 82 | delete_file test.db test.db-journal |
| 83 | delete_file test3.db test3.db-journal |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 84 | sqlite3 db test.db; set DB [sqlite3_connection_pointer db] |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 85 | |
drh | b3366b9 | 2015-09-11 20:54:44 +0000 | [diff] [blame] | 86 | # EVIDENCE-OF: R-13861-56665 PRAGMA schema.cache_size; PRAGMA |
| 87 | # schema.cache_size = pages; PRAGMA schema.cache_size = -kibibytes; |
drh | 9d356fb | 2015-02-27 20:28:08 +0000 | [diff] [blame] | 88 | # Query or change the suggested maximum number of database disk pages |
| 89 | # that SQLite will hold in memory at once per open database file. |
| 90 | # |
danielk1977 | c7b4a44 | 2004-11-23 10:52:51 +0000 | [diff] [blame] | 91 | ifcapable pager_pragmas { |
drh | 1e9daa6 | 2007-04-06 21:42:22 +0000 | [diff] [blame] | 92 | set DFLT_CACHE_SZ [db one {PRAGMA default_cache_size}] |
| 93 | set TEMP_CACHE_SZ [db one {PRAGMA temp.default_cache_size}] |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 94 | do_test pragma-1.1 { |
| 95 | execsql { |
| 96 | PRAGMA cache_size; |
| 97 | PRAGMA default_cache_size; |
| 98 | PRAGMA synchronous; |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 99 | } |
drh | 1e9daa6 | 2007-04-06 21:42:22 +0000 | [diff] [blame] | 100 | } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2] |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 101 | do_test pragma-1.2 { |
drh | 9d356fb | 2015-02-27 20:28:08 +0000 | [diff] [blame] | 102 | # EVIDENCE-OF: R-42059-47211 If the argument N is positive then the |
| 103 | # suggested cache size is set to N. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 104 | execsql { |
drh | eb43e5c | 2005-05-22 20:30:39 +0000 | [diff] [blame] | 105 | PRAGMA synchronous=OFF; |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 106 | PRAGMA cache_size=1234; |
| 107 | PRAGMA cache_size; |
| 108 | PRAGMA default_cache_size; |
| 109 | PRAGMA synchronous; |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 110 | } |
drh | 1e9daa6 | 2007-04-06 21:42:22 +0000 | [diff] [blame] | 111 | } [list 1234 $DFLT_CACHE_SZ 0] |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 112 | do_test pragma-1.3 { |
| 113 | db close |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 114 | sqlite3 db test.db |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 115 | execsql { |
| 116 | PRAGMA cache_size; |
| 117 | PRAGMA default_cache_size; |
| 118 | PRAGMA synchronous; |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 119 | } |
drh | 1e9daa6 | 2007-04-06 21:42:22 +0000 | [diff] [blame] | 120 | } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2] |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 121 | do_test pragma-1.4 { |
| 122 | execsql { |
| 123 | PRAGMA synchronous=OFF; |
| 124 | PRAGMA cache_size; |
| 125 | PRAGMA default_cache_size; |
| 126 | PRAGMA synchronous; |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 127 | } |
drh | 1e9daa6 | 2007-04-06 21:42:22 +0000 | [diff] [blame] | 128 | } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 0] |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 129 | do_test pragma-1.5 { |
| 130 | execsql { |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 131 | PRAGMA cache_size=-4321; |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 132 | PRAGMA cache_size; |
| 133 | PRAGMA default_cache_size; |
| 134 | PRAGMA synchronous; |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 135 | } |
drh | 3b42abb | 2011-11-09 14:23:04 +0000 | [diff] [blame] | 136 | } [list -4321 $DFLT_CACHE_SZ 0] |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 137 | do_test pragma-1.6 { |
| 138 | execsql { |
| 139 | PRAGMA synchronous=ON; |
| 140 | PRAGMA cache_size; |
| 141 | PRAGMA default_cache_size; |
| 142 | PRAGMA synchronous; |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 143 | } |
drh | 3b42abb | 2011-11-09 14:23:04 +0000 | [diff] [blame] | 144 | } [list -4321 $DFLT_CACHE_SZ 1] |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 145 | do_test pragma-1.7 { |
| 146 | db close |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 147 | sqlite3 db test.db |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 148 | execsql { |
| 149 | PRAGMA cache_size; |
| 150 | PRAGMA default_cache_size; |
| 151 | PRAGMA synchronous; |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 152 | } |
drh | 1e9daa6 | 2007-04-06 21:42:22 +0000 | [diff] [blame] | 153 | } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2] |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 154 | do_test pragma-1.8 { |
| 155 | execsql { |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 156 | PRAGMA default_cache_size=-123; |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 157 | PRAGMA cache_size; |
| 158 | PRAGMA default_cache_size; |
| 159 | PRAGMA synchronous; |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 160 | } |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 161 | } {123 123 2} |
drh | 802d69a | 2005-02-13 23:34:24 +0000 | [diff] [blame] | 162 | do_test pragma-1.9.1 { |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 163 | db close |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 164 | sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db] |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 165 | execsql { |
| 166 | PRAGMA cache_size; |
| 167 | PRAGMA default_cache_size; |
| 168 | PRAGMA synchronous; |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 169 | } |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 170 | } {123 123 2} |
drh | 802d69a | 2005-02-13 23:34:24 +0000 | [diff] [blame] | 171 | ifcapable vacuum { |
| 172 | do_test pragma-1.9.2 { |
| 173 | execsql { |
| 174 | VACUUM; |
| 175 | PRAGMA cache_size; |
| 176 | PRAGMA default_cache_size; |
| 177 | PRAGMA synchronous; |
| 178 | } |
| 179 | } {123 123 2} |
| 180 | } |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 181 | do_test pragma-1.10 { |
drh | 5a38705 | 2003-01-11 14:19:51 +0000 | [diff] [blame] | 182 | execsql { |
drh | 4303fee | 2003-02-15 23:09:17 +0000 | [diff] [blame] | 183 | PRAGMA synchronous=NORMAL; |
| 184 | PRAGMA cache_size; |
| 185 | PRAGMA default_cache_size; |
| 186 | PRAGMA synchronous; |
drh | 4303fee | 2003-02-15 23:09:17 +0000 | [diff] [blame] | 187 | } |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 188 | } {123 123 1} |
drh | 6841b1c | 2016-02-03 19:20:15 +0000 | [diff] [blame] | 189 | do_test pragma-1.11.1 { |
| 190 | execsql { |
| 191 | PRAGMA synchronous=EXTRA; |
| 192 | PRAGMA cache_size; |
| 193 | PRAGMA default_cache_size; |
| 194 | PRAGMA synchronous; |
| 195 | } |
| 196 | } {123 123 3} |
| 197 | do_test pragma-1.11.2 { |
drh | 4303fee | 2003-02-15 23:09:17 +0000 | [diff] [blame] | 198 | execsql { |
| 199 | PRAGMA synchronous=FULL; |
| 200 | PRAGMA cache_size; |
| 201 | PRAGMA default_cache_size; |
| 202 | PRAGMA synchronous; |
drh | 4303fee | 2003-02-15 23:09:17 +0000 | [diff] [blame] | 203 | } |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 204 | } {123 123 2} |
| 205 | do_test pragma-1.12 { |
drh | 4303fee | 2003-02-15 23:09:17 +0000 | [diff] [blame] | 206 | db close |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 207 | sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db] |
drh | 4303fee | 2003-02-15 23:09:17 +0000 | [diff] [blame] | 208 | execsql { |
| 209 | PRAGMA cache_size; |
| 210 | PRAGMA default_cache_size; |
| 211 | PRAGMA synchronous; |
drh | 4303fee | 2003-02-15 23:09:17 +0000 | [diff] [blame] | 212 | } |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 213 | } {123 123 2} |
| 214 | |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 215 | # Make sure the pragma handler understands numeric values in addition |
| 216 | # to keywords like "off" and "full". |
| 217 | # |
| 218 | do_test pragma-1.13 { |
| 219 | execsql { |
| 220 | PRAGMA synchronous=0; |
| 221 | PRAGMA synchronous; |
| 222 | } |
| 223 | } {0} |
| 224 | do_test pragma-1.14 { |
| 225 | execsql { |
| 226 | PRAGMA synchronous=2; |
| 227 | PRAGMA synchronous; |
| 228 | } |
| 229 | } {2} |
drh | 59ac655 | 2015-04-16 16:04:39 +0000 | [diff] [blame] | 230 | do_test pragma-1.14.1 { |
| 231 | execsql { |
| 232 | PRAGMA synchronous=4; |
| 233 | PRAGMA synchronous; |
| 234 | } |
drh | 6841b1c | 2016-02-03 19:20:15 +0000 | [diff] [blame] | 235 | } {4} |
drh | 59ac655 | 2015-04-16 16:04:39 +0000 | [diff] [blame] | 236 | do_test pragma-1.14.2 { |
| 237 | execsql { |
drh | d99d283 | 2015-04-17 15:58:33 +0000 | [diff] [blame] | 238 | PRAGMA synchronous=3; |
| 239 | PRAGMA synchronous; |
| 240 | } |
drh | 6841b1c | 2016-02-03 19:20:15 +0000 | [diff] [blame] | 241 | } {3} |
drh | d99d283 | 2015-04-17 15:58:33 +0000 | [diff] [blame] | 242 | do_test pragma-1.14.3 { |
| 243 | execsql { |
drh | 6841b1c | 2016-02-03 19:20:15 +0000 | [diff] [blame] | 244 | PRAGMA synchronous=8; |
| 245 | PRAGMA synchronous; |
| 246 | } |
| 247 | } {0} |
| 248 | do_test pragma-1.14.4 { |
| 249 | execsql { |
drh | 59ac655 | 2015-04-16 16:04:39 +0000 | [diff] [blame] | 250 | PRAGMA synchronous=10; |
| 251 | PRAGMA synchronous; |
| 252 | } |
| 253 | } {2} |
dan | b84b38f | 2019-04-04 17:58:34 +0000 | [diff] [blame] | 254 | |
| 255 | do_execsql_test 1.15.1 { |
| 256 | PRAGMA default_cache_size = 0; |
| 257 | } |
| 258 | do_execsql_test 1.15.2 { |
| 259 | PRAGMA default_cache_size; |
| 260 | } $DFLT_CACHE_SZ |
| 261 | do_execsql_test 1.15.3 { |
| 262 | PRAGMA default_cache_size = -500; |
| 263 | } |
| 264 | do_execsql_test 1.15.4 { |
| 265 | PRAGMA default_cache_size; |
| 266 | } 500 |
| 267 | do_execsql_test 1.15.3 { |
| 268 | PRAGMA default_cache_size = 500; |
| 269 | } |
| 270 | do_execsql_test 1.15.4 { |
| 271 | PRAGMA default_cache_size; |
| 272 | } 500 |
| 273 | db close |
| 274 | hexio_write test.db 48 FFFFFF00 |
| 275 | sqlite3 db test.db |
| 276 | do_execsql_test 1.15.4 { |
| 277 | PRAGMA default_cache_size; |
| 278 | } 256 |
danielk1977 | c7b4a44 | 2004-11-23 10:52:51 +0000 | [diff] [blame] | 279 | } ;# ifcapable pager_pragmas |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 280 | |
| 281 | # Test turning "flag" pragmas on and off. |
| 282 | # |
danielk1977 | 6338c76 | 2007-05-17 16:38:30 +0000 | [diff] [blame] | 283 | ifcapable debug { |
| 284 | # Pragma "vdbe_listing" is only available if compiled with SQLITE_DEBUG |
| 285 | # |
| 286 | do_test pragma-1.15 { |
| 287 | execsql { |
| 288 | PRAGMA vdbe_listing=YES; |
| 289 | PRAGMA vdbe_listing; |
| 290 | } |
| 291 | } {1} |
| 292 | do_test pragma-1.16 { |
| 293 | execsql { |
| 294 | PRAGMA vdbe_listing=NO; |
| 295 | PRAGMA vdbe_listing; |
| 296 | } |
| 297 | } {0} |
| 298 | } |
| 299 | |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 300 | do_test pragma-1.17 { |
| 301 | execsql { |
| 302 | PRAGMA parser_trace=ON; |
| 303 | PRAGMA parser_trace=OFF; |
| 304 | } |
| 305 | } {} |
| 306 | do_test pragma-1.18 { |
| 307 | execsql { |
| 308 | PRAGMA bogus = -1234; -- Parsing of negative values |
| 309 | } |
| 310 | } {} |
| 311 | |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 312 | # Test modifying the safety_level of an attached database. |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 313 | ifcapable pager_pragmas&&attach { |
| 314 | do_test pragma-2.1 { |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 315 | forcedelete test2.db |
| 316 | forcedelete test2.db-journal |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 317 | execsql { |
| 318 | ATTACH 'test2.db' AS aux; |
| 319 | } |
| 320 | } {} |
| 321 | do_test pragma-2.2 { |
| 322 | execsql { |
| 323 | pragma aux.synchronous; |
| 324 | } |
| 325 | } {2} |
| 326 | do_test pragma-2.3 { |
| 327 | execsql { |
| 328 | pragma aux.synchronous = OFF; |
| 329 | pragma aux.synchronous; |
| 330 | pragma synchronous; |
| 331 | } |
| 332 | } {0 2} |
| 333 | do_test pragma-2.4 { |
| 334 | execsql { |
| 335 | pragma aux.synchronous = ON; |
| 336 | pragma synchronous; |
| 337 | pragma aux.synchronous; |
| 338 | } |
| 339 | } {2 1} |
danielk1977 | c7b4a44 | 2004-11-23 10:52:51 +0000 | [diff] [blame] | 340 | } ;# ifcapable pager_pragmas |
drh | 4303fee | 2003-02-15 23:09:17 +0000 | [diff] [blame] | 341 | |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 342 | # Construct a corrupted index and make sure the integrity_check |
| 343 | # pragma finds it. |
| 344 | # |
drh | 25d6543 | 2004-07-22 15:02:25 +0000 | [diff] [blame] | 345 | # These tests won't work if the database is encrypted |
| 346 | # |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 347 | do_test pragma-3.1 { |
drh | 1e9daa6 | 2007-04-06 21:42:22 +0000 | [diff] [blame] | 348 | db close |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 349 | forcedelete test.db test.db-journal |
drh | 1e9daa6 | 2007-04-06 21:42:22 +0000 | [diff] [blame] | 350 | sqlite3 db test.db |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 351 | execsql { |
drh | 1e9daa6 | 2007-04-06 21:42:22 +0000 | [diff] [blame] | 352 | PRAGMA auto_vacuum=OFF; |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 353 | BEGIN; |
| 354 | CREATE TABLE t2(a,b,c); |
| 355 | CREATE INDEX i2 ON t2(a); |
| 356 | INSERT INTO t2 VALUES(11,2,3); |
| 357 | INSERT INTO t2 VALUES(22,3,4); |
| 358 | COMMIT; |
| 359 | SELECT rowid, * from t2; |
| 360 | } |
| 361 | } {1 11 2 3 2 22 3 4} |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 362 | ifcapable attach { |
| 363 | if {![sqlite3 -has-codec] && $sqlite_options(integrityck)} { |
| 364 | do_test pragma-3.2 { |
drh | bb8a279 | 2008-03-19 00:21:30 +0000 | [diff] [blame] | 365 | db eval {SELECT rootpage FROM sqlite_master WHERE name='i2'} break |
| 366 | set pgsz [db eval {PRAGMA page_size}] |
| 367 | # overwrite the header on the rootpage of the index in order to |
| 368 | # make the index appear to be empty. |
| 369 | # |
| 370 | set offset [expr {$pgsz*($rootpage-1)}] |
| 371 | hexio_write test.db $offset 0a00000000040000000000 |
| 372 | db close |
| 373 | sqlite3 db test.db |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 374 | execsql {PRAGMA integrity_check} |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 375 | } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}} |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 376 | do_test pragma-3.3 { |
| 377 | execsql {PRAGMA integrity_check=1} |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 378 | } {{row 1 missing from index i2}} |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 379 | do_test pragma-3.4 { |
| 380 | execsql { |
| 381 | ATTACH DATABASE 'test.db' AS t2; |
| 382 | PRAGMA integrity_check |
| 383 | } |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 384 | } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}} |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 385 | do_test pragma-3.5 { |
| 386 | execsql { |
drh | bb8a279 | 2008-03-19 00:21:30 +0000 | [diff] [blame] | 387 | PRAGMA integrity_check=4 |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 388 | } |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 389 | } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2}} |
drh | 17d2d59 | 2020-07-23 00:45:06 +0000 | [diff] [blame] | 390 | do_catchsql_test pragma-3.6 { |
| 391 | PRAGMA integrity_check=xyz |
| 392 | } {1 {no such table: xyz}} |
| 393 | do_catchsql_test pragma-3.6b { |
| 394 | PRAGMA integrity_check=t2 |
| 395 | } {0 {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}} |
| 396 | do_catchsql_test pragma-3.6c { |
| 397 | PRAGMA integrity_check=sqlite_schema |
| 398 | } {0 ok} |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 399 | do_test pragma-3.7 { |
| 400 | execsql { |
| 401 | PRAGMA integrity_check=0 |
| 402 | } |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 403 | } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}} |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 404 | |
| 405 | # Add additional corruption by appending unused pages to the end of |
| 406 | # the database file testerr.db |
| 407 | # |
| 408 | do_test pragma-3.8 { |
| 409 | execsql {DETACH t2} |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 410 | forcedelete testerr.db testerr.db-journal |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 411 | set out [open testerr.db w] |
| 412 | fconfigure $out -translation binary |
| 413 | set in [open test.db r] |
| 414 | fconfigure $in -translation binary |
| 415 | puts -nonewline $out [read $in] |
| 416 | seek $in 0 |
| 417 | puts -nonewline $out [read $in] |
| 418 | close $in |
| 419 | close $out |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 420 | hexio_write testerr.db 28 00000000 |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 421 | execsql {REINDEX t2} |
| 422 | execsql {PRAGMA integrity_check} |
| 423 | } {ok} |
danielk1977 | 41c58b7 | 2007-12-29 13:39:19 +0000 | [diff] [blame] | 424 | do_test pragma-3.8.1 { |
| 425 | execsql {PRAGMA quick_check} |
| 426 | } {ok} |
drh | 5d16a9a | 2011-10-13 14:41:22 +0000 | [diff] [blame] | 427 | do_test pragma-3.8.2 { |
| 428 | execsql {PRAGMA QUICK_CHECK} |
| 429 | } {ok} |
drh | 17d2d59 | 2020-07-23 00:45:06 +0000 | [diff] [blame] | 430 | do_test pragma-3.9a { |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 431 | execsql { |
| 432 | ATTACH 'testerr.db' AS t2; |
| 433 | PRAGMA integrity_check |
| 434 | } |
| 435 | } {{*** in database t2 *** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 436 | Page 4 is never used |
| 437 | Page 5 is never used |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 438 | Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}} |
drh | 17d2d59 | 2020-07-23 00:45:06 +0000 | [diff] [blame] | 439 | do_execsql_test pragma-3.9b { |
| 440 | PRAGMA t2.integrity_check=t2; |
| 441 | } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}} |
| 442 | do_execsql_test pragma-3.9c { |
| 443 | PRAGMA t2.integrity_check=sqlite_schema; |
| 444 | } {ok} |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 445 | do_test pragma-3.10 { |
| 446 | execsql { |
| 447 | PRAGMA integrity_check=1 |
| 448 | } |
| 449 | } {{*** in database t2 *** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 450 | Page 4 is never used}} |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 451 | do_test pragma-3.11 { |
| 452 | execsql { |
| 453 | PRAGMA integrity_check=5 |
| 454 | } |
| 455 | } {{*** in database t2 *** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 456 | Page 4 is never used |
| 457 | Page 5 is never used |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 458 | Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2}} |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 459 | do_test pragma-3.12 { |
| 460 | execsql { |
| 461 | PRAGMA integrity_check=4 |
| 462 | } |
| 463 | } {{*** in database t2 *** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 464 | Page 4 is never used |
| 465 | Page 5 is never used |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 466 | Page 6 is never used} {row 1 missing from index i2}} |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 467 | do_test pragma-3.13 { |
| 468 | execsql { |
| 469 | PRAGMA integrity_check=3 |
| 470 | } |
| 471 | } {{*** in database t2 *** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 472 | Page 4 is never used |
| 473 | Page 5 is never used |
| 474 | Page 6 is never used}} |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 475 | do_test pragma-3.14 { |
| 476 | execsql { |
| 477 | PRAGMA integrity_check(2) |
| 478 | } |
| 479 | } {{*** in database t2 *** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 480 | Page 4 is never used |
| 481 | Page 5 is never used}} |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 482 | do_test pragma-3.15 { |
| 483 | execsql { |
| 484 | ATTACH 'testerr.db' AS t3; |
| 485 | PRAGMA integrity_check |
| 486 | } |
| 487 | } {{*** in database t2 *** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 488 | Page 4 is never used |
| 489 | Page 5 is never used |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 490 | Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 *** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 491 | Page 4 is never used |
| 492 | Page 5 is never used |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 493 | Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}} |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 494 | do_test pragma-3.16 { |
| 495 | execsql { |
drh | bb8a279 | 2008-03-19 00:21:30 +0000 | [diff] [blame] | 496 | PRAGMA integrity_check(10) |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 497 | } |
| 498 | } {{*** in database t2 *** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 499 | Page 4 is never used |
| 500 | Page 5 is never used |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 501 | Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 *** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 502 | Page 4 is never used |
| 503 | Page 5 is never used |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 504 | Page 6 is never used} {row 1 missing from index i2}} |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 505 | do_test pragma-3.17 { |
| 506 | execsql { |
drh | bb8a279 | 2008-03-19 00:21:30 +0000 | [diff] [blame] | 507 | PRAGMA integrity_check=8 |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 508 | } |
| 509 | } {{*** in database t2 *** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 510 | Page 4 is never used |
| 511 | Page 5 is never used |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 512 | Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 *** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 513 | Page 4 is never used |
| 514 | Page 5 is never used}} |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 515 | do_test pragma-3.18 { |
| 516 | execsql { |
| 517 | PRAGMA integrity_check=4 |
| 518 | } |
| 519 | } {{*** in database t2 *** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 520 | Page 4 is never used |
| 521 | Page 5 is never used |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 522 | Page 6 is never used} {row 1 missing from index i2}} |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 523 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 524 | do_test pragma-3.19 { |
| 525 | catch {db close} |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 526 | forcedelete test.db test.db-journal |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 527 | sqlite3 db test.db |
| 528 | db eval {PRAGMA integrity_check} |
| 529 | } {ok} |
drh | 25d6543 | 2004-07-22 15:02:25 +0000 | [diff] [blame] | 530 | } |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 531 | |
| 532 | # Verify that PRAGMA integrity_check catches UNIQUE and NOT NULL |
| 533 | # constraint violations. |
| 534 | # |
drh | 6ab91a7 | 2018-11-07 02:17:01 +0000 | [diff] [blame] | 535 | sqlite3_db_config db DEFENSIVE 0 |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 536 | do_execsql_test pragma-3.20 { |
| 537 | CREATE TABLE t1(a,b); |
| 538 | CREATE INDEX t1a ON t1(a); |
| 539 | INSERT INTO t1 VALUES(1,1),(2,2),(3,3),(2,4),(NULL,5),(NULL,6); |
| 540 | PRAGMA writable_schema=ON; |
| 541 | UPDATE sqlite_master SET sql='CREATE UNIQUE INDEX t1a ON t1(a)' |
| 542 | WHERE name='t1a'; |
| 543 | UPDATE sqlite_master SET sql='CREATE TABLE t1(a NOT NULL,b)' |
| 544 | WHERE name='t1'; |
| 545 | PRAGMA writable_schema=OFF; |
| 546 | ALTER TABLE t1 RENAME TO t1x; |
| 547 | PRAGMA integrity_check; |
| 548 | } {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a} {NULL value in t1x.a}} |
| 549 | do_execsql_test pragma-3.21 { |
| 550 | PRAGMA integrity_check(3); |
| 551 | } {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a}} |
| 552 | do_execsql_test pragma-3.22 { |
| 553 | PRAGMA integrity_check(2); |
| 554 | } {{non-unique entry in index t1a} {NULL value in t1x.a}} |
drh | 7efa426 | 2014-12-16 00:08:31 +0000 | [diff] [blame] | 555 | do_execsql_test pragma-3.23 { |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 556 | PRAGMA integrity_check(1); |
| 557 | } {{non-unique entry in index t1a}} |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 558 | |
drh | 7efa426 | 2014-12-16 00:08:31 +0000 | [diff] [blame] | 559 | # PRAGMA integrity check (or more specifically the sqlite3BtreeCount() |
| 560 | # interface) used to leave index cursors in an inconsistent state |
| 561 | # which could result in an assertion fault in sqlite3BtreeKey() |
| 562 | # called from saveCursorPosition() if content is removed from the |
| 563 | # index while the integrity_check is still running. This test verifies |
| 564 | # that problem has been fixed. |
| 565 | # |
| 566 | do_test pragma-3.30 { |
| 567 | db close |
| 568 | delete_file test.db |
| 569 | sqlite3 db test.db |
| 570 | db eval { |
| 571 | CREATE TABLE t1(a,b,c); |
| 572 | WITH RECURSIVE |
| 573 | c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<100) |
| 574 | INSERT INTO t1(a,b,c) SELECT i, printf('xyz%08x',i), 2000-i FROM c; |
| 575 | CREATE INDEX t1a ON t1(a); |
| 576 | CREATE INDEX t1bc ON t1(b,c); |
| 577 | } |
| 578 | db eval {PRAGMA integrity_check} { |
| 579 | db eval {DELETE FROM t1} |
| 580 | } |
| 581 | } {} |
| 582 | |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 583 | # Test modifying the cache_size of an attached database. |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 584 | ifcapable pager_pragmas&&attach { |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 585 | do_test pragma-4.1 { |
| 586 | execsql { |
drh | 1e9daa6 | 2007-04-06 21:42:22 +0000 | [diff] [blame] | 587 | ATTACH 'test2.db' AS aux; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 588 | pragma aux.cache_size; |
| 589 | pragma aux.default_cache_size; |
| 590 | } |
drh | 1e9daa6 | 2007-04-06 21:42:22 +0000 | [diff] [blame] | 591 | } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ] |
drh | 1bdd9b5 | 2004-04-23 17:04:44 +0000 | [diff] [blame] | 592 | do_test pragma-4.2 { |
| 593 | execsql { |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 594 | pragma aux.cache_size = 50; |
| 595 | pragma aux.cache_size; |
| 596 | pragma aux.default_cache_size; |
| 597 | } |
drh | 1e9daa6 | 2007-04-06 21:42:22 +0000 | [diff] [blame] | 598 | } [list 50 $DFLT_CACHE_SZ] |
drh | 1bdd9b5 | 2004-04-23 17:04:44 +0000 | [diff] [blame] | 599 | do_test pragma-4.3 { |
| 600 | execsql { |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 601 | pragma aux.default_cache_size = 456; |
| 602 | pragma aux.cache_size; |
| 603 | pragma aux.default_cache_size; |
| 604 | } |
| 605 | } {456 456} |
drh | 1bdd9b5 | 2004-04-23 17:04:44 +0000 | [diff] [blame] | 606 | do_test pragma-4.4 { |
| 607 | execsql { |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 608 | pragma cache_size; |
| 609 | pragma default_cache_size; |
| 610 | } |
drh | 1e9daa6 | 2007-04-06 21:42:22 +0000 | [diff] [blame] | 611 | } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ] |
drh | 1bdd9b5 | 2004-04-23 17:04:44 +0000 | [diff] [blame] | 612 | do_test pragma-4.5 { |
| 613 | execsql { |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 614 | DETACH aux; |
| 615 | ATTACH 'test3.db' AS aux; |
| 616 | pragma aux.cache_size; |
| 617 | pragma aux.default_cache_size; |
| 618 | } |
drh | 1e9daa6 | 2007-04-06 21:42:22 +0000 | [diff] [blame] | 619 | } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ] |
drh | 1bdd9b5 | 2004-04-23 17:04:44 +0000 | [diff] [blame] | 620 | do_test pragma-4.6 { |
| 621 | execsql { |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 622 | DETACH aux; |
| 623 | ATTACH 'test2.db' AS aux; |
| 624 | pragma aux.cache_size; |
| 625 | pragma aux.default_cache_size; |
| 626 | } |
| 627 | } {456 456} |
danielk1977 | c7b4a44 | 2004-11-23 10:52:51 +0000 | [diff] [blame] | 628 | } ;# ifcapable pager_pragmas |
drh | 1bdd9b5 | 2004-04-23 17:04:44 +0000 | [diff] [blame] | 629 | |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 630 | # Test that modifying the sync-level in the middle of a transaction is |
| 631 | # disallowed. |
danielk1977 | c7b4a44 | 2004-11-23 10:52:51 +0000 | [diff] [blame] | 632 | ifcapable pager_pragmas { |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 633 | do_test pragma-5.0 { |
drh | 1bdd9b5 | 2004-04-23 17:04:44 +0000 | [diff] [blame] | 634 | execsql { |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 635 | pragma synchronous; |
| 636 | } |
| 637 | } {2} |
| 638 | do_test pragma-5.1 { |
drh | 1bdd9b5 | 2004-04-23 17:04:44 +0000 | [diff] [blame] | 639 | catchsql { |
| 640 | BEGIN; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 641 | pragma synchronous = OFF; |
| 642 | } |
| 643 | } {1 {Safety level may not be changed inside a transaction}} |
| 644 | do_test pragma-5.2 { |
| 645 | execsql { |
| 646 | pragma synchronous; |
| 647 | } |
| 648 | } {2} |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 649 | catchsql {COMMIT;} |
danielk1977 | c7b4a44 | 2004-11-23 10:52:51 +0000 | [diff] [blame] | 650 | } ;# ifcapable pager_pragmas |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 651 | |
| 652 | # Test schema-query pragmas |
| 653 | # |
danielk1977 | 27188fb | 2004-11-23 10:13:03 +0000 | [diff] [blame] | 654 | ifcapable schema_pragmas { |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 655 | ifcapable tempdb&&attach { |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 656 | do_test pragma-6.1 { |
| 657 | set res {} |
| 658 | execsql {SELECT * FROM sqlite_temp_master} |
| 659 | foreach {idx name file} [execsql {pragma database_list}] { |
| 660 | lappend res $idx $name |
| 661 | } |
| 662 | set res |
| 663 | } {0 main 1 temp 2 aux} |
| 664 | } |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 665 | do_test pragma-6.2 { |
| 666 | execsql { |
drh | a6dddd9 | 2016-04-18 15:46:14 +0000 | [diff] [blame] | 667 | CREATE TABLE t2(a TYPE_X, b [TYPE_Y], c "TYPE_Z"); |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 668 | pragma table_info(t2) |
| 669 | } |
drh | a6dddd9 | 2016-04-18 15:46:14 +0000 | [diff] [blame] | 670 | } {0 a TYPE_X 0 {} 0 1 b TYPE_Y 0 {} 0 2 c TYPE_Z 0 {} 0} |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 671 | do_test pragma-6.2.1 { |
| 672 | execsql { |
| 673 | pragma table_info; |
| 674 | } |
| 675 | } {} |
drh | 736c7d4 | 2006-11-30 13:06:37 +0000 | [diff] [blame] | 676 | db nullvalue <<NULL>> |
drh | 417ec63 | 2006-08-14 14:23:41 +0000 | [diff] [blame] | 677 | do_test pragma-6.2.2 { |
| 678 | execsql { |
drh | 736c7d4 | 2006-11-30 13:06:37 +0000 | [diff] [blame] | 679 | CREATE TABLE t5( |
| 680 | a TEXT DEFAULT CURRENT_TIMESTAMP, |
| 681 | b DEFAULT (5+3), |
| 682 | c TEXT, |
| 683 | d INTEGER DEFAULT NULL, |
drh | 384b7fe | 2013-01-01 13:55:31 +0000 | [diff] [blame] | 684 | e TEXT DEFAULT '', |
| 685 | UNIQUE(b,c,d), |
| 686 | PRIMARY KEY(e,b,c) |
drh | 736c7d4 | 2006-11-30 13:06:37 +0000 | [diff] [blame] | 687 | ); |
drh | 417ec63 | 2006-08-14 14:23:41 +0000 | [diff] [blame] | 688 | PRAGMA table_info(t5); |
| 689 | } |
drh | 384b7fe | 2013-01-01 13:55:31 +0000 | [diff] [blame] | 690 | } {0 a TEXT 0 CURRENT_TIMESTAMP 0 1 b {} 0 5+3 2 2 c TEXT 0 <<NULL>> 3 3 d INTEGER 0 NULL 0 4 e TEXT 0 '' 1} |
drh | 736c7d4 | 2006-11-30 13:06:37 +0000 | [diff] [blame] | 691 | db nullvalue {} |
drh | 384b7fe | 2013-01-01 13:55:31 +0000 | [diff] [blame] | 692 | do_test pragma-6.2.3 { |
| 693 | execsql { |
| 694 | CREATE TABLE t2_3(a,b INTEGER PRIMARY KEY,c); |
| 695 | pragma table_info(t2_3) |
| 696 | } |
| 697 | } {0 a {} 0 {} 0 1 b INTEGER 0 {} 1 2 c {} 0 {} 0} |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 698 | ifcapable {foreignkey} { |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 699 | do_test pragma-6.3.1 { |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 700 | execsql { |
| 701 | CREATE TABLE t3(a int references t2(b), b UNIQUE); |
| 702 | pragma foreign_key_list(t3); |
| 703 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 704 | } {0 0 t2 a b {NO ACTION} {NO ACTION} NONE} |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 705 | do_test pragma-6.3.2 { |
| 706 | execsql { |
| 707 | pragma foreign_key_list; |
| 708 | } |
| 709 | } {} |
| 710 | do_test pragma-6.3.3 { |
| 711 | execsql { |
| 712 | pragma foreign_key_list(t3_bogus); |
| 713 | } |
| 714 | } {} |
| 715 | do_test pragma-6.3.4 { |
| 716 | execsql { |
| 717 | pragma foreign_key_list(t5); |
| 718 | } |
| 719 | } {} |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 720 | do_test pragma-6.4 { |
drh | c228be5 | 2015-01-31 02:00:01 +0000 | [diff] [blame] | 721 | capture_pragma db out { |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 722 | pragma index_list(t3); |
| 723 | } |
drh | c228be5 | 2015-01-31 02:00:01 +0000 | [diff] [blame] | 724 | db eval {SELECT seq, "name", "unique" FROM out ORDER BY seq} |
drh | 3ef2615 | 2013-10-12 20:22:00 +0000 | [diff] [blame] | 725 | } {0 sqlite_autoindex_t3_1 1} |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 726 | } |
| 727 | ifcapable {!foreignkey} { |
| 728 | execsql {CREATE TABLE t3(a,b UNIQUE)} |
| 729 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 730 | do_test pragma-6.5.1 { |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 731 | execsql { |
| 732 | CREATE INDEX t3i1 ON t3(a,b); |
drh | c228be5 | 2015-01-31 02:00:01 +0000 | [diff] [blame] | 733 | } |
| 734 | capture_pragma db out { |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 735 | pragma index_info(t3i1); |
| 736 | } |
drh | c228be5 | 2015-01-31 02:00:01 +0000 | [diff] [blame] | 737 | db eval {SELECT seqno, cid, name FROM out ORDER BY seqno} |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 738 | } {0 0 a 1 1 b} |
drh | 7be0fd9 | 2015-03-05 15:34:15 +0000 | [diff] [blame] | 739 | |
| 740 | # EVIDENCE-OF: R-23114-21695 The auxiliary index-columns are not shown |
| 741 | # by the index_info pragma, but they are listed by the index_xinfo |
| 742 | # pragma. |
| 743 | # |
| 744 | do_test pragma-6.5.1b { |
| 745 | capture_pragma db out {PRAGMA index_xinfo(t3i1)} |
| 746 | db eval {SELECT seqno, cid, name FROM out ORDER BY seqno} |
| 747 | } {0 0 a 1 1 b 2 -1 {}} |
| 748 | |
| 749 | |
drh | b3366b9 | 2015-09-11 20:54:44 +0000 | [diff] [blame] | 750 | # EVIDENCE-OF: R-29448-60346 PRAGMA schema.index_info(index-name); This |
| 751 | # pragma returns one row for each key column in the named index. |
drh | 7be0fd9 | 2015-03-05 15:34:15 +0000 | [diff] [blame] | 752 | # |
| 753 | # (The first column of output from PRAGMA index_info is...) |
| 754 | # EVIDENCE-OF: R-34186-52914 The rank of the column within the index. (0 |
| 755 | # means left-most.) |
| 756 | # |
| 757 | # (The second column of output from PRAGMA index_info is...) |
| 758 | # EVIDENCE-OF: R-65019-08383 The rank of the column within the table |
| 759 | # being indexed. |
| 760 | # |
| 761 | # (The third column of output from PRAGMA index_info is...) |
| 762 | # EVIDENCE-OF: R-09773-34266 The name of the column being indexed. |
| 763 | # |
| 764 | do_execsql_test pragma-6.5.1c { |
| 765 | CREATE INDEX t3i2 ON t3(b,a); |
| 766 | PRAGMA index_info='t3i2'; |
| 767 | DROP INDEX t3i2; |
| 768 | } {0 1 b 1 0 a} |
| 769 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 770 | do_test pragma-6.5.2 { |
| 771 | execsql { |
| 772 | pragma index_info(t3i1_bogus); |
| 773 | } |
| 774 | } {} |
danielk1977 | 260d8a6 | 2008-08-20 16:34:24 +0000 | [diff] [blame] | 775 | |
| 776 | ifcapable tempdb { |
| 777 | # Test for ticket #3320. When a temp table of the same name exists, make |
| 778 | # sure the schema of the main table can still be queried using |
| 779 | # "pragma table_info": |
| 780 | do_test pragma-6.6.1 { |
| 781 | execsql { |
| 782 | CREATE TABLE trial(col_main); |
| 783 | CREATE TEMP TABLE trial(col_temp); |
| 784 | } |
| 785 | } {} |
| 786 | do_test pragma-6.6.2 { |
| 787 | execsql { |
| 788 | PRAGMA table_info(trial); |
| 789 | } |
| 790 | } {0 col_temp {} 0 {} 0} |
| 791 | do_test pragma-6.6.3 { |
| 792 | execsql { |
| 793 | PRAGMA temp.table_info(trial); |
| 794 | } |
| 795 | } {0 col_temp {} 0 {} 0} |
| 796 | do_test pragma-6.6.4 { |
| 797 | execsql { |
| 798 | PRAGMA main.table_info(trial); |
| 799 | } |
| 800 | } {0 col_main {} 0 {} 0} |
| 801 | } |
danielk1977 | f96a377 | 2008-10-23 05:45:07 +0000 | [diff] [blame] | 802 | |
danielk1977 | f96a377 | 2008-10-23 05:45:07 +0000 | [diff] [blame] | 803 | do_test pragma-6.7 { |
| 804 | execsql { |
| 805 | CREATE TABLE test_table( |
| 806 | one INT NOT NULL DEFAULT -1, |
| 807 | two text, |
| 808 | three VARCHAR(45, 65) DEFAULT 'abcde', |
| 809 | four REAL DEFAULT X'abcdef', |
| 810 | five DEFAULT CURRENT_TIME |
| 811 | ); |
danielk1977 | f96a377 | 2008-10-23 05:45:07 +0000 | [diff] [blame] | 812 | } |
drh | c228be5 | 2015-01-31 02:00:01 +0000 | [diff] [blame] | 813 | capture_pragma db out {PRAGMA table_info(test_table)} |
| 814 | db eval {SELECT cid, "name", type, "notnull", dflt_value, pk FROM out |
| 815 | ORDER BY cid} |
danielk1977 | f96a377 | 2008-10-23 05:45:07 +0000 | [diff] [blame] | 816 | } [concat \ |
| 817 | {0 one INT 1 -1 0} \ |
| 818 | {1 two text 0 {} 0} \ |
| 819 | {2 three {VARCHAR(45, 65)} 0 'abcde' 0} \ |
| 820 | {3 four REAL 0 X'abcdef' 0} \ |
| 821 | {4 five {} 0 CURRENT_TIME 0} \ |
| 822 | ] |
drh | 1b67896 | 2015-04-15 07:19:27 +0000 | [diff] [blame] | 823 | do_test pragma-6.8 { |
| 824 | execsql { |
| 825 | CREATE TABLE t68(a,b,c,PRIMARY KEY(a,b,a,c)); |
| 826 | PRAGMA table_info(t68); |
| 827 | } |
| 828 | } [concat \ |
| 829 | {0 a {} 0 {} 1} \ |
| 830 | {1 b {} 0 {} 2} \ |
| 831 | {2 c {} 0 {} 4} \ |
| 832 | ] |
danielk1977 | 27188fb | 2004-11-23 10:13:03 +0000 | [diff] [blame] | 833 | } ;# ifcapable schema_pragmas |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 834 | # Miscellaneous tests |
| 835 | # |
danielk1977 | 27188fb | 2004-11-23 10:13:03 +0000 | [diff] [blame] | 836 | ifcapable schema_pragmas { |
drh | b3366b9 | 2015-09-11 20:54:44 +0000 | [diff] [blame] | 837 | # EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This |
| 838 | # pragma returns one row for each index associated with the given table. |
| 839 | # |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 840 | do_test pragma-7.1.1 { |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 841 | # Make sure a pragma knows to read the schema if it needs to |
| 842 | db close |
| 843 | sqlite3 db test.db |
drh | c228be5 | 2015-01-31 02:00:01 +0000 | [diff] [blame] | 844 | capture_pragma db out "PRAGMA index_list(t3)" |
| 845 | db eval {SELECT name, "origin" FROM out ORDER BY name DESC} |
| 846 | } {t3i1 c sqlite_autoindex_t3_1 u} |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 847 | do_test pragma-7.1.2 { |
| 848 | execsql { |
| 849 | pragma index_list(t3_bogus); |
| 850 | } |
| 851 | } {} |
danielk1977 | 27188fb | 2004-11-23 10:13:03 +0000 | [diff] [blame] | 852 | } ;# ifcapable schema_pragmas |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 853 | ifcapable {utf16} { |
dan | cb35460 | 2010-07-08 09:44:42 +0000 | [diff] [blame] | 854 | if {[permutation] == ""} { |
| 855 | do_test pragma-7.2 { |
| 856 | db close |
| 857 | sqlite3 db test.db |
| 858 | catchsql { |
| 859 | pragma encoding=bogus; |
| 860 | } |
| 861 | } {1 {unsupported encoding: bogus}} |
| 862 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 863 | } |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 864 | ifcapable tempdb { |
| 865 | do_test pragma-7.3 { |
| 866 | db close |
| 867 | sqlite3 db test.db |
| 868 | execsql { |
| 869 | pragma lock_status; |
| 870 | } |
| 871 | } {main unlocked temp closed} |
| 872 | } else { |
| 873 | do_test pragma-7.3 { |
| 874 | db close |
| 875 | sqlite3 db test.db |
| 876 | execsql { |
| 877 | pragma lock_status; |
| 878 | } |
| 879 | } {main unlocked} |
| 880 | } |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 881 | |
| 882 | |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 883 | #---------------------------------------------------------------------- |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 884 | # Test cases pragma-8.* test the "PRAGMA schema_version" and "PRAGMA |
| 885 | # user_version" statements. |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 886 | # |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 887 | # pragma-8.1: PRAGMA schema_version |
| 888 | # pragma-8.2: PRAGMA user_version |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 889 | # |
| 890 | |
danielk1977 | 11cf9fb | 2004-11-23 11:16:42 +0000 | [diff] [blame] | 891 | ifcapable schema_version { |
| 892 | |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 893 | # First check that we can set the schema version and then retrieve the |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 894 | # same value. |
| 895 | do_test pragma-8.1.1 { |
| 896 | execsql { |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 897 | PRAGMA schema_version = 105; |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 898 | } |
| 899 | } {} |
| 900 | do_test pragma-8.1.2 { |
drh | 2540365 | 2007-01-04 22:13:41 +0000 | [diff] [blame] | 901 | execsql2 { |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 902 | PRAGMA schema_version; |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 903 | } |
drh | 2540365 | 2007-01-04 22:13:41 +0000 | [diff] [blame] | 904 | } {schema_version 105} |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 905 | do_test pragma-8.1.3 { |
| 906 | execsql { |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 907 | PRAGMA schema_version = 106; |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 908 | } |
| 909 | } {} |
| 910 | do_test pragma-8.1.4 { |
| 911 | execsql { |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 912 | PRAGMA schema_version; |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 913 | } |
| 914 | } 106 |
| 915 | |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 916 | # Check that creating a table modifies the schema-version (this is really |
| 917 | # to verify that the value being read is in fact the schema version). |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 918 | do_test pragma-8.1.5 { |
| 919 | execsql { |
| 920 | CREATE TABLE t4(a, b, c); |
| 921 | INSERT INTO t4 VALUES(1, 2, 3); |
| 922 | SELECT * FROM t4; |
| 923 | } |
| 924 | } {1 2 3} |
| 925 | do_test pragma-8.1.6 { |
| 926 | execsql { |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 927 | PRAGMA schema_version; |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 928 | } |
| 929 | } 107 |
| 930 | |
| 931 | # Now open a second connection to the database. Ensure that changing the |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 932 | # schema-version using the first connection forces the second connection |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 933 | # to reload the schema. This has to be done using the C-API test functions, |
| 934 | # because the TCL API accounts for SCHEMA_ERROR and retries the query. |
| 935 | do_test pragma-8.1.7 { |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 936 | sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2] |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 937 | execsql { |
| 938 | SELECT * FROM t4; |
| 939 | } db2 |
| 940 | } {1 2 3} |
| 941 | do_test pragma-8.1.8 { |
| 942 | execsql { |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 943 | PRAGMA schema_version = 108; |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 944 | } |
| 945 | } {} |
| 946 | do_test pragma-8.1.9 { |
| 947 | set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM t4" -1 DUMMY] |
| 948 | sqlite3_step $::STMT |
| 949 | } SQLITE_ERROR |
| 950 | do_test pragma-8.1.10 { |
| 951 | sqlite3_finalize $::STMT |
| 952 | } SQLITE_SCHEMA |
| 953 | |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 954 | # Make sure the schema-version can be manipulated in an attached database. |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 955 | forcedelete test2.db |
| 956 | forcedelete test2.db-journal |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 957 | ifcapable attach { |
| 958 | do_test pragma-8.1.11 { |
| 959 | execsql { |
| 960 | ATTACH 'test2.db' AS aux; |
| 961 | CREATE TABLE aux.t1(a, b, c); |
| 962 | PRAGMA aux.schema_version = 205; |
| 963 | } |
| 964 | } {} |
| 965 | do_test pragma-8.1.12 { |
| 966 | execsql { |
| 967 | PRAGMA aux.schema_version; |
| 968 | } |
| 969 | } 205 |
| 970 | } |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 971 | do_test pragma-8.1.13 { |
| 972 | execsql { |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 973 | PRAGMA schema_version; |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 974 | } |
| 975 | } 108 |
| 976 | |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 977 | # And check that modifying the schema-version in an attached database |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 978 | # forces the second connection to reload the schema. |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 979 | ifcapable attach { |
| 980 | do_test pragma-8.1.14 { |
| 981 | sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2] |
| 982 | execsql { |
| 983 | ATTACH 'test2.db' AS aux; |
| 984 | SELECT * FROM aux.t1; |
| 985 | } db2 |
| 986 | } {} |
| 987 | do_test pragma-8.1.15 { |
| 988 | execsql { |
| 989 | PRAGMA aux.schema_version = 206; |
| 990 | } |
| 991 | } {} |
| 992 | do_test pragma-8.1.16 { |
| 993 | set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM aux.t1" -1 DUMMY] |
| 994 | sqlite3_step $::STMT |
| 995 | } SQLITE_ERROR |
| 996 | do_test pragma-8.1.17 { |
| 997 | sqlite3_finalize $::STMT |
| 998 | } SQLITE_SCHEMA |
| 999 | do_test pragma-8.1.18 { |
| 1000 | db2 close |
| 1001 | } {} |
| 1002 | } |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1003 | |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 1004 | # Now test that the user-version can be read and written (and that we aren't |
| 1005 | # accidentally manipulating the schema-version instead). |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1006 | do_test pragma-8.2.1 { |
drh | 2540365 | 2007-01-04 22:13:41 +0000 | [diff] [blame] | 1007 | execsql2 { |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 1008 | PRAGMA user_version; |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1009 | } |
drh | 2540365 | 2007-01-04 22:13:41 +0000 | [diff] [blame] | 1010 | } {user_version 0} |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1011 | do_test pragma-8.2.2 { |
| 1012 | execsql { |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 1013 | PRAGMA user_version = 2; |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1014 | } |
| 1015 | } {} |
drh | 802d69a | 2005-02-13 23:34:24 +0000 | [diff] [blame] | 1016 | do_test pragma-8.2.3.1 { |
drh | 2540365 | 2007-01-04 22:13:41 +0000 | [diff] [blame] | 1017 | execsql2 { |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 1018 | PRAGMA user_version; |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1019 | } |
drh | 2540365 | 2007-01-04 22:13:41 +0000 | [diff] [blame] | 1020 | } {user_version 2} |
drh | 802d69a | 2005-02-13 23:34:24 +0000 | [diff] [blame] | 1021 | do_test pragma-8.2.3.2 { |
| 1022 | db close |
| 1023 | sqlite3 db test.db |
| 1024 | execsql { |
| 1025 | PRAGMA user_version; |
| 1026 | } |
| 1027 | } {2} |
| 1028 | do_test pragma-8.2.4.1 { |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1029 | execsql { |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 1030 | PRAGMA schema_version; |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1031 | } |
| 1032 | } {108} |
drh | 802d69a | 2005-02-13 23:34:24 +0000 | [diff] [blame] | 1033 | ifcapable vacuum { |
| 1034 | do_test pragma-8.2.4.2 { |
| 1035 | execsql { |
| 1036 | VACUUM; |
| 1037 | PRAGMA user_version; |
| 1038 | } |
| 1039 | } {2} |
| 1040 | do_test pragma-8.2.4.3 { |
| 1041 | execsql { |
| 1042 | PRAGMA schema_version; |
| 1043 | } |
| 1044 | } {109} |
| 1045 | } |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1046 | |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 1047 | ifcapable attach { |
| 1048 | db eval {ATTACH 'test2.db' AS aux} |
| 1049 | |
| 1050 | # Check that the user-version in the auxilary database can be manipulated ( |
| 1051 | # and that we aren't accidentally manipulating the same in the main db). |
| 1052 | do_test pragma-8.2.5 { |
| 1053 | execsql { |
| 1054 | PRAGMA aux.user_version; |
| 1055 | } |
| 1056 | } {0} |
| 1057 | do_test pragma-8.2.6 { |
| 1058 | execsql { |
| 1059 | PRAGMA aux.user_version = 3; |
| 1060 | } |
| 1061 | } {} |
| 1062 | do_test pragma-8.2.7 { |
| 1063 | execsql { |
| 1064 | PRAGMA aux.user_version; |
| 1065 | } |
| 1066 | } {3} |
| 1067 | do_test pragma-8.2.8 { |
| 1068 | execsql { |
| 1069 | PRAGMA main.user_version; |
| 1070 | } |
| 1071 | } {2} |
| 1072 | |
| 1073 | # Now check that a ROLLBACK resets the user-version if it has been modified |
| 1074 | # within a transaction. |
| 1075 | do_test pragma-8.2.9 { |
| 1076 | execsql { |
| 1077 | BEGIN; |
| 1078 | PRAGMA aux.user_version = 10; |
| 1079 | PRAGMA user_version = 11; |
| 1080 | } |
| 1081 | } {} |
| 1082 | do_test pragma-8.2.10 { |
| 1083 | execsql { |
| 1084 | PRAGMA aux.user_version; |
| 1085 | } |
| 1086 | } {10} |
| 1087 | do_test pragma-8.2.11 { |
| 1088 | execsql { |
| 1089 | PRAGMA main.user_version; |
| 1090 | } |
| 1091 | } {11} |
| 1092 | do_test pragma-8.2.12 { |
| 1093 | execsql { |
| 1094 | ROLLBACK; |
| 1095 | PRAGMA aux.user_version; |
| 1096 | } |
| 1097 | } {3} |
| 1098 | do_test pragma-8.2.13 { |
| 1099 | execsql { |
| 1100 | PRAGMA main.user_version; |
| 1101 | } |
| 1102 | } {2} |
| 1103 | } |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1104 | |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 1105 | # Try a negative value for the user-version |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1106 | do_test pragma-8.2.14 { |
| 1107 | execsql { |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 1108 | PRAGMA user_version = -450; |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1109 | } |
| 1110 | } {} |
| 1111 | do_test pragma-8.2.15 { |
| 1112 | execsql { |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 1113 | PRAGMA user_version; |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1114 | } |
| 1115 | } {-450} |
danielk1977 | d9c847d | 2005-01-07 10:42:48 +0000 | [diff] [blame] | 1116 | } ; # ifcapable schema_version |
| 1117 | |
drh | d19744f | 2008-03-18 13:46:53 +0000 | [diff] [blame] | 1118 | # Check to see if TEMP_STORE is memory or disk. Return strings |
| 1119 | # "memory" or "disk" as appropriate. |
| 1120 | # |
| 1121 | proc check_temp_store {} { |
dan | 9131ab9 | 2016-04-06 18:20:51 +0000 | [diff] [blame] | 1122 | db eval { |
| 1123 | PRAGMA temp.cache_size = 1; |
| 1124 | CREATE TEMP TABLE IF NOT EXISTS a(b); |
| 1125 | DELETE FROM a; |
| 1126 | INSERT INTO a VALUES(randomblob(1000)); |
| 1127 | INSERT INTO a SELECT * FROM a; |
| 1128 | INSERT INTO a SELECT * FROM a; |
| 1129 | INSERT INTO a SELECT * FROM a; |
| 1130 | INSERT INTO a SELECT * FROM a; |
| 1131 | INSERT INTO a SELECT * FROM a; |
| 1132 | INSERT INTO a SELECT * FROM a; |
| 1133 | INSERT INTO a SELECT * FROM a; |
| 1134 | INSERT INTO a SELECT * FROM a; |
| 1135 | } |
drh | d19744f | 2008-03-18 13:46:53 +0000 | [diff] [blame] | 1136 | db eval {PRAGMA database_list} { |
| 1137 | if {$name=="temp"} { |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1138 | set bt [btree_from_db db 1] |
| 1139 | if {[btree_ismemdb $bt]} { |
drh | d19744f | 2008-03-18 13:46:53 +0000 | [diff] [blame] | 1140 | return "memory" |
drh | d19744f | 2008-03-18 13:46:53 +0000 | [diff] [blame] | 1141 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1142 | return "disk" |
drh | d19744f | 2008-03-18 13:46:53 +0000 | [diff] [blame] | 1143 | } |
| 1144 | } |
| 1145 | return "unknown" |
| 1146 | } |
| 1147 | |
drh | 4ee09b4 | 2013-05-01 19:49:27 +0000 | [diff] [blame] | 1148 | # Application_ID |
| 1149 | # |
| 1150 | do_test pragma-8.3.1 { |
| 1151 | execsql { |
| 1152 | PRAGMA application_id; |
| 1153 | } |
| 1154 | } {0} |
| 1155 | do_test pragma-8.3.2 { |
| 1156 | execsql {PRAGMA Application_ID(12345); PRAGMA application_id;} |
| 1157 | } {12345} |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 1158 | |
| 1159 | # Test temp_store and temp_store_directory pragmas |
| 1160 | # |
drh | 268283b | 2005-01-08 15:44:25 +0000 | [diff] [blame] | 1161 | ifcapable pager_pragmas { |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 1162 | do_test pragma-9.1 { |
| 1163 | db close |
| 1164 | sqlite3 db test.db |
| 1165 | execsql { |
| 1166 | PRAGMA temp_store; |
| 1167 | } |
| 1168 | } {0} |
drh | d19744f | 2008-03-18 13:46:53 +0000 | [diff] [blame] | 1169 | if {$TEMP_STORE<=1} { |
| 1170 | do_test pragma-9.1.1 { |
| 1171 | check_temp_store |
| 1172 | } {disk} |
| 1173 | } else { |
| 1174 | do_test pragma-9.1.1 { |
| 1175 | check_temp_store |
| 1176 | } {memory} |
| 1177 | } |
| 1178 | |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 1179 | do_test pragma-9.2 { |
drh | d19744f | 2008-03-18 13:46:53 +0000 | [diff] [blame] | 1180 | db close |
| 1181 | sqlite3 db test.db |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 1182 | execsql { |
| 1183 | PRAGMA temp_store=file; |
| 1184 | PRAGMA temp_store; |
| 1185 | } |
| 1186 | } {1} |
drh | d19744f | 2008-03-18 13:46:53 +0000 | [diff] [blame] | 1187 | if {$TEMP_STORE==3} { |
| 1188 | # When TEMP_STORE is 3, always use memory regardless of pragma settings. |
| 1189 | do_test pragma-9.2.1 { |
| 1190 | check_temp_store |
| 1191 | } {memory} |
| 1192 | } else { |
| 1193 | do_test pragma-9.2.1 { |
| 1194 | check_temp_store |
| 1195 | } {disk} |
| 1196 | } |
| 1197 | |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 1198 | do_test pragma-9.3 { |
drh | d19744f | 2008-03-18 13:46:53 +0000 | [diff] [blame] | 1199 | db close |
| 1200 | sqlite3 db test.db |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 1201 | execsql { |
| 1202 | PRAGMA temp_store=memory; |
| 1203 | PRAGMA temp_store; |
| 1204 | } |
| 1205 | } {2} |
drh | d19744f | 2008-03-18 13:46:53 +0000 | [diff] [blame] | 1206 | if {$TEMP_STORE==0} { |
| 1207 | # When TEMP_STORE is 0, always use the disk regardless of pragma settings. |
| 1208 | do_test pragma-9.3.1 { |
| 1209 | check_temp_store |
| 1210 | } {disk} |
| 1211 | } else { |
| 1212 | do_test pragma-9.3.1 { |
| 1213 | check_temp_store |
| 1214 | } {memory} |
| 1215 | } |
| 1216 | |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 1217 | do_test pragma-9.4 { |
| 1218 | execsql { |
| 1219 | PRAGMA temp_store_directory; |
| 1220 | } |
| 1221 | } {} |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1222 | ifcapable wsd { |
| 1223 | do_test pragma-9.5 { |
mistachkin | f8a7846 | 2012-03-08 20:00:36 +0000 | [diff] [blame] | 1224 | set pwd [string map {' ''} [file nativename [get_pwd]]] |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1225 | execsql " |
| 1226 | PRAGMA temp_store_directory='$pwd'; |
| 1227 | " |
| 1228 | } {} |
| 1229 | do_test pragma-9.6 { |
| 1230 | execsql { |
| 1231 | PRAGMA temp_store_directory; |
| 1232 | } |
mistachkin | f8a7846 | 2012-03-08 20:00:36 +0000 | [diff] [blame] | 1233 | } [list [file nativename [get_pwd]]] |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1234 | do_test pragma-9.7 { |
| 1235 | catchsql { |
| 1236 | PRAGMA temp_store_directory='/NON/EXISTENT/PATH/FOOBAR'; |
| 1237 | } |
| 1238 | } {1 {not a writable directory}} |
| 1239 | do_test pragma-9.8 { |
| 1240 | execsql { |
| 1241 | PRAGMA temp_store_directory=''; |
| 1242 | } |
| 1243 | } {} |
| 1244 | if {![info exists TEMP_STORE] || $TEMP_STORE<=1} { |
| 1245 | ifcapable tempdb { |
| 1246 | do_test pragma-9.9 { |
| 1247 | execsql { |
| 1248 | PRAGMA temp_store_directory; |
| 1249 | PRAGMA temp_store=FILE; |
| 1250 | CREATE TEMP TABLE temp_store_directory_test(a integer); |
| 1251 | INSERT INTO temp_store_directory_test values (2); |
| 1252 | SELECT * FROM temp_store_directory_test; |
| 1253 | } |
| 1254 | } {2} |
| 1255 | do_test pragma-9.10 { |
| 1256 | catchsql " |
| 1257 | PRAGMA temp_store_directory='$pwd'; |
| 1258 | SELECT * FROM temp_store_directory_test; |
| 1259 | " |
| 1260 | } {1 {no such table: temp_store_directory_test}} |
| 1261 | } |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 1262 | } |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1263 | } |
danielk1977 | 95b289b | 2007-03-30 17:11:12 +0000 | [diff] [blame] | 1264 | do_test pragma-9.11 { |
| 1265 | execsql { |
| 1266 | PRAGMA temp_store = 0; |
| 1267 | PRAGMA temp_store; |
| 1268 | } |
| 1269 | } {0} |
| 1270 | do_test pragma-9.12 { |
| 1271 | execsql { |
| 1272 | PRAGMA temp_store = 1; |
| 1273 | PRAGMA temp_store; |
| 1274 | } |
| 1275 | } {1} |
| 1276 | do_test pragma-9.13 { |
| 1277 | execsql { |
| 1278 | PRAGMA temp_store = 2; |
| 1279 | PRAGMA temp_store; |
| 1280 | } |
| 1281 | } {2} |
| 1282 | do_test pragma-9.14 { |
| 1283 | execsql { |
| 1284 | PRAGMA temp_store = 3; |
| 1285 | PRAGMA temp_store; |
| 1286 | } |
| 1287 | } {0} |
danielk1977 | 95b289b | 2007-03-30 17:11:12 +0000 | [diff] [blame] | 1288 | do_test pragma-9.15 { |
| 1289 | catchsql { |
| 1290 | BEGIN EXCLUSIVE; |
| 1291 | CREATE TEMP TABLE temp_table(t); |
| 1292 | INSERT INTO temp_table VALUES('valuable data'); |
| 1293 | PRAGMA temp_store = 1; |
| 1294 | } |
| 1295 | } {1 {temporary storage cannot be changed from within a transaction}} |
| 1296 | do_test pragma-9.16 { |
| 1297 | execsql { |
| 1298 | SELECT * FROM temp_table; |
| 1299 | COMMIT; |
| 1300 | } |
| 1301 | } {{valuable data}} |
danielk1977 | 983e230 | 2008-07-08 07:35:51 +0000 | [diff] [blame] | 1302 | |
| 1303 | do_test pragma-9.17 { |
| 1304 | execsql { |
| 1305 | INSERT INTO temp_table VALUES('valuable data II'); |
| 1306 | SELECT * FROM temp_table; |
| 1307 | } |
| 1308 | } {{valuable data} {valuable data II}} |
| 1309 | |
| 1310 | do_test pragma-9.18 { |
| 1311 | set rc [catch { |
| 1312 | db eval {SELECT t FROM temp_table} { |
| 1313 | execsql {pragma temp_store = 1} |
| 1314 | } |
| 1315 | } msg] |
| 1316 | list $rc $msg |
| 1317 | } {1 {temporary storage cannot be changed from within a transaction}} |
| 1318 | |
drh | 268283b | 2005-01-08 15:44:25 +0000 | [diff] [blame] | 1319 | } ;# ifcapable pager_pragmas |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 1320 | |
danielk1977 | cc6bd38 | 2005-01-10 02:48:49 +0000 | [diff] [blame] | 1321 | ifcapable trigger { |
| 1322 | |
| 1323 | do_test pragma-10.0 { |
| 1324 | catchsql { |
| 1325 | DROP TABLE main.t1; |
| 1326 | } |
| 1327 | execsql { |
| 1328 | PRAGMA count_changes = 1; |
| 1329 | |
| 1330 | CREATE TABLE t1(a PRIMARY KEY); |
| 1331 | CREATE TABLE t1_mirror(a); |
| 1332 | CREATE TABLE t1_mirror2(a); |
| 1333 | CREATE TRIGGER t1_bi BEFORE INSERT ON t1 BEGIN |
| 1334 | INSERT INTO t1_mirror VALUES(new.a); |
| 1335 | END; |
| 1336 | CREATE TRIGGER t1_ai AFTER INSERT ON t1 BEGIN |
| 1337 | INSERT INTO t1_mirror2 VALUES(new.a); |
| 1338 | END; |
| 1339 | CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 BEGIN |
| 1340 | UPDATE t1_mirror SET a = new.a WHERE a = old.a; |
| 1341 | END; |
| 1342 | CREATE TRIGGER t1_au AFTER UPDATE ON t1 BEGIN |
| 1343 | UPDATE t1_mirror2 SET a = new.a WHERE a = old.a; |
| 1344 | END; |
| 1345 | CREATE TRIGGER t1_bd BEFORE DELETE ON t1 BEGIN |
| 1346 | DELETE FROM t1_mirror WHERE a = old.a; |
| 1347 | END; |
| 1348 | CREATE TRIGGER t1_ad AFTER DELETE ON t1 BEGIN |
| 1349 | DELETE FROM t1_mirror2 WHERE a = old.a; |
| 1350 | END; |
| 1351 | } |
| 1352 | } {} |
| 1353 | |
| 1354 | do_test pragma-10.1 { |
| 1355 | execsql { |
| 1356 | INSERT INTO t1 VALUES(randstr(10,10)); |
| 1357 | } |
| 1358 | } {1} |
| 1359 | do_test pragma-10.2 { |
| 1360 | execsql { |
| 1361 | UPDATE t1 SET a = randstr(10,10); |
| 1362 | } |
| 1363 | } {1} |
| 1364 | do_test pragma-10.3 { |
| 1365 | execsql { |
| 1366 | DELETE FROM t1; |
| 1367 | } |
| 1368 | } {1} |
| 1369 | |
| 1370 | } ;# ifcapable trigger |
| 1371 | |
danielk1977 | 48af65a | 2005-02-09 03:20:37 +0000 | [diff] [blame] | 1372 | ifcapable schema_pragmas { |
| 1373 | do_test pragma-11.1 { |
| 1374 | execsql2 { |
| 1375 | pragma collation_list; |
| 1376 | } |
drh | 2d82331 | 2014-11-20 23:11:30 +0000 | [diff] [blame] | 1377 | } {seq 0 name RTRIM seq 1 name NOCASE seq 2 name BINARY} |
danielk1977 | 48af65a | 2005-02-09 03:20:37 +0000 | [diff] [blame] | 1378 | do_test pragma-11.2 { |
| 1379 | db collate New_Collation blah... |
| 1380 | execsql { |
| 1381 | pragma collation_list; |
| 1382 | } |
drh | 2d82331 | 2014-11-20 23:11:30 +0000 | [diff] [blame] | 1383 | } {0 New_Collation 1 RTRIM 2 NOCASE 3 BINARY} |
danielk1977 | 48af65a | 2005-02-09 03:20:37 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
danielk1977 | ddfb2f0 | 2006-02-17 12:25:14 +0000 | [diff] [blame] | 1386 | ifcapable schema_pragmas&&tempdb { |
| 1387 | do_test pragma-12.1 { |
| 1388 | sqlite3 db2 test.db |
| 1389 | execsql { |
| 1390 | PRAGMA temp.table_info('abc'); |
| 1391 | } db2 |
| 1392 | } {} |
| 1393 | db2 close |
| 1394 | |
| 1395 | do_test pragma-12.2 { |
| 1396 | sqlite3 db2 test.db |
| 1397 | execsql { |
| 1398 | PRAGMA temp.default_cache_size = 200; |
| 1399 | PRAGMA temp.default_cache_size; |
| 1400 | } db2 |
| 1401 | } {200} |
| 1402 | db2 close |
| 1403 | |
| 1404 | do_test pragma-12.3 { |
| 1405 | sqlite3 db2 test.db |
| 1406 | execsql { |
| 1407 | PRAGMA temp.cache_size = 400; |
| 1408 | PRAGMA temp.cache_size; |
| 1409 | } db2 |
| 1410 | } {400} |
| 1411 | db2 close |
| 1412 | } |
| 1413 | |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 1414 | ifcapable bloblit { |
| 1415 | |
drh | 05a8298 | 2006-03-19 13:00:25 +0000 | [diff] [blame] | 1416 | do_test pragma-13.1 { |
| 1417 | execsql { |
| 1418 | DROP TABLE IF EXISTS t4; |
| 1419 | PRAGMA vdbe_trace=on; |
| 1420 | PRAGMA vdbe_listing=on; |
| 1421 | PRAGMA sql_trace=on; |
| 1422 | CREATE TABLE t4(a INTEGER PRIMARY KEY,b); |
| 1423 | INSERT INTO t4(b) VALUES(x'0123456789abcdef0123456789abcdef0123456789'); |
| 1424 | INSERT INTO t4(b) VALUES(randstr(30,30)); |
| 1425 | INSERT INTO t4(b) VALUES(1.23456); |
| 1426 | INSERT INTO t4(b) VALUES(NULL); |
| 1427 | INSERT INTO t4(b) VALUES(0); |
| 1428 | INSERT INTO t4(b) SELECT b||b||b||b FROM t4; |
| 1429 | SELECT * FROM t4; |
| 1430 | } |
| 1431 | execsql { |
| 1432 | PRAGMA vdbe_trace=off; |
| 1433 | PRAGMA vdbe_listing=off; |
| 1434 | PRAGMA sql_trace=off; |
| 1435 | } |
| 1436 | } {} |
| 1437 | |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 1438 | } ;# ifcapable bloblit |
| 1439 | |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 1440 | ifcapable pager_pragmas { |
| 1441 | db close |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 1442 | forcedelete test.db |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 1443 | sqlite3 db test.db |
drh | 51a74d4 | 2015-02-28 01:04:27 +0000 | [diff] [blame] | 1444 | |
drh | b3366b9 | 2015-09-11 20:54:44 +0000 | [diff] [blame] | 1445 | # EVIDENCE-OF: R-15672-33611 PRAGMA schema.page_count; Return the total |
| 1446 | # number of pages in the database file. |
drh | 51a74d4 | 2015-02-28 01:04:27 +0000 | [diff] [blame] | 1447 | # |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 1448 | do_test pragma-14.1 { |
| 1449 | execsql { pragma auto_vacuum = 0 } |
drh | 51a74d4 | 2015-02-28 01:04:27 +0000 | [diff] [blame] | 1450 | execsql { pragma page_count; pragma main.page_count } |
| 1451 | } {0 0} |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 1452 | |
| 1453 | do_test pragma-14.2 { |
| 1454 | execsql { |
| 1455 | CREATE TABLE abc(a, b, c); |
| 1456 | PRAGMA page_count; |
drh | 51a74d4 | 2015-02-28 01:04:27 +0000 | [diff] [blame] | 1457 | PRAGMA main.page_count; |
| 1458 | PRAGMA temp.page_count; |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 1459 | } |
drh | 51a74d4 | 2015-02-28 01:04:27 +0000 | [diff] [blame] | 1460 | } {2 2 0} |
drh | 5d16a9a | 2011-10-13 14:41:22 +0000 | [diff] [blame] | 1461 | do_test pragma-14.2uc { |
| 1462 | execsql {pragma PAGE_COUNT} |
| 1463 | } {2} |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 1464 | |
| 1465 | do_test pragma-14.3 { |
| 1466 | execsql { |
| 1467 | BEGIN; |
| 1468 | CREATE TABLE def(a, b, c); |
| 1469 | PRAGMA page_count; |
| 1470 | } |
| 1471 | } {3} |
drh | 5d16a9a | 2011-10-13 14:41:22 +0000 | [diff] [blame] | 1472 | do_test pragma-14.3uc { |
| 1473 | execsql {pragma PAGE_COUNT} |
| 1474 | } {3} |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 1475 | |
| 1476 | do_test pragma-14.4 { |
| 1477 | set page_size [db one {pragma page_size}] |
| 1478 | expr [file size test.db] / $page_size |
| 1479 | } {2} |
| 1480 | |
| 1481 | do_test pragma-14.5 { |
| 1482 | execsql { |
| 1483 | ROLLBACK; |
| 1484 | PRAGMA page_count; |
| 1485 | } |
| 1486 | } {2} |
| 1487 | |
| 1488 | do_test pragma-14.6 { |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 1489 | forcedelete test2.db |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 1490 | sqlite3 db2 test2.db |
| 1491 | execsql { |
| 1492 | PRAGMA auto_vacuum = 0; |
| 1493 | CREATE TABLE t1(a, b, c); |
| 1494 | CREATE TABLE t2(a, b, c); |
| 1495 | CREATE TABLE t3(a, b, c); |
| 1496 | CREATE TABLE t4(a, b, c); |
| 1497 | } db2 |
| 1498 | db2 close |
| 1499 | execsql { |
| 1500 | ATTACH 'test2.db' AS aux; |
| 1501 | PRAGMA aux.page_count; |
| 1502 | } |
| 1503 | } {5} |
drh | 5d16a9a | 2011-10-13 14:41:22 +0000 | [diff] [blame] | 1504 | do_test pragma-14.6uc { |
| 1505 | execsql {pragma AUX.PAGE_COUNT} |
| 1506 | } {5} |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
danielk1977 | 8cf6c55 | 2008-06-23 16:53:46 +0000 | [diff] [blame] | 1509 | # Test that the value set using the cache_size pragma is not reset when the |
| 1510 | # schema is reloaded. |
| 1511 | # |
| 1512 | ifcapable pager_pragmas { |
| 1513 | db close |
| 1514 | sqlite3 db test.db |
| 1515 | do_test pragma-15.1 { |
| 1516 | execsql { |
| 1517 | PRAGMA cache_size=59; |
| 1518 | PRAGMA cache_size; |
| 1519 | } |
| 1520 | } {59} |
| 1521 | do_test pragma-15.2 { |
| 1522 | sqlite3 db2 test.db |
| 1523 | execsql { |
| 1524 | CREATE TABLE newtable(a, b, c); |
| 1525 | } db2 |
| 1526 | db2 close |
| 1527 | } {} |
| 1528 | do_test pragma-15.3 { |
| 1529 | # Evaluating this statement will cause the schema to be reloaded (because |
| 1530 | # the schema was changed by another connection in pragma-15.2). At one |
| 1531 | # point there was a bug that reset the cache_size to its default value |
| 1532 | # when this happened. |
| 1533 | execsql { SELECT * FROM sqlite_master } |
| 1534 | execsql { PRAGMA cache_size } |
| 1535 | } {59} |
| 1536 | } |
| 1537 | |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 1538 | # Reset the sqlite3_temp_directory variable for the next run of tests: |
| 1539 | sqlite3 dbX :memory: |
| 1540 | dbX eval {PRAGMA temp_store_directory = ""} |
| 1541 | dbX close |
| 1542 | |
danielk1977 | 838cce4 | 2009-01-12 14:01:45 +0000 | [diff] [blame] | 1543 | ifcapable lock_proxy_pragmas&&prefer_proxy_locking { |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1544 | set sqlite_hostid_num 1 |
| 1545 | |
| 1546 | set using_proxy 0 |
| 1547 | foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] { |
| 1548 | set using_proxy $value |
| 1549 | } |
| 1550 | |
| 1551 | # Test the lock_proxy_file pragmas. |
| 1552 | # |
| 1553 | db close |
| 1554 | set env(SQLITE_FORCE_PROXY_LOCKING) "0" |
| 1555 | |
| 1556 | sqlite3 db test.db |
| 1557 | do_test pragma-16.1 { |
| 1558 | execsql { |
| 1559 | PRAGMA lock_proxy_file="mylittleproxy"; |
| 1560 | select * from sqlite_master; |
| 1561 | } |
| 1562 | execsql { |
| 1563 | PRAGMA lock_proxy_file; |
| 1564 | } |
| 1565 | } {mylittleproxy} |
| 1566 | |
| 1567 | do_test pragma-16.2 { |
| 1568 | sqlite3 db2 test.db |
| 1569 | execsql { |
| 1570 | PRAGMA lock_proxy_file="mylittleproxy"; |
| 1571 | } db2 |
| 1572 | } {} |
| 1573 | |
| 1574 | db2 close |
| 1575 | do_test pragma-16.2.1 { |
| 1576 | sqlite3 db2 test.db |
| 1577 | execsql { |
| 1578 | PRAGMA lock_proxy_file=":auto:"; |
| 1579 | select * from sqlite_master; |
| 1580 | } db2 |
| 1581 | execsql { |
| 1582 | PRAGMA lock_proxy_file; |
| 1583 | } db2 |
| 1584 | } {mylittleproxy} |
| 1585 | |
| 1586 | db2 close |
| 1587 | do_test pragma-16.3 { |
| 1588 | sqlite3 db2 test.db |
| 1589 | execsql { |
| 1590 | PRAGMA lock_proxy_file="myotherproxy"; |
| 1591 | } db2 |
| 1592 | catchsql { |
| 1593 | select * from sqlite_master; |
| 1594 | } db2 |
| 1595 | } {1 {database is locked}} |
| 1596 | |
| 1597 | do_test pragma-16.4 { |
| 1598 | db2 close |
| 1599 | db close |
| 1600 | sqlite3 db2 test.db |
| 1601 | execsql { |
| 1602 | PRAGMA lock_proxy_file="myoriginalproxy"; |
| 1603 | PRAGMA lock_proxy_file="myotherproxy"; |
| 1604 | PRAGMA lock_proxy_file; |
| 1605 | } db2 |
| 1606 | } {myotherproxy} |
| 1607 | |
| 1608 | db2 close |
| 1609 | set env(SQLITE_FORCE_PROXY_LOCKING) "1" |
| 1610 | do_test pragma-16.5 { |
| 1611 | sqlite3 db2 test.db |
| 1612 | execsql { |
| 1613 | PRAGMA lock_proxy_file=":auto:"; |
| 1614 | PRAGMA lock_proxy_file; |
| 1615 | } db2 |
| 1616 | } {myotherproxy} |
| 1617 | |
| 1618 | do_test pragma-16.6 { |
| 1619 | db2 close |
| 1620 | sqlite3 db2 test2.db |
| 1621 | set lockpath [execsql { |
| 1622 | PRAGMA lock_proxy_file=":auto:"; |
| 1623 | PRAGMA lock_proxy_file; |
| 1624 | } db2] |
| 1625 | string match "*test2.db:auto:" $lockpath |
| 1626 | } {1} |
| 1627 | |
| 1628 | set sqlite_hostid_num 2 |
| 1629 | do_test pragma-16.7 { |
dan | 14d1460 | 2010-10-06 16:42:52 +0000 | [diff] [blame] | 1630 | list [catch { |
| 1631 | sqlite3 db test2.db |
| 1632 | execsql { |
| 1633 | PRAGMA lock_proxy_file=":auto:"; |
| 1634 | select * from sqlite_master; |
| 1635 | } |
| 1636 | } msg] $msg |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1637 | } {1 {database is locked}} |
| 1638 | db close |
| 1639 | |
| 1640 | do_test pragma-16.8 { |
dan | 14d1460 | 2010-10-06 16:42:52 +0000 | [diff] [blame] | 1641 | list [catch { |
| 1642 | sqlite3 db test2.db |
| 1643 | execsql { select * from sqlite_master } |
| 1644 | } msg] $msg |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1645 | } {1 {database is locked}} |
| 1646 | |
| 1647 | db2 close |
| 1648 | do_test pragma-16.8.1 { |
| 1649 | execsql { |
| 1650 | PRAGMA lock_proxy_file="yetanotherproxy"; |
| 1651 | PRAGMA lock_proxy_file; |
| 1652 | } |
| 1653 | } {yetanotherproxy} |
| 1654 | do_test pragma-16.8.2 { |
| 1655 | execsql { |
| 1656 | create table mine(x); |
| 1657 | } |
| 1658 | } {} |
| 1659 | |
| 1660 | db close |
| 1661 | do_test pragma-16.9 { |
| 1662 | sqlite3 db proxytest.db |
| 1663 | set lockpath2 [execsql { |
| 1664 | PRAGMA lock_proxy_file=":auto:"; |
| 1665 | PRAGMA lock_proxy_file; |
| 1666 | } db] |
| 1667 | string match "*proxytest.db:auto:" $lockpath2 |
| 1668 | } {1} |
| 1669 | |
| 1670 | set env(SQLITE_FORCE_PROXY_LOCKING) $using_proxy |
| 1671 | set sqlite_hostid_num 0 |
| 1672 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 1673 | |
| 1674 | # Parsing of auto_vacuum settings. |
| 1675 | # |
| 1676 | foreach {autovac_setting val} { |
| 1677 | 0 0 |
| 1678 | 1 1 |
| 1679 | 2 2 |
| 1680 | 3 0 |
| 1681 | -1 0 |
| 1682 | none 0 |
| 1683 | NONE 0 |
| 1684 | NoNe 0 |
| 1685 | full 1 |
| 1686 | FULL 1 |
| 1687 | incremental 2 |
| 1688 | INCREMENTAL 2 |
| 1689 | -1234 0 |
| 1690 | 1234 0 |
| 1691 | } { |
| 1692 | do_test pragma-17.1.$autovac_setting { |
| 1693 | catch {db close} |
| 1694 | sqlite3 db :memory: |
| 1695 | execsql " |
| 1696 | PRAGMA auto_vacuum=$::autovac_setting; |
| 1697 | PRAGMA auto_vacuum; |
| 1698 | " |
| 1699 | } $val |
| 1700 | } |
| 1701 | |
| 1702 | # Parsing of temp_store settings. |
| 1703 | # |
| 1704 | foreach {temp_setting val} { |
| 1705 | 0 0 |
| 1706 | 1 1 |
| 1707 | 2 2 |
| 1708 | 3 0 |
| 1709 | -1 0 |
| 1710 | file 1 |
| 1711 | FILE 1 |
| 1712 | fIlE 1 |
| 1713 | memory 2 |
| 1714 | MEMORY 2 |
| 1715 | MeMoRy 2 |
| 1716 | } { |
| 1717 | do_test pragma-18.1.$temp_setting { |
| 1718 | catch {db close} |
| 1719 | sqlite3 db :memory: |
| 1720 | execsql " |
| 1721 | PRAGMA temp_store=$::temp_setting; |
| 1722 | PRAGMA temp_store=$::temp_setting; |
| 1723 | PRAGMA temp_store; |
| 1724 | " |
| 1725 | } $val |
| 1726 | } |
| 1727 | |
drh | 92c700d | 2012-02-22 19:56:17 +0000 | [diff] [blame] | 1728 | # The SQLITE_FCNTL_PRAGMA logic, with error handling. |
| 1729 | # |
| 1730 | db close |
| 1731 | testvfs tvfs |
| 1732 | sqlite3 db test.db -vfs tvfs |
| 1733 | do_test pragma-19.1 { |
| 1734 | catchsql {PRAGMA error} |
drh | a690ff3 | 2017-07-07 19:43:23 +0000 | [diff] [blame] | 1735 | } {1 {SQL logic error}} |
drh | 92c700d | 2012-02-22 19:56:17 +0000 | [diff] [blame] | 1736 | do_test pragma-19.2 { |
| 1737 | catchsql {PRAGMA error='This is the error message'} |
| 1738 | } {1 {This is the error message}} |
| 1739 | do_test pragma-19.3 { |
| 1740 | catchsql {PRAGMA error='7 This is the error message'} |
| 1741 | } {1 {This is the error message}} |
| 1742 | do_test pragma-19.4 { |
| 1743 | catchsql {PRAGMA error=7} |
| 1744 | } {1 {out of memory}} |
drh | c8517f6 | 2012-02-22 20:08:49 +0000 | [diff] [blame] | 1745 | do_test pragma-19.5 { |
mistachkin | 5b04454 | 2012-03-02 22:41:06 +0000 | [diff] [blame] | 1746 | file tail [lindex [execsql {PRAGMA filename}] 0] |
drh | c8517f6 | 2012-02-22 20:08:49 +0000 | [diff] [blame] | 1747 | } {test.db} |
drh | 92c700d | 2012-02-22 19:56:17 +0000 | [diff] [blame] | 1748 | |
drh | cc71645 | 2012-06-06 23:23:23 +0000 | [diff] [blame] | 1749 | if {$tcl_platform(platform)=="windows"} { |
mistachkin | a112d14 | 2012-03-14 00:44:01 +0000 | [diff] [blame] | 1750 | # Test data_store_directory pragma |
| 1751 | # |
| 1752 | db close |
| 1753 | sqlite3 db test.db |
| 1754 | file mkdir data_dir |
| 1755 | do_test pragma-20.1 { |
| 1756 | catchsql {PRAGMA data_store_directory} |
| 1757 | } {0 {}} |
| 1758 | do_test pragma-20.2 { |
| 1759 | set pwd [string map {' ''} [file nativename [get_pwd]]] |
| 1760 | catchsql "PRAGMA data_store_directory='$pwd';" |
| 1761 | } {0 {}} |
| 1762 | do_test pragma-20.3 { |
| 1763 | catchsql {PRAGMA data_store_directory} |
| 1764 | } [list 0 [list [file nativename [get_pwd]]]] |
| 1765 | do_test pragma-20.4 { |
| 1766 | set pwd [string map {' ''} [file nativename \ |
| 1767 | [file join [get_pwd] data_dir]]] |
| 1768 | catchsql "PRAGMA data_store_directory='$pwd';" |
| 1769 | } {0 {}} |
| 1770 | do_test pragma-20.5 { |
| 1771 | sqlite3 db2 test2.db |
| 1772 | catchsql "PRAGMA database_list;" db2 |
| 1773 | } [list 0 [list 0 main [file nativename \ |
| 1774 | [file join [get_pwd] data_dir test2.db]]]] |
| 1775 | catch {db2 close} |
| 1776 | do_test pragma-20.6 { |
| 1777 | sqlite3 db2 [file join [get_pwd] test2.db] |
| 1778 | catchsql "PRAGMA database_list;" db2 |
| 1779 | } [list 0 [list 0 main [file nativename \ |
| 1780 | [file join [get_pwd] test2.db]]]] |
| 1781 | catch {db2 close} |
| 1782 | do_test pragma-20.7 { |
| 1783 | catchsql "PRAGMA data_store_directory='';" |
| 1784 | } {0 {}} |
| 1785 | do_test pragma-20.8 { |
| 1786 | catchsql {PRAGMA data_store_directory} |
| 1787 | } {0 {}} |
drh | 92c700d | 2012-02-22 19:56:17 +0000 | [diff] [blame] | 1788 | |
mistachkin | a112d14 | 2012-03-14 00:44:01 +0000 | [diff] [blame] | 1789 | forcedelete data_dir |
drh | cc71645 | 2012-06-06 23:23:23 +0000 | [diff] [blame] | 1790 | } ;# endif windows |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 1791 | |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 1792 | database_may_be_corrupt |
drh | 7da56b4 | 2016-03-14 18:34:42 +0000 | [diff] [blame] | 1793 | if {![nonzero_reserved_bytes]} { |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 1794 | |
drh | 7da56b4 | 2016-03-14 18:34:42 +0000 | [diff] [blame] | 1795 | do_test 21.1 { |
| 1796 | # Create a corrupt database in testerr.db. And a non-corrupt at test.db. |
| 1797 | # |
| 1798 | db close |
| 1799 | forcedelete test.db |
| 1800 | sqlite3 db test.db |
| 1801 | execsql { |
| 1802 | PRAGMA page_size = 1024; |
| 1803 | PRAGMA auto_vacuum = 0; |
| 1804 | CREATE TABLE t1(a PRIMARY KEY, b); |
| 1805 | INSERT INTO t1 VALUES(1, 1); |
| 1806 | } |
| 1807 | for {set i 0} {$i < 10} {incr i} { |
| 1808 | execsql { INSERT INTO t1 SELECT a + (1 << $i), b + (1 << $i) FROM t1 } |
| 1809 | } |
| 1810 | db close |
| 1811 | forcecopy test.db testerr.db |
| 1812 | hexio_write testerr.db 15000 [string repeat 55 100] |
| 1813 | } {100} |
| 1814 | |
| 1815 | set mainerr {*** in database main *** |
dan | 5885e76 | 2012-07-16 10:06:12 +0000 | [diff] [blame] | 1816 | Multiple uses for byte 672 of page 15} |
drh | 7da56b4 | 2016-03-14 18:34:42 +0000 | [diff] [blame] | 1817 | set auxerr {*** in database aux *** |
dan | 5885e76 | 2012-07-16 10:06:12 +0000 | [diff] [blame] | 1818 | Multiple uses for byte 672 of page 15} |
drh | 7da56b4 | 2016-03-14 18:34:42 +0000 | [diff] [blame] | 1819 | |
| 1820 | set mainerr {/{\*\*\* in database main \*\*\* |
dan | 597515d | 2014-02-28 18:39:51 +0000 | [diff] [blame] | 1821 | Multiple uses for byte 672 of page 15}.*/} |
drh | 7da56b4 | 2016-03-14 18:34:42 +0000 | [diff] [blame] | 1822 | set auxerr {/{\*\*\* in database aux \*\*\* |
dan | 597515d | 2014-02-28 18:39:51 +0000 | [diff] [blame] | 1823 | Multiple uses for byte 672 of page 15}.*/} |
drh | 7da56b4 | 2016-03-14 18:34:42 +0000 | [diff] [blame] | 1824 | |
| 1825 | do_test 22.2 { |
| 1826 | catch { db close } |
| 1827 | sqlite3 db testerr.db |
| 1828 | execsql { PRAGMA integrity_check } |
| 1829 | } $mainerr |
| 1830 | |
| 1831 | do_test 22.3.1 { |
| 1832 | catch { db close } |
| 1833 | sqlite3 db test.db |
| 1834 | execsql { |
| 1835 | ATTACH 'testerr.db' AS 'aux'; |
| 1836 | PRAGMA integrity_check; |
| 1837 | } |
| 1838 | } $auxerr |
| 1839 | do_test 22.3.2 { |
| 1840 | execsql { PRAGMA main.integrity_check; } |
| 1841 | } {ok} |
| 1842 | do_test 22.3.3 { |
| 1843 | execsql { PRAGMA aux.integrity_check; } |
| 1844 | } $auxerr |
| 1845 | |
| 1846 | do_test 22.4.1 { |
| 1847 | catch { db close } |
| 1848 | sqlite3 db testerr.db |
| 1849 | execsql { |
| 1850 | ATTACH 'test.db' AS 'aux'; |
| 1851 | PRAGMA integrity_check; |
| 1852 | } |
| 1853 | } $mainerr |
| 1854 | do_test 22.4.2 { |
| 1855 | execsql { PRAGMA main.integrity_check; } |
| 1856 | } $mainerr |
| 1857 | do_test 22.4.3 { |
| 1858 | execsql { PRAGMA aux.integrity_check; } |
| 1859 | } {ok} |
| 1860 | } |
| 1861 | |
drh | c95e01d | 2013-02-14 16:16:05 +0000 | [diff] [blame] | 1862 | db close |
| 1863 | forcedelete test.db test.db-wal test.db-journal |
| 1864 | sqlite3 db test.db |
| 1865 | sqlite3 db2 test.db |
| 1866 | do_test 23.1 { |
| 1867 | db eval { |
| 1868 | CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c,d); |
| 1869 | CREATE INDEX i1 ON t1(b,c); |
| 1870 | CREATE INDEX i2 ON t1(c,d); |
drh | 7be0fd9 | 2015-03-05 15:34:15 +0000 | [diff] [blame] | 1871 | CREATE INDEX i2x ON t1(d COLLATE nocase, c DESC); |
drh | 5f419b3 | 2019-09-27 17:36:37 +0000 | [diff] [blame] | 1872 | CREATE INDEX i3 ON t1(d,b+c,c); |
drh | c95e01d | 2013-02-14 16:16:05 +0000 | [diff] [blame] | 1873 | CREATE TABLE t2(x INTEGER REFERENCES t1); |
| 1874 | } |
| 1875 | db2 eval {SELECT name FROM sqlite_master} |
drh | 5f419b3 | 2019-09-27 17:36:37 +0000 | [diff] [blame] | 1876 | } {t1 i1 i2 i2x i3 t2} |
drh | c228be5 | 2015-01-31 02:00:01 +0000 | [diff] [blame] | 1877 | do_test 23.2a { |
drh | c95e01d | 2013-02-14 16:16:05 +0000 | [diff] [blame] | 1878 | db eval { |
| 1879 | DROP INDEX i2; |
| 1880 | CREATE INDEX i2 ON t1(c,d,b); |
| 1881 | } |
drh | c228be5 | 2015-01-31 02:00:01 +0000 | [diff] [blame] | 1882 | capture_pragma db2 out {PRAGMA index_info(i2)} |
drh | 5e7028c | 2015-03-05 14:29:02 +0000 | [diff] [blame] | 1883 | db2 eval {SELECT cid, name, '|' FROM out ORDER BY seqno} |
| 1884 | } {2 c | 3 d | 1 b |} |
drh | 7be0fd9 | 2015-03-05 15:34:15 +0000 | [diff] [blame] | 1885 | |
drh | b3366b9 | 2015-09-11 20:54:44 +0000 | [diff] [blame] | 1886 | # EVIDENCE-OF: R-56143-29319 PRAGMA schema.index_xinfo(index-name); This |
| 1887 | # pragma returns information about every column in an index. |
drh | 7be0fd9 | 2015-03-05 15:34:15 +0000 | [diff] [blame] | 1888 | # |
| 1889 | # EVIDENCE-OF: R-45970-35618 Unlike this index_info pragma, this pragma |
| 1890 | # returns information about every column in the index, not just the key |
| 1891 | # columns. |
| 1892 | # |
drh | c228be5 | 2015-01-31 02:00:01 +0000 | [diff] [blame] | 1893 | do_test 23.2b { |
| 1894 | capture_pragma db2 out {PRAGMA index_xinfo(i2)} |
| 1895 | db2 eval {SELECT cid, name, "desc", coll, "key", '|' FROM out ORDER BY seqno} |
| 1896 | } {2 c 0 BINARY 1 | 3 d 0 BINARY 1 | 1 b 0 BINARY 1 | -1 {} 0 BINARY 0 |} |
drh | 7be0fd9 | 2015-03-05 15:34:15 +0000 | [diff] [blame] | 1897 | |
| 1898 | # (The first column of output from PRAGMA index_xinfo is...) |
| 1899 | # EVIDENCE-OF: R-00197-14279 The rank of the column within the index. (0 |
| 1900 | # means left-most. Key columns come before auxiliary columns.) |
| 1901 | # |
| 1902 | # (The second column of output from PRAGMA index_xinfo is...) |
drh | 5f419b3 | 2019-09-27 17:36:37 +0000 | [diff] [blame] | 1903 | # EVIDENCE-OF: R-06603-49335 The rank of the column within the table |
drh | 7be0fd9 | 2015-03-05 15:34:15 +0000 | [diff] [blame] | 1904 | # being indexed, or -1 if the index-column is the rowid of the table |
drh | 5f419b3 | 2019-09-27 17:36:37 +0000 | [diff] [blame] | 1905 | # being indexed and -2 if the index is on an expression. |
drh | 7be0fd9 | 2015-03-05 15:34:15 +0000 | [diff] [blame] | 1906 | # |
| 1907 | # (The third column of output from PRAGMA index_xinfo is...) |
drh | 5f419b3 | 2019-09-27 17:36:37 +0000 | [diff] [blame] | 1908 | # EVIDENCE-OF: R-40641-22898 The name of the column being indexed, or |
| 1909 | # NULL if the index-column is the rowid of the table being indexed or an |
| 1910 | # expression. |
drh | 7be0fd9 | 2015-03-05 15:34:15 +0000 | [diff] [blame] | 1911 | # |
| 1912 | # (The fourth column of output from PRAGMA index_xinfo is...) |
| 1913 | # EVIDENCE-OF: R-11847-09179 1 if the index-column is sorted in reverse |
| 1914 | # (DESC) order by the index and 0 otherwise. |
| 1915 | # |
| 1916 | # (The fifth column of output from PRAGMA index_xinfo is...) |
| 1917 | # EVIDENCE-OF: R-15313-19540 The name for the collating sequence used to |
| 1918 | # compare values in the index-column. |
| 1919 | # |
| 1920 | # (The sixth column of output from PRAGMA index_xinfo is...) |
| 1921 | # EVIDENCE-OF: R-14310-64553 1 if the index-column is a key column and 0 |
| 1922 | # if the index-column is an auxiliary column. |
| 1923 | # |
| 1924 | do_test 23.2c { |
| 1925 | db2 eval {PRAGMA index_xinfo(i2)} |
| 1926 | } {0 2 c 0 BINARY 1 1 3 d 0 BINARY 1 2 1 b 0 BINARY 1 3 -1 {} 0 BINARY 0} |
| 1927 | do_test 23.2d { |
| 1928 | db2 eval {PRAGMA index_xinfo(i2x)} |
| 1929 | } {0 3 d 0 nocase 1 1 2 c 1 BINARY 1 2 -1 {} 0 BINARY 0} |
drh | 5f419b3 | 2019-09-27 17:36:37 +0000 | [diff] [blame] | 1930 | do_test 23.2e { |
| 1931 | db2 eval {PRAGMA index_xinfo(i3)} |
| 1932 | } {0 3 d 0 BINARY 1 1 -2 {} 0 BINARY 1 2 2 c 0 BINARY 1 3 -1 {} 0 BINARY 0} |
drh | 7be0fd9 | 2015-03-05 15:34:15 +0000 | [diff] [blame] | 1933 | |
drh | b3366b9 | 2015-09-11 20:54:44 +0000 | [diff] [blame] | 1934 | # EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This |
| 1935 | # pragma returns one row for each index associated with the given table. |
drh | 7be0fd9 | 2015-03-05 15:34:15 +0000 | [diff] [blame] | 1936 | # |
| 1937 | # (The first column of output from PRAGMA index_list is...) |
| 1938 | # EVIDENCE-OF: R-02753-24748 A sequence number assigned to each index |
| 1939 | # for internal tracking purposes. |
| 1940 | # |
| 1941 | # (The second column of output from PRAGMA index_list is...) |
| 1942 | # EVIDENCE-OF: R-35496-03635 The name of the index. |
| 1943 | # |
| 1944 | # (The third column of output from PRAGMA index_list is...) |
| 1945 | # EVIDENCE-OF: R-57301-64506 "1" if the index is UNIQUE and "0" if not. |
| 1946 | # |
| 1947 | # (The fourth column of output from PRAGMA index_list is...) |
| 1948 | # EVIDENCE-OF: R-36609-39554 "c" if the index was created by a CREATE |
| 1949 | # INDEX statement, "u" if the index was created by a UNIQUE constraint, |
| 1950 | # or "pk" if the index was created by a PRIMARY KEY constraint. |
| 1951 | # |
drh | c95e01d | 2013-02-14 16:16:05 +0000 | [diff] [blame] | 1952 | do_test 23.3 { |
| 1953 | db eval { |
drh | 5f419b3 | 2019-09-27 17:36:37 +0000 | [diff] [blame] | 1954 | DROP INDEX IF EXISTS i3; |
drh | c95e01d | 2013-02-14 16:16:05 +0000 | [diff] [blame] | 1955 | CREATE INDEX i3 ON t1(d,b,c); |
| 1956 | } |
drh | c228be5 | 2015-01-31 02:00:01 +0000 | [diff] [blame] | 1957 | capture_pragma db2 out {PRAGMA index_list(t1)} |
drh | 7be0fd9 | 2015-03-05 15:34:15 +0000 | [diff] [blame] | 1958 | db2 eval {SELECT seq, name, "unique", origin, '|' FROM out ORDER BY seq} |
| 1959 | } {0 i3 0 c | 1 i2 0 c | 2 i2x 0 c | 3 i1 0 c |} |
drh | c95e01d | 2013-02-14 16:16:05 +0000 | [diff] [blame] | 1960 | do_test 23.4 { |
| 1961 | db eval { |
| 1962 | ALTER TABLE t1 ADD COLUMN e; |
| 1963 | } |
| 1964 | db2 eval { |
| 1965 | PRAGMA table_info(t1); |
| 1966 | } |
| 1967 | } {/4 e {} 0 {} 0/} |
| 1968 | do_test 23.5 { |
| 1969 | db eval { |
| 1970 | DROP TABLE t2; |
| 1971 | CREATE TABLE t2(x, y INTEGER REFERENCES t1); |
| 1972 | } |
| 1973 | db2 eval { |
| 1974 | PRAGMA foreign_key_list(t2); |
| 1975 | } |
| 1976 | } {0 0 t1 y {} {NO ACTION} {NO ACTION} NONE} |
drh | 3e1e14d | 2017-09-12 00:24:45 +0000 | [diff] [blame] | 1977 | db2 close |
drh | c95e01d | 2013-02-14 16:16:05 +0000 | [diff] [blame] | 1978 | |
drh | efeaec3 | 2017-10-23 16:34:07 +0000 | [diff] [blame] | 1979 | ifcapable !has_codec { |
| 1980 | reset_db |
| 1981 | do_execsql_test 24.0 { |
| 1982 | PRAGMA page_size = 1024; |
| 1983 | CREATE TABLE t1(a, b, c); |
| 1984 | CREATE INDEX i1 ON t1(b); |
| 1985 | INSERT INTO t1 VALUES('a', 'b', 'c'); |
| 1986 | PRAGMA integrity_check; |
| 1987 | } {ok} |
| 1988 | |
| 1989 | set r [db one {SELECT rootpage FROM sqlite_master WHERE name = 't1'}] |
| 1990 | db close |
| 1991 | hexio_write test.db [expr $r*1024 - 16] 000000000000000701040f0f1f616263 |
| 1992 | |
| 1993 | sqlite3 db test.db |
| 1994 | do_catchsql_test 24.1 { |
| 1995 | SELECT * FROM t1; |
| 1996 | } {1 {database disk image is malformed}} |
| 1997 | do_catchsql_test 24.2 { |
| 1998 | PRAGMA integrity_check; |
| 1999 | } {0 {{database disk image is malformed}}} |
| 2000 | } |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 2001 | database_never_corrupt |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2002 | finish_test |