blob: 1bc232727f1658271f4a33a07ba7d36025f08e1d [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{
mistachkind5578432012-08-25 10:01:29 +0000114 /* Figure out the db that the trigger will be created in */
danielk1977ef2cb632004-05-29 02:37:19 +0000115 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
116 if( iDb<0 ){
117 goto trigger_cleanup;
118 }
119 }
drh6fea83e2011-07-01 13:50:44 +0000120 if( !pTableName || db->mallocFailed ){
121 goto trigger_cleanup;
122 }
123
124 /* A long-standing parser bug is that this syntax was allowed:
125 **
126 ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
127 ** ^^^^^^^^
128 **
129 ** To maintain backwards compatibility, ignore the database
peter.d.reid60ec9142014-09-06 16:39:46 +0000130 ** name on pTableName if we are reparsing out of SQLITE_MASTER.
drh6fea83e2011-07-01 13:50:44 +0000131 */
132 if( db->init.busy && iDb!=1 ){
133 sqlite3DbFree(db, pTableName->a[0].zDatabase);
134 pTableName->a[0].zDatabase = 0;
135 }
danielk1977ef2cb632004-05-29 02:37:19 +0000136
137 /* If the trigger name was unqualified, and the table is a temp table,
138 ** then set iDb to 1 to create the trigger in the temporary database.
139 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
140 ** exist, the error is caught by the block below.
drh9adf9ac2002-05-15 11:44:13 +0000141 */
danielk1977ef2cb632004-05-29 02:37:19 +0000142 pTab = sqlite3SrcListLookup(pParse, pTableName);
drh622d2882010-02-15 16:54:55 +0000143 if( db->init.busy==0 && pName2->n==0 && pTab
144 && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977ef2cb632004-05-29 02:37:19 +0000145 iDb = 1;
146 }
147
148 /* Ensure the table name matches database name and that the table exists */
drh17435752007-08-16 04:30:38 +0000149 if( db->mallocFailed ) goto trigger_cleanup;
drhd24cc422003-03-27 12:51:24 +0000150 assert( pTableName->nSrc==1 );
drhd100f692013-10-03 15:39:44 +0000151 sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
152 if( sqlite3FixSrcList(&sFix, pTableName) ){
drh4312db52003-06-03 01:47:11 +0000153 goto trigger_cleanup;
drhf26e09c2003-05-31 16:21:12 +0000154 }
danielk1977ef2cb632004-05-29 02:37:19 +0000155 pTab = sqlite3SrcListLookup(pParse, pTableName);
156 if( !pTab ){
157 /* The table does not exist. */
drh3d5f74b2009-08-06 17:43:31 +0000158 if( db->init.iDb==1 ){
159 /* Ticket #3810.
160 ** Normally, whenever a table is dropped, all associated triggers are
161 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
162 ** and the table is dropped by a different database connection, the
163 ** trigger is not visible to the database connection that does the
164 ** drop so the trigger cannot be dropped. This results in an
165 ** "orphaned trigger" - a trigger whose associated table is missing.
166 */
167 db->init.orphanTrigger = 1;
168 }
drhd24cc422003-03-27 12:51:24 +0000169 goto trigger_cleanup;
170 }
drh4cbdda92006-06-14 19:00:20 +0000171 if( IsVirtual(pTab) ){
172 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
173 goto trigger_cleanup;
174 }
drhd24cc422003-03-27 12:51:24 +0000175
danielk1977d8123362004-06-12 09:25:12 +0000176 /* Check that the trigger name is not reserved and that no trigger of the
177 ** specified name exists */
drh17435752007-08-16 04:30:38 +0000178 zName = sqlite3NameFromToken(db, pName);
danielk1977d8123362004-06-12 09:25:12 +0000179 if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
180 goto trigger_cleanup;
181 }
drh21206082011-04-04 18:22:02 +0000182 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drhacbcb7e2014-08-21 20:26:37 +0000183 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
drhfdd48a72006-09-11 23:45:48 +0000184 if( !noErr ){
185 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
dan7687c832011-04-09 15:39:02 +0000186 }else{
187 assert( !db->init.busy );
188 sqlite3CodeVerifySchema(pParse, iDb);
drhfdd48a72006-09-11 23:45:48 +0000189 }
drhe5f9c642003-01-13 23:27:31 +0000190 goto trigger_cleanup;
danielk1977c3f9bad2002-05-15 08:30:12 +0000191 }
danielk1977ef2cb632004-05-29 02:37:19 +0000192
193 /* Do not create a trigger on a system table */
drhdca76842004-12-07 14:06:13 +0000194 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk19774adee202004-05-08 08:23:19 +0000195 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
drhd24cc422003-03-27 12:51:24 +0000196 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40 +0000197 }
danielk1977ef2cb632004-05-29 02:37:19 +0000198
199 /* INSTEAD of triggers are only for views and views only support INSTEAD
200 ** of triggers.
201 */
202 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000203 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drhda93d232003-03-31 02:12:46 +0000204 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000205 goto trigger_cleanup;
206 }
danielk1977ef2cb632004-05-29 02:37:19 +0000207 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000208 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drhda93d232003-03-31 02:12:46 +0000209 " trigger on table: %S", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000210 goto trigger_cleanup;
211 }
danielk1977da184232006-01-05 11:34:32 +0000212 iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977ef2cb632004-05-29 02:37:19 +0000213
drhd24cc422003-03-27 12:51:24 +0000214#ifndef SQLITE_OMIT_AUTHORIZATION
215 {
216 int code = SQLITE_CREATE_TRIGGER;
danielk1977da184232006-01-05 11:34:32 +0000217 const char *zDb = db->aDb[iTabDb].zName;
drhe22a3342003-04-22 20:30:37 +0000218 const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
danielk1977da184232006-01-05 11:34:32 +0000219 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19 +0000220 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24 +0000221 goto trigger_cleanup;
222 }
danielk1977da184232006-01-05 11:34:32 +0000223 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24 +0000224 goto trigger_cleanup;
225 }
226 }
227#endif
danielk1977d702fcc2002-05-26 23:24:40 +0000228
danielk1977ef2cb632004-05-29 02:37:19 +0000229 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04 +0000230 ** cannot appear on views. So we might as well translate every
231 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
232 ** elsewhere.
233 */
danielk1977d702fcc2002-05-26 23:24:40 +0000234 if (tr_tm == TK_INSTEAD){
235 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12 +0000236 }
237
238 /* Build the Trigger object */
drh17435752007-08-16 04:30:38 +0000239 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19 +0000240 if( pTrigger==0 ) goto trigger_cleanup;
dan165921a2009-08-28 18:53:45 +0000241 pTrigger->zName = zName;
drhe5f9c642003-01-13 23:27:31 +0000242 zName = 0;
drh17435752007-08-16 04:30:38 +0000243 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32 +0000244 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48 +0000245 pTrigger->pTabSchema = pTab->pSchema;
drhaa78bec2008-12-09 03:55:14 +0000246 pTrigger->op = (u8)op;
drhdca76842004-12-07 14:06:13 +0000247 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
danielk19776ab3a2e2009-02-19 14:39:25 +0000248 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
drh17435752007-08-16 04:30:38 +0000249 pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
drhf0f258b2003-04-21 18:48:45 +0000250 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19 +0000251 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45 +0000252
253trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000254 sqlite3DbFree(db, zName);
255 sqlite3SrcListDelete(db, pTableName);
256 sqlite3IdListDelete(db, pColumns);
257 sqlite3ExprDelete(db, pWhen);
danielk1977d5d56522005-03-16 12:15:20 +0000258 if( !pParse->pNewTrigger ){
drh633e6d52008-07-28 19:34:53 +0000259 sqlite3DeleteTrigger(db, pTrigger);
danielk1977d5d56522005-03-16 12:15:20 +0000260 }else{
261 assert( pParse->pNewTrigger==pTrigger );
262 }
drhf0f258b2003-04-21 18:48:45 +0000263}
264
265/*
266** This routine is called after all of the trigger actions have been parsed
267** in order to complete the process of building the trigger.
268*/
danielk19774adee202004-05-08 08:23:19 +0000269void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45 +0000270 Parse *pParse, /* Parser context */
271 TriggerStep *pStepList, /* The triggered program */
272 Token *pAll /* Token that describes the complete CREATE TRIGGER */
273){
drha8c62df2010-02-15 15:47:18 +0000274 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
275 char *zName; /* Name of trigger */
276 sqlite3 *db = pParse->db; /* The database */
277 DbFixer sFix; /* Fixer object */
278 int iDb; /* Database containing the trigger */
279 Token nameToken; /* Trigger name for error reporting */
drhf0f258b2003-04-21 18:48:45 +0000280
drhf0f258b2003-04-21 18:48:45 +0000281 pParse->pNewTrigger = 0;
drh9ab4c2e2009-05-09 00:18:38 +0000282 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
dan165921a2009-08-28 18:53:45 +0000283 zName = pTrig->zName;
danielk1977da184232006-01-05 11:34:32 +0000284 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32 +0000285 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53 +0000286 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32 +0000287 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53 +0000288 pStepList = pStepList->pNext;
289 }
dan165921a2009-08-28 18:53:45 +0000290 nameToken.z = pTrig->zName;
drhb7916a72009-05-27 10:31:29 +0000291 nameToken.n = sqlite3Strlen30(nameToken.z);
drhd100f692013-10-03 15:39:44 +0000292 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
293 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
dan46539d72013-10-03 12:29:38 +0000294 || sqlite3FixExpr(&sFix, pTrig->pWhen)
drhd100f692013-10-03 15:39:44 +0000295 ){
drhf26e09c2003-05-31 16:21:12 +0000296 goto triggerfinish_cleanup;
297 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000298
drha8c62df2010-02-15 15:47:18 +0000299 /* if we are not initializing,
drh9adf9ac2002-05-15 11:44:13 +0000300 ** build the sqlite_master entry
301 */
drh1d85d932004-02-14 23:05:52 +0000302 if( !db->init.busy ){
drhc977f7f2002-05-21 11:38:11 +0000303 Vdbe *v;
drh2d401ab2008-01-10 23:50:11 +0000304 char *z;
danielk1977c3f9bad2002-05-15 08:30:12 +0000305
306 /* Make an entry in the sqlite_master table */
danielk19774adee202004-05-08 08:23:19 +0000307 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000308 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000309 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11 +0000310 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
311 sqlite3NestedParse(pParse,
312 "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
danielk19772f886d12009-02-28 10:47:41 +0000313 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
drh2d401ab2008-01-10 23:50:11 +0000314 pTrig->table, z);
drh633e6d52008-07-28 19:34:53 +0000315 sqlite3DbFree(db, z);
drh9cbf3422008-01-17 16:22:13 +0000316 sqlite3ChangeCookie(pParse, iDb);
drh5d9c9da2011-06-03 20:11:17 +0000317 sqlite3VdbeAddParseSchemaOp(v, iDb,
318 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
danielk1977c3f9bad2002-05-15 08:30:12 +0000319 }
320
drh956bc922004-07-24 17:38:29 +0000321 if( db->init.busy ){
danielk19772f886d12009-02-28 10:47:41 +0000322 Trigger *pLink = pTrig;
323 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
drh21206082011-04-04 18:22:02 +0000324 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drhacbcb7e2014-08-21 20:26:37 +0000325 pTrig = sqlite3HashInsert(pHash, zName, pTrig);
danielk19772f886d12009-02-28 10:47:41 +0000326 if( pTrig ){
drhf3a65f72007-08-22 20:18:21 +0000327 db->mallocFailed = 1;
danielk19772f886d12009-02-28 10:47:41 +0000328 }else if( pLink->pSchema==pLink->pTabSchema ){
329 Table *pTab;
drhacbcb7e2014-08-21 20:26:37 +0000330 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
danielk19772f886d12009-02-28 10:47:41 +0000331 assert( pTab!=0 );
332 pLink->pNext = pTab->pTrigger;
333 pTab->pTrigger = pLink;
danielk1977d5d56522005-03-16 12:15:20 +0000334 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000335 }
336
drhf0f258b2003-04-21 18:48:45 +0000337triggerfinish_cleanup:
drh633e6d52008-07-28 19:34:53 +0000338 sqlite3DeleteTrigger(db, pTrig);
danielk1977bfb9e352005-01-24 13:03:32 +0000339 assert( !pParse->pNewTrigger );
drh633e6d52008-07-28 19:34:53 +0000340 sqlite3DeleteTriggerStep(db, pStepList);
drh4b59ab52002-08-24 18:24:51 +0000341}
danielk1977c3f9bad2002-05-15 08:30:12 +0000342
drh4b59ab52002-08-24 18:24:51 +0000343/*
drhc977f7f2002-05-21 11:38:11 +0000344** Turn a SELECT statement (that the pSelect parameter points to) into
345** a trigger step. Return a pointer to a TriggerStep structure.
346**
347** The parser calls this routine when it finds a SELECT statement in
348** body of a TRIGGER.
349*/
drh17435752007-08-16 04:30:38 +0000350TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
351 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000352 if( pTriggerStep==0 ) {
drh633e6d52008-07-28 19:34:53 +0000353 sqlite3SelectDelete(db, pSelect);
danielk19772e588c72005-12-09 14:25:08 +0000354 return 0;
355 }
danielk1977633ed082002-05-17 00:05:58 +0000356 pTriggerStep->op = TK_SELECT;
357 pTriggerStep->pSelect = pSelect;
358 pTriggerStep->orconf = OE_Default;
drhb7916a72009-05-27 10:31:29 +0000359 return pTriggerStep;
360}
danielk1977c3f9bad2002-05-15 08:30:12 +0000361
drhb7916a72009-05-27 10:31:29 +0000362/*
363** Allocate space to hold a new trigger step. The allocated space
364** holds both the TriggerStep object and the TriggerStep.target.z string.
365**
366** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
367*/
368static TriggerStep *triggerStepAllocate(
369 sqlite3 *db, /* Database connection */
shane5eff7cf2009-08-10 03:57:58 +0000370 u8 op, /* Trigger opcode */
drhb7916a72009-05-27 10:31:29 +0000371 Token *pName /* The target name */
372){
373 TriggerStep *pTriggerStep;
374
375 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n);
376 if( pTriggerStep ){
377 char *z = (char*)&pTriggerStep[1];
378 memcpy(z, pName->z, pName->n);
379 pTriggerStep->target.z = z;
380 pTriggerStep->target.n = pName->n;
381 pTriggerStep->op = op;
382 }
danielk1977633ed082002-05-17 00:05:58 +0000383 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000384}
385
drhc977f7f2002-05-21 11:38:11 +0000386/*
387** Build a trigger step out of an INSERT statement. Return a pointer
388** to the new trigger step.
389**
390** The parser calls this routine when it sees an INSERT inside the
391** body of a trigger.
392*/
danielk19774adee202004-05-08 08:23:19 +0000393TriggerStep *sqlite3TriggerInsertStep(
drh17435752007-08-16 04:30:38 +0000394 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000395 Token *pTableName, /* Name of the table into which we insert */
396 IdList *pColumn, /* List of columns in pTableName to insert into */
drhc977f7f2002-05-21 11:38:11 +0000397 Select *pSelect, /* A SELECT statement that supplies values */
shane5eff7cf2009-08-10 03:57:58 +0000398 u8 orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
danielk1977633ed082002-05-17 00:05:58 +0000399){
drhf3a65f72007-08-22 20:18:21 +0000400 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000401
drh75593d92014-01-10 20:46:55 +0000402 assert(pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000403
drhb7916a72009-05-27 10:31:29 +0000404 pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
danielk1977d5d56522005-03-16 12:15:20 +0000405 if( pTriggerStep ){
drh33e619f2009-05-28 01:00:55 +0000406 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
danielk1977d5d56522005-03-16 12:15:20 +0000407 pTriggerStep->pIdList = pColumn;
danielk1977d5d56522005-03-16 12:15:20 +0000408 pTriggerStep->orconf = orconf;
danielk1977d5d56522005-03-16 12:15:20 +0000409 }else{
drh633e6d52008-07-28 19:34:53 +0000410 sqlite3IdListDelete(db, pColumn);
danielk1977d5d56522005-03-16 12:15:20 +0000411 }
drh33e619f2009-05-28 01:00:55 +0000412 sqlite3SelectDelete(db, pSelect);
danielk1977c3f9bad2002-05-15 08:30:12 +0000413
danielk1977633ed082002-05-17 00:05:58 +0000414 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000415}
416
drhc977f7f2002-05-21 11:38:11 +0000417/*
418** Construct a trigger step that implements an UPDATE statement and return
419** a pointer to that trigger step. The parser calls this routine when it
420** sees an UPDATE statement inside the body of a CREATE TRIGGER.
421*/
danielk19774adee202004-05-08 08:23:19 +0000422TriggerStep *sqlite3TriggerUpdateStep(
drh17435752007-08-16 04:30:38 +0000423 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000424 Token *pTableName, /* Name of the table to be updated */
425 ExprList *pEList, /* The SET clause: list of column and new values */
426 Expr *pWhere, /* The WHERE clause */
shane5eff7cf2009-08-10 03:57:58 +0000427 u8 orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
drhc977f7f2002-05-21 11:38:11 +0000428){
drhb7916a72009-05-27 10:31:29 +0000429 TriggerStep *pTriggerStep;
430
431 pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
drh33e619f2009-05-28 01:00:55 +0000432 if( pTriggerStep ){
433 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
434 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
435 pTriggerStep->orconf = orconf;
drh0e3a6f32007-03-30 20:40:34 +0000436 }
drh33e619f2009-05-28 01:00:55 +0000437 sqlite3ExprListDelete(db, pEList);
438 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000439 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000440}
441
drhc977f7f2002-05-21 11:38:11 +0000442/*
443** Construct a trigger step that implements a DELETE statement and return
444** a pointer to that trigger step. The parser calls this routine when it
445** sees a DELETE statement inside the body of a CREATE TRIGGER.
446*/
drh17435752007-08-16 04:30:38 +0000447TriggerStep *sqlite3TriggerDeleteStep(
448 sqlite3 *db, /* Database connection */
449 Token *pTableName, /* The table from which rows are deleted */
450 Expr *pWhere /* The WHERE clause */
451){
drhb7916a72009-05-27 10:31:29 +0000452 TriggerStep *pTriggerStep;
453
454 pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
drh33e619f2009-05-28 01:00:55 +0000455 if( pTriggerStep ){
456 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
457 pTriggerStep->orconf = OE_Default;
drhb63a53d2007-03-31 01:34:44 +0000458 }
drh33e619f2009-05-28 01:00:55 +0000459 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000460 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000461}
462
danielk1977633ed082002-05-17 00:05:58 +0000463/*
464** Recursively delete a Trigger structure
465*/
drh633e6d52008-07-28 19:34:53 +0000466void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
drhf0f258b2003-04-21 18:48:45 +0000467 if( pTrigger==0 ) return;
drh633e6d52008-07-28 19:34:53 +0000468 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
dan165921a2009-08-28 18:53:45 +0000469 sqlite3DbFree(db, pTrigger->zName);
drh633e6d52008-07-28 19:34:53 +0000470 sqlite3DbFree(db, pTrigger->table);
471 sqlite3ExprDelete(db, pTrigger->pWhen);
472 sqlite3IdListDelete(db, pTrigger->pColumns);
drh633e6d52008-07-28 19:34:53 +0000473 sqlite3DbFree(db, pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000474}
475
476/*
danielk19778a414492004-06-29 08:59:35 +0000477** This function is called to drop a trigger from the database schema.
478**
479** This may be called directly from the parser and therefore identifies
480** the trigger by name. The sqlite3DropTriggerPtr() routine does the
481** same job as this routine except it takes a pointer to the trigger
482** instead of the trigger name.
483**/
drhfdd48a72006-09-11 23:45:48 +0000484void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000485 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000486 int i;
487 const char *zDb;
488 const char *zName;
drh9bb575f2004-09-06 17:24:11 +0000489 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000490
drh17435752007-08-16 04:30:38 +0000491 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000492 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000493 goto drop_trigger_cleanup;
494 }
danielk19778e227872004-06-07 07:52:17 +0000495
drhd24cc422003-03-27 12:51:24 +0000496 assert( pName->nSrc==1 );
497 zDb = pName->a[0].zDatabase;
498 zName = pName->a[0].zName;
drh21206082011-04-04 18:22:02 +0000499 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
danielk197753c0f742005-03-29 03:10:59 +0000500 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000501 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000502 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
drh21206082011-04-04 18:22:02 +0000503 assert( sqlite3SchemaMutexHeld(db, j, 0) );
drhacbcb7e2014-08-21 20:26:37 +0000504 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
drhd24cc422003-03-27 12:51:24 +0000505 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000506 }
drhd24cc422003-03-27 12:51:24 +0000507 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000508 if( !noErr ){
509 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
dan57966752011-04-09 17:32:58 +0000510 }else{
511 sqlite3CodeVerifyNamedSchema(pParse, zDb);
drhfdd48a72006-09-11 23:45:48 +0000512 }
dan1db95102010-06-28 10:15:19 +0000513 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +0000514 goto drop_trigger_cleanup;
515 }
drh74161702006-02-24 02:53:49 +0000516 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000517
518drop_trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000519 sqlite3SrcListDelete(db, pName);
drh79a519c2003-05-17 19:04:03 +0000520}
521
522/*
drh956bc922004-07-24 17:38:29 +0000523** Return a pointer to the Table structure for the table that a trigger
524** is set on.
525*/
drh74161702006-02-24 02:53:49 +0000526static Table *tableOfTrigger(Trigger *pTrigger){
drhacbcb7e2014-08-21 20:26:37 +0000527 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
drh956bc922004-07-24 17:38:29 +0000528}
529
530
531/*
drh74161702006-02-24 02:53:49 +0000532** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000533*/
drh74161702006-02-24 02:53:49 +0000534void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000535 Table *pTable;
536 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000537 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000538 int iDb;
drh79a519c2003-05-17 19:04:03 +0000539
danielk1977da184232006-01-05 11:34:32 +0000540 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000541 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000542 pTable = tableOfTrigger(pTrigger);
drh43617e92006-03-06 20:55:46 +0000543 assert( pTable );
danielk1977da184232006-01-05 11:34:32 +0000544 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000545#ifndef SQLITE_OMIT_AUTHORIZATION
546 {
547 int code = SQLITE_DROP_TRIGGER;
drh956bc922004-07-24 17:38:29 +0000548 const char *zDb = db->aDb[iDb].zName;
549 const char *zTab = SCHEMA_TABLE(iDb);
550 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
dan2bd93512009-08-31 08:22:46 +0000551 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
danielk19774adee202004-05-08 08:23:19 +0000552 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000553 return;
drhe5f9c642003-01-13 23:27:31 +0000554 }
drhed6c8672003-01-12 18:02:16 +0000555 }
drhe5f9c642003-01-13 23:27:31 +0000556#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000557
drhf0f258b2003-04-21 18:48:45 +0000558 /* Generate code to destroy the database record of the trigger.
559 */
drh43617e92006-03-06 20:55:46 +0000560 assert( pTable!=0 );
561 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drhf0f258b2003-04-21 18:48:45 +0000562 int base;
drhb06a4ec2014-03-10 18:03:09 +0000563 static const int iLn = VDBE_OFFSET_LINENO(2);
drh57196282004-10-06 15:41:16 +0000564 static const VdbeOpList dropTrigger[] = {
drh9b1b01b2003-08-16 12:37:51 +0000565 { OP_Rewind, 0, ADDR(9), 0},
drh1db639c2008-01-17 02:36:28 +0000566 { OP_String8, 0, 1, 0}, /* 1 */
567 { OP_Column, 0, 1, 2},
568 { OP_Ne, 2, ADDR(8), 1},
569 { OP_String8, 0, 1, 0}, /* 4: "trigger" */
570 { OP_Column, 0, 0, 2},
571 { OP_Ne, 2, ADDR(8), 1},
drhf0f258b2003-04-21 18:48:45 +0000572 { OP_Delete, 0, 0, 0},
drh9b1b01b2003-08-16 12:37:51 +0000573 { OP_Next, 0, ADDR(1), 0}, /* 8 */
drhf0f258b2003-04-21 18:48:45 +0000574 };
575
drh956bc922004-07-24 17:38:29 +0000576 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk1977c00da102006-01-07 13:21:04 +0000577 sqlite3OpenMasterTable(pParse, iDb);
drh688852a2014-02-17 22:40:43 +0000578 base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger, iLn);
drh8d129422011-04-05 12:25:19 +0000579 sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, P4_TRANSIENT);
drh24003452008-01-03 01:28:59 +0000580 sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
drh9cbf3422008-01-17 16:22:13 +0000581 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +0000582 sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
dan165921a2009-08-28 18:53:45 +0000583 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
danielk19776ab3a2e2009-02-19 14:39:25 +0000584 if( pParse->nMem<3 ){
585 pParse->nMem = 3;
586 }
drhf0f258b2003-04-21 18:48:45 +0000587 }
drh956bc922004-07-24 17:38:29 +0000588}
drhf0f258b2003-04-21 18:48:45 +0000589
drh956bc922004-07-24 17:38:29 +0000590/*
591** Remove a trigger from the hash tables of the sqlite* pointer.
592*/
593void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
594 Trigger *pTrigger;
drh21206082011-04-04 18:22:02 +0000595 Hash *pHash;
596
597 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
598 pHash = &(db->aDb[iDb].pSchema->trigHash);
drhacbcb7e2014-08-21 20:26:37 +0000599 pTrigger = sqlite3HashInsert(pHash, zName, 0);
drh9ab4c2e2009-05-09 00:18:38 +0000600 if( ALWAYS(pTrigger) ){
danielk19772f886d12009-02-28 10:47:41 +0000601 if( pTrigger->pSchema==pTrigger->pTabSchema ){
602 Table *pTab = tableOfTrigger(pTrigger);
603 Trigger **pp;
604 for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
605 *pp = (*pp)->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +0000606 }
drh633e6d52008-07-28 19:34:53 +0000607 sqlite3DeleteTrigger(db, pTrigger);
drh956bc922004-07-24 17:38:29 +0000608 db->flags |= SQLITE_InternChanges;
danielk1977c3f9bad2002-05-15 08:30:12 +0000609 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000610}
611
drhc977f7f2002-05-21 11:38:11 +0000612/*
613** pEList is the SET clause of an UPDATE statement. Each entry
614** in pEList is of the format <id>=<expr>. If any of the entries
615** in pEList have an <id> which matches an identifier in pIdList,
616** then return TRUE. If pIdList==NULL, then it is considered a
617** wildcard that matches anything. Likewise if pEList==NULL then
618** it matches anything so always return true. Return false only
619** if there is no match.
620*/
drh9ab4c2e2009-05-09 00:18:38 +0000621static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000622 int e;
drh9ab4c2e2009-05-09 00:18:38 +0000623 if( pIdList==0 || NEVER(pEList==0) ) return 1;
drhad2d8302002-05-24 20:31:36 +0000624 for(e=0; e<pEList->nExpr; e++){
danielk19774adee202004-05-08 08:23:19 +0000625 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000626 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000627 return 0;
628}
629
danielk1977c3f9bad2002-05-15 08:30:12 +0000630/*
danielk19772f886d12009-02-28 10:47:41 +0000631** Return a list of all triggers on table pTab if there exists at least
632** one trigger that must be fired when an operation of type 'op' is
633** performed on the table, and, if that operation is an UPDATE, if at
634** least one of the columns in pChanges is being modified.
drhdca76842004-12-07 14:06:13 +0000635*/
danielk19772f886d12009-02-28 10:47:41 +0000636Trigger *sqlite3TriggersExist(
637 Parse *pParse, /* Parse context */
drhdca76842004-12-07 14:06:13 +0000638 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000639 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
danielk19772f886d12009-02-28 10:47:41 +0000640 ExprList *pChanges, /* Columns that change in an UPDATE statement */
641 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
drhc977f7f2002-05-21 11:38:11 +0000642){
drhdca76842004-12-07 14:06:13 +0000643 int mask = 0;
drhe83cafd2011-03-21 17:15:58 +0000644 Trigger *pList = 0;
danielk19772f886d12009-02-28 10:47:41 +0000645 Trigger *p;
drhe83cafd2011-03-21 17:15:58 +0000646
647 if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
648 pList = sqlite3TriggerList(pParse, pTab);
649 }
danielk19772f886d12009-02-28 10:47:41 +0000650 assert( pList==0 || IsVirtual(pTab)==0 );
651 for(p=pList; p; p=p->pNext){
drh9ab4c2e2009-05-09 00:18:38 +0000652 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
danielk19772f886d12009-02-28 10:47:41 +0000653 mask |= p->tr_tm;
danielk1977c3f9bad2002-05-15 08:30:12 +0000654 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000655 }
danielk19772f886d12009-02-28 10:47:41 +0000656 if( pMask ){
657 *pMask = mask;
658 }
659 return (mask ? pList : 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000660}
661
drhc977f7f2002-05-21 11:38:11 +0000662/*
drhf26e09c2003-05-31 16:21:12 +0000663** Convert the pStep->target token into a SrcList and return a pointer
664** to that SrcList.
665**
666** This routine adds a specific database name, if needed, to the target when
667** forming the SrcList. This prevents a trigger in one database from
668** referring to a target in another database. An exception is when the
669** trigger is in TEMP in which case it can refer to any other database it
670** wants.
671*/
672static SrcList *targetSrcList(
673 Parse *pParse, /* The parsing context */
674 TriggerStep *pStep /* The trigger containing the target token */
675){
drhf26e09c2003-05-31 16:21:12 +0000676 int iDb; /* Index of the database to use */
677 SrcList *pSrc; /* SrcList to be returned */
678
drhb7916a72009-05-27 10:31:29 +0000679 pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
680 if( pSrc ){
681 assert( pSrc->nSrc>0 );
drhb7916a72009-05-27 10:31:29 +0000682 iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
683 if( iDb==0 || iDb>=2 ){
684 sqlite3 *db = pParse->db;
685 assert( iDb<pParse->db->nDb );
686 pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
687 }
drhf26e09c2003-05-31 16:21:12 +0000688 }
689 return pSrc;
690}
691
692/*
dan65a7cd12009-09-01 12:16:01 +0000693** Generate VDBE code for the statements inside the body of a single
694** trigger.
drhc977f7f2002-05-21 11:38:11 +0000695*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000696static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000697 Parse *pParse, /* The parser context */
698 TriggerStep *pStepList, /* List of statements inside the trigger body */
dan65a7cd12009-09-01 12:16:01 +0000699 int orconf /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000700){
dan65a7cd12009-09-01 12:16:01 +0000701 TriggerStep *pStep;
drh344737f2004-09-19 00:50:20 +0000702 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000703 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000704
dan65a7cd12009-09-01 12:16:01 +0000705 assert( pParse->pTriggerTab && pParse->pToplevel );
706 assert( pStepList );
drh344737f2004-09-19 00:50:20 +0000707 assert( v!=0 );
dan65a7cd12009-09-01 12:16:01 +0000708 for(pStep=pStepList; pStep; pStep=pStep->pNext){
dan165921a2009-08-28 18:53:45 +0000709 /* Figure out the ON CONFLICT policy that will be used for this step
710 ** of the trigger program. If the statement that caused this trigger
711 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
712 ** the ON CONFLICT policy that was specified as part of the trigger
713 ** step statement. Example:
714 **
715 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
716 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
717 ** END;
718 **
719 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
720 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
721 */
shanecea72b22009-09-07 04:38:36 +0000722 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
drhaceb31b2014-02-08 01:40:27 +0000723 assert( pParse->okConstFactor==0 );
danf78baaf2012-12-06 19:37:22 +0000724
dan165921a2009-08-28 18:53:45 +0000725 switch( pStep->op ){
danielk1977633ed082002-05-17 00:05:58 +0000726 case TK_UPDATE: {
dan165921a2009-08-28 18:53:45 +0000727 sqlite3Update(pParse,
728 targetSrcList(pParse, pStep),
729 sqlite3ExprListDup(db, pStep->pExprList, 0),
730 sqlite3ExprDup(db, pStep->pWhere, 0),
dan65a7cd12009-09-01 12:16:01 +0000731 pParse->eOrconf
dan165921a2009-08-28 18:53:45 +0000732 );
danielk1977633ed082002-05-17 00:05:58 +0000733 break;
734 }
735 case TK_INSERT: {
dan165921a2009-08-28 18:53:45 +0000736 sqlite3Insert(pParse,
737 targetSrcList(pParse, pStep),
dan165921a2009-08-28 18:53:45 +0000738 sqlite3SelectDup(db, pStep->pSelect, 0),
739 sqlite3IdListDup(db, pStep->pIdList),
dan65a7cd12009-09-01 12:16:01 +0000740 pParse->eOrconf
dan165921a2009-08-28 18:53:45 +0000741 );
danielk1977633ed082002-05-17 00:05:58 +0000742 break;
743 }
744 case TK_DELETE: {
dan165921a2009-08-28 18:53:45 +0000745 sqlite3DeleteFrom(pParse,
746 targetSrcList(pParse, pStep),
747 sqlite3ExprDup(db, pStep->pWhere, 0)
748 );
danielk1977633ed082002-05-17 00:05:58 +0000749 break;
750 }
dan165921a2009-08-28 18:53:45 +0000751 default: assert( pStep->op==TK_SELECT ); {
752 SelectDest sDest;
753 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
754 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
755 sqlite3Select(pParse, pSelect, &sDest);
756 sqlite3SelectDelete(db, pSelect);
drh9ab4c2e2009-05-09 00:18:38 +0000757 break;
758 }
danielk1977633ed082002-05-17 00:05:58 +0000759 }
dan76d462e2009-08-30 11:42:51 +0000760 if( pStep->op!=TK_SELECT ){
drhb7f1d9a2009-09-08 02:27:58 +0000761 sqlite3VdbeAddOp0(v, OP_ResetCount);
dan76d462e2009-08-30 11:42:51 +0000762 }
danielk1977633ed082002-05-17 00:05:58 +0000763 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000764
danielk1977633ed082002-05-17 00:05:58 +0000765 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000766}
767
drhc7379ce2013-10-30 02:28:23 +0000768#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
dan165921a2009-08-28 18:53:45 +0000769/*
770** This function is used to add VdbeComment() annotations to a VDBE
771** program. It is not used in production code, only for debugging.
772*/
773static const char *onErrorText(int onError){
774 switch( onError ){
775 case OE_Abort: return "abort";
776 case OE_Rollback: return "rollback";
777 case OE_Fail: return "fail";
778 case OE_Replace: return "replace";
779 case OE_Ignore: return "ignore";
780 case OE_Default: return "default";
781 }
782 return "n/a";
783}
784#endif
785
786/*
787** Parse context structure pFrom has just been used to create a sub-vdbe
788** (trigger program). If an error has occurred, transfer error information
789** from pFrom to pTo.
790*/
791static void transferParseError(Parse *pTo, Parse *pFrom){
792 assert( pFrom->zErrMsg==0 || pFrom->nErr );
793 assert( pTo->zErrMsg==0 || pTo->nErr );
794 if( pTo->nErr==0 ){
795 pTo->zErrMsg = pFrom->zErrMsg;
796 pTo->nErr = pFrom->nErr;
drhe06874e2015-04-16 15:47:06 +0000797 pTo->rc = pFrom->rc;
dan165921a2009-08-28 18:53:45 +0000798 }else{
799 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
800 }
801}
802
dan65a7cd12009-09-01 12:16:01 +0000803/*
804** Create and populate a new TriggerPrg object with a sub-program
805** implementing trigger pTrigger with ON CONFLICT policy orconf.
806*/
dan2832ad42009-08-31 15:27:27 +0000807static TriggerPrg *codeRowTrigger(
dan165921a2009-08-28 18:53:45 +0000808 Parse *pParse, /* Current parse context */
809 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +0000810 Table *pTab, /* The table pTrigger is attached to */
811 int orconf /* ON CONFLICT policy to code trigger program with */
dan165921a2009-08-28 18:53:45 +0000812){
dan65a7cd12009-09-01 12:16:01 +0000813 Parse *pTop = sqlite3ParseToplevel(pParse);
814 sqlite3 *db = pParse->db; /* Database handle */
815 TriggerPrg *pPrg; /* Value to return */
dan165921a2009-08-28 18:53:45 +0000816 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
817 Vdbe *v; /* Temporary VM */
dan165921a2009-08-28 18:53:45 +0000818 NameContext sNC; /* Name context for sub-vdbe */
819 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
820 Parse *pSubParse; /* Parse context for sub-vdbe */
821 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
822
dan1da40a32009-09-19 17:00:31 +0000823 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dand19c9332010-07-26 12:05:17 +0000824 assert( pTop->pVdbe );
dan65a7cd12009-09-01 12:16:01 +0000825
826 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
827 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
828 ** list of the top-level Parse object sooner rather than later. */
dan2832ad42009-08-31 15:27:27 +0000829 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
830 if( !pPrg ) return 0;
dan65a7cd12009-09-01 12:16:01 +0000831 pPrg->pNext = pTop->pTriggerPrg;
832 pTop->pTriggerPrg = pPrg;
dan2832ad42009-08-31 15:27:27 +0000833 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
dan165921a2009-08-28 18:53:45 +0000834 if( !pProgram ) return 0;
dand19c9332010-07-26 12:05:17 +0000835 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
dan2832ad42009-08-31 15:27:27 +0000836 pPrg->pTrigger = pTrigger;
837 pPrg->orconf = orconf;
danbb5f1682009-11-27 12:12:34 +0000838 pPrg->aColmask[0] = 0xffffffff;
839 pPrg->aColmask[1] = 0xffffffff;
dan165921a2009-08-28 18:53:45 +0000840
dan65a7cd12009-09-01 12:16:01 +0000841 /* Allocate and populate a new Parse context to use for coding the
842 ** trigger sub-program. */
843 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
844 if( !pSubParse ) return 0;
dan165921a2009-08-28 18:53:45 +0000845 memset(&sNC, 0, sizeof(sNC));
846 sNC.pParse = pSubParse;
847 pSubParse->db = db;
848 pSubParse->pTriggerTab = pTab;
dan65a7cd12009-09-01 12:16:01 +0000849 pSubParse->pToplevel = pTop;
dan2bd93512009-08-31 08:22:46 +0000850 pSubParse->zAuthContext = pTrigger->zName;
dan65a7cd12009-09-01 12:16:01 +0000851 pSubParse->eTriggerOp = pTrigger->op;
drh8b307fb2010-04-06 15:57:05 +0000852 pSubParse->nQueryLoop = pParse->nQueryLoop;
dan165921a2009-08-28 18:53:45 +0000853
854 v = sqlite3GetVdbe(pSubParse);
855 if( v ){
dan76d462e2009-08-30 11:42:51 +0000856 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
857 pTrigger->zName, onErrorText(orconf),
dan165921a2009-08-28 18:53:45 +0000858 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
dan65a7cd12009-09-01 12:16:01 +0000859 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
860 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
861 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
dan76d462e2009-08-30 11:42:51 +0000862 pTab->zName
dan165921a2009-08-28 18:53:45 +0000863 ));
dan76d462e2009-08-30 11:42:51 +0000864#ifndef SQLITE_OMIT_TRACE
865 sqlite3VdbeChangeP4(v, -1,
866 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
867 );
868#endif
dan165921a2009-08-28 18:53:45 +0000869
dan65a7cd12009-09-01 12:16:01 +0000870 /* If one was specified, code the WHEN clause. If it evaluates to false
871 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
872 ** OP_Halt inserted at the end of the program. */
dan165921a2009-08-28 18:53:45 +0000873 if( pTrigger->pWhen ){
dan165921a2009-08-28 18:53:45 +0000874 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
dan523a0872009-08-31 05:23:32 +0000875 if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
876 && db->mallocFailed==0
877 ){
dan165921a2009-08-28 18:53:45 +0000878 iEndTrigger = sqlite3VdbeMakeLabel(v);
879 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
880 }
881 sqlite3ExprDelete(db, pWhen);
882 }
883
884 /* Code the trigger program into the sub-vdbe. */
dan76d462e2009-08-30 11:42:51 +0000885 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
dan65a7cd12009-09-01 12:16:01 +0000886
887 /* Insert an OP_Halt at the end of the sub-program. */
dan165921a2009-08-28 18:53:45 +0000888 if( iEndTrigger ){
889 sqlite3VdbeResolveLabel(v, iEndTrigger);
890 }
891 sqlite3VdbeAddOp0(v, OP_Halt);
dan76d462e2009-08-30 11:42:51 +0000892 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
893
dan165921a2009-08-28 18:53:45 +0000894 transferParseError(pParse, pSubParse);
dan523a0872009-08-31 05:23:32 +0000895 if( db->mallocFailed==0 ){
dan65a7cd12009-09-01 12:16:01 +0000896 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
dan523a0872009-08-31 05:23:32 +0000897 }
dan165921a2009-08-28 18:53:45 +0000898 pProgram->nMem = pSubParse->nMem;
899 pProgram->nCsr = pSubParse->nTab;
dan1d8cb212011-12-09 13:24:16 +0000900 pProgram->nOnce = pSubParse->nOnce;
dan165921a2009-08-28 18:53:45 +0000901 pProgram->token = (void *)pTrigger;
danbb5f1682009-11-27 12:12:34 +0000902 pPrg->aColmask[0] = pSubParse->oldmask;
903 pPrg->aColmask[1] = pSubParse->newmask;
dan165921a2009-08-28 18:53:45 +0000904 sqlite3VdbeDelete(v);
dan165921a2009-08-28 18:53:45 +0000905 }
dan65a7cd12009-09-01 12:16:01 +0000906
907 assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
908 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
drhf30a9692013-11-15 01:10:18 +0000909 sqlite3ParserReset(pSubParse);
dan165921a2009-08-28 18:53:45 +0000910 sqlite3StackFree(db, pSubParse);
911
dan2832ad42009-08-31 15:27:27 +0000912 return pPrg;
dan165921a2009-08-28 18:53:45 +0000913}
914
dan65a7cd12009-09-01 12:16:01 +0000915/*
916** Return a pointer to a TriggerPrg object containing the sub-program for
917** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
918** TriggerPrg object exists, a new object is allocated and populated before
919** being returned.
920*/
dan2832ad42009-08-31 15:27:27 +0000921static TriggerPrg *getRowTrigger(
dan65a7cd12009-09-01 12:16:01 +0000922 Parse *pParse, /* Current parse context */
dan165921a2009-08-28 18:53:45 +0000923 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +0000924 Table *pTab, /* The table trigger pTrigger is attached to */
925 int orconf /* ON CONFLICT algorithm. */
dan165921a2009-08-28 18:53:45 +0000926){
dan65a7cd12009-09-01 12:16:01 +0000927 Parse *pRoot = sqlite3ParseToplevel(pParse);
dan2832ad42009-08-31 15:27:27 +0000928 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +0000929
dan1da40a32009-09-19 17:00:31 +0000930 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dan165921a2009-08-28 18:53:45 +0000931
932 /* It may be that this trigger has already been coded (or is in the
933 ** process of being coded). If this is the case, then an entry with
dan2832ad42009-08-31 15:27:27 +0000934 ** a matching TriggerPrg.pTrigger field will be present somewhere
935 ** in the Parse.pTriggerPrg list. Search for such an entry. */
dan2832ad42009-08-31 15:27:27 +0000936 for(pPrg=pRoot->pTriggerPrg;
937 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
938 pPrg=pPrg->pNext
dan165921a2009-08-28 18:53:45 +0000939 );
940
dan65a7cd12009-09-01 12:16:01 +0000941 /* If an existing TriggerPrg could not be located, create a new one. */
dan2832ad42009-08-31 15:27:27 +0000942 if( !pPrg ){
dan65a7cd12009-09-01 12:16:01 +0000943 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
dan165921a2009-08-28 18:53:45 +0000944 }
945
dan2832ad42009-08-31 15:27:27 +0000946 return pPrg;
dan165921a2009-08-28 18:53:45 +0000947}
948
dan94d7f502009-09-24 09:05:49 +0000949/*
950** Generate code for the trigger program associated with trigger p on
951** table pTab. The reg, orconf and ignoreJump parameters passed to this
952** function are the same as those described in the header function for
953** sqlite3CodeRowTrigger()
954*/
dan1da40a32009-09-19 17:00:31 +0000955void sqlite3CodeRowTriggerDirect(
956 Parse *pParse, /* Parse context */
957 Trigger *p, /* Trigger to code */
958 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +0000959 int reg, /* Reg array containing OLD.* and NEW.* values */
dan1da40a32009-09-19 17:00:31 +0000960 int orconf, /* ON CONFLICT policy */
961 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
962){
963 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
964 TriggerPrg *pPrg;
965 pPrg = getRowTrigger(pParse, p, pTab, orconf);
966 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
967
968 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
969 ** is a pointer to the sub-vdbe containing the trigger program. */
970 if( pPrg ){
dand19c9332010-07-26 12:05:17 +0000971 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
972
dan94d7f502009-09-24 09:05:49 +0000973 sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem);
dan1da40a32009-09-19 17:00:31 +0000974 sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
975 VdbeComment(
976 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
977
978 /* Set the P5 operand of the OP_Program instruction to non-zero if
979 ** recursive invocation of this trigger program is disallowed. Recursive
980 ** invocation is disallowed if (a) the sub-program is really a trigger,
981 ** not a foreign key action, and (b) the flag to enable recursive triggers
982 ** is clear. */
dand19c9332010-07-26 12:05:17 +0000983 sqlite3VdbeChangeP5(v, (u8)bRecursive);
dan1da40a32009-09-19 17:00:31 +0000984 }
985}
986
danielk1977633ed082002-05-17 00:05:58 +0000987/*
dan94d7f502009-09-24 09:05:49 +0000988** This is called to code the required FOR EACH ROW triggers for an operation
989** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
drhf7b54962013-05-28 12:11:54 +0000990** is given by the op parameter. The tr_tm parameter determines whether the
dan94d7f502009-09-24 09:05:49 +0000991** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
992** parameter pChanges is passed the list of columns being modified.
danielk1977633ed082002-05-17 00:05:58 +0000993**
dan94d7f502009-09-24 09:05:49 +0000994** If there are no triggers that fire at the specified time for the specified
995** operation on pTab, this function is a no-op.
drhc977f7f2002-05-21 11:38:11 +0000996**
dan94d7f502009-09-24 09:05:49 +0000997** The reg argument is the address of the first in an array of registers
998** that contain the values substituted for the new.* and old.* references
999** in the trigger program. If N is the number of columns in table pTab
1000** (a copy of pTab->nCol), then registers are populated as follows:
drhc977f7f2002-05-21 11:38:11 +00001001**
dan94d7f502009-09-24 09:05:49 +00001002** Register Contains
1003** ------------------------------------------------------
1004** reg+0 OLD.rowid
1005** reg+1 OLD.* value of left-most column of pTab
1006** ... ...
1007** reg+N OLD.* value of right-most column of pTab
1008** reg+N+1 NEW.rowid
1009** reg+N+2 OLD.* value of left-most column of pTab
1010** ... ...
1011** reg+N+N+1 NEW.* value of right-most column of pTab
drhc977f7f2002-05-21 11:38:11 +00001012**
dan94d7f502009-09-24 09:05:49 +00001013** For ON DELETE triggers, the registers containing the NEW.* values will
1014** never be accessed by the trigger program, so they are not allocated or
1015** populated by the caller (there is no data to populate them with anyway).
1016** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
1017** are never accessed, and so are not allocated by the caller. So, for an
1018** ON INSERT trigger, the value passed to this function as parameter reg
1019** is not a readable register, although registers (reg+N) through
1020** (reg+N+N+1) are.
danielk1977633ed082002-05-17 00:05:58 +00001021**
dan94d7f502009-09-24 09:05:49 +00001022** Parameter orconf is the default conflict resolution algorithm for the
1023** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
1024** is the instruction that control should jump to if a trigger program
1025** raises an IGNORE exception.
danielk1977633ed082002-05-17 00:05:58 +00001026*/
dan165921a2009-08-28 18:53:45 +00001027void sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +00001028 Parse *pParse, /* Parse context */
danielk19772f886d12009-02-28 10:47:41 +00001029 Trigger *pTrigger, /* List of triggers on table pTab */
danielk1977633ed082002-05-17 00:05:58 +00001030 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
1031 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +00001032 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +00001033 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001034 int reg, /* The first in an array of registers (see above) */
danielk19776f349032002-06-11 02:25:40 +00001035 int orconf, /* ON CONFLICT policy */
dan165921a2009-08-28 18:53:45 +00001036 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
drhc977f7f2002-05-21 11:38:11 +00001037){
dan94d7f502009-09-24 09:05:49 +00001038 Trigger *p; /* Used to iterate through pTrigger list */
danielk19778f2c54e2008-01-01 19:02:09 +00001039
dan94d7f502009-09-24 09:05:49 +00001040 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
1041 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
1042 assert( (op==TK_UPDATE)==(pChanges!=0) );
danielk1977c3f9bad2002-05-15 08:30:12 +00001043
danielk19772f886d12009-02-28 10:47:41 +00001044 for(p=pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +00001045
drh9ab4c2e2009-05-09 00:18:38 +00001046 /* Sanity checking: The schema for the trigger and for the table are
1047 ** always defined. The trigger must be in the same schema as the table
1048 ** or else it must be a TEMP trigger. */
1049 assert( p->pSchema!=0 );
1050 assert( p->pTabSchema!=0 );
dan165921a2009-08-28 18:53:45 +00001051 assert( p->pSchema==p->pTabSchema
1052 || p->pSchema==pParse->db->aDb[1].pSchema );
drh9ab4c2e2009-05-09 00:18:38 +00001053
danielk1977eecfb3e2006-01-10 12:31:39 +00001054 /* Determine whether we should code this trigger */
dan165921a2009-08-28 18:53:45 +00001055 if( p->op==op
1056 && p->tr_tm==tr_tm
dan94d7f502009-09-24 09:05:49 +00001057 && checkColumnOverlap(p->pColumns, pChanges)
danielk1977eecfb3e2006-01-10 12:31:39 +00001058 ){
dan94d7f502009-09-24 09:05:49 +00001059 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
danielk1977c3f9bad2002-05-15 08:30:12 +00001060 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001061 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001062}
dan165921a2009-08-28 18:53:45 +00001063
dan2832ad42009-08-31 15:27:27 +00001064/*
danbb5f1682009-11-27 12:12:34 +00001065** Triggers may access values stored in the old.* or new.* pseudo-table.
1066** This function returns a 32-bit bitmask indicating which columns of the
1067** old.* or new.* tables actually are used by triggers. This information
1068** may be used by the caller, for example, to avoid having to load the entire
1069** old.* record into memory when executing an UPDATE or DELETE command.
dan2832ad42009-08-31 15:27:27 +00001070**
1071** Bit 0 of the returned mask is set if the left-most column of the
danbb5f1682009-11-27 12:12:34 +00001072** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
dan2832ad42009-08-31 15:27:27 +00001073** the second leftmost column value is required, and so on. If there
1074** are more than 32 columns in the table, and at least one of the columns
1075** with an index greater than 32 may be accessed, 0xffffffff is returned.
1076**
danbb5f1682009-11-27 12:12:34 +00001077** It is not possible to determine if the old.rowid or new.rowid column is
1078** accessed by triggers. The caller must always assume that it is.
dan2832ad42009-08-31 15:27:27 +00001079**
danbb5f1682009-11-27 12:12:34 +00001080** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
1081** applies to the old.* table. If 1, the new.* table.
1082**
1083** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
1084** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
1085** included in the returned mask if the TRIGGER_BEFORE bit is set in the
1086** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
1087** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
dan2832ad42009-08-31 15:27:27 +00001088*/
danbb5f1682009-11-27 12:12:34 +00001089u32 sqlite3TriggerColmask(
dan165921a2009-08-28 18:53:45 +00001090 Parse *pParse, /* Parse context */
1091 Trigger *pTrigger, /* List of triggers on table pTab */
dan165921a2009-08-28 18:53:45 +00001092 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
danbb5f1682009-11-27 12:12:34 +00001093 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
1094 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
dan165921a2009-08-28 18:53:45 +00001095 Table *pTab, /* The table to code triggers from */
dan2832ad42009-08-31 15:27:27 +00001096 int orconf /* Default ON CONFLICT policy for trigger steps */
dan165921a2009-08-28 18:53:45 +00001097){
dan1da40a32009-09-19 17:00:31 +00001098 const int op = pChanges ? TK_UPDATE : TK_DELETE;
dan2832ad42009-08-31 15:27:27 +00001099 u32 mask = 0;
dan165921a2009-08-28 18:53:45 +00001100 Trigger *p;
dan165921a2009-08-28 18:53:45 +00001101
danbb5f1682009-11-27 12:12:34 +00001102 assert( isNew==1 || isNew==0 );
dan165921a2009-08-28 18:53:45 +00001103 for(p=pTrigger; p; p=p->pNext){
danbb5f1682009-11-27 12:12:34 +00001104 if( p->op==op && (tr_tm&p->tr_tm)
1105 && checkColumnOverlap(p->pColumns,pChanges)
1106 ){
dan2832ad42009-08-31 15:27:27 +00001107 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +00001108 pPrg = getRowTrigger(pParse, p, pTab, orconf);
dan2832ad42009-08-31 15:27:27 +00001109 if( pPrg ){
danbb5f1682009-11-27 12:12:34 +00001110 mask |= pPrg->aColmask[isNew];
dan165921a2009-08-28 18:53:45 +00001111 }
1112 }
1113 }
dan2832ad42009-08-31 15:27:27 +00001114
1115 return mask;
dan165921a2009-08-28 18:53:45 +00001116}
1117
drhb7f91642004-10-31 02:22:47 +00001118#endif /* !defined(SQLITE_OMIT_TRIGGER) */