os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/autoinc.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
# 2004 November 12
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 testing the AUTOINCREMENT features.
sl@0
    13
#
sl@0
    14
# $Id: autoinc.test,v 1.13 2008/08/11 18:44:58 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
# If the library is not compiled with autoincrement support then
sl@0
    21
# skip all tests in this file.
sl@0
    22
#
sl@0
    23
ifcapable {!autoinc} {
sl@0
    24
  finish_test
sl@0
    25
  return
sl@0
    26
}
sl@0
    27
sl@0
    28
# The database is initially empty.
sl@0
    29
#
sl@0
    30
do_test autoinc-1.1 {
sl@0
    31
  execsql {
sl@0
    32
    SELECT name FROM sqlite_master WHERE type='table';
sl@0
    33
  }
sl@0
    34
} {}
sl@0
    35
sl@0
    36
# Add a table with the AUTOINCREMENT feature.  Verify that the
sl@0
    37
# SQLITE_SEQUENCE table gets created.
sl@0
    38
#
sl@0
    39
do_test autoinc-1.2 {
sl@0
    40
  execsql {
sl@0
    41
    CREATE TABLE t1(x INTEGER PRIMARY KEY AUTOINCREMENT, y);
sl@0
    42
    SELECT name FROM sqlite_master WHERE type='table';
sl@0
    43
  }
sl@0
    44
} {t1 sqlite_sequence}
sl@0
    45
sl@0
    46
# The SQLITE_SEQUENCE table is initially empty
sl@0
    47
#
sl@0
    48
do_test autoinc-1.3 {
sl@0
    49
  execsql {
sl@0
    50
    SELECT * FROM sqlite_sequence;
sl@0
    51
  }
sl@0
    52
} {}
sl@0
    53
do_test autoinc-1.3.1 {
sl@0
    54
  catchsql {
sl@0
    55
    CREATE INDEX seqidx ON sqlite_sequence(name)
sl@0
    56
  }
sl@0
    57
} {1 {table sqlite_sequence may not be indexed}}
sl@0
    58
sl@0
    59
# Close and reopen the database.  Verify that everything is still there.
sl@0
    60
#
sl@0
    61
do_test autoinc-1.4 {
sl@0
    62
  db close
sl@0
    63
  sqlite3 db test.db
sl@0
    64
  execsql {
sl@0
    65
    SELECT * FROM sqlite_sequence;
sl@0
    66
  }
sl@0
    67
} {}
sl@0
    68
sl@0
    69
# We are not allowed to drop the sqlite_sequence table.
sl@0
    70
#
sl@0
    71
do_test autoinc-1.5 {
sl@0
    72
  catchsql {DROP TABLE sqlite_sequence}
sl@0
    73
} {1 {table sqlite_sequence may not be dropped}}
sl@0
    74
do_test autoinc-1.6 {
sl@0
    75
  execsql {SELECT name FROM sqlite_master WHERE type='table'}
sl@0
    76
} {t1 sqlite_sequence}
sl@0
    77
sl@0
    78
# Insert an entries into the t1 table and make sure the largest key
sl@0
    79
# is always recorded in the sqlite_sequence table.
sl@0
    80
#
sl@0
    81
do_test autoinc-2.1 {
sl@0
    82
  execsql {
sl@0
    83
    SELECT * FROM sqlite_sequence
sl@0
    84
  }
sl@0
    85
} {}
sl@0
    86
do_test autoinc-2.2 {
sl@0
    87
  execsql {
sl@0
    88
    INSERT INTO t1 VALUES(12,34);
sl@0
    89
    SELECT * FROM sqlite_sequence;
sl@0
    90
  }
sl@0
    91
} {t1 12}
sl@0
    92
do_test autoinc-2.3 {
sl@0
    93
  execsql {
sl@0
    94
    INSERT INTO t1 VALUES(1,23);
sl@0
    95
    SELECT * FROM sqlite_sequence;
sl@0
    96
  }
sl@0
    97
} {t1 12}
sl@0
    98
do_test autoinc-2.4 {
sl@0
    99
  execsql {
sl@0
   100
    INSERT INTO t1 VALUES(123,456);
sl@0
   101
    SELECT * FROM sqlite_sequence;
sl@0
   102
  }
sl@0
   103
} {t1 123}
sl@0
   104
