|
1 /* |
|
2 ** 2001 September 15 |
|
3 ** |
|
4 ** The author disclaims copyright to this source code. In place of |
|
5 ** a legal notice, here is a blessing: |
|
6 ** |
|
7 ** May you do good and not evil. |
|
8 ** May you find forgiveness for yourself and forgive others. |
|
9 ** May you share freely, never taking more than you give. |
|
10 ** |
|
11 ************************************************************************* |
|
12 ** This file contains C code routines that are called by the parser |
|
13 ** in order to generate code for DELETE FROM statements. |
|
14 ** |
|
15 ** $Id: delete.c,v 1.182 2008/10/10 23:48:26 drh Exp $ |
|
16 */ |
|
17 #include "sqliteInt.h" |
|
18 |
|
19 /* |
|
20 ** Look up every table that is named in pSrc. If any table is not found, |
|
21 ** add an error message to pParse->zErrMsg and return NULL. If all tables |
|
22 ** are found, return a pointer to the last table. |
|
23 */ |
|
24 Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){ |
|
25 struct SrcList_item *pItem = pSrc->a; |
|
26 Table *pTab; |
|
27 assert( pItem && pSrc->nSrc==1 ); |
|
28 pTab = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase); |
|
29 sqlite3DeleteTable(pItem->pTab); |
|
30 pItem->pTab = pTab; |
|
31 if( pTab ){ |
|
32 pTab->nRef++; |
|
33 } |
|
34 if( sqlite3IndexedByLookup(pParse, pItem) ){ |
|
35 pTab = 0; |
|
36 } |
|
37 return pTab; |
|
38 } |
|
39 |
|
40 /* |
|
41 ** Check to make sure the given table is writable. If it is not |
|
42 ** writable, generate an error message and return 1. If it is |
|
43 ** writable return 0; |
|
44 */ |
|
45 int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){ |
|
46 if( ((pTab->tabFlags & TF_Readonly)!=0 |
|
47 && (pParse->db->flags & SQLITE_WriteSchema)==0 |
|
48 && pParse->nested==0) |
|
49 #ifndef SQLITE_OMIT_VIRTUALTABLE |
|
50 || (pTab->pMod && pTab->pMod->pModule->xUpdate==0) |
|
51 #endif |
|
52 ){ |
|
53 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName); |
|
54 return 1; |
|
55 } |
|
56 #ifndef SQLITE_OMIT_VIEW |
|
57 if( !viewOk && pTab->pSelect ){ |
|
58 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName); |
|
59 return 1; |
|
60 } |
|
61 #endif |
|
62 return 0; |
|
63 } |
|
64 |
|
65 /* |
|
66 ** Generate code that will open a table for reading. |
|
67 */ |
|
68 void sqlite3OpenTable( |
|
69 Parse *p, /* Generate code into this VDBE */ |
|
70 int iCur, /* The cursor number of the table */ |
|
71 int iDb, /* The database index in sqlite3.aDb[] */ |
|
72 Table *pTab, /* The table to be opened */ |
|
73 int opcode /* OP_OpenRead or OP_OpenWrite */ |
|
74 ){ |
|
75 Vdbe *v; |
|
76 if( IsVirtual(pTab) ) return; |
|
77 v = sqlite3GetVdbe(p); |
|
78 assert( opcode==OP_OpenWrite || opcode==OP_OpenRead ); |
|
79 sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite), pTab->zName); |
|
80 sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pTab->nCol); |
|
81 sqlite3VdbeAddOp3(v, opcode, iCur, pTab->tnum, iDb); |
|
82 VdbeComment((v, "%s", pTab->zName)); |
|
83 } |
|
84 |
|
85 |
|
86 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) |
|
87 /* |
|
88 ** Evaluate a view and store its result in an ephemeral table. The |
|
89 ** pWhere argument is an optional WHERE clause that restricts the |
|
90 ** set of rows in the view that are to be added to the ephemeral table. |
|
91 */ |
|
92 void sqlite3MaterializeView( |
|
93 Parse *pParse, /* Parsing context */ |
|
94 Table *pView, /* View definition */ |
|
95 Expr *pWhere, /* Optional WHERE clause to be added */ |
|
96 int iCur /* Cursor number for ephemerial table */ |
|
97 ){ |
|
98 SelectDest dest; |
|
99 Select *pDup; |
|
100 sqlite3 *db = pParse->db; |
|
101 |
|
102 pDup = sqlite3SelectDup(db, pView->pSelect); |
|
103 if( pWhere ){ |
|
104 SrcList *pFrom; |
|
105 Token viewName; |
|
106 |
|
107 pWhere = sqlite3ExprDup(db, pWhere); |
|
108 viewName.z = (u8*)pView->zName; |
|
109 viewName.n = (unsigned int)strlen((const char*)viewName.z); |
|
110 pFrom = sqlite3SrcListAppendFromTerm(pParse, 0, 0, 0, &viewName, pDup, 0,0); |
|
111 pDup = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0); |
|
112 } |
|
113 sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur); |
|
114 sqlite3Select(pParse, pDup, &dest); |
|
115 sqlite3SelectDelete(db, pDup); |
|
116 } |
|
117 #endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */ |
|
118 |
|
119 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) |
|
120 /* |
|
121 ** Generate an expression tree to implement the WHERE, ORDER BY, |
|
122 ** and LIMIT/OFFSET portion of DELETE and UPDATE statements. |
|
123 ** |
|
124 ** DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1; |
|
125 ** \__________________________/ |
|
126 ** pLimitWhere (pInClause) |
|
127 */ |
|
128 Expr *sqlite3LimitWhere( |
|
129 Parse *pParse, /* The parser context */ |
|
130 SrcList *pSrc, /* the FROM clause -- which tables to scan */ |
|
131 Expr *pWhere, /* The WHERE clause. May be null */ |
|
132 ExprList *pOrderBy, /* The ORDER BY clause. May be null */ |
|
133 Expr *pLimit, /* The LIMIT clause. May be null */ |
|
134 Expr *pOffset, /* The OFFSET clause. May be null */ |
|
135 char *zStmtType /* Either DELETE or UPDATE. For error messages. */ |
|
136 ){ |
|
137 Expr *pWhereRowid = NULL; /* WHERE rowid .. */ |
|
138 Expr *pInClause = NULL; /* WHERE rowid IN ( select ) */ |
|
139 Expr *pSelectRowid = NULL; /* SELECT rowid ... */ |
|
140 ExprList *pEList = NULL; /* Expression list contaning only pSelectRowid */ |
|
141 SrcList *pSelectSrc = NULL; /* SELECT rowid FROM x ... (dup of pSrc) */ |
|
142 Select *pSelect = NULL; /* Complete SELECT tree */ |
|
143 |
|
144 /* Check that there isn't an ORDER BY without a LIMIT clause. |
|
145 */ |
|
146 if( pOrderBy && (pLimit == 0) ) { |
|
147 sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType); |
|
148 pParse->parseError = 1; |
|
149 goto limit_where_cleanup_2; |
|
150 } |
|
151 |
|
152 /* We only need to generate a select expression if there |
|
153 ** is a limit/offset term to enforce. |
|
154 */ |
|
155 if( pLimit == 0 ) { |
|
156 /* if pLimit is null, pOffset will always be null as well. */ |
|
157 assert( pOffset == 0 ); |
|
158 return pWhere; |
|
159 } |
|
160 |
|
161 /* Generate a select expression tree to enforce the limit/offset |
|
162 ** term for the DELETE or UPDATE statement. For example: |
|
163 ** DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1 |
|
164 ** becomes: |
|
165 ** DELETE FROM table_a WHERE rowid IN ( |
|
166 ** SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1 |
|
167 ** ); |
|
168 */ |
|
169 |
|
170 pSelectRowid = sqlite3Expr(pParse->db, TK_ROW, 0, 0, 0); |
|
171 if( pSelectRowid == 0 ) goto limit_where_cleanup_2; |
|
172 pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid, 0); |
|
173 if( pEList == 0 ) goto limit_where_cleanup_2; |
|
174 |
|
175 /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree |
|
176 ** and the SELECT subtree. */ |
|
177 pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc); |
|
178 if( pSelectSrc == 0 ) { |
|
179 sqlite3ExprListDelete(pParse->db, pEList); |
|
180 goto limit_where_cleanup_2; |
|
181 } |
|
182 |
|
183 /* generate the SELECT expression tree. */ |
|
184 pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0,pOrderBy,0,pLimit,pOffset); |
|
185 if( pSelect == 0 ) return 0; |
|
186 |
|
187 /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */ |
|
188 pWhereRowid = sqlite3Expr(pParse->db, TK_ROW, 0, 0, 0); |
|
189 if( pWhereRowid == 0 ) goto limit_where_cleanup_1; |
|
190 pInClause = sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0, 0); |
|
191 if( pInClause == 0 ) goto limit_where_cleanup_1; |
|
192 |
|
193 pInClause->pSelect = pSelect; |
|
194 sqlite3ExprSetHeight(pParse, pInClause); |
|
195 return pInClause; |
|
196 |
|
197 /* something went wrong. clean up anything allocated. */ |
|
198 limit_where_cleanup_1: |
|
199 sqlite3SelectDelete(pParse->db, pSelect); |
|
200 return 0; |
|
201 |
|
202 limit_where_cleanup_2: |
|
203 sqlite3ExprDelete(pParse->db, pWhere); |
|
204 sqlite3ExprListDelete(pParse->db, pOrderBy); |
|
205 sqlite3ExprDelete(pParse->db, pLimit); |
|
206 sqlite3ExprDelete(pParse->db, pOffset); |
|
207 return 0; |
|
208 } |
|
209 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */ |
|
210 |
|
211 /* |
|
212 ** Generate code for a DELETE FROM statement. |
|
213 ** |
|
214 ** DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL; |
|
215 ** \________/ \________________/ |
|
216 ** pTabList pWhere |
|
217 */ |
|
218 void sqlite3DeleteFrom( |
|
219 Parse *pParse, /* The parser context */ |
|
220 SrcList *pTabList, /* The table from which we should delete things */ |
|
221 Expr *pWhere /* The WHERE clause. May be null */ |
|
222 ){ |
|
223 Vdbe *v; /* The virtual database engine */ |
|
224 Table *pTab; /* The table from which records will be deleted */ |
|
225 const char *zDb; /* Name of database holding pTab */ |
|
226 int end, addr = 0; /* A couple addresses of generated code */ |
|
227 int i; /* Loop counter */ |
|
228 WhereInfo *pWInfo; /* Information about the WHERE clause */ |
|
229 Index *pIdx; /* For looping over indices of the table */ |
|
230 int iCur; /* VDBE Cursor number for pTab */ |
|
231 sqlite3 *db; /* Main database structure */ |
|
232 AuthContext sContext; /* Authorization context */ |
|
233 int oldIdx = -1; /* Cursor for the OLD table of AFTER triggers */ |
|
234 NameContext sNC; /* Name context to resolve expressions in */ |
|
235 int iDb; /* Database number */ |
|
236 int memCnt = 0; /* Memory cell used for change counting */ |
|
237 |
|
238 #ifndef SQLITE_OMIT_TRIGGER |
|
239 int isView; /* True if attempting to delete from a view */ |
|
240 int triggers_exist = 0; /* True if any triggers exist */ |
|
241 #endif |
|
242 int iBeginAfterTrigger; /* Address of after trigger program */ |
|
243 int iEndAfterTrigger; /* Exit of after trigger program */ |
|
244 int iBeginBeforeTrigger; /* Address of before trigger program */ |
|
245 int iEndBeforeTrigger; /* Exit of before trigger program */ |
|
246 u32 old_col_mask = 0; /* Mask of OLD.* columns in use */ |
|
247 |
|
248 sContext.pParse = 0; |
|
249 db = pParse->db; |
|
250 if( pParse->nErr || db->mallocFailed ){ |
|
251 goto delete_from_cleanup; |
|
252 } |
|
253 assert( pTabList->nSrc==1 ); |
|
254 |
|
255 /* Locate the table which we want to delete. This table has to be |
|
256 ** put in an SrcList structure because some of the subroutines we |
|
257 ** will be calling are designed to work with multiple tables and expect |
|
258 ** an SrcList* parameter instead of just a Table* parameter. |
|
259 */ |
|
260 pTab = sqlite3SrcListLookup(pParse, pTabList); |
|
261 if( pTab==0 ) goto delete_from_cleanup; |
|
262 |
|
263 /* Figure out if we have any triggers and if the table being |
|
264 ** deleted from is a view |
|
265 */ |
|
266 #ifndef SQLITE_OMIT_TRIGGER |
|
267 triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0); |
|
268 isView = pTab->pSelect!=0; |
|
269 #else |
|
270 # define triggers_exist 0 |
|
271 # define isView 0 |
|
272 #endif |
|
273 #ifdef SQLITE_OMIT_VIEW |
|
274 # undef isView |
|
275 # define isView 0 |
|
276 #endif |
|
277 |
|
278 if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){ |
|
279 goto delete_from_cleanup; |
|
280 } |
|
281 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
|
282 assert( iDb<db->nDb ); |
|
283 zDb = db->aDb[iDb].zName; |
|
284 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){ |
|
285 goto delete_from_cleanup; |
|
286 } |
|
287 |
|
288 /* If pTab is really a view, make sure it has been initialized. |
|
289 */ |
|
290 if( sqlite3ViewGetColumnNames(pParse, pTab) ){ |
|
291 goto delete_from_cleanup; |
|
292 } |
|
293 |
|
294 /* Allocate a cursor used to store the old.* data for a trigger. |
|
295 */ |
|
296 if( triggers_exist ){ |
|
297 oldIdx = pParse->nTab++; |
|
298 } |
|
299 |
|
300 /* Assign cursor number to the table and all its indices. |
|
301 */ |
|
302 assert( pTabList->nSrc==1 ); |
|
303 iCur = pTabList->a[0].iCursor = pParse->nTab++; |
|
304 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
|
305 pParse->nTab++; |
|
306 } |
|
307 |
|
308 /* Start the view context |
|
309 */ |
|
310 if( isView ){ |
|
311 sqlite3AuthContextPush(pParse, &sContext, pTab->zName); |
|
312 } |
|
313 |
|
314 /* Begin generating code. |
|
315 */ |
|
316 v = sqlite3GetVdbe(pParse); |
|
317 if( v==0 ){ |
|
318 goto delete_from_cleanup; |
|
319 } |
|
320 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); |
|
321 sqlite3BeginWriteOperation(pParse, triggers_exist, iDb); |
|
322 |
|
323 if( triggers_exist ){ |
|
324 int orconf = ((pParse->trigStack)?pParse->trigStack->orconf:OE_Default); |
|
325 int iGoto = sqlite3VdbeAddOp0(v, OP_Goto); |
|
326 addr = sqlite3VdbeMakeLabel(v); |
|
327 |
|
328 iBeginBeforeTrigger = sqlite3VdbeCurrentAddr(v); |
|
329 (void)sqlite3CodeRowTrigger(pParse, TK_DELETE, 0, TRIGGER_BEFORE, pTab, |
|
330 -1, oldIdx, orconf, addr, &old_col_mask, 0); |
|
331 iEndBeforeTrigger = sqlite3VdbeAddOp0(v, OP_Goto); |
|
332 |
|
333 iBeginAfterTrigger = sqlite3VdbeCurrentAddr(v); |
|
334 (void)sqlite3CodeRowTrigger(pParse, TK_DELETE, 0, TRIGGER_AFTER, pTab, -1, |
|
335 oldIdx, orconf, addr, &old_col_mask, 0); |
|
336 iEndAfterTrigger = sqlite3VdbeAddOp0(v, OP_Goto); |
|
337 |
|
338 sqlite3VdbeJumpHere(v, iGoto); |
|
339 } |
|
340 |
|
341 /* If we are trying to delete from a view, realize that view into |
|
342 ** a ephemeral table. |
|
343 */ |
|
344 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) |
|
345 if( isView ){ |
|
346 sqlite3MaterializeView(pParse, pTab, pWhere, iCur); |
|
347 } |
|
348 #endif |
|
349 |
|
350 /* Resolve the column names in the WHERE clause. |
|
351 */ |
|
352 memset(&sNC, 0, sizeof(sNC)); |
|
353 sNC.pParse = pParse; |
|
354 sNC.pSrcList = pTabList; |
|
355 if( sqlite3ResolveExprNames(&sNC, pWhere) ){ |
|
356 goto delete_from_cleanup; |
|
357 } |
|
358 |
|
359 /* Initialize the counter of the number of rows deleted, if |
|
360 ** we are counting rows. |
|
361 */ |
|
362 if( db->flags & SQLITE_CountRows ){ |
|
363 memCnt = ++pParse->nMem; |
|
364 sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt); |
|
365 } |
|
366 |
|
367 #ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION |
|
368 /* Special case: A DELETE without a WHERE clause deletes everything. |
|
369 ** It is easier just to erase the whole table. Note, however, that |
|
370 ** this means that the row change count will be incorrect. |
|
371 */ |
|
372 if( pWhere==0 && !triggers_exist && !IsVirtual(pTab) ){ |
|
373 if( db->flags & SQLITE_CountRows ){ |
|
374 /* If counting rows deleted, just count the total number of |
|
375 ** entries in the table. */ |
|
376 int addr2; |
|
377 if( !isView ){ |
|
378 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead); |
|
379 } |
|
380 sqlite3VdbeAddOp2(v, OP_Rewind, iCur, sqlite3VdbeCurrentAddr(v)+2); |
|
381 addr2 = sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1); |
|
382 sqlite3VdbeAddOp2(v, OP_Next, iCur, addr2); |
|
383 sqlite3VdbeAddOp1(v, OP_Close, iCur); |
|
384 } |
|
385 if( !isView ){ |
|
386 sqlite3VdbeAddOp2(v, OP_Clear, pTab->tnum, iDb); |
|
387 if( !pParse->nested ){ |
|
388 sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC); |
|
389 } |
|
390 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
|
391 assert( pIdx->pSchema==pTab->pSchema ); |
|
392 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb); |
|
393 } |
|
394 } |
|
395 }else |
|
396 #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */ |
|
397 /* The usual case: There is a WHERE clause so we have to scan through |
|
398 ** the table and pick which records to delete. |
|
399 */ |
|
400 { |
|
401 int iRowid = ++pParse->nMem; /* Used for storing rowid values. */ |
|
402 |
|
403 /* Begin the database scan |
|
404 */ |
|
405 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0); |
|
406 if( pWInfo==0 ) goto delete_from_cleanup; |
|
407 |
|
408 /* Remember the rowid of every item to be deleted. |
|
409 */ |
|
410 sqlite3VdbeAddOp2(v, IsVirtual(pTab) ? OP_VRowid : OP_Rowid, iCur, iRowid); |
|
411 sqlite3VdbeAddOp1(v, OP_FifoWrite, iRowid); |
|
412 if( db->flags & SQLITE_CountRows ){ |
|
413 sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1); |
|
414 } |
|
415 |
|
416 /* End the database scan loop. |
|
417 */ |
|
418 sqlite3WhereEnd(pWInfo); |
|
419 |
|
420 /* Open the pseudo-table used to store OLD if there are triggers. |
|
421 */ |
|
422 if( triggers_exist ){ |
|
423 sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pTab->nCol); |
|
424 sqlite3VdbeAddOp1(v, OP_OpenPseudo, oldIdx); |
|
425 } |
|
426 |
|
427 /* Delete every item whose key was written to the list during the |
|
428 ** database scan. We have to delete items after the scan is complete |
|
429 ** because deleting an item can change the scan order. |
|
430 */ |
|
431 end = sqlite3VdbeMakeLabel(v); |
|
432 |
|
433 if( !isView ){ |
|
434 /* Open cursors for the table we are deleting from and |
|
435 ** all its indices. |
|
436 */ |
|
437 sqlite3OpenTableAndIndices(pParse, pTab, iCur, OP_OpenWrite); |
|
438 } |
|
439 |
|
440 /* This is the beginning of the delete loop. If a trigger encounters |
|
441 ** an IGNORE constraint, it jumps back to here. |
|
442 */ |
|
443 if( triggers_exist ){ |
|
444 sqlite3VdbeResolveLabel(v, addr); |
|
445 } |
|
446 addr = sqlite3VdbeAddOp2(v, OP_FifoRead, iRowid, end); |
|
447 |
|
448 if( triggers_exist ){ |
|
449 int iData = ++pParse->nMem; /* For storing row data of OLD table */ |
|
450 |
|
451 /* If the record is no longer present in the table, jump to the |
|
452 ** next iteration of the loop through the contents of the fifo. |
|
453 */ |
|
454 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, iRowid); |
|
455 |
|
456 /* Populate the OLD.* pseudo-table */ |
|
457 if( old_col_mask ){ |
|
458 sqlite3VdbeAddOp2(v, OP_RowData, iCur, iData); |
|
459 }else{ |
|
460 sqlite3VdbeAddOp2(v, OP_Null, 0, iData); |
|
461 } |
|
462 sqlite3VdbeAddOp3(v, OP_Insert, oldIdx, iData, iRowid); |
|
463 |
|
464 /* Jump back and run the BEFORE triggers */ |
|
465 sqlite3VdbeAddOp2(v, OP_Goto, 0, iBeginBeforeTrigger); |
|
466 sqlite3VdbeJumpHere(v, iEndBeforeTrigger); |
|
467 } |
|
468 |
|
469 if( !isView ){ |
|
470 /* Delete the row */ |
|
471 #ifndef SQLITE_OMIT_VIRTUALTABLE |
|
472 if( IsVirtual(pTab) ){ |
|
473 const char *pVtab = (const char *)pTab->pVtab; |
|
474 sqlite3VtabMakeWritable(pParse, pTab); |
|
475 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, pVtab, P4_VTAB); |
|
476 }else |
|
477 #endif |
|
478 { |
|
479 sqlite3GenerateRowDelete(pParse, pTab, iCur, iRowid, pParse->nested==0); |
|
480 } |
|
481 } |
|
482 |
|
483 /* If there are row triggers, close all cursors then invoke |
|
484 ** the AFTER triggers |
|
485 */ |
|
486 if( triggers_exist ){ |
|
487 /* Jump back and run the AFTER triggers */ |
|
488 sqlite3VdbeAddOp2(v, OP_Goto, 0, iBeginAfterTrigger); |
|
489 sqlite3VdbeJumpHere(v, iEndAfterTrigger); |
|
490 } |
|
491 |
|
492 /* End of the delete loop */ |
|
493 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr); |
|
494 sqlite3VdbeResolveLabel(v, end); |
|
495 |
|
496 /* Close the cursors after the loop if there are no row triggers */ |
|
497 if( !isView && !IsVirtual(pTab) ){ |
|
498 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
|
499 sqlite3VdbeAddOp2(v, OP_Close, iCur + i, pIdx->tnum); |
|
500 } |
|
501 sqlite3VdbeAddOp1(v, OP_Close, iCur); |
|
502 } |
|
503 } |
|
504 |
|
505 /* |
|
506 ** Return the number of rows that were deleted. If this routine is |
|
507 ** generating code because of a call to sqlite3NestedParse(), do not |
|
508 ** invoke the callback function. |
|
509 */ |
|
510 if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){ |
|
511 sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1); |
|
512 sqlite3VdbeSetNumCols(v, 1); |
|
513 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", P4_STATIC); |
|
514 } |
|
515 |
|
516 delete_from_cleanup: |
|
517 sqlite3AuthContextPop(&sContext); |
|
518 sqlite3SrcListDelete(db, pTabList); |
|
519 sqlite3ExprDelete(db, pWhere); |
|
520 return; |
|
521 } |
|
522 |
|
523 /* |
|
524 ** This routine generates VDBE code that causes a single row of a |
|
525 ** single table to be deleted. |
|
526 ** |
|
527 ** The VDBE must be in a particular state when this routine is called. |
|
528 ** These are the requirements: |
|
529 ** |
|
530 ** 1. A read/write cursor pointing to pTab, the table containing the row |
|
531 ** to be deleted, must be opened as cursor number "base". |
|
532 ** |
|
533 ** 2. Read/write cursors for all indices of pTab must be open as |
|
534 ** cursor number base+i for the i-th index. |
|
535 ** |
|
536 ** 3. The record number of the row to be deleted must be stored in |
|
537 ** memory cell iRowid. |
|
538 ** |
|
539 ** This routine pops the top of the stack to remove the record number |
|
540 ** and then generates code to remove both the table record and all index |
|
541 ** entries that point to that record. |
|
542 */ |
|
543 void sqlite3GenerateRowDelete( |
|
544 Parse *pParse, /* Parsing context */ |
|
545 Table *pTab, /* Table containing the row to be deleted */ |
|
546 int iCur, /* Cursor number for the table */ |
|
547 int iRowid, /* Memory cell that contains the rowid to delete */ |
|
548 int count /* Increment the row change counter */ |
|
549 ){ |
|
550 int addr; |
|
551 Vdbe *v; |
|
552 |
|
553 v = pParse->pVdbe; |
|
554 addr = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowid); |
|
555 sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, 0); |
|
556 sqlite3VdbeAddOp2(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0)); |
|
557 if( count ){ |
|
558 sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC); |
|
559 } |
|
560 sqlite3VdbeJumpHere(v, addr); |
|
561 } |
|
562 |
|
563 /* |
|
564 ** This routine generates VDBE code that causes the deletion of all |
|
565 ** index entries associated with a single row of a single table. |
|
566 ** |
|
567 ** The VDBE must be in a particular state when this routine is called. |
|
568 ** These are the requirements: |
|
569 ** |
|
570 ** 1. A read/write cursor pointing to pTab, the table containing the row |
|
571 ** to be deleted, must be opened as cursor number "iCur". |
|
572 ** |
|
573 ** 2. Read/write cursors for all indices of pTab must be open as |
|
574 ** cursor number iCur+i for the i-th index. |
|
575 ** |
|
576 ** 3. The "iCur" cursor must be pointing to the row that is to be |
|
577 ** deleted. |
|
578 */ |
|
579 void sqlite3GenerateRowIndexDelete( |
|
580 Parse *pParse, /* Parsing and code generating context */ |
|
581 Table *pTab, /* Table containing the row to be deleted */ |
|
582 int iCur, /* Cursor number for the table */ |
|
583 int *aRegIdx /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */ |
|
584 ){ |
|
585 int i; |
|
586 Index *pIdx; |
|
587 int r1; |
|
588 |
|
589 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
|
590 if( aRegIdx!=0 && aRegIdx[i-1]==0 ) continue; |
|
591 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iCur, 0, 0); |
|
592 sqlite3VdbeAddOp3(pParse->pVdbe, OP_IdxDelete, iCur+i, r1,pIdx->nColumn+1); |
|
593 } |
|
594 } |
|
595 |
|
596 /* |
|
597 ** Generate code that will assemble an index key and put it in register |
|
598 ** regOut. The key with be for index pIdx which is an index on pTab. |
|
599 ** iCur is the index of a cursor open on the pTab table and pointing to |
|
600 ** the entry that needs indexing. |
|
601 ** |
|
602 ** Return a register number which is the first in a block of |
|
603 ** registers that holds the elements of the index key. The |
|
604 ** block of registers has already been deallocated by the time |
|
605 ** this routine returns. |
|
606 */ |
|
607 int sqlite3GenerateIndexKey( |
|
608 Parse *pParse, /* Parsing context */ |
|
609 Index *pIdx, /* The index for which to generate a key */ |
|
610 int iCur, /* Cursor number for the pIdx->pTable table */ |
|
611 int regOut, /* Write the new index key to this register */ |
|
612 int doMakeRec /* Run the OP_MakeRecord instruction if true */ |
|
613 ){ |
|
614 Vdbe *v = pParse->pVdbe; |
|
615 int j; |
|
616 Table *pTab = pIdx->pTable; |
|
617 int regBase; |
|
618 int nCol; |
|
619 |
|
620 nCol = pIdx->nColumn; |
|
621 regBase = sqlite3GetTempRange(pParse, nCol+1); |
|
622 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regBase+nCol); |
|
623 for(j=0; j<nCol; j++){ |
|
624 int idx = pIdx->aiColumn[j]; |
|
625 if( idx==pTab->iPKey ){ |
|
626 sqlite3VdbeAddOp2(v, OP_SCopy, regBase+nCol, regBase+j); |
|
627 }else{ |
|
628 sqlite3VdbeAddOp3(v, OP_Column, iCur, idx, regBase+j); |
|
629 sqlite3ColumnDefault(v, pTab, idx); |
|
630 } |
|
631 } |
|
632 if( doMakeRec ){ |
|
633 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol+1, regOut); |
|
634 sqlite3IndexAffinityStr(v, pIdx); |
|
635 sqlite3ExprCacheAffinityChange(pParse, regBase, nCol+1); |
|
636 } |
|
637 sqlite3ReleaseTempRange(pParse, regBase, nCol+1); |
|
638 return regBase; |
|
639 } |
|
640 |
|
641 /* Make sure "isView" gets undefined in case this file becomes part of |
|
642 ** the amalgamation - so that subsequent files do not see isView as a |
|
643 ** macro. */ |
|
644 #undef isView |