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