blob: 718e46e11017170688669e3fe527a516503ae74d [file] [log] [blame]
danielk1977c3f9bad2002-05-15 08:30:12 +00001/*
danielk1977633ed082002-05-17 00:05:58 +00002**
3** The author disclaims copyright to this source code. In place of
4** a legal notice, here is a blessing:
5**
6** May you do good and not evil.
7** May you find forgiveness for yourself and forgive others.
8** May you share freely, never taking more than you give.
9**
10*************************************************************************
danielk1977822a5162008-05-16 04:51:54 +000011**
12**
drhb7916a72009-05-27 10:31:29 +000013** $Id: trigger.c,v 1.140 2009/05/27 10:31:29 drh Exp $
drh9adf9ac2002-05-15 11:44:13 +000014*/
danielk1977c3f9bad2002-05-15 08:30:12 +000015#include "sqliteInt.h"
drh9adf9ac2002-05-15 11:44:13 +000016
drhb7f91642004-10-31 02:22:47 +000017#ifndef SQLITE_OMIT_TRIGGER
danielk1977c3f9bad2002-05-15 08:30:12 +000018/*
drh4b59ab52002-08-24 18:24:51 +000019** Delete a linked list of TriggerStep structures.
20*/
drh633e6d52008-07-28 19:34:53 +000021void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
drh4b59ab52002-08-24 18:24:51 +000022 while( pTriggerStep ){
23 TriggerStep * pTmp = pTriggerStep;
24 pTriggerStep = pTriggerStep->pNext;
25
drh633e6d52008-07-28 19:34:53 +000026 sqlite3ExprDelete(db, pTmp->pWhere);
27 sqlite3ExprListDelete(db, pTmp->pExprList);
28 sqlite3SelectDelete(db, pTmp->pSelect);
29 sqlite3IdListDelete(db, pTmp->pIdList);
drh4b59ab52002-08-24 18:24:51 +000030
drh633e6d52008-07-28 19:34:53 +000031 sqlite3DbFree(db, pTmp);
drh4b59ab52002-08-24 18:24:51 +000032 }
33}
34
35/*
danielk19772f886d12009-02-28 10:47:41 +000036** Given table pTab, return a list of all the triggers attached to
37** the table. The list is connected by Trigger.pNext pointers.
drh9ab4c2e2009-05-09 00:18:38 +000038**
39** All of the triggers on pTab that are in the same database as pTab
40** are already attached to pTab->pTrigger. But there might be additional
41** triggers on pTab in the TEMP schema. This routine prepends all
42** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
43** and returns the combined list.
44**
45** To state it another way: This routine returns a list of all triggers
46** that fire off of pTab. The list will include any TEMP triggers on
47** pTab as well as the triggers lised in pTab->pTrigger.
danielk19772f886d12009-02-28 10:47:41 +000048*/
49Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
50 Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
51 Trigger *pList = 0; /* List of triggers to return */
52
53 if( pTmpSchema!=pTab->pSchema ){
54 HashElem *p;
55 for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
56 Trigger *pTrig = (Trigger *)sqliteHashData(p);
57 if( pTrig->pTabSchema==pTab->pSchema
58 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
59 ){
60 pTrig->pNext = (pList ? pList : pTab->pTrigger);
61 pList = pTrig;
62 }
63 }
64 }
65
66 return (pList ? pList : pTab->pTrigger);
67}
68
69/*
drhf0f258b2003-04-21 18:48:45 +000070** This is called by the parser when it sees a CREATE TRIGGER statement
71** up to the point of the BEGIN before the trigger actions. A Trigger
72** structure is generated based on the information available and stored
73** in pParse->pNewTrigger. After the trigger actions have been parsed, the
danielk19774adee202004-05-08 08:23:19 +000074** sqlite3FinishTrigger() function is called to complete the trigger
drhf0f258b2003-04-21 18:48:45 +000075** construction process.
drh9adf9ac2002-05-15 11:44:13 +000076*/
danielk19774adee202004-05-08 08:23:19 +000077void sqlite3BeginTrigger(
drh9adf9ac2002-05-15 11:44:13 +000078 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
danielk1977ef2cb632004-05-29 02:37:19 +000079 Token *pName1, /* The name of the trigger */
80 Token *pName2, /* The name of the trigger */
drh5cf590c2003-04-24 01:45:04 +000081 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
drh9adf9ac2002-05-15 11:44:13 +000082 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
danielk1977633ed082002-05-17 00:05:58 +000083 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
drhd24cc422003-03-27 12:51:24 +000084 SrcList *pTableName,/* The name of the table/view the trigger applies to */
drh9adf9ac2002-05-15 11:44:13 +000085 Expr *pWhen, /* WHEN clause */
drhfdd48a72006-09-11 23:45:48 +000086 int isTemp, /* True if the TEMPORARY keyword is present */
87 int noErr /* Suppress errors if the trigger already exists */
drh9adf9ac2002-05-15 11:44:13 +000088){
danielk1977d5d56522005-03-16 12:15:20 +000089 Trigger *pTrigger = 0;
danielk1977ef2cb632004-05-29 02:37:19 +000090 Table *pTab;
drhf0f258b2003-04-21 18:48:45 +000091 char *zName = 0; /* Name of the trigger */
drh9bb575f2004-09-06 17:24:11 +000092 sqlite3 *db = pParse->db;
danielk1977ef2cb632004-05-29 02:37:19 +000093 int iDb; /* The database to store the trigger in */
94 Token *pName; /* The unqualified db name */
drh4312db52003-06-03 01:47:11 +000095 DbFixer sFix;
danielk1977da184232006-01-05 11:34:32 +000096 int iTabDb;
drhed6c8672003-01-12 18:02:16 +000097
drh43617e92006-03-06 20:55:46 +000098 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
99 assert( pName2!=0 );
drhaa78bec2008-12-09 03:55:14 +0000100 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
101 assert( op>0 && op<0xff );
danielk1977ef2cb632004-05-29 02:37:19 +0000102 if( isTemp ){
103 /* If TEMP was specified, then the trigger name may not be qualified. */
drh43617e92006-03-06 20:55:46 +0000104 if( pName2->n>0 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000105 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
106 goto trigger_cleanup;
107 }
108 iDb = 1;
109 pName = pName1;
110 }else{
111 /* Figure out the db that the the trigger will be created in */
112 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
113 if( iDb<0 ){
114 goto trigger_cleanup;
115 }
116 }
117
118 /* If the trigger name was unqualified, and the table is a temp table,
119 ** then set iDb to 1 to create the trigger in the temporary database.
120 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
121 ** exist, the error is caught by the block below.
drh9adf9ac2002-05-15 11:44:13 +0000122 */
drh17435752007-08-16 04:30:38 +0000123 if( !pTableName || db->mallocFailed ){
drh6f7adc82006-01-11 21:41:20 +0000124 goto trigger_cleanup;
125 }
danielk1977ef2cb632004-05-29 02:37:19 +0000126 pTab = sqlite3SrcListLookup(pParse, pTableName);
danielk1977da184232006-01-05 11:34:32 +0000127 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977ef2cb632004-05-29 02:37:19 +0000128 iDb = 1;
129 }
130
131 /* Ensure the table name matches database name and that the table exists */
drh17435752007-08-16 04:30:38 +0000132 if( db->mallocFailed ) goto trigger_cleanup;
drhd24cc422003-03-27 12:51:24 +0000133 assert( pTableName->nSrc==1 );
danielk1977ef2cb632004-05-29 02:37:19 +0000134 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) &&
135 sqlite3FixSrcList(&sFix, pTableName) ){
drh4312db52003-06-03 01:47:11 +0000136 goto trigger_cleanup;
drhf26e09c2003-05-31 16:21:12 +0000137 }
danielk1977ef2cb632004-05-29 02:37:19 +0000138 pTab = sqlite3SrcListLookup(pParse, pTableName);
139 if( !pTab ){
140 /* The table does not exist. */
drhd24cc422003-03-27 12:51:24 +0000141 goto trigger_cleanup;
142 }
drh4cbdda92006-06-14 19:00:20 +0000143 if( IsVirtual(pTab) ){
144 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
145 goto trigger_cleanup;
146 }
drhd24cc422003-03-27 12:51:24 +0000147
danielk1977d8123362004-06-12 09:25:12 +0000148 /* Check that the trigger name is not reserved and that no trigger of the
149 ** specified name exists */
drh17435752007-08-16 04:30:38 +0000150 zName = sqlite3NameFromToken(db, pName);
danielk1977d8123362004-06-12 09:25:12 +0000151 if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
152 goto trigger_cleanup;
153 }
drhaa78bec2008-12-09 03:55:14 +0000154 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
drhea678832008-12-10 19:26:22 +0000155 zName, sqlite3Strlen30(zName)) ){
drhfdd48a72006-09-11 23:45:48 +0000156 if( !noErr ){
157 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
158 }
drhe5f9c642003-01-13 23:27:31 +0000159 goto trigger_cleanup;
danielk1977c3f9bad2002-05-15 08:30:12 +0000160 }
danielk1977ef2cb632004-05-29 02:37:19 +0000161
162 /* Do not create a trigger on a system table */
drhdca76842004-12-07 14:06:13 +0000163 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk19774adee202004-05-08 08:23:19 +0000164 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
drhd24cc422003-03-27 12:51:24 +0000165 pParse->nErr++;
166 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40 +0000167 }
danielk1977ef2cb632004-05-29 02:37:19 +0000168
169 /* INSTEAD of triggers are only for views and views only support INSTEAD
170 ** of triggers.
171 */
172 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000173 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drhda93d232003-03-31 02:12:46 +0000174 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000175 goto trigger_cleanup;
176 }
danielk1977ef2cb632004-05-29 02:37:19 +0000177 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000178 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drhda93d232003-03-31 02:12:46 +0000179 " trigger on table: %S", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000180 goto trigger_cleanup;
181 }
danielk1977da184232006-01-05 11:34:32 +0000182 iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977ef2cb632004-05-29 02:37:19 +0000183
drhd24cc422003-03-27 12:51:24 +0000184#ifndef SQLITE_OMIT_AUTHORIZATION
185 {
186 int code = SQLITE_CREATE_TRIGGER;
danielk1977da184232006-01-05 11:34:32 +0000187 const char *zDb = db->aDb[iTabDb].zName;
drhe22a3342003-04-22 20:30:37 +0000188 const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
danielk1977da184232006-01-05 11:34:32 +0000189 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19 +0000190 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24 +0000191 goto trigger_cleanup;
192 }
danielk1977da184232006-01-05 11:34:32 +0000193 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24 +0000194 goto trigger_cleanup;
195 }
196 }
197#endif
danielk1977d702fcc2002-05-26 23:24:40 +0000198
danielk1977ef2cb632004-05-29 02:37:19 +0000199 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04 +0000200 ** cannot appear on views. So we might as well translate every
201 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
202 ** elsewhere.
203 */
danielk1977d702fcc2002-05-26 23:24:40 +0000204 if (tr_tm == TK_INSTEAD){
205 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12 +0000206 }
207
208 /* Build the Trigger object */
drh17435752007-08-16 04:30:38 +0000209 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19 +0000210 if( pTrigger==0 ) goto trigger_cleanup;
211 pTrigger->name = zName;
drhe5f9c642003-01-13 23:27:31 +0000212 zName = 0;
drh17435752007-08-16 04:30:38 +0000213 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32 +0000214 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48 +0000215 pTrigger->pTabSchema = pTab->pSchema;
drhaa78bec2008-12-09 03:55:14 +0000216 pTrigger->op = (u8)op;
drhdca76842004-12-07 14:06:13 +0000217 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
danielk19776ab3a2e2009-02-19 14:39:25 +0000218 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
drh17435752007-08-16 04:30:38 +0000219 pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
drhf0f258b2003-04-21 18:48:45 +0000220 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19 +0000221 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45 +0000222
223trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000224 sqlite3DbFree(db, zName);
225 sqlite3SrcListDelete(db, pTableName);
226 sqlite3IdListDelete(db, pColumns);
227 sqlite3ExprDelete(db, pWhen);
danielk1977d5d56522005-03-16 12:15:20 +0000228 if( !pParse->pNewTrigger ){
drh633e6d52008-07-28 19:34:53 +0000229 sqlite3DeleteTrigger(db, pTrigger);
danielk1977d5d56522005-03-16 12:15:20 +0000230 }else{
231 assert( pParse->pNewTrigger==pTrigger );
232 }
drhf0f258b2003-04-21 18:48:45 +0000233}
234
235/*
236** This routine is called after all of the trigger actions have been parsed
237** in order to complete the process of building the trigger.
238*/
danielk19774adee202004-05-08 08:23:19 +0000239void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45 +0000240 Parse *pParse, /* Parser context */
241 TriggerStep *pStepList, /* The triggered program */
242 Token *pAll /* Token that describes the complete CREATE TRIGGER */
243){
danielk19772f886d12009-02-28 10:47:41 +0000244 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
245 char *zName; /* Name of trigger */
246 sqlite3 *db = pParse->db; /* The database */
drhf26e09c2003-05-31 16:21:12 +0000247 DbFixer sFix;
danielk19772f886d12009-02-28 10:47:41 +0000248 int iDb; /* Database containing the trigger */
drhb7916a72009-05-27 10:31:29 +0000249 Token nameToken; /* Trigger name for error reporting */
drhf0f258b2003-04-21 18:48:45 +0000250
danielk1977bfb9e352005-01-24 13:03:32 +0000251 pTrig = pParse->pNewTrigger;
drhf0f258b2003-04-21 18:48:45 +0000252 pParse->pNewTrigger = 0;
drh9ab4c2e2009-05-09 00:18:38 +0000253 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
danielk19772f886d12009-02-28 10:47:41 +0000254 zName = pTrig->name;
danielk1977da184232006-01-05 11:34:32 +0000255 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32 +0000256 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53 +0000257 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32 +0000258 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53 +0000259 pStepList = pStepList->pNext;
260 }
drhb7916a72009-05-27 10:31:29 +0000261 nameToken.z = pTrig->name;
262 nameToken.n = sqlite3Strlen30(nameToken.z);
263 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken)
danielk1977bfb9e352005-01-24 13:03:32 +0000264 && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
drhf26e09c2003-05-31 16:21:12 +0000265 goto triggerfinish_cleanup;
266 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000267
268 /* if we are not initializing, and this trigger is not on a TEMP table,
drh9adf9ac2002-05-15 11:44:13 +0000269 ** build the sqlite_master entry
270 */
drh1d85d932004-02-14 23:05:52 +0000271 if( !db->init.busy ){
drhc977f7f2002-05-21 11:38:11 +0000272 Vdbe *v;
drh2d401ab2008-01-10 23:50:11 +0000273 char *z;
danielk1977c3f9bad2002-05-15 08:30:12 +0000274
275 /* Make an entry in the sqlite_master table */
danielk19774adee202004-05-08 08:23:19 +0000276 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000277 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000278 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11 +0000279 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
280 sqlite3NestedParse(pParse,
281 "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
danielk19772f886d12009-02-28 10:47:41 +0000282 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
drh2d401ab2008-01-10 23:50:11 +0000283 pTrig->table, z);
drh633e6d52008-07-28 19:34:53 +0000284 sqlite3DbFree(db, z);
drh9cbf3422008-01-17 16:22:13 +0000285 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +0000286 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, sqlite3MPrintf(
danielk19772f886d12009-02-28 10:47:41 +0000287 db, "type='trigger' AND name='%q'", zName), P4_DYNAMIC
danielk19771e536952007-08-16 10:09:01 +0000288 );
danielk1977c3f9bad2002-05-15 08:30:12 +0000289 }
290
drh956bc922004-07-24 17:38:29 +0000291 if( db->init.busy ){
danielk19772f886d12009-02-28 10:47:41 +0000292 Trigger *pLink = pTrig;
293 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
294 pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);
295 if( pTrig ){
drhf3a65f72007-08-22 20:18:21 +0000296 db->mallocFailed = 1;
danielk19772f886d12009-02-28 10:47:41 +0000297 }else if( pLink->pSchema==pLink->pTabSchema ){
298 Table *pTab;
drha83ccca2009-04-28 13:01:09 +0000299 int n = sqlite3Strlen30(pLink->table);
danielk19772f886d12009-02-28 10:47:41 +0000300 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n);
301 assert( pTab!=0 );
302 pLink->pNext = pTab->pTrigger;
303 pTab->pTrigger = pLink;
danielk1977d5d56522005-03-16 12:15:20 +0000304 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000305 }
306
drhf0f258b2003-04-21 18:48:45 +0000307triggerfinish_cleanup:
drh633e6d52008-07-28 19:34:53 +0000308 sqlite3DeleteTrigger(db, pTrig);
danielk1977bfb9e352005-01-24 13:03:32 +0000309 assert( !pParse->pNewTrigger );
drh633e6d52008-07-28 19:34:53 +0000310 sqlite3DeleteTriggerStep(db, pStepList);
drh4b59ab52002-08-24 18:24:51 +0000311}
danielk1977c3f9bad2002-05-15 08:30:12 +0000312
drh4b59ab52002-08-24 18:24:51 +0000313/*
drhc977f7f2002-05-21 11:38:11 +0000314** Turn a SELECT statement (that the pSelect parameter points to) into
315** a trigger step. Return a pointer to a TriggerStep structure.
316**
317** The parser calls this routine when it finds a SELECT statement in
318** body of a TRIGGER.
319*/
drh17435752007-08-16 04:30:38 +0000320TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
321 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000322 if( pTriggerStep==0 ) {
drh633e6d52008-07-28 19:34:53 +0000323 sqlite3SelectDelete(db, pSelect);
danielk19772e588c72005-12-09 14:25:08 +0000324 return 0;
325 }
danielk1977633ed082002-05-17 00:05:58 +0000326 pTriggerStep->op = TK_SELECT;
327 pTriggerStep->pSelect = pSelect;
328 pTriggerStep->orconf = OE_Default;
drhb7916a72009-05-27 10:31:29 +0000329 return pTriggerStep;
330}
danielk1977c3f9bad2002-05-15 08:30:12 +0000331
drhb7916a72009-05-27 10:31:29 +0000332/*
333** Allocate space to hold a new trigger step. The allocated space
334** holds both the TriggerStep object and the TriggerStep.target.z string.
335**
336** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
337*/
338static TriggerStep *triggerStepAllocate(
339 sqlite3 *db, /* Database connection */
340 int op, /* Trigger opcode */
341 Token *pName /* The target name */
342){
343 TriggerStep *pTriggerStep;
344
345 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n);
346 if( pTriggerStep ){
347 char *z = (char*)&pTriggerStep[1];
348 memcpy(z, pName->z, pName->n);
349 pTriggerStep->target.z = z;
350 pTriggerStep->target.n = pName->n;
351 pTriggerStep->op = op;
352 }
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
drhb7916a72009-05-27 10:31:29 +0000376 pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
danielk1977d5d56522005-03-16 12:15:20 +0000377 if( pTriggerStep ){
danielk1977d5d56522005-03-16 12:15:20 +0000378 pTriggerStep->pSelect = pSelect;
danielk1977d5d56522005-03-16 12:15:20 +0000379 pTriggerStep->pIdList = pColumn;
380 pTriggerStep->pExprList = pEList;
381 pTriggerStep->orconf = orconf;
danielk1977d5d56522005-03-16 12:15:20 +0000382 }else{
drh633e6d52008-07-28 19:34:53 +0000383 sqlite3IdListDelete(db, pColumn);
384 sqlite3ExprListDelete(db, pEList);
385 sqlite3SelectDelete(db, pSelect);
danielk1977d5d56522005-03-16 12:15:20 +0000386 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000387
danielk1977633ed082002-05-17 00:05:58 +0000388 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000389}
390
drhc977f7f2002-05-21 11:38:11 +0000391/*
392** Construct a trigger step that implements an UPDATE statement and return
393** a pointer to that trigger step. The parser calls this routine when it
394** sees an UPDATE statement inside the body of a CREATE TRIGGER.
395*/
danielk19774adee202004-05-08 08:23:19 +0000396TriggerStep *sqlite3TriggerUpdateStep(
drh17435752007-08-16 04:30:38 +0000397 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000398 Token *pTableName, /* Name of the table to be updated */
399 ExprList *pEList, /* The SET clause: list of column and new values */
400 Expr *pWhere, /* The WHERE clause */
401 int orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
402){
drhb7916a72009-05-27 10:31:29 +0000403 TriggerStep *pTriggerStep;
404
405 pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
drh0e3a6f32007-03-30 20:40:34 +0000406 if( pTriggerStep==0 ){
drh633e6d52008-07-28 19:34:53 +0000407 sqlite3ExprListDelete(db, pEList);
408 sqlite3ExprDelete(db, pWhere);
drh0e3a6f32007-03-30 20:40:34 +0000409 return 0;
410 }
danielk1977633ed082002-05-17 00:05:58 +0000411 pTriggerStep->pExprList = pEList;
412 pTriggerStep->pWhere = pWhere;
413 pTriggerStep->orconf = orconf;
danielk1977633ed082002-05-17 00:05:58 +0000414 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000415}
416
drhc977f7f2002-05-21 11:38:11 +0000417/*
418** Construct a trigger step that implements a DELETE statement and return
419** a pointer to that trigger step. The parser calls this routine when it
420** sees a DELETE statement inside the body of a CREATE TRIGGER.
421*/
drh17435752007-08-16 04:30:38 +0000422TriggerStep *sqlite3TriggerDeleteStep(
423 sqlite3 *db, /* Database connection */
424 Token *pTableName, /* The table from which rows are deleted */
425 Expr *pWhere /* The WHERE clause */
426){
drhb7916a72009-05-27 10:31:29 +0000427 TriggerStep *pTriggerStep;
428
429 pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
drhb63a53d2007-03-31 01:34:44 +0000430 if( pTriggerStep==0 ){
drh633e6d52008-07-28 19:34:53 +0000431 sqlite3ExprDelete(db, pWhere);
drhb63a53d2007-03-31 01:34:44 +0000432 return 0;
433 }
danielk1977633ed082002-05-17 00:05:58 +0000434 pTriggerStep->pWhere = pWhere;
435 pTriggerStep->orconf = OE_Default;
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*/
drh633e6d52008-07-28 19:34:53 +0000443void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
drhf0f258b2003-04-21 18:48:45 +0000444 if( pTrigger==0 ) return;
drh633e6d52008-07-28 19:34:53 +0000445 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
446 sqlite3DbFree(db, pTrigger->name);
447 sqlite3DbFree(db, pTrigger->table);
448 sqlite3ExprDelete(db, pTrigger->pWhen);
449 sqlite3IdListDelete(db, pTrigger->pColumns);
drh633e6d52008-07-28 19:34:53 +0000450 sqlite3DbFree(db, pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000451}
452
453/*
danielk19778a414492004-06-29 08:59:35 +0000454** This function is called to drop a trigger from the database schema.
455**
456** This may be called directly from the parser and therefore identifies
457** the trigger by name. The sqlite3DropTriggerPtr() routine does the
458** same job as this routine except it takes a pointer to the trigger
459** instead of the trigger name.
460**/
drhfdd48a72006-09-11 23:45:48 +0000461void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000462 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000463 int i;
464 const char *zDb;
465 const char *zName;
466 int nName;
drh9bb575f2004-09-06 17:24:11 +0000467 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000468
drh17435752007-08-16 04:30:38 +0000469 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000470 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000471 goto drop_trigger_cleanup;
472 }
danielk19778e227872004-06-07 07:52:17 +0000473
drhd24cc422003-03-27 12:51:24 +0000474 assert( pName->nSrc==1 );
475 zDb = pName->a[0].zDatabase;
476 zName = pName->a[0].zName;
drhea678832008-12-10 19:26:22 +0000477 nName = sqlite3Strlen30(zName);
danielk197753c0f742005-03-29 03:10:59 +0000478 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000479 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000480 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
drhe4df0e72006-03-29 00:24:06 +0000481 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
drhd24cc422003-03-27 12:51:24 +0000482 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000483 }
drhd24cc422003-03-27 12:51:24 +0000484 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000485 if( !noErr ){
486 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
487 }
drhd24cc422003-03-27 12:51:24 +0000488 goto drop_trigger_cleanup;
489 }
drh74161702006-02-24 02:53:49 +0000490 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000491
492drop_trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000493 sqlite3SrcListDelete(db, pName);
drh79a519c2003-05-17 19:04:03 +0000494}
495
496/*
drh956bc922004-07-24 17:38:29 +0000497** Return a pointer to the Table structure for the table that a trigger
498** is set on.
499*/
drh74161702006-02-24 02:53:49 +0000500static Table *tableOfTrigger(Trigger *pTrigger){
drha83ccca2009-04-28 13:01:09 +0000501 int n = sqlite3Strlen30(pTrigger->table);
danielk1977aaf22682006-01-06 15:03:48 +0000502 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
drh956bc922004-07-24 17:38:29 +0000503}
504
505
506/*
drh74161702006-02-24 02:53:49 +0000507** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000508*/
drh74161702006-02-24 02:53:49 +0000509void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000510 Table *pTable;
511 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000512 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000513 int iDb;
drh79a519c2003-05-17 19:04:03 +0000514
danielk1977da184232006-01-05 11:34:32 +0000515 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000516 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000517 pTable = tableOfTrigger(pTrigger);
drh43617e92006-03-06 20:55:46 +0000518 assert( pTable );
danielk1977da184232006-01-05 11:34:32 +0000519 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000520#ifndef SQLITE_OMIT_AUTHORIZATION
521 {
522 int code = SQLITE_DROP_TRIGGER;
drh956bc922004-07-24 17:38:29 +0000523 const char *zDb = db->aDb[iDb].zName;
524 const char *zTab = SCHEMA_TABLE(iDb);
525 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
danielk19774adee202004-05-08 08:23:19 +0000526 if( sqlite3AuthCheck(pParse, code, pTrigger->name, pTable->zName, zDb) ||
527 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000528 return;
drhe5f9c642003-01-13 23:27:31 +0000529 }
drhed6c8672003-01-12 18:02:16 +0000530 }
drhe5f9c642003-01-13 23:27:31 +0000531#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000532
drhf0f258b2003-04-21 18:48:45 +0000533 /* Generate code to destroy the database record of the trigger.
534 */
drh43617e92006-03-06 20:55:46 +0000535 assert( pTable!=0 );
536 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drhf0f258b2003-04-21 18:48:45 +0000537 int base;
drh57196282004-10-06 15:41:16 +0000538 static const VdbeOpList dropTrigger[] = {
drh9b1b01b2003-08-16 12:37:51 +0000539 { OP_Rewind, 0, ADDR(9), 0},
drh1db639c2008-01-17 02:36:28 +0000540 { OP_String8, 0, 1, 0}, /* 1 */
541 { OP_Column, 0, 1, 2},
542 { OP_Ne, 2, ADDR(8), 1},
543 { OP_String8, 0, 1, 0}, /* 4: "trigger" */
544 { OP_Column, 0, 0, 2},
545 { OP_Ne, 2, ADDR(8), 1},
drhf0f258b2003-04-21 18:48:45 +0000546 { OP_Delete, 0, 0, 0},
drh9b1b01b2003-08-16 12:37:51 +0000547 { OP_Next, 0, ADDR(1), 0}, /* 8 */
drhf0f258b2003-04-21 18:48:45 +0000548 };
549
drh956bc922004-07-24 17:38:29 +0000550 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk1977c00da102006-01-07 13:21:04 +0000551 sqlite3OpenMasterTable(pParse, iDb);
danielk19774adee202004-05-08 08:23:19 +0000552 base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger);
drh66a51672008-01-03 00:01:23 +0000553 sqlite3VdbeChangeP4(v, base+1, pTrigger->name, 0);
drh24003452008-01-03 01:28:59 +0000554 sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
drh9cbf3422008-01-17 16:22:13 +0000555 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +0000556 sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
557 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->name, 0);
danielk19776ab3a2e2009-02-19 14:39:25 +0000558 if( pParse->nMem<3 ){
559 pParse->nMem = 3;
560 }
drhf0f258b2003-04-21 18:48:45 +0000561 }
drh956bc922004-07-24 17:38:29 +0000562}
drhf0f258b2003-04-21 18:48:45 +0000563
drh956bc922004-07-24 17:38:29 +0000564/*
565** Remove a trigger from the hash tables of the sqlite* pointer.
566*/
567void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
danielk19772f886d12009-02-28 10:47:41 +0000568 Hash *pHash = &(db->aDb[iDb].pSchema->trigHash);
drh956bc922004-07-24 17:38:29 +0000569 Trigger *pTrigger;
danielk19772f886d12009-02-28 10:47:41 +0000570 pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0);
drh9ab4c2e2009-05-09 00:18:38 +0000571 if( ALWAYS(pTrigger) ){
danielk19772f886d12009-02-28 10:47:41 +0000572 if( pTrigger->pSchema==pTrigger->pTabSchema ){
573 Table *pTab = tableOfTrigger(pTrigger);
574 Trigger **pp;
575 for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
576 *pp = (*pp)->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +0000577 }
drh633e6d52008-07-28 19:34:53 +0000578 sqlite3DeleteTrigger(db, pTrigger);
drh956bc922004-07-24 17:38:29 +0000579 db->flags |= SQLITE_InternChanges;
danielk1977c3f9bad2002-05-15 08:30:12 +0000580 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000581}
582
drhc977f7f2002-05-21 11:38:11 +0000583/*
584** pEList is the SET clause of an UPDATE statement. Each entry
585** in pEList is of the format <id>=<expr>. If any of the entries
586** in pEList have an <id> which matches an identifier in pIdList,
587** then return TRUE. If pIdList==NULL, then it is considered a
588** wildcard that matches anything. Likewise if pEList==NULL then
589** it matches anything so always return true. Return false only
590** if there is no match.
591*/
drh9ab4c2e2009-05-09 00:18:38 +0000592static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000593 int e;
drh9ab4c2e2009-05-09 00:18:38 +0000594 if( pIdList==0 || NEVER(pEList==0) ) return 1;
drhad2d8302002-05-24 20:31:36 +0000595 for(e=0; e<pEList->nExpr; e++){
danielk19774adee202004-05-08 08:23:19 +0000596 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000597 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000598 return 0;
599}
600
danielk1977c3f9bad2002-05-15 08:30:12 +0000601/*
danielk19772f886d12009-02-28 10:47:41 +0000602** Return a list of all triggers on table pTab if there exists at least
603** one trigger that must be fired when an operation of type 'op' is
604** performed on the table, and, if that operation is an UPDATE, if at
605** least one of the columns in pChanges is being modified.
drhdca76842004-12-07 14:06:13 +0000606*/
danielk19772f886d12009-02-28 10:47:41 +0000607Trigger *sqlite3TriggersExist(
608 Parse *pParse, /* Parse context */
drhdca76842004-12-07 14:06:13 +0000609 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000610 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
danielk19772f886d12009-02-28 10:47:41 +0000611 ExprList *pChanges, /* Columns that change in an UPDATE statement */
612 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
drhc977f7f2002-05-21 11:38:11 +0000613){
drhdca76842004-12-07 14:06:13 +0000614 int mask = 0;
danielk19772f886d12009-02-28 10:47:41 +0000615 Trigger *pList = sqlite3TriggerList(pParse, pTab);
616 Trigger *p;
617 assert( pList==0 || IsVirtual(pTab)==0 );
618 for(p=pList; p; p=p->pNext){
drh9ab4c2e2009-05-09 00:18:38 +0000619 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
danielk19772f886d12009-02-28 10:47:41 +0000620 mask |= p->tr_tm;
danielk1977c3f9bad2002-05-15 08:30:12 +0000621 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000622 }
danielk19772f886d12009-02-28 10:47:41 +0000623 if( pMask ){
624 *pMask = mask;
625 }
626 return (mask ? pList : 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000627}
628
drhc977f7f2002-05-21 11:38:11 +0000629/*
drhf26e09c2003-05-31 16:21:12 +0000630** Convert the pStep->target token into a SrcList and return a pointer
631** to that SrcList.
632**
633** This routine adds a specific database name, if needed, to the target when
634** forming the SrcList. This prevents a trigger in one database from
635** referring to a target in another database. An exception is when the
636** trigger is in TEMP in which case it can refer to any other database it
637** wants.
638*/
639static SrcList *targetSrcList(
640 Parse *pParse, /* The parsing context */
641 TriggerStep *pStep /* The trigger containing the target token */
642){
643 Token sDb; /* Dummy database name token */
644 int iDb; /* Index of the database to use */
645 SrcList *pSrc; /* SrcList to be returned */
646
drhb7916a72009-05-27 10:31:29 +0000647 pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
648 if( pSrc ){
649 assert( pSrc->nSrc>0 );
650 assert( pSrc->a!=0 );
651 iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
652 if( iDb==0 || iDb>=2 ){
653 sqlite3 *db = pParse->db;
654 assert( iDb<pParse->db->nDb );
655 pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
656 }
drhf26e09c2003-05-31 16:21:12 +0000657 }
658 return pSrc;
659}
660
661/*
drhc977f7f2002-05-21 11:38:11 +0000662** Generate VDBE code for zero or more statements inside the body of a
663** trigger.
664*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000665static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000666 Parse *pParse, /* The parser context */
667 TriggerStep *pStepList, /* List of statements inside the trigger body */
668 int orconfin /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000669){
670 TriggerStep * pTriggerStep = pStepList;
671 int orconf;
drh344737f2004-09-19 00:50:20 +0000672 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000673 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000674
drh344737f2004-09-19 00:50:20 +0000675 assert( pTriggerStep!=0 );
676 assert( v!=0 );
drh66a51672008-01-03 00:01:23 +0000677 sqlite3VdbeAddOp2(v, OP_ContextPush, 0, 0);
drhd4e70eb2008-01-02 00:34:36 +0000678 VdbeComment((v, "begin trigger %s", pStepList->pTrig->name));
danielk1977633ed082002-05-17 00:05:58 +0000679 while( pTriggerStep ){
drhceea3322009-04-23 13:22:42 +0000680 sqlite3ExprCacheClear(pParse);
danielk1977633ed082002-05-17 00:05:58 +0000681 orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin;
682 pParse->trigStack->orconf = orconf;
683 switch( pTriggerStep->op ){
danielk1977633ed082002-05-17 00:05:58 +0000684 case TK_UPDATE: {
drh113088e2003-03-20 01:16:58 +0000685 SrcList *pSrc;
drhf26e09c2003-05-31 16:21:12 +0000686 pSrc = targetSrcList(pParse, pTriggerStep);
drh66a51672008-01-03 00:01:23 +0000687 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000688 sqlite3Update(pParse, pSrc,
danielk19776ab3a2e2009-02-19 14:39:25 +0000689 sqlite3ExprListDup(db, pTriggerStep->pExprList, 0),
690 sqlite3ExprDup(db, pTriggerStep->pWhere, 0), orconf);
drh66a51672008-01-03 00:01:23 +0000691 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000692 break;
693 }
694 case TK_INSERT: {
drh113088e2003-03-20 01:16:58 +0000695 SrcList *pSrc;
drhf26e09c2003-05-31 16:21:12 +0000696 pSrc = targetSrcList(pParse, pTriggerStep);
drh66a51672008-01-03 00:01:23 +0000697 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000698 sqlite3Insert(pParse, pSrc,
danielk19776ab3a2e2009-02-19 14:39:25 +0000699 sqlite3ExprListDup(db, pTriggerStep->pExprList, 0),
700 sqlite3SelectDup(db, pTriggerStep->pSelect, 0),
drh17435752007-08-16 04:30:38 +0000701 sqlite3IdListDup(db, pTriggerStep->pIdList), orconf);
drh66a51672008-01-03 00:01:23 +0000702 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000703 break;
704 }
705 case TK_DELETE: {
drh113088e2003-03-20 01:16:58 +0000706 SrcList *pSrc;
drh66a51672008-01-03 00:01:23 +0000707 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
drhf26e09c2003-05-31 16:21:12 +0000708 pSrc = targetSrcList(pParse, pTriggerStep);
drh17435752007-08-16 04:30:38 +0000709 sqlite3DeleteFrom(pParse, pSrc,
danielk19776ab3a2e2009-02-19 14:39:25 +0000710 sqlite3ExprDup(db, pTriggerStep->pWhere, 0));
drh66a51672008-01-03 00:01:23 +0000711 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000712 break;
713 }
drh9ab4c2e2009-05-09 00:18:38 +0000714 default: assert( pTriggerStep->op==TK_SELECT ); {
715 Select *ss = sqlite3SelectDup(db, pTriggerStep->pSelect, 0);
716 if( ss ){
717 SelectDest dest;
718
719 sqlite3SelectDestInit(&dest, SRT_Discard, 0);
720 sqlite3Select(pParse, ss, &dest);
721 sqlite3SelectDelete(db, ss);
722 }
723 break;
724 }
danielk1977633ed082002-05-17 00:05:58 +0000725 }
danielk1977633ed082002-05-17 00:05:58 +0000726 pTriggerStep = pTriggerStep->pNext;
727 }
drh66a51672008-01-03 00:01:23 +0000728 sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
drhd4e70eb2008-01-02 00:34:36 +0000729 VdbeComment((v, "end trigger %s", pStepList->pTrig->name));
danielk1977c3f9bad2002-05-15 08:30:12 +0000730
danielk1977633ed082002-05-17 00:05:58 +0000731 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000732}
733
danielk1977633ed082002-05-17 00:05:58 +0000734/*
735** This is called to code FOR EACH ROW triggers.
736**
737** When the code that this function generates is executed, the following
738** must be true:
drhc977f7f2002-05-21 11:38:11 +0000739**
740** 1. No cursors may be open in the main database. (But newIdx and oldIdx
741** can be indices of cursors in temporary tables. See below.)
742**
danielk1977633ed082002-05-17 00:05:58 +0000743** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then
744** a temporary vdbe cursor (index newIdx) must be open and pointing at
745** a row containing values to be substituted for new.* expressions in the
746** trigger program(s).
drhc977f7f2002-05-21 11:38:11 +0000747**
danielk1977633ed082002-05-17 00:05:58 +0000748** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then
749** a temporary vdbe cursor (index oldIdx) must be open and pointing at
750** a row containing values to be substituted for old.* expressions in the
751** trigger program(s).
752**
danielk19778f2c54e2008-01-01 19:02:09 +0000753** If they are not NULL, the piOldColMask and piNewColMask output variables
754** are set to values that describe the columns used by the trigger program
755** in the OLD.* and NEW.* tables respectively. If column N of the
756** pseudo-table is read at least once, the corresponding bit of the output
757** mask is set. If a column with an index greater than 32 is read, the
758** output mask is set to the special value 0xffffffff.
759**
danielk1977633ed082002-05-17 00:05:58 +0000760*/
danielk19774adee202004-05-08 08:23:19 +0000761int sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +0000762 Parse *pParse, /* Parse context */
danielk19772f886d12009-02-28 10:47:41 +0000763 Trigger *pTrigger, /* List of triggers on table pTab */
danielk1977633ed082002-05-17 00:05:58 +0000764 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
765 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +0000766 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +0000767 Table *pTab, /* The table to code triggers from */
768 int newIdx, /* The indice of the "new" row to access */
769 int oldIdx, /* The indice of the "old" row to access */
danielk19776f349032002-06-11 02:25:40 +0000770 int orconf, /* ON CONFLICT policy */
danielk19778f2c54e2008-01-01 19:02:09 +0000771 int ignoreJump, /* Instruction to jump to for RAISE(IGNORE) */
772 u32 *piOldColMask, /* OUT: Mask of columns used from the OLD.* table */
773 u32 *piNewColMask /* OUT: Mask of columns used from the NEW.* table */
drhc977f7f2002-05-21 11:38:11 +0000774){
danielk1977eecfb3e2006-01-10 12:31:39 +0000775 Trigger *p;
drh0e359b32008-01-13 19:02:11 +0000776 sqlite3 *db = pParse->db;
drh67040462004-09-24 12:24:36 +0000777 TriggerStack trigStackEntry;
danielk1977c3f9bad2002-05-15 08:30:12 +0000778
danielk19778f2c54e2008-01-01 19:02:09 +0000779 trigStackEntry.oldColMask = 0;
780 trigStackEntry.newColMask = 0;
781
danielk1977c3f9bad2002-05-15 08:30:12 +0000782 assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE);
drhdca76842004-12-07 14:06:13 +0000783 assert(tr_tm == TRIGGER_BEFORE || tr_tm == TRIGGER_AFTER );
danielk1977c3f9bad2002-05-15 08:30:12 +0000784
danielk1977633ed082002-05-17 00:05:58 +0000785 assert(newIdx != -1 || oldIdx != -1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000786
danielk19772f886d12009-02-28 10:47:41 +0000787 for(p=pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +0000788 int fire_this = 0;
789
drh9ab4c2e2009-05-09 00:18:38 +0000790 /* Sanity checking: The schema for the trigger and for the table are
791 ** always defined. The trigger must be in the same schema as the table
792 ** or else it must be a TEMP trigger. */
793 assert( p->pSchema!=0 );
794 assert( p->pTabSchema!=0 );
795 assert( p->pSchema==p->pTabSchema || p->pSchema==db->aDb[1].pSchema );
796
danielk1977eecfb3e2006-01-10 12:31:39 +0000797 /* Determine whether we should code this trigger */
798 if(
799 p->op==op &&
800 p->tr_tm==tr_tm &&
drh9ab4c2e2009-05-09 00:18:38 +0000801 checkColumnOverlap(p->pColumns,pChanges)
danielk1977eecfb3e2006-01-10 12:31:39 +0000802 ){
803 TriggerStack *pS; /* Pointer to trigger-stack entry */
drh74161702006-02-24 02:53:49 +0000804 for(pS=pParse->trigStack; pS && p!=pS->pTrigger; pS=pS->pNext){}
danielk1977eecfb3e2006-01-10 12:31:39 +0000805 if( !pS ){
806 fire_this = 1;
danielk1977f29ce552002-05-19 23:43:12 +0000807 }
drh229caa32006-03-25 15:52:19 +0000808#if 0 /* Give no warning for recursive triggers. Just do not do them */
809 else{
810 sqlite3ErrorMsg(pParse, "recursive triggers not supported (%s)",
811 p->name);
812 return SQLITE_ERROR;
813 }
814#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000815 }
drhad6d9462004-09-19 02:15:24 +0000816
drh67040462004-09-24 12:24:36 +0000817 if( fire_this ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000818 int endTrigger;
danielk1977c3f9bad2002-05-15 08:30:12 +0000819 Expr * whenExpr;
drh85e20962003-04-25 17:52:11 +0000820 AuthContext sContext;
danielk1977b3bce662005-01-29 08:32:43 +0000821 NameContext sNC;
danielk1977c3f9bad2002-05-15 08:30:12 +0000822
drh949f9cd2008-01-12 21:35:57 +0000823#ifndef SQLITE_OMIT_TRACE
824 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Trace, 0, 0, 0,
drh0e359b32008-01-13 19:02:11 +0000825 sqlite3MPrintf(db, "-- TRIGGER %s", p->name),
drh949f9cd2008-01-12 21:35:57 +0000826 P4_DYNAMIC);
827#endif
danielk1977b3bce662005-01-29 08:32:43 +0000828 memset(&sNC, 0, sizeof(sNC));
829 sNC.pParse = pParse;
danielk1977c3f9bad2002-05-15 08:30:12 +0000830
831 /* Push an entry on to the trigger stack */
danielk1977eecfb3e2006-01-10 12:31:39 +0000832 trigStackEntry.pTrigger = p;
drh67040462004-09-24 12:24:36 +0000833 trigStackEntry.newIdx = newIdx;
834 trigStackEntry.oldIdx = oldIdx;
835 trigStackEntry.pTab = pTab;
836 trigStackEntry.pNext = pParse->trigStack;
837 trigStackEntry.ignoreJump = ignoreJump;
838 pParse->trigStack = &trigStackEntry;
danielk1977eecfb3e2006-01-10 12:31:39 +0000839 sqlite3AuthContextPush(pParse, &sContext, p->name);
danielk1977c3f9bad2002-05-15 08:30:12 +0000840
841 /* code the WHEN clause */
danielk19774adee202004-05-08 08:23:19 +0000842 endTrigger = sqlite3VdbeMakeLabel(pParse->pVdbe);
danielk19776ab3a2e2009-02-19 14:39:25 +0000843 whenExpr = sqlite3ExprDup(db, p->pWhen, 0);
drh7d10d5a2008-08-20 16:35:10 +0000844 if( db->mallocFailed || sqlite3ResolveExprNames(&sNC, whenExpr) ){
drh67040462004-09-24 12:24:36 +0000845 pParse->trigStack = trigStackEntry.pNext;
drh633e6d52008-07-28 19:34:53 +0000846 sqlite3ExprDelete(db, whenExpr);
drh9adf9ac2002-05-15 11:44:13 +0000847 return 1;
danielk1977c3f9bad2002-05-15 08:30:12 +0000848 }
drh35573352008-01-08 23:54:25 +0000849 sqlite3ExprIfFalse(pParse, whenExpr, endTrigger, SQLITE_JUMPIFNULL);
drh633e6d52008-07-28 19:34:53 +0000850 sqlite3ExprDelete(db, whenExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000851
danielk1977eecfb3e2006-01-10 12:31:39 +0000852 codeTriggerProgram(pParse, p->step_list, orconf);
danielk1977c3f9bad2002-05-15 08:30:12 +0000853
854 /* Pop the entry off the trigger stack */
drh67040462004-09-24 12:24:36 +0000855 pParse->trigStack = trigStackEntry.pNext;
danielk19774adee202004-05-08 08:23:19 +0000856 sqlite3AuthContextPop(&sContext);
danielk1977c3f9bad2002-05-15 08:30:12 +0000857
danielk19774adee202004-05-08 08:23:19 +0000858 sqlite3VdbeResolveLabel(pParse->pVdbe, endTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000859 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000860 }
danielk19778f2c54e2008-01-01 19:02:09 +0000861 if( piOldColMask ) *piOldColMask |= trigStackEntry.oldColMask;
862 if( piNewColMask ) *piNewColMask |= trigStackEntry.newColMask;
danielk1977c3f9bad2002-05-15 08:30:12 +0000863 return 0;
864}
drhb7f91642004-10-31 02:22:47 +0000865#endif /* !defined(SQLITE_OMIT_TRIGGER) */