xref: /sqlite-3.40.0/test/update.test (revision 7d44b22d)
1b19a2bc6Sdrh# 2001 September 15
21d37e284Sdrh#
3b19a2bc6Sdrh# The author disclaims copyright to this source code.  In place of
4b19a2bc6Sdrh# a legal notice, here is a blessing:
51d37e284Sdrh#
6b19a2bc6Sdrh#    May you do good and not evil.
7b19a2bc6Sdrh#    May you find forgiveness for yourself and forgive others.
8b19a2bc6Sdrh#    May you share freely, never taking more than you give.
91d37e284Sdrh#
101d37e284Sdrh#***********************************************************************
111d37e284Sdrh# This file implements regression tests for SQLite library.  The
121d37e284Sdrh# focus of this file is testing the UPDATE statement.
131d37e284Sdrh#
1480106e5cSdrh# $Id: update.test,v 1.19 2008/04/10 18:44:36 drh Exp $
151d37e284Sdrh
161d37e284Sdrhset testdir [file dirname $argv0]
171d37e284Sdrhsource $testdir/tester.tcl
181d37e284Sdrh
191d37e284Sdrh# Try to update an non-existent table
201d37e284Sdrh#
211d37e284Sdrhdo_test update-1.1 {
221d37e284Sdrh  set v [catch {execsql {UPDATE test1 SET f2=5 WHERE f1<1}} msg]
231d37e284Sdrh  lappend v $msg
241d37e284Sdrh} {1 {no such table: test1}}
251d37e284Sdrh
261d37e284Sdrh# Try to update a read-only table
271d37e284Sdrh#
281d37e284Sdrhdo_test update-2.1 {
291d37e284Sdrh  set v [catch \
301d37e284Sdrh       {execsql {UPDATE sqlite_master SET name='xyz' WHERE name='123'}} msg]
311d37e284Sdrh  lappend v $msg
321d37e284Sdrh} {1 {table sqlite_master may not be modified}}
331d37e284Sdrh
341d37e284Sdrh# Create a table to work with
351d37e284Sdrh#
361d37e284Sdrhdo_test update-3.1 {
371d37e284Sdrh  execsql {CREATE TABLE test1(f1 int,f2 int)}
381d37e284Sdrh  for {set i 1} {$i<=10} {incr i} {
3924acd8f9Sdanielk1977    set sql "INSERT INTO test1 VALUES($i,[expr {1<<$i}])"
401d37e284Sdrh    execsql $sql
411d37e284Sdrh  }
421d37e284Sdrh  execsql {SELECT * FROM test1 ORDER BY f1}
431d37e284Sdrh} {1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024}
441d37e284Sdrh
45967e8b73Sdrh# Unknown column name in an expression
461d37e284Sdrh#
471d37e284Sdrhdo_test update-3.2 {
481d37e284Sdrh  set v [catch {execsql {UPDATE test1 SET f1=f3*2 WHERE f2==32}} msg]
491d37e284Sdrh  lappend v $msg
50967e8b73Sdrh} {1 {no such column: f3}}
511d37e284Sdrhdo_test update-3.3 {
521d37e284Sdrh  set v [catch {execsql {UPDATE test1 SET f1=test2.f1*2 WHERE f2==32}} msg]
531d37e284Sdrh  lappend v $msg
54967e8b73Sdrh} {1 {no such column: test2.f1}}
551d37e284Sdrhdo_test update-3.4 {
561d37e284Sdrh  set v [catch {execsql {UPDATE test1 SET f3=f1*2 WHERE f2==32}} msg]
571d37e284Sdrh  lappend v $msg
58967e8b73Sdrh} {1 {no such column: f3}}
591d37e284Sdrh
601d37e284Sdrh# Actually do some updates
611d37e284Sdrh#
621d37e284Sdrhdo_test update-3.5 {
631d37e284Sdrh  execsql {UPDATE test1 SET f2=f2*3}
641bee3d7bSdrh} {}
6580106e5cSdrhdo_test update-3.5.1 {
6680106e5cSdrh  db changes
6780106e5cSdrh} {10}
6880106e5cSdrh
6980106e5cSdrh# verify that SELECT does not reset the change counter
7080106e5cSdrhdo_test update-3.5.2 {
7180106e5cSdrh  db eval {SELECT count(*) FROM test1}
7280106e5cSdrh} {10}
7380106e5cSdrhdo_test update-3.5.3 {
7480106e5cSdrh  db changes
7580106e5cSdrh} {10}
7680106e5cSdrh
771bee3d7bSdrhdo_test update-3.6 {
781d37e284Sdrh  execsql {SELECT * FROM test1 ORDER BY f1}
791d37e284Sdrh} {1 6 2 12 3 24 4 48 5 96 6 192 7 384 8 768 9 1536 10 3072}
801bee3d7bSdrhdo_test update-3.7 {
811bee3d7bSdrh  execsql {PRAGMA count_changes=on}
821d37e284Sdrh  execsql {UPDATE test1 SET f2=f2/3 WHERE f1<=5}
831bee3d7bSdrh} {5}
841bee3d7bSdrhdo_test update-3.8 {
851d37e284Sdrh  execsql {SELECT * FROM test1 ORDER BY f1}
861d37e284Sdrh} {1 2 2 4 3 8 4 16 5 32 6 192 7 384 8 768 9 1536 10 3072}
871bee3d7bSdrhdo_test update-3.9 {
881d37e284Sdrh  execsql {UPDATE test1 SET f2=f2/3 WHERE f1>5}
891bee3d7bSdrh} {5}
901bee3d7bSdrhdo_test update-3.10 {
911d37e284Sdrh  execsql {SELECT * FROM test1 ORDER BY f1}
921d37e284Sdrh} {1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024}
931d37e284Sdrh
941d37e284Sdrh# Swap the values of f1 and f2 for all elements
951d37e284Sdrh#
961bee3d7bSdrhdo_test update-3.11 {
976206d50aSdrh  execsql {UPDATE test1 SET F2=f1, F1=f2}
981bee3d7bSdrh} {10}
991bee3d7bSdrhdo_test update-3.12 {
1006206d50aSdrh  execsql {SELECT * FROM test1 ORDER BY F1}
1011d37e284Sdrh} {2 1 4 2 8 3 16 4 32 5 64 6 128 7 256 8 512 9 1024 10}
1021bee3d7bSdrhdo_test update-3.13 {
1031bee3d7bSdrh  execsql {PRAGMA count_changes=off}
1044f3c63e4Sdrh  execsql {UPDATE test1 SET F2=f1, F1=f2}
1051bee3d7bSdrh} {}
1061bee3d7bSdrhdo_test update-3.14 {
1074f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY F1}
1084f3c63e4Sdrh} {1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024}
1094f3c63e4Sdrh
1104f3c63e4Sdrh# Create duplicate entries and make sure updating still
1114f3c63e4Sdrh# works.
1124f3c63e4Sdrh#
1134f3c63e4Sdrhdo_test update-4.0 {
1144f3c63e4Sdrh  execsql {
1154f3c63e4Sdrh    DELETE FROM test1 WHERE f1<=5;
1164f3c63e4Sdrh    INSERT INTO test1(f1,f2) VALUES(8,88);
1174f3c63e4Sdrh    INSERT INTO test1(f1,f2) VALUES(8,888);
1184f3c63e4Sdrh    INSERT INTO test1(f1,f2) VALUES(77,128);
1194f3c63e4Sdrh    INSERT INTO test1(f1,f2) VALUES(777,128);
1204f3c63e4Sdrh  }
1214f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
1224f3c63e4Sdrh} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
1234f3c63e4Sdrhdo_test update-4.1 {
1244f3c63e4Sdrh  execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8}
1254f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
1264f3c63e4Sdrh} {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128}
1274f3c63e4Sdrhdo_test update-4.2 {
1284f3c63e4Sdrh  execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800}
1294f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
1304f3c63e4Sdrh} {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128}
1314f3c63e4Sdrhdo_test update-4.3 {
1324f3c63e4Sdrh  execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800}
1334f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
1344f3c63e4Sdrh} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
1354f3c63e4Sdrhdo_test update-4.4 {
1364f3c63e4Sdrh  execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128}
1374f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
1384f3c63e4Sdrh} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128}
1394f3c63e4Sdrhdo_test update-4.5 {
1404f3c63e4Sdrh  execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128}
1414f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
1424f3c63e4Sdrh} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128}
1434f3c63e4Sdrhdo_test update-4.6 {
1441bee3d7bSdrh  execsql {
1451bee3d7bSdrh    PRAGMA count_changes=on;
1461bee3d7bSdrh    UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128;
1471bee3d7bSdrh  }
1481bee3d7bSdrh} {2}
1491bee3d7bSdrhdo_test update-4.7 {
1501bee3d7bSdrh  execsql {
1511bee3d7bSdrh    PRAGMA count_changes=off;
1521bee3d7bSdrh    SELECT * FROM test1 ORDER BY f1,f2
1531bee3d7bSdrh  }
1544f3c63e4Sdrh} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
1554f3c63e4Sdrh
1564f3c63e4Sdrh# Repeat the previous sequence of tests with an index.
1574f3c63e4Sdrh#
1584f3c63e4Sdrhdo_test update-5.0 {
1594f3c63e4Sdrh  execsql {CREATE INDEX idx1 ON test1(f1)}
1604f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
1614f3c63e4Sdrh} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
1624f3c63e4Sdrhdo_test update-5.1 {
1634f3c63e4Sdrh  execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8}
1644f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
1654f3c63e4Sdrh} {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128}
1664f3c63e4Sdrhdo_test update-5.2 {
1674f3c63e4Sdrh  execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800}
1684f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
1694f3c63e4Sdrh} {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128}
1704f3c63e4Sdrhdo_test update-5.3 {
1714f3c63e4Sdrh  execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800}
1724f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
1734f3c63e4Sdrh} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
1744f3c63e4Sdrhdo_test update-5.4 {
1754f3c63e4Sdrh  execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128}
1764f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
1774f3c63e4Sdrh} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128}
1784f3c63e4Sdrhdo_test update-5.4.1 {
1794f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
1804f3c63e4Sdrh} {78 128}
1814f3c63e4Sdrhdo_test update-5.4.2 {
1824f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
1834f3c63e4Sdrh} {778 128}
1844f3c63e4Sdrhdo_test update-5.4.3 {
1854f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
1864f3c63e4Sdrh} {8 88 8 128 8 256 8 888}
1874f3c63e4Sdrhdo_test update-5.5 {
1884f3c63e4Sdrh  execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128}
1891bee3d7bSdrh} {}
1901bee3d7bSdrhdo_test update-5.5.1 {
1914f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
1924f3c63e4Sdrh} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128}
1931bee3d7bSdrhdo_test update-5.5.2 {
1944f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
1954f3c63e4Sdrh} {78 128}
1961bee3d7bSdrhdo_test update-5.5.3 {
1974f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
1984f3c63e4Sdrh} {}
1991bee3d7bSdrhdo_test update-5.5.4 {
2004f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
2014f3c63e4Sdrh} {777 128}
2021bee3d7bSdrhdo_test update-5.5.5 {
2034f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
2044f3c63e4Sdrh} {8 88 8 128 8 256 8 888}
2054f3c63e4Sdrhdo_test update-5.6 {
2061bee3d7bSdrh  execsql {
2071bee3d7bSdrh    PRAGMA count_changes=on;
2081bee3d7bSdrh    UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128;
2091bee3d7bSdrh  }
2101bee3d7bSdrh} {2}
2114f3c63e4Sdrhdo_test update-5.6.1 {
2121bee3d7bSdrh  execsql {
2131bee3d7bSdrh    PRAGMA count_changes=off;
2141bee3d7bSdrh    SELECT * FROM test1 ORDER BY f1,f2
2151bee3d7bSdrh  }
2161bee3d7bSdrh} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
2171bee3d7bSdrhdo_test update-5.6.2 {
2184f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==77 ORDER BY f1,f2}
2194f3c63e4Sdrh} {77 128}
2201bee3d7bSdrhdo_test update-5.6.3 {
2214f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
2224f3c63e4Sdrh} {}
2231bee3d7bSdrhdo_test update-5.6.4 {
2244f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
2254f3c63e4Sdrh} {777 128}
2261bee3d7bSdrhdo_test update-5.6.5 {
2274f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
2284f3c63e4Sdrh} {8 88 8 256 8 888}
2294f3c63e4Sdrh
2304f3c63e4Sdrh# Repeat the previous sequence of tests with a different index.
2314f3c63e4Sdrh#
2324303feedSdrhexecsql {PRAGMA synchronous=FULL}
2334f3c63e4Sdrhdo_test update-6.0 {
2344f3c63e4Sdrh  execsql {DROP INDEX idx1}
2354f3c63e4Sdrh  execsql {CREATE INDEX idx1 ON test1(f2)}
2364f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
2374f3c63e4Sdrh} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
2384f3c63e4Sdrhdo_test update-6.1 {
2394f3c63e4Sdrh  execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8}
2404f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
2414f3c63e4Sdrh} {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128}
2424f3c63e4Sdrhdo_test update-6.1.1 {
2434f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
2444f3c63e4Sdrh} {8 89 8 257 8 889}
2454f3c63e4Sdrhdo_test update-6.1.2 {
2464f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2}
2474f3c63e4Sdrh} {8 89}
2484f3c63e4Sdrhdo_test update-6.1.3 {
2494f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==88 ORDER BY f1,f2}
2504f3c63e4Sdrh} {}
2514f3c63e4Sdrhdo_test update-6.2 {
2524f3c63e4Sdrh  execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800}
2534f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
2544f3c63e4Sdrh} {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128}
2554f3c63e4Sdrhdo_test update-6.3 {
2564f3c63e4Sdrh  execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800}
2574f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
2584f3c63e4Sdrh} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
2594f3c63e4Sdrhdo_test update-6.3.1 {
2604f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
2614f3c63e4Sdrh} {8 88 8 256 8 888}
2624f3c63e4Sdrhdo_test update-6.3.2 {
2634f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2}
2644f3c63e4Sdrh} {}
2654f3c63e4Sdrhdo_test update-6.3.3 {
2664f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f2==88 ORDER BY f1,f2}
2674f3c63e4Sdrh} {8 88}
2684f3c63e4Sdrhdo_test update-6.4 {
2694f3c63e4Sdrh  execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128}
2704f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
2714f3c63e4Sdrh} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128}
2724f3c63e4Sdrhdo_test update-6.4.1 {
2734f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
2744f3c63e4Sdrh} {78 128}
2754f3c63e4Sdrhdo_test update-6.4.2 {
2764f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
2774f3c63e4Sdrh} {778 128}
2784f3c63e4Sdrhdo_test update-6.4.3 {
2794f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
2804f3c63e4Sdrh} {8 88 8 128 8 256 8 888}
2814f3c63e4Sdrhdo_test update-6.5 {
2824f3c63e4Sdrh  execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128}
2834f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
2844f3c63e4Sdrh} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128}
2854f3c63e4Sdrhdo_test update-6.5.1 {
2864f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
2874f3c63e4Sdrh} {78 128}
2884f3c63e4Sdrhdo_test update-6.5.2 {
2894f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
2904f3c63e4Sdrh} {}
2914f3c63e4Sdrhdo_test update-6.5.3 {
2924f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
2934f3c63e4Sdrh} {777 128}
2944f3c63e4Sdrhdo_test update-6.5.4 {
2954f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
2964f3c63e4Sdrh} {8 88 8 128 8 256 8 888}
2974f3c63e4Sdrhdo_test update-6.6 {
2984f3c63e4Sdrh  execsql {UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128}
2994f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
3004f3c63e4Sdrh} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
3014f3c63e4Sdrhdo_test update-6.6.1 {
3024f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==77 ORDER BY f1,f2}
3034f3c63e4Sdrh} {77 128}
3044f3c63e4Sdrhdo_test update-6.6.2 {
3054f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
3064f3c63e4Sdrh} {}
3074f3c63e4Sdrhdo_test update-6.6.3 {
3084f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
3094f3c63e4Sdrh} {777 128}
3104f3c63e4Sdrhdo_test update-6.6.4 {
3114f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
3124f3c63e4Sdrh} {8 88 8 256 8 888}
3134f3c63e4Sdrh
3144f3c63e4Sdrh# Repeat the previous sequence of tests with multiple
3154f3c63e4Sdrh# indices
3164f3c63e4Sdrh#
3174f3c63e4Sdrhdo_test update-7.0 {
3184f3c63e4Sdrh  execsql {CREATE INDEX idx2 ON test1(f2)}
3194f3c63e4Sdrh  execsql {CREATE INDEX idx3 ON test1(f1,f2)}
3204f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
3214f3c63e4Sdrh} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
3224f3c63e4Sdrhdo_test update-7.1 {
3234f3c63e4Sdrh  execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8}
3244f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
3254f3c63e4Sdrh} {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128}
3264f3c63e4Sdrhdo_test update-7.1.1 {
3274f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
3284f3c63e4Sdrh} {8 89 8 257 8 889}
3294f3c63e4Sdrhdo_test update-7.1.2 {
3304f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2}
3314f3c63e4Sdrh} {8 89}
3324f3c63e4Sdrhdo_test update-7.1.3 {
3334f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==88 ORDER BY f1,f2}
3344f3c63e4Sdrh} {}
3354f3c63e4Sdrhdo_test update-7.2 {
3364f3c63e4Sdrh  execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800}
3374f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
3384f3c63e4Sdrh} {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128}
3394f3c63e4Sdrhdo_test update-7.3 {
34004096485Sdrh  # explain {UPDATE test1 SET f2=f2-1 WHERE f1==8 and F2<300}
3414f3c63e4Sdrh  execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800}
3424f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
3434f3c63e4Sdrh} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
3444f3c63e4Sdrhdo_test update-7.3.1 {
3454f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
3464f3c63e4Sdrh} {8 88 8 256 8 888}
3474f3c63e4Sdrhdo_test update-7.3.2 {
3484f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2}
3494f3c63e4Sdrh} {}
3504f3c63e4Sdrhdo_test update-7.3.3 {
3514f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f2==88 ORDER BY f1,f2}
3524f3c63e4Sdrh} {8 88}
3534f3c63e4Sdrhdo_test update-7.4 {
3544f3c63e4Sdrh  execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128}
3554f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
3564f3c63e4Sdrh} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128}
3574f3c63e4Sdrhdo_test update-7.4.1 {
3584f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
3594f3c63e4Sdrh} {78 128}
3604f3c63e4Sdrhdo_test update-7.4.2 {
3614f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
3624f3c63e4Sdrh} {778 128}
3634f3c63e4Sdrhdo_test update-7.4.3 {
3644f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
3654f3c63e4Sdrh} {8 88 8 128 8 256 8 888}
3664f3c63e4Sdrhdo_test update-7.5 {
3674f3c63e4Sdrh  execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128}
3684f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
3694f3c63e4Sdrh} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128}
3704f3c63e4Sdrhdo_test update-7.5.1 {
3714f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
3724f3c63e4Sdrh} {78 128}
3734f3c63e4Sdrhdo_test update-7.5.2 {
3744f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
3754f3c63e4Sdrh} {}
3764f3c63e4Sdrhdo_test update-7.5.3 {
3774f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
3784f3c63e4Sdrh} {777 128}
3794f3c63e4Sdrhdo_test update-7.5.4 {
3804f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
3814f3c63e4Sdrh} {8 88 8 128 8 256 8 888}
3824f3c63e4Sdrhdo_test update-7.6 {
3834f3c63e4Sdrh  execsql {UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128}
3844f3c63e4Sdrh  execsql {SELECT * FROM test1 ORDER BY f1,f2}
3854f3c63e4Sdrh} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
3864f3c63e4Sdrhdo_test update-7.6.1 {
3874f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==77 ORDER BY f1,f2}
3884f3c63e4Sdrh} {77 128}
3894f3c63e4Sdrhdo_test update-7.6.2 {
3904f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
3914f3c63e4Sdrh} {}
3924f3c63e4Sdrhdo_test update-7.6.3 {
3934f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
3944f3c63e4Sdrh} {777 128}
3954f3c63e4Sdrhdo_test update-7.6.4 {
3964f3c63e4Sdrh  execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
3974f3c63e4Sdrh} {8 88 8 256 8 888}
3981d37e284Sdrh
399c837e709Sdrh# Error messages
400c837e709Sdrh#
4014f3c63e4Sdrhdo_test update-9.1 {
402c837e709Sdrh  set v [catch {execsql {
403c837e709Sdrh    UPDATE test1 SET x=11 WHERE f1=1025
404c837e709Sdrh  }} msg]
405c837e709Sdrh  lappend v $msg
406967e8b73Sdrh} {1 {no such column: x}}
4074f3c63e4Sdrhdo_test update-9.2 {
408c837e709Sdrh  set v [catch {execsql {
409c837e709Sdrh    UPDATE test1 SET f1=x(11) WHERE f1=1025
410c837e709Sdrh  }} msg]
411c837e709Sdrh  lappend v $msg
412c837e709Sdrh} {1 {no such function: x}}
4134f3c63e4Sdrhdo_test update-9.3 {
414c837e709Sdrh  set v [catch {execsql {
415c837e709Sdrh    UPDATE test1 SET f1=11 WHERE x=1025
416c837e709Sdrh  }} msg]
417c837e709Sdrh  lappend v $msg
418967e8b73Sdrh} {1 {no such column: x}}
4194f3c63e4Sdrhdo_test update-9.4 {
420c837e709Sdrh  set v [catch {execsql {
421c837e709Sdrh    UPDATE test1 SET f1=11 WHERE x(f1)=1025
422c837e709Sdrh  }} msg]
423c837e709Sdrh  lappend v $msg
424c837e709Sdrh} {1 {no such function: x}}
4251d37e284Sdrh
42679b0c956Sdrh# Try doing updates on a unique column where the value does not
42779b0c956Sdrh# really change.
42879b0c956Sdrh#
42979b0c956Sdrhdo_test update-10.1 {
43079b0c956Sdrh  execsql {
43179b0c956Sdrh    DROP TABLE test1;
43279b0c956Sdrh    CREATE TABLE t1(
43379b0c956Sdrh       a integer primary key,
43479b0c956Sdrh       b UNIQUE,
43579b0c956Sdrh       c, d,
43679b0c956Sdrh       e, f,
43779b0c956Sdrh       UNIQUE(c,d)
43879b0c956Sdrh    );
43979b0c956Sdrh    INSERT INTO t1 VALUES(1,2,3,4,5,6);
44079b0c956Sdrh    INSERT INTO t1 VALUES(2,3,4,4,6,7);
44179b0c956Sdrh    SELECT * FROM t1
44279b0c956Sdrh  }
44379b0c956Sdrh} {1 2 3 4 5 6 2 3 4 4 6 7}
44479b0c956Sdrhdo_test update-10.2 {
44579b0c956Sdrh  catchsql {
44679b0c956Sdrh    UPDATE t1 SET a=1, e=9 WHERE f=6;
44779b0c956Sdrh    SELECT * FROM t1;
44879b0c956Sdrh  }
44979b0c956Sdrh} {0 {1 2 3 4 9 6 2 3 4 4 6 7}}
45079b0c956Sdrhdo_test update-10.3 {
45179b0c956Sdrh  catchsql {
45279b0c956Sdrh    UPDATE t1 SET a=1, e=10 WHERE f=7;
45379b0c956Sdrh    SELECT * FROM t1;
45479b0c956Sdrh  }
455f9c8ce3cSdrh} {1 {UNIQUE constraint failed: t1.a}}
45679b0c956Sdrhdo_test update-10.4 {
45779b0c956Sdrh  catchsql {
45879b0c956Sdrh    SELECT * FROM t1;
45979b0c956Sdrh  }
46079b0c956Sdrh} {0 {1 2 3 4 9 6 2 3 4 4 6 7}}
46179b0c956Sdrhdo_test update-10.5 {
46279b0c956Sdrh  catchsql {
46379b0c956Sdrh    UPDATE t1 SET b=2, e=11 WHERE f=6;
46479b0c956Sdrh    SELECT * FROM t1;
46579b0c956Sdrh  }
46679b0c956Sdrh} {0 {1 2 3 4 11 6 2 3 4 4 6 7}}
46779b0c956Sdrhdo_test update-10.6 {
46879b0c956Sdrh  catchsql {
46979b0c956Sdrh    UPDATE t1 SET b=2, e=12 WHERE f=7;
47079b0c956Sdrh    SELECT * FROM t1;
47179b0c956Sdrh  }
472f9c8ce3cSdrh} {1 {UNIQUE constraint failed: t1.b}}
47379b0c956Sdrhdo_test update-10.7 {
47479b0c956Sdrh  catchsql {
47579b0c956Sdrh    SELECT * FROM t1;
47679b0c956Sdrh  }
47779b0c956Sdrh} {0 {1 2 3 4 11 6 2 3 4 4 6 7}}
47879b0c956Sdrhdo_test update-10.8 {
47979b0c956Sdrh  catchsql {
48079b0c956Sdrh    UPDATE t1 SET c=3, d=4, e=13 WHERE f=6;
48179b0c956Sdrh    SELECT * FROM t1;
48279b0c956Sdrh  }
48379b0c956Sdrh} {0 {1 2 3 4 13 6 2 3 4 4 6 7}}
48479b0c956Sdrhdo_test update-10.9 {
48579b0c956Sdrh  catchsql {
48679b0c956Sdrh    UPDATE t1 SET c=3, d=4, e=14 WHERE f=7;
48779b0c956Sdrh    SELECT * FROM t1;
48879b0c956Sdrh  }
489f9c8ce3cSdrh} {1 {UNIQUE constraint failed: t1.c, t1.d}}
49079b0c956Sdrhdo_test update-10.10 {
49179b0c956Sdrh  catchsql {
49279b0c956Sdrh    SELECT * FROM t1;
49379b0c956Sdrh  }
49479b0c956Sdrh} {0 {1 2 3 4 13 6 2 3 4 4 6 7}}
4951d37e284Sdrh
49653e3fc70Sdrh# Make sure we can handle a subquery in the where clause.
49753e3fc70Sdrh#
4983e8c37e7Sdanielk1977ifcapable subquery {
49953e3fc70Sdrh  do_test update-11.1 {
50053e3fc70Sdrh    execsql {
50153e3fc70Sdrh      UPDATE t1 SET e=e+1 WHERE b IN (SELECT b FROM t1);
50253e3fc70Sdrh      SELECT b,e FROM t1;
50353e3fc70Sdrh    }
50453e3fc70Sdrh  } {2 14 3 7}
50553e3fc70Sdrh  do_test update-11.2 {
50653e3fc70Sdrh    execsql {
50753e3fc70Sdrh      UPDATE t1 SET e=e+1 WHERE a IN (SELECT a FROM t1);
50853e3fc70Sdrh      SELECT a,e FROM t1;
50953e3fc70Sdrh    }
51053e3fc70Sdrh  } {1 15 2 8}
5115e3a6ebfSdrh  do_test update-11.3 {
5125e3a6ebfSdrh    execsql {
5135e3a6ebfSdrh      UPDATE t1 AS xyz SET e=e+1 WHERE xyz.a IN (SELECT a FROM t1);
5145e3a6ebfSdrh      SELECT a,e FROM t1;
5155e3a6ebfSdrh    }
5165e3a6ebfSdrh  } {1 16 2 9}
5175e3a6ebfSdrh  do_test update-11.4 {
5185e3a6ebfSdrh    execsql {
5195e3a6ebfSdrh      UPDATE t1 AS xyz SET e=e+1 WHERE EXISTS(SELECT 1 FROM t1 WHERE t1.a<xyz.a);
5205e3a6ebfSdrh      SELECT a,e FROM t1;
5215e3a6ebfSdrh    }
5225e3a6ebfSdrh  } {1 16 2 10}
5233e8c37e7Sdanielk1977}
52453e3fc70Sdrh
525ed717fe3Sdrhintegrity_check update-12.1
526ed717fe3Sdrh
527fb044c1dSdrh# Ticket 602.  Updates should occur in the same order as the records
528fb044c1dSdrh# were discovered in the WHERE clause.
529fb044c1dSdrh#
530fb044c1dSdrhdo_test update-13.1 {
531fb044c1dSdrh  execsql {
532fb044c1dSdrh    BEGIN;
533fb044c1dSdrh    CREATE TABLE t2(a);
534fb044c1dSdrh    INSERT INTO t2 VALUES(1);
535fb044c1dSdrh    INSERT INTO t2 VALUES(2);
536fb044c1dSdrh    INSERT INTO t2 SELECT a+2 FROM t2;
537fb044c1dSdrh    INSERT INTO t2 SELECT a+4 FROM t2;
538fb044c1dSdrh    INSERT INTO t2 SELECT a+8 FROM t2;
539fb044c1dSdrh    INSERT INTO t2 SELECT a+16 FROM t2;
540fb044c1dSdrh    INSERT INTO t2 SELECT a+32 FROM t2;
541fb044c1dSdrh    INSERT INTO t2 SELECT a+64 FROM t2;
542fb044c1dSdrh    INSERT INTO t2 SELECT a+128 FROM t2;
543fb044c1dSdrh    INSERT INTO t2 SELECT a+256 FROM t2;
544fb044c1dSdrh    INSERT INTO t2 SELECT a+512 FROM t2;
545fb044c1dSdrh    INSERT INTO t2 SELECT a+1024 FROM t2;
546fb044c1dSdrh    COMMIT;
547fb044c1dSdrh    SELECT count(*) FROM t2;
548fb044c1dSdrh  }
549fb044c1dSdrh} {2048}
550fb044c1dSdrhdo_test update-13.2 {
551fb044c1dSdrh  execsql {
552fb044c1dSdrh    SELECT count(*) FROM t2 WHERE a=rowid;
553fb044c1dSdrh  }
554fb044c1dSdrh} {2048}
555fb044c1dSdrhdo_test update-13.3 {
556fb044c1dSdrh  execsql {
557fb044c1dSdrh    UPDATE t2 SET rowid=rowid-1;
558fb044c1dSdrh    SELECT count(*) FROM t2 WHERE a=rowid+1;
559fb044c1dSdrh  }
560fb044c1dSdrh} {2048}
561fb044c1dSdrhdo_test update-13.3 {
562fb044c1dSdrh  execsql {
563fb044c1dSdrh    UPDATE t2 SET rowid=rowid+10000;
564fb044c1dSdrh    UPDATE t2 SET rowid=rowid-9999;
565fb044c1dSdrh    SELECT count(*) FROM t2 WHERE a=rowid;
566fb044c1dSdrh  }
567fb044c1dSdrh} {2048}
568fb044c1dSdrhdo_test update-13.4 {
569fb044c1dSdrh  execsql {
570fb044c1dSdrh    BEGIN;
571fb044c1dSdrh    INSERT INTO t2 SELECT a+2048 FROM t2;
572fb044c1dSdrh    INSERT INTO t2 SELECT a+4096 FROM t2;
573fb044c1dSdrh    INSERT INTO t2 SELECT a+8192 FROM t2;
574fb044c1dSdrh    SELECT count(*) FROM t2 WHERE a=rowid;
575fb044c1dSdrh    COMMIT;
576fb044c1dSdrh  }
577fb044c1dSdrh} 16384
578fb044c1dSdrhdo_test update-13.5 {
579fb044c1dSdrh  execsql {
580fb044c1dSdrh    UPDATE t2 SET rowid=rowid-1;
581fb044c1dSdrh    SELECT count(*) FROM t2 WHERE a=rowid+1;
582fb044c1dSdrh  }
583fb044c1dSdrh} 16384
584fb044c1dSdrh
585fb044c1dSdrhintegrity_check update-13.6
586fb044c1dSdrh
5878b07c717Sdrhifcapable {trigger} {
5888b07c717Sdrh# Test for proper detection of malformed WHEN clauses on UPDATE triggers.
5898b07c717Sdrh#
5908b07c717Sdrhdo_test update-14.1 {
5918b07c717Sdrh  execsql {
5928b07c717Sdrh    CREATE TABLE t3(a,b,c);
5938b07c717Sdrh    CREATE TRIGGER t3r1 BEFORE UPDATE on t3 WHEN nosuchcol BEGIN
5948b07c717Sdrh      SELECT 'illegal WHEN clause';
5958b07c717Sdrh    END;
5968b07c717Sdrh  }
5978b07c717Sdrh} {}
5988b07c717Sdrhdo_test update-14.2 {
5998b07c717Sdrh  catchsql {
6008b07c717Sdrh    UPDATE t3 SET a=1;
6018b07c717Sdrh  }
6028b07c717Sdrh} {1 {no such column: nosuchcol}}
6038b07c717Sdrhdo_test update-14.3 {
6048b07c717Sdrh  execsql {
6058b07c717Sdrh    CREATE TABLE t4(a,b,c);
6068b07c717Sdrh    CREATE TRIGGER t4r1 AFTER UPDATE on t4 WHEN nosuchcol BEGIN
6078b07c717Sdrh      SELECT 'illegal WHEN clause';
6088b07c717Sdrh    END;
6098b07c717Sdrh  }
6108b07c717Sdrh} {}
6118b07c717Sdrhdo_test update-14.4 {
6128b07c717Sdrh  catchsql {
6138b07c717Sdrh    UPDATE t4 SET a=1;
6148b07c717Sdrh  }
6158b07c717Sdrh} {1 {no such column: nosuchcol}}
6168b07c717Sdrh
6178b07c717Sdrh} ;# ifcapable {trigger}
6188b07c717Sdrh
61922e8d833Sdrh# Ticket [https://www.sqlite.org/src/tktview/43107840f1c02] on 2014-10-29
62022e8d833Sdrh# An assertion fault on UPDATE
62122e8d833Sdrh#
62237f3ac8fSdanifcapable altertable {
62322e8d833Sdrh  do_execsql_test update-15.1 {
62422e8d833Sdrh    CREATE TABLE t15(a INTEGER PRIMARY KEY, b);
62522e8d833Sdrh    INSERT INTO t15(a,b) VALUES(10,'abc'),(20,'def'),(30,'ghi');
62622e8d833Sdrh    ALTER TABLE t15 ADD COLUMN c;
62722e8d833Sdrh    CREATE INDEX t15c ON t15(c);
62822e8d833Sdrh    INSERT INTO t15(a,b)
62922e8d833Sdrh      VALUES(5,'zyx'),(15,'wvu'),(25,'tsr'),(35,'qpo');
630*7d44b22dSdrh    UPDATE t15 SET c=printf('y%d',a) WHERE c IS NULL;
63122e8d833Sdrh    SELECT a,b,c,'|' FROM t15 ORDER BY a;
63222e8d833Sdrh  } {5 zyx y5 | 10 abc y10 | 15 wvu y15 | 20 def y20 | 25 tsr y25 | 30 ghi y30 | 35 qpo y35 |}
63337f3ac8fSdan}
63422e8d833Sdrh
635bb6b1ca7Sdrh# Unreleased bug in UPDATE caused by the UPSERT changes.
636bb6b1ca7Sdrh# Found by OSSFuzz as soon as the UPSERT changes landed on trunk.
637bb6b1ca7Sdrh# Never released into the wild.  2018-04-19.
638bb6b1ca7Sdrh#
639bb6b1ca7Sdrhdo_execsql_test update-16.1 {
640bb6b1ca7Sdrh  CREATE TABLE t16(a INTEGER PRIMARY KEY ON CONFLICT REPLACE, b UNIQUE);
641bb6b1ca7Sdrh  INSERT INTO t16(a,b) VALUES(1,2),(3,4),(5,6);
642bb6b1ca7Sdrh  UPDATE t16 SET a=a;
643bb6b1ca7Sdrh  SELECT * FROM t16 ORDER BY +a;
644bb6b1ca7Sdrh} {1 2 3 4 5 6}
645fb044c1dSdrh
6467a969845Sdrh# 2019-12-09 gramfuzz find
6477a969845Sdrh# If a partial index that does not reference any column of its table (which is you
6487a969845Sdrh# must admit is a very strange index, but one that is allowed) is used by an UPDATE
6497a969845Sdrh# statement, void the use of OP_DeferredSeek on the main loop, as the seek will not
6507a969845Sdrh# be resolved prior to the OP_Delete.
6517a969845Sdrh#
6527a969845Sdrhdo_execsql_test update-17.10 {
6537a969845Sdrh  DROP TABLE IF EXISTS t1;
6547a969845Sdrh  CREATE TABLE t1(x,y);
6557a969845Sdrh  INSERT INTO t1(x) VALUES(1);
6567a969845Sdrh  CREATE INDEX t1x1 ON t1(1) WHERE 3;
6577a969845Sdrh  UPDATE t1 SET x=2, y=3 WHERE 3;
6587a969845Sdrh  SELECT * FROM t1;
6597a969845Sdrh} {2 3}
6607a969845Sdrh
6615d762b2dSdrh# 2019-12-22 ticket 5ad2aa6921faa1ee
6625d762b2dSdrh# Make a hard-copy of values that need to be run through OP_RealAffinity
6635d762b2dSdrh# rather than a soft-copy.  This is not strictly necessary, but it avoids
6645d762b2dSdrh# a memory-accounting assert().
6655d762b2dSdrh#
6665d762b2dSdrhreset_db
6675d762b2dSdrhdo_execsql_test update-18.10 {
6685d762b2dSdrh  PRAGMA encoding = 'UTF16';
6695d762b2dSdrh  CREATE TABLE t0(c0 REAL, c1);
6705d762b2dSdrh  INSERT INTO t0(c0,c1) VALUES('xyz',11),('uvw',22);
6715d762b2dSdrh  CREATE INDEX i0 ON t0(c1) WHERE c0 GLOB 3;
6725d762b2dSdrh  CREATE INDEX i1 ON t0(c0,c1) WHERE typeof(c0)='text' AND typeof(c1)='integer';
6735d762b2dSdrh  UPDATE t0 SET c1=345;
6745d762b2dSdrh  SELECT * FROM t0;
6755d762b2dSdrh} {xyz 345 uvw 345}
6767a969845Sdrh
67713d79502Sdrh# 2019-12-22 ticket c62c5e58524b204d
67813d79502Sdrh# This is really the same underlying problem as 5ad2aa6921faa1ee
67913d79502Sdrh#
68013d79502Sdrhreset_db
68113d79502Sdrhdo_execsql_test update-18.20 {
68213d79502Sdrh  PRAGMA encoding = 'utf16';
68313d79502Sdrh  CREATE TABLE t0(c0 TEXT);
68413d79502Sdrh  CREATE INDEX i0 ON t0(0 LIKE COALESCE(c0, 0));
68513d79502Sdrh  INSERT INTO t0(c0) VALUES (0), (0);
68613d79502Sdrh  SELECT * FROM t0;
68713d79502Sdrh} {0 0}
68813d79502Sdrh
689be3da241Sdrh# 2019-12-28 assertion fault reported by Yongheng
690be3da241Sdrh# Similar to ticket ec8abb025e78f40c
691be3da241Sdrh# An UPDATE was reaching the OP_Delete after running OP_DeferredSeek
692be3da241Sdrh# without ever hitting an OP_Column. The enhanced solution is to
693be3da241Sdrh# fix OP_Delete so that it can do the seek itself.
694be3da241Sdrh#
695be3da241Sdrhreset_db
696be3da241Sdrhdo_execsql_test update-19.10 {
697be3da241Sdrh  CREATE TABLE t1(
698be3da241Sdrh   a TEXT,
699be3da241Sdrh   b INTEGER PRIMARY KEY UNIQUE
700be3da241Sdrh  );
701be3da241Sdrh  INSERT INTO t1 VALUES(1,2);
702be3da241Sdrh  UPDATE t1 SET a = quote(b) WHERE b>=2;
703be3da241Sdrh  SELECT * FROM t1;
704be3da241Sdrh} {2 2}
705be3da241Sdrh
7067b14b65dSdrh# 2019-12-29 ticket https://www.sqlite.org/src/info/314cc133e5ada126
7077b14b65dSdrh# REPLACE conflict resolution during an UPDATE causes a DELETE trigger
7087b14b65dSdrh# to fire.  If that DELETE trigger subsequently modifies the row
7097b14b65dSdrh# being updated, bad things can happen.  Prevent this by prohibiting
7107b14b65dSdrh# triggers from making changes to the table being updated while doing
7117b14b65dSdrh# REPLACE conflict resolution on the UPDATE.
7127b14b65dSdrh#
7137b14b65dSdrh# See also tickets:
7147b14b65dSdrh#   https://www.sqlite.org/src/info/c1e19e12046d23fe 2019-10-25
7157b14b65dSdrh#   https://www.sqlite.org/src/info/a8a4847a2d96f5de 2019-10-16
7167b14b65dSdrh#
7177b14b65dSdrhreset_db
7187b14b65dSdrhdo_execsql_test update-20.10 {
7197b14b65dSdrh  PRAGMA recursive_triggers = true;
7207b14b65dSdrh  CREATE TABLE t1(a UNIQUE ON CONFLICT REPLACE, b);
7217b14b65dSdrh  INSERT INTO t1(a,b) VALUES(4,12),(9,13);
7227b14b65dSdrh  CREATE INDEX i0 ON t1(b);
7237b14b65dSdrh  CREATE TRIGGER tr0 DELETE ON t1 BEGIN
7247b14b65dSdrh    UPDATE t1 SET b = a;
7257b14b65dSdrh  END;
7267b14b65dSdrh  PRAGMA integrity_check;
7277b14b65dSdrh} {ok}
7287b14b65dSdrhdo_catchsql_test update-20.20 {
7297b14b65dSdrh  UPDATE t1 SET a=0;
7307b14b65dSdrh} {1 {constraint failed}}
7317b14b65dSdrhdo_execsql_test update-20.30 {
7327b14b65dSdrh  PRAGMA integrity_check;
7337b14b65dSdrh} {ok}
7347b14b65dSdrh
7351d37e284Sdrhfinish_test
736