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 * 12 */ 13 #include "sqliteInt.h" 14 15 #ifndef SQLITE_OMIT_TRIGGER 16 /* 17 ** Delete a linked list of TriggerStep structures. 18 */ 19 void sqlite3DeleteTriggerStep(TriggerStep *pTriggerStep){ 20 while( pTriggerStep ){ 21 TriggerStep * pTmp = pTriggerStep; 22 pTriggerStep = pTriggerStep->pNext; 23 24 if( pTmp->target.dyn ) sqliteFree((char*)pTmp->target.z); 25 sqlite3ExprDelete(pTmp->pWhere); 26 sqlite3ExprListDelete(pTmp->pExprList); 27 sqlite3SelectDelete(pTmp->pSelect); 28 sqlite3IdListDelete(pTmp->pIdList); 29 30 sqliteFree(pTmp); 31 } 32 } 33 34 /* 35 ** This is called by the parser when it sees a CREATE TRIGGER statement 36 ** up to the point of the BEGIN before the trigger actions. A Trigger 37 ** structure is generated based on the information available and stored 38 ** in pParse->pNewTrigger. After the trigger actions have been parsed, the 39 ** sqlite3FinishTrigger() function is called to complete the trigger 40 ** construction process. 41 */ 42 void sqlite3BeginTrigger( 43 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */ 44 Token *pName1, /* The name of the trigger */ 45 Token *pName2, /* The name of the trigger */ 46 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */ 47 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */ 48 IdList *pColumns, /* column list if this is an UPDATE OF trigger */ 49 SrcList *pTableName,/* The name of the table/view the trigger applies to */ 50 int foreach, /* One of TK_ROW or TK_STATEMENT */ 51 Expr *pWhen, /* WHEN clause */ 52 int isTemp /* True if the TEMPORARY keyword is present */ 53 ){ 54 Trigger *pTrigger = 0; 55 Table *pTab; 56 char *zName = 0; /* Name of the trigger */ 57 sqlite3 *db = pParse->db; 58 int iDb; /* The database to store the trigger in */ 59 Token *pName; /* The unqualified db name */ 60 DbFixer sFix; 61 62 if( isTemp ){ 63 /* If TEMP was specified, then the trigger name may not be qualified. */ 64 if( pName2 && pName2->n>0 ){ 65 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name"); 66 goto trigger_cleanup; 67 } 68 iDb = 1; 69 pName = pName1; 70 }else{ 71 /* Figure out the db that the the trigger will be created in */ 72 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); 73 if( iDb<0 ){ 74 goto trigger_cleanup; 75 } 76 } 77 78 /* If the trigger name was unqualified, and the table is a temp table, 79 ** then set iDb to 1 to create the trigger in the temporary database. 80 ** If sqlite3SrcListLookup() returns 0, indicating the table does not 81 ** exist, the error is caught by the block below. 82 */ 83 if( !pTableName || sqlite3_malloc_failed ) goto trigger_cleanup; 84 pTab = sqlite3SrcListLookup(pParse, pTableName); 85 if( pName2->n==0 && pTab && pTab->iDb==1 ){ 86 iDb = 1; 87 } 88 89 /* Ensure the table name matches database name and that the table exists */ 90 if( sqlite3_malloc_failed ) goto trigger_cleanup; 91 assert( pTableName->nSrc==1 ); 92 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) && 93 sqlite3FixSrcList(&sFix, pTableName) ){ 94 goto trigger_cleanup; 95 } 96 pTab = sqlite3SrcListLookup(pParse, pTableName); 97 if( !pTab ){ 98 /* The table does not exist. */ 99 goto trigger_cleanup; 100 } 101 102 /* Check that the trigger name is not reserved and that no trigger of the 103 ** specified name exists */ 104 zName = sqlite3NameFromToken(pName); 105 if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ 106 goto trigger_cleanup; 107 } 108 if( sqlite3HashFind(&(db->aDb[iDb].trigHash), zName,pName->n+1) ){ 109 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName); 110 goto trigger_cleanup; 111 } 112 113 /* Do not create a trigger on a system table */ 114 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){ 115 sqlite3ErrorMsg(pParse, "cannot create trigger on system table"); 116 pParse->nErr++; 117 goto trigger_cleanup; 118 } 119 120 /* INSTEAD of triggers are only for views and views only support INSTEAD 121 ** of triggers. 122 */ 123 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){ 124 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S", 125 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0); 126 goto trigger_cleanup; 127 } 128 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){ 129 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF" 130 " trigger on table: %S", pTableName, 0); 131 goto trigger_cleanup; 132 } 133 134 #ifndef SQLITE_OMIT_AUTHORIZATION 135 { 136 int code = SQLITE_CREATE_TRIGGER; 137 const char *zDb = db->aDb[pTab->iDb].zName; 138 const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb; 139 if( pTab->iDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER; 140 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){ 141 goto trigger_cleanup; 142 } 143 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(pTab->iDb),0,zDb)){ 144 goto trigger_cleanup; 145 } 146 } 147 #endif 148 149 /* INSTEAD OF triggers can only appear on views and BEFORE triggers 150 ** cannot appear on views. So we might as well translate every 151 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code 152 ** elsewhere. 153 */ 154 if (tr_tm == TK_INSTEAD){ 155 tr_tm = TK_BEFORE; 156 } 157 158 /* Build the Trigger object */ 159 pTrigger = (Trigger*)sqliteMalloc(sizeof(Trigger)); 160 if( pTrigger==0 ) goto trigger_cleanup; 161 pTrigger->name = zName; 162 zName = 0; 163 pTrigger->table = sqliteStrDup(pTableName->a[0].zName); 164 pTrigger->iDb = iDb; 165 pTrigger->iTabDb = pTab->iDb; 166 pTrigger->op = op; 167 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER; 168 pTrigger->pWhen = sqlite3ExprDup(pWhen); 169 pTrigger->pColumns = sqlite3IdListDup(pColumns); 170 pTrigger->foreach = foreach; 171 sqlite3TokenCopy(&pTrigger->nameToken,pName); 172 assert( pParse->pNewTrigger==0 ); 173 pParse->pNewTrigger = pTrigger; 174 175 trigger_cleanup: 176 sqliteFree(zName); 177 sqlite3SrcListDelete(pTableName); 178 sqlite3IdListDelete(pColumns); 179 sqlite3ExprDelete(pWhen); 180 if( !pParse->pNewTrigger ){ 181 sqlite3DeleteTrigger(pTrigger); 182 }else{ 183 assert( pParse->pNewTrigger==pTrigger ); 184 } 185 } 186 187 /* 188 ** This routine is called after all of the trigger actions have been parsed 189 ** in order to complete the process of building the trigger. 190 */ 191 void sqlite3FinishTrigger( 192 Parse *pParse, /* Parser context */ 193 TriggerStep *pStepList, /* The triggered program */ 194 Token *pAll /* Token that describes the complete CREATE TRIGGER */ 195 ){ 196 Trigger *pTrig = 0; /* The trigger whose construction is finishing up */ 197 sqlite3 *db = pParse->db; /* The database */ 198 DbFixer sFix; 199 200 pTrig = pParse->pNewTrigger; 201 pParse->pNewTrigger = 0; 202 if( pParse->nErr || pTrig==0 ) goto triggerfinish_cleanup; 203 pTrig->step_list = pStepList; 204 while( pStepList ){ 205 pStepList->pTrig = pTrig; 206 pStepList = pStepList->pNext; 207 } 208 if( sqlite3FixInit(&sFix, pParse, pTrig->iDb, "trigger", &pTrig->nameToken) 209 && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){ 210 goto triggerfinish_cleanup; 211 } 212 213 /* if we are not initializing, and this trigger is not on a TEMP table, 214 ** build the sqlite_master entry 215 */ 216 if( !db->init.busy ){ 217 static const VdbeOpList insertTrig[] = { 218 { OP_NewRowid, 0, 0, 0 }, 219 { OP_String8, 0, 0, "trigger" }, 220 { OP_String8, 0, 0, 0 }, /* 2: trigger name */ 221 { OP_String8, 0, 0, 0 }, /* 3: table name */ 222 { OP_Integer, 0, 0, 0 }, 223 { OP_String8, 0, 0, "CREATE TRIGGER "}, 224 { OP_String8, 0, 0, 0 }, /* 6: SQL */ 225 { OP_Concat, 0, 0, 0 }, 226 { OP_MakeRecord, 5, 0, "tttit" }, 227 { OP_Insert, 0, 0, 0 }, 228 }; 229 int addr; 230 Vdbe *v; 231 232 /* Make an entry in the sqlite_master table */ 233 v = sqlite3GetVdbe(pParse); 234 if( v==0 ) goto triggerfinish_cleanup; 235 sqlite3BeginWriteOperation(pParse, 0, pTrig->iDb); 236 sqlite3OpenMasterTable(v, pTrig->iDb); 237 addr = sqlite3VdbeAddOpList(v, ArraySize(insertTrig), insertTrig); 238 sqlite3VdbeChangeP3(v, addr+2, pTrig->name, 0); 239 sqlite3VdbeChangeP3(v, addr+3, pTrig->table, 0); 240 sqlite3VdbeChangeP3(v, addr+6, pAll->z, pAll->n); 241 sqlite3ChangeCookie(db, v, pTrig->iDb); 242 sqlite3VdbeAddOp(v, OP_Close, 0, 0); 243 sqlite3VdbeOp3(v, OP_ParseSchema, pTrig->iDb, 0, 244 sqlite3MPrintf("type='trigger' AND name='%q'", pTrig->name), P3_DYNAMIC); 245 } 246 247 if( db->init.busy ){ 248 Table *pTab; 249 Trigger *pDel; 250 pDel = sqlite3HashInsert(&db->aDb[pTrig->iDb].trigHash, 251 pTrig->name, strlen(pTrig->name)+1, pTrig); 252 if( pDel ){ 253 assert( sqlite3_malloc_failed && pDel==pTrig ); 254 goto triggerfinish_cleanup; 255 } 256 pTab = sqlite3LocateTable(pParse,pTrig->table,db->aDb[pTrig->iTabDb].zName); 257 assert( pTab!=0 ); 258 pTrig->pNext = pTab->pTrigger; 259 pTab->pTrigger = pTrig; 260 pTrig = 0; 261 } 262 263 triggerfinish_cleanup: 264 sqlite3DeleteTrigger(pTrig); 265 assert( !pParse->pNewTrigger ); 266 sqlite3DeleteTriggerStep(pStepList); 267 } 268 269 /* 270 ** Make a copy of all components of the given trigger step. This has 271 ** the effect of copying all Expr.token.z values into memory obtained 272 ** from sqliteMalloc(). As initially created, the Expr.token.z values 273 ** all point to the input string that was fed to the parser. But that 274 ** string is ephemeral - it will go away as soon as the sqlite3_exec() 275 ** call that started the parser exits. This routine makes a persistent 276 ** copy of all the Expr.token.z strings so that the TriggerStep structure 277 ** will be valid even after the sqlite3_exec() call returns. 278 */ 279 static void sqlitePersistTriggerStep(TriggerStep *p){ 280 if( p->target.z ){ 281 p->target.z = sqliteStrNDup(p->target.z, p->target.n); 282 p->target.dyn = 1; 283 } 284 if( p->pSelect ){ 285 Select *pNew = sqlite3SelectDup(p->pSelect); 286 sqlite3SelectDelete(p->pSelect); 287 p->pSelect = pNew; 288 } 289 if( p->pWhere ){ 290 Expr *pNew = sqlite3ExprDup(p->pWhere); 291 sqlite3ExprDelete(p->pWhere); 292 p->pWhere = pNew; 293 } 294 if( p->pExprList ){ 295 ExprList *pNew = sqlite3ExprListDup(p->pExprList); 296 sqlite3ExprListDelete(p->pExprList); 297 p->pExprList = pNew; 298 } 299 if( p->pIdList ){ 300 IdList *pNew = sqlite3IdListDup(p->pIdList); 301 sqlite3IdListDelete(p->pIdList); 302 p->pIdList = pNew; 303 } 304 } 305 306 /* 307 ** Turn a SELECT statement (that the pSelect parameter points to) into 308 ** a trigger step. Return a pointer to a TriggerStep structure. 309 ** 310 ** The parser calls this routine when it finds a SELECT statement in 311 ** body of a TRIGGER. 312 */ 313 TriggerStep *sqlite3TriggerSelectStep(Select *pSelect){ 314 TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep)); 315 if( pTriggerStep==0 ) return 0; 316 317 pTriggerStep->op = TK_SELECT; 318 pTriggerStep->pSelect = pSelect; 319 pTriggerStep->orconf = OE_Default; 320 sqlitePersistTriggerStep(pTriggerStep); 321 322 return pTriggerStep; 323 } 324 325 /* 326 ** Build a trigger step out of an INSERT statement. Return a pointer 327 ** to the new trigger step. 328 ** 329 ** The parser calls this routine when it sees an INSERT inside the 330 ** body of a trigger. 331 */ 332 TriggerStep *sqlite3TriggerInsertStep( 333 Token *pTableName, /* Name of the table into which we insert */ 334 IdList *pColumn, /* List of columns in pTableName to insert into */ 335 ExprList *pEList, /* The VALUE clause: a list of values to be inserted */ 336 Select *pSelect, /* A SELECT statement that supplies values */ 337 int orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */ 338 ){ 339 TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep)); 340 341 assert(pEList == 0 || pSelect == 0); 342 assert(pEList != 0 || pSelect != 0); 343 344 if( pTriggerStep ){ 345 pTriggerStep->op = TK_INSERT; 346 pTriggerStep->pSelect = pSelect; 347 pTriggerStep->target = *pTableName; 348 pTriggerStep->pIdList = pColumn; 349 pTriggerStep->pExprList = pEList; 350 pTriggerStep->orconf = orconf; 351 sqlitePersistTriggerStep(pTriggerStep); 352 }else{ 353 sqlite3IdListDelete(pColumn); 354 sqlite3ExprListDelete(pEList); 355 sqlite3SelectDup(pSelect); 356 } 357 358 return pTriggerStep; 359 } 360 361 /* 362 ** Construct a trigger step that implements an UPDATE statement and return 363 ** a pointer to that trigger step. The parser calls this routine when it 364 ** sees an UPDATE statement inside the body of a CREATE TRIGGER. 365 */ 366 TriggerStep *sqlite3TriggerUpdateStep( 367 Token *pTableName, /* Name of the table to be updated */ 368 ExprList *pEList, /* The SET clause: list of column and new values */ 369 Expr *pWhere, /* The WHERE clause */ 370 int orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */ 371 ){ 372 TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep)); 373 if( pTriggerStep==0 ) return 0; 374 375 pTriggerStep->op = TK_UPDATE; 376 pTriggerStep->target = *pTableName; 377 pTriggerStep->pExprList = pEList; 378 pTriggerStep->pWhere = pWhere; 379 pTriggerStep->orconf = orconf; 380 sqlitePersistTriggerStep(pTriggerStep); 381 382 return pTriggerStep; 383 } 384 385 /* 386 ** Construct a trigger step that implements a DELETE statement and return 387 ** a pointer to that trigger step. The parser calls this routine when it 388 ** sees a DELETE statement inside the body of a CREATE TRIGGER. 389 */ 390 TriggerStep *sqlite3TriggerDeleteStep(Token *pTableName, Expr *pWhere){ 391 TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep)); 392 if( pTriggerStep==0 ) return 0; 393 394 pTriggerStep->op = TK_DELETE; 395 pTriggerStep->target = *pTableName; 396 pTriggerStep->pWhere = pWhere; 397 pTriggerStep->orconf = OE_Default; 398 sqlitePersistTriggerStep(pTriggerStep); 399 400 return pTriggerStep; 401 } 402 403 /* 404 ** Recursively delete a Trigger structure 405 */ 406 void sqlite3DeleteTrigger(Trigger *pTrigger){ 407 if( pTrigger==0 ) return; 408 sqlite3DeleteTriggerStep(pTrigger->step_list); 409 sqliteFree(pTrigger->name); 410 sqliteFree(pTrigger->table); 411 sqlite3ExprDelete(pTrigger->pWhen); 412 sqlite3IdListDelete(pTrigger->pColumns); 413 if( pTrigger->nameToken.dyn ) sqliteFree((char*)pTrigger->nameToken.z); 414 sqliteFree(pTrigger); 415 } 416 417 /* 418 ** This function is called to drop a trigger from the database schema. 419 ** 420 ** This may be called directly from the parser and therefore identifies 421 ** the trigger by name. The sqlite3DropTriggerPtr() routine does the 422 ** same job as this routine except it takes a pointer to the trigger 423 ** instead of the trigger name. 424 **/ 425 void sqlite3DropTrigger(Parse *pParse, SrcList *pName){ 426 Trigger *pTrigger = 0; 427 int i; 428 const char *zDb; 429 const char *zName; 430 int nName; 431 sqlite3 *db = pParse->db; 432 433 if( sqlite3_malloc_failed ) goto drop_trigger_cleanup; 434 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ 435 goto drop_trigger_cleanup; 436 } 437 438 assert( pName->nSrc==1 ); 439 zDb = pName->a[0].zDatabase; 440 zName = pName->a[0].zName; 441 nName = strlen(zName); 442 for(i=OMIT_TEMPDB; i<db->nDb; i++){ 443 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ 444 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue; 445 pTrigger = sqlite3HashFind(&(db->aDb[j].trigHash), zName, nName+1); 446 if( pTrigger ) break; 447 } 448 if( !pTrigger ){ 449 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0); 450 goto drop_trigger_cleanup; 451 } 452 sqlite3DropTriggerPtr(pParse, pTrigger, 0); 453 454 drop_trigger_cleanup: 455 sqlite3SrcListDelete(pName); 456 } 457 458 /* 459 ** Return a pointer to the Table structure for the table that a trigger 460 ** is set on. 461 */ 462 static Table *tableOfTrigger(sqlite3 *db, Trigger *pTrigger){ 463 return sqlite3FindTable(db,pTrigger->table,db->aDb[pTrigger->iTabDb].zName); 464 } 465 466 467 /* 468 ** Drop a trigger given a pointer to that trigger. If nested is false, 469 ** then also generate code to remove the trigger from the SQLITE_MASTER 470 ** table. 471 */ 472 void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger, int nested){ 473 Table *pTable; 474 Vdbe *v; 475 sqlite3 *db = pParse->db; 476 int iDb; 477 478 iDb = pTrigger->iDb; 479 assert( iDb>=0 && iDb<db->nDb ); 480 pTable = tableOfTrigger(db, pTrigger); 481 assert(pTable); 482 assert( pTable->iDb==iDb || iDb==1 ); 483 #ifndef SQLITE_OMIT_AUTHORIZATION 484 { 485 int code = SQLITE_DROP_TRIGGER; 486 const char *zDb = db->aDb[iDb].zName; 487 const char *zTab = SCHEMA_TABLE(iDb); 488 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER; 489 if( sqlite3AuthCheck(pParse, code, pTrigger->name, pTable->zName, zDb) || 490 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ 491 return; 492 } 493 } 494 #endif 495 496 /* Generate code to destroy the database record of the trigger. 497 */ 498 if( pTable!=0 && (v = sqlite3GetVdbe(pParse))!=0 ){ 499 int base; 500 static const VdbeOpList dropTrigger[] = { 501 { OP_Rewind, 0, ADDR(9), 0}, 502 { OP_String8, 0, 0, 0}, /* 1 */ 503 { OP_Column, 0, 1, 0}, 504 { OP_Ne, 0, ADDR(8), 0}, 505 { OP_String8, 0, 0, "trigger"}, 506 { OP_Column, 0, 0, 0}, 507 { OP_Ne, 0, ADDR(8), 0}, 508 { OP_Delete, 0, 0, 0}, 509 { OP_Next, 0, ADDR(1), 0}, /* 8 */ 510 }; 511 512 sqlite3BeginWriteOperation(pParse, 0, iDb); 513 sqlite3OpenMasterTable(v, iDb); 514 base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger); 515 sqlite3VdbeChangeP3(v, base+1, pTrigger->name, 0); 516 sqlite3ChangeCookie(db, v, iDb); 517 sqlite3VdbeAddOp(v, OP_Close, 0, 0); 518 sqlite3VdbeOp3(v, OP_DropTrigger, iDb, 0, pTrigger->name, 0); 519 } 520 } 521 522 /* 523 ** Remove a trigger from the hash tables of the sqlite* pointer. 524 */ 525 void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){ 526 Trigger *pTrigger; 527 int nName = strlen(zName); 528 pTrigger = sqlite3HashInsert(&(db->aDb[iDb].trigHash), zName, nName+1, 0); 529 if( pTrigger ){ 530 Table *pTable = tableOfTrigger(db, pTrigger); 531 assert( pTable!=0 ); 532 if( pTable->pTrigger == pTrigger ){ 533 pTable->pTrigger = pTrigger->pNext; 534 }else{ 535 Trigger *cc = pTable->pTrigger; 536 while( cc ){ 537 if( cc->pNext == pTrigger ){ 538 cc->pNext = cc->pNext->pNext; 539 break; 540 } 541 cc = cc->pNext; 542 } 543 assert(cc); 544 } 545 sqlite3DeleteTrigger(pTrigger); 546 db->flags |= SQLITE_InternChanges; 547 } 548 } 549 550 /* 551 ** pEList is the SET clause of an UPDATE statement. Each entry 552 ** in pEList is of the format <id>=<expr>. If any of the entries 553 ** in pEList have an <id> which matches an identifier in pIdList, 554 ** then return TRUE. If pIdList==NULL, then it is considered a 555 ** wildcard that matches anything. Likewise if pEList==NULL then 556 ** it matches anything so always return true. Return false only 557 ** if there is no match. 558 */ 559 static int checkColumnOverLap(IdList *pIdList, ExprList *pEList){ 560 int e; 561 if( !pIdList || !pEList ) return 1; 562 for(e=0; e<pEList->nExpr; e++){ 563 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1; 564 } 565 return 0; 566 } 567 568 /* 569 ** Return a bit vector to indicate what kind of triggers exist for operation 570 ** "op" on table pTab. If pChanges is not NULL then it is a list of columns 571 ** that are being updated. Triggers only match if the ON clause of the 572 ** trigger definition overlaps the set of columns being updated. 573 ** 574 ** The returned bit vector is some combination of TRIGGER_BEFORE and 575 ** TRIGGER_AFTER. 576 */ 577 int sqlite3TriggersExist( 578 Parse *pParse, /* Used to check for recursive triggers */ 579 Table *pTab, /* The table the contains the triggers */ 580 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */ 581 ExprList *pChanges /* Columns that change in an UPDATE statement */ 582 ){ 583 Trigger *pTrigger = pTab->pTrigger; 584 int mask = 0; 585 586 while( pTrigger ){ 587 if( pTrigger->op==op && checkColumnOverLap(pTrigger->pColumns, pChanges) ){ 588 TriggerStack *ss; 589 ss = pParse->trigStack; 590 while( ss && ss->pTrigger!=pTab->pTrigger ){ 591 ss = ss->pNext; 592 } 593 if( ss==0 ){ 594 mask |= pTrigger->tr_tm; 595 } 596 } 597 pTrigger = pTrigger->pNext; 598 } 599 return mask; 600 } 601 602 /* 603 ** Convert the pStep->target token into a SrcList and return a pointer 604 ** to that SrcList. 605 ** 606 ** This routine adds a specific database name, if needed, to the target when 607 ** forming the SrcList. This prevents a trigger in one database from 608 ** referring to a target in another database. An exception is when the 609 ** trigger is in TEMP in which case it can refer to any other database it 610 ** wants. 611 */ 612 static SrcList *targetSrcList( 613 Parse *pParse, /* The parsing context */ 614 TriggerStep *pStep /* The trigger containing the target token */ 615 ){ 616 Token sDb; /* Dummy database name token */ 617 int iDb; /* Index of the database to use */ 618 SrcList *pSrc; /* SrcList to be returned */ 619 620 iDb = pStep->pTrig->iDb; 621 if( iDb==0 || iDb>=2 ){ 622 assert( iDb<pParse->db->nDb ); 623 sDb.z = pParse->db->aDb[iDb].zName; 624 sDb.n = strlen(sDb.z); 625 pSrc = sqlite3SrcListAppend(0, &sDb, &pStep->target); 626 } else { 627 pSrc = sqlite3SrcListAppend(0, &pStep->target, 0); 628 } 629 return pSrc; 630 } 631 632 /* 633 ** Generate VDBE code for zero or more statements inside the body of a 634 ** trigger. 635 */ 636 static int codeTriggerProgram( 637 Parse *pParse, /* The parser context */ 638 TriggerStep *pStepList, /* List of statements inside the trigger body */ 639 int orconfin /* Conflict algorithm. (OE_Abort, etc) */ 640 ){ 641 TriggerStep * pTriggerStep = pStepList; 642 int orconf; 643 Vdbe *v = pParse->pVdbe; 644 645 assert( pTriggerStep!=0 ); 646 assert( v!=0 ); 647 sqlite3VdbeAddOp(v, OP_ContextPush, 0, 0); 648 VdbeComment((v, "# begin trigger %s", pStepList->pTrig->name)); 649 while( pTriggerStep ){ 650 orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin; 651 pParse->trigStack->orconf = orconf; 652 switch( pTriggerStep->op ){ 653 case TK_SELECT: { 654 Select * ss = sqlite3SelectDup(pTriggerStep->pSelect); 655 assert(ss); 656 assert(ss->pSrc); 657 sqlite3SelectResolve(pParse, ss, 0); 658 sqlite3Select(pParse, ss, SRT_Discard, 0, 0, 0, 0, 0); 659 sqlite3SelectDelete(ss); 660 break; 661 } 662 case TK_UPDATE: { 663 SrcList *pSrc; 664 pSrc = targetSrcList(pParse, pTriggerStep); 665 sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0); 666 sqlite3Update(pParse, pSrc, 667 sqlite3ExprListDup(pTriggerStep->pExprList), 668 sqlite3ExprDup(pTriggerStep->pWhere), orconf); 669 sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0); 670 break; 671 } 672 case TK_INSERT: { 673 SrcList *pSrc; 674 pSrc = targetSrcList(pParse, pTriggerStep); 675 sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0); 676 sqlite3Insert(pParse, pSrc, 677 sqlite3ExprListDup(pTriggerStep->pExprList), 678 sqlite3SelectDup(pTriggerStep->pSelect), 679 sqlite3IdListDup(pTriggerStep->pIdList), orconf); 680 sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0); 681 break; 682 } 683 case TK_DELETE: { 684 SrcList *pSrc; 685 sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0); 686 pSrc = targetSrcList(pParse, pTriggerStep); 687 sqlite3DeleteFrom(pParse, pSrc, sqlite3ExprDup(pTriggerStep->pWhere)); 688 sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0); 689 break; 690 } 691 default: 692 assert(0); 693 } 694 pTriggerStep = pTriggerStep->pNext; 695 } 696 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0); 697 VdbeComment((v, "# end trigger %s", pStepList->pTrig->name)); 698 699 return 0; 700 } 701 702 /* 703 ** This is called to code FOR EACH ROW triggers. 704 ** 705 ** When the code that this function generates is executed, the following 706 ** must be true: 707 ** 708 ** 1. No cursors may be open in the main database. (But newIdx and oldIdx 709 ** can be indices of cursors in temporary tables. See below.) 710 ** 711 ** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then 712 ** a temporary vdbe cursor (index newIdx) must be open and pointing at 713 ** a row containing values to be substituted for new.* expressions in the 714 ** trigger program(s). 715 ** 716 ** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then 717 ** a temporary vdbe cursor (index oldIdx) must be open and pointing at 718 ** a row containing values to be substituted for old.* expressions in the 719 ** trigger program(s). 720 ** 721 */ 722 int sqlite3CodeRowTrigger( 723 Parse *pParse, /* Parse context */ 724 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */ 725 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */ 726 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */ 727 Table *pTab, /* The table to code triggers from */ 728 int newIdx, /* The indice of the "new" row to access */ 729 int oldIdx, /* The indice of the "old" row to access */ 730 int orconf, /* ON CONFLICT policy */ 731 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */ 732 ){ 733 Trigger *pTrigger; 734 TriggerStack *pStack; 735 TriggerStack trigStackEntry; 736 737 assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE); 738 assert(tr_tm == TRIGGER_BEFORE || tr_tm == TRIGGER_AFTER ); 739 740 assert(newIdx != -1 || oldIdx != -1); 741 742 pTrigger = pTab->pTrigger; 743 while( pTrigger ){ 744 int fire_this = 0; 745 746 /* determine whether we should code this trigger */ 747 if( pTrigger->op == op && pTrigger->tr_tm == tr_tm ){ 748 fire_this = 1; 749 for(pStack=pParse->trigStack; pStack; pStack=pStack->pNext){ 750 if( pStack->pTrigger==pTrigger ){ 751 fire_this = 0; 752 } 753 } 754 if( op == TK_UPDATE && pTrigger->pColumns && 755 !checkColumnOverLap(pTrigger->pColumns, pChanges) ){ 756 fire_this = 0; 757 } 758 } 759 760 if( fire_this ){ 761 int endTrigger; 762 Expr * whenExpr; 763 AuthContext sContext; 764 NameContext sNC; 765 766 memset(&sNC, 0, sizeof(sNC)); 767 sNC.pParse = pParse; 768 769 /* Push an entry on to the trigger stack */ 770 trigStackEntry.pTrigger = pTrigger; 771 trigStackEntry.newIdx = newIdx; 772 trigStackEntry.oldIdx = oldIdx; 773 trigStackEntry.pTab = pTab; 774 trigStackEntry.pNext = pParse->trigStack; 775 trigStackEntry.ignoreJump = ignoreJump; 776 pParse->trigStack = &trigStackEntry; 777 sqlite3AuthContextPush(pParse, &sContext, pTrigger->name); 778 779 /* code the WHEN clause */ 780 endTrigger = sqlite3VdbeMakeLabel(pParse->pVdbe); 781 whenExpr = sqlite3ExprDup(pTrigger->pWhen); 782 if( sqlite3ExprResolveNames(&sNC, whenExpr) ){ 783 pParse->trigStack = trigStackEntry.pNext; 784 sqlite3ExprDelete(whenExpr); 785 return 1; 786 } 787 sqlite3ExprIfFalse(pParse, whenExpr, endTrigger, 1); 788 sqlite3ExprDelete(whenExpr); 789 790 codeTriggerProgram(pParse, pTrigger->step_list, orconf); 791 792 /* Pop the entry off the trigger stack */ 793 pParse->trigStack = trigStackEntry.pNext; 794 sqlite3AuthContextPop(&sContext); 795 796 sqlite3VdbeResolveLabel(pParse->pVdbe, endTrigger); 797 } 798 pTrigger = pTrigger->pNext; 799 } 800 return 0; 801 } 802 #endif /* !defined(SQLITE_OMIT_TRIGGER) */ 803