Update contrib.
3 ** The author disclaims copyright to this source code. In place of
4 ** a legal notice, here is a blessing:
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.
10 *************************************************************************
13 ** $Id: trigger.c,v 1.129 2008/08/20 16:35:10 drh Exp $
15 #include "sqliteInt.h"
17 #ifndef SQLITE_OMIT_TRIGGER
19 ** Delete a linked list of TriggerStep structures.
21 void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
22 while( pTriggerStep ){
23 TriggerStep * pTmp = pTriggerStep;
24 pTriggerStep = pTriggerStep->pNext;
26 if( pTmp->target.dyn ) sqlite3DbFree(db, (char*)pTmp->target.z);
27 sqlite3ExprDelete(db, pTmp->pWhere);
28 sqlite3ExprListDelete(db, pTmp->pExprList);
29 sqlite3SelectDelete(db, pTmp->pSelect);
30 sqlite3IdListDelete(db, pTmp->pIdList);
32 sqlite3DbFree(db, pTmp);
37 ** This is called by the parser when it sees a CREATE TRIGGER statement
38 ** up to the point of the BEGIN before the trigger actions. A Trigger
39 ** structure is generated based on the information available and stored
40 ** in pParse->pNewTrigger. After the trigger actions have been parsed, the
41 ** sqlite3FinishTrigger() function is called to complete the trigger
42 ** construction process.
44 void sqlite3BeginTrigger(
45 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
46 Token *pName1, /* The name of the trigger */
47 Token *pName2, /* The name of the trigger */
48 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
49 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
50 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
51 SrcList *pTableName,/* The name of the table/view the trigger applies to */
52 Expr *pWhen, /* WHEN clause */
53 int isTemp, /* True if the TEMPORARY keyword is present */
54 int noErr /* Suppress errors if the trigger already exists */
56 Trigger *pTrigger = 0;
58 char *zName = 0; /* Name of the trigger */
59 sqlite3 *db = pParse->db;
60 int iDb; /* The database to store the trigger in */
61 Token *pName; /* The unqualified db name */
65 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
68 /* If TEMP was specified, then the trigger name may not be qualified. */
70 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
76 /* Figure out the db that the the trigger will be created in */
77 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
83 /* If the trigger name was unqualified, and the table is a temp table,
84 ** then set iDb to 1 to create the trigger in the temporary database.
85 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
86 ** exist, the error is caught by the block below.
88 if( !pTableName || db->mallocFailed ){
91 pTab = sqlite3SrcListLookup(pParse, pTableName);
92 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
96 /* Ensure the table name matches database name and that the table exists */
97 if( db->mallocFailed ) goto trigger_cleanup;
98 assert( pTableName->nSrc==1 );
99 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) &&
100 sqlite3FixSrcList(&sFix, pTableName) ){
101 goto trigger_cleanup;
103 pTab = sqlite3SrcListLookup(pParse, pTableName);
105 /* The table does not exist. */
106 goto trigger_cleanup;
108 if( IsVirtual(pTab) ){
109 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
110 goto trigger_cleanup;
113 /* Check that the trigger name is not reserved and that no trigger of the
114 ** specified name exists */
115 zName = sqlite3NameFromToken(db, pName);
116 if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
117 goto trigger_cleanup;
119 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash), zName,strlen(zName)) ){
121 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
123 goto trigger_cleanup;
126 /* Do not create a trigger on a system table */
127 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
128 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
130 goto trigger_cleanup;
133 /* INSTEAD of triggers are only for views and views only support INSTEAD
136 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
137 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
138 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
139 goto trigger_cleanup;
141 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
142 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
143 " trigger on table: %S", pTableName, 0);
144 goto trigger_cleanup;
146 iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
148 #ifndef SQLITE_OMIT_AUTHORIZATION
150 int code = SQLITE_CREATE_TRIGGER;
151 const char *zDb = db->aDb[iTabDb].zName;
152 const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
153 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
154 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
155 goto trigger_cleanup;
157 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
158 goto trigger_cleanup;
163 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
164 ** cannot appear on views. So we might as well translate every
165 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
168 if (tr_tm == TK_INSTEAD){
172 /* Build the Trigger object */
173 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
174 if( pTrigger==0 ) goto trigger_cleanup;
175 pTrigger->name = zName;
177 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
178 pTrigger->pSchema = db->aDb[iDb].pSchema;
179 pTrigger->pTabSchema = pTab->pSchema;
181 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
182 pTrigger->pWhen = sqlite3ExprDup(db, pWhen);
183 pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
184 sqlite3TokenCopy(db, &pTrigger->nameToken,pName);
185 assert( pParse->pNewTrigger==0 );
186 pParse->pNewTrigger = pTrigger;
189 sqlite3DbFree(db, zName);
190 sqlite3SrcListDelete(db, pTableName);
191 sqlite3IdListDelete(db, pColumns);
192 sqlite3ExprDelete(db, pWhen);
193 if( !pParse->pNewTrigger ){
194 sqlite3DeleteTrigger(db, pTrigger);
196 assert( pParse->pNewTrigger==pTrigger );
201 ** This routine is called after all of the trigger actions have been parsed
202 ** in order to complete the process of building the trigger.
204 void sqlite3FinishTrigger(
205 Parse *pParse, /* Parser context */
206 TriggerStep *pStepList, /* The triggered program */
207 Token *pAll /* Token that describes the complete CREATE TRIGGER */
209 Trigger *pTrig = 0; /* The trigger whose construction is finishing up */
210 sqlite3 *db = pParse->db; /* The database */
212 int iDb; /* Database containing the trigger */
214 pTrig = pParse->pNewTrigger;
215 pParse->pNewTrigger = 0;
216 if( pParse->nErr || !pTrig ) goto triggerfinish_cleanup;
217 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
218 pTrig->step_list = pStepList;
220 pStepList->pTrig = pTrig;
221 pStepList = pStepList->pNext;
223 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &pTrig->nameToken)
224 && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
225 goto triggerfinish_cleanup;
228 /* if we are not initializing, and this trigger is not on a TEMP table,
229 ** build the sqlite_master entry
231 if( !db->init.busy ){
235 /* Make an entry in the sqlite_master table */
236 v = sqlite3GetVdbe(pParse);
237 if( v==0 ) goto triggerfinish_cleanup;
238 sqlite3BeginWriteOperation(pParse, 0, iDb);
239 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
240 sqlite3NestedParse(pParse,
241 "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
242 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pTrig->name,
244 sqlite3DbFree(db, z);
245 sqlite3ChangeCookie(pParse, iDb);
246 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, sqlite3MPrintf(
247 db, "type='trigger' AND name='%q'", pTrig->name), P4_DYNAMIC
255 pDel = sqlite3HashInsert(&db->aDb[iDb].pSchema->trigHash,
256 pTrig->name, strlen(pTrig->name), pTrig);
258 assert( pDel==pTrig );
259 db->mallocFailed = 1;
260 goto triggerfinish_cleanup;
262 n = strlen(pTrig->table) + 1;
263 pTab = sqlite3HashFind(&pTrig->pTabSchema->tblHash, pTrig->table, n);
265 pTrig->pNext = pTab->pTrigger;
266 pTab->pTrigger = pTrig;
270 triggerfinish_cleanup:
271 sqlite3DeleteTrigger(db, pTrig);
272 assert( !pParse->pNewTrigger );
273 sqlite3DeleteTriggerStep(db, pStepList);
277 ** Make a copy of all components of the given trigger step. This has
278 ** the effect of copying all Expr.token.z values into memory obtained
279 ** from sqlite3_malloc(). As initially created, the Expr.token.z values
280 ** all point to the input string that was fed to the parser. But that
281 ** string is ephemeral - it will go away as soon as the sqlite3_exec()
282 ** call that started the parser exits. This routine makes a persistent
283 ** copy of all the Expr.token.z strings so that the TriggerStep structure
284 ** will be valid even after the sqlite3_exec() call returns.
286 static void sqlitePersistTriggerStep(sqlite3 *db, TriggerStep *p){
288 p->target.z = (u8*)sqlite3DbStrNDup(db, (char*)p->target.z, p->target.n);
292 Select *pNew = sqlite3SelectDup(db, p->pSelect);
293 sqlite3SelectDelete(db, p->pSelect);
297 Expr *pNew = sqlite3ExprDup(db, p->pWhere);
298 sqlite3ExprDelete(db, p->pWhere);
302 ExprList *pNew = sqlite3ExprListDup(db, p->pExprList);
303 sqlite3ExprListDelete(db, p->pExprList);
307 IdList *pNew = sqlite3IdListDup(db, p->pIdList);
308 sqlite3IdListDelete(db, p->pIdList);
314 ** Turn a SELECT statement (that the pSelect parameter points to) into
315 ** a trigger step. Return a pointer to a TriggerStep structure.
317 ** The parser calls this routine when it finds a SELECT statement in
318 ** body of a TRIGGER.
320 TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
321 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
322 if( pTriggerStep==0 ) {
323 sqlite3SelectDelete(db, pSelect);
327 pTriggerStep->op = TK_SELECT;
328 pTriggerStep->pSelect = pSelect;
329 pTriggerStep->orconf = OE_Default;
330 sqlitePersistTriggerStep(db, pTriggerStep);
336 ** Build a trigger step out of an INSERT statement. Return a pointer
337 ** to the new trigger step.
339 ** The parser calls this routine when it sees an INSERT inside the
340 ** body of a trigger.
342 TriggerStep *sqlite3TriggerInsertStep(
343 sqlite3 *db, /* The database connection */
344 Token *pTableName, /* Name of the table into which we insert */
345 IdList *pColumn, /* List of columns in pTableName to insert into */
346 ExprList *pEList, /* The VALUE clause: a list of values to be inserted */
347 Select *pSelect, /* A SELECT statement that supplies values */
348 int orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
350 TriggerStep *pTriggerStep;
352 assert(pEList == 0 || pSelect == 0);
353 assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
355 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
357 pTriggerStep->op = TK_INSERT;
358 pTriggerStep->pSelect = pSelect;
359 pTriggerStep->target = *pTableName;
360 pTriggerStep->pIdList = pColumn;
361 pTriggerStep->pExprList = pEList;
362 pTriggerStep->orconf = orconf;
363 sqlitePersistTriggerStep(db, pTriggerStep);
365 sqlite3IdListDelete(db, pColumn);
366 sqlite3ExprListDelete(db, pEList);
367 sqlite3SelectDelete(db, pSelect);
374 ** Construct a trigger step that implements an UPDATE statement and return
375 ** a pointer to that trigger step. The parser calls this routine when it
376 ** sees an UPDATE statement inside the body of a CREATE TRIGGER.
378 TriggerStep *sqlite3TriggerUpdateStep(
379 sqlite3 *db, /* The database connection */
380 Token *pTableName, /* Name of the table to be updated */
381 ExprList *pEList, /* The SET clause: list of column and new values */
382 Expr *pWhere, /* The WHERE clause */
383 int orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
385 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
386 if( pTriggerStep==0 ){
387 sqlite3ExprListDelete(db, pEList);
388 sqlite3ExprDelete(db, pWhere);
392 pTriggerStep->op = TK_UPDATE;
393 pTriggerStep->target = *pTableName;
394 pTriggerStep->pExprList = pEList;
395 pTriggerStep->pWhere = pWhere;
396 pTriggerStep->orconf = orconf;
397 sqlitePersistTriggerStep(db, pTriggerStep);
403 ** Construct a trigger step that implements a DELETE statement and return
404 ** a pointer to that trigger step. The parser calls this routine when it
405 ** sees a DELETE statement inside the body of a CREATE TRIGGER.
407 TriggerStep *sqlite3TriggerDeleteStep(
408 sqlite3 *db, /* Database connection */
409 Token *pTableName, /* The table from which rows are deleted */
410 Expr *pWhere /* The WHERE clause */
412 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
413 if( pTriggerStep==0 ){
414 sqlite3ExprDelete(db, pWhere);
418 pTriggerStep->op = TK_DELETE;
419 pTriggerStep->target = *pTableName;
420 pTriggerStep->pWhere = pWhere;
421 pTriggerStep->orconf = OE_Default;
422 sqlitePersistTriggerStep(db, pTriggerStep);
428 ** Recursively delete a Trigger structure
430 void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
431 if( pTrigger==0 ) return;
432 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
433 sqlite3DbFree(db, pTrigger->name);
434 sqlite3DbFree(db, pTrigger->table);
435 sqlite3ExprDelete(db, pTrigger->pWhen);
436 sqlite3IdListDelete(db, pTrigger->pColumns);
437 if( pTrigger->nameToken.dyn ) sqlite3DbFree(db, (char*)pTrigger->nameToken.z);
438 sqlite3DbFree(db, pTrigger);
442 ** This function is called to drop a trigger from the database schema.
444 ** This may be called directly from the parser and therefore identifies
445 ** the trigger by name. The sqlite3DropTriggerPtr() routine does the
446 ** same job as this routine except it takes a pointer to the trigger
447 ** instead of the trigger name.
449 void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
450 Trigger *pTrigger = 0;
455 sqlite3 *db = pParse->db;
457 if( db->mallocFailed ) goto drop_trigger_cleanup;
458 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
459 goto drop_trigger_cleanup;
462 assert( pName->nSrc==1 );
463 zDb = pName->a[0].zDatabase;
464 zName = pName->a[0].zName;
465 nName = strlen(zName);
466 for(i=OMIT_TEMPDB; i<db->nDb; i++){
467 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
468 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
469 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
470 if( pTrigger ) break;
474 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
476 goto drop_trigger_cleanup;
478 sqlite3DropTriggerPtr(pParse, pTrigger);
480 drop_trigger_cleanup:
481 sqlite3SrcListDelete(db, pName);
485 ** Return a pointer to the Table structure for the table that a trigger
488 static Table *tableOfTrigger(Trigger *pTrigger){
489 int n = strlen(pTrigger->table) + 1;
490 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
495 ** Drop a trigger given a pointer to that trigger.
497 void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
500 sqlite3 *db = pParse->db;
503 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
504 assert( iDb>=0 && iDb<db->nDb );
505 pTable = tableOfTrigger(pTrigger);
507 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
508 #ifndef SQLITE_OMIT_AUTHORIZATION
510 int code = SQLITE_DROP_TRIGGER;
511 const char *zDb = db->aDb[iDb].zName;
512 const char *zTab = SCHEMA_TABLE(iDb);
513 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
514 if( sqlite3AuthCheck(pParse, code, pTrigger->name, pTable->zName, zDb) ||
515 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
521 /* Generate code to destroy the database record of the trigger.
524 if( (v = sqlite3GetVdbe(pParse))!=0 ){
526 static const VdbeOpList dropTrigger[] = {
527 { OP_Rewind, 0, ADDR(9), 0},
528 { OP_String8, 0, 1, 0}, /* 1 */
529 { OP_Column, 0, 1, 2},
530 { OP_Ne, 2, ADDR(8), 1},
531 { OP_String8, 0, 1, 0}, /* 4: "trigger" */
532 { OP_Column, 0, 0, 2},
533 { OP_Ne, 2, ADDR(8), 1},
534 { OP_Delete, 0, 0, 0},
535 { OP_Next, 0, ADDR(1), 0}, /* 8 */
538 sqlite3BeginWriteOperation(pParse, 0, iDb);
539 sqlite3OpenMasterTable(pParse, iDb);
540 base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger);
541 sqlite3VdbeChangeP4(v, base+1, pTrigger->name, 0);
542 sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
543 sqlite3ChangeCookie(pParse, iDb);
544 sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
545 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->name, 0);
550 ** Remove a trigger from the hash tables of the sqlite* pointer.
552 void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
554 int nName = strlen(zName);
555 pTrigger = sqlite3HashInsert(&(db->aDb[iDb].pSchema->trigHash),
558 Table *pTable = tableOfTrigger(pTrigger);
560 if( pTable->pTrigger == pTrigger ){
561 pTable->pTrigger = pTrigger->pNext;
563 Trigger *cc = pTable->pTrigger;
565 if( cc->pNext == pTrigger ){
566 cc->pNext = cc->pNext->pNext;
573 sqlite3DeleteTrigger(db, pTrigger);
574 db->flags |= SQLITE_InternChanges;
579 ** pEList is the SET clause of an UPDATE statement. Each entry
580 ** in pEList is of the format <id>=<expr>. If any of the entries
581 ** in pEList have an <id> which matches an identifier in pIdList,
582 ** then return TRUE. If pIdList==NULL, then it is considered a
583 ** wildcard that matches anything. Likewise if pEList==NULL then
584 ** it matches anything so always return true. Return false only
585 ** if there is no match.
587 static int checkColumnOverLap(IdList *pIdList, ExprList *pEList){
589 if( !pIdList || !pEList ) return 1;
590 for(e=0; e<pEList->nExpr; e++){
591 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
597 ** Return a bit vector to indicate what kind of triggers exist for operation
598 ** "op" on table pTab. If pChanges is not NULL then it is a list of columns
599 ** that are being updated. Triggers only match if the ON clause of the
600 ** trigger definition overlaps the set of columns being updated.
602 ** The returned bit vector is some combination of TRIGGER_BEFORE and
605 int sqlite3TriggersExist(
606 Parse *pParse, /* Used to check for recursive triggers */
607 Table *pTab, /* The table the contains the triggers */
608 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
609 ExprList *pChanges /* Columns that change in an UPDATE statement */
614 pTrigger = IsVirtual(pTab) ? 0 : pTab->pTrigger;
616 if( pTrigger->op==op && checkColumnOverLap(pTrigger->pColumns, pChanges) ){
617 mask |= pTrigger->tr_tm;
619 pTrigger = pTrigger->pNext;
625 ** Convert the pStep->target token into a SrcList and return a pointer
628 ** This routine adds a specific database name, if needed, to the target when
629 ** forming the SrcList. This prevents a trigger in one database from
630 ** referring to a target in another database. An exception is when the
631 ** trigger is in TEMP in which case it can refer to any other database it
634 static SrcList *targetSrcList(
635 Parse *pParse, /* The parsing context */
636 TriggerStep *pStep /* The trigger containing the target token */
638 Token sDb; /* Dummy database name token */
639 int iDb; /* Index of the database to use */
640 SrcList *pSrc; /* SrcList to be returned */
642 iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
643 if( iDb==0 || iDb>=2 ){
644 assert( iDb<pParse->db->nDb );
645 sDb.z = (u8*)pParse->db->aDb[iDb].zName;
646 sDb.n = strlen((char*)sDb.z);
647 pSrc = sqlite3SrcListAppend(pParse->db, 0, &sDb, &pStep->target);
649 pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
655 ** Generate VDBE code for zero or more statements inside the body of a
658 static int codeTriggerProgram(
659 Parse *pParse, /* The parser context */
660 TriggerStep *pStepList, /* List of statements inside the trigger body */
661 int orconfin /* Conflict algorithm. (OE_Abort, etc) */
663 TriggerStep * pTriggerStep = pStepList;
665 Vdbe *v = pParse->pVdbe;
666 sqlite3 *db = pParse->db;
668 assert( pTriggerStep!=0 );
670 sqlite3VdbeAddOp2(v, OP_ContextPush, 0, 0);
671 VdbeComment((v, "begin trigger %s", pStepList->pTrig->name));
672 while( pTriggerStep ){
673 orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin;
674 pParse->trigStack->orconf = orconf;
675 switch( pTriggerStep->op ){
677 Select *ss = sqlite3SelectDup(db, pTriggerStep->pSelect);
681 sqlite3SelectDestInit(&dest, SRT_Discard, 0);
682 sqlite3Select(pParse, ss, &dest);
683 sqlite3SelectDelete(db, ss);
689 pSrc = targetSrcList(pParse, pTriggerStep);
690 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
691 sqlite3Update(pParse, pSrc,
692 sqlite3ExprListDup(db, pTriggerStep->pExprList),
693 sqlite3ExprDup(db, pTriggerStep->pWhere), orconf);
694 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
699 pSrc = targetSrcList(pParse, pTriggerStep);
700 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
701 sqlite3Insert(pParse, pSrc,
702 sqlite3ExprListDup(db, pTriggerStep->pExprList),
703 sqlite3SelectDup(db, pTriggerStep->pSelect),
704 sqlite3IdListDup(db, pTriggerStep->pIdList), orconf);
705 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
710 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
711 pSrc = targetSrcList(pParse, pTriggerStep);
712 sqlite3DeleteFrom(pParse, pSrc,
713 sqlite3ExprDup(db, pTriggerStep->pWhere));
714 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
720 pTriggerStep = pTriggerStep->pNext;
722 sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
723 VdbeComment((v, "end trigger %s", pStepList->pTrig->name));
729 ** This is called to code FOR EACH ROW triggers.
731 ** When the code that this function generates is executed, the following
734 ** 1. No cursors may be open in the main database. (But newIdx and oldIdx
735 ** can be indices of cursors in temporary tables. See below.)
737 ** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then
738 ** a temporary vdbe cursor (index newIdx) must be open and pointing at
739 ** a row containing values to be substituted for new.* expressions in the
740 ** trigger program(s).
742 ** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then
743 ** a temporary vdbe cursor (index oldIdx) must be open and pointing at
744 ** a row containing values to be substituted for old.* expressions in the
745 ** trigger program(s).
747 ** If they are not NULL, the piOldColMask and piNewColMask output variables
748 ** are set to values that describe the columns used by the trigger program
749 ** in the OLD.* and NEW.* tables respectively. If column N of the
750 ** pseudo-table is read at least once, the corresponding bit of the output
751 ** mask is set. If a column with an index greater than 32 is read, the
752 ** output mask is set to the special value 0xffffffff.
755 int sqlite3CodeRowTrigger(
756 Parse *pParse, /* Parse context */
757 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
758 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
759 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
760 Table *pTab, /* The table to code triggers from */
761 int newIdx, /* The indice of the "new" row to access */
762 int oldIdx, /* The indice of the "old" row to access */
763 int orconf, /* ON CONFLICT policy */
764 int ignoreJump, /* Instruction to jump to for RAISE(IGNORE) */
765 u32 *piOldColMask, /* OUT: Mask of columns used from the OLD.* table */
766 u32 *piNewColMask /* OUT: Mask of columns used from the NEW.* table */
769 sqlite3 *db = pParse->db;
770 TriggerStack trigStackEntry;
772 trigStackEntry.oldColMask = 0;
773 trigStackEntry.newColMask = 0;
775 assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE);
776 assert(tr_tm == TRIGGER_BEFORE || tr_tm == TRIGGER_AFTER );
778 assert(newIdx != -1 || oldIdx != -1);
780 for(p=pTab->pTrigger; p; p=p->pNext){
783 /* Determine whether we should code this trigger */
787 (p->pSchema==p->pTabSchema || p->pSchema==db->aDb[1].pSchema) &&
788 (op!=TK_UPDATE||!p->pColumns||checkColumnOverLap(p->pColumns,pChanges))
790 TriggerStack *pS; /* Pointer to trigger-stack entry */
791 for(pS=pParse->trigStack; pS && p!=pS->pTrigger; pS=pS->pNext){}
795 #if 0 /* Give no warning for recursive triggers. Just do not do them */
797 sqlite3ErrorMsg(pParse, "recursive triggers not supported (%s)",
807 AuthContext sContext;
810 #ifndef SQLITE_OMIT_TRACE
811 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Trace, 0, 0, 0,
812 sqlite3MPrintf(db, "-- TRIGGER %s", p->name),
815 memset(&sNC, 0, sizeof(sNC));
818 /* Push an entry on to the trigger stack */
819 trigStackEntry.pTrigger = p;
820 trigStackEntry.newIdx = newIdx;
821 trigStackEntry.oldIdx = oldIdx;
822 trigStackEntry.pTab = pTab;
823 trigStackEntry.pNext = pParse->trigStack;
824 trigStackEntry.ignoreJump = ignoreJump;
825 pParse->trigStack = &trigStackEntry;
826 sqlite3AuthContextPush(pParse, &sContext, p->name);
828 /* code the WHEN clause */
829 endTrigger = sqlite3VdbeMakeLabel(pParse->pVdbe);
830 whenExpr = sqlite3ExprDup(db, p->pWhen);
831 if( db->mallocFailed || sqlite3ResolveExprNames(&sNC, whenExpr) ){
832 pParse->trigStack = trigStackEntry.pNext;
833 sqlite3ExprDelete(db, whenExpr);
836 sqlite3ExprIfFalse(pParse, whenExpr, endTrigger, SQLITE_JUMPIFNULL);
837 sqlite3ExprDelete(db, whenExpr);
839 codeTriggerProgram(pParse, p->step_list, orconf);
841 /* Pop the entry off the trigger stack */
842 pParse->trigStack = trigStackEntry.pNext;
843 sqlite3AuthContextPop(&sContext);
845 sqlite3VdbeResolveLabel(pParse->pVdbe, endTrigger);
848 if( piOldColMask ) *piOldColMask |= trigStackEntry.oldColMask;
849 if( piNewColMask ) *piNewColMask |= trigStackEntry.newColMask;
852 #endif /* !defined(SQLITE_OMIT_TRIGGER) */