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