blob: 70cf6aba53184d1f9ee153a8ecf71764e8cd2e7d [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 ){
drhc977f7f2002-05-21 11:38:11 +0000230 Vdbe *v;
drh2d401ab2008-01-10 23:50:11 +0000231 char *z;
danielk1977c3f9bad2002-05-15 08:30:12 +0000232
233 /* Make an entry in the sqlite_master table */
danielk19774adee202004-05-08 08:23:19 +0000234 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000235 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000236 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11 +0000237 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
238 sqlite3NestedParse(pParse,
239 "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
240 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pTrig->name,
241 pTrig->table, z);
242 sqlite3_free(z);
drh9cbf3422008-01-17 16:22:13 +0000243 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +0000244 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, sqlite3MPrintf(
245 db, "type='trigger' AND name='%q'", pTrig->name), P4_DYNAMIC
danielk19771e536952007-08-16 10:09:01 +0000246 );
danielk1977c3f9bad2002-05-15 08:30:12 +0000247 }
248
drh956bc922004-07-24 17:38:29 +0000249 if( db->init.busy ){
danielk1977aaf22682006-01-06 15:03:48 +0000250 int n;
drhf0f258b2003-04-21 18:48:45 +0000251 Table *pTab;
danielk1977d5d56522005-03-16 12:15:20 +0000252 Trigger *pDel;
danielk1977da184232006-01-05 11:34:32 +0000253 pDel = sqlite3HashInsert(&db->aDb[iDb].pSchema->trigHash,
drhe4df0e72006-03-29 00:24:06 +0000254 pTrig->name, strlen(pTrig->name), pTrig);
danielk1977d5d56522005-03-16 12:15:20 +0000255 if( pDel ){
drhf3a65f72007-08-22 20:18:21 +0000256 assert( pDel==pTrig );
257 db->mallocFailed = 1;
danielk1977d5d56522005-03-16 12:15:20 +0000258 goto triggerfinish_cleanup;
259 }
danielk1977aaf22682006-01-06 15:03:48 +0000260 n = strlen(pTrig->table) + 1;
261 pTab = sqlite3HashFind(&pTrig->pTabSchema->tblHash, pTrig->table, n);
drhf0f258b2003-04-21 18:48:45 +0000262 assert( pTab!=0 );
danielk1977bfb9e352005-01-24 13:03:32 +0000263 pTrig->pNext = pTab->pTrigger;
264 pTab->pTrigger = pTrig;
265 pTrig = 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000266 }
267
drhf0f258b2003-04-21 18:48:45 +0000268triggerfinish_cleanup:
danielk1977bfb9e352005-01-24 13:03:32 +0000269 sqlite3DeleteTrigger(pTrig);
270 assert( !pParse->pNewTrigger );
danielk19774adee202004-05-08 08:23:19 +0000271 sqlite3DeleteTriggerStep(pStepList);
drh4b59ab52002-08-24 18:24:51 +0000272}
danielk1977c3f9bad2002-05-15 08:30:12 +0000273
drh4b59ab52002-08-24 18:24:51 +0000274/*
275** Make a copy of all components of the given trigger step. This has
276** the effect of copying all Expr.token.z values into memory obtained
drh17435752007-08-16 04:30:38 +0000277** from sqlite3_malloc(). As initially created, the Expr.token.z values
drh4b59ab52002-08-24 18:24:51 +0000278** all point to the input string that was fed to the parser. But that
danielk19776f8a5032004-05-10 10:34:51 +0000279** string is ephemeral - it will go away as soon as the sqlite3_exec()
drh4b59ab52002-08-24 18:24:51 +0000280** call that started the parser exits. This routine makes a persistent
281** copy of all the Expr.token.z strings so that the TriggerStep structure
danielk19776f8a5032004-05-10 10:34:51 +0000282** will be valid even after the sqlite3_exec() call returns.
drh4b59ab52002-08-24 18:24:51 +0000283*/
drh17435752007-08-16 04:30:38 +0000284static void sqlitePersistTriggerStep(sqlite3 *db, TriggerStep *p){
drh4b59ab52002-08-24 18:24:51 +0000285 if( p->target.z ){
drh17435752007-08-16 04:30:38 +0000286 p->target.z = (u8*)sqlite3DbStrNDup(db, (char*)p->target.z, p->target.n);
drh4b59ab52002-08-24 18:24:51 +0000287 p->target.dyn = 1;
288 }
289 if( p->pSelect ){
drh17435752007-08-16 04:30:38 +0000290 Select *pNew = sqlite3SelectDup(db, p->pSelect);
danielk19774adee202004-05-08 08:23:19 +0000291 sqlite3SelectDelete(p->pSelect);
drh4b59ab52002-08-24 18:24:51 +0000292 p->pSelect = pNew;
293 }
294 if( p->pWhere ){
drh17435752007-08-16 04:30:38 +0000295 Expr *pNew = sqlite3ExprDup(db, p->pWhere);
danielk19774adee202004-05-08 08:23:19 +0000296 sqlite3ExprDelete(p->pWhere);
drh4b59ab52002-08-24 18:24:51 +0000297 p->pWhere = pNew;
298 }
299 if( p->pExprList ){
drh17435752007-08-16 04:30:38 +0000300 ExprList *pNew = sqlite3ExprListDup(db, p->pExprList);
danielk19774adee202004-05-08 08:23:19 +0000301 sqlite3ExprListDelete(p->pExprList);
drh4b59ab52002-08-24 18:24:51 +0000302 p->pExprList = pNew;
303 }
304 if( p->pIdList ){
drh17435752007-08-16 04:30:38 +0000305 IdList *pNew = sqlite3IdListDup(db, p->pIdList);
danielk19774adee202004-05-08 08:23:19 +0000306 sqlite3IdListDelete(p->pIdList);
drh4b59ab52002-08-24 18:24:51 +0000307 p->pIdList = pNew;
danielk1977c3f9bad2002-05-15 08:30:12 +0000308 }
309}
310
drhc977f7f2002-05-21 11:38:11 +0000311/*
312** Turn a SELECT statement (that the pSelect parameter points to) into
313** a trigger step. Return a pointer to a TriggerStep structure.
314**
315** The parser calls this routine when it finds a SELECT statement in
316** body of a TRIGGER.
317*/
drh17435752007-08-16 04:30:38 +0000318TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
319 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000320 if( pTriggerStep==0 ) {
321 sqlite3SelectDelete(pSelect);
322 return 0;
323 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000324
danielk1977633ed082002-05-17 00:05:58 +0000325 pTriggerStep->op = TK_SELECT;
326 pTriggerStep->pSelect = pSelect;
327 pTriggerStep->orconf = OE_Default;
drh17435752007-08-16 04:30:38 +0000328 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977c3f9bad2002-05-15 08:30:12 +0000329
danielk1977633ed082002-05-17 00:05:58 +0000330 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000331}
332
drhc977f7f2002-05-21 11:38:11 +0000333/*
334** Build a trigger step out of an INSERT statement. Return a pointer
335** to the new trigger step.
336**
337** The parser calls this routine when it sees an INSERT inside the
338** body of a trigger.
339*/
danielk19774adee202004-05-08 08:23:19 +0000340TriggerStep *sqlite3TriggerInsertStep(
drh17435752007-08-16 04:30:38 +0000341 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000342 Token *pTableName, /* Name of the table into which we insert */
343 IdList *pColumn, /* List of columns in pTableName to insert into */
344 ExprList *pEList, /* The VALUE clause: a list of values to be inserted */
345 Select *pSelect, /* A SELECT statement that supplies values */
346 int orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
danielk1977633ed082002-05-17 00:05:58 +0000347){
drhf3a65f72007-08-22 20:18:21 +0000348 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000349
danielk1977633ed082002-05-17 00:05:58 +0000350 assert(pEList == 0 || pSelect == 0);
drhf3a65f72007-08-22 20:18:21 +0000351 assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000352
drhf3a65f72007-08-22 20:18:21 +0000353 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk1977d5d56522005-03-16 12:15:20 +0000354 if( pTriggerStep ){
355 pTriggerStep->op = TK_INSERT;
356 pTriggerStep->pSelect = pSelect;
357 pTriggerStep->target = *pTableName;
358 pTriggerStep->pIdList = pColumn;
359 pTriggerStep->pExprList = pEList;
360 pTriggerStep->orconf = orconf;
drh17435752007-08-16 04:30:38 +0000361 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977d5d56522005-03-16 12:15:20 +0000362 }else{
363 sqlite3IdListDelete(pColumn);
364 sqlite3ExprListDelete(pEList);
drh17435752007-08-16 04:30:38 +0000365 sqlite3SelectDelete(pSelect);
danielk1977d5d56522005-03-16 12:15:20 +0000366 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000367
danielk1977633ed082002-05-17 00:05:58 +0000368 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000369}
370
drhc977f7f2002-05-21 11:38:11 +0000371/*
372** Construct a trigger step that implements an UPDATE statement and return
373** a pointer to that trigger step. The parser calls this routine when it
374** sees an UPDATE statement inside the body of a CREATE TRIGGER.
375*/
danielk19774adee202004-05-08 08:23:19 +0000376TriggerStep *sqlite3TriggerUpdateStep(
drh17435752007-08-16 04:30:38 +0000377 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000378 Token *pTableName, /* Name of the table to be updated */
379 ExprList *pEList, /* The SET clause: list of column and new values */
380 Expr *pWhere, /* The WHERE clause */
381 int orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
382){
drh17435752007-08-16 04:30:38 +0000383 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
drh0e3a6f32007-03-30 20:40:34 +0000384 if( pTriggerStep==0 ){
385 sqlite3ExprListDelete(pEList);
386 sqlite3ExprDelete(pWhere);
387 return 0;
388 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000389
danielk1977633ed082002-05-17 00:05:58 +0000390 pTriggerStep->op = TK_UPDATE;
391 pTriggerStep->target = *pTableName;
392 pTriggerStep->pExprList = pEList;
393 pTriggerStep->pWhere = pWhere;
394 pTriggerStep->orconf = orconf;
drh17435752007-08-16 04:30:38 +0000395 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977c3f9bad2002-05-15 08:30:12 +0000396
danielk1977633ed082002-05-17 00:05:58 +0000397 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000398}
399
drhc977f7f2002-05-21 11:38:11 +0000400/*
401** Construct a trigger step that implements a DELETE statement and return
402** a pointer to that trigger step. The parser calls this routine when it
403** sees a DELETE statement inside the body of a CREATE TRIGGER.
404*/
drh17435752007-08-16 04:30:38 +0000405TriggerStep *sqlite3TriggerDeleteStep(
406 sqlite3 *db, /* Database connection */
407 Token *pTableName, /* The table from which rows are deleted */
408 Expr *pWhere /* The WHERE clause */
409){
410 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
drhb63a53d2007-03-31 01:34:44 +0000411 if( pTriggerStep==0 ){
412 sqlite3ExprDelete(pWhere);
413 return 0;
414 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000415
danielk1977633ed082002-05-17 00:05:58 +0000416 pTriggerStep->op = TK_DELETE;
417 pTriggerStep->target = *pTableName;
418 pTriggerStep->pWhere = pWhere;
419 pTriggerStep->orconf = OE_Default;
drh17435752007-08-16 04:30:38 +0000420 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977c3f9bad2002-05-15 08:30:12 +0000421
danielk1977633ed082002-05-17 00:05:58 +0000422 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000423}
424
danielk1977633ed082002-05-17 00:05:58 +0000425/*
426** Recursively delete a Trigger structure
427*/
danielk19774adee202004-05-08 08:23:19 +0000428void sqlite3DeleteTrigger(Trigger *pTrigger){
drhf0f258b2003-04-21 18:48:45 +0000429 if( pTrigger==0 ) return;
danielk19774adee202004-05-08 08:23:19 +0000430 sqlite3DeleteTriggerStep(pTrigger->step_list);
drh17435752007-08-16 04:30:38 +0000431 sqlite3_free(pTrigger->name);
432 sqlite3_free(pTrigger->table);
danielk19774adee202004-05-08 08:23:19 +0000433 sqlite3ExprDelete(pTrigger->pWhen);
434 sqlite3IdListDelete(pTrigger->pColumns);
drh17435752007-08-16 04:30:38 +0000435 if( pTrigger->nameToken.dyn ) sqlite3_free((char*)pTrigger->nameToken.z);
436 sqlite3_free(pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000437}
438
439/*
danielk19778a414492004-06-29 08:59:35 +0000440** This function is called to drop a trigger from the database schema.
441**
442** This may be called directly from the parser and therefore identifies
443** the trigger by name. The sqlite3DropTriggerPtr() routine does the
444** same job as this routine except it takes a pointer to the trigger
445** instead of the trigger name.
446**/
drhfdd48a72006-09-11 23:45:48 +0000447void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000448 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000449 int i;
450 const char *zDb;
451 const char *zName;
452 int nName;
drh9bb575f2004-09-06 17:24:11 +0000453 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000454
drh17435752007-08-16 04:30:38 +0000455 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000456 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000457 goto drop_trigger_cleanup;
458 }
danielk19778e227872004-06-07 07:52:17 +0000459
drhd24cc422003-03-27 12:51:24 +0000460 assert( pName->nSrc==1 );
461 zDb = pName->a[0].zDatabase;
462 zName = pName->a[0].zName;
463 nName = strlen(zName);
danielk197753c0f742005-03-29 03:10:59 +0000464 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000465 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000466 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
drhe4df0e72006-03-29 00:24:06 +0000467 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
drhd24cc422003-03-27 12:51:24 +0000468 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000469 }
drhd24cc422003-03-27 12:51:24 +0000470 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000471 if( !noErr ){
472 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
473 }
drhd24cc422003-03-27 12:51:24 +0000474 goto drop_trigger_cleanup;
475 }
drh74161702006-02-24 02:53:49 +0000476 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000477
478drop_trigger_cleanup:
danielk19774adee202004-05-08 08:23:19 +0000479 sqlite3SrcListDelete(pName);
drh79a519c2003-05-17 19:04:03 +0000480}
481
482/*
drh956bc922004-07-24 17:38:29 +0000483** Return a pointer to the Table structure for the table that a trigger
484** is set on.
485*/
drh74161702006-02-24 02:53:49 +0000486static Table *tableOfTrigger(Trigger *pTrigger){
danielk1977aaf22682006-01-06 15:03:48 +0000487 int n = strlen(pTrigger->table) + 1;
488 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
drh956bc922004-07-24 17:38:29 +0000489}
490
491
492/*
drh74161702006-02-24 02:53:49 +0000493** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000494*/
drh74161702006-02-24 02:53:49 +0000495void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000496 Table *pTable;
497 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000498 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000499 int iDb;
drh79a519c2003-05-17 19:04:03 +0000500
danielk1977da184232006-01-05 11:34:32 +0000501 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000502 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000503 pTable = tableOfTrigger(pTrigger);
drh43617e92006-03-06 20:55:46 +0000504 assert( pTable );
danielk1977da184232006-01-05 11:34:32 +0000505 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000506#ifndef SQLITE_OMIT_AUTHORIZATION
507 {
508 int code = SQLITE_DROP_TRIGGER;
drh956bc922004-07-24 17:38:29 +0000509 const char *zDb = db->aDb[iDb].zName;
510 const char *zTab = SCHEMA_TABLE(iDb);
511 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
danielk19774adee202004-05-08 08:23:19 +0000512 if( sqlite3AuthCheck(pParse, code, pTrigger->name, pTable->zName, zDb) ||
513 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000514 return;
drhe5f9c642003-01-13 23:27:31 +0000515 }
drhed6c8672003-01-12 18:02:16 +0000516 }
drhe5f9c642003-01-13 23:27:31 +0000517#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000518
drhf0f258b2003-04-21 18:48:45 +0000519 /* Generate code to destroy the database record of the trigger.
520 */
drh43617e92006-03-06 20:55:46 +0000521 assert( pTable!=0 );
522 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drhf0f258b2003-04-21 18:48:45 +0000523 int base;
drh57196282004-10-06 15:41:16 +0000524 static const VdbeOpList dropTrigger[] = {
drh9b1b01b2003-08-16 12:37:51 +0000525 { OP_Rewind, 0, ADDR(9), 0},
drh1db639c2008-01-17 02:36:28 +0000526 { OP_String8, 0, 1, 0}, /* 1 */
527 { OP_Column, 0, 1, 2},
528 { OP_Ne, 2, ADDR(8), 1},
529 { OP_String8, 0, 1, 0}, /* 4: "trigger" */
530 { OP_Column, 0, 0, 2},
531 { OP_Ne, 2, ADDR(8), 1},
drhf0f258b2003-04-21 18:48:45 +0000532 { OP_Delete, 0, 0, 0},
drh9b1b01b2003-08-16 12:37:51 +0000533 { OP_Next, 0, ADDR(1), 0}, /* 8 */
drhf0f258b2003-04-21 18:48:45 +0000534 };
535
drh956bc922004-07-24 17:38:29 +0000536 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk1977c00da102006-01-07 13:21:04 +0000537 sqlite3OpenMasterTable(pParse, iDb);
danielk19774adee202004-05-08 08:23:19 +0000538 base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger);
drh66a51672008-01-03 00:01:23 +0000539 sqlite3VdbeChangeP4(v, base+1, pTrigger->name, 0);
drh24003452008-01-03 01:28:59 +0000540 sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
drh9cbf3422008-01-17 16:22:13 +0000541 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +0000542 sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
543 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->name, 0);
drhf0f258b2003-04-21 18:48:45 +0000544 }
drh956bc922004-07-24 17:38:29 +0000545}
drhf0f258b2003-04-21 18:48:45 +0000546
drh956bc922004-07-24 17:38:29 +0000547/*
548** Remove a trigger from the hash tables of the sqlite* pointer.
549*/
550void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
551 Trigger *pTrigger;
552 int nName = strlen(zName);
drhe4df0e72006-03-29 00:24:06 +0000553 pTrigger = sqlite3HashInsert(&(db->aDb[iDb].pSchema->trigHash),
554 zName, nName, 0);
drh956bc922004-07-24 17:38:29 +0000555 if( pTrigger ){
drh74161702006-02-24 02:53:49 +0000556 Table *pTable = tableOfTrigger(pTrigger);
drh956bc922004-07-24 17:38:29 +0000557 assert( pTable!=0 );
danielk1977633ed082002-05-17 00:05:58 +0000558 if( pTable->pTrigger == pTrigger ){
559 pTable->pTrigger = pTrigger->pNext;
danielk1977f29ce552002-05-19 23:43:12 +0000560 }else{
danielk1977633ed082002-05-17 00:05:58 +0000561 Trigger *cc = pTable->pTrigger;
562 while( cc ){
563 if( cc->pNext == pTrigger ){
drh9adf9ac2002-05-15 11:44:13 +0000564 cc->pNext = cc->pNext->pNext;
565 break;
566 }
567 cc = cc->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +0000568 }
569 assert(cc);
570 }
danielk19774adee202004-05-08 08:23:19 +0000571 sqlite3DeleteTrigger(pTrigger);
drh956bc922004-07-24 17:38:29 +0000572 db->flags |= SQLITE_InternChanges;
danielk1977c3f9bad2002-05-15 08:30:12 +0000573 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000574}
575
drhc977f7f2002-05-21 11:38:11 +0000576/*
577** pEList is the SET clause of an UPDATE statement. Each entry
578** in pEList is of the format <id>=<expr>. If any of the entries
579** in pEList have an <id> which matches an identifier in pIdList,
580** then return TRUE. If pIdList==NULL, then it is considered a
581** wildcard that matches anything. Likewise if pEList==NULL then
582** it matches anything so always return true. Return false only
583** if there is no match.
584*/
585static int checkColumnOverLap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000586 int e;
587 if( !pIdList || !pEList ) return 1;
588 for(e=0; e<pEList->nExpr; e++){
danielk19774adee202004-05-08 08:23:19 +0000589 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000590 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000591 return 0;
592}
593
danielk1977c3f9bad2002-05-15 08:30:12 +0000594/*
drhdca76842004-12-07 14:06:13 +0000595** Return a bit vector to indicate what kind of triggers exist for operation
596** "op" on table pTab. If pChanges is not NULL then it is a list of columns
597** that are being updated. Triggers only match if the ON clause of the
598** trigger definition overlaps the set of columns being updated.
599**
600** The returned bit vector is some combination of TRIGGER_BEFORE and
601** TRIGGER_AFTER.
602*/
danielk19774adee202004-05-08 08:23:19 +0000603int sqlite3TriggersExist(
drhc977f7f2002-05-21 11:38:11 +0000604 Parse *pParse, /* Used to check for recursive triggers */
drhdca76842004-12-07 14:06:13 +0000605 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000606 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
drhc977f7f2002-05-21 11:38:11 +0000607 ExprList *pChanges /* Columns that change in an UPDATE statement */
608){
drh4cbdda92006-06-14 19:00:20 +0000609 Trigger *pTrigger;
drhdca76842004-12-07 14:06:13 +0000610 int mask = 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000611
drh4cbdda92006-06-14 19:00:20 +0000612 pTrigger = IsVirtual(pTab) ? 0 : pTab->pTrigger;
drhdca76842004-12-07 14:06:13 +0000613 while( pTrigger ){
614 if( pTrigger->op==op && checkColumnOverLap(pTrigger->pColumns, pChanges) ){
drh229caa32006-03-25 15:52:19 +0000615 mask |= pTrigger->tr_tm;
danielk1977c3f9bad2002-05-15 08:30:12 +0000616 }
drhdca76842004-12-07 14:06:13 +0000617 pTrigger = pTrigger->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +0000618 }
drhdca76842004-12-07 14:06:13 +0000619 return mask;
danielk1977c3f9bad2002-05-15 08:30:12 +0000620}
621
drhc977f7f2002-05-21 11:38:11 +0000622/*
drhf26e09c2003-05-31 16:21:12 +0000623** Convert the pStep->target token into a SrcList and return a pointer
624** to that SrcList.
625**
626** This routine adds a specific database name, if needed, to the target when
627** forming the SrcList. This prevents a trigger in one database from
628** referring to a target in another database. An exception is when the
629** trigger is in TEMP in which case it can refer to any other database it
630** wants.
631*/
632static SrcList *targetSrcList(
633 Parse *pParse, /* The parsing context */
634 TriggerStep *pStep /* The trigger containing the target token */
635){
636 Token sDb; /* Dummy database name token */
637 int iDb; /* Index of the database to use */
638 SrcList *pSrc; /* SrcList to be returned */
639
danielk1977da184232006-01-05 11:34:32 +0000640 iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
drhf26e09c2003-05-31 16:21:12 +0000641 if( iDb==0 || iDb>=2 ){
642 assert( iDb<pParse->db->nDb );
drh2646da72005-12-09 20:02:05 +0000643 sDb.z = (u8*)pParse->db->aDb[iDb].zName;
644 sDb.n = strlen((char*)sDb.z);
drh17435752007-08-16 04:30:38 +0000645 pSrc = sqlite3SrcListAppend(pParse->db, 0, &sDb, &pStep->target);
drhf26e09c2003-05-31 16:21:12 +0000646 } else {
drh17435752007-08-16 04:30:38 +0000647 pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
drhf26e09c2003-05-31 16:21:12 +0000648 }
649 return pSrc;
650}
651
652/*
drhc977f7f2002-05-21 11:38:11 +0000653** Generate VDBE code for zero or more statements inside the body of a
654** trigger.
655*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000656static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000657 Parse *pParse, /* The parser context */
658 TriggerStep *pStepList, /* List of statements inside the trigger body */
659 int orconfin /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000660){
661 TriggerStep * pTriggerStep = pStepList;
662 int orconf;
drh344737f2004-09-19 00:50:20 +0000663 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000664 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000665
drh344737f2004-09-19 00:50:20 +0000666 assert( pTriggerStep!=0 );
667 assert( v!=0 );
drh66a51672008-01-03 00:01:23 +0000668 sqlite3VdbeAddOp2(v, OP_ContextPush, 0, 0);
drhd4e70eb2008-01-02 00:34:36 +0000669 VdbeComment((v, "begin trigger %s", pStepList->pTrig->name));
danielk1977633ed082002-05-17 00:05:58 +0000670 while( pTriggerStep ){
danielk1977633ed082002-05-17 00:05:58 +0000671 orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin;
672 pParse->trigStack->orconf = orconf;
673 switch( pTriggerStep->op ){
674 case TK_SELECT: {
drh17435752007-08-16 04:30:38 +0000675 Select *ss = sqlite3SelectDup(db, pTriggerStep->pSelect);
drh28f45912006-10-18 23:26:38 +0000676 if( ss ){
drh1013c932008-01-06 00:25:21 +0000677 SelectDest dest;
678
679 sqlite3SelectDestInit(&dest, SRT_Discard, 0);
drh28f45912006-10-18 23:26:38 +0000680 sqlite3SelectResolve(pParse, ss, 0);
danielk19776c8c8ce2008-01-02 16:27:09 +0000681 sqlite3Select(pParse, ss, &dest, 0, 0, 0, 0);
drh28f45912006-10-18 23:26:38 +0000682 sqlite3SelectDelete(ss);
683 }
drhfd131da2007-08-07 17:13:03 +0000684 break;
danielk1977633ed082002-05-17 00:05:58 +0000685 }
686 case TK_UPDATE: {
drh113088e2003-03-20 01:16:58 +0000687 SrcList *pSrc;
drhf26e09c2003-05-31 16:21:12 +0000688 pSrc = targetSrcList(pParse, pTriggerStep);
drh66a51672008-01-03 00:01:23 +0000689 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000690 sqlite3Update(pParse, pSrc,
drh17435752007-08-16 04:30:38 +0000691 sqlite3ExprListDup(db, pTriggerStep->pExprList),
692 sqlite3ExprDup(db, pTriggerStep->pWhere), orconf);
drh66a51672008-01-03 00:01:23 +0000693 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000694 break;
695 }
696 case TK_INSERT: {
drh113088e2003-03-20 01:16:58 +0000697 SrcList *pSrc;
drhf26e09c2003-05-31 16:21:12 +0000698 pSrc = targetSrcList(pParse, pTriggerStep);
drh66a51672008-01-03 00:01:23 +0000699 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000700 sqlite3Insert(pParse, pSrc,
drh17435752007-08-16 04:30:38 +0000701 sqlite3ExprListDup(db, pTriggerStep->pExprList),
702 sqlite3SelectDup(db, pTriggerStep->pSelect),
703 sqlite3IdListDup(db, pTriggerStep->pIdList), orconf);
drh66a51672008-01-03 00:01:23 +0000704 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000705 break;
706 }
707 case TK_DELETE: {
drh113088e2003-03-20 01:16:58 +0000708 SrcList *pSrc;
drh66a51672008-01-03 00:01:23 +0000709 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
drhf26e09c2003-05-31 16:21:12 +0000710 pSrc = targetSrcList(pParse, pTriggerStep);
drh17435752007-08-16 04:30:38 +0000711 sqlite3DeleteFrom(pParse, pSrc,
712 sqlite3ExprDup(db, pTriggerStep->pWhere));
drh66a51672008-01-03 00:01:23 +0000713 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000714 break;
715 }
716 default:
717 assert(0);
718 }
danielk1977633ed082002-05-17 00:05:58 +0000719 pTriggerStep = pTriggerStep->pNext;
720 }
drh66a51672008-01-03 00:01:23 +0000721 sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
drhd4e70eb2008-01-02 00:34:36 +0000722 VdbeComment((v, "end trigger %s", pStepList->pTrig->name));
danielk1977c3f9bad2002-05-15 08:30:12 +0000723
danielk1977633ed082002-05-17 00:05:58 +0000724 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000725}
726
danielk1977633ed082002-05-17 00:05:58 +0000727/*
728** This is called to code FOR EACH ROW triggers.
729**
730** When the code that this function generates is executed, the following
731** must be true:
drhc977f7f2002-05-21 11:38:11 +0000732**
733** 1. No cursors may be open in the main database. (But newIdx and oldIdx
734** can be indices of cursors in temporary tables. See below.)
735**
danielk1977633ed082002-05-17 00:05:58 +0000736** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then
737** a temporary vdbe cursor (index newIdx) must be open and pointing at
738** a row containing values to be substituted for new.* expressions in the
739** trigger program(s).
drhc977f7f2002-05-21 11:38:11 +0000740**
danielk1977633ed082002-05-17 00:05:58 +0000741** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then
742** a temporary vdbe cursor (index oldIdx) must be open and pointing at
743** a row containing values to be substituted for old.* expressions in the
744** trigger program(s).
745**
danielk19778f2c54e2008-01-01 19:02:09 +0000746** If they are not NULL, the piOldColMask and piNewColMask output variables
747** are set to values that describe the columns used by the trigger program
748** in the OLD.* and NEW.* tables respectively. If column N of the
749** pseudo-table is read at least once, the corresponding bit of the output
750** mask is set. If a column with an index greater than 32 is read, the
751** output mask is set to the special value 0xffffffff.
752**
danielk1977633ed082002-05-17 00:05:58 +0000753*/
danielk19774adee202004-05-08 08:23:19 +0000754int sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +0000755 Parse *pParse, /* Parse context */
756 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
757 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +0000758 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +0000759 Table *pTab, /* The table to code triggers from */
760 int newIdx, /* The indice of the "new" row to access */
761 int oldIdx, /* The indice of the "old" row to access */
danielk19776f349032002-06-11 02:25:40 +0000762 int orconf, /* ON CONFLICT policy */
danielk19778f2c54e2008-01-01 19:02:09 +0000763 int ignoreJump, /* Instruction to jump to for RAISE(IGNORE) */
764 u32 *piOldColMask, /* OUT: Mask of columns used from the OLD.* table */
765 u32 *piNewColMask /* OUT: Mask of columns used from the NEW.* table */
drhc977f7f2002-05-21 11:38:11 +0000766){
danielk1977eecfb3e2006-01-10 12:31:39 +0000767 Trigger *p;
drh0e359b32008-01-13 19:02:11 +0000768 sqlite3 *db = pParse->db;
drh67040462004-09-24 12:24:36 +0000769 TriggerStack trigStackEntry;
danielk1977c3f9bad2002-05-15 08:30:12 +0000770
danielk19778f2c54e2008-01-01 19:02:09 +0000771 trigStackEntry.oldColMask = 0;
772 trigStackEntry.newColMask = 0;
773
danielk1977c3f9bad2002-05-15 08:30:12 +0000774 assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE);
drhdca76842004-12-07 14:06:13 +0000775 assert(tr_tm == TRIGGER_BEFORE || tr_tm == TRIGGER_AFTER );
danielk1977c3f9bad2002-05-15 08:30:12 +0000776
danielk1977633ed082002-05-17 00:05:58 +0000777 assert(newIdx != -1 || oldIdx != -1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000778
danielk1977eecfb3e2006-01-10 12:31:39 +0000779 for(p=pTab->pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +0000780 int fire_this = 0;
781
danielk1977eecfb3e2006-01-10 12:31:39 +0000782 /* Determine whether we should code this trigger */
783 if(
784 p->op==op &&
785 p->tr_tm==tr_tm &&
danielk1977932083c2007-11-16 14:55:46 +0000786 (p->pSchema==p->pTabSchema || p->pSchema==db->aDb[1].pSchema) &&
danielk1977eecfb3e2006-01-10 12:31:39 +0000787 (op!=TK_UPDATE||!p->pColumns||checkColumnOverLap(p->pColumns,pChanges))
788 ){
789 TriggerStack *pS; /* Pointer to trigger-stack entry */
drh74161702006-02-24 02:53:49 +0000790 for(pS=pParse->trigStack; pS && p!=pS->pTrigger; pS=pS->pNext){}
danielk1977eecfb3e2006-01-10 12:31:39 +0000791 if( !pS ){
792 fire_this = 1;
danielk1977f29ce552002-05-19 23:43:12 +0000793 }
drh229caa32006-03-25 15:52:19 +0000794#if 0 /* Give no warning for recursive triggers. Just do not do them */
795 else{
796 sqlite3ErrorMsg(pParse, "recursive triggers not supported (%s)",
797 p->name);
798 return SQLITE_ERROR;
799 }
800#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000801 }
drhad6d9462004-09-19 02:15:24 +0000802
drh67040462004-09-24 12:24:36 +0000803 if( fire_this ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000804 int endTrigger;
danielk1977c3f9bad2002-05-15 08:30:12 +0000805 Expr * whenExpr;
drh85e20962003-04-25 17:52:11 +0000806 AuthContext sContext;
danielk1977b3bce662005-01-29 08:32:43 +0000807 NameContext sNC;
danielk1977c3f9bad2002-05-15 08:30:12 +0000808
drh949f9cd2008-01-12 21:35:57 +0000809#ifndef SQLITE_OMIT_TRACE
810 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Trace, 0, 0, 0,
drh0e359b32008-01-13 19:02:11 +0000811 sqlite3MPrintf(db, "-- TRIGGER %s", p->name),
drh949f9cd2008-01-12 21:35:57 +0000812 P4_DYNAMIC);
813#endif
danielk1977b3bce662005-01-29 08:32:43 +0000814 memset(&sNC, 0, sizeof(sNC));
815 sNC.pParse = pParse;
danielk1977c3f9bad2002-05-15 08:30:12 +0000816
817 /* Push an entry on to the trigger stack */
danielk1977eecfb3e2006-01-10 12:31:39 +0000818 trigStackEntry.pTrigger = p;
drh67040462004-09-24 12:24:36 +0000819 trigStackEntry.newIdx = newIdx;
820 trigStackEntry.oldIdx = oldIdx;
821 trigStackEntry.pTab = pTab;
822 trigStackEntry.pNext = pParse->trigStack;
823 trigStackEntry.ignoreJump = ignoreJump;
824 pParse->trigStack = &trigStackEntry;
danielk1977eecfb3e2006-01-10 12:31:39 +0000825 sqlite3AuthContextPush(pParse, &sContext, p->name);
danielk1977c3f9bad2002-05-15 08:30:12 +0000826
827 /* code the WHEN clause */
danielk19774adee202004-05-08 08:23:19 +0000828 endTrigger = sqlite3VdbeMakeLabel(pParse->pVdbe);
danielk1977932083c2007-11-16 14:55:46 +0000829 whenExpr = sqlite3ExprDup(db, p->pWhen);
830 if( db->mallocFailed || sqlite3ExprResolveNames(&sNC, whenExpr) ){
drh67040462004-09-24 12:24:36 +0000831 pParse->trigStack = trigStackEntry.pNext;
danielk19774adee202004-05-08 08:23:19 +0000832 sqlite3ExprDelete(whenExpr);
drh9adf9ac2002-05-15 11:44:13 +0000833 return 1;
danielk1977c3f9bad2002-05-15 08:30:12 +0000834 }
drh35573352008-01-08 23:54:25 +0000835 sqlite3ExprIfFalse(pParse, whenExpr, endTrigger, SQLITE_JUMPIFNULL);
danielk19774adee202004-05-08 08:23:19 +0000836 sqlite3ExprDelete(whenExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000837
danielk1977eecfb3e2006-01-10 12:31:39 +0000838 codeTriggerProgram(pParse, p->step_list, orconf);
danielk1977c3f9bad2002-05-15 08:30:12 +0000839
840 /* Pop the entry off the trigger stack */
drh67040462004-09-24 12:24:36 +0000841 pParse->trigStack = trigStackEntry.pNext;
danielk19774adee202004-05-08 08:23:19 +0000842 sqlite3AuthContextPop(&sContext);
danielk1977c3f9bad2002-05-15 08:30:12 +0000843
danielk19774adee202004-05-08 08:23:19 +0000844 sqlite3VdbeResolveLabel(pParse->pVdbe, endTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000845 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000846 }
danielk19778f2c54e2008-01-01 19:02:09 +0000847 if( piOldColMask ) *piOldColMask |= trigStackEntry.oldColMask;
848 if( piNewColMask ) *piNewColMask |= trigStackEntry.newColMask;
danielk1977c3f9bad2002-05-15 08:30:12 +0000849 return 0;
850}
drhb7f91642004-10-31 02:22:47 +0000851#endif /* !defined(SQLITE_OMIT_TRIGGER) */