Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Autoincrement is now working and has regression tests. (CVS 2095) |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
10c712a21961dbc3bff89c49d5ec3b84 |
User & Date: | drh 2004-11-13 03:48:07.000 |
Context
2004-11-13
| ||
03:59 | Fix the keyword generator so that it works with SQLITE_OMIT_ALTERTABLE. (CVS 2096) (check-in: 60ace9985d user: drh tags: trunk) | |
03:48 | Autoincrement is now working and has regression tests. (CVS 2095) (check-in: 10c712a219 user: drh tags: trunk) | |
2004-11-12
| ||
16:12 | Change pragmas schema_cookie and user_cookie to schema_version and user_version. (CVS 2094) (check-in: 5e05831844 user: danielk1977 tags: trunk) | |
Changes
Changes to src/build.c.
︙ | ︙ | |||
18 19 20 21 22 23 24 | ** CREATE INDEX ** DROP INDEX ** creating ID lists ** BEGIN TRANSACTION ** COMMIT ** ROLLBACK ** | | | 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | ** CREATE INDEX ** DROP INDEX ** creating ID lists ** BEGIN TRANSACTION ** COMMIT ** ROLLBACK ** ** $Id: build.c,v 1.279 2004/11/13 03:48:07 drh Exp $ */ #include "sqliteInt.h" #include <ctype.h> /* ** This routine is called when a new SQL statement is beginning to ** be parsed. Check to see if the schema for the database needs |
︙ | ︙ | |||
1461 1462 1463 1464 1465 1466 1467 | /* Check to see if we need to create an sqlite_sequence table for ** keeping track of autoincrement keys. */ if( p->autoInc ){ Db *pDb = &db->aDb[p->iDb]; if( pDb->pSeqTab==0 ){ sqlite3NestedParse(pParse, | | | < < < < < | 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 | /* Check to see if we need to create an sqlite_sequence table for ** keeping track of autoincrement keys. */ if( p->autoInc ){ Db *pDb = &db->aDb[p->iDb]; if( pDb->pSeqTab==0 ){ sqlite3NestedParse(pParse, "CREATE TABLE %Q.sqlite_sequence(name,seq)", pDb->zName ); } } #endif /* Reparse everything to update our internal data structures */ sqlite3VdbeOp3(v, OP_ParseSchema, p->iDb, 0, |
︙ | ︙ | |||
1807 1808 1809 1810 1811 1812 1813 | goto exit_drop_table; } if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){ goto exit_drop_table; } } #endif | | < | 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 | goto exit_drop_table; } if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){ goto exit_drop_table; } } #endif if( pTab->readOnly || pTab==db->aDb[iDb].pSeqTab ){ sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName); goto exit_drop_table; } if( isView && pTab->pSelect==0 ){ sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName); goto exit_drop_table; } if( !isView && pTab->pSelect ){ |
︙ | ︙ | |||
3002 3003 3004 3005 3006 3007 3008 | zWhere = sqlite3MPrintf("tbl_name=%Q", zName); sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, zWhere, P3_DYNAMIC); } sqliteFree(zName); } #endif | < | 2996 2997 2998 2999 3000 3001 3002 | zWhere = sqlite3MPrintf("tbl_name=%Q", zName); sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, zWhere, P3_DYNAMIC); } sqliteFree(zName); } #endif |
Changes to src/insert.c.
︙ | ︙ | |||
8 9 10 11 12 13 14 | ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** This file contains C code routines that are called by the parser ** to handle INSERT statements in SQLite. ** | | | 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** This file contains C code routines that are called by the parser ** to handle INSERT statements in SQLite. ** ** $Id: insert.c,v 1.126 2004/11/13 03:48:07 drh Exp $ */ #include "sqliteInt.h" /* ** Set P3 of the most recently inserted opcode to a column affinity ** string for index pIdx. A column affinity string has one character ** for each column in the table, according to the affinity of the column: |
︙ | ︙ | |||
194 195 196 197 198 199 200 201 202 203 204 205 206 207 | #ifndef SQLITE_OMIT_TRIGGER int isView; /* True if attempting to insert into a view */ int row_triggers_exist = 0; /* True if there are FOR EACH ROW triggers */ int before_triggers; /* True if there are BEFORE triggers */ int after_triggers; /* True if there are AFTER triggers */ #endif if( pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup; db = pParse->db; /* Locate the table into which we will be inserting new information. */ assert( pTabList->nSrc==1 ); | > > > > | 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | #ifndef SQLITE_OMIT_TRIGGER int isView; /* True if attempting to insert into a view */ int row_triggers_exist = 0; /* True if there are FOR EACH ROW triggers */ int before_triggers; /* True if there are BEFORE triggers */ int after_triggers; /* True if there are AFTER triggers */ #endif #ifndef SQLITE_OMIT_AUTOINCREMENT int counterRowid; /* Memory cell holding rowid of autoinc counter */ #endif if( pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup; db = pParse->db; /* Locate the table into which we will be inserting new information. */ assert( pTabList->nSrc==1 ); |
︙ | ︙ | |||
271 272 273 274 275 276 277 | /* if there are row triggers, allocate a temp table for new.* references. */ if( row_triggers_exist ){ newIdx = pParse->nTab++; } #ifndef SQLITE_OMIT_AUTOINCREMENT /* If this is an AUTOINCREMENT table, look up the sequence number in the | | | > > > | | | > > > | | > | > > > > > > | 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | /* if there are row triggers, allocate a temp table for new.* references. */ if( row_triggers_exist ){ newIdx = pParse->nTab++; } #ifndef SQLITE_OMIT_AUTOINCREMENT /* If this is an AUTOINCREMENT table, look up the sequence number in the ** sqlite_sequence table and store it in memory cell counterMem. Also ** remember the rowid of the sqlite_sequence table entry in memory cell ** counterRowid. */ if( pTab->autoInc ){ int iCur = pParse->nTab; int base = sqlite3VdbeCurrentAddr(v); counterRowid = pParse->nMem++; counterMem = pParse->nMem++; sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0); sqlite3VdbeAddOp(v, OP_OpenRead, iCur, pDb->pSeqTab->tnum); sqlite3VdbeAddOp(v, OP_SetNumColumns, iCur, 2); sqlite3VdbeAddOp(v, OP_Rewind, iCur, base+13); sqlite3VdbeAddOp(v, OP_Column, iCur, 0); sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0); sqlite3VdbeAddOp(v, OP_Ne, 28417, base+12); sqlite3VdbeAddOp(v, OP_Recno, iCur, 0); sqlite3VdbeAddOp(v, OP_MemStore, counterRowid, 1); sqlite3VdbeAddOp(v, OP_Column, iCur, 1); sqlite3VdbeAddOp(v, OP_MemStore, counterMem, 1); sqlite3VdbeAddOp(v, OP_Goto, 0, base+13); sqlite3VdbeAddOp(v, OP_Next, iCur, base+4); sqlite3VdbeAddOp(v, OP_Close, iCur, 0); } #endif /* SQLITE_OMIT_AUTOINCREMENT */ /* Figure out how many columns of data are supplied. If the data ** is coming from a SELECT statement, then this step also generates ** all the code to implement the SELECT statement and invoke a subroutine ** to process each row of the result. (Template 2.) If the SELECT |
︙ | ︙ | |||
562 563 564 565 566 567 568 | ** to generate a unique primary key value. */ sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); sqlite3VdbeAddOp(v, OP_Pop, 1, 0); sqlite3VdbeAddOp(v, OP_NewRecno, base, counterMem); sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); }else{ | | | 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 | ** to generate a unique primary key value. */ sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); sqlite3VdbeAddOp(v, OP_Pop, 1, 0); sqlite3VdbeAddOp(v, OP_NewRecno, base, counterMem); sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); }else{ sqlite3VdbeAddOp(v, OP_NewRecno, base, counterMem); } #ifndef SQLITE_OMIT_AUTOINCREMENT if( pTab->autoInc ){ sqlite3VdbeAddOp(v, OP_MemMax, counterMem, 0); } #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
︙ | ︙ | |||
653 654 655 656 657 658 659 | sqlite3VdbeAddOp(v, OP_Close, base, 0); for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ sqlite3VdbeAddOp(v, OP_Close, idx+base, 0); } } #ifndef SQLITE_OMIT_AUTOINCREMENT | | > > | | > > > > > > > > | | > > | 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 | sqlite3VdbeAddOp(v, OP_Close, base, 0); for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ sqlite3VdbeAddOp(v, OP_Close, idx+base, 0); } } #ifndef SQLITE_OMIT_AUTOINCREMENT /* Update the sqlite_sequence table by storing the content of the ** counter value in memory counterMem back into the sqlite_sequence ** table. */ if( pTab->autoInc ){ int iCur = pParse->nTab; int base = sqlite3VdbeCurrentAddr(v); sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0); sqlite3VdbeAddOp(v, OP_OpenWrite, iCur, pDb->pSeqTab->tnum); sqlite3VdbeAddOp(v, OP_SetNumColumns, iCur, 2); sqlite3VdbeAddOp(v, OP_MemLoad, counterRowid, 0); sqlite3VdbeAddOp(v, OP_NotNull, -1, base+7); sqlite3VdbeAddOp(v, OP_Pop, 1, 0); sqlite3VdbeAddOp(v, OP_NewRecno, iCur, 0); sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0); sqlite3VdbeAddOp(v, OP_MemLoad, counterMem, 0); sqlite3VdbeAddOp(v, OP_MakeRecord, 2, 0); sqlite3VdbeAddOp(v, OP_PutIntKey, iCur, 0); sqlite3VdbeAddOp(v, OP_Close, iCur, 0); } #endif /* ** Return the number of rows inserted. If this routine is ** generating code because of a call to sqlite3NestedParse(), do not ** invoke the callback function. |
︙ | ︙ |
Changes to src/parse.y.
︙ | ︙ | |||
10 11 12 13 14 15 16 | ** ************************************************************************* ** This file contains SQLite's grammar for SQL. Process this file ** using the lemon parser generator to generate C code that runs ** the parser. Lemon will also generate a header file containing ** numeric codes for all of the tokens. ** | | | 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | ** ************************************************************************* ** This file contains SQLite's grammar for SQL. Process this file ** using the lemon parser generator to generate C code that runs ** the parser. Lemon will also generate a header file containing ** numeric codes for all of the tokens. ** ** @(#) $Id: parse.y,v 1.155 2004/11/13 03:48:07 drh Exp $ */ %token_prefix TK_ %token_type {Token} %default_type {Token} %extra_argument {Parse *pParse} %syntax_error { if( pParse->zErrMsg==0 ){ |
︙ | ︙ | |||
264 265 266 267 268 269 270 | // conslist_opt ::= . conslist_opt ::= COMMA conslist. conslist ::= conslist COMMA tcons. conslist ::= conslist tcons. conslist ::= tcons. tcons ::= CONSTRAINT nm. | | | 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | // conslist_opt ::= . conslist_opt ::= COMMA conslist. conslist ::= conslist COMMA tcons. conslist ::= conslist tcons. conslist ::= tcons. tcons ::= CONSTRAINT nm. tcons ::= PRIMARY KEY LP idxlist(X) autoinc(I) RP onconf(R). {sqlite3AddPrimaryKey(pParse,X,R,I);} tcons ::= UNIQUE LP idxlist(X) RP onconf(R). {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0);} tcons ::= CHECK expr onconf. tcons ::= FOREIGN KEY LP idxlist(FA) RP REFERENCES nm(T) idxlist_opt(TA) refargs(R) defer_subclause_opt(D). { sqlite3CreateForeignKey(pParse, FA, &T, TA, R); |
︙ | ︙ | |||
316 317 318 319 320 321 322 | //////////////////////// The SELECT statement ///////////////////////////////// // cmd ::= select(X). { sqlite3Select(pParse, X, SRT_Callback, 0, 0, 0, 0, 0); sqlite3SelectDelete(X); } | < < < < < < < < < < < < < < < | 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | //////////////////////// The SELECT statement ///////////////////////////////// // cmd ::= select(X). { sqlite3Select(pParse, X, SRT_Callback, 0, 0, 0, 0, 0); sqlite3SelectDelete(X); } %type select {Select*} %destructor select {sqlite3SelectDelete($$);} %type oneselect {Select*} %destructor oneselect {sqlite3SelectDelete($$);} select(A) ::= oneselect(X). {A = X;} %ifndef SQLITE_OMIT_COMPOUND_SELECT |
︙ | ︙ |
Changes to src/test1.c.
︙ | ︙ | |||
9 10 11 12 13 14 15 | ** May you share freely, never taking more than you give. ** ************************************************************************* ** Code for testing the printf() interface to SQLite. This code ** is not included in the SQLite library. It is used for automated ** testing of the SQLite library. ** | | | 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | ** May you share freely, never taking more than you give. ** ************************************************************************* ** Code for testing the printf() interface to SQLite. This code ** is not included in the SQLite library. It is used for automated ** testing of the SQLite library. ** ** $Id: test1.c,v 1.111 2004/11/13 03:48:07 drh Exp $ */ #include "sqliteInt.h" #include "tcl.h" #include "os.h" #include <stdlib.h> #include <string.h> |
︙ | ︙ | |||
2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 | Tcl_SetVar2(interp, "sqlite_options", "reindex", "1", TCL_GLOBAL_ONLY); #endif #ifdef SQLITE_OMIT_ALTERTABLE Tcl_SetVar2(interp, "sqlite_options", "altertable", "0", TCL_GLOBAL_ONLY); #else Tcl_SetVar2(interp, "sqlite_options", "altertable", "1", TCL_GLOBAL_ONLY); #endif #ifdef SQLITE_OMIT_AUTOVACUUM Tcl_SetVar2(interp, "sqlite_options", "autovacuum", "0", TCL_GLOBAL_ONLY); #else Tcl_SetVar2(interp, "sqlite_options", "autovacuum", "1", TCL_GLOBAL_ONLY); | > > > > > > > | < | 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 | Tcl_SetVar2(interp, "sqlite_options", "reindex", "1", TCL_GLOBAL_ONLY); #endif #ifdef SQLITE_OMIT_ALTERTABLE Tcl_SetVar2(interp, "sqlite_options", "altertable", "0", TCL_GLOBAL_ONLY); #else Tcl_SetVar2(interp, "sqlite_options", "altertable", "1", TCL_GLOBAL_ONLY); #endif #ifdef SQLITE_OMIT_AUTOINCREMENT Tcl_SetVar2(interp, "sqlite_options", "autoinc", "0", TCL_GLOBAL_ONLY); #else Tcl_SetVar2(interp, "sqlite_options", "autoinc", "1", TCL_GLOBAL_ONLY); #endif #ifdef SQLITE_OMIT_AUTOVACUUM Tcl_SetVar2(interp, "sqlite_options", "autovacuum", "0", TCL_GLOBAL_ONLY); #else Tcl_SetVar2(interp, "sqlite_options", "autovacuum", "1", TCL_GLOBAL_ONLY); #endif /* SQLITE_OMIT_AUTOVACUUM */ #if !defined(SQLITE_DEFAULT_AUTOVACCUM) || SQLITE_DEFAULT_AUTOVACUUM==0 Tcl_SetVar2(interp,"sqlite_options","default_autovacuum","0",TCL_GLOBAL_ONLY); #else Tcl_SetVar2(interp,"sqlite_options","default_autovacuum","1",TCL_GLOBAL_ONLY); #endif } /* ** Register commands with the TCL interpreter. */ int Sqlitetest1_Init(Tcl_Interp *interp){ extern int sqlite3_search_count; |
︙ | ︙ |
Changes to src/vdbe.c.
︙ | ︙ | |||
39 40 41 42 43 44 45 | ** ** Various scripts scan this source file in order to generate HTML ** documentation, headers files, or other derived files. The formatting ** of the code in this file is, therefore, important. See other comments ** in this file for details. If in doubt, do not deviate from existing ** commenting and indentation practices when changing or adding code. ** | | | 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | ** ** Various scripts scan this source file in order to generate HTML ** documentation, headers files, or other derived files. The formatting ** of the code in this file is, therefore, important. See other comments ** in this file for details. If in doubt, do not deviate from existing ** commenting and indentation practices when changing or adding code. ** ** $Id: vdbe.c,v 1.427 2004/11/13 03:48:07 drh Exp $ */ #include "sqliteInt.h" #include "os.h" #include <ctype.h> #include "vdbeInt.h" /* |
︙ | ︙ | |||
2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 | } #ifndef SQLITE_OMIT_AUTOINCREMENT if( pOp->p2 ){ Mem *pMem; assert( pOp->p2>0 && pOp->p2<p->nMem ); /* P2 is a valid memory cell */ pMem = &p->aMem[pOp->p2]; assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P2) holds an integer */ if( pMem->i==0x7fffffffffffffff || pC->useRandomRowid ){ rc = SQLITE_FULL; goto abort_due_to_error; } if( v<pMem->i+1 ){ v = pMem->i + 1; | > | 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 | } #ifndef SQLITE_OMIT_AUTOINCREMENT if( pOp->p2 ){ Mem *pMem; assert( pOp->p2>0 && pOp->p2<p->nMem ); /* P2 is a valid memory cell */ pMem = &p->aMem[pOp->p2]; Integerify(pMem); assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P2) holds an integer */ if( pMem->i==0x7fffffffffffffff || pC->useRandomRowid ){ rc = SQLITE_FULL; goto abort_due_to_error; } if( v<pMem->i+1 ){ v = pMem->i + 1; |
︙ | ︙ | |||
4122 4123 4124 4125 4126 4127 4128 | */ case OP_MemMax: { int i = pOp->p1; Mem *pMem; assert( pTos>=p->aStack ); assert( i>=0 && i<p->nMem ); pMem = &p->aMem[i]; | | | 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 | */ case OP_MemMax: { int i = pOp->p1; Mem *pMem; assert( pTos>=p->aStack ); assert( i>=0 && i<p->nMem ); pMem = &p->aMem[i]; Integerify(pMem); Integerify(pTos); if( pMem->i<pTos->i){ pMem->i = pTos->i; } break; } #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
︙ | ︙ |
Added test/autoinc.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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 | # 2004 November 12 # # 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. The # focus of this script is testing the AUTOINCREMENT features. # # $Id: autoinc.test,v 1.1 2004/11/13 03:48:07 drh Exp $ # set testdir [file dirname $argv0] source $testdir/tester.tcl # If the library is not compiled with autoincrement support then # skip all tests in this file. # ifcapable {!autoinc} { finish_test return } # The database is initially empty. # do_test autoinc-1.1 { execsql { SELECT name FROM sqlite_master WHERE type='table'; } } {} # Add a table with the AUTOINCREMENT feature. Verify that the # SQLITE_SEQUENCE table gets created. # do_test autoinc-1.2 { execsql { CREATE TABLE t1(x INTEGER PRIMARY KEY AUTOINCREMENT, y); SELECT name FROM sqlite_master WHERE type='table'; } } {t1 sqlite_sequence} # The SQLITE_SEQUENCE table is initially empty # do_test autoinc-1.3 { execsql { SELECT * FROM sqlite_sequence; } } {} # Close and reopen the database. Verify that everything is still there. # do_test autoinc-1.4 { db close sqlite3 db test.db execsql { SELECT * FROM sqlite_sequence; } } {} # We are not allowed to drop the sqlite_sequence table. # do_test autoinc-1.5 { catchsql {DROP TABLE sqlite_sequence} } {1 {table sqlite_sequence may not be dropped}} do_test autoinc-1.6 { execsql {SELECT name FROM sqlite_master WHERE type='table'} } {t1 sqlite_sequence} # Insert an entries into the t1 table and make sure the largest key # is always recorded in the sqlite_sequence table. # do_test autoinc-2.1 { execsql { SELECT * FROM sqlite_sequence } } {} do_test autoinc-2.2 { execsql { INSERT INTO t1 VALUES(12,34); SELECT * FROM sqlite_sequence; } } {t1 12} do_test autoinc-2.3 { execsql { INSERT INTO t1 VALUES(1,23); SELECT * FROM sqlite_sequence; } } {t1 12} do_test autoinc-2.4 { execsql { INSERT INTO t1 VALUES(123,456); SELECT * FROM sqlite_sequence; } } {t1 123} do_test autoinc-2.5 { execsql { INSERT INTO t1 VALUES(NULL,567); SELECT * FROM sqlite_sequence; } } {t1 124} do_test autoinc-2.6 { execsql { DELETE FROM t1 WHERE y=567; SELECT * FROM sqlite_sequence; } } {t1 124} do_test autoinc-2.7 { execsql { INSERT INTO t1 VALUES(NULL,567); SELECT * FROM sqlite_sequence; } } {t1 125} do_test autoinc-2.8 { execsql { DELETE FROM t1; SELECT * FROM sqlite_sequence; } } {t1 125} do_test autoinc-2.9 { execsql { INSERT INTO t1 VALUES(12,34); SELECT * FROM sqlite_sequence; } } {t1 125} do_test autoinc-2.10 { execsql { INSERT INTO t1 VALUES(125,456); SELECT * FROM sqlite_sequence; } } {t1 125} do_test autoinc-2.11 { execsql { INSERT INTO t1 VALUES(-1234567,-1); SELECT * FROM sqlite_sequence; } } {t1 125} do_test autoinc-2.12 { execsql { INSERT INTO t1 VALUES(234,5678); SELECT * FROM sqlite_sequence; } } {t1 234} do_test autoinc-2.13 { execsql { DELETE FROM t1; INSERT INTO t1 VALUES(NULL,1); SELECT * FROM sqlite_sequence; } } {t1 235} do_test autoinc-2.14 { execsql { SELECT * FROM t1; } } {235 1} # Manually change the autoincrement values in sqlite_sequence. # do_test autoinc-2.20 { execsql { UPDATE sqlite_sequence SET seq=1234 WHERE name='t1'; INSERT INTO t1 VALUES(NULL,2); SELECT * FROM t1; } } {235 1 1235 2} do_test autoinc-2.21 { execsql { SELECT * FROM sqlite_sequence; } } {t1 1235} do_test autoinc-2.22 { execsql { UPDATE sqlite_sequence SET seq=NULL WHERE name='t1'; INSERT INTO t1 VALUES(NULL,3); SELECT * FROM t1; } } {235 1 1235 2 1236 3} do_test autoinc-2.23 { execsql { SELECT * FROM sqlite_sequence; } } {t1 1236} do_test autoinc-2.24 { execsql { UPDATE sqlite_sequence SET seq='a-string' WHERE name='t1'; INSERT INTO t1 VALUES(NULL,4); SELECT * FROM t1; } } {235 1 1235 2 1236 3 1237 4} do_test autoinc-2.25 { execsql { SELECT * FROM sqlite_sequence; } } {t1 1237} do_test autoinc-2.26 { execsql { DELETE FROM sqlite_sequence WHERE name='t1'; INSERT INTO t1 VALUES(NULL,5); SELECT * FROM t1; } } {235 1 1235 2 1236 3 1237 4 1238 5} do_test autoinc-2.27 { execsql { SELECT * FROM sqlite_sequence; } } {t1 1238} do_test autoinc-2.28 { execsql { UPDATE sqlite_sequence SET seq='12345678901234567890' WHERE name='t1'; INSERT INTO t1 VALUES(NULL,6); SELECT * FROM t1; } } {235 1 1235 2 1236 3 1237 4 1238 5 1239 6} do_test autoinc-2.29 { execsql { SELECT * FROM sqlite_sequence; } } {t1 1239} # Test multi-row inserts # do_test autoinc-2.50 { execsql { DELETE FROM t1 WHERE y>=3; INSERT INTO t1 SELECT NULL, y+2 FROM t1; SELECT * FROM t1; } } {235 1 1235 2 1240 3 1241 4} do_test autoinc-2.51 { execsql { SELECT * FROM sqlite_sequence } } {t1 1241} do_test autoinc-2.52 { execsql { CREATE TEMP TABLE t2 AS SELECT y FROM t1; INSERT INTO t1 SELECT NULL, y+4 FROM t2; SELECT * FROM t1; } } {235 1 1235 2 1240 3 1241 4 1242 5 1243 6 1244 7 1245 8} do_test autoinc-2.53 { execsql { SELECT * FROM sqlite_sequence } } {t1 1245} do_test autoinc-2.54 { execsql { DELETE FROM t1; INSERT INTO t1 SELECT NULL, y FROM t2; SELECT * FROM t1; } } {1246 1 1247 2 1248 3 1249 4} do_test autoinc-2.55 { execsql { SELECT * FROM sqlite_sequence } } {t1 1249} # Create multiple AUTOINCREMENT tables. Make sure all sequences are # tracked separately and do not interfere with one another. # do_test autoinc-2.70 { execsql { DROP TABLE t2; CREATE TABLE t2(d, e INTEGER PRIMARY KEY AUTOINCREMENT, f); INSERT INTO t2(d) VALUES(1); SELECT * FROM sqlite_sequence; } } {t1 1249 t2 1} do_test autoinc-2.71 { execsql { INSERT INTO t2(d) VALUES(2); SELECT * FROM sqlite_sequence; } } {t1 1249 t2 2} do_test autoinc-2.72 { execsql { INSERT INTO t1(x) VALUES(10000); SELECT * FROM sqlite_sequence; } } {t1 10000 t2 2} do_test autoinc-2.73 { execsql { CREATE TABLE t3(g INTEGER PRIMARY KEY AUTOINCREMENT, h); INSERT INTO t3(h) VALUES(1); SELECT * FROM sqlite_sequence; } } {t1 10000 t2 2 t3 1} do_test autoinc-2.74 { execsql { INSERT INTO t2(d,e) VALUES(3,100); SELECT * FROM sqlite_sequence; } } {t1 10000 t2 100 t3 1} # When a table with an AUTOINCREMENT is deleted, the corresponding entry # in the SQLITE_SEQUENCE table should also be deleted. But the SQLITE_SEQUENCE # table itself should remain behind. # do_test autoinc-3.1 { execsql {SELECT name FROM sqlite_sequence} } {t1 t2 t3} do_test autoinc-3.2 { execsql { DROP TABLE t1; SELECT name FROM sqlite_sequence; } } {t2 t3} do_test autoinc-3.3 { execsql { DROP TABLE t3; SELECT name FROM sqlite_sequence; } } {t2} do_test autoinc-3.4 { execsql { DROP TABLE t2; SELECT name FROM sqlite_sequence; } } {} # AUTOINCREMENT on TEMP tables. # do_test autoinc-4.1 { execsql { SELECT 1, name FROM sqlite_master WHERE type='table' UNION ALL SELECT 2, name FROM sqlite_temp_master WHERE type='table' } } {1 sqlite_sequence} do_test autoinc-4.2 { execsql { CREATE TABLE t1(x INTEGER PRIMARY KEY AUTOINCREMENT, y); CREATE TEMP TABLE t3(a INTEGER PRIMARY KEY AUTOINCREMENT, b); SELECT 1, name FROM sqlite_master WHERE type='table' UNION ALL SELECT 2, name FROM sqlite_temp_master WHERE type='table' } } {1 sqlite_sequence 1 t1 2 t3 2 sqlite_sequence} do_test autoinc-4.3 { execsql { SELECT 1, * FROM main.sqlite_sequence UNION ALL SELECT 2, * FROM temp.sqlite_sequence } } {} do_test autoinc-4.4 { execsql { INSERT INTO t1 VALUES(10,1); INSERT INTO t3 VALUES(20,2); INSERT INTO t1 VALUES(NULL,3); INSERT INTO t3 VALUES(NULL,4); SELECT * FROM t1 UNION ALL SELECT * FROM t3; } } {10 1 11 3 20 2 21 4} do_test autoinc-4.5 { execsql { SELECT 1, * FROM main.sqlite_sequence UNION ALL SELECT 2, * FROM temp.sqlite_sequence } } {1 t1 11 2 t3 21} do_test autoinc-4.6 { execsql { INSERT INTO t1 SELECT * FROM t3; SELECT 1, * FROM main.sqlite_sequence UNION ALL SELECT 2, * FROM temp.sqlite_sequence } } {1 t1 21 2 t3 21} do_test autoinc-4.7 { execsql { INSERT INTO t3 SELECT x+100, y FROM t1; SELECT 1, * FROM main.sqlite_sequence UNION ALL SELECT 2, * FROM temp.sqlite_sequence } } {1 t1 21 2 t3 121} do_test autoinc-4.8 { execsql { DROP TABLE t3; SELECT 1, * FROM main.sqlite_sequence UNION ALL SELECT 2, * FROM temp.sqlite_sequence } } {1 t1 21} do_test autoinc-4.9 { execsql { CREATE TEMP TABLE t2(p INTEGER PRIMARY KEY AUTOINCREMENT, q); INSERT INTO t2 SELECT * FROM t1; DROP TABLE t1; SELECT 1, * FROM main.sqlite_sequence UNION ALL SELECT 2, * FROM temp.sqlite_sequence } } {2 t2 21} do_test autoinc-4.10 { execsql { DROP TABLE t2; SELECT 1, * FROM main.sqlite_sequence UNION ALL SELECT 2, * FROM temp.sqlite_sequence } } {} # Make sure AUTOINCREMENT works on ATTACH-ed tables. # do_test autoinc-5.1 { file delete -force test2.db file delete -force test2.db-journal sqlite3 db2 test2.db execsql { CREATE TABLE t4(m INTEGER PRIMARY KEY AUTOINCREMENT, n); CREATE TABLE t5(o, p INTEGER PRIMARY KEY AUTOINCREMENT); } db2; execsql { ATTACH 'test2.db' as aux; SELECT 1, * FROM main.sqlite_sequence UNION ALL SELECT 2, * FROM temp.sqlite_sequence UNION ALL SELECT 3, * FROM aux.sqlite_sequence } } {} do_test autoinc-5.2 { execsql { INSERT INTO t4 VALUES(NULL,1); SELECT 1, * FROM main.sqlite_sequence UNION ALL SELECT 2, * FROM temp.sqlite_sequence UNION ALL SELECT 3, * FROM aux.sqlite_sequence } } {3 t4 1} do_test autoinc-5.3 { execsql { INSERT INTO t5 VALUES(100,200); SELECT * FROM sqlite_sequence } db2 } {t4 1 t5 200} do_test autoinc-5.4 { execsql { SELECT 1, * FROM main.sqlite_sequence UNION ALL SELECT 2, * FROM temp.sqlite_sequence UNION ALL SELECT 3, * FROM aux.sqlite_sequence } } {3 t4 1 3 t5 200} # Requirement REQ00310: Make sure an insert fails if the sequence is # already at its maximum value. # do_test autoinc-6.1 { execsql { CREATE TABLE t6(v INTEGER PRIMARY KEY AUTOINCREMENT, w); INSERT INTO t6 VALUES(9223372036854775807,1); SELECT seq FROM main.sqlite_sequence WHERE name='t6'; } } 9223372036854775807 do_test autoinc-6.2 { catchsql { INSERT INTO t6 VALUES(NULL,1); } } {1 {database is full}} # Allow the AUTOINCREMENT keyword inside the parentheses # on a separate PRIMARY KEY designation. # do_test autoinc-7.1 { execsql { CREATE TABLE t7(x INTEGER, y REAL, PRIMARY KEY(x AUTOINCREMENT)); INSERT INTO t7(y) VALUES(123); INSERT INTO t7(y) VALUES(234); DELETE FROM t7; INSERT INTO t7(y) VALUES(345); SELECT * FROM t7; } } {3 345} catch {db2 close} finish_test |