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
#include "sqliteInt.h"
15
#ifndef SQLITE_OMIT_TRIGGER
17
** Delete a linked list of TriggerStep structures.
19
void sqlite3DeleteTriggerStep(TriggerStep *pTriggerStep){
20
while( pTriggerStep ){
21
TriggerStep * pTmp = pTriggerStep;
22
pTriggerStep = pTriggerStep->pNext;
24
if( pTmp->target.dyn ) sqliteFree((char*)pTmp->target.z);
25
sqlite3ExprDelete(pTmp->pWhere);
26
sqlite3ExprListDelete(pTmp->pExprList);
27
sqlite3SelectDelete(pTmp->pSelect);
28
sqlite3IdListDelete(pTmp->pIdList);
35
** This is called by the parser when it sees a CREATE TRIGGER statement
36
** up to the point of the BEGIN before the trigger actions. A Trigger
37
** structure is generated based on the information available and stored
38
** in pParse->pNewTrigger. After the trigger actions have been parsed, the
39
** sqlite3FinishTrigger() function is called to complete the trigger
40
** construction process.
42
void sqlite3BeginTrigger(
43
Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
44
Token *pName1, /* The name of the trigger */
45
Token *pName2, /* The name of the trigger */
46
int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
47
int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
48
IdList *pColumns, /* column list if this is an UPDATE OF trigger */
49
SrcList *pTableName,/* The name of the table/view the trigger applies to */
50
int foreach, /* One of TK_ROW or TK_STATEMENT */
51
Expr *pWhen, /* WHEN clause */
52
int isTemp /* True if the TEMPORARY keyword is present */
54
Trigger *pTrigger = 0;
56
char *zName = 0; /* Name of the trigger */
57
sqlite3 *db = pParse->db;
58
int iDb; /* The database to store the trigger in */
59
Token *pName; /* The unqualified db name */
63
/* If TEMP was specified, then the trigger name may not be qualified. */
64
if( pName2 && pName2->n>0 ){
65
sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
71
/* Figure out the db that the the trigger will be created in */
72
iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
78
/* If the trigger name was unqualified, and the table is a temp table,
79
** then set iDb to 1 to create the trigger in the temporary database.
80
** If sqlite3SrcListLookup() returns 0, indicating the table does not
81
** exist, the error is caught by the block below.
83
if( !pTableName || sqlite3_malloc_failed ) goto trigger_cleanup;
84
pTab = sqlite3SrcListLookup(pParse, pTableName);
85
if( pName2->n==0 && pTab && pTab->iDb==1 ){
89
/* Ensure the table name matches database name and that the table exists */
90
if( sqlite3_malloc_failed ) goto trigger_cleanup;
91
assert( pTableName->nSrc==1 );
92
if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) &&
93
sqlite3FixSrcList(&sFix, pTableName) ){
96
pTab = sqlite3SrcListLookup(pParse, pTableName);
98
/* The table does not exist. */
102
/* Check that the trigger name is not reserved and that no trigger of the
103
** specified name exists */
104
zName = sqlite3NameFromToken(pName);
105
if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
106
goto trigger_cleanup;
108
if( sqlite3HashFind(&(db->aDb[iDb].trigHash), zName,pName->n+1) ){
109
sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
110
goto trigger_cleanup;
113
/* Do not create a trigger on a system table */
114
if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
115
sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
117
goto trigger_cleanup;
120
/* INSTEAD of triggers are only for views and views only support INSTEAD
123
if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
124
sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
125
(tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
126
goto trigger_cleanup;
128
if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
129
sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
130
" trigger on table: %S", pTableName, 0);
131
goto trigger_cleanup;
134
#ifndef SQLITE_OMIT_AUTHORIZATION
136
int code = SQLITE_CREATE_TRIGGER;
137
const char *zDb = db->aDb[pTab->iDb].zName;
138
const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
139
if( pTab->iDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
140
if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
141
goto trigger_cleanup;
143
if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(pTab->iDb),0,zDb)){
144
goto trigger_cleanup;
149
/* INSTEAD OF triggers can only appear on views and BEFORE triggers
150
** cannot appear on views. So we might as well translate every
151
** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
154
if (tr_tm == TK_INSTEAD){
158
/* Build the Trigger object */
159
pTrigger = (Trigger*)sqliteMalloc(sizeof(Trigger));
160
if( pTrigger==0 ) goto trigger_cleanup;
161
pTrigger->name = zName;
163
pTrigger->table = sqliteStrDup(pTableName->a[0].zName);
165
pTrigger->iTabDb = pTab->iDb;
167
pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
168
pTrigger->pWhen = sqlite3ExprDup(pWhen);
169
pTrigger->pColumns = sqlite3IdListDup(pColumns);
170
pTrigger->foreach = foreach;
171
sqlite3TokenCopy(&pTrigger->nameToken,pName);
172
assert( pParse->pNewTrigger==0 );
173
pParse->pNewTrigger = pTrigger;
177
sqlite3SrcListDelete(pTableName);
178
sqlite3IdListDelete(pColumns);
179
sqlite3ExprDelete(pWhen);
180
if( !pParse->pNewTrigger ){
181
sqlite3DeleteTrigger(pTrigger);
183
assert( pParse->pNewTrigger==pTrigger );
188
** This routine is called after all of the trigger actions have been parsed
189
** in order to complete the process of building the trigger.
191
void sqlite3FinishTrigger(
192
Parse *pParse, /* Parser context */
193
TriggerStep *pStepList, /* The triggered program */
194
Token *pAll /* Token that describes the complete CREATE TRIGGER */
196
Trigger *pTrig = 0; /* The trigger whose construction is finishing up */
197
sqlite3 *db = pParse->db; /* The database */
200
pTrig = pParse->pNewTrigger;
201
pParse->pNewTrigger = 0;
202
if( pParse->nErr || pTrig==0 ) goto triggerfinish_cleanup;
203
pTrig->step_list = pStepList;
205
pStepList->pTrig = pTrig;
206
pStepList = pStepList->pNext;
208
if( sqlite3FixInit(&sFix, pParse, pTrig->iDb, "trigger", &pTrig->nameToken)
209
&& sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
210
goto triggerfinish_cleanup;
213
/* if we are not initializing, and this trigger is not on a TEMP table,
214
** build the sqlite_master entry
216
if( !db->init.busy ){
217
static const VdbeOpList insertTrig[] = {
218
{ OP_NewRecno, 0, 0, 0 },
219
{ OP_String8, 0, 0, "trigger" },
220
{ OP_String8, 0, 0, 0 }, /* 2: trigger name */
221
{ OP_String8, 0, 0, 0 }, /* 3: table name */
222
{ OP_Integer, 0, 0, 0 },
223
{ OP_String8, 0, 0, "CREATE TRIGGER "},
224
{ OP_String8, 0, 0, 0 }, /* 6: SQL */
225
{ OP_Concat, 0, 0, 0 },
226
{ OP_MakeRecord, 5, 0, "tttit" },
227
{ OP_PutIntKey, 0, 0, 0 },
232
/* Make an entry in the sqlite_master table */
233
v = sqlite3GetVdbe(pParse);
234
if( v==0 ) goto triggerfinish_cleanup;
235
sqlite3BeginWriteOperation(pParse, 0, pTrig->iDb);
236
sqlite3OpenMasterTable(v, pTrig->iDb);
237
addr = sqlite3VdbeAddOpList(v, ArraySize(insertTrig), insertTrig);
238
sqlite3VdbeChangeP3(v, addr+2, pTrig->name, 0);
239
sqlite3VdbeChangeP3(v, addr+3, pTrig->table, 0);
240
sqlite3VdbeChangeP3(v, addr+6, pAll->z, pAll->n);
241
sqlite3ChangeCookie(db, v, pTrig->iDb);
242
sqlite3VdbeAddOp(v, OP_Close, 0, 0);
243
sqlite3VdbeOp3(v, OP_ParseSchema, pTrig->iDb, 0,
244
sqlite3MPrintf("type='trigger' AND name='%q'", pTrig->name), P3_DYNAMIC);
250
pDel = sqlite3HashInsert(&db->aDb[pTrig->iDb].trigHash,
251
pTrig->name, strlen(pTrig->name)+1, pTrig);
253
assert( sqlite3_malloc_failed && pDel==pTrig );
254
goto triggerfinish_cleanup;
256
pTab = sqlite3LocateTable(pParse,pTrig->table,db->aDb[pTrig->iTabDb].zName);
258
pTrig->pNext = pTab->pTrigger;
259
pTab->pTrigger = pTrig;
263
triggerfinish_cleanup:
264
sqlite3DeleteTrigger(pTrig);
265
assert( !pParse->pNewTrigger );
266
sqlite3DeleteTriggerStep(pStepList);
270
** Make a copy of all components of the given trigger step. This has
271
** the effect of copying all Expr.token.z values into memory obtained
272
** from sqliteMalloc(). As initially created, the Expr.token.z values
273
** all point to the input string that was fed to the parser. But that
274
** string is ephemeral - it will go away as soon as the sqlite3_exec()
275
** call that started the parser exits. This routine makes a persistent
276
** copy of all the Expr.token.z strings so that the TriggerStep structure
277
** will be valid even after the sqlite3_exec() call returns.
279
static void sqlitePersistTriggerStep(TriggerStep *p){
281
p->target.z = sqliteStrNDup(p->target.z, p->target.n);
285
Select *pNew = sqlite3SelectDup(p->pSelect);
286
sqlite3SelectDelete(p->pSelect);
290
Expr *pNew = sqlite3ExprDup(p->pWhere);
291
sqlite3ExprDelete(p->pWhere);
295
ExprList *pNew = sqlite3ExprListDup(p->pExprList);
296
sqlite3ExprListDelete(p->pExprList);
300
IdList *pNew = sqlite3IdListDup(p->pIdList);
301
sqlite3IdListDelete(p->pIdList);
307
** Turn a SELECT statement (that the pSelect parameter points to) into
308
** a trigger step. Return a pointer to a TriggerStep structure.
310
** The parser calls this routine when it finds a SELECT statement in
311
** body of a TRIGGER.
313
TriggerStep *sqlite3TriggerSelectStep(Select *pSelect){
314
TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
315
if( pTriggerStep==0 ) return 0;
317
pTriggerStep->op = TK_SELECT;
318
pTriggerStep->pSelect = pSelect;
319
pTriggerStep->orconf = OE_Default;
320
sqlitePersistTriggerStep(pTriggerStep);
326
** Build a trigger step out of an INSERT statement. Return a pointer
327
** to the new trigger step.
329
** The parser calls this routine when it sees an INSERT inside the
330
** body of a trigger.
332
TriggerStep *sqlite3TriggerInsertStep(
333
Token *pTableName, /* Name of the table into which we insert */
334
IdList *pColumn, /* List of columns in pTableName to insert into */
335
ExprList *pEList, /* The VALUE clause: a list of values to be inserted */
336
Select *pSelect, /* A SELECT statement that supplies values */
337
int orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
339
TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
341
assert(pEList == 0 || pSelect == 0);
342
assert(pEList != 0 || pSelect != 0);
345
pTriggerStep->op = TK_INSERT;
346
pTriggerStep->pSelect = pSelect;
347
pTriggerStep->target = *pTableName;
348
pTriggerStep->pIdList = pColumn;
349
pTriggerStep->pExprList = pEList;
350
pTriggerStep->orconf = orconf;
351
sqlitePersistTriggerStep(pTriggerStep);
353
sqlite3IdListDelete(pColumn);
354
sqlite3ExprListDelete(pEList);
355
sqlite3SelectDup(pSelect);
362
** Construct a trigger step that implements an UPDATE statement and return
363
** a pointer to that trigger step. The parser calls this routine when it
364
** sees an UPDATE statement inside the body of a CREATE TRIGGER.
366
TriggerStep *sqlite3TriggerUpdateStep(
367
Token *pTableName, /* Name of the table to be updated */
368
ExprList *pEList, /* The SET clause: list of column and new values */
369
Expr *pWhere, /* The WHERE clause */
370
int orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
372
TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
373
if( pTriggerStep==0 ) return 0;
375
pTriggerStep->op = TK_UPDATE;
376
pTriggerStep->target = *pTableName;
377
pTriggerStep->pExprList = pEList;
378
pTriggerStep->pWhere = pWhere;
379
pTriggerStep->orconf = orconf;
380
sqlitePersistTriggerStep(pTriggerStep);
386
** Construct a trigger step that implements a DELETE statement and return
387
** a pointer to that trigger step. The parser calls this routine when it
388
** sees a DELETE statement inside the body of a CREATE TRIGGER.
390
TriggerStep *sqlite3TriggerDeleteStep(Token *pTableName, Expr *pWhere){
391
TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
392
if( pTriggerStep==0 ) return 0;
394
pTriggerStep->op = TK_DELETE;
395
pTriggerStep->target = *pTableName;
396
pTriggerStep->pWhere = pWhere;
397
pTriggerStep->orconf = OE_Default;
398
sqlitePersistTriggerStep(pTriggerStep);
404
** Recursively delete a Trigger structure
406
void sqlite3DeleteTrigger(Trigger *pTrigger){
407
if( pTrigger==0 ) return;
408
sqlite3DeleteTriggerStep(pTrigger->step_list);
409
sqliteFree(pTrigger->name);
410
sqliteFree(pTrigger->table);
411
sqlite3ExprDelete(pTrigger->pWhen);
412
sqlite3IdListDelete(pTrigger->pColumns);
413
if( pTrigger->nameToken.dyn ) sqliteFree((char*)pTrigger->nameToken.z);
414
sqliteFree(pTrigger);
418
** This function is called to drop a trigger from the database schema.
420
** This may be called directly from the parser and therefore identifies
421
** the trigger by name. The sqlite3DropTriggerPtr() routine does the
422
** same job as this routine except it takes a pointer to the trigger
423
** instead of the trigger name.
425
void sqlite3DropTrigger(Parse *pParse, SrcList *pName){
426
Trigger *pTrigger = 0;
431
sqlite3 *db = pParse->db;
433
if( sqlite3_malloc_failed ) goto drop_trigger_cleanup;
434
if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
435
goto drop_trigger_cleanup;
438
assert( pName->nSrc==1 );
439
zDb = pName->a[0].zDatabase;
440
zName = pName->a[0].zName;
441
nName = strlen(zName);
442
for(i=OMIT_TEMPDB; i<db->nDb; i++){
443
int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
444
if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
445
pTrigger = sqlite3HashFind(&(db->aDb[j].trigHash), zName, nName+1);
446
if( pTrigger ) break;
449
sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
450
goto drop_trigger_cleanup;
452
sqlite3DropTriggerPtr(pParse, pTrigger, 0);
454
drop_trigger_cleanup:
455
sqlite3SrcListDelete(pName);
459
** Return a pointer to the Table structure for the table that a trigger
462
static Table *tableOfTrigger(sqlite3 *db, Trigger *pTrigger){
463
return sqlite3FindTable(db,pTrigger->table,db->aDb[pTrigger->iTabDb].zName);
468
** Drop a trigger given a pointer to that trigger. If nested is false,
469
** then also generate code to remove the trigger from the SQLITE_MASTER
472
void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger, int nested){
475
sqlite3 *db = pParse->db;
479
assert( iDb>=0 && iDb<db->nDb );
480
pTable = tableOfTrigger(db, pTrigger);
482
assert( pTable->iDb==iDb || iDb==1 );
483
#ifndef SQLITE_OMIT_AUTHORIZATION
485
int code = SQLITE_DROP_TRIGGER;
486
const char *zDb = db->aDb[iDb].zName;
487
const char *zTab = SCHEMA_TABLE(iDb);
488
if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
489
if( sqlite3AuthCheck(pParse, code, pTrigger->name, pTable->zName, zDb) ||
490
sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
496
/* Generate code to destroy the database record of the trigger.
498
if( pTable!=0 && (v = sqlite3GetVdbe(pParse))!=0 ){
500
static const VdbeOpList dropTrigger[] = {
501
{ OP_Rewind, 0, ADDR(9), 0},
502
{ OP_String8, 0, 0, 0}, /* 1 */
503
{ OP_Column, 0, 1, 0},
504
{ OP_Ne, 0, ADDR(8), 0},
505
{ OP_String8, 0, 0, "trigger"},
506
{ OP_Column, 0, 0, 0},
507
{ OP_Ne, 0, ADDR(8), 0},
508
{ OP_Delete, 0, 0, 0},
509
{ OP_Next, 0, ADDR(1), 0}, /* 8 */
512
sqlite3BeginWriteOperation(pParse, 0, iDb);
513
sqlite3OpenMasterTable(v, iDb);
514
base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger);
515
sqlite3VdbeChangeP3(v, base+1, pTrigger->name, 0);
516
sqlite3ChangeCookie(db, v, iDb);
517
sqlite3VdbeAddOp(v, OP_Close, 0, 0);
518
sqlite3VdbeOp3(v, OP_DropTrigger, iDb, 0, pTrigger->name, 0);
523
** Remove a trigger from the hash tables of the sqlite* pointer.
525
void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
527
int nName = strlen(zName);
528
pTrigger = sqlite3HashInsert(&(db->aDb[iDb].trigHash), zName, nName+1, 0);
530
Table *pTable = tableOfTrigger(db, pTrigger);
532
if( pTable->pTrigger == pTrigger ){
533
pTable->pTrigger = pTrigger->pNext;
535
Trigger *cc = pTable->pTrigger;
537
if( cc->pNext == pTrigger ){
538
cc->pNext = cc->pNext->pNext;
545
sqlite3DeleteTrigger(pTrigger);
546
db->flags |= SQLITE_InternChanges;
551
** pEList is the SET clause of an UPDATE statement. Each entry
552
** in pEList is of the format <id>=<expr>. If any of the entries
553
** in pEList have an <id> which matches an identifier in pIdList,
554
** then return TRUE. If pIdList==NULL, then it is considered a
555
** wildcard that matches anything. Likewise if pEList==NULL then
556
** it matches anything so always return true. Return false only
557
** if there is no match.
559
static int checkColumnOverLap(IdList *pIdList, ExprList *pEList){
561
if( !pIdList || !pEList ) return 1;
562
for(e=0; e<pEList->nExpr; e++){
563
if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
569
** Return a bit vector to indicate what kind of triggers exist for operation
570
** "op" on table pTab. If pChanges is not NULL then it is a list of columns
571
** that are being updated. Triggers only match if the ON clause of the
572
** trigger definition overlaps the set of columns being updated.
574
** The returned bit vector is some combination of TRIGGER_BEFORE and
577
int sqlite3TriggersExist(
578
Parse *pParse, /* Used to check for recursive triggers */
579
Table *pTab, /* The table the contains the triggers */
580
int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
581
ExprList *pChanges /* Columns that change in an UPDATE statement */
583
Trigger *pTrigger = pTab->pTrigger;
587
if( pTrigger->op==op && checkColumnOverLap(pTrigger->pColumns, pChanges) ){
589
ss = pParse->trigStack;
590
while( ss && ss->pTrigger!=pTab->pTrigger ){
594
mask |= pTrigger->tr_tm;
597
pTrigger = pTrigger->pNext;
603
** Convert the pStep->target token into a SrcList and return a pointer
606
** This routine adds a specific database name, if needed, to the target when
607
** forming the SrcList. This prevents a trigger in one database from
608
** referring to a target in another database. An exception is when the
609
** trigger is in TEMP in which case it can refer to any other database it
612
static SrcList *targetSrcList(
613
Parse *pParse, /* The parsing context */
614
TriggerStep *pStep /* The trigger containing the target token */
616
Token sDb; /* Dummy database name token */
617
int iDb; /* Index of the database to use */
618
SrcList *pSrc; /* SrcList to be returned */
620
iDb = pStep->pTrig->iDb;
621
if( iDb==0 || iDb>=2 ){
622
assert( iDb<pParse->db->nDb );
623
sDb.z = pParse->db->aDb[iDb].zName;
624
sDb.n = strlen(sDb.z);
625
pSrc = sqlite3SrcListAppend(0, &sDb, &pStep->target);
627
pSrc = sqlite3SrcListAppend(0, &pStep->target, 0);
633
** Generate VDBE code for zero or more statements inside the body of a
636
static int codeTriggerProgram(
637
Parse *pParse, /* The parser context */
638
TriggerStep *pStepList, /* List of statements inside the trigger body */
639
int orconfin /* Conflict algorithm. (OE_Abort, etc) */
641
TriggerStep * pTriggerStep = pStepList;
643
Vdbe *v = pParse->pVdbe;
645
assert( pTriggerStep!=0 );
647
sqlite3VdbeAddOp(v, OP_ContextPush, 0, 0);
648
VdbeComment((v, "# begin trigger %s", pStepList->pTrig->name));
649
while( pTriggerStep ){
650
orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin;
651
pParse->trigStack->orconf = orconf;
652
switch( pTriggerStep->op ){
654
Select * ss = sqlite3SelectDup(pTriggerStep->pSelect);
657
sqlite3SelectResolve(pParse, ss, 0);
658
sqlite3Select(pParse, ss, SRT_Discard, 0, 0, 0, 0, 0);
659
sqlite3SelectDelete(ss);
664
pSrc = targetSrcList(pParse, pTriggerStep);
665
sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0);
666
sqlite3Update(pParse, pSrc,
667
sqlite3ExprListDup(pTriggerStep->pExprList),
668
sqlite3ExprDup(pTriggerStep->pWhere), orconf);
669
sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0);
674
pSrc = targetSrcList(pParse, pTriggerStep);
675
sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0);
676
sqlite3Insert(pParse, pSrc,
677
sqlite3ExprListDup(pTriggerStep->pExprList),
678
sqlite3SelectDup(pTriggerStep->pSelect),
679
sqlite3IdListDup(pTriggerStep->pIdList), orconf);
680
sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0);
685
sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0);
686
pSrc = targetSrcList(pParse, pTriggerStep);
687
sqlite3DeleteFrom(pParse, pSrc, sqlite3ExprDup(pTriggerStep->pWhere));
688
sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0);
694
pTriggerStep = pTriggerStep->pNext;
696
sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
697
VdbeComment((v, "# end trigger %s", pStepList->pTrig->name));
703
** This is called to code FOR EACH ROW triggers.
705
** When the code that this function generates is executed, the following
708
** 1. No cursors may be open in the main database. (But newIdx and oldIdx
709
** can be indices of cursors in temporary tables. See below.)
711
** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then
712
** a temporary vdbe cursor (index newIdx) must be open and pointing at
713
** a row containing values to be substituted for new.* expressions in the
714
** trigger program(s).
716
** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then
717
** a temporary vdbe cursor (index oldIdx) must be open and pointing at
718
** a row containing values to be substituted for old.* expressions in the
719
** trigger program(s).
722
int sqlite3CodeRowTrigger(
723
Parse *pParse, /* Parse context */
724
int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
725
ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
726
int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
727
Table *pTab, /* The table to code triggers from */
728
int newIdx, /* The indice of the "new" row to access */
729
int oldIdx, /* The indice of the "old" row to access */
730
int orconf, /* ON CONFLICT policy */
731
int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
734
TriggerStack *pStack;
735
TriggerStack trigStackEntry;
737
assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE);
738
assert(tr_tm == TRIGGER_BEFORE || tr_tm == TRIGGER_AFTER );
740
assert(newIdx != -1 || oldIdx != -1);
742
pTrigger = pTab->pTrigger;
746
/* determine whether we should code this trigger */
747
if( pTrigger->op == op && pTrigger->tr_tm == tr_tm ){
749
for(pStack=pParse->trigStack; pStack; pStack=pStack->pNext){
750
if( pStack->pTrigger==pTrigger ){
754
if( op == TK_UPDATE && pTrigger->pColumns &&
755
!checkColumnOverLap(pTrigger->pColumns, pChanges) ){
763
AuthContext sContext;
766
memset(&sNC, 0, sizeof(sNC));
769
/* Push an entry on to the trigger stack */
770
trigStackEntry.pTrigger = pTrigger;
771
trigStackEntry.newIdx = newIdx;
772
trigStackEntry.oldIdx = oldIdx;
773
trigStackEntry.pTab = pTab;
774
trigStackEntry.pNext = pParse->trigStack;
775
trigStackEntry.ignoreJump = ignoreJump;
776
pParse->trigStack = &trigStackEntry;
777
sqlite3AuthContextPush(pParse, &sContext, pTrigger->name);
779
/* code the WHEN clause */
780
endTrigger = sqlite3VdbeMakeLabel(pParse->pVdbe);
781
whenExpr = sqlite3ExprDup(pTrigger->pWhen);
782
if( sqlite3ExprResolveNames(&sNC, whenExpr) ){
783
pParse->trigStack = trigStackEntry.pNext;
784
sqlite3ExprDelete(whenExpr);
787
sqlite3ExprIfFalse(pParse, whenExpr, endTrigger, 1);
788
sqlite3ExprDelete(whenExpr);
790
codeTriggerProgram(pParse, pTrigger->step_list, orconf);
792
/* Pop the entry off the trigger stack */
793
pParse->trigStack = trigStackEntry.pNext;
794
sqlite3AuthContextPop(&sContext);
796
sqlite3VdbeResolveLabel(pParse->pVdbe, endTrigger);
798
pTrigger = pTrigger->pNext;
802
#endif /* !defined(SQLITE_OMIT_TRIGGER) */