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 29do_execsql_test e_insert-0.0 { 30 CREATE TABLE a1(a, b); 31 CREATE TABLE a2(a, b, c DEFAULT 'xyz'); 32 33 CREATE TABLE a3(x DEFAULT 1.0, y DEFAULT 'string', z); 34} {} 35 36proc delete_all_data {} { 37 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} { 38 db eval "DELETE FROM '[string map {' ''} $t]'" 39 } 40} 41 42proc do_insert_tests {args} { 43 uplevel do_select_tests $args 44} 45 46# EVIDENCE-OF: R-41448-54465 -- syntax diagram insert-stmt 47# 48do_insert_tests e_insert-0 { 49 1 "INSERT INTO a1 DEFAULT VALUES" {} 50 2 "INSERT INTO main.a1 DEFAULT VALUES" {} 51 3 "INSERT OR ROLLBACK INTO main.a1 DEFAULT VALUES" {} 52 4 "INSERT OR ROLLBACK INTO a1 DEFAULT VALUES" {} 53 5 "INSERT OR ABORT INTO main.a1 DEFAULT VALUES" {} 54 6 "INSERT OR ABORT INTO a1 DEFAULT VALUES" {} 55 7 "INSERT OR REPLACE INTO main.a1 DEFAULT VALUES" {} 56 8 "INSERT OR REPLACE INTO a1 DEFAULT VALUES" {} 57 9 "INSERT OR FAIL INTO main.a1 DEFAULT VALUES" {} 58 10 "INSERT OR FAIL INTO a1 DEFAULT VALUES" {} 59 11 "INSERT OR FAIL INTO main.a1 DEFAULT VALUES" {} 60 12 "INSERT OR IGNORE INTO a1 DEFAULT VALUES" {} 61 13 "REPLACE INTO a1 DEFAULT VALUES" {} 62 14 "REPLACE INTO main.a1 DEFAULT VALUES" {} 63 15 "INSERT INTO a1 VALUES(1, 2)" {} 64 16 "INSERT INTO main.a1 VALUES(1, 2)" {} 65 17 "INSERT OR ROLLBACK INTO main.a1 VALUES(1, 2)" {} 66 18 "INSERT OR ROLLBACK INTO a1 VALUES(1, 2)" {} 67 19 "INSERT OR ABORT INTO main.a1 VALUES(1, 2)" {} 68 20 "INSERT OR ABORT INTO a1 VALUES(1, 2)" {} 69 21 "INSERT OR REPLACE INTO main.a1 VALUES(1, 2)" {} 70 22 "INSERT OR REPLACE INTO a1 VALUES(1, 2)" {} 71 23 "INSERT OR FAIL INTO main.a1 VALUES(1, 2)" {} 72 24 "INSERT OR FAIL INTO a1 VALUES(1, 2)" {} 73 25 "INSERT OR FAIL INTO main.a1 VALUES(1, 2)" {} 74 26 "INSERT OR IGNORE INTO a1 VALUES(1, 2)" {} 75 27 "REPLACE INTO a1 VALUES(1, 2)" {} 76 28 "REPLACE INTO main.a1 VALUES(1, 2)" {} 77 29 "INSERT INTO a1 (b, a) VALUES(1, 2)" {} 78 30 "INSERT INTO main.a1 (b, a) VALUES(1, 2)" {} 79 31 "INSERT OR ROLLBACK INTO main.a1 (b, a) VALUES(1, 2)" {} 80 32 "INSERT OR ROLLBACK INTO a1 (b, a) VALUES(1, 2)" {} 81 33 "INSERT OR ABORT INTO main.a1 (b, a) VALUES(1, 2)" {} 82 34 "INSERT OR ABORT INTO a1 (b, a) VALUES(1, 2)" {} 83 35 "INSERT OR REPLACE INTO main.a1 (b, a) VALUES(1, 2)" {} 84 36 "INSERT OR REPLACE INTO a1 (b, a) VALUES(1, 2)" {} 85 37 "INSERT OR FAIL INTO main.a1 (b, a) VALUES(1, 2)" {} 86 38 "INSERT OR FAIL INTO a1 (b, a) VALUES(1, 2)" {} 87 39 "INSERT OR FAIL INTO main.a1 (b, a) VALUES(1, 2)" {} 88 40 "INSERT OR IGNORE INTO a1 (b, a) VALUES(1, 2)" {} 89 41 "REPLACE INTO a1 (b, a) VALUES(1, 2)" {} 90 42 "REPLACE INTO main.a1 (b, a) VALUES(1, 2)" {} 91 43 "INSERT INTO a1 SELECT c, b FROM a2" {} 92 44 "INSERT INTO main.a1 SELECT c, b FROM a2" {} 93 45 "INSERT OR ROLLBACK INTO main.a1 SELECT c, b FROM a2" {} 94 46 "INSERT OR ROLLBACK INTO a1 SELECT c, b FROM a2" {} 95 47 "INSERT OR ABORT INTO main.a1 SELECT c, b FROM a2" {} 96 48 "INSERT OR ABORT INTO a1 SELECT c, b FROM a2" {} 97 49 "INSERT OR REPLACE INTO main.a1 SELECT c, b FROM a2" {} 98 50 "INSERT OR REPLACE INTO a1 SELECT c, b FROM a2" {} 99 51 "INSERT OR FAIL INTO main.a1 SELECT c, b FROM a2" {} 100 52 "INSERT OR FAIL INTO a1 SELECT c, b FROM a2" {} 101 53 "INSERT OR FAIL INTO main.a1 SELECT c, b FROM a2" {} 102 54 "INSERT OR IGNORE INTO a1 SELECT c, b FROM a2" {} 103 55 "REPLACE INTO a1 SELECT c, b FROM a2" {} 104 56 "REPLACE INTO main.a1 SELECT c, b FROM a2" {} 105 57 "INSERT INTO a1 (b, a) SELECT c, b FROM a2" {} 106 58 "INSERT INTO main.a1 (b, a) SELECT c, b FROM a2" {} 107 59 "INSERT OR ROLLBACK INTO main.a1 (b, a) SELECT c, b FROM a2" {} 108 60 "INSERT OR ROLLBACK INTO a1 (b, a) SELECT c, b FROM a2" {} 109 61 "INSERT OR ABORT INTO main.a1 (b, a) SELECT c, b FROM a2" {} 110 62 "INSERT OR ABORT INTO a1 (b, a) SELECT c, b FROM a2" {} 111 63 "INSERT OR REPLACE INTO main.a1 (b, a) SELECT c, b FROM a2" {} 112 64 "INSERT OR REPLACE INTO a1 (b, a) SELECT c, b FROM a2" {} 113 65 "INSERT OR FAIL INTO main.a1 (b, a) SELECT c, b FROM a2" {} 114 66 "INSERT OR FAIL INTO a1 (b, a) SELECT c, b FROM a2" {} 115 67 "INSERT OR FAIL INTO main.a1 (b, a) SELECT c, b FROM a2" {} 116 68 "INSERT OR IGNORE INTO a1 (b, a) SELECT c, b FROM a2" {} 117 69 "REPLACE INTO a1 (b, a) SELECT c, b FROM a2" {} 118 70 "REPLACE INTO main.a1 (b, a) SELECT c, b FROM a2" {} 119} 120 121delete_all_data 122 123# EVIDENCE-OF: R-20288-20462 The first form (with the "VALUES" keyword) 124# creates a single new row in an existing table. 125# 126do_insert_tests e_insert-1.1 { 127 0 "SELECT count(*) FROM a2" {0} 128 129 1a "INSERT INTO a2 VALUES(1, 2, 3)" {} 130 1b "SELECT count(*) FROM a2" {1} 131 132 2a "INSERT INTO a2(a, b) VALUES(1, 2)" {} 133 2b "SELECT count(*) FROM a2" {2} 134} 135 136# EVIDENCE-OF: R-36040-20870 If no column-list is specified then the 137# number of values must be the same as the number of columns in the 138# table. 139# 140# A test in the block above verifies that if the VALUES list has the 141# correct number of columns (for table a2, 3 columns) works. So these 142# tests just show that other values cause an error. 143# 144do_insert_tests e_insert-1.2 -error { 145 table %s has %d columns but %d values were supplied 146} { 147 1 "INSERT INTO a2 VALUES(1)" {a2 3 1} 148 2 "INSERT INTO a2 VALUES(1,2)" {a2 3 2} 149 3 "INSERT INTO a2 VALUES(1,2,3,4)" {a2 3 4} 150 4 "INSERT INTO a2 VALUES(1,2,3,4,5)" {a2 3 5} 151} 152 153# EVIDENCE-OF: R-52422-65517 In this case the result of evaluting the 154# left-most expression in the VALUES list is inserted into the left-most 155# column of the new row, and so on. 156# 157delete_all_data 158do_insert_tests e_insert-1.3 { 159 1a "INSERT INTO a2 VALUES(1, 2, 3)" {} 160 1b "SELECT * FROM a2 WHERE oid=last_insert_rowid()" {1 2 3} 161 162 2a "INSERT INTO a2 VALUES('abc', NULL, 3*3+1)" {} 163 2b "SELECT * FROM a2 WHERE oid=last_insert_rowid()" {abc {} 10} 164 165 3a "INSERT INTO a2 VALUES((SELECT count(*) FROM a2), 'x', 'y')" {} 166 3b "SELECT * FROM a2 WHERE oid=last_insert_rowid()" {2 x y} 167} 168 169# EVIDENCE-OF: R-62524-00361 If a column-list is specified, then the 170# number of values must match the number of specified columns. 171# 172do_insert_tests e_insert-1.4 -error { 173 %d values for %d columns 174} { 175 1 "INSERT INTO a2(a, b, c) VALUES(1)" {1 3} 176 2 "INSERT INTO a2(a, b, c) VALUES(1,2)" {2 3} 177 3 "INSERT INTO a2(a, b, c) VALUES(1,2,3,4)" {4 3} 178 4 "INSERT INTO a2(a, b, c) VALUES(1,2,3,4,5)" {5 3} 179 180 5 "INSERT INTO a2(c, a) VALUES(1)" {1 2} 181 6 "INSERT INTO a2(c, a) VALUES(1,2,3)" {3 2} 182 7 "INSERT INTO a2(c, a) VALUES(1,2,3,4)" {4 2} 183 8 "INSERT INTO a2(c, a) VALUES(1,2,3,4,5)" {5 2} 184} 185 186# EVIDENCE-OF: R-07016-26442 Each of the named columns of the new row is 187# populated with the results of evaluating the corresponding VALUES 188# expression. 189# 190# EVIDENCE-OF: R-12183-43719 Table columns that do not appear in the 191# column list are populated with the default column value (specified as 192# part of the CREATE TABLE statement), or with NULL if no default value 193# is specified. 194# 195delete_all_data 196do_insert_tests e_insert-1.5 { 197 1a "INSERT INTO a2(b, c) VALUES('b', 'c')" {} 198 1b "SELECT * FROM a2" {{} b c} 199 200 2a "INSERT INTO a2(a, b) VALUES('a', 'b')" {} 201 2b "SELECT * FROM a2" {{} b c a b xyz} 202} 203 204delete_all_data 205 206 207# EVIDENCE-OF: R-63614-47421 If a column-list is specified, the number 208# of columns in the result of the SELECT must be the same as the number 209# of items in the column-list. 210# 211do_insert_tests e_insert-2.2 -error { 212 %d values for %d columns 213} { 214 1 "INSERT INTO a3(x, y) SELECT a, b, c FROM a2" {3 2} 215 2 "INSERT INTO a3(x, y) SELECT * FROM a2" {3 2} 216 3 "INSERT INTO a3(x, y) SELECT * FROM a2 CROSS JOIN a1" {5 2} 217 4 "INSERT INTO a3(x, y) SELECT * FROM a2 NATURAL JOIN a1" {3 2} 218 5 "INSERT INTO a3(x, y) SELECT a2.a FROM a2,a1" {1 2} 219 220 6 "INSERT INTO a3(z) SELECT a, b, c FROM a2" {3 1} 221 7 "INSERT INTO a3(z) SELECT * FROM a2" {3 1} 222 8 "INSERT INTO a3(z) SELECT * FROM a2 CROSS JOIN a1" {5 1} 223 9 "INSERT INTO a3(z) SELECT * FROM a2 NATURAL JOIN a1" {3 1} 224 10 "INSERT INTO a3(z) SELECT a1.* FROM a2,a1" {2 1} 225} 226 227# EVIDENCE-OF: R-58951-07798 Otherwise, if no column-list is specified, 228# the number of columns in the result of the SELECT must be the same as 229# the number of columns in the table. 230# 231do_insert_tests e_insert-2.3 -error { 232 table %s has %d columns but %d values were supplied 233} { 234 1 "INSERT INTO a1 SELECT a, b, c FROM a2" {a1 2 3} 235 2 "INSERT INTO a1 SELECT * FROM a2" {a1 2 3} 236 3 "INSERT INTO a1 SELECT * FROM a2 CROSS JOIN a1" {a1 2 5} 237 4 "INSERT INTO a1 SELECT * FROM a2 NATURAL JOIN a1" {a1 2 3} 238 5 "INSERT INTO a1 SELECT a2.a FROM a2,a1" {a1 2 1} 239} 240 241 242# EVIDENCE-OF: R-25149-22012 The INSERT ... DEFAULT VALUES statement 243# inserts a single new row into the named table. 244# 245delete_all_data 246do_insert_tests e_insert-3.1 { 247 1 "SELECT count(*) FROM a3" {0} 248 2a "INSERT INTO a3 DEFAULT VALUES" {} 249 2b "SELECT count(*) FROM a3" {1} 250} 251 252# EVIDENCE-OF: R-18927-01951 Each column of the new row is populated 253# with its default value, or with a NULL if no default value is 254# specified as part of the column definition in the CREATE TABLE 255# statement. 256# 257delete_all_data 258do_insert_tests e_insert-3.2 { 259 1.1 "INSERT INTO a3 DEFAULT VALUES" {} 260 1.2 "SELECT * FROM a3" {1.0 string {}} 261 262 2.1 "INSERT INTO a3 DEFAULT VALUES" {} 263 2.2 "SELECT * FROM a3" {1.0 string {} 1.0 string {}} 264 265 3.1 "INSERT INTO a2 DEFAULT VALUES" {} 266 3.2 "SELECT * FROM a2" {{} {} xyz} 267 268 4.1 "INSERT INTO a2 DEFAULT VALUES" {} 269 4.2 "SELECT * FROM a2" {{} {} xyz {} {} xyz} 270 271 5.1 "INSERT INTO a1 DEFAULT VALUES" {} 272 5.2 "SELECT * FROM a1" {{} {}} 273 274 6.1 "INSERT INTO a1 DEFAULT VALUES" {} 275 6.2 "SELECT * FROM a1" {{} {} {} {}} 276} 277 278 279 280delete_all_data 281 282finish_test 283