blob: fcf1f77033a1e74bc259fce858301d5cfe25bbb6 [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);
drh3765c032021-04-27 17:18:10 +000060 pList = pTab->pTrigger;
drha4767682021-04-27 13:04:18 +000061 while( p ){
62 Trigger *pTrig = (Trigger *)sqliteHashData(p);
63 if( pTrig->pTabSchema==pTab->pSchema
64 && pTrig->table
65 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
drh3765c032021-04-27 17:18:10 +000066 && pTrig->pTabSchema!=pTmpSchema
drha4767682021-04-27 13:04:18 +000067 ){
68 pTrig->pNext = pList;
69 pList = pTrig;
70 }else if( pTrig->op==TK_RETURNING
71#ifndef SQLITE_OMIT_VIRTUALTABLE
72 && pParse->db->pVtabCtx==0
73#endif
74 ){
75 assert( pParse->bReturning );
76 assert( &(pParse->u1.pReturning->retTrig) == pTrig );
77 pTrig->table = pTab->zName;
78 pTrig->pTabSchema = pTab->pSchema;
79 pTrig->pNext = pList;
80 pList = pTrig;
81 }
82 p = sqliteHashNext(p);
83 }
84#if 0
85 if( pList ){
86 Trigger *pX;
87 printf("Triggers for %s:", pTab->zName);
88 for(pX=pList; pX; pX=pX->pNext){
89 printf(" %s", pX->zName);
90 }
91 printf("\n");
92 fflush(stdout);
93 }
94#endif
drhf54a80f2021-01-29 13:47:36 +000095 return pList;
danielk19772f886d12009-02-28 10:47:41 +000096}
97
98/*
drhf0f258b2003-04-21 18:48:45 +000099** This is called by the parser when it sees a CREATE TRIGGER statement
100** up to the point of the BEGIN before the trigger actions. A Trigger
101** structure is generated based on the information available and stored
102** in pParse->pNewTrigger. After the trigger actions have been parsed, the
danielk19774adee202004-05-08 08:23:19 +0000103** sqlite3FinishTrigger() function is called to complete the trigger
drhf0f258b2003-04-21 18:48:45 +0000104** construction process.
drh9adf9ac2002-05-15 11:44:13 +0000105*/
danielk19774adee202004-05-08 08:23:19 +0000106void sqlite3BeginTrigger(
drh9adf9ac2002-05-15 11:44:13 +0000107 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
danielk1977ef2cb632004-05-29 02:37:19 +0000108 Token *pName1, /* The name of the trigger */
109 Token *pName2, /* The name of the trigger */
drh5cf590c2003-04-24 01:45:04 +0000110 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
drh9adf9ac2002-05-15 11:44:13 +0000111 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
danielk1977633ed082002-05-17 00:05:58 +0000112 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
drhd24cc422003-03-27 12:51:24 +0000113 SrcList *pTableName,/* The name of the table/view the trigger applies to */
drh9adf9ac2002-05-15 11:44:13 +0000114 Expr *pWhen, /* WHEN clause */
drhfdd48a72006-09-11 23:45:48 +0000115 int isTemp, /* True if the TEMPORARY keyword is present */
116 int noErr /* Suppress errors if the trigger already exists */
drh9adf9ac2002-05-15 11:44:13 +0000117){
drh3d5f74b2009-08-06 17:43:31 +0000118 Trigger *pTrigger = 0; /* The new trigger */
119 Table *pTab; /* Table that the trigger fires off of */
drhf0f258b2003-04-21 18:48:45 +0000120 char *zName = 0; /* Name of the trigger */
drh3d5f74b2009-08-06 17:43:31 +0000121 sqlite3 *db = pParse->db; /* The database connection */
danielk1977ef2cb632004-05-29 02:37:19 +0000122 int iDb; /* The database to store the trigger in */
123 Token *pName; /* The unqualified db name */
drh3d5f74b2009-08-06 17:43:31 +0000124 DbFixer sFix; /* State vector for the DB fixer */
drhed6c8672003-01-12 18:02:16 +0000125
drh43617e92006-03-06 20:55:46 +0000126 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
127 assert( pName2!=0 );
drhaa78bec2008-12-09 03:55:14 +0000128 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
129 assert( op>0 && op<0xff );
danielk1977ef2cb632004-05-29 02:37:19 +0000130 if( isTemp ){
131 /* If TEMP was specified, then the trigger name may not be qualified. */
drh43617e92006-03-06 20:55:46 +0000132 if( pName2->n>0 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000133 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
134 goto trigger_cleanup;
135 }
136 iDb = 1;
137 pName = pName1;
138 }else{
mistachkind5578432012-08-25 10:01:29 +0000139 /* Figure out the db that the trigger will be created in */
danielk1977ef2cb632004-05-29 02:37:19 +0000140 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
141 if( iDb<0 ){
142 goto trigger_cleanup;
143 }
144 }
drh6fea83e2011-07-01 13:50:44 +0000145 if( !pTableName || db->mallocFailed ){
146 goto trigger_cleanup;
147 }
148
149 /* A long-standing parser bug is that this syntax was allowed:
150 **
151 ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
152 ** ^^^^^^^^
153 **
154 ** To maintain backwards compatibility, ignore the database
drh346a70c2020-06-15 20:27:35 +0000155 ** name on pTableName if we are reparsing out of the schema table
drh6fea83e2011-07-01 13:50:44 +0000156 */
157 if( db->init.busy && iDb!=1 ){
158 sqlite3DbFree(db, pTableName->a[0].zDatabase);
159 pTableName->a[0].zDatabase = 0;
160 }
danielk1977ef2cb632004-05-29 02:37:19 +0000161
162 /* If the trigger name was unqualified, and the table is a temp table,
163 ** then set iDb to 1 to create the trigger in the temporary database.
164 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
165 ** exist, the error is caught by the block below.
drh9adf9ac2002-05-15 11:44:13 +0000166 */
danielk1977ef2cb632004-05-29 02:37:19 +0000167 pTab = sqlite3SrcListLookup(pParse, pTableName);
drh622d2882010-02-15 16:54:55 +0000168 if( db->init.busy==0 && pName2->n==0 && pTab
169 && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977ef2cb632004-05-29 02:37:19 +0000170 iDb = 1;
171 }
172
173 /* Ensure the table name matches database name and that the table exists */
drh17435752007-08-16 04:30:38 +0000174 if( db->mallocFailed ) goto trigger_cleanup;
drhd24cc422003-03-27 12:51:24 +0000175 assert( pTableName->nSrc==1 );
drhd100f692013-10-03 15:39:44 +0000176 sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
177 if( sqlite3FixSrcList(&sFix, pTableName) ){
drh4312db52003-06-03 01:47:11 +0000178 goto trigger_cleanup;
drhf26e09c2003-05-31 16:21:12 +0000179 }
danielk1977ef2cb632004-05-29 02:37:19 +0000180 pTab = sqlite3SrcListLookup(pParse, pTableName);
181 if( !pTab ){
182 /* The table does not exist. */
drh4e451aa2020-11-05 19:13:44 +0000183 goto trigger_orphan_error;
drhd24cc422003-03-27 12:51:24 +0000184 }
drh4cbdda92006-06-14 19:00:20 +0000185 if( IsVirtual(pTab) ){
186 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
drh4e451aa2020-11-05 19:13:44 +0000187 goto trigger_orphan_error;
drh4cbdda92006-06-14 19:00:20 +0000188 }
drhd24cc422003-03-27 12:51:24 +0000189
danielk1977d8123362004-06-12 09:25:12 +0000190 /* Check that the trigger name is not reserved and that no trigger of the
191 ** specified name exists */
drh17435752007-08-16 04:30:38 +0000192 zName = sqlite3NameFromToken(db, pName);
drhc5a93d42019-08-12 00:08:07 +0000193 if( zName==0 ){
194 assert( db->mallocFailed );
195 goto trigger_cleanup;
196 }
197 if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
danielk1977d8123362004-06-12 09:25:12 +0000198 goto trigger_cleanup;
199 }
drh21206082011-04-04 18:22:02 +0000200 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danc9461ec2018-08-29 21:00:16 +0000201 if( !IN_RENAME_OBJECT ){
dan5496d6a2018-08-13 17:14:26 +0000202 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
203 if( !noErr ){
204 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
205 }else{
206 assert( !db->init.busy );
207 sqlite3CodeVerifySchema(pParse, iDb);
208 }
209 goto trigger_cleanup;
drhfdd48a72006-09-11 23:45:48 +0000210 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000211 }
danielk1977ef2cb632004-05-29 02:37:19 +0000212
213 /* Do not create a trigger on a system table */
drhdca76842004-12-07 14:06:13 +0000214 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk19774adee202004-05-08 08:23:19 +0000215 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
drhd24cc422003-03-27 12:51:24 +0000216 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40 +0000217 }
danielk1977ef2cb632004-05-29 02:37:19 +0000218
219 /* INSTEAD of triggers are only for views and views only support INSTEAD
220 ** of triggers.
221 */
drhf38524d2021-08-02 16:41:57 +0000222 if( IsView(pTab) && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000223 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drha9799932021-03-19 13:00:28 +0000224 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName->a);
drh4e451aa2020-11-05 19:13:44 +0000225 goto trigger_orphan_error;
drhd24cc422003-03-27 12:51:24 +0000226 }
drhf38524d2021-08-02 16:41:57 +0000227 if( !IsView(pTab) && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000228 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drha9799932021-03-19 13:00:28 +0000229 " trigger on table: %S", pTableName->a);
drh4e451aa2020-11-05 19:13:44 +0000230 goto trigger_orphan_error;
drhd24cc422003-03-27 12:51:24 +0000231 }
danielk1977ef2cb632004-05-29 02:37:19 +0000232
drhd24cc422003-03-27 12:51:24 +0000233#ifndef SQLITE_OMIT_AUTHORIZATION
danc9461ec2018-08-29 21:00:16 +0000234 if( !IN_RENAME_OBJECT ){
drha0daa752016-09-16 11:53:10 +0000235 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drhd24cc422003-03-27 12:51:24 +0000236 int code = SQLITE_CREATE_TRIGGER;
drh69c33822016-08-18 14:33:11 +0000237 const char *zDb = db->aDb[iTabDb].zDbSName;
238 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
danielk1977da184232006-01-05 11:34:32 +0000239 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19 +0000240 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24 +0000241 goto trigger_cleanup;
242 }
danielk1977da184232006-01-05 11:34:32 +0000243 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24 +0000244 goto trigger_cleanup;
245 }
246 }
247#endif
danielk1977d702fcc2002-05-26 23:24:40 +0000248
danielk1977ef2cb632004-05-29 02:37:19 +0000249 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04 +0000250 ** cannot appear on views. So we might as well translate every
251 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
252 ** elsewhere.
253 */
danielk1977d702fcc2002-05-26 23:24:40 +0000254 if (tr_tm == TK_INSTEAD){
255 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12 +0000256 }
257
258 /* Build the Trigger object */
drh17435752007-08-16 04:30:38 +0000259 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19 +0000260 if( pTrigger==0 ) goto trigger_cleanup;
dan165921a2009-08-28 18:53:45 +0000261 pTrigger->zName = zName;
drhe5f9c642003-01-13 23:27:31 +0000262 zName = 0;
drh17435752007-08-16 04:30:38 +0000263 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32 +0000264 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48 +0000265 pTrigger->pTabSchema = pTab->pSchema;
drhaa78bec2008-12-09 03:55:14 +0000266 pTrigger->op = (u8)op;
drhdca76842004-12-07 14:06:13 +0000267 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
danc9461ec2018-08-29 21:00:16 +0000268 if( IN_RENAME_OBJECT ){
269 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
dan5496d6a2018-08-13 17:14:26 +0000270 pTrigger->pWhen = pWhen;
271 pWhen = 0;
272 }else{
273 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
274 }
275 pTrigger->pColumns = pColumns;
276 pColumns = 0;
drhf0f258b2003-04-21 18:48:45 +0000277 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19 +0000278 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45 +0000279
280trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000281 sqlite3DbFree(db, zName);
282 sqlite3SrcListDelete(db, pTableName);
283 sqlite3IdListDelete(db, pColumns);
284 sqlite3ExprDelete(db, pWhen);
danielk1977d5d56522005-03-16 12:15:20 +0000285 if( !pParse->pNewTrigger ){
drh633e6d52008-07-28 19:34:53 +0000286 sqlite3DeleteTrigger(db, pTrigger);
danielk1977d5d56522005-03-16 12:15:20 +0000287 }else{
288 assert( pParse->pNewTrigger==pTrigger );
289 }
drh4e451aa2020-11-05 19:13:44 +0000290 return;
291
292trigger_orphan_error:
293 if( db->init.iDb==1 ){
294 /* Ticket #3810.
295 ** Normally, whenever a table is dropped, all associated triggers are
296 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
297 ** and the table is dropped by a different database connection, the
298 ** trigger is not visible to the database connection that does the
299 ** drop so the trigger cannot be dropped. This results in an
300 ** "orphaned trigger" - a trigger whose associated table is missing.
301 **
302 ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df
303 */
304 db->init.orphanTrigger = 1;
305 }
306 goto trigger_cleanup;
drhf0f258b2003-04-21 18:48:45 +0000307}
308
309/*
310** This routine is called after all of the trigger actions have been parsed
311** in order to complete the process of building the trigger.
312*/
danielk19774adee202004-05-08 08:23:19 +0000313void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45 +0000314 Parse *pParse, /* Parser context */
315 TriggerStep *pStepList, /* The triggered program */
316 Token *pAll /* Token that describes the complete CREATE TRIGGER */
317){
drha8c62df2010-02-15 15:47:18 +0000318 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
319 char *zName; /* Name of trigger */
320 sqlite3 *db = pParse->db; /* The database */
321 DbFixer sFix; /* Fixer object */
322 int iDb; /* Database containing the trigger */
323 Token nameToken; /* Trigger name for error reporting */
drhf0f258b2003-04-21 18:48:45 +0000324
drhf0f258b2003-04-21 18:48:45 +0000325 pParse->pNewTrigger = 0;
drh9ab4c2e2009-05-09 00:18:38 +0000326 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
dan165921a2009-08-28 18:53:45 +0000327 zName = pTrig->zName;
danielk1977da184232006-01-05 11:34:32 +0000328 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32 +0000329 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53 +0000330 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32 +0000331 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53 +0000332 pStepList = pStepList->pNext;
333 }
drh40aced52016-01-22 17:48:09 +0000334 sqlite3TokenInit(&nameToken, pTrig->zName);
drhd100f692013-10-03 15:39:44 +0000335 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
336 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
dan46539d72013-10-03 12:29:38 +0000337 || sqlite3FixExpr(&sFix, pTrig->pWhen)
drhd100f692013-10-03 15:39:44 +0000338 ){
drhf26e09c2003-05-31 16:21:12 +0000339 goto triggerfinish_cleanup;
340 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000341
dan5496d6a2018-08-13 17:14:26 +0000342#ifndef SQLITE_OMIT_ALTERTABLE
danc9461ec2018-08-29 21:00:16 +0000343 if( IN_RENAME_OBJECT ){
dan5496d6a2018-08-13 17:14:26 +0000344 assert( !db->init.busy );
345 pParse->pNewTrigger = pTrig;
346 pTrig = 0;
347 }else
348#endif
349
drha8c62df2010-02-15 15:47:18 +0000350 /* if we are not initializing,
drh1e32bed2020-06-19 13:33:53 +0000351 ** build the sqlite_schema entry
drh9adf9ac2002-05-15 11:44:13 +0000352 */
drh1d85d932004-02-14 23:05:52 +0000353 if( !db->init.busy ){
drhc977f7f2002-05-21 11:38:11 +0000354 Vdbe *v;
drh2d401ab2008-01-10 23:50:11 +0000355 char *z;
danielk1977c3f9bad2002-05-15 08:30:12 +0000356
drhd7feae82022-10-18 18:55:52 +0000357 /* If this is a new CREATE TABLE statement, and if shadow tables
358 ** are read-only, and the trigger makes a change to a shadow table,
359 ** then raise an error - do not allow the trigger to be created. */
360 if( sqlite3ReadOnlyShadowTables(db) ){
361 TriggerStep *pStep;
362 for(pStep=pTrig->step_list; pStep; pStep=pStep->pNext){
363 if( pStep->zTarget!=0
364 && sqlite3ShadowTableName(db, pStep->zTarget)
365 ){
366 sqlite3ErrorMsg(pParse,
367 "trigger \"%s\" may not write to shadow table \"%s\"",
368 pTrig->zName, pStep->zTarget);
369 goto triggerfinish_cleanup;
370 }
371 }
372 }
373
drh1e32bed2020-06-19 13:33:53 +0000374 /* Make an entry in the sqlite_schema table */
danielk19774adee202004-05-08 08:23:19 +0000375 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000376 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000377 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11 +0000378 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
drhd96cc6f2017-06-08 14:35:21 +0000379 testcase( z==0 );
drh2d401ab2008-01-10 23:50:11 +0000380 sqlite3NestedParse(pParse,
drha4a871c2021-11-04 14:04:20 +0000381 "INSERT INTO %Q." LEGACY_SCHEMA_TABLE
drh346a70c2020-06-15 20:27:35 +0000382 " VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
383 db->aDb[iDb].zDbSName, zName,
drh2d401ab2008-01-10 23:50:11 +0000384 pTrig->table, z);
drh633e6d52008-07-28 19:34:53 +0000385 sqlite3DbFree(db, z);
drh9cbf3422008-01-17 16:22:13 +0000386 sqlite3ChangeCookie(pParse, iDb);
drh5d9c9da2011-06-03 20:11:17 +0000387 sqlite3VdbeAddParseSchemaOp(v, iDb,
dan6a5a13d2021-02-17 20:08:22 +0000388 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName), 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000389 }
390
drh956bc922004-07-24 17:38:29 +0000391 if( db->init.busy ){
danielk19772f886d12009-02-28 10:47:41 +0000392 Trigger *pLink = pTrig;
393 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
drh21206082011-04-04 18:22:02 +0000394 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh55f66b32019-07-16 19:44:32 +0000395 assert( pLink!=0 );
drhacbcb7e2014-08-21 20:26:37 +0000396 pTrig = sqlite3HashInsert(pHash, zName, pTrig);
danielk19772f886d12009-02-28 10:47:41 +0000397 if( pTrig ){
drh4a642b62016-02-05 01:55:27 +0000398 sqlite3OomFault(db);
danielk19772f886d12009-02-28 10:47:41 +0000399 }else if( pLink->pSchema==pLink->pTabSchema ){
400 Table *pTab;
drhacbcb7e2014-08-21 20:26:37 +0000401 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
danielk19772f886d12009-02-28 10:47:41 +0000402 assert( pTab!=0 );
403 pLink->pNext = pTab->pTrigger;
404 pTab->pTrigger = pLink;
danielk1977d5d56522005-03-16 12:15:20 +0000405 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000406 }
407
drhf0f258b2003-04-21 18:48:45 +0000408triggerfinish_cleanup:
drh633e6d52008-07-28 19:34:53 +0000409 sqlite3DeleteTrigger(db, pTrig);
danc9461ec2018-08-29 21:00:16 +0000410 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
drh633e6d52008-07-28 19:34:53 +0000411 sqlite3DeleteTriggerStep(db, pStepList);
drh4b59ab52002-08-24 18:24:51 +0000412}
danielk1977c3f9bad2002-05-15 08:30:12 +0000413
drh4b59ab52002-08-24 18:24:51 +0000414/*
drhf259df52017-12-27 20:38:35 +0000415** Duplicate a range of text from an SQL statement, then convert all
416** whitespace characters into ordinary space characters.
417*/
418static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
419 char *z = sqlite3DbSpanDup(db, zStart, zEnd);
420 int i;
421 if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i]) ) z[i] = ' ';
422 return z;
423}
424
425/*
drhc977f7f2002-05-21 11:38:11 +0000426** Turn a SELECT statement (that the pSelect parameter points to) into
427** a trigger step. Return a pointer to a TriggerStep structure.
428**
429** The parser calls this routine when it finds a SELECT statement in
430** body of a TRIGGER.
431*/
drhf259df52017-12-27 20:38:35 +0000432TriggerStep *sqlite3TriggerSelectStep(
433 sqlite3 *db, /* Database connection */
434 Select *pSelect, /* The SELECT statement */
435 const char *zStart, /* Start of SQL text */
436 const char *zEnd /* End of SQL text */
437){
drh17435752007-08-16 04:30:38 +0000438 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000439 if( pTriggerStep==0 ) {
drh633e6d52008-07-28 19:34:53 +0000440 sqlite3SelectDelete(db, pSelect);
danielk19772e588c72005-12-09 14:25:08 +0000441 return 0;
442 }
danielk1977633ed082002-05-17 00:05:58 +0000443 pTriggerStep->op = TK_SELECT;
444 pTriggerStep->pSelect = pSelect;
445 pTriggerStep->orconf = OE_Default;
drhf259df52017-12-27 20:38:35 +0000446 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
drhb7916a72009-05-27 10:31:29 +0000447 return pTriggerStep;
448}
danielk1977c3f9bad2002-05-15 08:30:12 +0000449
drhb7916a72009-05-27 10:31:29 +0000450/*
451** Allocate space to hold a new trigger step. The allocated space
452** holds both the TriggerStep object and the TriggerStep.target.z string.
453**
454** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
455*/
456static TriggerStep *triggerStepAllocate(
danc9461ec2018-08-29 21:00:16 +0000457 Parse *pParse, /* Parser context */
shane5eff7cf2009-08-10 03:57:58 +0000458 u8 op, /* Trigger opcode */
drhf259df52017-12-27 20:38:35 +0000459 Token *pName, /* The target name */
460 const char *zStart, /* Start of SQL text */
461 const char *zEnd /* End of SQL text */
drhb7916a72009-05-27 10:31:29 +0000462){
danc9461ec2018-08-29 21:00:16 +0000463 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000464 TriggerStep *pTriggerStep;
465
drh54068132022-03-07 16:40:44 +0000466 if( pParse->nErr ) return 0;
dan46408352015-04-21 16:38:49 +0000467 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
drhb7916a72009-05-27 10:31:29 +0000468 if( pTriggerStep ){
469 char *z = (char*)&pTriggerStep[1];
470 memcpy(z, pName->z, pName->n);
dan46408352015-04-21 16:38:49 +0000471 sqlite3Dequote(z);
472 pTriggerStep->zTarget = z;
drhb7916a72009-05-27 10:31:29 +0000473 pTriggerStep->op = op;
drhf259df52017-12-27 20:38:35 +0000474 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
danc9461ec2018-08-29 21:00:16 +0000475 if( IN_RENAME_OBJECT ){
476 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
477 }
drhb7916a72009-05-27 10:31:29 +0000478 }
danielk1977633ed082002-05-17 00:05:58 +0000479 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000480}
481
drhc977f7f2002-05-21 11:38:11 +0000482/*
483** Build a trigger step out of an INSERT statement. Return a pointer
484** to the new trigger step.
485**
486** The parser calls this routine when it sees an INSERT inside the
487** body of a trigger.
488*/
danielk19774adee202004-05-08 08:23:19 +0000489TriggerStep *sqlite3TriggerInsertStep(
dan5be60c52018-08-15 20:28:39 +0000490 Parse *pParse, /* Parser */
drhc977f7f2002-05-21 11:38:11 +0000491 Token *pTableName, /* Name of the table into which we insert */
492 IdList *pColumn, /* List of columns in pTableName to insert into */
drhc977f7f2002-05-21 11:38:11 +0000493 Select *pSelect, /* A SELECT statement that supplies values */
drhf259df52017-12-27 20:38:35 +0000494 u8 orconf, /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
drh46d2e5c2018-04-12 13:15:43 +0000495 Upsert *pUpsert, /* ON CONFLICT clauses for upsert */
drhf259df52017-12-27 20:38:35 +0000496 const char *zStart, /* Start of SQL text */
497 const char *zEnd /* End of SQL text */
danielk1977633ed082002-05-17 00:05:58 +0000498){
dan5be60c52018-08-15 20:28:39 +0000499 sqlite3 *db = pParse->db;
drhf3a65f72007-08-22 20:18:21 +0000500 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000501
drh75593d92014-01-10 20:46:55 +0000502 assert(pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000503
danc9461ec2018-08-29 21:00:16 +0000504 pTriggerStep = triggerStepAllocate(pParse, TK_INSERT, pTableName,zStart,zEnd);
danielk1977d5d56522005-03-16 12:15:20 +0000505 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000506 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000507 pTriggerStep->pSelect = pSelect;
508 pSelect = 0;
509 }else{
510 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
511 }
danielk1977d5d56522005-03-16 12:15:20 +0000512 pTriggerStep->pIdList = pColumn;
drh46d2e5c2018-04-12 13:15:43 +0000513 pTriggerStep->pUpsert = pUpsert;
danielk1977d5d56522005-03-16 12:15:20 +0000514 pTriggerStep->orconf = orconf;
dan9105fd52019-08-19 17:26:32 +0000515 if( pUpsert ){
516 sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
517 }
danielk1977d5d56522005-03-16 12:15:20 +0000518 }else{
drh99160482018-04-18 01:34:39 +0000519 testcase( pColumn );
drh633e6d52008-07-28 19:34:53 +0000520 sqlite3IdListDelete(db, pColumn);
drh99160482018-04-18 01:34:39 +0000521 testcase( pUpsert );
drh46d2e5c2018-04-12 13:15:43 +0000522 sqlite3UpsertDelete(db, pUpsert);
danielk1977d5d56522005-03-16 12:15:20 +0000523 }
drh33e619f2009-05-28 01:00:55 +0000524 sqlite3SelectDelete(db, pSelect);
danielk1977c3f9bad2002-05-15 08:30:12 +0000525
danielk1977633ed082002-05-17 00:05:58 +0000526 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000527}
528
drhc977f7f2002-05-21 11:38:11 +0000529/*
530** Construct a trigger step that implements an UPDATE statement and return
531** a pointer to that trigger step. The parser calls this routine when it
532** sees an UPDATE statement inside the body of a CREATE TRIGGER.
533*/
danielk19774adee202004-05-08 08:23:19 +0000534TriggerStep *sqlite3TriggerUpdateStep(
dan5be60c52018-08-15 20:28:39 +0000535 Parse *pParse, /* Parser */
drhc977f7f2002-05-21 11:38:11 +0000536 Token *pTableName, /* Name of the table to be updated */
dane7877b22020-07-14 19:51:01 +0000537 SrcList *pFrom,
drhc977f7f2002-05-21 11:38:11 +0000538 ExprList *pEList, /* The SET clause: list of column and new values */
539 Expr *pWhere, /* The WHERE clause */
drhf259df52017-12-27 20:38:35 +0000540 u8 orconf, /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
541 const char *zStart, /* Start of SQL text */
542 const char *zEnd /* End of SQL text */
drhc977f7f2002-05-21 11:38:11 +0000543){
dan5be60c52018-08-15 20:28:39 +0000544 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000545 TriggerStep *pTriggerStep;
546
danc9461ec2018-08-29 21:00:16 +0000547 pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE, pTableName,zStart,zEnd);
drh33e619f2009-05-28 01:00:55 +0000548 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000549 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000550 pTriggerStep->pExprList = pEList;
551 pTriggerStep->pWhere = pWhere;
dane7877b22020-07-14 19:51:01 +0000552 pTriggerStep->pFrom = pFrom;
dan5be60c52018-08-15 20:28:39 +0000553 pEList = 0;
554 pWhere = 0;
dane7877b22020-07-14 19:51:01 +0000555 pFrom = 0;
dan5be60c52018-08-15 20:28:39 +0000556 }else{
557 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
558 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
dane7877b22020-07-14 19:51:01 +0000559 pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE);
dan5be60c52018-08-15 20:28:39 +0000560 }
drh33e619f2009-05-28 01:00:55 +0000561 pTriggerStep->orconf = orconf;
drh0e3a6f32007-03-30 20:40:34 +0000562 }
drh33e619f2009-05-28 01:00:55 +0000563 sqlite3ExprListDelete(db, pEList);
564 sqlite3ExprDelete(db, pWhere);
dane7877b22020-07-14 19:51:01 +0000565 sqlite3SrcListDelete(db, pFrom);
danielk1977633ed082002-05-17 00:05:58 +0000566 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000567}
568
drhc977f7f2002-05-21 11:38:11 +0000569/*
570** Construct a trigger step that implements a DELETE statement and return
571** a pointer to that trigger step. The parser calls this routine when it
572** sees a DELETE statement inside the body of a CREATE TRIGGER.
573*/
drh17435752007-08-16 04:30:38 +0000574TriggerStep *sqlite3TriggerDeleteStep(
dan5be60c52018-08-15 20:28:39 +0000575 Parse *pParse, /* Parser */
drh17435752007-08-16 04:30:38 +0000576 Token *pTableName, /* The table from which rows are deleted */
drhf259df52017-12-27 20:38:35 +0000577 Expr *pWhere, /* The WHERE clause */
578 const char *zStart, /* Start of SQL text */
579 const char *zEnd /* End of SQL text */
drh17435752007-08-16 04:30:38 +0000580){
dan5be60c52018-08-15 20:28:39 +0000581 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000582 TriggerStep *pTriggerStep;
583
danc9461ec2018-08-29 21:00:16 +0000584 pTriggerStep = triggerStepAllocate(pParse, TK_DELETE, pTableName,zStart,zEnd);
drh33e619f2009-05-28 01:00:55 +0000585 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000586 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000587 pTriggerStep->pWhere = pWhere;
588 pWhere = 0;
589 }else{
590 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
591 }
drh33e619f2009-05-28 01:00:55 +0000592 pTriggerStep->orconf = OE_Default;
drhb63a53d2007-03-31 01:34:44 +0000593 }
drh33e619f2009-05-28 01:00:55 +0000594 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000595 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000596}
597
danielk1977633ed082002-05-17 00:05:58 +0000598/*
599** Recursively delete a Trigger structure
600*/
drh633e6d52008-07-28 19:34:53 +0000601void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
drh658f0a32021-01-30 02:43:26 +0000602 if( pTrigger==0 || pTrigger->bReturning ) return;
drh633e6d52008-07-28 19:34:53 +0000603 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
dan165921a2009-08-28 18:53:45 +0000604 sqlite3DbFree(db, pTrigger->zName);
drh633e6d52008-07-28 19:34:53 +0000605 sqlite3DbFree(db, pTrigger->table);
606 sqlite3ExprDelete(db, pTrigger->pWhen);
607 sqlite3IdListDelete(db, pTrigger->pColumns);
drh633e6d52008-07-28 19:34:53 +0000608 sqlite3DbFree(db, pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000609}
610
611/*
danielk19778a414492004-06-29 08:59:35 +0000612** This function is called to drop a trigger from the database schema.
613**
614** This may be called directly from the parser and therefore identifies
615** the trigger by name. The sqlite3DropTriggerPtr() routine does the
616** same job as this routine except it takes a pointer to the trigger
617** instead of the trigger name.
618**/
drhfdd48a72006-09-11 23:45:48 +0000619void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000620 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000621 int i;
622 const char *zDb;
623 const char *zName;
drh9bb575f2004-09-06 17:24:11 +0000624 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000625
drh17435752007-08-16 04:30:38 +0000626 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000627 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000628 goto drop_trigger_cleanup;
629 }
danielk19778e227872004-06-07 07:52:17 +0000630
drhd24cc422003-03-27 12:51:24 +0000631 assert( pName->nSrc==1 );
632 zDb = pName->a[0].zDatabase;
633 zName = pName->a[0].zName;
drh21206082011-04-04 18:22:02 +0000634 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
danielk197753c0f742005-03-29 03:10:59 +0000635 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000636 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
dan465c2b82020-03-21 15:10:40 +0000637 if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
drh21206082011-04-04 18:22:02 +0000638 assert( sqlite3SchemaMutexHeld(db, j, 0) );
drhacbcb7e2014-08-21 20:26:37 +0000639 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
drhd24cc422003-03-27 12:51:24 +0000640 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000641 }
drhd24cc422003-03-27 12:51:24 +0000642 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000643 if( !noErr ){
drha9799932021-03-19 13:00:28 +0000644 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName->a);
dan57966752011-04-09 17:32:58 +0000645 }else{
646 sqlite3CodeVerifyNamedSchema(pParse, zDb);
drhfdd48a72006-09-11 23:45:48 +0000647 }
dan1db95102010-06-28 10:15:19 +0000648 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +0000649 goto drop_trigger_cleanup;
650 }
drh74161702006-02-24 02:53:49 +0000651 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000652
653drop_trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000654 sqlite3SrcListDelete(db, pName);
drh79a519c2003-05-17 19:04:03 +0000655}
656
657/*
drh956bc922004-07-24 17:38:29 +0000658** Return a pointer to the Table structure for the table that a trigger
659** is set on.
660*/
drh74161702006-02-24 02:53:49 +0000661static Table *tableOfTrigger(Trigger *pTrigger){
drhacbcb7e2014-08-21 20:26:37 +0000662 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
drh956bc922004-07-24 17:38:29 +0000663}
664
665
666/*
drh74161702006-02-24 02:53:49 +0000667** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000668*/
drh74161702006-02-24 02:53:49 +0000669void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000670 Table *pTable;
671 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000672 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000673 int iDb;
drh79a519c2003-05-17 19:04:03 +0000674
danielk1977da184232006-01-05 11:34:32 +0000675 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000676 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000677 pTable = tableOfTrigger(pTrigger);
drh6397a782019-08-27 10:05:45 +0000678 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000679#ifndef SQLITE_OMIT_AUTHORIZATION
drh6397a782019-08-27 10:05:45 +0000680 if( pTable ){
drhe5f9c642003-01-13 23:27:31 +0000681 int code = SQLITE_DROP_TRIGGER;
drh69c33822016-08-18 14:33:11 +0000682 const char *zDb = db->aDb[iDb].zDbSName;
drh956bc922004-07-24 17:38:29 +0000683 const char *zTab = SCHEMA_TABLE(iDb);
684 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
dan2bd93512009-08-31 08:22:46 +0000685 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
danielk19774adee202004-05-08 08:23:19 +0000686 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000687 return;
drhe5f9c642003-01-13 23:27:31 +0000688 }
drhed6c8672003-01-12 18:02:16 +0000689 }
drhe5f9c642003-01-13 23:27:31 +0000690#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000691
drhf0f258b2003-04-21 18:48:45 +0000692 /* Generate code to destroy the database record of the trigger.
693 */
drh43617e92006-03-06 20:55:46 +0000694 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drh8c8dddc2015-12-09 16:26:38 +0000695 sqlite3NestedParse(pParse,
drha4a871c2021-11-04 14:04:20 +0000696 "DELETE FROM %Q." LEGACY_SCHEMA_TABLE " WHERE name=%Q AND type='trigger'",
drh346a70c2020-06-15 20:27:35 +0000697 db->aDb[iDb].zDbSName, pTrigger->zName
drh8c8dddc2015-12-09 16:26:38 +0000698 );
drh9cbf3422008-01-17 16:22:13 +0000699 sqlite3ChangeCookie(pParse, iDb);
dan165921a2009-08-28 18:53:45 +0000700 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
drhf0f258b2003-04-21 18:48:45 +0000701 }
drh956bc922004-07-24 17:38:29 +0000702}
drhf0f258b2003-04-21 18:48:45 +0000703
drh956bc922004-07-24 17:38:29 +0000704/*
705** Remove a trigger from the hash tables of the sqlite* pointer.
706*/
707void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
708 Trigger *pTrigger;
drh21206082011-04-04 18:22:02 +0000709 Hash *pHash;
710
711 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
712 pHash = &(db->aDb[iDb].pSchema->trigHash);
drhacbcb7e2014-08-21 20:26:37 +0000713 pTrigger = sqlite3HashInsert(pHash, zName, 0);
drh9ab4c2e2009-05-09 00:18:38 +0000714 if( ALWAYS(pTrigger) ){
danielk19772f886d12009-02-28 10:47:41 +0000715 if( pTrigger->pSchema==pTrigger->pTabSchema ){
716 Table *pTab = tableOfTrigger(pTrigger);
drh6397a782019-08-27 10:05:45 +0000717 if( pTab ){
718 Trigger **pp;
dan0232dad2019-12-03 03:34:06 +0000719 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
720 if( *pp==pTrigger ){
721 *pp = (*pp)->pNext;
722 break;
723 }
724 }
drh6397a782019-08-27 10:05:45 +0000725 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000726 }
drh633e6d52008-07-28 19:34:53 +0000727 sqlite3DeleteTrigger(db, pTrigger);
drh8257aa82017-07-26 19:59:13 +0000728 db->mDbFlags |= DBFLAG_SchemaChange;
danielk1977c3f9bad2002-05-15 08:30:12 +0000729 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000730}
731
drhc977f7f2002-05-21 11:38:11 +0000732/*
733** pEList is the SET clause of an UPDATE statement. Each entry
734** in pEList is of the format <id>=<expr>. If any of the entries
735** in pEList have an <id> which matches an identifier in pIdList,
736** then return TRUE. If pIdList==NULL, then it is considered a
737** wildcard that matches anything. Likewise if pEList==NULL then
738** it matches anything so always return true. Return false only
739** if there is no match.
740*/
drh9ab4c2e2009-05-09 00:18:38 +0000741static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000742 int e;
drh9ab4c2e2009-05-09 00:18:38 +0000743 if( pIdList==0 || NEVER(pEList==0) ) return 1;
drhad2d8302002-05-24 20:31:36 +0000744 for(e=0; e<pEList->nExpr; e++){
drh41cee662019-12-12 20:22:34 +0000745 if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000746 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000747 return 0;
748}
749
danielk1977c3f9bad2002-05-15 08:30:12 +0000750/*
danielk19772f886d12009-02-28 10:47:41 +0000751** Return a list of all triggers on table pTab if there exists at least
752** one trigger that must be fired when an operation of type 'op' is
753** performed on the table, and, if that operation is an UPDATE, if at
754** least one of the columns in pChanges is being modified.
drhdca76842004-12-07 14:06:13 +0000755*/
danielk19772f886d12009-02-28 10:47:41 +0000756Trigger *sqlite3TriggersExist(
757 Parse *pParse, /* Parse context */
drhdca76842004-12-07 14:06:13 +0000758 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000759 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
danielk19772f886d12009-02-28 10:47:41 +0000760 ExprList *pChanges, /* Columns that change in an UPDATE statement */
761 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
drhc977f7f2002-05-21 11:38:11 +0000762){
drhdca76842004-12-07 14:06:13 +0000763 int mask = 0;
drhe83cafd2011-03-21 17:15:58 +0000764 Trigger *pList = 0;
danielk19772f886d12009-02-28 10:47:41 +0000765 Trigger *p;
drhe83cafd2011-03-21 17:15:58 +0000766
drh2aa41c82021-02-03 00:55:34 +0000767 pList = sqlite3TriggerList(pParse, pTab);
drhc9be8632021-02-02 00:16:15 +0000768 assert( pList==0 || IsVirtual(pTab)==0
769 || (pList->bReturning && pList->pNext==0) );
drh2aa41c82021-02-03 00:55:34 +0000770 if( pList!=0 ){
771 p = pList;
772 if( (pParse->db->flags & SQLITE_EnableTrigger)==0
773 && pTab->pTrigger!=0
774 ){
775 /* The SQLITE_DBCONFIG_ENABLE_TRIGGER setting is off. That means that
776 ** only TEMP triggers are allowed. Truncate the pList so that it
777 ** includes only TEMP triggers */
778 if( pList==pTab->pTrigger ){
779 pList = 0;
780 goto exit_triggers_exist;
drh709dd132021-02-02 12:01:22 +0000781 }
drh2aa41c82021-02-03 00:55:34 +0000782 while( ALWAYS(p->pNext) && p->pNext!=pTab->pTrigger ) p = p->pNext;
783 p->pNext = 0;
784 p = pList;
danielk1977c3f9bad2002-05-15 08:30:12 +0000785 }
drh2aa41c82021-02-03 00:55:34 +0000786 do{
787 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
788 mask |= p->tr_tm;
789 }else if( p->op==TK_RETURNING ){
790 /* The first time a RETURNING trigger is seen, the "op" value tells
791 ** us what time of trigger it should be. */
792 assert( sqlite3IsToplevel(pParse) );
793 p->op = op;
drh7dec8042021-02-04 22:59:19 +0000794 if( IsVirtual(pTab) ){
795 if( op!=TK_INSERT ){
796 sqlite3ErrorMsg(pParse,
797 "%s RETURNING is not available on virtual tables",
798 op==TK_DELETE ? "DELETE" : "UPDATE");
799 }
800 p->tr_tm = TRIGGER_BEFORE;
801 }else{
802 p->tr_tm = TRIGGER_AFTER;
drh2aa41c82021-02-03 00:55:34 +0000803 }
drh7dec8042021-02-04 22:59:19 +0000804 mask |= p->tr_tm;
drh2aa41c82021-02-03 00:55:34 +0000805 }else if( p->bReturning && p->op==TK_INSERT && op==TK_UPDATE
806 && sqlite3IsToplevel(pParse) ){
807 /* Also fire a RETURNING trigger for an UPSERT */
drh7dec8042021-02-04 22:59:19 +0000808 mask |= p->tr_tm;
drh2aa41c82021-02-03 00:55:34 +0000809 }
810 p = p->pNext;
811 }while( p );
danielk1977c3f9bad2002-05-15 08:30:12 +0000812 }
drh2aa41c82021-02-03 00:55:34 +0000813exit_triggers_exist:
danielk19772f886d12009-02-28 10:47:41 +0000814 if( pMask ){
815 *pMask = mask;
816 }
817 return (mask ? pList : 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000818}
819
drhc977f7f2002-05-21 11:38:11 +0000820/*
dan46408352015-04-21 16:38:49 +0000821** Convert the pStep->zTarget string into a SrcList and return a pointer
drhf26e09c2003-05-31 16:21:12 +0000822** to that SrcList.
823**
824** This routine adds a specific database name, if needed, to the target when
825** forming the SrcList. This prevents a trigger in one database from
826** referring to a target in another database. An exception is when the
827** trigger is in TEMP in which case it can refer to any other database it
828** wants.
829*/
dane7877b22020-07-14 19:51:01 +0000830SrcList *sqlite3TriggerStepSrc(
drhf26e09c2003-05-31 16:21:12 +0000831 Parse *pParse, /* The parsing context */
832 TriggerStep *pStep /* The trigger containing the target token */
833){
dan46408352015-04-21 16:38:49 +0000834 sqlite3 *db = pParse->db;
dane7877b22020-07-14 19:51:01 +0000835 SrcList *pSrc; /* SrcList to be returned */
836 char *zName = sqlite3DbStrDup(db, pStep->zTarget);
drh29c992c2019-01-17 15:40:41 +0000837 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
dane7877b22020-07-14 19:51:01 +0000838 assert( pSrc==0 || pSrc->nSrc==1 );
839 assert( zName || pSrc==0 );
drhb7916a72009-05-27 10:31:29 +0000840 if( pSrc ){
dane7877b22020-07-14 19:51:01 +0000841 Schema *pSchema = pStep->pTrig->pSchema;
842 pSrc->a[0].zName = zName;
843 if( pSchema!=db->aDb[1].pSchema ){
844 pSrc->a[0].pSchema = pSchema;
drhb7916a72009-05-27 10:31:29 +0000845 }
dane7877b22020-07-14 19:51:01 +0000846 if( pStep->pFrom ){
847 SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0);
848 pSrc = sqlite3SrcListAppendList(pParse, pSrc, pDup);
849 }
850 }else{
851 sqlite3DbFree(db, zName);
drhf26e09c2003-05-31 16:21:12 +0000852 }
853 return pSrc;
854}
855
drh0d23f672021-03-30 01:52:21 +0000856/*
857** Return true if the pExpr term from the RETURNING clause argument
858** list is of the form "*". Raise an error if the terms if of the
859** form "table.*".
860*/
861static int isAsteriskTerm(
862 Parse *pParse, /* Parsing context */
863 Expr *pTerm /* A term in the RETURNING clause */
864){
865 assert( pTerm!=0 );
866 if( pTerm->op==TK_ASTERISK ) return 1;
867 if( pTerm->op!=TK_DOT ) return 0;
868 assert( pTerm->pRight!=0 );
869 assert( pTerm->pLeft!=0 );
870 if( pTerm->pRight->op!=TK_ASTERISK ) return 0;
871 sqlite3ErrorMsg(pParse, "RETURNING may not use \"TABLE.*\" wildcards");
872 return 1;
873}
874
drhdac9a5f2021-01-29 21:18:46 +0000875/* The input list pList is the list of result set terms from a RETURNING
876** clause. The table that we are returning from is pTab.
877**
878** This routine makes a copy of the pList, and at the same time expands
879** any "*" wildcards to be the complete set of columns from pTab.
880*/
881static ExprList *sqlite3ExpandReturning(
882 Parse *pParse, /* Parsing context */
883 ExprList *pList, /* The arguments to RETURNING */
884 Table *pTab /* The table being updated */
885){
886 ExprList *pNew = 0;
887 sqlite3 *db = pParse->db;
888 int i;
drh552562c2021-02-04 20:52:20 +0000889
drhdac9a5f2021-01-29 21:18:46 +0000890 for(i=0; i<pList->nExpr; i++){
891 Expr *pOldExpr = pList->a[i].pExpr;
drh0d23f672021-03-30 01:52:21 +0000892 if( NEVER(pOldExpr==0) ) continue;
893 if( isAsteriskTerm(pParse, pOldExpr) ){
drhc9be8632021-02-02 00:16:15 +0000894 int jj;
895 for(jj=0; jj<pTab->nCol; jj++){
drh87296422021-02-07 12:59:43 +0000896 Expr *pNewExpr;
drhc9be8632021-02-02 00:16:15 +0000897 if( IsHiddenColumn(pTab->aCol+jj) ) continue;
drhcf9d36d2021-08-02 18:03:43 +0000898 pNewExpr = sqlite3Expr(db, TK_ID, pTab->aCol[jj].zCnName);
drhdac9a5f2021-01-29 21:18:46 +0000899 pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr);
900 if( !db->mallocFailed ){
901 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
drhcf9d36d2021-08-02 18:03:43 +0000902 pItem->zEName = sqlite3DbStrDup(db, pTab->aCol[jj].zCnName);
drhdac9a5f2021-01-29 21:18:46 +0000903 pItem->eEName = ENAME_NAME;
904 }
905 }
906 }else{
907 Expr *pNewExpr = sqlite3ExprDup(db, pOldExpr, 0);
908 pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr);
drh9407b6e2021-01-31 16:45:10 +0000909 if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0) ){
drhdac9a5f2021-01-29 21:18:46 +0000910 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
911 pItem->zEName = sqlite3DbStrDup(db, pList->a[i].zEName);
912 pItem->eEName = pList->a[i].eEName;
913 }
914 }
915 }
916 return pNew;
917}
918
drhf26e09c2003-05-31 16:21:12 +0000919/*
drh381bdac2021-02-04 17:29:04 +0000920** Generate code for the RETURNING trigger. Unlike other triggers
921** that invoke a subprogram in the bytecode, the code for RETURNING
922** is generated in-line.
923*/
924static void codeReturningTrigger(
925 Parse *pParse, /* Parse context */
926 Trigger *pTrigger, /* The trigger step that defines the RETURNING */
927 Table *pTab, /* The table to code triggers from */
drh552562c2021-02-04 20:52:20 +0000928 int regIn /* The first in an array of registers */
drh381bdac2021-02-04 17:29:04 +0000929){
930 Vdbe *v = pParse->pVdbe;
drh90881862021-05-19 12:17:03 +0000931 sqlite3 *db = pParse->db;
drh381bdac2021-02-04 17:29:04 +0000932 ExprList *pNew;
933 Returning *pReturning;
drh90881862021-05-19 12:17:03 +0000934 Select sSelect;
935 SrcList sFrom;
drh381bdac2021-02-04 17:29:04 +0000936
937 assert( v!=0 );
938 assert( pParse->bReturning );
drh0c7d3d32022-01-24 16:47:12 +0000939 assert( db->pParse==pParse );
drh381bdac2021-02-04 17:29:04 +0000940 pReturning = pParse->u1.pReturning;
941 assert( pTrigger == &(pReturning->retTrig) );
drh90881862021-05-19 12:17:03 +0000942 memset(&sSelect, 0, sizeof(sSelect));
943 memset(&sFrom, 0, sizeof(sFrom));
944 sSelect.pEList = sqlite3ExprListDup(db, pReturning->pReturnEL, 0);
945 sSelect.pSrc = &sFrom;
946 sFrom.nSrc = 1;
947 sFrom.a[0].pTab = pTab;
dancf1e2552021-06-28 15:25:17 +0000948 sFrom.a[0].iCursor = -1;
drh90881862021-05-19 12:17:03 +0000949 sqlite3SelectPrep(pParse, &sSelect, 0);
drh0c7d3d32022-01-24 16:47:12 +0000950 if( pParse->nErr==0 ){
951 assert( db->mallocFailed==0 );
drh90881862021-05-19 12:17:03 +0000952 sqlite3GenerateColumnNames(pParse, &sSelect);
953 }
954 sqlite3ExprListDelete(db, sSelect.pEList);
drh381bdac2021-02-04 17:29:04 +0000955 pNew = sqlite3ExpandReturning(pParse, pReturning->pReturnEL, pTab);
drhebc44342022-01-05 11:49:58 +0000956 if( !db->mallocFailed ){
drh552562c2021-02-04 20:52:20 +0000957 NameContext sNC;
958 memset(&sNC, 0, sizeof(sNC));
drhe1db9342021-02-04 21:17:12 +0000959 if( pReturning->nRetCol==0 ){
960 pReturning->nRetCol = pNew->nExpr;
961 pReturning->iRetCur = pParse->nTab++;
962 }
drh552562c2021-02-04 20:52:20 +0000963 sNC.pParse = pParse;
964 sNC.uNC.iBaseReg = regIn;
965 sNC.ncFlags = NC_UBaseReg;
966 pParse->eTriggerOp = pTrigger->op;
967 pParse->pTriggerTab = pTab;
danc6977c12022-01-05 15:54:02 +0000968 if( sqlite3ResolveExprListNames(&sNC, pNew)==SQLITE_OK
drh1da88b52022-01-24 19:38:56 +0000969 && ALWAYS(!db->mallocFailed)
danc6977c12022-01-05 15:54:02 +0000970 ){
drh552562c2021-02-04 20:52:20 +0000971 int i;
972 int nCol = pNew->nExpr;
973 int reg = pParse->nMem+1;
974 pParse->nMem += nCol+2;
975 pReturning->iRetReg = reg;
976 for(i=0; i<nCol; i++){
drh90881862021-05-19 12:17:03 +0000977 Expr *pCol = pNew->a[i].pExpr;
danc6977c12022-01-05 15:54:02 +0000978 assert( pCol!=0 ); /* Due to !db->mallocFailed ~9 lines above */
drh90881862021-05-19 12:17:03 +0000979 sqlite3ExprCodeFactorable(pParse, pCol, reg+i);
drh41584df2021-12-29 04:31:54 +0000980 if( sqlite3ExprAffinity(pCol)==SQLITE_AFF_REAL ){
981 sqlite3VdbeAddOp1(v, OP_RealAffinity, reg+i);
982 }
drh381bdac2021-02-04 17:29:04 +0000983 }
drh552562c2021-02-04 20:52:20 +0000984 sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, i, reg+i);
985 sqlite3VdbeAddOp2(v, OP_NewRowid, pReturning->iRetCur, reg+i+1);
986 sqlite3VdbeAddOp3(v, OP_Insert, pReturning->iRetCur, reg+i, reg+i+1);
987 }
drh552562c2021-02-04 20:52:20 +0000988 }
drhebc44342022-01-05 11:49:58 +0000989 sqlite3ExprListDelete(db, pNew);
990 pParse->eTriggerOp = 0;
991 pParse->pTriggerTab = 0;
drh381bdac2021-02-04 17:29:04 +0000992}
993
994
995
996/*
dan65a7cd12009-09-01 12:16:01 +0000997** Generate VDBE code for the statements inside the body of a single
998** trigger.
drhc977f7f2002-05-21 11:38:11 +0000999*/
danielk1977c3f9bad2002-05-15 08:30:12 +00001000static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +00001001 Parse *pParse, /* The parser context */
1002 TriggerStep *pStepList, /* List of statements inside the trigger body */
dan65a7cd12009-09-01 12:16:01 +00001003 int orconf /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +00001004){
dan65a7cd12009-09-01 12:16:01 +00001005 TriggerStep *pStep;
drh344737f2004-09-19 00:50:20 +00001006 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +00001007 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +00001008
dan65a7cd12009-09-01 12:16:01 +00001009 assert( pParse->pTriggerTab && pParse->pToplevel );
1010 assert( pStepList );
drh344737f2004-09-19 00:50:20 +00001011 assert( v!=0 );
dan65a7cd12009-09-01 12:16:01 +00001012 for(pStep=pStepList; pStep; pStep=pStep->pNext){
dan165921a2009-08-28 18:53:45 +00001013 /* Figure out the ON CONFLICT policy that will be used for this step
1014 ** of the trigger program. If the statement that caused this trigger
1015 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
1016 ** the ON CONFLICT policy that was specified as part of the trigger
1017 ** step statement. Example:
1018 **
1019 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
1020 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
1021 ** END;
1022 **
1023 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
1024 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
1025 */
shanecea72b22009-09-07 04:38:36 +00001026 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
drhaceb31b2014-02-08 01:40:27 +00001027 assert( pParse->okConstFactor==0 );
danf78baaf2012-12-06 19:37:22 +00001028
drhf259df52017-12-27 20:38:35 +00001029#ifndef SQLITE_OMIT_TRACE
drhe307e112017-12-27 21:30:34 +00001030 if( pStep->zSpan ){
1031 sqlite3VdbeAddOp4(v, OP_Trace, 0x7fffffff, 1, 0,
1032 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
1033 P4_DYNAMIC);
1034 }
drhf259df52017-12-27 20:38:35 +00001035#endif
1036
dan165921a2009-08-28 18:53:45 +00001037 switch( pStep->op ){
danielk1977633ed082002-05-17 00:05:58 +00001038 case TK_UPDATE: {
dan165921a2009-08-28 18:53:45 +00001039 sqlite3Update(pParse,
dane7877b22020-07-14 19:51:01 +00001040 sqlite3TriggerStepSrc(pParse, pStep),
dan165921a2009-08-28 18:53:45 +00001041 sqlite3ExprListDup(db, pStep->pExprList, 0),
1042 sqlite3ExprDup(db, pStep->pWhere, 0),
drheac9fab2018-04-16 13:00:50 +00001043 pParse->eOrconf, 0, 0, 0
dan165921a2009-08-28 18:53:45 +00001044 );
drhdac9a5f2021-01-29 21:18:46 +00001045 sqlite3VdbeAddOp0(v, OP_ResetCount);
danielk1977633ed082002-05-17 00:05:58 +00001046 break;
1047 }
1048 case TK_INSERT: {
dan165921a2009-08-28 18:53:45 +00001049 sqlite3Insert(pParse,
dane7877b22020-07-14 19:51:01 +00001050 sqlite3TriggerStepSrc(pParse, pStep),
dan165921a2009-08-28 18:53:45 +00001051 sqlite3SelectDup(db, pStep->pSelect, 0),
1052 sqlite3IdListDup(db, pStep->pIdList),
drh46d2e5c2018-04-12 13:15:43 +00001053 pParse->eOrconf,
1054 sqlite3UpsertDup(db, pStep->pUpsert)
dan165921a2009-08-28 18:53:45 +00001055 );
drhdac9a5f2021-01-29 21:18:46 +00001056 sqlite3VdbeAddOp0(v, OP_ResetCount);
danielk1977633ed082002-05-17 00:05:58 +00001057 break;
1058 }
1059 case TK_DELETE: {
dan165921a2009-08-28 18:53:45 +00001060 sqlite3DeleteFrom(pParse,
dane7877b22020-07-14 19:51:01 +00001061 sqlite3TriggerStepSrc(pParse, pStep),
drh8c0833f2017-11-14 23:48:23 +00001062 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
dan165921a2009-08-28 18:53:45 +00001063 );
drhdac9a5f2021-01-29 21:18:46 +00001064 sqlite3VdbeAddOp0(v, OP_ResetCount);
danielk1977633ed082002-05-17 00:05:58 +00001065 break;
1066 }
drh381bdac2021-02-04 17:29:04 +00001067 default: assert( pStep->op==TK_SELECT ); {
dan165921a2009-08-28 18:53:45 +00001068 SelectDest sDest;
1069 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
drhdac9a5f2021-01-29 21:18:46 +00001070 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
dan165921a2009-08-28 18:53:45 +00001071 sqlite3Select(pParse, pSelect, &sDest);
1072 sqlite3SelectDelete(db, pSelect);
drh9ab4c2e2009-05-09 00:18:38 +00001073 break;
1074 }
danielk1977633ed082002-05-17 00:05:58 +00001075 }
danielk1977633ed082002-05-17 00:05:58 +00001076 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001077
danielk1977633ed082002-05-17 00:05:58 +00001078 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +00001079}
1080
drhc7379ce2013-10-30 02:28:23 +00001081#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
dan165921a2009-08-28 18:53:45 +00001082/*
1083** This function is used to add VdbeComment() annotations to a VDBE
1084** program. It is not used in production code, only for debugging.
1085*/
1086static const char *onErrorText(int onError){
1087 switch( onError ){
1088 case OE_Abort: return "abort";
1089 case OE_Rollback: return "rollback";
1090 case OE_Fail: return "fail";
1091 case OE_Replace: return "replace";
1092 case OE_Ignore: return "ignore";
1093 case OE_Default: return "default";
1094 }
1095 return "n/a";
1096}
1097#endif
1098
1099/*
1100** Parse context structure pFrom has just been used to create a sub-vdbe
1101** (trigger program). If an error has occurred, transfer error information
1102** from pFrom to pTo.
1103*/
1104static void transferParseError(Parse *pTo, Parse *pFrom){
1105 assert( pFrom->zErrMsg==0 || pFrom->nErr );
1106 assert( pTo->zErrMsg==0 || pTo->nErr );
1107 if( pTo->nErr==0 ){
1108 pTo->zErrMsg = pFrom->zErrMsg;
1109 pTo->nErr = pFrom->nErr;
drhe06874e2015-04-16 15:47:06 +00001110 pTo->rc = pFrom->rc;
dan165921a2009-08-28 18:53:45 +00001111 }else{
1112 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
1113 }
1114}
1115
dan65a7cd12009-09-01 12:16:01 +00001116/*
1117** Create and populate a new TriggerPrg object with a sub-program
1118** implementing trigger pTrigger with ON CONFLICT policy orconf.
1119*/
dan2832ad42009-08-31 15:27:27 +00001120static TriggerPrg *codeRowTrigger(
dan165921a2009-08-28 18:53:45 +00001121 Parse *pParse, /* Current parse context */
1122 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +00001123 Table *pTab, /* The table pTrigger is attached to */
1124 int orconf /* ON CONFLICT policy to code trigger program with */
dan165921a2009-08-28 18:53:45 +00001125){
dan65a7cd12009-09-01 12:16:01 +00001126 Parse *pTop = sqlite3ParseToplevel(pParse);
1127 sqlite3 *db = pParse->db; /* Database handle */
1128 TriggerPrg *pPrg; /* Value to return */
dan165921a2009-08-28 18:53:45 +00001129 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
1130 Vdbe *v; /* Temporary VM */
dan165921a2009-08-28 18:53:45 +00001131 NameContext sNC; /* Name context for sub-vdbe */
1132 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
dan165921a2009-08-28 18:53:45 +00001133 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
drhc692df22022-01-24 15:34:55 +00001134 Parse sSubParse; /* Parse context for sub-vdbe */
dan165921a2009-08-28 18:53:45 +00001135
dan1da40a32009-09-19 17:00:31 +00001136 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dand19c9332010-07-26 12:05:17 +00001137 assert( pTop->pVdbe );
dan65a7cd12009-09-01 12:16:01 +00001138
1139 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
1140 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
1141 ** list of the top-level Parse object sooner rather than later. */
dan2832ad42009-08-31 15:27:27 +00001142 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
1143 if( !pPrg ) return 0;
dan65a7cd12009-09-01 12:16:01 +00001144 pPrg->pNext = pTop->pTriggerPrg;
1145 pTop->pTriggerPrg = pPrg;
dan2832ad42009-08-31 15:27:27 +00001146 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
dan165921a2009-08-28 18:53:45 +00001147 if( !pProgram ) return 0;
dand19c9332010-07-26 12:05:17 +00001148 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
dan2832ad42009-08-31 15:27:27 +00001149 pPrg->pTrigger = pTrigger;
1150 pPrg->orconf = orconf;
danbb5f1682009-11-27 12:12:34 +00001151 pPrg->aColmask[0] = 0xffffffff;
1152 pPrg->aColmask[1] = 0xffffffff;
dan165921a2009-08-28 18:53:45 +00001153
dan65a7cd12009-09-01 12:16:01 +00001154 /* Allocate and populate a new Parse context to use for coding the
1155 ** trigger sub-program. */
drhc692df22022-01-24 15:34:55 +00001156 sqlite3ParseObjectInit(&sSubParse, db);
dan165921a2009-08-28 18:53:45 +00001157 memset(&sNC, 0, sizeof(sNC));
drhc692df22022-01-24 15:34:55 +00001158 sNC.pParse = &sSubParse;
1159 sSubParse.pTriggerTab = pTab;
1160 sSubParse.pToplevel = pTop;
1161 sSubParse.zAuthContext = pTrigger->zName;
1162 sSubParse.eTriggerOp = pTrigger->op;
1163 sSubParse.nQueryLoop = pParse->nQueryLoop;
1164 sSubParse.disableVtab = pParse->disableVtab;
dan165921a2009-08-28 18:53:45 +00001165
drhc692df22022-01-24 15:34:55 +00001166 v = sqlite3GetVdbe(&sSubParse);
dan165921a2009-08-28 18:53:45 +00001167 if( v ){
dan76d462e2009-08-30 11:42:51 +00001168 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
1169 pTrigger->zName, onErrorText(orconf),
dan165921a2009-08-28 18:53:45 +00001170 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
dan65a7cd12009-09-01 12:16:01 +00001171 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
1172 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
1173 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
dan76d462e2009-08-30 11:42:51 +00001174 pTab->zName
dan165921a2009-08-28 18:53:45 +00001175 ));
dan76d462e2009-08-30 11:42:51 +00001176#ifndef SQLITE_OMIT_TRACE
drhe307e112017-12-27 21:30:34 +00001177 if( pTrigger->zName ){
1178 sqlite3VdbeChangeP4(v, -1,
1179 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
1180 );
1181 }
dan76d462e2009-08-30 11:42:51 +00001182#endif
dan165921a2009-08-28 18:53:45 +00001183
dan65a7cd12009-09-01 12:16:01 +00001184 /* If one was specified, code the WHEN clause. If it evaluates to false
1185 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
1186 ** OP_Halt inserted at the end of the program. */
dan165921a2009-08-28 18:53:45 +00001187 if( pTrigger->pWhen ){
dan165921a2009-08-28 18:53:45 +00001188 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
drh4c4a2572021-04-06 12:50:24 +00001189 if( db->mallocFailed==0
1190 && SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
dan523a0872009-08-31 05:23:32 +00001191 ){
drhc692df22022-01-24 15:34:55 +00001192 iEndTrigger = sqlite3VdbeMakeLabel(&sSubParse);
1193 sqlite3ExprIfFalse(&sSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
dan165921a2009-08-28 18:53:45 +00001194 }
1195 sqlite3ExprDelete(db, pWhen);
1196 }
1197
1198 /* Code the trigger program into the sub-vdbe. */
drhc692df22022-01-24 15:34:55 +00001199 codeTriggerProgram(&sSubParse, pTrigger->step_list, orconf);
dan65a7cd12009-09-01 12:16:01 +00001200
1201 /* Insert an OP_Halt at the end of the sub-program. */
dan165921a2009-08-28 18:53:45 +00001202 if( iEndTrigger ){
1203 sqlite3VdbeResolveLabel(v, iEndTrigger);
1204 }
1205 sqlite3VdbeAddOp0(v, OP_Halt);
dan76d462e2009-08-30 11:42:51 +00001206 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
drhc692df22022-01-24 15:34:55 +00001207 transferParseError(pParse, &sSubParse);
dan76d462e2009-08-30 11:42:51 +00001208
drh0c7d3d32022-01-24 16:47:12 +00001209 if( pParse->nErr==0 ){
1210 assert( db->mallocFailed==0 );
dan65a7cd12009-09-01 12:16:01 +00001211 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
dan523a0872009-08-31 05:23:32 +00001212 }
drhc692df22022-01-24 15:34:55 +00001213 pProgram->nMem = sSubParse.nMem;
1214 pProgram->nCsr = sSubParse.nTab;
dan165921a2009-08-28 18:53:45 +00001215 pProgram->token = (void *)pTrigger;
drhc692df22022-01-24 15:34:55 +00001216 pPrg->aColmask[0] = sSubParse.oldmask;
1217 pPrg->aColmask[1] = sSubParse.newmask;
dan165921a2009-08-28 18:53:45 +00001218 sqlite3VdbeDelete(v);
drh0c7d3d32022-01-24 16:47:12 +00001219 }else{
1220 transferParseError(pParse, &sSubParse);
dan165921a2009-08-28 18:53:45 +00001221 }
dan65a7cd12009-09-01 12:16:01 +00001222
drhc692df22022-01-24 15:34:55 +00001223 assert( !sSubParse.pTriggerPrg && !sSubParse.nMaxArg );
1224 sqlite3ParseObjectReset(&sSubParse);
dan2832ad42009-08-31 15:27:27 +00001225 return pPrg;
dan165921a2009-08-28 18:53:45 +00001226}
1227
dan65a7cd12009-09-01 12:16:01 +00001228/*
1229** Return a pointer to a TriggerPrg object containing the sub-program for
1230** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
1231** TriggerPrg object exists, a new object is allocated and populated before
1232** being returned.
1233*/
dan2832ad42009-08-31 15:27:27 +00001234static TriggerPrg *getRowTrigger(
dan65a7cd12009-09-01 12:16:01 +00001235 Parse *pParse, /* Current parse context */
dan165921a2009-08-28 18:53:45 +00001236 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +00001237 Table *pTab, /* The table trigger pTrigger is attached to */
1238 int orconf /* ON CONFLICT algorithm. */
dan165921a2009-08-28 18:53:45 +00001239){
dan65a7cd12009-09-01 12:16:01 +00001240 Parse *pRoot = sqlite3ParseToplevel(pParse);
dan2832ad42009-08-31 15:27:27 +00001241 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +00001242
dan1da40a32009-09-19 17:00:31 +00001243 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dan165921a2009-08-28 18:53:45 +00001244
1245 /* It may be that this trigger has already been coded (or is in the
1246 ** process of being coded). If this is the case, then an entry with
dan2832ad42009-08-31 15:27:27 +00001247 ** a matching TriggerPrg.pTrigger field will be present somewhere
1248 ** in the Parse.pTriggerPrg list. Search for such an entry. */
dan2832ad42009-08-31 15:27:27 +00001249 for(pPrg=pRoot->pTriggerPrg;
1250 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
1251 pPrg=pPrg->pNext
dan165921a2009-08-28 18:53:45 +00001252 );
1253
dan65a7cd12009-09-01 12:16:01 +00001254 /* If an existing TriggerPrg could not be located, create a new one. */
dan2832ad42009-08-31 15:27:27 +00001255 if( !pPrg ){
dan65a7cd12009-09-01 12:16:01 +00001256 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
drhe1c47432022-02-07 18:52:56 +00001257 pParse->db->errByteOffset = -1;
dan165921a2009-08-28 18:53:45 +00001258 }
1259
dan2832ad42009-08-31 15:27:27 +00001260 return pPrg;
dan165921a2009-08-28 18:53:45 +00001261}
1262
dan94d7f502009-09-24 09:05:49 +00001263/*
1264** Generate code for the trigger program associated with trigger p on
1265** table pTab. The reg, orconf and ignoreJump parameters passed to this
1266** function are the same as those described in the header function for
1267** sqlite3CodeRowTrigger()
1268*/
dan1da40a32009-09-19 17:00:31 +00001269void sqlite3CodeRowTriggerDirect(
1270 Parse *pParse, /* Parse context */
1271 Trigger *p, /* Trigger to code */
1272 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001273 int reg, /* Reg array containing OLD.* and NEW.* values */
dan1da40a32009-09-19 17:00:31 +00001274 int orconf, /* ON CONFLICT policy */
1275 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
1276){
1277 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
1278 TriggerPrg *pPrg;
1279 pPrg = getRowTrigger(pParse, p, pTab, orconf);
drh0c7d3d32022-01-24 16:47:12 +00001280 assert( pPrg || pParse->nErr );
dan1da40a32009-09-19 17:00:31 +00001281
1282 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
1283 ** is a pointer to the sub-vdbe containing the trigger program. */
1284 if( pPrg ){
dand19c9332010-07-26 12:05:17 +00001285 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
1286
drh9b34abe2016-01-16 15:12:35 +00001287 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
1288 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
dan1da40a32009-09-19 17:00:31 +00001289 VdbeComment(
1290 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
1291
1292 /* Set the P5 operand of the OP_Program instruction to non-zero if
1293 ** recursive invocation of this trigger program is disallowed. Recursive
1294 ** invocation is disallowed if (a) the sub-program is really a trigger,
1295 ** not a foreign key action, and (b) the flag to enable recursive triggers
1296 ** is clear. */
dand19c9332010-07-26 12:05:17 +00001297 sqlite3VdbeChangeP5(v, (u8)bRecursive);
dan1da40a32009-09-19 17:00:31 +00001298 }
1299}
1300
danielk1977633ed082002-05-17 00:05:58 +00001301/*
dan94d7f502009-09-24 09:05:49 +00001302** This is called to code the required FOR EACH ROW triggers for an operation
1303** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
drhf7b54962013-05-28 12:11:54 +00001304** is given by the op parameter. The tr_tm parameter determines whether the
dan94d7f502009-09-24 09:05:49 +00001305** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
1306** parameter pChanges is passed the list of columns being modified.
danielk1977633ed082002-05-17 00:05:58 +00001307**
dan94d7f502009-09-24 09:05:49 +00001308** If there are no triggers that fire at the specified time for the specified
1309** operation on pTab, this function is a no-op.
drhc977f7f2002-05-21 11:38:11 +00001310**
dan94d7f502009-09-24 09:05:49 +00001311** The reg argument is the address of the first in an array of registers
1312** that contain the values substituted for the new.* and old.* references
1313** in the trigger program. If N is the number of columns in table pTab
1314** (a copy of pTab->nCol), then registers are populated as follows:
drhc977f7f2002-05-21 11:38:11 +00001315**
dan94d7f502009-09-24 09:05:49 +00001316** Register Contains
1317** ------------------------------------------------------
1318** reg+0 OLD.rowid
1319** reg+1 OLD.* value of left-most column of pTab
1320** ... ...
1321** reg+N OLD.* value of right-most column of pTab
1322** reg+N+1 NEW.rowid
drh381bdac2021-02-04 17:29:04 +00001323** reg+N+2 NEW.* value of left-most column of pTab
dan94d7f502009-09-24 09:05:49 +00001324** ... ...
1325** reg+N+N+1 NEW.* value of right-most column of pTab
drhc977f7f2002-05-21 11:38:11 +00001326**
dan94d7f502009-09-24 09:05:49 +00001327** For ON DELETE triggers, the registers containing the NEW.* values will
1328** never be accessed by the trigger program, so they are not allocated or
1329** populated by the caller (there is no data to populate them with anyway).
1330** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
1331** are never accessed, and so are not allocated by the caller. So, for an
1332** ON INSERT trigger, the value passed to this function as parameter reg
1333** is not a readable register, although registers (reg+N) through
1334** (reg+N+N+1) are.
danielk1977633ed082002-05-17 00:05:58 +00001335**
dan94d7f502009-09-24 09:05:49 +00001336** Parameter orconf is the default conflict resolution algorithm for the
1337** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
1338** is the instruction that control should jump to if a trigger program
1339** raises an IGNORE exception.
danielk1977633ed082002-05-17 00:05:58 +00001340*/
dan165921a2009-08-28 18:53:45 +00001341void sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +00001342 Parse *pParse, /* Parse context */
danielk19772f886d12009-02-28 10:47:41 +00001343 Trigger *pTrigger, /* List of triggers on table pTab */
danielk1977633ed082002-05-17 00:05:58 +00001344 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
1345 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +00001346 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +00001347 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001348 int reg, /* The first in an array of registers (see above) */
danielk19776f349032002-06-11 02:25:40 +00001349 int orconf, /* ON CONFLICT policy */
dan165921a2009-08-28 18:53:45 +00001350 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
drhc977f7f2002-05-21 11:38:11 +00001351){
dan94d7f502009-09-24 09:05:49 +00001352 Trigger *p; /* Used to iterate through pTrigger list */
danielk19778f2c54e2008-01-01 19:02:09 +00001353
dan94d7f502009-09-24 09:05:49 +00001354 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
1355 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
1356 assert( (op==TK_UPDATE)==(pChanges!=0) );
danielk1977c3f9bad2002-05-15 08:30:12 +00001357
danielk19772f886d12009-02-28 10:47:41 +00001358 for(p=pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +00001359
drh9ab4c2e2009-05-09 00:18:38 +00001360 /* Sanity checking: The schema for the trigger and for the table are
1361 ** always defined. The trigger must be in the same schema as the table
1362 ** or else it must be a TEMP trigger. */
1363 assert( p->pSchema!=0 );
1364 assert( p->pTabSchema!=0 );
dan165921a2009-08-28 18:53:45 +00001365 assert( p->pSchema==p->pTabSchema
1366 || p->pSchema==pParse->db->aDb[1].pSchema );
drh9ab4c2e2009-05-09 00:18:38 +00001367
drh28828c52021-01-30 21:55:38 +00001368 /* Determine whether we should code this trigger. One of two choices:
1369 ** 1. The trigger is an exact match to the current DML statement
1370 ** 2. This is a RETURNING trigger for INSERT but we are currently
1371 ** doing the UPDATE part of an UPSERT.
1372 */
1373 if( (p->op==op || (p->bReturning && p->op==TK_INSERT && op==TK_UPDATE))
dan165921a2009-08-28 18:53:45 +00001374 && p->tr_tm==tr_tm
dan94d7f502009-09-24 09:05:49 +00001375 && checkColumnOverlap(p->pColumns, pChanges)
danielk1977eecfb3e2006-01-10 12:31:39 +00001376 ){
drh381bdac2021-02-04 17:29:04 +00001377 if( !p->bReturning ){
1378 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
1379 }else if( sqlite3IsToplevel(pParse) ){
1380 codeReturningTrigger(pParse, p, pTab, reg);
1381 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001382 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001383 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001384}
dan165921a2009-08-28 18:53:45 +00001385
dan2832ad42009-08-31 15:27:27 +00001386/*
danbb5f1682009-11-27 12:12:34 +00001387** Triggers may access values stored in the old.* or new.* pseudo-table.
1388** This function returns a 32-bit bitmask indicating which columns of the
1389** old.* or new.* tables actually are used by triggers. This information
1390** may be used by the caller, for example, to avoid having to load the entire
1391** old.* record into memory when executing an UPDATE or DELETE command.
dan2832ad42009-08-31 15:27:27 +00001392**
1393** Bit 0 of the returned mask is set if the left-most column of the
danbb5f1682009-11-27 12:12:34 +00001394** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
dan2832ad42009-08-31 15:27:27 +00001395** the second leftmost column value is required, and so on. If there
1396** are more than 32 columns in the table, and at least one of the columns
1397** with an index greater than 32 may be accessed, 0xffffffff is returned.
1398**
danbb5f1682009-11-27 12:12:34 +00001399** It is not possible to determine if the old.rowid or new.rowid column is
1400** accessed by triggers. The caller must always assume that it is.
dan2832ad42009-08-31 15:27:27 +00001401**
danbb5f1682009-11-27 12:12:34 +00001402** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
1403** applies to the old.* table. If 1, the new.* table.
1404**
1405** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
1406** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
1407** included in the returned mask if the TRIGGER_BEFORE bit is set in the
1408** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
1409** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
dan2832ad42009-08-31 15:27:27 +00001410*/
danbb5f1682009-11-27 12:12:34 +00001411u32 sqlite3TriggerColmask(
dan165921a2009-08-28 18:53:45 +00001412 Parse *pParse, /* Parse context */
1413 Trigger *pTrigger, /* List of triggers on table pTab */
dan165921a2009-08-28 18:53:45 +00001414 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
danbb5f1682009-11-27 12:12:34 +00001415 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
1416 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
dan165921a2009-08-28 18:53:45 +00001417 Table *pTab, /* The table to code triggers from */
dan2832ad42009-08-31 15:27:27 +00001418 int orconf /* Default ON CONFLICT policy for trigger steps */
dan165921a2009-08-28 18:53:45 +00001419){
dan1da40a32009-09-19 17:00:31 +00001420 const int op = pChanges ? TK_UPDATE : TK_DELETE;
dan2832ad42009-08-31 15:27:27 +00001421 u32 mask = 0;
dan165921a2009-08-28 18:53:45 +00001422 Trigger *p;
dan165921a2009-08-28 18:53:45 +00001423
danbb5f1682009-11-27 12:12:34 +00001424 assert( isNew==1 || isNew==0 );
dan165921a2009-08-28 18:53:45 +00001425 for(p=pTrigger; p; p=p->pNext){
drh381bdac2021-02-04 17:29:04 +00001426 if( p->op==op
1427 && (tr_tm&p->tr_tm)
danbb5f1682009-11-27 12:12:34 +00001428 && checkColumnOverlap(p->pColumns,pChanges)
1429 ){
drh381bdac2021-02-04 17:29:04 +00001430 if( p->bReturning ){
1431 mask = 0xffffffff;
1432 }else{
1433 TriggerPrg *pPrg;
1434 pPrg = getRowTrigger(pParse, p, pTab, orconf);
1435 if( pPrg ){
1436 mask |= pPrg->aColmask[isNew];
1437 }
dan165921a2009-08-28 18:53:45 +00001438 }
1439 }
1440 }
dan2832ad42009-08-31 15:27:27 +00001441
1442 return mask;
dan165921a2009-08-28 18:53:45 +00001443}
1444
drhb7f91642004-10-31 02:22:47 +00001445#endif /* !defined(SQLITE_OMIT_TRIGGER) */