blob: 1dfc7f244395f3293dbe53a951b49182a74de581 [file] [log] [blame]
drhcce7d172000-05-31 15:34:51 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drhcce7d172000-05-31 15:34:51 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drhcce7d172000-05-31 15:34:51 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drhcce7d172000-05-31 15:34:51 +000010**
11*************************************************************************
12** This file contains C code routines that are called by the parser
13** to handle DELETE FROM statements.
14**
drhe3184742002-06-19 14:27:05 +000015** $Id: delete.c,v 1.38 2002/06/19 14:27:05 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
drha76b5df2002-02-23 02:32:10 +000019
20/*
21** Given a table name, find the corresponding table and make sure the
22** table is writeable. Generate an error and return NULL if not. If
23** everything checks out, return a pointer to the Table structure.
24*/
25Table *sqliteTableNameToTable(Parse *pParse, const char *zTab){
26 Table *pTab;
27 pTab = sqliteFindTable(pParse->db, zTab);
28 if( pTab==0 ){
29 sqliteSetString(&pParse->zErrMsg, "no such table: ", zTab, 0);
30 pParse->nErr++;
31 return 0;
32 }
33 if( pTab->readOnly || pTab->pSelect ){
34 sqliteSetString(&pParse->zErrMsg,
35 pTab->pSelect ? "view " : "table ",
36 zTab,
37 " may not be modified", 0);
38 pParse->nErr++;
39 return 0;
40 }
41 return pTab;
42}
43
44/*
45** Given a table name, check to make sure the table exists, is writable
drhad3cab52002-05-24 02:04:32 +000046** and is not a view. If everything is OK, construct an SrcList holding
47** the table and return a pointer to the SrcList. The calling function
48** is responsible for freeing the SrcList when it has finished with it.
drha76b5df2002-02-23 02:32:10 +000049** If there is an error, leave a message on pParse->zErrMsg and return
50** NULL.
51*/
drhad3cab52002-05-24 02:04:32 +000052SrcList *sqliteTableTokenToSrcList(Parse *pParse, Token *pTableName){
drha76b5df2002-02-23 02:32:10 +000053 Table *pTab;
drhad3cab52002-05-24 02:04:32 +000054 SrcList *pTabList;
drha76b5df2002-02-23 02:32:10 +000055
drhad3cab52002-05-24 02:04:32 +000056 pTabList = sqliteSrcListAppend(0, pTableName);
drha76b5df2002-02-23 02:32:10 +000057 if( pTabList==0 ) return 0;
drhad3cab52002-05-24 02:04:32 +000058 assert( pTabList->nSrc==1 );
drha76b5df2002-02-23 02:32:10 +000059 pTab = sqliteTableNameToTable(pParse, pTabList->a[0].zName);
60 if( pTab==0 ){
drhad3cab52002-05-24 02:04:32 +000061 sqliteSrcListDelete(pTabList);
drha76b5df2002-02-23 02:32:10 +000062 return 0;
63 }
64 pTabList->a[0].pTab = pTab;
65 return pTabList;
66}
67
drhcce7d172000-05-31 15:34:51 +000068/*
69** Process a DELETE FROM statement.
70*/
71void sqliteDeleteFrom(
72 Parse *pParse, /* The parser context */
73 Token *pTableName, /* The table from which we should delete things */
74 Expr *pWhere /* The WHERE clause. May be null */
75){
76 Vdbe *v; /* The virtual database engine */
77 Table *pTab; /* The table from which records will be deleted */
drhc977f7f2002-05-21 11:38:11 +000078 char *zTab; /* Name of the table from which we are deleting */
drhad3cab52002-05-24 02:04:32 +000079 SrcList *pTabList; /* A fake FROM clause holding just pTab */
drhcce7d172000-05-31 15:34:51 +000080 int end, addr; /* A couple addresses of generated code */
81 int i; /* Loop counter */
82 WhereInfo *pWInfo; /* Information about the WHERE clause */
83 Index *pIdx; /* For looping over indices of the table */
drh4794b982000-06-06 13:54:14 +000084 int base; /* Index of the first available table cursor */
drhecdc7532001-09-23 02:35:53 +000085 sqlite *db; /* Main database structure */
drh1bee3d72001-10-15 00:44:35 +000086 int openOp; /* Opcode used to open a cursor to the table */
87
danielk1977c3f9bad2002-05-15 08:30:12 +000088 int row_triggers_exist = 0;
89 int oldIdx = -1;
drhcce7d172000-05-31 15:34:51 +000090
drhdaffd0e2001-04-11 14:28:42 +000091 if( pParse->nErr || sqlite_malloc_failed ){
92 pTabList = 0;
93 goto delete_from_cleanup;
94 }
drhecdc7532001-09-23 02:35:53 +000095 db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +000096
danielk1977c3f9bad2002-05-15 08:30:12 +000097 /* Check for the special case of a VIEW with one or more ON DELETE triggers
danielk1977f29ce552002-05-19 23:43:12 +000098 ** defined
99 */
drhc977f7f2002-05-21 11:38:11 +0000100 zTab = sqliteTableNameFromToken(pTableName);
101 if( zTab != 0 ){
102 pTab = sqliteFindTable(pParse->db, zTab);
103 if( pTab ){
104 row_triggers_exist =
105 sqliteTriggersExist(pParse, pTab->pTrigger,
106 TK_DELETE, TK_BEFORE, TK_ROW, 0) ||
107 sqliteTriggersExist(pParse, pTab->pTrigger,
108 TK_DELETE, TK_AFTER, TK_ROW, 0);
109 }
110 sqliteFree(zTab);
111 if( row_triggers_exist && pTab->pSelect ){
112 /* Just fire VIEW triggers */
113 sqliteViewTriggers(pParse, pTab, pWhere, OE_Replace, 0);
114 return;
danielk1977c3f9bad2002-05-15 08:30:12 +0000115 }
116 }
117
drh1ccde152000-06-17 13:12:39 +0000118 /* Locate the table which we want to delete. This table has to be
drhad3cab52002-05-24 02:04:32 +0000119 ** put in an SrcList structure because some of the subroutines we
drhcce7d172000-05-31 15:34:51 +0000120 ** will be calling are designed to work with multiple tables and expect
drhad3cab52002-05-24 02:04:32 +0000121 ** an SrcList* parameter instead of just a Table* parameter.
drhcce7d172000-05-31 15:34:51 +0000122 */
drhad3cab52002-05-24 02:04:32 +0000123 pTabList = sqliteTableTokenToSrcList(pParse, pTableName);
drhdaffd0e2001-04-11 14:28:42 +0000124 if( pTabList==0 ) goto delete_from_cleanup;
drhad3cab52002-05-24 02:04:32 +0000125 assert( pTabList->nSrc==1 );
drhcce7d172000-05-31 15:34:51 +0000126 pTab = pTabList->a[0].pTab;
drh417be792002-03-03 18:59:40 +0000127 assert( pTab->pSelect==0 ); /* This table is not a view */
drhcce7d172000-05-31 15:34:51 +0000128
drhc977f7f2002-05-21 11:38:11 +0000129 /* Allocate a cursor used to store the old.* data for a trigger.
130 */
danielk1977f29ce552002-05-19 23:43:12 +0000131 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000132 oldIdx = pParse->nTab++;
danielk1977f29ce552002-05-19 23:43:12 +0000133 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000134
drh967e8b72000-06-21 13:59:10 +0000135 /* Resolve the column names in all the expressions.
drhcce7d172000-05-31 15:34:51 +0000136 */
drh832508b2002-03-02 17:04:07 +0000137 base = pParse->nTab++;
drhcce7d172000-05-31 15:34:51 +0000138 if( pWhere ){
drh832508b2002-03-02 17:04:07 +0000139 if( sqliteExprResolveIds(pParse, base, pTabList, 0, pWhere) ){
drhcce7d172000-05-31 15:34:51 +0000140 goto delete_from_cleanup;
141 }
142 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
143 goto delete_from_cleanup;
144 }
145 }
146
147 /* Begin generating code.
148 */
drhd8bc7082000-06-07 23:51:50 +0000149 v = sqliteGetVdbe(pParse);
danielk1977f29ce552002-05-19 23:43:12 +0000150 if( v==0 ){
151 goto delete_from_cleanup;
152 }
drhc977f7f2002-05-21 11:38:11 +0000153 sqliteBeginWriteOperation(pParse, row_triggers_exist);
drh5e00f6c2001-09-13 13:46:56 +0000154
drh1bee3d72001-10-15 00:44:35 +0000155 /* Initialize the counter of the number of rows deleted, if
156 ** we are counting rows.
157 */
158 if( db->flags & SQLITE_CountRows ){
159 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
160 }
drhcce7d172000-05-31 15:34:51 +0000161
drh0353ced2001-03-20 22:05:00 +0000162 /* Special case: A DELETE without a WHERE clause deletes everything.
drhc977f7f2002-05-21 11:38:11 +0000163 ** It is easier just to erase the whole table. Note, however, that
164 ** this means that the row change count will be incorrect.
drhcce7d172000-05-31 15:34:51 +0000165 */
danielk1977f29ce552002-05-19 23:43:12 +0000166 if( pWhere==0 && !row_triggers_exist ){
drh1bee3d72001-10-15 00:44:35 +0000167 if( db->flags & SQLITE_CountRows ){
168 /* If counting rows deleted, just count the total number of
169 ** entries in the table. */
170 int endOfLoop = sqliteVdbeMakeLabel(v);
171 int addr;
172 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh832508b2002-03-02 17:04:07 +0000173 assert( base==0 );
drh1bee3d72001-10-15 00:44:35 +0000174 sqliteVdbeAddOp(v, openOp, 0, pTab->tnum);
drh6b563442001-11-07 16:48:26 +0000175 sqliteVdbeAddOp(v, OP_Rewind, 0, sqliteVdbeCurrentAddr(v)+2);
176 addr = sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
177 sqliteVdbeAddOp(v, OP_Next, 0, addr);
drh1bee3d72001-10-15 00:44:35 +0000178 sqliteVdbeResolveLabel(v, endOfLoop);
179 sqliteVdbeAddOp(v, OP_Close, 0, 0);
180 }
drh99fcd712001-10-13 01:06:47 +0000181 sqliteVdbeAddOp(v, OP_Clear, pTab->tnum, pTab->isTemp);
drh0353ced2001-03-20 22:05:00 +0000182 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh99fcd712001-10-13 01:06:47 +0000183 sqliteVdbeAddOp(v, OP_Clear, pIdx->tnum, pTab->isTemp);
drhcce7d172000-05-31 15:34:51 +0000184 }
185 }
drh0353ced2001-03-20 22:05:00 +0000186
187 /* The usual case: There is a WHERE clause so we have to scan through
188 ** the table an pick which records to delete.
189 */
190 else{
191 /* Begin the database scan
192 */
drhe3184742002-06-19 14:27:05 +0000193 pWInfo = sqliteWhereBegin(pParse, base, pTabList, pWhere, 1, 0);
drh0353ced2001-03-20 22:05:00 +0000194 if( pWInfo==0 ) goto delete_from_cleanup;
195
196 /* Remember the key of every item to be deleted.
197 */
drh99fcd712001-10-13 01:06:47 +0000198 sqliteVdbeAddOp(v, OP_ListWrite, 0, 0);
drh1bee3d72001-10-15 00:44:35 +0000199 if( db->flags & SQLITE_CountRows ){
200 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
201 }
drh0353ced2001-03-20 22:05:00 +0000202
203 /* End the database scan loop.
204 */
205 sqliteWhereEnd(pWInfo);
206
207 /* Delete every item whose key was written to the list during the
208 ** database scan. We have to delete items after the scan is complete
209 ** because deleting an item can change the scan order.
210 */
drh99fcd712001-10-13 01:06:47 +0000211 sqliteVdbeAddOp(v, OP_ListRewind, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000212 end = sqliteVdbeMakeLabel(v);
213
drhc977f7f2002-05-21 11:38:11 +0000214 /* This is the beginning of the delete loop when there are
215 ** row triggers.
216 */
danielk1977f29ce552002-05-19 23:43:12 +0000217 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000218 addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end);
219 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
220
221 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
222 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
223 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
224 sqliteVdbeAddOp(v, OP_OpenTemp, oldIdx, 0);
225
226 sqliteVdbeAddOp(v, OP_Integer, 13, 0);
drhc977f7f2002-05-21 11:38:11 +0000227 for(i = 0; i<pTab->nCol; i++){
228 if( i==pTab->iPKey ){
drh9adf9ac2002-05-15 11:44:13 +0000229 sqliteVdbeAddOp(v, OP_Recno, base, 0);
danielk1977f29ce552002-05-19 23:43:12 +0000230 } else {
drhc977f7f2002-05-21 11:38:11 +0000231 sqliteVdbeAddOp(v, OP_Column, base, i);
danielk1977f29ce552002-05-19 23:43:12 +0000232 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000233 }
234 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
235 sqliteVdbeAddOp(v, OP_PutIntKey, oldIdx, 0);
236 sqliteVdbeAddOp(v, OP_Close, base, 0);
237 sqliteVdbeAddOp(v, OP_Rewind, oldIdx, 0);
238
239 sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_BEFORE, pTab, -1,
danielk19776f349032002-06-11 02:25:40 +0000240 oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default,
241 addr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000242 }
243
drhc977f7f2002-05-21 11:38:11 +0000244 /* Open cursors for the table we are deleting from and all its
245 ** indices. If there are row triggers, this happens inside the
246 ** OP_ListRead loop because the cursor have to all be closed
247 ** before the trigger fires. If there are no row triggers, the
248 ** cursors are opened only once on the outside the loop.
249 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000250 pParse->nTab = base + 1;
drhf57b3392001-10-08 13:22:32 +0000251 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
drh99fcd712001-10-13 01:06:47 +0000252 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
drh0353ced2001-03-20 22:05:00 +0000253 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +0000254 sqliteVdbeAddOp(v, openOp, pParse->nTab++, pIdx->tnum);
drh0353ced2001-03-20 22:05:00 +0000255 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000256
drhc977f7f2002-05-21 11:38:11 +0000257 /* This is the beginning of the delete loop when there are no
258 ** row triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000259 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000260 addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end);
danielk1977f29ce552002-05-19 23:43:12 +0000261 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000262
drhc977f7f2002-05-21 11:38:11 +0000263 /* Delete the row */
264 sqliteGenerateRowDelete(v, pTab, base, pParse->trigStack==0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000265
drhc977f7f2002-05-21 11:38:11 +0000266 /* If there are row triggers, close all cursors then invoke
267 ** the AFTER triggers
268 */
danielk1977f29ce552002-05-19 23:43:12 +0000269 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000270 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
drh9adf9ac2002-05-15 11:44:13 +0000271 sqliteVdbeAddOp(v, OP_Close, base + i, pIdx->tnum);
danielk1977c3f9bad2002-05-15 08:30:12 +0000272 }
273 sqliteVdbeAddOp(v, OP_Close, base, 0);
274 sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_AFTER, pTab, -1,
danielk19776f349032002-06-11 02:25:40 +0000275 oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default,
276 addr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000277 }
278
drhc977f7f2002-05-21 11:38:11 +0000279 /* End of the delete loop */
drh99fcd712001-10-13 01:06:47 +0000280 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
281 sqliteVdbeResolveLabel(v, end);
drha8b38d22001-11-01 14:41:34 +0000282 sqliteVdbeAddOp(v, OP_ListReset, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000283
drhc977f7f2002-05-21 11:38:11 +0000284 /* Close the cursors after the loop if there are no row triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000285 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000286 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
drh9adf9ac2002-05-15 11:44:13 +0000287 sqliteVdbeAddOp(v, OP_Close, base + i, pIdx->tnum);
danielk1977c3f9bad2002-05-15 08:30:12 +0000288 }
289 sqliteVdbeAddOp(v, OP_Close, base, 0);
290 pParse->nTab = base;
291 }
drh0353ced2001-03-20 22:05:00 +0000292 }
drh1c928532002-01-31 15:54:21 +0000293 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +0000294
drh1bee3d72001-10-15 00:44:35 +0000295 /*
296 ** Return the number of rows that were deleted.
297 */
298 if( db->flags & SQLITE_CountRows ){
299 sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0);
300 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
301 sqliteVdbeChangeP3(v, -1, "rows deleted", P3_STATIC);
302 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
303 }
drhcce7d172000-05-31 15:34:51 +0000304
305delete_from_cleanup:
drhad3cab52002-05-24 02:04:32 +0000306 sqliteSrcListDelete(pTabList);
drhcce7d172000-05-31 15:34:51 +0000307 sqliteExprDelete(pWhere);
308 return;
309}
drh9cfcf5d2002-01-29 18:41:24 +0000310
311/*
312** This routine generates VDBE code that causes a single row of a
313** single table to be deleted.
314**
315** The VDBE must be in a particular state when this routine is called.
316** These are the requirements:
317**
318** 1. A read/write cursor pointing to pTab, the table containing the row
319** to be deleted, must be opened as cursor number "base".
320**
321** 2. Read/write cursors for all indices of pTab must be open as
322** cursor number base+i for the i-th index.
323**
324** 3. The record number of the row to be deleted must be on the top
325** of the stack.
326**
327** This routine pops the top of the stack to remove the record number
328** and then generates code to remove both the table record and all index
329** entries that point to that record.
330*/
331void sqliteGenerateRowDelete(
332 Vdbe *v, /* Generate code into this VDBE */
333 Table *pTab, /* Table containing the row to be deleted */
drhc8d30ac2002-04-12 10:08:59 +0000334 int base, /* Cursor number for the table */
335 int count /* Increment the row change counter */
drh9cfcf5d2002-01-29 18:41:24 +0000336){
drh07d6e3a2002-05-23 12:50:18 +0000337 int addr;
338 addr = sqliteVdbeAddOp(v, OP_NotExists, base, 0);
drh0ca3e242002-01-29 23:07:02 +0000339 sqliteGenerateRowIndexDelete(v, pTab, base, 0);
drhc8d30ac2002-04-12 10:08:59 +0000340 sqliteVdbeAddOp(v, OP_Delete, base, count);
drh07d6e3a2002-05-23 12:50:18 +0000341 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drh0ca3e242002-01-29 23:07:02 +0000342}
343
344/*
345** This routine generates VDBE code that causes the deletion of all
346** index entries associated with a single row of a single table.
347**
348** The VDBE must be in a particular state when this routine is called.
349** These are the requirements:
350**
351** 1. A read/write cursor pointing to pTab, the table containing the row
352** to be deleted, must be opened as cursor number "base".
353**
354** 2. Read/write cursors for all indices of pTab must be open as
355** cursor number base+i for the i-th index.
356**
357** 3. The "base" cursor must be pointing to the row that is to be
358** deleted.
359*/
360void sqliteGenerateRowIndexDelete(
361 Vdbe *v, /* Generate code into this VDBE */
362 Table *pTab, /* Table containing the row to be deleted */
363 int base, /* Cursor number for the table */
364 char *aIdxUsed /* Only delete if aIdxUsed!=0 && aIdxUsed[i]!=0 */
365){
drh9cfcf5d2002-01-29 18:41:24 +0000366 int i;
367 Index *pIdx;
368
drh0ca3e242002-01-29 23:07:02 +0000369 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
370 int j;
371 if( aIdxUsed!=0 && aIdxUsed[i-1]==0 ) continue;
372 sqliteVdbeAddOp(v, OP_Recno, base, 0);
373 for(j=0; j<pIdx->nColumn; j++){
374 int idx = pIdx->aiColumn[j];
375 if( idx==pTab->iPKey ){
376 sqliteVdbeAddOp(v, OP_Dup, j, 0);
377 }else{
378 sqliteVdbeAddOp(v, OP_Column, base, idx);
drh9cfcf5d2002-01-29 18:41:24 +0000379 }
drh9cfcf5d2002-01-29 18:41:24 +0000380 }
drh0ca3e242002-01-29 23:07:02 +0000381 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
382 sqliteVdbeAddOp(v, OP_IdxDelete, base+i, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000383 }
drh9cfcf5d2002-01-29 18:41:24 +0000384}