do_test autoinc-2.5 {
sl@0
   105
  execsql {
sl@0
   106
    INSERT INTO t1 VALUES(NULL,567);
sl@0
   107
    SELECT * FROM sqlite_sequence;
sl@0
   108
  }
sl@0
   109
} {t1 124}
sl@0
   110
do_test autoinc-2.6 {
sl@0
   111
  execsql {
sl@0
   112
    DELETE FROM t1 WHERE y=567;
sl@0
   113
    SELECT * FROM sqlite_sequence;
sl@0
   114
  }
sl@0
   115
} {t1 124}
sl@0
   116
do_test autoinc-2.7 {
sl@0
   117
  execsql {
sl@0
   118
    INSERT INTO t1 VALUES(NULL,567);
sl@0
   119
    SELECT * FROM sqlite_sequence;
sl@0
   120
  }
sl@0
   121
} {t1 125}
sl@0
   122
do_test autoinc-2.8 {
sl@0
   123
  execsql {
sl@0
   124
    DELETE FROM t1;
sl@0
   125
    SELECT * FROM sqlite_sequence;
sl@0
   126
  }
sl@0
   127
} {t1 125}
sl@0
   128
do_test autoinc-2.9 {
sl@0
   129
  execsql {
sl@0
   130
    INSERT INTO t1 VALUES(12,34);
sl@0
   131
    SELECT * FROM sqlite_sequence;
sl@0
   132
  }
sl@0
   133
} {t1 125}
sl@0
   134
do_test autoinc-2.10 {
sl@0
   135
  execsql {
sl@0
   136
    INSERT INTO t1 VALUES(125,456);
sl@0
   137
    SELECT * FROM sqlite_sequence;
sl@0
   138
  }
sl@0
   139
} {t1 125}
sl@0
   140
do_test autoinc-2.11 {
sl@0
   141
  execsql {
sl@0
   142
    INSERT INTO t1 VALUES(-1234567,-1);
sl@0
   143
    SELECT * FROM sqlite_sequence;
sl@0
   144
  }
sl@0
   145
} {t1 125}
sl@0
   146
do_test autoinc-2.12 {
sl@0
   147
  execsql {
sl@0
   148
    INSERT INTO t1 VALUES(234,5678);
sl@0
   149
    SELECT * FROM sqlite_sequence;
sl@0
   150
  }
sl@0
   151
} {t1 234}
sl@0
   152
do_test autoinc-2.13 {
sl@0
   153
  execsql {
sl@0
   154
    DELETE FROM t1;
sl@0
   155
    INSERT INTO t1 VALUES(NULL,1);
sl@0
   156
    SELECT * FROM sqlite_sequence;
sl@0
   157
  }
sl@0
   158
} {t1 235}
sl@0
   159
do_test autoinc-2.14 {
sl@0
   160
  execsql {
sl@0
   161
    SELECT * FROM t1;
sl@0
   162
  }
sl@0
   163
} {235 1}
sl@0
   164
sl@0
   165
# Manually change the autoincrement values in sqlite_sequence.
sl@0
   166
#
sl@0
   167
do_test autoinc-2.20 {
sl@0
   168
  execsql {
sl@0
   169
    UPDATE sqlite_sequence SET seq=1234 WHERE name='t1';
sl@0
   170
    INSERT INTO t1 VALUES(NULL,2);
sl@0
   171
    SELECT * FROM t1;
sl@0
   172
  }
sl@0
   173
} {235 1 1235 2}
sl@0
   174
do_test autoinc-2.21 {
sl@0
   175
  execsql {
sl@0
   176
    SELECT * FROM sqlite_sequence;
sl@0
   177
  }
sl@0
   178
} {t1 1235}
sl@0
   179
do_test autoinc-2.22 {
sl@0
   180
  execsql {
sl@0
   181
    UPDATE sqlite_sequence SET seq=NULL WHERE name='t1';
sl@0
   182
    INSERT INTO t1 VALUES(NULL,3);
sl@0
   183
    SELECT * FROM t1;
sl@0
   184
  }
sl@0
   185
} {235 1 1235 2 1236 3}
sl@0
   186
