3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing the SELECT statement.
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
19 ifcapable !vtab||!compound {
24 # This module uses hard-coded results that depend on exact measurements of
25 # pages sizes at the byte level, and hence will not work if the reserved_bytes
27 if {[nonzero_reserved_bytes]} {finish_test; return;}
30 proc a_string {n} { string range [string repeat [incr ::asc]. $n] 1 $n }
31 db func a_string a_string
33 register_dbstat_vtab db
34 do_execsql_test stat-0.0 {
35 PRAGMA table_info(dbstat);
36 } {/0 name TEXT .* 1 path TEXT .* 9 pgsize INTEGER/}
38 # Attempts to drop an eponymous virtual table are a no-op.
39 do_catchsql_test stat-0.1a {
41 } {1 {table dbstat may not be dropped}}
42 do_execsql_test stat-0.1b {
43 PRAGMA table_info=dbstat;
44 } {/0 name TEXT .* 1 path TEXT .* 9 pgsize INTEGER/}
49 db func a_string a_string
50 register_dbstat_vtab db
51 do_execsql_test stat-0.2 {
52 PRAGMA auto_vacuum = OFF;
53 CREATE VIRTUAL TABLE temp.stat USING dbstat;
58 if {[wal_is_capable]} {
59 do_execsql_test stat-0.1 {
60 PRAGMA journal_mode = WAL;
61 PRAGMA journal_mode = delete;
62 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
64 } {wal delete sqlite_schema / 1 leaf 0 0 916 0}
69 CREATE TABLE t1(a, b);
70 CREATE INDEX i1 ON t1(b);
71 INSERT INTO t1(rowid, a, b) VALUES(2, 2, 3);
72 INSERT INTO t1(rowid, a, b) VALUES(3, 4, 5);
77 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
78 FROM stat WHERE name = 't1';
80 } {t1 / 2 leaf 2 10 998 5}
83 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
84 FROM stat WHERE name = 'i1';
86 } {i1 / 3 leaf 2 10 1000 5}
89 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
90 FROM stat WHERE name = 'sqlite_schema';
92 } {sqlite_schema / 1 leaf 2 77 831 40}
99 do_execsql_test stat-2.1 {
100 CREATE TABLE t3(a PRIMARY KEY, b);
101 INSERT INTO t3(rowid, a, b) VALUES(2, a_string(111), a_string(222));
102 INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3
104 INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3
106 INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3
108 INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3
110 INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3
112 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
113 FROM stat WHERE name != 'sqlite_schema' ORDER BY name;
115 sqlite_autoindex_t3_1 / 3 internal 3 368 623 125 \
116 sqlite_autoindex_t3_1 /000/ 8 leaf 8 946 46 123 \
117 sqlite_autoindex_t3_1 /001/ 9 leaf 8 988 2 131 \
118 sqlite_autoindex_t3_1 /002/ 15 leaf 7 857 137 132 \
119 sqlite_autoindex_t3_1 /003/ 20 leaf 6 739 257 129 \
120 t3 / 2 internal 15 0 907 0 \
121 t3 /000/ 4 leaf 2 678 328 340 \
122 t3 /001/ 5 leaf 2 682 324 342 \
123 t3 /002/ 6 leaf 2 682 324 342 \
124 t3 /003/ 7 leaf 2 690 316 346 \
125 t3 /004/ 10 leaf 2 682 324 342 \
126 t3 /005/ 11 leaf 2 690 316 346 \
127 t3 /006/ 12 leaf 2 698 308 350 \
128 t3 /007/ 13 leaf 2 706 300 354 \
129 t3 /008/ 14 leaf 2 682 324 342 \
130 t3 /009/ 16 leaf 2 690 316 346 \
131 t3 /00a/ 17 leaf 2 698 308 350 \
132 t3 /00b/ 18 leaf 2 706 300 354 \
133 t3 /00c/ 19 leaf 2 714 292 358 \
134 t3 /00d/ 21 leaf 2 722 284 362 \
135 t3 /00e/ 22 leaf 2 730 276 366 \
136 t3 /00f/ 23 leaf 2 738 268 370 \
139 do_execsql_test stat-2.1agg {
140 SELECT * FROM dbstat WHERE aggregate=TRUE ORDER BY name;
142 sqlite_autoindex_t3_1 {} 5 {} 32 3898 1065 132 {} 5120 \
143 sqlite_schema {} 1 {} 2 84 824 49 {} 1024 \
144 t3 {} 17 {} 47 11188 5815 370 {} 17408 \
147 # With every index entry overflowing, make sure no pages are missed
148 # (other than the locking page which is 64 in this test build.)
150 do_execsql_test stat-2.2 {
151 UPDATE t3 SET a=a||hex(randomblob(700));
153 SELECT pageno FROM stat EXCEPT SELECT pageno-1 FROM stat;
156 do_execsql_test stat-2.3 { DROP TABLE t3; VACUUM; } {}
158 do_execsql_test stat-3.1 {
160 CREATE INDEX i4 ON t4(x);
161 INSERT INTO t4(rowid, x) VALUES(2, a_string(7777));
162 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
163 FROM stat WHERE name != 'sqlite_schema' ORDER BY name;
165 i4 / 3 leaf 1 103 905 7782 \
166 i4 /000+000000 4 overflow 0 1020 0 0 \
167 i4 /000+000001 5 overflow 0 1020 0 0 \
168 i4 /000+000002 6 overflow 0 1020 0 0 \
169 i4 /000+000003 7 overflow 0 1020 0 0 \
170 i4 /000+000004 8 overflow 0 1020 0 0 \
171 i4 /000+000005 9 overflow 0 1020 0 0 \
172 i4 /000+000006 10 overflow 0 1020 0 0 \
173 i4 /000+000007 11 overflow 0 539 481 0 \
174 t4 / 2 leaf 1 640 367 7780 \
175 t4 /000+000000 12 overflow 0 1020 0 0 \
176 t4 /000+000001 13 overflow 0 1020 0 0 \
177 t4 /000+000002 14 overflow 0 1020 0 0 \
178 t4 /000+000003 15 overflow 0 1020 0 0 \
179 t4 /000+000004 16 overflow 0 1020 0 0 \
180 t4 /000+000005 17 overflow 0 1020 0 0 \
181 t4 /000+000006 18 overflow 0 1020 0 0 \
184 do_execsql_test stat-3.2 {
185 SELECT *, '|' FROM dbstat WHERE aggregate=TRUE ORDER BY name;
187 i4 {} 9 {} 1 7782 1386 7782 {} 9216 | \
188 sqlite_schema {} 1 {} 2 74 834 40 {} 1024 | \
189 t4 {} 8 {} 1 7780 367 7780 {} 8192 | \
193 do_execsql_test stat-4.1 {
195 CREATE INDEX i5 ON t5(x);
196 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
197 FROM stat WHERE name = 't5' OR name = 'i5';
199 i5 / 20 leaf 0 0 1016 0 \
200 t5 / 19 leaf 0 0 1016 0 \
206 register_dbstat_vtab db
207 do_execsql_test stat-5.1 {
208 PRAGMA auto_vacuum = OFF;
210 ATTACH ':memory:' AS aux1;
211 CREATE VIRTUAL TABLE temp.stat USING dbstat(aux1);
212 CREATE TABLE aux1.t1(x);
213 INSERT INTO t1 VALUES(zeroblob(1513));
214 INSERT INTO t1 VALUES(zeroblob(1514));
215 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
216 FROM stat WHERE name = 't1';
218 t1 / 2 leaf 2 993 5 1517 \
219 t1 /000+000000 3 overflow 0 1020 0 0 \
220 t1 /001+000000 4 overflow 0 1020 0 0 \
223 do_execsql_test stat-5.20 {
224 SELECT name, quote(path), pageno, quote(pagetype), ncell, payload,
225 unused, mx_payload, '|' FROM dbstat('main',1);
226 } {sqlite_schema NULL 1 NULL 1 34 878 34 | tx NULL 1 NULL 0 0 1016 0 |}
227 do_execsql_test stat-5.21 {
228 SELECT name, quote(path), pageno, quote(pagetype), ncell, payload,
229 unused, mx_payload, '|' FROM dbstat('aux1',1);
230 } {sqlite_schema NULL 1 NULL 1 34 878 34 | t1 NULL 3 NULL 2 3033 5 1517 |}
233 do_catchsql_test stat-6.1 {
234 CREATE VIRTUAL TABLE temp.s2 USING dbstat(mainx);
235 } {1 {no such database: mainx}}
237 #-------------------------------------------------------------------------
238 # Test that the argument passed to the dbstat constructor is dequoted
239 # before it is matched against the names of attached databases.
242 do_execsql_test 7.1 {
243 ATTACH 'test.db2' AS '123';
244 PRAGMA "123".auto_vacuum = OFF;
245 CREATE TABLE "123".x1(a, b);
246 INSERT INTO x1 VALUES(1, 2);
249 do_execsql_test 7.1.1 {
250 SELECT * FROM dbstat('123');
252 sqlite_schema / 1 leaf 1 37 875 37 0 1024
253 x1 / 2 leaf 1 4 1008 4 1024 1024
255 do_execsql_test 7.1.2 {
256 SELECT * FROM dbstat(123);
258 sqlite_schema / 1 leaf 1 37 875 37 0 1024
259 x1 / 2 leaf 1 4 1008 4 1024 1024
261 do_execsql_test 7.1.3 {
262 CREATE VIRTUAL TABLE x2 USING dbstat('123');
265 sqlite_schema / 1 leaf 1 37 875 37 0 1024
266 x1 / 2 leaf 1 4 1008 4 1024 1024
268 do_execsql_test 7.1.4 {
269 CREATE VIRTUAL TABLE x3 USING dbstat(123);
272 sqlite_schema / 1 leaf 1 37 875 37 0 1024
273 x1 / 2 leaf 1 4 1008 4 1024 1024
276 do_execsql_test 7.2 {
280 ATTACH 'test.db2' AS '123corp';
282 do_execsql_test 7.2.1 {
283 SELECT * FROM dbstat('123corp');
285 sqlite_schema / 1 leaf 1 37 875 37 0 1024
286 x1 / 2 leaf 1 4 1008 4 1024 1024
288 do_catchsql_test 7.2.2 {
289 SELECT * FROM dbstat(123corp);
290 } {1 {unrecognized token: "123corp"}}
291 do_execsql_test 7.2.3 {
292 CREATE VIRTUAL TABLE x2 USING dbstat('123corp');
295 sqlite_schema / 1 leaf 1 37 875 37 0 1024
296 x1 / 2 leaf 1 4 1008 4 1024 1024
298 do_catchsql_test 7.2.4 {
299 CREATE VIRTUAL TABLE x3 USING dbstat(123corp);
301 } {1 {unrecognized token: "123corp"}}
304 do_execsql_test 8.1 {
305 CREATE VIRTUAL TABLE st4 USING dbstat;
307 do_execsql_test 8.2 {
308 SELECT * FROM st4 WHERE st4.aggregate = NULL;
310 do_execsql_test 8.3 {
311 SELECT aggregate=1 FROM st4 WHERE aggregate = 5
313 do_execsql_test 8.4 {
314 SELECT * FROM st4 WHERE name = NULL;
316 do_execsql_test 8.5 {
317 SELECT * FROM st4 WHERE schema = NULL;
320 #-------------------------------------------------------------------------
323 do_catchsql_test 9.1 {
324 CREATE TABLE dbstat(x, y);
325 DROP TABLE nosuchdb.dbstat;
326 } {/1 {(no such table: nosuchdb.dbstat|table dbstat may not be dropped)}/}