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