os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/trans.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
# 2001 September 15
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.  The
sl@0
    12
# focus of this script is database locks.
sl@0
    13
#
sl@0
    14
# $Id: trans.test,v 1.38 2008/04/19 20:34:19 drh Exp $
sl@0
    15
sl@0
    16
sl@0
    17
set testdir [file dirname $argv0]
sl@0
    18
source $testdir/tester.tcl
sl@0
    19
sl@0
    20
sl@0
    21
# Create several tables to work with.
sl@0
    22
#
sl@0
    23
do_test trans-1.0 {
sl@0
    24
  execsql {
sl@0
    25
    CREATE TABLE one(a int PRIMARY KEY, b text);
sl@0
    26
    INSERT INTO one VALUES(1,'one');
sl@0
    27
    INSERT INTO one VALUES(2,'two');
sl@0
    28
    INSERT INTO one VALUES(3,'three');
sl@0
    29
    SELECT b FROM one ORDER BY a;
sl@0
    30
  }
sl@0
    31
} {one two three}
sl@0
    32
integrity_check trans-1.0.1
sl@0
    33
do_test trans-1.1 {
sl@0
    34
  execsql {
sl@0
    35
    CREATE TABLE two(a int PRIMARY KEY, b text);
sl@0
    36
    INSERT INTO two VALUES(1,'I');
sl@0
    37
    INSERT INTO two VALUES(5,'V');
sl@0
    38
    INSERT INTO two VALUES(10,'X');
sl@0
    39
    SELECT b FROM two ORDER BY a;
sl@0
    40
  }
sl@0
    41
} {I V X}
sl@0
    42
do_test trans-1.9 {
sl@0
    43
  sqlite3 altdb test.db
sl@0
    44
  execsql {SELECT b FROM one ORDER BY a} altdb
sl@0
    45
} {one two three}
sl@0
    46
do_test trans-1.10 {
sl@0
    47
  execsql {SELECT b FROM two ORDER BY a} altdb
sl@0
    48
} {I V X}
sl@0
    49
integrity_check trans-1.11
sl@0
    50
sl@0
    51
# Basic transactions
sl@0
    52
#
sl@0
    53
do_test trans-2.1 {
sl@0
    54
  set v [catch {execsql {BEGIN}} msg]
sl@0
    55
  lappend v $msg
sl@0
    56
} {0 {}}
sl@0
    57
do_test trans-2.2 {
sl@0
    58
  set v [catch {execsql {END}} msg]
sl@0
    59
  lappend v $msg
sl@0
    60
} {0 {}}
sl@0
    61
do_test trans-2.3 {
sl@0
    62
  set v [catch {execsql {BEGIN TRANSACTION}} msg]
sl@0
    63
  lappend v $msg
sl@0
    64
} {0 {}}
sl@0
    65
do_test trans-2.4 {
sl@0
    66
  set v [catch {execsql {COMMIT TRANSACTION}} msg]
sl@0
    67
  lappend v $msg
sl@0
    68
} {0 {}}
sl@0
    69
do_test trans-2.5 {
sl@0
    70
  set v [catch {execsql {BEGIN TRANSACTION 'foo'}} msg]
sl@0
    71
  lappend v $msg
sl@0
    72
} {0 {}}
sl@0
    73
do_test trans-2.6 {
sl@0
    74
  set v [catch {execsql {ROLLBACK TRANSACTION 'foo'}} msg]
sl@0
    75
  lappend v $msg
sl@0
    76
} {0 {}}
sl@0
    77
do_test trans-2.10 {
sl@0
    78
  execsql {
sl@0
    79
    BEGIN;
sl@0
    80
    SELECT a FROM one ORDER BY a;
sl@0
    81
    SELECT a FROM two ORDER BY a;
sl@0
    82
    END;
sl@0
    83
  }
sl@0
    84
} {1 2 3 1 5 10}
sl@0
    85
integrity_check trans-2.11
sl@0
    86
sl@0
    87
# Check the locking behavior
sl@0
    88
#
sl@0
    89
do_test trans-3.1 {
sl@0
    90
  execsql {
sl@0
    91
    BEGIN;
sl@0
    92
    UPDATE one SET a = 0 WHERE 0;
sl@0
    93
    SELECT a FROM one ORDER BY a;
sl@0
    94
  }
sl@0
    95
} {1 2 3}
sl@0
    96
do_test trans-3.2 {
sl@0
    97
  catchsql {
sl@0
    98
    SELECT a FROM two ORDER BY a;
sl@0
    99
  } altdb
sl@0
   100
} {0 {1 5 10}}
sl@0
   101
sl@0
   102
do_test trans-3.3 {
sl@0
   103
  catchsql {
sl@0
   104
    SELECT a FROM one ORDER BY a;
sl@0
   105
  } altdb
sl@0
   106
} {0 {1 2 3}}
sl@0
   107
do_test trans-3.4 {
sl@0
   108
  catchsql {
sl@0
   109
    INSERT INTO one VALUES(4,'four');
sl@0
   110
  }
sl@0
   111
} {0 {}}
sl@0
   112
do_test trans-3.5 {
sl@0
   113
  catchsql {
sl@0
   114
    SELECT a FROM two ORDER BY a;
sl@0
   115
  } altdb
sl@0
   116
} {0 {1 5 10}}
sl@0
   117
do_test trans-3.6 {
sl@0
   118
  catchsql {
sl@0
   119
    SELECT a FROM one ORDER BY a;
sl@0
   120
  } altdb
sl@0
   121
} {0 {1 2 3}}
sl@0
   122
do_test trans-3.7 {
sl@0
   123
  catchsql {
sl@0
   124
    INSERT INTO two VALUES(4,'IV');
sl@0
   125
  }
sl@0
   126
} {0 {}}
sl@0
   127
