Index: src/build.c ================================================================== --- src/build.c +++ src/build.c @@ -23,11 +23,11 @@ ** BEGIN TRANSACTION ** COMMIT ** ROLLBACK ** PRAGMA ** -** $Id: build.c,v 1.90 2002/05/15 12:45:43 drh Exp $ +** $Id: build.c,v 1.91 2002/05/19 23:43:14 danielk1977 Exp $ */ #include "sqliteInt.h" #include /* @@ -257,11 +257,11 @@ pTrigger->isCommit = 1; } /* Delete the structures for triggers removed this transaction */ pElem = sqliteHashFirst(&db->trigDrop); - while (pElem) { + while( pElem ){ Trigger *pTrigger = sqliteHashData(pElem); sqliteDeleteTrigger(pTrigger); pElem = sqliteHashNext(pElem); } sqliteHashClear(&db->trigDrop); @@ -321,21 +321,21 @@ } sqliteHashClear(&db->idxDrop); /* Remove any triggers that haven't been commited yet */ for(pElem = sqliteHashFirst(&db->trigHash); pElem; - pElem = (pElem?sqliteHashNext(pElem):0)) { + pElem = (pElem?sqliteHashNext(pElem):0)){ Trigger *pTrigger = sqliteHashData(pElem); if( !pTrigger->isCommit ){ Table *pTbl = sqliteFindTable(db, pTrigger->table); if( pTbl ){ if( pTbl->pTrigger == pTrigger ){ pTbl->pTrigger = pTrigger->pNext; }else{ Trigger *cc = pTbl->pTrigger; while( cc ){ - if (cc->pNext == pTrigger) { + if( cc->pNext == pTrigger ){ cc->pNext = cc->pNext->pNext; break; } cc = cc->pNext; } @@ -349,17 +349,17 @@ } } /* Any triggers that were dropped - put 'em back in place */ for(pElem = sqliteHashFirst(&db->trigDrop); pElem; - pElem = sqliteHashNext(pElem)) { - Trigger * pTrigger = sqliteHashData(pElem); - Table * tab = sqliteFindTable(db, pTrigger->table); + pElem = sqliteHashNext(pElem)){ + Trigger *pTrigger = sqliteHashData(pElem); + Table *pTbl = sqliteFindTable(db, pTrigger->table); sqliteHashInsert(&db->trigHash, pTrigger->name, strlen(pTrigger->name) + 1, pTrigger); - pTrigger->pNext = tab->pTrigger; - tab->pTrigger = pTrigger; + pTrigger->pNext = pTbl->pTrigger; + pTbl->pTrigger = pTrigger; } sqliteHashClear(&db->trigDrop); db->flags &= ~SQLITE_InternChanges; } @@ -1753,11 +1753,11 @@ ** If a transaction was started, then commit it. If a checkpoint was ** started then commit that. */ void sqliteEndWriteOperation(Parse *pParse){ Vdbe *v; - if (pParse->trigStack) return; /* if this is in a trigger */ + if( pParse->trigStack ) return; /* if this is in a trigger */ v = sqliteGetVdbe(pParse); if( v==0 ) return; if( pParse->db->flags & SQLITE_InTrans ){ /* Do Nothing */ }else{ Index: src/delete.c ================================================================== --- src/delete.c +++ src/delete.c @@ -10,11 +10,11 @@ ** ************************************************************************* ** This file contains C code routines that are called by the parser ** to handle DELETE FROM statements. ** -** $Id: delete.c,v 1.32 2002/05/15 11:44:14 drh Exp $ +** $Id: delete.c,v 1.33 2002/05/19 23:43:14 danielk1977 Exp $ */ #include "sqliteInt.h" /* @@ -92,27 +92,27 @@ goto delete_from_cleanup; } db = pParse->db; /* Check for the special case of a VIEW with one or more ON DELETE triggers - * defined - */ + ** defined + */ { - Table * pTab; - char * zTab = sqliteTableNameFromToken(pTableName); + Table *pTab; + char *zTab = sqliteTableNameFromToken(pTableName); - if(zTab != 0) { + if( zTab != 0 ){ pTab = sqliteFindTable(pParse->db, zTab); - if (pTab) { + if( pTab ){ row_triggers_exist = sqliteTriggersExist(pParse, pTab->pTrigger, TK_DELETE, TK_BEFORE, TK_ROW, 0) || sqliteTriggersExist(pParse, pTab->pTrigger, TK_DELETE, TK_AFTER, TK_ROW, 0); } sqliteFree(zTab); - if (row_triggers_exist && pTab->pSelect ) { + if( row_triggers_exist && pTab->pSelect ){ /* Just fire VIEW triggers */ sqliteViewTriggers(pParse, pTab, pWhere, OE_Replace, 0); return; } } @@ -127,12 +127,13 @@ if( pTabList==0 ) goto delete_from_cleanup; assert( pTabList->nId==1 ); pTab = pTabList->a[0].pTab; assert( pTab->pSelect==0 ); /* This table is not a view */ - if (row_triggers_exist) + if( row_triggers_exist ){ oldIdx = pParse->nTab++; + } /* Resolve the column names in all the expressions. */ base = pParse->nTab++; if( pWhere ){ @@ -145,15 +146,18 @@ } /* Begin generating code. */ v = sqliteGetVdbe(pParse); - if( v==0 ) goto delete_from_cleanup; - if (row_triggers_exist) + if( v==0 ){ + goto delete_from_cleanup; + } + if( row_triggers_exist ){ sqliteBeginMultiWriteOperation(pParse); - else + } else { sqliteBeginWriteOperation(pParse); + } /* Initialize the counter of the number of rows deleted, if ** we are counting rows. */ if( db->flags & SQLITE_CountRows ){ @@ -161,11 +165,11 @@ } /* Special case: A DELETE without a WHERE clause deletes everything. ** It is easier just to erase the whole table. */ - if( pWhere==0 && !row_triggers_exist){ + if( pWhere==0 && !row_triggers_exist ){ if( db->flags & SQLITE_CountRows ){ /* If counting rows deleted, just count the total number of ** entries in the table. */ int endOfLoop = sqliteVdbeMakeLabel(v); int addr; @@ -209,11 +213,11 @@ ** because deleting an item can change the scan order. */ sqliteVdbeAddOp(v, OP_ListRewind, 0, 0); end = sqliteVdbeMakeLabel(v); - if (row_triggers_exist) { + if( row_triggers_exist ){ int ii; addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end); sqliteVdbeAddOp(v, OP_Dup, 0, 0); openOp = pTab->isTemp ? OP_OpenAux : OP_Open; @@ -220,15 +224,16 @@ sqliteVdbeAddOp(v, openOp, base, pTab->tnum); sqliteVdbeAddOp(v, OP_MoveTo, base, 0); sqliteVdbeAddOp(v, OP_OpenTemp, oldIdx, 0); sqliteVdbeAddOp(v, OP_Integer, 13, 0); - for (ii = 0; ii < pTab->nCol; ii++) { - if (ii == pTab->iPKey) + for(ii = 0; iinCol; ii++){ + if( ii==pTab->iPKey ){ sqliteVdbeAddOp(v, OP_Recno, base, 0); - else + } else { sqliteVdbeAddOp(v, OP_Column, base, ii); + } } sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); sqliteVdbeAddOp(v, OP_PutIntKey, oldIdx, 0); sqliteVdbeAddOp(v, OP_Close, base, 0); sqliteVdbeAddOp(v, OP_Rewind, oldIdx, 0); @@ -242,16 +247,17 @@ sqliteVdbeAddOp(v, openOp, base, pTab->tnum); for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ sqliteVdbeAddOp(v, openOp, pParse->nTab++, pIdx->tnum); } - if (!row_triggers_exist) + if( !row_triggers_exist ){ addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end); + } sqliteGenerateRowDelete(v, pTab, base, pParse->trigStack?0:1); - if (row_triggers_exist) { + if( row_triggers_exist ){ for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ sqliteVdbeAddOp(v, OP_Close, base + i, pIdx->tnum); } sqliteVdbeAddOp(v, OP_Close, base, 0); sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_AFTER, pTab, -1, @@ -260,11 +266,11 @@ sqliteVdbeAddOp(v, OP_Goto, 0, addr); sqliteVdbeResolveLabel(v, end); sqliteVdbeAddOp(v, OP_ListReset, 0, 0); - if (!row_triggers_exist) { + if( !row_triggers_exist ){ for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ sqliteVdbeAddOp(v, OP_Close, base + i, pIdx->tnum); } sqliteVdbeAddOp(v, OP_Close, base, 0); pParse->nTab = base; Index: src/expr.c ================================================================== --- src/expr.c +++ src/expr.c @@ -10,11 +10,11 @@ ** ************************************************************************* ** This file contains routines used for analyzing expressions and ** for generating VDBE code that evaluates expressions in SQLite. ** -** $Id: expr.c,v 1.59 2002/05/15 08:30:13 danielk1977 Exp $ +** $Id: expr.c,v 1.60 2002/05/19 23:43:14 danielk1977 Exp $ */ #include "sqliteInt.h" /* @@ -482,32 +482,33 @@ } } /* If we have not already resolved this *.* expression, then maybe * it is a new.* or old.* trigger argument reference */ - if (cnt == 0 && pParse->trigStack != 0) { - TriggerStack * tt = pParse->trigStack; - int j; + if( cnt == 0 && pParse->trigStack != 0 ){ + TriggerStack *pTriggerStack = pParse->trigStack; int t = 0; - if (tt->newIdx != -1 && sqliteStrICmp("new", zLeft) == 0) { - pExpr->iTable = tt->newIdx; + if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zLeft) == 0 ){ + pExpr->iTable = pTriggerStack->newIdx; cntTab++; t = 1; } - if (tt->oldIdx != -1 && sqliteStrICmp("old", zLeft) == 0) { - pExpr->iTable = tt->oldIdx; + if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zLeft) == 0 ){ + pExpr->iTable = pTriggerStack->oldIdx; cntTab++; t = 1; } - if (t) - for(j=0; jpTab->nCol; j++) { - if( sqliteStrICmp(tt->pTab->aCol[j].zName, zRight)==0 ){ + if( t ){ + int j; + for(j=0; j < pTriggerStack->pTab->nCol; j++) { + if( sqliteStrICmp(pTriggerStack->pTab->aCol[j].zName, zRight)==0 ){ cnt++; pExpr->iColumn = j; } } + } } if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){ cnt = 1; pExpr->iColumn = -1; Index: src/insert.c ================================================================== --- src/insert.c +++ src/insert.c @@ -10,11 +10,11 @@ ** ************************************************************************* ** This file contains C code routines that are called by the parser ** to handle INSERT statements in SQLite. ** -** $Id: insert.c,v 1.54 2002/05/15 11:44:14 drh Exp $ +** $Id: insert.c,v 1.55 2002/05/19 23:43:14 danielk1977 Exp $ */ #include "sqliteInt.h" /* ** This routine is call to handle SQL of the following forms: @@ -100,12 +100,13 @@ }else{ sqliteBeginWriteOperation(pParse); } /* if there are row triggers, allocate a temp table for new.* references. */ - if (row_triggers_exist) + if( row_triggers_exist ){ newIdx = pParse->nTab++; + } /* Figure out how many columns of data are supplied. If the data ** is coming from a SELECT statement, then this step has to generate ** all the code to implement the SELECT statement and leave the data ** in a temporary table. If data is coming from an expression list, @@ -202,21 +203,22 @@ if( pColumn==0 ){ keyColumn = pTab->iPKey; } /* Open the temp table for FOR EACH ROW triggers */ - if (row_triggers_exist) + if( row_triggers_exist ){ sqliteVdbeAddOp(v, OP_OpenTemp, newIdx, 0); + } /* Initialize the count of rows to be inserted */ - if( db->flags & SQLITE_CountRows && !pParse->trigStack){ + if( db->flags & SQLITE_CountRows && !pParse->trigStack ){ sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */ } /* Open tables and indices if there are no row triggers */ - if (!row_triggers_exist) { + if( !row_triggers_exist ){ base = pParse->nTab; openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite; sqliteVdbeAddOp(v, openOp, base, pTab->tnum); sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ @@ -235,11 +237,11 @@ iBreak = sqliteVdbeMakeLabel(v); sqliteVdbeAddOp(v, OP_Rewind, srcTab, iBreak); iCont = sqliteVdbeCurrentAddr(v); } - if (row_triggers_exist) { + if( row_triggers_exist ){ /* build the new.* reference row */ sqliteVdbeAddOp(v, OP_Integer, 13, 0); for(i=0; inCol; i++){ if( pColumn==0 ){ @@ -261,17 +263,17 @@ sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); sqliteVdbeAddOp(v, OP_PutIntKey, newIdx, 0); sqliteVdbeAddOp(v, OP_Rewind, newIdx, 0); /* Fire BEFORE triggers */ - if ( - sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_BEFORE, pTab, newIdx, -1, - onError) - ) goto insert_cleanup; + if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_BEFORE, pTab, newIdx, -1, + onError) ){ + goto insert_cleanup; + } /* Open the tables and indices for the INSERT */ - if (!pTab->pSelect) { + if( !pTab->pSelect ){ base = pParse->nTab; openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite; sqliteVdbeAddOp(v, openOp, base, pTab->tnum); sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ @@ -285,21 +287,21 @@ /* Push the record number for the new entry onto the stack. The ** record number is a randomly generate integer created by NewRecno ** except when the table has an INTEGER PRIMARY KEY column, in which ** case the record number is the same as that column. */ - if (!pTab->pSelect) { + if( !pTab->pSelect ){ if( keyColumn>=0 ){ if( srcTab>=0 ){ sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn); }else{ int addr; sqliteExprCode(pParse, pList->a[keyColumn].pExpr); /* If the PRIMARY KEY expression is NULL, then use OP_NewRecno - ** to generate a unique primary key value. - */ + ** to generate a unique primary key value. + */ addr = sqliteVdbeAddOp(v, OP_Dup, 0, 1); sqliteVdbeAddOp(v, OP_NotNull, 0, addr+4); sqliteVdbeAddOp(v, OP_Pop, 1, 0); sqliteVdbeAddOp(v, OP_NewRecno, base, 0); } @@ -307,18 +309,18 @@ }else{ sqliteVdbeAddOp(v, OP_NewRecno, base, 0); } /* Push onto the stack, data for all columns of the new entry, beginning - ** with the first column. - */ + ** with the first column. + */ for(i=0; inCol; i++){ if( i==pTab->iPKey ){ /* The value of the INTEGER PRIMARY KEY column is always a NULL. - ** Whenever this column is read, the record number will be substituted - ** in its place. So will fill this column with a NULL to avoid - ** taking up data space with information that will never be used. */ + ** Whenever this column is read, the record number will be substituted + ** in its place. So will fill this column with a NULL to avoid + ** taking up data space with information that will never be used. */ sqliteVdbeAddOp(v, OP_String, 0, 0); continue; } if( pColumn==0 ){ j = i; @@ -336,49 +338,49 @@ sqliteExprCode(pParse, pList->a[j].pExpr); } } /* Generate code to check constraints and generate index keys and - ** do the insertion. - */ + ** do the insertion. + */ endOfLoop = sqliteVdbeMakeLabel(v); sqliteGenerateConstraintChecks(pParse, pTab, base, 0,0,0,onError,endOfLoop); sqliteCompleteInsertion(pParse, pTab, base, 0,0,0); /* Update the count of rows that are inserted - */ + */ if( (db->flags & SQLITE_CountRows)!=0 && !pParse->trigStack){ sqliteVdbeAddOp(v, OP_AddImm, 1, 0); } } - if (row_triggers_exist) { + if( row_triggers_exist ){ /* Close all tables opened */ - if (!pTab->pSelect) { + if( !pTab->pSelect ){ sqliteVdbeAddOp(v, OP_Close, base, 0); for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ sqliteVdbeAddOp(v, OP_Close, idx+base, 0); } } /* Code AFTER triggers */ - if ( - sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_AFTER, pTab, newIdx, -1, - onError) - ) goto insert_cleanup; + if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_AFTER, pTab, newIdx, -1, + onError) ){ + goto insert_cleanup; + } } /* The bottom of the loop, if the data source is a SELECT statement - */ + */ sqliteVdbeResolveLabel(v, endOfLoop); if( srcTab>=0 ){ sqliteVdbeAddOp(v, OP_Next, srcTab, iCont); sqliteVdbeResolveLabel(v, iBreak); sqliteVdbeAddOp(v, OP_Close, srcTab, 0); } - if (!row_triggers_exist) { + if( !row_triggers_exist ){ /* Close all tables opened */ sqliteVdbeAddOp(v, OP_Close, base, 0); for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ sqliteVdbeAddOp(v, OP_Close, idx+base, 0); } @@ -385,11 +387,11 @@ } sqliteEndWriteOperation(pParse); /* - ** Return the number of rows inserted. + ** Return the number of rows inserted. */ if( db->flags & SQLITE_CountRows && !pParse->trigStack ){ sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0); sqliteVdbeAddOp(v, OP_ColumnName, 0, 0); sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC); Index: src/main.c ================================================================== --- src/main.c +++ src/main.c @@ -12,11 +12,11 @@ ** Main file for the SQLite library. The routines in this file ** implement the programmer interface to the library. Routines in ** other files are for internal use by SQLite and should not be ** accessed by users of the library. ** -** $Id: main.c,v 1.74 2002/05/15 14:17:45 drh Exp $ +** $Id: main.c,v 1.75 2002/05/19 23:43:14 danielk1977 Exp $ */ #include "sqliteInt.h" #include "os.h" #include @@ -392,18 +392,18 @@ temp1 = db->tblHash; temp2 = db->trigHash; sqliteHashInit(&db->trigHash, SQLITE_HASH_STRING, 0); sqliteHashClear(&db->idxHash); - for (pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){ + for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){ Trigger * pTrigger = sqliteHashData(pElem); Table *pTab = sqliteFindTable(db, pTrigger->table); assert(pTab); - if (pTab->isTemp) { + if( pTab->isTemp ){ sqliteHashInsert(&db->trigHash, pTrigger->name, strlen(pTrigger->name), pTrigger); - } else { + }else{ sqliteDeleteTrigger(pTrigger); } } sqliteHashClear(&temp2); @@ -432,11 +432,10 @@ }else{ sqliteDeleteTable(db, pTab); } } sqliteHashClear(&temp1); - db->flags &= ~SQLITE_Initialized; } /* ** Return the ROWID of the most recent insert Index: src/trigger.c ================================================================== --- src/trigger.c +++ src/trigger.c @@ -78,13 +78,15 @@ offset = (int)(nt->strings - zData); sqliteExprMoveStrings(nt->pWhen, offset); ss = nt->step_list; - while (ss) { + while( ss ){ sqliteSelectMoveStrings(ss->pSelect, offset); - if (ss->target.z) ss->target.z += offset; + if( ss->target.z ){ + ss->target.z += offset; + } sqliteExprMoveStrings(ss->pWhere, offset); sqliteExprListMoveStrings(ss->pExprList, offset); ss = ss->pNext; } @@ -128,11 +130,11 @@ /* Attach it to the table object */ nt->pNext = tab->pTrigger; tab->pTrigger = nt; return; - } else { + }else{ sqliteFree(nt->strings); sqliteFree(nt->name); sqliteFree(nt->table); sqliteFree(nt); } @@ -144,11 +146,11 @@ { TriggerStep * pp; TriggerStep * nn; pp = pStepList; - while (pp) { + while( pp ){ nn = pp->pNext; sqliteExprDelete(pp->pWhere); sqliteExprListDelete(pp->pExprList); sqliteSelectDelete(pp->pSelect); sqliteIdListDelete(pp->pIdList); @@ -226,11 +228,11 @@ void sqliteDeleteTrigger(Trigger *pTrigger) { TriggerStep *pTriggerStep; pTriggerStep = pTrigger->step_list; - while (pTriggerStep) { + while( pTriggerStep ){ TriggerStep * pTmp = pTriggerStep; pTriggerStep = pTriggerStep->pNext; sqliteExprDelete(pTmp->pWhere); sqliteExprListDelete(pTmp->pExprList); @@ -285,11 +287,11 @@ /* 1 */ pTable = sqliteFindTable(pParse->db, pTrigger->table); assert(pTable); if( pTable->pTrigger == pTrigger ){ pTable->pTrigger = pTrigger->pNext; - } else { + }else{ Trigger *cc = pTable->pTrigger; while( cc ){ if( cc->pNext == pTrigger ){ cc->pNext = cc->pNext->pNext; break; @@ -344,17 +346,20 @@ } static int checkColumnOverLap(IdList * pIdList, ExprList * pEList) { int i, e; - if (!pIdList) return 1; - if (!pEList) return 1; + if( !pIdList )return 1; + if( !pEList )return 1; - for (i = 0; i < pIdList->nId; i++) - for (e = 0; e < pEList->nExpr; e++) - if (!sqliteStrICmp(pIdList->a[i].zName, pEList->a[e].zName)) + for(i = 0; i < pIdList->nId; i++){ + for(e = 0; e < pEList->nExpr; e++){ + if( !sqliteStrICmp(pIdList->a[i].zName, pEList->a[e].zName) ){ return 1; + } + } + } return 0; } /* A global variable that is TRUE if we should always set up temp tables for @@ -392,12 +397,14 @@ pTriggerCursor->tr_tm == tr_tm && pTriggerCursor->foreach == foreach && checkColumnOverLap(pTriggerCursor->pColumns, pChanges) ){ TriggerStack * ss; ss = pParse->trigStack; - while (ss && ss->pTrigger != pTrigger) ss = ss->pNext; - if (!ss) return 1; + while( ss && ss->pTrigger != pTrigger ){ + ss = ss->pNext; + } + if( !ss )return 1; } pTriggerCursor = pTriggerCursor->pNext; } return 0; @@ -491,28 +498,31 @@ assert(tr_tm == TK_BEFORE || tr_tm == TK_AFTER); assert(newIdx != -1 || oldIdx != -1); pTrigger = pTab->pTrigger; - while (pTrigger) { + while( pTrigger ){ int fire_this = 0; /* determine whether we should code this trigger */ - if (pTrigger->op == op && pTrigger->tr_tm == tr_tm && - pTrigger->foreach == TK_ROW) { + if( pTrigger->op == op && pTrigger->tr_tm == tr_tm && + pTrigger->foreach == TK_ROW ){ fire_this = 1; pTriggerStack = pParse->trigStack; - while (pTriggerStack) { - if (pTriggerStack->pTrigger == pTrigger) fire_this = 0; + while( pTriggerStack ){ + if( pTriggerStack->pTrigger == pTrigger ){ + fire_this = 0; + } pTriggerStack = pTriggerStack->pNext; } - if (op == TK_UPDATE && pTrigger->pColumns && - !checkColumnOverLap(pTrigger->pColumns, pChanges)) + if( op == TK_UPDATE && pTrigger->pColumns && + !checkColumnOverLap(pTrigger->pColumns, pChanges) ){ fire_this = 0; + } } - if (fire_this) { + if( fire_this ){ int endTrigger; IdList dummyTablist; Expr * whenExpr; dummyTablist.nId = 0; @@ -528,11 +538,11 @@ pParse->trigStack = pTriggerStack; /* code the WHEN clause */ endTrigger = sqliteVdbeMakeLabel(pParse->pVdbe); whenExpr = sqliteExprDup(pTrigger->pWhen); - if (sqliteExprResolveIds(pParse, 0, &dummyTablist, 0, whenExpr)) { + if( sqliteExprResolveIds(pParse, 0, &dummyTablist, 0, whenExpr) ){ pParse->trigStack = pParse->trigStack->pNext; sqliteFree(pTriggerStack); sqliteExprDelete(whenExpr); return 1; } @@ -634,12 +644,13 @@ } for(ii=0; iinExpr; ii++){ int jj; if( sqliteExprResolveIds(pParse, oldIdx, theSelect.pSrc , 0, - pChanges->a[ii].pExpr) ) + pChanges->a[ii].pExpr) ){ goto trigger_cleanup; + } if( sqliteExprCheck(pParse, pChanges->a[ii].pExpr, 0, 0) ) goto trigger_cleanup; for(jj=0; jjnCol; jj++){ @@ -659,11 +670,11 @@ sqliteVdbeAddOp(v, OP_Integer, 13, 0); for(ii = 0; iinCol; ii++){ if( aXRef[ii] < 0 ){ sqliteVdbeAddOp(v, OP_Column, oldIdx, ii); - } else { + }else{ sqliteExprCode(pParse, pChanges->a[aXRef[ii]].pExpr); } } sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); @@ -672,11 +683,11 @@ sqliteCodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_BEFORE, pTab, newIdx, oldIdx, orconf); sqliteCodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_AFTER, pTab, newIdx, oldIdx, orconf); - } else { + }else{ sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_BEFORE, pTab, -1, oldIdx, orconf); sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_AFTER, pTab, -1, oldIdx, orconf); } Index: src/update.c ================================================================== --- src/update.c +++ src/update.c @@ -10,11 +10,11 @@ ** ************************************************************************* ** This file contains C code routines that are called by the parser ** to handle UPDATE statements. ** -** $Id: update.c,v 1.38 2002/05/15 11:44:15 drh Exp $ +** $Id: update.c,v 1.39 2002/05/19 23:43:14 danielk1977 Exp $ */ #include "sqliteInt.h" /* ** Process an UPDATE statement. @@ -57,23 +57,23 @@ /* Check for the special case of a VIEW with one or more ON UPDATE triggers * defined */ { - char * zTab = sqliteTableNameFromToken(pTableName); + char *zTab = sqliteTableNameFromToken(pTableName); - if(zTab != 0) { + if( zTab != 0 ){ pTab = sqliteFindTable(pParse->db, zTab); - if (pTab) { + if( pTab ){ row_triggers_exist = sqliteTriggersExist(pParse, pTab->pTrigger, TK_UPDATE, TK_BEFORE, TK_ROW, pChanges) || sqliteTriggersExist(pParse, pTab->pTrigger, TK_UPDATE, TK_AFTER, TK_ROW, pChanges); } sqliteFree(zTab); - if (row_triggers_exist && pTab->pSelect ) { + if( row_triggers_exist && pTab->pSelect ){ /* Just fire VIEW triggers */ sqliteViewTriggers(pParse, pTab, pWhere, onError, pChanges); return; } } @@ -90,11 +90,12 @@ assert( pTab->pSelect==0 ); /* This table is not a VIEW */ aXRef = sqliteMalloc( sizeof(int) * pTab->nCol ); if( aXRef==0 ) goto update_cleanup; for(i=0; inCol; i++) aXRef[i] = -1; - if (row_triggers_exist) { + /* If there are FOR EACH ROW triggers, allocate temp tables */ + if( row_triggers_exist ){ newIdx = pParse->nTab++; oldIdx = pParse->nTab++; } /* Resolve the column names in all the expressions in both the @@ -195,11 +196,11 @@ */ if( db->flags & SQLITE_CountRows && !pParse->trigStack ){ sqliteVdbeAddOp(v, OP_Integer, 0, 0); } - if (row_triggers_exist) { + if( row_triggers_exist ){ int ii; sqliteVdbeAddOp(v, OP_OpenTemp, oldIdx, 0); sqliteVdbeAddOp(v, OP_OpenTemp, newIdx, 0); @@ -210,26 +211,28 @@ sqliteVdbeAddOp(v, OP_Dup, 0, 0); sqliteVdbeAddOp(v, OP_Open, base, pTab->tnum); sqliteVdbeAddOp(v, OP_MoveTo, base, 0); sqliteVdbeAddOp(v, OP_Integer, 13, 0); - for (ii = 0; ii < pTab->nCol; ii++) { - if (ii == pTab->iPKey) - sqliteVdbeAddOp(v, OP_Recno, base, 0); - else - sqliteVdbeAddOp(v, OP_Column, base, ii); + for(ii = 0; ii < pTab->nCol; ii++){ + if( ii == pTab->iPKey ){ + sqliteVdbeAddOp(v, OP_Recno, base, 0); + }else{ + sqliteVdbeAddOp(v, OP_Column, base, ii); + } } sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); sqliteVdbeAddOp(v, OP_PutIntKey, oldIdx, 0); sqliteVdbeAddOp(v, OP_Integer, 13, 0); - for (ii = 0; ii < pTab->nCol; ii++){ + for(ii = 0; ii < pTab->nCol; ii++){ if( aXRef[ii] < 0 ){ - if (ii == pTab->iPKey) + if( ii == pTab->iPKey ){ sqliteVdbeAddOp(v, OP_Recno, base, 0); - else + }else{ sqliteVdbeAddOp(v, OP_Column, base, ii); + } }else{ sqliteExprCode(pParse, pChanges->a[aXRef[ii]].pExpr); } } sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); @@ -237,12 +240,14 @@ sqliteVdbeAddOp(v, OP_Close, base, 0); sqliteVdbeAddOp(v, OP_Rewind, oldIdx, 0); sqliteVdbeAddOp(v, OP_Rewind, newIdx, 0); - if (sqliteCodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_BEFORE, pTab, - newIdx, oldIdx, onError)) goto update_cleanup; + if( sqliteCodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_BEFORE, pTab, + newIdx, oldIdx, onError) ){ + goto update_cleanup; + } } /* Rewind the list of records that need to be updated and ** open every index that needs updating. Note that if any ** index could potentially invoke a REPLACE conflict resolution @@ -274,11 +279,11 @@ ** the old data for each record to be updated because some columns ** might not change and we will need to copy the old value. ** Also, the old data is needed to delete the old index entires. ** So make the cursor point at the old record. */ - if (!row_triggers_exist) { + if( !row_triggers_exist ){ int ii; sqliteVdbeAddOp(v, OP_ListRewind, 0, 0); addr = sqliteVdbeAddOp(v, OP_ListRead, 0, 0); sqliteVdbeAddOp(v, OP_Dup, 0, 0); } @@ -335,20 +340,22 @@ */ if( db->flags & SQLITE_CountRows && !pParse->trigStack){ sqliteVdbeAddOp(v, OP_AddImm, 1, 0); } - if (row_triggers_exist) { + if( row_triggers_exist ){ for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ if( openAll || aIdxUsed[i] ) sqliteVdbeAddOp(v, OP_Close, base+i+1, 0); } sqliteVdbeAddOp(v, OP_Close, base, 0); pParse->nTab = base; - if (sqliteCodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_AFTER, pTab, - newIdx, oldIdx, onError)) goto update_cleanup; + if( sqliteCodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_AFTER, pTab, + newIdx, oldIdx, onError) ){ + goto update_cleanup; + } } /* Repeat the above with the next record to be updated, until ** all record selected by the WHERE clause have been updated. */ @@ -355,19 +362,19 @@ sqliteVdbeAddOp(v, OP_Goto, 0, addr); sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v)); sqliteVdbeAddOp(v, OP_ListReset, 0, 0); /* Close all tables if there were no FOR EACH ROW triggers */ - if (!row_triggers_exist) { + if( !row_triggers_exist ){ for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ if( openAll || aIdxUsed[i] ){ sqliteVdbeAddOp(v, OP_Close, base+i+1, 0); } } sqliteVdbeAddOp(v, OP_Close, base, 0); pParse->nTab = base; - } else { + }else{ sqliteVdbeAddOp(v, OP_Close, newIdx, 0); sqliteVdbeAddOp(v, OP_Close, oldIdx, 0); } sqliteEndWriteOperation(pParse); Index: src/vdbe.c ================================================================== --- src/vdbe.c +++ src/vdbe.c @@ -28,11 +28,11 @@ ** Most of the code in this file is taken up by the sqliteVdbeExec() ** function which does the work of interpreting a VDBE program. ** But other routines are also provided to help in building up ** a program instruction by instruction. ** -** $Id: vdbe.c,v 1.143 2002/05/15 11:44:15 drh Exp $ +** $Id: vdbe.c,v 1.144 2002/05/19 23:43:14 danielk1977 Exp $ */ #include "sqliteInt.h" #include /* @@ -247,13 +247,12 @@ int nSet; /* Number of sets allocated */ Set *aSet; /* An array of sets */ int nCallback; /* Number of callbacks invoked so far */ int iLimit; /* Limit on the number of callbacks remaining */ int iOffset; /* Offset before beginning to do callbacks */ - - int keylistStackDepth; - Keylist ** keylistStack; + int keylistStackDepth; /* The size of the "keylist" stack */ + Keylist **keylistStack; /* The stack used by opcodes PushList & PopList */ }; /* ** Create a new virtual database engine. */ @@ -1000,13 +999,13 @@ sqliteHashClear(&p->aSet[i].hash); } sqliteFree(p->aSet); p->aSet = 0; p->nSet = 0; - if (p->keylistStackDepth > 0) { + if( p->keylistStackDepth > 0 ){ int ii; - for (ii = 0; ii < p->keylistStackDepth; ii++) { + for(ii = 0; ii < p->keylistStackDepth; ii++){ KeylistFree(p->keylistStack[ii]); } sqliteFree(p->keylistStack); p->keylistStackDepth = 0; p->keylistStack = 0; @@ -4575,11 +4574,11 @@ assert(p->keylistStackDepth > 0); p->keylistStackDepth--; KeylistFree(p->pList); p->pList = p->keylistStack[p->keylistStackDepth]; p->keylistStack[p->keylistStackDepth] = 0; - if (p->keylistStackDepth == 0) { + if( p->keylistStackDepth == 0 ){ sqliteFree(p->keylistStack); p->keylistStack = 0; } break; } Index: src/where.c ================================================================== --- src/where.c +++ src/where.c @@ -11,11 +11,11 @@ ************************************************************************* ** This module contains C code that generates VDBE code used to process ** the WHERE clause of SQL statements. Also found here are subroutines ** to generate VDBE code to evaluate expressions. ** -** $Id: where.c,v 1.43 2002/05/15 11:44:15 drh Exp $ +** $Id: where.c,v 1.44 2002/05/19 23:43:14 danielk1977 Exp $ */ #include "sqliteInt.h" /* ** The query generator uses an array of instances of this structure to @@ -214,19 +214,19 @@ /* Analyze all of the subexpressions. */ for(i=0; itrigStack && pParse->trigStack->newIdx >= 0) { + if( pParse->trigStack && pParse->trigStack->newIdx >= 0 ){ aExpr[i].prereqRight = aExpr[i].prereqRight & ~(1 << pParse->trigStack->newIdx - base); aExpr[i].prereqLeft = aExpr[i].prereqLeft & ~(1 << pParse->trigStack->newIdx - base); aExpr[i].prereqAll = aExpr[i].prereqAll & ~(1 << pParse->trigStack->newIdx - base); } - if (pParse->trigStack && pParse->trigStack->oldIdx >= 0) { + if( pParse->trigStack && pParse->trigStack->oldIdx >= 0 ){ aExpr[i].prereqRight = aExpr[i].prereqRight & ~(1 << pParse->trigStack->oldIdx - base); aExpr[i].prereqLeft = aExpr[i].prereqLeft & ~(1 << pParse->trigStack->oldIdx - base); aExpr[i].prereqAll =