do_test autoinc-2.23 {
sl@0
   187
  execsql {
sl@0
   188
    SELECT * FROM sqlite_sequence;
sl@0
   189
  }
sl@0
   190
} {t1 1236}
sl@0
   191
do_test autoinc-2.24 {
sl@0
   192
  execsql {
sl@0
   193
    UPDATE sqlite_sequence SET seq='a-string' WHERE name='t1';
sl@0
   194
    INSERT INTO t1 VALUES(NULL,4);
sl@0
   195
    SELECT * FROM t1;
sl@0
   196
  }
sl@0
   197
} {235 1 1235 2 1236 3 1237 4}
sl@0
   198
do_test autoinc-2.25 {
sl@0
   199
  execsql {
sl@0
   200
    SELECT * FROM sqlite_sequence;
sl@0
   201
  }
sl@0
   202
} {t1 1237}
sl@0
   203
do_test autoinc-2.26 {
sl@0
   204
  execsql {
sl@0
   205
    DELETE FROM sqlite_sequence WHERE name='t1';
sl@0
   206
    INSERT INTO t1 VALUES(NULL,5);
sl@0
   207
    SELECT * FROM t1;
sl@0
   208
  }
sl@0
   209
} {235 1 1235 2 1236 3 1237 4 1238 5}
sl@0
   210
do_test autoinc-2.27 {
sl@0
   211
  execsql {
sl@0
   212
    SELECT * FROM sqlite_sequence;
sl@0
   213
  }
sl@0
   214
} {t1 1238}
sl@0
   215
do_test autoinc-2.28 {
sl@0
   216
  execsql {
sl@0
   217
    UPDATE sqlite_sequence SET seq='12345678901234567890'
sl@0
   218
      WHERE name='t1';
sl@0
   219
    INSERT INTO t1 VALUES(NULL,6);
sl@0
   220
    SELECT * FROM t1;
sl@0
   221
  }
sl@0
   222
} {235 1 1235 2 1236 3 1237 4 1238 5 1239 6}
sl@0
   223
do_test autoinc-2.29 {
sl@0
   224
  execsql {
sl@0
   225
    SELECT * FROM sqlite_sequence;
sl@0
   226
  }
sl@0
   227
} {t1 1239}
sl@0
   228
sl@0
   229
# Test multi-row inserts
sl@0
   230
#
sl@0
   231
do_test autoinc-2.50 {
sl@0
   232
  execsql {
sl@0
   233
    DELETE FROM t1 WHERE y>=3;
sl@0
   234
    INSERT INTO t1 SELECT NULL, y+2 FROM t1;
sl@0
   235
    SELECT * FROM t1;
sl@0
   236
  }
sl@0
   237
} {235 1 1235 2 1240 3 1241 4}
sl@0
   238
do_test autoinc-2.51 {
sl@0
   239
  execsql {
sl@0
   240
    SELECT * FROM sqlite_sequence
sl@0
   241
  }
sl@0
   242
} {t1 1241}
sl@0
   243
sl@0
   244
ifcapable tempdb {
sl@0
   245
  do_test autoinc-2.52 {
sl@0
   246
    execsql {
sl@0
   247
      CREATE TEMP TABLE t2 AS SELECT y FROM t1;
sl@0
   248
    }
sl@0
   249
    execsql {
sl@0
   250
      INSERT INTO t1 SELECT NULL, y+4 FROM t2;
sl@0
   251
      SELECT * FROM t1;
sl@0
   252
    }
sl@0
   253
  } {235 1 1235 2 1240 3 1241 4 1242 5 1243 6 1244 7 1245 8}
sl@0
   254
  do_test autoinc-2.53 {
sl@0
   255
    execsql {
sl@0
   256
      SELECT * FROM sqlite_sequence
sl@0
   257
    }
sl@0
   258
  } {t1 1245}
sl@0
   259
  do_test autoinc-2.54 {
sl@0
   260
    execsql {
sl@0
   261
      DELETE FROM t1;
sl@0
   262
      INSERT INTO t1 SELECT NULL, y FROM t2;
sl@0
   263
      SELECT * FROM t1;
sl@0
   264
    }
sl@0
   265
  } {1246 1 1247 2 1248 3 1249 4}
sl@0
   266
  do_test autoinc-2.55 {
sl@0
   267
    execsql {
sl@0
   268
      SELECT * FROM sqlite_sequence
sl@0
   269
    }
sl@0
   270
  } {t1 1249}
sl@0
   271
}
sl@0
   272
