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