extensions/sqlite/sqlite-source/trigger.c
author Asher Baker <asherkin@gmail.com>
Thu Apr 26 16:58:18 2012 +0100 (2012-04-26)
changeset 3488 bee90c8ef6c3
parent 1952 b7aa2b626807
permissions -rw-r--r--
Fixed client serials not being unique on Windows (bug 5285, r=psychonic).
ds@1952
     1
/*
ds@1952
     2
**
ds@1952
     3
** The author disclaims copyright to this source code.  In place of
ds@1952
     4
** a legal notice, here is a blessing:
ds@1952
     5
**
ds@1952
     6
**    May you do good and not evil.
ds@1952
     7
**    May you find forgiveness for yourself and forgive others.
ds@1952
     8
**    May you share freely, never taking more than you give.
ds@1952
     9
**
ds@1952
    10
*************************************************************************
ds@1952
    11
*
ds@1952
    12
*/
ds@1952
    13
#include "sqliteInt.h"
ds@1952
    14
ds@1952
    15
#ifndef SQLITE_OMIT_TRIGGER
ds@1952
    16
/*
ds@1952
    17
** Delete a linked list of TriggerStep structures.
ds@1952
    18
*/
ds@1952
    19
void sqlite3DeleteTriggerStep(TriggerStep *pTriggerStep){
ds@1952
    20
  while( pTriggerStep ){
ds@1952
    21
    TriggerStep * pTmp = pTriggerStep;
ds@1952
    22
    pTriggerStep = pTriggerStep->pNext;
ds@1952
    23
ds@1952
    24
    if( pTmp->target.dyn ) sqlite3_free((char*)pTmp->target.z);
ds@1952
    25
    sqlite3ExprDelete(pTmp->pWhere);
ds@1952
    26
    sqlite3ExprListDelete(pTmp->pExprList);
ds@1952
    27
    sqlite3SelectDelete(pTmp->pSelect);
ds@1952
    28
    sqlite3IdListDelete(pTmp->pIdList);
ds@1952
    29
ds@1952
    30
    sqlite3_free(pTmp);
ds@1952
    31
  }
ds@1952
    32
}
ds@1952
    33
ds@1952
    34
/*
ds@1952
    35
** This is called by the parser when it sees a CREATE TRIGGER statement
ds@1952
    36
** up to the point of the BEGIN before the trigger actions.  A Trigger
ds@1952
    37
** structure is generated based on the information available and stored
ds@1952
    38
** in pParse->pNewTrigger.  After the trigger actions have been parsed, the
ds@1952
    39
** sqlite3FinishTrigger() function is called to complete the trigger
ds@1952
    40
** construction process.
ds@1952
    41
*/
ds@1952
    42
void sqlite3BeginTrigger(
ds@1952
    43
  Parse *pParse,      /* The parse context of the CREATE TRIGGER statement */
ds@1952
    44
  Token *pName1,      /* The name of the trigger */
ds@1952
    45
  Token *pName2,      /* The name of the trigger */
ds@1952
    46
  int tr_tm,          /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
ds@1952
    47
  int op,             /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
ds@1952
    48
  IdList *pColumns,   /* column list if this is an UPDATE OF trigger */
ds@1952
    49
  SrcList *pTableName,/* The name of the table/view the trigger applies to */
ds@1952
    50
  Expr *pWhen,        /* WHEN clause */
ds@1952
    51
  int isTemp,         /* True if the TEMPORARY keyword is present */
ds@1952
    52
  int noErr           /* Suppress errors if the trigger already exists */
ds@1952
    53
){
ds@1952
    54
  Trigger *pTrigger = 0;
ds@1952
    55
  Table *pTab;
ds@1952
    56
  char *zName = 0;        /* Name of the trigger */
ds@1952
    57
  sqlite3 *db = pParse->db;
ds@1952
    58
  int iDb;                /* The database to store the trigger in */
ds@1952
    59
  Token *pName;           /* The unqualified db name */
ds@1952
    60
  DbFixer sFix;
ds@1952
    61
  int iTabDb;
ds@1952
    62
ds@1952
    63
  assert( pName1!=0 );   /* pName1->z might be NULL, but not pName1 itself */
ds@1952
    64
  assert( pName2!=0 );
ds@1952
    65
  if( isTemp ){
ds@1952
    66
    /* If TEMP was specified, then the trigger name may not be qualified. */
ds@1952
    67
    if( pName2->n>0 ){
ds@1952
    68
      sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
ds@1952
    69
      goto trigger_cleanup;
ds@1952
    70
    }
ds@1952
    71
    iDb = 1;
ds@1952
    72
    pName = pName1;
ds@1952
    73
  }else{
ds@1952
    74
    /* Figure out the db that the the trigger will be created in */
ds@1952
    75
    iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
ds@1952
    76
    if( iDb<0 ){
ds@1952
    77
      goto trigger_cleanup;
ds@1952
    78
    }
ds@1952
    79
  }
ds@1952
    80
ds@1952
    81
  /* If the trigger name was unqualified, and the table is a temp table,
ds@1952
    82
  ** then set iDb to 1 to create the trigger in the temporary database.
ds@1952
    83
  ** If sqlite3SrcListLookup() returns 0, indicating the table does not
ds@1952
    84
  ** exist, the error is caught by the block below.
ds@1952
    85
  */
ds@1952
    86
  if( !pTableName || db->mallocFailed ){
ds@1952
    87
    goto trigger_cleanup;
ds@1952
    88
  }
ds@1952
    89
  pTab = sqlite3SrcListLookup(pParse, pTableName);
ds@1952
    90
  if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
ds@1952
    91
    iDb = 1;
ds@1952
    92
  }
ds@1952
    93
ds@1952
    94
  /* Ensure the table name matches database name and that the table exists */
ds@1952
    95
  if( db->mallocFailed ) goto trigger_cleanup;
ds@1952
    96
  assert( pTableName->nSrc==1 );
ds@1952
    97
  if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) && 
ds@1952
    98
      sqlite3FixSrcList(&sFix, pTableName) ){
ds@1952
    99
    goto trigger_cleanup;
ds@1952
   100
  }
