blob: dcbaf5d3321b1156a62675cf72fd536a53df083a [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
130 ** name on pTableName if we are reparsing our of SQLITE_MASTER.
131 */
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) );
drhaa78bec2008-12-09 03:55:14 +0000183 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
drhea678832008-12-10 19:26:22 +0000184 zName, sqlite3Strlen30(zName)) ){
drhfdd48a72006-09-11 23:45:48 +0000185 if( !noErr ){
186 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
dan7687c832011-04-09 15:39:02 +0000187 }else{
188 assert( !db->init.busy );
189 sqlite3CodeVerifySchema(pParse, iDb);
drhfdd48a72006-09-11 23:45:48 +0000190 }
drhe5f9c642003-01-13 23:27:31 +0000191 goto trigger_cleanup;
danielk1977c3f9bad2002-05-15 08:30:12 +0000192 }
danielk1977ef2cb632004-05-29 02:37:19 +0000193
194 /* Do not create a trigger on a system table */
drhdca76842004-12-07 14:06:13 +0000195 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk19774adee202004-05-08 08:23:19 +0000196 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
drhd24cc422003-03-27 12:51:24 +0000197 pParse->nErr++;
198 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40 +0000199 }
danielk1977ef2cb632004-05-29 02:37:19 +0000200
201 /* INSTEAD of triggers are only for views and views only support INSTEAD
202 ** of triggers.
203 */
204 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000205 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drhda93d232003-03-31 02:12:46 +0000206 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000207 goto trigger_cleanup;
208 }
danielk1977ef2cb632004-05-29 02:37:19 +0000209 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000210 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drhda93d232003-03-31 02:12:46 +0000211 " trigger on table: %S", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000212 goto trigger_cleanup;
213 }
danielk1977da184232006-01-05 11:34:32 +0000214 iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977ef2cb632004-05-29 02:37:19 +0000215
drhd24cc422003-03-27 12:51:24 +0000216#ifndef SQLITE_OMIT_AUTHORIZATION
217 {
218 int code = SQLITE_CREATE_TRIGGER;
danielk1977da184232006-01-05 11:34:32 +0000219 const char *zDb = db->aDb[iTabDb].zName;
drhe22a3342003-04-22 20:30:37 +0000220 const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
danielk1977da184232006-01-05 11:34:32 +0000221 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19 +0000222 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24 +0000223 goto trigger_cleanup;
224 }
danielk1977da184232006-01-05 11:34:32 +0000225 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24 +0000226 goto trigger_cleanup;
227 }
228 }
229#endif
danielk1977d702fcc2002-05-26 23:24:40 +0000230
danielk1977ef2cb632004-05-29 02:37:19 +0000231 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04 +0000232 ** cannot appear on views. So we might as well translate every
233 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
234 ** elsewhere.
235 */
danielk1977d702fcc2002-05-26 23:24:40 +0000236 if (tr_tm == TK_INSTEAD){
237 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12 +0000238 }
239
240 /* Build the Trigger object */
drh17435752007-08-16 04:30:38 +0000241 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19 +0000242 if( pTrigger==0 ) goto trigger_cleanup;
dan165921a2009-08-28 18:53:45 +0000243 pTrigger->zName = zName;
drhe5f9c642003-01-13 23:27:31 +0000244 zName = 0;
drh17435752007-08-16 04:30:38 +0000245 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32 +0000246 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48 +0000247 pTrigger->pTabSchema = pTab->pSchema;
drhaa78bec2008-12-09 03:55:14 +0000248 pTrigger->op = (u8)op;
drhdca76842004-12-07 14:06:13 +0000249 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
danielk19776ab3a2e2009-02-19 14:39:25 +0000250 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
drh17435752007-08-16 04:30:38 +0000251 pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
drhf0f258b2003-04-21 18:48:45 +0000252 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19 +0000253 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45 +0000254
255trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000256 sqlite3DbFree(db, zName);
257 sqlite3SrcListDelete(db, pTableName);
258 sqlite3IdListDelete(db, pColumns);
259 sqlite3ExprDelete(db, pWhen);
danielk1977d5d56522005-03-16 12:15:20 +0000260 if( !pParse->pNewTrigger ){
drh633e6d52008-07-28 19:34:53 +0000261 sqlite3DeleteTrigger(db, pTrigger);
danielk1977d5d56522005-03-16 12:15:20 +0000262 }else{
263 assert( pParse->pNewTrigger==pTrigger );
264 }
drhf0f258b2003-04-21 18:48:45 +0000265}
266
267/*
268** This routine is called after all of the trigger actions have been parsed
269** in order to complete the process of building the trigger.
270*/
danielk19774adee202004-05-08 08:23:19 +0000271void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45 +0000272 Parse *pParse, /* Parser context */
273 TriggerStep *pStepList, /* The triggered program */
274 Token *pAll /* Token that describes the complete CREATE TRIGGER */
275){
drha8c62df2010-02-15 15:47:18 +0000276 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
277 char *zName; /* Name of trigger */
278 sqlite3 *db = pParse->db; /* The database */
279 DbFixer sFix; /* Fixer object */
280 int iDb; /* Database containing the trigger */
281 Token nameToken; /* Trigger name for error reporting */
drhf0f258b2003-04-21 18:48:45 +0000282
drhf0f258b2003-04-21 18:48:45 +0000283 pParse->pNewTrigger = 0;
drh9ab4c2e2009-05-09 00:18:38 +0000284 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
dan165921a2009-08-28 18:53:45 +0000285 zName = pTrig->zName;
danielk1977da184232006-01-05 11:34:32 +0000286 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32 +0000287 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53 +0000288 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32 +0000289 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53 +0000290 pStepList = pStepList->pNext;
291 }
dan165921a2009-08-28 18:53:45 +0000292 nameToken.z = pTrig->zName;
drhb7916a72009-05-27 10:31:29 +0000293 nameToken.n = sqlite3Strlen30(nameToken.z);
drhd100f692013-10-03 15:39:44 +0000294 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
295 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
dan46539d72013-10-03 12:29:38 +0000296 || sqlite3FixExpr(&sFix, pTrig->pWhen)
drhd100f692013-10-03 15:39:44 +0000297 ){
drhf26e09c2003-05-31 16:21:12 +0000298 goto triggerfinish_cleanup;
299 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000300
drha8c62df2010-02-15 15:47:18 +0000301 /* if we are not initializing,
drh9adf9ac2002-05-15 11:44:13 +0000302 ** build the sqlite_master entry
303 */
drh1d85d932004-02-14 23:05:52 +0000304 if( !db->init.busy ){
drhc977f7f2002-05-21 11:38:11 +0000305 Vdbe *v;
drh2d401ab2008-01-10 23:50:11 +0000306 char *z;
danielk1977c3f9bad2002-05-15 08:30:12 +0000307
308 /* Make an entry in the sqlite_master table */
danielk19774adee202004-05-08 08:23:19 +0000309 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000310 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000311 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11 +0000312 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
313 sqlite3NestedParse(pParse,
314 "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
danielk19772f886d12009-02-28 10:47:41 +0000315 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
drh2d401ab2008-01-10 23:50:11 +0000316 pTrig->table, z);
drh633e6d52008-07-28 19:34:53 +0000317 sqlite3DbFree(db, z);
drh9cbf3422008-01-17 16:22:13 +0000318 sqlite3ChangeCookie(pParse, iDb);
drh5d9c9da2011-06-03 20:11:17 +0000319 sqlite3VdbeAddParseSchemaOp(v, iDb,
320 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
danielk1977c3f9bad2002-05-15 08:30:12 +0000321 }
322
drh956bc922004-07-24 17:38:29 +0000323 if( db->init.busy ){
danielk19772f886d12009-02-28 10:47:41 +0000324 Trigger *pLink = pTrig;
325 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
drh21206082011-04-04 18:22:02 +0000326 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk19772f886d12009-02-28 10:47:41 +0000327 pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);
328 if( pTrig ){
drhf3a65f72007-08-22 20:18:21 +0000329 db->mallocFailed = 1;
danielk19772f886d12009-02-28 10:47:41 +0000330 }else if( pLink->pSchema==pLink->pTabSchema ){
331 Table *pTab;
drha83ccca2009-04-28 13:01:09 +0000332 int n = sqlite3Strlen30(pLink->table);
danielk19772f886d12009-02-28 10:47:41 +0000333 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n);
334 assert( pTab!=0 );
335 pLink->pNext = pTab->pTrigger;
336 pTab->pTrigger = pLink;
danielk1977d5d56522005-03-16 12:15:20 +0000337 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000338 }
339
drhf0f258b2003-04-21 18:48:45 +0000340triggerfinish_cleanup:
drh633e6d52008-07-28 19:34:53 +0000341 sqlite3DeleteTrigger(db, pTrig);
danielk1977bfb9e352005-01-24 13:03:32 +0000342 assert( !pParse->pNewTrigger );
drh633e6d52008-07-28 19:34:53 +0000343 sqlite3DeleteTriggerStep(db, pStepList);
drh4b59ab52002-08-24 18:24:51 +0000344}
danielk1977c3f9bad2002-05-15 08:30:12 +0000345
drh4b59ab52002-08-24 18:24:51 +0000346/*
drhc977f7f2002-05-21 11:38:11 +0000347** Turn a SELECT statement (that the pSelect parameter points to) into
348** a trigger step. Return a pointer to a TriggerStep structure.
349**
350** The parser calls this routine when it finds a SELECT statement in
351** body of a TRIGGER.
352*/
drh17435752007-08-16 04:30:38 +0000353TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
354 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000355 if( pTriggerStep==0 ) {
drh633e6d52008-07-28 19:34:53 +0000356 sqlite3SelectDelete(db, pSelect);
danielk19772e588c72005-12-09 14:25:08 +0000357 return 0;
358 }
danielk1977633ed082002-05-17 00:05:58 +0000359 pTriggerStep->op = TK_SELECT;
360 pTriggerStep->pSelect = pSelect;
361 pTriggerStep->orconf = OE_Default;
drhb7916a72009-05-27 10:31:29 +0000362 return pTriggerStep;
363}
danielk1977c3f9bad2002-05-15 08:30:12 +0000364
drhb7916a72009-05-27 10:31:29 +0000365/*
366** Allocate space to hold a new trigger step. The allocated space
367** holds both the TriggerStep object and the TriggerStep.target.z string.
368**
369** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
370*/
371static TriggerStep *triggerStepAllocate(
372 sqlite3 *db, /* Database connection */
shane5eff7cf2009-08-10 03:57:58 +0000373 u8 op, /* Trigger opcode */
drhb7916a72009-05-27 10:31:29 +0000374 Token *pName /* The target name */
375){
376 TriggerStep *pTriggerStep;
377
378 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n);
379 if( pTriggerStep ){
380 char *z = (char*)&pTriggerStep[1];
381 memcpy(z, pName->z, pName->n);
382 pTriggerStep->target.z = z;
383 pTriggerStep->target.n = pName->n;
384 pTriggerStep->op = op;
385 }
danielk1977633ed082002-05-17 00:05:58 +0000386 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000387}
388
drhc977f7f2002-05-21 11:38:11 +0000389/*
390** Build a trigger step out of an INSERT statement. Return a pointer
391** to the new trigger step.
392**
393** The parser calls this routine when it sees an INSERT inside the
394** body of a trigger.
395*/
danielk19774adee202004-05-08 08:23:19 +0000396TriggerStep *sqlite3TriggerInsertStep(
drh17435752007-08-16 04:30:38 +0000397 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000398 Token *pTableName, /* Name of the table into which we insert */
399 IdList *pColumn, /* List of columns in pTableName to insert into */
drhc977f7f2002-05-21 11:38:11 +0000400 Select *pSelect, /* A SELECT statement that supplies values */
shane5eff7cf2009-08-10 03:57:58 +0000401 u8 orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
danielk1977633ed082002-05-17 00:05:58 +0000402){
drhf3a65f72007-08-22 20:18:21 +0000403 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000404
drh75593d92014-01-10 20:46:55 +0000405 assert(pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000406
drhb7916a72009-05-27 10:31:29 +0000407 pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
danielk1977d5d56522005-03-16 12:15:20 +0000408 if( pTriggerStep ){
drh33e619f2009-05-28 01:00:55 +0000409 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
danielk1977d5d56522005-03-16 12:15:20 +0000410 pTriggerStep->pIdList = pColumn;
danielk1977d5d56522005-03-16 12:15:20 +0000411 pTriggerStep->orconf = orconf;
danielk1977d5d56522005-03-16 12:15:20 +0000412 }else{
drh633e6d52008-07-28 19:34:53 +0000413 sqlite3IdListDelete(db, pColumn);
danielk1977d5d56522005-03-16 12:15:20 +0000414 }
drh33e619f2009-05-28 01:00:55 +0000415 sqlite3SelectDelete(db, pSelect);
danielk1977c3f9bad2002-05-15 08:30:12 +0000416
danielk1977633ed082002-05-17 00:05:58 +0000417 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000418}
419
drhc977f7f2002-05-21 11:38:11 +0000420/*
421** Construct a trigger step that implements an UPDATE statement and return
422** a pointer to that trigger step. The parser calls this routine when it
423** sees an UPDATE statement inside the body of a CREATE TRIGGER.
424*/
danielk19774adee202004-05-08 08:23:19 +0000425TriggerStep *sqlite3TriggerUpdateStep(
drh17435752007-08-16 04:30:38 +0000426 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000427 Token *pTableName, /* Name of the table to be updated */
428 ExprList *pEList, /* The SET clause: list of column and new values */
429 Expr *pWhere, /* The WHERE clause */
shane5eff7cf2009-08-10 03:57:58 +0000430 u8 orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
drhc977f7f2002-05-21 11:38:11 +0000431){
drhb7916a72009-05-27 10:31:29 +0000432 TriggerStep *pTriggerStep;
433
434 pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
drh33e619f2009-05-28 01:00:55 +0000435 if( pTriggerStep ){
436 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
437 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
438 pTriggerStep->orconf = orconf;
drh0e3a6f32007-03-30 20:40:34 +0000439 }
drh33e619f2009-05-28 01:00:55 +0000440 sqlite3ExprListDelete(db, pEList);
441 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000442 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000443}
444
drhc977f7f2002-05-21 11:38:11 +0000445/*
446** Construct a trigger step that implements a DELETE statement and return
447** a pointer to that trigger step. The parser calls this routine when it
448** sees a DELETE statement inside the body of a CREATE TRIGGER.
449*/
drh17435752007-08-16 04:30:38 +0000450TriggerStep *sqlite3TriggerDeleteStep(
451 sqlite3 *db, /* Database connection */
452 Token *pTableName, /* The table from which rows are deleted */
453 Expr *pWhere /* The WHERE clause */
454){
drhb7916a72009-05-27 10:31:29 +0000455 TriggerStep *pTriggerStep;
456
457 pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
drh33e619f2009-05-28 01:00:55 +0000458 if( pTriggerStep ){
459 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
460 pTriggerStep->orconf = OE_Default;
drhb63a53d2007-03-31 01:34:44 +0000461 }
drh33e619f2009-05-28 01:00:55 +0000462 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000463 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000464}
465
danielk1977633ed082002-05-17 00:05:58 +0000466/*
467** Recursively delete a Trigger structure
468*/
drh633e6d52008-07-28 19:34:53 +0000469void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
drhf0f258b2003-04-21 18:48:45 +0000470 if( pTrigger==0 ) return;
drh633e6d52008-07-28 19:34:53 +0000471 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
dan165921a2009-08-28 18:53:45 +0000472 sqlite3DbFree(db, pTrigger->zName);
drh633e6d52008-07-28 19:34:53 +0000473 sqlite3DbFree(db, pTrigger->table);
474 sqlite3ExprDelete(db, pTrigger->pWhen);
475 sqlite3IdListDelete(db, pTrigger->pColumns);
drh633e6d52008-07-28 19:34:53 +0000476 sqlite3DbFree(db, pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000477}
478
479/*
danielk19778a414492004-06-29 08:59:35 +0000480** This function is called to drop a trigger from the database schema.
481**
482** This may be called directly from the parser and therefore identifies
483** the trigger by name. The sqlite3DropTriggerPtr() routine does the
484** same job as this routine except it takes a pointer to the trigger
485** instead of the trigger name.
486**/
drhfdd48a72006-09-11 23:45:48 +0000487void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000488 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000489 int i;
490 const char *zDb;
491 const char *zName;
492 int nName;
drh9bb575f2004-09-06 17:24:11 +0000493 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000494
drh17435752007-08-16 04:30:38 +0000495 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000496 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000497 goto drop_trigger_cleanup;
498 }
danielk19778e227872004-06-07 07:52:17 +0000499
drhd24cc422003-03-27 12:51:24 +0000500 assert( pName->nSrc==1 );
501 zDb = pName->a[0].zDatabase;
502 zName = pName->a[0].zName;
drhea678832008-12-10 19:26:22 +0000503 nName = sqlite3Strlen30(zName);
drh21206082011-04-04 18:22:02 +0000504 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
danielk197753c0f742005-03-29 03:10:59 +0000505 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000506 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000507 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
drh21206082011-04-04 18:22:02 +0000508 assert( sqlite3SchemaMutexHeld(db, j, 0) );
drhe4df0e72006-03-29 00:24:06 +0000509 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
drhd24cc422003-03-27 12:51:24 +0000510 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000511 }
drhd24cc422003-03-27 12:51:24 +0000512 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000513 if( !noErr ){
514 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
dan57966752011-04-09 17:32:58 +0000515 }else{
516 sqlite3CodeVerifyNamedSchema(pParse, zDb);
drhfdd48a72006-09-11 23:45:48 +0000517 }
dan1db95102010-06-28 10:15:19 +0000518 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +0000519 goto drop_trigger_cleanup;
520 }
drh74161702006-02-24 02:53:49 +0000521 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000522
523drop_trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000524 sqlite3SrcListDelete(db, pName);
drh79a519c2003-05-17 19:04:03 +0000525}
526
527/*
drh956bc922004-07-24 17:38:29 +0000528** Return a pointer to the Table structure for the table that a trigger
529** is set on.
530*/
drh74161702006-02-24 02:53:49 +0000531static Table *tableOfTrigger(Trigger *pTrigger){
drha83ccca2009-04-28 13:01:09 +0000532 int n = sqlite3Strlen30(pTrigger->table);
danielk1977aaf22682006-01-06 15:03:48 +0000533 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
drh956bc922004-07-24 17:38:29 +0000534}
535
536
537/*
drh74161702006-02-24 02:53:49 +0000538** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000539*/
drh74161702006-02-24 02:53:49 +0000540void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000541 Table *pTable;
542 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000543 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000544 int iDb;
drh79a519c2003-05-17 19:04:03 +0000545
danielk1977da184232006-01-05 11:34:32 +0000546 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000547 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000548 pTable = tableOfTrigger(pTrigger);
drh43617e92006-03-06 20:55:46 +0000549 assert( pTable );
danielk1977da184232006-01-05 11:34:32 +0000550 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000551#ifndef SQLITE_OMIT_AUTHORIZATION
552 {
553 int code = SQLITE_DROP_TRIGGER;
drh956bc922004-07-24 17:38:29 +0000554 const char *zDb = db->aDb[iDb].zName;
555 const char *zTab = SCHEMA_TABLE(iDb);
556 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
dan2bd93512009-08-31 08:22:46 +0000557 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
danielk19774adee202004-05-08 08:23:19 +0000558 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000559 return;
drhe5f9c642003-01-13 23:27:31 +0000560 }
drhed6c8672003-01-12 18:02:16 +0000561 }
drhe5f9c642003-01-13 23:27:31 +0000562#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000563
drhf0f258b2003-04-21 18:48:45 +0000564 /* Generate code to destroy the database record of the trigger.
565 */
drh43617e92006-03-06 20:55:46 +0000566 assert( pTable!=0 );
567 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drhf0f258b2003-04-21 18:48:45 +0000568 int base;
drh688852a2014-02-17 22:40:43 +0000569 static const int iLn = __LINE__+2;
drh57196282004-10-06 15:41:16 +0000570 static const VdbeOpList dropTrigger[] = {
drh9b1b01b2003-08-16 12:37:51 +0000571 { OP_Rewind, 0, ADDR(9), 0},
drh1db639c2008-01-17 02:36:28 +0000572 { OP_String8, 0, 1, 0}, /* 1 */
573 { OP_Column, 0, 1, 2},
574 { OP_Ne, 2, ADDR(8), 1},
575 { OP_String8, 0, 1, 0}, /* 4: "trigger" */
576 { OP_Column, 0, 0, 2},
577 { OP_Ne, 2, ADDR(8), 1},
drhf0f258b2003-04-21 18:48:45 +0000578 { OP_Delete, 0, 0, 0},
drh9b1b01b2003-08-16 12:37:51 +0000579 { OP_Next, 0, ADDR(1), 0}, /* 8 */
drhf0f258b2003-04-21 18:48:45 +0000580 };
581
drh956bc922004-07-24 17:38:29 +0000582 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk1977c00da102006-01-07 13:21:04 +0000583 sqlite3OpenMasterTable(pParse, iDb);
drh688852a2014-02-17 22:40:43 +0000584 base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger, iLn);
drh8d129422011-04-05 12:25:19 +0000585 sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, P4_TRANSIENT);
drh24003452008-01-03 01:28:59 +0000586 sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
drh9cbf3422008-01-17 16:22:13 +0000587 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +0000588 sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
dan165921a2009-08-28 18:53:45 +0000589 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
danielk19776ab3a2e2009-02-19 14:39:25 +0000590 if( pParse->nMem<3 ){
591 pParse->nMem = 3;
592 }
drhf0f258b2003-04-21 18:48:45 +0000593 }
drh956bc922004-07-24 17:38:29 +0000594}
drhf0f258b2003-04-21 18:48:45 +0000595
drh956bc922004-07-24 17:38:29 +0000596/*
597** Remove a trigger from the hash tables of the sqlite* pointer.
598*/
599void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
600 Trigger *pTrigger;
drh21206082011-04-04 18:22:02 +0000601 Hash *pHash;
602
603 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
604 pHash = &(db->aDb[iDb].pSchema->trigHash);
danielk19772f886d12009-02-28 10:47:41 +0000605 pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0);
drh9ab4c2e2009-05-09 00:18:38 +0000606 if( ALWAYS(pTrigger) ){
danielk19772f886d12009-02-28 10:47:41 +0000607 if( pTrigger->pSchema==pTrigger->pTabSchema ){
608 Table *pTab = tableOfTrigger(pTrigger);
609 Trigger **pp;
610 for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
611 *pp = (*pp)->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +0000612 }
drh633e6d52008-07-28 19:34:53 +0000613 sqlite3DeleteTrigger(db, pTrigger);
drh956bc922004-07-24 17:38:29 +0000614 db->flags |= SQLITE_InternChanges;
danielk1977c3f9bad2002-05-15 08:30:12 +0000615 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000616}
617
drhc977f7f2002-05-21 11:38:11 +0000618/*
619** pEList is the SET clause of an UPDATE statement. Each entry
620** in pEList is of the format <id>=<expr>. If any of the entries
621** in pEList have an <id> which matches an identifier in pIdList,
622** then return TRUE. If pIdList==NULL, then it is considered a
623** wildcard that matches anything. Likewise if pEList==NULL then
624** it matches anything so always return true. Return false only
625** if there is no match.
626*/
drh9ab4c2e2009-05-09 00:18:38 +0000627static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000628 int e;
drh9ab4c2e2009-05-09 00:18:38 +0000629 if( pIdList==0 || NEVER(pEList==0) ) return 1;
drhad2d8302002-05-24 20:31:36 +0000630 for(e=0; e<pEList->nExpr; e++){
danielk19774adee202004-05-08 08:23:19 +0000631 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000632 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000633 return 0;
634}
635
danielk1977c3f9bad2002-05-15 08:30:12 +0000636/*
danielk19772f886d12009-02-28 10:47:41 +0000637** Return a list of all triggers on table pTab if there exists at least
638** one trigger that must be fired when an operation of type 'op' is
639** performed on the table, and, if that operation is an UPDATE, if at
640** least one of the columns in pChanges is being modified.
drhdca76842004-12-07 14:06:13 +0000641*/
danielk19772f886d12009-02-28 10:47:41 +0000642Trigger *sqlite3TriggersExist(
643 Parse *pParse, /* Parse context */
drhdca76842004-12-07 14:06:13 +0000644 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000645 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
danielk19772f886d12009-02-28 10:47:41 +0000646 ExprList *pChanges, /* Columns that change in an UPDATE statement */
647 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
drhc977f7f2002-05-21 11:38:11 +0000648){
drhdca76842004-12-07 14:06:13 +0000649 int mask = 0;
drhe83cafd2011-03-21 17:15:58 +0000650 Trigger *pList = 0;
danielk19772f886d12009-02-28 10:47:41 +0000651 Trigger *p;
drhe83cafd2011-03-21 17:15:58 +0000652
653 if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
654 pList = sqlite3TriggerList(pParse, pTab);
655 }
danielk19772f886d12009-02-28 10:47:41 +0000656 assert( pList==0 || IsVirtual(pTab)==0 );
657 for(p=pList; p; p=p->pNext){
drh9ab4c2e2009-05-09 00:18:38 +0000658 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
danielk19772f886d12009-02-28 10:47:41 +0000659 mask |= p->tr_tm;
danielk1977c3f9bad2002-05-15 08:30:12 +0000660 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000661 }
danielk19772f886d12009-02-28 10:47:41 +0000662 if( pMask ){
663 *pMask = mask;
664 }
665 return (mask ? pList : 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000666}
667
drhc977f7f2002-05-21 11:38:11 +0000668/*
drhf26e09c2003-05-31 16:21:12 +0000669** Convert the pStep->target token into a SrcList and return a pointer
670** to that SrcList.
671**
672** This routine adds a specific database name, if needed, to the target when
673** forming the SrcList. This prevents a trigger in one database from
674** referring to a target in another database. An exception is when the
675** trigger is in TEMP in which case it can refer to any other database it
676** wants.
677*/
678static SrcList *targetSrcList(
679 Parse *pParse, /* The parsing context */
680 TriggerStep *pStep /* The trigger containing the target token */
681){
drhf26e09c2003-05-31 16:21:12 +0000682 int iDb; /* Index of the database to use */
683 SrcList *pSrc; /* SrcList to be returned */
684
drhb7916a72009-05-27 10:31:29 +0000685 pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
686 if( pSrc ){
687 assert( pSrc->nSrc>0 );
688 assert( pSrc->a!=0 );
689 iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
690 if( iDb==0 || iDb>=2 ){
691 sqlite3 *db = pParse->db;
692 assert( iDb<pParse->db->nDb );
693 pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
694 }
drhf26e09c2003-05-31 16:21:12 +0000695 }
696 return pSrc;
697}
698
699/*
dan65a7cd12009-09-01 12:16:01 +0000700** Generate VDBE code for the statements inside the body of a single
701** trigger.
drhc977f7f2002-05-21 11:38:11 +0000702*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000703static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000704 Parse *pParse, /* The parser context */
705 TriggerStep *pStepList, /* List of statements inside the trigger body */
dan65a7cd12009-09-01 12:16:01 +0000706 int orconf /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000707){
dan65a7cd12009-09-01 12:16:01 +0000708 TriggerStep *pStep;
drh344737f2004-09-19 00:50:20 +0000709 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000710 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000711
dan65a7cd12009-09-01 12:16:01 +0000712 assert( pParse->pTriggerTab && pParse->pToplevel );
713 assert( pStepList );
drh344737f2004-09-19 00:50:20 +0000714 assert( v!=0 );
dan65a7cd12009-09-01 12:16:01 +0000715 for(pStep=pStepList; pStep; pStep=pStep->pNext){
dan165921a2009-08-28 18:53:45 +0000716 /* Figure out the ON CONFLICT policy that will be used for this step
717 ** of the trigger program. If the statement that caused this trigger
718 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
719 ** the ON CONFLICT policy that was specified as part of the trigger
720 ** step statement. Example:
721 **
722 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
723 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
724 ** END;
725 **
726 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
727 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
728 */
shanecea72b22009-09-07 04:38:36 +0000729 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
drhaceb31b2014-02-08 01:40:27 +0000730 assert( pParse->okConstFactor==0 );
danf78baaf2012-12-06 19:37:22 +0000731
dan165921a2009-08-28 18:53:45 +0000732 switch( pStep->op ){
danielk1977633ed082002-05-17 00:05:58 +0000733 case TK_UPDATE: {
dan165921a2009-08-28 18:53:45 +0000734 sqlite3Update(pParse,
735 targetSrcList(pParse, pStep),
736 sqlite3ExprListDup(db, pStep->pExprList, 0),
737 sqlite3ExprDup(db, pStep->pWhere, 0),
dan65a7cd12009-09-01 12:16:01 +0000738 pParse->eOrconf
dan165921a2009-08-28 18:53:45 +0000739 );
danielk1977633ed082002-05-17 00:05:58 +0000740 break;
741 }
742 case TK_INSERT: {
dan165921a2009-08-28 18:53:45 +0000743 sqlite3Insert(pParse,
744 targetSrcList(pParse, pStep),
dan165921a2009-08-28 18:53:45 +0000745 sqlite3SelectDup(db, pStep->pSelect, 0),
746 sqlite3IdListDup(db, pStep->pIdList),
dan65a7cd12009-09-01 12:16:01 +0000747 pParse->eOrconf
dan165921a2009-08-28 18:53:45 +0000748 );
danielk1977633ed082002-05-17 00:05:58 +0000749 break;
750 }
751 case TK_DELETE: {
dan165921a2009-08-28 18:53:45 +0000752 sqlite3DeleteFrom(pParse,
753 targetSrcList(pParse, pStep),
754 sqlite3ExprDup(db, pStep->pWhere, 0)
755 );
danielk1977633ed082002-05-17 00:05:58 +0000756 break;
757 }
dan165921a2009-08-28 18:53:45 +0000758 default: assert( pStep->op==TK_SELECT ); {
759 SelectDest sDest;
760 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
761 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
762 sqlite3Select(pParse, pSelect, &sDest);
763 sqlite3SelectDelete(db, pSelect);
drh9ab4c2e2009-05-09 00:18:38 +0000764 break;
765 }
danielk1977633ed082002-05-17 00:05:58 +0000766 }
dan76d462e2009-08-30 11:42:51 +0000767 if( pStep->op!=TK_SELECT ){
drhb7f1d9a2009-09-08 02:27:58 +0000768 sqlite3VdbeAddOp0(v, OP_ResetCount);
dan76d462e2009-08-30 11:42:51 +0000769 }
danielk1977633ed082002-05-17 00:05:58 +0000770 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000771
danielk1977633ed082002-05-17 00:05:58 +0000772 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000773}
774
drhc7379ce2013-10-30 02:28:23 +0000775#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
dan165921a2009-08-28 18:53:45 +0000776/*
777** This function is used to add VdbeComment() annotations to a VDBE
778** program. It is not used in production code, only for debugging.
779*/
780static const char *onErrorText(int onError){
781 switch( onError ){
782 case OE_Abort: return "abort";
783 case OE_Rollback: return "rollback";
784 case OE_Fail: return "fail";
785 case OE_Replace: return "replace";
786 case OE_Ignore: return "ignore";
787 case OE_Default: return "default";
788 }
789 return "n/a";
790}
791#endif
792
793/*
794** Parse context structure pFrom has just been used to create a sub-vdbe
795** (trigger program). If an error has occurred, transfer error information
796** from pFrom to pTo.
797*/
798static void transferParseError(Parse *pTo, Parse *pFrom){
799 assert( pFrom->zErrMsg==0 || pFrom->nErr );
800 assert( pTo->zErrMsg==0 || pTo->nErr );
801 if( pTo->nErr==0 ){
802 pTo->zErrMsg = pFrom->zErrMsg;
803 pTo->nErr = pFrom->nErr;
804 }else{
805 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
806 }
807}
808
dan65a7cd12009-09-01 12:16:01 +0000809/*
810** Create and populate a new TriggerPrg object with a sub-program
811** implementing trigger pTrigger with ON CONFLICT policy orconf.
812*/
dan2832ad42009-08-31 15:27:27 +0000813static TriggerPrg *codeRowTrigger(
dan165921a2009-08-28 18:53:45 +0000814 Parse *pParse, /* Current parse context */
815 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +0000816 Table *pTab, /* The table pTrigger is attached to */
817 int orconf /* ON CONFLICT policy to code trigger program with */
dan165921a2009-08-28 18:53:45 +0000818){
dan65a7cd12009-09-01 12:16:01 +0000819 Parse *pTop = sqlite3ParseToplevel(pParse);
820 sqlite3 *db = pParse->db; /* Database handle */
821 TriggerPrg *pPrg; /* Value to return */
dan165921a2009-08-28 18:53:45 +0000822 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
823 Vdbe *v; /* Temporary VM */
dan165921a2009-08-28 18:53:45 +0000824 NameContext sNC; /* Name context for sub-vdbe */
825 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
826 Parse *pSubParse; /* Parse context for sub-vdbe */
827 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
828
dan1da40a32009-09-19 17:00:31 +0000829 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dand19c9332010-07-26 12:05:17 +0000830 assert( pTop->pVdbe );
dan65a7cd12009-09-01 12:16:01 +0000831
832 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
833 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
834 ** list of the top-level Parse object sooner rather than later. */
dan2832ad42009-08-31 15:27:27 +0000835 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
836 if( !pPrg ) return 0;
dan65a7cd12009-09-01 12:16:01 +0000837 pPrg->pNext = pTop->pTriggerPrg;
838 pTop->pTriggerPrg = pPrg;
dan2832ad42009-08-31 15:27:27 +0000839 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
dan165921a2009-08-28 18:53:45 +0000840 if( !pProgram ) return 0;
dand19c9332010-07-26 12:05:17 +0000841 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
dan2832ad42009-08-31 15:27:27 +0000842 pPrg->pTrigger = pTrigger;
843 pPrg->orconf = orconf;
danbb5f1682009-11-27 12:12:34 +0000844 pPrg->aColmask[0] = 0xffffffff;
845 pPrg->aColmask[1] = 0xffffffff;
dan165921a2009-08-28 18:53:45 +0000846
dan65a7cd12009-09-01 12:16:01 +0000847 /* Allocate and populate a new Parse context to use for coding the
848 ** trigger sub-program. */
849 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
850 if( !pSubParse ) return 0;
dan165921a2009-08-28 18:53:45 +0000851 memset(&sNC, 0, sizeof(sNC));
852 sNC.pParse = pSubParse;
853 pSubParse->db = db;
854 pSubParse->pTriggerTab = pTab;
dan65a7cd12009-09-01 12:16:01 +0000855 pSubParse->pToplevel = pTop;
dan2bd93512009-08-31 08:22:46 +0000856 pSubParse->zAuthContext = pTrigger->zName;
dan65a7cd12009-09-01 12:16:01 +0000857 pSubParse->eTriggerOp = pTrigger->op;
drh8b307fb2010-04-06 15:57:05 +0000858 pSubParse->nQueryLoop = pParse->nQueryLoop;
dan165921a2009-08-28 18:53:45 +0000859
860 v = sqlite3GetVdbe(pSubParse);
861 if( v ){
dan76d462e2009-08-30 11:42:51 +0000862 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
863 pTrigger->zName, onErrorText(orconf),
dan165921a2009-08-28 18:53:45 +0000864 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
dan65a7cd12009-09-01 12:16:01 +0000865 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
866 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
867 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
dan76d462e2009-08-30 11:42:51 +0000868 pTab->zName
dan165921a2009-08-28 18:53:45 +0000869 ));
dan76d462e2009-08-30 11:42:51 +0000870#ifndef SQLITE_OMIT_TRACE
871 sqlite3VdbeChangeP4(v, -1,
872 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
873 );
874#endif
dan165921a2009-08-28 18:53:45 +0000875
dan65a7cd12009-09-01 12:16:01 +0000876 /* If one was specified, code the WHEN clause. If it evaluates to false
877 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
878 ** OP_Halt inserted at the end of the program. */
dan165921a2009-08-28 18:53:45 +0000879 if( pTrigger->pWhen ){
dan165921a2009-08-28 18:53:45 +0000880 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
dan523a0872009-08-31 05:23:32 +0000881 if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
882 && db->mallocFailed==0
883 ){
dan165921a2009-08-28 18:53:45 +0000884 iEndTrigger = sqlite3VdbeMakeLabel(v);
885 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
886 }
887 sqlite3ExprDelete(db, pWhen);
888 }
889
890 /* Code the trigger program into the sub-vdbe. */
dan76d462e2009-08-30 11:42:51 +0000891 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
dan65a7cd12009-09-01 12:16:01 +0000892
893 /* Insert an OP_Halt at the end of the sub-program. */
dan165921a2009-08-28 18:53:45 +0000894 if( iEndTrigger ){
895 sqlite3VdbeResolveLabel(v, iEndTrigger);
896 }
897 sqlite3VdbeAddOp0(v, OP_Halt);
dan76d462e2009-08-30 11:42:51 +0000898 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
899
dan165921a2009-08-28 18:53:45 +0000900 transferParseError(pParse, pSubParse);
dan523a0872009-08-31 05:23:32 +0000901 if( db->mallocFailed==0 ){
dan65a7cd12009-09-01 12:16:01 +0000902 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
dan523a0872009-08-31 05:23:32 +0000903 }
dan165921a2009-08-28 18:53:45 +0000904 pProgram->nMem = pSubParse->nMem;
905 pProgram->nCsr = pSubParse->nTab;
dan1d8cb212011-12-09 13:24:16 +0000906 pProgram->nOnce = pSubParse->nOnce;
dan165921a2009-08-28 18:53:45 +0000907 pProgram->token = (void *)pTrigger;
danbb5f1682009-11-27 12:12:34 +0000908 pPrg->aColmask[0] = pSubParse->oldmask;
909 pPrg->aColmask[1] = pSubParse->newmask;
dan165921a2009-08-28 18:53:45 +0000910 sqlite3VdbeDelete(v);
dan165921a2009-08-28 18:53:45 +0000911 }
dan65a7cd12009-09-01 12:16:01 +0000912
913 assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
914 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
drhf30a9692013-11-15 01:10:18 +0000915 sqlite3ParserReset(pSubParse);
dan165921a2009-08-28 18:53:45 +0000916 sqlite3StackFree(db, pSubParse);
917
dan2832ad42009-08-31 15:27:27 +0000918 return pPrg;
dan165921a2009-08-28 18:53:45 +0000919}
920
dan65a7cd12009-09-01 12:16:01 +0000921/*
922** Return a pointer to a TriggerPrg object containing the sub-program for
923** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
924** TriggerPrg object exists, a new object is allocated and populated before
925** being returned.
926*/
dan2832ad42009-08-31 15:27:27 +0000927static TriggerPrg *getRowTrigger(
dan65a7cd12009-09-01 12:16:01 +0000928 Parse *pParse, /* Current parse context */
dan165921a2009-08-28 18:53:45 +0000929 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +0000930 Table *pTab, /* The table trigger pTrigger is attached to */
931 int orconf /* ON CONFLICT algorithm. */
dan165921a2009-08-28 18:53:45 +0000932){
dan65a7cd12009-09-01 12:16:01 +0000933 Parse *pRoot = sqlite3ParseToplevel(pParse);
dan2832ad42009-08-31 15:27:27 +0000934 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +0000935
dan1da40a32009-09-19 17:00:31 +0000936 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dan165921a2009-08-28 18:53:45 +0000937
938 /* It may be that this trigger has already been coded (or is in the
939 ** process of being coded). If this is the case, then an entry with
dan2832ad42009-08-31 15:27:27 +0000940 ** a matching TriggerPrg.pTrigger field will be present somewhere
941 ** in the Parse.pTriggerPrg list. Search for such an entry. */
dan2832ad42009-08-31 15:27:27 +0000942 for(pPrg=pRoot->pTriggerPrg;
943 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
944 pPrg=pPrg->pNext
dan165921a2009-08-28 18:53:45 +0000945 );
946
dan65a7cd12009-09-01 12:16:01 +0000947 /* If an existing TriggerPrg could not be located, create a new one. */
dan2832ad42009-08-31 15:27:27 +0000948 if( !pPrg ){
dan65a7cd12009-09-01 12:16:01 +0000949 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
dan165921a2009-08-28 18:53:45 +0000950 }
951
dan2832ad42009-08-31 15:27:27 +0000952 return pPrg;
dan165921a2009-08-28 18:53:45 +0000953}
954
dan94d7f502009-09-24 09:05:49 +0000955/*
956** Generate code for the trigger program associated with trigger p on
957** table pTab. The reg, orconf and ignoreJump parameters passed to this
958** function are the same as those described in the header function for
959** sqlite3CodeRowTrigger()
960*/
dan1da40a32009-09-19 17:00:31 +0000961void sqlite3CodeRowTriggerDirect(
962 Parse *pParse, /* Parse context */
963 Trigger *p, /* Trigger to code */
964 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +0000965 int reg, /* Reg array containing OLD.* and NEW.* values */
dan1da40a32009-09-19 17:00:31 +0000966 int orconf, /* ON CONFLICT policy */
967 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
968){
969 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
970 TriggerPrg *pPrg;
971 pPrg = getRowTrigger(pParse, p, pTab, orconf);
972 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
973
974 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
975 ** is a pointer to the sub-vdbe containing the trigger program. */
976 if( pPrg ){
dand19c9332010-07-26 12:05:17 +0000977 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
978
dan94d7f502009-09-24 09:05:49 +0000979 sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem);
dan1da40a32009-09-19 17:00:31 +0000980 sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
981 VdbeComment(
982 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
983
984 /* Set the P5 operand of the OP_Program instruction to non-zero if
985 ** recursive invocation of this trigger program is disallowed. Recursive
986 ** invocation is disallowed if (a) the sub-program is really a trigger,
987 ** not a foreign key action, and (b) the flag to enable recursive triggers
988 ** is clear. */
dand19c9332010-07-26 12:05:17 +0000989 sqlite3VdbeChangeP5(v, (u8)bRecursive);
dan1da40a32009-09-19 17:00:31 +0000990 }
991}
992
danielk1977633ed082002-05-17 00:05:58 +0000993/*
dan94d7f502009-09-24 09:05:49 +0000994** This is called to code the required FOR EACH ROW triggers for an operation
995** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
drhf7b54962013-05-28 12:11:54 +0000996** is given by the op parameter. The tr_tm parameter determines whether the
dan94d7f502009-09-24 09:05:49 +0000997** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
998** parameter pChanges is passed the list of columns being modified.
danielk1977633ed082002-05-17 00:05:58 +0000999**
dan94d7f502009-09-24 09:05:49 +00001000** If there are no triggers that fire at the specified time for the specified
1001** operation on pTab, this function is a no-op.
drhc977f7f2002-05-21 11:38:11 +00001002**
dan94d7f502009-09-24 09:05:49 +00001003** The reg argument is the address of the first in an array of registers
1004** that contain the values substituted for the new.* and old.* references
1005** in the trigger program. If N is the number of columns in table pTab
1006** (a copy of pTab->nCol), then registers are populated as follows:
drhc977f7f2002-05-21 11:38:11 +00001007**
dan94d7f502009-09-24 09:05:49 +00001008** Register Contains
1009** ------------------------------------------------------
1010** reg+0 OLD.rowid
1011** reg+1 OLD.* value of left-most column of pTab
1012** ... ...
1013** reg+N OLD.* value of right-most column of pTab
1014** reg+N+1 NEW.rowid
1015** reg+N+2 OLD.* value of left-most column of pTab
1016** ... ...
1017** reg+N+N+1 NEW.* value of right-most column of pTab
drhc977f7f2002-05-21 11:38:11 +00001018**
dan94d7f502009-09-24 09:05:49 +00001019** For ON DELETE triggers, the registers containing the NEW.* values will
1020** never be accessed by the trigger program, so they are not allocated or
1021** populated by the caller (there is no data to populate them with anyway).
1022** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
1023** are never accessed, and so are not allocated by the caller. So, for an
1024** ON INSERT trigger, the value passed to this function as parameter reg
1025** is not a readable register, although registers (reg+N) through
1026** (reg+N+N+1) are.
danielk1977633ed082002-05-17 00:05:58 +00001027**
dan94d7f502009-09-24 09:05:49 +00001028** Parameter orconf is the default conflict resolution algorithm for the
1029** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
1030** is the instruction that control should jump to if a trigger program
1031** raises an IGNORE exception.
danielk1977633ed082002-05-17 00:05:58 +00001032*/
dan165921a2009-08-28 18:53:45 +00001033void sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +00001034 Parse *pParse, /* Parse context */
danielk19772f886d12009-02-28 10:47:41 +00001035 Trigger *pTrigger, /* List of triggers on table pTab */
danielk1977633ed082002-05-17 00:05:58 +00001036 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
1037 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +00001038 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +00001039 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001040 int reg, /* The first in an array of registers (see above) */
danielk19776f349032002-06-11 02:25:40 +00001041 int orconf, /* ON CONFLICT policy */
dan165921a2009-08-28 18:53:45 +00001042 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
drhc977f7f2002-05-21 11:38:11 +00001043){
dan94d7f502009-09-24 09:05:49 +00001044 Trigger *p; /* Used to iterate through pTrigger list */
danielk19778f2c54e2008-01-01 19:02:09 +00001045
dan94d7f502009-09-24 09:05:49 +00001046 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
1047 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
1048 assert( (op==TK_UPDATE)==(pChanges!=0) );
danielk1977c3f9bad2002-05-15 08:30:12 +00001049
danielk19772f886d12009-02-28 10:47:41 +00001050 for(p=pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +00001051
drh9ab4c2e2009-05-09 00:18:38 +00001052 /* Sanity checking: The schema for the trigger and for the table are
1053 ** always defined. The trigger must be in the same schema as the table
1054 ** or else it must be a TEMP trigger. */
1055 assert( p->pSchema!=0 );
1056 assert( p->pTabSchema!=0 );
dan165921a2009-08-28 18:53:45 +00001057 assert( p->pSchema==p->pTabSchema
1058 || p->pSchema==pParse->db->aDb[1].pSchema );
drh9ab4c2e2009-05-09 00:18:38 +00001059
danielk1977eecfb3e2006-01-10 12:31:39 +00001060 /* Determine whether we should code this trigger */
dan165921a2009-08-28 18:53:45 +00001061 if( p->op==op
1062 && p->tr_tm==tr_tm
dan94d7f502009-09-24 09:05:49 +00001063 && checkColumnOverlap(p->pColumns, pChanges)
danielk1977eecfb3e2006-01-10 12:31:39 +00001064 ){
dan94d7f502009-09-24 09:05:49 +00001065 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
danielk1977c3f9bad2002-05-15 08:30:12 +00001066 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001067 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001068}
dan165921a2009-08-28 18:53:45 +00001069
dan2832ad42009-08-31 15:27:27 +00001070/*
danbb5f1682009-11-27 12:12:34 +00001071** Triggers may access values stored in the old.* or new.* pseudo-table.
1072** This function returns a 32-bit bitmask indicating which columns of the
1073** old.* or new.* tables actually are used by triggers. This information
1074** may be used by the caller, for example, to avoid having to load the entire
1075** old.* record into memory when executing an UPDATE or DELETE command.
dan2832ad42009-08-31 15:27:27 +00001076**
1077** Bit 0 of the returned mask is set if the left-most column of the
danbb5f1682009-11-27 12:12:34 +00001078** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
dan2832ad42009-08-31 15:27:27 +00001079** the second leftmost column value is required, and so on. If there
1080** are more than 32 columns in the table, and at least one of the columns
1081** with an index greater than 32 may be accessed, 0xffffffff is returned.
1082**
danbb5f1682009-11-27 12:12:34 +00001083** It is not possible to determine if the old.rowid or new.rowid column is
1084** accessed by triggers. The caller must always assume that it is.
dan2832ad42009-08-31 15:27:27 +00001085**
danbb5f1682009-11-27 12:12:34 +00001086** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
1087** applies to the old.* table. If 1, the new.* table.
1088**
1089** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
1090** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
1091** included in the returned mask if the TRIGGER_BEFORE bit is set in the
1092** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
1093** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
dan2832ad42009-08-31 15:27:27 +00001094*/
danbb5f1682009-11-27 12:12:34 +00001095u32 sqlite3TriggerColmask(
dan165921a2009-08-28 18:53:45 +00001096 Parse *pParse, /* Parse context */
1097 Trigger *pTrigger, /* List of triggers on table pTab */
dan165921a2009-08-28 18:53:45 +00001098 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
danbb5f1682009-11-27 12:12:34 +00001099 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
1100 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
dan165921a2009-08-28 18:53:45 +00001101 Table *pTab, /* The table to code triggers from */
dan2832ad42009-08-31 15:27:27 +00001102 int orconf /* Default ON CONFLICT policy for trigger steps */
dan165921a2009-08-28 18:53:45 +00001103){
dan1da40a32009-09-19 17:00:31 +00001104 const int op = pChanges ? TK_UPDATE : TK_DELETE;
dan2832ad42009-08-31 15:27:27 +00001105 u32 mask = 0;
dan165921a2009-08-28 18:53:45 +00001106 Trigger *p;
dan165921a2009-08-28 18:53:45 +00001107
danbb5f1682009-11-27 12:12:34 +00001108 assert( isNew==1 || isNew==0 );
dan165921a2009-08-28 18:53:45 +00001109 for(p=pTrigger; p; p=p->pNext){
danbb5f1682009-11-27 12:12:34 +00001110 if( p->op==op && (tr_tm&p->tr_tm)
1111 && checkColumnOverlap(p->pColumns,pChanges)
1112 ){
dan2832ad42009-08-31 15:27:27 +00001113 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +00001114 pPrg = getRowTrigger(pParse, p, pTab, orconf);
dan2832ad42009-08-31 15:27:27 +00001115 if( pPrg ){
danbb5f1682009-11-27 12:12:34 +00001116 mask |= pPrg->aColmask[isNew];
dan165921a2009-08-28 18:53:45 +00001117 }
1118 }
1119 }
dan2832ad42009-08-31 15:27:27 +00001120
1121 return mask;
dan165921a2009-08-28 18:53:45 +00001122}
1123
drhb7f91642004-10-31 02:22:47 +00001124#endif /* !defined(SQLITE_OMIT_TRIGGER) */