1# 2003 June 21 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 miscellanous features that were 14# left out of other test files. 15# 16# $Id: misc2.test,v 1.17 2004/11/22 13:35:42 danielk1977 Exp $ 17 18set testdir [file dirname $argv0] 19source $testdir/tester.tcl 20 21ifcapable {trigger} { 22# Test for ticket #360 23# 24do_test misc2-1.1 { 25 catchsql { 26 CREATE TABLE FOO(bar integer); 27 CREATE TRIGGER foo_insert BEFORE INSERT ON foo BEGIN 28 SELECT CASE WHEN (NOT new.bar BETWEEN 0 AND 20) 29 THEN raise(rollback, 'aiieee') END; 30 END; 31 INSERT INTO foo(bar) VALUES (1); 32 } 33} {0 {}} 34do_test misc2-1.2 { 35 catchsql { 36 INSERT INTO foo(bar) VALUES (111); 37 } 38} {1 aiieee} 39} ;# endif trigger 40 41# Make sure ROWID works on a view and a subquery. Ticket #364 42# 43ifcapable view { 44do_test misc2-2.1 { 45 execsql { 46 CREATE TABLE t1(a,b,c); 47 INSERT INTO t1 VALUES(1,2,3); 48 CREATE TABLE t2(a,b,c); 49 INSERT INTO t2 VALUES(7,8,9); 50 SELECT rowid, * FROM (SELECT * FROM t1, t2); 51 } 52} {{} 1 2 3 7 8 9} 53do_test misc2-2.2 { 54 execsql { 55 CREATE VIEW v1 AS SELECT * FROM t1, t2; 56 SELECT rowid, * FROM v1; 57 } 58} {{} 1 2 3 7 8 9} 59} ;# ifcapable view 60 61# Check name binding precedence. Ticket #387 62# 63do_test misc2-3.1 { 64 catchsql { 65 SELECT t1.b+t2.b AS a, t1.a, t2.a FROM t1, t2 WHERE a==10 66 } 67} {1 {ambiguous column name: a}} 68 69# Make sure 32-bit integer overflow is handled properly in queries. 70# ticket #408 71# 72do_test misc2-4.1 { 73 execsql { 74 INSERT INTO t1 VALUES(4000000000,'a','b'); 75 SELECT a FROM t1 WHERE a>1; 76 } 77} {4000000000} 78do_test misc2-4.2 { 79 execsql { 80 INSERT INTO t1 VALUES(2147483648,'b2','c2'); 81 INSERT INTO t1 VALUES(2147483647,'b3','c3'); 82 SELECT a FROM t1 WHERE a>2147483647; 83 } 84} {4000000000 2147483648} 85do_test misc2-4.3 { 86 execsql { 87 SELECT a FROM t1 WHERE a<2147483648; 88 } 89} {1 2147483647} 90do_test misc2-4.4 { 91 execsql { 92 SELECT a FROM t1 WHERE a<=2147483648; 93 } 94} {1 2147483648 2147483647} 95do_test misc2-4.5 { 96 execsql { 97 SELECT a FROM t1 WHERE a<10000000000; 98 } 99} {1 4000000000 2147483648 2147483647} 100do_test misc2-4.6 { 101 execsql { 102 SELECT a FROM t1 WHERE a<1000000000000 ORDER BY 1; 103 } 104} {1 2147483647 2147483648 4000000000} 105 106# There were some issues with expanding a SrcList object using a call 107# to sqliteSrcListAppend() if the SrcList had previously been duplicated 108# using a call to sqliteSrcListDup(). Ticket #416. The following test 109# makes sure the problem has been fixed. 110# 111ifcapable view { 112do_test misc2-5.1 { 113 execsql { 114 CREATE TABLE x(a,b); 115 CREATE VIEW y AS 116 SELECT x1.b AS p, x2.b AS q FROM x AS x1, x AS x2 WHERE x1.a=x2.a; 117 CREATE VIEW z AS 118 SELECT y1.p, y2.p FROM y AS y1, y AS y2 WHERE y1.q=y2.q; 119 SELECT * from z; 120 } 121} {} 122} 123 124# Make sure we can open a database with an empty filename. What this 125# does is store the database in a temporary file that is deleted when 126# the database is closed. Ticket #432. 127# 128do_test misc2-6.1 { 129 db close 130 sqlite3 db {} 131 execsql { 132 CREATE TABLE t1(a,b); 133 INSERT INTO t1 VALUES(1,2); 134 SELECT * FROM t1; 135 } 136} {1 2} 137 138# Make sure we get an error message (not a segfault) on an attempt to 139# update a table from within the callback of a select on that same 140# table. 141# 142do_test misc2-7.1 { 143 db close 144 file delete -force test.db 145 sqlite3 db test.db 146 execsql { 147 CREATE TABLE t1(x); 148 INSERT INTO t1 VALUES(1); 149 } 150 set rc [catch { 151 db eval {SELECT rowid FROM t1} {} { 152 db eval "DELETE FROM t1 WHERE rowid=$rowid" 153 } 154 } msg] 155 lappend rc $msg 156} {1 {database table is locked}} 157do_test misc2-7.2 { 158 set rc [catch { 159 db eval {SELECT rowid FROM t1} {} { 160 db eval "INSERT INTO t1 VALUES(3)" 161 } 162 } msg] 163 lappend rc $msg 164} {1 {database table is locked}} 165do_test misc2-7.3 { 166 db close 167 file delete -force test.db 168 sqlite3 db :memory: 169 execsql { 170 CREATE TABLE t1(x); 171 INSERT INTO t1 VALUES(1); 172 } 173 set rc [catch { 174 db eval {SELECT rowid FROM t1} {} { 175 db eval "DELETE FROM t1 WHERE rowid=$rowid" 176 } 177 } msg] 178 lappend rc $msg 179} {1 {database table is locked}} 180do_test misc2-7.4 { 181 set rc [catch { 182 db eval {SELECT rowid FROM t1} {} { 183 db eval "INSERT INTO t1 VALUES(3)" 184 } 185 } msg] 186 lappend rc $msg 187} {1 {database table is locked}} 188 189# Ticket #453. If the SQL ended with "-", the tokenizer was calling that 190# an incomplete token, which caused problem. The solution was to just call 191# it a minus sign. 192# 193do_test misc2-8.1 { 194 catchsql {-} 195} {1 {near "-": syntax error}} 196 197# Ticket #513. Make sure the VDBE stack does not grow on a 3-way join. 198# 199do_test misc2-9.1 { 200 execsql { 201 BEGIN; 202 CREATE TABLE counts(n INTEGER PRIMARY KEY); 203 INSERT INTO counts VALUES(0); 204 INSERT INTO counts VALUES(1); 205 INSERT INTO counts SELECT n+2 FROM counts; 206 INSERT INTO counts SELECT n+4 FROM counts; 207 INSERT INTO counts SELECT n+8 FROM counts; 208 COMMIT; 209 210 CREATE TEMP TABLE x AS 211 SELECT dim1.n, dim2.n, dim3.n 212 FROM counts AS dim1, counts AS dim2, counts AS dim3 213 WHERE dim1.n<10 AND dim2.n<10 AND dim3.n<10; 214 215 SELECT count(*) FROM x; 216 } 217} {1000} 218do_test misc2-9.2 { 219 execsql { 220 DROP TABLE x; 221 CREATE TEMP TABLE x AS 222 SELECT dim1.n, dim2.n, dim3.n 223 FROM counts AS dim1, counts AS dim2, counts AS dim3 224 WHERE dim1.n>=6 AND dim2.n>=6 AND dim3.n>=6; 225 226 SELECT count(*) FROM x; 227 } 228} {1000} 229do_test misc2-9.3 { 230 execsql { 231 DROP TABLE x; 232 CREATE TEMP TABLE x AS 233 SELECT dim1.n, dim2.n, dim3.n, dim4.n 234 FROM counts AS dim1, counts AS dim2, counts AS dim3, counts AS dim4 235 WHERE dim1.n<5 AND dim2.n<5 AND dim3.n<5 AND dim4.n<5; 236 237 SELECT count(*) FROM x; 238 } 239} [expr 5*5*5*5] 240 241finish_test 242