1# 2010 July 09 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. The 12# focus of this file is testing the SELECT statement. 13# 14 15set testdir [file dirname $argv0] 16source $testdir/tester.tcl 17set testprefix stat 18 19ifcapable !vtab||!compound { 20 finish_test 21 return 22} 23 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 26# value is nonzero. 27if {[nonzero_reserved_bytes]} {finish_test; return;} 28 29set ::asc 1 30proc a_string {n} { string range [string repeat [incr ::asc]. $n] 1 $n } 31db func a_string a_string 32 33register_dbstat_vtab db 34do_execsql_test stat-0.0 { 35 PRAGMA table_info(dbstat); 36} {/0 name TEXT .* 1 path TEXT .* 9 pgsize INTEGER/} 37do_execsql_test stat-0.1 { 38 PRAGMA auto_vacuum = OFF; 39 CREATE VIRTUAL TABLE temp.stat USING dbstat; 40 SELECT * FROM stat; 41} {} 42 43if {[wal_is_capable]} { 44 do_execsql_test stat-0.1 { 45 PRAGMA journal_mode = WAL; 46 PRAGMA journal_mode = delete; 47 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload 48 FROM stat; 49 } {wal delete sqlite_master / 1 leaf 0 0 916 0} 50} 51 52do_test stat-1.0 { 53 execsql { 54 CREATE TABLE t1(a, b); 55 CREATE INDEX i1 ON t1(b); 56 INSERT INTO t1(rowid, a, b) VALUES(2, 2, 3); 57 INSERT INTO t1(rowid, a, b) VALUES(3, 4, 5); 58 } 59} {} 60do_test stat-1.1 { 61 execsql { 62 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload 63 FROM stat WHERE name = 't1'; 64 } 65} {t1 / 2 leaf 2 10 998 5} 66do_test stat-1.2 { 67 execsql { 68 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload 69 FROM stat WHERE name = 'i1'; 70 } 71} {i1 / 3 leaf 2 10 1000 5} 72do_test stat-1.3 { 73 execsql { 74 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload 75 FROM stat WHERE name = 'sqlite_master'; 76 } 77} {sqlite_master / 1 leaf 2 77 831 40} 78do_test stat-1.4 { 79 execsql { 80 DROP TABLE t1; 81 } 82} {} 83 84do_execsql_test stat-2.1 { 85 CREATE TABLE t3(a PRIMARY KEY, b); 86 INSERT INTO t3(rowid, a, b) VALUES(2, a_string(111), a_string(222)); 87 INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3 88 ORDER BY rowid; 89 INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3 90 ORDER BY rowid; 91 INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3 92 ORDER BY rowid; 93 INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3 94 ORDER BY rowid; 95 INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3 96 ORDER BY rowid; 97 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload 98 FROM stat WHERE name != 'sqlite_master'; 99} [list \ 100 sqlite_autoindex_t3_1 / 3 internal 3 368 623 125 \ 101 sqlite_autoindex_t3_1 /000/ 8 leaf 8 946 46 123 \ 102 sqlite_autoindex_t3_1 /001/ 9 leaf 8 988 2 131 \ 103 sqlite_autoindex_t3_1 /002/ 15 leaf 7 857 137 132 \ 104 sqlite_autoindex_t3_1 /003/ 20 leaf 6 739 257 129 \ 105 t3 / 2 internal 15 0 907 0 \ 106 t3 /000/ 4 leaf 2 678 328 340 \ 107 t3 /001/ 5 leaf 2 682 324 342 \ 108 t3 /002/ 6 leaf 2 682 324 342 \ 109 t3 /003/ 7 leaf 2 690 316 346 \ 110 t3 /004/ 10 leaf 2 682 324 342 \ 111 t3 /005/ 11 leaf 2 690 316 346 \ 112 t3 /006/ 12 leaf 2 698 308 350 \ 113 t3 /007/ 13 leaf 2 706 300 354 \ 114 t3 /008/ 14 leaf 2 682 324 342 \ 115 t3 /009/ 16 leaf 2 690 316 346 \ 116 t3 /00a/ 17 leaf 2 698 308 350 \ 117 t3 /00b/ 18 leaf 2 706 300 354 \ 118 t3 /00c/ 19 leaf 2 714 292 358 \ 119 t3 /00d/ 21 leaf 2 722 284 362 \ 120 t3 /00e/ 22 leaf 2 730 276 366 \ 121 t3 /00f/ 23 leaf 2 738 268 370 \ 122] 123 124# With every index entry overflowing, make sure no pages are missed 125# (other than the locking page which is 64 in this test build.) 126# 127do_execsql_test stat-2.2 { 128 UPDATE t3 SET a=a||hex(randomblob(700)); 129 VACUUM; 130 SELECT pageno FROM stat EXCEPT SELECT pageno-1 FROM stat; 131} {64 136} 132 133do_execsql_test stat-2.3 { DROP TABLE t3; VACUUM; } {} 134 135do_execsql_test stat-3.1 { 136 CREATE TABLE t4(x); 137 CREATE INDEX i4 ON t4(x); 138 INSERT INTO t4(rowid, x) VALUES(2, a_string(7777)); 139 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload 140 FROM stat WHERE name != 'sqlite_master'; 141} [list \ 142 i4 / 3 leaf 1 103 905 7782 \ 143 i4 /000+000000 4 overflow 0 1020 0 0 \ 144 i4 /000+000001 5 overflow 0 1020 0 0 \ 145 i4 /000+000002 6 overflow 0 1020 0 0 \ 146 i4 /000+000003 7 overflow 0 1020 0 0 \ 147 i4 /000+000004 8 overflow 0 1020 0 0 \ 148 i4 /000+000005 9 overflow 0 1020 0 0 \ 149 i4 /000+000006 10 overflow 0 1020 0 0 \ 150 i4 /000+000007 11 overflow 0 539 481 0 \ 151 t4 / 2 leaf 1 640 367 7780 \ 152 t4 /000+000000 12 overflow 0 1020 0 0 \ 153 t4 /000+000001 13 overflow 0 1020 0 0 \ 154 t4 /000+000002 14 overflow 0 1020 0 0 \ 155 t4 /000+000003 15 overflow 0 1020 0 0 \ 156 t4 /000+000004 16 overflow 0 1020 0 0 \ 157 t4 /000+000005 17 overflow 0 1020 0 0 \ 158 t4 /000+000006 18 overflow 0 1020 0 0 \ 159] 160 161do_execsql_test stat-4.1 { 162 CREATE TABLE t5(x); 163 CREATE INDEX i5 ON t5(x); 164 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload 165 FROM stat WHERE name = 't5' OR name = 'i5'; 166} [list \ 167 i5 / 20 leaf 0 0 1016 0 \ 168 t5 / 19 leaf 0 0 1016 0 \ 169] 170 171db close 172forcedelete test.db 173sqlite3 db test.db 174register_dbstat_vtab db 175do_execsql_test stat-5.1 { 176 PRAGMA auto_vacuum = OFF; 177 CREATE TABLE tx(y); 178 ATTACH ':memory:' AS aux1; 179 CREATE VIRTUAL TABLE temp.stat USING dbstat(aux1); 180 CREATE TABLE aux1.t1(x); 181 INSERT INTO t1 VALUES(zeroblob(1513)); 182 INSERT INTO t1 VALUES(zeroblob(1514)); 183 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload 184 FROM stat WHERE name = 't1'; 185} [list \ 186 t1 / 2 leaf 2 993 5 1517 \ 187 t1 /000+000000 3 overflow 0 1020 0 0 \ 188 t1 /001+000000 4 overflow 0 1020 0 0 \ 189] 190 191do_catchsql_test stat-6.1 { 192 CREATE VIRTUAL TABLE temp.s2 USING dbstat(mainx); 193} {1 {no such database: mainx}} 194 195#------------------------------------------------------------------------- 196# Test that the argument passed to the dbstat constructor is dequoted 197# before it is matched against the names of attached databases. 198# 199forcedelete test.db2 200do_execsql_test 7.1 { 201 ATTACH 'test.db2' AS '123'; 202 PRAGMA "123".auto_vacuum = OFF; 203 CREATE TABLE "123".x1(a, b); 204 INSERT INTO x1 VALUES(1, 2); 205} 206 207do_execsql_test 7.1.1 { 208 SELECT * FROM dbstat('123'); 209} { 210 sqlite_master / 1 leaf 1 37 875 37 0 1024 211 x1 / 2 leaf 1 4 1008 4 1024 1024 212} 213do_execsql_test 7.1.2 { 214 SELECT * FROM dbstat(123); 215} { 216 sqlite_master / 1 leaf 1 37 875 37 0 1024 217 x1 / 2 leaf 1 4 1008 4 1024 1024 218} 219do_execsql_test 7.1.3 { 220 CREATE VIRTUAL TABLE x2 USING dbstat('123'); 221 SELECT * FROM x2; 222} { 223 sqlite_master / 1 leaf 1 37 875 37 0 1024 224 x1 / 2 leaf 1 4 1008 4 1024 1024 225} 226do_execsql_test 7.1.4 { 227 CREATE VIRTUAL TABLE x3 USING dbstat(123); 228 SELECT * FROM x3; 229} { 230 sqlite_master / 1 leaf 1 37 875 37 0 1024 231 x1 / 2 leaf 1 4 1008 4 1024 1024 232} 233 234do_execsql_test 7.2 { 235 DETACH 123; 236 DROP TABLE x2; 237 DROP TABLE x3; 238 ATTACH 'test.db2' AS '123corp'; 239} 240do_execsql_test 7.2.1 { 241 SELECT * FROM dbstat('123corp'); 242} { 243 sqlite_master / 1 leaf 1 37 875 37 0 1024 244 x1 / 2 leaf 1 4 1008 4 1024 1024 245} 246do_catchsql_test 7.2.2 { 247 SELECT * FROM dbstat(123corp); 248} {1 {unrecognized token: "123corp"}} 249do_execsql_test 7.2.3 { 250 CREATE VIRTUAL TABLE x2 USING dbstat('123corp'); 251 SELECT * FROM x2; 252} { 253 sqlite_master / 1 leaf 1 37 875 37 0 1024 254 x1 / 2 leaf 1 4 1008 4 1024 1024 255} 256do_catchsql_test 7.2.4 { 257 CREATE VIRTUAL TABLE x3 USING dbstat(123corp); 258 SELECT * FROM x3; 259} {1 {unrecognized token: "123corp"}} 260 261finish_test 262