blob: 04f5bd0fbec750297e1d4242066c084f2a59300e [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}
danb84b38f2019-04-04 17:58:34 +0000254
255do_execsql_test 1.15.1 {
256 PRAGMA default_cache_size = 0;
257}
258do_execsql_test 1.15.2 {
259 PRAGMA default_cache_size;
260} $DFLT_CACHE_SZ
261do_execsql_test 1.15.3 {
262 PRAGMA default_cache_size = -500;
263}
264do_execsql_test 1.15.4 {
265 PRAGMA default_cache_size;
266} 500
267do_execsql_test 1.15.3 {
268 PRAGMA default_cache_size = 500;
269}
270do_execsql_test 1.15.4 {
271 PRAGMA default_cache_size;
272} 500
273db close
274hexio_write test.db 48 FFFFFF00
275sqlite3 db test.db
276do_execsql_test 1.15.4 {
277 PRAGMA default_cache_size;
278} 256
danielk1977c7b4a442004-11-23 10:52:51 +0000279} ;# ifcapable pager_pragmas
drh5260f7e2004-06-26 19:35:29 +0000280
281# Test turning "flag" pragmas on and off.
282#
danielk19776338c762007-05-17 16:38:30 +0000283ifcapable debug {
284 # Pragma "vdbe_listing" is only available if compiled with SQLITE_DEBUG
285 #
286 do_test pragma-1.15 {
287 execsql {
288 PRAGMA vdbe_listing=YES;
289 PRAGMA vdbe_listing;
290 }
291 } {1}
292 do_test pragma-1.16 {
293 execsql {
294 PRAGMA vdbe_listing=NO;
295 PRAGMA vdbe_listing;
296 }
297 } {0}
298}
299
drh5260f7e2004-06-26 19:35:29 +0000300do_test pragma-1.17 {
301 execsql {
302 PRAGMA parser_trace=ON;
303 PRAGMA parser_trace=OFF;
304 }
305} {}
306do_test pragma-1.18 {
307 execsql {
308 PRAGMA bogus = -1234; -- Parsing of negative values
309 }
310} {}
311
danielk197791cf71b2004-06-26 06:37:06 +0000312# Test modifying the safety_level of an attached database.
danielk19775a8f9372007-10-09 08:29:32 +0000313ifcapable pager_pragmas&&attach {
314 do_test pragma-2.1 {
mistachkinfda06be2011-08-02 00:57:34 +0000315 forcedelete test2.db
316 forcedelete test2.db-journal
danielk19775a8f9372007-10-09 08:29:32 +0000317 execsql {
318 ATTACH 'test2.db' AS aux;
319 }
320 } {}
321 do_test pragma-2.2 {
322 execsql {
323 pragma aux.synchronous;
324 }
325 } {2}
326 do_test pragma-2.3 {
327 execsql {
328 pragma aux.synchronous = OFF;
329 pragma aux.synchronous;
330 pragma synchronous;
331 }
332 } {0 2}
333 do_test pragma-2.4 {
334 execsql {
335 pragma aux.synchronous = ON;
336 pragma synchronous;
337 pragma aux.synchronous;
338 }
339 } {2 1}
danielk1977c7b4a442004-11-23 10:52:51 +0000340} ;# ifcapable pager_pragmas
drh4303fee2003-02-15 23:09:17 +0000341
drhed717fe2003-06-15 23:42:24 +0000342# Construct a corrupted index and make sure the integrity_check
343# pragma finds it.
344#
drh25d65432004-07-22 15:02:25 +0000345# These tests won't work if the database is encrypted
346#
drhed717fe2003-06-15 23:42:24 +0000347do_test pragma-3.1 {
drh1e9daa62007-04-06 21:42:22 +0000348 db close
mistachkinfda06be2011-08-02 00:57:34 +0000349 forcedelete test.db test.db-journal
drh1e9daa62007-04-06 21:42:22 +0000350 sqlite3 db test.db
drhed717fe2003-06-15 23:42:24 +0000351 execsql {
drh1e9daa62007-04-06 21:42:22 +0000352 PRAGMA auto_vacuum=OFF;
drhed717fe2003-06-15 23:42:24 +0000353 BEGIN;
354 CREATE TABLE t2(a,b,c);
355 CREATE INDEX i2 ON t2(a);
356 INSERT INTO t2 VALUES(11,2,3);
357 INSERT INTO t2 VALUES(22,3,4);
358 COMMIT;
359 SELECT rowid, * from t2;
360 }
361} {1 11 2 3 2 22 3 4}
danielk19775a8f9372007-10-09 08:29:32 +0000362ifcapable attach {
363 if {![sqlite3 -has-codec] && $sqlite_options(integrityck)} {
364 do_test pragma-3.2 {
drhbb8a2792008-03-19 00:21:30 +0000365 db eval {SELECT rootpage FROM sqlite_master WHERE name='i2'} break
366 set pgsz [db eval {PRAGMA page_size}]
367 # overwrite the header on the rootpage of the index in order to
368 # make the index appear to be empty.
369 #
370 set offset [expr {$pgsz*($rootpage-1)}]
371 hexio_write test.db $offset 0a00000000040000000000
372 db close
373 sqlite3 db test.db
danielk19775a8f9372007-10-09 08:29:32 +0000374 execsql {PRAGMA integrity_check}
drh6fbe41a2013-10-30 20:22:55 +0000375 } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000376 do_test pragma-3.3 {
377 execsql {PRAGMA integrity_check=1}
drh6fbe41a2013-10-30 20:22:55 +0000378 } {{row 1 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000379 do_test pragma-3.4 {
380 execsql {
381 ATTACH DATABASE 'test.db' AS t2;
382 PRAGMA integrity_check
383 }
drh6fbe41a2013-10-30 20:22:55 +0000384 } {{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 +0000385 do_test pragma-3.5 {
386 execsql {
drhbb8a2792008-03-19 00:21:30 +0000387 PRAGMA integrity_check=4
danielk19775a8f9372007-10-09 08:29:32 +0000388 }
drh6fbe41a2013-10-30 20:22:55 +0000389 } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2}}
drh17d2d592020-07-23 00:45:06 +0000390 do_catchsql_test pragma-3.6 {
391 PRAGMA integrity_check=xyz
392 } {1 {no such table: xyz}}
393 do_catchsql_test pragma-3.6b {
394 PRAGMA integrity_check=t2
395 } {0 {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}}
396 do_catchsql_test pragma-3.6c {
397 PRAGMA integrity_check=sqlite_schema
398 } {0 ok}
danielk19775a8f9372007-10-09 08:29:32 +0000399 do_test pragma-3.7 {
400 execsql {
401 PRAGMA integrity_check=0
402 }
drh6fbe41a2013-10-30 20:22:55 +0000403 } {{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 +0000404
405 # Add additional corruption by appending unused pages to the end of
406 # the database file testerr.db
407 #
408 do_test pragma-3.8 {
409 execsql {DETACH t2}
mistachkinfda06be2011-08-02 00:57:34 +0000410 forcedelete testerr.db testerr.db-journal
danielk19775a8f9372007-10-09 08:29:32 +0000411 set out [open testerr.db w]
412 fconfigure $out -translation binary
413 set in [open test.db r]
414 fconfigure $in -translation binary
415 puts -nonewline $out [read $in]
416 seek $in 0
417 puts -nonewline $out [read $in]
418 close $in
419 close $out
drhdd3cd972010-03-27 17:12:36 +0000420 hexio_write testerr.db 28 00000000
danielk19775a8f9372007-10-09 08:29:32 +0000421 execsql {REINDEX t2}
422 execsql {PRAGMA integrity_check}
423 } {ok}
danielk197741c58b72007-12-29 13:39:19 +0000424 do_test pragma-3.8.1 {
425 execsql {PRAGMA quick_check}
426 } {ok}
drh5d16a9a2011-10-13 14:41:22 +0000427 do_test pragma-3.8.2 {
428 execsql {PRAGMA QUICK_CHECK}
429 } {ok}
drh17d2d592020-07-23 00:45:06 +0000430 do_test pragma-3.9a {
danielk19775a8f9372007-10-09 08:29:32 +0000431 execsql {
432 ATTACH 'testerr.db' AS t2;
433 PRAGMA integrity_check
434 }
435 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000436Page 4 is never used
437Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000438Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
drh17d2d592020-07-23 00:45:06 +0000439 do_execsql_test pragma-3.9b {
440 PRAGMA t2.integrity_check=t2;
441 } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
442 do_execsql_test pragma-3.9c {
443 PRAGMA t2.integrity_check=sqlite_schema;
444 } {ok}
danielk19775a8f9372007-10-09 08:29:32 +0000445 do_test pragma-3.10 {
446 execsql {
447 PRAGMA integrity_check=1
448 }
449 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000450Page 4 is never used}}
danielk19775a8f9372007-10-09 08:29:32 +0000451 do_test pragma-3.11 {
452 execsql {
453 PRAGMA integrity_check=5
454 }
455 } {{*** in database t2 ***
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}}
danielk19775a8f9372007-10-09 08:29:32 +0000459 do_test pragma-3.12 {
460 execsql {
461 PRAGMA integrity_check=4
462 }
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}}
danielk19775a8f9372007-10-09 08:29:32 +0000467 do_test pragma-3.13 {
468 execsql {
469 PRAGMA integrity_check=3
470 }
471 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000472Page 4 is never used
473Page 5 is never used
474Page 6 is never used}}
danielk19775a8f9372007-10-09 08:29:32 +0000475 do_test pragma-3.14 {
476 execsql {
477 PRAGMA integrity_check(2)
478 }
479 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000480Page 4 is never used
481Page 5 is never used}}
danielk19775a8f9372007-10-09 08:29:32 +0000482 do_test pragma-3.15 {
483 execsql {
484 ATTACH 'testerr.db' AS t3;
485 PRAGMA integrity_check
486 }
487 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000488Page 4 is never used
489Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000490Page 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 +0000491Page 4 is never used
492Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000493Page 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 +0000494 do_test pragma-3.16 {
495 execsql {
drhbb8a2792008-03-19 00:21:30 +0000496 PRAGMA integrity_check(10)
danielk19775a8f9372007-10-09 08:29:32 +0000497 }
498 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000499Page 4 is never used
500Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000501Page 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 +0000502Page 4 is never used
503Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000504Page 6 is never used} {row 1 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000505 do_test pragma-3.17 {
506 execsql {
drhbb8a2792008-03-19 00:21:30 +0000507 PRAGMA integrity_check=8
danielk19775a8f9372007-10-09 08:29:32 +0000508 }
509 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000510Page 4 is never used
511Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000512Page 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 +0000513Page 4 is never used
514Page 5 is never used}}
danielk19775a8f9372007-10-09 08:29:32 +0000515 do_test pragma-3.18 {
516 execsql {
517 PRAGMA integrity_check=4
518 }
519 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000520Page 4 is never used
521Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000522Page 6 is never used} {row 1 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000523 }
drhd2cb50b2009-01-09 21:41:17 +0000524 do_test pragma-3.19 {
525 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +0000526 forcedelete test.db test.db-journal
drhd2cb50b2009-01-09 21:41:17 +0000527 sqlite3 db test.db
528 db eval {PRAGMA integrity_check}
529 } {ok}
drh25d65432004-07-22 15:02:25 +0000530}
drhcefc87f2014-08-01 01:40:33 +0000531
532# Verify that PRAGMA integrity_check catches UNIQUE and NOT NULL
533# constraint violations.
534#
drh6ab91a72018-11-07 02:17:01 +0000535sqlite3_db_config db DEFENSIVE 0
drhcefc87f2014-08-01 01:40:33 +0000536do_execsql_test pragma-3.20 {
537 CREATE TABLE t1(a,b);
538 CREATE INDEX t1a ON t1(a);
539 INSERT INTO t1 VALUES(1,1),(2,2),(3,3),(2,4),(NULL,5),(NULL,6);
540 PRAGMA writable_schema=ON;
541 UPDATE sqlite_master SET sql='CREATE UNIQUE INDEX t1a ON t1(a)'
542 WHERE name='t1a';
543 UPDATE sqlite_master SET sql='CREATE TABLE t1(a NOT NULL,b)'
544 WHERE name='t1';
545 PRAGMA writable_schema=OFF;
546 ALTER TABLE t1 RENAME TO t1x;
547 PRAGMA integrity_check;
548} {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a} {NULL value in t1x.a}}
549do_execsql_test pragma-3.21 {
550 PRAGMA integrity_check(3);
551} {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a}}
552do_execsql_test pragma-3.22 {
553 PRAGMA integrity_check(2);
554} {{non-unique entry in index t1a} {NULL value in t1x.a}}
drh7efa4262014-12-16 00:08:31 +0000555do_execsql_test pragma-3.23 {
drhcefc87f2014-08-01 01:40:33 +0000556 PRAGMA integrity_check(1);
557} {{non-unique entry in index t1a}}
danielk197791cf71b2004-06-26 06:37:06 +0000558
drh7efa4262014-12-16 00:08:31 +0000559# PRAGMA integrity check (or more specifically the sqlite3BtreeCount()
560# interface) used to leave index cursors in an inconsistent state
561# which could result in an assertion fault in sqlite3BtreeKey()
562# called from saveCursorPosition() if content is removed from the
563# index while the integrity_check is still running. This test verifies
564# that problem has been fixed.
565#
566do_test pragma-3.30 {
567 db close
568 delete_file test.db
569 sqlite3 db test.db
570 db eval {
571 CREATE TABLE t1(a,b,c);
572 WITH RECURSIVE
573 c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<100)
574 INSERT INTO t1(a,b,c) SELECT i, printf('xyz%08x',i), 2000-i FROM c;
575 CREATE INDEX t1a ON t1(a);
576 CREATE INDEX t1bc ON t1(b,c);
577 }
578 db eval {PRAGMA integrity_check} {
579 db eval {DELETE FROM t1}
580 }
581} {}
582
danielk197791cf71b2004-06-26 06:37:06 +0000583# Test modifying the cache_size of an attached database.
danielk19775a8f9372007-10-09 08:29:32 +0000584ifcapable pager_pragmas&&attach {
danielk197791cf71b2004-06-26 06:37:06 +0000585do_test pragma-4.1 {
586 execsql {
drh1e9daa62007-04-06 21:42:22 +0000587 ATTACH 'test2.db' AS aux;
danielk197791cf71b2004-06-26 06:37:06 +0000588 pragma aux.cache_size;
589 pragma aux.default_cache_size;
590 }
drh1e9daa62007-04-06 21:42:22 +0000591} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000592do_test pragma-4.2 {
593 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000594 pragma aux.cache_size = 50;
595 pragma aux.cache_size;
596 pragma aux.default_cache_size;
597 }
drh1e9daa62007-04-06 21:42:22 +0000598} [list 50 $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000599do_test pragma-4.3 {
600 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000601 pragma aux.default_cache_size = 456;
602 pragma aux.cache_size;
603 pragma aux.default_cache_size;
604 }
605} {456 456}
drh1bdd9b52004-04-23 17:04:44 +0000606do_test pragma-4.4 {
607 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000608 pragma cache_size;
609 pragma default_cache_size;
610 }
drh1e9daa62007-04-06 21:42:22 +0000611} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000612do_test pragma-4.5 {
613 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000614 DETACH aux;
615 ATTACH 'test3.db' AS aux;
616 pragma aux.cache_size;
617 pragma aux.default_cache_size;
618 }
drh1e9daa62007-04-06 21:42:22 +0000619} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000620do_test pragma-4.6 {
621 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000622 DETACH aux;
623 ATTACH 'test2.db' AS aux;
624 pragma aux.cache_size;
625 pragma aux.default_cache_size;
626 }
627} {456 456}
danielk1977c7b4a442004-11-23 10:52:51 +0000628} ;# ifcapable pager_pragmas
drh1bdd9b52004-04-23 17:04:44 +0000629
danielk197791cf71b2004-06-26 06:37:06 +0000630# Test that modifying the sync-level in the middle of a transaction is
631# disallowed.
danielk1977c7b4a442004-11-23 10:52:51 +0000632ifcapable pager_pragmas {
danielk197791cf71b2004-06-26 06:37:06 +0000633do_test pragma-5.0 {
drh1bdd9b52004-04-23 17:04:44 +0000634 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000635 pragma synchronous;
636 }
637} {2}
638do_test pragma-5.1 {
drh1bdd9b52004-04-23 17:04:44 +0000639 catchsql {
640 BEGIN;
danielk197791cf71b2004-06-26 06:37:06 +0000641 pragma synchronous = OFF;
642 }
643} {1 {Safety level may not be changed inside a transaction}}
644do_test pragma-5.2 {
645 execsql {
646 pragma synchronous;
647 }
648} {2}
drh5260f7e2004-06-26 19:35:29 +0000649catchsql {COMMIT;}
danielk1977c7b4a442004-11-23 10:52:51 +0000650} ;# ifcapable pager_pragmas
drh5260f7e2004-06-26 19:35:29 +0000651
652# Test schema-query pragmas
653#
danielk197727188fb2004-11-23 10:13:03 +0000654ifcapable schema_pragmas {
danielk19775a8f9372007-10-09 08:29:32 +0000655ifcapable tempdb&&attach {
danielk197753c0f742005-03-29 03:10:59 +0000656 do_test pragma-6.1 {
657 set res {}
658 execsql {SELECT * FROM sqlite_temp_master}
659 foreach {idx name file} [execsql {pragma database_list}] {
660 lappend res $idx $name
661 }
662 set res
663 } {0 main 1 temp 2 aux}
664}
drh5260f7e2004-06-26 19:35:29 +0000665do_test pragma-6.2 {
666 execsql {
drha6dddd92016-04-18 15:46:14 +0000667 CREATE TABLE t2(a TYPE_X, b [TYPE_Y], c "TYPE_Z");
drh5260f7e2004-06-26 19:35:29 +0000668 pragma table_info(t2)
669 }
drha6dddd92016-04-18 15:46:14 +0000670} {0 a TYPE_X 0 {} 0 1 b TYPE_Y 0 {} 0 2 c TYPE_Z 0 {} 0}
drhd2cb50b2009-01-09 21:41:17 +0000671do_test pragma-6.2.1 {
672 execsql {
673 pragma table_info;
674 }
675} {}
drh736c7d42006-11-30 13:06:37 +0000676db nullvalue <<NULL>>
drh417ec632006-08-14 14:23:41 +0000677do_test pragma-6.2.2 {
678 execsql {
drh736c7d42006-11-30 13:06:37 +0000679 CREATE TABLE t5(
680 a TEXT DEFAULT CURRENT_TIMESTAMP,
681 b DEFAULT (5+3),
682 c TEXT,
683 d INTEGER DEFAULT NULL,
drh384b7fe2013-01-01 13:55:31 +0000684 e TEXT DEFAULT '',
685 UNIQUE(b,c,d),
686 PRIMARY KEY(e,b,c)
drh736c7d42006-11-30 13:06:37 +0000687 );
drh417ec632006-08-14 14:23:41 +0000688 PRAGMA table_info(t5);
689 }
drh384b7fe2013-01-01 13:55:31 +0000690} {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 +0000691db nullvalue {}
drh384b7fe2013-01-01 13:55:31 +0000692do_test pragma-6.2.3 {
693 execsql {
694 CREATE TABLE t2_3(a,b INTEGER PRIMARY KEY,c);
695 pragma table_info(t2_3)
696 }
697} {0 a {} 0 {} 0 1 b INTEGER 0 {} 1 2 c {} 0 {} 0}
drh6bf89572004-11-03 16:27:01 +0000698ifcapable {foreignkey} {
drhd2cb50b2009-01-09 21:41:17 +0000699 do_test pragma-6.3.1 {
drh6bf89572004-11-03 16:27:01 +0000700 execsql {
701 CREATE TABLE t3(a int references t2(b), b UNIQUE);
702 pragma foreign_key_list(t3);
703 }
dan1da40a32009-09-19 17:00:31 +0000704 } {0 0 t2 a b {NO ACTION} {NO ACTION} NONE}
drhd2cb50b2009-01-09 21:41:17 +0000705 do_test pragma-6.3.2 {
706 execsql {
707 pragma foreign_key_list;
708 }
709 } {}
710 do_test pragma-6.3.3 {
711 execsql {
712 pragma foreign_key_list(t3_bogus);
713 }
714 } {}
715 do_test pragma-6.3.4 {
716 execsql {
717 pragma foreign_key_list(t5);
718 }
719 } {}
drh6bf89572004-11-03 16:27:01 +0000720 do_test pragma-6.4 {
drhc228be52015-01-31 02:00:01 +0000721 capture_pragma db out {
drh6bf89572004-11-03 16:27:01 +0000722 pragma index_list(t3);
723 }
drhc228be52015-01-31 02:00:01 +0000724 db eval {SELECT seq, "name", "unique" FROM out ORDER BY seq}
drh3ef26152013-10-12 20:22:00 +0000725 } {0 sqlite_autoindex_t3_1 1}
drh6bf89572004-11-03 16:27:01 +0000726}
727ifcapable {!foreignkey} {
728 execsql {CREATE TABLE t3(a,b UNIQUE)}
729}
drhd2cb50b2009-01-09 21:41:17 +0000730do_test pragma-6.5.1 {
drh5260f7e2004-06-26 19:35:29 +0000731 execsql {
732 CREATE INDEX t3i1 ON t3(a,b);
drhc228be52015-01-31 02:00:01 +0000733 }
734 capture_pragma db out {
drh5260f7e2004-06-26 19:35:29 +0000735 pragma index_info(t3i1);
736 }
drhc228be52015-01-31 02:00:01 +0000737 db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
drh5260f7e2004-06-26 19:35:29 +0000738} {0 0 a 1 1 b}
drh7be0fd92015-03-05 15:34:15 +0000739
740# EVIDENCE-OF: R-23114-21695 The auxiliary index-columns are not shown
741# by the index_info pragma, but they are listed by the index_xinfo
742# pragma.
743#
744do_test pragma-6.5.1b {
745 capture_pragma db out {PRAGMA index_xinfo(t3i1)}
746 db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
747} {0 0 a 1 1 b 2 -1 {}}
748
749
drhb3366b92015-09-11 20:54:44 +0000750# EVIDENCE-OF: R-29448-60346 PRAGMA schema.index_info(index-name); This
751# pragma returns one row for each key column in the named index.
drh7be0fd92015-03-05 15:34:15 +0000752#
753# (The first column of output from PRAGMA index_info is...)
754# EVIDENCE-OF: R-34186-52914 The rank of the column within the index. (0
755# means left-most.)
756#
757# (The second column of output from PRAGMA index_info is...)
758# EVIDENCE-OF: R-65019-08383 The rank of the column within the table
759# being indexed.
760#
761# (The third column of output from PRAGMA index_info is...)
762# EVIDENCE-OF: R-09773-34266 The name of the column being indexed.
763#
764do_execsql_test pragma-6.5.1c {
765 CREATE INDEX t3i2 ON t3(b,a);
766 PRAGMA index_info='t3i2';
767 DROP INDEX t3i2;
768} {0 1 b 1 0 a}
769
drhd2cb50b2009-01-09 21:41:17 +0000770do_test pragma-6.5.2 {
771 execsql {
772 pragma index_info(t3i1_bogus);
773 }
774} {}
danielk1977260d8a62008-08-20 16:34:24 +0000775
776ifcapable tempdb {
777 # Test for ticket #3320. When a temp table of the same name exists, make
778 # sure the schema of the main table can still be queried using
779 # "pragma table_info":
780 do_test pragma-6.6.1 {
781 execsql {
782 CREATE TABLE trial(col_main);
783 CREATE TEMP TABLE trial(col_temp);
784 }
785 } {}
786 do_test pragma-6.6.2 {
787 execsql {
788 PRAGMA table_info(trial);
789 }
790 } {0 col_temp {} 0 {} 0}
791 do_test pragma-6.6.3 {
792 execsql {
793 PRAGMA temp.table_info(trial);
794 }
795 } {0 col_temp {} 0 {} 0}
796 do_test pragma-6.6.4 {
797 execsql {
798 PRAGMA main.table_info(trial);
799 }
800 } {0 col_main {} 0 {} 0}
801}
danielk1977f96a3772008-10-23 05:45:07 +0000802
danielk1977f96a3772008-10-23 05:45:07 +0000803do_test pragma-6.7 {
804 execsql {
805 CREATE TABLE test_table(
806 one INT NOT NULL DEFAULT -1,
807 two text,
808 three VARCHAR(45, 65) DEFAULT 'abcde',
809 four REAL DEFAULT X'abcdef',
810 five DEFAULT CURRENT_TIME
811 );
danielk1977f96a3772008-10-23 05:45:07 +0000812 }
drhc228be52015-01-31 02:00:01 +0000813 capture_pragma db out {PRAGMA table_info(test_table)}
814 db eval {SELECT cid, "name", type, "notnull", dflt_value, pk FROM out
815 ORDER BY cid}
danielk1977f96a3772008-10-23 05:45:07 +0000816} [concat \
817 {0 one INT 1 -1 0} \
818 {1 two text 0 {} 0} \
819 {2 three {VARCHAR(45, 65)} 0 'abcde' 0} \
820 {3 four REAL 0 X'abcdef' 0} \
821 {4 five {} 0 CURRENT_TIME 0} \
822]
drh1b678962015-04-15 07:19:27 +0000823do_test pragma-6.8 {
824 execsql {
825 CREATE TABLE t68(a,b,c,PRIMARY KEY(a,b,a,c));
826 PRAGMA table_info(t68);
827 }
828} [concat \
829 {0 a {} 0 {} 1} \
830 {1 b {} 0 {} 2} \
831 {2 c {} 0 {} 4} \
832]
danielk197727188fb2004-11-23 10:13:03 +0000833} ;# ifcapable schema_pragmas
drh5260f7e2004-06-26 19:35:29 +0000834# Miscellaneous tests
835#
danielk197727188fb2004-11-23 10:13:03 +0000836ifcapable schema_pragmas {
drhb3366b92015-09-11 20:54:44 +0000837# EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This
838# pragma returns one row for each index associated with the given table.
839#
drhd2cb50b2009-01-09 21:41:17 +0000840do_test pragma-7.1.1 {
drh5260f7e2004-06-26 19:35:29 +0000841 # Make sure a pragma knows to read the schema if it needs to
842 db close
843 sqlite3 db test.db
drhc228be52015-01-31 02:00:01 +0000844 capture_pragma db out "PRAGMA index_list(t3)"
845 db eval {SELECT name, "origin" FROM out ORDER BY name DESC}
846} {t3i1 c sqlite_autoindex_t3_1 u}
drhd2cb50b2009-01-09 21:41:17 +0000847do_test pragma-7.1.2 {
848 execsql {
849 pragma index_list(t3_bogus);
850 }
851} {}
danielk197727188fb2004-11-23 10:13:03 +0000852} ;# ifcapable schema_pragmas
drh6c626082004-11-14 21:56:29 +0000853ifcapable {utf16} {
dancb354602010-07-08 09:44:42 +0000854 if {[permutation] == ""} {
855 do_test pragma-7.2 {
856 db close
857 sqlite3 db test.db
858 catchsql {
859 pragma encoding=bogus;
860 }
861 } {1 {unsupported encoding: bogus}}
862 }
drh6c626082004-11-14 21:56:29 +0000863}
danielk197753c0f742005-03-29 03:10:59 +0000864ifcapable tempdb {
865 do_test pragma-7.3 {
866 db close
867 sqlite3 db test.db
868 execsql {
869 pragma lock_status;
870 }
871 } {main unlocked temp closed}
872} else {
873 do_test pragma-7.3 {
874 db close
875 sqlite3 db test.db
876 execsql {
877 pragma lock_status;
878 }
879 } {main unlocked}
880}
drh5260f7e2004-06-26 19:35:29 +0000881
882
danielk1977dae24952004-11-11 05:10:43 +0000883#----------------------------------------------------------------------
danielk1977b92b70b2004-11-12 16:11:59 +0000884# Test cases pragma-8.* test the "PRAGMA schema_version" and "PRAGMA
885# user_version" statements.
danielk1977dae24952004-11-11 05:10:43 +0000886#
danielk1977b92b70b2004-11-12 16:11:59 +0000887# pragma-8.1: PRAGMA schema_version
888# pragma-8.2: PRAGMA user_version
danielk1977dae24952004-11-11 05:10:43 +0000889#
890
danielk197711cf9fb2004-11-23 11:16:42 +0000891ifcapable schema_version {
892
danielk1977b92b70b2004-11-12 16:11:59 +0000893# First check that we can set the schema version and then retrieve the
danielk1977dae24952004-11-11 05:10:43 +0000894# same value.
895do_test pragma-8.1.1 {
896 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000897 PRAGMA schema_version = 105;
danielk1977dae24952004-11-11 05:10:43 +0000898 }
899} {}
900do_test pragma-8.1.2 {
drh25403652007-01-04 22:13:41 +0000901 execsql2 {
danielk1977b92b70b2004-11-12 16:11:59 +0000902 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000903 }
drh25403652007-01-04 22:13:41 +0000904} {schema_version 105}
danielk1977dae24952004-11-11 05:10:43 +0000905do_test pragma-8.1.3 {
906 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000907 PRAGMA schema_version = 106;
danielk1977dae24952004-11-11 05:10:43 +0000908 }
909} {}
910do_test pragma-8.1.4 {
911 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000912 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000913 }
914} 106
915
danielk1977b92b70b2004-11-12 16:11:59 +0000916# Check that creating a table modifies the schema-version (this is really
917# to verify that the value being read is in fact the schema version).
danielk1977dae24952004-11-11 05:10:43 +0000918do_test pragma-8.1.5 {
919 execsql {
920 CREATE TABLE t4(a, b, c);
921 INSERT INTO t4 VALUES(1, 2, 3);
922 SELECT * FROM t4;
923 }
924} {1 2 3}
925do_test pragma-8.1.6 {
926 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000927 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000928 }
929} 107
930
931# Now open a second connection to the database. Ensure that changing the
danielk1977b92b70b2004-11-12 16:11:59 +0000932# schema-version using the first connection forces the second connection
danielk1977dae24952004-11-11 05:10:43 +0000933# to reload the schema. This has to be done using the C-API test functions,
934# because the TCL API accounts for SCHEMA_ERROR and retries the query.
935do_test pragma-8.1.7 {
drhdddca282006-01-03 00:33:50 +0000936 sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
danielk1977dae24952004-11-11 05:10:43 +0000937 execsql {
938 SELECT * FROM t4;
939 } db2
940} {1 2 3}
941do_test pragma-8.1.8 {
942 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000943 PRAGMA schema_version = 108;
danielk1977dae24952004-11-11 05:10:43 +0000944 }
945} {}
946do_test pragma-8.1.9 {
947 set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM t4" -1 DUMMY]
948 sqlite3_step $::STMT
949} SQLITE_ERROR
950do_test pragma-8.1.10 {
951 sqlite3_finalize $::STMT
952} SQLITE_SCHEMA
953
danielk1977b92b70b2004-11-12 16:11:59 +0000954# Make sure the schema-version can be manipulated in an attached database.
mistachkinfda06be2011-08-02 00:57:34 +0000955forcedelete test2.db
956forcedelete test2.db-journal
danielk19775a8f9372007-10-09 08:29:32 +0000957ifcapable attach {
958 do_test pragma-8.1.11 {
959 execsql {
960 ATTACH 'test2.db' AS aux;
961 CREATE TABLE aux.t1(a, b, c);
962 PRAGMA aux.schema_version = 205;
963 }
964 } {}
965 do_test pragma-8.1.12 {
966 execsql {
967 PRAGMA aux.schema_version;
968 }
969 } 205
970}
danielk1977dae24952004-11-11 05:10:43 +0000971do_test pragma-8.1.13 {
972 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000973 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000974 }
975} 108
976
danielk1977b92b70b2004-11-12 16:11:59 +0000977# And check that modifying the schema-version in an attached database
danielk1977dae24952004-11-11 05:10:43 +0000978# forces the second connection to reload the schema.
danielk19775a8f9372007-10-09 08:29:32 +0000979ifcapable attach {
980 do_test pragma-8.1.14 {
981 sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
982 execsql {
983 ATTACH 'test2.db' AS aux;
984 SELECT * FROM aux.t1;
985 } db2
986 } {}
987 do_test pragma-8.1.15 {
988 execsql {
989 PRAGMA aux.schema_version = 206;
990 }
991 } {}
992 do_test pragma-8.1.16 {
993 set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM aux.t1" -1 DUMMY]
994 sqlite3_step $::STMT
995 } SQLITE_ERROR
996 do_test pragma-8.1.17 {
997 sqlite3_finalize $::STMT
998 } SQLITE_SCHEMA
999 do_test pragma-8.1.18 {
1000 db2 close
1001 } {}
1002}
danielk1977dae24952004-11-11 05:10:43 +00001003
danielk1977b92b70b2004-11-12 16:11:59 +00001004# Now test that the user-version can be read and written (and that we aren't
1005# accidentally manipulating the schema-version instead).
danielk1977dae24952004-11-11 05:10:43 +00001006do_test pragma-8.2.1 {
drh25403652007-01-04 22:13:41 +00001007 execsql2 {
danielk1977b92b70b2004-11-12 16:11:59 +00001008 PRAGMA user_version;
danielk1977dae24952004-11-11 05:10:43 +00001009 }
drh25403652007-01-04 22:13:41 +00001010} {user_version 0}
danielk1977dae24952004-11-11 05:10:43 +00001011do_test pragma-8.2.2 {
1012 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +00001013 PRAGMA user_version = 2;
danielk1977dae24952004-11-11 05:10:43 +00001014 }
1015} {}
drh802d69a2005-02-13 23:34:24 +00001016do_test pragma-8.2.3.1 {
drh25403652007-01-04 22:13:41 +00001017 execsql2 {
danielk1977b92b70b2004-11-12 16:11:59 +00001018 PRAGMA user_version;
danielk1977dae24952004-11-11 05:10:43 +00001019 }
drh25403652007-01-04 22:13:41 +00001020} {user_version 2}
drh802d69a2005-02-13 23:34:24 +00001021do_test pragma-8.2.3.2 {
1022 db close
1023 sqlite3 db test.db
1024 execsql {
1025 PRAGMA user_version;
1026 }
1027} {2}
1028do_test pragma-8.2.4.1 {
danielk1977dae24952004-11-11 05:10:43 +00001029 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +00001030 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +00001031 }
1032} {108}
drh802d69a2005-02-13 23:34:24 +00001033ifcapable vacuum {
1034 do_test pragma-8.2.4.2 {
1035 execsql {
1036 VACUUM;
1037 PRAGMA user_version;
1038 }
1039 } {2}
1040 do_test pragma-8.2.4.3 {
1041 execsql {
1042 PRAGMA schema_version;
1043 }
1044 } {109}
1045}
danielk1977dae24952004-11-11 05:10:43 +00001046
danielk19775a8f9372007-10-09 08:29:32 +00001047ifcapable attach {
1048 db eval {ATTACH 'test2.db' AS aux}
1049
1050 # Check that the user-version in the auxilary database can be manipulated (
1051 # and that we aren't accidentally manipulating the same in the main db).
1052 do_test pragma-8.2.5 {
1053 execsql {
1054 PRAGMA aux.user_version;
1055 }
1056 } {0}
1057 do_test pragma-8.2.6 {
1058 execsql {
1059 PRAGMA aux.user_version = 3;
1060 }
1061 } {}
1062 do_test pragma-8.2.7 {
1063 execsql {
1064 PRAGMA aux.user_version;
1065 }
1066 } {3}
1067 do_test pragma-8.2.8 {
1068 execsql {
1069 PRAGMA main.user_version;
1070 }
1071 } {2}
1072
1073 # Now check that a ROLLBACK resets the user-version if it has been modified
1074 # within a transaction.
1075 do_test pragma-8.2.9 {
1076 execsql {
1077 BEGIN;
1078 PRAGMA aux.user_version = 10;
1079 PRAGMA user_version = 11;
1080 }
1081 } {}
1082 do_test pragma-8.2.10 {
1083 execsql {
1084 PRAGMA aux.user_version;
1085 }
1086 } {10}
1087 do_test pragma-8.2.11 {
1088 execsql {
1089 PRAGMA main.user_version;
1090 }
1091 } {11}
1092 do_test pragma-8.2.12 {
1093 execsql {
1094 ROLLBACK;
1095 PRAGMA aux.user_version;
1096 }
1097 } {3}
1098 do_test pragma-8.2.13 {
1099 execsql {
1100 PRAGMA main.user_version;
1101 }
1102 } {2}
1103}
danielk1977dae24952004-11-11 05:10:43 +00001104
danielk1977b92b70b2004-11-12 16:11:59 +00001105# Try a negative value for the user-version
danielk1977dae24952004-11-11 05:10:43 +00001106do_test pragma-8.2.14 {
1107 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +00001108 PRAGMA user_version = -450;
danielk1977dae24952004-11-11 05:10:43 +00001109 }
1110} {}
1111do_test pragma-8.2.15 {
1112 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +00001113 PRAGMA user_version;
danielk1977dae24952004-11-11 05:10:43 +00001114 }
1115} {-450}
danielk1977d9c847d2005-01-07 10:42:48 +00001116} ; # ifcapable schema_version
1117
drhd19744f2008-03-18 13:46:53 +00001118# Check to see if TEMP_STORE is memory or disk. Return strings
1119# "memory" or "disk" as appropriate.
1120#
1121proc check_temp_store {} {
dan9131ab92016-04-06 18:20:51 +00001122 db eval {
1123 PRAGMA temp.cache_size = 1;
1124 CREATE TEMP TABLE IF NOT EXISTS a(b);
1125 DELETE FROM a;
1126 INSERT INTO a VALUES(randomblob(1000));
1127 INSERT INTO a SELECT * FROM a;
1128 INSERT INTO a SELECT * FROM a;
1129 INSERT INTO a SELECT * FROM a;
1130 INSERT INTO a SELECT * FROM a;
1131 INSERT INTO a SELECT * FROM a;
1132 INSERT INTO a SELECT * FROM a;
1133 INSERT INTO a SELECT * FROM a;
1134 INSERT INTO a SELECT * FROM a;
1135 }
drhd19744f2008-03-18 13:46:53 +00001136 db eval {PRAGMA database_list} {
1137 if {$name=="temp"} {
danielk197717b90b52008-06-06 11:11:25 +00001138 set bt [btree_from_db db 1]
1139 if {[btree_ismemdb $bt]} {
drhd19744f2008-03-18 13:46:53 +00001140 return "memory"
drhd19744f2008-03-18 13:46:53 +00001141 }
danielk197717b90b52008-06-06 11:11:25 +00001142 return "disk"
drhd19744f2008-03-18 13:46:53 +00001143 }
1144 }
1145 return "unknown"
1146}
1147
drh4ee09b42013-05-01 19:49:27 +00001148# Application_ID
1149#
1150do_test pragma-8.3.1 {
1151 execsql {
1152 PRAGMA application_id;
1153 }
1154} {0}
1155do_test pragma-8.3.2 {
1156 execsql {PRAGMA Application_ID(12345); PRAGMA application_id;}
1157} {12345}
tpoindex9a09a3c2004-12-20 19:01:32 +00001158
1159# Test temp_store and temp_store_directory pragmas
1160#
drh268283b2005-01-08 15:44:25 +00001161ifcapable pager_pragmas {
tpoindex9a09a3c2004-12-20 19:01:32 +00001162do_test pragma-9.1 {
1163 db close
1164 sqlite3 db test.db
1165 execsql {
1166 PRAGMA temp_store;
1167 }
1168} {0}
drhd19744f2008-03-18 13:46:53 +00001169if {$TEMP_STORE<=1} {
1170 do_test pragma-9.1.1 {
1171 check_temp_store
1172 } {disk}
1173} else {
1174 do_test pragma-9.1.1 {
1175 check_temp_store
1176 } {memory}
1177}
1178
tpoindex9a09a3c2004-12-20 19:01:32 +00001179do_test pragma-9.2 {
drhd19744f2008-03-18 13:46:53 +00001180 db close
1181 sqlite3 db test.db
tpoindex9a09a3c2004-12-20 19:01:32 +00001182 execsql {
1183 PRAGMA temp_store=file;
1184 PRAGMA temp_store;
1185 }
1186} {1}
drhd19744f2008-03-18 13:46:53 +00001187if {$TEMP_STORE==3} {
1188 # When TEMP_STORE is 3, always use memory regardless of pragma settings.
1189 do_test pragma-9.2.1 {
1190 check_temp_store
1191 } {memory}
1192} else {
1193 do_test pragma-9.2.1 {
1194 check_temp_store
1195 } {disk}
1196}
1197
tpoindex9a09a3c2004-12-20 19:01:32 +00001198do_test pragma-9.3 {
drhd19744f2008-03-18 13:46:53 +00001199 db close
1200 sqlite3 db test.db
tpoindex9a09a3c2004-12-20 19:01:32 +00001201 execsql {
1202 PRAGMA temp_store=memory;
1203 PRAGMA temp_store;
1204 }
1205} {2}
drhd19744f2008-03-18 13:46:53 +00001206if {$TEMP_STORE==0} {
1207 # When TEMP_STORE is 0, always use the disk regardless of pragma settings.
1208 do_test pragma-9.3.1 {
1209 check_temp_store
1210 } {disk}
1211} else {
1212 do_test pragma-9.3.1 {
1213 check_temp_store
1214 } {memory}
1215}
1216
tpoindex9a09a3c2004-12-20 19:01:32 +00001217do_test pragma-9.4 {
1218 execsql {
1219 PRAGMA temp_store_directory;
1220 }
1221} {}
drh78f82d12008-09-02 00:52:52 +00001222ifcapable wsd {
1223 do_test pragma-9.5 {
mistachkinf8a78462012-03-08 20:00:36 +00001224 set pwd [string map {' ''} [file nativename [get_pwd]]]
drh78f82d12008-09-02 00:52:52 +00001225 execsql "
1226 PRAGMA temp_store_directory='$pwd';
1227 "
1228 } {}
1229 do_test pragma-9.6 {
1230 execsql {
1231 PRAGMA temp_store_directory;
1232 }
mistachkinf8a78462012-03-08 20:00:36 +00001233 } [list [file nativename [get_pwd]]]
drh78f82d12008-09-02 00:52:52 +00001234 do_test pragma-9.7 {
1235 catchsql {
1236 PRAGMA temp_store_directory='/NON/EXISTENT/PATH/FOOBAR';
1237 }
1238 } {1 {not a writable directory}}
1239 do_test pragma-9.8 {
1240 execsql {
1241 PRAGMA temp_store_directory='';
1242 }
1243 } {}
1244 if {![info exists TEMP_STORE] || $TEMP_STORE<=1} {
1245 ifcapable tempdb {
1246 do_test pragma-9.9 {
1247 execsql {
1248 PRAGMA temp_store_directory;
1249 PRAGMA temp_store=FILE;
1250 CREATE TEMP TABLE temp_store_directory_test(a integer);
1251 INSERT INTO temp_store_directory_test values (2);
1252 SELECT * FROM temp_store_directory_test;
1253 }
1254 } {2}
1255 do_test pragma-9.10 {
1256 catchsql "
1257 PRAGMA temp_store_directory='$pwd';
1258 SELECT * FROM temp_store_directory_test;
1259 "
1260 } {1 {no such table: temp_store_directory_test}}
1261 }
tpoindex9a09a3c2004-12-20 19:01:32 +00001262 }
drh78f82d12008-09-02 00:52:52 +00001263}
danielk197795b289b2007-03-30 17:11:12 +00001264do_test pragma-9.11 {
1265 execsql {
1266 PRAGMA temp_store = 0;
1267 PRAGMA temp_store;
1268 }
1269} {0}
1270do_test pragma-9.12 {
1271 execsql {
1272 PRAGMA temp_store = 1;
1273 PRAGMA temp_store;
1274 }
1275} {1}
1276do_test pragma-9.13 {
1277 execsql {
1278 PRAGMA temp_store = 2;
1279 PRAGMA temp_store;
1280 }
1281} {2}
1282do_test pragma-9.14 {
1283 execsql {
1284 PRAGMA temp_store = 3;
1285 PRAGMA temp_store;
1286 }
1287} {0}
danielk197795b289b2007-03-30 17:11:12 +00001288do_test pragma-9.15 {
1289 catchsql {
1290 BEGIN EXCLUSIVE;
1291 CREATE TEMP TABLE temp_table(t);
1292 INSERT INTO temp_table VALUES('valuable data');
1293 PRAGMA temp_store = 1;
1294 }
1295} {1 {temporary storage cannot be changed from within a transaction}}
1296do_test pragma-9.16 {
1297 execsql {
1298 SELECT * FROM temp_table;
1299 COMMIT;
1300 }
1301} {{valuable data}}
danielk1977983e2302008-07-08 07:35:51 +00001302
1303do_test pragma-9.17 {
1304 execsql {
1305 INSERT INTO temp_table VALUES('valuable data II');
1306 SELECT * FROM temp_table;
1307 }
1308} {{valuable data} {valuable data II}}
1309
1310do_test pragma-9.18 {
1311 set rc [catch {
1312 db eval {SELECT t FROM temp_table} {
1313 execsql {pragma temp_store = 1}
1314 }
1315 } msg]
1316 list $rc $msg
1317} {1 {temporary storage cannot be changed from within a transaction}}
1318
drh268283b2005-01-08 15:44:25 +00001319} ;# ifcapable pager_pragmas
tpoindex9a09a3c2004-12-20 19:01:32 +00001320
danielk1977cc6bd382005-01-10 02:48:49 +00001321ifcapable trigger {
1322
1323do_test pragma-10.0 {
1324 catchsql {
1325 DROP TABLE main.t1;
1326 }
1327 execsql {
1328 PRAGMA count_changes = 1;
1329
1330 CREATE TABLE t1(a PRIMARY KEY);
1331 CREATE TABLE t1_mirror(a);
1332 CREATE TABLE t1_mirror2(a);
1333 CREATE TRIGGER t1_bi BEFORE INSERT ON t1 BEGIN
1334 INSERT INTO t1_mirror VALUES(new.a);
1335 END;
1336 CREATE TRIGGER t1_ai AFTER INSERT ON t1 BEGIN
1337 INSERT INTO t1_mirror2 VALUES(new.a);
1338 END;
1339 CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 BEGIN
1340 UPDATE t1_mirror SET a = new.a WHERE a = old.a;
1341 END;
1342 CREATE TRIGGER t1_au AFTER UPDATE ON t1 BEGIN
1343 UPDATE t1_mirror2 SET a = new.a WHERE a = old.a;
1344 END;
1345 CREATE TRIGGER t1_bd BEFORE DELETE ON t1 BEGIN
1346 DELETE FROM t1_mirror WHERE a = old.a;
1347 END;
1348 CREATE TRIGGER t1_ad AFTER DELETE ON t1 BEGIN
1349 DELETE FROM t1_mirror2 WHERE a = old.a;
1350 END;
1351 }
1352} {}
1353
1354do_test pragma-10.1 {
1355 execsql {
1356 INSERT INTO t1 VALUES(randstr(10,10));
1357 }
1358} {1}
1359do_test pragma-10.2 {
1360 execsql {
1361 UPDATE t1 SET a = randstr(10,10);
1362 }
1363} {1}
1364do_test pragma-10.3 {
1365 execsql {
1366 DELETE FROM t1;
1367 }
1368} {1}
1369
1370} ;# ifcapable trigger
1371
danielk197748af65a2005-02-09 03:20:37 +00001372ifcapable schema_pragmas {
1373 do_test pragma-11.1 {
1374 execsql2 {
1375 pragma collation_list;
1376 }
drh2d823312014-11-20 23:11:30 +00001377 } {seq 0 name RTRIM seq 1 name NOCASE seq 2 name BINARY}
danielk197748af65a2005-02-09 03:20:37 +00001378 do_test pragma-11.2 {
1379 db collate New_Collation blah...
1380 execsql {
1381 pragma collation_list;
1382 }
drh2d823312014-11-20 23:11:30 +00001383 } {0 New_Collation 1 RTRIM 2 NOCASE 3 BINARY}
danielk197748af65a2005-02-09 03:20:37 +00001384}
1385
danielk1977ddfb2f02006-02-17 12:25:14 +00001386ifcapable schema_pragmas&&tempdb {
1387 do_test pragma-12.1 {
1388 sqlite3 db2 test.db
1389 execsql {
1390 PRAGMA temp.table_info('abc');
1391 } db2
1392 } {}
1393 db2 close
1394
1395 do_test pragma-12.2 {
1396 sqlite3 db2 test.db
1397 execsql {
1398 PRAGMA temp.default_cache_size = 200;
1399 PRAGMA temp.default_cache_size;
1400 } db2
1401 } {200}
1402 db2 close
1403
1404 do_test pragma-12.3 {
1405 sqlite3 db2 test.db
1406 execsql {
1407 PRAGMA temp.cache_size = 400;
1408 PRAGMA temp.cache_size;
1409 } db2
1410 } {400}
1411 db2 close
1412}
1413
danielk19774b2688a2006-06-20 11:01:07 +00001414ifcapable bloblit {
1415
drh05a82982006-03-19 13:00:25 +00001416do_test pragma-13.1 {
1417 execsql {
1418 DROP TABLE IF EXISTS t4;
1419 PRAGMA vdbe_trace=on;
1420 PRAGMA vdbe_listing=on;
1421 PRAGMA sql_trace=on;
1422 CREATE TABLE t4(a INTEGER PRIMARY KEY,b);
1423 INSERT INTO t4(b) VALUES(x'0123456789abcdef0123456789abcdef0123456789');
1424 INSERT INTO t4(b) VALUES(randstr(30,30));
1425 INSERT INTO t4(b) VALUES(1.23456);
1426 INSERT INTO t4(b) VALUES(NULL);
1427 INSERT INTO t4(b) VALUES(0);
1428 INSERT INTO t4(b) SELECT b||b||b||b FROM t4;
1429 SELECT * FROM t4;
1430 }
1431 execsql {
1432 PRAGMA vdbe_trace=off;
1433 PRAGMA vdbe_listing=off;
1434 PRAGMA sql_trace=off;
1435 }
1436} {}
1437
danielk19774b2688a2006-06-20 11:01:07 +00001438} ;# ifcapable bloblit
1439
danielk197759a93792008-05-15 17:48:20 +00001440ifcapable pager_pragmas {
1441 db close
mistachkinfda06be2011-08-02 00:57:34 +00001442 forcedelete test.db
danielk197759a93792008-05-15 17:48:20 +00001443 sqlite3 db test.db
drh51a74d42015-02-28 01:04:27 +00001444
drhb3366b92015-09-11 20:54:44 +00001445 # EVIDENCE-OF: R-15672-33611 PRAGMA schema.page_count; Return the total
1446 # number of pages in the database file.
drh51a74d42015-02-28 01:04:27 +00001447 #
danielk197759a93792008-05-15 17:48:20 +00001448 do_test pragma-14.1 {
1449 execsql { pragma auto_vacuum = 0 }
drh51a74d42015-02-28 01:04:27 +00001450 execsql { pragma page_count; pragma main.page_count }
1451 } {0 0}
danielk197759a93792008-05-15 17:48:20 +00001452
1453 do_test pragma-14.2 {
1454 execsql {
1455 CREATE TABLE abc(a, b, c);
1456 PRAGMA page_count;
drh51a74d42015-02-28 01:04:27 +00001457 PRAGMA main.page_count;
1458 PRAGMA temp.page_count;
danielk197759a93792008-05-15 17:48:20 +00001459 }
drh51a74d42015-02-28 01:04:27 +00001460 } {2 2 0}
drh5d16a9a2011-10-13 14:41:22 +00001461 do_test pragma-14.2uc {
1462 execsql {pragma PAGE_COUNT}
1463 } {2}
danielk197759a93792008-05-15 17:48:20 +00001464
1465 do_test pragma-14.3 {
1466 execsql {
1467 BEGIN;
1468 CREATE TABLE def(a, b, c);
1469 PRAGMA page_count;
1470 }
1471 } {3}
drh5d16a9a2011-10-13 14:41:22 +00001472 do_test pragma-14.3uc {
1473 execsql {pragma PAGE_COUNT}
1474 } {3}
danielk197759a93792008-05-15 17:48:20 +00001475
1476 do_test pragma-14.4 {
1477 set page_size [db one {pragma page_size}]
1478 expr [file size test.db] / $page_size
1479 } {2}
1480
1481 do_test pragma-14.5 {
1482 execsql {
1483 ROLLBACK;
1484 PRAGMA page_count;
1485 }
1486 } {2}
1487
1488 do_test pragma-14.6 {
mistachkinfda06be2011-08-02 00:57:34 +00001489 forcedelete test2.db
danielk197759a93792008-05-15 17:48:20 +00001490 sqlite3 db2 test2.db
1491 execsql {
1492 PRAGMA auto_vacuum = 0;
1493 CREATE TABLE t1(a, b, c);
1494 CREATE TABLE t2(a, b, c);
1495 CREATE TABLE t3(a, b, c);
1496 CREATE TABLE t4(a, b, c);
1497 } db2
1498 db2 close
1499 execsql {
1500 ATTACH 'test2.db' AS aux;
1501 PRAGMA aux.page_count;
1502 }
1503 } {5}
drh5d16a9a2011-10-13 14:41:22 +00001504 do_test pragma-14.6uc {
1505 execsql {pragma AUX.PAGE_COUNT}
1506 } {5}
danielk197759a93792008-05-15 17:48:20 +00001507}
1508
danielk19778cf6c552008-06-23 16:53:46 +00001509# Test that the value set using the cache_size pragma is not reset when the
1510# schema is reloaded.
1511#
1512ifcapable pager_pragmas {
1513 db close
1514 sqlite3 db test.db
1515 do_test pragma-15.1 {
1516 execsql {
1517 PRAGMA cache_size=59;
1518 PRAGMA cache_size;
1519 }
1520 } {59}
1521 do_test pragma-15.2 {
1522 sqlite3 db2 test.db
1523 execsql {
1524 CREATE TABLE newtable(a, b, c);
1525 } db2
1526 db2 close
1527 } {}
1528 do_test pragma-15.3 {
1529 # Evaluating this statement will cause the schema to be reloaded (because
1530 # the schema was changed by another connection in pragma-15.2). At one
1531 # point there was a bug that reset the cache_size to its default value
1532 # when this happened.
1533 execsql { SELECT * FROM sqlite_master }
1534 execsql { PRAGMA cache_size }
1535 } {59}
1536}
1537
danielk19775558a8a2005-01-17 07:53:44 +00001538# Reset the sqlite3_temp_directory variable for the next run of tests:
1539sqlite3 dbX :memory:
1540dbX eval {PRAGMA temp_store_directory = ""}
1541dbX close
1542
danielk1977838cce42009-01-12 14:01:45 +00001543ifcapable lock_proxy_pragmas&&prefer_proxy_locking {
aswiftaebf4132008-11-21 00:10:35 +00001544 set sqlite_hostid_num 1
1545
1546 set using_proxy 0
1547 foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
1548 set using_proxy $value
1549 }
1550
1551 # Test the lock_proxy_file pragmas.
1552 #
1553 db close
1554 set env(SQLITE_FORCE_PROXY_LOCKING) "0"
1555
1556 sqlite3 db test.db
1557 do_test pragma-16.1 {
1558 execsql {
1559 PRAGMA lock_proxy_file="mylittleproxy";
1560 select * from sqlite_master;
1561 }
1562 execsql {
1563 PRAGMA lock_proxy_file;
1564 }
1565 } {mylittleproxy}
1566
1567 do_test pragma-16.2 {
1568 sqlite3 db2 test.db
1569 execsql {
1570 PRAGMA lock_proxy_file="mylittleproxy";
1571 } db2
1572 } {}
1573
1574 db2 close
1575 do_test pragma-16.2.1 {
1576 sqlite3 db2 test.db
1577 execsql {
1578 PRAGMA lock_proxy_file=":auto:";
1579 select * from sqlite_master;
1580 } db2
1581 execsql {
1582 PRAGMA lock_proxy_file;
1583 } db2
1584 } {mylittleproxy}
1585
1586 db2 close
1587 do_test pragma-16.3 {
1588 sqlite3 db2 test.db
1589 execsql {
1590 PRAGMA lock_proxy_file="myotherproxy";
1591 } db2
1592 catchsql {
1593 select * from sqlite_master;
1594 } db2
1595 } {1 {database is locked}}
1596
1597 do_test pragma-16.4 {
1598 db2 close
1599 db close
1600 sqlite3 db2 test.db
1601 execsql {
1602 PRAGMA lock_proxy_file="myoriginalproxy";
1603 PRAGMA lock_proxy_file="myotherproxy";
1604 PRAGMA lock_proxy_file;
1605 } db2
1606 } {myotherproxy}
1607
1608 db2 close
1609 set env(SQLITE_FORCE_PROXY_LOCKING) "1"
1610 do_test pragma-16.5 {
1611 sqlite3 db2 test.db
1612 execsql {
1613 PRAGMA lock_proxy_file=":auto:";
1614 PRAGMA lock_proxy_file;
1615 } db2
1616 } {myotherproxy}
1617
1618 do_test pragma-16.6 {
1619 db2 close
1620 sqlite3 db2 test2.db
1621 set lockpath [execsql {
1622 PRAGMA lock_proxy_file=":auto:";
1623 PRAGMA lock_proxy_file;
1624 } db2]
1625 string match "*test2.db:auto:" $lockpath
1626 } {1}
1627
1628 set sqlite_hostid_num 2
1629 do_test pragma-16.7 {
dan14d14602010-10-06 16:42:52 +00001630 list [catch {
1631 sqlite3 db test2.db
1632 execsql {
1633 PRAGMA lock_proxy_file=":auto:";
1634 select * from sqlite_master;
1635 }
1636 } msg] $msg
aswiftaebf4132008-11-21 00:10:35 +00001637 } {1 {database is locked}}
1638 db close
1639
1640 do_test pragma-16.8 {
dan14d14602010-10-06 16:42:52 +00001641 list [catch {
1642 sqlite3 db test2.db
1643 execsql { select * from sqlite_master }
1644 } msg] $msg
aswiftaebf4132008-11-21 00:10:35 +00001645 } {1 {database is locked}}
1646
1647 db2 close
1648 do_test pragma-16.8.1 {
1649 execsql {
1650 PRAGMA lock_proxy_file="yetanotherproxy";
1651 PRAGMA lock_proxy_file;
1652 }
1653 } {yetanotherproxy}
1654 do_test pragma-16.8.2 {
1655 execsql {
1656 create table mine(x);
1657 }
1658 } {}
1659
1660 db close
1661 do_test pragma-16.9 {
1662 sqlite3 db proxytest.db
1663 set lockpath2 [execsql {
1664 PRAGMA lock_proxy_file=":auto:";
1665 PRAGMA lock_proxy_file;
1666 } db]
1667 string match "*proxytest.db:auto:" $lockpath2
1668 } {1}
1669
1670 set env(SQLITE_FORCE_PROXY_LOCKING) $using_proxy
1671 set sqlite_hostid_num 0
1672}
drhd2cb50b2009-01-09 21:41:17 +00001673
1674# Parsing of auto_vacuum settings.
1675#
1676foreach {autovac_setting val} {
1677 0 0
1678 1 1
1679 2 2
1680 3 0
1681 -1 0
1682 none 0
1683 NONE 0
1684 NoNe 0
1685 full 1
1686 FULL 1
1687 incremental 2
1688 INCREMENTAL 2
1689 -1234 0
1690 1234 0
1691} {
1692 do_test pragma-17.1.$autovac_setting {
1693 catch {db close}
1694 sqlite3 db :memory:
1695 execsql "
1696 PRAGMA auto_vacuum=$::autovac_setting;
1697 PRAGMA auto_vacuum;
1698 "
1699 } $val
1700}
1701
1702# Parsing of temp_store settings.
1703#
1704foreach {temp_setting val} {
1705 0 0
1706 1 1
1707 2 2
1708 3 0
1709 -1 0
1710 file 1
1711 FILE 1
1712 fIlE 1
1713 memory 2
1714 MEMORY 2
1715 MeMoRy 2
1716} {
1717 do_test pragma-18.1.$temp_setting {
1718 catch {db close}
1719 sqlite3 db :memory:
1720 execsql "
1721 PRAGMA temp_store=$::temp_setting;
1722 PRAGMA temp_store=$::temp_setting;
1723 PRAGMA temp_store;
1724 "
1725 } $val
1726}
1727
drh92c700d2012-02-22 19:56:17 +00001728# The SQLITE_FCNTL_PRAGMA logic, with error handling.
1729#
1730db close
1731testvfs tvfs
1732sqlite3 db test.db -vfs tvfs
1733do_test pragma-19.1 {
1734 catchsql {PRAGMA error}
drha690ff32017-07-07 19:43:23 +00001735} {1 {SQL logic error}}
drh92c700d2012-02-22 19:56:17 +00001736do_test pragma-19.2 {
1737 catchsql {PRAGMA error='This is the error message'}
1738} {1 {This is the error message}}
1739do_test pragma-19.3 {
1740 catchsql {PRAGMA error='7 This is the error message'}
1741} {1 {This is the error message}}
1742do_test pragma-19.4 {
1743 catchsql {PRAGMA error=7}
1744} {1 {out of memory}}
drhc8517f62012-02-22 20:08:49 +00001745do_test pragma-19.5 {
mistachkin5b044542012-03-02 22:41:06 +00001746 file tail [lindex [execsql {PRAGMA filename}] 0]
drhc8517f62012-02-22 20:08:49 +00001747} {test.db}
drh92c700d2012-02-22 19:56:17 +00001748
drhcc716452012-06-06 23:23:23 +00001749if {$tcl_platform(platform)=="windows"} {
mistachkina112d142012-03-14 00:44:01 +00001750# Test data_store_directory pragma
1751#
1752db close
1753sqlite3 db test.db
1754file mkdir data_dir
1755do_test pragma-20.1 {
1756 catchsql {PRAGMA data_store_directory}
1757} {0 {}}
1758do_test pragma-20.2 {
1759 set pwd [string map {' ''} [file nativename [get_pwd]]]
1760 catchsql "PRAGMA data_store_directory='$pwd';"
1761} {0 {}}
1762do_test pragma-20.3 {
1763 catchsql {PRAGMA data_store_directory}
1764} [list 0 [list [file nativename [get_pwd]]]]
1765do_test pragma-20.4 {
1766 set pwd [string map {' ''} [file nativename \
1767 [file join [get_pwd] data_dir]]]
1768 catchsql "PRAGMA data_store_directory='$pwd';"
1769} {0 {}}
1770do_test pragma-20.5 {
1771 sqlite3 db2 test2.db
1772 catchsql "PRAGMA database_list;" db2
1773} [list 0 [list 0 main [file nativename \
1774 [file join [get_pwd] data_dir test2.db]]]]
1775catch {db2 close}
1776do_test pragma-20.6 {
1777 sqlite3 db2 [file join [get_pwd] test2.db]
1778 catchsql "PRAGMA database_list;" db2
1779} [list 0 [list 0 main [file nativename \
1780 [file join [get_pwd] test2.db]]]]
1781catch {db2 close}
1782do_test pragma-20.7 {
1783 catchsql "PRAGMA data_store_directory='';"
1784} {0 {}}
1785do_test pragma-20.8 {
1786 catchsql {PRAGMA data_store_directory}
1787} {0 {}}
drh92c700d2012-02-22 19:56:17 +00001788
mistachkina112d142012-03-14 00:44:01 +00001789forcedelete data_dir
drhcc716452012-06-06 23:23:23 +00001790} ;# endif windows
drhcd61c282002-03-06 22:01:34 +00001791
dan1fed5da2014-02-25 21:01:25 +00001792database_may_be_corrupt
drh7da56b42016-03-14 18:34:42 +00001793if {![nonzero_reserved_bytes]} {
dan1fed5da2014-02-25 21:01:25 +00001794
drh7da56b42016-03-14 18:34:42 +00001795 do_test 21.1 {
1796 # Create a corrupt database in testerr.db. And a non-corrupt at test.db.
1797 #
1798 db close
1799 forcedelete test.db
1800 sqlite3 db test.db
1801 execsql {
1802 PRAGMA page_size = 1024;
1803 PRAGMA auto_vacuum = 0;
1804 CREATE TABLE t1(a PRIMARY KEY, b);
1805 INSERT INTO t1 VALUES(1, 1);
1806 }
1807 for {set i 0} {$i < 10} {incr i} {
1808 execsql { INSERT INTO t1 SELECT a + (1 << $i), b + (1 << $i) FROM t1 }
1809 }
1810 db close
1811 forcecopy test.db testerr.db
1812 hexio_write testerr.db 15000 [string repeat 55 100]
1813 } {100}
1814
1815 set mainerr {*** in database main ***
dan5885e762012-07-16 10:06:12 +00001816Multiple uses for byte 672 of page 15}
drh7da56b42016-03-14 18:34:42 +00001817 set auxerr {*** in database aux ***
dan5885e762012-07-16 10:06:12 +00001818Multiple uses for byte 672 of page 15}
drh7da56b42016-03-14 18:34:42 +00001819
1820 set mainerr {/{\*\*\* in database main \*\*\*
dan597515d2014-02-28 18:39:51 +00001821Multiple uses for byte 672 of page 15}.*/}
drh7da56b42016-03-14 18:34:42 +00001822 set auxerr {/{\*\*\* in database aux \*\*\*
dan597515d2014-02-28 18:39:51 +00001823Multiple uses for byte 672 of page 15}.*/}
drh7da56b42016-03-14 18:34:42 +00001824
1825 do_test 22.2 {
1826 catch { db close }
1827 sqlite3 db testerr.db
1828 execsql { PRAGMA integrity_check }
1829 } $mainerr
1830
1831 do_test 22.3.1 {
1832 catch { db close }
1833 sqlite3 db test.db
1834 execsql {
1835 ATTACH 'testerr.db' AS 'aux';
1836 PRAGMA integrity_check;
1837 }
1838 } $auxerr
1839 do_test 22.3.2 {
1840 execsql { PRAGMA main.integrity_check; }
1841 } {ok}
1842 do_test 22.3.3 {
1843 execsql { PRAGMA aux.integrity_check; }
1844 } $auxerr
1845
1846 do_test 22.4.1 {
1847 catch { db close }
1848 sqlite3 db testerr.db
1849 execsql {
1850 ATTACH 'test.db' AS 'aux';
1851 PRAGMA integrity_check;
1852 }
1853 } $mainerr
1854 do_test 22.4.2 {
1855 execsql { PRAGMA main.integrity_check; }
1856 } $mainerr
1857 do_test 22.4.3 {
1858 execsql { PRAGMA aux.integrity_check; }
1859 } {ok}
1860}
1861
drhc95e01d2013-02-14 16:16:05 +00001862db close
1863forcedelete test.db test.db-wal test.db-journal
1864sqlite3 db test.db
1865sqlite3 db2 test.db
1866do_test 23.1 {
1867 db eval {
1868 CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c,d);
1869 CREATE INDEX i1 ON t1(b,c);
1870 CREATE INDEX i2 ON t1(c,d);
drh7be0fd92015-03-05 15:34:15 +00001871 CREATE INDEX i2x ON t1(d COLLATE nocase, c DESC);
drh5f419b32019-09-27 17:36:37 +00001872 CREATE INDEX i3 ON t1(d,b+c,c);
drhc95e01d2013-02-14 16:16:05 +00001873 CREATE TABLE t2(x INTEGER REFERENCES t1);
1874 }
1875 db2 eval {SELECT name FROM sqlite_master}
drh5f419b32019-09-27 17:36:37 +00001876} {t1 i1 i2 i2x i3 t2}
drhc228be52015-01-31 02:00:01 +00001877do_test 23.2a {
drhc95e01d2013-02-14 16:16:05 +00001878 db eval {
1879 DROP INDEX i2;
1880 CREATE INDEX i2 ON t1(c,d,b);
1881 }
drhc228be52015-01-31 02:00:01 +00001882 capture_pragma db2 out {PRAGMA index_info(i2)}
drh5e7028c2015-03-05 14:29:02 +00001883 db2 eval {SELECT cid, name, '|' FROM out ORDER BY seqno}
1884} {2 c | 3 d | 1 b |}
drh7be0fd92015-03-05 15:34:15 +00001885
drhb3366b92015-09-11 20:54:44 +00001886# EVIDENCE-OF: R-56143-29319 PRAGMA schema.index_xinfo(index-name); This
1887# pragma returns information about every column in an index.
drh7be0fd92015-03-05 15:34:15 +00001888#
1889# EVIDENCE-OF: R-45970-35618 Unlike this index_info pragma, this pragma
1890# returns information about every column in the index, not just the key
1891# columns.
1892#
drhc228be52015-01-31 02:00:01 +00001893do_test 23.2b {
1894 capture_pragma db2 out {PRAGMA index_xinfo(i2)}
1895 db2 eval {SELECT cid, name, "desc", coll, "key", '|' FROM out ORDER BY seqno}
1896} {2 c 0 BINARY 1 | 3 d 0 BINARY 1 | 1 b 0 BINARY 1 | -1 {} 0 BINARY 0 |}
drh7be0fd92015-03-05 15:34:15 +00001897
1898# (The first column of output from PRAGMA index_xinfo is...)
1899# EVIDENCE-OF: R-00197-14279 The rank of the column within the index. (0
1900# means left-most. Key columns come before auxiliary columns.)
1901#
1902# (The second column of output from PRAGMA index_xinfo is...)
drh5f419b32019-09-27 17:36:37 +00001903# EVIDENCE-OF: R-06603-49335 The rank of the column within the table
drh7be0fd92015-03-05 15:34:15 +00001904# being indexed, or -1 if the index-column is the rowid of the table
drh5f419b32019-09-27 17:36:37 +00001905# being indexed and -2 if the index is on an expression.
drh7be0fd92015-03-05 15:34:15 +00001906#
1907# (The third column of output from PRAGMA index_xinfo is...)
drh5f419b32019-09-27 17:36:37 +00001908# EVIDENCE-OF: R-40641-22898 The name of the column being indexed, or
1909# NULL if the index-column is the rowid of the table being indexed or an
1910# expression.
drh7be0fd92015-03-05 15:34:15 +00001911#
1912# (The fourth column of output from PRAGMA index_xinfo is...)
1913# EVIDENCE-OF: R-11847-09179 1 if the index-column is sorted in reverse
1914# (DESC) order by the index and 0 otherwise.
1915#
1916# (The fifth column of output from PRAGMA index_xinfo is...)
1917# EVIDENCE-OF: R-15313-19540 The name for the collating sequence used to
1918# compare values in the index-column.
1919#
1920# (The sixth column of output from PRAGMA index_xinfo is...)
1921# EVIDENCE-OF: R-14310-64553 1 if the index-column is a key column and 0
1922# if the index-column is an auxiliary column.
1923#
1924do_test 23.2c {
1925 db2 eval {PRAGMA index_xinfo(i2)}
1926} {0 2 c 0 BINARY 1 1 3 d 0 BINARY 1 2 1 b 0 BINARY 1 3 -1 {} 0 BINARY 0}
1927do_test 23.2d {
1928 db2 eval {PRAGMA index_xinfo(i2x)}
1929} {0 3 d 0 nocase 1 1 2 c 1 BINARY 1 2 -1 {} 0 BINARY 0}
drh5f419b32019-09-27 17:36:37 +00001930do_test 23.2e {
1931 db2 eval {PRAGMA index_xinfo(i3)}
1932} {0 3 d 0 BINARY 1 1 -2 {} 0 BINARY 1 2 2 c 0 BINARY 1 3 -1 {} 0 BINARY 0}
drh7be0fd92015-03-05 15:34:15 +00001933
drhb3366b92015-09-11 20:54:44 +00001934# EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This
1935# pragma returns one row for each index associated with the given table.
drh7be0fd92015-03-05 15:34:15 +00001936#
1937# (The first column of output from PRAGMA index_list is...)
1938# EVIDENCE-OF: R-02753-24748 A sequence number assigned to each index
1939# for internal tracking purposes.
1940#
1941# (The second column of output from PRAGMA index_list is...)
1942# EVIDENCE-OF: R-35496-03635 The name of the index.
1943#
1944# (The third column of output from PRAGMA index_list is...)
1945# EVIDENCE-OF: R-57301-64506 "1" if the index is UNIQUE and "0" if not.
1946#
1947# (The fourth column of output from PRAGMA index_list is...)
1948# EVIDENCE-OF: R-36609-39554 "c" if the index was created by a CREATE
1949# INDEX statement, "u" if the index was created by a UNIQUE constraint,
1950# or "pk" if the index was created by a PRIMARY KEY constraint.
1951#
drhc95e01d2013-02-14 16:16:05 +00001952do_test 23.3 {
1953 db eval {
drh5f419b32019-09-27 17:36:37 +00001954 DROP INDEX IF EXISTS i3;
drhc95e01d2013-02-14 16:16:05 +00001955 CREATE INDEX i3 ON t1(d,b,c);
1956 }
drhc228be52015-01-31 02:00:01 +00001957 capture_pragma db2 out {PRAGMA index_list(t1)}
drh7be0fd92015-03-05 15:34:15 +00001958 db2 eval {SELECT seq, name, "unique", origin, '|' FROM out ORDER BY seq}
1959} {0 i3 0 c | 1 i2 0 c | 2 i2x 0 c | 3 i1 0 c |}
drhc95e01d2013-02-14 16:16:05 +00001960do_test 23.4 {
1961 db eval {
1962 ALTER TABLE t1 ADD COLUMN e;
1963 }
1964 db2 eval {
1965 PRAGMA table_info(t1);
1966 }
1967} {/4 e {} 0 {} 0/}
1968do_test 23.5 {
1969 db eval {
1970 DROP TABLE t2;
1971 CREATE TABLE t2(x, y INTEGER REFERENCES t1);
1972 }
1973 db2 eval {
1974 PRAGMA foreign_key_list(t2);
1975 }
1976} {0 0 t1 y {} {NO ACTION} {NO ACTION} NONE}
drh3e1e14d2017-09-12 00:24:45 +00001977db2 close
drhc95e01d2013-02-14 16:16:05 +00001978
drhefeaec32017-10-23 16:34:07 +00001979ifcapable !has_codec {
1980 reset_db
1981 do_execsql_test 24.0 {
1982 PRAGMA page_size = 1024;
1983 CREATE TABLE t1(a, b, c);
1984 CREATE INDEX i1 ON t1(b);
1985 INSERT INTO t1 VALUES('a', 'b', 'c');
1986 PRAGMA integrity_check;
1987 } {ok}
1988
1989 set r [db one {SELECT rootpage FROM sqlite_master WHERE name = 't1'}]
1990 db close
1991 hexio_write test.db [expr $r*1024 - 16] 000000000000000701040f0f1f616263
1992
1993 sqlite3 db test.db
1994 do_catchsql_test 24.1 {
1995 SELECT * FROM t1;
1996 } {1 {database disk image is malformed}}
1997 do_catchsql_test 24.2 {
1998 PRAGMA integrity_check;
1999 } {0 {{database disk image is malformed}}}
2000}
dan1fed5da2014-02-25 21:01:25 +00002001database_never_corrupt
drhcd61c282002-03-06 22:01:34 +00002002finish_test