blob: f66833522b4742aa192cbe822bde6a9602a2ec9c [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
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 }
dan1db95102010-06-28 10:15:19 +0000498 pParse->checkSchema = 1;
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;
drhe83cafd2011-03-21 17:15:58 +0000626 Trigger *pList = 0;
danielk19772f886d12009-02-28 10:47:41 +0000627 Trigger *p;
drhe83cafd2011-03-21 17:15:58 +0000628
629 if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
630 pList = sqlite3TriggerList(pParse, pTab);
631 }
danielk19772f886d12009-02-28 10:47:41 +0000632 assert( pList==0 || IsVirtual(pTab)==0 );
633 for(p=pList; p; p=p->pNext){
drh9ab4c2e2009-05-09 00:18:38 +0000634 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
danielk19772f886d12009-02-28 10:47:41 +0000635 mask |= p->tr_tm;
danielk1977c3f9bad2002-05-15 08:30:12 +0000636 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000637 }
danielk19772f886d12009-02-28 10:47:41 +0000638 if( pMask ){
639 *pMask = mask;
640 }
641 return (mask ? pList : 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000642}
643
drhc977f7f2002-05-21 11:38:11 +0000644/*
drhf26e09c2003-05-31 16:21:12 +0000645** Convert the pStep->target token into a SrcList and return a pointer
646** to that SrcList.
647**
648** This routine adds a specific database name, if needed, to the target when
649** forming the SrcList. This prevents a trigger in one database from
650** referring to a target in another database. An exception is when the
651** trigger is in TEMP in which case it can refer to any other database it
652** wants.
653*/
654static SrcList *targetSrcList(
655 Parse *pParse, /* The parsing context */
656 TriggerStep *pStep /* The trigger containing the target token */
657){
drhf26e09c2003-05-31 16:21:12 +0000658 int iDb; /* Index of the database to use */
659 SrcList *pSrc; /* SrcList to be returned */
660
drhb7916a72009-05-27 10:31:29 +0000661 pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
662 if( pSrc ){
663 assert( pSrc->nSrc>0 );
664 assert( pSrc->a!=0 );
665 iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
666 if( iDb==0 || iDb>=2 ){
667 sqlite3 *db = pParse->db;
668 assert( iDb<pParse->db->nDb );
669 pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
670 }
drhf26e09c2003-05-31 16:21:12 +0000671 }
672 return pSrc;
673}
674
675/*
dan65a7cd12009-09-01 12:16:01 +0000676** Generate VDBE code for the statements inside the body of a single
677** trigger.
drhc977f7f2002-05-21 11:38:11 +0000678*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000679static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000680 Parse *pParse, /* The parser context */
681 TriggerStep *pStepList, /* List of statements inside the trigger body */
dan65a7cd12009-09-01 12:16:01 +0000682 int orconf /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000683){
dan65a7cd12009-09-01 12:16:01 +0000684 TriggerStep *pStep;
drh344737f2004-09-19 00:50:20 +0000685 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000686 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000687
dan65a7cd12009-09-01 12:16:01 +0000688 assert( pParse->pTriggerTab && pParse->pToplevel );
689 assert( pStepList );
drh344737f2004-09-19 00:50:20 +0000690 assert( v!=0 );
dan65a7cd12009-09-01 12:16:01 +0000691 for(pStep=pStepList; pStep; pStep=pStep->pNext){
dan165921a2009-08-28 18:53:45 +0000692 /* Figure out the ON CONFLICT policy that will be used for this step
693 ** of the trigger program. If the statement that caused this trigger
694 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
695 ** the ON CONFLICT policy that was specified as part of the trigger
696 ** step statement. Example:
697 **
698 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
699 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
700 ** END;
701 **
702 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
703 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
704 */
shanecea72b22009-09-07 04:38:36 +0000705 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
dan165921a2009-08-28 18:53:45 +0000706
707 switch( pStep->op ){
danielk1977633ed082002-05-17 00:05:58 +0000708 case TK_UPDATE: {
dan165921a2009-08-28 18:53:45 +0000709 sqlite3Update(pParse,
710 targetSrcList(pParse, pStep),
711 sqlite3ExprListDup(db, pStep->pExprList, 0),
712 sqlite3ExprDup(db, pStep->pWhere, 0),
dan65a7cd12009-09-01 12:16:01 +0000713 pParse->eOrconf
dan165921a2009-08-28 18:53:45 +0000714 );
danielk1977633ed082002-05-17 00:05:58 +0000715 break;
716 }
717 case TK_INSERT: {
dan165921a2009-08-28 18:53:45 +0000718 sqlite3Insert(pParse,
719 targetSrcList(pParse, pStep),
720 sqlite3ExprListDup(db, pStep->pExprList, 0),
721 sqlite3SelectDup(db, pStep->pSelect, 0),
722 sqlite3IdListDup(db, pStep->pIdList),
dan65a7cd12009-09-01 12:16:01 +0000723 pParse->eOrconf
dan165921a2009-08-28 18:53:45 +0000724 );
danielk1977633ed082002-05-17 00:05:58 +0000725 break;
726 }
727 case TK_DELETE: {
dan165921a2009-08-28 18:53:45 +0000728 sqlite3DeleteFrom(pParse,
729 targetSrcList(pParse, pStep),
730 sqlite3ExprDup(db, pStep->pWhere, 0)
731 );
danielk1977633ed082002-05-17 00:05:58 +0000732 break;
733 }
dan165921a2009-08-28 18:53:45 +0000734 default: assert( pStep->op==TK_SELECT ); {
735 SelectDest sDest;
736 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
737 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
738 sqlite3Select(pParse, pSelect, &sDest);
739 sqlite3SelectDelete(db, pSelect);
drh9ab4c2e2009-05-09 00:18:38 +0000740 break;
741 }
danielk1977633ed082002-05-17 00:05:58 +0000742 }
dan76d462e2009-08-30 11:42:51 +0000743 if( pStep->op!=TK_SELECT ){
drhb7f1d9a2009-09-08 02:27:58 +0000744 sqlite3VdbeAddOp0(v, OP_ResetCount);
dan76d462e2009-08-30 11:42:51 +0000745 }
danielk1977633ed082002-05-17 00:05:58 +0000746 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000747
danielk1977633ed082002-05-17 00:05:58 +0000748 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000749}
750
dan165921a2009-08-28 18:53:45 +0000751#ifdef SQLITE_DEBUG
752/*
753** This function is used to add VdbeComment() annotations to a VDBE
754** program. It is not used in production code, only for debugging.
755*/
756static const char *onErrorText(int onError){
757 switch( onError ){
758 case OE_Abort: return "abort";
759 case OE_Rollback: return "rollback";
760 case OE_Fail: return "fail";
761 case OE_Replace: return "replace";
762 case OE_Ignore: return "ignore";
763 case OE_Default: return "default";
764 }
765 return "n/a";
766}
767#endif
768
769/*
770** Parse context structure pFrom has just been used to create a sub-vdbe
771** (trigger program). If an error has occurred, transfer error information
772** from pFrom to pTo.
773*/
774static void transferParseError(Parse *pTo, Parse *pFrom){
775 assert( pFrom->zErrMsg==0 || pFrom->nErr );
776 assert( pTo->zErrMsg==0 || pTo->nErr );
777 if( pTo->nErr==0 ){
778 pTo->zErrMsg = pFrom->zErrMsg;
779 pTo->nErr = pFrom->nErr;
780 }else{
781 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
782 }
783}
784
dan65a7cd12009-09-01 12:16:01 +0000785/*
786** Create and populate a new TriggerPrg object with a sub-program
787** implementing trigger pTrigger with ON CONFLICT policy orconf.
788*/
dan2832ad42009-08-31 15:27:27 +0000789static TriggerPrg *codeRowTrigger(
dan165921a2009-08-28 18:53:45 +0000790 Parse *pParse, /* Current parse context */
791 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +0000792 Table *pTab, /* The table pTrigger is attached to */
793 int orconf /* ON CONFLICT policy to code trigger program with */
dan165921a2009-08-28 18:53:45 +0000794){
dan65a7cd12009-09-01 12:16:01 +0000795 Parse *pTop = sqlite3ParseToplevel(pParse);
796 sqlite3 *db = pParse->db; /* Database handle */
797 TriggerPrg *pPrg; /* Value to return */
dan165921a2009-08-28 18:53:45 +0000798 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
799 Vdbe *v; /* Temporary VM */
dan165921a2009-08-28 18:53:45 +0000800 NameContext sNC; /* Name context for sub-vdbe */
801 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
802 Parse *pSubParse; /* Parse context for sub-vdbe */
803 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
804
dan1da40a32009-09-19 17:00:31 +0000805 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dand19c9332010-07-26 12:05:17 +0000806 assert( pTop->pVdbe );
dan65a7cd12009-09-01 12:16:01 +0000807
808 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
809 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
810 ** list of the top-level Parse object sooner rather than later. */
dan2832ad42009-08-31 15:27:27 +0000811 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
812 if( !pPrg ) return 0;
dan65a7cd12009-09-01 12:16:01 +0000813 pPrg->pNext = pTop->pTriggerPrg;
814 pTop->pTriggerPrg = pPrg;
dan2832ad42009-08-31 15:27:27 +0000815 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
dan165921a2009-08-28 18:53:45 +0000816 if( !pProgram ) return 0;
dand19c9332010-07-26 12:05:17 +0000817 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
dan2832ad42009-08-31 15:27:27 +0000818 pPrg->pTrigger = pTrigger;
819 pPrg->orconf = orconf;
danbb5f1682009-11-27 12:12:34 +0000820 pPrg->aColmask[0] = 0xffffffff;
821 pPrg->aColmask[1] = 0xffffffff;
dan165921a2009-08-28 18:53:45 +0000822
dan65a7cd12009-09-01 12:16:01 +0000823 /* Allocate and populate a new Parse context to use for coding the
824 ** trigger sub-program. */
825 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
826 if( !pSubParse ) return 0;
dan165921a2009-08-28 18:53:45 +0000827 memset(&sNC, 0, sizeof(sNC));
828 sNC.pParse = pSubParse;
829 pSubParse->db = db;
830 pSubParse->pTriggerTab = pTab;
dan65a7cd12009-09-01 12:16:01 +0000831 pSubParse->pToplevel = pTop;
dan2bd93512009-08-31 08:22:46 +0000832 pSubParse->zAuthContext = pTrigger->zName;
dan65a7cd12009-09-01 12:16:01 +0000833 pSubParse->eTriggerOp = pTrigger->op;
drh8b307fb2010-04-06 15:57:05 +0000834 pSubParse->nQueryLoop = pParse->nQueryLoop;
dan165921a2009-08-28 18:53:45 +0000835
836 v = sqlite3GetVdbe(pSubParse);
837 if( v ){
dan76d462e2009-08-30 11:42:51 +0000838 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
839 pTrigger->zName, onErrorText(orconf),
dan165921a2009-08-28 18:53:45 +0000840 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
dan65a7cd12009-09-01 12:16:01 +0000841 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
842 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
843 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
dan76d462e2009-08-30 11:42:51 +0000844 pTab->zName
dan165921a2009-08-28 18:53:45 +0000845 ));
dan76d462e2009-08-30 11:42:51 +0000846#ifndef SQLITE_OMIT_TRACE
847 sqlite3VdbeChangeP4(v, -1,
848 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
849 );
850#endif
dan165921a2009-08-28 18:53:45 +0000851
dan65a7cd12009-09-01 12:16:01 +0000852 /* If one was specified, code the WHEN clause. If it evaluates to false
853 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
854 ** OP_Halt inserted at the end of the program. */
dan165921a2009-08-28 18:53:45 +0000855 if( pTrigger->pWhen ){
dan165921a2009-08-28 18:53:45 +0000856 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
dan523a0872009-08-31 05:23:32 +0000857 if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
858 && db->mallocFailed==0
859 ){
dan165921a2009-08-28 18:53:45 +0000860 iEndTrigger = sqlite3VdbeMakeLabel(v);
861 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
862 }
863 sqlite3ExprDelete(db, pWhen);
864 }
865
866 /* Code the trigger program into the sub-vdbe. */
dan76d462e2009-08-30 11:42:51 +0000867 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
dan65a7cd12009-09-01 12:16:01 +0000868
869 /* Insert an OP_Halt at the end of the sub-program. */
dan165921a2009-08-28 18:53:45 +0000870 if( iEndTrigger ){
871 sqlite3VdbeResolveLabel(v, iEndTrigger);
872 }
873 sqlite3VdbeAddOp0(v, OP_Halt);
dan76d462e2009-08-30 11:42:51 +0000874 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
875
dan165921a2009-08-28 18:53:45 +0000876 transferParseError(pParse, pSubParse);
dan523a0872009-08-31 05:23:32 +0000877 if( db->mallocFailed==0 ){
dan65a7cd12009-09-01 12:16:01 +0000878 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
dan523a0872009-08-31 05:23:32 +0000879 }
dan165921a2009-08-28 18:53:45 +0000880 pProgram->nMem = pSubParse->nMem;
881 pProgram->nCsr = pSubParse->nTab;
882 pProgram->token = (void *)pTrigger;
danbb5f1682009-11-27 12:12:34 +0000883 pPrg->aColmask[0] = pSubParse->oldmask;
884 pPrg->aColmask[1] = pSubParse->newmask;
dan165921a2009-08-28 18:53:45 +0000885 sqlite3VdbeDelete(v);
dan165921a2009-08-28 18:53:45 +0000886 }
dan65a7cd12009-09-01 12:16:01 +0000887
888 assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
889 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
dan165921a2009-08-28 18:53:45 +0000890 sqlite3StackFree(db, pSubParse);
891
dan2832ad42009-08-31 15:27:27 +0000892 return pPrg;
dan165921a2009-08-28 18:53:45 +0000893}
894
dan65a7cd12009-09-01 12:16:01 +0000895/*
896** Return a pointer to a TriggerPrg object containing the sub-program for
897** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
898** TriggerPrg object exists, a new object is allocated and populated before
899** being returned.
900*/
dan2832ad42009-08-31 15:27:27 +0000901static TriggerPrg *getRowTrigger(
dan65a7cd12009-09-01 12:16:01 +0000902 Parse *pParse, /* Current parse context */
dan165921a2009-08-28 18:53:45 +0000903 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +0000904 Table *pTab, /* The table trigger pTrigger is attached to */
905 int orconf /* ON CONFLICT algorithm. */
dan165921a2009-08-28 18:53:45 +0000906){
dan65a7cd12009-09-01 12:16:01 +0000907 Parse *pRoot = sqlite3ParseToplevel(pParse);
dan2832ad42009-08-31 15:27:27 +0000908 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +0000909
dan1da40a32009-09-19 17:00:31 +0000910 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dan165921a2009-08-28 18:53:45 +0000911
912 /* It may be that this trigger has already been coded (or is in the
913 ** process of being coded). If this is the case, then an entry with
dan2832ad42009-08-31 15:27:27 +0000914 ** a matching TriggerPrg.pTrigger field will be present somewhere
915 ** in the Parse.pTriggerPrg list. Search for such an entry. */
dan2832ad42009-08-31 15:27:27 +0000916 for(pPrg=pRoot->pTriggerPrg;
917 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
918 pPrg=pPrg->pNext
dan165921a2009-08-28 18:53:45 +0000919 );
920
dan65a7cd12009-09-01 12:16:01 +0000921 /* If an existing TriggerPrg could not be located, create a new one. */
dan2832ad42009-08-31 15:27:27 +0000922 if( !pPrg ){
dan65a7cd12009-09-01 12:16:01 +0000923 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
dan165921a2009-08-28 18:53:45 +0000924 }
925
dan2832ad42009-08-31 15:27:27 +0000926 return pPrg;
dan165921a2009-08-28 18:53:45 +0000927}
928
dan94d7f502009-09-24 09:05:49 +0000929/*
930** Generate code for the trigger program associated with trigger p on
931** table pTab. The reg, orconf and ignoreJump parameters passed to this
932** function are the same as those described in the header function for
933** sqlite3CodeRowTrigger()
934*/
dan1da40a32009-09-19 17:00:31 +0000935void sqlite3CodeRowTriggerDirect(
936 Parse *pParse, /* Parse context */
937 Trigger *p, /* Trigger to code */
938 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +0000939 int reg, /* Reg array containing OLD.* and NEW.* values */
dan1da40a32009-09-19 17:00:31 +0000940 int orconf, /* ON CONFLICT policy */
941 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
942){
943 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
944 TriggerPrg *pPrg;
945 pPrg = getRowTrigger(pParse, p, pTab, orconf);
946 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
947
948 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
949 ** is a pointer to the sub-vdbe containing the trigger program. */
950 if( pPrg ){
dand19c9332010-07-26 12:05:17 +0000951 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
952
dan94d7f502009-09-24 09:05:49 +0000953 sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem);
dan1da40a32009-09-19 17:00:31 +0000954 sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
955 VdbeComment(
956 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
957
958 /* Set the P5 operand of the OP_Program instruction to non-zero if
959 ** recursive invocation of this trigger program is disallowed. Recursive
960 ** invocation is disallowed if (a) the sub-program is really a trigger,
961 ** not a foreign key action, and (b) the flag to enable recursive triggers
962 ** is clear. */
dand19c9332010-07-26 12:05:17 +0000963 sqlite3VdbeChangeP5(v, (u8)bRecursive);
dan1da40a32009-09-19 17:00:31 +0000964 }
965}
966
danielk1977633ed082002-05-17 00:05:58 +0000967/*
dan94d7f502009-09-24 09:05:49 +0000968** This is called to code the required FOR EACH ROW triggers for an operation
969** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
970** is given by the op paramater. The tr_tm parameter determines whether the
971** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
972** parameter pChanges is passed the list of columns being modified.
danielk1977633ed082002-05-17 00:05:58 +0000973**
dan94d7f502009-09-24 09:05:49 +0000974** If there are no triggers that fire at the specified time for the specified
975** operation on pTab, this function is a no-op.
drhc977f7f2002-05-21 11:38:11 +0000976**
dan94d7f502009-09-24 09:05:49 +0000977** The reg argument is the address of the first in an array of registers
978** that contain the values substituted for the new.* and old.* references
979** in the trigger program. If N is the number of columns in table pTab
980** (a copy of pTab->nCol), then registers are populated as follows:
drhc977f7f2002-05-21 11:38:11 +0000981**
dan94d7f502009-09-24 09:05:49 +0000982** Register Contains
983** ------------------------------------------------------
984** reg+0 OLD.rowid
985** reg+1 OLD.* value of left-most column of pTab
986** ... ...
987** reg+N OLD.* value of right-most column of pTab
988** reg+N+1 NEW.rowid
989** reg+N+2 OLD.* value of left-most column of pTab
990** ... ...
991** reg+N+N+1 NEW.* value of right-most column of pTab
drhc977f7f2002-05-21 11:38:11 +0000992**
dan94d7f502009-09-24 09:05:49 +0000993** For ON DELETE triggers, the registers containing the NEW.* values will
994** never be accessed by the trigger program, so they are not allocated or
995** populated by the caller (there is no data to populate them with anyway).
996** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
997** are never accessed, and so are not allocated by the caller. So, for an
998** ON INSERT trigger, the value passed to this function as parameter reg
999** is not a readable register, although registers (reg+N) through
1000** (reg+N+N+1) are.
danielk1977633ed082002-05-17 00:05:58 +00001001**
dan94d7f502009-09-24 09:05:49 +00001002** Parameter orconf is the default conflict resolution algorithm for the
1003** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
1004** is the instruction that control should jump to if a trigger program
1005** raises an IGNORE exception.
danielk1977633ed082002-05-17 00:05:58 +00001006*/
dan165921a2009-08-28 18:53:45 +00001007void sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +00001008 Parse *pParse, /* Parse context */
danielk19772f886d12009-02-28 10:47:41 +00001009 Trigger *pTrigger, /* List of triggers on table pTab */
danielk1977633ed082002-05-17 00:05:58 +00001010 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
1011 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +00001012 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +00001013 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001014 int reg, /* The first in an array of registers (see above) */
danielk19776f349032002-06-11 02:25:40 +00001015 int orconf, /* ON CONFLICT policy */
dan165921a2009-08-28 18:53:45 +00001016 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
drhc977f7f2002-05-21 11:38:11 +00001017){
dan94d7f502009-09-24 09:05:49 +00001018 Trigger *p; /* Used to iterate through pTrigger list */
danielk19778f2c54e2008-01-01 19:02:09 +00001019
dan94d7f502009-09-24 09:05:49 +00001020 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
1021 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
1022 assert( (op==TK_UPDATE)==(pChanges!=0) );
danielk1977c3f9bad2002-05-15 08:30:12 +00001023
danielk19772f886d12009-02-28 10:47:41 +00001024 for(p=pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +00001025
drh9ab4c2e2009-05-09 00:18:38 +00001026 /* Sanity checking: The schema for the trigger and for the table are
1027 ** always defined. The trigger must be in the same schema as the table
1028 ** or else it must be a TEMP trigger. */
1029 assert( p->pSchema!=0 );
1030 assert( p->pTabSchema!=0 );
dan165921a2009-08-28 18:53:45 +00001031 assert( p->pSchema==p->pTabSchema
1032 || p->pSchema==pParse->db->aDb[1].pSchema );
drh9ab4c2e2009-05-09 00:18:38 +00001033
danielk1977eecfb3e2006-01-10 12:31:39 +00001034 /* Determine whether we should code this trigger */
dan165921a2009-08-28 18:53:45 +00001035 if( p->op==op
1036 && p->tr_tm==tr_tm
dan94d7f502009-09-24 09:05:49 +00001037 && checkColumnOverlap(p->pColumns, pChanges)
danielk1977eecfb3e2006-01-10 12:31:39 +00001038 ){
dan94d7f502009-09-24 09:05:49 +00001039 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
danielk1977c3f9bad2002-05-15 08:30:12 +00001040 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001041 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001042}
dan165921a2009-08-28 18:53:45 +00001043
dan2832ad42009-08-31 15:27:27 +00001044/*
danbb5f1682009-11-27 12:12:34 +00001045** Triggers may access values stored in the old.* or new.* pseudo-table.
1046** This function returns a 32-bit bitmask indicating which columns of the
1047** old.* or new.* tables actually are used by triggers. This information
1048** may be used by the caller, for example, to avoid having to load the entire
1049** old.* record into memory when executing an UPDATE or DELETE command.
dan2832ad42009-08-31 15:27:27 +00001050**
1051** Bit 0 of the returned mask is set if the left-most column of the
danbb5f1682009-11-27 12:12:34 +00001052** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
dan2832ad42009-08-31 15:27:27 +00001053** the second leftmost column value is required, and so on. If there
1054** are more than 32 columns in the table, and at least one of the columns
1055** with an index greater than 32 may be accessed, 0xffffffff is returned.
1056**
danbb5f1682009-11-27 12:12:34 +00001057** It is not possible to determine if the old.rowid or new.rowid column is
1058** accessed by triggers. The caller must always assume that it is.
dan2832ad42009-08-31 15:27:27 +00001059**
danbb5f1682009-11-27 12:12:34 +00001060** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
1061** applies to the old.* table. If 1, the new.* table.
1062**
1063** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
1064** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
1065** included in the returned mask if the TRIGGER_BEFORE bit is set in the
1066** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
1067** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
dan2832ad42009-08-31 15:27:27 +00001068*/
danbb5f1682009-11-27 12:12:34 +00001069u32 sqlite3TriggerColmask(
dan165921a2009-08-28 18:53:45 +00001070 Parse *pParse, /* Parse context */
1071 Trigger *pTrigger, /* List of triggers on table pTab */
dan165921a2009-08-28 18:53:45 +00001072 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
danbb5f1682009-11-27 12:12:34 +00001073 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
1074 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
dan165921a2009-08-28 18:53:45 +00001075 Table *pTab, /* The table to code triggers from */
dan2832ad42009-08-31 15:27:27 +00001076 int orconf /* Default ON CONFLICT policy for trigger steps */
dan165921a2009-08-28 18:53:45 +00001077){
dan1da40a32009-09-19 17:00:31 +00001078 const int op = pChanges ? TK_UPDATE : TK_DELETE;
dan2832ad42009-08-31 15:27:27 +00001079 u32 mask = 0;
dan165921a2009-08-28 18:53:45 +00001080 Trigger *p;
dan165921a2009-08-28 18:53:45 +00001081
danbb5f1682009-11-27 12:12:34 +00001082 assert( isNew==1 || isNew==0 );
dan165921a2009-08-28 18:53:45 +00001083 for(p=pTrigger; p; p=p->pNext){
danbb5f1682009-11-27 12:12:34 +00001084 if( p->op==op && (tr_tm&p->tr_tm)
1085 && checkColumnOverlap(p->pColumns,pChanges)
1086 ){
dan2832ad42009-08-31 15:27:27 +00001087 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +00001088 pPrg = getRowTrigger(pParse, p, pTab, orconf);
dan2832ad42009-08-31 15:27:27 +00001089 if( pPrg ){
danbb5f1682009-11-27 12:12:34 +00001090 mask |= pPrg->aColmask[isNew];
dan165921a2009-08-28 18:53:45 +00001091 }
1092 }
1093 }
dan2832ad42009-08-31 15:27:27 +00001094
1095 return mask;
dan165921a2009-08-28 18:53:45 +00001096}
1097
drhb7f91642004-10-31 02:22:47 +00001098#endif /* !defined(SQLITE_OMIT_TRIGGER) */