blob: 98a49eddcd019d58e69838389e34fe05d4bf5e5e [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**
drhceea3322009-04-23 13:22:42 +000013** $Id: trigger.c,v 1.136 2009/04/23 13:22:44 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 if( pTmp->target.dyn ) sqlite3DbFree(db, (char*)pTmp->target.z);
27 sqlite3ExprDelete(db, pTmp->pWhere);
28 sqlite3ExprListDelete(db, pTmp->pExprList);
29 sqlite3SelectDelete(db, pTmp->pSelect);
30 sqlite3IdListDelete(db, pTmp->pIdList);
drh4b59ab52002-08-24 18:24:51 +000031
drh633e6d52008-07-28 19:34:53 +000032 sqlite3DbFree(db, pTmp);
drh4b59ab52002-08-24 18:24:51 +000033 }
34}
35
36/*
danielk19772f886d12009-02-28 10:47:41 +000037** Given table pTab, return a list of all the triggers attached to
38** the table. The list is connected by Trigger.pNext pointers.
39*/
40Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
41 Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
42 Trigger *pList = 0; /* List of triggers to return */
43
44 if( pTmpSchema!=pTab->pSchema ){
45 HashElem *p;
46 for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
47 Trigger *pTrig = (Trigger *)sqliteHashData(p);
48 if( pTrig->pTabSchema==pTab->pSchema
49 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
50 ){
51 pTrig->pNext = (pList ? pList : pTab->pTrigger);
52 pList = pTrig;
53 }
54 }
55 }
56
57 return (pList ? pList : pTab->pTrigger);
58}
59
60/*
drhf0f258b2003-04-21 18:48:45 +000061** This is called by the parser when it sees a CREATE TRIGGER statement
62** up to the point of the BEGIN before the trigger actions. A Trigger
63** structure is generated based on the information available and stored
64** in pParse->pNewTrigger. After the trigger actions have been parsed, the
danielk19774adee202004-05-08 08:23:19 +000065** sqlite3FinishTrigger() function is called to complete the trigger
drhf0f258b2003-04-21 18:48:45 +000066** construction process.
drh9adf9ac2002-05-15 11:44:13 +000067*/
danielk19774adee202004-05-08 08:23:19 +000068void sqlite3BeginTrigger(
drh9adf9ac2002-05-15 11:44:13 +000069 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
danielk1977ef2cb632004-05-29 02:37:19 +000070 Token *pName1, /* The name of the trigger */
71 Token *pName2, /* The name of the trigger */
drh5cf590c2003-04-24 01:45:04 +000072 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
drh9adf9ac2002-05-15 11:44:13 +000073 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
danielk1977633ed082002-05-17 00:05:58 +000074 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
drhd24cc422003-03-27 12:51:24 +000075 SrcList *pTableName,/* The name of the table/view the trigger applies to */
drh9adf9ac2002-05-15 11:44:13 +000076 Expr *pWhen, /* WHEN clause */
drhfdd48a72006-09-11 23:45:48 +000077 int isTemp, /* True if the TEMPORARY keyword is present */
78 int noErr /* Suppress errors if the trigger already exists */
drh9adf9ac2002-05-15 11:44:13 +000079){
danielk1977d5d56522005-03-16 12:15:20 +000080 Trigger *pTrigger = 0;
danielk1977ef2cb632004-05-29 02:37:19 +000081 Table *pTab;
drhf0f258b2003-04-21 18:48:45 +000082 char *zName = 0; /* Name of the trigger */
drh9bb575f2004-09-06 17:24:11 +000083 sqlite3 *db = pParse->db;
danielk1977ef2cb632004-05-29 02:37:19 +000084 int iDb; /* The database to store the trigger in */
85 Token *pName; /* The unqualified db name */
drh4312db52003-06-03 01:47:11 +000086 DbFixer sFix;
danielk1977da184232006-01-05 11:34:32 +000087 int iTabDb;
drhed6c8672003-01-12 18:02:16 +000088
drh43617e92006-03-06 20:55:46 +000089 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
90 assert( pName2!=0 );
drhaa78bec2008-12-09 03:55:14 +000091 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
92 assert( op>0 && op<0xff );
danielk1977ef2cb632004-05-29 02:37:19 +000093 if( isTemp ){
94 /* If TEMP was specified, then the trigger name may not be qualified. */
drh43617e92006-03-06 20:55:46 +000095 if( pName2->n>0 ){
danielk1977ef2cb632004-05-29 02:37:19 +000096 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
97 goto trigger_cleanup;
98 }
99 iDb = 1;
100 pName = pName1;
101 }else{
102 /* Figure out the db that the the trigger will be created in */
103 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
104 if( iDb<0 ){
105 goto trigger_cleanup;
106 }
107 }
108
109 /* If the trigger name was unqualified, and the table is a temp table,
110 ** then set iDb to 1 to create the trigger in the temporary database.
111 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
112 ** exist, the error is caught by the block below.
drh9adf9ac2002-05-15 11:44:13 +0000113 */
drh17435752007-08-16 04:30:38 +0000114 if( !pTableName || db->mallocFailed ){
drh6f7adc82006-01-11 21:41:20 +0000115 goto trigger_cleanup;
116 }
danielk1977ef2cb632004-05-29 02:37:19 +0000117 pTab = sqlite3SrcListLookup(pParse, pTableName);
danielk1977da184232006-01-05 11:34:32 +0000118 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977ef2cb632004-05-29 02:37:19 +0000119 iDb = 1;
120 }
121
122 /* Ensure the table name matches database name and that the table exists */
drh17435752007-08-16 04:30:38 +0000123 if( db->mallocFailed ) goto trigger_cleanup;
drhd24cc422003-03-27 12:51:24 +0000124 assert( pTableName->nSrc==1 );
danielk1977ef2cb632004-05-29 02:37:19 +0000125 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) &&
126 sqlite3FixSrcList(&sFix, pTableName) ){
drh4312db52003-06-03 01:47:11 +0000127 goto trigger_cleanup;
drhf26e09c2003-05-31 16:21:12 +0000128 }
danielk1977ef2cb632004-05-29 02:37:19 +0000129 pTab = sqlite3SrcListLookup(pParse, pTableName);
130 if( !pTab ){
131 /* The table does not exist. */
drhd24cc422003-03-27 12:51:24 +0000132 goto trigger_cleanup;
133 }
drh4cbdda92006-06-14 19:00:20 +0000134 if( IsVirtual(pTab) ){
135 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
136 goto trigger_cleanup;
137 }
drhd24cc422003-03-27 12:51:24 +0000138
danielk1977d8123362004-06-12 09:25:12 +0000139 /* Check that the trigger name is not reserved and that no trigger of the
140 ** specified name exists */
drh17435752007-08-16 04:30:38 +0000141 zName = sqlite3NameFromToken(db, pName);
danielk1977d8123362004-06-12 09:25:12 +0000142 if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
143 goto trigger_cleanup;
144 }
drhaa78bec2008-12-09 03:55:14 +0000145 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
drhea678832008-12-10 19:26:22 +0000146 zName, sqlite3Strlen30(zName)) ){
drhfdd48a72006-09-11 23:45:48 +0000147 if( !noErr ){
148 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
149 }
drhe5f9c642003-01-13 23:27:31 +0000150 goto trigger_cleanup;
danielk1977c3f9bad2002-05-15 08:30:12 +0000151 }
danielk1977ef2cb632004-05-29 02:37:19 +0000152
153 /* Do not create a trigger on a system table */
drhdca76842004-12-07 14:06:13 +0000154 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk19774adee202004-05-08 08:23:19 +0000155 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
drhd24cc422003-03-27 12:51:24 +0000156 pParse->nErr++;
157 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40 +0000158 }
danielk1977ef2cb632004-05-29 02:37:19 +0000159
160 /* INSTEAD of triggers are only for views and views only support INSTEAD
161 ** of triggers.
162 */
163 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000164 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drhda93d232003-03-31 02:12:46 +0000165 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000166 goto trigger_cleanup;
167 }
danielk1977ef2cb632004-05-29 02:37:19 +0000168 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000169 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drhda93d232003-03-31 02:12:46 +0000170 " trigger on table: %S", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000171 goto trigger_cleanup;
172 }
danielk1977da184232006-01-05 11:34:32 +0000173 iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977ef2cb632004-05-29 02:37:19 +0000174
drhd24cc422003-03-27 12:51:24 +0000175#ifndef SQLITE_OMIT_AUTHORIZATION
176 {
177 int code = SQLITE_CREATE_TRIGGER;
danielk1977da184232006-01-05 11:34:32 +0000178 const char *zDb = db->aDb[iTabDb].zName;
drhe22a3342003-04-22 20:30:37 +0000179 const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
danielk1977da184232006-01-05 11:34:32 +0000180 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19 +0000181 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24 +0000182 goto trigger_cleanup;
183 }
danielk1977da184232006-01-05 11:34:32 +0000184 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24 +0000185 goto trigger_cleanup;
186 }
187 }
188#endif
danielk1977d702fcc2002-05-26 23:24:40 +0000189
danielk1977ef2cb632004-05-29 02:37:19 +0000190 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04 +0000191 ** cannot appear on views. So we might as well translate every
192 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
193 ** elsewhere.
194 */
danielk1977d702fcc2002-05-26 23:24:40 +0000195 if (tr_tm == TK_INSTEAD){
196 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12 +0000197 }
198
199 /* Build the Trigger object */
drh17435752007-08-16 04:30:38 +0000200 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19 +0000201 if( pTrigger==0 ) goto trigger_cleanup;
202 pTrigger->name = zName;
drhe5f9c642003-01-13 23:27:31 +0000203 zName = 0;
drh17435752007-08-16 04:30:38 +0000204 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32 +0000205 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48 +0000206 pTrigger->pTabSchema = pTab->pSchema;
drhaa78bec2008-12-09 03:55:14 +0000207 pTrigger->op = (u8)op;
drhdca76842004-12-07 14:06:13 +0000208 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
danielk19776ab3a2e2009-02-19 14:39:25 +0000209 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
drh17435752007-08-16 04:30:38 +0000210 pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
211 sqlite3TokenCopy(db, &pTrigger->nameToken,pName);
drhf0f258b2003-04-21 18:48:45 +0000212 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19 +0000213 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45 +0000214
215trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000216 sqlite3DbFree(db, zName);
217 sqlite3SrcListDelete(db, pTableName);
218 sqlite3IdListDelete(db, pColumns);
219 sqlite3ExprDelete(db, pWhen);
danielk1977d5d56522005-03-16 12:15:20 +0000220 if( !pParse->pNewTrigger ){
drh633e6d52008-07-28 19:34:53 +0000221 sqlite3DeleteTrigger(db, pTrigger);
danielk1977d5d56522005-03-16 12:15:20 +0000222 }else{
223 assert( pParse->pNewTrigger==pTrigger );
224 }
drhf0f258b2003-04-21 18:48:45 +0000225}
226
227/*
228** This routine is called after all of the trigger actions have been parsed
229** in order to complete the process of building the trigger.
230*/
danielk19774adee202004-05-08 08:23:19 +0000231void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45 +0000232 Parse *pParse, /* Parser context */
233 TriggerStep *pStepList, /* The triggered program */
234 Token *pAll /* Token that describes the complete CREATE TRIGGER */
235){
danielk19772f886d12009-02-28 10:47:41 +0000236 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
237 char *zName; /* Name of trigger */
238 sqlite3 *db = pParse->db; /* The database */
drhf26e09c2003-05-31 16:21:12 +0000239 DbFixer sFix;
danielk19772f886d12009-02-28 10:47:41 +0000240 int iDb; /* Database containing the trigger */
drhf0f258b2003-04-21 18:48:45 +0000241
danielk1977bfb9e352005-01-24 13:03:32 +0000242 pTrig = pParse->pNewTrigger;
drhf0f258b2003-04-21 18:48:45 +0000243 pParse->pNewTrigger = 0;
danielk19772e588c72005-12-09 14:25:08 +0000244 if( pParse->nErr || !pTrig ) goto triggerfinish_cleanup;
danielk19772f886d12009-02-28 10:47:41 +0000245 zName = pTrig->name;
danielk1977da184232006-01-05 11:34:32 +0000246 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32 +0000247 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53 +0000248 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32 +0000249 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53 +0000250 pStepList = pStepList->pNext;
251 }
danielk1977da184232006-01-05 11:34:32 +0000252 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &pTrig->nameToken)
danielk1977bfb9e352005-01-24 13:03:32 +0000253 && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
drhf26e09c2003-05-31 16:21:12 +0000254 goto triggerfinish_cleanup;
255 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000256
257 /* if we are not initializing, and this trigger is not on a TEMP table,
drh9adf9ac2002-05-15 11:44:13 +0000258 ** build the sqlite_master entry
259 */
drh1d85d932004-02-14 23:05:52 +0000260 if( !db->init.busy ){
drhc977f7f2002-05-21 11:38:11 +0000261 Vdbe *v;
drh2d401ab2008-01-10 23:50:11 +0000262 char *z;
danielk1977c3f9bad2002-05-15 08:30:12 +0000263
264 /* Make an entry in the sqlite_master table */
danielk19774adee202004-05-08 08:23:19 +0000265 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000266 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000267 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11 +0000268 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
269 sqlite3NestedParse(pParse,
270 "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
danielk19772f886d12009-02-28 10:47:41 +0000271 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
drh2d401ab2008-01-10 23:50:11 +0000272 pTrig->table, z);
drh633e6d52008-07-28 19:34:53 +0000273 sqlite3DbFree(db, z);
drh9cbf3422008-01-17 16:22:13 +0000274 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +0000275 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, sqlite3MPrintf(
danielk19772f886d12009-02-28 10:47:41 +0000276 db, "type='trigger' AND name='%q'", zName), P4_DYNAMIC
danielk19771e536952007-08-16 10:09:01 +0000277 );
danielk1977c3f9bad2002-05-15 08:30:12 +0000278 }
279
drh956bc922004-07-24 17:38:29 +0000280 if( db->init.busy ){
danielk19772f886d12009-02-28 10:47:41 +0000281 Trigger *pLink = pTrig;
282 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
283 pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);
284 if( pTrig ){
drhf3a65f72007-08-22 20:18:21 +0000285 db->mallocFailed = 1;
danielk19772f886d12009-02-28 10:47:41 +0000286 }else if( pLink->pSchema==pLink->pTabSchema ){
287 Table *pTab;
288 int n = sqlite3Strlen30(pLink->table) + 1;
289 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n);
290 assert( pTab!=0 );
291 pLink->pNext = pTab->pTrigger;
292 pTab->pTrigger = pLink;
danielk1977d5d56522005-03-16 12:15:20 +0000293 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000294 }
295
drhf0f258b2003-04-21 18:48:45 +0000296triggerfinish_cleanup:
drh633e6d52008-07-28 19:34:53 +0000297 sqlite3DeleteTrigger(db, pTrig);
danielk1977bfb9e352005-01-24 13:03:32 +0000298 assert( !pParse->pNewTrigger );
drh633e6d52008-07-28 19:34:53 +0000299 sqlite3DeleteTriggerStep(db, pStepList);
drh4b59ab52002-08-24 18:24:51 +0000300}
danielk1977c3f9bad2002-05-15 08:30:12 +0000301
drh4b59ab52002-08-24 18:24:51 +0000302/*
303** Make a copy of all components of the given trigger step. This has
304** the effect of copying all Expr.token.z values into memory obtained
drh17435752007-08-16 04:30:38 +0000305** from sqlite3_malloc(). As initially created, the Expr.token.z values
drh4b59ab52002-08-24 18:24:51 +0000306** all point to the input string that was fed to the parser. But that
danielk19776f8a5032004-05-10 10:34:51 +0000307** string is ephemeral - it will go away as soon as the sqlite3_exec()
drh4b59ab52002-08-24 18:24:51 +0000308** call that started the parser exits. This routine makes a persistent
309** copy of all the Expr.token.z strings so that the TriggerStep structure
danielk19776f8a5032004-05-10 10:34:51 +0000310** will be valid even after the sqlite3_exec() call returns.
drh4b59ab52002-08-24 18:24:51 +0000311*/
drh17435752007-08-16 04:30:38 +0000312static void sqlitePersistTriggerStep(sqlite3 *db, TriggerStep *p){
drh4b59ab52002-08-24 18:24:51 +0000313 if( p->target.z ){
drh17435752007-08-16 04:30:38 +0000314 p->target.z = (u8*)sqlite3DbStrNDup(db, (char*)p->target.z, p->target.n);
drh4b59ab52002-08-24 18:24:51 +0000315 p->target.dyn = 1;
316 }
317 if( p->pSelect ){
danielk19776ab3a2e2009-02-19 14:39:25 +0000318 Select *pNew = sqlite3SelectDup(db, p->pSelect, 1);
drh633e6d52008-07-28 19:34:53 +0000319 sqlite3SelectDelete(db, p->pSelect);
drh4b59ab52002-08-24 18:24:51 +0000320 p->pSelect = pNew;
321 }
322 if( p->pWhere ){
danielk19776ab3a2e2009-02-19 14:39:25 +0000323 Expr *pNew = sqlite3ExprDup(db, p->pWhere, EXPRDUP_REDUCE);
drh633e6d52008-07-28 19:34:53 +0000324 sqlite3ExprDelete(db, p->pWhere);
drh4b59ab52002-08-24 18:24:51 +0000325 p->pWhere = pNew;
326 }
327 if( p->pExprList ){
danielk19776ab3a2e2009-02-19 14:39:25 +0000328 ExprList *pNew = sqlite3ExprListDup(db, p->pExprList, 1);
drh633e6d52008-07-28 19:34:53 +0000329 sqlite3ExprListDelete(db, p->pExprList);
drh4b59ab52002-08-24 18:24:51 +0000330 p->pExprList = pNew;
331 }
332 if( p->pIdList ){
drh17435752007-08-16 04:30:38 +0000333 IdList *pNew = sqlite3IdListDup(db, p->pIdList);
drh633e6d52008-07-28 19:34:53 +0000334 sqlite3IdListDelete(db, p->pIdList);
drh4b59ab52002-08-24 18:24:51 +0000335 p->pIdList = pNew;
danielk1977c3f9bad2002-05-15 08:30:12 +0000336 }
337}
338
drhc977f7f2002-05-21 11:38:11 +0000339/*
340** Turn a SELECT statement (that the pSelect parameter points to) into
341** a trigger step. Return a pointer to a TriggerStep structure.
342**
343** The parser calls this routine when it finds a SELECT statement in
344** body of a TRIGGER.
345*/
drh17435752007-08-16 04:30:38 +0000346TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
347 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000348 if( pTriggerStep==0 ) {
drh633e6d52008-07-28 19:34:53 +0000349 sqlite3SelectDelete(db, pSelect);
danielk19772e588c72005-12-09 14:25:08 +0000350 return 0;
351 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000352
danielk1977633ed082002-05-17 00:05:58 +0000353 pTriggerStep->op = TK_SELECT;
354 pTriggerStep->pSelect = pSelect;
355 pTriggerStep->orconf = OE_Default;
drh17435752007-08-16 04:30:38 +0000356 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977c3f9bad2002-05-15 08:30:12 +0000357
danielk1977633ed082002-05-17 00:05:58 +0000358 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000359}
360
drhc977f7f2002-05-21 11:38:11 +0000361/*
362** Build a trigger step out of an INSERT statement. Return a pointer
363** to the new trigger step.
364**
365** The parser calls this routine when it sees an INSERT inside the
366** body of a trigger.
367*/
danielk19774adee202004-05-08 08:23:19 +0000368TriggerStep *sqlite3TriggerInsertStep(
drh17435752007-08-16 04:30:38 +0000369 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000370 Token *pTableName, /* Name of the table into which we insert */
371 IdList *pColumn, /* List of columns in pTableName to insert into */
372 ExprList *pEList, /* The VALUE clause: a list of values to be inserted */
373 Select *pSelect, /* A SELECT statement that supplies values */
374 int orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
danielk1977633ed082002-05-17 00:05:58 +0000375){
drhf3a65f72007-08-22 20:18:21 +0000376 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000377
danielk1977633ed082002-05-17 00:05:58 +0000378 assert(pEList == 0 || pSelect == 0);
drhf3a65f72007-08-22 20:18:21 +0000379 assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000380
drhf3a65f72007-08-22 20:18:21 +0000381 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk1977d5d56522005-03-16 12:15:20 +0000382 if( pTriggerStep ){
383 pTriggerStep->op = TK_INSERT;
384 pTriggerStep->pSelect = pSelect;
385 pTriggerStep->target = *pTableName;
386 pTriggerStep->pIdList = pColumn;
387 pTriggerStep->pExprList = pEList;
388 pTriggerStep->orconf = orconf;
drh17435752007-08-16 04:30:38 +0000389 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977d5d56522005-03-16 12:15:20 +0000390 }else{
drh633e6d52008-07-28 19:34:53 +0000391 sqlite3IdListDelete(db, pColumn);
392 sqlite3ExprListDelete(db, pEList);
393 sqlite3SelectDelete(db, pSelect);
danielk1977d5d56522005-03-16 12:15:20 +0000394 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000395
danielk1977633ed082002-05-17 00:05:58 +0000396 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000397}
398
drhc977f7f2002-05-21 11:38:11 +0000399/*
400** Construct a trigger step that implements an UPDATE statement and return
401** a pointer to that trigger step. The parser calls this routine when it
402** sees an UPDATE statement inside the body of a CREATE TRIGGER.
403*/
danielk19774adee202004-05-08 08:23:19 +0000404TriggerStep *sqlite3TriggerUpdateStep(
drh17435752007-08-16 04:30:38 +0000405 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000406 Token *pTableName, /* Name of the table to be updated */
407 ExprList *pEList, /* The SET clause: list of column and new values */
408 Expr *pWhere, /* The WHERE clause */
409 int orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
410){
drh17435752007-08-16 04:30:38 +0000411 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
drh0e3a6f32007-03-30 20:40:34 +0000412 if( pTriggerStep==0 ){
drh633e6d52008-07-28 19:34:53 +0000413 sqlite3ExprListDelete(db, pEList);
414 sqlite3ExprDelete(db, pWhere);
drh0e3a6f32007-03-30 20:40:34 +0000415 return 0;
416 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000417
danielk1977633ed082002-05-17 00:05:58 +0000418 pTriggerStep->op = TK_UPDATE;
419 pTriggerStep->target = *pTableName;
420 pTriggerStep->pExprList = pEList;
421 pTriggerStep->pWhere = pWhere;
422 pTriggerStep->orconf = orconf;
drh17435752007-08-16 04:30:38 +0000423 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977c3f9bad2002-05-15 08:30:12 +0000424
danielk1977633ed082002-05-17 00:05:58 +0000425 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000426}
427
drhc977f7f2002-05-21 11:38:11 +0000428/*
429** Construct a trigger step that implements a DELETE statement and return
430** a pointer to that trigger step. The parser calls this routine when it
431** sees a DELETE statement inside the body of a CREATE TRIGGER.
432*/
drh17435752007-08-16 04:30:38 +0000433TriggerStep *sqlite3TriggerDeleteStep(
434 sqlite3 *db, /* Database connection */
435 Token *pTableName, /* The table from which rows are deleted */
436 Expr *pWhere /* The WHERE clause */
437){
438 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
drhb63a53d2007-03-31 01:34:44 +0000439 if( pTriggerStep==0 ){
drh633e6d52008-07-28 19:34:53 +0000440 sqlite3ExprDelete(db, pWhere);
drhb63a53d2007-03-31 01:34:44 +0000441 return 0;
442 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000443
danielk1977633ed082002-05-17 00:05:58 +0000444 pTriggerStep->op = TK_DELETE;
445 pTriggerStep->target = *pTableName;
446 pTriggerStep->pWhere = pWhere;
447 pTriggerStep->orconf = OE_Default;
drh17435752007-08-16 04:30:38 +0000448 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977c3f9bad2002-05-15 08:30:12 +0000449
danielk1977633ed082002-05-17 00:05:58 +0000450 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000451}
452
danielk1977633ed082002-05-17 00:05:58 +0000453/*
454** Recursively delete a Trigger structure
455*/
drh633e6d52008-07-28 19:34:53 +0000456void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
drhf0f258b2003-04-21 18:48:45 +0000457 if( pTrigger==0 ) return;
drh633e6d52008-07-28 19:34:53 +0000458 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
459 sqlite3DbFree(db, pTrigger->name);
460 sqlite3DbFree(db, pTrigger->table);
461 sqlite3ExprDelete(db, pTrigger->pWhen);
462 sqlite3IdListDelete(db, pTrigger->pColumns);
463 if( pTrigger->nameToken.dyn ) sqlite3DbFree(db, (char*)pTrigger->nameToken.z);
464 sqlite3DbFree(db, pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000465}
466
467/*
danielk19778a414492004-06-29 08:59:35 +0000468** This function is called to drop a trigger from the database schema.
469**
470** This may be called directly from the parser and therefore identifies
471** the trigger by name. The sqlite3DropTriggerPtr() routine does the
472** same job as this routine except it takes a pointer to the trigger
473** instead of the trigger name.
474**/
drhfdd48a72006-09-11 23:45:48 +0000475void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000476 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000477 int i;
478 const char *zDb;
479 const char *zName;
480 int nName;
drh9bb575f2004-09-06 17:24:11 +0000481 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000482
drh17435752007-08-16 04:30:38 +0000483 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000484 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000485 goto drop_trigger_cleanup;
486 }
danielk19778e227872004-06-07 07:52:17 +0000487
drhd24cc422003-03-27 12:51:24 +0000488 assert( pName->nSrc==1 );
489 zDb = pName->a[0].zDatabase;
490 zName = pName->a[0].zName;
drhea678832008-12-10 19:26:22 +0000491 nName = sqlite3Strlen30(zName);
danielk197753c0f742005-03-29 03:10:59 +0000492 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000493 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000494 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
drhe4df0e72006-03-29 00:24:06 +0000495 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
drhd24cc422003-03-27 12:51:24 +0000496 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000497 }
drhd24cc422003-03-27 12:51:24 +0000498 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000499 if( !noErr ){
500 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
501 }
drhd24cc422003-03-27 12:51:24 +0000502 goto drop_trigger_cleanup;
503 }
drh74161702006-02-24 02:53:49 +0000504 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000505
506drop_trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000507 sqlite3SrcListDelete(db, pName);
drh79a519c2003-05-17 19:04:03 +0000508}
509
510/*
drh956bc922004-07-24 17:38:29 +0000511** Return a pointer to the Table structure for the table that a trigger
512** is set on.
513*/
drh74161702006-02-24 02:53:49 +0000514static Table *tableOfTrigger(Trigger *pTrigger){
drhea678832008-12-10 19:26:22 +0000515 int n = sqlite3Strlen30(pTrigger->table) + 1;
danielk1977aaf22682006-01-06 15:03:48 +0000516 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
drh956bc922004-07-24 17:38:29 +0000517}
518
519
520/*
drh74161702006-02-24 02:53:49 +0000521** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000522*/
drh74161702006-02-24 02:53:49 +0000523void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000524 Table *pTable;
525 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000526 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000527 int iDb;
drh79a519c2003-05-17 19:04:03 +0000528
danielk1977da184232006-01-05 11:34:32 +0000529 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000530 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000531 pTable = tableOfTrigger(pTrigger);
drh43617e92006-03-06 20:55:46 +0000532 assert( pTable );
danielk1977da184232006-01-05 11:34:32 +0000533 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000534#ifndef SQLITE_OMIT_AUTHORIZATION
535 {
536 int code = SQLITE_DROP_TRIGGER;
drh956bc922004-07-24 17:38:29 +0000537 const char *zDb = db->aDb[iDb].zName;
538 const char *zTab = SCHEMA_TABLE(iDb);
539 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
danielk19774adee202004-05-08 08:23:19 +0000540 if( sqlite3AuthCheck(pParse, code, pTrigger->name, pTable->zName, zDb) ||
541 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000542 return;
drhe5f9c642003-01-13 23:27:31 +0000543 }
drhed6c8672003-01-12 18:02:16 +0000544 }
drhe5f9c642003-01-13 23:27:31 +0000545#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000546
drhf0f258b2003-04-21 18:48:45 +0000547 /* Generate code to destroy the database record of the trigger.
548 */
drh43617e92006-03-06 20:55:46 +0000549 assert( pTable!=0 );
550 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drhf0f258b2003-04-21 18:48:45 +0000551 int base;
drh57196282004-10-06 15:41:16 +0000552 static const VdbeOpList dropTrigger[] = {
drh9b1b01b2003-08-16 12:37:51 +0000553 { OP_Rewind, 0, ADDR(9), 0},
drh1db639c2008-01-17 02:36:28 +0000554 { OP_String8, 0, 1, 0}, /* 1 */
555 { OP_Column, 0, 1, 2},
556 { OP_Ne, 2, ADDR(8), 1},
557 { OP_String8, 0, 1, 0}, /* 4: "trigger" */
558 { OP_Column, 0, 0, 2},
559 { OP_Ne, 2, ADDR(8), 1},
drhf0f258b2003-04-21 18:48:45 +0000560 { OP_Delete, 0, 0, 0},
drh9b1b01b2003-08-16 12:37:51 +0000561 { OP_Next, 0, ADDR(1), 0}, /* 8 */
drhf0f258b2003-04-21 18:48:45 +0000562 };
563
drh956bc922004-07-24 17:38:29 +0000564 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk1977c00da102006-01-07 13:21:04 +0000565 sqlite3OpenMasterTable(pParse, iDb);
danielk19774adee202004-05-08 08:23:19 +0000566 base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger);
drh66a51672008-01-03 00:01:23 +0000567 sqlite3VdbeChangeP4(v, base+1, pTrigger->name, 0);
drh24003452008-01-03 01:28:59 +0000568 sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
drh9cbf3422008-01-17 16:22:13 +0000569 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +0000570 sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
571 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->name, 0);
danielk19776ab3a2e2009-02-19 14:39:25 +0000572 if( pParse->nMem<3 ){
573 pParse->nMem = 3;
574 }
drhf0f258b2003-04-21 18:48:45 +0000575 }
drh956bc922004-07-24 17:38:29 +0000576}
drhf0f258b2003-04-21 18:48:45 +0000577
drh956bc922004-07-24 17:38:29 +0000578/*
579** Remove a trigger from the hash tables of the sqlite* pointer.
580*/
581void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
danielk19772f886d12009-02-28 10:47:41 +0000582 Hash *pHash = &(db->aDb[iDb].pSchema->trigHash);
drh956bc922004-07-24 17:38:29 +0000583 Trigger *pTrigger;
danielk19772f886d12009-02-28 10:47:41 +0000584 pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0);
drh956bc922004-07-24 17:38:29 +0000585 if( pTrigger ){
danielk19772f886d12009-02-28 10:47:41 +0000586 if( pTrigger->pSchema==pTrigger->pTabSchema ){
587 Table *pTab = tableOfTrigger(pTrigger);
588 Trigger **pp;
589 for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
590 *pp = (*pp)->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +0000591 }
drh633e6d52008-07-28 19:34:53 +0000592 sqlite3DeleteTrigger(db, pTrigger);
drh956bc922004-07-24 17:38:29 +0000593 db->flags |= SQLITE_InternChanges;
danielk1977c3f9bad2002-05-15 08:30:12 +0000594 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000595}
596
drhc977f7f2002-05-21 11:38:11 +0000597/*
598** pEList is the SET clause of an UPDATE statement. Each entry
599** in pEList is of the format <id>=<expr>. If any of the entries
600** in pEList have an <id> which matches an identifier in pIdList,
601** then return TRUE. If pIdList==NULL, then it is considered a
602** wildcard that matches anything. Likewise if pEList==NULL then
603** it matches anything so always return true. Return false only
604** if there is no match.
605*/
606static int checkColumnOverLap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000607 int e;
608 if( !pIdList || !pEList ) return 1;
609 for(e=0; e<pEList->nExpr; e++){
danielk19774adee202004-05-08 08:23:19 +0000610 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000611 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000612 return 0;
613}
614
danielk1977c3f9bad2002-05-15 08:30:12 +0000615/*
danielk19772f886d12009-02-28 10:47:41 +0000616** Return a list of all triggers on table pTab if there exists at least
617** one trigger that must be fired when an operation of type 'op' is
618** performed on the table, and, if that operation is an UPDATE, if at
619** least one of the columns in pChanges is being modified.
drhdca76842004-12-07 14:06:13 +0000620*/
danielk19772f886d12009-02-28 10:47:41 +0000621Trigger *sqlite3TriggersExist(
622 Parse *pParse, /* Parse context */
drhdca76842004-12-07 14:06:13 +0000623 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000624 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
danielk19772f886d12009-02-28 10:47:41 +0000625 ExprList *pChanges, /* Columns that change in an UPDATE statement */
626 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
drhc977f7f2002-05-21 11:38:11 +0000627){
drhdca76842004-12-07 14:06:13 +0000628 int mask = 0;
danielk19772f886d12009-02-28 10:47:41 +0000629 Trigger *pList = sqlite3TriggerList(pParse, pTab);
630 Trigger *p;
631 assert( pList==0 || IsVirtual(pTab)==0 );
632 for(p=pList; p; p=p->pNext){
633 if( p->op==op && checkColumnOverLap(p->pColumns, pChanges) ){
634 mask |= p->tr_tm;
danielk1977c3f9bad2002-05-15 08:30:12 +0000635 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000636 }
danielk19772f886d12009-02-28 10:47:41 +0000637 if( pMask ){
638 *pMask = mask;
639 }
640 return (mask ? pList : 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000641}
642
drhc977f7f2002-05-21 11:38:11 +0000643/*
drhf26e09c2003-05-31 16:21:12 +0000644** Convert the pStep->target token into a SrcList and return a pointer
645** to that SrcList.
646**
647** This routine adds a specific database name, if needed, to the target when
648** forming the SrcList. This prevents a trigger in one database from
649** referring to a target in another database. An exception is when the
650** trigger is in TEMP in which case it can refer to any other database it
651** wants.
652*/
653static SrcList *targetSrcList(
654 Parse *pParse, /* The parsing context */
655 TriggerStep *pStep /* The trigger containing the target token */
656){
657 Token sDb; /* Dummy database name token */
658 int iDb; /* Index of the database to use */
659 SrcList *pSrc; /* SrcList to be returned */
660
danielk1977da184232006-01-05 11:34:32 +0000661 iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
drhf26e09c2003-05-31 16:21:12 +0000662 if( iDb==0 || iDb>=2 ){
663 assert( iDb<pParse->db->nDb );
drh2646da72005-12-09 20:02:05 +0000664 sDb.z = (u8*)pParse->db->aDb[iDb].zName;
drhea678832008-12-10 19:26:22 +0000665 sDb.n = sqlite3Strlen30((char*)sDb.z);
drh17435752007-08-16 04:30:38 +0000666 pSrc = sqlite3SrcListAppend(pParse->db, 0, &sDb, &pStep->target);
drhf26e09c2003-05-31 16:21:12 +0000667 } else {
drh17435752007-08-16 04:30:38 +0000668 pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
drhf26e09c2003-05-31 16:21:12 +0000669 }
670 return pSrc;
671}
672
673/*
drhc977f7f2002-05-21 11:38:11 +0000674** Generate VDBE code for zero or more statements inside the body of a
675** trigger.
676*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000677static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000678 Parse *pParse, /* The parser context */
679 TriggerStep *pStepList, /* List of statements inside the trigger body */
680 int orconfin /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000681){
682 TriggerStep * pTriggerStep = pStepList;
683 int orconf;
drh344737f2004-09-19 00:50:20 +0000684 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000685 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000686
drh344737f2004-09-19 00:50:20 +0000687 assert( pTriggerStep!=0 );
688 assert( v!=0 );
drh66a51672008-01-03 00:01:23 +0000689 sqlite3VdbeAddOp2(v, OP_ContextPush, 0, 0);
drhd4e70eb2008-01-02 00:34:36 +0000690 VdbeComment((v, "begin trigger %s", pStepList->pTrig->name));
danielk1977633ed082002-05-17 00:05:58 +0000691 while( pTriggerStep ){
drhceea3322009-04-23 13:22:42 +0000692 sqlite3ExprCacheClear(pParse);
danielk1977633ed082002-05-17 00:05:58 +0000693 orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin;
694 pParse->trigStack->orconf = orconf;
695 switch( pTriggerStep->op ){
696 case TK_SELECT: {
danielk19776ab3a2e2009-02-19 14:39:25 +0000697 Select *ss = sqlite3SelectDup(db, pTriggerStep->pSelect, 0);
drh28f45912006-10-18 23:26:38 +0000698 if( ss ){
drh1013c932008-01-06 00:25:21 +0000699 SelectDest dest;
700
701 sqlite3SelectDestInit(&dest, SRT_Discard, 0);
drh7d10d5a2008-08-20 16:35:10 +0000702 sqlite3Select(pParse, ss, &dest);
drh633e6d52008-07-28 19:34:53 +0000703 sqlite3SelectDelete(db, ss);
drh28f45912006-10-18 23:26:38 +0000704 }
drhfd131da2007-08-07 17:13:03 +0000705 break;
danielk1977633ed082002-05-17 00:05:58 +0000706 }
707 case TK_UPDATE: {
drh113088e2003-03-20 01:16:58 +0000708 SrcList *pSrc;
drhf26e09c2003-05-31 16:21:12 +0000709 pSrc = targetSrcList(pParse, pTriggerStep);
drh66a51672008-01-03 00:01:23 +0000710 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000711 sqlite3Update(pParse, pSrc,
danielk19776ab3a2e2009-02-19 14:39:25 +0000712 sqlite3ExprListDup(db, pTriggerStep->pExprList, 0),
713 sqlite3ExprDup(db, pTriggerStep->pWhere, 0), orconf);
drh66a51672008-01-03 00:01:23 +0000714 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000715 break;
716 }
717 case TK_INSERT: {
drh113088e2003-03-20 01:16:58 +0000718 SrcList *pSrc;
drhf26e09c2003-05-31 16:21:12 +0000719 pSrc = targetSrcList(pParse, pTriggerStep);
drh66a51672008-01-03 00:01:23 +0000720 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000721 sqlite3Insert(pParse, pSrc,
danielk19776ab3a2e2009-02-19 14:39:25 +0000722 sqlite3ExprListDup(db, pTriggerStep->pExprList, 0),
723 sqlite3SelectDup(db, pTriggerStep->pSelect, 0),
drh17435752007-08-16 04:30:38 +0000724 sqlite3IdListDup(db, pTriggerStep->pIdList), orconf);
drh66a51672008-01-03 00:01:23 +0000725 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000726 break;
727 }
728 case TK_DELETE: {
drh113088e2003-03-20 01:16:58 +0000729 SrcList *pSrc;
drh66a51672008-01-03 00:01:23 +0000730 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
drhf26e09c2003-05-31 16:21:12 +0000731 pSrc = targetSrcList(pParse, pTriggerStep);
drh17435752007-08-16 04:30:38 +0000732 sqlite3DeleteFrom(pParse, pSrc,
danielk19776ab3a2e2009-02-19 14:39:25 +0000733 sqlite3ExprDup(db, pTriggerStep->pWhere, 0));
drh66a51672008-01-03 00:01:23 +0000734 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000735 break;
736 }
737 default:
738 assert(0);
739 }
danielk1977633ed082002-05-17 00:05:58 +0000740 pTriggerStep = pTriggerStep->pNext;
741 }
drh66a51672008-01-03 00:01:23 +0000742 sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
drhd4e70eb2008-01-02 00:34:36 +0000743 VdbeComment((v, "end trigger %s", pStepList->pTrig->name));
danielk1977c3f9bad2002-05-15 08:30:12 +0000744
danielk1977633ed082002-05-17 00:05:58 +0000745 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000746}
747
danielk1977633ed082002-05-17 00:05:58 +0000748/*
749** This is called to code FOR EACH ROW triggers.
750**
751** When the code that this function generates is executed, the following
752** must be true:
drhc977f7f2002-05-21 11:38:11 +0000753**
754** 1. No cursors may be open in the main database. (But newIdx and oldIdx
755** can be indices of cursors in temporary tables. See below.)
756**
danielk1977633ed082002-05-17 00:05:58 +0000757** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then
758** a temporary vdbe cursor (index newIdx) must be open and pointing at
759** a row containing values to be substituted for new.* expressions in the
760** trigger program(s).
drhc977f7f2002-05-21 11:38:11 +0000761**
danielk1977633ed082002-05-17 00:05:58 +0000762** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then
763** a temporary vdbe cursor (index oldIdx) must be open and pointing at
764** a row containing values to be substituted for old.* expressions in the
765** trigger program(s).
766**
danielk19778f2c54e2008-01-01 19:02:09 +0000767** If they are not NULL, the piOldColMask and piNewColMask output variables
768** are set to values that describe the columns used by the trigger program
769** in the OLD.* and NEW.* tables respectively. If column N of the
770** pseudo-table is read at least once, the corresponding bit of the output
771** mask is set. If a column with an index greater than 32 is read, the
772** output mask is set to the special value 0xffffffff.
773**
danielk1977633ed082002-05-17 00:05:58 +0000774*/
danielk19774adee202004-05-08 08:23:19 +0000775int sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +0000776 Parse *pParse, /* Parse context */
danielk19772f886d12009-02-28 10:47:41 +0000777 Trigger *pTrigger, /* List of triggers on table pTab */
danielk1977633ed082002-05-17 00:05:58 +0000778 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
779 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +0000780 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +0000781 Table *pTab, /* The table to code triggers from */
782 int newIdx, /* The indice of the "new" row to access */
783 int oldIdx, /* The indice of the "old" row to access */
danielk19776f349032002-06-11 02:25:40 +0000784 int orconf, /* ON CONFLICT policy */
danielk19778f2c54e2008-01-01 19:02:09 +0000785 int ignoreJump, /* Instruction to jump to for RAISE(IGNORE) */
786 u32 *piOldColMask, /* OUT: Mask of columns used from the OLD.* table */
787 u32 *piNewColMask /* OUT: Mask of columns used from the NEW.* table */
drhc977f7f2002-05-21 11:38:11 +0000788){
danielk1977eecfb3e2006-01-10 12:31:39 +0000789 Trigger *p;
drh0e359b32008-01-13 19:02:11 +0000790 sqlite3 *db = pParse->db;
drh67040462004-09-24 12:24:36 +0000791 TriggerStack trigStackEntry;
danielk1977c3f9bad2002-05-15 08:30:12 +0000792
danielk19778f2c54e2008-01-01 19:02:09 +0000793 trigStackEntry.oldColMask = 0;
794 trigStackEntry.newColMask = 0;
795
danielk1977c3f9bad2002-05-15 08:30:12 +0000796 assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE);
drhdca76842004-12-07 14:06:13 +0000797 assert(tr_tm == TRIGGER_BEFORE || tr_tm == TRIGGER_AFTER );
danielk1977c3f9bad2002-05-15 08:30:12 +0000798
danielk1977633ed082002-05-17 00:05:58 +0000799 assert(newIdx != -1 || oldIdx != -1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000800
danielk19772f886d12009-02-28 10:47:41 +0000801 for(p=pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +0000802 int fire_this = 0;
803
danielk1977eecfb3e2006-01-10 12:31:39 +0000804 /* Determine whether we should code this trigger */
805 if(
806 p->op==op &&
807 p->tr_tm==tr_tm &&
danielk1977932083c2007-11-16 14:55:46 +0000808 (p->pSchema==p->pTabSchema || p->pSchema==db->aDb[1].pSchema) &&
danielk1977eecfb3e2006-01-10 12:31:39 +0000809 (op!=TK_UPDATE||!p->pColumns||checkColumnOverLap(p->pColumns,pChanges))
810 ){
811 TriggerStack *pS; /* Pointer to trigger-stack entry */
drh74161702006-02-24 02:53:49 +0000812 for(pS=pParse->trigStack; pS && p!=pS->pTrigger; pS=pS->pNext){}
danielk1977eecfb3e2006-01-10 12:31:39 +0000813 if( !pS ){
814 fire_this = 1;
danielk1977f29ce552002-05-19 23:43:12 +0000815 }
drh229caa32006-03-25 15:52:19 +0000816#if 0 /* Give no warning for recursive triggers. Just do not do them */
817 else{
818 sqlite3ErrorMsg(pParse, "recursive triggers not supported (%s)",
819 p->name);
820 return SQLITE_ERROR;
821 }
822#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000823 }
drhad6d9462004-09-19 02:15:24 +0000824
drh67040462004-09-24 12:24:36 +0000825 if( fire_this ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000826 int endTrigger;
danielk1977c3f9bad2002-05-15 08:30:12 +0000827 Expr * whenExpr;
drh85e20962003-04-25 17:52:11 +0000828 AuthContext sContext;
danielk1977b3bce662005-01-29 08:32:43 +0000829 NameContext sNC;
danielk1977c3f9bad2002-05-15 08:30:12 +0000830
drh949f9cd2008-01-12 21:35:57 +0000831#ifndef SQLITE_OMIT_TRACE
832 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Trace, 0, 0, 0,
drh0e359b32008-01-13 19:02:11 +0000833 sqlite3MPrintf(db, "-- TRIGGER %s", p->name),
drh949f9cd2008-01-12 21:35:57 +0000834 P4_DYNAMIC);
835#endif
danielk1977b3bce662005-01-29 08:32:43 +0000836 memset(&sNC, 0, sizeof(sNC));
837 sNC.pParse = pParse;
danielk1977c3f9bad2002-05-15 08:30:12 +0000838
839 /* Push an entry on to the trigger stack */
danielk1977eecfb3e2006-01-10 12:31:39 +0000840 trigStackEntry.pTrigger = p;
drh67040462004-09-24 12:24:36 +0000841 trigStackEntry.newIdx = newIdx;
842 trigStackEntry.oldIdx = oldIdx;
843 trigStackEntry.pTab = pTab;
844 trigStackEntry.pNext = pParse->trigStack;
845 trigStackEntry.ignoreJump = ignoreJump;
846 pParse->trigStack = &trigStackEntry;
danielk1977eecfb3e2006-01-10 12:31:39 +0000847 sqlite3AuthContextPush(pParse, &sContext, p->name);
danielk1977c3f9bad2002-05-15 08:30:12 +0000848
849 /* code the WHEN clause */
danielk19774adee202004-05-08 08:23:19 +0000850 endTrigger = sqlite3VdbeMakeLabel(pParse->pVdbe);
danielk19776ab3a2e2009-02-19 14:39:25 +0000851 whenExpr = sqlite3ExprDup(db, p->pWhen, 0);
drh7d10d5a2008-08-20 16:35:10 +0000852 if( db->mallocFailed || sqlite3ResolveExprNames(&sNC, whenExpr) ){
drh67040462004-09-24 12:24:36 +0000853 pParse->trigStack = trigStackEntry.pNext;
drh633e6d52008-07-28 19:34:53 +0000854 sqlite3ExprDelete(db, whenExpr);
drh9adf9ac2002-05-15 11:44:13 +0000855 return 1;
danielk1977c3f9bad2002-05-15 08:30:12 +0000856 }
drh35573352008-01-08 23:54:25 +0000857 sqlite3ExprIfFalse(pParse, whenExpr, endTrigger, SQLITE_JUMPIFNULL);
drh633e6d52008-07-28 19:34:53 +0000858 sqlite3ExprDelete(db, whenExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000859
danielk1977eecfb3e2006-01-10 12:31:39 +0000860 codeTriggerProgram(pParse, p->step_list, orconf);
danielk1977c3f9bad2002-05-15 08:30:12 +0000861
862 /* Pop the entry off the trigger stack */
drh67040462004-09-24 12:24:36 +0000863 pParse->trigStack = trigStackEntry.pNext;
danielk19774adee202004-05-08 08:23:19 +0000864 sqlite3AuthContextPop(&sContext);
danielk1977c3f9bad2002-05-15 08:30:12 +0000865
danielk19774adee202004-05-08 08:23:19 +0000866 sqlite3VdbeResolveLabel(pParse->pVdbe, endTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000867 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000868 }
danielk19778f2c54e2008-01-01 19:02:09 +0000869 if( piOldColMask ) *piOldColMask |= trigStackEntry.oldColMask;
870 if( piNewColMask ) *piNewColMask |= trigStackEntry.newColMask;
danielk1977c3f9bad2002-05-15 08:30:12 +0000871 return 0;
872}
drhb7f91642004-10-31 02:22:47 +0000873#endif /* !defined(SQLITE_OMIT_TRIGGER) */