blob: ca9033b2d6eed48a1dccd2ac646b21056363ea55 [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
drhb8352472021-01-29 19:32:17 +000068 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
danielk19772f886d12009-02-28 10:47:41 +000069 ){
drhf54a80f2021-01-29 13:47:36 +000070 pTrig->pNext = pList;
danielk19772f886d12009-02-28 10:47:41 +000071 pList = pTrig;
drhb8352472021-01-29 19:32:17 +000072 }else if( pTrig->op==TK_RETURNING ){
73 pTrig->table = pTab->zName;
74 pTrig->pTabSchema = pTab->pSchema;
75 pTrig->pNext = pList;
76 pList = pTrig;
77 }
drhf54a80f2021-01-29 13:47:36 +000078 p = sqliteHashNext(p);
danielk19772f886d12009-02-28 10:47:41 +000079 }
80 }
drhf54a80f2021-01-29 13:47:36 +000081 return pList;
danielk19772f886d12009-02-28 10:47:41 +000082}
83
84/*
drhf0f258b2003-04-21 18:48:45 +000085** This is called by the parser when it sees a CREATE TRIGGER statement
86** up to the point of the BEGIN before the trigger actions. A Trigger
87** structure is generated based on the information available and stored
88** in pParse->pNewTrigger. After the trigger actions have been parsed, the
danielk19774adee202004-05-08 08:23:19 +000089** sqlite3FinishTrigger() function is called to complete the trigger
drhf0f258b2003-04-21 18:48:45 +000090** construction process.
drh9adf9ac2002-05-15 11:44:13 +000091*/
danielk19774adee202004-05-08 08:23:19 +000092void sqlite3BeginTrigger(
drh9adf9ac2002-05-15 11:44:13 +000093 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
danielk1977ef2cb632004-05-29 02:37:19 +000094 Token *pName1, /* The name of the trigger */
95 Token *pName2, /* The name of the trigger */
drh5cf590c2003-04-24 01:45:04 +000096 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
drh9adf9ac2002-05-15 11:44:13 +000097 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
danielk1977633ed082002-05-17 00:05:58 +000098 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
drhd24cc422003-03-27 12:51:24 +000099 SrcList *pTableName,/* The name of the table/view the trigger applies to */
drh9adf9ac2002-05-15 11:44:13 +0000100 Expr *pWhen, /* WHEN clause */
drhfdd48a72006-09-11 23:45:48 +0000101 int isTemp, /* True if the TEMPORARY keyword is present */
102 int noErr /* Suppress errors if the trigger already exists */
drh9adf9ac2002-05-15 11:44:13 +0000103){
drh3d5f74b2009-08-06 17:43:31 +0000104 Trigger *pTrigger = 0; /* The new trigger */
105 Table *pTab; /* Table that the trigger fires off of */
drhf0f258b2003-04-21 18:48:45 +0000106 char *zName = 0; /* Name of the trigger */
drh3d5f74b2009-08-06 17:43:31 +0000107 sqlite3 *db = pParse->db; /* The database connection */
danielk1977ef2cb632004-05-29 02:37:19 +0000108 int iDb; /* The database to store the trigger in */
109 Token *pName; /* The unqualified db name */
drh3d5f74b2009-08-06 17:43:31 +0000110 DbFixer sFix; /* State vector for the DB fixer */
drhed6c8672003-01-12 18:02:16 +0000111
drh43617e92006-03-06 20:55:46 +0000112 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
113 assert( pName2!=0 );
drhaa78bec2008-12-09 03:55:14 +0000114 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
115 assert( op>0 && op<0xff );
danielk1977ef2cb632004-05-29 02:37:19 +0000116 if( isTemp ){
117 /* If TEMP was specified, then the trigger name may not be qualified. */
drh43617e92006-03-06 20:55:46 +0000118 if( pName2->n>0 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000119 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
120 goto trigger_cleanup;
121 }
122 iDb = 1;
123 pName = pName1;
124 }else{
mistachkind5578432012-08-25 10:01:29 +0000125 /* Figure out the db that the trigger will be created in */
danielk1977ef2cb632004-05-29 02:37:19 +0000126 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
127 if( iDb<0 ){
128 goto trigger_cleanup;
129 }
130 }
drh6fea83e2011-07-01 13:50:44 +0000131 if( !pTableName || db->mallocFailed ){
132 goto trigger_cleanup;
133 }
134
135 /* A long-standing parser bug is that this syntax was allowed:
136 **
137 ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
138 ** ^^^^^^^^
139 **
140 ** To maintain backwards compatibility, ignore the database
drh346a70c2020-06-15 20:27:35 +0000141 ** name on pTableName if we are reparsing out of the schema table
drh6fea83e2011-07-01 13:50:44 +0000142 */
143 if( db->init.busy && iDb!=1 ){
144 sqlite3DbFree(db, pTableName->a[0].zDatabase);
145 pTableName->a[0].zDatabase = 0;
146 }
danielk1977ef2cb632004-05-29 02:37:19 +0000147
148 /* If the trigger name was unqualified, and the table is a temp table,
149 ** then set iDb to 1 to create the trigger in the temporary database.
150 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
151 ** exist, the error is caught by the block below.
drh9adf9ac2002-05-15 11:44:13 +0000152 */
danielk1977ef2cb632004-05-29 02:37:19 +0000153 pTab = sqlite3SrcListLookup(pParse, pTableName);
drh622d2882010-02-15 16:54:55 +0000154 if( db->init.busy==0 && pName2->n==0 && pTab
155 && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977ef2cb632004-05-29 02:37:19 +0000156 iDb = 1;
157 }
158
159 /* Ensure the table name matches database name and that the table exists */
drh17435752007-08-16 04:30:38 +0000160 if( db->mallocFailed ) goto trigger_cleanup;
drhd24cc422003-03-27 12:51:24 +0000161 assert( pTableName->nSrc==1 );
drhd100f692013-10-03 15:39:44 +0000162 sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
163 if( sqlite3FixSrcList(&sFix, pTableName) ){
drh4312db52003-06-03 01:47:11 +0000164 goto trigger_cleanup;
drhf26e09c2003-05-31 16:21:12 +0000165 }
danielk1977ef2cb632004-05-29 02:37:19 +0000166 pTab = sqlite3SrcListLookup(pParse, pTableName);
167 if( !pTab ){
168 /* The table does not exist. */
drh4e451aa2020-11-05 19:13:44 +0000169 goto trigger_orphan_error;
drhd24cc422003-03-27 12:51:24 +0000170 }
drh4cbdda92006-06-14 19:00:20 +0000171 if( IsVirtual(pTab) ){
172 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
drh4e451aa2020-11-05 19:13:44 +0000173 goto trigger_orphan_error;
drh4cbdda92006-06-14 19:00:20 +0000174 }
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);
drhc5a93d42019-08-12 00:08:07 +0000179 if( zName==0 ){
180 assert( db->mallocFailed );
181 goto trigger_cleanup;
182 }
183 if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
danielk1977d8123362004-06-12 09:25:12 +0000184 goto trigger_cleanup;
185 }
drh21206082011-04-04 18:22:02 +0000186 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danc9461ec2018-08-29 21:00:16 +0000187 if( !IN_RENAME_OBJECT ){
dan5496d6a2018-08-13 17:14:26 +0000188 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
189 if( !noErr ){
190 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
191 }else{
192 assert( !db->init.busy );
193 sqlite3CodeVerifySchema(pParse, iDb);
194 }
195 goto trigger_cleanup;
drhfdd48a72006-09-11 23:45:48 +0000196 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000197 }
danielk1977ef2cb632004-05-29 02:37:19 +0000198
199 /* Do not create a trigger on a system table */
drhdca76842004-12-07 14:06:13 +0000200 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk19774adee202004-05-08 08:23:19 +0000201 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
drhd24cc422003-03-27 12:51:24 +0000202 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40 +0000203 }
danielk1977ef2cb632004-05-29 02:37:19 +0000204
205 /* INSTEAD of triggers are only for views and views only support INSTEAD
206 ** of triggers.
207 */
208 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000209 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drhda93d232003-03-31 02:12:46 +0000210 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", 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 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000214 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drhda93d232003-03-31 02:12:46 +0000215 " trigger on table: %S", pTableName, 0);
drh4e451aa2020-11-05 19:13:44 +0000216 goto trigger_orphan_error;
drhd24cc422003-03-27 12:51:24 +0000217 }
danielk1977ef2cb632004-05-29 02:37:19 +0000218
drhd24cc422003-03-27 12:51:24 +0000219#ifndef SQLITE_OMIT_AUTHORIZATION
danc9461ec2018-08-29 21:00:16 +0000220 if( !IN_RENAME_OBJECT ){
drha0daa752016-09-16 11:53:10 +0000221 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drhd24cc422003-03-27 12:51:24 +0000222 int code = SQLITE_CREATE_TRIGGER;
drh69c33822016-08-18 14:33:11 +0000223 const char *zDb = db->aDb[iTabDb].zDbSName;
224 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
danielk1977da184232006-01-05 11:34:32 +0000225 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19 +0000226 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24 +0000227 goto trigger_cleanup;
228 }
danielk1977da184232006-01-05 11:34:32 +0000229 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24 +0000230 goto trigger_cleanup;
231 }
232 }
233#endif
danielk1977d702fcc2002-05-26 23:24:40 +0000234
danielk1977ef2cb632004-05-29 02:37:19 +0000235 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04 +0000236 ** cannot appear on views. So we might as well translate every
237 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
238 ** elsewhere.
239 */
danielk1977d702fcc2002-05-26 23:24:40 +0000240 if (tr_tm == TK_INSTEAD){
241 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12 +0000242 }
243
244 /* Build the Trigger object */
drh17435752007-08-16 04:30:38 +0000245 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19 +0000246 if( pTrigger==0 ) goto trigger_cleanup;
dan165921a2009-08-28 18:53:45 +0000247 pTrigger->zName = zName;
drhe5f9c642003-01-13 23:27:31 +0000248 zName = 0;
drh17435752007-08-16 04:30:38 +0000249 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32 +0000250 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48 +0000251 pTrigger->pTabSchema = pTab->pSchema;
drhaa78bec2008-12-09 03:55:14 +0000252 pTrigger->op = (u8)op;
drhdca76842004-12-07 14:06:13 +0000253 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
danc9461ec2018-08-29 21:00:16 +0000254 if( IN_RENAME_OBJECT ){
255 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
dan5496d6a2018-08-13 17:14:26 +0000256 pTrigger->pWhen = pWhen;
257 pWhen = 0;
258 }else{
259 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
260 }
261 pTrigger->pColumns = pColumns;
262 pColumns = 0;
drhf0f258b2003-04-21 18:48:45 +0000263 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19 +0000264 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45 +0000265
266trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000267 sqlite3DbFree(db, zName);
268 sqlite3SrcListDelete(db, pTableName);
269 sqlite3IdListDelete(db, pColumns);
270 sqlite3ExprDelete(db, pWhen);
danielk1977d5d56522005-03-16 12:15:20 +0000271 if( !pParse->pNewTrigger ){
drh633e6d52008-07-28 19:34:53 +0000272 sqlite3DeleteTrigger(db, pTrigger);
danielk1977d5d56522005-03-16 12:15:20 +0000273 }else{
274 assert( pParse->pNewTrigger==pTrigger );
275 }
drh4e451aa2020-11-05 19:13:44 +0000276 return;
277
278trigger_orphan_error:
279 if( db->init.iDb==1 ){
280 /* Ticket #3810.
281 ** Normally, whenever a table is dropped, all associated triggers are
282 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
283 ** and the table is dropped by a different database connection, the
284 ** trigger is not visible to the database connection that does the
285 ** drop so the trigger cannot be dropped. This results in an
286 ** "orphaned trigger" - a trigger whose associated table is missing.
287 **
288 ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df
289 */
290 db->init.orphanTrigger = 1;
291 }
292 goto trigger_cleanup;
drhf0f258b2003-04-21 18:48:45 +0000293}
294
295/*
296** This routine is called after all of the trigger actions have been parsed
297** in order to complete the process of building the trigger.
298*/
danielk19774adee202004-05-08 08:23:19 +0000299void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45 +0000300 Parse *pParse, /* Parser context */
301 TriggerStep *pStepList, /* The triggered program */
302 Token *pAll /* Token that describes the complete CREATE TRIGGER */
303){
drha8c62df2010-02-15 15:47:18 +0000304 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
305 char *zName; /* Name of trigger */
306 sqlite3 *db = pParse->db; /* The database */
307 DbFixer sFix; /* Fixer object */
308 int iDb; /* Database containing the trigger */
309 Token nameToken; /* Trigger name for error reporting */
drhf0f258b2003-04-21 18:48:45 +0000310
drhf0f258b2003-04-21 18:48:45 +0000311 pParse->pNewTrigger = 0;
drh9ab4c2e2009-05-09 00:18:38 +0000312 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
dan165921a2009-08-28 18:53:45 +0000313 zName = pTrig->zName;
danielk1977da184232006-01-05 11:34:32 +0000314 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32 +0000315 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53 +0000316 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32 +0000317 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53 +0000318 pStepList = pStepList->pNext;
319 }
drh40aced52016-01-22 17:48:09 +0000320 sqlite3TokenInit(&nameToken, pTrig->zName);
drhd100f692013-10-03 15:39:44 +0000321 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
322 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
dan46539d72013-10-03 12:29:38 +0000323 || sqlite3FixExpr(&sFix, pTrig->pWhen)
drhd100f692013-10-03 15:39:44 +0000324 ){
drhf26e09c2003-05-31 16:21:12 +0000325 goto triggerfinish_cleanup;
326 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000327
dan5496d6a2018-08-13 17:14:26 +0000328#ifndef SQLITE_OMIT_ALTERTABLE
danc9461ec2018-08-29 21:00:16 +0000329 if( IN_RENAME_OBJECT ){
dan5496d6a2018-08-13 17:14:26 +0000330 assert( !db->init.busy );
331 pParse->pNewTrigger = pTrig;
332 pTrig = 0;
333 }else
334#endif
335
drha8c62df2010-02-15 15:47:18 +0000336 /* if we are not initializing,
drh1e32bed2020-06-19 13:33:53 +0000337 ** build the sqlite_schema entry
drh9adf9ac2002-05-15 11:44:13 +0000338 */
drh1d85d932004-02-14 23:05:52 +0000339 if( !db->init.busy ){
drhc977f7f2002-05-21 11:38:11 +0000340 Vdbe *v;
drh2d401ab2008-01-10 23:50:11 +0000341 char *z;
danielk1977c3f9bad2002-05-15 08:30:12 +0000342
drh1e32bed2020-06-19 13:33:53 +0000343 /* Make an entry in the sqlite_schema table */
danielk19774adee202004-05-08 08:23:19 +0000344 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000345 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000346 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11 +0000347 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
drhd96cc6f2017-06-08 14:35:21 +0000348 testcase( z==0 );
drh2d401ab2008-01-10 23:50:11 +0000349 sqlite3NestedParse(pParse,
drh346a70c2020-06-15 20:27:35 +0000350 "INSERT INTO %Q." DFLT_SCHEMA_TABLE
351 " VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
352 db->aDb[iDb].zDbSName, zName,
drh2d401ab2008-01-10 23:50:11 +0000353 pTrig->table, z);
drh633e6d52008-07-28 19:34:53 +0000354 sqlite3DbFree(db, z);
drh9cbf3422008-01-17 16:22:13 +0000355 sqlite3ChangeCookie(pParse, iDb);
drh5d9c9da2011-06-03 20:11:17 +0000356 sqlite3VdbeAddParseSchemaOp(v, iDb,
357 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
danielk1977c3f9bad2002-05-15 08:30:12 +0000358 }
359
drh956bc922004-07-24 17:38:29 +0000360 if( db->init.busy ){
danielk19772f886d12009-02-28 10:47:41 +0000361 Trigger *pLink = pTrig;
362 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
drh21206082011-04-04 18:22:02 +0000363 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh55f66b32019-07-16 19:44:32 +0000364 assert( pLink!=0 );
drhacbcb7e2014-08-21 20:26:37 +0000365 pTrig = sqlite3HashInsert(pHash, zName, pTrig);
danielk19772f886d12009-02-28 10:47:41 +0000366 if( pTrig ){
drh4a642b62016-02-05 01:55:27 +0000367 sqlite3OomFault(db);
danielk19772f886d12009-02-28 10:47:41 +0000368 }else if( pLink->pSchema==pLink->pTabSchema ){
369 Table *pTab;
drhacbcb7e2014-08-21 20:26:37 +0000370 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
danielk19772f886d12009-02-28 10:47:41 +0000371 assert( pTab!=0 );
372 pLink->pNext = pTab->pTrigger;
373 pTab->pTrigger = pLink;
danielk1977d5d56522005-03-16 12:15:20 +0000374 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000375 }
376
drhf0f258b2003-04-21 18:48:45 +0000377triggerfinish_cleanup:
drh633e6d52008-07-28 19:34:53 +0000378 sqlite3DeleteTrigger(db, pTrig);
danc9461ec2018-08-29 21:00:16 +0000379 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
drh633e6d52008-07-28 19:34:53 +0000380 sqlite3DeleteTriggerStep(db, pStepList);
drh4b59ab52002-08-24 18:24:51 +0000381}
danielk1977c3f9bad2002-05-15 08:30:12 +0000382
drh4b59ab52002-08-24 18:24:51 +0000383/*
drhf259df52017-12-27 20:38:35 +0000384** Duplicate a range of text from an SQL statement, then convert all
385** whitespace characters into ordinary space characters.
386*/
387static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
388 char *z = sqlite3DbSpanDup(db, zStart, zEnd);
389 int i;
390 if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i]) ) z[i] = ' ';
391 return z;
392}
393
394/*
drhc977f7f2002-05-21 11:38:11 +0000395** Turn a SELECT statement (that the pSelect parameter points to) into
396** a trigger step. Return a pointer to a TriggerStep structure.
397**
398** The parser calls this routine when it finds a SELECT statement in
399** body of a TRIGGER.
400*/
drhf259df52017-12-27 20:38:35 +0000401TriggerStep *sqlite3TriggerSelectStep(
402 sqlite3 *db, /* Database connection */
403 Select *pSelect, /* The SELECT statement */
404 const char *zStart, /* Start of SQL text */
405 const char *zEnd /* End of SQL text */
406){
drh17435752007-08-16 04:30:38 +0000407 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000408 if( pTriggerStep==0 ) {
drh633e6d52008-07-28 19:34:53 +0000409 sqlite3SelectDelete(db, pSelect);
danielk19772e588c72005-12-09 14:25:08 +0000410 return 0;
411 }
danielk1977633ed082002-05-17 00:05:58 +0000412 pTriggerStep->op = TK_SELECT;
413 pTriggerStep->pSelect = pSelect;
414 pTriggerStep->orconf = OE_Default;
drhf259df52017-12-27 20:38:35 +0000415 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
drhb7916a72009-05-27 10:31:29 +0000416 return pTriggerStep;
417}
danielk1977c3f9bad2002-05-15 08:30:12 +0000418
drhb7916a72009-05-27 10:31:29 +0000419/*
420** Allocate space to hold a new trigger step. The allocated space
421** holds both the TriggerStep object and the TriggerStep.target.z string.
422**
423** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
424*/
425static TriggerStep *triggerStepAllocate(
danc9461ec2018-08-29 21:00:16 +0000426 Parse *pParse, /* Parser context */
shane5eff7cf2009-08-10 03:57:58 +0000427 u8 op, /* Trigger opcode */
drhf259df52017-12-27 20:38:35 +0000428 Token *pName, /* The target name */
429 const char *zStart, /* Start of SQL text */
430 const char *zEnd /* End of SQL text */
drhb7916a72009-05-27 10:31:29 +0000431){
danc9461ec2018-08-29 21:00:16 +0000432 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000433 TriggerStep *pTriggerStep;
434
dan46408352015-04-21 16:38:49 +0000435 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
drhb7916a72009-05-27 10:31:29 +0000436 if( pTriggerStep ){
437 char *z = (char*)&pTriggerStep[1];
438 memcpy(z, pName->z, pName->n);
dan46408352015-04-21 16:38:49 +0000439 sqlite3Dequote(z);
440 pTriggerStep->zTarget = z;
drhb7916a72009-05-27 10:31:29 +0000441 pTriggerStep->op = op;
drhf259df52017-12-27 20:38:35 +0000442 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
danc9461ec2018-08-29 21:00:16 +0000443 if( IN_RENAME_OBJECT ){
444 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
445 }
drhb7916a72009-05-27 10:31:29 +0000446 }
danielk1977633ed082002-05-17 00:05:58 +0000447 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000448}
449
drhc977f7f2002-05-21 11:38:11 +0000450/*
451** Build a trigger step out of an INSERT statement. Return a pointer
452** to the new trigger step.
453**
454** The parser calls this routine when it sees an INSERT inside the
455** body of a trigger.
456*/
danielk19774adee202004-05-08 08:23:19 +0000457TriggerStep *sqlite3TriggerInsertStep(
dan5be60c52018-08-15 20:28:39 +0000458 Parse *pParse, /* Parser */
drhc977f7f2002-05-21 11:38:11 +0000459 Token *pTableName, /* Name of the table into which we insert */
460 IdList *pColumn, /* List of columns in pTableName to insert into */
drhc977f7f2002-05-21 11:38:11 +0000461 Select *pSelect, /* A SELECT statement that supplies values */
drhf259df52017-12-27 20:38:35 +0000462 u8 orconf, /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
drh46d2e5c2018-04-12 13:15:43 +0000463 Upsert *pUpsert, /* ON CONFLICT clauses for upsert */
drhf259df52017-12-27 20:38:35 +0000464 const char *zStart, /* Start of SQL text */
465 const char *zEnd /* End of SQL text */
danielk1977633ed082002-05-17 00:05:58 +0000466){
dan5be60c52018-08-15 20:28:39 +0000467 sqlite3 *db = pParse->db;
drhf3a65f72007-08-22 20:18:21 +0000468 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000469
drh75593d92014-01-10 20:46:55 +0000470 assert(pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000471
danc9461ec2018-08-29 21:00:16 +0000472 pTriggerStep = triggerStepAllocate(pParse, TK_INSERT, pTableName,zStart,zEnd);
danielk1977d5d56522005-03-16 12:15:20 +0000473 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000474 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000475 pTriggerStep->pSelect = pSelect;
476 pSelect = 0;
477 }else{
478 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
479 }
danielk1977d5d56522005-03-16 12:15:20 +0000480 pTriggerStep->pIdList = pColumn;
drh46d2e5c2018-04-12 13:15:43 +0000481 pTriggerStep->pUpsert = pUpsert;
danielk1977d5d56522005-03-16 12:15:20 +0000482 pTriggerStep->orconf = orconf;
dan9105fd52019-08-19 17:26:32 +0000483 if( pUpsert ){
484 sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
485 }
danielk1977d5d56522005-03-16 12:15:20 +0000486 }else{
drh99160482018-04-18 01:34:39 +0000487 testcase( pColumn );
drh633e6d52008-07-28 19:34:53 +0000488 sqlite3IdListDelete(db, pColumn);
drh99160482018-04-18 01:34:39 +0000489 testcase( pUpsert );
drh46d2e5c2018-04-12 13:15:43 +0000490 sqlite3UpsertDelete(db, pUpsert);
danielk1977d5d56522005-03-16 12:15:20 +0000491 }
drh33e619f2009-05-28 01:00:55 +0000492 sqlite3SelectDelete(db, pSelect);
danielk1977c3f9bad2002-05-15 08:30:12 +0000493
danielk1977633ed082002-05-17 00:05:58 +0000494 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000495}
496
drhc977f7f2002-05-21 11:38:11 +0000497/*
498** Construct a trigger step that implements an UPDATE statement and return
499** a pointer to that trigger step. The parser calls this routine when it
500** sees an UPDATE statement inside the body of a CREATE TRIGGER.
501*/
danielk19774adee202004-05-08 08:23:19 +0000502TriggerStep *sqlite3TriggerUpdateStep(
dan5be60c52018-08-15 20:28:39 +0000503 Parse *pParse, /* Parser */
drhc977f7f2002-05-21 11:38:11 +0000504 Token *pTableName, /* Name of the table to be updated */
dane7877b22020-07-14 19:51:01 +0000505 SrcList *pFrom,
drhc977f7f2002-05-21 11:38:11 +0000506 ExprList *pEList, /* The SET clause: list of column and new values */
507 Expr *pWhere, /* The WHERE clause */
drhf259df52017-12-27 20:38:35 +0000508 u8 orconf, /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
509 const char *zStart, /* Start of SQL text */
510 const char *zEnd /* End of SQL text */
drhc977f7f2002-05-21 11:38:11 +0000511){
dan5be60c52018-08-15 20:28:39 +0000512 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000513 TriggerStep *pTriggerStep;
514
danc9461ec2018-08-29 21:00:16 +0000515 pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE, pTableName,zStart,zEnd);
drh33e619f2009-05-28 01:00:55 +0000516 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000517 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000518 pTriggerStep->pExprList = pEList;
519 pTriggerStep->pWhere = pWhere;
dane7877b22020-07-14 19:51:01 +0000520 pTriggerStep->pFrom = pFrom;
dan5be60c52018-08-15 20:28:39 +0000521 pEList = 0;
522 pWhere = 0;
dane7877b22020-07-14 19:51:01 +0000523 pFrom = 0;
dan5be60c52018-08-15 20:28:39 +0000524 }else{
525 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
526 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
dane7877b22020-07-14 19:51:01 +0000527 pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE);
dan5be60c52018-08-15 20:28:39 +0000528 }
drh33e619f2009-05-28 01:00:55 +0000529 pTriggerStep->orconf = orconf;
drh0e3a6f32007-03-30 20:40:34 +0000530 }
drh33e619f2009-05-28 01:00:55 +0000531 sqlite3ExprListDelete(db, pEList);
532 sqlite3ExprDelete(db, pWhere);
dane7877b22020-07-14 19:51:01 +0000533 sqlite3SrcListDelete(db, pFrom);
danielk1977633ed082002-05-17 00:05:58 +0000534 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000535}
536
drhc977f7f2002-05-21 11:38:11 +0000537/*
538** Construct a trigger step that implements a DELETE statement and return
539** a pointer to that trigger step. The parser calls this routine when it
540** sees a DELETE statement inside the body of a CREATE TRIGGER.
541*/
drh17435752007-08-16 04:30:38 +0000542TriggerStep *sqlite3TriggerDeleteStep(
dan5be60c52018-08-15 20:28:39 +0000543 Parse *pParse, /* Parser */
drh17435752007-08-16 04:30:38 +0000544 Token *pTableName, /* The table from which rows are deleted */
drhf259df52017-12-27 20:38:35 +0000545 Expr *pWhere, /* The WHERE clause */
546 const char *zStart, /* Start of SQL text */
547 const char *zEnd /* End of SQL text */
drh17435752007-08-16 04:30:38 +0000548){
dan5be60c52018-08-15 20:28:39 +0000549 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000550 TriggerStep *pTriggerStep;
551
danc9461ec2018-08-29 21:00:16 +0000552 pTriggerStep = triggerStepAllocate(pParse, TK_DELETE, pTableName,zStart,zEnd);
drh33e619f2009-05-28 01:00:55 +0000553 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000554 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000555 pTriggerStep->pWhere = pWhere;
556 pWhere = 0;
557 }else{
558 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
559 }
drh33e619f2009-05-28 01:00:55 +0000560 pTriggerStep->orconf = OE_Default;
drhb63a53d2007-03-31 01:34:44 +0000561 }
drh33e619f2009-05-28 01:00:55 +0000562 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000563 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000564}
565
danielk1977633ed082002-05-17 00:05:58 +0000566/*
567** Recursively delete a Trigger structure
568*/
drh633e6d52008-07-28 19:34:53 +0000569void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
drh658f0a32021-01-30 02:43:26 +0000570 if( pTrigger==0 || pTrigger->bReturning ) return;
drh633e6d52008-07-28 19:34:53 +0000571 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
dan165921a2009-08-28 18:53:45 +0000572 sqlite3DbFree(db, pTrigger->zName);
drh633e6d52008-07-28 19:34:53 +0000573 sqlite3DbFree(db, pTrigger->table);
574 sqlite3ExprDelete(db, pTrigger->pWhen);
575 sqlite3IdListDelete(db, pTrigger->pColumns);
drh633e6d52008-07-28 19:34:53 +0000576 sqlite3DbFree(db, pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000577}
578
579/*
danielk19778a414492004-06-29 08:59:35 +0000580** This function is called to drop a trigger from the database schema.
581**
582** This may be called directly from the parser and therefore identifies
583** the trigger by name. The sqlite3DropTriggerPtr() routine does the
584** same job as this routine except it takes a pointer to the trigger
585** instead of the trigger name.
586**/
drhfdd48a72006-09-11 23:45:48 +0000587void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000588 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000589 int i;
590 const char *zDb;
591 const char *zName;
drh9bb575f2004-09-06 17:24:11 +0000592 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000593
drh17435752007-08-16 04:30:38 +0000594 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000595 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000596 goto drop_trigger_cleanup;
597 }
danielk19778e227872004-06-07 07:52:17 +0000598
drhd24cc422003-03-27 12:51:24 +0000599 assert( pName->nSrc==1 );
600 zDb = pName->a[0].zDatabase;
601 zName = pName->a[0].zName;
drh21206082011-04-04 18:22:02 +0000602 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
danielk197753c0f742005-03-29 03:10:59 +0000603 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000604 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
dan465c2b82020-03-21 15:10:40 +0000605 if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
drh21206082011-04-04 18:22:02 +0000606 assert( sqlite3SchemaMutexHeld(db, j, 0) );
drhacbcb7e2014-08-21 20:26:37 +0000607 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
drhd24cc422003-03-27 12:51:24 +0000608 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000609 }
drhd24cc422003-03-27 12:51:24 +0000610 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000611 if( !noErr ){
612 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
dan57966752011-04-09 17:32:58 +0000613 }else{
614 sqlite3CodeVerifyNamedSchema(pParse, zDb);
drhfdd48a72006-09-11 23:45:48 +0000615 }
dan1db95102010-06-28 10:15:19 +0000616 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +0000617 goto drop_trigger_cleanup;
618 }
drh74161702006-02-24 02:53:49 +0000619 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000620
621drop_trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000622 sqlite3SrcListDelete(db, pName);
drh79a519c2003-05-17 19:04:03 +0000623}
624
625/*
drh956bc922004-07-24 17:38:29 +0000626** Return a pointer to the Table structure for the table that a trigger
627** is set on.
628*/
drh74161702006-02-24 02:53:49 +0000629static Table *tableOfTrigger(Trigger *pTrigger){
drhacbcb7e2014-08-21 20:26:37 +0000630 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
drh956bc922004-07-24 17:38:29 +0000631}
632
633
634/*
drh74161702006-02-24 02:53:49 +0000635** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000636*/
drh74161702006-02-24 02:53:49 +0000637void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000638 Table *pTable;
639 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000640 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000641 int iDb;
drh79a519c2003-05-17 19:04:03 +0000642
danielk1977da184232006-01-05 11:34:32 +0000643 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000644 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000645 pTable = tableOfTrigger(pTrigger);
drh6397a782019-08-27 10:05:45 +0000646 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000647#ifndef SQLITE_OMIT_AUTHORIZATION
drh6397a782019-08-27 10:05:45 +0000648 if( pTable ){
drhe5f9c642003-01-13 23:27:31 +0000649 int code = SQLITE_DROP_TRIGGER;
drh69c33822016-08-18 14:33:11 +0000650 const char *zDb = db->aDb[iDb].zDbSName;
drh956bc922004-07-24 17:38:29 +0000651 const char *zTab = SCHEMA_TABLE(iDb);
652 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
dan2bd93512009-08-31 08:22:46 +0000653 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
danielk19774adee202004-05-08 08:23:19 +0000654 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000655 return;
drhe5f9c642003-01-13 23:27:31 +0000656 }
drhed6c8672003-01-12 18:02:16 +0000657 }
drhe5f9c642003-01-13 23:27:31 +0000658#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000659
drhf0f258b2003-04-21 18:48:45 +0000660 /* Generate code to destroy the database record of the trigger.
661 */
drh43617e92006-03-06 20:55:46 +0000662 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drh8c8dddc2015-12-09 16:26:38 +0000663 sqlite3NestedParse(pParse,
drh346a70c2020-06-15 20:27:35 +0000664 "DELETE FROM %Q." DFLT_SCHEMA_TABLE " WHERE name=%Q AND type='trigger'",
665 db->aDb[iDb].zDbSName, pTrigger->zName
drh8c8dddc2015-12-09 16:26:38 +0000666 );
drh9cbf3422008-01-17 16:22:13 +0000667 sqlite3ChangeCookie(pParse, iDb);
dan165921a2009-08-28 18:53:45 +0000668 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
drhf0f258b2003-04-21 18:48:45 +0000669 }
drh956bc922004-07-24 17:38:29 +0000670}
drhf0f258b2003-04-21 18:48:45 +0000671
drh956bc922004-07-24 17:38:29 +0000672/*
673** Remove a trigger from the hash tables of the sqlite* pointer.
674*/
675void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
676 Trigger *pTrigger;
drh21206082011-04-04 18:22:02 +0000677 Hash *pHash;
678
679 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
680 pHash = &(db->aDb[iDb].pSchema->trigHash);
drhacbcb7e2014-08-21 20:26:37 +0000681 pTrigger = sqlite3HashInsert(pHash, zName, 0);
drh9ab4c2e2009-05-09 00:18:38 +0000682 if( ALWAYS(pTrigger) ){
danielk19772f886d12009-02-28 10:47:41 +0000683 if( pTrigger->pSchema==pTrigger->pTabSchema ){
684 Table *pTab = tableOfTrigger(pTrigger);
drh6397a782019-08-27 10:05:45 +0000685 if( pTab ){
686 Trigger **pp;
dan0232dad2019-12-03 03:34:06 +0000687 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
688 if( *pp==pTrigger ){
689 *pp = (*pp)->pNext;
690 break;
691 }
692 }
drh6397a782019-08-27 10:05:45 +0000693 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000694 }
drh633e6d52008-07-28 19:34:53 +0000695 sqlite3DeleteTrigger(db, pTrigger);
drh8257aa82017-07-26 19:59:13 +0000696 db->mDbFlags |= DBFLAG_SchemaChange;
danielk1977c3f9bad2002-05-15 08:30:12 +0000697 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000698}
699
drhc977f7f2002-05-21 11:38:11 +0000700/*
701** pEList is the SET clause of an UPDATE statement. Each entry
702** in pEList is of the format <id>=<expr>. If any of the entries
703** in pEList have an <id> which matches an identifier in pIdList,
704** then return TRUE. If pIdList==NULL, then it is considered a
705** wildcard that matches anything. Likewise if pEList==NULL then
706** it matches anything so always return true. Return false only
707** if there is no match.
708*/
drh9ab4c2e2009-05-09 00:18:38 +0000709static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000710 int e;
drh9ab4c2e2009-05-09 00:18:38 +0000711 if( pIdList==0 || NEVER(pEList==0) ) return 1;
drhad2d8302002-05-24 20:31:36 +0000712 for(e=0; e<pEList->nExpr; e++){
drh41cee662019-12-12 20:22:34 +0000713 if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000714 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000715 return 0;
716}
717
danielk1977c3f9bad2002-05-15 08:30:12 +0000718/*
danielk19772f886d12009-02-28 10:47:41 +0000719** Return a list of all triggers on table pTab if there exists at least
720** one trigger that must be fired when an operation of type 'op' is
721** performed on the table, and, if that operation is an UPDATE, if at
722** least one of the columns in pChanges is being modified.
drhdca76842004-12-07 14:06:13 +0000723*/
danielk19772f886d12009-02-28 10:47:41 +0000724Trigger *sqlite3TriggersExist(
725 Parse *pParse, /* Parse context */
drhdca76842004-12-07 14:06:13 +0000726 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000727 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
danielk19772f886d12009-02-28 10:47:41 +0000728 ExprList *pChanges, /* Columns that change in an UPDATE statement */
729 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
drhc977f7f2002-05-21 11:38:11 +0000730){
drhdca76842004-12-07 14:06:13 +0000731 int mask = 0;
drhe83cafd2011-03-21 17:15:58 +0000732 Trigger *pList = 0;
danielk19772f886d12009-02-28 10:47:41 +0000733 Trigger *p;
drhe83cafd2011-03-21 17:15:58 +0000734
735 if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
736 pList = sqlite3TriggerList(pParse, pTab);
737 }
danielk19772f886d12009-02-28 10:47:41 +0000738 assert( pList==0 || IsVirtual(pTab)==0 );
739 for(p=pList; p; p=p->pNext){
drh9ab4c2e2009-05-09 00:18:38 +0000740 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
danielk19772f886d12009-02-28 10:47:41 +0000741 mask |= p->tr_tm;
drh28828c52021-01-30 21:55:38 +0000742 }else if( p->op==TK_RETURNING ){
743 /* The first time a RETURNING trigger is seen, the "op" value tells
744 ** us what time of trigger it should be. */
drh7baf3d42021-02-01 01:57:55 +0000745 assert( sqlite3IsToplevel(pParse) );
drhb8352472021-01-29 19:32:17 +0000746 p->op = op;
747 mask |= TRIGGER_AFTER;
drh7baf3d42021-02-01 01:57:55 +0000748 }else if( p->bReturning && p->op==TK_INSERT && op==TK_UPDATE
749 && sqlite3IsToplevel(pParse) ){
drh28828c52021-01-30 21:55:38 +0000750 /* Also fire a RETURNING trigger for INSERT on the UPDATE of an UPSERT */
751 mask |= TRIGGER_AFTER;
danielk1977c3f9bad2002-05-15 08:30:12 +0000752 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000753 }
danielk19772f886d12009-02-28 10:47:41 +0000754 if( pMask ){
755 *pMask = mask;
756 }
757 return (mask ? pList : 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000758}
759
drhc977f7f2002-05-21 11:38:11 +0000760/*
dan46408352015-04-21 16:38:49 +0000761** Convert the pStep->zTarget string into a SrcList and return a pointer
drhf26e09c2003-05-31 16:21:12 +0000762** to that SrcList.
763**
764** This routine adds a specific database name, if needed, to the target when
765** forming the SrcList. This prevents a trigger in one database from
766** referring to a target in another database. An exception is when the
767** trigger is in TEMP in which case it can refer to any other database it
768** wants.
769*/
dane7877b22020-07-14 19:51:01 +0000770SrcList *sqlite3TriggerStepSrc(
drhf26e09c2003-05-31 16:21:12 +0000771 Parse *pParse, /* The parsing context */
772 TriggerStep *pStep /* The trigger containing the target token */
773){
dan46408352015-04-21 16:38:49 +0000774 sqlite3 *db = pParse->db;
dane7877b22020-07-14 19:51:01 +0000775 SrcList *pSrc; /* SrcList to be returned */
776 char *zName = sqlite3DbStrDup(db, pStep->zTarget);
drh29c992c2019-01-17 15:40:41 +0000777 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
dane7877b22020-07-14 19:51:01 +0000778 assert( pSrc==0 || pSrc->nSrc==1 );
779 assert( zName || pSrc==0 );
drhb7916a72009-05-27 10:31:29 +0000780 if( pSrc ){
dane7877b22020-07-14 19:51:01 +0000781 Schema *pSchema = pStep->pTrig->pSchema;
782 pSrc->a[0].zName = zName;
783 if( pSchema!=db->aDb[1].pSchema ){
784 pSrc->a[0].pSchema = pSchema;
drhb7916a72009-05-27 10:31:29 +0000785 }
dane7877b22020-07-14 19:51:01 +0000786 if( pStep->pFrom ){
787 SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0);
788 pSrc = sqlite3SrcListAppendList(pParse, pSrc, pDup);
789 }
790 }else{
791 sqlite3DbFree(db, zName);
drhf26e09c2003-05-31 16:21:12 +0000792 }
793 return pSrc;
794}
795
drhdac9a5f2021-01-29 21:18:46 +0000796/* The input list pList is the list of result set terms from a RETURNING
797** clause. The table that we are returning from is pTab.
798**
799** This routine makes a copy of the pList, and at the same time expands
800** any "*" wildcards to be the complete set of columns from pTab.
801*/
802static ExprList *sqlite3ExpandReturning(
803 Parse *pParse, /* Parsing context */
804 ExprList *pList, /* The arguments to RETURNING */
805 Table *pTab /* The table being updated */
806){
807 ExprList *pNew = 0;
808 sqlite3 *db = pParse->db;
809 int i;
810 for(i=0; i<pList->nExpr; i++){
811 Expr *pOldExpr = pList->a[i].pExpr;
812 if( ALWAYS(pOldExpr!=0) && pOldExpr->op==TK_ASTERISK ){
813 int j;
814 for(j=0; j<pTab->nCol; j++){
815 Expr *pNewExpr = sqlite3Expr(db, TK_ID, pTab->aCol[j].zName);
816 pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr);
817 if( !db->mallocFailed ){
818 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
819 pItem->zEName = sqlite3DbStrDup(db, pTab->aCol[j].zName);
820 pItem->eEName = ENAME_NAME;
821 }
822 }
823 }else{
824 Expr *pNewExpr = sqlite3ExprDup(db, pOldExpr, 0);
825 pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr);
drh9407b6e2021-01-31 16:45:10 +0000826 if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0) ){
drhdac9a5f2021-01-29 21:18:46 +0000827 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
828 pItem->zEName = sqlite3DbStrDup(db, pList->a[i].zEName);
829 pItem->eEName = pList->a[i].eEName;
830 }
831 }
832 }
833 return pNew;
834}
835
drhf26e09c2003-05-31 16:21:12 +0000836/*
dan65a7cd12009-09-01 12:16:01 +0000837** Generate VDBE code for the statements inside the body of a single
838** trigger.
drhc977f7f2002-05-21 11:38:11 +0000839*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000840static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000841 Parse *pParse, /* The parser context */
842 TriggerStep *pStepList, /* List of statements inside the trigger body */
dan65a7cd12009-09-01 12:16:01 +0000843 int orconf /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000844){
dan65a7cd12009-09-01 12:16:01 +0000845 TriggerStep *pStep;
drh344737f2004-09-19 00:50:20 +0000846 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000847 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000848
dan65a7cd12009-09-01 12:16:01 +0000849 assert( pParse->pTriggerTab && pParse->pToplevel );
850 assert( pStepList );
drh344737f2004-09-19 00:50:20 +0000851 assert( v!=0 );
dan65a7cd12009-09-01 12:16:01 +0000852 for(pStep=pStepList; pStep; pStep=pStep->pNext){
dan165921a2009-08-28 18:53:45 +0000853 /* Figure out the ON CONFLICT policy that will be used for this step
854 ** of the trigger program. If the statement that caused this trigger
855 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
856 ** the ON CONFLICT policy that was specified as part of the trigger
857 ** step statement. Example:
858 **
859 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
860 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
861 ** END;
862 **
863 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
864 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
865 */
shanecea72b22009-09-07 04:38:36 +0000866 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
drhaceb31b2014-02-08 01:40:27 +0000867 assert( pParse->okConstFactor==0 );
danf78baaf2012-12-06 19:37:22 +0000868
drhf259df52017-12-27 20:38:35 +0000869#ifndef SQLITE_OMIT_TRACE
drhe307e112017-12-27 21:30:34 +0000870 if( pStep->zSpan ){
871 sqlite3VdbeAddOp4(v, OP_Trace, 0x7fffffff, 1, 0,
872 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
873 P4_DYNAMIC);
874 }
drhf259df52017-12-27 20:38:35 +0000875#endif
876
dan165921a2009-08-28 18:53:45 +0000877 switch( pStep->op ){
danielk1977633ed082002-05-17 00:05:58 +0000878 case TK_UPDATE: {
dan165921a2009-08-28 18:53:45 +0000879 sqlite3Update(pParse,
dane7877b22020-07-14 19:51:01 +0000880 sqlite3TriggerStepSrc(pParse, pStep),
dan165921a2009-08-28 18:53:45 +0000881 sqlite3ExprListDup(db, pStep->pExprList, 0),
882 sqlite3ExprDup(db, pStep->pWhere, 0),
drheac9fab2018-04-16 13:00:50 +0000883 pParse->eOrconf, 0, 0, 0
dan165921a2009-08-28 18:53:45 +0000884 );
drhdac9a5f2021-01-29 21:18:46 +0000885 sqlite3VdbeAddOp0(v, OP_ResetCount);
danielk1977633ed082002-05-17 00:05:58 +0000886 break;
887 }
888 case TK_INSERT: {
dan165921a2009-08-28 18:53:45 +0000889 sqlite3Insert(pParse,
dane7877b22020-07-14 19:51:01 +0000890 sqlite3TriggerStepSrc(pParse, pStep),
dan165921a2009-08-28 18:53:45 +0000891 sqlite3SelectDup(db, pStep->pSelect, 0),
892 sqlite3IdListDup(db, pStep->pIdList),
drh46d2e5c2018-04-12 13:15:43 +0000893 pParse->eOrconf,
894 sqlite3UpsertDup(db, pStep->pUpsert)
dan165921a2009-08-28 18:53:45 +0000895 );
drhdac9a5f2021-01-29 21:18:46 +0000896 sqlite3VdbeAddOp0(v, OP_ResetCount);
danielk1977633ed082002-05-17 00:05:58 +0000897 break;
898 }
899 case TK_DELETE: {
dan165921a2009-08-28 18:53:45 +0000900 sqlite3DeleteFrom(pParse,
dane7877b22020-07-14 19:51:01 +0000901 sqlite3TriggerStepSrc(pParse, pStep),
drh8c0833f2017-11-14 23:48:23 +0000902 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
dan165921a2009-08-28 18:53:45 +0000903 );
drhdac9a5f2021-01-29 21:18:46 +0000904 sqlite3VdbeAddOp0(v, OP_ResetCount);
danielk1977633ed082002-05-17 00:05:58 +0000905 break;
906 }
drhdac9a5f2021-01-29 21:18:46 +0000907 case TK_SELECT: {
dan165921a2009-08-28 18:53:45 +0000908 SelectDest sDest;
909 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
drhdac9a5f2021-01-29 21:18:46 +0000910 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
dan165921a2009-08-28 18:53:45 +0000911 sqlite3Select(pParse, pSelect, &sDest);
912 sqlite3SelectDelete(db, pSelect);
drh9ab4c2e2009-05-09 00:18:38 +0000913 break;
914 }
drhdac9a5f2021-01-29 21:18:46 +0000915 default: assert( pStep->op==TK_RETURNING ); {
916 Select *pSelect = pStep->pSelect;
917 ExprList *pList = pSelect->pEList;
918 SelectDest sDest;
919 pSelect->pEList =
920 sqlite3ExpandReturning(pParse, pList, pParse->pTriggerTab);
921 sqlite3SelectDestInit(&sDest, SRT_Output, 0);
drhba71a8a2021-01-30 01:30:26 +0000922 pSelect->selFlags = 0;
drhdac9a5f2021-01-29 21:18:46 +0000923 sqlite3Select(pParse, pSelect, &sDest);
924 sqlite3ExprListDelete(db, pSelect->pEList);
925 pSelect->pEList = pList;
926 break;
927 }
danielk1977633ed082002-05-17 00:05:58 +0000928 }
danielk1977633ed082002-05-17 00:05:58 +0000929 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000930
danielk1977633ed082002-05-17 00:05:58 +0000931 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000932}
933
drhc7379ce2013-10-30 02:28:23 +0000934#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
dan165921a2009-08-28 18:53:45 +0000935/*
936** This function is used to add VdbeComment() annotations to a VDBE
937** program. It is not used in production code, only for debugging.
938*/
939static const char *onErrorText(int onError){
940 switch( onError ){
941 case OE_Abort: return "abort";
942 case OE_Rollback: return "rollback";
943 case OE_Fail: return "fail";
944 case OE_Replace: return "replace";
945 case OE_Ignore: return "ignore";
946 case OE_Default: return "default";
947 }
948 return "n/a";
949}
950#endif
951
952/*
953** Parse context structure pFrom has just been used to create a sub-vdbe
954** (trigger program). If an error has occurred, transfer error information
955** from pFrom to pTo.
956*/
957static void transferParseError(Parse *pTo, Parse *pFrom){
958 assert( pFrom->zErrMsg==0 || pFrom->nErr );
959 assert( pTo->zErrMsg==0 || pTo->nErr );
960 if( pTo->nErr==0 ){
961 pTo->zErrMsg = pFrom->zErrMsg;
962 pTo->nErr = pFrom->nErr;
drhe06874e2015-04-16 15:47:06 +0000963 pTo->rc = pFrom->rc;
dan165921a2009-08-28 18:53:45 +0000964 }else{
965 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
966 }
967}
968
dan65a7cd12009-09-01 12:16:01 +0000969/*
970** Create and populate a new TriggerPrg object with a sub-program
971** implementing trigger pTrigger with ON CONFLICT policy orconf.
972*/
dan2832ad42009-08-31 15:27:27 +0000973static TriggerPrg *codeRowTrigger(
dan165921a2009-08-28 18:53:45 +0000974 Parse *pParse, /* Current parse context */
975 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +0000976 Table *pTab, /* The table pTrigger is attached to */
977 int orconf /* ON CONFLICT policy to code trigger program with */
dan165921a2009-08-28 18:53:45 +0000978){
dan65a7cd12009-09-01 12:16:01 +0000979 Parse *pTop = sqlite3ParseToplevel(pParse);
980 sqlite3 *db = pParse->db; /* Database handle */
981 TriggerPrg *pPrg; /* Value to return */
dan165921a2009-08-28 18:53:45 +0000982 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
983 Vdbe *v; /* Temporary VM */
dan165921a2009-08-28 18:53:45 +0000984 NameContext sNC; /* Name context for sub-vdbe */
985 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
986 Parse *pSubParse; /* Parse context for sub-vdbe */
987 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
988
dan1da40a32009-09-19 17:00:31 +0000989 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dand19c9332010-07-26 12:05:17 +0000990 assert( pTop->pVdbe );
dan65a7cd12009-09-01 12:16:01 +0000991
992 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
993 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
994 ** list of the top-level Parse object sooner rather than later. */
dan2832ad42009-08-31 15:27:27 +0000995 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
996 if( !pPrg ) return 0;
dan65a7cd12009-09-01 12:16:01 +0000997 pPrg->pNext = pTop->pTriggerPrg;
998 pTop->pTriggerPrg = pPrg;
dan2832ad42009-08-31 15:27:27 +0000999 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
dan165921a2009-08-28 18:53:45 +00001000 if( !pProgram ) return 0;
dand19c9332010-07-26 12:05:17 +00001001 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
dan2832ad42009-08-31 15:27:27 +00001002 pPrg->pTrigger = pTrigger;
1003 pPrg->orconf = orconf;
danbb5f1682009-11-27 12:12:34 +00001004 pPrg->aColmask[0] = 0xffffffff;
1005 pPrg->aColmask[1] = 0xffffffff;
dan165921a2009-08-28 18:53:45 +00001006
dan65a7cd12009-09-01 12:16:01 +00001007 /* Allocate and populate a new Parse context to use for coding the
1008 ** trigger sub-program. */
1009 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
1010 if( !pSubParse ) return 0;
dan165921a2009-08-28 18:53:45 +00001011 memset(&sNC, 0, sizeof(sNC));
1012 sNC.pParse = pSubParse;
1013 pSubParse->db = db;
1014 pSubParse->pTriggerTab = pTab;
dan65a7cd12009-09-01 12:16:01 +00001015 pSubParse->pToplevel = pTop;
dan2bd93512009-08-31 08:22:46 +00001016 pSubParse->zAuthContext = pTrigger->zName;
dan65a7cd12009-09-01 12:16:01 +00001017 pSubParse->eTriggerOp = pTrigger->op;
drhb8352472021-01-29 19:32:17 +00001018 pSubParse->bReturning = pTrigger->bReturning;
drh8b307fb2010-04-06 15:57:05 +00001019 pSubParse->nQueryLoop = pParse->nQueryLoop;
dan1ea04432018-12-21 19:29:11 +00001020 pSubParse->disableVtab = pParse->disableVtab;
dan165921a2009-08-28 18:53:45 +00001021
1022 v = sqlite3GetVdbe(pSubParse);
1023 if( v ){
dan76d462e2009-08-30 11:42:51 +00001024 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
1025 pTrigger->zName, onErrorText(orconf),
dan165921a2009-08-28 18:53:45 +00001026 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
dan65a7cd12009-09-01 12:16:01 +00001027 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
1028 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
1029 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
dan76d462e2009-08-30 11:42:51 +00001030 pTab->zName
dan165921a2009-08-28 18:53:45 +00001031 ));
dan76d462e2009-08-30 11:42:51 +00001032#ifndef SQLITE_OMIT_TRACE
drhe307e112017-12-27 21:30:34 +00001033 if( pTrigger->zName ){
1034 sqlite3VdbeChangeP4(v, -1,
1035 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
1036 );
1037 }
dan76d462e2009-08-30 11:42:51 +00001038#endif
dan165921a2009-08-28 18:53:45 +00001039
dan65a7cd12009-09-01 12:16:01 +00001040 /* If one was specified, code the WHEN clause. If it evaluates to false
1041 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
1042 ** OP_Halt inserted at the end of the program. */
dan165921a2009-08-28 18:53:45 +00001043 if( pTrigger->pWhen ){
dan165921a2009-08-28 18:53:45 +00001044 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
dan523a0872009-08-31 05:23:32 +00001045 if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
1046 && db->mallocFailed==0
1047 ){
drhec4ccdb2018-12-29 02:26:59 +00001048 iEndTrigger = sqlite3VdbeMakeLabel(pSubParse);
dan165921a2009-08-28 18:53:45 +00001049 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
1050 }
1051 sqlite3ExprDelete(db, pWhen);
1052 }
1053
1054 /* Code the trigger program into the sub-vdbe. */
dan76d462e2009-08-30 11:42:51 +00001055 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
dan65a7cd12009-09-01 12:16:01 +00001056
1057 /* Insert an OP_Halt at the end of the sub-program. */
dan165921a2009-08-28 18:53:45 +00001058 if( iEndTrigger ){
1059 sqlite3VdbeResolveLabel(v, iEndTrigger);
1060 }
1061 sqlite3VdbeAddOp0(v, OP_Halt);
dan76d462e2009-08-30 11:42:51 +00001062 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
1063
dan165921a2009-08-28 18:53:45 +00001064 transferParseError(pParse, pSubParse);
dan08951172017-11-28 20:43:40 +00001065 if( db->mallocFailed==0 && pParse->nErr==0 ){
dan65a7cd12009-09-01 12:16:01 +00001066 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
dan523a0872009-08-31 05:23:32 +00001067 }
drhb8352472021-01-29 19:32:17 +00001068 if( pTrigger->bReturning ){
drh662fe792021-01-31 12:41:20 +00001069 sqlite3VdbeColumnInfoXfer(sqlite3ParseToplevel(pParse)->pVdbe, v);
drhb8352472021-01-29 19:32:17 +00001070 }
dan165921a2009-08-28 18:53:45 +00001071 pProgram->nMem = pSubParse->nMem;
1072 pProgram->nCsr = pSubParse->nTab;
1073 pProgram->token = (void *)pTrigger;
danbb5f1682009-11-27 12:12:34 +00001074 pPrg->aColmask[0] = pSubParse->oldmask;
1075 pPrg->aColmask[1] = pSubParse->newmask;
dan165921a2009-08-28 18:53:45 +00001076 sqlite3VdbeDelete(v);
dan165921a2009-08-28 18:53:45 +00001077 }
dan65a7cd12009-09-01 12:16:01 +00001078
dan65a7cd12009-09-01 12:16:01 +00001079 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
drhf30a9692013-11-15 01:10:18 +00001080 sqlite3ParserReset(pSubParse);
dan165921a2009-08-28 18:53:45 +00001081 sqlite3StackFree(db, pSubParse);
1082
dan2832ad42009-08-31 15:27:27 +00001083 return pPrg;
dan165921a2009-08-28 18:53:45 +00001084}
1085
dan65a7cd12009-09-01 12:16:01 +00001086/*
1087** Return a pointer to a TriggerPrg object containing the sub-program for
1088** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
1089** TriggerPrg object exists, a new object is allocated and populated before
1090** being returned.
1091*/
dan2832ad42009-08-31 15:27:27 +00001092static TriggerPrg *getRowTrigger(
dan65a7cd12009-09-01 12:16:01 +00001093 Parse *pParse, /* Current parse context */
dan165921a2009-08-28 18:53:45 +00001094 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +00001095 Table *pTab, /* The table trigger pTrigger is attached to */
1096 int orconf /* ON CONFLICT algorithm. */
dan165921a2009-08-28 18:53:45 +00001097){
dan65a7cd12009-09-01 12:16:01 +00001098 Parse *pRoot = sqlite3ParseToplevel(pParse);
dan2832ad42009-08-31 15:27:27 +00001099 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +00001100
dan1da40a32009-09-19 17:00:31 +00001101 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dan165921a2009-08-28 18:53:45 +00001102
1103 /* It may be that this trigger has already been coded (or is in the
1104 ** process of being coded). If this is the case, then an entry with
dan2832ad42009-08-31 15:27:27 +00001105 ** a matching TriggerPrg.pTrigger field will be present somewhere
1106 ** in the Parse.pTriggerPrg list. Search for such an entry. */
dan2832ad42009-08-31 15:27:27 +00001107 for(pPrg=pRoot->pTriggerPrg;
1108 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
1109 pPrg=pPrg->pNext
dan165921a2009-08-28 18:53:45 +00001110 );
1111
dan65a7cd12009-09-01 12:16:01 +00001112 /* If an existing TriggerPrg could not be located, create a new one. */
dan2832ad42009-08-31 15:27:27 +00001113 if( !pPrg ){
dan65a7cd12009-09-01 12:16:01 +00001114 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
dan165921a2009-08-28 18:53:45 +00001115 }
1116
dan2832ad42009-08-31 15:27:27 +00001117 return pPrg;
dan165921a2009-08-28 18:53:45 +00001118}
1119
dan94d7f502009-09-24 09:05:49 +00001120/*
1121** Generate code for the trigger program associated with trigger p on
1122** table pTab. The reg, orconf and ignoreJump parameters passed to this
1123** function are the same as those described in the header function for
1124** sqlite3CodeRowTrigger()
1125*/
dan1da40a32009-09-19 17:00:31 +00001126void sqlite3CodeRowTriggerDirect(
1127 Parse *pParse, /* Parse context */
1128 Trigger *p, /* Trigger to code */
1129 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001130 int reg, /* Reg array containing OLD.* and NEW.* values */
dan1da40a32009-09-19 17:00:31 +00001131 int orconf, /* ON CONFLICT policy */
1132 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
1133){
1134 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
1135 TriggerPrg *pPrg;
1136 pPrg = getRowTrigger(pParse, p, pTab, orconf);
1137 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
1138
1139 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
1140 ** is a pointer to the sub-vdbe containing the trigger program. */
1141 if( pPrg ){
dand19c9332010-07-26 12:05:17 +00001142 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
1143
drh9b34abe2016-01-16 15:12:35 +00001144 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
1145 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
dan1da40a32009-09-19 17:00:31 +00001146 VdbeComment(
1147 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
1148
1149 /* Set the P5 operand of the OP_Program instruction to non-zero if
1150 ** recursive invocation of this trigger program is disallowed. Recursive
1151 ** invocation is disallowed if (a) the sub-program is really a trigger,
1152 ** not a foreign key action, and (b) the flag to enable recursive triggers
1153 ** is clear. */
dand19c9332010-07-26 12:05:17 +00001154 sqlite3VdbeChangeP5(v, (u8)bRecursive);
dan1da40a32009-09-19 17:00:31 +00001155 }
1156}
1157
danielk1977633ed082002-05-17 00:05:58 +00001158/*
dan94d7f502009-09-24 09:05:49 +00001159** This is called to code the required FOR EACH ROW triggers for an operation
1160** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
drhf7b54962013-05-28 12:11:54 +00001161** is given by the op parameter. The tr_tm parameter determines whether the
dan94d7f502009-09-24 09:05:49 +00001162** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
1163** parameter pChanges is passed the list of columns being modified.
danielk1977633ed082002-05-17 00:05:58 +00001164**
dan94d7f502009-09-24 09:05:49 +00001165** If there are no triggers that fire at the specified time for the specified
1166** operation on pTab, this function is a no-op.
drhc977f7f2002-05-21 11:38:11 +00001167**
dan94d7f502009-09-24 09:05:49 +00001168** The reg argument is the address of the first in an array of registers
1169** that contain the values substituted for the new.* and old.* references
1170** in the trigger program. If N is the number of columns in table pTab
1171** (a copy of pTab->nCol), then registers are populated as follows:
drhc977f7f2002-05-21 11:38:11 +00001172**
dan94d7f502009-09-24 09:05:49 +00001173** Register Contains
1174** ------------------------------------------------------
1175** reg+0 OLD.rowid
1176** reg+1 OLD.* value of left-most column of pTab
1177** ... ...
1178** reg+N OLD.* value of right-most column of pTab
1179** reg+N+1 NEW.rowid
1180** reg+N+2 OLD.* value of left-most column of pTab
1181** ... ...
1182** reg+N+N+1 NEW.* value of right-most column of pTab
drhc977f7f2002-05-21 11:38:11 +00001183**
dan94d7f502009-09-24 09:05:49 +00001184** For ON DELETE triggers, the registers containing the NEW.* values will
1185** never be accessed by the trigger program, so they are not allocated or
1186** populated by the caller (there is no data to populate them with anyway).
1187** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
1188** are never accessed, and so are not allocated by the caller. So, for an
1189** ON INSERT trigger, the value passed to this function as parameter reg
1190** is not a readable register, although registers (reg+N) through
1191** (reg+N+N+1) are.
danielk1977633ed082002-05-17 00:05:58 +00001192**
dan94d7f502009-09-24 09:05:49 +00001193** Parameter orconf is the default conflict resolution algorithm for the
1194** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
1195** is the instruction that control should jump to if a trigger program
1196** raises an IGNORE exception.
danielk1977633ed082002-05-17 00:05:58 +00001197*/
dan165921a2009-08-28 18:53:45 +00001198void sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +00001199 Parse *pParse, /* Parse context */
danielk19772f886d12009-02-28 10:47:41 +00001200 Trigger *pTrigger, /* List of triggers on table pTab */
danielk1977633ed082002-05-17 00:05:58 +00001201 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
1202 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +00001203 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +00001204 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001205 int reg, /* The first in an array of registers (see above) */
danielk19776f349032002-06-11 02:25:40 +00001206 int orconf, /* ON CONFLICT policy */
dan165921a2009-08-28 18:53:45 +00001207 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
drhc977f7f2002-05-21 11:38:11 +00001208){
dan94d7f502009-09-24 09:05:49 +00001209 Trigger *p; /* Used to iterate through pTrigger list */
danielk19778f2c54e2008-01-01 19:02:09 +00001210
dan94d7f502009-09-24 09:05:49 +00001211 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
1212 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
1213 assert( (op==TK_UPDATE)==(pChanges!=0) );
danielk1977c3f9bad2002-05-15 08:30:12 +00001214
danielk19772f886d12009-02-28 10:47:41 +00001215 for(p=pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +00001216
drh9ab4c2e2009-05-09 00:18:38 +00001217 /* Sanity checking: The schema for the trigger and for the table are
1218 ** always defined. The trigger must be in the same schema as the table
1219 ** or else it must be a TEMP trigger. */
1220 assert( p->pSchema!=0 );
1221 assert( p->pTabSchema!=0 );
dan165921a2009-08-28 18:53:45 +00001222 assert( p->pSchema==p->pTabSchema
1223 || p->pSchema==pParse->db->aDb[1].pSchema );
drh9ab4c2e2009-05-09 00:18:38 +00001224
drh28828c52021-01-30 21:55:38 +00001225 /* Determine whether we should code this trigger. One of two choices:
1226 ** 1. The trigger is an exact match to the current DML statement
1227 ** 2. This is a RETURNING trigger for INSERT but we are currently
1228 ** doing the UPDATE part of an UPSERT.
1229 */
1230 if( (p->op==op || (p->bReturning && p->op==TK_INSERT && op==TK_UPDATE))
dan165921a2009-08-28 18:53:45 +00001231 && p->tr_tm==tr_tm
dan94d7f502009-09-24 09:05:49 +00001232 && checkColumnOverlap(p->pColumns, pChanges)
drh7baf3d42021-02-01 01:57:55 +00001233 && (sqlite3IsToplevel(pParse) || !p->bReturning)
danielk1977eecfb3e2006-01-10 12:31:39 +00001234 ){
drh28828c52021-01-30 21:55:38 +00001235 u8 origOp = p->op;
drh343256b2021-01-30 02:34:47 +00001236 p->op = op;
dan94d7f502009-09-24 09:05:49 +00001237 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
drh28828c52021-01-30 21:55:38 +00001238 p->op = origOp;
danielk1977c3f9bad2002-05-15 08:30:12 +00001239 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001240 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001241}
dan165921a2009-08-28 18:53:45 +00001242
dan2832ad42009-08-31 15:27:27 +00001243/*
danbb5f1682009-11-27 12:12:34 +00001244** Triggers may access values stored in the old.* or new.* pseudo-table.
1245** This function returns a 32-bit bitmask indicating which columns of the
1246** old.* or new.* tables actually are used by triggers. This information
1247** may be used by the caller, for example, to avoid having to load the entire
1248** old.* record into memory when executing an UPDATE or DELETE command.
dan2832ad42009-08-31 15:27:27 +00001249**
1250** Bit 0 of the returned mask is set if the left-most column of the
danbb5f1682009-11-27 12:12:34 +00001251** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
dan2832ad42009-08-31 15:27:27 +00001252** the second leftmost column value is required, and so on. If there
1253** are more than 32 columns in the table, and at least one of the columns
1254** with an index greater than 32 may be accessed, 0xffffffff is returned.
1255**
danbb5f1682009-11-27 12:12:34 +00001256** It is not possible to determine if the old.rowid or new.rowid column is
1257** accessed by triggers. The caller must always assume that it is.
dan2832ad42009-08-31 15:27:27 +00001258**
danbb5f1682009-11-27 12:12:34 +00001259** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
1260** applies to the old.* table. If 1, the new.* table.
1261**
1262** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
1263** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
1264** included in the returned mask if the TRIGGER_BEFORE bit is set in the
1265** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
1266** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
dan2832ad42009-08-31 15:27:27 +00001267*/
danbb5f1682009-11-27 12:12:34 +00001268u32 sqlite3TriggerColmask(
dan165921a2009-08-28 18:53:45 +00001269 Parse *pParse, /* Parse context */
1270 Trigger *pTrigger, /* List of triggers on table pTab */
dan165921a2009-08-28 18:53:45 +00001271 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
danbb5f1682009-11-27 12:12:34 +00001272 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
1273 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
dan165921a2009-08-28 18:53:45 +00001274 Table *pTab, /* The table to code triggers from */
dan2832ad42009-08-31 15:27:27 +00001275 int orconf /* Default ON CONFLICT policy for trigger steps */
dan165921a2009-08-28 18:53:45 +00001276){
dan1da40a32009-09-19 17:00:31 +00001277 const int op = pChanges ? TK_UPDATE : TK_DELETE;
dan2832ad42009-08-31 15:27:27 +00001278 u32 mask = 0;
dan165921a2009-08-28 18:53:45 +00001279 Trigger *p;
dan165921a2009-08-28 18:53:45 +00001280
danbb5f1682009-11-27 12:12:34 +00001281 assert( isNew==1 || isNew==0 );
dan165921a2009-08-28 18:53:45 +00001282 for(p=pTrigger; p; p=p->pNext){
danbb5f1682009-11-27 12:12:34 +00001283 if( p->op==op && (tr_tm&p->tr_tm)
1284 && checkColumnOverlap(p->pColumns,pChanges)
1285 ){
dan2832ad42009-08-31 15:27:27 +00001286 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +00001287 pPrg = getRowTrigger(pParse, p, pTab, orconf);
dan2832ad42009-08-31 15:27:27 +00001288 if( pPrg ){
danbb5f1682009-11-27 12:12:34 +00001289 mask |= pPrg->aColmask[isNew];
dan165921a2009-08-28 18:53:45 +00001290 }
1291 }
1292 }
dan2832ad42009-08-31 15:27:27 +00001293
1294 return mask;
dan165921a2009-08-28 18:53:45 +00001295}
1296
drhb7f91642004-10-31 02:22:47 +00001297#endif /* !defined(SQLITE_OMIT_TRIGGER) */