1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-08 14:02:16 +03:00

Support for a future ALTER TABLE command to add columns with default values. (CVS 2367)

FossilOrigin-Name: 9d5abc1ddf6da37563c12d5a0401b89bb4e51c59
This commit is contained in:
danielk1977
2005-03-09 12:26:50 +00:00
parent 97ba4c94fc
commit aee18ef8e2
15 changed files with 326 additions and 47 deletions

View File

@@ -13,7 +13,7 @@
# file format change that may be used in the future to implement
# "ALTER TABLE ... ADD COLUMN".
#
# $Id: alter2.test,v 1.2 2005/02/26 18:10:45 drh Exp $
# $Id: alter2.test,v 1.3 2005/03/09 12:26:51 danielk1977 Exp $
#
set testdir [file dirname $argv0]
@@ -57,9 +57,11 @@ proc get_file_format {{fname test.db}} {
}
# This procedure sets the SQL statement stored for table $tbl in the
# sqlite_master table of file 'test.db' to $sql.
# sqlite_master table of file 'test.db' to $sql. Also set the file format
# to the supplied value. This is 2 if the added column has a default that is
# NULL, or 3 otherwise.
#
proc alter_table {tbl sql} {
proc alter_table {tbl sql {file_format 2}} {
sqlite3 dbat test.db
dbat eval {
PRAGMA writable_schema = 1;
@@ -222,12 +224,12 @@ ifcapable trigger {
#---------------------------------------------------------------------
# Check that an error occurs if the database is upgraded to a file
# format that SQLite does not support (in this case 3). Note: The
# format that SQLite does not support (in this case 4). Note: The
# file format is checked each time the schema is read, so changing the
# file format requires incrementing the schema cookie.
#
do_test alter2-4.1 {
set_file_format 3
set_file_format 4
} {}
do_test alter2-4.2 {
catchsql {
@@ -291,4 +293,137 @@ do_test alter2-6.3 {
get_file_format
} {2}
#---------------------------------------------------------------------
# Test that types and values for columns added with default values
# other than NULL work with SELECT statements.
#
do_test alter2-7.1 {
execsql {
DROP TABLE t1;
CREATE TABLE t1(a);
INSERT INTO t1 VALUES(1);
INSERT INTO t1 VALUES(2);
INSERT INTO t1 VALUES(3);
INSERT INTO t1 VALUES(4);
SELECT * FROM t1;
}
} {1 2 3 4}
do_test alter2-7.2 {
set sql {CREATE TABLE t1(a, b DEFAULT '123', c INTEGER DEFAULT '123')}
alter_table t1 $sql 3
execsql {
SELECT * FROM t1 LIMIT 1;
}
} {1 123 123}
do_test alter2-7.3 {
execsql {
SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
}
} {1 integer 123 text 123 integer}
do_test alter2-7.4 {
execsql {
SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
}
} {1 integer 123 text 123 integer}
do_test alter2-7.5 {
set sql {CREATE TABLE t1(a, b DEFAULT -123.0, c VARCHAR(10) default 5)}
alter_table t1 $sql 3
execsql {
SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
}
} {1 integer -123.0 real 5 text}
#-----------------------------------------------------------------------
# Test that UPDATE trigger tables work with default values, and that when
# a row is updated the default values are correctly transfered to the
# new row.
#
ifcapable trigger {
db function set_val {set ::val}
do_test alter2-8.1 {
execsql {
CREATE TRIGGER trig1 BEFORE UPDATE ON t1 BEGIN
SELECT set_val(
old.b||' '||typeof(old.b)||' '||old.c||' '||typeof(old.c)||' '||
new.b||' '||typeof(new.b)||' '||new.c||' '||typeof(new.c)
);
END;
}
list
} {}
}
do_test alter2-8.2 {
execsql {
UPDATE t1 SET c = 10 WHERE a = 1;
SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
}
} {1 integer -123.0 real 10 text}
ifcapable trigger {
do_test alter2-8.3 {
set ::val
} {-123 real 5 text -123 real 10 text}
}
#-----------------------------------------------------------------------
# Test that DELETE trigger tables work with default values, and that when
# a row is updated the default values are correctly transfered to the
# new row.
#
ifcapable trigger {
do_test alter2-9.1 {
execsql {
CREATE TRIGGER trig2 BEFORE DELETE ON t1 BEGIN
SELECT set_val(
old.b||' '||typeof(old.b)||' '||old.c||' '||typeof(old.c)
);
END;
}
list
} {}
do_test alter2-9.2 {
execsql {
DELETE FROM t1 WHERE a = 2;
}
set ::val
} {-123 real 5 text}
}
#-----------------------------------------------------------------------
# Test creating an index on a column added with a default value.
#
do_test alter2-10.1 {
execsql {
CREATE TABLE t2(a);
INSERT INTO t2 VALUES('a');
INSERT INTO t2 VALUES('b');
INSERT INTO t2 VALUES('c');
INSERT INTO t2 VALUES('d');
}
alter_table t2 {CREATE TABLE t2(a, b DEFAULT X'ABCD', c DEFAULT NULL);} 3
catchsql {
SELECT * FROM sqlite_master;
}
execsql {
SELECT quote(a), quote(b), quote(c) FROM t2 LIMIT 1;
}
} {'a' X'ABCD' NULL}
do_test alter2-10.2 {
execsql {
CREATE INDEX i1 ON t2(b);
SELECT a FROM t2 WHERE b = X'ABCD';
}
} {a b c d}
do_test alter2-10.3 {
execsql {
DELETE FROM t2 WHERE a = 'c';
SELECT a FROM t2 WHERE b = X'ABCD';
}
} {a b d}
do_test alter2-10.4 {
execsql {
SELECT count(b) FROM t2 WHERE b = X'ABCD';
}
} {3}
finish_test

View File

@@ -11,7 +11,7 @@
# This file implements regression tests for SQLite library. The
# focus of this script testing the callback-free C/C++ API.
#
# $Id: capi3.test,v 1.30 2005/02/19 08:18:06 danielk1977 Exp $
# $Id: capi3.test,v 1.31 2005/03/09 12:26:51 danielk1977 Exp $
#
set testdir [file dirname $argv0]
@@ -487,7 +487,7 @@ if {![sqlite3 -has-codec]} {
set ::bt [btree_open test.db 10 0]
btree_begin_transaction $::bt
set meta [btree_get_meta $::bt]
lset meta 2 3
lset meta 2 4
eval [concat btree_update_meta $::bt [lrange $meta 0 end]]
btree_commit $::bt
btree_close $::bt