1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-16 23:02:26 +03:00

Check in implementation of foreign key constraints.

FossilOrigin-Name: d5d399811876391642937edeb9e8434dd9e356f5
This commit is contained in:
dan
2009-09-19 17:00:31 +00:00
parent 3991bb0dee
commit 1da40a381f
27 changed files with 1698 additions and 182 deletions

View File

@@ -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;
}
/*