diff options
Diffstat (limited to 'src/build.c')
-rw-r--r-- | src/build.c | 37 |
1 files changed, 26 insertions, 11 deletions
diff --git a/src/build.c b/src/build.c index c23d92fb9..0fbe07e19 100644 --- a/src/build.c +++ b/src/build.c @@ -504,7 +504,6 @@ static void sqliteResetColumnNames(Table *pTable){ */ void sqlite3DeleteTable(Table *pTable){ Index *pIndex, *pNext; - FKey *pFKey, *pNextFKey; sqlite3 *db; if( pTable==0 ) return; @@ -526,13 +525,8 @@ void sqlite3DeleteTable(Table *pTable){ sqlite3DeleteIndex(pIndex); } -#ifndef SQLITE_OMIT_FOREIGN_KEY - /* Delete all foreign keys associated with this table. */ - for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){ - pNextFKey = pFKey->pNextFrom; - sqlite3DbFree(db, pFKey); - } -#endif + /* Delete any foreign keys attached to this table. */ + sqlite3FkDelete(pTable); /* Delete the Table structure itself. */ @@ -1174,7 +1168,11 @@ void sqlite3AddPrimaryKey( "INTEGER PRIMARY KEY"); #endif }else{ - sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0); + Index *p; + p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0); + if( p ){ + p->autoIndex = 2; + } pList = 0; } @@ -2153,6 +2151,7 @@ void sqlite3CreateForeignKey( sqlite3 *db = pParse->db; #ifndef SQLITE_OMIT_FOREIGN_KEY FKey *pFKey = 0; + FKey *pNextTo; Table *p = pParse->pNewTable; int nByte; int i; @@ -2231,6 +2230,16 @@ void sqlite3CreateForeignKey( pFKey->updateConf = (u8)((flags >> 8 ) & 0xff); pFKey->insertConf = (u8)((flags >> 16 ) & 0xff); + pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, + pFKey->zTo, sqlite3Strlen30(pFKey->zTo), (void *)pFKey + ); + if( pNextTo==pFKey ) goto fk_end; + if( pNextTo ){ + assert( pNextTo->pPrevTo==0 ); + pFKey->pNextTo = pNextTo; + pNextTo->pPrevTo = pFKey; + } + /* Link the foreign key to the table as the last step. */ p->pFKey = pFKey; @@ -2350,8 +2359,12 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ ** pList is a list of columns to be indexed. pList will be NULL if this ** is a primary key or unique-constraint on the most recent column added ** to the table currently under construction. +** +** If the index is created successfully, return a pointer to the new Index +** structure. This is used by sqlite3AddPrimaryKey() to mark the index +** as the tables primary key (Index.autoIndex==2). */ -void sqlite3CreateIndex( +Index *sqlite3CreateIndex( Parse *pParse, /* All information about this parse */ Token *pName1, /* First part of index name. May be NULL */ Token *pName2, /* Second part of index name. May be NULL */ @@ -2363,6 +2376,7 @@ void sqlite3CreateIndex( int sortOrder, /* Sort order of primary key when pList==NULL */ int ifNotExist /* Omit error if index already exists */ ){ + Index *pRet = 0; /* Pointer to return */ Table *pTab = 0; /* Table to be indexed */ Index *pIndex = 0; /* The index to be created */ char *zName = 0; /* Name of the index */ @@ -2798,6 +2812,7 @@ void sqlite3CreateIndex( pIndex->pNext = pOther->pNext; pOther->pNext = pIndex; } + pRet = pIndex; pIndex = 0; } @@ -2810,7 +2825,7 @@ exit_create_index: sqlite3ExprListDelete(db, pList); sqlite3SrcListDelete(db, pTblName); sqlite3DbFree(db, zName); - return; + return pRet; } /* |