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