Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: 779da5e5fbd0a2b5e514d7637229725eab67d427 [file] [log] [blame]
danielk1977c3f9bad2002-05-15 08:30:121/*
danielk1977633ed082002-05-17 00:05:582**
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:5211** This file contains the implementation for TRIGGERs
drh9adf9ac2002-05-15 11:44:1312*/
danielk1977c3f9bad2002-05-15 08:30:1213#include "sqliteInt.h"
drh9adf9ac2002-05-15 11:44:1314
drhb7f91642004-10-31 02:22:4715#ifndef SQLITE_OMIT_TRIGGER
danielk1977c3f9bad2002-05-15 08:30:1216/*
drh4b59ab52002-08-24 18:24:5117** Delete a linked list of TriggerStep structures.
18*/
drh633e6d52008-07-28 19:34:5319void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
drh4b59ab52002-08-24 18:24:5120 while( pTriggerStep ){
21 TriggerStep * pTmp = pTriggerStep;
22 pTriggerStep = pTriggerStep->pNext;
23
drh633e6d52008-07-28 19:34:5324 sqlite3ExprDelete(db, pTmp->pWhere);
25 sqlite3ExprListDelete(db, pTmp->pExprList);
26 sqlite3SelectDelete(db, pTmp->pSelect);
27 sqlite3IdListDelete(db, pTmp->pIdList);
drh46d2e5c2018-04-12 13:15:4328 sqlite3UpsertDelete(db, pTmp->pUpsert);
dane7877b22020-07-14 19:51:0129 sqlite3SrcListDelete(db, pTmp->pFrom);
drhf259df52017-12-27 20:38:3530 sqlite3DbFree(db, pTmp->zSpan);
drh4b59ab52002-08-24 18:24:5131
drh633e6d52008-07-28 19:34:5332 sqlite3DbFree(db, pTmp);
drh4b59ab52002-08-24 18:24:5133 }
34}
35
36/*
danielk19772f886d12009-02-28 10:47:4137** 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:3839**
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:4149*/
50Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
drhf54a80f2021-01-29 13:47:3651 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:4154
drhce5dd9e2022-04-07 20:45:3855 assert( pParse->disableTriggers==0 );
drhf54a80f2021-01-29 13:47:3656 pTmpSchema = pParse->db->aDb[1].pSchema;
57 p = sqliteHashFirst(&pTmpSchema->trigHash);
drh3765c032021-04-27 17:18:1058 pList = pTab->pTrigger;
drha4767682021-04-27 13:04:1859 while( p ){
60 Trigger *pTrig = (Trigger *)sqliteHashData(p);
61 if( pTrig->pTabSchema==pTab->pSchema
62 && pTrig->table
63 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
danabbfa7a2022-11-28 13:47:2764 && (pTrig->pTabSchema!=pTmpSchema || pTrig->bReturning)
drha4767682021-04-27 13:04:1865 ){
66 pTrig->pNext = pList;
67 pList = pTrig;
drh389e0562022-03-17 23:49:5868 }else if( pTrig->op==TK_RETURNING ){
drha4767682021-04-27 13:04:1869#ifndef SQLITE_OMIT_VIRTUALTABLE
drh389e0562022-03-17 23:49:5870 assert( pParse->db->pVtabCtx==0 );
drha4767682021-04-27 13:04:1871#endif
drha4767682021-04-27 13:04:1872 assert( pParse->bReturning );
drh7fd936e2025-02-07 15:49:2173 assert( !pParse->isCreate );
74 assert( &(pParse->u1.d.pReturning->retTrig) == pTrig );
drha4767682021-04-27 13:04:1875 pTrig->table = pTab->zName;
76 pTrig->pTabSchema = pTab->pSchema;
77 pTrig->pNext = pList;
78 pList = pTrig;
79 }
80 p = sqliteHashNext(p);
81 }
82#if 0
83 if( pList ){
84 Trigger *pX;
85 printf("Triggers for %s:", pTab->zName);
86 for(pX=pList; pX; pX=pX->pNext){
87 printf(" %s", pX->zName);
88 }
89 printf("\n");
90 fflush(stdout);
91 }
92#endif
drhf54a80f2021-01-29 13:47:3693 return pList;
danielk19772f886d12009-02-28 10:47:4194}
95
96/*
drhf0f258b2003-04-21 18:48:4597** This is called by the parser when it sees a CREATE TRIGGER statement
98** up to the point of the BEGIN before the trigger actions. A Trigger
99** structure is generated based on the information available and stored
100** in pParse->pNewTrigger. After the trigger actions have been parsed, the
danielk19774adee202004-05-08 08:23:19101** sqlite3FinishTrigger() function is called to complete the trigger
drhf0f258b2003-04-21 18:48:45102** construction process.
drh9adf9ac2002-05-15 11:44:13103*/
danielk19774adee202004-05-08 08:23:19104void sqlite3BeginTrigger(
drh9adf9ac2002-05-15 11:44:13105 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
danielk1977ef2cb632004-05-29 02:37:19106 Token *pName1, /* The name of the trigger */
107 Token *pName2, /* The name of the trigger */
drh5cf590c2003-04-24 01:45:04108 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
drh9adf9ac2002-05-15 11:44:13109 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
danielk1977633ed082002-05-17 00:05:58110 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
drhd24cc422003-03-27 12:51:24111 SrcList *pTableName,/* The name of the table/view the trigger applies to */
drh9adf9ac2002-05-15 11:44:13112 Expr *pWhen, /* WHEN clause */
drhfdd48a72006-09-11 23:45:48113 int isTemp, /* True if the TEMPORARY keyword is present */
114 int noErr /* Suppress errors if the trigger already exists */
drh9adf9ac2002-05-15 11:44:13115){
drh3d5f74b2009-08-06 17:43:31116 Trigger *pTrigger = 0; /* The new trigger */
117 Table *pTab; /* Table that the trigger fires off of */
drhf0f258b2003-04-21 18:48:45118 char *zName = 0; /* Name of the trigger */
drh3d5f74b2009-08-06 17:43:31119 sqlite3 *db = pParse->db; /* The database connection */
danielk1977ef2cb632004-05-29 02:37:19120 int iDb; /* The database to store the trigger in */
121 Token *pName; /* The unqualified db name */
drh3d5f74b2009-08-06 17:43:31122 DbFixer sFix; /* State vector for the DB fixer */
drhed6c8672003-01-12 18:02:16123
drh43617e92006-03-06 20:55:46124 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
125 assert( pName2!=0 );
drhaa78bec2008-12-09 03:55:14126 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
127 assert( op>0 && op<0xff );
danielk1977ef2cb632004-05-29 02:37:19128 if( isTemp ){
129 /* If TEMP was specified, then the trigger name may not be qualified. */
drh43617e92006-03-06 20:55:46130 if( pName2->n>0 ){
danielk1977ef2cb632004-05-29 02:37:19131 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
132 goto trigger_cleanup;
133 }
134 iDb = 1;
135 pName = pName1;
136 }else{
mistachkind5578432012-08-25 10:01:29137 /* Figure out the db that the trigger will be created in */
danielk1977ef2cb632004-05-29 02:37:19138 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
139 if( iDb<0 ){
140 goto trigger_cleanup;
141 }
142 }
drh6fea83e2011-07-01 13:50:44143 if( !pTableName || db->mallocFailed ){
144 goto trigger_cleanup;
145 }
146
147 /* A long-standing parser bug is that this syntax was allowed:
148 **
149 ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
150 ** ^^^^^^^^
151 **
152 ** To maintain backwards compatibility, ignore the database
drh346a70c2020-06-15 20:27:35153 ** name on pTableName if we are reparsing out of the schema table
drh6fea83e2011-07-01 13:50:44154 */
drh692c1602024-08-20 19:09:59155 if( db->init.busy && iDb!=1 ){
156 assert( pTableName->a[0].fg.fixedSchema==0 );
157 assert( pTableName->a[0].fg.isSubquery==0 );
drh8797bd62024-08-17 19:46:49158 sqlite3DbFree(db, pTableName->a[0].u4.zDatabase);
159 pTableName->a[0].u4.zDatabase = 0;
drh6fea83e2011-07-01 13:50:44160 }
danielk1977ef2cb632004-05-29 02:37:19161
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:13166 */
danielk1977ef2cb632004-05-29 02:37:19167 pTab = sqlite3SrcListLookup(pParse, pTableName);
drh622d2882010-02-15 16:54:55168 if( db->init.busy==0 && pName2->n==0 && pTab
169 && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977ef2cb632004-05-29 02:37:19170 iDb = 1;
171 }
172
173 /* Ensure the table name matches database name and that the table exists */
drh17435752007-08-16 04:30:38174 if( db->mallocFailed ) goto trigger_cleanup;
drhd24cc422003-03-27 12:51:24175 assert( pTableName->nSrc==1 );
drhd100f692013-10-03 15:39:44176 sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
177 if( sqlite3FixSrcList(&sFix, pTableName) ){
drh4312db52003-06-03 01:47:11178 goto trigger_cleanup;
drhf26e09c2003-05-31 16:21:12179 }
danielk1977ef2cb632004-05-29 02:37:19180 pTab = sqlite3SrcListLookup(pParse, pTableName);
181 if( !pTab ){
182 /* The table does not exist. */
drh4e451aa2020-11-05 19:13:44183 goto trigger_orphan_error;
drhd24cc422003-03-27 12:51:24184 }
drh4cbdda92006-06-14 19:00:20185 if( IsVirtual(pTab) ){
186 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
drh4e451aa2020-11-05 19:13:44187 goto trigger_orphan_error;
drh4cbdda92006-06-14 19:00:20188 }
drh6f12e512023-10-13 18:29:18189 if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){
190 sqlite3ErrorMsg(pParse, "cannot create triggers on shadow tables");
191 goto trigger_orphan_error;
192 }
drhd24cc422003-03-27 12:51:24193
danielk1977d8123362004-06-12 09:25:12194 /* Check that the trigger name is not reserved and that no trigger of the
195 ** specified name exists */
drh17435752007-08-16 04:30:38196 zName = sqlite3NameFromToken(db, pName);
drhc5a93d42019-08-12 00:08:07197 if( zName==0 ){
198 assert( db->mallocFailed );
199 goto trigger_cleanup;
200 }
201 if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
danielk1977d8123362004-06-12 09:25:12202 goto trigger_cleanup;
203 }
drh21206082011-04-04 18:22:02204 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danc9461ec2018-08-29 21:00:16205 if( !IN_RENAME_OBJECT ){
dan5496d6a2018-08-13 17:14:26206 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
207 if( !noErr ){
208 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
209 }else{
210 assert( !db->init.busy );
211 sqlite3CodeVerifySchema(pParse, iDb);
drha84ead12023-03-17 00:01:32212 VVA_ONLY( pParse->ifNotExists = 1; )
dan5496d6a2018-08-13 17:14:26213 }
214 goto trigger_cleanup;
drhfdd48a72006-09-11 23:45:48215 }
danielk1977c3f9bad2002-05-15 08:30:12216 }
danielk1977ef2cb632004-05-29 02:37:19217
218 /* Do not create a trigger on a system table */
drhdca76842004-12-07 14:06:13219 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk19774adee202004-05-08 08:23:19220 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
drhd24cc422003-03-27 12:51:24221 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40222 }
danielk1977ef2cb632004-05-29 02:37:19223
224 /* INSTEAD of triggers are only for views and views only support INSTEAD
225 ** of triggers.
226 */
drhf38524d2021-08-02 16:41:57227 if( IsView(pTab) && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19228 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drha9799932021-03-19 13:00:28229 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName->a);
drh4e451aa2020-11-05 19:13:44230 goto trigger_orphan_error;
drhd24cc422003-03-27 12:51:24231 }
drhf38524d2021-08-02 16:41:57232 if( !IsView(pTab) && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19233 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drha9799932021-03-19 13:00:28234 " trigger on table: %S", pTableName->a);
drh4e451aa2020-11-05 19:13:44235 goto trigger_orphan_error;
drhd24cc422003-03-27 12:51:24236 }
danielk1977ef2cb632004-05-29 02:37:19237
drhd24cc422003-03-27 12:51:24238#ifndef SQLITE_OMIT_AUTHORIZATION
danc9461ec2018-08-29 21:00:16239 if( !IN_RENAME_OBJECT ){
drha0daa752016-09-16 11:53:10240 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drhd24cc422003-03-27 12:51:24241 int code = SQLITE_CREATE_TRIGGER;
drh69c33822016-08-18 14:33:11242 const char *zDb = db->aDb[iTabDb].zDbSName;
243 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
danielk1977da184232006-01-05 11:34:32244 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19245 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24246 goto trigger_cleanup;
247 }
danielk1977da184232006-01-05 11:34:32248 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24249 goto trigger_cleanup;
250 }
251 }
252#endif
danielk1977d702fcc2002-05-26 23:24:40253
danielk1977ef2cb632004-05-29 02:37:19254 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04255 ** cannot appear on views. So we might as well translate every
256 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
257 ** elsewhere.
258 */
danielk1977d702fcc2002-05-26 23:24:40259 if (tr_tm == TK_INSTEAD){
260 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12261 }
262
263 /* Build the Trigger object */
drh17435752007-08-16 04:30:38264 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19265 if( pTrigger==0 ) goto trigger_cleanup;
dan165921a2009-08-28 18:53:45266 pTrigger->zName = zName;
drhe5f9c642003-01-13 23:27:31267 zName = 0;
drh17435752007-08-16 04:30:38268 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32269 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48270 pTrigger->pTabSchema = pTab->pSchema;
drhaa78bec2008-12-09 03:55:14271 pTrigger->op = (u8)op;
drhdca76842004-12-07 14:06:13272 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
danc9461ec2018-08-29 21:00:16273 if( IN_RENAME_OBJECT ){
274 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
dan5496d6a2018-08-13 17:14:26275 pTrigger->pWhen = pWhen;
276 pWhen = 0;
277 }else{
278 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
279 }
280 pTrigger->pColumns = pColumns;
281 pColumns = 0;
drhf0f258b2003-04-21 18:48:45282 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19283 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45284
285trigger_cleanup:
drh633e6d52008-07-28 19:34:53286 sqlite3DbFree(db, zName);
287 sqlite3SrcListDelete(db, pTableName);
288 sqlite3IdListDelete(db, pColumns);
289 sqlite3ExprDelete(db, pWhen);
danielk1977d5d56522005-03-16 12:15:20290 if( !pParse->pNewTrigger ){
drh633e6d52008-07-28 19:34:53291 sqlite3DeleteTrigger(db, pTrigger);
danielk1977d5d56522005-03-16 12:15:20292 }else{
293 assert( pParse->pNewTrigger==pTrigger );
294 }
drh4e451aa2020-11-05 19:13:44295 return;
296
297trigger_orphan_error:
298 if( db->init.iDb==1 ){
299 /* Ticket #3810.
300 ** Normally, whenever a table is dropped, all associated triggers are
301 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
302 ** and the table is dropped by a different database connection, the
303 ** trigger is not visible to the database connection that does the
304 ** drop so the trigger cannot be dropped. This results in an
305 ** "orphaned trigger" - a trigger whose associated table is missing.
306 **
307 ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df
308 */
309 db->init.orphanTrigger = 1;
310 }
311 goto trigger_cleanup;
drhf0f258b2003-04-21 18:48:45312}
313
314/*
315** This routine is called after all of the trigger actions have been parsed
316** in order to complete the process of building the trigger.
317*/
danielk19774adee202004-05-08 08:23:19318void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45319 Parse *pParse, /* Parser context */
320 TriggerStep *pStepList, /* The triggered program */
321 Token *pAll /* Token that describes the complete CREATE TRIGGER */
322){
drha8c62df2010-02-15 15:47:18323 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
324 char *zName; /* Name of trigger */
325 sqlite3 *db = pParse->db; /* The database */
326 DbFixer sFix; /* Fixer object */
327 int iDb; /* Database containing the trigger */
328 Token nameToken; /* Trigger name for error reporting */
drhf0f258b2003-04-21 18:48:45329
drhf0f258b2003-04-21 18:48:45330 pParse->pNewTrigger = 0;
drh9ab4c2e2009-05-09 00:18:38331 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
dan165921a2009-08-28 18:53:45332 zName = pTrig->zName;
danielk1977da184232006-01-05 11:34:32333 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32334 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53335 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32336 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53337 pStepList = pStepList->pNext;
338 }
drh40aced52016-01-22 17:48:09339 sqlite3TokenInit(&nameToken, pTrig->zName);
drhd100f692013-10-03 15:39:44340 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
341 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
dan46539d72013-10-03 12:29:38342 || sqlite3FixExpr(&sFix, pTrig->pWhen)
drhd100f692013-10-03 15:39:44343 ){
drhf26e09c2003-05-31 16:21:12344 goto triggerfinish_cleanup;
345 }
danielk1977c3f9bad2002-05-15 08:30:12346
dan5496d6a2018-08-13 17:14:26347#ifndef SQLITE_OMIT_ALTERTABLE
danc9461ec2018-08-29 21:00:16348 if( IN_RENAME_OBJECT ){
dan5496d6a2018-08-13 17:14:26349 assert( !db->init.busy );
350 pParse->pNewTrigger = pTrig;
351 pTrig = 0;
352 }else
353#endif
354
drha8c62df2010-02-15 15:47:18355 /* if we are not initializing,
drh1e32bed2020-06-19 13:33:53356 ** build the sqlite_schema entry
drh9adf9ac2002-05-15 11:44:13357 */
drh1d85d932004-02-14 23:05:52358 if( !db->init.busy ){
drhc977f7f2002-05-21 11:38:11359 Vdbe *v;
drh2d401ab2008-01-10 23:50:11360 char *z;
danielk1977c3f9bad2002-05-15 08:30:12361
drh80cf8912022-09-27 00:56:45362 /* If this is a new CREATE TABLE statement, and if shadow tables
363 ** are read-only, and the trigger makes a change to a shadow table,
364 ** then raise an error - do not allow the trigger to be created. */
365 if( sqlite3ReadOnlyShadowTables(db) ){
366 TriggerStep *pStep;
367 for(pStep=pTrig->step_list; pStep; pStep=pStep->pNext){
368 if( pStep->zTarget!=0
369 && sqlite3ShadowTableName(db, pStep->zTarget)
370 ){
371 sqlite3ErrorMsg(pParse,
372 "trigger \"%s\" may not write to shadow table \"%s\"",
373 pTrig->zName, pStep->zTarget);
374 goto triggerfinish_cleanup;
375 }
376 }
377 }
378
drh1e32bed2020-06-19 13:33:53379 /* Make an entry in the sqlite_schema table */
danielk19774adee202004-05-08 08:23:19380 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45381 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32382 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11383 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
drhd96cc6f2017-06-08 14:35:21384 testcase( z==0 );
drh2d401ab2008-01-10 23:50:11385 sqlite3NestedParse(pParse,
drha4a871c2021-11-04 14:04:20386 "INSERT INTO %Q." LEGACY_SCHEMA_TABLE
drh346a70c2020-06-15 20:27:35387 " VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
388 db->aDb[iDb].zDbSName, zName,
drh2d401ab2008-01-10 23:50:11389 pTrig->table, z);
drh633e6d52008-07-28 19:34:53390 sqlite3DbFree(db, z);
drh9cbf3422008-01-17 16:22:13391 sqlite3ChangeCookie(pParse, iDb);
drh5d9c9da2011-06-03 20:11:17392 sqlite3VdbeAddParseSchemaOp(v, iDb,
dan6a5a13d2021-02-17 20:08:22393 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName), 0);
danielk1977c3f9bad2002-05-15 08:30:12394 }
395
drh956bc922004-07-24 17:38:29396 if( db->init.busy ){
danielk19772f886d12009-02-28 10:47:41397 Trigger *pLink = pTrig;
398 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
drh21206082011-04-04 18:22:02399 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh55f66b32019-07-16 19:44:32400 assert( pLink!=0 );
drhacbcb7e2014-08-21 20:26:37401 pTrig = sqlite3HashInsert(pHash, zName, pTrig);
danielk19772f886d12009-02-28 10:47:41402 if( pTrig ){
drh4a642b62016-02-05 01:55:27403 sqlite3OomFault(db);
danielk19772f886d12009-02-28 10:47:41404 }else if( pLink->pSchema==pLink->pTabSchema ){
405 Table *pTab;
drhacbcb7e2014-08-21 20:26:37406 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
danielk19772f886d12009-02-28 10:47:41407 assert( pTab!=0 );
408 pLink->pNext = pTab->pTrigger;
409 pTab->pTrigger = pLink;
danielk1977d5d56522005-03-16 12:15:20410 }
danielk1977c3f9bad2002-05-15 08:30:12411 }
412
drhf0f258b2003-04-21 18:48:45413triggerfinish_cleanup:
drh633e6d52008-07-28 19:34:53414 sqlite3DeleteTrigger(db, pTrig);
danc9461ec2018-08-29 21:00:16415 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
drh633e6d52008-07-28 19:34:53416 sqlite3DeleteTriggerStep(db, pStepList);
drh4b59ab52002-08-24 18:24:51417}
danielk1977c3f9bad2002-05-15 08:30:12418
drh4b59ab52002-08-24 18:24:51419/*
drhf259df52017-12-27 20:38:35420** Duplicate a range of text from an SQL statement, then convert all
421** whitespace characters into ordinary space characters.
422*/
423static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
424 char *z = sqlite3DbSpanDup(db, zStart, zEnd);
425 int i;
426 if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i]) ) z[i] = ' ';
427 return z;
428}
429
430/*
drhc977f7f2002-05-21 11:38:11431** Turn a SELECT statement (that the pSelect parameter points to) into
432** a trigger step. Return a pointer to a TriggerStep structure.
433**
434** The parser calls this routine when it finds a SELECT statement in
435** body of a TRIGGER.
436*/
drhf259df52017-12-27 20:38:35437TriggerStep *sqlite3TriggerSelectStep(
438 sqlite3 *db, /* Database connection */
439 Select *pSelect, /* The SELECT statement */
440 const char *zStart, /* Start of SQL text */
441 const char *zEnd /* End of SQL text */
442){
drh17435752007-08-16 04:30:38443 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08444 if( pTriggerStep==0 ) {
drh633e6d52008-07-28 19:34:53445 sqlite3SelectDelete(db, pSelect);
danielk19772e588c72005-12-09 14:25:08446 return 0;
447 }
danielk1977633ed082002-05-17 00:05:58448 pTriggerStep->op = TK_SELECT;
449 pTriggerStep->pSelect = pSelect;
450 pTriggerStep->orconf = OE_Default;
drhf259df52017-12-27 20:38:35451 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
drhb7916a72009-05-27 10:31:29452 return pTriggerStep;
453}
danielk1977c3f9bad2002-05-15 08:30:12454
drhb7916a72009-05-27 10:31:29455/*
456** Allocate space to hold a new trigger step. The allocated space
457** holds both the TriggerStep object and the TriggerStep.target.z string.
458**
459** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
460*/
461static TriggerStep *triggerStepAllocate(
danc9461ec2018-08-29 21:00:16462 Parse *pParse, /* Parser context */
shane5eff7cf2009-08-10 03:57:58463 u8 op, /* Trigger opcode */
drhf259df52017-12-27 20:38:35464 Token *pName, /* The target name */
465 const char *zStart, /* Start of SQL text */
466 const char *zEnd /* End of SQL text */
drhb7916a72009-05-27 10:31:29467){
danc9461ec2018-08-29 21:00:16468 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29469 TriggerStep *pTriggerStep;
470
drh4c460bb2022-03-07 16:22:31471 if( pParse->nErr ) return 0;
dan46408352015-04-21 16:38:49472 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
drhb7916a72009-05-27 10:31:29473 if( pTriggerStep ){
474 char *z = (char*)&pTriggerStep[1];
475 memcpy(z, pName->z, pName->n);
dan46408352015-04-21 16:38:49476 sqlite3Dequote(z);
477 pTriggerStep->zTarget = z;
drhb7916a72009-05-27 10:31:29478 pTriggerStep->op = op;
drhf259df52017-12-27 20:38:35479 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
danc9461ec2018-08-29 21:00:16480 if( IN_RENAME_OBJECT ){
481 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
482 }
drhb7916a72009-05-27 10:31:29483 }
danielk1977633ed082002-05-17 00:05:58484 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12485}
486
drhc977f7f2002-05-21 11:38:11487/*
488** Build a trigger step out of an INSERT statement. Return a pointer
489** to the new trigger step.
490**
491** The parser calls this routine when it sees an INSERT inside the
492** body of a trigger.
493*/
danielk19774adee202004-05-08 08:23:19494TriggerStep *sqlite3TriggerInsertStep(
dan5be60c52018-08-15 20:28:39495 Parse *pParse, /* Parser */
drhc977f7f2002-05-21 11:38:11496 Token *pTableName, /* Name of the table into which we insert */
497 IdList *pColumn, /* List of columns in pTableName to insert into */
drhc977f7f2002-05-21 11:38:11498 Select *pSelect, /* A SELECT statement that supplies values */
drhf259df52017-12-27 20:38:35499 u8 orconf, /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
drh46d2e5c2018-04-12 13:15:43500 Upsert *pUpsert, /* ON CONFLICT clauses for upsert */
drhf259df52017-12-27 20:38:35501 const char *zStart, /* Start of SQL text */
502 const char *zEnd /* End of SQL text */
danielk1977633ed082002-05-17 00:05:58503){
dan5be60c52018-08-15 20:28:39504 sqlite3 *db = pParse->db;
drhf3a65f72007-08-22 20:18:21505 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12506
drh75593d92014-01-10 20:46:55507 assert(pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12508
danc9461ec2018-08-29 21:00:16509 pTriggerStep = triggerStepAllocate(pParse, TK_INSERT, pTableName,zStart,zEnd);
danielk1977d5d56522005-03-16 12:15:20510 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16511 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39512 pTriggerStep->pSelect = pSelect;
513 pSelect = 0;
514 }else{
515 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
516 }
danielk1977d5d56522005-03-16 12:15:20517 pTriggerStep->pIdList = pColumn;
drh46d2e5c2018-04-12 13:15:43518 pTriggerStep->pUpsert = pUpsert;
danielk1977d5d56522005-03-16 12:15:20519 pTriggerStep->orconf = orconf;
dan9105fd52019-08-19 17:26:32520 if( pUpsert ){
521 sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
522 }
danielk1977d5d56522005-03-16 12:15:20523 }else{
drh99160482018-04-18 01:34:39524 testcase( pColumn );
drh633e6d52008-07-28 19:34:53525 sqlite3IdListDelete(db, pColumn);
drh99160482018-04-18 01:34:39526 testcase( pUpsert );
drh46d2e5c2018-04-12 13:15:43527 sqlite3UpsertDelete(db, pUpsert);
danielk1977d5d56522005-03-16 12:15:20528 }
drh33e619f2009-05-28 01:00:55529 sqlite3SelectDelete(db, pSelect);
danielk1977c3f9bad2002-05-15 08:30:12530
danielk1977633ed082002-05-17 00:05:58531 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12532}
533
drhc977f7f2002-05-21 11:38:11534/*
535** Construct a trigger step that implements an UPDATE statement and return
536** a pointer to that trigger step. The parser calls this routine when it
537** sees an UPDATE statement inside the body of a CREATE TRIGGER.
538*/
danielk19774adee202004-05-08 08:23:19539TriggerStep *sqlite3TriggerUpdateStep(
dan5be60c52018-08-15 20:28:39540 Parse *pParse, /* Parser */
drhc977f7f2002-05-21 11:38:11541 Token *pTableName, /* Name of the table to be updated */
drh26c4df02022-05-26 17:33:42542 SrcList *pFrom, /* FROM clause for an UPDATE-FROM, or NULL */
drhc977f7f2002-05-21 11:38:11543 ExprList *pEList, /* The SET clause: list of column and new values */
544 Expr *pWhere, /* The WHERE clause */
drhf259df52017-12-27 20:38:35545 u8 orconf, /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
546 const char *zStart, /* Start of SQL text */
547 const char *zEnd /* End of SQL text */
drhc977f7f2002-05-21 11:38:11548){
dan5be60c52018-08-15 20:28:39549 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29550 TriggerStep *pTriggerStep;
551
danc9461ec2018-08-29 21:00:16552 pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE, pTableName,zStart,zEnd);
drh33e619f2009-05-28 01:00:55553 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16554 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39555 pTriggerStep->pExprList = pEList;
556 pTriggerStep->pWhere = pWhere;
dane7877b22020-07-14 19:51:01557 pTriggerStep->pFrom = pFrom;
dan5be60c52018-08-15 20:28:39558 pEList = 0;
559 pWhere = 0;
dane7877b22020-07-14 19:51:01560 pFrom = 0;
dan5be60c52018-08-15 20:28:39561 }else{
562 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
563 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
dane7877b22020-07-14 19:51:01564 pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE);
dan5be60c52018-08-15 20:28:39565 }
drh33e619f2009-05-28 01:00:55566 pTriggerStep->orconf = orconf;
drh0e3a6f32007-03-30 20:40:34567 }
drh33e619f2009-05-28 01:00:55568 sqlite3ExprListDelete(db, pEList);
569 sqlite3ExprDelete(db, pWhere);
dane7877b22020-07-14 19:51:01570 sqlite3SrcListDelete(db, pFrom);
danielk1977633ed082002-05-17 00:05:58571 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12572}
573
drhc977f7f2002-05-21 11:38:11574/*
575** Construct a trigger step that implements a DELETE statement and return
576** a pointer to that trigger step. The parser calls this routine when it
577** sees a DELETE statement inside the body of a CREATE TRIGGER.
578*/
drh17435752007-08-16 04:30:38579TriggerStep *sqlite3TriggerDeleteStep(
dan5be60c52018-08-15 20:28:39580 Parse *pParse, /* Parser */
drh17435752007-08-16 04:30:38581 Token *pTableName, /* The table from which rows are deleted */
drhf259df52017-12-27 20:38:35582 Expr *pWhere, /* The WHERE clause */
583 const char *zStart, /* Start of SQL text */
584 const char *zEnd /* End of SQL text */
drh17435752007-08-16 04:30:38585){
dan5be60c52018-08-15 20:28:39586 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29587 TriggerStep *pTriggerStep;
588
danc9461ec2018-08-29 21:00:16589 pTriggerStep = triggerStepAllocate(pParse, TK_DELETE, pTableName,zStart,zEnd);
drh33e619f2009-05-28 01:00:55590 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16591 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39592 pTriggerStep->pWhere = pWhere;
593 pWhere = 0;
594 }else{
595 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
596 }
drh33e619f2009-05-28 01:00:55597 pTriggerStep->orconf = OE_Default;
drhb63a53d2007-03-31 01:34:44598 }
drh33e619f2009-05-28 01:00:55599 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58600 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12601}
602
danielk1977633ed082002-05-17 00:05:58603/*
604** Recursively delete a Trigger structure
605*/
drh633e6d52008-07-28 19:34:53606void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
drh658f0a32021-01-30 02:43:26607 if( pTrigger==0 || pTrigger->bReturning ) return;
drh633e6d52008-07-28 19:34:53608 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
dan165921a2009-08-28 18:53:45609 sqlite3DbFree(db, pTrigger->zName);
drh633e6d52008-07-28 19:34:53610 sqlite3DbFree(db, pTrigger->table);
611 sqlite3ExprDelete(db, pTrigger->pWhen);
612 sqlite3IdListDelete(db, pTrigger->pColumns);
drh633e6d52008-07-28 19:34:53613 sqlite3DbFree(db, pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12614}
615
616/*
danielk19778a414492004-06-29 08:59:35617** This function is called to drop a trigger from the database schema.
618**
619** This may be called directly from the parser and therefore identifies
620** the trigger by name. The sqlite3DropTriggerPtr() routine does the
621** same job as this routine except it takes a pointer to the trigger
622** instead of the trigger name.
623**/
drhfdd48a72006-09-11 23:45:48624void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43625 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24626 int i;
627 const char *zDb;
628 const char *zName;
drh9bb575f2004-09-06 17:24:11629 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12630
drh17435752007-08-16 04:30:38631 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35632 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04633 goto drop_trigger_cleanup;
634 }
danielk19778e227872004-06-07 07:52:17635
drhd24cc422003-03-27 12:51:24636 assert( pName->nSrc==1 );
drh692c1602024-08-20 19:09:59637 assert( pName->a[0].fg.fixedSchema==0 && pName->a[0].fg.isSubquery==0 );
drh8797bd62024-08-17 19:46:49638 zDb = pName->a[0].u4.zDatabase;
drhd24cc422003-03-27 12:51:24639 zName = pName->a[0].zName;
drh21206082011-04-04 18:22:02640 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
danielk197753c0f742005-03-29 03:10:59641 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00642 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
dan465c2b82020-03-21 15:10:40643 if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
drh21206082011-04-04 18:22:02644 assert( sqlite3SchemaMutexHeld(db, j, 0) );
drhacbcb7e2014-08-21 20:26:37645 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
drhd24cc422003-03-27 12:51:24646 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12647 }
drhd24cc422003-03-27 12:51:24648 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48649 if( !noErr ){
drha9799932021-03-19 13:00:28650 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName->a);
dan57966752011-04-09 17:32:58651 }else{
652 sqlite3CodeVerifyNamedSchema(pParse, zDb);
drhfdd48a72006-09-11 23:45:48653 }
dan1db95102010-06-28 10:15:19654 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24655 goto drop_trigger_cleanup;
656 }
drh74161702006-02-24 02:53:49657 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03658
659drop_trigger_cleanup:
drh633e6d52008-07-28 19:34:53660 sqlite3SrcListDelete(db, pName);
drh79a519c2003-05-17 19:04:03661}
662
663/*
drh956bc922004-07-24 17:38:29664** Return a pointer to the Table structure for the table that a trigger
665** is set on.
666*/
drh74161702006-02-24 02:53:49667static Table *tableOfTrigger(Trigger *pTrigger){
drhacbcb7e2014-08-21 20:26:37668 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
drh956bc922004-07-24 17:38:29669}
670
671
672/*
drh74161702006-02-24 02:53:49673** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03674*/
drh74161702006-02-24 02:53:49675void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03676 Table *pTable;
677 Vdbe *v;
drh9bb575f2004-09-06 17:24:11678 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29679 int iDb;
drh79a519c2003-05-17 19:04:03680
danielk1977da184232006-01-05 11:34:32681 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29682 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49683 pTable = tableOfTrigger(pTrigger);
drh6397a782019-08-27 10:05:45684 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
drhe5f9c642003-01-13 23:27:31685#ifndef SQLITE_OMIT_AUTHORIZATION
drh6397a782019-08-27 10:05:45686 if( pTable ){
drhe5f9c642003-01-13 23:27:31687 int code = SQLITE_DROP_TRIGGER;
drh69c33822016-08-18 14:33:11688 const char *zDb = db->aDb[iDb].zDbSName;
drh956bc922004-07-24 17:38:29689 const char *zTab = SCHEMA_TABLE(iDb);
690 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
dan2bd93512009-08-31 08:22:46691 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
danielk19774adee202004-05-08 08:23:19692 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03693 return;
drhe5f9c642003-01-13 23:27:31694 }
drhed6c8672003-01-12 18:02:16695 }
drhe5f9c642003-01-13 23:27:31696#endif
danielk1977c3f9bad2002-05-15 08:30:12697
drhf0f258b2003-04-21 18:48:45698 /* Generate code to destroy the database record of the trigger.
699 */
drh43617e92006-03-06 20:55:46700 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drh8c8dddc2015-12-09 16:26:38701 sqlite3NestedParse(pParse,
drha4a871c2021-11-04 14:04:20702 "DELETE FROM %Q." LEGACY_SCHEMA_TABLE " WHERE name=%Q AND type='trigger'",
drh346a70c2020-06-15 20:27:35703 db->aDb[iDb].zDbSName, pTrigger->zName
drh8c8dddc2015-12-09 16:26:38704 );
drh9cbf3422008-01-17 16:22:13705 sqlite3ChangeCookie(pParse, iDb);
dan165921a2009-08-28 18:53:45706 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
drhf0f258b2003-04-21 18:48:45707 }
drh956bc922004-07-24 17:38:29708}
drhf0f258b2003-04-21 18:48:45709
drh956bc922004-07-24 17:38:29710/*
711** Remove a trigger from the hash tables of the sqlite* pointer.
712*/
713void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
714 Trigger *pTrigger;
drh21206082011-04-04 18:22:02715 Hash *pHash;
716
717 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
718 pHash = &(db->aDb[iDb].pSchema->trigHash);
drhacbcb7e2014-08-21 20:26:37719 pTrigger = sqlite3HashInsert(pHash, zName, 0);
drh9ab4c2e2009-05-09 00:18:38720 if( ALWAYS(pTrigger) ){
danielk19772f886d12009-02-28 10:47:41721 if( pTrigger->pSchema==pTrigger->pTabSchema ){
722 Table *pTab = tableOfTrigger(pTrigger);
drh6397a782019-08-27 10:05:45723 if( pTab ){
724 Trigger **pp;
dan0232dad2019-12-03 03:34:06725 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
726 if( *pp==pTrigger ){
727 *pp = (*pp)->pNext;
728 break;
729 }
730 }
drh6397a782019-08-27 10:05:45731 }
danielk1977c3f9bad2002-05-15 08:30:12732 }
drh633e6d52008-07-28 19:34:53733 sqlite3DeleteTrigger(db, pTrigger);
drh8257aa82017-07-26 19:59:13734 db->mDbFlags |= DBFLAG_SchemaChange;
danielk1977c3f9bad2002-05-15 08:30:12735 }
danielk1977c3f9bad2002-05-15 08:30:12736}
737
drhc977f7f2002-05-21 11:38:11738/*
739** pEList is the SET clause of an UPDATE statement. Each entry
740** in pEList is of the format <id>=<expr>. If any of the entries
741** in pEList have an <id> which matches an identifier in pIdList,
742** then return TRUE. If pIdList==NULL, then it is considered a
743** wildcard that matches anything. Likewise if pEList==NULL then
744** it matches anything so always return true. Return false only
745** if there is no match.
746*/
drh9ab4c2e2009-05-09 00:18:38747static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36748 int e;
drh9ab4c2e2009-05-09 00:18:38749 if( pIdList==0 || NEVER(pEList==0) ) return 1;
drhad2d8302002-05-24 20:31:36750 for(e=0; e<pEList->nExpr; e++){
drh41cee662019-12-12 20:22:34751 if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12752 }
danielk1977c3f9bad2002-05-15 08:30:12753 return 0;
754}
755
danielk1977c3f9bad2002-05-15 08:30:12756/*
drha7441672022-04-07 14:03:07757** Return true if any TEMP triggers exist
758*/
759static int tempTriggersExist(sqlite3 *db){
drhce5dd9e2022-04-07 20:45:38760 if( NEVER(db->aDb[1].pSchema==0) ) return 0;
drha7441672022-04-07 14:03:07761 if( sqliteHashFirst(&db->aDb[1].pSchema->trigHash)==0 ) return 0;
762 return 1;
763}
764
765/*
danielk19772f886d12009-02-28 10:47:41766** Return a list of all triggers on table pTab if there exists at least
767** one trigger that must be fired when an operation of type 'op' is
768** performed on the table, and, if that operation is an UPDATE, if at
769** least one of the columns in pChanges is being modified.
drhdca76842004-12-07 14:06:13770*/
drha7441672022-04-07 14:03:07771static SQLITE_NOINLINE Trigger *triggersReallyExist(
danielk19772f886d12009-02-28 10:47:41772 Parse *pParse, /* Parse context */
drhdca76842004-12-07 14:06:13773 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58774 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
danielk19772f886d12009-02-28 10:47:41775 ExprList *pChanges, /* Columns that change in an UPDATE statement */
776 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
drhc977f7f2002-05-21 11:38:11777){
drhdca76842004-12-07 14:06:13778 int mask = 0;
drhe83cafd2011-03-21 17:15:58779 Trigger *pList = 0;
danielk19772f886d12009-02-28 10:47:41780 Trigger *p;
drhe83cafd2011-03-21 17:15:58781
drh2aa41c82021-02-03 00:55:34782 pList = sqlite3TriggerList(pParse, pTab);
drhc9be8632021-02-02 00:16:15783 assert( pList==0 || IsVirtual(pTab)==0
784 || (pList->bReturning && pList->pNext==0) );
drh2aa41c82021-02-03 00:55:34785 if( pList!=0 ){
786 p = pList;
787 if( (pParse->db->flags & SQLITE_EnableTrigger)==0
788 && pTab->pTrigger!=0
789 ){
790 /* The SQLITE_DBCONFIG_ENABLE_TRIGGER setting is off. That means that
791 ** only TEMP triggers are allowed. Truncate the pList so that it
792 ** includes only TEMP triggers */
793 if( pList==pTab->pTrigger ){
794 pList = 0;
795 goto exit_triggers_exist;
drh709dd132021-02-02 12:01:22796 }
drh2aa41c82021-02-03 00:55:34797 while( ALWAYS(p->pNext) && p->pNext!=pTab->pTrigger ) p = p->pNext;
798 p->pNext = 0;
799 p = pList;
danielk1977c3f9bad2002-05-15 08:30:12800 }
drh2aa41c82021-02-03 00:55:34801 do{
802 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
803 mask |= p->tr_tm;
804 }else if( p->op==TK_RETURNING ){
805 /* The first time a RETURNING trigger is seen, the "op" value tells
806 ** us what time of trigger it should be. */
807 assert( sqlite3IsToplevel(pParse) );
808 p->op = op;
drh7dec8042021-02-04 22:59:19809 if( IsVirtual(pTab) ){
810 if( op!=TK_INSERT ){
811 sqlite3ErrorMsg(pParse,
812 "%s RETURNING is not available on virtual tables",
813 op==TK_DELETE ? "DELETE" : "UPDATE");
814 }
815 p->tr_tm = TRIGGER_BEFORE;
816 }else{
817 p->tr_tm = TRIGGER_AFTER;
drh2aa41c82021-02-03 00:55:34818 }
drh7dec8042021-02-04 22:59:19819 mask |= p->tr_tm;
drh2aa41c82021-02-03 00:55:34820 }else if( p->bReturning && p->op==TK_INSERT && op==TK_UPDATE
821 && sqlite3IsToplevel(pParse) ){
822 /* Also fire a RETURNING trigger for an UPSERT */
drh7dec8042021-02-04 22:59:19823 mask |= p->tr_tm;
drh2aa41c82021-02-03 00:55:34824 }
825 p = p->pNext;
826 }while( p );
danielk1977c3f9bad2002-05-15 08:30:12827 }
drh2aa41c82021-02-03 00:55:34828exit_triggers_exist:
danielk19772f886d12009-02-28 10:47:41829 if( pMask ){
830 *pMask = mask;
831 }
832 return (mask ? pList : 0);
danielk1977c3f9bad2002-05-15 08:30:12833}
drha7441672022-04-07 14:03:07834Trigger *sqlite3TriggersExist(
835 Parse *pParse, /* Parse context */
836 Table *pTab, /* The table the contains the triggers */
837 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
838 ExprList *pChanges, /* Columns that change in an UPDATE statement */
839 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
840){
841 assert( pTab!=0 );
842 if( (pTab->pTrigger==0 && !tempTriggersExist(pParse->db))
843 || pParse->disableTriggers
844 ){
845 if( pMask ) *pMask = 0;
846 return 0;
847 }
848 return triggersReallyExist(pParse,pTab,op,pChanges,pMask);
849}
danielk1977c3f9bad2002-05-15 08:30:12850
drhc977f7f2002-05-21 11:38:11851/*
dan46408352015-04-21 16:38:49852** Convert the pStep->zTarget string into a SrcList and return a pointer
drhf26e09c2003-05-31 16:21:12853** to that SrcList.
854**
855** This routine adds a specific database name, if needed, to the target when
856** forming the SrcList. This prevents a trigger in one database from
857** referring to a target in another database. An exception is when the
858** trigger is in TEMP in which case it can refer to any other database it
859** wants.
860*/
dane7877b22020-07-14 19:51:01861SrcList *sqlite3TriggerStepSrc(
drhf26e09c2003-05-31 16:21:12862 Parse *pParse, /* The parsing context */
863 TriggerStep *pStep /* The trigger containing the target token */
864){
dan46408352015-04-21 16:38:49865 sqlite3 *db = pParse->db;
dane7877b22020-07-14 19:51:01866 SrcList *pSrc; /* SrcList to be returned */
867 char *zName = sqlite3DbStrDup(db, pStep->zTarget);
drh29c992c2019-01-17 15:40:41868 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
dane7877b22020-07-14 19:51:01869 assert( pSrc==0 || pSrc->nSrc==1 );
870 assert( zName || pSrc==0 );
drhb7916a72009-05-27 10:31:29871 if( pSrc ){
dane7877b22020-07-14 19:51:01872 Schema *pSchema = pStep->pTrig->pSchema;
873 pSrc->a[0].zName = zName;
874 if( pSchema!=db->aDb[1].pSchema ){
drh8797bd62024-08-17 19:46:49875 assert( pSrc->a[0].fg.fixedSchema || pSrc->a[0].u4.zDatabase==0 );
876 pSrc->a[0].u4.pSchema = pSchema;
877 pSrc->a[0].fg.fixedSchema = 1;
drhb7916a72009-05-27 10:31:29878 }
dane7877b22020-07-14 19:51:01879 if( pStep->pFrom ){
880 SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0);
dan4209d552022-05-27 15:04:43881 if( pDup && pDup->nSrc>1 && !IN_RENAME_OBJECT ){
drh26c4df02022-05-26 17:33:42882 Select *pSubquery;
883 Token as;
884 pSubquery = sqlite3SelectNew(pParse,0,pDup,0,0,0,0,SF_NestedFrom,0);
885 as.n = 0;
886 as.z = 0;
887 pDup = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0);
888 }
dane7877b22020-07-14 19:51:01889 pSrc = sqlite3SrcListAppendList(pParse, pSrc, pDup);
890 }
891 }else{
892 sqlite3DbFree(db, zName);
drhf26e09c2003-05-31 16:21:12893 }
894 return pSrc;
895}
896
drh0d23f672021-03-30 01:52:21897/*
898** Return true if the pExpr term from the RETURNING clause argument
899** list is of the form "*". Raise an error if the terms if of the
900** form "table.*".
901*/
902static int isAsteriskTerm(
903 Parse *pParse, /* Parsing context */
904 Expr *pTerm /* A term in the RETURNING clause */
905){
906 assert( pTerm!=0 );
907 if( pTerm->op==TK_ASTERISK ) return 1;
908 if( pTerm->op!=TK_DOT ) return 0;
909 assert( pTerm->pRight!=0 );
910 assert( pTerm->pLeft!=0 );
911 if( pTerm->pRight->op!=TK_ASTERISK ) return 0;
912 sqlite3ErrorMsg(pParse, "RETURNING may not use \"TABLE.*\" wildcards");
913 return 1;
914}
915
drhdac9a5f2021-01-29 21:18:46916/* The input list pList is the list of result set terms from a RETURNING
917** clause. The table that we are returning from is pTab.
918**
919** This routine makes a copy of the pList, and at the same time expands
920** any "*" wildcards to be the complete set of columns from pTab.
921*/
922static ExprList *sqlite3ExpandReturning(
923 Parse *pParse, /* Parsing context */
924 ExprList *pList, /* The arguments to RETURNING */
925 Table *pTab /* The table being updated */
926){
927 ExprList *pNew = 0;
928 sqlite3 *db = pParse->db;
929 int i;
drh552562c2021-02-04 20:52:20930
drhdac9a5f2021-01-29 21:18:46931 for(i=0; i<pList->nExpr; i++){
932 Expr *pOldExpr = pList->a[i].pExpr;
drh0d23f672021-03-30 01:52:21933 if( NEVER(pOldExpr==0) ) continue;
934 if( isAsteriskTerm(pParse, pOldExpr) ){
drhc9be8632021-02-02 00:16:15935 int jj;
936 for(jj=0; jj<pTab->nCol; jj++){
drh87296422021-02-07 12:59:43937 Expr *pNewExpr;
drhc9be8632021-02-02 00:16:15938 if( IsHiddenColumn(pTab->aCol+jj) ) continue;
drhcf9d36d2021-08-02 18:03:43939 pNewExpr = sqlite3Expr(db, TK_ID, pTab->aCol[jj].zCnName);
drhdac9a5f2021-01-29 21:18:46940 pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr);
941 if( !db->mallocFailed ){
942 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
drhcf9d36d2021-08-02 18:03:43943 pItem->zEName = sqlite3DbStrDup(db, pTab->aCol[jj].zCnName);
drhd88fd532022-05-02 20:49:30944 pItem->fg.eEName = ENAME_NAME;
drhdac9a5f2021-01-29 21:18:46945 }
946 }
947 }else{
948 Expr *pNewExpr = sqlite3ExprDup(db, pOldExpr, 0);
949 pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr);
drh9407b6e2021-01-31 16:45:10950 if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0) ){
drhdac9a5f2021-01-29 21:18:46951 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
952 pItem->zEName = sqlite3DbStrDup(db, pList->a[i].zEName);
drhd88fd532022-05-02 20:49:30953 pItem->fg.eEName = pList->a[i].fg.eEName;
drhdac9a5f2021-01-29 21:18:46954 }
955 }
956 }
957 return pNew;
958}
959
drh0868d582024-04-24 16:36:37960/* If the Expr node is a subquery or an EXISTS operator or an IN operator that
961** uses a subquery, and if the subquery is SF_Correlated, then mark the
962** expression as EP_VarSelect.
963*/
964static int sqlite3ReturningSubqueryVarSelect(Walker *NotUsed, Expr *pExpr){
965 UNUSED_PARAMETER(NotUsed);
966 if( ExprUseXSelect(pExpr)
967 && (pExpr->x.pSelect->selFlags & SF_Correlated)!=0
968 ){
969 testcase( ExprHasProperty(pExpr, EP_VarSelect) );
970 ExprSetProperty(pExpr, EP_VarSelect);
971 }
972 return WRC_Continue;
973}
974
975
976/*
977** If the SELECT references the table pWalker->u.pTab, then do two things:
978**
979** (1) Mark the SELECT as as SF_Correlated.
980** (2) Set pWalker->eCode to non-zero so that the caller will know
981** that (1) has happened.
982*/
983static int sqlite3ReturningSubqueryCorrelated(Walker *pWalker, Select *pSelect){
984 int i;
985 SrcList *pSrc;
986 assert( pSelect!=0 );
987 pSrc = pSelect->pSrc;
988 assert( pSrc!=0 );
989 for(i=0; i<pSrc->nSrc; i++){
drhb204b6a2024-08-17 23:23:23990 if( pSrc->a[i].pSTab==pWalker->u.pTab ){
drh0868d582024-04-24 16:36:37991 testcase( pSelect->selFlags & SF_Correlated );
992 pSelect->selFlags |= SF_Correlated;
993 pWalker->eCode = 1;
994 break;
995 }
996 }
997 return WRC_Continue;
998}
999
1000/*
1001** Scan the expression list that is the argument to RETURNING looking
1002** for subqueries that depend on the table which is being modified in the
1003** statement that is hosting the RETURNING clause (pTab). Mark all such
1004** subqueries as SF_Correlated. If the subqueries are part of an
1005** expression, mark the expression as EP_VarSelect.
1006**
1007** https://sqlite.org/forum/forumpost/2c83569ce8945d39
1008*/
1009static void sqlite3ProcessReturningSubqueries(
1010 ExprList *pEList,
1011 Table *pTab
1012){
1013 Walker w;
1014 memset(&w, 0, sizeof(w));
1015 w.xExprCallback = sqlite3ExprWalkNoop;
1016 w.xSelectCallback = sqlite3ReturningSubqueryCorrelated;
1017 w.u.pTab = pTab;
1018 sqlite3WalkExprList(&w, pEList);
1019 if( w.eCode ){
1020 w.xExprCallback = sqlite3ReturningSubqueryVarSelect;
1021 w.xSelectCallback = sqlite3SelectWalkNoop;
1022 sqlite3WalkExprList(&w, pEList);
1023 }
1024}
1025
drhf26e09c2003-05-31 16:21:121026/*
drh381bdac2021-02-04 17:29:041027** Generate code for the RETURNING trigger. Unlike other triggers
1028** that invoke a subprogram in the bytecode, the code for RETURNING
1029** is generated in-line.
1030*/
1031static void codeReturningTrigger(
1032 Parse *pParse, /* Parse context */
1033 Trigger *pTrigger, /* The trigger step that defines the RETURNING */
1034 Table *pTab, /* The table to code triggers from */
drh552562c2021-02-04 20:52:201035 int regIn /* The first in an array of registers */
drh381bdac2021-02-04 17:29:041036){
1037 Vdbe *v = pParse->pVdbe;
drh90881862021-05-19 12:17:031038 sqlite3 *db = pParse->db;
drh381bdac2021-02-04 17:29:041039 ExprList *pNew;
1040 Returning *pReturning;
drh90881862021-05-19 12:17:031041 Select sSelect;
drhcebf06c2025-03-14 18:10:021042 SrcList *pFrom;
1043 u8 fromSpace[SZ_SRCLIST_1];
drh381bdac2021-02-04 17:29:041044
1045 assert( v!=0 );
drh113e15f2023-10-26 16:59:221046 if( !pParse->bReturning ){
1047 /* This RETURNING trigger must be for a different statement as
1048 ** this statement lacks a RETURNING clause. */
1049 return;
1050 }
drh0c7d3d32022-01-24 16:47:121051 assert( db->pParse==pParse );
drh7fd936e2025-02-07 15:49:211052 assert( !pParse->isCreate );
1053 pReturning = pParse->u1.d.pReturning;
drh113e15f2023-10-26 16:59:221054 if( pTrigger != &(pReturning->retTrig) ){
1055 /* This RETURNING trigger is for a different statement */
1056 return;
1057 }
drh90881862021-05-19 12:17:031058 memset(&sSelect, 0, sizeof(sSelect));
drhcebf06c2025-03-14 18:10:021059 pFrom = (SrcList*)fromSpace;
1060 memset(pFrom, 0, SZ_SRCLIST_1);
drh90881862021-05-19 12:17:031061 sSelect.pEList = sqlite3ExprListDup(db, pReturning->pReturnEL, 0);
drhcebf06c2025-03-14 18:10:021062 sSelect.pSrc = pFrom;
1063 pFrom->nSrc = 1;
1064 pFrom->a[0].pSTab = pTab;
1065 pFrom->a[0].zName = pTab->zName; /* tag-20240424-1 */
1066 pFrom->a[0].iCursor = -1;
drh90881862021-05-19 12:17:031067 sqlite3SelectPrep(pParse, &sSelect, 0);
drh0c7d3d32022-01-24 16:47:121068 if( pParse->nErr==0 ){
1069 assert( db->mallocFailed==0 );
drh90881862021-05-19 12:17:031070 sqlite3GenerateColumnNames(pParse, &sSelect);
1071 }
1072 sqlite3ExprListDelete(db, sSelect.pEList);
drh381bdac2021-02-04 17:29:041073 pNew = sqlite3ExpandReturning(pParse, pReturning->pReturnEL, pTab);
drh8c26e6f2023-03-08 23:05:181074 if( pParse->nErr==0 ){
drh552562c2021-02-04 20:52:201075 NameContext sNC;
1076 memset(&sNC, 0, sizeof(sNC));
drhe1db9342021-02-04 21:17:121077 if( pReturning->nRetCol==0 ){
1078 pReturning->nRetCol = pNew->nExpr;
1079 pReturning->iRetCur = pParse->nTab++;
1080 }
drh552562c2021-02-04 20:52:201081 sNC.pParse = pParse;
1082 sNC.uNC.iBaseReg = regIn;
1083 sNC.ncFlags = NC_UBaseReg;
1084 pParse->eTriggerOp = pTrigger->op;
1085 pParse->pTriggerTab = pTab;
danc6977c12022-01-05 15:54:021086 if( sqlite3ResolveExprListNames(&sNC, pNew)==SQLITE_OK
drh1da88b52022-01-24 19:38:561087 && ALWAYS(!db->mallocFailed)
danc6977c12022-01-05 15:54:021088 ){
drh552562c2021-02-04 20:52:201089 int i;
1090 int nCol = pNew->nExpr;
1091 int reg = pParse->nMem+1;
drh0868d582024-04-24 16:36:371092 sqlite3ProcessReturningSubqueries(pNew, pTab);
drh552562c2021-02-04 20:52:201093 pParse->nMem += nCol+2;
1094 pReturning->iRetReg = reg;
1095 for(i=0; i<nCol; i++){
drh90881862021-05-19 12:17:031096 Expr *pCol = pNew->a[i].pExpr;
danc6977c12022-01-05 15:54:021097 assert( pCol!=0 ); /* Due to !db->mallocFailed ~9 lines above */
drh90881862021-05-19 12:17:031098 sqlite3ExprCodeFactorable(pParse, pCol, reg+i);
drh41584df2021-12-29 04:31:541099 if( sqlite3ExprAffinity(pCol)==SQLITE_AFF_REAL ){
1100 sqlite3VdbeAddOp1(v, OP_RealAffinity, reg+i);
1101 }
drh381bdac2021-02-04 17:29:041102 }
drh552562c2021-02-04 20:52:201103 sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, i, reg+i);
1104 sqlite3VdbeAddOp2(v, OP_NewRowid, pReturning->iRetCur, reg+i+1);
1105 sqlite3VdbeAddOp3(v, OP_Insert, pReturning->iRetCur, reg+i, reg+i+1);
1106 }
drh552562c2021-02-04 20:52:201107 }
drhebc44342022-01-05 11:49:581108 sqlite3ExprListDelete(db, pNew);
1109 pParse->eTriggerOp = 0;
1110 pParse->pTriggerTab = 0;
drh381bdac2021-02-04 17:29:041111}
1112
1113
1114
1115/*
dan65a7cd12009-09-01 12:16:011116** Generate VDBE code for the statements inside the body of a single
1117** trigger.
drhc977f7f2002-05-21 11:38:111118*/
danielk1977c3f9bad2002-05-15 08:30:121119static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:111120 Parse *pParse, /* The parser context */
1121 TriggerStep *pStepList, /* List of statements inside the trigger body */
dan65a7cd12009-09-01 12:16:011122 int orconf /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:581123){
dan65a7cd12009-09-01 12:16:011124 TriggerStep *pStep;
drh344737f2004-09-19 00:50:201125 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:381126 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:121127
dan65a7cd12009-09-01 12:16:011128 assert( pParse->pTriggerTab && pParse->pToplevel );
1129 assert( pStepList );
drh344737f2004-09-19 00:50:201130 assert( v!=0 );
dan65a7cd12009-09-01 12:16:011131 for(pStep=pStepList; pStep; pStep=pStep->pNext){
dan165921a2009-08-28 18:53:451132 /* Figure out the ON CONFLICT policy that will be used for this step
1133 ** of the trigger program. If the statement that caused this trigger
1134 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
1135 ** the ON CONFLICT policy that was specified as part of the trigger
1136 ** step statement. Example:
1137 **
1138 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
1139 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
1140 ** END;
1141 **
1142 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
1143 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
1144 */
shanecea72b22009-09-07 04:38:361145 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
drhaceb31b2014-02-08 01:40:271146 assert( pParse->okConstFactor==0 );
danf78baaf2012-12-06 19:37:221147
drhf259df52017-12-27 20:38:351148#ifndef SQLITE_OMIT_TRACE
drhe307e112017-12-27 21:30:341149 if( pStep->zSpan ){
1150 sqlite3VdbeAddOp4(v, OP_Trace, 0x7fffffff, 1, 0,
1151 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
1152 P4_DYNAMIC);
1153 }
drhf259df52017-12-27 20:38:351154#endif
1155
dan165921a2009-08-28 18:53:451156 switch( pStep->op ){
danielk1977633ed082002-05-17 00:05:581157 case TK_UPDATE: {
dan165921a2009-08-28 18:53:451158 sqlite3Update(pParse,
dane7877b22020-07-14 19:51:011159 sqlite3TriggerStepSrc(pParse, pStep),
dan165921a2009-08-28 18:53:451160 sqlite3ExprListDup(db, pStep->pExprList, 0),
1161 sqlite3ExprDup(db, pStep->pWhere, 0),
drheac9fab2018-04-16 13:00:501162 pParse->eOrconf, 0, 0, 0
dan165921a2009-08-28 18:53:451163 );
drhdac9a5f2021-01-29 21:18:461164 sqlite3VdbeAddOp0(v, OP_ResetCount);
danielk1977633ed082002-05-17 00:05:581165 break;
1166 }
1167 case TK_INSERT: {
dan165921a2009-08-28 18:53:451168 sqlite3Insert(pParse,
dane7877b22020-07-14 19:51:011169 sqlite3TriggerStepSrc(pParse, pStep),
dan165921a2009-08-28 18:53:451170 sqlite3SelectDup(db, pStep->pSelect, 0),
1171 sqlite3IdListDup(db, pStep->pIdList),
drh46d2e5c2018-04-12 13:15:431172 pParse->eOrconf,
1173 sqlite3UpsertDup(db, pStep->pUpsert)
dan165921a2009-08-28 18:53:451174 );
drhdac9a5f2021-01-29 21:18:461175 sqlite3VdbeAddOp0(v, OP_ResetCount);
danielk1977633ed082002-05-17 00:05:581176 break;
1177 }
1178 case TK_DELETE: {
dan165921a2009-08-28 18:53:451179 sqlite3DeleteFrom(pParse,
dane7877b22020-07-14 19:51:011180 sqlite3TriggerStepSrc(pParse, pStep),
drh8c0833f2017-11-14 23:48:231181 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
dan165921a2009-08-28 18:53:451182 );
drhdac9a5f2021-01-29 21:18:461183 sqlite3VdbeAddOp0(v, OP_ResetCount);
danielk1977633ed082002-05-17 00:05:581184 break;
1185 }
drh381bdac2021-02-04 17:29:041186 default: assert( pStep->op==TK_SELECT ); {
dan165921a2009-08-28 18:53:451187 SelectDest sDest;
1188 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
drhdac9a5f2021-01-29 21:18:461189 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
dan165921a2009-08-28 18:53:451190 sqlite3Select(pParse, pSelect, &sDest);
1191 sqlite3SelectDelete(db, pSelect);
drh9ab4c2e2009-05-09 00:18:381192 break;
1193 }
danielk1977633ed082002-05-17 00:05:581194 }
danielk1977633ed082002-05-17 00:05:581195 }
danielk1977c3f9bad2002-05-15 08:30:121196
danielk1977633ed082002-05-17 00:05:581197 return 0;
danielk1977c3f9bad2002-05-15 08:30:121198}
1199
drhc7379ce2013-10-30 02:28:231200#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
dan165921a2009-08-28 18:53:451201/*
1202** This function is used to add VdbeComment() annotations to a VDBE
1203** program. It is not used in production code, only for debugging.
1204*/
1205static const char *onErrorText(int onError){
1206 switch( onError ){
1207 case OE_Abort: return "abort";
1208 case OE_Rollback: return "rollback";
1209 case OE_Fail: return "fail";
1210 case OE_Replace: return "replace";
1211 case OE_Ignore: return "ignore";
1212 case OE_Default: return "default";
1213 }
1214 return "n/a";
1215}
1216#endif
1217
1218/*
1219** Parse context structure pFrom has just been used to create a sub-vdbe
1220** (trigger program). If an error has occurred, transfer error information
1221** from pFrom to pTo.
1222*/
1223static void transferParseError(Parse *pTo, Parse *pFrom){
1224 assert( pFrom->zErrMsg==0 || pFrom->nErr );
1225 assert( pTo->zErrMsg==0 || pTo->nErr );
1226 if( pTo->nErr==0 ){
1227 pTo->zErrMsg = pFrom->zErrMsg;
1228 pTo->nErr = pFrom->nErr;
drhe06874e2015-04-16 15:47:061229 pTo->rc = pFrom->rc;
dan165921a2009-08-28 18:53:451230 }else{
1231 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
1232 }
1233}
1234
dan65a7cd12009-09-01 12:16:011235/*
1236** Create and populate a new TriggerPrg object with a sub-program
1237** implementing trigger pTrigger with ON CONFLICT policy orconf.
1238*/
dan2832ad42009-08-31 15:27:271239static TriggerPrg *codeRowTrigger(
dan165921a2009-08-28 18:53:451240 Parse *pParse, /* Current parse context */
1241 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:011242 Table *pTab, /* The table pTrigger is attached to */
1243 int orconf /* ON CONFLICT policy to code trigger program with */
dan165921a2009-08-28 18:53:451244){
dan65a7cd12009-09-01 12:16:011245 Parse *pTop = sqlite3ParseToplevel(pParse);
1246 sqlite3 *db = pParse->db; /* Database handle */
1247 TriggerPrg *pPrg; /* Value to return */
dan165921a2009-08-28 18:53:451248 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
1249 Vdbe *v; /* Temporary VM */
dan165921a2009-08-28 18:53:451250 NameContext sNC; /* Name context for sub-vdbe */
1251 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
dan165921a2009-08-28 18:53:451252 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
drhc692df22022-01-24 15:34:551253 Parse sSubParse; /* Parse context for sub-vdbe */
dan165921a2009-08-28 18:53:451254
dan1da40a32009-09-19 17:00:311255 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dand19c9332010-07-26 12:05:171256 assert( pTop->pVdbe );
dan65a7cd12009-09-01 12:16:011257
1258 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
1259 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
1260 ** list of the top-level Parse object sooner rather than later. */
dan2832ad42009-08-31 15:27:271261 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
1262 if( !pPrg ) return 0;
dan65a7cd12009-09-01 12:16:011263 pPrg->pNext = pTop->pTriggerPrg;
1264 pTop->pTriggerPrg = pPrg;
dan2832ad42009-08-31 15:27:271265 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
dan165921a2009-08-28 18:53:451266 if( !pProgram ) return 0;
dand19c9332010-07-26 12:05:171267 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
dan2832ad42009-08-31 15:27:271268 pPrg->pTrigger = pTrigger;
1269 pPrg->orconf = orconf;
danbb5f1682009-11-27 12:12:341270 pPrg->aColmask[0] = 0xffffffff;
1271 pPrg->aColmask[1] = 0xffffffff;
dan165921a2009-08-28 18:53:451272
dan65a7cd12009-09-01 12:16:011273 /* Allocate and populate a new Parse context to use for coding the
1274 ** trigger sub-program. */
drhc692df22022-01-24 15:34:551275 sqlite3ParseObjectInit(&sSubParse, db);
dan165921a2009-08-28 18:53:451276 memset(&sNC, 0, sizeof(sNC));
drhc692df22022-01-24 15:34:551277 sNC.pParse = &sSubParse;
1278 sSubParse.pTriggerTab = pTab;
1279 sSubParse.pToplevel = pTop;
1280 sSubParse.zAuthContext = pTrigger->zName;
1281 sSubParse.eTriggerOp = pTrigger->op;
1282 sSubParse.nQueryLoop = pParse->nQueryLoop;
drh7424aef2022-10-01 13:17:531283 sSubParse.prepFlags = pParse->prepFlags;
drhede16902025-02-07 13:37:151284 sSubParse.oldmask = 0;
1285 sSubParse.newmask = 0;
dan165921a2009-08-28 18:53:451286
drhc692df22022-01-24 15:34:551287 v = sqlite3GetVdbe(&sSubParse);
dan165921a2009-08-28 18:53:451288 if( v ){
dan76d462e2009-08-30 11:42:511289 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
1290 pTrigger->zName, onErrorText(orconf),
dan165921a2009-08-28 18:53:451291 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
dan65a7cd12009-09-01 12:16:011292 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
1293 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
1294 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
dan76d462e2009-08-30 11:42:511295 pTab->zName
dan165921a2009-08-28 18:53:451296 ));
dan76d462e2009-08-30 11:42:511297#ifndef SQLITE_OMIT_TRACE
drhe307e112017-12-27 21:30:341298 if( pTrigger->zName ){
1299 sqlite3VdbeChangeP4(v, -1,
1300 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
1301 );
1302 }
dan76d462e2009-08-30 11:42:511303#endif
dan165921a2009-08-28 18:53:451304
dan65a7cd12009-09-01 12:16:011305 /* If one was specified, code the WHEN clause. If it evaluates to false
1306 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
1307 ** OP_Halt inserted at the end of the program. */
dan165921a2009-08-28 18:53:451308 if( pTrigger->pWhen ){
dan165921a2009-08-28 18:53:451309 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
drh4c4a2572021-04-06 12:50:241310 if( db->mallocFailed==0
1311 && SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
dan523a0872009-08-31 05:23:321312 ){
drhc692df22022-01-24 15:34:551313 iEndTrigger = sqlite3VdbeMakeLabel(&sSubParse);
1314 sqlite3ExprIfFalse(&sSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
dan165921a2009-08-28 18:53:451315 }
1316 sqlite3ExprDelete(db, pWhen);
1317 }
1318
1319 /* Code the trigger program into the sub-vdbe. */
drhc692df22022-01-24 15:34:551320 codeTriggerProgram(&sSubParse, pTrigger->step_list, orconf);
dan65a7cd12009-09-01 12:16:011321
1322 /* Insert an OP_Halt at the end of the sub-program. */
dan165921a2009-08-28 18:53:451323 if( iEndTrigger ){
1324 sqlite3VdbeResolveLabel(v, iEndTrigger);
1325 }
1326 sqlite3VdbeAddOp0(v, OP_Halt);
dan76d462e2009-08-30 11:42:511327 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
drhc692df22022-01-24 15:34:551328 transferParseError(pParse, &sSubParse);
dan76d462e2009-08-30 11:42:511329
drh0c7d3d32022-01-24 16:47:121330 if( pParse->nErr==0 ){
1331 assert( db->mallocFailed==0 );
dan65a7cd12009-09-01 12:16:011332 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
dan523a0872009-08-31 05:23:321333 }
drhc692df22022-01-24 15:34:551334 pProgram->nMem = sSubParse.nMem;
1335 pProgram->nCsr = sSubParse.nTab;
dan165921a2009-08-28 18:53:451336 pProgram->token = (void *)pTrigger;
drhc692df22022-01-24 15:34:551337 pPrg->aColmask[0] = sSubParse.oldmask;
1338 pPrg->aColmask[1] = sSubParse.newmask;
dan165921a2009-08-28 18:53:451339 sqlite3VdbeDelete(v);
drh0c7d3d32022-01-24 16:47:121340 }else{
1341 transferParseError(pParse, &sSubParse);
dan165921a2009-08-28 18:53:451342 }
dan65a7cd12009-09-01 12:16:011343
drhc692df22022-01-24 15:34:551344 assert( !sSubParse.pTriggerPrg && !sSubParse.nMaxArg );
1345 sqlite3ParseObjectReset(&sSubParse);
dan2832ad42009-08-31 15:27:271346 return pPrg;
dan165921a2009-08-28 18:53:451347}
1348
dan65a7cd12009-09-01 12:16:011349/*
1350** Return a pointer to a TriggerPrg object containing the sub-program for
1351** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
1352** TriggerPrg object exists, a new object is allocated and populated before
1353** being returned.
1354*/
dan2832ad42009-08-31 15:27:271355static TriggerPrg *getRowTrigger(
dan65a7cd12009-09-01 12:16:011356 Parse *pParse, /* Current parse context */
dan165921a2009-08-28 18:53:451357 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:011358 Table *pTab, /* The table trigger pTrigger is attached to */
1359 int orconf /* ON CONFLICT algorithm. */
dan165921a2009-08-28 18:53:451360){
dan65a7cd12009-09-01 12:16:011361 Parse *pRoot = sqlite3ParseToplevel(pParse);
dan2832ad42009-08-31 15:27:271362 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:011363
dan1da40a32009-09-19 17:00:311364 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dan165921a2009-08-28 18:53:451365
1366 /* It may be that this trigger has already been coded (or is in the
1367 ** process of being coded). If this is the case, then an entry with
dan2832ad42009-08-31 15:27:271368 ** a matching TriggerPrg.pTrigger field will be present somewhere
1369 ** in the Parse.pTriggerPrg list. Search for such an entry. */
dan2832ad42009-08-31 15:27:271370 for(pPrg=pRoot->pTriggerPrg;
1371 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
1372 pPrg=pPrg->pNext
dan165921a2009-08-28 18:53:451373 );
1374
dan65a7cd12009-09-01 12:16:011375 /* If an existing TriggerPrg could not be located, create a new one. */
dan2832ad42009-08-31 15:27:271376 if( !pPrg ){
dan65a7cd12009-09-01 12:16:011377 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
drhe1c47432022-02-07 18:52:561378 pParse->db->errByteOffset = -1;
dan165921a2009-08-28 18:53:451379 }
1380
dan2832ad42009-08-31 15:27:271381 return pPrg;
dan165921a2009-08-28 18:53:451382}
1383
dan94d7f502009-09-24 09:05:491384/*
1385** Generate code for the trigger program associated with trigger p on
1386** table pTab. The reg, orconf and ignoreJump parameters passed to this
1387** function are the same as those described in the header function for
1388** sqlite3CodeRowTrigger()
1389*/
dan1da40a32009-09-19 17:00:311390void sqlite3CodeRowTriggerDirect(
1391 Parse *pParse, /* Parse context */
1392 Trigger *p, /* Trigger to code */
1393 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:491394 int reg, /* Reg array containing OLD.* and NEW.* values */
dan1da40a32009-09-19 17:00:311395 int orconf, /* ON CONFLICT policy */
1396 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
1397){
1398 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
1399 TriggerPrg *pPrg;
1400 pPrg = getRowTrigger(pParse, p, pTab, orconf);
drh0c7d3d32022-01-24 16:47:121401 assert( pPrg || pParse->nErr );
dan1da40a32009-09-19 17:00:311402
1403 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
1404 ** is a pointer to the sub-vdbe containing the trigger program. */
1405 if( pPrg ){
dand19c9332010-07-26 12:05:171406 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
1407
drh9b34abe2016-01-16 15:12:351408 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
1409 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
dan1da40a32009-09-19 17:00:311410 VdbeComment(
1411 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
1412
1413 /* Set the P5 operand of the OP_Program instruction to non-zero if
1414 ** recursive invocation of this trigger program is disallowed. Recursive
1415 ** invocation is disallowed if (a) the sub-program is really a trigger,
1416 ** not a foreign key action, and (b) the flag to enable recursive triggers
1417 ** is clear. */
drh35d302c2024-12-12 15:11:271418 sqlite3VdbeChangeP5(v, (u16)bRecursive);
dan1da40a32009-09-19 17:00:311419 }
1420}
1421
danielk1977633ed082002-05-17 00:05:581422/*
dan94d7f502009-09-24 09:05:491423** This is called to code the required FOR EACH ROW triggers for an operation
1424** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
drhf7b54962013-05-28 12:11:541425** is given by the op parameter. The tr_tm parameter determines whether the
dan94d7f502009-09-24 09:05:491426** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
1427** parameter pChanges is passed the list of columns being modified.
danielk1977633ed082002-05-17 00:05:581428**
dan94d7f502009-09-24 09:05:491429** If there are no triggers that fire at the specified time for the specified
1430** operation on pTab, this function is a no-op.
drhc977f7f2002-05-21 11:38:111431**
dan94d7f502009-09-24 09:05:491432** The reg argument is the address of the first in an array of registers
1433** that contain the values substituted for the new.* and old.* references
1434** in the trigger program. If N is the number of columns in table pTab
1435** (a copy of pTab->nCol), then registers are populated as follows:
drhc977f7f2002-05-21 11:38:111436**
dan94d7f502009-09-24 09:05:491437** Register Contains
1438** ------------------------------------------------------
1439** reg+0 OLD.rowid
1440** reg+1 OLD.* value of left-most column of pTab
1441** ... ...
1442** reg+N OLD.* value of right-most column of pTab
1443** reg+N+1 NEW.rowid
drh381bdac2021-02-04 17:29:041444** reg+N+2 NEW.* value of left-most column of pTab
dan94d7f502009-09-24 09:05:491445** ... ...
1446** reg+N+N+1 NEW.* value of right-most column of pTab
drhc977f7f2002-05-21 11:38:111447**
dan94d7f502009-09-24 09:05:491448** For ON DELETE triggers, the registers containing the NEW.* values will
1449** never be accessed by the trigger program, so they are not allocated or
1450** populated by the caller (there is no data to populate them with anyway).
1451** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
1452** are never accessed, and so are not allocated by the caller. So, for an
1453** ON INSERT trigger, the value passed to this function as parameter reg
1454** is not a readable register, although registers (reg+N) through
1455** (reg+N+N+1) are.
danielk1977633ed082002-05-17 00:05:581456**
dan94d7f502009-09-24 09:05:491457** Parameter orconf is the default conflict resolution algorithm for the
1458** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
1459** is the instruction that control should jump to if a trigger program
1460** raises an IGNORE exception.
danielk1977633ed082002-05-17 00:05:581461*/
dan165921a2009-08-28 18:53:451462void sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:581463 Parse *pParse, /* Parse context */
danielk19772f886d12009-02-28 10:47:411464 Trigger *pTrigger, /* List of triggers on table pTab */
danielk1977633ed082002-05-17 00:05:581465 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
1466 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:131467 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:581468 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:491469 int reg, /* The first in an array of registers (see above) */
danielk19776f349032002-06-11 02:25:401470 int orconf, /* ON CONFLICT policy */
dan165921a2009-08-28 18:53:451471 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
drhc977f7f2002-05-21 11:38:111472){
dan94d7f502009-09-24 09:05:491473 Trigger *p; /* Used to iterate through pTrigger list */
danielk19778f2c54e2008-01-01 19:02:091474
dan94d7f502009-09-24 09:05:491475 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
1476 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
1477 assert( (op==TK_UPDATE)==(pChanges!=0) );
danielk1977c3f9bad2002-05-15 08:30:121478
danielk19772f886d12009-02-28 10:47:411479 for(p=pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:121480
drh9ab4c2e2009-05-09 00:18:381481 /* Sanity checking: The schema for the trigger and for the table are
1482 ** always defined. The trigger must be in the same schema as the table
1483 ** or else it must be a TEMP trigger. */
1484 assert( p->pSchema!=0 );
1485 assert( p->pTabSchema!=0 );
dan165921a2009-08-28 18:53:451486 assert( p->pSchema==p->pTabSchema
1487 || p->pSchema==pParse->db->aDb[1].pSchema );
drh9ab4c2e2009-05-09 00:18:381488
drh28828c52021-01-30 21:55:381489 /* Determine whether we should code this trigger. One of two choices:
1490 ** 1. The trigger is an exact match to the current DML statement
1491 ** 2. This is a RETURNING trigger for INSERT but we are currently
1492 ** doing the UPDATE part of an UPSERT.
1493 */
1494 if( (p->op==op || (p->bReturning && p->op==TK_INSERT && op==TK_UPDATE))
dan165921a2009-08-28 18:53:451495 && p->tr_tm==tr_tm
dan94d7f502009-09-24 09:05:491496 && checkColumnOverlap(p->pColumns, pChanges)
danielk1977eecfb3e2006-01-10 12:31:391497 ){
drh381bdac2021-02-04 17:29:041498 if( !p->bReturning ){
1499 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
1500 }else if( sqlite3IsToplevel(pParse) ){
1501 codeReturningTrigger(pParse, p, pTab, reg);
1502 }
danielk1977c3f9bad2002-05-15 08:30:121503 }
danielk1977c3f9bad2002-05-15 08:30:121504 }
danielk1977c3f9bad2002-05-15 08:30:121505}
dan165921a2009-08-28 18:53:451506
dan2832ad42009-08-31 15:27:271507/*
danbb5f1682009-11-27 12:12:341508** Triggers may access values stored in the old.* or new.* pseudo-table.
1509** This function returns a 32-bit bitmask indicating which columns of the
1510** old.* or new.* tables actually are used by triggers. This information
1511** may be used by the caller, for example, to avoid having to load the entire
1512** old.* record into memory when executing an UPDATE or DELETE command.
dan2832ad42009-08-31 15:27:271513**
1514** Bit 0 of the returned mask is set if the left-most column of the
danbb5f1682009-11-27 12:12:341515** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
dan2832ad42009-08-31 15:27:271516** the second leftmost column value is required, and so on. If there
1517** are more than 32 columns in the table, and at least one of the columns
1518** with an index greater than 32 may be accessed, 0xffffffff is returned.
1519**
danbb5f1682009-11-27 12:12:341520** It is not possible to determine if the old.rowid or new.rowid column is
1521** accessed by triggers. The caller must always assume that it is.
dan2832ad42009-08-31 15:27:271522**
danbb5f1682009-11-27 12:12:341523** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
1524** applies to the old.* table. If 1, the new.* table.
1525**
1526** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
1527** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
1528** included in the returned mask if the TRIGGER_BEFORE bit is set in the
1529** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
1530** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
dan2832ad42009-08-31 15:27:271531*/
danbb5f1682009-11-27 12:12:341532u32 sqlite3TriggerColmask(
dan165921a2009-08-28 18:53:451533 Parse *pParse, /* Parse context */
1534 Trigger *pTrigger, /* List of triggers on table pTab */
dan165921a2009-08-28 18:53:451535 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
danbb5f1682009-11-27 12:12:341536 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
1537 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
dan165921a2009-08-28 18:53:451538 Table *pTab, /* The table to code triggers from */
dan2832ad42009-08-31 15:27:271539 int orconf /* Default ON CONFLICT policy for trigger steps */
dan165921a2009-08-28 18:53:451540){
dan1da40a32009-09-19 17:00:311541 const int op = pChanges ? TK_UPDATE : TK_DELETE;
dan2832ad42009-08-31 15:27:271542 u32 mask = 0;
dan165921a2009-08-28 18:53:451543 Trigger *p;
dan165921a2009-08-28 18:53:451544
danbb5f1682009-11-27 12:12:341545 assert( isNew==1 || isNew==0 );
drh3cbf38c2023-03-28 11:18:041546 if( IsView(pTab) ){
1547 return 0xffffffff;
1548 }
dan165921a2009-08-28 18:53:451549 for(p=pTrigger; p; p=p->pNext){
drh381bdac2021-02-04 17:29:041550 if( p->op==op
1551 && (tr_tm&p->tr_tm)
danbb5f1682009-11-27 12:12:341552 && checkColumnOverlap(p->pColumns,pChanges)
1553 ){
drh381bdac2021-02-04 17:29:041554 if( p->bReturning ){
1555 mask = 0xffffffff;
1556 }else{
1557 TriggerPrg *pPrg;
1558 pPrg = getRowTrigger(pParse, p, pTab, orconf);
1559 if( pPrg ){
1560 mask |= pPrg->aColmask[isNew];
1561 }
dan165921a2009-08-28 18:53:451562 }
1563 }
1564 }
dan2832ad42009-08-31 15:27:271565
1566 return mask;
dan165921a2009-08-28 18:53:451567}
1568
drhb7f91642004-10-31 02:22:471569#endif /* !defined(SQLITE_OMIT_TRIGGER) */