blob: a9378fd3a376acdd75f0db910be3684259e8af43 [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);
drh46d2e5c2018-04-12 13:15:43 +000028 sqlite3UpsertDelete(db, pTmp->pUpsert);
dane7877b22020-07-14 19:51:01 +000029 sqlite3SrcListDelete(db, pTmp->pFrom);
drhf259df52017-12-27 20:38:35 +000030 sqlite3DbFree(db, pTmp->zSpan);
drh4b59ab52002-08-24 18:24:51 +000031
drh633e6d52008-07-28 19:34:53 +000032 sqlite3DbFree(db, pTmp);
drh4b59ab52002-08-24 18:24:51 +000033 }
34}
35
36/*
danielk19772f886d12009-02-28 10:47:41 +000037** Given table pTab, return a list of all the triggers attached to
38** the table. The list is connected by Trigger.pNext pointers.
drh9ab4c2e2009-05-09 00:18:38 +000039**
40** All of the triggers on pTab that are in the same database as pTab
41** are already attached to pTab->pTrigger. But there might be additional
42** triggers on pTab in the TEMP schema. This routine prepends all
43** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
44** and returns the combined list.
45**
46** To state it another way: This routine returns a list of all triggers
47** that fire off of pTab. The list will include any TEMP triggers on
48** pTab as well as the triggers lised in pTab->pTrigger.
danielk19772f886d12009-02-28 10:47:41 +000049*/
50Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
drhf54a80f2021-01-29 13:47:36 +000051 Schema *pTmpSchema; /* Schema of the pTab table */
52 Trigger *pList; /* List of triggers to return */
53 HashElem *p; /* Loop variable for TEMP triggers */
danielk19772f886d12009-02-28 10:47:41 +000054
dand66c8302009-09-28 14:49:01 +000055 if( pParse->disableTriggers ){
56 return 0;
57 }
drhf54a80f2021-01-29 13:47:36 +000058 pTmpSchema = pParse->db->aDb[1].pSchema;
59 p = sqliteHashFirst(&pTmpSchema->trigHash);
60 if( p==0 ){
61 return pTab->pTrigger;
62 }
63 pList = pTab->pTrigger;
danielk19772f886d12009-02-28 10:47:41 +000064 if( pTmpSchema!=pTab->pSchema ){
drhf54a80f2021-01-29 13:47:36 +000065 while( p ){
danielk19772f886d12009-02-28 10:47:41 +000066 Trigger *pTrig = (Trigger *)sqliteHashData(p);
67 if( pTrig->pTabSchema==pTab->pSchema
68 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
69 ){
drhf54a80f2021-01-29 13:47:36 +000070 pTrig->pNext = pList;
danielk19772f886d12009-02-28 10:47:41 +000071 pList = pTrig;
72 }
drhf54a80f2021-01-29 13:47:36 +000073 p = sqliteHashNext(p);
danielk19772f886d12009-02-28 10:47:41 +000074 }
75 }
drhf54a80f2021-01-29 13:47:36 +000076 return pList;
danielk19772f886d12009-02-28 10:47:41 +000077}
78
79/*
drhf0f258b2003-04-21 18:48:45 +000080** This is called by the parser when it sees a CREATE TRIGGER statement
81** up to the point of the BEGIN before the trigger actions. A Trigger
82** structure is generated based on the information available and stored
83** in pParse->pNewTrigger. After the trigger actions have been parsed, the
danielk19774adee202004-05-08 08:23:19 +000084** sqlite3FinishTrigger() function is called to complete the trigger
drhf0f258b2003-04-21 18:48:45 +000085** construction process.
drh9adf9ac2002-05-15 11:44:13 +000086*/
danielk19774adee202004-05-08 08:23:19 +000087void sqlite3BeginTrigger(
drh9adf9ac2002-05-15 11:44:13 +000088 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
danielk1977ef2cb632004-05-29 02:37:19 +000089 Token *pName1, /* The name of the trigger */
90 Token *pName2, /* The name of the trigger */
drh5cf590c2003-04-24 01:45:04 +000091 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
drh9adf9ac2002-05-15 11:44:13 +000092 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
danielk1977633ed082002-05-17 00:05:58 +000093 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
drhd24cc422003-03-27 12:51:24 +000094 SrcList *pTableName,/* The name of the table/view the trigger applies to */
drh9adf9ac2002-05-15 11:44:13 +000095 Expr *pWhen, /* WHEN clause */
drhfdd48a72006-09-11 23:45:48 +000096 int isTemp, /* True if the TEMPORARY keyword is present */
97 int noErr /* Suppress errors if the trigger already exists */
drh9adf9ac2002-05-15 11:44:13 +000098){
drh3d5f74b2009-08-06 17:43:31 +000099 Trigger *pTrigger = 0; /* The new trigger */
100 Table *pTab; /* Table that the trigger fires off of */
drhf0f258b2003-04-21 18:48:45 +0000101 char *zName = 0; /* Name of the trigger */
drh3d5f74b2009-08-06 17:43:31 +0000102 sqlite3 *db = pParse->db; /* The database connection */
danielk1977ef2cb632004-05-29 02:37:19 +0000103 int iDb; /* The database to store the trigger in */
104 Token *pName; /* The unqualified db name */
drh3d5f74b2009-08-06 17:43:31 +0000105 DbFixer sFix; /* State vector for the DB fixer */
drhed6c8672003-01-12 18:02:16 +0000106
drh43617e92006-03-06 20:55:46 +0000107 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
108 assert( pName2!=0 );
drhaa78bec2008-12-09 03:55:14 +0000109 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
110 assert( op>0 && op<0xff );
danielk1977ef2cb632004-05-29 02:37:19 +0000111 if( isTemp ){
112 /* If TEMP was specified, then the trigger name may not be qualified. */
drh43617e92006-03-06 20:55:46 +0000113 if( pName2->n>0 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000114 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
115 goto trigger_cleanup;
116 }
117 iDb = 1;
118 pName = pName1;
119 }else{
mistachkind5578432012-08-25 10:01:29 +0000120 /* Figure out the db that the trigger will be created in */
danielk1977ef2cb632004-05-29 02:37:19 +0000121 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
122 if( iDb<0 ){
123 goto trigger_cleanup;
124 }
125 }
drh6fea83e2011-07-01 13:50:44 +0000126 if( !pTableName || db->mallocFailed ){
127 goto trigger_cleanup;
128 }
129
130 /* A long-standing parser bug is that this syntax was allowed:
131 **
132 ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
133 ** ^^^^^^^^
134 **
135 ** To maintain backwards compatibility, ignore the database
drh346a70c2020-06-15 20:27:35 +0000136 ** name on pTableName if we are reparsing out of the schema table
drh6fea83e2011-07-01 13:50:44 +0000137 */
138 if( db->init.busy && iDb!=1 ){
139 sqlite3DbFree(db, pTableName->a[0].zDatabase);
140 pTableName->a[0].zDatabase = 0;
141 }
danielk1977ef2cb632004-05-29 02:37:19 +0000142
143 /* If the trigger name was unqualified, and the table is a temp table,
144 ** then set iDb to 1 to create the trigger in the temporary database.
145 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
146 ** exist, the error is caught by the block below.
drh9adf9ac2002-05-15 11:44:13 +0000147 */
danielk1977ef2cb632004-05-29 02:37:19 +0000148 pTab = sqlite3SrcListLookup(pParse, pTableName);
drh622d2882010-02-15 16:54:55 +0000149 if( db->init.busy==0 && pName2->n==0 && pTab
150 && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977ef2cb632004-05-29 02:37:19 +0000151 iDb = 1;
152 }
153
154 /* Ensure the table name matches database name and that the table exists */
drh17435752007-08-16 04:30:38 +0000155 if( db->mallocFailed ) goto trigger_cleanup;
drhd24cc422003-03-27 12:51:24 +0000156 assert( pTableName->nSrc==1 );
drhd100f692013-10-03 15:39:44 +0000157 sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
158 if( sqlite3FixSrcList(&sFix, pTableName) ){
drh4312db52003-06-03 01:47:11 +0000159 goto trigger_cleanup;
drhf26e09c2003-05-31 16:21:12 +0000160 }
danielk1977ef2cb632004-05-29 02:37:19 +0000161 pTab = sqlite3SrcListLookup(pParse, pTableName);
162 if( !pTab ){
163 /* The table does not exist. */
drh4e451aa2020-11-05 19:13:44 +0000164 goto trigger_orphan_error;
drhd24cc422003-03-27 12:51:24 +0000165 }
drh4cbdda92006-06-14 19:00:20 +0000166 if( IsVirtual(pTab) ){
167 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
drh4e451aa2020-11-05 19:13:44 +0000168 goto trigger_orphan_error;
drh4cbdda92006-06-14 19:00:20 +0000169 }
drhd24cc422003-03-27 12:51:24 +0000170
danielk1977d8123362004-06-12 09:25:12 +0000171 /* Check that the trigger name is not reserved and that no trigger of the
172 ** specified name exists */
drh17435752007-08-16 04:30:38 +0000173 zName = sqlite3NameFromToken(db, pName);
drhc5a93d42019-08-12 00:08:07 +0000174 if( zName==0 ){
175 assert( db->mallocFailed );
176 goto trigger_cleanup;
177 }
178 if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
danielk1977d8123362004-06-12 09:25:12 +0000179 goto trigger_cleanup;
180 }
drh21206082011-04-04 18:22:02 +0000181 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danc9461ec2018-08-29 21:00:16 +0000182 if( !IN_RENAME_OBJECT ){
dan5496d6a2018-08-13 17:14:26 +0000183 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
184 if( !noErr ){
185 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
186 }else{
187 assert( !db->init.busy );
188 sqlite3CodeVerifySchema(pParse, iDb);
189 }
190 goto trigger_cleanup;
drhfdd48a72006-09-11 23:45:48 +0000191 }
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 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40 +0000198 }
danielk1977ef2cb632004-05-29 02:37:19 +0000199
200 /* INSTEAD of triggers are only for views and views only support INSTEAD
201 ** of triggers.
202 */
203 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000204 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drhda93d232003-03-31 02:12:46 +0000205 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
drh4e451aa2020-11-05 19:13:44 +0000206 goto trigger_orphan_error;
drhd24cc422003-03-27 12:51:24 +0000207 }
danielk1977ef2cb632004-05-29 02:37:19 +0000208 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000209 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drhda93d232003-03-31 02:12:46 +0000210 " trigger on table: %S", pTableName, 0);
drh4e451aa2020-11-05 19:13:44 +0000211 goto trigger_orphan_error;
drhd24cc422003-03-27 12:51:24 +0000212 }
danielk1977ef2cb632004-05-29 02:37:19 +0000213
drhd24cc422003-03-27 12:51:24 +0000214#ifndef SQLITE_OMIT_AUTHORIZATION
danc9461ec2018-08-29 21:00:16 +0000215 if( !IN_RENAME_OBJECT ){
drha0daa752016-09-16 11:53:10 +0000216 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drhd24cc422003-03-27 12:51:24 +0000217 int code = SQLITE_CREATE_TRIGGER;
drh69c33822016-08-18 14:33:11 +0000218 const char *zDb = db->aDb[iTabDb].zDbSName;
219 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
danielk1977da184232006-01-05 11:34:32 +0000220 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19 +0000221 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24 +0000222 goto trigger_cleanup;
223 }
danielk1977da184232006-01-05 11:34:32 +0000224 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24 +0000225 goto trigger_cleanup;
226 }
227 }
228#endif
danielk1977d702fcc2002-05-26 23:24:40 +0000229
danielk1977ef2cb632004-05-29 02:37:19 +0000230 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04 +0000231 ** cannot appear on views. So we might as well translate every
232 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
233 ** elsewhere.
234 */
danielk1977d702fcc2002-05-26 23:24:40 +0000235 if (tr_tm == TK_INSTEAD){
236 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12 +0000237 }
238
239 /* Build the Trigger object */
drh17435752007-08-16 04:30:38 +0000240 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19 +0000241 if( pTrigger==0 ) goto trigger_cleanup;
dan165921a2009-08-28 18:53:45 +0000242 pTrigger->zName = zName;
drhe5f9c642003-01-13 23:27:31 +0000243 zName = 0;
drh17435752007-08-16 04:30:38 +0000244 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32 +0000245 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48 +0000246 pTrigger->pTabSchema = pTab->pSchema;
drhaa78bec2008-12-09 03:55:14 +0000247 pTrigger->op = (u8)op;
drhdca76842004-12-07 14:06:13 +0000248 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
danc9461ec2018-08-29 21:00:16 +0000249 if( IN_RENAME_OBJECT ){
250 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
dan5496d6a2018-08-13 17:14:26 +0000251 pTrigger->pWhen = pWhen;
252 pWhen = 0;
253 }else{
254 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
255 }
256 pTrigger->pColumns = pColumns;
257 pColumns = 0;
drhf0f258b2003-04-21 18:48:45 +0000258 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19 +0000259 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45 +0000260
261trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000262 sqlite3DbFree(db, zName);
263 sqlite3SrcListDelete(db, pTableName);
264 sqlite3IdListDelete(db, pColumns);
265 sqlite3ExprDelete(db, pWhen);
danielk1977d5d56522005-03-16 12:15:20 +0000266 if( !pParse->pNewTrigger ){
drh633e6d52008-07-28 19:34:53 +0000267 sqlite3DeleteTrigger(db, pTrigger);
danielk1977d5d56522005-03-16 12:15:20 +0000268 }else{
269 assert( pParse->pNewTrigger==pTrigger );
270 }
drh4e451aa2020-11-05 19:13:44 +0000271 return;
272
273trigger_orphan_error:
274 if( db->init.iDb==1 ){
275 /* Ticket #3810.
276 ** Normally, whenever a table is dropped, all associated triggers are
277 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
278 ** and the table is dropped by a different database connection, the
279 ** trigger is not visible to the database connection that does the
280 ** drop so the trigger cannot be dropped. This results in an
281 ** "orphaned trigger" - a trigger whose associated table is missing.
282 **
283 ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df
284 */
285 db->init.orphanTrigger = 1;
286 }
287 goto trigger_cleanup;
drhf0f258b2003-04-21 18:48:45 +0000288}
289
290/*
291** This routine is called after all of the trigger actions have been parsed
292** in order to complete the process of building the trigger.
293*/
danielk19774adee202004-05-08 08:23:19 +0000294void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45 +0000295 Parse *pParse, /* Parser context */
296 TriggerStep *pStepList, /* The triggered program */
297 Token *pAll /* Token that describes the complete CREATE TRIGGER */
298){
drha8c62df2010-02-15 15:47:18 +0000299 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
300 char *zName; /* Name of trigger */
301 sqlite3 *db = pParse->db; /* The database */
302 DbFixer sFix; /* Fixer object */
303 int iDb; /* Database containing the trigger */
304 Token nameToken; /* Trigger name for error reporting */
drhf0f258b2003-04-21 18:48:45 +0000305
drhf0f258b2003-04-21 18:48:45 +0000306 pParse->pNewTrigger = 0;
drh9ab4c2e2009-05-09 00:18:38 +0000307 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
dan165921a2009-08-28 18:53:45 +0000308 zName = pTrig->zName;
danielk1977da184232006-01-05 11:34:32 +0000309 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32 +0000310 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53 +0000311 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32 +0000312 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53 +0000313 pStepList = pStepList->pNext;
314 }
drh40aced52016-01-22 17:48:09 +0000315 sqlite3TokenInit(&nameToken, pTrig->zName);
drhd100f692013-10-03 15:39:44 +0000316 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
317 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
dan46539d72013-10-03 12:29:38 +0000318 || sqlite3FixExpr(&sFix, pTrig->pWhen)
drhd100f692013-10-03 15:39:44 +0000319 ){
drhf26e09c2003-05-31 16:21:12 +0000320 goto triggerfinish_cleanup;
321 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000322
dan5496d6a2018-08-13 17:14:26 +0000323#ifndef SQLITE_OMIT_ALTERTABLE
danc9461ec2018-08-29 21:00:16 +0000324 if( IN_RENAME_OBJECT ){
dan5496d6a2018-08-13 17:14:26 +0000325 assert( !db->init.busy );
326 pParse->pNewTrigger = pTrig;
327 pTrig = 0;
328 }else
329#endif
330
drha8c62df2010-02-15 15:47:18 +0000331 /* if we are not initializing,
drh1e32bed2020-06-19 13:33:53 +0000332 ** build the sqlite_schema entry
drh9adf9ac2002-05-15 11:44:13 +0000333 */
drh1d85d932004-02-14 23:05:52 +0000334 if( !db->init.busy ){
drhc977f7f2002-05-21 11:38:11 +0000335 Vdbe *v;
drh2d401ab2008-01-10 23:50:11 +0000336 char *z;
danielk1977c3f9bad2002-05-15 08:30:12 +0000337
drh1e32bed2020-06-19 13:33:53 +0000338 /* Make an entry in the sqlite_schema table */
danielk19774adee202004-05-08 08:23:19 +0000339 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000340 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000341 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11 +0000342 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
drhd96cc6f2017-06-08 14:35:21 +0000343 testcase( z==0 );
drh2d401ab2008-01-10 23:50:11 +0000344 sqlite3NestedParse(pParse,
drh346a70c2020-06-15 20:27:35 +0000345 "INSERT INTO %Q." DFLT_SCHEMA_TABLE
346 " VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
347 db->aDb[iDb].zDbSName, zName,
drh2d401ab2008-01-10 23:50:11 +0000348 pTrig->table, z);
drh633e6d52008-07-28 19:34:53 +0000349 sqlite3DbFree(db, z);
drh9cbf3422008-01-17 16:22:13 +0000350 sqlite3ChangeCookie(pParse, iDb);
drh5d9c9da2011-06-03 20:11:17 +0000351 sqlite3VdbeAddParseSchemaOp(v, iDb,
352 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
danielk1977c3f9bad2002-05-15 08:30:12 +0000353 }
354
drh956bc922004-07-24 17:38:29 +0000355 if( db->init.busy ){
danielk19772f886d12009-02-28 10:47:41 +0000356 Trigger *pLink = pTrig;
357 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
drh21206082011-04-04 18:22:02 +0000358 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh55f66b32019-07-16 19:44:32 +0000359 assert( pLink!=0 );
drhacbcb7e2014-08-21 20:26:37 +0000360 pTrig = sqlite3HashInsert(pHash, zName, pTrig);
danielk19772f886d12009-02-28 10:47:41 +0000361 if( pTrig ){
drh4a642b62016-02-05 01:55:27 +0000362 sqlite3OomFault(db);
danielk19772f886d12009-02-28 10:47:41 +0000363 }else if( pLink->pSchema==pLink->pTabSchema ){
364 Table *pTab;
drhacbcb7e2014-08-21 20:26:37 +0000365 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
danielk19772f886d12009-02-28 10:47:41 +0000366 assert( pTab!=0 );
367 pLink->pNext = pTab->pTrigger;
368 pTab->pTrigger = pLink;
danielk1977d5d56522005-03-16 12:15:20 +0000369 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000370 }
371
drhf0f258b2003-04-21 18:48:45 +0000372triggerfinish_cleanup:
drh633e6d52008-07-28 19:34:53 +0000373 sqlite3DeleteTrigger(db, pTrig);
danc9461ec2018-08-29 21:00:16 +0000374 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
drh633e6d52008-07-28 19:34:53 +0000375 sqlite3DeleteTriggerStep(db, pStepList);
drh4b59ab52002-08-24 18:24:51 +0000376}
danielk1977c3f9bad2002-05-15 08:30:12 +0000377
drh4b59ab52002-08-24 18:24:51 +0000378/*
drhf259df52017-12-27 20:38:35 +0000379** Duplicate a range of text from an SQL statement, then convert all
380** whitespace characters into ordinary space characters.
381*/
382static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
383 char *z = sqlite3DbSpanDup(db, zStart, zEnd);
384 int i;
385 if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i]) ) z[i] = ' ';
386 return z;
387}
388
389/*
drhc977f7f2002-05-21 11:38:11 +0000390** Turn a SELECT statement (that the pSelect parameter points to) into
391** a trigger step. Return a pointer to a TriggerStep structure.
392**
393** The parser calls this routine when it finds a SELECT statement in
394** body of a TRIGGER.
395*/
drhf259df52017-12-27 20:38:35 +0000396TriggerStep *sqlite3TriggerSelectStep(
397 sqlite3 *db, /* Database connection */
398 Select *pSelect, /* The SELECT statement */
399 const char *zStart, /* Start of SQL text */
400 const char *zEnd /* End of SQL text */
401){
drh17435752007-08-16 04:30:38 +0000402 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000403 if( pTriggerStep==0 ) {
drh633e6d52008-07-28 19:34:53 +0000404 sqlite3SelectDelete(db, pSelect);
danielk19772e588c72005-12-09 14:25:08 +0000405 return 0;
406 }
danielk1977633ed082002-05-17 00:05:58 +0000407 pTriggerStep->op = TK_SELECT;
408 pTriggerStep->pSelect = pSelect;
409 pTriggerStep->orconf = OE_Default;
drhf259df52017-12-27 20:38:35 +0000410 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
drhb7916a72009-05-27 10:31:29 +0000411 return pTriggerStep;
412}
danielk1977c3f9bad2002-05-15 08:30:12 +0000413
drhb7916a72009-05-27 10:31:29 +0000414/*
415** Allocate space to hold a new trigger step. The allocated space
416** holds both the TriggerStep object and the TriggerStep.target.z string.
417**
418** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
419*/
420static TriggerStep *triggerStepAllocate(
danc9461ec2018-08-29 21:00:16 +0000421 Parse *pParse, /* Parser context */
shane5eff7cf2009-08-10 03:57:58 +0000422 u8 op, /* Trigger opcode */
drhf259df52017-12-27 20:38:35 +0000423 Token *pName, /* The target name */
424 const char *zStart, /* Start of SQL text */
425 const char *zEnd /* End of SQL text */
drhb7916a72009-05-27 10:31:29 +0000426){
danc9461ec2018-08-29 21:00:16 +0000427 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000428 TriggerStep *pTriggerStep;
429
dan46408352015-04-21 16:38:49 +0000430 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
drhb7916a72009-05-27 10:31:29 +0000431 if( pTriggerStep ){
432 char *z = (char*)&pTriggerStep[1];
433 memcpy(z, pName->z, pName->n);
dan46408352015-04-21 16:38:49 +0000434 sqlite3Dequote(z);
435 pTriggerStep->zTarget = z;
drhb7916a72009-05-27 10:31:29 +0000436 pTriggerStep->op = op;
drhf259df52017-12-27 20:38:35 +0000437 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
danc9461ec2018-08-29 21:00:16 +0000438 if( IN_RENAME_OBJECT ){
439 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
440 }
drhb7916a72009-05-27 10:31:29 +0000441 }
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** Build a trigger step out of an INSERT statement. Return a pointer
447** to the new trigger step.
448**
449** The parser calls this routine when it sees an INSERT inside the
450** body of a trigger.
451*/
danielk19774adee202004-05-08 08:23:19 +0000452TriggerStep *sqlite3TriggerInsertStep(
dan5be60c52018-08-15 20:28:39 +0000453 Parse *pParse, /* Parser */
drhc977f7f2002-05-21 11:38:11 +0000454 Token *pTableName, /* Name of the table into which we insert */
455 IdList *pColumn, /* List of columns in pTableName to insert into */
drhc977f7f2002-05-21 11:38:11 +0000456 Select *pSelect, /* A SELECT statement that supplies values */
drhf259df52017-12-27 20:38:35 +0000457 u8 orconf, /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
drh46d2e5c2018-04-12 13:15:43 +0000458 Upsert *pUpsert, /* ON CONFLICT clauses for upsert */
drhf259df52017-12-27 20:38:35 +0000459 const char *zStart, /* Start of SQL text */
460 const char *zEnd /* End of SQL text */
danielk1977633ed082002-05-17 00:05:58 +0000461){
dan5be60c52018-08-15 20:28:39 +0000462 sqlite3 *db = pParse->db;
drhf3a65f72007-08-22 20:18:21 +0000463 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000464
drh75593d92014-01-10 20:46:55 +0000465 assert(pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000466
danc9461ec2018-08-29 21:00:16 +0000467 pTriggerStep = triggerStepAllocate(pParse, TK_INSERT, pTableName,zStart,zEnd);
danielk1977d5d56522005-03-16 12:15:20 +0000468 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000469 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000470 pTriggerStep->pSelect = pSelect;
471 pSelect = 0;
472 }else{
473 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
474 }
danielk1977d5d56522005-03-16 12:15:20 +0000475 pTriggerStep->pIdList = pColumn;
drh46d2e5c2018-04-12 13:15:43 +0000476 pTriggerStep->pUpsert = pUpsert;
danielk1977d5d56522005-03-16 12:15:20 +0000477 pTriggerStep->orconf = orconf;
dan9105fd52019-08-19 17:26:32 +0000478 if( pUpsert ){
479 sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
480 }
danielk1977d5d56522005-03-16 12:15:20 +0000481 }else{
drh99160482018-04-18 01:34:39 +0000482 testcase( pColumn );
drh633e6d52008-07-28 19:34:53 +0000483 sqlite3IdListDelete(db, pColumn);
drh99160482018-04-18 01:34:39 +0000484 testcase( pUpsert );
drh46d2e5c2018-04-12 13:15:43 +0000485 sqlite3UpsertDelete(db, pUpsert);
danielk1977d5d56522005-03-16 12:15:20 +0000486 }
drh33e619f2009-05-28 01:00:55 +0000487 sqlite3SelectDelete(db, pSelect);
danielk1977c3f9bad2002-05-15 08:30:12 +0000488
danielk1977633ed082002-05-17 00:05:58 +0000489 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000490}
491
drhc977f7f2002-05-21 11:38:11 +0000492/*
493** Construct a trigger step that implements an UPDATE statement and return
494** a pointer to that trigger step. The parser calls this routine when it
495** sees an UPDATE statement inside the body of a CREATE TRIGGER.
496*/
danielk19774adee202004-05-08 08:23:19 +0000497TriggerStep *sqlite3TriggerUpdateStep(
dan5be60c52018-08-15 20:28:39 +0000498 Parse *pParse, /* Parser */
drhc977f7f2002-05-21 11:38:11 +0000499 Token *pTableName, /* Name of the table to be updated */
dane7877b22020-07-14 19:51:01 +0000500 SrcList *pFrom,
drhc977f7f2002-05-21 11:38:11 +0000501 ExprList *pEList, /* The SET clause: list of column and new values */
502 Expr *pWhere, /* The WHERE clause */
drhf259df52017-12-27 20:38:35 +0000503 u8 orconf, /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
504 const char *zStart, /* Start of SQL text */
505 const char *zEnd /* End of SQL text */
drhc977f7f2002-05-21 11:38:11 +0000506){
dan5be60c52018-08-15 20:28:39 +0000507 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000508 TriggerStep *pTriggerStep;
509
danc9461ec2018-08-29 21:00:16 +0000510 pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE, pTableName,zStart,zEnd);
drh33e619f2009-05-28 01:00:55 +0000511 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000512 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000513 pTriggerStep->pExprList = pEList;
514 pTriggerStep->pWhere = pWhere;
dane7877b22020-07-14 19:51:01 +0000515 pTriggerStep->pFrom = pFrom;
dan5be60c52018-08-15 20:28:39 +0000516 pEList = 0;
517 pWhere = 0;
dane7877b22020-07-14 19:51:01 +0000518 pFrom = 0;
dan5be60c52018-08-15 20:28:39 +0000519 }else{
520 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
521 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
dane7877b22020-07-14 19:51:01 +0000522 pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE);
dan5be60c52018-08-15 20:28:39 +0000523 }
drh33e619f2009-05-28 01:00:55 +0000524 pTriggerStep->orconf = orconf;
drh0e3a6f32007-03-30 20:40:34 +0000525 }
drh33e619f2009-05-28 01:00:55 +0000526 sqlite3ExprListDelete(db, pEList);
527 sqlite3ExprDelete(db, pWhere);
dane7877b22020-07-14 19:51:01 +0000528 sqlite3SrcListDelete(db, pFrom);
danielk1977633ed082002-05-17 00:05:58 +0000529 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000530}
531
drhc977f7f2002-05-21 11:38:11 +0000532/*
533** Construct a trigger step that implements a DELETE statement and return
534** a pointer to that trigger step. The parser calls this routine when it
535** sees a DELETE statement inside the body of a CREATE TRIGGER.
536*/
drh17435752007-08-16 04:30:38 +0000537TriggerStep *sqlite3TriggerDeleteStep(
dan5be60c52018-08-15 20:28:39 +0000538 Parse *pParse, /* Parser */
drh17435752007-08-16 04:30:38 +0000539 Token *pTableName, /* The table from which rows are deleted */
drhf259df52017-12-27 20:38:35 +0000540 Expr *pWhere, /* The WHERE clause */
541 const char *zStart, /* Start of SQL text */
542 const char *zEnd /* End of SQL text */
drh17435752007-08-16 04:30:38 +0000543){
dan5be60c52018-08-15 20:28:39 +0000544 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000545 TriggerStep *pTriggerStep;
546
danc9461ec2018-08-29 21:00:16 +0000547 pTriggerStep = triggerStepAllocate(pParse, TK_DELETE, pTableName,zStart,zEnd);
drh33e619f2009-05-28 01:00:55 +0000548 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000549 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000550 pTriggerStep->pWhere = pWhere;
551 pWhere = 0;
552 }else{
553 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
554 }
drh33e619f2009-05-28 01:00:55 +0000555 pTriggerStep->orconf = OE_Default;
drhb63a53d2007-03-31 01:34:44 +0000556 }
drh33e619f2009-05-28 01:00:55 +0000557 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000558 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000559}
560
danielk1977633ed082002-05-17 00:05:58 +0000561/*
562** Recursively delete a Trigger structure
563*/
drh633e6d52008-07-28 19:34:53 +0000564void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
drhf0f258b2003-04-21 18:48:45 +0000565 if( pTrigger==0 ) return;
drh633e6d52008-07-28 19:34:53 +0000566 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
dan165921a2009-08-28 18:53:45 +0000567 sqlite3DbFree(db, pTrigger->zName);
drh633e6d52008-07-28 19:34:53 +0000568 sqlite3DbFree(db, pTrigger->table);
569 sqlite3ExprDelete(db, pTrigger->pWhen);
570 sqlite3IdListDelete(db, pTrigger->pColumns);
drh633e6d52008-07-28 19:34:53 +0000571 sqlite3DbFree(db, pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000572}
573
574/*
danielk19778a414492004-06-29 08:59:35 +0000575** This function is called to drop a trigger from the database schema.
576**
577** This may be called directly from the parser and therefore identifies
578** the trigger by name. The sqlite3DropTriggerPtr() routine does the
579** same job as this routine except it takes a pointer to the trigger
580** instead of the trigger name.
581**/
drhfdd48a72006-09-11 23:45:48 +0000582void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000583 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000584 int i;
585 const char *zDb;
586 const char *zName;
drh9bb575f2004-09-06 17:24:11 +0000587 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000588
drh17435752007-08-16 04:30:38 +0000589 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000590 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000591 goto drop_trigger_cleanup;
592 }
danielk19778e227872004-06-07 07:52:17 +0000593
drhd24cc422003-03-27 12:51:24 +0000594 assert( pName->nSrc==1 );
595 zDb = pName->a[0].zDatabase;
596 zName = pName->a[0].zName;
drh21206082011-04-04 18:22:02 +0000597 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
danielk197753c0f742005-03-29 03:10:59 +0000598 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000599 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
dan465c2b82020-03-21 15:10:40 +0000600 if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
drh21206082011-04-04 18:22:02 +0000601 assert( sqlite3SchemaMutexHeld(db, j, 0) );
drhacbcb7e2014-08-21 20:26:37 +0000602 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
drhd24cc422003-03-27 12:51:24 +0000603 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000604 }
drhd24cc422003-03-27 12:51:24 +0000605 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000606 if( !noErr ){
607 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
dan57966752011-04-09 17:32:58 +0000608 }else{
609 sqlite3CodeVerifyNamedSchema(pParse, zDb);
drhfdd48a72006-09-11 23:45:48 +0000610 }
dan1db95102010-06-28 10:15:19 +0000611 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +0000612 goto drop_trigger_cleanup;
613 }
drh74161702006-02-24 02:53:49 +0000614 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000615
616drop_trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000617 sqlite3SrcListDelete(db, pName);
drh79a519c2003-05-17 19:04:03 +0000618}
619
620/*
drh956bc922004-07-24 17:38:29 +0000621** Return a pointer to the Table structure for the table that a trigger
622** is set on.
623*/
drh74161702006-02-24 02:53:49 +0000624static Table *tableOfTrigger(Trigger *pTrigger){
drhacbcb7e2014-08-21 20:26:37 +0000625 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
drh956bc922004-07-24 17:38:29 +0000626}
627
628
629/*
drh74161702006-02-24 02:53:49 +0000630** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000631*/
drh74161702006-02-24 02:53:49 +0000632void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000633 Table *pTable;
634 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000635 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000636 int iDb;
drh79a519c2003-05-17 19:04:03 +0000637
danielk1977da184232006-01-05 11:34:32 +0000638 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000639 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000640 pTable = tableOfTrigger(pTrigger);
drh6397a782019-08-27 10:05:45 +0000641 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000642#ifndef SQLITE_OMIT_AUTHORIZATION
drh6397a782019-08-27 10:05:45 +0000643 if( pTable ){
drhe5f9c642003-01-13 23:27:31 +0000644 int code = SQLITE_DROP_TRIGGER;
drh69c33822016-08-18 14:33:11 +0000645 const char *zDb = db->aDb[iDb].zDbSName;
drh956bc922004-07-24 17:38:29 +0000646 const char *zTab = SCHEMA_TABLE(iDb);
647 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
dan2bd93512009-08-31 08:22:46 +0000648 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
danielk19774adee202004-05-08 08:23:19 +0000649 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000650 return;
drhe5f9c642003-01-13 23:27:31 +0000651 }
drhed6c8672003-01-12 18:02:16 +0000652 }
drhe5f9c642003-01-13 23:27:31 +0000653#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000654
drhf0f258b2003-04-21 18:48:45 +0000655 /* Generate code to destroy the database record of the trigger.
656 */
drh43617e92006-03-06 20:55:46 +0000657 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drh8c8dddc2015-12-09 16:26:38 +0000658 sqlite3NestedParse(pParse,
drh346a70c2020-06-15 20:27:35 +0000659 "DELETE FROM %Q." DFLT_SCHEMA_TABLE " WHERE name=%Q AND type='trigger'",
660 db->aDb[iDb].zDbSName, pTrigger->zName
drh8c8dddc2015-12-09 16:26:38 +0000661 );
drh9cbf3422008-01-17 16:22:13 +0000662 sqlite3ChangeCookie(pParse, iDb);
dan165921a2009-08-28 18:53:45 +0000663 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
drhf0f258b2003-04-21 18:48:45 +0000664 }
drh956bc922004-07-24 17:38:29 +0000665}
drhf0f258b2003-04-21 18:48:45 +0000666
drh956bc922004-07-24 17:38:29 +0000667/*
668** Remove a trigger from the hash tables of the sqlite* pointer.
669*/
670void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
671 Trigger *pTrigger;
drh21206082011-04-04 18:22:02 +0000672 Hash *pHash;
673
674 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
675 pHash = &(db->aDb[iDb].pSchema->trigHash);
drhacbcb7e2014-08-21 20:26:37 +0000676 pTrigger = sqlite3HashInsert(pHash, zName, 0);
drh9ab4c2e2009-05-09 00:18:38 +0000677 if( ALWAYS(pTrigger) ){
danielk19772f886d12009-02-28 10:47:41 +0000678 if( pTrigger->pSchema==pTrigger->pTabSchema ){
679 Table *pTab = tableOfTrigger(pTrigger);
drh6397a782019-08-27 10:05:45 +0000680 if( pTab ){
681 Trigger **pp;
dan0232dad2019-12-03 03:34:06 +0000682 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
683 if( *pp==pTrigger ){
684 *pp = (*pp)->pNext;
685 break;
686 }
687 }
drh6397a782019-08-27 10:05:45 +0000688 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000689 }
drh633e6d52008-07-28 19:34:53 +0000690 sqlite3DeleteTrigger(db, pTrigger);
drh8257aa82017-07-26 19:59:13 +0000691 db->mDbFlags |= DBFLAG_SchemaChange;
danielk1977c3f9bad2002-05-15 08:30:12 +0000692 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000693}
694
drhc977f7f2002-05-21 11:38:11 +0000695/*
696** pEList is the SET clause of an UPDATE statement. Each entry
697** in pEList is of the format <id>=<expr>. If any of the entries
698** in pEList have an <id> which matches an identifier in pIdList,
699** then return TRUE. If pIdList==NULL, then it is considered a
700** wildcard that matches anything. Likewise if pEList==NULL then
701** it matches anything so always return true. Return false only
702** if there is no match.
703*/
drh9ab4c2e2009-05-09 00:18:38 +0000704static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000705 int e;
drh9ab4c2e2009-05-09 00:18:38 +0000706 if( pIdList==0 || NEVER(pEList==0) ) return 1;
drhad2d8302002-05-24 20:31:36 +0000707 for(e=0; e<pEList->nExpr; e++){
drh41cee662019-12-12 20:22:34 +0000708 if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000709 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000710 return 0;
711}
712
danielk1977c3f9bad2002-05-15 08:30:12 +0000713/*
danielk19772f886d12009-02-28 10:47:41 +0000714** Return a list of all triggers on table pTab if there exists at least
715** one trigger that must be fired when an operation of type 'op' is
716** performed on the table, and, if that operation is an UPDATE, if at
717** least one of the columns in pChanges is being modified.
drhdca76842004-12-07 14:06:13 +0000718*/
danielk19772f886d12009-02-28 10:47:41 +0000719Trigger *sqlite3TriggersExist(
720 Parse *pParse, /* Parse context */
drhdca76842004-12-07 14:06:13 +0000721 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000722 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
danielk19772f886d12009-02-28 10:47:41 +0000723 ExprList *pChanges, /* Columns that change in an UPDATE statement */
724 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
drhc977f7f2002-05-21 11:38:11 +0000725){
drhdca76842004-12-07 14:06:13 +0000726 int mask = 0;
drhe83cafd2011-03-21 17:15:58 +0000727 Trigger *pList = 0;
danielk19772f886d12009-02-28 10:47:41 +0000728 Trigger *p;
drhe83cafd2011-03-21 17:15:58 +0000729
730 if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
731 pList = sqlite3TriggerList(pParse, pTab);
732 }
danielk19772f886d12009-02-28 10:47:41 +0000733 assert( pList==0 || IsVirtual(pTab)==0 );
734 for(p=pList; p; p=p->pNext){
drh9ab4c2e2009-05-09 00:18:38 +0000735 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
danielk19772f886d12009-02-28 10:47:41 +0000736 mask |= p->tr_tm;
danielk1977c3f9bad2002-05-15 08:30:12 +0000737 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000738 }
danielk19772f886d12009-02-28 10:47:41 +0000739 if( pMask ){
740 *pMask = mask;
741 }
742 return (mask ? pList : 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000743}
744
drhc977f7f2002-05-21 11:38:11 +0000745/*
dan46408352015-04-21 16:38:49 +0000746** Convert the pStep->zTarget string into a SrcList and return a pointer
drhf26e09c2003-05-31 16:21:12 +0000747** to that SrcList.
748**
749** This routine adds a specific database name, if needed, to the target when
750** forming the SrcList. This prevents a trigger in one database from
751** referring to a target in another database. An exception is when the
752** trigger is in TEMP in which case it can refer to any other database it
753** wants.
754*/
dane7877b22020-07-14 19:51:01 +0000755SrcList *sqlite3TriggerStepSrc(
drhf26e09c2003-05-31 16:21:12 +0000756 Parse *pParse, /* The parsing context */
757 TriggerStep *pStep /* The trigger containing the target token */
758){
dan46408352015-04-21 16:38:49 +0000759 sqlite3 *db = pParse->db;
dane7877b22020-07-14 19:51:01 +0000760 SrcList *pSrc; /* SrcList to be returned */
761 char *zName = sqlite3DbStrDup(db, pStep->zTarget);
drh29c992c2019-01-17 15:40:41 +0000762 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
dane7877b22020-07-14 19:51:01 +0000763 assert( pSrc==0 || pSrc->nSrc==1 );
764 assert( zName || pSrc==0 );
drhb7916a72009-05-27 10:31:29 +0000765 if( pSrc ){
dane7877b22020-07-14 19:51:01 +0000766 Schema *pSchema = pStep->pTrig->pSchema;
767 pSrc->a[0].zName = zName;
768 if( pSchema!=db->aDb[1].pSchema ){
769 pSrc->a[0].pSchema = pSchema;
drhb7916a72009-05-27 10:31:29 +0000770 }
dane7877b22020-07-14 19:51:01 +0000771 if( pStep->pFrom ){
772 SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0);
773 pSrc = sqlite3SrcListAppendList(pParse, pSrc, pDup);
774 }
775 }else{
776 sqlite3DbFree(db, zName);
drhf26e09c2003-05-31 16:21:12 +0000777 }
778 return pSrc;
779}
780
781/*
dan65a7cd12009-09-01 12:16:01 +0000782** Generate VDBE code for the statements inside the body of a single
783** trigger.
drhc977f7f2002-05-21 11:38:11 +0000784*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000785static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000786 Parse *pParse, /* The parser context */
787 TriggerStep *pStepList, /* List of statements inside the trigger body */
dan65a7cd12009-09-01 12:16:01 +0000788 int orconf /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000789){
dan65a7cd12009-09-01 12:16:01 +0000790 TriggerStep *pStep;
drh344737f2004-09-19 00:50:20 +0000791 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000792 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000793
dan65a7cd12009-09-01 12:16:01 +0000794 assert( pParse->pTriggerTab && pParse->pToplevel );
795 assert( pStepList );
drh344737f2004-09-19 00:50:20 +0000796 assert( v!=0 );
dan65a7cd12009-09-01 12:16:01 +0000797 for(pStep=pStepList; pStep; pStep=pStep->pNext){
dan165921a2009-08-28 18:53:45 +0000798 /* Figure out the ON CONFLICT policy that will be used for this step
799 ** of the trigger program. If the statement that caused this trigger
800 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
801 ** the ON CONFLICT policy that was specified as part of the trigger
802 ** step statement. Example:
803 **
804 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
805 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
806 ** END;
807 **
808 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
809 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
810 */
shanecea72b22009-09-07 04:38:36 +0000811 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
drhaceb31b2014-02-08 01:40:27 +0000812 assert( pParse->okConstFactor==0 );
danf78baaf2012-12-06 19:37:22 +0000813
drhf259df52017-12-27 20:38:35 +0000814#ifndef SQLITE_OMIT_TRACE
drhe307e112017-12-27 21:30:34 +0000815 if( pStep->zSpan ){
816 sqlite3VdbeAddOp4(v, OP_Trace, 0x7fffffff, 1, 0,
817 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
818 P4_DYNAMIC);
819 }
drhf259df52017-12-27 20:38:35 +0000820#endif
821
dan165921a2009-08-28 18:53:45 +0000822 switch( pStep->op ){
danielk1977633ed082002-05-17 00:05:58 +0000823 case TK_UPDATE: {
dan165921a2009-08-28 18:53:45 +0000824 sqlite3Update(pParse,
dane7877b22020-07-14 19:51:01 +0000825 sqlite3TriggerStepSrc(pParse, pStep),
dan165921a2009-08-28 18:53:45 +0000826 sqlite3ExprListDup(db, pStep->pExprList, 0),
827 sqlite3ExprDup(db, pStep->pWhere, 0),
drheac9fab2018-04-16 13:00:50 +0000828 pParse->eOrconf, 0, 0, 0
dan165921a2009-08-28 18:53:45 +0000829 );
danielk1977633ed082002-05-17 00:05:58 +0000830 break;
831 }
832 case TK_INSERT: {
dan165921a2009-08-28 18:53:45 +0000833 sqlite3Insert(pParse,
dane7877b22020-07-14 19:51:01 +0000834 sqlite3TriggerStepSrc(pParse, pStep),
dan165921a2009-08-28 18:53:45 +0000835 sqlite3SelectDup(db, pStep->pSelect, 0),
836 sqlite3IdListDup(db, pStep->pIdList),
drh46d2e5c2018-04-12 13:15:43 +0000837 pParse->eOrconf,
838 sqlite3UpsertDup(db, pStep->pUpsert)
dan165921a2009-08-28 18:53:45 +0000839 );
danielk1977633ed082002-05-17 00:05:58 +0000840 break;
841 }
842 case TK_DELETE: {
dan165921a2009-08-28 18:53:45 +0000843 sqlite3DeleteFrom(pParse,
dane7877b22020-07-14 19:51:01 +0000844 sqlite3TriggerStepSrc(pParse, pStep),
drh8c0833f2017-11-14 23:48:23 +0000845 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
dan165921a2009-08-28 18:53:45 +0000846 );
danielk1977633ed082002-05-17 00:05:58 +0000847 break;
848 }
dan165921a2009-08-28 18:53:45 +0000849 default: assert( pStep->op==TK_SELECT ); {
850 SelectDest sDest;
851 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
852 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
853 sqlite3Select(pParse, pSelect, &sDest);
854 sqlite3SelectDelete(db, pSelect);
drh9ab4c2e2009-05-09 00:18:38 +0000855 break;
856 }
danielk1977633ed082002-05-17 00:05:58 +0000857 }
dan76d462e2009-08-30 11:42:51 +0000858 if( pStep->op!=TK_SELECT ){
drhb7f1d9a2009-09-08 02:27:58 +0000859 sqlite3VdbeAddOp0(v, OP_ResetCount);
dan76d462e2009-08-30 11:42:51 +0000860 }
danielk1977633ed082002-05-17 00:05:58 +0000861 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000862
danielk1977633ed082002-05-17 00:05:58 +0000863 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000864}
865
drhc7379ce2013-10-30 02:28:23 +0000866#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
dan165921a2009-08-28 18:53:45 +0000867/*
868** This function is used to add VdbeComment() annotations to a VDBE
869** program. It is not used in production code, only for debugging.
870*/
871static const char *onErrorText(int onError){
872 switch( onError ){
873 case OE_Abort: return "abort";
874 case OE_Rollback: return "rollback";
875 case OE_Fail: return "fail";
876 case OE_Replace: return "replace";
877 case OE_Ignore: return "ignore";
878 case OE_Default: return "default";
879 }
880 return "n/a";
881}
882#endif
883
884/*
885** Parse context structure pFrom has just been used to create a sub-vdbe
886** (trigger program). If an error has occurred, transfer error information
887** from pFrom to pTo.
888*/
889static void transferParseError(Parse *pTo, Parse *pFrom){
890 assert( pFrom->zErrMsg==0 || pFrom->nErr );
891 assert( pTo->zErrMsg==0 || pTo->nErr );
892 if( pTo->nErr==0 ){
893 pTo->zErrMsg = pFrom->zErrMsg;
894 pTo->nErr = pFrom->nErr;
drhe06874e2015-04-16 15:47:06 +0000895 pTo->rc = pFrom->rc;
dan165921a2009-08-28 18:53:45 +0000896 }else{
897 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
898 }
899}
900
dan65a7cd12009-09-01 12:16:01 +0000901/*
902** Create and populate a new TriggerPrg object with a sub-program
903** implementing trigger pTrigger with ON CONFLICT policy orconf.
904*/
dan2832ad42009-08-31 15:27:27 +0000905static TriggerPrg *codeRowTrigger(
dan165921a2009-08-28 18:53:45 +0000906 Parse *pParse, /* Current parse context */
907 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +0000908 Table *pTab, /* The table pTrigger is attached to */
909 int orconf /* ON CONFLICT policy to code trigger program with */
dan165921a2009-08-28 18:53:45 +0000910){
dan65a7cd12009-09-01 12:16:01 +0000911 Parse *pTop = sqlite3ParseToplevel(pParse);
912 sqlite3 *db = pParse->db; /* Database handle */
913 TriggerPrg *pPrg; /* Value to return */
dan165921a2009-08-28 18:53:45 +0000914 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
915 Vdbe *v; /* Temporary VM */
dan165921a2009-08-28 18:53:45 +0000916 NameContext sNC; /* Name context for sub-vdbe */
917 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
918 Parse *pSubParse; /* Parse context for sub-vdbe */
919 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
920
dan1da40a32009-09-19 17:00:31 +0000921 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dand19c9332010-07-26 12:05:17 +0000922 assert( pTop->pVdbe );
dan65a7cd12009-09-01 12:16:01 +0000923
924 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
925 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
926 ** list of the top-level Parse object sooner rather than later. */
dan2832ad42009-08-31 15:27:27 +0000927 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
928 if( !pPrg ) return 0;
dan65a7cd12009-09-01 12:16:01 +0000929 pPrg->pNext = pTop->pTriggerPrg;
930 pTop->pTriggerPrg = pPrg;
dan2832ad42009-08-31 15:27:27 +0000931 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
dan165921a2009-08-28 18:53:45 +0000932 if( !pProgram ) return 0;
dand19c9332010-07-26 12:05:17 +0000933 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
dan2832ad42009-08-31 15:27:27 +0000934 pPrg->pTrigger = pTrigger;
935 pPrg->orconf = orconf;
danbb5f1682009-11-27 12:12:34 +0000936 pPrg->aColmask[0] = 0xffffffff;
937 pPrg->aColmask[1] = 0xffffffff;
dan165921a2009-08-28 18:53:45 +0000938
dan65a7cd12009-09-01 12:16:01 +0000939 /* Allocate and populate a new Parse context to use for coding the
940 ** trigger sub-program. */
941 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
942 if( !pSubParse ) return 0;
dan165921a2009-08-28 18:53:45 +0000943 memset(&sNC, 0, sizeof(sNC));
944 sNC.pParse = pSubParse;
945 pSubParse->db = db;
946 pSubParse->pTriggerTab = pTab;
dan65a7cd12009-09-01 12:16:01 +0000947 pSubParse->pToplevel = pTop;
dan2bd93512009-08-31 08:22:46 +0000948 pSubParse->zAuthContext = pTrigger->zName;
dan65a7cd12009-09-01 12:16:01 +0000949 pSubParse->eTriggerOp = pTrigger->op;
drh8b307fb2010-04-06 15:57:05 +0000950 pSubParse->nQueryLoop = pParse->nQueryLoop;
dan1ea04432018-12-21 19:29:11 +0000951 pSubParse->disableVtab = pParse->disableVtab;
dan165921a2009-08-28 18:53:45 +0000952
953 v = sqlite3GetVdbe(pSubParse);
954 if( v ){
dan76d462e2009-08-30 11:42:51 +0000955 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
956 pTrigger->zName, onErrorText(orconf),
dan165921a2009-08-28 18:53:45 +0000957 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
dan65a7cd12009-09-01 12:16:01 +0000958 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
959 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
960 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
dan76d462e2009-08-30 11:42:51 +0000961 pTab->zName
dan165921a2009-08-28 18:53:45 +0000962 ));
dan76d462e2009-08-30 11:42:51 +0000963#ifndef SQLITE_OMIT_TRACE
drhe307e112017-12-27 21:30:34 +0000964 if( pTrigger->zName ){
965 sqlite3VdbeChangeP4(v, -1,
966 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
967 );
968 }
dan76d462e2009-08-30 11:42:51 +0000969#endif
dan165921a2009-08-28 18:53:45 +0000970
dan65a7cd12009-09-01 12:16:01 +0000971 /* If one was specified, code the WHEN clause. If it evaluates to false
972 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
973 ** OP_Halt inserted at the end of the program. */
dan165921a2009-08-28 18:53:45 +0000974 if( pTrigger->pWhen ){
dan165921a2009-08-28 18:53:45 +0000975 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
dan523a0872009-08-31 05:23:32 +0000976 if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
977 && db->mallocFailed==0
978 ){
drhec4ccdb2018-12-29 02:26:59 +0000979 iEndTrigger = sqlite3VdbeMakeLabel(pSubParse);
dan165921a2009-08-28 18:53:45 +0000980 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
981 }
982 sqlite3ExprDelete(db, pWhen);
983 }
984
985 /* Code the trigger program into the sub-vdbe. */
dan76d462e2009-08-30 11:42:51 +0000986 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
dan65a7cd12009-09-01 12:16:01 +0000987
988 /* Insert an OP_Halt at the end of the sub-program. */
dan165921a2009-08-28 18:53:45 +0000989 if( iEndTrigger ){
990 sqlite3VdbeResolveLabel(v, iEndTrigger);
991 }
992 sqlite3VdbeAddOp0(v, OP_Halt);
dan76d462e2009-08-30 11:42:51 +0000993 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
994
dan165921a2009-08-28 18:53:45 +0000995 transferParseError(pParse, pSubParse);
dan08951172017-11-28 20:43:40 +0000996 if( db->mallocFailed==0 && pParse->nErr==0 ){
dan65a7cd12009-09-01 12:16:01 +0000997 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
dan523a0872009-08-31 05:23:32 +0000998 }
dan165921a2009-08-28 18:53:45 +0000999 pProgram->nMem = pSubParse->nMem;
1000 pProgram->nCsr = pSubParse->nTab;
1001 pProgram->token = (void *)pTrigger;
danbb5f1682009-11-27 12:12:34 +00001002 pPrg->aColmask[0] = pSubParse->oldmask;
1003 pPrg->aColmask[1] = pSubParse->newmask;
dan165921a2009-08-28 18:53:45 +00001004 sqlite3VdbeDelete(v);
dan165921a2009-08-28 18:53:45 +00001005 }
dan65a7cd12009-09-01 12:16:01 +00001006
dan65a7cd12009-09-01 12:16:01 +00001007 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
drhf30a9692013-11-15 01:10:18 +00001008 sqlite3ParserReset(pSubParse);
dan165921a2009-08-28 18:53:45 +00001009 sqlite3StackFree(db, pSubParse);
1010
dan2832ad42009-08-31 15:27:27 +00001011 return pPrg;
dan165921a2009-08-28 18:53:45 +00001012}
1013
dan65a7cd12009-09-01 12:16:01 +00001014/*
1015** Return a pointer to a TriggerPrg object containing the sub-program for
1016** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
1017** TriggerPrg object exists, a new object is allocated and populated before
1018** being returned.
1019*/
dan2832ad42009-08-31 15:27:27 +00001020static TriggerPrg *getRowTrigger(
dan65a7cd12009-09-01 12:16:01 +00001021 Parse *pParse, /* Current parse context */
dan165921a2009-08-28 18:53:45 +00001022 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +00001023 Table *pTab, /* The table trigger pTrigger is attached to */
1024 int orconf /* ON CONFLICT algorithm. */
dan165921a2009-08-28 18:53:45 +00001025){
dan65a7cd12009-09-01 12:16:01 +00001026 Parse *pRoot = sqlite3ParseToplevel(pParse);
dan2832ad42009-08-31 15:27:27 +00001027 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +00001028
dan1da40a32009-09-19 17:00:31 +00001029 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dan165921a2009-08-28 18:53:45 +00001030
1031 /* It may be that this trigger has already been coded (or is in the
1032 ** process of being coded). If this is the case, then an entry with
dan2832ad42009-08-31 15:27:27 +00001033 ** a matching TriggerPrg.pTrigger field will be present somewhere
1034 ** in the Parse.pTriggerPrg list. Search for such an entry. */
dan2832ad42009-08-31 15:27:27 +00001035 for(pPrg=pRoot->pTriggerPrg;
1036 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
1037 pPrg=pPrg->pNext
dan165921a2009-08-28 18:53:45 +00001038 );
1039
dan65a7cd12009-09-01 12:16:01 +00001040 /* If an existing TriggerPrg could not be located, create a new one. */
dan2832ad42009-08-31 15:27:27 +00001041 if( !pPrg ){
dan65a7cd12009-09-01 12:16:01 +00001042 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
dan165921a2009-08-28 18:53:45 +00001043 }
1044
dan2832ad42009-08-31 15:27:27 +00001045 return pPrg;
dan165921a2009-08-28 18:53:45 +00001046}
1047
dan94d7f502009-09-24 09:05:49 +00001048/*
1049** Generate code for the trigger program associated with trigger p on
1050** table pTab. The reg, orconf and ignoreJump parameters passed to this
1051** function are the same as those described in the header function for
1052** sqlite3CodeRowTrigger()
1053*/
dan1da40a32009-09-19 17:00:31 +00001054void sqlite3CodeRowTriggerDirect(
1055 Parse *pParse, /* Parse context */
1056 Trigger *p, /* Trigger to code */
1057 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001058 int reg, /* Reg array containing OLD.* and NEW.* values */
dan1da40a32009-09-19 17:00:31 +00001059 int orconf, /* ON CONFLICT policy */
1060 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
1061){
1062 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
1063 TriggerPrg *pPrg;
1064 pPrg = getRowTrigger(pParse, p, pTab, orconf);
1065 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
1066
1067 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
1068 ** is a pointer to the sub-vdbe containing the trigger program. */
1069 if( pPrg ){
dand19c9332010-07-26 12:05:17 +00001070 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
1071
drh9b34abe2016-01-16 15:12:35 +00001072 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
1073 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
dan1da40a32009-09-19 17:00:31 +00001074 VdbeComment(
1075 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
1076
1077 /* Set the P5 operand of the OP_Program instruction to non-zero if
1078 ** recursive invocation of this trigger program is disallowed. Recursive
1079 ** invocation is disallowed if (a) the sub-program is really a trigger,
1080 ** not a foreign key action, and (b) the flag to enable recursive triggers
1081 ** is clear. */
dand19c9332010-07-26 12:05:17 +00001082 sqlite3VdbeChangeP5(v, (u8)bRecursive);
dan1da40a32009-09-19 17:00:31 +00001083 }
1084}
1085
danielk1977633ed082002-05-17 00:05:58 +00001086/*
dan94d7f502009-09-24 09:05:49 +00001087** This is called to code the required FOR EACH ROW triggers for an operation
1088** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
drhf7b54962013-05-28 12:11:54 +00001089** is given by the op parameter. The tr_tm parameter determines whether the
dan94d7f502009-09-24 09:05:49 +00001090** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
1091** parameter pChanges is passed the list of columns being modified.
danielk1977633ed082002-05-17 00:05:58 +00001092**
dan94d7f502009-09-24 09:05:49 +00001093** If there are no triggers that fire at the specified time for the specified
1094** operation on pTab, this function is a no-op.
drhc977f7f2002-05-21 11:38:11 +00001095**
dan94d7f502009-09-24 09:05:49 +00001096** The reg argument is the address of the first in an array of registers
1097** that contain the values substituted for the new.* and old.* references
1098** in the trigger program. If N is the number of columns in table pTab
1099** (a copy of pTab->nCol), then registers are populated as follows:
drhc977f7f2002-05-21 11:38:11 +00001100**
dan94d7f502009-09-24 09:05:49 +00001101** Register Contains
1102** ------------------------------------------------------
1103** reg+0 OLD.rowid
1104** reg+1 OLD.* value of left-most column of pTab
1105** ... ...
1106** reg+N OLD.* value of right-most column of pTab
1107** reg+N+1 NEW.rowid
1108** reg+N+2 OLD.* value of left-most column of pTab
1109** ... ...
1110** reg+N+N+1 NEW.* value of right-most column of pTab
drhc977f7f2002-05-21 11:38:11 +00001111**
dan94d7f502009-09-24 09:05:49 +00001112** For ON DELETE triggers, the registers containing the NEW.* values will
1113** never be accessed by the trigger program, so they are not allocated or
1114** populated by the caller (there is no data to populate them with anyway).
1115** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
1116** are never accessed, and so are not allocated by the caller. So, for an
1117** ON INSERT trigger, the value passed to this function as parameter reg
1118** is not a readable register, although registers (reg+N) through
1119** (reg+N+N+1) are.
danielk1977633ed082002-05-17 00:05:58 +00001120**
dan94d7f502009-09-24 09:05:49 +00001121** Parameter orconf is the default conflict resolution algorithm for the
1122** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
1123** is the instruction that control should jump to if a trigger program
1124** raises an IGNORE exception.
danielk1977633ed082002-05-17 00:05:58 +00001125*/
dan165921a2009-08-28 18:53:45 +00001126void sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +00001127 Parse *pParse, /* Parse context */
danielk19772f886d12009-02-28 10:47:41 +00001128 Trigger *pTrigger, /* List of triggers on table pTab */
danielk1977633ed082002-05-17 00:05:58 +00001129 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
1130 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +00001131 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +00001132 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001133 int reg, /* The first in an array of registers (see above) */
danielk19776f349032002-06-11 02:25:40 +00001134 int orconf, /* ON CONFLICT policy */
dan165921a2009-08-28 18:53:45 +00001135 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
drhc977f7f2002-05-21 11:38:11 +00001136){
dan94d7f502009-09-24 09:05:49 +00001137 Trigger *p; /* Used to iterate through pTrigger list */
danielk19778f2c54e2008-01-01 19:02:09 +00001138
dan94d7f502009-09-24 09:05:49 +00001139 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
1140 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
1141 assert( (op==TK_UPDATE)==(pChanges!=0) );
danielk1977c3f9bad2002-05-15 08:30:12 +00001142
danielk19772f886d12009-02-28 10:47:41 +00001143 for(p=pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +00001144
drh9ab4c2e2009-05-09 00:18:38 +00001145 /* Sanity checking: The schema for the trigger and for the table are
1146 ** always defined. The trigger must be in the same schema as the table
1147 ** or else it must be a TEMP trigger. */
1148 assert( p->pSchema!=0 );
1149 assert( p->pTabSchema!=0 );
dan165921a2009-08-28 18:53:45 +00001150 assert( p->pSchema==p->pTabSchema
1151 || p->pSchema==pParse->db->aDb[1].pSchema );
drh9ab4c2e2009-05-09 00:18:38 +00001152
danielk1977eecfb3e2006-01-10 12:31:39 +00001153 /* Determine whether we should code this trigger */
dan165921a2009-08-28 18:53:45 +00001154 if( p->op==op
1155 && p->tr_tm==tr_tm
dan94d7f502009-09-24 09:05:49 +00001156 && checkColumnOverlap(p->pColumns, pChanges)
danielk1977eecfb3e2006-01-10 12:31:39 +00001157 ){
dan94d7f502009-09-24 09:05:49 +00001158 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
danielk1977c3f9bad2002-05-15 08:30:12 +00001159 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001160 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001161}
dan165921a2009-08-28 18:53:45 +00001162
dan2832ad42009-08-31 15:27:27 +00001163/*
danbb5f1682009-11-27 12:12:34 +00001164** Triggers may access values stored in the old.* or new.* pseudo-table.
1165** This function returns a 32-bit bitmask indicating which columns of the
1166** old.* or new.* tables actually are used by triggers. This information
1167** may be used by the caller, for example, to avoid having to load the entire
1168** old.* record into memory when executing an UPDATE or DELETE command.
dan2832ad42009-08-31 15:27:27 +00001169**
1170** Bit 0 of the returned mask is set if the left-most column of the
danbb5f1682009-11-27 12:12:34 +00001171** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
dan2832ad42009-08-31 15:27:27 +00001172** the second leftmost column value is required, and so on. If there
1173** are more than 32 columns in the table, and at least one of the columns
1174** with an index greater than 32 may be accessed, 0xffffffff is returned.
1175**
danbb5f1682009-11-27 12:12:34 +00001176** It is not possible to determine if the old.rowid or new.rowid column is
1177** accessed by triggers. The caller must always assume that it is.
dan2832ad42009-08-31 15:27:27 +00001178**
danbb5f1682009-11-27 12:12:34 +00001179** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
1180** applies to the old.* table. If 1, the new.* table.
1181**
1182** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
1183** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
1184** included in the returned mask if the TRIGGER_BEFORE bit is set in the
1185** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
1186** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
dan2832ad42009-08-31 15:27:27 +00001187*/
danbb5f1682009-11-27 12:12:34 +00001188u32 sqlite3TriggerColmask(
dan165921a2009-08-28 18:53:45 +00001189 Parse *pParse, /* Parse context */
1190 Trigger *pTrigger, /* List of triggers on table pTab */
dan165921a2009-08-28 18:53:45 +00001191 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
danbb5f1682009-11-27 12:12:34 +00001192 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
1193 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
dan165921a2009-08-28 18:53:45 +00001194 Table *pTab, /* The table to code triggers from */
dan2832ad42009-08-31 15:27:27 +00001195 int orconf /* Default ON CONFLICT policy for trigger steps */
dan165921a2009-08-28 18:53:45 +00001196){
dan1da40a32009-09-19 17:00:31 +00001197 const int op = pChanges ? TK_UPDATE : TK_DELETE;
dan2832ad42009-08-31 15:27:27 +00001198 u32 mask = 0;
dan165921a2009-08-28 18:53:45 +00001199 Trigger *p;
dan165921a2009-08-28 18:53:45 +00001200
danbb5f1682009-11-27 12:12:34 +00001201 assert( isNew==1 || isNew==0 );
dan165921a2009-08-28 18:53:45 +00001202 for(p=pTrigger; p; p=p->pNext){
danbb5f1682009-11-27 12:12:34 +00001203 if( p->op==op && (tr_tm&p->tr_tm)
1204 && checkColumnOverlap(p->pColumns,pChanges)
1205 ){
dan2832ad42009-08-31 15:27:27 +00001206 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +00001207 pPrg = getRowTrigger(pParse, p, pTab, orconf);
dan2832ad42009-08-31 15:27:27 +00001208 if( pPrg ){
danbb5f1682009-11-27 12:12:34 +00001209 mask |= pPrg->aColmask[isNew];
dan165921a2009-08-28 18:53:45 +00001210 }
1211 }
1212 }
dan2832ad42009-08-31 15:27:27 +00001213
1214 return mask;
dan165921a2009-08-28 18:53:45 +00001215}
1216
drhb7f91642004-10-31 02:22:47 +00001217#endif /* !defined(SQLITE_OMIT_TRIGGER) */