blob: 9fe781f008d6662587afd13fc8c683d3cfcdf37e [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" */
drhb1fdb2a2008-01-05 04:06:03 +0000240 { OP_Move, 0, 0, 0 }, /* 9: Store data */
241 { OP_Move, 0, 0, 0 }, /* 10: Store key */
drhf0863fe2005-06-12 21:35:51 +0000242 { OP_Insert, 0, 0, 0 },
drhc977f7f2002-05-21 11:38:11 +0000243 };
244 int addr;
245 Vdbe *v;
drh0a07c102008-01-03 18:03:08 +0000246 int iKey = ++pParse->nMem;
247 int iData = ++pParse->nMem;
danielk1977c3f9bad2002-05-15 08:30:12 +0000248
249 /* Make an entry in the sqlite_master table */
danielk19774adee202004-05-08 08:23:19 +0000250 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000251 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000252 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk1977c00da102006-01-07 13:21:04 +0000253 sqlite3OpenMasterTable(pParse, iDb);
danielk19774adee202004-05-08 08:23:19 +0000254 addr = sqlite3VdbeAddOpList(v, ArraySize(insertTrig), insertTrig);
drh24003452008-01-03 01:28:59 +0000255 sqlite3VdbeChangeP4(v, addr+1, "trigger", P4_STATIC);
drh66a51672008-01-03 00:01:23 +0000256 sqlite3VdbeChangeP4(v, addr+2, pTrig->name, 0);
drh24003452008-01-03 01:28:59 +0000257 sqlite3VdbeChangeP4(v, addr+3, pTrig->table, 0);
258 sqlite3VdbeChangeP4(v, addr+5, "CREATE TRIGGER ", P4_STATIC);
drh66a51672008-01-03 00:01:23 +0000259 sqlite3VdbeChangeP4(v, addr+6, (char*)pAll->z, pAll->n);
drh24003452008-01-03 01:28:59 +0000260 sqlite3VdbeChangeP4(v, addr+8, "aaada", P4_STATIC);
drhb1fdb2a2008-01-05 04:06:03 +0000261 sqlite3VdbeChangeP2(v, addr+9, iData);
262 sqlite3VdbeChangeP2(v, addr+10, iKey);
danielk19771f4aa332008-01-03 09:51:55 +0000263 sqlite3VdbeChangeP2(v, addr+11, iData);
danielk19771f4aa332008-01-03 09:51:55 +0000264 sqlite3VdbeChangeP3(v, addr+11, iKey);
danielk1977da184232006-01-05 11:34:32 +0000265 sqlite3ChangeCookie(db, v, iDb);
drh66a51672008-01-03 00:01:23 +0000266 sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
267 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, sqlite3MPrintf(
268 db, "type='trigger' AND name='%q'", pTrig->name), P4_DYNAMIC
danielk19771e536952007-08-16 10:09:01 +0000269 );
danielk1977c3f9bad2002-05-15 08:30:12 +0000270 }
271
drh956bc922004-07-24 17:38:29 +0000272 if( db->init.busy ){
danielk1977aaf22682006-01-06 15:03:48 +0000273 int n;
drhf0f258b2003-04-21 18:48:45 +0000274 Table *pTab;
danielk1977d5d56522005-03-16 12:15:20 +0000275 Trigger *pDel;
danielk1977da184232006-01-05 11:34:32 +0000276 pDel = sqlite3HashInsert(&db->aDb[iDb].pSchema->trigHash,
drhe4df0e72006-03-29 00:24:06 +0000277 pTrig->name, strlen(pTrig->name), pTrig);
danielk1977d5d56522005-03-16 12:15:20 +0000278 if( pDel ){
drhf3a65f72007-08-22 20:18:21 +0000279 assert( pDel==pTrig );
280 db->mallocFailed = 1;
danielk1977d5d56522005-03-16 12:15:20 +0000281 goto triggerfinish_cleanup;
282 }
danielk1977aaf22682006-01-06 15:03:48 +0000283 n = strlen(pTrig->table) + 1;
284 pTab = sqlite3HashFind(&pTrig->pTabSchema->tblHash, pTrig->table, n);
drhf0f258b2003-04-21 18:48:45 +0000285 assert( pTab!=0 );
danielk1977bfb9e352005-01-24 13:03:32 +0000286 pTrig->pNext = pTab->pTrigger;
287 pTab->pTrigger = pTrig;
288 pTrig = 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000289 }
290
drhf0f258b2003-04-21 18:48:45 +0000291triggerfinish_cleanup:
danielk1977bfb9e352005-01-24 13:03:32 +0000292 sqlite3DeleteTrigger(pTrig);
293 assert( !pParse->pNewTrigger );
danielk19774adee202004-05-08 08:23:19 +0000294 sqlite3DeleteTriggerStep(pStepList);
drh4b59ab52002-08-24 18:24:51 +0000295}
danielk1977c3f9bad2002-05-15 08:30:12 +0000296
drh4b59ab52002-08-24 18:24:51 +0000297/*
298** Make a copy of all components of the given trigger step. This has
299** the effect of copying all Expr.token.z values into memory obtained
drh17435752007-08-16 04:30:38 +0000300** from sqlite3_malloc(). As initially created, the Expr.token.z values
drh4b59ab52002-08-24 18:24:51 +0000301** all point to the input string that was fed to the parser. But that
danielk19776f8a5032004-05-10 10:34:51 +0000302** string is ephemeral - it will go away as soon as the sqlite3_exec()
drh4b59ab52002-08-24 18:24:51 +0000303** call that started the parser exits. This routine makes a persistent
304** copy of all the Expr.token.z strings so that the TriggerStep structure
danielk19776f8a5032004-05-10 10:34:51 +0000305** will be valid even after the sqlite3_exec() call returns.
drh4b59ab52002-08-24 18:24:51 +0000306*/
drh17435752007-08-16 04:30:38 +0000307static void sqlitePersistTriggerStep(sqlite3 *db, TriggerStep *p){
drh4b59ab52002-08-24 18:24:51 +0000308 if( p->target.z ){
drh17435752007-08-16 04:30:38 +0000309 p->target.z = (u8*)sqlite3DbStrNDup(db, (char*)p->target.z, p->target.n);
drh4b59ab52002-08-24 18:24:51 +0000310 p->target.dyn = 1;
311 }
312 if( p->pSelect ){
drh17435752007-08-16 04:30:38 +0000313 Select *pNew = sqlite3SelectDup(db, p->pSelect);
danielk19774adee202004-05-08 08:23:19 +0000314 sqlite3SelectDelete(p->pSelect);
drh4b59ab52002-08-24 18:24:51 +0000315 p->pSelect = pNew;
316 }
317 if( p->pWhere ){
drh17435752007-08-16 04:30:38 +0000318 Expr *pNew = sqlite3ExprDup(db, p->pWhere);
danielk19774adee202004-05-08 08:23:19 +0000319 sqlite3ExprDelete(p->pWhere);
drh4b59ab52002-08-24 18:24:51 +0000320 p->pWhere = pNew;
321 }
322 if( p->pExprList ){
drh17435752007-08-16 04:30:38 +0000323 ExprList *pNew = sqlite3ExprListDup(db, p->pExprList);
danielk19774adee202004-05-08 08:23:19 +0000324 sqlite3ExprListDelete(p->pExprList);
drh4b59ab52002-08-24 18:24:51 +0000325 p->pExprList = pNew;
326 }
327 if( p->pIdList ){
drh17435752007-08-16 04:30:38 +0000328 IdList *pNew = sqlite3IdListDup(db, p->pIdList);
danielk19774adee202004-05-08 08:23:19 +0000329 sqlite3IdListDelete(p->pIdList);
drh4b59ab52002-08-24 18:24:51 +0000330 p->pIdList = pNew;
danielk1977c3f9bad2002-05-15 08:30:12 +0000331 }
332}
333
drhc977f7f2002-05-21 11:38:11 +0000334/*
335** Turn a SELECT statement (that the pSelect parameter points to) into
336** a trigger step. Return a pointer to a TriggerStep structure.
337**
338** The parser calls this routine when it finds a SELECT statement in
339** body of a TRIGGER.
340*/
drh17435752007-08-16 04:30:38 +0000341TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
342 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000343 if( pTriggerStep==0 ) {
344 sqlite3SelectDelete(pSelect);
345 return 0;
346 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000347
danielk1977633ed082002-05-17 00:05:58 +0000348 pTriggerStep->op = TK_SELECT;
349 pTriggerStep->pSelect = pSelect;
350 pTriggerStep->orconf = OE_Default;
drh17435752007-08-16 04:30:38 +0000351 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977c3f9bad2002-05-15 08:30:12 +0000352
danielk1977633ed082002-05-17 00:05:58 +0000353 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000354}
355
drhc977f7f2002-05-21 11:38:11 +0000356/*
357** Build a trigger step out of an INSERT statement. Return a pointer
358** to the new trigger step.
359**
360** The parser calls this routine when it sees an INSERT inside the
361** body of a trigger.
362*/
danielk19774adee202004-05-08 08:23:19 +0000363TriggerStep *sqlite3TriggerInsertStep(
drh17435752007-08-16 04:30:38 +0000364 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000365 Token *pTableName, /* Name of the table into which we insert */
366 IdList *pColumn, /* List of columns in pTableName to insert into */
367 ExprList *pEList, /* The VALUE clause: a list of values to be inserted */
368 Select *pSelect, /* A SELECT statement that supplies values */
369 int orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
danielk1977633ed082002-05-17 00:05:58 +0000370){
drhf3a65f72007-08-22 20:18:21 +0000371 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000372
danielk1977633ed082002-05-17 00:05:58 +0000373 assert(pEList == 0 || pSelect == 0);
drhf3a65f72007-08-22 20:18:21 +0000374 assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000375
drhf3a65f72007-08-22 20:18:21 +0000376 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk1977d5d56522005-03-16 12:15:20 +0000377 if( pTriggerStep ){
378 pTriggerStep->op = TK_INSERT;
379 pTriggerStep->pSelect = pSelect;
380 pTriggerStep->target = *pTableName;
381 pTriggerStep->pIdList = pColumn;
382 pTriggerStep->pExprList = pEList;
383 pTriggerStep->orconf = orconf;
drh17435752007-08-16 04:30:38 +0000384 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977d5d56522005-03-16 12:15:20 +0000385 }else{
386 sqlite3IdListDelete(pColumn);
387 sqlite3ExprListDelete(pEList);
drh17435752007-08-16 04:30:38 +0000388 sqlite3SelectDelete(pSelect);
danielk1977d5d56522005-03-16 12:15:20 +0000389 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000390
danielk1977633ed082002-05-17 00:05:58 +0000391 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000392}
393
drhc977f7f2002-05-21 11:38:11 +0000394/*
395** Construct a trigger step that implements an UPDATE statement and return
396** a pointer to that trigger step. The parser calls this routine when it
397** sees an UPDATE statement inside the body of a CREATE TRIGGER.
398*/
danielk19774adee202004-05-08 08:23:19 +0000399TriggerStep *sqlite3TriggerUpdateStep(
drh17435752007-08-16 04:30:38 +0000400 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000401 Token *pTableName, /* Name of the table to be updated */
402 ExprList *pEList, /* The SET clause: list of column and new values */
403 Expr *pWhere, /* The WHERE clause */
404 int orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
405){
drh17435752007-08-16 04:30:38 +0000406 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
drh0e3a6f32007-03-30 20:40:34 +0000407 if( pTriggerStep==0 ){
408 sqlite3ExprListDelete(pEList);
409 sqlite3ExprDelete(pWhere);
410 return 0;
411 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000412
danielk1977633ed082002-05-17 00:05:58 +0000413 pTriggerStep->op = TK_UPDATE;
414 pTriggerStep->target = *pTableName;
415 pTriggerStep->pExprList = pEList;
416 pTriggerStep->pWhere = pWhere;
417 pTriggerStep->orconf = orconf;
drh17435752007-08-16 04:30:38 +0000418 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977c3f9bad2002-05-15 08:30:12 +0000419
danielk1977633ed082002-05-17 00:05:58 +0000420 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000421}
422
drhc977f7f2002-05-21 11:38:11 +0000423/*
424** Construct a trigger step that implements a DELETE statement and return
425** a pointer to that trigger step. The parser calls this routine when it
426** sees a DELETE statement inside the body of a CREATE TRIGGER.
427*/
drh17435752007-08-16 04:30:38 +0000428TriggerStep *sqlite3TriggerDeleteStep(
429 sqlite3 *db, /* Database connection */
430 Token *pTableName, /* The table from which rows are deleted */
431 Expr *pWhere /* The WHERE clause */
432){
433 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
drhb63a53d2007-03-31 01:34:44 +0000434 if( pTriggerStep==0 ){
435 sqlite3ExprDelete(pWhere);
436 return 0;
437 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000438
danielk1977633ed082002-05-17 00:05:58 +0000439 pTriggerStep->op = TK_DELETE;
440 pTriggerStep->target = *pTableName;
441 pTriggerStep->pWhere = pWhere;
442 pTriggerStep->orconf = OE_Default;
drh17435752007-08-16 04:30:38 +0000443 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977c3f9bad2002-05-15 08:30:12 +0000444
danielk1977633ed082002-05-17 00:05:58 +0000445 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000446}
447
danielk1977633ed082002-05-17 00:05:58 +0000448/*
449** Recursively delete a Trigger structure
450*/
danielk19774adee202004-05-08 08:23:19 +0000451void sqlite3DeleteTrigger(Trigger *pTrigger){
drhf0f258b2003-04-21 18:48:45 +0000452 if( pTrigger==0 ) return;
danielk19774adee202004-05-08 08:23:19 +0000453 sqlite3DeleteTriggerStep(pTrigger->step_list);
drh17435752007-08-16 04:30:38 +0000454 sqlite3_free(pTrigger->name);
455 sqlite3_free(pTrigger->table);
danielk19774adee202004-05-08 08:23:19 +0000456 sqlite3ExprDelete(pTrigger->pWhen);
457 sqlite3IdListDelete(pTrigger->pColumns);
drh17435752007-08-16 04:30:38 +0000458 if( pTrigger->nameToken.dyn ) sqlite3_free((char*)pTrigger->nameToken.z);
459 sqlite3_free(pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000460}
461
462/*
danielk19778a414492004-06-29 08:59:35 +0000463** This function is called to drop a trigger from the database schema.
464**
465** This may be called directly from the parser and therefore identifies
466** the trigger by name. The sqlite3DropTriggerPtr() routine does the
467** same job as this routine except it takes a pointer to the trigger
468** instead of the trigger name.
469**/
drhfdd48a72006-09-11 23:45:48 +0000470void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000471 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000472 int i;
473 const char *zDb;
474 const char *zName;
475 int nName;
drh9bb575f2004-09-06 17:24:11 +0000476 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000477
drh17435752007-08-16 04:30:38 +0000478 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000479 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000480 goto drop_trigger_cleanup;
481 }
danielk19778e227872004-06-07 07:52:17 +0000482
drhd24cc422003-03-27 12:51:24 +0000483 assert( pName->nSrc==1 );
484 zDb = pName->a[0].zDatabase;
485 zName = pName->a[0].zName;
486 nName = strlen(zName);
danielk197753c0f742005-03-29 03:10:59 +0000487 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000488 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000489 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
drhe4df0e72006-03-29 00:24:06 +0000490 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
drhd24cc422003-03-27 12:51:24 +0000491 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000492 }
drhd24cc422003-03-27 12:51:24 +0000493 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000494 if( !noErr ){
495 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
496 }
drhd24cc422003-03-27 12:51:24 +0000497 goto drop_trigger_cleanup;
498 }
drh74161702006-02-24 02:53:49 +0000499 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000500
501drop_trigger_cleanup:
danielk19774adee202004-05-08 08:23:19 +0000502 sqlite3SrcListDelete(pName);
drh79a519c2003-05-17 19:04:03 +0000503}
504
505/*
drh956bc922004-07-24 17:38:29 +0000506** Return a pointer to the Table structure for the table that a trigger
507** is set on.
508*/
drh74161702006-02-24 02:53:49 +0000509static Table *tableOfTrigger(Trigger *pTrigger){
danielk1977aaf22682006-01-06 15:03:48 +0000510 int n = strlen(pTrigger->table) + 1;
511 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
drh956bc922004-07-24 17:38:29 +0000512}
513
514
515/*
drh74161702006-02-24 02:53:49 +0000516** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000517*/
drh74161702006-02-24 02:53:49 +0000518void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000519 Table *pTable;
520 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000521 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000522 int iDb;
drh79a519c2003-05-17 19:04:03 +0000523
danielk1977da184232006-01-05 11:34:32 +0000524 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000525 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000526 pTable = tableOfTrigger(pTrigger);
drh43617e92006-03-06 20:55:46 +0000527 assert( pTable );
danielk1977da184232006-01-05 11:34:32 +0000528 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000529#ifndef SQLITE_OMIT_AUTHORIZATION
530 {
531 int code = SQLITE_DROP_TRIGGER;
drh956bc922004-07-24 17:38:29 +0000532 const char *zDb = db->aDb[iDb].zName;
533 const char *zTab = SCHEMA_TABLE(iDb);
534 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
danielk19774adee202004-05-08 08:23:19 +0000535 if( sqlite3AuthCheck(pParse, code, pTrigger->name, pTable->zName, zDb) ||
536 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000537 return;
drhe5f9c642003-01-13 23:27:31 +0000538 }
drhed6c8672003-01-12 18:02:16 +0000539 }
drhe5f9c642003-01-13 23:27:31 +0000540#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000541
drhf0f258b2003-04-21 18:48:45 +0000542 /* Generate code to destroy the database record of the trigger.
543 */
drh43617e92006-03-06 20:55:46 +0000544 assert( pTable!=0 );
545 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drhf0f258b2003-04-21 18:48:45 +0000546 int base;
drh57196282004-10-06 15:41:16 +0000547 static const VdbeOpList dropTrigger[] = {
drh9b1b01b2003-08-16 12:37:51 +0000548 { OP_Rewind, 0, ADDR(9), 0},
drh57196282004-10-06 15:41:16 +0000549 { OP_String8, 0, 0, 0}, /* 1 */
drhf0f258b2003-04-21 18:48:45 +0000550 { OP_Column, 0, 1, 0},
drh9b1b01b2003-08-16 12:37:51 +0000551 { OP_Ne, 0, ADDR(8), 0},
drh24003452008-01-03 01:28:59 +0000552 { OP_String8, 0, 0, 0}, /* 4: "trigger" */
drh9b1b01b2003-08-16 12:37:51 +0000553 { OP_Column, 0, 0, 0},
554 { OP_Ne, 0, ADDR(8), 0},
drhf0f258b2003-04-21 18:48:45 +0000555 { OP_Delete, 0, 0, 0},
drh9b1b01b2003-08-16 12:37:51 +0000556 { OP_Next, 0, ADDR(1), 0}, /* 8 */
drhf0f258b2003-04-21 18:48:45 +0000557 };
558
drh956bc922004-07-24 17:38:29 +0000559 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk1977c00da102006-01-07 13:21:04 +0000560 sqlite3OpenMasterTable(pParse, iDb);
danielk19774adee202004-05-08 08:23:19 +0000561 base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger);
drh66a51672008-01-03 00:01:23 +0000562 sqlite3VdbeChangeP4(v, base+1, pTrigger->name, 0);
drh24003452008-01-03 01:28:59 +0000563 sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
drh956bc922004-07-24 17:38:29 +0000564 sqlite3ChangeCookie(db, v, iDb);
drh66a51672008-01-03 00:01:23 +0000565 sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
566 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->name, 0);
drhf0f258b2003-04-21 18:48:45 +0000567 }
drh956bc922004-07-24 17:38:29 +0000568}
drhf0f258b2003-04-21 18:48:45 +0000569
drh956bc922004-07-24 17:38:29 +0000570/*
571** Remove a trigger from the hash tables of the sqlite* pointer.
572*/
573void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
574 Trigger *pTrigger;
575 int nName = strlen(zName);
drhe4df0e72006-03-29 00:24:06 +0000576 pTrigger = sqlite3HashInsert(&(db->aDb[iDb].pSchema->trigHash),
577 zName, nName, 0);
drh956bc922004-07-24 17:38:29 +0000578 if( pTrigger ){
drh74161702006-02-24 02:53:49 +0000579 Table *pTable = tableOfTrigger(pTrigger);
drh956bc922004-07-24 17:38:29 +0000580 assert( pTable!=0 );
danielk1977633ed082002-05-17 00:05:58 +0000581 if( pTable->pTrigger == pTrigger ){
582 pTable->pTrigger = pTrigger->pNext;
danielk1977f29ce552002-05-19 23:43:12 +0000583 }else{
danielk1977633ed082002-05-17 00:05:58 +0000584 Trigger *cc = pTable->pTrigger;
585 while( cc ){
586 if( cc->pNext == pTrigger ){
drh9adf9ac2002-05-15 11:44:13 +0000587 cc->pNext = cc->pNext->pNext;
588 break;
589 }
590 cc = cc->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +0000591 }
592 assert(cc);
593 }
danielk19774adee202004-05-08 08:23:19 +0000594 sqlite3DeleteTrigger(pTrigger);
drh956bc922004-07-24 17:38:29 +0000595 db->flags |= SQLITE_InternChanges;
danielk1977c3f9bad2002-05-15 08:30:12 +0000596 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000597}
598
drhc977f7f2002-05-21 11:38:11 +0000599/*
600** pEList is the SET clause of an UPDATE statement. Each entry
601** in pEList is of the format <id>=<expr>. If any of the entries
602** in pEList have an <id> which matches an identifier in pIdList,
603** then return TRUE. If pIdList==NULL, then it is considered a
604** wildcard that matches anything. Likewise if pEList==NULL then
605** it matches anything so always return true. Return false only
606** if there is no match.
607*/
608static int checkColumnOverLap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000609 int e;
610 if( !pIdList || !pEList ) return 1;
611 for(e=0; e<pEList->nExpr; e++){
danielk19774adee202004-05-08 08:23:19 +0000612 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000613 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000614 return 0;
615}
616
danielk1977c3f9bad2002-05-15 08:30:12 +0000617/*
drhdca76842004-12-07 14:06:13 +0000618** Return a bit vector to indicate what kind of triggers exist for operation
619** "op" on table pTab. If pChanges is not NULL then it is a list of columns
620** that are being updated. Triggers only match if the ON clause of the
621** trigger definition overlaps the set of columns being updated.
622**
623** The returned bit vector is some combination of TRIGGER_BEFORE and
624** TRIGGER_AFTER.
625*/
danielk19774adee202004-05-08 08:23:19 +0000626int sqlite3TriggersExist(
drhc977f7f2002-05-21 11:38:11 +0000627 Parse *pParse, /* Used to check for recursive triggers */
drhdca76842004-12-07 14:06:13 +0000628 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000629 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
drhc977f7f2002-05-21 11:38:11 +0000630 ExprList *pChanges /* Columns that change in an UPDATE statement */
631){
drh4cbdda92006-06-14 19:00:20 +0000632 Trigger *pTrigger;
drhdca76842004-12-07 14:06:13 +0000633 int mask = 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000634
drh4cbdda92006-06-14 19:00:20 +0000635 pTrigger = IsVirtual(pTab) ? 0 : pTab->pTrigger;
drhdca76842004-12-07 14:06:13 +0000636 while( pTrigger ){
637 if( pTrigger->op==op && checkColumnOverLap(pTrigger->pColumns, pChanges) ){
drh229caa32006-03-25 15:52:19 +0000638 mask |= pTrigger->tr_tm;
danielk1977c3f9bad2002-05-15 08:30:12 +0000639 }
drhdca76842004-12-07 14:06:13 +0000640 pTrigger = pTrigger->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +0000641 }
drhdca76842004-12-07 14:06:13 +0000642 return mask;
danielk1977c3f9bad2002-05-15 08:30:12 +0000643}
644
drhc977f7f2002-05-21 11:38:11 +0000645/*
drhf26e09c2003-05-31 16:21:12 +0000646** Convert the pStep->target token into a SrcList and return a pointer
647** to that SrcList.
648**
649** This routine adds a specific database name, if needed, to the target when
650** forming the SrcList. This prevents a trigger in one database from
651** referring to a target in another database. An exception is when the
652** trigger is in TEMP in which case it can refer to any other database it
653** wants.
654*/
655static SrcList *targetSrcList(
656 Parse *pParse, /* The parsing context */
657 TriggerStep *pStep /* The trigger containing the target token */
658){
659 Token sDb; /* Dummy database name token */
660 int iDb; /* Index of the database to use */
661 SrcList *pSrc; /* SrcList to be returned */
662
danielk1977da184232006-01-05 11:34:32 +0000663 iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
drhf26e09c2003-05-31 16:21:12 +0000664 if( iDb==0 || iDb>=2 ){
665 assert( iDb<pParse->db->nDb );
drh2646da72005-12-09 20:02:05 +0000666 sDb.z = (u8*)pParse->db->aDb[iDb].zName;
667 sDb.n = strlen((char*)sDb.z);
drh17435752007-08-16 04:30:38 +0000668 pSrc = sqlite3SrcListAppend(pParse->db, 0, &sDb, &pStep->target);
drhf26e09c2003-05-31 16:21:12 +0000669 } else {
drh17435752007-08-16 04:30:38 +0000670 pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
drhf26e09c2003-05-31 16:21:12 +0000671 }
672 return pSrc;
673}
674
675/*
drhc977f7f2002-05-21 11:38:11 +0000676** Generate VDBE code for zero or more statements inside the body of a
677** trigger.
678*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000679static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000680 Parse *pParse, /* The parser context */
681 TriggerStep *pStepList, /* List of statements inside the trigger body */
682 int orconfin /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000683){
684 TriggerStep * pTriggerStep = pStepList;
685 int orconf;
drh344737f2004-09-19 00:50:20 +0000686 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000687 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000688
drh344737f2004-09-19 00:50:20 +0000689 assert( pTriggerStep!=0 );
690 assert( v!=0 );
drh66a51672008-01-03 00:01:23 +0000691 sqlite3VdbeAddOp2(v, OP_ContextPush, 0, 0);
drhd4e70eb2008-01-02 00:34:36 +0000692 VdbeComment((v, "begin trigger %s", pStepList->pTrig->name));
danielk1977633ed082002-05-17 00:05:58 +0000693 while( pTriggerStep ){
danielk1977633ed082002-05-17 00:05:58 +0000694 orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin;
695 pParse->trigStack->orconf = orconf;
696 switch( pTriggerStep->op ){
697 case TK_SELECT: {
drh17435752007-08-16 04:30:38 +0000698 Select *ss = sqlite3SelectDup(db, pTriggerStep->pSelect);
drh28f45912006-10-18 23:26:38 +0000699 if( ss ){
danielk19776c8c8ce2008-01-02 16:27:09 +0000700 SelectDest dest = {SRT_Discard, 0, 0};
drh28f45912006-10-18 23:26:38 +0000701 sqlite3SelectResolve(pParse, ss, 0);
danielk19776c8c8ce2008-01-02 16:27:09 +0000702 sqlite3Select(pParse, ss, &dest, 0, 0, 0, 0);
drh28f45912006-10-18 23:26:38 +0000703 sqlite3SelectDelete(ss);
704 }
drhfd131da2007-08-07 17:13:03 +0000705 break;
danielk1977633ed082002-05-17 00:05:58 +0000706 }
707 case TK_UPDATE: {
drh113088e2003-03-20 01:16:58 +0000708 SrcList *pSrc;
drhf26e09c2003-05-31 16:21:12 +0000709 pSrc = targetSrcList(pParse, pTriggerStep);
drh66a51672008-01-03 00:01:23 +0000710 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000711 sqlite3Update(pParse, pSrc,
drh17435752007-08-16 04:30:38 +0000712 sqlite3ExprListDup(db, pTriggerStep->pExprList),
713 sqlite3ExprDup(db, pTriggerStep->pWhere), orconf);
drh66a51672008-01-03 00:01:23 +0000714 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000715 break;
716 }
717 case TK_INSERT: {
drh113088e2003-03-20 01:16:58 +0000718 SrcList *pSrc;
drhf26e09c2003-05-31 16:21:12 +0000719 pSrc = targetSrcList(pParse, pTriggerStep);
drh66a51672008-01-03 00:01:23 +0000720 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000721 sqlite3Insert(pParse, pSrc,
drh17435752007-08-16 04:30:38 +0000722 sqlite3ExprListDup(db, pTriggerStep->pExprList),
723 sqlite3SelectDup(db, pTriggerStep->pSelect),
724 sqlite3IdListDup(db, pTriggerStep->pIdList), orconf);
drh66a51672008-01-03 00:01:23 +0000725 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000726 break;
727 }
728 case TK_DELETE: {
drh113088e2003-03-20 01:16:58 +0000729 SrcList *pSrc;
drh66a51672008-01-03 00:01:23 +0000730 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
drhf26e09c2003-05-31 16:21:12 +0000731 pSrc = targetSrcList(pParse, pTriggerStep);
drh17435752007-08-16 04:30:38 +0000732 sqlite3DeleteFrom(pParse, pSrc,
733 sqlite3ExprDup(db, pTriggerStep->pWhere));
drh66a51672008-01-03 00:01:23 +0000734 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000735 break;
736 }
737 default:
738 assert(0);
739 }
danielk1977633ed082002-05-17 00:05:58 +0000740 pTriggerStep = pTriggerStep->pNext;
741 }
drh66a51672008-01-03 00:01:23 +0000742 sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
drhd4e70eb2008-01-02 00:34:36 +0000743 VdbeComment((v, "end trigger %s", pStepList->pTrig->name));
danielk1977c3f9bad2002-05-15 08:30:12 +0000744
danielk1977633ed082002-05-17 00:05:58 +0000745 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000746}
747
danielk1977633ed082002-05-17 00:05:58 +0000748/*
749** This is called to code FOR EACH ROW triggers.
750**
751** When the code that this function generates is executed, the following
752** must be true:
drhc977f7f2002-05-21 11:38:11 +0000753**
754** 1. No cursors may be open in the main database. (But newIdx and oldIdx
755** can be indices of cursors in temporary tables. See below.)
756**
danielk1977633ed082002-05-17 00:05:58 +0000757** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then
758** a temporary vdbe cursor (index newIdx) must be open and pointing at
759** a row containing values to be substituted for new.* expressions in the
760** trigger program(s).
drhc977f7f2002-05-21 11:38:11 +0000761**
danielk1977633ed082002-05-17 00:05:58 +0000762** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then
763** a temporary vdbe cursor (index oldIdx) must be open and pointing at
764** a row containing values to be substituted for old.* expressions in the
765** trigger program(s).
766**
danielk19778f2c54e2008-01-01 19:02:09 +0000767** If they are not NULL, the piOldColMask and piNewColMask output variables
768** are set to values that describe the columns used by the trigger program
769** in the OLD.* and NEW.* tables respectively. If column N of the
770** pseudo-table is read at least once, the corresponding bit of the output
771** mask is set. If a column with an index greater than 32 is read, the
772** output mask is set to the special value 0xffffffff.
773**
danielk1977633ed082002-05-17 00:05:58 +0000774*/
danielk19774adee202004-05-08 08:23:19 +0000775int sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +0000776 Parse *pParse, /* Parse context */
777 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
778 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +0000779 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +0000780 Table *pTab, /* The table to code triggers from */
781 int newIdx, /* The indice of the "new" row to access */
782 int oldIdx, /* The indice of the "old" row to access */
danielk19776f349032002-06-11 02:25:40 +0000783 int orconf, /* ON CONFLICT policy */
danielk19778f2c54e2008-01-01 19:02:09 +0000784 int ignoreJump, /* Instruction to jump to for RAISE(IGNORE) */
785 u32 *piOldColMask, /* OUT: Mask of columns used from the OLD.* table */
786 u32 *piNewColMask /* OUT: Mask of columns used from the NEW.* table */
drhc977f7f2002-05-21 11:38:11 +0000787){
danielk1977eecfb3e2006-01-10 12:31:39 +0000788 Trigger *p;
drh67040462004-09-24 12:24:36 +0000789 TriggerStack trigStackEntry;
danielk1977c3f9bad2002-05-15 08:30:12 +0000790
danielk19778f2c54e2008-01-01 19:02:09 +0000791 trigStackEntry.oldColMask = 0;
792 trigStackEntry.newColMask = 0;
793
danielk1977c3f9bad2002-05-15 08:30:12 +0000794 assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE);
drhdca76842004-12-07 14:06:13 +0000795 assert(tr_tm == TRIGGER_BEFORE || tr_tm == TRIGGER_AFTER );
danielk1977c3f9bad2002-05-15 08:30:12 +0000796
danielk1977633ed082002-05-17 00:05:58 +0000797 assert(newIdx != -1 || oldIdx != -1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000798
danielk1977eecfb3e2006-01-10 12:31:39 +0000799 for(p=pTab->pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +0000800 int fire_this = 0;
danielk1977932083c2007-11-16 14:55:46 +0000801 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000802
danielk1977eecfb3e2006-01-10 12:31:39 +0000803 /* Determine whether we should code this trigger */
804 if(
805 p->op==op &&
806 p->tr_tm==tr_tm &&
danielk1977932083c2007-11-16 14:55:46 +0000807 (p->pSchema==p->pTabSchema || p->pSchema==db->aDb[1].pSchema) &&
danielk1977eecfb3e2006-01-10 12:31:39 +0000808 (op!=TK_UPDATE||!p->pColumns||checkColumnOverLap(p->pColumns,pChanges))
809 ){
810 TriggerStack *pS; /* Pointer to trigger-stack entry */
drh74161702006-02-24 02:53:49 +0000811 for(pS=pParse->trigStack; pS && p!=pS->pTrigger; pS=pS->pNext){}
danielk1977eecfb3e2006-01-10 12:31:39 +0000812 if( !pS ){
813 fire_this = 1;
danielk1977f29ce552002-05-19 23:43:12 +0000814 }
drh229caa32006-03-25 15:52:19 +0000815#if 0 /* Give no warning for recursive triggers. Just do not do them */
816 else{
817 sqlite3ErrorMsg(pParse, "recursive triggers not supported (%s)",
818 p->name);
819 return SQLITE_ERROR;
820 }
821#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000822 }
drhad6d9462004-09-19 02:15:24 +0000823
drh67040462004-09-24 12:24:36 +0000824 if( fire_this ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000825 int endTrigger;
danielk1977c3f9bad2002-05-15 08:30:12 +0000826 Expr * whenExpr;
drh85e20962003-04-25 17:52:11 +0000827 AuthContext sContext;
danielk1977b3bce662005-01-29 08:32:43 +0000828 NameContext sNC;
danielk1977c3f9bad2002-05-15 08:30:12 +0000829
danielk1977b3bce662005-01-29 08:32:43 +0000830 memset(&sNC, 0, sizeof(sNC));
831 sNC.pParse = pParse;
danielk1977c3f9bad2002-05-15 08:30:12 +0000832
833 /* Push an entry on to the trigger stack */
danielk1977eecfb3e2006-01-10 12:31:39 +0000834 trigStackEntry.pTrigger = p;
drh67040462004-09-24 12:24:36 +0000835 trigStackEntry.newIdx = newIdx;
836 trigStackEntry.oldIdx = oldIdx;
837 trigStackEntry.pTab = pTab;
838 trigStackEntry.pNext = pParse->trigStack;
839 trigStackEntry.ignoreJump = ignoreJump;
840 pParse->trigStack = &trigStackEntry;
danielk1977eecfb3e2006-01-10 12:31:39 +0000841 sqlite3AuthContextPush(pParse, &sContext, p->name);
danielk1977c3f9bad2002-05-15 08:30:12 +0000842
843 /* code the WHEN clause */
danielk19774adee202004-05-08 08:23:19 +0000844 endTrigger = sqlite3VdbeMakeLabel(pParse->pVdbe);
danielk1977932083c2007-11-16 14:55:46 +0000845 whenExpr = sqlite3ExprDup(db, p->pWhen);
846 if( db->mallocFailed || sqlite3ExprResolveNames(&sNC, whenExpr) ){
drh67040462004-09-24 12:24:36 +0000847 pParse->trigStack = trigStackEntry.pNext;
danielk19774adee202004-05-08 08:23:19 +0000848 sqlite3ExprDelete(whenExpr);
drh9adf9ac2002-05-15 11:44:13 +0000849 return 1;
danielk1977c3f9bad2002-05-15 08:30:12 +0000850 }
danielk19774adee202004-05-08 08:23:19 +0000851 sqlite3ExprIfFalse(pParse, whenExpr, endTrigger, 1);
852 sqlite3ExprDelete(whenExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000853
danielk1977eecfb3e2006-01-10 12:31:39 +0000854 codeTriggerProgram(pParse, p->step_list, orconf);
danielk1977c3f9bad2002-05-15 08:30:12 +0000855
856 /* Pop the entry off the trigger stack */
drh67040462004-09-24 12:24:36 +0000857 pParse->trigStack = trigStackEntry.pNext;
danielk19774adee202004-05-08 08:23:19 +0000858 sqlite3AuthContextPop(&sContext);
danielk1977c3f9bad2002-05-15 08:30:12 +0000859
danielk19774adee202004-05-08 08:23:19 +0000860 sqlite3VdbeResolveLabel(pParse->pVdbe, endTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000861 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000862 }
danielk19778f2c54e2008-01-01 19:02:09 +0000863 if( piOldColMask ) *piOldColMask |= trigStackEntry.oldColMask;
864 if( piNewColMask ) *piNewColMask |= trigStackEntry.newColMask;
danielk1977c3f9bad2002-05-15 08:30:12 +0000865 return 0;
866}
drhb7f91642004-10-31 02:22:47 +0000867#endif /* !defined(SQLITE_OMIT_TRIGGER) */