blob: 3b7d0d9e88c61742e744debcee508c80d997b25c [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
drhce5dd9e2022-04-07 20:45:38 +000055 assert( pParse->disableTriggers==0 );
drhf54a80f2021-01-29 13:47:36 +000056 pTmpSchema = pParse->db->aDb[1].pSchema;
57 p = sqliteHashFirst(&pTmpSchema->trigHash);
drh3765c032021-04-27 17:18:10 +000058 pList = pTab->pTrigger;
drha4767682021-04-27 13:04:18 +000059 while( p ){
60 Trigger *pTrig = (Trigger *)sqliteHashData(p);
61 if( pTrig->pTabSchema==pTab->pSchema
62 && pTrig->table
63 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
drh3765c032021-04-27 17:18:10 +000064 && pTrig->pTabSchema!=pTmpSchema
drha4767682021-04-27 13:04:18 +000065 ){
66 pTrig->pNext = pList;
67 pList = pTrig;
drh389e0562022-03-17 23:49:58 +000068 }else if( pTrig->op==TK_RETURNING ){
drha4767682021-04-27 13:04:18 +000069#ifndef SQLITE_OMIT_VIRTUALTABLE
drh389e0562022-03-17 23:49:58 +000070 assert( pParse->db->pVtabCtx==0 );
drha4767682021-04-27 13:04:18 +000071#endif
drha4767682021-04-27 13:04:18 +000072 assert( pParse->bReturning );
73 assert( &(pParse->u1.pReturning->retTrig) == pTrig );
74 pTrig->table = pTab->zName;
75 pTrig->pTabSchema = pTab->pSchema;
76 pTrig->pNext = pList;
77 pList = pTrig;
78 }
79 p = sqliteHashNext(p);
80 }
81#if 0
82 if( pList ){
83 Trigger *pX;
84 printf("Triggers for %s:", pTab->zName);
85 for(pX=pList; pX; pX=pX->pNext){
86 printf(" %s", pX->zName);
87 }
88 printf("\n");
89 fflush(stdout);
90 }
91#endif
drhf54a80f2021-01-29 13:47:36 +000092 return pList;
danielk19772f886d12009-02-28 10:47:41 +000093}
94
95/*
drhf0f258b2003-04-21 18:48:45 +000096** This is called by the parser when it sees a CREATE TRIGGER statement
97** up to the point of the BEGIN before the trigger actions. A Trigger
98** structure is generated based on the information available and stored
99** in pParse->pNewTrigger. After the trigger actions have been parsed, the
danielk19774adee202004-05-08 08:23:19 +0000100** sqlite3FinishTrigger() function is called to complete the trigger
drhf0f258b2003-04-21 18:48:45 +0000101** construction process.
drh9adf9ac2002-05-15 11:44:13 +0000102*/
danielk19774adee202004-05-08 08:23:19 +0000103void sqlite3BeginTrigger(
drh9adf9ac2002-05-15 11:44:13 +0000104 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
danielk1977ef2cb632004-05-29 02:37:19 +0000105 Token *pName1, /* The name of the trigger */
106 Token *pName2, /* The name of the trigger */
drh5cf590c2003-04-24 01:45:04 +0000107 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
drh9adf9ac2002-05-15 11:44:13 +0000108 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
danielk1977633ed082002-05-17 00:05:58 +0000109 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
drhd24cc422003-03-27 12:51:24 +0000110 SrcList *pTableName,/* The name of the table/view the trigger applies to */
drh9adf9ac2002-05-15 11:44:13 +0000111 Expr *pWhen, /* WHEN clause */
drhfdd48a72006-09-11 23:45:48 +0000112 int isTemp, /* True if the TEMPORARY keyword is present */
113 int noErr /* Suppress errors if the trigger already exists */
drh9adf9ac2002-05-15 11:44:13 +0000114){
drh3d5f74b2009-08-06 17:43:31 +0000115 Trigger *pTrigger = 0; /* The new trigger */
116 Table *pTab; /* Table that the trigger fires off of */
drhf0f258b2003-04-21 18:48:45 +0000117 char *zName = 0; /* Name of the trigger */
drh3d5f74b2009-08-06 17:43:31 +0000118 sqlite3 *db = pParse->db; /* The database connection */
danielk1977ef2cb632004-05-29 02:37:19 +0000119 int iDb; /* The database to store the trigger in */
120 Token *pName; /* The unqualified db name */
drh3d5f74b2009-08-06 17:43:31 +0000121 DbFixer sFix; /* State vector for the DB fixer */
drhed6c8672003-01-12 18:02:16 +0000122
drh43617e92006-03-06 20:55:46 +0000123 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
124 assert( pName2!=0 );
drhaa78bec2008-12-09 03:55:14 +0000125 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
126 assert( op>0 && op<0xff );
danielk1977ef2cb632004-05-29 02:37:19 +0000127 if( isTemp ){
128 /* If TEMP was specified, then the trigger name may not be qualified. */
drh43617e92006-03-06 20:55:46 +0000129 if( pName2->n>0 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000130 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
131 goto trigger_cleanup;
132 }
133 iDb = 1;
134 pName = pName1;
135 }else{
mistachkind5578432012-08-25 10:01:29 +0000136 /* Figure out the db that the trigger will be created in */
danielk1977ef2cb632004-05-29 02:37:19 +0000137 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
138 if( iDb<0 ){
139 goto trigger_cleanup;
140 }
141 }
drh6fea83e2011-07-01 13:50:44 +0000142 if( !pTableName || db->mallocFailed ){
143 goto trigger_cleanup;
144 }
145
146 /* A long-standing parser bug is that this syntax was allowed:
147 **
148 ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
149 ** ^^^^^^^^
150 **
151 ** To maintain backwards compatibility, ignore the database
drh346a70c2020-06-15 20:27:35 +0000152 ** name on pTableName if we are reparsing out of the schema table
drh6fea83e2011-07-01 13:50:44 +0000153 */
154 if( db->init.busy && iDb!=1 ){
155 sqlite3DbFree(db, pTableName->a[0].zDatabase);
156 pTableName->a[0].zDatabase = 0;
157 }
danielk1977ef2cb632004-05-29 02:37:19 +0000158
159 /* If the trigger name was unqualified, and the table is a temp table,
160 ** then set iDb to 1 to create the trigger in the temporary database.
161 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
162 ** exist, the error is caught by the block below.
drh9adf9ac2002-05-15 11:44:13 +0000163 */
danielk1977ef2cb632004-05-29 02:37:19 +0000164 pTab = sqlite3SrcListLookup(pParse, pTableName);
drh622d2882010-02-15 16:54:55 +0000165 if( db->init.busy==0 && pName2->n==0 && pTab
166 && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977ef2cb632004-05-29 02:37:19 +0000167 iDb = 1;
168 }
169
170 /* Ensure the table name matches database name and that the table exists */
drh17435752007-08-16 04:30:38 +0000171 if( db->mallocFailed ) goto trigger_cleanup;
drhd24cc422003-03-27 12:51:24 +0000172 assert( pTableName->nSrc==1 );
drhd100f692013-10-03 15:39:44 +0000173 sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
174 if( sqlite3FixSrcList(&sFix, pTableName) ){
drh4312db52003-06-03 01:47:11 +0000175 goto trigger_cleanup;
drhf26e09c2003-05-31 16:21:12 +0000176 }
danielk1977ef2cb632004-05-29 02:37:19 +0000177 pTab = sqlite3SrcListLookup(pParse, pTableName);
178 if( !pTab ){
179 /* The table does not exist. */
drh4e451aa2020-11-05 19:13:44 +0000180 goto trigger_orphan_error;
drhd24cc422003-03-27 12:51:24 +0000181 }
drh4cbdda92006-06-14 19:00:20 +0000182 if( IsVirtual(pTab) ){
183 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
drh4e451aa2020-11-05 19:13:44 +0000184 goto trigger_orphan_error;
drh4cbdda92006-06-14 19:00:20 +0000185 }
drhd24cc422003-03-27 12:51:24 +0000186
danielk1977d8123362004-06-12 09:25:12 +0000187 /* Check that the trigger name is not reserved and that no trigger of the
188 ** specified name exists */
drh17435752007-08-16 04:30:38 +0000189 zName = sqlite3NameFromToken(db, pName);
drhc5a93d42019-08-12 00:08:07 +0000190 if( zName==0 ){
191 assert( db->mallocFailed );
192 goto trigger_cleanup;
193 }
194 if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
danielk1977d8123362004-06-12 09:25:12 +0000195 goto trigger_cleanup;
196 }
drh21206082011-04-04 18:22:02 +0000197 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danc9461ec2018-08-29 21:00:16 +0000198 if( !IN_RENAME_OBJECT ){
dan5496d6a2018-08-13 17:14:26 +0000199 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
200 if( !noErr ){
201 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
202 }else{
203 assert( !db->init.busy );
204 sqlite3CodeVerifySchema(pParse, iDb);
205 }
206 goto trigger_cleanup;
drhfdd48a72006-09-11 23:45:48 +0000207 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000208 }
danielk1977ef2cb632004-05-29 02:37:19 +0000209
210 /* Do not create a trigger on a system table */
drhdca76842004-12-07 14:06:13 +0000211 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk19774adee202004-05-08 08:23:19 +0000212 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
drhd24cc422003-03-27 12:51:24 +0000213 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40 +0000214 }
danielk1977ef2cb632004-05-29 02:37:19 +0000215
216 /* INSTEAD of triggers are only for views and views only support INSTEAD
217 ** of triggers.
218 */
drhf38524d2021-08-02 16:41:57 +0000219 if( IsView(pTab) && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000220 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drha9799932021-03-19 13:00:28 +0000221 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName->a);
drh4e451aa2020-11-05 19:13:44 +0000222 goto trigger_orphan_error;
drhd24cc422003-03-27 12:51:24 +0000223 }
drhf38524d2021-08-02 16:41:57 +0000224 if( !IsView(pTab) && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000225 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drha9799932021-03-19 13:00:28 +0000226 " trigger on table: %S", pTableName->a);
drh4e451aa2020-11-05 19:13:44 +0000227 goto trigger_orphan_error;
drhd24cc422003-03-27 12:51:24 +0000228 }
danielk1977ef2cb632004-05-29 02:37:19 +0000229
drhd24cc422003-03-27 12:51:24 +0000230#ifndef SQLITE_OMIT_AUTHORIZATION
danc9461ec2018-08-29 21:00:16 +0000231 if( !IN_RENAME_OBJECT ){
drha0daa752016-09-16 11:53:10 +0000232 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drhd24cc422003-03-27 12:51:24 +0000233 int code = SQLITE_CREATE_TRIGGER;
drh69c33822016-08-18 14:33:11 +0000234 const char *zDb = db->aDb[iTabDb].zDbSName;
235 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
danielk1977da184232006-01-05 11:34:32 +0000236 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19 +0000237 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24 +0000238 goto trigger_cleanup;
239 }
danielk1977da184232006-01-05 11:34:32 +0000240 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24 +0000241 goto trigger_cleanup;
242 }
243 }
244#endif
danielk1977d702fcc2002-05-26 23:24:40 +0000245
danielk1977ef2cb632004-05-29 02:37:19 +0000246 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04 +0000247 ** cannot appear on views. So we might as well translate every
248 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
249 ** elsewhere.
250 */
danielk1977d702fcc2002-05-26 23:24:40 +0000251 if (tr_tm == TK_INSTEAD){
252 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12 +0000253 }
254
255 /* Build the Trigger object */
drh17435752007-08-16 04:30:38 +0000256 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19 +0000257 if( pTrigger==0 ) goto trigger_cleanup;
dan165921a2009-08-28 18:53:45 +0000258 pTrigger->zName = zName;
drhe5f9c642003-01-13 23:27:31 +0000259 zName = 0;
drh17435752007-08-16 04:30:38 +0000260 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32 +0000261 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48 +0000262 pTrigger->pTabSchema = pTab->pSchema;
drhaa78bec2008-12-09 03:55:14 +0000263 pTrigger->op = (u8)op;
drhdca76842004-12-07 14:06:13 +0000264 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
danc9461ec2018-08-29 21:00:16 +0000265 if( IN_RENAME_OBJECT ){
266 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
dan5496d6a2018-08-13 17:14:26 +0000267 pTrigger->pWhen = pWhen;
268 pWhen = 0;
269 }else{
270 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
271 }
272 pTrigger->pColumns = pColumns;
273 pColumns = 0;
drhf0f258b2003-04-21 18:48:45 +0000274 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19 +0000275 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45 +0000276
277trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000278 sqlite3DbFree(db, zName);
279 sqlite3SrcListDelete(db, pTableName);
280 sqlite3IdListDelete(db, pColumns);
281 sqlite3ExprDelete(db, pWhen);
danielk1977d5d56522005-03-16 12:15:20 +0000282 if( !pParse->pNewTrigger ){
drh633e6d52008-07-28 19:34:53 +0000283 sqlite3DeleteTrigger(db, pTrigger);
danielk1977d5d56522005-03-16 12:15:20 +0000284 }else{
285 assert( pParse->pNewTrigger==pTrigger );
286 }
drh4e451aa2020-11-05 19:13:44 +0000287 return;
288
289trigger_orphan_error:
290 if( db->init.iDb==1 ){
291 /* Ticket #3810.
292 ** Normally, whenever a table is dropped, all associated triggers are
293 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
294 ** and the table is dropped by a different database connection, the
295 ** trigger is not visible to the database connection that does the
296 ** drop so the trigger cannot be dropped. This results in an
297 ** "orphaned trigger" - a trigger whose associated table is missing.
298 **
299 ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df
300 */
301 db->init.orphanTrigger = 1;
302 }
303 goto trigger_cleanup;
drhf0f258b2003-04-21 18:48:45 +0000304}
305
306/*
307** This routine is called after all of the trigger actions have been parsed
308** in order to complete the process of building the trigger.
309*/
danielk19774adee202004-05-08 08:23:19 +0000310void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45 +0000311 Parse *pParse, /* Parser context */
312 TriggerStep *pStepList, /* The triggered program */
313 Token *pAll /* Token that describes the complete CREATE TRIGGER */
314){
drha8c62df2010-02-15 15:47:18 +0000315 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
316 char *zName; /* Name of trigger */
317 sqlite3 *db = pParse->db; /* The database */
318 DbFixer sFix; /* Fixer object */
319 int iDb; /* Database containing the trigger */
320 Token nameToken; /* Trigger name for error reporting */
drhf0f258b2003-04-21 18:48:45 +0000321
drhf0f258b2003-04-21 18:48:45 +0000322 pParse->pNewTrigger = 0;
drh9ab4c2e2009-05-09 00:18:38 +0000323 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
dan165921a2009-08-28 18:53:45 +0000324 zName = pTrig->zName;
danielk1977da184232006-01-05 11:34:32 +0000325 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32 +0000326 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53 +0000327 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32 +0000328 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53 +0000329 pStepList = pStepList->pNext;
330 }
drh40aced52016-01-22 17:48:09 +0000331 sqlite3TokenInit(&nameToken, pTrig->zName);
drhd100f692013-10-03 15:39:44 +0000332 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
333 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
dan46539d72013-10-03 12:29:38 +0000334 || sqlite3FixExpr(&sFix, pTrig->pWhen)
drhd100f692013-10-03 15:39:44 +0000335 ){
drhf26e09c2003-05-31 16:21:12 +0000336 goto triggerfinish_cleanup;
337 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000338
dan5496d6a2018-08-13 17:14:26 +0000339#ifndef SQLITE_OMIT_ALTERTABLE
danc9461ec2018-08-29 21:00:16 +0000340 if( IN_RENAME_OBJECT ){
dan5496d6a2018-08-13 17:14:26 +0000341 assert( !db->init.busy );
342 pParse->pNewTrigger = pTrig;
343 pTrig = 0;
344 }else
345#endif
346
drha8c62df2010-02-15 15:47:18 +0000347 /* if we are not initializing,
drh1e32bed2020-06-19 13:33:53 +0000348 ** build the sqlite_schema entry
drh9adf9ac2002-05-15 11:44:13 +0000349 */
drh1d85d932004-02-14 23:05:52 +0000350 if( !db->init.busy ){
drhc977f7f2002-05-21 11:38:11 +0000351 Vdbe *v;
drh2d401ab2008-01-10 23:50:11 +0000352 char *z;
danielk1977c3f9bad2002-05-15 08:30:12 +0000353
drh1e32bed2020-06-19 13:33:53 +0000354 /* Make an entry in the sqlite_schema table */
danielk19774adee202004-05-08 08:23:19 +0000355 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000356 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000357 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11 +0000358 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
drhd96cc6f2017-06-08 14:35:21 +0000359 testcase( z==0 );
drh2d401ab2008-01-10 23:50:11 +0000360 sqlite3NestedParse(pParse,
drha4a871c2021-11-04 14:04:20 +0000361 "INSERT INTO %Q." LEGACY_SCHEMA_TABLE
drh346a70c2020-06-15 20:27:35 +0000362 " VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
363 db->aDb[iDb].zDbSName, zName,
drh2d401ab2008-01-10 23:50:11 +0000364 pTrig->table, z);
drh633e6d52008-07-28 19:34:53 +0000365 sqlite3DbFree(db, z);
drh9cbf3422008-01-17 16:22:13 +0000366 sqlite3ChangeCookie(pParse, iDb);
drh5d9c9da2011-06-03 20:11:17 +0000367 sqlite3VdbeAddParseSchemaOp(v, iDb,
dan6a5a13d2021-02-17 20:08:22 +0000368 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName), 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000369 }
370
drh956bc922004-07-24 17:38:29 +0000371 if( db->init.busy ){
danielk19772f886d12009-02-28 10:47:41 +0000372 Trigger *pLink = pTrig;
373 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
drh21206082011-04-04 18:22:02 +0000374 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh55f66b32019-07-16 19:44:32 +0000375 assert( pLink!=0 );
drhacbcb7e2014-08-21 20:26:37 +0000376 pTrig = sqlite3HashInsert(pHash, zName, pTrig);
danielk19772f886d12009-02-28 10:47:41 +0000377 if( pTrig ){
drh4a642b62016-02-05 01:55:27 +0000378 sqlite3OomFault(db);
danielk19772f886d12009-02-28 10:47:41 +0000379 }else if( pLink->pSchema==pLink->pTabSchema ){
380 Table *pTab;
drhacbcb7e2014-08-21 20:26:37 +0000381 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
danielk19772f886d12009-02-28 10:47:41 +0000382 assert( pTab!=0 );
383 pLink->pNext = pTab->pTrigger;
384 pTab->pTrigger = pLink;
danielk1977d5d56522005-03-16 12:15:20 +0000385 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000386 }
387
drhf0f258b2003-04-21 18:48:45 +0000388triggerfinish_cleanup:
drh633e6d52008-07-28 19:34:53 +0000389 sqlite3DeleteTrigger(db, pTrig);
danc9461ec2018-08-29 21:00:16 +0000390 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
drh633e6d52008-07-28 19:34:53 +0000391 sqlite3DeleteTriggerStep(db, pStepList);
drh4b59ab52002-08-24 18:24:51 +0000392}
danielk1977c3f9bad2002-05-15 08:30:12 +0000393
drh4b59ab52002-08-24 18:24:51 +0000394/*
drhf259df52017-12-27 20:38:35 +0000395** Duplicate a range of text from an SQL statement, then convert all
396** whitespace characters into ordinary space characters.
397*/
398static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
399 char *z = sqlite3DbSpanDup(db, zStart, zEnd);
400 int i;
401 if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i]) ) z[i] = ' ';
402 return z;
403}
404
405/*
drhc977f7f2002-05-21 11:38:11 +0000406** Turn a SELECT statement (that the pSelect parameter points to) into
407** a trigger step. Return a pointer to a TriggerStep structure.
408**
409** The parser calls this routine when it finds a SELECT statement in
410** body of a TRIGGER.
411*/
drhf259df52017-12-27 20:38:35 +0000412TriggerStep *sqlite3TriggerSelectStep(
413 sqlite3 *db, /* Database connection */
414 Select *pSelect, /* The SELECT statement */
415 const char *zStart, /* Start of SQL text */
416 const char *zEnd /* End of SQL text */
417){
drh17435752007-08-16 04:30:38 +0000418 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000419 if( pTriggerStep==0 ) {
drh633e6d52008-07-28 19:34:53 +0000420 sqlite3SelectDelete(db, pSelect);
danielk19772e588c72005-12-09 14:25:08 +0000421 return 0;
422 }
danielk1977633ed082002-05-17 00:05:58 +0000423 pTriggerStep->op = TK_SELECT;
424 pTriggerStep->pSelect = pSelect;
425 pTriggerStep->orconf = OE_Default;
drhf259df52017-12-27 20:38:35 +0000426 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
drhb7916a72009-05-27 10:31:29 +0000427 return pTriggerStep;
428}
danielk1977c3f9bad2002-05-15 08:30:12 +0000429
drhb7916a72009-05-27 10:31:29 +0000430/*
431** Allocate space to hold a new trigger step. The allocated space
432** holds both the TriggerStep object and the TriggerStep.target.z string.
433**
434** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
435*/
436static TriggerStep *triggerStepAllocate(
danc9461ec2018-08-29 21:00:16 +0000437 Parse *pParse, /* Parser context */
shane5eff7cf2009-08-10 03:57:58 +0000438 u8 op, /* Trigger opcode */
drhf259df52017-12-27 20:38:35 +0000439 Token *pName, /* The target name */
440 const char *zStart, /* Start of SQL text */
441 const char *zEnd /* End of SQL text */
drhb7916a72009-05-27 10:31:29 +0000442){
danc9461ec2018-08-29 21:00:16 +0000443 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000444 TriggerStep *pTriggerStep;
445
drh4c460bb2022-03-07 16:22:31 +0000446 if( pParse->nErr ) return 0;
dan46408352015-04-21 16:38:49 +0000447 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
drhb7916a72009-05-27 10:31:29 +0000448 if( pTriggerStep ){
449 char *z = (char*)&pTriggerStep[1];
450 memcpy(z, pName->z, pName->n);
dan46408352015-04-21 16:38:49 +0000451 sqlite3Dequote(z);
452 pTriggerStep->zTarget = z;
drhb7916a72009-05-27 10:31:29 +0000453 pTriggerStep->op = op;
drhf259df52017-12-27 20:38:35 +0000454 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
danc9461ec2018-08-29 21:00:16 +0000455 if( IN_RENAME_OBJECT ){
456 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
457 }
drhb7916a72009-05-27 10:31:29 +0000458 }
danielk1977633ed082002-05-17 00:05:58 +0000459 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000460}
461
drhc977f7f2002-05-21 11:38:11 +0000462/*
463** Build a trigger step out of an INSERT statement. Return a pointer
464** to the new trigger step.
465**
466** The parser calls this routine when it sees an INSERT inside the
467** body of a trigger.
468*/
danielk19774adee202004-05-08 08:23:19 +0000469TriggerStep *sqlite3TriggerInsertStep(
dan5be60c52018-08-15 20:28:39 +0000470 Parse *pParse, /* Parser */
drhc977f7f2002-05-21 11:38:11 +0000471 Token *pTableName, /* Name of the table into which we insert */
472 IdList *pColumn, /* List of columns in pTableName to insert into */
drhc977f7f2002-05-21 11:38:11 +0000473 Select *pSelect, /* A SELECT statement that supplies values */
drhf259df52017-12-27 20:38:35 +0000474 u8 orconf, /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
drh46d2e5c2018-04-12 13:15:43 +0000475 Upsert *pUpsert, /* ON CONFLICT clauses for upsert */
drhf259df52017-12-27 20:38:35 +0000476 const char *zStart, /* Start of SQL text */
477 const char *zEnd /* End of SQL text */
danielk1977633ed082002-05-17 00:05:58 +0000478){
dan5be60c52018-08-15 20:28:39 +0000479 sqlite3 *db = pParse->db;
drhf3a65f72007-08-22 20:18:21 +0000480 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000481
drh75593d92014-01-10 20:46:55 +0000482 assert(pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000483
danc9461ec2018-08-29 21:00:16 +0000484 pTriggerStep = triggerStepAllocate(pParse, TK_INSERT, pTableName,zStart,zEnd);
danielk1977d5d56522005-03-16 12:15:20 +0000485 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000486 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000487 pTriggerStep->pSelect = pSelect;
488 pSelect = 0;
489 }else{
490 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
491 }
danielk1977d5d56522005-03-16 12:15:20 +0000492 pTriggerStep->pIdList = pColumn;
drh46d2e5c2018-04-12 13:15:43 +0000493 pTriggerStep->pUpsert = pUpsert;
danielk1977d5d56522005-03-16 12:15:20 +0000494 pTriggerStep->orconf = orconf;
dan9105fd52019-08-19 17:26:32 +0000495 if( pUpsert ){
496 sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
497 }
danielk1977d5d56522005-03-16 12:15:20 +0000498 }else{
drh99160482018-04-18 01:34:39 +0000499 testcase( pColumn );
drh633e6d52008-07-28 19:34:53 +0000500 sqlite3IdListDelete(db, pColumn);
drh99160482018-04-18 01:34:39 +0000501 testcase( pUpsert );
drh46d2e5c2018-04-12 13:15:43 +0000502 sqlite3UpsertDelete(db, pUpsert);
danielk1977d5d56522005-03-16 12:15:20 +0000503 }
drh33e619f2009-05-28 01:00:55 +0000504 sqlite3SelectDelete(db, pSelect);
danielk1977c3f9bad2002-05-15 08:30:12 +0000505
danielk1977633ed082002-05-17 00:05:58 +0000506 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000507}
508
drhc977f7f2002-05-21 11:38:11 +0000509/*
510** Construct a trigger step that implements an UPDATE statement and return
511** a pointer to that trigger step. The parser calls this routine when it
512** sees an UPDATE statement inside the body of a CREATE TRIGGER.
513*/
danielk19774adee202004-05-08 08:23:19 +0000514TriggerStep *sqlite3TriggerUpdateStep(
dan5be60c52018-08-15 20:28:39 +0000515 Parse *pParse, /* Parser */
drhc977f7f2002-05-21 11:38:11 +0000516 Token *pTableName, /* Name of the table to be updated */
drh26c4df02022-05-26 17:33:42 +0000517 SrcList *pFrom, /* FROM clause for an UPDATE-FROM, or NULL */
drhc977f7f2002-05-21 11:38:11 +0000518 ExprList *pEList, /* The SET clause: list of column and new values */
519 Expr *pWhere, /* The WHERE clause */
drhf259df52017-12-27 20:38:35 +0000520 u8 orconf, /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
521 const char *zStart, /* Start of SQL text */
522 const char *zEnd /* End of SQL text */
drhc977f7f2002-05-21 11:38:11 +0000523){
dan5be60c52018-08-15 20:28:39 +0000524 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000525 TriggerStep *pTriggerStep;
526
danc9461ec2018-08-29 21:00:16 +0000527 pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE, pTableName,zStart,zEnd);
drh33e619f2009-05-28 01:00:55 +0000528 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000529 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000530 pTriggerStep->pExprList = pEList;
531 pTriggerStep->pWhere = pWhere;
dane7877b22020-07-14 19:51:01 +0000532 pTriggerStep->pFrom = pFrom;
dan5be60c52018-08-15 20:28:39 +0000533 pEList = 0;
534 pWhere = 0;
dane7877b22020-07-14 19:51:01 +0000535 pFrom = 0;
dan5be60c52018-08-15 20:28:39 +0000536 }else{
537 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
538 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
dane7877b22020-07-14 19:51:01 +0000539 pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE);
dan5be60c52018-08-15 20:28:39 +0000540 }
drh33e619f2009-05-28 01:00:55 +0000541 pTriggerStep->orconf = orconf;
drh0e3a6f32007-03-30 20:40:34 +0000542 }
drh33e619f2009-05-28 01:00:55 +0000543 sqlite3ExprListDelete(db, pEList);
544 sqlite3ExprDelete(db, pWhere);
dane7877b22020-07-14 19:51:01 +0000545 sqlite3SrcListDelete(db, pFrom);
danielk1977633ed082002-05-17 00:05:58 +0000546 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000547}
548
drhc977f7f2002-05-21 11:38:11 +0000549/*
550** Construct a trigger step that implements a DELETE statement and return
551** a pointer to that trigger step. The parser calls this routine when it
552** sees a DELETE statement inside the body of a CREATE TRIGGER.
553*/
drh17435752007-08-16 04:30:38 +0000554TriggerStep *sqlite3TriggerDeleteStep(
dan5be60c52018-08-15 20:28:39 +0000555 Parse *pParse, /* Parser */
drh17435752007-08-16 04:30:38 +0000556 Token *pTableName, /* The table from which rows are deleted */
drhf259df52017-12-27 20:38:35 +0000557 Expr *pWhere, /* The WHERE clause */
558 const char *zStart, /* Start of SQL text */
559 const char *zEnd /* End of SQL text */
drh17435752007-08-16 04:30:38 +0000560){
dan5be60c52018-08-15 20:28:39 +0000561 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000562 TriggerStep *pTriggerStep;
563
danc9461ec2018-08-29 21:00:16 +0000564 pTriggerStep = triggerStepAllocate(pParse, TK_DELETE, pTableName,zStart,zEnd);
drh33e619f2009-05-28 01:00:55 +0000565 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000566 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000567 pTriggerStep->pWhere = pWhere;
568 pWhere = 0;
569 }else{
570 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
571 }
drh33e619f2009-05-28 01:00:55 +0000572 pTriggerStep->orconf = OE_Default;
drhb63a53d2007-03-31 01:34:44 +0000573 }
drh33e619f2009-05-28 01:00:55 +0000574 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000575 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000576}
577
danielk1977633ed082002-05-17 00:05:58 +0000578/*
579** Recursively delete a Trigger structure
580*/
drh633e6d52008-07-28 19:34:53 +0000581void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
drh658f0a32021-01-30 02:43:26 +0000582 if( pTrigger==0 || pTrigger->bReturning ) return;
drh633e6d52008-07-28 19:34:53 +0000583 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
dan165921a2009-08-28 18:53:45 +0000584 sqlite3DbFree(db, pTrigger->zName);
drh633e6d52008-07-28 19:34:53 +0000585 sqlite3DbFree(db, pTrigger->table);
586 sqlite3ExprDelete(db, pTrigger->pWhen);
587 sqlite3IdListDelete(db, pTrigger->pColumns);
drh633e6d52008-07-28 19:34:53 +0000588 sqlite3DbFree(db, pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000589}
590
591/*
danielk19778a414492004-06-29 08:59:35 +0000592** This function is called to drop a trigger from the database schema.
593**
594** This may be called directly from the parser and therefore identifies
595** the trigger by name. The sqlite3DropTriggerPtr() routine does the
596** same job as this routine except it takes a pointer to the trigger
597** instead of the trigger name.
598**/
drhfdd48a72006-09-11 23:45:48 +0000599void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000600 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000601 int i;
602 const char *zDb;
603 const char *zName;
drh9bb575f2004-09-06 17:24:11 +0000604 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000605
drh17435752007-08-16 04:30:38 +0000606 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000607 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000608 goto drop_trigger_cleanup;
609 }
danielk19778e227872004-06-07 07:52:17 +0000610
drhd24cc422003-03-27 12:51:24 +0000611 assert( pName->nSrc==1 );
612 zDb = pName->a[0].zDatabase;
613 zName = pName->a[0].zName;
drh21206082011-04-04 18:22:02 +0000614 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
danielk197753c0f742005-03-29 03:10:59 +0000615 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000616 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
dan465c2b82020-03-21 15:10:40 +0000617 if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
drh21206082011-04-04 18:22:02 +0000618 assert( sqlite3SchemaMutexHeld(db, j, 0) );
drhacbcb7e2014-08-21 20:26:37 +0000619 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
drhd24cc422003-03-27 12:51:24 +0000620 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000621 }
drhd24cc422003-03-27 12:51:24 +0000622 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000623 if( !noErr ){
drha9799932021-03-19 13:00:28 +0000624 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName->a);
dan57966752011-04-09 17:32:58 +0000625 }else{
626 sqlite3CodeVerifyNamedSchema(pParse, zDb);
drhfdd48a72006-09-11 23:45:48 +0000627 }
dan1db95102010-06-28 10:15:19 +0000628 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +0000629 goto drop_trigger_cleanup;
630 }
drh74161702006-02-24 02:53:49 +0000631 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000632
633drop_trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000634 sqlite3SrcListDelete(db, pName);
drh79a519c2003-05-17 19:04:03 +0000635}
636
637/*
drh956bc922004-07-24 17:38:29 +0000638** Return a pointer to the Table structure for the table that a trigger
639** is set on.
640*/
drh74161702006-02-24 02:53:49 +0000641static Table *tableOfTrigger(Trigger *pTrigger){
drhacbcb7e2014-08-21 20:26:37 +0000642 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
drh956bc922004-07-24 17:38:29 +0000643}
644
645
646/*
drh74161702006-02-24 02:53:49 +0000647** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000648*/
drh74161702006-02-24 02:53:49 +0000649void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000650 Table *pTable;
651 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000652 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000653 int iDb;
drh79a519c2003-05-17 19:04:03 +0000654
danielk1977da184232006-01-05 11:34:32 +0000655 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000656 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000657 pTable = tableOfTrigger(pTrigger);
drh6397a782019-08-27 10:05:45 +0000658 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000659#ifndef SQLITE_OMIT_AUTHORIZATION
drh6397a782019-08-27 10:05:45 +0000660 if( pTable ){
drhe5f9c642003-01-13 23:27:31 +0000661 int code = SQLITE_DROP_TRIGGER;
drh69c33822016-08-18 14:33:11 +0000662 const char *zDb = db->aDb[iDb].zDbSName;
drh956bc922004-07-24 17:38:29 +0000663 const char *zTab = SCHEMA_TABLE(iDb);
664 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
dan2bd93512009-08-31 08:22:46 +0000665 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
danielk19774adee202004-05-08 08:23:19 +0000666 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000667 return;
drhe5f9c642003-01-13 23:27:31 +0000668 }
drhed6c8672003-01-12 18:02:16 +0000669 }
drhe5f9c642003-01-13 23:27:31 +0000670#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000671
drhf0f258b2003-04-21 18:48:45 +0000672 /* Generate code to destroy the database record of the trigger.
673 */
drh43617e92006-03-06 20:55:46 +0000674 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drh8c8dddc2015-12-09 16:26:38 +0000675 sqlite3NestedParse(pParse,
drha4a871c2021-11-04 14:04:20 +0000676 "DELETE FROM %Q." LEGACY_SCHEMA_TABLE " WHERE name=%Q AND type='trigger'",
drh346a70c2020-06-15 20:27:35 +0000677 db->aDb[iDb].zDbSName, pTrigger->zName
drh8c8dddc2015-12-09 16:26:38 +0000678 );
drh9cbf3422008-01-17 16:22:13 +0000679 sqlite3ChangeCookie(pParse, iDb);
dan165921a2009-08-28 18:53:45 +0000680 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
drhf0f258b2003-04-21 18:48:45 +0000681 }
drh956bc922004-07-24 17:38:29 +0000682}
drhf0f258b2003-04-21 18:48:45 +0000683
drh956bc922004-07-24 17:38:29 +0000684/*
685** Remove a trigger from the hash tables of the sqlite* pointer.
686*/
687void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
688 Trigger *pTrigger;
drh21206082011-04-04 18:22:02 +0000689 Hash *pHash;
690
691 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
692 pHash = &(db->aDb[iDb].pSchema->trigHash);
drhacbcb7e2014-08-21 20:26:37 +0000693 pTrigger = sqlite3HashInsert(pHash, zName, 0);
drh9ab4c2e2009-05-09 00:18:38 +0000694 if( ALWAYS(pTrigger) ){
danielk19772f886d12009-02-28 10:47:41 +0000695 if( pTrigger->pSchema==pTrigger->pTabSchema ){
696 Table *pTab = tableOfTrigger(pTrigger);
drh6397a782019-08-27 10:05:45 +0000697 if( pTab ){
698 Trigger **pp;
dan0232dad2019-12-03 03:34:06 +0000699 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
700 if( *pp==pTrigger ){
701 *pp = (*pp)->pNext;
702 break;
703 }
704 }
drh6397a782019-08-27 10:05:45 +0000705 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000706 }
drh633e6d52008-07-28 19:34:53 +0000707 sqlite3DeleteTrigger(db, pTrigger);
drh8257aa82017-07-26 19:59:13 +0000708 db->mDbFlags |= DBFLAG_SchemaChange;
danielk1977c3f9bad2002-05-15 08:30:12 +0000709 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000710}
711
drhc977f7f2002-05-21 11:38:11 +0000712/*
713** pEList is the SET clause of an UPDATE statement. Each entry
714** in pEList is of the format <id>=<expr>. If any of the entries
715** in pEList have an <id> which matches an identifier in pIdList,
716** then return TRUE. If pIdList==NULL, then it is considered a
717** wildcard that matches anything. Likewise if pEList==NULL then
718** it matches anything so always return true. Return false only
719** if there is no match.
720*/
drh9ab4c2e2009-05-09 00:18:38 +0000721static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000722 int e;
drh9ab4c2e2009-05-09 00:18:38 +0000723 if( pIdList==0 || NEVER(pEList==0) ) return 1;
drhad2d8302002-05-24 20:31:36 +0000724 for(e=0; e<pEList->nExpr; e++){
drh41cee662019-12-12 20:22:34 +0000725 if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000726 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000727 return 0;
728}
729
danielk1977c3f9bad2002-05-15 08:30:12 +0000730/*
drha7441672022-04-07 14:03:07 +0000731** Return true if any TEMP triggers exist
732*/
733static int tempTriggersExist(sqlite3 *db){
drhce5dd9e2022-04-07 20:45:38 +0000734 if( NEVER(db->aDb[1].pSchema==0) ) return 0;
drha7441672022-04-07 14:03:07 +0000735 if( sqliteHashFirst(&db->aDb[1].pSchema->trigHash)==0 ) return 0;
736 return 1;
737}
738
739/*
danielk19772f886d12009-02-28 10:47:41 +0000740** Return a list of all triggers on table pTab if there exists at least
741** one trigger that must be fired when an operation of type 'op' is
742** performed on the table, and, if that operation is an UPDATE, if at
743** least one of the columns in pChanges is being modified.
drhdca76842004-12-07 14:06:13 +0000744*/
drha7441672022-04-07 14:03:07 +0000745static SQLITE_NOINLINE Trigger *triggersReallyExist(
danielk19772f886d12009-02-28 10:47:41 +0000746 Parse *pParse, /* Parse context */
drhdca76842004-12-07 14:06:13 +0000747 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000748 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
danielk19772f886d12009-02-28 10:47:41 +0000749 ExprList *pChanges, /* Columns that change in an UPDATE statement */
750 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
drhc977f7f2002-05-21 11:38:11 +0000751){
drhdca76842004-12-07 14:06:13 +0000752 int mask = 0;
drhe83cafd2011-03-21 17:15:58 +0000753 Trigger *pList = 0;
danielk19772f886d12009-02-28 10:47:41 +0000754 Trigger *p;
drhe83cafd2011-03-21 17:15:58 +0000755
drh2aa41c82021-02-03 00:55:34 +0000756 pList = sqlite3TriggerList(pParse, pTab);
drhc9be8632021-02-02 00:16:15 +0000757 assert( pList==0 || IsVirtual(pTab)==0
758 || (pList->bReturning && pList->pNext==0) );
drh2aa41c82021-02-03 00:55:34 +0000759 if( pList!=0 ){
760 p = pList;
761 if( (pParse->db->flags & SQLITE_EnableTrigger)==0
762 && pTab->pTrigger!=0
763 ){
764 /* The SQLITE_DBCONFIG_ENABLE_TRIGGER setting is off. That means that
765 ** only TEMP triggers are allowed. Truncate the pList so that it
766 ** includes only TEMP triggers */
767 if( pList==pTab->pTrigger ){
768 pList = 0;
769 goto exit_triggers_exist;
drh709dd132021-02-02 12:01:22 +0000770 }
drh2aa41c82021-02-03 00:55:34 +0000771 while( ALWAYS(p->pNext) && p->pNext!=pTab->pTrigger ) p = p->pNext;
772 p->pNext = 0;
773 p = pList;
danielk1977c3f9bad2002-05-15 08:30:12 +0000774 }
drh2aa41c82021-02-03 00:55:34 +0000775 do{
776 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
777 mask |= p->tr_tm;
778 }else if( p->op==TK_RETURNING ){
779 /* The first time a RETURNING trigger is seen, the "op" value tells
780 ** us what time of trigger it should be. */
781 assert( sqlite3IsToplevel(pParse) );
782 p->op = op;
drh7dec8042021-02-04 22:59:19 +0000783 if( IsVirtual(pTab) ){
784 if( op!=TK_INSERT ){
785 sqlite3ErrorMsg(pParse,
786 "%s RETURNING is not available on virtual tables",
787 op==TK_DELETE ? "DELETE" : "UPDATE");
788 }
789 p->tr_tm = TRIGGER_BEFORE;
790 }else{
791 p->tr_tm = TRIGGER_AFTER;
drh2aa41c82021-02-03 00:55:34 +0000792 }
drh7dec8042021-02-04 22:59:19 +0000793 mask |= p->tr_tm;
drh2aa41c82021-02-03 00:55:34 +0000794 }else if( p->bReturning && p->op==TK_INSERT && op==TK_UPDATE
795 && sqlite3IsToplevel(pParse) ){
796 /* Also fire a RETURNING trigger for an UPSERT */
drh7dec8042021-02-04 22:59:19 +0000797 mask |= p->tr_tm;
drh2aa41c82021-02-03 00:55:34 +0000798 }
799 p = p->pNext;
800 }while( p );
danielk1977c3f9bad2002-05-15 08:30:12 +0000801 }
drh2aa41c82021-02-03 00:55:34 +0000802exit_triggers_exist:
danielk19772f886d12009-02-28 10:47:41 +0000803 if( pMask ){
804 *pMask = mask;
805 }
806 return (mask ? pList : 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000807}
drha7441672022-04-07 14:03:07 +0000808Trigger *sqlite3TriggersExist(
809 Parse *pParse, /* Parse context */
810 Table *pTab, /* The table the contains the triggers */
811 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
812 ExprList *pChanges, /* Columns that change in an UPDATE statement */
813 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
814){
815 assert( pTab!=0 );
816 if( (pTab->pTrigger==0 && !tempTriggersExist(pParse->db))
817 || pParse->disableTriggers
818 ){
819 if( pMask ) *pMask = 0;
820 return 0;
821 }
822 return triggersReallyExist(pParse,pTab,op,pChanges,pMask);
823}
danielk1977c3f9bad2002-05-15 08:30:12 +0000824
drhc977f7f2002-05-21 11:38:11 +0000825/*
dan46408352015-04-21 16:38:49 +0000826** Convert the pStep->zTarget string into a SrcList and return a pointer
drhf26e09c2003-05-31 16:21:12 +0000827** to that SrcList.
828**
829** This routine adds a specific database name, if needed, to the target when
830** forming the SrcList. This prevents a trigger in one database from
831** referring to a target in another database. An exception is when the
832** trigger is in TEMP in which case it can refer to any other database it
833** wants.
834*/
dane7877b22020-07-14 19:51:01 +0000835SrcList *sqlite3TriggerStepSrc(
drhf26e09c2003-05-31 16:21:12 +0000836 Parse *pParse, /* The parsing context */
837 TriggerStep *pStep /* The trigger containing the target token */
838){
dan46408352015-04-21 16:38:49 +0000839 sqlite3 *db = pParse->db;
dane7877b22020-07-14 19:51:01 +0000840 SrcList *pSrc; /* SrcList to be returned */
841 char *zName = sqlite3DbStrDup(db, pStep->zTarget);
drh29c992c2019-01-17 15:40:41 +0000842 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
dane7877b22020-07-14 19:51:01 +0000843 assert( pSrc==0 || pSrc->nSrc==1 );
844 assert( zName || pSrc==0 );
drhb7916a72009-05-27 10:31:29 +0000845 if( pSrc ){
dane7877b22020-07-14 19:51:01 +0000846 Schema *pSchema = pStep->pTrig->pSchema;
847 pSrc->a[0].zName = zName;
848 if( pSchema!=db->aDb[1].pSchema ){
849 pSrc->a[0].pSchema = pSchema;
drhb7916a72009-05-27 10:31:29 +0000850 }
dane7877b22020-07-14 19:51:01 +0000851 if( pStep->pFrom ){
852 SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0);
dan4209d552022-05-27 15:04:43 +0000853 if( pDup && pDup->nSrc>1 && !IN_RENAME_OBJECT ){
drh26c4df02022-05-26 17:33:42 +0000854 Select *pSubquery;
855 Token as;
856 pSubquery = sqlite3SelectNew(pParse,0,pDup,0,0,0,0,SF_NestedFrom,0);
857 as.n = 0;
858 as.z = 0;
859 pDup = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0);
860 }
dane7877b22020-07-14 19:51:01 +0000861 pSrc = sqlite3SrcListAppendList(pParse, pSrc, pDup);
862 }
863 }else{
864 sqlite3DbFree(db, zName);
drhf26e09c2003-05-31 16:21:12 +0000865 }
866 return pSrc;
867}
868
drh0d23f672021-03-30 01:52:21 +0000869/*
870** Return true if the pExpr term from the RETURNING clause argument
871** list is of the form "*". Raise an error if the terms if of the
872** form "table.*".
873*/
874static int isAsteriskTerm(
875 Parse *pParse, /* Parsing context */
876 Expr *pTerm /* A term in the RETURNING clause */
877){
878 assert( pTerm!=0 );
879 if( pTerm->op==TK_ASTERISK ) return 1;
880 if( pTerm->op!=TK_DOT ) return 0;
881 assert( pTerm->pRight!=0 );
882 assert( pTerm->pLeft!=0 );
883 if( pTerm->pRight->op!=TK_ASTERISK ) return 0;
884 sqlite3ErrorMsg(pParse, "RETURNING may not use \"TABLE.*\" wildcards");
885 return 1;
886}
887
drhdac9a5f2021-01-29 21:18:46 +0000888/* The input list pList is the list of result set terms from a RETURNING
889** clause. The table that we are returning from is pTab.
890**
891** This routine makes a copy of the pList, and at the same time expands
892** any "*" wildcards to be the complete set of columns from pTab.
893*/
894static ExprList *sqlite3ExpandReturning(
895 Parse *pParse, /* Parsing context */
896 ExprList *pList, /* The arguments to RETURNING */
897 Table *pTab /* The table being updated */
898){
899 ExprList *pNew = 0;
900 sqlite3 *db = pParse->db;
901 int i;
drh552562c2021-02-04 20:52:20 +0000902
drhdac9a5f2021-01-29 21:18:46 +0000903 for(i=0; i<pList->nExpr; i++){
904 Expr *pOldExpr = pList->a[i].pExpr;
drh0d23f672021-03-30 01:52:21 +0000905 if( NEVER(pOldExpr==0) ) continue;
906 if( isAsteriskTerm(pParse, pOldExpr) ){
drhc9be8632021-02-02 00:16:15 +0000907 int jj;
908 for(jj=0; jj<pTab->nCol; jj++){
drh87296422021-02-07 12:59:43 +0000909 Expr *pNewExpr;
drhc9be8632021-02-02 00:16:15 +0000910 if( IsHiddenColumn(pTab->aCol+jj) ) continue;
drhcf9d36d2021-08-02 18:03:43 +0000911 pNewExpr = sqlite3Expr(db, TK_ID, pTab->aCol[jj].zCnName);
drhdac9a5f2021-01-29 21:18:46 +0000912 pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr);
913 if( !db->mallocFailed ){
914 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
drhcf9d36d2021-08-02 18:03:43 +0000915 pItem->zEName = sqlite3DbStrDup(db, pTab->aCol[jj].zCnName);
drhd88fd532022-05-02 20:49:30 +0000916 pItem->fg.eEName = ENAME_NAME;
drhdac9a5f2021-01-29 21:18:46 +0000917 }
918 }
919 }else{
920 Expr *pNewExpr = sqlite3ExprDup(db, pOldExpr, 0);
921 pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr);
drh9407b6e2021-01-31 16:45:10 +0000922 if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0) ){
drhdac9a5f2021-01-29 21:18:46 +0000923 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
924 pItem->zEName = sqlite3DbStrDup(db, pList->a[i].zEName);
drhd88fd532022-05-02 20:49:30 +0000925 pItem->fg.eEName = pList->a[i].fg.eEName;
drhdac9a5f2021-01-29 21:18:46 +0000926 }
927 }
928 }
929 return pNew;
930}
931
drhf26e09c2003-05-31 16:21:12 +0000932/*
drh381bdac2021-02-04 17:29:04 +0000933** Generate code for the RETURNING trigger. Unlike other triggers
934** that invoke a subprogram in the bytecode, the code for RETURNING
935** is generated in-line.
936*/
937static void codeReturningTrigger(
938 Parse *pParse, /* Parse context */
939 Trigger *pTrigger, /* The trigger step that defines the RETURNING */
940 Table *pTab, /* The table to code triggers from */
drh552562c2021-02-04 20:52:20 +0000941 int regIn /* The first in an array of registers */
drh381bdac2021-02-04 17:29:04 +0000942){
943 Vdbe *v = pParse->pVdbe;
drh90881862021-05-19 12:17:03 +0000944 sqlite3 *db = pParse->db;
drh381bdac2021-02-04 17:29:04 +0000945 ExprList *pNew;
946 Returning *pReturning;
drh90881862021-05-19 12:17:03 +0000947 Select sSelect;
948 SrcList sFrom;
drh381bdac2021-02-04 17:29:04 +0000949
950 assert( v!=0 );
951 assert( pParse->bReturning );
drh0c7d3d32022-01-24 16:47:12 +0000952 assert( db->pParse==pParse );
drh381bdac2021-02-04 17:29:04 +0000953 pReturning = pParse->u1.pReturning;
954 assert( pTrigger == &(pReturning->retTrig) );
drh90881862021-05-19 12:17:03 +0000955 memset(&sSelect, 0, sizeof(sSelect));
956 memset(&sFrom, 0, sizeof(sFrom));
957 sSelect.pEList = sqlite3ExprListDup(db, pReturning->pReturnEL, 0);
958 sSelect.pSrc = &sFrom;
959 sFrom.nSrc = 1;
960 sFrom.a[0].pTab = pTab;
dancf1e2552021-06-28 15:25:17 +0000961 sFrom.a[0].iCursor = -1;
drh90881862021-05-19 12:17:03 +0000962 sqlite3SelectPrep(pParse, &sSelect, 0);
drh0c7d3d32022-01-24 16:47:12 +0000963 if( pParse->nErr==0 ){
964 assert( db->mallocFailed==0 );
drh90881862021-05-19 12:17:03 +0000965 sqlite3GenerateColumnNames(pParse, &sSelect);
966 }
967 sqlite3ExprListDelete(db, sSelect.pEList);
drh381bdac2021-02-04 17:29:04 +0000968 pNew = sqlite3ExpandReturning(pParse, pReturning->pReturnEL, pTab);
drhebc44342022-01-05 11:49:58 +0000969 if( !db->mallocFailed ){
drh552562c2021-02-04 20:52:20 +0000970 NameContext sNC;
971 memset(&sNC, 0, sizeof(sNC));
drhe1db9342021-02-04 21:17:12 +0000972 if( pReturning->nRetCol==0 ){
973 pReturning->nRetCol = pNew->nExpr;
974 pReturning->iRetCur = pParse->nTab++;
975 }
drh552562c2021-02-04 20:52:20 +0000976 sNC.pParse = pParse;
977 sNC.uNC.iBaseReg = regIn;
978 sNC.ncFlags = NC_UBaseReg;
979 pParse->eTriggerOp = pTrigger->op;
980 pParse->pTriggerTab = pTab;
danc6977c12022-01-05 15:54:02 +0000981 if( sqlite3ResolveExprListNames(&sNC, pNew)==SQLITE_OK
drh1da88b52022-01-24 19:38:56 +0000982 && ALWAYS(!db->mallocFailed)
danc6977c12022-01-05 15:54:02 +0000983 ){
drh552562c2021-02-04 20:52:20 +0000984 int i;
985 int nCol = pNew->nExpr;
986 int reg = pParse->nMem+1;
987 pParse->nMem += nCol+2;
988 pReturning->iRetReg = reg;
989 for(i=0; i<nCol; i++){
drh90881862021-05-19 12:17:03 +0000990 Expr *pCol = pNew->a[i].pExpr;
danc6977c12022-01-05 15:54:02 +0000991 assert( pCol!=0 ); /* Due to !db->mallocFailed ~9 lines above */
drh90881862021-05-19 12:17:03 +0000992 sqlite3ExprCodeFactorable(pParse, pCol, reg+i);
drh41584df2021-12-29 04:31:54 +0000993 if( sqlite3ExprAffinity(pCol)==SQLITE_AFF_REAL ){
994 sqlite3VdbeAddOp1(v, OP_RealAffinity, reg+i);
995 }
drh381bdac2021-02-04 17:29:04 +0000996 }
drh552562c2021-02-04 20:52:20 +0000997 sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, i, reg+i);
998 sqlite3VdbeAddOp2(v, OP_NewRowid, pReturning->iRetCur, reg+i+1);
999 sqlite3VdbeAddOp3(v, OP_Insert, pReturning->iRetCur, reg+i, reg+i+1);
1000 }
drh552562c2021-02-04 20:52:20 +00001001 }
drhebc44342022-01-05 11:49:58 +00001002 sqlite3ExprListDelete(db, pNew);
1003 pParse->eTriggerOp = 0;
1004 pParse->pTriggerTab = 0;
drh381bdac2021-02-04 17:29:04 +00001005}
1006
1007
1008
1009/*
dan65a7cd12009-09-01 12:16:01 +00001010** Generate VDBE code for the statements inside the body of a single
1011** trigger.
drhc977f7f2002-05-21 11:38:11 +00001012*/
danielk1977c3f9bad2002-05-15 08:30:12 +00001013static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +00001014 Parse *pParse, /* The parser context */
1015 TriggerStep *pStepList, /* List of statements inside the trigger body */
dan65a7cd12009-09-01 12:16:01 +00001016 int orconf /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +00001017){
dan65a7cd12009-09-01 12:16:01 +00001018 TriggerStep *pStep;
drh344737f2004-09-19 00:50:20 +00001019 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +00001020 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +00001021
dan65a7cd12009-09-01 12:16:01 +00001022 assert( pParse->pTriggerTab && pParse->pToplevel );
1023 assert( pStepList );
drh344737f2004-09-19 00:50:20 +00001024 assert( v!=0 );
dan65a7cd12009-09-01 12:16:01 +00001025 for(pStep=pStepList; pStep; pStep=pStep->pNext){
dan165921a2009-08-28 18:53:45 +00001026 /* Figure out the ON CONFLICT policy that will be used for this step
1027 ** of the trigger program. If the statement that caused this trigger
1028 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
1029 ** the ON CONFLICT policy that was specified as part of the trigger
1030 ** step statement. Example:
1031 **
1032 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
1033 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
1034 ** END;
1035 **
1036 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
1037 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
1038 */
shanecea72b22009-09-07 04:38:36 +00001039 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
drhaceb31b2014-02-08 01:40:27 +00001040 assert( pParse->okConstFactor==0 );
danf78baaf2012-12-06 19:37:22 +00001041
drhf259df52017-12-27 20:38:35 +00001042#ifndef SQLITE_OMIT_TRACE
drhe307e112017-12-27 21:30:34 +00001043 if( pStep->zSpan ){
1044 sqlite3VdbeAddOp4(v, OP_Trace, 0x7fffffff, 1, 0,
1045 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
1046 P4_DYNAMIC);
1047 }
drhf259df52017-12-27 20:38:35 +00001048#endif
1049
dan165921a2009-08-28 18:53:45 +00001050 switch( pStep->op ){
danielk1977633ed082002-05-17 00:05:58 +00001051 case TK_UPDATE: {
dan165921a2009-08-28 18:53:45 +00001052 sqlite3Update(pParse,
dane7877b22020-07-14 19:51:01 +00001053 sqlite3TriggerStepSrc(pParse, pStep),
dan165921a2009-08-28 18:53:45 +00001054 sqlite3ExprListDup(db, pStep->pExprList, 0),
1055 sqlite3ExprDup(db, pStep->pWhere, 0),
drheac9fab2018-04-16 13:00:50 +00001056 pParse->eOrconf, 0, 0, 0
dan165921a2009-08-28 18:53:45 +00001057 );
drhdac9a5f2021-01-29 21:18:46 +00001058 sqlite3VdbeAddOp0(v, OP_ResetCount);
danielk1977633ed082002-05-17 00:05:58 +00001059 break;
1060 }
1061 case TK_INSERT: {
dan165921a2009-08-28 18:53:45 +00001062 sqlite3Insert(pParse,
dane7877b22020-07-14 19:51:01 +00001063 sqlite3TriggerStepSrc(pParse, pStep),
dan165921a2009-08-28 18:53:45 +00001064 sqlite3SelectDup(db, pStep->pSelect, 0),
1065 sqlite3IdListDup(db, pStep->pIdList),
drh46d2e5c2018-04-12 13:15:43 +00001066 pParse->eOrconf,
1067 sqlite3UpsertDup(db, pStep->pUpsert)
dan165921a2009-08-28 18:53:45 +00001068 );
drhdac9a5f2021-01-29 21:18:46 +00001069 sqlite3VdbeAddOp0(v, OP_ResetCount);
danielk1977633ed082002-05-17 00:05:58 +00001070 break;
1071 }
1072 case TK_DELETE: {
dan165921a2009-08-28 18:53:45 +00001073 sqlite3DeleteFrom(pParse,
dane7877b22020-07-14 19:51:01 +00001074 sqlite3TriggerStepSrc(pParse, pStep),
drh8c0833f2017-11-14 23:48:23 +00001075 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
dan165921a2009-08-28 18:53:45 +00001076 );
drhdac9a5f2021-01-29 21:18:46 +00001077 sqlite3VdbeAddOp0(v, OP_ResetCount);
danielk1977633ed082002-05-17 00:05:58 +00001078 break;
1079 }
drh381bdac2021-02-04 17:29:04 +00001080 default: assert( pStep->op==TK_SELECT ); {
dan165921a2009-08-28 18:53:45 +00001081 SelectDest sDest;
1082 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
drhdac9a5f2021-01-29 21:18:46 +00001083 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
dan165921a2009-08-28 18:53:45 +00001084 sqlite3Select(pParse, pSelect, &sDest);
1085 sqlite3SelectDelete(db, pSelect);
drh9ab4c2e2009-05-09 00:18:38 +00001086 break;
1087 }
danielk1977633ed082002-05-17 00:05:58 +00001088 }
danielk1977633ed082002-05-17 00:05:58 +00001089 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001090
danielk1977633ed082002-05-17 00:05:58 +00001091 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +00001092}
1093
drhc7379ce2013-10-30 02:28:23 +00001094#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
dan165921a2009-08-28 18:53:45 +00001095/*
1096** This function is used to add VdbeComment() annotations to a VDBE
1097** program. It is not used in production code, only for debugging.
1098*/
1099static const char *onErrorText(int onError){
1100 switch( onError ){
1101 case OE_Abort: return "abort";
1102 case OE_Rollback: return "rollback";
1103 case OE_Fail: return "fail";
1104 case OE_Replace: return "replace";
1105 case OE_Ignore: return "ignore";
1106 case OE_Default: return "default";
1107 }
1108 return "n/a";
1109}
1110#endif
1111
1112/*
1113** Parse context structure pFrom has just been used to create a sub-vdbe
1114** (trigger program). If an error has occurred, transfer error information
1115** from pFrom to pTo.
1116*/
1117static void transferParseError(Parse *pTo, Parse *pFrom){
1118 assert( pFrom->zErrMsg==0 || pFrom->nErr );
1119 assert( pTo->zErrMsg==0 || pTo->nErr );
1120 if( pTo->nErr==0 ){
1121 pTo->zErrMsg = pFrom->zErrMsg;
1122 pTo->nErr = pFrom->nErr;
drhe06874e2015-04-16 15:47:06 +00001123 pTo->rc = pFrom->rc;
dan165921a2009-08-28 18:53:45 +00001124 }else{
1125 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
1126 }
1127}
1128
dan65a7cd12009-09-01 12:16:01 +00001129/*
1130** Create and populate a new TriggerPrg object with a sub-program
1131** implementing trigger pTrigger with ON CONFLICT policy orconf.
1132*/
dan2832ad42009-08-31 15:27:27 +00001133static TriggerPrg *codeRowTrigger(
dan165921a2009-08-28 18:53:45 +00001134 Parse *pParse, /* Current parse context */
1135 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +00001136 Table *pTab, /* The table pTrigger is attached to */
1137 int orconf /* ON CONFLICT policy to code trigger program with */
dan165921a2009-08-28 18:53:45 +00001138){
dan65a7cd12009-09-01 12:16:01 +00001139 Parse *pTop = sqlite3ParseToplevel(pParse);
1140 sqlite3 *db = pParse->db; /* Database handle */
1141 TriggerPrg *pPrg; /* Value to return */
dan165921a2009-08-28 18:53:45 +00001142 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
1143 Vdbe *v; /* Temporary VM */
dan165921a2009-08-28 18:53:45 +00001144 NameContext sNC; /* Name context for sub-vdbe */
1145 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
dan165921a2009-08-28 18:53:45 +00001146 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
drhc692df22022-01-24 15:34:55 +00001147 Parse sSubParse; /* Parse context for sub-vdbe */
dan165921a2009-08-28 18:53:45 +00001148
dan1da40a32009-09-19 17:00:31 +00001149 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dand19c9332010-07-26 12:05:17 +00001150 assert( pTop->pVdbe );
dan65a7cd12009-09-01 12:16:01 +00001151
1152 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
1153 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
1154 ** list of the top-level Parse object sooner rather than later. */
dan2832ad42009-08-31 15:27:27 +00001155 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
1156 if( !pPrg ) return 0;
dan65a7cd12009-09-01 12:16:01 +00001157 pPrg->pNext = pTop->pTriggerPrg;
1158 pTop->pTriggerPrg = pPrg;
dan2832ad42009-08-31 15:27:27 +00001159 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
dan165921a2009-08-28 18:53:45 +00001160 if( !pProgram ) return 0;
dand19c9332010-07-26 12:05:17 +00001161 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
dan2832ad42009-08-31 15:27:27 +00001162 pPrg->pTrigger = pTrigger;
1163 pPrg->orconf = orconf;
danbb5f1682009-11-27 12:12:34 +00001164 pPrg->aColmask[0] = 0xffffffff;
1165 pPrg->aColmask[1] = 0xffffffff;
dan165921a2009-08-28 18:53:45 +00001166
dan65a7cd12009-09-01 12:16:01 +00001167 /* Allocate and populate a new Parse context to use for coding the
1168 ** trigger sub-program. */
drhc692df22022-01-24 15:34:55 +00001169 sqlite3ParseObjectInit(&sSubParse, db);
dan165921a2009-08-28 18:53:45 +00001170 memset(&sNC, 0, sizeof(sNC));
drhc692df22022-01-24 15:34:55 +00001171 sNC.pParse = &sSubParse;
1172 sSubParse.pTriggerTab = pTab;
1173 sSubParse.pToplevel = pTop;
1174 sSubParse.zAuthContext = pTrigger->zName;
1175 sSubParse.eTriggerOp = pTrigger->op;
1176 sSubParse.nQueryLoop = pParse->nQueryLoop;
1177 sSubParse.disableVtab = pParse->disableVtab;
dan165921a2009-08-28 18:53:45 +00001178
drhc692df22022-01-24 15:34:55 +00001179 v = sqlite3GetVdbe(&sSubParse);
dan165921a2009-08-28 18:53:45 +00001180 if( v ){
dan76d462e2009-08-30 11:42:51 +00001181 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
1182 pTrigger->zName, onErrorText(orconf),
dan165921a2009-08-28 18:53:45 +00001183 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
dan65a7cd12009-09-01 12:16:01 +00001184 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
1185 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
1186 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
dan76d462e2009-08-30 11:42:51 +00001187 pTab->zName
dan165921a2009-08-28 18:53:45 +00001188 ));
dan76d462e2009-08-30 11:42:51 +00001189#ifndef SQLITE_OMIT_TRACE
drhe307e112017-12-27 21:30:34 +00001190 if( pTrigger->zName ){
1191 sqlite3VdbeChangeP4(v, -1,
1192 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
1193 );
1194 }
dan76d462e2009-08-30 11:42:51 +00001195#endif
dan165921a2009-08-28 18:53:45 +00001196
dan65a7cd12009-09-01 12:16:01 +00001197 /* If one was specified, code the WHEN clause. If it evaluates to false
1198 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
1199 ** OP_Halt inserted at the end of the program. */
dan165921a2009-08-28 18:53:45 +00001200 if( pTrigger->pWhen ){
dan165921a2009-08-28 18:53:45 +00001201 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
drh4c4a2572021-04-06 12:50:24 +00001202 if( db->mallocFailed==0
1203 && SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
dan523a0872009-08-31 05:23:32 +00001204 ){
drhc692df22022-01-24 15:34:55 +00001205 iEndTrigger = sqlite3VdbeMakeLabel(&sSubParse);
1206 sqlite3ExprIfFalse(&sSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
dan165921a2009-08-28 18:53:45 +00001207 }
1208 sqlite3ExprDelete(db, pWhen);
1209 }
1210
1211 /* Code the trigger program into the sub-vdbe. */
drhc692df22022-01-24 15:34:55 +00001212 codeTriggerProgram(&sSubParse, pTrigger->step_list, orconf);
dan65a7cd12009-09-01 12:16:01 +00001213
1214 /* Insert an OP_Halt at the end of the sub-program. */
dan165921a2009-08-28 18:53:45 +00001215 if( iEndTrigger ){
1216 sqlite3VdbeResolveLabel(v, iEndTrigger);
1217 }
1218 sqlite3VdbeAddOp0(v, OP_Halt);
dan76d462e2009-08-30 11:42:51 +00001219 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
drhc692df22022-01-24 15:34:55 +00001220 transferParseError(pParse, &sSubParse);
dan76d462e2009-08-30 11:42:51 +00001221
drh0c7d3d32022-01-24 16:47:12 +00001222 if( pParse->nErr==0 ){
1223 assert( db->mallocFailed==0 );
dan65a7cd12009-09-01 12:16:01 +00001224 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
dan523a0872009-08-31 05:23:32 +00001225 }
drhc692df22022-01-24 15:34:55 +00001226 pProgram->nMem = sSubParse.nMem;
1227 pProgram->nCsr = sSubParse.nTab;
dan165921a2009-08-28 18:53:45 +00001228 pProgram->token = (void *)pTrigger;
drhc692df22022-01-24 15:34:55 +00001229 pPrg->aColmask[0] = sSubParse.oldmask;
1230 pPrg->aColmask[1] = sSubParse.newmask;
dan165921a2009-08-28 18:53:45 +00001231 sqlite3VdbeDelete(v);
drh0c7d3d32022-01-24 16:47:12 +00001232 }else{
1233 transferParseError(pParse, &sSubParse);
dan165921a2009-08-28 18:53:45 +00001234 }
dan65a7cd12009-09-01 12:16:01 +00001235
drhc692df22022-01-24 15:34:55 +00001236 assert( !sSubParse.pTriggerPrg && !sSubParse.nMaxArg );
1237 sqlite3ParseObjectReset(&sSubParse);
dan2832ad42009-08-31 15:27:27 +00001238 return pPrg;
dan165921a2009-08-28 18:53:45 +00001239}
1240
dan65a7cd12009-09-01 12:16:01 +00001241/*
1242** Return a pointer to a TriggerPrg object containing the sub-program for
1243** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
1244** TriggerPrg object exists, a new object is allocated and populated before
1245** being returned.
1246*/
dan2832ad42009-08-31 15:27:27 +00001247static TriggerPrg *getRowTrigger(
dan65a7cd12009-09-01 12:16:01 +00001248 Parse *pParse, /* Current parse context */
dan165921a2009-08-28 18:53:45 +00001249 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +00001250 Table *pTab, /* The table trigger pTrigger is attached to */
1251 int orconf /* ON CONFLICT algorithm. */
dan165921a2009-08-28 18:53:45 +00001252){
dan65a7cd12009-09-01 12:16:01 +00001253 Parse *pRoot = sqlite3ParseToplevel(pParse);
dan2832ad42009-08-31 15:27:27 +00001254 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +00001255
dan1da40a32009-09-19 17:00:31 +00001256 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dan165921a2009-08-28 18:53:45 +00001257
1258 /* It may be that this trigger has already been coded (or is in the
1259 ** process of being coded). If this is the case, then an entry with
dan2832ad42009-08-31 15:27:27 +00001260 ** a matching TriggerPrg.pTrigger field will be present somewhere
1261 ** in the Parse.pTriggerPrg list. Search for such an entry. */
dan2832ad42009-08-31 15:27:27 +00001262 for(pPrg=pRoot->pTriggerPrg;
1263 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
1264 pPrg=pPrg->pNext
dan165921a2009-08-28 18:53:45 +00001265 );
1266
dan65a7cd12009-09-01 12:16:01 +00001267 /* If an existing TriggerPrg could not be located, create a new one. */
dan2832ad42009-08-31 15:27:27 +00001268 if( !pPrg ){
dan65a7cd12009-09-01 12:16:01 +00001269 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
drhe1c47432022-02-07 18:52:56 +00001270 pParse->db->errByteOffset = -1;
dan165921a2009-08-28 18:53:45 +00001271 }
1272
dan2832ad42009-08-31 15:27:27 +00001273 return pPrg;
dan165921a2009-08-28 18:53:45 +00001274}
1275
dan94d7f502009-09-24 09:05:49 +00001276/*
1277** Generate code for the trigger program associated with trigger p on
1278** table pTab. The reg, orconf and ignoreJump parameters passed to this
1279** function are the same as those described in the header function for
1280** sqlite3CodeRowTrigger()
1281*/
dan1da40a32009-09-19 17:00:31 +00001282void sqlite3CodeRowTriggerDirect(
1283 Parse *pParse, /* Parse context */
1284 Trigger *p, /* Trigger to code */
1285 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001286 int reg, /* Reg array containing OLD.* and NEW.* values */
dan1da40a32009-09-19 17:00:31 +00001287 int orconf, /* ON CONFLICT policy */
1288 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
1289){
1290 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
1291 TriggerPrg *pPrg;
1292 pPrg = getRowTrigger(pParse, p, pTab, orconf);
drh0c7d3d32022-01-24 16:47:12 +00001293 assert( pPrg || pParse->nErr );
dan1da40a32009-09-19 17:00:31 +00001294
1295 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
1296 ** is a pointer to the sub-vdbe containing the trigger program. */
1297 if( pPrg ){
dand19c9332010-07-26 12:05:17 +00001298 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
1299
drh9b34abe2016-01-16 15:12:35 +00001300 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
1301 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
dan1da40a32009-09-19 17:00:31 +00001302 VdbeComment(
1303 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
1304
1305 /* Set the P5 operand of the OP_Program instruction to non-zero if
1306 ** recursive invocation of this trigger program is disallowed. Recursive
1307 ** invocation is disallowed if (a) the sub-program is really a trigger,
1308 ** not a foreign key action, and (b) the flag to enable recursive triggers
1309 ** is clear. */
dand19c9332010-07-26 12:05:17 +00001310 sqlite3VdbeChangeP5(v, (u8)bRecursive);
dan1da40a32009-09-19 17:00:31 +00001311 }
1312}
1313
danielk1977633ed082002-05-17 00:05:58 +00001314/*
dan94d7f502009-09-24 09:05:49 +00001315** This is called to code the required FOR EACH ROW triggers for an operation
1316** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
drhf7b54962013-05-28 12:11:54 +00001317** is given by the op parameter. The tr_tm parameter determines whether the
dan94d7f502009-09-24 09:05:49 +00001318** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
1319** parameter pChanges is passed the list of columns being modified.
danielk1977633ed082002-05-17 00:05:58 +00001320**
dan94d7f502009-09-24 09:05:49 +00001321** If there are no triggers that fire at the specified time for the specified
1322** operation on pTab, this function is a no-op.
drhc977f7f2002-05-21 11:38:11 +00001323**
dan94d7f502009-09-24 09:05:49 +00001324** The reg argument is the address of the first in an array of registers
1325** that contain the values substituted for the new.* and old.* references
1326** in the trigger program. If N is the number of columns in table pTab
1327** (a copy of pTab->nCol), then registers are populated as follows:
drhc977f7f2002-05-21 11:38:11 +00001328**
dan94d7f502009-09-24 09:05:49 +00001329** Register Contains
1330** ------------------------------------------------------
1331** reg+0 OLD.rowid
1332** reg+1 OLD.* value of left-most column of pTab
1333** ... ...
1334** reg+N OLD.* value of right-most column of pTab
1335** reg+N+1 NEW.rowid
drh381bdac2021-02-04 17:29:04 +00001336** reg+N+2 NEW.* value of left-most column of pTab
dan94d7f502009-09-24 09:05:49 +00001337** ... ...
1338** reg+N+N+1 NEW.* value of right-most column of pTab
drhc977f7f2002-05-21 11:38:11 +00001339**
dan94d7f502009-09-24 09:05:49 +00001340** For ON DELETE triggers, the registers containing the NEW.* values will
1341** never be accessed by the trigger program, so they are not allocated or
1342** populated by the caller (there is no data to populate them with anyway).
1343** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
1344** are never accessed, and so are not allocated by the caller. So, for an
1345** ON INSERT trigger, the value passed to this function as parameter reg
1346** is not a readable register, although registers (reg+N) through
1347** (reg+N+N+1) are.
danielk1977633ed082002-05-17 00:05:58 +00001348**
dan94d7f502009-09-24 09:05:49 +00001349** Parameter orconf is the default conflict resolution algorithm for the
1350** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
1351** is the instruction that control should jump to if a trigger program
1352** raises an IGNORE exception.
danielk1977633ed082002-05-17 00:05:58 +00001353*/
dan165921a2009-08-28 18:53:45 +00001354void sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +00001355 Parse *pParse, /* Parse context */
danielk19772f886d12009-02-28 10:47:41 +00001356 Trigger *pTrigger, /* List of triggers on table pTab */
danielk1977633ed082002-05-17 00:05:58 +00001357 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
1358 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +00001359 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +00001360 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001361 int reg, /* The first in an array of registers (see above) */
danielk19776f349032002-06-11 02:25:40 +00001362 int orconf, /* ON CONFLICT policy */
dan165921a2009-08-28 18:53:45 +00001363 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
drhc977f7f2002-05-21 11:38:11 +00001364){
dan94d7f502009-09-24 09:05:49 +00001365 Trigger *p; /* Used to iterate through pTrigger list */
danielk19778f2c54e2008-01-01 19:02:09 +00001366
dan94d7f502009-09-24 09:05:49 +00001367 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
1368 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
1369 assert( (op==TK_UPDATE)==(pChanges!=0) );
danielk1977c3f9bad2002-05-15 08:30:12 +00001370
danielk19772f886d12009-02-28 10:47:41 +00001371 for(p=pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +00001372
drh9ab4c2e2009-05-09 00:18:38 +00001373 /* Sanity checking: The schema for the trigger and for the table are
1374 ** always defined. The trigger must be in the same schema as the table
1375 ** or else it must be a TEMP trigger. */
1376 assert( p->pSchema!=0 );
1377 assert( p->pTabSchema!=0 );
dan165921a2009-08-28 18:53:45 +00001378 assert( p->pSchema==p->pTabSchema
1379 || p->pSchema==pParse->db->aDb[1].pSchema );
drh9ab4c2e2009-05-09 00:18:38 +00001380
drh28828c52021-01-30 21:55:38 +00001381 /* Determine whether we should code this trigger. One of two choices:
1382 ** 1. The trigger is an exact match to the current DML statement
1383 ** 2. This is a RETURNING trigger for INSERT but we are currently
1384 ** doing the UPDATE part of an UPSERT.
1385 */
1386 if( (p->op==op || (p->bReturning && p->op==TK_INSERT && op==TK_UPDATE))
dan165921a2009-08-28 18:53:45 +00001387 && p->tr_tm==tr_tm
dan94d7f502009-09-24 09:05:49 +00001388 && checkColumnOverlap(p->pColumns, pChanges)
danielk1977eecfb3e2006-01-10 12:31:39 +00001389 ){
drh381bdac2021-02-04 17:29:04 +00001390 if( !p->bReturning ){
1391 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
1392 }else if( sqlite3IsToplevel(pParse) ){
1393 codeReturningTrigger(pParse, p, pTab, reg);
1394 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001395 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001396 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001397}
dan165921a2009-08-28 18:53:45 +00001398
dan2832ad42009-08-31 15:27:27 +00001399/*
danbb5f1682009-11-27 12:12:34 +00001400** Triggers may access values stored in the old.* or new.* pseudo-table.
1401** This function returns a 32-bit bitmask indicating which columns of the
1402** old.* or new.* tables actually are used by triggers. This information
1403** may be used by the caller, for example, to avoid having to load the entire
1404** old.* record into memory when executing an UPDATE or DELETE command.
dan2832ad42009-08-31 15:27:27 +00001405**
1406** Bit 0 of the returned mask is set if the left-most column of the
danbb5f1682009-11-27 12:12:34 +00001407** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
dan2832ad42009-08-31 15:27:27 +00001408** the second leftmost column value is required, and so on. If there
1409** are more than 32 columns in the table, and at least one of the columns
1410** with an index greater than 32 may be accessed, 0xffffffff is returned.
1411**
danbb5f1682009-11-27 12:12:34 +00001412** It is not possible to determine if the old.rowid or new.rowid column is
1413** accessed by triggers. The caller must always assume that it is.
dan2832ad42009-08-31 15:27:27 +00001414**
danbb5f1682009-11-27 12:12:34 +00001415** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
1416** applies to the old.* table. If 1, the new.* table.
1417**
1418** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
1419** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
1420** included in the returned mask if the TRIGGER_BEFORE bit is set in the
1421** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
1422** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
dan2832ad42009-08-31 15:27:27 +00001423*/
danbb5f1682009-11-27 12:12:34 +00001424u32 sqlite3TriggerColmask(
dan165921a2009-08-28 18:53:45 +00001425 Parse *pParse, /* Parse context */
1426 Trigger *pTrigger, /* List of triggers on table pTab */
dan165921a2009-08-28 18:53:45 +00001427 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
danbb5f1682009-11-27 12:12:34 +00001428 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
1429 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
dan165921a2009-08-28 18:53:45 +00001430 Table *pTab, /* The table to code triggers from */
dan2832ad42009-08-31 15:27:27 +00001431 int orconf /* Default ON CONFLICT policy for trigger steps */
dan165921a2009-08-28 18:53:45 +00001432){
dan1da40a32009-09-19 17:00:31 +00001433 const int op = pChanges ? TK_UPDATE : TK_DELETE;
dan2832ad42009-08-31 15:27:27 +00001434 u32 mask = 0;
dan165921a2009-08-28 18:53:45 +00001435 Trigger *p;
dan165921a2009-08-28 18:53:45 +00001436
danbb5f1682009-11-27 12:12:34 +00001437 assert( isNew==1 || isNew==0 );
dan165921a2009-08-28 18:53:45 +00001438 for(p=pTrigger; p; p=p->pNext){
drh381bdac2021-02-04 17:29:04 +00001439 if( p->op==op
1440 && (tr_tm&p->tr_tm)
danbb5f1682009-11-27 12:12:34 +00001441 && checkColumnOverlap(p->pColumns,pChanges)
1442 ){
drh381bdac2021-02-04 17:29:04 +00001443 if( p->bReturning ){
1444 mask = 0xffffffff;
1445 }else{
1446 TriggerPrg *pPrg;
1447 pPrg = getRowTrigger(pParse, p, pTab, orconf);
1448 if( pPrg ){
1449 mask |= pPrg->aColmask[isNew];
1450 }
dan165921a2009-08-28 18:53:45 +00001451 }
1452 }
1453 }
dan2832ad42009-08-31 15:27:27 +00001454
1455 return mask;
dan165921a2009-08-28 18:53:45 +00001456}
1457
drhb7f91642004-10-31 02:22:47 +00001458#endif /* !defined(SQLITE_OMIT_TRIGGER) */