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