ds@1952
   101
  pTab = sqlite3SrcListLookup(pParse, pTableName);
ds@1952
   102
  if( !pTab ){
ds@1952
   103
    /* The table does not exist. */
ds@1952
   104
    goto trigger_cleanup;
ds@1952
   105
  }
ds@1952
   106
  if( IsVirtual(pTab) ){
ds@1952
   107
    sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
ds@1952
   108
    goto trigger_cleanup;
ds@1952
   109
  }
ds@1952
   110
ds@1952
   111
  /* Check that the trigger name is not reserved and that no trigger of the
ds@1952
   112
  ** specified name exists */
ds@1952
   113
  zName = sqlite3NameFromToken(db, pName);
ds@1952
   114
  if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
ds@1952
   115
    goto trigger_cleanup;
ds@1952
   116
  }
ds@1952
   117
  if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash), zName,strlen(zName)) ){
ds@1952
   118
    if( !noErr ){
ds@1952
   119
      sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
ds@1952
   120
    }
ds@1952
   121
    goto trigger_cleanup;
ds@1952
   122
  }
ds@1952
   123
ds@1952
   124
  /* Do not create a trigger on a system table */
ds@1952
   125
  if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
ds@1952
   126
    sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
ds@1952
   127
    pParse->nErr++;
ds@1952
   128
    goto trigger_cleanup;
ds@1952
   129
  }
ds@1952
   130
ds@1952
   131
  /* INSTEAD of triggers are only for views and views only support INSTEAD
ds@1952
   132
  ** of triggers.
ds@1952
   133
  */
ds@1952
   134
  if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
ds@1952
   135
    sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S", 
ds@1952
   136
        (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
ds@1952
   137
    goto trigger_cleanup;
ds@1952
   138
  }
ds@1952
   139
  if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
ds@1952
   140
    sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
ds@1952
   141
        " trigger on table: %S", pTableName, 0);
ds@1952
   142
    goto trigger_cleanup;
ds@1952
   143
  }
ds@1952
   144
  iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
ds@1952
   145
ds@1952
   146
#ifndef SQLITE_OMIT_AUTHORIZATION
ds@1952
   147
  {
ds@1952
   148
    int code = SQLITE_CREATE_TRIGGER;
ds@1952
   149
    const char *zDb = db->aDb[iTabDb].zName;
ds@1952
   150
    const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
ds@1952
   151
    if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
ds@1952
   152
    if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
ds@1952
   153
      goto trigger_cleanup;
ds@1952
   154
    }
ds@1952
   155
    if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
ds@1952
   156
      goto trigger_cleanup;
ds@1952
   157
    }
ds@1952
   158
  }
ds@1952
   159
#endif
ds@1952
   160
ds@1952
   161
  /* INSTEAD OF triggers can only appear on views and BEFORE triggers
ds@1952
   162
  ** cannot appear on views.  So we might as well translate every
ds@1952
   163
  ** INSTEAD OF trigger into a BEFORE trigger.  It simplifies code
ds@1952
   164
  ** elsewhere.
ds@1952
   165
  */
ds@1952
   166
  if (tr_tm == TK_INSTEAD){
ds@1952
   167
    tr_tm = TK_BEFORE;
ds@1952
   168
  }
ds@1952
   169
ds@1952
   170
  /* Build the Trigger object */
ds@1952
   171
  pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
ds@1952
   172
  if( pTrigger==0 ) goto trigger_cleanup;
ds@1952
   173
  pTrigger->name = zName;
ds@1952
   174
  zName = 0;
ds@1952
   175
  pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
ds@1952
   176
  pTrigger->pSchema = db->aDb[iDb].pSchema;
ds@1952
   177
  pTrigger->pTabSchema = pTab->pSchema;
ds@1952
   178
  pTrigger->op = op;
ds@1952
   179
  pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
ds@1952
   180
  pTrigger->pWhen = sqlite3ExprDup(db, pWhen);
ds@1952
   181
  pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
ds@1952
   182
  sqlite3TokenCopy(db, &pTrigger->nameToken,pName);
ds@1952
   183
  assert( pParse->pNewTrigger==0 );
ds@1952
   184
  pParse->pNewTrigger = pTrigger;
ds@1952
   185
ds@1952
   186
trigger_cleanup:
ds@1952
   187
  sqlite3_free(zName);
ds@1952
   188
  sqlite3SrcListDelete(pTableName);
ds@1952
   189
  sqlite3IdListDelete(pColumns);
ds@1952
   190
  sqlite3ExprDelete(pWhen);
ds@1952
   191
  if( !pParse->pNewTrigger ){
ds@1952
   192
    sqlite3DeleteTrigger(pTrigger);
ds@1952
   193
  }else{
ds@1952
   194
    assert( pParse->pNewTrigger==pTrigger );
ds@1952
   195
  }
ds@1952
   196
}
ds@1952
   197
ds@1952
   198
/*
ds@1952
   199
** This routine is called after all of the trigger actions have been parsed
ds@1952
   200
** in order to complete the process of building the trigger.
ds@1952
   201
*/
ds@1952
   202
void sqlite3FinishTrigger(
ds@1952
   203
  Parse *pParse,          /* Parser context */
ds@1952
   204
  TriggerStep *pStepList, /* The triggered program */
ds@1952
   205
  Token *pAll             /* Token that describes the complete CREATE TRIGGER */
ds@1952
   206
){
ds@1952
   207
  Trigger *pTrig = 0;     /* The trigger whose construction is finishing up */
ds@1952
   208
  sqlite3 *db = pParse->db;  /* The database */
ds@1952
   209
  DbFixer sFix;
ds@1952
   210
  int iDb;                   /* Database containing the trigger */
ds@1952
   211
ds@1952
   212
  pTrig = pParse->pNewTrigger;
ds@1952
   213
  pParse->pNewTrigger = 0;
ds@1952
   214
  if( pParse->nErr || !pTrig ) goto triggerfinish_cleanup;
ds@1952
   215
  iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
ds@1952
   216
  pTrig->step_list = pStepList;
ds@1952
   217
  while( pStepList ){
ds@1952
   218
    pStepList->pTrig = pTrig;
ds@1952
   219
    pStepList = pStepList->pNext;
ds@1952
   220
  }
ds@1952
   221
  if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &pTrig->nameToken) 
