blob: 578d1e42ef32b558bf1bc7cd91ae2a5eb9759536 [file] [log] [blame]
danielk1977c3f9bad2002-05-15 08:30:12 +00001/*
danielk1977633ed082002-05-17 00:05:58 +00002**
3** The author disclaims copyright to this source code. In place of
4** a legal notice, here is a blessing:
5**
6** May you do good and not evil.
7** May you find forgiveness for yourself and forgive others.
8** May you share freely, never taking more than you give.
9**
10*************************************************************************
drhc81c11f2009-11-10 01:30:52 +000011** This file contains the implementation for TRIGGERs
drh9adf9ac2002-05-15 11:44:13 +000012*/
danielk1977c3f9bad2002-05-15 08:30:12 +000013#include "sqliteInt.h"
drh9adf9ac2002-05-15 11:44:13 +000014
drhb7f91642004-10-31 02:22:47 +000015#ifndef SQLITE_OMIT_TRIGGER
danielk1977c3f9bad2002-05-15 08:30:12 +000016/*
drh4b59ab52002-08-24 18:24:51 +000017** Delete a linked list of TriggerStep structures.
18*/
drh633e6d52008-07-28 19:34:53 +000019void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
drh4b59ab52002-08-24 18:24:51 +000020 while( pTriggerStep ){
21 TriggerStep * pTmp = pTriggerStep;
22 pTriggerStep = pTriggerStep->pNext;
23
drh633e6d52008-07-28 19:34:53 +000024 sqlite3ExprDelete(db, pTmp->pWhere);
25 sqlite3ExprListDelete(db, pTmp->pExprList);
26 sqlite3SelectDelete(db, pTmp->pSelect);
27 sqlite3IdListDelete(db, pTmp->pIdList);
drh46d2e5c2018-04-12 13:15:43 +000028 sqlite3UpsertDelete(db, pTmp->pUpsert);
dane7877b22020-07-14 19:51:01 +000029 sqlite3SrcListDelete(db, pTmp->pFrom);
drhf259df52017-12-27 20:38:35 +000030 sqlite3DbFree(db, pTmp->zSpan);
drh4b59ab52002-08-24 18:24:51 +000031
drh633e6d52008-07-28 19:34:53 +000032 sqlite3DbFree(db, pTmp);
drh4b59ab52002-08-24 18:24:51 +000033 }
34}
35
36/*
danielk19772f886d12009-02-28 10:47:41 +000037** Given table pTab, return a list of all the triggers attached to
38** the table. The list is connected by Trigger.pNext pointers.
drh9ab4c2e2009-05-09 00:18:38 +000039**
40** All of the triggers on pTab that are in the same database as pTab
41** are already attached to pTab->pTrigger. But there might be additional
42** triggers on pTab in the TEMP schema. This routine prepends all
43** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
44** and returns the combined list.
45**
46** To state it another way: This routine returns a list of all triggers
47** that fire off of pTab. The list will include any TEMP triggers on
48** pTab as well as the triggers lised in pTab->pTrigger.
danielk19772f886d12009-02-28 10:47:41 +000049*/
50Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
drhf54a80f2021-01-29 13:47:36 +000051 Schema *pTmpSchema; /* Schema of the pTab table */
52 Trigger *pList; /* List of triggers to return */
53 HashElem *p; /* Loop variable for TEMP triggers */
danielk19772f886d12009-02-28 10:47:41 +000054
dand66c8302009-09-28 14:49:01 +000055 if( pParse->disableTriggers ){
56 return 0;
57 }
drhf54a80f2021-01-29 13:47:36 +000058 pTmpSchema = pParse->db->aDb[1].pSchema;
59 p = sqliteHashFirst(&pTmpSchema->trigHash);
60 if( p==0 ){
61 return pTab->pTrigger;
62 }
63 pList = pTab->pTrigger;
danielk19772f886d12009-02-28 10:47:41 +000064 if( pTmpSchema!=pTab->pSchema ){
drhf54a80f2021-01-29 13:47:36 +000065 while( p ){
danielk19772f886d12009-02-28 10:47:41 +000066 Trigger *pTrig = (Trigger *)sqliteHashData(p);
67 if( pTrig->pTabSchema==pTab->pSchema
drhb8352472021-01-29 19:32:17 +000068 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
danielk19772f886d12009-02-28 10:47:41 +000069 ){
drhf54a80f2021-01-29 13:47:36 +000070 pTrig->pNext = pList;
danielk19772f886d12009-02-28 10:47:41 +000071 pList = pTrig;
larrybrb0e62b02021-04-04 14:22:02 +000072 }else if( pTrig->op==TK_RETURNING
73#ifndef SQLITE_OMIT_VIRTUALTABLE
74 && pParse->db->pVtabCtx==0
75#endif
76 ){
drh381bdac2021-02-04 17:29:04 +000077 assert( pParse->bReturning );
78 assert( &(pParse->u1.pReturning->retTrig) == pTrig );
drhb8352472021-01-29 19:32:17 +000079 pTrig->table = pTab->zName;
80 pTrig->pTabSchema = pTab->pSchema;
81 pTrig->pNext = pList;
82 pList = pTrig;
83 }
drhf54a80f2021-01-29 13:47:36 +000084 p = sqliteHashNext(p);
danielk19772f886d12009-02-28 10:47:41 +000085 }
86 }
drhf54a80f2021-01-29 13:47:36 +000087 return pList;
danielk19772f886d12009-02-28 10:47:41 +000088}
89
90/*
drhf0f258b2003-04-21 18:48:45 +000091** This is called by the parser when it sees a CREATE TRIGGER statement
92** up to the point of the BEGIN before the trigger actions. A Trigger
93** structure is generated based on the information available and stored
94** in pParse->pNewTrigger. After the trigger actions have been parsed, the
danielk19774adee202004-05-08 08:23:19 +000095** sqlite3FinishTrigger() function is called to complete the trigger
drhf0f258b2003-04-21 18:48:45 +000096** construction process.
drh9adf9ac2002-05-15 11:44:13 +000097*/
danielk19774adee202004-05-08 08:23:19 +000098void sqlite3BeginTrigger(
drh9adf9ac2002-05-15 11:44:13 +000099 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
danielk1977ef2cb632004-05-29 02:37:19 +0000100 Token *pName1, /* The name of the trigger */
101 Token *pName2, /* The name of the trigger */
drh5cf590c2003-04-24 01:45:04 +0000102 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
drh9adf9ac2002-05-15 11:44:13 +0000103 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
danielk1977633ed082002-05-17 00:05:58 +0000104 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
drhd24cc422003-03-27 12:51:24 +0000105 SrcList *pTableName,/* The name of the table/view the trigger applies to */
drh9adf9ac2002-05-15 11:44:13 +0000106 Expr *pWhen, /* WHEN clause */
drhfdd48a72006-09-11 23:45:48 +0000107 int isTemp, /* True if the TEMPORARY keyword is present */
108 int noErr /* Suppress errors if the trigger already exists */
drh9adf9ac2002-05-15 11:44:13 +0000109){
drh3d5f74b2009-08-06 17:43:31 +0000110 Trigger *pTrigger = 0; /* The new trigger */
111 Table *pTab; /* Table that the trigger fires off of */
drhf0f258b2003-04-21 18:48:45 +0000112 char *zName = 0; /* Name of the trigger */
drh3d5f74b2009-08-06 17:43:31 +0000113 sqlite3 *db = pParse->db; /* The database connection */
danielk1977ef2cb632004-05-29 02:37:19 +0000114 int iDb; /* The database to store the trigger in */
115 Token *pName; /* The unqualified db name */
drh3d5f74b2009-08-06 17:43:31 +0000116 DbFixer sFix; /* State vector for the DB fixer */
drhed6c8672003-01-12 18:02:16 +0000117
drh43617e92006-03-06 20:55:46 +0000118 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
119 assert( pName2!=0 );
drhaa78bec2008-12-09 03:55:14 +0000120 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
121 assert( op>0 && op<0xff );
danielk1977ef2cb632004-05-29 02:37:19 +0000122 if( isTemp ){
123 /* If TEMP was specified, then the trigger name may not be qualified. */
drh43617e92006-03-06 20:55:46 +0000124 if( pName2->n>0 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000125 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
126 goto trigger_cleanup;
127 }
128 iDb = 1;
129 pName = pName1;
130 }else{
mistachkind5578432012-08-25 10:01:29 +0000131 /* Figure out the db that the trigger will be created in */
danielk1977ef2cb632004-05-29 02:37:19 +0000132 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
133 if( iDb<0 ){
134 goto trigger_cleanup;
135 }
136 }
drh6fea83e2011-07-01 13:50:44 +0000137 if( !pTableName || db->mallocFailed ){
138 goto trigger_cleanup;
139 }
140
141 /* A long-standing parser bug is that this syntax was allowed:
142 **
143 ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
144 ** ^^^^^^^^
145 **
146 ** To maintain backwards compatibility, ignore the database
drh346a70c2020-06-15 20:27:35 +0000147 ** name on pTableName if we are reparsing out of the schema table
drh6fea83e2011-07-01 13:50:44 +0000148 */
149 if( db->init.busy && iDb!=1 ){
150 sqlite3DbFree(db, pTableName->a[0].zDatabase);
151 pTableName->a[0].zDatabase = 0;
152 }
danielk1977ef2cb632004-05-29 02:37:19 +0000153
154 /* If the trigger name was unqualified, and the table is a temp table,
155 ** then set iDb to 1 to create the trigger in the temporary database.
156 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
157 ** exist, the error is caught by the block below.
drh9adf9ac2002-05-15 11:44:13 +0000158 */
danielk1977ef2cb632004-05-29 02:37:19 +0000159 pTab = sqlite3SrcListLookup(pParse, pTableName);
drh622d2882010-02-15 16:54:55 +0000160 if( db->init.busy==0 && pName2->n==0 && pTab
161 && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977ef2cb632004-05-29 02:37:19 +0000162 iDb = 1;
163 }
164
165 /* Ensure the table name matches database name and that the table exists */
drh17435752007-08-16 04:30:38 +0000166 if( db->mallocFailed ) goto trigger_cleanup;
drhd24cc422003-03-27 12:51:24 +0000167 assert( pTableName->nSrc==1 );
drhd100f692013-10-03 15:39:44 +0000168 sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
169 if( sqlite3FixSrcList(&sFix, pTableName) ){
drh4312db52003-06-03 01:47:11 +0000170 goto trigger_cleanup;
drhf26e09c2003-05-31 16:21:12 +0000171 }
danielk1977ef2cb632004-05-29 02:37:19 +0000172 pTab = sqlite3SrcListLookup(pParse, pTableName);
173 if( !pTab ){
174 /* The table does not exist. */
drh4e451aa2020-11-05 19:13:44 +0000175 goto trigger_orphan_error;
drhd24cc422003-03-27 12:51:24 +0000176 }
drh4cbdda92006-06-14 19:00:20 +0000177 if( IsVirtual(pTab) ){
178 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
drh4e451aa2020-11-05 19:13:44 +0000179 goto trigger_orphan_error;
drh4cbdda92006-06-14 19:00:20 +0000180 }
drhd24cc422003-03-27 12:51:24 +0000181
danielk1977d8123362004-06-12 09:25:12 +0000182 /* Check that the trigger name is not reserved and that no trigger of the
183 ** specified name exists */
drh17435752007-08-16 04:30:38 +0000184 zName = sqlite3NameFromToken(db, pName);
drhc5a93d42019-08-12 00:08:07 +0000185 if( zName==0 ){
186 assert( db->mallocFailed );
187 goto trigger_cleanup;
188 }
189 if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
danielk1977d8123362004-06-12 09:25:12 +0000190 goto trigger_cleanup;
191 }
drh21206082011-04-04 18:22:02 +0000192 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danc9461ec2018-08-29 21:00:16 +0000193 if( !IN_RENAME_OBJECT ){
dan5496d6a2018-08-13 17:14:26 +0000194 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
195 if( !noErr ){
196 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
197 }else{
198 assert( !db->init.busy );
199 sqlite3CodeVerifySchema(pParse, iDb);
200 }
201 goto trigger_cleanup;
drhfdd48a72006-09-11 23:45:48 +0000202 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000203 }
danielk1977ef2cb632004-05-29 02:37:19 +0000204
205 /* Do not create a trigger on a system table */
drhdca76842004-12-07 14:06:13 +0000206 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk19774adee202004-05-08 08:23:19 +0000207 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
drhd24cc422003-03-27 12:51:24 +0000208 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40 +0000209 }
danielk1977ef2cb632004-05-29 02:37:19 +0000210
211 /* INSTEAD of triggers are only for views and views only support INSTEAD
212 ** of triggers.
213 */
214 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000215 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drha9799932021-03-19 13:00:28 +0000216 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName->a);
drh4e451aa2020-11-05 19:13:44 +0000217 goto trigger_orphan_error;
drhd24cc422003-03-27 12:51:24 +0000218 }
danielk1977ef2cb632004-05-29 02:37:19 +0000219 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000220 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drha9799932021-03-19 13:00:28 +0000221 " trigger on table: %S", pTableName->a);
drh4e451aa2020-11-05 19:13:44 +0000222 goto trigger_orphan_error;
drhd24cc422003-03-27 12:51:24 +0000223 }
danielk1977ef2cb632004-05-29 02:37:19 +0000224
drhd24cc422003-03-27 12:51:24 +0000225#ifndef SQLITE_OMIT_AUTHORIZATION
danc9461ec2018-08-29 21:00:16 +0000226 if( !IN_RENAME_OBJECT ){
drha0daa752016-09-16 11:53:10 +0000227 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drhd24cc422003-03-27 12:51:24 +0000228 int code = SQLITE_CREATE_TRIGGER;
drh69c33822016-08-18 14:33:11 +0000229 const char *zDb = db->aDb[iTabDb].zDbSName;
230 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
danielk1977da184232006-01-05 11:34:32 +0000231 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19 +0000232 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24 +0000233 goto trigger_cleanup;
234 }
danielk1977da184232006-01-05 11:34:32 +0000235 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24 +0000236 goto trigger_cleanup;
237 }
238 }
239#endif
danielk1977d702fcc2002-05-26 23:24:40 +0000240
danielk1977ef2cb632004-05-29 02:37:19 +0000241 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04 +0000242 ** cannot appear on views. So we might as well translate every
243 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
244 ** elsewhere.
245 */
danielk1977d702fcc2002-05-26 23:24:40 +0000246 if (tr_tm == TK_INSTEAD){
247 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12 +0000248 }
249
250 /* Build the Trigger object */
drh17435752007-08-16 04:30:38 +0000251 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19 +0000252 if( pTrigger==0 ) goto trigger_cleanup;
dan165921a2009-08-28 18:53:45 +0000253 pTrigger->zName = zName;
drhe5f9c642003-01-13 23:27:31 +0000254 zName = 0;
drh17435752007-08-16 04:30:38 +0000255 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32 +0000256 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48 +0000257 pTrigger->pTabSchema = pTab->pSchema;
drhaa78bec2008-12-09 03:55:14 +0000258 pTrigger->op = (u8)op;
drhdca76842004-12-07 14:06:13 +0000259 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
danc9461ec2018-08-29 21:00:16 +0000260 if( IN_RENAME_OBJECT ){
261 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
dan5496d6a2018-08-13 17:14:26 +0000262 pTrigger->pWhen = pWhen;
263 pWhen = 0;
264 }else{
265 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
266 }
267 pTrigger->pColumns = pColumns;
268 pColumns = 0;
drhf0f258b2003-04-21 18:48:45 +0000269 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19 +0000270 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45 +0000271
272trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000273 sqlite3DbFree(db, zName);
274 sqlite3SrcListDelete(db, pTableName);
275 sqlite3IdListDelete(db, pColumns);
276 sqlite3ExprDelete(db, pWhen);
danielk1977d5d56522005-03-16 12:15:20 +0000277 if( !pParse->pNewTrigger ){
drh633e6d52008-07-28 19:34:53 +0000278 sqlite3DeleteTrigger(db, pTrigger);
danielk1977d5d56522005-03-16 12:15:20 +0000279 }else{
280 assert( pParse->pNewTrigger==pTrigger );
281 }
drh4e451aa2020-11-05 19:13:44 +0000282 return;
283
284trigger_orphan_error:
285 if( db->init.iDb==1 ){
286 /* Ticket #3810.
287 ** Normally, whenever a table is dropped, all associated triggers are
288 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
289 ** and the table is dropped by a different database connection, the
290 ** trigger is not visible to the database connection that does the
291 ** drop so the trigger cannot be dropped. This results in an
292 ** "orphaned trigger" - a trigger whose associated table is missing.
293 **
294 ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df
295 */
296 db->init.orphanTrigger = 1;
297 }
298 goto trigger_cleanup;
drhf0f258b2003-04-21 18:48:45 +0000299}
300
301/*
302** This routine is called after all of the trigger actions have been parsed
303** in order to complete the process of building the trigger.
304*/
danielk19774adee202004-05-08 08:23:19 +0000305void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45 +0000306 Parse *pParse, /* Parser context */
307 TriggerStep *pStepList, /* The triggered program */
308 Token *pAll /* Token that describes the complete CREATE TRIGGER */
309){
drha8c62df2010-02-15 15:47:18 +0000310 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
311 char *zName; /* Name of trigger */
312 sqlite3 *db = pParse->db; /* The database */
313 DbFixer sFix; /* Fixer object */
314 int iDb; /* Database containing the trigger */
315 Token nameToken; /* Trigger name for error reporting */
drhf0f258b2003-04-21 18:48:45 +0000316
drhf0f258b2003-04-21 18:48:45 +0000317 pParse->pNewTrigger = 0;
drh9ab4c2e2009-05-09 00:18:38 +0000318 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
dan165921a2009-08-28 18:53:45 +0000319 zName = pTrig->zName;
danielk1977da184232006-01-05 11:34:32 +0000320 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32 +0000321 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53 +0000322 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32 +0000323 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53 +0000324 pStepList = pStepList->pNext;
325 }
drh40aced52016-01-22 17:48:09 +0000326 sqlite3TokenInit(&nameToken, pTrig->zName);
drhd100f692013-10-03 15:39:44 +0000327 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
328 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
dan46539d72013-10-03 12:29:38 +0000329 || sqlite3FixExpr(&sFix, pTrig->pWhen)
drhd100f692013-10-03 15:39:44 +0000330 ){
drhf26e09c2003-05-31 16:21:12 +0000331 goto triggerfinish_cleanup;
332 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000333
dan5496d6a2018-08-13 17:14:26 +0000334#ifndef SQLITE_OMIT_ALTERTABLE
danc9461ec2018-08-29 21:00:16 +0000335 if( IN_RENAME_OBJECT ){
dan5496d6a2018-08-13 17:14:26 +0000336 assert( !db->init.busy );
337 pParse->pNewTrigger = pTrig;
338 pTrig = 0;
339 }else
340#endif
341
drha8c62df2010-02-15 15:47:18 +0000342 /* if we are not initializing,
drh1e32bed2020-06-19 13:33:53 +0000343 ** build the sqlite_schema entry
drh9adf9ac2002-05-15 11:44:13 +0000344 */
drh1d85d932004-02-14 23:05:52 +0000345 if( !db->init.busy ){
drhc977f7f2002-05-21 11:38:11 +0000346 Vdbe *v;
drh2d401ab2008-01-10 23:50:11 +0000347 char *z;
danielk1977c3f9bad2002-05-15 08:30:12 +0000348
drh1e32bed2020-06-19 13:33:53 +0000349 /* Make an entry in the sqlite_schema table */
danielk19774adee202004-05-08 08:23:19 +0000350 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000351 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000352 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11 +0000353 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
drhd96cc6f2017-06-08 14:35:21 +0000354 testcase( z==0 );
drh2d401ab2008-01-10 23:50:11 +0000355 sqlite3NestedParse(pParse,
drh346a70c2020-06-15 20:27:35 +0000356 "INSERT INTO %Q." DFLT_SCHEMA_TABLE
357 " VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
358 db->aDb[iDb].zDbSName, zName,
drh2d401ab2008-01-10 23:50:11 +0000359 pTrig->table, z);
drh633e6d52008-07-28 19:34:53 +0000360 sqlite3DbFree(db, z);
drh9cbf3422008-01-17 16:22:13 +0000361 sqlite3ChangeCookie(pParse, iDb);
drh5d9c9da2011-06-03 20:11:17 +0000362 sqlite3VdbeAddParseSchemaOp(v, iDb,
dan6a5a13d2021-02-17 20:08:22 +0000363 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName), 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000364 }
365
drh956bc922004-07-24 17:38:29 +0000366 if( db->init.busy ){
danielk19772f886d12009-02-28 10:47:41 +0000367 Trigger *pLink = pTrig;
368 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
drh21206082011-04-04 18:22:02 +0000369 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh55f66b32019-07-16 19:44:32 +0000370 assert( pLink!=0 );
drhacbcb7e2014-08-21 20:26:37 +0000371 pTrig = sqlite3HashInsert(pHash, zName, pTrig);
danielk19772f886d12009-02-28 10:47:41 +0000372 if( pTrig ){
drh4a642b62016-02-05 01:55:27 +0000373 sqlite3OomFault(db);
danielk19772f886d12009-02-28 10:47:41 +0000374 }else if( pLink->pSchema==pLink->pTabSchema ){
375 Table *pTab;
drhacbcb7e2014-08-21 20:26:37 +0000376 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
danielk19772f886d12009-02-28 10:47:41 +0000377 assert( pTab!=0 );
378 pLink->pNext = pTab->pTrigger;
379 pTab->pTrigger = pLink;
danielk1977d5d56522005-03-16 12:15:20 +0000380 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000381 }
382
drhf0f258b2003-04-21 18:48:45 +0000383triggerfinish_cleanup:
drh633e6d52008-07-28 19:34:53 +0000384 sqlite3DeleteTrigger(db, pTrig);
danc9461ec2018-08-29 21:00:16 +0000385 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
drh633e6d52008-07-28 19:34:53 +0000386 sqlite3DeleteTriggerStep(db, pStepList);
drh4b59ab52002-08-24 18:24:51 +0000387}
danielk1977c3f9bad2002-05-15 08:30:12 +0000388
drh4b59ab52002-08-24 18:24:51 +0000389/*
drhf259df52017-12-27 20:38:35 +0000390** Duplicate a range of text from an SQL statement, then convert all
391** whitespace characters into ordinary space characters.
392*/
393static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
394 char *z = sqlite3DbSpanDup(db, zStart, zEnd);
395 int i;
396 if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i]) ) z[i] = ' ';
397 return z;
398}
399
400/*
drhc977f7f2002-05-21 11:38:11 +0000401** Turn a SELECT statement (that the pSelect parameter points to) into
402** a trigger step. Return a pointer to a TriggerStep structure.
403**
404** The parser calls this routine when it finds a SELECT statement in
405** body of a TRIGGER.
406*/
drhf259df52017-12-27 20:38:35 +0000407TriggerStep *sqlite3TriggerSelectStep(
408 sqlite3 *db, /* Database connection */
409 Select *pSelect, /* The SELECT statement */
410 const char *zStart, /* Start of SQL text */
411 const char *zEnd /* End of SQL text */
412){
drh17435752007-08-16 04:30:38 +0000413 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000414 if( pTriggerStep==0 ) {
drh633e6d52008-07-28 19:34:53 +0000415 sqlite3SelectDelete(db, pSelect);
danielk19772e588c72005-12-09 14:25:08 +0000416 return 0;
417 }
danielk1977633ed082002-05-17 00:05:58 +0000418 pTriggerStep->op = TK_SELECT;
419 pTriggerStep->pSelect = pSelect;
420 pTriggerStep->orconf = OE_Default;
drhf259df52017-12-27 20:38:35 +0000421 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
drhb7916a72009-05-27 10:31:29 +0000422 return pTriggerStep;
423}
danielk1977c3f9bad2002-05-15 08:30:12 +0000424
drhb7916a72009-05-27 10:31:29 +0000425/*
426** Allocate space to hold a new trigger step. The allocated space
427** holds both the TriggerStep object and the TriggerStep.target.z string.
428**
429** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
430*/
431static TriggerStep *triggerStepAllocate(
danc9461ec2018-08-29 21:00:16 +0000432 Parse *pParse, /* Parser context */
shane5eff7cf2009-08-10 03:57:58 +0000433 u8 op, /* Trigger opcode */
drhf259df52017-12-27 20:38:35 +0000434 Token *pName, /* The target name */
435 const char *zStart, /* Start of SQL text */
436 const char *zEnd /* End of SQL text */
drhb7916a72009-05-27 10:31:29 +0000437){
danc9461ec2018-08-29 21:00:16 +0000438 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000439 TriggerStep *pTriggerStep;
440
dan46408352015-04-21 16:38:49 +0000441 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
drhb7916a72009-05-27 10:31:29 +0000442 if( pTriggerStep ){
443 char *z = (char*)&pTriggerStep[1];
444 memcpy(z, pName->z, pName->n);
dan46408352015-04-21 16:38:49 +0000445 sqlite3Dequote(z);
446 pTriggerStep->zTarget = z;
drhb7916a72009-05-27 10:31:29 +0000447 pTriggerStep->op = op;
drhf259df52017-12-27 20:38:35 +0000448 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
danc9461ec2018-08-29 21:00:16 +0000449 if( IN_RENAME_OBJECT ){
450 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
451 }
drhb7916a72009-05-27 10:31:29 +0000452 }
danielk1977633ed082002-05-17 00:05:58 +0000453 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000454}
455
drhc977f7f2002-05-21 11:38:11 +0000456/*
457** Build a trigger step out of an INSERT statement. Return a pointer
458** to the new trigger step.
459**
460** The parser calls this routine when it sees an INSERT inside the
461** body of a trigger.
462*/
danielk19774adee202004-05-08 08:23:19 +0000463TriggerStep *sqlite3TriggerInsertStep(
dan5be60c52018-08-15 20:28:39 +0000464 Parse *pParse, /* Parser */
drhc977f7f2002-05-21 11:38:11 +0000465 Token *pTableName, /* Name of the table into which we insert */
466 IdList *pColumn, /* List of columns in pTableName to insert into */
drhc977f7f2002-05-21 11:38:11 +0000467 Select *pSelect, /* A SELECT statement that supplies values */
drhf259df52017-12-27 20:38:35 +0000468 u8 orconf, /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
drh46d2e5c2018-04-12 13:15:43 +0000469 Upsert *pUpsert, /* ON CONFLICT clauses for upsert */
drhf259df52017-12-27 20:38:35 +0000470 const char *zStart, /* Start of SQL text */
471 const char *zEnd /* End of SQL text */
danielk1977633ed082002-05-17 00:05:58 +0000472){
dan5be60c52018-08-15 20:28:39 +0000473 sqlite3 *db = pParse->db;
drhf3a65f72007-08-22 20:18:21 +0000474 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000475
drh75593d92014-01-10 20:46:55 +0000476 assert(pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000477
danc9461ec2018-08-29 21:00:16 +0000478 pTriggerStep = triggerStepAllocate(pParse, TK_INSERT, pTableName,zStart,zEnd);
danielk1977d5d56522005-03-16 12:15:20 +0000479 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000480 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000481 pTriggerStep->pSelect = pSelect;
482 pSelect = 0;
483 }else{
484 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
485 }
danielk1977d5d56522005-03-16 12:15:20 +0000486 pTriggerStep->pIdList = pColumn;
drh46d2e5c2018-04-12 13:15:43 +0000487 pTriggerStep->pUpsert = pUpsert;
danielk1977d5d56522005-03-16 12:15:20 +0000488 pTriggerStep->orconf = orconf;
dan9105fd52019-08-19 17:26:32 +0000489 if( pUpsert ){
490 sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
491 }
danielk1977d5d56522005-03-16 12:15:20 +0000492 }else{
drh99160482018-04-18 01:34:39 +0000493 testcase( pColumn );
drh633e6d52008-07-28 19:34:53 +0000494 sqlite3IdListDelete(db, pColumn);
drh99160482018-04-18 01:34:39 +0000495 testcase( pUpsert );
drh46d2e5c2018-04-12 13:15:43 +0000496 sqlite3UpsertDelete(db, pUpsert);
danielk1977d5d56522005-03-16 12:15:20 +0000497 }
drh33e619f2009-05-28 01:00:55 +0000498 sqlite3SelectDelete(db, pSelect);
danielk1977c3f9bad2002-05-15 08:30:12 +0000499
danielk1977633ed082002-05-17 00:05:58 +0000500 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000501}
502
drhc977f7f2002-05-21 11:38:11 +0000503/*
504** Construct a trigger step that implements an UPDATE statement and return
505** a pointer to that trigger step. The parser calls this routine when it
506** sees an UPDATE statement inside the body of a CREATE TRIGGER.
507*/
danielk19774adee202004-05-08 08:23:19 +0000508TriggerStep *sqlite3TriggerUpdateStep(
dan5be60c52018-08-15 20:28:39 +0000509 Parse *pParse, /* Parser */
drhc977f7f2002-05-21 11:38:11 +0000510 Token *pTableName, /* Name of the table to be updated */
dane7877b22020-07-14 19:51:01 +0000511 SrcList *pFrom,
drhc977f7f2002-05-21 11:38:11 +0000512 ExprList *pEList, /* The SET clause: list of column and new values */
513 Expr *pWhere, /* The WHERE clause */
drhf259df52017-12-27 20:38:35 +0000514 u8 orconf, /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
515 const char *zStart, /* Start of SQL text */
516 const char *zEnd /* End of SQL text */
drhc977f7f2002-05-21 11:38:11 +0000517){
dan5be60c52018-08-15 20:28:39 +0000518 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000519 TriggerStep *pTriggerStep;
520
danc9461ec2018-08-29 21:00:16 +0000521 pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE, pTableName,zStart,zEnd);
drh33e619f2009-05-28 01:00:55 +0000522 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000523 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000524 pTriggerStep->pExprList = pEList;
525 pTriggerStep->pWhere = pWhere;
dane7877b22020-07-14 19:51:01 +0000526 pTriggerStep->pFrom = pFrom;
dan5be60c52018-08-15 20:28:39 +0000527 pEList = 0;
528 pWhere = 0;
dane7877b22020-07-14 19:51:01 +0000529 pFrom = 0;
dan5be60c52018-08-15 20:28:39 +0000530 }else{
531 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
532 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
dane7877b22020-07-14 19:51:01 +0000533 pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE);
dan5be60c52018-08-15 20:28:39 +0000534 }
drh33e619f2009-05-28 01:00:55 +0000535 pTriggerStep->orconf = orconf;
drh0e3a6f32007-03-30 20:40:34 +0000536 }
drh33e619f2009-05-28 01:00:55 +0000537 sqlite3ExprListDelete(db, pEList);
538 sqlite3ExprDelete(db, pWhere);
dane7877b22020-07-14 19:51:01 +0000539 sqlite3SrcListDelete(db, pFrom);
danielk1977633ed082002-05-17 00:05:58 +0000540 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000541}
542
drhc977f7f2002-05-21 11:38:11 +0000543/*
544** Construct a trigger step that implements a DELETE statement and return
545** a pointer to that trigger step. The parser calls this routine when it
546** sees a DELETE statement inside the body of a CREATE TRIGGER.
547*/
drh17435752007-08-16 04:30:38 +0000548TriggerStep *sqlite3TriggerDeleteStep(
dan5be60c52018-08-15 20:28:39 +0000549 Parse *pParse, /* Parser */
drh17435752007-08-16 04:30:38 +0000550 Token *pTableName, /* The table from which rows are deleted */
drhf259df52017-12-27 20:38:35 +0000551 Expr *pWhere, /* The WHERE clause */
552 const char *zStart, /* Start of SQL text */
553 const char *zEnd /* End of SQL text */
drh17435752007-08-16 04:30:38 +0000554){
dan5be60c52018-08-15 20:28:39 +0000555 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000556 TriggerStep *pTriggerStep;
557
danc9461ec2018-08-29 21:00:16 +0000558 pTriggerStep = triggerStepAllocate(pParse, TK_DELETE, pTableName,zStart,zEnd);
drh33e619f2009-05-28 01:00:55 +0000559 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000560 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000561 pTriggerStep->pWhere = pWhere;
562 pWhere = 0;
563 }else{
564 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
565 }
drh33e619f2009-05-28 01:00:55 +0000566 pTriggerStep->orconf = OE_Default;
drhb63a53d2007-03-31 01:34:44 +0000567 }
drh33e619f2009-05-28 01:00:55 +0000568 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000569 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000570}
571
danielk1977633ed082002-05-17 00:05:58 +0000572/*
573** Recursively delete a Trigger structure
574*/
drh633e6d52008-07-28 19:34:53 +0000575void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
drh658f0a32021-01-30 02:43:26 +0000576 if( pTrigger==0 || pTrigger->bReturning ) return;
drh633e6d52008-07-28 19:34:53 +0000577 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
dan165921a2009-08-28 18:53:45 +0000578 sqlite3DbFree(db, pTrigger->zName);
drh633e6d52008-07-28 19:34:53 +0000579 sqlite3DbFree(db, pTrigger->table);
580 sqlite3ExprDelete(db, pTrigger->pWhen);
581 sqlite3IdListDelete(db, pTrigger->pColumns);
drh633e6d52008-07-28 19:34:53 +0000582 sqlite3DbFree(db, pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000583}
584
585/*
danielk19778a414492004-06-29 08:59:35 +0000586** This function is called to drop a trigger from the database schema.
587**
588** This may be called directly from the parser and therefore identifies
589** the trigger by name. The sqlite3DropTriggerPtr() routine does the
590** same job as this routine except it takes a pointer to the trigger
591** instead of the trigger name.
592**/
drhfdd48a72006-09-11 23:45:48 +0000593void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000594 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000595 int i;
596 const char *zDb;
597 const char *zName;
drh9bb575f2004-09-06 17:24:11 +0000598 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000599
drh17435752007-08-16 04:30:38 +0000600 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000601 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000602 goto drop_trigger_cleanup;
603 }
danielk19778e227872004-06-07 07:52:17 +0000604
drhd24cc422003-03-27 12:51:24 +0000605 assert( pName->nSrc==1 );
606 zDb = pName->a[0].zDatabase;
607 zName = pName->a[0].zName;
drh21206082011-04-04 18:22:02 +0000608 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
danielk197753c0f742005-03-29 03:10:59 +0000609 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000610 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
dan465c2b82020-03-21 15:10:40 +0000611 if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
drh21206082011-04-04 18:22:02 +0000612 assert( sqlite3SchemaMutexHeld(db, j, 0) );
drhacbcb7e2014-08-21 20:26:37 +0000613 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
drhd24cc422003-03-27 12:51:24 +0000614 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000615 }
drhd24cc422003-03-27 12:51:24 +0000616 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000617 if( !noErr ){
drha9799932021-03-19 13:00:28 +0000618 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName->a);
dan57966752011-04-09 17:32:58 +0000619 }else{
620 sqlite3CodeVerifyNamedSchema(pParse, zDb);
drhfdd48a72006-09-11 23:45:48 +0000621 }
dan1db95102010-06-28 10:15:19 +0000622 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +0000623 goto drop_trigger_cleanup;
624 }
drh74161702006-02-24 02:53:49 +0000625 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000626
627drop_trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000628 sqlite3SrcListDelete(db, pName);
drh79a519c2003-05-17 19:04:03 +0000629}
630
631/*
drh956bc922004-07-24 17:38:29 +0000632** Return a pointer to the Table structure for the table that a trigger
633** is set on.
634*/
drh74161702006-02-24 02:53:49 +0000635static Table *tableOfTrigger(Trigger *pTrigger){
drhacbcb7e2014-08-21 20:26:37 +0000636 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
drh956bc922004-07-24 17:38:29 +0000637}
638
639
640/*
drh74161702006-02-24 02:53:49 +0000641** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000642*/
drh74161702006-02-24 02:53:49 +0000643void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000644 Table *pTable;
645 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000646 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000647 int iDb;
drh79a519c2003-05-17 19:04:03 +0000648
danielk1977da184232006-01-05 11:34:32 +0000649 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000650 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000651 pTable = tableOfTrigger(pTrigger);
drh6397a782019-08-27 10:05:45 +0000652 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000653#ifndef SQLITE_OMIT_AUTHORIZATION
drh6397a782019-08-27 10:05:45 +0000654 if( pTable ){
drhe5f9c642003-01-13 23:27:31 +0000655 int code = SQLITE_DROP_TRIGGER;
drh69c33822016-08-18 14:33:11 +0000656 const char *zDb = db->aDb[iDb].zDbSName;
drh956bc922004-07-24 17:38:29 +0000657 const char *zTab = SCHEMA_TABLE(iDb);
658 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
dan2bd93512009-08-31 08:22:46 +0000659 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
danielk19774adee202004-05-08 08:23:19 +0000660 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000661 return;
drhe5f9c642003-01-13 23:27:31 +0000662 }
drhed6c8672003-01-12 18:02:16 +0000663 }
drhe5f9c642003-01-13 23:27:31 +0000664#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000665
drhf0f258b2003-04-21 18:48:45 +0000666 /* Generate code to destroy the database record of the trigger.
667 */
drh43617e92006-03-06 20:55:46 +0000668 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drh8c8dddc2015-12-09 16:26:38 +0000669 sqlite3NestedParse(pParse,
drh346a70c2020-06-15 20:27:35 +0000670 "DELETE FROM %Q." DFLT_SCHEMA_TABLE " WHERE name=%Q AND type='trigger'",
671 db->aDb[iDb].zDbSName, pTrigger->zName
drh8c8dddc2015-12-09 16:26:38 +0000672 );
drh9cbf3422008-01-17 16:22:13 +0000673 sqlite3ChangeCookie(pParse, iDb);
dan165921a2009-08-28 18:53:45 +0000674 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
drhf0f258b2003-04-21 18:48:45 +0000675 }
drh956bc922004-07-24 17:38:29 +0000676}
drhf0f258b2003-04-21 18:48:45 +0000677
drh956bc922004-07-24 17:38:29 +0000678/*
679** Remove a trigger from the hash tables of the sqlite* pointer.
680*/
681void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
682 Trigger *pTrigger;
drh21206082011-04-04 18:22:02 +0000683 Hash *pHash;
684
685 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
686 pHash = &(db->aDb[iDb].pSchema->trigHash);
drhacbcb7e2014-08-21 20:26:37 +0000687 pTrigger = sqlite3HashInsert(pHash, zName, 0);
drh9ab4c2e2009-05-09 00:18:38 +0000688 if( ALWAYS(pTrigger) ){
danielk19772f886d12009-02-28 10:47:41 +0000689 if( pTrigger->pSchema==pTrigger->pTabSchema ){
690 Table *pTab = tableOfTrigger(pTrigger);
drh6397a782019-08-27 10:05:45 +0000691 if( pTab ){
692 Trigger **pp;
dan0232dad2019-12-03 03:34:06 +0000693 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
694 if( *pp==pTrigger ){
695 *pp = (*pp)->pNext;
696 break;
697 }
698 }
drh6397a782019-08-27 10:05:45 +0000699 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000700 }
drh633e6d52008-07-28 19:34:53 +0000701 sqlite3DeleteTrigger(db, pTrigger);
drh8257aa82017-07-26 19:59:13 +0000702 db->mDbFlags |= DBFLAG_SchemaChange;
danielk1977c3f9bad2002-05-15 08:30:12 +0000703 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000704}
705
drhc977f7f2002-05-21 11:38:11 +0000706/*
707** pEList is the SET clause of an UPDATE statement. Each entry
708** in pEList is of the format <id>=<expr>. If any of the entries
709** in pEList have an <id> which matches an identifier in pIdList,
710** then return TRUE. If pIdList==NULL, then it is considered a
711** wildcard that matches anything. Likewise if pEList==NULL then
712** it matches anything so always return true. Return false only
713** if there is no match.
714*/
drh9ab4c2e2009-05-09 00:18:38 +0000715static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000716 int e;
drh9ab4c2e2009-05-09 00:18:38 +0000717 if( pIdList==0 || NEVER(pEList==0) ) return 1;
drhad2d8302002-05-24 20:31:36 +0000718 for(e=0; e<pEList->nExpr; e++){
drh41cee662019-12-12 20:22:34 +0000719 if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000720 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000721 return 0;
722}
723
danielk1977c3f9bad2002-05-15 08:30:12 +0000724/*
danielk19772f886d12009-02-28 10:47:41 +0000725** Return a list of all triggers on table pTab if there exists at least
726** one trigger that must be fired when an operation of type 'op' is
727** performed on the table, and, if that operation is an UPDATE, if at
728** least one of the columns in pChanges is being modified.
drhdca76842004-12-07 14:06:13 +0000729*/
danielk19772f886d12009-02-28 10:47:41 +0000730Trigger *sqlite3TriggersExist(
731 Parse *pParse, /* Parse context */
drhdca76842004-12-07 14:06:13 +0000732 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000733 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
danielk19772f886d12009-02-28 10:47:41 +0000734 ExprList *pChanges, /* Columns that change in an UPDATE statement */
735 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
drhc977f7f2002-05-21 11:38:11 +0000736){
drhdca76842004-12-07 14:06:13 +0000737 int mask = 0;
drhe83cafd2011-03-21 17:15:58 +0000738 Trigger *pList = 0;
danielk19772f886d12009-02-28 10:47:41 +0000739 Trigger *p;
drhe83cafd2011-03-21 17:15:58 +0000740
drh2aa41c82021-02-03 00:55:34 +0000741 pList = sqlite3TriggerList(pParse, pTab);
drhc9be8632021-02-02 00:16:15 +0000742 assert( pList==0 || IsVirtual(pTab)==0
743 || (pList->bReturning && pList->pNext==0) );
drh2aa41c82021-02-03 00:55:34 +0000744 if( pList!=0 ){
745 p = pList;
746 if( (pParse->db->flags & SQLITE_EnableTrigger)==0
747 && pTab->pTrigger!=0
748 ){
749 /* The SQLITE_DBCONFIG_ENABLE_TRIGGER setting is off. That means that
750 ** only TEMP triggers are allowed. Truncate the pList so that it
751 ** includes only TEMP triggers */
752 if( pList==pTab->pTrigger ){
753 pList = 0;
754 goto exit_triggers_exist;
drh709dd132021-02-02 12:01:22 +0000755 }
drh2aa41c82021-02-03 00:55:34 +0000756 while( ALWAYS(p->pNext) && p->pNext!=pTab->pTrigger ) p = p->pNext;
757 p->pNext = 0;
758 p = pList;
danielk1977c3f9bad2002-05-15 08:30:12 +0000759 }
drh2aa41c82021-02-03 00:55:34 +0000760 do{
761 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
762 mask |= p->tr_tm;
763 }else if( p->op==TK_RETURNING ){
764 /* The first time a RETURNING trigger is seen, the "op" value tells
765 ** us what time of trigger it should be. */
766 assert( sqlite3IsToplevel(pParse) );
767 p->op = op;
drh7dec8042021-02-04 22:59:19 +0000768 if( IsVirtual(pTab) ){
769 if( op!=TK_INSERT ){
770 sqlite3ErrorMsg(pParse,
771 "%s RETURNING is not available on virtual tables",
772 op==TK_DELETE ? "DELETE" : "UPDATE");
773 }
774 p->tr_tm = TRIGGER_BEFORE;
775 }else{
776 p->tr_tm = TRIGGER_AFTER;
drh2aa41c82021-02-03 00:55:34 +0000777 }
drh7dec8042021-02-04 22:59:19 +0000778 mask |= p->tr_tm;
drh2aa41c82021-02-03 00:55:34 +0000779 }else if( p->bReturning && p->op==TK_INSERT && op==TK_UPDATE
780 && sqlite3IsToplevel(pParse) ){
781 /* Also fire a RETURNING trigger for an UPSERT */
drh7dec8042021-02-04 22:59:19 +0000782 mask |= p->tr_tm;
drh2aa41c82021-02-03 00:55:34 +0000783 }
784 p = p->pNext;
785 }while( p );
danielk1977c3f9bad2002-05-15 08:30:12 +0000786 }
drh2aa41c82021-02-03 00:55:34 +0000787exit_triggers_exist:
danielk19772f886d12009-02-28 10:47:41 +0000788 if( pMask ){
789 *pMask = mask;
790 }
791 return (mask ? pList : 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000792}
793
drhc977f7f2002-05-21 11:38:11 +0000794/*
dan46408352015-04-21 16:38:49 +0000795** Convert the pStep->zTarget string into a SrcList and return a pointer
drhf26e09c2003-05-31 16:21:12 +0000796** to that SrcList.
797**
798** This routine adds a specific database name, if needed, to the target when
799** forming the SrcList. This prevents a trigger in one database from
800** referring to a target in another database. An exception is when the
801** trigger is in TEMP in which case it can refer to any other database it
802** wants.
803*/
dane7877b22020-07-14 19:51:01 +0000804SrcList *sqlite3TriggerStepSrc(
drhf26e09c2003-05-31 16:21:12 +0000805 Parse *pParse, /* The parsing context */
806 TriggerStep *pStep /* The trigger containing the target token */
807){
dan46408352015-04-21 16:38:49 +0000808 sqlite3 *db = pParse->db;
dane7877b22020-07-14 19:51:01 +0000809 SrcList *pSrc; /* SrcList to be returned */
810 char *zName = sqlite3DbStrDup(db, pStep->zTarget);
drh29c992c2019-01-17 15:40:41 +0000811 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
dane7877b22020-07-14 19:51:01 +0000812 assert( pSrc==0 || pSrc->nSrc==1 );
813 assert( zName || pSrc==0 );
drhb7916a72009-05-27 10:31:29 +0000814 if( pSrc ){
dane7877b22020-07-14 19:51:01 +0000815 Schema *pSchema = pStep->pTrig->pSchema;
816 pSrc->a[0].zName = zName;
817 if( pSchema!=db->aDb[1].pSchema ){
818 pSrc->a[0].pSchema = pSchema;
drhb7916a72009-05-27 10:31:29 +0000819 }
dane7877b22020-07-14 19:51:01 +0000820 if( pStep->pFrom ){
821 SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0);
822 pSrc = sqlite3SrcListAppendList(pParse, pSrc, pDup);
823 }
824 }else{
825 sqlite3DbFree(db, zName);
drhf26e09c2003-05-31 16:21:12 +0000826 }
827 return pSrc;
828}
829
drh0d23f672021-03-30 01:52:21 +0000830/*
831** Return true if the pExpr term from the RETURNING clause argument
832** list is of the form "*". Raise an error if the terms if of the
833** form "table.*".
834*/
835static int isAsteriskTerm(
836 Parse *pParse, /* Parsing context */
837 Expr *pTerm /* A term in the RETURNING clause */
838){
839 assert( pTerm!=0 );
840 if( pTerm->op==TK_ASTERISK ) return 1;
841 if( pTerm->op!=TK_DOT ) return 0;
842 assert( pTerm->pRight!=0 );
843 assert( pTerm->pLeft!=0 );
844 if( pTerm->pRight->op!=TK_ASTERISK ) return 0;
845 sqlite3ErrorMsg(pParse, "RETURNING may not use \"TABLE.*\" wildcards");
846 return 1;
847}
848
drhdac9a5f2021-01-29 21:18:46 +0000849/* The input list pList is the list of result set terms from a RETURNING
850** clause. The table that we are returning from is pTab.
851**
852** This routine makes a copy of the pList, and at the same time expands
853** any "*" wildcards to be the complete set of columns from pTab.
854*/
855static ExprList *sqlite3ExpandReturning(
856 Parse *pParse, /* Parsing context */
857 ExprList *pList, /* The arguments to RETURNING */
858 Table *pTab /* The table being updated */
859){
860 ExprList *pNew = 0;
861 sqlite3 *db = pParse->db;
862 int i;
drh552562c2021-02-04 20:52:20 +0000863
drhdac9a5f2021-01-29 21:18:46 +0000864 for(i=0; i<pList->nExpr; i++){
865 Expr *pOldExpr = pList->a[i].pExpr;
drh0d23f672021-03-30 01:52:21 +0000866 if( NEVER(pOldExpr==0) ) continue;
867 if( isAsteriskTerm(pParse, pOldExpr) ){
drhc9be8632021-02-02 00:16:15 +0000868 int jj;
869 for(jj=0; jj<pTab->nCol; jj++){
drh87296422021-02-07 12:59:43 +0000870 Expr *pNewExpr;
drhc9be8632021-02-02 00:16:15 +0000871 if( IsHiddenColumn(pTab->aCol+jj) ) continue;
drh87296422021-02-07 12:59:43 +0000872 pNewExpr = sqlite3Expr(db, TK_ID, pTab->aCol[jj].zName);
drhdac9a5f2021-01-29 21:18:46 +0000873 pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr);
874 if( !db->mallocFailed ){
875 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
drhc9be8632021-02-02 00:16:15 +0000876 pItem->zEName = sqlite3DbStrDup(db, pTab->aCol[jj].zName);
drhdac9a5f2021-01-29 21:18:46 +0000877 pItem->eEName = ENAME_NAME;
878 }
879 }
880 }else{
881 Expr *pNewExpr = sqlite3ExprDup(db, pOldExpr, 0);
882 pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr);
drh9407b6e2021-01-31 16:45:10 +0000883 if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0) ){
drhdac9a5f2021-01-29 21:18:46 +0000884 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
885 pItem->zEName = sqlite3DbStrDup(db, pList->a[i].zEName);
886 pItem->eEName = pList->a[i].eEName;
887 }
888 }
889 }
drh29f6a362021-02-05 17:34:47 +0000890 if( !db->mallocFailed ){
drh552562c2021-02-04 20:52:20 +0000891 Vdbe *v = pParse->pVdbe;
892 assert( v!=0 );
893 sqlite3VdbeSetNumCols(v, pNew->nExpr);
894 for(i=0; i<pNew->nExpr; i++){
895 sqlite3VdbeSetColName(v, i, COLNAME_NAME, pNew->a[i].zEName,
896 SQLITE_TRANSIENT);
897 }
898 }
drhdac9a5f2021-01-29 21:18:46 +0000899 return pNew;
900}
901
drhf26e09c2003-05-31 16:21:12 +0000902/*
drh381bdac2021-02-04 17:29:04 +0000903** Generate code for the RETURNING trigger. Unlike other triggers
904** that invoke a subprogram in the bytecode, the code for RETURNING
905** is generated in-line.
906*/
907static void codeReturningTrigger(
908 Parse *pParse, /* Parse context */
909 Trigger *pTrigger, /* The trigger step that defines the RETURNING */
910 Table *pTab, /* The table to code triggers from */
drh552562c2021-02-04 20:52:20 +0000911 int regIn /* The first in an array of registers */
drh381bdac2021-02-04 17:29:04 +0000912){
913 Vdbe *v = pParse->pVdbe;
914 ExprList *pNew;
915 Returning *pReturning;
916
917 assert( v!=0 );
918 assert( pParse->bReturning );
919 pReturning = pParse->u1.pReturning;
920 assert( pTrigger == &(pReturning->retTrig) );
drh381bdac2021-02-04 17:29:04 +0000921 pNew = sqlite3ExpandReturning(pParse, pReturning->pReturnEL, pTab);
922 if( pNew ){
drh552562c2021-02-04 20:52:20 +0000923 NameContext sNC;
924 memset(&sNC, 0, sizeof(sNC));
drhe1db9342021-02-04 21:17:12 +0000925 if( pReturning->nRetCol==0 ){
926 pReturning->nRetCol = pNew->nExpr;
927 pReturning->iRetCur = pParse->nTab++;
928 }
drh552562c2021-02-04 20:52:20 +0000929 sNC.pParse = pParse;
930 sNC.uNC.iBaseReg = regIn;
931 sNC.ncFlags = NC_UBaseReg;
932 pParse->eTriggerOp = pTrigger->op;
933 pParse->pTriggerTab = pTab;
934 if( sqlite3ResolveExprListNames(&sNC, pNew)==SQLITE_OK ){
935 int i;
936 int nCol = pNew->nExpr;
937 int reg = pParse->nMem+1;
938 pParse->nMem += nCol+2;
939 pReturning->iRetReg = reg;
940 for(i=0; i<nCol; i++){
941 sqlite3ExprCodeFactorable(pParse, pNew->a[i].pExpr, reg+i);
drh381bdac2021-02-04 17:29:04 +0000942 }
drh552562c2021-02-04 20:52:20 +0000943 sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, i, reg+i);
944 sqlite3VdbeAddOp2(v, OP_NewRowid, pReturning->iRetCur, reg+i+1);
945 sqlite3VdbeAddOp3(v, OP_Insert, pReturning->iRetCur, reg+i, reg+i+1);
946 }
947 sqlite3ExprListDelete(pParse->db, pNew);
948 pParse->eTriggerOp = 0;
949 pParse->pTriggerTab = 0;
950 }
drh381bdac2021-02-04 17:29:04 +0000951}
952
953
954
955/*
dan65a7cd12009-09-01 12:16:01 +0000956** Generate VDBE code for the statements inside the body of a single
957** trigger.
drhc977f7f2002-05-21 11:38:11 +0000958*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000959static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000960 Parse *pParse, /* The parser context */
961 TriggerStep *pStepList, /* List of statements inside the trigger body */
dan65a7cd12009-09-01 12:16:01 +0000962 int orconf /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000963){
dan65a7cd12009-09-01 12:16:01 +0000964 TriggerStep *pStep;
drh344737f2004-09-19 00:50:20 +0000965 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000966 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000967
dan65a7cd12009-09-01 12:16:01 +0000968 assert( pParse->pTriggerTab && pParse->pToplevel );
969 assert( pStepList );
drh344737f2004-09-19 00:50:20 +0000970 assert( v!=0 );
dan65a7cd12009-09-01 12:16:01 +0000971 for(pStep=pStepList; pStep; pStep=pStep->pNext){
dan165921a2009-08-28 18:53:45 +0000972 /* Figure out the ON CONFLICT policy that will be used for this step
973 ** of the trigger program. If the statement that caused this trigger
974 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
975 ** the ON CONFLICT policy that was specified as part of the trigger
976 ** step statement. Example:
977 **
978 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
979 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
980 ** END;
981 **
982 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
983 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
984 */
shanecea72b22009-09-07 04:38:36 +0000985 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
drhaceb31b2014-02-08 01:40:27 +0000986 assert( pParse->okConstFactor==0 );
danf78baaf2012-12-06 19:37:22 +0000987
drhf259df52017-12-27 20:38:35 +0000988#ifndef SQLITE_OMIT_TRACE
drhe307e112017-12-27 21:30:34 +0000989 if( pStep->zSpan ){
990 sqlite3VdbeAddOp4(v, OP_Trace, 0x7fffffff, 1, 0,
991 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
992 P4_DYNAMIC);
993 }
drhf259df52017-12-27 20:38:35 +0000994#endif
995
dan165921a2009-08-28 18:53:45 +0000996 switch( pStep->op ){
danielk1977633ed082002-05-17 00:05:58 +0000997 case TK_UPDATE: {
dan165921a2009-08-28 18:53:45 +0000998 sqlite3Update(pParse,
dane7877b22020-07-14 19:51:01 +0000999 sqlite3TriggerStepSrc(pParse, pStep),
dan165921a2009-08-28 18:53:45 +00001000 sqlite3ExprListDup(db, pStep->pExprList, 0),
1001 sqlite3ExprDup(db, pStep->pWhere, 0),
drheac9fab2018-04-16 13:00:50 +00001002 pParse->eOrconf, 0, 0, 0
dan165921a2009-08-28 18:53:45 +00001003 );
drhdac9a5f2021-01-29 21:18:46 +00001004 sqlite3VdbeAddOp0(v, OP_ResetCount);
danielk1977633ed082002-05-17 00:05:58 +00001005 break;
1006 }
1007 case TK_INSERT: {
dan165921a2009-08-28 18:53:45 +00001008 sqlite3Insert(pParse,
dane7877b22020-07-14 19:51:01 +00001009 sqlite3TriggerStepSrc(pParse, pStep),
dan165921a2009-08-28 18:53:45 +00001010 sqlite3SelectDup(db, pStep->pSelect, 0),
1011 sqlite3IdListDup(db, pStep->pIdList),
drh46d2e5c2018-04-12 13:15:43 +00001012 pParse->eOrconf,
1013 sqlite3UpsertDup(db, pStep->pUpsert)
dan165921a2009-08-28 18:53:45 +00001014 );
drhdac9a5f2021-01-29 21:18:46 +00001015 sqlite3VdbeAddOp0(v, OP_ResetCount);
danielk1977633ed082002-05-17 00:05:58 +00001016 break;
1017 }
1018 case TK_DELETE: {
dan165921a2009-08-28 18:53:45 +00001019 sqlite3DeleteFrom(pParse,
dane7877b22020-07-14 19:51:01 +00001020 sqlite3TriggerStepSrc(pParse, pStep),
drh8c0833f2017-11-14 23:48:23 +00001021 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
dan165921a2009-08-28 18:53:45 +00001022 );
drhdac9a5f2021-01-29 21:18:46 +00001023 sqlite3VdbeAddOp0(v, OP_ResetCount);
danielk1977633ed082002-05-17 00:05:58 +00001024 break;
1025 }
drh381bdac2021-02-04 17:29:04 +00001026 default: assert( pStep->op==TK_SELECT ); {
dan165921a2009-08-28 18:53:45 +00001027 SelectDest sDest;
1028 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
drhdac9a5f2021-01-29 21:18:46 +00001029 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
dan165921a2009-08-28 18:53:45 +00001030 sqlite3Select(pParse, pSelect, &sDest);
1031 sqlite3SelectDelete(db, pSelect);
drh9ab4c2e2009-05-09 00:18:38 +00001032 break;
1033 }
danielk1977633ed082002-05-17 00:05:58 +00001034 }
danielk1977633ed082002-05-17 00:05:58 +00001035 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001036
danielk1977633ed082002-05-17 00:05:58 +00001037 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +00001038}
1039
drhc7379ce2013-10-30 02:28:23 +00001040#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
dan165921a2009-08-28 18:53:45 +00001041/*
1042** This function is used to add VdbeComment() annotations to a VDBE
1043** program. It is not used in production code, only for debugging.
1044*/
1045static const char *onErrorText(int onError){
1046 switch( onError ){
1047 case OE_Abort: return "abort";
1048 case OE_Rollback: return "rollback";
1049 case OE_Fail: return "fail";
1050 case OE_Replace: return "replace";
1051 case OE_Ignore: return "ignore";
1052 case OE_Default: return "default";
1053 }
1054 return "n/a";
1055}
1056#endif
1057
1058/*
1059** Parse context structure pFrom has just been used to create a sub-vdbe
1060** (trigger program). If an error has occurred, transfer error information
1061** from pFrom to pTo.
1062*/
1063static void transferParseError(Parse *pTo, Parse *pFrom){
1064 assert( pFrom->zErrMsg==0 || pFrom->nErr );
1065 assert( pTo->zErrMsg==0 || pTo->nErr );
1066 if( pTo->nErr==0 ){
1067 pTo->zErrMsg = pFrom->zErrMsg;
1068 pTo->nErr = pFrom->nErr;
drhe06874e2015-04-16 15:47:06 +00001069 pTo->rc = pFrom->rc;
dan165921a2009-08-28 18:53:45 +00001070 }else{
1071 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
1072 }
1073}
1074
dan65a7cd12009-09-01 12:16:01 +00001075/*
1076** Create and populate a new TriggerPrg object with a sub-program
1077** implementing trigger pTrigger with ON CONFLICT policy orconf.
1078*/
dan2832ad42009-08-31 15:27:27 +00001079static TriggerPrg *codeRowTrigger(
dan165921a2009-08-28 18:53:45 +00001080 Parse *pParse, /* Current parse context */
1081 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +00001082 Table *pTab, /* The table pTrigger is attached to */
1083 int orconf /* ON CONFLICT policy to code trigger program with */
dan165921a2009-08-28 18:53:45 +00001084){
dan65a7cd12009-09-01 12:16:01 +00001085 Parse *pTop = sqlite3ParseToplevel(pParse);
1086 sqlite3 *db = pParse->db; /* Database handle */
1087 TriggerPrg *pPrg; /* Value to return */
dan165921a2009-08-28 18:53:45 +00001088 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
1089 Vdbe *v; /* Temporary VM */
dan165921a2009-08-28 18:53:45 +00001090 NameContext sNC; /* Name context for sub-vdbe */
1091 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
1092 Parse *pSubParse; /* Parse context for sub-vdbe */
1093 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
1094
dan1da40a32009-09-19 17:00:31 +00001095 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dand19c9332010-07-26 12:05:17 +00001096 assert( pTop->pVdbe );
dan65a7cd12009-09-01 12:16:01 +00001097
1098 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
1099 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
1100 ** list of the top-level Parse object sooner rather than later. */
dan2832ad42009-08-31 15:27:27 +00001101 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
1102 if( !pPrg ) return 0;
dan65a7cd12009-09-01 12:16:01 +00001103 pPrg->pNext = pTop->pTriggerPrg;
1104 pTop->pTriggerPrg = pPrg;
dan2832ad42009-08-31 15:27:27 +00001105 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
dan165921a2009-08-28 18:53:45 +00001106 if( !pProgram ) return 0;
dand19c9332010-07-26 12:05:17 +00001107 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
dan2832ad42009-08-31 15:27:27 +00001108 pPrg->pTrigger = pTrigger;
1109 pPrg->orconf = orconf;
danbb5f1682009-11-27 12:12:34 +00001110 pPrg->aColmask[0] = 0xffffffff;
1111 pPrg->aColmask[1] = 0xffffffff;
dan165921a2009-08-28 18:53:45 +00001112
dan65a7cd12009-09-01 12:16:01 +00001113 /* Allocate and populate a new Parse context to use for coding the
1114 ** trigger sub-program. */
1115 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
1116 if( !pSubParse ) return 0;
dan165921a2009-08-28 18:53:45 +00001117 memset(&sNC, 0, sizeof(sNC));
1118 sNC.pParse = pSubParse;
1119 pSubParse->db = db;
1120 pSubParse->pTriggerTab = pTab;
dan65a7cd12009-09-01 12:16:01 +00001121 pSubParse->pToplevel = pTop;
dan2bd93512009-08-31 08:22:46 +00001122 pSubParse->zAuthContext = pTrigger->zName;
dan65a7cd12009-09-01 12:16:01 +00001123 pSubParse->eTriggerOp = pTrigger->op;
drh8b307fb2010-04-06 15:57:05 +00001124 pSubParse->nQueryLoop = pParse->nQueryLoop;
dan1ea04432018-12-21 19:29:11 +00001125 pSubParse->disableVtab = pParse->disableVtab;
dan165921a2009-08-28 18:53:45 +00001126
1127 v = sqlite3GetVdbe(pSubParse);
1128 if( v ){
dan76d462e2009-08-30 11:42:51 +00001129 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
1130 pTrigger->zName, onErrorText(orconf),
dan165921a2009-08-28 18:53:45 +00001131 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
dan65a7cd12009-09-01 12:16:01 +00001132 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
1133 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
1134 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
dan76d462e2009-08-30 11:42:51 +00001135 pTab->zName
dan165921a2009-08-28 18:53:45 +00001136 ));
dan76d462e2009-08-30 11:42:51 +00001137#ifndef SQLITE_OMIT_TRACE
drhe307e112017-12-27 21:30:34 +00001138 if( pTrigger->zName ){
1139 sqlite3VdbeChangeP4(v, -1,
1140 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
1141 );
1142 }
dan76d462e2009-08-30 11:42:51 +00001143#endif
dan165921a2009-08-28 18:53:45 +00001144
dan65a7cd12009-09-01 12:16:01 +00001145 /* If one was specified, code the WHEN clause. If it evaluates to false
1146 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
1147 ** OP_Halt inserted at the end of the program. */
dan165921a2009-08-28 18:53:45 +00001148 if( pTrigger->pWhen ){
dan165921a2009-08-28 18:53:45 +00001149 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
drh4c4a2572021-04-06 12:50:24 +00001150 if( db->mallocFailed==0
1151 && SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
dan523a0872009-08-31 05:23:32 +00001152 ){
drhec4ccdb2018-12-29 02:26:59 +00001153 iEndTrigger = sqlite3VdbeMakeLabel(pSubParse);
dan165921a2009-08-28 18:53:45 +00001154 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
1155 }
1156 sqlite3ExprDelete(db, pWhen);
1157 }
1158
1159 /* Code the trigger program into the sub-vdbe. */
dan76d462e2009-08-30 11:42:51 +00001160 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
dan65a7cd12009-09-01 12:16:01 +00001161
1162 /* Insert an OP_Halt at the end of the sub-program. */
dan165921a2009-08-28 18:53:45 +00001163 if( iEndTrigger ){
1164 sqlite3VdbeResolveLabel(v, iEndTrigger);
1165 }
1166 sqlite3VdbeAddOp0(v, OP_Halt);
dan76d462e2009-08-30 11:42:51 +00001167 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
1168
dan165921a2009-08-28 18:53:45 +00001169 transferParseError(pParse, pSubParse);
dan08951172017-11-28 20:43:40 +00001170 if( db->mallocFailed==0 && pParse->nErr==0 ){
dan65a7cd12009-09-01 12:16:01 +00001171 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
dan523a0872009-08-31 05:23:32 +00001172 }
dan165921a2009-08-28 18:53:45 +00001173 pProgram->nMem = pSubParse->nMem;
1174 pProgram->nCsr = pSubParse->nTab;
1175 pProgram->token = (void *)pTrigger;
danbb5f1682009-11-27 12:12:34 +00001176 pPrg->aColmask[0] = pSubParse->oldmask;
1177 pPrg->aColmask[1] = pSubParse->newmask;
dan165921a2009-08-28 18:53:45 +00001178 sqlite3VdbeDelete(v);
dan165921a2009-08-28 18:53:45 +00001179 }
dan65a7cd12009-09-01 12:16:01 +00001180
dan65a7cd12009-09-01 12:16:01 +00001181 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
drhf30a9692013-11-15 01:10:18 +00001182 sqlite3ParserReset(pSubParse);
dan165921a2009-08-28 18:53:45 +00001183 sqlite3StackFree(db, pSubParse);
1184
dan2832ad42009-08-31 15:27:27 +00001185 return pPrg;
dan165921a2009-08-28 18:53:45 +00001186}
1187
dan65a7cd12009-09-01 12:16:01 +00001188/*
1189** Return a pointer to a TriggerPrg object containing the sub-program for
1190** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
1191** TriggerPrg object exists, a new object is allocated and populated before
1192** being returned.
1193*/
dan2832ad42009-08-31 15:27:27 +00001194static TriggerPrg *getRowTrigger(
dan65a7cd12009-09-01 12:16:01 +00001195 Parse *pParse, /* Current parse context */
dan165921a2009-08-28 18:53:45 +00001196 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +00001197 Table *pTab, /* The table trigger pTrigger is attached to */
1198 int orconf /* ON CONFLICT algorithm. */
dan165921a2009-08-28 18:53:45 +00001199){
dan65a7cd12009-09-01 12:16:01 +00001200 Parse *pRoot = sqlite3ParseToplevel(pParse);
dan2832ad42009-08-31 15:27:27 +00001201 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +00001202
dan1da40a32009-09-19 17:00:31 +00001203 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dan165921a2009-08-28 18:53:45 +00001204
1205 /* It may be that this trigger has already been coded (or is in the
1206 ** process of being coded). If this is the case, then an entry with
dan2832ad42009-08-31 15:27:27 +00001207 ** a matching TriggerPrg.pTrigger field will be present somewhere
1208 ** in the Parse.pTriggerPrg list. Search for such an entry. */
dan2832ad42009-08-31 15:27:27 +00001209 for(pPrg=pRoot->pTriggerPrg;
1210 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
1211 pPrg=pPrg->pNext
dan165921a2009-08-28 18:53:45 +00001212 );
1213
dan65a7cd12009-09-01 12:16:01 +00001214 /* If an existing TriggerPrg could not be located, create a new one. */
dan2832ad42009-08-31 15:27:27 +00001215 if( !pPrg ){
dan65a7cd12009-09-01 12:16:01 +00001216 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
dan165921a2009-08-28 18:53:45 +00001217 }
1218
dan2832ad42009-08-31 15:27:27 +00001219 return pPrg;
dan165921a2009-08-28 18:53:45 +00001220}
1221
dan94d7f502009-09-24 09:05:49 +00001222/*
1223** Generate code for the trigger program associated with trigger p on
1224** table pTab. The reg, orconf and ignoreJump parameters passed to this
1225** function are the same as those described in the header function for
1226** sqlite3CodeRowTrigger()
1227*/
dan1da40a32009-09-19 17:00:31 +00001228void sqlite3CodeRowTriggerDirect(
1229 Parse *pParse, /* Parse context */
1230 Trigger *p, /* Trigger to code */
1231 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001232 int reg, /* Reg array containing OLD.* and NEW.* values */
dan1da40a32009-09-19 17:00:31 +00001233 int orconf, /* ON CONFLICT policy */
1234 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
1235){
1236 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
1237 TriggerPrg *pPrg;
1238 pPrg = getRowTrigger(pParse, p, pTab, orconf);
1239 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
1240
1241 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
1242 ** is a pointer to the sub-vdbe containing the trigger program. */
1243 if( pPrg ){
dand19c9332010-07-26 12:05:17 +00001244 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
1245
drh9b34abe2016-01-16 15:12:35 +00001246 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
1247 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
dan1da40a32009-09-19 17:00:31 +00001248 VdbeComment(
1249 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
1250
1251 /* Set the P5 operand of the OP_Program instruction to non-zero if
1252 ** recursive invocation of this trigger program is disallowed. Recursive
1253 ** invocation is disallowed if (a) the sub-program is really a trigger,
1254 ** not a foreign key action, and (b) the flag to enable recursive triggers
1255 ** is clear. */
dand19c9332010-07-26 12:05:17 +00001256 sqlite3VdbeChangeP5(v, (u8)bRecursive);
dan1da40a32009-09-19 17:00:31 +00001257 }
1258}
1259
danielk1977633ed082002-05-17 00:05:58 +00001260/*
dan94d7f502009-09-24 09:05:49 +00001261** This is called to code the required FOR EACH ROW triggers for an operation
1262** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
drhf7b54962013-05-28 12:11:54 +00001263** is given by the op parameter. The tr_tm parameter determines whether the
dan94d7f502009-09-24 09:05:49 +00001264** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
1265** parameter pChanges is passed the list of columns being modified.
danielk1977633ed082002-05-17 00:05:58 +00001266**
dan94d7f502009-09-24 09:05:49 +00001267** If there are no triggers that fire at the specified time for the specified
1268** operation on pTab, this function is a no-op.
drhc977f7f2002-05-21 11:38:11 +00001269**
dan94d7f502009-09-24 09:05:49 +00001270** The reg argument is the address of the first in an array of registers
1271** that contain the values substituted for the new.* and old.* references
1272** in the trigger program. If N is the number of columns in table pTab
1273** (a copy of pTab->nCol), then registers are populated as follows:
drhc977f7f2002-05-21 11:38:11 +00001274**
dan94d7f502009-09-24 09:05:49 +00001275** Register Contains
1276** ------------------------------------------------------
1277** reg+0 OLD.rowid
1278** reg+1 OLD.* value of left-most column of pTab
1279** ... ...
1280** reg+N OLD.* value of right-most column of pTab
1281** reg+N+1 NEW.rowid
drh381bdac2021-02-04 17:29:04 +00001282** reg+N+2 NEW.* value of left-most column of pTab
dan94d7f502009-09-24 09:05:49 +00001283** ... ...
1284** reg+N+N+1 NEW.* value of right-most column of pTab
drhc977f7f2002-05-21 11:38:11 +00001285**
dan94d7f502009-09-24 09:05:49 +00001286** For ON DELETE triggers, the registers containing the NEW.* values will
1287** never be accessed by the trigger program, so they are not allocated or
1288** populated by the caller (there is no data to populate them with anyway).
1289** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
1290** are never accessed, and so are not allocated by the caller. So, for an
1291** ON INSERT trigger, the value passed to this function as parameter reg
1292** is not a readable register, although registers (reg+N) through
1293** (reg+N+N+1) are.
danielk1977633ed082002-05-17 00:05:58 +00001294**
dan94d7f502009-09-24 09:05:49 +00001295** Parameter orconf is the default conflict resolution algorithm for the
1296** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
1297** is the instruction that control should jump to if a trigger program
1298** raises an IGNORE exception.
danielk1977633ed082002-05-17 00:05:58 +00001299*/
dan165921a2009-08-28 18:53:45 +00001300void sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +00001301 Parse *pParse, /* Parse context */
danielk19772f886d12009-02-28 10:47:41 +00001302 Trigger *pTrigger, /* List of triggers on table pTab */
danielk1977633ed082002-05-17 00:05:58 +00001303 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
1304 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +00001305 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +00001306 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001307 int reg, /* The first in an array of registers (see above) */
danielk19776f349032002-06-11 02:25:40 +00001308 int orconf, /* ON CONFLICT policy */
dan165921a2009-08-28 18:53:45 +00001309 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
drhc977f7f2002-05-21 11:38:11 +00001310){
dan94d7f502009-09-24 09:05:49 +00001311 Trigger *p; /* Used to iterate through pTrigger list */
danielk19778f2c54e2008-01-01 19:02:09 +00001312
dan94d7f502009-09-24 09:05:49 +00001313 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
1314 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
1315 assert( (op==TK_UPDATE)==(pChanges!=0) );
danielk1977c3f9bad2002-05-15 08:30:12 +00001316
danielk19772f886d12009-02-28 10:47:41 +00001317 for(p=pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +00001318
drh9ab4c2e2009-05-09 00:18:38 +00001319 /* Sanity checking: The schema for the trigger and for the table are
1320 ** always defined. The trigger must be in the same schema as the table
1321 ** or else it must be a TEMP trigger. */
1322 assert( p->pSchema!=0 );
1323 assert( p->pTabSchema!=0 );
dan165921a2009-08-28 18:53:45 +00001324 assert( p->pSchema==p->pTabSchema
1325 || p->pSchema==pParse->db->aDb[1].pSchema );
drh9ab4c2e2009-05-09 00:18:38 +00001326
drh28828c52021-01-30 21:55:38 +00001327 /* Determine whether we should code this trigger. One of two choices:
1328 ** 1. The trigger is an exact match to the current DML statement
1329 ** 2. This is a RETURNING trigger for INSERT but we are currently
1330 ** doing the UPDATE part of an UPSERT.
1331 */
1332 if( (p->op==op || (p->bReturning && p->op==TK_INSERT && op==TK_UPDATE))
dan165921a2009-08-28 18:53:45 +00001333 && p->tr_tm==tr_tm
dan94d7f502009-09-24 09:05:49 +00001334 && checkColumnOverlap(p->pColumns, pChanges)
danielk1977eecfb3e2006-01-10 12:31:39 +00001335 ){
drh381bdac2021-02-04 17:29:04 +00001336 if( !p->bReturning ){
1337 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
1338 }else if( sqlite3IsToplevel(pParse) ){
1339 codeReturningTrigger(pParse, p, pTab, reg);
1340 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001341 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001342 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001343}
dan165921a2009-08-28 18:53:45 +00001344
dan2832ad42009-08-31 15:27:27 +00001345/*
danbb5f1682009-11-27 12:12:34 +00001346** Triggers may access values stored in the old.* or new.* pseudo-table.
1347** This function returns a 32-bit bitmask indicating which columns of the
1348** old.* or new.* tables actually are used by triggers. This information
1349** may be used by the caller, for example, to avoid having to load the entire
1350** old.* record into memory when executing an UPDATE or DELETE command.
dan2832ad42009-08-31 15:27:27 +00001351**
1352** Bit 0 of the returned mask is set if the left-most column of the
danbb5f1682009-11-27 12:12:34 +00001353** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
dan2832ad42009-08-31 15:27:27 +00001354** the second leftmost column value is required, and so on. If there
1355** are more than 32 columns in the table, and at least one of the columns
1356** with an index greater than 32 may be accessed, 0xffffffff is returned.
1357**
danbb5f1682009-11-27 12:12:34 +00001358** It is not possible to determine if the old.rowid or new.rowid column is
1359** accessed by triggers. The caller must always assume that it is.
dan2832ad42009-08-31 15:27:27 +00001360**
danbb5f1682009-11-27 12:12:34 +00001361** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
1362** applies to the old.* table. If 1, the new.* table.
1363**
1364** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
1365** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
1366** included in the returned mask if the TRIGGER_BEFORE bit is set in the
1367** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
1368** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
dan2832ad42009-08-31 15:27:27 +00001369*/
danbb5f1682009-11-27 12:12:34 +00001370u32 sqlite3TriggerColmask(
dan165921a2009-08-28 18:53:45 +00001371 Parse *pParse, /* Parse context */
1372 Trigger *pTrigger, /* List of triggers on table pTab */
dan165921a2009-08-28 18:53:45 +00001373 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
danbb5f1682009-11-27 12:12:34 +00001374 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
1375 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
dan165921a2009-08-28 18:53:45 +00001376 Table *pTab, /* The table to code triggers from */
dan2832ad42009-08-31 15:27:27 +00001377 int orconf /* Default ON CONFLICT policy for trigger steps */
dan165921a2009-08-28 18:53:45 +00001378){
dan1da40a32009-09-19 17:00:31 +00001379 const int op = pChanges ? TK_UPDATE : TK_DELETE;
dan2832ad42009-08-31 15:27:27 +00001380 u32 mask = 0;
dan165921a2009-08-28 18:53:45 +00001381 Trigger *p;
dan165921a2009-08-28 18:53:45 +00001382
danbb5f1682009-11-27 12:12:34 +00001383 assert( isNew==1 || isNew==0 );
dan165921a2009-08-28 18:53:45 +00001384 for(p=pTrigger; p; p=p->pNext){
drh381bdac2021-02-04 17:29:04 +00001385 if( p->op==op
1386 && (tr_tm&p->tr_tm)
danbb5f1682009-11-27 12:12:34 +00001387 && checkColumnOverlap(p->pColumns,pChanges)
1388 ){
drh381bdac2021-02-04 17:29:04 +00001389 if( p->bReturning ){
1390 mask = 0xffffffff;
1391 }else{
1392 TriggerPrg *pPrg;
1393 pPrg = getRowTrigger(pParse, p, pTab, orconf);
1394 if( pPrg ){
1395 mask |= pPrg->aColmask[isNew];
1396 }
dan165921a2009-08-28 18:53:45 +00001397 }
1398 }
1399 }
dan2832ad42009-08-31 15:27:27 +00001400
1401 return mask;
dan165921a2009-08-28 18:53:45 +00001402}
1403
drhb7f91642004-10-31 02:22:47 +00001404#endif /* !defined(SQLITE_OMIT_TRIGGER) */