123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- # 2001 September 15
- #
- # The author disclaims copyright to this source code. In place of
- # a legal notice, here is a blessing:
- #
- # May you do good and not evil.
- # May you find forgiveness for yourself and forgive others.
- # May you share freely, never taking more than you give.
- #
- #***********************************************************************
- # This file implements regression tests for SQLite library.
- #
- # This file implements tests for the special processing associated
- # with INTEGER PRIMARY KEY columns.
- #
- # $Id: intpkey.test,v 1.9 2002/03/31 18:29:03 drh Exp $
- set testdir [file dirname $argv0]
- source $testdir/tester.tcl
- # Create a table with a primary key and a datatype other than
- # integer
- #
- do_test intpkey-1.0 {
- execsql {
- CREATE TABLE t1(a TEXT PRIMARY KEY, b, c);
- }
- } {}
- # There should be an index associated with the primary key
- #
- do_test intpkey-1.1 {
- execsql {
- SELECT name FROM sqlite_master
- WHERE type='index' AND tbl_name='t1';
- }
- } {{(t1 autoindex 1)}}
- # Now create a table with an integer primary key and verify that
- # there is no associated index.
- #
- do_test intpkey-1.2 {
- execsql {
- DROP TABLE t1;
- CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
- SELECT name FROM sqlite_master
- WHERE type='index' AND tbl_name='t1';
- }
- } {}
- # Insert some records into the new table. Specify the primary key
- # and verify that the key is used as the record number.
- #
- do_test intpkey-1.3 {
- execsql {
- INSERT INTO t1 VALUES(5,'hello','world');
- }
- db last_insert_rowid
- } {5}
- do_test intpkey-1.4 {
- execsql {
- SELECT * FROM t1;
- }
- } {5 hello world}
- do_test intpkey-1.5 {
- execsql {
- SELECT rowid, * FROM t1;
- }
- } {5 5 hello world}
- # Attempting to insert a duplicate primary key should give a constraint
- # failure.
- #
- do_test intpkey-1.6 {
- set r [catch {execsql {
- INSERT INTO t1 VALUES(5,'second','entry');
- }} msg]
- lappend r $msg
- } {1 {constraint failed}}
- do_test intpkey-1.7 {
- execsql {
- SELECT rowid, * FROM t1;
- }
- } {5 5 hello world}
- do_test intpkey-1.8 {
- set r [catch {execsql {
- INSERT INTO t1 VALUES(6,'second','entry');
- }} msg]
- lappend r $msg
- } {0 {}}
- do_test intpkey-1.8.1 {
- db last_insert_rowid
- } {6}
- do_test intpkey-1.9 {
- execsql {
- SELECT rowid, * FROM t1;
- }
- } {5 5 hello world 6 6 second entry}
- # A ROWID is automatically generated for new records that do not specify
- # the integer primary key.
- #
- do_test intpkey-1.10 {
- execsql {
- INSERT INTO t1(b,c) VALUES('one','two');
- SELECT b FROM t1 ORDER BY b;
- }
- } {hello one second}
- # Try to change the ROWID for the new entry.
- #
- do_test intpkey-1.11 {
- execsql {
- UPDATE t1 SET a=4 WHERE b='one';
- SELECT * FROM t1;
- }
- } {4 one two 5 hello world 6 second entry}
- # Make sure SELECT statements are able to use the primary key column
- # as an index.
- #
- do_test intpkey-1.12 {
- execsql {
- SELECT * FROM t1 WHERE a==4;
- }
- } {4 one two}
- # Try to insert a non-integer value into the primary key field. This
- # should result in a data type mismatch.
- #
- do_test intpkey-1.13 {
- set r [catch {execsql {
- INSERT INTO t1 VALUES('x','y','z');
- }} msg]
- lappend r $msg
- } {1 {datatype mismatch}}
- do_test intpkey-1.14 {
- set r [catch {execsql {
- INSERT INTO t1 VALUES(3.4,'y','z');
- }} msg]
- lappend r $msg
- } {1 {datatype mismatch}}
- do_test intpkey-1.15 {
- set r [catch {execsql {
- INSERT INTO t1 VALUES(-3,'y','z');
- }} msg]
- lappend r $msg
- } {0 {}}
- do_test intpkey-1.16 {
- execsql {SELECT * FROM t1}
- } {-3 y z 4 one two 5 hello world 6 second entry}
- #### INDICES
- # Check to make sure indices work correctly with integer primary keys
- #
- do_test intpkey-2.1 {
- execsql {
- CREATE INDEX i1 ON t1(b);
- SELECT * FROM t1 WHERE b=='y'
- }
- } {-3 y z}
- do_test intpkey-2.1.1 {
- execsql {
- SELECT * FROM t1 WHERE b=='y' AND rowid<0
- }
- } {-3 y z}
- do_test intpkey-2.1.2 {
- execsql {
- SELECT * FROM t1 WHERE b=='y' AND rowid<0 AND rowid>=-20
- }
- } {-3 y z}
- do_test intpkey-2.1.3 {
- execsql {
- SELECT * FROM t1 WHERE b>='y'
- }
- } {-3 y z}
- do_test intpkey-2.1.4 {
- execsql {
- SELECT * FROM t1 WHERE b>='y' AND rowid<10
- }
- } {-3 y z}
- do_test intpkey-2.2 {
- execsql {
- UPDATE t1 SET a=8 WHERE b=='y';
- SELECT * FROM t1 WHERE b=='y';
- }
- } {8 y z}
- do_test intpkey-2.3 {
- execsql {
- SELECT rowid, * FROM t1;
- }
- } {4 4 one two 5 5 hello world 6 6 second entry 8 8 y z}
- do_test intpkey-2.4 {
- execsql {
- SELECT rowid, * FROM t1 WHERE b<'second'
- }
- } {5 5 hello world 4 4 one two}
- do_test intpkey-2.4.1 {
- execsql {
- SELECT rowid, * FROM t1 WHERE 'second'>b
- }
- } {5 5 hello world 4 4 one two}
- do_test intpkey-2.4.2 {
- execsql {
- SELECT rowid, * FROM t1 WHERE 8>rowid AND 'second'>b
- }
- } {4 4 one two 5 5 hello world}
- do_test intpkey-2.4.3 {
- execsql {
- SELECT rowid, * FROM t1 WHERE 8>rowid AND 'second'>b AND 0<rowid
- }
- } {4 4 one two 5 5 hello world}
- do_test intpkey-2.5 {
- execsql {
- SELECT rowid, * FROM t1 WHERE b>'a'
- }
- } {5 5 hello world 4 4 one two 6 6 second entry 8 8 y z}
- do_test intpkey-2.6 {
- execsql {
- DELETE FROM t1 WHERE rowid=4;
- SELECT * FROM t1 WHERE b>'a';
- }
- } {5 hello world 6 second entry 8 y z}
- do_test intpkey-2.7 {
- execsql {
- UPDATE t1 SET a=-4 WHERE rowid=8;
- SELECT * FROM t1 WHERE b>'a';
- }
- } {5 hello world 6 second entry -4 y z}
- do_test intpkey-2.7 {
- execsql {
- SELECT * FROM t1
- }
- } {-4 y z 5 hello world 6 second entry}
- # Do an SQL statement. Append the search count to the end of the result.
- #
- proc count sql {
- set ::sqlite_search_count 0
- return [concat [execsql $sql] $::sqlite_search_count]
- }
- # Create indices that include the integer primary key as one of their
- # columns.
- #
- do_test intpkey-3.1 {
- execsql {
- CREATE INDEX i2 ON t1(a);
- }
- } {}
- do_test intpkey-3.2 {
- count {
- SELECT * FROM t1 WHERE a=5;
- }
- } {5 hello world 0}
- do_test intpkey-3.3 {
- count {
- SELECT * FROM t1 WHERE a>4 AND a<6;
- }
- } {5 hello world 2}
- do_test intpkey-3.4 {
- count {
- SELECT * FROM t1 WHERE b>='hello' AND b<'hello2';
- }
- } {5 hello world 3}
- do_test intpkey-3.5 {
- execsql {
- CREATE INDEX i3 ON t1(c,a);
- }
- } {}
- do_test intpkey-3.6 {
- count {
- SELECT * FROM t1 WHERE c=='world';
- }
- } {5 hello world 3}
- do_test intpkey-3.7 {
- execsql {INSERT INTO t1 VALUES(11,'hello','world')}
- count {
- SELECT * FROM t1 WHERE c=='world';
- }
- } {5 hello world 11 hello world 5}
- do_test intpkey-3.8 {
- count {
- SELECT * FROM t1 WHERE c=='world' AND a>7;
- }
- } {11 hello world 5}
- do_test intpkey-3.9 {
- count {
- SELECT * FROM t1 WHERE 7<a;
- }
- } {11 hello world 1}
- # Test inequality constraints on integer primary keys and rowids
- #
- do_test intpkey-4.1 {
- count {
- SELECT * FROM t1 WHERE 11=rowid
- }
- } {11 hello world 0}
- do_test intpkey-4.2 {
- count {
- SELECT * FROM t1 WHERE 11=rowid AND b=='hello'
- }
- } {11 hello world 0}
- do_test intpkey-4.3 {
- count {
- SELECT * FROM t1 WHERE 11=rowid AND b=='hello' AND c IS NOT NULL;
- }
- } {11 hello world 0}
- do_test intpkey-4.4 {
- count {
- SELECT * FROM t1 WHERE rowid==11
- }
- } {11 hello world 0}
- do_test intpkey-4.5 {
- count {
- SELECT * FROM t1 WHERE oid==11 AND b=='hello'
- }
- } {11 hello world 0}
- do_test intpkey-4.6 {
- count {
- SELECT * FROM t1 WHERE a==11 AND b=='hello' AND c IS NOT NULL;
- }
- } {11 hello world 0}
- do_test intpkey-4.7 {
- count {
- SELECT * FROM t1 WHERE 8<rowid;
- }
- } {11 hello world 1}
- do_test intpkey-4.8 {
- count {
- SELECT * FROM t1 WHERE 8<rowid AND 11>=oid;
- }
- } {11 hello world 1}
- do_test intpkey-4.9 {
- count {
- SELECT * FROM t1 WHERE 11<=_rowid_ AND 12>=a;
- }
- } {11 hello world 1}
- do_test intpkey-4.10 {
- count {
- SELECT * FROM t1 WHERE 0>=_rowid_;
- }
- } {-4 y z 1}
- do_test intpkey-4.11 {
- count {
- SELECT * FROM t1 WHERE a<0;
- }
- } {-4 y z 1}
- do_test intpkey-4.12 {
- count {
- SELECT * FROM t1 WHERE a<0 AND a>10;
- }
- } {1}
- # Make sure it is OK to insert a rowid of 0
- #
- do_test intpkey-5.1 {
- execsql {
- INSERT INTO t1 VALUES(0,'zero','entry');
- }
- count {
- SELECT * FROM t1 WHERE a=0;
- }
- } {0 zero entry 0}
- do_test intpkey=5.2 {
- execsql {
- SELECT rowid, a FROM t1
- }
- } {-4 -4 0 0 5 5 6 6 11 11}
- # Test the ability of the COPY command to put data into a
- # table that contains an integer primary key.
- #
- do_test intpkey-6.1 {
- set f [open ./data1.txt w]
- puts $f "20\tb-20\tc-20"
- puts $f "21\tb-21\tc-21"
- puts $f "22\tb-22\tc-22"
- close $f
- execsql {
- COPY t1 FROM 'data1.txt';
- SELECT * FROM t1 WHERE a>=20;
- }
- } {20 b-20 c-20 21 b-21 c-21 22 b-22 c-22}
- do_test intpkey-6.2 {
- execsql {
- SELECT * FROM t1 WHERE b=='hello'
- }
- } {5 hello world 11 hello world}
- do_test intpkey-6.3 {
- execsql {
- DELETE FROM t1 WHERE b='b-21';
- SELECT * FROM t1 WHERE b=='b-21';
- }
- } {}
- do_test intpkey-6.4 {
- execsql {
- SELECT * FROM t1 WHERE a>=20
- }
- } {20 b-20 c-20 22 b-22 c-22}
- # Do an insert of values with the columns specified out of order.
- #
- do_test intpkey-7.1 {
- execsql {
- INSERT INTO t1(c,b,a) VALUES('row','new',30);
- SELECT * FROM t1 WHERE rowid>=30;
- }
- } {30 new row}
- do_test intpkey-7.2 {
- execsql {
- SELECT * FROM t1 WHERE rowid>20;
- }
- } {22 b-22 c-22 30 new row}
- # Do an insert from a select statement.
- #
- do_test intpkey-8.1 {
- execsql {
- CREATE TABLE t2(x INTEGER PRIMARY KEY, y, z);
- INSERT INTO t2 SELECT * FROM t1;
- SELECT rowid FROM t2;
- }
- } {-4 0 5 6 11 20 22 30}
- do_test intpkey-8.2 {
- execsql {
- SELECT x FROM t2;
- }
- } {-4 0 5 6 11 20 22 30}
- do_test intpkey-9.1 {
- execsql {
- UPDATE t1 SET c='www' WHERE c='world';
- SELECT rowid, a, c FROM t1 WHERE c=='www';
- }
- } {5 5 www 11 11 www}
- # Check insert of NULL for primary key
- #
- do_test intpkey-10.1 {
- execsql {
- DROP TABLE t2;
- CREATE TABLE t2(x INTEGER PRIMARY KEY, y, z);
- INSERT INTO t2 VALUES(NULL, 1, 2);
- SELECT * from t2;
- }
- } {1 1 2}
- do_test intpkey-10.2 {
- execsql {
- INSERT INTO t2 VALUES(NULL, 2, 3);
- SELECT * from t2 WHERE x=2;
- }
- } {2 2 3}
- finish_test
|