a46838cb13
statement where the new row conflicts with both the IPK and the ON CONFLICT indexes. FossilOrigin-Name: d8eb9f8d9b61400c7e12f01ef5c233257b03532221f7c7a8386f7ac2db439626
233 lines
6.6 KiB
Plaintext
233 lines
6.6 KiB
Plaintext
# 2018-04-17
|
|
#
|
|
# The author disclaims copyright to this source code. In place of
|
|
# a legal notice, here is a blessing:
|
|
#
|
|
# May you do good and not evil.
|
|
# May you find forgiveness for yourself and forgive others.
|
|
# May you share freely, never taking more than you give.
|
|
#
|
|
#***********************************************************************
|
|
#
|
|
# Test cases for UPSERT
|
|
|
|
set testdir [file dirname $argv0]
|
|
source $testdir/tester.tcl
|
|
set testprefix upsert4
|
|
|
|
foreach {tn sql} {
|
|
1 { CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c UNIQUE) }
|
|
2 { CREATE TABLE t1(a INT PRIMARY KEY, b, c UNIQUE) }
|
|
3 { CREATE TABLE t1(a INT PRIMARY KEY, b, c UNIQUE) WITHOUT ROWID}
|
|
} {
|
|
reset_db
|
|
execsql $sql
|
|
|
|
do_execsql_test 1.$tn.0 {
|
|
INSERT INTO t1 VALUES(1, NULL, 'one');
|
|
INSERT INTO t1 VALUES(2, NULL, 'two');
|
|
INSERT INTO t1 VALUES(3, NULL, 'three');
|
|
}
|
|
|
|
do_execsql_test 1.$tn.1 {
|
|
INSERT INTO t1 VALUES(1, NULL, 'xyz') ON CONFLICT DO NOTHING;
|
|
SELECT * FROM t1;
|
|
} {
|
|
1 {} one 2 {} two 3 {} three
|
|
}
|
|
|
|
do_execsql_test 1.$tn.2 {
|
|
INSERT INTO t1 VALUES(4, NULL, 'two') ON CONFLICT DO NOTHING;
|
|
SELECT * FROM t1;
|
|
} {
|
|
1 {} one 2 {} two 3 {} three
|
|
}
|
|
|
|
do_execsql_test 1.$tn.3 {
|
|
INSERT INTO t1 VALUES(4, NULL, 'two') ON CONFLICT (c) DO UPDATE SET b = 1;
|
|
SELECT * FROM t1;
|
|
} {
|
|
1 {} one 2 1 two 3 {} three
|
|
}
|
|
|
|
do_execsql_test 1.$tn.4 {
|
|
INSERT INTO t1 VALUES(2, NULL, 'zero') ON CONFLICT (a) DO UPDATE SET b=2;
|
|
SELECT * FROM t1;
|
|
} {1 {} one 2 2 two 3 {} three}
|
|
|
|
do_catchsql_test 1.$tn.5 {
|
|
INSERT INTO t1 VALUES(2, NULL, 'zero') ON CONFLICT (a)
|
|
DO UPDATE SET c = 'one';
|
|
} {1 {UNIQUE constraint failed: t1.c}}
|
|
|
|
do_execsql_test 1.$tn.6 {
|
|
SELECT * FROM t1;
|
|
} {1 {} one 2 2 two 3 {} three}
|
|
|
|
do_execsql_test 1.$tn.7 {
|
|
INSERT INTO t1 VALUES(2, NULL, 'zero') ON CONFLICT (a)
|
|
DO UPDATE SET (b, c) = (SELECT 'x', 'y');
|
|
SELECT * FROM t1;
|
|
} {1 {} one 2 x y 3 {} three}
|
|
|
|
do_execsql_test 1.$tn.8 {
|
|
INSERT INTO t1 VALUES(1, NULL, NULL) ON CONFLICT (a)
|
|
DO UPDATE SET (c, a) = ('four', 4);
|
|
SELECT * FROM t1 ORDER BY 1;
|
|
} {2 x y 3 {} three 4 {} four}
|
|
}
|
|
|
|
#-------------------------------------------------------------------------
|
|
# Test target analysis.
|
|
#
|
|
set rtbl(0) {0 {}}
|
|
set rtbl(1) {/1 .*failed.*/}
|
|
set rtbl(2) {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}}
|
|
|
|
foreach {tn sql} {
|
|
1 {
|
|
CREATE TABLE xyz(a INTEGER PRIMARY KEY, b, c, d);
|
|
CREATE UNIQUE INDEX xyz1 ON xyz(d, c, b COLLATE nocase);
|
|
}
|
|
|
|
2 {
|
|
CREATE TABLE xyz(a INT PRIMARY KEY, b, c, d);
|
|
CREATE UNIQUE INDEX xyz1 ON xyz(d, c, b COLLATE nocase);
|
|
}
|
|
|
|
3 {
|
|
CREATE TABLE xyz(a INT PRIMARY KEY, b, c, d) WITHOUT ROWID;
|
|
CREATE UNIQUE INDEX xyz1 ON xyz(d, c, b COLLATE nocase);
|
|
}
|
|
} {
|
|
reset_db
|
|
execsql $sql
|
|
do_execsql_test 2.$tn.1 {
|
|
INSERT INTO xyz VALUES(10, 1, 1, 'one');
|
|
}
|
|
|
|
|
|
foreach {tn2 oc res} {
|
|
1 "ON CONFLICT (b COLLATE nocase, c, d) DO NOTHING" 0
|
|
2 "ON CONFLICT (b, c, d) DO NOTHING" 0
|
|
3 "ON CONFLICT (b, c COLLATE nocase, d) DO NOTHING" 2
|
|
4 "ON CONFLICT (a) DO NOTHING" 1
|
|
5 "ON CONFLICT DO NOTHING" 0
|
|
6 "ON CONFLICT (b, c, d) WHERE a!=0 DO NOTHING" 0
|
|
7 "ON CONFLICT (d, c, c) WHERE a!=0 DO NOTHING" 2
|
|
8 "ON CONFLICT (b COLLATE nocase, c COLLATE nocase, d) DO NOTHING" 2
|
|
9 "ON CONFLICT (b, c, d) WHERE b==45 DO NOTHING" 0
|
|
} {
|
|
|
|
do_catchsql_test 2.$tn.2.$tn2 "
|
|
INSERT INTO xyz VALUES(11, 1, 1, 'one') $oc
|
|
" $rtbl($res)
|
|
}
|
|
|
|
do_execsql_test 2.$tn.3 {
|
|
SELECT * FROM xyz;
|
|
} {10 1 1 one}
|
|
}
|
|
|
|
foreach {tn sql} {
|
|
1 {
|
|
CREATE TABLE abc(a INTEGER PRIMARY KEY, x, y);
|
|
CREATE UNIQUE INDEX abc1 ON abc(('x' || x) COLLATE nocase);
|
|
}
|
|
2 {
|
|
CREATE TABLE abc(a INT PRIMARY KEY, x, y);
|
|
CREATE UNIQUE INDEX abc1 ON abc(('x' || x) COLLATE nocase);
|
|
}
|
|
3 {
|
|
CREATE TABLE abc(a INT PRIMARY KEY, x, y) WITHOUT ROWID;
|
|
CREATE UNIQUE INDEX abc1 ON abc(('x' || x) COLLATE nocase);
|
|
}
|
|
} {
|
|
reset_db
|
|
execsql $sql
|
|
do_execsql_test 3.$tn.1 {
|
|
INSERT INTO abc VALUES(1, 'one', 'two');
|
|
}
|
|
|
|
foreach {tn2 oc res} {
|
|
1 "ON CONFLICT DO NOTHING" 0
|
|
2 "ON CONFLICT ('x' || x) DO NOTHING" 0
|
|
3 "ON CONFLICT (('x' || x) COLLATE nocase) DO NOTHING" 0
|
|
4 "ON CONFLICT (('x' || x) COLLATE binary) DO NOTHING" 2
|
|
5 "ON CONFLICT (x || 'x') DO NOTHING" 2
|
|
6 "ON CONFLICT ((('x' || x))) DO NOTHING" 0
|
|
} {
|
|
do_catchsql_test 3.$tn.2.$tn2 "
|
|
INSERT INTO abc VALUES(2, 'one', NULL) $oc;
|
|
" $rtbl($res)
|
|
}
|
|
|
|
do_execsql_test 3.$tn.3 {
|
|
SELECT * FROM abc
|
|
} {1 one two}
|
|
}
|
|
|
|
foreach {tn sql} {
|
|
1 {
|
|
CREATE TABLE abc(a INTEGER PRIMARY KEY, x, y);
|
|
CREATE UNIQUE INDEX abc1 ON abc(x) WHERE y>0;
|
|
CREATE UNIQUE INDEX abc2 ON abc(y) WHERE x='xyz' COLLATE nocase;
|
|
}
|
|
} {
|
|
reset_db
|
|
execsql $sql
|
|
do_execsql_test 4.$tn.1 {
|
|
INSERT INTO abc VALUES(1, 'one', 1);
|
|
INSERT INTO abc VALUES(2, 'two', 2);
|
|
INSERT INTO abc VALUES(3, 'xyz', 3);
|
|
INSERT INTO abc VALUES(4, 'XYZ', 4);
|
|
}
|
|
|
|
foreach {tn2 oc res} {
|
|
1 "ON CONFLICT DO NOTHING" 0
|
|
2 "ON CONFLICT(x) WHERE y>0 DO NOTHING" 0
|
|
3 "ON CONFLICT(x) DO NOTHING" 2
|
|
4 "ON CONFLICT(x) WHERE y>=0 DO NOTHING" 2
|
|
5 "ON CONFLICT(y) WHERE x='xyz' COLLATE nocase DO NOTHING" 1
|
|
} {
|
|
do_catchsql_test 4.$tn.2.$tn2 "
|
|
INSERT INTO abc VALUES(5, 'one', 10) $oc
|
|
" $rtbl($res)
|
|
}
|
|
|
|
do_execsql_test 4.$tn.3 {
|
|
SELECT * FROM abc
|
|
} {1 one 1 2 two 2 3 xyz 3 4 XYZ 4}
|
|
|
|
foreach {tn2 oc res} {
|
|
1 "ON CONFLICT DO NOTHING" 0
|
|
2 "ON CONFLICT(y) WHERE x='xyz' COLLATE nocase DO NOTHING" 0
|
|
3 "ON CONFLICT(y) WHERE x='xyz' COLLATE binary DO NOTHING" 2
|
|
4 "ON CONFLICT(x) WHERE y>0 DO NOTHING" 1
|
|
} {
|
|
do_catchsql_test 4.$tn.2.$tn2 "
|
|
INSERT INTO abc VALUES(5, 'xYz', 3) $oc
|
|
" $rtbl($res)
|
|
}
|
|
}
|
|
|
|
do_catchsql_test 5.0 {
|
|
CREATE TABLE w1(a INT PRIMARY KEY, x, y);
|
|
CREATE UNIQUE INDEX w1expr ON w1(('x' || x));
|
|
INSERT INTO w1 VALUES(2, 'one', NULL)
|
|
ON CONFLICT (('x' || x) COLLATE nocase) DO NOTHING;
|
|
} {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}}
|
|
|
|
#-------------------------------------------------------------------------
|
|
#
|
|
do_execsql_test 6.0 {
|
|
CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE, c);
|
|
INSERT INTO t1 VALUES(1, 1, 'one');
|
|
INSERT INTO t1 VALUES(2, 2, 'two');
|
|
INSERT OR REPLACE INTO t1 VALUES(1, 2, 'two') ON CONFLICT(b) DO NOTHING;
|
|
PRAGMA integrity_check;
|
|
} {ok}
|
|
|
|
finish_test
|
|
|