ds@1952
   222
          && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
ds@1952
   223
    goto triggerfinish_cleanup;
ds@1952
   224
  }
ds@1952
   225
ds@1952
   226
  /* if we are not initializing, and this trigger is not on a TEMP table, 
ds@1952
   227
  ** build the sqlite_master entry
ds@1952
   228
  */
ds@1952
   229
  if( !db->init.busy ){
ds@1952
   230
    static const VdbeOpList insertTrig[] = {
ds@1952
   231
      { OP_NewRowid,   0, 0,  0          },
ds@1952
   232
      { OP_String8,    0, 0,  "trigger"  },
ds@1952
   233
      { OP_String8,    0, 0,  0          },  /* 2: trigger name */
ds@1952
   234
      { OP_String8,    0, 0,  0          },  /* 3: table name */
ds@1952
   235
      { OP_Integer,    0, 0,  0          },
ds@1952
   236
      { OP_String8,    0, 0,  "CREATE TRIGGER "},
ds@1952
   237
      { OP_String8,    0, 0,  0          },  /* 6: SQL */
ds@1952
   238
      { OP_Concat,     0, 0,  0          }, 
ds@1952
   239
      { OP_MakeRecord, 5, 0,  "aaada"    },
ds@1952
   240
      { OP_Insert,     0, 0,  0          },
ds@1952
   241
    };
ds@1952
   242
    int addr;
ds@1952
   243
    Vdbe *v;
ds@1952
   244
ds@1952
   245
    /* Make an entry in the sqlite_master table */
ds@1952
   246
    v = sqlite3GetVdbe(pParse);
ds@1952
   247
    if( v==0 ) goto triggerfinish_cleanup;
ds@1952
   248
    sqlite3BeginWriteOperation(pParse, 0, iDb);
ds@1952
   249
    sqlite3OpenMasterTable(pParse, iDb);
ds@1952
   250
    addr = sqlite3VdbeAddOpList(v, ArraySize(insertTrig), insertTrig);
ds@1952
   251
    sqlite3VdbeChangeP3(v, addr+2, pTrig->name, 0); 
ds@1952
   252
    sqlite3VdbeChangeP3(v, addr+3, pTrig->table, 0); 
ds@1952
   253
    sqlite3VdbeChangeP3(v, addr+6, (char*)pAll->z, pAll->n);
ds@1952
   254
    sqlite3ChangeCookie(db, v, iDb);
ds@1952
   255
    sqlite3VdbeAddOp(v, OP_Close, 0, 0);
ds@1952
   256
    sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, sqlite3MPrintf(
ds@1952
   257
        db, "type='trigger' AND name='%q'", pTrig->name), P3_DYNAMIC
ds@1952
   258
    );
ds@1952
   259
  }
ds@1952
   260
ds@1952
   261
  if( db->init.busy ){
ds@1952
   262
    int n;
ds@1952
   263
    Table *pTab;
ds@1952
   264
    Trigger *pDel;
ds@1952
   265
    pDel = sqlite3HashInsert(&db->aDb[iDb].pSchema->trigHash, 
ds@1952
   266
                     pTrig->name, strlen(pTrig->name), pTrig);
ds@1952
   267
    if( pDel ){
ds@1952
   268
      assert( pDel==pTrig );
ds@1952
   269
      db->mallocFailed = 1;
ds@1952
   270
      goto triggerfinish_cleanup;
ds@1952
   271
    }
ds@1952
   272
    n = strlen(pTrig->table) + 1;
ds@1952
   273
    pTab = sqlite3HashFind(&pTrig->pTabSchema->tblHash, pTrig->table, n);
ds@1952
   274
    assert( pTab!=0 );
ds@1952
   275
    pTrig->pNext = pTab->pTrigger;
ds@1952
   276
    pTab->pTrigger = pTrig;
ds@1952
   277
    pTrig = 0;
ds@1952
   278
  }
ds@1952
   279
ds@1952
   280
triggerfinish_cleanup:
ds@1952
   281
  sqlite3DeleteTrigger(pTrig);
ds@1952
   282
  assert( !pParse->pNewTrigger );
ds@1952
   283
  sqlite3DeleteTriggerStep(pStepList);
ds@1952
   284
}
ds@1952
   285
ds@1952
   286
/*
ds@1952
   287
** Make a copy of all components of the given trigger step.  This has
ds@1952
   288
** the effect of copying all Expr.token.z values into memory obtained
ds@1952
   289
** from sqlite3_malloc().  As initially created, the Expr.token.z values
ds@1952
   290
** all point to the input string that was fed to the parser.  But that
ds@1952
   291
** string is ephemeral - it will go away as soon as the sqlite3_exec()
ds@1952
   292
** call that started the parser exits.  This routine makes a persistent
ds@1952
   293
** copy of all the Expr.token.z strings so that the TriggerStep structure
ds@1952
   294
** will be valid even after the sqlite3_exec() call returns.
ds@1952
   295
*/
ds@1952
   296
