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