blob: 15289846540909b4a9013cebddc80bc775e10099 [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#
drh6ab91a72018-11-07 02:17:01 +0000500sqlite3_db_config db DEFENSIVE 0
drhcefc87f2014-08-01 01:40:33 +0000501do_execsql_test pragma-3.20 {
502 CREATE TABLE t1(a,b);
503 CREATE INDEX t1a ON t1(a);
504 INSERT INTO t1 VALUES(1,1),(2,2),(3,3),(2,4),(NULL,5),(NULL,6);
505 PRAGMA writable_schema=ON;
506 UPDATE sqlite_master SET sql='CREATE UNIQUE INDEX t1a ON t1(a)'
507 WHERE name='t1a';
508 UPDATE sqlite_master SET sql='CREATE TABLE t1(a NOT NULL,b)'
509 WHERE name='t1';
510 PRAGMA writable_schema=OFF;
511 ALTER TABLE t1 RENAME TO t1x;
512 PRAGMA integrity_check;
513} {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a} {NULL value in t1x.a}}
514do_execsql_test pragma-3.21 {
515 PRAGMA integrity_check(3);
516} {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a}}
517do_execsql_test pragma-3.22 {
518 PRAGMA integrity_check(2);
519} {{non-unique entry in index t1a} {NULL value in t1x.a}}
drh7efa4262014-12-16 00:08:31 +0000520do_execsql_test pragma-3.23 {
drhcefc87f2014-08-01 01:40:33 +0000521 PRAGMA integrity_check(1);
522} {{non-unique entry in index t1a}}
danielk197791cf71b2004-06-26 06:37:06 +0000523
drh7efa4262014-12-16 00:08:31 +0000524# PRAGMA integrity check (or more specifically the sqlite3BtreeCount()
525# interface) used to leave index cursors in an inconsistent state
526# which could result in an assertion fault in sqlite3BtreeKey()
527# called from saveCursorPosition() if content is removed from the
528# index while the integrity_check is still running. This test verifies
529# that problem has been fixed.
530#
531do_test pragma-3.30 {
532 db close
533 delete_file test.db
534 sqlite3 db test.db
535 db eval {
536 CREATE TABLE t1(a,b,c);
537 WITH RECURSIVE
538 c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<100)
539 INSERT INTO t1(a,b,c) SELECT i, printf('xyz%08x',i), 2000-i FROM c;
540 CREATE INDEX t1a ON t1(a);
541 CREATE INDEX t1bc ON t1(b,c);
542 }
543 db eval {PRAGMA integrity_check} {
544 db eval {DELETE FROM t1}
545 }
546} {}
547
danielk197791cf71b2004-06-26 06:37:06 +0000548# Test modifying the cache_size of an attached database.
danielk19775a8f9372007-10-09 08:29:32 +0000549ifcapable pager_pragmas&&attach {
danielk197791cf71b2004-06-26 06:37:06 +0000550do_test pragma-4.1 {
551 execsql {
drh1e9daa62007-04-06 21:42:22 +0000552 ATTACH 'test2.db' AS aux;
danielk197791cf71b2004-06-26 06:37:06 +0000553 pragma aux.cache_size;
554 pragma aux.default_cache_size;
555 }
drh1e9daa62007-04-06 21:42:22 +0000556} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000557do_test pragma-4.2 {
558 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000559 pragma aux.cache_size = 50;
560 pragma aux.cache_size;
561 pragma aux.default_cache_size;
562 }
drh1e9daa62007-04-06 21:42:22 +0000563} [list 50 $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000564do_test pragma-4.3 {
565 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000566 pragma aux.default_cache_size = 456;
567 pragma aux.cache_size;
568 pragma aux.default_cache_size;
569 }
570} {456 456}
drh1bdd9b52004-04-23 17:04:44 +0000571do_test pragma-4.4 {
572 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000573 pragma cache_size;
574 pragma default_cache_size;
575 }
drh1e9daa62007-04-06 21:42:22 +0000576} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000577do_test pragma-4.5 {
578 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000579 DETACH aux;
580 ATTACH 'test3.db' AS aux;
581 pragma aux.cache_size;
582 pragma aux.default_cache_size;
583 }
drh1e9daa62007-04-06 21:42:22 +0000584} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000585do_test pragma-4.6 {
586 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000587 DETACH aux;
588 ATTACH 'test2.db' AS aux;
589 pragma aux.cache_size;
590 pragma aux.default_cache_size;
591 }
592} {456 456}
danielk1977c7b4a442004-11-23 10:52:51 +0000593} ;# ifcapable pager_pragmas
drh1bdd9b52004-04-23 17:04:44 +0000594
danielk197791cf71b2004-06-26 06:37:06 +0000595# Test that modifying the sync-level in the middle of a transaction is
596# disallowed.
danielk1977c7b4a442004-11-23 10:52:51 +0000597ifcapable pager_pragmas {
danielk197791cf71b2004-06-26 06:37:06 +0000598do_test pragma-5.0 {
drh1bdd9b52004-04-23 17:04:44 +0000599 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000600 pragma synchronous;
601 }
602} {2}
603do_test pragma-5.1 {
drh1bdd9b52004-04-23 17:04:44 +0000604 catchsql {
605 BEGIN;
danielk197791cf71b2004-06-26 06:37:06 +0000606 pragma synchronous = OFF;
607 }
608} {1 {Safety level may not be changed inside a transaction}}
609do_test pragma-5.2 {
610 execsql {
611 pragma synchronous;
612 }
613} {2}
drh5260f7e2004-06-26 19:35:29 +0000614catchsql {COMMIT;}
danielk1977c7b4a442004-11-23 10:52:51 +0000615} ;# ifcapable pager_pragmas
drh5260f7e2004-06-26 19:35:29 +0000616
617# Test schema-query pragmas
618#
danielk197727188fb2004-11-23 10:13:03 +0000619ifcapable schema_pragmas {
danielk19775a8f9372007-10-09 08:29:32 +0000620ifcapable tempdb&&attach {
danielk197753c0f742005-03-29 03:10:59 +0000621 do_test pragma-6.1 {
622 set res {}
623 execsql {SELECT * FROM sqlite_temp_master}
624 foreach {idx name file} [execsql {pragma database_list}] {
625 lappend res $idx $name
626 }
627 set res
628 } {0 main 1 temp 2 aux}
629}
drh5260f7e2004-06-26 19:35:29 +0000630do_test pragma-6.2 {
631 execsql {
drha6dddd92016-04-18 15:46:14 +0000632 CREATE TABLE t2(a TYPE_X, b [TYPE_Y], c "TYPE_Z");
drh5260f7e2004-06-26 19:35:29 +0000633 pragma table_info(t2)
634 }
drha6dddd92016-04-18 15:46:14 +0000635} {0 a TYPE_X 0 {} 0 1 b TYPE_Y 0 {} 0 2 c TYPE_Z 0 {} 0}
drhd2cb50b2009-01-09 21:41:17 +0000636do_test pragma-6.2.1 {
637 execsql {
638 pragma table_info;
639 }
640} {}
drh736c7d42006-11-30 13:06:37 +0000641db nullvalue <<NULL>>
drh417ec632006-08-14 14:23:41 +0000642do_test pragma-6.2.2 {
643 execsql {
drh736c7d42006-11-30 13:06:37 +0000644 CREATE TABLE t5(
645 a TEXT DEFAULT CURRENT_TIMESTAMP,
646 b DEFAULT (5+3),
647 c TEXT,
648 d INTEGER DEFAULT NULL,
drh384b7fe2013-01-01 13:55:31 +0000649 e TEXT DEFAULT '',
650 UNIQUE(b,c,d),
651 PRIMARY KEY(e,b,c)
drh736c7d42006-11-30 13:06:37 +0000652 );
drh417ec632006-08-14 14:23:41 +0000653 PRAGMA table_info(t5);
654 }
drh384b7fe2013-01-01 13:55:31 +0000655} {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 +0000656db nullvalue {}
drh384b7fe2013-01-01 13:55:31 +0000657do_test pragma-6.2.3 {
658 execsql {
659 CREATE TABLE t2_3(a,b INTEGER PRIMARY KEY,c);
660 pragma table_info(t2_3)
661 }
662} {0 a {} 0 {} 0 1 b INTEGER 0 {} 1 2 c {} 0 {} 0}
drh6bf89572004-11-03 16:27:01 +0000663ifcapable {foreignkey} {
drhd2cb50b2009-01-09 21:41:17 +0000664 do_test pragma-6.3.1 {
drh6bf89572004-11-03 16:27:01 +0000665 execsql {
666 CREATE TABLE t3(a int references t2(b), b UNIQUE);
667 pragma foreign_key_list(t3);
668 }
dan1da40a32009-09-19 17:00:31 +0000669 } {0 0 t2 a b {NO ACTION} {NO ACTION} NONE}
drhd2cb50b2009-01-09 21:41:17 +0000670 do_test pragma-6.3.2 {
671 execsql {
672 pragma foreign_key_list;
673 }
674 } {}
675 do_test pragma-6.3.3 {
676 execsql {
677 pragma foreign_key_list(t3_bogus);
678 }
679 } {}
680 do_test pragma-6.3.4 {
681 execsql {
682 pragma foreign_key_list(t5);
683 }
684 } {}
drh6bf89572004-11-03 16:27:01 +0000685 do_test pragma-6.4 {
drhc228be52015-01-31 02:00:01 +0000686 capture_pragma db out {
drh6bf89572004-11-03 16:27:01 +0000687 pragma index_list(t3);
688 }
drhc228be52015-01-31 02:00:01 +0000689 db eval {SELECT seq, "name", "unique" FROM out ORDER BY seq}
drh3ef26152013-10-12 20:22:00 +0000690 } {0 sqlite_autoindex_t3_1 1}
drh6bf89572004-11-03 16:27:01 +0000691}
692ifcapable {!foreignkey} {
693 execsql {CREATE TABLE t3(a,b UNIQUE)}
694}
drhd2cb50b2009-01-09 21:41:17 +0000695do_test pragma-6.5.1 {
drh5260f7e2004-06-26 19:35:29 +0000696 execsql {
697 CREATE INDEX t3i1 ON t3(a,b);
drhc228be52015-01-31 02:00:01 +0000698 }
699 capture_pragma db out {
drh5260f7e2004-06-26 19:35:29 +0000700 pragma index_info(t3i1);
701 }
drhc228be52015-01-31 02:00:01 +0000702 db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
drh5260f7e2004-06-26 19:35:29 +0000703} {0 0 a 1 1 b}
drh7be0fd92015-03-05 15:34:15 +0000704
705# EVIDENCE-OF: R-23114-21695 The auxiliary index-columns are not shown
706# by the index_info pragma, but they are listed by the index_xinfo
707# pragma.
708#
709do_test pragma-6.5.1b {
710 capture_pragma db out {PRAGMA index_xinfo(t3i1)}
711 db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
712} {0 0 a 1 1 b 2 -1 {}}
713
714
drhb3366b92015-09-11 20:54:44 +0000715# EVIDENCE-OF: R-29448-60346 PRAGMA schema.index_info(index-name); This
716# pragma returns one row for each key column in the named index.
drh7be0fd92015-03-05 15:34:15 +0000717#
718# (The first column of output from PRAGMA index_info is...)
719# EVIDENCE-OF: R-34186-52914 The rank of the column within the index. (0
720# means left-most.)
721#
722# (The second column of output from PRAGMA index_info is...)
723# EVIDENCE-OF: R-65019-08383 The rank of the column within the table
724# being indexed.
725#
726# (The third column of output from PRAGMA index_info is...)
727# EVIDENCE-OF: R-09773-34266 The name of the column being indexed.
728#
729do_execsql_test pragma-6.5.1c {
730 CREATE INDEX t3i2 ON t3(b,a);
731 PRAGMA index_info='t3i2';
732 DROP INDEX t3i2;
733} {0 1 b 1 0 a}
734
drhd2cb50b2009-01-09 21:41:17 +0000735do_test pragma-6.5.2 {
736 execsql {
737 pragma index_info(t3i1_bogus);
738 }
739} {}
danielk1977260d8a62008-08-20 16:34:24 +0000740
741ifcapable tempdb {
742 # Test for ticket #3320. When a temp table of the same name exists, make
743 # sure the schema of the main table can still be queried using
744 # "pragma table_info":
745 do_test pragma-6.6.1 {
746 execsql {
747 CREATE TABLE trial(col_main);
748 CREATE TEMP TABLE trial(col_temp);
749 }
750 } {}
751 do_test pragma-6.6.2 {
752 execsql {
753 PRAGMA table_info(trial);
754 }
755 } {0 col_temp {} 0 {} 0}
756 do_test pragma-6.6.3 {
757 execsql {
758 PRAGMA temp.table_info(trial);
759 }
760 } {0 col_temp {} 0 {} 0}
761 do_test pragma-6.6.4 {
762 execsql {
763 PRAGMA main.table_info(trial);
764 }
765 } {0 col_main {} 0 {} 0}
766}
danielk1977f96a3772008-10-23 05:45:07 +0000767
danielk1977f96a3772008-10-23 05:45:07 +0000768do_test pragma-6.7 {
769 execsql {
770 CREATE TABLE test_table(
771 one INT NOT NULL DEFAULT -1,
772 two text,
773 three VARCHAR(45, 65) DEFAULT 'abcde',
774 four REAL DEFAULT X'abcdef',
775 five DEFAULT CURRENT_TIME
776 );
danielk1977f96a3772008-10-23 05:45:07 +0000777 }
drhc228be52015-01-31 02:00:01 +0000778 capture_pragma db out {PRAGMA table_info(test_table)}
779 db eval {SELECT cid, "name", type, "notnull", dflt_value, pk FROM out
780 ORDER BY cid}
danielk1977f96a3772008-10-23 05:45:07 +0000781} [concat \
782 {0 one INT 1 -1 0} \
783 {1 two text 0 {} 0} \
784 {2 three {VARCHAR(45, 65)} 0 'abcde' 0} \
785 {3 four REAL 0 X'abcdef' 0} \
786 {4 five {} 0 CURRENT_TIME 0} \
787]
drh1b678962015-04-15 07:19:27 +0000788do_test pragma-6.8 {
789 execsql {
790 CREATE TABLE t68(a,b,c,PRIMARY KEY(a,b,a,c));
791 PRAGMA table_info(t68);
792 }
793} [concat \
794 {0 a {} 0 {} 1} \
795 {1 b {} 0 {} 2} \
796 {2 c {} 0 {} 4} \
797]
danielk197727188fb2004-11-23 10:13:03 +0000798} ;# ifcapable schema_pragmas
drh5260f7e2004-06-26 19:35:29 +0000799# Miscellaneous tests
800#
danielk197727188fb2004-11-23 10:13:03 +0000801ifcapable schema_pragmas {
drhb3366b92015-09-11 20:54:44 +0000802# EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This
803# pragma returns one row for each index associated with the given table.
804#
drhd2cb50b2009-01-09 21:41:17 +0000805do_test pragma-7.1.1 {
drh5260f7e2004-06-26 19:35:29 +0000806 # Make sure a pragma knows to read the schema if it needs to
807 db close
808 sqlite3 db test.db
drhc228be52015-01-31 02:00:01 +0000809 capture_pragma db out "PRAGMA index_list(t3)"
810 db eval {SELECT name, "origin" FROM out ORDER BY name DESC}
811} {t3i1 c sqlite_autoindex_t3_1 u}
drhd2cb50b2009-01-09 21:41:17 +0000812do_test pragma-7.1.2 {
813 execsql {
814 pragma index_list(t3_bogus);
815 }
816} {}
danielk197727188fb2004-11-23 10:13:03 +0000817} ;# ifcapable schema_pragmas
drh6c626082004-11-14 21:56:29 +0000818ifcapable {utf16} {
dancb354602010-07-08 09:44:42 +0000819 if {[permutation] == ""} {
820 do_test pragma-7.2 {
821 db close
822 sqlite3 db test.db
823 catchsql {
824 pragma encoding=bogus;
825 }
826 } {1 {unsupported encoding: bogus}}
827 }
drh6c626082004-11-14 21:56:29 +0000828}
danielk197753c0f742005-03-29 03:10:59 +0000829ifcapable tempdb {
830 do_test pragma-7.3 {
831 db close
832 sqlite3 db test.db
833 execsql {
834 pragma lock_status;
835 }
836 } {main unlocked temp closed}
837} else {
838 do_test pragma-7.3 {
839 db close
840 sqlite3 db test.db
841 execsql {
842 pragma lock_status;
843 }
844 } {main unlocked}
845}
drh5260f7e2004-06-26 19:35:29 +0000846
847
danielk1977dae24952004-11-11 05:10:43 +0000848#----------------------------------------------------------------------
danielk1977b92b70b2004-11-12 16:11:59 +0000849# Test cases pragma-8.* test the "PRAGMA schema_version" and "PRAGMA
850# user_version" statements.
danielk1977dae24952004-11-11 05:10:43 +0000851#
danielk1977b92b70b2004-11-12 16:11:59 +0000852# pragma-8.1: PRAGMA schema_version
853# pragma-8.2: PRAGMA user_version
danielk1977dae24952004-11-11 05:10:43 +0000854#
855
danielk197711cf9fb2004-11-23 11:16:42 +0000856ifcapable schema_version {
857
danielk1977b92b70b2004-11-12 16:11:59 +0000858# First check that we can set the schema version and then retrieve the
danielk1977dae24952004-11-11 05:10:43 +0000859# same value.
860do_test pragma-8.1.1 {
861 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000862 PRAGMA schema_version = 105;
danielk1977dae24952004-11-11 05:10:43 +0000863 }
864} {}
865do_test pragma-8.1.2 {
drh25403652007-01-04 22:13:41 +0000866 execsql2 {
danielk1977b92b70b2004-11-12 16:11:59 +0000867 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000868 }
drh25403652007-01-04 22:13:41 +0000869} {schema_version 105}
danielk1977dae24952004-11-11 05:10:43 +0000870do_test pragma-8.1.3 {
871 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000872 PRAGMA schema_version = 106;
danielk1977dae24952004-11-11 05:10:43 +0000873 }
874} {}
875do_test pragma-8.1.4 {
876 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000877 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000878 }
879} 106
880
danielk1977b92b70b2004-11-12 16:11:59 +0000881# Check that creating a table modifies the schema-version (this is really
882# to verify that the value being read is in fact the schema version).
danielk1977dae24952004-11-11 05:10:43 +0000883do_test pragma-8.1.5 {
884 execsql {
885 CREATE TABLE t4(a, b, c);
886 INSERT INTO t4 VALUES(1, 2, 3);
887 SELECT * FROM t4;
888 }
889} {1 2 3}
890do_test pragma-8.1.6 {
891 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000892 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000893 }
894} 107
895
896# Now open a second connection to the database. Ensure that changing the
danielk1977b92b70b2004-11-12 16:11:59 +0000897# schema-version using the first connection forces the second connection
danielk1977dae24952004-11-11 05:10:43 +0000898# to reload the schema. This has to be done using the C-API test functions,
899# because the TCL API accounts for SCHEMA_ERROR and retries the query.
900do_test pragma-8.1.7 {
drhdddca282006-01-03 00:33:50 +0000901 sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
danielk1977dae24952004-11-11 05:10:43 +0000902 execsql {
903 SELECT * FROM t4;
904 } db2
905} {1 2 3}
906do_test pragma-8.1.8 {
907 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000908 PRAGMA schema_version = 108;
danielk1977dae24952004-11-11 05:10:43 +0000909 }
910} {}
911do_test pragma-8.1.9 {
912 set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM t4" -1 DUMMY]
913 sqlite3_step $::STMT
914} SQLITE_ERROR
915do_test pragma-8.1.10 {
916 sqlite3_finalize $::STMT
917} SQLITE_SCHEMA
918
danielk1977b92b70b2004-11-12 16:11:59 +0000919# Make sure the schema-version can be manipulated in an attached database.
mistachkinfda06be2011-08-02 00:57:34 +0000920forcedelete test2.db
921forcedelete test2.db-journal
danielk19775a8f9372007-10-09 08:29:32 +0000922ifcapable attach {
923 do_test pragma-8.1.11 {
924 execsql {
925 ATTACH 'test2.db' AS aux;
926 CREATE TABLE aux.t1(a, b, c);
927 PRAGMA aux.schema_version = 205;
928 }
929 } {}
930 do_test pragma-8.1.12 {
931 execsql {
932 PRAGMA aux.schema_version;
933 }
934 } 205
935}
danielk1977dae24952004-11-11 05:10:43 +0000936do_test pragma-8.1.13 {
937 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000938 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000939 }
940} 108
941
danielk1977b92b70b2004-11-12 16:11:59 +0000942# And check that modifying the schema-version in an attached database
danielk1977dae24952004-11-11 05:10:43 +0000943# forces the second connection to reload the schema.
danielk19775a8f9372007-10-09 08:29:32 +0000944ifcapable attach {
945 do_test pragma-8.1.14 {
946 sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
947 execsql {
948 ATTACH 'test2.db' AS aux;
949 SELECT * FROM aux.t1;
950 } db2
951 } {}
952 do_test pragma-8.1.15 {
953 execsql {
954 PRAGMA aux.schema_version = 206;
955 }
956 } {}
957 do_test pragma-8.1.16 {
958 set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM aux.t1" -1 DUMMY]
959 sqlite3_step $::STMT
960 } SQLITE_ERROR
961 do_test pragma-8.1.17 {
962 sqlite3_finalize $::STMT
963 } SQLITE_SCHEMA
964 do_test pragma-8.1.18 {
965 db2 close
966 } {}
967}
danielk1977dae24952004-11-11 05:10:43 +0000968
danielk1977b92b70b2004-11-12 16:11:59 +0000969# Now test that the user-version can be read and written (and that we aren't
970# accidentally manipulating the schema-version instead).
danielk1977dae24952004-11-11 05:10:43 +0000971do_test pragma-8.2.1 {
drh25403652007-01-04 22:13:41 +0000972 execsql2 {
danielk1977b92b70b2004-11-12 16:11:59 +0000973 PRAGMA user_version;
danielk1977dae24952004-11-11 05:10:43 +0000974 }
drh25403652007-01-04 22:13:41 +0000975} {user_version 0}
danielk1977dae24952004-11-11 05:10:43 +0000976do_test pragma-8.2.2 {
977 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000978 PRAGMA user_version = 2;
danielk1977dae24952004-11-11 05:10:43 +0000979 }
980} {}
drh802d69a2005-02-13 23:34:24 +0000981do_test pragma-8.2.3.1 {
drh25403652007-01-04 22:13:41 +0000982 execsql2 {
danielk1977b92b70b2004-11-12 16:11:59 +0000983 PRAGMA user_version;
danielk1977dae24952004-11-11 05:10:43 +0000984 }
drh25403652007-01-04 22:13:41 +0000985} {user_version 2}
drh802d69a2005-02-13 23:34:24 +0000986do_test pragma-8.2.3.2 {
987 db close
988 sqlite3 db test.db
989 execsql {
990 PRAGMA user_version;
991 }
992} {2}
993do_test pragma-8.2.4.1 {
danielk1977dae24952004-11-11 05:10:43 +0000994 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000995 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000996 }
997} {108}
drh802d69a2005-02-13 23:34:24 +0000998ifcapable vacuum {
999 do_test pragma-8.2.4.2 {
1000 execsql {
1001 VACUUM;
1002 PRAGMA user_version;
1003 }
1004 } {2}
1005 do_test pragma-8.2.4.3 {
1006 execsql {
1007 PRAGMA schema_version;
1008 }
1009 } {109}
1010}
danielk1977dae24952004-11-11 05:10:43 +00001011
danielk19775a8f9372007-10-09 08:29:32 +00001012ifcapable attach {
1013 db eval {ATTACH 'test2.db' AS aux}
1014
1015 # Check that the user-version in the auxilary database can be manipulated (
1016 # and that we aren't accidentally manipulating the same in the main db).
1017 do_test pragma-8.2.5 {
1018 execsql {
1019 PRAGMA aux.user_version;
1020 }
1021 } {0}
1022 do_test pragma-8.2.6 {
1023 execsql {
1024 PRAGMA aux.user_version = 3;
1025 }
1026 } {}
1027 do_test pragma-8.2.7 {
1028 execsql {
1029 PRAGMA aux.user_version;
1030 }
1031 } {3}
1032 do_test pragma-8.2.8 {
1033 execsql {
1034 PRAGMA main.user_version;
1035 }
1036 } {2}
1037
1038 # Now check that a ROLLBACK resets the user-version if it has been modified
1039 # within a transaction.
1040 do_test pragma-8.2.9 {
1041 execsql {
1042 BEGIN;
1043 PRAGMA aux.user_version = 10;
1044 PRAGMA user_version = 11;
1045 }
1046 } {}
1047 do_test pragma-8.2.10 {
1048 execsql {
1049 PRAGMA aux.user_version;
1050 }
1051 } {10}
1052 do_test pragma-8.2.11 {
1053 execsql {
1054 PRAGMA main.user_version;
1055 }
1056 } {11}
1057 do_test pragma-8.2.12 {
1058 execsql {
1059 ROLLBACK;
1060 PRAGMA aux.user_version;
1061 }
1062 } {3}
1063 do_test pragma-8.2.13 {
1064 execsql {
1065 PRAGMA main.user_version;
1066 }
1067 } {2}
1068}
danielk1977dae24952004-11-11 05:10:43 +00001069
danielk1977b92b70b2004-11-12 16:11:59 +00001070# Try a negative value for the user-version
danielk1977dae24952004-11-11 05:10:43 +00001071do_test pragma-8.2.14 {
1072 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +00001073 PRAGMA user_version = -450;
danielk1977dae24952004-11-11 05:10:43 +00001074 }
1075} {}
1076do_test pragma-8.2.15 {
1077 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +00001078 PRAGMA user_version;
danielk1977dae24952004-11-11 05:10:43 +00001079 }
1080} {-450}
danielk1977d9c847d2005-01-07 10:42:48 +00001081} ; # ifcapable schema_version
1082
drhd19744f2008-03-18 13:46:53 +00001083# Check to see if TEMP_STORE is memory or disk. Return strings
1084# "memory" or "disk" as appropriate.
1085#
1086proc check_temp_store {} {
dan9131ab92016-04-06 18:20:51 +00001087 db eval {
1088 PRAGMA temp.cache_size = 1;
1089 CREATE TEMP TABLE IF NOT EXISTS a(b);
1090 DELETE FROM a;
1091 INSERT INTO a VALUES(randomblob(1000));
1092 INSERT INTO a SELECT * FROM a;
1093 INSERT INTO a SELECT * FROM a;
1094 INSERT INTO a SELECT * FROM a;
1095 INSERT INTO a SELECT * FROM a;
1096 INSERT INTO a SELECT * FROM a;
1097 INSERT INTO a SELECT * FROM a;
1098 INSERT INTO a SELECT * FROM a;
1099 INSERT INTO a SELECT * FROM a;
1100 }
drhd19744f2008-03-18 13:46:53 +00001101 db eval {PRAGMA database_list} {
1102 if {$name=="temp"} {
danielk197717b90b52008-06-06 11:11:25 +00001103 set bt [btree_from_db db 1]
1104 if {[btree_ismemdb $bt]} {
drhd19744f2008-03-18 13:46:53 +00001105 return "memory"
drhd19744f2008-03-18 13:46:53 +00001106 }
danielk197717b90b52008-06-06 11:11:25 +00001107 return "disk"
drhd19744f2008-03-18 13:46:53 +00001108 }
1109 }
1110 return "unknown"
1111}
1112
drh4ee09b42013-05-01 19:49:27 +00001113# Application_ID
1114#
1115do_test pragma-8.3.1 {
1116 execsql {
1117 PRAGMA application_id;
1118 }
1119} {0}
1120do_test pragma-8.3.2 {
1121 execsql {PRAGMA Application_ID(12345); PRAGMA application_id;}
1122} {12345}
tpoindex9a09a3c2004-12-20 19:01:32 +00001123
1124# Test temp_store and temp_store_directory pragmas
1125#
drh268283b2005-01-08 15:44:25 +00001126ifcapable pager_pragmas {
tpoindex9a09a3c2004-12-20 19:01:32 +00001127do_test pragma-9.1 {
1128 db close
1129 sqlite3 db test.db
1130 execsql {
1131 PRAGMA temp_store;
1132 }
1133} {0}
drhd19744f2008-03-18 13:46:53 +00001134if {$TEMP_STORE<=1} {
1135 do_test pragma-9.1.1 {
1136 check_temp_store
1137 } {disk}
1138} else {
1139 do_test pragma-9.1.1 {
1140 check_temp_store
1141 } {memory}
1142}
1143
tpoindex9a09a3c2004-12-20 19:01:32 +00001144do_test pragma-9.2 {
drhd19744f2008-03-18 13:46:53 +00001145 db close
1146 sqlite3 db test.db
tpoindex9a09a3c2004-12-20 19:01:32 +00001147 execsql {
1148 PRAGMA temp_store=file;
1149 PRAGMA temp_store;
1150 }
1151} {1}
drhd19744f2008-03-18 13:46:53 +00001152if {$TEMP_STORE==3} {
1153 # When TEMP_STORE is 3, always use memory regardless of pragma settings.
1154 do_test pragma-9.2.1 {
1155 check_temp_store
1156 } {memory}
1157} else {
1158 do_test pragma-9.2.1 {
1159 check_temp_store
1160 } {disk}
1161}
1162
tpoindex9a09a3c2004-12-20 19:01:32 +00001163do_test pragma-9.3 {
drhd19744f2008-03-18 13:46:53 +00001164 db close
1165 sqlite3 db test.db
tpoindex9a09a3c2004-12-20 19:01:32 +00001166 execsql {
1167 PRAGMA temp_store=memory;
1168 PRAGMA temp_store;
1169 }
1170} {2}
drhd19744f2008-03-18 13:46:53 +00001171if {$TEMP_STORE==0} {
1172 # When TEMP_STORE is 0, always use the disk regardless of pragma settings.
1173 do_test pragma-9.3.1 {
1174 check_temp_store
1175 } {disk}
1176} else {
1177 do_test pragma-9.3.1 {
1178 check_temp_store
1179 } {memory}
1180}
1181
tpoindex9a09a3c2004-12-20 19:01:32 +00001182do_test pragma-9.4 {
1183 execsql {
1184 PRAGMA temp_store_directory;
1185 }
1186} {}
drh78f82d12008-09-02 00:52:52 +00001187ifcapable wsd {
1188 do_test pragma-9.5 {
mistachkinf8a78462012-03-08 20:00:36 +00001189 set pwd [string map {' ''} [file nativename [get_pwd]]]
drh78f82d12008-09-02 00:52:52 +00001190 execsql "
1191 PRAGMA temp_store_directory='$pwd';
1192 "
1193 } {}
1194 do_test pragma-9.6 {
1195 execsql {
1196 PRAGMA temp_store_directory;
1197 }
mistachkinf8a78462012-03-08 20:00:36 +00001198 } [list [file nativename [get_pwd]]]
drh78f82d12008-09-02 00:52:52 +00001199 do_test pragma-9.7 {
1200 catchsql {
1201 PRAGMA temp_store_directory='/NON/EXISTENT/PATH/FOOBAR';
1202 }
1203 } {1 {not a writable directory}}
1204 do_test pragma-9.8 {
1205 execsql {
1206 PRAGMA temp_store_directory='';
1207 }
1208 } {}
1209 if {![info exists TEMP_STORE] || $TEMP_STORE<=1} {
1210 ifcapable tempdb {
1211 do_test pragma-9.9 {
1212 execsql {
1213 PRAGMA temp_store_directory;
1214 PRAGMA temp_store=FILE;
1215 CREATE TEMP TABLE temp_store_directory_test(a integer);
1216 INSERT INTO temp_store_directory_test values (2);
1217 SELECT * FROM temp_store_directory_test;
1218 }
1219 } {2}
1220 do_test pragma-9.10 {
1221 catchsql "
1222 PRAGMA temp_store_directory='$pwd';
1223 SELECT * FROM temp_store_directory_test;
1224 "
1225 } {1 {no such table: temp_store_directory_test}}
1226 }
tpoindex9a09a3c2004-12-20 19:01:32 +00001227 }
drh78f82d12008-09-02 00:52:52 +00001228}
danielk197795b289b2007-03-30 17:11:12 +00001229do_test pragma-9.11 {
1230 execsql {
1231 PRAGMA temp_store = 0;
1232 PRAGMA temp_store;
1233 }
1234} {0}
1235do_test pragma-9.12 {
1236 execsql {
1237 PRAGMA temp_store = 1;
1238 PRAGMA temp_store;
1239 }
1240} {1}
1241do_test pragma-9.13 {
1242 execsql {
1243 PRAGMA temp_store = 2;
1244 PRAGMA temp_store;
1245 }
1246} {2}
1247do_test pragma-9.14 {
1248 execsql {
1249 PRAGMA temp_store = 3;
1250 PRAGMA temp_store;
1251 }
1252} {0}
danielk197795b289b2007-03-30 17:11:12 +00001253do_test pragma-9.15 {
1254 catchsql {
1255 BEGIN EXCLUSIVE;
1256 CREATE TEMP TABLE temp_table(t);
1257 INSERT INTO temp_table VALUES('valuable data');
1258 PRAGMA temp_store = 1;
1259 }
1260} {1 {temporary storage cannot be changed from within a transaction}}
1261do_test pragma-9.16 {
1262 execsql {
1263 SELECT * FROM temp_table;
1264 COMMIT;
1265 }
1266} {{valuable data}}
danielk1977983e2302008-07-08 07:35:51 +00001267
1268do_test pragma-9.17 {
1269 execsql {
1270 INSERT INTO temp_table VALUES('valuable data II');
1271 SELECT * FROM temp_table;
1272 }
1273} {{valuable data} {valuable data II}}
1274
1275do_test pragma-9.18 {
1276 set rc [catch {
1277 db eval {SELECT t FROM temp_table} {
1278 execsql {pragma temp_store = 1}
1279 }
1280 } msg]
1281 list $rc $msg
1282} {1 {temporary storage cannot be changed from within a transaction}}
1283
drh268283b2005-01-08 15:44:25 +00001284} ;# ifcapable pager_pragmas
tpoindex9a09a3c2004-12-20 19:01:32 +00001285
danielk1977cc6bd382005-01-10 02:48:49 +00001286ifcapable trigger {
1287
1288do_test pragma-10.0 {
1289 catchsql {
1290 DROP TABLE main.t1;
1291 }
1292 execsql {
1293 PRAGMA count_changes = 1;
1294
1295 CREATE TABLE t1(a PRIMARY KEY);
1296 CREATE TABLE t1_mirror(a);
1297 CREATE TABLE t1_mirror2(a);
1298 CREATE TRIGGER t1_bi BEFORE INSERT ON t1 BEGIN
1299 INSERT INTO t1_mirror VALUES(new.a);
1300 END;
1301 CREATE TRIGGER t1_ai AFTER INSERT ON t1 BEGIN
1302 INSERT INTO t1_mirror2 VALUES(new.a);
1303 END;
1304 CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 BEGIN
1305 UPDATE t1_mirror SET a = new.a WHERE a = old.a;
1306 END;
1307 CREATE TRIGGER t1_au AFTER UPDATE ON t1 BEGIN
1308 UPDATE t1_mirror2 SET a = new.a WHERE a = old.a;
1309 END;
1310 CREATE TRIGGER t1_bd BEFORE DELETE ON t1 BEGIN
1311 DELETE FROM t1_mirror WHERE a = old.a;
1312 END;
1313 CREATE TRIGGER t1_ad AFTER DELETE ON t1 BEGIN
1314 DELETE FROM t1_mirror2 WHERE a = old.a;
1315 END;
1316 }
1317} {}
1318
1319do_test pragma-10.1 {
1320 execsql {
1321 INSERT INTO t1 VALUES(randstr(10,10));
1322 }
1323} {1}
1324do_test pragma-10.2 {
1325 execsql {
1326 UPDATE t1 SET a = randstr(10,10);
1327 }
1328} {1}
1329do_test pragma-10.3 {
1330 execsql {
1331 DELETE FROM t1;
1332 }
1333} {1}
1334
1335} ;# ifcapable trigger
1336
danielk197748af65a2005-02-09 03:20:37 +00001337ifcapable schema_pragmas {
1338 do_test pragma-11.1 {
1339 execsql2 {
1340 pragma collation_list;
1341 }
drh2d823312014-11-20 23:11:30 +00001342 } {seq 0 name RTRIM seq 1 name NOCASE seq 2 name BINARY}
danielk197748af65a2005-02-09 03:20:37 +00001343 do_test pragma-11.2 {
1344 db collate New_Collation blah...
1345 execsql {
1346 pragma collation_list;
1347 }
drh2d823312014-11-20 23:11:30 +00001348 } {0 New_Collation 1 RTRIM 2 NOCASE 3 BINARY}
danielk197748af65a2005-02-09 03:20:37 +00001349}
1350
danielk1977ddfb2f02006-02-17 12:25:14 +00001351ifcapable schema_pragmas&&tempdb {
1352 do_test pragma-12.1 {
1353 sqlite3 db2 test.db
1354 execsql {
1355 PRAGMA temp.table_info('abc');
1356 } db2
1357 } {}
1358 db2 close
1359
1360 do_test pragma-12.2 {
1361 sqlite3 db2 test.db
1362 execsql {
1363 PRAGMA temp.default_cache_size = 200;
1364 PRAGMA temp.default_cache_size;
1365 } db2
1366 } {200}
1367 db2 close
1368
1369 do_test pragma-12.3 {
1370 sqlite3 db2 test.db
1371 execsql {
1372 PRAGMA temp.cache_size = 400;
1373 PRAGMA temp.cache_size;
1374 } db2
1375 } {400}
1376 db2 close
1377}
1378
danielk19774b2688a2006-06-20 11:01:07 +00001379ifcapable bloblit {
1380
drh05a82982006-03-19 13:00:25 +00001381do_test pragma-13.1 {
1382 execsql {
1383 DROP TABLE IF EXISTS t4;
1384 PRAGMA vdbe_trace=on;
1385 PRAGMA vdbe_listing=on;
1386 PRAGMA sql_trace=on;
1387 CREATE TABLE t4(a INTEGER PRIMARY KEY,b);
1388 INSERT INTO t4(b) VALUES(x'0123456789abcdef0123456789abcdef0123456789');
1389 INSERT INTO t4(b) VALUES(randstr(30,30));
1390 INSERT INTO t4(b) VALUES(1.23456);
1391 INSERT INTO t4(b) VALUES(NULL);
1392 INSERT INTO t4(b) VALUES(0);
1393 INSERT INTO t4(b) SELECT b||b||b||b FROM t4;
1394 SELECT * FROM t4;
1395 }
1396 execsql {
1397 PRAGMA vdbe_trace=off;
1398 PRAGMA vdbe_listing=off;
1399 PRAGMA sql_trace=off;
1400 }
1401} {}
1402
danielk19774b2688a2006-06-20 11:01:07 +00001403} ;# ifcapable bloblit
1404
danielk197759a93792008-05-15 17:48:20 +00001405ifcapable pager_pragmas {
1406 db close
mistachkinfda06be2011-08-02 00:57:34 +00001407 forcedelete test.db
danielk197759a93792008-05-15 17:48:20 +00001408 sqlite3 db test.db
drh51a74d42015-02-28 01:04:27 +00001409
drhb3366b92015-09-11 20:54:44 +00001410 # EVIDENCE-OF: R-15672-33611 PRAGMA schema.page_count; Return the total
1411 # number of pages in the database file.
drh51a74d42015-02-28 01:04:27 +00001412 #
danielk197759a93792008-05-15 17:48:20 +00001413 do_test pragma-14.1 {
1414 execsql { pragma auto_vacuum = 0 }
drh51a74d42015-02-28 01:04:27 +00001415 execsql { pragma page_count; pragma main.page_count }
1416 } {0 0}
danielk197759a93792008-05-15 17:48:20 +00001417
1418 do_test pragma-14.2 {
1419 execsql {
1420 CREATE TABLE abc(a, b, c);
1421 PRAGMA page_count;
drh51a74d42015-02-28 01:04:27 +00001422 PRAGMA main.page_count;
1423 PRAGMA temp.page_count;
danielk197759a93792008-05-15 17:48:20 +00001424 }
drh51a74d42015-02-28 01:04:27 +00001425 } {2 2 0}
drh5d16a9a2011-10-13 14:41:22 +00001426 do_test pragma-14.2uc {
1427 execsql {pragma PAGE_COUNT}
1428 } {2}
danielk197759a93792008-05-15 17:48:20 +00001429
1430 do_test pragma-14.3 {
1431 execsql {
1432 BEGIN;
1433 CREATE TABLE def(a, b, c);
1434 PRAGMA page_count;
1435 }
1436 } {3}
drh5d16a9a2011-10-13 14:41:22 +00001437 do_test pragma-14.3uc {
1438 execsql {pragma PAGE_COUNT}
1439 } {3}
danielk197759a93792008-05-15 17:48:20 +00001440
1441 do_test pragma-14.4 {
1442 set page_size [db one {pragma page_size}]
1443 expr [file size test.db] / $page_size
1444 } {2}
1445
1446 do_test pragma-14.5 {
1447 execsql {
1448 ROLLBACK;
1449 PRAGMA page_count;
1450 }
1451 } {2}
1452
1453 do_test pragma-14.6 {
mistachkinfda06be2011-08-02 00:57:34 +00001454 forcedelete test2.db
danielk197759a93792008-05-15 17:48:20 +00001455 sqlite3 db2 test2.db
1456 execsql {
1457 PRAGMA auto_vacuum = 0;
1458 CREATE TABLE t1(a, b, c);
1459 CREATE TABLE t2(a, b, c);
1460 CREATE TABLE t3(a, b, c);
1461 CREATE TABLE t4(a, b, c);
1462 } db2
1463 db2 close
1464 execsql {
1465 ATTACH 'test2.db' AS aux;
1466 PRAGMA aux.page_count;
1467 }
1468 } {5}
drh5d16a9a2011-10-13 14:41:22 +00001469 do_test pragma-14.6uc {
1470 execsql {pragma AUX.PAGE_COUNT}
1471 } {5}
danielk197759a93792008-05-15 17:48:20 +00001472}
1473
danielk19778cf6c552008-06-23 16:53:46 +00001474# Test that the value set using the cache_size pragma is not reset when the
1475# schema is reloaded.
1476#
1477ifcapable pager_pragmas {
1478 db close
1479 sqlite3 db test.db
1480 do_test pragma-15.1 {
1481 execsql {
1482 PRAGMA cache_size=59;
1483 PRAGMA cache_size;
1484 }
1485 } {59}
1486 do_test pragma-15.2 {
1487 sqlite3 db2 test.db
1488 execsql {
1489 CREATE TABLE newtable(a, b, c);
1490 } db2
1491 db2 close
1492 } {}
1493 do_test pragma-15.3 {
1494 # Evaluating this statement will cause the schema to be reloaded (because
1495 # the schema was changed by another connection in pragma-15.2). At one
1496 # point there was a bug that reset the cache_size to its default value
1497 # when this happened.
1498 execsql { SELECT * FROM sqlite_master }
1499 execsql { PRAGMA cache_size }
1500 } {59}
1501}
1502
danielk19775558a8a2005-01-17 07:53:44 +00001503# Reset the sqlite3_temp_directory variable for the next run of tests:
1504sqlite3 dbX :memory:
1505dbX eval {PRAGMA temp_store_directory = ""}
1506dbX close
1507
danielk1977838cce42009-01-12 14:01:45 +00001508ifcapable lock_proxy_pragmas&&prefer_proxy_locking {
aswiftaebf4132008-11-21 00:10:35 +00001509 set sqlite_hostid_num 1
1510
1511 set using_proxy 0
1512 foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
1513 set using_proxy $value
1514 }
1515
1516 # Test the lock_proxy_file pragmas.
1517 #
1518 db close
1519 set env(SQLITE_FORCE_PROXY_LOCKING) "0"
1520
1521 sqlite3 db test.db
1522 do_test pragma-16.1 {
1523 execsql {
1524 PRAGMA lock_proxy_file="mylittleproxy";
1525 select * from sqlite_master;
1526 }
1527 execsql {
1528 PRAGMA lock_proxy_file;
1529 }
1530 } {mylittleproxy}
1531
1532 do_test pragma-16.2 {
1533 sqlite3 db2 test.db
1534 execsql {
1535 PRAGMA lock_proxy_file="mylittleproxy";
1536 } db2
1537 } {}
1538
1539 db2 close
1540 do_test pragma-16.2.1 {
1541 sqlite3 db2 test.db
1542 execsql {
1543 PRAGMA lock_proxy_file=":auto:";
1544 select * from sqlite_master;
1545 } db2
1546 execsql {
1547 PRAGMA lock_proxy_file;
1548 } db2
1549 } {mylittleproxy}
1550
1551 db2 close
1552 do_test pragma-16.3 {
1553 sqlite3 db2 test.db
1554 execsql {
1555 PRAGMA lock_proxy_file="myotherproxy";
1556 } db2
1557 catchsql {
1558 select * from sqlite_master;
1559 } db2
1560 } {1 {database is locked}}
1561
1562 do_test pragma-16.4 {
1563 db2 close
1564 db close
1565 sqlite3 db2 test.db
1566 execsql {
1567 PRAGMA lock_proxy_file="myoriginalproxy";
1568 PRAGMA lock_proxy_file="myotherproxy";
1569 PRAGMA lock_proxy_file;
1570 } db2
1571 } {myotherproxy}
1572
1573 db2 close
1574 set env(SQLITE_FORCE_PROXY_LOCKING) "1"
1575 do_test pragma-16.5 {
1576 sqlite3 db2 test.db
1577 execsql {
1578 PRAGMA lock_proxy_file=":auto:";
1579 PRAGMA lock_proxy_file;
1580 } db2
1581 } {myotherproxy}
1582
1583 do_test pragma-16.6 {
1584 db2 close
1585 sqlite3 db2 test2.db
1586 set lockpath [execsql {
1587 PRAGMA lock_proxy_file=":auto:";
1588 PRAGMA lock_proxy_file;
1589 } db2]
1590 string match "*test2.db:auto:" $lockpath
1591 } {1}
1592
1593 set sqlite_hostid_num 2
1594 do_test pragma-16.7 {
dan14d14602010-10-06 16:42:52 +00001595 list [catch {
1596 sqlite3 db test2.db
1597 execsql {
1598 PRAGMA lock_proxy_file=":auto:";
1599 select * from sqlite_master;
1600 }
1601 } msg] $msg
aswiftaebf4132008-11-21 00:10:35 +00001602 } {1 {database is locked}}
1603 db close
1604
1605 do_test pragma-16.8 {
dan14d14602010-10-06 16:42:52 +00001606 list [catch {
1607 sqlite3 db test2.db
1608 execsql { select * from sqlite_master }
1609 } msg] $msg
aswiftaebf4132008-11-21 00:10:35 +00001610 } {1 {database is locked}}
1611
1612 db2 close
1613 do_test pragma-16.8.1 {
1614 execsql {
1615 PRAGMA lock_proxy_file="yetanotherproxy";
1616 PRAGMA lock_proxy_file;
1617 }
1618 } {yetanotherproxy}
1619 do_test pragma-16.8.2 {
1620 execsql {
1621 create table mine(x);
1622 }
1623 } {}
1624
1625 db close
1626 do_test pragma-16.9 {
1627 sqlite3 db proxytest.db
1628 set lockpath2 [execsql {
1629 PRAGMA lock_proxy_file=":auto:";
1630 PRAGMA lock_proxy_file;
1631 } db]
1632 string match "*proxytest.db:auto:" $lockpath2
1633 } {1}
1634
1635 set env(SQLITE_FORCE_PROXY_LOCKING) $using_proxy
1636 set sqlite_hostid_num 0
1637}
drhd2cb50b2009-01-09 21:41:17 +00001638
1639# Parsing of auto_vacuum settings.
1640#
1641foreach {autovac_setting val} {
1642 0 0
1643 1 1
1644 2 2
1645 3 0
1646 -1 0
1647 none 0
1648 NONE 0
1649 NoNe 0
1650 full 1
1651 FULL 1
1652 incremental 2
1653 INCREMENTAL 2
1654 -1234 0
1655 1234 0
1656} {
1657 do_test pragma-17.1.$autovac_setting {
1658 catch {db close}
1659 sqlite3 db :memory:
1660 execsql "
1661 PRAGMA auto_vacuum=$::autovac_setting;
1662 PRAGMA auto_vacuum;
1663 "
1664 } $val
1665}
1666
1667# Parsing of temp_store settings.
1668#
1669foreach {temp_setting val} {
1670 0 0
1671 1 1
1672 2 2
1673 3 0
1674 -1 0
1675 file 1
1676 FILE 1
1677 fIlE 1
1678 memory 2
1679 MEMORY 2
1680 MeMoRy 2
1681} {
1682 do_test pragma-18.1.$temp_setting {
1683 catch {db close}
1684 sqlite3 db :memory:
1685 execsql "
1686 PRAGMA temp_store=$::temp_setting;
1687 PRAGMA temp_store=$::temp_setting;
1688 PRAGMA temp_store;
1689 "
1690 } $val
1691}
1692
drh92c700d2012-02-22 19:56:17 +00001693# The SQLITE_FCNTL_PRAGMA logic, with error handling.
1694#
1695db close
1696testvfs tvfs
1697sqlite3 db test.db -vfs tvfs
1698do_test pragma-19.1 {
1699 catchsql {PRAGMA error}
drha690ff32017-07-07 19:43:23 +00001700} {1 {SQL logic error}}
drh92c700d2012-02-22 19:56:17 +00001701do_test pragma-19.2 {
1702 catchsql {PRAGMA error='This is the error message'}
1703} {1 {This is the error message}}
1704do_test pragma-19.3 {
1705 catchsql {PRAGMA error='7 This is the error message'}
1706} {1 {This is the error message}}
1707do_test pragma-19.4 {
1708 catchsql {PRAGMA error=7}
1709} {1 {out of memory}}
drhc8517f62012-02-22 20:08:49 +00001710do_test pragma-19.5 {
mistachkin5b044542012-03-02 22:41:06 +00001711 file tail [lindex [execsql {PRAGMA filename}] 0]
drhc8517f62012-02-22 20:08:49 +00001712} {test.db}
drh92c700d2012-02-22 19:56:17 +00001713
drhcc716452012-06-06 23:23:23 +00001714if {$tcl_platform(platform)=="windows"} {
mistachkina112d142012-03-14 00:44:01 +00001715# Test data_store_directory pragma
1716#
1717db close
1718sqlite3 db test.db
1719file mkdir data_dir
1720do_test pragma-20.1 {
1721 catchsql {PRAGMA data_store_directory}
1722} {0 {}}
1723do_test pragma-20.2 {
1724 set pwd [string map {' ''} [file nativename [get_pwd]]]
1725 catchsql "PRAGMA data_store_directory='$pwd';"
1726} {0 {}}
1727do_test pragma-20.3 {
1728 catchsql {PRAGMA data_store_directory}
1729} [list 0 [list [file nativename [get_pwd]]]]
1730do_test pragma-20.4 {
1731 set pwd [string map {' ''} [file nativename \
1732 [file join [get_pwd] data_dir]]]
1733 catchsql "PRAGMA data_store_directory='$pwd';"
1734} {0 {}}
1735do_test pragma-20.5 {
1736 sqlite3 db2 test2.db
1737 catchsql "PRAGMA database_list;" db2
1738} [list 0 [list 0 main [file nativename \
1739 [file join [get_pwd] data_dir test2.db]]]]
1740catch {db2 close}
1741do_test pragma-20.6 {
1742 sqlite3 db2 [file join [get_pwd] test2.db]
1743 catchsql "PRAGMA database_list;" db2
1744} [list 0 [list 0 main [file nativename \
1745 [file join [get_pwd] test2.db]]]]
1746catch {db2 close}
1747do_test pragma-20.7 {
1748 catchsql "PRAGMA data_store_directory='';"
1749} {0 {}}
1750do_test pragma-20.8 {
1751 catchsql {PRAGMA data_store_directory}
1752} {0 {}}
drh92c700d2012-02-22 19:56:17 +00001753
mistachkina112d142012-03-14 00:44:01 +00001754forcedelete data_dir
drhcc716452012-06-06 23:23:23 +00001755} ;# endif windows
drhcd61c282002-03-06 22:01:34 +00001756
dan1fed5da2014-02-25 21:01:25 +00001757database_may_be_corrupt
drh7da56b42016-03-14 18:34:42 +00001758if {![nonzero_reserved_bytes]} {
dan1fed5da2014-02-25 21:01:25 +00001759
drh7da56b42016-03-14 18:34:42 +00001760 do_test 21.1 {
1761 # Create a corrupt database in testerr.db. And a non-corrupt at test.db.
1762 #
1763 db close
1764 forcedelete test.db
1765 sqlite3 db test.db
1766 execsql {
1767 PRAGMA page_size = 1024;
1768 PRAGMA auto_vacuum = 0;
1769 CREATE TABLE t1(a PRIMARY KEY, b);
1770 INSERT INTO t1 VALUES(1, 1);
1771 }
1772 for {set i 0} {$i < 10} {incr i} {
1773 execsql { INSERT INTO t1 SELECT a + (1 << $i), b + (1 << $i) FROM t1 }
1774 }
1775 db close
1776 forcecopy test.db testerr.db
1777 hexio_write testerr.db 15000 [string repeat 55 100]
1778 } {100}
1779
1780 set mainerr {*** in database main ***
dan5885e762012-07-16 10:06:12 +00001781Multiple uses for byte 672 of page 15}
drh7da56b42016-03-14 18:34:42 +00001782 set auxerr {*** in database aux ***
dan5885e762012-07-16 10:06:12 +00001783Multiple uses for byte 672 of page 15}
drh7da56b42016-03-14 18:34:42 +00001784
1785 set mainerr {/{\*\*\* in database main \*\*\*
dan597515d2014-02-28 18:39:51 +00001786Multiple uses for byte 672 of page 15}.*/}
drh7da56b42016-03-14 18:34:42 +00001787 set auxerr {/{\*\*\* in database aux \*\*\*
dan597515d2014-02-28 18:39:51 +00001788Multiple uses for byte 672 of page 15}.*/}
drh7da56b42016-03-14 18:34:42 +00001789
1790 do_test 22.2 {
1791 catch { db close }
1792 sqlite3 db testerr.db
1793 execsql { PRAGMA integrity_check }
1794 } $mainerr
1795
1796 do_test 22.3.1 {
1797 catch { db close }
1798 sqlite3 db test.db
1799 execsql {
1800 ATTACH 'testerr.db' AS 'aux';
1801 PRAGMA integrity_check;
1802 }
1803 } $auxerr
1804 do_test 22.3.2 {
1805 execsql { PRAGMA main.integrity_check; }
1806 } {ok}
1807 do_test 22.3.3 {
1808 execsql { PRAGMA aux.integrity_check; }
1809 } $auxerr
1810
1811 do_test 22.4.1 {
1812 catch { db close }
1813 sqlite3 db testerr.db
1814 execsql {
1815 ATTACH 'test.db' AS 'aux';
1816 PRAGMA integrity_check;
1817 }
1818 } $mainerr
1819 do_test 22.4.2 {
1820 execsql { PRAGMA main.integrity_check; }
1821 } $mainerr
1822 do_test 22.4.3 {
1823 execsql { PRAGMA aux.integrity_check; }
1824 } {ok}
1825}
1826
drhc95e01d2013-02-14 16:16:05 +00001827db close
1828forcedelete test.db test.db-wal test.db-journal
1829sqlite3 db test.db
1830sqlite3 db2 test.db
1831do_test 23.1 {
1832 db eval {
1833 CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c,d);
1834 CREATE INDEX i1 ON t1(b,c);
1835 CREATE INDEX i2 ON t1(c,d);
drh7be0fd92015-03-05 15:34:15 +00001836 CREATE INDEX i2x ON t1(d COLLATE nocase, c DESC);
drhc95e01d2013-02-14 16:16:05 +00001837 CREATE TABLE t2(x INTEGER REFERENCES t1);
1838 }
1839 db2 eval {SELECT name FROM sqlite_master}
drh7be0fd92015-03-05 15:34:15 +00001840} {t1 i1 i2 i2x t2}
drhc228be52015-01-31 02:00:01 +00001841do_test 23.2a {
drhc95e01d2013-02-14 16:16:05 +00001842 db eval {
1843 DROP INDEX i2;
1844 CREATE INDEX i2 ON t1(c,d,b);
1845 }
drhc228be52015-01-31 02:00:01 +00001846 capture_pragma db2 out {PRAGMA index_info(i2)}
drh5e7028c2015-03-05 14:29:02 +00001847 db2 eval {SELECT cid, name, '|' FROM out ORDER BY seqno}
1848} {2 c | 3 d | 1 b |}
drh7be0fd92015-03-05 15:34:15 +00001849
drhb3366b92015-09-11 20:54:44 +00001850# EVIDENCE-OF: R-56143-29319 PRAGMA schema.index_xinfo(index-name); This
1851# pragma returns information about every column in an index.
drh7be0fd92015-03-05 15:34:15 +00001852#
1853# EVIDENCE-OF: R-45970-35618 Unlike this index_info pragma, this pragma
1854# returns information about every column in the index, not just the key
1855# columns.
1856#
drhc228be52015-01-31 02:00:01 +00001857do_test 23.2b {
1858 capture_pragma db2 out {PRAGMA index_xinfo(i2)}
1859 db2 eval {SELECT cid, name, "desc", coll, "key", '|' FROM out ORDER BY seqno}
1860} {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 +00001861
1862# (The first column of output from PRAGMA index_xinfo is...)
1863# EVIDENCE-OF: R-00197-14279 The rank of the column within the index. (0
1864# means left-most. Key columns come before auxiliary columns.)
1865#
1866# (The second column of output from PRAGMA index_xinfo is...)
1867# EVIDENCE-OF: R-40889-06838 The rank of the column within the table
1868# being indexed, or -1 if the index-column is the rowid of the table
1869# being indexed.
1870#
1871# (The third column of output from PRAGMA index_xinfo is...)
1872# EVIDENCE-OF: R-22751-28901 The name of the column being indexed, or
1873# NULL if the index-column is the rowid of the table being indexed.
1874#
1875# (The fourth column of output from PRAGMA index_xinfo is...)
1876# EVIDENCE-OF: R-11847-09179 1 if the index-column is sorted in reverse
1877# (DESC) order by the index and 0 otherwise.
1878#
1879# (The fifth column of output from PRAGMA index_xinfo is...)
1880# EVIDENCE-OF: R-15313-19540 The name for the collating sequence used to
1881# compare values in the index-column.
1882#
1883# (The sixth column of output from PRAGMA index_xinfo is...)
1884# EVIDENCE-OF: R-14310-64553 1 if the index-column is a key column and 0
1885# if the index-column is an auxiliary column.
1886#
1887do_test 23.2c {
1888 db2 eval {PRAGMA index_xinfo(i2)}
1889} {0 2 c 0 BINARY 1 1 3 d 0 BINARY 1 2 1 b 0 BINARY 1 3 -1 {} 0 BINARY 0}
1890do_test 23.2d {
1891 db2 eval {PRAGMA index_xinfo(i2x)}
1892} {0 3 d 0 nocase 1 1 2 c 1 BINARY 1 2 -1 {} 0 BINARY 0}
1893
drhb3366b92015-09-11 20:54:44 +00001894# EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This
1895# pragma returns one row for each index associated with the given table.
drh7be0fd92015-03-05 15:34:15 +00001896#
1897# (The first column of output from PRAGMA index_list is...)
1898# EVIDENCE-OF: R-02753-24748 A sequence number assigned to each index
1899# for internal tracking purposes.
1900#
1901# (The second column of output from PRAGMA index_list is...)
1902# EVIDENCE-OF: R-35496-03635 The name of the index.
1903#
1904# (The third column of output from PRAGMA index_list is...)
1905# EVIDENCE-OF: R-57301-64506 "1" if the index is UNIQUE and "0" if not.
1906#
1907# (The fourth column of output from PRAGMA index_list is...)
1908# EVIDENCE-OF: R-36609-39554 "c" if the index was created by a CREATE
1909# INDEX statement, "u" if the index was created by a UNIQUE constraint,
1910# or "pk" if the index was created by a PRIMARY KEY constraint.
1911#
drhc95e01d2013-02-14 16:16:05 +00001912do_test 23.3 {
1913 db eval {
1914 CREATE INDEX i3 ON t1(d,b,c);
1915 }
drhc228be52015-01-31 02:00:01 +00001916 capture_pragma db2 out {PRAGMA index_list(t1)}
drh7be0fd92015-03-05 15:34:15 +00001917 db2 eval {SELECT seq, name, "unique", origin, '|' FROM out ORDER BY seq}
1918} {0 i3 0 c | 1 i2 0 c | 2 i2x 0 c | 3 i1 0 c |}
drhc95e01d2013-02-14 16:16:05 +00001919do_test 23.4 {
1920 db eval {
1921 ALTER TABLE t1 ADD COLUMN e;
1922 }
1923 db2 eval {
1924 PRAGMA table_info(t1);
1925 }
1926} {/4 e {} 0 {} 0/}
1927do_test 23.5 {
1928 db eval {
1929 DROP TABLE t2;
1930 CREATE TABLE t2(x, y INTEGER REFERENCES t1);
1931 }
1932 db2 eval {
1933 PRAGMA foreign_key_list(t2);
1934 }
1935} {0 0 t1 y {} {NO ACTION} {NO ACTION} NONE}
drh3e1e14d2017-09-12 00:24:45 +00001936db2 close
drhc95e01d2013-02-14 16:16:05 +00001937
drhefeaec32017-10-23 16:34:07 +00001938ifcapable !has_codec {
1939 reset_db
1940 do_execsql_test 24.0 {
1941 PRAGMA page_size = 1024;
1942 CREATE TABLE t1(a, b, c);
1943 CREATE INDEX i1 ON t1(b);
1944 INSERT INTO t1 VALUES('a', 'b', 'c');
1945 PRAGMA integrity_check;
1946 } {ok}
1947
1948 set r [db one {SELECT rootpage FROM sqlite_master WHERE name = 't1'}]
1949 db close
1950 hexio_write test.db [expr $r*1024 - 16] 000000000000000701040f0f1f616263
1951
1952 sqlite3 db test.db
1953 do_catchsql_test 24.1 {
1954 SELECT * FROM t1;
1955 } {1 {database disk image is malformed}}
1956 do_catchsql_test 24.2 {
1957 PRAGMA integrity_check;
1958 } {0 {{database disk image is malformed}}}
1959}
dan1fed5da2014-02-25 21:01:25 +00001960database_never_corrupt
drhcd61c282002-03-06 22:01:34 +00001961finish_test