blob: 96e9b295841b9cb2b965b7034f25a7bb56121c70 [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 },
drh24003452008-01-03 01:28:59 +0000232 { OP_String8, 0, 0, 0 }, /* 1: "trigger" */
drh956bc922004-07-24 17:38:29 +0000233 { 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 },
drh24003452008-01-03 01:28:59 +0000236 { OP_String8, 0, 0, 0 }, /* 5: "CREATE TRIGGER " */
drh956bc922004-07-24 17:38:29 +0000237 { OP_String8, 0, 0, 0 }, /* 6: SQL */
drh855eb1c2004-08-31 13:45:11 +0000238 { OP_Concat, 0, 0, 0 },
drh24003452008-01-03 01:28:59 +0000239 { OP_MakeRecord, 5, 0, 0 }, /* 8: "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);
drh24003452008-01-03 01:28:59 +0000251 sqlite3VdbeChangeP4(v, addr+1, "trigger", P4_STATIC);
drh66a51672008-01-03 00:01:23 +0000252 sqlite3VdbeChangeP4(v, addr+2, pTrig->name, 0);
drh24003452008-01-03 01:28:59 +0000253 sqlite3VdbeChangeP4(v, addr+3, pTrig->table, 0);
254 sqlite3VdbeChangeP4(v, addr+5, "CREATE TRIGGER ", P4_STATIC);
drh66a51672008-01-03 00:01:23 +0000255 sqlite3VdbeChangeP4(v, addr+6, (char*)pAll->z, pAll->n);
drh24003452008-01-03 01:28:59 +0000256 sqlite3VdbeChangeP4(v, addr+8, "aaada", P4_STATIC);
danielk1977da184232006-01-05 11:34:32 +0000257 sqlite3ChangeCookie(db, v, iDb);
drh66a51672008-01-03 00:01:23 +0000258 sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
259 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, sqlite3MPrintf(
260 db, "type='trigger' AND name='%q'", pTrig->name), P4_DYNAMIC
danielk19771e536952007-08-16 10:09:01 +0000261 );
danielk1977c3f9bad2002-05-15 08:30:12 +0000262 }
263
drh956bc922004-07-24 17:38:29 +0000264 if( db->init.busy ){
danielk1977aaf22682006-01-06 15:03:48 +0000265 int n;
drhf0f258b2003-04-21 18:48:45 +0000266 Table *pTab;
danielk1977d5d56522005-03-16 12:15:20 +0000267 Trigger *pDel;
danielk1977da184232006-01-05 11:34:32 +0000268 pDel = sqlite3HashInsert(&db->aDb[iDb].pSchema->trigHash,
drhe4df0e72006-03-29 00:24:06 +0000269 pTrig->name, strlen(pTrig->name), pTrig);
danielk1977d5d56522005-03-16 12:15:20 +0000270 if( pDel ){
drhf3a65f72007-08-22 20:18:21 +0000271 assert( pDel==pTrig );
272 db->mallocFailed = 1;
danielk1977d5d56522005-03-16 12:15:20 +0000273 goto triggerfinish_cleanup;
274 }
danielk1977aaf22682006-01-06 15:03:48 +0000275 n = strlen(pTrig->table) + 1;
276 pTab = sqlite3HashFind(&pTrig->pTabSchema->tblHash, pTrig->table, n);
drhf0f258b2003-04-21 18:48:45 +0000277 assert( pTab!=0 );
danielk1977bfb9e352005-01-24 13:03:32 +0000278 pTrig->pNext = pTab->pTrigger;
279 pTab->pTrigger = pTrig;
280 pTrig = 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000281 }
282
drhf0f258b2003-04-21 18:48:45 +0000283triggerfinish_cleanup:
danielk1977bfb9e352005-01-24 13:03:32 +0000284 sqlite3DeleteTrigger(pTrig);
285 assert( !pParse->pNewTrigger );
danielk19774adee202004-05-08 08:23:19 +0000286 sqlite3DeleteTriggerStep(pStepList);
drh4b59ab52002-08-24 18:24:51 +0000287}
danielk1977c3f9bad2002-05-15 08:30:12 +0000288
drh4b59ab52002-08-24 18:24:51 +0000289/*
290** Make a copy of all components of the given trigger step. This has
291** the effect of copying all Expr.token.z values into memory obtained
drh17435752007-08-16 04:30:38 +0000292** from sqlite3_malloc(). As initially created, the Expr.token.z values
drh4b59ab52002-08-24 18:24:51 +0000293** all point to the input string that was fed to the parser. But that
danielk19776f8a5032004-05-10 10:34:51 +0000294** string is ephemeral - it will go away as soon as the sqlite3_exec()
drh4b59ab52002-08-24 18:24:51 +0000295** call that started the parser exits. This routine makes a persistent
296** copy of all the Expr.token.z strings so that the TriggerStep structure
danielk19776f8a5032004-05-10 10:34:51 +0000297** will be valid even after the sqlite3_exec() call returns.
drh4b59ab52002-08-24 18:24:51 +0000298*/
drh17435752007-08-16 04:30:38 +0000299static void sqlitePersistTriggerStep(sqlite3 *db, TriggerStep *p){
drh4b59ab52002-08-24 18:24:51 +0000300 if( p->target.z ){
drh17435752007-08-16 04:30:38 +0000301 p->target.z = (u8*)sqlite3DbStrNDup(db, (char*)p->target.z, p->target.n);
drh4b59ab52002-08-24 18:24:51 +0000302 p->target.dyn = 1;
303 }
304 if( p->pSelect ){
drh17435752007-08-16 04:30:38 +0000305 Select *pNew = sqlite3SelectDup(db, p->pSelect);
danielk19774adee202004-05-08 08:23:19 +0000306 sqlite3SelectDelete(p->pSelect);
drh4b59ab52002-08-24 18:24:51 +0000307 p->pSelect = pNew;
308 }
309 if( p->pWhere ){
drh17435752007-08-16 04:30:38 +0000310 Expr *pNew = sqlite3ExprDup(db, p->pWhere);
danielk19774adee202004-05-08 08:23:19 +0000311 sqlite3ExprDelete(p->pWhere);
drh4b59ab52002-08-24 18:24:51 +0000312 p->pWhere = pNew;
313 }
314 if( p->pExprList ){
drh17435752007-08-16 04:30:38 +0000315 ExprList *pNew = sqlite3ExprListDup(db, p->pExprList);
danielk19774adee202004-05-08 08:23:19 +0000316 sqlite3ExprListDelete(p->pExprList);
drh4b59ab52002-08-24 18:24:51 +0000317 p->pExprList = pNew;
318 }
319 if( p->pIdList ){
drh17435752007-08-16 04:30:38 +0000320 IdList *pNew = sqlite3IdListDup(db, p->pIdList);
danielk19774adee202004-05-08 08:23:19 +0000321 sqlite3IdListDelete(p->pIdList);
drh4b59ab52002-08-24 18:24:51 +0000322 p->pIdList = pNew;
danielk1977c3f9bad2002-05-15 08:30:12 +0000323 }
324}
325
drhc977f7f2002-05-21 11:38:11 +0000326/*
327** Turn a SELECT statement (that the pSelect parameter points to) into
328** a trigger step. Return a pointer to a TriggerStep structure.
329**
330** The parser calls this routine when it finds a SELECT statement in
331** body of a TRIGGER.
332*/
drh17435752007-08-16 04:30:38 +0000333TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
334 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000335 if( pTriggerStep==0 ) {
336 sqlite3SelectDelete(pSelect);
337 return 0;
338 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000339
danielk1977633ed082002-05-17 00:05:58 +0000340 pTriggerStep->op = TK_SELECT;
341 pTriggerStep->pSelect = pSelect;
342 pTriggerStep->orconf = OE_Default;
drh17435752007-08-16 04:30:38 +0000343 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977c3f9bad2002-05-15 08:30:12 +0000344
danielk1977633ed082002-05-17 00:05:58 +0000345 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000346}
347
drhc977f7f2002-05-21 11:38:11 +0000348/*
349** Build a trigger step out of an INSERT statement. Return a pointer
350** to the new trigger step.
351**
352** The parser calls this routine when it sees an INSERT inside the
353** body of a trigger.
354*/
danielk19774adee202004-05-08 08:23:19 +0000355TriggerStep *sqlite3TriggerInsertStep(
drh17435752007-08-16 04:30:38 +0000356 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000357 Token *pTableName, /* Name of the table into which we insert */
358 IdList *pColumn, /* List of columns in pTableName to insert into */
359 ExprList *pEList, /* The VALUE clause: a list of values to be inserted */
360 Select *pSelect, /* A SELECT statement that supplies values */
361 int orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
danielk1977633ed082002-05-17 00:05:58 +0000362){
drhf3a65f72007-08-22 20:18:21 +0000363 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000364
danielk1977633ed082002-05-17 00:05:58 +0000365 assert(pEList == 0 || pSelect == 0);
drhf3a65f72007-08-22 20:18:21 +0000366 assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000367
drhf3a65f72007-08-22 20:18:21 +0000368 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk1977d5d56522005-03-16 12:15:20 +0000369 if( pTriggerStep ){
370 pTriggerStep->op = TK_INSERT;
371 pTriggerStep->pSelect = pSelect;
372 pTriggerStep->target = *pTableName;
373 pTriggerStep->pIdList = pColumn;
374 pTriggerStep->pExprList = pEList;
375 pTriggerStep->orconf = orconf;
drh17435752007-08-16 04:30:38 +0000376 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977d5d56522005-03-16 12:15:20 +0000377 }else{
378 sqlite3IdListDelete(pColumn);
379 sqlite3ExprListDelete(pEList);
drh17435752007-08-16 04:30:38 +0000380 sqlite3SelectDelete(pSelect);
danielk1977d5d56522005-03-16 12:15:20 +0000381 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000382
danielk1977633ed082002-05-17 00:05:58 +0000383 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000384}
385
drhc977f7f2002-05-21 11:38:11 +0000386/*
387** Construct a trigger step that implements an UPDATE statement and return
388** a pointer to that trigger step. The parser calls this routine when it
389** sees an UPDATE statement inside the body of a CREATE TRIGGER.
390*/
danielk19774adee202004-05-08 08:23:19 +0000391TriggerStep *sqlite3TriggerUpdateStep(
drh17435752007-08-16 04:30:38 +0000392 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000393 Token *pTableName, /* Name of the table to be updated */
394 ExprList *pEList, /* The SET clause: list of column and new values */
395 Expr *pWhere, /* The WHERE clause */
396 int orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
397){
drh17435752007-08-16 04:30:38 +0000398 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
drh0e3a6f32007-03-30 20:40:34 +0000399 if( pTriggerStep==0 ){
400 sqlite3ExprListDelete(pEList);
401 sqlite3ExprDelete(pWhere);
402 return 0;
403 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000404
danielk1977633ed082002-05-17 00:05:58 +0000405 pTriggerStep->op = TK_UPDATE;
406 pTriggerStep->target = *pTableName;
407 pTriggerStep->pExprList = pEList;
408 pTriggerStep->pWhere = pWhere;
409 pTriggerStep->orconf = orconf;
drh17435752007-08-16 04:30:38 +0000410 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977c3f9bad2002-05-15 08:30:12 +0000411
danielk1977633ed082002-05-17 00:05:58 +0000412 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000413}
414
drhc977f7f2002-05-21 11:38:11 +0000415/*
416** Construct a trigger step that implements a DELETE statement and return
417** a pointer to that trigger step. The parser calls this routine when it
418** sees a DELETE statement inside the body of a CREATE TRIGGER.
419*/
drh17435752007-08-16 04:30:38 +0000420TriggerStep *sqlite3TriggerDeleteStep(
421 sqlite3 *db, /* Database connection */
422 Token *pTableName, /* The table from which rows are deleted */
423 Expr *pWhere /* The WHERE clause */
424){
425 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
drhb63a53d2007-03-31 01:34:44 +0000426 if( pTriggerStep==0 ){
427 sqlite3ExprDelete(pWhere);
428 return 0;
429 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000430
danielk1977633ed082002-05-17 00:05:58 +0000431 pTriggerStep->op = TK_DELETE;
432 pTriggerStep->target = *pTableName;
433 pTriggerStep->pWhere = pWhere;
434 pTriggerStep->orconf = OE_Default;
drh17435752007-08-16 04:30:38 +0000435 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977c3f9bad2002-05-15 08:30:12 +0000436
danielk1977633ed082002-05-17 00:05:58 +0000437 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000438}
439
danielk1977633ed082002-05-17 00:05:58 +0000440/*
441** Recursively delete a Trigger structure
442*/
danielk19774adee202004-05-08 08:23:19 +0000443void sqlite3DeleteTrigger(Trigger *pTrigger){
drhf0f258b2003-04-21 18:48:45 +0000444 if( pTrigger==0 ) return;
danielk19774adee202004-05-08 08:23:19 +0000445 sqlite3DeleteTriggerStep(pTrigger->step_list);
drh17435752007-08-16 04:30:38 +0000446 sqlite3_free(pTrigger->name);
447 sqlite3_free(pTrigger->table);
danielk19774adee202004-05-08 08:23:19 +0000448 sqlite3ExprDelete(pTrigger->pWhen);
449 sqlite3IdListDelete(pTrigger->pColumns);
drh17435752007-08-16 04:30:38 +0000450 if( pTrigger->nameToken.dyn ) sqlite3_free((char*)pTrigger->nameToken.z);
451 sqlite3_free(pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000452}
453
454/*
danielk19778a414492004-06-29 08:59:35 +0000455** This function is called to drop a trigger from the database schema.
456**
457** This may be called directly from the parser and therefore identifies
458** the trigger by name. The sqlite3DropTriggerPtr() routine does the
459** same job as this routine except it takes a pointer to the trigger
460** instead of the trigger name.
461**/
drhfdd48a72006-09-11 23:45:48 +0000462void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000463 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000464 int i;
465 const char *zDb;
466 const char *zName;
467 int nName;
drh9bb575f2004-09-06 17:24:11 +0000468 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000469
drh17435752007-08-16 04:30:38 +0000470 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000471 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000472 goto drop_trigger_cleanup;
473 }
danielk19778e227872004-06-07 07:52:17 +0000474
drhd24cc422003-03-27 12:51:24 +0000475 assert( pName->nSrc==1 );
476 zDb = pName->a[0].zDatabase;
477 zName = pName->a[0].zName;
478 nName = strlen(zName);
danielk197753c0f742005-03-29 03:10:59 +0000479 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000480 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000481 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
drhe4df0e72006-03-29 00:24:06 +0000482 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
drhd24cc422003-03-27 12:51:24 +0000483 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000484 }
drhd24cc422003-03-27 12:51:24 +0000485 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000486 if( !noErr ){
487 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
488 }
drhd24cc422003-03-27 12:51:24 +0000489 goto drop_trigger_cleanup;
490 }
drh74161702006-02-24 02:53:49 +0000491 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000492
493drop_trigger_cleanup:
danielk19774adee202004-05-08 08:23:19 +0000494 sqlite3SrcListDelete(pName);
drh79a519c2003-05-17 19:04:03 +0000495}
496
497/*
drh956bc922004-07-24 17:38:29 +0000498** Return a pointer to the Table structure for the table that a trigger
499** is set on.
500*/
drh74161702006-02-24 02:53:49 +0000501static Table *tableOfTrigger(Trigger *pTrigger){
danielk1977aaf22682006-01-06 15:03:48 +0000502 int n = strlen(pTrigger->table) + 1;
503 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
drh956bc922004-07-24 17:38:29 +0000504}
505
506
507/*
drh74161702006-02-24 02:53:49 +0000508** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000509*/
drh74161702006-02-24 02:53:49 +0000510void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000511 Table *pTable;
512 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000513 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000514 int iDb;
drh79a519c2003-05-17 19:04:03 +0000515
danielk1977da184232006-01-05 11:34:32 +0000516 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000517 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000518 pTable = tableOfTrigger(pTrigger);
drh43617e92006-03-06 20:55:46 +0000519 assert( pTable );
danielk1977da184232006-01-05 11:34:32 +0000520 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000521#ifndef SQLITE_OMIT_AUTHORIZATION
522 {
523 int code = SQLITE_DROP_TRIGGER;
drh956bc922004-07-24 17:38:29 +0000524 const char *zDb = db->aDb[iDb].zName;
525 const char *zTab = SCHEMA_TABLE(iDb);
526 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
danielk19774adee202004-05-08 08:23:19 +0000527 if( sqlite3AuthCheck(pParse, code, pTrigger->name, pTable->zName, zDb) ||
528 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000529 return;
drhe5f9c642003-01-13 23:27:31 +0000530 }
drhed6c8672003-01-12 18:02:16 +0000531 }
drhe5f9c642003-01-13 23:27:31 +0000532#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000533
drhf0f258b2003-04-21 18:48:45 +0000534 /* Generate code to destroy the database record of the trigger.
535 */
drh43617e92006-03-06 20:55:46 +0000536 assert( pTable!=0 );
537 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drhf0f258b2003-04-21 18:48:45 +0000538 int base;
drh57196282004-10-06 15:41:16 +0000539 static const VdbeOpList dropTrigger[] = {
drh9b1b01b2003-08-16 12:37:51 +0000540 { OP_Rewind, 0, ADDR(9), 0},
drh57196282004-10-06 15:41:16 +0000541 { OP_String8, 0, 0, 0}, /* 1 */
drhf0f258b2003-04-21 18:48:45 +0000542 { OP_Column, 0, 1, 0},
drh9b1b01b2003-08-16 12:37:51 +0000543 { OP_Ne, 0, ADDR(8), 0},
drh24003452008-01-03 01:28:59 +0000544 { OP_String8, 0, 0, 0}, /* 4: "trigger" */
drh9b1b01b2003-08-16 12:37:51 +0000545 { OP_Column, 0, 0, 0},
546 { OP_Ne, 0, ADDR(8), 0},
drhf0f258b2003-04-21 18:48:45 +0000547 { OP_Delete, 0, 0, 0},
drh9b1b01b2003-08-16 12:37:51 +0000548 { OP_Next, 0, ADDR(1), 0}, /* 8 */
drhf0f258b2003-04-21 18:48:45 +0000549 };
550
drh956bc922004-07-24 17:38:29 +0000551 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk1977c00da102006-01-07 13:21:04 +0000552 sqlite3OpenMasterTable(pParse, iDb);
danielk19774adee202004-05-08 08:23:19 +0000553 base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger);
drh66a51672008-01-03 00:01:23 +0000554 sqlite3VdbeChangeP4(v, base+1, pTrigger->name, 0);
drh24003452008-01-03 01:28:59 +0000555 sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
drh956bc922004-07-24 17:38:29 +0000556 sqlite3ChangeCookie(db, v, iDb);
drh66a51672008-01-03 00:01:23 +0000557 sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
558 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->name, 0);
drhf0f258b2003-04-21 18:48:45 +0000559 }
drh956bc922004-07-24 17:38:29 +0000560}
drhf0f258b2003-04-21 18:48:45 +0000561
drh956bc922004-07-24 17:38:29 +0000562/*
563** Remove a trigger from the hash tables of the sqlite* pointer.
564*/
565void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
566 Trigger *pTrigger;
567 int nName = strlen(zName);
drhe4df0e72006-03-29 00:24:06 +0000568 pTrigger = sqlite3HashInsert(&(db->aDb[iDb].pSchema->trigHash),
569 zName, nName, 0);
drh956bc922004-07-24 17:38:29 +0000570 if( pTrigger ){
drh74161702006-02-24 02:53:49 +0000571 Table *pTable = tableOfTrigger(pTrigger);
drh956bc922004-07-24 17:38:29 +0000572 assert( pTable!=0 );
danielk1977633ed082002-05-17 00:05:58 +0000573 if( pTable->pTrigger == pTrigger ){
574 pTable->pTrigger = pTrigger->pNext;
danielk1977f29ce552002-05-19 23:43:12 +0000575 }else{
danielk1977633ed082002-05-17 00:05:58 +0000576 Trigger *cc = pTable->pTrigger;
577 while( cc ){
578 if( cc->pNext == pTrigger ){
drh9adf9ac2002-05-15 11:44:13 +0000579 cc->pNext = cc->pNext->pNext;
580 break;
581 }
582 cc = cc->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +0000583 }
584 assert(cc);
585 }
danielk19774adee202004-05-08 08:23:19 +0000586 sqlite3DeleteTrigger(pTrigger);
drh956bc922004-07-24 17:38:29 +0000587 db->flags |= SQLITE_InternChanges;
danielk1977c3f9bad2002-05-15 08:30:12 +0000588 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000589}
590
drhc977f7f2002-05-21 11:38:11 +0000591/*
592** pEList is the SET clause of an UPDATE statement. Each entry
593** in pEList is of the format <id>=<expr>. If any of the entries
594** in pEList have an <id> which matches an identifier in pIdList,
595** then return TRUE. If pIdList==NULL, then it is considered a
596** wildcard that matches anything. Likewise if pEList==NULL then
597** it matches anything so always return true. Return false only
598** if there is no match.
599*/
600static int checkColumnOverLap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000601 int e;
602 if( !pIdList || !pEList ) return 1;
603 for(e=0; e<pEList->nExpr; e++){
danielk19774adee202004-05-08 08:23:19 +0000604 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000605 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000606 return 0;
607}
608
danielk1977c3f9bad2002-05-15 08:30:12 +0000609/*
drhdca76842004-12-07 14:06:13 +0000610** Return a bit vector to indicate what kind of triggers exist for operation
611** "op" on table pTab. If pChanges is not NULL then it is a list of columns
612** that are being updated. Triggers only match if the ON clause of the
613** trigger definition overlaps the set of columns being updated.
614**
615** The returned bit vector is some combination of TRIGGER_BEFORE and
616** TRIGGER_AFTER.
617*/
danielk19774adee202004-05-08 08:23:19 +0000618int sqlite3TriggersExist(
drhc977f7f2002-05-21 11:38:11 +0000619 Parse *pParse, /* Used to check for recursive triggers */
drhdca76842004-12-07 14:06:13 +0000620 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000621 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
drhc977f7f2002-05-21 11:38:11 +0000622 ExprList *pChanges /* Columns that change in an UPDATE statement */
623){
drh4cbdda92006-06-14 19:00:20 +0000624 Trigger *pTrigger;
drhdca76842004-12-07 14:06:13 +0000625 int mask = 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000626
drh4cbdda92006-06-14 19:00:20 +0000627 pTrigger = IsVirtual(pTab) ? 0 : pTab->pTrigger;
drhdca76842004-12-07 14:06:13 +0000628 while( pTrigger ){
629 if( pTrigger->op==op && checkColumnOverLap(pTrigger->pColumns, pChanges) ){
drh229caa32006-03-25 15:52:19 +0000630 mask |= pTrigger->tr_tm;
danielk1977c3f9bad2002-05-15 08:30:12 +0000631 }
drhdca76842004-12-07 14:06:13 +0000632 pTrigger = pTrigger->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +0000633 }
drhdca76842004-12-07 14:06:13 +0000634 return mask;
danielk1977c3f9bad2002-05-15 08:30:12 +0000635}
636
drhc977f7f2002-05-21 11:38:11 +0000637/*
drhf26e09c2003-05-31 16:21:12 +0000638** Convert the pStep->target token into a SrcList and return a pointer
639** to that SrcList.
640**
641** This routine adds a specific database name, if needed, to the target when
642** forming the SrcList. This prevents a trigger in one database from
643** referring to a target in another database. An exception is when the
644** trigger is in TEMP in which case it can refer to any other database it
645** wants.
646*/
647static SrcList *targetSrcList(
648 Parse *pParse, /* The parsing context */
649 TriggerStep *pStep /* The trigger containing the target token */
650){
651 Token sDb; /* Dummy database name token */
652 int iDb; /* Index of the database to use */
653 SrcList *pSrc; /* SrcList to be returned */
654
danielk1977da184232006-01-05 11:34:32 +0000655 iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
drhf26e09c2003-05-31 16:21:12 +0000656 if( iDb==0 || iDb>=2 ){
657 assert( iDb<pParse->db->nDb );
drh2646da72005-12-09 20:02:05 +0000658 sDb.z = (u8*)pParse->db->aDb[iDb].zName;
659 sDb.n = strlen((char*)sDb.z);
drh17435752007-08-16 04:30:38 +0000660 pSrc = sqlite3SrcListAppend(pParse->db, 0, &sDb, &pStep->target);
drhf26e09c2003-05-31 16:21:12 +0000661 } else {
drh17435752007-08-16 04:30:38 +0000662 pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
drhf26e09c2003-05-31 16:21:12 +0000663 }
664 return pSrc;
665}
666
667/*
drhc977f7f2002-05-21 11:38:11 +0000668** Generate VDBE code for zero or more statements inside the body of a
669** trigger.
670*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000671static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000672 Parse *pParse, /* The parser context */
673 TriggerStep *pStepList, /* List of statements inside the trigger body */
674 int orconfin /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000675){
676 TriggerStep * pTriggerStep = pStepList;
677 int orconf;
drh344737f2004-09-19 00:50:20 +0000678 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000679 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000680
drh344737f2004-09-19 00:50:20 +0000681 assert( pTriggerStep!=0 );
682 assert( v!=0 );
drh66a51672008-01-03 00:01:23 +0000683 sqlite3VdbeAddOp2(v, OP_ContextPush, 0, 0);
drhd4e70eb2008-01-02 00:34:36 +0000684 VdbeComment((v, "begin trigger %s", pStepList->pTrig->name));
danielk1977633ed082002-05-17 00:05:58 +0000685 while( pTriggerStep ){
danielk1977633ed082002-05-17 00:05:58 +0000686 orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin;
687 pParse->trigStack->orconf = orconf;
688 switch( pTriggerStep->op ){
689 case TK_SELECT: {
drh17435752007-08-16 04:30:38 +0000690 Select *ss = sqlite3SelectDup(db, pTriggerStep->pSelect);
drh28f45912006-10-18 23:26:38 +0000691 if( ss ){
danielk19776c8c8ce2008-01-02 16:27:09 +0000692 SelectDest dest = {SRT_Discard, 0, 0};
drh28f45912006-10-18 23:26:38 +0000693 sqlite3SelectResolve(pParse, ss, 0);
danielk19776c8c8ce2008-01-02 16:27:09 +0000694 sqlite3Select(pParse, ss, &dest, 0, 0, 0, 0);
drh28f45912006-10-18 23:26:38 +0000695 sqlite3SelectDelete(ss);
696 }
drhfd131da2007-08-07 17:13:03 +0000697 break;
danielk1977633ed082002-05-17 00:05:58 +0000698 }
699 case TK_UPDATE: {
drh113088e2003-03-20 01:16:58 +0000700 SrcList *pSrc;
drhf26e09c2003-05-31 16:21:12 +0000701 pSrc = targetSrcList(pParse, pTriggerStep);
drh66a51672008-01-03 00:01:23 +0000702 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000703 sqlite3Update(pParse, pSrc,
drh17435752007-08-16 04:30:38 +0000704 sqlite3ExprListDup(db, pTriggerStep->pExprList),
705 sqlite3ExprDup(db, pTriggerStep->pWhere), orconf);
drh66a51672008-01-03 00:01:23 +0000706 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000707 break;
708 }
709 case TK_INSERT: {
drh113088e2003-03-20 01:16:58 +0000710 SrcList *pSrc;
drhf26e09c2003-05-31 16:21:12 +0000711 pSrc = targetSrcList(pParse, pTriggerStep);
drh66a51672008-01-03 00:01:23 +0000712 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000713 sqlite3Insert(pParse, pSrc,
drh17435752007-08-16 04:30:38 +0000714 sqlite3ExprListDup(db, pTriggerStep->pExprList),
715 sqlite3SelectDup(db, pTriggerStep->pSelect),
716 sqlite3IdListDup(db, pTriggerStep->pIdList), orconf);
drh66a51672008-01-03 00:01:23 +0000717 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000718 break;
719 }
720 case TK_DELETE: {
drh113088e2003-03-20 01:16:58 +0000721 SrcList *pSrc;
drh66a51672008-01-03 00:01:23 +0000722 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
drhf26e09c2003-05-31 16:21:12 +0000723 pSrc = targetSrcList(pParse, pTriggerStep);
drh17435752007-08-16 04:30:38 +0000724 sqlite3DeleteFrom(pParse, pSrc,
725 sqlite3ExprDup(db, pTriggerStep->pWhere));
drh66a51672008-01-03 00:01:23 +0000726 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000727 break;
728 }
729 default:
730 assert(0);
731 }
danielk1977633ed082002-05-17 00:05:58 +0000732 pTriggerStep = pTriggerStep->pNext;
733 }
drh66a51672008-01-03 00:01:23 +0000734 sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
drhd4e70eb2008-01-02 00:34:36 +0000735 VdbeComment((v, "end trigger %s", pStepList->pTrig->name));
danielk1977c3f9bad2002-05-15 08:30:12 +0000736
danielk1977633ed082002-05-17 00:05:58 +0000737 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000738}
739
danielk1977633ed082002-05-17 00:05:58 +0000740/*
741** This is called to code FOR EACH ROW triggers.
742**
743** When the code that this function generates is executed, the following
744** must be true:
drhc977f7f2002-05-21 11:38:11 +0000745**
746** 1. No cursors may be open in the main database. (But newIdx and oldIdx
747** can be indices of cursors in temporary tables. See below.)
748**
danielk1977633ed082002-05-17 00:05:58 +0000749** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then
750** a temporary vdbe cursor (index newIdx) must be open and pointing at
751** a row containing values to be substituted for new.* expressions in the
752** trigger program(s).
drhc977f7f2002-05-21 11:38:11 +0000753**
danielk1977633ed082002-05-17 00:05:58 +0000754** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then
755** a temporary vdbe cursor (index oldIdx) must be open and pointing at
756** a row containing values to be substituted for old.* expressions in the
757** trigger program(s).
758**
danielk19778f2c54e2008-01-01 19:02:09 +0000759** If they are not NULL, the piOldColMask and piNewColMask output variables
760** are set to values that describe the columns used by the trigger program
761** in the OLD.* and NEW.* tables respectively. If column N of the
762** pseudo-table is read at least once, the corresponding bit of the output
763** mask is set. If a column with an index greater than 32 is read, the
764** output mask is set to the special value 0xffffffff.
765**
danielk1977633ed082002-05-17 00:05:58 +0000766*/
danielk19774adee202004-05-08 08:23:19 +0000767int sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +0000768 Parse *pParse, /* Parse context */
769 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
770 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +0000771 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +0000772 Table *pTab, /* The table to code triggers from */
773 int newIdx, /* The indice of the "new" row to access */
774 int oldIdx, /* The indice of the "old" row to access */
danielk19776f349032002-06-11 02:25:40 +0000775 int orconf, /* ON CONFLICT policy */
danielk19778f2c54e2008-01-01 19:02:09 +0000776 int ignoreJump, /* Instruction to jump to for RAISE(IGNORE) */
777 u32 *piOldColMask, /* OUT: Mask of columns used from the OLD.* table */
778 u32 *piNewColMask /* OUT: Mask of columns used from the NEW.* table */
drhc977f7f2002-05-21 11:38:11 +0000779){
danielk1977eecfb3e2006-01-10 12:31:39 +0000780 Trigger *p;
drh67040462004-09-24 12:24:36 +0000781 TriggerStack trigStackEntry;
danielk1977c3f9bad2002-05-15 08:30:12 +0000782
danielk19778f2c54e2008-01-01 19:02:09 +0000783 trigStackEntry.oldColMask = 0;
784 trigStackEntry.newColMask = 0;
785
danielk1977c3f9bad2002-05-15 08:30:12 +0000786 assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE);
drhdca76842004-12-07 14:06:13 +0000787 assert(tr_tm == TRIGGER_BEFORE || tr_tm == TRIGGER_AFTER );
danielk1977c3f9bad2002-05-15 08:30:12 +0000788
danielk1977633ed082002-05-17 00:05:58 +0000789 assert(newIdx != -1 || oldIdx != -1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000790
danielk1977eecfb3e2006-01-10 12:31:39 +0000791 for(p=pTab->pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +0000792 int fire_this = 0;
danielk1977932083c2007-11-16 14:55:46 +0000793 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000794
danielk1977eecfb3e2006-01-10 12:31:39 +0000795 /* Determine whether we should code this trigger */
796 if(
797 p->op==op &&
798 p->tr_tm==tr_tm &&
danielk1977932083c2007-11-16 14:55:46 +0000799 (p->pSchema==p->pTabSchema || p->pSchema==db->aDb[1].pSchema) &&
danielk1977eecfb3e2006-01-10 12:31:39 +0000800 (op!=TK_UPDATE||!p->pColumns||checkColumnOverLap(p->pColumns,pChanges))
801 ){
802 TriggerStack *pS; /* Pointer to trigger-stack entry */
drh74161702006-02-24 02:53:49 +0000803 for(pS=pParse->trigStack; pS && p!=pS->pTrigger; pS=pS->pNext){}
danielk1977eecfb3e2006-01-10 12:31:39 +0000804 if( !pS ){
805 fire_this = 1;
danielk1977f29ce552002-05-19 23:43:12 +0000806 }
drh229caa32006-03-25 15:52:19 +0000807#if 0 /* Give no warning for recursive triggers. Just do not do them */
808 else{
809 sqlite3ErrorMsg(pParse, "recursive triggers not supported (%s)",
810 p->name);
811 return SQLITE_ERROR;
812 }
813#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000814 }
drhad6d9462004-09-19 02:15:24 +0000815
drh67040462004-09-24 12:24:36 +0000816 if( fire_this ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000817 int endTrigger;
danielk1977c3f9bad2002-05-15 08:30:12 +0000818 Expr * whenExpr;
drh85e20962003-04-25 17:52:11 +0000819 AuthContext sContext;
danielk1977b3bce662005-01-29 08:32:43 +0000820 NameContext sNC;
danielk1977c3f9bad2002-05-15 08:30:12 +0000821
danielk1977b3bce662005-01-29 08:32:43 +0000822 memset(&sNC, 0, sizeof(sNC));
823 sNC.pParse = pParse;
danielk1977c3f9bad2002-05-15 08:30:12 +0000824
825 /* Push an entry on to the trigger stack */
danielk1977eecfb3e2006-01-10 12:31:39 +0000826 trigStackEntry.pTrigger = p;
drh67040462004-09-24 12:24:36 +0000827 trigStackEntry.newIdx = newIdx;
828 trigStackEntry.oldIdx = oldIdx;
829 trigStackEntry.pTab = pTab;
830 trigStackEntry.pNext = pParse->trigStack;
831 trigStackEntry.ignoreJump = ignoreJump;
832 pParse->trigStack = &trigStackEntry;
danielk1977eecfb3e2006-01-10 12:31:39 +0000833 sqlite3AuthContextPush(pParse, &sContext, p->name);
danielk1977c3f9bad2002-05-15 08:30:12 +0000834
835 /* code the WHEN clause */
danielk19774adee202004-05-08 08:23:19 +0000836 endTrigger = sqlite3VdbeMakeLabel(pParse->pVdbe);
danielk1977932083c2007-11-16 14:55:46 +0000837 whenExpr = sqlite3ExprDup(db, p->pWhen);
838 if( db->mallocFailed || sqlite3ExprResolveNames(&sNC, whenExpr) ){
drh67040462004-09-24 12:24:36 +0000839 pParse->trigStack = trigStackEntry.pNext;
danielk19774adee202004-05-08 08:23:19 +0000840 sqlite3ExprDelete(whenExpr);
drh9adf9ac2002-05-15 11:44:13 +0000841 return 1;
danielk1977c3f9bad2002-05-15 08:30:12 +0000842 }
danielk19774adee202004-05-08 08:23:19 +0000843 sqlite3ExprIfFalse(pParse, whenExpr, endTrigger, 1);
844 sqlite3ExprDelete(whenExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000845
danielk1977eecfb3e2006-01-10 12:31:39 +0000846 codeTriggerProgram(pParse, p->step_list, orconf);
danielk1977c3f9bad2002-05-15 08:30:12 +0000847
848 /* Pop the entry off the trigger stack */
drh67040462004-09-24 12:24:36 +0000849 pParse->trigStack = trigStackEntry.pNext;
danielk19774adee202004-05-08 08:23:19 +0000850 sqlite3AuthContextPop(&sContext);
danielk1977c3f9bad2002-05-15 08:30:12 +0000851
danielk19774adee202004-05-08 08:23:19 +0000852 sqlite3VdbeResolveLabel(pParse->pVdbe, endTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000853 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000854 }
danielk19778f2c54e2008-01-01 19:02:09 +0000855 if( piOldColMask ) *piOldColMask |= trigStackEntry.oldColMask;
856 if( piNewColMask ) *piNewColMask |= trigStackEntry.newColMask;
danielk1977c3f9bad2002-05-15 08:30:12 +0000857 return 0;
858}
drhb7f91642004-10-31 02:22:47 +0000859#endif /* !defined(SQLITE_OMIT_TRIGGER) */