drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1 | # 2001 September 15 |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 5 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 9 | # |
| 10 | #*********************************************************************** |
| 11 | # This file implements regression tests for SQLite library. The |
| 12 | # focus of this file is testing the magic ROWID column that is |
| 13 | # found on all tables. |
| 14 | # |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 15 | # $Id: rowid.test,v 1.6 2001/11/08 00:45:22 drh Exp $ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 16 | |
| 17 | set testdir [file dirname $argv0] |
| 18 | source $testdir/tester.tcl |
| 19 | |
| 20 | # Basic ROWID functionality tests. |
| 21 | # |
| 22 | do_test rowid-1.1 { |
| 23 | execsql { |
| 24 | CREATE TABLE t1(x int, y int); |
| 25 | INSERT INTO t1 VALUES(1,2); |
| 26 | INSERT INTO t1 VALUES(3,4); |
| 27 | SELECT x FROM t1 ORDER BY y; |
| 28 | } |
| 29 | } {1 3} |
| 30 | do_test rowid-1.2 { |
| 31 | set r [execsql {SELECT rowid FROM t1 ORDER BY x}] |
| 32 | global x2rowid rowid2x |
| 33 | set x2rowid(1) [lindex $r 0] |
| 34 | set x2rowid(3) [lindex $r 1] |
| 35 | set rowid2x($x2rowid(1)) 1 |
| 36 | set rowid2x($x2rowid(3)) 3 |
| 37 | llength $r |
| 38 | } {2} |
| 39 | do_test rowid-1.3 { |
| 40 | global x2rowid |
| 41 | set sql "SELECT x FROM t1 WHERE rowid==$x2rowid(1)" |
| 42 | execsql $sql |
| 43 | } {1} |
| 44 | do_test rowid-1.4 { |
| 45 | global x2rowid |
| 46 | set sql "SELECT x FROM t1 WHERE rowid==$x2rowid(3)" |
| 47 | execsql $sql |
| 48 | } {3} |
| 49 | do_test rowid-1.5 { |
| 50 | global x2rowid |
| 51 | set sql "SELECT x FROM t1 WHERE oid==$x2rowid(1)" |
| 52 | execsql $sql |
| 53 | } {1} |
| 54 | do_test rowid-1.6 { |
| 55 | global x2rowid |
| 56 | set sql "SELECT x FROM t1 WHERE OID==$x2rowid(3)" |
| 57 | execsql $sql |
| 58 | } {3} |
| 59 | do_test rowid-1.7 { |
| 60 | global x2rowid |
| 61 | set sql "SELECT x FROM t1 WHERE _rowid_==$x2rowid(1)" |
| 62 | execsql $sql |
| 63 | } {1} |
drh | 1eaa269 | 2001-09-18 02:02:23 +0000 | [diff] [blame] | 64 | do_test rowid-1.7.1 { |
| 65 | while 1 { |
| 66 | set norow [expr {int(rand()*1000000)}] |
| 67 | if {$norow!=$x2rowid(1) && $norow!=$x2rowid(3)} break |
| 68 | } |
| 69 | execsql "SELECT x FROM t1 WHERE rowid=$norow" |
| 70 | } {} |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 71 | do_test rowid-1.8 { |
| 72 | global x2rowid |
| 73 | set v [execsql {SELECT x, oid FROM t1 order by x}] |
| 74 | set v2 [list 1 $x2rowid(1) 3 $x2rowid(3)] |
| 75 | expr {$v==$v2} |
| 76 | } {1} |
| 77 | do_test rowid-1.9 { |
| 78 | global x2rowid |
| 79 | set v [execsql {SELECT x, RowID FROM t1 order by x}] |
| 80 | set v2 [list 1 $x2rowid(1) 3 $x2rowid(3)] |
| 81 | expr {$v==$v2} |
| 82 | } {1} |
| 83 | do_test rowid-1.9 { |
| 84 | global x2rowid |
| 85 | set v [execsql {SELECT x, _rowid_ FROM t1 order by x}] |
| 86 | set v2 [list 1 $x2rowid(1) 3 $x2rowid(3)] |
| 87 | expr {$v==$v2} |
| 88 | } {1} |
| 89 | |
| 90 | # We cannot update or insert the ROWID column |
| 91 | # |
| 92 | do_test rowid-2.1 { |
| 93 | set v [catch {execsql {INSERT INTO t1(rowid,x,y) VALUES(1234,5,6)}} msg] |
| 94 | lappend v $msg |
| 95 | } {1 {table t1 has no column named rowid}} |
| 96 | do_test rowid-2.2 { |
| 97 | set v [catch {execsql {UPDATE t1 SET rowid=12345 WHERE x==1}}] |
| 98 | lappend v $msg |
| 99 | } {1 {table t1 has no column named rowid}} |
| 100 | do_test rowid-2.3 { |
| 101 | set v [catch {execsql {INSERT INTO t1(oid,x,y) VALUES(1234,5,6)}} msg] |
| 102 | lappend v $msg |
| 103 | } {1 {table t1 has no column named oid}} |
| 104 | do_test rowid-2.4 { |
| 105 | set v [catch {execsql {UPDATE t1 SET oid=12345 WHERE x==1}}] |
| 106 | lappend v $msg |
| 107 | } {1 {table t1 has no column named oid}} |
| 108 | do_test rowid-2.5 { |
| 109 | set v [catch {execsql {INSERT INTO t1(_rowid_,x,y) VALUES(1234,5,6)}} msg] |
| 110 | lappend v $msg |
| 111 | } {1 {table t1 has no column named _rowid_}} |
| 112 | do_test rowid-2.6 { |
| 113 | set v [catch {execsql {UPDATE t1 SET _rowid_=12345 WHERE x==1}}] |
| 114 | lappend v $msg |
| 115 | } {1 {table t1 has no column named _rowid_}} |
| 116 | |
| 117 | # But we can use ROWID in the WHERE clause of an UPDATE that does not |
| 118 | # change the ROWID. |
| 119 | # |
| 120 | do_test rowid-2.7 { |
| 121 | global x2rowid |
| 122 | set sql "UPDATE t1 SET x=2 WHERE OID==$x2rowid(3)" |
| 123 | execsql $sql |
| 124 | execsql {SELECT x FROM t1 ORDER BY x} |
| 125 | } {1 2} |
| 126 | do_test rowid-2.8 { |
| 127 | global x2rowid |
| 128 | set sql "UPDATE t1 SET x=3 WHERE _rowid_==$x2rowid(3)" |
| 129 | execsql $sql |
| 130 | execsql {SELECT x FROM t1 ORDER BY x} |
| 131 | } {1 3} |
| 132 | |
| 133 | # We cannot index by ROWID |
| 134 | # |
| 135 | do_test rowid-2.9 { |
| 136 | set v [catch {execsql {CREATE INDEX idxt1 ON t1(rowid)}} msg] |
| 137 | lappend v $msg |
| 138 | } {1 {table t1 has no column named rowid}} |
| 139 | do_test rowid-2.10 { |
| 140 | set v [catch {execsql {CREATE INDEX idxt1 ON t1(_rowid_)}} msg] |
| 141 | lappend v $msg |
| 142 | } {1 {table t1 has no column named _rowid_}} |
| 143 | do_test rowid-2.11 { |
| 144 | set v [catch {execsql {CREATE INDEX idxt1 ON t1(oid)}} msg] |
| 145 | lappend v $msg |
| 146 | } {1 {table t1 has no column named oid}} |
| 147 | do_test rowid-2.12 { |
| 148 | set v [catch {execsql {CREATE INDEX idxt1 ON t1(x, rowid)}} msg] |
| 149 | lappend v $msg |
| 150 | } {1 {table t1 has no column named rowid}} |
| 151 | |
| 152 | # Columns defined in the CREATE statement override the buildin ROWID |
| 153 | # column names. |
| 154 | # |
| 155 | do_test rowid-3.1 { |
| 156 | execsql { |
| 157 | CREATE TABLE t2(rowid int, x int, y int); |
| 158 | INSERT INTO t2 VALUES(1,2,3); |
| 159 | INSERT INTO t2 VALUES(4,5,6); |
| 160 | INSERT INTO t2 VALUES(7,8,9); |
| 161 | SELECT * FROM t2 ORDER BY x; |
| 162 | } |
| 163 | } {1 2 3 4 5 6 7 8 9} |
| 164 | do_test rowid-3.2 { |
| 165 | execsql {SELECT * FROM t2 ORDER BY rowid} |
| 166 | } {1 2 3 4 5 6 7 8 9} |
| 167 | do_test rowid-3.3 { |
| 168 | execsql {SELECT rowid, x, y FROM t2 ORDER BY rowid} |
| 169 | } {1 2 3 4 5 6 7 8 9} |
| 170 | do_test rowid-3.4 { |
| 171 | set r1 [execsql {SELECT _rowid_, rowid FROM t2 ORDER BY rowid}] |
| 172 | foreach {a b c d e f} $r1 {} |
| 173 | set r2 [execsql {SELECT _rowid_, rowid FROM t2 ORDER BY x DESC}] |
| 174 | foreach {u v w x y z} $r2 {} |
| 175 | expr {$u==$e && $w==$c && $y==$a} |
| 176 | } {1} |
| 177 | do_probtest rowid-3.5 { |
| 178 | set r1 [execsql {SELECT _rowid_, rowid FROM t2 ORDER BY rowid}] |
| 179 | foreach {a b c d e f} $r1 {} |
| 180 | expr {$a!=$b && $c!=$d && $e!=$f} |
| 181 | } {1} |
| 182 | |
| 183 | # Let's try some more complex examples, including some joins. |
| 184 | # |
| 185 | do_test rowid-4.1 { |
| 186 | execsql { |
| 187 | DELETE FROM t1; |
| 188 | DELETE FROM t2; |
| 189 | } |
| 190 | for {set i 1} {$i<=50} {incr i} { |
| 191 | execsql "INSERT INTO t1(x,y) VALUES($i,[expr {$i*$i}])" |
| 192 | } |
| 193 | execsql {INSERT INTO t2 SELECT _rowid_, x*y, y*y FROM t1} |
| 194 | execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1.rowid==t2.rowid} |
| 195 | } {256} |
| 196 | do_test rowid-4.2 { |
| 197 | execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1.rowid==t2.rowid} |
| 198 | } {256} |
| 199 | do_test rowid-4.2.1 { |
| 200 | execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1.oid==t2.rowid} |
| 201 | } {256} |
| 202 | do_test rowid-4.2.2 { |
| 203 | execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1._rowid_==t2.rowid} |
| 204 | } {256} |
| 205 | do_test rowid-4.2.3 { |
| 206 | execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t2.rowid==t1.rowid} |
| 207 | } {256} |
| 208 | do_test rowid-4.2.4 { |
| 209 | execsql {SELECT t2.y FROM t2, t1 WHERE t2.rowid==t1.oid AND t1.x==4} |
| 210 | } {256} |
| 211 | do_test rowid-4.2.5 { |
| 212 | execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1._rowid_==t2.rowid} |
| 213 | } {256} |
| 214 | do_test rowid-4.2.6 { |
| 215 | execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t2.rowid==t1.rowid} |
| 216 | } {256} |
| 217 | do_test rowid-4.2.7 { |
| 218 | execsql {SELECT t2.y FROM t1, t2 WHERE t2.rowid==t1.oid AND t1.x==4} |
| 219 | } {256} |
| 220 | do_test rowid-4.3 { |
| 221 | execsql {CREATE INDEX idxt1 ON t1(x)} |
| 222 | execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1.rowid==t2.rowid} |
| 223 | } {256} |
| 224 | do_test rowid-4.3.1 { |
| 225 | execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1._rowid_==t2.rowid} |
| 226 | } {256} |
| 227 | do_test rowid-4.3.2 { |
| 228 | execsql {SELECT t2.y FROM t1, t2 WHERE t2.rowid==t1.oid AND 4==t1.x} |
| 229 | } {256} |
| 230 | do_test rowid-4.4 { |
| 231 | execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1.rowid==t2.rowid} |
| 232 | } {256} |
| 233 | do_test rowid-4.4.1 { |
| 234 | execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1._rowid_==t2.rowid} |
| 235 | } {256} |
| 236 | do_test rowid-4.4.2 { |
| 237 | execsql {SELECT t2.y FROM t2, t1 WHERE t2.rowid==t1.oid AND 4==t1.x} |
| 238 | } {256} |
| 239 | do_test rowid-4.5 { |
| 240 | execsql {CREATE INDEX idxt2 ON t2(y)} |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 241 | set sqlite_search_count 0 |
| 242 | concat [execsql { |
| 243 | SELECT t1.x FROM t2, t1 |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 244 | WHERE t2.y==256 AND t1.rowid==t2.rowid |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 245 | }] $sqlite_search_count |
| 246 | } {4 3} |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 247 | do_test rowid-4.5.1 { |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 248 | set sqlite_search_count 0 |
| 249 | concat [execsql { |
| 250 | SELECT t1.x FROM t2, t1 |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 251 | WHERE t1.OID==t2.rowid AND t2.y==81 |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 252 | }] $sqlite_search_count |
| 253 | } {3 3} |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 254 | do_test rowid-4.6 { |
| 255 | execsql { |
| 256 | SELECT t1.x FROM t1, t2 |
| 257 | WHERE t2.y==256 AND t1.rowid==t2.rowid |
| 258 | } |
| 259 | } {4} |
| 260 | |
| 261 | do_test rowid-5.1 { |
| 262 | execsql {DELETE FROM t1 WHERE _rowid_ IN (SELECT oid FROM t1 WHERE x>8)} |
| 263 | execsql {SELECT max(x) FROM t1} |
| 264 | } {8} |
drh | 3543b3e | 2001-09-15 00:57:59 +0000 | [diff] [blame] | 265 | |
| 266 | finish_test |