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