do_test trans-3.8 {
sl@0
   128
  catchsql {
sl@0
   129
    SELECT a FROM two ORDER BY a;
sl@0
   130
  } altdb
sl@0
   131
} {0 {1 5 10}}
sl@0
   132
do_test trans-3.9 {
sl@0
   133
  catchsql {
sl@0
   134
    SELECT a FROM one ORDER BY a;
sl@0
   135
  } altdb
sl@0
   136
} {0 {1 2 3}}
sl@0
   137
do_test trans-3.10 {
sl@0
   138
  execsql {END TRANSACTION}
sl@0
   139
} {}
sl@0
   140
sl@0
   141
do_test trans-3.11 {
sl@0
   142
  set v [catch {execsql {
sl@0
   143
    SELECT a FROM two ORDER BY a;
sl@0
   144
  } altdb} msg]
sl@0
   145
  lappend v $msg
sl@0
   146
} {0 {1 4 5 10}}
sl@0
   147
do_test trans-3.12 {
sl@0
   148
  set v [catch {execsql {
sl@0
   149
    SELECT a FROM one ORDER BY a;
sl@0
   150
  } altdb} msg]
sl@0
   151
  lappend v $msg
sl@0
   152
} {0 {1 2 3 4}}
sl@0
   153
do_test trans-3.13 {
sl@0
   154
  set v [catch {execsql {
sl@0
   155
    SELECT a FROM two ORDER BY a;
sl@0
   156
  } db} msg]
sl@0
   157
  lappend v $msg
sl@0
   158
} {0 {1 4 5 10}}
sl@0
   159
do_test trans-3.14 {
sl@0
   160
  set v [catch {execsql {
sl@0
   161
    SELECT a FROM one ORDER BY a;
sl@0
   162
  } db} msg]
sl@0
   163
  lappend v $msg
sl@0
   164
} {0 {1 2 3 4}}
sl@0
   165
integrity_check trans-3.15
sl@0
   166
sl@0
   167
do_test trans-4.1 {
sl@0
   168
  set v [catch {execsql {
sl@0
   169
    COMMIT;
sl@0
   170
  } db} msg]
sl@0
   171
  lappend v $msg
sl@0
   172
} {1 {cannot commit - no transaction is active}}
sl@0
   173
do_test trans-4.2 {
sl@0
   174
  set v [catch {execsql {
sl@0
   175
    ROLLBACK;
sl@0
   176
  } db} msg]
sl@0
   177
  lappend v $msg
sl@0
   178
} {1 {cannot rollback - no transaction is active}}
sl@0
   179
do_test trans-4.3 {
sl@0
   180
  catchsql {
sl@0
   181
    BEGIN TRANSACTION;
sl@0
   182
    UPDATE two SET a = 0 WHERE 0;
sl@0
   183
    SELECT a FROM two ORDER BY a;
sl@0
   184
  } db
sl@0
   185
} {0 {1 4 5 10}}
sl@0
   186
do_test trans-4.4 {
sl@0
   187
  catchsql {
sl@0
   188
    SELECT a FROM two ORDER BY a;
sl@0
   189
  } altdb
sl@0
   190
} {0 {1 4 5 10}}
sl@0
   191
do_test trans-4.5 {
sl@0
   192
  catchsql {
sl@0
   193
    SELECT a FROM one ORDER BY a;
sl@0
   194
  } altdb
sl@0
   195
} {0 {1 2 3 4}}
sl@0
   196
do_test trans-4.6 {
sl@0
   197
  catchsql {
sl@0
   198
    BEGIN TRANSACTION;
sl@0
   199
    SELECT a FROM one ORDER BY a;
sl@0
   200
  } db
sl@0
   201
} {1 {cannot start a transaction within a transaction}}
sl@0
   202
do_test trans-4.7 {
sl@0
   203
  catchsql {
sl@0
   204
    SELECT a FROM two ORDER BY a;
sl@0
   205
  } altdb
sl@0
   206
} {0 {1 4 5 10}}
sl@0
   207
do_test trans-4.8 {
sl@0
   208
  catchsql {
sl@0
   209
    SELECT a FROM one ORDER BY a;
sl@0
   210
  } altdb
sl@0
   211
} {0 {1 2 3 4}}
sl@0
   212
do_test trans-4.9 {
sl@0
   213
  set v [catch {execsql {
sl@0
   214
    END TRANSACTION;
sl@0
   215
    SELECT a FROM two ORDER BY a;
sl@0
   216
  } db} msg]
sl@0
   217
  lappend v $msg
sl@0
   218
} {0 {1 4 5 10}}
sl@0
   219
do_test trans-4.10 {
sl@0
   220
  set v [catch {execsql {
sl@0
   221
    SELECT a FROM two ORDER BY a;
sl@0
   222
  } altdb} msg]
sl@0
   223
  lappend v $msg
sl@0
   224
} {0 {1 4 5 10}}
sl@0
   225
do_test trans-4.11 {
sl@0
   226
  set v [catch {execsql {
sl@0
   227
    SELECT a FROM one ORDER BY a;
sl@0
   228
  } altdb} msg]
sl@0
   229
  lappend v $msg
sl@0
   230
} {0 {1 2 3 4}}
sl@0
   231
integrity_check trans-4.12
sl@0
   232
do_test trans-4.98 {
sl@0
   233
  altdb close
sl@0
   234
  execsql {
sl@0
   235
    DROP TABLE one;
sl@0
   236
    DROP TABLE two;
sl@0
   237
  }
sl@0
   238
} {}
sl@0
   239
integrity_check trans-4.99
sl@0
   240
sl@0
   241
# Check out the commit/rollback behavior of the database
sl@0
   242
#
sl@0
   243
do_test trans-5.1 {
sl@0
   244
  execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
sl@0
   245
} {}
sl@0
   246
