blob: 66464bdae478a8b268256fdae898bcee0104e97e [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);
drh622d2882010-02-15 16:54:55 +0000129 if( db->init.busy==0 && pName2->n==0 && pTab
130 && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977ef2cb632004-05-29 02:37:19 +0000131 iDb = 1;
132 }
133
134 /* Ensure the table name matches database name and that the table exists */
drh17435752007-08-16 04:30:38 +0000135 if( db->mallocFailed ) goto trigger_cleanup;
drhd24cc422003-03-27 12:51:24 +0000136 assert( pTableName->nSrc==1 );
danielk1977ef2cb632004-05-29 02:37:19 +0000137 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) &&
138 sqlite3FixSrcList(&sFix, pTableName) ){
drh4312db52003-06-03 01:47:11 +0000139 goto trigger_cleanup;
drhf26e09c2003-05-31 16:21:12 +0000140 }
danielk1977ef2cb632004-05-29 02:37:19 +0000141 pTab = sqlite3SrcListLookup(pParse, pTableName);
142 if( !pTab ){
143 /* The table does not exist. */
drh3d5f74b2009-08-06 17:43:31 +0000144 if( db->init.iDb==1 ){
145 /* Ticket #3810.
146 ** Normally, whenever a table is dropped, all associated triggers are
147 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
148 ** and the table is dropped by a different database connection, the
149 ** trigger is not visible to the database connection that does the
150 ** drop so the trigger cannot be dropped. This results in an
151 ** "orphaned trigger" - a trigger whose associated table is missing.
152 */
153 db->init.orphanTrigger = 1;
154 }
drhd24cc422003-03-27 12:51:24 +0000155 goto trigger_cleanup;
156 }
drh4cbdda92006-06-14 19:00:20 +0000157 if( IsVirtual(pTab) ){
158 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
159 goto trigger_cleanup;
160 }
drhd24cc422003-03-27 12:51:24 +0000161
danielk1977d8123362004-06-12 09:25:12 +0000162 /* Check that the trigger name is not reserved and that no trigger of the
163 ** specified name exists */
drh17435752007-08-16 04:30:38 +0000164 zName = sqlite3NameFromToken(db, pName);
danielk1977d8123362004-06-12 09:25:12 +0000165 if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
166 goto trigger_cleanup;
167 }
drhaa78bec2008-12-09 03:55:14 +0000168 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
drhea678832008-12-10 19:26:22 +0000169 zName, sqlite3Strlen30(zName)) ){
drhfdd48a72006-09-11 23:45:48 +0000170 if( !noErr ){
171 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
172 }
drhe5f9c642003-01-13 23:27:31 +0000173 goto trigger_cleanup;
danielk1977c3f9bad2002-05-15 08:30:12 +0000174 }
danielk1977ef2cb632004-05-29 02:37:19 +0000175
176 /* Do not create a trigger on a system table */
drhdca76842004-12-07 14:06:13 +0000177 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk19774adee202004-05-08 08:23:19 +0000178 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
drhd24cc422003-03-27 12:51:24 +0000179 pParse->nErr++;
180 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40 +0000181 }
danielk1977ef2cb632004-05-29 02:37:19 +0000182
183 /* INSTEAD of triggers are only for views and views only support INSTEAD
184 ** of triggers.
185 */
186 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000187 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drhda93d232003-03-31 02:12:46 +0000188 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000189 goto trigger_cleanup;
190 }
danielk1977ef2cb632004-05-29 02:37:19 +0000191 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000192 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drhda93d232003-03-31 02:12:46 +0000193 " trigger on table: %S", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000194 goto trigger_cleanup;
195 }
danielk1977da184232006-01-05 11:34:32 +0000196 iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977ef2cb632004-05-29 02:37:19 +0000197
drhd24cc422003-03-27 12:51:24 +0000198#ifndef SQLITE_OMIT_AUTHORIZATION
199 {
200 int code = SQLITE_CREATE_TRIGGER;
danielk1977da184232006-01-05 11:34:32 +0000201 const char *zDb = db->aDb[iTabDb].zName;
drhe22a3342003-04-22 20:30:37 +0000202 const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
danielk1977da184232006-01-05 11:34:32 +0000203 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19 +0000204 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24 +0000205 goto trigger_cleanup;
206 }
danielk1977da184232006-01-05 11:34:32 +0000207 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24 +0000208 goto trigger_cleanup;
209 }
210 }
211#endif
danielk1977d702fcc2002-05-26 23:24:40 +0000212
danielk1977ef2cb632004-05-29 02:37:19 +0000213 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04 +0000214 ** cannot appear on views. So we might as well translate every
215 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
216 ** elsewhere.
217 */
danielk1977d702fcc2002-05-26 23:24:40 +0000218 if (tr_tm == TK_INSTEAD){
219 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12 +0000220 }
221
222 /* Build the Trigger object */
drh17435752007-08-16 04:30:38 +0000223 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19 +0000224 if( pTrigger==0 ) goto trigger_cleanup;
dan165921a2009-08-28 18:53:45 +0000225 pTrigger->zName = zName;
drhe5f9c642003-01-13 23:27:31 +0000226 zName = 0;
drh17435752007-08-16 04:30:38 +0000227 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32 +0000228 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48 +0000229 pTrigger->pTabSchema = pTab->pSchema;
drhaa78bec2008-12-09 03:55:14 +0000230 pTrigger->op = (u8)op;
drhdca76842004-12-07 14:06:13 +0000231 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
danielk19776ab3a2e2009-02-19 14:39:25 +0000232 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
drh17435752007-08-16 04:30:38 +0000233 pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
drhf0f258b2003-04-21 18:48:45 +0000234 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19 +0000235 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45 +0000236
237trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000238 sqlite3DbFree(db, zName);
239 sqlite3SrcListDelete(db, pTableName);
240 sqlite3IdListDelete(db, pColumns);
241 sqlite3ExprDelete(db, pWhen);
danielk1977d5d56522005-03-16 12:15:20 +0000242 if( !pParse->pNewTrigger ){
drh633e6d52008-07-28 19:34:53 +0000243 sqlite3DeleteTrigger(db, pTrigger);
danielk1977d5d56522005-03-16 12:15:20 +0000244 }else{
245 assert( pParse->pNewTrigger==pTrigger );
246 }
drhf0f258b2003-04-21 18:48:45 +0000247}
248
249/*
250** This routine is called after all of the trigger actions have been parsed
251** in order to complete the process of building the trigger.
252*/
danielk19774adee202004-05-08 08:23:19 +0000253void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45 +0000254 Parse *pParse, /* Parser context */
255 TriggerStep *pStepList, /* The triggered program */
256 Token *pAll /* Token that describes the complete CREATE TRIGGER */
257){
drha8c62df2010-02-15 15:47:18 +0000258 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
259 char *zName; /* Name of trigger */
260 sqlite3 *db = pParse->db; /* The database */
261 DbFixer sFix; /* Fixer object */
262 int iDb; /* Database containing the trigger */
263 Token nameToken; /* Trigger name for error reporting */
drhf0f258b2003-04-21 18:48:45 +0000264
danielk1977bfb9e352005-01-24 13:03:32 +0000265 pTrig = pParse->pNewTrigger;
drhf0f258b2003-04-21 18:48:45 +0000266 pParse->pNewTrigger = 0;
drh9ab4c2e2009-05-09 00:18:38 +0000267 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
dan165921a2009-08-28 18:53:45 +0000268 zName = pTrig->zName;
danielk1977da184232006-01-05 11:34:32 +0000269 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32 +0000270 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53 +0000271 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32 +0000272 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53 +0000273 pStepList = pStepList->pNext;
274 }
dan165921a2009-08-28 18:53:45 +0000275 nameToken.z = pTrig->zName;
drhb7916a72009-05-27 10:31:29 +0000276 nameToken.n = sqlite3Strlen30(nameToken.z);
277 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken)
danielk1977bfb9e352005-01-24 13:03:32 +0000278 && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
drhf26e09c2003-05-31 16:21:12 +0000279 goto triggerfinish_cleanup;
280 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000281
drha8c62df2010-02-15 15:47:18 +0000282 /* if we are not initializing,
drh9adf9ac2002-05-15 11:44:13 +0000283 ** build the sqlite_master entry
284 */
drh1d85d932004-02-14 23:05:52 +0000285 if( !db->init.busy ){
drhc977f7f2002-05-21 11:38:11 +0000286 Vdbe *v;
drh2d401ab2008-01-10 23:50:11 +0000287 char *z;
danielk1977c3f9bad2002-05-15 08:30:12 +0000288
289 /* Make an entry in the sqlite_master table */
danielk19774adee202004-05-08 08:23:19 +0000290 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000291 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000292 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11 +0000293 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
294 sqlite3NestedParse(pParse,
295 "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
danielk19772f886d12009-02-28 10:47:41 +0000296 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
drh2d401ab2008-01-10 23:50:11 +0000297 pTrig->table, z);
drh633e6d52008-07-28 19:34:53 +0000298 sqlite3DbFree(db, z);
drh9cbf3422008-01-17 16:22:13 +0000299 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +0000300 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, sqlite3MPrintf(
danielk19772f886d12009-02-28 10:47:41 +0000301 db, "type='trigger' AND name='%q'", zName), P4_DYNAMIC
danielk19771e536952007-08-16 10:09:01 +0000302 );
danielk1977c3f9bad2002-05-15 08:30:12 +0000303 }
304
drh956bc922004-07-24 17:38:29 +0000305 if( db->init.busy ){
danielk19772f886d12009-02-28 10:47:41 +0000306 Trigger *pLink = pTrig;
307 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
308 pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);
309 if( pTrig ){
drhf3a65f72007-08-22 20:18:21 +0000310 db->mallocFailed = 1;
danielk19772f886d12009-02-28 10:47:41 +0000311 }else if( pLink->pSchema==pLink->pTabSchema ){
312 Table *pTab;
drha83ccca2009-04-28 13:01:09 +0000313 int n = sqlite3Strlen30(pLink->table);
danielk19772f886d12009-02-28 10:47:41 +0000314 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n);
315 assert( pTab!=0 );
316 pLink->pNext = pTab->pTrigger;
317 pTab->pTrigger = pLink;
danielk1977d5d56522005-03-16 12:15:20 +0000318 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000319 }
320
drhf0f258b2003-04-21 18:48:45 +0000321triggerfinish_cleanup:
drh633e6d52008-07-28 19:34:53 +0000322 sqlite3DeleteTrigger(db, pTrig);
danielk1977bfb9e352005-01-24 13:03:32 +0000323 assert( !pParse->pNewTrigger );
drh633e6d52008-07-28 19:34:53 +0000324 sqlite3DeleteTriggerStep(db, pStepList);
drh4b59ab52002-08-24 18:24:51 +0000325}
danielk1977c3f9bad2002-05-15 08:30:12 +0000326
drh4b59ab52002-08-24 18:24:51 +0000327/*
drhc977f7f2002-05-21 11:38:11 +0000328** Turn a SELECT statement (that the pSelect parameter points to) into
329** a trigger step. Return a pointer to a TriggerStep structure.
330**
331** The parser calls this routine when it finds a SELECT statement in
332** body of a TRIGGER.
333*/
drh17435752007-08-16 04:30:38 +0000334TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
335 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000336 if( pTriggerStep==0 ) {
drh633e6d52008-07-28 19:34:53 +0000337 sqlite3SelectDelete(db, pSelect);
danielk19772e588c72005-12-09 14:25:08 +0000338 return 0;
339 }
danielk1977633ed082002-05-17 00:05:58 +0000340 pTriggerStep->op = TK_SELECT;
341 pTriggerStep->pSelect = pSelect;
342 pTriggerStep->orconf = OE_Default;
drhb7916a72009-05-27 10:31:29 +0000343 return pTriggerStep;
344}
danielk1977c3f9bad2002-05-15 08:30:12 +0000345
drhb7916a72009-05-27 10:31:29 +0000346/*
347** Allocate space to hold a new trigger step. The allocated space
348** holds both the TriggerStep object and the TriggerStep.target.z string.
349**
350** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
351*/
352static TriggerStep *triggerStepAllocate(
353 sqlite3 *db, /* Database connection */
shane5eff7cf2009-08-10 03:57:58 +0000354 u8 op, /* Trigger opcode */
drhb7916a72009-05-27 10:31:29 +0000355 Token *pName /* The target name */
356){
357 TriggerStep *pTriggerStep;
358
359 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n);
360 if( pTriggerStep ){
361 char *z = (char*)&pTriggerStep[1];
362 memcpy(z, pName->z, pName->n);
363 pTriggerStep->target.z = z;
364 pTriggerStep->target.n = pName->n;
365 pTriggerStep->op = op;
366 }
danielk1977633ed082002-05-17 00:05:58 +0000367 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000368}
369
drhc977f7f2002-05-21 11:38:11 +0000370/*
371** Build a trigger step out of an INSERT statement. Return a pointer
372** to the new trigger step.
373**
374** The parser calls this routine when it sees an INSERT inside the
375** body of a trigger.
376*/
danielk19774adee202004-05-08 08:23:19 +0000377TriggerStep *sqlite3TriggerInsertStep(
drh17435752007-08-16 04:30:38 +0000378 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000379 Token *pTableName, /* Name of the table into which we insert */
380 IdList *pColumn, /* List of columns in pTableName to insert into */
381 ExprList *pEList, /* The VALUE clause: a list of values to be inserted */
382 Select *pSelect, /* A SELECT statement that supplies values */
shane5eff7cf2009-08-10 03:57:58 +0000383 u8 orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
danielk1977633ed082002-05-17 00:05:58 +0000384){
drhf3a65f72007-08-22 20:18:21 +0000385 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000386
danielk1977633ed082002-05-17 00:05:58 +0000387 assert(pEList == 0 || pSelect == 0);
drhf3a65f72007-08-22 20:18:21 +0000388 assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000389
drhb7916a72009-05-27 10:31:29 +0000390 pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
danielk1977d5d56522005-03-16 12:15:20 +0000391 if( pTriggerStep ){
drh33e619f2009-05-28 01:00:55 +0000392 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
danielk1977d5d56522005-03-16 12:15:20 +0000393 pTriggerStep->pIdList = pColumn;
drh33e619f2009-05-28 01:00:55 +0000394 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
danielk1977d5d56522005-03-16 12:15:20 +0000395 pTriggerStep->orconf = orconf;
danielk1977d5d56522005-03-16 12:15:20 +0000396 }else{
drh633e6d52008-07-28 19:34:53 +0000397 sqlite3IdListDelete(db, pColumn);
danielk1977d5d56522005-03-16 12:15:20 +0000398 }
drh33e619f2009-05-28 01:00:55 +0000399 sqlite3ExprListDelete(db, pEList);
400 sqlite3SelectDelete(db, pSelect);
danielk1977c3f9bad2002-05-15 08:30:12 +0000401
danielk1977633ed082002-05-17 00:05:58 +0000402 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000403}
404
drhc977f7f2002-05-21 11:38:11 +0000405/*
406** Construct a trigger step that implements an UPDATE statement and return
407** a pointer to that trigger step. The parser calls this routine when it
408** sees an UPDATE statement inside the body of a CREATE TRIGGER.
409*/
danielk19774adee202004-05-08 08:23:19 +0000410TriggerStep *sqlite3TriggerUpdateStep(
drh17435752007-08-16 04:30:38 +0000411 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000412 Token *pTableName, /* Name of the table to be updated */
413 ExprList *pEList, /* The SET clause: list of column and new values */
414 Expr *pWhere, /* The WHERE clause */
shane5eff7cf2009-08-10 03:57:58 +0000415 u8 orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
drhc977f7f2002-05-21 11:38:11 +0000416){
drhb7916a72009-05-27 10:31:29 +0000417 TriggerStep *pTriggerStep;
418
419 pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
drh33e619f2009-05-28 01:00:55 +0000420 if( pTriggerStep ){
421 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
422 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
423 pTriggerStep->orconf = orconf;
drh0e3a6f32007-03-30 20:40:34 +0000424 }
drh33e619f2009-05-28 01:00:55 +0000425 sqlite3ExprListDelete(db, pEList);
426 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000427 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000428}
429
drhc977f7f2002-05-21 11:38:11 +0000430/*
431** Construct a trigger step that implements a DELETE statement and return
432** a pointer to that trigger step. The parser calls this routine when it
433** sees a DELETE statement inside the body of a CREATE TRIGGER.
434*/
drh17435752007-08-16 04:30:38 +0000435TriggerStep *sqlite3TriggerDeleteStep(
436 sqlite3 *db, /* Database connection */
437 Token *pTableName, /* The table from which rows are deleted */
438 Expr *pWhere /* The WHERE clause */
439){
drhb7916a72009-05-27 10:31:29 +0000440 TriggerStep *pTriggerStep;
441
442 pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
drh33e619f2009-05-28 01:00:55 +0000443 if( pTriggerStep ){
444 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
445 pTriggerStep->orconf = OE_Default;
drhb63a53d2007-03-31 01:34:44 +0000446 }
drh33e619f2009-05-28 01:00:55 +0000447 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000448 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000449}
450
danielk1977633ed082002-05-17 00:05:58 +0000451/*
452** Recursively delete a Trigger structure
453*/
drh633e6d52008-07-28 19:34:53 +0000454void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
drhf0f258b2003-04-21 18:48:45 +0000455 if( pTrigger==0 ) return;
drh633e6d52008-07-28 19:34:53 +0000456 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
dan165921a2009-08-28 18:53:45 +0000457 sqlite3DbFree(db, pTrigger->zName);
drh633e6d52008-07-28 19:34:53 +0000458 sqlite3DbFree(db, pTrigger->table);
459 sqlite3ExprDelete(db, pTrigger->pWhen);
460 sqlite3IdListDelete(db, pTrigger->pColumns);
drh633e6d52008-07-28 19:34:53 +0000461 sqlite3DbFree(db, pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000462}
463
464/*
danielk19778a414492004-06-29 08:59:35 +0000465** This function is called to drop a trigger from the database schema.
466**
467** This may be called directly from the parser and therefore identifies
468** the trigger by name. The sqlite3DropTriggerPtr() routine does the
469** same job as this routine except it takes a pointer to the trigger
470** instead of the trigger name.
471**/
drhfdd48a72006-09-11 23:45:48 +0000472void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000473 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000474 int i;
475 const char *zDb;
476 const char *zName;
477 int nName;
drh9bb575f2004-09-06 17:24:11 +0000478 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000479
drh17435752007-08-16 04:30:38 +0000480 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000481 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000482 goto drop_trigger_cleanup;
483 }
danielk19778e227872004-06-07 07:52:17 +0000484
drhd24cc422003-03-27 12:51:24 +0000485 assert( pName->nSrc==1 );
486 zDb = pName->a[0].zDatabase;
487 zName = pName->a[0].zName;
drhea678832008-12-10 19:26:22 +0000488 nName = sqlite3Strlen30(zName);
danielk197753c0f742005-03-29 03:10:59 +0000489 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000490 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000491 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
drhe4df0e72006-03-29 00:24:06 +0000492 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
drhd24cc422003-03-27 12:51:24 +0000493 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000494 }
drhd24cc422003-03-27 12:51:24 +0000495 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000496 if( !noErr ){
497 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
498 }
drhd24cc422003-03-27 12:51:24 +0000499 goto drop_trigger_cleanup;
500 }
drh74161702006-02-24 02:53:49 +0000501 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000502
503drop_trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000504 sqlite3SrcListDelete(db, pName);
drh79a519c2003-05-17 19:04:03 +0000505}
506
507/*
drh956bc922004-07-24 17:38:29 +0000508** Return a pointer to the Table structure for the table that a trigger
509** is set on.
510*/
drh74161702006-02-24 02:53:49 +0000511static Table *tableOfTrigger(Trigger *pTrigger){
drha83ccca2009-04-28 13:01:09 +0000512 int n = sqlite3Strlen30(pTrigger->table);
danielk1977aaf22682006-01-06 15:03:48 +0000513 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
drh956bc922004-07-24 17:38:29 +0000514}
515
516
517/*
drh74161702006-02-24 02:53:49 +0000518** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000519*/
drh74161702006-02-24 02:53:49 +0000520void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000521 Table *pTable;
522 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000523 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000524 int iDb;
drh79a519c2003-05-17 19:04:03 +0000525
danielk1977da184232006-01-05 11:34:32 +0000526 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000527 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000528 pTable = tableOfTrigger(pTrigger);
drh43617e92006-03-06 20:55:46 +0000529 assert( pTable );
danielk1977da184232006-01-05 11:34:32 +0000530 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000531#ifndef SQLITE_OMIT_AUTHORIZATION
532 {
533 int code = SQLITE_DROP_TRIGGER;
drh956bc922004-07-24 17:38:29 +0000534 const char *zDb = db->aDb[iDb].zName;
535 const char *zTab = SCHEMA_TABLE(iDb);
536 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
dan2bd93512009-08-31 08:22:46 +0000537 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
danielk19774adee202004-05-08 08:23:19 +0000538 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000539 return;
drhe5f9c642003-01-13 23:27:31 +0000540 }
drhed6c8672003-01-12 18:02:16 +0000541 }
drhe5f9c642003-01-13 23:27:31 +0000542#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000543
drhf0f258b2003-04-21 18:48:45 +0000544 /* Generate code to destroy the database record of the trigger.
545 */
drh43617e92006-03-06 20:55:46 +0000546 assert( pTable!=0 );
547 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drhf0f258b2003-04-21 18:48:45 +0000548 int base;
drh57196282004-10-06 15:41:16 +0000549 static const VdbeOpList dropTrigger[] = {
drh9b1b01b2003-08-16 12:37:51 +0000550 { OP_Rewind, 0, ADDR(9), 0},
drh1db639c2008-01-17 02:36:28 +0000551 { OP_String8, 0, 1, 0}, /* 1 */
552 { OP_Column, 0, 1, 2},
553 { OP_Ne, 2, ADDR(8), 1},
554 { OP_String8, 0, 1, 0}, /* 4: "trigger" */
555 { OP_Column, 0, 0, 2},
556 { OP_Ne, 2, ADDR(8), 1},
drhf0f258b2003-04-21 18:48:45 +0000557 { OP_Delete, 0, 0, 0},
drh9b1b01b2003-08-16 12:37:51 +0000558 { OP_Next, 0, ADDR(1), 0}, /* 8 */
drhf0f258b2003-04-21 18:48:45 +0000559 };
560
drh956bc922004-07-24 17:38:29 +0000561 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk1977c00da102006-01-07 13:21:04 +0000562 sqlite3OpenMasterTable(pParse, iDb);
danielk19774adee202004-05-08 08:23:19 +0000563 base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger);
dan165921a2009-08-28 18:53:45 +0000564 sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, 0);
drh24003452008-01-03 01:28:59 +0000565 sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
drh9cbf3422008-01-17 16:22:13 +0000566 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +0000567 sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
dan165921a2009-08-28 18:53:45 +0000568 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
danielk19776ab3a2e2009-02-19 14:39:25 +0000569 if( pParse->nMem<3 ){
570 pParse->nMem = 3;
571 }
drhf0f258b2003-04-21 18:48:45 +0000572 }
drh956bc922004-07-24 17:38:29 +0000573}
drhf0f258b2003-04-21 18:48:45 +0000574
drh956bc922004-07-24 17:38:29 +0000575/*
576** Remove a trigger from the hash tables of the sqlite* pointer.
577*/
578void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
danielk19772f886d12009-02-28 10:47:41 +0000579 Hash *pHash = &(db->aDb[iDb].pSchema->trigHash);
drh956bc922004-07-24 17:38:29 +0000580 Trigger *pTrigger;
danielk19772f886d12009-02-28 10:47:41 +0000581 pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0);
drh9ab4c2e2009-05-09 00:18:38 +0000582 if( ALWAYS(pTrigger) ){
danielk19772f886d12009-02-28 10:47:41 +0000583 if( pTrigger->pSchema==pTrigger->pTabSchema ){
584 Table *pTab = tableOfTrigger(pTrigger);
585 Trigger **pp;
586 for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
587 *pp = (*pp)->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +0000588 }
drh633e6d52008-07-28 19:34:53 +0000589 sqlite3DeleteTrigger(db, pTrigger);
drh956bc922004-07-24 17:38:29 +0000590 db->flags |= SQLITE_InternChanges;
danielk1977c3f9bad2002-05-15 08:30:12 +0000591 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000592}
593
drhc977f7f2002-05-21 11:38:11 +0000594/*
595** pEList is the SET clause of an UPDATE statement. Each entry
596** in pEList is of the format <id>=<expr>. If any of the entries
597** in pEList have an <id> which matches an identifier in pIdList,
598** then return TRUE. If pIdList==NULL, then it is considered a
599** wildcard that matches anything. Likewise if pEList==NULL then
600** it matches anything so always return true. Return false only
601** if there is no match.
602*/
drh9ab4c2e2009-05-09 00:18:38 +0000603static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000604 int e;
drh9ab4c2e2009-05-09 00:18:38 +0000605 if( pIdList==0 || NEVER(pEList==0) ) return 1;
drhad2d8302002-05-24 20:31:36 +0000606 for(e=0; e<pEList->nExpr; e++){
danielk19774adee202004-05-08 08:23:19 +0000607 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000608 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000609 return 0;
610}
611
danielk1977c3f9bad2002-05-15 08:30:12 +0000612/*
danielk19772f886d12009-02-28 10:47:41 +0000613** Return a list of all triggers on table pTab if there exists at least
614** one trigger that must be fired when an operation of type 'op' is
615** performed on the table, and, if that operation is an UPDATE, if at
616** least one of the columns in pChanges is being modified.
drhdca76842004-12-07 14:06:13 +0000617*/
danielk19772f886d12009-02-28 10:47:41 +0000618Trigger *sqlite3TriggersExist(
619 Parse *pParse, /* Parse context */
drhdca76842004-12-07 14:06:13 +0000620 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000621 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
danielk19772f886d12009-02-28 10:47:41 +0000622 ExprList *pChanges, /* Columns that change in an UPDATE statement */
623 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
drhc977f7f2002-05-21 11:38:11 +0000624){
drhdca76842004-12-07 14:06:13 +0000625 int mask = 0;
danielk19772f886d12009-02-28 10:47:41 +0000626 Trigger *pList = sqlite3TriggerList(pParse, pTab);
627 Trigger *p;
628 assert( pList==0 || IsVirtual(pTab)==0 );
629 for(p=pList; p; p=p->pNext){
drh9ab4c2e2009-05-09 00:18:38 +0000630 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
danielk19772f886d12009-02-28 10:47:41 +0000631 mask |= p->tr_tm;
danielk1977c3f9bad2002-05-15 08:30:12 +0000632 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000633 }
danielk19772f886d12009-02-28 10:47:41 +0000634 if( pMask ){
635 *pMask = mask;
636 }
637 return (mask ? pList : 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000638}
639
drhc977f7f2002-05-21 11:38:11 +0000640/*
drhf26e09c2003-05-31 16:21:12 +0000641** Convert the pStep->target token into a SrcList and return a pointer
642** to that SrcList.
643**
644** This routine adds a specific database name, if needed, to the target when
645** forming the SrcList. This prevents a trigger in one database from
646** referring to a target in another database. An exception is when the
647** trigger is in TEMP in which case it can refer to any other database it
648** wants.
649*/
650static SrcList *targetSrcList(
651 Parse *pParse, /* The parsing context */
652 TriggerStep *pStep /* The trigger containing the target token */
653){
drhf26e09c2003-05-31 16:21:12 +0000654 int iDb; /* Index of the database to use */
655 SrcList *pSrc; /* SrcList to be returned */
656
drhb7916a72009-05-27 10:31:29 +0000657 pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
658 if( pSrc ){
659 assert( pSrc->nSrc>0 );
660 assert( pSrc->a!=0 );
661 iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
662 if( iDb==0 || iDb>=2 ){
663 sqlite3 *db = pParse->db;
664 assert( iDb<pParse->db->nDb );
665 pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
666 }
drhf26e09c2003-05-31 16:21:12 +0000667 }
668 return pSrc;
669}
670
671/*
dan65a7cd12009-09-01 12:16:01 +0000672** Generate VDBE code for the statements inside the body of a single
673** trigger.
drhc977f7f2002-05-21 11:38:11 +0000674*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000675static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000676 Parse *pParse, /* The parser context */
677 TriggerStep *pStepList, /* List of statements inside the trigger body */
dan65a7cd12009-09-01 12:16:01 +0000678 int orconf /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000679){
dan65a7cd12009-09-01 12:16:01 +0000680 TriggerStep *pStep;
drh344737f2004-09-19 00:50:20 +0000681 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000682 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000683
dan65a7cd12009-09-01 12:16:01 +0000684 assert( pParse->pTriggerTab && pParse->pToplevel );
685 assert( pStepList );
drh344737f2004-09-19 00:50:20 +0000686 assert( v!=0 );
dan65a7cd12009-09-01 12:16:01 +0000687 for(pStep=pStepList; pStep; pStep=pStep->pNext){
dan165921a2009-08-28 18:53:45 +0000688 /* Figure out the ON CONFLICT policy that will be used for this step
689 ** of the trigger program. If the statement that caused this trigger
690 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
691 ** the ON CONFLICT policy that was specified as part of the trigger
692 ** step statement. Example:
693 **
694 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
695 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
696 ** END;
697 **
698 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
699 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
700 */
shanecea72b22009-09-07 04:38:36 +0000701 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
dan165921a2009-08-28 18:53:45 +0000702
703 switch( pStep->op ){
danielk1977633ed082002-05-17 00:05:58 +0000704 case TK_UPDATE: {
dan165921a2009-08-28 18:53:45 +0000705 sqlite3Update(pParse,
706 targetSrcList(pParse, pStep),
707 sqlite3ExprListDup(db, pStep->pExprList, 0),
708 sqlite3ExprDup(db, pStep->pWhere, 0),
dan65a7cd12009-09-01 12:16:01 +0000709 pParse->eOrconf
dan165921a2009-08-28 18:53:45 +0000710 );
danielk1977633ed082002-05-17 00:05:58 +0000711 break;
712 }
713 case TK_INSERT: {
dan165921a2009-08-28 18:53:45 +0000714 sqlite3Insert(pParse,
715 targetSrcList(pParse, pStep),
716 sqlite3ExprListDup(db, pStep->pExprList, 0),
717 sqlite3SelectDup(db, pStep->pSelect, 0),
718 sqlite3IdListDup(db, pStep->pIdList),
dan65a7cd12009-09-01 12:16:01 +0000719 pParse->eOrconf
dan165921a2009-08-28 18:53:45 +0000720 );
danielk1977633ed082002-05-17 00:05:58 +0000721 break;
722 }
723 case TK_DELETE: {
dan165921a2009-08-28 18:53:45 +0000724 sqlite3DeleteFrom(pParse,
725 targetSrcList(pParse, pStep),
726 sqlite3ExprDup(db, pStep->pWhere, 0)
727 );
danielk1977633ed082002-05-17 00:05:58 +0000728 break;
729 }
dan165921a2009-08-28 18:53:45 +0000730 default: assert( pStep->op==TK_SELECT ); {
731 SelectDest sDest;
732 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
733 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
734 sqlite3Select(pParse, pSelect, &sDest);
735 sqlite3SelectDelete(db, pSelect);
drh9ab4c2e2009-05-09 00:18:38 +0000736 break;
737 }
danielk1977633ed082002-05-17 00:05:58 +0000738 }
dan76d462e2009-08-30 11:42:51 +0000739 if( pStep->op!=TK_SELECT ){
drhb7f1d9a2009-09-08 02:27:58 +0000740 sqlite3VdbeAddOp0(v, OP_ResetCount);
dan76d462e2009-08-30 11:42:51 +0000741 }
danielk1977633ed082002-05-17 00:05:58 +0000742 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000743
danielk1977633ed082002-05-17 00:05:58 +0000744 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000745}
746
dan165921a2009-08-28 18:53:45 +0000747#ifdef SQLITE_DEBUG
748/*
749** This function is used to add VdbeComment() annotations to a VDBE
750** program. It is not used in production code, only for debugging.
751*/
752static const char *onErrorText(int onError){
753 switch( onError ){
754 case OE_Abort: return "abort";
755 case OE_Rollback: return "rollback";
756 case OE_Fail: return "fail";
757 case OE_Replace: return "replace";
758 case OE_Ignore: return "ignore";
759 case OE_Default: return "default";
760 }
761 return "n/a";
762}
763#endif
764
765/*
766** Parse context structure pFrom has just been used to create a sub-vdbe
767** (trigger program). If an error has occurred, transfer error information
768** from pFrom to pTo.
769*/
770static void transferParseError(Parse *pTo, Parse *pFrom){
771 assert( pFrom->zErrMsg==0 || pFrom->nErr );
772 assert( pTo->zErrMsg==0 || pTo->nErr );
773 if( pTo->nErr==0 ){
774 pTo->zErrMsg = pFrom->zErrMsg;
775 pTo->nErr = pFrom->nErr;
776 }else{
777 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
778 }
779}
780
dan65a7cd12009-09-01 12:16:01 +0000781/*
782** Create and populate a new TriggerPrg object with a sub-program
783** implementing trigger pTrigger with ON CONFLICT policy orconf.
784*/
dan2832ad42009-08-31 15:27:27 +0000785static TriggerPrg *codeRowTrigger(
dan165921a2009-08-28 18:53:45 +0000786 Parse *pParse, /* Current parse context */
787 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +0000788 Table *pTab, /* The table pTrigger is attached to */
789 int orconf /* ON CONFLICT policy to code trigger program with */
dan165921a2009-08-28 18:53:45 +0000790){
dan65a7cd12009-09-01 12:16:01 +0000791 Parse *pTop = sqlite3ParseToplevel(pParse);
792 sqlite3 *db = pParse->db; /* Database handle */
793 TriggerPrg *pPrg; /* Value to return */
dan165921a2009-08-28 18:53:45 +0000794 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
795 Vdbe *v; /* Temporary VM */
dan165921a2009-08-28 18:53:45 +0000796 NameContext sNC; /* Name context for sub-vdbe */
797 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
798 Parse *pSubParse; /* Parse context for sub-vdbe */
799 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
800
dan1da40a32009-09-19 17:00:31 +0000801 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dan65a7cd12009-09-01 12:16:01 +0000802
803 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
804 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
805 ** list of the top-level Parse object sooner rather than later. */
dan2832ad42009-08-31 15:27:27 +0000806 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
807 if( !pPrg ) return 0;
dan65a7cd12009-09-01 12:16:01 +0000808 pPrg->pNext = pTop->pTriggerPrg;
809 pTop->pTriggerPrg = pPrg;
dan2832ad42009-08-31 15:27:27 +0000810 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
dan165921a2009-08-28 18:53:45 +0000811 if( !pProgram ) return 0;
812 pProgram->nRef = 1;
dan2832ad42009-08-31 15:27:27 +0000813 pPrg->pTrigger = pTrigger;
814 pPrg->orconf = orconf;
danbb5f1682009-11-27 12:12:34 +0000815 pPrg->aColmask[0] = 0xffffffff;
816 pPrg->aColmask[1] = 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;
drh8b307fb2010-04-06 15:57:05 +0000829 pSubParse->nQueryLoop = pParse->nQueryLoop;
dan165921a2009-08-28 18:53:45 +0000830
831 v = sqlite3GetVdbe(pSubParse);
832 if( v ){
dan76d462e2009-08-30 11:42:51 +0000833 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
834 pTrigger->zName, onErrorText(orconf),
dan165921a2009-08-28 18:53:45 +0000835 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
dan65a7cd12009-09-01 12:16:01 +0000836 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
837 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
838 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
dan76d462e2009-08-30 11:42:51 +0000839 pTab->zName
dan165921a2009-08-28 18:53:45 +0000840 ));
dan76d462e2009-08-30 11:42:51 +0000841#ifndef SQLITE_OMIT_TRACE
842 sqlite3VdbeChangeP4(v, -1,
843 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
844 );
845#endif
dan165921a2009-08-28 18:53:45 +0000846
dan65a7cd12009-09-01 12:16:01 +0000847 /* If one was specified, code the WHEN clause. If it evaluates to false
848 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
849 ** OP_Halt inserted at the end of the program. */
dan165921a2009-08-28 18:53:45 +0000850 if( pTrigger->pWhen ){
dan165921a2009-08-28 18:53:45 +0000851 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
dan523a0872009-08-31 05:23:32 +0000852 if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
853 && db->mallocFailed==0
854 ){
dan165921a2009-08-28 18:53:45 +0000855 iEndTrigger = sqlite3VdbeMakeLabel(v);
856 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
857 }
858 sqlite3ExprDelete(db, pWhen);
859 }
860
861 /* Code the trigger program into the sub-vdbe. */
dan76d462e2009-08-30 11:42:51 +0000862 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
dan65a7cd12009-09-01 12:16:01 +0000863
864 /* Insert an OP_Halt at the end of the sub-program. */
dan165921a2009-08-28 18:53:45 +0000865 if( iEndTrigger ){
866 sqlite3VdbeResolveLabel(v, iEndTrigger);
867 }
868 sqlite3VdbeAddOp0(v, OP_Halt);
dan76d462e2009-08-30 11:42:51 +0000869 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
870
dan165921a2009-08-28 18:53:45 +0000871 transferParseError(pParse, pSubParse);
dan523a0872009-08-31 05:23:32 +0000872 if( db->mallocFailed==0 ){
dan65a7cd12009-09-01 12:16:01 +0000873 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
dan523a0872009-08-31 05:23:32 +0000874 }
dan165921a2009-08-28 18:53:45 +0000875 pProgram->nMem = pSubParse->nMem;
876 pProgram->nCsr = pSubParse->nTab;
877 pProgram->token = (void *)pTrigger;
danbb5f1682009-11-27 12:12:34 +0000878 pPrg->aColmask[0] = pSubParse->oldmask;
879 pPrg->aColmask[1] = pSubParse->newmask;
dan165921a2009-08-28 18:53:45 +0000880 sqlite3VdbeDelete(v);
dan165921a2009-08-28 18:53:45 +0000881 }
dan65a7cd12009-09-01 12:16:01 +0000882
883 assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
884 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
dan165921a2009-08-28 18:53:45 +0000885 sqlite3StackFree(db, pSubParse);
886
dan2832ad42009-08-31 15:27:27 +0000887 return pPrg;
dan165921a2009-08-28 18:53:45 +0000888}
889
dan65a7cd12009-09-01 12:16:01 +0000890/*
891** Return a pointer to a TriggerPrg object containing the sub-program for
892** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
893** TriggerPrg object exists, a new object is allocated and populated before
894** being returned.
895*/
dan2832ad42009-08-31 15:27:27 +0000896static TriggerPrg *getRowTrigger(
dan65a7cd12009-09-01 12:16:01 +0000897 Parse *pParse, /* Current parse context */
dan165921a2009-08-28 18:53:45 +0000898 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +0000899 Table *pTab, /* The table trigger pTrigger is attached to */
900 int orconf /* ON CONFLICT algorithm. */
dan165921a2009-08-28 18:53:45 +0000901){
dan65a7cd12009-09-01 12:16:01 +0000902 Parse *pRoot = sqlite3ParseToplevel(pParse);
dan2832ad42009-08-31 15:27:27 +0000903 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +0000904
dan1da40a32009-09-19 17:00:31 +0000905 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dan165921a2009-08-28 18:53:45 +0000906
907 /* It may be that this trigger has already been coded (or is in the
908 ** process of being coded). If this is the case, then an entry with
dan2832ad42009-08-31 15:27:27 +0000909 ** a matching TriggerPrg.pTrigger field will be present somewhere
910 ** in the Parse.pTriggerPrg list. Search for such an entry. */
dan2832ad42009-08-31 15:27:27 +0000911 for(pPrg=pRoot->pTriggerPrg;
912 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
913 pPrg=pPrg->pNext
dan165921a2009-08-28 18:53:45 +0000914 );
915
dan65a7cd12009-09-01 12:16:01 +0000916 /* If an existing TriggerPrg could not be located, create a new one. */
dan2832ad42009-08-31 15:27:27 +0000917 if( !pPrg ){
dan65a7cd12009-09-01 12:16:01 +0000918 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
dan165921a2009-08-28 18:53:45 +0000919 }
920
dan2832ad42009-08-31 15:27:27 +0000921 return pPrg;
dan165921a2009-08-28 18:53:45 +0000922}
923
dan94d7f502009-09-24 09:05:49 +0000924/*
925** Generate code for the trigger program associated with trigger p on
926** table pTab. The reg, orconf and ignoreJump parameters passed to this
927** function are the same as those described in the header function for
928** sqlite3CodeRowTrigger()
929*/
dan1da40a32009-09-19 17:00:31 +0000930void sqlite3CodeRowTriggerDirect(
931 Parse *pParse, /* Parse context */
932 Trigger *p, /* Trigger to code */
933 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +0000934 int reg, /* Reg array containing OLD.* and NEW.* values */
dan1da40a32009-09-19 17:00:31 +0000935 int orconf, /* ON CONFLICT policy */
936 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
937){
938 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
939 TriggerPrg *pPrg;
940 pPrg = getRowTrigger(pParse, p, pTab, orconf);
941 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
942
943 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
944 ** is a pointer to the sub-vdbe containing the trigger program. */
945 if( pPrg ){
dan94d7f502009-09-24 09:05:49 +0000946 sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem);
dan1da40a32009-09-19 17:00:31 +0000947 pPrg->pProgram->nRef++;
948 sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
949 VdbeComment(
950 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
951
952 /* Set the P5 operand of the OP_Program instruction to non-zero if
953 ** recursive invocation of this trigger program is disallowed. Recursive
954 ** invocation is disallowed if (a) the sub-program is really a trigger,
955 ** not a foreign key action, and (b) the flag to enable recursive triggers
956 ** is clear. */
drhfcb9f7a2009-10-13 19:19:23 +0000957 sqlite3VdbeChangeP5(v, (u8)(p->zName && !(pParse->db->flags&SQLITE_RecTriggers)));
dan1da40a32009-09-19 17:00:31 +0000958 }
959}
960
danielk1977633ed082002-05-17 00:05:58 +0000961/*
dan94d7f502009-09-24 09:05:49 +0000962** This is called to code the required FOR EACH ROW triggers for an operation
963** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
964** is given by the op paramater. The tr_tm parameter determines whether the
965** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
966** parameter pChanges is passed the list of columns being modified.
danielk1977633ed082002-05-17 00:05:58 +0000967**
dan94d7f502009-09-24 09:05:49 +0000968** If there are no triggers that fire at the specified time for the specified
969** operation on pTab, this function is a no-op.
drhc977f7f2002-05-21 11:38:11 +0000970**
dan94d7f502009-09-24 09:05:49 +0000971** The reg argument is the address of the first in an array of registers
972** that contain the values substituted for the new.* and old.* references
973** in the trigger program. If N is the number of columns in table pTab
974** (a copy of pTab->nCol), then registers are populated as follows:
drhc977f7f2002-05-21 11:38:11 +0000975**
dan94d7f502009-09-24 09:05:49 +0000976** Register Contains
977** ------------------------------------------------------
978** reg+0 OLD.rowid
979** reg+1 OLD.* value of left-most column of pTab
980** ... ...
981** reg+N OLD.* value of right-most column of pTab
982** reg+N+1 NEW.rowid
983** reg+N+2 OLD.* value of left-most column of pTab
984** ... ...
985** reg+N+N+1 NEW.* value of right-most column of pTab
drhc977f7f2002-05-21 11:38:11 +0000986**
dan94d7f502009-09-24 09:05:49 +0000987** For ON DELETE triggers, the registers containing the NEW.* values will
988** never be accessed by the trigger program, so they are not allocated or
989** populated by the caller (there is no data to populate them with anyway).
990** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
991** are never accessed, and so are not allocated by the caller. So, for an
992** ON INSERT trigger, the value passed to this function as parameter reg
993** is not a readable register, although registers (reg+N) through
994** (reg+N+N+1) are.
danielk1977633ed082002-05-17 00:05:58 +0000995**
dan94d7f502009-09-24 09:05:49 +0000996** Parameter orconf is the default conflict resolution algorithm for the
997** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
998** is the instruction that control should jump to if a trigger program
999** raises an IGNORE exception.
danielk1977633ed082002-05-17 00:05:58 +00001000*/
dan165921a2009-08-28 18:53:45 +00001001void sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +00001002 Parse *pParse, /* Parse context */
danielk19772f886d12009-02-28 10:47:41 +00001003 Trigger *pTrigger, /* List of triggers on table pTab */
danielk1977633ed082002-05-17 00:05:58 +00001004 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
1005 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +00001006 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +00001007 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001008 int reg, /* The first in an array of registers (see above) */
danielk19776f349032002-06-11 02:25:40 +00001009 int orconf, /* ON CONFLICT policy */
dan165921a2009-08-28 18:53:45 +00001010 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
drhc977f7f2002-05-21 11:38:11 +00001011){
dan94d7f502009-09-24 09:05:49 +00001012 Trigger *p; /* Used to iterate through pTrigger list */
danielk19778f2c54e2008-01-01 19:02:09 +00001013
dan94d7f502009-09-24 09:05:49 +00001014 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
1015 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
1016 assert( (op==TK_UPDATE)==(pChanges!=0) );
danielk1977c3f9bad2002-05-15 08:30:12 +00001017
danielk19772f886d12009-02-28 10:47:41 +00001018 for(p=pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +00001019
drh9ab4c2e2009-05-09 00:18:38 +00001020 /* Sanity checking: The schema for the trigger and for the table are
1021 ** always defined. The trigger must be in the same schema as the table
1022 ** or else it must be a TEMP trigger. */
1023 assert( p->pSchema!=0 );
1024 assert( p->pTabSchema!=0 );
dan165921a2009-08-28 18:53:45 +00001025 assert( p->pSchema==p->pTabSchema
1026 || p->pSchema==pParse->db->aDb[1].pSchema );
drh9ab4c2e2009-05-09 00:18:38 +00001027
danielk1977eecfb3e2006-01-10 12:31:39 +00001028 /* Determine whether we should code this trigger */
dan165921a2009-08-28 18:53:45 +00001029 if( p->op==op
1030 && p->tr_tm==tr_tm
dan94d7f502009-09-24 09:05:49 +00001031 && checkColumnOverlap(p->pColumns, pChanges)
danielk1977eecfb3e2006-01-10 12:31:39 +00001032 ){
dan94d7f502009-09-24 09:05:49 +00001033 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
danielk1977c3f9bad2002-05-15 08:30:12 +00001034 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001035 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001036}
dan165921a2009-08-28 18:53:45 +00001037
dan2832ad42009-08-31 15:27:27 +00001038/*
danbb5f1682009-11-27 12:12:34 +00001039** Triggers may access values stored in the old.* or new.* pseudo-table.
1040** This function returns a 32-bit bitmask indicating which columns of the
1041** old.* or new.* tables actually are used by triggers. This information
1042** may be used by the caller, for example, to avoid having to load the entire
1043** old.* record into memory when executing an UPDATE or DELETE command.
dan2832ad42009-08-31 15:27:27 +00001044**
1045** Bit 0 of the returned mask is set if the left-most column of the
danbb5f1682009-11-27 12:12:34 +00001046** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
dan2832ad42009-08-31 15:27:27 +00001047** the second leftmost column value is required, and so on. If there
1048** are more than 32 columns in the table, and at least one of the columns
1049** with an index greater than 32 may be accessed, 0xffffffff is returned.
1050**
danbb5f1682009-11-27 12:12:34 +00001051** It is not possible to determine if the old.rowid or new.rowid column is
1052** accessed by triggers. The caller must always assume that it is.
dan2832ad42009-08-31 15:27:27 +00001053**
danbb5f1682009-11-27 12:12:34 +00001054** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
1055** applies to the old.* table. If 1, the new.* table.
1056**
1057** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
1058** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
1059** included in the returned mask if the TRIGGER_BEFORE bit is set in the
1060** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
1061** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
dan2832ad42009-08-31 15:27:27 +00001062*/
danbb5f1682009-11-27 12:12:34 +00001063u32 sqlite3TriggerColmask(
dan165921a2009-08-28 18:53:45 +00001064 Parse *pParse, /* Parse context */
1065 Trigger *pTrigger, /* List of triggers on table pTab */
dan165921a2009-08-28 18:53:45 +00001066 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
danbb5f1682009-11-27 12:12:34 +00001067 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
1068 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
dan165921a2009-08-28 18:53:45 +00001069 Table *pTab, /* The table to code triggers from */
dan2832ad42009-08-31 15:27:27 +00001070 int orconf /* Default ON CONFLICT policy for trigger steps */
dan165921a2009-08-28 18:53:45 +00001071){
dan1da40a32009-09-19 17:00:31 +00001072 const int op = pChanges ? TK_UPDATE : TK_DELETE;
dan2832ad42009-08-31 15:27:27 +00001073 u32 mask = 0;
dan165921a2009-08-28 18:53:45 +00001074 Trigger *p;
dan165921a2009-08-28 18:53:45 +00001075
danbb5f1682009-11-27 12:12:34 +00001076 assert( isNew==1 || isNew==0 );
dan165921a2009-08-28 18:53:45 +00001077 for(p=pTrigger; p; p=p->pNext){
danbb5f1682009-11-27 12:12:34 +00001078 if( p->op==op && (tr_tm&p->tr_tm)
1079 && checkColumnOverlap(p->pColumns,pChanges)
1080 ){
dan2832ad42009-08-31 15:27:27 +00001081 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +00001082 pPrg = getRowTrigger(pParse, p, pTab, orconf);
dan2832ad42009-08-31 15:27:27 +00001083 if( pPrg ){
danbb5f1682009-11-27 12:12:34 +00001084 mask |= pPrg->aColmask[isNew];
dan165921a2009-08-28 18:53:45 +00001085 }
1086 }
1087 }
dan2832ad42009-08-31 15:27:27 +00001088
1089 return mask;
dan165921a2009-08-28 18:53:45 +00001090}
1091
drhb7f91642004-10-31 02:22:47 +00001092#endif /* !defined(SQLITE_OMIT_TRIGGER) */