static void sqlitePersistTriggerStep(sqlite3 *db, TriggerStep *p){
ds@1952
   297
  if( p->target.z ){
ds@1952
   298
    p->target.z = (u8*)sqlite3DbStrNDup(db, (char*)p->target.z, p->target.n);
ds@1952
   299
    p->target.dyn = 1;
ds@1952
   300
  }
ds@1952
   301
  if( p->pSelect ){
ds@1952
   302
    Select *pNew = sqlite3SelectDup(db, p->pSelect);
ds@1952
   303
    sqlite3SelectDelete(p->pSelect);
ds@1952
   304
    p->pSelect = pNew;
ds@1952
   305
  }
ds@1952
   306
  if( p->pWhere ){
ds@1952
   307
    Expr *pNew = sqlite3ExprDup(db, p->pWhere);
ds@1952
   308
    sqlite3ExprDelete(p->pWhere);
ds@1952
   309
    p->pWhere = pNew;
ds@1952
   310
  }
ds@1952
   311
  if( p->pExprList ){
ds@1952
   312
    ExprList *pNew = sqlite3ExprListDup(db, p->pExprList);
ds@1952
   313
    sqlite3ExprListDelete(p->pExprList);
ds@1952
   314
    p->pExprList = pNew;
ds@1952
   315
  }
ds@1952
   316
  if( p->pIdList ){
ds@1952
   317
    IdList *pNew = sqlite3IdListDup(db, p->pIdList);
ds@1952
   318
    sqlite3IdListDelete(p->pIdList);
ds@1952
   319
    p->pIdList = pNew;
ds@1952
   320
  }
ds@1952
   321
}
ds@1952
   322
ds@1952
   323
/*
ds@1952
   324
** Turn a SELECT statement (that the pSelect parameter points to) into
ds@1952
   325
** a trigger step.  Return a pointer to a TriggerStep structure.
ds@1952
   326
**
ds@1952
   327
** The parser calls this routine when it finds a SELECT statement in
ds@1952
   328
** body of a TRIGGER.  
ds@1952
   329
*/
ds@1952
   330
TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
ds@1952
   331
  TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
ds@1952
   332
  if( pTriggerStep==0 ) {
ds@1952
   333
    sqlite3SelectDelete(pSelect);
ds@1952
   334
    return 0;
ds@1952
   335
  }
ds@1952
   336
ds@1952
   337
  pTriggerStep->op = TK_SELECT;
ds@1952
   338
  pTriggerStep->pSelect = pSelect;
ds@1952
   339
  pTriggerStep->orconf = OE_Default;
ds@1952
   340
  sqlitePersistTriggerStep(db, pTriggerStep);
ds@1952
   341
ds@1952
   342
  return pTriggerStep;
ds@1952
   343
}
ds@1952
   344
ds@1952
   345
/*
ds@1952
   346
** Build a trigger step out of an INSERT statement.  Return a pointer
ds@1952
   347
** to the new trigger step.
ds@1952
   348
**
ds@1952
   349
** The parser calls this routine when it sees an INSERT inside the
ds@1952
   350
** body of a trigger.
ds@1952
   351
*/
ds@1952
   352
TriggerStep *sqlite3TriggerInsertStep(
ds@1952
   353
  sqlite3 *db,        /* The database connection */
ds@1952
   354
  Token *pTableName,  /* Name of the table into which we insert */
ds@1952
   355
  IdList *pColumn,    /* List of columns in pTableName to insert into */
ds@1952
   356
  ExprList *pEList,   /* The VALUE clause: a list of values to be inserted */
ds@1952
   357
  Select *pSelect,    /* A SELECT statement that supplies values */
ds@1952
   358
  int orconf          /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
ds@1952
   359
){
ds@1952
   360
  TriggerStep *pTriggerStep;
ds@1952
   361
ds@1952
   362
  assert(pEList == 0 || pSelect == 0);
ds@1952
   363
  assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
ds@1952
   364
ds@1952
   365
  pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
ds@1952
   366
  if( pTriggerStep ){
ds@1952
   367
    pTriggerStep->op = TK_INSERT;
ds@1952
   368
    pTriggerStep->pSelect = pSelect;
ds@1952
   369
    pTriggerStep->target  = *pTableName;
ds@1952
   370
    pTriggerStep->pIdList = pColumn;
ds@1952
   371
    pTriggerStep->pExprList = pEList;
ds@1952
   372
    pTriggerStep->orconf = orconf;
ds@1952
   373
    sqlitePersistTriggerStep(db, pTriggerStep);
ds@1952
   374
  }else{
ds@1952
   375
    sqlite3IdListDelete(pColumn);
ds@1952
   376
    sqlite3ExprListDelete(pEList);
ds@1952
   377
    sqlite3SelectDelete(pSelect);
ds@1952
   378
  }
ds@1952
   379
ds@1952
   380
  return pTriggerStep;
ds@1952
   381
}
ds@1952
   382
ds@1952
   383
/*
ds@1952
   384
** Construct a trigger step that implements an UPDATE statement and return
ds@1952
   385
** a pointer to that trigger step.  The parser calls this routine when it
ds@1952
   386
** sees an UPDATE statement inside the body of a CREATE TRIGGER.
ds@1952
   387
*/
ds@1952
   388
TriggerStep *sqlite3TriggerUpdateStep(
ds@1952
   389
  sqlite3 *db,         /* The database connection */
ds@1952
   390
  Token *pTableName,   /* Name of the table to be updated */
ds@1952
   391
  ExprList *pEList,    /* The SET clause: list of column and new values */
ds@1952
   392
  Expr *pWhere,        /* The WHERE clause */
ds@1952
   393
  int orconf           /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
ds@1952
   394
){
ds@1952
   395
  TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
ds@1952
   396
  if( pTriggerStep==0 ){
ds@1952
   397
     sqlite3ExprListDelete(pEList);
ds@1952
   398
     sqlite3ExprDelete(pWhere);
ds@1952
   399
     return 0;
ds@1952
   400
  }
ds@1952
   401
ds@1952
   402
  pTriggerStep->op = TK_UPDATE;
ds@1952
   403
  pTriggerStep->target  = *pTableName;
ds@1952
   404
  pTriggerStep->pExprList = pEList;
ds@1952
   405
  pTriggerStep->pWhere = pWhere;
ds@1952
   406
  pTriggerStep->orconf = orconf;
ds@1952
   407
  sqlitePersistTriggerStep(db, pTriggerStep);
ds@1952
   408
ds@1952
   409
  return pTriggerStep;
ds@1952
   410
}
ds@1952
   411
ds@1952
   412
/*
ds@1952
   413
** Construct a trigger step that implements a DELETE statement and return
ds@1952
   414
** a pointer to that trigger step.  The parser calls this routine when it
ds@1952
   415
** sees a DELETE statement inside the body of a CREATE TRIGGER.
ds@1952
   416
*/
ds@1952
   417