do_test trans-5.2 {
sl@0
   247
  execsql {BEGIN TRANSACTION}
sl@0
   248
  execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
sl@0
   249
} {}
sl@0
   250
do_test trans-5.3 {
sl@0
   251
  execsql {CREATE TABLE one(a text, b int)}
sl@0
   252
  execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
sl@0
   253
} {one}
sl@0
   254
do_test trans-5.4 {
sl@0
   255
  execsql {SELECT a,b FROM one ORDER BY b}
sl@0
   256
} {}
sl@0
   257
do_test trans-5.5 {
sl@0
   258
  execsql {INSERT INTO one(a,b) VALUES('hello', 1)}
sl@0
   259
  execsql {SELECT a,b FROM one ORDER BY b}
sl@0
   260
} {hello 1}
sl@0
   261
do_test trans-5.6 {
sl@0
   262
  execsql {ROLLBACK}
sl@0
   263
  execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
sl@0
   264
} {}
sl@0
   265
do_test trans-5.7 {
sl@0
   266
  set v [catch {
sl@0
   267
    execsql {SELECT a,b FROM one ORDER BY b}
sl@0
   268
  } msg]
sl@0
   269
  lappend v $msg
sl@0
   270
} {1 {no such table: one}}
sl@0
   271
sl@0
   272
# Test commits and rollbacks of table CREATE TABLEs, CREATE INDEXs
sl@0
   273
# DROP TABLEs and DROP INDEXs
sl@0
   274
#
sl@0
   275
do_test trans-5.8 {
sl@0
   276
  execsql {
sl@0
   277
    SELECT name fROM sqlite_master 
sl@0
   278
    WHERE type='table' OR type='index'
sl@0
   279
    ORDER BY name
sl@0
   280
  }
sl@0
   281
} {}
sl@0
   282
do_test trans-5.9 {
sl@0
   283
  execsql {
sl@0
   284
    BEGIN TRANSACTION;
sl@0
   285
    CREATE TABLE t1(a int, b int, c int);
sl@0
   286
    SELECT name fROM sqlite_master 
sl@0
   287
    WHERE type='table' OR type='index'
sl@0
   288
    ORDER BY name;
sl@0
   289
  }
sl@0
   290
} {t1}
sl@0
   291
do_test trans-5.10 {
sl@0
   292
  execsql {
sl@0
   293
    CREATE INDEX i1 ON t1(a);
sl@0
   294
    SELECT name fROM sqlite_master 
sl@0
   295
    WHERE type='table' OR type='index'
sl@0
   296
    ORDER BY name;
sl@0
   297
  }
sl@0
   298
} {i1 t1}
sl@0
   299
do_test trans-5.11 {
sl@0
   300
  execsql {
sl@0
   301
    COMMIT;
sl@0
   302
    SELECT name fROM sqlite_master 
sl@0
   303
    WHERE type='table' OR type='index'
sl@0
   304
    ORDER BY name;
sl@0
   305
  }
sl@0
   306
} {i1 t1}
sl@0
   307
do_test trans-5.12 {
sl@0
   308
  execsql {
sl@0
   309
    BEGIN TRANSACTION;
sl@0
   310
    CREATE TABLE t2(a int, b int, c int);
sl@0
   311
    CREATE INDEX i2a ON t2(a);
sl@0
   312
    CREATE INDEX i2b ON t2(b);
sl@0
   313
    DROP TABLE t1;
sl@0
   314
    SELECT name fROM sqlite_master 
sl@0
   315
    WHERE type='table' OR type='index'
sl@0
   316
    ORDER BY name;
sl@0
   317
  }
sl@0
   318
} {i2a i2b t2}
sl@0
   319
do_test trans-5.13 {
sl@0
   320
  execsql {
sl@0
   321
    ROLLBACK;
sl@0
   322
    SELECT name fROM sqlite_master 
sl@0
   323
    WHERE type='table' OR type='index'
sl@0
   324
    ORDER BY name;
sl@0
   325
  }
sl@0
   326
} {i1 t1}
sl@0
   327
do_test trans-5.14 {
sl@0
   328
  execsql {
sl@0
   329
    BEGIN TRANSACTION;
sl@0
   330
    DROP INDEX i1;
sl@0
   331
    SELECT name fROM sqlite_master 
sl@0
   332
    WHERE type='table' OR type='index'
sl@0
   333
    ORDER BY name;
sl@0
   334
  }
sl@0
   335
} {t1}
sl@0
   336
do_test trans-5.15 {
sl@0
   337
  execsql {
sl@0
   338
    ROLLBACK;
sl@0
   339
    SELECT name fROM sqlite_master 
sl@0
   340
    WHERE type='table' OR type='index'
sl@0
   341
    ORDER BY name;
sl@0
   342
  }
sl@0
   343
} {i1 t1}
sl@0
   344
do_test trans-5.16 {
sl@0
   345
  execsql {
sl@0
   346
    BEGIN TRANSACTION;
sl@0
   347
    DROP INDEX i1;
sl@0
   348
    CREATE TABLE t2(x int, y int, z int);
sl@0
   349
    CREATE INDEX i2x ON t2(x);
sl@0
   350
    CREATE INDEX i2y ON t2(y);
sl@0
   351
    INSERT INTO t2 VALUES(1,2,3);
sl@0
   352
    SELECT name fROM sqlite_master 
sl@0
   353
    WHERE type='table' OR type='index'
sl@0
   354
    ORDER BY name;
sl@0
   355
  }
sl@0
   356
} {i2x i2y t1 t2}
sl@0
   357
do_test trans-5.17 {
sl@0
   358
  execsql {
sl@0
   359
    COMMIT;
sl@0
   360
    SELECT name fROM sqlite_master 
sl@0
   361
    WHERE type='table' OR type='index'
sl@0
   362
    ORDER BY name;
sl@0
   363
  }
sl@0
   364
} {i2x i2y t1 t2}
sl@0
   365
