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