3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
12 # Test cases for UPSERT
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
16 set testprefix zipfile
18 do_execsql_test upsert1-100 {
19 CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c DEFAULT 0);
20 CREATE UNIQUE INDEX t1x1 ON t1(b);
21 INSERT INTO t1(a,b) VALUES(1,2) ON CONFLICT DO NOTHING;
22 INSERT INTO t1(a,b) VALUES(1,99),(99,2) ON CONFLICT DO NOTHING;
25 do_execsql_test upsert1-101 {
27 INSERT INTO t1(a,b) VALUES(2,3) ON CONFLICT(a) DO NOTHING;
28 INSERT INTO t1(a,b) VALUES(2,99) ON CONFLICT(a) DO NOTHING;
31 do_execsql_test upsert1-102 {
33 INSERT INTO t1(a,b) VALUES(3,4) ON CONFLICT(b) DO NOTHING;
34 INSERT INTO t1(a,b) VALUES(99,4) ON CONFLICT(b) DO NOTHING;
37 do_catchsql_test upsert1-110 {
38 INSERT INTO t1(a,b) VALUES(5,6) ON CONFLICT(x) DO NOTHING;
40 } {1 {no such column: x}}
41 do_catchsql_test upsert1-120 {
42 INSERT INTO t1(a,b) VALUES(5,6) ON CONFLICT(c) DO NOTHING;
44 } {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}}
46 do_catchsql_test upsert1-130 {
47 INSERT INTO t1(a,b) VALUES(5,6) ON CONFLICT(b COLLATE nocase) DO NOTHING;
49 } {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}}
50 do_execsql_test upsert1-140 {
52 INSERT INTO t1(a,b) VALUES(5,6) ON CONFLICT(b COLLATE binary) DO NOTHING;
56 do_catchsql_test upsert1-200 {
58 CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT, c DEFAULT 0);
59 CREATE UNIQUE INDEX t1x1 ON t1(a+b);
60 INSERT INTO t1(a,b) VALUES(7,8) ON CONFLICT(a+b) DO NOTHING;
61 INSERT INTO t1(a,b) VALUES(8,7),(9,6) ON CONFLICT(a+b) DO NOTHING;
64 do_catchsql_test upsert1-201 {
65 INSERT INTO t1(a,b) VALUES(8,7),(9,6) ON CONFLICT(a) DO NOTHING;
66 } {1 {UNIQUE constraint failed: index 't1x1'}}
67 do_catchsql_test upsert1-210 {
69 INSERT INTO t1(a,b) VALUES(9,10) ON CONFLICT(a+(+b)) DO NOTHING;
71 } {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}}
73 do_catchsql_test upsert1-300 {
76 CREATE UNIQUE INDEX t1x1 ON t1(b) WHERE b>10;
77 INSERT INTO t1(a,b) VALUES(1,2),(3,2) ON CONFLICT(b) DO NOTHING;
79 } {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}}
80 do_catchsql_test upsert1-310 {
82 INSERT INTO t1(a,b) VALUES(1,2),(3,2) ON CONFLICT(b) WHERE b!=10 DO NOTHING;
84 } {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}}
85 do_execsql_test upsert1-320 {
87 INSERT INTO t1(a,b) VALUES(1,2),(3,2),(4,20),(5,20)
88 ON CONFLICT(b) WHERE b>10 DO NOTHING;
89 SELECT *, 'x' FROM t1 ORDER BY b, a;
90 } {1 2 0 x 3 2 0 x 4 20 0 x}
92 # Upsert works with count_changes=on;
93 do_execsql_test upsert1-400 {
94 DROP TABLE IF EXISTS t2;
95 CREATE TABLE t2(a TEXT UNIQUE, b INT DEFAULT 1);
96 INSERT INTO t2(a) VALUES('one'),('two'),('three');
97 PRAGMA count_changes=ON;
98 INSERT INTO t2(a) VALUES('one'),('one'),('three'),('four')
99 ON CONFLICT(a) DO UPDATE SET b=b+1;
101 do_execsql_test upsert1-410 {
102 PRAGMA count_changes=OFF;
103 SELECT a, b FROM t2 ORDER BY a;
104 } {four 1 one 3 three 2 two 1}
106 # Problem found by AFL prior to any release
107 do_execsql_test upsert1-500 {
109 CREATE TABLE t1(x INTEGER PRIMARY KEY, y INT UNIQUE);
110 INSERT INTO t1(x,y) SELECT 1,2 WHERE true
111 ON CONFLICT(x) DO UPDATE SET y=max(t1.y,excluded.y) AND true;