annotate sqlite/trigger.c @ 1566:e513069caf71 trunk

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