blob: 6c103b1ea3fb3fddb9e926b2468a86bdab835ca0 [file] [log] [blame]
drh348784e2000-05-29 20:41:49 +00001# Copyright (c) 1999, 2000 D. Richard Hipp
2#
3# This program is free software; you can redistribute it and/or
4# modify it under the terms of the GNU General Public
5# License as published by the Free Software Foundation; either
6# version 2 of the License, or (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11# General Public License for more details.
12#
13# You should have received a copy of the GNU General Public
14# License along with this library; if not, write to the
15# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16# Boston, MA 02111-1307, USA.
17#
18# Author contact information:
19# drh@hwaci.com
20# http://www.hwaci.com/drh/
21#
22#***********************************************************************
23# This file implements regression tests for SQLite library. The
24# focus of this file is testing the CREATE TABLE statement.
25#
drhd4007282001-04-12 23:21:58 +000026# $Id: table.test,v 1.9 2001/04/12 23:21:59 drh Exp $
drh348784e2000-05-29 20:41:49 +000027
28set testdir [file dirname $argv0]
29source $testdir/tester.tcl
30
31# Create a basic table and verify it is added to sqlite_master
32#
drh1b6a71f2000-05-29 23:58:11 +000033do_test table-1.1 {
drh348784e2000-05-29 20:41:49 +000034 execsql {
35 CREATE TABLE test1 (
36 one varchar(10),
37 two text
38 )
39 }
40 execsql {
drh28037572000-08-02 13:47:41 +000041 SELECT sql FROM sqlite_master WHERE type!='meta'
drh348784e2000-05-29 20:41:49 +000042 }
43} {{CREATE TABLE test1 (
44 one varchar(10),
45 two text
46 )}}
47
48# Verify that both table files exists in the database directory
49#
drh1b6a71f2000-05-29 23:58:11 +000050do_test table-1.2 {
drh348784e2000-05-29 20:41:49 +000051 execsql {INSERT INTO test1 VALUES('hi', 'y''all')}
drh767c2002000-10-19 14:10:08 +000052} {}
53testif gdbm:
54do_test table-1.2b {
drh348784e2000-05-29 20:41:49 +000055 lsort [glob -nocomplain testdb/*.tbl]
56} {testdb/sqlite_master.tbl testdb/test1.tbl}
57
58# Verify the other fields of the sqlite_master file.
59#
drh1b6a71f2000-05-29 23:58:11 +000060do_test table-1.3 {
drh28037572000-08-02 13:47:41 +000061 execsql {SELECT name, tbl_name, type FROM sqlite_master WHERE type!='meta'}
drh348784e2000-05-29 20:41:49 +000062} {test1 test1 table}
63
64# Close and reopen the database. Verify that everything is
65# still the same.
66#
drh767c2002000-10-19 14:10:08 +000067skipif memory:
drh1b6a71f2000-05-29 23:58:11 +000068do_test table-1.4 {
drh348784e2000-05-29 20:41:49 +000069 db close
70 sqlite db testdb
drh28037572000-08-02 13:47:41 +000071 execsql {SELECT name, tbl_name, type from sqlite_master WHERE type!='meta'}
drh348784e2000-05-29 20:41:49 +000072} {test1 test1 table}
73
74# Drop the database and make sure it disappears.
75#
drh1b6a71f2000-05-29 23:58:11 +000076do_test table-1.5 {
drh348784e2000-05-29 20:41:49 +000077 execsql {DROP TABLE test1}
drh28037572000-08-02 13:47:41 +000078 execsql {SELECT * FROM sqlite_master WHERE type!='meta'}
drh348784e2000-05-29 20:41:49 +000079} {}
80
81# Verify that the file associated with the database is gone.
82#
drh767c2002000-10-19 14:10:08 +000083testif gdbm:
drh1b6a71f2000-05-29 23:58:11 +000084do_test table-1.5 {
drh348784e2000-05-29 20:41:49 +000085 lsort [glob -nocomplain testdb/*.tbl]
86} {testdb/sqlite_master.tbl}
87
88# Close and reopen the database. Verify that the table is
89# still gone.
90#
drh767c2002000-10-19 14:10:08 +000091skipif memory:
drh1b6a71f2000-05-29 23:58:11 +000092do_test table-1.6 {
drh348784e2000-05-29 20:41:49 +000093 db close
94 sqlite db testdb
drh28037572000-08-02 13:47:41 +000095 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
drh348784e2000-05-29 20:41:49 +000096} {}
97
drh982cef72000-05-30 16:27:03 +000098# Repeat the above steps, but this time quote the table name.
99#
100do_test table-1.10 {
101 execsql {CREATE TABLE "create" (f1 int)}
drh28037572000-08-02 13:47:41 +0000102 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
drh982cef72000-05-30 16:27:03 +0000103} {create}
104do_test table-1.11 {
105 execsql {DROP TABLE "create"}
drh28037572000-08-02 13:47:41 +0000106 execsql {SELECT name FROM "sqlite_master" WHERE type!='meta'}
drh982cef72000-05-30 16:27:03 +0000107} {}
108do_test table-1.12 {
109 execsql {CREATE TABLE test1("f1 ho" int)}
drh28037572000-08-02 13:47:41 +0000110 execsql {SELECT name as "X" FROM sqlite_master WHERE type!='meta'}
drh982cef72000-05-30 16:27:03 +0000111} {test1}
112do_test table-1.13 {
113 execsql {DROP TABLE "TEST1"}
drh28037572000-08-02 13:47:41 +0000114 execsql {SELECT name FROM "sqlite_master" WHERE type!='meta'}
drh982cef72000-05-30 16:27:03 +0000115} {}
116
117
drhb24fcbe2000-05-29 23:30:50 +0000118
119# Verify that we cannot make two tables with the same name
120#
drh1b6a71f2000-05-29 23:58:11 +0000121do_test table-2.1 {
drh4cfa7932000-06-08 15:10:46 +0000122 execsql {CREATE TABLE TEST2(one text)}
drhb24fcbe2000-05-29 23:30:50 +0000123 set v [catch {execsql {CREATE TABLE test2(two text)}} msg]
124 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000125} {1 {table test2 already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000126do_test table-2.1b {
drhb24fcbe2000-05-29 23:30:50 +0000127 set v [catch {execsql {CREATE TABLE sqlite_master(two text)}} msg]
128 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000129} {1 {table sqlite_master already exists}}
drh767c2002000-10-19 14:10:08 +0000130skipif memory:
drh1b6a71f2000-05-29 23:58:11 +0000131do_test table-2.1c {
drhb24fcbe2000-05-29 23:30:50 +0000132 db close
133 sqlite db testdb
134 set v [catch {execsql {CREATE TABLE sqlite_master(two text)}} msg]
135 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000136} {1 {table sqlite_master already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000137do_test table-2.1d {
drh28037572000-08-02 13:47:41 +0000138 execsql {DROP TABLE test2; SELECT name FROM sqlite_master WHERE type!='meta'}
drhb24fcbe2000-05-29 23:30:50 +0000139} {}
140
141# Verify that we cannot make a table with the same name as an index
142#
drhdcc581c2000-05-30 13:44:19 +0000143do_test table-2.2a {
drhb24fcbe2000-05-29 23:30:50 +0000144 execsql {CREATE TABLE test2(one text); CREATE INDEX test3 ON test2(one)}
145 set v [catch {execsql {CREATE TABLE test3(two text)}} msg]
146 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000147} {1 {there is already an index named test3}}
drh767c2002000-10-19 14:10:08 +0000148skipif memory:
drh1b6a71f2000-05-29 23:58:11 +0000149do_test table-2.2b {
drhb24fcbe2000-05-29 23:30:50 +0000150 db close
151 sqlite db testdb
152 set v [catch {execsql {CREATE TABLE test3(two text)}} msg]
153 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000154} {1 {there is already an index named test3}}
drh1b6a71f2000-05-29 23:58:11 +0000155do_test table-2.2c {
drhb24fcbe2000-05-29 23:30:50 +0000156 execsql {DROP INDEX test3}
157 set v [catch {execsql {CREATE TABLE test3(two text)}} msg]
158 lappend v $msg
159} {0 {}}
drh1b6a71f2000-05-29 23:58:11 +0000160do_test table-2.2d {
drh28037572000-08-02 13:47:41 +0000161 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000162} {test2 test3}
drh1b6a71f2000-05-29 23:58:11 +0000163do_test table-2.2e {
drhb24fcbe2000-05-29 23:30:50 +0000164 execsql {DROP TABLE test2; DROP TABLE test3}
drh28037572000-08-02 13:47:41 +0000165 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000166} {}
167
168# Create a table with many field names
169#
170set big_table \
171{CREATE TABLE big(
172 f1 varchar(20),
173 f2 char(10),
drhdcc581c2000-05-30 13:44:19 +0000174 f3 varchar(30) primary key,
drhb24fcbe2000-05-29 23:30:50 +0000175 f4 text,
176 f5 text,
177 f6 text,
178 f7 text,
179 f8 text,
180 f9 text,
181 f10 text,
182 f11 text,
183 f12 text,
184 f13 text,
185 f14 text,
186 f15 text,
187 f16 text,
188 f17 text,
189 f18 text,
190 f19 text,
191 f20 text
192)}
drh1b6a71f2000-05-29 23:58:11 +0000193do_test table-3.1 {
drhb24fcbe2000-05-29 23:30:50 +0000194 execsql $big_table
drh28037572000-08-02 13:47:41 +0000195 execsql {SELECT sql FROM sqlite_master WHERE type!='meta'}
drhb24fcbe2000-05-29 23:30:50 +0000196} \{$big_table\}
drh1b6a71f2000-05-29 23:58:11 +0000197do_test table-3.2 {
drhb24fcbe2000-05-29 23:30:50 +0000198 set v [catch {execsql {CREATE TABLE BIG(xyz foo)}} msg]
199 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000200} {1 {table BIG already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000201do_test table-3.3 {
drhb24fcbe2000-05-29 23:30:50 +0000202 set v [catch {execsql {CREATE TABLE biG(xyz foo)}} msg]
203 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000204} {1 {table biG already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000205do_test table-3.4 {
drhb24fcbe2000-05-29 23:30:50 +0000206 set v [catch {execsql {CREATE TABLE bIg(xyz foo)}} msg]
207 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000208} {1 {table bIg already exists}}
drh767c2002000-10-19 14:10:08 +0000209skipif memory:
drh1b6a71f2000-05-29 23:58:11 +0000210do_test table-3.5 {
drhb24fcbe2000-05-29 23:30:50 +0000211 db close
212 sqlite db testdb
213 set v [catch {execsql {CREATE TABLE Big(xyz foo)}} msg]
214 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000215} {1 {table Big already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000216do_test table-3.6 {
drhb24fcbe2000-05-29 23:30:50 +0000217 execsql {DROP TABLE big}
drh28037572000-08-02 13:47:41 +0000218 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
drhb24fcbe2000-05-29 23:30:50 +0000219} {}
220
221# Try creating large numbers of tables
222#
223set r {}
224for {set i 1} {$i<=100} {incr i} {
225 lappend r test$i
226}
drh1b6a71f2000-05-29 23:58:11 +0000227do_test table-4.1 {
drhb24fcbe2000-05-29 23:30:50 +0000228 for {set i 1} {$i<=100} {incr i} {
229 set sql "CREATE TABLE test$i ("
230 for {set k 1} {$k<$i} {incr k} {
231 append sql "field$k text,"
232 }
233 append sql "last_field text)"
234 execsql $sql
235 }
drh28037572000-08-02 13:47:41 +0000236 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000237} $r
drh767c2002000-10-19 14:10:08 +0000238skipif memory:
drh1b6a71f2000-05-29 23:58:11 +0000239do_test table-4.1b {
drhb24fcbe2000-05-29 23:30:50 +0000240 db close
241 sqlite db testdb
drh28037572000-08-02 13:47:41 +0000242 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000243} $r
244
drhc4a3c772001-04-04 11:48:57 +0000245# Drop the even numbered tables
drhb24fcbe2000-05-29 23:30:50 +0000246#
247set r {}
248for {set i 1} {$i<=100} {incr i 2} {
249 lappend r test$i
250}
drh1b6a71f2000-05-29 23:58:11 +0000251do_test table-4.2 {
drhb24fcbe2000-05-29 23:30:50 +0000252 for {set i 2} {$i<=100} {incr i 2} {
253 set sql "DROP TABLE TEST$i"
254 execsql $sql
255 }
drh28037572000-08-02 13:47:41 +0000256 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000257} $r
258
259# Drop the odd number tables
260#
drh1b6a71f2000-05-29 23:58:11 +0000261do_test table-4.3 {
drhb24fcbe2000-05-29 23:30:50 +0000262 for {set i 1} {$i<=100} {incr i 2} {
263 set sql "DROP TABLE test$i"
264 execsql $sql
265 }
drh28037572000-08-02 13:47:41 +0000266 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000267} {}
268
drh1b6a71f2000-05-29 23:58:11 +0000269# Try to drop a table that does not exist
270#
271do_test table-5.1 {
272 set v [catch {execsql {DROP TABLE test9}} msg]
273 lappend v $msg
274} {1 {no such table: test9}}
275
276# Try to drop sqlite_master
277#
278do_test table-5.2 {
279 set v [catch {execsql {DROP TABLE sqlite_master}} msg]
280 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000281} {1 {table sqlite_master may not be dropped}}
drh1b6a71f2000-05-29 23:58:11 +0000282
283# Make sure an EXPLAIN does not really create a new table
284#
285do_test table-5.3 {
286 execsql {EXPLAIN CREATE TABLE test1(f1 int)}
drh28037572000-08-02 13:47:41 +0000287 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
drh1b6a71f2000-05-29 23:58:11 +0000288} {}
289
290# Make sure an EXPLAIN does not really drop an existing table
291#
292do_test table-5.4 {
293 execsql {CREATE TABLE test1(f1 int)}
294 execsql {EXPLAIN DROP TABLE test1}
drh28037572000-08-02 13:47:41 +0000295 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
drh1b6a71f2000-05-29 23:58:11 +0000296} {test1}
297
drh4cfa7932000-06-08 15:10:46 +0000298# Create a table with a goofy name
299#
drh767c2002000-10-19 14:10:08 +0000300testif gdbm:
drh4cfa7932000-06-08 15:10:46 +0000301do_test table-6.1 {
302 execsql {CREATE TABLE 'Spaces In This Name!'(x int)}
303 execsql {INSERT INTO 'spaces in this name!' VALUES(1)}
304 set list [glob -nocomplain testdb/spaces*.tbl]
305} {testdb/spaces+in+this+name+.tbl}
306
drhc4a3c772001-04-04 11:48:57 +0000307# Try using keywords as table names or column names.
308#
309do_test table-7.1 {
310 set v [catch {execsql {
311 CREATE TABLE weird(
312 desc text,
313 asc text,
314 explain int,
315 vacuum boolean,
drhd4007282001-04-12 23:21:58 +0000316 delimiters varchar(10),
317 begin blob,
318 end clob
drhc4a3c772001-04-04 11:48:57 +0000319 )
320 }} msg]
321 lappend v $msg
322} {0 {}}
323do_test table-7.2 {
324 execsql {
drhd4007282001-04-12 23:21:58 +0000325 INSERT INTO weird VALUES('a','b',9,0,'xyz','hi','y''all');
drhc4a3c772001-04-04 11:48:57 +0000326 SELECT * FROM weird;
327 }
drhd4007282001-04-12 23:21:58 +0000328} {a b 9 0 xyz hi y'all}
drhc4a3c772001-04-04 11:48:57 +0000329do_test table-7.3 {
330 execsql2 {
331 SELECT * FROM weird;
332 }
drhd4007282001-04-12 23:21:58 +0000333} {desc a asc b explain 9 vacuum 0 delimiters xyz begin hi end y'all}
drhc4a3c772001-04-04 11:48:57 +0000334
drh348784e2000-05-29 20:41:49 +0000335finish_test