conflict.test 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. # 2002 January 29
  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. # This file implements regression tests for SQLite library.
  12. #
  13. # This file implements tests for the conflict resolution extension
  14. # to SQLite.
  15. #
  16. # $Id: conflict.test,v 1.32 2009/04/30 09:10:38 danielk1977 Exp $
  17. set testdir [file dirname $argv0]
  18. source $testdir/tester.tcl
  19. ifcapable !conflict {
  20. finish_test
  21. return
  22. }
  23. # Create tables for the first group of tests.
  24. #
  25. do_test conflict-1.0 {
  26. execsql {
  27. CREATE TABLE t1(a, b, c, UNIQUE(a,b));
  28. CREATE TABLE t2(x);
  29. SELECT c FROM t1 ORDER BY c;
  30. }
  31. } {}
  32. # Six columns of configuration data as follows:
  33. #
  34. # i The reference number of the test
  35. # cmd An INSERT or REPLACE command to execute against table t1
  36. # t0 True if there is an error from $cmd
  37. # t1 Content of "c" column of t1 assuming no error in $cmd
  38. # t2 Content of "x" column of t2
  39. # t3 Number of temporary files created by this test
  40. #
  41. foreach {i cmd t0 t1 t2 t3} {
  42. 1 INSERT 1 {} 1 0
  43. 2 {INSERT OR IGNORE} 0 3 1 0
  44. 3 {INSERT OR REPLACE} 0 4 1 0
  45. 4 REPLACE 0 4 1 0
  46. 5 {INSERT OR FAIL} 1 {} 1 0
  47. 6 {INSERT OR ABORT} 1 {} 1 0
  48. 7 {INSERT OR ROLLBACK} 1 {} {} 0
  49. } {
  50. do_test conflict-1.$i {
  51. set ::sqlite_opentemp_count 0
  52. set r0 [catch {execsql [subst {
  53. DELETE FROM t1;
  54. DELETE FROM t2;
  55. INSERT INTO t1 VALUES(1,2,3);
  56. BEGIN;
  57. INSERT INTO t2 VALUES(1);
  58. $cmd INTO t1 VALUES(1,2,4);
  59. }]} r1]
  60. catch {execsql {COMMIT}}
  61. if {$r0} {set r1 {}} {set r1 [execsql {SELECT c FROM t1}]}
  62. set r2 [execsql {SELECT x FROM t2}]
  63. set r3 $::sqlite_opentemp_count
  64. list $r0 $r1 $r2 $r3
  65. } [list $t0 $t1 $t2 $t3]
  66. }
  67. # Create tables for the first group of tests.
  68. #
  69. do_test conflict-2.0 {
  70. execsql {
  71. DROP TABLE t1;
  72. DROP TABLE t2;
  73. CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, UNIQUE(a,b));
  74. CREATE TABLE t2(x);
  75. SELECT c FROM t1 ORDER BY c;
  76. }
  77. } {}
  78. # Six columns of configuration data as follows:
  79. #
  80. # i The reference number of the test
  81. # cmd An INSERT or REPLACE command to execute against table t1
  82. # t0 True if there is an error from $cmd
  83. # t1 Content of "c" column of t1 assuming no error in $cmd
  84. # t2 Content of "x" column of t2
  85. #
  86. foreach {i cmd t0 t1 t2} {
  87. 1 INSERT 1 {} 1
  88. 2 {INSERT OR IGNORE} 0 3 1
  89. 3 {INSERT OR REPLACE} 0 4 1
  90. 4 REPLACE 0 4 1
  91. 5 {INSERT OR FAIL} 1 {} 1
  92. 6 {INSERT OR ABORT} 1 {} 1
  93. 7 {INSERT OR ROLLBACK} 1 {} {}
  94. } {
  95. do_test conflict-2.$i {
  96. set r0 [catch {execsql [subst {
  97. DELETE FROM t1;
  98. DELETE FROM t2;
  99. INSERT INTO t1 VALUES(1,2,3);
  100. BEGIN;
  101. INSERT INTO t2 VALUES(1);
  102. $cmd INTO t1 VALUES(1,2,4);
  103. }]} r1]
  104. catch {execsql {COMMIT}}
  105. if {$r0} {set r1 {}} {set r1 [execsql {SELECT c FROM t1}]}
  106. set r2 [execsql {SELECT x FROM t2}]
  107. list $r0 $r1 $r2
  108. } [list $t0 $t1 $t2]
  109. }
  110. # Create tables for the first group of tests.
  111. #
  112. do_test conflict-3.0 {
  113. execsql {
  114. DROP TABLE t1;
  115. DROP TABLE t2;
  116. CREATE TABLE t1(a, b, c INTEGER, PRIMARY KEY(c), UNIQUE(a,b));
  117. CREATE TABLE t2(x);
  118. SELECT c FROM t1 ORDER BY c;
  119. }
  120. } {}
  121. # Six columns of configuration data as follows:
  122. #
  123. # i The reference number of the test
  124. # cmd An INSERT or REPLACE command to execute against table t1
  125. # t0 True if there is an error from $cmd
  126. # t1 Content of "c" column of t1 assuming no error in $cmd
  127. # t2 Content of "x" column of t2
  128. #
  129. foreach {i cmd t0 t1 t2} {
  130. 1 INSERT 1 {} 1
  131. 2 {INSERT OR IGNORE} 0 3 1
  132. 3 {INSERT OR REPLACE} 0 4 1
  133. 4 REPLACE 0 4 1
  134. 5 {INSERT OR FAIL} 1 {} 1
  135. 6 {INSERT OR ABORT} 1 {} 1
  136. 7 {INSERT OR ROLLBACK} 1 {} {}
  137. } {
  138. do_test conflict-3.$i {
  139. set r0 [catch {execsql [subst {
  140. DELETE FROM t1;
  141. DELETE FROM t2;
  142. INSERT INTO t1 VALUES(1,2,3);
  143. BEGIN;
  144. INSERT INTO t2 VALUES(1);
  145. $cmd INTO t1 VALUES(1,2,4);
  146. }]} r1]
  147. catch {execsql {COMMIT}}
  148. if {$r0} {set r1 {}} {set r1 [execsql {SELECT c FROM t1}]}
  149. set r2 [execsql {SELECT x FROM t2}]
  150. list $r0 $r1 $r2
  151. } [list $t0 $t1 $t2]
  152. }
  153. do_test conflict-4.0 {
  154. execsql {
  155. DROP TABLE t2;
  156. CREATE TABLE t2(x);
  157. SELECT x FROM t2;
  158. }
  159. } {}
  160. # Six columns of configuration data as follows:
  161. #
  162. # i The reference number of the test
  163. # conf1 The conflict resolution algorithm on the UNIQUE constraint
  164. # cmd An INSERT or REPLACE command to execute against table t1
  165. # t0 True if there is an error from $cmd
  166. # t1 Content of "c" column of t1 assuming no error in $cmd
  167. # t2 Content of "x" column of t2
  168. #
  169. foreach {i conf1 cmd t0 t1 t2} {
  170. 1 {} INSERT 1 {} 1
  171. 2 REPLACE INSERT 0 4 1
  172. 3 IGNORE INSERT 0 3 1
  173. 4 FAIL INSERT 1 {} 1
  174. 5 ABORT INSERT 1 {} 1
  175. 6 ROLLBACK INSERT 1 {} {}
  176. 7 REPLACE {INSERT OR IGNORE} 0 3 1
  177. 8 IGNORE {INSERT OR REPLACE} 0 4 1
  178. 9 FAIL {INSERT OR IGNORE} 0 3 1
  179. 10 ABORT {INSERT OR REPLACE} 0 4 1
  180. 11 ROLLBACK {INSERT OR IGNORE } 0 3 1
  181. } {
  182. do_test conflict-4.$i {
  183. if {$conf1!=""} {set conf1 "ON CONFLICT $conf1"}
  184. set r0 [catch {execsql [subst {
  185. DROP TABLE t1;
  186. CREATE TABLE t1(a,b,c,UNIQUE(a,b) $conf1);
  187. DELETE FROM t2;
  188. INSERT INTO t1 VALUES(1,2,3);
  189. BEGIN;
  190. INSERT INTO t2 VALUES(1);
  191. $cmd INTO t1 VALUES(1,2,4);
  192. }]} r1]
  193. catch {execsql {COMMIT}}
  194. if {$r0} {set r1 {}} {set r1 [execsql {SELECT c FROM t1}]}
  195. set r2 [execsql {SELECT x FROM t2}]
  196. list $r0 $r1 $r2
  197. } [list $t0 $t1 $t2]
  198. }
  199. do_test conflict-5.0 {
  200. execsql {
  201. DROP TABLE t2;
  202. CREATE TABLE t2(x);
  203. SELECT x FROM t2;
  204. }
  205. } {}
  206. # Six columns of configuration data as follows:
  207. #
  208. # i The reference number of the test
  209. # conf1 The conflict resolution algorithm on the NOT NULL constraint
  210. # cmd An INSERT or REPLACE command to execute against table t1
  211. # t0 True if there is an error from $cmd
  212. # t1 Content of "c" column of t1 assuming no error in $cmd
  213. # t2 Content of "x" column of t2
  214. #
  215. foreach {i conf1 cmd t0 t1 t2} {
  216. 1 {} INSERT 1 {} 1
  217. 2 REPLACE INSERT 0 5 1
  218. 3 IGNORE INSERT 0 {} 1
  219. 4 FAIL INSERT 1 {} 1
  220. 5 ABORT INSERT 1 {} 1
  221. 6 ROLLBACK INSERT 1 {} {}
  222. 7 REPLACE {INSERT OR IGNORE} 0 {} 1
  223. 8 IGNORE {INSERT OR REPLACE} 0 5 1
  224. 9 FAIL {INSERT OR IGNORE} 0 {} 1
  225. 10 ABORT {INSERT OR REPLACE} 0 5 1
  226. 11 ROLLBACK {INSERT OR IGNORE} 0 {} 1
  227. 12 {} {INSERT OR IGNORE} 0 {} 1
  228. 13 {} {INSERT OR REPLACE} 0 5 1
  229. 14 {} {INSERT OR FAIL} 1 {} 1
  230. 15 {} {INSERT OR ABORT} 1 {} 1
  231. 16 {} {INSERT OR ROLLBACK} 1 {} {}
  232. } {
  233. if {$t0} {set t1 {t1.c may not be NULL}}
  234. do_test conflict-5.$i {
  235. if {$conf1!=""} {set conf1 "ON CONFLICT $conf1"}
  236. set r0 [catch {execsql [subst {
  237. DROP TABLE t1;
  238. CREATE TABLE t1(a,b,c NOT NULL $conf1 DEFAULT 5);
  239. DELETE FROM t2;
  240. BEGIN;
  241. INSERT INTO t2 VALUES(1);
  242. $cmd INTO t1 VALUES(1,2,NULL);
  243. }]} r1]
  244. catch {execsql {COMMIT}}
  245. if {!$r0} {set r1 [execsql {SELECT c FROM t1}]}
  246. set r2 [execsql {SELECT x FROM t2}]
  247. list $r0 $r1 $r2
  248. } [list $t0 $t1 $t2]
  249. }
  250. do_test conflict-6.0 {
  251. execsql {
  252. DROP TABLE t2;
  253. CREATE TABLE t2(a,b,c);
  254. INSERT INTO t2 VALUES(1,2,1);
  255. INSERT INTO t2 VALUES(2,3,2);
  256. INSERT INTO t2 VALUES(3,4,1);
  257. INSERT INTO t2 VALUES(4,5,4);
  258. SELECT c FROM t2 ORDER BY b;
  259. CREATE TABLE t3(x);
  260. INSERT INTO t3 VALUES(1);
  261. }
  262. } {1 2 1 4}
  263. # Six columns of configuration data as follows:
  264. #
  265. # i The reference number of the test
  266. # conf1 The conflict resolution algorithm on the UNIQUE constraint
  267. # cmd An UPDATE command to execute against table t1
  268. # t0 True if there is an error from $cmd
  269. # t1 Content of "b" column of t1 assuming no error in $cmd
  270. # t2 Content of "x" column of t3
  271. # t3 Number of temporary files for tables
  272. # t4 Number of temporary files for statement journals
  273. #
  274. # Update: Since temporary table files are now opened lazily, and none
  275. # of the following tests use large quantities of data, t3 is always 0.
  276. #
  277. foreach {i conf1 cmd t0 t1 t2 t3 t4} {
  278. 1 {} UPDATE 1 {6 7 8 9} 1 0 1
  279. 2 REPLACE UPDATE 0 {7 6 9} 1 0 0
  280. 3 IGNORE UPDATE 0 {6 7 3 9} 1 0 0
  281. 4 FAIL UPDATE 1 {6 7 3 4} 1 0 0
  282. 5 ABORT UPDATE 1 {1 2 3 4} 1 0 1
  283. 6 ROLLBACK UPDATE 1 {1 2 3 4} 0 0 0
  284. 7 REPLACE {UPDATE OR IGNORE} 0 {6 7 3 9} 1 0 0
  285. 8 IGNORE {UPDATE OR REPLACE} 0 {7 6 9} 1 0 0
  286. 9 FAIL {UPDATE OR IGNORE} 0 {6 7 3 9} 1 0 0
  287. 10 ABORT {UPDATE OR REPLACE} 0 {7 6 9} 1 0 0
  288. 11 ROLLBACK {UPDATE OR IGNORE} 0 {6 7 3 9} 1 0 0
  289. 12 {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1 0 0
  290. 13 {} {UPDATE OR REPLACE} 0 {7 6 9} 1 0 0
  291. 14 {} {UPDATE OR FAIL} 1 {6 7 3 4} 1 0 0
  292. 15 {} {UPDATE OR ABORT} 1 {1 2 3 4} 1 0 1
  293. 16 {} {UPDATE OR ROLLBACK} 1 {1 2 3 4} 0 0 0
  294. } {
  295. if {$t0} {set t1 {column a is not unique}}
  296. if {[info exists TEMP_STORE] && $TEMP_STORE==3} {
  297. set t3 0
  298. } else {
  299. set t3 [expr {$t3+$t4}]
  300. }
  301. do_test conflict-6.$i {
  302. db close
  303. sqlite3 db test.db
  304. if {$conf1!=""} {set conf1 "ON CONFLICT $conf1"}
  305. execsql {pragma temp_store=file}
  306. set ::sqlite_opentemp_count 0
  307. set r0 [catch {execsql [subst {
  308. DROP TABLE t1;
  309. CREATE TABLE t1(a,b,c, UNIQUE(a) $conf1);
  310. INSERT INTO t1 SELECT * FROM t2;
  311. UPDATE t3 SET x=0;
  312. BEGIN;
  313. $cmd t3 SET x=1;
  314. $cmd t1 SET b=b*2;
  315. $cmd t1 SET a=c+5;
  316. }]} r1]
  317. catch {execsql {COMMIT}}
  318. if {!$r0} {set r1 [execsql {SELECT a FROM t1 ORDER BY b}]}
  319. set r2 [execsql {SELECT x FROM t3}]
  320. list $r0 $r1 $r2 $::sqlite_opentemp_count
  321. } [list $t0 $t1 $t2 $t3]
  322. }
  323. # Test to make sure a lot of IGNOREs don't cause a stack overflow
  324. #
  325. do_test conflict-7.1 {
  326. execsql {
  327. DROP TABLE t1;
  328. DROP TABLE t2;
  329. DROP TABLE t3;
  330. CREATE TABLE t1(a unique, b);
  331. }
  332. for {set i 1} {$i<=50} {incr i} {
  333. execsql "INSERT into t1 values($i,[expr {$i+1}]);"
  334. }
  335. execsql {
  336. SELECT count(*), min(a), max(b) FROM t1;
  337. }
  338. } {50 1 51}
  339. do_test conflict-7.2 {
  340. execsql {
  341. PRAGMA count_changes=on;
  342. UPDATE OR IGNORE t1 SET a=1000;
  343. }
  344. } {1}
  345. do_test conflict-7.2.1 {
  346. db changes
  347. } {1}
  348. do_test conflict-7.3 {
  349. execsql {
  350. SELECT b FROM t1 WHERE a=1000;
  351. }
  352. } {2}
  353. do_test conflict-7.4 {
  354. execsql {
  355. SELECT count(*) FROM t1;
  356. }
  357. } {50}
  358. do_test conflict-7.5 {
  359. execsql {
  360. PRAGMA count_changes=on;
  361. UPDATE OR REPLACE t1 SET a=1001;
  362. }
  363. } {50}
  364. do_test conflict-7.5.1 {
  365. db changes
  366. } {50}
  367. do_test conflict-7.6 {
  368. execsql {
  369. SELECT b FROM t1 WHERE a=1001;
  370. }
  371. } {51}
  372. do_test conflict-7.7 {
  373. execsql {
  374. SELECT count(*) FROM t1;
  375. }
  376. } {1}
  377. # Update for version 3: A SELECT statement no longer resets the change
  378. # counter (Test result changes from 0 to 50).
  379. do_test conflict-7.7.1 {
  380. db changes
  381. } {50}
  382. # Make sure the row count is right for rows that are ignored on
  383. # an insert.
  384. #
  385. do_test conflict-8.1 {
  386. execsql {
  387. DELETE FROM t1;
  388. INSERT INTO t1 VALUES(1,2);
  389. }
  390. execsql {
  391. INSERT OR IGNORE INTO t1 VALUES(2,3);
  392. }
  393. } {1}
  394. do_test conflict-8.1.1 {
  395. db changes
  396. } {1}
  397. do_test conflict-8.2 {
  398. execsql {
  399. INSERT OR IGNORE INTO t1 VALUES(2,4);
  400. }
  401. } {0}
  402. do_test conflict-8.2.1 {
  403. db changes
  404. } {0}
  405. do_test conflict-8.3 {
  406. execsql {
  407. INSERT OR REPLACE INTO t1 VALUES(2,4);
  408. }
  409. } {1}
  410. do_test conflict-8.3.1 {
  411. db changes
  412. } {1}
  413. do_test conflict-8.4 {
  414. execsql {
  415. INSERT OR IGNORE INTO t1 SELECT * FROM t1;
  416. }
  417. } {0}
  418. do_test conflict-8.4.1 {
  419. db changes
  420. } {0}
  421. do_test conflict-8.5 {
  422. execsql {
  423. INSERT OR IGNORE INTO t1 SELECT a+2,b+2 FROM t1;
  424. }
  425. } {2}
  426. do_test conflict-8.5.1 {
  427. db changes
  428. } {2}
  429. do_test conflict-8.6 {
  430. execsql {
  431. INSERT OR IGNORE INTO t1 SELECT a+3,b+3 FROM t1;
  432. }
  433. } {3}
  434. do_test conflict-8.6.1 {
  435. db changes
  436. } {3}
  437. integrity_check conflict-8.99
  438. do_test conflict-9.1 {
  439. execsql {
  440. PRAGMA count_changes=0;
  441. CREATE TABLE t2(
  442. a INTEGER UNIQUE ON CONFLICT IGNORE,
  443. b INTEGER UNIQUE ON CONFLICT FAIL,
  444. c INTEGER UNIQUE ON CONFLICT REPLACE,
  445. d INTEGER UNIQUE ON CONFLICT ABORT,
  446. e INTEGER UNIQUE ON CONFLICT ROLLBACK
  447. );
  448. CREATE TABLE t3(x);
  449. INSERT INTO t3 VALUES(1);
  450. SELECT * FROM t3;
  451. }
  452. } {1}
  453. do_test conflict-9.2 {
  454. catchsql {
  455. INSERT INTO t2 VALUES(1,1,1,1,1);
  456. INSERT INTO t2 VALUES(2,2,2,2,2);
  457. SELECT * FROM t2;
  458. }
  459. } {0 {1 1 1 1 1 2 2 2 2 2}}
  460. do_test conflict-9.3 {
  461. catchsql {
  462. INSERT INTO t2 VALUES(1,3,3,3,3);
  463. SELECT * FROM t2;
  464. }
  465. } {0 {1 1 1 1 1 2 2 2 2 2}}
  466. do_test conflict-9.4 {
  467. catchsql {
  468. UPDATE t2 SET a=a+1 WHERE a=1;
  469. SELECT * FROM t2;
  470. }
  471. } {0 {1 1 1 1 1 2 2 2 2 2}}
  472. do_test conflict-9.5 {
  473. catchsql {
  474. INSERT INTO t2 VALUES(3,1,3,3,3);
  475. SELECT * FROM t2;
  476. }
  477. } {1 {column b is not unique}}
  478. do_test conflict-9.6 {
  479. catchsql {
  480. UPDATE t2 SET b=b+1 WHERE b=1;
  481. SELECT * FROM t2;
  482. }
  483. } {1 {column b is not unique}}
  484. do_test conflict-9.7 {
  485. catchsql {
  486. BEGIN;
  487. UPDATE t3 SET x=x+1;
  488. INSERT INTO t2 VALUES(3,1,3,3,3);
  489. SELECT * FROM t2;
  490. }
  491. } {1 {column b is not unique}}
  492. do_test conflict-9.8 {
  493. execsql {COMMIT}
  494. execsql {SELECT * FROM t3}
  495. } {2}
  496. do_test conflict-9.9 {
  497. catchsql {
  498. BEGIN;
  499. UPDATE t3 SET x=x+1;
  500. UPDATE t2 SET b=b+1 WHERE b=1;
  501. SELECT * FROM t2;
  502. }
  503. } {1 {column b is not unique}}
  504. do_test conflict-9.10 {
  505. execsql {COMMIT}
  506. execsql {SELECT * FROM t3}
  507. } {3}
  508. do_test conflict-9.11 {
  509. catchsql {
  510. INSERT INTO t2 VALUES(3,3,3,1,3);
  511. SELECT * FROM t2;
  512. }
  513. } {1 {column d is not unique}}
  514. do_test conflict-9.12 {
  515. catchsql {
  516. UPDATE t2 SET d=d+1 WHERE d=1;
  517. SELECT * FROM t2;
  518. }
  519. } {1 {column d is not unique}}
  520. do_test conflict-9.13 {
  521. catchsql {
  522. BEGIN;
  523. UPDATE t3 SET x=x+1;
  524. INSERT INTO t2 VALUES(3,3,3,1,3);
  525. SELECT * FROM t2;
  526. }
  527. } {1 {column d is not unique}}
  528. do_test conflict-9.14 {
  529. execsql {COMMIT}
  530. execsql {SELECT * FROM t3}
  531. } {4}
  532. do_test conflict-9.15 {
  533. catchsql {
  534. BEGIN;
  535. UPDATE t3 SET x=x+1;
  536. UPDATE t2 SET d=d+1 WHERE d=1;
  537. SELECT * FROM t2;
  538. }
  539. } {1 {column d is not unique}}
  540. do_test conflict-9.16 {
  541. execsql {COMMIT}
  542. execsql {SELECT * FROM t3}
  543. } {5}
  544. do_test conflict-9.17 {
  545. catchsql {
  546. INSERT INTO t2 VALUES(3,3,3,3,1);
  547. SELECT * FROM t2;
  548. }
  549. } {1 {column e is not unique}}
  550. do_test conflict-9.18 {
  551. catchsql {
  552. UPDATE t2 SET e=e+1 WHERE e=1;
  553. SELECT * FROM t2;
  554. }
  555. } {1 {column e is not unique}}
  556. do_test conflict-9.19 {
  557. catchsql {
  558. BEGIN;
  559. UPDATE t3 SET x=x+1;
  560. INSERT INTO t2 VALUES(3,3,3,3,1);
  561. SELECT * FROM t2;
  562. }
  563. } {1 {column e is not unique}}
  564. verify_ex_errcode conflict-9.21b SQLITE_CONSTRAINT_UNIQUE
  565. do_test conflict-9.20 {
  566. catch {execsql {COMMIT}}
  567. execsql {SELECT * FROM t3}
  568. } {5}
  569. do_test conflict-9.21 {
  570. catchsql {
  571. BEGIN;
  572. UPDATE t3 SET x=x+1;
  573. UPDATE t2 SET e=e+1 WHERE e=1;
  574. SELECT * FROM t2;
  575. }
  576. } {1 {column e is not unique}}
  577. verify_ex_errcode conflict-9.21b SQLITE_CONSTRAINT_UNIQUE
  578. do_test conflict-9.22 {
  579. catch {execsql {COMMIT}}
  580. execsql {SELECT * FROM t3}
  581. } {5}
  582. do_test conflict-9.23 {
  583. catchsql {
  584. INSERT INTO t2 VALUES(3,3,1,3,3);
  585. SELECT * FROM t2;
  586. }
  587. } {0 {2 2 2 2 2 3 3 1 3 3}}
  588. do_test conflict-9.24 {
  589. catchsql {
  590. UPDATE t2 SET c=c-1 WHERE c=2;
  591. SELECT * FROM t2;
  592. }
  593. } {0 {2 2 1 2 2}}
  594. do_test conflict-9.25 {
  595. catchsql {
  596. BEGIN;
  597. UPDATE t3 SET x=x+1;
  598. INSERT INTO t2 VALUES(3,3,1,3,3);
  599. SELECT * FROM t2;
  600. }
  601. } {0 {3 3 1 3 3}}
  602. do_test conflict-9.26 {
  603. catch {execsql {COMMIT}}
  604. execsql {SELECT * FROM t3}
  605. } {6}
  606. do_test conflict-10.1 {
  607. catchsql {
  608. DELETE FROM t1;
  609. BEGIN;
  610. INSERT OR ROLLBACK INTO t1 VALUES(1,2);
  611. INSERT OR ROLLBACK INTO t1 VALUES(1,3);
  612. COMMIT;
  613. }
  614. execsql {SELECT * FROM t1}
  615. } {}
  616. do_test conflict-10.2 {
  617. catchsql {
  618. CREATE TABLE t4(x);
  619. CREATE UNIQUE INDEX t4x ON t4(x);
  620. BEGIN;
  621. INSERT OR ROLLBACK INTO t4 VALUES(1);
  622. INSERT OR ROLLBACK INTO t4 VALUES(1);
  623. COMMIT;
  624. }
  625. execsql {SELECT * FROM t4}
  626. } {}
  627. # Ticket #1171. Make sure statement rollbacks do not
  628. # damage the database.
  629. #
  630. do_test conflict-11.1 {
  631. execsql {
  632. -- Create a database object (pages 2, 3 of the file)
  633. BEGIN;
  634. CREATE TABLE abc(a UNIQUE, b, c);
  635. INSERT INTO abc VALUES(1, 2, 3);
  636. INSERT INTO abc VALUES(4, 5, 6);
  637. INSERT INTO abc VALUES(7, 8, 9);
  638. COMMIT;
  639. }
  640. # Set a small cache size so that changes will spill into
  641. # the database file.
  642. execsql {
  643. PRAGMA cache_size = 10;
  644. }
  645. # Make lots of changes. Because of the small cache, some
  646. # (most?) of these changes will spill into the disk file.
  647. # In other words, some of the changes will not be held in
  648. # cache.
  649. #
  650. execsql {
  651. BEGIN;
  652. -- Make sure the pager is in EXCLUSIVE state.
  653. CREATE TABLE def(d, e, f);
  654. INSERT INTO def VALUES
  655. ('xxxxxxxxxxxxxxx', 'yyyyyyyyyyyyyyyy', 'zzzzzzzzzzzzzzzz');
  656. INSERT INTO def SELECT * FROM def;
  657. INSERT INTO def SELECT * FROM def;
  658. INSERT INTO def SELECT * FROM def;
  659. INSERT INTO def SELECT * FROM def;
  660. INSERT INTO def SELECT * FROM def;
  661. INSERT INTO def SELECT * FROM def;
  662. INSERT INTO def SELECT * FROM def;
  663. DELETE FROM abc WHERE a = 4;
  664. }
  665. # Execute a statement that does a statement rollback due to
  666. # a constraint failure.
  667. #
  668. catchsql {
  669. INSERT INTO abc SELECT 10, 20, 30 FROM def;
  670. }
  671. # Rollback the database. Verify that the state of the ABC table
  672. # is unchanged from the beginning of the transaction. In other words,
  673. # make sure the DELETE on table ABC that occurred within the transaction
  674. # had no effect.
  675. #
  676. execsql {
  677. ROLLBACK;
  678. SELECT * FROM abc;
  679. }
  680. } {1 2 3 4 5 6 7 8 9}
  681. integrity_check conflict-11.2
  682. # Repeat test conflict-11.1 but this time commit.
  683. #
  684. do_test conflict-11.3 {
  685. execsql {
  686. BEGIN;
  687. -- Make sure the pager is in EXCLUSIVE state.
  688. UPDATE abc SET a=a+1;
  689. CREATE TABLE def(d, e, f);
  690. INSERT INTO def VALUES
  691. ('xxxxxxxxxxxxxxx', 'yyyyyyyyyyyyyyyy', 'zzzzzzzzzzzzzzzz');
  692. INSERT INTO def SELECT * FROM def;
  693. INSERT INTO def SELECT * FROM def;
  694. INSERT INTO def SELECT * FROM def;
  695. INSERT INTO def SELECT * FROM def;
  696. INSERT INTO def SELECT * FROM def;
  697. INSERT INTO def SELECT * FROM def;
  698. INSERT INTO def SELECT * FROM def;
  699. DELETE FROM abc WHERE a = 4;
  700. }
  701. catchsql {
  702. INSERT INTO abc SELECT 10, 20, 30 FROM def;
  703. }
  704. execsql {
  705. ROLLBACK;
  706. SELECT * FROM abc;
  707. }
  708. } {1 2 3 4 5 6 7 8 9}
  709. # Repeat test conflict-11.1 but this time commit.
  710. #
  711. do_test conflict-11.5 {
  712. execsql {
  713. BEGIN;
  714. -- Make sure the pager is in EXCLUSIVE state.
  715. CREATE TABLE def(d, e, f);
  716. INSERT INTO def VALUES
  717. ('xxxxxxxxxxxxxxx', 'yyyyyyyyyyyyyyyy', 'zzzzzzzzzzzzzzzz');
  718. INSERT INTO def SELECT * FROM def;
  719. INSERT INTO def SELECT * FROM def;
  720. INSERT INTO def SELECT * FROM def;
  721. INSERT INTO def SELECT * FROM def;
  722. INSERT INTO def SELECT * FROM def;
  723. INSERT INTO def SELECT * FROM def;
  724. INSERT INTO def SELECT * FROM def;
  725. DELETE FROM abc WHERE a = 4;
  726. }
  727. catchsql {
  728. INSERT INTO abc SELECT 10, 20, 30 FROM def;
  729. }
  730. execsql {
  731. COMMIT;
  732. SELECT * FROM abc;
  733. }
  734. } {1 2 3 7 8 9}
  735. integrity_check conflict-11.6
  736. # Make sure UPDATE OR REPLACE works on tables that have only
  737. # an INTEGER PRIMARY KEY.
  738. #
  739. do_test conflict-12.1 {
  740. execsql {
  741. CREATE TABLE t5(a INTEGER PRIMARY KEY, b text);
  742. INSERT INTO t5 VALUES(1,'one');
  743. INSERT INTO t5 VALUES(2,'two');
  744. SELECT * FROM t5
  745. }
  746. } {1 one 2 two}
  747. do_test conflict-12.2 {
  748. execsql {
  749. UPDATE OR IGNORE t5 SET a=a+1 WHERE a=1;
  750. SELECT * FROM t5;
  751. }
  752. } {1 one 2 two}
  753. do_test conflict-12.3 {
  754. catchsql {
  755. UPDATE t5 SET a=a+1 WHERE a=1;
  756. }
  757. } {1 {PRIMARY KEY must be unique}}
  758. verify_ex_errcode conflict-12.3b SQLITE_CONSTRAINT_PRIMARYKEY
  759. do_test conflict-12.4 {
  760. execsql {
  761. UPDATE OR REPLACE t5 SET a=a+1 WHERE a=1;
  762. SELECT * FROM t5;
  763. }
  764. } {2 one}
  765. # Ticket [c38baa3d969eab7946dc50ba9d9b4f0057a19437]
  766. # REPLACE works like ABORT on a CHECK constraint.
  767. #
  768. do_test conflict-13.1 {
  769. execsql {
  770. CREATE TABLE t13(a CHECK(a!=2));
  771. BEGIN;
  772. REPLACE INTO t13 VALUES(1);
  773. }
  774. catchsql {
  775. REPLACE INTO t13 VALUES(2);
  776. }
  777. } {1 {constraint failed}}
  778. verify_ex_errcode conflict-13.1b SQLITE_CONSTRAINT_CHECK
  779. do_test conflict-13.2 {
  780. execsql {
  781. REPLACE INTO t13 VALUES(3);
  782. COMMIT;
  783. SELECT * FROM t13;
  784. }
  785. } {1 3}
  786. finish_test