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