blob: 3acdf1431c63f5d2914a16c6b168ff67a195732f [file] [log] [blame]
danielk1977c3f9bad2002-05-15 08:30:12 +00001/*
danielk1977633ed082002-05-17 00:05:58 +00002**
3** The author disclaims copyright to this source code. In place of
4** a legal notice, here is a blessing:
5**
6** May you do good and not evil.
7** May you find forgiveness for yourself and forgive others.
8** May you share freely, never taking more than you give.
9**
10*************************************************************************
drhc81c11f2009-11-10 01:30:52 +000011** This file contains the implementation for TRIGGERs
drh9adf9ac2002-05-15 11:44:13 +000012*/
danielk1977c3f9bad2002-05-15 08:30:12 +000013#include "sqliteInt.h"
drh9adf9ac2002-05-15 11:44:13 +000014
drhb7f91642004-10-31 02:22:47 +000015#ifndef SQLITE_OMIT_TRIGGER
danielk1977c3f9bad2002-05-15 08:30:12 +000016/*
drh4b59ab52002-08-24 18:24:51 +000017** Delete a linked list of TriggerStep structures.
18*/
drh633e6d52008-07-28 19:34:53 +000019void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
drh4b59ab52002-08-24 18:24:51 +000020 while( pTriggerStep ){
21 TriggerStep * pTmp = pTriggerStep;
22 pTriggerStep = pTriggerStep->pNext;
23
drh633e6d52008-07-28 19:34:53 +000024 sqlite3ExprDelete(db, pTmp->pWhere);
25 sqlite3ExprListDelete(db, pTmp->pExprList);
26 sqlite3SelectDelete(db, pTmp->pSelect);
27 sqlite3IdListDelete(db, pTmp->pIdList);
drh46d2e5c2018-04-12 13:15:43 +000028 sqlite3UpsertDelete(db, pTmp->pUpsert);
dane7877b22020-07-14 19:51:01 +000029 sqlite3SrcListDelete(db, pTmp->pFrom);
drhf259df52017-12-27 20:38:35 +000030 sqlite3DbFree(db, pTmp->zSpan);
drh4b59ab52002-08-24 18:24:51 +000031
drh633e6d52008-07-28 19:34:53 +000032 sqlite3DbFree(db, pTmp);
drh4b59ab52002-08-24 18:24:51 +000033 }
34}
35
36/*
danielk19772f886d12009-02-28 10:47:41 +000037** Given table pTab, return a list of all the triggers attached to
38** the table. The list is connected by Trigger.pNext pointers.
drh9ab4c2e2009-05-09 00:18:38 +000039**
40** All of the triggers on pTab that are in the same database as pTab
41** are already attached to pTab->pTrigger. But there might be additional
42** triggers on pTab in the TEMP schema. This routine prepends all
43** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
44** and returns the combined list.
45**
46** To state it another way: This routine returns a list of all triggers
47** that fire off of pTab. The list will include any TEMP triggers on
48** pTab as well as the triggers lised in pTab->pTrigger.
danielk19772f886d12009-02-28 10:47:41 +000049*/
50Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
drhf54a80f2021-01-29 13:47:36 +000051 Schema *pTmpSchema; /* Schema of the pTab table */
52 Trigger *pList; /* List of triggers to return */
53 HashElem *p; /* Loop variable for TEMP triggers */
danielk19772f886d12009-02-28 10:47:41 +000054
dand66c8302009-09-28 14:49:01 +000055 if( pParse->disableTriggers ){
56 return 0;
57 }
drhf54a80f2021-01-29 13:47:36 +000058 pTmpSchema = pParse->db->aDb[1].pSchema;
59 p = sqliteHashFirst(&pTmpSchema->trigHash);
60 if( p==0 ){
61 return pTab->pTrigger;
62 }
63 pList = pTab->pTrigger;
danielk19772f886d12009-02-28 10:47:41 +000064 if( pTmpSchema!=pTab->pSchema ){
drhf54a80f2021-01-29 13:47:36 +000065 while( p ){
danielk19772f886d12009-02-28 10:47:41 +000066 Trigger *pTrig = (Trigger *)sqliteHashData(p);
67 if( pTrig->pTabSchema==pTab->pSchema
drhb8352472021-01-29 19:32:17 +000068 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
danielk19772f886d12009-02-28 10:47:41 +000069 ){
drhf54a80f2021-01-29 13:47:36 +000070 pTrig->pNext = pList;
danielk19772f886d12009-02-28 10:47:41 +000071 pList = pTrig;
drhb8352472021-01-29 19:32:17 +000072 }else if( pTrig->op==TK_RETURNING ){
drh381bdac2021-02-04 17:29:04 +000073 assert( pParse->bReturning );
74 assert( &(pParse->u1.pReturning->retTrig) == pTrig );
drhb8352472021-01-29 19:32:17 +000075 pTrig->table = pTab->zName;
76 pTrig->pTabSchema = pTab->pSchema;
77 pTrig->pNext = pList;
78 pList = pTrig;
79 }
drhf54a80f2021-01-29 13:47:36 +000080 p = sqliteHashNext(p);
danielk19772f886d12009-02-28 10:47:41 +000081 }
82 }
drhf54a80f2021-01-29 13:47:36 +000083 return pList;
danielk19772f886d12009-02-28 10:47:41 +000084}
85
86/*
drhf0f258b2003-04-21 18:48:45 +000087** This is called by the parser when it sees a CREATE TRIGGER statement
88** up to the point of the BEGIN before the trigger actions. A Trigger
89** structure is generated based on the information available and stored
90** in pParse->pNewTrigger. After the trigger actions have been parsed, the
danielk19774adee202004-05-08 08:23:19 +000091** sqlite3FinishTrigger() function is called to complete the trigger
drhf0f258b2003-04-21 18:48:45 +000092** construction process.
drh9adf9ac2002-05-15 11:44:13 +000093*/
danielk19774adee202004-05-08 08:23:19 +000094void sqlite3BeginTrigger(
drh9adf9ac2002-05-15 11:44:13 +000095 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
danielk1977ef2cb632004-05-29 02:37:19 +000096 Token *pName1, /* The name of the trigger */
97 Token *pName2, /* The name of the trigger */
drh5cf590c2003-04-24 01:45:04 +000098 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
drh9adf9ac2002-05-15 11:44:13 +000099 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
danielk1977633ed082002-05-17 00:05:58 +0000100 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
drhd24cc422003-03-27 12:51:24 +0000101 SrcList *pTableName,/* The name of the table/view the trigger applies to */
drh9adf9ac2002-05-15 11:44:13 +0000102 Expr *pWhen, /* WHEN clause */
drhfdd48a72006-09-11 23:45:48 +0000103 int isTemp, /* True if the TEMPORARY keyword is present */
104 int noErr /* Suppress errors if the trigger already exists */
drh9adf9ac2002-05-15 11:44:13 +0000105){
drh3d5f74b2009-08-06 17:43:31 +0000106 Trigger *pTrigger = 0; /* The new trigger */
107 Table *pTab; /* Table that the trigger fires off of */
drhf0f258b2003-04-21 18:48:45 +0000108 char *zName = 0; /* Name of the trigger */
drh3d5f74b2009-08-06 17:43:31 +0000109 sqlite3 *db = pParse->db; /* The database connection */
danielk1977ef2cb632004-05-29 02:37:19 +0000110 int iDb; /* The database to store the trigger in */
111 Token *pName; /* The unqualified db name */
drh3d5f74b2009-08-06 17:43:31 +0000112 DbFixer sFix; /* State vector for the DB fixer */
drhed6c8672003-01-12 18:02:16 +0000113
drh43617e92006-03-06 20:55:46 +0000114 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
115 assert( pName2!=0 );
drhaa78bec2008-12-09 03:55:14 +0000116 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
117 assert( op>0 && op<0xff );
danielk1977ef2cb632004-05-29 02:37:19 +0000118 if( isTemp ){
119 /* If TEMP was specified, then the trigger name may not be qualified. */
drh43617e92006-03-06 20:55:46 +0000120 if( pName2->n>0 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000121 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
122 goto trigger_cleanup;
123 }
124 iDb = 1;
125 pName = pName1;
126 }else{
mistachkind5578432012-08-25 10:01:29 +0000127 /* Figure out the db that the trigger will be created in */
danielk1977ef2cb632004-05-29 02:37:19 +0000128 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
129 if( iDb<0 ){
130 goto trigger_cleanup;
131 }
132 }
drh6fea83e2011-07-01 13:50:44 +0000133 if( !pTableName || db->mallocFailed ){
134 goto trigger_cleanup;
135 }
136
137 /* A long-standing parser bug is that this syntax was allowed:
138 **
139 ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
140 ** ^^^^^^^^
141 **
142 ** To maintain backwards compatibility, ignore the database
drh346a70c2020-06-15 20:27:35 +0000143 ** name on pTableName if we are reparsing out of the schema table
drh6fea83e2011-07-01 13:50:44 +0000144 */
145 if( db->init.busy && iDb!=1 ){
146 sqlite3DbFree(db, pTableName->a[0].zDatabase);
147 pTableName->a[0].zDatabase = 0;
148 }
danielk1977ef2cb632004-05-29 02:37:19 +0000149
150 /* If the trigger name was unqualified, and the table is a temp table,
151 ** then set iDb to 1 to create the trigger in the temporary database.
152 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
153 ** exist, the error is caught by the block below.
drh9adf9ac2002-05-15 11:44:13 +0000154 */
danielk1977ef2cb632004-05-29 02:37:19 +0000155 pTab = sqlite3SrcListLookup(pParse, pTableName);
drh622d2882010-02-15 16:54:55 +0000156 if( db->init.busy==0 && pName2->n==0 && pTab
157 && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977ef2cb632004-05-29 02:37:19 +0000158 iDb = 1;
159 }
160
161 /* Ensure the table name matches database name and that the table exists */
drh17435752007-08-16 04:30:38 +0000162 if( db->mallocFailed ) goto trigger_cleanup;
drhd24cc422003-03-27 12:51:24 +0000163 assert( pTableName->nSrc==1 );
drhd100f692013-10-03 15:39:44 +0000164 sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
165 if( sqlite3FixSrcList(&sFix, pTableName) ){
drh4312db52003-06-03 01:47:11 +0000166 goto trigger_cleanup;
drhf26e09c2003-05-31 16:21:12 +0000167 }
danielk1977ef2cb632004-05-29 02:37:19 +0000168 pTab = sqlite3SrcListLookup(pParse, pTableName);
169 if( !pTab ){
170 /* The table does not exist. */
drh4e451aa2020-11-05 19:13:44 +0000171 goto trigger_orphan_error;
drhd24cc422003-03-27 12:51:24 +0000172 }
drh4cbdda92006-06-14 19:00:20 +0000173 if( IsVirtual(pTab) ){
174 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
drh4e451aa2020-11-05 19:13:44 +0000175 goto trigger_orphan_error;
drh4cbdda92006-06-14 19:00:20 +0000176 }
drhd24cc422003-03-27 12:51:24 +0000177
danielk1977d8123362004-06-12 09:25:12 +0000178 /* Check that the trigger name is not reserved and that no trigger of the
179 ** specified name exists */
drh17435752007-08-16 04:30:38 +0000180 zName = sqlite3NameFromToken(db, pName);
drhc5a93d42019-08-12 00:08:07 +0000181 if( zName==0 ){
182 assert( db->mallocFailed );
183 goto trigger_cleanup;
184 }
185 if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
danielk1977d8123362004-06-12 09:25:12 +0000186 goto trigger_cleanup;
187 }
drh21206082011-04-04 18:22:02 +0000188 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danc9461ec2018-08-29 21:00:16 +0000189 if( !IN_RENAME_OBJECT ){
dan5496d6a2018-08-13 17:14:26 +0000190 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
191 if( !noErr ){
192 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
193 }else{
194 assert( !db->init.busy );
195 sqlite3CodeVerifySchema(pParse, iDb);
196 }
197 goto trigger_cleanup;
drhfdd48a72006-09-11 23:45:48 +0000198 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000199 }
danielk1977ef2cb632004-05-29 02:37:19 +0000200
201 /* Do not create a trigger on a system table */
drhdca76842004-12-07 14:06:13 +0000202 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk19774adee202004-05-08 08:23:19 +0000203 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
drhd24cc422003-03-27 12:51:24 +0000204 goto trigger_cleanup;
danielk1977d702fcc2002-05-26 23:24:40 +0000205 }
danielk1977ef2cb632004-05-29 02:37:19 +0000206
207 /* INSTEAD of triggers are only for views and views only support INSTEAD
208 ** of triggers.
209 */
210 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000211 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
drha9799932021-03-19 13:00:28 +0000212 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName->a);
drh4e451aa2020-11-05 19:13:44 +0000213 goto trigger_orphan_error;
drhd24cc422003-03-27 12:51:24 +0000214 }
danielk1977ef2cb632004-05-29 02:37:19 +0000215 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
danielk19774adee202004-05-08 08:23:19 +0000216 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
drha9799932021-03-19 13:00:28 +0000217 " trigger on table: %S", pTableName->a);
drh4e451aa2020-11-05 19:13:44 +0000218 goto trigger_orphan_error;
drhd24cc422003-03-27 12:51:24 +0000219 }
danielk1977ef2cb632004-05-29 02:37:19 +0000220
drhd24cc422003-03-27 12:51:24 +0000221#ifndef SQLITE_OMIT_AUTHORIZATION
danc9461ec2018-08-29 21:00:16 +0000222 if( !IN_RENAME_OBJECT ){
drha0daa752016-09-16 11:53:10 +0000223 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drhd24cc422003-03-27 12:51:24 +0000224 int code = SQLITE_CREATE_TRIGGER;
drh69c33822016-08-18 14:33:11 +0000225 const char *zDb = db->aDb[iTabDb].zDbSName;
226 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
danielk1977da184232006-01-05 11:34:32 +0000227 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
danielk1977ef2cb632004-05-29 02:37:19 +0000228 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
drhd24cc422003-03-27 12:51:24 +0000229 goto trigger_cleanup;
230 }
danielk1977da184232006-01-05 11:34:32 +0000231 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
drhd24cc422003-03-27 12:51:24 +0000232 goto trigger_cleanup;
233 }
234 }
235#endif
danielk1977d702fcc2002-05-26 23:24:40 +0000236
danielk1977ef2cb632004-05-29 02:37:19 +0000237 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
drh5cf590c2003-04-24 01:45:04 +0000238 ** cannot appear on views. So we might as well translate every
239 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
240 ** elsewhere.
241 */
danielk1977d702fcc2002-05-26 23:24:40 +0000242 if (tr_tm == TK_INSTEAD){
243 tr_tm = TK_BEFORE;
danielk1977c3f9bad2002-05-15 08:30:12 +0000244 }
245
246 /* Build the Trigger object */
drh17435752007-08-16 04:30:38 +0000247 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
danielk1977ef2cb632004-05-29 02:37:19 +0000248 if( pTrigger==0 ) goto trigger_cleanup;
dan165921a2009-08-28 18:53:45 +0000249 pTrigger->zName = zName;
drhe5f9c642003-01-13 23:27:31 +0000250 zName = 0;
drh17435752007-08-16 04:30:38 +0000251 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
danielk1977da184232006-01-05 11:34:32 +0000252 pTrigger->pSchema = db->aDb[iDb].pSchema;
danielk1977aaf22682006-01-06 15:03:48 +0000253 pTrigger->pTabSchema = pTab->pSchema;
drhaa78bec2008-12-09 03:55:14 +0000254 pTrigger->op = (u8)op;
drhdca76842004-12-07 14:06:13 +0000255 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
danc9461ec2018-08-29 21:00:16 +0000256 if( IN_RENAME_OBJECT ){
257 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
dan5496d6a2018-08-13 17:14:26 +0000258 pTrigger->pWhen = pWhen;
259 pWhen = 0;
260 }else{
261 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
262 }
263 pTrigger->pColumns = pColumns;
264 pColumns = 0;
drhf0f258b2003-04-21 18:48:45 +0000265 assert( pParse->pNewTrigger==0 );
danielk1977ef2cb632004-05-29 02:37:19 +0000266 pParse->pNewTrigger = pTrigger;
drhf0f258b2003-04-21 18:48:45 +0000267
268trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000269 sqlite3DbFree(db, zName);
270 sqlite3SrcListDelete(db, pTableName);
271 sqlite3IdListDelete(db, pColumns);
272 sqlite3ExprDelete(db, pWhen);
danielk1977d5d56522005-03-16 12:15:20 +0000273 if( !pParse->pNewTrigger ){
drh633e6d52008-07-28 19:34:53 +0000274 sqlite3DeleteTrigger(db, pTrigger);
danielk1977d5d56522005-03-16 12:15:20 +0000275 }else{
276 assert( pParse->pNewTrigger==pTrigger );
277 }
drh4e451aa2020-11-05 19:13:44 +0000278 return;
279
280trigger_orphan_error:
281 if( db->init.iDb==1 ){
282 /* Ticket #3810.
283 ** Normally, whenever a table is dropped, all associated triggers are
284 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
285 ** and the table is dropped by a different database connection, the
286 ** trigger is not visible to the database connection that does the
287 ** drop so the trigger cannot be dropped. This results in an
288 ** "orphaned trigger" - a trigger whose associated table is missing.
289 **
290 ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df
291 */
292 db->init.orphanTrigger = 1;
293 }
294 goto trigger_cleanup;
drhf0f258b2003-04-21 18:48:45 +0000295}
296
297/*
298** This routine is called after all of the trigger actions have been parsed
299** in order to complete the process of building the trigger.
300*/
danielk19774adee202004-05-08 08:23:19 +0000301void sqlite3FinishTrigger(
drhf0f258b2003-04-21 18:48:45 +0000302 Parse *pParse, /* Parser context */
303 TriggerStep *pStepList, /* The triggered program */
304 Token *pAll /* Token that describes the complete CREATE TRIGGER */
305){
drha8c62df2010-02-15 15:47:18 +0000306 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
307 char *zName; /* Name of trigger */
308 sqlite3 *db = pParse->db; /* The database */
309 DbFixer sFix; /* Fixer object */
310 int iDb; /* Database containing the trigger */
311 Token nameToken; /* Trigger name for error reporting */
drhf0f258b2003-04-21 18:48:45 +0000312
drhf0f258b2003-04-21 18:48:45 +0000313 pParse->pNewTrigger = 0;
drh9ab4c2e2009-05-09 00:18:38 +0000314 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
dan165921a2009-08-28 18:53:45 +0000315 zName = pTrig->zName;
danielk1977da184232006-01-05 11:34:32 +0000316 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
danielk1977bfb9e352005-01-24 13:03:32 +0000317 pTrig->step_list = pStepList;
drha69d9162003-04-17 22:57:53 +0000318 while( pStepList ){
danielk1977bfb9e352005-01-24 13:03:32 +0000319 pStepList->pTrig = pTrig;
drha69d9162003-04-17 22:57:53 +0000320 pStepList = pStepList->pNext;
321 }
drh40aced52016-01-22 17:48:09 +0000322 sqlite3TokenInit(&nameToken, pTrig->zName);
drhd100f692013-10-03 15:39:44 +0000323 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
324 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
dan46539d72013-10-03 12:29:38 +0000325 || sqlite3FixExpr(&sFix, pTrig->pWhen)
drhd100f692013-10-03 15:39:44 +0000326 ){
drhf26e09c2003-05-31 16:21:12 +0000327 goto triggerfinish_cleanup;
328 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000329
dan5496d6a2018-08-13 17:14:26 +0000330#ifndef SQLITE_OMIT_ALTERTABLE
danc9461ec2018-08-29 21:00:16 +0000331 if( IN_RENAME_OBJECT ){
dan5496d6a2018-08-13 17:14:26 +0000332 assert( !db->init.busy );
333 pParse->pNewTrigger = pTrig;
334 pTrig = 0;
335 }else
336#endif
337
drha8c62df2010-02-15 15:47:18 +0000338 /* if we are not initializing,
drh1e32bed2020-06-19 13:33:53 +0000339 ** build the sqlite_schema entry
drh9adf9ac2002-05-15 11:44:13 +0000340 */
drh1d85d932004-02-14 23:05:52 +0000341 if( !db->init.busy ){
drhc977f7f2002-05-21 11:38:11 +0000342 Vdbe *v;
drh2d401ab2008-01-10 23:50:11 +0000343 char *z;
danielk1977c3f9bad2002-05-15 08:30:12 +0000344
drh1e32bed2020-06-19 13:33:53 +0000345 /* Make an entry in the sqlite_schema table */
danielk19774adee202004-05-08 08:23:19 +0000346 v = sqlite3GetVdbe(pParse);
drhf0f258b2003-04-21 18:48:45 +0000347 if( v==0 ) goto triggerfinish_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000348 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh2d401ab2008-01-10 23:50:11 +0000349 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
drhd96cc6f2017-06-08 14:35:21 +0000350 testcase( z==0 );
drh2d401ab2008-01-10 23:50:11 +0000351 sqlite3NestedParse(pParse,
drh346a70c2020-06-15 20:27:35 +0000352 "INSERT INTO %Q." DFLT_SCHEMA_TABLE
353 " VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
354 db->aDb[iDb].zDbSName, zName,
drh2d401ab2008-01-10 23:50:11 +0000355 pTrig->table, z);
drh633e6d52008-07-28 19:34:53 +0000356 sqlite3DbFree(db, z);
drh9cbf3422008-01-17 16:22:13 +0000357 sqlite3ChangeCookie(pParse, iDb);
drh5d9c9da2011-06-03 20:11:17 +0000358 sqlite3VdbeAddParseSchemaOp(v, iDb,
dan6a5a13d2021-02-17 20:08:22 +0000359 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName), 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000360 }
361
drh956bc922004-07-24 17:38:29 +0000362 if( db->init.busy ){
danielk19772f886d12009-02-28 10:47:41 +0000363 Trigger *pLink = pTrig;
364 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
drh21206082011-04-04 18:22:02 +0000365 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh55f66b32019-07-16 19:44:32 +0000366 assert( pLink!=0 );
drhacbcb7e2014-08-21 20:26:37 +0000367 pTrig = sqlite3HashInsert(pHash, zName, pTrig);
danielk19772f886d12009-02-28 10:47:41 +0000368 if( pTrig ){
drh4a642b62016-02-05 01:55:27 +0000369 sqlite3OomFault(db);
danielk19772f886d12009-02-28 10:47:41 +0000370 }else if( pLink->pSchema==pLink->pTabSchema ){
371 Table *pTab;
drhacbcb7e2014-08-21 20:26:37 +0000372 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
danielk19772f886d12009-02-28 10:47:41 +0000373 assert( pTab!=0 );
374 pLink->pNext = pTab->pTrigger;
375 pTab->pTrigger = pLink;
danielk1977d5d56522005-03-16 12:15:20 +0000376 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000377 }
378
drhf0f258b2003-04-21 18:48:45 +0000379triggerfinish_cleanup:
drh633e6d52008-07-28 19:34:53 +0000380 sqlite3DeleteTrigger(db, pTrig);
danc9461ec2018-08-29 21:00:16 +0000381 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
drh633e6d52008-07-28 19:34:53 +0000382 sqlite3DeleteTriggerStep(db, pStepList);
drh4b59ab52002-08-24 18:24:51 +0000383}
danielk1977c3f9bad2002-05-15 08:30:12 +0000384
drh4b59ab52002-08-24 18:24:51 +0000385/*
drhf259df52017-12-27 20:38:35 +0000386** Duplicate a range of text from an SQL statement, then convert all
387** whitespace characters into ordinary space characters.
388*/
389static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
390 char *z = sqlite3DbSpanDup(db, zStart, zEnd);
391 int i;
392 if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i]) ) z[i] = ' ';
393 return z;
394}
395
396/*
drhc977f7f2002-05-21 11:38:11 +0000397** Turn a SELECT statement (that the pSelect parameter points to) into
398** a trigger step. Return a pointer to a TriggerStep structure.
399**
400** The parser calls this routine when it finds a SELECT statement in
401** body of a TRIGGER.
402*/
drhf259df52017-12-27 20:38:35 +0000403TriggerStep *sqlite3TriggerSelectStep(
404 sqlite3 *db, /* Database connection */
405 Select *pSelect, /* The SELECT statement */
406 const char *zStart, /* Start of SQL text */
407 const char *zEnd /* End of SQL text */
408){
drh17435752007-08-16 04:30:38 +0000409 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
danielk19772e588c72005-12-09 14:25:08 +0000410 if( pTriggerStep==0 ) {
drh633e6d52008-07-28 19:34:53 +0000411 sqlite3SelectDelete(db, pSelect);
danielk19772e588c72005-12-09 14:25:08 +0000412 return 0;
413 }
danielk1977633ed082002-05-17 00:05:58 +0000414 pTriggerStep->op = TK_SELECT;
415 pTriggerStep->pSelect = pSelect;
416 pTriggerStep->orconf = OE_Default;
drhf259df52017-12-27 20:38:35 +0000417 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
drhb7916a72009-05-27 10:31:29 +0000418 return pTriggerStep;
419}
danielk1977c3f9bad2002-05-15 08:30:12 +0000420
drhb7916a72009-05-27 10:31:29 +0000421/*
422** Allocate space to hold a new trigger step. The allocated space
423** holds both the TriggerStep object and the TriggerStep.target.z string.
424**
425** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
426*/
427static TriggerStep *triggerStepAllocate(
danc9461ec2018-08-29 21:00:16 +0000428 Parse *pParse, /* Parser context */
shane5eff7cf2009-08-10 03:57:58 +0000429 u8 op, /* Trigger opcode */
drhf259df52017-12-27 20:38:35 +0000430 Token *pName, /* The target name */
431 const char *zStart, /* Start of SQL text */
432 const char *zEnd /* End of SQL text */
drhb7916a72009-05-27 10:31:29 +0000433){
danc9461ec2018-08-29 21:00:16 +0000434 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000435 TriggerStep *pTriggerStep;
436
dan46408352015-04-21 16:38:49 +0000437 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
drhb7916a72009-05-27 10:31:29 +0000438 if( pTriggerStep ){
439 char *z = (char*)&pTriggerStep[1];
440 memcpy(z, pName->z, pName->n);
dan46408352015-04-21 16:38:49 +0000441 sqlite3Dequote(z);
442 pTriggerStep->zTarget = z;
drhb7916a72009-05-27 10:31:29 +0000443 pTriggerStep->op = op;
drhf259df52017-12-27 20:38:35 +0000444 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
danc9461ec2018-08-29 21:00:16 +0000445 if( IN_RENAME_OBJECT ){
446 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
447 }
drhb7916a72009-05-27 10:31:29 +0000448 }
danielk1977633ed082002-05-17 00:05:58 +0000449 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000450}
451
drhc977f7f2002-05-21 11:38:11 +0000452/*
453** Build a trigger step out of an INSERT statement. Return a pointer
454** to the new trigger step.
455**
456** The parser calls this routine when it sees an INSERT inside the
457** body of a trigger.
458*/
danielk19774adee202004-05-08 08:23:19 +0000459TriggerStep *sqlite3TriggerInsertStep(
dan5be60c52018-08-15 20:28:39 +0000460 Parse *pParse, /* Parser */
drhc977f7f2002-05-21 11:38:11 +0000461 Token *pTableName, /* Name of the table into which we insert */
462 IdList *pColumn, /* List of columns in pTableName to insert into */
drhc977f7f2002-05-21 11:38:11 +0000463 Select *pSelect, /* A SELECT statement that supplies values */
drhf259df52017-12-27 20:38:35 +0000464 u8 orconf, /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
drh46d2e5c2018-04-12 13:15:43 +0000465 Upsert *pUpsert, /* ON CONFLICT clauses for upsert */
drhf259df52017-12-27 20:38:35 +0000466 const char *zStart, /* Start of SQL text */
467 const char *zEnd /* End of SQL text */
danielk1977633ed082002-05-17 00:05:58 +0000468){
dan5be60c52018-08-15 20:28:39 +0000469 sqlite3 *db = pParse->db;
drhf3a65f72007-08-22 20:18:21 +0000470 TriggerStep *pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000471
drh75593d92014-01-10 20:46:55 +0000472 assert(pSelect != 0 || db->mallocFailed);
danielk1977c3f9bad2002-05-15 08:30:12 +0000473
danc9461ec2018-08-29 21:00:16 +0000474 pTriggerStep = triggerStepAllocate(pParse, TK_INSERT, pTableName,zStart,zEnd);
danielk1977d5d56522005-03-16 12:15:20 +0000475 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000476 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000477 pTriggerStep->pSelect = pSelect;
478 pSelect = 0;
479 }else{
480 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
481 }
danielk1977d5d56522005-03-16 12:15:20 +0000482 pTriggerStep->pIdList = pColumn;
drh46d2e5c2018-04-12 13:15:43 +0000483 pTriggerStep->pUpsert = pUpsert;
danielk1977d5d56522005-03-16 12:15:20 +0000484 pTriggerStep->orconf = orconf;
dan9105fd52019-08-19 17:26:32 +0000485 if( pUpsert ){
486 sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
487 }
danielk1977d5d56522005-03-16 12:15:20 +0000488 }else{
drh99160482018-04-18 01:34:39 +0000489 testcase( pColumn );
drh633e6d52008-07-28 19:34:53 +0000490 sqlite3IdListDelete(db, pColumn);
drh99160482018-04-18 01:34:39 +0000491 testcase( pUpsert );
drh46d2e5c2018-04-12 13:15:43 +0000492 sqlite3UpsertDelete(db, pUpsert);
danielk1977d5d56522005-03-16 12:15:20 +0000493 }
drh33e619f2009-05-28 01:00:55 +0000494 sqlite3SelectDelete(db, pSelect);
danielk1977c3f9bad2002-05-15 08:30:12 +0000495
danielk1977633ed082002-05-17 00:05:58 +0000496 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000497}
498
drhc977f7f2002-05-21 11:38:11 +0000499/*
500** Construct a trigger step that implements an UPDATE statement and return
501** a pointer to that trigger step. The parser calls this routine when it
502** sees an UPDATE statement inside the body of a CREATE TRIGGER.
503*/
danielk19774adee202004-05-08 08:23:19 +0000504TriggerStep *sqlite3TriggerUpdateStep(
dan5be60c52018-08-15 20:28:39 +0000505 Parse *pParse, /* Parser */
drhc977f7f2002-05-21 11:38:11 +0000506 Token *pTableName, /* Name of the table to be updated */
dane7877b22020-07-14 19:51:01 +0000507 SrcList *pFrom,
drhc977f7f2002-05-21 11:38:11 +0000508 ExprList *pEList, /* The SET clause: list of column and new values */
509 Expr *pWhere, /* The WHERE clause */
drhf259df52017-12-27 20:38:35 +0000510 u8 orconf, /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
511 const char *zStart, /* Start of SQL text */
512 const char *zEnd /* End of SQL text */
drhc977f7f2002-05-21 11:38:11 +0000513){
dan5be60c52018-08-15 20:28:39 +0000514 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000515 TriggerStep *pTriggerStep;
516
danc9461ec2018-08-29 21:00:16 +0000517 pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE, pTableName,zStart,zEnd);
drh33e619f2009-05-28 01:00:55 +0000518 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000519 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000520 pTriggerStep->pExprList = pEList;
521 pTriggerStep->pWhere = pWhere;
dane7877b22020-07-14 19:51:01 +0000522 pTriggerStep->pFrom = pFrom;
dan5be60c52018-08-15 20:28:39 +0000523 pEList = 0;
524 pWhere = 0;
dane7877b22020-07-14 19:51:01 +0000525 pFrom = 0;
dan5be60c52018-08-15 20:28:39 +0000526 }else{
527 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
528 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
dane7877b22020-07-14 19:51:01 +0000529 pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE);
dan5be60c52018-08-15 20:28:39 +0000530 }
drh33e619f2009-05-28 01:00:55 +0000531 pTriggerStep->orconf = orconf;
drh0e3a6f32007-03-30 20:40:34 +0000532 }
drh33e619f2009-05-28 01:00:55 +0000533 sqlite3ExprListDelete(db, pEList);
534 sqlite3ExprDelete(db, pWhere);
dane7877b22020-07-14 19:51:01 +0000535 sqlite3SrcListDelete(db, pFrom);
danielk1977633ed082002-05-17 00:05:58 +0000536 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000537}
538
drhc977f7f2002-05-21 11:38:11 +0000539/*
540** Construct a trigger step that implements a DELETE statement and return
541** a pointer to that trigger step. The parser calls this routine when it
542** sees a DELETE statement inside the body of a CREATE TRIGGER.
543*/
drh17435752007-08-16 04:30:38 +0000544TriggerStep *sqlite3TriggerDeleteStep(
dan5be60c52018-08-15 20:28:39 +0000545 Parse *pParse, /* Parser */
drh17435752007-08-16 04:30:38 +0000546 Token *pTableName, /* The table from which rows are deleted */
drhf259df52017-12-27 20:38:35 +0000547 Expr *pWhere, /* The WHERE clause */
548 const char *zStart, /* Start of SQL text */
549 const char *zEnd /* End of SQL text */
drh17435752007-08-16 04:30:38 +0000550){
dan5be60c52018-08-15 20:28:39 +0000551 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000552 TriggerStep *pTriggerStep;
553
danc9461ec2018-08-29 21:00:16 +0000554 pTriggerStep = triggerStepAllocate(pParse, TK_DELETE, pTableName,zStart,zEnd);
drh33e619f2009-05-28 01:00:55 +0000555 if( pTriggerStep ){
danc9461ec2018-08-29 21:00:16 +0000556 if( IN_RENAME_OBJECT ){
dan5be60c52018-08-15 20:28:39 +0000557 pTriggerStep->pWhere = pWhere;
558 pWhere = 0;
559 }else{
560 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
561 }
drh33e619f2009-05-28 01:00:55 +0000562 pTriggerStep->orconf = OE_Default;
drhb63a53d2007-03-31 01:34:44 +0000563 }
drh33e619f2009-05-28 01:00:55 +0000564 sqlite3ExprDelete(db, pWhere);
danielk1977633ed082002-05-17 00:05:58 +0000565 return pTriggerStep;
danielk1977c3f9bad2002-05-15 08:30:12 +0000566}
567
danielk1977633ed082002-05-17 00:05:58 +0000568/*
569** Recursively delete a Trigger structure
570*/
drh633e6d52008-07-28 19:34:53 +0000571void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
drh658f0a32021-01-30 02:43:26 +0000572 if( pTrigger==0 || pTrigger->bReturning ) return;
drh633e6d52008-07-28 19:34:53 +0000573 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
dan165921a2009-08-28 18:53:45 +0000574 sqlite3DbFree(db, pTrigger->zName);
drh633e6d52008-07-28 19:34:53 +0000575 sqlite3DbFree(db, pTrigger->table);
576 sqlite3ExprDelete(db, pTrigger->pWhen);
577 sqlite3IdListDelete(db, pTrigger->pColumns);
drh633e6d52008-07-28 19:34:53 +0000578 sqlite3DbFree(db, pTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000579}
580
581/*
danielk19778a414492004-06-29 08:59:35 +0000582** This function is called to drop a trigger from the database schema.
583**
584** This may be called directly from the parser and therefore identifies
585** the trigger by name. The sqlite3DropTriggerPtr() routine does the
586** same job as this routine except it takes a pointer to the trigger
587** instead of the trigger name.
588**/
drhfdd48a72006-09-11 23:45:48 +0000589void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
danielk1977742f9472004-06-16 12:02:43 +0000590 Trigger *pTrigger = 0;
drhd24cc422003-03-27 12:51:24 +0000591 int i;
592 const char *zDb;
593 const char *zName;
drh9bb575f2004-09-06 17:24:11 +0000594 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000595
drh17435752007-08-16 04:30:38 +0000596 if( db->mallocFailed ) goto drop_trigger_cleanup;
danielk19778a414492004-06-29 08:59:35 +0000597 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977c0391392004-06-09 12:30:04 +0000598 goto drop_trigger_cleanup;
599 }
danielk19778e227872004-06-07 07:52:17 +0000600
drhd24cc422003-03-27 12:51:24 +0000601 assert( pName->nSrc==1 );
602 zDb = pName->a[0].zDatabase;
603 zName = pName->a[0].zName;
drh21206082011-04-04 18:22:02 +0000604 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
danielk197753c0f742005-03-29 03:10:59 +0000605 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000606 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
dan465c2b82020-03-21 15:10:40 +0000607 if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
drh21206082011-04-04 18:22:02 +0000608 assert( sqlite3SchemaMutexHeld(db, j, 0) );
drhacbcb7e2014-08-21 20:26:37 +0000609 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
drhd24cc422003-03-27 12:51:24 +0000610 if( pTrigger ) break;
danielk1977c3f9bad2002-05-15 08:30:12 +0000611 }
drhd24cc422003-03-27 12:51:24 +0000612 if( !pTrigger ){
drhfdd48a72006-09-11 23:45:48 +0000613 if( !noErr ){
drha9799932021-03-19 13:00:28 +0000614 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName->a);
dan57966752011-04-09 17:32:58 +0000615 }else{
616 sqlite3CodeVerifyNamedSchema(pParse, zDb);
drhfdd48a72006-09-11 23:45:48 +0000617 }
dan1db95102010-06-28 10:15:19 +0000618 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +0000619 goto drop_trigger_cleanup;
620 }
drh74161702006-02-24 02:53:49 +0000621 sqlite3DropTriggerPtr(pParse, pTrigger);
drh79a519c2003-05-17 19:04:03 +0000622
623drop_trigger_cleanup:
drh633e6d52008-07-28 19:34:53 +0000624 sqlite3SrcListDelete(db, pName);
drh79a519c2003-05-17 19:04:03 +0000625}
626
627/*
drh956bc922004-07-24 17:38:29 +0000628** Return a pointer to the Table structure for the table that a trigger
629** is set on.
630*/
drh74161702006-02-24 02:53:49 +0000631static Table *tableOfTrigger(Trigger *pTrigger){
drhacbcb7e2014-08-21 20:26:37 +0000632 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
drh956bc922004-07-24 17:38:29 +0000633}
634
635
636/*
drh74161702006-02-24 02:53:49 +0000637** Drop a trigger given a pointer to that trigger.
drh79a519c2003-05-17 19:04:03 +0000638*/
drh74161702006-02-24 02:53:49 +0000639void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
drh79a519c2003-05-17 19:04:03 +0000640 Table *pTable;
641 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +0000642 sqlite3 *db = pParse->db;
drh956bc922004-07-24 17:38:29 +0000643 int iDb;
drh79a519c2003-05-17 19:04:03 +0000644
danielk1977da184232006-01-05 11:34:32 +0000645 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
drh956bc922004-07-24 17:38:29 +0000646 assert( iDb>=0 && iDb<db->nDb );
drh74161702006-02-24 02:53:49 +0000647 pTable = tableOfTrigger(pTrigger);
drh6397a782019-08-27 10:05:45 +0000648 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
drhe5f9c642003-01-13 23:27:31 +0000649#ifndef SQLITE_OMIT_AUTHORIZATION
drh6397a782019-08-27 10:05:45 +0000650 if( pTable ){
drhe5f9c642003-01-13 23:27:31 +0000651 int code = SQLITE_DROP_TRIGGER;
drh69c33822016-08-18 14:33:11 +0000652 const char *zDb = db->aDb[iDb].zDbSName;
drh956bc922004-07-24 17:38:29 +0000653 const char *zTab = SCHEMA_TABLE(iDb);
654 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
dan2bd93512009-08-31 08:22:46 +0000655 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
danielk19774adee202004-05-08 08:23:19 +0000656 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drh79a519c2003-05-17 19:04:03 +0000657 return;
drhe5f9c642003-01-13 23:27:31 +0000658 }
drhed6c8672003-01-12 18:02:16 +0000659 }
drhe5f9c642003-01-13 23:27:31 +0000660#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000661
drhf0f258b2003-04-21 18:48:45 +0000662 /* Generate code to destroy the database record of the trigger.
663 */
drh43617e92006-03-06 20:55:46 +0000664 if( (v = sqlite3GetVdbe(pParse))!=0 ){
drh8c8dddc2015-12-09 16:26:38 +0000665 sqlite3NestedParse(pParse,
drh346a70c2020-06-15 20:27:35 +0000666 "DELETE FROM %Q." DFLT_SCHEMA_TABLE " WHERE name=%Q AND type='trigger'",
667 db->aDb[iDb].zDbSName, pTrigger->zName
drh8c8dddc2015-12-09 16:26:38 +0000668 );
drh9cbf3422008-01-17 16:22:13 +0000669 sqlite3ChangeCookie(pParse, iDb);
dan165921a2009-08-28 18:53:45 +0000670 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
drhf0f258b2003-04-21 18:48:45 +0000671 }
drh956bc922004-07-24 17:38:29 +0000672}
drhf0f258b2003-04-21 18:48:45 +0000673
drh956bc922004-07-24 17:38:29 +0000674/*
675** Remove a trigger from the hash tables of the sqlite* pointer.
676*/
677void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
678 Trigger *pTrigger;
drh21206082011-04-04 18:22:02 +0000679 Hash *pHash;
680
681 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
682 pHash = &(db->aDb[iDb].pSchema->trigHash);
drhacbcb7e2014-08-21 20:26:37 +0000683 pTrigger = sqlite3HashInsert(pHash, zName, 0);
drh9ab4c2e2009-05-09 00:18:38 +0000684 if( ALWAYS(pTrigger) ){
danielk19772f886d12009-02-28 10:47:41 +0000685 if( pTrigger->pSchema==pTrigger->pTabSchema ){
686 Table *pTab = tableOfTrigger(pTrigger);
drh6397a782019-08-27 10:05:45 +0000687 if( pTab ){
688 Trigger **pp;
dan0232dad2019-12-03 03:34:06 +0000689 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
690 if( *pp==pTrigger ){
691 *pp = (*pp)->pNext;
692 break;
693 }
694 }
drh6397a782019-08-27 10:05:45 +0000695 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000696 }
drh633e6d52008-07-28 19:34:53 +0000697 sqlite3DeleteTrigger(db, pTrigger);
drh8257aa82017-07-26 19:59:13 +0000698 db->mDbFlags |= DBFLAG_SchemaChange;
danielk1977c3f9bad2002-05-15 08:30:12 +0000699 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000700}
701
drhc977f7f2002-05-21 11:38:11 +0000702/*
703** pEList is the SET clause of an UPDATE statement. Each entry
704** in pEList is of the format <id>=<expr>. If any of the entries
705** in pEList have an <id> which matches an identifier in pIdList,
706** then return TRUE. If pIdList==NULL, then it is considered a
707** wildcard that matches anything. Likewise if pEList==NULL then
708** it matches anything so always return true. Return false only
709** if there is no match.
710*/
drh9ab4c2e2009-05-09 00:18:38 +0000711static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
drhad2d8302002-05-24 20:31:36 +0000712 int e;
drh9ab4c2e2009-05-09 00:18:38 +0000713 if( pIdList==0 || NEVER(pEList==0) ) return 1;
drhad2d8302002-05-24 20:31:36 +0000714 for(e=0; e<pEList->nExpr; e++){
drh41cee662019-12-12 20:22:34 +0000715 if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
danielk1977f29ce552002-05-19 23:43:12 +0000716 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000717 return 0;
718}
719
danielk1977c3f9bad2002-05-15 08:30:12 +0000720/*
danielk19772f886d12009-02-28 10:47:41 +0000721** Return a list of all triggers on table pTab if there exists at least
722** one trigger that must be fired when an operation of type 'op' is
723** performed on the table, and, if that operation is an UPDATE, if at
724** least one of the columns in pChanges is being modified.
drhdca76842004-12-07 14:06:13 +0000725*/
danielk19772f886d12009-02-28 10:47:41 +0000726Trigger *sqlite3TriggersExist(
727 Parse *pParse, /* Parse context */
drhdca76842004-12-07 14:06:13 +0000728 Table *pTab, /* The table the contains the triggers */
danielk1977633ed082002-05-17 00:05:58 +0000729 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
danielk19772f886d12009-02-28 10:47:41 +0000730 ExprList *pChanges, /* Columns that change in an UPDATE statement */
731 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
drhc977f7f2002-05-21 11:38:11 +0000732){
drhdca76842004-12-07 14:06:13 +0000733 int mask = 0;
drhe83cafd2011-03-21 17:15:58 +0000734 Trigger *pList = 0;
danielk19772f886d12009-02-28 10:47:41 +0000735 Trigger *p;
drhe83cafd2011-03-21 17:15:58 +0000736
drh2aa41c82021-02-03 00:55:34 +0000737 pList = sqlite3TriggerList(pParse, pTab);
drhc9be8632021-02-02 00:16:15 +0000738 assert( pList==0 || IsVirtual(pTab)==0
739 || (pList->bReturning && pList->pNext==0) );
drh2aa41c82021-02-03 00:55:34 +0000740 if( pList!=0 ){
741 p = pList;
742 if( (pParse->db->flags & SQLITE_EnableTrigger)==0
743 && pTab->pTrigger!=0
744 ){
745 /* The SQLITE_DBCONFIG_ENABLE_TRIGGER setting is off. That means that
746 ** only TEMP triggers are allowed. Truncate the pList so that it
747 ** includes only TEMP triggers */
748 if( pList==pTab->pTrigger ){
749 pList = 0;
750 goto exit_triggers_exist;
drh709dd132021-02-02 12:01:22 +0000751 }
drh2aa41c82021-02-03 00:55:34 +0000752 while( ALWAYS(p->pNext) && p->pNext!=pTab->pTrigger ) p = p->pNext;
753 p->pNext = 0;
754 p = pList;
danielk1977c3f9bad2002-05-15 08:30:12 +0000755 }
drh2aa41c82021-02-03 00:55:34 +0000756 do{
757 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
758 mask |= p->tr_tm;
759 }else if( p->op==TK_RETURNING ){
760 /* The first time a RETURNING trigger is seen, the "op" value tells
761 ** us what time of trigger it should be. */
762 assert( sqlite3IsToplevel(pParse) );
763 p->op = op;
drh7dec8042021-02-04 22:59:19 +0000764 if( IsVirtual(pTab) ){
765 if( op!=TK_INSERT ){
766 sqlite3ErrorMsg(pParse,
767 "%s RETURNING is not available on virtual tables",
768 op==TK_DELETE ? "DELETE" : "UPDATE");
769 }
770 p->tr_tm = TRIGGER_BEFORE;
771 }else{
772 p->tr_tm = TRIGGER_AFTER;
drh2aa41c82021-02-03 00:55:34 +0000773 }
drh7dec8042021-02-04 22:59:19 +0000774 mask |= p->tr_tm;
drh2aa41c82021-02-03 00:55:34 +0000775 }else if( p->bReturning && p->op==TK_INSERT && op==TK_UPDATE
776 && sqlite3IsToplevel(pParse) ){
777 /* Also fire a RETURNING trigger for an UPSERT */
drh7dec8042021-02-04 22:59:19 +0000778 mask |= p->tr_tm;
drh2aa41c82021-02-03 00:55:34 +0000779 }
780 p = p->pNext;
781 }while( p );
danielk1977c3f9bad2002-05-15 08:30:12 +0000782 }
drh2aa41c82021-02-03 00:55:34 +0000783exit_triggers_exist:
danielk19772f886d12009-02-28 10:47:41 +0000784 if( pMask ){
785 *pMask = mask;
786 }
787 return (mask ? pList : 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000788}
789
drhc977f7f2002-05-21 11:38:11 +0000790/*
dan46408352015-04-21 16:38:49 +0000791** Convert the pStep->zTarget string into a SrcList and return a pointer
drhf26e09c2003-05-31 16:21:12 +0000792** to that SrcList.
793**
794** This routine adds a specific database name, if needed, to the target when
795** forming the SrcList. This prevents a trigger in one database from
796** referring to a target in another database. An exception is when the
797** trigger is in TEMP in which case it can refer to any other database it
798** wants.
799*/
dane7877b22020-07-14 19:51:01 +0000800SrcList *sqlite3TriggerStepSrc(
drhf26e09c2003-05-31 16:21:12 +0000801 Parse *pParse, /* The parsing context */
802 TriggerStep *pStep /* The trigger containing the target token */
803){
dan46408352015-04-21 16:38:49 +0000804 sqlite3 *db = pParse->db;
dane7877b22020-07-14 19:51:01 +0000805 SrcList *pSrc; /* SrcList to be returned */
806 char *zName = sqlite3DbStrDup(db, pStep->zTarget);
drh29c992c2019-01-17 15:40:41 +0000807 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
dane7877b22020-07-14 19:51:01 +0000808 assert( pSrc==0 || pSrc->nSrc==1 );
809 assert( zName || pSrc==0 );
drhb7916a72009-05-27 10:31:29 +0000810 if( pSrc ){
dane7877b22020-07-14 19:51:01 +0000811 Schema *pSchema = pStep->pTrig->pSchema;
812 pSrc->a[0].zName = zName;
813 if( pSchema!=db->aDb[1].pSchema ){
814 pSrc->a[0].pSchema = pSchema;
drhb7916a72009-05-27 10:31:29 +0000815 }
dane7877b22020-07-14 19:51:01 +0000816 if( pStep->pFrom ){
817 SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0);
818 pSrc = sqlite3SrcListAppendList(pParse, pSrc, pDup);
819 }
820 }else{
821 sqlite3DbFree(db, zName);
drhf26e09c2003-05-31 16:21:12 +0000822 }
823 return pSrc;
824}
825
drh0d23f672021-03-30 01:52:21 +0000826/*
827** Return true if the pExpr term from the RETURNING clause argument
828** list is of the form "*". Raise an error if the terms if of the
829** form "table.*".
830*/
831static int isAsteriskTerm(
832 Parse *pParse, /* Parsing context */
833 Expr *pTerm /* A term in the RETURNING clause */
834){
835 assert( pTerm!=0 );
836 if( pTerm->op==TK_ASTERISK ) return 1;
837 if( pTerm->op!=TK_DOT ) return 0;
838 assert( pTerm->pRight!=0 );
839 assert( pTerm->pLeft!=0 );
840 if( pTerm->pRight->op!=TK_ASTERISK ) return 0;
841 sqlite3ErrorMsg(pParse, "RETURNING may not use \"TABLE.*\" wildcards");
842 return 1;
843}
844
drhdac9a5f2021-01-29 21:18:46 +0000845/* The input list pList is the list of result set terms from a RETURNING
846** clause. The table that we are returning from is pTab.
847**
848** This routine makes a copy of the pList, and at the same time expands
849** any "*" wildcards to be the complete set of columns from pTab.
850*/
851static ExprList *sqlite3ExpandReturning(
852 Parse *pParse, /* Parsing context */
853 ExprList *pList, /* The arguments to RETURNING */
854 Table *pTab /* The table being updated */
855){
856 ExprList *pNew = 0;
857 sqlite3 *db = pParse->db;
858 int i;
drh552562c2021-02-04 20:52:20 +0000859
drhdac9a5f2021-01-29 21:18:46 +0000860 for(i=0; i<pList->nExpr; i++){
861 Expr *pOldExpr = pList->a[i].pExpr;
drh0d23f672021-03-30 01:52:21 +0000862 if( NEVER(pOldExpr==0) ) continue;
863 if( isAsteriskTerm(pParse, pOldExpr) ){
drhc9be8632021-02-02 00:16:15 +0000864 int jj;
865 for(jj=0; jj<pTab->nCol; jj++){
drh87296422021-02-07 12:59:43 +0000866 Expr *pNewExpr;
drhc9be8632021-02-02 00:16:15 +0000867 if( IsHiddenColumn(pTab->aCol+jj) ) continue;
drh87296422021-02-07 12:59:43 +0000868 pNewExpr = sqlite3Expr(db, TK_ID, pTab->aCol[jj].zName);
drhdac9a5f2021-01-29 21:18:46 +0000869 pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr);
870 if( !db->mallocFailed ){
871 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
drhc9be8632021-02-02 00:16:15 +0000872 pItem->zEName = sqlite3DbStrDup(db, pTab->aCol[jj].zName);
drhdac9a5f2021-01-29 21:18:46 +0000873 pItem->eEName = ENAME_NAME;
874 }
875 }
876 }else{
877 Expr *pNewExpr = sqlite3ExprDup(db, pOldExpr, 0);
878 pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr);
drh9407b6e2021-01-31 16:45:10 +0000879 if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0) ){
drhdac9a5f2021-01-29 21:18:46 +0000880 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
881 pItem->zEName = sqlite3DbStrDup(db, pList->a[i].zEName);
882 pItem->eEName = pList->a[i].eEName;
883 }
884 }
885 }
drh29f6a362021-02-05 17:34:47 +0000886 if( !db->mallocFailed ){
drh552562c2021-02-04 20:52:20 +0000887 Vdbe *v = pParse->pVdbe;
888 assert( v!=0 );
889 sqlite3VdbeSetNumCols(v, pNew->nExpr);
890 for(i=0; i<pNew->nExpr; i++){
891 sqlite3VdbeSetColName(v, i, COLNAME_NAME, pNew->a[i].zEName,
892 SQLITE_TRANSIENT);
893 }
894 }
drhdac9a5f2021-01-29 21:18:46 +0000895 return pNew;
896}
897
drhf26e09c2003-05-31 16:21:12 +0000898/*
drh381bdac2021-02-04 17:29:04 +0000899** Generate code for the RETURNING trigger. Unlike other triggers
900** that invoke a subprogram in the bytecode, the code for RETURNING
901** is generated in-line.
902*/
903static void codeReturningTrigger(
904 Parse *pParse, /* Parse context */
905 Trigger *pTrigger, /* The trigger step that defines the RETURNING */
906 Table *pTab, /* The table to code triggers from */
drh552562c2021-02-04 20:52:20 +0000907 int regIn /* The first in an array of registers */
drh381bdac2021-02-04 17:29:04 +0000908){
909 Vdbe *v = pParse->pVdbe;
910 ExprList *pNew;
911 Returning *pReturning;
912
913 assert( v!=0 );
914 assert( pParse->bReturning );
915 pReturning = pParse->u1.pReturning;
916 assert( pTrigger == &(pReturning->retTrig) );
drh381bdac2021-02-04 17:29:04 +0000917 pNew = sqlite3ExpandReturning(pParse, pReturning->pReturnEL, pTab);
918 if( pNew ){
drh552562c2021-02-04 20:52:20 +0000919 NameContext sNC;
920 memset(&sNC, 0, sizeof(sNC));
drhe1db9342021-02-04 21:17:12 +0000921 if( pReturning->nRetCol==0 ){
922 pReturning->nRetCol = pNew->nExpr;
923 pReturning->iRetCur = pParse->nTab++;
924 }
drh552562c2021-02-04 20:52:20 +0000925 sNC.pParse = pParse;
926 sNC.uNC.iBaseReg = regIn;
927 sNC.ncFlags = NC_UBaseReg;
928 pParse->eTriggerOp = pTrigger->op;
929 pParse->pTriggerTab = pTab;
930 if( sqlite3ResolveExprListNames(&sNC, pNew)==SQLITE_OK ){
931 int i;
932 int nCol = pNew->nExpr;
933 int reg = pParse->nMem+1;
934 pParse->nMem += nCol+2;
935 pReturning->iRetReg = reg;
936 for(i=0; i<nCol; i++){
937 sqlite3ExprCodeFactorable(pParse, pNew->a[i].pExpr, reg+i);
drh381bdac2021-02-04 17:29:04 +0000938 }
drh552562c2021-02-04 20:52:20 +0000939 sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, i, reg+i);
940 sqlite3VdbeAddOp2(v, OP_NewRowid, pReturning->iRetCur, reg+i+1);
941 sqlite3VdbeAddOp3(v, OP_Insert, pReturning->iRetCur, reg+i, reg+i+1);
942 }
943 sqlite3ExprListDelete(pParse->db, pNew);
944 pParse->eTriggerOp = 0;
945 pParse->pTriggerTab = 0;
946 }
drh381bdac2021-02-04 17:29:04 +0000947}
948
949
950
951/*
dan65a7cd12009-09-01 12:16:01 +0000952** Generate VDBE code for the statements inside the body of a single
953** trigger.
drhc977f7f2002-05-21 11:38:11 +0000954*/
danielk1977c3f9bad2002-05-15 08:30:12 +0000955static int codeTriggerProgram(
drhc977f7f2002-05-21 11:38:11 +0000956 Parse *pParse, /* The parser context */
957 TriggerStep *pStepList, /* List of statements inside the trigger body */
dan65a7cd12009-09-01 12:16:01 +0000958 int orconf /* Conflict algorithm. (OE_Abort, etc) */
danielk1977633ed082002-05-17 00:05:58 +0000959){
dan65a7cd12009-09-01 12:16:01 +0000960 TriggerStep *pStep;
drh344737f2004-09-19 00:50:20 +0000961 Vdbe *v = pParse->pVdbe;
drh17435752007-08-16 04:30:38 +0000962 sqlite3 *db = pParse->db;
danielk1977c3f9bad2002-05-15 08:30:12 +0000963
dan65a7cd12009-09-01 12:16:01 +0000964 assert( pParse->pTriggerTab && pParse->pToplevel );
965 assert( pStepList );
drh344737f2004-09-19 00:50:20 +0000966 assert( v!=0 );
dan65a7cd12009-09-01 12:16:01 +0000967 for(pStep=pStepList; pStep; pStep=pStep->pNext){
dan165921a2009-08-28 18:53:45 +0000968 /* Figure out the ON CONFLICT policy that will be used for this step
969 ** of the trigger program. If the statement that caused this trigger
970 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
971 ** the ON CONFLICT policy that was specified as part of the trigger
972 ** step statement. Example:
973 **
974 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
975 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
976 ** END;
977 **
978 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
979 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
980 */
shanecea72b22009-09-07 04:38:36 +0000981 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
drhaceb31b2014-02-08 01:40:27 +0000982 assert( pParse->okConstFactor==0 );
danf78baaf2012-12-06 19:37:22 +0000983
drhf259df52017-12-27 20:38:35 +0000984#ifndef SQLITE_OMIT_TRACE
drhe307e112017-12-27 21:30:34 +0000985 if( pStep->zSpan ){
986 sqlite3VdbeAddOp4(v, OP_Trace, 0x7fffffff, 1, 0,
987 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
988 P4_DYNAMIC);
989 }
drhf259df52017-12-27 20:38:35 +0000990#endif
991
dan165921a2009-08-28 18:53:45 +0000992 switch( pStep->op ){
danielk1977633ed082002-05-17 00:05:58 +0000993 case TK_UPDATE: {
dan165921a2009-08-28 18:53:45 +0000994 sqlite3Update(pParse,
dane7877b22020-07-14 19:51:01 +0000995 sqlite3TriggerStepSrc(pParse, pStep),
dan165921a2009-08-28 18:53:45 +0000996 sqlite3ExprListDup(db, pStep->pExprList, 0),
997 sqlite3ExprDup(db, pStep->pWhere, 0),
drheac9fab2018-04-16 13:00:50 +0000998 pParse->eOrconf, 0, 0, 0
dan165921a2009-08-28 18:53:45 +0000999 );
drhdac9a5f2021-01-29 21:18:46 +00001000 sqlite3VdbeAddOp0(v, OP_ResetCount);
danielk1977633ed082002-05-17 00:05:58 +00001001 break;
1002 }
1003 case TK_INSERT: {
dan165921a2009-08-28 18:53:45 +00001004 sqlite3Insert(pParse,
dane7877b22020-07-14 19:51:01 +00001005 sqlite3TriggerStepSrc(pParse, pStep),
dan165921a2009-08-28 18:53:45 +00001006 sqlite3SelectDup(db, pStep->pSelect, 0),
1007 sqlite3IdListDup(db, pStep->pIdList),
drh46d2e5c2018-04-12 13:15:43 +00001008 pParse->eOrconf,
1009 sqlite3UpsertDup(db, pStep->pUpsert)
dan165921a2009-08-28 18:53:45 +00001010 );
drhdac9a5f2021-01-29 21:18:46 +00001011 sqlite3VdbeAddOp0(v, OP_ResetCount);
danielk1977633ed082002-05-17 00:05:58 +00001012 break;
1013 }
1014 case TK_DELETE: {
dan165921a2009-08-28 18:53:45 +00001015 sqlite3DeleteFrom(pParse,
dane7877b22020-07-14 19:51:01 +00001016 sqlite3TriggerStepSrc(pParse, pStep),
drh8c0833f2017-11-14 23:48:23 +00001017 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
dan165921a2009-08-28 18:53:45 +00001018 );
drhdac9a5f2021-01-29 21:18:46 +00001019 sqlite3VdbeAddOp0(v, OP_ResetCount);
danielk1977633ed082002-05-17 00:05:58 +00001020 break;
1021 }
drh381bdac2021-02-04 17:29:04 +00001022 default: assert( pStep->op==TK_SELECT ); {
dan165921a2009-08-28 18:53:45 +00001023 SelectDest sDest;
1024 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
drhdac9a5f2021-01-29 21:18:46 +00001025 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
dan165921a2009-08-28 18:53:45 +00001026 sqlite3Select(pParse, pSelect, &sDest);
1027 sqlite3SelectDelete(db, pSelect);
drh9ab4c2e2009-05-09 00:18:38 +00001028 break;
1029 }
danielk1977633ed082002-05-17 00:05:58 +00001030 }
danielk1977633ed082002-05-17 00:05:58 +00001031 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001032
danielk1977633ed082002-05-17 00:05:58 +00001033 return 0;
danielk1977c3f9bad2002-05-15 08:30:12 +00001034}
1035
drhc7379ce2013-10-30 02:28:23 +00001036#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
dan165921a2009-08-28 18:53:45 +00001037/*
1038** This function is used to add VdbeComment() annotations to a VDBE
1039** program. It is not used in production code, only for debugging.
1040*/
1041static const char *onErrorText(int onError){
1042 switch( onError ){
1043 case OE_Abort: return "abort";
1044 case OE_Rollback: return "rollback";
1045 case OE_Fail: return "fail";
1046 case OE_Replace: return "replace";
1047 case OE_Ignore: return "ignore";
1048 case OE_Default: return "default";
1049 }
1050 return "n/a";
1051}
1052#endif
1053
1054/*
1055** Parse context structure pFrom has just been used to create a sub-vdbe
1056** (trigger program). If an error has occurred, transfer error information
1057** from pFrom to pTo.
1058*/
1059static void transferParseError(Parse *pTo, Parse *pFrom){
1060 assert( pFrom->zErrMsg==0 || pFrom->nErr );
1061 assert( pTo->zErrMsg==0 || pTo->nErr );
1062 if( pTo->nErr==0 ){
1063 pTo->zErrMsg = pFrom->zErrMsg;
1064 pTo->nErr = pFrom->nErr;
drhe06874e2015-04-16 15:47:06 +00001065 pTo->rc = pFrom->rc;
dan165921a2009-08-28 18:53:45 +00001066 }else{
1067 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
1068 }
1069}
1070
dan65a7cd12009-09-01 12:16:01 +00001071/*
1072** Create and populate a new TriggerPrg object with a sub-program
1073** implementing trigger pTrigger with ON CONFLICT policy orconf.
1074*/
dan2832ad42009-08-31 15:27:27 +00001075static TriggerPrg *codeRowTrigger(
dan165921a2009-08-28 18:53:45 +00001076 Parse *pParse, /* Current parse context */
1077 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +00001078 Table *pTab, /* The table pTrigger is attached to */
1079 int orconf /* ON CONFLICT policy to code trigger program with */
dan165921a2009-08-28 18:53:45 +00001080){
dan65a7cd12009-09-01 12:16:01 +00001081 Parse *pTop = sqlite3ParseToplevel(pParse);
1082 sqlite3 *db = pParse->db; /* Database handle */
1083 TriggerPrg *pPrg; /* Value to return */
dan165921a2009-08-28 18:53:45 +00001084 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
1085 Vdbe *v; /* Temporary VM */
dan165921a2009-08-28 18:53:45 +00001086 NameContext sNC; /* Name context for sub-vdbe */
1087 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
1088 Parse *pSubParse; /* Parse context for sub-vdbe */
1089 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
1090
dan1da40a32009-09-19 17:00:31 +00001091 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dand19c9332010-07-26 12:05:17 +00001092 assert( pTop->pVdbe );
dan65a7cd12009-09-01 12:16:01 +00001093
1094 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
1095 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
1096 ** list of the top-level Parse object sooner rather than later. */
dan2832ad42009-08-31 15:27:27 +00001097 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
1098 if( !pPrg ) return 0;
dan65a7cd12009-09-01 12:16:01 +00001099 pPrg->pNext = pTop->pTriggerPrg;
1100 pTop->pTriggerPrg = pPrg;
dan2832ad42009-08-31 15:27:27 +00001101 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
dan165921a2009-08-28 18:53:45 +00001102 if( !pProgram ) return 0;
dand19c9332010-07-26 12:05:17 +00001103 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
dan2832ad42009-08-31 15:27:27 +00001104 pPrg->pTrigger = pTrigger;
1105 pPrg->orconf = orconf;
danbb5f1682009-11-27 12:12:34 +00001106 pPrg->aColmask[0] = 0xffffffff;
1107 pPrg->aColmask[1] = 0xffffffff;
dan165921a2009-08-28 18:53:45 +00001108
dan65a7cd12009-09-01 12:16:01 +00001109 /* Allocate and populate a new Parse context to use for coding the
1110 ** trigger sub-program. */
1111 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
1112 if( !pSubParse ) return 0;
dan165921a2009-08-28 18:53:45 +00001113 memset(&sNC, 0, sizeof(sNC));
1114 sNC.pParse = pSubParse;
1115 pSubParse->db = db;
1116 pSubParse->pTriggerTab = pTab;
dan65a7cd12009-09-01 12:16:01 +00001117 pSubParse->pToplevel = pTop;
dan2bd93512009-08-31 08:22:46 +00001118 pSubParse->zAuthContext = pTrigger->zName;
dan65a7cd12009-09-01 12:16:01 +00001119 pSubParse->eTriggerOp = pTrigger->op;
drh8b307fb2010-04-06 15:57:05 +00001120 pSubParse->nQueryLoop = pParse->nQueryLoop;
dan1ea04432018-12-21 19:29:11 +00001121 pSubParse->disableVtab = pParse->disableVtab;
dan165921a2009-08-28 18:53:45 +00001122
1123 v = sqlite3GetVdbe(pSubParse);
1124 if( v ){
dan76d462e2009-08-30 11:42:51 +00001125 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
1126 pTrigger->zName, onErrorText(orconf),
dan165921a2009-08-28 18:53:45 +00001127 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
dan65a7cd12009-09-01 12:16:01 +00001128 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
1129 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
1130 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
dan76d462e2009-08-30 11:42:51 +00001131 pTab->zName
dan165921a2009-08-28 18:53:45 +00001132 ));
dan76d462e2009-08-30 11:42:51 +00001133#ifndef SQLITE_OMIT_TRACE
drhe307e112017-12-27 21:30:34 +00001134 if( pTrigger->zName ){
1135 sqlite3VdbeChangeP4(v, -1,
1136 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
1137 );
1138 }
dan76d462e2009-08-30 11:42:51 +00001139#endif
dan165921a2009-08-28 18:53:45 +00001140
dan65a7cd12009-09-01 12:16:01 +00001141 /* If one was specified, code the WHEN clause. If it evaluates to false
1142 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
1143 ** OP_Halt inserted at the end of the program. */
dan165921a2009-08-28 18:53:45 +00001144 if( pTrigger->pWhen ){
dan165921a2009-08-28 18:53:45 +00001145 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
dan523a0872009-08-31 05:23:32 +00001146 if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
1147 && db->mallocFailed==0
1148 ){
drhec4ccdb2018-12-29 02:26:59 +00001149 iEndTrigger = sqlite3VdbeMakeLabel(pSubParse);
dan165921a2009-08-28 18:53:45 +00001150 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
1151 }
1152 sqlite3ExprDelete(db, pWhen);
1153 }
1154
1155 /* Code the trigger program into the sub-vdbe. */
dan76d462e2009-08-30 11:42:51 +00001156 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
dan65a7cd12009-09-01 12:16:01 +00001157
1158 /* Insert an OP_Halt at the end of the sub-program. */
dan165921a2009-08-28 18:53:45 +00001159 if( iEndTrigger ){
1160 sqlite3VdbeResolveLabel(v, iEndTrigger);
1161 }
1162 sqlite3VdbeAddOp0(v, OP_Halt);
dan76d462e2009-08-30 11:42:51 +00001163 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
1164
dan165921a2009-08-28 18:53:45 +00001165 transferParseError(pParse, pSubParse);
dan08951172017-11-28 20:43:40 +00001166 if( db->mallocFailed==0 && pParse->nErr==0 ){
dan65a7cd12009-09-01 12:16:01 +00001167 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
dan523a0872009-08-31 05:23:32 +00001168 }
dan165921a2009-08-28 18:53:45 +00001169 pProgram->nMem = pSubParse->nMem;
1170 pProgram->nCsr = pSubParse->nTab;
1171 pProgram->token = (void *)pTrigger;
danbb5f1682009-11-27 12:12:34 +00001172 pPrg->aColmask[0] = pSubParse->oldmask;
1173 pPrg->aColmask[1] = pSubParse->newmask;
dan165921a2009-08-28 18:53:45 +00001174 sqlite3VdbeDelete(v);
dan165921a2009-08-28 18:53:45 +00001175 }
dan65a7cd12009-09-01 12:16:01 +00001176
dan65a7cd12009-09-01 12:16:01 +00001177 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
drhf30a9692013-11-15 01:10:18 +00001178 sqlite3ParserReset(pSubParse);
dan165921a2009-08-28 18:53:45 +00001179 sqlite3StackFree(db, pSubParse);
1180
dan2832ad42009-08-31 15:27:27 +00001181 return pPrg;
dan165921a2009-08-28 18:53:45 +00001182}
1183
dan65a7cd12009-09-01 12:16:01 +00001184/*
1185** Return a pointer to a TriggerPrg object containing the sub-program for
1186** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
1187** TriggerPrg object exists, a new object is allocated and populated before
1188** being returned.
1189*/
dan2832ad42009-08-31 15:27:27 +00001190static TriggerPrg *getRowTrigger(
dan65a7cd12009-09-01 12:16:01 +00001191 Parse *pParse, /* Current parse context */
dan165921a2009-08-28 18:53:45 +00001192 Trigger *pTrigger, /* Trigger to code */
dan65a7cd12009-09-01 12:16:01 +00001193 Table *pTab, /* The table trigger pTrigger is attached to */
1194 int orconf /* ON CONFLICT algorithm. */
dan165921a2009-08-28 18:53:45 +00001195){
dan65a7cd12009-09-01 12:16:01 +00001196 Parse *pRoot = sqlite3ParseToplevel(pParse);
dan2832ad42009-08-31 15:27:27 +00001197 TriggerPrg *pPrg;
dan65a7cd12009-09-01 12:16:01 +00001198
dan1da40a32009-09-19 17:00:31 +00001199 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
dan165921a2009-08-28 18:53:45 +00001200
1201 /* It may be that this trigger has already been coded (or is in the
1202 ** process of being coded). If this is the case, then an entry with
dan2832ad42009-08-31 15:27:27 +00001203 ** a matching TriggerPrg.pTrigger field will be present somewhere
1204 ** in the Parse.pTriggerPrg list. Search for such an entry. */
dan2832ad42009-08-31 15:27:27 +00001205 for(pPrg=pRoot->pTriggerPrg;
1206 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
1207 pPrg=pPrg->pNext
dan165921a2009-08-28 18:53:45 +00001208 );
1209
dan65a7cd12009-09-01 12:16:01 +00001210 /* If an existing TriggerPrg could not be located, create a new one. */
dan2832ad42009-08-31 15:27:27 +00001211 if( !pPrg ){
dan65a7cd12009-09-01 12:16:01 +00001212 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
dan165921a2009-08-28 18:53:45 +00001213 }
1214
dan2832ad42009-08-31 15:27:27 +00001215 return pPrg;
dan165921a2009-08-28 18:53:45 +00001216}
1217
dan94d7f502009-09-24 09:05:49 +00001218/*
1219** Generate code for the trigger program associated with trigger p on
1220** table pTab. The reg, orconf and ignoreJump parameters passed to this
1221** function are the same as those described in the header function for
1222** sqlite3CodeRowTrigger()
1223*/
dan1da40a32009-09-19 17:00:31 +00001224void sqlite3CodeRowTriggerDirect(
1225 Parse *pParse, /* Parse context */
1226 Trigger *p, /* Trigger to code */
1227 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001228 int reg, /* Reg array containing OLD.* and NEW.* values */
dan1da40a32009-09-19 17:00:31 +00001229 int orconf, /* ON CONFLICT policy */
1230 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
1231){
1232 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
1233 TriggerPrg *pPrg;
1234 pPrg = getRowTrigger(pParse, p, pTab, orconf);
1235 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
1236
1237 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
1238 ** is a pointer to the sub-vdbe containing the trigger program. */
1239 if( pPrg ){
dand19c9332010-07-26 12:05:17 +00001240 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
1241
drh9b34abe2016-01-16 15:12:35 +00001242 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
1243 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
dan1da40a32009-09-19 17:00:31 +00001244 VdbeComment(
1245 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
1246
1247 /* Set the P5 operand of the OP_Program instruction to non-zero if
1248 ** recursive invocation of this trigger program is disallowed. Recursive
1249 ** invocation is disallowed if (a) the sub-program is really a trigger,
1250 ** not a foreign key action, and (b) the flag to enable recursive triggers
1251 ** is clear. */
dand19c9332010-07-26 12:05:17 +00001252 sqlite3VdbeChangeP5(v, (u8)bRecursive);
dan1da40a32009-09-19 17:00:31 +00001253 }
1254}
1255
danielk1977633ed082002-05-17 00:05:58 +00001256/*
dan94d7f502009-09-24 09:05:49 +00001257** This is called to code the required FOR EACH ROW triggers for an operation
1258** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
drhf7b54962013-05-28 12:11:54 +00001259** is given by the op parameter. The tr_tm parameter determines whether the
dan94d7f502009-09-24 09:05:49 +00001260** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
1261** parameter pChanges is passed the list of columns being modified.
danielk1977633ed082002-05-17 00:05:58 +00001262**
dan94d7f502009-09-24 09:05:49 +00001263** If there are no triggers that fire at the specified time for the specified
1264** operation on pTab, this function is a no-op.
drhc977f7f2002-05-21 11:38:11 +00001265**
dan94d7f502009-09-24 09:05:49 +00001266** The reg argument is the address of the first in an array of registers
1267** that contain the values substituted for the new.* and old.* references
1268** in the trigger program. If N is the number of columns in table pTab
1269** (a copy of pTab->nCol), then registers are populated as follows:
drhc977f7f2002-05-21 11:38:11 +00001270**
dan94d7f502009-09-24 09:05:49 +00001271** Register Contains
1272** ------------------------------------------------------
1273** reg+0 OLD.rowid
1274** reg+1 OLD.* value of left-most column of pTab
1275** ... ...
1276** reg+N OLD.* value of right-most column of pTab
1277** reg+N+1 NEW.rowid
drh381bdac2021-02-04 17:29:04 +00001278** reg+N+2 NEW.* value of left-most column of pTab
dan94d7f502009-09-24 09:05:49 +00001279** ... ...
1280** reg+N+N+1 NEW.* value of right-most column of pTab
drhc977f7f2002-05-21 11:38:11 +00001281**
dan94d7f502009-09-24 09:05:49 +00001282** For ON DELETE triggers, the registers containing the NEW.* values will
1283** never be accessed by the trigger program, so they are not allocated or
1284** populated by the caller (there is no data to populate them with anyway).
1285** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
1286** are never accessed, and so are not allocated by the caller. So, for an
1287** ON INSERT trigger, the value passed to this function as parameter reg
1288** is not a readable register, although registers (reg+N) through
1289** (reg+N+N+1) are.
danielk1977633ed082002-05-17 00:05:58 +00001290**
dan94d7f502009-09-24 09:05:49 +00001291** Parameter orconf is the default conflict resolution algorithm for the
1292** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
1293** is the instruction that control should jump to if a trigger program
1294** raises an IGNORE exception.
danielk1977633ed082002-05-17 00:05:58 +00001295*/
dan165921a2009-08-28 18:53:45 +00001296void sqlite3CodeRowTrigger(
danielk1977633ed082002-05-17 00:05:58 +00001297 Parse *pParse, /* Parse context */
danielk19772f886d12009-02-28 10:47:41 +00001298 Trigger *pTrigger, /* List of triggers on table pTab */
danielk1977633ed082002-05-17 00:05:58 +00001299 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
1300 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
drhdca76842004-12-07 14:06:13 +00001301 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
danielk1977633ed082002-05-17 00:05:58 +00001302 Table *pTab, /* The table to code triggers from */
dan94d7f502009-09-24 09:05:49 +00001303 int reg, /* The first in an array of registers (see above) */
danielk19776f349032002-06-11 02:25:40 +00001304 int orconf, /* ON CONFLICT policy */
dan165921a2009-08-28 18:53:45 +00001305 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
drhc977f7f2002-05-21 11:38:11 +00001306){
dan94d7f502009-09-24 09:05:49 +00001307 Trigger *p; /* Used to iterate through pTrigger list */
danielk19778f2c54e2008-01-01 19:02:09 +00001308
dan94d7f502009-09-24 09:05:49 +00001309 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
1310 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
1311 assert( (op==TK_UPDATE)==(pChanges!=0) );
danielk1977c3f9bad2002-05-15 08:30:12 +00001312
danielk19772f886d12009-02-28 10:47:41 +00001313 for(p=pTrigger; p; p=p->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +00001314
drh9ab4c2e2009-05-09 00:18:38 +00001315 /* Sanity checking: The schema for the trigger and for the table are
1316 ** always defined. The trigger must be in the same schema as the table
1317 ** or else it must be a TEMP trigger. */
1318 assert( p->pSchema!=0 );
1319 assert( p->pTabSchema!=0 );
dan165921a2009-08-28 18:53:45 +00001320 assert( p->pSchema==p->pTabSchema
1321 || p->pSchema==pParse->db->aDb[1].pSchema );
drh9ab4c2e2009-05-09 00:18:38 +00001322
drh28828c52021-01-30 21:55:38 +00001323 /* Determine whether we should code this trigger. One of two choices:
1324 ** 1. The trigger is an exact match to the current DML statement
1325 ** 2. This is a RETURNING trigger for INSERT but we are currently
1326 ** doing the UPDATE part of an UPSERT.
1327 */
1328 if( (p->op==op || (p->bReturning && p->op==TK_INSERT && op==TK_UPDATE))
dan165921a2009-08-28 18:53:45 +00001329 && p->tr_tm==tr_tm
dan94d7f502009-09-24 09:05:49 +00001330 && checkColumnOverlap(p->pColumns, pChanges)
danielk1977eecfb3e2006-01-10 12:31:39 +00001331 ){
drh381bdac2021-02-04 17:29:04 +00001332 if( !p->bReturning ){
1333 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
1334 }else if( sqlite3IsToplevel(pParse) ){
1335 codeReturningTrigger(pParse, p, pTab, reg);
1336 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001337 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001338 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001339}
dan165921a2009-08-28 18:53:45 +00001340
dan2832ad42009-08-31 15:27:27 +00001341/*
danbb5f1682009-11-27 12:12:34 +00001342** Triggers may access values stored in the old.* or new.* pseudo-table.
1343** This function returns a 32-bit bitmask indicating which columns of the
1344** old.* or new.* tables actually are used by triggers. This information
1345** may be used by the caller, for example, to avoid having to load the entire
1346** old.* record into memory when executing an UPDATE or DELETE command.
dan2832ad42009-08-31 15:27:27 +00001347**
1348** Bit 0 of the returned mask is set if the left-most column of the
danbb5f1682009-11-27 12:12:34 +00001349** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
dan2832ad42009-08-31 15:27:27 +00001350** the second leftmost column value is required, and so on. If there
1351** are more than 32 columns in the table, and at least one of the columns
1352** with an index greater than 32 may be accessed, 0xffffffff is returned.
1353**
danbb5f1682009-11-27 12:12:34 +00001354** It is not possible to determine if the old.rowid or new.rowid column is
1355** accessed by triggers. The caller must always assume that it is.
dan2832ad42009-08-31 15:27:27 +00001356**
danbb5f1682009-11-27 12:12:34 +00001357** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
1358** applies to the old.* table. If 1, the new.* table.
1359**
1360** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
1361** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
1362** included in the returned mask if the TRIGGER_BEFORE bit is set in the
1363** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
1364** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
dan2832ad42009-08-31 15:27:27 +00001365*/
danbb5f1682009-11-27 12:12:34 +00001366u32 sqlite3TriggerColmask(
dan165921a2009-08-28 18:53:45 +00001367 Parse *pParse, /* Parse context */
1368 Trigger *pTrigger, /* List of triggers on table pTab */
dan165921a2009-08-28 18:53:45 +00001369 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
danbb5f1682009-11-27 12:12:34 +00001370 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
1371 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
dan165921a2009-08-28 18:53:45 +00001372 Table *pTab, /* The table to code triggers from */
dan2832ad42009-08-31 15:27:27 +00001373 int orconf /* Default ON CONFLICT policy for trigger steps */
dan165921a2009-08-28 18:53:45 +00001374){
dan1da40a32009-09-19 17:00:31 +00001375 const int op = pChanges ? TK_UPDATE : TK_DELETE;
dan2832ad42009-08-31 15:27:27 +00001376 u32 mask = 0;
dan165921a2009-08-28 18:53:45 +00001377 Trigger *p;
dan165921a2009-08-28 18:53:45 +00001378
danbb5f1682009-11-27 12:12:34 +00001379 assert( isNew==1 || isNew==0 );
dan165921a2009-08-28 18:53:45 +00001380 for(p=pTrigger; p; p=p->pNext){
drh381bdac2021-02-04 17:29:04 +00001381 if( p->op==op
1382 && (tr_tm&p->tr_tm)
danbb5f1682009-11-27 12:12:34 +00001383 && checkColumnOverlap(p->pColumns,pChanges)
1384 ){
drh381bdac2021-02-04 17:29:04 +00001385 if( p->bReturning ){
1386 mask = 0xffffffff;
1387 }else{
1388 TriggerPrg *pPrg;
1389 pPrg = getRowTrigger(pParse, p, pTab, orconf);
1390 if( pPrg ){
1391 mask |= pPrg->aColmask[isNew];
1392 }
dan165921a2009-08-28 18:53:45 +00001393 }
1394 }
1395 }
dan2832ad42009-08-31 15:27:27 +00001396
1397 return mask;
dan165921a2009-08-28 18:53:45 +00001398}
1399
drhb7f91642004-10-31 02:22:47 +00001400#endif /* !defined(SQLITE_OMIT_TRIGGER) */