do_test trans-5.18 {
sl@0
   366
  execsql {
sl@0
   367
    SELECT * FROM t2;
sl@0
   368
  }
sl@0
   369
} {1 2 3}
sl@0
   370
do_test trans-5.19 {
sl@0
   371
  execsql {
sl@0
   372
    SELECT x FROM t2 WHERE y=2;
sl@0
   373
  }
sl@0
   374
} {1}
sl@0
   375
do_test trans-5.20 {
sl@0
   376
  execsql {
sl@0
   377
    BEGIN TRANSACTION;
sl@0
   378
    DROP TABLE t1;
sl@0
   379
    DROP TABLE t2;
sl@0
   380
    SELECT name fROM sqlite_master 
sl@0
   381
    WHERE type='table' OR type='index'
sl@0
   382
    ORDER BY name;
sl@0
   383
  }
sl@0
   384
} {}
sl@0
   385
do_test trans-5.21 {
sl@0
   386
  set r [catch {execsql {
sl@0
   387
    SELECT * FROM t2
sl@0
   388
  }} msg]
sl@0
   389
  lappend r $msg
sl@0
   390
} {1 {no such table: t2}}
sl@0
   391
do_test trans-5.22 {
sl@0
   392
  execsql {
sl@0
   393
    ROLLBACK;
sl@0
   394
    SELECT name fROM sqlite_master 
sl@0
   395
    WHERE type='table' OR type='index'
sl@0
   396
    ORDER BY name;
sl@0
   397
  }
sl@0
   398
} {i2x i2y t1 t2}
sl@0
   399
do_test trans-5.23 {
sl@0
   400
  execsql {
sl@0
   401
    SELECT * FROM t2;
sl@0
   402
  }
sl@0
   403
} {1 2 3}
sl@0
   404
integrity_check trans-5.23
sl@0
   405
sl@0
   406
sl@0
   407
# Try to DROP and CREATE tables and indices with the same name
sl@0
   408
# within a transaction.  Make sure ROLLBACK works.
sl@0
   409
#
sl@0
   410
do_test trans-6.1 {
sl@0
   411
  execsql2 {
sl@0
   412
    INSERT INTO t1 VALUES(1,2,3);
sl@0
   413
    BEGIN TRANSACTION;
sl@0
   414
    DROP TABLE t1;
sl@0
   415
    CREATE TABLE t1(p,q,r);
sl@0
   416
    ROLLBACK;
sl@0
   417
    SELECT * FROM t1;
sl@0
   418
  }
sl@0
   419
} {a 1 b 2 c 3}
sl@0
   420
do_test trans-6.2 {
sl@0
   421
  execsql2 {
sl@0
   422
    INSERT INTO t1 VALUES(1,2,3);
sl@0
   423
    BEGIN TRANSACTION;
sl@0
   424
    DROP TABLE t1;
sl@0
   425
    CREATE TABLE t1(p,q,r);
sl@0
   426
    COMMIT;
sl@0
   427
    SELECT * FROM t1;
sl@0
   428
  }
sl@0
   429
} {}
sl@0
   430
do_test trans-6.3 {
sl@0
   431
  execsql2 {
sl@0
   432
    INSERT INTO t1 VALUES(1,2,3);
sl@0
   433
    SELECT * FROM t1;
sl@0
   434
  }
sl@0
   435
} {p 1 q 2 r 3}
sl@0
   436
do_test trans-6.4 {
sl@0
   437
  execsql2 {
sl@0
   438
    BEGIN TRANSACTION;
sl@0
   439
    DROP TABLE t1;
sl@0
   440
    CREATE TABLE t1(a,b,c);
sl@0
   441
    INSERT INTO t1 VALUES(4,5,6);
sl@0
   442
    SELECT * FROM t1;
sl@0
   443
    DROP TABLE t1;
sl@0
   444
  }
sl@0
   445
} {a 4 b 5 c 6}
sl@0
   446
do_test trans-6.5 {
sl@0
   447
  execsql2 {
sl@0
   448
    ROLLBACK;
sl@0
   449
    SELECT * FROM t1;
sl@0
   450
  }
sl@0
   451
} {p 1 q 2 r 3}
sl@0
   452
do_test trans-6.6 {
sl@0
   453
  execsql2 {
sl@0
   454
    BEGIN TRANSACTION;
sl@0
   455
    DROP TABLE t1;
sl@0
   456
    CREATE TABLE t1(a,b,c);
sl@0
   457
    INSERT INTO t1 VALUES(4,5,6);
sl@0
   458
    SELECT * FROM t1;
sl@0
   459
    DROP TABLE t1;
sl@0
   460
  }
sl@0
   461
} {a 4 b 5 c 6}
sl@0
   462
do_test trans-6.7 {
sl@0
   463
  catchsql {
sl@0
   464
    COMMIT;
sl@0
   465
    SELECT * FROM t1;
sl@0
   466
  }
sl@0
   467
} {1 {no such table: t1}}
sl@0
   468
sl@0
   469
# Repeat on a table with an automatically generated index.
sl@0
   470
#
sl@0
   471
do_test trans-6.10 {
sl@0
   472
  execsql2 {
sl@0
   473
    CREATE TABLE t1(a unique,b,c);
sl@0
   474
    INSERT INTO t1 VALUES(1,2,3);
sl@0
   475
    BEGIN TRANSACTION;
sl@0
   476
    DROP TABLE t1;
sl@0
   477
    CREATE TABLE t1(p unique,q,r);
sl@0
   478
    ROLLBACK;
sl@0
   479
    SELECT * FROM t1;
sl@0
   480
  }
sl@0
   481
} {a 1 b 2 c 3}
sl@0
   482
