blob: ab94f246acb6f0d88821c9b794eecd6036fbc349 [file] [log] [blame]
danielk1977c3f9bad2002-05-15 08:30:12 +00001/*
danielk1977633ed082002-05-17 00:05:58 +00002**
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*************************************************************************
danielk1977822a5162008-05-16 04:51:54 +000011**
12**
drh33e619f2009-05-28 01:00:55 +000013** $Id: trigger.c,v 1.141 2009/05/28 01:00:55 drh Exp $
drh9adf9ac2002-05-15 11:44:13 +000014*/
danielk1977c3f9bad2002-05-15 08:30:12 +000015#include "sqliteInt.h"
drh9adf9ac2002-05-15 11:44:13 +000016
drhb7f91642004-10-31 02:22:47 +000017#ifndef SQLITE_OMIT_TRIGGER
danielk1977c3f9bad2002-05-15 08:30:12 +000018/*
drh4b59ab52002-08-24 18:24:51 +000019** Delete a linked list of TriggerStep structures.
20*/
drh633e6d52008-07-28 19:34:53 +000021void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
drh4b59ab52002-08-24 18:24:51 +000022 while( pTriggerStep ){
23 TriggerStep * pTmp = pTriggerStep;
24 pTriggerStep = pTriggerStep->pNext;
25
drh633e6d52008-07-28 19:34:53 +000026 sqlite3ExprDelete(db, pTmp->pWhere);
27 sqlite3ExprListDelete(db, pTmp->pExprList);
28 sqlite3SelectDelete(db, pTmp->pSelect);
29 sqlite3IdListDelete(db, pTmp->pIdList);
drh4b59ab52002-08-24 18:24:51 +000030
drh633e6d52008-07-28 19:34:53 +000031 sqlite3DbFree(db, pTmp);
drh4b59ab52002-08-24 18:24:51 +000032 }
33}
34
35/*
danielk19772f886d12009-02-28 10:47:41 +000036** Given table pTab, return a list of all the triggers attached to
37** the table. The list is connected by Trigger.pNext pointers.
drh9ab4c2e2009-05-09 00:18:38 +000038**
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.
44**
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.
danielk19772f886d12009-02-28 10:47:41 +000048*/
49Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
50 Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
51 Trigger *pList = 0; /* List of triggers to return */
52
53 if( pTmpSchema!=pTab->pSchema ){
54 HashElem *p;
55 for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
56 Trigger *pTrig = (Trigger *)sqliteHashData(p);
57 if( pTrig->pTabSchema==pTab->pSchema
58 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
59 ){
60 pTrig->pNext = (pList ? pList : pTab->pTrigger);
61 pList = pTrig;
62 }
63 }
64 }
65
66 return (pList ? pList : pTab->pTrigger);
67}
68
69/*
drhf0f258b2003-04-21 18:48:45 +000070** This is called by the parser when it sees a CREATE TRIGGER statement
71** up to the point of the BEGIN before the trigger actions. A Trigger
72** structure is generated based on the information available and stored
73** in pParse->pNewTrigger. After the trigger actions have been parsed, the
danielk19774adee202004-05-08 08:23:19 +000074** sqlite3FinishTrigger() function is called to complete the trigger
drhf0f258b2003-04-21 18:48:45 +000075** construction process.
drh9adf9ac2002-05-15 11:44:13 +000076*/
danielk19774adee202004-05-08 08:23:19 +000077void sqlite3BeginTrigger(
drh9adf9ac2002-05-15 11:44:13 +000078 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
danielk1977ef2cb632004-05-29 02:37:19 +000079 Token *pName1, /* The name of the trigger */
80 Token *pName2, /* The name of the trigger */
drh5cf590c2003-04-24 01:45:04 +000081 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
drh9adf9ac2002-05-15 11:44:13 +000082 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
danielk1977633ed082002-05-17 00:05:58 +000083 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
drhd24cc422003-03-27 12:51:24 +000084 SrcList *pTableName,/* The name of the table/view the trigger applies to */
drh9adf9ac2002-05-15 11:44:13 +000085 Expr *pWhen, /* WHEN clause */
drhfdd48a72006-09-11 23:45:48 +000086 int isTemp, /* True if the TEMPORARY keyword is present */
87 int noErr /* Suppress errors if the trigger already exists */
drh9adf9ac2002-05-15 11:44:13 +000088){
danielk1977d5d56522005-03-16 12:15:20 +000089 Trigger *pTrigger = 0;
danielk1977ef2cb632004-05-29 02:37:19 +000090 Table *pTab;
drhf0f258b2003-04-21 18:48:45 +000091 char *zName = 0; /* Name of the trigger */
drh9bb575f2004-09-06 17:24:11 +000092 sqlite3 *db = pParse->db;
danielk1977ef2cb632004-05-29 02:37:19 +000093 int iDb; /* The database to store the trigger in */
94 Token *pName; /* The unqualified db name */
drh4312db52003-06-03 01:47:11 +000095 DbFixer sFix;
danielk1977da184232006-01-05 11:34:32 +000096 int iTabDb;
drhed6c8672003-01-12 18:02:16 +000097
drh43617e92006-03-06 20:55:46 +000098 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
99 assert( pName2!=0 );
drhaa78bec2008-12-09 03:55:14 +0000100 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
101 assert( op>0 && op<0xff );
danielk1977ef2cb632004-05-29 02:37:19 +0000102 if( isTemp ){
103 /* If TEMP was specified, then the trigger name may not be qualified. */
drh43617e92006-03-06 20:55:46 +0000104 if( pName2->n>0 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000105 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
106 goto trigger_cleanup;
107 }
108 iDb = 1;
109 pName = pName1;
110 }else{
111 /* Figure out the db that the the trigger will be created in */
112 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
113 if( iDb<0 ){
114 goto trigger_cleanup;
115 }
116 }
117
118 /* If the trigger name was unqualified, and the table is a temp table,
119 ** then set iDb to 1 to create the trigger in the temporary database.
120 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
121 ** exist, the error is caught by the block below.
drh9adf9ac2002-05-15 11:44:13 +0000122 */
drh17435752007-08-16 04:30:38 +0000123 if( !pTableName || db->mallocFailed ){
drh6f7adc82006-01-11 21:41:20 +0000124 goto trigger_cleanup;
125 }
danielk1977ef2cb632004-05-29 02:37:19 +0000126 pTab = sqlite3SrcListLookup(pParse, pTableName);
danielk1977da184232006-01-05 11:34:32 +0000127 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977ef2cb632004-05-29 02:37:19 +0000128 iDb = 1;
129 }
130
131 /* Ensure the table name matches database name and that the table exists */
drh17435752007-08-16 04:30:38 +0000132 if( db->mallocFailed ) goto trigger_cleanup;
drhd24cc422003-03-27 12:51:24 +0000133 assert( pTableName->nSrc==1 );
danielk1977ef2cb632004-05-29 02:37:19 +0000134 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) &&
135 sqlite3FixSrcList(&sFix, pTableName) ){
drh4312db52003-06-03 01:47:11 +0000136 goto trigger_cleanup;
drhf26e09c2003-05-31 16:21:12 +0000137 }
danielk1977ef2cb632004-05-29 02:37:19 +0000138 pTab = sqlite3SrcListLookup(pParse, pTableName);
139 if( !pTab ){
140 /* The table does not exist. */
drhd24cc422003-03-27 12:51:24 +0000141 goto trigger_cleanup;
142 }
drh4cbdda92006-06-14 19:00:20 +0000143 if( IsVirtual(pTab) ){
144 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
145 goto trigger_cleanup;
146 }
drhd24cc422003-03-27 12:51:24 +0000147
danielk1977d8123362004-06-12 09:25:12 +0000148 /* Check that the trigger name is not reserved and that no trigger of the
149 ** specified name exists */
drh17435752007-08-16 04:30:38 +0000150 zName = sqlite3NameFromToken(db, pName);
danielk1977d8123362004-06-12 09:25:12 +0000151 if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
152 goto trigger_cleanup;
153 }
drhaa78bec2008-12-09 03:55:14 +0000154 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
drhea678832008-12-10 19:26:22 +0000155 zName, sqlite3Strlen30(zName)) ){
drhfdd48a72006-09-11 23:45:48 +0000156 if( !noErr ){
157 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
158 }
drhe5f9c642003-01-13 23:27:31 +0000159 goto trigger_cleanup;
danielk1977c3f9bad2002-05-15 08:30:12 +0000160 }
danielk1977ef2cb632004-05-29 02:37:19 +0000161
162 /* Do not create a trigger on a system table */
drhdca76842004-12-07 14:06:13 +0000163 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk19774adee202004-05-08 08:23:19 +0000164 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
drhd24cc422003-03-27 12:51:24 +0000165 pParse->nErr++;
166 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40 +0000167 }
danielk1977ef2cb632004-05-29 02:37:19 +0000168
169 /* INSTEAD of triggers are only for views and views only support INSTEAD
170 ** of triggers.
171 */
172 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000173 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drhda93d232003-03-31 02:12:46 +0000174 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000175 goto trigger_cleanup;
176 }
danielk1977ef2cb632004-05-29 02:37:19 +0000177 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000178 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drhda93d232003-03-31 02:12:46 +0000179 " trigger on table: %S", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000180 goto trigger_cleanup;
181 }
danielk1977da184232006-01-05 11:34:32 +0000182 iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977ef2cb632004-05-29 02:37:19 +0000183
drhd24cc422003-03-27 12:51:24 +0000184#ifndef SQLITE_OMIT_AUTHORIZATION
185 {
186 int code = SQLITE_CREATE_TRIGGER;
danielk1977da184232006-01-05 11:34:32 +0000187 const char *zDb = db->aDb[iTabDb].zName;
drhe22a3342003-04-22 20:30:37 +0000188 const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
danielk1977da184232006-01-05 11:34:32 +0000189 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19 +0000190 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24 +0000191 goto trigger_cleanup;
192 }
danielk1977da184232006-01-05 11:34:32 +0000193 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24 +0000194 goto trigger_cleanup;
195 }
196 }
197#endif
danielk1977d702fcc2002-05-26 23:24:40 +0000198
danielk1977ef2cb632004-05-29 02:37:19 +0000199 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04 +0000200 ** cannot appear on views. So we might as well translate every
201 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
202 ** elsewhere.
203 */
danielk1977d702fcc2002-05-26 23:24:40 +0000204 if (tr_tm == TK_INSTEAD){
205 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12 +0000206 }
207
208 /* Build the Trigger object */
drh17435752007-08-16 04:30:38 +0000209 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19 +0000210 if( pTrigger==0 ) goto trigger_cleanup;
211 pTrigger->name = zName;
drhe5f9c642003-01-13 23:27:31 +0000212 zName = 0;
drh17435752007-08-16 04:30:38 +0000213 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32 +0000214 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48 +0000215 pTrigger->pTabSchema = pTab->pSchema;
drhaa78bec2008-12-09 03:55:14 +0000216 pTrigger->op = (u8)op;
drhdca76842004-12-07 14:06:13 +0000217 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
danielk19776ab3a2e2009-02-19 14:39:25 +0000218 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
drh17435752007-08-16 04:30:38 +0000219 pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
drhf0f258b2003-04-21 18:48:45 +0000220 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19 +0000221 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45 +0000222
223trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000224 sqlite3DbFree(db, zName);
225 sqlite3SrcListDelete(db, pTableName);
226 sqlite3IdListDelete(db, pColumns);
227 sqlite3ExprDelete(db, pWhen);
danielk1977d5d56522005-03-16 12:15:20 +0000228 if( !pParse->pNewTrigger ){
drh633e6d52008-07-28 19:34:53 +0000229 sqlite3DeleteTrigger(db, pTrigger);
danielk1977d5d56522005-03-16 12:15:20 +0000230 }else{
231 assert( pParse->pNewTrigger==pTrigger );
232 }
drhf0f258b2003-04-21 18:48:45 +0000233}
234
235/*
236** This routine is called after all of the trigger actions have been parsed
237** in order to complete the process of building the trigger.
238*/
danielk19774adee202004-05-08 08:23:19 +0000239void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45 +0000240 Parse *pParse, /* Parser context */
241 TriggerStep *pStepList, /* The triggered program */
242 Token *pAll /* Token that describes the complete CREATE TRIGGER */
243){
danielk19772f886d12009-02-28 10:47:41 +0000244 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
245 char *zName; /* Name of trigger */
246 sqlite3 *db = pParse->db; /* The database */
drhf26e09c2003-05-31 16:21:12 +0000247 DbFixer sFix;
danielk19772f886d12009-02-28 10:47:41 +0000248 int iDb; /* Database containing the trigger */
drhb7916a72009-05-27 10:31:29 +0000249 Token nameToken; /* Trigger name for error reporting */
drhf0f258b2003-04-21 18:48:45 +0000250
danielk1977bfb9e352005-01-24 13:03:32 +0000251 pTrig = pParse->pNewTrigger;
drhf0f258b2003-04-21 18:48:45 +0000252 pParse->pNewTrigger = 0;
drh9ab4c2e2009-05-09 00:18:38 +0000253 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
danielk19772f886d12009-02-28 10:47:41 +0000254 zName = pTrig->name;
danielk1977da184232006-01-05 11:34:32 +0000255 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32 +0000256 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53 +0000257 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32 +0000258 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53 +0000259 pStepList = pStepList->pNext;
260 }
drhb7916a72009-05-27 10:31:29 +0000261 nameToken.z = pTrig->name;
262 nameToken.n = sqlite3Strlen30(nameToken.z);
263 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken)
danielk1977bfb9e352005-01-24 13:03:32 +0000264 && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
drhf26e09c2003-05-31 16:21:12 +0000265 goto triggerfinish_cleanup;
266 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000267
268 /* if we are not initializing, and this trigger is not on a TEMP table,
drh9adf9ac2002-05-15 11:44:13 +0000269 ** build the sqlite_master entry
270 */
drh1d85d932004-02-14 23:05:52 +0000271 if( !db->init.busy ){
drhc977f7f2002-05-21 11:38:11 +0000272 Vdbe *v;
drh2d401ab2008-01-10 23:50:11 +0000273 char *z;
danielk1977c3f9bad2002-05-15 08:30:12 +0000274
275 /* Make an entry in the sqlite_master table */
danielk19774adee202004-05-08 08:23:19 +0000276 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000277 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000278 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11 +0000279 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
280 sqlite3NestedParse(pParse,
281 "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
danielk19772f886d12009-02-28 10:47:41 +0000282 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
drh2d401ab2008-01-10 23:50:11 +0000283 pTrig->table, z);
drh633e6d52008-07-28 19:34:53 +0000284 sqlite3DbFree(db, z);
drh9cbf3422008-01-17 16:22:13 +0000285 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +0000286 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, sqlite3MPrintf(
danielk19772f886d12009-02-28 10:47:41 +0000287 db, "type='trigger' AND name='%q'", zName), P4_DYNAMIC
danielk19771e536952007-08-16 10:09:01 +0000288 );
danielk1977c3f9bad2002-05-15 08:30:12 +0000289 }
290
drh956bc922004-07-24 17:38:29 +0000291 if( db->init.busy ){
danielk19772f886d12009-02-28 10:47:41 +0000292 Trigger *pLink = pTrig;
293 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
294 pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);
295 if( pTrig ){
drhf3a65f72007-08-22 20:18:21 +0000296 db->mallocFailed = 1;
danielk19772f886d12009-02-28 10:47:41 +0000297 }else if( pLink->pSchema==pLink->pTabSchema ){
298 Table *pTab;
drha83ccca2009-04-28 13:01:09 +0000299 int n = sqlite3Strlen30(pLink->table);
danielk19772f886d12009-02-28 10:47:41 +0000300 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n);
301 assert( pTab!=0 );
302 pLink->pNext = pTab->pTrigger;
303 pTab->pTrigger = pLink;
danielk1977d5d56522005-03-16 12:15:20 +0000304 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000305 }
306
drhf0f258b2003-04-21 18:48:45 +0000307triggerfinish_cleanup:
drh633e6d52008-07-28 19:34:53 +0000308 sqlite3DeleteTrigger(db, pTrig);
danielk1977bfb9e352005-01-24 13:03:32 +0000309 assert( !pParse->pNewTrigger );
drh633e6d52008-07-28 19:34:53 +0000310 sqlite3DeleteTriggerStep(db, pStepList);
drh4b59ab52002-08-24 18:24:51 +0000311}
danielk1977c3f9bad2002-05-15 08:30:12 +0000312
drh4b59ab52002-08-24 18:24:51 +0000313/*
drhc977f7f2002-05-21 11:38:11 +0000314** Turn a SELECT statement (that the pSelect parameter points to) into
315** a trigger step. Return a pointer to a TriggerStep structure.
316**
317** The parser calls this routine when it finds a SELECT statement in
318** body of a TRIGGER.
319*/
drh17435752007-08-16 04:30:38 +0000320TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
321 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000322 if( pTriggerStep==0 ) {
drh633e6d52008-07-28 19:34:53 +0000323 sqlite3SelectDelete(db, pSelect);
danielk19772e588c72005-12-09 14:25:08 +0000324 return 0;
325 }
danielk1977633ed082002-05-17 00:05:58 +0000326 pTriggerStep->op = TK_SELECT;
327 pTriggerStep->pSelect = pSelect;
328 pTriggerStep->orconf = OE_Default;
drhb7916a72009-05-27 10:31:29 +0000329 return pTriggerStep;
330}
danielk1977c3f9bad2002-05-15 08:30:12 +0000331
drhb7916a72009-05-27 10:31:29 +0000332/*
333** Allocate space to hold a new trigger step. The allocated space
334** holds both the TriggerStep object and the TriggerStep.target.z string.
335**
336** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
337*/
338static TriggerStep *triggerStepAllocate(
339 sqlite3 *db, /* Database connection */
340 int op, /* Trigger opcode */
341 Token *pName /* The target name */
342){
343 TriggerStep *pTriggerStep;
344
345 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n);
346 if( pTriggerStep ){
347 char *z = (char*)&pTriggerStep[1];
348 memcpy(z, pName->z, pName->n);
349 pTriggerStep->target.z = z;
350 pTriggerStep->target.n = pName->n;
351 pTriggerStep->op = op;
352 }
danielk1977633ed082002-05-17 00:05:58 +0000353 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000354}
355
drhc977f7f2002-05-21 11:38:11 +0000356/*
357** Build a trigger step out of an INSERT statement. Return a pointer
358** to the new trigger step.
359**
360** The parser calls this routine when it sees an INSERT inside the
361** body of a trigger.
362*/
danielk19774adee202004-05-08 08:23:19 +0000363TriggerStep *sqlite3TriggerInsertStep(
drh17435752007-08-16 04:30:38 +0000364 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000365 Token *pTableName, /* Name of the table into which we insert */
366 IdList *pColumn, /* List of columns in pTableName to insert into */
367 ExprList *pEList, /* The VALUE clause: a list of values to be inserted */
368 Select *pSelect, /* A SELECT statement that supplies values */
369 int orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
danielk1977633ed082002-05-17 00:05:58 +0000370){
drhf3a65f72007-08-22 20:18:21 +0000371 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000372
danielk1977633ed082002-05-17 00:05:58 +0000373 assert(pEList == 0 || pSelect == 0);
drhf3a65f72007-08-22 20:18:21 +0000374 assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000375
drhb7916a72009-05-27 10:31:29 +0000376 pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
danielk1977d5d56522005-03-16 12:15:20 +0000377 if( pTriggerStep ){
drh33e619f2009-05-28 01:00:55 +0000378 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
danielk1977d5d56522005-03-16 12:15:20 +0000379 pTriggerStep->pIdList = pColumn;
drh33e619f2009-05-28 01:00:55 +0000380 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
danielk1977d5d56522005-03-16 12:15:20 +0000381 pTriggerStep->orconf = orconf;
danielk1977d5d56522005-03-16 12:15:20 +0000382 }else{
drh633e6d52008-07-28 19:34:53 +0000383 sqlite3IdListDelete(db, pColumn);
danielk1977d5d56522005-03-16 12:15:20 +0000384 }
drh33e619f2009-05-28 01:00:55 +0000385 sqlite3ExprListDelete(db, pEList);
386 sqlite3SelectDelete(db, pSelect);
danielk1977c3f9bad2002-05-15 08:30:12 +0000387
danielk1977633ed082002-05-17 00:05:58 +0000388 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000389}
390
drhc977f7f2002-05-21 11:38:11 +0000391/*
392** Construct a trigger step that implements an UPDATE statement and return
393** a pointer to that trigger step. The parser calls this routine when it
394** sees an UPDATE statement inside the body of a CREATE TRIGGER.
395*/
danielk19774adee202004-05-08 08:23:19 +0000396TriggerStep *sqlite3TriggerUpdateStep(
drh17435752007-08-16 04:30:38 +0000397 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000398 Token *pTableName, /* Name of the table to be updated */
399 ExprList *pEList, /* The SET clause: list of column and new values */
400 Expr *pWhere, /* The WHERE clause */
401 int orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
402){
drhb7916a72009-05-27 10:31:29 +0000403 TriggerStep *pTriggerStep;
404
405 pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
drh33e619f2009-05-28 01:00:55 +0000406 if( pTriggerStep ){
407 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
408 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
409 pTriggerStep->orconf = orconf;
drh0e3a6f32007-03-30 20:40:34 +0000410 }
drh33e619f2009-05-28 01:00:55 +0000411 sqlite3ExprListDelete(db, pEList);
412 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000413 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000414}
415
drhc977f7f2002-05-21 11:38:11 +0000416/*
417** Construct a trigger step that implements a DELETE statement and return
418** a pointer to that trigger step. The parser calls this routine when it
419** sees a DELETE statement inside the body of a CREATE TRIGGER.
420*/
drh17435752007-08-16 04:30:38 +0000421TriggerStep *sqlite3TriggerDeleteStep(
422 sqlite3 *db, /* Database connection */
423 Token *pTableName, /* The table from which rows are deleted */
424 Expr *pWhere /* The WHERE clause */
425){
drhb7916a72009-05-27 10:31:29 +0000426 TriggerStep *pTriggerStep;
427
428 pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
drh33e619f2009-05-28 01:00:55 +0000429 if( pTriggerStep ){
430 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
431 pTriggerStep->orconf = OE_Default;
drhb63a53d2007-03-31 01:34:44 +0000432 }
drh33e619f2009-05-28 01:00:55 +0000433 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000434 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000435}
436
danielk1977633ed082002-05-17 00:05:58 +0000437/*
438** Recursively delete a Trigger structure
439*/
drh633e6d52008-07-28 19:34:53 +0000440void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
drhf0f258b2003-04-21 18:48:45 +0000441 if( pTrigger==0 ) return;
drh633e6d52008-07-28 19:34:53 +0000442 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
443 sqlite3DbFree(db, pTrigger->name);
444 sqlite3DbFree(db, pTrigger->table);
445 sqlite3ExprDelete(db, pTrigger->pWhen);
446 sqlite3IdListDelete(db, pTrigger->pColumns);
drh633e6d52008-07-28 19:34:53 +0000447 sqlite3DbFree(db, pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000448}
449
450/*
danielk19778a414492004-06-29 08:59:35 +0000451** This function is called to drop a trigger from the database schema.
452**
453** This may be called directly from the parser and therefore identifies
454** the trigger by name. The sqlite3DropTriggerPtr() routine does the
455** same job as this routine except it takes a pointer to the trigger
456** instead of the trigger name.
457**/
drhfdd48a72006-09-11 23:45:48 +0000458void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000459 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000460 int i;
461 const char *zDb;
462 const char *zName;
463 int nName;
drh9bb575f2004-09-06 17:24:11 +0000464 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000465
drh17435752007-08-16 04:30:38 +0000466 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000467 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000468 goto drop_trigger_cleanup;
469 }
danielk19778e227872004-06-07 07:52:17 +0000470
drhd24cc422003-03-27 12:51:24 +0000471 assert( pName->nSrc==1 );
472 zDb = pName->a[0].zDatabase;
473 zName = pName->a[0].zName;
drhea678832008-12-10 19:26:22 +0000474 nName = sqlite3Strlen30(zName);
danielk197753c0f742005-03-29 03:10:59 +0000475 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000476 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000477 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
drhe4df0e72006-03-29 00:24:06 +0000478 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
drhd24cc422003-03-27 12:51:24 +0000479 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000480 }
drhd24cc422003-03-27 12:51:24 +0000481 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000482 if( !noErr ){
483 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
484 }
drhd24cc422003-03-27 12:51:24 +0000485 goto drop_trigger_cleanup;
486 }
drh74161702006-02-24 02:53:49 +0000487 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000488
489drop_trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000490 sqlite3SrcListDelete(db, pName);
drh79a519c2003-05-17 19:04:03 +0000491}
492
493/*
drh956bc922004-07-24 17:38:29 +0000494** Return a pointer to the Table structure for the table that a trigger
495** is set on.
496*/
drh74161702006-02-24 02:53:49 +0000497static Table *tableOfTrigger(Trigger *pTrigger){
drha83ccca2009-04-28 13:01:09 +0000498 int n = sqlite3Strlen30(pTrigger->table);
danielk1977aaf22682006-01-06 15:03:48 +0000499 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
drh956bc922004-07-24 17:38:29 +0000500}
501
502
503/*
drh74161702006-02-24 02:53:49 +0000504** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000505*/
drh74161702006-02-24 02:53:49 +0000506void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000507 Table *pTable;
508 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000509 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000510 int iDb;
drh79a519c2003-05-17 19:04:03 +0000511
danielk1977da184232006-01-05 11:34:32 +0000512 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000513 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000514 pTable = tableOfTrigger(pTrigger);
drh43617e92006-03-06 20:55:46 +0000515 assert( pTable );
danielk1977da184232006-01-05 11:34:32 +0000516 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000517#ifndef SQLITE_OMIT_AUTHORIZATION
518 {
519 int code = SQLITE_DROP_TRIGGER;
drh956bc922004-07-24 17:38:29 +0000520 const char *zDb = db->aDb[iDb].zName;
521 const char *zTab = SCHEMA_TABLE(iDb);
522 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
danielk19774adee202004-05-08 08:23:19 +0000523 if( sqlite3AuthCheck(pParse, code, pTrigger->name, pTable->zName, zDb) ||
524 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000525 return;
drhe5f9c642003-01-13 23:27:31 +0000526 }
drhed6c8672003-01-12 18:02:16 +0000527 }
drhe5f9c642003-01-13 23:27:31 +0000528#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000529
drhf0f258b2003-04-21 18:48:45 +0000530 /* Generate code to destroy the database record of the trigger.
531 */
drh43617e92006-03-06 20:55:46 +0000532 assert( pTable!=0 );
533 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drhf0f258b2003-04-21 18:48:45 +0000534 int base;
drh57196282004-10-06 15:41:16 +0000535 static const VdbeOpList dropTrigger[] = {
drh9b1b01b2003-08-16 12:37:51 +0000536 { OP_Rewind, 0, ADDR(9), 0},
drh1db639c2008-01-17 02:36:28 +0000537 { OP_String8, 0, 1, 0}, /* 1 */
538 { OP_Column, 0, 1, 2},
539 { OP_Ne, 2, ADDR(8), 1},
540 { OP_String8, 0, 1, 0}, /* 4: "trigger" */
541 { OP_Column, 0, 0, 2},
542 { OP_Ne, 2, ADDR(8), 1},
drhf0f258b2003-04-21 18:48:45 +0000543 { OP_Delete, 0, 0, 0},
drh9b1b01b2003-08-16 12:37:51 +0000544 { OP_Next, 0, ADDR(1), 0}, /* 8 */
drhf0f258b2003-04-21 18:48:45 +0000545 };
546
drh956bc922004-07-24 17:38:29 +0000547 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk1977c00da102006-01-07 13:21:04 +0000548 sqlite3OpenMasterTable(pParse, iDb);
danielk19774adee202004-05-08 08:23:19 +0000549 base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger);
drh66a51672008-01-03 00:01:23 +0000550 sqlite3VdbeChangeP4(v, base+1, pTrigger->name, 0);
drh24003452008-01-03 01:28:59 +0000551 sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
drh9cbf3422008-01-17 16:22:13 +0000552 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +0000553 sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
554 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->name, 0);
danielk19776ab3a2e2009-02-19 14:39:25 +0000555 if( pParse->nMem<3 ){
556 pParse->nMem = 3;
557 }
drhf0f258b2003-04-21 18:48:45 +0000558 }
drh956bc922004-07-24 17:38:29 +0000559}
drhf0f258b2003-04-21 18:48:45 +0000560
drh956bc922004-07-24 17:38:29 +0000561/*
562** Remove a trigger from the hash tables of the sqlite* pointer.
563*/
564void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
danielk19772f886d12009-02-28 10:47:41 +0000565 Hash *pHash = &(db->aDb[iDb].pSchema->trigHash);
drh956bc922004-07-24 17:38:29 +0000566 Trigger *pTrigger;
danielk19772f886d12009-02-28 10:47:41 +0000567 pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0);
drh9ab4c2e2009-05-09 00:18:38 +0000568 if( ALWAYS(pTrigger) ){
danielk19772f886d12009-02-28 10:47:41 +0000569 if( pTrigger->pSchema==pTrigger->pTabSchema ){
570 Table *pTab = tableOfTrigger(pTrigger);
571 Trigger **pp;
572 for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
573 *pp = (*pp)->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +0000574 }
drh633e6d52008-07-28 19:34:53 +0000575 sqlite3DeleteTrigger(db, pTrigger);
drh956bc922004-07-24 17:38:29 +0000576 db->flags |= SQLITE_InternChanges;
danielk1977c3f9bad2002-05-15 08:30:12 +0000577 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000578}
579
drhc977f7f2002-05-21 11:38:11 +0000580/*
581** pEList is the SET clause of an UPDATE statement. Each entry
582** in pEList is of the format <id>=<expr>. If any of the entries
583** in pEList have an <id> which matches an identifier in pIdList,
584** then return TRUE. If pIdList==NULL, then it is considered a
585** wildcard that matches anything. Likewise if pEList==NULL then
586** it matches anything so always return true. Return false only
587** if there is no match.
588*/
drh9ab4c2e2009-05-09 00:18:38 +0000589static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000590 int e;
drh9ab4c2e2009-05-09 00:18:38 +0000591 if( pIdList==0 || NEVER(pEList==0) ) return 1;
drhad2d8302002-05-24 20:31:36 +0000592 for(e=0; e<pEList->nExpr; e++){
danielk19774adee202004-05-08 08:23:19 +0000593 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000594 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000595 return 0;
596}
597
danielk1977c3f9bad2002-05-15 08:30:12 +0000598/*
danielk19772f886d12009-02-28 10:47:41 +0000599** Return a list of all triggers on table pTab if there exists at least
600** one trigger that must be fired when an operation of type 'op' is
601** performed on the table, and, if that operation is an UPDATE, if at
602** least one of the columns in pChanges is being modified.
drhdca76842004-12-07 14:06:13 +0000603*/
danielk19772f886d12009-02-28 10:47:41 +0000604Trigger *sqlite3TriggersExist(
605 Parse *pParse, /* Parse context */
drhdca76842004-12-07 14:06:13 +0000606 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000607 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
danielk19772f886d12009-02-28 10:47:41 +0000608 ExprList *pChanges, /* Columns that change in an UPDATE statement */
609 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
drhc977f7f2002-05-21 11:38:11 +0000610){
drhdca76842004-12-07 14:06:13 +0000611 int mask = 0;
danielk19772f886d12009-02-28 10:47:41 +0000612 Trigger *pList = sqlite3TriggerList(pParse, pTab);
613 Trigger *p;
614 assert( pList==0 || IsVirtual(pTab)==0 );
615 for(p=pList; p; p=p->pNext){
drh9ab4c2e2009-05-09 00:18:38 +0000616 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
danielk19772f886d12009-02-28 10:47:41 +0000617 mask |= p->tr_tm;
danielk1977c3f9bad2002-05-15 08:30:12 +0000618 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000619 }
danielk19772f886d12009-02-28 10:47:41 +0000620 if( pMask ){
621 *pMask = mask;
622 }
623 return (mask ? pList : 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000624}
625
drhc977f7f2002-05-21 11:38:11 +0000626/*
drhf26e09c2003-05-31 16:21:12 +0000627** Convert the pStep->target token into a SrcList and return a pointer
628** to that SrcList.
629**
630** This routine adds a specific database name, if needed, to the target when
631** forming the SrcList. This prevents a trigger in one database from
632** referring to a target in another database. An exception is when the
633** trigger is in TEMP in which case it can refer to any other database it
634** wants.
635*/
636static SrcList *targetSrcList(
637 Parse *pParse, /* The parsing context */
638 TriggerStep *pStep /* The trigger containing the target token */
639){
drhf26e09c2003-05-31 16:21:12 +0000640 int iDb; /* Index of the database to use */
641 SrcList *pSrc; /* SrcList to be returned */
642
drhb7916a72009-05-27 10:31:29 +0000643 pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
644 if( pSrc ){
645 assert( pSrc->nSrc>0 );
646 assert( pSrc->a!=0 );
647 iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
648 if( iDb==0 || iDb>=2 ){
649 sqlite3 *db = pParse->db;
650 assert( iDb<pParse->db->nDb );
651 pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
652 }
drhf26e09c2003-05-31 16:21:12 +0000653 }
654 return pSrc;
655}
656
657/*
drhc977f7f2002-05-21 11:38:11 +0000658** Generate VDBE code for zero or more statements inside the body of a
659** trigger.
660*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000661static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000662 Parse *pParse, /* The parser context */
663 TriggerStep *pStepList, /* List of statements inside the trigger body */
664 int orconfin /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000665){
666 TriggerStep * pTriggerStep = pStepList;
667 int orconf;
drh344737f2004-09-19 00:50:20 +0000668 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000669 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000670
drh344737f2004-09-19 00:50:20 +0000671 assert( pTriggerStep!=0 );
672 assert( v!=0 );
drh66a51672008-01-03 00:01:23 +0000673 sqlite3VdbeAddOp2(v, OP_ContextPush, 0, 0);
drhd4e70eb2008-01-02 00:34:36 +0000674 VdbeComment((v, "begin trigger %s", pStepList->pTrig->name));
danielk1977633ed082002-05-17 00:05:58 +0000675 while( pTriggerStep ){
drhceea3322009-04-23 13:22:42 +0000676 sqlite3ExprCacheClear(pParse);
danielk1977633ed082002-05-17 00:05:58 +0000677 orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin;
678 pParse->trigStack->orconf = orconf;
679 switch( pTriggerStep->op ){
danielk1977633ed082002-05-17 00:05:58 +0000680 case TK_UPDATE: {
drh113088e2003-03-20 01:16:58 +0000681 SrcList *pSrc;
drhf26e09c2003-05-31 16:21:12 +0000682 pSrc = targetSrcList(pParse, pTriggerStep);
drh66a51672008-01-03 00:01:23 +0000683 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000684 sqlite3Update(pParse, pSrc,
danielk19776ab3a2e2009-02-19 14:39:25 +0000685 sqlite3ExprListDup(db, pTriggerStep->pExprList, 0),
686 sqlite3ExprDup(db, pTriggerStep->pWhere, 0), orconf);
drh66a51672008-01-03 00:01:23 +0000687 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000688 break;
689 }
690 case TK_INSERT: {
drh113088e2003-03-20 01:16:58 +0000691 SrcList *pSrc;
drhf26e09c2003-05-31 16:21:12 +0000692 pSrc = targetSrcList(pParse, pTriggerStep);
drh66a51672008-01-03 00:01:23 +0000693 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000694 sqlite3Insert(pParse, pSrc,
danielk19776ab3a2e2009-02-19 14:39:25 +0000695 sqlite3ExprListDup(db, pTriggerStep->pExprList, 0),
696 sqlite3SelectDup(db, pTriggerStep->pSelect, 0),
drh17435752007-08-16 04:30:38 +0000697 sqlite3IdListDup(db, pTriggerStep->pIdList), orconf);
drh66a51672008-01-03 00:01:23 +0000698 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000699 break;
700 }
701 case TK_DELETE: {
drh113088e2003-03-20 01:16:58 +0000702 SrcList *pSrc;
drh66a51672008-01-03 00:01:23 +0000703 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
drhf26e09c2003-05-31 16:21:12 +0000704 pSrc = targetSrcList(pParse, pTriggerStep);
drh17435752007-08-16 04:30:38 +0000705 sqlite3DeleteFrom(pParse, pSrc,
danielk19776ab3a2e2009-02-19 14:39:25 +0000706 sqlite3ExprDup(db, pTriggerStep->pWhere, 0));
drh66a51672008-01-03 00:01:23 +0000707 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000708 break;
709 }
drh9ab4c2e2009-05-09 00:18:38 +0000710 default: assert( pTriggerStep->op==TK_SELECT ); {
711 Select *ss = sqlite3SelectDup(db, pTriggerStep->pSelect, 0);
712 if( ss ){
713 SelectDest dest;
714
715 sqlite3SelectDestInit(&dest, SRT_Discard, 0);
716 sqlite3Select(pParse, ss, &dest);
717 sqlite3SelectDelete(db, ss);
718 }
719 break;
720 }
danielk1977633ed082002-05-17 00:05:58 +0000721 }
danielk1977633ed082002-05-17 00:05:58 +0000722 pTriggerStep = pTriggerStep->pNext;
723 }
drh66a51672008-01-03 00:01:23 +0000724 sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
drhd4e70eb2008-01-02 00:34:36 +0000725 VdbeComment((v, "end trigger %s", pStepList->pTrig->name));
danielk1977c3f9bad2002-05-15 08:30:12 +0000726
danielk1977633ed082002-05-17 00:05:58 +0000727 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000728}
729
danielk1977633ed082002-05-17 00:05:58 +0000730/*
731** This is called to code FOR EACH ROW triggers.
732**
733** When the code that this function generates is executed, the following
734** must be true:
drhc977f7f2002-05-21 11:38:11 +0000735**
736** 1. No cursors may be open in the main database. (But newIdx and oldIdx
737** can be indices of cursors in temporary tables. See below.)
738**
danielk1977633ed082002-05-17 00:05:58 +0000739** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then
740** a temporary vdbe cursor (index newIdx) must be open and pointing at
741** a row containing values to be substituted for new.* expressions in the
742** trigger program(s).
drhc977f7f2002-05-21 11:38:11 +0000743**
danielk1977633ed082002-05-17 00:05:58 +0000744** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then
745** a temporary vdbe cursor (index oldIdx) must be open and pointing at
746** a row containing values to be substituted for old.* expressions in the
747** trigger program(s).
748**
danielk19778f2c54e2008-01-01 19:02:09 +0000749** If they are not NULL, the piOldColMask and piNewColMask output variables
750** are set to values that describe the columns used by the trigger program
751** in the OLD.* and NEW.* tables respectively. If column N of the
752** pseudo-table is read at least once, the corresponding bit of the output
753** mask is set. If a column with an index greater than 32 is read, the
754** output mask is set to the special value 0xffffffff.
755**
danielk1977633ed082002-05-17 00:05:58 +0000756*/
danielk19774adee202004-05-08 08:23:19 +0000757int sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +0000758 Parse *pParse, /* Parse context */
danielk19772f886d12009-02-28 10:47:41 +0000759 Trigger *pTrigger, /* List of triggers on table pTab */
danielk1977633ed082002-05-17 00:05:58 +0000760 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
761 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +0000762 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +0000763 Table *pTab, /* The table to code triggers from */
764 int newIdx, /* The indice of the "new" row to access */
765 int oldIdx, /* The indice of the "old" row to access */
danielk19776f349032002-06-11 02:25:40 +0000766 int orconf, /* ON CONFLICT policy */
danielk19778f2c54e2008-01-01 19:02:09 +0000767 int ignoreJump, /* Instruction to jump to for RAISE(IGNORE) */
768 u32 *piOldColMask, /* OUT: Mask of columns used from the OLD.* table */
769 u32 *piNewColMask /* OUT: Mask of columns used from the NEW.* table */
drhc977f7f2002-05-21 11:38:11 +0000770){
danielk1977eecfb3e2006-01-10 12:31:39 +0000771 Trigger *p;
drh0e359b32008-01-13 19:02:11 +0000772 sqlite3 *db = pParse->db;
drh67040462004-09-24 12:24:36 +0000773 TriggerStack trigStackEntry;
danielk1977c3f9bad2002-05-15 08:30:12 +0000774
danielk19778f2c54e2008-01-01 19:02:09 +0000775 trigStackEntry.oldColMask = 0;
776 trigStackEntry.newColMask = 0;
777
danielk1977c3f9bad2002-05-15 08:30:12 +0000778 assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE);
drhdca76842004-12-07 14:06:13 +0000779 assert(tr_tm == TRIGGER_BEFORE || tr_tm == TRIGGER_AFTER );
danielk1977c3f9bad2002-05-15 08:30:12 +0000780
danielk1977633ed082002-05-17 00:05:58 +0000781 assert(newIdx != -1 || oldIdx != -1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000782
danielk19772f886d12009-02-28 10:47:41 +0000783 for(p=pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +0000784 int fire_this = 0;
785
drh9ab4c2e2009-05-09 00:18:38 +0000786 /* Sanity checking: The schema for the trigger and for the table are
787 ** always defined. The trigger must be in the same schema as the table
788 ** or else it must be a TEMP trigger. */
789 assert( p->pSchema!=0 );
790 assert( p->pTabSchema!=0 );
791 assert( p->pSchema==p->pTabSchema || p->pSchema==db->aDb[1].pSchema );
792
danielk1977eecfb3e2006-01-10 12:31:39 +0000793 /* Determine whether we should code this trigger */
794 if(
795 p->op==op &&
796 p->tr_tm==tr_tm &&
drh9ab4c2e2009-05-09 00:18:38 +0000797 checkColumnOverlap(p->pColumns,pChanges)
danielk1977eecfb3e2006-01-10 12:31:39 +0000798 ){
799 TriggerStack *pS; /* Pointer to trigger-stack entry */
drh74161702006-02-24 02:53:49 +0000800 for(pS=pParse->trigStack; pS && p!=pS->pTrigger; pS=pS->pNext){}
danielk1977eecfb3e2006-01-10 12:31:39 +0000801 if( !pS ){
802 fire_this = 1;
danielk1977f29ce552002-05-19 23:43:12 +0000803 }
drh229caa32006-03-25 15:52:19 +0000804#if 0 /* Give no warning for recursive triggers. Just do not do them */
805 else{
806 sqlite3ErrorMsg(pParse, "recursive triggers not supported (%s)",
807 p->name);
808 return SQLITE_ERROR;
809 }
810#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000811 }
drhad6d9462004-09-19 02:15:24 +0000812
drh67040462004-09-24 12:24:36 +0000813 if( fire_this ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000814 int endTrigger;
danielk1977c3f9bad2002-05-15 08:30:12 +0000815 Expr * whenExpr;
drh85e20962003-04-25 17:52:11 +0000816 AuthContext sContext;
danielk1977b3bce662005-01-29 08:32:43 +0000817 NameContext sNC;
danielk1977c3f9bad2002-05-15 08:30:12 +0000818
drh949f9cd2008-01-12 21:35:57 +0000819#ifndef SQLITE_OMIT_TRACE
820 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Trace, 0, 0, 0,
drh0e359b32008-01-13 19:02:11 +0000821 sqlite3MPrintf(db, "-- TRIGGER %s", p->name),
drh949f9cd2008-01-12 21:35:57 +0000822 P4_DYNAMIC);
823#endif
danielk1977b3bce662005-01-29 08:32:43 +0000824 memset(&sNC, 0, sizeof(sNC));
825 sNC.pParse = pParse;
danielk1977c3f9bad2002-05-15 08:30:12 +0000826
827 /* Push an entry on to the trigger stack */
danielk1977eecfb3e2006-01-10 12:31:39 +0000828 trigStackEntry.pTrigger = p;
drh67040462004-09-24 12:24:36 +0000829 trigStackEntry.newIdx = newIdx;
830 trigStackEntry.oldIdx = oldIdx;
831 trigStackEntry.pTab = pTab;
832 trigStackEntry.pNext = pParse->trigStack;
833 trigStackEntry.ignoreJump = ignoreJump;
834 pParse->trigStack = &trigStackEntry;
danielk1977eecfb3e2006-01-10 12:31:39 +0000835 sqlite3AuthContextPush(pParse, &sContext, p->name);
danielk1977c3f9bad2002-05-15 08:30:12 +0000836
837 /* code the WHEN clause */
danielk19774adee202004-05-08 08:23:19 +0000838 endTrigger = sqlite3VdbeMakeLabel(pParse->pVdbe);
danielk19776ab3a2e2009-02-19 14:39:25 +0000839 whenExpr = sqlite3ExprDup(db, p->pWhen, 0);
drh7d10d5a2008-08-20 16:35:10 +0000840 if( db->mallocFailed || sqlite3ResolveExprNames(&sNC, whenExpr) ){
drh67040462004-09-24 12:24:36 +0000841 pParse->trigStack = trigStackEntry.pNext;
drh633e6d52008-07-28 19:34:53 +0000842 sqlite3ExprDelete(db, whenExpr);
drh9adf9ac2002-05-15 11:44:13 +0000843 return 1;
danielk1977c3f9bad2002-05-15 08:30:12 +0000844 }
drh35573352008-01-08 23:54:25 +0000845 sqlite3ExprIfFalse(pParse, whenExpr, endTrigger, SQLITE_JUMPIFNULL);
drh633e6d52008-07-28 19:34:53 +0000846 sqlite3ExprDelete(db, whenExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000847
danielk1977eecfb3e2006-01-10 12:31:39 +0000848 codeTriggerProgram(pParse, p->step_list, orconf);
danielk1977c3f9bad2002-05-15 08:30:12 +0000849
850 /* Pop the entry off the trigger stack */
drh67040462004-09-24 12:24:36 +0000851 pParse->trigStack = trigStackEntry.pNext;
danielk19774adee202004-05-08 08:23:19 +0000852 sqlite3AuthContextPop(&sContext);
danielk1977c3f9bad2002-05-15 08:30:12 +0000853
danielk19774adee202004-05-08 08:23:19 +0000854 sqlite3VdbeResolveLabel(pParse->pVdbe, endTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000855 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000856 }
danielk19778f2c54e2008-01-01 19:02:09 +0000857 if( piOldColMask ) *piOldColMask |= trigStackEntry.oldColMask;
858 if( piNewColMask ) *piNewColMask |= trigStackEntry.newColMask;
danielk1977c3f9bad2002-05-15 08:30:12 +0000859 return 0;
860}
drhb7f91642004-10-31 02:22:47 +0000861#endif /* !defined(SQLITE_OMIT_TRIGGER) */