xref: /onnv-gate/usr/src/lib/libsqlite/test/pragma.test (revision 4520:7dbeadedd7fe)
1*4520Snw141292
2*4520Snw141292#pragma ident	"%Z%%M%	%I%	%E% SMI"
3*4520Snw141292
4*4520Snw141292# 2002 March 6
5*4520Snw141292#
6*4520Snw141292# The author disclaims copyright to this source code.  In place of
7*4520Snw141292# a legal notice, here is a blessing:
8*4520Snw141292#
9*4520Snw141292#    May you do good and not evil.
10*4520Snw141292#    May you find forgiveness for yourself and forgive others.
11*4520Snw141292#    May you share freely, never taking more than you give.
12*4520Snw141292#
13*4520Snw141292#***********************************************************************
14*4520Snw141292# This file implements regression tests for SQLite library.
15*4520Snw141292#
16*4520Snw141292# This file implements tests for the PRAGMA command.
17*4520Snw141292#
18*4520Snw141292# $Id: pragma.test,v 1.9 2004/04/23 17:04:45 drh Exp $
19*4520Snw141292
20*4520Snw141292set testdir [file dirname $argv0]
21*4520Snw141292source $testdir/tester.tcl
22*4520Snw141292
23*4520Snw141292# Delete the preexisting database to avoid the special setup
24*4520Snw141292# that the "all.test" script does.
25*4520Snw141292#
26*4520Snw141292db close
27*4520Snw141292file delete test.db
28*4520Snw141292set DB [sqlite db test.db]
29*4520Snw141292
30*4520Snw141292do_test pragma-1.1 {
31*4520Snw141292  execsql {
32*4520Snw141292    PRAGMA cache_size;
33*4520Snw141292    PRAGMA default_cache_size;
34*4520Snw141292    PRAGMA synchronous;
35*4520Snw141292    PRAGMA default_synchronous;
36*4520Snw141292  }
37*4520Snw141292} {2000 2000 1 1}
38*4520Snw141292do_test pragma-1.2 {
39*4520Snw141292  execsql {
40*4520Snw141292    PRAGMA cache_size=1234;
41*4520Snw141292    PRAGMA cache_size;
42*4520Snw141292    PRAGMA default_cache_size;
43*4520Snw141292    PRAGMA synchronous;
44*4520Snw141292    PRAGMA default_synchronous;
45*4520Snw141292  }
46*4520Snw141292} {1234 2000 1 1}
47*4520Snw141292do_test pragma-1.3 {
48*4520Snw141292  db close
49*4520Snw141292  sqlite db test.db
50*4520Snw141292  execsql {
51*4520Snw141292    PRAGMA cache_size;
52*4520Snw141292    PRAGMA default_cache_size;
53*4520Snw141292    PRAGMA synchronous;
54*4520Snw141292    PRAGMA default_synchronous;
55*4520Snw141292  }
56*4520Snw141292} {2000 2000 1 1}
57*4520Snw141292do_test pragma-1.4 {
58*4520Snw141292  execsql {
59*4520Snw141292    PRAGMA synchronous=OFF;
60*4520Snw141292    PRAGMA cache_size;
61*4520Snw141292    PRAGMA default_cache_size;
62*4520Snw141292    PRAGMA synchronous;
63*4520Snw141292    PRAGMA default_synchronous;
64*4520Snw141292  }
65*4520Snw141292} {2000 2000 0 1}
66*4520Snw141292do_test pragma-1.5 {
67*4520Snw141292  execsql {
68*4520Snw141292    PRAGMA cache_size=4321;
69*4520Snw141292    PRAGMA cache_size;
70*4520Snw141292    PRAGMA default_cache_size;
71*4520Snw141292    PRAGMA synchronous;
72*4520Snw141292    PRAGMA default_synchronous;
73*4520Snw141292  }
74*4520Snw141292} {4321 2000 0 1}
75*4520Snw141292do_test pragma-1.6 {
76*4520Snw141292  execsql {
77*4520Snw141292    PRAGMA synchronous=ON;
78*4520Snw141292    PRAGMA cache_size;
79*4520Snw141292    PRAGMA default_cache_size;
80*4520Snw141292    PRAGMA synchronous;
81*4520Snw141292    PRAGMA default_synchronous;
82*4520Snw141292  }
83*4520Snw141292} {4321 2000 1 1}
84*4520Snw141292do_test pragma-1.7 {
85*4520Snw141292  db close
86*4520Snw141292  sqlite db test.db
87*4520Snw141292  execsql {
88*4520Snw141292    PRAGMA cache_size;
89*4520Snw141292    PRAGMA default_cache_size;
90*4520Snw141292    PRAGMA synchronous;
91*4520Snw141292    PRAGMA default_synchronous;
92*4520Snw141292  }
93*4520Snw141292} {2000 2000 1 1}
94*4520Snw141292do_test pragma-1.8 {
95*4520Snw141292  execsql {
96*4520Snw141292    PRAGMA default_synchronous=OFF;
97*4520Snw141292    PRAGMA cache_size;
98*4520Snw141292    PRAGMA default_cache_size;
99*4520Snw141292    PRAGMA synchronous;
100*4520Snw141292    PRAGMA default_synchronous;
101*4520Snw141292  }
102*4520Snw141292} {2000 2000 0 0}
103*4520Snw141292do_test pragma-1.9 {
104*4520Snw141292  execsql {
105*4520Snw141292    PRAGMA default_cache_size=123;
106*4520Snw141292    PRAGMA cache_size;
107*4520Snw141292    PRAGMA default_cache_size;
108*4520Snw141292    PRAGMA synchronous;
109*4520Snw141292    PRAGMA default_synchronous;
110*4520Snw141292  }
111*4520Snw141292} {123 123 0 0}
112*4520Snw141292do_test pragma-1.10 {
113*4520Snw141292  db close
114*4520Snw141292  set ::DB [sqlite db test.db]
115*4520Snw141292  execsql {
116*4520Snw141292    PRAGMA cache_size;
117*4520Snw141292    PRAGMA default_cache_size;
118*4520Snw141292    PRAGMA synchronous;
119*4520Snw141292    PRAGMA default_synchronous;
120*4520Snw141292  }
121*4520Snw141292} {123 123 0 0}
122*4520Snw141292do_test pragma-1.11 {
123*4520Snw141292  execsql {
124*4520Snw141292    PRAGMA synchronous=NORMAL;
125*4520Snw141292    PRAGMA cache_size;
126*4520Snw141292    PRAGMA default_cache_size;
127*4520Snw141292    PRAGMA synchronous;
128*4520Snw141292    PRAGMA default_synchronous;
129*4520Snw141292  }
130*4520Snw141292} {123 123 1 0}
131*4520Snw141292do_test pragma-1.12 {
132*4520Snw141292  execsql {
133*4520Snw141292    PRAGMA synchronous=FULL;
134*4520Snw141292    PRAGMA cache_size;
135*4520Snw141292    PRAGMA default_cache_size;
136*4520Snw141292    PRAGMA synchronous;
137*4520Snw141292    PRAGMA default_synchronous;
138*4520Snw141292  }
139*4520Snw141292} {123 123 2 0}
140*4520Snw141292do_test pragma-1.13 {
141*4520Snw141292  db close
142*4520Snw141292  set ::DB [sqlite db test.db]
143*4520Snw141292  execsql {
144*4520Snw141292    PRAGMA cache_size;
145*4520Snw141292    PRAGMA default_cache_size;
146*4520Snw141292    PRAGMA synchronous;
147*4520Snw141292    PRAGMA default_synchronous;
148*4520Snw141292  }
149*4520Snw141292} {123 123 0 0}
150*4520Snw141292do_test pragma-1.14 {
151*4520Snw141292  execsql {
152*4520Snw141292    PRAGMA default_synchronous=FULL;
153*4520Snw141292    PRAGMA cache_size;
154*4520Snw141292    PRAGMA default_cache_size;
155*4520Snw141292    PRAGMA synchronous;
156*4520Snw141292    PRAGMA default_synchronous;
157*4520Snw141292  }
158*4520Snw141292} {123 123 2 2}
159*4520Snw141292do_test pragma-1.15 {
160*4520Snw141292  db close
161*4520Snw141292  set ::DB [sqlite db test.db]
162*4520Snw141292  execsql {
163*4520Snw141292    PRAGMA cache_size;
164*4520Snw141292    PRAGMA default_cache_size;
165*4520Snw141292    PRAGMA synchronous;
166*4520Snw141292    PRAGMA default_synchronous;
167*4520Snw141292  }
168*4520Snw141292} {123 123 2 2}
169*4520Snw141292
170*4520Snw141292do_test pragma-2.1 {
171*4520Snw141292  execsql {
172*4520Snw141292    PRAGMA show_datatypes=on;
173*4520Snw141292    PRAGMA empty_result_callbacks=off;
174*4520Snw141292  }
175*4520Snw141292  sqlite_datatypes $::DB {SELECT * FROM sqlite_master}
176*4520Snw141292} {}
177*4520Snw141292do_test pragma-2.2 {
178*4520Snw141292  execsql {
179*4520Snw141292    PRAGMA empty_result_callbacks=on;
180*4520Snw141292  }
181*4520Snw141292  sqlite_datatypes $::DB {SELECT * FROM sqlite_master}
182*4520Snw141292} {text text text integer text}
183*4520Snw141292
184*4520Snw141292# Make sure we can read the schema when empty_result_callbacks are
185*4520Snw141292# turned on. Ticket #406
186*4520Snw141292do_test pragma-2.2.1 {
187*4520Snw141292  execsql {
188*4520Snw141292    BEGIN;
189*4520Snw141292    CREATE TABLE tabx(a,b,c,d);
190*4520Snw141292    ROLLBACK;
191*4520Snw141292    SELECT count(*) FROM sqlite_master;
192*4520Snw141292  }
193*4520Snw141292} {0}
194*4520Snw141292
195*4520Snw141292do_test pragma-2.3 {
196*4520Snw141292  execsql {
197*4520Snw141292    CREATE TABLE t1(
198*4520Snw141292       a INTEGER,
199*4520Snw141292       b TEXT,
200*4520Snw141292       c WHATEVER,
201*4520Snw141292       d CLOB,
202*4520Snw141292       e BLOB,
203*4520Snw141292       f VARCHAR(123),
204*4520Snw141292       g nVaRcHaR(432)
205*4520Snw141292    );
206*4520Snw141292  }
207*4520Snw141292  sqlite_datatypes $::DB {SELECT * FROM t1}
208*4520Snw141292} {INTEGER TEXT WHATEVER CLOB BLOB VARCHAR(123) nVaRcHaR(432)}
209*4520Snw141292do_test pragma-2.4 {
210*4520Snw141292  sqlite_datatypes $::DB {
211*4520Snw141292     SELECT 1, 'hello', NULL
212*4520Snw141292  }
213*4520Snw141292} {NUMERIC TEXT TEXT}
214*4520Snw141292do_test pragma-2.5 {
215*4520Snw141292  sqlite_datatypes $::DB {
216*4520Snw141292     SELECT 1+2 AS X, 'hello' || 5 AS Y, NULL AS Z
217*4520Snw141292  }
218*4520Snw141292} {NUMERIC TEXT TEXT}
219*4520Snw141292do_test pragma-2.6 {
220*4520Snw141292  execsql {
221*4520Snw141292    CREATE VIEW v1 AS SELECT a+b, b||c, * FROM t1;
222*4520Snw141292  }
223*4520Snw141292  sqlite_datatypes $::DB {SELECT * FROM v1}
224*4520Snw141292} {NUMERIC TEXT INTEGER TEXT WHATEVER CLOB BLOB VARCHAR(123) nVaRcHaR(432)}
225*4520Snw141292do_test pragma-2.7 {
226*4520Snw141292  sqlite_datatypes $::DB {
227*4520Snw141292    SELECT d,e FROM t1 UNION SELECT a,c FROM t1
228*4520Snw141292  }
229*4520Snw141292} {INTEGER WHATEVER}
230*4520Snw141292do_test pragma-2.8 {
231*4520Snw141292  sqlite_datatypes $::DB {
232*4520Snw141292    SELECT d,e FROM t1 EXCEPT SELECT c,e FROM t1
233*4520Snw141292  }
234*4520Snw141292} {WHATEVER BLOB}
235*4520Snw141292do_test pragma-2.9 {
236*4520Snw141292  sqlite_datatypes $::DB {
237*4520Snw141292    SELECT d,e FROM t1 INTERSECT SELECT c,e FROM t1
238*4520Snw141292  }
239*4520Snw141292} {WHATEVER BLOB}
240*4520Snw141292do_test pragma-2.10 {
241*4520Snw141292  sqlite_datatypes $::DB {
242*4520Snw141292    SELECT d,e FROM t1 INTERSECT SELECT c,e FROM v1
243*4520Snw141292  }
244*4520Snw141292} {WHATEVER BLOB}
245*4520Snw141292
246*4520Snw141292# Construct a corrupted index and make sure the integrity_check
247*4520Snw141292# pragma finds it.
248*4520Snw141292#
249*4520Snw141292if {![sqlite -has-codec]} {
250*4520Snw141292do_test pragma-3.1 {
251*4520Snw141292  execsql {
252*4520Snw141292    BEGIN;
253*4520Snw141292    CREATE TABLE t2(a,b,c);
254*4520Snw141292    CREATE INDEX i2 ON t2(a);
255*4520Snw141292    INSERT INTO t2 VALUES(11,2,3);
256*4520Snw141292    INSERT INTO t2 VALUES(22,3,4);
257*4520Snw141292    COMMIT;
258*4520Snw141292    SELECT rowid, * from t2;
259*4520Snw141292  }
260*4520Snw141292} {1 11 2 3 2 22 3 4}
261*4520Snw141292do_test pragma-3.2 {
262*4520Snw141292  set rootpage [execsql {SELECT rootpage FROM sqlite_master WHERE name='i2'}]
263*4520Snw141292  set db [btree_open test.db]
264*4520Snw141292  btree_begin_transaction $db
265*4520Snw141292  set c [btree_cursor $db $rootpage 1]
266*4520Snw141292  btree_first $c
267*4520Snw141292  btree_delete $c
268*4520Snw141292  btree_commit $db
269*4520Snw141292  btree_close $db
270*4520Snw141292  execsql {PRAGMA integrity_check}
271*4520Snw141292} {{rowid 1 missing from index i2} {wrong # of entries in index i2}}
272*4520Snw141292}; # endif has-codec
273*4520Snw141292
274*4520Snw141292# Test the temp_store and default_temp_store pragmas
275*4520Snw141292#
276*4520Snw141292do_test pragma-4.2 {
277*4520Snw141292  execsql {
278*4520Snw141292    PRAGMA temp_store='default';
279*4520Snw141292    PRAGMA temp_store;
280*4520Snw141292  }
281*4520Snw141292} {0}
282*4520Snw141292do_test pragma-4.3 {
283*4520Snw141292  execsql {
284*4520Snw141292    PRAGMA temp_store='file';
285*4520Snw141292    PRAGMA temp_store;
286*4520Snw141292  }
287*4520Snw141292} {1}
288*4520Snw141292do_test pragma-4.4 {
289*4520Snw141292  execsql {
290*4520Snw141292    PRAGMA temp_store='memory';
291*4520Snw141292    PRAGMA temp_store;
292*4520Snw141292  }
293*4520Snw141292} {2}
294*4520Snw141292do_test pragma-4.5 {
295*4520Snw141292  execsql {
296*4520Snw141292    PRAGMA default_temp_store='default';
297*4520Snw141292    PRAGMA default_temp_store;
298*4520Snw141292  }
299*4520Snw141292} {0}
300*4520Snw141292do_test pragma-4.6 {
301*4520Snw141292  execsql {
302*4520Snw141292    PRAGMA temp_store;
303*4520Snw141292  }
304*4520Snw141292} {2}
305*4520Snw141292do_test pragma-4.7 {
306*4520Snw141292  db close
307*4520Snw141292  sqlite db test.db
308*4520Snw141292  execsql {
309*4520Snw141292    PRAGMA temp_store;
310*4520Snw141292  }
311*4520Snw141292} {0}
312*4520Snw141292do_test pragma-4.8 {
313*4520Snw141292  execsql {
314*4520Snw141292    PRAGMA default_temp_store;
315*4520Snw141292  }
316*4520Snw141292} {0}
317*4520Snw141292do_test pragma-4.9 {
318*4520Snw141292  execsql {
319*4520Snw141292    PRAGMA default_temp_store='file';
320*4520Snw141292    PRAGMA default_temp_store;
321*4520Snw141292  }
322*4520Snw141292} {1}
323*4520Snw141292do_test pragma-4.10 {
324*4520Snw141292  execsql {
325*4520Snw141292    PRAGMA temp_store;
326*4520Snw141292  }
327*4520Snw141292} {0}
328*4520Snw141292do_test pragma-4.11 {
329*4520Snw141292  db close
330*4520Snw141292  sqlite db test.db
331*4520Snw141292  execsql {
332*4520Snw141292    PRAGMA temp_store;
333*4520Snw141292  }
334*4520Snw141292} {1}
335*4520Snw141292do_test pragma-4.12 {
336*4520Snw141292  execsql {
337*4520Snw141292    PRAGMA default_temp_store;
338*4520Snw141292  }
339*4520Snw141292} {1}
340*4520Snw141292do_test pragma-4.13 {
341*4520Snw141292  execsql {
342*4520Snw141292    PRAGMA default_temp_store='memory';
343*4520Snw141292    PRAGMA default_temp_store;
344*4520Snw141292  }
345*4520Snw141292} {2}
346*4520Snw141292do_test pragma-4.14 {
347*4520Snw141292  execsql {
348*4520Snw141292    PRAGMA temp_store;
349*4520Snw141292  }
350*4520Snw141292} {1}
351*4520Snw141292do_test pragma-4.15 {
352*4520Snw141292  db close
353*4520Snw141292  sqlite db test.db
354*4520Snw141292  execsql {
355*4520Snw141292    PRAGMA temp_store;
356*4520Snw141292  }
357*4520Snw141292} {2}
358*4520Snw141292do_test pragma-4.16 {
359*4520Snw141292  execsql {
360*4520Snw141292    PRAGMA default_temp_store;
361*4520Snw141292  }
362*4520Snw141292} {2}
363*4520Snw141292do_test pragma-4.17 {
364*4520Snw141292  execsql {
365*4520Snw141292    PRAGMA temp_store='file';
366*4520Snw141292    PRAGMA temp_store
367*4520Snw141292  }
368*4520Snw141292} {1}
369*4520Snw141292do_test pragma-4.18 {
370*4520Snw141292  execsql {
371*4520Snw141292    PRAGMA default_temp_store
372*4520Snw141292  }
373*4520Snw141292} {2}
374*4520Snw141292do_test pragma-4.19 {
375*4520Snw141292  db close
376*4520Snw141292  sqlite db test.db
377*4520Snw141292  execsql {
378*4520Snw141292    PRAGMA temp_store
379*4520Snw141292  }
380*4520Snw141292} {2}
381*4520Snw141292
382*4520Snw141292# Changing the TEMP_STORE deletes any existing temporary tables
383*4520Snw141292#
384*4520Snw141292do_test pragma-4.20 {
385*4520Snw141292  execsql {SELECT name FROM sqlite_temp_master}
386*4520Snw141292} {}
387*4520Snw141292do_test pragma-4.21 {
388*4520Snw141292  execsql {
389*4520Snw141292    CREATE TEMP TABLE test1(a,b,c);
390*4520Snw141292    SELECT name FROM sqlite_temp_master;
391*4520Snw141292  }
392*4520Snw141292} {test1}
393*4520Snw141292do_test pragma-4.22 {
394*4520Snw141292  execsql {
395*4520Snw141292    PRAGMA temp_store='file';
396*4520Snw141292    SELECT name FROM sqlite_temp_master;
397*4520Snw141292  }
398*4520Snw141292} {}
399*4520Snw141292do_test pragma-4.23 {
400*4520Snw141292  execsql {
401*4520Snw141292    CREATE TEMP TABLE test1(a,b,c);
402*4520Snw141292    SELECT name FROM sqlite_temp_master;
403*4520Snw141292  }
404*4520Snw141292} {test1}
405*4520Snw141292do_test pragma-4.24 {
406*4520Snw141292  execsql {
407*4520Snw141292    PRAGMA temp_store='memory';
408*4520Snw141292    SELECT name FROM sqlite_temp_master;
409*4520Snw141292  }
410*4520Snw141292} {}
411*4520Snw141292do_test pragma-4.25 {
412*4520Snw141292  catchsql {
413*4520Snw141292    BEGIN;
414*4520Snw141292    PRAGMA temp_store='default';
415*4520Snw141292    COMMIT;
416*4520Snw141292  }
417*4520Snw141292} {1 {temporary storage cannot be changed from within a transaction}}
418*4520Snw141292catchsql {COMMIT}
419*4520Snw141292
420*4520Snw141292finish_test
421