extensions/sqlite/sqlite-source/trigger.c
branchsourcemod-1.1.0
changeset 2283 03a67e41afbc
parent 1951 c7b4237fff50
child 2287 017a725c9d17
     1.1 --- a/extensions/sqlite/sqlite-source/trigger.c	Fri Mar 28 06:21:34 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,839 +0,0 @@
     1.4 -/*
     1.5 -**
     1.6 -** The author disclaims copyright to this source code.  In place of
     1.7 -** a legal notice, here is a blessing:
     1.8 -**
     1.9 -**    May you do good and not evil.
    1.10 -**    May you find forgiveness for yourself and forgive others.
    1.11 -**    May you share freely, never taking more than you give.
    1.12 -**
    1.13 -*************************************************************************
    1.14 -*
    1.15 -*/
    1.16 -#include "sqliteInt.h"
    1.17 -
    1.18 -#ifndef SQLITE_OMIT_TRIGGER
    1.19 -/*
    1.20 -** Delete a linked list of TriggerStep structures.
    1.21 -*/
    1.22 -void sqlite3DeleteTriggerStep(TriggerStep *pTriggerStep){
    1.23 -  while( pTriggerStep ){
    1.24 -    TriggerStep * pTmp = pTriggerStep;
    1.25 -    pTriggerStep = pTriggerStep->pNext;
    1.26 -
    1.27 -    if( pTmp->target.dyn ) sqlite3_free((char*)pTmp->target.z);
    1.28 -    sqlite3ExprDelete(pTmp->pWhere);
    1.29 -    sqlite3ExprListDelete(pTmp->pExprList);
    1.30 -    sqlite3SelectDelete(pTmp->pSelect);
    1.31 -    sqlite3IdListDelete(pTmp->pIdList);
    1.32 -
    1.33 -    sqlite3_free(pTmp);
    1.34 -  }
    1.35 -}
    1.36 -
    1.37 -/*
    1.38 -** This is called by the parser when it sees a CREATE TRIGGER statement
    1.39 -** up to the point of the BEGIN before the trigger actions.  A Trigger
    1.40 -** structure is generated based on the information available and stored
    1.41 -** in pParse->pNewTrigger.  After the trigger actions have been parsed, the
    1.42 -** sqlite3FinishTrigger() function is called to complete the trigger
    1.43 -** construction process.
    1.44 -*/
    1.45 -void sqlite3BeginTrigger(
    1.46 -  Parse *pParse,      /* The parse context of the CREATE TRIGGER statement */
    1.47 -  Token *pName1,      /* The name of the trigger */
    1.48 -  Token *pName2,      /* The name of the trigger */
    1.49 -  int tr_tm,          /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
    1.50 -  int op,             /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
    1.51 -  IdList *pColumns,   /* column list if this is an UPDATE OF trigger */
    1.52 -  SrcList *pTableName,/* The name of the table/view the trigger applies to */
    1.53 -  Expr *pWhen,        /* WHEN clause */
    1.54 -  int isTemp,         /* True if the TEMPORARY keyword is present */
    1.55 -  int noErr           /* Suppress errors if the trigger already exists */
    1.56 -){
    1.57 -  Trigger *pTrigger = 0;
    1.58 -  Table *pTab;
    1.59 -  char *zName = 0;        /* Name of the trigger */
    1.60 -  sqlite3 *db = pParse->db;
    1.61 -  int iDb;                /* The database to store the trigger in */
    1.62 -  Token *pName;           /* The unqualified db name */
    1.63 -  DbFixer sFix;
    1.64 -  int iTabDb;
    1.65 -
    1.66 -  assert( pName1!=0 );   /* pName1->z might be NULL, but not pName1 itself */
    1.67 -  assert( pName2!=0 );
    1.68 -  if( isTemp ){
    1.69 -    /* If TEMP was specified, then the trigger name may not be qualified. */
    1.70 -    if( pName2->n>0 ){
    1.71 -      sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
    1.72 -      goto trigger_cleanup;
    1.73 -    }
    1.74 -    iDb = 1;
    1.75 -    pName = pName1;
    1.76 -  }else{
    1.77 -    /* Figure out the db that the the trigger will be created in */
    1.78 -    iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
    1.79 -    if( iDb<0 ){
    1.80 -      goto trigger_cleanup;
    1.81 -    }
    1.82 -  }
    1.83 -
    1.84 -  /* If the trigger name was unqualified, and the table is a temp table,
    1.85 -  ** then set iDb to 1 to create the trigger in the temporary database.
    1.86 -  ** If sqlite3SrcListLookup() returns 0, indicating the table does not
    1.87 -  ** exist, the error is caught by the block below.
    1.88 -  */
    1.89 -  if( !pTableName || db->mallocFailed ){
    1.90 -    goto trigger_cleanup;
    1.91 -  }
    1.92 -  pTab = sqlite3SrcListLookup(pParse, pTableName);
    1.93 -  if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
    1.94 -    iDb = 1;
    1.95 -  }
    1.96 -
    1.97 -  /* Ensure the table name matches database name and that the table exists */
    1.98 -  if( db->mallocFailed ) goto trigger_cleanup;
    1.99 -  assert( pTableName->nSrc==1 );
   1.100 -  if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) && 
   1.101 -      sqlite3FixSrcList(&sFix, pTableName) ){
   1.102 -    goto trigger_cleanup;
   1.103 -  }
   1.104 -  pTab = sqlite3SrcListLookup(pParse, pTableName);
   1.105 -  if( !pTab ){
   1.106 -    /* The table does not exist. */
   1.107 -    goto trigger_cleanup;
   1.108 -  }
   1.109 -  if( IsVirtual(pTab) ){
   1.110 -    sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
   1.111 -    goto trigger_cleanup;
   1.112 -  }
   1.113 -
   1.114 -  /* Check that the trigger name is not reserved and that no trigger of the
   1.115 -  ** specified name exists */
   1.116 -  zName = sqlite3NameFromToken(db, pName);
   1.117 -  if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
   1.118 -    goto trigger_cleanup;
   1.119 -  }
   1.120 -  if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash), zName,strlen(zName)) ){
   1.121 -    if( !noErr ){
   1.122 -      sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
   1.123 -    }
   1.124 -    goto trigger_cleanup;
   1.125 -  }
   1.126 -
   1.127 -  /* Do not create a trigger on a system table */
   1.128 -  if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
   1.129 -    sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
   1.130 -    pParse->nErr++;
   1.131 -    goto trigger_cleanup;
   1.132 -  }
   1.133 -
   1.134 -  /* INSTEAD of triggers are only for views and views only support INSTEAD
   1.135 -  ** of triggers.
   1.136 -  */
   1.137 -  if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
   1.138 -    sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S", 
   1.139 -        (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
   1.140 -    goto trigger_cleanup;
   1.141 -  }
   1.142 -  if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
   1.143 -    sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
   1.144 -        " trigger on table: %S", pTableName, 0);
   1.145 -    goto trigger_cleanup;
   1.146 -  }
   1.147 -  iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
   1.148 -
   1.149 -#ifndef SQLITE_OMIT_AUTHORIZATION
   1.150 -  {
   1.151 -    int code = SQLITE_CREATE_TRIGGER;
   1.152 -    const char *zDb = db->aDb[iTabDb].zName;
   1.153 -    const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
   1.154 -    if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
   1.155 -    if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
   1.156 -      goto trigger_cleanup;
   1.157 -    }
   1.158 -    if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
   1.159 -      goto trigger_cleanup;
   1.160 -    }
   1.161 -  }
   1.162 -#endif
   1.163 -
   1.164 -  /* INSTEAD OF triggers can only appear on views and BEFORE triggers
   1.165 -  ** cannot appear on views.  So we might as well translate every
   1.166 -  ** INSTEAD OF trigger into a BEFORE trigger.  It simplifies code
   1.167 -  ** elsewhere.
   1.168 -  */
   1.169 -  if (tr_tm == TK_INSTEAD){
   1.170 -    tr_tm = TK_BEFORE;
   1.171 -  }
   1.172 -
   1.173 -  /* Build the Trigger object */
   1.174 -  pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
   1.175 -  if( pTrigger==0 ) goto trigger_cleanup;
   1.176 -  pTrigger->name = zName;
   1.177 -  zName = 0;
   1.178 -  pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
   1.179 -  pTrigger->pSchema = db->aDb[iDb].pSchema;
   1.180 -  pTrigger->pTabSchema = pTab->pSchema;
   1.181 -  pTrigger->op = op;
   1.182 -  pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
   1.183 -  pTrigger->pWhen = sqlite3ExprDup(db, pWhen);
   1.184 -  pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
   1.185 -  sqlite3TokenCopy(db, &pTrigger->nameToken,pName);
   1.186 -  assert( pParse->pNewTrigger==0 );
   1.187 -  pParse->pNewTrigger = pTrigger;
   1.188 -
   1.189 -trigger_cleanup:
   1.190 -  sqlite3_free(zName);
   1.191 -  sqlite3SrcListDelete(pTableName);
   1.192 -  sqlite3IdListDelete(pColumns);
   1.193 -  sqlite3ExprDelete(pWhen);
   1.194 -  if( !pParse->pNewTrigger ){
   1.195 -    sqlite3DeleteTrigger(pTrigger);
   1.196 -  }else{
   1.197 -    assert( pParse->pNewTrigger==pTrigger );
   1.198 -  }
   1.199 -}
   1.200 -
   1.201 -/*
   1.202 -** This routine is called after all of the trigger actions have been parsed
   1.203 -** in order to complete the process of building the trigger.
   1.204 -*/
   1.205 -void sqlite3FinishTrigger(
   1.206 -  Parse *pParse,          /* Parser context */
   1.207 -  TriggerStep *pStepList, /* The triggered program */
   1.208 -  Token *pAll             /* Token that describes the complete CREATE TRIGGER */
   1.209 -){
   1.210 -  Trigger *pTrig = 0;     /* The trigger whose construction is finishing up */
   1.211 -  sqlite3 *db = pParse->db;  /* The database */
   1.212 -  DbFixer sFix;
   1.213 -  int iDb;                   /* Database containing the trigger */
   1.214 -
   1.215 -  pTrig = pParse->pNewTrigger;
   1.216 -  pParse->pNewTrigger = 0;
   1.217 -  if( pParse->nErr || !pTrig ) goto triggerfinish_cleanup;
   1.218 -  iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
   1.219 -  pTrig->step_list = pStepList;
   1.220 -  while( pStepList ){
   1.221 -    pStepList->pTrig = pTrig;
   1.222 -    pStepList = pStepList->pNext;
   1.223 -  }
   1.224 -  if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &pTrig->nameToken) 
   1.225 -          && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
   1.226 -    goto triggerfinish_cleanup;
   1.227 -  }
   1.228 -
   1.229 -  /* if we are not initializing, and this trigger is not on a TEMP table, 
   1.230 -  ** build the sqlite_master entry
   1.231 -  */
   1.232 -  if( !db->init.busy ){
   1.233 -    static const VdbeOpList insertTrig[] = {
   1.234 -      { OP_NewRowid,   0, 0,  0          },
   1.235 -      { OP_String8,    0, 0,  "trigger"  },
   1.236 -      { OP_String8,    0, 0,  0          },  /* 2: trigger name */
   1.237 -      { OP_String8,    0, 0,  0          },  /* 3: table name */
   1.238 -      { OP_Integer,    0, 0,  0          },
   1.239 -      { OP_String8,    0, 0,  "CREATE TRIGGER "},
   1.240 -      { OP_String8,    0, 0,  0          },  /* 6: SQL */
   1.241 -      { OP_Concat,     0, 0,  0          }, 
   1.242 -      { OP_MakeRecord, 5, 0,  "aaada"    },
   1.243 -      { OP_Insert,     0, 0,  0          },
   1.244 -    };
   1.245 -    int addr;
   1.246 -    Vdbe *v;
   1.247 -
   1.248 -    /* Make an entry in the sqlite_master table */
   1.249 -    v = sqlite3GetVdbe(pParse);
   1.250 -    if( v==0 ) goto triggerfinish_cleanup;
   1.251 -    sqlite3BeginWriteOperation(pParse, 0, iDb);
   1.252 -    sqlite3OpenMasterTable(pParse, iDb);
   1.253 -    addr = sqlite3VdbeAddOpList(v, ArraySize(insertTrig), insertTrig);
   1.254 -    sqlite3VdbeChangeP3(v, addr+2, pTrig->name, 0); 
   1.255 -    sqlite3VdbeChangeP3(v, addr+3, pTrig->table, 0); 
   1.256 -    sqlite3VdbeChangeP3(v, addr+6, (char*)pAll->z, pAll->n);
   1.257 -    sqlite3ChangeCookie(db, v, iDb);
   1.258 -    sqlite3VdbeAddOp(v, OP_Close, 0, 0);
   1.259 -    sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, sqlite3MPrintf(
   1.260 -        db, "type='trigger' AND name='%q'", pTrig->name), P3_DYNAMIC
   1.261 -    );
   1.262 -  }
   1.263 -
   1.264 -  if( db->init.busy ){
   1.265 -    int n;
   1.266 -    Table *pTab;
   1.267 -    Trigger *pDel;
   1.268 -    pDel = sqlite3HashInsert(&db->aDb[iDb].pSchema->trigHash, 
   1.269 -                     pTrig->name, strlen(pTrig->name), pTrig);
   1.270 -    if( pDel ){
   1.271 -      assert( pDel==pTrig );
   1.272 -      db->mallocFailed = 1;
   1.273 -      goto triggerfinish_cleanup;
   1.274 -    }
   1.275 -    n = strlen(pTrig->table) + 1;
   1.276 -    pTab = sqlite3HashFind(&pTrig->pTabSchema->tblHash, pTrig->table, n);
   1.277 -    assert( pTab!=0 );
   1.278 -    pTrig->pNext = pTab->pTrigger;
   1.279 -    pTab->pTrigger = pTrig;
   1.280 -    pTrig = 0;
   1.281 -  }
   1.282 -
   1.283 -triggerfinish_cleanup:
   1.284 -  sqlite3DeleteTrigger(pTrig);
   1.285 -  assert( !pParse->pNewTrigger );
   1.286 -  sqlite3DeleteTriggerStep(pStepList);
   1.287 -}
   1.288 -
   1.289 -/*
   1.290 -** Make a copy of all components of the given trigger step.  This has
   1.291 -** the effect of copying all Expr.token.z values into memory obtained
   1.292 -** from sqlite3_malloc().  As initially created, the Expr.token.z values
   1.293 -** all point to the input string that was fed to the parser.  But that
   1.294 -** string is ephemeral - it will go away as soon as the sqlite3_exec()
   1.295 -** call that started the parser exits.  This routine makes a persistent
   1.296 -** copy of all the Expr.token.z strings so that the TriggerStep structure
   1.297 -** will be valid even after the sqlite3_exec() call returns.
   1.298 -*/
   1.299 -static void sqlitePersistTriggerStep(sqlite3 *db, TriggerStep *p){
   1.300 -  if( p->target.z ){
   1.301 -    p->target.z = (u8*)sqlite3DbStrNDup(db, (char*)p->target.z, p->target.n);
   1.302 -    p->target.dyn = 1;
   1.303 -  }
   1.304 -  if( p->pSelect ){
   1.305 -    Select *pNew = sqlite3SelectDup(db, p->pSelect);
   1.306 -    sqlite3SelectDelete(p->pSelect);
   1.307 -    p->pSelect = pNew;
   1.308 -  }
   1.309 -  if( p->pWhere ){
   1.310 -    Expr *pNew = sqlite3ExprDup(db, p->pWhere);
   1.311 -    sqlite3ExprDelete(p->pWhere);
   1.312 -    p->pWhere = pNew;
   1.313 -  }
   1.314 -  if( p->pExprList ){
   1.315 -    ExprList *pNew = sqlite3ExprListDup(db, p->pExprList);
   1.316 -    sqlite3ExprListDelete(p->pExprList);
   1.317 -    p->pExprList = pNew;
   1.318 -  }
   1.319 -  if( p->pIdList ){
   1.320 -    IdList *pNew = sqlite3IdListDup(db, p->pIdList);
   1.321 -    sqlite3IdListDelete(p->pIdList);
   1.322 -    p->pIdList = pNew;
   1.323 -  }
   1.324 -}
   1.325 -
   1.326 -/*
   1.327 -** Turn a SELECT statement (that the pSelect parameter points to) into
   1.328 -** a trigger step.  Return a pointer to a TriggerStep structure.
   1.329 -**
   1.330 -** The parser calls this routine when it finds a SELECT statement in
   1.331 -** body of a TRIGGER.  
   1.332 -*/
   1.333 -TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
   1.334 -  TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
   1.335 -  if( pTriggerStep==0 ) {
   1.336 -    sqlite3SelectDelete(pSelect);
   1.337 -    return 0;
   1.338 -  }
   1.339 -
   1.340 -  pTriggerStep->op = TK_SELECT;
   1.341 -  pTriggerStep->pSelect = pSelect;
   1.342 -  pTriggerStep->orconf = OE_Default;
   1.343 -  sqlitePersistTriggerStep(db, pTriggerStep);
   1.344 -
   1.345 -  return pTriggerStep;
   1.346 -}
   1.347 -
   1.348 -/*
   1.349 -** Build a trigger step out of an INSERT statement.  Return a pointer
   1.350 -** to the new trigger step.
   1.351 -**
   1.352 -** The parser calls this routine when it sees an INSERT inside the
   1.353 -** body of a trigger.
   1.354 -*/
   1.355 -TriggerStep *sqlite3TriggerInsertStep(
   1.356 -  sqlite3 *db,        /* The database connection */
   1.357 -  Token *pTableName,  /* Name of the table into which we insert */
   1.358 -  IdList *pColumn,    /* List of columns in pTableName to insert into */
   1.359 -  ExprList *pEList,   /* The VALUE clause: a list of values to be inserted */
   1.360 -  Select *pSelect,    /* A SELECT statement that supplies values */
   1.361 -  int orconf          /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
   1.362 -){
   1.363 -  TriggerStep *pTriggerStep;
   1.364 -
   1.365 -  assert(pEList == 0 || pSelect == 0);
   1.366 -  assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
   1.367 -
   1.368 -  pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
   1.369 -  if( pTriggerStep ){
   1.370 -    pTriggerStep->op = TK_INSERT;
   1.371 -    pTriggerStep->pSelect = pSelect;
   1.372 -    pTriggerStep->target  = *pTableName;
   1.373 -    pTriggerStep->pIdList = pColumn;
   1.374 -    pTriggerStep->pExprList = pEList;
   1.375 -    pTriggerStep->orconf = orconf;
   1.376 -    sqlitePersistTriggerStep(db, pTriggerStep);
   1.377 -  }else{
   1.378 -    sqlite3IdListDelete(pColumn);
   1.379 -    sqlite3ExprListDelete(pEList);
   1.380 -    sqlite3SelectDelete(pSelect);
   1.381 -  }
   1.382 -
   1.383 -  return pTriggerStep;
   1.384 -}
   1.385 -
   1.386 -/*
   1.387 -** Construct a trigger step that implements an UPDATE statement and return
   1.388 -** a pointer to that trigger step.  The parser calls this routine when it
   1.389 -** sees an UPDATE statement inside the body of a CREATE TRIGGER.
   1.390 -*/
   1.391 -TriggerStep *sqlite3TriggerUpdateStep(
   1.392 -  sqlite3 *db,         /* The database connection */
   1.393 -  Token *pTableName,   /* Name of the table to be updated */
   1.394 -  ExprList *pEList,    /* The SET clause: list of column and new values */
   1.395 -  Expr *pWhere,        /* The WHERE clause */
   1.396 -  int orconf           /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
   1.397 -){
   1.398 -  TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
   1.399 -  if( pTriggerStep==0 ){
   1.400 -     sqlite3ExprListDelete(pEList);
   1.401 -     sqlite3ExprDelete(pWhere);
   1.402 -     return 0;
   1.403 -  }
   1.404 -
   1.405 -  pTriggerStep->op = TK_UPDATE;
   1.406 -  pTriggerStep->target  = *pTableName;
   1.407 -  pTriggerStep->pExprList = pEList;
   1.408 -  pTriggerStep->pWhere = pWhere;
   1.409 -  pTriggerStep->orconf = orconf;
   1.410 -  sqlitePersistTriggerStep(db, pTriggerStep);
   1.411 -
   1.412 -  return pTriggerStep;
   1.413 -}
   1.414 -
   1.415 -/*
   1.416 -** Construct a trigger step that implements a DELETE statement and return
   1.417 -** a pointer to that trigger step.  The parser calls this routine when it
   1.418 -** sees a DELETE statement inside the body of a CREATE TRIGGER.
   1.419 -*/
   1.420 -TriggerStep *sqlite3TriggerDeleteStep(
   1.421 -  sqlite3 *db,            /* Database connection */
   1.422 -  Token *pTableName,      /* The table from which rows are deleted */
   1.423 -  Expr *pWhere            /* The WHERE clause */
   1.424 -){
   1.425 -  TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
   1.426 -  if( pTriggerStep==0 ){
   1.427 -    sqlite3ExprDelete(pWhere);
   1.428 -    return 0;
   1.429 -  }
   1.430 -
   1.431 -  pTriggerStep->op = TK_DELETE;
   1.432 -  pTriggerStep->target  = *pTableName;
   1.433 -  pTriggerStep->pWhere = pWhere;
   1.434 -  pTriggerStep->orconf = OE_Default;
   1.435 -  sqlitePersistTriggerStep(db, pTriggerStep);
   1.436 -
   1.437 -  return pTriggerStep;
   1.438 -}
   1.439 -
   1.440 -/* 
   1.441 -** Recursively delete a Trigger structure
   1.442 -*/
   1.443 -void sqlite3DeleteTrigger(Trigger *pTrigger){
   1.444 -  if( pTrigger==0 ) return;
   1.445 -  sqlite3DeleteTriggerStep(pTrigger->step_list);
   1.446 -  sqlite3_free(pTrigger->name);
   1.447 -  sqlite3_free(pTrigger->table);
   1.448 -  sqlite3ExprDelete(pTrigger->pWhen);
   1.449 -  sqlite3IdListDelete(pTrigger->pColumns);
   1.450 -  if( pTrigger->nameToken.dyn ) sqlite3_free((char*)pTrigger->nameToken.z);
   1.451 -  sqlite3_free(pTrigger);
   1.452 -}
   1.453 -
   1.454 -/*
   1.455 -** This function is called to drop a trigger from the database schema. 
   1.456 -**
   1.457 -** This may be called directly from the parser and therefore identifies
   1.458 -** the trigger by name.  The sqlite3DropTriggerPtr() routine does the
   1.459 -** same job as this routine except it takes a pointer to the trigger
   1.460 -** instead of the trigger name.
   1.461 -**/
   1.462 -void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
   1.463 -  Trigger *pTrigger = 0;
   1.464 -  int i;
   1.465 -  const char *zDb;
   1.466 -  const char *zName;
   1.467 -  int nName;
   1.468 -  sqlite3 *db = pParse->db;
   1.469 -
   1.470 -  if( db->mallocFailed ) goto drop_trigger_cleanup;
   1.471 -  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
   1.472 -    goto drop_trigger_cleanup;
   1.473 -  }
   1.474 -
   1.475 -  assert( pName->nSrc==1 );
   1.476 -  zDb = pName->a[0].zDatabase;
   1.477 -  zName = pName->a[0].zName;
   1.478 -  nName = strlen(zName);
   1.479 -  for(i=OMIT_TEMPDB; i<db->nDb; i++){
   1.480 -    int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
   1.481 -    if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
   1.482 -    pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
   1.483 -    if( pTrigger ) break;
   1.484 -  }
   1.485 -  if( !pTrigger ){
   1.486 -    if( !noErr ){
   1.487 -      sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
   1.488 -    }
   1.489 -    goto drop_trigger_cleanup;
   1.490 -  }
   1.491 -  sqlite3DropTriggerPtr(pParse, pTrigger);
   1.492 -
   1.493 -drop_trigger_cleanup:
   1.494 -  sqlite3SrcListDelete(pName);
   1.495 -}
   1.496 -
   1.497 -/*
   1.498 -** Return a pointer to the Table structure for the table that a trigger
   1.499 -** is set on.
   1.500 -*/
   1.501 -static Table *tableOfTrigger(Trigger *pTrigger){
   1.502 -  int n = strlen(pTrigger->table) + 1;
   1.503 -  return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
   1.504 -}
   1.505 -
   1.506 -
   1.507 -/*
   1.508 -** Drop a trigger given a pointer to that trigger. 
   1.509 -*/
   1.510 -void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
   1.511 -  Table   *pTable;
   1.512 -  Vdbe *v;
   1.513 -  sqlite3 *db = pParse->db;
   1.514 -  int iDb;
   1.515 -
   1.516 -  iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
   1.517 -  assert( iDb>=0 && iDb<db->nDb );
   1.518 -  pTable = tableOfTrigger(pTrigger);
   1.519 -  assert( pTable );
   1.520 -  assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
   1.521 -#ifndef SQLITE_OMIT_AUTHORIZATION
   1.522 -  {
   1.523 -    int code = SQLITE_DROP_TRIGGER;
   1.524 -    const char *zDb = db->aDb[iDb].zName;
   1.525 -    const char *zTab = SCHEMA_TABLE(iDb);
   1.526 -    if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
   1.527 -    if( sqlite3AuthCheck(pParse, code, pTrigger->name, pTable->zName, zDb) ||
   1.528 -      sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
   1.529 -      return;
   1.530 -    }
   1.531 -  }
   1.532 -#endif
   1.533 -
   1.534 -  /* Generate code to destroy the database record of the trigger.
   1.535 -  */
   1.536 -  assert( pTable!=0 );
   1.537 -  if( (v = sqlite3GetVdbe(pParse))!=0 ){
   1.538 -    int base;
   1.539 -    static const VdbeOpList dropTrigger[] = {
   1.540 -      { OP_Rewind,     0, ADDR(9),  0},
   1.541 -      { OP_String8,    0, 0,        0}, /* 1 */
   1.542 -      { OP_Column,     0, 1,        0},
   1.543 -      { OP_Ne,         0, ADDR(8),  0},
   1.544 -      { OP_String8,    0, 0,        "trigger"},
   1.545 -      { OP_Column,     0, 0,        0},
   1.546 -      { OP_Ne,         0, ADDR(8),  0},
   1.547 -      { OP_Delete,     0, 0,        0},
   1.548 -      { OP_Next,       0, ADDR(1),  0}, /* 8 */
   1.549 -    };
   1.550 -
   1.551 -    sqlite3BeginWriteOperation(pParse, 0, iDb);
   1.552 -    sqlite3OpenMasterTable(pParse, iDb);
   1.553 -    base = sqlite3VdbeAddOpList(v,  ArraySize(dropTrigger), dropTrigger);
   1.554 -    sqlite3VdbeChangeP3(v, base+1, pTrigger->name, 0);
   1.555 -    sqlite3ChangeCookie(db, v, iDb);
   1.556 -    sqlite3VdbeAddOp(v, OP_Close, 0, 0);
   1.557 -    sqlite3VdbeOp3(v, OP_DropTrigger, iDb, 0, pTrigger->name, 0);
   1.558 -  }
   1.559 -}
   1.560 -
   1.561 -/*
   1.562 -** Remove a trigger from the hash tables of the sqlite* pointer.
   1.563 -*/
   1.564 -void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
   1.565 -  Trigger *pTrigger;
   1.566 -  int nName = strlen(zName);
   1.567 -  pTrigger = sqlite3HashInsert(&(db->aDb[iDb].pSchema->trigHash),
   1.568 -                               zName, nName, 0);
   1.569 -  if( pTrigger ){
   1.570 -    Table *pTable = tableOfTrigger(pTrigger);
   1.571 -    assert( pTable!=0 );
   1.572 -    if( pTable->pTrigger == pTrigger ){
   1.573 -      pTable->pTrigger = pTrigger->pNext;
   1.574 -    }else{
   1.575 -      Trigger *cc = pTable->pTrigger;
   1.576 -      while( cc ){ 
   1.577 -        if( cc->pNext == pTrigger ){
   1.578 -          cc->pNext = cc->pNext->pNext;
   1.579 -          break;
   1.580 -        }
   1.581 -        cc = cc->pNext;
   1.582 -      }
   1.583 -      assert(cc);
   1.584 -    }
   1.585 -    sqlite3DeleteTrigger(pTrigger);
   1.586 -    db->flags |= SQLITE_InternChanges;
   1.587 -  }
   1.588 -}
   1.589 -
   1.590 -/*
   1.591 -** pEList is the SET clause of an UPDATE statement.  Each entry
   1.592 -** in pEList is of the format <id>=<expr>.  If any of the entries
   1.593 -** in pEList have an <id> which matches an identifier in pIdList,
   1.594 -** then return TRUE.  If pIdList==NULL, then it is considered a
   1.595 -** wildcard that matches anything.  Likewise if pEList==NULL then
   1.596 -** it matches anything so always return true.  Return false only
   1.597 -** if there is no match.
   1.598 -*/
   1.599 -static int checkColumnOverLap(IdList *pIdList, ExprList *pEList){
   1.600 -  int e;
   1.601 -  if( !pIdList || !pEList ) return 1;
   1.602 -  for(e=0; e<pEList->nExpr; e++){
   1.603 -    if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
   1.604 -  }
   1.605 -  return 0; 
   1.606 -}
   1.607 -
   1.608 -/*
   1.609 -** Return a bit vector to indicate what kind of triggers exist for operation
   1.610 -** "op" on table pTab.  If pChanges is not NULL then it is a list of columns
   1.611 -** that are being updated.  Triggers only match if the ON clause of the
   1.612 -** trigger definition overlaps the set of columns being updated.
   1.613 -**
   1.614 -** The returned bit vector is some combination of TRIGGER_BEFORE and
   1.615 -** TRIGGER_AFTER.
   1.616 -*/
   1.617 -int sqlite3TriggersExist(
   1.618 -  Parse *pParse,          /* Used to check for recursive triggers */
   1.619 -  Table *pTab,            /* The table the contains the triggers */
   1.620 -  int op,                 /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
   1.621 -  ExprList *pChanges      /* Columns that change in an UPDATE statement */
   1.622 -){
   1.623 -  Trigger *pTrigger;
   1.624 -  int mask = 0;
   1.625 -
   1.626 -  pTrigger = IsVirtual(pTab) ? 0 : pTab->pTrigger;
   1.627 -  while( pTrigger ){
   1.628 -    if( pTrigger->op==op && checkColumnOverLap(pTrigger->pColumns, pChanges) ){
   1.629 -      mask |= pTrigger->tr_tm;
   1.630 -    }
   1.631 -    pTrigger = pTrigger->pNext;
   1.632 -  }
   1.633 -  return mask;
   1.634 -}
   1.635 -
   1.636 -/*
   1.637 -** Convert the pStep->target token into a SrcList and return a pointer
   1.638 -** to that SrcList.
   1.639 -**
   1.640 -** This routine adds a specific database name, if needed, to the target when
   1.641 -** forming the SrcList.  This prevents a trigger in one database from
   1.642 -** referring to a target in another database.  An exception is when the
   1.643 -** trigger is in TEMP in which case it can refer to any other database it
   1.644 -** wants.
   1.645 -*/
   1.646 -static SrcList *targetSrcList(
   1.647 -  Parse *pParse,       /* The parsing context */
   1.648 -  TriggerStep *pStep   /* The trigger containing the target token */
   1.649 -){
   1.650 -  Token sDb;           /* Dummy database name token */
   1.651 -  int iDb;             /* Index of the database to use */
   1.652 -  SrcList *pSrc;       /* SrcList to be returned */
   1.653 -
   1.654 -  iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
   1.655 -  if( iDb==0 || iDb>=2 ){
   1.656 -    assert( iDb<pParse->db->nDb );
   1.657 -    sDb.z = (u8*)pParse->db->aDb[iDb].zName;
   1.658 -    sDb.n = strlen((char*)sDb.z);
   1.659 -    pSrc = sqlite3SrcListAppend(pParse->db, 0, &sDb, &pStep->target);
   1.660 -  } else {
   1.661 -    pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
   1.662 -  }
   1.663 -  return pSrc;
   1.664 -}
   1.665 -
   1.666 -/*
   1.667 -** Generate VDBE code for zero or more statements inside the body of a
   1.668 -** trigger.  
   1.669 -*/
   1.670 -static int codeTriggerProgram(
   1.671 -  Parse *pParse,            /* The parser context */
   1.672 -  TriggerStep *pStepList,   /* List of statements inside the trigger body */
   1.673 -  int orconfin              /* Conflict algorithm. (OE_Abort, etc) */  
   1.674 -){
   1.675 -  TriggerStep * pTriggerStep = pStepList;
   1.676 -  int orconf;
   1.677 -  Vdbe *v = pParse->pVdbe;
   1.678 -  sqlite3 *db = pParse->db;
   1.679 -
   1.680 -  assert( pTriggerStep!=0 );
   1.681 -  assert( v!=0 );
   1.682 -  sqlite3VdbeAddOp(v, OP_ContextPush, 0, 0);
   1.683 -  VdbeComment((v, "# begin trigger %s", pStepList->pTrig->name));
   1.684 -  while( pTriggerStep ){
   1.685 -    orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin;
   1.686 -    pParse->trigStack->orconf = orconf;
   1.687 -    switch( pTriggerStep->op ){
   1.688 -      case TK_SELECT: {
   1.689 -        Select *ss = sqlite3SelectDup(db, pTriggerStep->pSelect);
   1.690 -        if( ss ){
   1.691 -          sqlite3SelectResolve(pParse, ss, 0);
   1.692 -          sqlite3Select(pParse, ss, SRT_Discard, 0, 0, 0, 0, 0);
   1.693 -          sqlite3SelectDelete(ss);
   1.694 -        }
   1.695 -        break;
   1.696 -      }
   1.697 -      case TK_UPDATE: {
   1.698 -        SrcList *pSrc;
   1.699 -        pSrc = targetSrcList(pParse, pTriggerStep);
   1.700 -        sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0);
   1.701 -        sqlite3Update(pParse, pSrc,
   1.702 -                sqlite3ExprListDup(db, pTriggerStep->pExprList), 
   1.703 -                sqlite3ExprDup(db, pTriggerStep->pWhere), orconf);
   1.704 -        sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0);
   1.705 -        break;
   1.706 -      }
   1.707 -      case TK_INSERT: {
   1.708 -        SrcList *pSrc;
   1.709 -        pSrc = targetSrcList(pParse, pTriggerStep);
   1.710 -        sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0);
   1.711 -        sqlite3Insert(pParse, pSrc,
   1.712 -          sqlite3ExprListDup(db, pTriggerStep->pExprList), 
   1.713 -          sqlite3SelectDup(db, pTriggerStep->pSelect), 
   1.714 -          sqlite3IdListDup(db, pTriggerStep->pIdList), orconf);
   1.715 -        sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0);
   1.716 -        break;
   1.717 -      }
   1.718 -      case TK_DELETE: {
   1.719 -        SrcList *pSrc;
   1.720 -        sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0);
   1.721 -        pSrc = targetSrcList(pParse, pTriggerStep);
   1.722 -        sqlite3DeleteFrom(pParse, pSrc, 
   1.723 -                          sqlite3ExprDup(db, pTriggerStep->pWhere));
   1.724 -        sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0);
   1.725 -        break;
   1.726 -      }
   1.727 -      default:
   1.728 -        assert(0);
   1.729 -    } 
   1.730 -    pTriggerStep = pTriggerStep->pNext;
   1.731 -  }
   1.732 -  sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
   1.733 -  VdbeComment((v, "# end trigger %s", pStepList->pTrig->name));
   1.734 -
   1.735 -  return 0;
   1.736 -}
   1.737 -
   1.738 -/*
   1.739 -** This is called to code FOR EACH ROW triggers.
   1.740 -**
   1.741 -** When the code that this function generates is executed, the following 
   1.742 -** must be true:
   1.743 -**
   1.744 -** 1. No cursors may be open in the main database.  (But newIdx and oldIdx
   1.745 -**    can be indices of cursors in temporary tables.  See below.)
   1.746 -**
   1.747 -** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then
   1.748 -**    a temporary vdbe cursor (index newIdx) must be open and pointing at
   1.749 -**    a row containing values to be substituted for new.* expressions in the
   1.750 -**    trigger program(s).
   1.751 -**
   1.752 -** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then
   1.753 -**    a temporary vdbe cursor (index oldIdx) must be open and pointing at
   1.754 -**    a row containing values to be substituted for old.* expressions in the
   1.755 -**    trigger program(s).
   1.756 -**
   1.757 -*/
   1.758 -int sqlite3CodeRowTrigger(
   1.759 -  Parse *pParse,       /* Parse context */
   1.760 -  int op,              /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
   1.761 -  ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
   1.762 -  int tr_tm,           /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
   1.763 -  Table *pTab,         /* The table to code triggers from */
   1.764 -  int newIdx,          /* The indice of the "new" row to access */
   1.765 -  int oldIdx,          /* The indice of the "old" row to access */
   1.766 -  int orconf,          /* ON CONFLICT policy */
   1.767 -  int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
   1.768 -){
   1.769 -  Trigger *p;
   1.770 -  TriggerStack trigStackEntry;
   1.771 -
   1.772 -  assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE);
   1.773 -  assert(tr_tm == TRIGGER_BEFORE || tr_tm == TRIGGER_AFTER );
   1.774 -
   1.775 -  assert(newIdx != -1 || oldIdx != -1);
   1.776 -
   1.777 -  for(p=pTab->pTrigger; p; p=p->pNext){
   1.778 -    int fire_this = 0;
   1.779 -
   1.780 -    /* Determine whether we should code this trigger */
   1.781 -    if( 
   1.782 -      p->op==op && 
   1.783 -      p->tr_tm==tr_tm && 
   1.784 -      (p->pSchema==p->pTabSchema || p->pSchema==pParse->db->aDb[1].pSchema) &&
   1.785 -      (op!=TK_UPDATE||!p->pColumns||checkColumnOverLap(p->pColumns,pChanges))
   1.786 -    ){
   1.787 -      TriggerStack *pS;      /* Pointer to trigger-stack entry */
   1.788 -      for(pS=pParse->trigStack; pS && p!=pS->pTrigger; pS=pS->pNext){}
   1.789 -      if( !pS ){
   1.790 -        fire_this = 1;
   1.791 -      }
   1.792 -#if 0    /* Give no warning for recursive triggers.  Just do not do them */
   1.793 -      else{
   1.794 -        sqlite3ErrorMsg(pParse, "recursive triggers not supported (%s)",
   1.795 -            p->name);
   1.796 -        return SQLITE_ERROR;
   1.797 -      }
   1.798 -#endif
   1.799 -    }
   1.800 - 
   1.801 -    if( fire_this ){
   1.802 -      int endTrigger;
   1.803 -      Expr * whenExpr;
   1.804 -      AuthContext sContext;
   1.805 -      NameContext sNC;
   1.806 -
   1.807 -      memset(&sNC, 0, sizeof(sNC));
   1.808 -      sNC.pParse = pParse;
   1.809 -
   1.810 -      /* Push an entry on to the trigger stack */
   1.811 -      trigStackEntry.pTrigger = p;
   1.812 -      trigStackEntry.newIdx = newIdx;
   1.813 -      trigStackEntry.oldIdx = oldIdx;
   1.814 -      trigStackEntry.pTab = pTab;
   1.815 -      trigStackEntry.pNext = pParse->trigStack;
   1.816 -      trigStackEntry.ignoreJump = ignoreJump;
   1.817 -      pParse->trigStack = &trigStackEntry;
   1.818 -      sqlite3AuthContextPush(pParse, &sContext, p->name);
   1.819 -
   1.820 -      /* code the WHEN clause */
   1.821 -      endTrigger = sqlite3VdbeMakeLabel(pParse->pVdbe);
   1.822 -      whenExpr = sqlite3ExprDup(pParse->db, p->pWhen);
   1.823 -      if( sqlite3ExprResolveNames(&sNC, whenExpr) ){
   1.824 -        pParse->trigStack = trigStackEntry.pNext;
   1.825 -        sqlite3ExprDelete(whenExpr);
   1.826 -        return 1;
   1.827 -      }
   1.828 -      sqlite3ExprIfFalse(pParse, whenExpr, endTrigger, 1);
   1.829 -      sqlite3ExprDelete(whenExpr);
   1.830 -
   1.831 -      codeTriggerProgram(pParse, p->step_list, orconf); 
   1.832 -
   1.833 -      /* Pop the entry off the trigger stack */
   1.834 -      pParse->trigStack = trigStackEntry.pNext;
   1.835 -      sqlite3AuthContextPop(&sContext);
   1.836 -
   1.837 -      sqlite3VdbeResolveLabel(pParse->pVdbe, endTrigger);
   1.838 -    }
   1.839 -  }
   1.840 -  return 0;
   1.841 -}
   1.842 -#endif /* !defined(SQLITE_OMIT_TRIGGER) */