sl@0
   273
# Create multiple AUTOINCREMENT tables.  Make sure all sequences are
sl@0
   274
# tracked separately and do not interfere with one another.
sl@0
   275
#
sl@0
   276
do_test autoinc-2.70 {
sl@0
   277
  catchsql {
sl@0
   278
    DROP TABLE t2;
sl@0
   279
  }
sl@0
   280
  execsql {
sl@0
   281
    CREATE TABLE t2(d, e INTEGER PRIMARY KEY AUTOINCREMENT, f);
sl@0
   282
    INSERT INTO t2(d) VALUES(1);
sl@0
   283
    SELECT * FROM sqlite_sequence;
sl@0
   284
  }
sl@0
   285
} [ifcapable tempdb {list t1 1249 t2 1} else {list t1 1241 t2 1}]
sl@0
   286
do_test autoinc-2.71 {
sl@0
   287
  execsql {
sl@0
   288
    INSERT INTO t2(d) VALUES(2);
sl@0
   289
    SELECT * FROM sqlite_sequence;
sl@0
   290
  }
sl@0
   291
} [ifcapable tempdb {list t1 1249 t2 2} else {list t1 1241 t2 2}]
sl@0
   292
do_test autoinc-2.72 {
sl@0
   293
  execsql {
sl@0
   294
    INSERT INTO t1(x) VALUES(10000);
sl@0
   295
    SELECT * FROM sqlite_sequence;
sl@0
   296
  }
sl@0
   297
} {t1 10000 t2 2}
sl@0
   298
do_test autoinc-2.73 {
sl@0
   299
  execsql {
sl@0
   300
    CREATE TABLE t3(g INTEGER PRIMARY KEY AUTOINCREMENT, h);
sl@0
   301
    INSERT INTO t3(h) VALUES(1);
sl@0
   302
    SELECT * FROM sqlite_sequence;
sl@0
   303
  }
sl@0
   304
} {t1 10000 t2 2 t3 1}
sl@0
   305
do_test autoinc-2.74 {
sl@0
   306
  execsql {
sl@0
   307
    INSERT INTO t2(d,e) VALUES(3,100);
sl@0
   308
    SELECT * FROM sqlite_sequence;
sl@0
   309
  }
sl@0
   310
} {t1 10000 t2 100 t3 1}
sl@0
   311
sl@0
   312
sl@0
   313
# When a table with an AUTOINCREMENT is deleted, the corresponding entry
sl@0
   314
# in the SQLITE_SEQUENCE table should also be deleted.  But the SQLITE_SEQUENCE
sl@0
   315
# table itself should remain behind.
sl@0
   316
#
sl@0
   317
do_test autoinc-3.1 {
sl@0
   318
  execsql {SELECT name FROM sqlite_sequence}
sl@0
   319
} {t1 t2 t3}
sl@0
   320
do_test autoinc-3.2 {
sl@0
   321
  execsql {
sl@0
   322
    DROP TABLE t1;
sl@0
   323
    SELECT name FROM sqlite_sequence;
sl@0
   324
  }
sl@0
   325
} {t2 t3}
sl@0
   326
do_test autoinc-3.3 {
sl@0
   327
  execsql {
sl@0
   328
    DROP TABLE t3;
sl@0
   329
    SELECT name FROM sqlite_sequence;
sl@0
   330
  }
sl@0
   331
} {t2}
sl@0
   332
do_test autoinc-3.4 {
sl@0
   333
  execsql {
sl@0
   334
    DROP TABLE t2;
sl@0
   335
    SELECT name FROM sqlite_sequence;
sl@0
   336
  }
sl@0
   337
} {}
sl@0
   338
sl@0
   339
# AUTOINCREMENT on TEMP tables.
sl@0
   340
#
sl@0
   341
