blob: 0c8cf5334f204a2f0654142f6d684454841bd6ca [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){
51 Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
52 Trigger *pList = 0; /* List of triggers to return */
53
dand66c8302009-09-28 14:49:01 +000054 if( pParse->disableTriggers ){
55 return 0;
56 }
57
danielk19772f886d12009-02-28 10:47:41 +000058 if( pTmpSchema!=pTab->pSchema ){
59 HashElem *p;
drh21206082011-04-04 18:22:02 +000060 assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) );
danielk19772f886d12009-02-28 10:47:41 +000061 for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
62 Trigger *pTrig = (Trigger *)sqliteHashData(p);
63 if( pTrig->pTabSchema==pTab->pSchema
64 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
65 ){
66 pTrig->pNext = (pList ? pList : pTab->pTrigger);
67 pList = pTrig;
68 }
69 }
70 }
71
72 return (pList ? pList : pTab->pTrigger);
73}
74
75/*
drhf0f258b2003-04-21 18:48:45 +000076** This is called by the parser when it sees a CREATE TRIGGER statement
77** up to the point of the BEGIN before the trigger actions. A Trigger
78** structure is generated based on the information available and stored
79** in pParse->pNewTrigger. After the trigger actions have been parsed, the
danielk19774adee202004-05-08 08:23:19 +000080** sqlite3FinishTrigger() function is called to complete the trigger
drhf0f258b2003-04-21 18:48:45 +000081** construction process.
drh9adf9ac2002-05-15 11:44:13 +000082*/
danielk19774adee202004-05-08 08:23:19 +000083void sqlite3BeginTrigger(
drh9adf9ac2002-05-15 11:44:13 +000084 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
danielk1977ef2cb632004-05-29 02:37:19 +000085 Token *pName1, /* The name of the trigger */
86 Token *pName2, /* The name of the trigger */
drh5cf590c2003-04-24 01:45:04 +000087 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
drh9adf9ac2002-05-15 11:44:13 +000088 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
danielk1977633ed082002-05-17 00:05:58 +000089 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
drhd24cc422003-03-27 12:51:24 +000090 SrcList *pTableName,/* The name of the table/view the trigger applies to */
drh9adf9ac2002-05-15 11:44:13 +000091 Expr *pWhen, /* WHEN clause */
drhfdd48a72006-09-11 23:45:48 +000092 int isTemp, /* True if the TEMPORARY keyword is present */
93 int noErr /* Suppress errors if the trigger already exists */
drh9adf9ac2002-05-15 11:44:13 +000094){
drh3d5f74b2009-08-06 17:43:31 +000095 Trigger *pTrigger = 0; /* The new trigger */
96 Table *pTab; /* Table that the trigger fires off of */
drhf0f258b2003-04-21 18:48:45 +000097 char *zName = 0; /* Name of the trigger */
drh3d5f74b2009-08-06 17:43:31 +000098 sqlite3 *db = pParse->db; /* The database connection */
danielk1977ef2cb632004-05-29 02:37:19 +000099 int iDb; /* The database to store the trigger in */
100 Token *pName; /* The unqualified db name */
drh3d5f74b2009-08-06 17:43:31 +0000101 DbFixer sFix; /* State vector for the DB fixer */
drhed6c8672003-01-12 18:02:16 +0000102
drh43617e92006-03-06 20:55:46 +0000103 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
104 assert( pName2!=0 );
drhaa78bec2008-12-09 03:55:14 +0000105 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
106 assert( op>0 && op<0xff );
danielk1977ef2cb632004-05-29 02:37:19 +0000107 if( isTemp ){
108 /* If TEMP was specified, then the trigger name may not be qualified. */
drh43617e92006-03-06 20:55:46 +0000109 if( pName2->n>0 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000110 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
111 goto trigger_cleanup;
112 }
113 iDb = 1;
114 pName = pName1;
115 }else{
mistachkind5578432012-08-25 10:01:29 +0000116 /* Figure out the db that the trigger will be created in */
danielk1977ef2cb632004-05-29 02:37:19 +0000117 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
118 if( iDb<0 ){
119 goto trigger_cleanup;
120 }
121 }
drh6fea83e2011-07-01 13:50:44 +0000122 if( !pTableName || db->mallocFailed ){
123 goto trigger_cleanup;
124 }
125
126 /* A long-standing parser bug is that this syntax was allowed:
127 **
128 ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
129 ** ^^^^^^^^
130 **
131 ** To maintain backwards compatibility, ignore the database
drh346a70c2020-06-15 20:27:35 +0000132 ** name on pTableName if we are reparsing out of the schema table
drh6fea83e2011-07-01 13:50:44 +0000133 */
134 if( db->init.busy && iDb!=1 ){
135 sqlite3DbFree(db, pTableName->a[0].zDatabase);
136 pTableName->a[0].zDatabase = 0;
137 }
danielk1977ef2cb632004-05-29 02:37:19 +0000138
139 /* If the trigger name was unqualified, and the table is a temp table,
140 ** then set iDb to 1 to create the trigger in the temporary database.
141 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
142 ** exist, the error is caught by the block below.
drh9adf9ac2002-05-15 11:44:13 +0000143 */
danielk1977ef2cb632004-05-29 02:37:19 +0000144 pTab = sqlite3SrcListLookup(pParse, pTableName);
drh622d2882010-02-15 16:54:55 +0000145 if( db->init.busy==0 && pName2->n==0 && pTab
146 && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977ef2cb632004-05-29 02:37:19 +0000147 iDb = 1;
148 }
149
150 /* Ensure the table name matches database name and that the table exists */
drh17435752007-08-16 04:30:38 +0000151 if( db->mallocFailed ) goto trigger_cleanup;
drhd24cc422003-03-27 12:51:24 +0000152 assert( pTableName->nSrc==1 );
drhd100f692013-10-03 15:39:44 +0000153 sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
154 if( sqlite3FixSrcList(&sFix, pTableName) ){
drh4312db52003-06-03 01:47:11 +0000155 goto trigger_cleanup;
drhf26e09c2003-05-31 16:21:12 +0000156 }
danielk1977ef2cb632004-05-29 02:37:19 +0000157 pTab = sqlite3SrcListLookup(pParse, pTableName);
158 if( !pTab ){
159 /* The table does not exist. */
drh4e451aa2020-11-05 19:13:44 +0000160 goto trigger_orphan_error;
drhd24cc422003-03-27 12:51:24 +0000161 }
drh4cbdda92006-06-14 19:00:20 +0000162 if( IsVirtual(pTab) ){
163 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
drh4e451aa2020-11-05 19:13:44 +0000164 goto trigger_orphan_error;
drh4cbdda92006-06-14 19:00:20 +0000165 }
drhd24cc422003-03-27 12:51:24 +0000166
danielk1977d8123362004-06-12 09:25:12 +0000167 /* Check that the trigger name is not reserved and that no trigger of the
168 ** specified name exists */
drh17435752007-08-16 04:30:38 +0000169 zName = sqlite3NameFromToken(db, pName);
drhc5a93d42019-08-12 00:08:07 +0000170 if( zName==0 ){
171 assert( db->mallocFailed );
172 goto trigger_cleanup;
173 }
174 if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
danielk1977d8123362004-06-12 09:25:12 +0000175 goto trigger_cleanup;
176 }
drh21206082011-04-04 18:22:02 +0000177 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danc9461ec2018-08-29 21:00:16 +0000178 if( !IN_RENAME_OBJECT ){
dan5496d6a2018-08-13 17:14:26 +0000179 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
180 if( !noErr ){
181 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
182 }else{
183 assert( !db->init.busy );
184 sqlite3CodeVerifySchema(pParse, iDb);
185 }
186 goto trigger_cleanup;
drhfdd48a72006-09-11 23:45:48 +0000187 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000188 }
danielk1977ef2cb632004-05-29 02:37:19 +0000189
190 /* Do not create a trigger on a system table */
drhdca76842004-12-07 14:06:13 +0000191 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk19774adee202004-05-08 08:23:19 +0000192 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
drhd24cc422003-03-27 12:51:24 +0000193 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40 +0000194 }
danielk1977ef2cb632004-05-29 02:37:19 +0000195
196 /* INSTEAD of triggers are only for views and views only support INSTEAD
197 ** of triggers.
198 */
199 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000200 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drhda93d232003-03-31 02:12:46 +0000201 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
drh4e451aa2020-11-05 19:13:44 +0000202 goto trigger_orphan_error;
drhd24cc422003-03-27 12:51:24 +0000203 }
danielk1977ef2cb632004-05-29 02:37:19 +0000204 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000205 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drhda93d232003-03-31 02:12:46 +0000206 " trigger on table: %S", pTableName, 0);
drh4e451aa2020-11-05 19:13:44 +0000207 goto trigger_orphan_error;
drhd24cc422003-03-27 12:51:24 +0000208 }
danielk1977ef2cb632004-05-29 02:37:19 +0000209
drhd24cc422003-03-27 12:51:24 +0000210#ifndef SQLITE_OMIT_AUTHORIZATION
danc9461ec2018-08-29 21:00:16 +0000211 if( !IN_RENAME_OBJECT ){
drha0daa752016-09-16 11:53:10 +0000212 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drhd24cc422003-03-27 12:51:24 +0000213 int code = SQLITE_CREATE_TRIGGER;
drh69c33822016-08-18 14:33:11 +0000214 const char *zDb = db->aDb[iTabDb].zDbSName;
215 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
danielk1977da184232006-01-05 11:34:32 +0000216 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19 +0000217 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24 +0000218 goto trigger_cleanup;
219 }
danielk1977da184232006-01-05 11:34:32 +0000220 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24 +0000221 goto trigger_cleanup;
222 }
223 }
224#endif
danielk1977d702fcc2002-05-26 23:24:40 +0000225
danielk1977ef2cb632004-05-29 02:37:19 +0000226 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04 +0000227 ** cannot appear on views. So we might as well translate every
228 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
229 ** elsewhere.
230 */
danielk1977d702fcc2002-05-26 23:24:40 +0000231 if (tr_tm == TK_INSTEAD){
232 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12 +0000233 }
234
235 /* Build the Trigger object */
drh17435752007-08-16 04:30:38 +0000236 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19 +0000237 if( pTrigger==0 ) goto trigger_cleanup;
dan165921a2009-08-28 18:53:45 +0000238 pTrigger->zName = zName;
drhe5f9c642003-01-13 23:27:31 +0000239 zName = 0;
drh17435752007-08-16 04:30:38 +0000240 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32 +0000241 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48 +0000242 pTrigger->pTabSchema = pTab->pSchema;
drhaa78bec2008-12-09 03:55:14 +0000243 pTrigger->op = (u8)op;
drhdca76842004-12-07 14:06:13 +0000244 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
danc9461ec2018-08-29 21:00:16 +0000245 if( IN_RENAME_OBJECT ){
246 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
dan5496d6a2018-08-13 17:14:26 +0000247 pTrigger->pWhen = pWhen;
248 pWhen = 0;
249 }else{
250 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
251 }
252 pTrigger->pColumns = pColumns;
253 pColumns = 0;
drhf0f258b2003-04-21 18:48:45 +0000254 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19 +0000255 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45 +0000256
257trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000258 sqlite3DbFree(db, zName);
259 sqlite3SrcListDelete(db, pTableName);
260 sqlite3IdListDelete(db, pColumns);
261 sqlite3ExprDelete(db, pWhen);
danielk1977d5d56522005-03-16 12:15:20 +0000262 if( !pParse->pNewTrigger ){
drh633e6d52008-07-28 19:34:53 +0000263 sqlite3DeleteTrigger(db, pTrigger);
danielk1977d5d56522005-03-16 12:15:20 +0000264 }else{
265 assert( pParse->pNewTrigger==pTrigger );
266 }
drh4e451aa2020-11-05 19:13:44 +0000267 return;
268
269trigger_orphan_error:
270 if( db->init.iDb==1 ){
271 /* Ticket #3810.
272 ** Normally, whenever a table is dropped, all associated triggers are
273 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
274 ** and the table is dropped by a different database connection, the
275 ** trigger is not visible to the database connection that does the
276 ** drop so the trigger cannot be dropped. This results in an
277 ** "orphaned trigger" - a trigger whose associated table is missing.
278 **
279 ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df
280 */
281 db->init.orphanTrigger = 1;
282 }
283 goto trigger_cleanup;
drhf0f258b2003-04-21 18:48:45 +0000284}
285
286/*
287** This routine is called after all of the trigger actions have been parsed
288** in order to complete the process of building the trigger.
289*/
danielk19774adee202004-05-08 08:23:19 +0000290void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45 +0000291 Parse *pParse, /* Parser context */
292 TriggerStep *pStepList, /* The triggered program */
293 Token *pAll /* Token that describes the complete CREATE TRIGGER */
294){
drha8c62df2010-02-15 15:47:18 +0000295 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
296 char *zName; /* Name of trigger */
297 sqlite3 *db = pParse->db; /* The database */
298 DbFixer sFix; /* Fixer object */
299 int iDb; /* Database containing the trigger */
300 Token nameToken; /* Trigger name for error reporting */
drhf0f258b2003-04-21 18:48:45 +0000301
drhf0f258b2003-04-21 18:48:45 +0000302 pParse->pNewTrigger = 0;
drh9ab4c2e2009-05-09 00:18:38 +0000303 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
dan165921a2009-08-28 18:53:45 +0000304 zName = pTrig->zName;
danielk1977da184232006-01-05 11:34:32 +0000305 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32 +0000306 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53 +0000307 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32 +0000308 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53 +0000309 pStepList = pStepList->pNext;
310 }
drh40aced52016-01-22 17:48:09 +0000311 sqlite3TokenInit(&nameToken, pTrig->zName);
drhd100f692013-10-03 15:39:44 +0000312 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
313 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
dan46539d72013-10-03 12:29:38 +0000314 || sqlite3FixExpr(&sFix, pTrig->pWhen)
drhd100f692013-10-03 15:39:44 +0000315 ){
drhf26e09c2003-05-31 16:21:12 +0000316 goto triggerfinish_cleanup;
317 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000318
dan5496d6a2018-08-13 17:14:26 +0000319#ifndef SQLITE_OMIT_ALTERTABLE
danc9461ec2018-08-29 21:00:16 +0000320 if( IN_RENAME_OBJECT ){
dan5496d6a2018-08-13 17:14:26 +0000321 assert( !db->init.busy );
322 pParse->pNewTrigger = pTrig;
323 pTrig = 0;
324 }else
325#endif
326
drha8c62df2010-02-15 15:47:18 +0000327 /* if we are not initializing,
drh1e32bed2020-06-19 13:33:53 +0000328 ** build the sqlite_schema entry
drh9adf9ac2002-05-15 11:44:13 +0000329 */
drh1d85d932004-02-14 23:05:52 +0000330 if( !db->init.busy ){
drhc977f7f2002-05-21 11:38:11 +0000331 Vdbe *v;
drh2d401ab2008-01-10 23:50:11 +0000332 char *z;
danielk1977c3f9bad2002-05-15 08:30:12 +0000333
drh1e32bed2020-06-19 13:33:53 +0000334 /* Make an entry in the sqlite_schema table */
danielk19774adee202004-05-08 08:23:19 +0000335 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000336 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000337 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11 +0000338 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
drhd96cc6f2017-06-08 14:35:21 +0000339 testcase( z==0 );
drh2d401ab2008-01-10 23:50:11 +0000340 sqlite3NestedParse(pParse,
drh346a70c2020-06-15 20:27:35 +0000341 "INSERT INTO %Q." DFLT_SCHEMA_TABLE
342 " VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
343 db->aDb[iDb].zDbSName, zName,
drh2d401ab2008-01-10 23:50:11 +0000344 pTrig->table, z);
drh633e6d52008-07-28 19:34:53 +0000345 sqlite3DbFree(db, z);
drh9cbf3422008-01-17 16:22:13 +0000346 sqlite3ChangeCookie(pParse, iDb);
drh5d9c9da2011-06-03 20:11:17 +0000347 sqlite3VdbeAddParseSchemaOp(v, iDb,
348 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
danielk1977c3f9bad2002-05-15 08:30:12 +0000349 }
350
drh956bc922004-07-24 17:38:29 +0000351 if( db->init.busy ){
danielk19772f886d12009-02-28 10:47:41 +0000352 Trigger *pLink = pTrig;
353 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
drh21206082011-04-04 18:22:02 +0000354 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh55f66b32019-07-16 19:44:32 +0000355 assert( pLink!=0 );
drhacbcb7e2014-08-21 20:26:37 +0000356 pTrig = sqlite3HashInsert(pHash, zName, pTrig);
danielk19772f886d12009-02-28 10:47:41 +0000357 if( pTrig ){
drh4a642b62016-02-05 01:55:27 +0000358 sqlite3OomFault(db);
danielk19772f886d12009-02-28 10:47:41 +0000359 }else if( pLink->pSchema==pLink->pTabSchema ){
360 Table *pTab;
drhacbcb7e2014-08-21 20:26:37 +0000361 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
danielk19772f886d12009-02-28 10:47:41 +0000362 assert( pTab!=0 );
363 pLink->pNext = pTab->pTrigger;
364 pTab->pTrigger = pLink;
danielk1977d5d56522005-03-16 12:15:20 +0000365 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000366 }
367
drhf0f258b2003-04-21 18:48:45 +0000368triggerfinish_cleanup:
drh633e6d52008-07-28 19:34:53 +0000369 sqlite3DeleteTrigger(db, pTrig);
danc9461ec2018-08-29 21:00:16 +0000370 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
drh633e6d52008-07-28 19:34:53 +0000371 sqlite3DeleteTriggerStep(db, pStepList);
drh4b59ab52002-08-24 18:24:51 +0000372}
danielk1977c3f9bad2002-05-15 08:30:12 +0000373
drh4b59ab52002-08-24 18:24:51 +0000374/*
drhf259df52017-12-27 20:38:35 +0000375** Duplicate a range of text from an SQL statement, then convert all
376** whitespace characters into ordinary space characters.
377*/
378static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
379 char *z = sqlite3DbSpanDup(db, zStart, zEnd);
380 int i;
381 if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i]) ) z[i] = ' ';
382 return z;
383}
384
385/*
drhc977f7f2002-05-21 11:38:11 +0000386** Turn a SELECT statement (that the pSelect parameter points to) into
387** a trigger step. Return a pointer to a TriggerStep structure.
388**
389** The parser calls this routine when it finds a SELECT statement in
390** body of a TRIGGER.
391*/
drhf259df52017-12-27 20:38:35 +0000392TriggerStep *sqlite3TriggerSelectStep(
393 sqlite3 *db, /* Database connection */
394 Select *pSelect, /* The SELECT statement */
395 const char *zStart, /* Start of SQL text */
396 const char *zEnd /* End of SQL text */
397){
drh17435752007-08-16 04:30:38 +0000398 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000399 if( pTriggerStep==0 ) {
drh633e6d52008-07-28 19:34:53 +0000400 sqlite3SelectDelete(db, pSelect);
danielk19772e588c72005-12-09 14:25:08 +0000401 return 0;
402 }
danielk1977633ed082002-05-17 00:05:58 +0000403 pTriggerStep->op = TK_SELECT;
404 pTriggerStep->pSelect = pSelect;
405 pTriggerStep->orconf = OE_Default;
drhf259df52017-12-27 20:38:35 +0000406 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
drhb7916a72009-05-27 10:31:29 +0000407 return pTriggerStep;
408}
danielk1977c3f9bad2002-05-15 08:30:12 +0000409
drhb7916a72009-05-27 10:31:29 +0000410/*
411** Allocate space to hold a new trigger step. The allocated space
412** holds both the TriggerStep object and the TriggerStep.target.z string.
413**
414** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
415*/
416static TriggerStep *triggerStepAllocate(
danc9461ec2018-08-29 21:00:16 +0000417 Parse *pParse, /* Parser context */
shane5eff7cf2009-08-10 03:57:58 +0000418 u8 op, /* Trigger opcode */
drhf259df52017-12-27 20:38:35 +0000419 Token *pName, /* The target name */
420 const char *zStart, /* Start of SQL text */
421 const char *zEnd /* End of SQL text */
drhb7916a72009-05-27 10:31:29 +0000422){
danc9461ec2018-08-29 21:00:16 +0000423 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000424 TriggerStep *pTriggerStep;
425
dan46408352015-04-21 16:38:49 +0000426 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
drhb7916a72009-05-27 10:31:29 +0000427 if( pTriggerStep ){
428 char *z = (char*)&pTriggerStep[1];
429 memcpy(z, pName->z, pName->n);
dan46408352015-04-21 16:38:49 +0000430 sqlite3Dequote(z);
431 pTriggerStep->zTarget = z;
drhb7916a72009-05-27 10:31:29 +0000432 pTriggerStep->op = op;
drhf259df52017-12-27 20:38:35 +0000433 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
danc9461ec2018-08-29 21:00:16 +0000434 if( IN_RENAME_OBJECT ){
435 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
436 }
drhb7916a72009-05-27 10:31:29 +0000437 }
danielk1977633ed082002-05-17 00:05:58 +0000438 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000439}
440
drhc977f7f2002-05-21 11:38:11 +0000441/*
442** Build a trigger step out of an INSERT statement. Return a pointer
443** to the new trigger step.
444**
445** The parser calls this routine when it sees an INSERT inside the
446** body of a trigger.
447*/
danielk19774adee202004-05-08 08:23:19 +0000448TriggerStep *sqlite3TriggerInsertStep(
dan5be60c52018-08-15 20:28:39 +0000449 Parse *pParse, /* Parser */
drhc977f7f2002-05-21 11:38:11 +0000450 Token *pTableName, /* Name of the table into which we insert */
451 IdList *pColumn, /* List of columns in pTableName to insert into */
drhc977f7f2002-05-21 11:38:11 +0000452 Select *pSelect, /* A SELECT statement that supplies values */
drhf259df52017-12-27 20:38:35 +0000453 u8 orconf, /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
drh46d2e5c2018-04-12 13:15:43 +0000454 Upsert *pUpsert, /* ON CONFLICT clauses for upsert */
drhf259df52017-12-27 20:38:35 +0000455 const char *zStart, /* Start of SQL text */
456 const char *zEnd /* End of SQL text */
danielk1977633ed082002-05-17 00:05:58 +0000457){
dan5be60c52018-08-15 20:28:39 +0000458 sqlite3 *db = pParse->db;
drhf3a65f72007-08-22 20:18:21 +0000459 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000460
drh75593d92014-01-10 20:46:55 +0000461 assert(pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000462
danc9461ec2018-08-29 21:00:16 +0000463 pTriggerStep = triggerStepAllocate(pParse, TK_INSERT, pTableName,zStart,zEnd);
danielk1977d5d56522005-03-16 12:15:20 +0000464 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000465 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000466 pTriggerStep->pSelect = pSelect;
467 pSelect = 0;
468 }else{
469 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
470 }
danielk1977d5d56522005-03-16 12:15:20 +0000471 pTriggerStep->pIdList = pColumn;
drh46d2e5c2018-04-12 13:15:43 +0000472 pTriggerStep->pUpsert = pUpsert;
danielk1977d5d56522005-03-16 12:15:20 +0000473 pTriggerStep->orconf = orconf;
dan9105fd52019-08-19 17:26:32 +0000474 if( pUpsert ){
475 sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
476 }
danielk1977d5d56522005-03-16 12:15:20 +0000477 }else{
drh99160482018-04-18 01:34:39 +0000478 testcase( pColumn );
drh633e6d52008-07-28 19:34:53 +0000479 sqlite3IdListDelete(db, pColumn);
drh99160482018-04-18 01:34:39 +0000480 testcase( pUpsert );
drh46d2e5c2018-04-12 13:15:43 +0000481 sqlite3UpsertDelete(db, pUpsert);
danielk1977d5d56522005-03-16 12:15:20 +0000482 }
drh33e619f2009-05-28 01:00:55 +0000483 sqlite3SelectDelete(db, pSelect);
danielk1977c3f9bad2002-05-15 08:30:12 +0000484
danielk1977633ed082002-05-17 00:05:58 +0000485 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000486}
487
drhc977f7f2002-05-21 11:38:11 +0000488/*
489** Construct a trigger step that implements an UPDATE statement and return
490** a pointer to that trigger step. The parser calls this routine when it
491** sees an UPDATE statement inside the body of a CREATE TRIGGER.
492*/
danielk19774adee202004-05-08 08:23:19 +0000493TriggerStep *sqlite3TriggerUpdateStep(
dan5be60c52018-08-15 20:28:39 +0000494 Parse *pParse, /* Parser */
drhc977f7f2002-05-21 11:38:11 +0000495 Token *pTableName, /* Name of the table to be updated */
dane7877b22020-07-14 19:51:01 +0000496 SrcList *pFrom,
drhc977f7f2002-05-21 11:38:11 +0000497 ExprList *pEList, /* The SET clause: list of column and new values */
498 Expr *pWhere, /* The WHERE clause */
drhf259df52017-12-27 20:38:35 +0000499 u8 orconf, /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
500 const char *zStart, /* Start of SQL text */
501 const char *zEnd /* End of SQL text */
drhc977f7f2002-05-21 11:38:11 +0000502){
dan5be60c52018-08-15 20:28:39 +0000503 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000504 TriggerStep *pTriggerStep;
505
danc9461ec2018-08-29 21:00:16 +0000506 pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE, pTableName,zStart,zEnd);
drh33e619f2009-05-28 01:00:55 +0000507 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000508 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000509 pTriggerStep->pExprList = pEList;
510 pTriggerStep->pWhere = pWhere;
dane7877b22020-07-14 19:51:01 +0000511 pTriggerStep->pFrom = pFrom;
dan5be60c52018-08-15 20:28:39 +0000512 pEList = 0;
513 pWhere = 0;
dane7877b22020-07-14 19:51:01 +0000514 pFrom = 0;
dan5be60c52018-08-15 20:28:39 +0000515 }else{
516 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
517 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
dane7877b22020-07-14 19:51:01 +0000518 pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE);
dan5be60c52018-08-15 20:28:39 +0000519 }
drh33e619f2009-05-28 01:00:55 +0000520 pTriggerStep->orconf = orconf;
drh0e3a6f32007-03-30 20:40:34 +0000521 }
drh33e619f2009-05-28 01:00:55 +0000522 sqlite3ExprListDelete(db, pEList);
523 sqlite3ExprDelete(db, pWhere);
dane7877b22020-07-14 19:51:01 +0000524 sqlite3SrcListDelete(db, pFrom);
danielk1977633ed082002-05-17 00:05:58 +0000525 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000526}
527
drhc977f7f2002-05-21 11:38:11 +0000528/*
529** Construct a trigger step that implements a DELETE statement and return
530** a pointer to that trigger step. The parser calls this routine when it
531** sees a DELETE statement inside the body of a CREATE TRIGGER.
532*/
drh17435752007-08-16 04:30:38 +0000533TriggerStep *sqlite3TriggerDeleteStep(
dan5be60c52018-08-15 20:28:39 +0000534 Parse *pParse, /* Parser */
drh17435752007-08-16 04:30:38 +0000535 Token *pTableName, /* The table from which rows are deleted */
drhf259df52017-12-27 20:38:35 +0000536 Expr *pWhere, /* The WHERE clause */
537 const char *zStart, /* Start of SQL text */
538 const char *zEnd /* End of SQL text */
drh17435752007-08-16 04:30:38 +0000539){
dan5be60c52018-08-15 20:28:39 +0000540 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000541 TriggerStep *pTriggerStep;
542
danc9461ec2018-08-29 21:00:16 +0000543 pTriggerStep = triggerStepAllocate(pParse, TK_DELETE, pTableName,zStart,zEnd);
drh33e619f2009-05-28 01:00:55 +0000544 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000545 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000546 pTriggerStep->pWhere = pWhere;
547 pWhere = 0;
548 }else{
549 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
550 }
drh33e619f2009-05-28 01:00:55 +0000551 pTriggerStep->orconf = OE_Default;
drhb63a53d2007-03-31 01:34:44 +0000552 }
drh33e619f2009-05-28 01:00:55 +0000553 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000554 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000555}
556
danielk1977633ed082002-05-17 00:05:58 +0000557/*
558** Recursively delete a Trigger structure
559*/
drh633e6d52008-07-28 19:34:53 +0000560void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
drhf0f258b2003-04-21 18:48:45 +0000561 if( pTrigger==0 ) return;
drh633e6d52008-07-28 19:34:53 +0000562 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
dan165921a2009-08-28 18:53:45 +0000563 sqlite3DbFree(db, pTrigger->zName);
drh633e6d52008-07-28 19:34:53 +0000564 sqlite3DbFree(db, pTrigger->table);
565 sqlite3ExprDelete(db, pTrigger->pWhen);
566 sqlite3IdListDelete(db, pTrigger->pColumns);
drh633e6d52008-07-28 19:34:53 +0000567 sqlite3DbFree(db, pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000568}
569
570/*
danielk19778a414492004-06-29 08:59:35 +0000571** This function is called to drop a trigger from the database schema.
572**
573** This may be called directly from the parser and therefore identifies
574** the trigger by name. The sqlite3DropTriggerPtr() routine does the
575** same job as this routine except it takes a pointer to the trigger
576** instead of the trigger name.
577**/
drhfdd48a72006-09-11 23:45:48 +0000578void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000579 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000580 int i;
581 const char *zDb;
582 const char *zName;
drh9bb575f2004-09-06 17:24:11 +0000583 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000584
drh17435752007-08-16 04:30:38 +0000585 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000586 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000587 goto drop_trigger_cleanup;
588 }
danielk19778e227872004-06-07 07:52:17 +0000589
drhd24cc422003-03-27 12:51:24 +0000590 assert( pName->nSrc==1 );
591 zDb = pName->a[0].zDatabase;
592 zName = pName->a[0].zName;
drh21206082011-04-04 18:22:02 +0000593 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
danielk197753c0f742005-03-29 03:10:59 +0000594 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000595 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
dan465c2b82020-03-21 15:10:40 +0000596 if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
drh21206082011-04-04 18:22:02 +0000597 assert( sqlite3SchemaMutexHeld(db, j, 0) );
drhacbcb7e2014-08-21 20:26:37 +0000598 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
drhd24cc422003-03-27 12:51:24 +0000599 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000600 }
drhd24cc422003-03-27 12:51:24 +0000601 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000602 if( !noErr ){
603 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
dan57966752011-04-09 17:32:58 +0000604 }else{
605 sqlite3CodeVerifyNamedSchema(pParse, zDb);
drhfdd48a72006-09-11 23:45:48 +0000606 }
dan1db95102010-06-28 10:15:19 +0000607 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +0000608 goto drop_trigger_cleanup;
609 }
drh74161702006-02-24 02:53:49 +0000610 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000611
612drop_trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000613 sqlite3SrcListDelete(db, pName);
drh79a519c2003-05-17 19:04:03 +0000614}
615
616/*
drh956bc922004-07-24 17:38:29 +0000617** Return a pointer to the Table structure for the table that a trigger
618** is set on.
619*/
drh74161702006-02-24 02:53:49 +0000620static Table *tableOfTrigger(Trigger *pTrigger){
drhacbcb7e2014-08-21 20:26:37 +0000621 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
drh956bc922004-07-24 17:38:29 +0000622}
623
624
625/*
drh74161702006-02-24 02:53:49 +0000626** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000627*/
drh74161702006-02-24 02:53:49 +0000628void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000629 Table *pTable;
630 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000631 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000632 int iDb;
drh79a519c2003-05-17 19:04:03 +0000633
danielk1977da184232006-01-05 11:34:32 +0000634 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000635 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000636 pTable = tableOfTrigger(pTrigger);
drh6397a782019-08-27 10:05:45 +0000637 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000638#ifndef SQLITE_OMIT_AUTHORIZATION
drh6397a782019-08-27 10:05:45 +0000639 if( pTable ){
drhe5f9c642003-01-13 23:27:31 +0000640 int code = SQLITE_DROP_TRIGGER;
drh69c33822016-08-18 14:33:11 +0000641 const char *zDb = db->aDb[iDb].zDbSName;
drh956bc922004-07-24 17:38:29 +0000642 const char *zTab = SCHEMA_TABLE(iDb);
643 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
dan2bd93512009-08-31 08:22:46 +0000644 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
danielk19774adee202004-05-08 08:23:19 +0000645 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000646 return;
drhe5f9c642003-01-13 23:27:31 +0000647 }
drhed6c8672003-01-12 18:02:16 +0000648 }
drhe5f9c642003-01-13 23:27:31 +0000649#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000650
drhf0f258b2003-04-21 18:48:45 +0000651 /* Generate code to destroy the database record of the trigger.
652 */
drh43617e92006-03-06 20:55:46 +0000653 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drh8c8dddc2015-12-09 16:26:38 +0000654 sqlite3NestedParse(pParse,
drh346a70c2020-06-15 20:27:35 +0000655 "DELETE FROM %Q." DFLT_SCHEMA_TABLE " WHERE name=%Q AND type='trigger'",
656 db->aDb[iDb].zDbSName, pTrigger->zName
drh8c8dddc2015-12-09 16:26:38 +0000657 );
drh9cbf3422008-01-17 16:22:13 +0000658 sqlite3ChangeCookie(pParse, iDb);
dan165921a2009-08-28 18:53:45 +0000659 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
drhf0f258b2003-04-21 18:48:45 +0000660 }
drh956bc922004-07-24 17:38:29 +0000661}
drhf0f258b2003-04-21 18:48:45 +0000662
drh956bc922004-07-24 17:38:29 +0000663/*
664** Remove a trigger from the hash tables of the sqlite* pointer.
665*/
666void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
667 Trigger *pTrigger;
drh21206082011-04-04 18:22:02 +0000668 Hash *pHash;
669
670 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
671 pHash = &(db->aDb[iDb].pSchema->trigHash);
drhacbcb7e2014-08-21 20:26:37 +0000672 pTrigger = sqlite3HashInsert(pHash, zName, 0);
drh9ab4c2e2009-05-09 00:18:38 +0000673 if( ALWAYS(pTrigger) ){
danielk19772f886d12009-02-28 10:47:41 +0000674 if( pTrigger->pSchema==pTrigger->pTabSchema ){
675 Table *pTab = tableOfTrigger(pTrigger);
drh6397a782019-08-27 10:05:45 +0000676 if( pTab ){
677 Trigger **pp;
dan0232dad2019-12-03 03:34:06 +0000678 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
679 if( *pp==pTrigger ){
680 *pp = (*pp)->pNext;
681 break;
682 }
683 }
drh6397a782019-08-27 10:05:45 +0000684 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000685 }
drh633e6d52008-07-28 19:34:53 +0000686 sqlite3DeleteTrigger(db, pTrigger);
drh8257aa82017-07-26 19:59:13 +0000687 db->mDbFlags |= DBFLAG_SchemaChange;
danielk1977c3f9bad2002-05-15 08:30:12 +0000688 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000689}
690
drhc977f7f2002-05-21 11:38:11 +0000691/*
692** pEList is the SET clause of an UPDATE statement. Each entry
693** in pEList is of the format <id>=<expr>. If any of the entries
694** in pEList have an <id> which matches an identifier in pIdList,
695** then return TRUE. If pIdList==NULL, then it is considered a
696** wildcard that matches anything. Likewise if pEList==NULL then
697** it matches anything so always return true. Return false only
698** if there is no match.
699*/
drh9ab4c2e2009-05-09 00:18:38 +0000700static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000701 int e;
drh9ab4c2e2009-05-09 00:18:38 +0000702 if( pIdList==0 || NEVER(pEList==0) ) return 1;
drhad2d8302002-05-24 20:31:36 +0000703 for(e=0; e<pEList->nExpr; e++){
drh41cee662019-12-12 20:22:34 +0000704 if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000705 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000706 return 0;
707}
708
danielk1977c3f9bad2002-05-15 08:30:12 +0000709/*
danielk19772f886d12009-02-28 10:47:41 +0000710** Return a list of all triggers on table pTab if there exists at least
711** one trigger that must be fired when an operation of type 'op' is
712** performed on the table, and, if that operation is an UPDATE, if at
713** least one of the columns in pChanges is being modified.
drhdca76842004-12-07 14:06:13 +0000714*/
danielk19772f886d12009-02-28 10:47:41 +0000715Trigger *sqlite3TriggersExist(
716 Parse *pParse, /* Parse context */
drhdca76842004-12-07 14:06:13 +0000717 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000718 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
danielk19772f886d12009-02-28 10:47:41 +0000719 ExprList *pChanges, /* Columns that change in an UPDATE statement */
720 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
drhc977f7f2002-05-21 11:38:11 +0000721){
drhdca76842004-12-07 14:06:13 +0000722 int mask = 0;
drhe83cafd2011-03-21 17:15:58 +0000723 Trigger *pList = 0;
danielk19772f886d12009-02-28 10:47:41 +0000724 Trigger *p;
drhe83cafd2011-03-21 17:15:58 +0000725
726 if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
727 pList = sqlite3TriggerList(pParse, pTab);
728 }
danielk19772f886d12009-02-28 10:47:41 +0000729 assert( pList==0 || IsVirtual(pTab)==0 );
730 for(p=pList; p; p=p->pNext){
drh9ab4c2e2009-05-09 00:18:38 +0000731 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
danielk19772f886d12009-02-28 10:47:41 +0000732 mask |= p->tr_tm;
danielk1977c3f9bad2002-05-15 08:30:12 +0000733 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000734 }
danielk19772f886d12009-02-28 10:47:41 +0000735 if( pMask ){
736 *pMask = mask;
737 }
738 return (mask ? pList : 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000739}
740
drhc977f7f2002-05-21 11:38:11 +0000741/*
dan46408352015-04-21 16:38:49 +0000742** Convert the pStep->zTarget string into a SrcList and return a pointer
drhf26e09c2003-05-31 16:21:12 +0000743** to that SrcList.
744**
745** This routine adds a specific database name, if needed, to the target when
746** forming the SrcList. This prevents a trigger in one database from
747** referring to a target in another database. An exception is when the
748** trigger is in TEMP in which case it can refer to any other database it
749** wants.
750*/
dane7877b22020-07-14 19:51:01 +0000751SrcList *sqlite3TriggerStepSrc(
drhf26e09c2003-05-31 16:21:12 +0000752 Parse *pParse, /* The parsing context */
753 TriggerStep *pStep /* The trigger containing the target token */
754){
dan46408352015-04-21 16:38:49 +0000755 sqlite3 *db = pParse->db;
dane7877b22020-07-14 19:51:01 +0000756 SrcList *pSrc; /* SrcList to be returned */
757 char *zName = sqlite3DbStrDup(db, pStep->zTarget);
drh29c992c2019-01-17 15:40:41 +0000758 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
dane7877b22020-07-14 19:51:01 +0000759 assert( pSrc==0 || pSrc->nSrc==1 );
760 assert( zName || pSrc==0 );
drhb7916a72009-05-27 10:31:29 +0000761 if( pSrc ){
dane7877b22020-07-14 19:51:01 +0000762 Schema *pSchema = pStep->pTrig->pSchema;
763 pSrc->a[0].zName = zName;
764 if( pSchema!=db->aDb[1].pSchema ){
765 pSrc->a[0].pSchema = pSchema;
drhb7916a72009-05-27 10:31:29 +0000766 }
dane7877b22020-07-14 19:51:01 +0000767 if( pStep->pFrom ){
768 SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0);
769 pSrc = sqlite3SrcListAppendList(pParse, pSrc, pDup);
770 }
771 }else{
772 sqlite3DbFree(db, zName);
drhf26e09c2003-05-31 16:21:12 +0000773 }
774 return pSrc;
775}
776
777/*
dan65a7cd12009-09-01 12:16:01 +0000778** Generate VDBE code for the statements inside the body of a single
779** trigger.
drhc977f7f2002-05-21 11:38:11 +0000780*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000781static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000782 Parse *pParse, /* The parser context */
783 TriggerStep *pStepList, /* List of statements inside the trigger body */
dan65a7cd12009-09-01 12:16:01 +0000784 int orconf /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000785){
dan65a7cd12009-09-01 12:16:01 +0000786 TriggerStep *pStep;
drh344737f2004-09-19 00:50:20 +0000787 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000788 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000789
dan65a7cd12009-09-01 12:16:01 +0000790 assert( pParse->pTriggerTab && pParse->pToplevel );
791 assert( pStepList );
drh344737f2004-09-19 00:50:20 +0000792 assert( v!=0 );
dan65a7cd12009-09-01 12:16:01 +0000793 for(pStep=pStepList; pStep; pStep=pStep->pNext){
dan165921a2009-08-28 18:53:45 +0000794 /* Figure out the ON CONFLICT policy that will be used for this step
795 ** of the trigger program. If the statement that caused this trigger
796 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
797 ** the ON CONFLICT policy that was specified as part of the trigger
798 ** step statement. Example:
799 **
800 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
801 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
802 ** END;
803 **
804 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
805 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
806 */
shanecea72b22009-09-07 04:38:36 +0000807 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
drhaceb31b2014-02-08 01:40:27 +0000808 assert( pParse->okConstFactor==0 );
danf78baaf2012-12-06 19:37:22 +0000809
drhf259df52017-12-27 20:38:35 +0000810#ifndef SQLITE_OMIT_TRACE
drhe307e112017-12-27 21:30:34 +0000811 if( pStep->zSpan ){
812 sqlite3VdbeAddOp4(v, OP_Trace, 0x7fffffff, 1, 0,
813 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
814 P4_DYNAMIC);
815 }
drhf259df52017-12-27 20:38:35 +0000816#endif
817
dan165921a2009-08-28 18:53:45 +0000818 switch( pStep->op ){
danielk1977633ed082002-05-17 00:05:58 +0000819 case TK_UPDATE: {
dan165921a2009-08-28 18:53:45 +0000820 sqlite3Update(pParse,
dane7877b22020-07-14 19:51:01 +0000821 sqlite3TriggerStepSrc(pParse, pStep),
dan165921a2009-08-28 18:53:45 +0000822 sqlite3ExprListDup(db, pStep->pExprList, 0),
823 sqlite3ExprDup(db, pStep->pWhere, 0),
drheac9fab2018-04-16 13:00:50 +0000824 pParse->eOrconf, 0, 0, 0
dan165921a2009-08-28 18:53:45 +0000825 );
danielk1977633ed082002-05-17 00:05:58 +0000826 break;
827 }
828 case TK_INSERT: {
dan165921a2009-08-28 18:53:45 +0000829 sqlite3Insert(pParse,
dane7877b22020-07-14 19:51:01 +0000830 sqlite3TriggerStepSrc(pParse, pStep),
dan165921a2009-08-28 18:53:45 +0000831 sqlite3SelectDup(db, pStep->pSelect, 0),
832 sqlite3IdListDup(db, pStep->pIdList),
drh46d2e5c2018-04-12 13:15:43 +0000833 pParse->eOrconf,
834 sqlite3UpsertDup(db, pStep->pUpsert)
dan165921a2009-08-28 18:53:45 +0000835 );
danielk1977633ed082002-05-17 00:05:58 +0000836 break;
837 }
838 case TK_DELETE: {
dan165921a2009-08-28 18:53:45 +0000839 sqlite3DeleteFrom(pParse,
dane7877b22020-07-14 19:51:01 +0000840 sqlite3TriggerStepSrc(pParse, pStep),
drh8c0833f2017-11-14 23:48:23 +0000841 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
dan165921a2009-08-28 18:53:45 +0000842 );
danielk1977633ed082002-05-17 00:05:58 +0000843 break;
844 }
dan165921a2009-08-28 18:53:45 +0000845 default: assert( pStep->op==TK_SELECT ); {
846 SelectDest sDest;
847 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
848 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
849 sqlite3Select(pParse, pSelect, &sDest);
850 sqlite3SelectDelete(db, pSelect);
drh9ab4c2e2009-05-09 00:18:38 +0000851 break;
852 }
danielk1977633ed082002-05-17 00:05:58 +0000853 }
dan76d462e2009-08-30 11:42:51 +0000854 if( pStep->op!=TK_SELECT ){
drhb7f1d9a2009-09-08 02:27:58 +0000855 sqlite3VdbeAddOp0(v, OP_ResetCount);
dan76d462e2009-08-30 11:42:51 +0000856 }
danielk1977633ed082002-05-17 00:05:58 +0000857 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000858
danielk1977633ed082002-05-17 00:05:58 +0000859 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000860}
861
drhc7379ce2013-10-30 02:28:23 +0000862#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
dan165921a2009-08-28 18:53:45 +0000863/*
864** This function is used to add VdbeComment() annotations to a VDBE
865** program. It is not used in production code, only for debugging.
866*/
867static const char *onErrorText(int onError){
868 switch( onError ){
869 case OE_Abort: return "abort";
870 case OE_Rollback: return "rollback";
871 case OE_Fail: return "fail";
872 case OE_Replace: return "replace";
873 case OE_Ignore: return "ignore";
874 case OE_Default: return "default";
875 }
876 return "n/a";
877}
878#endif
879
880/*
881** Parse context structure pFrom has just been used to create a sub-vdbe
882** (trigger program). If an error has occurred, transfer error information
883** from pFrom to pTo.
884*/
885static void transferParseError(Parse *pTo, Parse *pFrom){
886 assert( pFrom->zErrMsg==0 || pFrom->nErr );
887 assert( pTo->zErrMsg==0 || pTo->nErr );
888 if( pTo->nErr==0 ){
889 pTo->zErrMsg = pFrom->zErrMsg;
890 pTo->nErr = pFrom->nErr;
drhe06874e2015-04-16 15:47:06 +0000891 pTo->rc = pFrom->rc;
dan165921a2009-08-28 18:53:45 +0000892 }else{
893 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
894 }
895}
896
dan65a7cd12009-09-01 12:16:01 +0000897/*
898** Create and populate a new TriggerPrg object with a sub-program
899** implementing trigger pTrigger with ON CONFLICT policy orconf.
900*/
dan2832ad42009-08-31 15:27:27 +0000901static TriggerPrg *codeRowTrigger(
dan165921a2009-08-28 18:53:45 +0000902 Parse *pParse, /* Current parse context */
903 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +0000904 Table *pTab, /* The table pTrigger is attached to */
905 int orconf /* ON CONFLICT policy to code trigger program with */
dan165921a2009-08-28 18:53:45 +0000906){
dan65a7cd12009-09-01 12:16:01 +0000907 Parse *pTop = sqlite3ParseToplevel(pParse);
908 sqlite3 *db = pParse->db; /* Database handle */
909 TriggerPrg *pPrg; /* Value to return */
dan165921a2009-08-28 18:53:45 +0000910 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
911 Vdbe *v; /* Temporary VM */
dan165921a2009-08-28 18:53:45 +0000912 NameContext sNC; /* Name context for sub-vdbe */
913 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
914 Parse *pSubParse; /* Parse context for sub-vdbe */
915 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
916
dan1da40a32009-09-19 17:00:31 +0000917 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dand19c9332010-07-26 12:05:17 +0000918 assert( pTop->pVdbe );
dan65a7cd12009-09-01 12:16:01 +0000919
920 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
921 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
922 ** list of the top-level Parse object sooner rather than later. */
dan2832ad42009-08-31 15:27:27 +0000923 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
924 if( !pPrg ) return 0;
dan65a7cd12009-09-01 12:16:01 +0000925 pPrg->pNext = pTop->pTriggerPrg;
926 pTop->pTriggerPrg = pPrg;
dan2832ad42009-08-31 15:27:27 +0000927 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
dan165921a2009-08-28 18:53:45 +0000928 if( !pProgram ) return 0;
dand19c9332010-07-26 12:05:17 +0000929 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
dan2832ad42009-08-31 15:27:27 +0000930 pPrg->pTrigger = pTrigger;
931 pPrg->orconf = orconf;
danbb5f1682009-11-27 12:12:34 +0000932 pPrg->aColmask[0] = 0xffffffff;
933 pPrg->aColmask[1] = 0xffffffff;
dan165921a2009-08-28 18:53:45 +0000934
dan65a7cd12009-09-01 12:16:01 +0000935 /* Allocate and populate a new Parse context to use for coding the
936 ** trigger sub-program. */
937 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
938 if( !pSubParse ) return 0;
dan165921a2009-08-28 18:53:45 +0000939 memset(&sNC, 0, sizeof(sNC));
940 sNC.pParse = pSubParse;
941 pSubParse->db = db;
942 pSubParse->pTriggerTab = pTab;
dan65a7cd12009-09-01 12:16:01 +0000943 pSubParse->pToplevel = pTop;
dan2bd93512009-08-31 08:22:46 +0000944 pSubParse->zAuthContext = pTrigger->zName;
dan65a7cd12009-09-01 12:16:01 +0000945 pSubParse->eTriggerOp = pTrigger->op;
drh8b307fb2010-04-06 15:57:05 +0000946 pSubParse->nQueryLoop = pParse->nQueryLoop;
dan1ea04432018-12-21 19:29:11 +0000947 pSubParse->disableVtab = pParse->disableVtab;
dan165921a2009-08-28 18:53:45 +0000948
949 v = sqlite3GetVdbe(pSubParse);
950 if( v ){
dan76d462e2009-08-30 11:42:51 +0000951 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
952 pTrigger->zName, onErrorText(orconf),
dan165921a2009-08-28 18:53:45 +0000953 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
dan65a7cd12009-09-01 12:16:01 +0000954 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
955 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
956 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
dan76d462e2009-08-30 11:42:51 +0000957 pTab->zName
dan165921a2009-08-28 18:53:45 +0000958 ));
dan76d462e2009-08-30 11:42:51 +0000959#ifndef SQLITE_OMIT_TRACE
drhe307e112017-12-27 21:30:34 +0000960 if( pTrigger->zName ){
961 sqlite3VdbeChangeP4(v, -1,
962 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
963 );
964 }
dan76d462e2009-08-30 11:42:51 +0000965#endif
dan165921a2009-08-28 18:53:45 +0000966
dan65a7cd12009-09-01 12:16:01 +0000967 /* If one was specified, code the WHEN clause. If it evaluates to false
968 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
969 ** OP_Halt inserted at the end of the program. */
dan165921a2009-08-28 18:53:45 +0000970 if( pTrigger->pWhen ){
dan165921a2009-08-28 18:53:45 +0000971 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
dan523a0872009-08-31 05:23:32 +0000972 if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
973 && db->mallocFailed==0
974 ){
drhec4ccdb2018-12-29 02:26:59 +0000975 iEndTrigger = sqlite3VdbeMakeLabel(pSubParse);
dan165921a2009-08-28 18:53:45 +0000976 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
977 }
978 sqlite3ExprDelete(db, pWhen);
979 }
980
981 /* Code the trigger program into the sub-vdbe. */
dan76d462e2009-08-30 11:42:51 +0000982 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
dan65a7cd12009-09-01 12:16:01 +0000983
984 /* Insert an OP_Halt at the end of the sub-program. */
dan165921a2009-08-28 18:53:45 +0000985 if( iEndTrigger ){
986 sqlite3VdbeResolveLabel(v, iEndTrigger);
987 }
988 sqlite3VdbeAddOp0(v, OP_Halt);
dan76d462e2009-08-30 11:42:51 +0000989 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
990
dan165921a2009-08-28 18:53:45 +0000991 transferParseError(pParse, pSubParse);
dan08951172017-11-28 20:43:40 +0000992 if( db->mallocFailed==0 && pParse->nErr==0 ){
dan65a7cd12009-09-01 12:16:01 +0000993 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
dan523a0872009-08-31 05:23:32 +0000994 }
dan165921a2009-08-28 18:53:45 +0000995 pProgram->nMem = pSubParse->nMem;
996 pProgram->nCsr = pSubParse->nTab;
997 pProgram->token = (void *)pTrigger;
danbb5f1682009-11-27 12:12:34 +0000998 pPrg->aColmask[0] = pSubParse->oldmask;
999 pPrg->aColmask[1] = pSubParse->newmask;
dan165921a2009-08-28 18:53:45 +00001000 sqlite3VdbeDelete(v);
dan165921a2009-08-28 18:53:45 +00001001 }
dan65a7cd12009-09-01 12:16:01 +00001002
dan65a7cd12009-09-01 12:16:01 +00001003 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
drhf30a9692013-11-15 01:10:18 +00001004 sqlite3ParserReset(pSubParse);
dan165921a2009-08-28 18:53:45 +00001005 sqlite3StackFree(db, pSubParse);
1006
dan2832ad42009-08-31 15:27:27 +00001007 return pPrg;
dan165921a2009-08-28 18:53:45 +00001008}
1009
dan65a7cd12009-09-01 12:16:01 +00001010/*
1011** Return a pointer to a TriggerPrg object containing the sub-program for
1012** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
1013** TriggerPrg object exists, a new object is allocated and populated before
1014** being returned.
1015*/
dan2832ad42009-08-31 15:27:27 +00001016static TriggerPrg *getRowTrigger(
dan65a7cd12009-09-01 12:16:01 +00001017 Parse *pParse, /* Current parse context */
dan165921a2009-08-28 18:53:45 +00001018 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +00001019 Table *pTab, /* The table trigger pTrigger is attached to */
1020 int orconf /* ON CONFLICT algorithm. */
dan165921a2009-08-28 18:53:45 +00001021){
dan65a7cd12009-09-01 12:16:01 +00001022 Parse *pRoot = sqlite3ParseToplevel(pParse);
dan2832ad42009-08-31 15:27:27 +00001023 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +00001024
dan1da40a32009-09-19 17:00:31 +00001025 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dan165921a2009-08-28 18:53:45 +00001026
1027 /* It may be that this trigger has already been coded (or is in the
1028 ** process of being coded). If this is the case, then an entry with
dan2832ad42009-08-31 15:27:27 +00001029 ** a matching TriggerPrg.pTrigger field will be present somewhere
1030 ** in the Parse.pTriggerPrg list. Search for such an entry. */
dan2832ad42009-08-31 15:27:27 +00001031 for(pPrg=pRoot->pTriggerPrg;
1032 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
1033 pPrg=pPrg->pNext
dan165921a2009-08-28 18:53:45 +00001034 );
1035
dan65a7cd12009-09-01 12:16:01 +00001036 /* If an existing TriggerPrg could not be located, create a new one. */
dan2832ad42009-08-31 15:27:27 +00001037 if( !pPrg ){
dan65a7cd12009-09-01 12:16:01 +00001038 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
dan165921a2009-08-28 18:53:45 +00001039 }
1040
dan2832ad42009-08-31 15:27:27 +00001041 return pPrg;
dan165921a2009-08-28 18:53:45 +00001042}
1043
dan94d7f502009-09-24 09:05:49 +00001044/*
1045** Generate code for the trigger program associated with trigger p on
1046** table pTab. The reg, orconf and ignoreJump parameters passed to this
1047** function are the same as those described in the header function for
1048** sqlite3CodeRowTrigger()
1049*/
dan1da40a32009-09-19 17:00:31 +00001050void sqlite3CodeRowTriggerDirect(
1051 Parse *pParse, /* Parse context */
1052 Trigger *p, /* Trigger to code */
1053 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001054 int reg, /* Reg array containing OLD.* and NEW.* values */
dan1da40a32009-09-19 17:00:31 +00001055 int orconf, /* ON CONFLICT policy */
1056 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
1057){
1058 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
1059 TriggerPrg *pPrg;
1060 pPrg = getRowTrigger(pParse, p, pTab, orconf);
1061 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
1062
1063 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
1064 ** is a pointer to the sub-vdbe containing the trigger program. */
1065 if( pPrg ){
dand19c9332010-07-26 12:05:17 +00001066 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
1067
drh9b34abe2016-01-16 15:12:35 +00001068 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
1069 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
dan1da40a32009-09-19 17:00:31 +00001070 VdbeComment(
1071 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
1072
1073 /* Set the P5 operand of the OP_Program instruction to non-zero if
1074 ** recursive invocation of this trigger program is disallowed. Recursive
1075 ** invocation is disallowed if (a) the sub-program is really a trigger,
1076 ** not a foreign key action, and (b) the flag to enable recursive triggers
1077 ** is clear. */
dand19c9332010-07-26 12:05:17 +00001078 sqlite3VdbeChangeP5(v, (u8)bRecursive);
dan1da40a32009-09-19 17:00:31 +00001079 }
1080}
1081
danielk1977633ed082002-05-17 00:05:58 +00001082/*
dan94d7f502009-09-24 09:05:49 +00001083** This is called to code the required FOR EACH ROW triggers for an operation
1084** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
drhf7b54962013-05-28 12:11:54 +00001085** is given by the op parameter. The tr_tm parameter determines whether the
dan94d7f502009-09-24 09:05:49 +00001086** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
1087** parameter pChanges is passed the list of columns being modified.
danielk1977633ed082002-05-17 00:05:58 +00001088**
dan94d7f502009-09-24 09:05:49 +00001089** If there are no triggers that fire at the specified time for the specified
1090** operation on pTab, this function is a no-op.
drhc977f7f2002-05-21 11:38:11 +00001091**
dan94d7f502009-09-24 09:05:49 +00001092** The reg argument is the address of the first in an array of registers
1093** that contain the values substituted for the new.* and old.* references
1094** in the trigger program. If N is the number of columns in table pTab
1095** (a copy of pTab->nCol), then registers are populated as follows:
drhc977f7f2002-05-21 11:38:11 +00001096**
dan94d7f502009-09-24 09:05:49 +00001097** Register Contains
1098** ------------------------------------------------------
1099** reg+0 OLD.rowid
1100** reg+1 OLD.* value of left-most column of pTab
1101** ... ...
1102** reg+N OLD.* value of right-most column of pTab
1103** reg+N+1 NEW.rowid
1104** reg+N+2 OLD.* value of left-most column of pTab
1105** ... ...
1106** reg+N+N+1 NEW.* value of right-most column of pTab
drhc977f7f2002-05-21 11:38:11 +00001107**
dan94d7f502009-09-24 09:05:49 +00001108** For ON DELETE triggers, the registers containing the NEW.* values will
1109** never be accessed by the trigger program, so they are not allocated or
1110** populated by the caller (there is no data to populate them with anyway).
1111** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
1112** are never accessed, and so are not allocated by the caller. So, for an
1113** ON INSERT trigger, the value passed to this function as parameter reg
1114** is not a readable register, although registers (reg+N) through
1115** (reg+N+N+1) are.
danielk1977633ed082002-05-17 00:05:58 +00001116**
dan94d7f502009-09-24 09:05:49 +00001117** Parameter orconf is the default conflict resolution algorithm for the
1118** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
1119** is the instruction that control should jump to if a trigger program
1120** raises an IGNORE exception.
danielk1977633ed082002-05-17 00:05:58 +00001121*/
dan165921a2009-08-28 18:53:45 +00001122void sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +00001123 Parse *pParse, /* Parse context */
danielk19772f886d12009-02-28 10:47:41 +00001124 Trigger *pTrigger, /* List of triggers on table pTab */
danielk1977633ed082002-05-17 00:05:58 +00001125 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
1126 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +00001127 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +00001128 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001129 int reg, /* The first in an array of registers (see above) */
danielk19776f349032002-06-11 02:25:40 +00001130 int orconf, /* ON CONFLICT policy */
dan165921a2009-08-28 18:53:45 +00001131 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
drhc977f7f2002-05-21 11:38:11 +00001132){
dan94d7f502009-09-24 09:05:49 +00001133 Trigger *p; /* Used to iterate through pTrigger list */
danielk19778f2c54e2008-01-01 19:02:09 +00001134
dan94d7f502009-09-24 09:05:49 +00001135 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
1136 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
1137 assert( (op==TK_UPDATE)==(pChanges!=0) );
danielk1977c3f9bad2002-05-15 08:30:12 +00001138
danielk19772f886d12009-02-28 10:47:41 +00001139 for(p=pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +00001140
drh9ab4c2e2009-05-09 00:18:38 +00001141 /* Sanity checking: The schema for the trigger and for the table are
1142 ** always defined. The trigger must be in the same schema as the table
1143 ** or else it must be a TEMP trigger. */
1144 assert( p->pSchema!=0 );
1145 assert( p->pTabSchema!=0 );
dan165921a2009-08-28 18:53:45 +00001146 assert( p->pSchema==p->pTabSchema
1147 || p->pSchema==pParse->db->aDb[1].pSchema );
drh9ab4c2e2009-05-09 00:18:38 +00001148
danielk1977eecfb3e2006-01-10 12:31:39 +00001149 /* Determine whether we should code this trigger */
dan165921a2009-08-28 18:53:45 +00001150 if( p->op==op
1151 && p->tr_tm==tr_tm
dan94d7f502009-09-24 09:05:49 +00001152 && checkColumnOverlap(p->pColumns, pChanges)
danielk1977eecfb3e2006-01-10 12:31:39 +00001153 ){
dan94d7f502009-09-24 09:05:49 +00001154 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
danielk1977c3f9bad2002-05-15 08:30:12 +00001155 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001156 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001157}
dan165921a2009-08-28 18:53:45 +00001158
dan2832ad42009-08-31 15:27:27 +00001159/*
danbb5f1682009-11-27 12:12:34 +00001160** Triggers may access values stored in the old.* or new.* pseudo-table.
1161** This function returns a 32-bit bitmask indicating which columns of the
1162** old.* or new.* tables actually are used by triggers. This information
1163** may be used by the caller, for example, to avoid having to load the entire
1164** old.* record into memory when executing an UPDATE or DELETE command.
dan2832ad42009-08-31 15:27:27 +00001165**
1166** Bit 0 of the returned mask is set if the left-most column of the
danbb5f1682009-11-27 12:12:34 +00001167** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
dan2832ad42009-08-31 15:27:27 +00001168** the second leftmost column value is required, and so on. If there
1169** are more than 32 columns in the table, and at least one of the columns
1170** with an index greater than 32 may be accessed, 0xffffffff is returned.
1171**
danbb5f1682009-11-27 12:12:34 +00001172** It is not possible to determine if the old.rowid or new.rowid column is
1173** accessed by triggers. The caller must always assume that it is.
dan2832ad42009-08-31 15:27:27 +00001174**
danbb5f1682009-11-27 12:12:34 +00001175** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
1176** applies to the old.* table. If 1, the new.* table.
1177**
1178** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
1179** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
1180** included in the returned mask if the TRIGGER_BEFORE bit is set in the
1181** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
1182** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
dan2832ad42009-08-31 15:27:27 +00001183*/
danbb5f1682009-11-27 12:12:34 +00001184u32 sqlite3TriggerColmask(
dan165921a2009-08-28 18:53:45 +00001185 Parse *pParse, /* Parse context */
1186 Trigger *pTrigger, /* List of triggers on table pTab */
dan165921a2009-08-28 18:53:45 +00001187 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
danbb5f1682009-11-27 12:12:34 +00001188 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
1189 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
dan165921a2009-08-28 18:53:45 +00001190 Table *pTab, /* The table to code triggers from */
dan2832ad42009-08-31 15:27:27 +00001191 int orconf /* Default ON CONFLICT policy for trigger steps */
dan165921a2009-08-28 18:53:45 +00001192){
dan1da40a32009-09-19 17:00:31 +00001193 const int op = pChanges ? TK_UPDATE : TK_DELETE;
dan2832ad42009-08-31 15:27:27 +00001194 u32 mask = 0;
dan165921a2009-08-28 18:53:45 +00001195 Trigger *p;
dan165921a2009-08-28 18:53:45 +00001196
danbb5f1682009-11-27 12:12:34 +00001197 assert( isNew==1 || isNew==0 );
dan165921a2009-08-28 18:53:45 +00001198 for(p=pTrigger; p; p=p->pNext){
danbb5f1682009-11-27 12:12:34 +00001199 if( p->op==op && (tr_tm&p->tr_tm)
1200 && checkColumnOverlap(p->pColumns,pChanges)
1201 ){
dan2832ad42009-08-31 15:27:27 +00001202 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +00001203 pPrg = getRowTrigger(pParse, p, pTab, orconf);
dan2832ad42009-08-31 15:27:27 +00001204 if( pPrg ){
danbb5f1682009-11-27 12:12:34 +00001205 mask |= pPrg->aColmask[isNew];
dan165921a2009-08-28 18:53:45 +00001206 }
1207 }
1208 }
dan2832ad42009-08-31 15:27:27 +00001209
1210 return mask;
dan165921a2009-08-28 18:53:45 +00001211}
1212
drhb7f91642004-10-31 02:22:47 +00001213#endif /* !defined(SQLITE_OMIT_TRIGGER) */