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.
|
|
|
|
**
|
|
|
|
*************************************************************************
|
|
|
|
*
|
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
|
|
|
|
2002-08-24 22:24:51 +04:00
|
|
|
/*
|
|
|
|
** Delete a linked list of TriggerStep structures.
|
|
|
|
*/
|
2003-04-21 22:48:45 +04:00
|
|
|
void sqliteDeleteTriggerStep(TriggerStep *pTriggerStep){
|
2002-08-24 22:24:51 +04:00
|
|
|
while( pTriggerStep ){
|
|
|
|
TriggerStep * pTmp = pTriggerStep;
|
|
|
|
pTriggerStep = pTriggerStep->pNext;
|
|
|
|
|
2002-08-25 23:20:40 +04:00
|
|
|
if( pTmp->target.dyn ) sqliteFree((char*)pTmp->target.z);
|
2002-08-24 22:24:51 +04:00
|
|
|
sqliteExprDelete(pTmp->pWhere);
|
|
|
|
sqliteExprListDelete(pTmp->pExprList);
|
|
|
|
sqliteSelectDelete(pTmp->pSelect);
|
|
|
|
sqliteIdListDelete(pTmp->pIdList);
|
|
|
|
|
|
|
|
sqliteFree(pTmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
** sqliteFinishTrigger() function is called to complete the trigger
|
|
|
|
** construction process.
|
2002-05-15 15:44:13 +04:00
|
|
|
*/
|
2003-04-21 22:48:45 +04:00
|
|
|
void sqliteBeginTrigger(
|
2002-05-15 15:44:13 +04:00
|
|
|
Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
|
2002-05-17 04:05:58 +04:00
|
|
|
Token *pName, /* 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
|
|
|
int foreach, /* One of TK_ROW or TK_STATEMENT */
|
|
|
|
Expr *pWhen, /* WHEN clause */
|
2003-04-21 22:48:45 +04:00
|
|
|
int isTemp /* True if the TEMPORARY keyword is present */
|
2002-05-15 15:44:13 +04:00
|
|
|
){
|
|
|
|
Trigger *nt;
|
|
|
|
Table *tab;
|
2003-04-21 22:48:45 +04:00
|
|
|
char *zName = 0; /* Name of the trigger */
|
2003-03-27 15:51:24 +03:00
|
|
|
sqlite *db = pParse->db;
|
2003-04-21 22:48:45 +04:00
|
|
|
int iDb; /* When database to store the trigger in */
|
2003-01-12 21:02:16 +03:00
|
|
|
|
2002-05-15 12:30:12 +04:00
|
|
|
/* Check that:
|
2002-05-15 15:44:13 +04:00
|
|
|
** 1. the trigger name does not already exist.
|
2003-04-21 22:48:45 +04:00
|
|
|
** 2. the table (or view) does exist in the same database as the trigger.
|
2002-05-27 03:24:40 +04:00
|
|
|
** 3. that we are not trying to create a trigger on the sqlite_master table
|
|
|
|
** 4. That we are not trying to create an INSTEAD OF trigger on a table.
|
|
|
|
** 5. That we are not trying to create a BEFORE or AFTER trigger on a view.
|
2002-05-15 15:44:13 +04:00
|
|
|
*/
|
2003-03-27 15:51:24 +03:00
|
|
|
if( sqlite_malloc_failed ) goto trigger_cleanup;
|
|
|
|
assert( pTableName->nSrc==1 );
|
2003-03-27 16:50:00 +03:00
|
|
|
tab = sqliteSrcListLookup(pParse, pTableName);
|
2003-03-27 15:51:24 +03:00
|
|
|
if( !tab ){
|
|
|
|
goto trigger_cleanup;
|
|
|
|
}
|
2003-04-21 22:48:45 +04:00
|
|
|
iDb = isTemp ? 1 : tab->iDb;
|
|
|
|
if( iDb>=2 && !pParse->initFlag ){
|
2003-03-31 06:12:46 +04:00
|
|
|
sqliteErrorMsg(pParse, "triggers may not be added to auxiliary "
|
|
|
|
"database %s", db->aDb[tab->iDb].zName);
|
2003-03-27 15:51:24 +03:00
|
|
|
goto trigger_cleanup;
|
|
|
|
}
|
|
|
|
|
2003-01-14 02:27:31 +03:00
|
|
|
zName = sqliteStrNDup(pName->z, pName->n);
|
2003-04-21 22:48:45 +04:00
|
|
|
if( sqliteHashFind(&(db->aDb[iDb].trigHash), zName,pName->n+1) ){
|
2003-03-31 06:12:46 +04:00
|
|
|
sqliteErrorMsg(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
|
|
|
}
|
2003-03-27 15:51:24 +03:00
|
|
|
if( sqliteStrNICmp(tab->zName, "sqlite_", 7)==0 ){
|
2003-03-31 06:12:46 +04:00
|
|
|
sqliteErrorMsg(pParse, "cannot create trigger on system table");
|
2003-03-27 15:51:24 +03:00
|
|
|
pParse->nErr++;
|
|
|
|
goto trigger_cleanup;
|
|
|
|
}
|
|
|
|
if( tab->pSelect && tr_tm != TK_INSTEAD ){
|
2003-03-31 06:12:46 +04:00
|
|
|
sqliteErrorMsg(pParse, "cannot create %s trigger on view: %S",
|
|
|
|
(tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
|
2003-03-27 15:51:24 +03:00
|
|
|
goto trigger_cleanup;
|
|
|
|
}
|
|
|
|
if( !tab->pSelect && tr_tm == TK_INSTEAD ){
|
2003-03-31 06:12:46 +04:00
|
|
|
sqliteErrorMsg(pParse, "cannot create INSTEAD OF"
|
|
|
|
" trigger on table: %S", pTableName, 0);
|
2003-03-27 15:51:24 +03:00
|
|
|
goto trigger_cleanup;
|
|
|
|
}
|
|
|
|
#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;
|
2003-04-23 00:30:37 +04:00
|
|
|
const char *zDb = db->aDb[tab->iDb].zName;
|
|
|
|
const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
|
2003-04-21 22:48:45 +04:00
|
|
|
if( tab->iDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
|
2003-04-23 00:30:37 +04:00
|
|
|
if( sqliteAuthCheck(pParse, code, zName, tab->zName, zDbTrig) ){
|
2002-05-23 04:30:31 +04:00
|
|
|
goto trigger_cleanup;
|
|
|
|
}
|
2003-04-23 00:30:37 +04:00
|
|
|
if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(tab->iDb), 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
|
|
|
|
2003-04-24 05:45:04 +04:00
|
|
|
/* INSTEAD OF triggers can only appear on views and BEGIN triggers
|
|
|
|
** 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 */
|
2002-05-15 15:44:13 +04:00
|
|
|
nt = (Trigger*)sqliteMalloc(sizeof(Trigger));
|
2002-05-23 06:09:03 +04:00
|
|
|
if( nt==0 ) goto trigger_cleanup;
|
2003-01-14 02:27:31 +03:00
|
|
|
nt->name = zName;
|
|
|
|
zName = 0;
|
2003-03-27 15:51:24 +03:00
|
|
|
nt->table = sqliteStrDup(pTableName->a[0].zName);
|
2002-05-23 06:09:03 +04:00
|
|
|
if( sqlite_malloc_failed ) goto trigger_cleanup;
|
2003-04-21 22:48:45 +04:00
|
|
|
nt->iDb = iDb;
|
2002-05-15 12:30:12 +04:00
|
|
|
nt->op = op;
|
|
|
|
nt->tr_tm = tr_tm;
|
2002-08-24 22:24:51 +04:00
|
|
|
nt->pWhen = sqliteExprDup(pWhen);
|
|
|
|
nt->pColumns = sqliteIdListDup(pColumns);
|
2002-05-15 12:30:12 +04:00
|
|
|
nt->foreach = foreach;
|
2003-04-21 22:48:45 +04:00
|
|
|
assert( pParse->pNewTrigger==0 );
|
|
|
|
pParse->pNewTrigger = nt;
|
|
|
|
|
|
|
|
trigger_cleanup:
|
|
|
|
sqliteFree(zName);
|
|
|
|
sqliteSrcListDelete(pTableName);
|
|
|
|
sqliteIdListDelete(pColumns);
|
|
|
|
sqliteExprDelete(pWhen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This routine is called after all of the trigger actions have been parsed
|
|
|
|
** in order to complete the process of building the trigger.
|
|
|
|
*/
|
|
|
|
void sqliteFinishTrigger(
|
|
|
|
Parse *pParse, /* Parser context */
|
|
|
|
TriggerStep *pStepList, /* The triggered program */
|
|
|
|
Token *pAll /* Token that describes the complete CREATE TRIGGER */
|
|
|
|
){
|
|
|
|
Trigger *nt; /* The trigger whose construction is finishing up */
|
|
|
|
sqlite *db = pParse->db; /* The database */
|
|
|
|
|
|
|
|
if( pParse->nErr || pParse->pNewTrigger==0 ) goto triggerfinish_cleanup;
|
|
|
|
nt = pParse->pNewTrigger;
|
|
|
|
pParse->pNewTrigger = 0;
|
2002-05-17 04:05:58 +04:00
|
|
|
nt->step_list = pStepList;
|
2003-04-18 02:57:53 +04:00
|
|
|
while( pStepList ){
|
|
|
|
pStepList->pTrig = nt;
|
|
|
|
pStepList = pStepList->pNext;
|
|
|
|
}
|
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
|
|
|
|
*/
|
2002-06-25 05:09:11 +04:00
|
|
|
if( !pParse->initFlag ){
|
2002-05-21 15:38:11 +04:00
|
|
|
static VdbeOp insertTrig[] = {
|
|
|
|
{ OP_NewRecno, 0, 0, 0 },
|
|
|
|
{ OP_String, 0, 0, "trigger" },
|
2002-06-25 05:09:11 +04:00
|
|
|
{ OP_String, 0, 0, 0 }, /* 2: trigger name */
|
|
|
|
{ OP_String, 0, 0, 0 }, /* 3: table name */
|
2002-05-21 15:38:11 +04:00
|
|
|
{ OP_Integer, 0, 0, 0 },
|
2002-06-25 05:09:11 +04:00
|
|
|
{ OP_String, 0, 0, 0 }, /* 5: SQL */
|
2002-05-21 15:38:11 +04:00
|
|
|
{ OP_MakeRecord, 5, 0, 0 },
|
|
|
|
{ OP_PutIntKey, 0, 0, 0 },
|
|
|
|
};
|
|
|
|
int addr;
|
|
|
|
Vdbe *v;
|
2002-05-15 12:30:12 +04:00
|
|
|
|
|
|
|
/* Make an entry in the sqlite_master table */
|
2002-05-21 15:38:11 +04:00
|
|
|
v = sqliteGetVdbe(pParse);
|
2003-04-21 22:48:45 +04:00
|
|
|
if( v==0 ) goto triggerfinish_cleanup;
|
2002-09-14 17:47:32 +04:00
|
|
|
sqliteBeginWriteOperation(pParse, 0, 0);
|
2003-04-21 22:48:45 +04:00
|
|
|
sqliteOpenMasterTable(v, nt->iDb==1);
|
2002-05-21 15:38:11 +04:00
|
|
|
addr = sqliteVdbeAddOpList(v, ArraySize(insertTrig), insertTrig);
|
2002-06-25 05:09:11 +04:00
|
|
|
sqliteVdbeChangeP3(v, addr+2, nt->name, 0);
|
|
|
|
sqliteVdbeChangeP3(v, addr+3, nt->table, 0);
|
2002-08-24 22:24:51 +04:00
|
|
|
sqliteVdbeChangeP3(v, addr+5, pAll->z, pAll->n);
|
2003-04-21 22:48:45 +04:00
|
|
|
if( nt->iDb==0 ){
|
2003-03-27 15:51:24 +03:00
|
|
|
sqliteChangeCookie(db, v);
|
2002-06-25 05:09:11 +04:00
|
|
|
}
|
|
|
|
sqliteVdbeAddOp(v, OP_Close, 0, 0);
|
2002-05-15 12:30:12 +04:00
|
|
|
sqliteEndWriteOperation(pParse);
|
|
|
|
}
|
|
|
|
|
2002-05-17 04:05:58 +04:00
|
|
|
if( !pParse->explain ){
|
2003-04-21 22:48:45 +04:00
|
|
|
Table *pTab;
|
|
|
|
sqliteHashInsert(&db->aDb[nt->iDb].trigHash,
|
|
|
|
nt->name, strlen(nt->name)+1, nt);
|
|
|
|
pTab = sqliteLocateTable(pParse, nt->table, 0);
|
|
|
|
assert( pTab!=0 );
|
|
|
|
nt->pNext = pTab->pTrigger;
|
|
|
|
pTab->pTrigger = nt;
|
2002-05-20 03:43:12 +04:00
|
|
|
}else{
|
2003-04-21 22:48:45 +04:00
|
|
|
sqliteDeleteTrigger(nt);
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
|
|
|
|
2003-04-21 22:48:45 +04:00
|
|
|
triggerfinish_cleanup:
|
|
|
|
sqliteDeleteTrigger(pParse->pNewTrigger);
|
|
|
|
pParse->pNewTrigger = 0;
|
2002-08-24 22:24:51 +04:00
|
|
|
sqliteDeleteTriggerStep(pStepList);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** 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
|
|
|
|
** from sqliteMalloc(). As initially created, the Expr.token.z values
|
|
|
|
** all point to the input string that was fed to the parser. But that
|
|
|
|
** string is ephemeral - it will go away as soon as the sqlite_exec()
|
|
|
|
** call that started the parser exits. This routine makes a persistent
|
|
|
|
** copy of all the Expr.token.z strings so that the TriggerStep structure
|
|
|
|
** will be valid even after the sqlite_exec() call returns.
|
|
|
|
*/
|
|
|
|
static void sqlitePersistTriggerStep(TriggerStep *p){
|
|
|
|
if( p->target.z ){
|
|
|
|
p->target.z = sqliteStrNDup(p->target.z, p->target.n);
|
|
|
|
p->target.dyn = 1;
|
|
|
|
}
|
|
|
|
if( p->pSelect ){
|
|
|
|
Select *pNew = sqliteSelectDup(p->pSelect);
|
|
|
|
sqliteSelectDelete(p->pSelect);
|
|
|
|
p->pSelect = pNew;
|
|
|
|
}
|
|
|
|
if( p->pWhere ){
|
|
|
|
Expr *pNew = sqliteExprDup(p->pWhere);
|
|
|
|
sqliteExprDelete(p->pWhere);
|
|
|
|
p->pWhere = pNew;
|
|
|
|
}
|
|
|
|
if( p->pExprList ){
|
|
|
|
ExprList *pNew = sqliteExprListDup(p->pExprList);
|
|
|
|
sqliteExprListDelete(p->pExprList);
|
|
|
|
p->pExprList = pNew;
|
|
|
|
}
|
|
|
|
if( p->pIdList ){
|
|
|
|
IdList *pNew = sqliteIdListDup(p->pIdList);
|
|
|
|
sqliteIdListDelete(p->pIdList);
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
TriggerStep *sqliteTriggerSelectStep(Select *pSelect){
|
2002-05-17 04:05:58 +04:00
|
|
|
TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
|
2002-05-23 06:09:03 +04:00
|
|
|
if( pTriggerStep==0 ) 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;
|
2002-08-24 22:24:51 +04:00
|
|
|
sqlitePersistTriggerStep(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.
|
|
|
|
*/
|
2002-05-17 04:05:58 +04:00
|
|
|
TriggerStep *sqliteTriggerInsertStep(
|
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
|
|
|
){
|
|
|
|
TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
|
2002-05-23 06:09:03 +04:00
|
|
|
if( pTriggerStep==0 ) return 0;
|
2002-05-15 12:30:12 +04:00
|
|
|
|
2002-05-17 04:05:58 +04:00
|
|
|
assert(pEList == 0 || pSelect == 0);
|
|
|
|
assert(pEList != 0 || pSelect != 0);
|
2002-05-15 12:30:12 +04:00
|
|
|
|
2002-05-17 04:05:58 +04:00
|
|
|
pTriggerStep->op = TK_INSERT;
|
|
|
|
pTriggerStep->pSelect = pSelect;
|
|
|
|
pTriggerStep->target = *pTableName;
|
|
|
|
pTriggerStep->pIdList = pColumn;
|
|
|
|
pTriggerStep->pExprList = pEList;
|
|
|
|
pTriggerStep->orconf = orconf;
|
2002-08-24 22:24:51 +04:00
|
|
|
sqlitePersistTriggerStep(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 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.
|
|
|
|
*/
|
2002-05-17 04:05:58 +04:00
|
|
|
TriggerStep *sqliteTriggerUpdateStep(
|
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) */
|
|
|
|
){
|
2002-05-17 04:05:58 +04:00
|
|
|
TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
|
2002-05-23 06:09:03 +04:00
|
|
|
if( pTriggerStep==0 ) 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;
|
2002-08-24 22:24:51 +04:00
|
|
|
sqlitePersistTriggerStep(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.
|
|
|
|
*/
|
|
|
|
TriggerStep *sqliteTriggerDeleteStep(Token *pTableName, Expr *pWhere){
|
2002-05-23 06:09:03 +04:00
|
|
|
TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
|
|
|
|
if( pTriggerStep==0 ) 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;
|
2002-08-24 22:24:51 +04:00
|
|
|
sqlitePersistTriggerStep(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
|
|
|
|
*/
|
2002-05-21 17:18:25 +04:00
|
|
|
void sqliteDeleteTrigger(Trigger *pTrigger){
|
2003-04-21 22:48:45 +04:00
|
|
|
if( pTrigger==0 ) return;
|
2002-08-24 22:24:51 +04:00
|
|
|
sqliteDeleteTriggerStep(pTrigger->step_list);
|
2002-05-17 04:05:58 +04:00
|
|
|
sqliteFree(pTrigger->name);
|
|
|
|
sqliteFree(pTrigger->table);
|
|
|
|
sqliteExprDelete(pTrigger->pWhen);
|
|
|
|
sqliteIdListDelete(pTrigger->pColumns);
|
|
|
|
sqliteFree(pTrigger);
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-05-17 04:05:58 +04:00
|
|
|
* This function is called to drop a trigger from the database schema.
|
|
|
|
*
|
|
|
|
* This may be called directly from the parser, or from within
|
|
|
|
* sqliteDropTable(). In the latter case the "nested" argument is true.
|
|
|
|
*
|
|
|
|
* Note that this function does not delete the trigger entirely. Instead it
|
|
|
|
* removes it from the internal schema and places it in the trigDrop hash
|
|
|
|
* table. This is so that the trigger can be restored into the database schema
|
|
|
|
* if the transaction is rolled back.
|
2002-05-15 12:30:12 +04:00
|
|
|
*/
|
2003-03-27 15:51:24 +03:00
|
|
|
void sqliteDropTrigger(Parse *pParse, SrcList *pName, int nested){
|
2002-05-17 04:05:58 +04:00
|
|
|
Trigger *pTrigger;
|
|
|
|
Table *pTable;
|
2002-06-25 05:09:11 +04:00
|
|
|
Vdbe *v;
|
2003-03-27 15:51:24 +03:00
|
|
|
int i;
|
|
|
|
const char *zDb;
|
|
|
|
const char *zName;
|
|
|
|
int nName;
|
|
|
|
sqlite *db = pParse->db;
|
|
|
|
|
|
|
|
if( sqlite_malloc_failed ) goto drop_trigger_cleanup;
|
|
|
|
assert( pName->nSrc==1 );
|
|
|
|
zDb = pName->a[0].zDatabase;
|
|
|
|
zName = pName->a[0].zName;
|
|
|
|
nName = strlen(zName);
|
|
|
|
for(i=0; i<db->nDb; i++){
|
2003-03-27 16:50:00 +03:00
|
|
|
int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
|
|
|
|
if( zDb && sqliteStrICmp(db->aDb[j].zName, zDb) ) continue;
|
|
|
|
pTrigger = sqliteHashFind(&(db->aDb[j].trigHash), zName, nName+1);
|
2003-03-27 15:51:24 +03:00
|
|
|
if( pTrigger ) break;
|
|
|
|
}
|
2002-05-17 04:05:58 +04:00
|
|
|
if( !pTrigger ){
|
2003-03-31 06:12:46 +04:00
|
|
|
sqliteErrorMsg(pParse, "no such trigger: %S", pName, 0);
|
2003-03-27 15:51:24 +03:00
|
|
|
goto drop_trigger_cleanup;
|
|
|
|
}
|
2003-04-21 22:48:45 +04:00
|
|
|
assert( pTrigger->iDb<db->nDb );
|
2003-03-27 15:51:24 +03:00
|
|
|
if( pTrigger->iDb>=2 ){
|
2003-03-31 06:12:46 +04:00
|
|
|
sqliteErrorMsg(pParse, "triggers may not be removed from "
|
|
|
|
"auxiliary database %s", db->aDb[pTrigger->iDb].zName);
|
2003-03-27 15:51:24 +03:00
|
|
|
goto drop_trigger_cleanup;
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
2003-03-27 15:51:24 +03:00
|
|
|
pTable = sqliteFindTable(db, pTrigger->table, db->aDb[pTrigger->iDb].zName);
|
2003-01-12 21:02:16 +03:00
|
|
|
assert(pTable);
|
2003-04-21 22:48:45 +04:00
|
|
|
assert( pTable->iDb==pTrigger->iDb || pTrigger->iDb==1 );
|
2003-01-14 02:27:31 +03:00
|
|
|
#ifndef SQLITE_OMIT_AUTHORIZATION
|
|
|
|
{
|
|
|
|
int code = SQLITE_DROP_TRIGGER;
|
2003-04-23 00:30:37 +04:00
|
|
|
const char *zDb = db->aDb[pTrigger->iDb].zName;
|
|
|
|
const char *zTab = SCHEMA_TABLE(pTrigger->iDb);
|
2003-04-21 22:48:45 +04:00
|
|
|
if( pTrigger->iDb ) code = SQLITE_DROP_TEMP_TRIGGER;
|
2003-04-23 00:30:37 +04:00
|
|
|
if( sqliteAuthCheck(pParse, code, pTrigger->name, pTable->zName, zDb) ||
|
|
|
|
sqliteAuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
|
2003-03-30 04:19:49 +04:00
|
|
|
goto drop_trigger_cleanup;
|
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.
|
|
|
|
*/
|
|
|
|
if( pTable!=0 && !nested && (v = sqliteGetVdbe(pParse))!=0 ){
|
2002-05-15 12:30:12 +04:00
|
|
|
int base;
|
|
|
|
static VdbeOp dropTrigger[] = {
|
2002-06-25 05:09:11 +04:00
|
|
|
{ OP_Rewind, 0, ADDR(8), 0},
|
|
|
|
{ OP_String, 0, 0, 0}, /* 1 */
|
2002-05-15 12:30:12 +04:00
|
|
|
{ OP_MemStore, 1, 1, 0},
|
2002-06-25 05:09:11 +04:00
|
|
|
{ OP_MemLoad, 1, 0, 0}, /* 3 */
|
2002-05-15 12:30:12 +04:00
|
|
|
{ OP_Column, 0, 1, 0},
|
2002-06-25 05:09:11 +04:00
|
|
|
{ OP_Ne, 0, ADDR(7), 0},
|
2002-05-15 12:30:12 +04:00
|
|
|
{ OP_Delete, 0, 0, 0},
|
2002-06-25 05:09:11 +04:00
|
|
|
{ OP_Next, 0, ADDR(3), 0}, /* 7 */
|
2002-05-15 12:30:12 +04:00
|
|
|
};
|
|
|
|
|
2002-09-14 17:47:32 +04:00
|
|
|
sqliteBeginWriteOperation(pParse, 0, 0);
|
2003-04-21 22:48:45 +04:00
|
|
|
sqliteOpenMasterTable(v, pTrigger->iDb);
|
2002-06-25 05:09:11 +04:00
|
|
|
base = sqliteVdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger);
|
|
|
|
sqliteVdbeChangeP3(v, base+1, zName, 0);
|
2003-04-21 22:48:45 +04:00
|
|
|
if( pTrigger->iDb==0 ){
|
2003-03-27 15:51:24 +03:00
|
|
|
sqliteChangeCookie(db, v);
|
2002-05-15 16:45:43 +04:00
|
|
|
}
|
2002-06-25 05:09:11 +04:00
|
|
|
sqliteVdbeAddOp(v, OP_Close, 0, 0);
|
|
|
|
sqliteEndWriteOperation(pParse);
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
|
|
|
|
2003-04-21 22:48:45 +04:00
|
|
|
/*
|
|
|
|
* If this is not an "explain", then delete the trigger structure.
|
|
|
|
*/
|
|
|
|
if( !pParse->explain ){
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
sqliteHashInsert(&(db->aDb[pTrigger->iDb].trigHash), zName, nName+1, 0);
|
|
|
|
sqliteDeleteTrigger(pTrigger);
|
|
|
|
}
|
|
|
|
|
2003-03-27 15:51:24 +03:00
|
|
|
drop_trigger_cleanup:
|
|
|
|
sqliteSrcListDelete(pName);
|
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++){
|
|
|
|
if( sqliteIdListIndex(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* A global variable that is TRUE if we should always set up temp tables for
|
|
|
|
* for triggers, even if there are no triggers to code. This is used to test
|
|
|
|
* how much overhead the triggers algorithm is causing.
|
|
|
|
*
|
|
|
|
* This flag can be set or cleared using the "trigger_overhead_test" pragma.
|
|
|
|
* The pragma is not documented since it is not really part of the interface
|
|
|
|
* to SQLite, just the test procedure.
|
|
|
|
*/
|
|
|
|
int always_code_trigger_setup = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true if a trigger matching op, tr_tm and foreach that is NOT already
|
|
|
|
* on the Parse objects trigger-stack (to prevent recursive trigger firing) is
|
|
|
|
* found in the list specified as pTrigger.
|
|
|
|
*/
|
|
|
|
int sqliteTriggersExist(
|
2002-05-21 15:38:11 +04:00
|
|
|
Parse *pParse, /* Used to check for recursive triggers */
|
|
|
|
Trigger *pTrigger, /* A list of triggers associated with a table */
|
2002-05-17 04:05:58 +04:00
|
|
|
int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
|
|
|
|
int tr_tm, /* one of TK_BEFORE, TK_AFTER */
|
|
|
|
int foreach, /* one of TK_ROW or TK_STATEMENT */
|
2002-05-21 15:38:11 +04:00
|
|
|
ExprList *pChanges /* Columns that change in an UPDATE statement */
|
|
|
|
){
|
2002-05-17 04:05:58 +04:00
|
|
|
Trigger * pTriggerCursor;
|
2002-05-15 12:30:12 +04:00
|
|
|
|
2002-05-17 04:05:58 +04:00
|
|
|
if( always_code_trigger_setup ){
|
|
|
|
return 1;
|
|
|
|
}
|
2002-05-15 12:30:12 +04:00
|
|
|
|
2002-05-17 04:05:58 +04:00
|
|
|
pTriggerCursor = pTrigger;
|
|
|
|
while( pTriggerCursor ){
|
|
|
|
if( pTriggerCursor->op == op &&
|
|
|
|
pTriggerCursor->tr_tm == tr_tm &&
|
|
|
|
pTriggerCursor->foreach == foreach &&
|
|
|
|
checkColumnOverLap(pTriggerCursor->pColumns, pChanges) ){
|
2002-05-15 12:30:12 +04:00
|
|
|
TriggerStack * ss;
|
|
|
|
ss = pParse->trigStack;
|
2002-05-20 03:43:12 +04:00
|
|
|
while( ss && ss->pTrigger != pTrigger ){
|
|
|
|
ss = ss->pNext;
|
|
|
|
}
|
|
|
|
if( !ss )return 1;
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
2002-05-17 04:05:58 +04:00
|
|
|
pTriggerCursor = pTriggerCursor->pNext;
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
while( pTriggerStep ){
|
|
|
|
int saveNTab = pParse->nTab;
|
2003-04-18 02:57:53 +04:00
|
|
|
int saveUseDb = pParse->useDb;
|
2002-05-17 04:05:58 +04:00
|
|
|
orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin;
|
|
|
|
pParse->trigStack->orconf = orconf;
|
2003-04-18 02:57:53 +04:00
|
|
|
pParse->useDb = pTriggerStep->pTrig->iDb;
|
2003-04-21 22:48:45 +04:00
|
|
|
if( pParse->useDb==1 ) pParse->useDb = -1;
|
2002-05-17 04:05:58 +04:00
|
|
|
switch( pTriggerStep->op ){
|
|
|
|
case TK_SELECT: {
|
2002-06-11 06:25:40 +04:00
|
|
|
Select * ss = sqliteSelectDup(pTriggerStep->pSelect);
|
|
|
|
assert(ss);
|
|
|
|
assert(ss->pSrc);
|
|
|
|
sqliteSelect(pParse, ss, SRT_Discard, 0, 0, 0, 0);
|
|
|
|
sqliteSelectDelete(ss);
|
2002-05-17 04:05:58 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TK_UPDATE: {
|
2003-03-20 04:16:58 +03:00
|
|
|
SrcList *pSrc;
|
|
|
|
pSrc = sqliteSrcListAppend(0, &pTriggerStep->target, 0);
|
2002-05-24 02:07:02 +04:00
|
|
|
sqliteVdbeAddOp(pParse->pVdbe, OP_ListPush, 0, 0);
|
2003-03-20 04:16:58 +03:00
|
|
|
sqliteUpdate(pParse, pSrc,
|
2002-06-11 06:25:40 +04:00
|
|
|
sqliteExprListDup(pTriggerStep->pExprList),
|
|
|
|
sqliteExprDup(pTriggerStep->pWhere), orconf);
|
2002-05-24 02:07:02 +04:00
|
|
|
sqliteVdbeAddOp(pParse->pVdbe, OP_ListPop, 0, 0);
|
2002-05-17 04:05:58 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TK_INSERT: {
|
2003-03-20 04:16:58 +03:00
|
|
|
SrcList *pSrc;
|
|
|
|
pSrc = sqliteSrcListAppend(0, &pTriggerStep->target, 0);
|
|
|
|
sqliteInsert(pParse, pSrc,
|
|
|
|
sqliteExprListDup(pTriggerStep->pExprList),
|
|
|
|
sqliteSelectDup(pTriggerStep->pSelect),
|
|
|
|
sqliteIdListDup(pTriggerStep->pIdList), orconf);
|
2002-05-17 04:05:58 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TK_DELETE: {
|
2003-03-20 04:16:58 +03:00
|
|
|
SrcList *pSrc;
|
2002-05-24 02:07:02 +04:00
|
|
|
sqliteVdbeAddOp(pParse->pVdbe, OP_ListPush, 0, 0);
|
2003-03-20 04:16:58 +03:00
|
|
|
pSrc = sqliteSrcListAppend(0, &pTriggerStep->target, 0);
|
|
|
|
sqliteDeleteFrom(pParse, pSrc, sqliteExprDup(pTriggerStep->pWhere));
|
2002-05-24 02:07:02 +04:00
|
|
|
sqliteVdbeAddOp(pParse->pVdbe, OP_ListPop, 0, 0);
|
2002-05-17 04:05:58 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
pParse->nTab = saveNTab;
|
2003-04-18 02:57:53 +04:00
|
|
|
pParse->useDb = saveUseDb;
|
2002-05-17 04:05:58 +04:00
|
|
|
pTriggerStep = pTriggerStep->pNext;
|
|
|
|
}
|
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).
|
|
|
|
**
|
|
|
|
*/
|
2002-05-15 12:30:12 +04:00
|
|
|
int sqliteCodeRowTrigger(
|
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 */
|
|
|
|
int tr_tm, /* One of TK_BEFORE, TK_AFTER */
|
|
|
|
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 */
|
|
|
|
int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
|
2002-05-21 15:38:11 +04:00
|
|
|
){
|
2002-05-15 12:30:12 +04:00
|
|
|
Trigger * pTrigger;
|
|
|
|
TriggerStack * pTriggerStack;
|
|
|
|
|
|
|
|
assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE);
|
2003-04-24 05:45:04 +04:00
|
|
|
assert(tr_tm == TK_BEFORE || tr_tm == TK_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
|
|
|
|
2002-05-17 04:05:58 +04:00
|
|
|
pTrigger = pTab->pTrigger;
|
2002-05-20 03:43:12 +04:00
|
|
|
while( pTrigger ){
|
2002-05-15 12:30:12 +04:00
|
|
|
int fire_this = 0;
|
|
|
|
|
|
|
|
/* determine whether we should code this trigger */
|
2002-05-20 03:43:12 +04:00
|
|
|
if( pTrigger->op == op && pTrigger->tr_tm == tr_tm &&
|
|
|
|
pTrigger->foreach == TK_ROW ){
|
2002-05-15 12:30:12 +04:00
|
|
|
fire_this = 1;
|
|
|
|
pTriggerStack = pParse->trigStack;
|
2002-05-20 03:43:12 +04:00
|
|
|
while( pTriggerStack ){
|
|
|
|
if( pTriggerStack->pTrigger == pTrigger ){
|
|
|
|
fire_this = 0;
|
|
|
|
}
|
2002-05-15 15:44:13 +04:00
|
|
|
pTriggerStack = pTriggerStack->pNext;
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
2002-05-20 03:43:12 +04:00
|
|
|
if( op == TK_UPDATE && pTrigger->pColumns &&
|
|
|
|
!checkColumnOverLap(pTrigger->pColumns, pChanges) ){
|
2002-05-15 15:44:13 +04:00
|
|
|
fire_this = 0;
|
2002-05-20 03:43:12 +04:00
|
|
|
}
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
|
|
|
|
2002-05-23 06:09:03 +04:00
|
|
|
if( fire_this && (pTriggerStack = sqliteMalloc(sizeof(TriggerStack)))!=0 ){
|
2002-05-15 12:30:12 +04:00
|
|
|
int endTrigger;
|
2002-05-24 06:04:32 +04:00
|
|
|
SrcList dummyTablist;
|
2002-05-15 12:30:12 +04:00
|
|
|
Expr * whenExpr;
|
2003-04-25 21:52:11 +04:00
|
|
|
AuthContext sContext;
|
2002-05-15 12:30:12 +04:00
|
|
|
|
2002-05-24 06:04:32 +04:00
|
|
|
dummyTablist.nSrc = 0;
|
2002-05-15 12:30:12 +04:00
|
|
|
|
|
|
|
/* Push an entry on to the trigger stack */
|
|
|
|
pTriggerStack->pTrigger = pTrigger;
|
2002-05-17 04:05:58 +04:00
|
|
|
pTriggerStack->newIdx = newIdx;
|
|
|
|
pTriggerStack->oldIdx = oldIdx;
|
|
|
|
pTriggerStack->pTab = pTab;
|
2002-05-15 12:30:12 +04:00
|
|
|
pTriggerStack->pNext = pParse->trigStack;
|
2002-06-11 06:25:40 +04:00
|
|
|
pTriggerStack->ignoreJump = ignoreJump;
|
2002-05-15 12:30:12 +04:00
|
|
|
pParse->trigStack = pTriggerStack;
|
2003-04-25 21:52:11 +04:00
|
|
|
sqliteAuthContextPush(pParse, &sContext, pTrigger->name);
|
2002-05-15 12:30:12 +04:00
|
|
|
|
|
|
|
/* code the WHEN clause */
|
|
|
|
endTrigger = sqliteVdbeMakeLabel(pParse->pVdbe);
|
|
|
|
whenExpr = sqliteExprDup(pTrigger->pWhen);
|
2003-05-02 18:32:12 +04:00
|
|
|
if( sqliteExprResolveIds(pParse, &dummyTablist, 0, whenExpr) ){
|
2002-05-15 15:44:13 +04:00
|
|
|
pParse->trigStack = pParse->trigStack->pNext;
|
|
|
|
sqliteFree(pTriggerStack);
|
|
|
|
sqliteExprDelete(whenExpr);
|
|
|
|
return 1;
|
2002-05-15 12:30:12 +04:00
|
|
|
}
|
2002-05-27 00:54:33 +04:00
|
|
|
sqliteExprIfFalse(pParse, whenExpr, endTrigger, 1);
|
2002-05-15 12:30:12 +04:00
|
|
|
sqliteExprDelete(whenExpr);
|
|
|
|
|
2002-05-17 04:05:58 +04:00
|
|
|
codeTriggerProgram(pParse, pTrigger->step_list, orconf);
|
2002-05-15 12:30:12 +04:00
|
|
|
|
|
|
|
/* Pop the entry off the trigger stack */
|
|
|
|
pParse->trigStack = pParse->trigStack->pNext;
|
2003-04-25 21:52:11 +04:00
|
|
|
sqliteAuthContextPop(&sContext);
|
2002-05-15 12:30:12 +04:00
|
|
|
sqliteFree(pTriggerStack);
|
|
|
|
|
|
|
|
sqliteVdbeResolveLabel(pParse->pVdbe, endTrigger);
|
|
|
|
}
|
|
|
|
pTrigger = pTrigger->pNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|