sl@0: # 2001 September 15 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. sl@0: # sl@0: # This file implements tests for the special processing associated sl@0: # with INTEGER PRIMARY KEY columns. sl@0: # sl@0: # $Id: intpkey.test,v 1.24 2007/11/29 17:43:28 danielk1977 Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: # Create a table with a primary key and a datatype other than sl@0: # integer sl@0: # sl@0: do_test intpkey-1.0 { sl@0: execsql { sl@0: CREATE TABLE t1(a TEXT PRIMARY KEY, b, c); sl@0: } sl@0: } {} sl@0: sl@0: # There should be an index associated with the primary key sl@0: # sl@0: do_test intpkey-1.1 { sl@0: execsql { sl@0: SELECT name FROM sqlite_master sl@0: WHERE type='index' AND tbl_name='t1'; sl@0: } sl@0: } {sqlite_autoindex_t1_1} sl@0: sl@0: # Now create a table with an integer primary key and verify that sl@0: # there is no associated index. sl@0: # sl@0: do_test intpkey-1.2 { sl@0: execsql { sl@0: DROP TABLE t1; sl@0: CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c); sl@0: SELECT name FROM sqlite_master sl@0: WHERE type='index' AND tbl_name='t1'; sl@0: } sl@0: } {} sl@0: sl@0: # Insert some records into the new table. Specify the primary key sl@0: # and verify that the key is used as the record number. sl@0: # sl@0: do_test intpkey-1.3 { sl@0: execsql { sl@0: INSERT INTO t1 VALUES(5,'hello','world'); sl@0: } sl@0: db last_insert_rowid sl@0: } {5} sl@0: do_test intpkey-1.4 { sl@0: execsql { sl@0: SELECT * FROM t1; sl@0: } sl@0: } {5 hello world} sl@0: do_test intpkey-1.5 { sl@0: execsql { sl@0: SELECT rowid, * FROM t1; sl@0: } sl@0: } {5 5 hello world} sl@0: sl@0: # Attempting to insert a duplicate primary key should give a constraint sl@0: # failure. sl@0: # sl@0: do_test intpkey-1.6 { sl@0: set r [catch {execsql { sl@0: INSERT INTO t1 VALUES(5,'second','entry'); sl@0: }} msg] sl@0: lappend r $msg sl@0: } {1 {PRIMARY KEY must be unique}} sl@0: do_test intpkey-1.7 { sl@0: execsql { sl@0: SELECT rowid, * FROM t1; sl@0: } sl@0: } {5 5 hello world} sl@0: do_test intpkey-1.8 { sl@0: set r [catch {execsql { sl@0: INSERT INTO t1 VALUES(6,'second','entry'); sl@0: }} msg] sl@0: lappend r $msg sl@0: } {0 {}} sl@0: do_test intpkey-1.8.1 { sl@0: db last_insert_rowid sl@0: } {6} sl@0: do_test intpkey-1.9 { sl@0: execsql { sl@0: SELECT rowid, * FROM t1; sl@0: } sl@0: } {5 5 hello world 6 6 second entry} sl@0: sl@0: # A ROWID is automatically generated for new records that do not specify sl@0: # the integer primary key. sl@0: # sl@0: do_test intpkey-1.10 { sl@0: execsql { sl@0: INSERT INTO t1(b,c) VALUES('one','two'); sl@0: SELECT b FROM t1 ORDER BY b; sl@0: } sl@0: } {hello one second} sl@0: sl@0: # Try to change the ROWID for the new entry. sl@0: # sl@0: do_test intpkey-1.11 { sl@0: execsql { sl@0: UPDATE t1 SET a=4 WHERE b='one'; sl@0: SELECT * FROM t1; sl@0: } sl@0: } {4 one two 5 hello world 6 second entry} sl@0: sl@0: # Make sure SELECT statements are able to use the primary key column sl@0: # as an index. sl@0: # sl@0: do_test intpkey-1.12.1 { sl@0: execsql { sl@0: SELECT * FROM t1 WHERE a==4; sl@0: } sl@0: } {4 one two} sl@0: do_test intpkey-1.12.2 { sl@0: set sqlite_query_plan sl@0: } {t1 *} sl@0: sl@0: # Try to insert a non-integer value into the primary key field. This sl@0: # should result in a data type mismatch. sl@0: # sl@0: do_test intpkey-1.13.1 { sl@0: set r [catch {execsql { sl@0: INSERT INTO t1 VALUES('x','y','z'); sl@0: }} msg] sl@0: lappend r $msg sl@0: } {1 {datatype mismatch}} sl@0: do_test intpkey-1.13.2 { sl@0: set r [catch {execsql { sl@0: INSERT INTO t1 VALUES('','y','z'); sl@0: }} msg] sl@0: lappend r $msg sl@0: } {1 {datatype mismatch}} sl@0: do_test intpkey-1.14 { sl@0: set r [catch {execsql { sl@0: INSERT INTO t1 VALUES(3.4,'y','z'); sl@0: }} msg] sl@0: lappend r $msg sl@0: } {1 {datatype mismatch}} sl@0: do_test intpkey-1.15 { sl@0: set r [catch {execsql { sl@0: INSERT INTO t1 VALUES(-3,'y','z'); sl@0: }} msg] sl@0: lappend r $msg sl@0: } {0 {}} sl@0: do_test intpkey-1.16 { sl@0: execsql {SELECT * FROM t1} sl@0: } {-3 y z 4 one two 5 hello world 6 second entry} sl@0: sl@0: #### INDICES sl@0: # Check to make sure indices work correctly with integer primary keys sl@0: # sl@0: do_test intpkey-2.1 { sl@0: execsql { sl@0: CREATE INDEX i1 ON t1(b); sl@0: SELECT * FROM t1 WHERE b=='y' sl@0: } sl@0: } {-3 y z} sl@0: do_test intpkey-2.1.1 { sl@0: execsql { sl@0: SELECT * FROM t1 WHERE b=='y' AND rowid<0 sl@0: } sl@0: } {-3 y z} sl@0: do_test intpkey-2.1.2 { sl@0: execsql { sl@0: SELECT * FROM t1 WHERE b=='y' AND rowid<0 AND rowid>=-20 sl@0: } sl@0: } {-3 y z} sl@0: do_test intpkey-2.1.3 { sl@0: execsql { sl@0: SELECT * FROM t1 WHERE b>='y' sl@0: } sl@0: } {-3 y z} sl@0: do_test intpkey-2.1.4 { sl@0: execsql { sl@0: SELECT * FROM t1 WHERE b>='y' AND rowid<10 sl@0: } sl@0: } {-3 y z} sl@0: sl@0: do_test intpkey-2.2 { sl@0: execsql { sl@0: UPDATE t1 SET a=8 WHERE b=='y'; sl@0: SELECT * FROM t1 WHERE b=='y'; sl@0: } sl@0: } {8 y z} sl@0: do_test intpkey-2.3 { sl@0: execsql { sl@0: SELECT rowid, * FROM t1; sl@0: } sl@0: } {4 4 one two 5 5 hello world 6 6 second entry 8 8 y z} sl@0: do_test intpkey-2.4 { sl@0: execsql { sl@0: SELECT rowid, * FROM t1 WHERE b<'second' sl@0: } sl@0: } {5 5 hello world 4 4 one two} sl@0: do_test intpkey-2.4.1 { sl@0: execsql { sl@0: SELECT rowid, * FROM t1 WHERE 'second'>b sl@0: } sl@0: } {5 5 hello world 4 4 one two} sl@0: do_test intpkey-2.4.2 { sl@0: execsql { sl@0: SELECT rowid, * FROM t1 WHERE 8>rowid AND 'second'>b sl@0: } sl@0: } {4 4 one two 5 5 hello world} sl@0: do_test intpkey-2.4.3 { sl@0: execsql { sl@0: SELECT rowid, * FROM t1 WHERE 8>rowid AND 'second'>b AND 0'a' sl@0: } sl@0: } {5 5 hello world 4 4 one two 6 6 second entry 8 8 y z} sl@0: do_test intpkey-2.6 { sl@0: execsql { sl@0: DELETE FROM t1 WHERE rowid=4; sl@0: SELECT * FROM t1 WHERE b>'a'; sl@0: } sl@0: } {5 hello world 6 second entry 8 y z} sl@0: do_test intpkey-2.7 { sl@0: execsql { sl@0: UPDATE t1 SET a=-4 WHERE rowid=8; sl@0: SELECT * FROM t1 WHERE b>'a'; sl@0: } sl@0: } {5 hello world 6 second entry -4 y z} sl@0: do_test intpkey-2.7 { sl@0: execsql { sl@0: SELECT * FROM t1 sl@0: } sl@0: } {-4 y z 5 hello world 6 second entry} sl@0: sl@0: # Do an SQL statement. Append the search count to the end of the result. sl@0: # sl@0: proc count sql { sl@0: set ::sqlite_search_count 0 sl@0: return [concat [execsql $sql] $::sqlite_search_count] sl@0: } sl@0: sl@0: # Create indices that include the integer primary key as one of their sl@0: # columns. sl@0: # sl@0: do_test intpkey-3.1 { sl@0: execsql { sl@0: CREATE INDEX i2 ON t1(a); sl@0: } sl@0: } {} sl@0: do_test intpkey-3.2 { sl@0: count { sl@0: SELECT * FROM t1 WHERE a=5; sl@0: } sl@0: } {5 hello world 0} sl@0: do_test intpkey-3.3 { sl@0: count { sl@0: SELECT * FROM t1 WHERE a>4 AND a<6; sl@0: } sl@0: } {5 hello world 2} sl@0: do_test intpkey-3.4 { sl@0: count { sl@0: SELECT * FROM t1 WHERE b>='hello' AND b<'hello2'; sl@0: } sl@0: } {5 hello world 3} sl@0: do_test intpkey-3.5 { sl@0: execsql { sl@0: CREATE INDEX i3 ON t1(c,a); sl@0: } sl@0: } {} sl@0: do_test intpkey-3.6 { sl@0: count { sl@0: SELECT * FROM t1 WHERE c=='world'; sl@0: } sl@0: } {5 hello world 3} sl@0: do_test intpkey-3.7 { sl@0: execsql {INSERT INTO t1 VALUES(11,'hello','world')} sl@0: count { sl@0: SELECT * FROM t1 WHERE c=='world'; sl@0: } sl@0: } {5 hello world 11 hello world 5} sl@0: do_test intpkey-3.8 { sl@0: count { sl@0: SELECT * FROM t1 WHERE c=='world' AND a>7; sl@0: } sl@0: } {11 hello world 4} sl@0: do_test intpkey-3.9 { sl@0: count { sl@0: SELECT * FROM t1 WHERE 7=oid; sl@0: } sl@0: } {11 hello world 1} sl@0: do_test intpkey-4.9 { sl@0: count { sl@0: SELECT * FROM t1 WHERE 11<=_rowid_ AND 12>=a; sl@0: } sl@0: } {11 hello world 1} sl@0: do_test intpkey-4.10 { sl@0: count { sl@0: SELECT * FROM t1 WHERE 0>=_rowid_; sl@0: } sl@0: } {-4 y z 1} sl@0: do_test intpkey-4.11 { sl@0: count { sl@0: SELECT * FROM t1 WHERE a<0; sl@0: } sl@0: } {-4 y z 1} sl@0: do_test intpkey-4.12 { sl@0: count { sl@0: SELECT * FROM t1 WHERE a<0 AND a>10; sl@0: } sl@0: } {1} sl@0: sl@0: # Make sure it is OK to insert a rowid of 0 sl@0: # sl@0: do_test intpkey-5.1 { sl@0: execsql { sl@0: INSERT INTO t1 VALUES(0,'zero','entry'); sl@0: } sl@0: count { sl@0: SELECT * FROM t1 WHERE a=0; sl@0: } sl@0: } {0 zero entry 0} sl@0: do_test intpkey-5.2 { sl@0: execsql { sl@0: SELECT rowid, a FROM t1 sl@0: } sl@0: } {-4 -4 0 0 5 5 6 6 11 11} sl@0: sl@0: # Test the ability of the COPY command to put data into a sl@0: # table that contains an integer primary key. sl@0: # sl@0: # COPY command has been removed. But we retain these tests so sl@0: # that the tables will contain the right data for tests that follow. sl@0: # sl@0: do_test intpkey-6.1 { sl@0: execsql { sl@0: BEGIN; sl@0: INSERT INTO t1 VALUES(20,'b-20','c-20'); sl@0: INSERT INTO t1 VALUES(21,'b-21','c-21'); sl@0: INSERT INTO t1 VALUES(22,'b-22','c-22'); sl@0: COMMIT; sl@0: SELECT * FROM t1 WHERE a>=20; sl@0: } sl@0: } {20 b-20 c-20 21 b-21 c-21 22 b-22 c-22} sl@0: do_test intpkey-6.2 { sl@0: execsql { sl@0: SELECT * FROM t1 WHERE b=='hello' sl@0: } sl@0: } {5 hello world 11 hello world} sl@0: do_test intpkey-6.3 { sl@0: execsql { sl@0: DELETE FROM t1 WHERE b='b-21'; sl@0: SELECT * FROM t1 WHERE b=='b-21'; sl@0: } sl@0: } {} sl@0: do_test intpkey-6.4 { sl@0: execsql { sl@0: SELECT * FROM t1 WHERE a>=20 sl@0: } sl@0: } {20 b-20 c-20 22 b-22 c-22} sl@0: sl@0: # Do an insert of values with the columns specified out of order. sl@0: # sl@0: do_test intpkey-7.1 { sl@0: execsql { sl@0: INSERT INTO t1(c,b,a) VALUES('row','new',30); sl@0: SELECT * FROM t1 WHERE rowid>=30; sl@0: } sl@0: } {30 new row} sl@0: do_test intpkey-7.2 { sl@0: execsql { sl@0: SELECT * FROM t1 WHERE rowid>20; sl@0: } sl@0: } {22 b-22 c-22 30 new row} sl@0: sl@0: # Do an insert from a select statement. sl@0: # sl@0: do_test intpkey-8.1 { sl@0: execsql { sl@0: CREATE TABLE t2(x INTEGER PRIMARY KEY, y, z); sl@0: INSERT INTO t2 SELECT * FROM t1; sl@0: SELECT rowid FROM t2; sl@0: } sl@0: } {-4 0 5 6 11 20 22 30} sl@0: do_test intpkey-8.2 { sl@0: execsql { sl@0: SELECT x FROM t2; sl@0: } sl@0: } {-4 0 5 6 11 20 22 30} sl@0: sl@0: do_test intpkey-9.1 { sl@0: execsql { sl@0: UPDATE t1 SET c='www' WHERE c='world'; sl@0: SELECT rowid, a, c FROM t1 WHERE c=='www'; sl@0: } sl@0: } {5 5 www 11 11 www} sl@0: sl@0: sl@0: # Check insert of NULL for primary key sl@0: # sl@0: do_test intpkey-10.1 { sl@0: execsql { sl@0: DROP TABLE t2; sl@0: CREATE TABLE t2(x INTEGER PRIMARY KEY, y, z); sl@0: INSERT INTO t2 VALUES(NULL, 1, 2); sl@0: SELECT * from t2; sl@0: } sl@0: } {1 1 2} sl@0: do_test intpkey-10.2 { sl@0: execsql { sl@0: INSERT INTO t2 VALUES(NULL, 2, 3); sl@0: SELECT * from t2 WHERE x=2; sl@0: } sl@0: } {2 2 3} sl@0: do_test intpkey-10.3 { sl@0: execsql { sl@0: INSERT INTO t2 SELECT NULL, z, y FROM t2; sl@0: SELECT * FROM t2; sl@0: } sl@0: } {1 1 2 2 2 3 3 2 1 4 3 2} sl@0: sl@0: # This tests checks to see if a floating point number can be used sl@0: # to reference an integer primary key. sl@0: # sl@0: do_test intpkey-11.1 { sl@0: execsql { sl@0: SELECT b FROM t1 WHERE a=2.0+3.0; sl@0: } sl@0: } {hello} sl@0: do_test intpkey-11.1 { sl@0: execsql { sl@0: SELECT b FROM t1 WHERE a=2.0+3.5; sl@0: } sl@0: } {} sl@0: sl@0: integrity_check intpkey-12.1 sl@0: sl@0: # Try to use a string that looks like a floating point number as sl@0: # an integer primary key. This should actually work when the floating sl@0: # point value can be rounded to an integer without loss of data. sl@0: # sl@0: do_test intpkey-13.1 { sl@0: execsql { sl@0: SELECT * FROM t1 WHERE a=1; sl@0: } sl@0: } {} sl@0: do_test intpkey-13.2 { sl@0: execsql { sl@0: INSERT INTO t1 VALUES('1.0',2,3); sl@0: SELECT * FROM t1 WHERE a=1; sl@0: } sl@0: } {1 2 3} sl@0: do_test intpkey-13.3 { sl@0: catchsql { sl@0: INSERT INTO t1 VALUES('1.5',3,4); sl@0: } sl@0: } {1 {datatype mismatch}} sl@0: ifcapable {bloblit} { sl@0: do_test intpkey-13.4 { sl@0: catchsql { sl@0: INSERT INTO t1 VALUES(x'123456',3,4); sl@0: } sl@0: } {1 {datatype mismatch}} sl@0: } sl@0: do_test intpkey-13.5 { sl@0: catchsql { sl@0: INSERT INTO t1 VALUES('+1234567890',3,4); sl@0: } sl@0: } {0 {}} sl@0: sl@0: # Compare an INTEGER PRIMARY KEY against a TEXT expression. The INTEGER sl@0: # affinity should be applied to the text value before the comparison sl@0: # takes place. sl@0: # sl@0: do_test intpkey-14.1 { sl@0: execsql { sl@0: CREATE TABLE t3(a INTEGER PRIMARY KEY, b INTEGER, c TEXT); sl@0: INSERT INTO t3 VALUES(1, 1, 'one'); sl@0: INSERT INTO t3 VALUES(2, 2, '2'); sl@0: INSERT INTO t3 VALUES(3, 3, 3); sl@0: } sl@0: } {} sl@0: do_test intpkey-14.2 { sl@0: execsql { sl@0: SELECT * FROM t3 WHERE a>2; sl@0: } sl@0: } {3 3 3} sl@0: do_test intpkey-14.3 { sl@0: execsql { sl@0: SELECT * FROM t3 WHERE a>'2'; sl@0: } sl@0: } {3 3 3} sl@0: do_test intpkey-14.4 { sl@0: execsql { sl@0: SELECT * FROM t3 WHERE a<'2'; sl@0: } sl@0: } {1 1 one} sl@0: do_test intpkey-14.5 { sl@0: execsql { sl@0: SELECT * FROM t3 WHERE a2147483648; sl@0: } sl@0: } {} sl@0: do_test intpkey-15.2 { sl@0: execsql { sl@0: INSERT INTO t1 VALUES(NULL, 'big-2', 234); sl@0: SELECT b FROM t1 WHERE a>=2147483648; sl@0: } sl@0: } {big-2} sl@0: do_test intpkey-15.3 { sl@0: execsql { sl@0: SELECT b FROM t1 WHERE a>2147483648; sl@0: } sl@0: } {} sl@0: do_test intpkey-15.4 { sl@0: execsql { sl@0: SELECT b FROM t1 WHERE a>=2147483647; sl@0: } sl@0: } {big-1 big-2} sl@0: do_test intpkey-15.5 { sl@0: execsql { sl@0: SELECT b FROM t1 WHERE a<2147483648; sl@0: } sl@0: } {y zero 2 hello second hello b-20 b-22 new 3 big-1} sl@0: do_test intpkey-15.6 { sl@0: execsql { sl@0: SELECT b FROM t1 WHERE a<12345678901; sl@0: } sl@0: } {y zero 2 hello second hello b-20 b-22 new 3 big-1 big-2} sl@0: do_test intpkey-15.7 { sl@0: execsql { sl@0: SELECT b FROM t1 WHERE a>12345678901; sl@0: } sl@0: } {} sl@0: sl@0: sl@0: finish_test