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