blob: 642833cb1482cc3d15354f983b4f90dc03931fcf [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*************************************************************************
drhc81c11f2009-11-10 01:30:52 +000011** This file contains the implementation for TRIGGERs
drh9adf9ac2002-05-15 11:44:13 +000012*/
danielk1977c3f9bad2002-05-15 08:30:12 +000013#include "sqliteInt.h"
drh9adf9ac2002-05-15 11:44:13 +000014
drhb7f91642004-10-31 02:22:47 +000015#ifndef SQLITE_OMIT_TRIGGER
danielk1977c3f9bad2002-05-15 08:30:12 +000016/*
drh4b59ab52002-08-24 18:24:51 +000017** Delete a linked list of TriggerStep structures.
18*/
drh633e6d52008-07-28 19:34:53 +000019void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
drh4b59ab52002-08-24 18:24:51 +000020 while( pTriggerStep ){
21 TriggerStep * pTmp = pTriggerStep;
22 pTriggerStep = pTriggerStep->pNext;
23
drh633e6d52008-07-28 19:34:53 +000024 sqlite3ExprDelete(db, pTmp->pWhere);
25 sqlite3ExprListDelete(db, pTmp->pExprList);
26 sqlite3SelectDelete(db, pTmp->pSelect);
27 sqlite3IdListDelete(db, pTmp->pIdList);
drh4b59ab52002-08-24 18:24:51 +000028
drh633e6d52008-07-28 19:34:53 +000029 sqlite3DbFree(db, pTmp);
drh4b59ab52002-08-24 18:24:51 +000030 }
31}
32
33/*
danielk19772f886d12009-02-28 10:47:41 +000034** Given table pTab, return a list of all the triggers attached to
35** the table. The list is connected by Trigger.pNext pointers.
drh9ab4c2e2009-05-09 00:18:38 +000036**
37** All of the triggers on pTab that are in the same database as pTab
38** are already attached to pTab->pTrigger. But there might be additional
39** triggers on pTab in the TEMP schema. This routine prepends all
40** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
41** and returns the combined list.
42**
43** To state it another way: This routine returns a list of all triggers
44** that fire off of pTab. The list will include any TEMP triggers on
45** pTab as well as the triggers lised in pTab->pTrigger.
danielk19772f886d12009-02-28 10:47:41 +000046*/
47Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
48 Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
49 Trigger *pList = 0; /* List of triggers to return */
50
dand66c8302009-09-28 14:49:01 +000051 if( pParse->disableTriggers ){
52 return 0;
53 }
54
danielk19772f886d12009-02-28 10:47:41 +000055 if( pTmpSchema!=pTab->pSchema ){
56 HashElem *p;
57 for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
58 Trigger *pTrig = (Trigger *)sqliteHashData(p);
59 if( pTrig->pTabSchema==pTab->pSchema
60 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
61 ){
62 pTrig->pNext = (pList ? pList : pTab->pTrigger);
63 pList = pTrig;
64 }
65 }
66 }
67
68 return (pList ? pList : pTab->pTrigger);
69}
70
71/*
drhf0f258b2003-04-21 18:48:45 +000072** This is called by the parser when it sees a CREATE TRIGGER statement
73** up to the point of the BEGIN before the trigger actions. A Trigger
74** structure is generated based on the information available and stored
75** in pParse->pNewTrigger. After the trigger actions have been parsed, the
danielk19774adee202004-05-08 08:23:19 +000076** sqlite3FinishTrigger() function is called to complete the trigger
drhf0f258b2003-04-21 18:48:45 +000077** construction process.
drh9adf9ac2002-05-15 11:44:13 +000078*/
danielk19774adee202004-05-08 08:23:19 +000079void sqlite3BeginTrigger(
drh9adf9ac2002-05-15 11:44:13 +000080 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
danielk1977ef2cb632004-05-29 02:37:19 +000081 Token *pName1, /* The name of the trigger */
82 Token *pName2, /* The name of the trigger */
drh5cf590c2003-04-24 01:45:04 +000083 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
drh9adf9ac2002-05-15 11:44:13 +000084 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
danielk1977633ed082002-05-17 00:05:58 +000085 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
drhd24cc422003-03-27 12:51:24 +000086 SrcList *pTableName,/* The name of the table/view the trigger applies to */
drh9adf9ac2002-05-15 11:44:13 +000087 Expr *pWhen, /* WHEN clause */
drhfdd48a72006-09-11 23:45:48 +000088 int isTemp, /* True if the TEMPORARY keyword is present */
89 int noErr /* Suppress errors if the trigger already exists */
drh9adf9ac2002-05-15 11:44:13 +000090){
drh3d5f74b2009-08-06 17:43:31 +000091 Trigger *pTrigger = 0; /* The new trigger */
92 Table *pTab; /* Table that the trigger fires off of */
drhf0f258b2003-04-21 18:48:45 +000093 char *zName = 0; /* Name of the trigger */
drh3d5f74b2009-08-06 17:43:31 +000094 sqlite3 *db = pParse->db; /* The database connection */
danielk1977ef2cb632004-05-29 02:37:19 +000095 int iDb; /* The database to store the trigger in */
96 Token *pName; /* The unqualified db name */
drh3d5f74b2009-08-06 17:43:31 +000097 DbFixer sFix; /* State vector for the DB fixer */
98 int iTabDb; /* Index of the database holding pTab */
drhed6c8672003-01-12 18:02:16 +000099
drh43617e92006-03-06 20:55:46 +0000100 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
101 assert( pName2!=0 );
drhaa78bec2008-12-09 03:55:14 +0000102 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
103 assert( op>0 && op<0xff );
danielk1977ef2cb632004-05-29 02:37:19 +0000104 if( isTemp ){
105 /* If TEMP was specified, then the trigger name may not be qualified. */
drh43617e92006-03-06 20:55:46 +0000106 if( pName2->n>0 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000107 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
108 goto trigger_cleanup;
109 }
110 iDb = 1;
111 pName = pName1;
112 }else{
113 /* Figure out the db that the the trigger will be created in */
114 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
115 if( iDb<0 ){
116 goto trigger_cleanup;
117 }
118 }
119
120 /* If the trigger name was unqualified, and the table is a temp table,
121 ** then set iDb to 1 to create the trigger in the temporary database.
122 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
123 ** exist, the error is caught by the block below.
drh9adf9ac2002-05-15 11:44:13 +0000124 */
drh17435752007-08-16 04:30:38 +0000125 if( !pTableName || db->mallocFailed ){
drh6f7adc82006-01-11 21:41:20 +0000126 goto trigger_cleanup;
127 }
danielk1977ef2cb632004-05-29 02:37:19 +0000128 pTab = sqlite3SrcListLookup(pParse, pTableName);
danielk1977da184232006-01-05 11:34:32 +0000129 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977ef2cb632004-05-29 02:37:19 +0000130 iDb = 1;
131 }
132
133 /* Ensure the table name matches database name and that the table exists */
drh17435752007-08-16 04:30:38 +0000134 if( db->mallocFailed ) goto trigger_cleanup;
drhd24cc422003-03-27 12:51:24 +0000135 assert( pTableName->nSrc==1 );
danielk1977ef2cb632004-05-29 02:37:19 +0000136 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) &&
137 sqlite3FixSrcList(&sFix, pTableName) ){
drh4312db52003-06-03 01:47:11 +0000138 goto trigger_cleanup;
drhf26e09c2003-05-31 16:21:12 +0000139 }
danielk1977ef2cb632004-05-29 02:37:19 +0000140 pTab = sqlite3SrcListLookup(pParse, pTableName);
141 if( !pTab ){
142 /* The table does not exist. */
drh3d5f74b2009-08-06 17:43:31 +0000143 if( db->init.iDb==1 ){
144 /* Ticket #3810.
145 ** Normally, whenever a table is dropped, all associated triggers are
146 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
147 ** and the table is dropped by a different database connection, the
148 ** trigger is not visible to the database connection that does the
149 ** drop so the trigger cannot be dropped. This results in an
150 ** "orphaned trigger" - a trigger whose associated table is missing.
151 */
152 db->init.orphanTrigger = 1;
153 }
drhd24cc422003-03-27 12:51:24 +0000154 goto trigger_cleanup;
155 }
drh4cbdda92006-06-14 19:00:20 +0000156 if( IsVirtual(pTab) ){
157 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
158 goto trigger_cleanup;
159 }
drhd24cc422003-03-27 12:51:24 +0000160
danielk1977d8123362004-06-12 09:25:12 +0000161 /* Check that the trigger name is not reserved and that no trigger of the
162 ** specified name exists */
drh17435752007-08-16 04:30:38 +0000163 zName = sqlite3NameFromToken(db, pName);
danielk1977d8123362004-06-12 09:25:12 +0000164 if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
165 goto trigger_cleanup;
166 }
drhaa78bec2008-12-09 03:55:14 +0000167 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
drhea678832008-12-10 19:26:22 +0000168 zName, sqlite3Strlen30(zName)) ){
drhfdd48a72006-09-11 23:45:48 +0000169 if( !noErr ){
170 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
171 }
drhe5f9c642003-01-13 23:27:31 +0000172 goto trigger_cleanup;
danielk1977c3f9bad2002-05-15 08:30:12 +0000173 }
danielk1977ef2cb632004-05-29 02:37:19 +0000174
175 /* Do not create a trigger on a system table */
drhdca76842004-12-07 14:06:13 +0000176 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk19774adee202004-05-08 08:23:19 +0000177 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
drhd24cc422003-03-27 12:51:24 +0000178 pParse->nErr++;
179 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40 +0000180 }
danielk1977ef2cb632004-05-29 02:37:19 +0000181
182 /* INSTEAD of triggers are only for views and views only support INSTEAD
183 ** of triggers.
184 */
185 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000186 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drhda93d232003-03-31 02:12:46 +0000187 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000188 goto trigger_cleanup;
189 }
danielk1977ef2cb632004-05-29 02:37:19 +0000190 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000191 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drhda93d232003-03-31 02:12:46 +0000192 " trigger on table: %S", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000193 goto trigger_cleanup;
194 }
danielk1977da184232006-01-05 11:34:32 +0000195 iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977ef2cb632004-05-29 02:37:19 +0000196
drhd24cc422003-03-27 12:51:24 +0000197#ifndef SQLITE_OMIT_AUTHORIZATION
198 {
199 int code = SQLITE_CREATE_TRIGGER;
danielk1977da184232006-01-05 11:34:32 +0000200 const char *zDb = db->aDb[iTabDb].zName;
drhe22a3342003-04-22 20:30:37 +0000201 const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
danielk1977da184232006-01-05 11:34:32 +0000202 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19 +0000203 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24 +0000204 goto trigger_cleanup;
205 }
danielk1977da184232006-01-05 11:34:32 +0000206 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24 +0000207 goto trigger_cleanup;
208 }
209 }
210#endif
danielk1977d702fcc2002-05-26 23:24:40 +0000211
danielk1977ef2cb632004-05-29 02:37:19 +0000212 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04 +0000213 ** cannot appear on views. So we might as well translate every
214 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
215 ** elsewhere.
216 */
danielk1977d702fcc2002-05-26 23:24:40 +0000217 if (tr_tm == TK_INSTEAD){
218 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12 +0000219 }
220
221 /* Build the Trigger object */
drh17435752007-08-16 04:30:38 +0000222 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19 +0000223 if( pTrigger==0 ) goto trigger_cleanup;
dan165921a2009-08-28 18:53:45 +0000224 pTrigger->zName = zName;
drhe5f9c642003-01-13 23:27:31 +0000225 zName = 0;
drh17435752007-08-16 04:30:38 +0000226 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32 +0000227 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48 +0000228 pTrigger->pTabSchema = pTab->pSchema;
drhaa78bec2008-12-09 03:55:14 +0000229 pTrigger->op = (u8)op;
drhdca76842004-12-07 14:06:13 +0000230 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
danielk19776ab3a2e2009-02-19 14:39:25 +0000231 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
drh17435752007-08-16 04:30:38 +0000232 pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
drhf0f258b2003-04-21 18:48:45 +0000233 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19 +0000234 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45 +0000235
236trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000237 sqlite3DbFree(db, zName);
238 sqlite3SrcListDelete(db, pTableName);
239 sqlite3IdListDelete(db, pColumns);
240 sqlite3ExprDelete(db, pWhen);
danielk1977d5d56522005-03-16 12:15:20 +0000241 if( !pParse->pNewTrigger ){
drh633e6d52008-07-28 19:34:53 +0000242 sqlite3DeleteTrigger(db, pTrigger);
danielk1977d5d56522005-03-16 12:15:20 +0000243 }else{
244 assert( pParse->pNewTrigger==pTrigger );
245 }
drhf0f258b2003-04-21 18:48:45 +0000246}
247
248/*
249** This routine is called after all of the trigger actions have been parsed
250** in order to complete the process of building the trigger.
251*/
danielk19774adee202004-05-08 08:23:19 +0000252void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45 +0000253 Parse *pParse, /* Parser context */
254 TriggerStep *pStepList, /* The triggered program */
255 Token *pAll /* Token that describes the complete CREATE TRIGGER */
256){
drha8c62df2010-02-15 15:47:18 +0000257 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
258 char *zName; /* Name of trigger */
259 sqlite3 *db = pParse->db; /* The database */
260 DbFixer sFix; /* Fixer object */
261 int iDb; /* Database containing the trigger */
262 Token nameToken; /* Trigger name for error reporting */
drhf0f258b2003-04-21 18:48:45 +0000263
danielk1977bfb9e352005-01-24 13:03:32 +0000264 pTrig = pParse->pNewTrigger;
drhf0f258b2003-04-21 18:48:45 +0000265 pParse->pNewTrigger = 0;
drh9ab4c2e2009-05-09 00:18:38 +0000266 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
dan165921a2009-08-28 18:53:45 +0000267 zName = pTrig->zName;
danielk1977da184232006-01-05 11:34:32 +0000268 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32 +0000269 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53 +0000270 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32 +0000271 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53 +0000272 pStepList = pStepList->pNext;
273 }
dan165921a2009-08-28 18:53:45 +0000274 nameToken.z = pTrig->zName;
drhb7916a72009-05-27 10:31:29 +0000275 nameToken.n = sqlite3Strlen30(nameToken.z);
276 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken)
danielk1977bfb9e352005-01-24 13:03:32 +0000277 && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
drhf26e09c2003-05-31 16:21:12 +0000278 goto triggerfinish_cleanup;
279 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000280
drha8c62df2010-02-15 15:47:18 +0000281 /* if we are not initializing,
drh9adf9ac2002-05-15 11:44:13 +0000282 ** build the sqlite_master entry
283 */
drh1d85d932004-02-14 23:05:52 +0000284 if( !db->init.busy ){
drhc977f7f2002-05-21 11:38:11 +0000285 Vdbe *v;
drh2d401ab2008-01-10 23:50:11 +0000286 char *z;
danielk1977c3f9bad2002-05-15 08:30:12 +0000287
288 /* Make an entry in the sqlite_master table */
danielk19774adee202004-05-08 08:23:19 +0000289 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000290 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000291 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11 +0000292 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
293 sqlite3NestedParse(pParse,
294 "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
danielk19772f886d12009-02-28 10:47:41 +0000295 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
drh2d401ab2008-01-10 23:50:11 +0000296 pTrig->table, z);
drh633e6d52008-07-28 19:34:53 +0000297 sqlite3DbFree(db, z);
drh9cbf3422008-01-17 16:22:13 +0000298 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +0000299 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, sqlite3MPrintf(
danielk19772f886d12009-02-28 10:47:41 +0000300 db, "type='trigger' AND name='%q'", zName), P4_DYNAMIC
danielk19771e536952007-08-16 10:09:01 +0000301 );
danielk1977c3f9bad2002-05-15 08:30:12 +0000302 }
303
drh956bc922004-07-24 17:38:29 +0000304 if( db->init.busy ){
danielk19772f886d12009-02-28 10:47:41 +0000305 Trigger *pLink = pTrig;
306 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
307 pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);
308 if( pTrig ){
drhf3a65f72007-08-22 20:18:21 +0000309 db->mallocFailed = 1;
danielk19772f886d12009-02-28 10:47:41 +0000310 }else if( pLink->pSchema==pLink->pTabSchema ){
311 Table *pTab;
drha83ccca2009-04-28 13:01:09 +0000312 int n = sqlite3Strlen30(pLink->table);
danielk19772f886d12009-02-28 10:47:41 +0000313 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n);
314 assert( pTab!=0 );
315 pLink->pNext = pTab->pTrigger;
316 pTab->pTrigger = pLink;
danielk1977d5d56522005-03-16 12:15:20 +0000317 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000318 }
319
drhf0f258b2003-04-21 18:48:45 +0000320triggerfinish_cleanup:
drh633e6d52008-07-28 19:34:53 +0000321 sqlite3DeleteTrigger(db, pTrig);
danielk1977bfb9e352005-01-24 13:03:32 +0000322 assert( !pParse->pNewTrigger );
drh633e6d52008-07-28 19:34:53 +0000323 sqlite3DeleteTriggerStep(db, pStepList);
drh4b59ab52002-08-24 18:24:51 +0000324}
danielk1977c3f9bad2002-05-15 08:30:12 +0000325
drh4b59ab52002-08-24 18:24:51 +0000326/*
drhc977f7f2002-05-21 11:38:11 +0000327** Turn a SELECT statement (that the pSelect parameter points to) into
328** a trigger step. Return a pointer to a TriggerStep structure.
329**
330** The parser calls this routine when it finds a SELECT statement in
331** body of a TRIGGER.
332*/
drh17435752007-08-16 04:30:38 +0000333TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
334 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000335 if( pTriggerStep==0 ) {
drh633e6d52008-07-28 19:34:53 +0000336 sqlite3SelectDelete(db, pSelect);
danielk19772e588c72005-12-09 14:25:08 +0000337 return 0;
338 }
danielk1977633ed082002-05-17 00:05:58 +0000339 pTriggerStep->op = TK_SELECT;
340 pTriggerStep->pSelect = pSelect;
341 pTriggerStep->orconf = OE_Default;
drhb7916a72009-05-27 10:31:29 +0000342 return pTriggerStep;
343}
danielk1977c3f9bad2002-05-15 08:30:12 +0000344
drhb7916a72009-05-27 10:31:29 +0000345/*
346** Allocate space to hold a new trigger step. The allocated space
347** holds both the TriggerStep object and the TriggerStep.target.z string.
348**
349** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
350*/
351static TriggerStep *triggerStepAllocate(
352 sqlite3 *db, /* Database connection */
shane5eff7cf2009-08-10 03:57:58 +0000353 u8 op, /* Trigger opcode */
drhb7916a72009-05-27 10:31:29 +0000354 Token *pName /* The target name */
355){
356 TriggerStep *pTriggerStep;
357
358 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n);
359 if( pTriggerStep ){
360 char *z = (char*)&pTriggerStep[1];
361 memcpy(z, pName->z, pName->n);
362 pTriggerStep->target.z = z;
363 pTriggerStep->target.n = pName->n;
364 pTriggerStep->op = op;
365 }
danielk1977633ed082002-05-17 00:05:58 +0000366 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000367}
368
drhc977f7f2002-05-21 11:38:11 +0000369/*
370** Build a trigger step out of an INSERT statement. Return a pointer
371** to the new trigger step.
372**
373** The parser calls this routine when it sees an INSERT inside the
374** body of a trigger.
375*/
danielk19774adee202004-05-08 08:23:19 +0000376TriggerStep *sqlite3TriggerInsertStep(
drh17435752007-08-16 04:30:38 +0000377 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000378 Token *pTableName, /* Name of the table into which we insert */
379 IdList *pColumn, /* List of columns in pTableName to insert into */
380 ExprList *pEList, /* The VALUE clause: a list of values to be inserted */
381 Select *pSelect, /* A SELECT statement that supplies values */
shane5eff7cf2009-08-10 03:57:58 +0000382 u8 orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
danielk1977633ed082002-05-17 00:05:58 +0000383){
drhf3a65f72007-08-22 20:18:21 +0000384 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000385
danielk1977633ed082002-05-17 00:05:58 +0000386 assert(pEList == 0 || pSelect == 0);
drhf3a65f72007-08-22 20:18:21 +0000387 assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000388
drhb7916a72009-05-27 10:31:29 +0000389 pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
danielk1977d5d56522005-03-16 12:15:20 +0000390 if( pTriggerStep ){
drh33e619f2009-05-28 01:00:55 +0000391 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
danielk1977d5d56522005-03-16 12:15:20 +0000392 pTriggerStep->pIdList = pColumn;
drh33e619f2009-05-28 01:00:55 +0000393 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
danielk1977d5d56522005-03-16 12:15:20 +0000394 pTriggerStep->orconf = orconf;
danielk1977d5d56522005-03-16 12:15:20 +0000395 }else{
drh633e6d52008-07-28 19:34:53 +0000396 sqlite3IdListDelete(db, pColumn);
danielk1977d5d56522005-03-16 12:15:20 +0000397 }
drh33e619f2009-05-28 01:00:55 +0000398 sqlite3ExprListDelete(db, pEList);
399 sqlite3SelectDelete(db, pSelect);
danielk1977c3f9bad2002-05-15 08:30:12 +0000400
danielk1977633ed082002-05-17 00:05:58 +0000401 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000402}
403
drhc977f7f2002-05-21 11:38:11 +0000404/*
405** Construct a trigger step that implements an UPDATE statement and return
406** a pointer to that trigger step. The parser calls this routine when it
407** sees an UPDATE statement inside the body of a CREATE TRIGGER.
408*/
danielk19774adee202004-05-08 08:23:19 +0000409TriggerStep *sqlite3TriggerUpdateStep(
drh17435752007-08-16 04:30:38 +0000410 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000411 Token *pTableName, /* Name of the table to be updated */
412 ExprList *pEList, /* The SET clause: list of column and new values */
413 Expr *pWhere, /* The WHERE clause */
shane5eff7cf2009-08-10 03:57:58 +0000414 u8 orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
drhc977f7f2002-05-21 11:38:11 +0000415){
drhb7916a72009-05-27 10:31:29 +0000416 TriggerStep *pTriggerStep;
417
418 pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
drh33e619f2009-05-28 01:00:55 +0000419 if( pTriggerStep ){
420 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
421 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
422 pTriggerStep->orconf = orconf;
drh0e3a6f32007-03-30 20:40:34 +0000423 }
drh33e619f2009-05-28 01:00:55 +0000424 sqlite3ExprListDelete(db, pEList);
425 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000426 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000427}
428
drhc977f7f2002-05-21 11:38:11 +0000429/*
430** Construct a trigger step that implements a DELETE statement and return
431** a pointer to that trigger step. The parser calls this routine when it
432** sees a DELETE statement inside the body of a CREATE TRIGGER.
433*/
drh17435752007-08-16 04:30:38 +0000434TriggerStep *sqlite3TriggerDeleteStep(
435 sqlite3 *db, /* Database connection */
436 Token *pTableName, /* The table from which rows are deleted */
437 Expr *pWhere /* The WHERE clause */
438){
drhb7916a72009-05-27 10:31:29 +0000439 TriggerStep *pTriggerStep;
440
441 pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
drh33e619f2009-05-28 01:00:55 +0000442 if( pTriggerStep ){
443 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
444 pTriggerStep->orconf = OE_Default;
drhb63a53d2007-03-31 01:34:44 +0000445 }
drh33e619f2009-05-28 01:00:55 +0000446 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000447 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000448}
449
danielk1977633ed082002-05-17 00:05:58 +0000450/*
451** Recursively delete a Trigger structure
452*/
drh633e6d52008-07-28 19:34:53 +0000453void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
drhf0f258b2003-04-21 18:48:45 +0000454 if( pTrigger==0 ) return;
drh633e6d52008-07-28 19:34:53 +0000455 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
dan165921a2009-08-28 18:53:45 +0000456 sqlite3DbFree(db, pTrigger->zName);
drh633e6d52008-07-28 19:34:53 +0000457 sqlite3DbFree(db, pTrigger->table);
458 sqlite3ExprDelete(db, pTrigger->pWhen);
459 sqlite3IdListDelete(db, pTrigger->pColumns);
drh633e6d52008-07-28 19:34:53 +0000460 sqlite3DbFree(db, pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000461}
462
463/*
danielk19778a414492004-06-29 08:59:35 +0000464** This function is called to drop a trigger from the database schema.
465**
466** This may be called directly from the parser and therefore identifies
467** the trigger by name. The sqlite3DropTriggerPtr() routine does the
468** same job as this routine except it takes a pointer to the trigger
469** instead of the trigger name.
470**/
drhfdd48a72006-09-11 23:45:48 +0000471void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000472 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000473 int i;
474 const char *zDb;
475 const char *zName;
476 int nName;
drh9bb575f2004-09-06 17:24:11 +0000477 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000478
drh17435752007-08-16 04:30:38 +0000479 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000480 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000481 goto drop_trigger_cleanup;
482 }
danielk19778e227872004-06-07 07:52:17 +0000483
drhd24cc422003-03-27 12:51:24 +0000484 assert( pName->nSrc==1 );
485 zDb = pName->a[0].zDatabase;
486 zName = pName->a[0].zName;
drhea678832008-12-10 19:26:22 +0000487 nName = sqlite3Strlen30(zName);
danielk197753c0f742005-03-29 03:10:59 +0000488 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000489 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000490 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
drhe4df0e72006-03-29 00:24:06 +0000491 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
drhd24cc422003-03-27 12:51:24 +0000492 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000493 }
drhd24cc422003-03-27 12:51:24 +0000494 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000495 if( !noErr ){
496 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
497 }
drhd24cc422003-03-27 12:51:24 +0000498 goto drop_trigger_cleanup;
499 }
drh74161702006-02-24 02:53:49 +0000500 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000501
502drop_trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000503 sqlite3SrcListDelete(db, pName);
drh79a519c2003-05-17 19:04:03 +0000504}
505
506/*
drh956bc922004-07-24 17:38:29 +0000507** Return a pointer to the Table structure for the table that a trigger
508** is set on.
509*/
drh74161702006-02-24 02:53:49 +0000510static Table *tableOfTrigger(Trigger *pTrigger){
drha83ccca2009-04-28 13:01:09 +0000511 int n = sqlite3Strlen30(pTrigger->table);
danielk1977aaf22682006-01-06 15:03:48 +0000512 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
drh956bc922004-07-24 17:38:29 +0000513}
514
515
516/*
drh74161702006-02-24 02:53:49 +0000517** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000518*/
drh74161702006-02-24 02:53:49 +0000519void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000520 Table *pTable;
521 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000522 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000523 int iDb;
drh79a519c2003-05-17 19:04:03 +0000524
danielk1977da184232006-01-05 11:34:32 +0000525 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000526 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000527 pTable = tableOfTrigger(pTrigger);
drh43617e92006-03-06 20:55:46 +0000528 assert( pTable );
danielk1977da184232006-01-05 11:34:32 +0000529 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000530#ifndef SQLITE_OMIT_AUTHORIZATION
531 {
532 int code = SQLITE_DROP_TRIGGER;
drh956bc922004-07-24 17:38:29 +0000533 const char *zDb = db->aDb[iDb].zName;
534 const char *zTab = SCHEMA_TABLE(iDb);
535 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
dan2bd93512009-08-31 08:22:46 +0000536 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
danielk19774adee202004-05-08 08:23:19 +0000537 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000538 return;
drhe5f9c642003-01-13 23:27:31 +0000539 }
drhed6c8672003-01-12 18:02:16 +0000540 }
drhe5f9c642003-01-13 23:27:31 +0000541#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000542
drhf0f258b2003-04-21 18:48:45 +0000543 /* Generate code to destroy the database record of the trigger.
544 */
drh43617e92006-03-06 20:55:46 +0000545 assert( pTable!=0 );
546 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drhf0f258b2003-04-21 18:48:45 +0000547 int base;
drh57196282004-10-06 15:41:16 +0000548 static const VdbeOpList dropTrigger[] = {
drh9b1b01b2003-08-16 12:37:51 +0000549 { OP_Rewind, 0, ADDR(9), 0},
drh1db639c2008-01-17 02:36:28 +0000550 { OP_String8, 0, 1, 0}, /* 1 */
551 { OP_Column, 0, 1, 2},
552 { OP_Ne, 2, ADDR(8), 1},
553 { OP_String8, 0, 1, 0}, /* 4: "trigger" */
554 { OP_Column, 0, 0, 2},
555 { OP_Ne, 2, ADDR(8), 1},
drhf0f258b2003-04-21 18:48:45 +0000556 { OP_Delete, 0, 0, 0},
drh9b1b01b2003-08-16 12:37:51 +0000557 { OP_Next, 0, ADDR(1), 0}, /* 8 */
drhf0f258b2003-04-21 18:48:45 +0000558 };
559
drh956bc922004-07-24 17:38:29 +0000560 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk1977c00da102006-01-07 13:21:04 +0000561 sqlite3OpenMasterTable(pParse, iDb);
danielk19774adee202004-05-08 08:23:19 +0000562 base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger);
dan165921a2009-08-28 18:53:45 +0000563 sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, 0);
drh24003452008-01-03 01:28:59 +0000564 sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
drh9cbf3422008-01-17 16:22:13 +0000565 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +0000566 sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
dan165921a2009-08-28 18:53:45 +0000567 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
danielk19776ab3a2e2009-02-19 14:39:25 +0000568 if( pParse->nMem<3 ){
569 pParse->nMem = 3;
570 }
drhf0f258b2003-04-21 18:48:45 +0000571 }
drh956bc922004-07-24 17:38:29 +0000572}
drhf0f258b2003-04-21 18:48:45 +0000573
drh956bc922004-07-24 17:38:29 +0000574/*
575** Remove a trigger from the hash tables of the sqlite* pointer.
576*/
577void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
danielk19772f886d12009-02-28 10:47:41 +0000578 Hash *pHash = &(db->aDb[iDb].pSchema->trigHash);
drh956bc922004-07-24 17:38:29 +0000579 Trigger *pTrigger;
danielk19772f886d12009-02-28 10:47:41 +0000580 pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0);
drh9ab4c2e2009-05-09 00:18:38 +0000581 if( ALWAYS(pTrigger) ){
danielk19772f886d12009-02-28 10:47:41 +0000582 if( pTrigger->pSchema==pTrigger->pTabSchema ){
583 Table *pTab = tableOfTrigger(pTrigger);
584 Trigger **pp;
585 for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
586 *pp = (*pp)->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +0000587 }
drh633e6d52008-07-28 19:34:53 +0000588 sqlite3DeleteTrigger(db, pTrigger);
drh956bc922004-07-24 17:38:29 +0000589 db->flags |= SQLITE_InternChanges;
danielk1977c3f9bad2002-05-15 08:30:12 +0000590 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000591}
592
drhc977f7f2002-05-21 11:38:11 +0000593/*
594** pEList is the SET clause of an UPDATE statement. Each entry
595** in pEList is of the format <id>=<expr>. If any of the entries
596** in pEList have an <id> which matches an identifier in pIdList,
597** then return TRUE. If pIdList==NULL, then it is considered a
598** wildcard that matches anything. Likewise if pEList==NULL then
599** it matches anything so always return true. Return false only
600** if there is no match.
601*/
drh9ab4c2e2009-05-09 00:18:38 +0000602static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000603 int e;
drh9ab4c2e2009-05-09 00:18:38 +0000604 if( pIdList==0 || NEVER(pEList==0) ) return 1;
drhad2d8302002-05-24 20:31:36 +0000605 for(e=0; e<pEList->nExpr; e++){
danielk19774adee202004-05-08 08:23:19 +0000606 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000607 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000608 return 0;
609}
610
danielk1977c3f9bad2002-05-15 08:30:12 +0000611/*
danielk19772f886d12009-02-28 10:47:41 +0000612** Return a list of all triggers on table pTab if there exists at least
613** one trigger that must be fired when an operation of type 'op' is
614** performed on the table, and, if that operation is an UPDATE, if at
615** least one of the columns in pChanges is being modified.
drhdca76842004-12-07 14:06:13 +0000616*/
danielk19772f886d12009-02-28 10:47:41 +0000617Trigger *sqlite3TriggersExist(
618 Parse *pParse, /* Parse context */
drhdca76842004-12-07 14:06:13 +0000619 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000620 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
danielk19772f886d12009-02-28 10:47:41 +0000621 ExprList *pChanges, /* Columns that change in an UPDATE statement */
622 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
drhc977f7f2002-05-21 11:38:11 +0000623){
drhdca76842004-12-07 14:06:13 +0000624 int mask = 0;
danielk19772f886d12009-02-28 10:47:41 +0000625 Trigger *pList = sqlite3TriggerList(pParse, pTab);
626 Trigger *p;
627 assert( pList==0 || IsVirtual(pTab)==0 );
628 for(p=pList; p; p=p->pNext){
drh9ab4c2e2009-05-09 00:18:38 +0000629 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
danielk19772f886d12009-02-28 10:47:41 +0000630 mask |= p->tr_tm;
danielk1977c3f9bad2002-05-15 08:30:12 +0000631 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000632 }
danielk19772f886d12009-02-28 10:47:41 +0000633 if( pMask ){
634 *pMask = mask;
635 }
636 return (mask ? pList : 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000637}
638
drhc977f7f2002-05-21 11:38:11 +0000639/*
drhf26e09c2003-05-31 16:21:12 +0000640** Convert the pStep->target token into a SrcList and return a pointer
641** to that SrcList.
642**
643** This routine adds a specific database name, if needed, to the target when
644** forming the SrcList. This prevents a trigger in one database from
645** referring to a target in another database. An exception is when the
646** trigger is in TEMP in which case it can refer to any other database it
647** wants.
648*/
649static SrcList *targetSrcList(
650 Parse *pParse, /* The parsing context */
651 TriggerStep *pStep /* The trigger containing the target token */
652){
drhf26e09c2003-05-31 16:21:12 +0000653 int iDb; /* Index of the database to use */
654 SrcList *pSrc; /* SrcList to be returned */
655
drhb7916a72009-05-27 10:31:29 +0000656 pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
657 if( pSrc ){
658 assert( pSrc->nSrc>0 );
659 assert( pSrc->a!=0 );
660 iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
661 if( iDb==0 || iDb>=2 ){
662 sqlite3 *db = pParse->db;
663 assert( iDb<pParse->db->nDb );
664 pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
665 }
drhf26e09c2003-05-31 16:21:12 +0000666 }
667 return pSrc;
668}
669
670/*
dan65a7cd12009-09-01 12:16:01 +0000671** Generate VDBE code for the statements inside the body of a single
672** trigger.
drhc977f7f2002-05-21 11:38:11 +0000673*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000674static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000675 Parse *pParse, /* The parser context */
676 TriggerStep *pStepList, /* List of statements inside the trigger body */
dan65a7cd12009-09-01 12:16:01 +0000677 int orconf /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000678){
dan65a7cd12009-09-01 12:16:01 +0000679 TriggerStep *pStep;
drh344737f2004-09-19 00:50:20 +0000680 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000681 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000682
dan65a7cd12009-09-01 12:16:01 +0000683 assert( pParse->pTriggerTab && pParse->pToplevel );
684 assert( pStepList );
drh344737f2004-09-19 00:50:20 +0000685 assert( v!=0 );
dan65a7cd12009-09-01 12:16:01 +0000686 for(pStep=pStepList; pStep; pStep=pStep->pNext){
dan165921a2009-08-28 18:53:45 +0000687 /* Figure out the ON CONFLICT policy that will be used for this step
688 ** of the trigger program. If the statement that caused this trigger
689 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
690 ** the ON CONFLICT policy that was specified as part of the trigger
691 ** step statement. Example:
692 **
693 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
694 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
695 ** END;
696 **
697 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
698 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
699 */
shanecea72b22009-09-07 04:38:36 +0000700 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
dan165921a2009-08-28 18:53:45 +0000701
702 switch( pStep->op ){
danielk1977633ed082002-05-17 00:05:58 +0000703 case TK_UPDATE: {
dan165921a2009-08-28 18:53:45 +0000704 sqlite3Update(pParse,
705 targetSrcList(pParse, pStep),
706 sqlite3ExprListDup(db, pStep->pExprList, 0),
707 sqlite3ExprDup(db, pStep->pWhere, 0),
dan65a7cd12009-09-01 12:16:01 +0000708 pParse->eOrconf
dan165921a2009-08-28 18:53:45 +0000709 );
danielk1977633ed082002-05-17 00:05:58 +0000710 break;
711 }
712 case TK_INSERT: {
dan165921a2009-08-28 18:53:45 +0000713 sqlite3Insert(pParse,
714 targetSrcList(pParse, pStep),
715 sqlite3ExprListDup(db, pStep->pExprList, 0),
716 sqlite3SelectDup(db, pStep->pSelect, 0),
717 sqlite3IdListDup(db, pStep->pIdList),
dan65a7cd12009-09-01 12:16:01 +0000718 pParse->eOrconf
dan165921a2009-08-28 18:53:45 +0000719 );
danielk1977633ed082002-05-17 00:05:58 +0000720 break;
721 }
722 case TK_DELETE: {
dan165921a2009-08-28 18:53:45 +0000723 sqlite3DeleteFrom(pParse,
724 targetSrcList(pParse, pStep),
725 sqlite3ExprDup(db, pStep->pWhere, 0)
726 );
danielk1977633ed082002-05-17 00:05:58 +0000727 break;
728 }
dan165921a2009-08-28 18:53:45 +0000729 default: assert( pStep->op==TK_SELECT ); {
730 SelectDest sDest;
731 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
732 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
733 sqlite3Select(pParse, pSelect, &sDest);
734 sqlite3SelectDelete(db, pSelect);
drh9ab4c2e2009-05-09 00:18:38 +0000735 break;
736 }
danielk1977633ed082002-05-17 00:05:58 +0000737 }
dan76d462e2009-08-30 11:42:51 +0000738 if( pStep->op!=TK_SELECT ){
drhb7f1d9a2009-09-08 02:27:58 +0000739 sqlite3VdbeAddOp0(v, OP_ResetCount);
dan76d462e2009-08-30 11:42:51 +0000740 }
danielk1977633ed082002-05-17 00:05:58 +0000741 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000742
danielk1977633ed082002-05-17 00:05:58 +0000743 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000744}
745
dan165921a2009-08-28 18:53:45 +0000746#ifdef SQLITE_DEBUG
747/*
748** This function is used to add VdbeComment() annotations to a VDBE
749** program. It is not used in production code, only for debugging.
750*/
751static const char *onErrorText(int onError){
752 switch( onError ){
753 case OE_Abort: return "abort";
754 case OE_Rollback: return "rollback";
755 case OE_Fail: return "fail";
756 case OE_Replace: return "replace";
757 case OE_Ignore: return "ignore";
758 case OE_Default: return "default";
759 }
760 return "n/a";
761}
762#endif
763
764/*
765** Parse context structure pFrom has just been used to create a sub-vdbe
766** (trigger program). If an error has occurred, transfer error information
767** from pFrom to pTo.
768*/
769static void transferParseError(Parse *pTo, Parse *pFrom){
770 assert( pFrom->zErrMsg==0 || pFrom->nErr );
771 assert( pTo->zErrMsg==0 || pTo->nErr );
772 if( pTo->nErr==0 ){
773 pTo->zErrMsg = pFrom->zErrMsg;
774 pTo->nErr = pFrom->nErr;
775 }else{
776 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
777 }
778}
779
dan65a7cd12009-09-01 12:16:01 +0000780/*
781** Create and populate a new TriggerPrg object with a sub-program
782** implementing trigger pTrigger with ON CONFLICT policy orconf.
783*/
dan2832ad42009-08-31 15:27:27 +0000784static TriggerPrg *codeRowTrigger(
dan165921a2009-08-28 18:53:45 +0000785 Parse *pParse, /* Current parse context */
786 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +0000787 Table *pTab, /* The table pTrigger is attached to */
788 int orconf /* ON CONFLICT policy to code trigger program with */
dan165921a2009-08-28 18:53:45 +0000789){
dan65a7cd12009-09-01 12:16:01 +0000790 Parse *pTop = sqlite3ParseToplevel(pParse);
791 sqlite3 *db = pParse->db; /* Database handle */
792 TriggerPrg *pPrg; /* Value to return */
dan165921a2009-08-28 18:53:45 +0000793 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
794 Vdbe *v; /* Temporary VM */
dan165921a2009-08-28 18:53:45 +0000795 NameContext sNC; /* Name context for sub-vdbe */
796 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
797 Parse *pSubParse; /* Parse context for sub-vdbe */
798 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
799
dan1da40a32009-09-19 17:00:31 +0000800 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dan65a7cd12009-09-01 12:16:01 +0000801
802 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
803 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
804 ** list of the top-level Parse object sooner rather than later. */
dan2832ad42009-08-31 15:27:27 +0000805 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
806 if( !pPrg ) return 0;
dan65a7cd12009-09-01 12:16:01 +0000807 pPrg->pNext = pTop->pTriggerPrg;
808 pTop->pTriggerPrg = pPrg;
dan2832ad42009-08-31 15:27:27 +0000809 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
dan165921a2009-08-28 18:53:45 +0000810 if( !pProgram ) return 0;
811 pProgram->nRef = 1;
dan2832ad42009-08-31 15:27:27 +0000812 pPrg->pTrigger = pTrigger;
813 pPrg->orconf = orconf;
danbb5f1682009-11-27 12:12:34 +0000814 pPrg->aColmask[0] = 0xffffffff;
815 pPrg->aColmask[1] = 0xffffffff;
dan165921a2009-08-28 18:53:45 +0000816
dan65a7cd12009-09-01 12:16:01 +0000817 /* Allocate and populate a new Parse context to use for coding the
818 ** trigger sub-program. */
819 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
820 if( !pSubParse ) return 0;
dan165921a2009-08-28 18:53:45 +0000821 memset(&sNC, 0, sizeof(sNC));
822 sNC.pParse = pSubParse;
823 pSubParse->db = db;
824 pSubParse->pTriggerTab = pTab;
dan65a7cd12009-09-01 12:16:01 +0000825 pSubParse->pToplevel = pTop;
dan2bd93512009-08-31 08:22:46 +0000826 pSubParse->zAuthContext = pTrigger->zName;
dan65a7cd12009-09-01 12:16:01 +0000827 pSubParse->eTriggerOp = pTrigger->op;
dan165921a2009-08-28 18:53:45 +0000828
829 v = sqlite3GetVdbe(pSubParse);
830 if( v ){
dan76d462e2009-08-30 11:42:51 +0000831 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
832 pTrigger->zName, onErrorText(orconf),
dan165921a2009-08-28 18:53:45 +0000833 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
dan65a7cd12009-09-01 12:16:01 +0000834 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
835 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
836 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
dan76d462e2009-08-30 11:42:51 +0000837 pTab->zName
dan165921a2009-08-28 18:53:45 +0000838 ));
dan76d462e2009-08-30 11:42:51 +0000839#ifndef SQLITE_OMIT_TRACE
840 sqlite3VdbeChangeP4(v, -1,
841 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
842 );
843#endif
dan165921a2009-08-28 18:53:45 +0000844
dan65a7cd12009-09-01 12:16:01 +0000845 /* If one was specified, code the WHEN clause. If it evaluates to false
846 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
847 ** OP_Halt inserted at the end of the program. */
dan165921a2009-08-28 18:53:45 +0000848 if( pTrigger->pWhen ){
dan165921a2009-08-28 18:53:45 +0000849 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
dan523a0872009-08-31 05:23:32 +0000850 if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
851 && db->mallocFailed==0
852 ){
dan165921a2009-08-28 18:53:45 +0000853 iEndTrigger = sqlite3VdbeMakeLabel(v);
854 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
855 }
856 sqlite3ExprDelete(db, pWhen);
857 }
858
859 /* Code the trigger program into the sub-vdbe. */
dan76d462e2009-08-30 11:42:51 +0000860 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
dan65a7cd12009-09-01 12:16:01 +0000861
862 /* Insert an OP_Halt at the end of the sub-program. */
dan165921a2009-08-28 18:53:45 +0000863 if( iEndTrigger ){
864 sqlite3VdbeResolveLabel(v, iEndTrigger);
865 }
866 sqlite3VdbeAddOp0(v, OP_Halt);
dan76d462e2009-08-30 11:42:51 +0000867 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
868
dan165921a2009-08-28 18:53:45 +0000869 transferParseError(pParse, pSubParse);
dan523a0872009-08-31 05:23:32 +0000870 if( db->mallocFailed==0 ){
dan65a7cd12009-09-01 12:16:01 +0000871 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
dan523a0872009-08-31 05:23:32 +0000872 }
dan165921a2009-08-28 18:53:45 +0000873 pProgram->nMem = pSubParse->nMem;
874 pProgram->nCsr = pSubParse->nTab;
875 pProgram->token = (void *)pTrigger;
danbb5f1682009-11-27 12:12:34 +0000876 pPrg->aColmask[0] = pSubParse->oldmask;
877 pPrg->aColmask[1] = pSubParse->newmask;
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/*
danbb5f1682009-11-27 12:12:34 +00001037** Triggers may access values stored in the old.* or new.* pseudo-table.
1038** This function returns a 32-bit bitmask indicating which columns of the
1039** old.* or new.* tables actually are used by triggers. This information
1040** may be used by the caller, for example, to avoid having to load the entire
1041** old.* record into memory when executing an UPDATE or DELETE command.
dan2832ad42009-08-31 15:27:27 +00001042**
1043** Bit 0 of the returned mask is set if the left-most column of the
danbb5f1682009-11-27 12:12:34 +00001044** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
dan2832ad42009-08-31 15:27:27 +00001045** the second leftmost column value is required, and so on. If there
1046** are more than 32 columns in the table, and at least one of the columns
1047** with an index greater than 32 may be accessed, 0xffffffff is returned.
1048**
danbb5f1682009-11-27 12:12:34 +00001049** It is not possible to determine if the old.rowid or new.rowid column is
1050** accessed by triggers. The caller must always assume that it is.
dan2832ad42009-08-31 15:27:27 +00001051**
danbb5f1682009-11-27 12:12:34 +00001052** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
1053** applies to the old.* table. If 1, the new.* table.
1054**
1055** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
1056** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
1057** included in the returned mask if the TRIGGER_BEFORE bit is set in the
1058** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
1059** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
dan2832ad42009-08-31 15:27:27 +00001060*/
danbb5f1682009-11-27 12:12:34 +00001061u32 sqlite3TriggerColmask(
dan165921a2009-08-28 18:53:45 +00001062 Parse *pParse, /* Parse context */
1063 Trigger *pTrigger, /* List of triggers on table pTab */
dan165921a2009-08-28 18:53:45 +00001064 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
danbb5f1682009-11-27 12:12:34 +00001065 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
1066 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
dan165921a2009-08-28 18:53:45 +00001067 Table *pTab, /* The table to code triggers from */
dan2832ad42009-08-31 15:27:27 +00001068 int orconf /* Default ON CONFLICT policy for trigger steps */
dan165921a2009-08-28 18:53:45 +00001069){
dan1da40a32009-09-19 17:00:31 +00001070 const int op = pChanges ? TK_UPDATE : TK_DELETE;
dan2832ad42009-08-31 15:27:27 +00001071 u32 mask = 0;
dan165921a2009-08-28 18:53:45 +00001072 Trigger *p;
dan165921a2009-08-28 18:53:45 +00001073
danbb5f1682009-11-27 12:12:34 +00001074 assert( isNew==1 || isNew==0 );
dan165921a2009-08-28 18:53:45 +00001075 for(p=pTrigger; p; p=p->pNext){
danbb5f1682009-11-27 12:12:34 +00001076 if( p->op==op && (tr_tm&p->tr_tm)
1077 && checkColumnOverlap(p->pColumns,pChanges)
1078 ){
dan2832ad42009-08-31 15:27:27 +00001079 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +00001080 pPrg = getRowTrigger(pParse, p, pTab, orconf);
dan2832ad42009-08-31 15:27:27 +00001081 if( pPrg ){
danbb5f1682009-11-27 12:12:34 +00001082 mask |= pPrg->aColmask[isNew];
dan165921a2009-08-28 18:53:45 +00001083 }
1084 }
1085 }
dan2832ad42009-08-31 15:27:27 +00001086
1087 return mask;
dan165921a2009-08-28 18:53:45 +00001088}
1089
drhb7f91642004-10-31 02:22:47 +00001090#endif /* !defined(SQLITE_OMIT_TRIGGER) */