do_test trans-6.11 {
sl@0
   483
  execsql2 {
sl@0
   484
    BEGIN TRANSACTION;
sl@0
   485
    DROP TABLE t1;
sl@0
   486
    CREATE TABLE t1(p unique,q,r);
sl@0
   487
    COMMIT;
sl@0
   488
    SELECT * FROM t1;
sl@0
   489
  }
sl@0
   490
} {}
sl@0
   491
do_test trans-6.12 {
sl@0
   492
  execsql2 {
sl@0
   493
    INSERT INTO t1 VALUES(1,2,3);
sl@0
   494
    SELECT * FROM t1;
sl@0
   495
  }
sl@0
   496
} {p 1 q 2 r 3}
sl@0
   497
do_test trans-6.13 {
sl@0
   498
  execsql2 {
sl@0
   499
    BEGIN TRANSACTION;
sl@0
   500
    DROP TABLE t1;
sl@0
   501
    CREATE TABLE t1(a unique,b,c);
sl@0
   502
    INSERT INTO t1 VALUES(4,5,6);
sl@0
   503
    SELECT * FROM t1;
sl@0
   504
    DROP TABLE t1;
sl@0
   505
  }
sl@0
   506
} {a 4 b 5 c 6}
sl@0
   507
do_test trans-6.14 {
sl@0
   508
  execsql2 {
sl@0
   509
    ROLLBACK;
sl@0
   510
    SELECT * FROM t1;
sl@0
   511
  }
sl@0
   512
} {p 1 q 2 r 3}
sl@0
   513
do_test trans-6.15 {
sl@0
   514
  execsql2 {
sl@0
   515
    BEGIN TRANSACTION;
sl@0
   516
    DROP TABLE t1;
sl@0
   517
    CREATE TABLE t1(a unique,b,c);
sl@0
   518
    INSERT INTO t1 VALUES(4,5,6);
sl@0
   519
    SELECT * FROM t1;
sl@0
   520
    DROP TABLE t1;
sl@0
   521
  }
sl@0
   522
} {a 4 b 5 c 6}
sl@0
   523
do_test trans-6.16 {
sl@0
   524
  catchsql {
sl@0
   525
    COMMIT;
sl@0
   526
    SELECT * FROM t1;
sl@0
   527
  }
sl@0
   528
} {1 {no such table: t1}}
sl@0
   529
sl@0
   530
do_test trans-6.20 {
sl@0
   531
  execsql {
sl@0
   532
    CREATE TABLE t1(a integer primary key,b,c);
sl@0
   533
    INSERT INTO t1 VALUES(1,-2,-3);
sl@0
   534
    INSERT INTO t1 VALUES(4,-5,-6);
sl@0
   535
    SELECT * FROM t1;
sl@0
   536
  }
sl@0
   537
} {1 -2 -3 4 -5 -6}
sl@0
   538
do_test trans-6.21 {
sl@0
   539
  execsql {
sl@0
   540
    CREATE INDEX i1 ON t1(b);
sl@0
   541
    SELECT * FROM t1 WHERE b<1;
sl@0
   542
  }
sl@0
   543
} {4 -5 -6 1 -2 -3}
sl@0
   544
do_test trans-6.22 {
sl@0
   545
  execsql {
sl@0
   546
    BEGIN TRANSACTION;
sl@0
   547
    DROP INDEX i1;
sl@0
   548
    SELECT * FROM t1 WHERE b<1;
sl@0
   549
    ROLLBACK;
sl@0
   550
  }
sl@0
   551
} {1 -2 -3 4 -5 -6}
sl@0
   552
do_test trans-6.23 {
sl@0
   553
  execsql {
sl@0
   554
    SELECT * FROM t1 WHERE b<1;
sl@0
   555
  }
sl@0
   556
} {4 -5 -6 1 -2 -3}
sl@0
   557
do_test trans-6.24 {
sl@0
   558
  execsql {
sl@0
   559
    BEGIN TRANSACTION;
sl@0
   560
    DROP TABLE t1;
sl@0
   561
    ROLLBACK;
sl@0
   562
    SELECT * FROM t1 WHERE b<1;
sl@0
   563
  }
sl@0
   564
} {4 -5 -6 1 -2 -3}
sl@0
   565
sl@0
   566
do_test trans-6.25 {
sl@0
   567
  execsql {
sl@0
   568
    BEGIN TRANSACTION;
sl@0
   569
    DROP INDEX i1;
sl@0
   570
    CREATE INDEX i1 ON t1(c);
sl@0
   571
    SELECT * FROM t1 WHERE b<1;
sl@0
   572
  }
sl@0
   573
} {1 -2 -3 4 -5 -6}
sl@0
   574
do_test trans-6.26 {
sl@0
   575
  execsql {
sl@0
   576
    SELECT * FROM t1 WHERE c<1;
sl@0
   577
  }
sl@0
   578
} {4 -5 -6 1 -2 -3}
sl@0
   579
do_test trans-6.27 {
sl@0
   580
  execsql {
sl@0
   581
    ROLLBACK;
sl@0
   582
    SELECT * FROM t1 WHERE b<1;
sl@0
   583
  }
sl@0
   584
} {4 -5 -6 1 -2 -3}
sl@0
   585
do_test trans-6.28 {
sl@0
   586
  execsql {
sl@0
   587
    SELECT * FROM t1 WHERE c<1;
sl@0
   588
  }
sl@0
   589
} {1 -2 -3 4 -5 -6}
sl@0
   590
sl@0
   591
# The following repeats steps 6.20 through 6.28, but puts a "unique"
sl@0
   592
# constraint the first field of the table in order to generate an
sl@0
   593
# automatic index.
sl@0
   594
#
sl@0
   595
