blob: 4748b4ecbab16b3a299bae503f618b38afcc2cd7 [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*************************************************************************
11*
drh9adf9ac2002-05-15 11:44:13 +000012*/
danielk1977c3f9bad2002-05-15 08:30:12 +000013#include "sqliteInt.h"
drh9adf9ac2002-05-15 11:44:13 +000014
drhb7f91642004-10-31 02:22:47 +000015#ifndef SQLITE_OMIT_TRIGGER
danielk1977c3f9bad2002-05-15 08:30:12 +000016/*
drh4b59ab52002-08-24 18:24:51 +000017** Delete a linked list of TriggerStep structures.
18*/
danielk19774adee202004-05-08 08:23:19 +000019void sqlite3DeleteTriggerStep(TriggerStep *pTriggerStep){
drh4b59ab52002-08-24 18:24:51 +000020 while( pTriggerStep ){
21 TriggerStep * pTmp = pTriggerStep;
22 pTriggerStep = pTriggerStep->pNext;
23
drh17435752007-08-16 04:30:38 +000024 if( pTmp->target.dyn ) sqlite3_free((char*)pTmp->target.z);
danielk19774adee202004-05-08 08:23:19 +000025 sqlite3ExprDelete(pTmp->pWhere);
26 sqlite3ExprListDelete(pTmp->pExprList);
27 sqlite3SelectDelete(pTmp->pSelect);
28 sqlite3IdListDelete(pTmp->pIdList);
drh4b59ab52002-08-24 18:24:51 +000029
drh17435752007-08-16 04:30:38 +000030 sqlite3_free(pTmp);
drh4b59ab52002-08-24 18:24:51 +000031 }
32}
33
34/*
drhf0f258b2003-04-21 18:48:45 +000035** This is called by the parser when it sees a CREATE TRIGGER statement
36** up to the point of the BEGIN before the trigger actions. A Trigger
37** structure is generated based on the information available and stored
38** in pParse->pNewTrigger. After the trigger actions have been parsed, the
danielk19774adee202004-05-08 08:23:19 +000039** sqlite3FinishTrigger() function is called to complete the trigger
drhf0f258b2003-04-21 18:48:45 +000040** construction process.
drh9adf9ac2002-05-15 11:44:13 +000041*/
danielk19774adee202004-05-08 08:23:19 +000042void sqlite3BeginTrigger(
drh9adf9ac2002-05-15 11:44:13 +000043 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
danielk1977ef2cb632004-05-29 02:37:19 +000044 Token *pName1, /* The name of the trigger */
45 Token *pName2, /* The name of the trigger */
drh5cf590c2003-04-24 01:45:04 +000046 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
drh9adf9ac2002-05-15 11:44:13 +000047 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
danielk1977633ed082002-05-17 00:05:58 +000048 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
drhd24cc422003-03-27 12:51:24 +000049 SrcList *pTableName,/* The name of the table/view the trigger applies to */
drh9adf9ac2002-05-15 11:44:13 +000050 Expr *pWhen, /* WHEN clause */
drhfdd48a72006-09-11 23:45:48 +000051 int isTemp, /* True if the TEMPORARY keyword is present */
52 int noErr /* Suppress errors if the trigger already exists */
drh9adf9ac2002-05-15 11:44:13 +000053){
danielk1977d5d56522005-03-16 12:15:20 +000054 Trigger *pTrigger = 0;
danielk1977ef2cb632004-05-29 02:37:19 +000055 Table *pTab;
drhf0f258b2003-04-21 18:48:45 +000056 char *zName = 0; /* Name of the trigger */
drh9bb575f2004-09-06 17:24:11 +000057 sqlite3 *db = pParse->db;
danielk1977ef2cb632004-05-29 02:37:19 +000058 int iDb; /* The database to store the trigger in */
59 Token *pName; /* The unqualified db name */
drh4312db52003-06-03 01:47:11 +000060 DbFixer sFix;
danielk1977da184232006-01-05 11:34:32 +000061 int iTabDb;
drhed6c8672003-01-12 18:02:16 +000062
drh43617e92006-03-06 20:55:46 +000063 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
64 assert( pName2!=0 );
danielk1977ef2cb632004-05-29 02:37:19 +000065 if( isTemp ){
66 /* If TEMP was specified, then the trigger name may not be qualified. */
drh43617e92006-03-06 20:55:46 +000067 if( pName2->n>0 ){
danielk1977ef2cb632004-05-29 02:37:19 +000068 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
69 goto trigger_cleanup;
70 }
71 iDb = 1;
72 pName = pName1;
73 }else{
74 /* Figure out the db that the the trigger will be created in */
75 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
76 if( iDb<0 ){
77 goto trigger_cleanup;
78 }
79 }
80
81 /* If the trigger name was unqualified, and the table is a temp table,
82 ** then set iDb to 1 to create the trigger in the temporary database.
83 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
84 ** exist, the error is caught by the block below.
drh9adf9ac2002-05-15 11:44:13 +000085 */
drh17435752007-08-16 04:30:38 +000086 if( !pTableName || db->mallocFailed ){
drh6f7adc82006-01-11 21:41:20 +000087 goto trigger_cleanup;
88 }
danielk1977ef2cb632004-05-29 02:37:19 +000089 pTab = sqlite3SrcListLookup(pParse, pTableName);
danielk1977da184232006-01-05 11:34:32 +000090 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977ef2cb632004-05-29 02:37:19 +000091 iDb = 1;
92 }
93
94 /* Ensure the table name matches database name and that the table exists */
drh17435752007-08-16 04:30:38 +000095 if( db->mallocFailed ) goto trigger_cleanup;
drhd24cc422003-03-27 12:51:24 +000096 assert( pTableName->nSrc==1 );
danielk1977ef2cb632004-05-29 02:37:19 +000097 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) &&
98 sqlite3FixSrcList(&sFix, pTableName) ){
drh4312db52003-06-03 01:47:11 +000099 goto trigger_cleanup;
drhf26e09c2003-05-31 16:21:12 +0000100 }
danielk1977ef2cb632004-05-29 02:37:19 +0000101 pTab = sqlite3SrcListLookup(pParse, pTableName);
102 if( !pTab ){
103 /* The table does not exist. */
drhd24cc422003-03-27 12:51:24 +0000104 goto trigger_cleanup;
105 }
drh4cbdda92006-06-14 19:00:20 +0000106 if( IsVirtual(pTab) ){
107 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
108 goto trigger_cleanup;
109 }
drhd24cc422003-03-27 12:51:24 +0000110
danielk1977d8123362004-06-12 09:25:12 +0000111 /* Check that the trigger name is not reserved and that no trigger of the
112 ** specified name exists */
drh17435752007-08-16 04:30:38 +0000113 zName = sqlite3NameFromToken(db, pName);
danielk1977d8123362004-06-12 09:25:12 +0000114 if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
115 goto trigger_cleanup;
116 }
drhe4df0e72006-03-29 00:24:06 +0000117 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash), zName,strlen(zName)) ){
drhfdd48a72006-09-11 23:45:48 +0000118 if( !noErr ){
119 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
120 }
drhe5f9c642003-01-13 23:27:31 +0000121 goto trigger_cleanup;
danielk1977c3f9bad2002-05-15 08:30:12 +0000122 }
danielk1977ef2cb632004-05-29 02:37:19 +0000123
124 /* Do not create a trigger on a system table */
drhdca76842004-12-07 14:06:13 +0000125 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk19774adee202004-05-08 08:23:19 +0000126 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
drhd24cc422003-03-27 12:51:24 +0000127 pParse->nErr++;
128 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40 +0000129 }
danielk1977ef2cb632004-05-29 02:37:19 +0000130
131 /* INSTEAD of triggers are only for views and views only support INSTEAD
132 ** of triggers.
133 */
134 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000135 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drhda93d232003-03-31 02:12:46 +0000136 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000137 goto trigger_cleanup;
138 }
danielk1977ef2cb632004-05-29 02:37:19 +0000139 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000140 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drhda93d232003-03-31 02:12:46 +0000141 " trigger on table: %S", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000142 goto trigger_cleanup;
143 }
danielk1977da184232006-01-05 11:34:32 +0000144 iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977ef2cb632004-05-29 02:37:19 +0000145
drhd24cc422003-03-27 12:51:24 +0000146#ifndef SQLITE_OMIT_AUTHORIZATION
147 {
148 int code = SQLITE_CREATE_TRIGGER;
danielk1977da184232006-01-05 11:34:32 +0000149 const char *zDb = db->aDb[iTabDb].zName;
drhe22a3342003-04-22 20:30:37 +0000150 const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
danielk1977da184232006-01-05 11:34:32 +0000151 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19 +0000152 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24 +0000153 goto trigger_cleanup;
154 }
danielk1977da184232006-01-05 11:34:32 +0000155 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24 +0000156 goto trigger_cleanup;
157 }
158 }
159#endif
danielk1977d702fcc2002-05-26 23:24:40 +0000160
danielk1977ef2cb632004-05-29 02:37:19 +0000161 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04 +0000162 ** cannot appear on views. So we might as well translate every
163 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
164 ** elsewhere.
165 */
danielk1977d702fcc2002-05-26 23:24:40 +0000166 if (tr_tm == TK_INSTEAD){
167 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12 +0000168 }
169
170 /* Build the Trigger object */
drh17435752007-08-16 04:30:38 +0000171 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19 +0000172 if( pTrigger==0 ) goto trigger_cleanup;
173 pTrigger->name = zName;
drhe5f9c642003-01-13 23:27:31 +0000174 zName = 0;
drh17435752007-08-16 04:30:38 +0000175 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32 +0000176 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48 +0000177 pTrigger->pTabSchema = pTab->pSchema;
danielk1977ef2cb632004-05-29 02:37:19 +0000178 pTrigger->op = op;
drhdca76842004-12-07 14:06:13 +0000179 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
drh17435752007-08-16 04:30:38 +0000180 pTrigger->pWhen = sqlite3ExprDup(db, pWhen);
181 pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
182 sqlite3TokenCopy(db, &pTrigger->nameToken,pName);
drhf0f258b2003-04-21 18:48:45 +0000183 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19 +0000184 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45 +0000185
186trigger_cleanup:
drh17435752007-08-16 04:30:38 +0000187 sqlite3_free(zName);
danielk19774adee202004-05-08 08:23:19 +0000188 sqlite3SrcListDelete(pTableName);
189 sqlite3IdListDelete(pColumns);
190 sqlite3ExprDelete(pWhen);
danielk1977d5d56522005-03-16 12:15:20 +0000191 if( !pParse->pNewTrigger ){
192 sqlite3DeleteTrigger(pTrigger);
193 }else{
194 assert( pParse->pNewTrigger==pTrigger );
195 }
drhf0f258b2003-04-21 18:48:45 +0000196}
197
198/*
199** This routine is called after all of the trigger actions have been parsed
200** in order to complete the process of building the trigger.
201*/
danielk19774adee202004-05-08 08:23:19 +0000202void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45 +0000203 Parse *pParse, /* Parser context */
204 TriggerStep *pStepList, /* The triggered program */
205 Token *pAll /* Token that describes the complete CREATE TRIGGER */
206){
danielk1977bfb9e352005-01-24 13:03:32 +0000207 Trigger *pTrig = 0; /* The trigger whose construction is finishing up */
drh9bb575f2004-09-06 17:24:11 +0000208 sqlite3 *db = pParse->db; /* The database */
drhf26e09c2003-05-31 16:21:12 +0000209 DbFixer sFix;
danielk1977da184232006-01-05 11:34:32 +0000210 int iDb; /* Database containing the trigger */
drhf0f258b2003-04-21 18:48:45 +0000211
danielk1977bfb9e352005-01-24 13:03:32 +0000212 pTrig = pParse->pNewTrigger;
drhf0f258b2003-04-21 18:48:45 +0000213 pParse->pNewTrigger = 0;
danielk19772e588c72005-12-09 14:25:08 +0000214 if( pParse->nErr || !pTrig ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000215 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32 +0000216 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53 +0000217 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32 +0000218 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53 +0000219 pStepList = pStepList->pNext;
220 }
danielk1977da184232006-01-05 11:34:32 +0000221 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &pTrig->nameToken)
danielk1977bfb9e352005-01-24 13:03:32 +0000222 && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
drhf26e09c2003-05-31 16:21:12 +0000223 goto triggerfinish_cleanup;
224 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000225
226 /* if we are not initializing, and this trigger is not on a TEMP table,
drh9adf9ac2002-05-15 11:44:13 +0000227 ** build the sqlite_master entry
228 */
drh1d85d932004-02-14 23:05:52 +0000229 if( !db->init.busy ){
drh57196282004-10-06 15:41:16 +0000230 static const VdbeOpList insertTrig[] = {
drhf0863fe2005-06-12 21:35:51 +0000231 { OP_NewRowid, 0, 0, 0 },
drh956bc922004-07-24 17:38:29 +0000232 { OP_String8, 0, 0, "trigger" },
233 { OP_String8, 0, 0, 0 }, /* 2: trigger name */
234 { OP_String8, 0, 0, 0 }, /* 3: table name */
drhc977f7f2002-05-21 11:38:11 +0000235 { OP_Integer, 0, 0, 0 },
drh956bc922004-07-24 17:38:29 +0000236 { OP_String8, 0, 0, "CREATE TRIGGER "},
237 { OP_String8, 0, 0, 0 }, /* 6: SQL */
drh855eb1c2004-08-31 13:45:11 +0000238 { OP_Concat, 0, 0, 0 },
drh8a512562005-11-14 22:29:05 +0000239 { OP_MakeRecord, 5, 0, "aaada" },
drhf0863fe2005-06-12 21:35:51 +0000240 { OP_Insert, 0, 0, 0 },
drhc977f7f2002-05-21 11:38:11 +0000241 };
242 int addr;
243 Vdbe *v;
danielk1977c3f9bad2002-05-15 08:30:12 +0000244
245 /* Make an entry in the sqlite_master table */
danielk19774adee202004-05-08 08:23:19 +0000246 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000247 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000248 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk1977c00da102006-01-07 13:21:04 +0000249 sqlite3OpenMasterTable(pParse, iDb);
danielk19774adee202004-05-08 08:23:19 +0000250 addr = sqlite3VdbeAddOpList(v, ArraySize(insertTrig), insertTrig);
danielk1977bfb9e352005-01-24 13:03:32 +0000251 sqlite3VdbeChangeP3(v, addr+2, pTrig->name, 0);
252 sqlite3VdbeChangeP3(v, addr+3, pTrig->table, 0);
drh2646da72005-12-09 20:02:05 +0000253 sqlite3VdbeChangeP3(v, addr+6, (char*)pAll->z, pAll->n);
danielk1977da184232006-01-05 11:34:32 +0000254 sqlite3ChangeCookie(db, v, iDb);
danielk19774adee202004-05-08 08:23:19 +0000255 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
danielk19771e536952007-08-16 10:09:01 +0000256 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, sqlite3MPrintf(
257 db, "type='trigger' AND name='%q'", pTrig->name), P3_DYNAMIC
258 );
danielk1977c3f9bad2002-05-15 08:30:12 +0000259 }
260
drh956bc922004-07-24 17:38:29 +0000261 if( db->init.busy ){
danielk1977aaf22682006-01-06 15:03:48 +0000262 int n;
drhf0f258b2003-04-21 18:48:45 +0000263 Table *pTab;
danielk1977d5d56522005-03-16 12:15:20 +0000264 Trigger *pDel;
danielk1977da184232006-01-05 11:34:32 +0000265 pDel = sqlite3HashInsert(&db->aDb[iDb].pSchema->trigHash,
drhe4df0e72006-03-29 00:24:06 +0000266 pTrig->name, strlen(pTrig->name), pTrig);
danielk1977d5d56522005-03-16 12:15:20 +0000267 if( pDel ){
drhf3a65f72007-08-22 20:18:21 +0000268 assert( pDel==pTrig );
269 db->mallocFailed = 1;
danielk1977d5d56522005-03-16 12:15:20 +0000270 goto triggerfinish_cleanup;
271 }
danielk1977aaf22682006-01-06 15:03:48 +0000272 n = strlen(pTrig->table) + 1;
273 pTab = sqlite3HashFind(&pTrig->pTabSchema->tblHash, pTrig->table, n);
drhf0f258b2003-04-21 18:48:45 +0000274 assert( pTab!=0 );
danielk1977bfb9e352005-01-24 13:03:32 +0000275 pTrig->pNext = pTab->pTrigger;
276 pTab->pTrigger = pTrig;
277 pTrig = 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000278 }
279
drhf0f258b2003-04-21 18:48:45 +0000280triggerfinish_cleanup:
danielk1977bfb9e352005-01-24 13:03:32 +0000281 sqlite3DeleteTrigger(pTrig);
282 assert( !pParse->pNewTrigger );
danielk19774adee202004-05-08 08:23:19 +0000283 sqlite3DeleteTriggerStep(pStepList);
drh4b59ab52002-08-24 18:24:51 +0000284}
danielk1977c3f9bad2002-05-15 08:30:12 +0000285
drh4b59ab52002-08-24 18:24:51 +0000286/*
287** Make a copy of all components of the given trigger step. This has
288** the effect of copying all Expr.token.z values into memory obtained
drh17435752007-08-16 04:30:38 +0000289** from sqlite3_malloc(). As initially created, the Expr.token.z values
drh4b59ab52002-08-24 18:24:51 +0000290** all point to the input string that was fed to the parser. But that
danielk19776f8a5032004-05-10 10:34:51 +0000291** string is ephemeral - it will go away as soon as the sqlite3_exec()
drh4b59ab52002-08-24 18:24:51 +0000292** call that started the parser exits. This routine makes a persistent
293** copy of all the Expr.token.z strings so that the TriggerStep structure
danielk19776f8a5032004-05-10 10:34:51 +0000294** will be valid even after the sqlite3_exec() call returns.
drh4b59ab52002-08-24 18:24:51 +0000295*/
drh17435752007-08-16 04:30:38 +0000296static void sqlitePersistTriggerStep(sqlite3 *db, TriggerStep *p){
drh4b59ab52002-08-24 18:24:51 +0000297 if( p->target.z ){
drh17435752007-08-16 04:30:38 +0000298 p->target.z = (u8*)sqlite3DbStrNDup(db, (char*)p->target.z, p->target.n);
drh4b59ab52002-08-24 18:24:51 +0000299 p->target.dyn = 1;
300 }
301 if( p->pSelect ){
drh17435752007-08-16 04:30:38 +0000302 Select *pNew = sqlite3SelectDup(db, p->pSelect);
danielk19774adee202004-05-08 08:23:19 +0000303 sqlite3SelectDelete(p->pSelect);
drh4b59ab52002-08-24 18:24:51 +0000304 p->pSelect = pNew;
305 }
306 if( p->pWhere ){
drh17435752007-08-16 04:30:38 +0000307 Expr *pNew = sqlite3ExprDup(db, p->pWhere);
danielk19774adee202004-05-08 08:23:19 +0000308 sqlite3ExprDelete(p->pWhere);
drh4b59ab52002-08-24 18:24:51 +0000309 p->pWhere = pNew;
310 }
311 if( p->pExprList ){
drh17435752007-08-16 04:30:38 +0000312 ExprList *pNew = sqlite3ExprListDup(db, p->pExprList);
danielk19774adee202004-05-08 08:23:19 +0000313 sqlite3ExprListDelete(p->pExprList);
drh4b59ab52002-08-24 18:24:51 +0000314 p->pExprList = pNew;
315 }
316 if( p->pIdList ){
drh17435752007-08-16 04:30:38 +0000317 IdList *pNew = sqlite3IdListDup(db, p->pIdList);
danielk19774adee202004-05-08 08:23:19 +0000318 sqlite3IdListDelete(p->pIdList);
drh4b59ab52002-08-24 18:24:51 +0000319 p->pIdList = pNew;
danielk1977c3f9bad2002-05-15 08:30:12 +0000320 }
321}
322
drhc977f7f2002-05-21 11:38:11 +0000323/*
324** Turn a SELECT statement (that the pSelect parameter points to) into
325** a trigger step. Return a pointer to a TriggerStep structure.
326**
327** The parser calls this routine when it finds a SELECT statement in
328** body of a TRIGGER.
329*/
drh17435752007-08-16 04:30:38 +0000330TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
331 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000332 if( pTriggerStep==0 ) {
333 sqlite3SelectDelete(pSelect);
334 return 0;
335 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000336
danielk1977633ed082002-05-17 00:05:58 +0000337 pTriggerStep->op = TK_SELECT;
338 pTriggerStep->pSelect = pSelect;
339 pTriggerStep->orconf = OE_Default;
drh17435752007-08-16 04:30:38 +0000340 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977c3f9bad2002-05-15 08:30:12 +0000341
danielk1977633ed082002-05-17 00:05:58 +0000342 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000343}
344
drhc977f7f2002-05-21 11:38:11 +0000345/*
346** Build a trigger step out of an INSERT statement. Return a pointer
347** to the new trigger step.
348**
349** The parser calls this routine when it sees an INSERT inside the
350** body of a trigger.
351*/
danielk19774adee202004-05-08 08:23:19 +0000352TriggerStep *sqlite3TriggerInsertStep(
drh17435752007-08-16 04:30:38 +0000353 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000354 Token *pTableName, /* Name of the table into which we insert */
355 IdList *pColumn, /* List of columns in pTableName to insert into */
356 ExprList *pEList, /* The VALUE clause: a list of values to be inserted */
357 Select *pSelect, /* A SELECT statement that supplies values */
358 int orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
danielk1977633ed082002-05-17 00:05:58 +0000359){
drhf3a65f72007-08-22 20:18:21 +0000360 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000361
danielk1977633ed082002-05-17 00:05:58 +0000362 assert(pEList == 0 || pSelect == 0);
drhf3a65f72007-08-22 20:18:21 +0000363 assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000364
drhf3a65f72007-08-22 20:18:21 +0000365 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk1977d5d56522005-03-16 12:15:20 +0000366 if( pTriggerStep ){
367 pTriggerStep->op = TK_INSERT;
368 pTriggerStep->pSelect = pSelect;
369 pTriggerStep->target = *pTableName;
370 pTriggerStep->pIdList = pColumn;
371 pTriggerStep->pExprList = pEList;
372 pTriggerStep->orconf = orconf;
drh17435752007-08-16 04:30:38 +0000373 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977d5d56522005-03-16 12:15:20 +0000374 }else{
375 sqlite3IdListDelete(pColumn);
376 sqlite3ExprListDelete(pEList);
drh17435752007-08-16 04:30:38 +0000377 sqlite3SelectDelete(pSelect);
danielk1977d5d56522005-03-16 12:15:20 +0000378 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000379
danielk1977633ed082002-05-17 00:05:58 +0000380 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000381}
382
drhc977f7f2002-05-21 11:38:11 +0000383/*
384** Construct a trigger step that implements an UPDATE statement and return
385** a pointer to that trigger step. The parser calls this routine when it
386** sees an UPDATE statement inside the body of a CREATE TRIGGER.
387*/
danielk19774adee202004-05-08 08:23:19 +0000388TriggerStep *sqlite3TriggerUpdateStep(
drh17435752007-08-16 04:30:38 +0000389 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000390 Token *pTableName, /* Name of the table to be updated */
391 ExprList *pEList, /* The SET clause: list of column and new values */
392 Expr *pWhere, /* The WHERE clause */
393 int orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
394){
drh17435752007-08-16 04:30:38 +0000395 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
drh0e3a6f32007-03-30 20:40:34 +0000396 if( pTriggerStep==0 ){
397 sqlite3ExprListDelete(pEList);
398 sqlite3ExprDelete(pWhere);
399 return 0;
400 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000401
danielk1977633ed082002-05-17 00:05:58 +0000402 pTriggerStep->op = TK_UPDATE;
403 pTriggerStep->target = *pTableName;
404 pTriggerStep->pExprList = pEList;
405 pTriggerStep->pWhere = pWhere;
406 pTriggerStep->orconf = orconf;
drh17435752007-08-16 04:30:38 +0000407 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977c3f9bad2002-05-15 08:30:12 +0000408
danielk1977633ed082002-05-17 00:05:58 +0000409 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000410}
411
drhc977f7f2002-05-21 11:38:11 +0000412/*
413** Construct a trigger step that implements a DELETE statement and return
414** a pointer to that trigger step. The parser calls this routine when it
415** sees a DELETE statement inside the body of a CREATE TRIGGER.
416*/
drh17435752007-08-16 04:30:38 +0000417TriggerStep *sqlite3TriggerDeleteStep(
418 sqlite3 *db, /* Database connection */
419 Token *pTableName, /* The table from which rows are deleted */
420 Expr *pWhere /* The WHERE clause */
421){
422 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
drhb63a53d2007-03-31 01:34:44 +0000423 if( pTriggerStep==0 ){
424 sqlite3ExprDelete(pWhere);
425 return 0;
426 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000427
danielk1977633ed082002-05-17 00:05:58 +0000428 pTriggerStep->op = TK_DELETE;
429 pTriggerStep->target = *pTableName;
430 pTriggerStep->pWhere = pWhere;
431 pTriggerStep->orconf = OE_Default;
drh17435752007-08-16 04:30:38 +0000432 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977c3f9bad2002-05-15 08:30:12 +0000433
danielk1977633ed082002-05-17 00:05:58 +0000434 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000435}
436
danielk1977633ed082002-05-17 00:05:58 +0000437/*
438** Recursively delete a Trigger structure
439*/
danielk19774adee202004-05-08 08:23:19 +0000440void sqlite3DeleteTrigger(Trigger *pTrigger){
drhf0f258b2003-04-21 18:48:45 +0000441 if( pTrigger==0 ) return;
danielk19774adee202004-05-08 08:23:19 +0000442 sqlite3DeleteTriggerStep(pTrigger->step_list);
drh17435752007-08-16 04:30:38 +0000443 sqlite3_free(pTrigger->name);
444 sqlite3_free(pTrigger->table);
danielk19774adee202004-05-08 08:23:19 +0000445 sqlite3ExprDelete(pTrigger->pWhen);
446 sqlite3IdListDelete(pTrigger->pColumns);
drh17435752007-08-16 04:30:38 +0000447 if( pTrigger->nameToken.dyn ) sqlite3_free((char*)pTrigger->nameToken.z);
448 sqlite3_free(pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000449}
450
451/*
danielk19778a414492004-06-29 08:59:35 +0000452** This function is called to drop a trigger from the database schema.
453**
454** This may be called directly from the parser and therefore identifies
455** the trigger by name. The sqlite3DropTriggerPtr() routine does the
456** same job as this routine except it takes a pointer to the trigger
457** instead of the trigger name.
458**/
drhfdd48a72006-09-11 23:45:48 +0000459void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000460 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000461 int i;
462 const char *zDb;
463 const char *zName;
464 int nName;
drh9bb575f2004-09-06 17:24:11 +0000465 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000466
drh17435752007-08-16 04:30:38 +0000467 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000468 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000469 goto drop_trigger_cleanup;
470 }
danielk19778e227872004-06-07 07:52:17 +0000471
drhd24cc422003-03-27 12:51:24 +0000472 assert( pName->nSrc==1 );
473 zDb = pName->a[0].zDatabase;
474 zName = pName->a[0].zName;
475 nName = strlen(zName);
danielk197753c0f742005-03-29 03:10:59 +0000476 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000477 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000478 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
drhe4df0e72006-03-29 00:24:06 +0000479 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
drhd24cc422003-03-27 12:51:24 +0000480 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000481 }
drhd24cc422003-03-27 12:51:24 +0000482 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000483 if( !noErr ){
484 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
485 }
drhd24cc422003-03-27 12:51:24 +0000486 goto drop_trigger_cleanup;
487 }
drh74161702006-02-24 02:53:49 +0000488 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000489
490drop_trigger_cleanup:
danielk19774adee202004-05-08 08:23:19 +0000491 sqlite3SrcListDelete(pName);
drh79a519c2003-05-17 19:04:03 +0000492}
493
494/*
drh956bc922004-07-24 17:38:29 +0000495** Return a pointer to the Table structure for the table that a trigger
496** is set on.
497*/
drh74161702006-02-24 02:53:49 +0000498static Table *tableOfTrigger(Trigger *pTrigger){
danielk1977aaf22682006-01-06 15:03:48 +0000499 int n = strlen(pTrigger->table) + 1;
500 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
drh956bc922004-07-24 17:38:29 +0000501}
502
503
504/*
drh74161702006-02-24 02:53:49 +0000505** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000506*/
drh74161702006-02-24 02:53:49 +0000507void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000508 Table *pTable;
509 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000510 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000511 int iDb;
drh79a519c2003-05-17 19:04:03 +0000512
danielk1977da184232006-01-05 11:34:32 +0000513 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000514 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000515 pTable = tableOfTrigger(pTrigger);
drh43617e92006-03-06 20:55:46 +0000516 assert( pTable );
danielk1977da184232006-01-05 11:34:32 +0000517 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000518#ifndef SQLITE_OMIT_AUTHORIZATION
519 {
520 int code = SQLITE_DROP_TRIGGER;
drh956bc922004-07-24 17:38:29 +0000521 const char *zDb = db->aDb[iDb].zName;
522 const char *zTab = SCHEMA_TABLE(iDb);
523 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
danielk19774adee202004-05-08 08:23:19 +0000524 if( sqlite3AuthCheck(pParse, code, pTrigger->name, pTable->zName, zDb) ||
525 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000526 return;
drhe5f9c642003-01-13 23:27:31 +0000527 }
drhed6c8672003-01-12 18:02:16 +0000528 }
drhe5f9c642003-01-13 23:27:31 +0000529#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000530
drhf0f258b2003-04-21 18:48:45 +0000531 /* Generate code to destroy the database record of the trigger.
532 */
drh43617e92006-03-06 20:55:46 +0000533 assert( pTable!=0 );
534 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drhf0f258b2003-04-21 18:48:45 +0000535 int base;
drh57196282004-10-06 15:41:16 +0000536 static const VdbeOpList dropTrigger[] = {
drh9b1b01b2003-08-16 12:37:51 +0000537 { OP_Rewind, 0, ADDR(9), 0},
drh57196282004-10-06 15:41:16 +0000538 { OP_String8, 0, 0, 0}, /* 1 */
drhf0f258b2003-04-21 18:48:45 +0000539 { OP_Column, 0, 1, 0},
drh9b1b01b2003-08-16 12:37:51 +0000540 { OP_Ne, 0, ADDR(8), 0},
drh57196282004-10-06 15:41:16 +0000541 { OP_String8, 0, 0, "trigger"},
drh9b1b01b2003-08-16 12:37:51 +0000542 { OP_Column, 0, 0, 0},
543 { OP_Ne, 0, ADDR(8), 0},
drhf0f258b2003-04-21 18:48:45 +0000544 { OP_Delete, 0, 0, 0},
drh9b1b01b2003-08-16 12:37:51 +0000545 { OP_Next, 0, ADDR(1), 0}, /* 8 */
drhf0f258b2003-04-21 18:48:45 +0000546 };
547
drh956bc922004-07-24 17:38:29 +0000548 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk1977c00da102006-01-07 13:21:04 +0000549 sqlite3OpenMasterTable(pParse, iDb);
danielk19774adee202004-05-08 08:23:19 +0000550 base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger);
551 sqlite3VdbeChangeP3(v, base+1, pTrigger->name, 0);
drh956bc922004-07-24 17:38:29 +0000552 sqlite3ChangeCookie(db, v, iDb);
danielk19774adee202004-05-08 08:23:19 +0000553 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
drh956bc922004-07-24 17:38:29 +0000554 sqlite3VdbeOp3(v, OP_DropTrigger, iDb, 0, pTrigger->name, 0);
drhf0f258b2003-04-21 18:48:45 +0000555 }
drh956bc922004-07-24 17:38:29 +0000556}
drhf0f258b2003-04-21 18:48:45 +0000557
drh956bc922004-07-24 17:38:29 +0000558/*
559** Remove a trigger from the hash tables of the sqlite* pointer.
560*/
561void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
562 Trigger *pTrigger;
563 int nName = strlen(zName);
drhe4df0e72006-03-29 00:24:06 +0000564 pTrigger = sqlite3HashInsert(&(db->aDb[iDb].pSchema->trigHash),
565 zName, nName, 0);
drh956bc922004-07-24 17:38:29 +0000566 if( pTrigger ){
drh74161702006-02-24 02:53:49 +0000567 Table *pTable = tableOfTrigger(pTrigger);
drh956bc922004-07-24 17:38:29 +0000568 assert( pTable!=0 );
danielk1977633ed082002-05-17 00:05:58 +0000569 if( pTable->pTrigger == pTrigger ){
570 pTable->pTrigger = pTrigger->pNext;
danielk1977f29ce552002-05-19 23:43:12 +0000571 }else{
danielk1977633ed082002-05-17 00:05:58 +0000572 Trigger *cc = pTable->pTrigger;
573 while( cc ){
574 if( cc->pNext == pTrigger ){
drh9adf9ac2002-05-15 11:44:13 +0000575 cc->pNext = cc->pNext->pNext;
576 break;
577 }
578 cc = cc->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +0000579 }
580 assert(cc);
581 }
danielk19774adee202004-05-08 08:23:19 +0000582 sqlite3DeleteTrigger(pTrigger);
drh956bc922004-07-24 17:38:29 +0000583 db->flags |= SQLITE_InternChanges;
danielk1977c3f9bad2002-05-15 08:30:12 +0000584 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000585}
586
drhc977f7f2002-05-21 11:38:11 +0000587/*
588** pEList is the SET clause of an UPDATE statement. Each entry
589** in pEList is of the format <id>=<expr>. If any of the entries
590** in pEList have an <id> which matches an identifier in pIdList,
591** then return TRUE. If pIdList==NULL, then it is considered a
592** wildcard that matches anything. Likewise if pEList==NULL then
593** it matches anything so always return true. Return false only
594** if there is no match.
595*/
596static int checkColumnOverLap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000597 int e;
598 if( !pIdList || !pEList ) return 1;
599 for(e=0; e<pEList->nExpr; e++){
danielk19774adee202004-05-08 08:23:19 +0000600 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000601 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000602 return 0;
603}
604
danielk1977c3f9bad2002-05-15 08:30:12 +0000605/*
drhdca76842004-12-07 14:06:13 +0000606** Return a bit vector to indicate what kind of triggers exist for operation
607** "op" on table pTab. If pChanges is not NULL then it is a list of columns
608** that are being updated. Triggers only match if the ON clause of the
609** trigger definition overlaps the set of columns being updated.
610**
611** The returned bit vector is some combination of TRIGGER_BEFORE and
612** TRIGGER_AFTER.
613*/
danielk19774adee202004-05-08 08:23:19 +0000614int sqlite3TriggersExist(
drhc977f7f2002-05-21 11:38:11 +0000615 Parse *pParse, /* Used to check for recursive triggers */
drhdca76842004-12-07 14:06:13 +0000616 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000617 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
drhc977f7f2002-05-21 11:38:11 +0000618 ExprList *pChanges /* Columns that change in an UPDATE statement */
619){
drh4cbdda92006-06-14 19:00:20 +0000620 Trigger *pTrigger;
drhdca76842004-12-07 14:06:13 +0000621 int mask = 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000622
drh4cbdda92006-06-14 19:00:20 +0000623 pTrigger = IsVirtual(pTab) ? 0 : pTab->pTrigger;
drhdca76842004-12-07 14:06:13 +0000624 while( pTrigger ){
625 if( pTrigger->op==op && checkColumnOverLap(pTrigger->pColumns, pChanges) ){
drh229caa32006-03-25 15:52:19 +0000626 mask |= pTrigger->tr_tm;
danielk1977c3f9bad2002-05-15 08:30:12 +0000627 }
drhdca76842004-12-07 14:06:13 +0000628 pTrigger = pTrigger->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +0000629 }
drhdca76842004-12-07 14:06:13 +0000630 return mask;
danielk1977c3f9bad2002-05-15 08:30:12 +0000631}
632
drhc977f7f2002-05-21 11:38:11 +0000633/*
drhf26e09c2003-05-31 16:21:12 +0000634** Convert the pStep->target token into a SrcList and return a pointer
635** to that SrcList.
636**
637** This routine adds a specific database name, if needed, to the target when
638** forming the SrcList. This prevents a trigger in one database from
639** referring to a target in another database. An exception is when the
640** trigger is in TEMP in which case it can refer to any other database it
641** wants.
642*/
643static SrcList *targetSrcList(
644 Parse *pParse, /* The parsing context */
645 TriggerStep *pStep /* The trigger containing the target token */
646){
647 Token sDb; /* Dummy database name token */
648 int iDb; /* Index of the database to use */
649 SrcList *pSrc; /* SrcList to be returned */
650
danielk1977da184232006-01-05 11:34:32 +0000651 iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
drhf26e09c2003-05-31 16:21:12 +0000652 if( iDb==0 || iDb>=2 ){
653 assert( iDb<pParse->db->nDb );
drh2646da72005-12-09 20:02:05 +0000654 sDb.z = (u8*)pParse->db->aDb[iDb].zName;
655 sDb.n = strlen((char*)sDb.z);
drh17435752007-08-16 04:30:38 +0000656 pSrc = sqlite3SrcListAppend(pParse->db, 0, &sDb, &pStep->target);
drhf26e09c2003-05-31 16:21:12 +0000657 } else {
drh17435752007-08-16 04:30:38 +0000658 pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
drhf26e09c2003-05-31 16:21:12 +0000659 }
660 return pSrc;
661}
662
663/*
drhc977f7f2002-05-21 11:38:11 +0000664** Generate VDBE code for zero or more statements inside the body of a
665** trigger.
666*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000667static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000668 Parse *pParse, /* The parser context */
669 TriggerStep *pStepList, /* List of statements inside the trigger body */
670 int orconfin /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000671){
672 TriggerStep * pTriggerStep = pStepList;
673 int orconf;
drh344737f2004-09-19 00:50:20 +0000674 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000675 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000676
drh344737f2004-09-19 00:50:20 +0000677 assert( pTriggerStep!=0 );
678 assert( v!=0 );
679 sqlite3VdbeAddOp(v, OP_ContextPush, 0, 0);
drh67040462004-09-24 12:24:36 +0000680 VdbeComment((v, "# begin trigger %s", pStepList->pTrig->name));
danielk1977633ed082002-05-17 00:05:58 +0000681 while( pTriggerStep ){
danielk1977633ed082002-05-17 00:05:58 +0000682 orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin;
683 pParse->trigStack->orconf = orconf;
684 switch( pTriggerStep->op ){
685 case TK_SELECT: {
drh17435752007-08-16 04:30:38 +0000686 Select *ss = sqlite3SelectDup(db, pTriggerStep->pSelect);
drh28f45912006-10-18 23:26:38 +0000687 if( ss ){
688 sqlite3SelectResolve(pParse, ss, 0);
689 sqlite3Select(pParse, ss, SRT_Discard, 0, 0, 0, 0, 0);
690 sqlite3SelectDelete(ss);
691 }
drhfd131da2007-08-07 17:13:03 +0000692 break;
danielk1977633ed082002-05-17 00:05:58 +0000693 }
694 case TK_UPDATE: {
drh113088e2003-03-20 01:16:58 +0000695 SrcList *pSrc;
drhf26e09c2003-05-31 16:21:12 +0000696 pSrc = targetSrcList(pParse, pTriggerStep);
drh344737f2004-09-19 00:50:20 +0000697 sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000698 sqlite3Update(pParse, pSrc,
drh17435752007-08-16 04:30:38 +0000699 sqlite3ExprListDup(db, pTriggerStep->pExprList),
700 sqlite3ExprDup(db, pTriggerStep->pWhere), orconf);
drh344737f2004-09-19 00:50:20 +0000701 sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000702 break;
703 }
704 case TK_INSERT: {
drh113088e2003-03-20 01:16:58 +0000705 SrcList *pSrc;
drhf26e09c2003-05-31 16:21:12 +0000706 pSrc = targetSrcList(pParse, pTriggerStep);
drh344737f2004-09-19 00:50:20 +0000707 sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000708 sqlite3Insert(pParse, pSrc,
drh17435752007-08-16 04:30:38 +0000709 sqlite3ExprListDup(db, pTriggerStep->pExprList),
710 sqlite3SelectDup(db, pTriggerStep->pSelect),
711 sqlite3IdListDup(db, pTriggerStep->pIdList), orconf);
drh344737f2004-09-19 00:50:20 +0000712 sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000713 break;
714 }
715 case TK_DELETE: {
drh113088e2003-03-20 01:16:58 +0000716 SrcList *pSrc;
drh344737f2004-09-19 00:50:20 +0000717 sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0);
drhf26e09c2003-05-31 16:21:12 +0000718 pSrc = targetSrcList(pParse, pTriggerStep);
drh17435752007-08-16 04:30:38 +0000719 sqlite3DeleteFrom(pParse, pSrc,
720 sqlite3ExprDup(db, pTriggerStep->pWhere));
drh344737f2004-09-19 00:50:20 +0000721 sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000722 break;
723 }
724 default:
725 assert(0);
726 }
danielk1977633ed082002-05-17 00:05:58 +0000727 pTriggerStep = pTriggerStep->pNext;
728 }
drh344737f2004-09-19 00:50:20 +0000729 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
drh67040462004-09-24 12:24:36 +0000730 VdbeComment((v, "# end trigger %s", pStepList->pTrig->name));
danielk1977c3f9bad2002-05-15 08:30:12 +0000731
danielk1977633ed082002-05-17 00:05:58 +0000732 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000733}
734
danielk1977633ed082002-05-17 00:05:58 +0000735/*
736** This is called to code FOR EACH ROW triggers.
737**
738** When the code that this function generates is executed, the following
739** must be true:
drhc977f7f2002-05-21 11:38:11 +0000740**
741** 1. No cursors may be open in the main database. (But newIdx and oldIdx
742** can be indices of cursors in temporary tables. See below.)
743**
danielk1977633ed082002-05-17 00:05:58 +0000744** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then
745** a temporary vdbe cursor (index newIdx) must be open and pointing at
746** a row containing values to be substituted for new.* expressions in the
747** trigger program(s).
drhc977f7f2002-05-21 11:38:11 +0000748**
danielk1977633ed082002-05-17 00:05:58 +0000749** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then
750** a temporary vdbe cursor (index oldIdx) must be open and pointing at
751** a row containing values to be substituted for old.* expressions in the
752** trigger program(s).
753**
danielk19778f2c54e2008-01-01 19:02:09 +0000754** If they are not NULL, the piOldColMask and piNewColMask output variables
755** are set to values that describe the columns used by the trigger program
756** in the OLD.* and NEW.* tables respectively. If column N of the
757** pseudo-table is read at least once, the corresponding bit of the output
758** mask is set. If a column with an index greater than 32 is read, the
759** output mask is set to the special value 0xffffffff.
760**
danielk1977633ed082002-05-17 00:05:58 +0000761*/
danielk19774adee202004-05-08 08:23:19 +0000762int sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +0000763 Parse *pParse, /* Parse context */
764 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
765 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +0000766 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +0000767 Table *pTab, /* The table to code triggers from */
768 int newIdx, /* The indice of the "new" row to access */
769 int oldIdx, /* The indice of the "old" row to access */
danielk19776f349032002-06-11 02:25:40 +0000770 int orconf, /* ON CONFLICT policy */
danielk19778f2c54e2008-01-01 19:02:09 +0000771 int ignoreJump, /* Instruction to jump to for RAISE(IGNORE) */
772 u32 *piOldColMask, /* OUT: Mask of columns used from the OLD.* table */
773 u32 *piNewColMask /* OUT: Mask of columns used from the NEW.* table */
drhc977f7f2002-05-21 11:38:11 +0000774){
danielk1977eecfb3e2006-01-10 12:31:39 +0000775 Trigger *p;
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;
danielk1977932083c2007-11-16 14:55:46 +0000788 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000789
danielk1977eecfb3e2006-01-10 12:31:39 +0000790 /* Determine whether we should code this trigger */
791 if(
792 p->op==op &&
793 p->tr_tm==tr_tm &&
danielk1977932083c2007-11-16 14:55:46 +0000794 (p->pSchema==p->pTabSchema || p->pSchema==db->aDb[1].pSchema) &&
danielk1977eecfb3e2006-01-10 12:31:39 +0000795 (op!=TK_UPDATE||!p->pColumns||checkColumnOverLap(p->pColumns,pChanges))
796 ){
797 TriggerStack *pS; /* Pointer to trigger-stack entry */
drh74161702006-02-24 02:53:49 +0000798 for(pS=pParse->trigStack; pS && p!=pS->pTrigger; pS=pS->pNext){}
danielk1977eecfb3e2006-01-10 12:31:39 +0000799 if( !pS ){
800 fire_this = 1;
danielk1977f29ce552002-05-19 23:43:12 +0000801 }
drh229caa32006-03-25 15:52:19 +0000802#if 0 /* Give no warning for recursive triggers. Just do not do them */
803 else{
804 sqlite3ErrorMsg(pParse, "recursive triggers not supported (%s)",
805 p->name);
806 return SQLITE_ERROR;
807 }
808#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000809 }
drhad6d9462004-09-19 02:15:24 +0000810
drh67040462004-09-24 12:24:36 +0000811 if( fire_this ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000812 int endTrigger;
danielk1977c3f9bad2002-05-15 08:30:12 +0000813 Expr * whenExpr;
drh85e20962003-04-25 17:52:11 +0000814 AuthContext sContext;
danielk1977b3bce662005-01-29 08:32:43 +0000815 NameContext sNC;
danielk1977c3f9bad2002-05-15 08:30:12 +0000816
danielk1977b3bce662005-01-29 08:32:43 +0000817 memset(&sNC, 0, sizeof(sNC));
818 sNC.pParse = pParse;
danielk1977c3f9bad2002-05-15 08:30:12 +0000819
820 /* Push an entry on to the trigger stack */
danielk1977eecfb3e2006-01-10 12:31:39 +0000821 trigStackEntry.pTrigger = p;
drh67040462004-09-24 12:24:36 +0000822 trigStackEntry.newIdx = newIdx;
823 trigStackEntry.oldIdx = oldIdx;
824 trigStackEntry.pTab = pTab;
825 trigStackEntry.pNext = pParse->trigStack;
826 trigStackEntry.ignoreJump = ignoreJump;
827 pParse->trigStack = &trigStackEntry;
danielk1977eecfb3e2006-01-10 12:31:39 +0000828 sqlite3AuthContextPush(pParse, &sContext, p->name);
danielk1977c3f9bad2002-05-15 08:30:12 +0000829
830 /* code the WHEN clause */
danielk19774adee202004-05-08 08:23:19 +0000831 endTrigger = sqlite3VdbeMakeLabel(pParse->pVdbe);
danielk1977932083c2007-11-16 14:55:46 +0000832 whenExpr = sqlite3ExprDup(db, p->pWhen);
833 if( db->mallocFailed || sqlite3ExprResolveNames(&sNC, whenExpr) ){
drh67040462004-09-24 12:24:36 +0000834 pParse->trigStack = trigStackEntry.pNext;
danielk19774adee202004-05-08 08:23:19 +0000835 sqlite3ExprDelete(whenExpr);
drh9adf9ac2002-05-15 11:44:13 +0000836 return 1;
danielk1977c3f9bad2002-05-15 08:30:12 +0000837 }
danielk19774adee202004-05-08 08:23:19 +0000838 sqlite3ExprIfFalse(pParse, whenExpr, endTrigger, 1);
839 sqlite3ExprDelete(whenExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000840
danielk1977eecfb3e2006-01-10 12:31:39 +0000841 codeTriggerProgram(pParse, p->step_list, orconf);
danielk1977c3f9bad2002-05-15 08:30:12 +0000842
843 /* Pop the entry off the trigger stack */
drh67040462004-09-24 12:24:36 +0000844 pParse->trigStack = trigStackEntry.pNext;
danielk19774adee202004-05-08 08:23:19 +0000845 sqlite3AuthContextPop(&sContext);
danielk1977c3f9bad2002-05-15 08:30:12 +0000846
danielk19774adee202004-05-08 08:23:19 +0000847 sqlite3VdbeResolveLabel(pParse->pVdbe, endTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000848 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000849 }
danielk19778f2c54e2008-01-01 19:02:09 +0000850 if( piOldColMask ) *piOldColMask |= trigStackEntry.oldColMask;
851 if( piNewColMask ) *piNewColMask |= trigStackEntry.newColMask;
danielk1977c3f9bad2002-05-15 08:30:12 +0000852 return 0;
853}
drhb7f91642004-10-31 02:22:47 +0000854#endif /* !defined(SQLITE_OMIT_TRIGGER) */