blob: befa5cf548f9b2c0735b2c947be09a00ea952a8f [file] [log] [blame]
drhcd61c282002-03-06 22:01:34 +00001# 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#
danielk1977838cce42009-01-12 14:01:45 +000015# $Id: pragma.test,v 1.73 2009/01/12 14:01:45 danielk1977 Exp $
drhcd61c282002-03-06 22:01:34 +000016
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
dan5885e762012-07-16 10:06:12 +000019set testprefix pragma
drhcd61c282002-03-06 22:01:34 +000020
dan68928b62010-06-22 13:46:43 +000021# 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#
24do_not_use_codec
25
danielk197791cf71b2004-06-26 06:37:06 +000026# 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.
danielk1977cc6bd382005-01-10 02:48:49 +000034# pragma-6.*: Test schema-query pragmas.
35# pragma-7.*: Miscellaneous tests.
36# pragma-8.*: Test user_version and schema_version pragmas.
tpoindex9a09a3c2004-12-20 19:01:32 +000037# pragma-9.*: Test temp_store and temp_store_directory.
danielk1977cc6bd382005-01-10 02:48:49 +000038# pragma-10.*: Test the count_changes pragma in the presence of triggers.
danielk197748af65a2005-02-09 03:20:37 +000039# pragma-11.*: Test the collation_list pragma.
danielk197759a93792008-05-15 17:48:20 +000040# pragma-14.*: Test the page_count pragma.
danielk19778cf6c552008-06-23 16:53:46 +000041# pragma-15.*: Test that the value set using the cache_size pragma is not
42# reset when the schema is reloaded.
aswiftaebf4132008-11-21 00:10:35 +000043# pragma-16.*: Test proxy locking
mistachkina112d142012-03-14 00:44:01 +000044# pragma-20.*: Test data_store_directory.
dan5885e762012-07-16 10:06:12 +000045# pragma-22.*: Test that "PRAGMA [db].integrity_check" respects the "db"
46# directive - if it is present.
danielk197791cf71b2004-06-26 06:37:06 +000047#
48
drh05a82982006-03-19 13:00:25 +000049ifcapable !pragma {
50 finish_test
51 return
52}
53
drhc228be52015-01-31 02:00:01 +000054# Capture the output of a pragma in a TEMP table.
55#
56proc 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
drhcd61c282002-03-06 22:01:34 +000078# Delete the preexisting database to avoid the special setup
79# that the "all.test" script does.
80#
81db close
mistachkinfda06be2011-08-02 00:57:34 +000082delete_file test.db test.db-journal
83delete_file test3.db test3.db-journal
drhdddca282006-01-03 00:33:50 +000084sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
drhcd61c282002-03-06 22:01:34 +000085
drhb3366b92015-09-11 20:54:44 +000086# EVIDENCE-OF: R-13861-56665 PRAGMA schema.cache_size; PRAGMA
87# schema.cache_size = pages; PRAGMA schema.cache_size = -kibibytes;
drh9d356fb2015-02-27 20:28:08 +000088# 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#
danielk1977c7b4a442004-11-23 10:52:51 +000091ifcapable pager_pragmas {
drh1e9daa62007-04-06 21:42:22 +000092set DFLT_CACHE_SZ [db one {PRAGMA default_cache_size}]
93set TEMP_CACHE_SZ [db one {PRAGMA temp.default_cache_size}]
drhcd61c282002-03-06 22:01:34 +000094do_test pragma-1.1 {
95 execsql {
96 PRAGMA cache_size;
97 PRAGMA default_cache_size;
98 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +000099 }
drh1e9daa62007-04-06 21:42:22 +0000100} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
drhcd61c282002-03-06 22:01:34 +0000101do_test pragma-1.2 {
drh9d356fb2015-02-27 20:28:08 +0000102 # EVIDENCE-OF: R-42059-47211 If the argument N is positive then the
103 # suggested cache size is set to N.
drhcd61c282002-03-06 22:01:34 +0000104 execsql {
drheb43e5c2005-05-22 20:30:39 +0000105 PRAGMA synchronous=OFF;
drhcd61c282002-03-06 22:01:34 +0000106 PRAGMA cache_size=1234;
107 PRAGMA cache_size;
108 PRAGMA default_cache_size;
109 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +0000110 }
drh1e9daa62007-04-06 21:42:22 +0000111} [list 1234 $DFLT_CACHE_SZ 0]
drhcd61c282002-03-06 22:01:34 +0000112do_test pragma-1.3 {
113 db close
drhef4ac8f2004-06-19 00:16:31 +0000114 sqlite3 db test.db
drhcd61c282002-03-06 22:01:34 +0000115 execsql {
116 PRAGMA cache_size;
117 PRAGMA default_cache_size;
118 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +0000119 }
drh1e9daa62007-04-06 21:42:22 +0000120} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
drhcd61c282002-03-06 22:01:34 +0000121do_test pragma-1.4 {
122 execsql {
123 PRAGMA synchronous=OFF;
124 PRAGMA cache_size;
125 PRAGMA default_cache_size;
126 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +0000127 }
drh1e9daa62007-04-06 21:42:22 +0000128} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 0]
drhcd61c282002-03-06 22:01:34 +0000129do_test pragma-1.5 {
130 execsql {
drhd2cb50b2009-01-09 21:41:17 +0000131 PRAGMA cache_size=-4321;
drhcd61c282002-03-06 22:01:34 +0000132 PRAGMA cache_size;
133 PRAGMA default_cache_size;
134 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +0000135 }
drh3b42abb2011-11-09 14:23:04 +0000136} [list -4321 $DFLT_CACHE_SZ 0]
drhcd61c282002-03-06 22:01:34 +0000137do_test pragma-1.6 {
138 execsql {
139 PRAGMA synchronous=ON;
140 PRAGMA cache_size;
141 PRAGMA default_cache_size;
142 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +0000143 }
drh3b42abb2011-11-09 14:23:04 +0000144} [list -4321 $DFLT_CACHE_SZ 1]
drhcd61c282002-03-06 22:01:34 +0000145do_test pragma-1.7 {
146 db close
drhef4ac8f2004-06-19 00:16:31 +0000147 sqlite3 db test.db
drhcd61c282002-03-06 22:01:34 +0000148 execsql {
149 PRAGMA cache_size;
150 PRAGMA default_cache_size;
151 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +0000152 }
drh1e9daa62007-04-06 21:42:22 +0000153} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
drhcd61c282002-03-06 22:01:34 +0000154do_test pragma-1.8 {
155 execsql {
drhd2cb50b2009-01-09 21:41:17 +0000156 PRAGMA default_cache_size=-123;
drhcd61c282002-03-06 22:01:34 +0000157 PRAGMA cache_size;
158 PRAGMA default_cache_size;
159 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +0000160 }
danielk197791cf71b2004-06-26 06:37:06 +0000161} {123 123 2}
drh802d69a2005-02-13 23:34:24 +0000162do_test pragma-1.9.1 {
drhcd61c282002-03-06 22:01:34 +0000163 db close
drhdddca282006-01-03 00:33:50 +0000164 sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
drhcd61c282002-03-06 22:01:34 +0000165 execsql {
166 PRAGMA cache_size;
167 PRAGMA default_cache_size;
168 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +0000169 }
danielk197791cf71b2004-06-26 06:37:06 +0000170} {123 123 2}
drh802d69a2005-02-13 23:34:24 +0000171ifcapable 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}
danielk197791cf71b2004-06-26 06:37:06 +0000181do_test pragma-1.10 {
drh5a387052003-01-11 14:19:51 +0000182 execsql {
drh4303fee2003-02-15 23:09:17 +0000183 PRAGMA synchronous=NORMAL;
184 PRAGMA cache_size;
185 PRAGMA default_cache_size;
186 PRAGMA synchronous;
drh4303fee2003-02-15 23:09:17 +0000187 }
danielk197791cf71b2004-06-26 06:37:06 +0000188} {123 123 1}
drh6841b1c2016-02-03 19:20:15 +0000189do_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}
197do_test pragma-1.11.2 {
drh4303fee2003-02-15 23:09:17 +0000198 execsql {
199 PRAGMA synchronous=FULL;
200 PRAGMA cache_size;
201 PRAGMA default_cache_size;
202 PRAGMA synchronous;
drh4303fee2003-02-15 23:09:17 +0000203 }
danielk197791cf71b2004-06-26 06:37:06 +0000204} {123 123 2}
205do_test pragma-1.12 {
drh4303fee2003-02-15 23:09:17 +0000206 db close
drhdddca282006-01-03 00:33:50 +0000207 sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
drh4303fee2003-02-15 23:09:17 +0000208 execsql {
209 PRAGMA cache_size;
210 PRAGMA default_cache_size;
211 PRAGMA synchronous;
drh4303fee2003-02-15 23:09:17 +0000212 }
danielk197791cf71b2004-06-26 06:37:06 +0000213} {123 123 2}
214
drh5260f7e2004-06-26 19:35:29 +0000215# Make sure the pragma handler understands numeric values in addition
216# to keywords like "off" and "full".
217#
218do_test pragma-1.13 {
219 execsql {
220 PRAGMA synchronous=0;
221 PRAGMA synchronous;
222 }
223} {0}
224do_test pragma-1.14 {
225 execsql {
226 PRAGMA synchronous=2;
227 PRAGMA synchronous;
228 }
229} {2}
drh59ac6552015-04-16 16:04:39 +0000230do_test pragma-1.14.1 {
231 execsql {
232 PRAGMA synchronous=4;
233 PRAGMA synchronous;
234 }
drh6841b1c2016-02-03 19:20:15 +0000235} {4}
drh59ac6552015-04-16 16:04:39 +0000236do_test pragma-1.14.2 {
237 execsql {
drhd99d2832015-04-17 15:58:33 +0000238 PRAGMA synchronous=3;
239 PRAGMA synchronous;
240 }
drh6841b1c2016-02-03 19:20:15 +0000241} {3}
drhd99d2832015-04-17 15:58:33 +0000242do_test pragma-1.14.3 {
243 execsql {
drh6841b1c2016-02-03 19:20:15 +0000244 PRAGMA synchronous=8;
245 PRAGMA synchronous;
246 }
247} {0}
248do_test pragma-1.14.4 {
249 execsql {
drh59ac6552015-04-16 16:04:39 +0000250 PRAGMA synchronous=10;
251 PRAGMA synchronous;
252 }
253} {2}
danielk1977c7b4a442004-11-23 10:52:51 +0000254} ;# ifcapable pager_pragmas
drh5260f7e2004-06-26 19:35:29 +0000255
256# Test turning "flag" pragmas on and off.
257#
danielk19776338c762007-05-17 16:38:30 +0000258ifcapable debug {
259 # Pragma "vdbe_listing" is only available if compiled with SQLITE_DEBUG
260 #
261 do_test pragma-1.15 {
262 execsql {
263 PRAGMA vdbe_listing=YES;
264 PRAGMA vdbe_listing;
265 }
266 } {1}
267 do_test pragma-1.16 {
268 execsql {
269 PRAGMA vdbe_listing=NO;
270 PRAGMA vdbe_listing;
271 }
272 } {0}
273}
274
drh5260f7e2004-06-26 19:35:29 +0000275do_test pragma-1.17 {
276 execsql {
277 PRAGMA parser_trace=ON;
278 PRAGMA parser_trace=OFF;
279 }
280} {}
281do_test pragma-1.18 {
282 execsql {
283 PRAGMA bogus = -1234; -- Parsing of negative values
284 }
285} {}
286
danielk197791cf71b2004-06-26 06:37:06 +0000287# Test modifying the safety_level of an attached database.
danielk19775a8f9372007-10-09 08:29:32 +0000288ifcapable pager_pragmas&&attach {
289 do_test pragma-2.1 {
mistachkinfda06be2011-08-02 00:57:34 +0000290 forcedelete test2.db
291 forcedelete test2.db-journal
danielk19775a8f9372007-10-09 08:29:32 +0000292 execsql {
293 ATTACH 'test2.db' AS aux;
294 }
295 } {}
296 do_test pragma-2.2 {
297 execsql {
298 pragma aux.synchronous;
299 }
300 } {2}
301 do_test pragma-2.3 {
302 execsql {
303 pragma aux.synchronous = OFF;
304 pragma aux.synchronous;
305 pragma synchronous;
306 }
307 } {0 2}
308 do_test pragma-2.4 {
309 execsql {
310 pragma aux.synchronous = ON;
311 pragma synchronous;
312 pragma aux.synchronous;
313 }
314 } {2 1}
danielk1977c7b4a442004-11-23 10:52:51 +0000315} ;# ifcapable pager_pragmas
drh4303fee2003-02-15 23:09:17 +0000316
drhed717fe2003-06-15 23:42:24 +0000317# Construct a corrupted index and make sure the integrity_check
318# pragma finds it.
319#
drh25d65432004-07-22 15:02:25 +0000320# These tests won't work if the database is encrypted
321#
drhed717fe2003-06-15 23:42:24 +0000322do_test pragma-3.1 {
drh1e9daa62007-04-06 21:42:22 +0000323 db close
mistachkinfda06be2011-08-02 00:57:34 +0000324 forcedelete test.db test.db-journal
drh1e9daa62007-04-06 21:42:22 +0000325 sqlite3 db test.db
drhed717fe2003-06-15 23:42:24 +0000326 execsql {
drh1e9daa62007-04-06 21:42:22 +0000327 PRAGMA auto_vacuum=OFF;
drhed717fe2003-06-15 23:42:24 +0000328 BEGIN;
329 CREATE TABLE t2(a,b,c);
330 CREATE INDEX i2 ON t2(a);
331 INSERT INTO t2 VALUES(11,2,3);
332 INSERT INTO t2 VALUES(22,3,4);
333 COMMIT;
334 SELECT rowid, * from t2;
335 }
336} {1 11 2 3 2 22 3 4}
danielk19775a8f9372007-10-09 08:29:32 +0000337ifcapable attach {
338 if {![sqlite3 -has-codec] && $sqlite_options(integrityck)} {
339 do_test pragma-3.2 {
drhbb8a2792008-03-19 00:21:30 +0000340 db eval {SELECT rootpage FROM sqlite_master WHERE name='i2'} break
341 set pgsz [db eval {PRAGMA page_size}]
342 # overwrite the header on the rootpage of the index in order to
343 # make the index appear to be empty.
344 #
345 set offset [expr {$pgsz*($rootpage-1)}]
346 hexio_write test.db $offset 0a00000000040000000000
347 db close
348 sqlite3 db test.db
danielk19775a8f9372007-10-09 08:29:32 +0000349 execsql {PRAGMA integrity_check}
drh6fbe41a2013-10-30 20:22:55 +0000350 } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000351 do_test pragma-3.3 {
352 execsql {PRAGMA integrity_check=1}
drh6fbe41a2013-10-30 20:22:55 +0000353 } {{row 1 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000354 do_test pragma-3.4 {
355 execsql {
356 ATTACH DATABASE 'test.db' AS t2;
357 PRAGMA integrity_check
358 }
drh6fbe41a2013-10-30 20:22:55 +0000359 } {{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}}
danielk19775a8f9372007-10-09 08:29:32 +0000360 do_test pragma-3.5 {
361 execsql {
drhbb8a2792008-03-19 00:21:30 +0000362 PRAGMA integrity_check=4
danielk19775a8f9372007-10-09 08:29:32 +0000363 }
drh6fbe41a2013-10-30 20:22:55 +0000364 } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000365 do_test pragma-3.6 {
366 execsql {
367 PRAGMA integrity_check=xyz
368 }
drh6fbe41a2013-10-30 20:22:55 +0000369 } {{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}}
danielk19775a8f9372007-10-09 08:29:32 +0000370 do_test pragma-3.7 {
371 execsql {
372 PRAGMA integrity_check=0
373 }
drh6fbe41a2013-10-30 20:22:55 +0000374 } {{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}}
danielk19775a8f9372007-10-09 08:29:32 +0000375
376 # Add additional corruption by appending unused pages to the end of
377 # the database file testerr.db
378 #
379 do_test pragma-3.8 {
380 execsql {DETACH t2}
mistachkinfda06be2011-08-02 00:57:34 +0000381 forcedelete testerr.db testerr.db-journal
danielk19775a8f9372007-10-09 08:29:32 +0000382 set out [open testerr.db w]
383 fconfigure $out -translation binary
384 set in [open test.db r]
385 fconfigure $in -translation binary
386 puts -nonewline $out [read $in]
387 seek $in 0
388 puts -nonewline $out [read $in]
389 close $in
390 close $out
drhdd3cd972010-03-27 17:12:36 +0000391 hexio_write testerr.db 28 00000000
danielk19775a8f9372007-10-09 08:29:32 +0000392 execsql {REINDEX t2}
393 execsql {PRAGMA integrity_check}
394 } {ok}
danielk197741c58b72007-12-29 13:39:19 +0000395 do_test pragma-3.8.1 {
396 execsql {PRAGMA quick_check}
397 } {ok}
drh5d16a9a2011-10-13 14:41:22 +0000398 do_test pragma-3.8.2 {
399 execsql {PRAGMA QUICK_CHECK}
400 } {ok}
danielk19775a8f9372007-10-09 08:29:32 +0000401 do_test pragma-3.9 {
402 execsql {
403 ATTACH 'testerr.db' AS t2;
404 PRAGMA integrity_check
405 }
406 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000407Page 4 is never used
408Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000409Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000410 do_test pragma-3.10 {
411 execsql {
412 PRAGMA integrity_check=1
413 }
414 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000415Page 4 is never used}}
danielk19775a8f9372007-10-09 08:29:32 +0000416 do_test pragma-3.11 {
417 execsql {
418 PRAGMA integrity_check=5
419 }
420 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000421Page 4 is never used
422Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000423Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000424 do_test pragma-3.12 {
425 execsql {
426 PRAGMA integrity_check=4
427 }
428 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000429Page 4 is never used
430Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000431Page 6 is never used} {row 1 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000432 do_test pragma-3.13 {
433 execsql {
434 PRAGMA integrity_check=3
435 }
436 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000437Page 4 is never used
438Page 5 is never used
439Page 6 is never used}}
danielk19775a8f9372007-10-09 08:29:32 +0000440 do_test pragma-3.14 {
441 execsql {
442 PRAGMA integrity_check(2)
443 }
444 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000445Page 4 is never used
446Page 5 is never used}}
danielk19775a8f9372007-10-09 08:29:32 +0000447 do_test pragma-3.15 {
448 execsql {
449 ATTACH 'testerr.db' AS t3;
450 PRAGMA integrity_check
451 }
452 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000453Page 4 is never used
454Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000455Page 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 ***
drh1dcdbc02007-01-27 02:24:54 +0000456Page 4 is never used
457Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000458Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000459 do_test pragma-3.16 {
460 execsql {
drhbb8a2792008-03-19 00:21:30 +0000461 PRAGMA integrity_check(10)
danielk19775a8f9372007-10-09 08:29:32 +0000462 }
463 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000464Page 4 is never used
465Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000466Page 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 ***
drh1dcdbc02007-01-27 02:24:54 +0000467Page 4 is never used
468Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000469Page 6 is never used} {row 1 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000470 do_test pragma-3.17 {
471 execsql {
drhbb8a2792008-03-19 00:21:30 +0000472 PRAGMA integrity_check=8
danielk19775a8f9372007-10-09 08:29:32 +0000473 }
474 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000475Page 4 is never used
476Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000477Page 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 ***
drh1dcdbc02007-01-27 02:24:54 +0000478Page 4 is never used
479Page 5 is never used}}
danielk19775a8f9372007-10-09 08:29:32 +0000480 do_test pragma-3.18 {
481 execsql {
482 PRAGMA integrity_check=4
483 }
484 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000485Page 4 is never used
486Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000487Page 6 is never used} {row 1 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000488 }
drhd2cb50b2009-01-09 21:41:17 +0000489 do_test pragma-3.19 {
490 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +0000491 forcedelete test.db test.db-journal
drhd2cb50b2009-01-09 21:41:17 +0000492 sqlite3 db test.db
493 db eval {PRAGMA integrity_check}
494 } {ok}
drh25d65432004-07-22 15:02:25 +0000495}
drhcefc87f2014-08-01 01:40:33 +0000496
497# Verify that PRAGMA integrity_check catches UNIQUE and NOT NULL
498# constraint violations.
499#
500do_execsql_test pragma-3.20 {
501 CREATE TABLE t1(a,b);
502 CREATE INDEX t1a ON t1(a);
503 INSERT INTO t1 VALUES(1,1),(2,2),(3,3),(2,4),(NULL,5),(NULL,6);
504 PRAGMA writable_schema=ON;
505 UPDATE sqlite_master SET sql='CREATE UNIQUE INDEX t1a ON t1(a)'
506 WHERE name='t1a';
507 UPDATE sqlite_master SET sql='CREATE TABLE t1(a NOT NULL,b)'
508 WHERE name='t1';
509 PRAGMA writable_schema=OFF;
510 ALTER TABLE t1 RENAME TO t1x;
511 PRAGMA integrity_check;
512} {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a} {NULL value in t1x.a}}
513do_execsql_test pragma-3.21 {
514 PRAGMA integrity_check(3);
515} {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a}}
516do_execsql_test pragma-3.22 {
517 PRAGMA integrity_check(2);
518} {{non-unique entry in index t1a} {NULL value in t1x.a}}
drh7efa4262014-12-16 00:08:31 +0000519do_execsql_test pragma-3.23 {
drhcefc87f2014-08-01 01:40:33 +0000520 PRAGMA integrity_check(1);
521} {{non-unique entry in index t1a}}
danielk197791cf71b2004-06-26 06:37:06 +0000522
drh7efa4262014-12-16 00:08:31 +0000523# PRAGMA integrity check (or more specifically the sqlite3BtreeCount()
524# interface) used to leave index cursors in an inconsistent state
525# which could result in an assertion fault in sqlite3BtreeKey()
526# called from saveCursorPosition() if content is removed from the
527# index while the integrity_check is still running. This test verifies
528# that problem has been fixed.
529#
530do_test pragma-3.30 {
531 db close
532 delete_file test.db
533 sqlite3 db test.db
534 db eval {
535 CREATE TABLE t1(a,b,c);
536 WITH RECURSIVE
537 c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<100)
538 INSERT INTO t1(a,b,c) SELECT i, printf('xyz%08x',i), 2000-i FROM c;
539 CREATE INDEX t1a ON t1(a);
540 CREATE INDEX t1bc ON t1(b,c);
541 }
542 db eval {PRAGMA integrity_check} {
543 db eval {DELETE FROM t1}
544 }
545} {}
546
danielk197791cf71b2004-06-26 06:37:06 +0000547# Test modifying the cache_size of an attached database.
danielk19775a8f9372007-10-09 08:29:32 +0000548ifcapable pager_pragmas&&attach {
danielk197791cf71b2004-06-26 06:37:06 +0000549do_test pragma-4.1 {
550 execsql {
drh1e9daa62007-04-06 21:42:22 +0000551 ATTACH 'test2.db' AS aux;
danielk197791cf71b2004-06-26 06:37:06 +0000552 pragma aux.cache_size;
553 pragma aux.default_cache_size;
554 }
drh1e9daa62007-04-06 21:42:22 +0000555} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000556do_test pragma-4.2 {
557 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000558 pragma aux.cache_size = 50;
559 pragma aux.cache_size;
560 pragma aux.default_cache_size;
561 }
drh1e9daa62007-04-06 21:42:22 +0000562} [list 50 $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000563do_test pragma-4.3 {
564 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000565 pragma aux.default_cache_size = 456;
566 pragma aux.cache_size;
567 pragma aux.default_cache_size;
568 }
569} {456 456}
drh1bdd9b52004-04-23 17:04:44 +0000570do_test pragma-4.4 {
571 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000572 pragma cache_size;
573 pragma default_cache_size;
574 }
drh1e9daa62007-04-06 21:42:22 +0000575} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000576do_test pragma-4.5 {
577 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000578 DETACH aux;
579 ATTACH 'test3.db' AS aux;
580 pragma aux.cache_size;
581 pragma aux.default_cache_size;
582 }
drh1e9daa62007-04-06 21:42:22 +0000583} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000584do_test pragma-4.6 {
585 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000586 DETACH aux;
587 ATTACH 'test2.db' AS aux;
588 pragma aux.cache_size;
589 pragma aux.default_cache_size;
590 }
591} {456 456}
danielk1977c7b4a442004-11-23 10:52:51 +0000592} ;# ifcapable pager_pragmas
drh1bdd9b52004-04-23 17:04:44 +0000593
danielk197791cf71b2004-06-26 06:37:06 +0000594# Test that modifying the sync-level in the middle of a transaction is
595# disallowed.
danielk1977c7b4a442004-11-23 10:52:51 +0000596ifcapable pager_pragmas {
danielk197791cf71b2004-06-26 06:37:06 +0000597do_test pragma-5.0 {
drh1bdd9b52004-04-23 17:04:44 +0000598 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000599 pragma synchronous;
600 }
601} {2}
602do_test pragma-5.1 {
drh1bdd9b52004-04-23 17:04:44 +0000603 catchsql {
604 BEGIN;
danielk197791cf71b2004-06-26 06:37:06 +0000605 pragma synchronous = OFF;
606 }
607} {1 {Safety level may not be changed inside a transaction}}
608do_test pragma-5.2 {
609 execsql {
610 pragma synchronous;
611 }
612} {2}
drh5260f7e2004-06-26 19:35:29 +0000613catchsql {COMMIT;}
danielk1977c7b4a442004-11-23 10:52:51 +0000614} ;# ifcapable pager_pragmas
drh5260f7e2004-06-26 19:35:29 +0000615
616# Test schema-query pragmas
617#
danielk197727188fb2004-11-23 10:13:03 +0000618ifcapable schema_pragmas {
danielk19775a8f9372007-10-09 08:29:32 +0000619ifcapable tempdb&&attach {
danielk197753c0f742005-03-29 03:10:59 +0000620 do_test pragma-6.1 {
621 set res {}
622 execsql {SELECT * FROM sqlite_temp_master}
623 foreach {idx name file} [execsql {pragma database_list}] {
624 lappend res $idx $name
625 }
626 set res
627 } {0 main 1 temp 2 aux}
628}
drh5260f7e2004-06-26 19:35:29 +0000629do_test pragma-6.2 {
630 execsql {
drhd2cb50b2009-01-09 21:41:17 +0000631 CREATE TABLE t2(a,b,c);
drh5260f7e2004-06-26 19:35:29 +0000632 pragma table_info(t2)
633 }
drhbfa8b102006-03-03 21:20:16 +0000634} {0 a {} 0 {} 0 1 b {} 0 {} 0 2 c {} 0 {} 0}
drhd2cb50b2009-01-09 21:41:17 +0000635do_test pragma-6.2.1 {
636 execsql {
637 pragma table_info;
638 }
639} {}
drh736c7d42006-11-30 13:06:37 +0000640db nullvalue <<NULL>>
drh417ec632006-08-14 14:23:41 +0000641do_test pragma-6.2.2 {
642 execsql {
drh736c7d42006-11-30 13:06:37 +0000643 CREATE TABLE t5(
644 a TEXT DEFAULT CURRENT_TIMESTAMP,
645 b DEFAULT (5+3),
646 c TEXT,
647 d INTEGER DEFAULT NULL,
drh384b7fe2013-01-01 13:55:31 +0000648 e TEXT DEFAULT '',
649 UNIQUE(b,c,d),
650 PRIMARY KEY(e,b,c)
drh736c7d42006-11-30 13:06:37 +0000651 );
drh417ec632006-08-14 14:23:41 +0000652 PRAGMA table_info(t5);
653 }
drh384b7fe2013-01-01 13:55:31 +0000654} {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}
drh736c7d42006-11-30 13:06:37 +0000655db nullvalue {}
drh384b7fe2013-01-01 13:55:31 +0000656do_test pragma-6.2.3 {
657 execsql {
658 CREATE TABLE t2_3(a,b INTEGER PRIMARY KEY,c);
659 pragma table_info(t2_3)
660 }
661} {0 a {} 0 {} 0 1 b INTEGER 0 {} 1 2 c {} 0 {} 0}
drh6bf89572004-11-03 16:27:01 +0000662ifcapable {foreignkey} {
drhd2cb50b2009-01-09 21:41:17 +0000663 do_test pragma-6.3.1 {
drh6bf89572004-11-03 16:27:01 +0000664 execsql {
665 CREATE TABLE t3(a int references t2(b), b UNIQUE);
666 pragma foreign_key_list(t3);
667 }
dan1da40a32009-09-19 17:00:31 +0000668 } {0 0 t2 a b {NO ACTION} {NO ACTION} NONE}
drhd2cb50b2009-01-09 21:41:17 +0000669 do_test pragma-6.3.2 {
670 execsql {
671 pragma foreign_key_list;
672 }
673 } {}
674 do_test pragma-6.3.3 {
675 execsql {
676 pragma foreign_key_list(t3_bogus);
677 }
678 } {}
679 do_test pragma-6.3.4 {
680 execsql {
681 pragma foreign_key_list(t5);
682 }
683 } {}
drh6bf89572004-11-03 16:27:01 +0000684 do_test pragma-6.4 {
drhc228be52015-01-31 02:00:01 +0000685 capture_pragma db out {
drh6bf89572004-11-03 16:27:01 +0000686 pragma index_list(t3);
687 }
drhc228be52015-01-31 02:00:01 +0000688 db eval {SELECT seq, "name", "unique" FROM out ORDER BY seq}
drh3ef26152013-10-12 20:22:00 +0000689 } {0 sqlite_autoindex_t3_1 1}
drh6bf89572004-11-03 16:27:01 +0000690}
691ifcapable {!foreignkey} {
692 execsql {CREATE TABLE t3(a,b UNIQUE)}
693}
drhd2cb50b2009-01-09 21:41:17 +0000694do_test pragma-6.5.1 {
drh5260f7e2004-06-26 19:35:29 +0000695 execsql {
696 CREATE INDEX t3i1 ON t3(a,b);
drhc228be52015-01-31 02:00:01 +0000697 }
698 capture_pragma db out {
drh5260f7e2004-06-26 19:35:29 +0000699 pragma index_info(t3i1);
700 }
drhc228be52015-01-31 02:00:01 +0000701 db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
drh5260f7e2004-06-26 19:35:29 +0000702} {0 0 a 1 1 b}
drh7be0fd92015-03-05 15:34:15 +0000703
704# EVIDENCE-OF: R-23114-21695 The auxiliary index-columns are not shown
705# by the index_info pragma, but they are listed by the index_xinfo
706# pragma.
707#
708do_test pragma-6.5.1b {
709 capture_pragma db out {PRAGMA index_xinfo(t3i1)}
710 db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
711} {0 0 a 1 1 b 2 -1 {}}
712
713
drhb3366b92015-09-11 20:54:44 +0000714# EVIDENCE-OF: R-29448-60346 PRAGMA schema.index_info(index-name); This
715# pragma returns one row for each key column in the named index.
drh7be0fd92015-03-05 15:34:15 +0000716#
717# (The first column of output from PRAGMA index_info is...)
718# EVIDENCE-OF: R-34186-52914 The rank of the column within the index. (0
719# means left-most.)
720#
721# (The second column of output from PRAGMA index_info is...)
722# EVIDENCE-OF: R-65019-08383 The rank of the column within the table
723# being indexed.
724#
725# (The third column of output from PRAGMA index_info is...)
726# EVIDENCE-OF: R-09773-34266 The name of the column being indexed.
727#
728do_execsql_test pragma-6.5.1c {
729 CREATE INDEX t3i2 ON t3(b,a);
730 PRAGMA index_info='t3i2';
731 DROP INDEX t3i2;
732} {0 1 b 1 0 a}
733
drhd2cb50b2009-01-09 21:41:17 +0000734do_test pragma-6.5.2 {
735 execsql {
736 pragma index_info(t3i1_bogus);
737 }
738} {}
danielk1977260d8a62008-08-20 16:34:24 +0000739
740ifcapable tempdb {
741 # Test for ticket #3320. When a temp table of the same name exists, make
742 # sure the schema of the main table can still be queried using
743 # "pragma table_info":
744 do_test pragma-6.6.1 {
745 execsql {
746 CREATE TABLE trial(col_main);
747 CREATE TEMP TABLE trial(col_temp);
748 }
749 } {}
750 do_test pragma-6.6.2 {
751 execsql {
752 PRAGMA table_info(trial);
753 }
754 } {0 col_temp {} 0 {} 0}
755 do_test pragma-6.6.3 {
756 execsql {
757 PRAGMA temp.table_info(trial);
758 }
759 } {0 col_temp {} 0 {} 0}
760 do_test pragma-6.6.4 {
761 execsql {
762 PRAGMA main.table_info(trial);
763 }
764 } {0 col_main {} 0 {} 0}
765}
danielk1977f96a3772008-10-23 05:45:07 +0000766
danielk1977f96a3772008-10-23 05:45:07 +0000767do_test pragma-6.7 {
768 execsql {
769 CREATE TABLE test_table(
770 one INT NOT NULL DEFAULT -1,
771 two text,
772 three VARCHAR(45, 65) DEFAULT 'abcde',
773 four REAL DEFAULT X'abcdef',
774 five DEFAULT CURRENT_TIME
775 );
danielk1977f96a3772008-10-23 05:45:07 +0000776 }
drhc228be52015-01-31 02:00:01 +0000777 capture_pragma db out {PRAGMA table_info(test_table)}
778 db eval {SELECT cid, "name", type, "notnull", dflt_value, pk FROM out
779 ORDER BY cid}
danielk1977f96a3772008-10-23 05:45:07 +0000780} [concat \
781 {0 one INT 1 -1 0} \
782 {1 two text 0 {} 0} \
783 {2 three {VARCHAR(45, 65)} 0 'abcde' 0} \
784 {3 four REAL 0 X'abcdef' 0} \
785 {4 five {} 0 CURRENT_TIME 0} \
786]
drh1b678962015-04-15 07:19:27 +0000787do_test pragma-6.8 {
788 execsql {
789 CREATE TABLE t68(a,b,c,PRIMARY KEY(a,b,a,c));
790 PRAGMA table_info(t68);
791 }
792} [concat \
793 {0 a {} 0 {} 1} \
794 {1 b {} 0 {} 2} \
795 {2 c {} 0 {} 4} \
796]
danielk197727188fb2004-11-23 10:13:03 +0000797} ;# ifcapable schema_pragmas
drh5260f7e2004-06-26 19:35:29 +0000798# Miscellaneous tests
799#
danielk197727188fb2004-11-23 10:13:03 +0000800ifcapable schema_pragmas {
drhb3366b92015-09-11 20:54:44 +0000801# EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This
802# pragma returns one row for each index associated with the given table.
803#
drhd2cb50b2009-01-09 21:41:17 +0000804do_test pragma-7.1.1 {
drh5260f7e2004-06-26 19:35:29 +0000805 # Make sure a pragma knows to read the schema if it needs to
806 db close
807 sqlite3 db test.db
drhc228be52015-01-31 02:00:01 +0000808 capture_pragma db out "PRAGMA index_list(t3)"
809 db eval {SELECT name, "origin" FROM out ORDER BY name DESC}
810} {t3i1 c sqlite_autoindex_t3_1 u}
drhd2cb50b2009-01-09 21:41:17 +0000811do_test pragma-7.1.2 {
812 execsql {
813 pragma index_list(t3_bogus);
814 }
815} {}
danielk197727188fb2004-11-23 10:13:03 +0000816} ;# ifcapable schema_pragmas
drh6c626082004-11-14 21:56:29 +0000817ifcapable {utf16} {
dancb354602010-07-08 09:44:42 +0000818 if {[permutation] == ""} {
819 do_test pragma-7.2 {
820 db close
821 sqlite3 db test.db
822 catchsql {
823 pragma encoding=bogus;
824 }
825 } {1 {unsupported encoding: bogus}}
826 }
drh6c626082004-11-14 21:56:29 +0000827}
danielk197753c0f742005-03-29 03:10:59 +0000828ifcapable tempdb {
829 do_test pragma-7.3 {
830 db close
831 sqlite3 db test.db
832 execsql {
833 pragma lock_status;
834 }
835 } {main unlocked temp closed}
836} else {
837 do_test pragma-7.3 {
838 db close
839 sqlite3 db test.db
840 execsql {
841 pragma lock_status;
842 }
843 } {main unlocked}
844}
drh5260f7e2004-06-26 19:35:29 +0000845
846
danielk1977dae24952004-11-11 05:10:43 +0000847#----------------------------------------------------------------------
danielk1977b92b70b2004-11-12 16:11:59 +0000848# Test cases pragma-8.* test the "PRAGMA schema_version" and "PRAGMA
849# user_version" statements.
danielk1977dae24952004-11-11 05:10:43 +0000850#
danielk1977b92b70b2004-11-12 16:11:59 +0000851# pragma-8.1: PRAGMA schema_version
852# pragma-8.2: PRAGMA user_version
danielk1977dae24952004-11-11 05:10:43 +0000853#
854
danielk197711cf9fb2004-11-23 11:16:42 +0000855ifcapable schema_version {
856
danielk1977b92b70b2004-11-12 16:11:59 +0000857# First check that we can set the schema version and then retrieve the
danielk1977dae24952004-11-11 05:10:43 +0000858# same value.
859do_test pragma-8.1.1 {
860 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000861 PRAGMA schema_version = 105;
danielk1977dae24952004-11-11 05:10:43 +0000862 }
863} {}
864do_test pragma-8.1.2 {
drh25403652007-01-04 22:13:41 +0000865 execsql2 {
danielk1977b92b70b2004-11-12 16:11:59 +0000866 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000867 }
drh25403652007-01-04 22:13:41 +0000868} {schema_version 105}
danielk1977dae24952004-11-11 05:10:43 +0000869do_test pragma-8.1.3 {
870 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000871 PRAGMA schema_version = 106;
danielk1977dae24952004-11-11 05:10:43 +0000872 }
873} {}
874do_test pragma-8.1.4 {
875 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000876 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000877 }
878} 106
879
danielk1977b92b70b2004-11-12 16:11:59 +0000880# Check that creating a table modifies the schema-version (this is really
881# to verify that the value being read is in fact the schema version).
danielk1977dae24952004-11-11 05:10:43 +0000882do_test pragma-8.1.5 {
883 execsql {
884 CREATE TABLE t4(a, b, c);
885 INSERT INTO t4 VALUES(1, 2, 3);
886 SELECT * FROM t4;
887 }
888} {1 2 3}
889do_test pragma-8.1.6 {
890 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000891 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000892 }
893} 107
894
895# Now open a second connection to the database. Ensure that changing the
danielk1977b92b70b2004-11-12 16:11:59 +0000896# schema-version using the first connection forces the second connection
danielk1977dae24952004-11-11 05:10:43 +0000897# to reload the schema. This has to be done using the C-API test functions,
898# because the TCL API accounts for SCHEMA_ERROR and retries the query.
899do_test pragma-8.1.7 {
drhdddca282006-01-03 00:33:50 +0000900 sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
danielk1977dae24952004-11-11 05:10:43 +0000901 execsql {
902 SELECT * FROM t4;
903 } db2
904} {1 2 3}
905do_test pragma-8.1.8 {
906 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000907 PRAGMA schema_version = 108;
danielk1977dae24952004-11-11 05:10:43 +0000908 }
909} {}
910do_test pragma-8.1.9 {
911 set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM t4" -1 DUMMY]
912 sqlite3_step $::STMT
913} SQLITE_ERROR
914do_test pragma-8.1.10 {
915 sqlite3_finalize $::STMT
916} SQLITE_SCHEMA
917
danielk1977b92b70b2004-11-12 16:11:59 +0000918# Make sure the schema-version can be manipulated in an attached database.
mistachkinfda06be2011-08-02 00:57:34 +0000919forcedelete test2.db
920forcedelete test2.db-journal
danielk19775a8f9372007-10-09 08:29:32 +0000921ifcapable attach {
922 do_test pragma-8.1.11 {
923 execsql {
924 ATTACH 'test2.db' AS aux;
925 CREATE TABLE aux.t1(a, b, c);
926 PRAGMA aux.schema_version = 205;
927 }
928 } {}
929 do_test pragma-8.1.12 {
930 execsql {
931 PRAGMA aux.schema_version;
932 }
933 } 205
934}
danielk1977dae24952004-11-11 05:10:43 +0000935do_test pragma-8.1.13 {
936 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000937 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000938 }
939} 108
940
danielk1977b92b70b2004-11-12 16:11:59 +0000941# And check that modifying the schema-version in an attached database
danielk1977dae24952004-11-11 05:10:43 +0000942# forces the second connection to reload the schema.
danielk19775a8f9372007-10-09 08:29:32 +0000943ifcapable attach {
944 do_test pragma-8.1.14 {
945 sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
946 execsql {
947 ATTACH 'test2.db' AS aux;
948 SELECT * FROM aux.t1;
949 } db2
950 } {}
951 do_test pragma-8.1.15 {
952 execsql {
953 PRAGMA aux.schema_version = 206;
954 }
955 } {}
956 do_test pragma-8.1.16 {
957 set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM aux.t1" -1 DUMMY]
958 sqlite3_step $::STMT
959 } SQLITE_ERROR
960 do_test pragma-8.1.17 {
961 sqlite3_finalize $::STMT
962 } SQLITE_SCHEMA
963 do_test pragma-8.1.18 {
964 db2 close
965 } {}
966}
danielk1977dae24952004-11-11 05:10:43 +0000967
danielk1977b92b70b2004-11-12 16:11:59 +0000968# Now test that the user-version can be read and written (and that we aren't
969# accidentally manipulating the schema-version instead).
danielk1977dae24952004-11-11 05:10:43 +0000970do_test pragma-8.2.1 {
drh25403652007-01-04 22:13:41 +0000971 execsql2 {
danielk1977b92b70b2004-11-12 16:11:59 +0000972 PRAGMA user_version;
danielk1977dae24952004-11-11 05:10:43 +0000973 }
drh25403652007-01-04 22:13:41 +0000974} {user_version 0}
danielk1977dae24952004-11-11 05:10:43 +0000975do_test pragma-8.2.2 {
976 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000977 PRAGMA user_version = 2;
danielk1977dae24952004-11-11 05:10:43 +0000978 }
979} {}
drh802d69a2005-02-13 23:34:24 +0000980do_test pragma-8.2.3.1 {
drh25403652007-01-04 22:13:41 +0000981 execsql2 {
danielk1977b92b70b2004-11-12 16:11:59 +0000982 PRAGMA user_version;
danielk1977dae24952004-11-11 05:10:43 +0000983 }
drh25403652007-01-04 22:13:41 +0000984} {user_version 2}
drh802d69a2005-02-13 23:34:24 +0000985do_test pragma-8.2.3.2 {
986 db close
987 sqlite3 db test.db
988 execsql {
989 PRAGMA user_version;
990 }
991} {2}
992do_test pragma-8.2.4.1 {
danielk1977dae24952004-11-11 05:10:43 +0000993 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000994 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000995 }
996} {108}
drh802d69a2005-02-13 23:34:24 +0000997ifcapable vacuum {
998 do_test pragma-8.2.4.2 {
999 execsql {
1000 VACUUM;
1001 PRAGMA user_version;
1002 }
1003 } {2}
1004 do_test pragma-8.2.4.3 {
1005 execsql {
1006 PRAGMA schema_version;
1007 }
1008 } {109}
1009}
danielk1977dae24952004-11-11 05:10:43 +00001010
danielk19775a8f9372007-10-09 08:29:32 +00001011ifcapable attach {
1012 db eval {ATTACH 'test2.db' AS aux}
1013
1014 # Check that the user-version in the auxilary database can be manipulated (
1015 # and that we aren't accidentally manipulating the same in the main db).
1016 do_test pragma-8.2.5 {
1017 execsql {
1018 PRAGMA aux.user_version;
1019 }
1020 } {0}
1021 do_test pragma-8.2.6 {
1022 execsql {
1023 PRAGMA aux.user_version = 3;
1024 }
1025 } {}
1026 do_test pragma-8.2.7 {
1027 execsql {
1028 PRAGMA aux.user_version;
1029 }
1030 } {3}
1031 do_test pragma-8.2.8 {
1032 execsql {
1033 PRAGMA main.user_version;
1034 }
1035 } {2}
1036
1037 # Now check that a ROLLBACK resets the user-version if it has been modified
1038 # within a transaction.
1039 do_test pragma-8.2.9 {
1040 execsql {
1041 BEGIN;
1042 PRAGMA aux.user_version = 10;
1043 PRAGMA user_version = 11;
1044 }
1045 } {}
1046 do_test pragma-8.2.10 {
1047 execsql {
1048 PRAGMA aux.user_version;
1049 }
1050 } {10}
1051 do_test pragma-8.2.11 {
1052 execsql {
1053 PRAGMA main.user_version;
1054 }
1055 } {11}
1056 do_test pragma-8.2.12 {
1057 execsql {
1058 ROLLBACK;
1059 PRAGMA aux.user_version;
1060 }
1061 } {3}
1062 do_test pragma-8.2.13 {
1063 execsql {
1064 PRAGMA main.user_version;
1065 }
1066 } {2}
1067}
danielk1977dae24952004-11-11 05:10:43 +00001068
danielk1977b92b70b2004-11-12 16:11:59 +00001069# Try a negative value for the user-version
danielk1977dae24952004-11-11 05:10:43 +00001070do_test pragma-8.2.14 {
1071 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +00001072 PRAGMA user_version = -450;
danielk1977dae24952004-11-11 05:10:43 +00001073 }
1074} {}
1075do_test pragma-8.2.15 {
1076 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +00001077 PRAGMA user_version;
danielk1977dae24952004-11-11 05:10:43 +00001078 }
1079} {-450}
danielk1977d9c847d2005-01-07 10:42:48 +00001080} ; # ifcapable schema_version
1081
drhd19744f2008-03-18 13:46:53 +00001082# Check to see if TEMP_STORE is memory or disk. Return strings
1083# "memory" or "disk" as appropriate.
1084#
1085proc check_temp_store {} {
1086 db eval {CREATE TEMP TABLE IF NOT EXISTS a(b)}
1087 db eval {PRAGMA database_list} {
1088 if {$name=="temp"} {
danielk197717b90b52008-06-06 11:11:25 +00001089 set bt [btree_from_db db 1]
1090 if {[btree_ismemdb $bt]} {
drhd19744f2008-03-18 13:46:53 +00001091 return "memory"
drhd19744f2008-03-18 13:46:53 +00001092 }
danielk197717b90b52008-06-06 11:11:25 +00001093 return "disk"
drhd19744f2008-03-18 13:46:53 +00001094 }
1095 }
1096 return "unknown"
1097}
1098
drh4ee09b42013-05-01 19:49:27 +00001099# Application_ID
1100#
1101do_test pragma-8.3.1 {
1102 execsql {
1103 PRAGMA application_id;
1104 }
1105} {0}
1106do_test pragma-8.3.2 {
1107 execsql {PRAGMA Application_ID(12345); PRAGMA application_id;}
1108} {12345}
tpoindex9a09a3c2004-12-20 19:01:32 +00001109
1110# Test temp_store and temp_store_directory pragmas
1111#
drh268283b2005-01-08 15:44:25 +00001112ifcapable pager_pragmas {
tpoindex9a09a3c2004-12-20 19:01:32 +00001113do_test pragma-9.1 {
1114 db close
1115 sqlite3 db test.db
1116 execsql {
1117 PRAGMA temp_store;
1118 }
1119} {0}
drhd19744f2008-03-18 13:46:53 +00001120if {$TEMP_STORE<=1} {
1121 do_test pragma-9.1.1 {
1122 check_temp_store
1123 } {disk}
1124} else {
1125 do_test pragma-9.1.1 {
1126 check_temp_store
1127 } {memory}
1128}
1129
tpoindex9a09a3c2004-12-20 19:01:32 +00001130do_test pragma-9.2 {
drhd19744f2008-03-18 13:46:53 +00001131 db close
1132 sqlite3 db test.db
tpoindex9a09a3c2004-12-20 19:01:32 +00001133 execsql {
1134 PRAGMA temp_store=file;
1135 PRAGMA temp_store;
1136 }
1137} {1}
drhd19744f2008-03-18 13:46:53 +00001138if {$TEMP_STORE==3} {
1139 # When TEMP_STORE is 3, always use memory regardless of pragma settings.
1140 do_test pragma-9.2.1 {
1141 check_temp_store
1142 } {memory}
1143} else {
1144 do_test pragma-9.2.1 {
1145 check_temp_store
1146 } {disk}
1147}
1148
tpoindex9a09a3c2004-12-20 19:01:32 +00001149do_test pragma-9.3 {
drhd19744f2008-03-18 13:46:53 +00001150 db close
1151 sqlite3 db test.db
tpoindex9a09a3c2004-12-20 19:01:32 +00001152 execsql {
1153 PRAGMA temp_store=memory;
1154 PRAGMA temp_store;
1155 }
1156} {2}
drhd19744f2008-03-18 13:46:53 +00001157if {$TEMP_STORE==0} {
1158 # When TEMP_STORE is 0, always use the disk regardless of pragma settings.
1159 do_test pragma-9.3.1 {
1160 check_temp_store
1161 } {disk}
1162} else {
1163 do_test pragma-9.3.1 {
1164 check_temp_store
1165 } {memory}
1166}
1167
tpoindex9a09a3c2004-12-20 19:01:32 +00001168do_test pragma-9.4 {
1169 execsql {
1170 PRAGMA temp_store_directory;
1171 }
1172} {}
drh78f82d12008-09-02 00:52:52 +00001173ifcapable wsd {
1174 do_test pragma-9.5 {
mistachkinf8a78462012-03-08 20:00:36 +00001175 set pwd [string map {' ''} [file nativename [get_pwd]]]
drh78f82d12008-09-02 00:52:52 +00001176 execsql "
1177 PRAGMA temp_store_directory='$pwd';
1178 "
1179 } {}
1180 do_test pragma-9.6 {
1181 execsql {
1182 PRAGMA temp_store_directory;
1183 }
mistachkinf8a78462012-03-08 20:00:36 +00001184 } [list [file nativename [get_pwd]]]
drh78f82d12008-09-02 00:52:52 +00001185 do_test pragma-9.7 {
1186 catchsql {
1187 PRAGMA temp_store_directory='/NON/EXISTENT/PATH/FOOBAR';
1188 }
1189 } {1 {not a writable directory}}
1190 do_test pragma-9.8 {
1191 execsql {
1192 PRAGMA temp_store_directory='';
1193 }
1194 } {}
1195 if {![info exists TEMP_STORE] || $TEMP_STORE<=1} {
1196 ifcapable tempdb {
1197 do_test pragma-9.9 {
1198 execsql {
1199 PRAGMA temp_store_directory;
1200 PRAGMA temp_store=FILE;
1201 CREATE TEMP TABLE temp_store_directory_test(a integer);
1202 INSERT INTO temp_store_directory_test values (2);
1203 SELECT * FROM temp_store_directory_test;
1204 }
1205 } {2}
1206 do_test pragma-9.10 {
1207 catchsql "
1208 PRAGMA temp_store_directory='$pwd';
1209 SELECT * FROM temp_store_directory_test;
1210 "
1211 } {1 {no such table: temp_store_directory_test}}
1212 }
tpoindex9a09a3c2004-12-20 19:01:32 +00001213 }
drh78f82d12008-09-02 00:52:52 +00001214}
danielk197795b289b2007-03-30 17:11:12 +00001215do_test pragma-9.11 {
1216 execsql {
1217 PRAGMA temp_store = 0;
1218 PRAGMA temp_store;
1219 }
1220} {0}
1221do_test pragma-9.12 {
1222 execsql {
1223 PRAGMA temp_store = 1;
1224 PRAGMA temp_store;
1225 }
1226} {1}
1227do_test pragma-9.13 {
1228 execsql {
1229 PRAGMA temp_store = 2;
1230 PRAGMA temp_store;
1231 }
1232} {2}
1233do_test pragma-9.14 {
1234 execsql {
1235 PRAGMA temp_store = 3;
1236 PRAGMA temp_store;
1237 }
1238} {0}
danielk197795b289b2007-03-30 17:11:12 +00001239do_test pragma-9.15 {
1240 catchsql {
1241 BEGIN EXCLUSIVE;
1242 CREATE TEMP TABLE temp_table(t);
1243 INSERT INTO temp_table VALUES('valuable data');
1244 PRAGMA temp_store = 1;
1245 }
1246} {1 {temporary storage cannot be changed from within a transaction}}
1247do_test pragma-9.16 {
1248 execsql {
1249 SELECT * FROM temp_table;
1250 COMMIT;
1251 }
1252} {{valuable data}}
danielk1977983e2302008-07-08 07:35:51 +00001253
1254do_test pragma-9.17 {
1255 execsql {
1256 INSERT INTO temp_table VALUES('valuable data II');
1257 SELECT * FROM temp_table;
1258 }
1259} {{valuable data} {valuable data II}}
1260
1261do_test pragma-9.18 {
1262 set rc [catch {
1263 db eval {SELECT t FROM temp_table} {
1264 execsql {pragma temp_store = 1}
1265 }
1266 } msg]
1267 list $rc $msg
1268} {1 {temporary storage cannot be changed from within a transaction}}
1269
drh268283b2005-01-08 15:44:25 +00001270} ;# ifcapable pager_pragmas
tpoindex9a09a3c2004-12-20 19:01:32 +00001271
danielk1977cc6bd382005-01-10 02:48:49 +00001272ifcapable trigger {
1273
1274do_test pragma-10.0 {
1275 catchsql {
1276 DROP TABLE main.t1;
1277 }
1278 execsql {
1279 PRAGMA count_changes = 1;
1280
1281 CREATE TABLE t1(a PRIMARY KEY);
1282 CREATE TABLE t1_mirror(a);
1283 CREATE TABLE t1_mirror2(a);
1284 CREATE TRIGGER t1_bi BEFORE INSERT ON t1 BEGIN
1285 INSERT INTO t1_mirror VALUES(new.a);
1286 END;
1287 CREATE TRIGGER t1_ai AFTER INSERT ON t1 BEGIN
1288 INSERT INTO t1_mirror2 VALUES(new.a);
1289 END;
1290 CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 BEGIN
1291 UPDATE t1_mirror SET a = new.a WHERE a = old.a;
1292 END;
1293 CREATE TRIGGER t1_au AFTER UPDATE ON t1 BEGIN
1294 UPDATE t1_mirror2 SET a = new.a WHERE a = old.a;
1295 END;
1296 CREATE TRIGGER t1_bd BEFORE DELETE ON t1 BEGIN
1297 DELETE FROM t1_mirror WHERE a = old.a;
1298 END;
1299 CREATE TRIGGER t1_ad AFTER DELETE ON t1 BEGIN
1300 DELETE FROM t1_mirror2 WHERE a = old.a;
1301 END;
1302 }
1303} {}
1304
1305do_test pragma-10.1 {
1306 execsql {
1307 INSERT INTO t1 VALUES(randstr(10,10));
1308 }
1309} {1}
1310do_test pragma-10.2 {
1311 execsql {
1312 UPDATE t1 SET a = randstr(10,10);
1313 }
1314} {1}
1315do_test pragma-10.3 {
1316 execsql {
1317 DELETE FROM t1;
1318 }
1319} {1}
1320
1321} ;# ifcapable trigger
1322
danielk197748af65a2005-02-09 03:20:37 +00001323ifcapable schema_pragmas {
1324 do_test pragma-11.1 {
1325 execsql2 {
1326 pragma collation_list;
1327 }
drh2d823312014-11-20 23:11:30 +00001328 } {seq 0 name RTRIM seq 1 name NOCASE seq 2 name BINARY}
danielk197748af65a2005-02-09 03:20:37 +00001329 do_test pragma-11.2 {
1330 db collate New_Collation blah...
1331 execsql {
1332 pragma collation_list;
1333 }
drh2d823312014-11-20 23:11:30 +00001334 } {0 New_Collation 1 RTRIM 2 NOCASE 3 BINARY}
danielk197748af65a2005-02-09 03:20:37 +00001335}
1336
danielk1977ddfb2f02006-02-17 12:25:14 +00001337ifcapable schema_pragmas&&tempdb {
1338 do_test pragma-12.1 {
1339 sqlite3 db2 test.db
1340 execsql {
1341 PRAGMA temp.table_info('abc');
1342 } db2
1343 } {}
1344 db2 close
1345
1346 do_test pragma-12.2 {
1347 sqlite3 db2 test.db
1348 execsql {
1349 PRAGMA temp.default_cache_size = 200;
1350 PRAGMA temp.default_cache_size;
1351 } db2
1352 } {200}
1353 db2 close
1354
1355 do_test pragma-12.3 {
1356 sqlite3 db2 test.db
1357 execsql {
1358 PRAGMA temp.cache_size = 400;
1359 PRAGMA temp.cache_size;
1360 } db2
1361 } {400}
1362 db2 close
1363}
1364
danielk19774b2688a2006-06-20 11:01:07 +00001365ifcapable bloblit {
1366
drh05a82982006-03-19 13:00:25 +00001367do_test pragma-13.1 {
1368 execsql {
1369 DROP TABLE IF EXISTS t4;
1370 PRAGMA vdbe_trace=on;
1371 PRAGMA vdbe_listing=on;
1372 PRAGMA sql_trace=on;
1373 CREATE TABLE t4(a INTEGER PRIMARY KEY,b);
1374 INSERT INTO t4(b) VALUES(x'0123456789abcdef0123456789abcdef0123456789');
1375 INSERT INTO t4(b) VALUES(randstr(30,30));
1376 INSERT INTO t4(b) VALUES(1.23456);
1377 INSERT INTO t4(b) VALUES(NULL);
1378 INSERT INTO t4(b) VALUES(0);
1379 INSERT INTO t4(b) SELECT b||b||b||b FROM t4;
1380 SELECT * FROM t4;
1381 }
1382 execsql {
1383 PRAGMA vdbe_trace=off;
1384 PRAGMA vdbe_listing=off;
1385 PRAGMA sql_trace=off;
1386 }
1387} {}
1388
danielk19774b2688a2006-06-20 11:01:07 +00001389} ;# ifcapable bloblit
1390
danielk197759a93792008-05-15 17:48:20 +00001391ifcapable pager_pragmas {
1392 db close
mistachkinfda06be2011-08-02 00:57:34 +00001393 forcedelete test.db
danielk197759a93792008-05-15 17:48:20 +00001394 sqlite3 db test.db
drh51a74d42015-02-28 01:04:27 +00001395
drhb3366b92015-09-11 20:54:44 +00001396 # EVIDENCE-OF: R-15672-33611 PRAGMA schema.page_count; Return the total
1397 # number of pages in the database file.
drh51a74d42015-02-28 01:04:27 +00001398 #
danielk197759a93792008-05-15 17:48:20 +00001399 do_test pragma-14.1 {
1400 execsql { pragma auto_vacuum = 0 }
drh51a74d42015-02-28 01:04:27 +00001401 execsql { pragma page_count; pragma main.page_count }
1402 } {0 0}
danielk197759a93792008-05-15 17:48:20 +00001403
1404 do_test pragma-14.2 {
1405 execsql {
1406 CREATE TABLE abc(a, b, c);
1407 PRAGMA page_count;
drh51a74d42015-02-28 01:04:27 +00001408 PRAGMA main.page_count;
1409 PRAGMA temp.page_count;
danielk197759a93792008-05-15 17:48:20 +00001410 }
drh51a74d42015-02-28 01:04:27 +00001411 } {2 2 0}
drh5d16a9a2011-10-13 14:41:22 +00001412 do_test pragma-14.2uc {
1413 execsql {pragma PAGE_COUNT}
1414 } {2}
danielk197759a93792008-05-15 17:48:20 +00001415
1416 do_test pragma-14.3 {
1417 execsql {
1418 BEGIN;
1419 CREATE TABLE def(a, b, c);
1420 PRAGMA page_count;
1421 }
1422 } {3}
drh5d16a9a2011-10-13 14:41:22 +00001423 do_test pragma-14.3uc {
1424 execsql {pragma PAGE_COUNT}
1425 } {3}
danielk197759a93792008-05-15 17:48:20 +00001426
1427 do_test pragma-14.4 {
1428 set page_size [db one {pragma page_size}]
1429 expr [file size test.db] / $page_size
1430 } {2}
1431
1432 do_test pragma-14.5 {
1433 execsql {
1434 ROLLBACK;
1435 PRAGMA page_count;
1436 }
1437 } {2}
1438
1439 do_test pragma-14.6 {
mistachkinfda06be2011-08-02 00:57:34 +00001440 forcedelete test2.db
danielk197759a93792008-05-15 17:48:20 +00001441 sqlite3 db2 test2.db
1442 execsql {
1443 PRAGMA auto_vacuum = 0;
1444 CREATE TABLE t1(a, b, c);
1445 CREATE TABLE t2(a, b, c);
1446 CREATE TABLE t3(a, b, c);
1447 CREATE TABLE t4(a, b, c);
1448 } db2
1449 db2 close
1450 execsql {
1451 ATTACH 'test2.db' AS aux;
1452 PRAGMA aux.page_count;
1453 }
1454 } {5}
drh5d16a9a2011-10-13 14:41:22 +00001455 do_test pragma-14.6uc {
1456 execsql {pragma AUX.PAGE_COUNT}
1457 } {5}
danielk197759a93792008-05-15 17:48:20 +00001458}
1459
danielk19778cf6c552008-06-23 16:53:46 +00001460# Test that the value set using the cache_size pragma is not reset when the
1461# schema is reloaded.
1462#
1463ifcapable pager_pragmas {
1464 db close
1465 sqlite3 db test.db
1466 do_test pragma-15.1 {
1467 execsql {
1468 PRAGMA cache_size=59;
1469 PRAGMA cache_size;
1470 }
1471 } {59}
1472 do_test pragma-15.2 {
1473 sqlite3 db2 test.db
1474 execsql {
1475 CREATE TABLE newtable(a, b, c);
1476 } db2
1477 db2 close
1478 } {}
1479 do_test pragma-15.3 {
1480 # Evaluating this statement will cause the schema to be reloaded (because
1481 # the schema was changed by another connection in pragma-15.2). At one
1482 # point there was a bug that reset the cache_size to its default value
1483 # when this happened.
1484 execsql { SELECT * FROM sqlite_master }
1485 execsql { PRAGMA cache_size }
1486 } {59}
1487}
1488
danielk19775558a8a2005-01-17 07:53:44 +00001489# Reset the sqlite3_temp_directory variable for the next run of tests:
1490sqlite3 dbX :memory:
1491dbX eval {PRAGMA temp_store_directory = ""}
1492dbX close
1493
danielk1977838cce42009-01-12 14:01:45 +00001494ifcapable lock_proxy_pragmas&&prefer_proxy_locking {
aswiftaebf4132008-11-21 00:10:35 +00001495 set sqlite_hostid_num 1
1496
1497 set using_proxy 0
1498 foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
1499 set using_proxy $value
1500 }
1501
1502 # Test the lock_proxy_file pragmas.
1503 #
1504 db close
1505 set env(SQLITE_FORCE_PROXY_LOCKING) "0"
1506
1507 sqlite3 db test.db
1508 do_test pragma-16.1 {
1509 execsql {
1510 PRAGMA lock_proxy_file="mylittleproxy";
1511 select * from sqlite_master;
1512 }
1513 execsql {
1514 PRAGMA lock_proxy_file;
1515 }
1516 } {mylittleproxy}
1517
1518 do_test pragma-16.2 {
1519 sqlite3 db2 test.db
1520 execsql {
1521 PRAGMA lock_proxy_file="mylittleproxy";
1522 } db2
1523 } {}
1524
1525 db2 close
1526 do_test pragma-16.2.1 {
1527 sqlite3 db2 test.db
1528 execsql {
1529 PRAGMA lock_proxy_file=":auto:";
1530 select * from sqlite_master;
1531 } db2
1532 execsql {
1533 PRAGMA lock_proxy_file;
1534 } db2
1535 } {mylittleproxy}
1536
1537 db2 close
1538 do_test pragma-16.3 {
1539 sqlite3 db2 test.db
1540 execsql {
1541 PRAGMA lock_proxy_file="myotherproxy";
1542 } db2
1543 catchsql {
1544 select * from sqlite_master;
1545 } db2
1546 } {1 {database is locked}}
1547
1548 do_test pragma-16.4 {
1549 db2 close
1550 db close
1551 sqlite3 db2 test.db
1552 execsql {
1553 PRAGMA lock_proxy_file="myoriginalproxy";
1554 PRAGMA lock_proxy_file="myotherproxy";
1555 PRAGMA lock_proxy_file;
1556 } db2
1557 } {myotherproxy}
1558
1559 db2 close
1560 set env(SQLITE_FORCE_PROXY_LOCKING) "1"
1561 do_test pragma-16.5 {
1562 sqlite3 db2 test.db
1563 execsql {
1564 PRAGMA lock_proxy_file=":auto:";
1565 PRAGMA lock_proxy_file;
1566 } db2
1567 } {myotherproxy}
1568
1569 do_test pragma-16.6 {
1570 db2 close
1571 sqlite3 db2 test2.db
1572 set lockpath [execsql {
1573 PRAGMA lock_proxy_file=":auto:";
1574 PRAGMA lock_proxy_file;
1575 } db2]
1576 string match "*test2.db:auto:" $lockpath
1577 } {1}
1578
1579 set sqlite_hostid_num 2
1580 do_test pragma-16.7 {
dan14d14602010-10-06 16:42:52 +00001581 list [catch {
1582 sqlite3 db test2.db
1583 execsql {
1584 PRAGMA lock_proxy_file=":auto:";
1585 select * from sqlite_master;
1586 }
1587 } msg] $msg
aswiftaebf4132008-11-21 00:10:35 +00001588 } {1 {database is locked}}
1589 db close
1590
1591 do_test pragma-16.8 {
dan14d14602010-10-06 16:42:52 +00001592 list [catch {
1593 sqlite3 db test2.db
1594 execsql { select * from sqlite_master }
1595 } msg] $msg
aswiftaebf4132008-11-21 00:10:35 +00001596 } {1 {database is locked}}
1597
1598 db2 close
1599 do_test pragma-16.8.1 {
1600 execsql {
1601 PRAGMA lock_proxy_file="yetanotherproxy";
1602 PRAGMA lock_proxy_file;
1603 }
1604 } {yetanotherproxy}
1605 do_test pragma-16.8.2 {
1606 execsql {
1607 create table mine(x);
1608 }
1609 } {}
1610
1611 db close
1612 do_test pragma-16.9 {
1613 sqlite3 db proxytest.db
1614 set lockpath2 [execsql {
1615 PRAGMA lock_proxy_file=":auto:";
1616 PRAGMA lock_proxy_file;
1617 } db]
1618 string match "*proxytest.db:auto:" $lockpath2
1619 } {1}
1620
1621 set env(SQLITE_FORCE_PROXY_LOCKING) $using_proxy
1622 set sqlite_hostid_num 0
1623}
drhd2cb50b2009-01-09 21:41:17 +00001624
1625# Parsing of auto_vacuum settings.
1626#
1627foreach {autovac_setting val} {
1628 0 0
1629 1 1
1630 2 2
1631 3 0
1632 -1 0
1633 none 0
1634 NONE 0
1635 NoNe 0
1636 full 1
1637 FULL 1
1638 incremental 2
1639 INCREMENTAL 2
1640 -1234 0
1641 1234 0
1642} {
1643 do_test pragma-17.1.$autovac_setting {
1644 catch {db close}
1645 sqlite3 db :memory:
1646 execsql "
1647 PRAGMA auto_vacuum=$::autovac_setting;
1648 PRAGMA auto_vacuum;
1649 "
1650 } $val
1651}
1652
1653# Parsing of temp_store settings.
1654#
1655foreach {temp_setting val} {
1656 0 0
1657 1 1
1658 2 2
1659 3 0
1660 -1 0
1661 file 1
1662 FILE 1
1663 fIlE 1
1664 memory 2
1665 MEMORY 2
1666 MeMoRy 2
1667} {
1668 do_test pragma-18.1.$temp_setting {
1669 catch {db close}
1670 sqlite3 db :memory:
1671 execsql "
1672 PRAGMA temp_store=$::temp_setting;
1673 PRAGMA temp_store=$::temp_setting;
1674 PRAGMA temp_store;
1675 "
1676 } $val
1677}
1678
drh92c700d2012-02-22 19:56:17 +00001679# The SQLITE_FCNTL_PRAGMA logic, with error handling.
1680#
1681db close
1682testvfs tvfs
1683sqlite3 db test.db -vfs tvfs
1684do_test pragma-19.1 {
1685 catchsql {PRAGMA error}
1686} {1 {SQL logic error or missing database}}
1687do_test pragma-19.2 {
1688 catchsql {PRAGMA error='This is the error message'}
1689} {1 {This is the error message}}
1690do_test pragma-19.3 {
1691 catchsql {PRAGMA error='7 This is the error message'}
1692} {1 {This is the error message}}
1693do_test pragma-19.4 {
1694 catchsql {PRAGMA error=7}
1695} {1 {out of memory}}
drhc8517f62012-02-22 20:08:49 +00001696do_test pragma-19.5 {
mistachkin5b044542012-03-02 22:41:06 +00001697 file tail [lindex [execsql {PRAGMA filename}] 0]
drhc8517f62012-02-22 20:08:49 +00001698} {test.db}
drh92c700d2012-02-22 19:56:17 +00001699
drhcc716452012-06-06 23:23:23 +00001700if {$tcl_platform(platform)=="windows"} {
mistachkina112d142012-03-14 00:44:01 +00001701# Test data_store_directory pragma
1702#
1703db close
1704sqlite3 db test.db
1705file mkdir data_dir
1706do_test pragma-20.1 {
1707 catchsql {PRAGMA data_store_directory}
1708} {0 {}}
1709do_test pragma-20.2 {
1710 set pwd [string map {' ''} [file nativename [get_pwd]]]
1711 catchsql "PRAGMA data_store_directory='$pwd';"
1712} {0 {}}
1713do_test pragma-20.3 {
1714 catchsql {PRAGMA data_store_directory}
1715} [list 0 [list [file nativename [get_pwd]]]]
1716do_test pragma-20.4 {
1717 set pwd [string map {' ''} [file nativename \
1718 [file join [get_pwd] data_dir]]]
1719 catchsql "PRAGMA data_store_directory='$pwd';"
1720} {0 {}}
1721do_test pragma-20.5 {
1722 sqlite3 db2 test2.db
1723 catchsql "PRAGMA database_list;" db2
1724} [list 0 [list 0 main [file nativename \
1725 [file join [get_pwd] data_dir test2.db]]]]
1726catch {db2 close}
1727do_test pragma-20.6 {
1728 sqlite3 db2 [file join [get_pwd] test2.db]
1729 catchsql "PRAGMA database_list;" db2
1730} [list 0 [list 0 main [file nativename \
1731 [file join [get_pwd] test2.db]]]]
1732catch {db2 close}
1733do_test pragma-20.7 {
1734 catchsql "PRAGMA data_store_directory='';"
1735} {0 {}}
1736do_test pragma-20.8 {
1737 catchsql {PRAGMA data_store_directory}
1738} {0 {}}
drh92c700d2012-02-22 19:56:17 +00001739
mistachkina112d142012-03-14 00:44:01 +00001740forcedelete data_dir
drhcc716452012-06-06 23:23:23 +00001741} ;# endif windows
drhcd61c282002-03-06 22:01:34 +00001742
dan1fed5da2014-02-25 21:01:25 +00001743database_may_be_corrupt
1744
dan5885e762012-07-16 10:06:12 +00001745do_test 21.1 {
1746 # Create a corrupt database in testerr.db. And a non-corrupt at test.db.
1747 #
1748 db close
1749 forcedelete test.db
1750 sqlite3 db test.db
1751 execsql {
1752 PRAGMA page_size = 1024;
1753 PRAGMA auto_vacuum = 0;
1754 CREATE TABLE t1(a PRIMARY KEY, b);
1755 INSERT INTO t1 VALUES(1, 1);
1756 }
1757 for {set i 0} {$i < 10} {incr i} {
1758 execsql { INSERT INTO t1 SELECT a + (1 << $i), b + (1 << $i) FROM t1 }
1759 }
1760 db close
1761 forcecopy test.db testerr.db
1762 hexio_write testerr.db 15000 [string repeat 55 100]
1763} {100}
1764
1765set mainerr {*** in database main ***
1766Multiple uses for byte 672 of page 15}
1767set auxerr {*** in database aux ***
1768Multiple uses for byte 672 of page 15}
1769
dan597515d2014-02-28 18:39:51 +00001770set mainerr {/{\*\*\* in database main \*\*\*
1771Multiple uses for byte 672 of page 15}.*/}
1772set auxerr {/{\*\*\* in database aux \*\*\*
1773Multiple uses for byte 672 of page 15}.*/}
1774
dan5885e762012-07-16 10:06:12 +00001775do_test 22.2 {
1776 catch { db close }
1777 sqlite3 db testerr.db
1778 execsql { PRAGMA integrity_check }
dan597515d2014-02-28 18:39:51 +00001779} $mainerr
dan5885e762012-07-16 10:06:12 +00001780
1781do_test 22.3.1 {
1782 catch { db close }
1783 sqlite3 db test.db
1784 execsql {
1785 ATTACH 'testerr.db' AS 'aux';
1786 PRAGMA integrity_check;
1787 }
dan597515d2014-02-28 18:39:51 +00001788} $auxerr
dan5885e762012-07-16 10:06:12 +00001789do_test 22.3.2 {
1790 execsql { PRAGMA main.integrity_check; }
1791} {ok}
1792do_test 22.3.3 {
1793 execsql { PRAGMA aux.integrity_check; }
dan597515d2014-02-28 18:39:51 +00001794} $auxerr
dan5885e762012-07-16 10:06:12 +00001795
1796do_test 22.4.1 {
1797 catch { db close }
1798 sqlite3 db testerr.db
1799 execsql {
1800 ATTACH 'test.db' AS 'aux';
1801 PRAGMA integrity_check;
1802 }
dan597515d2014-02-28 18:39:51 +00001803} $mainerr
dan5885e762012-07-16 10:06:12 +00001804do_test 22.4.2 {
1805 execsql { PRAGMA main.integrity_check; }
dan597515d2014-02-28 18:39:51 +00001806} $mainerr
dan5885e762012-07-16 10:06:12 +00001807do_test 22.4.3 {
1808 execsql { PRAGMA aux.integrity_check; }
1809} {ok}
1810
drhc95e01d2013-02-14 16:16:05 +00001811db close
1812forcedelete test.db test.db-wal test.db-journal
1813sqlite3 db test.db
1814sqlite3 db2 test.db
1815do_test 23.1 {
1816 db eval {
1817 CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c,d);
1818 CREATE INDEX i1 ON t1(b,c);
1819 CREATE INDEX i2 ON t1(c,d);
drh7be0fd92015-03-05 15:34:15 +00001820 CREATE INDEX i2x ON t1(d COLLATE nocase, c DESC);
drhc95e01d2013-02-14 16:16:05 +00001821 CREATE TABLE t2(x INTEGER REFERENCES t1);
1822 }
1823 db2 eval {SELECT name FROM sqlite_master}
drh7be0fd92015-03-05 15:34:15 +00001824} {t1 i1 i2 i2x t2}
drhc228be52015-01-31 02:00:01 +00001825do_test 23.2a {
drhc95e01d2013-02-14 16:16:05 +00001826 db eval {
1827 DROP INDEX i2;
1828 CREATE INDEX i2 ON t1(c,d,b);
1829 }
drhc228be52015-01-31 02:00:01 +00001830 capture_pragma db2 out {PRAGMA index_info(i2)}
drh5e7028c2015-03-05 14:29:02 +00001831 db2 eval {SELECT cid, name, '|' FROM out ORDER BY seqno}
1832} {2 c | 3 d | 1 b |}
drh7be0fd92015-03-05 15:34:15 +00001833
drhb3366b92015-09-11 20:54:44 +00001834# EVIDENCE-OF: R-56143-29319 PRAGMA schema.index_xinfo(index-name); This
1835# pragma returns information about every column in an index.
drh7be0fd92015-03-05 15:34:15 +00001836#
1837# EVIDENCE-OF: R-45970-35618 Unlike this index_info pragma, this pragma
1838# returns information about every column in the index, not just the key
1839# columns.
1840#
drhc228be52015-01-31 02:00:01 +00001841do_test 23.2b {
1842 capture_pragma db2 out {PRAGMA index_xinfo(i2)}
1843 db2 eval {SELECT cid, name, "desc", coll, "key", '|' FROM out ORDER BY seqno}
1844} {2 c 0 BINARY 1 | 3 d 0 BINARY 1 | 1 b 0 BINARY 1 | -1 {} 0 BINARY 0 |}
drh7be0fd92015-03-05 15:34:15 +00001845
1846# (The first column of output from PRAGMA index_xinfo is...)
1847# EVIDENCE-OF: R-00197-14279 The rank of the column within the index. (0
1848# means left-most. Key columns come before auxiliary columns.)
1849#
1850# (The second column of output from PRAGMA index_xinfo is...)
1851# EVIDENCE-OF: R-40889-06838 The rank of the column within the table
1852# being indexed, or -1 if the index-column is the rowid of the table
1853# being indexed.
1854#
1855# (The third column of output from PRAGMA index_xinfo is...)
1856# EVIDENCE-OF: R-22751-28901 The name of the column being indexed, or
1857# NULL if the index-column is the rowid of the table being indexed.
1858#
1859# (The fourth column of output from PRAGMA index_xinfo is...)
1860# EVIDENCE-OF: R-11847-09179 1 if the index-column is sorted in reverse
1861# (DESC) order by the index and 0 otherwise.
1862#
1863# (The fifth column of output from PRAGMA index_xinfo is...)
1864# EVIDENCE-OF: R-15313-19540 The name for the collating sequence used to
1865# compare values in the index-column.
1866#
1867# (The sixth column of output from PRAGMA index_xinfo is...)
1868# EVIDENCE-OF: R-14310-64553 1 if the index-column is a key column and 0
1869# if the index-column is an auxiliary column.
1870#
1871do_test 23.2c {
1872 db2 eval {PRAGMA index_xinfo(i2)}
1873} {0 2 c 0 BINARY 1 1 3 d 0 BINARY 1 2 1 b 0 BINARY 1 3 -1 {} 0 BINARY 0}
1874do_test 23.2d {
1875 db2 eval {PRAGMA index_xinfo(i2x)}
1876} {0 3 d 0 nocase 1 1 2 c 1 BINARY 1 2 -1 {} 0 BINARY 0}
1877
drhb3366b92015-09-11 20:54:44 +00001878# EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This
1879# pragma returns one row for each index associated with the given table.
drh7be0fd92015-03-05 15:34:15 +00001880#
1881# (The first column of output from PRAGMA index_list is...)
1882# EVIDENCE-OF: R-02753-24748 A sequence number assigned to each index
1883# for internal tracking purposes.
1884#
1885# (The second column of output from PRAGMA index_list is...)
1886# EVIDENCE-OF: R-35496-03635 The name of the index.
1887#
1888# (The third column of output from PRAGMA index_list is...)
1889# EVIDENCE-OF: R-57301-64506 "1" if the index is UNIQUE and "0" if not.
1890#
1891# (The fourth column of output from PRAGMA index_list is...)
1892# EVIDENCE-OF: R-36609-39554 "c" if the index was created by a CREATE
1893# INDEX statement, "u" if the index was created by a UNIQUE constraint,
1894# or "pk" if the index was created by a PRIMARY KEY constraint.
1895#
drhc95e01d2013-02-14 16:16:05 +00001896do_test 23.3 {
1897 db eval {
1898 CREATE INDEX i3 ON t1(d,b,c);
1899 }
drhc228be52015-01-31 02:00:01 +00001900 capture_pragma db2 out {PRAGMA index_list(t1)}
drh7be0fd92015-03-05 15:34:15 +00001901 db2 eval {SELECT seq, name, "unique", origin, '|' FROM out ORDER BY seq}
1902} {0 i3 0 c | 1 i2 0 c | 2 i2x 0 c | 3 i1 0 c |}
drhc95e01d2013-02-14 16:16:05 +00001903do_test 23.4 {
1904 db eval {
1905 ALTER TABLE t1 ADD COLUMN e;
1906 }
1907 db2 eval {
1908 PRAGMA table_info(t1);
1909 }
1910} {/4 e {} 0 {} 0/}
1911do_test 23.5 {
1912 db eval {
1913 DROP TABLE t2;
1914 CREATE TABLE t2(x, y INTEGER REFERENCES t1);
1915 }
1916 db2 eval {
1917 PRAGMA foreign_key_list(t2);
1918 }
1919} {0 0 t1 y {} {NO ACTION} {NO ACTION} NONE}
1920
dan1fed5da2014-02-25 21:01:25 +00001921database_never_corrupt
drhcd61c282002-03-06 22:01:34 +00001922finish_test