blob: 131b67599dad20e60dc6a1f556a5576e09310bbf [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*************************************************************************
danielk1977822a5162008-05-16 04:51:54 +000011**
12**
drh9ab4c2e2009-05-09 00:18:38 +000013** $Id: trigger.c,v 1.139 2009/05/09 00:18:38 drh Exp $
drh9adf9ac2002-05-15 11:44:13 +000014*/
danielk1977c3f9bad2002-05-15 08:30:12 +000015#include "sqliteInt.h"
drh9adf9ac2002-05-15 11:44:13 +000016
drhb7f91642004-10-31 02:22:47 +000017#ifndef SQLITE_OMIT_TRIGGER
danielk1977c3f9bad2002-05-15 08:30:12 +000018/*
drh4b59ab52002-08-24 18:24:51 +000019** Delete a linked list of TriggerStep structures.
20*/
drh633e6d52008-07-28 19:34:53 +000021void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
drh4b59ab52002-08-24 18:24:51 +000022 while( pTriggerStep ){
23 TriggerStep * pTmp = pTriggerStep;
24 pTriggerStep = pTriggerStep->pNext;
25
drh633e6d52008-07-28 19:34:53 +000026 if( pTmp->target.dyn ) sqlite3DbFree(db, (char*)pTmp->target.z);
27 sqlite3ExprDelete(db, pTmp->pWhere);
28 sqlite3ExprListDelete(db, pTmp->pExprList);
29 sqlite3SelectDelete(db, pTmp->pSelect);
30 sqlite3IdListDelete(db, pTmp->pIdList);
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
54 if( pTmpSchema!=pTab->pSchema ){
55 HashElem *p;
56 for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
57 Trigger *pTrig = (Trigger *)sqliteHashData(p);
58 if( pTrig->pTabSchema==pTab->pSchema
59 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
60 ){
61 pTrig->pNext = (pList ? pList : pTab->pTrigger);
62 pList = pTrig;
63 }
64 }
65 }
66
67 return (pList ? pList : pTab->pTrigger);
68}
69
70/*
drhf0f258b2003-04-21 18:48:45 +000071** This is called by the parser when it sees a CREATE TRIGGER statement
72** up to the point of the BEGIN before the trigger actions. A Trigger
73** structure is generated based on the information available and stored
74** in pParse->pNewTrigger. After the trigger actions have been parsed, the
danielk19774adee202004-05-08 08:23:19 +000075** sqlite3FinishTrigger() function is called to complete the trigger
drhf0f258b2003-04-21 18:48:45 +000076** construction process.
drh9adf9ac2002-05-15 11:44:13 +000077*/
danielk19774adee202004-05-08 08:23:19 +000078void sqlite3BeginTrigger(
drh9adf9ac2002-05-15 11:44:13 +000079 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
danielk1977ef2cb632004-05-29 02:37:19 +000080 Token *pName1, /* The name of the trigger */
81 Token *pName2, /* The name of the trigger */
drh5cf590c2003-04-24 01:45:04 +000082 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
drh9adf9ac2002-05-15 11:44:13 +000083 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
danielk1977633ed082002-05-17 00:05:58 +000084 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
drhd24cc422003-03-27 12:51:24 +000085 SrcList *pTableName,/* The name of the table/view the trigger applies to */
drh9adf9ac2002-05-15 11:44:13 +000086 Expr *pWhen, /* WHEN clause */
drhfdd48a72006-09-11 23:45:48 +000087 int isTemp, /* True if the TEMPORARY keyword is present */
88 int noErr /* Suppress errors if the trigger already exists */
drh9adf9ac2002-05-15 11:44:13 +000089){
danielk1977d5d56522005-03-16 12:15:20 +000090 Trigger *pTrigger = 0;
danielk1977ef2cb632004-05-29 02:37:19 +000091 Table *pTab;
drhf0f258b2003-04-21 18:48:45 +000092 char *zName = 0; /* Name of the trigger */
drh9bb575f2004-09-06 17:24:11 +000093 sqlite3 *db = pParse->db;
danielk1977ef2cb632004-05-29 02:37:19 +000094 int iDb; /* The database to store the trigger in */
95 Token *pName; /* The unqualified db name */
drh4312db52003-06-03 01:47:11 +000096 DbFixer sFix;
danielk1977da184232006-01-05 11:34:32 +000097 int iTabDb;
drhed6c8672003-01-12 18:02:16 +000098
drh43617e92006-03-06 20:55:46 +000099 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
100 assert( pName2!=0 );
drhaa78bec2008-12-09 03:55:14 +0000101 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
102 assert( op>0 && op<0xff );
danielk1977ef2cb632004-05-29 02:37:19 +0000103 if( isTemp ){
104 /* If TEMP was specified, then the trigger name may not be qualified. */
drh43617e92006-03-06 20:55:46 +0000105 if( pName2->n>0 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000106 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
107 goto trigger_cleanup;
108 }
109 iDb = 1;
110 pName = pName1;
111 }else{
112 /* Figure out the db that the the trigger will be created in */
113 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
114 if( iDb<0 ){
115 goto trigger_cleanup;
116 }
117 }
118
119 /* If the trigger name was unqualified, and the table is a temp table,
120 ** then set iDb to 1 to create the trigger in the temporary database.
121 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
122 ** exist, the error is caught by the block below.
drh9adf9ac2002-05-15 11:44:13 +0000123 */
drh17435752007-08-16 04:30:38 +0000124 if( !pTableName || db->mallocFailed ){
drh6f7adc82006-01-11 21:41:20 +0000125 goto trigger_cleanup;
126 }
danielk1977ef2cb632004-05-29 02:37:19 +0000127 pTab = sqlite3SrcListLookup(pParse, pTableName);
danielk1977da184232006-01-05 11:34:32 +0000128 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977ef2cb632004-05-29 02:37:19 +0000129 iDb = 1;
130 }
131
132 /* Ensure the table name matches database name and that the table exists */
drh17435752007-08-16 04:30:38 +0000133 if( db->mallocFailed ) goto trigger_cleanup;
drhd24cc422003-03-27 12:51:24 +0000134 assert( pTableName->nSrc==1 );
danielk1977ef2cb632004-05-29 02:37:19 +0000135 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) &&
136 sqlite3FixSrcList(&sFix, pTableName) ){
drh4312db52003-06-03 01:47:11 +0000137 goto trigger_cleanup;
drhf26e09c2003-05-31 16:21:12 +0000138 }
danielk1977ef2cb632004-05-29 02:37:19 +0000139 pTab = sqlite3SrcListLookup(pParse, pTableName);
140 if( !pTab ){
141 /* The table does not exist. */
drhd24cc422003-03-27 12:51:24 +0000142 goto trigger_cleanup;
143 }
drh4cbdda92006-06-14 19:00:20 +0000144 if( IsVirtual(pTab) ){
145 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
146 goto trigger_cleanup;
147 }
drhd24cc422003-03-27 12:51:24 +0000148
danielk1977d8123362004-06-12 09:25:12 +0000149 /* Check that the trigger name is not reserved and that no trigger of the
150 ** specified name exists */
drh17435752007-08-16 04:30:38 +0000151 zName = sqlite3NameFromToken(db, pName);
danielk1977d8123362004-06-12 09:25:12 +0000152 if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
153 goto trigger_cleanup;
154 }
drhaa78bec2008-12-09 03:55:14 +0000155 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
drhea678832008-12-10 19:26:22 +0000156 zName, sqlite3Strlen30(zName)) ){
drhfdd48a72006-09-11 23:45:48 +0000157 if( !noErr ){
158 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
159 }
drhe5f9c642003-01-13 23:27:31 +0000160 goto trigger_cleanup;
danielk1977c3f9bad2002-05-15 08:30:12 +0000161 }
danielk1977ef2cb632004-05-29 02:37:19 +0000162
163 /* Do not create a trigger on a system table */
drhdca76842004-12-07 14:06:13 +0000164 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk19774adee202004-05-08 08:23:19 +0000165 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
drhd24cc422003-03-27 12:51:24 +0000166 pParse->nErr++;
167 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40 +0000168 }
danielk1977ef2cb632004-05-29 02:37:19 +0000169
170 /* INSTEAD of triggers are only for views and views only support INSTEAD
171 ** of triggers.
172 */
173 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000174 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drhda93d232003-03-31 02:12:46 +0000175 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000176 goto trigger_cleanup;
177 }
danielk1977ef2cb632004-05-29 02:37:19 +0000178 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000179 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drhda93d232003-03-31 02:12:46 +0000180 " trigger on table: %S", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000181 goto trigger_cleanup;
182 }
danielk1977da184232006-01-05 11:34:32 +0000183 iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977ef2cb632004-05-29 02:37:19 +0000184
drhd24cc422003-03-27 12:51:24 +0000185#ifndef SQLITE_OMIT_AUTHORIZATION
186 {
187 int code = SQLITE_CREATE_TRIGGER;
danielk1977da184232006-01-05 11:34:32 +0000188 const char *zDb = db->aDb[iTabDb].zName;
drhe22a3342003-04-22 20:30:37 +0000189 const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
danielk1977da184232006-01-05 11:34:32 +0000190 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19 +0000191 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24 +0000192 goto trigger_cleanup;
193 }
danielk1977da184232006-01-05 11:34:32 +0000194 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24 +0000195 goto trigger_cleanup;
196 }
197 }
198#endif
danielk1977d702fcc2002-05-26 23:24:40 +0000199
danielk1977ef2cb632004-05-29 02:37:19 +0000200 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04 +0000201 ** cannot appear on views. So we might as well translate every
202 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
203 ** elsewhere.
204 */
danielk1977d702fcc2002-05-26 23:24:40 +0000205 if (tr_tm == TK_INSTEAD){
206 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12 +0000207 }
208
209 /* Build the Trigger object */
drh17435752007-08-16 04:30:38 +0000210 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19 +0000211 if( pTrigger==0 ) goto trigger_cleanup;
212 pTrigger->name = zName;
drhe5f9c642003-01-13 23:27:31 +0000213 zName = 0;
drh17435752007-08-16 04:30:38 +0000214 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32 +0000215 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48 +0000216 pTrigger->pTabSchema = pTab->pSchema;
drhaa78bec2008-12-09 03:55:14 +0000217 pTrigger->op = (u8)op;
drhdca76842004-12-07 14:06:13 +0000218 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
danielk19776ab3a2e2009-02-19 14:39:25 +0000219 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
drh17435752007-08-16 04:30:38 +0000220 pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
drh9ab4c2e2009-05-09 00:18:38 +0000221 sqlite3TokenCopy(db, &pTrigger->nameToken, pName);
drhf0f258b2003-04-21 18:48:45 +0000222 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19 +0000223 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45 +0000224
225trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000226 sqlite3DbFree(db, zName);
227 sqlite3SrcListDelete(db, pTableName);
228 sqlite3IdListDelete(db, pColumns);
229 sqlite3ExprDelete(db, pWhen);
danielk1977d5d56522005-03-16 12:15:20 +0000230 if( !pParse->pNewTrigger ){
drh633e6d52008-07-28 19:34:53 +0000231 sqlite3DeleteTrigger(db, pTrigger);
danielk1977d5d56522005-03-16 12:15:20 +0000232 }else{
233 assert( pParse->pNewTrigger==pTrigger );
234 }
drhf0f258b2003-04-21 18:48:45 +0000235}
236
237/*
238** This routine is called after all of the trigger actions have been parsed
239** in order to complete the process of building the trigger.
240*/
danielk19774adee202004-05-08 08:23:19 +0000241void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45 +0000242 Parse *pParse, /* Parser context */
243 TriggerStep *pStepList, /* The triggered program */
244 Token *pAll /* Token that describes the complete CREATE TRIGGER */
245){
danielk19772f886d12009-02-28 10:47:41 +0000246 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
247 char *zName; /* Name of trigger */
248 sqlite3 *db = pParse->db; /* The database */
drhf26e09c2003-05-31 16:21:12 +0000249 DbFixer sFix;
danielk19772f886d12009-02-28 10:47:41 +0000250 int iDb; /* Database containing the trigger */
drhf0f258b2003-04-21 18:48:45 +0000251
danielk1977bfb9e352005-01-24 13:03:32 +0000252 pTrig = pParse->pNewTrigger;
drhf0f258b2003-04-21 18:48:45 +0000253 pParse->pNewTrigger = 0;
drh9ab4c2e2009-05-09 00:18:38 +0000254 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
danielk19772f886d12009-02-28 10:47:41 +0000255 zName = pTrig->name;
danielk1977da184232006-01-05 11:34:32 +0000256 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32 +0000257 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53 +0000258 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32 +0000259 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53 +0000260 pStepList = pStepList->pNext;
261 }
danielk1977da184232006-01-05 11:34:32 +0000262 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &pTrig->nameToken)
danielk1977bfb9e352005-01-24 13:03:32 +0000263 && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
drhf26e09c2003-05-31 16:21:12 +0000264 goto triggerfinish_cleanup;
265 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000266
267 /* if we are not initializing, and this trigger is not on a TEMP table,
drh9adf9ac2002-05-15 11:44:13 +0000268 ** build the sqlite_master entry
269 */
drh1d85d932004-02-14 23:05:52 +0000270 if( !db->init.busy ){
drhc977f7f2002-05-21 11:38:11 +0000271 Vdbe *v;
drh2d401ab2008-01-10 23:50:11 +0000272 char *z;
danielk1977c3f9bad2002-05-15 08:30:12 +0000273
274 /* Make an entry in the sqlite_master table */
danielk19774adee202004-05-08 08:23:19 +0000275 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000276 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000277 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11 +0000278 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
279 sqlite3NestedParse(pParse,
280 "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
danielk19772f886d12009-02-28 10:47:41 +0000281 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
drh2d401ab2008-01-10 23:50:11 +0000282 pTrig->table, z);
drh633e6d52008-07-28 19:34:53 +0000283 sqlite3DbFree(db, z);
drh9cbf3422008-01-17 16:22:13 +0000284 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +0000285 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, sqlite3MPrintf(
danielk19772f886d12009-02-28 10:47:41 +0000286 db, "type='trigger' AND name='%q'", zName), P4_DYNAMIC
danielk19771e536952007-08-16 10:09:01 +0000287 );
danielk1977c3f9bad2002-05-15 08:30:12 +0000288 }
289
drh956bc922004-07-24 17:38:29 +0000290 if( db->init.busy ){
danielk19772f886d12009-02-28 10:47:41 +0000291 Trigger *pLink = pTrig;
292 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
293 pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);
294 if( pTrig ){
drhf3a65f72007-08-22 20:18:21 +0000295 db->mallocFailed = 1;
danielk19772f886d12009-02-28 10:47:41 +0000296 }else if( pLink->pSchema==pLink->pTabSchema ){
297 Table *pTab;
drha83ccca2009-04-28 13:01:09 +0000298 int n = sqlite3Strlen30(pLink->table);
danielk19772f886d12009-02-28 10:47:41 +0000299 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n);
300 assert( pTab!=0 );
301 pLink->pNext = pTab->pTrigger;
302 pTab->pTrigger = pLink;
danielk1977d5d56522005-03-16 12:15:20 +0000303 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000304 }
305
drhf0f258b2003-04-21 18:48:45 +0000306triggerfinish_cleanup:
drh633e6d52008-07-28 19:34:53 +0000307 sqlite3DeleteTrigger(db, pTrig);
danielk1977bfb9e352005-01-24 13:03:32 +0000308 assert( !pParse->pNewTrigger );
drh633e6d52008-07-28 19:34:53 +0000309 sqlite3DeleteTriggerStep(db, pStepList);
drh4b59ab52002-08-24 18:24:51 +0000310}
danielk1977c3f9bad2002-05-15 08:30:12 +0000311
drh4b59ab52002-08-24 18:24:51 +0000312/*
313** Make a copy of all components of the given trigger step. This has
314** the effect of copying all Expr.token.z values into memory obtained
drh17435752007-08-16 04:30:38 +0000315** from sqlite3_malloc(). As initially created, the Expr.token.z values
drh4b59ab52002-08-24 18:24:51 +0000316** all point to the input string that was fed to the parser. But that
danielk19776f8a5032004-05-10 10:34:51 +0000317** string is ephemeral - it will go away as soon as the sqlite3_exec()
drh4b59ab52002-08-24 18:24:51 +0000318** call that started the parser exits. This routine makes a persistent
319** copy of all the Expr.token.z strings so that the TriggerStep structure
danielk19776f8a5032004-05-10 10:34:51 +0000320** will be valid even after the sqlite3_exec() call returns.
drh4b59ab52002-08-24 18:24:51 +0000321*/
drh17435752007-08-16 04:30:38 +0000322static void sqlitePersistTriggerStep(sqlite3 *db, TriggerStep *p){
drh4b59ab52002-08-24 18:24:51 +0000323 if( p->target.z ){
drh17435752007-08-16 04:30:38 +0000324 p->target.z = (u8*)sqlite3DbStrNDup(db, (char*)p->target.z, p->target.n);
drh4b59ab52002-08-24 18:24:51 +0000325 p->target.dyn = 1;
326 }
327 if( p->pSelect ){
danielk19776ab3a2e2009-02-19 14:39:25 +0000328 Select *pNew = sqlite3SelectDup(db, p->pSelect, 1);
drh633e6d52008-07-28 19:34:53 +0000329 sqlite3SelectDelete(db, p->pSelect);
drh4b59ab52002-08-24 18:24:51 +0000330 p->pSelect = pNew;
331 }
332 if( p->pWhere ){
danielk19776ab3a2e2009-02-19 14:39:25 +0000333 Expr *pNew = sqlite3ExprDup(db, p->pWhere, EXPRDUP_REDUCE);
drh633e6d52008-07-28 19:34:53 +0000334 sqlite3ExprDelete(db, p->pWhere);
drh4b59ab52002-08-24 18:24:51 +0000335 p->pWhere = pNew;
336 }
337 if( p->pExprList ){
danielk19776ab3a2e2009-02-19 14:39:25 +0000338 ExprList *pNew = sqlite3ExprListDup(db, p->pExprList, 1);
drh633e6d52008-07-28 19:34:53 +0000339 sqlite3ExprListDelete(db, p->pExprList);
drh4b59ab52002-08-24 18:24:51 +0000340 p->pExprList = pNew;
341 }
342 if( p->pIdList ){
drh17435752007-08-16 04:30:38 +0000343 IdList *pNew = sqlite3IdListDup(db, p->pIdList);
drh633e6d52008-07-28 19:34:53 +0000344 sqlite3IdListDelete(db, p->pIdList);
drh4b59ab52002-08-24 18:24:51 +0000345 p->pIdList = pNew;
danielk1977c3f9bad2002-05-15 08:30:12 +0000346 }
347}
348
drhc977f7f2002-05-21 11:38:11 +0000349/*
350** Turn a SELECT statement (that the pSelect parameter points to) into
351** a trigger step. Return a pointer to a TriggerStep structure.
352**
353** The parser calls this routine when it finds a SELECT statement in
354** body of a TRIGGER.
355*/
drh17435752007-08-16 04:30:38 +0000356TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
357 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000358 if( pTriggerStep==0 ) {
drh633e6d52008-07-28 19:34:53 +0000359 sqlite3SelectDelete(db, pSelect);
danielk19772e588c72005-12-09 14:25:08 +0000360 return 0;
361 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000362
danielk1977633ed082002-05-17 00:05:58 +0000363 pTriggerStep->op = TK_SELECT;
364 pTriggerStep->pSelect = pSelect;
365 pTriggerStep->orconf = OE_Default;
drh17435752007-08-16 04:30:38 +0000366 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977c3f9bad2002-05-15 08:30:12 +0000367
danielk1977633ed082002-05-17 00:05:58 +0000368 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000369}
370
drhc977f7f2002-05-21 11:38:11 +0000371/*
372** Build a trigger step out of an INSERT statement. Return a pointer
373** to the new trigger step.
374**
375** The parser calls this routine when it sees an INSERT inside the
376** body of a trigger.
377*/
danielk19774adee202004-05-08 08:23:19 +0000378TriggerStep *sqlite3TriggerInsertStep(
drh17435752007-08-16 04:30:38 +0000379 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000380 Token *pTableName, /* Name of the table into which we insert */
381 IdList *pColumn, /* List of columns in pTableName to insert into */
382 ExprList *pEList, /* The VALUE clause: a list of values to be inserted */
383 Select *pSelect, /* A SELECT statement that supplies values */
384 int orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
danielk1977633ed082002-05-17 00:05:58 +0000385){
drhf3a65f72007-08-22 20:18:21 +0000386 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000387
danielk1977633ed082002-05-17 00:05:58 +0000388 assert(pEList == 0 || pSelect == 0);
drhf3a65f72007-08-22 20:18:21 +0000389 assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000390
drhf3a65f72007-08-22 20:18:21 +0000391 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk1977d5d56522005-03-16 12:15:20 +0000392 if( pTriggerStep ){
393 pTriggerStep->op = TK_INSERT;
394 pTriggerStep->pSelect = pSelect;
395 pTriggerStep->target = *pTableName;
396 pTriggerStep->pIdList = pColumn;
397 pTriggerStep->pExprList = pEList;
398 pTriggerStep->orconf = orconf;
drh17435752007-08-16 04:30:38 +0000399 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977d5d56522005-03-16 12:15:20 +0000400 }else{
drh633e6d52008-07-28 19:34:53 +0000401 sqlite3IdListDelete(db, pColumn);
402 sqlite3ExprListDelete(db, pEList);
403 sqlite3SelectDelete(db, pSelect);
danielk1977d5d56522005-03-16 12:15:20 +0000404 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000405
danielk1977633ed082002-05-17 00:05:58 +0000406 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000407}
408
drhc977f7f2002-05-21 11:38:11 +0000409/*
410** Construct a trigger step that implements an UPDATE statement and return
411** a pointer to that trigger step. The parser calls this routine when it
412** sees an UPDATE statement inside the body of a CREATE TRIGGER.
413*/
danielk19774adee202004-05-08 08:23:19 +0000414TriggerStep *sqlite3TriggerUpdateStep(
drh17435752007-08-16 04:30:38 +0000415 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000416 Token *pTableName, /* Name of the table to be updated */
417 ExprList *pEList, /* The SET clause: list of column and new values */
418 Expr *pWhere, /* The WHERE clause */
419 int orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
420){
drh17435752007-08-16 04:30:38 +0000421 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
drh0e3a6f32007-03-30 20:40:34 +0000422 if( pTriggerStep==0 ){
drh633e6d52008-07-28 19:34:53 +0000423 sqlite3ExprListDelete(db, pEList);
424 sqlite3ExprDelete(db, pWhere);
drh0e3a6f32007-03-30 20:40:34 +0000425 return 0;
426 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000427
danielk1977633ed082002-05-17 00:05:58 +0000428 pTriggerStep->op = TK_UPDATE;
429 pTriggerStep->target = *pTableName;
430 pTriggerStep->pExprList = pEList;
431 pTriggerStep->pWhere = pWhere;
432 pTriggerStep->orconf = orconf;
drh17435752007-08-16 04:30:38 +0000433 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977c3f9bad2002-05-15 08:30:12 +0000434
danielk1977633ed082002-05-17 00:05:58 +0000435 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000436}
437
drhc977f7f2002-05-21 11:38:11 +0000438/*
439** Construct a trigger step that implements a DELETE statement and return
440** a pointer to that trigger step. The parser calls this routine when it
441** sees a DELETE statement inside the body of a CREATE TRIGGER.
442*/
drh17435752007-08-16 04:30:38 +0000443TriggerStep *sqlite3TriggerDeleteStep(
444 sqlite3 *db, /* Database connection */
445 Token *pTableName, /* The table from which rows are deleted */
446 Expr *pWhere /* The WHERE clause */
447){
448 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
drhb63a53d2007-03-31 01:34:44 +0000449 if( pTriggerStep==0 ){
drh633e6d52008-07-28 19:34:53 +0000450 sqlite3ExprDelete(db, pWhere);
drhb63a53d2007-03-31 01:34:44 +0000451 return 0;
452 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000453
danielk1977633ed082002-05-17 00:05:58 +0000454 pTriggerStep->op = TK_DELETE;
455 pTriggerStep->target = *pTableName;
456 pTriggerStep->pWhere = pWhere;
457 pTriggerStep->orconf = OE_Default;
drh17435752007-08-16 04:30:38 +0000458 sqlitePersistTriggerStep(db, pTriggerStep);
danielk1977c3f9bad2002-05-15 08:30:12 +0000459
danielk1977633ed082002-05-17 00:05:58 +0000460 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000461}
462
danielk1977633ed082002-05-17 00:05:58 +0000463/*
464** Recursively delete a Trigger structure
465*/
drh633e6d52008-07-28 19:34:53 +0000466void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
drhf0f258b2003-04-21 18:48:45 +0000467 if( pTrigger==0 ) return;
drh633e6d52008-07-28 19:34:53 +0000468 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
469 sqlite3DbFree(db, pTrigger->name);
470 sqlite3DbFree(db, pTrigger->table);
471 sqlite3ExprDelete(db, pTrigger->pWhen);
472 sqlite3IdListDelete(db, pTrigger->pColumns);
drh9ab4c2e2009-05-09 00:18:38 +0000473 assert( pTrigger->nameToken.dyn );
474 sqlite3DbFree(db, (char*)pTrigger->nameToken.z);
drh633e6d52008-07-28 19:34:53 +0000475 sqlite3DbFree(db, pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000476}
477
478/*
danielk19778a414492004-06-29 08:59:35 +0000479** This function is called to drop a trigger from the database schema.
480**
481** This may be called directly from the parser and therefore identifies
482** the trigger by name. The sqlite3DropTriggerPtr() routine does the
483** same job as this routine except it takes a pointer to the trigger
484** instead of the trigger name.
485**/
drhfdd48a72006-09-11 23:45:48 +0000486void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000487 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000488 int i;
489 const char *zDb;
490 const char *zName;
491 int nName;
drh9bb575f2004-09-06 17:24:11 +0000492 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000493
drh17435752007-08-16 04:30:38 +0000494 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000495 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000496 goto drop_trigger_cleanup;
497 }
danielk19778e227872004-06-07 07:52:17 +0000498
drhd24cc422003-03-27 12:51:24 +0000499 assert( pName->nSrc==1 );
500 zDb = pName->a[0].zDatabase;
501 zName = pName->a[0].zName;
drhea678832008-12-10 19:26:22 +0000502 nName = sqlite3Strlen30(zName);
danielk197753c0f742005-03-29 03:10:59 +0000503 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000504 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000505 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
drhe4df0e72006-03-29 00:24:06 +0000506 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
drhd24cc422003-03-27 12:51:24 +0000507 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000508 }
drhd24cc422003-03-27 12:51:24 +0000509 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000510 if( !noErr ){
511 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
512 }
drhd24cc422003-03-27 12:51:24 +0000513 goto drop_trigger_cleanup;
514 }
drh74161702006-02-24 02:53:49 +0000515 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000516
517drop_trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000518 sqlite3SrcListDelete(db, pName);
drh79a519c2003-05-17 19:04:03 +0000519}
520
521/*
drh956bc922004-07-24 17:38:29 +0000522** Return a pointer to the Table structure for the table that a trigger
523** is set on.
524*/
drh74161702006-02-24 02:53:49 +0000525static Table *tableOfTrigger(Trigger *pTrigger){
drha83ccca2009-04-28 13:01:09 +0000526 int n = sqlite3Strlen30(pTrigger->table);
danielk1977aaf22682006-01-06 15:03:48 +0000527 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
drh956bc922004-07-24 17:38:29 +0000528}
529
530
531/*
drh74161702006-02-24 02:53:49 +0000532** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000533*/
drh74161702006-02-24 02:53:49 +0000534void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000535 Table *pTable;
536 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000537 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000538 int iDb;
drh79a519c2003-05-17 19:04:03 +0000539
danielk1977da184232006-01-05 11:34:32 +0000540 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000541 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000542 pTable = tableOfTrigger(pTrigger);
drh43617e92006-03-06 20:55:46 +0000543 assert( pTable );
danielk1977da184232006-01-05 11:34:32 +0000544 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000545#ifndef SQLITE_OMIT_AUTHORIZATION
546 {
547 int code = SQLITE_DROP_TRIGGER;
drh956bc922004-07-24 17:38:29 +0000548 const char *zDb = db->aDb[iDb].zName;
549 const char *zTab = SCHEMA_TABLE(iDb);
550 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
danielk19774adee202004-05-08 08:23:19 +0000551 if( sqlite3AuthCheck(pParse, code, pTrigger->name, pTable->zName, zDb) ||
552 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000553 return;
drhe5f9c642003-01-13 23:27:31 +0000554 }
drhed6c8672003-01-12 18:02:16 +0000555 }
drhe5f9c642003-01-13 23:27:31 +0000556#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000557
drhf0f258b2003-04-21 18:48:45 +0000558 /* Generate code to destroy the database record of the trigger.
559 */
drh43617e92006-03-06 20:55:46 +0000560 assert( pTable!=0 );
561 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drhf0f258b2003-04-21 18:48:45 +0000562 int base;
drh57196282004-10-06 15:41:16 +0000563 static const VdbeOpList dropTrigger[] = {
drh9b1b01b2003-08-16 12:37:51 +0000564 { OP_Rewind, 0, ADDR(9), 0},
drh1db639c2008-01-17 02:36:28 +0000565 { OP_String8, 0, 1, 0}, /* 1 */
566 { OP_Column, 0, 1, 2},
567 { OP_Ne, 2, ADDR(8), 1},
568 { OP_String8, 0, 1, 0}, /* 4: "trigger" */
569 { OP_Column, 0, 0, 2},
570 { OP_Ne, 2, ADDR(8), 1},
drhf0f258b2003-04-21 18:48:45 +0000571 { OP_Delete, 0, 0, 0},
drh9b1b01b2003-08-16 12:37:51 +0000572 { OP_Next, 0, ADDR(1), 0}, /* 8 */
drhf0f258b2003-04-21 18:48:45 +0000573 };
574
drh956bc922004-07-24 17:38:29 +0000575 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk1977c00da102006-01-07 13:21:04 +0000576 sqlite3OpenMasterTable(pParse, iDb);
danielk19774adee202004-05-08 08:23:19 +0000577 base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger);
drh66a51672008-01-03 00:01:23 +0000578 sqlite3VdbeChangeP4(v, base+1, pTrigger->name, 0);
drh24003452008-01-03 01:28:59 +0000579 sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
drh9cbf3422008-01-17 16:22:13 +0000580 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +0000581 sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
582 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->name, 0);
danielk19776ab3a2e2009-02-19 14:39:25 +0000583 if( pParse->nMem<3 ){
584 pParse->nMem = 3;
585 }
drhf0f258b2003-04-21 18:48:45 +0000586 }
drh956bc922004-07-24 17:38:29 +0000587}
drhf0f258b2003-04-21 18:48:45 +0000588
drh956bc922004-07-24 17:38:29 +0000589/*
590** Remove a trigger from the hash tables of the sqlite* pointer.
591*/
592void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
danielk19772f886d12009-02-28 10:47:41 +0000593 Hash *pHash = &(db->aDb[iDb].pSchema->trigHash);
drh956bc922004-07-24 17:38:29 +0000594 Trigger *pTrigger;
danielk19772f886d12009-02-28 10:47:41 +0000595 pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0);
drh9ab4c2e2009-05-09 00:18:38 +0000596 if( ALWAYS(pTrigger) ){
danielk19772f886d12009-02-28 10:47:41 +0000597 if( pTrigger->pSchema==pTrigger->pTabSchema ){
598 Table *pTab = tableOfTrigger(pTrigger);
599 Trigger **pp;
600 for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
601 *pp = (*pp)->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +0000602 }
drh633e6d52008-07-28 19:34:53 +0000603 sqlite3DeleteTrigger(db, pTrigger);
drh956bc922004-07-24 17:38:29 +0000604 db->flags |= SQLITE_InternChanges;
danielk1977c3f9bad2002-05-15 08:30:12 +0000605 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000606}
607
drhc977f7f2002-05-21 11:38:11 +0000608/*
609** pEList is the SET clause of an UPDATE statement. Each entry
610** in pEList is of the format <id>=<expr>. If any of the entries
611** in pEList have an <id> which matches an identifier in pIdList,
612** then return TRUE. If pIdList==NULL, then it is considered a
613** wildcard that matches anything. Likewise if pEList==NULL then
614** it matches anything so always return true. Return false only
615** if there is no match.
616*/
drh9ab4c2e2009-05-09 00:18:38 +0000617static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000618 int e;
drh9ab4c2e2009-05-09 00:18:38 +0000619 if( pIdList==0 || NEVER(pEList==0) ) return 1;
drhad2d8302002-05-24 20:31:36 +0000620 for(e=0; e<pEList->nExpr; e++){
danielk19774adee202004-05-08 08:23:19 +0000621 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000622 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000623 return 0;
624}
625
danielk1977c3f9bad2002-05-15 08:30:12 +0000626/*
danielk19772f886d12009-02-28 10:47:41 +0000627** Return a list of all triggers on table pTab if there exists at least
628** one trigger that must be fired when an operation of type 'op' is
629** performed on the table, and, if that operation is an UPDATE, if at
630** least one of the columns in pChanges is being modified.
drhdca76842004-12-07 14:06:13 +0000631*/
danielk19772f886d12009-02-28 10:47:41 +0000632Trigger *sqlite3TriggersExist(
633 Parse *pParse, /* Parse context */
drhdca76842004-12-07 14:06:13 +0000634 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000635 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
danielk19772f886d12009-02-28 10:47:41 +0000636 ExprList *pChanges, /* Columns that change in an UPDATE statement */
637 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
drhc977f7f2002-05-21 11:38:11 +0000638){
drhdca76842004-12-07 14:06:13 +0000639 int mask = 0;
danielk19772f886d12009-02-28 10:47:41 +0000640 Trigger *pList = sqlite3TriggerList(pParse, pTab);
641 Trigger *p;
642 assert( pList==0 || IsVirtual(pTab)==0 );
643 for(p=pList; p; p=p->pNext){
drh9ab4c2e2009-05-09 00:18:38 +0000644 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
danielk19772f886d12009-02-28 10:47:41 +0000645 mask |= p->tr_tm;
danielk1977c3f9bad2002-05-15 08:30:12 +0000646 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000647 }
danielk19772f886d12009-02-28 10:47:41 +0000648 if( pMask ){
649 *pMask = mask;
650 }
651 return (mask ? pList : 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000652}
653
drhc977f7f2002-05-21 11:38:11 +0000654/*
drhf26e09c2003-05-31 16:21:12 +0000655** Convert the pStep->target token into a SrcList and return a pointer
656** to that SrcList.
657**
658** This routine adds a specific database name, if needed, to the target when
659** forming the SrcList. This prevents a trigger in one database from
660** referring to a target in another database. An exception is when the
661** trigger is in TEMP in which case it can refer to any other database it
662** wants.
663*/
664static SrcList *targetSrcList(
665 Parse *pParse, /* The parsing context */
666 TriggerStep *pStep /* The trigger containing the target token */
667){
668 Token sDb; /* Dummy database name token */
669 int iDb; /* Index of the database to use */
670 SrcList *pSrc; /* SrcList to be returned */
671
danielk1977da184232006-01-05 11:34:32 +0000672 iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
drhf26e09c2003-05-31 16:21:12 +0000673 if( iDb==0 || iDb>=2 ){
674 assert( iDb<pParse->db->nDb );
drh2646da72005-12-09 20:02:05 +0000675 sDb.z = (u8*)pParse->db->aDb[iDb].zName;
drhea678832008-12-10 19:26:22 +0000676 sDb.n = sqlite3Strlen30((char*)sDb.z);
drh6a863cd2009-05-06 18:42:21 +0000677 sDb.quoted = 0;
drh17435752007-08-16 04:30:38 +0000678 pSrc = sqlite3SrcListAppend(pParse->db, 0, &sDb, &pStep->target);
drhf26e09c2003-05-31 16:21:12 +0000679 } else {
drh17435752007-08-16 04:30:38 +0000680 pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
drhf26e09c2003-05-31 16:21:12 +0000681 }
682 return pSrc;
683}
684
685/*
drhc977f7f2002-05-21 11:38:11 +0000686** Generate VDBE code for zero or more statements inside the body of a
687** trigger.
688*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000689static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000690 Parse *pParse, /* The parser context */
691 TriggerStep *pStepList, /* List of statements inside the trigger body */
692 int orconfin /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000693){
694 TriggerStep * pTriggerStep = pStepList;
695 int orconf;
drh344737f2004-09-19 00:50:20 +0000696 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000697 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000698
drh344737f2004-09-19 00:50:20 +0000699 assert( pTriggerStep!=0 );
700 assert( v!=0 );
drh66a51672008-01-03 00:01:23 +0000701 sqlite3VdbeAddOp2(v, OP_ContextPush, 0, 0);
drhd4e70eb2008-01-02 00:34:36 +0000702 VdbeComment((v, "begin trigger %s", pStepList->pTrig->name));
danielk1977633ed082002-05-17 00:05:58 +0000703 while( pTriggerStep ){
drhceea3322009-04-23 13:22:42 +0000704 sqlite3ExprCacheClear(pParse);
danielk1977633ed082002-05-17 00:05:58 +0000705 orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin;
706 pParse->trigStack->orconf = orconf;
707 switch( pTriggerStep->op ){
danielk1977633ed082002-05-17 00:05:58 +0000708 case TK_UPDATE: {
drh113088e2003-03-20 01:16:58 +0000709 SrcList *pSrc;
drhf26e09c2003-05-31 16:21:12 +0000710 pSrc = targetSrcList(pParse, pTriggerStep);
drh66a51672008-01-03 00:01:23 +0000711 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000712 sqlite3Update(pParse, pSrc,
danielk19776ab3a2e2009-02-19 14:39:25 +0000713 sqlite3ExprListDup(db, pTriggerStep->pExprList, 0),
714 sqlite3ExprDup(db, pTriggerStep->pWhere, 0), orconf);
drh66a51672008-01-03 00:01:23 +0000715 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000716 break;
717 }
718 case TK_INSERT: {
drh113088e2003-03-20 01:16:58 +0000719 SrcList *pSrc;
drhf26e09c2003-05-31 16:21:12 +0000720 pSrc = targetSrcList(pParse, pTriggerStep);
drh66a51672008-01-03 00:01:23 +0000721 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000722 sqlite3Insert(pParse, pSrc,
danielk19776ab3a2e2009-02-19 14:39:25 +0000723 sqlite3ExprListDup(db, pTriggerStep->pExprList, 0),
724 sqlite3SelectDup(db, pTriggerStep->pSelect, 0),
drh17435752007-08-16 04:30:38 +0000725 sqlite3IdListDup(db, pTriggerStep->pIdList), orconf);
drh66a51672008-01-03 00:01:23 +0000726 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000727 break;
728 }
729 case TK_DELETE: {
drh113088e2003-03-20 01:16:58 +0000730 SrcList *pSrc;
drh66a51672008-01-03 00:01:23 +0000731 sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
drhf26e09c2003-05-31 16:21:12 +0000732 pSrc = targetSrcList(pParse, pTriggerStep);
drh17435752007-08-16 04:30:38 +0000733 sqlite3DeleteFrom(pParse, pSrc,
danielk19776ab3a2e2009-02-19 14:39:25 +0000734 sqlite3ExprDup(db, pTriggerStep->pWhere, 0));
drh66a51672008-01-03 00:01:23 +0000735 sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
danielk1977633ed082002-05-17 00:05:58 +0000736 break;
737 }
drh9ab4c2e2009-05-09 00:18:38 +0000738 default: assert( pTriggerStep->op==TK_SELECT ); {
739 Select *ss = sqlite3SelectDup(db, pTriggerStep->pSelect, 0);
740 if( ss ){
741 SelectDest dest;
742
743 sqlite3SelectDestInit(&dest, SRT_Discard, 0);
744 sqlite3Select(pParse, ss, &dest);
745 sqlite3SelectDelete(db, ss);
746 }
747 break;
748 }
danielk1977633ed082002-05-17 00:05:58 +0000749 }
danielk1977633ed082002-05-17 00:05:58 +0000750 pTriggerStep = pTriggerStep->pNext;
751 }
drh66a51672008-01-03 00:01:23 +0000752 sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
drhd4e70eb2008-01-02 00:34:36 +0000753 VdbeComment((v, "end trigger %s", pStepList->pTrig->name));
danielk1977c3f9bad2002-05-15 08:30:12 +0000754
danielk1977633ed082002-05-17 00:05:58 +0000755 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000756}
757
danielk1977633ed082002-05-17 00:05:58 +0000758/*
759** This is called to code FOR EACH ROW triggers.
760**
761** When the code that this function generates is executed, the following
762** must be true:
drhc977f7f2002-05-21 11:38:11 +0000763**
764** 1. No cursors may be open in the main database. (But newIdx and oldIdx
765** can be indices of cursors in temporary tables. See below.)
766**
danielk1977633ed082002-05-17 00:05:58 +0000767** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then
768** a temporary vdbe cursor (index newIdx) must be open and pointing at
769** a row containing values to be substituted for new.* expressions in the
770** trigger program(s).
drhc977f7f2002-05-21 11:38:11 +0000771**
danielk1977633ed082002-05-17 00:05:58 +0000772** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then
773** a temporary vdbe cursor (index oldIdx) must be open and pointing at
774** a row containing values to be substituted for old.* expressions in the
775** trigger program(s).
776**
danielk19778f2c54e2008-01-01 19:02:09 +0000777** If they are not NULL, the piOldColMask and piNewColMask output variables
778** are set to values that describe the columns used by the trigger program
779** in the OLD.* and NEW.* tables respectively. If column N of the
780** pseudo-table is read at least once, the corresponding bit of the output
781** mask is set. If a column with an index greater than 32 is read, the
782** output mask is set to the special value 0xffffffff.
783**
danielk1977633ed082002-05-17 00:05:58 +0000784*/
danielk19774adee202004-05-08 08:23:19 +0000785int sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +0000786 Parse *pParse, /* Parse context */
danielk19772f886d12009-02-28 10:47:41 +0000787 Trigger *pTrigger, /* List of triggers on table pTab */
danielk1977633ed082002-05-17 00:05:58 +0000788 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
789 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +0000790 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +0000791 Table *pTab, /* The table to code triggers from */
792 int newIdx, /* The indice of the "new" row to access */
793 int oldIdx, /* The indice of the "old" row to access */
danielk19776f349032002-06-11 02:25:40 +0000794 int orconf, /* ON CONFLICT policy */
danielk19778f2c54e2008-01-01 19:02:09 +0000795 int ignoreJump, /* Instruction to jump to for RAISE(IGNORE) */
796 u32 *piOldColMask, /* OUT: Mask of columns used from the OLD.* table */
797 u32 *piNewColMask /* OUT: Mask of columns used from the NEW.* table */
drhc977f7f2002-05-21 11:38:11 +0000798){
danielk1977eecfb3e2006-01-10 12:31:39 +0000799 Trigger *p;
drh0e359b32008-01-13 19:02:11 +0000800 sqlite3 *db = pParse->db;
drh67040462004-09-24 12:24:36 +0000801 TriggerStack trigStackEntry;
danielk1977c3f9bad2002-05-15 08:30:12 +0000802
danielk19778f2c54e2008-01-01 19:02:09 +0000803 trigStackEntry.oldColMask = 0;
804 trigStackEntry.newColMask = 0;
805
danielk1977c3f9bad2002-05-15 08:30:12 +0000806 assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE);
drhdca76842004-12-07 14:06:13 +0000807 assert(tr_tm == TRIGGER_BEFORE || tr_tm == TRIGGER_AFTER );
danielk1977c3f9bad2002-05-15 08:30:12 +0000808
danielk1977633ed082002-05-17 00:05:58 +0000809 assert(newIdx != -1 || oldIdx != -1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000810
danielk19772f886d12009-02-28 10:47:41 +0000811 for(p=pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +0000812 int fire_this = 0;
813
drh9ab4c2e2009-05-09 00:18:38 +0000814 /* Sanity checking: The schema for the trigger and for the table are
815 ** always defined. The trigger must be in the same schema as the table
816 ** or else it must be a TEMP trigger. */
817 assert( p->pSchema!=0 );
818 assert( p->pTabSchema!=0 );
819 assert( p->pSchema==p->pTabSchema || p->pSchema==db->aDb[1].pSchema );
820
danielk1977eecfb3e2006-01-10 12:31:39 +0000821 /* Determine whether we should code this trigger */
822 if(
823 p->op==op &&
824 p->tr_tm==tr_tm &&
drh9ab4c2e2009-05-09 00:18:38 +0000825 checkColumnOverlap(p->pColumns,pChanges)
danielk1977eecfb3e2006-01-10 12:31:39 +0000826 ){
827 TriggerStack *pS; /* Pointer to trigger-stack entry */
drh74161702006-02-24 02:53:49 +0000828 for(pS=pParse->trigStack; pS && p!=pS->pTrigger; pS=pS->pNext){}
danielk1977eecfb3e2006-01-10 12:31:39 +0000829 if( !pS ){
830 fire_this = 1;
danielk1977f29ce552002-05-19 23:43:12 +0000831 }
drh229caa32006-03-25 15:52:19 +0000832#if 0 /* Give no warning for recursive triggers. Just do not do them */
833 else{
834 sqlite3ErrorMsg(pParse, "recursive triggers not supported (%s)",
835 p->name);
836 return SQLITE_ERROR;
837 }
838#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000839 }
drhad6d9462004-09-19 02:15:24 +0000840
drh67040462004-09-24 12:24:36 +0000841 if( fire_this ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000842 int endTrigger;
danielk1977c3f9bad2002-05-15 08:30:12 +0000843 Expr * whenExpr;
drh85e20962003-04-25 17:52:11 +0000844 AuthContext sContext;
danielk1977b3bce662005-01-29 08:32:43 +0000845 NameContext sNC;
danielk1977c3f9bad2002-05-15 08:30:12 +0000846
drh949f9cd2008-01-12 21:35:57 +0000847#ifndef SQLITE_OMIT_TRACE
848 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Trace, 0, 0, 0,
drh0e359b32008-01-13 19:02:11 +0000849 sqlite3MPrintf(db, "-- TRIGGER %s", p->name),
drh949f9cd2008-01-12 21:35:57 +0000850 P4_DYNAMIC);
851#endif
danielk1977b3bce662005-01-29 08:32:43 +0000852 memset(&sNC, 0, sizeof(sNC));
853 sNC.pParse = pParse;
danielk1977c3f9bad2002-05-15 08:30:12 +0000854
855 /* Push an entry on to the trigger stack */
danielk1977eecfb3e2006-01-10 12:31:39 +0000856 trigStackEntry.pTrigger = p;
drh67040462004-09-24 12:24:36 +0000857 trigStackEntry.newIdx = newIdx;
858 trigStackEntry.oldIdx = oldIdx;
859 trigStackEntry.pTab = pTab;
860 trigStackEntry.pNext = pParse->trigStack;
861 trigStackEntry.ignoreJump = ignoreJump;
862 pParse->trigStack = &trigStackEntry;
danielk1977eecfb3e2006-01-10 12:31:39 +0000863 sqlite3AuthContextPush(pParse, &sContext, p->name);
danielk1977c3f9bad2002-05-15 08:30:12 +0000864
865 /* code the WHEN clause */
danielk19774adee202004-05-08 08:23:19 +0000866 endTrigger = sqlite3VdbeMakeLabel(pParse->pVdbe);
danielk19776ab3a2e2009-02-19 14:39:25 +0000867 whenExpr = sqlite3ExprDup(db, p->pWhen, 0);
drh7d10d5a2008-08-20 16:35:10 +0000868 if( db->mallocFailed || sqlite3ResolveExprNames(&sNC, whenExpr) ){
drh67040462004-09-24 12:24:36 +0000869 pParse->trigStack = trigStackEntry.pNext;
drh633e6d52008-07-28 19:34:53 +0000870 sqlite3ExprDelete(db, whenExpr);
drh9adf9ac2002-05-15 11:44:13 +0000871 return 1;
danielk1977c3f9bad2002-05-15 08:30:12 +0000872 }
drh35573352008-01-08 23:54:25 +0000873 sqlite3ExprIfFalse(pParse, whenExpr, endTrigger, SQLITE_JUMPIFNULL);
drh633e6d52008-07-28 19:34:53 +0000874 sqlite3ExprDelete(db, whenExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000875
danielk1977eecfb3e2006-01-10 12:31:39 +0000876 codeTriggerProgram(pParse, p->step_list, orconf);
danielk1977c3f9bad2002-05-15 08:30:12 +0000877
878 /* Pop the entry off the trigger stack */
drh67040462004-09-24 12:24:36 +0000879 pParse->trigStack = trigStackEntry.pNext;
danielk19774adee202004-05-08 08:23:19 +0000880 sqlite3AuthContextPop(&sContext);
danielk1977c3f9bad2002-05-15 08:30:12 +0000881
danielk19774adee202004-05-08 08:23:19 +0000882 sqlite3VdbeResolveLabel(pParse->pVdbe, endTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000883 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000884 }
danielk19778f2c54e2008-01-01 19:02:09 +0000885 if( piOldColMask ) *piOldColMask |= trigStackEntry.oldColMask;
886 if( piNewColMask ) *piNewColMask |= trigStackEntry.newColMask;
danielk1977c3f9bad2002-05-15 08:30:12 +0000887 return 0;
888}
drhb7f91642004-10-31 02:22:47 +0000889#endif /* !defined(SQLITE_OMIT_TRIGGER) */