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