blob: 883eddc2d11093213419d7cb193862d6abf290ca [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. */
drh3d5f74b2009-08-06 17:43:31 +0000160 if( db->init.iDb==1 ){
161 /* Ticket #3810.
162 ** Normally, whenever a table is dropped, all associated triggers are
163 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
164 ** and the table is dropped by a different database connection, the
165 ** trigger is not visible to the database connection that does the
166 ** drop so the trigger cannot be dropped. This results in an
167 ** "orphaned trigger" - a trigger whose associated table is missing.
168 */
169 db->init.orphanTrigger = 1;
170 }
drhd24cc422003-03-27 12:51:24 +0000171 goto trigger_cleanup;
172 }
drh4cbdda92006-06-14 19:00:20 +0000173 if( IsVirtual(pTab) ){
174 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
175 goto trigger_cleanup;
176 }
drhd24cc422003-03-27 12:51:24 +0000177
danielk1977d8123362004-06-12 09:25:12 +0000178 /* Check that the trigger name is not reserved and that no trigger of the
179 ** specified name exists */
drh17435752007-08-16 04:30:38 +0000180 zName = sqlite3NameFromToken(db, pName);
drhc5a93d42019-08-12 00:08:07 +0000181 if( zName==0 ){
182 assert( db->mallocFailed );
183 goto trigger_cleanup;
184 }
185 if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
danielk1977d8123362004-06-12 09:25:12 +0000186 goto trigger_cleanup;
187 }
drh21206082011-04-04 18:22:02 +0000188 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danc9461ec2018-08-29 21:00:16 +0000189 if( !IN_RENAME_OBJECT ){
dan5496d6a2018-08-13 17:14:26 +0000190 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
191 if( !noErr ){
192 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
193 }else{
194 assert( !db->init.busy );
195 sqlite3CodeVerifySchema(pParse, iDb);
196 }
197 goto trigger_cleanup;
drhfdd48a72006-09-11 23:45:48 +0000198 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000199 }
danielk1977ef2cb632004-05-29 02:37:19 +0000200
201 /* Do not create a trigger on a system table */
drhdca76842004-12-07 14:06:13 +0000202 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk19774adee202004-05-08 08:23:19 +0000203 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
drhd24cc422003-03-27 12:51:24 +0000204 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40 +0000205 }
danielk1977ef2cb632004-05-29 02:37:19 +0000206
207 /* INSTEAD of triggers are only for views and views only support INSTEAD
208 ** of triggers.
209 */
210 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000211 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drhda93d232003-03-31 02:12:46 +0000212 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000213 goto trigger_cleanup;
214 }
danielk1977ef2cb632004-05-29 02:37:19 +0000215 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000216 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drhda93d232003-03-31 02:12:46 +0000217 " trigger on table: %S", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000218 goto trigger_cleanup;
219 }
danielk1977ef2cb632004-05-29 02:37:19 +0000220
drhd24cc422003-03-27 12:51:24 +0000221#ifndef SQLITE_OMIT_AUTHORIZATION
danc9461ec2018-08-29 21:00:16 +0000222 if( !IN_RENAME_OBJECT ){
drha0daa752016-09-16 11:53:10 +0000223 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drhd24cc422003-03-27 12:51:24 +0000224 int code = SQLITE_CREATE_TRIGGER;
drh69c33822016-08-18 14:33:11 +0000225 const char *zDb = db->aDb[iTabDb].zDbSName;
226 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
danielk1977da184232006-01-05 11:34:32 +0000227 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19 +0000228 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24 +0000229 goto trigger_cleanup;
230 }
danielk1977da184232006-01-05 11:34:32 +0000231 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24 +0000232 goto trigger_cleanup;
233 }
234 }
235#endif
danielk1977d702fcc2002-05-26 23:24:40 +0000236
danielk1977ef2cb632004-05-29 02:37:19 +0000237 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04 +0000238 ** cannot appear on views. So we might as well translate every
239 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
240 ** elsewhere.
241 */
danielk1977d702fcc2002-05-26 23:24:40 +0000242 if (tr_tm == TK_INSTEAD){
243 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12 +0000244 }
245
246 /* Build the Trigger object */
drh17435752007-08-16 04:30:38 +0000247 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19 +0000248 if( pTrigger==0 ) goto trigger_cleanup;
dan165921a2009-08-28 18:53:45 +0000249 pTrigger->zName = zName;
drhe5f9c642003-01-13 23:27:31 +0000250 zName = 0;
drh17435752007-08-16 04:30:38 +0000251 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32 +0000252 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48 +0000253 pTrigger->pTabSchema = pTab->pSchema;
drhaa78bec2008-12-09 03:55:14 +0000254 pTrigger->op = (u8)op;
drhdca76842004-12-07 14:06:13 +0000255 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
danc9461ec2018-08-29 21:00:16 +0000256 if( IN_RENAME_OBJECT ){
257 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
dan5496d6a2018-08-13 17:14:26 +0000258 pTrigger->pWhen = pWhen;
259 pWhen = 0;
260 }else{
261 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
262 }
263 pTrigger->pColumns = pColumns;
264 pColumns = 0;
drhf0f258b2003-04-21 18:48:45 +0000265 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19 +0000266 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45 +0000267
268trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000269 sqlite3DbFree(db, zName);
270 sqlite3SrcListDelete(db, pTableName);
271 sqlite3IdListDelete(db, pColumns);
272 sqlite3ExprDelete(db, pWhen);
danielk1977d5d56522005-03-16 12:15:20 +0000273 if( !pParse->pNewTrigger ){
drh633e6d52008-07-28 19:34:53 +0000274 sqlite3DeleteTrigger(db, pTrigger);
danielk1977d5d56522005-03-16 12:15:20 +0000275 }else{
276 assert( pParse->pNewTrigger==pTrigger );
277 }
drhf0f258b2003-04-21 18:48:45 +0000278}
279
280/*
281** This routine is called after all of the trigger actions have been parsed
282** in order to complete the process of building the trigger.
283*/
danielk19774adee202004-05-08 08:23:19 +0000284void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45 +0000285 Parse *pParse, /* Parser context */
286 TriggerStep *pStepList, /* The triggered program */
287 Token *pAll /* Token that describes the complete CREATE TRIGGER */
288){
drha8c62df2010-02-15 15:47:18 +0000289 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
290 char *zName; /* Name of trigger */
291 sqlite3 *db = pParse->db; /* The database */
292 DbFixer sFix; /* Fixer object */
293 int iDb; /* Database containing the trigger */
294 Token nameToken; /* Trigger name for error reporting */
drhf0f258b2003-04-21 18:48:45 +0000295
drhf0f258b2003-04-21 18:48:45 +0000296 pParse->pNewTrigger = 0;
drh9ab4c2e2009-05-09 00:18:38 +0000297 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
dan165921a2009-08-28 18:53:45 +0000298 zName = pTrig->zName;
danielk1977da184232006-01-05 11:34:32 +0000299 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32 +0000300 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53 +0000301 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32 +0000302 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53 +0000303 pStepList = pStepList->pNext;
304 }
drh40aced52016-01-22 17:48:09 +0000305 sqlite3TokenInit(&nameToken, pTrig->zName);
drhd100f692013-10-03 15:39:44 +0000306 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
307 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
dan46539d72013-10-03 12:29:38 +0000308 || sqlite3FixExpr(&sFix, pTrig->pWhen)
drhd100f692013-10-03 15:39:44 +0000309 ){
drhf26e09c2003-05-31 16:21:12 +0000310 goto triggerfinish_cleanup;
311 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000312
dan5496d6a2018-08-13 17:14:26 +0000313#ifndef SQLITE_OMIT_ALTERTABLE
danc9461ec2018-08-29 21:00:16 +0000314 if( IN_RENAME_OBJECT ){
dan5496d6a2018-08-13 17:14:26 +0000315 assert( !db->init.busy );
316 pParse->pNewTrigger = pTrig;
317 pTrig = 0;
318 }else
319#endif
320
drha8c62df2010-02-15 15:47:18 +0000321 /* if we are not initializing,
drh1e32bed2020-06-19 13:33:53 +0000322 ** build the sqlite_schema entry
drh9adf9ac2002-05-15 11:44:13 +0000323 */
drh1d85d932004-02-14 23:05:52 +0000324 if( !db->init.busy ){
drhc977f7f2002-05-21 11:38:11 +0000325 Vdbe *v;
drh2d401ab2008-01-10 23:50:11 +0000326 char *z;
danielk1977c3f9bad2002-05-15 08:30:12 +0000327
drh1e32bed2020-06-19 13:33:53 +0000328 /* Make an entry in the sqlite_schema table */
danielk19774adee202004-05-08 08:23:19 +0000329 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000330 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000331 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11 +0000332 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
drhd96cc6f2017-06-08 14:35:21 +0000333 testcase( z==0 );
drh2d401ab2008-01-10 23:50:11 +0000334 sqlite3NestedParse(pParse,
drh346a70c2020-06-15 20:27:35 +0000335 "INSERT INTO %Q." DFLT_SCHEMA_TABLE
336 " VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
337 db->aDb[iDb].zDbSName, zName,
drh2d401ab2008-01-10 23:50:11 +0000338 pTrig->table, z);
drh633e6d52008-07-28 19:34:53 +0000339 sqlite3DbFree(db, z);
drh9cbf3422008-01-17 16:22:13 +0000340 sqlite3ChangeCookie(pParse, iDb);
drh5d9c9da2011-06-03 20:11:17 +0000341 sqlite3VdbeAddParseSchemaOp(v, iDb,
342 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
danielk1977c3f9bad2002-05-15 08:30:12 +0000343 }
344
drh956bc922004-07-24 17:38:29 +0000345 if( db->init.busy ){
danielk19772f886d12009-02-28 10:47:41 +0000346 Trigger *pLink = pTrig;
347 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
drh21206082011-04-04 18:22:02 +0000348 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh55f66b32019-07-16 19:44:32 +0000349 assert( pLink!=0 );
drhacbcb7e2014-08-21 20:26:37 +0000350 pTrig = sqlite3HashInsert(pHash, zName, pTrig);
danielk19772f886d12009-02-28 10:47:41 +0000351 if( pTrig ){
drh4a642b62016-02-05 01:55:27 +0000352 sqlite3OomFault(db);
danielk19772f886d12009-02-28 10:47:41 +0000353 }else if( pLink->pSchema==pLink->pTabSchema ){
354 Table *pTab;
drhacbcb7e2014-08-21 20:26:37 +0000355 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
danielk19772f886d12009-02-28 10:47:41 +0000356 assert( pTab!=0 );
357 pLink->pNext = pTab->pTrigger;
358 pTab->pTrigger = pLink;
danielk1977d5d56522005-03-16 12:15:20 +0000359 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000360 }
361
drhf0f258b2003-04-21 18:48:45 +0000362triggerfinish_cleanup:
drh633e6d52008-07-28 19:34:53 +0000363 sqlite3DeleteTrigger(db, pTrig);
danc9461ec2018-08-29 21:00:16 +0000364 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
drh633e6d52008-07-28 19:34:53 +0000365 sqlite3DeleteTriggerStep(db, pStepList);
drh4b59ab52002-08-24 18:24:51 +0000366}
danielk1977c3f9bad2002-05-15 08:30:12 +0000367
drh4b59ab52002-08-24 18:24:51 +0000368/*
drhf259df52017-12-27 20:38:35 +0000369** Duplicate a range of text from an SQL statement, then convert all
370** whitespace characters into ordinary space characters.
371*/
372static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
373 char *z = sqlite3DbSpanDup(db, zStart, zEnd);
374 int i;
375 if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i]) ) z[i] = ' ';
376 return z;
377}
378
379/*
drhc977f7f2002-05-21 11:38:11 +0000380** Turn a SELECT statement (that the pSelect parameter points to) into
381** a trigger step. Return a pointer to a TriggerStep structure.
382**
383** The parser calls this routine when it finds a SELECT statement in
384** body of a TRIGGER.
385*/
drhf259df52017-12-27 20:38:35 +0000386TriggerStep *sqlite3TriggerSelectStep(
387 sqlite3 *db, /* Database connection */
388 Select *pSelect, /* The SELECT statement */
389 const char *zStart, /* Start of SQL text */
390 const char *zEnd /* End of SQL text */
391){
drh17435752007-08-16 04:30:38 +0000392 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000393 if( pTriggerStep==0 ) {
drh633e6d52008-07-28 19:34:53 +0000394 sqlite3SelectDelete(db, pSelect);
danielk19772e588c72005-12-09 14:25:08 +0000395 return 0;
396 }
danielk1977633ed082002-05-17 00:05:58 +0000397 pTriggerStep->op = TK_SELECT;
398 pTriggerStep->pSelect = pSelect;
399 pTriggerStep->orconf = OE_Default;
drhf259df52017-12-27 20:38:35 +0000400 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
drhb7916a72009-05-27 10:31:29 +0000401 return pTriggerStep;
402}
danielk1977c3f9bad2002-05-15 08:30:12 +0000403
drhb7916a72009-05-27 10:31:29 +0000404/*
405** Allocate space to hold a new trigger step. The allocated space
406** holds both the TriggerStep object and the TriggerStep.target.z string.
407**
408** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
409*/
410static TriggerStep *triggerStepAllocate(
danc9461ec2018-08-29 21:00:16 +0000411 Parse *pParse, /* Parser context */
shane5eff7cf2009-08-10 03:57:58 +0000412 u8 op, /* Trigger opcode */
drhf259df52017-12-27 20:38:35 +0000413 Token *pName, /* The target name */
414 const char *zStart, /* Start of SQL text */
415 const char *zEnd /* End of SQL text */
drhb7916a72009-05-27 10:31:29 +0000416){
danc9461ec2018-08-29 21:00:16 +0000417 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000418 TriggerStep *pTriggerStep;
419
dan46408352015-04-21 16:38:49 +0000420 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
drhb7916a72009-05-27 10:31:29 +0000421 if( pTriggerStep ){
422 char *z = (char*)&pTriggerStep[1];
423 memcpy(z, pName->z, pName->n);
dan46408352015-04-21 16:38:49 +0000424 sqlite3Dequote(z);
425 pTriggerStep->zTarget = z;
drhb7916a72009-05-27 10:31:29 +0000426 pTriggerStep->op = op;
drhf259df52017-12-27 20:38:35 +0000427 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
danc9461ec2018-08-29 21:00:16 +0000428 if( IN_RENAME_OBJECT ){
429 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
430 }
drhb7916a72009-05-27 10:31:29 +0000431 }
danielk1977633ed082002-05-17 00:05:58 +0000432 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000433}
434
drhc977f7f2002-05-21 11:38:11 +0000435/*
436** Build a trigger step out of an INSERT statement. Return a pointer
437** to the new trigger step.
438**
439** The parser calls this routine when it sees an INSERT inside the
440** body of a trigger.
441*/
danielk19774adee202004-05-08 08:23:19 +0000442TriggerStep *sqlite3TriggerInsertStep(
dan5be60c52018-08-15 20:28:39 +0000443 Parse *pParse, /* Parser */
drhc977f7f2002-05-21 11:38:11 +0000444 Token *pTableName, /* Name of the table into which we insert */
445 IdList *pColumn, /* List of columns in pTableName to insert into */
drhc977f7f2002-05-21 11:38:11 +0000446 Select *pSelect, /* A SELECT statement that supplies values */
drhf259df52017-12-27 20:38:35 +0000447 u8 orconf, /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
drh46d2e5c2018-04-12 13:15:43 +0000448 Upsert *pUpsert, /* ON CONFLICT clauses for upsert */
drhf259df52017-12-27 20:38:35 +0000449 const char *zStart, /* Start of SQL text */
450 const char *zEnd /* End of SQL text */
danielk1977633ed082002-05-17 00:05:58 +0000451){
dan5be60c52018-08-15 20:28:39 +0000452 sqlite3 *db = pParse->db;
drhf3a65f72007-08-22 20:18:21 +0000453 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000454
drh75593d92014-01-10 20:46:55 +0000455 assert(pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000456
danc9461ec2018-08-29 21:00:16 +0000457 pTriggerStep = triggerStepAllocate(pParse, TK_INSERT, pTableName,zStart,zEnd);
danielk1977d5d56522005-03-16 12:15:20 +0000458 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000459 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000460 pTriggerStep->pSelect = pSelect;
461 pSelect = 0;
462 }else{
463 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
464 }
danielk1977d5d56522005-03-16 12:15:20 +0000465 pTriggerStep->pIdList = pColumn;
drh46d2e5c2018-04-12 13:15:43 +0000466 pTriggerStep->pUpsert = pUpsert;
danielk1977d5d56522005-03-16 12:15:20 +0000467 pTriggerStep->orconf = orconf;
dan9105fd52019-08-19 17:26:32 +0000468 if( pUpsert ){
469 sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
470 }
danielk1977d5d56522005-03-16 12:15:20 +0000471 }else{
drh99160482018-04-18 01:34:39 +0000472 testcase( pColumn );
drh633e6d52008-07-28 19:34:53 +0000473 sqlite3IdListDelete(db, pColumn);
drh99160482018-04-18 01:34:39 +0000474 testcase( pUpsert );
drh46d2e5c2018-04-12 13:15:43 +0000475 sqlite3UpsertDelete(db, pUpsert);
danielk1977d5d56522005-03-16 12:15:20 +0000476 }
drh33e619f2009-05-28 01:00:55 +0000477 sqlite3SelectDelete(db, pSelect);
danielk1977c3f9bad2002-05-15 08:30:12 +0000478
danielk1977633ed082002-05-17 00:05:58 +0000479 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000480}
481
drhc977f7f2002-05-21 11:38:11 +0000482/*
483** Construct a trigger step that implements an UPDATE statement and return
484** a pointer to that trigger step. The parser calls this routine when it
485** sees an UPDATE statement inside the body of a CREATE TRIGGER.
486*/
danielk19774adee202004-05-08 08:23:19 +0000487TriggerStep *sqlite3TriggerUpdateStep(
dan5be60c52018-08-15 20:28:39 +0000488 Parse *pParse, /* Parser */
drhc977f7f2002-05-21 11:38:11 +0000489 Token *pTableName, /* Name of the table to be updated */
dane7877b22020-07-14 19:51:01 +0000490 SrcList *pFrom,
drhc977f7f2002-05-21 11:38:11 +0000491 ExprList *pEList, /* The SET clause: list of column and new values */
492 Expr *pWhere, /* The WHERE clause */
drhf259df52017-12-27 20:38:35 +0000493 u8 orconf, /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
494 const char *zStart, /* Start of SQL text */
495 const char *zEnd /* End of SQL text */
drhc977f7f2002-05-21 11:38:11 +0000496){
dan5be60c52018-08-15 20:28:39 +0000497 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000498 TriggerStep *pTriggerStep;
499
danc9461ec2018-08-29 21:00:16 +0000500 pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE, pTableName,zStart,zEnd);
drh33e619f2009-05-28 01:00:55 +0000501 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000502 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000503 pTriggerStep->pExprList = pEList;
504 pTriggerStep->pWhere = pWhere;
dane7877b22020-07-14 19:51:01 +0000505 pTriggerStep->pFrom = pFrom;
dan5be60c52018-08-15 20:28:39 +0000506 pEList = 0;
507 pWhere = 0;
dane7877b22020-07-14 19:51:01 +0000508 pFrom = 0;
dan5be60c52018-08-15 20:28:39 +0000509 }else{
510 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
511 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
dane7877b22020-07-14 19:51:01 +0000512 pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE);
dan5be60c52018-08-15 20:28:39 +0000513 }
drh33e619f2009-05-28 01:00:55 +0000514 pTriggerStep->orconf = orconf;
dane7877b22020-07-14 19:51:01 +0000515 }else{
516 sqlite3SrcListDelete(db, pFrom);
drh0e3a6f32007-03-30 20:40:34 +0000517 }
drh33e619f2009-05-28 01:00:55 +0000518 sqlite3ExprListDelete(db, pEList);
519 sqlite3ExprDelete(db, pWhere);
dane7877b22020-07-14 19:51:01 +0000520 sqlite3SrcListDelete(db, pFrom);
danielk1977633ed082002-05-17 00:05:58 +0000521 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000522}
523
drhc977f7f2002-05-21 11:38:11 +0000524/*
525** Construct a trigger step that implements a DELETE statement and return
526** a pointer to that trigger step. The parser calls this routine when it
527** sees a DELETE statement inside the body of a CREATE TRIGGER.
528*/
drh17435752007-08-16 04:30:38 +0000529TriggerStep *sqlite3TriggerDeleteStep(
dan5be60c52018-08-15 20:28:39 +0000530 Parse *pParse, /* Parser */
drh17435752007-08-16 04:30:38 +0000531 Token *pTableName, /* The table from which rows are deleted */
drhf259df52017-12-27 20:38:35 +0000532 Expr *pWhere, /* The WHERE clause */
533 const char *zStart, /* Start of SQL text */
534 const char *zEnd /* End of SQL text */
drh17435752007-08-16 04:30:38 +0000535){
dan5be60c52018-08-15 20:28:39 +0000536 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000537 TriggerStep *pTriggerStep;
538
danc9461ec2018-08-29 21:00:16 +0000539 pTriggerStep = triggerStepAllocate(pParse, TK_DELETE, pTableName,zStart,zEnd);
drh33e619f2009-05-28 01:00:55 +0000540 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000541 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000542 pTriggerStep->pWhere = pWhere;
543 pWhere = 0;
544 }else{
545 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
546 }
drh33e619f2009-05-28 01:00:55 +0000547 pTriggerStep->orconf = OE_Default;
drhb63a53d2007-03-31 01:34:44 +0000548 }
drh33e619f2009-05-28 01:00:55 +0000549 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000550 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000551}
552
danielk1977633ed082002-05-17 00:05:58 +0000553/*
554** Recursively delete a Trigger structure
555*/
drh633e6d52008-07-28 19:34:53 +0000556void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
drhf0f258b2003-04-21 18:48:45 +0000557 if( pTrigger==0 ) return;
drh633e6d52008-07-28 19:34:53 +0000558 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
dan165921a2009-08-28 18:53:45 +0000559 sqlite3DbFree(db, pTrigger->zName);
drh633e6d52008-07-28 19:34:53 +0000560 sqlite3DbFree(db, pTrigger->table);
561 sqlite3ExprDelete(db, pTrigger->pWhen);
562 sqlite3IdListDelete(db, pTrigger->pColumns);
drh633e6d52008-07-28 19:34:53 +0000563 sqlite3DbFree(db, pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000564}
565
566/*
danielk19778a414492004-06-29 08:59:35 +0000567** This function is called to drop a trigger from the database schema.
568**
569** This may be called directly from the parser and therefore identifies
570** the trigger by name. The sqlite3DropTriggerPtr() routine does the
571** same job as this routine except it takes a pointer to the trigger
572** instead of the trigger name.
573**/
drhfdd48a72006-09-11 23:45:48 +0000574void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000575 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000576 int i;
577 const char *zDb;
578 const char *zName;
drh9bb575f2004-09-06 17:24:11 +0000579 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000580
drh17435752007-08-16 04:30:38 +0000581 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000582 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000583 goto drop_trigger_cleanup;
584 }
danielk19778e227872004-06-07 07:52:17 +0000585
drhd24cc422003-03-27 12:51:24 +0000586 assert( pName->nSrc==1 );
587 zDb = pName->a[0].zDatabase;
588 zName = pName->a[0].zName;
drh21206082011-04-04 18:22:02 +0000589 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
danielk197753c0f742005-03-29 03:10:59 +0000590 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000591 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
dan465c2b82020-03-21 15:10:40 +0000592 if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
drh21206082011-04-04 18:22:02 +0000593 assert( sqlite3SchemaMutexHeld(db, j, 0) );
drhacbcb7e2014-08-21 20:26:37 +0000594 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
drhd24cc422003-03-27 12:51:24 +0000595 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000596 }
drhd24cc422003-03-27 12:51:24 +0000597 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000598 if( !noErr ){
599 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
dan57966752011-04-09 17:32:58 +0000600 }else{
601 sqlite3CodeVerifyNamedSchema(pParse, zDb);
drhfdd48a72006-09-11 23:45:48 +0000602 }
dan1db95102010-06-28 10:15:19 +0000603 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +0000604 goto drop_trigger_cleanup;
605 }
drh74161702006-02-24 02:53:49 +0000606 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000607
608drop_trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000609 sqlite3SrcListDelete(db, pName);
drh79a519c2003-05-17 19:04:03 +0000610}
611
612/*
drh956bc922004-07-24 17:38:29 +0000613** Return a pointer to the Table structure for the table that a trigger
614** is set on.
615*/
drh74161702006-02-24 02:53:49 +0000616static Table *tableOfTrigger(Trigger *pTrigger){
drhacbcb7e2014-08-21 20:26:37 +0000617 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
drh956bc922004-07-24 17:38:29 +0000618}
619
620
621/*
drh74161702006-02-24 02:53:49 +0000622** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000623*/
drh74161702006-02-24 02:53:49 +0000624void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000625 Table *pTable;
626 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000627 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000628 int iDb;
drh79a519c2003-05-17 19:04:03 +0000629
danielk1977da184232006-01-05 11:34:32 +0000630 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000631 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000632 pTable = tableOfTrigger(pTrigger);
drh6397a782019-08-27 10:05:45 +0000633 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000634#ifndef SQLITE_OMIT_AUTHORIZATION
drh6397a782019-08-27 10:05:45 +0000635 if( pTable ){
drhe5f9c642003-01-13 23:27:31 +0000636 int code = SQLITE_DROP_TRIGGER;
drh69c33822016-08-18 14:33:11 +0000637 const char *zDb = db->aDb[iDb].zDbSName;
drh956bc922004-07-24 17:38:29 +0000638 const char *zTab = SCHEMA_TABLE(iDb);
639 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
dan2bd93512009-08-31 08:22:46 +0000640 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
danielk19774adee202004-05-08 08:23:19 +0000641 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000642 return;
drhe5f9c642003-01-13 23:27:31 +0000643 }
drhed6c8672003-01-12 18:02:16 +0000644 }
drhe5f9c642003-01-13 23:27:31 +0000645#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000646
drhf0f258b2003-04-21 18:48:45 +0000647 /* Generate code to destroy the database record of the trigger.
648 */
drh43617e92006-03-06 20:55:46 +0000649 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drh8c8dddc2015-12-09 16:26:38 +0000650 sqlite3NestedParse(pParse,
drh346a70c2020-06-15 20:27:35 +0000651 "DELETE FROM %Q." DFLT_SCHEMA_TABLE " WHERE name=%Q AND type='trigger'",
652 db->aDb[iDb].zDbSName, pTrigger->zName
drh8c8dddc2015-12-09 16:26:38 +0000653 );
drh9cbf3422008-01-17 16:22:13 +0000654 sqlite3ChangeCookie(pParse, iDb);
dan165921a2009-08-28 18:53:45 +0000655 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
drhf0f258b2003-04-21 18:48:45 +0000656 }
drh956bc922004-07-24 17:38:29 +0000657}
drhf0f258b2003-04-21 18:48:45 +0000658
drh956bc922004-07-24 17:38:29 +0000659/*
660** Remove a trigger from the hash tables of the sqlite* pointer.
661*/
662void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
663 Trigger *pTrigger;
drh21206082011-04-04 18:22:02 +0000664 Hash *pHash;
665
666 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
667 pHash = &(db->aDb[iDb].pSchema->trigHash);
drhacbcb7e2014-08-21 20:26:37 +0000668 pTrigger = sqlite3HashInsert(pHash, zName, 0);
drh9ab4c2e2009-05-09 00:18:38 +0000669 if( ALWAYS(pTrigger) ){
danielk19772f886d12009-02-28 10:47:41 +0000670 if( pTrigger->pSchema==pTrigger->pTabSchema ){
671 Table *pTab = tableOfTrigger(pTrigger);
drh6397a782019-08-27 10:05:45 +0000672 if( pTab ){
673 Trigger **pp;
dan0232dad2019-12-03 03:34:06 +0000674 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
675 if( *pp==pTrigger ){
676 *pp = (*pp)->pNext;
677 break;
678 }
679 }
drh6397a782019-08-27 10:05:45 +0000680 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000681 }
drh633e6d52008-07-28 19:34:53 +0000682 sqlite3DeleteTrigger(db, pTrigger);
drh8257aa82017-07-26 19:59:13 +0000683 db->mDbFlags |= DBFLAG_SchemaChange;
danielk1977c3f9bad2002-05-15 08:30:12 +0000684 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000685}
686
drhc977f7f2002-05-21 11:38:11 +0000687/*
688** pEList is the SET clause of an UPDATE statement. Each entry
689** in pEList is of the format <id>=<expr>. If any of the entries
690** in pEList have an <id> which matches an identifier in pIdList,
691** then return TRUE. If pIdList==NULL, then it is considered a
692** wildcard that matches anything. Likewise if pEList==NULL then
693** it matches anything so always return true. Return false only
694** if there is no match.
695*/
drh9ab4c2e2009-05-09 00:18:38 +0000696static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000697 int e;
drh9ab4c2e2009-05-09 00:18:38 +0000698 if( pIdList==0 || NEVER(pEList==0) ) return 1;
drhad2d8302002-05-24 20:31:36 +0000699 for(e=0; e<pEList->nExpr; e++){
drh41cee662019-12-12 20:22:34 +0000700 if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000701 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000702 return 0;
703}
704
danielk1977c3f9bad2002-05-15 08:30:12 +0000705/*
danielk19772f886d12009-02-28 10:47:41 +0000706** Return a list of all triggers on table pTab if there exists at least
707** one trigger that must be fired when an operation of type 'op' is
708** performed on the table, and, if that operation is an UPDATE, if at
709** least one of the columns in pChanges is being modified.
drhdca76842004-12-07 14:06:13 +0000710*/
danielk19772f886d12009-02-28 10:47:41 +0000711Trigger *sqlite3TriggersExist(
712 Parse *pParse, /* Parse context */
drhdca76842004-12-07 14:06:13 +0000713 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000714 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
danielk19772f886d12009-02-28 10:47:41 +0000715 ExprList *pChanges, /* Columns that change in an UPDATE statement */
716 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
drhc977f7f2002-05-21 11:38:11 +0000717){
drhdca76842004-12-07 14:06:13 +0000718 int mask = 0;
drhe83cafd2011-03-21 17:15:58 +0000719 Trigger *pList = 0;
danielk19772f886d12009-02-28 10:47:41 +0000720 Trigger *p;
drhe83cafd2011-03-21 17:15:58 +0000721
722 if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
723 pList = sqlite3TriggerList(pParse, pTab);
724 }
danielk19772f886d12009-02-28 10:47:41 +0000725 assert( pList==0 || IsVirtual(pTab)==0 );
726 for(p=pList; p; p=p->pNext){
drh9ab4c2e2009-05-09 00:18:38 +0000727 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
danielk19772f886d12009-02-28 10:47:41 +0000728 mask |= p->tr_tm;
danielk1977c3f9bad2002-05-15 08:30:12 +0000729 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000730 }
danielk19772f886d12009-02-28 10:47:41 +0000731 if( pMask ){
732 *pMask = mask;
733 }
734 return (mask ? pList : 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000735}
736
drhc977f7f2002-05-21 11:38:11 +0000737/*
dan46408352015-04-21 16:38:49 +0000738** Convert the pStep->zTarget string into a SrcList and return a pointer
drhf26e09c2003-05-31 16:21:12 +0000739** to that SrcList.
740**
741** This routine adds a specific database name, if needed, to the target when
742** forming the SrcList. This prevents a trigger in one database from
743** referring to a target in another database. An exception is when the
744** trigger is in TEMP in which case it can refer to any other database it
745** wants.
746*/
dane7877b22020-07-14 19:51:01 +0000747SrcList *sqlite3TriggerStepSrc(
drhf26e09c2003-05-31 16:21:12 +0000748 Parse *pParse, /* The parsing context */
749 TriggerStep *pStep /* The trigger containing the target token */
750){
dan46408352015-04-21 16:38:49 +0000751 sqlite3 *db = pParse->db;
dane7877b22020-07-14 19:51:01 +0000752 SrcList *pSrc; /* SrcList to be returned */
753 char *zName = sqlite3DbStrDup(db, pStep->zTarget);
drh29c992c2019-01-17 15:40:41 +0000754 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
dane7877b22020-07-14 19:51:01 +0000755 assert( pSrc==0 || pSrc->nSrc==1 );
756 assert( zName || pSrc==0 );
drhb7916a72009-05-27 10:31:29 +0000757 if( pSrc ){
dane7877b22020-07-14 19:51:01 +0000758 Schema *pSchema = pStep->pTrig->pSchema;
759 pSrc->a[0].zName = zName;
760 if( pSchema!=db->aDb[1].pSchema ){
761 pSrc->a[0].pSchema = pSchema;
drhb7916a72009-05-27 10:31:29 +0000762 }
dane7877b22020-07-14 19:51:01 +0000763 if( pStep->pFrom ){
764 SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0);
765 pSrc = sqlite3SrcListAppendList(pParse, pSrc, pDup);
766 }
767 }else{
768 sqlite3DbFree(db, zName);
drhf26e09c2003-05-31 16:21:12 +0000769 }
770 return pSrc;
771}
772
773/*
dan65a7cd12009-09-01 12:16:01 +0000774** Generate VDBE code for the statements inside the body of a single
775** trigger.
drhc977f7f2002-05-21 11:38:11 +0000776*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000777static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000778 Parse *pParse, /* The parser context */
779 TriggerStep *pStepList, /* List of statements inside the trigger body */
dan65a7cd12009-09-01 12:16:01 +0000780 int orconf /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000781){
dan65a7cd12009-09-01 12:16:01 +0000782 TriggerStep *pStep;
drh344737f2004-09-19 00:50:20 +0000783 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000784 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000785
dan65a7cd12009-09-01 12:16:01 +0000786 assert( pParse->pTriggerTab && pParse->pToplevel );
787 assert( pStepList );
drh344737f2004-09-19 00:50:20 +0000788 assert( v!=0 );
dan65a7cd12009-09-01 12:16:01 +0000789 for(pStep=pStepList; pStep; pStep=pStep->pNext){
dan165921a2009-08-28 18:53:45 +0000790 /* Figure out the ON CONFLICT policy that will be used for this step
791 ** of the trigger program. If the statement that caused this trigger
792 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
793 ** the ON CONFLICT policy that was specified as part of the trigger
794 ** step statement. Example:
795 **
796 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
797 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
798 ** END;
799 **
800 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
801 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
802 */
shanecea72b22009-09-07 04:38:36 +0000803 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
drhaceb31b2014-02-08 01:40:27 +0000804 assert( pParse->okConstFactor==0 );
danf78baaf2012-12-06 19:37:22 +0000805
drhf259df52017-12-27 20:38:35 +0000806#ifndef SQLITE_OMIT_TRACE
drhe307e112017-12-27 21:30:34 +0000807 if( pStep->zSpan ){
808 sqlite3VdbeAddOp4(v, OP_Trace, 0x7fffffff, 1, 0,
809 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
810 P4_DYNAMIC);
811 }
drhf259df52017-12-27 20:38:35 +0000812#endif
813
dan165921a2009-08-28 18:53:45 +0000814 switch( pStep->op ){
danielk1977633ed082002-05-17 00:05:58 +0000815 case TK_UPDATE: {
dan165921a2009-08-28 18:53:45 +0000816 sqlite3Update(pParse,
dane7877b22020-07-14 19:51:01 +0000817 sqlite3TriggerStepSrc(pParse, pStep),
dan165921a2009-08-28 18:53:45 +0000818 sqlite3ExprListDup(db, pStep->pExprList, 0),
819 sqlite3ExprDup(db, pStep->pWhere, 0),
drheac9fab2018-04-16 13:00:50 +0000820 pParse->eOrconf, 0, 0, 0
dan165921a2009-08-28 18:53:45 +0000821 );
danielk1977633ed082002-05-17 00:05:58 +0000822 break;
823 }
824 case TK_INSERT: {
dan165921a2009-08-28 18:53:45 +0000825 sqlite3Insert(pParse,
dane7877b22020-07-14 19:51:01 +0000826 sqlite3TriggerStepSrc(pParse, pStep),
dan165921a2009-08-28 18:53:45 +0000827 sqlite3SelectDup(db, pStep->pSelect, 0),
828 sqlite3IdListDup(db, pStep->pIdList),
drh46d2e5c2018-04-12 13:15:43 +0000829 pParse->eOrconf,
830 sqlite3UpsertDup(db, pStep->pUpsert)
dan165921a2009-08-28 18:53:45 +0000831 );
danielk1977633ed082002-05-17 00:05:58 +0000832 break;
833 }
834 case TK_DELETE: {
dan165921a2009-08-28 18:53:45 +0000835 sqlite3DeleteFrom(pParse,
dane7877b22020-07-14 19:51:01 +0000836 sqlite3TriggerStepSrc(pParse, pStep),
drh8c0833f2017-11-14 23:48:23 +0000837 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
dan165921a2009-08-28 18:53:45 +0000838 );
danielk1977633ed082002-05-17 00:05:58 +0000839 break;
840 }
dan165921a2009-08-28 18:53:45 +0000841 default: assert( pStep->op==TK_SELECT ); {
842 SelectDest sDest;
843 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
844 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
845 sqlite3Select(pParse, pSelect, &sDest);
846 sqlite3SelectDelete(db, pSelect);
drh9ab4c2e2009-05-09 00:18:38 +0000847 break;
848 }
danielk1977633ed082002-05-17 00:05:58 +0000849 }
dan76d462e2009-08-30 11:42:51 +0000850 if( pStep->op!=TK_SELECT ){
drhb7f1d9a2009-09-08 02:27:58 +0000851 sqlite3VdbeAddOp0(v, OP_ResetCount);
dan76d462e2009-08-30 11:42:51 +0000852 }
danielk1977633ed082002-05-17 00:05:58 +0000853 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000854
danielk1977633ed082002-05-17 00:05:58 +0000855 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000856}
857
drhc7379ce2013-10-30 02:28:23 +0000858#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
dan165921a2009-08-28 18:53:45 +0000859/*
860** This function is used to add VdbeComment() annotations to a VDBE
861** program. It is not used in production code, only for debugging.
862*/
863static const char *onErrorText(int onError){
864 switch( onError ){
865 case OE_Abort: return "abort";
866 case OE_Rollback: return "rollback";
867 case OE_Fail: return "fail";
868 case OE_Replace: return "replace";
869 case OE_Ignore: return "ignore";
870 case OE_Default: return "default";
871 }
872 return "n/a";
873}
874#endif
875
876/*
877** Parse context structure pFrom has just been used to create a sub-vdbe
878** (trigger program). If an error has occurred, transfer error information
879** from pFrom to pTo.
880*/
881static void transferParseError(Parse *pTo, Parse *pFrom){
882 assert( pFrom->zErrMsg==0 || pFrom->nErr );
883 assert( pTo->zErrMsg==0 || pTo->nErr );
884 if( pTo->nErr==0 ){
885 pTo->zErrMsg = pFrom->zErrMsg;
886 pTo->nErr = pFrom->nErr;
drhe06874e2015-04-16 15:47:06 +0000887 pTo->rc = pFrom->rc;
dan165921a2009-08-28 18:53:45 +0000888 }else{
889 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
890 }
891}
892
dan65a7cd12009-09-01 12:16:01 +0000893/*
894** Create and populate a new TriggerPrg object with a sub-program
895** implementing trigger pTrigger with ON CONFLICT policy orconf.
896*/
dan2832ad42009-08-31 15:27:27 +0000897static TriggerPrg *codeRowTrigger(
dan165921a2009-08-28 18:53:45 +0000898 Parse *pParse, /* Current parse context */
899 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +0000900 Table *pTab, /* The table pTrigger is attached to */
901 int orconf /* ON CONFLICT policy to code trigger program with */
dan165921a2009-08-28 18:53:45 +0000902){
dan65a7cd12009-09-01 12:16:01 +0000903 Parse *pTop = sqlite3ParseToplevel(pParse);
904 sqlite3 *db = pParse->db; /* Database handle */
905 TriggerPrg *pPrg; /* Value to return */
dan165921a2009-08-28 18:53:45 +0000906 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
907 Vdbe *v; /* Temporary VM */
dan165921a2009-08-28 18:53:45 +0000908 NameContext sNC; /* Name context for sub-vdbe */
909 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
910 Parse *pSubParse; /* Parse context for sub-vdbe */
911 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
912
dan1da40a32009-09-19 17:00:31 +0000913 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dand19c9332010-07-26 12:05:17 +0000914 assert( pTop->pVdbe );
dan65a7cd12009-09-01 12:16:01 +0000915
916 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
917 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
918 ** list of the top-level Parse object sooner rather than later. */
dan2832ad42009-08-31 15:27:27 +0000919 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
920 if( !pPrg ) return 0;
dan65a7cd12009-09-01 12:16:01 +0000921 pPrg->pNext = pTop->pTriggerPrg;
922 pTop->pTriggerPrg = pPrg;
dan2832ad42009-08-31 15:27:27 +0000923 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
dan165921a2009-08-28 18:53:45 +0000924 if( !pProgram ) return 0;
dand19c9332010-07-26 12:05:17 +0000925 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
dan2832ad42009-08-31 15:27:27 +0000926 pPrg->pTrigger = pTrigger;
927 pPrg->orconf = orconf;
danbb5f1682009-11-27 12:12:34 +0000928 pPrg->aColmask[0] = 0xffffffff;
929 pPrg->aColmask[1] = 0xffffffff;
dan165921a2009-08-28 18:53:45 +0000930
dan65a7cd12009-09-01 12:16:01 +0000931 /* Allocate and populate a new Parse context to use for coding the
932 ** trigger sub-program. */
933 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
934 if( !pSubParse ) return 0;
dan165921a2009-08-28 18:53:45 +0000935 memset(&sNC, 0, sizeof(sNC));
936 sNC.pParse = pSubParse;
937 pSubParse->db = db;
938 pSubParse->pTriggerTab = pTab;
dan65a7cd12009-09-01 12:16:01 +0000939 pSubParse->pToplevel = pTop;
dan2bd93512009-08-31 08:22:46 +0000940 pSubParse->zAuthContext = pTrigger->zName;
dan65a7cd12009-09-01 12:16:01 +0000941 pSubParse->eTriggerOp = pTrigger->op;
drh8b307fb2010-04-06 15:57:05 +0000942 pSubParse->nQueryLoop = pParse->nQueryLoop;
dan1ea04432018-12-21 19:29:11 +0000943 pSubParse->disableVtab = pParse->disableVtab;
dan165921a2009-08-28 18:53:45 +0000944
945 v = sqlite3GetVdbe(pSubParse);
946 if( v ){
dan76d462e2009-08-30 11:42:51 +0000947 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
948 pTrigger->zName, onErrorText(orconf),
dan165921a2009-08-28 18:53:45 +0000949 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
dan65a7cd12009-09-01 12:16:01 +0000950 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
951 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
952 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
dan76d462e2009-08-30 11:42:51 +0000953 pTab->zName
dan165921a2009-08-28 18:53:45 +0000954 ));
dan76d462e2009-08-30 11:42:51 +0000955#ifndef SQLITE_OMIT_TRACE
drhe307e112017-12-27 21:30:34 +0000956 if( pTrigger->zName ){
957 sqlite3VdbeChangeP4(v, -1,
958 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
959 );
960 }
dan76d462e2009-08-30 11:42:51 +0000961#endif
dan165921a2009-08-28 18:53:45 +0000962
dan65a7cd12009-09-01 12:16:01 +0000963 /* If one was specified, code the WHEN clause. If it evaluates to false
964 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
965 ** OP_Halt inserted at the end of the program. */
dan165921a2009-08-28 18:53:45 +0000966 if( pTrigger->pWhen ){
dan165921a2009-08-28 18:53:45 +0000967 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
dan523a0872009-08-31 05:23:32 +0000968 if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
969 && db->mallocFailed==0
970 ){
drhec4ccdb2018-12-29 02:26:59 +0000971 iEndTrigger = sqlite3VdbeMakeLabel(pSubParse);
dan165921a2009-08-28 18:53:45 +0000972 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
973 }
974 sqlite3ExprDelete(db, pWhen);
975 }
976
977 /* Code the trigger program into the sub-vdbe. */
dan76d462e2009-08-30 11:42:51 +0000978 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
dan65a7cd12009-09-01 12:16:01 +0000979
980 /* Insert an OP_Halt at the end of the sub-program. */
dan165921a2009-08-28 18:53:45 +0000981 if( iEndTrigger ){
982 sqlite3VdbeResolveLabel(v, iEndTrigger);
983 }
984 sqlite3VdbeAddOp0(v, OP_Halt);
dan76d462e2009-08-30 11:42:51 +0000985 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
986
dan165921a2009-08-28 18:53:45 +0000987 transferParseError(pParse, pSubParse);
dan08951172017-11-28 20:43:40 +0000988 if( db->mallocFailed==0 && pParse->nErr==0 ){
dan65a7cd12009-09-01 12:16:01 +0000989 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
dan523a0872009-08-31 05:23:32 +0000990 }
dan165921a2009-08-28 18:53:45 +0000991 pProgram->nMem = pSubParse->nMem;
992 pProgram->nCsr = pSubParse->nTab;
993 pProgram->token = (void *)pTrigger;
danbb5f1682009-11-27 12:12:34 +0000994 pPrg->aColmask[0] = pSubParse->oldmask;
995 pPrg->aColmask[1] = pSubParse->newmask;
dan165921a2009-08-28 18:53:45 +0000996 sqlite3VdbeDelete(v);
dan165921a2009-08-28 18:53:45 +0000997 }
dan65a7cd12009-09-01 12:16:01 +0000998
999 assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
1000 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
drhf30a9692013-11-15 01:10:18 +00001001 sqlite3ParserReset(pSubParse);
dan165921a2009-08-28 18:53:45 +00001002 sqlite3StackFree(db, pSubParse);
1003
dan2832ad42009-08-31 15:27:27 +00001004 return pPrg;
dan165921a2009-08-28 18:53:45 +00001005}
1006
dan65a7cd12009-09-01 12:16:01 +00001007/*
1008** Return a pointer to a TriggerPrg object containing the sub-program for
1009** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
1010** TriggerPrg object exists, a new object is allocated and populated before
1011** being returned.
1012*/
dan2832ad42009-08-31 15:27:27 +00001013static TriggerPrg *getRowTrigger(
dan65a7cd12009-09-01 12:16:01 +00001014 Parse *pParse, /* Current parse context */
dan165921a2009-08-28 18:53:45 +00001015 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +00001016 Table *pTab, /* The table trigger pTrigger is attached to */
1017 int orconf /* ON CONFLICT algorithm. */
dan165921a2009-08-28 18:53:45 +00001018){
dan65a7cd12009-09-01 12:16:01 +00001019 Parse *pRoot = sqlite3ParseToplevel(pParse);
dan2832ad42009-08-31 15:27:27 +00001020 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +00001021
dan1da40a32009-09-19 17:00:31 +00001022 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dan165921a2009-08-28 18:53:45 +00001023
1024 /* It may be that this trigger has already been coded (or is in the
1025 ** process of being coded). If this is the case, then an entry with
dan2832ad42009-08-31 15:27:27 +00001026 ** a matching TriggerPrg.pTrigger field will be present somewhere
1027 ** in the Parse.pTriggerPrg list. Search for such an entry. */
dan2832ad42009-08-31 15:27:27 +00001028 for(pPrg=pRoot->pTriggerPrg;
1029 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
1030 pPrg=pPrg->pNext
dan165921a2009-08-28 18:53:45 +00001031 );
1032
dan65a7cd12009-09-01 12:16:01 +00001033 /* If an existing TriggerPrg could not be located, create a new one. */
dan2832ad42009-08-31 15:27:27 +00001034 if( !pPrg ){
dan65a7cd12009-09-01 12:16:01 +00001035 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
dan165921a2009-08-28 18:53:45 +00001036 }
1037
dan2832ad42009-08-31 15:27:27 +00001038 return pPrg;
dan165921a2009-08-28 18:53:45 +00001039}
1040
dan94d7f502009-09-24 09:05:49 +00001041/*
1042** Generate code for the trigger program associated with trigger p on
1043** table pTab. The reg, orconf and ignoreJump parameters passed to this
1044** function are the same as those described in the header function for
1045** sqlite3CodeRowTrigger()
1046*/
dan1da40a32009-09-19 17:00:31 +00001047void sqlite3CodeRowTriggerDirect(
1048 Parse *pParse, /* Parse context */
1049 Trigger *p, /* Trigger to code */
1050 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001051 int reg, /* Reg array containing OLD.* and NEW.* values */
dan1da40a32009-09-19 17:00:31 +00001052 int orconf, /* ON CONFLICT policy */
1053 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
1054){
1055 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
1056 TriggerPrg *pPrg;
1057 pPrg = getRowTrigger(pParse, p, pTab, orconf);
1058 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
1059
1060 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
1061 ** is a pointer to the sub-vdbe containing the trigger program. */
1062 if( pPrg ){
dand19c9332010-07-26 12:05:17 +00001063 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
1064
drh9b34abe2016-01-16 15:12:35 +00001065 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
1066 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
dan1da40a32009-09-19 17:00:31 +00001067 VdbeComment(
1068 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
1069
1070 /* Set the P5 operand of the OP_Program instruction to non-zero if
1071 ** recursive invocation of this trigger program is disallowed. Recursive
1072 ** invocation is disallowed if (a) the sub-program is really a trigger,
1073 ** not a foreign key action, and (b) the flag to enable recursive triggers
1074 ** is clear. */
dand19c9332010-07-26 12:05:17 +00001075 sqlite3VdbeChangeP5(v, (u8)bRecursive);
dan1da40a32009-09-19 17:00:31 +00001076 }
1077}
1078
danielk1977633ed082002-05-17 00:05:58 +00001079/*
dan94d7f502009-09-24 09:05:49 +00001080** This is called to code the required FOR EACH ROW triggers for an operation
1081** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
drhf7b54962013-05-28 12:11:54 +00001082** is given by the op parameter. The tr_tm parameter determines whether the
dan94d7f502009-09-24 09:05:49 +00001083** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
1084** parameter pChanges is passed the list of columns being modified.
danielk1977633ed082002-05-17 00:05:58 +00001085**
dan94d7f502009-09-24 09:05:49 +00001086** If there are no triggers that fire at the specified time for the specified
1087** operation on pTab, this function is a no-op.
drhc977f7f2002-05-21 11:38:11 +00001088**
dan94d7f502009-09-24 09:05:49 +00001089** The reg argument is the address of the first in an array of registers
1090** that contain the values substituted for the new.* and old.* references
1091** in the trigger program. If N is the number of columns in table pTab
1092** (a copy of pTab->nCol), then registers are populated as follows:
drhc977f7f2002-05-21 11:38:11 +00001093**
dan94d7f502009-09-24 09:05:49 +00001094** Register Contains
1095** ------------------------------------------------------
1096** reg+0 OLD.rowid
1097** reg+1 OLD.* value of left-most column of pTab
1098** ... ...
1099** reg+N OLD.* value of right-most column of pTab
1100** reg+N+1 NEW.rowid
1101** reg+N+2 OLD.* value of left-most column of pTab
1102** ... ...
1103** reg+N+N+1 NEW.* value of right-most column of pTab
drhc977f7f2002-05-21 11:38:11 +00001104**
dan94d7f502009-09-24 09:05:49 +00001105** For ON DELETE triggers, the registers containing the NEW.* values will
1106** never be accessed by the trigger program, so they are not allocated or
1107** populated by the caller (there is no data to populate them with anyway).
1108** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
1109** are never accessed, and so are not allocated by the caller. So, for an
1110** ON INSERT trigger, the value passed to this function as parameter reg
1111** is not a readable register, although registers (reg+N) through
1112** (reg+N+N+1) are.
danielk1977633ed082002-05-17 00:05:58 +00001113**
dan94d7f502009-09-24 09:05:49 +00001114** Parameter orconf is the default conflict resolution algorithm for the
1115** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
1116** is the instruction that control should jump to if a trigger program
1117** raises an IGNORE exception.
danielk1977633ed082002-05-17 00:05:58 +00001118*/
dan165921a2009-08-28 18:53:45 +00001119void sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +00001120 Parse *pParse, /* Parse context */
danielk19772f886d12009-02-28 10:47:41 +00001121 Trigger *pTrigger, /* List of triggers on table pTab */
danielk1977633ed082002-05-17 00:05:58 +00001122 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
1123 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +00001124 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +00001125 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001126 int reg, /* The first in an array of registers (see above) */
danielk19776f349032002-06-11 02:25:40 +00001127 int orconf, /* ON CONFLICT policy */
dan165921a2009-08-28 18:53:45 +00001128 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
drhc977f7f2002-05-21 11:38:11 +00001129){
dan94d7f502009-09-24 09:05:49 +00001130 Trigger *p; /* Used to iterate through pTrigger list */
danielk19778f2c54e2008-01-01 19:02:09 +00001131
dan94d7f502009-09-24 09:05:49 +00001132 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
1133 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
1134 assert( (op==TK_UPDATE)==(pChanges!=0) );
danielk1977c3f9bad2002-05-15 08:30:12 +00001135
danielk19772f886d12009-02-28 10:47:41 +00001136 for(p=pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +00001137
drh9ab4c2e2009-05-09 00:18:38 +00001138 /* Sanity checking: The schema for the trigger and for the table are
1139 ** always defined. The trigger must be in the same schema as the table
1140 ** or else it must be a TEMP trigger. */
1141 assert( p->pSchema!=0 );
1142 assert( p->pTabSchema!=0 );
dan165921a2009-08-28 18:53:45 +00001143 assert( p->pSchema==p->pTabSchema
1144 || p->pSchema==pParse->db->aDb[1].pSchema );
drh9ab4c2e2009-05-09 00:18:38 +00001145
danielk1977eecfb3e2006-01-10 12:31:39 +00001146 /* Determine whether we should code this trigger */
dan165921a2009-08-28 18:53:45 +00001147 if( p->op==op
1148 && p->tr_tm==tr_tm
dan94d7f502009-09-24 09:05:49 +00001149 && checkColumnOverlap(p->pColumns, pChanges)
danielk1977eecfb3e2006-01-10 12:31:39 +00001150 ){
dan94d7f502009-09-24 09:05:49 +00001151 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
danielk1977c3f9bad2002-05-15 08:30:12 +00001152 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001153 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001154}
dan165921a2009-08-28 18:53:45 +00001155
dan2832ad42009-08-31 15:27:27 +00001156/*
danbb5f1682009-11-27 12:12:34 +00001157** Triggers may access values stored in the old.* or new.* pseudo-table.
1158** This function returns a 32-bit bitmask indicating which columns of the
1159** old.* or new.* tables actually are used by triggers. This information
1160** may be used by the caller, for example, to avoid having to load the entire
1161** old.* record into memory when executing an UPDATE or DELETE command.
dan2832ad42009-08-31 15:27:27 +00001162**
1163** Bit 0 of the returned mask is set if the left-most column of the
danbb5f1682009-11-27 12:12:34 +00001164** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
dan2832ad42009-08-31 15:27:27 +00001165** the second leftmost column value is required, and so on. If there
1166** are more than 32 columns in the table, and at least one of the columns
1167** with an index greater than 32 may be accessed, 0xffffffff is returned.
1168**
danbb5f1682009-11-27 12:12:34 +00001169** It is not possible to determine if the old.rowid or new.rowid column is
1170** accessed by triggers. The caller must always assume that it is.
dan2832ad42009-08-31 15:27:27 +00001171**
danbb5f1682009-11-27 12:12:34 +00001172** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
1173** applies to the old.* table. If 1, the new.* table.
1174**
1175** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
1176** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
1177** included in the returned mask if the TRIGGER_BEFORE bit is set in the
1178** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
1179** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
dan2832ad42009-08-31 15:27:27 +00001180*/
danbb5f1682009-11-27 12:12:34 +00001181u32 sqlite3TriggerColmask(
dan165921a2009-08-28 18:53:45 +00001182 Parse *pParse, /* Parse context */
1183 Trigger *pTrigger, /* List of triggers on table pTab */
dan165921a2009-08-28 18:53:45 +00001184 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
danbb5f1682009-11-27 12:12:34 +00001185 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
1186 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
dan165921a2009-08-28 18:53:45 +00001187 Table *pTab, /* The table to code triggers from */
dan2832ad42009-08-31 15:27:27 +00001188 int orconf /* Default ON CONFLICT policy for trigger steps */
dan165921a2009-08-28 18:53:45 +00001189){
dan1da40a32009-09-19 17:00:31 +00001190 const int op = pChanges ? TK_UPDATE : TK_DELETE;
dan2832ad42009-08-31 15:27:27 +00001191 u32 mask = 0;
dan165921a2009-08-28 18:53:45 +00001192 Trigger *p;
dan165921a2009-08-28 18:53:45 +00001193
danbb5f1682009-11-27 12:12:34 +00001194 assert( isNew==1 || isNew==0 );
dan165921a2009-08-28 18:53:45 +00001195 for(p=pTrigger; p; p=p->pNext){
danbb5f1682009-11-27 12:12:34 +00001196 if( p->op==op && (tr_tm&p->tr_tm)
1197 && checkColumnOverlap(p->pColumns,pChanges)
1198 ){
dan2832ad42009-08-31 15:27:27 +00001199 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +00001200 pPrg = getRowTrigger(pParse, p, pTab, orconf);
dan2832ad42009-08-31 15:27:27 +00001201 if( pPrg ){
danbb5f1682009-11-27 12:12:34 +00001202 mask |= pPrg->aColmask[isNew];
dan165921a2009-08-28 18:53:45 +00001203 }
1204 }
1205 }
dan2832ad42009-08-31 15:27:27 +00001206
1207 return mask;
dan165921a2009-08-28 18:53:45 +00001208}
1209
drhb7f91642004-10-31 02:22:47 +00001210#endif /* !defined(SQLITE_OMIT_TRIGGER) */