ifcapable tempdb {
sl@0
   342
  do_test autoinc-4.1 {
sl@0
   343
    execsql {
sl@0
   344
      SELECT 1, name FROM sqlite_master WHERE type='table';
sl@0
   345
      SELECT 2, name FROM sqlite_temp_master WHERE type='table';
sl@0
   346
    }
sl@0
   347
  } {1 sqlite_sequence}
sl@0
   348
  do_test autoinc-4.2 {
sl@0
   349
    execsql {
sl@0
   350
      CREATE TABLE t1(x INTEGER PRIMARY KEY AUTOINCREMENT, y);
sl@0
   351
      CREATE TEMP TABLE t3(a INTEGER PRIMARY KEY AUTOINCREMENT, b);
sl@0
   352
      SELECT 1, name FROM sqlite_master WHERE type='table';
sl@0
   353
      SELECT 2, name FROM sqlite_temp_master WHERE type='table';
sl@0
   354
    }
sl@0
   355
  } {1 sqlite_sequence 1 t1 2 t3 2 sqlite_sequence}
sl@0
   356
  do_test autoinc-4.3 {
sl@0
   357
    execsql {
sl@0
   358
      SELECT 1, * FROM main.sqlite_sequence;
sl@0
   359
      SELECT 2, * FROM temp.sqlite_sequence;
sl@0
   360
    }
sl@0
   361
  } {}
sl@0
   362
  do_test autoinc-4.4 {
sl@0
   363
    execsql {
sl@0
   364
      INSERT INTO t1 VALUES(10,1);
sl@0
   365
      INSERT INTO t3 VALUES(20,2);
sl@0
   366
      INSERT INTO t1 VALUES(NULL,3);
sl@0
   367
      INSERT INTO t3 VALUES(NULL,4);
sl@0
   368
    }
sl@0
   369
  } {}
sl@0
   370
  
sl@0
   371
  ifcapable compound {
sl@0
   372
  do_test autoinc-4.4.1 {
sl@0
   373
    execsql {
sl@0
   374
      SELECT * FROM t1 UNION ALL SELECT * FROM t3;
sl@0
   375
    }
sl@0
   376
  } {10 1 11 3 20 2 21 4}
sl@0
   377
  } ;# ifcapable compound
sl@0
   378
  
sl@0
   379
  do_test autoinc-4.5 {
sl@0
   380
    execsql {
sl@0
   381
      SELECT 1, * FROM main.sqlite_sequence;
sl@0
   382
      SELECT 2, * FROM temp.sqlite_sequence;
sl@0
   383
    }
sl@0
   384
  } {1 t1 11 2 t3 21}
sl@0
   385
  do_test autoinc-4.6 {
sl@0
   386
    execsql {
sl@0
   387
      INSERT INTO t1 SELECT * FROM t3;
sl@0
   388
      SELECT 1, * FROM main.sqlite_sequence;
sl@0
   389
      SELECT 2, * FROM temp.sqlite_sequence;
sl@0
   390
    }
sl@0
   391
  } {1 t1 21 2 t3 21}
sl@0
   392
  do_test autoinc-4.7 {
sl@0
   393
    execsql {
sl@0
   394
      INSERT INTO t3 SELECT x+100, y  FROM t1;
sl@0
   395
      SELECT 1, * FROM main.sqlite_sequence;
sl@0
   396
      SELECT 2, * FROM temp.sqlite_sequence;
sl@0
   397
    }
sl@0
   398
  } {1 t1 21 2 t3 121}
sl@0
   399
  do_test autoinc-4.8 {
sl@0
   400
    execsql {
sl@0
   401
      DROP TABLE t3;
sl@0
   402
      SELECT 1, * FROM main.sqlite_sequence;
sl@0
   403
      SELECT 2, * FROM temp.sqlite_sequence;
sl@0
   404
    }
sl@0
   405
  } {1 t1 21}
sl@0
   406
  do_test autoinc-4.9 {
sl@0
   407
    execsql {
sl@0
   408
      CREATE TEMP TABLE t2(p INTEGER PRIMARY KEY AUTOINCREMENT, q);
sl@0
   409
      INSERT INTO t2 SELECT * FROM t1;
sl@0
   410
      DROP TABLE t1;
sl@0
   411
      SELECT 1, * FROM main.sqlite_sequence;
sl@0
   412
      SELECT 2, * FROM temp.sqlite_sequence;
sl@0
   413
    }
sl@0
   414
  } {2 t2 21}
sl@0
   415
  do_test autoinc-4.10 {
sl@0
   416
    execsql {
sl@0
   417
      DROP TABLE t2;
sl@0
   418
      SELECT 1, * FROM main.sqlite_sequence;
sl@0
   419
      SELECT 2, * FROM temp.sqlite_sequence;
sl@0
   420
    }
sl@0
   421
  } {}
sl@0
   422
}
sl@0
   423