TriggerStep *sqlite3TriggerDeleteStep(
ds@1952
   418
  sqlite3 *db,            /* Database connection */
ds@1952
   419
  Token *pTableName,      /* The table from which rows are deleted */
ds@1952
   420
  Expr *pWhere            /* The WHERE clause */
ds@1952
   421
){
ds@1952
   422
  TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
ds@1952
   423
  if( pTriggerStep==0 ){
ds@1952
   424
    sqlite3ExprDelete(pWhere);
ds@1952
   425
    return 0;
ds@1952
   426
  }
ds@1952
   427
ds@1952
   428
  pTriggerStep->op = TK_DELETE;
ds@1952
   429
  pTriggerStep->target  = *pTableName;
ds@1952
   430
  pTriggerStep->pWhere = pWhere;
ds@1952
   431
  pTriggerStep->orconf = OE_Default;
ds@1952
   432
  sqlitePersistTriggerStep(db, pTriggerStep);
ds@1952
   433
ds@1952
   434
  return pTriggerStep;
ds@1952
   435
}
ds@1952
   436
ds@1952
   437
/* 
ds@1952
   438
** Recursively delete a Trigger structure
ds@1952
   439
*/
ds@1952
   440
void sqlite3DeleteTrigger(Trigger *pTrigger){
ds@1952
   441
  if( pTrigger==0 ) return;
ds@1952
   442
  sqlite3DeleteTriggerStep(pTrigger->step_list);
ds@1952
   443
  sqlite3_free(pTrigger->name);
ds@1952
   444
  sqlite3_free(pTrigger->table);
ds@1952
   445
  sqlite3ExprDelete(pTrigger->pWhen);
ds@1952
   446
  sqlite3IdListDelete(pTrigger->pColumns);
ds@1952
   447
  if( pTrigger->nameToken.dyn ) sqlite3_free((char*)pTrigger->nameToken.z);
ds@1952
   448
  sqlite3_free(pTrigger);
ds@1952
   449
}
ds@1952
   450
ds@1952
   451
/*
ds@1952
   452
** This function is called to drop a trigger from the database schema. 
ds@1952
   453
**
ds@1952
   454
** This may be called directly from the parser and therefore identifies
ds@1952
   455
** the trigger by name.  The sqlite3DropTriggerPtr() routine does the
ds@1952
   456
** same job as this routine except it takes a pointer to the trigger
ds@1952
   457
** instead of the trigger name.
ds@1952
   458
**/
ds@1952
   459
void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
ds@1952
   460
  Trigger *pTrigger = 0;
ds@1952
   461
  int i;
ds@1952
   462
  const char *zDb;
ds@1952
   463
  const char *zName;
ds@1952
   464
  int nName;
ds@1952
   465
  sqlite3 *db = pParse->db;
ds@1952
   466
ds@1952
   467
  if( db->mallocFailed ) goto drop_trigger_cleanup;
ds@1952
   468
  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
ds@1952
   469
    goto drop_trigger_cleanup;
ds@1952
   470
  }
ds@1952
   471
ds@1952
   472
  assert( pName->nSrc==1 );
ds@1952
   473
  zDb = pName->a[0].zDatabase;
ds@1952
   474
  zName = pName->a[0].zName;
ds@1952
   475
  nName = strlen(zName);
ds@1952
   476
  for(i=OMIT_TEMPDB; i<db->nDb; i++){
ds@1952
   477
    int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
ds@1952
   478
    if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
ds@1952
   479
    pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
ds@1952
   480
    if( pTrigger ) break;
ds@1952
   481
  }
ds@1952
   482
  if( !pTrigger ){
ds@1952
   483
    if( !noErr ){
ds@1952
   484
      sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
ds@1952
   485
    }
ds@1952
   486
    goto drop_trigger_cleanup;
ds@1952
   487
  }
ds@1952
   488
  sqlite3DropTriggerPtr(pParse, pTrigger);
ds@1952
   489
ds@1952
   490
drop_trigger_cleanup:
ds@1952
   491
  sqlite3SrcListDelete(pName);
ds@1952
   492
}
ds@1952
   493
ds@1952
   494
/*
ds@1952
   495
** Return a pointer to the Table structure for the table that a trigger
ds@1952
   496
** is set on.
ds@1952
   497
*/
ds@1952
   498
static Table *tableOfTrigger(Trigger *pTrigger){
ds@1952
   499
  int n = strlen(pTrigger->table) + 1;
ds@1952
   500
  return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
ds@1952
   501
}
ds@1952
   502
ds@1952
   503
ds@1952
   504
/*
ds@1952
   505
** Drop a trigger given a pointer to that trigger. 
ds@1952
   506
*/
ds@1952
   507
void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
ds@1952
   508
  Table   *pTable;
ds@1952
   509
  Vdbe *v;
ds@1952
   510
  sqlite3 *db = pParse->db;
ds@1952
   511
  int iDb;
ds@1952
   512
ds@1952
   513
  iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
ds@1952
   514
  assert( iDb>=0 && iDb<db->nDb );
ds@1952
   515
  pTable = tableOfTrigger(pTrigger);
ds@1952
   516
  assert( pTable );
ds@1952
   517
  assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
ds@1952
   518
#ifndef SQLITE_OMIT_AUTHORIZATION
ds@1952
   519
  {
ds@1952
   520
    int code = SQLITE_DROP_TRIGGER;
ds@1952
   521
    const char *zDb = db->aDb[iDb].zName;
ds@1952
   522
    const char *zTab = SCHEMA_TABLE(iDb);
ds@1952
   523
    if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
ds@1952
   524
    if( sqlite3AuthCheck(pParse, code, pTrigger->name, pTable->zName, zDb) ||
ds@1952
   525
      sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
ds@1952
   526
      return;
ds@1952
   527
    }
ds@1952
   528
  }
ds@1952
   529
#endif
ds@1952
   530
ds@1952
   531
  /* Generate code to destroy the database record of the trigger.
ds@1952
   532
  */
