blob: 4240357a68f96d1b7e535735d214e0adbf1cc7b0 [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);
drhf259df52017-12-27 20:38:35 +000028 sqlite3DbFree(db, pTmp->zSpan);
drh4b59ab52002-08-24 18:24:51 +000029
drh633e6d52008-07-28 19:34:53 +000030 sqlite3DbFree(db, pTmp);
drh4b59ab52002-08-24 18:24:51 +000031 }
32}
33
34/*
danielk19772f886d12009-02-28 10:47:41 +000035** Given table pTab, return a list of all the triggers attached to
36** the table. The list is connected by Trigger.pNext pointers.
drh9ab4c2e2009-05-09 00:18:38 +000037**
38** All of the triggers on pTab that are in the same database as pTab
39** are already attached to pTab->pTrigger. But there might be additional
40** triggers on pTab in the TEMP schema. This routine prepends all
41** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
42** and returns the combined list.
43**
44** To state it another way: This routine returns a list of all triggers
45** that fire off of pTab. The list will include any TEMP triggers on
46** pTab as well as the triggers lised in pTab->pTrigger.
danielk19772f886d12009-02-28 10:47:41 +000047*/
48Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
49 Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
50 Trigger *pList = 0; /* List of triggers to return */
51
dand66c8302009-09-28 14:49:01 +000052 if( pParse->disableTriggers ){
53 return 0;
54 }
55
danielk19772f886d12009-02-28 10:47:41 +000056 if( pTmpSchema!=pTab->pSchema ){
57 HashElem *p;
drh21206082011-04-04 18:22:02 +000058 assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) );
danielk19772f886d12009-02-28 10:47:41 +000059 for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
60 Trigger *pTrig = (Trigger *)sqliteHashData(p);
61 if( pTrig->pTabSchema==pTab->pSchema
62 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
63 ){
64 pTrig->pNext = (pList ? pList : pTab->pTrigger);
65 pList = pTrig;
66 }
67 }
68 }
69
70 return (pList ? pList : pTab->pTrigger);
71}
72
73/*
drhf0f258b2003-04-21 18:48:45 +000074** This is called by the parser when it sees a CREATE TRIGGER statement
75** up to the point of the BEGIN before the trigger actions. A Trigger
76** structure is generated based on the information available and stored
77** in pParse->pNewTrigger. After the trigger actions have been parsed, the
danielk19774adee202004-05-08 08:23:19 +000078** sqlite3FinishTrigger() function is called to complete the trigger
drhf0f258b2003-04-21 18:48:45 +000079** construction process.
drh9adf9ac2002-05-15 11:44:13 +000080*/
danielk19774adee202004-05-08 08:23:19 +000081void sqlite3BeginTrigger(
drh9adf9ac2002-05-15 11:44:13 +000082 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
danielk1977ef2cb632004-05-29 02:37:19 +000083 Token *pName1, /* The name of the trigger */
84 Token *pName2, /* The name of the trigger */
drh5cf590c2003-04-24 01:45:04 +000085 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
drh9adf9ac2002-05-15 11:44:13 +000086 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
danielk1977633ed082002-05-17 00:05:58 +000087 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
drhd24cc422003-03-27 12:51:24 +000088 SrcList *pTableName,/* The name of the table/view the trigger applies to */
drh9adf9ac2002-05-15 11:44:13 +000089 Expr *pWhen, /* WHEN clause */
drhfdd48a72006-09-11 23:45:48 +000090 int isTemp, /* True if the TEMPORARY keyword is present */
91 int noErr /* Suppress errors if the trigger already exists */
drh9adf9ac2002-05-15 11:44:13 +000092){
drh3d5f74b2009-08-06 17:43:31 +000093 Trigger *pTrigger = 0; /* The new trigger */
94 Table *pTab; /* Table that the trigger fires off of */
drhf0f258b2003-04-21 18:48:45 +000095 char *zName = 0; /* Name of the trigger */
drh3d5f74b2009-08-06 17:43:31 +000096 sqlite3 *db = pParse->db; /* The database connection */
danielk1977ef2cb632004-05-29 02:37:19 +000097 int iDb; /* The database to store the trigger in */
98 Token *pName; /* The unqualified db name */
drh3d5f74b2009-08-06 17:43:31 +000099 DbFixer sFix; /* State vector for the DB fixer */
drhed6c8672003-01-12 18:02:16 +0000100
drh43617e92006-03-06 20:55:46 +0000101 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
102 assert( pName2!=0 );
drhaa78bec2008-12-09 03:55:14 +0000103 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
104 assert( op>0 && op<0xff );
danielk1977ef2cb632004-05-29 02:37:19 +0000105 if( isTemp ){
106 /* If TEMP was specified, then the trigger name may not be qualified. */
drh43617e92006-03-06 20:55:46 +0000107 if( pName2->n>0 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000108 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
109 goto trigger_cleanup;
110 }
111 iDb = 1;
112 pName = pName1;
113 }else{
mistachkind5578432012-08-25 10:01:29 +0000114 /* Figure out the db that the trigger will be created in */
danielk1977ef2cb632004-05-29 02:37:19 +0000115 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
116 if( iDb<0 ){
117 goto trigger_cleanup;
118 }
119 }
drh6fea83e2011-07-01 13:50:44 +0000120 if( !pTableName || db->mallocFailed ){
121 goto trigger_cleanup;
122 }
123
124 /* A long-standing parser bug is that this syntax was allowed:
125 **
126 ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
127 ** ^^^^^^^^
128 **
129 ** To maintain backwards compatibility, ignore the database
peter.d.reid60ec9142014-09-06 16:39:46 +0000130 ** name on pTableName if we are reparsing out of SQLITE_MASTER.
drh6fea83e2011-07-01 13:50:44 +0000131 */
132 if( db->init.busy && iDb!=1 ){
133 sqlite3DbFree(db, pTableName->a[0].zDatabase);
134 pTableName->a[0].zDatabase = 0;
135 }
danielk1977ef2cb632004-05-29 02:37:19 +0000136
137 /* If the trigger name was unqualified, and the table is a temp table,
138 ** then set iDb to 1 to create the trigger in the temporary database.
139 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
140 ** exist, the error is caught by the block below.
drh9adf9ac2002-05-15 11:44:13 +0000141 */
danielk1977ef2cb632004-05-29 02:37:19 +0000142 pTab = sqlite3SrcListLookup(pParse, pTableName);
drh622d2882010-02-15 16:54:55 +0000143 if( db->init.busy==0 && pName2->n==0 && pTab
144 && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977ef2cb632004-05-29 02:37:19 +0000145 iDb = 1;
146 }
147
148 /* Ensure the table name matches database name and that the table exists */
drh17435752007-08-16 04:30:38 +0000149 if( db->mallocFailed ) goto trigger_cleanup;
drhd24cc422003-03-27 12:51:24 +0000150 assert( pTableName->nSrc==1 );
drhd100f692013-10-03 15:39:44 +0000151 sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
152 if( sqlite3FixSrcList(&sFix, pTableName) ){
drh4312db52003-06-03 01:47:11 +0000153 goto trigger_cleanup;
drhf26e09c2003-05-31 16:21:12 +0000154 }
danielk1977ef2cb632004-05-29 02:37:19 +0000155 pTab = sqlite3SrcListLookup(pParse, pTableName);
156 if( !pTab ){
157 /* The table does not exist. */
drh3d5f74b2009-08-06 17:43:31 +0000158 if( db->init.iDb==1 ){
159 /* Ticket #3810.
160 ** Normally, whenever a table is dropped, all associated triggers are
161 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
162 ** and the table is dropped by a different database connection, the
163 ** trigger is not visible to the database connection that does the
164 ** drop so the trigger cannot be dropped. This results in an
165 ** "orphaned trigger" - a trigger whose associated table is missing.
166 */
167 db->init.orphanTrigger = 1;
168 }
drhd24cc422003-03-27 12:51:24 +0000169 goto trigger_cleanup;
170 }
drh4cbdda92006-06-14 19:00:20 +0000171 if( IsVirtual(pTab) ){
172 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
173 goto trigger_cleanup;
174 }
drhd24cc422003-03-27 12:51:24 +0000175
danielk1977d8123362004-06-12 09:25:12 +0000176 /* Check that the trigger name is not reserved and that no trigger of the
177 ** specified name exists */
drh17435752007-08-16 04:30:38 +0000178 zName = sqlite3NameFromToken(db, pName);
danielk1977d8123362004-06-12 09:25:12 +0000179 if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
180 goto trigger_cleanup;
181 }
drh21206082011-04-04 18:22:02 +0000182 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drhacbcb7e2014-08-21 20:26:37 +0000183 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
drhfdd48a72006-09-11 23:45:48 +0000184 if( !noErr ){
185 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
dan7687c832011-04-09 15:39:02 +0000186 }else{
187 assert( !db->init.busy );
188 sqlite3CodeVerifySchema(pParse, iDb);
drhfdd48a72006-09-11 23:45:48 +0000189 }
drhe5f9c642003-01-13 23:27:31 +0000190 goto trigger_cleanup;
danielk1977c3f9bad2002-05-15 08:30:12 +0000191 }
danielk1977ef2cb632004-05-29 02:37:19 +0000192
193 /* Do not create a trigger on a system table */
drhdca76842004-12-07 14:06:13 +0000194 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk19774adee202004-05-08 08:23:19 +0000195 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
drhd24cc422003-03-27 12:51:24 +0000196 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40 +0000197 }
danielk1977ef2cb632004-05-29 02:37:19 +0000198
199 /* INSTEAD of triggers are only for views and views only support INSTEAD
200 ** of triggers.
201 */
202 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000203 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drhda93d232003-03-31 02:12:46 +0000204 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000205 goto trigger_cleanup;
206 }
danielk1977ef2cb632004-05-29 02:37:19 +0000207 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000208 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drhda93d232003-03-31 02:12:46 +0000209 " trigger on table: %S", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000210 goto trigger_cleanup;
211 }
danielk1977ef2cb632004-05-29 02:37:19 +0000212
drhd24cc422003-03-27 12:51:24 +0000213#ifndef SQLITE_OMIT_AUTHORIZATION
214 {
drha0daa752016-09-16 11:53:10 +0000215 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drhd24cc422003-03-27 12:51:24 +0000216 int code = SQLITE_CREATE_TRIGGER;
drh69c33822016-08-18 14:33:11 +0000217 const char *zDb = db->aDb[iTabDb].zDbSName;
218 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
danielk1977da184232006-01-05 11:34:32 +0000219 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19 +0000220 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24 +0000221 goto trigger_cleanup;
222 }
danielk1977da184232006-01-05 11:34:32 +0000223 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24 +0000224 goto trigger_cleanup;
225 }
226 }
227#endif
danielk1977d702fcc2002-05-26 23:24:40 +0000228
danielk1977ef2cb632004-05-29 02:37:19 +0000229 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04 +0000230 ** cannot appear on views. So we might as well translate every
231 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
232 ** elsewhere.
233 */
danielk1977d702fcc2002-05-26 23:24:40 +0000234 if (tr_tm == TK_INSTEAD){
235 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12 +0000236 }
237
238 /* Build the Trigger object */
drh17435752007-08-16 04:30:38 +0000239 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19 +0000240 if( pTrigger==0 ) goto trigger_cleanup;
dan165921a2009-08-28 18:53:45 +0000241 pTrigger->zName = zName;
drhe5f9c642003-01-13 23:27:31 +0000242 zName = 0;
drh17435752007-08-16 04:30:38 +0000243 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32 +0000244 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48 +0000245 pTrigger->pTabSchema = pTab->pSchema;
drhaa78bec2008-12-09 03:55:14 +0000246 pTrigger->op = (u8)op;
drhdca76842004-12-07 14:06:13 +0000247 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
danielk19776ab3a2e2009-02-19 14:39:25 +0000248 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
drh17435752007-08-16 04:30:38 +0000249 pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
drhf0f258b2003-04-21 18:48:45 +0000250 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19 +0000251 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45 +0000252
253trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000254 sqlite3DbFree(db, zName);
255 sqlite3SrcListDelete(db, pTableName);
256 sqlite3IdListDelete(db, pColumns);
257 sqlite3ExprDelete(db, pWhen);
danielk1977d5d56522005-03-16 12:15:20 +0000258 if( !pParse->pNewTrigger ){
drh633e6d52008-07-28 19:34:53 +0000259 sqlite3DeleteTrigger(db, pTrigger);
danielk1977d5d56522005-03-16 12:15:20 +0000260 }else{
261 assert( pParse->pNewTrigger==pTrigger );
262 }
drhf0f258b2003-04-21 18:48:45 +0000263}
264
265/*
266** This routine is called after all of the trigger actions have been parsed
267** in order to complete the process of building the trigger.
268*/
danielk19774adee202004-05-08 08:23:19 +0000269void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45 +0000270 Parse *pParse, /* Parser context */
271 TriggerStep *pStepList, /* The triggered program */
272 Token *pAll /* Token that describes the complete CREATE TRIGGER */
273){
drha8c62df2010-02-15 15:47:18 +0000274 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
275 char *zName; /* Name of trigger */
276 sqlite3 *db = pParse->db; /* The database */
277 DbFixer sFix; /* Fixer object */
278 int iDb; /* Database containing the trigger */
279 Token nameToken; /* Trigger name for error reporting */
drhf0f258b2003-04-21 18:48:45 +0000280
drhf0f258b2003-04-21 18:48:45 +0000281 pParse->pNewTrigger = 0;
drh9ab4c2e2009-05-09 00:18:38 +0000282 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
dan165921a2009-08-28 18:53:45 +0000283 zName = pTrig->zName;
danielk1977da184232006-01-05 11:34:32 +0000284 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32 +0000285 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53 +0000286 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32 +0000287 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53 +0000288 pStepList = pStepList->pNext;
289 }
drh40aced52016-01-22 17:48:09 +0000290 sqlite3TokenInit(&nameToken, pTrig->zName);
drhd100f692013-10-03 15:39:44 +0000291 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
292 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
dan46539d72013-10-03 12:29:38 +0000293 || sqlite3FixExpr(&sFix, pTrig->pWhen)
drhd100f692013-10-03 15:39:44 +0000294 ){
drhf26e09c2003-05-31 16:21:12 +0000295 goto triggerfinish_cleanup;
296 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000297
drha8c62df2010-02-15 15:47:18 +0000298 /* if we are not initializing,
drh9adf9ac2002-05-15 11:44:13 +0000299 ** build the sqlite_master entry
300 */
drh1d85d932004-02-14 23:05:52 +0000301 if( !db->init.busy ){
drhc977f7f2002-05-21 11:38:11 +0000302 Vdbe *v;
drh2d401ab2008-01-10 23:50:11 +0000303 char *z;
danielk1977c3f9bad2002-05-15 08:30:12 +0000304
305 /* Make an entry in the sqlite_master table */
danielk19774adee202004-05-08 08:23:19 +0000306 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000307 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000308 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11 +0000309 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
drhd96cc6f2017-06-08 14:35:21 +0000310 testcase( z==0 );
drh2d401ab2008-01-10 23:50:11 +0000311 sqlite3NestedParse(pParse,
312 "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
drhe0a04a32016-12-16 01:00:21 +0000313 db->aDb[iDb].zDbSName, MASTER_NAME, zName,
drh2d401ab2008-01-10 23:50:11 +0000314 pTrig->table, z);
drh633e6d52008-07-28 19:34:53 +0000315 sqlite3DbFree(db, z);
drh9cbf3422008-01-17 16:22:13 +0000316 sqlite3ChangeCookie(pParse, iDb);
drh5d9c9da2011-06-03 20:11:17 +0000317 sqlite3VdbeAddParseSchemaOp(v, iDb,
318 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
danielk1977c3f9bad2002-05-15 08:30:12 +0000319 }
320
drh956bc922004-07-24 17:38:29 +0000321 if( db->init.busy ){
danielk19772f886d12009-02-28 10:47:41 +0000322 Trigger *pLink = pTrig;
323 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
drh21206082011-04-04 18:22:02 +0000324 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drhacbcb7e2014-08-21 20:26:37 +0000325 pTrig = sqlite3HashInsert(pHash, zName, pTrig);
danielk19772f886d12009-02-28 10:47:41 +0000326 if( pTrig ){
drh4a642b62016-02-05 01:55:27 +0000327 sqlite3OomFault(db);
danielk19772f886d12009-02-28 10:47:41 +0000328 }else if( pLink->pSchema==pLink->pTabSchema ){
329 Table *pTab;
drhacbcb7e2014-08-21 20:26:37 +0000330 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
danielk19772f886d12009-02-28 10:47:41 +0000331 assert( pTab!=0 );
332 pLink->pNext = pTab->pTrigger;
333 pTab->pTrigger = pLink;
danielk1977d5d56522005-03-16 12:15:20 +0000334 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000335 }
336
drhf0f258b2003-04-21 18:48:45 +0000337triggerfinish_cleanup:
drh633e6d52008-07-28 19:34:53 +0000338 sqlite3DeleteTrigger(db, pTrig);
danielk1977bfb9e352005-01-24 13:03:32 +0000339 assert( !pParse->pNewTrigger );
drh633e6d52008-07-28 19:34:53 +0000340 sqlite3DeleteTriggerStep(db, pStepList);
drh4b59ab52002-08-24 18:24:51 +0000341}
danielk1977c3f9bad2002-05-15 08:30:12 +0000342
drh4b59ab52002-08-24 18:24:51 +0000343/*
drhf259df52017-12-27 20:38:35 +0000344** Duplicate a range of text from an SQL statement, then convert all
345** whitespace characters into ordinary space characters.
346*/
347static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
348 char *z = sqlite3DbSpanDup(db, zStart, zEnd);
349 int i;
350 if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i]) ) z[i] = ' ';
351 return z;
352}
353
354/*
drhc977f7f2002-05-21 11:38:11 +0000355** Turn a SELECT statement (that the pSelect parameter points to) into
356** a trigger step. Return a pointer to a TriggerStep structure.
357**
358** The parser calls this routine when it finds a SELECT statement in
359** body of a TRIGGER.
360*/
drhf259df52017-12-27 20:38:35 +0000361TriggerStep *sqlite3TriggerSelectStep(
362 sqlite3 *db, /* Database connection */
363 Select *pSelect, /* The SELECT statement */
364 const char *zStart, /* Start of SQL text */
365 const char *zEnd /* End of SQL text */
366){
drh17435752007-08-16 04:30:38 +0000367 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000368 if( pTriggerStep==0 ) {
drh633e6d52008-07-28 19:34:53 +0000369 sqlite3SelectDelete(db, pSelect);
danielk19772e588c72005-12-09 14:25:08 +0000370 return 0;
371 }
danielk1977633ed082002-05-17 00:05:58 +0000372 pTriggerStep->op = TK_SELECT;
373 pTriggerStep->pSelect = pSelect;
374 pTriggerStep->orconf = OE_Default;
drhf259df52017-12-27 20:38:35 +0000375 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
drhb7916a72009-05-27 10:31:29 +0000376 return pTriggerStep;
377}
danielk1977c3f9bad2002-05-15 08:30:12 +0000378
drhb7916a72009-05-27 10:31:29 +0000379/*
380** Allocate space to hold a new trigger step. The allocated space
381** holds both the TriggerStep object and the TriggerStep.target.z string.
382**
383** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
384*/
385static TriggerStep *triggerStepAllocate(
386 sqlite3 *db, /* Database connection */
shane5eff7cf2009-08-10 03:57:58 +0000387 u8 op, /* Trigger opcode */
drhf259df52017-12-27 20:38:35 +0000388 Token *pName, /* The target name */
389 const char *zStart, /* Start of SQL text */
390 const char *zEnd /* End of SQL text */
drhb7916a72009-05-27 10:31:29 +0000391){
392 TriggerStep *pTriggerStep;
393
dan46408352015-04-21 16:38:49 +0000394 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
drhb7916a72009-05-27 10:31:29 +0000395 if( pTriggerStep ){
396 char *z = (char*)&pTriggerStep[1];
397 memcpy(z, pName->z, pName->n);
dan46408352015-04-21 16:38:49 +0000398 sqlite3Dequote(z);
399 pTriggerStep->zTarget = z;
drhb7916a72009-05-27 10:31:29 +0000400 pTriggerStep->op = op;
drhf259df52017-12-27 20:38:35 +0000401 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
drhb7916a72009-05-27 10:31:29 +0000402 }
danielk1977633ed082002-05-17 00:05:58 +0000403 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000404}
405
drhc977f7f2002-05-21 11:38:11 +0000406/*
407** Build a trigger step out of an INSERT statement. Return a pointer
408** to the new trigger step.
409**
410** The parser calls this routine when it sees an INSERT inside the
411** body of a trigger.
412*/
danielk19774adee202004-05-08 08:23:19 +0000413TriggerStep *sqlite3TriggerInsertStep(
drh17435752007-08-16 04:30:38 +0000414 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000415 Token *pTableName, /* Name of the table into which we insert */
416 IdList *pColumn, /* List of columns in pTableName to insert into */
drhc977f7f2002-05-21 11:38:11 +0000417 Select *pSelect, /* A SELECT statement that supplies values */
drhf259df52017-12-27 20:38:35 +0000418 u8 orconf, /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
drh2c2e8442018-04-07 15:04:05 +0000419 ExprList *pUpsert, /* Upsert values */
drhf259df52017-12-27 20:38:35 +0000420 const char *zStart, /* Start of SQL text */
421 const char *zEnd /* End of SQL text */
danielk1977633ed082002-05-17 00:05:58 +0000422){
drhf3a65f72007-08-22 20:18:21 +0000423 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000424
drh75593d92014-01-10 20:46:55 +0000425 assert(pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000426
drhf259df52017-12-27 20:38:35 +0000427 pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName, zStart, zEnd);
danielk1977d5d56522005-03-16 12:15:20 +0000428 if( pTriggerStep ){
drh33e619f2009-05-28 01:00:55 +0000429 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
danielk1977d5d56522005-03-16 12:15:20 +0000430 pTriggerStep->pIdList = pColumn;
danielk1977d5d56522005-03-16 12:15:20 +0000431 pTriggerStep->orconf = orconf;
danielk1977d5d56522005-03-16 12:15:20 +0000432 }else{
drh633e6d52008-07-28 19:34:53 +0000433 sqlite3IdListDelete(db, pColumn);
drh2c2e8442018-04-07 15:04:05 +0000434 sqlite3ExprListDelete(db, pUpsert);
danielk1977d5d56522005-03-16 12:15:20 +0000435 }
drh33e619f2009-05-28 01:00:55 +0000436 sqlite3SelectDelete(db, pSelect);
danielk1977c3f9bad2002-05-15 08:30:12 +0000437
danielk1977633ed082002-05-17 00:05:58 +0000438 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000439}
440
drhc977f7f2002-05-21 11:38:11 +0000441/*
442** Construct a trigger step that implements an UPDATE statement and return
443** a pointer to that trigger step. The parser calls this routine when it
444** sees an UPDATE statement inside the body of a CREATE TRIGGER.
445*/
danielk19774adee202004-05-08 08:23:19 +0000446TriggerStep *sqlite3TriggerUpdateStep(
drh17435752007-08-16 04:30:38 +0000447 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000448 Token *pTableName, /* Name of the table to be updated */
449 ExprList *pEList, /* The SET clause: list of column and new values */
450 Expr *pWhere, /* The WHERE clause */
drhf259df52017-12-27 20:38:35 +0000451 u8 orconf, /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
452 const char *zStart, /* Start of SQL text */
453 const char *zEnd /* End of SQL text */
drhc977f7f2002-05-21 11:38:11 +0000454){
drhb7916a72009-05-27 10:31:29 +0000455 TriggerStep *pTriggerStep;
456
drhf259df52017-12-27 20:38:35 +0000457 pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName, zStart, zEnd);
drh33e619f2009-05-28 01:00:55 +0000458 if( pTriggerStep ){
459 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
460 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
461 pTriggerStep->orconf = orconf;
drh0e3a6f32007-03-30 20:40:34 +0000462 }
drh33e619f2009-05-28 01:00:55 +0000463 sqlite3ExprListDelete(db, pEList);
464 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000465 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000466}
467
drhc977f7f2002-05-21 11:38:11 +0000468/*
469** Construct a trigger step that implements a DELETE statement and return
470** a pointer to that trigger step. The parser calls this routine when it
471** sees a DELETE statement inside the body of a CREATE TRIGGER.
472*/
drh17435752007-08-16 04:30:38 +0000473TriggerStep *sqlite3TriggerDeleteStep(
474 sqlite3 *db, /* Database connection */
475 Token *pTableName, /* The table from which rows are deleted */
drhf259df52017-12-27 20:38:35 +0000476 Expr *pWhere, /* The WHERE clause */
477 const char *zStart, /* Start of SQL text */
478 const char *zEnd /* End of SQL text */
drh17435752007-08-16 04:30:38 +0000479){
drhb7916a72009-05-27 10:31:29 +0000480 TriggerStep *pTriggerStep;
481
drhf259df52017-12-27 20:38:35 +0000482 pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName, zStart, zEnd);
drh33e619f2009-05-28 01:00:55 +0000483 if( pTriggerStep ){
484 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
485 pTriggerStep->orconf = OE_Default;
drhb63a53d2007-03-31 01:34:44 +0000486 }
drh33e619f2009-05-28 01:00:55 +0000487 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000488 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000489}
490
danielk1977633ed082002-05-17 00:05:58 +0000491/*
492** Recursively delete a Trigger structure
493*/
drh633e6d52008-07-28 19:34:53 +0000494void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
drhf0f258b2003-04-21 18:48:45 +0000495 if( pTrigger==0 ) return;
drh633e6d52008-07-28 19:34:53 +0000496 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
dan165921a2009-08-28 18:53:45 +0000497 sqlite3DbFree(db, pTrigger->zName);
drh633e6d52008-07-28 19:34:53 +0000498 sqlite3DbFree(db, pTrigger->table);
499 sqlite3ExprDelete(db, pTrigger->pWhen);
500 sqlite3IdListDelete(db, pTrigger->pColumns);
drh633e6d52008-07-28 19:34:53 +0000501 sqlite3DbFree(db, pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000502}
503
504/*
danielk19778a414492004-06-29 08:59:35 +0000505** This function is called to drop a trigger from the database schema.
506**
507** This may be called directly from the parser and therefore identifies
508** the trigger by name. The sqlite3DropTriggerPtr() routine does the
509** same job as this routine except it takes a pointer to the trigger
510** instead of the trigger name.
511**/
drhfdd48a72006-09-11 23:45:48 +0000512void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000513 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000514 int i;
515 const char *zDb;
516 const char *zName;
drh9bb575f2004-09-06 17:24:11 +0000517 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000518
drh17435752007-08-16 04:30:38 +0000519 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000520 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000521 goto drop_trigger_cleanup;
522 }
danielk19778e227872004-06-07 07:52:17 +0000523
drhd24cc422003-03-27 12:51:24 +0000524 assert( pName->nSrc==1 );
525 zDb = pName->a[0].zDatabase;
526 zName = pName->a[0].zName;
drh21206082011-04-04 18:22:02 +0000527 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
danielk197753c0f742005-03-29 03:10:59 +0000528 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000529 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
drh69c33822016-08-18 14:33:11 +0000530 if( zDb && sqlite3StrICmp(db->aDb[j].zDbSName, zDb) ) continue;
drh21206082011-04-04 18:22:02 +0000531 assert( sqlite3SchemaMutexHeld(db, j, 0) );
drhacbcb7e2014-08-21 20:26:37 +0000532 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
drhd24cc422003-03-27 12:51:24 +0000533 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000534 }
drhd24cc422003-03-27 12:51:24 +0000535 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000536 if( !noErr ){
537 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
dan57966752011-04-09 17:32:58 +0000538 }else{
539 sqlite3CodeVerifyNamedSchema(pParse, zDb);
drhfdd48a72006-09-11 23:45:48 +0000540 }
dan1db95102010-06-28 10:15:19 +0000541 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +0000542 goto drop_trigger_cleanup;
543 }
drh74161702006-02-24 02:53:49 +0000544 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000545
546drop_trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000547 sqlite3SrcListDelete(db, pName);
drh79a519c2003-05-17 19:04:03 +0000548}
549
550/*
drh956bc922004-07-24 17:38:29 +0000551** Return a pointer to the Table structure for the table that a trigger
552** is set on.
553*/
drh74161702006-02-24 02:53:49 +0000554static Table *tableOfTrigger(Trigger *pTrigger){
drhacbcb7e2014-08-21 20:26:37 +0000555 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
drh956bc922004-07-24 17:38:29 +0000556}
557
558
559/*
drh74161702006-02-24 02:53:49 +0000560** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000561*/
drh74161702006-02-24 02:53:49 +0000562void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000563 Table *pTable;
564 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000565 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000566 int iDb;
drh79a519c2003-05-17 19:04:03 +0000567
danielk1977da184232006-01-05 11:34:32 +0000568 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000569 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000570 pTable = tableOfTrigger(pTrigger);
drh43617e92006-03-06 20:55:46 +0000571 assert( pTable );
danielk1977da184232006-01-05 11:34:32 +0000572 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000573#ifndef SQLITE_OMIT_AUTHORIZATION
574 {
575 int code = SQLITE_DROP_TRIGGER;
drh69c33822016-08-18 14:33:11 +0000576 const char *zDb = db->aDb[iDb].zDbSName;
drh956bc922004-07-24 17:38:29 +0000577 const char *zTab = SCHEMA_TABLE(iDb);
578 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
dan2bd93512009-08-31 08:22:46 +0000579 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
danielk19774adee202004-05-08 08:23:19 +0000580 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000581 return;
drhe5f9c642003-01-13 23:27:31 +0000582 }
drhed6c8672003-01-12 18:02:16 +0000583 }
drhe5f9c642003-01-13 23:27:31 +0000584#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000585
drhf0f258b2003-04-21 18:48:45 +0000586 /* Generate code to destroy the database record of the trigger.
587 */
drh43617e92006-03-06 20:55:46 +0000588 assert( pTable!=0 );
589 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drh8c8dddc2015-12-09 16:26:38 +0000590 sqlite3NestedParse(pParse,
591 "DELETE FROM %Q.%s WHERE name=%Q AND type='trigger'",
drhe0a04a32016-12-16 01:00:21 +0000592 db->aDb[iDb].zDbSName, MASTER_NAME, pTrigger->zName
drh8c8dddc2015-12-09 16:26:38 +0000593 );
drh9cbf3422008-01-17 16:22:13 +0000594 sqlite3ChangeCookie(pParse, iDb);
dan165921a2009-08-28 18:53:45 +0000595 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
drhf0f258b2003-04-21 18:48:45 +0000596 }
drh956bc922004-07-24 17:38:29 +0000597}
drhf0f258b2003-04-21 18:48:45 +0000598
drh956bc922004-07-24 17:38:29 +0000599/*
600** Remove a trigger from the hash tables of the sqlite* pointer.
601*/
602void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
603 Trigger *pTrigger;
drh21206082011-04-04 18:22:02 +0000604 Hash *pHash;
605
606 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
607 pHash = &(db->aDb[iDb].pSchema->trigHash);
drhacbcb7e2014-08-21 20:26:37 +0000608 pTrigger = sqlite3HashInsert(pHash, zName, 0);
drh9ab4c2e2009-05-09 00:18:38 +0000609 if( ALWAYS(pTrigger) ){
danielk19772f886d12009-02-28 10:47:41 +0000610 if( pTrigger->pSchema==pTrigger->pTabSchema ){
611 Table *pTab = tableOfTrigger(pTrigger);
612 Trigger **pp;
613 for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
614 *pp = (*pp)->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +0000615 }
drh633e6d52008-07-28 19:34:53 +0000616 sqlite3DeleteTrigger(db, pTrigger);
drh8257aa82017-07-26 19:59:13 +0000617 db->mDbFlags |= DBFLAG_SchemaChange;
danielk1977c3f9bad2002-05-15 08:30:12 +0000618 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000619}
620
drhc977f7f2002-05-21 11:38:11 +0000621/*
622** pEList is the SET clause of an UPDATE statement. Each entry
623** in pEList is of the format <id>=<expr>. If any of the entries
624** in pEList have an <id> which matches an identifier in pIdList,
625** then return TRUE. If pIdList==NULL, then it is considered a
626** wildcard that matches anything. Likewise if pEList==NULL then
627** it matches anything so always return true. Return false only
628** if there is no match.
629*/
drh9ab4c2e2009-05-09 00:18:38 +0000630static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000631 int e;
drh9ab4c2e2009-05-09 00:18:38 +0000632 if( pIdList==0 || NEVER(pEList==0) ) return 1;
drhad2d8302002-05-24 20:31:36 +0000633 for(e=0; e<pEList->nExpr; e++){
danielk19774adee202004-05-08 08:23:19 +0000634 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000635 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000636 return 0;
637}
638
danielk1977c3f9bad2002-05-15 08:30:12 +0000639/*
danielk19772f886d12009-02-28 10:47:41 +0000640** Return a list of all triggers on table pTab if there exists at least
641** one trigger that must be fired when an operation of type 'op' is
642** performed on the table, and, if that operation is an UPDATE, if at
643** least one of the columns in pChanges is being modified.
drhdca76842004-12-07 14:06:13 +0000644*/
danielk19772f886d12009-02-28 10:47:41 +0000645Trigger *sqlite3TriggersExist(
646 Parse *pParse, /* Parse context */
drhdca76842004-12-07 14:06:13 +0000647 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000648 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
danielk19772f886d12009-02-28 10:47:41 +0000649 ExprList *pChanges, /* Columns that change in an UPDATE statement */
650 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
drhc977f7f2002-05-21 11:38:11 +0000651){
drhdca76842004-12-07 14:06:13 +0000652 int mask = 0;
drhe83cafd2011-03-21 17:15:58 +0000653 Trigger *pList = 0;
danielk19772f886d12009-02-28 10:47:41 +0000654 Trigger *p;
drhe83cafd2011-03-21 17:15:58 +0000655
656 if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
657 pList = sqlite3TriggerList(pParse, pTab);
658 }
danielk19772f886d12009-02-28 10:47:41 +0000659 assert( pList==0 || IsVirtual(pTab)==0 );
660 for(p=pList; p; p=p->pNext){
drh9ab4c2e2009-05-09 00:18:38 +0000661 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
danielk19772f886d12009-02-28 10:47:41 +0000662 mask |= p->tr_tm;
danielk1977c3f9bad2002-05-15 08:30:12 +0000663 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000664 }
danielk19772f886d12009-02-28 10:47:41 +0000665 if( pMask ){
666 *pMask = mask;
667 }
668 return (mask ? pList : 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000669}
670
drhc977f7f2002-05-21 11:38:11 +0000671/*
dan46408352015-04-21 16:38:49 +0000672** Convert the pStep->zTarget string into a SrcList and return a pointer
drhf26e09c2003-05-31 16:21:12 +0000673** to that SrcList.
674**
675** This routine adds a specific database name, if needed, to the target when
676** forming the SrcList. This prevents a trigger in one database from
677** referring to a target in another database. An exception is when the
678** trigger is in TEMP in which case it can refer to any other database it
679** wants.
680*/
681static SrcList *targetSrcList(
682 Parse *pParse, /* The parsing context */
683 TriggerStep *pStep /* The trigger containing the target token */
684){
dan46408352015-04-21 16:38:49 +0000685 sqlite3 *db = pParse->db;
drhf26e09c2003-05-31 16:21:12 +0000686 int iDb; /* Index of the database to use */
687 SrcList *pSrc; /* SrcList to be returned */
688
dan46408352015-04-21 16:38:49 +0000689 pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
drhb7916a72009-05-27 10:31:29 +0000690 if( pSrc ){
691 assert( pSrc->nSrc>0 );
dan46408352015-04-21 16:38:49 +0000692 pSrc->a[pSrc->nSrc-1].zName = sqlite3DbStrDup(db, pStep->zTarget);
693 iDb = sqlite3SchemaToIndex(db, pStep->pTrig->pSchema);
drhb7916a72009-05-27 10:31:29 +0000694 if( iDb==0 || iDb>=2 ){
drh69c33822016-08-18 14:33:11 +0000695 const char *zDb;
dan46408352015-04-21 16:38:49 +0000696 assert( iDb<db->nDb );
drh69c33822016-08-18 14:33:11 +0000697 zDb = db->aDb[iDb].zDbSName;
698 pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, zDb);
drhb7916a72009-05-27 10:31:29 +0000699 }
drhf26e09c2003-05-31 16:21:12 +0000700 }
701 return pSrc;
702}
703
704/*
dan65a7cd12009-09-01 12:16:01 +0000705** Generate VDBE code for the statements inside the body of a single
706** trigger.
drhc977f7f2002-05-21 11:38:11 +0000707*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000708static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000709 Parse *pParse, /* The parser context */
710 TriggerStep *pStepList, /* List of statements inside the trigger body */
dan65a7cd12009-09-01 12:16:01 +0000711 int orconf /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000712){
dan65a7cd12009-09-01 12:16:01 +0000713 TriggerStep *pStep;
drh344737f2004-09-19 00:50:20 +0000714 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000715 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000716
dan65a7cd12009-09-01 12:16:01 +0000717 assert( pParse->pTriggerTab && pParse->pToplevel );
718 assert( pStepList );
drh344737f2004-09-19 00:50:20 +0000719 assert( v!=0 );
dan65a7cd12009-09-01 12:16:01 +0000720 for(pStep=pStepList; pStep; pStep=pStep->pNext){
dan165921a2009-08-28 18:53:45 +0000721 /* Figure out the ON CONFLICT policy that will be used for this step
722 ** of the trigger program. If the statement that caused this trigger
723 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
724 ** the ON CONFLICT policy that was specified as part of the trigger
725 ** step statement. Example:
726 **
727 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
728 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
729 ** END;
730 **
731 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
732 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
733 */
shanecea72b22009-09-07 04:38:36 +0000734 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
drhaceb31b2014-02-08 01:40:27 +0000735 assert( pParse->okConstFactor==0 );
danf78baaf2012-12-06 19:37:22 +0000736
drhf259df52017-12-27 20:38:35 +0000737#ifndef SQLITE_OMIT_TRACE
drhe307e112017-12-27 21:30:34 +0000738 if( pStep->zSpan ){
739 sqlite3VdbeAddOp4(v, OP_Trace, 0x7fffffff, 1, 0,
740 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
741 P4_DYNAMIC);
742 }
drhf259df52017-12-27 20:38:35 +0000743#endif
744
dan165921a2009-08-28 18:53:45 +0000745 switch( pStep->op ){
danielk1977633ed082002-05-17 00:05:58 +0000746 case TK_UPDATE: {
dan165921a2009-08-28 18:53:45 +0000747 sqlite3Update(pParse,
748 targetSrcList(pParse, pStep),
749 sqlite3ExprListDup(db, pStep->pExprList, 0),
750 sqlite3ExprDup(db, pStep->pWhere, 0),
drh8c0833f2017-11-14 23:48:23 +0000751 pParse->eOrconf, 0, 0
dan165921a2009-08-28 18:53:45 +0000752 );
danielk1977633ed082002-05-17 00:05:58 +0000753 break;
754 }
755 case TK_INSERT: {
dan165921a2009-08-28 18:53:45 +0000756 sqlite3Insert(pParse,
757 targetSrcList(pParse, pStep),
dan165921a2009-08-28 18:53:45 +0000758 sqlite3SelectDup(db, pStep->pSelect, 0),
759 sqlite3IdListDup(db, pStep->pIdList),
drh2c2e8442018-04-07 15:04:05 +0000760 pStep->pExprList ? OE_Update : pParse->eOrconf,
761 sqlite3ExprListDup(db, pStep->pExprList, 0)
dan165921a2009-08-28 18:53:45 +0000762 );
danielk1977633ed082002-05-17 00:05:58 +0000763 break;
764 }
765 case TK_DELETE: {
dan165921a2009-08-28 18:53:45 +0000766 sqlite3DeleteFrom(pParse,
767 targetSrcList(pParse, pStep),
drh8c0833f2017-11-14 23:48:23 +0000768 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
dan165921a2009-08-28 18:53:45 +0000769 );
danielk1977633ed082002-05-17 00:05:58 +0000770 break;
771 }
dan165921a2009-08-28 18:53:45 +0000772 default: assert( pStep->op==TK_SELECT ); {
773 SelectDest sDest;
774 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
775 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
776 sqlite3Select(pParse, pSelect, &sDest);
777 sqlite3SelectDelete(db, pSelect);
drh9ab4c2e2009-05-09 00:18:38 +0000778 break;
779 }
danielk1977633ed082002-05-17 00:05:58 +0000780 }
dan76d462e2009-08-30 11:42:51 +0000781 if( pStep->op!=TK_SELECT ){
drhb7f1d9a2009-09-08 02:27:58 +0000782 sqlite3VdbeAddOp0(v, OP_ResetCount);
dan76d462e2009-08-30 11:42:51 +0000783 }
danielk1977633ed082002-05-17 00:05:58 +0000784 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000785
danielk1977633ed082002-05-17 00:05:58 +0000786 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000787}
788
drhc7379ce2013-10-30 02:28:23 +0000789#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
dan165921a2009-08-28 18:53:45 +0000790/*
791** This function is used to add VdbeComment() annotations to a VDBE
792** program. It is not used in production code, only for debugging.
793*/
794static const char *onErrorText(int onError){
795 switch( onError ){
796 case OE_Abort: return "abort";
797 case OE_Rollback: return "rollback";
798 case OE_Fail: return "fail";
799 case OE_Replace: return "replace";
800 case OE_Ignore: return "ignore";
801 case OE_Default: return "default";
802 }
803 return "n/a";
804}
805#endif
806
807/*
808** Parse context structure pFrom has just been used to create a sub-vdbe
809** (trigger program). If an error has occurred, transfer error information
810** from pFrom to pTo.
811*/
812static void transferParseError(Parse *pTo, Parse *pFrom){
813 assert( pFrom->zErrMsg==0 || pFrom->nErr );
814 assert( pTo->zErrMsg==0 || pTo->nErr );
815 if( pTo->nErr==0 ){
816 pTo->zErrMsg = pFrom->zErrMsg;
817 pTo->nErr = pFrom->nErr;
drhe06874e2015-04-16 15:47:06 +0000818 pTo->rc = pFrom->rc;
dan165921a2009-08-28 18:53:45 +0000819 }else{
820 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
821 }
822}
823
dan65a7cd12009-09-01 12:16:01 +0000824/*
825** Create and populate a new TriggerPrg object with a sub-program
826** implementing trigger pTrigger with ON CONFLICT policy orconf.
827*/
dan2832ad42009-08-31 15:27:27 +0000828static TriggerPrg *codeRowTrigger(
dan165921a2009-08-28 18:53:45 +0000829 Parse *pParse, /* Current parse context */
830 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +0000831 Table *pTab, /* The table pTrigger is attached to */
832 int orconf /* ON CONFLICT policy to code trigger program with */
dan165921a2009-08-28 18:53:45 +0000833){
dan65a7cd12009-09-01 12:16:01 +0000834 Parse *pTop = sqlite3ParseToplevel(pParse);
835 sqlite3 *db = pParse->db; /* Database handle */
836 TriggerPrg *pPrg; /* Value to return */
dan165921a2009-08-28 18:53:45 +0000837 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
838 Vdbe *v; /* Temporary VM */
dan165921a2009-08-28 18:53:45 +0000839 NameContext sNC; /* Name context for sub-vdbe */
840 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
841 Parse *pSubParse; /* Parse context for sub-vdbe */
842 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
843
dan1da40a32009-09-19 17:00:31 +0000844 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dand19c9332010-07-26 12:05:17 +0000845 assert( pTop->pVdbe );
dan65a7cd12009-09-01 12:16:01 +0000846
847 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
848 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
849 ** list of the top-level Parse object sooner rather than later. */
dan2832ad42009-08-31 15:27:27 +0000850 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
851 if( !pPrg ) return 0;
dan65a7cd12009-09-01 12:16:01 +0000852 pPrg->pNext = pTop->pTriggerPrg;
853 pTop->pTriggerPrg = pPrg;
dan2832ad42009-08-31 15:27:27 +0000854 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
dan165921a2009-08-28 18:53:45 +0000855 if( !pProgram ) return 0;
dand19c9332010-07-26 12:05:17 +0000856 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
dan2832ad42009-08-31 15:27:27 +0000857 pPrg->pTrigger = pTrigger;
858 pPrg->orconf = orconf;
danbb5f1682009-11-27 12:12:34 +0000859 pPrg->aColmask[0] = 0xffffffff;
860 pPrg->aColmask[1] = 0xffffffff;
dan165921a2009-08-28 18:53:45 +0000861
dan65a7cd12009-09-01 12:16:01 +0000862 /* Allocate and populate a new Parse context to use for coding the
863 ** trigger sub-program. */
864 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
865 if( !pSubParse ) return 0;
dan165921a2009-08-28 18:53:45 +0000866 memset(&sNC, 0, sizeof(sNC));
867 sNC.pParse = pSubParse;
868 pSubParse->db = db;
869 pSubParse->pTriggerTab = pTab;
dan65a7cd12009-09-01 12:16:01 +0000870 pSubParse->pToplevel = pTop;
dan2bd93512009-08-31 08:22:46 +0000871 pSubParse->zAuthContext = pTrigger->zName;
dan65a7cd12009-09-01 12:16:01 +0000872 pSubParse->eTriggerOp = pTrigger->op;
drh8b307fb2010-04-06 15:57:05 +0000873 pSubParse->nQueryLoop = pParse->nQueryLoop;
dan165921a2009-08-28 18:53:45 +0000874
875 v = sqlite3GetVdbe(pSubParse);
876 if( v ){
dan76d462e2009-08-30 11:42:51 +0000877 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
878 pTrigger->zName, onErrorText(orconf),
dan165921a2009-08-28 18:53:45 +0000879 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
dan65a7cd12009-09-01 12:16:01 +0000880 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
881 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
882 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
dan76d462e2009-08-30 11:42:51 +0000883 pTab->zName
dan165921a2009-08-28 18:53:45 +0000884 ));
dan76d462e2009-08-30 11:42:51 +0000885#ifndef SQLITE_OMIT_TRACE
drhe307e112017-12-27 21:30:34 +0000886 if( pTrigger->zName ){
887 sqlite3VdbeChangeP4(v, -1,
888 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
889 );
890 }
dan76d462e2009-08-30 11:42:51 +0000891#endif
dan165921a2009-08-28 18:53:45 +0000892
dan65a7cd12009-09-01 12:16:01 +0000893 /* If one was specified, code the WHEN clause. If it evaluates to false
894 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
895 ** OP_Halt inserted at the end of the program. */
dan165921a2009-08-28 18:53:45 +0000896 if( pTrigger->pWhen ){
dan165921a2009-08-28 18:53:45 +0000897 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
dan523a0872009-08-31 05:23:32 +0000898 if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
899 && db->mallocFailed==0
900 ){
dan165921a2009-08-28 18:53:45 +0000901 iEndTrigger = sqlite3VdbeMakeLabel(v);
902 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
903 }
904 sqlite3ExprDelete(db, pWhen);
905 }
906
907 /* Code the trigger program into the sub-vdbe. */
dan76d462e2009-08-30 11:42:51 +0000908 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
dan65a7cd12009-09-01 12:16:01 +0000909
910 /* Insert an OP_Halt at the end of the sub-program. */
dan165921a2009-08-28 18:53:45 +0000911 if( iEndTrigger ){
912 sqlite3VdbeResolveLabel(v, iEndTrigger);
913 }
914 sqlite3VdbeAddOp0(v, OP_Halt);
dan76d462e2009-08-30 11:42:51 +0000915 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
916
dan165921a2009-08-28 18:53:45 +0000917 transferParseError(pParse, pSubParse);
dan08951172017-11-28 20:43:40 +0000918 if( db->mallocFailed==0 && pParse->nErr==0 ){
dan65a7cd12009-09-01 12:16:01 +0000919 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
dan523a0872009-08-31 05:23:32 +0000920 }
dan165921a2009-08-28 18:53:45 +0000921 pProgram->nMem = pSubParse->nMem;
922 pProgram->nCsr = pSubParse->nTab;
923 pProgram->token = (void *)pTrigger;
danbb5f1682009-11-27 12:12:34 +0000924 pPrg->aColmask[0] = pSubParse->oldmask;
925 pPrg->aColmask[1] = pSubParse->newmask;
dan165921a2009-08-28 18:53:45 +0000926 sqlite3VdbeDelete(v);
dan165921a2009-08-28 18:53:45 +0000927 }
dan65a7cd12009-09-01 12:16:01 +0000928
929 assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
930 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
drhf30a9692013-11-15 01:10:18 +0000931 sqlite3ParserReset(pSubParse);
dan165921a2009-08-28 18:53:45 +0000932 sqlite3StackFree(db, pSubParse);
933
dan2832ad42009-08-31 15:27:27 +0000934 return pPrg;
dan165921a2009-08-28 18:53:45 +0000935}
936
dan65a7cd12009-09-01 12:16:01 +0000937/*
938** Return a pointer to a TriggerPrg object containing the sub-program for
939** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
940** TriggerPrg object exists, a new object is allocated and populated before
941** being returned.
942*/
dan2832ad42009-08-31 15:27:27 +0000943static TriggerPrg *getRowTrigger(
dan65a7cd12009-09-01 12:16:01 +0000944 Parse *pParse, /* Current parse context */
dan165921a2009-08-28 18:53:45 +0000945 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +0000946 Table *pTab, /* The table trigger pTrigger is attached to */
947 int orconf /* ON CONFLICT algorithm. */
dan165921a2009-08-28 18:53:45 +0000948){
dan65a7cd12009-09-01 12:16:01 +0000949 Parse *pRoot = sqlite3ParseToplevel(pParse);
dan2832ad42009-08-31 15:27:27 +0000950 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +0000951
dan1da40a32009-09-19 17:00:31 +0000952 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dan165921a2009-08-28 18:53:45 +0000953
954 /* It may be that this trigger has already been coded (or is in the
955 ** process of being coded). If this is the case, then an entry with
dan2832ad42009-08-31 15:27:27 +0000956 ** a matching TriggerPrg.pTrigger field will be present somewhere
957 ** in the Parse.pTriggerPrg list. Search for such an entry. */
dan2832ad42009-08-31 15:27:27 +0000958 for(pPrg=pRoot->pTriggerPrg;
959 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
960 pPrg=pPrg->pNext
dan165921a2009-08-28 18:53:45 +0000961 );
962
dan65a7cd12009-09-01 12:16:01 +0000963 /* If an existing TriggerPrg could not be located, create a new one. */
dan2832ad42009-08-31 15:27:27 +0000964 if( !pPrg ){
dan65a7cd12009-09-01 12:16:01 +0000965 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
dan165921a2009-08-28 18:53:45 +0000966 }
967
dan2832ad42009-08-31 15:27:27 +0000968 return pPrg;
dan165921a2009-08-28 18:53:45 +0000969}
970
dan94d7f502009-09-24 09:05:49 +0000971/*
972** Generate code for the trigger program associated with trigger p on
973** table pTab. The reg, orconf and ignoreJump parameters passed to this
974** function are the same as those described in the header function for
975** sqlite3CodeRowTrigger()
976*/
dan1da40a32009-09-19 17:00:31 +0000977void sqlite3CodeRowTriggerDirect(
978 Parse *pParse, /* Parse context */
979 Trigger *p, /* Trigger to code */
980 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +0000981 int reg, /* Reg array containing OLD.* and NEW.* values */
dan1da40a32009-09-19 17:00:31 +0000982 int orconf, /* ON CONFLICT policy */
983 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
984){
985 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
986 TriggerPrg *pPrg;
987 pPrg = getRowTrigger(pParse, p, pTab, orconf);
988 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
989
990 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
991 ** is a pointer to the sub-vdbe containing the trigger program. */
992 if( pPrg ){
dand19c9332010-07-26 12:05:17 +0000993 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
994
drh9b34abe2016-01-16 15:12:35 +0000995 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
996 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
dan1da40a32009-09-19 17:00:31 +0000997 VdbeComment(
998 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
999
1000 /* Set the P5 operand of the OP_Program instruction to non-zero if
1001 ** recursive invocation of this trigger program is disallowed. Recursive
1002 ** invocation is disallowed if (a) the sub-program is really a trigger,
1003 ** not a foreign key action, and (b) the flag to enable recursive triggers
1004 ** is clear. */
dand19c9332010-07-26 12:05:17 +00001005 sqlite3VdbeChangeP5(v, (u8)bRecursive);
dan1da40a32009-09-19 17:00:31 +00001006 }
1007}
1008
danielk1977633ed082002-05-17 00:05:58 +00001009/*
dan94d7f502009-09-24 09:05:49 +00001010** This is called to code the required FOR EACH ROW triggers for an operation
1011** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
drhf7b54962013-05-28 12:11:54 +00001012** is given by the op parameter. The tr_tm parameter determines whether the
dan94d7f502009-09-24 09:05:49 +00001013** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
1014** parameter pChanges is passed the list of columns being modified.
danielk1977633ed082002-05-17 00:05:58 +00001015**
dan94d7f502009-09-24 09:05:49 +00001016** If there are no triggers that fire at the specified time for the specified
1017** operation on pTab, this function is a no-op.
drhc977f7f2002-05-21 11:38:11 +00001018**
dan94d7f502009-09-24 09:05:49 +00001019** The reg argument is the address of the first in an array of registers
1020** that contain the values substituted for the new.* and old.* references
1021** in the trigger program. If N is the number of columns in table pTab
1022** (a copy of pTab->nCol), then registers are populated as follows:
drhc977f7f2002-05-21 11:38:11 +00001023**
dan94d7f502009-09-24 09:05:49 +00001024** Register Contains
1025** ------------------------------------------------------
1026** reg+0 OLD.rowid
1027** reg+1 OLD.* value of left-most column of pTab
1028** ... ...
1029** reg+N OLD.* value of right-most column of pTab
1030** reg+N+1 NEW.rowid
1031** reg+N+2 OLD.* value of left-most column of pTab
1032** ... ...
1033** reg+N+N+1 NEW.* value of right-most column of pTab
drhc977f7f2002-05-21 11:38:11 +00001034**
dan94d7f502009-09-24 09:05:49 +00001035** For ON DELETE triggers, the registers containing the NEW.* values will
1036** never be accessed by the trigger program, so they are not allocated or
1037** populated by the caller (there is no data to populate them with anyway).
1038** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
1039** are never accessed, and so are not allocated by the caller. So, for an
1040** ON INSERT trigger, the value passed to this function as parameter reg
1041** is not a readable register, although registers (reg+N) through
1042** (reg+N+N+1) are.
danielk1977633ed082002-05-17 00:05:58 +00001043**
dan94d7f502009-09-24 09:05:49 +00001044** Parameter orconf is the default conflict resolution algorithm for the
1045** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
1046** is the instruction that control should jump to if a trigger program
1047** raises an IGNORE exception.
danielk1977633ed082002-05-17 00:05:58 +00001048*/
dan165921a2009-08-28 18:53:45 +00001049void sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +00001050 Parse *pParse, /* Parse context */
danielk19772f886d12009-02-28 10:47:41 +00001051 Trigger *pTrigger, /* List of triggers on table pTab */
danielk1977633ed082002-05-17 00:05:58 +00001052 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
1053 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +00001054 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +00001055 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001056 int reg, /* The first in an array of registers (see above) */
danielk19776f349032002-06-11 02:25:40 +00001057 int orconf, /* ON CONFLICT policy */
dan165921a2009-08-28 18:53:45 +00001058 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
drhc977f7f2002-05-21 11:38:11 +00001059){
dan94d7f502009-09-24 09:05:49 +00001060 Trigger *p; /* Used to iterate through pTrigger list */
danielk19778f2c54e2008-01-01 19:02:09 +00001061
dan94d7f502009-09-24 09:05:49 +00001062 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
1063 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
1064 assert( (op==TK_UPDATE)==(pChanges!=0) );
danielk1977c3f9bad2002-05-15 08:30:12 +00001065
danielk19772f886d12009-02-28 10:47:41 +00001066 for(p=pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +00001067
drh9ab4c2e2009-05-09 00:18:38 +00001068 /* Sanity checking: The schema for the trigger and for the table are
1069 ** always defined. The trigger must be in the same schema as the table
1070 ** or else it must be a TEMP trigger. */
1071 assert( p->pSchema!=0 );
1072 assert( p->pTabSchema!=0 );
dan165921a2009-08-28 18:53:45 +00001073 assert( p->pSchema==p->pTabSchema
1074 || p->pSchema==pParse->db->aDb[1].pSchema );
drh9ab4c2e2009-05-09 00:18:38 +00001075
danielk1977eecfb3e2006-01-10 12:31:39 +00001076 /* Determine whether we should code this trigger */
dan165921a2009-08-28 18:53:45 +00001077 if( p->op==op
1078 && p->tr_tm==tr_tm
dan94d7f502009-09-24 09:05:49 +00001079 && checkColumnOverlap(p->pColumns, pChanges)
danielk1977eecfb3e2006-01-10 12:31:39 +00001080 ){
dan94d7f502009-09-24 09:05:49 +00001081 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
danielk1977c3f9bad2002-05-15 08:30:12 +00001082 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001083 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001084}
dan165921a2009-08-28 18:53:45 +00001085
dan2832ad42009-08-31 15:27:27 +00001086/*
danbb5f1682009-11-27 12:12:34 +00001087** Triggers may access values stored in the old.* or new.* pseudo-table.
1088** This function returns a 32-bit bitmask indicating which columns of the
1089** old.* or new.* tables actually are used by triggers. This information
1090** may be used by the caller, for example, to avoid having to load the entire
1091** old.* record into memory when executing an UPDATE or DELETE command.
dan2832ad42009-08-31 15:27:27 +00001092**
1093** Bit 0 of the returned mask is set if the left-most column of the
danbb5f1682009-11-27 12:12:34 +00001094** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
dan2832ad42009-08-31 15:27:27 +00001095** the second leftmost column value is required, and so on. If there
1096** are more than 32 columns in the table, and at least one of the columns
1097** with an index greater than 32 may be accessed, 0xffffffff is returned.
1098**
danbb5f1682009-11-27 12:12:34 +00001099** It is not possible to determine if the old.rowid or new.rowid column is
1100** accessed by triggers. The caller must always assume that it is.
dan2832ad42009-08-31 15:27:27 +00001101**
danbb5f1682009-11-27 12:12:34 +00001102** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
1103** applies to the old.* table. If 1, the new.* table.
1104**
1105** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
1106** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
1107** included in the returned mask if the TRIGGER_BEFORE bit is set in the
1108** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
1109** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
dan2832ad42009-08-31 15:27:27 +00001110*/
danbb5f1682009-11-27 12:12:34 +00001111u32 sqlite3TriggerColmask(
dan165921a2009-08-28 18:53:45 +00001112 Parse *pParse, /* Parse context */
1113 Trigger *pTrigger, /* List of triggers on table pTab */
dan165921a2009-08-28 18:53:45 +00001114 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
danbb5f1682009-11-27 12:12:34 +00001115 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
1116 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
dan165921a2009-08-28 18:53:45 +00001117 Table *pTab, /* The table to code triggers from */
dan2832ad42009-08-31 15:27:27 +00001118 int orconf /* Default ON CONFLICT policy for trigger steps */
dan165921a2009-08-28 18:53:45 +00001119){
dan1da40a32009-09-19 17:00:31 +00001120 const int op = pChanges ? TK_UPDATE : TK_DELETE;
dan2832ad42009-08-31 15:27:27 +00001121 u32 mask = 0;
dan165921a2009-08-28 18:53:45 +00001122 Trigger *p;
dan165921a2009-08-28 18:53:45 +00001123
danbb5f1682009-11-27 12:12:34 +00001124 assert( isNew==1 || isNew==0 );
dan165921a2009-08-28 18:53:45 +00001125 for(p=pTrigger; p; p=p->pNext){
danbb5f1682009-11-27 12:12:34 +00001126 if( p->op==op && (tr_tm&p->tr_tm)
1127 && checkColumnOverlap(p->pColumns,pChanges)
1128 ){
dan2832ad42009-08-31 15:27:27 +00001129 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +00001130 pPrg = getRowTrigger(pParse, p, pTab, orconf);
dan2832ad42009-08-31 15:27:27 +00001131 if( pPrg ){
danbb5f1682009-11-27 12:12:34 +00001132 mask |= pPrg->aColmask[isNew];
dan165921a2009-08-28 18:53:45 +00001133 }
1134 }
1135 }
dan2832ad42009-08-31 15:27:27 +00001136
1137 return mask;
dan165921a2009-08-28 18:53:45 +00001138}
1139
drhb7f91642004-10-31 02:22:47 +00001140#endif /* !defined(SQLITE_OMIT_TRIGGER) */