sl@0
   424
# Make sure AUTOINCREMENT works on ATTACH-ed tables.
sl@0
   425
#
sl@0
   426
ifcapable tempdb&&attach {
sl@0
   427
  do_test autoinc-5.1 {
sl@0
   428
    file delete -force test2.db
sl@0
   429
    file delete -force test2.db-journal
sl@0
   430
    sqlite3 db2 test2.db
sl@0
   431
    execsql {
sl@0
   432
      CREATE TABLE t4(m INTEGER PRIMARY KEY AUTOINCREMENT, n);
sl@0
   433
      CREATE TABLE t5(o, p INTEGER PRIMARY KEY AUTOINCREMENT);
sl@0
   434
    } db2;
sl@0
   435
    execsql {
sl@0
   436
      ATTACH 'test2.db' as aux;
sl@0
   437
      SELECT 1, * FROM main.sqlite_sequence;
sl@0
   438
      SELECT 2, * FROM temp.sqlite_sequence;
sl@0
   439
      SELECT 3, * FROM aux.sqlite_sequence;
sl@0
   440
    }
sl@0
   441
  } {}
sl@0
   442
  do_test autoinc-5.2 {
sl@0
   443
    execsql {
sl@0
   444
      INSERT INTO t4 VALUES(NULL,1);
sl@0
   445
      SELECT 1, * FROM main.sqlite_sequence;
sl@0
   446
      SELECT 2, * FROM temp.sqlite_sequence;
sl@0
   447
      SELECT 3, * FROM aux.sqlite_sequence;
sl@0
   448
    }
sl@0
   449
  } {3 t4 1}
sl@0
   450
  do_test autoinc-5.3 {
sl@0
   451
    execsql {
sl@0
   452
      INSERT INTO t5 VALUES(100,200);
sl@0
   453
      SELECT * FROM sqlite_sequence
sl@0
   454
    } db2
sl@0
   455
  } {t4 1 t5 200}
sl@0
   456
  do_test autoinc-5.4 {
sl@0
   457
    execsql {
sl@0
   458
      SELECT 1, * FROM main.sqlite_sequence;
sl@0
   459
      SELECT 2, * FROM temp.sqlite_sequence;
sl@0
   460
      SELECT 3, * FROM aux.sqlite_sequence;
sl@0
   461
    }
sl@0
   462
  } {3 t4 1 3 t5 200}
sl@0
   463
}
sl@0
   464
sl@0
   465
# Requirement REQ00310:  Make sure an insert fails if the sequence is
sl@0
   466
# already at its maximum value.
sl@0
   467
#
sl@0
   468
ifcapable {rowid32} {
sl@0
   469
  do_test autoinc-6.1 {
sl@0
   470
    execsql {
sl@0
   471
      CREATE TABLE t6(v INTEGER PRIMARY KEY AUTOINCREMENT, w);
sl@0
   472
      INSERT INTO t6 VALUES(2147483647,1);
sl@0
   473
      SELECT seq FROM main.sqlite_sequence WHERE name='t6';
sl@0
   474
    }
sl@0
   475
  } 2147483647
sl@0
   476
}
sl@0
   477
ifcapable {!rowid32} {
sl@0
   478
  do_test autoinc-6.1 {
sl@0
   479
    execsql {
sl@0
   480
      CREATE TABLE t6(v INTEGER PRIMARY KEY AUTOINCREMENT, w);
sl@0
   481
      INSERT INTO t6 VALUES(9223372036854775807,1);
sl@0
   482
      SELECT seq FROM main.sqlite_sequence WHERE name='t6';
sl@0
   483
    }
sl@0
   484
  } 9223372036854775807
sl@0
   485
}
sl@0
   486
