blob: 50de878dd5a56ed9f0b943901edca56c20dd0901 [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);
drhf259df52017-12-27 20:38:35 +000029 sqlite3DbFree(db, pTmp->zSpan);
drh4b59ab52002-08-24 18:24:51 +000030
drh633e6d52008-07-28 19:34:53 +000031 sqlite3DbFree(db, pTmp);
drh4b59ab52002-08-24 18:24:51 +000032 }
33}
34
35/*
danielk19772f886d12009-02-28 10:47:41 +000036** Given table pTab, return a list of all the triggers attached to
37** the table. The list is connected by Trigger.pNext pointers.
drh9ab4c2e2009-05-09 00:18:38 +000038**
39** All of the triggers on pTab that are in the same database as pTab
40** are already attached to pTab->pTrigger. But there might be additional
41** triggers on pTab in the TEMP schema. This routine prepends all
42** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
43** and returns the combined list.
44**
45** To state it another way: This routine returns a list of all triggers
46** that fire off of pTab. The list will include any TEMP triggers on
47** pTab as well as the triggers lised in pTab->pTrigger.
danielk19772f886d12009-02-28 10:47:41 +000048*/
49Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
50 Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
51 Trigger *pList = 0; /* List of triggers to return */
52
dand66c8302009-09-28 14:49:01 +000053 if( pParse->disableTriggers ){
54 return 0;
55 }
56
danielk19772f886d12009-02-28 10:47:41 +000057 if( pTmpSchema!=pTab->pSchema ){
58 HashElem *p;
drh21206082011-04-04 18:22:02 +000059 assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) );
danielk19772f886d12009-02-28 10:47:41 +000060 for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
61 Trigger *pTrig = (Trigger *)sqliteHashData(p);
62 if( pTrig->pTabSchema==pTab->pSchema
63 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
64 ){
65 pTrig->pNext = (pList ? pList : pTab->pTrigger);
66 pList = pTrig;
67 }
68 }
69 }
70
71 return (pList ? pList : pTab->pTrigger);
72}
73
74/*
drhf0f258b2003-04-21 18:48:45 +000075** This is called by the parser when it sees a CREATE TRIGGER statement
76** up to the point of the BEGIN before the trigger actions. A Trigger
77** structure is generated based on the information available and stored
78** in pParse->pNewTrigger. After the trigger actions have been parsed, the
danielk19774adee202004-05-08 08:23:19 +000079** sqlite3FinishTrigger() function is called to complete the trigger
drhf0f258b2003-04-21 18:48:45 +000080** construction process.
drh9adf9ac2002-05-15 11:44:13 +000081*/
danielk19774adee202004-05-08 08:23:19 +000082void sqlite3BeginTrigger(
drh9adf9ac2002-05-15 11:44:13 +000083 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
danielk1977ef2cb632004-05-29 02:37:19 +000084 Token *pName1, /* The name of the trigger */
85 Token *pName2, /* The name of the trigger */
drh5cf590c2003-04-24 01:45:04 +000086 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
drh9adf9ac2002-05-15 11:44:13 +000087 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
danielk1977633ed082002-05-17 00:05:58 +000088 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
drhd24cc422003-03-27 12:51:24 +000089 SrcList *pTableName,/* The name of the table/view the trigger applies to */
drh9adf9ac2002-05-15 11:44:13 +000090 Expr *pWhen, /* WHEN clause */
drhfdd48a72006-09-11 23:45:48 +000091 int isTemp, /* True if the TEMPORARY keyword is present */
92 int noErr /* Suppress errors if the trigger already exists */
drh9adf9ac2002-05-15 11:44:13 +000093){
drh3d5f74b2009-08-06 17:43:31 +000094 Trigger *pTrigger = 0; /* The new trigger */
95 Table *pTab; /* Table that the trigger fires off of */
drhf0f258b2003-04-21 18:48:45 +000096 char *zName = 0; /* Name of the trigger */
drh3d5f74b2009-08-06 17:43:31 +000097 sqlite3 *db = pParse->db; /* The database connection */
danielk1977ef2cb632004-05-29 02:37:19 +000098 int iDb; /* The database to store the trigger in */
99 Token *pName; /* The unqualified db name */
drh3d5f74b2009-08-06 17:43:31 +0000100 DbFixer sFix; /* State vector for the DB fixer */
drhed6c8672003-01-12 18:02:16 +0000101
drh43617e92006-03-06 20:55:46 +0000102 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
103 assert( pName2!=0 );
drhaa78bec2008-12-09 03:55:14 +0000104 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
105 assert( op>0 && op<0xff );
danielk1977ef2cb632004-05-29 02:37:19 +0000106 if( isTemp ){
107 /* If TEMP was specified, then the trigger name may not be qualified. */
drh43617e92006-03-06 20:55:46 +0000108 if( pName2->n>0 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000109 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
110 goto trigger_cleanup;
111 }
112 iDb = 1;
113 pName = pName1;
114 }else{
mistachkind5578432012-08-25 10:01:29 +0000115 /* Figure out the db that the trigger will be created in */
danielk1977ef2cb632004-05-29 02:37:19 +0000116 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
117 if( iDb<0 ){
118 goto trigger_cleanup;
119 }
120 }
drh6fea83e2011-07-01 13:50:44 +0000121 if( !pTableName || db->mallocFailed ){
122 goto trigger_cleanup;
123 }
124
125 /* A long-standing parser bug is that this syntax was allowed:
126 **
127 ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
128 ** ^^^^^^^^
129 **
130 ** To maintain backwards compatibility, ignore the database
drh346a70c2020-06-15 20:27:35 +0000131 ** name on pTableName if we are reparsing out of the schema table
drh6fea83e2011-07-01 13:50:44 +0000132 */
133 if( db->init.busy && iDb!=1 ){
134 sqlite3DbFree(db, pTableName->a[0].zDatabase);
135 pTableName->a[0].zDatabase = 0;
136 }
danielk1977ef2cb632004-05-29 02:37:19 +0000137
138 /* If the trigger name was unqualified, and the table is a temp table,
139 ** then set iDb to 1 to create the trigger in the temporary database.
140 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
141 ** exist, the error is caught by the block below.
drh9adf9ac2002-05-15 11:44:13 +0000142 */
danielk1977ef2cb632004-05-29 02:37:19 +0000143 pTab = sqlite3SrcListLookup(pParse, pTableName);
drh622d2882010-02-15 16:54:55 +0000144 if( db->init.busy==0 && pName2->n==0 && pTab
145 && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977ef2cb632004-05-29 02:37:19 +0000146 iDb = 1;
147 }
148
149 /* Ensure the table name matches database name and that the table exists */
drh17435752007-08-16 04:30:38 +0000150 if( db->mallocFailed ) goto trigger_cleanup;
drhd24cc422003-03-27 12:51:24 +0000151 assert( pTableName->nSrc==1 );
drhd100f692013-10-03 15:39:44 +0000152 sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
153 if( sqlite3FixSrcList(&sFix, pTableName) ){
drh4312db52003-06-03 01:47:11 +0000154 goto trigger_cleanup;
drhf26e09c2003-05-31 16:21:12 +0000155 }
danielk1977ef2cb632004-05-29 02:37:19 +0000156 pTab = sqlite3SrcListLookup(pParse, pTableName);
157 if( !pTab ){
158 /* The table does not exist. */
drh3d5f74b2009-08-06 17:43:31 +0000159 if( db->init.iDb==1 ){
160 /* Ticket #3810.
161 ** Normally, whenever a table is dropped, all associated triggers are
162 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
163 ** and the table is dropped by a different database connection, the
164 ** trigger is not visible to the database connection that does the
165 ** drop so the trigger cannot be dropped. This results in an
166 ** "orphaned trigger" - a trigger whose associated table is missing.
167 */
168 db->init.orphanTrigger = 1;
169 }
drhd24cc422003-03-27 12:51:24 +0000170 goto trigger_cleanup;
171 }
drh4cbdda92006-06-14 19:00:20 +0000172 if( IsVirtual(pTab) ){
173 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
174 goto trigger_cleanup;
175 }
drhd24cc422003-03-27 12:51:24 +0000176
danielk1977d8123362004-06-12 09:25:12 +0000177 /* Check that the trigger name is not reserved and that no trigger of the
178 ** specified name exists */
drh17435752007-08-16 04:30:38 +0000179 zName = sqlite3NameFromToken(db, pName);
drhc5a93d42019-08-12 00:08:07 +0000180 if( zName==0 ){
181 assert( db->mallocFailed );
182 goto trigger_cleanup;
183 }
184 if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
danielk1977d8123362004-06-12 09:25:12 +0000185 goto trigger_cleanup;
186 }
drh21206082011-04-04 18:22:02 +0000187 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danc9461ec2018-08-29 21:00:16 +0000188 if( !IN_RENAME_OBJECT ){
dan5496d6a2018-08-13 17:14:26 +0000189 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
190 if( !noErr ){
191 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
192 }else{
193 assert( !db->init.busy );
194 sqlite3CodeVerifySchema(pParse, iDb);
195 }
196 goto trigger_cleanup;
drhfdd48a72006-09-11 23:45:48 +0000197 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000198 }
danielk1977ef2cb632004-05-29 02:37:19 +0000199
200 /* Do not create a trigger on a system table */
drhdca76842004-12-07 14:06:13 +0000201 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk19774adee202004-05-08 08:23:19 +0000202 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
drhd24cc422003-03-27 12:51:24 +0000203 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40 +0000204 }
danielk1977ef2cb632004-05-29 02:37:19 +0000205
206 /* INSTEAD of triggers are only for views and views only support INSTEAD
207 ** of triggers.
208 */
209 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000210 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drhda93d232003-03-31 02:12:46 +0000211 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000212 goto trigger_cleanup;
213 }
danielk1977ef2cb632004-05-29 02:37:19 +0000214 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000215 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drhda93d232003-03-31 02:12:46 +0000216 " trigger on table: %S", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000217 goto trigger_cleanup;
218 }
danielk1977ef2cb632004-05-29 02:37:19 +0000219
drhd24cc422003-03-27 12:51:24 +0000220#ifndef SQLITE_OMIT_AUTHORIZATION
danc9461ec2018-08-29 21:00:16 +0000221 if( !IN_RENAME_OBJECT ){
drha0daa752016-09-16 11:53:10 +0000222 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drhd24cc422003-03-27 12:51:24 +0000223 int code = SQLITE_CREATE_TRIGGER;
drh69c33822016-08-18 14:33:11 +0000224 const char *zDb = db->aDb[iTabDb].zDbSName;
225 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
danielk1977da184232006-01-05 11:34:32 +0000226 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19 +0000227 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24 +0000228 goto trigger_cleanup;
229 }
danielk1977da184232006-01-05 11:34:32 +0000230 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24 +0000231 goto trigger_cleanup;
232 }
233 }
234#endif
danielk1977d702fcc2002-05-26 23:24:40 +0000235
danielk1977ef2cb632004-05-29 02:37:19 +0000236 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04 +0000237 ** cannot appear on views. So we might as well translate every
238 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
239 ** elsewhere.
240 */
danielk1977d702fcc2002-05-26 23:24:40 +0000241 if (tr_tm == TK_INSTEAD){
242 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12 +0000243 }
244
245 /* Build the Trigger object */
drh17435752007-08-16 04:30:38 +0000246 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19 +0000247 if( pTrigger==0 ) goto trigger_cleanup;
dan165921a2009-08-28 18:53:45 +0000248 pTrigger->zName = zName;
drhe5f9c642003-01-13 23:27:31 +0000249 zName = 0;
drh17435752007-08-16 04:30:38 +0000250 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32 +0000251 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48 +0000252 pTrigger->pTabSchema = pTab->pSchema;
drhaa78bec2008-12-09 03:55:14 +0000253 pTrigger->op = (u8)op;
drhdca76842004-12-07 14:06:13 +0000254 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
danc9461ec2018-08-29 21:00:16 +0000255 if( IN_RENAME_OBJECT ){
256 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
dan5496d6a2018-08-13 17:14:26 +0000257 pTrigger->pWhen = pWhen;
258 pWhen = 0;
259 }else{
260 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
261 }
262 pTrigger->pColumns = pColumns;
263 pColumns = 0;
drhf0f258b2003-04-21 18:48:45 +0000264 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19 +0000265 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45 +0000266
267trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000268 sqlite3DbFree(db, zName);
269 sqlite3SrcListDelete(db, pTableName);
270 sqlite3IdListDelete(db, pColumns);
271 sqlite3ExprDelete(db, pWhen);
danielk1977d5d56522005-03-16 12:15:20 +0000272 if( !pParse->pNewTrigger ){
drh633e6d52008-07-28 19:34:53 +0000273 sqlite3DeleteTrigger(db, pTrigger);
danielk1977d5d56522005-03-16 12:15:20 +0000274 }else{
275 assert( pParse->pNewTrigger==pTrigger );
276 }
drhf0f258b2003-04-21 18:48:45 +0000277}
278
279/*
280** This routine is called after all of the trigger actions have been parsed
281** in order to complete the process of building the trigger.
282*/
danielk19774adee202004-05-08 08:23:19 +0000283void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45 +0000284 Parse *pParse, /* Parser context */
285 TriggerStep *pStepList, /* The triggered program */
286 Token *pAll /* Token that describes the complete CREATE TRIGGER */
287){
drha8c62df2010-02-15 15:47:18 +0000288 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
289 char *zName; /* Name of trigger */
290 sqlite3 *db = pParse->db; /* The database */
291 DbFixer sFix; /* Fixer object */
292 int iDb; /* Database containing the trigger */
293 Token nameToken; /* Trigger name for error reporting */
drhf0f258b2003-04-21 18:48:45 +0000294
drhf0f258b2003-04-21 18:48:45 +0000295 pParse->pNewTrigger = 0;
drh9ab4c2e2009-05-09 00:18:38 +0000296 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
dan165921a2009-08-28 18:53:45 +0000297 zName = pTrig->zName;
danielk1977da184232006-01-05 11:34:32 +0000298 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32 +0000299 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53 +0000300 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32 +0000301 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53 +0000302 pStepList = pStepList->pNext;
303 }
drh40aced52016-01-22 17:48:09 +0000304 sqlite3TokenInit(&nameToken, pTrig->zName);
drhd100f692013-10-03 15:39:44 +0000305 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
306 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
dan46539d72013-10-03 12:29:38 +0000307 || sqlite3FixExpr(&sFix, pTrig->pWhen)
drhd100f692013-10-03 15:39:44 +0000308 ){
drhf26e09c2003-05-31 16:21:12 +0000309 goto triggerfinish_cleanup;
310 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000311
dan5496d6a2018-08-13 17:14:26 +0000312#ifndef SQLITE_OMIT_ALTERTABLE
danc9461ec2018-08-29 21:00:16 +0000313 if( IN_RENAME_OBJECT ){
dan5496d6a2018-08-13 17:14:26 +0000314 assert( !db->init.busy );
315 pParse->pNewTrigger = pTrig;
316 pTrig = 0;
317 }else
318#endif
319
drha8c62df2010-02-15 15:47:18 +0000320 /* if we are not initializing,
drh1e32bed2020-06-19 13:33:53 +0000321 ** build the sqlite_schema entry
drh9adf9ac2002-05-15 11:44:13 +0000322 */
drh1d85d932004-02-14 23:05:52 +0000323 if( !db->init.busy ){
drhc977f7f2002-05-21 11:38:11 +0000324 Vdbe *v;
drh2d401ab2008-01-10 23:50:11 +0000325 char *z;
danielk1977c3f9bad2002-05-15 08:30:12 +0000326
drh1e32bed2020-06-19 13:33:53 +0000327 /* Make an entry in the sqlite_schema table */
danielk19774adee202004-05-08 08:23:19 +0000328 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000329 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000330 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11 +0000331 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
drhd96cc6f2017-06-08 14:35:21 +0000332 testcase( z==0 );
drh2d401ab2008-01-10 23:50:11 +0000333 sqlite3NestedParse(pParse,
drh346a70c2020-06-15 20:27:35 +0000334 "INSERT INTO %Q." DFLT_SCHEMA_TABLE
335 " VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
336 db->aDb[iDb].zDbSName, zName,
drh2d401ab2008-01-10 23:50:11 +0000337 pTrig->table, z);
drh633e6d52008-07-28 19:34:53 +0000338 sqlite3DbFree(db, z);
drh9cbf3422008-01-17 16:22:13 +0000339 sqlite3ChangeCookie(pParse, iDb);
drh5d9c9da2011-06-03 20:11:17 +0000340 sqlite3VdbeAddParseSchemaOp(v, iDb,
341 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
danielk1977c3f9bad2002-05-15 08:30:12 +0000342 }
343
drh956bc922004-07-24 17:38:29 +0000344 if( db->init.busy ){
danielk19772f886d12009-02-28 10:47:41 +0000345 Trigger *pLink = pTrig;
346 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
drh21206082011-04-04 18:22:02 +0000347 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh55f66b32019-07-16 19:44:32 +0000348 assert( pLink!=0 );
drhacbcb7e2014-08-21 20:26:37 +0000349 pTrig = sqlite3HashInsert(pHash, zName, pTrig);
danielk19772f886d12009-02-28 10:47:41 +0000350 if( pTrig ){
drh4a642b62016-02-05 01:55:27 +0000351 sqlite3OomFault(db);
danielk19772f886d12009-02-28 10:47:41 +0000352 }else if( pLink->pSchema==pLink->pTabSchema ){
353 Table *pTab;
drhacbcb7e2014-08-21 20:26:37 +0000354 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
danielk19772f886d12009-02-28 10:47:41 +0000355 assert( pTab!=0 );
356 pLink->pNext = pTab->pTrigger;
357 pTab->pTrigger = pLink;
danielk1977d5d56522005-03-16 12:15:20 +0000358 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000359 }
360
drhf0f258b2003-04-21 18:48:45 +0000361triggerfinish_cleanup:
drh633e6d52008-07-28 19:34:53 +0000362 sqlite3DeleteTrigger(db, pTrig);
danc9461ec2018-08-29 21:00:16 +0000363 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
drh633e6d52008-07-28 19:34:53 +0000364 sqlite3DeleteTriggerStep(db, pStepList);
drh4b59ab52002-08-24 18:24:51 +0000365}
danielk1977c3f9bad2002-05-15 08:30:12 +0000366
drh4b59ab52002-08-24 18:24:51 +0000367/*
drhf259df52017-12-27 20:38:35 +0000368** Duplicate a range of text from an SQL statement, then convert all
369** whitespace characters into ordinary space characters.
370*/
371static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
372 char *z = sqlite3DbSpanDup(db, zStart, zEnd);
373 int i;
374 if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i]) ) z[i] = ' ';
375 return z;
376}
377
378/*
drhc977f7f2002-05-21 11:38:11 +0000379** Turn a SELECT statement (that the pSelect parameter points to) into
380** a trigger step. Return a pointer to a TriggerStep structure.
381**
382** The parser calls this routine when it finds a SELECT statement in
383** body of a TRIGGER.
384*/
drhf259df52017-12-27 20:38:35 +0000385TriggerStep *sqlite3TriggerSelectStep(
386 sqlite3 *db, /* Database connection */
387 Select *pSelect, /* The SELECT statement */
388 const char *zStart, /* Start of SQL text */
389 const char *zEnd /* End of SQL text */
390){
drh17435752007-08-16 04:30:38 +0000391 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000392 if( pTriggerStep==0 ) {
drh633e6d52008-07-28 19:34:53 +0000393 sqlite3SelectDelete(db, pSelect);
danielk19772e588c72005-12-09 14:25:08 +0000394 return 0;
395 }
danielk1977633ed082002-05-17 00:05:58 +0000396 pTriggerStep->op = TK_SELECT;
397 pTriggerStep->pSelect = pSelect;
398 pTriggerStep->orconf = OE_Default;
drhf259df52017-12-27 20:38:35 +0000399 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
drhb7916a72009-05-27 10:31:29 +0000400 return pTriggerStep;
401}
danielk1977c3f9bad2002-05-15 08:30:12 +0000402
drhb7916a72009-05-27 10:31:29 +0000403/*
404** Allocate space to hold a new trigger step. The allocated space
405** holds both the TriggerStep object and the TriggerStep.target.z string.
406**
407** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
408*/
409static TriggerStep *triggerStepAllocate(
danc9461ec2018-08-29 21:00:16 +0000410 Parse *pParse, /* Parser context */
shane5eff7cf2009-08-10 03:57:58 +0000411 u8 op, /* Trigger opcode */
drhf259df52017-12-27 20:38:35 +0000412 Token *pName, /* The target name */
413 const char *zStart, /* Start of SQL text */
414 const char *zEnd /* End of SQL text */
drhb7916a72009-05-27 10:31:29 +0000415){
danc9461ec2018-08-29 21:00:16 +0000416 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000417 TriggerStep *pTriggerStep;
418
dan46408352015-04-21 16:38:49 +0000419 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
drhb7916a72009-05-27 10:31:29 +0000420 if( pTriggerStep ){
421 char *z = (char*)&pTriggerStep[1];
422 memcpy(z, pName->z, pName->n);
dan46408352015-04-21 16:38:49 +0000423 sqlite3Dequote(z);
424 pTriggerStep->zTarget = z;
drhb7916a72009-05-27 10:31:29 +0000425 pTriggerStep->op = op;
drhf259df52017-12-27 20:38:35 +0000426 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
danc9461ec2018-08-29 21:00:16 +0000427 if( IN_RENAME_OBJECT ){
428 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
429 }
drhb7916a72009-05-27 10:31:29 +0000430 }
danielk1977633ed082002-05-17 00:05:58 +0000431 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000432}
433
drhc977f7f2002-05-21 11:38:11 +0000434/*
435** Build a trigger step out of an INSERT statement. Return a pointer
436** to the new trigger step.
437**
438** The parser calls this routine when it sees an INSERT inside the
439** body of a trigger.
440*/
danielk19774adee202004-05-08 08:23:19 +0000441TriggerStep *sqlite3TriggerInsertStep(
dan5be60c52018-08-15 20:28:39 +0000442 Parse *pParse, /* Parser */
drhc977f7f2002-05-21 11:38:11 +0000443 Token *pTableName, /* Name of the table into which we insert */
444 IdList *pColumn, /* List of columns in pTableName to insert into */
drhc977f7f2002-05-21 11:38:11 +0000445 Select *pSelect, /* A SELECT statement that supplies values */
drhf259df52017-12-27 20:38:35 +0000446 u8 orconf, /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
drh46d2e5c2018-04-12 13:15:43 +0000447 Upsert *pUpsert, /* ON CONFLICT clauses for upsert */
drhf259df52017-12-27 20:38:35 +0000448 const char *zStart, /* Start of SQL text */
449 const char *zEnd /* End of SQL text */
danielk1977633ed082002-05-17 00:05:58 +0000450){
dan5be60c52018-08-15 20:28:39 +0000451 sqlite3 *db = pParse->db;
drhf3a65f72007-08-22 20:18:21 +0000452 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000453
drh75593d92014-01-10 20:46:55 +0000454 assert(pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000455
danc9461ec2018-08-29 21:00:16 +0000456 pTriggerStep = triggerStepAllocate(pParse, TK_INSERT, pTableName,zStart,zEnd);
danielk1977d5d56522005-03-16 12:15:20 +0000457 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000458 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000459 pTriggerStep->pSelect = pSelect;
460 pSelect = 0;
461 }else{
462 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
463 }
danielk1977d5d56522005-03-16 12:15:20 +0000464 pTriggerStep->pIdList = pColumn;
drh46d2e5c2018-04-12 13:15:43 +0000465 pTriggerStep->pUpsert = pUpsert;
danielk1977d5d56522005-03-16 12:15:20 +0000466 pTriggerStep->orconf = orconf;
dan9105fd52019-08-19 17:26:32 +0000467 if( pUpsert ){
468 sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
469 }
danielk1977d5d56522005-03-16 12:15:20 +0000470 }else{
drh99160482018-04-18 01:34:39 +0000471 testcase( pColumn );
drh633e6d52008-07-28 19:34:53 +0000472 sqlite3IdListDelete(db, pColumn);
drh99160482018-04-18 01:34:39 +0000473 testcase( pUpsert );
drh46d2e5c2018-04-12 13:15:43 +0000474 sqlite3UpsertDelete(db, pUpsert);
danielk1977d5d56522005-03-16 12:15:20 +0000475 }
drh33e619f2009-05-28 01:00:55 +0000476 sqlite3SelectDelete(db, pSelect);
danielk1977c3f9bad2002-05-15 08:30:12 +0000477
danielk1977633ed082002-05-17 00:05:58 +0000478 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000479}
480
drhc977f7f2002-05-21 11:38:11 +0000481/*
482** Construct a trigger step that implements an UPDATE statement and return
483** a pointer to that trigger step. The parser calls this routine when it
484** sees an UPDATE statement inside the body of a CREATE TRIGGER.
485*/
danielk19774adee202004-05-08 08:23:19 +0000486TriggerStep *sqlite3TriggerUpdateStep(
dan5be60c52018-08-15 20:28:39 +0000487 Parse *pParse, /* Parser */
drhc977f7f2002-05-21 11:38:11 +0000488 Token *pTableName, /* Name of the table to be updated */
489 ExprList *pEList, /* The SET clause: list of column and new values */
490 Expr *pWhere, /* The WHERE clause */
drhf259df52017-12-27 20:38:35 +0000491 u8 orconf, /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
492 const char *zStart, /* Start of SQL text */
493 const char *zEnd /* End of SQL text */
drhc977f7f2002-05-21 11:38:11 +0000494){
dan5be60c52018-08-15 20:28:39 +0000495 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000496 TriggerStep *pTriggerStep;
497
danc9461ec2018-08-29 21:00:16 +0000498 pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE, pTableName,zStart,zEnd);
drh33e619f2009-05-28 01:00:55 +0000499 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000500 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000501 pTriggerStep->pExprList = pEList;
502 pTriggerStep->pWhere = pWhere;
503 pEList = 0;
504 pWhere = 0;
505 }else{
506 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
507 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
508 }
drh33e619f2009-05-28 01:00:55 +0000509 pTriggerStep->orconf = orconf;
drh0e3a6f32007-03-30 20:40:34 +0000510 }
drh33e619f2009-05-28 01:00:55 +0000511 sqlite3ExprListDelete(db, pEList);
512 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000513 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000514}
515
drhc977f7f2002-05-21 11:38:11 +0000516/*
517** Construct a trigger step that implements a DELETE statement and return
518** a pointer to that trigger step. The parser calls this routine when it
519** sees a DELETE statement inside the body of a CREATE TRIGGER.
520*/
drh17435752007-08-16 04:30:38 +0000521TriggerStep *sqlite3TriggerDeleteStep(
dan5be60c52018-08-15 20:28:39 +0000522 Parse *pParse, /* Parser */
drh17435752007-08-16 04:30:38 +0000523 Token *pTableName, /* The table from which rows are deleted */
drhf259df52017-12-27 20:38:35 +0000524 Expr *pWhere, /* The WHERE clause */
525 const char *zStart, /* Start of SQL text */
526 const char *zEnd /* End of SQL text */
drh17435752007-08-16 04:30:38 +0000527){
dan5be60c52018-08-15 20:28:39 +0000528 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000529 TriggerStep *pTriggerStep;
530
danc9461ec2018-08-29 21:00:16 +0000531 pTriggerStep = triggerStepAllocate(pParse, TK_DELETE, pTableName,zStart,zEnd);
drh33e619f2009-05-28 01:00:55 +0000532 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000533 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000534 pTriggerStep->pWhere = pWhere;
535 pWhere = 0;
536 }else{
537 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
538 }
drh33e619f2009-05-28 01:00:55 +0000539 pTriggerStep->orconf = OE_Default;
drhb63a53d2007-03-31 01:34:44 +0000540 }
drh33e619f2009-05-28 01:00:55 +0000541 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000542 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000543}
544
danielk1977633ed082002-05-17 00:05:58 +0000545/*
546** Recursively delete a Trigger structure
547*/
drh633e6d52008-07-28 19:34:53 +0000548void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
drhf0f258b2003-04-21 18:48:45 +0000549 if( pTrigger==0 ) return;
drh633e6d52008-07-28 19:34:53 +0000550 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
dan165921a2009-08-28 18:53:45 +0000551 sqlite3DbFree(db, pTrigger->zName);
drh633e6d52008-07-28 19:34:53 +0000552 sqlite3DbFree(db, pTrigger->table);
553 sqlite3ExprDelete(db, pTrigger->pWhen);
554 sqlite3IdListDelete(db, pTrigger->pColumns);
drh633e6d52008-07-28 19:34:53 +0000555 sqlite3DbFree(db, pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000556}
557
558/*
danielk19778a414492004-06-29 08:59:35 +0000559** This function is called to drop a trigger from the database schema.
560**
561** This may be called directly from the parser and therefore identifies
562** the trigger by name. The sqlite3DropTriggerPtr() routine does the
563** same job as this routine except it takes a pointer to the trigger
564** instead of the trigger name.
565**/
drhfdd48a72006-09-11 23:45:48 +0000566void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000567 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000568 int i;
569 const char *zDb;
570 const char *zName;
drh9bb575f2004-09-06 17:24:11 +0000571 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000572
drh17435752007-08-16 04:30:38 +0000573 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000574 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000575 goto drop_trigger_cleanup;
576 }
danielk19778e227872004-06-07 07:52:17 +0000577
drhd24cc422003-03-27 12:51:24 +0000578 assert( pName->nSrc==1 );
579 zDb = pName->a[0].zDatabase;
580 zName = pName->a[0].zName;
drh21206082011-04-04 18:22:02 +0000581 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
danielk197753c0f742005-03-29 03:10:59 +0000582 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000583 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
dan465c2b82020-03-21 15:10:40 +0000584 if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
drh21206082011-04-04 18:22:02 +0000585 assert( sqlite3SchemaMutexHeld(db, j, 0) );
drhacbcb7e2014-08-21 20:26:37 +0000586 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
drhd24cc422003-03-27 12:51:24 +0000587 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000588 }
drhd24cc422003-03-27 12:51:24 +0000589 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000590 if( !noErr ){
591 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
dan57966752011-04-09 17:32:58 +0000592 }else{
593 sqlite3CodeVerifyNamedSchema(pParse, zDb);
drhfdd48a72006-09-11 23:45:48 +0000594 }
dan1db95102010-06-28 10:15:19 +0000595 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +0000596 goto drop_trigger_cleanup;
597 }
drh74161702006-02-24 02:53:49 +0000598 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000599
600drop_trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000601 sqlite3SrcListDelete(db, pName);
drh79a519c2003-05-17 19:04:03 +0000602}
603
604/*
drh956bc922004-07-24 17:38:29 +0000605** Return a pointer to the Table structure for the table that a trigger
606** is set on.
607*/
drh74161702006-02-24 02:53:49 +0000608static Table *tableOfTrigger(Trigger *pTrigger){
drhacbcb7e2014-08-21 20:26:37 +0000609 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
drh956bc922004-07-24 17:38:29 +0000610}
611
612
613/*
drh74161702006-02-24 02:53:49 +0000614** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000615*/
drh74161702006-02-24 02:53:49 +0000616void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000617 Table *pTable;
618 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000619 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000620 int iDb;
drh79a519c2003-05-17 19:04:03 +0000621
danielk1977da184232006-01-05 11:34:32 +0000622 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000623 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000624 pTable = tableOfTrigger(pTrigger);
drh6397a782019-08-27 10:05:45 +0000625 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000626#ifndef SQLITE_OMIT_AUTHORIZATION
drh6397a782019-08-27 10:05:45 +0000627 if( pTable ){
drhe5f9c642003-01-13 23:27:31 +0000628 int code = SQLITE_DROP_TRIGGER;
drh69c33822016-08-18 14:33:11 +0000629 const char *zDb = db->aDb[iDb].zDbSName;
drh956bc922004-07-24 17:38:29 +0000630 const char *zTab = SCHEMA_TABLE(iDb);
631 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
dan2bd93512009-08-31 08:22:46 +0000632 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
danielk19774adee202004-05-08 08:23:19 +0000633 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000634 return;
drhe5f9c642003-01-13 23:27:31 +0000635 }
drhed6c8672003-01-12 18:02:16 +0000636 }
drhe5f9c642003-01-13 23:27:31 +0000637#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000638
drhf0f258b2003-04-21 18:48:45 +0000639 /* Generate code to destroy the database record of the trigger.
640 */
drh43617e92006-03-06 20:55:46 +0000641 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drh8c8dddc2015-12-09 16:26:38 +0000642 sqlite3NestedParse(pParse,
drh346a70c2020-06-15 20:27:35 +0000643 "DELETE FROM %Q." DFLT_SCHEMA_TABLE " WHERE name=%Q AND type='trigger'",
644 db->aDb[iDb].zDbSName, pTrigger->zName
drh8c8dddc2015-12-09 16:26:38 +0000645 );
drh9cbf3422008-01-17 16:22:13 +0000646 sqlite3ChangeCookie(pParse, iDb);
dan165921a2009-08-28 18:53:45 +0000647 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
drhf0f258b2003-04-21 18:48:45 +0000648 }
drh956bc922004-07-24 17:38:29 +0000649}
drhf0f258b2003-04-21 18:48:45 +0000650
drh956bc922004-07-24 17:38:29 +0000651/*
652** Remove a trigger from the hash tables of the sqlite* pointer.
653*/
654void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
655 Trigger *pTrigger;
drh21206082011-04-04 18:22:02 +0000656 Hash *pHash;
657
658 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
659 pHash = &(db->aDb[iDb].pSchema->trigHash);
drhacbcb7e2014-08-21 20:26:37 +0000660 pTrigger = sqlite3HashInsert(pHash, zName, 0);
drh9ab4c2e2009-05-09 00:18:38 +0000661 if( ALWAYS(pTrigger) ){
danielk19772f886d12009-02-28 10:47:41 +0000662 if( pTrigger->pSchema==pTrigger->pTabSchema ){
663 Table *pTab = tableOfTrigger(pTrigger);
drh6397a782019-08-27 10:05:45 +0000664 if( pTab ){
665 Trigger **pp;
dan0232dad2019-12-03 03:34:06 +0000666 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
667 if( *pp==pTrigger ){
668 *pp = (*pp)->pNext;
669 break;
670 }
671 }
drh6397a782019-08-27 10:05:45 +0000672 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000673 }
drh633e6d52008-07-28 19:34:53 +0000674 sqlite3DeleteTrigger(db, pTrigger);
drh8257aa82017-07-26 19:59:13 +0000675 db->mDbFlags |= DBFLAG_SchemaChange;
danielk1977c3f9bad2002-05-15 08:30:12 +0000676 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000677}
678
drhc977f7f2002-05-21 11:38:11 +0000679/*
680** pEList is the SET clause of an UPDATE statement. Each entry
681** in pEList is of the format <id>=<expr>. If any of the entries
682** in pEList have an <id> which matches an identifier in pIdList,
683** then return TRUE. If pIdList==NULL, then it is considered a
684** wildcard that matches anything. Likewise if pEList==NULL then
685** it matches anything so always return true. Return false only
686** if there is no match.
687*/
drh9ab4c2e2009-05-09 00:18:38 +0000688static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000689 int e;
drh9ab4c2e2009-05-09 00:18:38 +0000690 if( pIdList==0 || NEVER(pEList==0) ) return 1;
drhad2d8302002-05-24 20:31:36 +0000691 for(e=0; e<pEList->nExpr; e++){
drh41cee662019-12-12 20:22:34 +0000692 if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000693 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000694 return 0;
695}
696
danielk1977c3f9bad2002-05-15 08:30:12 +0000697/*
danielk19772f886d12009-02-28 10:47:41 +0000698** Return a list of all triggers on table pTab if there exists at least
699** one trigger that must be fired when an operation of type 'op' is
700** performed on the table, and, if that operation is an UPDATE, if at
701** least one of the columns in pChanges is being modified.
drhdca76842004-12-07 14:06:13 +0000702*/
danielk19772f886d12009-02-28 10:47:41 +0000703Trigger *sqlite3TriggersExist(
704 Parse *pParse, /* Parse context */
drhdca76842004-12-07 14:06:13 +0000705 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000706 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
danielk19772f886d12009-02-28 10:47:41 +0000707 ExprList *pChanges, /* Columns that change in an UPDATE statement */
708 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
drhc977f7f2002-05-21 11:38:11 +0000709){
drhdca76842004-12-07 14:06:13 +0000710 int mask = 0;
drhe83cafd2011-03-21 17:15:58 +0000711 Trigger *pList = 0;
danielk19772f886d12009-02-28 10:47:41 +0000712 Trigger *p;
drhe83cafd2011-03-21 17:15:58 +0000713
714 if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
715 pList = sqlite3TriggerList(pParse, pTab);
716 }
danielk19772f886d12009-02-28 10:47:41 +0000717 assert( pList==0 || IsVirtual(pTab)==0 );
718 for(p=pList; p; p=p->pNext){
drh9ab4c2e2009-05-09 00:18:38 +0000719 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
danielk19772f886d12009-02-28 10:47:41 +0000720 mask |= p->tr_tm;
danielk1977c3f9bad2002-05-15 08:30:12 +0000721 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000722 }
danielk19772f886d12009-02-28 10:47:41 +0000723 if( pMask ){
724 *pMask = mask;
725 }
726 return (mask ? pList : 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000727}
728
drhc977f7f2002-05-21 11:38:11 +0000729/*
dan46408352015-04-21 16:38:49 +0000730** Convert the pStep->zTarget string into a SrcList and return a pointer
drhf26e09c2003-05-31 16:21:12 +0000731** to that SrcList.
732**
733** This routine adds a specific database name, if needed, to the target when
734** forming the SrcList. This prevents a trigger in one database from
735** referring to a target in another database. An exception is when the
736** trigger is in TEMP in which case it can refer to any other database it
737** wants.
738*/
739static SrcList *targetSrcList(
740 Parse *pParse, /* The parsing context */
741 TriggerStep *pStep /* The trigger containing the target token */
742){
dan46408352015-04-21 16:38:49 +0000743 sqlite3 *db = pParse->db;
drhf26e09c2003-05-31 16:21:12 +0000744 int iDb; /* Index of the database to use */
745 SrcList *pSrc; /* SrcList to be returned */
746
drh29c992c2019-01-17 15:40:41 +0000747 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
drhb7916a72009-05-27 10:31:29 +0000748 if( pSrc ){
749 assert( pSrc->nSrc>0 );
dan46408352015-04-21 16:38:49 +0000750 pSrc->a[pSrc->nSrc-1].zName = sqlite3DbStrDup(db, pStep->zTarget);
751 iDb = sqlite3SchemaToIndex(db, pStep->pTrig->pSchema);
drhb7916a72009-05-27 10:31:29 +0000752 if( iDb==0 || iDb>=2 ){
drh69c33822016-08-18 14:33:11 +0000753 const char *zDb;
dan46408352015-04-21 16:38:49 +0000754 assert( iDb<db->nDb );
drh69c33822016-08-18 14:33:11 +0000755 zDb = db->aDb[iDb].zDbSName;
756 pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, zDb);
drhb7916a72009-05-27 10:31:29 +0000757 }
drhf26e09c2003-05-31 16:21:12 +0000758 }
759 return pSrc;
760}
761
762/*
dan65a7cd12009-09-01 12:16:01 +0000763** Generate VDBE code for the statements inside the body of a single
764** trigger.
drhc977f7f2002-05-21 11:38:11 +0000765*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000766static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000767 Parse *pParse, /* The parser context */
768 TriggerStep *pStepList, /* List of statements inside the trigger body */
dan65a7cd12009-09-01 12:16:01 +0000769 int orconf /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000770){
dan65a7cd12009-09-01 12:16:01 +0000771 TriggerStep *pStep;
drh344737f2004-09-19 00:50:20 +0000772 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000773 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000774
dan65a7cd12009-09-01 12:16:01 +0000775 assert( pParse->pTriggerTab && pParse->pToplevel );
776 assert( pStepList );
drh344737f2004-09-19 00:50:20 +0000777 assert( v!=0 );
dan65a7cd12009-09-01 12:16:01 +0000778 for(pStep=pStepList; pStep; pStep=pStep->pNext){
dan165921a2009-08-28 18:53:45 +0000779 /* Figure out the ON CONFLICT policy that will be used for this step
780 ** of the trigger program. If the statement that caused this trigger
781 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
782 ** the ON CONFLICT policy that was specified as part of the trigger
783 ** step statement. Example:
784 **
785 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
786 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
787 ** END;
788 **
789 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
790 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
791 */
shanecea72b22009-09-07 04:38:36 +0000792 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
drhaceb31b2014-02-08 01:40:27 +0000793 assert( pParse->okConstFactor==0 );
danf78baaf2012-12-06 19:37:22 +0000794
drhf259df52017-12-27 20:38:35 +0000795#ifndef SQLITE_OMIT_TRACE
drhe307e112017-12-27 21:30:34 +0000796 if( pStep->zSpan ){
797 sqlite3VdbeAddOp4(v, OP_Trace, 0x7fffffff, 1, 0,
798 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
799 P4_DYNAMIC);
800 }
drhf259df52017-12-27 20:38:35 +0000801#endif
802
dan165921a2009-08-28 18:53:45 +0000803 switch( pStep->op ){
danielk1977633ed082002-05-17 00:05:58 +0000804 case TK_UPDATE: {
dan165921a2009-08-28 18:53:45 +0000805 sqlite3Update(pParse,
806 targetSrcList(pParse, pStep),
807 sqlite3ExprListDup(db, pStep->pExprList, 0),
808 sqlite3ExprDup(db, pStep->pWhere, 0),
drheac9fab2018-04-16 13:00:50 +0000809 pParse->eOrconf, 0, 0, 0
dan165921a2009-08-28 18:53:45 +0000810 );
danielk1977633ed082002-05-17 00:05:58 +0000811 break;
812 }
813 case TK_INSERT: {
dan165921a2009-08-28 18:53:45 +0000814 sqlite3Insert(pParse,
815 targetSrcList(pParse, pStep),
dan165921a2009-08-28 18:53:45 +0000816 sqlite3SelectDup(db, pStep->pSelect, 0),
817 sqlite3IdListDup(db, pStep->pIdList),
drh46d2e5c2018-04-12 13:15:43 +0000818 pParse->eOrconf,
819 sqlite3UpsertDup(db, pStep->pUpsert)
dan165921a2009-08-28 18:53:45 +0000820 );
danielk1977633ed082002-05-17 00:05:58 +0000821 break;
822 }
823 case TK_DELETE: {
dan165921a2009-08-28 18:53:45 +0000824 sqlite3DeleteFrom(pParse,
825 targetSrcList(pParse, pStep),
drh8c0833f2017-11-14 23:48:23 +0000826 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
dan165921a2009-08-28 18:53:45 +0000827 );
danielk1977633ed082002-05-17 00:05:58 +0000828 break;
829 }
dan165921a2009-08-28 18:53:45 +0000830 default: assert( pStep->op==TK_SELECT ); {
831 SelectDest sDest;
832 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
833 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
834 sqlite3Select(pParse, pSelect, &sDest);
835 sqlite3SelectDelete(db, pSelect);
drh9ab4c2e2009-05-09 00:18:38 +0000836 break;
837 }
danielk1977633ed082002-05-17 00:05:58 +0000838 }
dan76d462e2009-08-30 11:42:51 +0000839 if( pStep->op!=TK_SELECT ){
drhb7f1d9a2009-09-08 02:27:58 +0000840 sqlite3VdbeAddOp0(v, OP_ResetCount);
dan76d462e2009-08-30 11:42:51 +0000841 }
danielk1977633ed082002-05-17 00:05:58 +0000842 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000843
danielk1977633ed082002-05-17 00:05:58 +0000844 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000845}
846
drhc7379ce2013-10-30 02:28:23 +0000847#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
dan165921a2009-08-28 18:53:45 +0000848/*
849** This function is used to add VdbeComment() annotations to a VDBE
850** program. It is not used in production code, only for debugging.
851*/
852static const char *onErrorText(int onError){
853 switch( onError ){
854 case OE_Abort: return "abort";
855 case OE_Rollback: return "rollback";
856 case OE_Fail: return "fail";
857 case OE_Replace: return "replace";
858 case OE_Ignore: return "ignore";
859 case OE_Default: return "default";
860 }
861 return "n/a";
862}
863#endif
864
865/*
866** Parse context structure pFrom has just been used to create a sub-vdbe
867** (trigger program). If an error has occurred, transfer error information
868** from pFrom to pTo.
869*/
870static void transferParseError(Parse *pTo, Parse *pFrom){
871 assert( pFrom->zErrMsg==0 || pFrom->nErr );
872 assert( pTo->zErrMsg==0 || pTo->nErr );
873 if( pTo->nErr==0 ){
874 pTo->zErrMsg = pFrom->zErrMsg;
875 pTo->nErr = pFrom->nErr;
drhe06874e2015-04-16 15:47:06 +0000876 pTo->rc = pFrom->rc;
dan165921a2009-08-28 18:53:45 +0000877 }else{
878 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
879 }
880}
881
dan65a7cd12009-09-01 12:16:01 +0000882/*
883** Create and populate a new TriggerPrg object with a sub-program
884** implementing trigger pTrigger with ON CONFLICT policy orconf.
885*/
dan2832ad42009-08-31 15:27:27 +0000886static TriggerPrg *codeRowTrigger(
dan165921a2009-08-28 18:53:45 +0000887 Parse *pParse, /* Current parse context */
888 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +0000889 Table *pTab, /* The table pTrigger is attached to */
890 int orconf /* ON CONFLICT policy to code trigger program with */
dan165921a2009-08-28 18:53:45 +0000891){
dan65a7cd12009-09-01 12:16:01 +0000892 Parse *pTop = sqlite3ParseToplevel(pParse);
893 sqlite3 *db = pParse->db; /* Database handle */
894 TriggerPrg *pPrg; /* Value to return */
dan165921a2009-08-28 18:53:45 +0000895 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
896 Vdbe *v; /* Temporary VM */
dan165921a2009-08-28 18:53:45 +0000897 NameContext sNC; /* Name context for sub-vdbe */
898 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
899 Parse *pSubParse; /* Parse context for sub-vdbe */
900 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
901
dan1da40a32009-09-19 17:00:31 +0000902 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dand19c9332010-07-26 12:05:17 +0000903 assert( pTop->pVdbe );
dan65a7cd12009-09-01 12:16:01 +0000904
905 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
906 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
907 ** list of the top-level Parse object sooner rather than later. */
dan2832ad42009-08-31 15:27:27 +0000908 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
909 if( !pPrg ) return 0;
dan65a7cd12009-09-01 12:16:01 +0000910 pPrg->pNext = pTop->pTriggerPrg;
911 pTop->pTriggerPrg = pPrg;
dan2832ad42009-08-31 15:27:27 +0000912 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
dan165921a2009-08-28 18:53:45 +0000913 if( !pProgram ) return 0;
dand19c9332010-07-26 12:05:17 +0000914 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
dan2832ad42009-08-31 15:27:27 +0000915 pPrg->pTrigger = pTrigger;
916 pPrg->orconf = orconf;
danbb5f1682009-11-27 12:12:34 +0000917 pPrg->aColmask[0] = 0xffffffff;
918 pPrg->aColmask[1] = 0xffffffff;
dan165921a2009-08-28 18:53:45 +0000919
dan65a7cd12009-09-01 12:16:01 +0000920 /* Allocate and populate a new Parse context to use for coding the
921 ** trigger sub-program. */
922 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
923 if( !pSubParse ) return 0;
dan165921a2009-08-28 18:53:45 +0000924 memset(&sNC, 0, sizeof(sNC));
925 sNC.pParse = pSubParse;
926 pSubParse->db = db;
927 pSubParse->pTriggerTab = pTab;
dan65a7cd12009-09-01 12:16:01 +0000928 pSubParse->pToplevel = pTop;
dan2bd93512009-08-31 08:22:46 +0000929 pSubParse->zAuthContext = pTrigger->zName;
dan65a7cd12009-09-01 12:16:01 +0000930 pSubParse->eTriggerOp = pTrigger->op;
drh8b307fb2010-04-06 15:57:05 +0000931 pSubParse->nQueryLoop = pParse->nQueryLoop;
dan1ea04432018-12-21 19:29:11 +0000932 pSubParse->disableVtab = pParse->disableVtab;
dan165921a2009-08-28 18:53:45 +0000933
934 v = sqlite3GetVdbe(pSubParse);
935 if( v ){
dan76d462e2009-08-30 11:42:51 +0000936 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
937 pTrigger->zName, onErrorText(orconf),
dan165921a2009-08-28 18:53:45 +0000938 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
dan65a7cd12009-09-01 12:16:01 +0000939 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
940 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
941 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
dan76d462e2009-08-30 11:42:51 +0000942 pTab->zName
dan165921a2009-08-28 18:53:45 +0000943 ));
dan76d462e2009-08-30 11:42:51 +0000944#ifndef SQLITE_OMIT_TRACE
drhe307e112017-12-27 21:30:34 +0000945 if( pTrigger->zName ){
946 sqlite3VdbeChangeP4(v, -1,
947 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
948 );
949 }
dan76d462e2009-08-30 11:42:51 +0000950#endif
dan165921a2009-08-28 18:53:45 +0000951
dan65a7cd12009-09-01 12:16:01 +0000952 /* If one was specified, code the WHEN clause. If it evaluates to false
953 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
954 ** OP_Halt inserted at the end of the program. */
dan165921a2009-08-28 18:53:45 +0000955 if( pTrigger->pWhen ){
dan165921a2009-08-28 18:53:45 +0000956 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
dan523a0872009-08-31 05:23:32 +0000957 if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
958 && db->mallocFailed==0
959 ){
drhec4ccdb2018-12-29 02:26:59 +0000960 iEndTrigger = sqlite3VdbeMakeLabel(pSubParse);
dan165921a2009-08-28 18:53:45 +0000961 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
962 }
963 sqlite3ExprDelete(db, pWhen);
964 }
965
966 /* Code the trigger program into the sub-vdbe. */
dan76d462e2009-08-30 11:42:51 +0000967 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
dan65a7cd12009-09-01 12:16:01 +0000968
969 /* Insert an OP_Halt at the end of the sub-program. */
dan165921a2009-08-28 18:53:45 +0000970 if( iEndTrigger ){
971 sqlite3VdbeResolveLabel(v, iEndTrigger);
972 }
973 sqlite3VdbeAddOp0(v, OP_Halt);
dan76d462e2009-08-30 11:42:51 +0000974 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
975
dan165921a2009-08-28 18:53:45 +0000976 transferParseError(pParse, pSubParse);
dan08951172017-11-28 20:43:40 +0000977 if( db->mallocFailed==0 && pParse->nErr==0 ){
dan65a7cd12009-09-01 12:16:01 +0000978 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
dan523a0872009-08-31 05:23:32 +0000979 }
dan165921a2009-08-28 18:53:45 +0000980 pProgram->nMem = pSubParse->nMem;
981 pProgram->nCsr = pSubParse->nTab;
982 pProgram->token = (void *)pTrigger;
danbb5f1682009-11-27 12:12:34 +0000983 pPrg->aColmask[0] = pSubParse->oldmask;
984 pPrg->aColmask[1] = pSubParse->newmask;
dan165921a2009-08-28 18:53:45 +0000985 sqlite3VdbeDelete(v);
dan165921a2009-08-28 18:53:45 +0000986 }
dan65a7cd12009-09-01 12:16:01 +0000987
988 assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
989 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
drhf30a9692013-11-15 01:10:18 +0000990 sqlite3ParserReset(pSubParse);
dan165921a2009-08-28 18:53:45 +0000991 sqlite3StackFree(db, pSubParse);
992
dan2832ad42009-08-31 15:27:27 +0000993 return pPrg;
dan165921a2009-08-28 18:53:45 +0000994}
995
dan65a7cd12009-09-01 12:16:01 +0000996/*
997** Return a pointer to a TriggerPrg object containing the sub-program for
998** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
999** TriggerPrg object exists, a new object is allocated and populated before
1000** being returned.
1001*/
dan2832ad42009-08-31 15:27:27 +00001002static TriggerPrg *getRowTrigger(
dan65a7cd12009-09-01 12:16:01 +00001003 Parse *pParse, /* Current parse context */
dan165921a2009-08-28 18:53:45 +00001004 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +00001005 Table *pTab, /* The table trigger pTrigger is attached to */
1006 int orconf /* ON CONFLICT algorithm. */
dan165921a2009-08-28 18:53:45 +00001007){
dan65a7cd12009-09-01 12:16:01 +00001008 Parse *pRoot = sqlite3ParseToplevel(pParse);
dan2832ad42009-08-31 15:27:27 +00001009 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +00001010
dan1da40a32009-09-19 17:00:31 +00001011 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dan165921a2009-08-28 18:53:45 +00001012
1013 /* It may be that this trigger has already been coded (or is in the
1014 ** process of being coded). If this is the case, then an entry with
dan2832ad42009-08-31 15:27:27 +00001015 ** a matching TriggerPrg.pTrigger field will be present somewhere
1016 ** in the Parse.pTriggerPrg list. Search for such an entry. */
dan2832ad42009-08-31 15:27:27 +00001017 for(pPrg=pRoot->pTriggerPrg;
1018 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
1019 pPrg=pPrg->pNext
dan165921a2009-08-28 18:53:45 +00001020 );
1021
dan65a7cd12009-09-01 12:16:01 +00001022 /* If an existing TriggerPrg could not be located, create a new one. */
dan2832ad42009-08-31 15:27:27 +00001023 if( !pPrg ){
dan65a7cd12009-09-01 12:16:01 +00001024 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
dan165921a2009-08-28 18:53:45 +00001025 }
1026
dan2832ad42009-08-31 15:27:27 +00001027 return pPrg;
dan165921a2009-08-28 18:53:45 +00001028}
1029
dan94d7f502009-09-24 09:05:49 +00001030/*
1031** Generate code for the trigger program associated with trigger p on
1032** table pTab. The reg, orconf and ignoreJump parameters passed to this
1033** function are the same as those described in the header function for
1034** sqlite3CodeRowTrigger()
1035*/
dan1da40a32009-09-19 17:00:31 +00001036void sqlite3CodeRowTriggerDirect(
1037 Parse *pParse, /* Parse context */
1038 Trigger *p, /* Trigger to code */
1039 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001040 int reg, /* Reg array containing OLD.* and NEW.* values */
dan1da40a32009-09-19 17:00:31 +00001041 int orconf, /* ON CONFLICT policy */
1042 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
1043){
1044 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
1045 TriggerPrg *pPrg;
1046 pPrg = getRowTrigger(pParse, p, pTab, orconf);
1047 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
1048
1049 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
1050 ** is a pointer to the sub-vdbe containing the trigger program. */
1051 if( pPrg ){
dand19c9332010-07-26 12:05:17 +00001052 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
1053
drh9b34abe2016-01-16 15:12:35 +00001054 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
1055 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
dan1da40a32009-09-19 17:00:31 +00001056 VdbeComment(
1057 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
1058
1059 /* Set the P5 operand of the OP_Program instruction to non-zero if
1060 ** recursive invocation of this trigger program is disallowed. Recursive
1061 ** invocation is disallowed if (a) the sub-program is really a trigger,
1062 ** not a foreign key action, and (b) the flag to enable recursive triggers
1063 ** is clear. */
dand19c9332010-07-26 12:05:17 +00001064 sqlite3VdbeChangeP5(v, (u8)bRecursive);
dan1da40a32009-09-19 17:00:31 +00001065 }
1066}
1067
danielk1977633ed082002-05-17 00:05:58 +00001068/*
dan94d7f502009-09-24 09:05:49 +00001069** This is called to code the required FOR EACH ROW triggers for an operation
1070** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
drhf7b54962013-05-28 12:11:54 +00001071** is given by the op parameter. The tr_tm parameter determines whether the
dan94d7f502009-09-24 09:05:49 +00001072** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
1073** parameter pChanges is passed the list of columns being modified.
danielk1977633ed082002-05-17 00:05:58 +00001074**
dan94d7f502009-09-24 09:05:49 +00001075** If there are no triggers that fire at the specified time for the specified
1076** operation on pTab, this function is a no-op.
drhc977f7f2002-05-21 11:38:11 +00001077**
dan94d7f502009-09-24 09:05:49 +00001078** The reg argument is the address of the first in an array of registers
1079** that contain the values substituted for the new.* and old.* references
1080** in the trigger program. If N is the number of columns in table pTab
1081** (a copy of pTab->nCol), then registers are populated as follows:
drhc977f7f2002-05-21 11:38:11 +00001082**
dan94d7f502009-09-24 09:05:49 +00001083** Register Contains
1084** ------------------------------------------------------
1085** reg+0 OLD.rowid
1086** reg+1 OLD.* value of left-most column of pTab
1087** ... ...
1088** reg+N OLD.* value of right-most column of pTab
1089** reg+N+1 NEW.rowid
1090** reg+N+2 OLD.* value of left-most column of pTab
1091** ... ...
1092** reg+N+N+1 NEW.* value of right-most column of pTab
drhc977f7f2002-05-21 11:38:11 +00001093**
dan94d7f502009-09-24 09:05:49 +00001094** For ON DELETE triggers, the registers containing the NEW.* values will
1095** never be accessed by the trigger program, so they are not allocated or
1096** populated by the caller (there is no data to populate them with anyway).
1097** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
1098** are never accessed, and so are not allocated by the caller. So, for an
1099** ON INSERT trigger, the value passed to this function as parameter reg
1100** is not a readable register, although registers (reg+N) through
1101** (reg+N+N+1) are.
danielk1977633ed082002-05-17 00:05:58 +00001102**
dan94d7f502009-09-24 09:05:49 +00001103** Parameter orconf is the default conflict resolution algorithm for the
1104** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
1105** is the instruction that control should jump to if a trigger program
1106** raises an IGNORE exception.
danielk1977633ed082002-05-17 00:05:58 +00001107*/
dan165921a2009-08-28 18:53:45 +00001108void sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +00001109 Parse *pParse, /* Parse context */
danielk19772f886d12009-02-28 10:47:41 +00001110 Trigger *pTrigger, /* List of triggers on table pTab */
danielk1977633ed082002-05-17 00:05:58 +00001111 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
1112 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +00001113 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +00001114 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001115 int reg, /* The first in an array of registers (see above) */
danielk19776f349032002-06-11 02:25:40 +00001116 int orconf, /* ON CONFLICT policy */
dan165921a2009-08-28 18:53:45 +00001117 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
drhc977f7f2002-05-21 11:38:11 +00001118){
dan94d7f502009-09-24 09:05:49 +00001119 Trigger *p; /* Used to iterate through pTrigger list */
danielk19778f2c54e2008-01-01 19:02:09 +00001120
dan94d7f502009-09-24 09:05:49 +00001121 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
1122 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
1123 assert( (op==TK_UPDATE)==(pChanges!=0) );
danielk1977c3f9bad2002-05-15 08:30:12 +00001124
danielk19772f886d12009-02-28 10:47:41 +00001125 for(p=pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +00001126
drh9ab4c2e2009-05-09 00:18:38 +00001127 /* Sanity checking: The schema for the trigger and for the table are
1128 ** always defined. The trigger must be in the same schema as the table
1129 ** or else it must be a TEMP trigger. */
1130 assert( p->pSchema!=0 );
1131 assert( p->pTabSchema!=0 );
dan165921a2009-08-28 18:53:45 +00001132 assert( p->pSchema==p->pTabSchema
1133 || p->pSchema==pParse->db->aDb[1].pSchema );
drh9ab4c2e2009-05-09 00:18:38 +00001134
danielk1977eecfb3e2006-01-10 12:31:39 +00001135 /* Determine whether we should code this trigger */
dan165921a2009-08-28 18:53:45 +00001136 if( p->op==op
1137 && p->tr_tm==tr_tm
dan94d7f502009-09-24 09:05:49 +00001138 && checkColumnOverlap(p->pColumns, pChanges)
danielk1977eecfb3e2006-01-10 12:31:39 +00001139 ){
dan94d7f502009-09-24 09:05:49 +00001140 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
danielk1977c3f9bad2002-05-15 08:30:12 +00001141 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001142 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001143}
dan165921a2009-08-28 18:53:45 +00001144
dan2832ad42009-08-31 15:27:27 +00001145/*
danbb5f1682009-11-27 12:12:34 +00001146** Triggers may access values stored in the old.* or new.* pseudo-table.
1147** This function returns a 32-bit bitmask indicating which columns of the
1148** old.* or new.* tables actually are used by triggers. This information
1149** may be used by the caller, for example, to avoid having to load the entire
1150** old.* record into memory when executing an UPDATE or DELETE command.
dan2832ad42009-08-31 15:27:27 +00001151**
1152** Bit 0 of the returned mask is set if the left-most column of the
danbb5f1682009-11-27 12:12:34 +00001153** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
dan2832ad42009-08-31 15:27:27 +00001154** the second leftmost column value is required, and so on. If there
1155** are more than 32 columns in the table, and at least one of the columns
1156** with an index greater than 32 may be accessed, 0xffffffff is returned.
1157**
danbb5f1682009-11-27 12:12:34 +00001158** It is not possible to determine if the old.rowid or new.rowid column is
1159** accessed by triggers. The caller must always assume that it is.
dan2832ad42009-08-31 15:27:27 +00001160**
danbb5f1682009-11-27 12:12:34 +00001161** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
1162** applies to the old.* table. If 1, the new.* table.
1163**
1164** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
1165** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
1166** included in the returned mask if the TRIGGER_BEFORE bit is set in the
1167** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
1168** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
dan2832ad42009-08-31 15:27:27 +00001169*/
danbb5f1682009-11-27 12:12:34 +00001170u32 sqlite3TriggerColmask(
dan165921a2009-08-28 18:53:45 +00001171 Parse *pParse, /* Parse context */
1172 Trigger *pTrigger, /* List of triggers on table pTab */
dan165921a2009-08-28 18:53:45 +00001173 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
danbb5f1682009-11-27 12:12:34 +00001174 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
1175 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
dan165921a2009-08-28 18:53:45 +00001176 Table *pTab, /* The table to code triggers from */
dan2832ad42009-08-31 15:27:27 +00001177 int orconf /* Default ON CONFLICT policy for trigger steps */
dan165921a2009-08-28 18:53:45 +00001178){
dan1da40a32009-09-19 17:00:31 +00001179 const int op = pChanges ? TK_UPDATE : TK_DELETE;
dan2832ad42009-08-31 15:27:27 +00001180 u32 mask = 0;
dan165921a2009-08-28 18:53:45 +00001181 Trigger *p;
dan165921a2009-08-28 18:53:45 +00001182
danbb5f1682009-11-27 12:12:34 +00001183 assert( isNew==1 || isNew==0 );
dan165921a2009-08-28 18:53:45 +00001184 for(p=pTrigger; p; p=p->pNext){
danbb5f1682009-11-27 12:12:34 +00001185 if( p->op==op && (tr_tm&p->tr_tm)
1186 && checkColumnOverlap(p->pColumns,pChanges)
1187 ){
dan2832ad42009-08-31 15:27:27 +00001188 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +00001189 pPrg = getRowTrigger(pParse, p, pTab, orconf);
dan2832ad42009-08-31 15:27:27 +00001190 if( pPrg ){
danbb5f1682009-11-27 12:12:34 +00001191 mask |= pPrg->aColmask[isNew];
dan165921a2009-08-28 18:53:45 +00001192 }
1193 }
1194 }
dan2832ad42009-08-31 15:27:27 +00001195
1196 return mask;
dan165921a2009-08-28 18:53:45 +00001197}
1198
drhb7f91642004-10-31 02:22:47 +00001199#endif /* !defined(SQLITE_OMIT_TRIGGER) */