do_test trans-6.30 {
sl@0
   596
  execsql {
sl@0
   597
    BEGIN TRANSACTION;
sl@0
   598
    DROP TABLE t1;
sl@0
   599
    CREATE TABLE t1(a int unique,b,c);
sl@0
   600
    COMMIT;
sl@0
   601
    INSERT INTO t1 VALUES(1,-2,-3);
sl@0
   602
    INSERT INTO t1 VALUES(4,-5,-6);
sl@0
   603
    SELECT * FROM t1 ORDER BY a;
sl@0
   604
  }
sl@0
   605
} {1 -2 -3 4 -5 -6}
sl@0
   606
do_test trans-6.31 {
sl@0
   607
  execsql {
sl@0
   608
    CREATE INDEX i1 ON t1(b);
sl@0
   609
    SELECT * FROM t1 WHERE b<1;
sl@0
   610
  }
sl@0
   611
} {4 -5 -6 1 -2 -3}
sl@0
   612
do_test trans-6.32 {
sl@0
   613
  execsql {
sl@0
   614
    BEGIN TRANSACTION;
sl@0
   615
    DROP INDEX i1;
sl@0
   616
    SELECT * FROM t1 WHERE b<1;
sl@0
   617
    ROLLBACK;
sl@0
   618
  }
sl@0
   619
} {1 -2 -3 4 -5 -6}
sl@0
   620
do_test trans-6.33 {
sl@0
   621
  execsql {
sl@0
   622
    SELECT * FROM t1 WHERE b<1;
sl@0
   623
  }
sl@0
   624
} {4 -5 -6 1 -2 -3}
sl@0
   625
do_test trans-6.34 {
sl@0
   626
  execsql {
sl@0
   627
    BEGIN TRANSACTION;
sl@0
   628
    DROP TABLE t1;
sl@0
   629
    ROLLBACK;
sl@0
   630
    SELECT * FROM t1 WHERE b<1;
sl@0
   631
  }
sl@0
   632
} {4 -5 -6 1 -2 -3}
sl@0
   633
sl@0
   634
do_test trans-6.35 {
sl@0
   635
  execsql {
sl@0
   636
    BEGIN TRANSACTION;
sl@0
   637
    DROP INDEX i1;
sl@0
   638
    CREATE INDEX i1 ON t1(c);
sl@0
   639
    SELECT * FROM t1 WHERE b<1;
sl@0
   640
  }
sl@0
   641
} {1 -2 -3 4 -5 -6}
sl@0
   642
do_test trans-6.36 {
sl@0
   643
  execsql {
sl@0
   644
    SELECT * FROM t1 WHERE c<1;
sl@0
   645
  }
sl@0
   646
} {4 -5 -6 1 -2 -3}
sl@0
   647
do_test trans-6.37 {
sl@0
   648
  execsql {
sl@0
   649
    DROP INDEX i1;
sl@0
   650
    SELECT * FROM t1 WHERE c<1;
sl@0
   651
  }
sl@0
   652
} {1 -2 -3 4 -5 -6}
sl@0
   653
do_test trans-6.38 {
sl@0
   654
  execsql {
sl@0
   655
    ROLLBACK;
sl@0
   656
    SELECT * FROM t1 WHERE b<1;
sl@0
   657
  }
sl@0
   658
} {4 -5 -6 1 -2 -3}
sl@0
   659
do_test trans-6.39 {
sl@0
   660
  execsql {
sl@0
   661
    SELECT * FROM t1 WHERE c<1;
sl@0
   662
  }
sl@0
   663
} {1 -2 -3 4 -5 -6}
sl@0
   664
integrity_check trans-6.40
sl@0
   665
sl@0
   666
# Test to make sure rollback restores the database back to its original
sl@0
   667
# state.
sl@0
   668
#
sl@0
   669
do_test trans-7.1 {
sl@0
   670
  execsql {BEGIN}
sl@0
   671
  for {set i 0} {$i<1000} {incr i} {
sl@0
   672
    set r1 [expr {rand()}]
sl@0
   673
    set r2 [expr {rand()}]
sl@0
   674
    set r3 [expr {rand()}]
sl@0
   675
    execsql "INSERT INTO t2 VALUES($r1,$r2,$r3)"
sl@0
   676
  }
sl@0
   677
  execsql {COMMIT}
sl@0
   678
  set ::checksum [execsql {SELECT md5sum(x,y,z) FROM t2}]
sl@0
   679
  set ::checksum2 [
sl@0
   680
    execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
sl@0
   681
  ]
sl@0
   682
  execsql {SELECT count(*) FROM t2}
sl@0
   683
} {1001}
sl@0
   684
do_test trans-7.2 {
sl@0
   685
  execsql {SELECT md5sum(x,y,z) FROM t2}
sl@0
   686
} $checksum
sl@0
   687
do_test trans-7.2.1 {
sl@0
   688
  execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
sl@0
   689
} $checksum2
sl@0
   690
do_test trans-7.3 {
sl@0
   691
  execsql {
sl@0
   692
    BEGIN;
sl@0
   693
    DELETE FROM t2;
sl@0
   694
    ROLLBACK;
sl@0
   695
    SELECT md5sum(x,y,z) FROM t2;
sl@0
   696
  }
sl@0
   697
} $checksum
sl@0
   698
do_test trans-7.4 {
sl@0
   699
  execsql {
sl@0
   700
    BEGIN;
sl@0
   701
    INSERT INTO t2 SELECT * FROM t2;
sl@0
   702
    ROLLBACK;
sl@0
   703
    SELECT md5sum(x,y,z) FROM t2;
sl@0
   704
  }
sl@0
   705
} $checksum
sl@0
   706
do_test trans-7.5 {
sl@0
   707
  execsql {
sl@0
   708
    BEGIN;
sl@0
   709
    DELETE FROM t2;
sl@0
   710
    ROLLBACK;
sl@0
   711
    SELECT md5sum(x,y,z) FROM t2;
sl@0
   712
  }
sl@0
   713
} $checksum
sl@0
   714
