Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: third_party/sqlite/src/src/trigger.c

Issue 5626002: Update sqlite to 3.7.3. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/third_party/sqlite/src
Patch Set: Remove misc change. Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/sqlite/src/src/tokenize.c ('k') | third_party/sqlite/src/src/update.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 ** 2 **
3 ** The author disclaims copyright to this source code. In place of 3 ** The author disclaims copyright to this source code. In place of
4 ** a legal notice, here is a blessing: 4 ** a legal notice, here is a blessing:
5 ** 5 **
6 ** May you do good and not evil. 6 ** May you do good and not evil.
7 ** May you find forgiveness for yourself and forgive others. 7 ** May you find forgiveness for yourself and forgive others.
8 ** May you share freely, never taking more than you give. 8 ** May you share freely, never taking more than you give.
9 ** 9 **
10 ************************************************************************* 10 *************************************************************************
11 ** 11 ** This file contains the implementation for TRIGGERs
12 **
13 ** $Id: trigger.c,v 1.143 2009/08/10 03:57:58 shane Exp $
14 */ 12 */
15 #include "sqliteInt.h" 13 #include "sqliteInt.h"
16 14
17 #ifndef SQLITE_OMIT_TRIGGER 15 #ifndef SQLITE_OMIT_TRIGGER
18 /* 16 /*
19 ** Delete a linked list of TriggerStep structures. 17 ** Delete a linked list of TriggerStep structures.
20 */ 18 */
21 void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){ 19 void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
22 while( pTriggerStep ){ 20 while( pTriggerStep ){
23 TriggerStep * pTmp = pTriggerStep; 21 TriggerStep * pTmp = pTriggerStep;
(...skipping 19 matching lines...) Expand all
43 ** and returns the combined list. 41 ** and returns the combined list.
44 ** 42 **
45 ** To state it another way: This routine returns a list of all triggers 43 ** To state it another way: This routine returns a list of all triggers
46 ** that fire off of pTab. The list will include any TEMP triggers on 44 ** that fire off of pTab. The list will include any TEMP triggers on
47 ** pTab as well as the triggers lised in pTab->pTrigger. 45 ** pTab as well as the triggers lised in pTab->pTrigger.
48 */ 46 */
49 Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){ 47 Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
50 Schema * const pTmpSchema = pParse->db->aDb[1].pSchema; 48 Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
51 Trigger *pList = 0; /* List of triggers to return */ 49 Trigger *pList = 0; /* List of triggers to return */
52 50
51 if( pParse->disableTriggers ){
52 return 0;
53 }
54
53 if( pTmpSchema!=pTab->pSchema ){ 55 if( pTmpSchema!=pTab->pSchema ){
54 HashElem *p; 56 HashElem *p;
55 for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){ 57 for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
56 Trigger *pTrig = (Trigger *)sqliteHashData(p); 58 Trigger *pTrig = (Trigger *)sqliteHashData(p);
57 if( pTrig->pTabSchema==pTab->pSchema 59 if( pTrig->pTabSchema==pTab->pSchema
58 && 0==sqlite3StrICmp(pTrig->table, pTab->zName) 60 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
59 ){ 61 ){
60 pTrig->pNext = (pList ? pList : pTab->pTrigger); 62 pTrig->pNext = (pList ? pList : pTab->pTrigger);
61 pList = pTrig; 63 pList = pTrig;
62 } 64 }
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 119
118 /* If the trigger name was unqualified, and the table is a temp table, 120 /* If the trigger name was unqualified, and the table is a temp table,
119 ** then set iDb to 1 to create the trigger in the temporary database. 121 ** then set iDb to 1 to create the trigger in the temporary database.
120 ** If sqlite3SrcListLookup() returns 0, indicating the table does not 122 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
121 ** exist, the error is caught by the block below. 123 ** exist, the error is caught by the block below.
122 */ 124 */
123 if( !pTableName || db->mallocFailed ){ 125 if( !pTableName || db->mallocFailed ){
124 goto trigger_cleanup; 126 goto trigger_cleanup;
125 } 127 }
126 pTab = sqlite3SrcListLookup(pParse, pTableName); 128 pTab = sqlite3SrcListLookup(pParse, pTableName);
127 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){ 129 if( db->init.busy==0 && pName2->n==0 && pTab
130 && pTab->pSchema==db->aDb[1].pSchema ){
128 iDb = 1; 131 iDb = 1;
129 } 132 }
130 133
131 /* Ensure the table name matches database name and that the table exists */ 134 /* Ensure the table name matches database name and that the table exists */
132 if( db->mallocFailed ) goto trigger_cleanup; 135 if( db->mallocFailed ) goto trigger_cleanup;
133 assert( pTableName->nSrc==1 ); 136 assert( pTableName->nSrc==1 );
134 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) && 137 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) &&
135 sqlite3FixSrcList(&sFix, pTableName) ){ 138 sqlite3FixSrcList(&sFix, pTableName) ){
136 goto trigger_cleanup; 139 goto trigger_cleanup;
137 } 140 }
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 248
246 /* 249 /*
247 ** This routine is called after all of the trigger actions have been parsed 250 ** This routine is called after all of the trigger actions have been parsed
248 ** in order to complete the process of building the trigger. 251 ** in order to complete the process of building the trigger.
249 */ 252 */
250 void sqlite3FinishTrigger( 253 void sqlite3FinishTrigger(
251 Parse *pParse, /* Parser context */ 254 Parse *pParse, /* Parser context */
252 TriggerStep *pStepList, /* The triggered program */ 255 TriggerStep *pStepList, /* The triggered program */
253 Token *pAll /* Token that describes the complete CREATE TRIGGER */ 256 Token *pAll /* Token that describes the complete CREATE TRIGGER */
254 ){ 257 ){
255 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */ 258 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
256 char *zName; /* Name of trigger */ 259 char *zName; /* Name of trigger */
257 sqlite3 *db = pParse->db; /* The database */ 260 sqlite3 *db = pParse->db; /* The database */
258 DbFixer sFix; 261 DbFixer sFix; /* Fixer object */
259 int iDb; /* Database containing the trigger */ 262 int iDb; /* Database containing the trigger */
260 Token nameToken; /* Trigger name for error reporting */ 263 Token nameToken; /* Trigger name for error reporting */
261 264
262 pTrig = pParse->pNewTrigger; 265 pTrig = pParse->pNewTrigger;
263 pParse->pNewTrigger = 0; 266 pParse->pNewTrigger = 0;
264 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup; 267 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
265 zName = pTrig->zName; 268 zName = pTrig->zName;
266 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema); 269 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
267 pTrig->step_list = pStepList; 270 pTrig->step_list = pStepList;
268 while( pStepList ){ 271 while( pStepList ){
269 pStepList->pTrig = pTrig; 272 pStepList->pTrig = pTrig;
270 pStepList = pStepList->pNext; 273 pStepList = pStepList->pNext;
271 } 274 }
272 nameToken.z = pTrig->zName; 275 nameToken.z = pTrig->zName;
273 nameToken.n = sqlite3Strlen30(nameToken.z); 276 nameToken.n = sqlite3Strlen30(nameToken.z);
274 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken) 277 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken)
275 && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){ 278 && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
276 goto triggerfinish_cleanup; 279 goto triggerfinish_cleanup;
277 } 280 }
278 281
279 /* if we are not initializing, and this trigger is not on a TEMP table, 282 /* if we are not initializing,
280 ** build the sqlite_master entry 283 ** build the sqlite_master entry
281 */ 284 */
282 if( !db->init.busy ){ 285 if( !db->init.busy ){
283 Vdbe *v; 286 Vdbe *v;
284 char *z; 287 char *z;
285 288
286 /* Make an entry in the sqlite_master table */ 289 /* Make an entry in the sqlite_master table */
287 v = sqlite3GetVdbe(pParse); 290 v = sqlite3GetVdbe(pParse);
288 if( v==0 ) goto triggerfinish_cleanup; 291 if( v==0 ) goto triggerfinish_cleanup;
289 sqlite3BeginWriteOperation(pParse, 0, iDb); 292 sqlite3BeginWriteOperation(pParse, 0, iDb);
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
486 for(i=OMIT_TEMPDB; i<db->nDb; i++){ 489 for(i=OMIT_TEMPDB; i<db->nDb; i++){
487 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ 490 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
488 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue; 491 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
489 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName); 492 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
490 if( pTrigger ) break; 493 if( pTrigger ) break;
491 } 494 }
492 if( !pTrigger ){ 495 if( !pTrigger ){
493 if( !noErr ){ 496 if( !noErr ){
494 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0); 497 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
495 } 498 }
499 pParse->checkSchema = 1;
496 goto drop_trigger_cleanup; 500 goto drop_trigger_cleanup;
497 } 501 }
498 sqlite3DropTriggerPtr(pParse, pTrigger); 502 sqlite3DropTriggerPtr(pParse, pTrigger);
499 503
500 drop_trigger_cleanup: 504 drop_trigger_cleanup:
501 sqlite3SrcListDelete(db, pName); 505 sqlite3SrcListDelete(db, pName);
502 } 506 }
503 507
504 /* 508 /*
505 ** Return a pointer to the Table structure for the table that a trigger 509 ** Return a pointer to the Table structure for the table that a trigger
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
788 Parse *pTop = sqlite3ParseToplevel(pParse); 792 Parse *pTop = sqlite3ParseToplevel(pParse);
789 sqlite3 *db = pParse->db; /* Database handle */ 793 sqlite3 *db = pParse->db; /* Database handle */
790 TriggerPrg *pPrg; /* Value to return */ 794 TriggerPrg *pPrg; /* Value to return */
791 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */ 795 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
792 Vdbe *v; /* Temporary VM */ 796 Vdbe *v; /* Temporary VM */
793 NameContext sNC; /* Name context for sub-vdbe */ 797 NameContext sNC; /* Name context for sub-vdbe */
794 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */ 798 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
795 Parse *pSubParse; /* Parse context for sub-vdbe */ 799 Parse *pSubParse; /* Parse context for sub-vdbe */
796 int iEndTrigger = 0; /* Label to jump to if WHEN is false */ 800 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
797 801
798 assert( pTab==tableOfTrigger(pTrigger) ); 802 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
803 assert( pTop->pVdbe );
799 804
800 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they 805 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
801 ** are freed if an error occurs, link them into the Parse.pTriggerPrg 806 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
802 ** list of the top-level Parse object sooner rather than later. */ 807 ** list of the top-level Parse object sooner rather than later. */
803 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg)); 808 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
804 if( !pPrg ) return 0; 809 if( !pPrg ) return 0;
805 pPrg->pNext = pTop->pTriggerPrg; 810 pPrg->pNext = pTop->pTriggerPrg;
806 pTop->pTriggerPrg = pPrg; 811 pTop->pTriggerPrg = pPrg;
807 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram)); 812 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
808 if( !pProgram ) return 0; 813 if( !pProgram ) return 0;
809 pProgram->nRef = 1; 814 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
810 pPrg->pTrigger = pTrigger; 815 pPrg->pTrigger = pTrigger;
811 pPrg->orconf = orconf; 816 pPrg->orconf = orconf;
817 pPrg->aColmask[0] = 0xffffffff;
818 pPrg->aColmask[1] = 0xffffffff;
812 819
813 /* Allocate and populate a new Parse context to use for coding the 820 /* Allocate and populate a new Parse context to use for coding the
814 ** trigger sub-program. */ 821 ** trigger sub-program. */
815 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse)); 822 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
816 if( !pSubParse ) return 0; 823 if( !pSubParse ) return 0;
817 memset(&sNC, 0, sizeof(sNC)); 824 memset(&sNC, 0, sizeof(sNC));
818 sNC.pParse = pSubParse; 825 sNC.pParse = pSubParse;
819 pSubParse->db = db; 826 pSubParse->db = db;
820 pSubParse->pTriggerTab = pTab; 827 pSubParse->pTriggerTab = pTab;
821 pSubParse->pToplevel = pTop; 828 pSubParse->pToplevel = pTop;
822 pSubParse->zAuthContext = pTrigger->zName; 829 pSubParse->zAuthContext = pTrigger->zName;
823 pSubParse->eTriggerOp = pTrigger->op; 830 pSubParse->eTriggerOp = pTrigger->op;
831 pSubParse->nQueryLoop = pParse->nQueryLoop;
824 832
825 v = sqlite3GetVdbe(pSubParse); 833 v = sqlite3GetVdbe(pSubParse);
826 if( v ){ 834 if( v ){
827 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)", 835 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
828 pTrigger->zName, onErrorText(orconf), 836 pTrigger->zName, onErrorText(orconf),
829 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"), 837 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
830 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""), 838 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
831 (pTrigger->op==TK_INSERT ? "INSERT" : ""), 839 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
832 (pTrigger->op==TK_DELETE ? "DELETE" : ""), 840 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
833 pTab->zName 841 pTab->zName
(...skipping 28 matching lines...) Expand all
862 sqlite3VdbeAddOp0(v, OP_Halt); 870 sqlite3VdbeAddOp0(v, OP_Halt);
863 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf))); 871 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
864 872
865 transferParseError(pParse, pSubParse); 873 transferParseError(pParse, pSubParse);
866 if( db->mallocFailed==0 ){ 874 if( db->mallocFailed==0 ){
867 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg); 875 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
868 } 876 }
869 pProgram->nMem = pSubParse->nMem; 877 pProgram->nMem = pSubParse->nMem;
870 pProgram->nCsr = pSubParse->nTab; 878 pProgram->nCsr = pSubParse->nTab;
871 pProgram->token = (void *)pTrigger; 879 pProgram->token = (void *)pTrigger;
872 pPrg->oldmask = pSubParse->oldmask; 880 pPrg->aColmask[0] = pSubParse->oldmask;
881 pPrg->aColmask[1] = pSubParse->newmask;
873 sqlite3VdbeDelete(v); 882 sqlite3VdbeDelete(v);
874 } 883 }
875 884
876 assert( !pSubParse->pAinc && !pSubParse->pZombieTab ); 885 assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
877 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg ); 886 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
878 sqlite3StackFree(db, pSubParse); 887 sqlite3StackFree(db, pSubParse);
879 888
880 return pPrg; 889 return pPrg;
881 } 890 }
882 891
883 /* 892 /*
884 ** Return a pointer to a TriggerPrg object containing the sub-program for 893 ** Return a pointer to a TriggerPrg object containing the sub-program for
885 ** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such 894 ** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
886 ** TriggerPrg object exists, a new object is allocated and populated before 895 ** TriggerPrg object exists, a new object is allocated and populated before
887 ** being returned. 896 ** being returned.
888 */ 897 */
889 static TriggerPrg *getRowTrigger( 898 static TriggerPrg *getRowTrigger(
890 Parse *pParse, /* Current parse context */ 899 Parse *pParse, /* Current parse context */
891 Trigger *pTrigger, /* Trigger to code */ 900 Trigger *pTrigger, /* Trigger to code */
892 Table *pTab, /* The table trigger pTrigger is attached to */ 901 Table *pTab, /* The table trigger pTrigger is attached to */
893 int orconf /* ON CONFLICT algorithm. */ 902 int orconf /* ON CONFLICT algorithm. */
894 ){ 903 ){
895 Parse *pRoot = sqlite3ParseToplevel(pParse); 904 Parse *pRoot = sqlite3ParseToplevel(pParse);
896 TriggerPrg *pPrg; 905 TriggerPrg *pPrg;
897 906
898 assert( pTab==tableOfTrigger(pTrigger) ); 907 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
899 908
900 /* It may be that this trigger has already been coded (or is in the 909 /* It may be that this trigger has already been coded (or is in the
901 ** process of being coded). If this is the case, then an entry with 910 ** process of being coded). If this is the case, then an entry with
902 ** a matching TriggerPrg.pTrigger field will be present somewhere 911 ** a matching TriggerPrg.pTrigger field will be present somewhere
903 ** in the Parse.pTriggerPrg list. Search for such an entry. */ 912 ** in the Parse.pTriggerPrg list. Search for such an entry. */
904 for(pPrg=pRoot->pTriggerPrg; 913 for(pPrg=pRoot->pTriggerPrg;
905 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf); 914 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
906 pPrg=pPrg->pNext 915 pPrg=pPrg->pNext
907 ); 916 );
908 917
909 /* If an existing TriggerPrg could not be located, create a new one. */ 918 /* If an existing TriggerPrg could not be located, create a new one. */
910 if( !pPrg ){ 919 if( !pPrg ){
911 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf); 920 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
912 } 921 }
913 922
914 return pPrg; 923 return pPrg;
915 } 924 }
916 925
917 /* 926 /*
918 ** This is called to code FOR EACH ROW triggers. 927 ** Generate code for the trigger program associated with trigger p on
928 ** table pTab. The reg, orconf and ignoreJump parameters passed to this
929 ** function are the same as those described in the header function for
930 ** sqlite3CodeRowTrigger()
931 */
932 void sqlite3CodeRowTriggerDirect(
933 Parse *pParse, /* Parse context */
934 Trigger *p, /* Trigger to code */
935 Table *pTab, /* The table to code triggers from */
936 int reg, /* Reg array containing OLD.* and NEW.* values */
937 int orconf, /* ON CONFLICT policy */
938 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
939 ){
940 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
941 TriggerPrg *pPrg;
942 pPrg = getRowTrigger(pParse, p, pTab, orconf);
943 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
944
945 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
946 ** is a pointer to the sub-vdbe containing the trigger program. */
947 if( pPrg ){
948 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
949
950 sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem);
951 sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
952 VdbeComment(
953 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
954
955 /* Set the P5 operand of the OP_Program instruction to non-zero if
956 ** recursive invocation of this trigger program is disallowed. Recursive
957 ** invocation is disallowed if (a) the sub-program is really a trigger,
958 ** not a foreign key action, and (b) the flag to enable recursive triggers
959 ** is clear. */
960 sqlite3VdbeChangeP5(v, (u8)bRecursive);
961 }
962 }
963
964 /*
965 ** This is called to code the required FOR EACH ROW triggers for an operation
966 ** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
967 ** is given by the op paramater. The tr_tm parameter determines whether the
968 ** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
969 ** parameter pChanges is passed the list of columns being modified.
919 ** 970 **
920 ** When the code that this function generates is executed, the following 971 ** If there are no triggers that fire at the specified time for the specified
921 ** must be true: 972 ** operation on pTab, this function is a no-op.
922 ** 973 **
923 ** 1. No cursors may be open in the main database. (But newIdx and oldIdx 974 ** The reg argument is the address of the first in an array of registers
924 ** can be indices of cursors in temporary tables. See below.) 975 ** that contain the values substituted for the new.* and old.* references
976 ** in the trigger program. If N is the number of columns in table pTab
977 ** (a copy of pTab->nCol), then registers are populated as follows:
925 ** 978 **
926 ** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then 979 ** Register Contains
927 ** a temporary vdbe cursor (index newIdx) must be open and pointing at 980 ** ------------------------------------------------------
928 ** a row containing values to be substituted for new.* expressions in the 981 ** reg+0 OLD.rowid
929 ** trigger program(s). 982 ** reg+1 OLD.* value of left-most column of pTab
983 ** ... ...
984 ** reg+N OLD.* value of right-most column of pTab
985 ** reg+N+1 NEW.rowid
986 ** reg+N+2 OLD.* value of left-most column of pTab
987 ** ... ...
988 ** reg+N+N+1 NEW.* value of right-most column of pTab
930 ** 989 **
931 ** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then 990 ** For ON DELETE triggers, the registers containing the NEW.* values will
932 ** a temporary vdbe cursor (index oldIdx) must be open and pointing at 991 ** never be accessed by the trigger program, so they are not allocated or
933 ** a row containing values to be substituted for old.* expressions in the 992 ** populated by the caller (there is no data to populate them with anyway).
934 ** trigger program(s). 993 ** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
994 ** are never accessed, and so are not allocated by the caller. So, for an
995 ** ON INSERT trigger, the value passed to this function as parameter reg
996 ** is not a readable register, although registers (reg+N) through
997 ** (reg+N+N+1) are.
935 ** 998 **
936 ** If they are not NULL, the piOldColMask and piNewColMask output variables 999 ** Parameter orconf is the default conflict resolution algorithm for the
937 ** are set to values that describe the columns used by the trigger program 1000 ** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
938 ** in the OLD.* and NEW.* tables respectively. If column N of the 1001 ** is the instruction that control should jump to if a trigger program
939 ** pseudo-table is read at least once, the corresponding bit of the output 1002 ** raises an IGNORE exception.
940 ** mask is set. If a column with an index greater than 32 is read, the
941 ** output mask is set to the special value 0xffffffff.
942 **
943 */ 1003 */
944 void sqlite3CodeRowTrigger( 1004 void sqlite3CodeRowTrigger(
945 Parse *pParse, /* Parse context */ 1005 Parse *pParse, /* Parse context */
946 Trigger *pTrigger, /* List of triggers on table pTab */ 1006 Trigger *pTrigger, /* List of triggers on table pTab */
947 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */ 1007 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
948 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */ 1008 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
949 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */ 1009 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
950 Table *pTab, /* The table to code triggers from */ 1010 Table *pTab, /* The table to code triggers from */
951 int newIdx, /* The indice of the "new" row to access */ 1011 int reg, /* The first in an array of registers (see above) */
952 int oldIdx, /* The indice of the "old" row to access */
953 int orconf, /* ON CONFLICT policy */ 1012 int orconf, /* ON CONFLICT policy */
954 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */ 1013 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
955 ){ 1014 ){
956 Trigger *p; 1015 Trigger *p; /* Used to iterate through pTrigger list */
957 1016
958 UNUSED_PARAMETER(newIdx); 1017 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
959 1018 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
960 assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE); 1019 assert( (op==TK_UPDATE)==(pChanges!=0) );
961 assert(tr_tm == TRIGGER_BEFORE || tr_tm == TRIGGER_AFTER );
962 1020
963 for(p=pTrigger; p; p=p->pNext){ 1021 for(p=pTrigger; p; p=p->pNext){
964 1022
965 /* Sanity checking: The schema for the trigger and for the table are 1023 /* Sanity checking: The schema for the trigger and for the table are
966 ** always defined. The trigger must be in the same schema as the table 1024 ** always defined. The trigger must be in the same schema as the table
967 ** or else it must be a TEMP trigger. */ 1025 ** or else it must be a TEMP trigger. */
968 assert( p->pSchema!=0 ); 1026 assert( p->pSchema!=0 );
969 assert( p->pTabSchema!=0 ); 1027 assert( p->pTabSchema!=0 );
970 assert( p->pSchema==p->pTabSchema 1028 assert( p->pSchema==p->pTabSchema
971 || p->pSchema==pParse->db->aDb[1].pSchema ); 1029 || p->pSchema==pParse->db->aDb[1].pSchema );
972 1030
973 /* Determine whether we should code this trigger */ 1031 /* Determine whether we should code this trigger */
974 if( p->op==op 1032 if( p->op==op
975 && p->tr_tm==tr_tm 1033 && p->tr_tm==tr_tm
976 && checkColumnOverlap(p->pColumns,pChanges) 1034 && checkColumnOverlap(p->pColumns, pChanges)
977 ){ 1035 ){
978 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */ 1036 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
979 TriggerPrg *pPrg;
980 pPrg = getRowTrigger(pParse, p, pTab, orconf);
981 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
982
983 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
984 ** is a pointer to the sub-vdbe containing the trigger program. */
985 if( pPrg ){
986 sqlite3VdbeAddOp3(v, OP_Program, oldIdx, ignoreJump, ++pParse->nMem);
987 pPrg->pProgram->nRef++;
988 sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
989 VdbeComment((v, "Call: %s.%s", p->zName, onErrorText(orconf)));
990 }
991 } 1037 }
992 } 1038 }
993 } 1039 }
994 1040
995 /* 1041 /*
996 ** Triggers fired by UPDATE or DELETE statements may access values stored 1042 ** Triggers may access values stored in the old.* or new.* pseudo-table.
997 ** in the old.* pseudo-table. This function returns a 32-bit bitmask 1043 ** This function returns a 32-bit bitmask indicating which columns of the
998 ** indicating which columns of the old.* table actually are used by 1044 ** old.* or new.* tables actually are used by triggers. This information
999 ** triggers. This information may be used by the caller to avoid having 1045 ** may be used by the caller, for example, to avoid having to load the entire
1000 ** to load the entire old.* record into memory when executing an UPDATE 1046 ** old.* record into memory when executing an UPDATE or DELETE command.
1001 ** or DELETE command.
1002 ** 1047 **
1003 ** Bit 0 of the returned mask is set if the left-most column of the 1048 ** Bit 0 of the returned mask is set if the left-most column of the
1004 ** table may be accessed using an old.<col> reference. Bit 1 is set if 1049 ** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
1005 ** the second leftmost column value is required, and so on. If there 1050 ** the second leftmost column value is required, and so on. If there
1006 ** are more than 32 columns in the table, and at least one of the columns 1051 ** are more than 32 columns in the table, and at least one of the columns
1007 ** with an index greater than 32 may be accessed, 0xffffffff is returned. 1052 ** with an index greater than 32 may be accessed, 0xffffffff is returned.
1008 ** 1053 **
1009 ** It is not possible to determine if the old.rowid column is accessed 1054 ** It is not possible to determine if the old.rowid or new.rowid column is
1010 ** by triggers. The caller must always assume that it is. 1055 ** accessed by triggers. The caller must always assume that it is.
1011 ** 1056 **
1012 ** There is no equivalent function for new.* references. 1057 ** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
1058 ** applies to the old.* table. If 1, the new.* table.
1059 **
1060 ** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
1061 ** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
1062 ** included in the returned mask if the TRIGGER_BEFORE bit is set in the
1063 ** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
1064 ** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
1013 */ 1065 */
1014 u32 sqlite3TriggerOldmask( 1066 u32 sqlite3TriggerColmask(
1015 Parse *pParse, /* Parse context */ 1067 Parse *pParse, /* Parse context */
1016 Trigger *pTrigger, /* List of triggers on table pTab */ 1068 Trigger *pTrigger, /* List of triggers on table pTab */
1017 int op, /* Either TK_UPDATE or TK_DELETE */
1018 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */ 1069 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
1070 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
1071 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
1019 Table *pTab, /* The table to code triggers from */ 1072 Table *pTab, /* The table to code triggers from */
1020 int orconf /* Default ON CONFLICT policy for trigger steps */ 1073 int orconf /* Default ON CONFLICT policy for trigger steps */
1021 ){ 1074 ){
1075 const int op = pChanges ? TK_UPDATE : TK_DELETE;
1022 u32 mask = 0; 1076 u32 mask = 0;
1023 Trigger *p; 1077 Trigger *p;
1024 1078
1025 assert(op==TK_UPDATE || op==TK_DELETE); 1079 assert( isNew==1 || isNew==0 );
1026 for(p=pTrigger; p; p=p->pNext){ 1080 for(p=pTrigger; p; p=p->pNext){
1027 if( p->op==op && checkColumnOverlap(p->pColumns,pChanges) ){ 1081 if( p->op==op && (tr_tm&p->tr_tm)
1082 && checkColumnOverlap(p->pColumns,pChanges)
1083 ){
1028 TriggerPrg *pPrg; 1084 TriggerPrg *pPrg;
1029 pPrg = getRowTrigger(pParse, p, pTab, orconf); 1085 pPrg = getRowTrigger(pParse, p, pTab, orconf);
1030 if( pPrg ){ 1086 if( pPrg ){
1031 mask |= pPrg->oldmask; 1087 mask |= pPrg->aColmask[isNew];
1032 } 1088 }
1033 } 1089 }
1034 } 1090 }
1035 1091
1036 return mask; 1092 return mask;
1037 } 1093 }
1038 1094
1039 #endif /* !defined(SQLITE_OMIT_TRIGGER) */ 1095 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
OLDNEW
« no previous file with comments | « third_party/sqlite/src/src/tokenize.c ('k') | third_party/sqlite/src/src/update.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698