#include "sqliteInt.h"
#ifndef SQLITE_OMIT_TRIGGER
void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
while( pTriggerStep ){
TriggerStep * pTmp = pTriggerStep;
pTriggerStep = pTriggerStep->pNext;
sqlite3ExprDelete(db, pTmp->pWhere);
sqlite3ExprListDelete(db, pTmp->pExprList);
sqlite3SelectDelete(db, pTmp->pSelect);
sqlite3IdListDelete(db, pTmp->pIdList);
sqlite3UpsertDelete(db, pTmp->pUpsert);
sqlite3SrcListDelete(db, pTmp->pFrom);
sqlite3DbFree(db, pTmp->zSpan);
sqlite3DbFree(db, pTmp);
}
}
Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
Schema *pTmpSchema;
Trigger *pList;
HashElem *p;
assert( pParse->disableTriggers==0 );
pTmpSchema = pParse->db->aDb[1].pSchema;
p = sqliteHashFirst(&pTmpSchema->trigHash);
pList = pTab->pTrigger;
while( p ){
Trigger *pTrig = (Trigger *)sqliteHashData(p);
if( pTrig->pTabSchema==pTab->pSchema
&& pTrig->table
&& 0==sqlite3StrICmp(pTrig->table, pTab->zName)
&& (pTrig->pTabSchema!=pTmpSchema || pTrig->bReturning)
){
pTrig->pNext = pList;
pList = pTrig;
}else if( pTrig->op==TK_RETURNING ){
#ifndef SQLITE_OMIT_VIRTUALTABLE
assert( pParse->db->pVtabCtx==0 );
#endif
assert( pParse->bReturning );
assert( &(pParse->u1.pReturning->retTrig) == pTrig );
pTrig->table = pTab->zName;
pTrig->pTabSchema = pTab->pSchema;
pTrig->pNext = pList;
pList = pTrig;
}
p = sqliteHashNext(p);
}
#if 0#endif
return pList;
}
void sqlite3BeginTrigger(
Parse *pParse,
Token *pName1,
Token *pName2,
int tr_tm,
int op,
IdList *pColumns,
SrcList *pTableName,
Expr *pWhen,
int isTemp,
int noErr
){
Trigger *pTrigger = 0;
Table *pTab;
char *zName = 0;
sqlite3 *db = pParse->db;
int iDb;
Token *pName;
DbFixer sFix;
assert( pName1!=0 );
assert( pName2!=0 );
assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
assert( op>0 && op<0xff );
if( isTemp ){
if( pName2->n>0 ){
sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
goto trigger_cleanup;
}
iDb = 1;
pName = pName1;
}else{
iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
if( iDb<0 ){
goto trigger_cleanup;
}
}
if( !pTableName || db->mallocFailed ){
goto trigger_cleanup;
}
if( db->init.busy && iDb!=1 ){
sqlite3DbFree(db, pTableName->a[0].zDatabase);
pTableName->a[0].zDatabase = 0;
}
pTab = sqlite3SrcListLookup(pParse, pTableName);
if( db->init.busy==0 && pName2->n==0 && pTab
&& pTab->pSchema==db->aDb[1].pSchema ){
iDb = 1;
}
if( db->mallocFailed ) goto trigger_cleanup;
assert( pTableName->nSrc==1 );
sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
if( sqlite3FixSrcList(&sFix, pTableName) ){
goto trigger_cleanup;
}
pTab = sqlite3SrcListLookup(pParse, pTableName);
if( !pTab ){
goto trigger_orphan_error;
}
if( IsVirtual(pTab) ){
sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
goto trigger_orphan_error;
}
zName = sqlite3NameFromToken(db, pName);
if( zName==0 ){
assert( db->mallocFailed );
goto trigger_cleanup;
}
if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
goto trigger_cleanup;
}
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
if( !IN_RENAME_OBJECT ){
if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
if( !noErr ){
sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
}else{
assert( !db->init.busy );
sqlite3CodeVerifySchema(pParse, iDb);
VVA_ONLY( pParse->ifNotExists = 1; )
}
goto trigger_cleanup;
}
}
if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
goto trigger_cleanup;
}
if( IsView(pTab) && tr_tm!=TK_INSTEAD ){
sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
(tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName->a);
goto trigger_orphan_error;
}
if( !IsView(pTab) && tr_tm==TK_INSTEAD ){
sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
" trigger on table: %S", pTableName->a);
goto trigger_orphan_error;
}
#ifndef SQLITE_OMIT_AUTHORIZATION
if( !IN_RENAME_OBJECT ){
int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
int code = SQLITE_CREATE_TRIGGER;
const char *zDb = db->aDb[iTabDb].zDbSName;
const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
goto trigger_cleanup;
}
if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
goto trigger_cleanup;
}
}
#endif
if (tr_tm == TK_INSTEAD){
tr_tm = TK_BEFORE;
}
pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
if( pTrigger==0 ) goto trigger_cleanup;
pTrigger->zName = zName;
zName = 0;
pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
pTrigger->pSchema = db->aDb[iDb].pSchema;
pTrigger->pTabSchema = pTab->pSchema;
pTrigger->op = (u8)op;
pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
if( IN_RENAME_OBJECT ){
sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
pTrigger->pWhen = pWhen;
pWhen = 0;
}else{
pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
}
pTrigger->pColumns = pColumns;
pColumns = 0;
assert( pParse->pNewTrigger==0 );
pParse->pNewTrigger = pTrigger;
trigger_cleanup:
sqlite3DbFree(db, zName);
sqlite3SrcListDelete(db, pTableName);
sqlite3IdListDelete(db, pColumns);
sqlite3ExprDelete(db, pWhen);
if( !pParse->pNewTrigger ){
sqlite3DeleteTrigger(db, pTrigger);
}else{
assert( pParse->pNewTrigger==pTrigger );
}
return;
trigger_orphan_error:
if( db->init.iDb==1 ){
db->init.orphanTrigger = 1;
}
goto trigger_cleanup;
}
void sqlite3FinishTrigger(
Parse *pParse,
TriggerStep *pStepList,
Token *pAll
){
Trigger *pTrig = pParse->pNewTrigger;
char *zName;
sqlite3 *db = pParse->db;
DbFixer sFix;
int iDb;
Token nameToken;
pParse->pNewTrigger = 0;
if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
zName = pTrig->zName;
iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
pTrig->step_list = pStepList;
while( pStepList ){
pStepList->pTrig = pTrig;
pStepList = pStepList->pNext;
}
sqlite3TokenInit(&nameToken, pTrig->zName);
sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
|| sqlite3FixExpr(&sFix, pTrig->pWhen)
){
goto triggerfinish_cleanup;
}
#ifndef SQLITE_OMIT_ALTERTABLE
if( IN_RENAME_OBJECT ){
assert( !db->init.busy );
pParse->pNewTrigger = pTrig;
pTrig = 0;
}else
#endif
if( !db->init.busy ){
Vdbe *v;
char *z;
if( sqlite3ReadOnlyShadowTables(db) ){
TriggerStep *pStep;
for(pStep=pTrig->step_list; pStep; pStep=pStep->pNext){
if( pStep->zTarget!=0
&& sqlite3ShadowTableName(db, pStep->zTarget)
){
sqlite3ErrorMsg(pParse,
"trigger \"%s\" may not write to shadow table \"%s\"",
pTrig->zName, pStep->zTarget);
goto triggerfinish_cleanup;
}
}
}
v = sqlite3GetVdbe(pParse);
if( v==0 ) goto triggerfinish_cleanup;
sqlite3BeginWriteOperation(pParse, 0, iDb);
z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
testcase( z==0 );
sqlite3NestedParse(pParse,
"INSERT INTO %Q." LEGACY_SCHEMA_TABLE
" VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
db->aDb[iDb].zDbSName, zName,
pTrig->table, z);
sqlite3DbFree(db, z);
sqlite3ChangeCookie(pParse, iDb);
sqlite3VdbeAddParseSchemaOp(v, iDb,
sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName), 0);
}
if( db->init.busy ){
Trigger *pLink = pTrig;
Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
assert( pLink!=0 );
pTrig = sqlite3HashInsert(pHash, zName, pTrig);
if( pTrig ){
sqlite3OomFault(db);
}else if( pLink->pSchema==pLink->pTabSchema ){
Table *pTab;
pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
assert( pTab!=0 );
pLink->pNext = pTab->pTrigger;
pTab->pTrigger = pLink;
}
}
triggerfinish_cleanup:
sqlite3DeleteTrigger(db, pTrig);
assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
sqlite3DeleteTriggerStep(db, pStepList);
}
static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
char *z = sqlite3DbSpanDup(db, zStart, zEnd);
int i;
if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i]) ) z[i] = ' ';
return z;
}
TriggerStep *sqlite3TriggerSelectStep(
sqlite3 *db,
Select *pSelect,
const char *zStart,
const char *zEnd
){
TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
if( pTriggerStep==0 ) {
sqlite3SelectDelete(db, pSelect);
return 0;
}
pTriggerStep->op = TK_SELECT;
pTriggerStep->pSelect = pSelect;
pTriggerStep->orconf = OE_Default;
pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
return pTriggerStep;
}
static TriggerStep *triggerStepAllocate(
Parse *pParse,
u8 op,
Token *pName,
const char *zStart,
const char *zEnd
){
sqlite3 *db = pParse->db;
TriggerStep *pTriggerStep;
if( pParse->nErr ) return 0;
pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
if( pTriggerStep ){
char *z = (char*)&pTriggerStep[1];
memcpy(z, pName->z, pName->n);
sqlite3Dequote(z);
pTriggerStep->zTarget = z;
pTriggerStep->op = op;
pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
if( IN_RENAME_OBJECT ){
sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
}
}
return pTriggerStep;
}
TriggerStep *sqlite3TriggerInsertStep(
Parse *pParse,
Token *pTableName,
IdList *pColumn,
Select *pSelect,
u8 orconf,
Upsert *pUpsert,
const char *zStart,
const char *zEnd
){
sqlite3 *db = pParse->db;
TriggerStep *pTriggerStep;
assert(pSelect != 0 || db->mallocFailed);
pTriggerStep = triggerStepAllocate(pParse, TK_INSERT, pTableName,zStart,zEnd);
if( pTriggerStep ){
if( IN_RENAME_OBJECT ){
pTriggerStep->pSelect = pSelect;
pSelect = 0;
}else{
pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
}
pTriggerStep->pIdList = pColumn;
pTriggerStep->pUpsert = pUpsert;
pTriggerStep->orconf = orconf;
if( pUpsert ){
sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
}
}else{
testcase( pColumn );
sqlite3IdListDelete(db, pColumn);
testcase( pUpsert );
sqlite3UpsertDelete(db, pUpsert);
}
sqlite3SelectDelete(db, pSelect);
return pTriggerStep;
}
TriggerStep *sqlite3TriggerUpdateStep(
Parse *pParse,
Token *pTableName,
SrcList *pFrom,
ExprList *pEList,
Expr *pWhere,
u8 orconf,
const char *zStart,
const char *zEnd
){
sqlite3 *db = pParse->db;
TriggerStep *pTriggerStep;
pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE, pTableName,zStart,zEnd);
if( pTriggerStep ){
if( IN_RENAME_OBJECT ){
pTriggerStep->pExprList = pEList;
pTriggerStep->pWhere = pWhere;
pTriggerStep->pFrom = pFrom;
pEList = 0;
pWhere = 0;
pFrom = 0;
}else{
pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE);
}
pTriggerStep->orconf = orconf;
}
sqlite3ExprListDelete(db, pEList);
sqlite3ExprDelete(db, pWhere);
sqlite3SrcListDelete(db, pFrom);
return pTriggerStep;
}
TriggerStep *sqlite3TriggerDeleteStep(
Parse *pParse,
Token *pTableName,
Expr *pWhere,
const char *zStart,
const char *zEnd
){
sqlite3 *db = pParse->db;
TriggerStep *pTriggerStep;
pTriggerStep = triggerStepAllocate(pParse, TK_DELETE, pTableName,zStart,zEnd);
if( pTriggerStep ){
if( IN_RENAME_OBJECT ){
pTriggerStep->pWhere = pWhere;
pWhere = 0;
}else{
pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
}
pTriggerStep->orconf = OE_Default;
}
sqlite3ExprDelete(db, pWhere);
return pTriggerStep;
}
void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
if( pTrigger==0 || pTrigger->bReturning ) return;
sqlite3DeleteTriggerStep(db, pTrigger->step_list);
sqlite3DbFree(db, pTrigger->zName);
sqlite3DbFree(db, pTrigger->table);
sqlite3ExprDelete(db, pTrigger->pWhen);
sqlite3IdListDelete(db, pTrigger->pColumns);
sqlite3DbFree(db, pTrigger);
}
void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
Trigger *pTrigger = 0;
int i;
const char *zDb;
const char *zName;
sqlite3 *db = pParse->db;
if( db->mallocFailed ) goto drop_trigger_cleanup;
if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
goto drop_trigger_cleanup;
}
assert( pName->nSrc==1 );
zDb = pName->a[0].zDatabase;
zName = pName->a[0].zName;
assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
for(i=OMIT_TEMPDB; i<db->nDb; i++){
int j = (i<2) ? i^1 : i;
if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
assert( sqlite3SchemaMutexHeld(db, j, 0) );
pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
if( pTrigger ) break;
}
if( !pTrigger ){
if( !noErr ){
sqlite3ErrorMsg(pParse, "no such trigger: %S", pName->a);
}else{
sqlite3CodeVerifyNamedSchema(pParse, zDb);
}
pParse->checkSchema = 1;
goto drop_trigger_cleanup;
}
sqlite3DropTriggerPtr(pParse, pTrigger);
drop_trigger_cleanup:
sqlite3SrcListDelete(db, pName);
}
static Table *tableOfTrigger(Trigger *pTrigger){
return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
}
void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
Table *pTable;
Vdbe *v;
sqlite3 *db = pParse->db;
int iDb;
iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
assert( iDb>=0 && iDb<db->nDb );
pTable = tableOfTrigger(pTrigger);
assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
#ifndef SQLITE_OMIT_AUTHORIZATION
if( pTable ){
int code = SQLITE_DROP_TRIGGER;
const char *zDb = db->aDb[iDb].zDbSName;
const char *zTab = SCHEMA_TABLE(iDb);
if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
return;
}
}
#endif
if( (v = sqlite3GetVdbe(pParse))!=0 ){
sqlite3NestedParse(pParse,
"DELETE FROM %Q." LEGACY_SCHEMA_TABLE " WHERE name=%Q AND type='trigger'",
db->aDb[iDb].zDbSName, pTrigger->zName
);
sqlite3ChangeCookie(pParse, iDb);
sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
}
}
void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
Trigger *pTrigger;
Hash *pHash;
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
pHash = &(db->aDb[iDb].pSchema->trigHash);
pTrigger = sqlite3HashInsert(pHash, zName, 0);
if( ALWAYS(pTrigger) ){
if( pTrigger->pSchema==pTrigger->pTabSchema ){
Table *pTab = tableOfTrigger(pTrigger);
if( pTab ){
Trigger **pp;
for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
if( *pp==pTrigger ){
*pp = (*pp)->pNext;
break;
}
}
}
}
sqlite3DeleteTrigger(db, pTrigger);
db->mDbFlags |= DBFLAG_SchemaChange;
}
}
static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
int e;
if( pIdList==0 || NEVER(pEList==0) ) return 1;
for(e=0; e<pEList->nExpr; e++){
if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
}
return 0;
}
static int tempTriggersExist(sqlite3 *db){
if( NEVER(db->aDb[1].pSchema==0) ) return 0;
if( sqliteHashFirst(&db->aDb[1].pSchema->trigHash)==0 ) return 0;
return 1;
}
static SQLITE_NOINLINE Trigger *triggersReallyExist(
Parse *pParse,
Table *pTab,
int op,
ExprList *pChanges,
int *pMask
){
int mask = 0;
Trigger *pList = 0;
Trigger *p;
pList = sqlite3TriggerList(pParse, pTab);
assert( pList==0 || IsVirtual(pTab)==0
|| (pList->bReturning && pList->pNext==0) );
if( pList!=0 ){
p = pList;
if( (pParse->db->flags & SQLITE_EnableTrigger)==0
&& pTab->pTrigger!=0
){
if( pList==pTab->pTrigger ){
pList = 0;
goto exit_triggers_exist;
}
while( ALWAYS(p->pNext) && p->pNext!=pTab->pTrigger ) p = p->pNext;
p->pNext = 0;
p = pList;
}
do{
if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
mask |= p->tr_tm;
}else if( p->op==TK_RETURNING ){
assert( sqlite3IsToplevel(pParse) );
p->op = op;
if( IsVirtual(pTab) ){
if( op!=TK_INSERT ){
sqlite3ErrorMsg(pParse,
"%s RETURNING is not available on virtual tables",
op==TK_DELETE ? "DELETE" : "UPDATE");
}
p->tr_tm = TRIGGER_BEFORE;
}else{
p->tr_tm = TRIGGER_AFTER;
}
mask |= p->tr_tm;
}else if( p->bReturning && p->op==TK_INSERT && op==TK_UPDATE
&& sqlite3IsToplevel(pParse) ){
mask |= p->tr_tm;
}
p = p->pNext;
}while( p );
}
exit_triggers_exist:
if( pMask ){
*pMask = mask;
}
return (mask ? pList : 0);
}
Trigger *sqlite3TriggersExist(
Parse *pParse,
Table *pTab,
int op,
ExprList *pChanges,
int *pMask
){
assert( pTab!=0 );
if( (pTab->pTrigger==0 && !tempTriggersExist(pParse->db))
|| pParse->disableTriggers
){
if( pMask ) *pMask = 0;
return 0;
}
return triggersReallyExist(pParse,pTab,op,pChanges,pMask);
}
SrcList *sqlite3TriggerStepSrc(
Parse *pParse,
TriggerStep *pStep
){
sqlite3 *db = pParse->db;
SrcList *pSrc;
char *zName = sqlite3DbStrDup(db, pStep->zTarget);
pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
assert( pSrc==0 || pSrc->nSrc==1 );
assert( zName || pSrc==0 );
if( pSrc ){
Schema *pSchema = pStep->pTrig->pSchema;
pSrc->a[0].zName = zName;
if( pSchema!=db->aDb[1].pSchema ){
pSrc->a[0].pSchema = pSchema;
}
if( pStep->pFrom ){
SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0);
if( pDup && pDup->nSrc>1 && !IN_RENAME_OBJECT ){
Select *pSubquery;
Token as;
pSubquery = sqlite3SelectNew(pParse,0,pDup,0,0,0,0,SF_NestedFrom,0);
as.n = 0;
as.z = 0;
pDup = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0);
}
pSrc = sqlite3SrcListAppendList(pParse, pSrc, pDup);
}
}else{
sqlite3DbFree(db, zName);
}
return pSrc;
}
static int isAsteriskTerm(
Parse *pParse,
Expr *pTerm
){
assert( pTerm!=0 );
if( pTerm->op==TK_ASTERISK ) return 1;
if( pTerm->op!=TK_DOT ) return 0;
assert( pTerm->pRight!=0 );
assert( pTerm->pLeft!=0 );
if( pTerm->pRight->op!=TK_ASTERISK ) return 0;
sqlite3ErrorMsg(pParse, "RETURNING may not use \"TABLE.*\" wildcards");
return 1;
}
static ExprList *sqlite3ExpandReturning(
Parse *pParse,
ExprList *pList,
Table *pTab
){
ExprList *pNew = 0;
sqlite3 *db = pParse->db;
int i;
for(i=0; i<pList->nExpr; i++){
Expr *pOldExpr = pList->a[i].pExpr;
if( NEVER(pOldExpr==0) ) continue;
if( isAsteriskTerm(pParse, pOldExpr) ){
int jj;
for(jj=0; jj<pTab->nCol; jj++){
Expr *pNewExpr;
if( IsHiddenColumn(pTab->aCol+jj) ) continue;
pNewExpr = sqlite3Expr(db, TK_ID, pTab->aCol[jj].zCnName);
pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr);
if( !db->mallocFailed ){
struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
pItem->zEName = sqlite3DbStrDup(db, pTab->aCol[jj].zCnName);
pItem->fg.eEName = ENAME_NAME;
}
}
}else{
Expr *pNewExpr = sqlite3ExprDup(db, pOldExpr, 0);
pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr);
if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0) ){
struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
pItem->zEName = sqlite3DbStrDup(db, pList->a[i].zEName);
pItem->fg.eEName = pList->a[i].fg.eEName;
}
}
}
return pNew;
}
static void codeReturningTrigger(
Parse *pParse,
Trigger *pTrigger,
Table *pTab,
int regIn
){
Vdbe *v = pParse->pVdbe;
sqlite3 *db = pParse->db;
ExprList *pNew;
Returning *pReturning;
Select sSelect;
SrcList sFrom;
assert( v!=0 );
assert( pParse->bReturning );
assert( db->pParse==pParse );
pReturning = pParse->u1.pReturning;
assert( pTrigger == &(pReturning->retTrig) );
memset(&sSelect, 0, sizeof(sSelect));
memset(&sFrom, 0, sizeof(sFrom));
sSelect.pEList = sqlite3ExprListDup(db, pReturning->pReturnEL, 0);
sSelect.pSrc = &sFrom;
sFrom.nSrc = 1;
sFrom.a[0].pTab = pTab;
sFrom.a[0].iCursor = -1;
sqlite3SelectPrep(pParse, &sSelect, 0);
if( pParse->nErr==0 ){
assert( db->mallocFailed==0 );
sqlite3GenerateColumnNames(pParse, &sSelect);
}
sqlite3ExprListDelete(db, sSelect.pEList);
pNew = sqlite3ExpandReturning(pParse, pReturning->pReturnEL, pTab);
if( pParse->nErr==0 ){
NameContext sNC;
memset(&sNC, 0, sizeof(sNC));
if( pReturning->nRetCol==0 ){
pReturning->nRetCol = pNew->nExpr;
pReturning->iRetCur = pParse->nTab++;
}
sNC.pParse = pParse;
sNC.uNC.iBaseReg = regIn;
sNC.ncFlags = NC_UBaseReg;
pParse->eTriggerOp = pTrigger->op;
pParse->pTriggerTab = pTab;
if( sqlite3ResolveExprListNames(&sNC, pNew)==SQLITE_OK
&& ALWAYS(!db->mallocFailed)
){
int i;
int nCol = pNew->nExpr;
int reg = pParse->nMem+1;
pParse->nMem += nCol+2;
pReturning->iRetReg = reg;
for(i=0; i<nCol; i++){
Expr *pCol = pNew->a[i].pExpr;
assert( pCol!=0 );
sqlite3ExprCodeFactorable(pParse, pCol, reg+i);
if( sqlite3ExprAffinity(pCol)==SQLITE_AFF_REAL ){
sqlite3VdbeAddOp1(v, OP_RealAffinity, reg+i);
}
}
sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, i, reg+i);
sqlite3VdbeAddOp2(v, OP_NewRowid, pReturning->iRetCur, reg+i+1);
sqlite3VdbeAddOp3(v, OP_Insert, pReturning->iRetCur, reg+i, reg+i+1);
}
}
sqlite3ExprListDelete(db, pNew);
pParse->eTriggerOp = 0;
pParse->pTriggerTab = 0;
}
static int codeTriggerProgram(
Parse *pParse,
TriggerStep *pStepList,
int orconf
){
TriggerStep *pStep;
Vdbe *v = pParse->pVdbe;
sqlite3 *db = pParse->db;
assert( pParse->pTriggerTab && pParse->pToplevel );
assert( pStepList );
assert( v!=0 );
for(pStep=pStepList; pStep; pStep=pStep->pNext){
pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
assert( pParse->okConstFactor==0 );
#ifndef SQLITE_OMIT_TRACE
if( pStep->zSpan ){
sqlite3VdbeAddOp4(v, OP_Trace, 0x7fffffff, 1, 0,
sqlite3MPrintf(db, "-- %s", pStep->zSpan),
P4_DYNAMIC);
}
#endif
switch( pStep->op ){
case TK_UPDATE: {
sqlite3Update(pParse,
sqlite3TriggerStepSrc(pParse, pStep),
sqlite3ExprListDup(db, pStep->pExprList, 0),
sqlite3ExprDup(db, pStep->pWhere, 0),
pParse->eOrconf, 0, 0, 0
);
sqlite3VdbeAddOp0(v, OP_ResetCount);
break;
}
case TK_INSERT: {
sqlite3Insert(pParse,
sqlite3TriggerStepSrc(pParse, pStep),
sqlite3SelectDup(db, pStep->pSelect, 0),
sqlite3IdListDup(db, pStep->pIdList),
pParse->eOrconf,
sqlite3UpsertDup(db, pStep->pUpsert)
);
sqlite3VdbeAddOp0(v, OP_ResetCount);
break;
}
case TK_DELETE: {
sqlite3DeleteFrom(pParse,
sqlite3TriggerStepSrc(pParse, pStep),
sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
);
sqlite3VdbeAddOp0(v, OP_ResetCount);
break;
}
default: assert( pStep->op==TK_SELECT ); {
SelectDest sDest;
Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
sqlite3Select(pParse, pSelect, &sDest);
sqlite3SelectDelete(db, pSelect);
break;
}
}
}
return 0;
}
#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
static const char *onErrorText(int onError){
switch( onError ){
case OE_Abort: return "abort";
case OE_Rollback: return "rollback";
case OE_Fail: return "fail";
case OE_Replace: return "replace";
case OE_Ignore: return "ignore";
case OE_Default: return "default";
}
return "n/a";
}
#endif
static void transferParseError(Parse *pTo, Parse *pFrom){
assert( pFrom->zErrMsg==0 || pFrom->nErr );
assert( pTo->zErrMsg==0 || pTo->nErr );
if( pTo->nErr==0 ){
pTo->zErrMsg = pFrom->zErrMsg;
pTo->nErr = pFrom->nErr;
pTo->rc = pFrom->rc;
}else{
sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
}
}
static TriggerPrg *codeRowTrigger(
Parse *pParse,
Trigger *pTrigger,
Table *pTab,
int orconf
){
Parse *pTop = sqlite3ParseToplevel(pParse);
sqlite3 *db = pParse->db;
TriggerPrg *pPrg;
Expr *pWhen = 0;
Vdbe *v;
NameContext sNC;
SubProgram *pProgram = 0;
int iEndTrigger = 0;
Parse sSubParse;
assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
assert( pTop->pVdbe );
pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
if( !pPrg ) return 0;
pPrg->pNext = pTop->pTriggerPrg;
pTop->pTriggerPrg = pPrg;
pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
if( !pProgram ) return 0;
sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
pPrg->pTrigger = pTrigger;
pPrg->orconf = orconf;
pPrg->aColmask[0] = 0xffffffff;
pPrg->aColmask[1] = 0xffffffff;
sqlite3ParseObjectInit(&sSubParse, db);
memset(&sNC, 0, sizeof(sNC));
sNC.pParse = &sSubParse;
sSubParse.pTriggerTab = pTab;
sSubParse.pToplevel = pTop;
sSubParse.zAuthContext = pTrigger->zName;
sSubParse.eTriggerOp = pTrigger->op;
sSubParse.nQueryLoop = pParse->nQueryLoop;
sSubParse.prepFlags = pParse->prepFlags;
v = sqlite3GetVdbe(&sSubParse);
if( v ){
VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
pTrigger->zName, onErrorText(orconf),
(pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
(pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
(pTrigger->op==TK_INSERT ? "INSERT" : ""),
(pTrigger->op==TK_DELETE ? "DELETE" : ""),
pTab->zName
));
#ifndef SQLITE_OMIT_TRACE
if( pTrigger->zName ){
sqlite3VdbeChangeP4(v, -1,
sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
);
}
#endif
if( pTrigger->pWhen ){
pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
if( db->mallocFailed==0
&& SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
){
iEndTrigger = sqlite3VdbeMakeLabel(&sSubParse);
sqlite3ExprIfFalse(&sSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
}
sqlite3ExprDelete(db, pWhen);
}
codeTriggerProgram(&sSubParse, pTrigger->step_list, orconf);
if( iEndTrigger ){
sqlite3VdbeResolveLabel(v, iEndTrigger);
}
sqlite3VdbeAddOp0(v, OP_Halt);
VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
transferParseError(pParse, &sSubParse);
if( pParse->nErr==0 ){
assert( db->mallocFailed==0 );
pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
}
pProgram->nMem = sSubParse.nMem;
pProgram->nCsr = sSubParse.nTab;
pProgram->token = (void *)pTrigger;
pPrg->aColmask[0] = sSubParse.oldmask;
pPrg->aColmask[1] = sSubParse.newmask;
sqlite3VdbeDelete(v);
}else{
transferParseError(pParse, &sSubParse);
}
assert( !sSubParse.pTriggerPrg && !sSubParse.nMaxArg );
sqlite3ParseObjectReset(&sSubParse);
return pPrg;
}
static TriggerPrg *getRowTrigger(
Parse *pParse,
Trigger *pTrigger,
Table *pTab,
int orconf
){
Parse *pRoot = sqlite3ParseToplevel(pParse);
TriggerPrg *pPrg;
assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
for(pPrg=pRoot->pTriggerPrg;
pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
pPrg=pPrg->pNext
);
if( !pPrg ){
pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
pParse->db->errByteOffset = -1;
}
return pPrg;
}
void sqlite3CodeRowTriggerDirect(
Parse *pParse,
Trigger *p,
Table *pTab,
int reg,
int orconf,
int ignoreJump
){
Vdbe *v = sqlite3GetVdbe(pParse);
TriggerPrg *pPrg;
pPrg = getRowTrigger(pParse, p, pTab, orconf);
assert( pPrg || pParse->nErr );
if( pPrg ){
int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
(const char *)pPrg->pProgram, P4_SUBPROGRAM);
VdbeComment(
(v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
sqlite3VdbeChangeP5(v, (u8)bRecursive);
}
}
void sqlite3CodeRowTrigger(
Parse *pParse,
Trigger *pTrigger,
int op,
ExprList *pChanges,
int tr_tm,
Table *pTab,
int reg,
int orconf,
int ignoreJump
){
Trigger *p;
assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
assert( (op==TK_UPDATE)==(pChanges!=0) );
for(p=pTrigger; p; p=p->pNext){
assert( p->pSchema!=0 );
assert( p->pTabSchema!=0 );
assert( p->pSchema==p->pTabSchema
|| p->pSchema==pParse->db->aDb[1].pSchema );
if( (p->op==op || (p->bReturning && p->op==TK_INSERT && op==TK_UPDATE))
&& p->tr_tm==tr_tm
&& checkColumnOverlap(p->pColumns, pChanges)
){
if( !p->bReturning ){
sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
}else if( sqlite3IsToplevel(pParse) ){
codeReturningTrigger(pParse, p, pTab, reg);
}
}
}
}
u32 sqlite3TriggerColmask(
Parse *pParse,
Trigger *pTrigger,
ExprList *pChanges,
int isNew,
int tr_tm,
Table *pTab,
int orconf
){
const int op = pChanges ? TK_UPDATE : TK_DELETE;
u32 mask = 0;
Trigger *p;
assert( isNew==1 || isNew==0 );
if( IsView(pTab) ){
return 0xffffffff;
}
for(p=pTrigger; p; p=p->pNext){
if( p->op==op
&& (tr_tm&p->tr_tm)
&& checkColumnOverlap(p->pColumns,pChanges)
){
if( p->bReturning ){
mask = 0xffffffff;
}else{
TriggerPrg *pPrg;
pPrg = getRowTrigger(pParse, p, pTab, orconf);
if( pPrg ){
mask |= pPrg->aColmask[isNew];
}
}
}
}
return mask;
}
#endif