blob: d2a756f61a02417b9f719a50e737333aa0c3bc46 [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
19
dan68928b62010-06-22 13:46:43 +000020# Do not use a codec for tests in this file, as the database file is
21# manipulated directly using tcl scripts (using the [hexio_write] command).
22#
23do_not_use_codec
24
danielk197791cf71b2004-06-26 06:37:06 +000025# Test organization:
26#
27# pragma-1.*: Test cache_size, default_cache_size and synchronous on main db.
28# pragma-2.*: Test synchronous on attached db.
29# pragma-3.*: Test detection of table/index inconsistency by integrity_check.
30# pragma-4.*: Test cache_size and default_cache_size on attached db.
31# pragma-5.*: Test that pragma synchronous may not be used inside of a
32# transaction.
danielk1977cc6bd382005-01-10 02:48:49 +000033# pragma-6.*: Test schema-query pragmas.
34# pragma-7.*: Miscellaneous tests.
35# pragma-8.*: Test user_version and schema_version pragmas.
tpoindex9a09a3c2004-12-20 19:01:32 +000036# pragma-9.*: Test temp_store and temp_store_directory.
danielk1977cc6bd382005-01-10 02:48:49 +000037# pragma-10.*: Test the count_changes pragma in the presence of triggers.
danielk197748af65a2005-02-09 03:20:37 +000038# pragma-11.*: Test the collation_list pragma.
danielk197759a93792008-05-15 17:48:20 +000039# pragma-14.*: Test the page_count pragma.
danielk19778cf6c552008-06-23 16:53:46 +000040# pragma-15.*: Test that the value set using the cache_size pragma is not
41# reset when the schema is reloaded.
aswiftaebf4132008-11-21 00:10:35 +000042# pragma-16.*: Test proxy locking
danielk197791cf71b2004-06-26 06:37:06 +000043#
44
drh05a82982006-03-19 13:00:25 +000045ifcapable !pragma {
46 finish_test
47 return
48}
49
drhcd61c282002-03-06 22:01:34 +000050# Delete the preexisting database to avoid the special setup
51# that the "all.test" script does.
52#
53db close
mistachkinfda06be2011-08-02 00:57:34 +000054delete_file test.db test.db-journal
55delete_file test3.db test3.db-journal
drhdddca282006-01-03 00:33:50 +000056sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
drhcd61c282002-03-06 22:01:34 +000057
drh1e9daa62007-04-06 21:42:22 +000058
danielk1977c7b4a442004-11-23 10:52:51 +000059ifcapable pager_pragmas {
drh1e9daa62007-04-06 21:42:22 +000060set DFLT_CACHE_SZ [db one {PRAGMA default_cache_size}]
61set TEMP_CACHE_SZ [db one {PRAGMA temp.default_cache_size}]
drhcd61c282002-03-06 22:01:34 +000062do_test pragma-1.1 {
63 execsql {
64 PRAGMA cache_size;
65 PRAGMA default_cache_size;
66 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +000067 }
drh1e9daa62007-04-06 21:42:22 +000068} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
drhcd61c282002-03-06 22:01:34 +000069do_test pragma-1.2 {
70 execsql {
drheb43e5c2005-05-22 20:30:39 +000071 PRAGMA synchronous=OFF;
drhcd61c282002-03-06 22:01:34 +000072 PRAGMA cache_size=1234;
73 PRAGMA cache_size;
74 PRAGMA default_cache_size;
75 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +000076 }
drh1e9daa62007-04-06 21:42:22 +000077} [list 1234 $DFLT_CACHE_SZ 0]
drhcd61c282002-03-06 22:01:34 +000078do_test pragma-1.3 {
79 db close
drhef4ac8f2004-06-19 00:16:31 +000080 sqlite3 db test.db
drhcd61c282002-03-06 22:01:34 +000081 execsql {
82 PRAGMA cache_size;
83 PRAGMA default_cache_size;
84 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +000085 }
drh1e9daa62007-04-06 21:42:22 +000086} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
drhcd61c282002-03-06 22:01:34 +000087do_test pragma-1.4 {
88 execsql {
89 PRAGMA synchronous=OFF;
90 PRAGMA cache_size;
91 PRAGMA default_cache_size;
92 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +000093 }
drh1e9daa62007-04-06 21:42:22 +000094} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 0]
drhcd61c282002-03-06 22:01:34 +000095do_test pragma-1.5 {
96 execsql {
drhd2cb50b2009-01-09 21:41:17 +000097 PRAGMA cache_size=-4321;
drhcd61c282002-03-06 22:01:34 +000098 PRAGMA cache_size;
99 PRAGMA default_cache_size;
100 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +0000101 }
drh1e9daa62007-04-06 21:42:22 +0000102} [list 4321 $DFLT_CACHE_SZ 0]
drhcd61c282002-03-06 22:01:34 +0000103do_test pragma-1.6 {
104 execsql {
105 PRAGMA synchronous=ON;
106 PRAGMA cache_size;
107 PRAGMA default_cache_size;
108 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +0000109 }
drh1e9daa62007-04-06 21:42:22 +0000110} [list 4321 $DFLT_CACHE_SZ 1]
drhcd61c282002-03-06 22:01:34 +0000111do_test pragma-1.7 {
112 db close
drhef4ac8f2004-06-19 00:16:31 +0000113 sqlite3 db test.db
drhcd61c282002-03-06 22:01:34 +0000114 execsql {
115 PRAGMA cache_size;
116 PRAGMA default_cache_size;
117 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +0000118 }
drh1e9daa62007-04-06 21:42:22 +0000119} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
drhcd61c282002-03-06 22:01:34 +0000120do_test pragma-1.8 {
121 execsql {
drhd2cb50b2009-01-09 21:41:17 +0000122 PRAGMA default_cache_size=-123;
drhcd61c282002-03-06 22:01:34 +0000123 PRAGMA cache_size;
124 PRAGMA default_cache_size;
125 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +0000126 }
danielk197791cf71b2004-06-26 06:37:06 +0000127} {123 123 2}
drh802d69a2005-02-13 23:34:24 +0000128do_test pragma-1.9.1 {
drhcd61c282002-03-06 22:01:34 +0000129 db close
drhdddca282006-01-03 00:33:50 +0000130 sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
drhcd61c282002-03-06 22:01:34 +0000131 execsql {
132 PRAGMA cache_size;
133 PRAGMA default_cache_size;
134 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +0000135 }
danielk197791cf71b2004-06-26 06:37:06 +0000136} {123 123 2}
drh802d69a2005-02-13 23:34:24 +0000137ifcapable vacuum {
138 do_test pragma-1.9.2 {
139 execsql {
140 VACUUM;
141 PRAGMA cache_size;
142 PRAGMA default_cache_size;
143 PRAGMA synchronous;
144 }
145 } {123 123 2}
146}
danielk197791cf71b2004-06-26 06:37:06 +0000147do_test pragma-1.10 {
drh5a387052003-01-11 14:19:51 +0000148 execsql {
drh4303fee2003-02-15 23:09:17 +0000149 PRAGMA synchronous=NORMAL;
150 PRAGMA cache_size;
151 PRAGMA default_cache_size;
152 PRAGMA synchronous;
drh4303fee2003-02-15 23:09:17 +0000153 }
danielk197791cf71b2004-06-26 06:37:06 +0000154} {123 123 1}
155do_test pragma-1.11 {
drh4303fee2003-02-15 23:09:17 +0000156 execsql {
157 PRAGMA synchronous=FULL;
158 PRAGMA cache_size;
159 PRAGMA default_cache_size;
160 PRAGMA synchronous;
drh4303fee2003-02-15 23:09:17 +0000161 }
danielk197791cf71b2004-06-26 06:37:06 +0000162} {123 123 2}
163do_test pragma-1.12 {
drh4303fee2003-02-15 23:09:17 +0000164 db close
drhdddca282006-01-03 00:33:50 +0000165 sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
drh4303fee2003-02-15 23:09:17 +0000166 execsql {
167 PRAGMA cache_size;
168 PRAGMA default_cache_size;
169 PRAGMA synchronous;
drh4303fee2003-02-15 23:09:17 +0000170 }
danielk197791cf71b2004-06-26 06:37:06 +0000171} {123 123 2}
172
drh5260f7e2004-06-26 19:35:29 +0000173# Make sure the pragma handler understands numeric values in addition
174# to keywords like "off" and "full".
175#
176do_test pragma-1.13 {
177 execsql {
178 PRAGMA synchronous=0;
179 PRAGMA synchronous;
180 }
181} {0}
182do_test pragma-1.14 {
183 execsql {
184 PRAGMA synchronous=2;
185 PRAGMA synchronous;
186 }
187} {2}
danielk1977c7b4a442004-11-23 10:52:51 +0000188} ;# ifcapable pager_pragmas
drh5260f7e2004-06-26 19:35:29 +0000189
190# Test turning "flag" pragmas on and off.
191#
danielk19776338c762007-05-17 16:38:30 +0000192ifcapable debug {
193 # Pragma "vdbe_listing" is only available if compiled with SQLITE_DEBUG
194 #
195 do_test pragma-1.15 {
196 execsql {
197 PRAGMA vdbe_listing=YES;
198 PRAGMA vdbe_listing;
199 }
200 } {1}
201 do_test pragma-1.16 {
202 execsql {
203 PRAGMA vdbe_listing=NO;
204 PRAGMA vdbe_listing;
205 }
206 } {0}
207}
208
drh5260f7e2004-06-26 19:35:29 +0000209do_test pragma-1.17 {
210 execsql {
211 PRAGMA parser_trace=ON;
212 PRAGMA parser_trace=OFF;
213 }
214} {}
215do_test pragma-1.18 {
216 execsql {
217 PRAGMA bogus = -1234; -- Parsing of negative values
218 }
219} {}
220
danielk197791cf71b2004-06-26 06:37:06 +0000221# Test modifying the safety_level of an attached database.
danielk19775a8f9372007-10-09 08:29:32 +0000222ifcapable pager_pragmas&&attach {
223 do_test pragma-2.1 {
mistachkinfda06be2011-08-02 00:57:34 +0000224 forcedelete test2.db
225 forcedelete test2.db-journal
danielk19775a8f9372007-10-09 08:29:32 +0000226 execsql {
227 ATTACH 'test2.db' AS aux;
228 }
229 } {}
230 do_test pragma-2.2 {
231 execsql {
232 pragma aux.synchronous;
233 }
234 } {2}
235 do_test pragma-2.3 {
236 execsql {
237 pragma aux.synchronous = OFF;
238 pragma aux.synchronous;
239 pragma synchronous;
240 }
241 } {0 2}
242 do_test pragma-2.4 {
243 execsql {
244 pragma aux.synchronous = ON;
245 pragma synchronous;
246 pragma aux.synchronous;
247 }
248 } {2 1}
danielk1977c7b4a442004-11-23 10:52:51 +0000249} ;# ifcapable pager_pragmas
drh4303fee2003-02-15 23:09:17 +0000250
drhed717fe2003-06-15 23:42:24 +0000251# Construct a corrupted index and make sure the integrity_check
252# pragma finds it.
253#
drh25d65432004-07-22 15:02:25 +0000254# These tests won't work if the database is encrypted
255#
drhed717fe2003-06-15 23:42:24 +0000256do_test pragma-3.1 {
drh1e9daa62007-04-06 21:42:22 +0000257 db close
mistachkinfda06be2011-08-02 00:57:34 +0000258 forcedelete test.db test.db-journal
drh1e9daa62007-04-06 21:42:22 +0000259 sqlite3 db test.db
drhed717fe2003-06-15 23:42:24 +0000260 execsql {
drh1e9daa62007-04-06 21:42:22 +0000261 PRAGMA auto_vacuum=OFF;
drhed717fe2003-06-15 23:42:24 +0000262 BEGIN;
263 CREATE TABLE t2(a,b,c);
264 CREATE INDEX i2 ON t2(a);
265 INSERT INTO t2 VALUES(11,2,3);
266 INSERT INTO t2 VALUES(22,3,4);
267 COMMIT;
268 SELECT rowid, * from t2;
269 }
270} {1 11 2 3 2 22 3 4}
danielk19775a8f9372007-10-09 08:29:32 +0000271ifcapable attach {
272 if {![sqlite3 -has-codec] && $sqlite_options(integrityck)} {
273 do_test pragma-3.2 {
drhbb8a2792008-03-19 00:21:30 +0000274 db eval {SELECT rootpage FROM sqlite_master WHERE name='i2'} break
275 set pgsz [db eval {PRAGMA page_size}]
276 # overwrite the header on the rootpage of the index in order to
277 # make the index appear to be empty.
278 #
279 set offset [expr {$pgsz*($rootpage-1)}]
280 hexio_write test.db $offset 0a00000000040000000000
281 db close
282 sqlite3 db test.db
danielk19775a8f9372007-10-09 08:29:32 +0000283 execsql {PRAGMA integrity_check}
drhbb8a2792008-03-19 00:21:30 +0000284 } {{rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000285 do_test pragma-3.3 {
286 execsql {PRAGMA integrity_check=1}
287 } {{rowid 1 missing from index i2}}
288 do_test pragma-3.4 {
289 execsql {
290 ATTACH DATABASE 'test.db' AS t2;
291 PRAGMA integrity_check
292 }
drhbb8a2792008-03-19 00:21:30 +0000293 } {{rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000294 do_test pragma-3.5 {
295 execsql {
drhbb8a2792008-03-19 00:21:30 +0000296 PRAGMA integrity_check=4
danielk19775a8f9372007-10-09 08:29:32 +0000297 }
drhbb8a2792008-03-19 00:21:30 +0000298 } {{rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2} {rowid 1 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000299 do_test pragma-3.6 {
300 execsql {
301 PRAGMA integrity_check=xyz
302 }
drhbb8a2792008-03-19 00:21:30 +0000303 } {{rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000304 do_test pragma-3.7 {
305 execsql {
306 PRAGMA integrity_check=0
307 }
drhbb8a2792008-03-19 00:21:30 +0000308 } {{rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000309
310 # Add additional corruption by appending unused pages to the end of
311 # the database file testerr.db
312 #
313 do_test pragma-3.8 {
314 execsql {DETACH t2}
mistachkinfda06be2011-08-02 00:57:34 +0000315 forcedelete testerr.db testerr.db-journal
danielk19775a8f9372007-10-09 08:29:32 +0000316 set out [open testerr.db w]
317 fconfigure $out -translation binary
318 set in [open test.db r]
319 fconfigure $in -translation binary
320 puts -nonewline $out [read $in]
321 seek $in 0
322 puts -nonewline $out [read $in]
323 close $in
324 close $out
drhdd3cd972010-03-27 17:12:36 +0000325 hexio_write testerr.db 28 00000000
danielk19775a8f9372007-10-09 08:29:32 +0000326 execsql {REINDEX t2}
327 execsql {PRAGMA integrity_check}
328 } {ok}
danielk197741c58b72007-12-29 13:39:19 +0000329 do_test pragma-3.8.1 {
330 execsql {PRAGMA quick_check}
331 } {ok}
danielk19775a8f9372007-10-09 08:29:32 +0000332 do_test pragma-3.9 {
333 execsql {
334 ATTACH 'testerr.db' AS t2;
335 PRAGMA integrity_check
336 }
337 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000338Page 4 is never used
339Page 5 is never used
drhbb8a2792008-03-19 00:21:30 +0000340Page 6 is never used} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000341 do_test pragma-3.10 {
342 execsql {
343 PRAGMA integrity_check=1
344 }
345 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000346Page 4 is never used}}
danielk19775a8f9372007-10-09 08:29:32 +0000347 do_test pragma-3.11 {
348 execsql {
349 PRAGMA integrity_check=5
350 }
351 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000352Page 4 is never used
353Page 5 is never used
drhbb8a2792008-03-19 00:21:30 +0000354Page 6 is never used} {rowid 1 missing from index i2} {rowid 2 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000355 do_test pragma-3.12 {
356 execsql {
357 PRAGMA integrity_check=4
358 }
359 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000360Page 4 is never used
361Page 5 is never used
362Page 6 is never used} {rowid 1 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000363 do_test pragma-3.13 {
364 execsql {
365 PRAGMA integrity_check=3
366 }
367 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000368Page 4 is never used
369Page 5 is never used
370Page 6 is never used}}
danielk19775a8f9372007-10-09 08:29:32 +0000371 do_test pragma-3.14 {
372 execsql {
373 PRAGMA integrity_check(2)
374 }
375 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000376Page 4 is never used
377Page 5 is never used}}
danielk19775a8f9372007-10-09 08:29:32 +0000378 do_test pragma-3.15 {
379 execsql {
380 ATTACH 'testerr.db' AS t3;
381 PRAGMA integrity_check
382 }
383 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000384Page 4 is never used
385Page 5 is never used
drhbb8a2792008-03-19 00:21:30 +0000386Page 6 is never used} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 ***
drh1dcdbc02007-01-27 02:24:54 +0000387Page 4 is never used
388Page 5 is never used
drhbb8a2792008-03-19 00:21:30 +0000389Page 6 is never used} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000390 do_test pragma-3.16 {
391 execsql {
drhbb8a2792008-03-19 00:21:30 +0000392 PRAGMA integrity_check(10)
danielk19775a8f9372007-10-09 08:29:32 +0000393 }
394 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000395Page 4 is never used
396Page 5 is never used
drhbb8a2792008-03-19 00:21:30 +0000397Page 6 is never used} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 ***
drh1dcdbc02007-01-27 02:24:54 +0000398Page 4 is never used
399Page 5 is never used
400Page 6 is never used} {rowid 1 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000401 do_test pragma-3.17 {
402 execsql {
drhbb8a2792008-03-19 00:21:30 +0000403 PRAGMA integrity_check=8
danielk19775a8f9372007-10-09 08:29:32 +0000404 }
405 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000406Page 4 is never used
407Page 5 is never used
drhbb8a2792008-03-19 00:21:30 +0000408Page 6 is never used} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 ***
drh1dcdbc02007-01-27 02:24:54 +0000409Page 4 is never used
410Page 5 is never used}}
danielk19775a8f9372007-10-09 08:29:32 +0000411 do_test pragma-3.18 {
412 execsql {
413 PRAGMA integrity_check=4
414 }
415 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000416Page 4 is never used
417Page 5 is never used
418Page 6 is never used} {rowid 1 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000419 }
drhd2cb50b2009-01-09 21:41:17 +0000420 do_test pragma-3.19 {
421 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +0000422 forcedelete test.db test.db-journal
drhd2cb50b2009-01-09 21:41:17 +0000423 sqlite3 db test.db
424 db eval {PRAGMA integrity_check}
425 } {ok}
drh25d65432004-07-22 15:02:25 +0000426}
drhd2cb50b2009-01-09 21:41:17 +0000427#exit
danielk197791cf71b2004-06-26 06:37:06 +0000428
429# Test modifying the cache_size of an attached database.
danielk19775a8f9372007-10-09 08:29:32 +0000430ifcapable pager_pragmas&&attach {
danielk197791cf71b2004-06-26 06:37:06 +0000431do_test pragma-4.1 {
432 execsql {
drh1e9daa62007-04-06 21:42:22 +0000433 ATTACH 'test2.db' AS aux;
danielk197791cf71b2004-06-26 06:37:06 +0000434 pragma aux.cache_size;
435 pragma aux.default_cache_size;
436 }
drh1e9daa62007-04-06 21:42:22 +0000437} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000438do_test pragma-4.2 {
439 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000440 pragma aux.cache_size = 50;
441 pragma aux.cache_size;
442 pragma aux.default_cache_size;
443 }
drh1e9daa62007-04-06 21:42:22 +0000444} [list 50 $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000445do_test pragma-4.3 {
446 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000447 pragma aux.default_cache_size = 456;
448 pragma aux.cache_size;
449 pragma aux.default_cache_size;
450 }
451} {456 456}
drh1bdd9b52004-04-23 17:04:44 +0000452do_test pragma-4.4 {
453 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000454 pragma cache_size;
455 pragma default_cache_size;
456 }
drh1e9daa62007-04-06 21:42:22 +0000457} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000458do_test pragma-4.5 {
459 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000460 DETACH aux;
461 ATTACH 'test3.db' AS aux;
462 pragma aux.cache_size;
463 pragma aux.default_cache_size;
464 }
drh1e9daa62007-04-06 21:42:22 +0000465} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000466do_test pragma-4.6 {
467 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000468 DETACH aux;
469 ATTACH 'test2.db' AS aux;
470 pragma aux.cache_size;
471 pragma aux.default_cache_size;
472 }
473} {456 456}
danielk1977c7b4a442004-11-23 10:52:51 +0000474} ;# ifcapable pager_pragmas
drh1bdd9b52004-04-23 17:04:44 +0000475
danielk197791cf71b2004-06-26 06:37:06 +0000476# Test that modifying the sync-level in the middle of a transaction is
477# disallowed.
danielk1977c7b4a442004-11-23 10:52:51 +0000478ifcapable pager_pragmas {
danielk197791cf71b2004-06-26 06:37:06 +0000479do_test pragma-5.0 {
drh1bdd9b52004-04-23 17:04:44 +0000480 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000481 pragma synchronous;
482 }
483} {2}
484do_test pragma-5.1 {
drh1bdd9b52004-04-23 17:04:44 +0000485 catchsql {
486 BEGIN;
danielk197791cf71b2004-06-26 06:37:06 +0000487 pragma synchronous = OFF;
488 }
489} {1 {Safety level may not be changed inside a transaction}}
490do_test pragma-5.2 {
491 execsql {
492 pragma synchronous;
493 }
494} {2}
drh5260f7e2004-06-26 19:35:29 +0000495catchsql {COMMIT;}
danielk1977c7b4a442004-11-23 10:52:51 +0000496} ;# ifcapable pager_pragmas
drh5260f7e2004-06-26 19:35:29 +0000497
498# Test schema-query pragmas
499#
danielk197727188fb2004-11-23 10:13:03 +0000500ifcapable schema_pragmas {
danielk19775a8f9372007-10-09 08:29:32 +0000501ifcapable tempdb&&attach {
danielk197753c0f742005-03-29 03:10:59 +0000502 do_test pragma-6.1 {
503 set res {}
504 execsql {SELECT * FROM sqlite_temp_master}
505 foreach {idx name file} [execsql {pragma database_list}] {
506 lappend res $idx $name
507 }
508 set res
509 } {0 main 1 temp 2 aux}
510}
drh5260f7e2004-06-26 19:35:29 +0000511do_test pragma-6.2 {
512 execsql {
drhd2cb50b2009-01-09 21:41:17 +0000513 CREATE TABLE t2(a,b,c);
drh5260f7e2004-06-26 19:35:29 +0000514 pragma table_info(t2)
515 }
drhbfa8b102006-03-03 21:20:16 +0000516} {0 a {} 0 {} 0 1 b {} 0 {} 0 2 c {} 0 {} 0}
drhd2cb50b2009-01-09 21:41:17 +0000517do_test pragma-6.2.1 {
518 execsql {
519 pragma table_info;
520 }
521} {}
drh736c7d42006-11-30 13:06:37 +0000522db nullvalue <<NULL>>
drh417ec632006-08-14 14:23:41 +0000523do_test pragma-6.2.2 {
524 execsql {
drh736c7d42006-11-30 13:06:37 +0000525 CREATE TABLE t5(
526 a TEXT DEFAULT CURRENT_TIMESTAMP,
527 b DEFAULT (5+3),
528 c TEXT,
529 d INTEGER DEFAULT NULL,
530 e TEXT DEFAULT ''
531 );
drh417ec632006-08-14 14:23:41 +0000532 PRAGMA table_info(t5);
533 }
drh736c7d42006-11-30 13:06:37 +0000534} {0 a TEXT 0 CURRENT_TIMESTAMP 0 1 b {} 0 5+3 0 2 c TEXT 0 <<NULL>> 0 3 d INTEGER 0 NULL 0 4 e TEXT 0 '' 0}
535db nullvalue {}
drh6bf89572004-11-03 16:27:01 +0000536ifcapable {foreignkey} {
drhd2cb50b2009-01-09 21:41:17 +0000537 do_test pragma-6.3.1 {
drh6bf89572004-11-03 16:27:01 +0000538 execsql {
539 CREATE TABLE t3(a int references t2(b), b UNIQUE);
540 pragma foreign_key_list(t3);
541 }
dan1da40a32009-09-19 17:00:31 +0000542 } {0 0 t2 a b {NO ACTION} {NO ACTION} NONE}
drhd2cb50b2009-01-09 21:41:17 +0000543 do_test pragma-6.3.2 {
544 execsql {
545 pragma foreign_key_list;
546 }
547 } {}
548 do_test pragma-6.3.3 {
549 execsql {
550 pragma foreign_key_list(t3_bogus);
551 }
552 } {}
553 do_test pragma-6.3.4 {
554 execsql {
555 pragma foreign_key_list(t5);
556 }
557 } {}
drh6bf89572004-11-03 16:27:01 +0000558 do_test pragma-6.4 {
559 execsql {
560 pragma index_list(t3);
561 }
562 } {0 sqlite_autoindex_t3_1 1}
563}
564ifcapable {!foreignkey} {
565 execsql {CREATE TABLE t3(a,b UNIQUE)}
566}
drhd2cb50b2009-01-09 21:41:17 +0000567do_test pragma-6.5.1 {
drh5260f7e2004-06-26 19:35:29 +0000568 execsql {
569 CREATE INDEX t3i1 ON t3(a,b);
570 pragma index_info(t3i1);
571 }
572} {0 0 a 1 1 b}
drhd2cb50b2009-01-09 21:41:17 +0000573do_test pragma-6.5.2 {
574 execsql {
575 pragma index_info(t3i1_bogus);
576 }
577} {}
danielk1977260d8a62008-08-20 16:34:24 +0000578
579ifcapable tempdb {
580 # Test for ticket #3320. When a temp table of the same name exists, make
581 # sure the schema of the main table can still be queried using
582 # "pragma table_info":
583 do_test pragma-6.6.1 {
584 execsql {
585 CREATE TABLE trial(col_main);
586 CREATE TEMP TABLE trial(col_temp);
587 }
588 } {}
589 do_test pragma-6.6.2 {
590 execsql {
591 PRAGMA table_info(trial);
592 }
593 } {0 col_temp {} 0 {} 0}
594 do_test pragma-6.6.3 {
595 execsql {
596 PRAGMA temp.table_info(trial);
597 }
598 } {0 col_temp {} 0 {} 0}
599 do_test pragma-6.6.4 {
600 execsql {
601 PRAGMA main.table_info(trial);
602 }
603 } {0 col_main {} 0 {} 0}
604}
danielk1977f96a3772008-10-23 05:45:07 +0000605
danielk1977f96a3772008-10-23 05:45:07 +0000606do_test pragma-6.7 {
607 execsql {
608 CREATE TABLE test_table(
609 one INT NOT NULL DEFAULT -1,
610 two text,
611 three VARCHAR(45, 65) DEFAULT 'abcde',
612 four REAL DEFAULT X'abcdef',
613 five DEFAULT CURRENT_TIME
614 );
615 PRAGMA table_info(test_table);
616 }
617} [concat \
618 {0 one INT 1 -1 0} \
619 {1 two text 0 {} 0} \
620 {2 three {VARCHAR(45, 65)} 0 'abcde' 0} \
621 {3 four REAL 0 X'abcdef' 0} \
622 {4 five {} 0 CURRENT_TIME 0} \
623]
danielk197727188fb2004-11-23 10:13:03 +0000624} ;# ifcapable schema_pragmas
drh5260f7e2004-06-26 19:35:29 +0000625# Miscellaneous tests
626#
danielk197727188fb2004-11-23 10:13:03 +0000627ifcapable schema_pragmas {
drhd2cb50b2009-01-09 21:41:17 +0000628do_test pragma-7.1.1 {
drh5260f7e2004-06-26 19:35:29 +0000629 # Make sure a pragma knows to read the schema if it needs to
630 db close
631 sqlite3 db test.db
632 execsql {
633 pragma index_list(t3);
634 }
635} {0 t3i1 0 1 sqlite_autoindex_t3_1 1}
drhd2cb50b2009-01-09 21:41:17 +0000636do_test pragma-7.1.2 {
637 execsql {
638 pragma index_list(t3_bogus);
639 }
640} {}
danielk197727188fb2004-11-23 10:13:03 +0000641} ;# ifcapable schema_pragmas
drh6c626082004-11-14 21:56:29 +0000642ifcapable {utf16} {
dancb354602010-07-08 09:44:42 +0000643 if {[permutation] == ""} {
644 do_test pragma-7.2 {
645 db close
646 sqlite3 db test.db
647 catchsql {
648 pragma encoding=bogus;
649 }
650 } {1 {unsupported encoding: bogus}}
651 }
drh6c626082004-11-14 21:56:29 +0000652}
danielk197753c0f742005-03-29 03:10:59 +0000653ifcapable tempdb {
654 do_test pragma-7.3 {
655 db close
656 sqlite3 db test.db
657 execsql {
658 pragma lock_status;
659 }
660 } {main unlocked temp closed}
661} else {
662 do_test pragma-7.3 {
663 db close
664 sqlite3 db test.db
665 execsql {
666 pragma lock_status;
667 }
668 } {main unlocked}
669}
drh5260f7e2004-06-26 19:35:29 +0000670
671
danielk1977dae24952004-11-11 05:10:43 +0000672#----------------------------------------------------------------------
danielk1977b92b70b2004-11-12 16:11:59 +0000673# Test cases pragma-8.* test the "PRAGMA schema_version" and "PRAGMA
674# user_version" statements.
danielk1977dae24952004-11-11 05:10:43 +0000675#
danielk1977b92b70b2004-11-12 16:11:59 +0000676# pragma-8.1: PRAGMA schema_version
677# pragma-8.2: PRAGMA user_version
danielk1977dae24952004-11-11 05:10:43 +0000678#
679
danielk197711cf9fb2004-11-23 11:16:42 +0000680ifcapable schema_version {
681
danielk1977b92b70b2004-11-12 16:11:59 +0000682# First check that we can set the schema version and then retrieve the
danielk1977dae24952004-11-11 05:10:43 +0000683# same value.
684do_test pragma-8.1.1 {
685 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000686 PRAGMA schema_version = 105;
danielk1977dae24952004-11-11 05:10:43 +0000687 }
688} {}
689do_test pragma-8.1.2 {
drh25403652007-01-04 22:13:41 +0000690 execsql2 {
danielk1977b92b70b2004-11-12 16:11:59 +0000691 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000692 }
drh25403652007-01-04 22:13:41 +0000693} {schema_version 105}
danielk1977dae24952004-11-11 05:10:43 +0000694do_test pragma-8.1.3 {
695 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000696 PRAGMA schema_version = 106;
danielk1977dae24952004-11-11 05:10:43 +0000697 }
698} {}
699do_test pragma-8.1.4 {
700 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000701 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000702 }
703} 106
704
danielk1977b92b70b2004-11-12 16:11:59 +0000705# Check that creating a table modifies the schema-version (this is really
706# to verify that the value being read is in fact the schema version).
danielk1977dae24952004-11-11 05:10:43 +0000707do_test pragma-8.1.5 {
708 execsql {
709 CREATE TABLE t4(a, b, c);
710 INSERT INTO t4 VALUES(1, 2, 3);
711 SELECT * FROM t4;
712 }
713} {1 2 3}
714do_test pragma-8.1.6 {
715 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000716 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000717 }
718} 107
719
720# Now open a second connection to the database. Ensure that changing the
danielk1977b92b70b2004-11-12 16:11:59 +0000721# schema-version using the first connection forces the second connection
danielk1977dae24952004-11-11 05:10:43 +0000722# to reload the schema. This has to be done using the C-API test functions,
723# because the TCL API accounts for SCHEMA_ERROR and retries the query.
724do_test pragma-8.1.7 {
drhdddca282006-01-03 00:33:50 +0000725 sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
danielk1977dae24952004-11-11 05:10:43 +0000726 execsql {
727 SELECT * FROM t4;
728 } db2
729} {1 2 3}
730do_test pragma-8.1.8 {
731 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000732 PRAGMA schema_version = 108;
danielk1977dae24952004-11-11 05:10:43 +0000733 }
734} {}
735do_test pragma-8.1.9 {
736 set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM t4" -1 DUMMY]
737 sqlite3_step $::STMT
738} SQLITE_ERROR
739do_test pragma-8.1.10 {
740 sqlite3_finalize $::STMT
741} SQLITE_SCHEMA
742
danielk1977b92b70b2004-11-12 16:11:59 +0000743# Make sure the schema-version can be manipulated in an attached database.
mistachkinfda06be2011-08-02 00:57:34 +0000744forcedelete test2.db
745forcedelete test2.db-journal
danielk19775a8f9372007-10-09 08:29:32 +0000746ifcapable attach {
747 do_test pragma-8.1.11 {
748 execsql {
749 ATTACH 'test2.db' AS aux;
750 CREATE TABLE aux.t1(a, b, c);
751 PRAGMA aux.schema_version = 205;
752 }
753 } {}
754 do_test pragma-8.1.12 {
755 execsql {
756 PRAGMA aux.schema_version;
757 }
758 } 205
759}
danielk1977dae24952004-11-11 05:10:43 +0000760do_test pragma-8.1.13 {
761 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000762 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000763 }
764} 108
765
danielk1977b92b70b2004-11-12 16:11:59 +0000766# And check that modifying the schema-version in an attached database
danielk1977dae24952004-11-11 05:10:43 +0000767# forces the second connection to reload the schema.
danielk19775a8f9372007-10-09 08:29:32 +0000768ifcapable attach {
769 do_test pragma-8.1.14 {
770 sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
771 execsql {
772 ATTACH 'test2.db' AS aux;
773 SELECT * FROM aux.t1;
774 } db2
775 } {}
776 do_test pragma-8.1.15 {
777 execsql {
778 PRAGMA aux.schema_version = 206;
779 }
780 } {}
781 do_test pragma-8.1.16 {
782 set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM aux.t1" -1 DUMMY]
783 sqlite3_step $::STMT
784 } SQLITE_ERROR
785 do_test pragma-8.1.17 {
786 sqlite3_finalize $::STMT
787 } SQLITE_SCHEMA
788 do_test pragma-8.1.18 {
789 db2 close
790 } {}
791}
danielk1977dae24952004-11-11 05:10:43 +0000792
danielk1977b92b70b2004-11-12 16:11:59 +0000793# Now test that the user-version can be read and written (and that we aren't
794# accidentally manipulating the schema-version instead).
danielk1977dae24952004-11-11 05:10:43 +0000795do_test pragma-8.2.1 {
drh25403652007-01-04 22:13:41 +0000796 execsql2 {
danielk1977b92b70b2004-11-12 16:11:59 +0000797 PRAGMA user_version;
danielk1977dae24952004-11-11 05:10:43 +0000798 }
drh25403652007-01-04 22:13:41 +0000799} {user_version 0}
danielk1977dae24952004-11-11 05:10:43 +0000800do_test pragma-8.2.2 {
801 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000802 PRAGMA user_version = 2;
danielk1977dae24952004-11-11 05:10:43 +0000803 }
804} {}
drh802d69a2005-02-13 23:34:24 +0000805do_test pragma-8.2.3.1 {
drh25403652007-01-04 22:13:41 +0000806 execsql2 {
danielk1977b92b70b2004-11-12 16:11:59 +0000807 PRAGMA user_version;
danielk1977dae24952004-11-11 05:10:43 +0000808 }
drh25403652007-01-04 22:13:41 +0000809} {user_version 2}
drh802d69a2005-02-13 23:34:24 +0000810do_test pragma-8.2.3.2 {
811 db close
812 sqlite3 db test.db
813 execsql {
814 PRAGMA user_version;
815 }
816} {2}
817do_test pragma-8.2.4.1 {
danielk1977dae24952004-11-11 05:10:43 +0000818 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000819 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000820 }
821} {108}
drh802d69a2005-02-13 23:34:24 +0000822ifcapable vacuum {
823 do_test pragma-8.2.4.2 {
824 execsql {
825 VACUUM;
826 PRAGMA user_version;
827 }
828 } {2}
829 do_test pragma-8.2.4.3 {
830 execsql {
831 PRAGMA schema_version;
832 }
833 } {109}
834}
danielk1977dae24952004-11-11 05:10:43 +0000835
danielk19775a8f9372007-10-09 08:29:32 +0000836ifcapable attach {
837 db eval {ATTACH 'test2.db' AS aux}
838
839 # Check that the user-version in the auxilary database can be manipulated (
840 # and that we aren't accidentally manipulating the same in the main db).
841 do_test pragma-8.2.5 {
842 execsql {
843 PRAGMA aux.user_version;
844 }
845 } {0}
846 do_test pragma-8.2.6 {
847 execsql {
848 PRAGMA aux.user_version = 3;
849 }
850 } {}
851 do_test pragma-8.2.7 {
852 execsql {
853 PRAGMA aux.user_version;
854 }
855 } {3}
856 do_test pragma-8.2.8 {
857 execsql {
858 PRAGMA main.user_version;
859 }
860 } {2}
861
862 # Now check that a ROLLBACK resets the user-version if it has been modified
863 # within a transaction.
864 do_test pragma-8.2.9 {
865 execsql {
866 BEGIN;
867 PRAGMA aux.user_version = 10;
868 PRAGMA user_version = 11;
869 }
870 } {}
871 do_test pragma-8.2.10 {
872 execsql {
873 PRAGMA aux.user_version;
874 }
875 } {10}
876 do_test pragma-8.2.11 {
877 execsql {
878 PRAGMA main.user_version;
879 }
880 } {11}
881 do_test pragma-8.2.12 {
882 execsql {
883 ROLLBACK;
884 PRAGMA aux.user_version;
885 }
886 } {3}
887 do_test pragma-8.2.13 {
888 execsql {
889 PRAGMA main.user_version;
890 }
891 } {2}
892}
danielk1977dae24952004-11-11 05:10:43 +0000893
danielk1977b92b70b2004-11-12 16:11:59 +0000894# Try a negative value for the user-version
danielk1977dae24952004-11-11 05:10:43 +0000895do_test pragma-8.2.14 {
896 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000897 PRAGMA user_version = -450;
danielk1977dae24952004-11-11 05:10:43 +0000898 }
899} {}
900do_test pragma-8.2.15 {
901 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000902 PRAGMA user_version;
danielk1977dae24952004-11-11 05:10:43 +0000903 }
904} {-450}
danielk1977d9c847d2005-01-07 10:42:48 +0000905} ; # ifcapable schema_version
906
drhd19744f2008-03-18 13:46:53 +0000907# Check to see if TEMP_STORE is memory or disk. Return strings
908# "memory" or "disk" as appropriate.
909#
910proc check_temp_store {} {
911 db eval {CREATE TEMP TABLE IF NOT EXISTS a(b)}
912 db eval {PRAGMA database_list} {
913 if {$name=="temp"} {
danielk197717b90b52008-06-06 11:11:25 +0000914 set bt [btree_from_db db 1]
915 if {[btree_ismemdb $bt]} {
drhd19744f2008-03-18 13:46:53 +0000916 return "memory"
drhd19744f2008-03-18 13:46:53 +0000917 }
danielk197717b90b52008-06-06 11:11:25 +0000918 return "disk"
drhd19744f2008-03-18 13:46:53 +0000919 }
920 }
921 return "unknown"
922}
923
tpoindex9a09a3c2004-12-20 19:01:32 +0000924
925# Test temp_store and temp_store_directory pragmas
926#
drh268283b2005-01-08 15:44:25 +0000927ifcapable pager_pragmas {
tpoindex9a09a3c2004-12-20 19:01:32 +0000928do_test pragma-9.1 {
929 db close
930 sqlite3 db test.db
931 execsql {
932 PRAGMA temp_store;
933 }
934} {0}
drhd19744f2008-03-18 13:46:53 +0000935if {$TEMP_STORE<=1} {
936 do_test pragma-9.1.1 {
937 check_temp_store
938 } {disk}
939} else {
940 do_test pragma-9.1.1 {
941 check_temp_store
942 } {memory}
943}
944
tpoindex9a09a3c2004-12-20 19:01:32 +0000945do_test pragma-9.2 {
drhd19744f2008-03-18 13:46:53 +0000946 db close
947 sqlite3 db test.db
tpoindex9a09a3c2004-12-20 19:01:32 +0000948 execsql {
949 PRAGMA temp_store=file;
950 PRAGMA temp_store;
951 }
952} {1}
drhd19744f2008-03-18 13:46:53 +0000953if {$TEMP_STORE==3} {
954 # When TEMP_STORE is 3, always use memory regardless of pragma settings.
955 do_test pragma-9.2.1 {
956 check_temp_store
957 } {memory}
958} else {
959 do_test pragma-9.2.1 {
960 check_temp_store
961 } {disk}
962}
963
tpoindex9a09a3c2004-12-20 19:01:32 +0000964do_test pragma-9.3 {
drhd19744f2008-03-18 13:46:53 +0000965 db close
966 sqlite3 db test.db
tpoindex9a09a3c2004-12-20 19:01:32 +0000967 execsql {
968 PRAGMA temp_store=memory;
969 PRAGMA temp_store;
970 }
971} {2}
drhd19744f2008-03-18 13:46:53 +0000972if {$TEMP_STORE==0} {
973 # When TEMP_STORE is 0, always use the disk regardless of pragma settings.
974 do_test pragma-9.3.1 {
975 check_temp_store
976 } {disk}
977} else {
978 do_test pragma-9.3.1 {
979 check_temp_store
980 } {memory}
981}
982
tpoindex9a09a3c2004-12-20 19:01:32 +0000983do_test pragma-9.4 {
984 execsql {
985 PRAGMA temp_store_directory;
986 }
987} {}
drh78f82d12008-09-02 00:52:52 +0000988ifcapable wsd {
989 do_test pragma-9.5 {
danielk1977181ddaa2008-09-16 15:50:11 +0000990 set pwd [string map {' ''} [file nativename [pwd]]]
drh78f82d12008-09-02 00:52:52 +0000991 execsql "
992 PRAGMA temp_store_directory='$pwd';
993 "
994 } {}
995 do_test pragma-9.6 {
996 execsql {
997 PRAGMA temp_store_directory;
998 }
danielk1977181ddaa2008-09-16 15:50:11 +0000999 } [list [file nativename [pwd]]]
drh78f82d12008-09-02 00:52:52 +00001000 do_test pragma-9.7 {
1001 catchsql {
1002 PRAGMA temp_store_directory='/NON/EXISTENT/PATH/FOOBAR';
1003 }
1004 } {1 {not a writable directory}}
1005 do_test pragma-9.8 {
1006 execsql {
1007 PRAGMA temp_store_directory='';
1008 }
1009 } {}
1010 if {![info exists TEMP_STORE] || $TEMP_STORE<=1} {
1011 ifcapable tempdb {
1012 do_test pragma-9.9 {
1013 execsql {
1014 PRAGMA temp_store_directory;
1015 PRAGMA temp_store=FILE;
1016 CREATE TEMP TABLE temp_store_directory_test(a integer);
1017 INSERT INTO temp_store_directory_test values (2);
1018 SELECT * FROM temp_store_directory_test;
1019 }
1020 } {2}
1021 do_test pragma-9.10 {
1022 catchsql "
1023 PRAGMA temp_store_directory='$pwd';
1024 SELECT * FROM temp_store_directory_test;
1025 "
1026 } {1 {no such table: temp_store_directory_test}}
1027 }
tpoindex9a09a3c2004-12-20 19:01:32 +00001028 }
drh78f82d12008-09-02 00:52:52 +00001029}
danielk197795b289b2007-03-30 17:11:12 +00001030do_test pragma-9.11 {
1031 execsql {
1032 PRAGMA temp_store = 0;
1033 PRAGMA temp_store;
1034 }
1035} {0}
1036do_test pragma-9.12 {
1037 execsql {
1038 PRAGMA temp_store = 1;
1039 PRAGMA temp_store;
1040 }
1041} {1}
1042do_test pragma-9.13 {
1043 execsql {
1044 PRAGMA temp_store = 2;
1045 PRAGMA temp_store;
1046 }
1047} {2}
1048do_test pragma-9.14 {
1049 execsql {
1050 PRAGMA temp_store = 3;
1051 PRAGMA temp_store;
1052 }
1053} {0}
danielk197795b289b2007-03-30 17:11:12 +00001054do_test pragma-9.15 {
1055 catchsql {
1056 BEGIN EXCLUSIVE;
1057 CREATE TEMP TABLE temp_table(t);
1058 INSERT INTO temp_table VALUES('valuable data');
1059 PRAGMA temp_store = 1;
1060 }
1061} {1 {temporary storage cannot be changed from within a transaction}}
1062do_test pragma-9.16 {
1063 execsql {
1064 SELECT * FROM temp_table;
1065 COMMIT;
1066 }
1067} {{valuable data}}
danielk1977983e2302008-07-08 07:35:51 +00001068
1069do_test pragma-9.17 {
1070 execsql {
1071 INSERT INTO temp_table VALUES('valuable data II');
1072 SELECT * FROM temp_table;
1073 }
1074} {{valuable data} {valuable data II}}
1075
1076do_test pragma-9.18 {
1077 set rc [catch {
1078 db eval {SELECT t FROM temp_table} {
1079 execsql {pragma temp_store = 1}
1080 }
1081 } msg]
1082 list $rc $msg
1083} {1 {temporary storage cannot be changed from within a transaction}}
1084
drh268283b2005-01-08 15:44:25 +00001085} ;# ifcapable pager_pragmas
tpoindex9a09a3c2004-12-20 19:01:32 +00001086
danielk1977cc6bd382005-01-10 02:48:49 +00001087ifcapable trigger {
1088
1089do_test pragma-10.0 {
1090 catchsql {
1091 DROP TABLE main.t1;
1092 }
1093 execsql {
1094 PRAGMA count_changes = 1;
1095
1096 CREATE TABLE t1(a PRIMARY KEY);
1097 CREATE TABLE t1_mirror(a);
1098 CREATE TABLE t1_mirror2(a);
1099 CREATE TRIGGER t1_bi BEFORE INSERT ON t1 BEGIN
1100 INSERT INTO t1_mirror VALUES(new.a);
1101 END;
1102 CREATE TRIGGER t1_ai AFTER INSERT ON t1 BEGIN
1103 INSERT INTO t1_mirror2 VALUES(new.a);
1104 END;
1105 CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 BEGIN
1106 UPDATE t1_mirror SET a = new.a WHERE a = old.a;
1107 END;
1108 CREATE TRIGGER t1_au AFTER UPDATE ON t1 BEGIN
1109 UPDATE t1_mirror2 SET a = new.a WHERE a = old.a;
1110 END;
1111 CREATE TRIGGER t1_bd BEFORE DELETE ON t1 BEGIN
1112 DELETE FROM t1_mirror WHERE a = old.a;
1113 END;
1114 CREATE TRIGGER t1_ad AFTER DELETE ON t1 BEGIN
1115 DELETE FROM t1_mirror2 WHERE a = old.a;
1116 END;
1117 }
1118} {}
1119
1120do_test pragma-10.1 {
1121 execsql {
1122 INSERT INTO t1 VALUES(randstr(10,10));
1123 }
1124} {1}
1125do_test pragma-10.2 {
1126 execsql {
1127 UPDATE t1 SET a = randstr(10,10);
1128 }
1129} {1}
1130do_test pragma-10.3 {
1131 execsql {
1132 DELETE FROM t1;
1133 }
1134} {1}
1135
1136} ;# ifcapable trigger
1137
danielk197748af65a2005-02-09 03:20:37 +00001138ifcapable schema_pragmas {
1139 do_test pragma-11.1 {
1140 execsql2 {
1141 pragma collation_list;
1142 }
drh9b5adfa2008-01-20 23:19:56 +00001143 } {seq 0 name NOCASE seq 1 name RTRIM seq 2 name BINARY}
danielk197748af65a2005-02-09 03:20:37 +00001144 do_test pragma-11.2 {
1145 db collate New_Collation blah...
1146 execsql {
1147 pragma collation_list;
1148 }
drh9b5adfa2008-01-20 23:19:56 +00001149 } {0 New_Collation 1 NOCASE 2 RTRIM 3 BINARY}
danielk197748af65a2005-02-09 03:20:37 +00001150}
1151
danielk1977ddfb2f02006-02-17 12:25:14 +00001152ifcapable schema_pragmas&&tempdb {
1153 do_test pragma-12.1 {
1154 sqlite3 db2 test.db
1155 execsql {
1156 PRAGMA temp.table_info('abc');
1157 } db2
1158 } {}
1159 db2 close
1160
1161 do_test pragma-12.2 {
1162 sqlite3 db2 test.db
1163 execsql {
1164 PRAGMA temp.default_cache_size = 200;
1165 PRAGMA temp.default_cache_size;
1166 } db2
1167 } {200}
1168 db2 close
1169
1170 do_test pragma-12.3 {
1171 sqlite3 db2 test.db
1172 execsql {
1173 PRAGMA temp.cache_size = 400;
1174 PRAGMA temp.cache_size;
1175 } db2
1176 } {400}
1177 db2 close
1178}
1179
danielk19774b2688a2006-06-20 11:01:07 +00001180ifcapable bloblit {
1181
drh05a82982006-03-19 13:00:25 +00001182do_test pragma-13.1 {
1183 execsql {
1184 DROP TABLE IF EXISTS t4;
1185 PRAGMA vdbe_trace=on;
1186 PRAGMA vdbe_listing=on;
1187 PRAGMA sql_trace=on;
1188 CREATE TABLE t4(a INTEGER PRIMARY KEY,b);
1189 INSERT INTO t4(b) VALUES(x'0123456789abcdef0123456789abcdef0123456789');
1190 INSERT INTO t4(b) VALUES(randstr(30,30));
1191 INSERT INTO t4(b) VALUES(1.23456);
1192 INSERT INTO t4(b) VALUES(NULL);
1193 INSERT INTO t4(b) VALUES(0);
1194 INSERT INTO t4(b) SELECT b||b||b||b FROM t4;
1195 SELECT * FROM t4;
1196 }
1197 execsql {
1198 PRAGMA vdbe_trace=off;
1199 PRAGMA vdbe_listing=off;
1200 PRAGMA sql_trace=off;
1201 }
1202} {}
1203
danielk19774b2688a2006-06-20 11:01:07 +00001204} ;# ifcapable bloblit
1205
danielk197759a93792008-05-15 17:48:20 +00001206ifcapable pager_pragmas {
1207 db close
mistachkinfda06be2011-08-02 00:57:34 +00001208 forcedelete test.db
danielk197759a93792008-05-15 17:48:20 +00001209 sqlite3 db test.db
1210
1211 do_test pragma-14.1 {
1212 execsql { pragma auto_vacuum = 0 }
1213 execsql { pragma page_count }
1214 } {0}
1215
1216 do_test pragma-14.2 {
1217 execsql {
1218 CREATE TABLE abc(a, b, c);
1219 PRAGMA page_count;
1220 }
1221 } {2}
1222
1223 do_test pragma-14.3 {
1224 execsql {
1225 BEGIN;
1226 CREATE TABLE def(a, b, c);
1227 PRAGMA page_count;
1228 }
1229 } {3}
1230
1231 do_test pragma-14.4 {
1232 set page_size [db one {pragma page_size}]
1233 expr [file size test.db] / $page_size
1234 } {2}
1235
1236 do_test pragma-14.5 {
1237 execsql {
1238 ROLLBACK;
1239 PRAGMA page_count;
1240 }
1241 } {2}
1242
1243 do_test pragma-14.6 {
mistachkinfda06be2011-08-02 00:57:34 +00001244 forcedelete test2.db
danielk197759a93792008-05-15 17:48:20 +00001245 sqlite3 db2 test2.db
1246 execsql {
1247 PRAGMA auto_vacuum = 0;
1248 CREATE TABLE t1(a, b, c);
1249 CREATE TABLE t2(a, b, c);
1250 CREATE TABLE t3(a, b, c);
1251 CREATE TABLE t4(a, b, c);
1252 } db2
1253 db2 close
1254 execsql {
1255 ATTACH 'test2.db' AS aux;
1256 PRAGMA aux.page_count;
1257 }
1258 } {5}
1259}
1260
danielk19778cf6c552008-06-23 16:53:46 +00001261# Test that the value set using the cache_size pragma is not reset when the
1262# schema is reloaded.
1263#
1264ifcapable pager_pragmas {
1265 db close
1266 sqlite3 db test.db
1267 do_test pragma-15.1 {
1268 execsql {
1269 PRAGMA cache_size=59;
1270 PRAGMA cache_size;
1271 }
1272 } {59}
1273 do_test pragma-15.2 {
1274 sqlite3 db2 test.db
1275 execsql {
1276 CREATE TABLE newtable(a, b, c);
1277 } db2
1278 db2 close
1279 } {}
1280 do_test pragma-15.3 {
1281 # Evaluating this statement will cause the schema to be reloaded (because
1282 # the schema was changed by another connection in pragma-15.2). At one
1283 # point there was a bug that reset the cache_size to its default value
1284 # when this happened.
1285 execsql { SELECT * FROM sqlite_master }
1286 execsql { PRAGMA cache_size }
1287 } {59}
1288}
1289
danielk19775558a8a2005-01-17 07:53:44 +00001290# Reset the sqlite3_temp_directory variable for the next run of tests:
1291sqlite3 dbX :memory:
1292dbX eval {PRAGMA temp_store_directory = ""}
1293dbX close
1294
danielk1977838cce42009-01-12 14:01:45 +00001295ifcapable lock_proxy_pragmas&&prefer_proxy_locking {
aswiftaebf4132008-11-21 00:10:35 +00001296 set sqlite_hostid_num 1
1297
1298 set using_proxy 0
1299 foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
1300 set using_proxy $value
1301 }
1302
1303 # Test the lock_proxy_file pragmas.
1304 #
1305 db close
1306 set env(SQLITE_FORCE_PROXY_LOCKING) "0"
1307
1308 sqlite3 db test.db
1309 do_test pragma-16.1 {
1310 execsql {
1311 PRAGMA lock_proxy_file="mylittleproxy";
1312 select * from sqlite_master;
1313 }
1314 execsql {
1315 PRAGMA lock_proxy_file;
1316 }
1317 } {mylittleproxy}
1318
1319 do_test pragma-16.2 {
1320 sqlite3 db2 test.db
1321 execsql {
1322 PRAGMA lock_proxy_file="mylittleproxy";
1323 } db2
1324 } {}
1325
1326 db2 close
1327 do_test pragma-16.2.1 {
1328 sqlite3 db2 test.db
1329 execsql {
1330 PRAGMA lock_proxy_file=":auto:";
1331 select * from sqlite_master;
1332 } db2
1333 execsql {
1334 PRAGMA lock_proxy_file;
1335 } db2
1336 } {mylittleproxy}
1337
1338 db2 close
1339 do_test pragma-16.3 {
1340 sqlite3 db2 test.db
1341 execsql {
1342 PRAGMA lock_proxy_file="myotherproxy";
1343 } db2
1344 catchsql {
1345 select * from sqlite_master;
1346 } db2
1347 } {1 {database is locked}}
1348
1349 do_test pragma-16.4 {
1350 db2 close
1351 db close
1352 sqlite3 db2 test.db
1353 execsql {
1354 PRAGMA lock_proxy_file="myoriginalproxy";
1355 PRAGMA lock_proxy_file="myotherproxy";
1356 PRAGMA lock_proxy_file;
1357 } db2
1358 } {myotherproxy}
1359
1360 db2 close
1361 set env(SQLITE_FORCE_PROXY_LOCKING) "1"
1362 do_test pragma-16.5 {
1363 sqlite3 db2 test.db
1364 execsql {
1365 PRAGMA lock_proxy_file=":auto:";
1366 PRAGMA lock_proxy_file;
1367 } db2
1368 } {myotherproxy}
1369
1370 do_test pragma-16.6 {
1371 db2 close
1372 sqlite3 db2 test2.db
1373 set lockpath [execsql {
1374 PRAGMA lock_proxy_file=":auto:";
1375 PRAGMA lock_proxy_file;
1376 } db2]
1377 string match "*test2.db:auto:" $lockpath
1378 } {1}
1379
1380 set sqlite_hostid_num 2
1381 do_test pragma-16.7 {
dan14d14602010-10-06 16:42:52 +00001382 list [catch {
1383 sqlite3 db test2.db
1384 execsql {
1385 PRAGMA lock_proxy_file=":auto:";
1386 select * from sqlite_master;
1387 }
1388 } msg] $msg
aswiftaebf4132008-11-21 00:10:35 +00001389 } {1 {database is locked}}
1390 db close
1391
1392 do_test pragma-16.8 {
dan14d14602010-10-06 16:42:52 +00001393 list [catch {
1394 sqlite3 db test2.db
1395 execsql { select * from sqlite_master }
1396 } msg] $msg
aswiftaebf4132008-11-21 00:10:35 +00001397 } {1 {database is locked}}
1398
1399 db2 close
1400 do_test pragma-16.8.1 {
1401 execsql {
1402 PRAGMA lock_proxy_file="yetanotherproxy";
1403 PRAGMA lock_proxy_file;
1404 }
1405 } {yetanotherproxy}
1406 do_test pragma-16.8.2 {
1407 execsql {
1408 create table mine(x);
1409 }
1410 } {}
1411
1412 db close
1413 do_test pragma-16.9 {
1414 sqlite3 db proxytest.db
1415 set lockpath2 [execsql {
1416 PRAGMA lock_proxy_file=":auto:";
1417 PRAGMA lock_proxy_file;
1418 } db]
1419 string match "*proxytest.db:auto:" $lockpath2
1420 } {1}
1421
1422 set env(SQLITE_FORCE_PROXY_LOCKING) $using_proxy
1423 set sqlite_hostid_num 0
1424}
drhd2cb50b2009-01-09 21:41:17 +00001425
1426# Parsing of auto_vacuum settings.
1427#
1428foreach {autovac_setting val} {
1429 0 0
1430 1 1
1431 2 2
1432 3 0
1433 -1 0
1434 none 0
1435 NONE 0
1436 NoNe 0
1437 full 1
1438 FULL 1
1439 incremental 2
1440 INCREMENTAL 2
1441 -1234 0
1442 1234 0
1443} {
1444 do_test pragma-17.1.$autovac_setting {
1445 catch {db close}
1446 sqlite3 db :memory:
1447 execsql "
1448 PRAGMA auto_vacuum=$::autovac_setting;
1449 PRAGMA auto_vacuum;
1450 "
1451 } $val
1452}
1453
1454# Parsing of temp_store settings.
1455#
1456foreach {temp_setting val} {
1457 0 0
1458 1 1
1459 2 2
1460 3 0
1461 -1 0
1462 file 1
1463 FILE 1
1464 fIlE 1
1465 memory 2
1466 MEMORY 2
1467 MeMoRy 2
1468} {
1469 do_test pragma-18.1.$temp_setting {
1470 catch {db close}
1471 sqlite3 db :memory:
1472 execsql "
1473 PRAGMA temp_store=$::temp_setting;
1474 PRAGMA temp_store=$::temp_setting;
1475 PRAGMA temp_store;
1476 "
1477 } $val
1478}
1479
drhcd61c282002-03-06 22:01:34 +00001480finish_test