blob: 6242de5e6c2289f958aa1188a5b4215d4909a07b [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;
drh21206082011-04-04 18:22:02 +000057 assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) );
danielk19772f886d12009-02-28 10:47:41 +000058 for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
59 Trigger *pTrig = (Trigger *)sqliteHashData(p);
60 if( pTrig->pTabSchema==pTab->pSchema
61 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
62 ){
63 pTrig->pNext = (pList ? pList : pTab->pTrigger);
64 pList = pTrig;
65 }
66 }
67 }
68
69 return (pList ? pList : pTab->pTrigger);
70}
71
72/*
drhf0f258b2003-04-21 18:48:45 +000073** This is called by the parser when it sees a CREATE TRIGGER statement
74** up to the point of the BEGIN before the trigger actions. A Trigger
75** structure is generated based on the information available and stored
76** in pParse->pNewTrigger. After the trigger actions have been parsed, the
danielk19774adee202004-05-08 08:23:19 +000077** sqlite3FinishTrigger() function is called to complete the trigger
drhf0f258b2003-04-21 18:48:45 +000078** construction process.
drh9adf9ac2002-05-15 11:44:13 +000079*/
danielk19774adee202004-05-08 08:23:19 +000080void sqlite3BeginTrigger(
drh9adf9ac2002-05-15 11:44:13 +000081 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
danielk1977ef2cb632004-05-29 02:37:19 +000082 Token *pName1, /* The name of the trigger */
83 Token *pName2, /* The name of the trigger */
drh5cf590c2003-04-24 01:45:04 +000084 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
drh9adf9ac2002-05-15 11:44:13 +000085 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
danielk1977633ed082002-05-17 00:05:58 +000086 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
drhd24cc422003-03-27 12:51:24 +000087 SrcList *pTableName,/* The name of the table/view the trigger applies to */
drh9adf9ac2002-05-15 11:44:13 +000088 Expr *pWhen, /* WHEN clause */
drhfdd48a72006-09-11 23:45:48 +000089 int isTemp, /* True if the TEMPORARY keyword is present */
90 int noErr /* Suppress errors if the trigger already exists */
drh9adf9ac2002-05-15 11:44:13 +000091){
drh3d5f74b2009-08-06 17:43:31 +000092 Trigger *pTrigger = 0; /* The new trigger */
93 Table *pTab; /* Table that the trigger fires off of */
drhf0f258b2003-04-21 18:48:45 +000094 char *zName = 0; /* Name of the trigger */
drh3d5f74b2009-08-06 17:43:31 +000095 sqlite3 *db = pParse->db; /* The database connection */
danielk1977ef2cb632004-05-29 02:37:19 +000096 int iDb; /* The database to store the trigger in */
97 Token *pName; /* The unqualified db name */
drh3d5f74b2009-08-06 17:43:31 +000098 DbFixer sFix; /* State vector for the DB fixer */
99 int iTabDb; /* Index of the database holding pTab */
drhed6c8672003-01-12 18:02:16 +0000100
drh43617e92006-03-06 20:55:46 +0000101 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
102 assert( pName2!=0 );
drhaa78bec2008-12-09 03:55:14 +0000103 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
104 assert( op>0 && op<0xff );
danielk1977ef2cb632004-05-29 02:37:19 +0000105 if( isTemp ){
106 /* If TEMP was specified, then the trigger name may not be qualified. */
drh43617e92006-03-06 20:55:46 +0000107 if( pName2->n>0 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000108 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
109 goto trigger_cleanup;
110 }
111 iDb = 1;
112 pName = pName1;
113 }else{
114 /* Figure out the db that the the trigger will be created in */
115 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
116 if( iDb<0 ){
117 goto trigger_cleanup;
118 }
119 }
120
121 /* If the trigger name was unqualified, and the table is a temp table,
122 ** then set iDb to 1 to create the trigger in the temporary database.
123 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
124 ** exist, the error is caught by the block below.
drh9adf9ac2002-05-15 11:44:13 +0000125 */
drh17435752007-08-16 04:30:38 +0000126 if( !pTableName || db->mallocFailed ){
drh6f7adc82006-01-11 21:41:20 +0000127 goto trigger_cleanup;
128 }
danielk1977ef2cb632004-05-29 02:37:19 +0000129 pTab = sqlite3SrcListLookup(pParse, pTableName);
drh622d2882010-02-15 16:54:55 +0000130 if( db->init.busy==0 && pName2->n==0 && pTab
131 && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977ef2cb632004-05-29 02:37:19 +0000132 iDb = 1;
133 }
134
135 /* Ensure the table name matches database name and that the table exists */
drh17435752007-08-16 04:30:38 +0000136 if( db->mallocFailed ) goto trigger_cleanup;
drhd24cc422003-03-27 12:51:24 +0000137 assert( pTableName->nSrc==1 );
danielk1977ef2cb632004-05-29 02:37:19 +0000138 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) &&
139 sqlite3FixSrcList(&sFix, pTableName) ){
drh4312db52003-06-03 01:47:11 +0000140 goto trigger_cleanup;
drhf26e09c2003-05-31 16:21:12 +0000141 }
danielk1977ef2cb632004-05-29 02:37:19 +0000142 pTab = sqlite3SrcListLookup(pParse, pTableName);
143 if( !pTab ){
144 /* The table does not exist. */
drh3d5f74b2009-08-06 17:43:31 +0000145 if( db->init.iDb==1 ){
146 /* Ticket #3810.
147 ** Normally, whenever a table is dropped, all associated triggers are
148 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
149 ** and the table is dropped by a different database connection, the
150 ** trigger is not visible to the database connection that does the
151 ** drop so the trigger cannot be dropped. This results in an
152 ** "orphaned trigger" - a trigger whose associated table is missing.
153 */
154 db->init.orphanTrigger = 1;
155 }
drhd24cc422003-03-27 12:51:24 +0000156 goto trigger_cleanup;
157 }
drh4cbdda92006-06-14 19:00:20 +0000158 if( IsVirtual(pTab) ){
159 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
160 goto trigger_cleanup;
161 }
drhd24cc422003-03-27 12:51:24 +0000162
danielk1977d8123362004-06-12 09:25:12 +0000163 /* Check that the trigger name is not reserved and that no trigger of the
164 ** specified name exists */
drh17435752007-08-16 04:30:38 +0000165 zName = sqlite3NameFromToken(db, pName);
danielk1977d8123362004-06-12 09:25:12 +0000166 if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
167 goto trigger_cleanup;
168 }
drh21206082011-04-04 18:22:02 +0000169 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drhaa78bec2008-12-09 03:55:14 +0000170 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
drhea678832008-12-10 19:26:22 +0000171 zName, sqlite3Strlen30(zName)) ){
drhfdd48a72006-09-11 23:45:48 +0000172 if( !noErr ){
173 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
dan7687c832011-04-09 15:39:02 +0000174 }else{
175 assert( !db->init.busy );
176 sqlite3CodeVerifySchema(pParse, iDb);
drhfdd48a72006-09-11 23:45:48 +0000177 }
drhe5f9c642003-01-13 23:27:31 +0000178 goto trigger_cleanup;
danielk1977c3f9bad2002-05-15 08:30:12 +0000179 }
danielk1977ef2cb632004-05-29 02:37:19 +0000180
181 /* Do not create a trigger on a system table */
drhdca76842004-12-07 14:06:13 +0000182 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk19774adee202004-05-08 08:23:19 +0000183 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
drhd24cc422003-03-27 12:51:24 +0000184 pParse->nErr++;
185 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40 +0000186 }
danielk1977ef2cb632004-05-29 02:37:19 +0000187
188 /* INSTEAD of triggers are only for views and views only support INSTEAD
189 ** of triggers.
190 */
191 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000192 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drhda93d232003-03-31 02:12:46 +0000193 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000194 goto trigger_cleanup;
195 }
danielk1977ef2cb632004-05-29 02:37:19 +0000196 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000197 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drhda93d232003-03-31 02:12:46 +0000198 " trigger on table: %S", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000199 goto trigger_cleanup;
200 }
danielk1977da184232006-01-05 11:34:32 +0000201 iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977ef2cb632004-05-29 02:37:19 +0000202
drhd24cc422003-03-27 12:51:24 +0000203#ifndef SQLITE_OMIT_AUTHORIZATION
204 {
205 int code = SQLITE_CREATE_TRIGGER;
danielk1977da184232006-01-05 11:34:32 +0000206 const char *zDb = db->aDb[iTabDb].zName;
drhe22a3342003-04-22 20:30:37 +0000207 const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
danielk1977da184232006-01-05 11:34:32 +0000208 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19 +0000209 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24 +0000210 goto trigger_cleanup;
211 }
danielk1977da184232006-01-05 11:34:32 +0000212 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24 +0000213 goto trigger_cleanup;
214 }
215 }
216#endif
danielk1977d702fcc2002-05-26 23:24:40 +0000217
danielk1977ef2cb632004-05-29 02:37:19 +0000218 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04 +0000219 ** cannot appear on views. So we might as well translate every
220 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
221 ** elsewhere.
222 */
danielk1977d702fcc2002-05-26 23:24:40 +0000223 if (tr_tm == TK_INSTEAD){
224 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12 +0000225 }
226
227 /* Build the Trigger object */
drh17435752007-08-16 04:30:38 +0000228 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19 +0000229 if( pTrigger==0 ) goto trigger_cleanup;
dan165921a2009-08-28 18:53:45 +0000230 pTrigger->zName = zName;
drhe5f9c642003-01-13 23:27:31 +0000231 zName = 0;
drh17435752007-08-16 04:30:38 +0000232 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32 +0000233 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48 +0000234 pTrigger->pTabSchema = pTab->pSchema;
drhaa78bec2008-12-09 03:55:14 +0000235 pTrigger->op = (u8)op;
drhdca76842004-12-07 14:06:13 +0000236 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
danielk19776ab3a2e2009-02-19 14:39:25 +0000237 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
drh17435752007-08-16 04:30:38 +0000238 pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
drhf0f258b2003-04-21 18:48:45 +0000239 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19 +0000240 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45 +0000241
242trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000243 sqlite3DbFree(db, zName);
244 sqlite3SrcListDelete(db, pTableName);
245 sqlite3IdListDelete(db, pColumns);
246 sqlite3ExprDelete(db, pWhen);
danielk1977d5d56522005-03-16 12:15:20 +0000247 if( !pParse->pNewTrigger ){
drh633e6d52008-07-28 19:34:53 +0000248 sqlite3DeleteTrigger(db, pTrigger);
danielk1977d5d56522005-03-16 12:15:20 +0000249 }else{
250 assert( pParse->pNewTrigger==pTrigger );
251 }
drhf0f258b2003-04-21 18:48:45 +0000252}
253
254/*
255** This routine is called after all of the trigger actions have been parsed
256** in order to complete the process of building the trigger.
257*/
danielk19774adee202004-05-08 08:23:19 +0000258void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45 +0000259 Parse *pParse, /* Parser context */
260 TriggerStep *pStepList, /* The triggered program */
261 Token *pAll /* Token that describes the complete CREATE TRIGGER */
262){
drha8c62df2010-02-15 15:47:18 +0000263 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
264 char *zName; /* Name of trigger */
265 sqlite3 *db = pParse->db; /* The database */
266 DbFixer sFix; /* Fixer object */
267 int iDb; /* Database containing the trigger */
268 Token nameToken; /* Trigger name for error reporting */
drhf0f258b2003-04-21 18:48:45 +0000269
drhf0f258b2003-04-21 18:48:45 +0000270 pParse->pNewTrigger = 0;
drh9ab4c2e2009-05-09 00:18:38 +0000271 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
dan165921a2009-08-28 18:53:45 +0000272 zName = pTrig->zName;
danielk1977da184232006-01-05 11:34:32 +0000273 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32 +0000274 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53 +0000275 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32 +0000276 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53 +0000277 pStepList = pStepList->pNext;
278 }
dan165921a2009-08-28 18:53:45 +0000279 nameToken.z = pTrig->zName;
drhb7916a72009-05-27 10:31:29 +0000280 nameToken.n = sqlite3Strlen30(nameToken.z);
281 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken)
danielk1977bfb9e352005-01-24 13:03:32 +0000282 && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
drhf26e09c2003-05-31 16:21:12 +0000283 goto triggerfinish_cleanup;
284 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000285
drha8c62df2010-02-15 15:47:18 +0000286 /* if we are not initializing,
drh9adf9ac2002-05-15 11:44:13 +0000287 ** build the sqlite_master entry
288 */
drh1d85d932004-02-14 23:05:52 +0000289 if( !db->init.busy ){
drhc977f7f2002-05-21 11:38:11 +0000290 Vdbe *v;
drh2d401ab2008-01-10 23:50:11 +0000291 char *z;
danielk1977c3f9bad2002-05-15 08:30:12 +0000292
293 /* Make an entry in the sqlite_master table */
danielk19774adee202004-05-08 08:23:19 +0000294 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000295 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000296 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11 +0000297 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
298 sqlite3NestedParse(pParse,
299 "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
danielk19772f886d12009-02-28 10:47:41 +0000300 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
drh2d401ab2008-01-10 23:50:11 +0000301 pTrig->table, z);
drh633e6d52008-07-28 19:34:53 +0000302 sqlite3DbFree(db, z);
drh9cbf3422008-01-17 16:22:13 +0000303 sqlite3ChangeCookie(pParse, iDb);
drh5d9c9da2011-06-03 20:11:17 +0000304 sqlite3VdbeAddParseSchemaOp(v, iDb,
305 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
danielk1977c3f9bad2002-05-15 08:30:12 +0000306 }
307
drh956bc922004-07-24 17:38:29 +0000308 if( db->init.busy ){
danielk19772f886d12009-02-28 10:47:41 +0000309 Trigger *pLink = pTrig;
310 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
drh21206082011-04-04 18:22:02 +0000311 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk19772f886d12009-02-28 10:47:41 +0000312 pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);
313 if( pTrig ){
drhf3a65f72007-08-22 20:18:21 +0000314 db->mallocFailed = 1;
danielk19772f886d12009-02-28 10:47:41 +0000315 }else if( pLink->pSchema==pLink->pTabSchema ){
316 Table *pTab;
drha83ccca2009-04-28 13:01:09 +0000317 int n = sqlite3Strlen30(pLink->table);
danielk19772f886d12009-02-28 10:47:41 +0000318 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n);
319 assert( pTab!=0 );
320 pLink->pNext = pTab->pTrigger;
321 pTab->pTrigger = pLink;
danielk1977d5d56522005-03-16 12:15:20 +0000322 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000323 }
324
drhf0f258b2003-04-21 18:48:45 +0000325triggerfinish_cleanup:
drh633e6d52008-07-28 19:34:53 +0000326 sqlite3DeleteTrigger(db, pTrig);
danielk1977bfb9e352005-01-24 13:03:32 +0000327 assert( !pParse->pNewTrigger );
drh633e6d52008-07-28 19:34:53 +0000328 sqlite3DeleteTriggerStep(db, pStepList);
drh4b59ab52002-08-24 18:24:51 +0000329}
danielk1977c3f9bad2002-05-15 08:30:12 +0000330
drh4b59ab52002-08-24 18:24:51 +0000331/*
drhc977f7f2002-05-21 11:38:11 +0000332** Turn a SELECT statement (that the pSelect parameter points to) into
333** a trigger step. Return a pointer to a TriggerStep structure.
334**
335** The parser calls this routine when it finds a SELECT statement in
336** body of a TRIGGER.
337*/
drh17435752007-08-16 04:30:38 +0000338TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
339 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000340 if( pTriggerStep==0 ) {
drh633e6d52008-07-28 19:34:53 +0000341 sqlite3SelectDelete(db, pSelect);
danielk19772e588c72005-12-09 14:25:08 +0000342 return 0;
343 }
danielk1977633ed082002-05-17 00:05:58 +0000344 pTriggerStep->op = TK_SELECT;
345 pTriggerStep->pSelect = pSelect;
346 pTriggerStep->orconf = OE_Default;
drhb7916a72009-05-27 10:31:29 +0000347 return pTriggerStep;
348}
danielk1977c3f9bad2002-05-15 08:30:12 +0000349
drhb7916a72009-05-27 10:31:29 +0000350/*
351** Allocate space to hold a new trigger step. The allocated space
352** holds both the TriggerStep object and the TriggerStep.target.z string.
353**
354** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
355*/
356static TriggerStep *triggerStepAllocate(
357 sqlite3 *db, /* Database connection */
shane5eff7cf2009-08-10 03:57:58 +0000358 u8 op, /* Trigger opcode */
drhb7916a72009-05-27 10:31:29 +0000359 Token *pName /* The target name */
360){
361 TriggerStep *pTriggerStep;
362
363 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n);
364 if( pTriggerStep ){
365 char *z = (char*)&pTriggerStep[1];
366 memcpy(z, pName->z, pName->n);
367 pTriggerStep->target.z = z;
368 pTriggerStep->target.n = pName->n;
369 pTriggerStep->op = op;
370 }
danielk1977633ed082002-05-17 00:05:58 +0000371 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000372}
373
drhc977f7f2002-05-21 11:38:11 +0000374/*
375** Build a trigger step out of an INSERT statement. Return a pointer
376** to the new trigger step.
377**
378** The parser calls this routine when it sees an INSERT inside the
379** body of a trigger.
380*/
danielk19774adee202004-05-08 08:23:19 +0000381TriggerStep *sqlite3TriggerInsertStep(
drh17435752007-08-16 04:30:38 +0000382 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000383 Token *pTableName, /* Name of the table into which we insert */
384 IdList *pColumn, /* List of columns in pTableName to insert into */
385 ExprList *pEList, /* The VALUE clause: a list of values to be inserted */
386 Select *pSelect, /* A SELECT statement that supplies values */
shane5eff7cf2009-08-10 03:57:58 +0000387 u8 orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
danielk1977633ed082002-05-17 00:05:58 +0000388){
drhf3a65f72007-08-22 20:18:21 +0000389 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000390
danielk1977633ed082002-05-17 00:05:58 +0000391 assert(pEList == 0 || pSelect == 0);
drhf3a65f72007-08-22 20:18:21 +0000392 assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000393
drhb7916a72009-05-27 10:31:29 +0000394 pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
danielk1977d5d56522005-03-16 12:15:20 +0000395 if( pTriggerStep ){
drh33e619f2009-05-28 01:00:55 +0000396 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
danielk1977d5d56522005-03-16 12:15:20 +0000397 pTriggerStep->pIdList = pColumn;
drh33e619f2009-05-28 01:00:55 +0000398 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
danielk1977d5d56522005-03-16 12:15:20 +0000399 pTriggerStep->orconf = orconf;
danielk1977d5d56522005-03-16 12:15:20 +0000400 }else{
drh633e6d52008-07-28 19:34:53 +0000401 sqlite3IdListDelete(db, pColumn);
danielk1977d5d56522005-03-16 12:15:20 +0000402 }
drh33e619f2009-05-28 01:00:55 +0000403 sqlite3ExprListDelete(db, pEList);
404 sqlite3SelectDelete(db, pSelect);
danielk1977c3f9bad2002-05-15 08:30:12 +0000405
danielk1977633ed082002-05-17 00:05:58 +0000406 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000407}
408
drhc977f7f2002-05-21 11:38:11 +0000409/*
410** Construct a trigger step that implements an UPDATE statement and return
411** a pointer to that trigger step. The parser calls this routine when it
412** sees an UPDATE statement inside the body of a CREATE TRIGGER.
413*/
danielk19774adee202004-05-08 08:23:19 +0000414TriggerStep *sqlite3TriggerUpdateStep(
drh17435752007-08-16 04:30:38 +0000415 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000416 Token *pTableName, /* Name of the table to be updated */
417 ExprList *pEList, /* The SET clause: list of column and new values */
418 Expr *pWhere, /* The WHERE clause */
shane5eff7cf2009-08-10 03:57:58 +0000419 u8 orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
drhc977f7f2002-05-21 11:38:11 +0000420){
drhb7916a72009-05-27 10:31:29 +0000421 TriggerStep *pTriggerStep;
422
423 pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
drh33e619f2009-05-28 01:00:55 +0000424 if( pTriggerStep ){
425 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
426 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
427 pTriggerStep->orconf = orconf;
drh0e3a6f32007-03-30 20:40:34 +0000428 }
drh33e619f2009-05-28 01:00:55 +0000429 sqlite3ExprListDelete(db, pEList);
430 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000431 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000432}
433
drhc977f7f2002-05-21 11:38:11 +0000434/*
435** Construct a trigger step that implements a DELETE statement and return
436** a pointer to that trigger step. The parser calls this routine when it
437** sees a DELETE statement inside the body of a CREATE TRIGGER.
438*/
drh17435752007-08-16 04:30:38 +0000439TriggerStep *sqlite3TriggerDeleteStep(
440 sqlite3 *db, /* Database connection */
441 Token *pTableName, /* The table from which rows are deleted */
442 Expr *pWhere /* The WHERE clause */
443){
drhb7916a72009-05-27 10:31:29 +0000444 TriggerStep *pTriggerStep;
445
446 pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
drh33e619f2009-05-28 01:00:55 +0000447 if( pTriggerStep ){
448 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
449 pTriggerStep->orconf = OE_Default;
drhb63a53d2007-03-31 01:34:44 +0000450 }
drh33e619f2009-05-28 01:00:55 +0000451 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000452 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000453}
454
danielk1977633ed082002-05-17 00:05:58 +0000455/*
456** Recursively delete a Trigger structure
457*/
drh633e6d52008-07-28 19:34:53 +0000458void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
drhf0f258b2003-04-21 18:48:45 +0000459 if( pTrigger==0 ) return;
drh633e6d52008-07-28 19:34:53 +0000460 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
dan165921a2009-08-28 18:53:45 +0000461 sqlite3DbFree(db, pTrigger->zName);
drh633e6d52008-07-28 19:34:53 +0000462 sqlite3DbFree(db, pTrigger->table);
463 sqlite3ExprDelete(db, pTrigger->pWhen);
464 sqlite3IdListDelete(db, pTrigger->pColumns);
drh633e6d52008-07-28 19:34:53 +0000465 sqlite3DbFree(db, pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000466}
467
468/*
danielk19778a414492004-06-29 08:59:35 +0000469** This function is called to drop a trigger from the database schema.
470**
471** This may be called directly from the parser and therefore identifies
472** the trigger by name. The sqlite3DropTriggerPtr() routine does the
473** same job as this routine except it takes a pointer to the trigger
474** instead of the trigger name.
475**/
drhfdd48a72006-09-11 23:45:48 +0000476void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000477 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000478 int i;
479 const char *zDb;
480 const char *zName;
481 int nName;
drh9bb575f2004-09-06 17:24:11 +0000482 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000483
drh17435752007-08-16 04:30:38 +0000484 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000485 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000486 goto drop_trigger_cleanup;
487 }
danielk19778e227872004-06-07 07:52:17 +0000488
drhd24cc422003-03-27 12:51:24 +0000489 assert( pName->nSrc==1 );
490 zDb = pName->a[0].zDatabase;
491 zName = pName->a[0].zName;
drhea678832008-12-10 19:26:22 +0000492 nName = sqlite3Strlen30(zName);
drh21206082011-04-04 18:22:02 +0000493 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
danielk197753c0f742005-03-29 03:10:59 +0000494 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000495 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000496 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
drh21206082011-04-04 18:22:02 +0000497 assert( sqlite3SchemaMutexHeld(db, j, 0) );
drhe4df0e72006-03-29 00:24:06 +0000498 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
drhd24cc422003-03-27 12:51:24 +0000499 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000500 }
drhd24cc422003-03-27 12:51:24 +0000501 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000502 if( !noErr ){
503 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
dan57966752011-04-09 17:32:58 +0000504 }else{
505 sqlite3CodeVerifyNamedSchema(pParse, zDb);
drhfdd48a72006-09-11 23:45:48 +0000506 }
dan1db95102010-06-28 10:15:19 +0000507 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +0000508 goto drop_trigger_cleanup;
509 }
drh74161702006-02-24 02:53:49 +0000510 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000511
512drop_trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000513 sqlite3SrcListDelete(db, pName);
drh79a519c2003-05-17 19:04:03 +0000514}
515
516/*
drh956bc922004-07-24 17:38:29 +0000517** Return a pointer to the Table structure for the table that a trigger
518** is set on.
519*/
drh74161702006-02-24 02:53:49 +0000520static Table *tableOfTrigger(Trigger *pTrigger){
drha83ccca2009-04-28 13:01:09 +0000521 int n = sqlite3Strlen30(pTrigger->table);
danielk1977aaf22682006-01-06 15:03:48 +0000522 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
drh956bc922004-07-24 17:38:29 +0000523}
524
525
526/*
drh74161702006-02-24 02:53:49 +0000527** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000528*/
drh74161702006-02-24 02:53:49 +0000529void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000530 Table *pTable;
531 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000532 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000533 int iDb;
drh79a519c2003-05-17 19:04:03 +0000534
danielk1977da184232006-01-05 11:34:32 +0000535 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000536 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000537 pTable = tableOfTrigger(pTrigger);
drh43617e92006-03-06 20:55:46 +0000538 assert( pTable );
danielk1977da184232006-01-05 11:34:32 +0000539 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000540#ifndef SQLITE_OMIT_AUTHORIZATION
541 {
542 int code = SQLITE_DROP_TRIGGER;
drh956bc922004-07-24 17:38:29 +0000543 const char *zDb = db->aDb[iDb].zName;
544 const char *zTab = SCHEMA_TABLE(iDb);
545 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
dan2bd93512009-08-31 08:22:46 +0000546 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
danielk19774adee202004-05-08 08:23:19 +0000547 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000548 return;
drhe5f9c642003-01-13 23:27:31 +0000549 }
drhed6c8672003-01-12 18:02:16 +0000550 }
drhe5f9c642003-01-13 23:27:31 +0000551#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000552
drhf0f258b2003-04-21 18:48:45 +0000553 /* Generate code to destroy the database record of the trigger.
554 */
drh43617e92006-03-06 20:55:46 +0000555 assert( pTable!=0 );
556 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drhf0f258b2003-04-21 18:48:45 +0000557 int base;
drh57196282004-10-06 15:41:16 +0000558 static const VdbeOpList dropTrigger[] = {
drh9b1b01b2003-08-16 12:37:51 +0000559 { OP_Rewind, 0, ADDR(9), 0},
drh1db639c2008-01-17 02:36:28 +0000560 { OP_String8, 0, 1, 0}, /* 1 */
561 { OP_Column, 0, 1, 2},
562 { OP_Ne, 2, ADDR(8), 1},
563 { OP_String8, 0, 1, 0}, /* 4: "trigger" */
564 { OP_Column, 0, 0, 2},
565 { OP_Ne, 2, ADDR(8), 1},
drhf0f258b2003-04-21 18:48:45 +0000566 { OP_Delete, 0, 0, 0},
drh9b1b01b2003-08-16 12:37:51 +0000567 { OP_Next, 0, ADDR(1), 0}, /* 8 */
drhf0f258b2003-04-21 18:48:45 +0000568 };
569
drh956bc922004-07-24 17:38:29 +0000570 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk1977c00da102006-01-07 13:21:04 +0000571 sqlite3OpenMasterTable(pParse, iDb);
danielk19774adee202004-05-08 08:23:19 +0000572 base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger);
drh8d129422011-04-05 12:25:19 +0000573 sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, P4_TRANSIENT);
drh24003452008-01-03 01:28:59 +0000574 sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
drh9cbf3422008-01-17 16:22:13 +0000575 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +0000576 sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
dan165921a2009-08-28 18:53:45 +0000577 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
danielk19776ab3a2e2009-02-19 14:39:25 +0000578 if( pParse->nMem<3 ){
579 pParse->nMem = 3;
580 }
drhf0f258b2003-04-21 18:48:45 +0000581 }
drh956bc922004-07-24 17:38:29 +0000582}
drhf0f258b2003-04-21 18:48:45 +0000583
drh956bc922004-07-24 17:38:29 +0000584/*
585** Remove a trigger from the hash tables of the sqlite* pointer.
586*/
587void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
588 Trigger *pTrigger;
drh21206082011-04-04 18:22:02 +0000589 Hash *pHash;
590
591 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
592 pHash = &(db->aDb[iDb].pSchema->trigHash);
danielk19772f886d12009-02-28 10:47:41 +0000593 pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0);
drh9ab4c2e2009-05-09 00:18:38 +0000594 if( ALWAYS(pTrigger) ){
danielk19772f886d12009-02-28 10:47:41 +0000595 if( pTrigger->pSchema==pTrigger->pTabSchema ){
596 Table *pTab = tableOfTrigger(pTrigger);
597 Trigger **pp;
598 for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
599 *pp = (*pp)->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +0000600 }
drh633e6d52008-07-28 19:34:53 +0000601 sqlite3DeleteTrigger(db, pTrigger);
drh956bc922004-07-24 17:38:29 +0000602 db->flags |= SQLITE_InternChanges;
danielk1977c3f9bad2002-05-15 08:30:12 +0000603 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000604}
605
drhc977f7f2002-05-21 11:38:11 +0000606/*
607** pEList is the SET clause of an UPDATE statement. Each entry
608** in pEList is of the format <id>=<expr>. If any of the entries
609** in pEList have an <id> which matches an identifier in pIdList,
610** then return TRUE. If pIdList==NULL, then it is considered a
611** wildcard that matches anything. Likewise if pEList==NULL then
612** it matches anything so always return true. Return false only
613** if there is no match.
614*/
drh9ab4c2e2009-05-09 00:18:38 +0000615static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000616 int e;
drh9ab4c2e2009-05-09 00:18:38 +0000617 if( pIdList==0 || NEVER(pEList==0) ) return 1;
drhad2d8302002-05-24 20:31:36 +0000618 for(e=0; e<pEList->nExpr; e++){
danielk19774adee202004-05-08 08:23:19 +0000619 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000620 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000621 return 0;
622}
623
danielk1977c3f9bad2002-05-15 08:30:12 +0000624/*
danielk19772f886d12009-02-28 10:47:41 +0000625** Return a list of all triggers on table pTab if there exists at least
626** one trigger that must be fired when an operation of type 'op' is
627** performed on the table, and, if that operation is an UPDATE, if at
628** least one of the columns in pChanges is being modified.
drhdca76842004-12-07 14:06:13 +0000629*/
danielk19772f886d12009-02-28 10:47:41 +0000630Trigger *sqlite3TriggersExist(
631 Parse *pParse, /* Parse context */
drhdca76842004-12-07 14:06:13 +0000632 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000633 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
danielk19772f886d12009-02-28 10:47:41 +0000634 ExprList *pChanges, /* Columns that change in an UPDATE statement */
635 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
drhc977f7f2002-05-21 11:38:11 +0000636){
drhdca76842004-12-07 14:06:13 +0000637 int mask = 0;
drhe83cafd2011-03-21 17:15:58 +0000638 Trigger *pList = 0;
danielk19772f886d12009-02-28 10:47:41 +0000639 Trigger *p;
drhe83cafd2011-03-21 17:15:58 +0000640
641 if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
642 pList = sqlite3TriggerList(pParse, pTab);
643 }
danielk19772f886d12009-02-28 10:47:41 +0000644 assert( pList==0 || IsVirtual(pTab)==0 );
645 for(p=pList; p; p=p->pNext){
drh9ab4c2e2009-05-09 00:18:38 +0000646 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
danielk19772f886d12009-02-28 10:47:41 +0000647 mask |= p->tr_tm;
danielk1977c3f9bad2002-05-15 08:30:12 +0000648 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000649 }
danielk19772f886d12009-02-28 10:47:41 +0000650 if( pMask ){
651 *pMask = mask;
652 }
653 return (mask ? pList : 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000654}
655
drhc977f7f2002-05-21 11:38:11 +0000656/*
drhf26e09c2003-05-31 16:21:12 +0000657** Convert the pStep->target token into a SrcList and return a pointer
658** to that SrcList.
659**
660** This routine adds a specific database name, if needed, to the target when
661** forming the SrcList. This prevents a trigger in one database from
662** referring to a target in another database. An exception is when the
663** trigger is in TEMP in which case it can refer to any other database it
664** wants.
665*/
666static SrcList *targetSrcList(
667 Parse *pParse, /* The parsing context */
668 TriggerStep *pStep /* The trigger containing the target token */
669){
drhf26e09c2003-05-31 16:21:12 +0000670 int iDb; /* Index of the database to use */
671 SrcList *pSrc; /* SrcList to be returned */
672
drhb7916a72009-05-27 10:31:29 +0000673 pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
674 if( pSrc ){
675 assert( pSrc->nSrc>0 );
676 assert( pSrc->a!=0 );
677 iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
678 if( iDb==0 || iDb>=2 ){
679 sqlite3 *db = pParse->db;
680 assert( iDb<pParse->db->nDb );
681 pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
682 }
drhf26e09c2003-05-31 16:21:12 +0000683 }
684 return pSrc;
685}
686
687/*
dan65a7cd12009-09-01 12:16:01 +0000688** Generate VDBE code for the statements inside the body of a single
689** trigger.
drhc977f7f2002-05-21 11:38:11 +0000690*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000691static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000692 Parse *pParse, /* The parser context */
693 TriggerStep *pStepList, /* List of statements inside the trigger body */
dan65a7cd12009-09-01 12:16:01 +0000694 int orconf /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000695){
dan65a7cd12009-09-01 12:16:01 +0000696 TriggerStep *pStep;
drh344737f2004-09-19 00:50:20 +0000697 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000698 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000699
dan65a7cd12009-09-01 12:16:01 +0000700 assert( pParse->pTriggerTab && pParse->pToplevel );
701 assert( pStepList );
drh344737f2004-09-19 00:50:20 +0000702 assert( v!=0 );
dan65a7cd12009-09-01 12:16:01 +0000703 for(pStep=pStepList; pStep; pStep=pStep->pNext){
dan165921a2009-08-28 18:53:45 +0000704 /* Figure out the ON CONFLICT policy that will be used for this step
705 ** of the trigger program. If the statement that caused this trigger
706 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
707 ** the ON CONFLICT policy that was specified as part of the trigger
708 ** step statement. Example:
709 **
710 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
711 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
712 ** END;
713 **
714 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
715 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
716 */
shanecea72b22009-09-07 04:38:36 +0000717 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
dan165921a2009-08-28 18:53:45 +0000718
719 switch( pStep->op ){
danielk1977633ed082002-05-17 00:05:58 +0000720 case TK_UPDATE: {
dan165921a2009-08-28 18:53:45 +0000721 sqlite3Update(pParse,
722 targetSrcList(pParse, pStep),
723 sqlite3ExprListDup(db, pStep->pExprList, 0),
724 sqlite3ExprDup(db, pStep->pWhere, 0),
dan65a7cd12009-09-01 12:16:01 +0000725 pParse->eOrconf
dan165921a2009-08-28 18:53:45 +0000726 );
danielk1977633ed082002-05-17 00:05:58 +0000727 break;
728 }
729 case TK_INSERT: {
dan165921a2009-08-28 18:53:45 +0000730 sqlite3Insert(pParse,
731 targetSrcList(pParse, pStep),
732 sqlite3ExprListDup(db, pStep->pExprList, 0),
733 sqlite3SelectDup(db, pStep->pSelect, 0),
734 sqlite3IdListDup(db, pStep->pIdList),
dan65a7cd12009-09-01 12:16:01 +0000735 pParse->eOrconf
dan165921a2009-08-28 18:53:45 +0000736 );
danielk1977633ed082002-05-17 00:05:58 +0000737 break;
738 }
739 case TK_DELETE: {
dan165921a2009-08-28 18:53:45 +0000740 sqlite3DeleteFrom(pParse,
741 targetSrcList(pParse, pStep),
742 sqlite3ExprDup(db, pStep->pWhere, 0)
743 );
danielk1977633ed082002-05-17 00:05:58 +0000744 break;
745 }
dan165921a2009-08-28 18:53:45 +0000746 default: assert( pStep->op==TK_SELECT ); {
747 SelectDest sDest;
748 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
749 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
750 sqlite3Select(pParse, pSelect, &sDest);
751 sqlite3SelectDelete(db, pSelect);
drh9ab4c2e2009-05-09 00:18:38 +0000752 break;
753 }
danielk1977633ed082002-05-17 00:05:58 +0000754 }
dan76d462e2009-08-30 11:42:51 +0000755 if( pStep->op!=TK_SELECT ){
drhb7f1d9a2009-09-08 02:27:58 +0000756 sqlite3VdbeAddOp0(v, OP_ResetCount);
dan76d462e2009-08-30 11:42:51 +0000757 }
danielk1977633ed082002-05-17 00:05:58 +0000758 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000759
danielk1977633ed082002-05-17 00:05:58 +0000760 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000761}
762
dan165921a2009-08-28 18:53:45 +0000763#ifdef SQLITE_DEBUG
764/*
765** This function is used to add VdbeComment() annotations to a VDBE
766** program. It is not used in production code, only for debugging.
767*/
768static const char *onErrorText(int onError){
769 switch( onError ){
770 case OE_Abort: return "abort";
771 case OE_Rollback: return "rollback";
772 case OE_Fail: return "fail";
773 case OE_Replace: return "replace";
774 case OE_Ignore: return "ignore";
775 case OE_Default: return "default";
776 }
777 return "n/a";
778}
779#endif
780
781/*
782** Parse context structure pFrom has just been used to create a sub-vdbe
783** (trigger program). If an error has occurred, transfer error information
784** from pFrom to pTo.
785*/
786static void transferParseError(Parse *pTo, Parse *pFrom){
787 assert( pFrom->zErrMsg==0 || pFrom->nErr );
788 assert( pTo->zErrMsg==0 || pTo->nErr );
789 if( pTo->nErr==0 ){
790 pTo->zErrMsg = pFrom->zErrMsg;
791 pTo->nErr = pFrom->nErr;
792 }else{
793 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
794 }
795}
796
dan65a7cd12009-09-01 12:16:01 +0000797/*
798** Create and populate a new TriggerPrg object with a sub-program
799** implementing trigger pTrigger with ON CONFLICT policy orconf.
800*/
dan2832ad42009-08-31 15:27:27 +0000801static TriggerPrg *codeRowTrigger(
dan165921a2009-08-28 18:53:45 +0000802 Parse *pParse, /* Current parse context */
803 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +0000804 Table *pTab, /* The table pTrigger is attached to */
805 int orconf /* ON CONFLICT policy to code trigger program with */
dan165921a2009-08-28 18:53:45 +0000806){
dan65a7cd12009-09-01 12:16:01 +0000807 Parse *pTop = sqlite3ParseToplevel(pParse);
808 sqlite3 *db = pParse->db; /* Database handle */
809 TriggerPrg *pPrg; /* Value to return */
dan165921a2009-08-28 18:53:45 +0000810 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
811 Vdbe *v; /* Temporary VM */
dan165921a2009-08-28 18:53:45 +0000812 NameContext sNC; /* Name context for sub-vdbe */
813 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
814 Parse *pSubParse; /* Parse context for sub-vdbe */
815 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
816
dan1da40a32009-09-19 17:00:31 +0000817 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dand19c9332010-07-26 12:05:17 +0000818 assert( pTop->pVdbe );
dan65a7cd12009-09-01 12:16:01 +0000819
820 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
821 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
822 ** list of the top-level Parse object sooner rather than later. */
dan2832ad42009-08-31 15:27:27 +0000823 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
824 if( !pPrg ) return 0;
dan65a7cd12009-09-01 12:16:01 +0000825 pPrg->pNext = pTop->pTriggerPrg;
826 pTop->pTriggerPrg = pPrg;
dan2832ad42009-08-31 15:27:27 +0000827 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
dan165921a2009-08-28 18:53:45 +0000828 if( !pProgram ) return 0;
dand19c9332010-07-26 12:05:17 +0000829 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
dan2832ad42009-08-31 15:27:27 +0000830 pPrg->pTrigger = pTrigger;
831 pPrg->orconf = orconf;
danbb5f1682009-11-27 12:12:34 +0000832 pPrg->aColmask[0] = 0xffffffff;
833 pPrg->aColmask[1] = 0xffffffff;
dan165921a2009-08-28 18:53:45 +0000834
dan65a7cd12009-09-01 12:16:01 +0000835 /* Allocate and populate a new Parse context to use for coding the
836 ** trigger sub-program. */
837 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
838 if( !pSubParse ) return 0;
dan165921a2009-08-28 18:53:45 +0000839 memset(&sNC, 0, sizeof(sNC));
840 sNC.pParse = pSubParse;
841 pSubParse->db = db;
842 pSubParse->pTriggerTab = pTab;
dan65a7cd12009-09-01 12:16:01 +0000843 pSubParse->pToplevel = pTop;
dan2bd93512009-08-31 08:22:46 +0000844 pSubParse->zAuthContext = pTrigger->zName;
dan65a7cd12009-09-01 12:16:01 +0000845 pSubParse->eTriggerOp = pTrigger->op;
drh8b307fb2010-04-06 15:57:05 +0000846 pSubParse->nQueryLoop = pParse->nQueryLoop;
dan165921a2009-08-28 18:53:45 +0000847
848 v = sqlite3GetVdbe(pSubParse);
849 if( v ){
dan76d462e2009-08-30 11:42:51 +0000850 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
851 pTrigger->zName, onErrorText(orconf),
dan165921a2009-08-28 18:53:45 +0000852 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
dan65a7cd12009-09-01 12:16:01 +0000853 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
854 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
855 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
dan76d462e2009-08-30 11:42:51 +0000856 pTab->zName
dan165921a2009-08-28 18:53:45 +0000857 ));
dan76d462e2009-08-30 11:42:51 +0000858#ifndef SQLITE_OMIT_TRACE
859 sqlite3VdbeChangeP4(v, -1,
860 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
861 );
862#endif
dan165921a2009-08-28 18:53:45 +0000863
dan65a7cd12009-09-01 12:16:01 +0000864 /* If one was specified, code the WHEN clause. If it evaluates to false
865 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
866 ** OP_Halt inserted at the end of the program. */
dan165921a2009-08-28 18:53:45 +0000867 if( pTrigger->pWhen ){
dan165921a2009-08-28 18:53:45 +0000868 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
dan523a0872009-08-31 05:23:32 +0000869 if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
870 && db->mallocFailed==0
871 ){
dan165921a2009-08-28 18:53:45 +0000872 iEndTrigger = sqlite3VdbeMakeLabel(v);
873 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
874 }
875 sqlite3ExprDelete(db, pWhen);
876 }
877
878 /* Code the trigger program into the sub-vdbe. */
dan76d462e2009-08-30 11:42:51 +0000879 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
dan65a7cd12009-09-01 12:16:01 +0000880
881 /* Insert an OP_Halt at the end of the sub-program. */
dan165921a2009-08-28 18:53:45 +0000882 if( iEndTrigger ){
883 sqlite3VdbeResolveLabel(v, iEndTrigger);
884 }
885 sqlite3VdbeAddOp0(v, OP_Halt);
dan76d462e2009-08-30 11:42:51 +0000886 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
887
dan165921a2009-08-28 18:53:45 +0000888 transferParseError(pParse, pSubParse);
dan523a0872009-08-31 05:23:32 +0000889 if( db->mallocFailed==0 ){
dan65a7cd12009-09-01 12:16:01 +0000890 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
dan523a0872009-08-31 05:23:32 +0000891 }
dan165921a2009-08-28 18:53:45 +0000892 pProgram->nMem = pSubParse->nMem;
893 pProgram->nCsr = pSubParse->nTab;
894 pProgram->token = (void *)pTrigger;
danbb5f1682009-11-27 12:12:34 +0000895 pPrg->aColmask[0] = pSubParse->oldmask;
896 pPrg->aColmask[1] = pSubParse->newmask;
dan165921a2009-08-28 18:53:45 +0000897 sqlite3VdbeDelete(v);
dan165921a2009-08-28 18:53:45 +0000898 }
dan65a7cd12009-09-01 12:16:01 +0000899
900 assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
901 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
dan165921a2009-08-28 18:53:45 +0000902 sqlite3StackFree(db, pSubParse);
903
dan2832ad42009-08-31 15:27:27 +0000904 return pPrg;
dan165921a2009-08-28 18:53:45 +0000905}
906
dan65a7cd12009-09-01 12:16:01 +0000907/*
908** Return a pointer to a TriggerPrg object containing the sub-program for
909** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
910** TriggerPrg object exists, a new object is allocated and populated before
911** being returned.
912*/
dan2832ad42009-08-31 15:27:27 +0000913static TriggerPrg *getRowTrigger(
dan65a7cd12009-09-01 12:16:01 +0000914 Parse *pParse, /* Current parse context */
dan165921a2009-08-28 18:53:45 +0000915 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +0000916 Table *pTab, /* The table trigger pTrigger is attached to */
917 int orconf /* ON CONFLICT algorithm. */
dan165921a2009-08-28 18:53:45 +0000918){
dan65a7cd12009-09-01 12:16:01 +0000919 Parse *pRoot = sqlite3ParseToplevel(pParse);
dan2832ad42009-08-31 15:27:27 +0000920 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +0000921
dan1da40a32009-09-19 17:00:31 +0000922 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dan165921a2009-08-28 18:53:45 +0000923
924 /* It may be that this trigger has already been coded (or is in the
925 ** process of being coded). If this is the case, then an entry with
dan2832ad42009-08-31 15:27:27 +0000926 ** a matching TriggerPrg.pTrigger field will be present somewhere
927 ** in the Parse.pTriggerPrg list. Search for such an entry. */
dan2832ad42009-08-31 15:27:27 +0000928 for(pPrg=pRoot->pTriggerPrg;
929 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
930 pPrg=pPrg->pNext
dan165921a2009-08-28 18:53:45 +0000931 );
932
dan65a7cd12009-09-01 12:16:01 +0000933 /* If an existing TriggerPrg could not be located, create a new one. */
dan2832ad42009-08-31 15:27:27 +0000934 if( !pPrg ){
dan65a7cd12009-09-01 12:16:01 +0000935 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
dan165921a2009-08-28 18:53:45 +0000936 }
937
dan2832ad42009-08-31 15:27:27 +0000938 return pPrg;
dan165921a2009-08-28 18:53:45 +0000939}
940
dan94d7f502009-09-24 09:05:49 +0000941/*
942** Generate code for the trigger program associated with trigger p on
943** table pTab. The reg, orconf and ignoreJump parameters passed to this
944** function are the same as those described in the header function for
945** sqlite3CodeRowTrigger()
946*/
dan1da40a32009-09-19 17:00:31 +0000947void sqlite3CodeRowTriggerDirect(
948 Parse *pParse, /* Parse context */
949 Trigger *p, /* Trigger to code */
950 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +0000951 int reg, /* Reg array containing OLD.* and NEW.* values */
dan1da40a32009-09-19 17:00:31 +0000952 int orconf, /* ON CONFLICT policy */
953 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
954){
955 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
956 TriggerPrg *pPrg;
957 pPrg = getRowTrigger(pParse, p, pTab, orconf);
958 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
959
960 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
961 ** is a pointer to the sub-vdbe containing the trigger program. */
962 if( pPrg ){
dand19c9332010-07-26 12:05:17 +0000963 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
964
dan94d7f502009-09-24 09:05:49 +0000965 sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem);
dan1da40a32009-09-19 17:00:31 +0000966 sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
967 VdbeComment(
968 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
969
970 /* Set the P5 operand of the OP_Program instruction to non-zero if
971 ** recursive invocation of this trigger program is disallowed. Recursive
972 ** invocation is disallowed if (a) the sub-program is really a trigger,
973 ** not a foreign key action, and (b) the flag to enable recursive triggers
974 ** is clear. */
dand19c9332010-07-26 12:05:17 +0000975 sqlite3VdbeChangeP5(v, (u8)bRecursive);
dan1da40a32009-09-19 17:00:31 +0000976 }
977}
978
danielk1977633ed082002-05-17 00:05:58 +0000979/*
dan94d7f502009-09-24 09:05:49 +0000980** This is called to code the required FOR EACH ROW triggers for an operation
981** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
982** is given by the op paramater. The tr_tm parameter determines whether the
983** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
984** parameter pChanges is passed the list of columns being modified.
danielk1977633ed082002-05-17 00:05:58 +0000985**
dan94d7f502009-09-24 09:05:49 +0000986** If there are no triggers that fire at the specified time for the specified
987** operation on pTab, this function is a no-op.
drhc977f7f2002-05-21 11:38:11 +0000988**
dan94d7f502009-09-24 09:05:49 +0000989** The reg argument is the address of the first in an array of registers
990** that contain the values substituted for the new.* and old.* references
991** in the trigger program. If N is the number of columns in table pTab
992** (a copy of pTab->nCol), then registers are populated as follows:
drhc977f7f2002-05-21 11:38:11 +0000993**
dan94d7f502009-09-24 09:05:49 +0000994** Register Contains
995** ------------------------------------------------------
996** reg+0 OLD.rowid
997** reg+1 OLD.* value of left-most column of pTab
998** ... ...
999** reg+N OLD.* value of right-most column of pTab
1000** reg+N+1 NEW.rowid
1001** reg+N+2 OLD.* value of left-most column of pTab
1002** ... ...
1003** reg+N+N+1 NEW.* value of right-most column of pTab
drhc977f7f2002-05-21 11:38:11 +00001004**
dan94d7f502009-09-24 09:05:49 +00001005** For ON DELETE triggers, the registers containing the NEW.* values will
1006** never be accessed by the trigger program, so they are not allocated or
1007** populated by the caller (there is no data to populate them with anyway).
1008** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
1009** are never accessed, and so are not allocated by the caller. So, for an
1010** ON INSERT trigger, the value passed to this function as parameter reg
1011** is not a readable register, although registers (reg+N) through
1012** (reg+N+N+1) are.
danielk1977633ed082002-05-17 00:05:58 +00001013**
dan94d7f502009-09-24 09:05:49 +00001014** Parameter orconf is the default conflict resolution algorithm for the
1015** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
1016** is the instruction that control should jump to if a trigger program
1017** raises an IGNORE exception.
danielk1977633ed082002-05-17 00:05:58 +00001018*/
dan165921a2009-08-28 18:53:45 +00001019void sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +00001020 Parse *pParse, /* Parse context */
danielk19772f886d12009-02-28 10:47:41 +00001021 Trigger *pTrigger, /* List of triggers on table pTab */
danielk1977633ed082002-05-17 00:05:58 +00001022 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
1023 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +00001024 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +00001025 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001026 int reg, /* The first in an array of registers (see above) */
danielk19776f349032002-06-11 02:25:40 +00001027 int orconf, /* ON CONFLICT policy */
dan165921a2009-08-28 18:53:45 +00001028 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
drhc977f7f2002-05-21 11:38:11 +00001029){
dan94d7f502009-09-24 09:05:49 +00001030 Trigger *p; /* Used to iterate through pTrigger list */
danielk19778f2c54e2008-01-01 19:02:09 +00001031
dan94d7f502009-09-24 09:05:49 +00001032 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
1033 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
1034 assert( (op==TK_UPDATE)==(pChanges!=0) );
danielk1977c3f9bad2002-05-15 08:30:12 +00001035
danielk19772f886d12009-02-28 10:47:41 +00001036 for(p=pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +00001037
drh9ab4c2e2009-05-09 00:18:38 +00001038 /* Sanity checking: The schema for the trigger and for the table are
1039 ** always defined. The trigger must be in the same schema as the table
1040 ** or else it must be a TEMP trigger. */
1041 assert( p->pSchema!=0 );
1042 assert( p->pTabSchema!=0 );
dan165921a2009-08-28 18:53:45 +00001043 assert( p->pSchema==p->pTabSchema
1044 || p->pSchema==pParse->db->aDb[1].pSchema );
drh9ab4c2e2009-05-09 00:18:38 +00001045
danielk1977eecfb3e2006-01-10 12:31:39 +00001046 /* Determine whether we should code this trigger */
dan165921a2009-08-28 18:53:45 +00001047 if( p->op==op
1048 && p->tr_tm==tr_tm
dan94d7f502009-09-24 09:05:49 +00001049 && checkColumnOverlap(p->pColumns, pChanges)
danielk1977eecfb3e2006-01-10 12:31:39 +00001050 ){
dan94d7f502009-09-24 09:05:49 +00001051 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
danielk1977c3f9bad2002-05-15 08:30:12 +00001052 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001053 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001054}
dan165921a2009-08-28 18:53:45 +00001055
dan2832ad42009-08-31 15:27:27 +00001056/*
danbb5f1682009-11-27 12:12:34 +00001057** Triggers may access values stored in the old.* or new.* pseudo-table.
1058** This function returns a 32-bit bitmask indicating which columns of the
1059** old.* or new.* tables actually are used by triggers. This information
1060** may be used by the caller, for example, to avoid having to load the entire
1061** old.* record into memory when executing an UPDATE or DELETE command.
dan2832ad42009-08-31 15:27:27 +00001062**
1063** Bit 0 of the returned mask is set if the left-most column of the
danbb5f1682009-11-27 12:12:34 +00001064** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
dan2832ad42009-08-31 15:27:27 +00001065** the second leftmost column value is required, and so on. If there
1066** are more than 32 columns in the table, and at least one of the columns
1067** with an index greater than 32 may be accessed, 0xffffffff is returned.
1068**
danbb5f1682009-11-27 12:12:34 +00001069** It is not possible to determine if the old.rowid or new.rowid column is
1070** accessed by triggers. The caller must always assume that it is.
dan2832ad42009-08-31 15:27:27 +00001071**
danbb5f1682009-11-27 12:12:34 +00001072** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
1073** applies to the old.* table. If 1, the new.* table.
1074**
1075** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
1076** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
1077** included in the returned mask if the TRIGGER_BEFORE bit is set in the
1078** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
1079** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
dan2832ad42009-08-31 15:27:27 +00001080*/
danbb5f1682009-11-27 12:12:34 +00001081u32 sqlite3TriggerColmask(
dan165921a2009-08-28 18:53:45 +00001082 Parse *pParse, /* Parse context */
1083 Trigger *pTrigger, /* List of triggers on table pTab */
dan165921a2009-08-28 18:53:45 +00001084 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
danbb5f1682009-11-27 12:12:34 +00001085 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
1086 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
dan165921a2009-08-28 18:53:45 +00001087 Table *pTab, /* The table to code triggers from */
dan2832ad42009-08-31 15:27:27 +00001088 int orconf /* Default ON CONFLICT policy for trigger steps */
dan165921a2009-08-28 18:53:45 +00001089){
dan1da40a32009-09-19 17:00:31 +00001090 const int op = pChanges ? TK_UPDATE : TK_DELETE;
dan2832ad42009-08-31 15:27:27 +00001091 u32 mask = 0;
dan165921a2009-08-28 18:53:45 +00001092 Trigger *p;
dan165921a2009-08-28 18:53:45 +00001093
danbb5f1682009-11-27 12:12:34 +00001094 assert( isNew==1 || isNew==0 );
dan165921a2009-08-28 18:53:45 +00001095 for(p=pTrigger; p; p=p->pNext){
danbb5f1682009-11-27 12:12:34 +00001096 if( p->op==op && (tr_tm&p->tr_tm)
1097 && checkColumnOverlap(p->pColumns,pChanges)
1098 ){
dan2832ad42009-08-31 15:27:27 +00001099 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +00001100 pPrg = getRowTrigger(pParse, p, pTab, orconf);
dan2832ad42009-08-31 15:27:27 +00001101 if( pPrg ){
danbb5f1682009-11-27 12:12:34 +00001102 mask |= pPrg->aColmask[isNew];
dan165921a2009-08-28 18:53:45 +00001103 }
1104 }
1105 }
dan2832ad42009-08-31 15:27:27 +00001106
1107 return mask;
dan165921a2009-08-28 18:53:45 +00001108}
1109
drhb7f91642004-10-31 02:22:47 +00001110#endif /* !defined(SQLITE_OMIT_TRIGGER) */