do_test trans-7.6 {
sl@0
   715
  execsql {
sl@0
   716
    BEGIN;
sl@0
   717
    INSERT INTO t2 SELECT * FROM t2;
sl@0
   718
    ROLLBACK;
sl@0
   719
    SELECT md5sum(x,y,z) FROM t2;
sl@0
   720
  }
sl@0
   721
} $checksum
sl@0
   722
do_test trans-7.7 {
sl@0
   723
  execsql {
sl@0
   724
    BEGIN;
sl@0
   725
    CREATE TABLE t3 AS SELECT * FROM t2;
sl@0
   726
    INSERT INTO t2 SELECT * FROM t3;
sl@0
   727
    ROLLBACK;
sl@0
   728
    SELECT md5sum(x,y,z) FROM t2;
sl@0
   729
  }
sl@0
   730
} $checksum
sl@0
   731
do_test trans-7.8 {
sl@0
   732
  execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
sl@0
   733
} $checksum2
sl@0
   734
ifcapable tempdb {
sl@0
   735
  do_test trans-7.9 {
sl@0
   736
    execsql {
sl@0
   737
      BEGIN;
sl@0
   738
      CREATE TEMP TABLE t3 AS SELECT * FROM t2;
sl@0
   739
      INSERT INTO t2 SELECT * FROM t3;
sl@0
   740
      ROLLBACK;
sl@0
   741
      SELECT md5sum(x,y,z) FROM t2;
sl@0
   742
    }
sl@0
   743
  } $checksum
sl@0
   744
}
sl@0
   745
do_test trans-7.10 {
sl@0
   746
  execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
sl@0
   747
} $checksum2
sl@0
   748
ifcapable tempdb {
sl@0
   749
  do_test trans-7.11 {
sl@0
   750
    execsql {
sl@0
   751
      BEGIN;
sl@0
   752
      CREATE TEMP TABLE t3 AS SELECT * FROM t2;
sl@0
   753
      INSERT INTO t2 SELECT * FROM t3;
sl@0
   754
      DROP INDEX i2x;
sl@0
   755
      DROP INDEX i2y;
sl@0
   756
      CREATE INDEX i3a ON t3(x);
sl@0
   757
      ROLLBACK;
sl@0
   758
      SELECT md5sum(x,y,z) FROM t2;
sl@0
   759
    }
sl@0
   760
  } $checksum
sl@0
   761
}
sl@0
   762
do_test trans-7.12 {
sl@0
   763
  execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
sl@0
   764
} $checksum2
sl@0
   765
ifcapable tempdb {
sl@0
   766
  do_test trans-7.13 {
sl@0
   767
    execsql {
sl@0
   768
      BEGIN;
sl@0
   769
      DROP TABLE t2;
sl@0
   770
      ROLLBACK;
sl@0
   771
      SELECT md5sum(x,y,z) FROM t2;
sl@0
   772
    }
sl@0
   773
  } $checksum
sl@0
   774
}
sl@0
   775
do_test trans-7.14 {
sl@0
   776
  execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
sl@0
   777
} $checksum2
sl@0
   778
integrity_check trans-7.15
sl@0
   779
sl@0
   780
# Arrange for another process to begin modifying the database but abort
sl@0
   781
# and die in the middle of the modification.  Then have this process read
sl@0
   782
# the database.  This process should detect the journal file and roll it
sl@0
   783
# back.  Verify that this happens correctly.
sl@0
   784
#
sl@0
   785
print_text "TclSqlite3-2" "begin"
sl@0
   786
set fd [open test.tcl w]
sl@0
   787
puts $fd {
sl@0
   788
  sqlite3 db test.db
sl@0
   789
  db eval {
sl@0
   790
    PRAGMA default_cache_size=20;
sl@0
   791
    BEGIN;
sl@0
   792
    CREATE TABLE t3 AS SELECT * FROM t2;
sl@0
   793
    DELETE FROM t2;
sl@0
   794
  }
sl@0
   795
  sqlite_abort
sl@0
   796
}
sl@0
   797
close $fd
sl@0
   798
print_text "TclSqlite3-2" "end"
sl@0
   799
  
sl@0
   800
do_test trans-8.1 {
sl@0
   801
  catch {exec [info nameofexec] test.tcl}
sl@0
   802
  execsql {SELECT md5sum(x,y,z) FROM t2}
sl@0
   803
} $checksum
sl@0
   804
do_test trans-8.2 {
sl@0
   805
  execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
sl@0
   806
} $checksum2
sl@0
   807
integrity_check trans-8.3
sl@0
   808
sl@0
   809
print_text "TclSqlite3-2" "begin"
sl@0
   810
set fd [open test.tcl w]
sl@0
   811
puts $fd {
sl@0
   812
  sqlite3 db test.db
sl@0
   813
  db eval {
sl@0
   814
    PRAGMA journal_mode=persist;
sl@0
   815
    PRAGMA default_cache_size=20;
sl@0
   816
    BEGIN;
sl@0
   817
    CREATE TABLE t3 AS SELECT * FROM t2;
sl@0
   818
    DELETE FROM t2;
sl@0
   819
  }
sl@0
   820
  sqlite_abort
sl@0
   821
}
sl@0
   822
close $fd
sl@0
   823
print_text "TclSqlite3-2" "end"
sl@0
   824
sl@0
   825
do_test trans-8.4 {
sl@0
   826
  catch {exec [info nameofexec] test.tcl}
sl@0
   827
  execsql {SELECT md5sum(x,y,z) FROM t2}
sl@0
   828
} $checksum
sl@0
   829
do_test trans-8.5 {
sl@0
   830
  execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
sl@0
   831
} $checksum2
sl@0
   832
integrity_check trans-8.6
sl@0
   833
sl@0
   834
# In the following sequence of tests, compute the MD5 sum of the content
sl@0
   835
# of a table, make lots of modifications to that table, then do a rollback.
sl@0
   836
