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 goto drop_trigger_cleanup; 500 } 501 sqlite3DropTriggerPtr(pParse, pTrigger); 502 503 drop_trigger_cleanup: 504 sqlite3SrcListDelete(db, pName); 505 } 506 507 /* 508 ** Return a pointer to the Table structure for the table that a trigger 509 ** is set on. 510 */ 511 static Table *tableOfTrigger(Trigger *pTrigger){ 512 int n = sqlite3Strlen30(pTrigger->table); 513 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n); 514 } 515 516 517 /* 518 ** Drop a trigger given a pointer to that trigger. 519 */ 520 void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){ 521 Table *pTable; 522 Vdbe *v; 523 sqlite3 *db = pParse->db; 524 int iDb; 525 526 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema); 527 assert( iDb>=0 && iDb<db->nDb ); 528 pTable = tableOfTrigger(pTrigger); 529 assert( pTable ); 530 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 ); 531 #ifndef SQLITE_OMIT_AUTHORIZATION 532 { 533 int code = SQLITE_DROP_TRIGGER; 534 const char *zDb = db->aDb[iDb].zName; 535 const char *zTab = SCHEMA_TABLE(iDb); 536 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER; 537 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) || 538 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ 539 return; 540 } 541 } 542 #endif 543 544 /* Generate code to destroy the database record of the trigger. 545 */ 546 assert( pTable!=0 ); 547 if( (v = sqlite3GetVdbe(pParse))!=0 ){ 548 int base; 549 static const VdbeOpList dropTrigger[] = { 550 { OP_Rewind, 0, ADDR(9), 0}, 551 { OP_String8, 0, 1, 0}, /* 1 */ 552 { OP_Column, 0, 1, 2}, 553 { OP_Ne, 2, ADDR(8), 1}, 554 { OP_String8, 0, 1, 0}, /* 4: "trigger" */ 555 { OP_Column, 0, 0, 2}, 556 { OP_Ne, 2, ADDR(8), 1}, 557 { OP_Delete, 0, 0, 0}, 558 { OP_Next, 0, ADDR(1), 0}, /* 8 */ 559 }; 560 561 sqlite3BeginWriteOperation(pParse, 0, iDb); 562 sqlite3OpenMasterTable(pParse, iDb); 563 base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger); 564 sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, 0); 565 sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC); 566 sqlite3ChangeCookie(pParse, iDb); 567 sqlite3VdbeAddOp2(v, OP_Close, 0, 0); 568 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0); 569 if( pParse->nMem<3 ){ 570 pParse->nMem = 3; 571 } 572 } 573 } 574 575 /* 576 ** Remove a trigger from the hash tables of the sqlite* pointer. 577 */ 578 void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){ 579 Hash *pHash = &(db->aDb[iDb].pSchema->trigHash); 580 Trigger *pTrigger; 581 pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0); 582 if( ALWAYS(pTrigger) ){ 583 if( pTrigger->pSchema==pTrigger->pTabSchema ){ 584 Table *pTab = tableOfTrigger(pTrigger); 585 Trigger **pp; 586 for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext)); 587 *pp = (*pp)->pNext; 588 } 589 sqlite3DeleteTrigger(db, pTrigger); 590 db->flags |= SQLITE_InternChanges; 591 } 592 } 593 594 /* 595 ** pEList is the SET clause of an UPDATE statement. Each entry 596 ** in pEList is of the format <id>=<expr>. If any of the entries 597 ** in pEList have an <id> which matches an identifier in pIdList, 598 ** then return TRUE. If pIdList==NULL, then it is considered a 599 ** wildcard that matches anything. Likewise if pEList==NULL then 600 ** it matches anything so always return true. Return false only 601 ** if there is no match. 602 */ 603 static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){ 604 int e; 605 if( pIdList==0 || NEVER(pEList==0) ) return 1; 606 for(e=0; e<pEList->nExpr; e++){ 607 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1; 608 } 609 return 0; 610 } 611 612 /* 613 ** Return a list of all triggers on table pTab if there exists at least 614 ** one trigger that must be fired when an operation of type 'op' is 615 ** performed on the table, and, if that operation is an UPDATE, if at 616 ** least one of the columns in pChanges is being modified. 617 */ 618 Trigger *sqlite3TriggersExist( 619 Parse *pParse, /* Parse context */ 620 Table *pTab, /* The table the contains the triggers */ 621 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */ 622 ExprList *pChanges, /* Columns that change in an UPDATE statement */ 623 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */ 624 ){ 625 int mask = 0; 626 Trigger *pList = sqlite3TriggerList(pParse, pTab); 627 Trigger *p; 628 assert( pList==0 || IsVirtual(pTab)==0 ); 629 for(p=pList; p; p=p->pNext){ 630 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){ 631 mask |= p->tr_tm; 632 } 633 } 634 if( pMask ){ 635 *pMask = mask; 636 } 637 return (mask ? pList : 0); 638 } 639 640 /* 641 ** Convert the pStep->target token into a SrcList and return a pointer 642 ** to that SrcList. 643 ** 644 ** This routine adds a specific database name, if needed, to the target when 645 ** forming the SrcList. This prevents a trigger in one database from 646 ** referring to a target in another database. An exception is when the 647 ** trigger is in TEMP in which case it can refer to any other database it 648 ** wants. 649 */ 650 static SrcList *targetSrcList( 651 Parse *pParse, /* The parsing context */ 652 TriggerStep *pStep /* The trigger containing the target token */ 653 ){ 654 int iDb; /* Index of the database to use */ 655 SrcList *pSrc; /* SrcList to be returned */ 656 657 pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0); 658 if( pSrc ){ 659 assert( pSrc->nSrc>0 ); 660 assert( pSrc->a!=0 ); 661 iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema); 662 if( iDb==0 || iDb>=2 ){ 663 sqlite3 *db = pParse->db; 664 assert( iDb<pParse->db->nDb ); 665 pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName); 666 } 667 } 668 return pSrc; 669 } 670 671 /* 672 ** Generate VDBE code for the statements inside the body of a single 673 ** trigger. 674 */ 675 static int codeTriggerProgram( 676 Parse *pParse, /* The parser context */ 677 TriggerStep *pStepList, /* List of statements inside the trigger body */ 678 int orconf /* Conflict algorithm. (OE_Abort, etc) */ 679 ){ 680 TriggerStep *pStep; 681 Vdbe *v = pParse->pVdbe; 682 sqlite3 *db = pParse->db; 683 684 assert( pParse->pTriggerTab && pParse->pToplevel ); 685 assert( pStepList ); 686 assert( v!=0 ); 687 for(pStep=pStepList; pStep; pStep=pStep->pNext){ 688 /* Figure out the ON CONFLICT policy that will be used for this step 689 ** of the trigger program. If the statement that caused this trigger 690 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use 691 ** the ON CONFLICT policy that was specified as part of the trigger 692 ** step statement. Example: 693 ** 694 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN; 695 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b); 696 ** END; 697 ** 698 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy 699 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy 700 */ 701 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf; 702 703 switch( pStep->op ){ 704 case TK_UPDATE: { 705 sqlite3Update(pParse, 706 targetSrcList(pParse, pStep), 707 sqlite3ExprListDup(db, pStep->pExprList, 0), 708 sqlite3ExprDup(db, pStep->pWhere, 0), 709 pParse->eOrconf 710 ); 711 break; 712 } 713 case TK_INSERT: { 714 sqlite3Insert(pParse, 715 targetSrcList(pParse, pStep), 716 sqlite3ExprListDup(db, pStep->pExprList, 0), 717 sqlite3SelectDup(db, pStep->pSelect, 0), 718 sqlite3IdListDup(db, pStep->pIdList), 719 pParse->eOrconf 720 ); 721 break; 722 } 723 case TK_DELETE: { 724 sqlite3DeleteFrom(pParse, 725 targetSrcList(pParse, pStep), 726 sqlite3ExprDup(db, pStep->pWhere, 0) 727 ); 728 break; 729 } 730 default: assert( pStep->op==TK_SELECT ); { 731 SelectDest sDest; 732 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0); 733 sqlite3SelectDestInit(&sDest, SRT_Discard, 0); 734 sqlite3Select(pParse, pSelect, &sDest); 735 sqlite3SelectDelete(db, pSelect); 736 break; 737 } 738 } 739 if( pStep->op!=TK_SELECT ){ 740 sqlite3VdbeAddOp0(v, OP_ResetCount); 741 } 742 } 743 744 return 0; 745 } 746 747 #ifdef SQLITE_DEBUG 748 /* 749 ** This function is used to add VdbeComment() annotations to a VDBE 750 ** program. It is not used in production code, only for debugging. 751 */ 752 static const char *onErrorText(int onError){ 753 switch( onError ){ 754 case OE_Abort: return "abort"; 755 case OE_Rollback: return "rollback"; 756 case OE_Fail: return "fail"; 757 case OE_Replace: return "replace"; 758 case OE_Ignore: return "ignore"; 759 case OE_Default: return "default"; 760 } 761 return "n/a"; 762 } 763 #endif 764 765 /* 766 ** Parse context structure pFrom has just been used to create a sub-vdbe 767 ** (trigger program). If an error has occurred, transfer error information 768 ** from pFrom to pTo. 769 */ 770 static void transferParseError(Parse *pTo, Parse *pFrom){ 771 assert( pFrom->zErrMsg==0 || pFrom->nErr ); 772 assert( pTo->zErrMsg==0 || pTo->nErr ); 773 if( pTo->nErr==0 ){ 774 pTo->zErrMsg = pFrom->zErrMsg; 775 pTo->nErr = pFrom->nErr; 776 }else{ 777 sqlite3DbFree(pFrom->db, pFrom->zErrMsg); 778 } 779 } 780 781 /* 782 ** Create and populate a new TriggerPrg object with a sub-program 783 ** implementing trigger pTrigger with ON CONFLICT policy orconf. 784 */ 785 static TriggerPrg *codeRowTrigger( 786 Parse *pParse, /* Current parse context */ 787 Trigger *pTrigger, /* Trigger to code */ 788 Table *pTab, /* The table pTrigger is attached to */ 789 int orconf /* ON CONFLICT policy to code trigger program with */ 790 ){ 791 Parse *pTop = sqlite3ParseToplevel(pParse); 792 sqlite3 *db = pParse->db; /* Database handle */ 793 TriggerPrg *pPrg; /* Value to return */ 794 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */ 795 Vdbe *v; /* Temporary VM */ 796 NameContext sNC; /* Name context for sub-vdbe */ 797 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */ 798 Parse *pSubParse; /* Parse context for sub-vdbe */ 799 int iEndTrigger = 0; /* Label to jump to if WHEN is false */ 800 801 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) ); 802 803 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they 804 ** are freed if an error occurs, link them into the Parse.pTriggerPrg 805 ** list of the top-level Parse object sooner rather than later. */ 806 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg)); 807 if( !pPrg ) return 0; 808 pPrg->pNext = pTop->pTriggerPrg; 809 pTop->pTriggerPrg = pPrg; 810 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram)); 811 if( !pProgram ) return 0; 812 pProgram->nRef = 1; 813 pPrg->pTrigger = pTrigger; 814 pPrg->orconf = orconf; 815 pPrg->aColmask[0] = 0xffffffff; 816 pPrg->aColmask[1] = 0xffffffff; 817 818 /* Allocate and populate a new Parse context to use for coding the 819 ** trigger sub-program. */ 820 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse)); 821 if( !pSubParse ) return 0; 822 memset(&sNC, 0, sizeof(sNC)); 823 sNC.pParse = pSubParse; 824 pSubParse->db = db; 825 pSubParse->pTriggerTab = pTab; 826 pSubParse->pToplevel = pTop; 827 pSubParse->zAuthContext = pTrigger->zName; 828 pSubParse->eTriggerOp = pTrigger->op; 829 pSubParse->nQueryLoop = pParse->nQueryLoop; 830 831 v = sqlite3GetVdbe(pSubParse); 832 if( v ){ 833 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)", 834 pTrigger->zName, onErrorText(orconf), 835 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"), 836 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""), 837 (pTrigger->op==TK_INSERT ? "INSERT" : ""), 838 (pTrigger->op==TK_DELETE ? "DELETE" : ""), 839 pTab->zName 840 )); 841 #ifndef SQLITE_OMIT_TRACE 842 sqlite3VdbeChangeP4(v, -1, 843 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC 844 ); 845 #endif 846 847 /* If one was specified, code the WHEN clause. If it evaluates to false 848 ** (or NULL) the sub-vdbe is immediately halted by jumping to the 849 ** OP_Halt inserted at the end of the program. */ 850 if( pTrigger->pWhen ){ 851 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0); 852 if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen) 853 && db->mallocFailed==0 854 ){ 855 iEndTrigger = sqlite3VdbeMakeLabel(v); 856 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL); 857 } 858 sqlite3ExprDelete(db, pWhen); 859 } 860 861 /* Code the trigger program into the sub-vdbe. */ 862 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf); 863 864 /* Insert an OP_Halt at the end of the sub-program. */ 865 if( iEndTrigger ){ 866 sqlite3VdbeResolveLabel(v, iEndTrigger); 867 } 868 sqlite3VdbeAddOp0(v, OP_Halt); 869 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf))); 870 871 transferParseError(pParse, pSubParse); 872 if( db->mallocFailed==0 ){ 873 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg); 874 } 875 pProgram->nMem = pSubParse->nMem; 876 pProgram->nCsr = pSubParse->nTab; 877 pProgram->token = (void *)pTrigger; 878 pPrg->aColmask[0] = pSubParse->oldmask; 879 pPrg->aColmask[1] = pSubParse->newmask; 880 sqlite3VdbeDelete(v); 881 } 882 883 assert( !pSubParse->pAinc && !pSubParse->pZombieTab ); 884 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg ); 885 sqlite3StackFree(db, pSubParse); 886 887 return pPrg; 888 } 889 890 /* 891 ** Return a pointer to a TriggerPrg object containing the sub-program for 892 ** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such 893 ** TriggerPrg object exists, a new object is allocated and populated before 894 ** being returned. 895 */ 896 static TriggerPrg *getRowTrigger( 897 Parse *pParse, /* Current parse context */ 898 Trigger *pTrigger, /* Trigger to code */ 899 Table *pTab, /* The table trigger pTrigger is attached to */ 900 int orconf /* ON CONFLICT algorithm. */ 901 ){ 902 Parse *pRoot = sqlite3ParseToplevel(pParse); 903 TriggerPrg *pPrg; 904 905 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) ); 906 907 /* It may be that this trigger has already been coded (or is in the 908 ** process of being coded). If this is the case, then an entry with 909 ** a matching TriggerPrg.pTrigger field will be present somewhere 910 ** in the Parse.pTriggerPrg list. Search for such an entry. */ 911 for(pPrg=pRoot->pTriggerPrg; 912 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf); 913 pPrg=pPrg->pNext 914 ); 915 916 /* If an existing TriggerPrg could not be located, create a new one. */ 917 if( !pPrg ){ 918 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf); 919 } 920 921 return pPrg; 922 } 923 924 /* 925 ** Generate code for the trigger program associated with trigger p on 926 ** table pTab. The reg, orconf and ignoreJump parameters passed to this 927 ** function are the same as those described in the header function for 928 ** sqlite3CodeRowTrigger() 929 */ 930 void sqlite3CodeRowTriggerDirect( 931 Parse *pParse, /* Parse context */ 932 Trigger *p, /* Trigger to code */ 933 Table *pTab, /* The table to code triggers from */ 934 int reg, /* Reg array containing OLD.* and NEW.* values */ 935 int orconf, /* ON CONFLICT policy */ 936 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */ 937 ){ 938 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */ 939 TriggerPrg *pPrg; 940 pPrg = getRowTrigger(pParse, p, pTab, orconf); 941 assert( pPrg || pParse->nErr || pParse->db->mallocFailed ); 942 943 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program 944 ** is a pointer to the sub-vdbe containing the trigger program. */ 945 if( pPrg ){ 946 sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem); 947 pPrg->pProgram->nRef++; 948 sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM); 949 VdbeComment( 950 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf))); 951 952 /* Set the P5 operand of the OP_Program instruction to non-zero if 953 ** recursive invocation of this trigger program is disallowed. Recursive 954 ** invocation is disallowed if (a) the sub-program is really a trigger, 955 ** not a foreign key action, and (b) the flag to enable recursive triggers 956 ** is clear. */ 957 sqlite3VdbeChangeP5(v, (u8)(p->zName && !(pParse->db->flags&SQLITE_RecTriggers))); 958 } 959 } 960 961 /* 962 ** This is called to code the required FOR EACH ROW triggers for an operation 963 ** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE) 964 ** is given by the op paramater. The tr_tm parameter determines whether the 965 ** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then 966 ** parameter pChanges is passed the list of columns being modified. 967 ** 968 ** If there are no triggers that fire at the specified time for the specified 969 ** operation on pTab, this function is a no-op. 970 ** 971 ** The reg argument is the address of the first in an array of registers 972 ** that contain the values substituted for the new.* and old.* references 973 ** in the trigger program. If N is the number of columns in table pTab 974 ** (a copy of pTab->nCol), then registers are populated as follows: 975 ** 976 ** Register Contains 977 ** ------------------------------------------------------ 978 ** reg+0 OLD.rowid 979 ** reg+1 OLD.* value of left-most column of pTab 980 ** ... ... 981 ** reg+N OLD.* value of right-most column of pTab 982 ** reg+N+1 NEW.rowid 983 ** reg+N+2 OLD.* value of left-most column of pTab 984 ** ... ... 985 ** reg+N+N+1 NEW.* value of right-most column of pTab 986 ** 987 ** For ON DELETE triggers, the registers containing the NEW.* values will 988 ** never be accessed by the trigger program, so they are not allocated or 989 ** populated by the caller (there is no data to populate them with anyway). 990 ** Similarly, for ON INSERT triggers the values stored in the OLD.* registers 991 ** are never accessed, and so are not allocated by the caller. So, for an 992 ** ON INSERT trigger, the value passed to this function as parameter reg 993 ** is not a readable register, although registers (reg+N) through 994 ** (reg+N+N+1) are. 995 ** 996 ** Parameter orconf is the default conflict resolution algorithm for the 997 ** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump 998 ** is the instruction that control should jump to if a trigger program 999 ** raises an IGNORE exception. 1000 */ 1001 void sqlite3CodeRowTrigger( 1002 Parse *pParse, /* Parse context */ 1003 Trigger *pTrigger, /* List of triggers on table pTab */ 1004 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */ 1005 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */ 1006 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */ 1007 Table *pTab, /* The table to code triggers from */ 1008 int reg, /* The first in an array of registers (see above) */ 1009 int orconf, /* ON CONFLICT policy */ 1010 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */ 1011 ){ 1012 Trigger *p; /* Used to iterate through pTrigger list */ 1013 1014 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE ); 1015 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER ); 1016 assert( (op==TK_UPDATE)==(pChanges!=0) ); 1017 1018 for(p=pTrigger; p; p=p->pNext){ 1019 1020 /* Sanity checking: The schema for the trigger and for the table are 1021 ** always defined. The trigger must be in the same schema as the table 1022 ** or else it must be a TEMP trigger. */ 1023 assert( p->pSchema!=0 ); 1024 assert( p->pTabSchema!=0 ); 1025 assert( p->pSchema==p->pTabSchema 1026 || p->pSchema==pParse->db->aDb[1].pSchema ); 1027 1028 /* Determine whether we should code this trigger */ 1029 if( p->op==op 1030 && p->tr_tm==tr_tm 1031 && checkColumnOverlap(p->pColumns, pChanges) 1032 ){ 1033 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump); 1034 } 1035 } 1036 } 1037 1038 /* 1039 ** Triggers may access values stored in the old.* or new.* pseudo-table. 1040 ** This function returns a 32-bit bitmask indicating which columns of the 1041 ** old.* or new.* tables actually are used by triggers. This information 1042 ** may be used by the caller, for example, to avoid having to load the entire 1043 ** old.* record into memory when executing an UPDATE or DELETE command. 1044 ** 1045 ** Bit 0 of the returned mask is set if the left-most column of the 1046 ** table may be accessed using an [old|new].<col> reference. Bit 1 is set if 1047 ** the second leftmost column value is required, and so on. If there 1048 ** are more than 32 columns in the table, and at least one of the columns 1049 ** with an index greater than 32 may be accessed, 0xffffffff is returned. 1050 ** 1051 ** It is not possible to determine if the old.rowid or new.rowid column is 1052 ** accessed by triggers. The caller must always assume that it is. 1053 ** 1054 ** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned 1055 ** applies to the old.* table. If 1, the new.* table. 1056 ** 1057 ** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE 1058 ** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only 1059 ** included in the returned mask if the TRIGGER_BEFORE bit is set in the 1060 ** tr_tm parameter. Similarly, values accessed by AFTER triggers are only 1061 ** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm. 1062 */ 1063 u32 sqlite3TriggerColmask( 1064 Parse *pParse, /* Parse context */ 1065 Trigger *pTrigger, /* List of triggers on table pTab */ 1066 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */ 1067 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */ 1068 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */ 1069 Table *pTab, /* The table to code triggers from */ 1070 int orconf /* Default ON CONFLICT policy for trigger steps */ 1071 ){ 1072 const int op = pChanges ? TK_UPDATE : TK_DELETE; 1073 u32 mask = 0; 1074 Trigger *p; 1075 1076 assert( isNew==1 || isNew==0 ); 1077 for(p=pTrigger; p; p=p->pNext){ 1078 if( p->op==op && (tr_tm&p->tr_tm) 1079 && checkColumnOverlap(p->pColumns,pChanges) 1080 ){ 1081 TriggerPrg *pPrg; 1082 pPrg = getRowTrigger(pParse, p, pTab, orconf); 1083 if( pPrg ){ 1084 mask |= pPrg->aColmask[isNew]; 1085 } 1086 } 1087 } 1088 1089 return mask; 1090 } 1091 1092 #endif /* !defined(SQLITE_OMIT_TRIGGER) */ 1093