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