blob: 246a7cd8542d5c8ce2827e648aff19b2bc1b37da [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}
189do_test pragma-1.11 {
drh4303fee2003-02-15 23:09:17 +0000190 execsql {
191 PRAGMA synchronous=FULL;
192 PRAGMA cache_size;
193 PRAGMA default_cache_size;
194 PRAGMA synchronous;
drh4303fee2003-02-15 23:09:17 +0000195 }
danielk197791cf71b2004-06-26 06:37:06 +0000196} {123 123 2}
197do_test pragma-1.12 {
drh4303fee2003-02-15 23:09:17 +0000198 db close
drhdddca282006-01-03 00:33:50 +0000199 sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
drh4303fee2003-02-15 23:09:17 +0000200 execsql {
201 PRAGMA cache_size;
202 PRAGMA default_cache_size;
203 PRAGMA synchronous;
drh4303fee2003-02-15 23:09:17 +0000204 }
danielk197791cf71b2004-06-26 06:37:06 +0000205} {123 123 2}
206
drh5260f7e2004-06-26 19:35:29 +0000207# Make sure the pragma handler understands numeric values in addition
208# to keywords like "off" and "full".
209#
210do_test pragma-1.13 {
211 execsql {
212 PRAGMA synchronous=0;
213 PRAGMA synchronous;
214 }
215} {0}
216do_test pragma-1.14 {
217 execsql {
218 PRAGMA synchronous=2;
219 PRAGMA synchronous;
220 }
221} {2}
drh59ac6552015-04-16 16:04:39 +0000222do_test pragma-1.14.1 {
223 execsql {
224 PRAGMA synchronous=4;
225 PRAGMA synchronous;
226 }
227} {0}
228do_test pragma-1.14.2 {
229 execsql {
drhd99d2832015-04-17 15:58:33 +0000230 PRAGMA synchronous=3;
231 PRAGMA synchronous;
232 }
233} {0}
234do_test pragma-1.14.3 {
235 execsql {
drh59ac6552015-04-16 16:04:39 +0000236 PRAGMA synchronous=10;
237 PRAGMA synchronous;
238 }
239} {2}
danielk1977c7b4a442004-11-23 10:52:51 +0000240} ;# ifcapable pager_pragmas
drh5260f7e2004-06-26 19:35:29 +0000241
242# Test turning "flag" pragmas on and off.
243#
danielk19776338c762007-05-17 16:38:30 +0000244ifcapable debug {
245 # Pragma "vdbe_listing" is only available if compiled with SQLITE_DEBUG
246 #
247 do_test pragma-1.15 {
248 execsql {
249 PRAGMA vdbe_listing=YES;
250 PRAGMA vdbe_listing;
251 }
252 } {1}
253 do_test pragma-1.16 {
254 execsql {
255 PRAGMA vdbe_listing=NO;
256 PRAGMA vdbe_listing;
257 }
258 } {0}
259}
260
drh5260f7e2004-06-26 19:35:29 +0000261do_test pragma-1.17 {
262 execsql {
263 PRAGMA parser_trace=ON;
264 PRAGMA parser_trace=OFF;
265 }
266} {}
267do_test pragma-1.18 {
268 execsql {
269 PRAGMA bogus = -1234; -- Parsing of negative values
270 }
271} {}
272
danielk197791cf71b2004-06-26 06:37:06 +0000273# Test modifying the safety_level of an attached database.
danielk19775a8f9372007-10-09 08:29:32 +0000274ifcapable pager_pragmas&&attach {
275 do_test pragma-2.1 {
mistachkinfda06be2011-08-02 00:57:34 +0000276 forcedelete test2.db
277 forcedelete test2.db-journal
danielk19775a8f9372007-10-09 08:29:32 +0000278 execsql {
279 ATTACH 'test2.db' AS aux;
280 }
281 } {}
282 do_test pragma-2.2 {
283 execsql {
284 pragma aux.synchronous;
285 }
286 } {2}
287 do_test pragma-2.3 {
288 execsql {
289 pragma aux.synchronous = OFF;
290 pragma aux.synchronous;
291 pragma synchronous;
292 }
293 } {0 2}
294 do_test pragma-2.4 {
295 execsql {
296 pragma aux.synchronous = ON;
297 pragma synchronous;
298 pragma aux.synchronous;
299 }
300 } {2 1}
danielk1977c7b4a442004-11-23 10:52:51 +0000301} ;# ifcapable pager_pragmas
drh4303fee2003-02-15 23:09:17 +0000302
drhed717fe2003-06-15 23:42:24 +0000303# Construct a corrupted index and make sure the integrity_check
304# pragma finds it.
305#
drh25d65432004-07-22 15:02:25 +0000306# These tests won't work if the database is encrypted
307#
drhed717fe2003-06-15 23:42:24 +0000308do_test pragma-3.1 {
drh1e9daa62007-04-06 21:42:22 +0000309 db close
mistachkinfda06be2011-08-02 00:57:34 +0000310 forcedelete test.db test.db-journal
drh1e9daa62007-04-06 21:42:22 +0000311 sqlite3 db test.db
drhed717fe2003-06-15 23:42:24 +0000312 execsql {
drh1e9daa62007-04-06 21:42:22 +0000313 PRAGMA auto_vacuum=OFF;
drhed717fe2003-06-15 23:42:24 +0000314 BEGIN;
315 CREATE TABLE t2(a,b,c);
316 CREATE INDEX i2 ON t2(a);
317 INSERT INTO t2 VALUES(11,2,3);
318 INSERT INTO t2 VALUES(22,3,4);
319 COMMIT;
320 SELECT rowid, * from t2;
321 }
322} {1 11 2 3 2 22 3 4}
danielk19775a8f9372007-10-09 08:29:32 +0000323ifcapable attach {
324 if {![sqlite3 -has-codec] && $sqlite_options(integrityck)} {
325 do_test pragma-3.2 {
drhbb8a2792008-03-19 00:21:30 +0000326 db eval {SELECT rootpage FROM sqlite_master WHERE name='i2'} break
327 set pgsz [db eval {PRAGMA page_size}]
328 # overwrite the header on the rootpage of the index in order to
329 # make the index appear to be empty.
330 #
331 set offset [expr {$pgsz*($rootpage-1)}]
332 hexio_write test.db $offset 0a00000000040000000000
333 db close
334 sqlite3 db test.db
danielk19775a8f9372007-10-09 08:29:32 +0000335 execsql {PRAGMA integrity_check}
drh6fbe41a2013-10-30 20:22:55 +0000336 } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000337 do_test pragma-3.3 {
338 execsql {PRAGMA integrity_check=1}
drh6fbe41a2013-10-30 20:22:55 +0000339 } {{row 1 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000340 do_test pragma-3.4 {
341 execsql {
342 ATTACH DATABASE 'test.db' AS t2;
343 PRAGMA integrity_check
344 }
drh6fbe41a2013-10-30 20:22:55 +0000345 } {{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 +0000346 do_test pragma-3.5 {
347 execsql {
drhbb8a2792008-03-19 00:21:30 +0000348 PRAGMA integrity_check=4
danielk19775a8f9372007-10-09 08:29:32 +0000349 }
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} {row 1 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000351 do_test pragma-3.6 {
352 execsql {
353 PRAGMA integrity_check=xyz
354 }
drh6fbe41a2013-10-30 20:22:55 +0000355 } {{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 +0000356 do_test pragma-3.7 {
357 execsql {
358 PRAGMA integrity_check=0
359 }
drh6fbe41a2013-10-30 20:22:55 +0000360 } {{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 +0000361
362 # Add additional corruption by appending unused pages to the end of
363 # the database file testerr.db
364 #
365 do_test pragma-3.8 {
366 execsql {DETACH t2}
mistachkinfda06be2011-08-02 00:57:34 +0000367 forcedelete testerr.db testerr.db-journal
danielk19775a8f9372007-10-09 08:29:32 +0000368 set out [open testerr.db w]
369 fconfigure $out -translation binary
370 set in [open test.db r]
371 fconfigure $in -translation binary
372 puts -nonewline $out [read $in]
373 seek $in 0
374 puts -nonewline $out [read $in]
375 close $in
376 close $out
drhdd3cd972010-03-27 17:12:36 +0000377 hexio_write testerr.db 28 00000000
danielk19775a8f9372007-10-09 08:29:32 +0000378 execsql {REINDEX t2}
379 execsql {PRAGMA integrity_check}
380 } {ok}
danielk197741c58b72007-12-29 13:39:19 +0000381 do_test pragma-3.8.1 {
382 execsql {PRAGMA quick_check}
383 } {ok}
drh5d16a9a2011-10-13 14:41:22 +0000384 do_test pragma-3.8.2 {
385 execsql {PRAGMA QUICK_CHECK}
386 } {ok}
danielk19775a8f9372007-10-09 08:29:32 +0000387 do_test pragma-3.9 {
388 execsql {
389 ATTACH 'testerr.db' AS t2;
390 PRAGMA integrity_check
391 }
392 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000393Page 4 is never used
394Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000395Page 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 +0000396 do_test pragma-3.10 {
397 execsql {
398 PRAGMA integrity_check=1
399 }
400 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000401Page 4 is never used}}
danielk19775a8f9372007-10-09 08:29:32 +0000402 do_test pragma-3.11 {
403 execsql {
404 PRAGMA integrity_check=5
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}}
danielk19775a8f9372007-10-09 08:29:32 +0000410 do_test pragma-3.12 {
411 execsql {
412 PRAGMA integrity_check=4
413 }
414 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000415Page 4 is never used
416Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000417Page 6 is never used} {row 1 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000418 do_test pragma-3.13 {
419 execsql {
420 PRAGMA integrity_check=3
421 }
422 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000423Page 4 is never used
424Page 5 is never used
425Page 6 is never used}}
danielk19775a8f9372007-10-09 08:29:32 +0000426 do_test pragma-3.14 {
427 execsql {
428 PRAGMA integrity_check(2)
429 }
430 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000431Page 4 is never used
432Page 5 is never used}}
danielk19775a8f9372007-10-09 08:29:32 +0000433 do_test pragma-3.15 {
434 execsql {
435 ATTACH 'testerr.db' AS t3;
436 PRAGMA integrity_check
437 }
438 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000439Page 4 is never used
440Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000441Page 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 +0000442Page 4 is never used
443Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000444Page 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 +0000445 do_test pragma-3.16 {
446 execsql {
drhbb8a2792008-03-19 00:21:30 +0000447 PRAGMA integrity_check(10)
danielk19775a8f9372007-10-09 08:29:32 +0000448 }
449 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000450Page 4 is never used
451Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000452Page 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 +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}}
danielk19775a8f9372007-10-09 08:29:32 +0000456 do_test pragma-3.17 {
457 execsql {
drhbb8a2792008-03-19 00:21:30 +0000458 PRAGMA integrity_check=8
danielk19775a8f9372007-10-09 08:29:32 +0000459 }
460 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000461Page 4 is never used
462Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000463Page 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 +0000464Page 4 is never used
465Page 5 is never used}}
danielk19775a8f9372007-10-09 08:29:32 +0000466 do_test pragma-3.18 {
467 execsql {
468 PRAGMA integrity_check=4
469 }
470 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000471Page 4 is never used
472Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000473Page 6 is never used} {row 1 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000474 }
drhd2cb50b2009-01-09 21:41:17 +0000475 do_test pragma-3.19 {
476 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +0000477 forcedelete test.db test.db-journal
drhd2cb50b2009-01-09 21:41:17 +0000478 sqlite3 db test.db
479 db eval {PRAGMA integrity_check}
480 } {ok}
drh25d65432004-07-22 15:02:25 +0000481}
drhcefc87f2014-08-01 01:40:33 +0000482
483# Verify that PRAGMA integrity_check catches UNIQUE and NOT NULL
484# constraint violations.
485#
486do_execsql_test pragma-3.20 {
487 CREATE TABLE t1(a,b);
488 CREATE INDEX t1a ON t1(a);
489 INSERT INTO t1 VALUES(1,1),(2,2),(3,3),(2,4),(NULL,5),(NULL,6);
490 PRAGMA writable_schema=ON;
491 UPDATE sqlite_master SET sql='CREATE UNIQUE INDEX t1a ON t1(a)'
492 WHERE name='t1a';
493 UPDATE sqlite_master SET sql='CREATE TABLE t1(a NOT NULL,b)'
494 WHERE name='t1';
495 PRAGMA writable_schema=OFF;
496 ALTER TABLE t1 RENAME TO t1x;
497 PRAGMA integrity_check;
498} {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a} {NULL value in t1x.a}}
499do_execsql_test pragma-3.21 {
500 PRAGMA integrity_check(3);
501} {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a}}
502do_execsql_test pragma-3.22 {
503 PRAGMA integrity_check(2);
504} {{non-unique entry in index t1a} {NULL value in t1x.a}}
drh7efa4262014-12-16 00:08:31 +0000505do_execsql_test pragma-3.23 {
drhcefc87f2014-08-01 01:40:33 +0000506 PRAGMA integrity_check(1);
507} {{non-unique entry in index t1a}}
danielk197791cf71b2004-06-26 06:37:06 +0000508
drh7efa4262014-12-16 00:08:31 +0000509# PRAGMA integrity check (or more specifically the sqlite3BtreeCount()
510# interface) used to leave index cursors in an inconsistent state
511# which could result in an assertion fault in sqlite3BtreeKey()
512# called from saveCursorPosition() if content is removed from the
513# index while the integrity_check is still running. This test verifies
514# that problem has been fixed.
515#
516do_test pragma-3.30 {
517 db close
518 delete_file test.db
519 sqlite3 db test.db
520 db eval {
521 CREATE TABLE t1(a,b,c);
522 WITH RECURSIVE
523 c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<100)
524 INSERT INTO t1(a,b,c) SELECT i, printf('xyz%08x',i), 2000-i FROM c;
525 CREATE INDEX t1a ON t1(a);
526 CREATE INDEX t1bc ON t1(b,c);
527 }
528 db eval {PRAGMA integrity_check} {
529 db eval {DELETE FROM t1}
530 }
531} {}
532
danielk197791cf71b2004-06-26 06:37:06 +0000533# Test modifying the cache_size of an attached database.
danielk19775a8f9372007-10-09 08:29:32 +0000534ifcapable pager_pragmas&&attach {
danielk197791cf71b2004-06-26 06:37:06 +0000535do_test pragma-4.1 {
536 execsql {
drh1e9daa62007-04-06 21:42:22 +0000537 ATTACH 'test2.db' AS aux;
danielk197791cf71b2004-06-26 06:37:06 +0000538 pragma aux.cache_size;
539 pragma aux.default_cache_size;
540 }
drh1e9daa62007-04-06 21:42:22 +0000541} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000542do_test pragma-4.2 {
543 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000544 pragma aux.cache_size = 50;
545 pragma aux.cache_size;
546 pragma aux.default_cache_size;
547 }
drh1e9daa62007-04-06 21:42:22 +0000548} [list 50 $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000549do_test pragma-4.3 {
550 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000551 pragma aux.default_cache_size = 456;
552 pragma aux.cache_size;
553 pragma aux.default_cache_size;
554 }
555} {456 456}
drh1bdd9b52004-04-23 17:04:44 +0000556do_test pragma-4.4 {
557 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000558 pragma cache_size;
559 pragma default_cache_size;
560 }
drh1e9daa62007-04-06 21:42:22 +0000561} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000562do_test pragma-4.5 {
563 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000564 DETACH aux;
565 ATTACH 'test3.db' AS aux;
566 pragma aux.cache_size;
567 pragma aux.default_cache_size;
568 }
drh1e9daa62007-04-06 21:42:22 +0000569} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000570do_test pragma-4.6 {
571 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000572 DETACH aux;
573 ATTACH 'test2.db' AS aux;
574 pragma aux.cache_size;
575 pragma aux.default_cache_size;
576 }
577} {456 456}
danielk1977c7b4a442004-11-23 10:52:51 +0000578} ;# ifcapable pager_pragmas
drh1bdd9b52004-04-23 17:04:44 +0000579
danielk197791cf71b2004-06-26 06:37:06 +0000580# Test that modifying the sync-level in the middle of a transaction is
581# disallowed.
danielk1977c7b4a442004-11-23 10:52:51 +0000582ifcapable pager_pragmas {
danielk197791cf71b2004-06-26 06:37:06 +0000583do_test pragma-5.0 {
drh1bdd9b52004-04-23 17:04:44 +0000584 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000585 pragma synchronous;
586 }
587} {2}
588do_test pragma-5.1 {
drh1bdd9b52004-04-23 17:04:44 +0000589 catchsql {
590 BEGIN;
danielk197791cf71b2004-06-26 06:37:06 +0000591 pragma synchronous = OFF;
592 }
593} {1 {Safety level may not be changed inside a transaction}}
594do_test pragma-5.2 {
595 execsql {
596 pragma synchronous;
597 }
598} {2}
drh5260f7e2004-06-26 19:35:29 +0000599catchsql {COMMIT;}
danielk1977c7b4a442004-11-23 10:52:51 +0000600} ;# ifcapable pager_pragmas
drh5260f7e2004-06-26 19:35:29 +0000601
602# Test schema-query pragmas
603#
danielk197727188fb2004-11-23 10:13:03 +0000604ifcapable schema_pragmas {
danielk19775a8f9372007-10-09 08:29:32 +0000605ifcapable tempdb&&attach {
danielk197753c0f742005-03-29 03:10:59 +0000606 do_test pragma-6.1 {
607 set res {}
608 execsql {SELECT * FROM sqlite_temp_master}
609 foreach {idx name file} [execsql {pragma database_list}] {
610 lappend res $idx $name
611 }
612 set res
613 } {0 main 1 temp 2 aux}
614}
drh5260f7e2004-06-26 19:35:29 +0000615do_test pragma-6.2 {
616 execsql {
drhd2cb50b2009-01-09 21:41:17 +0000617 CREATE TABLE t2(a,b,c);
drh5260f7e2004-06-26 19:35:29 +0000618 pragma table_info(t2)
619 }
drhbfa8b102006-03-03 21:20:16 +0000620} {0 a {} 0 {} 0 1 b {} 0 {} 0 2 c {} 0 {} 0}
drhd2cb50b2009-01-09 21:41:17 +0000621do_test pragma-6.2.1 {
622 execsql {
623 pragma table_info;
624 }
625} {}
drh736c7d42006-11-30 13:06:37 +0000626db nullvalue <<NULL>>
drh417ec632006-08-14 14:23:41 +0000627do_test pragma-6.2.2 {
628 execsql {
drh736c7d42006-11-30 13:06:37 +0000629 CREATE TABLE t5(
630 a TEXT DEFAULT CURRENT_TIMESTAMP,
631 b DEFAULT (5+3),
632 c TEXT,
633 d INTEGER DEFAULT NULL,
drh384b7fe2013-01-01 13:55:31 +0000634 e TEXT DEFAULT '',
635 UNIQUE(b,c,d),
636 PRIMARY KEY(e,b,c)
drh736c7d42006-11-30 13:06:37 +0000637 );
drh417ec632006-08-14 14:23:41 +0000638 PRAGMA table_info(t5);
639 }
drh384b7fe2013-01-01 13:55:31 +0000640} {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 +0000641db nullvalue {}
drh384b7fe2013-01-01 13:55:31 +0000642do_test pragma-6.2.3 {
643 execsql {
644 CREATE TABLE t2_3(a,b INTEGER PRIMARY KEY,c);
645 pragma table_info(t2_3)
646 }
647} {0 a {} 0 {} 0 1 b INTEGER 0 {} 1 2 c {} 0 {} 0}
drh6bf89572004-11-03 16:27:01 +0000648ifcapable {foreignkey} {
drhd2cb50b2009-01-09 21:41:17 +0000649 do_test pragma-6.3.1 {
drh6bf89572004-11-03 16:27:01 +0000650 execsql {
651 CREATE TABLE t3(a int references t2(b), b UNIQUE);
652 pragma foreign_key_list(t3);
653 }
dan1da40a32009-09-19 17:00:31 +0000654 } {0 0 t2 a b {NO ACTION} {NO ACTION} NONE}
drhd2cb50b2009-01-09 21:41:17 +0000655 do_test pragma-6.3.2 {
656 execsql {
657 pragma foreign_key_list;
658 }
659 } {}
660 do_test pragma-6.3.3 {
661 execsql {
662 pragma foreign_key_list(t3_bogus);
663 }
664 } {}
665 do_test pragma-6.3.4 {
666 execsql {
667 pragma foreign_key_list(t5);
668 }
669 } {}
drh6bf89572004-11-03 16:27:01 +0000670 do_test pragma-6.4 {
drhc228be52015-01-31 02:00:01 +0000671 capture_pragma db out {
drh6bf89572004-11-03 16:27:01 +0000672 pragma index_list(t3);
673 }
drhc228be52015-01-31 02:00:01 +0000674 db eval {SELECT seq, "name", "unique" FROM out ORDER BY seq}
drh3ef26152013-10-12 20:22:00 +0000675 } {0 sqlite_autoindex_t3_1 1}
drh6bf89572004-11-03 16:27:01 +0000676}
677ifcapable {!foreignkey} {
678 execsql {CREATE TABLE t3(a,b UNIQUE)}
679}
drhd2cb50b2009-01-09 21:41:17 +0000680do_test pragma-6.5.1 {
drh5260f7e2004-06-26 19:35:29 +0000681 execsql {
682 CREATE INDEX t3i1 ON t3(a,b);
drhc228be52015-01-31 02:00:01 +0000683 }
684 capture_pragma db out {
drh5260f7e2004-06-26 19:35:29 +0000685 pragma index_info(t3i1);
686 }
drhc228be52015-01-31 02:00:01 +0000687 db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
drh5260f7e2004-06-26 19:35:29 +0000688} {0 0 a 1 1 b}
drh7be0fd92015-03-05 15:34:15 +0000689
690# EVIDENCE-OF: R-23114-21695 The auxiliary index-columns are not shown
691# by the index_info pragma, but they are listed by the index_xinfo
692# pragma.
693#
694do_test pragma-6.5.1b {
695 capture_pragma db out {PRAGMA index_xinfo(t3i1)}
696 db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
697} {0 0 a 1 1 b 2 -1 {}}
698
699
drhb3366b92015-09-11 20:54:44 +0000700# EVIDENCE-OF: R-29448-60346 PRAGMA schema.index_info(index-name); This
701# pragma returns one row for each key column in the named index.
drh7be0fd92015-03-05 15:34:15 +0000702#
703# (The first column of output from PRAGMA index_info is...)
704# EVIDENCE-OF: R-34186-52914 The rank of the column within the index. (0
705# means left-most.)
706#
707# (The second column of output from PRAGMA index_info is...)
708# EVIDENCE-OF: R-65019-08383 The rank of the column within the table
709# being indexed.
710#
711# (The third column of output from PRAGMA index_info is...)
712# EVIDENCE-OF: R-09773-34266 The name of the column being indexed.
713#
714do_execsql_test pragma-6.5.1c {
715 CREATE INDEX t3i2 ON t3(b,a);
716 PRAGMA index_info='t3i2';
717 DROP INDEX t3i2;
718} {0 1 b 1 0 a}
719
drhd2cb50b2009-01-09 21:41:17 +0000720do_test pragma-6.5.2 {
721 execsql {
722 pragma index_info(t3i1_bogus);
723 }
724} {}
danielk1977260d8a62008-08-20 16:34:24 +0000725
726ifcapable tempdb {
727 # Test for ticket #3320. When a temp table of the same name exists, make
728 # sure the schema of the main table can still be queried using
729 # "pragma table_info":
730 do_test pragma-6.6.1 {
731 execsql {
732 CREATE TABLE trial(col_main);
733 CREATE TEMP TABLE trial(col_temp);
734 }
735 } {}
736 do_test pragma-6.6.2 {
737 execsql {
738 PRAGMA table_info(trial);
739 }
740 } {0 col_temp {} 0 {} 0}
741 do_test pragma-6.6.3 {
742 execsql {
743 PRAGMA temp.table_info(trial);
744 }
745 } {0 col_temp {} 0 {} 0}
746 do_test pragma-6.6.4 {
747 execsql {
748 PRAGMA main.table_info(trial);
749 }
750 } {0 col_main {} 0 {} 0}
751}
danielk1977f96a3772008-10-23 05:45:07 +0000752
danielk1977f96a3772008-10-23 05:45:07 +0000753do_test pragma-6.7 {
754 execsql {
755 CREATE TABLE test_table(
756 one INT NOT NULL DEFAULT -1,
757 two text,
758 three VARCHAR(45, 65) DEFAULT 'abcde',
759 four REAL DEFAULT X'abcdef',
760 five DEFAULT CURRENT_TIME
761 );
danielk1977f96a3772008-10-23 05:45:07 +0000762 }
drhc228be52015-01-31 02:00:01 +0000763 capture_pragma db out {PRAGMA table_info(test_table)}
764 db eval {SELECT cid, "name", type, "notnull", dflt_value, pk FROM out
765 ORDER BY cid}
danielk1977f96a3772008-10-23 05:45:07 +0000766} [concat \
767 {0 one INT 1 -1 0} \
768 {1 two text 0 {} 0} \
769 {2 three {VARCHAR(45, 65)} 0 'abcde' 0} \
770 {3 four REAL 0 X'abcdef' 0} \
771 {4 five {} 0 CURRENT_TIME 0} \
772]
drh1b678962015-04-15 07:19:27 +0000773do_test pragma-6.8 {
774 execsql {
775 CREATE TABLE t68(a,b,c,PRIMARY KEY(a,b,a,c));
776 PRAGMA table_info(t68);
777 }
778} [concat \
779 {0 a {} 0 {} 1} \
780 {1 b {} 0 {} 2} \
781 {2 c {} 0 {} 4} \
782]
danielk197727188fb2004-11-23 10:13:03 +0000783} ;# ifcapable schema_pragmas
drh5260f7e2004-06-26 19:35:29 +0000784# Miscellaneous tests
785#
danielk197727188fb2004-11-23 10:13:03 +0000786ifcapable schema_pragmas {
drhb3366b92015-09-11 20:54:44 +0000787# EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This
788# pragma returns one row for each index associated with the given table.
789#
drhd2cb50b2009-01-09 21:41:17 +0000790do_test pragma-7.1.1 {
drh5260f7e2004-06-26 19:35:29 +0000791 # Make sure a pragma knows to read the schema if it needs to
792 db close
793 sqlite3 db test.db
drhc228be52015-01-31 02:00:01 +0000794 capture_pragma db out "PRAGMA index_list(t3)"
795 db eval {SELECT name, "origin" FROM out ORDER BY name DESC}
796} {t3i1 c sqlite_autoindex_t3_1 u}
drhd2cb50b2009-01-09 21:41:17 +0000797do_test pragma-7.1.2 {
798 execsql {
799 pragma index_list(t3_bogus);
800 }
801} {}
danielk197727188fb2004-11-23 10:13:03 +0000802} ;# ifcapable schema_pragmas
drh6c626082004-11-14 21:56:29 +0000803ifcapable {utf16} {
dancb354602010-07-08 09:44:42 +0000804 if {[permutation] == ""} {
805 do_test pragma-7.2 {
806 db close
807 sqlite3 db test.db
808 catchsql {
809 pragma encoding=bogus;
810 }
811 } {1 {unsupported encoding: bogus}}
812 }
drh6c626082004-11-14 21:56:29 +0000813}
danielk197753c0f742005-03-29 03:10:59 +0000814ifcapable tempdb {
815 do_test pragma-7.3 {
816 db close
817 sqlite3 db test.db
818 execsql {
819 pragma lock_status;
820 }
821 } {main unlocked temp closed}
822} else {
823 do_test pragma-7.3 {
824 db close
825 sqlite3 db test.db
826 execsql {
827 pragma lock_status;
828 }
829 } {main unlocked}
830}
drh5260f7e2004-06-26 19:35:29 +0000831
832
danielk1977dae24952004-11-11 05:10:43 +0000833#----------------------------------------------------------------------
danielk1977b92b70b2004-11-12 16:11:59 +0000834# Test cases pragma-8.* test the "PRAGMA schema_version" and "PRAGMA
835# user_version" statements.
danielk1977dae24952004-11-11 05:10:43 +0000836#
danielk1977b92b70b2004-11-12 16:11:59 +0000837# pragma-8.1: PRAGMA schema_version
838# pragma-8.2: PRAGMA user_version
danielk1977dae24952004-11-11 05:10:43 +0000839#
840
danielk197711cf9fb2004-11-23 11:16:42 +0000841ifcapable schema_version {
842
danielk1977b92b70b2004-11-12 16:11:59 +0000843# First check that we can set the schema version and then retrieve the
danielk1977dae24952004-11-11 05:10:43 +0000844# same value.
845do_test pragma-8.1.1 {
846 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000847 PRAGMA schema_version = 105;
danielk1977dae24952004-11-11 05:10:43 +0000848 }
849} {}
850do_test pragma-8.1.2 {
drh25403652007-01-04 22:13:41 +0000851 execsql2 {
danielk1977b92b70b2004-11-12 16:11:59 +0000852 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000853 }
drh25403652007-01-04 22:13:41 +0000854} {schema_version 105}
danielk1977dae24952004-11-11 05:10:43 +0000855do_test pragma-8.1.3 {
856 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000857 PRAGMA schema_version = 106;
danielk1977dae24952004-11-11 05:10:43 +0000858 }
859} {}
860do_test pragma-8.1.4 {
861 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000862 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000863 }
864} 106
865
danielk1977b92b70b2004-11-12 16:11:59 +0000866# Check that creating a table modifies the schema-version (this is really
867# to verify that the value being read is in fact the schema version).
danielk1977dae24952004-11-11 05:10:43 +0000868do_test pragma-8.1.5 {
869 execsql {
870 CREATE TABLE t4(a, b, c);
871 INSERT INTO t4 VALUES(1, 2, 3);
872 SELECT * FROM t4;
873 }
874} {1 2 3}
875do_test pragma-8.1.6 {
876 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000877 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000878 }
879} 107
880
881# Now open a second connection to the database. Ensure that changing the
danielk1977b92b70b2004-11-12 16:11:59 +0000882# schema-version using the first connection forces the second connection
danielk1977dae24952004-11-11 05:10:43 +0000883# to reload the schema. This has to be done using the C-API test functions,
884# because the TCL API accounts for SCHEMA_ERROR and retries the query.
885do_test pragma-8.1.7 {
drhdddca282006-01-03 00:33:50 +0000886 sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
danielk1977dae24952004-11-11 05:10:43 +0000887 execsql {
888 SELECT * FROM t4;
889 } db2
890} {1 2 3}
891do_test pragma-8.1.8 {
892 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000893 PRAGMA schema_version = 108;
danielk1977dae24952004-11-11 05:10:43 +0000894 }
895} {}
896do_test pragma-8.1.9 {
897 set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM t4" -1 DUMMY]
898 sqlite3_step $::STMT
899} SQLITE_ERROR
900do_test pragma-8.1.10 {
901 sqlite3_finalize $::STMT
902} SQLITE_SCHEMA
903
danielk1977b92b70b2004-11-12 16:11:59 +0000904# Make sure the schema-version can be manipulated in an attached database.
mistachkinfda06be2011-08-02 00:57:34 +0000905forcedelete test2.db
906forcedelete test2.db-journal
danielk19775a8f9372007-10-09 08:29:32 +0000907ifcapable attach {
908 do_test pragma-8.1.11 {
909 execsql {
910 ATTACH 'test2.db' AS aux;
911 CREATE TABLE aux.t1(a, b, c);
912 PRAGMA aux.schema_version = 205;
913 }
914 } {}
915 do_test pragma-8.1.12 {
916 execsql {
917 PRAGMA aux.schema_version;
918 }
919 } 205
920}
danielk1977dae24952004-11-11 05:10:43 +0000921do_test pragma-8.1.13 {
922 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000923 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000924 }
925} 108
926
danielk1977b92b70b2004-11-12 16:11:59 +0000927# And check that modifying the schema-version in an attached database
danielk1977dae24952004-11-11 05:10:43 +0000928# forces the second connection to reload the schema.
danielk19775a8f9372007-10-09 08:29:32 +0000929ifcapable attach {
930 do_test pragma-8.1.14 {
931 sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
932 execsql {
933 ATTACH 'test2.db' AS aux;
934 SELECT * FROM aux.t1;
935 } db2
936 } {}
937 do_test pragma-8.1.15 {
938 execsql {
939 PRAGMA aux.schema_version = 206;
940 }
941 } {}
942 do_test pragma-8.1.16 {
943 set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM aux.t1" -1 DUMMY]
944 sqlite3_step $::STMT
945 } SQLITE_ERROR
946 do_test pragma-8.1.17 {
947 sqlite3_finalize $::STMT
948 } SQLITE_SCHEMA
949 do_test pragma-8.1.18 {
950 db2 close
951 } {}
952}
danielk1977dae24952004-11-11 05:10:43 +0000953
danielk1977b92b70b2004-11-12 16:11:59 +0000954# Now test that the user-version can be read and written (and that we aren't
955# accidentally manipulating the schema-version instead).
danielk1977dae24952004-11-11 05:10:43 +0000956do_test pragma-8.2.1 {
drh25403652007-01-04 22:13:41 +0000957 execsql2 {
danielk1977b92b70b2004-11-12 16:11:59 +0000958 PRAGMA user_version;
danielk1977dae24952004-11-11 05:10:43 +0000959 }
drh25403652007-01-04 22:13:41 +0000960} {user_version 0}
danielk1977dae24952004-11-11 05:10:43 +0000961do_test pragma-8.2.2 {
962 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000963 PRAGMA user_version = 2;
danielk1977dae24952004-11-11 05:10:43 +0000964 }
965} {}
drh802d69a2005-02-13 23:34:24 +0000966do_test pragma-8.2.3.1 {
drh25403652007-01-04 22:13:41 +0000967 execsql2 {
danielk1977b92b70b2004-11-12 16:11:59 +0000968 PRAGMA user_version;
danielk1977dae24952004-11-11 05:10:43 +0000969 }
drh25403652007-01-04 22:13:41 +0000970} {user_version 2}
drh802d69a2005-02-13 23:34:24 +0000971do_test pragma-8.2.3.2 {
972 db close
973 sqlite3 db test.db
974 execsql {
975 PRAGMA user_version;
976 }
977} {2}
978do_test pragma-8.2.4.1 {
danielk1977dae24952004-11-11 05:10:43 +0000979 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000980 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000981 }
982} {108}
drh802d69a2005-02-13 23:34:24 +0000983ifcapable vacuum {
984 do_test pragma-8.2.4.2 {
985 execsql {
986 VACUUM;
987 PRAGMA user_version;
988 }
989 } {2}
990 do_test pragma-8.2.4.3 {
991 execsql {
992 PRAGMA schema_version;
993 }
994 } {109}
995}
danielk1977dae24952004-11-11 05:10:43 +0000996
danielk19775a8f9372007-10-09 08:29:32 +0000997ifcapable attach {
998 db eval {ATTACH 'test2.db' AS aux}
999
1000 # Check that the user-version in the auxilary database can be manipulated (
1001 # and that we aren't accidentally manipulating the same in the main db).
1002 do_test pragma-8.2.5 {
1003 execsql {
1004 PRAGMA aux.user_version;
1005 }
1006 } {0}
1007 do_test pragma-8.2.6 {
1008 execsql {
1009 PRAGMA aux.user_version = 3;
1010 }
1011 } {}
1012 do_test pragma-8.2.7 {
1013 execsql {
1014 PRAGMA aux.user_version;
1015 }
1016 } {3}
1017 do_test pragma-8.2.8 {
1018 execsql {
1019 PRAGMA main.user_version;
1020 }
1021 } {2}
1022
1023 # Now check that a ROLLBACK resets the user-version if it has been modified
1024 # within a transaction.
1025 do_test pragma-8.2.9 {
1026 execsql {
1027 BEGIN;
1028 PRAGMA aux.user_version = 10;
1029 PRAGMA user_version = 11;
1030 }
1031 } {}
1032 do_test pragma-8.2.10 {
1033 execsql {
1034 PRAGMA aux.user_version;
1035 }
1036 } {10}
1037 do_test pragma-8.2.11 {
1038 execsql {
1039 PRAGMA main.user_version;
1040 }
1041 } {11}
1042 do_test pragma-8.2.12 {
1043 execsql {
1044 ROLLBACK;
1045 PRAGMA aux.user_version;
1046 }
1047 } {3}
1048 do_test pragma-8.2.13 {
1049 execsql {
1050 PRAGMA main.user_version;
1051 }
1052 } {2}
1053}
danielk1977dae24952004-11-11 05:10:43 +00001054
danielk1977b92b70b2004-11-12 16:11:59 +00001055# Try a negative value for the user-version
danielk1977dae24952004-11-11 05:10:43 +00001056do_test pragma-8.2.14 {
1057 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +00001058 PRAGMA user_version = -450;
danielk1977dae24952004-11-11 05:10:43 +00001059 }
1060} {}
1061do_test pragma-8.2.15 {
1062 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +00001063 PRAGMA user_version;
danielk1977dae24952004-11-11 05:10:43 +00001064 }
1065} {-450}
danielk1977d9c847d2005-01-07 10:42:48 +00001066} ; # ifcapable schema_version
1067
drhd19744f2008-03-18 13:46:53 +00001068# Check to see if TEMP_STORE is memory or disk. Return strings
1069# "memory" or "disk" as appropriate.
1070#
1071proc check_temp_store {} {
1072 db eval {CREATE TEMP TABLE IF NOT EXISTS a(b)}
1073 db eval {PRAGMA database_list} {
1074 if {$name=="temp"} {
danielk197717b90b52008-06-06 11:11:25 +00001075 set bt [btree_from_db db 1]
1076 if {[btree_ismemdb $bt]} {
drhd19744f2008-03-18 13:46:53 +00001077 return "memory"
drhd19744f2008-03-18 13:46:53 +00001078 }
danielk197717b90b52008-06-06 11:11:25 +00001079 return "disk"
drhd19744f2008-03-18 13:46:53 +00001080 }
1081 }
1082 return "unknown"
1083}
1084
drh4ee09b42013-05-01 19:49:27 +00001085# Application_ID
1086#
1087do_test pragma-8.3.1 {
1088 execsql {
1089 PRAGMA application_id;
1090 }
1091} {0}
1092do_test pragma-8.3.2 {
1093 execsql {PRAGMA Application_ID(12345); PRAGMA application_id;}
1094} {12345}
tpoindex9a09a3c2004-12-20 19:01:32 +00001095
1096# Test temp_store and temp_store_directory pragmas
1097#
drh268283b2005-01-08 15:44:25 +00001098ifcapable pager_pragmas {
tpoindex9a09a3c2004-12-20 19:01:32 +00001099do_test pragma-9.1 {
1100 db close
1101 sqlite3 db test.db
1102 execsql {
1103 PRAGMA temp_store;
1104 }
1105} {0}
drhd19744f2008-03-18 13:46:53 +00001106if {$TEMP_STORE<=1} {
1107 do_test pragma-9.1.1 {
1108 check_temp_store
1109 } {disk}
1110} else {
1111 do_test pragma-9.1.1 {
1112 check_temp_store
1113 } {memory}
1114}
1115
tpoindex9a09a3c2004-12-20 19:01:32 +00001116do_test pragma-9.2 {
drhd19744f2008-03-18 13:46:53 +00001117 db close
1118 sqlite3 db test.db
tpoindex9a09a3c2004-12-20 19:01:32 +00001119 execsql {
1120 PRAGMA temp_store=file;
1121 PRAGMA temp_store;
1122 }
1123} {1}
drhd19744f2008-03-18 13:46:53 +00001124if {$TEMP_STORE==3} {
1125 # When TEMP_STORE is 3, always use memory regardless of pragma settings.
1126 do_test pragma-9.2.1 {
1127 check_temp_store
1128 } {memory}
1129} else {
1130 do_test pragma-9.2.1 {
1131 check_temp_store
1132 } {disk}
1133}
1134
tpoindex9a09a3c2004-12-20 19:01:32 +00001135do_test pragma-9.3 {
drhd19744f2008-03-18 13:46:53 +00001136 db close
1137 sqlite3 db test.db
tpoindex9a09a3c2004-12-20 19:01:32 +00001138 execsql {
1139 PRAGMA temp_store=memory;
1140 PRAGMA temp_store;
1141 }
1142} {2}
drhd19744f2008-03-18 13:46:53 +00001143if {$TEMP_STORE==0} {
1144 # When TEMP_STORE is 0, always use the disk regardless of pragma settings.
1145 do_test pragma-9.3.1 {
1146 check_temp_store
1147 } {disk}
1148} else {
1149 do_test pragma-9.3.1 {
1150 check_temp_store
1151 } {memory}
1152}
1153
tpoindex9a09a3c2004-12-20 19:01:32 +00001154do_test pragma-9.4 {
1155 execsql {
1156 PRAGMA temp_store_directory;
1157 }
1158} {}
drh78f82d12008-09-02 00:52:52 +00001159ifcapable wsd {
1160 do_test pragma-9.5 {
mistachkinf8a78462012-03-08 20:00:36 +00001161 set pwd [string map {' ''} [file nativename [get_pwd]]]
drh78f82d12008-09-02 00:52:52 +00001162 execsql "
1163 PRAGMA temp_store_directory='$pwd';
1164 "
1165 } {}
1166 do_test pragma-9.6 {
1167 execsql {
1168 PRAGMA temp_store_directory;
1169 }
mistachkinf8a78462012-03-08 20:00:36 +00001170 } [list [file nativename [get_pwd]]]
drh78f82d12008-09-02 00:52:52 +00001171 do_test pragma-9.7 {
1172 catchsql {
1173 PRAGMA temp_store_directory='/NON/EXISTENT/PATH/FOOBAR';
1174 }
1175 } {1 {not a writable directory}}
1176 do_test pragma-9.8 {
1177 execsql {
1178 PRAGMA temp_store_directory='';
1179 }
1180 } {}
1181 if {![info exists TEMP_STORE] || $TEMP_STORE<=1} {
1182 ifcapable tempdb {
1183 do_test pragma-9.9 {
1184 execsql {
1185 PRAGMA temp_store_directory;
1186 PRAGMA temp_store=FILE;
1187 CREATE TEMP TABLE temp_store_directory_test(a integer);
1188 INSERT INTO temp_store_directory_test values (2);
1189 SELECT * FROM temp_store_directory_test;
1190 }
1191 } {2}
1192 do_test pragma-9.10 {
1193 catchsql "
1194 PRAGMA temp_store_directory='$pwd';
1195 SELECT * FROM temp_store_directory_test;
1196 "
1197 } {1 {no such table: temp_store_directory_test}}
1198 }
tpoindex9a09a3c2004-12-20 19:01:32 +00001199 }
drh78f82d12008-09-02 00:52:52 +00001200}
danielk197795b289b2007-03-30 17:11:12 +00001201do_test pragma-9.11 {
1202 execsql {
1203 PRAGMA temp_store = 0;
1204 PRAGMA temp_store;
1205 }
1206} {0}
1207do_test pragma-9.12 {
1208 execsql {
1209 PRAGMA temp_store = 1;
1210 PRAGMA temp_store;
1211 }
1212} {1}
1213do_test pragma-9.13 {
1214 execsql {
1215 PRAGMA temp_store = 2;
1216 PRAGMA temp_store;
1217 }
1218} {2}
1219do_test pragma-9.14 {
1220 execsql {
1221 PRAGMA temp_store = 3;
1222 PRAGMA temp_store;
1223 }
1224} {0}
danielk197795b289b2007-03-30 17:11:12 +00001225do_test pragma-9.15 {
1226 catchsql {
1227 BEGIN EXCLUSIVE;
1228 CREATE TEMP TABLE temp_table(t);
1229 INSERT INTO temp_table VALUES('valuable data');
1230 PRAGMA temp_store = 1;
1231 }
1232} {1 {temporary storage cannot be changed from within a transaction}}
1233do_test pragma-9.16 {
1234 execsql {
1235 SELECT * FROM temp_table;
1236 COMMIT;
1237 }
1238} {{valuable data}}
danielk1977983e2302008-07-08 07:35:51 +00001239
1240do_test pragma-9.17 {
1241 execsql {
1242 INSERT INTO temp_table VALUES('valuable data II');
1243 SELECT * FROM temp_table;
1244 }
1245} {{valuable data} {valuable data II}}
1246
1247do_test pragma-9.18 {
1248 set rc [catch {
1249 db eval {SELECT t FROM temp_table} {
1250 execsql {pragma temp_store = 1}
1251 }
1252 } msg]
1253 list $rc $msg
1254} {1 {temporary storage cannot be changed from within a transaction}}
1255
drh268283b2005-01-08 15:44:25 +00001256} ;# ifcapable pager_pragmas
tpoindex9a09a3c2004-12-20 19:01:32 +00001257
danielk1977cc6bd382005-01-10 02:48:49 +00001258ifcapable trigger {
1259
1260do_test pragma-10.0 {
1261 catchsql {
1262 DROP TABLE main.t1;
1263 }
1264 execsql {
1265 PRAGMA count_changes = 1;
1266
1267 CREATE TABLE t1(a PRIMARY KEY);
1268 CREATE TABLE t1_mirror(a);
1269 CREATE TABLE t1_mirror2(a);
1270 CREATE TRIGGER t1_bi BEFORE INSERT ON t1 BEGIN
1271 INSERT INTO t1_mirror VALUES(new.a);
1272 END;
1273 CREATE TRIGGER t1_ai AFTER INSERT ON t1 BEGIN
1274 INSERT INTO t1_mirror2 VALUES(new.a);
1275 END;
1276 CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 BEGIN
1277 UPDATE t1_mirror SET a = new.a WHERE a = old.a;
1278 END;
1279 CREATE TRIGGER t1_au AFTER UPDATE ON t1 BEGIN
1280 UPDATE t1_mirror2 SET a = new.a WHERE a = old.a;
1281 END;
1282 CREATE TRIGGER t1_bd BEFORE DELETE ON t1 BEGIN
1283 DELETE FROM t1_mirror WHERE a = old.a;
1284 END;
1285 CREATE TRIGGER t1_ad AFTER DELETE ON t1 BEGIN
1286 DELETE FROM t1_mirror2 WHERE a = old.a;
1287 END;
1288 }
1289} {}
1290
1291do_test pragma-10.1 {
1292 execsql {
1293 INSERT INTO t1 VALUES(randstr(10,10));
1294 }
1295} {1}
1296do_test pragma-10.2 {
1297 execsql {
1298 UPDATE t1 SET a = randstr(10,10);
1299 }
1300} {1}
1301do_test pragma-10.3 {
1302 execsql {
1303 DELETE FROM t1;
1304 }
1305} {1}
1306
1307} ;# ifcapable trigger
1308
danielk197748af65a2005-02-09 03:20:37 +00001309ifcapable schema_pragmas {
1310 do_test pragma-11.1 {
1311 execsql2 {
1312 pragma collation_list;
1313 }
drh2d823312014-11-20 23:11:30 +00001314 } {seq 0 name RTRIM seq 1 name NOCASE seq 2 name BINARY}
danielk197748af65a2005-02-09 03:20:37 +00001315 do_test pragma-11.2 {
1316 db collate New_Collation blah...
1317 execsql {
1318 pragma collation_list;
1319 }
drh2d823312014-11-20 23:11:30 +00001320 } {0 New_Collation 1 RTRIM 2 NOCASE 3 BINARY}
danielk197748af65a2005-02-09 03:20:37 +00001321}
1322
danielk1977ddfb2f02006-02-17 12:25:14 +00001323ifcapable schema_pragmas&&tempdb {
1324 do_test pragma-12.1 {
1325 sqlite3 db2 test.db
1326 execsql {
1327 PRAGMA temp.table_info('abc');
1328 } db2
1329 } {}
1330 db2 close
1331
1332 do_test pragma-12.2 {
1333 sqlite3 db2 test.db
1334 execsql {
1335 PRAGMA temp.default_cache_size = 200;
1336 PRAGMA temp.default_cache_size;
1337 } db2
1338 } {200}
1339 db2 close
1340
1341 do_test pragma-12.3 {
1342 sqlite3 db2 test.db
1343 execsql {
1344 PRAGMA temp.cache_size = 400;
1345 PRAGMA temp.cache_size;
1346 } db2
1347 } {400}
1348 db2 close
1349}
1350
danielk19774b2688a2006-06-20 11:01:07 +00001351ifcapable bloblit {
1352
drh05a82982006-03-19 13:00:25 +00001353do_test pragma-13.1 {
1354 execsql {
1355 DROP TABLE IF EXISTS t4;
1356 PRAGMA vdbe_trace=on;
1357 PRAGMA vdbe_listing=on;
1358 PRAGMA sql_trace=on;
1359 CREATE TABLE t4(a INTEGER PRIMARY KEY,b);
1360 INSERT INTO t4(b) VALUES(x'0123456789abcdef0123456789abcdef0123456789');
1361 INSERT INTO t4(b) VALUES(randstr(30,30));
1362 INSERT INTO t4(b) VALUES(1.23456);
1363 INSERT INTO t4(b) VALUES(NULL);
1364 INSERT INTO t4(b) VALUES(0);
1365 INSERT INTO t4(b) SELECT b||b||b||b FROM t4;
1366 SELECT * FROM t4;
1367 }
1368 execsql {
1369 PRAGMA vdbe_trace=off;
1370 PRAGMA vdbe_listing=off;
1371 PRAGMA sql_trace=off;
1372 }
1373} {}
1374
danielk19774b2688a2006-06-20 11:01:07 +00001375} ;# ifcapable bloblit
1376
danielk197759a93792008-05-15 17:48:20 +00001377ifcapable pager_pragmas {
1378 db close
mistachkinfda06be2011-08-02 00:57:34 +00001379 forcedelete test.db
danielk197759a93792008-05-15 17:48:20 +00001380 sqlite3 db test.db
drh51a74d42015-02-28 01:04:27 +00001381
drhb3366b92015-09-11 20:54:44 +00001382 # EVIDENCE-OF: R-15672-33611 PRAGMA schema.page_count; Return the total
1383 # number of pages in the database file.
drh51a74d42015-02-28 01:04:27 +00001384 #
danielk197759a93792008-05-15 17:48:20 +00001385 do_test pragma-14.1 {
1386 execsql { pragma auto_vacuum = 0 }
drh51a74d42015-02-28 01:04:27 +00001387 execsql { pragma page_count; pragma main.page_count }
1388 } {0 0}
danielk197759a93792008-05-15 17:48:20 +00001389
1390 do_test pragma-14.2 {
1391 execsql {
1392 CREATE TABLE abc(a, b, c);
1393 PRAGMA page_count;
drh51a74d42015-02-28 01:04:27 +00001394 PRAGMA main.page_count;
1395 PRAGMA temp.page_count;
danielk197759a93792008-05-15 17:48:20 +00001396 }
drh51a74d42015-02-28 01:04:27 +00001397 } {2 2 0}
drh5d16a9a2011-10-13 14:41:22 +00001398 do_test pragma-14.2uc {
1399 execsql {pragma PAGE_COUNT}
1400 } {2}
danielk197759a93792008-05-15 17:48:20 +00001401
1402 do_test pragma-14.3 {
1403 execsql {
1404 BEGIN;
1405 CREATE TABLE def(a, b, c);
1406 PRAGMA page_count;
1407 }
1408 } {3}
drh5d16a9a2011-10-13 14:41:22 +00001409 do_test pragma-14.3uc {
1410 execsql {pragma PAGE_COUNT}
1411 } {3}
danielk197759a93792008-05-15 17:48:20 +00001412
1413 do_test pragma-14.4 {
1414 set page_size [db one {pragma page_size}]
1415 expr [file size test.db] / $page_size
1416 } {2}
1417
1418 do_test pragma-14.5 {
1419 execsql {
1420 ROLLBACK;
1421 PRAGMA page_count;
1422 }
1423 } {2}
1424
1425 do_test pragma-14.6 {
mistachkinfda06be2011-08-02 00:57:34 +00001426 forcedelete test2.db
danielk197759a93792008-05-15 17:48:20 +00001427 sqlite3 db2 test2.db
1428 execsql {
1429 PRAGMA auto_vacuum = 0;
1430 CREATE TABLE t1(a, b, c);
1431 CREATE TABLE t2(a, b, c);
1432 CREATE TABLE t3(a, b, c);
1433 CREATE TABLE t4(a, b, c);
1434 } db2
1435 db2 close
1436 execsql {
1437 ATTACH 'test2.db' AS aux;
1438 PRAGMA aux.page_count;
1439 }
1440 } {5}
drh5d16a9a2011-10-13 14:41:22 +00001441 do_test pragma-14.6uc {
1442 execsql {pragma AUX.PAGE_COUNT}
1443 } {5}
danielk197759a93792008-05-15 17:48:20 +00001444}
1445
danielk19778cf6c552008-06-23 16:53:46 +00001446# Test that the value set using the cache_size pragma is not reset when the
1447# schema is reloaded.
1448#
1449ifcapable pager_pragmas {
1450 db close
1451 sqlite3 db test.db
1452 do_test pragma-15.1 {
1453 execsql {
1454 PRAGMA cache_size=59;
1455 PRAGMA cache_size;
1456 }
1457 } {59}
1458 do_test pragma-15.2 {
1459 sqlite3 db2 test.db
1460 execsql {
1461 CREATE TABLE newtable(a, b, c);
1462 } db2
1463 db2 close
1464 } {}
1465 do_test pragma-15.3 {
1466 # Evaluating this statement will cause the schema to be reloaded (because
1467 # the schema was changed by another connection in pragma-15.2). At one
1468 # point there was a bug that reset the cache_size to its default value
1469 # when this happened.
1470 execsql { SELECT * FROM sqlite_master }
1471 execsql { PRAGMA cache_size }
1472 } {59}
1473}
1474
danielk19775558a8a2005-01-17 07:53:44 +00001475# Reset the sqlite3_temp_directory variable for the next run of tests:
1476sqlite3 dbX :memory:
1477dbX eval {PRAGMA temp_store_directory = ""}
1478dbX close
1479
danielk1977838cce42009-01-12 14:01:45 +00001480ifcapable lock_proxy_pragmas&&prefer_proxy_locking {
aswiftaebf4132008-11-21 00:10:35 +00001481 set sqlite_hostid_num 1
1482
1483 set using_proxy 0
1484 foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
1485 set using_proxy $value
1486 }
1487
1488 # Test the lock_proxy_file pragmas.
1489 #
1490 db close
1491 set env(SQLITE_FORCE_PROXY_LOCKING) "0"
1492
1493 sqlite3 db test.db
1494 do_test pragma-16.1 {
1495 execsql {
1496 PRAGMA lock_proxy_file="mylittleproxy";
1497 select * from sqlite_master;
1498 }
1499 execsql {
1500 PRAGMA lock_proxy_file;
1501 }
1502 } {mylittleproxy}
1503
1504 do_test pragma-16.2 {
1505 sqlite3 db2 test.db
1506 execsql {
1507 PRAGMA lock_proxy_file="mylittleproxy";
1508 } db2
1509 } {}
1510
1511 db2 close
1512 do_test pragma-16.2.1 {
1513 sqlite3 db2 test.db
1514 execsql {
1515 PRAGMA lock_proxy_file=":auto:";
1516 select * from sqlite_master;
1517 } db2
1518 execsql {
1519 PRAGMA lock_proxy_file;
1520 } db2
1521 } {mylittleproxy}
1522
1523 db2 close
1524 do_test pragma-16.3 {
1525 sqlite3 db2 test.db
1526 execsql {
1527 PRAGMA lock_proxy_file="myotherproxy";
1528 } db2
1529 catchsql {
1530 select * from sqlite_master;
1531 } db2
1532 } {1 {database is locked}}
1533
1534 do_test pragma-16.4 {
1535 db2 close
1536 db close
1537 sqlite3 db2 test.db
1538 execsql {
1539 PRAGMA lock_proxy_file="myoriginalproxy";
1540 PRAGMA lock_proxy_file="myotherproxy";
1541 PRAGMA lock_proxy_file;
1542 } db2
1543 } {myotherproxy}
1544
1545 db2 close
1546 set env(SQLITE_FORCE_PROXY_LOCKING) "1"
1547 do_test pragma-16.5 {
1548 sqlite3 db2 test.db
1549 execsql {
1550 PRAGMA lock_proxy_file=":auto:";
1551 PRAGMA lock_proxy_file;
1552 } db2
1553 } {myotherproxy}
1554
1555 do_test pragma-16.6 {
1556 db2 close
1557 sqlite3 db2 test2.db
1558 set lockpath [execsql {
1559 PRAGMA lock_proxy_file=":auto:";
1560 PRAGMA lock_proxy_file;
1561 } db2]
1562 string match "*test2.db:auto:" $lockpath
1563 } {1}
1564
1565 set sqlite_hostid_num 2
1566 do_test pragma-16.7 {
dan14d14602010-10-06 16:42:52 +00001567 list [catch {
1568 sqlite3 db test2.db
1569 execsql {
1570 PRAGMA lock_proxy_file=":auto:";
1571 select * from sqlite_master;
1572 }
1573 } msg] $msg
aswiftaebf4132008-11-21 00:10:35 +00001574 } {1 {database is locked}}
1575 db close
1576
1577 do_test pragma-16.8 {
dan14d14602010-10-06 16:42:52 +00001578 list [catch {
1579 sqlite3 db test2.db
1580 execsql { select * from sqlite_master }
1581 } msg] $msg
aswiftaebf4132008-11-21 00:10:35 +00001582 } {1 {database is locked}}
1583
1584 db2 close
1585 do_test pragma-16.8.1 {
1586 execsql {
1587 PRAGMA lock_proxy_file="yetanotherproxy";
1588 PRAGMA lock_proxy_file;
1589 }
1590 } {yetanotherproxy}
1591 do_test pragma-16.8.2 {
1592 execsql {
1593 create table mine(x);
1594 }
1595 } {}
1596
1597 db close
1598 do_test pragma-16.9 {
1599 sqlite3 db proxytest.db
1600 set lockpath2 [execsql {
1601 PRAGMA lock_proxy_file=":auto:";
1602 PRAGMA lock_proxy_file;
1603 } db]
1604 string match "*proxytest.db:auto:" $lockpath2
1605 } {1}
1606
1607 set env(SQLITE_FORCE_PROXY_LOCKING) $using_proxy
1608 set sqlite_hostid_num 0
1609}
drhd2cb50b2009-01-09 21:41:17 +00001610
1611# Parsing of auto_vacuum settings.
1612#
1613foreach {autovac_setting val} {
1614 0 0
1615 1 1
1616 2 2
1617 3 0
1618 -1 0
1619 none 0
1620 NONE 0
1621 NoNe 0
1622 full 1
1623 FULL 1
1624 incremental 2
1625 INCREMENTAL 2
1626 -1234 0
1627 1234 0
1628} {
1629 do_test pragma-17.1.$autovac_setting {
1630 catch {db close}
1631 sqlite3 db :memory:
1632 execsql "
1633 PRAGMA auto_vacuum=$::autovac_setting;
1634 PRAGMA auto_vacuum;
1635 "
1636 } $val
1637}
1638
1639# Parsing of temp_store settings.
1640#
1641foreach {temp_setting val} {
1642 0 0
1643 1 1
1644 2 2
1645 3 0
1646 -1 0
1647 file 1
1648 FILE 1
1649 fIlE 1
1650 memory 2
1651 MEMORY 2
1652 MeMoRy 2
1653} {
1654 do_test pragma-18.1.$temp_setting {
1655 catch {db close}
1656 sqlite3 db :memory:
1657 execsql "
1658 PRAGMA temp_store=$::temp_setting;
1659 PRAGMA temp_store=$::temp_setting;
1660 PRAGMA temp_store;
1661 "
1662 } $val
1663}
1664
drh92c700d2012-02-22 19:56:17 +00001665# The SQLITE_FCNTL_PRAGMA logic, with error handling.
1666#
1667db close
1668testvfs tvfs
1669sqlite3 db test.db -vfs tvfs
1670do_test pragma-19.1 {
1671 catchsql {PRAGMA error}
1672} {1 {SQL logic error or missing database}}
1673do_test pragma-19.2 {
1674 catchsql {PRAGMA error='This is the error message'}
1675} {1 {This is the error message}}
1676do_test pragma-19.3 {
1677 catchsql {PRAGMA error='7 This is the error message'}
1678} {1 {This is the error message}}
1679do_test pragma-19.4 {
1680 catchsql {PRAGMA error=7}
1681} {1 {out of memory}}
drhc8517f62012-02-22 20:08:49 +00001682do_test pragma-19.5 {
mistachkin5b044542012-03-02 22:41:06 +00001683 file tail [lindex [execsql {PRAGMA filename}] 0]
drhc8517f62012-02-22 20:08:49 +00001684} {test.db}
drh92c700d2012-02-22 19:56:17 +00001685
drhcc716452012-06-06 23:23:23 +00001686if {$tcl_platform(platform)=="windows"} {
mistachkina112d142012-03-14 00:44:01 +00001687# Test data_store_directory pragma
1688#
1689db close
1690sqlite3 db test.db
1691file mkdir data_dir
1692do_test pragma-20.1 {
1693 catchsql {PRAGMA data_store_directory}
1694} {0 {}}
1695do_test pragma-20.2 {
1696 set pwd [string map {' ''} [file nativename [get_pwd]]]
1697 catchsql "PRAGMA data_store_directory='$pwd';"
1698} {0 {}}
1699do_test pragma-20.3 {
1700 catchsql {PRAGMA data_store_directory}
1701} [list 0 [list [file nativename [get_pwd]]]]
1702do_test pragma-20.4 {
1703 set pwd [string map {' ''} [file nativename \
1704 [file join [get_pwd] data_dir]]]
1705 catchsql "PRAGMA data_store_directory='$pwd';"
1706} {0 {}}
1707do_test pragma-20.5 {
1708 sqlite3 db2 test2.db
1709 catchsql "PRAGMA database_list;" db2
1710} [list 0 [list 0 main [file nativename \
1711 [file join [get_pwd] data_dir test2.db]]]]
1712catch {db2 close}
1713do_test pragma-20.6 {
1714 sqlite3 db2 [file join [get_pwd] test2.db]
1715 catchsql "PRAGMA database_list;" db2
1716} [list 0 [list 0 main [file nativename \
1717 [file join [get_pwd] test2.db]]]]
1718catch {db2 close}
1719do_test pragma-20.7 {
1720 catchsql "PRAGMA data_store_directory='';"
1721} {0 {}}
1722do_test pragma-20.8 {
1723 catchsql {PRAGMA data_store_directory}
1724} {0 {}}
drh92c700d2012-02-22 19:56:17 +00001725
mistachkina112d142012-03-14 00:44:01 +00001726forcedelete data_dir
drhcc716452012-06-06 23:23:23 +00001727} ;# endif windows
drhcd61c282002-03-06 22:01:34 +00001728
dan1fed5da2014-02-25 21:01:25 +00001729database_may_be_corrupt
1730
dan5885e762012-07-16 10:06:12 +00001731do_test 21.1 {
1732 # Create a corrupt database in testerr.db. And a non-corrupt at test.db.
1733 #
1734 db close
1735 forcedelete test.db
1736 sqlite3 db test.db
1737 execsql {
1738 PRAGMA page_size = 1024;
1739 PRAGMA auto_vacuum = 0;
1740 CREATE TABLE t1(a PRIMARY KEY, b);
1741 INSERT INTO t1 VALUES(1, 1);
1742 }
1743 for {set i 0} {$i < 10} {incr i} {
1744 execsql { INSERT INTO t1 SELECT a + (1 << $i), b + (1 << $i) FROM t1 }
1745 }
1746 db close
1747 forcecopy test.db testerr.db
1748 hexio_write testerr.db 15000 [string repeat 55 100]
1749} {100}
1750
1751set mainerr {*** in database main ***
1752Multiple uses for byte 672 of page 15}
1753set auxerr {*** in database aux ***
1754Multiple uses for byte 672 of page 15}
1755
dan597515d2014-02-28 18:39:51 +00001756set mainerr {/{\*\*\* in database main \*\*\*
1757Multiple uses for byte 672 of page 15}.*/}
1758set auxerr {/{\*\*\* in database aux \*\*\*
1759Multiple uses for byte 672 of page 15}.*/}
1760
dan5885e762012-07-16 10:06:12 +00001761do_test 22.2 {
1762 catch { db close }
1763 sqlite3 db testerr.db
1764 execsql { PRAGMA integrity_check }
dan597515d2014-02-28 18:39:51 +00001765} $mainerr
dan5885e762012-07-16 10:06:12 +00001766
1767do_test 22.3.1 {
1768 catch { db close }
1769 sqlite3 db test.db
1770 execsql {
1771 ATTACH 'testerr.db' AS 'aux';
1772 PRAGMA integrity_check;
1773 }
dan597515d2014-02-28 18:39:51 +00001774} $auxerr
dan5885e762012-07-16 10:06:12 +00001775do_test 22.3.2 {
1776 execsql { PRAGMA main.integrity_check; }
1777} {ok}
1778do_test 22.3.3 {
1779 execsql { PRAGMA aux.integrity_check; }
dan597515d2014-02-28 18:39:51 +00001780} $auxerr
dan5885e762012-07-16 10:06:12 +00001781
1782do_test 22.4.1 {
1783 catch { db close }
1784 sqlite3 db testerr.db
1785 execsql {
1786 ATTACH 'test.db' AS 'aux';
1787 PRAGMA integrity_check;
1788 }
dan597515d2014-02-28 18:39:51 +00001789} $mainerr
dan5885e762012-07-16 10:06:12 +00001790do_test 22.4.2 {
1791 execsql { PRAGMA main.integrity_check; }
dan597515d2014-02-28 18:39:51 +00001792} $mainerr
dan5885e762012-07-16 10:06:12 +00001793do_test 22.4.3 {
1794 execsql { PRAGMA aux.integrity_check; }
1795} {ok}
1796
drhc95e01d2013-02-14 16:16:05 +00001797db close
1798forcedelete test.db test.db-wal test.db-journal
1799sqlite3 db test.db
1800sqlite3 db2 test.db
1801do_test 23.1 {
1802 db eval {
1803 CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c,d);
1804 CREATE INDEX i1 ON t1(b,c);
1805 CREATE INDEX i2 ON t1(c,d);
drh7be0fd92015-03-05 15:34:15 +00001806 CREATE INDEX i2x ON t1(d COLLATE nocase, c DESC);
drhc95e01d2013-02-14 16:16:05 +00001807 CREATE TABLE t2(x INTEGER REFERENCES t1);
1808 }
1809 db2 eval {SELECT name FROM sqlite_master}
drh7be0fd92015-03-05 15:34:15 +00001810} {t1 i1 i2 i2x t2}
drhc228be52015-01-31 02:00:01 +00001811do_test 23.2a {
drhc95e01d2013-02-14 16:16:05 +00001812 db eval {
1813 DROP INDEX i2;
1814 CREATE INDEX i2 ON t1(c,d,b);
1815 }
drhc228be52015-01-31 02:00:01 +00001816 capture_pragma db2 out {PRAGMA index_info(i2)}
drh5e7028c2015-03-05 14:29:02 +00001817 db2 eval {SELECT cid, name, '|' FROM out ORDER BY seqno}
1818} {2 c | 3 d | 1 b |}
drh7be0fd92015-03-05 15:34:15 +00001819
drhb3366b92015-09-11 20:54:44 +00001820# EVIDENCE-OF: R-56143-29319 PRAGMA schema.index_xinfo(index-name); This
1821# pragma returns information about every column in an index.
drh7be0fd92015-03-05 15:34:15 +00001822#
1823# EVIDENCE-OF: R-45970-35618 Unlike this index_info pragma, this pragma
1824# returns information about every column in the index, not just the key
1825# columns.
1826#
drhc228be52015-01-31 02:00:01 +00001827do_test 23.2b {
1828 capture_pragma db2 out {PRAGMA index_xinfo(i2)}
1829 db2 eval {SELECT cid, name, "desc", coll, "key", '|' FROM out ORDER BY seqno}
1830} {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 +00001831
1832# (The first column of output from PRAGMA index_xinfo is...)
1833# EVIDENCE-OF: R-00197-14279 The rank of the column within the index. (0
1834# means left-most. Key columns come before auxiliary columns.)
1835#
1836# (The second column of output from PRAGMA index_xinfo is...)
1837# EVIDENCE-OF: R-40889-06838 The rank of the column within the table
1838# being indexed, or -1 if the index-column is the rowid of the table
1839# being indexed.
1840#
1841# (The third column of output from PRAGMA index_xinfo is...)
1842# EVIDENCE-OF: R-22751-28901 The name of the column being indexed, or
1843# NULL if the index-column is the rowid of the table being indexed.
1844#
1845# (The fourth column of output from PRAGMA index_xinfo is...)
1846# EVIDENCE-OF: R-11847-09179 1 if the index-column is sorted in reverse
1847# (DESC) order by the index and 0 otherwise.
1848#
1849# (The fifth column of output from PRAGMA index_xinfo is...)
1850# EVIDENCE-OF: R-15313-19540 The name for the collating sequence used to
1851# compare values in the index-column.
1852#
1853# (The sixth column of output from PRAGMA index_xinfo is...)
1854# EVIDENCE-OF: R-14310-64553 1 if the index-column is a key column and 0
1855# if the index-column is an auxiliary column.
1856#
1857do_test 23.2c {
1858 db2 eval {PRAGMA index_xinfo(i2)}
1859} {0 2 c 0 BINARY 1 1 3 d 0 BINARY 1 2 1 b 0 BINARY 1 3 -1 {} 0 BINARY 0}
1860do_test 23.2d {
1861 db2 eval {PRAGMA index_xinfo(i2x)}
1862} {0 3 d 0 nocase 1 1 2 c 1 BINARY 1 2 -1 {} 0 BINARY 0}
1863
drhb3366b92015-09-11 20:54:44 +00001864# EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This
1865# pragma returns one row for each index associated with the given table.
drh7be0fd92015-03-05 15:34:15 +00001866#
1867# (The first column of output from PRAGMA index_list is...)
1868# EVIDENCE-OF: R-02753-24748 A sequence number assigned to each index
1869# for internal tracking purposes.
1870#
1871# (The second column of output from PRAGMA index_list is...)
1872# EVIDENCE-OF: R-35496-03635 The name of the index.
1873#
1874# (The third column of output from PRAGMA index_list is...)
1875# EVIDENCE-OF: R-57301-64506 "1" if the index is UNIQUE and "0" if not.
1876#
1877# (The fourth column of output from PRAGMA index_list is...)
1878# EVIDENCE-OF: R-36609-39554 "c" if the index was created by a CREATE
1879# INDEX statement, "u" if the index was created by a UNIQUE constraint,
1880# or "pk" if the index was created by a PRIMARY KEY constraint.
1881#
drhc95e01d2013-02-14 16:16:05 +00001882do_test 23.3 {
1883 db eval {
1884 CREATE INDEX i3 ON t1(d,b,c);
1885 }
drhc228be52015-01-31 02:00:01 +00001886 capture_pragma db2 out {PRAGMA index_list(t1)}
drh7be0fd92015-03-05 15:34:15 +00001887 db2 eval {SELECT seq, name, "unique", origin, '|' FROM out ORDER BY seq}
1888} {0 i3 0 c | 1 i2 0 c | 2 i2x 0 c | 3 i1 0 c |}
drhc95e01d2013-02-14 16:16:05 +00001889do_test 23.4 {
1890 db eval {
1891 ALTER TABLE t1 ADD COLUMN e;
1892 }
1893 db2 eval {
1894 PRAGMA table_info(t1);
1895 }
1896} {/4 e {} 0 {} 0/}
1897do_test 23.5 {
1898 db eval {
1899 DROP TABLE t2;
1900 CREATE TABLE t2(x, y INTEGER REFERENCES t1);
1901 }
1902 db2 eval {
1903 PRAGMA foreign_key_list(t2);
1904 }
1905} {0 0 t1 y {} {NO ACTION} {NO ACTION} NONE}
1906
dan1fed5da2014-02-25 21:01:25 +00001907database_never_corrupt
drhcd61c282002-03-06 22:01:34 +00001908finish_test