blob: 417ed2fd66026f64d16661aa71339ff4da47572d [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**
danielk1977f29ce552002-05-19 23:43:12 +000015** $Id: delete.c,v 1.33 2002/05/19 23:43:14 danielk1977 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
46** and is not a view. If everything is OK, construct an IdList holding
47** the table and return a pointer to the IdList. The calling function
48** is responsible for freeing the IdList when it has finished with it.
49** If there is an error, leave a message on pParse->zErrMsg and return
50** NULL.
51*/
52IdList *sqliteTableTokenToIdList(Parse *pParse, Token *pTableName){
53 Table *pTab;
54 IdList *pTabList;
55
56 pTabList = sqliteIdListAppend(0, pTableName);
57 if( pTabList==0 ) return 0;
58 assert( pTabList->nId==1 );
59 pTab = sqliteTableNameToTable(pParse, pTabList->a[0].zName);
60 if( pTab==0 ){
61 sqliteIdListDelete(pTabList);
62 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 */
78 IdList *pTabList; /* An ID list holding pTab and nothing else */
79 int end, addr; /* A couple addresses of generated code */
80 int i; /* Loop counter */
81 WhereInfo *pWInfo; /* Information about the WHERE clause */
82 Index *pIdx; /* For looping over indices of the table */
drh4794b982000-06-06 13:54:14 +000083 int base; /* Index of the first available table cursor */
drhecdc7532001-09-23 02:35:53 +000084 sqlite *db; /* Main database structure */
drh1bee3d72001-10-15 00:44:35 +000085 int openOp; /* Opcode used to open a cursor to the table */
86
danielk1977c3f9bad2002-05-15 08:30:12 +000087 int row_triggers_exist = 0;
88 int oldIdx = -1;
drhcce7d172000-05-31 15:34:51 +000089
drhdaffd0e2001-04-11 14:28:42 +000090 if( pParse->nErr || sqlite_malloc_failed ){
91 pTabList = 0;
92 goto delete_from_cleanup;
93 }
drhecdc7532001-09-23 02:35:53 +000094 db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +000095
danielk1977c3f9bad2002-05-15 08:30:12 +000096 /* Check for the special case of a VIEW with one or more ON DELETE triggers
danielk1977f29ce552002-05-19 23:43:12 +000097 ** defined
98 */
danielk1977c3f9bad2002-05-15 08:30:12 +000099 {
danielk1977f29ce552002-05-19 23:43:12 +0000100 Table *pTab;
101 char *zTab = sqliteTableNameFromToken(pTableName);
danielk1977c3f9bad2002-05-15 08:30:12 +0000102
danielk1977f29ce552002-05-19 23:43:12 +0000103 if( zTab != 0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000104 pTab = sqliteFindTable(pParse->db, zTab);
danielk1977f29ce552002-05-19 23:43:12 +0000105 if( pTab ){
drh9adf9ac2002-05-15 11:44:13 +0000106 row_triggers_exist =
107 sqliteTriggersExist(pParse, pTab->pTrigger,
108 TK_DELETE, TK_BEFORE, TK_ROW, 0) ||
109 sqliteTriggersExist(pParse, pTab->pTrigger,
110 TK_DELETE, TK_AFTER, TK_ROW, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000111 }
112 sqliteFree(zTab);
danielk1977f29ce552002-05-19 23:43:12 +0000113 if( row_triggers_exist && pTab->pSelect ){
drh9adf9ac2002-05-15 11:44:13 +0000114 /* Just fire VIEW triggers */
115 sqliteViewTriggers(pParse, pTab, pWhere, OE_Replace, 0);
116 return;
danielk1977c3f9bad2002-05-15 08:30:12 +0000117 }
118 }
119 }
120
drh1ccde152000-06-17 13:12:39 +0000121 /* Locate the table which we want to delete. This table has to be
122 ** put in an IdList structure because some of the subroutines we
drhcce7d172000-05-31 15:34:51 +0000123 ** will be calling are designed to work with multiple tables and expect
124 ** an IdList* parameter instead of just a Table* parameger.
125 */
drha76b5df2002-02-23 02:32:10 +0000126 pTabList = sqliteTableTokenToIdList(pParse, pTableName);
drhdaffd0e2001-04-11 14:28:42 +0000127 if( pTabList==0 ) goto delete_from_cleanup;
drh417be792002-03-03 18:59:40 +0000128 assert( pTabList->nId==1 );
drhcce7d172000-05-31 15:34:51 +0000129 pTab = pTabList->a[0].pTab;
drh417be792002-03-03 18:59:40 +0000130 assert( pTab->pSelect==0 ); /* This table is not a view */
drhcce7d172000-05-31 15:34:51 +0000131
danielk1977f29ce552002-05-19 23:43:12 +0000132 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000133 oldIdx = pParse->nTab++;
danielk1977f29ce552002-05-19 23:43:12 +0000134 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000135
drh967e8b72000-06-21 13:59:10 +0000136 /* Resolve the column names in all the expressions.
drhcce7d172000-05-31 15:34:51 +0000137 */
drh832508b2002-03-02 17:04:07 +0000138 base = pParse->nTab++;
drhcce7d172000-05-31 15:34:51 +0000139 if( pWhere ){
drh832508b2002-03-02 17:04:07 +0000140 if( sqliteExprResolveIds(pParse, base, pTabList, 0, pWhere) ){
drhcce7d172000-05-31 15:34:51 +0000141 goto delete_from_cleanup;
142 }
143 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
144 goto delete_from_cleanup;
145 }
146 }
147
148 /* Begin generating code.
149 */
drhd8bc7082000-06-07 23:51:50 +0000150 v = sqliteGetVdbe(pParse);
danielk1977f29ce552002-05-19 23:43:12 +0000151 if( v==0 ){
152 goto delete_from_cleanup;
153 }
154 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000155 sqliteBeginMultiWriteOperation(pParse);
danielk1977f29ce552002-05-19 23:43:12 +0000156 } else {
danielk1977c3f9bad2002-05-15 08:30:12 +0000157 sqliteBeginWriteOperation(pParse);
danielk1977f29ce552002-05-19 23:43:12 +0000158 }
drh5e00f6c2001-09-13 13:46:56 +0000159
drh1bee3d72001-10-15 00:44:35 +0000160 /* Initialize the counter of the number of rows deleted, if
161 ** we are counting rows.
162 */
163 if( db->flags & SQLITE_CountRows ){
164 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
165 }
drhcce7d172000-05-31 15:34:51 +0000166
drh0353ced2001-03-20 22:05:00 +0000167 /* Special case: A DELETE without a WHERE clause deletes everything.
drhecdc7532001-09-23 02:35:53 +0000168 ** It is easier just to erase the whole table.
drhcce7d172000-05-31 15:34:51 +0000169 */
danielk1977f29ce552002-05-19 23:43:12 +0000170 if( pWhere==0 && !row_triggers_exist ){
drh1bee3d72001-10-15 00:44:35 +0000171 if( db->flags & SQLITE_CountRows ){
172 /* If counting rows deleted, just count the total number of
173 ** entries in the table. */
174 int endOfLoop = sqliteVdbeMakeLabel(v);
175 int addr;
176 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh832508b2002-03-02 17:04:07 +0000177 assert( base==0 );
drh1bee3d72001-10-15 00:44:35 +0000178 sqliteVdbeAddOp(v, openOp, 0, pTab->tnum);
drh6b563442001-11-07 16:48:26 +0000179 sqliteVdbeAddOp(v, OP_Rewind, 0, sqliteVdbeCurrentAddr(v)+2);
180 addr = sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
181 sqliteVdbeAddOp(v, OP_Next, 0, addr);
drh1bee3d72001-10-15 00:44:35 +0000182 sqliteVdbeResolveLabel(v, endOfLoop);
183 sqliteVdbeAddOp(v, OP_Close, 0, 0);
184 }
drh99fcd712001-10-13 01:06:47 +0000185 sqliteVdbeAddOp(v, OP_Clear, pTab->tnum, pTab->isTemp);
drh0353ced2001-03-20 22:05:00 +0000186 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh99fcd712001-10-13 01:06:47 +0000187 sqliteVdbeAddOp(v, OP_Clear, pIdx->tnum, pTab->isTemp);
drhcce7d172000-05-31 15:34:51 +0000188 }
189 }
drh0353ced2001-03-20 22:05:00 +0000190
191 /* The usual case: There is a WHERE clause so we have to scan through
192 ** the table an pick which records to delete.
193 */
194 else{
195 /* Begin the database scan
196 */
drh832508b2002-03-02 17:04:07 +0000197 pWInfo = sqliteWhereBegin(pParse, base, pTabList, pWhere, 1);
drh0353ced2001-03-20 22:05:00 +0000198 if( pWInfo==0 ) goto delete_from_cleanup;
199
200 /* Remember the key of every item to be deleted.
201 */
drh99fcd712001-10-13 01:06:47 +0000202 sqliteVdbeAddOp(v, OP_ListWrite, 0, 0);
drh1bee3d72001-10-15 00:44:35 +0000203 if( db->flags & SQLITE_CountRows ){
204 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
205 }
drh0353ced2001-03-20 22:05:00 +0000206
207 /* End the database scan loop.
208 */
209 sqliteWhereEnd(pWInfo);
210
211 /* Delete every item whose key was written to the list during the
212 ** database scan. We have to delete items after the scan is complete
213 ** because deleting an item can change the scan order.
214 */
drh99fcd712001-10-13 01:06:47 +0000215 sqliteVdbeAddOp(v, OP_ListRewind, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000216 end = sqliteVdbeMakeLabel(v);
217
danielk1977f29ce552002-05-19 23:43:12 +0000218 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000219 int ii;
220 addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end);
221 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
222
223 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
224 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
225 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
226 sqliteVdbeAddOp(v, OP_OpenTemp, oldIdx, 0);
227
228 sqliteVdbeAddOp(v, OP_Integer, 13, 0);
danielk1977f29ce552002-05-19 23:43:12 +0000229 for(ii = 0; ii<pTab->nCol; ii++){
230 if( ii==pTab->iPKey ){
drh9adf9ac2002-05-15 11:44:13 +0000231 sqliteVdbeAddOp(v, OP_Recno, base, 0);
danielk1977f29ce552002-05-19 23:43:12 +0000232 } else {
drh9adf9ac2002-05-15 11:44:13 +0000233 sqliteVdbeAddOp(v, OP_Column, base, ii);
danielk1977f29ce552002-05-19 23:43:12 +0000234 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000235 }
236 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
237 sqliteVdbeAddOp(v, OP_PutIntKey, oldIdx, 0);
238 sqliteVdbeAddOp(v, OP_Close, base, 0);
239 sqliteVdbeAddOp(v, OP_Rewind, oldIdx, 0);
240
241 sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_BEFORE, pTab, -1,
drh9adf9ac2002-05-15 11:44:13 +0000242 oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default);
danielk1977c3f9bad2002-05-15 08:30:12 +0000243 }
244
245 pParse->nTab = base + 1;
drhf57b3392001-10-08 13:22:32 +0000246 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
drh99fcd712001-10-13 01:06:47 +0000247 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
drh0353ced2001-03-20 22:05:00 +0000248 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
danielk1977c3f9bad2002-05-15 08:30:12 +0000249 sqliteVdbeAddOp(v, openOp, pParse->nTab++, pIdx->tnum);
drh0353ced2001-03-20 22:05:00 +0000250 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000251
danielk1977f29ce552002-05-19 23:43:12 +0000252 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000253 addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end);
danielk1977f29ce552002-05-19 23:43:12 +0000254 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000255
256 sqliteGenerateRowDelete(v, pTab, base, pParse->trigStack?0:1);
257
danielk1977f29ce552002-05-19 23:43:12 +0000258 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000259 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
drh9adf9ac2002-05-15 11:44:13 +0000260 sqliteVdbeAddOp(v, OP_Close, base + i, pIdx->tnum);
danielk1977c3f9bad2002-05-15 08:30:12 +0000261 }
262 sqliteVdbeAddOp(v, OP_Close, base, 0);
263 sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_AFTER, pTab, -1,
drh9adf9ac2002-05-15 11:44:13 +0000264 oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default);
danielk1977c3f9bad2002-05-15 08:30:12 +0000265 }
266
drh99fcd712001-10-13 01:06:47 +0000267 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
268 sqliteVdbeResolveLabel(v, end);
drha8b38d22001-11-01 14:41:34 +0000269 sqliteVdbeAddOp(v, OP_ListReset, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000270
danielk1977f29ce552002-05-19 23:43:12 +0000271 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000272 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
drh9adf9ac2002-05-15 11:44:13 +0000273 sqliteVdbeAddOp(v, OP_Close, base + i, pIdx->tnum);
danielk1977c3f9bad2002-05-15 08:30:12 +0000274 }
275 sqliteVdbeAddOp(v, OP_Close, base, 0);
276 pParse->nTab = base;
277 }
drh0353ced2001-03-20 22:05:00 +0000278 }
drh1c928532002-01-31 15:54:21 +0000279 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +0000280
drh1bee3d72001-10-15 00:44:35 +0000281 /*
282 ** Return the number of rows that were deleted.
283 */
284 if( db->flags & SQLITE_CountRows ){
285 sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0);
286 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
287 sqliteVdbeChangeP3(v, -1, "rows deleted", P3_STATIC);
288 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
289 }
drhcce7d172000-05-31 15:34:51 +0000290
291delete_from_cleanup:
292 sqliteIdListDelete(pTabList);
293 sqliteExprDelete(pWhere);
294 return;
295}
drh9cfcf5d2002-01-29 18:41:24 +0000296
297/*
298** This routine generates VDBE code that causes a single row of a
299** single table to be deleted.
300**
301** The VDBE must be in a particular state when this routine is called.
302** These are the requirements:
303**
304** 1. A read/write cursor pointing to pTab, the table containing the row
305** to be deleted, must be opened as cursor number "base".
306**
307** 2. Read/write cursors for all indices of pTab must be open as
308** cursor number base+i for the i-th index.
309**
310** 3. The record number of the row to be deleted must be on the top
311** of the stack.
312**
313** This routine pops the top of the stack to remove the record number
314** and then generates code to remove both the table record and all index
315** entries that point to that record.
316*/
317void sqliteGenerateRowDelete(
318 Vdbe *v, /* Generate code into this VDBE */
319 Table *pTab, /* Table containing the row to be deleted */
drhc8d30ac2002-04-12 10:08:59 +0000320 int base, /* Cursor number for the table */
321 int count /* Increment the row change counter */
drh9cfcf5d2002-01-29 18:41:24 +0000322){
drh0ca3e242002-01-29 23:07:02 +0000323 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
324 sqliteGenerateRowIndexDelete(v, pTab, base, 0);
drhc8d30ac2002-04-12 10:08:59 +0000325 sqliteVdbeAddOp(v, OP_Delete, base, count);
drh0ca3e242002-01-29 23:07:02 +0000326}
327
328/*
329** This routine generates VDBE code that causes the deletion of all
330** index entries associated with a single row of a single table.
331**
332** The VDBE must be in a particular state when this routine is called.
333** These are the requirements:
334**
335** 1. A read/write cursor pointing to pTab, the table containing the row
336** to be deleted, must be opened as cursor number "base".
337**
338** 2. Read/write cursors for all indices of pTab must be open as
339** cursor number base+i for the i-th index.
340**
341** 3. The "base" cursor must be pointing to the row that is to be
342** deleted.
343*/
344void sqliteGenerateRowIndexDelete(
345 Vdbe *v, /* Generate code into this VDBE */
346 Table *pTab, /* Table containing the row to be deleted */
347 int base, /* Cursor number for the table */
348 char *aIdxUsed /* Only delete if aIdxUsed!=0 && aIdxUsed[i]!=0 */
349){
drh9cfcf5d2002-01-29 18:41:24 +0000350 int i;
351 Index *pIdx;
352
drh0ca3e242002-01-29 23:07:02 +0000353 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
354 int j;
355 if( aIdxUsed!=0 && aIdxUsed[i-1]==0 ) continue;
356 sqliteVdbeAddOp(v, OP_Recno, base, 0);
357 for(j=0; j<pIdx->nColumn; j++){
358 int idx = pIdx->aiColumn[j];
359 if( idx==pTab->iPKey ){
360 sqliteVdbeAddOp(v, OP_Dup, j, 0);
361 }else{
362 sqliteVdbeAddOp(v, OP_Column, base, idx);
drh9cfcf5d2002-01-29 18:41:24 +0000363 }
drh9cfcf5d2002-01-29 18:41:24 +0000364 }
drh0ca3e242002-01-29 23:07:02 +0000365 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
366 sqliteVdbeAddOp(v, OP_IdxDelete, base+i, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000367 }
drh9cfcf5d2002-01-29 18:41:24 +0000368}