ds@1952
   533
  assert( pTable!=0 );
ds@1952
   534
  if( (v = sqlite3GetVdbe(pParse))!=0 ){
ds@1952
   535
    int base;
ds@1952
   536
    static const VdbeOpList dropTrigger[] = {
ds@1952
   537
      { OP_Rewind,     0, ADDR(9),  0},
ds@1952
   538
      { OP_String8,    0, 0,        0}, /* 1 */
ds@1952
   539
      { OP_Column,     0, 1,        0},
ds@1952
   540
      { OP_Ne,         0, ADDR(8),  0},
ds@1952
   541
      { OP_String8,    0, 0,        "trigger"},
ds@1952
   542
      { OP_Column,     0, 0,        0},
ds@1952
   543
      { OP_Ne,         0, ADDR(8),  0},
ds@1952
   544
      { OP_Delete,     0, 0,        0},
ds@1952
   545
      { OP_Next,       0, ADDR(1),  0}, /* 8 */
ds@1952
   546
    };
ds@1952
   547
ds@1952
   548
    sqlite3BeginWriteOperation(pParse, 0, iDb);
ds@1952
   549
    sqlite3OpenMasterTable(pParse, iDb);
ds@1952
   550
    base = sqlite3VdbeAddOpList(v,  ArraySize(dropTrigger), dropTrigger);
ds@1952
   551
    sqlite3VdbeChangeP3(v, base+1, pTrigger->name, 0);
ds@1952
   552
    sqlite3ChangeCookie(db, v, iDb);
ds@1952
   553
    sqlite3VdbeAddOp(v, OP_Close, 0, 0);
ds@1952
   554
    sqlite3VdbeOp3(v, OP_DropTrigger, iDb, 0, pTrigger->name, 0);
ds@1952
   555
  }
ds@1952
   556
}
ds@1952
   557
ds@1952
   558
/*
ds@1952
   559
** Remove a trigger from the hash tables of the sqlite* pointer.
ds@1952
   560
*/
ds@1952
   561
void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
ds@1952
   562
  Trigger *pTrigger;
ds@1952
   563
  int nName = strlen(zName);
ds@1952
   564
  pTrigger = sqlite3HashInsert(&(db->aDb[iDb].pSchema->trigHash),
ds@1952
   565
                               zName, nName, 0);
ds@1952
   566
  if( pTrigger ){
ds@1952
   567
    Table *pTable = tableOfTrigger(pTrigger);
ds@1952
   568
    assert( pTable!=0 );
ds@1952
   569
    if( pTable->pTrigger == pTrigger ){
ds@1952
   570
      pTable->pTrigger = pTrigger->pNext;
ds@1952
   571
    }else{
ds@1952
   572
      Trigger *cc = pTable->pTrigger;
ds@1952
   573
      while( cc ){ 
ds@1952
   574
        if( cc->pNext == pTrigger ){
ds@1952
   575
          cc->pNext = cc->pNext->pNext;
ds@1952
   576
          break;
ds@1952
   577
        }
ds@1952
   578
        cc = cc->pNext;
ds@1952
   579
      }
ds@1952
   580
      assert(cc);
ds@1952
   581
    }
ds@1952
   582
    sqlite3DeleteTrigger(pTrigger);
ds@1952
   583
    db->flags |= SQLITE_InternChanges;
ds@1952
   584
  }
ds@1952
   585
}
ds@1952
   586
ds@1952
   587
/*
ds@1952
   588
** pEList is the SET clause of an UPDATE statement.  Each entry
ds@1952
   589
** in pEList is of the format <id>=<expr>.  If any of the entries
ds@1952
   590
** in pEList have an <id> which matches an identifier in pIdList,
ds@1952
   591
** then return TRUE.  If pIdList==NULL, then it is considered a
ds@1952
   592
** wildcard that matches anything.  Likewise if pEList==NULL then
ds@1952
   593
** it matches anything so always return true.  Return false only
ds@1952
   594
** if there is no match.
ds@1952
   595
*/
ds@1952
   596
static int checkColumnOverLap(IdList *pIdList, ExprList *pEList){
ds@1952
   597
  int e;
ds@1952
   598
  if( !pIdList || !pEList ) return 1;
ds@1952
   599
  for(e=0; e<pEList->nExpr; e++){
ds@1952
   600
    if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
ds@1952
   601
  }
ds@1952
   602
  return 0; 
ds@1952
   603
}
ds@1952
   604
ds@1952
   605
/*
ds@1952
   606
** Return a bit vector to indicate what kind of triggers exist for operation
ds@1952
   607
** "op" on table pTab.  If pChanges is not NULL then it is a list of columns
ds@1952
   608
** that are being updated.  Triggers only match if the ON clause of the
ds@1952
   609
** trigger definition overlaps the set of columns being updated.
ds@1952
   610
**
ds@1952
   611
** The returned bit vector is some combination of TRIGGER_BEFORE and
ds@1952
   612
** TRIGGER_AFTER.
ds@1952
   613
*/
ds@1952
   614
int sqlite3TriggersExist(
ds@1952
   615
  Parse *pParse,          /* Used to check for recursive triggers */
ds@1952
   616
  Table *pTab,            /* The table the contains the triggers */
ds@1952
   617
  int op,                 /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
ds@1952
   618
  ExprList *pChanges      /* Columns that change in an UPDATE statement */
ds@1952
   619
){
ds@1952
   620
  Trigger *pTrigger;
ds@1952
   621
  int mask = 0;
ds@1952
   622
ds@1952
   623
  pTrigger = IsVirtual(pTab) ? 0 : pTab->pTrigger;
ds@1952
   624
  while( pTrigger ){
ds@1952
   625
    if( pTrigger->op==op && checkColumnOverLap(pTrigger->pColumns, pChanges) ){
ds@1952
   626
      mask |= pTrigger->tr_tm;
ds@1952
   627
    }
ds@1952
   628
    pTrigger = pTrigger->pNext;
ds@1952
   629
  }
ds@1952
   630
  return mask;
ds@1952
   631
}
ds@1952
   632
