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.25 2006/08/16 16:42:48 drh 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# 43do_test misc2-2.1 { 44 execsql { 45 CREATE TABLE t1(a,b,c); 46 INSERT INTO t1 VALUES(1,2,3); 47 CREATE TABLE t2(a,b,c); 48 INSERT INTO t2 VALUES(7,8,9); 49 } 50} {} 51ifcapable view { 52 ifcapable subquery { 53 do_test misc2-2.2 { 54 execsql { 55 SELECT rowid, * FROM (SELECT * FROM t1, t2); 56 } 57 } {{} 1 2 3 7 8 9} 58 } 59 do_test misc2-2.3 { 60 execsql { 61 CREATE VIEW v1 AS SELECT * FROM t1, t2; 62 SELECT rowid, * FROM v1; 63 } 64 } {{} 1 2 3 7 8 9} 65} ;# ifcapable view 66 67# Check name binding precedence. Ticket #387 68# 69do_test misc2-3.1 { 70 catchsql { 71 SELECT t1.b+t2.b AS a, t1.a, t2.a FROM t1, t2 WHERE a==10 72 } 73} {1 {ambiguous column name: a}} 74 75# Make sure 32-bit integer overflow is handled properly in queries. 76# ticket #408 77# 78do_test misc2-4.1 { 79 execsql { 80 INSERT INTO t1 VALUES(4000000000,'a','b'); 81 SELECT a FROM t1 WHERE a>1; 82 } 83} {4000000000} 84do_test misc2-4.2 { 85 execsql { 86 INSERT INTO t1 VALUES(2147483648,'b2','c2'); 87 INSERT INTO t1 VALUES(2147483647,'b3','c3'); 88 SELECT a FROM t1 WHERE a>2147483647; 89 } 90} {4000000000 2147483648} 91do_test misc2-4.3 { 92 execsql { 93 SELECT a FROM t1 WHERE a<2147483648; 94 } 95} {1 2147483647} 96do_test misc2-4.4 { 97 execsql { 98 SELECT a FROM t1 WHERE a<=2147483648; 99 } 100} {1 2147483648 2147483647} 101do_test misc2-4.5 { 102 execsql { 103 SELECT a FROM t1 WHERE a<10000000000; 104 } 105} {1 4000000000 2147483648 2147483647} 106do_test misc2-4.6 { 107 execsql { 108 SELECT a FROM t1 WHERE a<1000000000000 ORDER BY 1; 109 } 110} {1 2147483647 2147483648 4000000000} 111 112# There were some issues with expanding a SrcList object using a call 113# to sqliteSrcListAppend() if the SrcList had previously been duplicated 114# using a call to sqliteSrcListDup(). Ticket #416. The following test 115# makes sure the problem has been fixed. 116# 117ifcapable view { 118do_test misc2-5.1 { 119 execsql { 120 CREATE TABLE x(a,b); 121 CREATE VIEW y AS 122 SELECT x1.b AS p, x2.b AS q FROM x AS x1, x AS x2 WHERE x1.a=x2.a; 123 CREATE VIEW z AS 124 SELECT y1.p, y2.p FROM y AS y1, y AS y2 WHERE y1.q=y2.q; 125 SELECT * from z; 126 } 127} {} 128} 129 130# Make sure we can open a database with an empty filename. What this 131# does is store the database in a temporary file that is deleted when 132# the database is closed. Ticket #432. 133# 134do_test misc2-6.1 { 135 db close 136 sqlite3 db {} 137 execsql { 138 CREATE TABLE t1(a,b); 139 INSERT INTO t1 VALUES(1,2); 140 SELECT * FROM t1; 141 } 142} {1 2} 143 144# Make sure we get an error message (not a segfault) on an attempt to 145# update a table from within the callback of a select on that same 146# table. 147# 148# 2006-08-16: This has changed. It is now permitted to update 149# the table being SELECTed from within the callback of the query. 150# 151do_test misc2-7.1 { 152 db close 153 file delete -force test.db 154 sqlite3 db test.db 155 execsql { 156 CREATE TABLE t1(x); 157 INSERT INTO t1 VALUES(1); 158 INSERT INTO t1 VALUES(2); 159 INSERT INTO t1 VALUES(3); 160 SELECT * FROM t1; 161 } 162} {1 2 3} 163do_test misc2-7.2 { 164 set rc [catch { 165 db eval {SELECT rowid FROM t1} {} { 166 db eval "DELETE FROM t1 WHERE rowid=$rowid" 167 } 168 } msg] 169 lappend rc $msg 170} {0 {}} 171do_test misc2-7.3 { 172 execsql {SELECT * FROM t1} 173} {} 174do_test misc2-7.4 { 175 execsql { 176 DELETE FROM t1; 177 INSERT INTO t1 VALUES(1); 178 INSERT INTO t1 VALUES(2); 179 INSERT INTO t1 VALUES(3); 180 INSERT INTO t1 VALUES(4); 181 } 182 db eval {SELECT rowid, x FROM t1} { 183 if {$x & 1} { 184 db eval {DELETE FROM t1 WHERE rowid=$rowid} 185 } 186 } 187 execsql {SELECT * FROM t1} 188} {2 4} 189do_test misc2-7.5 { 190 execsql { 191 DELETE FROM t1; 192 INSERT INTO t1 VALUES(1); 193 INSERT INTO t1 VALUES(2); 194 INSERT INTO t1 VALUES(3); 195 INSERT INTO t1 VALUES(4); 196 } 197 db eval {SELECT rowid, x FROM t1} { 198 if {$x & 1} { 199 db eval {DELETE FROM t1 WHERE rowid=$rowid+1} 200 } 201 } 202 execsql {SELECT * FROM t1} 203} {1 3} 204do_test misc2-7.6 { 205 execsql { 206 DELETE FROM t1; 207 INSERT INTO t1 VALUES(1); 208 INSERT INTO t1 VALUES(2); 209 INSERT INTO t1 VALUES(3); 210 INSERT INTO t1 VALUES(4); 211 } 212 db eval {SELECT rowid, x FROM t1} { 213 if {$x & 1} { 214 db eval {DELETE FROM t1} 215 } 216 } 217 execsql {SELECT * FROM t1} 218} {} 219do_test misc2-7.7 { 220 execsql { 221 DELETE FROM t1; 222 INSERT INTO t1 VALUES(1); 223 INSERT INTO t1 VALUES(2); 224 INSERT INTO t1 VALUES(3); 225 INSERT INTO t1 VALUES(4); 226 } 227 db eval {SELECT rowid, x FROM t1} { 228 if {$x & 1} { 229 db eval {UPDATE t1 SET x=x+100 WHERE rowid=$rowid} 230 } 231 } 232 execsql {SELECT * FROM t1} 233} {101 2 103 4} 234do_test misc2-7.8 { 235 execsql { 236 DELETE FROM t1; 237 INSERT INTO t1 VALUES(1); 238 } 239 db eval {SELECT rowid, x FROM t1} { 240 if {$x<10} { 241 db eval {INSERT INTO t1 VALUES($x+1)} 242 } 243 } 244 execsql {SELECT * FROM t1} 245} {1 2 3 4 5 6 7 8 9 10} 246 247db close 248file delete -force test.db 249sqlite3 db test.db 250 251# Ticket #453. If the SQL ended with "-", the tokenizer was calling that 252# an incomplete token, which caused problem. The solution was to just call 253# it a minus sign. 254# 255do_test misc2-8.1 { 256 catchsql {-} 257} {1 {near "-": syntax error}} 258 259# Ticket #513. Make sure the VDBE stack does not grow on a 3-way join. 260# 261ifcapable tempdb { 262 do_test misc2-9.1 { 263 execsql { 264 BEGIN; 265 CREATE TABLE counts(n INTEGER PRIMARY KEY); 266 INSERT INTO counts VALUES(0); 267 INSERT INTO counts VALUES(1); 268 INSERT INTO counts SELECT n+2 FROM counts; 269 INSERT INTO counts SELECT n+4 FROM counts; 270 INSERT INTO counts SELECT n+8 FROM counts; 271 COMMIT; 272 273 CREATE TEMP TABLE x AS 274 SELECT dim1.n, dim2.n, dim3.n 275 FROM counts AS dim1, counts AS dim2, counts AS dim3 276 WHERE dim1.n<10 AND dim2.n<10 AND dim3.n<10; 277 278 SELECT count(*) FROM x; 279 } 280 } {1000} 281 do_test misc2-9.2 { 282 execsql { 283 DROP TABLE x; 284 CREATE TEMP TABLE x AS 285 SELECT dim1.n, dim2.n, dim3.n 286 FROM counts AS dim1, counts AS dim2, counts AS dim3 287 WHERE dim1.n>=6 AND dim2.n>=6 AND dim3.n>=6; 288 289 SELECT count(*) FROM x; 290 } 291 } {1000} 292 do_test misc2-9.3 { 293 execsql { 294 DROP TABLE x; 295 CREATE TEMP TABLE x AS 296 SELECT dim1.n, dim2.n, dim3.n, dim4.n 297 FROM counts AS dim1, counts AS dim2, counts AS dim3, counts AS dim4 298 WHERE dim1.n<5 AND dim2.n<5 AND dim3.n<5 AND dim4.n<5; 299 300 SELECT count(*) FROM x; 301 } 302 } [expr 5*5*5*5] 303} 304 305# Ticket #1229. Sometimes when a "NEW.X" appears in a SELECT without 306# a FROM clause deep within a trigger, the code generator is unable to 307# trace the NEW.X back to an original table and thus figure out its 308# declared datatype. 309# 310# The SQL code below was causing a segfault. 311# 312ifcapable subquery&&trigger { 313 do_test misc2-10.1 { 314 execsql { 315 CREATE TABLE t1229(x); 316 CREATE TRIGGER r1229 BEFORE INSERT ON t1229 BEGIN 317 INSERT INTO t1229 SELECT y FROM (SELECT new.x y); 318 END; 319 INSERT INTO t1229 VALUES(1); 320 } 321 } {} 322} 323 324finish_test 325