do_test autoinc-6.2 {
sl@0
   487
  catchsql {
sl@0
   488
    INSERT INTO t6 VALUES(NULL,1);
sl@0
   489
  }
sl@0
   490
} {1 {database or disk is full}}
sl@0
   491
sl@0
   492
# Allow the AUTOINCREMENT keyword inside the parentheses
sl@0
   493
# on a separate PRIMARY KEY designation.
sl@0
   494
#
sl@0
   495
do_test autoinc-7.1 {
sl@0
   496
  execsql {
sl@0
   497
    CREATE TABLE t7(x INTEGER, y REAL, PRIMARY KEY(x AUTOINCREMENT));
sl@0
   498
    INSERT INTO t7(y) VALUES(123);
sl@0
   499
    INSERT INTO t7(y) VALUES(234);
sl@0
   500
    DELETE FROM t7;
sl@0
   501
    INSERT INTO t7(y) VALUES(345);
sl@0
   502
    SELECT * FROM t7;
sl@0
   503
  }
sl@0
   504
} {3 345.0}
sl@0
   505
sl@0
   506
# Test that if the AUTOINCREMENT is applied to a non integer primary key
sl@0
   507
# the error message is sensible.
sl@0
   508
do_test autoinc-7.2 {
sl@0
   509
  catchsql {
sl@0
   510
    CREATE TABLE t8(x TEXT PRIMARY KEY AUTOINCREMENT);
sl@0
   511
  }
sl@0
   512
} {1 {AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY}}
sl@0
   513
sl@0
   514
sl@0
   515
# Ticket #1283.  Make sure that preparing but never running a statement
sl@0
   516
# that creates the sqlite_sequence table does not mess up the database.
sl@0
   517
#
sl@0
   518
do_test autoinc-8.1 {
sl@0
   519
  catch {db2 close}
sl@0
   520
  catch {db close}
sl@0
   521
  file delete -force test.db
sl@0
   522
  sqlite3 db test.db
sl@0
   523
  set DB [sqlite3_connection_pointer db]
sl@0
   524
  set STMT [sqlite3_prepare $DB {
sl@0
   525
     CREATE TABLE t1(
sl@0
   526
       x INTEGER PRIMARY KEY AUTOINCREMENT
sl@0
   527
     )
sl@0
   528
  } -1 TAIL]
sl@0
   529
  sqlite3_finalize $STMT
sl@0
   530
  set STMT [sqlite3_prepare $DB {
sl@0
   531
     CREATE TABLE t1(
sl@0
   532
       x INTEGER PRIMARY KEY AUTOINCREMENT
sl@0
   533
     )
sl@0
   534
  } -1 TAIL]
sl@0
   535
  sqlite3_step $STMT
sl@0
   536
  sqlite3_finalize $STMT
sl@0
   537
  execsql {
sl@0
   538
    INSERT INTO t1 VALUES(NULL);
sl@0
   539
    SELECT * FROM t1;
sl@0
   540
  }
sl@0
   541
} {1}
sl@0
   542
sl@0
   543
# Ticket #3148
sl@0
   544
# Make sure the sqlite_sequence table is not damaged when doing
sl@0
   545
# an empty insert - an INSERT INTO ... SELECT ... where the SELECT
sl@0
   546
# clause returns an empty set.
sl@0
   547
#
sl@0
   548
do_test autoinc-9.1 {
sl@0
   549
  db eval {
sl@0
   550
    CREATE TABLE t2(x INTEGER PRIMARY KEY AUTOINCREMENT, y);
sl@0
   551
    INSERT INTO t2 VALUES(NULL, 1);
sl@0
   552
    CREATE TABLE t3(a INTEGER PRIMARY KEY AUTOINCREMENT, b);
sl@0
   553
    INSERT INTO t3 SELECT * FROM t2 WHERE y>1;
sl@0
   554
sl@0
   555
    SELECT * FROM sqlite_sequence WHERE name='t3';
sl@0
   556
  }
sl@0
   557
} {t3 0}
sl@0
   558
sl@0
   559
sl@0
   560
finish_test