ds@1952
   633
/*
ds@1952
   634
** Convert the pStep->target token into a SrcList and return a pointer
ds@1952
   635
** to that SrcList.
ds@1952
   636
**
ds@1952
   637
** This routine adds a specific database name, if needed, to the target when
ds@1952
   638
** forming the SrcList.  This prevents a trigger in one database from
ds@1952
   639
** referring to a target in another database.  An exception is when the
ds@1952
   640
** trigger is in TEMP in which case it can refer to any other database it
ds@1952
   641
** wants.
ds@1952
   642
*/
ds@1952
   643
static SrcList *targetSrcList(
ds@1952
   644
  Parse *pParse,       /* The parsing context */
ds@1952
   645
  TriggerStep *pStep   /* The trigger containing the target token */
ds@1952
   646
){
ds@1952
   647
  Token sDb;           /* Dummy database name token */
ds@1952
   648
  int iDb;             /* Index of the database to use */
ds@1952
   649
  SrcList *pSrc;       /* SrcList to be returned */
ds@1952
   650
ds@1952
   651
  iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
ds@1952
   652
  if( iDb==0 || iDb>=2 ){
ds@1952
   653
    assert( iDb<pParse->db->nDb );
ds@1952
   654
    sDb.z = (u8*)pParse->db->aDb[iDb].zName;
ds@1952
   655
    sDb.n = strlen((char*)sDb.z);
ds@1952
   656
    pSrc = sqlite3SrcListAppend(pParse->db, 0, &sDb, &pStep->target);
ds@1952
   657
  } else {
ds@1952
   658
    pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
ds@1952
   659
  }
ds@1952
   660
  return pSrc;
ds@1952
   661
}
ds@1952
   662
ds@1952
   663
/*
ds@1952
   664
** Generate VDBE code for zero or more statements inside the body of a
ds@1952
   665
** trigger.  
ds@1952
   666
*/
ds@1952
   667
static int codeTriggerProgram(
ds@1952
   668
  Parse *pParse,            /* The parser context */
ds@1952
   669
  TriggerStep *pStepList,   /* List of statements inside the trigger body */
ds@1952
   670
  int orconfin              /* Conflict algorithm. (OE_Abort, etc) */  
ds@1952
   671
){
ds@1952
   672
  TriggerStep * pTriggerStep = pStepList;
ds@1952
   673
  int orconf;
ds@1952
   674
  Vdbe *v = pParse->pVdbe;
ds@1952
   675
  sqlite3 *db = pParse->db;
ds@1952
   676
ds@1952
   677
  assert( pTriggerStep!=0 );
ds@1952
   678
  assert( v!=0 );
ds@1952
   679
  sqlite3VdbeAddOp(v, OP_ContextPush, 0, 0);
ds@1952
   680
  VdbeComment((v, "# begin trigger %s", pStepList->pTrig->name));
ds@1952
   681
  while( pTriggerStep ){
ds@1952
   682
    orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin;
ds@1952
   683
    pParse->trigStack->orconf = orconf;
ds@1952
   684
    switch( pTriggerStep->op ){
ds@1952
   685
      case TK_SELECT: {
ds@1952
   686
        Select *ss = sqlite3SelectDup(db, pTriggerStep->pSelect);
ds@1952
   687
        if( ss ){
ds@1952
   688
          sqlite3SelectResolve(pParse, ss, 0);
ds@1952
   689
          sqlite3Select(pParse, ss, SRT_Discard, 0, 0, 0, 0, 0);
ds@1952
   690
          sqlite3SelectDelete(ss);
ds@1952
   691
        }
ds@1952
   692
        break;
ds@1952
   693
      }
ds@1952
   694
      case TK_UPDATE: {
ds@1952
   695
        SrcList *pSrc;
ds@1952
   696
        pSrc = targetSrcList(pParse, pTriggerStep);
ds@1952
   697
        sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0);
ds@1952
   698
        sqlite3Update(pParse, pSrc,
ds@1952
   699
                sqlite3ExprListDup(db, pTriggerStep->pExprList), 
ds@1952
   700
                sqlite3ExprDup(db, pTriggerStep->pWhere), orconf);
ds@1952
   701
        sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0);
ds@1952
   702
        break;
ds@1952
   703
      }
ds@1952
   704
      case TK_INSERT: {
ds@1952
   705
        SrcList *pSrc;
ds@1952
   706
        pSrc = targetSrcList(pParse, pTriggerStep);
ds@1952
   707
        sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0);
ds@1952
   708
        sqlite3Insert(pParse, pSrc,
ds@1952
   709
          sqlite3ExprListDup(db, pTriggerStep->pExprList), 
ds@1952
   710
          sqlite3SelectDup(db, pTriggerStep->pSelect), 
ds@1952
   711
          sqlite3IdListDup(db, pTriggerStep->pIdList), orconf);
ds@1952
   712
        sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0);
ds@1952
   713
        break;
ds@1952
   714
      }
ds@1952
   715
      case TK_DELETE: {
ds@1952
   716
        SrcList *pSrc;
ds@1952
   717
        sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0);
ds@1952
   718
        pSrc = targetSrcList(pParse, pTriggerStep);
ds@1952
   719
        sqlite3DeleteFrom(pParse, pSrc, 
ds@1952
   720
                          sqlite3ExprDup(db, pTriggerStep->pWhere));
ds@1952
   721
        sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0);
ds@1952
   722
        break;
ds@1952
   723
      }
ds@1952
   724
      default:
ds@1952
   725
        assert(0);
ds@1952
   726
    } 
ds@1952
   727
    pTriggerStep = pTriggerStep->pNext;
ds@1952
   728
  }
ds@1952
   729
  sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
ds@1952
   730
  VdbeComment((v, "# end trigger %s", pStepList->pTrig->name));
ds@1952
   731
ds@1952
   732
  return 0;
ds@1952
   733
}
ds@1952
   734
ds@1952
   735
