You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

764 lines
25 KiB

  1. /*
  2. **
  3. ** The author disclaims copyright to this source code. In place of
  4. ** a legal notice, here is a blessing:
  5. **
  6. ** May you do good and not evil.
  7. ** May you find forgiveness for yourself and forgive others.
  8. ** May you share freely, never taking more than you give.
  9. **
  10. *************************************************************************
  11. *
  12. */
  13. #include "sqliteInt.h"
  14. /*
  15. ** Delete a linked list of TriggerStep structures.
  16. */
  17. void sqliteDeleteTriggerStep(TriggerStep *pTriggerStep){
  18. while( pTriggerStep ){
  19. TriggerStep * pTmp = pTriggerStep;
  20. pTriggerStep = pTriggerStep->pNext;
  21. if( pTmp->target.dyn ) sqliteFree((char*)pTmp->target.z);
  22. sqliteExprDelete(pTmp->pWhere);
  23. sqliteExprListDelete(pTmp->pExprList);
  24. sqliteSelectDelete(pTmp->pSelect);
  25. sqliteIdListDelete(pTmp->pIdList);
  26. sqliteFree(pTmp);
  27. }
  28. }
  29. /*
  30. ** This is called by the parser when it sees a CREATE TRIGGER statement
  31. ** up to the point of the BEGIN before the trigger actions. A Trigger
  32. ** structure is generated based on the information available and stored
  33. ** in pParse->pNewTrigger. After the trigger actions have been parsed, the
  34. ** sqliteFinishTrigger() function is called to complete the trigger
  35. ** construction process.
  36. */
  37. void sqliteBeginTrigger(
  38. Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
  39. Token *pName, /* The name of the trigger */
  40. int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
  41. int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
  42. IdList *pColumns, /* column list if this is an UPDATE OF trigger */
  43. SrcList *pTableName,/* The name of the table/view the trigger applies to */
  44. int foreach, /* One of TK_ROW or TK_STATEMENT */
  45. Expr *pWhen, /* WHEN clause */
  46. int isTemp /* True if the TEMPORARY keyword is present */
  47. ){
  48. Trigger *nt;
  49. Table *tab;
  50. char *zName = 0; /* Name of the trigger */
  51. sqlite *db = pParse->db;
  52. int iDb; /* When database to store the trigger in */
  53. DbFixer sFix;
  54. /* Check that:
  55. ** 1. the trigger name does not already exist.
  56. ** 2. the table (or view) does exist in the same database as the trigger.
  57. ** 3. that we are not trying to create a trigger on the sqlite_master table
  58. ** 4. That we are not trying to create an INSTEAD OF trigger on a table.
  59. ** 5. That we are not trying to create a BEFORE or AFTER trigger on a view.
  60. */
  61. if( sqlite_malloc_failed ) goto trigger_cleanup;
  62. assert( pTableName->nSrc==1 );
  63. if( db->init.busy
  64. && sqliteFixInit(&sFix, pParse, db->init.iDb, "trigger", pName)
  65. && sqliteFixSrcList(&sFix, pTableName)
  66. ){
  67. goto trigger_cleanup;
  68. }
  69. tab = sqliteSrcListLookup(pParse, pTableName);
  70. if( !tab ){
  71. goto trigger_cleanup;
  72. }
  73. iDb = isTemp ? 1 : tab->iDb;
  74. if( iDb>=2 && !db->init.busy ){
  75. sqliteErrorMsg(pParse, "triggers may not be added to auxiliary "
  76. "database %s", db->aDb[tab->iDb].zName);
  77. goto trigger_cleanup;
  78. }
  79. zName = sqliteStrNDup(pName->z, pName->n);
  80. sqliteDequote(zName);
  81. if( sqliteHashFind(&(db->aDb[iDb].trigHash), zName,pName->n+1) ){
  82. sqliteErrorMsg(pParse, "trigger %T already exists", pName);
  83. goto trigger_cleanup;
  84. }
  85. if( sqliteStrNICmp(tab->zName, "sqlite_", 7)==0 ){
  86. sqliteErrorMsg(pParse, "cannot create trigger on system table");
  87. pParse->nErr++;
  88. goto trigger_cleanup;
  89. }
  90. if( tab->pSelect && tr_tm != TK_INSTEAD ){
  91. sqliteErrorMsg(pParse, "cannot create %s trigger on view: %S",
  92. (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
  93. goto trigger_cleanup;
  94. }
  95. if( !tab->pSelect && tr_tm == TK_INSTEAD ){
  96. sqliteErrorMsg(pParse, "cannot create INSTEAD OF"
  97. " trigger on table: %S", pTableName, 0);
  98. goto trigger_cleanup;
  99. }
  100. #ifndef SQLITE_OMIT_AUTHORIZATION
  101. {
  102. int code = SQLITE_CREATE_TRIGGER;
  103. const char *zDb = db->aDb[tab->iDb].zName;
  104. const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
  105. if( tab->iDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
  106. if( sqliteAuthCheck(pParse, code, zName, tab->zName, zDbTrig) ){
  107. goto trigger_cleanup;
  108. }
  109. if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(tab->iDb), 0, zDb)){
  110. goto trigger_cleanup;
  111. }
  112. }
  113. #endif
  114. /* INSTEAD OF triggers can only appear on views and BEGIN triggers
  115. ** cannot appear on views. So we might as well translate every
  116. ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
  117. ** elsewhere.
  118. */
  119. if (tr_tm == TK_INSTEAD){
  120. tr_tm = TK_BEFORE;
  121. }
  122. /* Build the Trigger object */
  123. nt = (Trigger*)sqliteMalloc(sizeof(Trigger));
  124. if( nt==0 ) goto trigger_cleanup;
  125. nt->name = zName;
  126. zName = 0;
  127. nt->table = sqliteStrDup(pTableName->a[0].zName);
  128. if( sqlite_malloc_failed ) goto trigger_cleanup;
  129. nt->iDb = iDb;
  130. nt->iTabDb = tab->iDb;
  131. nt->op = op;
  132. nt->tr_tm = tr_tm;
  133. nt->pWhen = sqliteExprDup(pWhen);
  134. nt->pColumns = sqliteIdListDup(pColumns);
  135. nt->foreach = foreach;
  136. sqliteTokenCopy(&nt->nameToken,pName);
  137. assert( pParse->pNewTrigger==0 );
  138. pParse->pNewTrigger = nt;
  139. trigger_cleanup:
  140. sqliteFree(zName);
  141. sqliteSrcListDelete(pTableName);
  142. sqliteIdListDelete(pColumns);
  143. sqliteExprDelete(pWhen);
  144. }
  145. /*
  146. ** This routine is called after all of the trigger actions have been parsed
  147. ** in order to complete the process of building the trigger.
  148. */
  149. void sqliteFinishTrigger(
  150. Parse *pParse, /* Parser context */
  151. TriggerStep *pStepList, /* The triggered program */
  152. Token *pAll /* Token that describes the complete CREATE TRIGGER */
  153. ){
  154. Trigger *nt = 0; /* The trigger whose construction is finishing up */
  155. sqlite *db = pParse->db; /* The database */
  156. DbFixer sFix;
  157. if( pParse->nErr || pParse->pNewTrigger==0 ) goto triggerfinish_cleanup;
  158. nt = pParse->pNewTrigger;
  159. pParse->pNewTrigger = 0;
  160. nt->step_list = pStepList;
  161. while( pStepList ){
  162. pStepList->pTrig = nt;
  163. pStepList = pStepList->pNext;
  164. }
  165. if( sqliteFixInit(&sFix, pParse, nt->iDb, "trigger", &nt->nameToken)
  166. && sqliteFixTriggerStep(&sFix, nt->step_list) ){
  167. goto triggerfinish_cleanup;
  168. }
  169. /* if we are not initializing, and this trigger is not on a TEMP table,
  170. ** build the sqlite_master entry
  171. */
  172. if( !db->init.busy ){
  173. static VdbeOpList insertTrig[] = {
  174. { OP_NewRecno, 0, 0, 0 },
  175. { OP_String, 0, 0, "trigger" },
  176. { OP_String, 0, 0, 0 }, /* 2: trigger name */
  177. { OP_String, 0, 0, 0 }, /* 3: table name */
  178. { OP_Integer, 0, 0, 0 },
  179. { OP_String, 0, 0, 0 }, /* 5: SQL */
  180. { OP_MakeRecord, 5, 0, 0 },
  181. { OP_PutIntKey, 0, 0, 0 },
  182. };
  183. int addr;
  184. Vdbe *v;
  185. /* Make an entry in the sqlite_master table */
  186. v = sqliteGetVdbe(pParse);
  187. if( v==0 ) goto triggerfinish_cleanup;
  188. sqliteBeginWriteOperation(pParse, 0, 0);
  189. sqliteOpenMasterTable(v, nt->iDb);
  190. addr = sqliteVdbeAddOpList(v, ArraySize(insertTrig), insertTrig);
  191. sqliteVdbeChangeP3(v, addr+2, nt->name, 0);
  192. sqliteVdbeChangeP3(v, addr+3, nt->table, 0);
  193. sqliteVdbeChangeP3(v, addr+5, pAll->z, pAll->n);
  194. if( nt->iDb==0 ){
  195. sqliteChangeCookie(db, v);
  196. }
  197. sqliteVdbeAddOp(v, OP_Close, 0, 0);
  198. sqliteEndWriteOperation(pParse);
  199. }
  200. if( !pParse->explain ){
  201. Table *pTab;
  202. sqliteHashInsert(&db->aDb[nt->iDb].trigHash,
  203. nt->name, strlen(nt->name)+1, nt);
  204. pTab = sqliteLocateTable(pParse, nt->table, db->aDb[nt->iTabDb].zName);
  205. assert( pTab!=0 );
  206. nt->pNext = pTab->pTrigger;
  207. pTab->pTrigger = nt;
  208. nt = 0;
  209. }
  210. triggerfinish_cleanup:
  211. sqliteDeleteTrigger(nt);
  212. sqliteDeleteTrigger(pParse->pNewTrigger);
  213. pParse->pNewTrigger = 0;
  214. sqliteDeleteTriggerStep(pStepList);
  215. }
  216. /*
  217. ** Make a copy of all components of the given trigger step. This has
  218. ** the effect of copying all Expr.token.z values into memory obtained
  219. ** from sqliteMalloc(). As initially created, the Expr.token.z values
  220. ** all point to the input string that was fed to the parser. But that
  221. ** string is ephemeral - it will go away as soon as the sqlite_exec()
  222. ** call that started the parser exits. This routine makes a persistent
  223. ** copy of all the Expr.token.z strings so that the TriggerStep structure
  224. ** will be valid even after the sqlite_exec() call returns.
  225. */
  226. static void sqlitePersistTriggerStep(TriggerStep *p){
  227. if( p->target.z ){
  228. p->target.z = sqliteStrNDup(p->target.z, p->target.n);
  229. p->target.dyn = 1;
  230. }
  231. if( p->pSelect ){
  232. Select *pNew = sqliteSelectDup(p->pSelect);
  233. sqliteSelectDelete(p->pSelect);
  234. p->pSelect = pNew;
  235. }
  236. if( p->pWhere ){
  237. Expr *pNew = sqliteExprDup(p->pWhere);
  238. sqliteExprDelete(p->pWhere);
  239. p->pWhere = pNew;
  240. }
  241. if( p->pExprList ){
  242. ExprList *pNew = sqliteExprListDup(p->pExprList);
  243. sqliteExprListDelete(p->pExprList);
  244. p->pExprList = pNew;
  245. }
  246. if( p->pIdList ){
  247. IdList *pNew = sqliteIdListDup(p->pIdList);
  248. sqliteIdListDelete(p->pIdList);
  249. p->pIdList = pNew;
  250. }
  251. }
  252. /*
  253. ** Turn a SELECT statement (that the pSelect parameter points to) into
  254. ** a trigger step. Return a pointer to a TriggerStep structure.
  255. **
  256. ** The parser calls this routine when it finds a SELECT statement in
  257. ** body of a TRIGGER.
  258. */
  259. TriggerStep *sqliteTriggerSelectStep(Select *pSelect){
  260. TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
  261. if( pTriggerStep==0 ) return 0;
  262. pTriggerStep->op = TK_SELECT;
  263. pTriggerStep->pSelect = pSelect;
  264. pTriggerStep->orconf = OE_Default;
  265. sqlitePersistTriggerStep(pTriggerStep);
  266. return pTriggerStep;
  267. }
  268. /*
  269. ** Build a trigger step out of an INSERT statement. Return a pointer
  270. ** to the new trigger step.
  271. **
  272. ** The parser calls this routine when it sees an INSERT inside the
  273. ** body of a trigger.
  274. */
  275. TriggerStep *sqliteTriggerInsertStep(
  276. Token *pTableName, /* Name of the table into which we insert */
  277. IdList *pColumn, /* List of columns in pTableName to insert into */
  278. ExprList *pEList, /* The VALUE clause: a list of values to be inserted */
  279. Select *pSelect, /* A SELECT statement that supplies values */
  280. int orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
  281. ){
  282. TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
  283. if( pTriggerStep==0 ) return 0;
  284. assert(pEList == 0 || pSelect == 0);
  285. assert(pEList != 0 || pSelect != 0);
  286. pTriggerStep->op = TK_INSERT;
  287. pTriggerStep->pSelect = pSelect;
  288. pTriggerStep->target = *pTableName;
  289. pTriggerStep->pIdList = pColumn;
  290. pTriggerStep->pExprList = pEList;
  291. pTriggerStep->orconf = orconf;
  292. sqlitePersistTriggerStep(pTriggerStep);
  293. return pTriggerStep;
  294. }
  295. /*
  296. ** Construct a trigger step that implements an UPDATE statement and return
  297. ** a pointer to that trigger step. The parser calls this routine when it
  298. ** sees an UPDATE statement inside the body of a CREATE TRIGGER.
  299. */
  300. TriggerStep *sqliteTriggerUpdateStep(
  301. Token *pTableName, /* Name of the table to be updated */
  302. ExprList *pEList, /* The SET clause: list of column and new values */
  303. Expr *pWhere, /* The WHERE clause */
  304. int orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
  305. ){
  306. TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
  307. if( pTriggerStep==0 ) return 0;
  308. pTriggerStep->op = TK_UPDATE;
  309. pTriggerStep->target = *pTableName;
  310. pTriggerStep->pExprList = pEList;
  311. pTriggerStep->pWhere = pWhere;
  312. pTriggerStep->orconf = orconf;
  313. sqlitePersistTriggerStep(pTriggerStep);
  314. return pTriggerStep;
  315. }
  316. /*
  317. ** Construct a trigger step that implements a DELETE statement and return
  318. ** a pointer to that trigger step. The parser calls this routine when it
  319. ** sees a DELETE statement inside the body of a CREATE TRIGGER.
  320. */
  321. TriggerStep *sqliteTriggerDeleteStep(Token *pTableName, Expr *pWhere){
  322. TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
  323. if( pTriggerStep==0 ) return 0;
  324. pTriggerStep->op = TK_DELETE;
  325. pTriggerStep->target = *pTableName;
  326. pTriggerStep->pWhere = pWhere;
  327. pTriggerStep->orconf = OE_Default;
  328. sqlitePersistTriggerStep(pTriggerStep);
  329. return pTriggerStep;
  330. }
  331. /*
  332. ** Recursively delete a Trigger structure
  333. */
  334. void sqliteDeleteTrigger(Trigger *pTrigger){
  335. if( pTrigger==0 ) return;
  336. sqliteDeleteTriggerStep(pTrigger->step_list);
  337. sqliteFree(pTrigger->name);
  338. sqliteFree(pTrigger->table);
  339. sqliteExprDelete(pTrigger->pWhen);
  340. sqliteIdListDelete(pTrigger->pColumns);
  341. if( pTrigger->nameToken.dyn ) sqliteFree((char*)pTrigger->nameToken.z);
  342. sqliteFree(pTrigger);
  343. }
  344. /*
  345. * This function is called to drop a trigger from the database schema.
  346. *
  347. * This may be called directly from the parser and therefore identifies
  348. * the trigger by name. The sqliteDropTriggerPtr() routine does the
  349. * same job as this routine except it take a spointer to the trigger
  350. * instead of the trigger name.
  351. *
  352. * Note that this function does not delete the trigger entirely. Instead it
  353. * removes it from the internal schema and places it in the trigDrop hash
  354. * table. This is so that the trigger can be restored into the database schema
  355. * if the transaction is rolled back.
  356. */
  357. void sqliteDropTrigger(Parse *pParse, SrcList *pName){
  358. Trigger *pTrigger;
  359. int i;
  360. const char *zDb;
  361. const char *zName;
  362. int nName;
  363. sqlite *db = pParse->db;
  364. if( sqlite_malloc_failed ) goto drop_trigger_cleanup;
  365. assert( pName->nSrc==1 );
  366. zDb = pName->a[0].zDatabase;
  367. zName = pName->a[0].zName;
  368. nName = strlen(zName);
  369. for(i=0; i<db->nDb; i++){
  370. int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
  371. if( zDb && sqliteStrICmp(db->aDb[j].zName, zDb) ) continue;
  372. pTrigger = sqliteHashFind(&(db->aDb[j].trigHash), zName, nName+1);
  373. if( pTrigger ) break;
  374. }
  375. if( !pTrigger ){
  376. sqliteErrorMsg(pParse, "no such trigger: %S", pName, 0);
  377. goto drop_trigger_cleanup;
  378. }
  379. sqliteDropTriggerPtr(pParse, pTrigger, 0);
  380. drop_trigger_cleanup:
  381. sqliteSrcListDelete(pName);
  382. }
  383. /*
  384. ** Drop a trigger given a pointer to that trigger. If nested is false,
  385. ** then also generate code to remove the trigger from the SQLITE_MASTER
  386. ** table.
  387. */
  388. void sqliteDropTriggerPtr(Parse *pParse, Trigger *pTrigger, int nested){
  389. Table *pTable;
  390. Vdbe *v;
  391. sqlite *db = pParse->db;
  392. assert( pTrigger->iDb<db->nDb );
  393. if( pTrigger->iDb>=2 ){
  394. sqliteErrorMsg(pParse, "triggers may not be removed from "
  395. "auxiliary database %s", db->aDb[pTrigger->iDb].zName);
  396. return;
  397. }
  398. pTable = sqliteFindTable(db, pTrigger->table,db->aDb[pTrigger->iTabDb].zName);
  399. assert(pTable);
  400. assert( pTable->iDb==pTrigger->iDb || pTrigger->iDb==1 );
  401. #ifndef SQLITE_OMIT_AUTHORIZATION
  402. {
  403. int code = SQLITE_DROP_TRIGGER;
  404. const char *zDb = db->aDb[pTrigger->iDb].zName;
  405. const char *zTab = SCHEMA_TABLE(pTrigger->iDb);
  406. if( pTrigger->iDb ) code = SQLITE_DROP_TEMP_TRIGGER;
  407. if( sqliteAuthCheck(pParse, code, pTrigger->name, pTable->zName, zDb) ||
  408. sqliteAuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
  409. return;
  410. }
  411. }
  412. #endif
  413. /* Generate code to destroy the database record of the trigger.
  414. */
  415. if( pTable!=0 && !nested && (v = sqliteGetVdbe(pParse))!=0 ){
  416. int base;
  417. static VdbeOpList dropTrigger[] = {
  418. { OP_Rewind, 0, ADDR(9), 0},
  419. { OP_String, 0, 0, 0}, /* 1 */
  420. { OP_Column, 0, 1, 0},
  421. { OP_Ne, 0, ADDR(8), 0},
  422. { OP_String, 0, 0, "trigger"},
  423. { OP_Column, 0, 0, 0},
  424. { OP_Ne, 0, ADDR(8), 0},
  425. { OP_Delete, 0, 0, 0},
  426. { OP_Next, 0, ADDR(1), 0}, /* 8 */
  427. };
  428. sqliteBeginWriteOperation(pParse, 0, 0);
  429. sqliteOpenMasterTable(v, pTrigger->iDb);
  430. base = sqliteVdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger);
  431. sqliteVdbeChangeP3(v, base+1, pTrigger->name, 0);
  432. if( pTrigger->iDb==0 ){
  433. sqliteChangeCookie(db, v);
  434. }
  435. sqliteVdbeAddOp(v, OP_Close, 0, 0);
  436. sqliteEndWriteOperation(pParse);
  437. }
  438. /*
  439. * If this is not an "explain", then delete the trigger structure.
  440. */
  441. if( !pParse->explain ){
  442. const char *zName = pTrigger->name;
  443. int nName = strlen(zName);
  444. if( pTable->pTrigger == pTrigger ){
  445. pTable->pTrigger = pTrigger->pNext;
  446. }else{
  447. Trigger *cc = pTable->pTrigger;
  448. while( cc ){
  449. if( cc->pNext == pTrigger ){
  450. cc->pNext = cc->pNext->pNext;
  451. break;
  452. }
  453. cc = cc->pNext;
  454. }
  455. assert(cc);
  456. }
  457. sqliteHashInsert(&(db->aDb[pTrigger->iDb].trigHash), zName, nName+1, 0);
  458. sqliteDeleteTrigger(pTrigger);
  459. }
  460. }
  461. /*
  462. ** pEList is the SET clause of an UPDATE statement. Each entry
  463. ** in pEList is of the format <id>=<expr>. If any of the entries
  464. ** in pEList have an <id> which matches an identifier in pIdList,
  465. ** then return TRUE. If pIdList==NULL, then it is considered a
  466. ** wildcard that matches anything. Likewise if pEList==NULL then
  467. ** it matches anything so always return true. Return false only
  468. ** if there is no match.
  469. */
  470. static int checkColumnOverLap(IdList *pIdList, ExprList *pEList){
  471. int e;
  472. if( !pIdList || !pEList ) return 1;
  473. for(e=0; e<pEList->nExpr; e++){
  474. if( sqliteIdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
  475. }
  476. return 0;
  477. }
  478. /* A global variable that is TRUE if we should always set up temp tables for
  479. * for triggers, even if there are no triggers to code. This is used to test
  480. * how much overhead the triggers algorithm is causing.
  481. *
  482. * This flag can be set or cleared using the "trigger_overhead_test" pragma.
  483. * The pragma is not documented since it is not really part of the interface
  484. * to SQLite, just the test procedure.
  485. */
  486. int always_code_trigger_setup = 0;
  487. /*
  488. * Returns true if a trigger matching op, tr_tm and foreach that is NOT already
  489. * on the Parse objects trigger-stack (to prevent recursive trigger firing) is
  490. * found in the list specified as pTrigger.
  491. */
  492. int sqliteTriggersExist(
  493. Parse *pParse, /* Used to check for recursive triggers */
  494. Trigger *pTrigger, /* A list of triggers associated with a table */
  495. int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
  496. int tr_tm, /* one of TK_BEFORE, TK_AFTER */
  497. int foreach, /* one of TK_ROW or TK_STATEMENT */
  498. ExprList *pChanges /* Columns that change in an UPDATE statement */
  499. ){
  500. Trigger * pTriggerCursor;
  501. if( always_code_trigger_setup ){
  502. return 1;
  503. }
  504. pTriggerCursor = pTrigger;
  505. while( pTriggerCursor ){
  506. if( pTriggerCursor->op == op &&
  507. pTriggerCursor->tr_tm == tr_tm &&
  508. pTriggerCursor->foreach == foreach &&
  509. checkColumnOverLap(pTriggerCursor->pColumns, pChanges) ){
  510. TriggerStack * ss;
  511. ss = pParse->trigStack;
  512. while( ss && ss->pTrigger != pTrigger ){
  513. ss = ss->pNext;
  514. }
  515. if( !ss )return 1;
  516. }
  517. pTriggerCursor = pTriggerCursor->pNext;
  518. }
  519. return 0;
  520. }
  521. /*
  522. ** Convert the pStep->target token into a SrcList and return a pointer
  523. ** to that SrcList.
  524. **
  525. ** This routine adds a specific database name, if needed, to the target when
  526. ** forming the SrcList. This prevents a trigger in one database from
  527. ** referring to a target in another database. An exception is when the
  528. ** trigger is in TEMP in which case it can refer to any other database it
  529. ** wants.
  530. */
  531. static SrcList *targetSrcList(
  532. Parse *pParse, /* The parsing context */
  533. TriggerStep *pStep /* The trigger containing the target token */
  534. ){
  535. Token sDb; /* Dummy database name token */
  536. int iDb; /* Index of the database to use */
  537. SrcList *pSrc; /* SrcList to be returned */
  538. iDb = pStep->pTrig->iDb;
  539. if( iDb==0 || iDb>=2 ){
  540. assert( iDb<pParse->db->nDb );
  541. sDb.z = pParse->db->aDb[iDb].zName;
  542. sDb.n = strlen(sDb.z);
  543. pSrc = sqliteSrcListAppend(0, &sDb, &pStep->target);
  544. } else {
  545. pSrc = sqliteSrcListAppend(0, &pStep->target, 0);
  546. }
  547. return pSrc;
  548. }
  549. /*
  550. ** Generate VDBE code for zero or more statements inside the body of a
  551. ** trigger.
  552. */
  553. static int codeTriggerProgram(
  554. Parse *pParse, /* The parser context */
  555. TriggerStep *pStepList, /* List of statements inside the trigger body */
  556. int orconfin /* Conflict algorithm. (OE_Abort, etc) */
  557. ){
  558. TriggerStep * pTriggerStep = pStepList;
  559. int orconf;
  560. while( pTriggerStep ){
  561. int saveNTab = pParse->nTab;
  562. orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin;
  563. pParse->trigStack->orconf = orconf;
  564. switch( pTriggerStep->op ){
  565. case TK_SELECT: {
  566. Select * ss = sqliteSelectDup(pTriggerStep->pSelect);
  567. assert(ss);
  568. assert(ss->pSrc);
  569. sqliteSelect(pParse, ss, SRT_Discard, 0, 0, 0, 0);
  570. sqliteSelectDelete(ss);
  571. break;
  572. }
  573. case TK_UPDATE: {
  574. SrcList *pSrc;
  575. pSrc = targetSrcList(pParse, pTriggerStep);
  576. sqliteVdbeAddOp(pParse->pVdbe, OP_ListPush, 0, 0);
  577. sqliteUpdate(pParse, pSrc,
  578. sqliteExprListDup(pTriggerStep->pExprList),
  579. sqliteExprDup(pTriggerStep->pWhere), orconf);
  580. sqliteVdbeAddOp(pParse->pVdbe, OP_ListPop, 0, 0);
  581. break;
  582. }
  583. case TK_INSERT: {
  584. SrcList *pSrc;
  585. pSrc = targetSrcList(pParse, pTriggerStep);
  586. sqliteInsert(pParse, pSrc,
  587. sqliteExprListDup(pTriggerStep->pExprList),
  588. sqliteSelectDup(pTriggerStep->pSelect),
  589. sqliteIdListDup(pTriggerStep->pIdList), orconf);
  590. break;
  591. }
  592. case TK_DELETE: {
  593. SrcList *pSrc;
  594. sqliteVdbeAddOp(pParse->pVdbe, OP_ListPush, 0, 0);
  595. pSrc = targetSrcList(pParse, pTriggerStep);
  596. sqliteDeleteFrom(pParse, pSrc, sqliteExprDup(pTriggerStep->pWhere));
  597. sqliteVdbeAddOp(pParse->pVdbe, OP_ListPop, 0, 0);
  598. break;
  599. }
  600. default:
  601. assert(0);
  602. }
  603. pParse->nTab = saveNTab;
  604. pTriggerStep = pTriggerStep->pNext;
  605. }
  606. return 0;
  607. }
  608. /*
  609. ** This is called to code FOR EACH ROW triggers.
  610. **
  611. ** When the code that this function generates is executed, the following
  612. ** must be true:
  613. **
  614. ** 1. No cursors may be open in the main database. (But newIdx and oldIdx
  615. ** can be indices of cursors in temporary tables. See below.)
  616. **
  617. ** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then
  618. ** a temporary vdbe cursor (index newIdx) must be open and pointing at
  619. ** a row containing values to be substituted for new.* expressions in the
  620. ** trigger program(s).
  621. **
  622. ** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then
  623. ** a temporary vdbe cursor (index oldIdx) must be open and pointing at
  624. ** a row containing values to be substituted for old.* expressions in the
  625. ** trigger program(s).
  626. **
  627. */
  628. int sqliteCodeRowTrigger(
  629. Parse *pParse, /* Parse context */
  630. int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
  631. ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
  632. int tr_tm, /* One of TK_BEFORE, TK_AFTER */
  633. Table *pTab, /* The table to code triggers from */
  634. int newIdx, /* The indice of the "new" row to access */
  635. int oldIdx, /* The indice of the "old" row to access */
  636. int orconf, /* ON CONFLICT policy */
  637. int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
  638. ){
  639. Trigger * pTrigger;
  640. TriggerStack * pTriggerStack;
  641. assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE);
  642. assert(tr_tm == TK_BEFORE || tr_tm == TK_AFTER );
  643. assert(newIdx != -1 || oldIdx != -1);
  644. pTrigger = pTab->pTrigger;
  645. while( pTrigger ){
  646. int fire_this = 0;
  647. /* determine whether we should code this trigger */
  648. if( pTrigger->op == op && pTrigger->tr_tm == tr_tm &&
  649. pTrigger->foreach == TK_ROW ){
  650. fire_this = 1;
  651. pTriggerStack = pParse->trigStack;
  652. while( pTriggerStack ){
  653. if( pTriggerStack->pTrigger == pTrigger ){
  654. fire_this = 0;
  655. }
  656. pTriggerStack = pTriggerStack->pNext;
  657. }
  658. if( op == TK_UPDATE && pTrigger->pColumns &&
  659. !checkColumnOverLap(pTrigger->pColumns, pChanges) ){
  660. fire_this = 0;
  661. }
  662. }
  663. if( fire_this && (pTriggerStack = sqliteMalloc(sizeof(TriggerStack)))!=0 ){
  664. int endTrigger;
  665. SrcList dummyTablist;
  666. Expr * whenExpr;
  667. AuthContext sContext;
  668. dummyTablist.nSrc = 0;
  669. /* Push an entry on to the trigger stack */
  670. pTriggerStack->pTrigger = pTrigger;
  671. pTriggerStack->newIdx = newIdx;
  672. pTriggerStack->oldIdx = oldIdx;
  673. pTriggerStack->pTab = pTab;
  674. pTriggerStack->pNext = pParse->trigStack;
  675. pTriggerStack->ignoreJump = ignoreJump;
  676. pParse->trigStack = pTriggerStack;
  677. sqliteAuthContextPush(pParse, &sContext, pTrigger->name);
  678. /* code the WHEN clause */
  679. endTrigger = sqliteVdbeMakeLabel(pParse->pVdbe);
  680. whenExpr = sqliteExprDup(pTrigger->pWhen);
  681. if( sqliteExprResolveIds(pParse, &dummyTablist, 0, whenExpr) ){
  682. pParse->trigStack = pParse->trigStack->pNext;
  683. sqliteFree(pTriggerStack);
  684. sqliteExprDelete(whenExpr);
  685. return 1;
  686. }
  687. sqliteExprIfFalse(pParse, whenExpr, endTrigger, 1);
  688. sqliteExprDelete(whenExpr);
  689. sqliteVdbeAddOp(pParse->pVdbe, OP_ContextPush, 0, 0);
  690. codeTriggerProgram(pParse, pTrigger->step_list, orconf);
  691. sqliteVdbeAddOp(pParse->pVdbe, OP_ContextPop, 0, 0);
  692. /* Pop the entry off the trigger stack */
  693. pParse->trigStack = pParse->trigStack->pNext;
  694. sqliteAuthContextPop(&sContext);
  695. sqliteFree(pTriggerStack);
  696. sqliteVdbeResolveLabel(pParse->pVdbe, endTrigger);
  697. }
  698. pTrigger = pTrigger->pNext;
  699. }
  700. return 0;
  701. }