blob: f06226734a724163ce028ad1dd644f5f88487cdd [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**
shane5eff7cf2009-08-10 03:57:58 +000013** $Id: trigger.c,v 1.143 2009/08/10 03:57:58 shane 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 sqlite3ExprDelete(db, pTmp->pWhere);
27 sqlite3ExprListDelete(db, pTmp->pExprList);
28 sqlite3SelectDelete(db, pTmp->pSelect);
29 sqlite3IdListDelete(db, pTmp->pIdList);
drh4b59ab52002-08-24 18:24:51 +000030
drh633e6d52008-07-28 19:34:53 +000031 sqlite3DbFree(db, pTmp);
drh4b59ab52002-08-24 18:24:51 +000032 }
33}
34
35/*
danielk19772f886d12009-02-28 10:47:41 +000036** Given table pTab, return a list of all the triggers attached to
37** the table. The list is connected by Trigger.pNext pointers.
drh9ab4c2e2009-05-09 00:18:38 +000038**
39** All of the triggers on pTab that are in the same database as pTab
40** are already attached to pTab->pTrigger. But there might be additional
41** triggers on pTab in the TEMP schema. This routine prepends all
42** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
43** and returns the combined list.
44**
45** To state it another way: This routine returns a list of all triggers
46** that fire off of pTab. The list will include any TEMP triggers on
47** pTab as well as the triggers lised in pTab->pTrigger.
danielk19772f886d12009-02-28 10:47:41 +000048*/
49Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
50 Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
51 Trigger *pList = 0; /* List of triggers to return */
52
53 if( pTmpSchema!=pTab->pSchema ){
54 HashElem *p;
55 for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
56 Trigger *pTrig = (Trigger *)sqliteHashData(p);
57 if( pTrig->pTabSchema==pTab->pSchema
58 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
59 ){
60 pTrig->pNext = (pList ? pList : pTab->pTrigger);
61 pList = pTrig;
62 }
63 }
64 }
65
66 return (pList ? pList : pTab->pTrigger);
67}
68
69/*
drhf0f258b2003-04-21 18:48:45 +000070** This is called by the parser when it sees a CREATE TRIGGER statement
71** up to the point of the BEGIN before the trigger actions. A Trigger
72** structure is generated based on the information available and stored
73** in pParse->pNewTrigger. After the trigger actions have been parsed, the
danielk19774adee202004-05-08 08:23:19 +000074** sqlite3FinishTrigger() function is called to complete the trigger
drhf0f258b2003-04-21 18:48:45 +000075** construction process.
drh9adf9ac2002-05-15 11:44:13 +000076*/
danielk19774adee202004-05-08 08:23:19 +000077void sqlite3BeginTrigger(
drh9adf9ac2002-05-15 11:44:13 +000078 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
danielk1977ef2cb632004-05-29 02:37:19 +000079 Token *pName1, /* The name of the trigger */
80 Token *pName2, /* The name of the trigger */
drh5cf590c2003-04-24 01:45:04 +000081 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
drh9adf9ac2002-05-15 11:44:13 +000082 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
danielk1977633ed082002-05-17 00:05:58 +000083 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
drhd24cc422003-03-27 12:51:24 +000084 SrcList *pTableName,/* The name of the table/view the trigger applies to */
drh9adf9ac2002-05-15 11:44:13 +000085 Expr *pWhen, /* WHEN clause */
drhfdd48a72006-09-11 23:45:48 +000086 int isTemp, /* True if the TEMPORARY keyword is present */
87 int noErr /* Suppress errors if the trigger already exists */
drh9adf9ac2002-05-15 11:44:13 +000088){
drh3d5f74b2009-08-06 17:43:31 +000089 Trigger *pTrigger = 0; /* The new trigger */
90 Table *pTab; /* Table that the trigger fires off of */
drhf0f258b2003-04-21 18:48:45 +000091 char *zName = 0; /* Name of the trigger */
drh3d5f74b2009-08-06 17:43:31 +000092 sqlite3 *db = pParse->db; /* The database connection */
danielk1977ef2cb632004-05-29 02:37:19 +000093 int iDb; /* The database to store the trigger in */
94 Token *pName; /* The unqualified db name */
drh3d5f74b2009-08-06 17:43:31 +000095 DbFixer sFix; /* State vector for the DB fixer */
96 int iTabDb; /* Index of the database holding pTab */
drhed6c8672003-01-12 18:02:16 +000097
drh43617e92006-03-06 20:55:46 +000098 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
99 assert( pName2!=0 );
drhaa78bec2008-12-09 03:55:14 +0000100 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
101 assert( op>0 && op<0xff );
danielk1977ef2cb632004-05-29 02:37:19 +0000102 if( isTemp ){
103 /* If TEMP was specified, then the trigger name may not be qualified. */
drh43617e92006-03-06 20:55:46 +0000104 if( pName2->n>0 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000105 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
106 goto trigger_cleanup;
107 }
108 iDb = 1;
109 pName = pName1;
110 }else{
111 /* Figure out the db that the the trigger will be created in */
112 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
113 if( iDb<0 ){
114 goto trigger_cleanup;
115 }
116 }
117
118 /* If the trigger name was unqualified, and the table is a temp table,
119 ** then set iDb to 1 to create the trigger in the temporary database.
120 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
121 ** exist, the error is caught by the block below.
drh9adf9ac2002-05-15 11:44:13 +0000122 */
drh17435752007-08-16 04:30:38 +0000123 if( !pTableName || db->mallocFailed ){
drh6f7adc82006-01-11 21:41:20 +0000124 goto trigger_cleanup;
125 }
danielk1977ef2cb632004-05-29 02:37:19 +0000126 pTab = sqlite3SrcListLookup(pParse, pTableName);
danielk1977da184232006-01-05 11:34:32 +0000127 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977ef2cb632004-05-29 02:37:19 +0000128 iDb = 1;
129 }
130
131 /* Ensure the table name matches database name and that the table exists */
drh17435752007-08-16 04:30:38 +0000132 if( db->mallocFailed ) goto trigger_cleanup;
drhd24cc422003-03-27 12:51:24 +0000133 assert( pTableName->nSrc==1 );
danielk1977ef2cb632004-05-29 02:37:19 +0000134 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) &&
135 sqlite3FixSrcList(&sFix, pTableName) ){
drh4312db52003-06-03 01:47:11 +0000136 goto trigger_cleanup;
drhf26e09c2003-05-31 16:21:12 +0000137 }
danielk1977ef2cb632004-05-29 02:37:19 +0000138 pTab = sqlite3SrcListLookup(pParse, pTableName);
139 if( !pTab ){
140 /* The table does not exist. */
drh3d5f74b2009-08-06 17:43:31 +0000141 if( db->init.iDb==1 ){
142 /* Ticket #3810.
143 ** Normally, whenever a table is dropped, all associated triggers are
144 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
145 ** and the table is dropped by a different database connection, the
146 ** trigger is not visible to the database connection that does the
147 ** drop so the trigger cannot be dropped. This results in an
148 ** "orphaned trigger" - a trigger whose associated table is missing.
149 */
150 db->init.orphanTrigger = 1;
151 }
drhd24cc422003-03-27 12:51:24 +0000152 goto trigger_cleanup;
153 }
drh4cbdda92006-06-14 19:00:20 +0000154 if( IsVirtual(pTab) ){
155 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
156 goto trigger_cleanup;
157 }
drhd24cc422003-03-27 12:51:24 +0000158
danielk1977d8123362004-06-12 09:25:12 +0000159 /* Check that the trigger name is not reserved and that no trigger of the
160 ** specified name exists */
drh17435752007-08-16 04:30:38 +0000161 zName = sqlite3NameFromToken(db, pName);
danielk1977d8123362004-06-12 09:25:12 +0000162 if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
163 goto trigger_cleanup;
164 }
drhaa78bec2008-12-09 03:55:14 +0000165 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
drhea678832008-12-10 19:26:22 +0000166 zName, sqlite3Strlen30(zName)) ){
drhfdd48a72006-09-11 23:45:48 +0000167 if( !noErr ){
168 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
169 }
drhe5f9c642003-01-13 23:27:31 +0000170 goto trigger_cleanup;
danielk1977c3f9bad2002-05-15 08:30:12 +0000171 }
danielk1977ef2cb632004-05-29 02:37:19 +0000172
173 /* Do not create a trigger on a system table */
drhdca76842004-12-07 14:06:13 +0000174 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk19774adee202004-05-08 08:23:19 +0000175 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
drhd24cc422003-03-27 12:51:24 +0000176 pParse->nErr++;
177 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40 +0000178 }
danielk1977ef2cb632004-05-29 02:37:19 +0000179
180 /* INSTEAD of triggers are only for views and views only support INSTEAD
181 ** of triggers.
182 */
183 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000184 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drhda93d232003-03-31 02:12:46 +0000185 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000186 goto trigger_cleanup;
187 }
danielk1977ef2cb632004-05-29 02:37:19 +0000188 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000189 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drhda93d232003-03-31 02:12:46 +0000190 " trigger on table: %S", pTableName, 0);
drhd24cc422003-03-27 12:51:24 +0000191 goto trigger_cleanup;
192 }
danielk1977da184232006-01-05 11:34:32 +0000193 iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977ef2cb632004-05-29 02:37:19 +0000194
drhd24cc422003-03-27 12:51:24 +0000195#ifndef SQLITE_OMIT_AUTHORIZATION
196 {
197 int code = SQLITE_CREATE_TRIGGER;
danielk1977da184232006-01-05 11:34:32 +0000198 const char *zDb = db->aDb[iTabDb].zName;
drhe22a3342003-04-22 20:30:37 +0000199 const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
danielk1977da184232006-01-05 11:34:32 +0000200 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19 +0000201 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24 +0000202 goto trigger_cleanup;
203 }
danielk1977da184232006-01-05 11:34:32 +0000204 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24 +0000205 goto trigger_cleanup;
206 }
207 }
208#endif
danielk1977d702fcc2002-05-26 23:24:40 +0000209
danielk1977ef2cb632004-05-29 02:37:19 +0000210 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04 +0000211 ** cannot appear on views. So we might as well translate every
212 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
213 ** elsewhere.
214 */
danielk1977d702fcc2002-05-26 23:24:40 +0000215 if (tr_tm == TK_INSTEAD){
216 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12 +0000217 }
218
219 /* Build the Trigger object */
drh17435752007-08-16 04:30:38 +0000220 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19 +0000221 if( pTrigger==0 ) goto trigger_cleanup;
dan165921a2009-08-28 18:53:45 +0000222 pTrigger->zName = zName;
drhe5f9c642003-01-13 23:27:31 +0000223 zName = 0;
drh17435752007-08-16 04:30:38 +0000224 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32 +0000225 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48 +0000226 pTrigger->pTabSchema = pTab->pSchema;
drhaa78bec2008-12-09 03:55:14 +0000227 pTrigger->op = (u8)op;
drhdca76842004-12-07 14:06:13 +0000228 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
danielk19776ab3a2e2009-02-19 14:39:25 +0000229 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
drh17435752007-08-16 04:30:38 +0000230 pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
drhf0f258b2003-04-21 18:48:45 +0000231 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19 +0000232 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45 +0000233
234trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000235 sqlite3DbFree(db, zName);
236 sqlite3SrcListDelete(db, pTableName);
237 sqlite3IdListDelete(db, pColumns);
238 sqlite3ExprDelete(db, pWhen);
danielk1977d5d56522005-03-16 12:15:20 +0000239 if( !pParse->pNewTrigger ){
drh633e6d52008-07-28 19:34:53 +0000240 sqlite3DeleteTrigger(db, pTrigger);
danielk1977d5d56522005-03-16 12:15:20 +0000241 }else{
242 assert( pParse->pNewTrigger==pTrigger );
243 }
drhf0f258b2003-04-21 18:48:45 +0000244}
245
246/*
247** This routine is called after all of the trigger actions have been parsed
248** in order to complete the process of building the trigger.
249*/
danielk19774adee202004-05-08 08:23:19 +0000250void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45 +0000251 Parse *pParse, /* Parser context */
252 TriggerStep *pStepList, /* The triggered program */
253 Token *pAll /* Token that describes the complete CREATE TRIGGER */
254){
danielk19772f886d12009-02-28 10:47:41 +0000255 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
256 char *zName; /* Name of trigger */
257 sqlite3 *db = pParse->db; /* The database */
drhf26e09c2003-05-31 16:21:12 +0000258 DbFixer sFix;
danielk19772f886d12009-02-28 10:47:41 +0000259 int iDb; /* Database containing the trigger */
drhb7916a72009-05-27 10:31:29 +0000260 Token nameToken; /* Trigger name for error reporting */
drhf0f258b2003-04-21 18:48:45 +0000261
danielk1977bfb9e352005-01-24 13:03:32 +0000262 pTrig = pParse->pNewTrigger;
drhf0f258b2003-04-21 18:48:45 +0000263 pParse->pNewTrigger = 0;
drh9ab4c2e2009-05-09 00:18:38 +0000264 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
dan165921a2009-08-28 18:53:45 +0000265 zName = pTrig->zName;
danielk1977da184232006-01-05 11:34:32 +0000266 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32 +0000267 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53 +0000268 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32 +0000269 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53 +0000270 pStepList = pStepList->pNext;
271 }
dan165921a2009-08-28 18:53:45 +0000272 nameToken.z = pTrig->zName;
drhb7916a72009-05-27 10:31:29 +0000273 nameToken.n = sqlite3Strlen30(nameToken.z);
274 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken)
danielk1977bfb9e352005-01-24 13:03:32 +0000275 && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
drhf26e09c2003-05-31 16:21:12 +0000276 goto triggerfinish_cleanup;
277 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000278
279 /* if we are not initializing, and this trigger is not on a TEMP table,
drh9adf9ac2002-05-15 11:44:13 +0000280 ** build the sqlite_master entry
281 */
drh1d85d932004-02-14 23:05:52 +0000282 if( !db->init.busy ){
drhc977f7f2002-05-21 11:38:11 +0000283 Vdbe *v;
drh2d401ab2008-01-10 23:50:11 +0000284 char *z;
danielk1977c3f9bad2002-05-15 08:30:12 +0000285
286 /* Make an entry in the sqlite_master table */
danielk19774adee202004-05-08 08:23:19 +0000287 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000288 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000289 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11 +0000290 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
291 sqlite3NestedParse(pParse,
292 "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
danielk19772f886d12009-02-28 10:47:41 +0000293 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
drh2d401ab2008-01-10 23:50:11 +0000294 pTrig->table, z);
drh633e6d52008-07-28 19:34:53 +0000295 sqlite3DbFree(db, z);
drh9cbf3422008-01-17 16:22:13 +0000296 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +0000297 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, sqlite3MPrintf(
danielk19772f886d12009-02-28 10:47:41 +0000298 db, "type='trigger' AND name='%q'", zName), P4_DYNAMIC
danielk19771e536952007-08-16 10:09:01 +0000299 );
danielk1977c3f9bad2002-05-15 08:30:12 +0000300 }
301
drh956bc922004-07-24 17:38:29 +0000302 if( db->init.busy ){
danielk19772f886d12009-02-28 10:47:41 +0000303 Trigger *pLink = pTrig;
304 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
305 pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);
306 if( pTrig ){
drhf3a65f72007-08-22 20:18:21 +0000307 db->mallocFailed = 1;
danielk19772f886d12009-02-28 10:47:41 +0000308 }else if( pLink->pSchema==pLink->pTabSchema ){
309 Table *pTab;
drha83ccca2009-04-28 13:01:09 +0000310 int n = sqlite3Strlen30(pLink->table);
danielk19772f886d12009-02-28 10:47:41 +0000311 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n);
312 assert( pTab!=0 );
313 pLink->pNext = pTab->pTrigger;
314 pTab->pTrigger = pLink;
danielk1977d5d56522005-03-16 12:15:20 +0000315 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000316 }
317
drhf0f258b2003-04-21 18:48:45 +0000318triggerfinish_cleanup:
drh633e6d52008-07-28 19:34:53 +0000319 sqlite3DeleteTrigger(db, pTrig);
danielk1977bfb9e352005-01-24 13:03:32 +0000320 assert( !pParse->pNewTrigger );
drh633e6d52008-07-28 19:34:53 +0000321 sqlite3DeleteTriggerStep(db, pStepList);
drh4b59ab52002-08-24 18:24:51 +0000322}
danielk1977c3f9bad2002-05-15 08:30:12 +0000323
drh4b59ab52002-08-24 18:24:51 +0000324/*
drhc977f7f2002-05-21 11:38:11 +0000325** Turn a SELECT statement (that the pSelect parameter points to) into
326** a trigger step. Return a pointer to a TriggerStep structure.
327**
328** The parser calls this routine when it finds a SELECT statement in
329** body of a TRIGGER.
330*/
drh17435752007-08-16 04:30:38 +0000331TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
332 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000333 if( pTriggerStep==0 ) {
drh633e6d52008-07-28 19:34:53 +0000334 sqlite3SelectDelete(db, pSelect);
danielk19772e588c72005-12-09 14:25:08 +0000335 return 0;
336 }
danielk1977633ed082002-05-17 00:05:58 +0000337 pTriggerStep->op = TK_SELECT;
338 pTriggerStep->pSelect = pSelect;
339 pTriggerStep->orconf = OE_Default;
drhb7916a72009-05-27 10:31:29 +0000340 return pTriggerStep;
341}
danielk1977c3f9bad2002-05-15 08:30:12 +0000342
drhb7916a72009-05-27 10:31:29 +0000343/*
344** Allocate space to hold a new trigger step. The allocated space
345** holds both the TriggerStep object and the TriggerStep.target.z string.
346**
347** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
348*/
349static TriggerStep *triggerStepAllocate(
350 sqlite3 *db, /* Database connection */
shane5eff7cf2009-08-10 03:57:58 +0000351 u8 op, /* Trigger opcode */
drhb7916a72009-05-27 10:31:29 +0000352 Token *pName /* The target name */
353){
354 TriggerStep *pTriggerStep;
355
356 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n);
357 if( pTriggerStep ){
358 char *z = (char*)&pTriggerStep[1];
359 memcpy(z, pName->z, pName->n);
360 pTriggerStep->target.z = z;
361 pTriggerStep->target.n = pName->n;
362 pTriggerStep->op = op;
363 }
danielk1977633ed082002-05-17 00:05:58 +0000364 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000365}
366
drhc977f7f2002-05-21 11:38:11 +0000367/*
368** Build a trigger step out of an INSERT statement. Return a pointer
369** to the new trigger step.
370**
371** The parser calls this routine when it sees an INSERT inside the
372** body of a trigger.
373*/
danielk19774adee202004-05-08 08:23:19 +0000374TriggerStep *sqlite3TriggerInsertStep(
drh17435752007-08-16 04:30:38 +0000375 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000376 Token *pTableName, /* Name of the table into which we insert */
377 IdList *pColumn, /* List of columns in pTableName to insert into */
378 ExprList *pEList, /* The VALUE clause: a list of values to be inserted */
379 Select *pSelect, /* A SELECT statement that supplies values */
shane5eff7cf2009-08-10 03:57:58 +0000380 u8 orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
danielk1977633ed082002-05-17 00:05:58 +0000381){
drhf3a65f72007-08-22 20:18:21 +0000382 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000383
danielk1977633ed082002-05-17 00:05:58 +0000384 assert(pEList == 0 || pSelect == 0);
drhf3a65f72007-08-22 20:18:21 +0000385 assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000386
drhb7916a72009-05-27 10:31:29 +0000387 pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
danielk1977d5d56522005-03-16 12:15:20 +0000388 if( pTriggerStep ){
drh33e619f2009-05-28 01:00:55 +0000389 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
danielk1977d5d56522005-03-16 12:15:20 +0000390 pTriggerStep->pIdList = pColumn;
drh33e619f2009-05-28 01:00:55 +0000391 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
danielk1977d5d56522005-03-16 12:15:20 +0000392 pTriggerStep->orconf = orconf;
danielk1977d5d56522005-03-16 12:15:20 +0000393 }else{
drh633e6d52008-07-28 19:34:53 +0000394 sqlite3IdListDelete(db, pColumn);
danielk1977d5d56522005-03-16 12:15:20 +0000395 }
drh33e619f2009-05-28 01:00:55 +0000396 sqlite3ExprListDelete(db, pEList);
397 sqlite3SelectDelete(db, pSelect);
danielk1977c3f9bad2002-05-15 08:30:12 +0000398
danielk1977633ed082002-05-17 00:05:58 +0000399 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000400}
401
drhc977f7f2002-05-21 11:38:11 +0000402/*
403** Construct a trigger step that implements an UPDATE statement and return
404** a pointer to that trigger step. The parser calls this routine when it
405** sees an UPDATE statement inside the body of a CREATE TRIGGER.
406*/
danielk19774adee202004-05-08 08:23:19 +0000407TriggerStep *sqlite3TriggerUpdateStep(
drh17435752007-08-16 04:30:38 +0000408 sqlite3 *db, /* The database connection */
drhc977f7f2002-05-21 11:38:11 +0000409 Token *pTableName, /* Name of the table to be updated */
410 ExprList *pEList, /* The SET clause: list of column and new values */
411 Expr *pWhere, /* The WHERE clause */
shane5eff7cf2009-08-10 03:57:58 +0000412 u8 orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
drhc977f7f2002-05-21 11:38:11 +0000413){
drhb7916a72009-05-27 10:31:29 +0000414 TriggerStep *pTriggerStep;
415
416 pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
drh33e619f2009-05-28 01:00:55 +0000417 if( pTriggerStep ){
418 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
419 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
420 pTriggerStep->orconf = orconf;
drh0e3a6f32007-03-30 20:40:34 +0000421 }
drh33e619f2009-05-28 01:00:55 +0000422 sqlite3ExprListDelete(db, pEList);
423 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000424 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000425}
426
drhc977f7f2002-05-21 11:38:11 +0000427/*
428** Construct a trigger step that implements a DELETE statement and return
429** a pointer to that trigger step. The parser calls this routine when it
430** sees a DELETE statement inside the body of a CREATE TRIGGER.
431*/
drh17435752007-08-16 04:30:38 +0000432TriggerStep *sqlite3TriggerDeleteStep(
433 sqlite3 *db, /* Database connection */
434 Token *pTableName, /* The table from which rows are deleted */
435 Expr *pWhere /* The WHERE clause */
436){
drhb7916a72009-05-27 10:31:29 +0000437 TriggerStep *pTriggerStep;
438
439 pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
drh33e619f2009-05-28 01:00:55 +0000440 if( pTriggerStep ){
441 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
442 pTriggerStep->orconf = OE_Default;
drhb63a53d2007-03-31 01:34:44 +0000443 }
drh33e619f2009-05-28 01:00:55 +0000444 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000445 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000446}
447
danielk1977633ed082002-05-17 00:05:58 +0000448/*
449** Recursively delete a Trigger structure
450*/
drh633e6d52008-07-28 19:34:53 +0000451void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
drhf0f258b2003-04-21 18:48:45 +0000452 if( pTrigger==0 ) return;
drh633e6d52008-07-28 19:34:53 +0000453 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
dan165921a2009-08-28 18:53:45 +0000454 sqlite3DbFree(db, pTrigger->zName);
drh633e6d52008-07-28 19:34:53 +0000455 sqlite3DbFree(db, pTrigger->table);
456 sqlite3ExprDelete(db, pTrigger->pWhen);
457 sqlite3IdListDelete(db, pTrigger->pColumns);
drh633e6d52008-07-28 19:34:53 +0000458 sqlite3DbFree(db, pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000459}
460
461/*
danielk19778a414492004-06-29 08:59:35 +0000462** This function is called to drop a trigger from the database schema.
463**
464** This may be called directly from the parser and therefore identifies
465** the trigger by name. The sqlite3DropTriggerPtr() routine does the
466** same job as this routine except it takes a pointer to the trigger
467** instead of the trigger name.
468**/
drhfdd48a72006-09-11 23:45:48 +0000469void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000470 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000471 int i;
472 const char *zDb;
473 const char *zName;
474 int nName;
drh9bb575f2004-09-06 17:24:11 +0000475 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000476
drh17435752007-08-16 04:30:38 +0000477 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000478 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000479 goto drop_trigger_cleanup;
480 }
danielk19778e227872004-06-07 07:52:17 +0000481
drhd24cc422003-03-27 12:51:24 +0000482 assert( pName->nSrc==1 );
483 zDb = pName->a[0].zDatabase;
484 zName = pName->a[0].zName;
drhea678832008-12-10 19:26:22 +0000485 nName = sqlite3Strlen30(zName);
danielk197753c0f742005-03-29 03:10:59 +0000486 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000487 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000488 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
drhe4df0e72006-03-29 00:24:06 +0000489 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
drhd24cc422003-03-27 12:51:24 +0000490 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000491 }
drhd24cc422003-03-27 12:51:24 +0000492 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000493 if( !noErr ){
494 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
495 }
drhd24cc422003-03-27 12:51:24 +0000496 goto drop_trigger_cleanup;
497 }
drh74161702006-02-24 02:53:49 +0000498 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000499
500drop_trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000501 sqlite3SrcListDelete(db, pName);
drh79a519c2003-05-17 19:04:03 +0000502}
503
504/*
drh956bc922004-07-24 17:38:29 +0000505** Return a pointer to the Table structure for the table that a trigger
506** is set on.
507*/
drh74161702006-02-24 02:53:49 +0000508static Table *tableOfTrigger(Trigger *pTrigger){
drha83ccca2009-04-28 13:01:09 +0000509 int n = sqlite3Strlen30(pTrigger->table);
danielk1977aaf22682006-01-06 15:03:48 +0000510 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
drh956bc922004-07-24 17:38:29 +0000511}
512
513
514/*
drh74161702006-02-24 02:53:49 +0000515** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000516*/
drh74161702006-02-24 02:53:49 +0000517void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000518 Table *pTable;
519 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000520 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000521 int iDb;
drh79a519c2003-05-17 19:04:03 +0000522
danielk1977da184232006-01-05 11:34:32 +0000523 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000524 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000525 pTable = tableOfTrigger(pTrigger);
drh43617e92006-03-06 20:55:46 +0000526 assert( pTable );
danielk1977da184232006-01-05 11:34:32 +0000527 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000528#ifndef SQLITE_OMIT_AUTHORIZATION
529 {
530 int code = SQLITE_DROP_TRIGGER;
drh956bc922004-07-24 17:38:29 +0000531 const char *zDb = db->aDb[iDb].zName;
532 const char *zTab = SCHEMA_TABLE(iDb);
533 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
dan2bd93512009-08-31 08:22:46 +0000534 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
danielk19774adee202004-05-08 08:23:19 +0000535 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000536 return;
drhe5f9c642003-01-13 23:27:31 +0000537 }
drhed6c8672003-01-12 18:02:16 +0000538 }
drhe5f9c642003-01-13 23:27:31 +0000539#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000540
drhf0f258b2003-04-21 18:48:45 +0000541 /* Generate code to destroy the database record of the trigger.
542 */
drh43617e92006-03-06 20:55:46 +0000543 assert( pTable!=0 );
544 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drhf0f258b2003-04-21 18:48:45 +0000545 int base;
drh57196282004-10-06 15:41:16 +0000546 static const VdbeOpList dropTrigger[] = {
drh9b1b01b2003-08-16 12:37:51 +0000547 { OP_Rewind, 0, ADDR(9), 0},
drh1db639c2008-01-17 02:36:28 +0000548 { OP_String8, 0, 1, 0}, /* 1 */
549 { OP_Column, 0, 1, 2},
550 { OP_Ne, 2, ADDR(8), 1},
551 { OP_String8, 0, 1, 0}, /* 4: "trigger" */
552 { OP_Column, 0, 0, 2},
553 { OP_Ne, 2, ADDR(8), 1},
drhf0f258b2003-04-21 18:48:45 +0000554 { OP_Delete, 0, 0, 0},
drh9b1b01b2003-08-16 12:37:51 +0000555 { OP_Next, 0, ADDR(1), 0}, /* 8 */
drhf0f258b2003-04-21 18:48:45 +0000556 };
557
drh956bc922004-07-24 17:38:29 +0000558 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk1977c00da102006-01-07 13:21:04 +0000559 sqlite3OpenMasterTable(pParse, iDb);
danielk19774adee202004-05-08 08:23:19 +0000560 base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger);
dan165921a2009-08-28 18:53:45 +0000561 sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, 0);
drh24003452008-01-03 01:28:59 +0000562 sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
drh9cbf3422008-01-17 16:22:13 +0000563 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +0000564 sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
dan165921a2009-08-28 18:53:45 +0000565 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
danielk19776ab3a2e2009-02-19 14:39:25 +0000566 if( pParse->nMem<3 ){
567 pParse->nMem = 3;
568 }
drhf0f258b2003-04-21 18:48:45 +0000569 }
drh956bc922004-07-24 17:38:29 +0000570}
drhf0f258b2003-04-21 18:48:45 +0000571
drh956bc922004-07-24 17:38:29 +0000572/*
573** Remove a trigger from the hash tables of the sqlite* pointer.
574*/
575void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
danielk19772f886d12009-02-28 10:47:41 +0000576 Hash *pHash = &(db->aDb[iDb].pSchema->trigHash);
drh956bc922004-07-24 17:38:29 +0000577 Trigger *pTrigger;
danielk19772f886d12009-02-28 10:47:41 +0000578 pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0);
drh9ab4c2e2009-05-09 00:18:38 +0000579 if( ALWAYS(pTrigger) ){
danielk19772f886d12009-02-28 10:47:41 +0000580 if( pTrigger->pSchema==pTrigger->pTabSchema ){
581 Table *pTab = tableOfTrigger(pTrigger);
582 Trigger **pp;
583 for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
584 *pp = (*pp)->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +0000585 }
drh633e6d52008-07-28 19:34:53 +0000586 sqlite3DeleteTrigger(db, pTrigger);
drh956bc922004-07-24 17:38:29 +0000587 db->flags |= SQLITE_InternChanges;
danielk1977c3f9bad2002-05-15 08:30:12 +0000588 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000589}
590
drhc977f7f2002-05-21 11:38:11 +0000591/*
592** pEList is the SET clause of an UPDATE statement. Each entry
593** in pEList is of the format <id>=<expr>. If any of the entries
594** in pEList have an <id> which matches an identifier in pIdList,
595** then return TRUE. If pIdList==NULL, then it is considered a
596** wildcard that matches anything. Likewise if pEList==NULL then
597** it matches anything so always return true. Return false only
598** if there is no match.
599*/
drh9ab4c2e2009-05-09 00:18:38 +0000600static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000601 int e;
drh9ab4c2e2009-05-09 00:18:38 +0000602 if( pIdList==0 || NEVER(pEList==0) ) return 1;
drhad2d8302002-05-24 20:31:36 +0000603 for(e=0; e<pEList->nExpr; e++){
danielk19774adee202004-05-08 08:23:19 +0000604 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000605 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000606 return 0;
607}
608
danielk1977c3f9bad2002-05-15 08:30:12 +0000609/*
danielk19772f886d12009-02-28 10:47:41 +0000610** Return a list of all triggers on table pTab if there exists at least
611** one trigger that must be fired when an operation of type 'op' is
612** performed on the table, and, if that operation is an UPDATE, if at
613** least one of the columns in pChanges is being modified.
drhdca76842004-12-07 14:06:13 +0000614*/
danielk19772f886d12009-02-28 10:47:41 +0000615Trigger *sqlite3TriggersExist(
616 Parse *pParse, /* Parse context */
drhdca76842004-12-07 14:06:13 +0000617 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000618 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
danielk19772f886d12009-02-28 10:47:41 +0000619 ExprList *pChanges, /* Columns that change in an UPDATE statement */
620 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
drhc977f7f2002-05-21 11:38:11 +0000621){
drhdca76842004-12-07 14:06:13 +0000622 int mask = 0;
danielk19772f886d12009-02-28 10:47:41 +0000623 Trigger *pList = sqlite3TriggerList(pParse, pTab);
624 Trigger *p;
625 assert( pList==0 || IsVirtual(pTab)==0 );
626 for(p=pList; p; p=p->pNext){
drh9ab4c2e2009-05-09 00:18:38 +0000627 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
danielk19772f886d12009-02-28 10:47:41 +0000628 mask |= p->tr_tm;
danielk1977c3f9bad2002-05-15 08:30:12 +0000629 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000630 }
danielk19772f886d12009-02-28 10:47:41 +0000631 if( pMask ){
632 *pMask = mask;
633 }
634 return (mask ? pList : 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000635}
636
drhc977f7f2002-05-21 11:38:11 +0000637/*
drhf26e09c2003-05-31 16:21:12 +0000638** Convert the pStep->target token into a SrcList and return a pointer
639** to that SrcList.
640**
641** This routine adds a specific database name, if needed, to the target when
642** forming the SrcList. This prevents a trigger in one database from
643** referring to a target in another database. An exception is when the
644** trigger is in TEMP in which case it can refer to any other database it
645** wants.
646*/
647static SrcList *targetSrcList(
648 Parse *pParse, /* The parsing context */
649 TriggerStep *pStep /* The trigger containing the target token */
650){
drhf26e09c2003-05-31 16:21:12 +0000651 int iDb; /* Index of the database to use */
652 SrcList *pSrc; /* SrcList to be returned */
653
drhb7916a72009-05-27 10:31:29 +0000654 pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
655 if( pSrc ){
656 assert( pSrc->nSrc>0 );
657 assert( pSrc->a!=0 );
658 iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
659 if( iDb==0 || iDb>=2 ){
660 sqlite3 *db = pParse->db;
661 assert( iDb<pParse->db->nDb );
662 pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
663 }
drhf26e09c2003-05-31 16:21:12 +0000664 }
665 return pSrc;
666}
667
668/*
dan65a7cd12009-09-01 12:16:01 +0000669** Generate VDBE code for the statements inside the body of a single
670** trigger.
drhc977f7f2002-05-21 11:38:11 +0000671*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000672static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000673 Parse *pParse, /* The parser context */
674 TriggerStep *pStepList, /* List of statements inside the trigger body */
dan65a7cd12009-09-01 12:16:01 +0000675 int orconf /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000676){
dan65a7cd12009-09-01 12:16:01 +0000677 TriggerStep *pStep;
drh344737f2004-09-19 00:50:20 +0000678 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000679 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000680
dan65a7cd12009-09-01 12:16:01 +0000681 assert( pParse->pTriggerTab && pParse->pToplevel );
682 assert( pStepList );
drh344737f2004-09-19 00:50:20 +0000683 assert( v!=0 );
dan65a7cd12009-09-01 12:16:01 +0000684 for(pStep=pStepList; pStep; pStep=pStep->pNext){
dan165921a2009-08-28 18:53:45 +0000685 /* Figure out the ON CONFLICT policy that will be used for this step
686 ** of the trigger program. If the statement that caused this trigger
687 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
688 ** the ON CONFLICT policy that was specified as part of the trigger
689 ** step statement. Example:
690 **
691 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
692 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
693 ** END;
694 **
695 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
696 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
697 */
shanecea72b22009-09-07 04:38:36 +0000698 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
dan165921a2009-08-28 18:53:45 +0000699
700 switch( pStep->op ){
danielk1977633ed082002-05-17 00:05:58 +0000701 case TK_UPDATE: {
dan165921a2009-08-28 18:53:45 +0000702 sqlite3Update(pParse,
703 targetSrcList(pParse, pStep),
704 sqlite3ExprListDup(db, pStep->pExprList, 0),
705 sqlite3ExprDup(db, pStep->pWhere, 0),
dan65a7cd12009-09-01 12:16:01 +0000706 pParse->eOrconf
dan165921a2009-08-28 18:53:45 +0000707 );
danielk1977633ed082002-05-17 00:05:58 +0000708 break;
709 }
710 case TK_INSERT: {
dan165921a2009-08-28 18:53:45 +0000711 sqlite3Insert(pParse,
712 targetSrcList(pParse, pStep),
713 sqlite3ExprListDup(db, pStep->pExprList, 0),
714 sqlite3SelectDup(db, pStep->pSelect, 0),
715 sqlite3IdListDup(db, pStep->pIdList),
dan65a7cd12009-09-01 12:16:01 +0000716 pParse->eOrconf
dan165921a2009-08-28 18:53:45 +0000717 );
danielk1977633ed082002-05-17 00:05:58 +0000718 break;
719 }
720 case TK_DELETE: {
dan165921a2009-08-28 18:53:45 +0000721 sqlite3DeleteFrom(pParse,
722 targetSrcList(pParse, pStep),
723 sqlite3ExprDup(db, pStep->pWhere, 0)
724 );
danielk1977633ed082002-05-17 00:05:58 +0000725 break;
726 }
dan165921a2009-08-28 18:53:45 +0000727 default: assert( pStep->op==TK_SELECT ); {
728 SelectDest sDest;
729 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
730 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
731 sqlite3Select(pParse, pSelect, &sDest);
732 sqlite3SelectDelete(db, pSelect);
drh9ab4c2e2009-05-09 00:18:38 +0000733 break;
734 }
danielk1977633ed082002-05-17 00:05:58 +0000735 }
dan76d462e2009-08-30 11:42:51 +0000736 if( pStep->op!=TK_SELECT ){
drhb7f1d9a2009-09-08 02:27:58 +0000737 sqlite3VdbeAddOp0(v, OP_ResetCount);
dan76d462e2009-08-30 11:42:51 +0000738 }
danielk1977633ed082002-05-17 00:05:58 +0000739 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000740
danielk1977633ed082002-05-17 00:05:58 +0000741 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000742}
743
dan165921a2009-08-28 18:53:45 +0000744#ifdef SQLITE_DEBUG
745/*
746** This function is used to add VdbeComment() annotations to a VDBE
747** program. It is not used in production code, only for debugging.
748*/
749static const char *onErrorText(int onError){
750 switch( onError ){
751 case OE_Abort: return "abort";
752 case OE_Rollback: return "rollback";
753 case OE_Fail: return "fail";
754 case OE_Replace: return "replace";
755 case OE_Ignore: return "ignore";
756 case OE_Default: return "default";
757 }
758 return "n/a";
759}
760#endif
761
762/*
763** Parse context structure pFrom has just been used to create a sub-vdbe
764** (trigger program). If an error has occurred, transfer error information
765** from pFrom to pTo.
766*/
767static void transferParseError(Parse *pTo, Parse *pFrom){
768 assert( pFrom->zErrMsg==0 || pFrom->nErr );
769 assert( pTo->zErrMsg==0 || pTo->nErr );
770 if( pTo->nErr==0 ){
771 pTo->zErrMsg = pFrom->zErrMsg;
772 pTo->nErr = pFrom->nErr;
773 }else{
774 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
775 }
776}
777
dan65a7cd12009-09-01 12:16:01 +0000778/*
779** Create and populate a new TriggerPrg object with a sub-program
780** implementing trigger pTrigger with ON CONFLICT policy orconf.
781*/
dan2832ad42009-08-31 15:27:27 +0000782static TriggerPrg *codeRowTrigger(
dan165921a2009-08-28 18:53:45 +0000783 Parse *pParse, /* Current parse context */
784 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +0000785 Table *pTab, /* The table pTrigger is attached to */
786 int orconf /* ON CONFLICT policy to code trigger program with */
dan165921a2009-08-28 18:53:45 +0000787){
dan65a7cd12009-09-01 12:16:01 +0000788 Parse *pTop = sqlite3ParseToplevel(pParse);
789 sqlite3 *db = pParse->db; /* Database handle */
790 TriggerPrg *pPrg; /* Value to return */
dan165921a2009-08-28 18:53:45 +0000791 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
792 Vdbe *v; /* Temporary VM */
dan165921a2009-08-28 18:53:45 +0000793 NameContext sNC; /* Name context for sub-vdbe */
794 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
795 Parse *pSubParse; /* Parse context for sub-vdbe */
796 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
797
dan65a7cd12009-09-01 12:16:01 +0000798 assert( pTab==tableOfTrigger(pTrigger) );
799
800 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
801 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
802 ** list of the top-level Parse object sooner rather than later. */
dan2832ad42009-08-31 15:27:27 +0000803 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
804 if( !pPrg ) return 0;
dan65a7cd12009-09-01 12:16:01 +0000805 pPrg->pNext = pTop->pTriggerPrg;
806 pTop->pTriggerPrg = pPrg;
dan2832ad42009-08-31 15:27:27 +0000807 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
dan165921a2009-08-28 18:53:45 +0000808 if( !pProgram ) return 0;
809 pProgram->nRef = 1;
dan2832ad42009-08-31 15:27:27 +0000810 pPrg->pTrigger = pTrigger;
811 pPrg->orconf = orconf;
drh3991bb02009-09-17 00:41:19 +0000812 pPrg->oldmask = 0xffffffff;
dan165921a2009-08-28 18:53:45 +0000813
dan65a7cd12009-09-01 12:16:01 +0000814 /* Allocate and populate a new Parse context to use for coding the
815 ** trigger sub-program. */
816 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
817 if( !pSubParse ) return 0;
dan165921a2009-08-28 18:53:45 +0000818 memset(&sNC, 0, sizeof(sNC));
819 sNC.pParse = pSubParse;
820 pSubParse->db = db;
821 pSubParse->pTriggerTab = pTab;
dan65a7cd12009-09-01 12:16:01 +0000822 pSubParse->pToplevel = pTop;
dan2bd93512009-08-31 08:22:46 +0000823 pSubParse->zAuthContext = pTrigger->zName;
dan65a7cd12009-09-01 12:16:01 +0000824 pSubParse->eTriggerOp = pTrigger->op;
dan165921a2009-08-28 18:53:45 +0000825
826 v = sqlite3GetVdbe(pSubParse);
827 if( v ){
dan76d462e2009-08-30 11:42:51 +0000828 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
829 pTrigger->zName, onErrorText(orconf),
dan165921a2009-08-28 18:53:45 +0000830 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
dan65a7cd12009-09-01 12:16:01 +0000831 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
832 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
833 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
dan76d462e2009-08-30 11:42:51 +0000834 pTab->zName
dan165921a2009-08-28 18:53:45 +0000835 ));
dan76d462e2009-08-30 11:42:51 +0000836#ifndef SQLITE_OMIT_TRACE
837 sqlite3VdbeChangeP4(v, -1,
838 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
839 );
840#endif
dan165921a2009-08-28 18:53:45 +0000841
dan65a7cd12009-09-01 12:16:01 +0000842 /* If one was specified, code the WHEN clause. If it evaluates to false
843 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
844 ** OP_Halt inserted at the end of the program. */
dan165921a2009-08-28 18:53:45 +0000845 if( pTrigger->pWhen ){
dan165921a2009-08-28 18:53:45 +0000846 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
dan523a0872009-08-31 05:23:32 +0000847 if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
848 && db->mallocFailed==0
849 ){
dan165921a2009-08-28 18:53:45 +0000850 iEndTrigger = sqlite3VdbeMakeLabel(v);
851 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
852 }
853 sqlite3ExprDelete(db, pWhen);
854 }
855
856 /* Code the trigger program into the sub-vdbe. */
dan76d462e2009-08-30 11:42:51 +0000857 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
dan65a7cd12009-09-01 12:16:01 +0000858
859 /* Insert an OP_Halt at the end of the sub-program. */
dan165921a2009-08-28 18:53:45 +0000860 if( iEndTrigger ){
861 sqlite3VdbeResolveLabel(v, iEndTrigger);
862 }
863 sqlite3VdbeAddOp0(v, OP_Halt);
dan76d462e2009-08-30 11:42:51 +0000864 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
865
dan165921a2009-08-28 18:53:45 +0000866 transferParseError(pParse, pSubParse);
dan523a0872009-08-31 05:23:32 +0000867 if( db->mallocFailed==0 ){
dan65a7cd12009-09-01 12:16:01 +0000868 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
dan523a0872009-08-31 05:23:32 +0000869 }
dan165921a2009-08-28 18:53:45 +0000870 pProgram->nMem = pSubParse->nMem;
871 pProgram->nCsr = pSubParse->nTab;
872 pProgram->token = (void *)pTrigger;
dan2832ad42009-08-31 15:27:27 +0000873 pPrg->oldmask = pSubParse->oldmask;
dan165921a2009-08-28 18:53:45 +0000874 sqlite3VdbeDelete(v);
dan165921a2009-08-28 18:53:45 +0000875 }
dan65a7cd12009-09-01 12:16:01 +0000876
877 assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
878 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
dan165921a2009-08-28 18:53:45 +0000879 sqlite3StackFree(db, pSubParse);
880
dan2832ad42009-08-31 15:27:27 +0000881 return pPrg;
dan165921a2009-08-28 18:53:45 +0000882}
883
dan65a7cd12009-09-01 12:16:01 +0000884/*
885** Return a pointer to a TriggerPrg object containing the sub-program for
886** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
887** TriggerPrg object exists, a new object is allocated and populated before
888** being returned.
889*/
dan2832ad42009-08-31 15:27:27 +0000890static TriggerPrg *getRowTrigger(
dan65a7cd12009-09-01 12:16:01 +0000891 Parse *pParse, /* Current parse context */
dan165921a2009-08-28 18:53:45 +0000892 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +0000893 Table *pTab, /* The table trigger pTrigger is attached to */
894 int orconf /* ON CONFLICT algorithm. */
dan165921a2009-08-28 18:53:45 +0000895){
dan65a7cd12009-09-01 12:16:01 +0000896 Parse *pRoot = sqlite3ParseToplevel(pParse);
dan2832ad42009-08-31 15:27:27 +0000897 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +0000898
899 assert( pTab==tableOfTrigger(pTrigger) );
dan165921a2009-08-28 18:53:45 +0000900
901 /* It may be that this trigger has already been coded (or is in the
902 ** process of being coded). If this is the case, then an entry with
dan2832ad42009-08-31 15:27:27 +0000903 ** a matching TriggerPrg.pTrigger field will be present somewhere
904 ** in the Parse.pTriggerPrg list. Search for such an entry. */
dan2832ad42009-08-31 15:27:27 +0000905 for(pPrg=pRoot->pTriggerPrg;
906 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
907 pPrg=pPrg->pNext
dan165921a2009-08-28 18:53:45 +0000908 );
909
dan65a7cd12009-09-01 12:16:01 +0000910 /* If an existing TriggerPrg could not be located, create a new one. */
dan2832ad42009-08-31 15:27:27 +0000911 if( !pPrg ){
dan65a7cd12009-09-01 12:16:01 +0000912 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
dan165921a2009-08-28 18:53:45 +0000913 }
914
dan2832ad42009-08-31 15:27:27 +0000915 return pPrg;
dan165921a2009-08-28 18:53:45 +0000916}
917
danielk1977633ed082002-05-17 00:05:58 +0000918/*
919** This is called to code FOR EACH ROW triggers.
920**
921** When the code that this function generates is executed, the following
922** must be true:
drhc977f7f2002-05-21 11:38:11 +0000923**
924** 1. No cursors may be open in the main database. (But newIdx and oldIdx
925** can be indices of cursors in temporary tables. See below.)
926**
danielk1977633ed082002-05-17 00:05:58 +0000927** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then
928** a temporary vdbe cursor (index newIdx) must be open and pointing at
929** a row containing values to be substituted for new.* expressions in the
930** trigger program(s).
drhc977f7f2002-05-21 11:38:11 +0000931**
danielk1977633ed082002-05-17 00:05:58 +0000932** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then
933** a temporary vdbe cursor (index oldIdx) must be open and pointing at
934** a row containing values to be substituted for old.* expressions in the
935** trigger program(s).
936**
danielk19778f2c54e2008-01-01 19:02:09 +0000937** If they are not NULL, the piOldColMask and piNewColMask output variables
938** are set to values that describe the columns used by the trigger program
939** in the OLD.* and NEW.* tables respectively. If column N of the
940** pseudo-table is read at least once, the corresponding bit of the output
941** mask is set. If a column with an index greater than 32 is read, the
942** output mask is set to the special value 0xffffffff.
943**
danielk1977633ed082002-05-17 00:05:58 +0000944*/
dan165921a2009-08-28 18:53:45 +0000945void sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +0000946 Parse *pParse, /* Parse context */
danielk19772f886d12009-02-28 10:47:41 +0000947 Trigger *pTrigger, /* List of triggers on table pTab */
danielk1977633ed082002-05-17 00:05:58 +0000948 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
949 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +0000950 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +0000951 Table *pTab, /* The table to code triggers from */
952 int newIdx, /* The indice of the "new" row to access */
953 int oldIdx, /* The indice of the "old" row to access */
danielk19776f349032002-06-11 02:25:40 +0000954 int orconf, /* ON CONFLICT policy */
dan165921a2009-08-28 18:53:45 +0000955 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
drhc977f7f2002-05-21 11:38:11 +0000956){
danielk1977eecfb3e2006-01-10 12:31:39 +0000957 Trigger *p;
danielk19778f2c54e2008-01-01 19:02:09 +0000958
shanecea72b22009-09-07 04:38:36 +0000959 UNUSED_PARAMETER(newIdx);
960
danielk1977c3f9bad2002-05-15 08:30:12 +0000961 assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE);
drhdca76842004-12-07 14:06:13 +0000962 assert(tr_tm == TRIGGER_BEFORE || tr_tm == TRIGGER_AFTER );
danielk1977c3f9bad2002-05-15 08:30:12 +0000963
danielk19772f886d12009-02-28 10:47:41 +0000964 for(p=pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +0000965
drh9ab4c2e2009-05-09 00:18:38 +0000966 /* Sanity checking: The schema for the trigger and for the table are
967 ** always defined. The trigger must be in the same schema as the table
968 ** or else it must be a TEMP trigger. */
969 assert( p->pSchema!=0 );
970 assert( p->pTabSchema!=0 );
dan165921a2009-08-28 18:53:45 +0000971 assert( p->pSchema==p->pTabSchema
972 || p->pSchema==pParse->db->aDb[1].pSchema );
drh9ab4c2e2009-05-09 00:18:38 +0000973
danielk1977eecfb3e2006-01-10 12:31:39 +0000974 /* Determine whether we should code this trigger */
dan165921a2009-08-28 18:53:45 +0000975 if( p->op==op
976 && p->tr_tm==tr_tm
977 && checkColumnOverlap(p->pColumns,pChanges)
danielk1977eecfb3e2006-01-10 12:31:39 +0000978 ){
dan165921a2009-08-28 18:53:45 +0000979 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
dan2832ad42009-08-31 15:27:27 +0000980 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +0000981 pPrg = getRowTrigger(pParse, p, pTab, orconf);
dan2832ad42009-08-31 15:27:27 +0000982 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
dan165921a2009-08-28 18:53:45 +0000983
984 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
985 ** is a pointer to the sub-vdbe containing the trigger program. */
dan2832ad42009-08-31 15:27:27 +0000986 if( pPrg ){
dan76d462e2009-08-30 11:42:51 +0000987 sqlite3VdbeAddOp3(v, OP_Program, oldIdx, ignoreJump, ++pParse->nMem);
dan2832ad42009-08-31 15:27:27 +0000988 pPrg->pProgram->nRef++;
989 sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
dan76d462e2009-08-30 11:42:51 +0000990 VdbeComment((v, "Call: %s.%s", p->zName, onErrorText(orconf)));
danielk1977f29ce552002-05-19 23:43:12 +0000991 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000992 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000993 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000994}
dan165921a2009-08-28 18:53:45 +0000995
dan2832ad42009-08-31 15:27:27 +0000996/*
997** Triggers fired by UPDATE or DELETE statements may access values stored
998** in the old.* pseudo-table. This function returns a 32-bit bitmask
999** indicating which columns of the old.* table actually are used by
1000** triggers. This information may be used by the caller to avoid having
1001** to load the entire old.* record into memory when executing an UPDATE
1002** or DELETE command.
1003**
1004** Bit 0 of the returned mask is set if the left-most column of the
1005** table may be accessed using an old.<col> reference. Bit 1 is set if
1006** the second leftmost column value is required, and so on. If there
1007** are more than 32 columns in the table, and at least one of the columns
1008** with an index greater than 32 may be accessed, 0xffffffff is returned.
1009**
1010** It is not possible to determine if the old.rowid column is accessed
1011** by triggers. The caller must always assume that it is.
1012**
1013** There is no equivalent function for new.* references.
1014*/
1015u32 sqlite3TriggerOldmask(
dan165921a2009-08-28 18:53:45 +00001016 Parse *pParse, /* Parse context */
1017 Trigger *pTrigger, /* List of triggers on table pTab */
dan2832ad42009-08-31 15:27:27 +00001018 int op, /* Either TK_UPDATE or TK_DELETE */
dan165921a2009-08-28 18:53:45 +00001019 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
1020 Table *pTab, /* The table to code triggers from */
dan2832ad42009-08-31 15:27:27 +00001021 int orconf /* Default ON CONFLICT policy for trigger steps */
dan165921a2009-08-28 18:53:45 +00001022){
dan2832ad42009-08-31 15:27:27 +00001023 u32 mask = 0;
dan165921a2009-08-28 18:53:45 +00001024 Trigger *p;
dan165921a2009-08-28 18:53:45 +00001025
dan2832ad42009-08-31 15:27:27 +00001026 assert(op==TK_UPDATE || op==TK_DELETE);
dan165921a2009-08-28 18:53:45 +00001027 for(p=pTrigger; p; p=p->pNext){
1028 if( p->op==op && checkColumnOverlap(p->pColumns,pChanges) ){
dan2832ad42009-08-31 15:27:27 +00001029 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +00001030 pPrg = getRowTrigger(pParse, p, pTab, orconf);
dan2832ad42009-08-31 15:27:27 +00001031 if( pPrg ){
1032 mask |= pPrg->oldmask;
dan165921a2009-08-28 18:53:45 +00001033 }
1034 }
1035 }
dan2832ad42009-08-31 15:27:27 +00001036
1037 return mask;
dan165921a2009-08-28 18:53:45 +00001038}
1039
drhb7f91642004-10-31 02:22:47 +00001040#endif /* !defined(SQLITE_OMIT_TRIGGER) */