blob: 82acfbfed6af026d0522a21ca0ec9cf2a0aeccdf [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**
shane5eff7cf2009-08-10 03:57:58 +000013** $Id: trigger.c,v 1.143 2009/08/10 03:57:58 shane 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
dand66c8302009-09-28 14:49:01 +000053 if( pParse->disableTriggers ){
54 return 0;
55 }
56
danielk19772f886d12009-02-28 10:47:41 +000057 if( pTmpSchema!=pTab->pSchema ){
58 HashElem *p;
59 for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
60 Trigger *pTrig = (Trigger *)sqliteHashData(p);
61 if( pTrig->pTabSchema==pTab->pSchema
62 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
63 ){
64 pTrig->pNext = (pList ? pList : pTab->pTrigger);
65 pList = pTrig;
66 }
67 }
68 }
69
70 return (pList ? pList : pTab->pTrigger);
71}
72
73/*
drhf0f258b2003-04-21 18:48:45 +000074** This is called by the parser when it sees a CREATE TRIGGER statement
75** up to the point of the BEGIN before the trigger actions. A Trigger
76** structure is generated based on the information available and stored
77** in pParse->pNewTrigger. After the trigger actions have been parsed, the
danielk19774adee202004-05-08 08:23:19 +000078** sqlite3FinishTrigger() function is called to complete the trigger
drhf0f258b2003-04-21 18:48:45 +000079** construction process.
drh9adf9ac2002-05-15 11:44:13 +000080*/
danielk19774adee202004-05-08 08:23:19 +000081void sqlite3BeginTrigger(
drh9adf9ac2002-05-15 11:44:13 +000082 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
danielk1977ef2cb632004-05-29 02:37:19 +000083 Token *pName1, /* The name of the trigger */
84 Token *pName2, /* The name of the trigger */
drh5cf590c2003-04-24 01:45:04 +000085 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
drh9adf9ac2002-05-15 11:44:13 +000086 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
danielk1977633ed082002-05-17 00:05:58 +000087 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
drhd24cc422003-03-27 12:51:24 +000088 SrcList *pTableName,/* The name of the table/view the trigger applies to */
drh9adf9ac2002-05-15 11:44:13 +000089 Expr *pWhen, /* WHEN clause */
drhfdd48a72006-09-11 23:45:48 +000090 int isTemp, /* True if the TEMPORARY keyword is present */
91 int noErr /* Suppress errors if the trigger already exists */
drh9adf9ac2002-05-15 11:44:13 +000092){
drh3d5f74b2009-08-06 17:43:31 +000093 Trigger *pTrigger = 0; /* The new trigger */
94 Table *pTab; /* Table that the trigger fires off of */
drhf0f258b2003-04-21 18:48:45 +000095 char *zName = 0; /* Name of the trigger */
drh3d5f74b2009-08-06 17:43:31 +000096 sqlite3 *db = pParse->db; /* The database connection */
danielk1977ef2cb632004-05-29 02:37:19 +000097 int iDb; /* The database to store the trigger in */
98 Token *pName; /* The unqualified db name */
drh3d5f74b2009-08-06 17:43:31 +000099 DbFixer sFix; /* State vector for the DB fixer */
100 int iTabDb; /* Index of the database holding pTab */
drhed6c8672003-01-12 18:02:16 +0000101
drh43617e92006-03-06 20:55:46 +0000102 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
103 assert( pName2!=0 );
drhaa78bec2008-12-09 03:55:14 +0000104 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
105 assert( op>0 && op<0xff );
danielk1977ef2cb632004-05-29 02:37:19 +0000106 if( isTemp ){
107 /* If TEMP was specified, then the trigger name may not be qualified. */
drh43617e92006-03-06 20:55:46 +0000108 if( pName2->n>0 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000109 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
110 goto trigger_cleanup;
111 }
112 iDb = 1;
113 pName = pName1;
114 }else{
115 /* Figure out the db that the the trigger will be created in */
116 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
117 if( iDb<0 ){
118 goto trigger_cleanup;
119 }
120 }
121
122 /* If the trigger name was unqualified, and the table is a temp table,
123 ** then set iDb to 1 to create the trigger in the temporary database.
124 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
125 ** exist, the error is caught by the block below.
drh9adf9ac2002-05-15 11:44:13 +0000126 */
drh17435752007-08-16 04:30:38 +0000127 if( !pTableName || db->mallocFailed ){
drh6f7adc82006-01-11 21:41:20 +0000128 goto trigger_cleanup;
129 }
danielk1977ef2cb632004-05-29 02:37:19 +0000130 pTab = sqlite3SrcListLookup(pParse, pTableName);
danielk1977da184232006-01-05 11:34:32 +0000131 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977ef2cb632004-05-29 02:37:19 +0000132 iDb = 1;
133 }
134
135 /* Ensure the table name matches database name and that the table exists */
drh17435752007-08-16 04:30:38 +0000136 if( db->mallocFailed ) goto trigger_cleanup;
drhd24cc422003-03-27 12:51:24 +0000137 assert( pTableName->nSrc==1 );
danielk1977ef2cb632004-05-29 02:37:19 +0000138 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) &&
139 sqlite3FixSrcList(&sFix, pTableName) ){
drh4312db52003-06-03 01:47:11 +0000140 goto trigger_cleanup;
drhf26e09c2003-05-31 16:21:12 +0000141 }
danielk1977ef2cb632004-05-29 02:37:19 +0000142 pTab = sqlite3SrcListLookup(pParse, pTableName);
143 if( !pTab ){
144 /* The table does not exist. */
drh3d5f74b2009-08-06 17:43:31 +0000145 if( db->init.iDb==1 ){
146 /* Ticket #3810.
147 ** Normally, whenever a table is dropped, all associated triggers are
148 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
149 ** and the table is dropped by a different database connection, the
150 ** trigger is not visible to the database connection that does the
151 ** drop so the trigger cannot be dropped. This results in an
152 ** "orphaned trigger" - a trigger whose associated table is missing.
153 */
154 db->init.orphanTrigger = 1;
155 }
drhd24cc422003-03-27 12:51:24 +0000156 goto trigger_cleanup;
157 }
drh4cbdda92006-06-14 19:00:20 +0000158 if( IsVirtual(pTab) ){
159 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
160 goto trigger_cleanup;
161 }
drhd24cc422003-03-27 12:51:24 +0000162
danielk1977d8123362004-06-12 09:25:12 +0000163 /* Check that the trigger name is not reserved and that no trigger of the
164 ** specified name exists */
drh17435752007-08-16 04:30:38 +0000165 zName = sqlite3NameFromToken(db, pName);
danielk1977d8123362004-06-12 09:25:12 +0000166 if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
167 goto trigger_cleanup;
168 }
drhaa78bec2008-12-09 03:55:14 +0000169 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
drhea678832008-12-10 19:26:22 +0000170 zName, sqlite3Strlen30(zName)) ){
drhfdd48a72006-09-11 23:45:48 +0000171 if( !noErr ){
172 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
173 }
drhe5f9c642003-01-13 23:27:31 +0000174 goto trigger_cleanup;
danielk1977c3f9bad2002-05-15 08:30:12 +0000175 }
danielk1977ef2cb632004-05-29 02:37:19 +0000176
177 /* Do not create a trigger on a system table */
drhdca76842004-12-07 14:06:13 +0000178 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk19774adee202004-05-08 08:23:19 +0000179 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
drhd24cc422003-03-27 12:51:24 +0000180 pParse->nErr++;
181 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40 +0000182 }
danielk1977ef2cb632004-05-29 02:37:19 +0000183
184 /* INSTEAD of triggers are only for views and views only support INSTEAD
185 ** of triggers.
186 */
187 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000188 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drhda93d232003-03-31 02:12:46 +0000189 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000190 goto trigger_cleanup;
191 }
danielk1977ef2cb632004-05-29 02:37:19 +0000192 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000193 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drhda93d232003-03-31 02:12:46 +0000194 " trigger on table: %S", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000195 goto trigger_cleanup;
196 }
danielk1977da184232006-01-05 11:34:32 +0000197 iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977ef2cb632004-05-29 02:37:19 +0000198
drhd24cc422003-03-27 12:51:24 +0000199#ifndef SQLITE_OMIT_AUTHORIZATION
200 {
201 int code = SQLITE_CREATE_TRIGGER;
danielk1977da184232006-01-05 11:34:32 +0000202 const char *zDb = db->aDb[iTabDb].zName;
drhe22a3342003-04-22 20:30:37 +0000203 const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
danielk1977da184232006-01-05 11:34:32 +0000204 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19 +0000205 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24 +0000206 goto trigger_cleanup;
207 }
danielk1977da184232006-01-05 11:34:32 +0000208 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24 +0000209 goto trigger_cleanup;
210 }
211 }
212#endif
danielk1977d702fcc2002-05-26 23:24:40 +0000213
danielk1977ef2cb632004-05-29 02:37:19 +0000214 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04 +0000215 ** cannot appear on views. So we might as well translate every
216 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
217 ** elsewhere.
218 */
danielk1977d702fcc2002-05-26 23:24:40 +0000219 if (tr_tm == TK_INSTEAD){
220 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12 +0000221 }
222
223 /* Build the Trigger object */
drh17435752007-08-16 04:30:38 +0000224 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19 +0000225 if( pTrigger==0 ) goto trigger_cleanup;
dan165921a2009-08-28 18:53:45 +0000226 pTrigger->zName = zName;
drhe5f9c642003-01-13 23:27:31 +0000227 zName = 0;
drh17435752007-08-16 04:30:38 +0000228 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32 +0000229 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48 +0000230 pTrigger->pTabSchema = pTab->pSchema;
drhaa78bec2008-12-09 03:55:14 +0000231 pTrigger->op = (u8)op;
drhdca76842004-12-07 14:06:13 +0000232 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
danielk19776ab3a2e2009-02-19 14:39:25 +0000233 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
drh17435752007-08-16 04:30:38 +0000234 pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
drhf0f258b2003-04-21 18:48:45 +0000235 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19 +0000236 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45 +0000237
238trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000239 sqlite3DbFree(db, zName);
240 sqlite3SrcListDelete(db, pTableName);
241 sqlite3IdListDelete(db, pColumns);
242 sqlite3ExprDelete(db, pWhen);
danielk1977d5d56522005-03-16 12:15:20 +0000243 if( !pParse->pNewTrigger ){
drh633e6d52008-07-28 19:34:53 +0000244 sqlite3DeleteTrigger(db, pTrigger);
danielk1977d5d56522005-03-16 12:15:20 +0000245 }else{
246 assert( pParse->pNewTrigger==pTrigger );
247 }
drhf0f258b2003-04-21 18:48:45 +0000248}
249
250/*
251** This routine is called after all of the trigger actions have been parsed
252** in order to complete the process of building the trigger.
253*/
danielk19774adee202004-05-08 08:23:19 +0000254void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45 +0000255 Parse *pParse, /* Parser context */
256 TriggerStep *pStepList, /* The triggered program */
257 Token *pAll /* Token that describes the complete CREATE TRIGGER */
258){
danielk19772f886d12009-02-28 10:47:41 +0000259 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
260 char *zName; /* Name of trigger */
261 sqlite3 *db = pParse->db; /* The database */
drhf26e09c2003-05-31 16:21:12 +0000262 DbFixer sFix;
danielk19772f886d12009-02-28 10:47:41 +0000263 int iDb; /* Database containing the trigger */
drhb7916a72009-05-27 10:31:29 +0000264 Token nameToken; /* Trigger name for error reporting */
drhf0f258b2003-04-21 18:48:45 +0000265
danielk1977bfb9e352005-01-24 13:03:32 +0000266 pTrig = pParse->pNewTrigger;
drhf0f258b2003-04-21 18:48:45 +0000267 pParse->pNewTrigger = 0;
drh9ab4c2e2009-05-09 00:18:38 +0000268 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
dan165921a2009-08-28 18:53:45 +0000269 zName = pTrig->zName;
danielk1977da184232006-01-05 11:34:32 +0000270 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32 +0000271 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53 +0000272 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32 +0000273 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53 +0000274 pStepList = pStepList->pNext;
275 }
dan165921a2009-08-28 18:53:45 +0000276 nameToken.z = pTrig->zName;
drhb7916a72009-05-27 10:31:29 +0000277 nameToken.n = sqlite3Strlen30(nameToken.z);
278 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken)
danielk1977bfb9e352005-01-24 13:03:32 +0000279 && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
drhf26e09c2003-05-31 16:21:12 +0000280 goto triggerfinish_cleanup;
281 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000282
283 /* if we are not initializing, and this trigger is not on a TEMP table,
drh9adf9ac2002-05-15 11:44:13 +0000284 ** build the sqlite_master entry
285 */
drh1d85d932004-02-14 23:05:52 +0000286 if( !db->init.busy ){
drhc977f7f2002-05-21 11:38:11 +0000287 Vdbe *v;
drh2d401ab2008-01-10 23:50:11 +0000288 char *z;
danielk1977c3f9bad2002-05-15 08:30:12 +0000289
290 /* Make an entry in the sqlite_master table */
danielk19774adee202004-05-08 08:23:19 +0000291 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000292 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000293 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11 +0000294 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
295 sqlite3NestedParse(pParse,
296 "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
danielk19772f886d12009-02-28 10:47:41 +0000297 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
drh2d401ab2008-01-10 23:50:11 +0000298 pTrig->table, z);
drh633e6d52008-07-28 19:34:53 +0000299 sqlite3DbFree(db, z);
drh9cbf3422008-01-17 16:22:13 +0000300 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +0000301 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, sqlite3MPrintf(
danielk19772f886d12009-02-28 10:47:41 +0000302 db, "type='trigger' AND name='%q'", zName), P4_DYNAMIC
danielk19771e536952007-08-16 10:09:01 +0000303 );
danielk1977c3f9bad2002-05-15 08:30:12 +0000304 }
305
drh956bc922004-07-24 17:38:29 +0000306 if( db->init.busy ){
danielk19772f886d12009-02-28 10:47:41 +0000307 Trigger *pLink = pTrig;
308 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
309 pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);
310 if( pTrig ){
drhf3a65f72007-08-22 20:18:21 +0000311 db->mallocFailed = 1;
danielk19772f886d12009-02-28 10:47:41 +0000312 }else if( pLink->pSchema==pLink->pTabSchema ){
313 Table *pTab;
drha83ccca2009-04-28 13:01:09 +0000314 int n = sqlite3Strlen30(pLink->table);
danielk19772f886d12009-02-28 10:47:41 +0000315 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n);
316 assert( pTab!=0 );
317 pLink->pNext = pTab->pTrigger;
318 pTab->pTrigger = pLink;
danielk1977d5d56522005-03-16 12:15:20 +0000319 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000320 }
321
drhf0f258b2003-04-21 18:48:45 +0000322triggerfinish_cleanup:
drh633e6d52008-07-28 19:34:53 +0000323 sqlite3DeleteTrigger(db, pTrig);
danielk1977bfb9e352005-01-24 13:03:32 +0000324 assert( !pParse->pNewTrigger );
drh633e6d52008-07-28 19:34:53 +0000325 sqlite3DeleteTriggerStep(db, pStepList);
drh4b59ab52002-08-24 18:24:51 +0000326}
danielk1977c3f9bad2002-05-15 08:30:12 +0000327
drh4b59ab52002-08-24 18:24:51 +0000328/*
drhc977f7f2002-05-21 11:38:11 +0000329** Turn a SELECT statement (that the pSelect parameter points to) into
330** a trigger step. Return a pointer to a TriggerStep structure.
331**
332** The parser calls this routine when it finds a SELECT statement in
333** body of a TRIGGER.
334*/
drh17435752007-08-16 04:30:38 +0000335TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
336 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000337 if( pTriggerStep==0 ) {
drh633e6d52008-07-28 19:34:53 +0000338 sqlite3SelectDelete(db, pSelect);
danielk19772e588c72005-12-09 14:25:08 +0000339 return 0;
340 }
danielk1977633ed082002-05-17 00:05:58 +0000341 pTriggerStep->op = TK_SELECT;
342 pTriggerStep->pSelect = pSelect;
343 pTriggerStep->orconf = OE_Default;
drhb7916a72009-05-27 10:31:29 +0000344 return pTriggerStep;
345}
danielk1977c3f9bad2002-05-15 08:30:12 +0000346
drhb7916a72009-05-27 10:31:29 +0000347/*
348** Allocate space to hold a new trigger step. The allocated space
349** holds both the TriggerStep object and the TriggerStep.target.z string.
350**
351** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
352*/
353static TriggerStep *triggerStepAllocate(
354 sqlite3 *db, /* Database connection */
shane5eff7cf2009-08-10 03:57:58 +0000355 u8 op, /* Trigger opcode */
drhb7916a72009-05-27 10:31:29 +0000356 Token *pName /* The target name */
357){
358 TriggerStep *pTriggerStep;
359
360 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n);
361 if( pTriggerStep ){
362 char *z = (char*)&pTriggerStep[1];
363 memcpy(z, pName->z, pName->n);
364 pTriggerStep->target.z = z;
365 pTriggerStep->target.n = pName->n;
366 pTriggerStep->op = op;
367 }
danielk1977633ed082002-05-17 00:05:58 +0000368 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000369}
370
drhc977f7f2002-05-21 11:38:11 +0000371/*
372** Build a trigger step out of an INSERT statement. Return a pointer
373** to the new trigger step.
374**
375** The parser calls this routine when it sees an INSERT inside the
376** body of a trigger.
377*/
danielk19774adee202004-05-08 08:23:19 +0000378TriggerStep *sqlite3TriggerInsertStep(
drh17435752007-08-16 04:30:38 +0000379 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000380 Token *pTableName, /* Name of the table into which we insert */
381 IdList *pColumn, /* List of columns in pTableName to insert into */
382 ExprList *pEList, /* The VALUE clause: a list of values to be inserted */
383 Select *pSelect, /* A SELECT statement that supplies values */
shane5eff7cf2009-08-10 03:57:58 +0000384 u8 orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
danielk1977633ed082002-05-17 00:05:58 +0000385){
drhf3a65f72007-08-22 20:18:21 +0000386 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000387
danielk1977633ed082002-05-17 00:05:58 +0000388 assert(pEList == 0 || pSelect == 0);
drhf3a65f72007-08-22 20:18:21 +0000389 assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000390
drhb7916a72009-05-27 10:31:29 +0000391 pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
danielk1977d5d56522005-03-16 12:15:20 +0000392 if( pTriggerStep ){
drh33e619f2009-05-28 01:00:55 +0000393 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
danielk1977d5d56522005-03-16 12:15:20 +0000394 pTriggerStep->pIdList = pColumn;
drh33e619f2009-05-28 01:00:55 +0000395 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
danielk1977d5d56522005-03-16 12:15:20 +0000396 pTriggerStep->orconf = orconf;
danielk1977d5d56522005-03-16 12:15:20 +0000397 }else{
drh633e6d52008-07-28 19:34:53 +0000398 sqlite3IdListDelete(db, pColumn);
danielk1977d5d56522005-03-16 12:15:20 +0000399 }
drh33e619f2009-05-28 01:00:55 +0000400 sqlite3ExprListDelete(db, pEList);
401 sqlite3SelectDelete(db, pSelect);
danielk1977c3f9bad2002-05-15 08:30:12 +0000402
danielk1977633ed082002-05-17 00:05:58 +0000403 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000404}
405
drhc977f7f2002-05-21 11:38:11 +0000406/*
407** Construct a trigger step that implements an UPDATE statement and return
408** a pointer to that trigger step. The parser calls this routine when it
409** sees an UPDATE statement inside the body of a CREATE TRIGGER.
410*/
danielk19774adee202004-05-08 08:23:19 +0000411TriggerStep *sqlite3TriggerUpdateStep(
drh17435752007-08-16 04:30:38 +0000412 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000413 Token *pTableName, /* Name of the table to be updated */
414 ExprList *pEList, /* The SET clause: list of column and new values */
415 Expr *pWhere, /* The WHERE clause */
shane5eff7cf2009-08-10 03:57:58 +0000416 u8 orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
drhc977f7f2002-05-21 11:38:11 +0000417){
drhb7916a72009-05-27 10:31:29 +0000418 TriggerStep *pTriggerStep;
419
420 pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
drh33e619f2009-05-28 01:00:55 +0000421 if( pTriggerStep ){
422 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
423 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
424 pTriggerStep->orconf = orconf;
drh0e3a6f32007-03-30 20:40:34 +0000425 }
drh33e619f2009-05-28 01:00:55 +0000426 sqlite3ExprListDelete(db, pEList);
427 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000428 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000429}
430
drhc977f7f2002-05-21 11:38:11 +0000431/*
432** Construct a trigger step that implements a DELETE statement and return
433** a pointer to that trigger step. The parser calls this routine when it
434** sees a DELETE statement inside the body of a CREATE TRIGGER.
435*/
drh17435752007-08-16 04:30:38 +0000436TriggerStep *sqlite3TriggerDeleteStep(
437 sqlite3 *db, /* Database connection */
438 Token *pTableName, /* The table from which rows are deleted */
439 Expr *pWhere /* The WHERE clause */
440){
drhb7916a72009-05-27 10:31:29 +0000441 TriggerStep *pTriggerStep;
442
443 pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
drh33e619f2009-05-28 01:00:55 +0000444 if( pTriggerStep ){
445 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
446 pTriggerStep->orconf = OE_Default;
drhb63a53d2007-03-31 01:34:44 +0000447 }
drh33e619f2009-05-28 01:00:55 +0000448 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000449 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000450}
451
danielk1977633ed082002-05-17 00:05:58 +0000452/*
453** Recursively delete a Trigger structure
454*/
drh633e6d52008-07-28 19:34:53 +0000455void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
drhf0f258b2003-04-21 18:48:45 +0000456 if( pTrigger==0 ) return;
drh633e6d52008-07-28 19:34:53 +0000457 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
dan165921a2009-08-28 18:53:45 +0000458 sqlite3DbFree(db, pTrigger->zName);
drh633e6d52008-07-28 19:34:53 +0000459 sqlite3DbFree(db, pTrigger->table);
460 sqlite3ExprDelete(db, pTrigger->pWhen);
461 sqlite3IdListDelete(db, pTrigger->pColumns);
drh633e6d52008-07-28 19:34:53 +0000462 sqlite3DbFree(db, pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000463}
464
465/*
danielk19778a414492004-06-29 08:59:35 +0000466** This function is called to drop a trigger from the database schema.
467**
468** This may be called directly from the parser and therefore identifies
469** the trigger by name. The sqlite3DropTriggerPtr() routine does the
470** same job as this routine except it takes a pointer to the trigger
471** instead of the trigger name.
472**/
drhfdd48a72006-09-11 23:45:48 +0000473void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000474 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000475 int i;
476 const char *zDb;
477 const char *zName;
478 int nName;
drh9bb575f2004-09-06 17:24:11 +0000479 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000480
drh17435752007-08-16 04:30:38 +0000481 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000482 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000483 goto drop_trigger_cleanup;
484 }
danielk19778e227872004-06-07 07:52:17 +0000485
drhd24cc422003-03-27 12:51:24 +0000486 assert( pName->nSrc==1 );
487 zDb = pName->a[0].zDatabase;
488 zName = pName->a[0].zName;
drhea678832008-12-10 19:26:22 +0000489 nName = sqlite3Strlen30(zName);
danielk197753c0f742005-03-29 03:10:59 +0000490 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000491 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000492 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
drhe4df0e72006-03-29 00:24:06 +0000493 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
drhd24cc422003-03-27 12:51:24 +0000494 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000495 }
drhd24cc422003-03-27 12:51:24 +0000496 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000497 if( !noErr ){
498 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
499 }
drhd24cc422003-03-27 12:51:24 +0000500 goto drop_trigger_cleanup;
501 }
drh74161702006-02-24 02:53:49 +0000502 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000503
504drop_trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000505 sqlite3SrcListDelete(db, pName);
drh79a519c2003-05-17 19:04:03 +0000506}
507
508/*
drh956bc922004-07-24 17:38:29 +0000509** Return a pointer to the Table structure for the table that a trigger
510** is set on.
511*/
drh74161702006-02-24 02:53:49 +0000512static Table *tableOfTrigger(Trigger *pTrigger){
drha83ccca2009-04-28 13:01:09 +0000513 int n = sqlite3Strlen30(pTrigger->table);
danielk1977aaf22682006-01-06 15:03:48 +0000514 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
drh956bc922004-07-24 17:38:29 +0000515}
516
517
518/*
drh74161702006-02-24 02:53:49 +0000519** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000520*/
drh74161702006-02-24 02:53:49 +0000521void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000522 Table *pTable;
523 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000524 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000525 int iDb;
drh79a519c2003-05-17 19:04:03 +0000526
danielk1977da184232006-01-05 11:34:32 +0000527 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000528 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000529 pTable = tableOfTrigger(pTrigger);
drh43617e92006-03-06 20:55:46 +0000530 assert( pTable );
danielk1977da184232006-01-05 11:34:32 +0000531 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000532#ifndef SQLITE_OMIT_AUTHORIZATION
533 {
534 int code = SQLITE_DROP_TRIGGER;
drh956bc922004-07-24 17:38:29 +0000535 const char *zDb = db->aDb[iDb].zName;
536 const char *zTab = SCHEMA_TABLE(iDb);
537 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
dan2bd93512009-08-31 08:22:46 +0000538 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
danielk19774adee202004-05-08 08:23:19 +0000539 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000540 return;
drhe5f9c642003-01-13 23:27:31 +0000541 }
drhed6c8672003-01-12 18:02:16 +0000542 }
drhe5f9c642003-01-13 23:27:31 +0000543#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000544
drhf0f258b2003-04-21 18:48:45 +0000545 /* Generate code to destroy the database record of the trigger.
546 */
drh43617e92006-03-06 20:55:46 +0000547 assert( pTable!=0 );
548 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drhf0f258b2003-04-21 18:48:45 +0000549 int base;
drh57196282004-10-06 15:41:16 +0000550 static const VdbeOpList dropTrigger[] = {
drh9b1b01b2003-08-16 12:37:51 +0000551 { OP_Rewind, 0, ADDR(9), 0},
drh1db639c2008-01-17 02:36:28 +0000552 { OP_String8, 0, 1, 0}, /* 1 */
553 { OP_Column, 0, 1, 2},
554 { OP_Ne, 2, ADDR(8), 1},
555 { OP_String8, 0, 1, 0}, /* 4: "trigger" */
556 { OP_Column, 0, 0, 2},
557 { OP_Ne, 2, ADDR(8), 1},
drhf0f258b2003-04-21 18:48:45 +0000558 { OP_Delete, 0, 0, 0},
drh9b1b01b2003-08-16 12:37:51 +0000559 { OP_Next, 0, ADDR(1), 0}, /* 8 */
drhf0f258b2003-04-21 18:48:45 +0000560 };
561
drh956bc922004-07-24 17:38:29 +0000562 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk1977c00da102006-01-07 13:21:04 +0000563 sqlite3OpenMasterTable(pParse, iDb);
danielk19774adee202004-05-08 08:23:19 +0000564 base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger);
dan165921a2009-08-28 18:53:45 +0000565 sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, 0);
drh24003452008-01-03 01:28:59 +0000566 sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
drh9cbf3422008-01-17 16:22:13 +0000567 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +0000568 sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
dan165921a2009-08-28 18:53:45 +0000569 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
danielk19776ab3a2e2009-02-19 14:39:25 +0000570 if( pParse->nMem<3 ){
571 pParse->nMem = 3;
572 }
drhf0f258b2003-04-21 18:48:45 +0000573 }
drh956bc922004-07-24 17:38:29 +0000574}
drhf0f258b2003-04-21 18:48:45 +0000575
drh956bc922004-07-24 17:38:29 +0000576/*
577** Remove a trigger from the hash tables of the sqlite* pointer.
578*/
579void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
danielk19772f886d12009-02-28 10:47:41 +0000580 Hash *pHash = &(db->aDb[iDb].pSchema->trigHash);
drh956bc922004-07-24 17:38:29 +0000581 Trigger *pTrigger;
danielk19772f886d12009-02-28 10:47:41 +0000582 pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0);
drh9ab4c2e2009-05-09 00:18:38 +0000583 if( ALWAYS(pTrigger) ){
danielk19772f886d12009-02-28 10:47:41 +0000584 if( pTrigger->pSchema==pTrigger->pTabSchema ){
585 Table *pTab = tableOfTrigger(pTrigger);
586 Trigger **pp;
587 for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
588 *pp = (*pp)->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +0000589 }
drh633e6d52008-07-28 19:34:53 +0000590 sqlite3DeleteTrigger(db, pTrigger);
drh956bc922004-07-24 17:38:29 +0000591 db->flags |= SQLITE_InternChanges;
danielk1977c3f9bad2002-05-15 08:30:12 +0000592 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000593}
594
drhc977f7f2002-05-21 11:38:11 +0000595/*
596** pEList is the SET clause of an UPDATE statement. Each entry
597** in pEList is of the format <id>=<expr>. If any of the entries
598** in pEList have an <id> which matches an identifier in pIdList,
599** then return TRUE. If pIdList==NULL, then it is considered a
600** wildcard that matches anything. Likewise if pEList==NULL then
601** it matches anything so always return true. Return false only
602** if there is no match.
603*/
drh9ab4c2e2009-05-09 00:18:38 +0000604static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000605 int e;
drh9ab4c2e2009-05-09 00:18:38 +0000606 if( pIdList==0 || NEVER(pEList==0) ) return 1;
drhad2d8302002-05-24 20:31:36 +0000607 for(e=0; e<pEList->nExpr; e++){
danielk19774adee202004-05-08 08:23:19 +0000608 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000609 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000610 return 0;
611}
612
danielk1977c3f9bad2002-05-15 08:30:12 +0000613/*
danielk19772f886d12009-02-28 10:47:41 +0000614** Return a list of all triggers on table pTab if there exists at least
615** one trigger that must be fired when an operation of type 'op' is
616** performed on the table, and, if that operation is an UPDATE, if at
617** least one of the columns in pChanges is being modified.
drhdca76842004-12-07 14:06:13 +0000618*/
danielk19772f886d12009-02-28 10:47:41 +0000619Trigger *sqlite3TriggersExist(
620 Parse *pParse, /* Parse context */
drhdca76842004-12-07 14:06:13 +0000621 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000622 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
danielk19772f886d12009-02-28 10:47:41 +0000623 ExprList *pChanges, /* Columns that change in an UPDATE statement */
624 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
drhc977f7f2002-05-21 11:38:11 +0000625){
drhdca76842004-12-07 14:06:13 +0000626 int mask = 0;
danielk19772f886d12009-02-28 10:47:41 +0000627 Trigger *pList = sqlite3TriggerList(pParse, pTab);
628 Trigger *p;
629 assert( pList==0 || IsVirtual(pTab)==0 );
630 for(p=pList; p; p=p->pNext){
drh9ab4c2e2009-05-09 00:18:38 +0000631 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
danielk19772f886d12009-02-28 10:47:41 +0000632 mask |= p->tr_tm;
danielk1977c3f9bad2002-05-15 08:30:12 +0000633 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000634 }
danielk19772f886d12009-02-28 10:47:41 +0000635 if( pMask ){
636 *pMask = mask;
637 }
638 return (mask ? pList : 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000639}
640
drhc977f7f2002-05-21 11:38:11 +0000641/*
drhf26e09c2003-05-31 16:21:12 +0000642** Convert the pStep->target token into a SrcList and return a pointer
643** to that SrcList.
644**
645** This routine adds a specific database name, if needed, to the target when
646** forming the SrcList. This prevents a trigger in one database from
647** referring to a target in another database. An exception is when the
648** trigger is in TEMP in which case it can refer to any other database it
649** wants.
650*/
651static SrcList *targetSrcList(
652 Parse *pParse, /* The parsing context */
653 TriggerStep *pStep /* The trigger containing the target token */
654){
drhf26e09c2003-05-31 16:21:12 +0000655 int iDb; /* Index of the database to use */
656 SrcList *pSrc; /* SrcList to be returned */
657
drhb7916a72009-05-27 10:31:29 +0000658 pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
659 if( pSrc ){
660 assert( pSrc->nSrc>0 );
661 assert( pSrc->a!=0 );
662 iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
663 if( iDb==0 || iDb>=2 ){
664 sqlite3 *db = pParse->db;
665 assert( iDb<pParse->db->nDb );
666 pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
667 }
drhf26e09c2003-05-31 16:21:12 +0000668 }
669 return pSrc;
670}
671
672/*
dan65a7cd12009-09-01 12:16:01 +0000673** Generate VDBE code for the statements inside the body of a single
674** trigger.
drhc977f7f2002-05-21 11:38:11 +0000675*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000676static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000677 Parse *pParse, /* The parser context */
678 TriggerStep *pStepList, /* List of statements inside the trigger body */
dan65a7cd12009-09-01 12:16:01 +0000679 int orconf /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000680){
dan65a7cd12009-09-01 12:16:01 +0000681 TriggerStep *pStep;
drh344737f2004-09-19 00:50:20 +0000682 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000683 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000684
dan65a7cd12009-09-01 12:16:01 +0000685 assert( pParse->pTriggerTab && pParse->pToplevel );
686 assert( pStepList );
drh344737f2004-09-19 00:50:20 +0000687 assert( v!=0 );
dan65a7cd12009-09-01 12:16:01 +0000688 for(pStep=pStepList; pStep; pStep=pStep->pNext){
dan165921a2009-08-28 18:53:45 +0000689 /* Figure out the ON CONFLICT policy that will be used for this step
690 ** of the trigger program. If the statement that caused this trigger
691 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
692 ** the ON CONFLICT policy that was specified as part of the trigger
693 ** step statement. Example:
694 **
695 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
696 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
697 ** END;
698 **
699 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
700 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
701 */
shanecea72b22009-09-07 04:38:36 +0000702 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
dan165921a2009-08-28 18:53:45 +0000703
704 switch( pStep->op ){
danielk1977633ed082002-05-17 00:05:58 +0000705 case TK_UPDATE: {
dan165921a2009-08-28 18:53:45 +0000706 sqlite3Update(pParse,
707 targetSrcList(pParse, pStep),
708 sqlite3ExprListDup(db, pStep->pExprList, 0),
709 sqlite3ExprDup(db, pStep->pWhere, 0),
dan65a7cd12009-09-01 12:16:01 +0000710 pParse->eOrconf
dan165921a2009-08-28 18:53:45 +0000711 );
danielk1977633ed082002-05-17 00:05:58 +0000712 break;
713 }
714 case TK_INSERT: {
dan165921a2009-08-28 18:53:45 +0000715 sqlite3Insert(pParse,
716 targetSrcList(pParse, pStep),
717 sqlite3ExprListDup(db, pStep->pExprList, 0),
718 sqlite3SelectDup(db, pStep->pSelect, 0),
719 sqlite3IdListDup(db, pStep->pIdList),
dan65a7cd12009-09-01 12:16:01 +0000720 pParse->eOrconf
dan165921a2009-08-28 18:53:45 +0000721 );
danielk1977633ed082002-05-17 00:05:58 +0000722 break;
723 }
724 case TK_DELETE: {
dan165921a2009-08-28 18:53:45 +0000725 sqlite3DeleteFrom(pParse,
726 targetSrcList(pParse, pStep),
727 sqlite3ExprDup(db, pStep->pWhere, 0)
728 );
danielk1977633ed082002-05-17 00:05:58 +0000729 break;
730 }
dan165921a2009-08-28 18:53:45 +0000731 default: assert( pStep->op==TK_SELECT ); {
732 SelectDest sDest;
733 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
734 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
735 sqlite3Select(pParse, pSelect, &sDest);
736 sqlite3SelectDelete(db, pSelect);
drh9ab4c2e2009-05-09 00:18:38 +0000737 break;
738 }
danielk1977633ed082002-05-17 00:05:58 +0000739 }
dan76d462e2009-08-30 11:42:51 +0000740 if( pStep->op!=TK_SELECT ){
drhb7f1d9a2009-09-08 02:27:58 +0000741 sqlite3VdbeAddOp0(v, OP_ResetCount);
dan76d462e2009-08-30 11:42:51 +0000742 }
danielk1977633ed082002-05-17 00:05:58 +0000743 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000744
danielk1977633ed082002-05-17 00:05:58 +0000745 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000746}
747
dan165921a2009-08-28 18:53:45 +0000748#ifdef SQLITE_DEBUG
749/*
750** This function is used to add VdbeComment() annotations to a VDBE
751** program. It is not used in production code, only for debugging.
752*/
753static const char *onErrorText(int onError){
754 switch( onError ){
755 case OE_Abort: return "abort";
756 case OE_Rollback: return "rollback";
757 case OE_Fail: return "fail";
758 case OE_Replace: return "replace";
759 case OE_Ignore: return "ignore";
760 case OE_Default: return "default";
761 }
762 return "n/a";
763}
764#endif
765
766/*
767** Parse context structure pFrom has just been used to create a sub-vdbe
768** (trigger program). If an error has occurred, transfer error information
769** from pFrom to pTo.
770*/
771static void transferParseError(Parse *pTo, Parse *pFrom){
772 assert( pFrom->zErrMsg==0 || pFrom->nErr );
773 assert( pTo->zErrMsg==0 || pTo->nErr );
774 if( pTo->nErr==0 ){
775 pTo->zErrMsg = pFrom->zErrMsg;
776 pTo->nErr = pFrom->nErr;
777 }else{
778 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
779 }
780}
781
dan65a7cd12009-09-01 12:16:01 +0000782/*
783** Create and populate a new TriggerPrg object with a sub-program
784** implementing trigger pTrigger with ON CONFLICT policy orconf.
785*/
dan2832ad42009-08-31 15:27:27 +0000786static TriggerPrg *codeRowTrigger(
dan165921a2009-08-28 18:53:45 +0000787 Parse *pParse, /* Current parse context */
788 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +0000789 Table *pTab, /* The table pTrigger is attached to */
790 int orconf /* ON CONFLICT policy to code trigger program with */
dan165921a2009-08-28 18:53:45 +0000791){
dan65a7cd12009-09-01 12:16:01 +0000792 Parse *pTop = sqlite3ParseToplevel(pParse);
793 sqlite3 *db = pParse->db; /* Database handle */
794 TriggerPrg *pPrg; /* Value to return */
dan165921a2009-08-28 18:53:45 +0000795 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
796 Vdbe *v; /* Temporary VM */
dan165921a2009-08-28 18:53:45 +0000797 NameContext sNC; /* Name context for sub-vdbe */
798 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
799 Parse *pSubParse; /* Parse context for sub-vdbe */
800 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
801
dan1da40a32009-09-19 17:00:31 +0000802 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dan65a7cd12009-09-01 12:16:01 +0000803
804 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
805 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
806 ** list of the top-level Parse object sooner rather than later. */
dan2832ad42009-08-31 15:27:27 +0000807 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
808 if( !pPrg ) return 0;
dan65a7cd12009-09-01 12:16:01 +0000809 pPrg->pNext = pTop->pTriggerPrg;
810 pTop->pTriggerPrg = pPrg;
dan2832ad42009-08-31 15:27:27 +0000811 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
dan165921a2009-08-28 18:53:45 +0000812 if( !pProgram ) return 0;
813 pProgram->nRef = 1;
dan2832ad42009-08-31 15:27:27 +0000814 pPrg->pTrigger = pTrigger;
815 pPrg->orconf = orconf;
drh3991bb02009-09-17 00:41:19 +0000816 pPrg->oldmask = 0xffffffff;
dan165921a2009-08-28 18:53:45 +0000817
dan65a7cd12009-09-01 12:16:01 +0000818 /* Allocate and populate a new Parse context to use for coding the
819 ** trigger sub-program. */
820 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
821 if( !pSubParse ) return 0;
dan165921a2009-08-28 18:53:45 +0000822 memset(&sNC, 0, sizeof(sNC));
823 sNC.pParse = pSubParse;
824 pSubParse->db = db;
825 pSubParse->pTriggerTab = pTab;
dan65a7cd12009-09-01 12:16:01 +0000826 pSubParse->pToplevel = pTop;
dan2bd93512009-08-31 08:22:46 +0000827 pSubParse->zAuthContext = pTrigger->zName;
dan65a7cd12009-09-01 12:16:01 +0000828 pSubParse->eTriggerOp = pTrigger->op;
dan165921a2009-08-28 18:53:45 +0000829
830 v = sqlite3GetVdbe(pSubParse);
831 if( v ){
dan76d462e2009-08-30 11:42:51 +0000832 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
833 pTrigger->zName, onErrorText(orconf),
dan165921a2009-08-28 18:53:45 +0000834 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
dan65a7cd12009-09-01 12:16:01 +0000835 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
836 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
837 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
dan76d462e2009-08-30 11:42:51 +0000838 pTab->zName
dan165921a2009-08-28 18:53:45 +0000839 ));
dan76d462e2009-08-30 11:42:51 +0000840#ifndef SQLITE_OMIT_TRACE
841 sqlite3VdbeChangeP4(v, -1,
842 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
843 );
844#endif
dan165921a2009-08-28 18:53:45 +0000845
dan65a7cd12009-09-01 12:16:01 +0000846 /* If one was specified, code the WHEN clause. If it evaluates to false
847 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
848 ** OP_Halt inserted at the end of the program. */
dan165921a2009-08-28 18:53:45 +0000849 if( pTrigger->pWhen ){
dan165921a2009-08-28 18:53:45 +0000850 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
dan523a0872009-08-31 05:23:32 +0000851 if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
852 && db->mallocFailed==0
853 ){
dan165921a2009-08-28 18:53:45 +0000854 iEndTrigger = sqlite3VdbeMakeLabel(v);
855 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
856 }
857 sqlite3ExprDelete(db, pWhen);
858 }
859
860 /* Code the trigger program into the sub-vdbe. */
dan76d462e2009-08-30 11:42:51 +0000861 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
dan65a7cd12009-09-01 12:16:01 +0000862
863 /* Insert an OP_Halt at the end of the sub-program. */
dan165921a2009-08-28 18:53:45 +0000864 if( iEndTrigger ){
865 sqlite3VdbeResolveLabel(v, iEndTrigger);
866 }
867 sqlite3VdbeAddOp0(v, OP_Halt);
dan76d462e2009-08-30 11:42:51 +0000868 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
869
dan165921a2009-08-28 18:53:45 +0000870 transferParseError(pParse, pSubParse);
dan523a0872009-08-31 05:23:32 +0000871 if( db->mallocFailed==0 ){
dan65a7cd12009-09-01 12:16:01 +0000872 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
dan523a0872009-08-31 05:23:32 +0000873 }
dan165921a2009-08-28 18:53:45 +0000874 pProgram->nMem = pSubParse->nMem;
875 pProgram->nCsr = pSubParse->nTab;
876 pProgram->token = (void *)pTrigger;
dan2832ad42009-08-31 15:27:27 +0000877 pPrg->oldmask = pSubParse->oldmask;
dan165921a2009-08-28 18:53:45 +0000878 sqlite3VdbeDelete(v);
dan165921a2009-08-28 18:53:45 +0000879 }
dan65a7cd12009-09-01 12:16:01 +0000880
881 assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
882 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
dan165921a2009-08-28 18:53:45 +0000883 sqlite3StackFree(db, pSubParse);
884
dan2832ad42009-08-31 15:27:27 +0000885 return pPrg;
dan165921a2009-08-28 18:53:45 +0000886}
887
dan65a7cd12009-09-01 12:16:01 +0000888/*
889** Return a pointer to a TriggerPrg object containing the sub-program for
890** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
891** TriggerPrg object exists, a new object is allocated and populated before
892** being returned.
893*/
dan2832ad42009-08-31 15:27:27 +0000894static TriggerPrg *getRowTrigger(
dan65a7cd12009-09-01 12:16:01 +0000895 Parse *pParse, /* Current parse context */
dan165921a2009-08-28 18:53:45 +0000896 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +0000897 Table *pTab, /* The table trigger pTrigger is attached to */
898 int orconf /* ON CONFLICT algorithm. */
dan165921a2009-08-28 18:53:45 +0000899){
dan65a7cd12009-09-01 12:16:01 +0000900 Parse *pRoot = sqlite3ParseToplevel(pParse);
dan2832ad42009-08-31 15:27:27 +0000901 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +0000902
dan1da40a32009-09-19 17:00:31 +0000903 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dan165921a2009-08-28 18:53:45 +0000904
905 /* It may be that this trigger has already been coded (or is in the
906 ** process of being coded). If this is the case, then an entry with
dan2832ad42009-08-31 15:27:27 +0000907 ** a matching TriggerPrg.pTrigger field will be present somewhere
908 ** in the Parse.pTriggerPrg list. Search for such an entry. */
dan2832ad42009-08-31 15:27:27 +0000909 for(pPrg=pRoot->pTriggerPrg;
910 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
911 pPrg=pPrg->pNext
dan165921a2009-08-28 18:53:45 +0000912 );
913
dan65a7cd12009-09-01 12:16:01 +0000914 /* If an existing TriggerPrg could not be located, create a new one. */
dan2832ad42009-08-31 15:27:27 +0000915 if( !pPrg ){
dan65a7cd12009-09-01 12:16:01 +0000916 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
dan165921a2009-08-28 18:53:45 +0000917 }
918
dan2832ad42009-08-31 15:27:27 +0000919 return pPrg;
dan165921a2009-08-28 18:53:45 +0000920}
921
dan94d7f502009-09-24 09:05:49 +0000922/*
923** Generate code for the trigger program associated with trigger p on
924** table pTab. The reg, orconf and ignoreJump parameters passed to this
925** function are the same as those described in the header function for
926** sqlite3CodeRowTrigger()
927*/
dan1da40a32009-09-19 17:00:31 +0000928void sqlite3CodeRowTriggerDirect(
929 Parse *pParse, /* Parse context */
930 Trigger *p, /* Trigger to code */
931 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +0000932 int reg, /* Reg array containing OLD.* and NEW.* values */
dan1da40a32009-09-19 17:00:31 +0000933 int orconf, /* ON CONFLICT policy */
934 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
935){
936 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
937 TriggerPrg *pPrg;
938 pPrg = getRowTrigger(pParse, p, pTab, orconf);
939 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
940
941 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
942 ** is a pointer to the sub-vdbe containing the trigger program. */
943 if( pPrg ){
dan94d7f502009-09-24 09:05:49 +0000944 sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem);
dan1da40a32009-09-19 17:00:31 +0000945 pPrg->pProgram->nRef++;
946 sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
947 VdbeComment(
948 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
949
950 /* Set the P5 operand of the OP_Program instruction to non-zero if
951 ** recursive invocation of this trigger program is disallowed. Recursive
952 ** invocation is disallowed if (a) the sub-program is really a trigger,
953 ** not a foreign key action, and (b) the flag to enable recursive triggers
954 ** is clear. */
drhfcb9f7a2009-10-13 19:19:23 +0000955 sqlite3VdbeChangeP5(v, (u8)(p->zName && !(pParse->db->flags&SQLITE_RecTriggers)));
dan1da40a32009-09-19 17:00:31 +0000956 }
957}
958
danielk1977633ed082002-05-17 00:05:58 +0000959/*
dan94d7f502009-09-24 09:05:49 +0000960** This is called to code the required FOR EACH ROW triggers for an operation
961** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
962** is given by the op paramater. The tr_tm parameter determines whether the
963** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
964** parameter pChanges is passed the list of columns being modified.
danielk1977633ed082002-05-17 00:05:58 +0000965**
dan94d7f502009-09-24 09:05:49 +0000966** If there are no triggers that fire at the specified time for the specified
967** operation on pTab, this function is a no-op.
drhc977f7f2002-05-21 11:38:11 +0000968**
dan94d7f502009-09-24 09:05:49 +0000969** The reg argument is the address of the first in an array of registers
970** that contain the values substituted for the new.* and old.* references
971** in the trigger program. If N is the number of columns in table pTab
972** (a copy of pTab->nCol), then registers are populated as follows:
drhc977f7f2002-05-21 11:38:11 +0000973**
dan94d7f502009-09-24 09:05:49 +0000974** Register Contains
975** ------------------------------------------------------
976** reg+0 OLD.rowid
977** reg+1 OLD.* value of left-most column of pTab
978** ... ...
979** reg+N OLD.* value of right-most column of pTab
980** reg+N+1 NEW.rowid
981** reg+N+2 OLD.* value of left-most column of pTab
982** ... ...
983** reg+N+N+1 NEW.* value of right-most column of pTab
drhc977f7f2002-05-21 11:38:11 +0000984**
dan94d7f502009-09-24 09:05:49 +0000985** For ON DELETE triggers, the registers containing the NEW.* values will
986** never be accessed by the trigger program, so they are not allocated or
987** populated by the caller (there is no data to populate them with anyway).
988** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
989** are never accessed, and so are not allocated by the caller. So, for an
990** ON INSERT trigger, the value passed to this function as parameter reg
991** is not a readable register, although registers (reg+N) through
992** (reg+N+N+1) are.
danielk1977633ed082002-05-17 00:05:58 +0000993**
dan94d7f502009-09-24 09:05:49 +0000994** Parameter orconf is the default conflict resolution algorithm for the
995** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
996** is the instruction that control should jump to if a trigger program
997** raises an IGNORE exception.
danielk1977633ed082002-05-17 00:05:58 +0000998*/
dan165921a2009-08-28 18:53:45 +0000999void sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +00001000 Parse *pParse, /* Parse context */
danielk19772f886d12009-02-28 10:47:41 +00001001 Trigger *pTrigger, /* List of triggers on table pTab */
danielk1977633ed082002-05-17 00:05:58 +00001002 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
1003 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +00001004 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +00001005 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001006 int reg, /* The first in an array of registers (see above) */
danielk19776f349032002-06-11 02:25:40 +00001007 int orconf, /* ON CONFLICT policy */
dan165921a2009-08-28 18:53:45 +00001008 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
drhc977f7f2002-05-21 11:38:11 +00001009){
dan94d7f502009-09-24 09:05:49 +00001010 Trigger *p; /* Used to iterate through pTrigger list */
danielk19778f2c54e2008-01-01 19:02:09 +00001011
dan94d7f502009-09-24 09:05:49 +00001012 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
1013 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
1014 assert( (op==TK_UPDATE)==(pChanges!=0) );
danielk1977c3f9bad2002-05-15 08:30:12 +00001015
danielk19772f886d12009-02-28 10:47:41 +00001016 for(p=pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +00001017
drh9ab4c2e2009-05-09 00:18:38 +00001018 /* Sanity checking: The schema for the trigger and for the table are
1019 ** always defined. The trigger must be in the same schema as the table
1020 ** or else it must be a TEMP trigger. */
1021 assert( p->pSchema!=0 );
1022 assert( p->pTabSchema!=0 );
dan165921a2009-08-28 18:53:45 +00001023 assert( p->pSchema==p->pTabSchema
1024 || p->pSchema==pParse->db->aDb[1].pSchema );
drh9ab4c2e2009-05-09 00:18:38 +00001025
danielk1977eecfb3e2006-01-10 12:31:39 +00001026 /* Determine whether we should code this trigger */
dan165921a2009-08-28 18:53:45 +00001027 if( p->op==op
1028 && p->tr_tm==tr_tm
dan94d7f502009-09-24 09:05:49 +00001029 && checkColumnOverlap(p->pColumns, pChanges)
danielk1977eecfb3e2006-01-10 12:31:39 +00001030 ){
dan94d7f502009-09-24 09:05:49 +00001031 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
danielk1977c3f9bad2002-05-15 08:30:12 +00001032 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001033 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001034}
dan165921a2009-08-28 18:53:45 +00001035
dan2832ad42009-08-31 15:27:27 +00001036/*
1037** Triggers fired by UPDATE or DELETE statements may access values stored
1038** in the old.* pseudo-table. This function returns a 32-bit bitmask
1039** indicating which columns of the old.* table actually are used by
1040** triggers. This information may be used by the caller to avoid having
1041** to load the entire old.* record into memory when executing an UPDATE
1042** or DELETE command.
1043**
1044** Bit 0 of the returned mask is set if the left-most column of the
1045** table may be accessed using an old.<col> reference. Bit 1 is set if
1046** the second leftmost column value is required, and so on. If there
1047** are more than 32 columns in the table, and at least one of the columns
1048** with an index greater than 32 may be accessed, 0xffffffff is returned.
1049**
1050** It is not possible to determine if the old.rowid column is accessed
1051** by triggers. The caller must always assume that it is.
1052**
1053** There is no equivalent function for new.* references.
1054*/
1055u32 sqlite3TriggerOldmask(
dan165921a2009-08-28 18:53:45 +00001056 Parse *pParse, /* Parse context */
1057 Trigger *pTrigger, /* List of triggers on table pTab */
dan165921a2009-08-28 18:53:45 +00001058 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
1059 Table *pTab, /* The table to code triggers from */
dan2832ad42009-08-31 15:27:27 +00001060 int orconf /* Default ON CONFLICT policy for trigger steps */
dan165921a2009-08-28 18:53:45 +00001061){
dan1da40a32009-09-19 17:00:31 +00001062 const int op = pChanges ? TK_UPDATE : TK_DELETE;
dan2832ad42009-08-31 15:27:27 +00001063 u32 mask = 0;
dan165921a2009-08-28 18:53:45 +00001064 Trigger *p;
dan165921a2009-08-28 18:53:45 +00001065
1066 for(p=pTrigger; p; p=p->pNext){
1067 if( p->op==op && checkColumnOverlap(p->pColumns,pChanges) ){
dan2832ad42009-08-31 15:27:27 +00001068 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +00001069 pPrg = getRowTrigger(pParse, p, pTab, orconf);
dan2832ad42009-08-31 15:27:27 +00001070 if( pPrg ){
1071 mask |= pPrg->oldmask;
dan165921a2009-08-28 18:53:45 +00001072 }
1073 }
1074 }
dan2832ad42009-08-31 15:27:27 +00001075
1076 return mask;
dan165921a2009-08-28 18:53:45 +00001077}
1078
drhb7f91642004-10-31 02:22:47 +00001079#endif /* !defined(SQLITE_OMIT_TRIGGER) */