| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2001 September 15 | 2 ** 2001 September 15 |
| 3 ** | 3 ** |
| 4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
| 5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
| 6 ** | 6 ** |
| 7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
| 8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
| 9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
| 10 ** | 10 ** |
| 11 ************************************************************************* | 11 ************************************************************************* |
| 12 ** This file contains C code routines that are called by the SQLite parser | 12 ** This file contains C code routines that are called by the SQLite parser |
| 13 ** when syntax rules are reduced. The routines in this file handle the | 13 ** when syntax rules are reduced. The routines in this file handle the |
| 14 ** following kinds of SQL syntax: | 14 ** following kinds of SQL syntax: |
| 15 ** | 15 ** |
| 16 ** CREATE TABLE | 16 ** CREATE TABLE |
| 17 ** DROP TABLE | 17 ** DROP TABLE |
| 18 ** CREATE INDEX | 18 ** CREATE INDEX |
| 19 ** DROP INDEX | 19 ** DROP INDEX |
| 20 ** creating ID lists | 20 ** creating ID lists |
| 21 ** BEGIN TRANSACTION | 21 ** BEGIN TRANSACTION |
| 22 ** COMMIT | 22 ** COMMIT |
| 23 ** ROLLBACK | 23 ** ROLLBACK |
| 24 ** | |
| 25 ** $Id: build.c,v 1.557 2009/07/24 17:58:53 danielk1977 Exp $ | |
| 26 */ | 24 */ |
| 27 #include "sqliteInt.h" | 25 #include "sqliteInt.h" |
| 28 | 26 |
| 29 #include "pager.h" | 27 #include "pager.h" |
| 30 #include "btree.h" | 28 #include "btree.h" |
| 31 | 29 |
| 32 /* | 30 /* |
| 33 ** This routine is called when a new SQL statement is beginning to | 31 ** This routine is called when a new SQL statement is beginning to |
| 34 ** be parsed. Initialize the pParse structure as needed. | 32 ** be parsed. Initialize the pParse structure as needed. |
| 35 */ | 33 */ |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 | 190 |
| 193 | 191 |
| 194 /* Get the VDBE program ready for execution | 192 /* Get the VDBE program ready for execution |
| 195 */ | 193 */ |
| 196 if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){ | 194 if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){ |
| 197 #ifdef SQLITE_DEBUG | 195 #ifdef SQLITE_DEBUG |
| 198 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0; | 196 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0; |
| 199 sqlite3VdbeTrace(v, trace); | 197 sqlite3VdbeTrace(v, trace); |
| 200 #endif | 198 #endif |
| 201 assert( pParse->iCacheLevel==0 ); /* Disables and re-enables match */ | 199 assert( pParse->iCacheLevel==0 ); /* Disables and re-enables match */ |
| 200 /* A minimum of one cursor is required if autoincrement is used |
| 201 * See ticket [a696379c1f08866] */ |
| 202 if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1; |
| 202 sqlite3VdbeMakeReady(v, pParse->nVar, pParse->nMem, | 203 sqlite3VdbeMakeReady(v, pParse->nVar, pParse->nMem, |
| 203 pParse->nTab, pParse->nMaxArg, pParse->explain, | 204 pParse->nTab, pParse->nMaxArg, pParse->explain, |
| 204 pParse->isMultiWrite && pParse->mayAbort); | 205 pParse->isMultiWrite && pParse->mayAbort); |
| 205 pParse->rc = SQLITE_DONE; | 206 pParse->rc = SQLITE_DONE; |
| 206 pParse->colNamesSet = 0; | 207 pParse->colNamesSet = 0; |
| 207 }else if( pParse->rc==SQLITE_OK ){ | 208 }else{ |
| 208 pParse->rc = SQLITE_ERROR; | 209 pParse->rc = SQLITE_ERROR; |
| 209 } | 210 } |
| 210 pParse->nTab = 0; | 211 pParse->nTab = 0; |
| 211 pParse->nMem = 0; | 212 pParse->nMem = 0; |
| 212 pParse->nSet = 0; | 213 pParse->nSet = 0; |
| 213 pParse->nVar = 0; | 214 pParse->nVar = 0; |
| 214 pParse->cookieMask = 0; | 215 pParse->cookieMask = 0; |
| 215 pParse->cookieGoto = 0; | 216 pParse->cookieGoto = 0; |
| 216 } | 217 } |
| 217 | 218 |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue; | 341 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue; |
| 341 p = sqlite3HashFind(&pSchema->idxHash, zName, nName); | 342 p = sqlite3HashFind(&pSchema->idxHash, zName, nName); |
| 342 if( p ) break; | 343 if( p ) break; |
| 343 } | 344 } |
| 344 return p; | 345 return p; |
| 345 } | 346 } |
| 346 | 347 |
| 347 /* | 348 /* |
| 348 ** Reclaim the memory used by an index | 349 ** Reclaim the memory used by an index |
| 349 */ | 350 */ |
| 350 static void freeIndex(Index *p){ | 351 static void freeIndex(sqlite3 *db, Index *p){ |
| 351 sqlite3 *db = p->pTable->dbMem; | |
| 352 #ifndef SQLITE_OMIT_ANALYZE | 352 #ifndef SQLITE_OMIT_ANALYZE |
| 353 sqlite3DeleteIndexSamples(p); | 353 sqlite3DeleteIndexSamples(db, p); |
| 354 #endif | 354 #endif |
| 355 sqlite3DbFree(db, p->zColAff); | 355 sqlite3DbFree(db, p->zColAff); |
| 356 sqlite3DbFree(db, p); | 356 sqlite3DbFree(db, p); |
| 357 } | 357 } |
| 358 | 358 |
| 359 /* | 359 /* |
| 360 ** Remove the given index from the index hash table, and free | |
| 361 ** its memory structures. | |
| 362 ** | |
| 363 ** The index is removed from the database hash tables but | |
| 364 ** it is not unlinked from the Table that it indexes. | |
| 365 ** Unlinking from the Table must be done by the calling function. | |
| 366 */ | |
| 367 static void sqlite3DeleteIndex(Index *p){ | |
| 368 Index *pOld; | |
| 369 const char *zName = p->zName; | |
| 370 | |
| 371 pOld = sqlite3HashInsert(&p->pSchema->idxHash, zName, | |
| 372 sqlite3Strlen30(zName), 0); | |
| 373 assert( pOld==0 || pOld==p ); | |
| 374 freeIndex(p); | |
| 375 } | |
| 376 | |
| 377 /* | |
| 378 ** For the index called zIdxName which is found in the database iDb, | 360 ** For the index called zIdxName which is found in the database iDb, |
| 379 ** unlike that index from its Table then remove the index from | 361 ** unlike that index from its Table then remove the index from |
| 380 ** the index hash table and free all memory structures associated | 362 ** the index hash table and free all memory structures associated |
| 381 ** with the index. | 363 ** with the index. |
| 382 */ | 364 */ |
| 383 void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){ | 365 void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){ |
| 384 Index *pIndex; | 366 Index *pIndex; |
| 385 int len; | 367 int len; |
| 386 Hash *pHash = &db->aDb[iDb].pSchema->idxHash; | 368 Hash *pHash = &db->aDb[iDb].pSchema->idxHash; |
| 387 | 369 |
| 388 len = sqlite3Strlen30(zIdxName); | 370 len = sqlite3Strlen30(zIdxName); |
| 389 pIndex = sqlite3HashInsert(pHash, zIdxName, len, 0); | 371 pIndex = sqlite3HashInsert(pHash, zIdxName, len, 0); |
| 390 if( pIndex ){ | 372 if( pIndex ){ |
| 391 if( pIndex->pTable->pIndex==pIndex ){ | 373 if( pIndex->pTable->pIndex==pIndex ){ |
| 392 pIndex->pTable->pIndex = pIndex->pNext; | 374 pIndex->pTable->pIndex = pIndex->pNext; |
| 393 }else{ | 375 }else{ |
| 394 Index *p; | 376 Index *p; |
| 395 /* Justification of ALWAYS(); The index must be on the list of | 377 /* Justification of ALWAYS(); The index must be on the list of |
| 396 ** indices. */ | 378 ** indices. */ |
| 397 p = pIndex->pTable->pIndex; | 379 p = pIndex->pTable->pIndex; |
| 398 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; } | 380 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; } |
| 399 if( ALWAYS(p && p->pNext==pIndex) ){ | 381 if( ALWAYS(p && p->pNext==pIndex) ){ |
| 400 p->pNext = pIndex->pNext; | 382 p->pNext = pIndex->pNext; |
| 401 } | 383 } |
| 402 } | 384 } |
| 403 freeIndex(pIndex); | 385 freeIndex(db, pIndex); |
| 404 } | 386 } |
| 405 db->flags |= SQLITE_InternChanges; | 387 db->flags |= SQLITE_InternChanges; |
| 406 } | 388 } |
| 407 | 389 |
| 408 /* | 390 /* |
| 409 ** Erase all schema information from the in-memory hash tables of | 391 ** Erase all schema information from the in-memory hash tables of |
| 410 ** a single database. This routine is called to reclaim memory | 392 ** a single database. This routine is called to reclaim memory |
| 411 ** before the database closes. It is also called during a rollback | 393 ** before the database closes. It is also called during a rollback |
| 412 ** if there were schema changes during the transaction or if a | 394 ** if there were schema changes during the transaction or if a |
| 413 ** schema-cookie mismatch occurs. | 395 ** schema-cookie mismatch occurs. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 } | 446 } |
| 465 | 447 |
| 466 /* | 448 /* |
| 467 ** This routine is called when a commit occurs. | 449 ** This routine is called when a commit occurs. |
| 468 */ | 450 */ |
| 469 void sqlite3CommitInternalChanges(sqlite3 *db){ | 451 void sqlite3CommitInternalChanges(sqlite3 *db){ |
| 470 db->flags &= ~SQLITE_InternChanges; | 452 db->flags &= ~SQLITE_InternChanges; |
| 471 } | 453 } |
| 472 | 454 |
| 473 /* | 455 /* |
| 474 ** Clear the column names from a table or view. | 456 ** Delete memory allocated for the column names of a table or view (the |
| 457 ** Table.aCol[] array). |
| 475 */ | 458 */ |
| 476 static void sqliteResetColumnNames(Table *pTable){ | 459 static void sqliteDeleteColumnNames(sqlite3 *db, Table *pTable){ |
| 477 int i; | 460 int i; |
| 478 Column *pCol; | 461 Column *pCol; |
| 479 sqlite3 *db = pTable->dbMem; | |
| 480 testcase( db==0 ); | |
| 481 assert( pTable!=0 ); | 462 assert( pTable!=0 ); |
| 482 if( (pCol = pTable->aCol)!=0 ){ | 463 if( (pCol = pTable->aCol)!=0 ){ |
| 483 for(i=0; i<pTable->nCol; i++, pCol++){ | 464 for(i=0; i<pTable->nCol; i++, pCol++){ |
| 484 sqlite3DbFree(db, pCol->zName); | 465 sqlite3DbFree(db, pCol->zName); |
| 485 sqlite3ExprDelete(db, pCol->pDflt); | 466 sqlite3ExprDelete(db, pCol->pDflt); |
| 486 sqlite3DbFree(db, pCol->zDflt); | 467 sqlite3DbFree(db, pCol->zDflt); |
| 487 sqlite3DbFree(db, pCol->zType); | 468 sqlite3DbFree(db, pCol->zType); |
| 488 sqlite3DbFree(db, pCol->zColl); | 469 sqlite3DbFree(db, pCol->zColl); |
| 489 } | 470 } |
| 490 sqlite3DbFree(db, pTable->aCol); | 471 sqlite3DbFree(db, pTable->aCol); |
| 491 } | 472 } |
| 492 pTable->aCol = 0; | |
| 493 pTable->nCol = 0; | |
| 494 } | 473 } |
| 495 | 474 |
| 496 /* | 475 /* |
| 497 ** Remove the memory data structures associated with the given | 476 ** Remove the memory data structures associated with the given |
| 498 ** Table. No changes are made to disk by this routine. | 477 ** Table. No changes are made to disk by this routine. |
| 499 ** | 478 ** |
| 500 ** This routine just deletes the data structure. It does not unlink | 479 ** This routine just deletes the data structure. It does not unlink |
| 501 ** the table data structure from the hash table. But it does destroy | 480 ** the table data structure from the hash table. But it does destroy |
| 502 ** memory structures of the indices and foreign keys associated with | 481 ** memory structures of the indices and foreign keys associated with |
| 503 ** the table. | 482 ** the table. |
| 504 */ | 483 */ |
| 505 void sqlite3DeleteTable(Table *pTable){ | 484 void sqlite3DeleteTable(sqlite3 *db, Table *pTable){ |
| 506 Index *pIndex, *pNext; | 485 Index *pIndex, *pNext; |
| 507 FKey *pFKey, *pNextFKey; | |
| 508 sqlite3 *db; | |
| 509 | 486 |
| 510 if( pTable==0 ) return; | 487 assert( !pTable || pTable->nRef>0 ); |
| 511 db = pTable->dbMem; | |
| 512 testcase( db==0 ); | |
| 513 | 488 |
| 514 /* Do not delete the table until the reference count reaches zero. */ | 489 /* Do not delete the table until the reference count reaches zero. */ |
| 515 pTable->nRef--; | 490 if( !pTable ) return; |
| 516 if( pTable->nRef>0 ){ | 491 if( ((!db || db->pnBytesFreed==0) && (--pTable->nRef)>0) ) return; |
| 517 return; | |
| 518 } | |
| 519 assert( pTable->nRef==0 ); | |
| 520 | 492 |
| 521 /* Delete all indices associated with this table | 493 /* Delete all indices associated with this table. */ |
| 522 */ | |
| 523 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ | 494 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ |
| 524 pNext = pIndex->pNext; | 495 pNext = pIndex->pNext; |
| 525 assert( pIndex->pSchema==pTable->pSchema ); | 496 assert( pIndex->pSchema==pTable->pSchema ); |
| 526 sqlite3DeleteIndex(pIndex); | 497 if( !db || db->pnBytesFreed==0 ){ |
| 498 char *zName = pIndex->zName; |
| 499 TESTONLY ( Index *pOld = ) sqlite3HashInsert( |
| 500 » &pIndex->pSchema->idxHash, zName, sqlite3Strlen30(zName), 0 |
| 501 ); |
| 502 assert( pOld==pIndex || pOld==0 ); |
| 503 } |
| 504 freeIndex(db, pIndex); |
| 527 } | 505 } |
| 528 | 506 |
| 529 #ifndef SQLITE_OMIT_FOREIGN_KEY | 507 /* Delete any foreign keys attached to this table. */ |
| 530 /* Delete all foreign keys associated with this table. */ | 508 sqlite3FkDelete(db, pTable); |
| 531 for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){ | |
| 532 pNextFKey = pFKey->pNextFrom; | |
| 533 sqlite3DbFree(db, pFKey); | |
| 534 } | |
| 535 #endif | |
| 536 | 509 |
| 537 /* Delete the Table structure itself. | 510 /* Delete the Table structure itself. |
| 538 */ | 511 */ |
| 539 sqliteResetColumnNames(pTable); | 512 sqliteDeleteColumnNames(db, pTable); |
| 540 sqlite3DbFree(db, pTable->zName); | 513 sqlite3DbFree(db, pTable->zName); |
| 541 sqlite3DbFree(db, pTable->zColAff); | 514 sqlite3DbFree(db, pTable->zColAff); |
| 542 sqlite3SelectDelete(db, pTable->pSelect); | 515 sqlite3SelectDelete(db, pTable->pSelect); |
| 543 #ifndef SQLITE_OMIT_CHECK | 516 #ifndef SQLITE_OMIT_CHECK |
| 544 sqlite3ExprDelete(db, pTable->pCheck); | 517 sqlite3ExprDelete(db, pTable->pCheck); |
| 545 #endif | 518 #endif |
| 546 sqlite3VtabClear(pTable); | 519 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 520 sqlite3VtabClear(db, pTable); |
| 521 #endif |
| 547 sqlite3DbFree(db, pTable); | 522 sqlite3DbFree(db, pTable); |
| 548 } | 523 } |
| 549 | 524 |
| 550 /* | 525 /* |
| 551 ** Unlink the given table from the hash tables and the delete the | 526 ** Unlink the given table from the hash tables and the delete the |
| 552 ** table structure with all its indices and foreign keys. | 527 ** table structure with all its indices and foreign keys. |
| 553 */ | 528 */ |
| 554 void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){ | 529 void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){ |
| 555 Table *p; | 530 Table *p; |
| 556 Db *pDb; | 531 Db *pDb; |
| 557 | 532 |
| 558 assert( db!=0 ); | 533 assert( db!=0 ); |
| 559 assert( iDb>=0 && iDb<db->nDb ); | 534 assert( iDb>=0 && iDb<db->nDb ); |
| 560 assert( zTabName && zTabName[0] ); | 535 assert( zTabName ); |
| 536 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */ |
| 561 pDb = &db->aDb[iDb]; | 537 pDb = &db->aDb[iDb]; |
| 562 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, | 538 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, |
| 563 sqlite3Strlen30(zTabName),0); | 539 sqlite3Strlen30(zTabName),0); |
| 564 sqlite3DeleteTable(p); | 540 sqlite3DeleteTable(db, p); |
| 565 db->flags |= SQLITE_InternChanges; | 541 db->flags |= SQLITE_InternChanges; |
| 566 } | 542 } |
| 567 | 543 |
| 568 /* | 544 /* |
| 569 ** Given a token, return a string that consists of the text of that | 545 ** Given a token, return a string that consists of the text of that |
| 570 ** token. Space to hold the returned string | 546 ** token. Space to hold the returned string |
| 571 ** is obtained from sqliteMalloc() and must be freed by the calling | 547 ** is obtained from sqliteMalloc() and must be freed by the calling |
| 572 ** function. | 548 ** function. |
| 573 ** | 549 ** |
| 574 ** Any quotation marks (ex: "name", 'name', [name], or `name`) that | 550 ** Any quotation marks (ex: "name", 'name', [name], or `name`) that |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 746 ** | 722 ** |
| 747 ** Then pName1 is set to "yyy" and pName2 is "". | 723 ** Then pName1 is set to "yyy" and pName2 is "". |
| 748 ** | 724 ** |
| 749 ** The call below sets the pName pointer to point at the token (pName1 or | 725 ** The call below sets the pName pointer to point at the token (pName1 or |
| 750 ** pName2) that stores the unqualified table name. The variable iDb is | 726 ** pName2) that stores the unqualified table name. The variable iDb is |
| 751 ** set to the index of the database that the table or view is to be | 727 ** set to the index of the database that the table or view is to be |
| 752 ** created in. | 728 ** created in. |
| 753 */ | 729 */ |
| 754 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); | 730 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
| 755 if( iDb<0 ) return; | 731 if( iDb<0 ) return; |
| 756 if( !OMIT_TEMPDB && isTemp && iDb>1 ){ | 732 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){ |
| 757 /* If creating a temp table, the name may not be qualified */ | 733 /* If creating a temp table, the name may not be qualified. Unless |
| 734 ** the database name is "temp" anyway. */ |
| 758 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified"); | 735 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified"); |
| 759 return; | 736 return; |
| 760 } | 737 } |
| 761 if( !OMIT_TEMPDB && isTemp ) iDb = 1; | 738 if( !OMIT_TEMPDB && isTemp ) iDb = 1; |
| 762 | 739 |
| 763 pParse->sNameToken = *pName; | 740 pParse->sNameToken = *pName; |
| 764 zName = sqlite3NameFromToken(db, pName); | 741 zName = sqlite3NameFromToken(db, pName); |
| 765 if( zName==0 ) return; | 742 if( zName==0 ) return; |
| 766 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ | 743 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ |
| 767 goto begin_table_error; | 744 goto begin_table_error; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 795 #endif | 772 #endif |
| 796 | 773 |
| 797 /* Make sure the new table name does not collide with an existing | 774 /* Make sure the new table name does not collide with an existing |
| 798 ** index or table name in the same database. Issue an error message if | 775 ** index or table name in the same database. Issue an error message if |
| 799 ** it does. The exception is if the statement being parsed was passed | 776 ** it does. The exception is if the statement being parsed was passed |
| 800 ** to an sqlite3_declare_vtab() call. In that case only the column names | 777 ** to an sqlite3_declare_vtab() call. In that case only the column names |
| 801 ** and types will be used, so there is no need to test for namespace | 778 ** and types will be used, so there is no need to test for namespace |
| 802 ** collisions. | 779 ** collisions. |
| 803 */ | 780 */ |
| 804 if( !IN_DECLARE_VTAB ){ | 781 if( !IN_DECLARE_VTAB ){ |
| 782 char *zDb = db->aDb[iDb].zName; |
| 805 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ | 783 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 806 goto begin_table_error; | 784 goto begin_table_error; |
| 807 } | 785 } |
| 808 pTable = sqlite3FindTable(db, zName, db->aDb[iDb].zName); | 786 pTable = sqlite3FindTable(db, zName, zDb); |
| 809 if( pTable ){ | 787 if( pTable ){ |
| 810 if( !noErr ){ | 788 if( !noErr ){ |
| 811 sqlite3ErrorMsg(pParse, "table %T already exists", pName); | 789 sqlite3ErrorMsg(pParse, "table %T already exists", pName); |
| 812 } | 790 } |
| 813 goto begin_table_error; | 791 goto begin_table_error; |
| 814 } | 792 } |
| 815 if( sqlite3FindIndex(db, zName, 0)!=0 && (iDb==0 || !db->init.busy) ){ | 793 if( sqlite3FindIndex(db, zName, zDb)!=0 ){ |
| 816 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName); | 794 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName); |
| 817 goto begin_table_error; | 795 goto begin_table_error; |
| 818 } | 796 } |
| 819 } | 797 } |
| 820 | 798 |
| 821 pTable = sqlite3DbMallocZero(db, sizeof(Table)); | 799 pTable = sqlite3DbMallocZero(db, sizeof(Table)); |
| 822 if( pTable==0 ){ | 800 if( pTable==0 ){ |
| 823 db->mallocFailed = 1; | 801 db->mallocFailed = 1; |
| 824 pParse->rc = SQLITE_NOMEM; | 802 pParse->rc = SQLITE_NOMEM; |
| 825 pParse->nErr++; | 803 pParse->nErr++; |
| 826 goto begin_table_error; | 804 goto begin_table_error; |
| 827 } | 805 } |
| 828 pTable->zName = zName; | 806 pTable->zName = zName; |
| 829 pTable->iPKey = -1; | 807 pTable->iPKey = -1; |
| 830 pTable->pSchema = db->aDb[iDb].pSchema; | 808 pTable->pSchema = db->aDb[iDb].pSchema; |
| 831 pTable->nRef = 1; | 809 pTable->nRef = 1; |
| 832 pTable->dbMem = 0; | 810 pTable->nRowEst = 1000000; |
| 833 assert( pParse->pNewTable==0 ); | 811 assert( pParse->pNewTable==0 ); |
| 834 pParse->pNewTable = pTable; | 812 pParse->pNewTable = pTable; |
| 835 | 813 |
| 836 /* If this is the magic sqlite_sequence table used by autoincrement, | 814 /* If this is the magic sqlite_sequence table used by autoincrement, |
| 837 ** then record a pointer to this table in the main database structure | 815 ** then record a pointer to this table in the main database structure |
| 838 ** so that INSERT can find the table easily. | 816 ** so that INSERT can find the table easily. |
| 839 */ | 817 */ |
| 840 #ifndef SQLITE_OMIT_AUTOINCREMENT | 818 #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 841 if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){ | 819 if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){ |
| 842 pTable->pSchema->pSeqTab = pTable; | 820 pTable->pSchema->pSeqTab = pTable; |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1167 pTab->iPKey = iCol; | 1145 pTab->iPKey = iCol; |
| 1168 pTab->keyConf = (u8)onError; | 1146 pTab->keyConf = (u8)onError; |
| 1169 assert( autoInc==0 || autoInc==1 ); | 1147 assert( autoInc==0 || autoInc==1 ); |
| 1170 pTab->tabFlags |= autoInc*TF_Autoincrement; | 1148 pTab->tabFlags |= autoInc*TF_Autoincrement; |
| 1171 }else if( autoInc ){ | 1149 }else if( autoInc ){ |
| 1172 #ifndef SQLITE_OMIT_AUTOINCREMENT | 1150 #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 1173 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an " | 1151 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an " |
| 1174 "INTEGER PRIMARY KEY"); | 1152 "INTEGER PRIMARY KEY"); |
| 1175 #endif | 1153 #endif |
| 1176 }else{ | 1154 }else{ |
| 1177 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0); | 1155 Index *p; |
| 1156 p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0); |
| 1157 if( p ){ |
| 1158 p->autoIndex = 2; |
| 1159 } |
| 1178 pList = 0; | 1160 pList = 0; |
| 1179 } | 1161 } |
| 1180 | 1162 |
| 1181 primary_key_exit: | 1163 primary_key_exit: |
| 1182 sqlite3ExprListDelete(pParse->db, pList); | 1164 sqlite3ExprListDelete(pParse->db, pList); |
| 1183 return; | 1165 return; |
| 1184 } | 1166 } |
| 1185 | 1167 |
| 1186 /* | 1168 /* |
| 1187 ** Add a new CHECK constraint to the table currently under construction. | 1169 ** Add a new CHECK constraint to the table currently under construction. |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1370 if( n<50 ){ | 1352 if( n<50 ){ |
| 1371 zSep = ""; | 1353 zSep = ""; |
| 1372 zSep2 = ","; | 1354 zSep2 = ","; |
| 1373 zEnd = ")"; | 1355 zEnd = ")"; |
| 1374 }else{ | 1356 }else{ |
| 1375 zSep = "\n "; | 1357 zSep = "\n "; |
| 1376 zSep2 = ",\n "; | 1358 zSep2 = ",\n "; |
| 1377 zEnd = "\n)"; | 1359 zEnd = "\n)"; |
| 1378 } | 1360 } |
| 1379 n += 35 + 6*p->nCol; | 1361 n += 35 + 6*p->nCol; |
| 1380 zStmt = sqlite3Malloc( n ); | 1362 zStmt = sqlite3DbMallocRaw(0, n); |
| 1381 if( zStmt==0 ){ | 1363 if( zStmt==0 ){ |
| 1382 db->mallocFailed = 1; | 1364 db->mallocFailed = 1; |
| 1383 return 0; | 1365 return 0; |
| 1384 } | 1366 } |
| 1385 sqlite3_snprintf(n, zStmt, "CREATE TABLE "); | 1367 sqlite3_snprintf(n, zStmt, "CREATE TABLE "); |
| 1386 k = sqlite3Strlen30(zStmt); | 1368 k = sqlite3Strlen30(zStmt); |
| 1387 identPut(zStmt, &k, p->zName); | 1369 identPut(zStmt, &k, p->zName); |
| 1388 zStmt[k++] = '('; | 1370 zStmt[k++] = '('; |
| 1389 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){ | 1371 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){ |
| 1390 static const char * const azType[] = { | 1372 static const char * const azType[] = { |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1551 sqlite3Select(pParse, pSelect, &dest); | 1533 sqlite3Select(pParse, pSelect, &dest); |
| 1552 sqlite3VdbeAddOp1(v, OP_Close, 1); | 1534 sqlite3VdbeAddOp1(v, OP_Close, 1); |
| 1553 if( pParse->nErr==0 ){ | 1535 if( pParse->nErr==0 ){ |
| 1554 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect); | 1536 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect); |
| 1555 if( pSelTab==0 ) return; | 1537 if( pSelTab==0 ) return; |
| 1556 assert( p->aCol==0 ); | 1538 assert( p->aCol==0 ); |
| 1557 p->nCol = pSelTab->nCol; | 1539 p->nCol = pSelTab->nCol; |
| 1558 p->aCol = pSelTab->aCol; | 1540 p->aCol = pSelTab->aCol; |
| 1559 pSelTab->nCol = 0; | 1541 pSelTab->nCol = 0; |
| 1560 pSelTab->aCol = 0; | 1542 pSelTab->aCol = 0; |
| 1561 sqlite3DeleteTable(pSelTab); | 1543 sqlite3DeleteTable(db, pSelTab); |
| 1562 } | 1544 } |
| 1563 } | 1545 } |
| 1564 | 1546 |
| 1565 /* Compute the complete text of the CREATE statement */ | 1547 /* Compute the complete text of the CREATE statement */ |
| 1566 if( pSelect ){ | 1548 if( pSelect ){ |
| 1567 zStmt = createTableStmt(db, p); | 1549 zStmt = createTableStmt(db, p); |
| 1568 }else{ | 1550 }else{ |
| 1569 n = (int)(pEnd->z - pParse->sNameToken.z) + 1; | 1551 n = (int)(pEnd->z - pParse->sNameToken.z) + 1; |
| 1570 zStmt = sqlite3MPrintf(db, | 1552 zStmt = sqlite3MPrintf(db, |
| 1571 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z | 1553 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1665 int iDb; | 1647 int iDb; |
| 1666 sqlite3 *db = pParse->db; | 1648 sqlite3 *db = pParse->db; |
| 1667 | 1649 |
| 1668 if( pParse->nVar>0 ){ | 1650 if( pParse->nVar>0 ){ |
| 1669 sqlite3ErrorMsg(pParse, "parameters are not allowed in views"); | 1651 sqlite3ErrorMsg(pParse, "parameters are not allowed in views"); |
| 1670 sqlite3SelectDelete(db, pSelect); | 1652 sqlite3SelectDelete(db, pSelect); |
| 1671 return; | 1653 return; |
| 1672 } | 1654 } |
| 1673 sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr); | 1655 sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr); |
| 1674 p = pParse->pNewTable; | 1656 p = pParse->pNewTable; |
| 1675 if( p==0 ){ | 1657 if( p==0 || pParse->nErr ){ |
| 1676 sqlite3SelectDelete(db, pSelect); | 1658 sqlite3SelectDelete(db, pSelect); |
| 1677 return; | 1659 return; |
| 1678 } | 1660 } |
| 1679 assert( pParse->nErr==0 ); /* If sqlite3StartTable return non-NULL then | |
| 1680 ** there could not have been an error */ | |
| 1681 sqlite3TwoPartName(pParse, pName1, pName2, &pName); | 1661 sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
| 1682 iDb = sqlite3SchemaToIndex(db, p->pSchema); | 1662 iDb = sqlite3SchemaToIndex(db, p->pSchema); |
| 1683 if( sqlite3FixInit(&sFix, pParse, iDb, "view", pName) | 1663 if( sqlite3FixInit(&sFix, pParse, iDb, "view", pName) |
| 1684 && sqlite3FixSelect(&sFix, pSelect) | 1664 && sqlite3FixSelect(&sFix, pSelect) |
| 1685 ){ | 1665 ){ |
| 1686 sqlite3SelectDelete(db, pSelect); | 1666 sqlite3SelectDelete(db, pSelect); |
| 1687 return; | 1667 return; |
| 1688 } | 1668 } |
| 1689 | 1669 |
| 1690 /* Make a copy of the entire SELECT statement that defines the view. | 1670 /* Make a copy of the entire SELECT statement that defines the view. |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1795 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel); | 1775 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel); |
| 1796 #endif | 1776 #endif |
| 1797 db->lookaside.bEnabled = enableLookaside; | 1777 db->lookaside.bEnabled = enableLookaside; |
| 1798 pParse->nTab = n; | 1778 pParse->nTab = n; |
| 1799 if( pSelTab ){ | 1779 if( pSelTab ){ |
| 1800 assert( pTable->aCol==0 ); | 1780 assert( pTable->aCol==0 ); |
| 1801 pTable->nCol = pSelTab->nCol; | 1781 pTable->nCol = pSelTab->nCol; |
| 1802 pTable->aCol = pSelTab->aCol; | 1782 pTable->aCol = pSelTab->aCol; |
| 1803 pSelTab->nCol = 0; | 1783 pSelTab->nCol = 0; |
| 1804 pSelTab->aCol = 0; | 1784 pSelTab->aCol = 0; |
| 1805 sqlite3DeleteTable(pSelTab); | 1785 sqlite3DeleteTable(db, pSelTab); |
| 1806 pTable->pSchema->flags |= DB_UnresetViews; | 1786 pTable->pSchema->flags |= DB_UnresetViews; |
| 1807 }else{ | 1787 }else{ |
| 1808 pTable->nCol = 0; | 1788 pTable->nCol = 0; |
| 1809 nErr++; | 1789 nErr++; |
| 1810 } | 1790 } |
| 1811 sqlite3SelectDelete(db, pSel); | 1791 sqlite3SelectDelete(db, pSel); |
| 1812 } else { | 1792 } else { |
| 1813 nErr++; | 1793 nErr++; |
| 1814 } | 1794 } |
| 1815 #endif /* SQLITE_OMIT_VIEW */ | 1795 #endif /* SQLITE_OMIT_VIEW */ |
| 1816 return nErr; | 1796 return nErr; |
| 1817 } | 1797 } |
| 1818 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */ | 1798 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
| 1819 | 1799 |
| 1820 #ifndef SQLITE_OMIT_VIEW | 1800 #ifndef SQLITE_OMIT_VIEW |
| 1821 /* | 1801 /* |
| 1822 ** Clear the column names from every VIEW in database idx. | 1802 ** Clear the column names from every VIEW in database idx. |
| 1823 */ | 1803 */ |
| 1824 static void sqliteViewResetAll(sqlite3 *db, int idx){ | 1804 static void sqliteViewResetAll(sqlite3 *db, int idx){ |
| 1825 HashElem *i; | 1805 HashElem *i; |
| 1826 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return; | 1806 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return; |
| 1827 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){ | 1807 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){ |
| 1828 Table *pTab = sqliteHashData(i); | 1808 Table *pTab = sqliteHashData(i); |
| 1829 if( pTab->pSelect ){ | 1809 if( pTab->pSelect ){ |
| 1830 sqliteResetColumnNames(pTab); | 1810 sqliteDeleteColumnNames(db, pTab); |
| 1811 pTab->aCol = 0; |
| 1812 pTab->nCol = 0; |
| 1831 } | 1813 } |
| 1832 } | 1814 } |
| 1833 DbClearProperty(db, idx, DB_UnresetViews); | 1815 DbClearProperty(db, idx, DB_UnresetViews); |
| 1834 } | 1816 } |
| 1835 #else | 1817 #else |
| 1836 # define sqliteViewResetAll(A,B) | 1818 # define sqliteViewResetAll(A,B) |
| 1837 #endif /* SQLITE_OMIT_VIEW */ | 1819 #endif /* SQLITE_OMIT_VIEW */ |
| 1838 | 1820 |
| 1839 /* | 1821 /* |
| 1840 ** This function is called by the VDBE to adjust the internal schema | 1822 ** This function is called by the VDBE to adjust the internal schema |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1970 Table *pTab; | 1952 Table *pTab; |
| 1971 Vdbe *v; | 1953 Vdbe *v; |
| 1972 sqlite3 *db = pParse->db; | 1954 sqlite3 *db = pParse->db; |
| 1973 int iDb; | 1955 int iDb; |
| 1974 | 1956 |
| 1975 if( db->mallocFailed ){ | 1957 if( db->mallocFailed ){ |
| 1976 goto exit_drop_table; | 1958 goto exit_drop_table; |
| 1977 } | 1959 } |
| 1978 assert( pParse->nErr==0 ); | 1960 assert( pParse->nErr==0 ); |
| 1979 assert( pName->nSrc==1 ); | 1961 assert( pName->nSrc==1 ); |
| 1962 if( noErr ) db->suppressErr++; |
| 1980 pTab = sqlite3LocateTable(pParse, isView, | 1963 pTab = sqlite3LocateTable(pParse, isView, |
| 1981 pName->a[0].zName, pName->a[0].zDatabase); | 1964 pName->a[0].zName, pName->a[0].zDatabase); |
| 1965 if( noErr ) db->suppressErr--; |
| 1982 | 1966 |
| 1983 if( pTab==0 ){ | 1967 if( pTab==0 ){ |
| 1984 if( noErr ){ | |
| 1985 sqlite3ErrorClear(pParse); | |
| 1986 } | |
| 1987 goto exit_drop_table; | 1968 goto exit_drop_table; |
| 1988 } | 1969 } |
| 1989 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); | 1970 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 1990 assert( iDb>=0 && iDb<db->nDb ); | 1971 assert( iDb>=0 && iDb<db->nDb ); |
| 1991 | 1972 |
| 1992 /* If pTab is a virtual table, call ViewGetColumnNames() to ensure | 1973 /* If pTab is a virtual table, call ViewGetColumnNames() to ensure |
| 1993 ** it is initialized. | 1974 ** it is initialized. |
| 1994 */ | 1975 */ |
| 1995 if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){ | 1976 if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){ |
| 1996 goto exit_drop_table; | 1977 goto exit_drop_table; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2056 if( v ){ | 2037 if( v ){ |
| 2057 Trigger *pTrigger; | 2038 Trigger *pTrigger; |
| 2058 Db *pDb = &db->aDb[iDb]; | 2039 Db *pDb = &db->aDb[iDb]; |
| 2059 sqlite3BeginWriteOperation(pParse, 1, iDb); | 2040 sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 2060 | 2041 |
| 2061 #ifndef SQLITE_OMIT_VIRTUALTABLE | 2042 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2062 if( IsVirtual(pTab) ){ | 2043 if( IsVirtual(pTab) ){ |
| 2063 sqlite3VdbeAddOp0(v, OP_VBegin); | 2044 sqlite3VdbeAddOp0(v, OP_VBegin); |
| 2064 } | 2045 } |
| 2065 #endif | 2046 #endif |
| 2047 sqlite3FkDropTable(pParse, pName, pTab); |
| 2066 | 2048 |
| 2067 /* Drop all triggers associated with the table being dropped. Code | 2049 /* Drop all triggers associated with the table being dropped. Code |
| 2068 ** is generated to remove entries from sqlite_master and/or | 2050 ** is generated to remove entries from sqlite_master and/or |
| 2069 ** sqlite_temp_master if required. | 2051 ** sqlite_temp_master if required. |
| 2070 */ | 2052 */ |
| 2071 pTrigger = sqlite3TriggerList(pParse, pTab); | 2053 pTrigger = sqlite3TriggerList(pParse, pTab); |
| 2072 while( pTrigger ){ | 2054 while( pTrigger ){ |
| 2073 assert( pTrigger->pSchema==pTab->pSchema || | 2055 assert( pTrigger->pSchema==pTab->pSchema || |
| 2074 pTrigger->pSchema==db->aDb[1].pSchema ); | 2056 pTrigger->pSchema==db->aDb[1].pSchema ); |
| 2075 sqlite3DropTriggerPtr(pParse, pTrigger); | 2057 sqlite3DropTriggerPtr(pParse, pTrigger); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2146 void sqlite3CreateForeignKey( | 2128 void sqlite3CreateForeignKey( |
| 2147 Parse *pParse, /* Parsing context */ | 2129 Parse *pParse, /* Parsing context */ |
| 2148 ExprList *pFromCol, /* Columns in this table that point to other table */ | 2130 ExprList *pFromCol, /* Columns in this table that point to other table */ |
| 2149 Token *pTo, /* Name of the other table */ | 2131 Token *pTo, /* Name of the other table */ |
| 2150 ExprList *pToCol, /* Columns in the other table */ | 2132 ExprList *pToCol, /* Columns in the other table */ |
| 2151 int flags /* Conflict resolution algorithms. */ | 2133 int flags /* Conflict resolution algorithms. */ |
| 2152 ){ | 2134 ){ |
| 2153 sqlite3 *db = pParse->db; | 2135 sqlite3 *db = pParse->db; |
| 2154 #ifndef SQLITE_OMIT_FOREIGN_KEY | 2136 #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 2155 FKey *pFKey = 0; | 2137 FKey *pFKey = 0; |
| 2138 FKey *pNextTo; |
| 2156 Table *p = pParse->pNewTable; | 2139 Table *p = pParse->pNewTable; |
| 2157 int nByte; | 2140 int nByte; |
| 2158 int i; | 2141 int i; |
| 2159 int nCol; | 2142 int nCol; |
| 2160 char *z; | 2143 char *z; |
| 2161 | 2144 |
| 2162 assert( pTo!=0 ); | 2145 assert( pTo!=0 ); |
| 2163 if( p==0 || IN_DECLARE_VTAB ) goto fk_end; | 2146 if( p==0 || IN_DECLARE_VTAB ) goto fk_end; |
| 2164 if( pFromCol==0 ){ | 2147 if( pFromCol==0 ){ |
| 2165 int iCol = p->nCol-1; | 2148 int iCol = p->nCol-1; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2220 if( pToCol ){ | 2203 if( pToCol ){ |
| 2221 for(i=0; i<nCol; i++){ | 2204 for(i=0; i<nCol; i++){ |
| 2222 int n = sqlite3Strlen30(pToCol->a[i].zName); | 2205 int n = sqlite3Strlen30(pToCol->a[i].zName); |
| 2223 pFKey->aCol[i].zCol = z; | 2206 pFKey->aCol[i].zCol = z; |
| 2224 memcpy(z, pToCol->a[i].zName, n); | 2207 memcpy(z, pToCol->a[i].zName, n); |
| 2225 z[n] = 0; | 2208 z[n] = 0; |
| 2226 z += n+1; | 2209 z += n+1; |
| 2227 } | 2210 } |
| 2228 } | 2211 } |
| 2229 pFKey->isDeferred = 0; | 2212 pFKey->isDeferred = 0; |
| 2230 pFKey->deleteConf = (u8)(flags & 0xff); | 2213 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */ |
| 2231 pFKey->updateConf = (u8)((flags >> 8 ) & 0xff); | 2214 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */ |
| 2232 pFKey->insertConf = (u8)((flags >> 16 ) & 0xff); | 2215 |
| 2216 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, |
| 2217 pFKey->zTo, sqlite3Strlen30(pFKey->zTo), (void *)pFKey |
| 2218 ); |
| 2219 if( pNextTo==pFKey ){ |
| 2220 db->mallocFailed = 1; |
| 2221 goto fk_end; |
| 2222 } |
| 2223 if( pNextTo ){ |
| 2224 assert( pNextTo->pPrevTo==0 ); |
| 2225 pFKey->pNextTo = pNextTo; |
| 2226 pNextTo->pPrevTo = pFKey; |
| 2227 } |
| 2233 | 2228 |
| 2234 /* Link the foreign key to the table as the last step. | 2229 /* Link the foreign key to the table as the last step. |
| 2235 */ | 2230 */ |
| 2236 p->pFKey = pFKey; | 2231 p->pFKey = pFKey; |
| 2237 pFKey = 0; | 2232 pFKey = 0; |
| 2238 | 2233 |
| 2239 fk_end: | 2234 fk_end: |
| 2240 sqlite3DbFree(db, pFKey); | 2235 sqlite3DbFree(db, pFKey); |
| 2241 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ | 2236 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ |
| 2242 sqlite3ExprListDelete(db, pFromCol); | 2237 sqlite3ExprListDelete(db, pFromCol); |
| 2243 sqlite3ExprListDelete(db, pToCol); | 2238 sqlite3ExprListDelete(db, pToCol); |
| 2244 } | 2239 } |
| 2245 | 2240 |
| 2246 /* | 2241 /* |
| 2247 ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED | 2242 ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED |
| 2248 ** clause is seen as part of a foreign key definition. The isDeferred | 2243 ** clause is seen as part of a foreign key definition. The isDeferred |
| 2249 ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE. | 2244 ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE. |
| 2250 ** The behavior of the most recently created foreign key is adjusted | 2245 ** The behavior of the most recently created foreign key is adjusted |
| 2251 ** accordingly. | 2246 ** accordingly. |
| 2252 */ | 2247 */ |
| 2253 void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){ | 2248 void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){ |
| 2254 #ifndef SQLITE_OMIT_FOREIGN_KEY | 2249 #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 2255 Table *pTab; | 2250 Table *pTab; |
| 2256 FKey *pFKey; | 2251 FKey *pFKey; |
| 2257 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return; | 2252 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return; |
| 2258 assert( isDeferred==0 || isDeferred==1 ); | 2253 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */ |
| 2259 pFKey->isDeferred = (u8)isDeferred; | 2254 pFKey->isDeferred = (u8)isDeferred; |
| 2260 #endif | 2255 #endif |
| 2261 } | 2256 } |
| 2262 | 2257 |
| 2263 /* | 2258 /* |
| 2264 ** Generate code that will erase and refill index *pIdx. This is | 2259 ** Generate code that will erase and refill index *pIdx. This is |
| 2265 ** used to initialize a newly created index or to recompute the | 2260 ** used to initialize a newly created index or to recompute the |
| 2266 ** content of an index in response to a REINDEX command. | 2261 ** content of an index in response to a REINDEX command. |
| 2267 ** | 2262 ** |
| 2268 ** if memRootPage is not negative, it means that the index is newly | 2263 ** if memRootPage is not negative, it means that the index is newly |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2343 ** Create a new index for an SQL table. pName1.pName2 is the name of the index | 2338 ** Create a new index for an SQL table. pName1.pName2 is the name of the index |
| 2344 ** and pTblList is the name of the table that is to be indexed. Both will | 2339 ** and pTblList is the name of the table that is to be indexed. Both will |
| 2345 ** be NULL for a primary key or an index that is created to satisfy a | 2340 ** be NULL for a primary key or an index that is created to satisfy a |
| 2346 ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable | 2341 ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable |
| 2347 ** as the table to be indexed. pParse->pNewTable is a table that is | 2342 ** as the table to be indexed. pParse->pNewTable is a table that is |
| 2348 ** currently being constructed by a CREATE TABLE statement. | 2343 ** currently being constructed by a CREATE TABLE statement. |
| 2349 ** | 2344 ** |
| 2350 ** pList is a list of columns to be indexed. pList will be NULL if this | 2345 ** pList is a list of columns to be indexed. pList will be NULL if this |
| 2351 ** is a primary key or unique-constraint on the most recent column added | 2346 ** is a primary key or unique-constraint on the most recent column added |
| 2352 ** to the table currently under construction. | 2347 ** to the table currently under construction. |
| 2348 ** |
| 2349 ** If the index is created successfully, return a pointer to the new Index |
| 2350 ** structure. This is used by sqlite3AddPrimaryKey() to mark the index |
| 2351 ** as the tables primary key (Index.autoIndex==2). |
| 2353 */ | 2352 */ |
| 2354 void sqlite3CreateIndex( | 2353 Index *sqlite3CreateIndex( |
| 2355 Parse *pParse, /* All information about this parse */ | 2354 Parse *pParse, /* All information about this parse */ |
| 2356 Token *pName1, /* First part of index name. May be NULL */ | 2355 Token *pName1, /* First part of index name. May be NULL */ |
| 2357 Token *pName2, /* Second part of index name. May be NULL */ | 2356 Token *pName2, /* Second part of index name. May be NULL */ |
| 2358 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */ | 2357 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */ |
| 2359 ExprList *pList, /* A list of columns to be indexed */ | 2358 ExprList *pList, /* A list of columns to be indexed */ |
| 2360 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ | 2359 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
| 2361 Token *pStart, /* The CREATE token that begins this statement */ | 2360 Token *pStart, /* The CREATE token that begins this statement */ |
| 2362 Token *pEnd, /* The ")" that closes the CREATE INDEX statement */ | 2361 Token *pEnd, /* The ")" that closes the CREATE INDEX statement */ |
| 2363 int sortOrder, /* Sort order of primary key when pList==NULL */ | 2362 int sortOrder, /* Sort order of primary key when pList==NULL */ |
| 2364 int ifNotExist /* Omit error if index already exists */ | 2363 int ifNotExist /* Omit error if index already exists */ |
| 2365 ){ | 2364 ){ |
| 2365 Index *pRet = 0; /* Pointer to return */ |
| 2366 Table *pTab = 0; /* Table to be indexed */ | 2366 Table *pTab = 0; /* Table to be indexed */ |
| 2367 Index *pIndex = 0; /* The index to be created */ | 2367 Index *pIndex = 0; /* The index to be created */ |
| 2368 char *zName = 0; /* Name of the index */ | 2368 char *zName = 0; /* Name of the index */ |
| 2369 int nName; /* Number of characters in zName */ | 2369 int nName; /* Number of characters in zName */ |
| 2370 int i, j; | 2370 int i, j; |
| 2371 Token nullId; /* Fake token for an empty ID list */ | 2371 Token nullId; /* Fake token for an empty ID list */ |
| 2372 DbFixer sFix; /* For assigning database names to pTable */ | 2372 DbFixer sFix; /* For assigning database names to pTable */ |
| 2373 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */ | 2373 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */ |
| 2374 sqlite3 *db = pParse->db; | 2374 sqlite3 *db = pParse->db; |
| 2375 Db *pDb; /* The specific table containing the indexed database */ | 2375 Db *pDb; /* The specific table containing the indexed database */ |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2592 Column *pTabCol; | 2592 Column *pTabCol; |
| 2593 int requestedSortOrder; | 2593 int requestedSortOrder; |
| 2594 char *zColl; /* Collation sequence name */ | 2594 char *zColl; /* Collation sequence name */ |
| 2595 | 2595 |
| 2596 for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){ | 2596 for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){ |
| 2597 if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break; | 2597 if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break; |
| 2598 } | 2598 } |
| 2599 if( j>=pTab->nCol ){ | 2599 if( j>=pTab->nCol ){ |
| 2600 sqlite3ErrorMsg(pParse, "table %s has no column named %s", | 2600 sqlite3ErrorMsg(pParse, "table %s has no column named %s", |
| 2601 pTab->zName, zColName); | 2601 pTab->zName, zColName); |
| 2602 pParse->checkSchema = 1; |
| 2602 goto exit_create_index; | 2603 goto exit_create_index; |
| 2603 } | 2604 } |
| 2604 pIndex->aiColumn[i] = j; | 2605 pIndex->aiColumn[i] = j; |
| 2605 /* Justification of the ALWAYS(pListItem->pExpr->pColl): Because of | 2606 /* Justification of the ALWAYS(pListItem->pExpr->pColl): Because of |
| 2606 ** the way the "idxlist" non-terminal is constructed by the parser, | 2607 ** the way the "idxlist" non-terminal is constructed by the parser, |
| 2607 ** if pListItem->pExpr is not null then either pListItem->pExpr->pColl | 2608 ** if pListItem->pExpr is not null then either pListItem->pExpr->pColl |
| 2608 ** must exist or else there must have been an OOM error. But if there | 2609 ** must exist or else there must have been an OOM error. But if there |
| 2609 ** was an OOM error, we would never reach this point. */ | 2610 ** was an OOM error, we would never reach this point. */ |
| 2610 if( pListItem->pExpr && ALWAYS(pListItem->pExpr->pColl) ){ | 2611 if( pListItem->pExpr && ALWAYS(pListItem->pExpr->pColl) ){ |
| 2611 int nColl; | 2612 int nColl; |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2767 ); | 2768 ); |
| 2768 sqlite3DbFree(db, zStmt); | 2769 sqlite3DbFree(db, zStmt); |
| 2769 | 2770 |
| 2770 /* Fill the index with data and reparse the schema. Code an OP_Expire | 2771 /* Fill the index with data and reparse the schema. Code an OP_Expire |
| 2771 ** to invalidate all pre-compiled statements. | 2772 ** to invalidate all pre-compiled statements. |
| 2772 */ | 2773 */ |
| 2773 if( pTblName ){ | 2774 if( pTblName ){ |
| 2774 sqlite3RefillIndex(pParse, pIndex, iMem); | 2775 sqlite3RefillIndex(pParse, pIndex, iMem); |
| 2775 sqlite3ChangeCookie(pParse, iDb); | 2776 sqlite3ChangeCookie(pParse, iDb); |
| 2776 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, | 2777 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, |
| 2777 sqlite3MPrintf(db, "name='%q'", pIndex->zName), P4_DYNAMIC); | 2778 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), |
| 2779 P4_DYNAMIC); |
| 2778 sqlite3VdbeAddOp1(v, OP_Expire, 0); | 2780 sqlite3VdbeAddOp1(v, OP_Expire, 0); |
| 2779 } | 2781 } |
| 2780 } | 2782 } |
| 2781 | 2783 |
| 2782 /* When adding an index to the list of indices for a table, make | 2784 /* When adding an index to the list of indices for a table, make |
| 2783 ** sure all indices labeled OE_Replace come after all those labeled | 2785 ** sure all indices labeled OE_Replace come after all those labeled |
| 2784 ** OE_Ignore. This is necessary for the correct constraint check | 2786 ** OE_Ignore. This is necessary for the correct constraint check |
| 2785 ** processing (in sqlite3GenerateConstraintChecks()) as part of | 2787 ** processing (in sqlite3GenerateConstraintChecks()) as part of |
| 2786 ** UPDATE and INSERT statements. | 2788 ** UPDATE and INSERT statements. |
| 2787 */ | 2789 */ |
| 2788 if( db->init.busy || pTblName==0 ){ | 2790 if( db->init.busy || pTblName==0 ){ |
| 2789 if( onError!=OE_Replace || pTab->pIndex==0 | 2791 if( onError!=OE_Replace || pTab->pIndex==0 |
| 2790 || pTab->pIndex->onError==OE_Replace){ | 2792 || pTab->pIndex->onError==OE_Replace){ |
| 2791 pIndex->pNext = pTab->pIndex; | 2793 pIndex->pNext = pTab->pIndex; |
| 2792 pTab->pIndex = pIndex; | 2794 pTab->pIndex = pIndex; |
| 2793 }else{ | 2795 }else{ |
| 2794 Index *pOther = pTab->pIndex; | 2796 Index *pOther = pTab->pIndex; |
| 2795 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){ | 2797 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){ |
| 2796 pOther = pOther->pNext; | 2798 pOther = pOther->pNext; |
| 2797 } | 2799 } |
| 2798 pIndex->pNext = pOther->pNext; | 2800 pIndex->pNext = pOther->pNext; |
| 2799 pOther->pNext = pIndex; | 2801 pOther->pNext = pIndex; |
| 2800 } | 2802 } |
| 2803 pRet = pIndex; |
| 2801 pIndex = 0; | 2804 pIndex = 0; |
| 2802 } | 2805 } |
| 2803 | 2806 |
| 2804 /* Clean up before exiting */ | 2807 /* Clean up before exiting */ |
| 2805 exit_create_index: | 2808 exit_create_index: |
| 2806 if( pIndex ){ | 2809 if( pIndex ){ |
| 2807 sqlite3_free(pIndex->zColAff); | 2810 sqlite3DbFree(db, pIndex->zColAff); |
| 2808 sqlite3DbFree(db, pIndex); | 2811 sqlite3DbFree(db, pIndex); |
| 2809 } | 2812 } |
| 2810 sqlite3ExprListDelete(db, pList); | 2813 sqlite3ExprListDelete(db, pList); |
| 2811 sqlite3SrcListDelete(db, pTblName); | 2814 sqlite3SrcListDelete(db, pTblName); |
| 2812 sqlite3DbFree(db, zName); | 2815 sqlite3DbFree(db, zName); |
| 2813 return; | 2816 return pRet; |
| 2814 } | 2817 } |
| 2815 | 2818 |
| 2816 /* | 2819 /* |
| 2817 ** Fill the Index.aiRowEst[] array with default information - information | 2820 ** Fill the Index.aiRowEst[] array with default information - information |
| 2818 ** to be used when we have not run the ANALYZE command. | 2821 ** to be used when we have not run the ANALYZE command. |
| 2819 ** | 2822 ** |
| 2820 ** aiRowEst[0] is suppose to contain the number of elements in the index. | 2823 ** aiRowEst[0] is suppose to contain the number of elements in the index. |
| 2821 ** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the | 2824 ** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the |
| 2822 ** number of rows in the table that match any particular value of the | 2825 ** number of rows in the table that match any particular value of the |
| 2823 ** first column of the index. aiRowEst[2] is an estimate of the number | 2826 ** first column of the index. aiRowEst[2] is an estimate of the number |
| 2824 ** of rows that match any particular combiniation of the first 2 columns | 2827 ** of rows that match any particular combiniation of the first 2 columns |
| 2825 ** of the index. And so forth. It must always be the case that | 2828 ** of the index. And so forth. It must always be the case that |
| 2826 * | 2829 * |
| 2827 ** aiRowEst[N]<=aiRowEst[N-1] | 2830 ** aiRowEst[N]<=aiRowEst[N-1] |
| 2828 ** aiRowEst[N]>=1 | 2831 ** aiRowEst[N]>=1 |
| 2829 ** | 2832 ** |
| 2830 ** Apart from that, we have little to go on besides intuition as to | 2833 ** Apart from that, we have little to go on besides intuition as to |
| 2831 ** how aiRowEst[] should be initialized. The numbers generated here | 2834 ** how aiRowEst[] should be initialized. The numbers generated here |
| 2832 ** are based on typical values found in actual indices. | 2835 ** are based on typical values found in actual indices. |
| 2833 */ | 2836 */ |
| 2834 void sqlite3DefaultRowEst(Index *pIdx){ | 2837 void sqlite3DefaultRowEst(Index *pIdx){ |
| 2835 unsigned *a = pIdx->aiRowEst; | 2838 unsigned *a = pIdx->aiRowEst; |
| 2836 int i; | 2839 int i; |
| 2840 unsigned n; |
| 2837 assert( a!=0 ); | 2841 assert( a!=0 ); |
| 2838 a[0] = 1000000; | 2842 a[0] = pIdx->pTable->nRowEst; |
| 2839 for(i=pIdx->nColumn; i>=5; i--){ | 2843 if( a[0]<10 ) a[0] = 10; |
| 2840 a[i] = 5; | 2844 n = 10; |
| 2841 } | 2845 for(i=1; i<=pIdx->nColumn; i++){ |
| 2842 while( i>=1 ){ | 2846 a[i] = n; |
| 2843 a[i] = 11 - i; | 2847 if( n>5 ) n--; |
| 2844 i--; | |
| 2845 } | 2848 } |
| 2846 if( pIdx->onError!=OE_None ){ | 2849 if( pIdx->onError!=OE_None ){ |
| 2847 a[pIdx->nColumn] = 1; | 2850 a[pIdx->nColumn] = 1; |
| 2848 } | 2851 } |
| 2849 } | 2852 } |
| 2850 | 2853 |
| 2851 /* | 2854 /* |
| 2852 ** This routine will drop an existing named index. This routine | 2855 ** This routine will drop an existing named index. This routine |
| 2853 ** implements the DROP INDEX statement. | 2856 ** implements the DROP INDEX statement. |
| 2854 */ | 2857 */ |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2894 goto exit_drop_index; | 2897 goto exit_drop_index; |
| 2895 } | 2898 } |
| 2896 } | 2899 } |
| 2897 #endif | 2900 #endif |
| 2898 | 2901 |
| 2899 /* Generate code to remove the index and from the master table */ | 2902 /* Generate code to remove the index and from the master table */ |
| 2900 v = sqlite3GetVdbe(pParse); | 2903 v = sqlite3GetVdbe(pParse); |
| 2901 if( v ){ | 2904 if( v ){ |
| 2902 sqlite3BeginWriteOperation(pParse, 1, iDb); | 2905 sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 2903 sqlite3NestedParse(pParse, | 2906 sqlite3NestedParse(pParse, |
| 2904 "DELETE FROM %Q.%s WHERE name=%Q", | 2907 "DELETE FROM %Q.%s WHERE name=%Q AND type='index'", |
| 2905 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), | 2908 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), |
| 2906 pIndex->zName | 2909 pIndex->zName |
| 2907 ); | 2910 ); |
| 2908 if( sqlite3FindTable(db, "sqlite_stat1", db->aDb[iDb].zName) ){ | 2911 if( sqlite3FindTable(db, "sqlite_stat1", db->aDb[iDb].zName) ){ |
| 2909 sqlite3NestedParse(pParse, | 2912 sqlite3NestedParse(pParse, |
| 2910 "DELETE FROM %Q.sqlite_stat1 WHERE idx=%Q", | 2913 "DELETE FROM %Q.sqlite_stat1 WHERE idx=%Q", |
| 2911 db->aDb[iDb].zName, pIndex->zName | 2914 db->aDb[iDb].zName, pIndex->zName |
| 2912 ); | 2915 ); |
| 2913 } | 2916 } |
| 2914 sqlite3ChangeCookie(pParse, iDb); | 2917 sqlite3ChangeCookie(pParse, iDb); |
| (...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3176 */ | 3179 */ |
| 3177 void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){ | 3180 void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){ |
| 3178 int i; | 3181 int i; |
| 3179 struct SrcList_item *pItem; | 3182 struct SrcList_item *pItem; |
| 3180 if( pList==0 ) return; | 3183 if( pList==0 ) return; |
| 3181 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){ | 3184 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){ |
| 3182 sqlite3DbFree(db, pItem->zDatabase); | 3185 sqlite3DbFree(db, pItem->zDatabase); |
| 3183 sqlite3DbFree(db, pItem->zName); | 3186 sqlite3DbFree(db, pItem->zName); |
| 3184 sqlite3DbFree(db, pItem->zAlias); | 3187 sqlite3DbFree(db, pItem->zAlias); |
| 3185 sqlite3DbFree(db, pItem->zIndex); | 3188 sqlite3DbFree(db, pItem->zIndex); |
| 3186 sqlite3DeleteTable(pItem->pTab); | 3189 sqlite3DeleteTable(db, pItem->pTab); |
| 3187 sqlite3SelectDelete(db, pItem->pSelect); | 3190 sqlite3SelectDelete(db, pItem->pSelect); |
| 3188 sqlite3ExprDelete(db, pItem->pOn); | 3191 sqlite3ExprDelete(db, pItem->pOn); |
| 3189 sqlite3IdListDelete(db, pItem->pUsing); | 3192 sqlite3IdListDelete(db, pItem->pUsing); |
| 3190 } | 3193 } |
| 3191 sqlite3DbFree(db, pList); | 3194 sqlite3DbFree(db, pList); |
| 3192 } | 3195 } |
| 3193 | 3196 |
| 3194 /* | 3197 /* |
| 3195 ** This routine is called by the parser to add a new term to the | 3198 ** This routine is called by the parser to add a new term to the |
| 3196 ** end of a growing FROM clause. The "p" parameter is the part of | 3199 ** end of a growing FROM clause. The "p" parameter is the part of |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3359 | 3362 |
| 3360 /* | 3363 /* |
| 3361 ** This function is called by the parser when it parses a command to create, | 3364 ** This function is called by the parser when it parses a command to create, |
| 3362 ** release or rollback an SQL savepoint. | 3365 ** release or rollback an SQL savepoint. |
| 3363 */ | 3366 */ |
| 3364 void sqlite3Savepoint(Parse *pParse, int op, Token *pName){ | 3367 void sqlite3Savepoint(Parse *pParse, int op, Token *pName){ |
| 3365 char *zName = sqlite3NameFromToken(pParse->db, pName); | 3368 char *zName = sqlite3NameFromToken(pParse->db, pName); |
| 3366 if( zName ){ | 3369 if( zName ){ |
| 3367 Vdbe *v = sqlite3GetVdbe(pParse); | 3370 Vdbe *v = sqlite3GetVdbe(pParse); |
| 3368 #ifndef SQLITE_OMIT_AUTHORIZATION | 3371 #ifndef SQLITE_OMIT_AUTHORIZATION |
| 3369 static const char *az[] = { "BEGIN", "RELEASE", "ROLLBACK" }; | 3372 static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" }; |
| 3370 assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 ); | 3373 assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 ); |
| 3371 #endif | 3374 #endif |
| 3372 if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){ | 3375 if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){ |
| 3373 sqlite3DbFree(pParse->db, zName); | 3376 sqlite3DbFree(pParse->db, zName); |
| 3374 return; | 3377 return; |
| 3375 } | 3378 } |
| 3376 sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC); | 3379 sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC); |
| 3377 } | 3380 } |
| 3378 } | 3381 } |
| 3379 | 3382 |
| 3380 /* | 3383 /* |
| 3381 ** Make sure the TEMP database is open and available for use. Return | 3384 ** Make sure the TEMP database is open and available for use. Return |
| 3382 ** the number of errors. Leave any error messages in the pParse structure. | 3385 ** the number of errors. Leave any error messages in the pParse structure. |
| 3383 */ | 3386 */ |
| 3384 int sqlite3OpenTempDatabase(Parse *pParse){ | 3387 int sqlite3OpenTempDatabase(Parse *pParse){ |
| 3385 sqlite3 *db = pParse->db; | 3388 sqlite3 *db = pParse->db; |
| 3386 if( db->aDb[1].pBt==0 && !pParse->explain ){ | 3389 if( db->aDb[1].pBt==0 && !pParse->explain ){ |
| 3387 int rc; | 3390 int rc; |
| 3391 Btree *pBt; |
| 3388 static const int flags = | 3392 static const int flags = |
| 3389 SQLITE_OPEN_READWRITE | | 3393 SQLITE_OPEN_READWRITE | |
| 3390 SQLITE_OPEN_CREATE | | 3394 SQLITE_OPEN_CREATE | |
| 3391 SQLITE_OPEN_EXCLUSIVE | | 3395 SQLITE_OPEN_EXCLUSIVE | |
| 3392 SQLITE_OPEN_DELETEONCLOSE | | 3396 SQLITE_OPEN_DELETEONCLOSE | |
| 3393 SQLITE_OPEN_TEMP_DB; | 3397 SQLITE_OPEN_TEMP_DB; |
| 3394 | 3398 |
| 3395 rc = sqlite3BtreeFactory(db, 0, 0, SQLITE_DEFAULT_CACHE_SIZE, flags, | 3399 rc = sqlite3BtreeOpen(0, db, &pBt, 0, flags); |
| 3396 &db->aDb[1].pBt); | |
| 3397 if( rc!=SQLITE_OK ){ | 3400 if( rc!=SQLITE_OK ){ |
| 3398 sqlite3ErrorMsg(pParse, "unable to open a temporary database " | 3401 sqlite3ErrorMsg(pParse, "unable to open a temporary database " |
| 3399 "file for storing temporary tables"); | 3402 "file for storing temporary tables"); |
| 3400 pParse->rc = rc; | 3403 pParse->rc = rc; |
| 3401 return 1; | 3404 return 1; |
| 3402 } | 3405 } |
| 3403 assert( (db->flags & SQLITE_InTrans)==0 || db->autoCommit ); | 3406 db->aDb[1].pBt = pBt; |
| 3404 assert( db->aDb[1].pSchema ); | 3407 assert( db->aDb[1].pSchema ); |
| 3405 sqlite3PagerJournalMode(sqlite3BtreePager(db->aDb[1].pBt), | 3408 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){ |
| 3406 db->dfltJournalMode); | 3409 db->mallocFailed = 1; |
| 3410 return 1; |
| 3411 } |
| 3407 } | 3412 } |
| 3408 return 0; | 3413 return 0; |
| 3409 } | 3414 } |
| 3410 | 3415 |
| 3411 /* | 3416 /* |
| 3412 ** Generate VDBE code that will verify the schema cookie and start | 3417 ** Generate VDBE code that will verify the schema cookie and start |
| 3413 ** a read-transaction for all named database files. | 3418 ** a read-transaction for all named database files. |
| 3414 ** | 3419 ** |
| 3415 ** It is important that all schema cookies be verified and all | 3420 ** It is important that all schema cookies be verified and all |
| 3416 ** read transactions be started before anything else happens in | 3421 ** read transactions be started before anything else happens in |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3469 ** can be checked before any changes are made to the database, it is never | 3474 ** can be checked before any changes are made to the database, it is never |
| 3470 ** necessary to undo a write and the checkpoint should not be set. | 3475 ** necessary to undo a write and the checkpoint should not be set. |
| 3471 */ | 3476 */ |
| 3472 void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){ | 3477 void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){ |
| 3473 Parse *pToplevel = sqlite3ParseToplevel(pParse); | 3478 Parse *pToplevel = sqlite3ParseToplevel(pParse); |
| 3474 sqlite3CodeVerifySchema(pParse, iDb); | 3479 sqlite3CodeVerifySchema(pParse, iDb); |
| 3475 pToplevel->writeMask |= 1<<iDb; | 3480 pToplevel->writeMask |= 1<<iDb; |
| 3476 pToplevel->isMultiWrite |= setStatement; | 3481 pToplevel->isMultiWrite |= setStatement; |
| 3477 } | 3482 } |
| 3478 | 3483 |
| 3484 /* |
| 3485 ** Indicate that the statement currently under construction might write |
| 3486 ** more than one entry (example: deleting one row then inserting another, |
| 3487 ** inserting multiple rows in a table, or inserting a row and index entries.) |
| 3488 ** If an abort occurs after some of these writes have completed, then it will |
| 3489 ** be necessary to undo the completed writes. |
| 3490 */ |
| 3491 void sqlite3MultiWrite(Parse *pParse){ |
| 3492 Parse *pToplevel = sqlite3ParseToplevel(pParse); |
| 3493 pToplevel->isMultiWrite = 1; |
| 3494 } |
| 3495 |
| 3479 /* | 3496 /* |
| 3480 ** Set the "may throw abort exception" flag for the statement currently | 3497 ** The code generator calls this routine if is discovers that it is |
| 3481 ** being coded. | 3498 ** possible to abort a statement prior to completion. In order to |
| 3499 ** perform this abort without corrupting the database, we need to make |
| 3500 ** sure that the statement is protected by a statement transaction. |
| 3501 ** |
| 3502 ** Technically, we only need to set the mayAbort flag if the |
| 3503 ** isMultiWrite flag was previously set. There is a time dependency |
| 3504 ** such that the abort must occur after the multiwrite. This makes |
| 3505 ** some statements involving the REPLACE conflict resolution algorithm |
| 3506 ** go a little faster. But taking advantage of this time dependency |
| 3507 ** makes it more difficult to prove that the code is correct (in |
| 3508 ** particular, it prevents us from writing an effective |
| 3509 ** implementation of sqlite3AssertMayAbort()) and so we have chosen |
| 3510 ** to take the safe route and skip the optimization. |
| 3482 */ | 3511 */ |
| 3483 void sqlite3MayAbort(Parse *pParse){ | 3512 void sqlite3MayAbort(Parse *pParse){ |
| 3484 Parse *pToplevel = sqlite3ParseToplevel(pParse); | 3513 Parse *pToplevel = sqlite3ParseToplevel(pParse); |
| 3485 pToplevel->mayAbort = 1; | 3514 pToplevel->mayAbort = 1; |
| 3486 } | 3515 } |
| 3487 | 3516 |
| 3488 /* | 3517 /* |
| 3489 ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT | 3518 ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT |
| 3490 ** error. The onError parameter determines which (if any) of the statement | 3519 ** error. The onError parameter determines which (if any) of the statement |
| 3491 ** and/or current transaction is rolled back. | 3520 ** and/or current transaction is rolled back. |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3682 rc = sqlite3PagerLoadall(pPager); | 3711 rc = sqlite3PagerLoadall(pPager); |
| 3683 if (rc == SQLITE_OK) | 3712 if (rc == SQLITE_OK) |
| 3684 dbsLoaded++; | 3713 dbsLoaded++; |
| 3685 } | 3714 } |
| 3686 } | 3715 } |
| 3687 if (dbsLoaded == 0) | 3716 if (dbsLoaded == 0) |
| 3688 return SQLITE_ERROR; | 3717 return SQLITE_ERROR; |
| 3689 return SQLITE_OK; | 3718 return SQLITE_OK; |
| 3690 } | 3719 } |
| 3691 /* End preload-cache.patch for Chromium */ | 3720 /* End preload-cache.patch for Chromium */ |
| OLD | NEW |