1# 2010 September 18 2# 3# The author disclaims copyright to this source code. In place of 4# a legal notice, here is a blessing: 5# 6# 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. 9# 10#*********************************************************************** 11# 12# This file implements tests to verify that the "testable statements" in 13# the lang_insert.html document are correct. 14# 15set testdir [file dirname $argv0] 16source $testdir/tester.tcl 17 18# Organization of tests: 19# 20# e_insert-0.*: Test the syntax diagram. 21# 22# e_insert-1.*: Test statements of the form "INSERT ... VALUES(...)". 23# 24# e_insert-2.*: Test statements of the form "INSERT ... SELECT ...". 25# 26# e_insert-3.*: Test statements of the form "INSERT ... DEFAULT VALUES". 27# 28# e_insert-4.*: Test statements regarding the conflict clause. 29# 30# e_insert-5.*: Test that the qualified table name and "DEFAULT VALUES" 31# syntaxes do not work in trigger bodies. 32# 33 34do_execsql_test e_insert-0.0 { 35 CREATE TABLE a1(a, b); 36 CREATE TABLE a2(a, b, c DEFAULT 'xyz'); 37 CREATE TABLE a3(x DEFAULT 1.0, y DEFAULT 'string', z); 38 CREATE TABLE a4(c UNIQUE, d); 39} {} 40 41proc do_insert_tests {args} { 42 uplevel do_select_tests $args 43} 44 45# EVIDENCE-OF: R-41448-54465 -- syntax diagram insert-stmt 46# 47do_insert_tests e_insert-0 { 48 1 "INSERT INTO a1 DEFAULT VALUES" {} 49 2 "INSERT INTO main.a1 DEFAULT VALUES" {} 50 3 "INSERT OR ROLLBACK INTO main.a1 DEFAULT VALUES" {} 51 4 "INSERT OR ROLLBACK INTO a1 DEFAULT VALUES" {} 52 5 "INSERT OR ABORT INTO main.a1 DEFAULT VALUES" {} 53 6 "INSERT OR ABORT INTO a1 DEFAULT VALUES" {} 54 7 "INSERT OR REPLACE INTO main.a1 DEFAULT VALUES" {} 55 8 "INSERT OR REPLACE INTO a1 DEFAULT VALUES" {} 56 9 "INSERT OR FAIL INTO main.a1 DEFAULT VALUES" {} 57 10 "INSERT OR FAIL INTO a1 DEFAULT VALUES" {} 58 11 "INSERT OR FAIL INTO main.a1 DEFAULT VALUES" {} 59 12 "INSERT OR IGNORE INTO a1 DEFAULT VALUES" {} 60 13 "REPLACE INTO a1 DEFAULT VALUES" {} 61 14 "REPLACE INTO main.a1 DEFAULT VALUES" {} 62 15 "INSERT INTO a1 VALUES(1, 2)" {} 63 16 "INSERT INTO main.a1 VALUES(1, 2)" {} 64 17 "INSERT OR ROLLBACK INTO main.a1 VALUES(1, 2)" {} 65 18 "INSERT OR ROLLBACK INTO a1 VALUES(1, 2)" {} 66 19 "INSERT OR ABORT INTO main.a1 VALUES(1, 2)" {} 67 20 "INSERT OR ABORT INTO a1 VALUES(1, 2)" {} 68 21 "INSERT OR REPLACE INTO main.a1 VALUES(1, 2)" {} 69 22 "INSERT OR REPLACE INTO a1 VALUES(1, 2)" {} 70 23 "INSERT OR FAIL INTO main.a1 VALUES(1, 2)" {} 71 24 "INSERT OR FAIL INTO a1 VALUES(1, 2)" {} 72 25 "INSERT OR FAIL INTO main.a1 VALUES(1, 2)" {} 73 26 "INSERT OR IGNORE INTO a1 VALUES(1, 2)" {} 74 27 "REPLACE INTO a1 VALUES(1, 2)" {} 75 28 "REPLACE INTO main.a1 VALUES(1, 2)" {} 76 29 "INSERT INTO a1 (b, a) VALUES(1, 2)" {} 77 30 "INSERT INTO main.a1 (b, a) VALUES(1, 2)" {} 78 31 "INSERT OR ROLLBACK INTO main.a1 (b, a) VALUES(1, 2)" {} 79 32 "INSERT OR ROLLBACK INTO a1 (b, a) VALUES(1, 2)" {} 80 33 "INSERT OR ABORT INTO main.a1 (b, a) VALUES(1, 2)" {} 81 34 "INSERT OR ABORT INTO a1 (b, a) VALUES(1, 2)" {} 82 35 "INSERT OR REPLACE INTO main.a1 (b, a) VALUES(1, 2)" {} 83 36 "INSERT OR REPLACE INTO a1 (b, a) VALUES(1, 2)" {} 84 37 "INSERT OR FAIL INTO main.a1 (b, a) VALUES(1, 2)" {} 85 38 "INSERT OR FAIL INTO a1 (b, a) VALUES(1, 2)" {} 86 39 "INSERT OR FAIL INTO main.a1 (b, a) VALUES(1, 2)" {} 87 40 "INSERT OR IGNORE INTO a1 (b, a) VALUES(1, 2)" {} 88 41 "REPLACE INTO a1 (b, a) VALUES(1, 2)" {} 89 42 "REPLACE INTO main.a1 (b, a) VALUES(1, 2)" {} 90 43 "INSERT INTO a1 SELECT c, b FROM a2" {} 91 44 "INSERT INTO main.a1 SELECT c, b FROM a2" {} 92 45 "INSERT OR ROLLBACK INTO main.a1 SELECT c, b FROM a2" {} 93 46 "INSERT OR ROLLBACK INTO a1 SELECT c, b FROM a2" {} 94 47 "INSERT OR ABORT INTO main.a1 SELECT c, b FROM a2" {} 95 48 "INSERT OR ABORT INTO a1 SELECT c, b FROM a2" {} 96 49 "INSERT OR REPLACE INTO main.a1 SELECT c, b FROM a2" {} 97 50 "INSERT OR REPLACE INTO a1 SELECT c, b FROM a2" {} 98 51 "INSERT OR FAIL INTO main.a1 SELECT c, b FROM a2" {} 99 52 "INSERT OR FAIL INTO a1 SELECT c, b FROM a2" {} 100 53 "INSERT OR FAIL INTO main.a1 SELECT c, b FROM a2" {} 101 54 "INSERT OR IGNORE INTO a1 SELECT c, b FROM a2" {} 102 55 "REPLACE INTO a1 SELECT c, b FROM a2" {} 103 56 "REPLACE INTO main.a1 SELECT c, b FROM a2" {} 104 57 "INSERT INTO a1 (b, a) SELECT c, b FROM a2" {} 105 58 "INSERT INTO main.a1 (b, a) SELECT c, b FROM a2" {} 106 59 "INSERT OR ROLLBACK INTO main.a1 (b, a) SELECT c, b FROM a2" {} 107 60 "INSERT OR ROLLBACK INTO a1 (b, a) SELECT c, b FROM a2" {} 108 61 "INSERT OR ABORT INTO main.a1 (b, a) SELECT c, b FROM a2" {} 109 62 "INSERT OR ABORT INTO a1 (b, a) SELECT c, b FROM a2" {} 110 63 "INSERT OR REPLACE INTO main.a1 (b, a) SELECT c, b FROM a2" {} 111 64 "INSERT OR REPLACE INTO a1 (b, a) SELECT c, b FROM a2" {} 112 65 "INSERT OR FAIL INTO main.a1 (b, a) SELECT c, b FROM a2" {} 113 66 "INSERT OR FAIL INTO a1 (b, a) SELECT c, b FROM a2" {} 114 67 "INSERT OR FAIL INTO main.a1 (b, a) SELECT c, b FROM a2" {} 115 68 "INSERT OR IGNORE INTO a1 (b, a) SELECT c, b FROM a2" {} 116 69 "REPLACE INTO a1 (b, a) SELECT c, b FROM a2" {} 117 70 "REPLACE INTO main.a1 (b, a) SELECT c, b FROM a2" {} 118} 119 120delete_all_data 121 122# EVIDENCE-OF: R-20288-20462 The first form (with the "VALUES" keyword) 123# creates a single new row in an existing table. 124# 125do_insert_tests e_insert-1.1 { 126 0 "SELECT count(*) FROM a2" {0} 127 128 1a "INSERT INTO a2 VALUES(1, 2, 3)" {} 129 1b "SELECT count(*) FROM a2" {1} 130 131 2a "INSERT INTO a2(a, b) VALUES(1, 2)" {} 132 2b "SELECT count(*) FROM a2" {2} 133} 134 135# EVIDENCE-OF: R-36040-20870 If no column-list is specified then the 136# number of values must be the same as the number of columns in the 137# table. 138# 139# A test in the block above verifies that if the VALUES list has the 140# correct number of columns (for table a2, 3 columns) works. So these 141# tests just show that other values cause an error. 142# 143do_insert_tests e_insert-1.2 -error { 144 table %s has %d columns but %d values were supplied 145} { 146 1 "INSERT INTO a2 VALUES(1)" {a2 3 1} 147 2 "INSERT INTO a2 VALUES(1,2)" {a2 3 2} 148 3 "INSERT INTO a2 VALUES(1,2,3,4)" {a2 3 4} 149 4 "INSERT INTO a2 VALUES(1,2,3,4,5)" {a2 3 5} 150} 151 152# EVIDENCE-OF: R-52422-65517 In this case the result of evaluting the 153# left-most expression in the VALUES list is inserted into the left-most 154# column of the new row, and so on. 155# 156delete_all_data 157do_insert_tests e_insert-1.3 { 158 1a "INSERT INTO a2 VALUES(1, 2, 3)" {} 159 1b "SELECT * FROM a2 WHERE oid=last_insert_rowid()" {1 2 3} 160 161 2a "INSERT INTO a2 VALUES('abc', NULL, 3*3+1)" {} 162 2b "SELECT * FROM a2 WHERE oid=last_insert_rowid()" {abc {} 10} 163 164 3a "INSERT INTO a2 VALUES((SELECT count(*) FROM a2), 'x', 'y')" {} 165 3b "SELECT * FROM a2 WHERE oid=last_insert_rowid()" {2 x y} 166} 167 168# EVIDENCE-OF: R-62524-00361 If a column-list is specified, then the 169# number of values must match the number of specified columns. 170# 171do_insert_tests e_insert-1.4 -error { 172 %d values for %d columns 173} { 174 1 "INSERT INTO a2(a, b, c) VALUES(1)" {1 3} 175 2 "INSERT INTO a2(a, b, c) VALUES(1,2)" {2 3} 176 3 "INSERT INTO a2(a, b, c) VALUES(1,2,3,4)" {4 3} 177 4 "INSERT INTO a2(a, b, c) VALUES(1,2,3,4,5)" {5 3} 178 179 5 "INSERT INTO a2(c, a) VALUES(1)" {1 2} 180 6 "INSERT INTO a2(c, a) VALUES(1,2,3)" {3 2} 181 7 "INSERT INTO a2(c, a) VALUES(1,2,3,4)" {4 2} 182 8 "INSERT INTO a2(c, a) VALUES(1,2,3,4,5)" {5 2} 183} 184 185# EVIDENCE-OF: R-07016-26442 Each of the named columns of the new row is 186# populated with the results of evaluating the corresponding VALUES 187# expression. 188# 189# EVIDENCE-OF: R-12183-43719 Table columns that do not appear in the 190# column list are populated with the default column value (specified as 191# part of the CREATE TABLE statement), or with NULL if no default value 192# is specified. 193# 194delete_all_data 195do_insert_tests e_insert-1.5 { 196 1a "INSERT INTO a2(b, c) VALUES('b', 'c')" {} 197 1b "SELECT * FROM a2" {{} b c} 198 199 2a "INSERT INTO a2(a, b) VALUES('a', 'b')" {} 200 2b "SELECT * FROM a2" {{} b c a b xyz} 201} 202 203# EVIDENCE-OF: R-52173-30215 A new entry is inserted into the table for 204# each row of data returned by executing the SELECT statement. 205# 206delete_all_data 207do_insert_tests e_insert-2.1 { 208 0 "SELECT count(*) FROM a1" {0} 209 210 1a "SELECT count(*) FROM (SELECT 1, 2)" {1} 211 1b "INSERT INTO a1 SELECT 1, 2" {} 212 1c "SELECT count(*) FROM a1" {1} 213 214 2a "SELECT count(*) FROM (SELECT b, a FROM a1)" {1} 215 2b "INSERT INTO a1 SELECT b, a FROM a1" {} 216 2c "SELECT count(*) FROM a1" {2} 217 218 3a "SELECT count(*) FROM (SELECT b, a FROM a1)" {2} 219 3b "INSERT INTO a1 SELECT b, a FROM a1" {} 220 3c "SELECT count(*) FROM a1" {4} 221 222 4a "SELECT count(*) FROM (SELECT b, a FROM a1)" {4} 223 4b "INSERT INTO a1 SELECT b, a FROM a1" {} 224 4c "SELECT count(*) FROM a1" {8} 225 226 4a "SELECT count(*) FROM (SELECT min(b), min(a) FROM a1)" {1} 227 4b "INSERT INTO a1 SELECT min(b), min(a) FROM a1" {} 228 4c "SELECT count(*) FROM a1" {9} 229} 230 231 232# EVIDENCE-OF: R-63614-47421 If a column-list is specified, the number 233# of columns in the result of the SELECT must be the same as the number 234# of items in the column-list. 235# 236do_insert_tests e_insert-2.2 -error { 237 %d values for %d columns 238} { 239 1 "INSERT INTO a3(x, y) SELECT a, b, c FROM a2" {3 2} 240 2 "INSERT INTO a3(x, y) SELECT * FROM a2" {3 2} 241 3 "INSERT INTO a3(x, y) SELECT * FROM a2 CROSS JOIN a1" {5 2} 242 4 "INSERT INTO a3(x, y) SELECT * FROM a2 NATURAL JOIN a1" {3 2} 243 5 "INSERT INTO a3(x, y) SELECT a2.a FROM a2,a1" {1 2} 244 245 6 "INSERT INTO a3(z) SELECT a, b, c FROM a2" {3 1} 246 7 "INSERT INTO a3(z) SELECT * FROM a2" {3 1} 247 8 "INSERT INTO a3(z) SELECT * FROM a2 CROSS JOIN a1" {5 1} 248 9 "INSERT INTO a3(z) SELECT * FROM a2 NATURAL JOIN a1" {3 1} 249 10 "INSERT INTO a3(z) SELECT a1.* FROM a2,a1" {2 1} 250} 251 252# EVIDENCE-OF: R-58951-07798 Otherwise, if no column-list is specified, 253# the number of columns in the result of the SELECT must be the same as 254# the number of columns in the table. 255# 256do_insert_tests e_insert-2.3 -error { 257 table %s has %d columns but %d values were supplied 258} { 259 1 "INSERT INTO a1 SELECT a, b, c FROM a2" {a1 2 3} 260 2 "INSERT INTO a1 SELECT * FROM a2" {a1 2 3} 261 3 "INSERT INTO a1 SELECT * FROM a2 CROSS JOIN a1" {a1 2 5} 262 4 "INSERT INTO a1 SELECT * FROM a2 NATURAL JOIN a1" {a1 2 3} 263 5 "INSERT INTO a1 SELECT a2.a FROM a2,a1" {a1 2 1} 264} 265 266# EVIDENCE-OF: R-31074-37730 Any SELECT statement, including compound 267# SELECTs and SELECT statements with ORDER BY and/or LIMIT clauses, may 268# be used in an INSERT statement of this form. 269# 270delete_all_data 271do_execsql_test e_insert-2.3.0 { 272 INSERT INTO a1 VALUES('x', 'y'); 273} {} 274do_insert_tests e_insert-2.3 { 275 1 "INSERT INTO a1 SELECT a,b FROM a1 UNION SELECT b,a FROM a1 ORDER BY 1" {} 276 2 "INSERT INTO a1(b, a) SELECT * FROM a1 LIMIT 1" {} 277 3 "INSERT INTO a1 SELECT 'a'||a, 'b'||b FROM a1 LIMIT 2 OFFSET 1" {} 278 4 "INSERT INTO a1 SELECT * FROM a1 ORDER BY b, a" {} 279 S "SELECT * FROM a1" { 280 x y 281 x y y x 282 y x 283 ax by ay bx 284 ay bx ax by y x y x x y x y 285 } 286} 287 288# EVIDENCE-OF: R-25149-22012 The INSERT ... DEFAULT VALUES statement 289# inserts a single new row into the named table. 290# 291delete_all_data 292do_insert_tests e_insert-3.1 { 293 1 "SELECT count(*) FROM a3" {0} 294 2a "INSERT INTO a3 DEFAULT VALUES" {} 295 2b "SELECT count(*) FROM a3" {1} 296} 297 298# EVIDENCE-OF: R-18927-01951 Each column of the new row is populated 299# with its default value, or with a NULL if no default value is 300# specified as part of the column definition in the CREATE TABLE 301# statement. 302# 303delete_all_data 304do_insert_tests e_insert-3.2 { 305 1.1 "INSERT INTO a3 DEFAULT VALUES" {} 306 1.2 "SELECT * FROM a3" {1.0 string {}} 307 308 2.1 "INSERT INTO a3 DEFAULT VALUES" {} 309 2.2 "SELECT * FROM a3" {1.0 string {} 1.0 string {}} 310 311 3.1 "INSERT INTO a2 DEFAULT VALUES" {} 312 3.2 "SELECT * FROM a2" {{} {} xyz} 313 314 4.1 "INSERT INTO a2 DEFAULT VALUES" {} 315 4.2 "SELECT * FROM a2" {{} {} xyz {} {} xyz} 316 317 5.1 "INSERT INTO a1 DEFAULT VALUES" {} 318 5.2 "SELECT * FROM a1" {{} {}} 319 320 6.1 "INSERT INTO a1 DEFAULT VALUES" {} 321 6.2 "SELECT * FROM a1" {{} {} {} {}} 322} 323 324# EVIDENCE-OF: R-46928-50290 The optional conflict-clause allows the 325# specification of an alternative constraint conflict resolution 326# algorithm to use during this one INSERT command. 327# 328# EVIDENCE-OF: R-23110-47146 the parser allows the use of the single 329# keyword REPLACE as an alias for "INSERT OR REPLACE". 330# 331# The two requirements above are tested by e_select-4.1.* and 332# e_select-4.2.*, respectively. 333# 334do_execsql_test e_insert-4.1.0 { 335 INSERT INTO a4 VALUES(1, 'a'); 336 INSERT INTO a4 VALUES(2, 'a'); 337 INSERT INTO a4 VALUES(3, 'a'); 338} {} 339foreach {tn sql error ac data } { 340 1.1 "INSERT INTO a4 VALUES(2,'b')" {column c is not unique} 1 {1 a 2 a 3 a} 341 1.2 "INSERT OR REPLACE INTO a4 VALUES(2, 'b')" {} 1 {1 a 3 a 2 b} 342 1.3 "INSERT OR IGNORE INTO a4 VALUES(3, 'c')" {} 1 {1 a 3 a 2 b} 343 1.4 "BEGIN" {} 0 {1 a 3 a 2 b} 344 1.5 "INSERT INTO a4 VALUES(1, 'd')" {column c is not unique} 0 {1 a 3 a 2 b} 345 1.6 "INSERT OR ABORT INTO a4 VALUES(1, 'd')" 346 {column c is not unique} 0 {1 a 3 a 2 b} 347 1.7 "INSERT OR ROLLBACK INTO a4 VALUES(1, 'd')" 348 {column c is not unique} 1 {1 a 3 a 2 b} 349 1.8 "INSERT INTO a4 SELECT 4, 'e' UNION ALL SELECT 3, 'e'" 350 {column c is not unique} 1 {1 a 3 a 2 b} 351 1.9 "INSERT OR FAIL INTO a4 SELECT 4, 'e' UNION ALL SELECT 3, 'e'" 352 {column c is not unique} 1 {1 a 3 a 2 b 4 e} 353 354 2.1 "INSERT INTO a4 VALUES(2,'f')" 355 {column c is not unique} 1 {1 a 3 a 2 b 4 e} 356 2.2 "REPLACE INTO a4 VALUES(2, 'f')" {} 1 {1 a 3 a 4 e 2 f} 357} { 358 do_catchsql_test e_insert-4.1.$tn.1 $sql [list [expr {$error!=""}] $error] 359 do_execsql_test e_insert-4.1.$tn.2 {SELECT * FROM a4} [list {*}$data] 360 do_test e_insert-4.1.$tn.3 {sqlite3_get_autocommit db} $ac 361} 362 363# EVIDENCE-OF: R-64196-02418 The optional "database-name." prefix on the 364# table-name is support for top-level INSERT statements only. 365# 366# EVIDENCE-OF: R-05731-00924 The table name must be unqualified for 367# INSERT statements that occur within CREATE TRIGGER statements. 368# 369set err {1 {qualified table names are not allowed on INSERT, UPDATE, and DELETE statements within triggers}} 370 371do_catchsql_test e_insert-5.1.1 { 372 CREATE TRIGGER AFTER UPDATE ON a1 BEGIN 373 INSERT INTO main.a4 VALUES(new.a, new.b); 374 END; 375} $err 376do_catchsql_test e_insert-5.1.2 { 377 CREATE TEMP TABLE IF NOT EXISTS tmptable(a, b); 378 CREATE TRIGGER AFTER DELETE ON a3 BEGIN 379 INSERT INTO temp.tmptable VALUES(1, 2); 380 END; 381} $err 382 383# EVIDENCE-OF: R-15888-36326 Similarly, the "DEFAULT VALUES" form of the 384# INSERT statement is supported for top-level INSERT statements only and 385# not for INSERT statements within triggers. 386# 387do_catchsql_test e_insert-5.2.1 { 388 CREATE TRIGGER AFTER UPDATE ON a1 BEGIN 389 INSERT INTO a4 DEFAULT VALUES; 390 END; 391} {1 {near "DEFAULT": syntax error}} 392 393 394delete_all_data 395 396finish_test 397