monotone

monotone Mtn Source Tree

Root/sqlite/trigger.c

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*
12*/
13#include "sqliteInt.h"
14
15/*
16** Delete a linked list of TriggerStep structures.
17*/
18void sqliteDeleteTriggerStep(TriggerStep *pTriggerStep){
19 while( pTriggerStep ){
20 TriggerStep * pTmp = pTriggerStep;
21 pTriggerStep = pTriggerStep->pNext;
22
23 if( pTmp->target.dyn ) sqliteFree((char*)pTmp->target.z);
24 sqliteExprDelete(pTmp->pWhere);
25 sqliteExprListDelete(pTmp->pExprList);
26 sqliteSelectDelete(pTmp->pSelect);
27 sqliteIdListDelete(pTmp->pIdList);
28
29 sqliteFree(pTmp);
30 }
31}
32
33/*
34** This is called by the parser when it sees a CREATE TRIGGER statement
35** up to the point of the BEGIN before the trigger actions. A Trigger
36** structure is generated based on the information available and stored
37** in pParse->pNewTrigger. After the trigger actions have been parsed, the
38** sqliteFinishTrigger() function is called to complete the trigger
39** construction process.
40*/
41void sqliteBeginTrigger(
42 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
43 Token *pName, /* The name of the trigger */
44 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
45 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
46 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
47 SrcList *pTableName,/* The name of the table/view the trigger applies to */
48 int foreach, /* One of TK_ROW or TK_STATEMENT */
49 Expr *pWhen, /* WHEN clause */
50 int isTemp /* True if the TEMPORARY keyword is present */
51){
52 Trigger *nt;
53 Table *tab;
54 char *zName = 0; /* Name of the trigger */
55 sqlite *db = pParse->db;
56 int iDb; /* When database to store the trigger in */
57 DbFixer sFix;
58
59 /* Check that:
60 ** 1. the trigger name does not already exist.
61 ** 2. the table (or view) does exist in the same database as the trigger.
62 ** 3. that we are not trying to create a trigger on the sqlite_master table
63 ** 4. That we are not trying to create an INSTEAD OF trigger on a table.
64 ** 5. That we are not trying to create a BEFORE or AFTER trigger on a view.
65 */
66 if( sqlite_malloc_failed ) goto trigger_cleanup;
67 assert( pTableName->nSrc==1 );
68 if( pParse->initFlag
69 && sqliteFixInit(&sFix, pParse, pParse->iDb, "trigger", pName)
70 && sqliteFixSrcList(&sFix, pTableName)
71 ){
72 goto trigger_cleanup;
73 }
74 tab = sqliteSrcListLookup(pParse, pTableName);
75 if( !tab ){
76 goto trigger_cleanup;
77 }
78 iDb = isTemp ? 1 : tab->iDb;
79 if( iDb>=2 && !pParse->initFlag ){
80 sqliteErrorMsg(pParse, "triggers may not be added to auxiliary "
81 "database %s", db->aDb[tab->iDb].zName);
82 goto trigger_cleanup;
83 }
84
85 zName = sqliteStrNDup(pName->z, pName->n);
86 if( sqliteHashFind(&(db->aDb[iDb].trigHash), zName,pName->n+1) ){
87 sqliteErrorMsg(pParse, "trigger %T already exists", pName);
88 goto trigger_cleanup;
89 }
90 if( sqliteStrNICmp(tab->zName, "sqlite_", 7)==0 ){
91 sqliteErrorMsg(pParse, "cannot create trigger on system table");
92 pParse->nErr++;
93 goto trigger_cleanup;
94 }
95 if( tab->pSelect && tr_tm != TK_INSTEAD ){
96 sqliteErrorMsg(pParse, "cannot create %s trigger on view: %S",
97 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
98 goto trigger_cleanup;
99 }
100 if( !tab->pSelect && tr_tm == TK_INSTEAD ){
101 sqliteErrorMsg(pParse, "cannot create INSTEAD OF"
102 " trigger on table: %S", pTableName, 0);
103 goto trigger_cleanup;
104 }
105#ifndef SQLITE_OMIT_AUTHORIZATION
106 {
107 int code = SQLITE_CREATE_TRIGGER;
108 const char *zDb = db->aDb[tab->iDb].zName;
109 const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
110 if( tab->iDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
111 if( sqliteAuthCheck(pParse, code, zName, tab->zName, zDbTrig) ){
112 goto trigger_cleanup;
113 }
114 if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(tab->iDb), 0, zDb)){
115 goto trigger_cleanup;
116 }
117 }
118#endif
119
120 /* INSTEAD OF triggers can only appear on views and BEGIN triggers
121 ** cannot appear on views. So we might as well translate every
122 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
123 ** elsewhere.
124 */
125 if (tr_tm == TK_INSTEAD){
126 tr_tm = TK_BEFORE;
127 }
128
129 /* Build the Trigger object */
130 nt = (Trigger*)sqliteMalloc(sizeof(Trigger));
131 if( nt==0 ) goto trigger_cleanup;
132 nt->name = zName;
133 zName = 0;
134 nt->table = sqliteStrDup(pTableName->a[0].zName);
135 if( sqlite_malloc_failed ) goto trigger_cleanup;
136 nt->iDb = iDb;
137 nt->iTabDb = tab->iDb;
138 nt->op = op;
139 nt->tr_tm = tr_tm;
140 nt->pWhen = sqliteExprDup(pWhen);
141 nt->pColumns = sqliteIdListDup(pColumns);
142 nt->foreach = foreach;
143 sqliteTokenCopy(&nt->nameToken,pName);
144 assert( pParse->pNewTrigger==0 );
145 pParse->pNewTrigger = nt;
146
147trigger_cleanup:
148 sqliteFree(zName);
149 sqliteSrcListDelete(pTableName);
150 sqliteIdListDelete(pColumns);
151 sqliteExprDelete(pWhen);
152}
153
154/*
155** This routine is called after all of the trigger actions have been parsed
156** in order to complete the process of building the trigger.
157*/
158void sqliteFinishTrigger(
159 Parse *pParse, /* Parser context */
160 TriggerStep *pStepList, /* The triggered program */
161 Token *pAll /* Token that describes the complete CREATE TRIGGER */
162){
163 Trigger *nt = 0; /* The trigger whose construction is finishing up */
164 sqlite *db = pParse->db; /* The database */
165 DbFixer sFix;
166
167 if( pParse->nErr || pParse->pNewTrigger==0 ) goto triggerfinish_cleanup;
168 nt = pParse->pNewTrigger;
169 pParse->pNewTrigger = 0;
170 nt->step_list = pStepList;
171 while( pStepList ){
172 pStepList->pTrig = nt;
173 pStepList = pStepList->pNext;
174 }
175 if( sqliteFixInit(&sFix, pParse, nt->iDb, "trigger", &nt->nameToken)
176 && sqliteFixTriggerStep(&sFix, nt->step_list) ){
177 goto triggerfinish_cleanup;
178 }
179
180 /* if we are not initializing, and this trigger is not on a TEMP table,
181 ** build the sqlite_master entry
182 */
183 if( !pParse->initFlag ){
184 static VdbeOp insertTrig[] = {
185 { OP_NewRecno, 0, 0, 0 },
186 { OP_String, 0, 0, "trigger" },
187 { OP_String, 0, 0, 0 }, /* 2: trigger name */
188 { OP_String, 0, 0, 0 }, /* 3: table name */
189 { OP_Integer, 0, 0, 0 },
190 { OP_String, 0, 0, 0 }, /* 5: SQL */
191 { OP_MakeRecord, 5, 0, 0 },
192 { OP_PutIntKey, 0, 0, 0 },
193 };
194 int addr;
195 Vdbe *v;
196
197 /* Make an entry in the sqlite_master table */
198 v = sqliteGetVdbe(pParse);
199 if( v==0 ) goto triggerfinish_cleanup;
200 sqliteBeginWriteOperation(pParse, 0, 0);
201 sqliteOpenMasterTable(v, nt->iDb);
202 addr = sqliteVdbeAddOpList(v, ArraySize(insertTrig), insertTrig);
203 sqliteVdbeChangeP3(v, addr+2, nt->name, 0);
204 sqliteVdbeChangeP3(v, addr+3, nt->table, 0);
205 sqliteVdbeChangeP3(v, addr+5, pAll->z, pAll->n);
206 if( nt->iDb==0 ){
207 sqliteChangeCookie(db, v);
208 }
209 sqliteVdbeAddOp(v, OP_Close, 0, 0);
210 sqliteEndWriteOperation(pParse);
211 }
212
213 if( !pParse->explain ){
214 Table *pTab;
215 sqliteHashInsert(&db->aDb[nt->iDb].trigHash,
216 nt->name, strlen(nt->name)+1, nt);
217 pTab = sqliteLocateTable(pParse, nt->table, db->aDb[nt->iTabDb].zName);
218 assert( pTab!=0 );
219 nt->pNext = pTab->pTrigger;
220 pTab->pTrigger = nt;
221 nt = 0;
222 }
223
224triggerfinish_cleanup:
225 sqliteDeleteTrigger(nt);
226 sqliteDeleteTrigger(pParse->pNewTrigger);
227 pParse->pNewTrigger = 0;
228 sqliteDeleteTriggerStep(pStepList);
229}
230
231/*
232** Make a copy of all components of the given trigger step. This has
233** the effect of copying all Expr.token.z values into memory obtained
234** from sqliteMalloc(). As initially created, the Expr.token.z values
235** all point to the input string that was fed to the parser. But that
236** string is ephemeral - it will go away as soon as the sqlite_exec()
237** call that started the parser exits. This routine makes a persistent
238** copy of all the Expr.token.z strings so that the TriggerStep structure
239** will be valid even after the sqlite_exec() call returns.
240*/
241static void sqlitePersistTriggerStep(TriggerStep *p){
242 if( p->target.z ){
243 p->target.z = sqliteStrNDup(p->target.z, p->target.n);
244 p->target.dyn = 1;
245 }
246 if( p->pSelect ){
247 Select *pNew = sqliteSelectDup(p->pSelect);
248 sqliteSelectDelete(p->pSelect);
249 p->pSelect = pNew;
250 }
251 if( p->pWhere ){
252 Expr *pNew = sqliteExprDup(p->pWhere);
253 sqliteExprDelete(p->pWhere);
254 p->pWhere = pNew;
255 }
256 if( p->pExprList ){
257 ExprList *pNew = sqliteExprListDup(p->pExprList);
258 sqliteExprListDelete(p->pExprList);
259 p->pExprList = pNew;
260 }
261 if( p->pIdList ){
262 IdList *pNew = sqliteIdListDup(p->pIdList);
263 sqliteIdListDelete(p->pIdList);
264 p->pIdList = pNew;
265 }
266}
267
268/*
269** Turn a SELECT statement (that the pSelect parameter points to) into
270** a trigger step. Return a pointer to a TriggerStep structure.
271**
272** The parser calls this routine when it finds a SELECT statement in
273** body of a TRIGGER.
274*/
275TriggerStep *sqliteTriggerSelectStep(Select *pSelect){
276 TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
277 if( pTriggerStep==0 ) return 0;
278
279 pTriggerStep->op = TK_SELECT;
280 pTriggerStep->pSelect = pSelect;
281 pTriggerStep->orconf = OE_Default;
282 sqlitePersistTriggerStep(pTriggerStep);
283
284 return pTriggerStep;
285}
286
287/*
288** Build a trigger step out of an INSERT statement. Return a pointer
289** to the new trigger step.
290**
291** The parser calls this routine when it sees an INSERT inside the
292** body of a trigger.
293*/
294TriggerStep *sqliteTriggerInsertStep(
295 Token *pTableName, /* Name of the table into which we insert */
296 IdList *pColumn, /* List of columns in pTableName to insert into */
297 ExprList *pEList, /* The VALUE clause: a list of values to be inserted */
298 Select *pSelect, /* A SELECT statement that supplies values */
299 int orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
300){
301 TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
302 if( pTriggerStep==0 ) return 0;
303
304 assert(pEList == 0 || pSelect == 0);
305 assert(pEList != 0 || pSelect != 0);
306
307 pTriggerStep->op = TK_INSERT;
308 pTriggerStep->pSelect = pSelect;
309 pTriggerStep->target = *pTableName;
310 pTriggerStep->pIdList = pColumn;
311 pTriggerStep->pExprList = pEList;
312 pTriggerStep->orconf = orconf;
313 sqlitePersistTriggerStep(pTriggerStep);
314
315 return pTriggerStep;
316}
317
318/*
319** Construct a trigger step that implements an UPDATE statement and return
320** a pointer to that trigger step. The parser calls this routine when it
321** sees an UPDATE statement inside the body of a CREATE TRIGGER.
322*/
323TriggerStep *sqliteTriggerUpdateStep(
324 Token *pTableName, /* Name of the table to be updated */
325 ExprList *pEList, /* The SET clause: list of column and new values */
326 Expr *pWhere, /* The WHERE clause */
327 int orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
328){
329 TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
330 if( pTriggerStep==0 ) return 0;
331
332 pTriggerStep->op = TK_UPDATE;
333 pTriggerStep->target = *pTableName;
334 pTriggerStep->pExprList = pEList;
335 pTriggerStep->pWhere = pWhere;
336 pTriggerStep->orconf = orconf;
337 sqlitePersistTriggerStep(pTriggerStep);
338
339 return pTriggerStep;
340}
341
342/*
343** Construct a trigger step that implements a DELETE statement and return
344** a pointer to that trigger step. The parser calls this routine when it
345** sees a DELETE statement inside the body of a CREATE TRIGGER.
346*/
347TriggerStep *sqliteTriggerDeleteStep(Token *pTableName, Expr *pWhere){
348 TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
349 if( pTriggerStep==0 ) return 0;
350
351 pTriggerStep->op = TK_DELETE;
352 pTriggerStep->target = *pTableName;
353 pTriggerStep->pWhere = pWhere;
354 pTriggerStep->orconf = OE_Default;
355 sqlitePersistTriggerStep(pTriggerStep);
356
357 return pTriggerStep;
358}
359
360/*
361** Recursively delete a Trigger structure
362*/
363void sqliteDeleteTrigger(Trigger *pTrigger){
364 if( pTrigger==0 ) return;
365 sqliteDeleteTriggerStep(pTrigger->step_list);
366 sqliteFree(pTrigger->name);
367 sqliteFree(pTrigger->table);
368 sqliteExprDelete(pTrigger->pWhen);
369 sqliteIdListDelete(pTrigger->pColumns);
370 if( pTrigger->nameToken.dyn ) sqliteFree((char*)pTrigger->nameToken.z);
371 sqliteFree(pTrigger);
372}
373
374/*
375 * This function is called to drop a trigger from the database schema.
376 *
377 * This may be called directly from the parser and therefore identifies
378 * the trigger by name. The sqliteDropTriggerPtr() routine does the
379 * same job as this routine except it take a spointer to the trigger
380 * instead of the trigger name.
381 *
382 * Note that this function does not delete the trigger entirely. Instead it
383 * removes it from the internal schema and places it in the trigDrop hash
384 * table. This is so that the trigger can be restored into the database schema
385 * if the transaction is rolled back.
386 */
387void sqliteDropTrigger(Parse *pParse, SrcList *pName){
388 Trigger *pTrigger;
389 int i;
390 const char *zDb;
391 const char *zName;
392 int nName;
393 sqlite *db = pParse->db;
394
395 if( sqlite_malloc_failed ) goto drop_trigger_cleanup;
396 assert( pName->nSrc==1 );
397 zDb = pName->a[0].zDatabase;
398 zName = pName->a[0].zName;
399 nName = strlen(zName);
400 for(i=0; i<db->nDb; i++){
401 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
402 if( zDb && sqliteStrICmp(db->aDb[j].zName, zDb) ) continue;
403 pTrigger = sqliteHashFind(&(db->aDb[j].trigHash), zName, nName+1);
404 if( pTrigger ) break;
405 }
406 if( !pTrigger ){
407 sqliteErrorMsg(pParse, "no such trigger: %S", pName, 0);
408 goto drop_trigger_cleanup;
409 }
410 sqliteDropTriggerPtr(pParse, pTrigger, 0);
411
412drop_trigger_cleanup:
413 sqliteSrcListDelete(pName);
414}
415
416/*
417** Drop a trigger given a pointer to that trigger. If nested is false,
418** then also generate code to remove the trigger from the SQLITE_MASTER
419** table.
420*/
421void sqliteDropTriggerPtr(Parse *pParse, Trigger *pTrigger, int nested){
422 Table *pTable;
423 Vdbe *v;
424 sqlite *db = pParse->db;
425
426 assert( pTrigger->iDb<db->nDb );
427 if( pTrigger->iDb>=2 ){
428 sqliteErrorMsg(pParse, "triggers may not be removed from "
429 "auxiliary database %s", db->aDb[pTrigger->iDb].zName);
430 return;
431 }
432 pTable = sqliteFindTable(db, pTrigger->table,db->aDb[pTrigger->iTabDb].zName);
433 assert(pTable);
434 assert( pTable->iDb==pTrigger->iDb || pTrigger->iDb==1 );
435#ifndef SQLITE_OMIT_AUTHORIZATION
436 {
437 int code = SQLITE_DROP_TRIGGER;
438 const char *zDb = db->aDb[pTrigger->iDb].zName;
439 const char *zTab = SCHEMA_TABLE(pTrigger->iDb);
440 if( pTrigger->iDb ) code = SQLITE_DROP_TEMP_TRIGGER;
441 if( sqliteAuthCheck(pParse, code, pTrigger->name, pTable->zName, zDb) ||
442 sqliteAuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
443 return;
444 }
445 }
446#endif
447
448 /* Generate code to destroy the database record of the trigger.
449 */
450 if( pTable!=0 && !nested && (v = sqliteGetVdbe(pParse))!=0 ){
451 int base;
452 static VdbeOp dropTrigger[] = {
453 { OP_Rewind, 0, ADDR(8), 0},
454 { OP_String, 0, 0, 0}, /* 1 */
455 { OP_MemStore, 1, 1, 0},
456 { OP_MemLoad, 1, 0, 0}, /* 3 */
457 { OP_Column, 0, 1, 0},
458 { OP_Ne, 0, ADDR(7), 0},
459 { OP_Delete, 0, 0, 0},
460 { OP_Next, 0, ADDR(3), 0}, /* 7 */
461 };
462
463 sqliteBeginWriteOperation(pParse, 0, 0);
464 sqliteOpenMasterTable(v, pTrigger->iDb);
465 base = sqliteVdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger);
466 sqliteVdbeChangeP3(v, base+1, pTrigger->name, 0);
467 if( pTrigger->iDb==0 ){
468 sqliteChangeCookie(db, v);
469 }
470 sqliteVdbeAddOp(v, OP_Close, 0, 0);
471 sqliteEndWriteOperation(pParse);
472 }
473
474 /*
475 * If this is not an "explain", then delete the trigger structure.
476 */
477 if( !pParse->explain ){
478 const char *zName = pTrigger->name;
479 int nName = strlen(zName);
480 if( pTable->pTrigger == pTrigger ){
481 pTable->pTrigger = pTrigger->pNext;
482 }else{
483 Trigger *cc = pTable->pTrigger;
484 while( cc ){
485 if( cc->pNext == pTrigger ){
486 cc->pNext = cc->pNext->pNext;
487 break;
488 }
489 cc = cc->pNext;
490 }
491 assert(cc);
492 }
493 sqliteHashInsert(&(db->aDb[pTrigger->iDb].trigHash), zName, nName+1, 0);
494 sqliteDeleteTrigger(pTrigger);
495 }
496}
497
498/*
499** pEList is the SET clause of an UPDATE statement. Each entry
500** in pEList is of the format <id>=<expr>. If any of the entries
501** in pEList have an <id> which matches an identifier in pIdList,
502** then return TRUE. If pIdList==NULL, then it is considered a
503** wildcard that matches anything. Likewise if pEList==NULL then
504** it matches anything so always return true. Return false only
505** if there is no match.
506*/
507static int checkColumnOverLap(IdList *pIdList, ExprList *pEList){
508 int e;
509 if( !pIdList || !pEList ) return 1;
510 for(e=0; e<pEList->nExpr; e++){
511 if( sqliteIdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
512 }
513 return 0;
514}
515
516/* A global variable that is TRUE if we should always set up temp tables for
517 * for triggers, even if there are no triggers to code. This is used to test
518 * how much overhead the triggers algorithm is causing.
519 *
520 * This flag can be set or cleared using the "trigger_overhead_test" pragma.
521 * The pragma is not documented since it is not really part of the interface
522 * to SQLite, just the test procedure.
523*/
524int always_code_trigger_setup = 0;
525
526/*
527 * Returns true if a trigger matching op, tr_tm and foreach that is NOT already
528 * on the Parse objects trigger-stack (to prevent recursive trigger firing) is
529 * found in the list specified as pTrigger.
530 */
531int sqliteTriggersExist(
532 Parse *pParse, /* Used to check for recursive triggers */
533 Trigger *pTrigger, /* A list of triggers associated with a table */
534 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
535 int tr_tm, /* one of TK_BEFORE, TK_AFTER */
536 int foreach, /* one of TK_ROW or TK_STATEMENT */
537 ExprList *pChanges /* Columns that change in an UPDATE statement */
538){
539 Trigger * pTriggerCursor;
540
541 if( always_code_trigger_setup ){
542 return 1;
543 }
544
545 pTriggerCursor = pTrigger;
546 while( pTriggerCursor ){
547 if( pTriggerCursor->op == op &&
548pTriggerCursor->tr_tm == tr_tm &&
549pTriggerCursor->foreach == foreach &&
550checkColumnOverLap(pTriggerCursor->pColumns, pChanges) ){
551 TriggerStack * ss;
552 ss = pParse->trigStack;
553 while( ss && ss->pTrigger != pTrigger ){
554ss = ss->pNext;
555 }
556 if( !ss )return 1;
557 }
558 pTriggerCursor = pTriggerCursor->pNext;
559 }
560
561 return 0;
562}
563
564/*
565** Convert the pStep->target token into a SrcList and return a pointer
566** to that SrcList.
567**
568** This routine adds a specific database name, if needed, to the target when
569** forming the SrcList. This prevents a trigger in one database from
570** referring to a target in another database. An exception is when the
571** trigger is in TEMP in which case it can refer to any other database it
572** wants.
573*/
574static SrcList *targetSrcList(
575 Parse *pParse, /* The parsing context */
576 TriggerStep *pStep /* The trigger containing the target token */
577){
578 Token sDb; /* Dummy database name token */
579 int iDb; /* Index of the database to use */
580 SrcList *pSrc; /* SrcList to be returned */
581
582 iDb = pStep->pTrig->iDb;
583 if( iDb==0 || iDb>=2 ){
584 assert( iDb<pParse->db->nDb );
585 sDb.z = pParse->db->aDb[iDb].zName;
586 sDb.n = strlen(sDb.z);
587 pSrc = sqliteSrcListAppend(0, &sDb, &pStep->target);
588 } else {
589 pSrc = sqliteSrcListAppend(0, &pStep->target, 0);
590 }
591 return pSrc;
592}
593
594/*
595** Generate VDBE code for zero or more statements inside the body of a
596** trigger.
597*/
598static int codeTriggerProgram(
599 Parse *pParse, /* The parser context */
600 TriggerStep *pStepList, /* List of statements inside the trigger body */
601 int orconfin /* Conflict algorithm. (OE_Abort, etc) */
602){
603 TriggerStep * pTriggerStep = pStepList;
604 int orconf;
605
606 while( pTriggerStep ){
607 int saveNTab = pParse->nTab;
608
609 orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin;
610 pParse->trigStack->orconf = orconf;
611 switch( pTriggerStep->op ){
612 case TK_SELECT: {
613Select * ss = sqliteSelectDup(pTriggerStep->pSelect);
614assert(ss);
615assert(ss->pSrc);
616sqliteSelect(pParse, ss, SRT_Discard, 0, 0, 0, 0);
617sqliteSelectDelete(ss);
618break;
619 }
620 case TK_UPDATE: {
621 SrcList *pSrc;
622 pSrc = targetSrcList(pParse, pTriggerStep);
623 sqliteVdbeAddOp(pParse->pVdbe, OP_ListPush, 0, 0);
624 sqliteUpdate(pParse, pSrc,
625sqliteExprListDup(pTriggerStep->pExprList),
626sqliteExprDup(pTriggerStep->pWhere), orconf);
627 sqliteVdbeAddOp(pParse->pVdbe, OP_ListPop, 0, 0);
628 break;
629 }
630 case TK_INSERT: {
631 SrcList *pSrc;
632 pSrc = targetSrcList(pParse, pTriggerStep);
633 sqliteInsert(pParse, pSrc,
634 sqliteExprListDup(pTriggerStep->pExprList),
635 sqliteSelectDup(pTriggerStep->pSelect),
636 sqliteIdListDup(pTriggerStep->pIdList), orconf);
637 break;
638 }
639 case TK_DELETE: {
640 SrcList *pSrc;
641 sqliteVdbeAddOp(pParse->pVdbe, OP_ListPush, 0, 0);
642 pSrc = targetSrcList(pParse, pTriggerStep);
643 sqliteDeleteFrom(pParse, pSrc, sqliteExprDup(pTriggerStep->pWhere));
644 sqliteVdbeAddOp(pParse->pVdbe, OP_ListPop, 0, 0);
645 break;
646 }
647 default:
648 assert(0);
649 }
650 pParse->nTab = saveNTab;
651 pTriggerStep = pTriggerStep->pNext;
652 }
653
654 return 0;
655}
656
657/*
658** This is called to code FOR EACH ROW triggers.
659**
660** When the code that this function generates is executed, the following
661** must be true:
662**
663** 1. No cursors may be open in the main database. (But newIdx and oldIdx
664** can be indices of cursors in temporary tables. See below.)
665**
666** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then
667** a temporary vdbe cursor (index newIdx) must be open and pointing at
668** a row containing values to be substituted for new.* expressions in the
669** trigger program(s).
670**
671** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then
672** a temporary vdbe cursor (index oldIdx) must be open and pointing at
673** a row containing values to be substituted for old.* expressions in the
674** trigger program(s).
675**
676*/
677int sqliteCodeRowTrigger(
678 Parse *pParse, /* Parse context */
679 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
680 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
681 int tr_tm, /* One of TK_BEFORE, TK_AFTER */
682 Table *pTab, /* The table to code triggers from */
683 int newIdx, /* The indice of the "new" row to access */
684 int oldIdx, /* The indice of the "old" row to access */
685 int orconf, /* ON CONFLICT policy */
686 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
687){
688 Trigger * pTrigger;
689 TriggerStack * pTriggerStack;
690
691 assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE);
692 assert(tr_tm == TK_BEFORE || tr_tm == TK_AFTER );
693
694 assert(newIdx != -1 || oldIdx != -1);
695
696 pTrigger = pTab->pTrigger;
697 while( pTrigger ){
698 int fire_this = 0;
699
700 /* determine whether we should code this trigger */
701 if( pTrigger->op == op && pTrigger->tr_tm == tr_tm &&
702 pTrigger->foreach == TK_ROW ){
703 fire_this = 1;
704 pTriggerStack = pParse->trigStack;
705 while( pTriggerStack ){
706 if( pTriggerStack->pTrigger == pTrigger ){
707 fire_this = 0;
708}
709 pTriggerStack = pTriggerStack->pNext;
710 }
711 if( op == TK_UPDATE && pTrigger->pColumns &&
712 !checkColumnOverLap(pTrigger->pColumns, pChanges) ){
713 fire_this = 0;
714 }
715 }
716
717 if( fire_this && (pTriggerStack = sqliteMalloc(sizeof(TriggerStack)))!=0 ){
718 int endTrigger;
719 SrcList dummyTablist;
720 Expr * whenExpr;
721 AuthContext sContext;
722
723 dummyTablist.nSrc = 0;
724
725 /* Push an entry on to the trigger stack */
726 pTriggerStack->pTrigger = pTrigger;
727 pTriggerStack->newIdx = newIdx;
728 pTriggerStack->oldIdx = oldIdx;
729 pTriggerStack->pTab = pTab;
730 pTriggerStack->pNext = pParse->trigStack;
731 pTriggerStack->ignoreJump = ignoreJump;
732 pParse->trigStack = pTriggerStack;
733 sqliteAuthContextPush(pParse, &sContext, pTrigger->name);
734
735 /* code the WHEN clause */
736 endTrigger = sqliteVdbeMakeLabel(pParse->pVdbe);
737 whenExpr = sqliteExprDup(pTrigger->pWhen);
738 if( sqliteExprResolveIds(pParse, &dummyTablist, 0, whenExpr) ){
739 pParse->trigStack = pParse->trigStack->pNext;
740 sqliteFree(pTriggerStack);
741 sqliteExprDelete(whenExpr);
742 return 1;
743 }
744 sqliteExprIfFalse(pParse, whenExpr, endTrigger, 1);
745 sqliteExprDelete(whenExpr);
746
747 codeTriggerProgram(pParse, pTrigger->step_list, orconf);
748
749 /* Pop the entry off the trigger stack */
750 pParse->trigStack = pParse->trigStack->pNext;
751 sqliteAuthContextPop(&sContext);
752 sqliteFree(pTriggerStack);
753
754 sqliteVdbeResolveLabel(pParse->pVdbe, endTrigger);
755 }
756 pTrigger = pTrigger->pNext;
757 }
758
759 return 0;
760}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status