/*
ds@1952
   736
** This is called to code FOR EACH ROW triggers.
ds@1952
   737
**
ds@1952
   738
** When the code that this function generates is executed, the following 
ds@1952
   739
** must be true:
ds@1952
   740
**
ds@1952
   741
** 1. No cursors may be open in the main database.  (But newIdx and oldIdx
ds@1952
   742
**    can be indices of cursors in temporary tables.  See below.)
ds@1952
   743
**
ds@1952
   744
** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then
ds@1952
   745
**    a temporary vdbe cursor (index newIdx) must be open and pointing at
ds@1952
   746
**    a row containing values to be substituted for new.* expressions in the
ds@1952
   747
**    trigger program(s).
ds@1952
   748
**
ds@1952
   749
** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then
ds@1952
   750
**    a temporary vdbe cursor (index oldIdx) must be open and pointing at
ds@1952
   751
**    a row containing values to be substituted for old.* expressions in the
ds@1952
   752
**    trigger program(s).
ds@1952
   753
**
ds@1952
   754
*/
ds@1952
   755
int sqlite3CodeRowTrigger(
ds@1952
   756
  Parse *pParse,       /* Parse context */
ds@1952
   757
  int op,              /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
ds@1952
   758
  ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
ds@1952
   759
  int tr_tm,           /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
ds@1952
   760
  Table *pTab,         /* The table to code triggers from */
ds@1952
   761
  int newIdx,          /* The indice of the "new" row to access */
ds@1952
   762
  int oldIdx,          /* The indice of the "old" row to access */
ds@1952
   763
  int orconf,          /* ON CONFLICT policy */
ds@1952
   764
  int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
ds@1952
   765
){
ds@1952
   766
  Trigger *p;
ds@1952
   767
  TriggerStack trigStackEntry;
ds@1952
   768
ds@1952
   769
  assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE);
ds@1952
   770
  assert(tr_tm == TRIGGER_BEFORE || tr_tm == TRIGGER_AFTER );
ds@1952
   771
ds@1952
   772
  assert(newIdx != -1 || oldIdx != -1);
ds@1952
   773
ds@1952
   774
  for(p=pTab->pTrigger; p; p=p->pNext){
ds@1952
   775
    int fire_this = 0;
ds@1952
   776
ds@1952
   777
    /* Determine whether we should code this trigger */
ds@1952
   778
    if( 
ds@1952
   779
      p->op==op && 
ds@1952
   780
      p->tr_tm==tr_tm && 
ds@1952
   781
      (p->pSchema==p->pTabSchema || p->pSchema==pParse->db->aDb[1].pSchema) &&
ds@1952
   782
      (op!=TK_UPDATE||!p->pColumns||checkColumnOverLap(p->pColumns,pChanges))
ds@1952
   783
    ){
ds@1952
   784
      TriggerStack *pS;      /* Pointer to trigger-stack entry */
ds@1952
   785
      for(pS=pParse->trigStack; pS && p!=pS->pTrigger; pS=pS->pNext){}
ds@1952
   786
      if( !pS ){
ds@1952
   787
        fire_this = 1;
ds@1952
   788
      }
ds@1952
   789
#if 0    /* Give no warning for recursive triggers.  Just do not do them */
ds@1952
   790
      else{
ds@1952
   791
        sqlite3ErrorMsg(pParse, "recursive triggers not supported (%s)",
ds@1952
   792
            p->name);
ds@1952
   793
        return SQLITE_ERROR;
ds@1952
   794
      }
ds@1952
   795
#endif
ds@1952
   796
    }
ds@1952
   797
 
ds@1952
   798
    if( fire_this ){
ds@1952
   799
      int endTrigger;
ds@1952
   800
      Expr * whenExpr;
ds@1952
   801
      AuthContext sContext;
ds@1952
   802
      NameContext sNC;
ds@1952
   803
ds@1952
   804
      memset(&sNC, 0, sizeof(sNC));
ds@1952
   805
      sNC.pParse = pParse;
ds@1952
   806
ds@1952
   807
      /* Push an entry on to the trigger stack */
ds@1952
   808
      trigStackEntry.pTrigger = p;
ds@1952
   809
      trigStackEntry.newIdx = newIdx;
ds@1952
   810
      trigStackEntry.oldIdx = oldIdx;
ds@1952
   811
      trigStackEntry.pTab = pTab;
ds@1952
   812
      trigStackEntry.pNext = pParse->trigStack;
ds@1952
   813
      trigStackEntry.ignoreJump = ignoreJump;
ds@1952
   814
      pParse->trigStack = &trigStackEntry;
ds@1952
   815
      sqlite3AuthContextPush(pParse, &sContext, p->name);
ds@1952
   816
ds@1952
   817
      /* code the WHEN clause */
ds@1952
   818
      endTrigger = sqlite3VdbeMakeLabel(pParse->pVdbe);
ds@1952
   819
      whenExpr = sqlite3ExprDup(pParse->db, p->pWhen);
ds@1952
   820
      if( sqlite3ExprResolveNames(&sNC, whenExpr) ){
ds@1952
   821
        pParse->trigStack = trigStackEntry.pNext;
ds@1952
   822
        sqlite3ExprDelete(whenExpr);
ds@1952
   823
        return 1;
ds@1952
   824
      }
ds@1952
   825
      sqlite3ExprIfFalse(pParse, whenExpr, endTrigger, 1);
ds@1952
   826
      sqlite3ExprDelete(whenExpr);
ds@1952
   827
ds@1952
   828
      codeTriggerProgram(pParse, p->step_list, orconf); 
ds@1952
   829
ds@1952
   830
      /* Pop the entry off the trigger stack */
ds@1952
   831
      pParse->trigStack = trigStackEntry.pNext;
ds@1952
   832
      sqlite3AuthContextPop(&sContext);
ds@1952
   833
ds@1952
   834
      sqlite3VdbeResolveLabel(pParse->pVdbe, endTrigger);
ds@1952
   835
    }
ds@1952
   836
  }
ds@1952
   837
  return 0;
ds@1952
   838
}
ds@1952
   839
#endif /* !defined(SQLITE_OMIT_TRIGGER) */