From: dan Date: Thu, 1 Oct 2009 16:09:04 +0000 (+0000) Subject: If an update does not modify any child or parent key columns, omit foreign key proces... X-Git-Tag: fts3-refactor~133 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e7a94d8128bd3c5037ab66a76586a24a9f16fa31;p=thirdparty%2Fsqlite.git If an update does not modify any child or parent key columns, omit foreign key processing for the statement. FossilOrigin-Name: edff3500058eb8ad2381f855ef7a09ecb680f7b8 --- diff --git a/manifest b/manifest index 8c6234d275..ee63b30c39 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\stests\sto\scheck\sthat\sFK\ssupport\sinteracts\swith\scount-changes\scorrectly. -D 2009-10-01T04:35:06 +C If\san\supdate\sdoes\snot\smodify\sany\schild\sor\sparent\skey\scolumns,\somit\sforeign\skey\sprocessing\sfor\sthe\sstatement. +D 2009-10-01T16:09:04 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.in 4ca3f1dd6efa2075bcb27f4dc43eef749877740d F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -113,16 +113,16 @@ F src/build.c 1988dad2799d0421cd9fb6431fd3a1667115522e F src/callback.c 10d237171472865f58fb07d515737238c9e06688 F src/complete.c 5ad5c6cd4548211867c204c41a126d73a9fbcea0 F src/date.c 657ff12ca0f1195b531561afacbb38b772d16638 -F src/delete.c 2a3d6fc0861b2f8dbd9feb7847b390267b281c60 +F src/delete.c 308e300d599d2d11b838687e2cf7309d42f29a1a F src/expr.c c7f3f718bd5c392344ec8694a41c1824f30cf375 F src/fault.c dc88c821842157460750d2d61a8a8b4197d047ff -F src/fkey.c 8b438b1fda586519f8bb398538447fb2592bf668 +F src/fkey.c b20074e0dcbf6bbaafa8a520ed12efb30bb6aac3 F src/func.c e536218d193b8d326aab91120bc4c6f28aa2b606 F src/global.c 271952d199a8cc59d4ce840b3bbbfd2f30c8ba32 F src/hash.c ebcaa921ffd9d86f7ea5ae16a0a29d1c871130a7 F src/hash.h 35b216c13343d0b4f87d9f21969ac55ad72174e1 F src/hwtime.h 4a1d45f4cae1f402ea19686acf24acf4f0cb53cb -F src/insert.c 719465bf5c46349550e3bda483b1b9f0b9b6aebf +F src/insert.c 2fe2ef7bd03d6e0120e4525727c4ae7de5a2d571 F src/journal.c e00df0c0da8413ab6e1bb7d7cab5665d4a9000d0 F src/legacy.c 303b4ffcf1ae652fcf5ef635846c563c254564f6 F src/lempar.c 0c4d1ab0a5ef2b0381eb81a732c54f68f27a574d @@ -164,7 +164,7 @@ F src/select.c 1d0a13137532321b4364f964e46f057d271691e3 F src/shell.c d6e64471aafb81f355262533393169a70529847a F src/sqlite.h.in 5af8181f815831a8672c3834c60e6b4418448bcc F src/sqlite3ext.h 1db7d63ab5de4b3e6b83dd03d1a4e64fef6d2a17 -F src/sqliteInt.h 284a65c29bc4f2a77aa45994285fb55f7de537b9 +F src/sqliteInt.h 6730f9584eed76460ed85d2ade13914a6b68caab F src/sqliteLimit.h 504a3161886d2938cbd163054ad620b8356df758 F src/status.c 237b193efae0cf6ac3f0817a208de6c6c6ef6d76 F src/table.c cc86ad3d6ad54df7c63a3e807b5783c90411a08d @@ -202,7 +202,7 @@ F src/test_thread.c b8a1ab7ca1a632f18e8a361880d5d65eeea08eac F src/test_wsd.c 3ae5101de6cbfda2720152ab659ea84079719241 F src/tokenize.c af8a56e6a50c5042fc305bfa796275e9bf26ff2b F src/trigger.c 643e27f94a0112071a993f457d6c4aae24782944 -F src/update.c a7a5f60bf65a88557b434ac40f2ae6d2f7ad2d2d +F src/update.c 2c8a64237e4fae604468d14380b877d169545b63 F src/utf.c 99cf927eabb104621ba889ac0dd075fc1657ad30 F src/util.c 59d4e9456bf1fe581f415a783fa0cee6115c8f35 F src/vacuum.c 869d08eaab64e2a4eaf4ef9ea34b851892b65a75 @@ -330,8 +330,8 @@ F test/expr.test 9f521ae22f00e074959f72ce2e55d46b9ed23f68 F test/filectrl.test 8923a6dc7630f31c8a9dd3d3d740aa0922df7bf8 F test/filefmt.test 84e3d0fe9f12d0d2ac852465c6f8450aea0d6f43 F test/fkey1.test 01c7de578e11747e720c2d9aeef27f239853c4da -F test/fkey2.test 7ca63ae07839e700698655010788502b0e5d3e65 -F test/fkey3.test c17565b40c97a0dd5102610183c744611171b5ec +F test/fkey2.test 695b21b57e8e8916cd67cc232aa3d1d8dfd269df +F test/fkey3.test 42f88d6048d8dc079e2a8cf7baad1cc1483a7620 F test/fkey_malloc.test a5ede29bd2f6e56dea78c3d43fb86dd696c068c8 F test/format4.test 1f0cac8ff3895e9359ed87e41aaabee982a812eb F test/fts1a.test 46090311f85da51bb33bd5ce84f7948359c6d8d7 @@ -755,7 +755,7 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f -P 0a0a17d87c990a811a2755b9d9d4141b611b49db -R 71d9a87efb7de6c5e2fcba593dc5c2be +P 5b8366154b2a91c1be3e7e52ec270ecdfe7d2bb1 +R c95c8f6b969f3f74e7c6e66644a17c32 U dan -Z 7c4cee03a1a1dae14dd3d77db4d5358b +Z 8d8627e9a9b2426ee82543a58a4b15f8 diff --git a/manifest.uuid b/manifest.uuid index 7af80d081c..8ad9892418 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -5b8366154b2a91c1be3e7e52ec270ecdfe7d2bb1 \ No newline at end of file +edff3500058eb8ad2381f855ef7a09ecb680f7b8 \ No newline at end of file diff --git a/src/delete.c b/src/delete.c index d93ee36a02..9904d98d03 100644 --- a/src/delete.c +++ b/src/delete.c @@ -342,7 +342,7 @@ void sqlite3DeleteFrom( ** this optimization caused the row change count (the value returned by ** API function sqlite3_count_changes) to be set incorrectly. */ if( rcauth==SQLITE_OK && pWhere==0 && !pTrigger && !IsVirtual(pTab) - && 0==sqlite3FkRequired(pParse, pTab, 0) + && 0==sqlite3FkRequired(pParse, pTab, 0, 0) ){ assert( !isView ); sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt, @@ -492,14 +492,14 @@ void sqlite3GenerateRowDelete( /* If there are any triggers to fire, allocate a range of registers to ** use for the old.* references in the triggers. */ - if( sqlite3FkRequired(pParse, pTab, 0) || pTrigger ){ + if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){ u32 mask; /* Mask of OLD.* columns in use */ int iCol; /* Iterator used while populating OLD.* */ /* TODO: Could use temporary registers here. Also could attempt to ** avoid copying the contents of the rowid register. */ mask = sqlite3TriggerOldmask(pParse, pTrigger, 0, pTab, onconf); - mask |= sqlite3FkOldmask(pParse, pTab, 0); + mask |= sqlite3FkOldmask(pParse, pTab); iOld = pParse->nMem+1; pParse->nMem += (1 + pTab->nCol); @@ -528,7 +528,7 @@ void sqlite3GenerateRowDelete( /* Do FK processing. This call checks that any FK constraints that ** refer to this table (i.e. constraints attached to other tables) ** are not violated by deleting this row. */ - sqlite3FkCheck(pParse, pTab, 0, iOld, 0); + sqlite3FkCheck(pParse, pTab, iOld, 0); } /* Delete the index and table entries. Skip this step if pTab is really diff --git a/src/fkey.c b/src/fkey.c index 7ec256d7bc..4d6a32ad69 100644 --- a/src/fkey.c +++ b/src/fkey.c @@ -672,14 +672,11 @@ void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){ ** For an UPDATE operation, this function is called twice. Once before ** the original record is deleted from the table using the calling convention ** described for DELETE. Then again after the original record is deleted -** but before the new record is inserted using the INSERT convention. In -** both cases parameter pChanges is passed the list of columns being -** updated by the statement. +** but before the new record is inserted using the INSERT convention. */ void sqlite3FkCheck( Parse *pParse, /* Parse context */ Table *pTab, /* Row is being deleted from this table */ - ExprList *pChanges, /* Changed columns if this is an UPDATE */ int regOld, /* Previous row data is stored here */ int regNew /* New row data is stored here */ ){ @@ -725,11 +722,6 @@ void sqlite3FkCheck( } assert( pFKey->nCol==1 || (aiFree && pIdx) ); - /* If the key does not overlap with the pChanges list, skip this FK. */ - if( pChanges ){ - /* TODO */ - } - if( aiFree ){ aiCol = aiFree; }else{ @@ -782,14 +774,6 @@ void sqlite3FkCheck( } assert( aiCol || pFKey->nCol==1 ); - /* Check if this update statement has modified any of the child key - ** columns for this foreign key constraint. If it has not, there is - ** no need to search the child table for rows in violation. This is - ** just an optimization. Things would work fine without this check. */ - if( pChanges ){ - /* TODO */ - } - /* Create a SrcList structure containing a single table (the table ** the foreign key that refers to this table is attached to). This ** is required for the sqlite3WhereXXX() interface. */ @@ -822,14 +806,11 @@ void sqlite3FkCheck( /* ** This function is called before generating code to update or delete a -** row contained in table pTab. If the operation is an update, then -** pChanges is a pointer to the list of columns to modify. If this is a -** delete, then pChanges is NULL. +** row contained in table pTab. */ u32 sqlite3FkOldmask( Parse *pParse, /* Parse context */ - Table *pTab, /* Table being modified */ - ExprList *pChanges /* Non-NULL for UPDATE operations */ + Table *pTab /* Table being modified */ ){ u32 mask = 0; if( pParse->db->flags&SQLITE_ForeignKeys ){ @@ -851,9 +832,13 @@ u32 sqlite3FkOldmask( /* ** This function is called before generating code to update or delete a -** row contained in table pTab. If the operation is an update, then -** pChanges is a pointer to the list of columns to modify. If this is a -** delete, then pChanges is NULL. +** row contained in table pTab. If the operation is a DELETE, then +** parameter aChange is passed a NULL value. For an UPDATE, aChange points +** to an array of size N, where N is the number of columns in table pTab. +** If the i'th column is not modified by the UPDATE, then the corresponding +** entry in the aChange[] array is set to -1. If the column is modified, +** the value is 0 or greater. Parameter chngRowid is set to true if the +** UPDATE statement modifies the rowid fields of the table. ** ** If any foreign key processing will be required, this function returns ** true. If there is no foreign key related processing, this function @@ -862,10 +847,45 @@ u32 sqlite3FkOldmask( int sqlite3FkRequired( Parse *pParse, /* Parse context */ Table *pTab, /* Table being modified */ - ExprList *pChanges /* Non-NULL for UPDATE operations */ + int *aChange, /* Non-NULL for UPDATE operations */ + int chngRowid /* True for UPDATE that affects rowid */ ){ if( pParse->db->flags&SQLITE_ForeignKeys ){ - if( sqlite3FkReferences(pTab) || pTab->pFKey ) return 1; + if( !aChange ){ + /* A DELETE operation. Foreign key processing is required if the + ** table in question is either the child or parent table for any + ** foreign key constraint. */ + return (sqlite3FkReferences(pTab) || pTab->pFKey); + }else{ + /* This is an UPDATE. Foreign key processing is only required if the + ** operation modifies one or more child or parent key columns. */ + int i; + FKey *p; + + /* Check if any child key columns are being modified. */ + for(p=pTab->pFKey; p; p=p->pNextFrom){ + for(i=0; inCol; i++){ + int iChildKey = p->aCol[i].iFrom; + if( aChange[iChildKey]>=0 ) return 1; + if( iChildKey==pTab->iPKey && chngRowid ) return 1; + } + } + + /* Check if any parent key columns are being modified. */ + for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){ + for(i=0; inCol; i++){ + char *zKey = p->aCol[i].zCol; + int iKey; + for(iKey=0; iKeynCol; iKey++){ + Column *pCol = &pTab->aCol[iKey]; + if( (zKey ? !sqlite3StrICmp(pCol->zName, zKey) : pCol->isPrimKey) ){ + if( aChange[iKey]>=0 ) return 1; + if( iKey==pTab->iPKey && chngRowid ) return 1; + } + } + } + } + } } return 0; } diff --git a/src/insert.c b/src/insert.c index d714074203..94b741af9f 100644 --- a/src/insert.c +++ b/src/insert.c @@ -979,7 +979,7 @@ void sqlite3Insert( sqlite3GenerateConstraintChecks(pParse, pTab, baseCur, regIns, aRegIdx, keyColumn>=0, 0, onError, endOfLoop, &isReplace ); - sqlite3FkCheck(pParse, pTab, 0, 0, regIns); + sqlite3FkCheck(pParse, pTab, 0, regIns); sqlite3CompleteInsertion( pParse, pTab, baseCur, regIns, aRegIdx, 0, appendFlag, isReplace==0 ); @@ -1271,7 +1271,7 @@ void sqlite3GenerateConstraintChecks( pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); } sqlite3MultiWrite(pParse); - if( pTrigger || sqlite3FkRequired(pParse, pTab, 0) ){ + if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){ sqlite3GenerateRowDelete( pParse, pTab, baseCur, regRowid, 0, pTrigger, OE_Replace ); diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 2ae1ec8081..f97a81ca64 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -2949,18 +2949,18 @@ VTable *sqlite3GetVTable(sqlite3*, Table*); ** provided (enforcement of FK constraints requires the triggers sub-system). */ #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) - void sqlite3FkCheck(Parse*, Table*, ExprList*, int, int); + void sqlite3FkCheck(Parse*, Table*, int, int); void sqlite3FkDropTable(Parse*, SrcList *, Table*); void sqlite3FkActions(Parse*, Table*, ExprList*, int); - int sqlite3FkRequired(Parse*, Table*, ExprList*); - u32 sqlite3FkOldmask(Parse*, Table*, ExprList*); + int sqlite3FkRequired(Parse*, Table*, int*, int); + u32 sqlite3FkOldmask(Parse*, Table*); FKey *sqlite3FkReferences(Table *); #else #define sqlite3FkActions(a,b,c,d) - #define sqlite3FkCheck(a,b,c,d,e) + #define sqlite3FkCheck(a,b,c,d) #define sqlite3FkDropTable(a,b,c) - #define sqlite3FkOldmask(a,b,c) 0 - #define sqlite3FkRequired(a,b,c) 0 + #define sqlite3FkOldmask(a,b) 0 + #define sqlite3FkRequired(a,b,c,d) 0 #endif #ifndef SQLITE_OMIT_FOREIGN_KEY void sqlite3FkDelete(Table*); diff --git a/src/update.c b/src/update.c index f443dbc492..3703c1b59c 100644 --- a/src/update.c +++ b/src/update.c @@ -159,8 +159,6 @@ void sqlite3Update( # define isView 0 #endif - hasFK = sqlite3FkRequired(pParse, pTab, pChanges); - if( sqlite3ViewGetColumnNames(pParse, pTab) ){ goto update_cleanup; } @@ -230,6 +228,8 @@ void sqlite3Update( #endif } + hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngRowid); + /* Allocate memory for the array aRegIdx[]. There is one entry in the ** array for each index associated with table being updated. Fill in ** the value with a register number for indices that are to be used @@ -389,7 +389,7 @@ void sqlite3Update( /* If there are triggers on this table, populate an array of registers ** with the required old.* column data. */ if( hasFK || pTrigger ){ - u32 oldmask = sqlite3FkOldmask(pParse, pTab, pChanges); + u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0); oldmask |= sqlite3TriggerOldmask(pParse, pTrigger, pChanges, pTab, onError); for(i=0; inCol; i++){ if( aXRef[i]<0 || oldmask==0xffffffff || (oldmask & (1<