2002-05-15 12:30:12 +04:00
|
|
|
/*
|
2002-05-17 04:05:58 +04:00
|
|
|
**
|
|
|
|
** The author disclaims copyright to this source code. In place of
|
|
|
|
** a legal notice, here is a blessing:
|
|
|
|
**
|
|
|
|
** May you do good and not evil.
|
|
|
|
** May you find forgiveness for yourself and forgive others.
|
|
|
|
** May you share freely, never taking more than you give.
|
|
|
|
**
|
|
|
|
*************************************************************************
|
2008-05-16 08:51:54 +04:00
|
|
|
**
|
|
|
|
**
|
2008-11-19 12:05:26 +03:00
|
|
|
** $Id: trigger.c,v 1.130 2008/11/19 09:05:27 danielk1977 Exp $
|
2002-05-15 15:44:13 +04:00
|
|
|
*/
|
2002-05-15 12:30:12 +04:00
|
|
|
#include "sqliteInt.h"
|
2002-05-15 15:44:13 +04:00
|
|
|
|
2004-10-31 05:22:47 +03:00
|
|
|
#ifndef SQLITE_OMIT_TRIGGER
|
2002-08-24 22:24:51 +04:00
|
|
|
/*
|
|
|
|
** Delete a linked list of TriggerStep structures.
|
|
|
|
*/
|
2008-07-28 23:34:53 +04:00
|
|
|
void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
|
2002-08-24 22:24:51 +04:00
|
|
|
while( pTriggerStep ){
|
|
|
|
TriggerStep * pTmp = pTriggerStep;
|
|
|
|
pTriggerStep = pTriggerStep->pNext;
|
|
|
|
|
2008-07-28 23:34:53 +04:00
|
|
|
if( pTmp->target.dyn ) sqlite3DbFree(db, (char*)pTmp->target.z);
|
|
|
|
sqlite3ExprDelete(db, pTmp->pWhere);
|
|
|
|
sqlite3ExprListDelete(db, pTmp->pExprList);
|
|
|
|
sqlite3SelectDelete(db, pTmp->pSelect);
|
|
|
|
sqlite3IdListDelete(db, pTmp->pIdList);
|
2002-08-24 22:24:51 +04:00
|
|
|
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3DbFree(db, pTmp);
|
2002-08-24 22:24:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-15 12:30:12 +04:00
|
|
|
/*
|
2003-04-21 22:48:45 +04:00
|
|
|
** This is called by the parser when it sees a CREATE TRIGGER statement
|
|
|
|
** up to the point of the BEGIN before the trigger actions. A Trigger
|
|
|
|
** structure is generated based on the information available and stored
|
|
|
|
** in pParse->pNewTrigger. After the trigger actions have been parsed, the
|
2004-05-08 12:23:19 +04:00
|
|
|
** sqlite3FinishTrigger() function is called to complete the trigger
|
2003-04-21 22:48:45 +04:00
|
|
|
** construction process.
|
2002-05-15 15:44:13 +04:00
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
void sqlite3BeginTrigger(
|
2002-05-15 15:44:13 +04:00
|
|
|
Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
|
2004-05-29 06:37:19 +04:00
|
|
|
Token *pName1, /* The name of the trigger */
|
|
|
|
Token *pName2, /* The name of the trigger */
|
2003-04-24 05:45:04 +04:00
|
|
|
int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
|
2002-05-15 15:44:13 +04:00
|
|
|
int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
|
2002-05-17 04:05:58 +04:00
|
|
|
IdList *pColumns, /* column list if this is an UPDATE OF trigger */
|
2003-03-27 15:51:24 +03:00
|
|
|
SrcList *pTableName,/* The name of the table/view the trigger applies to */
|
2002-05-15 15:44:13 +04:00
|
|
|
Expr *pWhen, /* WHEN clause */
|
2006-09-12 03:45:48 +04:00
|
|
|
int isTemp, /* True if the TEMPORARY keyword is present */
|
|
|
|
int noErr /* Suppress errors if the trigger already exists */
|
2002-05-15 15:44:13 +04:00
|
|
|
){
|
2005-03-16 15:15:20 +03:00
|
|
|
Trigger *pTrigger = 0;
|
2004-05-29 06:37:19 +04:00
|
|
|
Table *pTab;
|
2003-04-21 22:48:45 +04:00
|
|
|
char *zName = 0; /* Name of the trigger */
|
2004-09-06 21:24:11 +04:00
|
|
|
sqlite3 *db = pParse->db;
|
2004-05-29 06:37:19 +04:00
|
|
|
int iDb; /* The database to store the trigger in */
|
|
|
|
Token *pName; /* The unqualified db name */
|
2003-06-03 05:47:11 +04:00
|
|
|
DbFixer sFix;
|
2006-01-05 14:34:32 +03:00
|
|
|
int iTabDb;
|
2003-01-12 21:02:16 +03:00
|
|
|
|
2006-03-06 23:55:46 +03:00
|
|
|
assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
|
|
|
|
assert( pName2!=0 );
|
2004-05-29 06:37:19 +04:00
|
|
|
if( isTemp ){
|
|
|
|
/* If TEMP was specified, then the trigger name may not be qualified. */
|
2006-03-06 23:55:46 +03:00
|
|
|
if( pName2->n>0 ){
|
2004-05-29 06:37:19 +04:00
|
|
|
sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
|
|
|
|
goto trigger_cleanup;
|
|
|
|
}
|
|
|
|
iDb = 1;
|
|
|
|
pName = pName1;
|
|
|
|
}else{
|
|
|
|
/* Figure out the db that the the trigger will be created in */
|
|
|
|
iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
|
|
|
|
if( iDb<0 ){
|
|
|
|
goto trigger_cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the trigger name was unqualified, and the table is a temp table,
|
|
|
|
** then set iDb to 1 to create the trigger in the temporary database.
|
|
|
|
** If sqlite3SrcListLookup() returns 0, indicating the table does not
|
|
|
|
** exist, the error is caught by the block below.
|
2002-05-15 15:44:13 +04:00
|
|
|
*/
|
2007-08-16 08:30:38 +04:00
|
|
|
if( !pTableName || db->mallocFailed ){
|
2006-01-12 00:41:20 +03:00
|
|
|
goto trigger_cleanup;
|
|
|
|
}
|
2004-05-29 06:37:19 +04:00
|
|
|
pTab = sqlite3SrcListLookup(pParse, pTableName);
|
2006-01-05 14:34:32 +03:00
|
|
|
if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
|
2004-05-29 06:37:19 +04:00
|
|
|
iDb = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure the table name matches database name and that the table exists */
|
2007-08-16 08:30:38 +04:00
|
|
|
if( db->mallocFailed ) goto trigger_cleanup;
|
2003-03-27 15:51:24 +03:00
|
|
|
assert( pTableName->nSrc==1 );
|
2004-05-29 06:37:19 +04:00
|
|
|
if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) &&
|
|
|
|
sqlite3FixSrcList(&sFix, pTableName) ){
|
2003-03-27 15:51:24 +03:00
|
|
|
goto trigger_cleanup;
|
|
|
|
}
|
2004-05-29 06:37:19 +04:00
|
|
|
pTab = sqlite3SrcListLookup(pParse, pTableName);
|
|
|
|
if( !pTab ){
|
|
|
|
/* The table does not exist. */
|
2003-03-27 15:51:24 +03:00
|
|
|
goto trigger_cleanup;
|
|
|
|
}
|
2006-06-14 23:00:20 +04:00
|
|
|
if( IsVirtual(pTab) ){
|
|
|
|
sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
|
|
|
|
goto trigger_cleanup;
|
|
|
|
}
|
2003-03-27 15:51:24 +03:00
|
|
|
|
2004-06-12 13:25:12 +04:00
|
|
|
/* Check that the trigger name is not reserved and that no trigger of the
|
|
|
|
** specified name exists */
|
2007-08-16 08:30:38 +04:00
|
|
|
zName = sqlite3NameFromToken(db, pName);
|
2004-06-12 13:25:12 +04:00
|
|
|
if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
|
|
|
|
goto trigger_cleanup;
|
|
|
|
}
|
2006-03-29 04:24:06 +04:00
|
|
|
if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash), zName,strlen(zName)) ){
|
2006-09-12 03:45:48 +04:00
|
|
|
if( !noErr ){
|
|
|
|
sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
|
|
|
|
}
|
2003-01-14 02:27:31 +03:00
|
|
|
goto trigger_cleanup;
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
2004-05-29 06:37:19 +04:00
|
|
|
|
|
|
|
/* Do not create a trigger on a system table */
|
2004-12-07 17:06:13 +03:00
|
|
|
if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
|
2003-03-27 15:51:24 +03:00
|
|
|
pParse->nErr++;
|
|
|
|
goto trigger_cleanup;
|
|
|
|
}
|
2004-05-29 06:37:19 +04:00
|
|
|
|
|
|
|
/* INSTEAD of triggers are only for views and views only support INSTEAD
|
|
|
|
** of triggers.
|
|
|
|
*/
|
|
|
|
if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
|
2003-03-31 06:12:46 +04:00
|
|
|
(tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
|
2003-03-27 15:51:24 +03:00
|
|
|
goto trigger_cleanup;
|
|
|
|
}
|
2004-05-29 06:37:19 +04:00
|
|
|
if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
|
2003-03-31 06:12:46 +04:00
|
|
|
" trigger on table: %S", pTableName, 0);
|
2003-03-27 15:51:24 +03:00
|
|
|
goto trigger_cleanup;
|
|
|
|
}
|
2006-01-05 14:34:32 +03:00
|
|
|
iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
|
2004-05-29 06:37:19 +04:00
|
|
|
|
2003-03-27 15:51:24 +03:00
|
|
|
#ifndef SQLITE_OMIT_AUTHORIZATION
|
2002-05-15 12:30:12 +04:00
|
|
|
{
|
2003-03-27 15:51:24 +03:00
|
|
|
int code = SQLITE_CREATE_TRIGGER;
|
2006-01-05 14:34:32 +03:00
|
|
|
const char *zDb = db->aDb[iTabDb].zName;
|
2003-04-23 00:30:37 +04:00
|
|
|
const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
|
2006-01-05 14:34:32 +03:00
|
|
|
if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
|
2004-05-29 06:37:19 +04:00
|
|
|
if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
|
2002-05-23 04:30:31 +04:00
|
|
|
goto trigger_cleanup;
|
|
|
|
}
|
2006-01-05 14:34:32 +03:00
|
|
|
if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
|
2002-06-25 05:09:11 +04:00
|
|
|
goto trigger_cleanup;
|
|
|
|
}
|
2002-05-27 03:24:40 +04:00
|
|
|
}
|
2003-03-27 15:51:24 +03:00
|
|
|
#endif
|
2002-05-27 03:24:40 +04:00
|
|
|
|
2004-05-29 06:37:19 +04:00
|
|
|
/* INSTEAD OF triggers can only appear on views and BEFORE triggers
|
2003-04-24 05:45:04 +04:00
|
|
|
** cannot appear on views. So we might as well translate every
|
|
|
|
** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
|
|
|
|
** elsewhere.
|
|
|
|
*/
|
2002-05-27 03:24:40 +04:00
|
|
|
if (tr_tm == TK_INSTEAD){
|
|
|
|
tr_tm = TK_BEFORE;
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Build the Trigger object */
|
2007-08-16 08:30:38 +04:00
|
|
|
pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
|
2004-05-29 06:37:19 +04:00
|
|
|
if( pTrigger==0 ) goto trigger_cleanup;
|
|
|
|
pTrigger->name = zName;
|
2003-01-14 02:27:31 +03:00
|
|
|
zName = 0;
|
2007-08-16 08:30:38 +04:00
|
|
|
pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
|
2006-01-05 14:34:32 +03:00
|
|
|
pTrigger->pSchema = db->aDb[iDb].pSchema;
|
2006-01-06 18:03:48 +03:00
|
|
|
pTrigger->pTabSchema = pTab->pSchema;
|
2004-05-29 06:37:19 +04:00
|
|
|
pTrigger->op = op;
|
2004-12-07 17:06:13 +03:00
|
|
|
pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
|
2007-08-16 08:30:38 +04:00
|
|
|
pTrigger->pWhen = sqlite3ExprDup(db, pWhen);
|
|
|
|
pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
|
|
|
|
sqlite3TokenCopy(db, &pTrigger->nameToken,pName);
|
2003-04-21 22:48:45 +04:00
|
|
|
assert( pParse->pNewTrigger==0 );
|
2004-05-29 06:37:19 +04:00
|
|
|
pParse->pNewTrigger = pTrigger;
|
2003-04-21 22:48:45 +04:00
|
|
|
|
|
|
|
trigger_cleanup:
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3DbFree(db, zName);
|
|
|
|
sqlite3SrcListDelete(db, pTableName);
|
|
|
|
sqlite3IdListDelete(db, pColumns);
|
|
|
|
sqlite3ExprDelete(db, pWhen);
|
2005-03-16 15:15:20 +03:00
|
|
|
if( !pParse->pNewTrigger ){
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3DeleteTrigger(db, pTrigger);
|
2005-03-16 15:15:20 +03:00
|
|
|
}else{
|
|
|
|
assert( pParse->pNewTrigger==pTrigger );
|
|
|
|
}
|
2003-04-21 22:48:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This routine is called after all of the trigger actions have been parsed
|
|
|
|
** in order to complete the process of building the trigger.
|
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
void sqlite3FinishTrigger(
|
2003-04-21 22:48:45 +04:00
|
|
|
Parse *pParse, /* Parser context */
|
|
|
|
TriggerStep *pStepList, /* The triggered program */
|
|
|
|
Token *pAll /* Token that describes the complete CREATE TRIGGER */
|
|
|
|
){
|
2005-01-24 16:03:32 +03:00
|
|
|
Trigger *pTrig = 0; /* The trigger whose construction is finishing up */
|
2004-09-06 21:24:11 +04:00
|
|
|
sqlite3 *db = pParse->db; /* The database */
|
2003-05-31 20:21:12 +04:00
|
|
|
DbFixer sFix;
|
2006-01-05 14:34:32 +03:00
|
|
|
int iDb; /* Database containing the trigger */
|
2003-04-21 22:48:45 +04:00
|
|
|
|
2005-01-24 16:03:32 +03:00
|
|
|
pTrig = pParse->pNewTrigger;
|
2003-04-21 22:48:45 +04:00
|
|
|
pParse->pNewTrigger = 0;
|
2005-12-09 17:25:08 +03:00
|
|
|
if( pParse->nErr || !pTrig ) goto triggerfinish_cleanup;
|
2006-01-05 14:34:32 +03:00
|
|
|
iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
|
2005-01-24 16:03:32 +03:00
|
|
|
pTrig->step_list = pStepList;
|
2003-04-18 02:57:53 +04:00
|
|
|
while( pStepList ){
|
2005-01-24 16:03:32 +03:00
|
|
|
pStepList->pTrig = pTrig;
|
2003-04-18 02:57:53 +04:00
|
|
|
pStepList = pStepList->pNext;
|
|
|
|
}
|
2006-01-05 14:34:32 +03:00
|
|
|
if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &pTrig->nameToken)
|
2005-01-24 16:03:32 +03:00
|
|
|
&& sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
|
2003-05-31 20:21:12 +04:00
|
|
|
goto triggerfinish_cleanup;
|
|
|
|
}
|
2002-05-15 12:30:12 +04:00
|
|
|
|
|
|
|
/* if we are not initializing, and this trigger is not on a TEMP table,
|
2002-05-15 15:44:13 +04:00
|
|
|
** build the sqlite_master entry
|
|
|
|
*/
|
2004-02-15 02:05:52 +03:00
|
|
|
if( !db->init.busy ){
|
2002-05-21 15:38:11 +04:00
|
|
|
Vdbe *v;
|
2008-01-11 02:50:11 +03:00
|
|
|
char *z;
|
2002-05-15 12:30:12 +04:00
|
|
|
|
|
|
|
/* Make an entry in the sqlite_master table */
|
2004-05-08 12:23:19 +04:00
|
|
|
v = sqlite3GetVdbe(pParse);
|
2003-04-21 22:48:45 +04:00
|
|
|
if( v==0 ) goto triggerfinish_cleanup;
|
2006-01-05 14:34:32 +03:00
|
|
|
sqlite3BeginWriteOperation(pParse, 0, iDb);
|
2008-01-11 02:50:11 +03:00
|
|
|
z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
|
|
|
|
sqlite3NestedParse(pParse,
|
|
|
|
"INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
|
|
|
|
db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pTrig->name,
|
|
|
|
pTrig->table, z);
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3DbFree(db, z);
|
2008-01-17 19:22:13 +03:00
|
|
|
sqlite3ChangeCookie(pParse, iDb);
|
2008-01-03 03:01:23 +03:00
|
|
|
sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, sqlite3MPrintf(
|
|
|
|
db, "type='trigger' AND name='%q'", pTrig->name), P4_DYNAMIC
|
2007-08-16 14:09:01 +04:00
|
|
|
);
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
|
|
|
|
2004-07-24 21:38:29 +04:00
|
|
|
if( db->init.busy ){
|
2006-01-06 18:03:48 +03:00
|
|
|
int n;
|
2003-04-21 22:48:45 +04:00
|
|
|
Table *pTab;
|
2005-03-16 15:15:20 +03:00
|
|
|
Trigger *pDel;
|
2006-01-05 14:34:32 +03:00
|
|
|
pDel = sqlite3HashInsert(&db->aDb[iDb].pSchema->trigHash,
|
2006-03-29 04:24:06 +04:00
|
|
|
pTrig->name, strlen(pTrig->name), pTrig);
|
2005-03-16 15:15:20 +03:00
|
|
|
if( pDel ){
|
2007-08-23 00:18:21 +04:00
|
|
|
assert( pDel==pTrig );
|
|
|
|
db->mallocFailed = 1;
|
2005-03-16 15:15:20 +03:00
|
|
|
goto triggerfinish_cleanup;
|
|
|
|
}
|
2006-01-06 18:03:48 +03:00
|
|
|
n = strlen(pTrig->table) + 1;
|
|
|
|
pTab = sqlite3HashFind(&pTrig->pTabSchema->tblHash, pTrig->table, n);
|
2003-04-21 22:48:45 +04:00
|
|
|
assert( pTab!=0 );
|
2005-01-24 16:03:32 +03:00
|
|
|
pTrig->pNext = pTab->pTrigger;
|
|
|
|
pTab->pTrigger = pTrig;
|
|
|
|
pTrig = 0;
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
|
|
|
|
2003-04-21 22:48:45 +04:00
|
|
|
triggerfinish_cleanup:
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3DeleteTrigger(db, pTrig);
|
2005-01-24 16:03:32 +03:00
|
|
|
assert( !pParse->pNewTrigger );
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3DeleteTriggerStep(db, pStepList);
|
2002-08-24 22:24:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Make a copy of all components of the given trigger step. This has
|
|
|
|
** the effect of copying all Expr.token.z values into memory obtained
|
2007-08-16 08:30:38 +04:00
|
|
|
** from sqlite3_malloc(). As initially created, the Expr.token.z values
|
2002-08-24 22:24:51 +04:00
|
|
|
** all point to the input string that was fed to the parser. But that
|
2004-05-10 14:34:51 +04:00
|
|
|
** string is ephemeral - it will go away as soon as the sqlite3_exec()
|
2002-08-24 22:24:51 +04:00
|
|
|
** call that started the parser exits. This routine makes a persistent
|
|
|
|
** copy of all the Expr.token.z strings so that the TriggerStep structure
|
2004-05-10 14:34:51 +04:00
|
|
|
** will be valid even after the sqlite3_exec() call returns.
|
2002-08-24 22:24:51 +04:00
|
|
|
*/
|
2007-08-16 08:30:38 +04:00
|
|
|
static void sqlitePersistTriggerStep(sqlite3 *db, TriggerStep *p){
|
2002-08-24 22:24:51 +04:00
|
|
|
if( p->target.z ){
|
2007-08-16 08:30:38 +04:00
|
|
|
p->target.z = (u8*)sqlite3DbStrNDup(db, (char*)p->target.z, p->target.n);
|
2002-08-24 22:24:51 +04:00
|
|
|
p->target.dyn = 1;
|
|
|
|
}
|
|
|
|
if( p->pSelect ){
|
2007-08-16 08:30:38 +04:00
|
|
|
Select *pNew = sqlite3SelectDup(db, p->pSelect);
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3SelectDelete(db, p->pSelect);
|
2002-08-24 22:24:51 +04:00
|
|
|
p->pSelect = pNew;
|
|
|
|
}
|
|
|
|
if( p->pWhere ){
|
2007-08-16 08:30:38 +04:00
|
|
|
Expr *pNew = sqlite3ExprDup(db, p->pWhere);
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3ExprDelete(db, p->pWhere);
|
2002-08-24 22:24:51 +04:00
|
|
|
p->pWhere = pNew;
|
|
|
|
}
|
|
|
|
if( p->pExprList ){
|
2007-08-16 08:30:38 +04:00
|
|
|
ExprList *pNew = sqlite3ExprListDup(db, p->pExprList);
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3ExprListDelete(db, p->pExprList);
|
2002-08-24 22:24:51 +04:00
|
|
|
p->pExprList = pNew;
|
|
|
|
}
|
|
|
|
if( p->pIdList ){
|
2007-08-16 08:30:38 +04:00
|
|
|
IdList *pNew = sqlite3IdListDup(db, p->pIdList);
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3IdListDelete(db, p->pIdList);
|
2002-08-24 22:24:51 +04:00
|
|
|
p->pIdList = pNew;
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-21 15:38:11 +04:00
|
|
|
/*
|
|
|
|
** Turn a SELECT statement (that the pSelect parameter points to) into
|
|
|
|
** a trigger step. Return a pointer to a TriggerStep structure.
|
|
|
|
**
|
|
|
|
** The parser calls this routine when it finds a SELECT statement in
|
|
|
|
** body of a TRIGGER.
|
|
|
|
*/
|
2007-08-16 08:30:38 +04:00
|
|
|
TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
|
|
|
|
TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
|
2005-12-09 17:25:08 +03:00
|
|
|
if( pTriggerStep==0 ) {
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3SelectDelete(db, pSelect);
|
2005-12-09 17:25:08 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2002-05-15 12:30:12 +04:00
|
|
|
|
2002-05-17 04:05:58 +04:00
|
|
|
pTriggerStep->op = TK_SELECT;
|
|
|
|
pTriggerStep->pSelect = pSelect;
|
|
|
|
pTriggerStep->orconf = OE_Default;
|
2007-08-16 08:30:38 +04:00
|
|
|
sqlitePersistTriggerStep(db, pTriggerStep);
|
2002-05-15 12:30:12 +04:00
|
|
|
|
2002-05-17 04:05:58 +04:00
|
|
|
return pTriggerStep;
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
|
|
|
|
2002-05-21 15:38:11 +04:00
|
|
|
/*
|
|
|
|
** Build a trigger step out of an INSERT statement. Return a pointer
|
|
|
|
** to the new trigger step.
|
|
|
|
**
|
|
|
|
** The parser calls this routine when it sees an INSERT inside the
|
|
|
|
** body of a trigger.
|
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
TriggerStep *sqlite3TriggerInsertStep(
|
2007-08-16 08:30:38 +04:00
|
|
|
sqlite3 *db, /* The database connection */
|
2002-05-21 15:38:11 +04:00
|
|
|
Token *pTableName, /* Name of the table into which we insert */
|
|
|
|
IdList *pColumn, /* List of columns in pTableName to insert into */
|
|
|
|
ExprList *pEList, /* The VALUE clause: a list of values to be inserted */
|
|
|
|
Select *pSelect, /* A SELECT statement that supplies values */
|
|
|
|
int orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
|
2002-05-17 04:05:58 +04:00
|
|
|
){
|
2007-08-23 00:18:21 +04:00
|
|
|
TriggerStep *pTriggerStep;
|
2002-05-15 12:30:12 +04:00
|
|
|
|
2002-05-17 04:05:58 +04:00
|
|
|
assert(pEList == 0 || pSelect == 0);
|
2007-08-23 00:18:21 +04:00
|
|
|
assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
|
2002-05-15 12:30:12 +04:00
|
|
|
|
2007-08-23 00:18:21 +04:00
|
|
|
pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
|
2005-03-16 15:15:20 +03:00
|
|
|
if( pTriggerStep ){
|
|
|
|
pTriggerStep->op = TK_INSERT;
|
|
|
|
pTriggerStep->pSelect = pSelect;
|
|
|
|
pTriggerStep->target = *pTableName;
|
|
|
|
pTriggerStep->pIdList = pColumn;
|
|
|
|
pTriggerStep->pExprList = pEList;
|
|
|
|
pTriggerStep->orconf = orconf;
|
2007-08-16 08:30:38 +04:00
|
|
|
sqlitePersistTriggerStep(db, pTriggerStep);
|
2005-03-16 15:15:20 +03:00
|
|
|
}else{
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3IdListDelete(db, pColumn);
|
|
|
|
sqlite3ExprListDelete(db, pEList);
|
|
|
|
sqlite3SelectDelete(db, pSelect);
|
2005-03-16 15:15:20 +03:00
|
|
|
}
|
2002-05-15 12:30:12 +04:00
|
|
|
|
2002-05-17 04:05:58 +04:00
|
|
|
return pTriggerStep;
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
|
|
|
|
2002-05-21 15:38:11 +04:00
|
|
|
/*
|
|
|
|
** Construct a trigger step that implements an UPDATE statement and return
|
|
|
|
** a pointer to that trigger step. The parser calls this routine when it
|
|
|
|
** sees an UPDATE statement inside the body of a CREATE TRIGGER.
|
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
TriggerStep *sqlite3TriggerUpdateStep(
|
2007-08-16 08:30:38 +04:00
|
|
|
sqlite3 *db, /* The database connection */
|
2002-05-21 15:38:11 +04:00
|
|
|
Token *pTableName, /* Name of the table to be updated */
|
|
|
|
ExprList *pEList, /* The SET clause: list of column and new values */
|
|
|
|
Expr *pWhere, /* The WHERE clause */
|
|
|
|
int orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
|
|
|
|
){
|
2007-08-16 08:30:38 +04:00
|
|
|
TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
|
2007-03-31 00:40:34 +04:00
|
|
|
if( pTriggerStep==0 ){
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3ExprListDelete(db, pEList);
|
|
|
|
sqlite3ExprDelete(db, pWhere);
|
2007-03-31 00:40:34 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2002-05-15 12:30:12 +04:00
|
|
|
|
2002-05-17 04:05:58 +04:00
|
|
|
pTriggerStep->op = TK_UPDATE;
|
|
|
|
pTriggerStep->target = *pTableName;
|
|
|
|
pTriggerStep->pExprList = pEList;
|
|
|
|
pTriggerStep->pWhere = pWhere;
|
|
|
|
pTriggerStep->orconf = orconf;
|
2007-08-16 08:30:38 +04:00
|
|
|
sqlitePersistTriggerStep(db, pTriggerStep);
|
2002-05-15 12:30:12 +04:00
|
|
|
|
2002-05-17 04:05:58 +04:00
|
|
|
return pTriggerStep;
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
|
|
|
|
2002-05-21 15:38:11 +04:00
|
|
|
/*
|
|
|
|
** Construct a trigger step that implements a DELETE statement and return
|
|
|
|
** a pointer to that trigger step. The parser calls this routine when it
|
|
|
|
** sees a DELETE statement inside the body of a CREATE TRIGGER.
|
|
|
|
*/
|
2007-08-16 08:30:38 +04:00
|
|
|
TriggerStep *sqlite3TriggerDeleteStep(
|
|
|
|
sqlite3 *db, /* Database connection */
|
|
|
|
Token *pTableName, /* The table from which rows are deleted */
|
|
|
|
Expr *pWhere /* The WHERE clause */
|
|
|
|
){
|
|
|
|
TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
|
2007-03-31 05:34:44 +04:00
|
|
|
if( pTriggerStep==0 ){
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3ExprDelete(db, pWhere);
|
2007-03-31 05:34:44 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2002-05-15 12:30:12 +04:00
|
|
|
|
2002-05-17 04:05:58 +04:00
|
|
|
pTriggerStep->op = TK_DELETE;
|
|
|
|
pTriggerStep->target = *pTableName;
|
|
|
|
pTriggerStep->pWhere = pWhere;
|
|
|
|
pTriggerStep->orconf = OE_Default;
|
2007-08-16 08:30:38 +04:00
|
|
|
sqlitePersistTriggerStep(db, pTriggerStep);
|
2002-05-15 12:30:12 +04:00
|
|
|
|
2002-05-17 04:05:58 +04:00
|
|
|
return pTriggerStep;
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
|
|
|
|
2002-05-17 04:05:58 +04:00
|
|
|
/*
|
|
|
|
** Recursively delete a Trigger structure
|
|
|
|
*/
|
2008-07-28 23:34:53 +04:00
|
|
|
void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
|
2003-04-21 22:48:45 +04:00
|
|
|
if( pTrigger==0 ) return;
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3DeleteTriggerStep(db, pTrigger->step_list);
|
|
|
|
sqlite3DbFree(db, pTrigger->name);
|
|
|
|
sqlite3DbFree(db, pTrigger->table);
|
|
|
|
sqlite3ExprDelete(db, pTrigger->pWhen);
|
|
|
|
sqlite3IdListDelete(db, pTrigger->pColumns);
|
|
|
|
if( pTrigger->nameToken.dyn ) sqlite3DbFree(db, (char*)pTrigger->nameToken.z);
|
|
|
|
sqlite3DbFree(db, pTrigger);
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-06-29 12:59:35 +04:00
|
|
|
** This function is called to drop a trigger from the database schema.
|
|
|
|
**
|
|
|
|
** This may be called directly from the parser and therefore identifies
|
|
|
|
** the trigger by name. The sqlite3DropTriggerPtr() routine does the
|
|
|
|
** same job as this routine except it takes a pointer to the trigger
|
|
|
|
** instead of the trigger name.
|
|
|
|
**/
|
2006-09-12 03:45:48 +04:00
|
|
|
void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
|
2004-06-16 16:02:43 +04:00
|
|
|
Trigger *pTrigger = 0;
|
2003-03-27 15:51:24 +03:00
|
|
|
int i;
|
|
|
|
const char *zDb;
|
|
|
|
const char *zName;
|
|
|
|
int nName;
|
2004-09-06 21:24:11 +04:00
|
|
|
sqlite3 *db = pParse->db;
|
2003-03-27 15:51:24 +03:00
|
|
|
|
2007-08-16 08:30:38 +04:00
|
|
|
if( db->mallocFailed ) goto drop_trigger_cleanup;
|
2004-06-29 12:59:35 +04:00
|
|
|
if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
|
2004-06-09 16:30:04 +04:00
|
|
|
goto drop_trigger_cleanup;
|
|
|
|
}
|
2004-06-07 11:52:17 +04:00
|
|
|
|
2003-03-27 15:51:24 +03:00
|
|
|
assert( pName->nSrc==1 );
|
|
|
|
zDb = pName->a[0].zDatabase;
|
|
|
|
zName = pName->a[0].zName;
|
|
|
|
nName = strlen(zName);
|
2005-03-29 07:10:59 +04:00
|
|
|
for(i=OMIT_TEMPDB; i<db->nDb; i++){
|
2003-03-27 16:50:00 +03:00
|
|
|
int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
|
2004-05-08 12:23:19 +04:00
|
|
|
if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
|
2006-03-29 04:24:06 +04:00
|
|
|
pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
|
2003-03-27 15:51:24 +03:00
|
|
|
if( pTrigger ) break;
|
|
|
|
}
|
2002-05-17 04:05:58 +04:00
|
|
|
if( !pTrigger ){
|
2006-09-12 03:45:48 +04:00
|
|
|
if( !noErr ){
|
|
|
|
sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
|
|
|
|
}
|
2003-03-27 15:51:24 +03:00
|
|
|
goto drop_trigger_cleanup;
|
|
|
|
}
|
2006-02-24 05:53:49 +03:00
|
|
|
sqlite3DropTriggerPtr(pParse, pTrigger);
|
2003-05-17 23:04:03 +04:00
|
|
|
|
|
|
|
drop_trigger_cleanup:
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3SrcListDelete(db, pName);
|
2003-05-17 23:04:03 +04:00
|
|
|
}
|
|
|
|
|
2004-07-24 21:38:29 +04:00
|
|
|
/*
|
|
|
|
** Return a pointer to the Table structure for the table that a trigger
|
|
|
|
** is set on.
|
|
|
|
*/
|
2006-02-24 05:53:49 +03:00
|
|
|
static Table *tableOfTrigger(Trigger *pTrigger){
|
2006-01-06 18:03:48 +03:00
|
|
|
int n = strlen(pTrigger->table) + 1;
|
|
|
|
return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
|
2004-07-24 21:38:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-05-17 23:04:03 +04:00
|
|
|
/*
|
2006-02-24 05:53:49 +03:00
|
|
|
** Drop a trigger given a pointer to that trigger.
|
2003-05-17 23:04:03 +04:00
|
|
|
*/
|
2006-02-24 05:53:49 +03:00
|
|
|
void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
|
2003-05-17 23:04:03 +04:00
|
|
|
Table *pTable;
|
|
|
|
Vdbe *v;
|
2004-09-06 21:24:11 +04:00
|
|
|
sqlite3 *db = pParse->db;
|
2004-07-24 21:38:29 +04:00
|
|
|
int iDb;
|
2003-05-17 23:04:03 +04:00
|
|
|
|
2006-01-05 14:34:32 +03:00
|
|
|
iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
|
2004-07-24 21:38:29 +04:00
|
|
|
assert( iDb>=0 && iDb<db->nDb );
|
2006-02-24 05:53:49 +03:00
|
|
|
pTable = tableOfTrigger(pTrigger);
|
2006-03-06 23:55:46 +03:00
|
|
|
assert( pTable );
|
2006-01-05 14:34:32 +03:00
|
|
|
assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
|
2003-01-14 02:27:31 +03:00
|
|
|
#ifndef SQLITE_OMIT_AUTHORIZATION
|
|
|
|
{
|
|
|
|
int code = SQLITE_DROP_TRIGGER;
|
2004-07-24 21:38:29 +04:00
|
|
|
const char *zDb = db->aDb[iDb].zName;
|
|
|
|
const char *zTab = SCHEMA_TABLE(iDb);
|
|
|
|
if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
|
2004-05-08 12:23:19 +04:00
|
|
|
if( sqlite3AuthCheck(pParse, code, pTrigger->name, pTable->zName, zDb) ||
|
|
|
|
sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
|
2003-05-17 23:04:03 +04:00
|
|
|
return;
|
2003-01-14 02:27:31 +03:00
|
|
|
}
|
2003-01-12 21:02:16 +03:00
|
|
|
}
|
2003-01-14 02:27:31 +03:00
|
|
|
#endif
|
2002-05-15 12:30:12 +04:00
|
|
|
|
2002-06-25 05:09:11 +04:00
|
|
|
/* Generate code to destroy the database record of the trigger.
|
|
|
|
*/
|
2006-03-06 23:55:46 +03:00
|
|
|
assert( pTable!=0 );
|
|
|
|
if( (v = sqlite3GetVdbe(pParse))!=0 ){
|
2002-05-15 12:30:12 +04:00
|
|
|
int base;
|
2004-10-06 19:41:16 +04:00
|
|
|
static const VdbeOpList dropTrigger[] = {
|
2003-08-16 16:37:51 +04:00
|
|
|
{ OP_Rewind, 0, ADDR(9), 0},
|
2008-01-17 05:36:28 +03:00
|
|
|
{ OP_String8, 0, 1, 0}, /* 1 */
|
|
|
|
{ OP_Column, 0, 1, 2},
|
|
|
|
{ OP_Ne, 2, ADDR(8), 1},
|
|
|
|
{ OP_String8, 0, 1, 0}, /* 4: "trigger" */
|
|
|
|
{ OP_Column, 0, 0, 2},
|
|
|
|
{ OP_Ne, 2, ADDR(8), 1},
|
2002-05-15 12:30:12 +04:00
|
|
|
{ OP_Delete, 0, 0, 0},
|
2003-08-16 16:37:51 +04:00
|
|
|
{ OP_Next, 0, ADDR(1), 0}, /* 8 */
|
2002-05-15 12:30:12 +04:00
|
|
|
};
|
|
|
|
|
2004-07-24 21:38:29 +04:00
|
|
|
sqlite3BeginWriteOperation(pParse, 0, iDb);
|
2006-01-07 16:21:04 +03:00
|
|
|
sqlite3OpenMasterTable(pParse, iDb);
|
2004-05-08 12:23:19 +04:00
|
|
|
base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger);
|
2008-01-03 03:01:23 +03:00
|
|
|
sqlite3VdbeChangeP4(v, base+1, pTrigger->name, 0);
|
2008-01-03 04:28:59 +03:00
|
|
|
sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
|
2008-01-17 19:22:13 +03:00
|
|
|
sqlite3ChangeCookie(pParse, iDb);
|
2008-01-03 03:01:23 +03:00
|
|
|
sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
|
|
|
|
sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->name, 0);
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
2004-07-24 21:38:29 +04:00
|
|
|
}
|
2002-05-15 12:30:12 +04:00
|
|
|
|
2004-07-24 21:38:29 +04:00
|
|
|
/*
|
|
|
|
** Remove a trigger from the hash tables of the sqlite* pointer.
|
|
|
|
*/
|
|
|
|
void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
|
|
|
|
Trigger *pTrigger;
|
|
|
|
int nName = strlen(zName);
|
2006-03-29 04:24:06 +04:00
|
|
|
pTrigger = sqlite3HashInsert(&(db->aDb[iDb].pSchema->trigHash),
|
|
|
|
zName, nName, 0);
|
2004-07-24 21:38:29 +04:00
|
|
|
if( pTrigger ){
|
2006-02-24 05:53:49 +03:00
|
|
|
Table *pTable = tableOfTrigger(pTrigger);
|
2004-07-24 21:38:29 +04:00
|
|
|
assert( pTable!=0 );
|
2003-04-21 22:48:45 +04:00
|
|
|
if( pTable->pTrigger == pTrigger ){
|
|
|
|
pTable->pTrigger = pTrigger->pNext;
|
|
|
|
}else{
|
|
|
|
Trigger *cc = pTable->pTrigger;
|
|
|
|
while( cc ){
|
|
|
|
if( cc->pNext == pTrigger ){
|
|
|
|
cc->pNext = cc->pNext->pNext;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cc = cc->pNext;
|
|
|
|
}
|
|
|
|
assert(cc);
|
|
|
|
}
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3DeleteTrigger(db, pTrigger);
|
2004-07-24 21:38:29 +04:00
|
|
|
db->flags |= SQLITE_InternChanges;
|
2003-04-21 22:48:45 +04:00
|
|
|
}
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
|
|
|
|
2002-05-21 15:38:11 +04:00
|
|
|
/*
|
|
|
|
** pEList is the SET clause of an UPDATE statement. Each entry
|
|
|
|
** in pEList is of the format <id>=<expr>. If any of the entries
|
|
|
|
** in pEList have an <id> which matches an identifier in pIdList,
|
|
|
|
** then return TRUE. If pIdList==NULL, then it is considered a
|
|
|
|
** wildcard that matches anything. Likewise if pEList==NULL then
|
|
|
|
** it matches anything so always return true. Return false only
|
|
|
|
** if there is no match.
|
|
|
|
*/
|
|
|
|
static int checkColumnOverLap(IdList *pIdList, ExprList *pEList){
|
2002-05-25 00:31:36 +04:00
|
|
|
int e;
|
|
|
|
if( !pIdList || !pEList ) return 1;
|
|
|
|
for(e=0; e<pEList->nExpr; e++){
|
2004-05-08 12:23:19 +04:00
|
|
|
if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
|
2002-05-20 03:43:12 +04:00
|
|
|
}
|
2002-05-15 12:30:12 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-12-07 17:06:13 +03:00
|
|
|
** Return a bit vector to indicate what kind of triggers exist for operation
|
|
|
|
** "op" on table pTab. If pChanges is not NULL then it is a list of columns
|
|
|
|
** that are being updated. Triggers only match if the ON clause of the
|
|
|
|
** trigger definition overlaps the set of columns being updated.
|
|
|
|
**
|
|
|
|
** The returned bit vector is some combination of TRIGGER_BEFORE and
|
|
|
|
** TRIGGER_AFTER.
|
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
int sqlite3TriggersExist(
|
2004-12-07 17:06:13 +03:00
|
|
|
Table *pTab, /* The table the contains the triggers */
|
2002-05-17 04:05:58 +04:00
|
|
|
int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
|
2002-05-21 15:38:11 +04:00
|
|
|
ExprList *pChanges /* Columns that change in an UPDATE statement */
|
|
|
|
){
|
2006-06-14 23:00:20 +04:00
|
|
|
Trigger *pTrigger;
|
2004-12-07 17:06:13 +03:00
|
|
|
int mask = 0;
|
|
|
|
|
2006-06-14 23:00:20 +04:00
|
|
|
pTrigger = IsVirtual(pTab) ? 0 : pTab->pTrigger;
|
2004-12-07 17:06:13 +03:00
|
|
|
while( pTrigger ){
|
|
|
|
if( pTrigger->op==op && checkColumnOverLap(pTrigger->pColumns, pChanges) ){
|
2006-03-25 18:52:19 +03:00
|
|
|
mask |= pTrigger->tr_tm;
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
2004-12-07 17:06:13 +03:00
|
|
|
pTrigger = pTrigger->pNext;
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
2004-12-07 17:06:13 +03:00
|
|
|
return mask;
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
|
|
|
|
2003-05-31 20:21:12 +04:00
|
|
|
/*
|
|
|
|
** Convert the pStep->target token into a SrcList and return a pointer
|
|
|
|
** to that SrcList.
|
|
|
|
**
|
|
|
|
** This routine adds a specific database name, if needed, to the target when
|
|
|
|
** forming the SrcList. This prevents a trigger in one database from
|
|
|
|
** referring to a target in another database. An exception is when the
|
|
|
|
** trigger is in TEMP in which case it can refer to any other database it
|
|
|
|
** wants.
|
|
|
|
*/
|
|
|
|
static SrcList *targetSrcList(
|
|
|
|
Parse *pParse, /* The parsing context */
|
|
|
|
TriggerStep *pStep /* The trigger containing the target token */
|
|
|
|
){
|
|
|
|
Token sDb; /* Dummy database name token */
|
|
|
|
int iDb; /* Index of the database to use */
|
|
|
|
SrcList *pSrc; /* SrcList to be returned */
|
|
|
|
|
2006-01-05 14:34:32 +03:00
|
|
|
iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
|
2003-05-31 20:21:12 +04:00
|
|
|
if( iDb==0 || iDb>=2 ){
|
|
|
|
assert( iDb<pParse->db->nDb );
|
2005-12-09 23:02:05 +03:00
|
|
|
sDb.z = (u8*)pParse->db->aDb[iDb].zName;
|
|
|
|
sDb.n = strlen((char*)sDb.z);
|
2007-08-16 08:30:38 +04:00
|
|
|
pSrc = sqlite3SrcListAppend(pParse->db, 0, &sDb, &pStep->target);
|
2003-05-31 20:21:12 +04:00
|
|
|
} else {
|
2007-08-16 08:30:38 +04:00
|
|
|
pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
|
2003-05-31 20:21:12 +04:00
|
|
|
}
|
|
|
|
return pSrc;
|
|
|
|
}
|
|
|
|
|
2002-05-21 15:38:11 +04:00
|
|
|
/*
|
|
|
|
** Generate VDBE code for zero or more statements inside the body of a
|
|
|
|
** trigger.
|
|
|
|
*/
|
2002-05-15 12:30:12 +04:00
|
|
|
static int codeTriggerProgram(
|
2002-05-21 15:38:11 +04:00
|
|
|
Parse *pParse, /* The parser context */
|
|
|
|
TriggerStep *pStepList, /* List of statements inside the trigger body */
|
|
|
|
int orconfin /* Conflict algorithm. (OE_Abort, etc) */
|
2002-05-17 04:05:58 +04:00
|
|
|
){
|
|
|
|
TriggerStep * pTriggerStep = pStepList;
|
|
|
|
int orconf;
|
2004-09-19 04:50:20 +04:00
|
|
|
Vdbe *v = pParse->pVdbe;
|
2007-08-16 08:30:38 +04:00
|
|
|
sqlite3 *db = pParse->db;
|
2002-05-17 04:05:58 +04:00
|
|
|
|
2004-09-19 04:50:20 +04:00
|
|
|
assert( pTriggerStep!=0 );
|
|
|
|
assert( v!=0 );
|
2008-01-03 03:01:23 +03:00
|
|
|
sqlite3VdbeAddOp2(v, OP_ContextPush, 0, 0);
|
2008-01-02 03:34:36 +03:00
|
|
|
VdbeComment((v, "begin trigger %s", pStepList->pTrig->name));
|
2002-05-17 04:05:58 +04:00
|
|
|
while( pTriggerStep ){
|
|
|
|
orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin;
|
|
|
|
pParse->trigStack->orconf = orconf;
|
|
|
|
switch( pTriggerStep->op ){
|
|
|
|
case TK_SELECT: {
|
2007-08-16 08:30:38 +04:00
|
|
|
Select *ss = sqlite3SelectDup(db, pTriggerStep->pSelect);
|
2006-10-19 03:26:38 +04:00
|
|
|
if( ss ){
|
2008-01-06 03:25:21 +03:00
|
|
|
SelectDest dest;
|
|
|
|
|
|
|
|
sqlite3SelectDestInit(&dest, SRT_Discard, 0);
|
2008-08-20 20:35:10 +04:00
|
|
|
sqlite3Select(pParse, ss, &dest);
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3SelectDelete(db, ss);
|
2006-10-19 03:26:38 +04:00
|
|
|
}
|
2007-08-07 21:13:03 +04:00
|
|
|
break;
|
2002-05-17 04:05:58 +04:00
|
|
|
}
|
|
|
|
case TK_UPDATE: {
|
2003-03-20 04:16:58 +03:00
|
|
|
SrcList *pSrc;
|
2003-05-31 20:21:12 +04:00
|
|
|
pSrc = targetSrcList(pParse, pTriggerStep);
|
2008-01-03 03:01:23 +03:00
|
|
|
sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3Update(pParse, pSrc,
|
2007-08-16 08:30:38 +04:00
|
|
|
sqlite3ExprListDup(db, pTriggerStep->pExprList),
|
|
|
|
sqlite3ExprDup(db, pTriggerStep->pWhere), orconf);
|
2008-01-03 03:01:23 +03:00
|
|
|
sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
|
2002-05-17 04:05:58 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TK_INSERT: {
|
2003-03-20 04:16:58 +03:00
|
|
|
SrcList *pSrc;
|
2003-05-31 20:21:12 +04:00
|
|
|
pSrc = targetSrcList(pParse, pTriggerStep);
|
2008-01-03 03:01:23 +03:00
|
|
|
sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3Insert(pParse, pSrc,
|
2007-08-16 08:30:38 +04:00
|
|
|
sqlite3ExprListDup(db, pTriggerStep->pExprList),
|
|
|
|
sqlite3SelectDup(db, pTriggerStep->pSelect),
|
|
|
|
sqlite3IdListDup(db, pTriggerStep->pIdList), orconf);
|
2008-01-03 03:01:23 +03:00
|
|
|
sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
|
2002-05-17 04:05:58 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TK_DELETE: {
|
2003-03-20 04:16:58 +03:00
|
|
|
SrcList *pSrc;
|
2008-01-03 03:01:23 +03:00
|
|
|
sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0);
|
2003-05-31 20:21:12 +04:00
|
|
|
pSrc = targetSrcList(pParse, pTriggerStep);
|
2007-08-16 08:30:38 +04:00
|
|
|
sqlite3DeleteFrom(pParse, pSrc,
|
|
|
|
sqlite3ExprDup(db, pTriggerStep->pWhere));
|
2008-01-03 03:01:23 +03:00
|
|
|
sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0);
|
2002-05-17 04:05:58 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
pTriggerStep = pTriggerStep->pNext;
|
|
|
|
}
|
2008-01-03 03:01:23 +03:00
|
|
|
sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
|
2008-01-02 03:34:36 +03:00
|
|
|
VdbeComment((v, "end trigger %s", pStepList->pTrig->name));
|
2002-05-15 12:30:12 +04:00
|
|
|
|
2002-05-17 04:05:58 +04:00
|
|
|
return 0;
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
|
|
|
|
2002-05-17 04:05:58 +04:00
|
|
|
/*
|
|
|
|
** This is called to code FOR EACH ROW triggers.
|
|
|
|
**
|
|
|
|
** When the code that this function generates is executed, the following
|
|
|
|
** must be true:
|
2002-05-21 15:38:11 +04:00
|
|
|
**
|
|
|
|
** 1. No cursors may be open in the main database. (But newIdx and oldIdx
|
|
|
|
** can be indices of cursors in temporary tables. See below.)
|
|
|
|
**
|
2002-05-17 04:05:58 +04:00
|
|
|
** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then
|
|
|
|
** a temporary vdbe cursor (index newIdx) must be open and pointing at
|
|
|
|
** a row containing values to be substituted for new.* expressions in the
|
|
|
|
** trigger program(s).
|
2002-05-21 15:38:11 +04:00
|
|
|
**
|
2002-05-17 04:05:58 +04:00
|
|
|
** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then
|
|
|
|
** a temporary vdbe cursor (index oldIdx) must be open and pointing at
|
|
|
|
** a row containing values to be substituted for old.* expressions in the
|
|
|
|
** trigger program(s).
|
|
|
|
**
|
2008-01-01 22:02:09 +03:00
|
|
|
** If they are not NULL, the piOldColMask and piNewColMask output variables
|
|
|
|
** are set to values that describe the columns used by the trigger program
|
|
|
|
** in the OLD.* and NEW.* tables respectively. If column N of the
|
|
|
|
** pseudo-table is read at least once, the corresponding bit of the output
|
|
|
|
** mask is set. If a column with an index greater than 32 is read, the
|
|
|
|
** output mask is set to the special value 0xffffffff.
|
|
|
|
**
|
2002-05-17 04:05:58 +04:00
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
int sqlite3CodeRowTrigger(
|
2002-05-17 04:05:58 +04:00
|
|
|
Parse *pParse, /* Parse context */
|
|
|
|
int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
|
|
|
|
ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
|
2004-12-07 17:06:13 +03:00
|
|
|
int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
|
2002-05-17 04:05:58 +04:00
|
|
|
Table *pTab, /* The table to code triggers from */
|
|
|
|
int newIdx, /* The indice of the "new" row to access */
|
|
|
|
int oldIdx, /* The indice of the "old" row to access */
|
2002-06-11 06:25:40 +04:00
|
|
|
int orconf, /* ON CONFLICT policy */
|
2008-01-01 22:02:09 +03:00
|
|
|
int ignoreJump, /* Instruction to jump to for RAISE(IGNORE) */
|
|
|
|
u32 *piOldColMask, /* OUT: Mask of columns used from the OLD.* table */
|
|
|
|
u32 *piNewColMask /* OUT: Mask of columns used from the NEW.* table */
|
2002-05-21 15:38:11 +04:00
|
|
|
){
|
2006-01-10 15:31:39 +03:00
|
|
|
Trigger *p;
|
2008-01-13 22:02:11 +03:00
|
|
|
sqlite3 *db = pParse->db;
|
2004-09-24 16:24:36 +04:00
|
|
|
TriggerStack trigStackEntry;
|
2002-05-15 12:30:12 +04:00
|
|
|
|
2008-01-01 22:02:09 +03:00
|
|
|
trigStackEntry.oldColMask = 0;
|
|
|
|
trigStackEntry.newColMask = 0;
|
|
|
|
|
2002-05-15 12:30:12 +04:00
|
|
|
assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE);
|
2004-12-07 17:06:13 +03:00
|
|
|
assert(tr_tm == TRIGGER_BEFORE || tr_tm == TRIGGER_AFTER );
|
2002-05-15 12:30:12 +04:00
|
|
|
|
2002-05-17 04:05:58 +04:00
|
|
|
assert(newIdx != -1 || oldIdx != -1);
|
2002-05-15 12:30:12 +04:00
|
|
|
|
2006-01-10 15:31:39 +03:00
|
|
|
for(p=pTab->pTrigger; p; p=p->pNext){
|
2002-05-15 12:30:12 +04:00
|
|
|
int fire_this = 0;
|
|
|
|
|
2006-01-10 15:31:39 +03:00
|
|
|
/* Determine whether we should code this trigger */
|
|
|
|
if(
|
|
|
|
p->op==op &&
|
|
|
|
p->tr_tm==tr_tm &&
|
2007-11-16 17:55:46 +03:00
|
|
|
(p->pSchema==p->pTabSchema || p->pSchema==db->aDb[1].pSchema) &&
|
2006-01-10 15:31:39 +03:00
|
|
|
(op!=TK_UPDATE||!p->pColumns||checkColumnOverLap(p->pColumns,pChanges))
|
|
|
|
){
|
|
|
|
TriggerStack *pS; /* Pointer to trigger-stack entry */
|
2006-02-24 05:53:49 +03:00
|
|
|
for(pS=pParse->trigStack; pS && p!=pS->pTrigger; pS=pS->pNext){}
|
2006-01-10 15:31:39 +03:00
|
|
|
if( !pS ){
|
|
|
|
fire_this = 1;
|
2002-05-20 03:43:12 +04:00
|
|
|
}
|
2006-03-25 18:52:19 +03:00
|
|
|
#if 0 /* Give no warning for recursive triggers. Just do not do them */
|
|
|
|
else{
|
|
|
|
sqlite3ErrorMsg(pParse, "recursive triggers not supported (%s)",
|
|
|
|
p->name);
|
|
|
|
return SQLITE_ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
2004-09-19 06:15:24 +04:00
|
|
|
|
2004-09-24 16:24:36 +04:00
|
|
|
if( fire_this ){
|
2002-05-15 12:30:12 +04:00
|
|
|
int endTrigger;
|
|
|
|
Expr * whenExpr;
|
2003-04-25 21:52:11 +04:00
|
|
|
AuthContext sContext;
|
2005-01-29 11:32:43 +03:00
|
|
|
NameContext sNC;
|
2002-05-15 12:30:12 +04:00
|
|
|
|
2008-01-13 00:35:57 +03:00
|
|
|
#ifndef SQLITE_OMIT_TRACE
|
|
|
|
sqlite3VdbeAddOp4(pParse->pVdbe, OP_Trace, 0, 0, 0,
|
2008-01-13 22:02:11 +03:00
|
|
|
sqlite3MPrintf(db, "-- TRIGGER %s", p->name),
|
2008-01-13 00:35:57 +03:00
|
|
|
P4_DYNAMIC);
|
|
|
|
#endif
|
2005-01-29 11:32:43 +03:00
|
|
|
memset(&sNC, 0, sizeof(sNC));
|
|
|
|
sNC.pParse = pParse;
|
2002-05-15 12:30:12 +04:00
|
|
|
|
|
|
|
/* Push an entry on to the trigger stack */
|
2006-01-10 15:31:39 +03:00
|
|
|
trigStackEntry.pTrigger = p;
|
2004-09-24 16:24:36 +04:00
|
|
|
trigStackEntry.newIdx = newIdx;
|
|
|
|
trigStackEntry.oldIdx = oldIdx;
|
|
|
|
trigStackEntry.pTab = pTab;
|
|
|
|
trigStackEntry.pNext = pParse->trigStack;
|
|
|
|
trigStackEntry.ignoreJump = ignoreJump;
|
|
|
|
pParse->trigStack = &trigStackEntry;
|
2006-01-10 15:31:39 +03:00
|
|
|
sqlite3AuthContextPush(pParse, &sContext, p->name);
|
2002-05-15 12:30:12 +04:00
|
|
|
|
|
|
|
/* code the WHEN clause */
|
2004-05-08 12:23:19 +04:00
|
|
|
endTrigger = sqlite3VdbeMakeLabel(pParse->pVdbe);
|
2007-11-16 17:55:46 +03:00
|
|
|
whenExpr = sqlite3ExprDup(db, p->pWhen);
|
2008-08-20 20:35:10 +04:00
|
|
|
if( db->mallocFailed || sqlite3ResolveExprNames(&sNC, whenExpr) ){
|
2004-09-24 16:24:36 +04:00
|
|
|
pParse->trigStack = trigStackEntry.pNext;
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3ExprDelete(db, whenExpr);
|
2002-05-15 15:44:13 +04:00
|
|
|
return 1;
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
2008-01-09 02:54:25 +03:00
|
|
|
sqlite3ExprIfFalse(pParse, whenExpr, endTrigger, SQLITE_JUMPIFNULL);
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3ExprDelete(db, whenExpr);
|
2002-05-15 12:30:12 +04:00
|
|
|
|
2006-01-10 15:31:39 +03:00
|
|
|
codeTriggerProgram(pParse, p->step_list, orconf);
|
2002-05-15 12:30:12 +04:00
|
|
|
|
|
|
|
/* Pop the entry off the trigger stack */
|
2004-09-24 16:24:36 +04:00
|
|
|
pParse->trigStack = trigStackEntry.pNext;
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3AuthContextPop(&sContext);
|
2002-05-15 12:30:12 +04:00
|
|
|
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3VdbeResolveLabel(pParse->pVdbe, endTrigger);
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
|
|
|
}
|
2008-01-01 22:02:09 +03:00
|
|
|
if( piOldColMask ) *piOldColMask |= trigStackEntry.oldColMask;
|
|
|
|
if( piNewColMask ) *piNewColMask |= trigStackEntry.newColMask;
|
2002-05-15 12:30:12 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2004-10-31 05:22:47 +03:00
|
|
|
#endif /* !defined(SQLITE_OMIT_TRIGGER) */
|