1 /* 2 ** 3 ** The author disclaims copyright to this source code. In place of 4 ** a legal notice, here is a blessing: 5 ** 6 ** May you do good and not evil. 7 ** May you find forgiveness for yourself and forgive others. 8 ** May you share freely, never taking more than you give. 9 ** 10 ************************************************************************* 11 ** This file contains the implementation for TRIGGERs 12 */ 13 #include "sqliteInt.h" 14 15 #ifndef SQLITE_OMIT_TRIGGER 16 /* 17 ** Delete a linked list of TriggerStep structures. 18 */ 19 void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){ 20 while( pTriggerStep ){ 21 TriggerStep * pTmp = pTriggerStep; 22 pTriggerStep = pTriggerStep->pNext; 23 24 sqlite3ExprDelete(db, pTmp->pWhere); 25 sqlite3ExprListDelete(db, pTmp->pExprList); 26 sqlite3SelectDelete(db, pTmp->pSelect); 27 sqlite3IdListDelete(db, pTmp->pIdList); 28 29 sqlite3DbFree(db, pTmp); 30 } 31 } 32 33 /* 34 ** Given table pTab, return a list of all the triggers attached to 35 ** the table. The list is connected by Trigger.pNext pointers. 36 ** 37 ** All of the triggers on pTab that are in the same database as pTab 38 ** are already attached to pTab->pTrigger. But there might be additional 39 ** triggers on pTab in the TEMP schema. This routine prepends all 40 ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list 41 ** and returns the combined list. 42 ** 43 ** To state it another way: This routine returns a list of all triggers 44 ** that fire off of pTab. The list will include any TEMP triggers on 45 ** pTab as well as the triggers lised in pTab->pTrigger. 46 */ 47 Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){ 48 Schema * const pTmpSchema = pParse->db->aDb[1].pSchema; 49 Trigger *pList = 0; /* List of triggers to return */ 50 51 if( pParse->disableTriggers ){ 52 return 0; 53 } 54 55 if( pTmpSchema!=pTab->pSchema ){ 56 HashElem *p; 57 for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){ 58 Trigger *pTrig = (Trigger *)sqliteHashData(p); 59 if( pTrig->pTabSchema==pTab->pSchema 60 && 0==sqlite3StrICmp(pTrig->table, pTab->zName) 61 ){ 62 pTrig->pNext = (pList ? pList : pTab->pTrigger); 63 pList = pTrig; 64 } 65 } 66 } 67 68 return (pList ? pList : pTab->pTrigger); 69 } 70 71 /* 72 ** This is called by the parser when it sees a CREATE TRIGGER statement 73 ** up to the point of the BEGIN before the trigger actions. A Trigger 74 ** structure is generated based on the information available and stored 75 ** in pParse->pNewTrigger. After the trigger actions have been parsed, the 76 ** sqlite3FinishTrigger() function is called to complete the trigger 77 ** construction process. 78 */ 79 void sqlite3BeginTrigger( 80 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */ 81 Token *pName1, /* The name of the trigger */ 82 Token *pName2, /* The name of the trigger */ 83 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */ 84 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */ 85 IdList *pColumns, /* column list if this is an UPDATE OF trigger */ 86 SrcList *pTableName,/* The name of the table/view the trigger applies to */ 87 Expr *pWhen, /* WHEN clause */ 88 int isTemp, /* True if the TEMPORARY keyword is present */ 89 int noErr /* Suppress errors if the trigger already exists */ 90 ){ 91 Trigger *pTrigger = 0; /* The new trigger */ 92 Table *pTab; /* Table that the trigger fires off of */ 93 char *zName = 0; /* Name of the trigger */ 94 sqlite3 *db = pParse->db; /* The database connection */ 95 int iDb; /* The database to store the trigger in */ 96 Token *pName; /* The unqualified db name */ 97 DbFixer sFix; /* State vector for the DB fixer */ 98 int iTabDb; /* Index of the database holding pTab */ 99 100 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */ 101 assert( pName2!=0 ); 102 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE ); 103 assert( op>0 && op<0xff ); 104 if( isTemp ){ 105 /* If TEMP was specified, then the trigger name may not be qualified. */ 106 if( pName2->n>0 ){ 107 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name"); 108 goto trigger_cleanup; 109 } 110 iDb = 1; 111 pName = pName1; 112 }else{ 113 /* Figure out the db that the the trigger will be created in */ 114 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); 115 if( iDb<0 ){ 116 goto trigger_cleanup; 117 } 118 } 119 120 /* If the trigger name was unqualified, and the table is a temp table, 121 ** then set iDb to 1 to create the trigger in the temporary database. 122 ** If sqlite3SrcListLookup() returns 0, indicating the table does not 123 ** exist, the error is caught by the block below. 124 */ 125 if( !pTableName || db->mallocFailed ){ 126 goto trigger_cleanup; 127 } 128 pTab = sqlite3SrcListLookup(pParse, pTableName); 129 if( db->init.busy==0 && pName2->n==0 && pTab 130 && pTab->pSchema==db->aDb[1].pSchema ){ 131 iDb = 1; 132 } 133 134 /* Ensure the table name matches database name and that the table exists */ 135 if( db->mallocFailed ) goto trigger_cleanup; 136 assert( pTableName->nSrc==1 ); 137 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) && 138 sqlite3FixSrcList(&sFix, pTableName) ){ 139 goto trigger_cleanup; 140 } 141 pTab = sqlite3SrcListLookup(pParse, pTableName); 142 if( !pTab ){ 143 /* The table does not exist. */ 144 if( db->init.iDb==1 ){ 145 /* Ticket #3810. 146 ** Normally, whenever a table is dropped, all associated triggers are 147 ** dropped too. But if a TEMP trigger is created on a non-TEMP table 148 ** and the table is dropped by a different database connection, the 149 ** trigger is not visible to the database connection that does the 150 ** drop so the trigger cannot be dropped. This results in an 151 ** "orphaned trigger" - a trigger whose associated table is missing. 152 */ 153 db->init.orphanTrigger = 1; 154 } 155 goto trigger_cleanup; 156 } 157 if( IsVirtual(pTab) ){ 158 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables"); 159 goto trigger_cleanup; 160 } 161 162 /* Check that the trigger name is not reserved and that no trigger of the 163 ** specified name exists */ 164 zName = sqlite3NameFromToken(db, pName); 165 if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ 166 goto trigger_cleanup; 167 } 168 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash), 169 zName, sqlite3Strlen30(zName)) ){ 170 if( !noErr ){ 171 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName); 172 } 173 goto trigger_cleanup; 174 } 175 176 /* Do not create a trigger on a system table */ 177 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){ 178 sqlite3ErrorMsg(pParse, "cannot create trigger on system table"); 179 pParse->nErr++; 180 goto trigger_cleanup; 181 } 182 183 /* INSTEAD of triggers are only for views and views only support INSTEAD 184 ** of triggers. 185 */ 186 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){ 187 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S", 188 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0); 189 goto trigger_cleanup; 190 } 191 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){ 192 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF" 193 " trigger on table: %S", pTableName, 0); 194 goto trigger_cleanup; 195 } 196 iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema); 197 198 #ifndef SQLITE_OMIT_AUTHORIZATION 199 { 200 int code = SQLITE_CREATE_TRIGGER; 201 const char *zDb = db->aDb[iTabDb].zName; 202 const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb; 203 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER; 204 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){ 205 goto trigger_cleanup; 206 } 207 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){ 208 goto trigger_cleanup; 209 } 210 } 211 #endif 212 213 /* INSTEAD OF triggers can only appear on views and BEFORE triggers 214 ** cannot appear on views. So we might as well translate every 215 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code 216 ** elsewhere. 217 */ 218 if (tr_tm == TK_INSTEAD){ 219 tr_tm = TK_BEFORE; 220 } 221 222 /* Build the Trigger object */ 223 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger)); 224 if( pTrigger==0 ) goto trigger_cleanup; 225 pTrigger->zName = zName; 226 zName = 0; 227 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName); 228 pTrigger->pSchema = db->aDb[iDb].pSchema; 229 pTrigger->pTabSchema = pTab->pSchema; 230 pTrigger->op = (u8)op; 231 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER; 232 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE); 233 pTrigger->pColumns = sqlite3IdListDup(db, pColumns); 234 assert( pParse->pNewTrigger==0 ); 235 pParse->pNewTrigger = pTrigger; 236 237 trigger_cleanup: 238 sqlite3DbFree(db, zName); 239 sqlite3SrcListDelete(db, pTableName); 240 sqlite3IdListDelete(db, pColumns); 241 sqlite3ExprDelete(db, pWhen); 242 if( !pParse->pNewTrigger ){ 243 sqlite3DeleteTrigger(db, pTrigger); 244 }else{ 245 assert( pParse->pNewTrigger==pTrigger ); 246 } 247 } 248 249 /* 250 ** This routine is called after all of the trigger actions have been parsed 251 ** in order to complete the process of building the trigger. 252 */ 253 void sqlite3FinishTrigger( 254 Parse *pParse, /* Parser context */ 255 TriggerStep *pStepList, /* The triggered program */ 256 Token *pAll /* Token that describes the complete CREATE TRIGGER */ 257 ){ 258 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */ 259 char *zName; /* Name of trigger */ 260 sqlite3 *db = pParse->db; /* The database */ 261 DbFixer sFix; /* Fixer object */ 262 int iDb; /* Database containing the trigger */ 263 Token nameToken; /* Trigger name for error reporting */ 264 265 pTrig = pParse->pNewTrigger; 266 pParse->pNewTrigger = 0; 267 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup; 268 zName = pTrig->zName; 269 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema); 270 pTrig->step_list = pStepList; 271 while( pStepList ){ 272 pStepList->pTrig = pTrig; 273 pStepList = pStepList->pNext; 274 } 275 nameToken.z = pTrig->zName; 276 nameToken.n = sqlite3Strlen30(nameToken.z); 277 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken) 278 && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){ 279 goto triggerfinish_cleanup; 280 } 281 282 /* if we are not initializing, 283 ** build the sqlite_master entry 284 */ 285 if( !db->init.busy ){ 286 Vdbe *v; 287 char *z; 288 289 /* Make an entry in the sqlite_master table */ 290 v = sqlite3GetVdbe(pParse); 291 if( v==0 ) goto triggerfinish_cleanup; 292 sqlite3BeginWriteOperation(pParse, 0, iDb); 293 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n); 294 sqlite3NestedParse(pParse, 295 "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')", 296 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName, 297 pTrig->table, z); 298 sqlite3DbFree(db, z); 299 sqlite3ChangeCookie(pParse, iDb); 300 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, sqlite3MPrintf( 301 db, "type='trigger' AND name='%q'", zName), P4_DYNAMIC 302 ); 303 } 304 305 if( db->init.busy ){ 306 Trigger *pLink = pTrig; 307 Hash *pHash = &db->aDb[iDb].pSchema->trigHash; 308 pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig); 309 if( pTrig ){ 310 db->mallocFailed = 1; 311 }else if( pLink->pSchema==pLink->pTabSchema ){ 312 Table *pTab; 313 int n = sqlite3Strlen30(pLink->table); 314 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n); 315 assert( pTab!=0 ); 316 pLink->pNext = pTab->pTrigger; 317 pTab->pTrigger = pLink; 318 } 319 } 320 321 triggerfinish_cleanup: 322 sqlite3DeleteTrigger(db, pTrig); 323 assert( !pParse->pNewTrigger ); 324 sqlite3DeleteTriggerStep(db, pStepList); 325 } 326 327 /* 328 ** Turn a SELECT statement (that the pSelect parameter points to) into 329 ** a trigger step. Return a pointer to a TriggerStep structure. 330 ** 331 ** The parser calls this routine when it finds a SELECT statement in 332 ** body of a TRIGGER. 333 */ 334 TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){ 335 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep)); 336 if( pTriggerStep==0 ) { 337 sqlite3SelectDelete(db, pSelect); 338 return 0; 339 } 340 pTriggerStep->op = TK_SELECT; 341 pTriggerStep->pSelect = pSelect; 342 pTriggerStep->orconf = OE_Default; 343 return pTriggerStep; 344 } 345 346 /* 347 ** Allocate space to hold a new trigger step. The allocated space 348 ** holds both the TriggerStep object and the TriggerStep.target.z string. 349 ** 350 ** If an OOM error occurs, NULL is returned and db->mallocFailed is set. 351 */ 352 static TriggerStep *triggerStepAllocate( 353 sqlite3 *db, /* Database connection */ 354 u8 op, /* Trigger opcode */ 355 Token *pName /* The target name */ 356 ){ 357 TriggerStep *pTriggerStep; 358 359 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n); 360 if( pTriggerStep ){ 361 char *z = (char*)&pTriggerStep[1]; 362 memcpy(z, pName->z, pName->n); 363 pTriggerStep->target.z = z; 364 pTriggerStep->target.n = pName->n; 365 pTriggerStep->op = op; 366 } 367 return pTriggerStep; 368 } 369 370 /* 371 ** Build a trigger step out of an INSERT statement. Return a pointer 372 ** to the new trigger step. 373 ** 374 ** The parser calls this routine when it sees an INSERT inside the 375 ** body of a trigger. 376 */ 377 TriggerStep *sqlite3TriggerInsertStep( 378 sqlite3 *db, /* The database connection */ 379 Token *pTableName, /* Name of the table into which we insert */ 380 IdList *pColumn, /* List of columns in pTableName to insert into */ 381 ExprList *pEList, /* The VALUE clause: a list of values to be inserted */ 382 Select *pSelect, /* A SELECT statement that supplies values */ 383 u8 orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */ 384 ){ 385 TriggerStep *pTriggerStep; 386 387 assert(pEList == 0 || pSelect == 0); 388 assert(pEList != 0 || pSelect != 0 || db->mallocFailed); 389 390 pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName); 391 if( pTriggerStep ){ 392 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); 393 pTriggerStep->pIdList = pColumn; 394 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE); 395 pTriggerStep->orconf = orconf; 396 }else{ 397 sqlite3IdListDelete(db, pColumn); 398 } 399 sqlite3ExprListDelete(db, pEList); 400 sqlite3SelectDelete(db, pSelect); 401 402 return pTriggerStep; 403 } 404 405 /* 406 ** Construct a trigger step that implements an UPDATE statement and return 407 ** a pointer to that trigger step. The parser calls this routine when it 408 ** sees an UPDATE statement inside the body of a CREATE TRIGGER. 409 */ 410 TriggerStep *sqlite3TriggerUpdateStep( 411 sqlite3 *db, /* The database connection */ 412 Token *pTableName, /* Name of the table to be updated */ 413 ExprList *pEList, /* The SET clause: list of column and new values */ 414 Expr *pWhere, /* The WHERE clause */ 415 u8 orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */ 416 ){ 417 TriggerStep *pTriggerStep; 418 419 pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName); 420 if( pTriggerStep ){ 421 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE); 422 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE); 423 pTriggerStep->orconf = orconf; 424 } 425 sqlite3ExprListDelete(db, pEList); 426 sqlite3ExprDelete(db, pWhere); 427 return pTriggerStep; 428 } 429 430 /* 431 ** Construct a trigger step that implements a DELETE statement and return 432 ** a pointer to that trigger step. The parser calls this routine when it 433 ** sees a DELETE statement inside the body of a CREATE TRIGGER. 434 */ 435 TriggerStep *sqlite3TriggerDeleteStep( 436 sqlite3 *db, /* Database connection */ 437 Token *pTableName, /* The table from which rows are deleted */ 438 Expr *pWhere /* The WHERE clause */ 439 ){ 440 TriggerStep *pTriggerStep; 441 442 pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName); 443 if( pTriggerStep ){ 444 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE); 445 pTriggerStep->orconf = OE_Default; 446 } 447 sqlite3ExprDelete(db, pWhere); 448 return pTriggerStep; 449 } 450 451 /* 452 ** Recursively delete a Trigger structure 453 */ 454 void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){ 455 if( pTrigger==0 ) return; 456 sqlite3DeleteTriggerStep(db, pTrigger->step_list); 457 sqlite3DbFree(db, pTrigger->zName); 458 sqlite3DbFree(db, pTrigger->table); 459 sqlite3ExprDelete(db, pTrigger->pWhen); 460 sqlite3IdListDelete(db, pTrigger->pColumns); 461 sqlite3DbFree(db, pTrigger); 462 } 463 464 /* 465 ** This function is called to drop a trigger from the database schema. 466 ** 467 ** This may be called directly from the parser and therefore identifies 468 ** the trigger by name. The sqlite3DropTriggerPtr() routine does the 469 ** same job as this routine except it takes a pointer to the trigger 470 ** instead of the trigger name. 471 **/ 472 void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){ 473 Trigger *pTrigger = 0; 474 int i; 475 const char *zDb; 476 const char *zName; 477 int nName; 478 sqlite3 *db = pParse->db; 479 480 if( db->mallocFailed ) goto drop_trigger_cleanup; 481 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ 482 goto drop_trigger_cleanup; 483 } 484 485 assert( pName->nSrc==1 ); 486 zDb = pName->a[0].zDatabase; 487 zName = pName->a[0].zName; 488 nName = sqlite3Strlen30(zName); 489 for(i=OMIT_TEMPDB; i<db->nDb; i++){ 490 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ 491 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue; 492 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName); 493 if( pTrigger ) break; 494 } 495 if( !pTrigger ){ 496 if( !noErr ){ 497 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0); 498 } 499 pParse->checkSchema = 1; 500 goto drop_trigger_cleanup; 501 } 502 sqlite3DropTriggerPtr(pParse, pTrigger); 503 504 drop_trigger_cleanup: 505 sqlite3SrcListDelete(db, pName); 506 } 507 508 /* 509 ** Return a pointer to the Table structure for the table that a trigger 510 ** is set on. 511 */ 512 static Table *tableOfTrigger(Trigger *pTrigger){ 513 int n = sqlite3Strlen30(pTrigger->table); 514 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n); 515 } 516 517 518 /* 519 ** Drop a trigger given a pointer to that trigger. 520 */ 521 void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){ 522 Table *pTable; 523 Vdbe *v; 524 sqlite3 *db = pParse->db; 525 int iDb; 526 527 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema); 528 assert( iDb>=0 && iDb<db->nDb ); 529 pTable = tableOfTrigger(pTrigger); 530 assert( pTable ); 531 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 ); 532 #ifndef SQLITE_OMIT_AUTHORIZATION 533 { 534 int code = SQLITE_DROP_TRIGGER; 535 const char *zDb = db->aDb[iDb].zName; 536 const char *zTab = SCHEMA_TABLE(iDb); 537 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER; 538 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) || 539 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ 540 return; 541 } 542 } 543 #endif 544 545 /* Generate code to destroy the database record of the trigger. 546 */ 547 assert( pTable!=0 ); 548 if( (v = sqlite3GetVdbe(pParse))!=0 ){ 549 int base; 550 static const VdbeOpList dropTrigger[] = { 551 { OP_Rewind, 0, ADDR(9), 0}, 552 { OP_String8, 0, 1, 0}, /* 1 */ 553 { OP_Column, 0, 1, 2}, 554 { OP_Ne, 2, ADDR(8), 1}, 555 { OP_String8, 0, 1, 0}, /* 4: "trigger" */ 556 { OP_Column, 0, 0, 2}, 557 { OP_Ne, 2, ADDR(8), 1}, 558 { OP_Delete, 0, 0, 0}, 559 { OP_Next, 0, ADDR(1), 0}, /* 8 */ 560 }; 561 562 sqlite3BeginWriteOperation(pParse, 0, iDb); 563 sqlite3OpenMasterTable(pParse, iDb); 564 base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger); 565 sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, 0); 566 sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC); 567 sqlite3ChangeCookie(pParse, iDb); 568 sqlite3VdbeAddOp2(v, OP_Close, 0, 0); 569 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0); 570 if( pParse->nMem<3 ){ 571 pParse->nMem = 3; 572 } 573 } 574 } 575 576 /* 577 ** Remove a trigger from the hash tables of the sqlite* pointer. 578 */ 579 void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){ 580 Hash *pHash = &(db->aDb[iDb].pSchema->trigHash); 581 Trigger *pTrigger; 582 pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0); 583 if( ALWAYS(pTrigger) ){ 584 if( pTrigger->pSchema==pTrigger->pTabSchema ){ 585 Table *pTab = tableOfTrigger(pTrigger); 586 Trigger **pp; 587 for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext)); 588 *pp = (*pp)->pNext; 589 } 590 sqlite3DeleteTrigger(db, pTrigger); 591 db->flags |= SQLITE_InternChanges; 592 } 593 } 594 595 /* 596 ** pEList is the SET clause of an UPDATE statement. Each entry 597 ** in pEList is of the format <id>=<expr>. If any of the entries 598 ** in pEList have an <id> which matches an identifier in pIdList, 599 ** then return TRUE. If pIdList==NULL, then it is considered a 600 ** wildcard that matches anything. Likewise if pEList==NULL then 601 ** it matches anything so always return true. Return false only 602 ** if there is no match. 603 */ 604 static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){ 605 int e; 606 if( pIdList==0 || NEVER(pEList==0) ) return 1; 607 for(e=0; e<pEList->nExpr; e++){ 608 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1; 609 } 610 return 0; 611 } 612 613 /* 614 ** Return a list of all triggers on table pTab if there exists at least 615 ** one trigger that must be fired when an operation of type 'op' is 616 ** performed on the table, and, if that operation is an UPDATE, if at 617 ** least one of the columns in pChanges is being modified. 618 */ 619 Trigger *sqlite3TriggersExist( 620 Parse *pParse, /* Parse context */ 621 Table *pTab, /* The table the contains the triggers */ 622 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */ 623 ExprList *pChanges, /* Columns that change in an UPDATE statement */ 624 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */ 625 ){ 626 int mask = 0; 627 Trigger *pList = sqlite3TriggerList(pParse, pTab); 628 Trigger *p; 629 assert( pList==0 || IsVirtual(pTab)==0 ); 630 for(p=pList; p; p=p->pNext){ 631 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){ 632 mask |= p->tr_tm; 633 } 634 } 635 if( pMask ){ 636 *pMask = mask; 637 } 638 return (mask ? pList : 0); 639 } 640 641 /* 642 ** Convert the pStep->target token into a SrcList and return a pointer 643 ** to that SrcList. 644 ** 645 ** This routine adds a specific database name, if needed, to the target when 646 ** forming the SrcList. This prevents a trigger in one database from 647 ** referring to a target in another database. An exception is when the 648 ** trigger is in TEMP in which case it can refer to any other database it 649 ** wants. 650 */ 651 static SrcList *targetSrcList( 652 Parse *pParse, /* The parsing context */ 653 TriggerStep *pStep /* The trigger containing the target token */ 654 ){ 655 int iDb; /* Index of the database to use */ 656 SrcList *pSrc; /* SrcList to be returned */ 657 658 pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0); 659 if( pSrc ){ 660 assert( pSrc->nSrc>0 ); 661 assert( pSrc->a!=0 ); 662 iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema); 663 if( iDb==0 || iDb>=2 ){ 664 sqlite3 *db = pParse->db; 665 assert( iDb<pParse->db->nDb ); 666 pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName); 667 } 668 } 669 return pSrc; 670 } 671 672 /* 673 ** Generate VDBE code for the statements inside the body of a single 674 ** trigger. 675 */ 676 static int codeTriggerProgram( 677 Parse *pParse, /* The parser context */ 678 TriggerStep *pStepList, /* List of statements inside the trigger body */ 679 int orconf /* Conflict algorithm. (OE_Abort, etc) */ 680 ){ 681 TriggerStep *pStep; 682 Vdbe *v = pParse->pVdbe; 683 sqlite3 *db = pParse->db; 684 685 assert( pParse->pTriggerTab && pParse->pToplevel ); 686 assert( pStepList ); 687 assert( v!=0 ); 688 for(pStep=pStepList; pStep; pStep=pStep->pNext){ 689 /* Figure out the ON CONFLICT policy that will be used for this step 690 ** of the trigger program. If the statement that caused this trigger 691 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use 692 ** the ON CONFLICT policy that was specified as part of the trigger 693 ** step statement. Example: 694 ** 695 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN; 696 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b); 697 ** END; 698 ** 699 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy 700 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy 701 */ 702 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf; 703 704 switch( pStep->op ){ 705 case TK_UPDATE: { 706 sqlite3Update(pParse, 707 targetSrcList(pParse, pStep), 708 sqlite3ExprListDup(db, pStep->pExprList, 0), 709 sqlite3ExprDup(db, pStep->pWhere, 0), 710 pParse->eOrconf 711 ); 712 break; 713 } 714 case TK_INSERT: { 715 sqlite3Insert(pParse, 716 targetSrcList(pParse, pStep), 717 sqlite3ExprListDup(db, pStep->pExprList, 0), 718 sqlite3SelectDup(db, pStep->pSelect, 0), 719 sqlite3IdListDup(db, pStep->pIdList), 720 pParse->eOrconf 721 ); 722 break; 723 } 724 case TK_DELETE: { 725 sqlite3DeleteFrom(pParse, 726 targetSrcList(pParse, pStep), 727 sqlite3ExprDup(db, pStep->pWhere, 0) 728 ); 729 break; 730 } 731 default: assert( pStep->op==TK_SELECT ); { 732 SelectDest sDest; 733 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0); 734 sqlite3SelectDestInit(&sDest, SRT_Discard, 0); 735 sqlite3Select(pParse, pSelect, &sDest); 736 sqlite3SelectDelete(db, pSelect); 737 break; 738 } 739 } 740 if( pStep->op!=TK_SELECT ){ 741 sqlite3VdbeAddOp0(v, OP_ResetCount); 742 } 743 } 744 745 return 0; 746 } 747 748 #ifdef SQLITE_DEBUG 749 /* 750 ** This function is used to add VdbeComment() annotations to a VDBE 751 ** program. It is not used in production code, only for debugging. 752 */ 753 static const char *onErrorText(int onError){ 754 switch( onError ){ 755 case OE_Abort: return "abort"; 756 case OE_Rollback: return "rollback"; 757 case OE_Fail: return "fail"; 758 case OE_Replace: return "replace"; 759 case OE_Ignore: return "ignore"; 760 case OE_Default: return "default"; 761 } 762 return "n/a"; 763 } 764 #endif 765 766 /* 767 ** Parse context structure pFrom has just been used to create a sub-vdbe 768 ** (trigger program). If an error has occurred, transfer error information 769 ** from pFrom to pTo. 770 */ 771 static void transferParseError(Parse *pTo, Parse *pFrom){ 772 assert( pFrom->zErrMsg==0 || pFrom->nErr ); 773 assert( pTo->zErrMsg==0 || pTo->nErr ); 774 if( pTo->nErr==0 ){ 775 pTo->zErrMsg = pFrom->zErrMsg; 776 pTo->nErr = pFrom->nErr; 777 }else{ 778 sqlite3DbFree(pFrom->db, pFrom->zErrMsg); 779 } 780 } 781 782 /* 783 ** Create and populate a new TriggerPrg object with a sub-program 784 ** implementing trigger pTrigger with ON CONFLICT policy orconf. 785 */ 786 static TriggerPrg *codeRowTrigger( 787 Parse *pParse, /* Current parse context */ 788 Trigger *pTrigger, /* Trigger to code */ 789 Table *pTab, /* The table pTrigger is attached to */ 790 int orconf /* ON CONFLICT policy to code trigger program with */ 791 ){ 792 Parse *pTop = sqlite3ParseToplevel(pParse); 793 sqlite3 *db = pParse->db; /* Database handle */ 794 TriggerPrg *pPrg; /* Value to return */ 795 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */ 796 Vdbe *v; /* Temporary VM */ 797 NameContext sNC; /* Name context for sub-vdbe */ 798 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */ 799 Parse *pSubParse; /* Parse context for sub-vdbe */ 800 int iEndTrigger = 0; /* Label to jump to if WHEN is false */ 801 802 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) ); 803 assert( pTop->pVdbe ); 804 805 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they 806 ** are freed if an error occurs, link them into the Parse.pTriggerPrg 807 ** list of the top-level Parse object sooner rather than later. */ 808 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg)); 809 if( !pPrg ) return 0; 810 pPrg->pNext = pTop->pTriggerPrg; 811 pTop->pTriggerPrg = pPrg; 812 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram)); 813 if( !pProgram ) return 0; 814 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram); 815 pPrg->pTrigger = pTrigger; 816 pPrg->orconf = orconf; 817 pPrg->aColmask[0] = 0xffffffff; 818 pPrg->aColmask[1] = 0xffffffff; 819 820 /* Allocate and populate a new Parse context to use for coding the 821 ** trigger sub-program. */ 822 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse)); 823 if( !pSubParse ) return 0; 824 memset(&sNC, 0, sizeof(sNC)); 825 sNC.pParse = pSubParse; 826 pSubParse->db = db; 827 pSubParse->pTriggerTab = pTab; 828 pSubParse->pToplevel = pTop; 829 pSubParse->zAuthContext = pTrigger->zName; 830 pSubParse->eTriggerOp = pTrigger->op; 831 pSubParse->nQueryLoop = pParse->nQueryLoop; 832 833 v = sqlite3GetVdbe(pSubParse); 834 if( v ){ 835 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)", 836 pTrigger->zName, onErrorText(orconf), 837 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"), 838 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""), 839 (pTrigger->op==TK_INSERT ? "INSERT" : ""), 840 (pTrigger->op==TK_DELETE ? "DELETE" : ""), 841 pTab->zName 842 )); 843 #ifndef SQLITE_OMIT_TRACE 844 sqlite3VdbeChangeP4(v, -1, 845 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC 846 ); 847 #endif 848 849 /* If one was specified, code the WHEN clause. If it evaluates to false 850 ** (or NULL) the sub-vdbe is immediately halted by jumping to the 851 ** OP_Halt inserted at the end of the program. */ 852 if( pTrigger->pWhen ){ 853 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0); 854 if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen) 855 && db->mallocFailed==0 856 ){ 857 iEndTrigger = sqlite3VdbeMakeLabel(v); 858 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL); 859 } 860 sqlite3ExprDelete(db, pWhen); 861 } 862 863 /* Code the trigger program into the sub-vdbe. */ 864 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf); 865 866 /* Insert an OP_Halt at the end of the sub-program. */ 867 if( iEndTrigger ){ 868 sqlite3VdbeResolveLabel(v, iEndTrigger); 869 } 870 sqlite3VdbeAddOp0(v, OP_Halt); 871 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf))); 872 873 transferParseError(pParse, pSubParse); 874 if( db->mallocFailed==0 ){ 875 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg); 876 } 877 pProgram->nMem = pSubParse->nMem; 878 pProgram->nCsr = pSubParse->nTab; 879 pProgram->token = (void *)pTrigger; 880 pPrg->aColmask[0] = pSubParse->oldmask; 881 pPrg->aColmask[1] = pSubParse->newmask; 882 sqlite3VdbeDelete(v); 883 } 884 885 assert( !pSubParse->pAinc && !pSubParse->pZombieTab ); 886 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg ); 887 sqlite3StackFree(db, pSubParse); 888 889 return pPrg; 890 } 891 892 /* 893 ** Return a pointer to a TriggerPrg object containing the sub-program for 894 ** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such 895 ** TriggerPrg object exists, a new object is allocated and populated before 896 ** being returned. 897 */ 898 static TriggerPrg *getRowTrigger( 899 Parse *pParse, /* Current parse context */ 900 Trigger *pTrigger, /* Trigger to code */ 901 Table *pTab, /* The table trigger pTrigger is attached to */ 902 int orconf /* ON CONFLICT algorithm. */ 903 ){ 904 Parse *pRoot = sqlite3ParseToplevel(pParse); 905 TriggerPrg *pPrg; 906 907 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) ); 908 909 /* It may be that this trigger has already been coded (or is in the 910 ** process of being coded). If this is the case, then an entry with 911 ** a matching TriggerPrg.pTrigger field will be present somewhere 912 ** in the Parse.pTriggerPrg list. Search for such an entry. */ 913 for(pPrg=pRoot->pTriggerPrg; 914 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf); 915 pPrg=pPrg->pNext 916 ); 917 918 /* If an existing TriggerPrg could not be located, create a new one. */ 919 if( !pPrg ){ 920 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf); 921 } 922 923 return pPrg; 924 } 925 926 /* 927 ** Generate code for the trigger program associated with trigger p on 928 ** table pTab. The reg, orconf and ignoreJump parameters passed to this 929 ** function are the same as those described in the header function for 930 ** sqlite3CodeRowTrigger() 931 */ 932 void sqlite3CodeRowTriggerDirect( 933 Parse *pParse, /* Parse context */ 934 Trigger *p, /* Trigger to code */ 935 Table *pTab, /* The table to code triggers from */ 936 int reg, /* Reg array containing OLD.* and NEW.* values */ 937 int orconf, /* ON CONFLICT policy */ 938 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */ 939 ){ 940 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */ 941 TriggerPrg *pPrg; 942 pPrg = getRowTrigger(pParse, p, pTab, orconf); 943 assert( pPrg || pParse->nErr || pParse->db->mallocFailed ); 944 945 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program 946 ** is a pointer to the sub-vdbe containing the trigger program. */ 947 if( pPrg ){ 948 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers)); 949 950 sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem); 951 sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM); 952 VdbeComment( 953 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf))); 954 955 /* Set the P5 operand of the OP_Program instruction to non-zero if 956 ** recursive invocation of this trigger program is disallowed. Recursive 957 ** invocation is disallowed if (a) the sub-program is really a trigger, 958 ** not a foreign key action, and (b) the flag to enable recursive triggers 959 ** is clear. */ 960 sqlite3VdbeChangeP5(v, (u8)bRecursive); 961 } 962 } 963 964 /* 965 ** This is called to code the required FOR EACH ROW triggers for an operation 966 ** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE) 967 ** is given by the op paramater. The tr_tm parameter determines whether the 968 ** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then 969 ** parameter pChanges is passed the list of columns being modified. 970 ** 971 ** If there are no triggers that fire at the specified time for the specified 972 ** operation on pTab, this function is a no-op. 973 ** 974 ** The reg argument is the address of the first in an array of registers 975 ** that contain the values substituted for the new.* and old.* references 976 ** in the trigger program. If N is the number of columns in table pTab 977 ** (a copy of pTab->nCol), then registers are populated as follows: 978 ** 979 ** Register Contains 980 ** ------------------------------------------------------ 981 ** reg+0 OLD.rowid 982 ** reg+1 OLD.* value of left-most column of pTab 983 ** ... ... 984 ** reg+N OLD.* value of right-most column of pTab 985 ** reg+N+1 NEW.rowid 986 ** reg+N+2 OLD.* value of left-most column of pTab 987 ** ... ... 988 ** reg+N+N+1 NEW.* value of right-most column of pTab 989 ** 990 ** For ON DELETE triggers, the registers containing the NEW.* values will 991 ** never be accessed by the trigger program, so they are not allocated or 992 ** populated by the caller (there is no data to populate them with anyway). 993 ** Similarly, for ON INSERT triggers the values stored in the OLD.* registers 994 ** are never accessed, and so are not allocated by the caller. So, for an 995 ** ON INSERT trigger, the value passed to this function as parameter reg 996 ** is not a readable register, although registers (reg+N) through 997 ** (reg+N+N+1) are. 998 ** 999 ** Parameter orconf is the default conflict resolution algorithm for the 1000 ** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump 1001 ** is the instruction that control should jump to if a trigger program 1002 ** raises an IGNORE exception. 1003 */ 1004 void sqlite3CodeRowTrigger( 1005 Parse *pParse, /* Parse context */ 1006 Trigger *pTrigger, /* List of triggers on table pTab */ 1007 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */ 1008 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */ 1009 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */ 1010 Table *pTab, /* The table to code triggers from */ 1011 int reg, /* The first in an array of registers (see above) */ 1012 int orconf, /* ON CONFLICT policy */ 1013 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */ 1014 ){ 1015 Trigger *p; /* Used to iterate through pTrigger list */ 1016 1017 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE ); 1018 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER ); 1019 assert( (op==TK_UPDATE)==(pChanges!=0) ); 1020 1021 for(p=pTrigger; p; p=p->pNext){ 1022 1023 /* Sanity checking: The schema for the trigger and for the table are 1024 ** always defined. The trigger must be in the same schema as the table 1025 ** or else it must be a TEMP trigger. */ 1026 assert( p->pSchema!=0 ); 1027 assert( p->pTabSchema!=0 ); 1028 assert( p->pSchema==p->pTabSchema 1029 || p->pSchema==pParse->db->aDb[1].pSchema ); 1030 1031 /* Determine whether we should code this trigger */ 1032 if( p->op==op 1033 && p->tr_tm==tr_tm 1034 && checkColumnOverlap(p->pColumns, pChanges) 1035 ){ 1036 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump); 1037 } 1038 } 1039 } 1040 1041 /* 1042 ** Triggers may access values stored in the old.* or new.* pseudo-table. 1043 ** This function returns a 32-bit bitmask indicating which columns of the 1044 ** old.* or new.* tables actually are used by triggers. This information 1045 ** may be used by the caller, for example, to avoid having to load the entire 1046 ** old.* record into memory when executing an UPDATE or DELETE command. 1047 ** 1048 ** Bit 0 of the returned mask is set if the left-most column of the 1049 ** table may be accessed using an [old|new].<col> reference. Bit 1 is set if 1050 ** the second leftmost column value is required, and so on. If there 1051 ** are more than 32 columns in the table, and at least one of the columns 1052 ** with an index greater than 32 may be accessed, 0xffffffff is returned. 1053 ** 1054 ** It is not possible to determine if the old.rowid or new.rowid column is 1055 ** accessed by triggers. The caller must always assume that it is. 1056 ** 1057 ** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned 1058 ** applies to the old.* table. If 1, the new.* table. 1059 ** 1060 ** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE 1061 ** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only 1062 ** included in the returned mask if the TRIGGER_BEFORE bit is set in the 1063 ** tr_tm parameter. Similarly, values accessed by AFTER triggers are only 1064 ** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm. 1065 */ 1066 u32 sqlite3TriggerColmask( 1067 Parse *pParse, /* Parse context */ 1068 Trigger *pTrigger, /* List of triggers on table pTab */ 1069 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */ 1070 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */ 1071 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */ 1072 Table *pTab, /* The table to code triggers from */ 1073 int orconf /* Default ON CONFLICT policy for trigger steps */ 1074 ){ 1075 const int op = pChanges ? TK_UPDATE : TK_DELETE; 1076 u32 mask = 0; 1077 Trigger *p; 1078 1079 assert( isNew==1 || isNew==0 ); 1080 for(p=pTrigger; p; p=p->pNext){ 1081 if( p->op==op && (tr_tm&p->tr_tm) 1082 && checkColumnOverlap(p->pColumns,pChanges) 1083 ){ 1084 TriggerPrg *pPrg; 1085 pPrg = getRowTrigger(pParse, p, pTab, orconf); 1086 if( pPrg ){ 1087 mask |= pPrg->aColmask[isNew]; 1088 } 1089 } 1090 } 1091 1092 return mask; 1093 } 1094 1095 #endif /* !defined(SQLITE_OMIT_TRIGGER) */ 1096