blob: 98f8d835d06027b81e4c63a43a7c7255c5bdda81 [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**
danielk19776ab3a2e2009-02-19 14:39:25 +000013** $Id: trigger.c,v 1.134 2009/02/19 14:39:25 danielk1977 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/*
drhf0f258b2003-04-21 18:48:45 +000037** This is called by the parser when it sees a CREATE TRIGGER statement
38** up to the point of the BEGIN before the trigger actions. A Trigger
39** structure is generated based on the information available and stored
40** in pParse->pNewTrigger. After the trigger actions have been parsed, the
danielk19774adee202004-05-08 08:23:19 +000041** sqlite3FinishTrigger() function is called to complete the trigger
drhf0f258b2003-04-21 18:48:45 +000042** construction process.
drh9adf9ac2002-05-15 11:44:13 +000043*/
danielk19774adee202004-05-08 08:23:19 +000044void sqlite3BeginTrigger(
drh9adf9ac2002-05-15 11:44:13 +000045 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
danielk1977ef2cb632004-05-29 02:37:19 +000046 Token *pName1, /* The name of the trigger */
47 Token *pName2, /* The name of the trigger */
drh5cf590c2003-04-24 01:45:04 +000048 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
drh9adf9ac2002-05-15 11:44:13 +000049 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
danielk1977633ed082002-05-17 00:05:58 +000050 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
drhd24cc422003-03-27 12:51:24 +000051 SrcList *pTableName,/* The name of the table/view the trigger applies to */
drh9adf9ac2002-05-15 11:44:13 +000052 Expr *pWhen, /* WHEN clause */
drhfdd48a72006-09-11 23:45:48 +000053 int isTemp, /* True if the TEMPORARY keyword is present */
54 int noErr /* Suppress errors if the trigger already exists */
drh9adf9ac2002-05-15 11:44:13 +000055){
danielk1977d5d56522005-03-16 12:15:20 +000056 Trigger *pTrigger = 0;
danielk1977ef2cb632004-05-29 02:37:19 +000057 Table *pTab;
drhf0f258b2003-04-21 18:48:45 +000058 char *zName = 0; /* Name of the trigger */
drh9bb575f2004-09-06 17:24:11 +000059 sqlite3 *db = pParse->db;
danielk1977ef2cb632004-05-29 02:37:19 +000060 int iDb; /* The database to store the trigger in */
61 Token *pName; /* The unqualified db name */
drh4312db52003-06-03 01:47:11 +000062 DbFixer sFix;
danielk1977da184232006-01-05 11:34:32 +000063 int iTabDb;
drhed6c8672003-01-12 18:02:16 +000064
drh43617e92006-03-06 20:55:46 +000065 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
66 assert( pName2!=0 );
drhaa78bec2008-12-09 03:55:14 +000067 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
68 assert( op>0 && op<0xff );
danielk1977ef2cb632004-05-29 02:37:19 +000069 if( isTemp ){
70 /* If TEMP was specified, then the trigger name may not be qualified. */
drh43617e92006-03-06 20:55:46 +000071 if( pName2->n>0 ){
danielk1977ef2cb632004-05-29 02:37:19 +000072 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
73 goto trigger_cleanup;
74 }
75 iDb = 1;
76 pName = pName1;
77 }else{
78 /* Figure out the db that the the trigger will be created in */
79 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
80 if( iDb<0 ){
81 goto trigger_cleanup;
82 }
83 }
84
85 /* If the trigger name was unqualified, and the table is a temp table,
86 ** then set iDb to 1 to create the trigger in the temporary database.
87 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
88 ** exist, the error is caught by the block below.
drh9adf9ac2002-05-15 11:44:13 +000089 */
drh17435752007-08-16 04:30:38 +000090 if( !pTableName || db->mallocFailed ){
drh6f7adc82006-01-11 21:41:20 +000091 goto trigger_cleanup;
92 }
danielk1977ef2cb632004-05-29 02:37:19 +000093 pTab = sqlite3SrcListLookup(pParse, pTableName);
danielk1977da184232006-01-05 11:34:32 +000094 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977ef2cb632004-05-29 02:37:19 +000095 iDb = 1;
96 }
97
98 /* Ensure the table name matches database name and that the table exists */
drh17435752007-08-16 04:30:38 +000099 if( db->mallocFailed ) goto trigger_cleanup;
drhd24cc422003-03-27 12:51:24 +0000100 assert( pTableName->nSrc==1 );
danielk1977ef2cb632004-05-29 02:37:19 +0000101 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) &&
102 sqlite3FixSrcList(&sFix, pTableName) ){
drh4312db52003-06-03 01:47:11 +0000103 goto trigger_cleanup;
drhf26e09c2003-05-31 16:21:12 +0000104 }
danielk1977ef2cb632004-05-29 02:37:19 +0000105 pTab = sqlite3SrcListLookup(pParse, pTableName);
106 if( !pTab ){
107 /* The table does not exist. */
drhd24cc422003-03-27 12:51:24 +0000108 goto trigger_cleanup;
109 }
drh4cbdda92006-06-14 19:00:20 +0000110 if( IsVirtual(pTab) ){
111 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
112 goto trigger_cleanup;
113 }
drhd24cc422003-03-27 12:51:24 +0000114
danielk1977d8123362004-06-12 09:25:12 +0000115 /* Check that the trigger name is not reserved and that no trigger of the
116 ** specified name exists */
drh17435752007-08-16 04:30:38 +0000117 zName = sqlite3NameFromToken(db, pName);
danielk1977d8123362004-06-12 09:25:12 +0000118 if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
119 goto trigger_cleanup;
120 }
drhaa78bec2008-12-09 03:55:14 +0000121 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
drhea678832008-12-10 19:26:22 +0000122 zName, sqlite3Strlen30(zName)) ){
drhfdd48a72006-09-11 23:45:48 +0000123 if( !noErr ){
124 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
125 }
drhe5f9c642003-01-13 23:27:31 +0000126 goto trigger_cleanup;
danielk1977c3f9bad2002-05-15 08:30:12 +0000127 }
danielk1977ef2cb632004-05-29 02:37:19 +0000128
129 /* Do not create a trigger on a system table */
drhdca76842004-12-07 14:06:13 +0000130 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk19774adee202004-05-08 08:23:19 +0000131 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
drhd24cc422003-03-27 12:51:24 +0000132 pParse->nErr++;
133 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40 +0000134 }
danielk1977ef2cb632004-05-29 02:37:19 +0000135
136 /* INSTEAD of triggers are only for views and views only support INSTEAD
137 ** of triggers.
138 */
139 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000140 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drhda93d232003-03-31 02:12:46 +0000141 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000142 goto trigger_cleanup;
143 }
danielk1977ef2cb632004-05-29 02:37:19 +0000144 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000145 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drhda93d232003-03-31 02:12:46 +0000146 " trigger on table: %S", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000147 goto trigger_cleanup;
148 }
danielk1977da184232006-01-05 11:34:32 +0000149 iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977ef2cb632004-05-29 02:37:19 +0000150
drhd24cc422003-03-27 12:51:24 +0000151#ifndef SQLITE_OMIT_AUTHORIZATION
152 {
153 int code = SQLITE_CREATE_TRIGGER;
danielk1977da184232006-01-05 11:34:32 +0000154 const char *zDb = db->aDb[iTabDb].zName;
drhe22a3342003-04-22 20:30:37 +0000155 const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
danielk1977da184232006-01-05 11:34:32 +0000156 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19 +0000157 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24 +0000158 goto trigger_cleanup;
159 }
danielk1977da184232006-01-05 11:34:32 +0000160 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24 +0000161 goto trigger_cleanup;
162 }
163 }
164#endif
danielk1977d702fcc2002-05-26 23:24:40 +0000165
danielk1977ef2cb632004-05-29 02:37:19 +0000166 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04 +0000167 ** cannot appear on views. So we might as well translate every
168 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
169 ** elsewhere.
170 */
danielk1977d702fcc2002-05-26 23:24:40 +0000171 if (tr_tm == TK_INSTEAD){
172 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12 +0000173 }
174
175 /* Build the Trigger object */
drh17435752007-08-16 04:30:38 +0000176 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19 +0000177 if( pTrigger==0 ) goto trigger_cleanup;
178 pTrigger->name = zName;
drhe5f9c642003-01-13 23:27:31 +0000179 zName = 0;
drh17435752007-08-16 04:30:38 +0000180 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32 +0000181 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48 +0000182 pTrigger->pTabSchema = pTab->pSchema;
drhaa78bec2008-12-09 03:55:14 +0000183 pTrigger->op = (u8)op;
drhdca76842004-12-07 14:06:13 +0000184 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
danielk19776ab3a2e2009-02-19 14:39:25 +0000185 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
drh17435752007-08-16 04:30:38 +0000186 pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
187 sqlite3TokenCopy(db, &pTrigger->nameToken,pName);
drhf0f258b2003-04-21 18:48:45 +0000188 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19 +0000189 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45 +0000190
191trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000192 sqlite3DbFree(db, zName);
193 sqlite3SrcListDelete(db, pTableName);
194 sqlite3IdListDelete(db, pColumns);
195 sqlite3ExprDelete(db, pWhen);
danielk1977d5d56522005-03-16 12:15:20 +0000196 if( !pParse->pNewTrigger ){
drh633e6d52008-07-28 19:34:53 +0000197 sqlite3DeleteTrigger(db, pTrigger);
danielk1977d5d56522005-03-16 12:15:20 +0000198 }else{
199 assert( pParse->pNewTrigger==pTrigger );
200 }
drhf0f258b2003-04-21 18:48:45 +0000201}
202
203/*
204** This routine is called after all of the trigger actions have been parsed
205** in order to complete the process of building the trigger.
206*/
danielk19774adee202004-05-08 08:23:19 +0000207void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45 +0000208 Parse *pParse, /* Parser context */
209 TriggerStep *pStepList, /* The triggered program */
210 Token *pAll /* Token that describes the complete CREATE TRIGGER */
211){
danielk1977bfb9e352005-01-24 13:03:32 +0000212 Trigger *pTrig = 0; /* The trigger whose construction is finishing up */
drh9bb575f2004-09-06 17:24:11 +0000213 sqlite3 *db = pParse->db; /* The database */
drhf26e09c2003-05-31 16:21:12 +0000214 DbFixer sFix;
danielk1977da184232006-01-05 11:34:32 +0000215 int iDb; /* Database containing the trigger */
drhf0f258b2003-04-21 18:48:45 +0000216
danielk1977bfb9e352005-01-24 13:03:32 +0000217 pTrig = pParse->pNewTrigger;
drhf0f258b2003-04-21 18:48:45 +0000218 pParse->pNewTrigger = 0;
danielk19772e588c72005-12-09 14:25:08 +0000219 if( pParse->nErr || !pTrig ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000220 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32 +0000221 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53 +0000222 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32 +0000223 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53 +0000224 pStepList = pStepList->pNext;
225 }
danielk1977da184232006-01-05 11:34:32 +0000226 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &pTrig->nameToken)
danielk1977bfb9e352005-01-24 13:03:32 +0000227 && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
drhf26e09c2003-05-31 16:21:12 +0000228 goto triggerfinish_cleanup;
229 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000230
231 /* if we are not initializing, and this trigger is not on a TEMP table,
drh9adf9ac2002-05-15 11:44:13 +0000232 ** build the sqlite_master entry
233 */
drh1d85d932004-02-14 23:05:52 +0000234 if( !db->init.busy ){
drhc977f7f2002-05-21 11:38:11 +0000235 Vdbe *v;
drh2d401ab2008-01-10 23:50:11 +0000236 char *z;
danielk1977c3f9bad2002-05-15 08:30:12 +0000237
238 /* Make an entry in the sqlite_master table */
danielk19774adee202004-05-08 08:23:19 +0000239 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000240 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000241 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11 +0000242 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
243 sqlite3NestedParse(pParse,
244 "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
245 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pTrig->name,
246 pTrig->table, z);
drh633e6d52008-07-28 19:34:53 +0000247 sqlite3DbFree(db, z);
drh9cbf3422008-01-17 16:22:13 +0000248 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +0000249 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, sqlite3MPrintf(
250 db, "type='trigger' AND name='%q'", pTrig->name), P4_DYNAMIC
danielk19771e536952007-08-16 10:09:01 +0000251 );
danielk1977c3f9bad2002-05-15 08:30:12 +0000252 }
253
drh956bc922004-07-24 17:38:29 +0000254 if( db->init.busy ){
danielk1977aaf22682006-01-06 15:03:48 +0000255 int n;
drhf0f258b2003-04-21 18:48:45 +0000256 Table *pTab;
danielk1977d5d56522005-03-16 12:15:20 +0000257 Trigger *pDel;
danielk1977da184232006-01-05 11:34:32 +0000258 pDel = sqlite3HashInsert(&db->aDb[iDb].pSchema->trigHash,
drhea678832008-12-10 19:26:22 +0000259 pTrig->name, sqlite3Strlen30(pTrig->name), pTrig);
danielk1977d5d56522005-03-16 12:15:20 +0000260 if( pDel ){
drhf3a65f72007-08-22 20:18:21 +0000261 assert( pDel==pTrig );
262 db->mallocFailed = 1;
danielk1977d5d56522005-03-16 12:15:20 +0000263 goto triggerfinish_cleanup;
264 }
drhea678832008-12-10 19:26:22 +0000265 n = sqlite3Strlen30(pTrig->table) + 1;
danielk1977aaf22682006-01-06 15:03:48 +0000266 pTab = sqlite3HashFind(&pTrig->pTabSchema->tblHash, pTrig->table, n);
drhf0f258b2003-04-21 18:48:45 +0000267 assert( pTab!=0 );
danielk1977bfb9e352005-01-24 13:03:32 +0000268 pTrig->pNext = pTab->pTrigger;
269 pTab->pTrigger = pTrig;
270 pTrig = 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000271 }
272
drhf0f258b2003-04-21 18:48:45 +0000273triggerfinish_cleanup:
drh633e6d52008-07-28 19:34:53 +0000274 sqlite3DeleteTrigger(db, pTrig);
danielk1977bfb9e352005-01-24 13:03:32 +0000275 assert( !pParse->pNewTrigger );
drh633e6d52008-07-28 19:34:53 +0000276 sqlite3DeleteTriggerStep(db, pStepList);
drh4b59ab52002-08-24 18:24:51 +0000277}
danielk1977c3f9bad2002-05-15 08:30:12 +0000278
drh4b59ab52002-08-24 18:24:51 +0000279/*
280** Make a copy of all components of the given trigger step. This has
281** the effect of copying all Expr.token.z values into memory obtained
drh17435752007-08-16 04:30:38 +0000282** from sqlite3_malloc(). As initially created, the Expr.token.z values
drh4b59ab52002-08-24 18:24:51 +0000283** all point to the input string that was fed to the parser. But that
danielk19776f8a5032004-05-10 10:34:51 +0000284** string is ephemeral - it will go away as soon as the sqlite3_exec()
drh4b59ab52002-08-24 18:24:51 +0000285** call that started the parser exits. This routine makes a persistent
286** copy of all the Expr.token.z strings so that the TriggerStep structure
danielk19776f8a5032004-05-10 10:34:51 +0000287** will be valid even after the sqlite3_exec() call returns.
drh4b59ab52002-08-24 18:24:51 +0000288*/
drh17435752007-08-16 04:30:38 +0000289static void sqlitePersistTriggerStep(sqlite3 *db, TriggerStep *p){
drh4b59ab52002-08-24 18:24:51 +0000290 if( p->target.z ){
drh17435752007-08-16 04:30:38 +0000291 p->target.z = (u8*)sqlite3DbStrNDup(db, (char*)p->target.z, p->target.n);
drh4b59ab52002-08-24 18:24:51 +0000292 p->target.dyn = 1;
293 }
294 if( p->pSelect ){
danielk19776ab3a2e2009-02-19 14:39:25 +0000295 Select *pNew = sqlite3SelectDup(db, p->pSelect, 1);
drh633e6d52008-07-28 19:34:53 +0000296 sqlite3SelectDelete(db, p->pSelect);
drh4b59ab52002-08-24 18:24:51 +0000297 p->pSelect = pNew;
298 }
299 if( p->pWhere ){
danielk19776ab3a2e2009-02-19 14:39:25 +0000300 Expr *pNew = sqlite3ExprDup(db, p->pWhere, EXPRDUP_REDUCE);
drh633e6d52008-07-28 19:34:53 +0000301 sqlite3ExprDelete(db, p->pWhere);
drh4b59ab52002-08-24 18:24:51 +0000302 p->pWhere = pNew;
303 }
304 if( p->pExprList ){
danielk19776ab3a2e2009-02-19 14:39:25 +0000305 ExprList *pNew = sqlite3ExprListDup(db, p->pExprList, 1);
drh633e6d52008-07-28 19:34:53 +0000306 sqlite3ExprListDelete(db, p->pExprList);
drh4b59ab52002-08-24 18:24:51 +0000307 p->pExprList = pNew;
308 }
309 if( p->pIdList ){
drh17435752007-08-16 04:30:38 +0000310 IdList *pNew = sqlite3IdListDup(db, p->pIdList);
drh633e6d52008-07-28 19:34:53 +0000311 sqlite3IdListDelete(db, p->pIdList);
drh4b59ab52002-08-24 18:24:51 +0000312 p->pIdList = pNew;
danielk1977c3f9bad2002-05-15 08:30:12 +0000313 }
314}
315
drhc977f7f2002-05-21 11:38:11 +0000316/*
317** Turn a SELECT statement (that the pSelect parameter points to) into
318** a trigger step. Return a pointer to a TriggerStep structure.
319**
320** The parser calls this routine when it finds a SELECT statement in
321** body of a TRIGGER.
322*/
drh17435752007-08-16 04:30:38 +0000323TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
324 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000325 if( pTriggerStep==0 ) {
drh633e6d52008-07-28 19:34:53 +0000326 sqlite3SelectDelete(db, pSelect);
danielk19772e588c72005-12-09 14:25:08 +0000327 return 0;
328 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000329
danielk1977633ed082002-05-17 00:05:58 +0000330 pTriggerStep->op = TK_SELECT;
331 pTriggerStep->pSelect = pSelect;
332 pTriggerStep->orconf = OE_Default;
drh17435752007-08-16 04:30:38 +0000333 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977c3f9bad2002-05-15 08:30:12 +0000334
danielk1977633ed082002-05-17 00:05:58 +0000335 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000336}
337
drhc977f7f2002-05-21 11:38:11 +0000338/*
339** Build a trigger step out of an INSERT statement. Return a pointer
340** to the new trigger step.
341**
342** The parser calls this routine when it sees an INSERT inside the
343** body of a trigger.
344*/
danielk19774adee202004-05-08 08:23:19 +0000345TriggerStep *sqlite3TriggerInsertStep(
drh17435752007-08-16 04:30:38 +0000346 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000347 Token *pTableName, /* Name of the table into which we insert */
348 IdList *pColumn, /* List of columns in pTableName to insert into */
349 ExprList *pEList, /* The VALUE clause: a list of values to be inserted */
350 Select *pSelect, /* A SELECT statement that supplies values */
351 int orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
danielk1977633ed082002-05-17 00:05:58 +0000352){
drhf3a65f72007-08-22 20:18:21 +0000353 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000354
danielk1977633ed082002-05-17 00:05:58 +0000355 assert(pEList == 0 || pSelect == 0);
drhf3a65f72007-08-22 20:18:21 +0000356 assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000357
drhf3a65f72007-08-22 20:18:21 +0000358 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk1977d5d56522005-03-16 12:15:20 +0000359 if( pTriggerStep ){
360 pTriggerStep->op = TK_INSERT;
361 pTriggerStep->pSelect = pSelect;
362 pTriggerStep->target = *pTableName;
363 pTriggerStep->pIdList = pColumn;
364 pTriggerStep->pExprList = pEList;
365 pTriggerStep->orconf = orconf;
drh17435752007-08-16 04:30:38 +0000366 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977d5d56522005-03-16 12:15:20 +0000367 }else{
drh633e6d52008-07-28 19:34:53 +0000368 sqlite3IdListDelete(db, pColumn);
369 sqlite3ExprListDelete(db, pEList);
370 sqlite3SelectDelete(db, pSelect);
danielk1977d5d56522005-03-16 12:15:20 +0000371 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000372
danielk1977633ed082002-05-17 00:05:58 +0000373 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000374}
375
drhc977f7f2002-05-21 11:38:11 +0000376/*
377** Construct a trigger step that implements an UPDATE statement and return
378** a pointer to that trigger step. The parser calls this routine when it
379** sees an UPDATE statement inside the body of a CREATE TRIGGER.
380*/
danielk19774adee202004-05-08 08:23:19 +0000381TriggerStep *sqlite3TriggerUpdateStep(
drh17435752007-08-16 04:30:38 +0000382 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000383 Token *pTableName, /* Name of the table to be updated */
384 ExprList *pEList, /* The SET clause: list of column and new values */
385 Expr *pWhere, /* The WHERE clause */
386 int orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
387){
drh17435752007-08-16 04:30:38 +0000388 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
drh0e3a6f32007-03-30 20:40:34 +0000389 if( pTriggerStep==0 ){
drh633e6d52008-07-28 19:34:53 +0000390 sqlite3ExprListDelete(db, pEList);
391 sqlite3ExprDelete(db, pWhere);
drh0e3a6f32007-03-30 20:40:34 +0000392 return 0;
393 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000394
danielk1977633ed082002-05-17 00:05:58 +0000395 pTriggerStep->op = TK_UPDATE;
396 pTriggerStep->target = *pTableName;
397 pTriggerStep->pExprList = pEList;
398 pTriggerStep->pWhere = pWhere;
399 pTriggerStep->orconf = orconf;
drh17435752007-08-16 04:30:38 +0000400 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977c3f9bad2002-05-15 08:30:12 +0000401
danielk1977633ed082002-05-17 00:05:58 +0000402 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000403}
404
drhc977f7f2002-05-21 11:38:11 +0000405/*
406** Construct a trigger step that implements a DELETE statement and return
407** a pointer to that trigger step. The parser calls this routine when it
408** sees a DELETE statement inside the body of a CREATE TRIGGER.
409*/
drh17435752007-08-16 04:30:38 +0000410TriggerStep *sqlite3TriggerDeleteStep(
411 sqlite3 *db, /* Database connection */
412 Token *pTableName, /* The table from which rows are deleted */
413 Expr *pWhere /* The WHERE clause */
414){
415 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
drhb63a53d2007-03-31 01:34:44 +0000416 if( pTriggerStep==0 ){
drh633e6d52008-07-28 19:34:53 +0000417 sqlite3ExprDelete(db, pWhere);
drhb63a53d2007-03-31 01:34:44 +0000418 return 0;
419 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000420
danielk1977633ed082002-05-17 00:05:58 +0000421 pTriggerStep->op = TK_DELETE;
422 pTriggerStep->target = *pTableName;
423 pTriggerStep->pWhere = pWhere;
424 pTriggerStep->orconf = OE_Default;
drh17435752007-08-16 04:30:38 +0000425 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977c3f9bad2002-05-15 08:30:12 +0000426
danielk1977633ed082002-05-17 00:05:58 +0000427 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000428}
429
danielk1977633ed082002-05-17 00:05:58 +0000430/*
431** Recursively delete a Trigger structure
432*/
drh633e6d52008-07-28 19:34:53 +0000433void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
drhf0f258b2003-04-21 18:48:45 +0000434 if( pTrigger==0 ) return;
drh633e6d52008-07-28 19:34:53 +0000435 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
436 sqlite3DbFree(db, pTrigger->name);
437 sqlite3DbFree(db, pTrigger->table);
438 sqlite3ExprDelete(db, pTrigger->pWhen);
439 sqlite3IdListDelete(db, pTrigger->pColumns);
440 if( pTrigger->nameToken.dyn ) sqlite3DbFree(db, (char*)pTrigger->nameToken.z);
441 sqlite3DbFree(db, pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000442}
443
444/*
danielk19778a414492004-06-29 08:59:35 +0000445** This function is called to drop a trigger from the database schema.
446**
447** This may be called directly from the parser and therefore identifies
448** the trigger by name. The sqlite3DropTriggerPtr() routine does the
449** same job as this routine except it takes a pointer to the trigger
450** instead of the trigger name.
451**/
drhfdd48a72006-09-11 23:45:48 +0000452void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000453 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000454 int i;
455 const char *zDb;
456 const char *zName;
457 int nName;
drh9bb575f2004-09-06 17:24:11 +0000458 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000459
drh17435752007-08-16 04:30:38 +0000460 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000461 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000462 goto drop_trigger_cleanup;
463 }
danielk19778e227872004-06-07 07:52:17 +0000464
drhd24cc422003-03-27 12:51:24 +0000465 assert( pName->nSrc==1 );
466 zDb = pName->a[0].zDatabase;
467 zName = pName->a[0].zName;
drhea678832008-12-10 19:26:22 +0000468 nName = sqlite3Strlen30(zName);
danielk197753c0f742005-03-29 03:10:59 +0000469 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000470 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000471 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
drhe4df0e72006-03-29 00:24:06 +0000472 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
drhd24cc422003-03-27 12:51:24 +0000473 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000474 }
drhd24cc422003-03-27 12:51:24 +0000475 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000476 if( !noErr ){
477 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
478 }
drhd24cc422003-03-27 12:51:24 +0000479 goto drop_trigger_cleanup;
480 }
drh74161702006-02-24 02:53:49 +0000481 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000482
483drop_trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000484 sqlite3SrcListDelete(db, pName);
drh79a519c2003-05-17 19:04:03 +0000485}
486
487/*
drh956bc922004-07-24 17:38:29 +0000488** Return a pointer to the Table structure for the table that a trigger
489** is set on.
490*/
drh74161702006-02-24 02:53:49 +0000491static Table *tableOfTrigger(Trigger *pTrigger){
drhea678832008-12-10 19:26:22 +0000492 int n = sqlite3Strlen30(pTrigger->table) + 1;
danielk1977aaf22682006-01-06 15:03:48 +0000493 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
drh956bc922004-07-24 17:38:29 +0000494}
495
496
497/*
drh74161702006-02-24 02:53:49 +0000498** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000499*/
drh74161702006-02-24 02:53:49 +0000500void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000501 Table *pTable;
502 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000503 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000504 int iDb;
drh79a519c2003-05-17 19:04:03 +0000505
danielk1977da184232006-01-05 11:34:32 +0000506 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000507 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000508 pTable = tableOfTrigger(pTrigger);
drh43617e92006-03-06 20:55:46 +0000509 assert( pTable );
danielk1977da184232006-01-05 11:34:32 +0000510 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000511#ifndef SQLITE_OMIT_AUTHORIZATION
512 {
513 int code = SQLITE_DROP_TRIGGER;
drh956bc922004-07-24 17:38:29 +0000514 const char *zDb = db->aDb[iDb].zName;
515 const char *zTab = SCHEMA_TABLE(iDb);
516 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
danielk19774adee202004-05-08 08:23:19 +0000517 if( sqlite3AuthCheck(pParse, code, pTrigger->name, pTable->zName, zDb) ||
518 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000519 return;
drhe5f9c642003-01-13 23:27:31 +0000520 }
drhed6c8672003-01-12 18:02:16 +0000521 }
drhe5f9c642003-01-13 23:27:31 +0000522#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000523
drhf0f258b2003-04-21 18:48:45 +0000524 /* Generate code to destroy the database record of the trigger.
525 */
drh43617e92006-03-06 20:55:46 +0000526 assert( pTable!=0 );
527 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drhf0f258b2003-04-21 18:48:45 +0000528 int base;
drh57196282004-10-06 15:41:16 +0000529 static const VdbeOpList dropTrigger[] = {
drh9b1b01b2003-08-16 12:37:51 +0000530 { OP_Rewind, 0, ADDR(9), 0},
drh1db639c2008-01-17 02:36:28 +0000531 { OP_String8, 0, 1, 0}, /* 1 */
532 { OP_Column, 0, 1, 2},
533 { OP_Ne, 2, ADDR(8), 1},
534 { OP_String8, 0, 1, 0}, /* 4: "trigger" */
535 { OP_Column, 0, 0, 2},
536 { OP_Ne, 2, ADDR(8), 1},
drhf0f258b2003-04-21 18:48:45 +0000537 { OP_Delete, 0, 0, 0},
drh9b1b01b2003-08-16 12:37:51 +0000538 { OP_Next, 0, ADDR(1), 0}, /* 8 */
drhf0f258b2003-04-21 18:48:45 +0000539 };
540
drh956bc922004-07-24 17:38:29 +0000541 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk1977c00da102006-01-07 13:21:04 +0000542 sqlite3OpenMasterTable(pParse, iDb);
danielk19774adee202004-05-08 08:23:19 +0000543 base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger);
drh66a51672008-01-03 00:01:23 +0000544 sqlite3VdbeChangeP4(v, base+1, pTrigger->name, 0);
drh24003452008-01-03 01:28:59 +0000545 sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
drh9cbf3422008-01-17 16:22:13 +0000546 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +0000547 sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
548 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->name, 0);
danielk19776ab3a2e2009-02-19 14:39:25 +0000549 if( pParse->nMem<3 ){
550 pParse->nMem = 3;
551 }
drhf0f258b2003-04-21 18:48:45 +0000552 }
drh956bc922004-07-24 17:38:29 +0000553}
drhf0f258b2003-04-21 18:48:45 +0000554
drh956bc922004-07-24 17:38:29 +0000555/*
556** Remove a trigger from the hash tables of the sqlite* pointer.
557*/
558void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
559 Trigger *pTrigger;
drhea678832008-12-10 19:26:22 +0000560 int nName = sqlite3Strlen30(zName);
drhe4df0e72006-03-29 00:24:06 +0000561 pTrigger = sqlite3HashInsert(&(db->aDb[iDb].pSchema->trigHash),
562 zName, nName, 0);
drh956bc922004-07-24 17:38:29 +0000563 if( pTrigger ){
drh74161702006-02-24 02:53:49 +0000564 Table *pTable = tableOfTrigger(pTrigger);
drh956bc922004-07-24 17:38:29 +0000565 assert( pTable!=0 );
danielk1977633ed082002-05-17 00:05:58 +0000566 if( pTable->pTrigger == pTrigger ){
567 pTable->pTrigger = pTrigger->pNext;
danielk1977f29ce552002-05-19 23:43:12 +0000568 }else{
danielk1977633ed082002-05-17 00:05:58 +0000569 Trigger *cc = pTable->pTrigger;
570 while( cc ){
571 if( cc->pNext == pTrigger ){
drh9adf9ac2002-05-15 11:44:13 +0000572 cc->pNext = cc->pNext->pNext;
573 break;
574 }
575 cc = cc->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +0000576 }
577 assert(cc);
578 }
drh633e6d52008-07-28 19:34:53 +0000579 sqlite3DeleteTrigger(db, pTrigger);
drh956bc922004-07-24 17:38:29 +0000580 db->flags |= SQLITE_InternChanges;
danielk1977c3f9bad2002-05-15 08:30:12 +0000581 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000582}
583
drhc977f7f2002-05-21 11:38:11 +0000584/*
585** pEList is the SET clause of an UPDATE statement. Each entry
586** in pEList is of the format <id>=<expr>. If any of the entries
587** in pEList have an <id> which matches an identifier in pIdList,
588** then return TRUE. If pIdList==NULL, then it is considered a
589** wildcard that matches anything. Likewise if pEList==NULL then
590** it matches anything so always return true. Return false only
591** if there is no match.
592*/
593static int checkColumnOverLap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000594 int e;
595 if( !pIdList || !pEList ) return 1;
596 for(e=0; e<pEList->nExpr; e++){
danielk19774adee202004-05-08 08:23:19 +0000597 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000598 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000599 return 0;
600}
601
danielk1977c3f9bad2002-05-15 08:30:12 +0000602/*
drhdca76842004-12-07 14:06:13 +0000603** Return a bit vector to indicate what kind of triggers exist for operation
604** "op" on table pTab. If pChanges is not NULL then it is a list of columns
605** that are being updated. Triggers only match if the ON clause of the
606** trigger definition overlaps the set of columns being updated.
607**
608** The returned bit vector is some combination of TRIGGER_BEFORE and
609** TRIGGER_AFTER.
610*/
danielk19774adee202004-05-08 08:23:19 +0000611int sqlite3TriggersExist(
drhdca76842004-12-07 14:06:13 +0000612 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000613 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
drhc977f7f2002-05-21 11:38:11 +0000614 ExprList *pChanges /* Columns that change in an UPDATE statement */
615){
drh4cbdda92006-06-14 19:00:20 +0000616 Trigger *pTrigger;
drhdca76842004-12-07 14:06:13 +0000617 int mask = 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000618
drh4cbdda92006-06-14 19:00:20 +0000619 pTrigger = IsVirtual(pTab) ? 0 : pTab->pTrigger;
drhdca76842004-12-07 14:06:13 +0000620 while( pTrigger ){
621 if( pTrigger->op==op && checkColumnOverLap(pTrigger->pColumns, pChanges) ){
drh229caa32006-03-25 15:52:19 +0000622 mask |= pTrigger->tr_tm;
danielk1977c3f9bad2002-05-15 08:30:12 +0000623 }
drhdca76842004-12-07 14:06:13 +0000624 pTrigger = pTrigger->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +0000625 }
drhdca76842004-12-07 14:06:13 +0000626 return mask;
danielk1977c3f9bad2002-05-15 08:30:12 +0000627}
628
drhc977f7f2002-05-21 11:38:11 +0000629/*
drhf26e09c2003-05-31 16:21:12 +0000630** Convert the pStep->target token into a SrcList and return a pointer
631** to that SrcList.
632**
633** This routine adds a specific database name, if needed, to the target when
634** forming the SrcList. This prevents a trigger in one database from
635** referring to a target in another database. An exception is when the
636** trigger is in TEMP in which case it can refer to any other database it
637** wants.
638*/
639static SrcList *targetSrcList(
640 Parse *pParse, /* The parsing context */
641 TriggerStep *pStep /* The trigger containing the target token */
642){
643 Token sDb; /* Dummy database name token */
644 int iDb; /* Index of the database to use */
645 SrcList *pSrc; /* SrcList to be returned */
646
danielk1977da184232006-01-05 11:34:32 +0000647 iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
drhf26e09c2003-05-31 16:21:12 +0000648 if( iDb==0 || iDb>=2 ){
649 assert( iDb<pParse->db->nDb );
drh2646da72005-12-09 20:02:05 +0000650 sDb.z = (u8*)pParse->db->aDb[iDb].zName;
drhea678832008-12-10 19:26:22 +0000651 sDb.n = sqlite3Strlen30((char*)sDb.z);
drh17435752007-08-16 04:30:38 +0000652 pSrc = sqlite3SrcListAppend(pParse->db, 0, &sDb, &pStep->target);
drhf26e09c2003-05-31 16:21:12 +0000653 } else {
drh17435752007-08-16 04:30:38 +0000654 pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
drhf26e09c2003-05-31 16:21:12 +0000655 }
656 return pSrc;
657}
658
659/*
drhc977f7f2002-05-21 11:38:11 +0000660** Generate VDBE code for zero or more statements inside the body of a
661** trigger.
662*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000663static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000664 Parse *pParse, /* The parser context */
665 TriggerStep *pStepList, /* List of statements inside the trigger body */
666 int orconfin /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000667){
668 TriggerStep * pTriggerStep = pStepList;
669 int orconf;
drh344737f2004-09-19 00:50:20 +0000670 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000671 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000672
drh344737f2004-09-19 00:50:20 +0000673 assert( pTriggerStep!=0 );
674 assert( v!=0 );
drh66a51672008-01-03 00:01:23 +0000675 sqlite3VdbeAddOp2(v, OP_ContextPush, 0, 0);
drhd4e70eb2008-01-02 00:34:36 +0000676 VdbeComment((v, "begin trigger %s", pStepList->pTrig->name));
danielk1977633ed082002-05-17 00:05:58 +0000677 while( pTriggerStep ){
danielk197745783d02008-12-26 07:56:39 +0000678 sqlite3ExprClearColumnCache(pParse, -1);
danielk1977633ed082002-05-17 00:05:58 +0000679 orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin;
680 pParse->trigStack->orconf = orconf;
681 switch( pTriggerStep->op ){
682 case TK_SELECT: {
danielk19776ab3a2e2009-02-19 14:39:25 +0000683 Select *ss = sqlite3SelectDup(db, pTriggerStep->pSelect, 0);
drh28f45912006-10-18 23:26:38 +0000684 if( ss ){
drh1013c932008-01-06 00:25:21 +0000685 SelectDest dest;
686
687 sqlite3SelectDestInit(&dest, SRT_Discard, 0);
drh7d10d5a2008-08-20 16:35:10 +0000688 sqlite3Select(pParse, ss, &dest);
drh633e6d52008-07-28 19:34:53 +0000689 sqlite3SelectDelete(db, ss);
drh28f45912006-10-18 23:26:38 +0000690 }
drhfd131da2007-08-07 17:13:03 +0000691 break;
danielk1977633ed082002-05-17 00:05:58 +0000692 }
693 case TK_UPDATE: {
drh113088e2003-03-20 01:16:58 +0000694 SrcList *pSrc;
drhf26e09c2003-05-31 16:21:12 +0000695 pSrc = targetSrcList(pParse, pTriggerStep);
drh66a51672008-01-03 00:01:23 +0000696 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000697 sqlite3Update(pParse, pSrc,
danielk19776ab3a2e2009-02-19 14:39:25 +0000698 sqlite3ExprListDup(db, pTriggerStep->pExprList, 0),
699 sqlite3ExprDup(db, pTriggerStep->pWhere, 0), orconf);
drh66a51672008-01-03 00:01:23 +0000700 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000701 break;
702 }
703 case TK_INSERT: {
drh113088e2003-03-20 01:16:58 +0000704 SrcList *pSrc;
drhf26e09c2003-05-31 16:21:12 +0000705 pSrc = targetSrcList(pParse, pTriggerStep);
drh66a51672008-01-03 00:01:23 +0000706 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000707 sqlite3Insert(pParse, pSrc,
danielk19776ab3a2e2009-02-19 14:39:25 +0000708 sqlite3ExprListDup(db, pTriggerStep->pExprList, 0),
709 sqlite3SelectDup(db, pTriggerStep->pSelect, 0),
drh17435752007-08-16 04:30:38 +0000710 sqlite3IdListDup(db, pTriggerStep->pIdList), orconf);
drh66a51672008-01-03 00:01:23 +0000711 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000712 break;
713 }
714 case TK_DELETE: {
drh113088e2003-03-20 01:16:58 +0000715 SrcList *pSrc;
drh66a51672008-01-03 00:01:23 +0000716 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
drhf26e09c2003-05-31 16:21:12 +0000717 pSrc = targetSrcList(pParse, pTriggerStep);
drh17435752007-08-16 04:30:38 +0000718 sqlite3DeleteFrom(pParse, pSrc,
danielk19776ab3a2e2009-02-19 14:39:25 +0000719 sqlite3ExprDup(db, pTriggerStep->pWhere, 0));
drh66a51672008-01-03 00:01:23 +0000720 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000721 break;
722 }
723 default:
724 assert(0);
725 }
danielk1977633ed082002-05-17 00:05:58 +0000726 pTriggerStep = pTriggerStep->pNext;
727 }
drh66a51672008-01-03 00:01:23 +0000728 sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
drhd4e70eb2008-01-02 00:34:36 +0000729 VdbeComment((v, "end trigger %s", pStepList->pTrig->name));
danielk1977c3f9bad2002-05-15 08:30:12 +0000730
danielk1977633ed082002-05-17 00:05:58 +0000731 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000732}
733
danielk1977633ed082002-05-17 00:05:58 +0000734/*
735** This is called to code FOR EACH ROW triggers.
736**
737** When the code that this function generates is executed, the following
738** must be true:
drhc977f7f2002-05-21 11:38:11 +0000739**
740** 1. No cursors may be open in the main database. (But newIdx and oldIdx
741** can be indices of cursors in temporary tables. See below.)
742**
danielk1977633ed082002-05-17 00:05:58 +0000743** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then
744** a temporary vdbe cursor (index newIdx) must be open and pointing at
745** a row containing values to be substituted for new.* expressions in the
746** trigger program(s).
drhc977f7f2002-05-21 11:38:11 +0000747**
danielk1977633ed082002-05-17 00:05:58 +0000748** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then
749** a temporary vdbe cursor (index oldIdx) must be open and pointing at
750** a row containing values to be substituted for old.* expressions in the
751** trigger program(s).
752**
danielk19778f2c54e2008-01-01 19:02:09 +0000753** If they are not NULL, the piOldColMask and piNewColMask output variables
754** are set to values that describe the columns used by the trigger program
755** in the OLD.* and NEW.* tables respectively. If column N of the
756** pseudo-table is read at least once, the corresponding bit of the output
757** mask is set. If a column with an index greater than 32 is read, the
758** output mask is set to the special value 0xffffffff.
759**
danielk1977633ed082002-05-17 00:05:58 +0000760*/
danielk19774adee202004-05-08 08:23:19 +0000761int sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +0000762 Parse *pParse, /* Parse context */
763 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
764 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +0000765 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +0000766 Table *pTab, /* The table to code triggers from */
767 int newIdx, /* The indice of the "new" row to access */
768 int oldIdx, /* The indice of the "old" row to access */
danielk19776f349032002-06-11 02:25:40 +0000769 int orconf, /* ON CONFLICT policy */
danielk19778f2c54e2008-01-01 19:02:09 +0000770 int ignoreJump, /* Instruction to jump to for RAISE(IGNORE) */
771 u32 *piOldColMask, /* OUT: Mask of columns used from the OLD.* table */
772 u32 *piNewColMask /* OUT: Mask of columns used from the NEW.* table */
drhc977f7f2002-05-21 11:38:11 +0000773){
danielk1977eecfb3e2006-01-10 12:31:39 +0000774 Trigger *p;
drh0e359b32008-01-13 19:02:11 +0000775 sqlite3 *db = pParse->db;
drh67040462004-09-24 12:24:36 +0000776 TriggerStack trigStackEntry;
danielk1977c3f9bad2002-05-15 08:30:12 +0000777
danielk19778f2c54e2008-01-01 19:02:09 +0000778 trigStackEntry.oldColMask = 0;
779 trigStackEntry.newColMask = 0;
780
danielk1977c3f9bad2002-05-15 08:30:12 +0000781 assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE);
drhdca76842004-12-07 14:06:13 +0000782 assert(tr_tm == TRIGGER_BEFORE || tr_tm == TRIGGER_AFTER );
danielk1977c3f9bad2002-05-15 08:30:12 +0000783
danielk1977633ed082002-05-17 00:05:58 +0000784 assert(newIdx != -1 || oldIdx != -1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000785
danielk1977eecfb3e2006-01-10 12:31:39 +0000786 for(p=pTab->pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +0000787 int fire_this = 0;
788
danielk1977eecfb3e2006-01-10 12:31:39 +0000789 /* Determine whether we should code this trigger */
790 if(
791 p->op==op &&
792 p->tr_tm==tr_tm &&
danielk1977932083c2007-11-16 14:55:46 +0000793 (p->pSchema==p->pTabSchema || p->pSchema==db->aDb[1].pSchema) &&
danielk1977eecfb3e2006-01-10 12:31:39 +0000794 (op!=TK_UPDATE||!p->pColumns||checkColumnOverLap(p->pColumns,pChanges))
795 ){
796 TriggerStack *pS; /* Pointer to trigger-stack entry */
drh74161702006-02-24 02:53:49 +0000797 for(pS=pParse->trigStack; pS && p!=pS->pTrigger; pS=pS->pNext){}
danielk1977eecfb3e2006-01-10 12:31:39 +0000798 if( !pS ){
799 fire_this = 1;
danielk1977f29ce552002-05-19 23:43:12 +0000800 }
drh229caa32006-03-25 15:52:19 +0000801#if 0 /* Give no warning for recursive triggers. Just do not do them */
802 else{
803 sqlite3ErrorMsg(pParse, "recursive triggers not supported (%s)",
804 p->name);
805 return SQLITE_ERROR;
806 }
807#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000808 }
drhad6d9462004-09-19 02:15:24 +0000809
drh67040462004-09-24 12:24:36 +0000810 if( fire_this ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000811 int endTrigger;
danielk1977c3f9bad2002-05-15 08:30:12 +0000812 Expr * whenExpr;
drh85e20962003-04-25 17:52:11 +0000813 AuthContext sContext;
danielk1977b3bce662005-01-29 08:32:43 +0000814 NameContext sNC;
danielk1977c3f9bad2002-05-15 08:30:12 +0000815
drh949f9cd2008-01-12 21:35:57 +0000816#ifndef SQLITE_OMIT_TRACE
817 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Trace, 0, 0, 0,
drh0e359b32008-01-13 19:02:11 +0000818 sqlite3MPrintf(db, "-- TRIGGER %s", p->name),
drh949f9cd2008-01-12 21:35:57 +0000819 P4_DYNAMIC);
820#endif
danielk1977b3bce662005-01-29 08:32:43 +0000821 memset(&sNC, 0, sizeof(sNC));
822 sNC.pParse = pParse;
danielk1977c3f9bad2002-05-15 08:30:12 +0000823
824 /* Push an entry on to the trigger stack */
danielk1977eecfb3e2006-01-10 12:31:39 +0000825 trigStackEntry.pTrigger = p;
drh67040462004-09-24 12:24:36 +0000826 trigStackEntry.newIdx = newIdx;
827 trigStackEntry.oldIdx = oldIdx;
828 trigStackEntry.pTab = pTab;
829 trigStackEntry.pNext = pParse->trigStack;
830 trigStackEntry.ignoreJump = ignoreJump;
831 pParse->trigStack = &trigStackEntry;
danielk1977eecfb3e2006-01-10 12:31:39 +0000832 sqlite3AuthContextPush(pParse, &sContext, p->name);
danielk1977c3f9bad2002-05-15 08:30:12 +0000833
834 /* code the WHEN clause */
danielk19774adee202004-05-08 08:23:19 +0000835 endTrigger = sqlite3VdbeMakeLabel(pParse->pVdbe);
danielk19776ab3a2e2009-02-19 14:39:25 +0000836 whenExpr = sqlite3ExprDup(db, p->pWhen, 0);
drh7d10d5a2008-08-20 16:35:10 +0000837 if( db->mallocFailed || sqlite3ResolveExprNames(&sNC, whenExpr) ){
drh67040462004-09-24 12:24:36 +0000838 pParse->trigStack = trigStackEntry.pNext;
drh633e6d52008-07-28 19:34:53 +0000839 sqlite3ExprDelete(db, whenExpr);
drh9adf9ac2002-05-15 11:44:13 +0000840 return 1;
danielk1977c3f9bad2002-05-15 08:30:12 +0000841 }
drh35573352008-01-08 23:54:25 +0000842 sqlite3ExprIfFalse(pParse, whenExpr, endTrigger, SQLITE_JUMPIFNULL);
drh633e6d52008-07-28 19:34:53 +0000843 sqlite3ExprDelete(db, whenExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000844
danielk1977eecfb3e2006-01-10 12:31:39 +0000845 codeTriggerProgram(pParse, p->step_list, orconf);
danielk1977c3f9bad2002-05-15 08:30:12 +0000846
847 /* Pop the entry off the trigger stack */
drh67040462004-09-24 12:24:36 +0000848 pParse->trigStack = trigStackEntry.pNext;
danielk19774adee202004-05-08 08:23:19 +0000849 sqlite3AuthContextPop(&sContext);
danielk1977c3f9bad2002-05-15 08:30:12 +0000850
danielk19774adee202004-05-08 08:23:19 +0000851 sqlite3VdbeResolveLabel(pParse->pVdbe, endTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000852 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000853 }
danielk19778f2c54e2008-01-01 19:02:09 +0000854 if( piOldColMask ) *piOldColMask |= trigStackEntry.oldColMask;
855 if( piNewColMask ) *piNewColMask |= trigStackEntry.newColMask;
danielk1977c3f9bad2002-05-15 08:30:12 +0000856 return 0;
857}
drhb7f91642004-10-31 02:22:47 +0000858#endif /* !defined(SQLITE_OMIT_TRIGGER) */