# Verify that after the rollback, the MD5 checksum is unchanged.
sl@0
   837
#
sl@0
   838
do_test trans-9.1 {
sl@0
   839
  execsql {
sl@0
   840
    PRAGMA default_cache_size=10;
sl@0
   841
  }
sl@0
   842
  db close
sl@0
   843
  sqlite3 db test.db
sl@0
   844
  execsql {
sl@0
   845
    BEGIN;
sl@0
   846
    CREATE TABLE t3(x TEXT);
sl@0
   847
    INSERT INTO t3 VALUES(randstr(10,400));
sl@0
   848
    INSERT INTO t3 VALUES(randstr(10,400));
sl@0
   849
    INSERT INTO t3 SELECT randstr(10,400) FROM t3;
sl@0
   850
    INSERT INTO t3 SELECT randstr(10,400) FROM t3;
sl@0
   851
    INSERT INTO t3 SELECT randstr(10,400) FROM t3;
sl@0
   852
    INSERT INTO t3 SELECT randstr(10,400) FROM t3;
sl@0
   853
    INSERT INTO t3 SELECT randstr(10,400) FROM t3;
sl@0
   854
    INSERT INTO t3 SELECT randstr(10,400) FROM t3;
sl@0
   855
    INSERT INTO t3 SELECT randstr(10,400) FROM t3;
sl@0
   856
    INSERT INTO t3 SELECT randstr(10,400) FROM t3;
sl@0
   857
    INSERT INTO t3 SELECT randstr(10,400) FROM t3;
sl@0
   858
    COMMIT;
sl@0
   859
    SELECT count(*) FROM t3;
sl@0
   860
  }
sl@0
   861
} {1024}
sl@0
   862
sl@0
   863
# The following procedure computes a "signature" for table "t3".  If
sl@0
   864
# T3 changes in any way, the signature should change.  
sl@0
   865
#
sl@0
   866
# This is used to test ROLLBACK.  We gather a signature for t3, then
sl@0
   867
# make lots of changes to t3, then rollback and take another signature.
sl@0
   868
# The two signatures should be the same.
sl@0
   869
#
sl@0
   870
proc signature {} {
sl@0
   871
  return [db eval {SELECT count(*), md5sum(x) FROM t3}]
sl@0
   872
}
sl@0
   873
sl@0
   874
# Repeat the following group of tests 20 times for quick testing and
sl@0
   875
# 40 times for full testing.  Each iteration of the test makes table
sl@0
   876
# t3 a little larger, and thus takes a little longer, so doing 40 tests
sl@0
   877
# is more than 2.0 times slower than doing 20 tests.  Considerably more.
sl@0
   878
#
sl@0
   879
if {[info exists ISQUICK]} {
sl@0
   880
  set limit 20
sl@0
   881
} elseif {[info exists SOAKTEST]} {
sl@0
   882
  set limit 100
sl@0
   883
} else {
sl@0
   884
  set limit 40
sl@0
   885
}
sl@0
   886
sl@0
   887
# Do rollbacks.  Make sure the signature does not change.
sl@0
   888
#
sl@0
   889
for {set i 2} {$i<=$limit} {incr i} {
sl@0
   890
  set ::sig [signature]
sl@0
   891
  set cnt [lindex $::sig 0]
sl@0
   892
  if {$i%2==0} {
sl@0
   893
    execsql {PRAGMA fullfsync=ON}
sl@0
   894
  } else {
sl@0
   895
    execsql {PRAGMA fullfsync=OFF}
sl@0
   896
  }
sl@0
   897
  set sqlite_sync_count 0
sl@0
   898
  set sqlite_fullsync_count 0
sl@0
   899
  do_test trans-9.$i.1-$cnt {
sl@0
   900
     execsql {
sl@0
   901
       BEGIN;
sl@0
   902
       DELETE FROM t3 WHERE random()%10!=0;
sl@0
   903
       INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
sl@0
   904
       INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
sl@0
   905
       ROLLBACK;
sl@0
   906
     }
sl@0
   907
     signature
sl@0
   908
  } $sig
sl@0
   909
  do_test trans-9.$i.2-$cnt {
sl@0
   910
     execsql {
sl@0
   911
       BEGIN;
sl@0
   912
       DELETE FROM t3 WHERE random()%10!=0;
sl@0
   913
       INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
sl@0
   914
       DELETE FROM t3 WHERE random()%10!=0;
sl@0
   915
       INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
sl@0
   916
       ROLLBACK;
sl@0
   917
     }
sl@0
   918
     signature
sl@0
   919
  } $sig
sl@0
   920
  if {$i<$limit} {
sl@0
   921
    do_test trans-9.$i.3-$cnt {
sl@0
   922
       execsql {
sl@0
   923
         INSERT INTO t3 SELECT randstr(10,400) FROM t3 WHERE random()%10==0;
sl@0
   924
       }
sl@0
   925
    } {}
sl@0
   926
    if {$tcl_platform(platform)=="unix"} {
sl@0
   927
      do_test trans-9.$i.4-$cnt {
sl@0
   928
         expr {$sqlite_sync_count>0}
sl@0
   929
      } 1
sl@0
   930
      ifcapable pager_pragmas {
sl@0
   931
        do_test trans-9.$i.5-$cnt {
sl@0
   932
           expr {$sqlite_fullsync_count>0}
sl@0
   933
        } [expr {$i%2==0}]
sl@0
   934
      } else {
sl@0
   935
        do_test trans-9.$i.5-$cnt {
sl@0
   936
          expr {$sqlite_fullsync_count==0}
sl@0
   937
        } {1}
sl@0
   938
      }
sl@0
   939
    }
sl@0
   940
  }
sl@0
   941
  set ::pager_old_format 0
sl@0
   942
}
sl@0
   943
   
sl@0
   944
finish_test