/ Check-in [3e5d38f5]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:More compact implementation of PRAGMA onconflict, and some test cases.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | pragma-onconflict
Files: files | file ages | folders
SHA1: 3e5d38f5b37da08a3d4704c23a51399624e3ecab
User & Date: drh 2016-02-27 19:03:22
Context
2016-02-27
19:03
More compact implementation of PRAGMA onconflict, and some test cases. Leaf check-in: 3e5d38f5 user: drh tags: pragma-onconflict
17:16
Experimental "PRAGMA onconflict=FAIL" statement to change the default ON CONFLICT algorithm to something other than ABORT. check-in: 3a0c347c user: drh tags: pragma-onconflict
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/pragma.c.

1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
  **   PRAGMA onconflict = FAIL
  **   PRAGMA onconflict = ABORT
  **   PRAGMA onconflict = ROLLBACK
  **
  ** Set the default conflict handling algorithm.
  */
  case PragTyp_ONCONFLICT: {
    const char *zRes = "ABORT";
    if( zRight ){
      if( sqlite3StrICmp(zRight,"FAIL")==0 ) db->dfltOnError = OE_Fail;
      if( sqlite3StrICmp(zRight,"ABORT")==0 ) db->dfltOnError = OE_Abort;
      if( sqlite3StrICmp(zRight,"ROLLBACK")==0 ) db->dfltOnError = OE_Rollback;
    }
    switch( db->dfltOnError ){
      case OE_Fail:     zRes = "FAIL";      break;
      case OE_Rollback: zRes = "ROLLBACK";  break;
    }
    returnSingleText(v, "onconflict", zRes);
    break;
  }

#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
  /*







|
|
|
|
|
|
|
<
<







1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912


1913
1914
1915
1916
1917
1918
1919
  **   PRAGMA onconflict = FAIL
  **   PRAGMA onconflict = ABORT
  **   PRAGMA onconflict = ROLLBACK
  **
  ** Set the default conflict handling algorithm.
  */
  case PragTyp_ONCONFLICT: {
    static const char *azMode[] = { "ABORT",  "FAIL",  "ROLLBACK"  };
    static const u8 aeMode[]    = { OE_Abort, OE_Fail, OE_Rollback };
    const char *zRes = 0;
    int i;
    for(i=0; i<ArraySize(azMode); i++){
      if( sqlite3_stricmp(azMode[i], zRight)==0 ) db->dfltOnError = aeMode[i];
      if( db->dfltOnError==aeMode[i] ) zRes = azMode[i];


    }
    returnSingleText(v, "onconflict", zRes);
    break;
  }

#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
  /*

Added test/onconflict1.test.







































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 2016-02-27
#
# 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.
#
#***********************************************************************
# This file implements regression tests for SQLite library.
#
# This file focuses on the PRAGMA onconflict statement.

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix onconflict1

do_execsql_test 100 {
  PRAGMA onconflict;
} {ABORT}
do_execsql_test 110 {
  PRAGMA onconflict=Rollback;
} {ROLLBACK}
do_execsql_test 120 {
  PRAGMA onconflict=fail;
} {FAIL}
do_execsql_test 130 {
  PRAGMA onconflict=whatever;
} {FAIL}
do_execsql_test 140 {
  PRAGMA onconflict=abort;
} {ABORT}

do_catchsql_test 200 {
  CREATE TABLE t1(a, b UNIQUE);
  INSERT INTO t1(a,b) VALUES(1,1),(2,2),(3,3),(98,98),(99,99);
  BEGIN;
  UPDATE t1 SET a=100 WHERE a=101;
  UPDATE t1 SET b=10;
} {1 {UNIQUE constraint failed: t1.b}}
do_execsql_test 201 {
  SELECT a, b FROM t1 ORDER BY a;
} {1 1 2 2 3 3 98 98 99 99}

do_catchsql_test 210 {
  PRAGMA onconflict=FAIL;
  UPDATE t1 SET b=10;
} {1 {UNIQUE constraint failed: t1.b}}
do_execsql_test 211 {
  SELECT a, b FROM t1 ORDER BY a;
} {1 10 2 2 3 3 98 98 99 99}

do_catchsql_test 220 {
  ROLLBACK;
  BEGIN;
  PRAGMA onconflict=ROLLBACK;
  UPDATE t1 SET b=10;
} {1 {UNIQUE constraint failed: t1.b}}
do_execsql_test 221 {
  SELECT a, b FROM t1 ORDER BY a;
} {1 1 2 2 3 3 98 98 99 99}
do_catchsql_test 222 {
  ROLLBACK
} {1 {cannot rollback - no transaction is active}}

do_catchsql_test 300 {
  PRAGMA onconflict=ABORT;
  BEGIN;
  INSERT INTO t1(a,b) VALUES(4,4),(5,1),(6,6);
} {1 {UNIQUE constraint failed: t1.b}}
do_execsql_test 301 {
  SELECT a, b FROM t1 ORDER BY a;
} {1 1 2 2 3 3 98 98 99 99}

do_catchsql_test 310 {
  PRAGMA onconflict=ROLLBACK;
  INSERT INTO t1(a,b) VALUES(4,4),(5,1),(6,6);
} {1 {UNIQUE constraint failed: t1.b}}
do_execsql_test 311 {
  SELECT a, b FROM t1 ORDER BY a;
} {1 1 2 2 3 3 98 98 99 99}
do_catchsql_test 312 {
  ROLLBACK
} {1 {cannot rollback - no transaction is active}}

do_catchsql_test 320 {
  PRAGMA onconflict=FAIL;
  BEGIN;
  INSERT INTO t1(a,b) VALUES(4,4),(5,1),(6,6);
} {1 {UNIQUE constraint failed: t1.b}}
do_execsql_test 321 {
  SELECT a, b FROM t1 ORDER BY a;
} {1 1 2 2 3 3 4 4 98 98 99 99}
do_catchsql_test 322 {
  ROLLBACK
} {0 {}}

finish_test