blob: c55c6f37ecc8aaf5b6122ebc6b391c167585a439 [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**
drh113088e2003-03-20 01:16:58 +000015** $Id: delete.c,v 1.47 2003/03/20 01:16:59 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/*
drhcce7d172000-05-31 15:34:51 +000045** Process a DELETE FROM statement.
46*/
47void sqliteDeleteFrom(
48 Parse *pParse, /* The parser context */
drh113088e2003-03-20 01:16:58 +000049 SrcList *pTabList, /* The table from which we should delete things */
drhcce7d172000-05-31 15:34:51 +000050 Expr *pWhere /* The WHERE clause. May be null */
51){
52 Vdbe *v; /* The virtual database engine */
53 Table *pTab; /* The table from which records will be deleted */
drhc977f7f2002-05-21 11:38:11 +000054 char *zTab; /* Name of the table from which we are deleting */
drhcce7d172000-05-31 15:34:51 +000055 int end, addr; /* A couple addresses of generated code */
56 int i; /* Loop counter */
57 WhereInfo *pWInfo; /* Information about the WHERE clause */
58 Index *pIdx; /* For looping over indices of the table */
drh4794b982000-06-06 13:54:14 +000059 int base; /* Index of the first available table cursor */
drhecdc7532001-09-23 02:35:53 +000060 sqlite *db; /* Main database structure */
drh1bee3d72001-10-15 00:44:35 +000061
danielk1977c3f9bad2002-05-15 08:30:12 +000062 int row_triggers_exist = 0;
63 int oldIdx = -1;
drhcce7d172000-05-31 15:34:51 +000064
drhe5f9c642003-01-13 23:27:31 +000065 if( pParse->nErr || sqlite_malloc_failed ){
drhdaffd0e2001-04-11 14:28:42 +000066 pTabList = 0;
67 goto delete_from_cleanup;
68 }
drhecdc7532001-09-23 02:35:53 +000069 db = pParse->db;
drh113088e2003-03-20 01:16:58 +000070 assert( pTabList->nSrc==1 );
drhdaffd0e2001-04-11 14:28:42 +000071
danielk1977c3f9bad2002-05-15 08:30:12 +000072 /* Check for the special case of a VIEW with one or more ON DELETE triggers
danielk1977f29ce552002-05-19 23:43:12 +000073 ** defined
74 */
drh113088e2003-03-20 01:16:58 +000075 zTab = pTabList->a[0].zName;
drhc977f7f2002-05-21 11:38:11 +000076 if( zTab != 0 ){
77 pTab = sqliteFindTable(pParse->db, zTab);
78 if( pTab ){
79 row_triggers_exist =
80 sqliteTriggersExist(pParse, pTab->pTrigger,
81 TK_DELETE, TK_BEFORE, TK_ROW, 0) ||
82 sqliteTriggersExist(pParse, pTab->pTrigger,
83 TK_DELETE, TK_AFTER, TK_ROW, 0);
84 }
drhc977f7f2002-05-21 11:38:11 +000085 if( row_triggers_exist && pTab->pSelect ){
86 /* Just fire VIEW triggers */
drh113088e2003-03-20 01:16:58 +000087 sqliteSrcListDelete(pTabList);
drhc977f7f2002-05-21 11:38:11 +000088 sqliteViewTriggers(pParse, pTab, pWhere, OE_Replace, 0);
89 return;
danielk1977c3f9bad2002-05-15 08:30:12 +000090 }
91 }
92
drh1ccde152000-06-17 13:12:39 +000093 /* Locate the table which we want to delete. This table has to be
drhad3cab52002-05-24 02:04:32 +000094 ** put in an SrcList structure because some of the subroutines we
drhcce7d172000-05-31 15:34:51 +000095 ** will be calling are designed to work with multiple tables and expect
drhad3cab52002-05-24 02:04:32 +000096 ** an SrcList* parameter instead of just a Table* parameter.
drhcce7d172000-05-31 15:34:51 +000097 */
drh113088e2003-03-20 01:16:58 +000098 pTab = pTabList->a[0].pTab = sqliteTableNameToTable(pParse, zTab);
99 if( pTab==0 ){
100 goto delete_from_cleanup;
101 }
drh417be792002-03-03 18:59:40 +0000102 assert( pTab->pSelect==0 ); /* This table is not a view */
drhe5f9c642003-01-13 23:27:31 +0000103 if( sqliteAuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0) ){
104 goto delete_from_cleanup;
105 }
drhcce7d172000-05-31 15:34:51 +0000106
drhc977f7f2002-05-21 11:38:11 +0000107 /* Allocate a cursor used to store the old.* data for a trigger.
108 */
danielk1977f29ce552002-05-19 23:43:12 +0000109 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000110 oldIdx = pParse->nTab++;
danielk1977f29ce552002-05-19 23:43:12 +0000111 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000112
drh967e8b72000-06-21 13:59:10 +0000113 /* Resolve the column names in all the expressions.
drhcce7d172000-05-31 15:34:51 +0000114 */
drh832508b2002-03-02 17:04:07 +0000115 base = pParse->nTab++;
drhcce7d172000-05-31 15:34:51 +0000116 if( pWhere ){
drh832508b2002-03-02 17:04:07 +0000117 if( sqliteExprResolveIds(pParse, base, pTabList, 0, pWhere) ){
drhcce7d172000-05-31 15:34:51 +0000118 goto delete_from_cleanup;
119 }
120 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
121 goto delete_from_cleanup;
122 }
123 }
124
125 /* Begin generating code.
126 */
drhd8bc7082000-06-07 23:51:50 +0000127 v = sqliteGetVdbe(pParse);
danielk1977f29ce552002-05-19 23:43:12 +0000128 if( v==0 ){
129 goto delete_from_cleanup;
130 }
drhcabb0812002-09-14 13:47:32 +0000131 sqliteBeginWriteOperation(pParse, row_triggers_exist,
132 !row_triggers_exist && pTab->isTemp);
drh5e00f6c2001-09-13 13:46:56 +0000133
drh1bee3d72001-10-15 00:44:35 +0000134 /* Initialize the counter of the number of rows deleted, if
135 ** we are counting rows.
136 */
137 if( db->flags & SQLITE_CountRows ){
138 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
139 }
drhcce7d172000-05-31 15:34:51 +0000140
drh0353ced2001-03-20 22:05:00 +0000141 /* Special case: A DELETE without a WHERE clause deletes everything.
drhc977f7f2002-05-21 11:38:11 +0000142 ** It is easier just to erase the whole table. Note, however, that
143 ** this means that the row change count will be incorrect.
drhcce7d172000-05-31 15:34:51 +0000144 */
danielk1977f29ce552002-05-19 23:43:12 +0000145 if( pWhere==0 && !row_triggers_exist ){
drh1bee3d72001-10-15 00:44:35 +0000146 if( db->flags & SQLITE_CountRows ){
147 /* If counting rows deleted, just count the total number of
148 ** entries in the table. */
149 int endOfLoop = sqliteVdbeMakeLabel(v);
150 int addr;
drh001bbcb2003-03-19 03:14:00 +0000151 sqliteVdbeAddOp(v, OP_Integer, pTab->isTemp, 0);
152 sqliteVdbeAddOp(v, OP_OpenRead, base, pTab->tnum);
drh26b3e1b2002-07-19 18:52:40 +0000153 sqliteVdbeAddOp(v, OP_Rewind, base, sqliteVdbeCurrentAddr(v)+2);
drh6b563442001-11-07 16:48:26 +0000154 addr = sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
drh26b3e1b2002-07-19 18:52:40 +0000155 sqliteVdbeAddOp(v, OP_Next, base, addr);
drh1bee3d72001-10-15 00:44:35 +0000156 sqliteVdbeResolveLabel(v, endOfLoop);
drh26b3e1b2002-07-19 18:52:40 +0000157 sqliteVdbeAddOp(v, OP_Close, base, 0);
drh1bee3d72001-10-15 00:44:35 +0000158 }
drh99fcd712001-10-13 01:06:47 +0000159 sqliteVdbeAddOp(v, OP_Clear, pTab->tnum, pTab->isTemp);
drh0353ced2001-03-20 22:05:00 +0000160 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh99fcd712001-10-13 01:06:47 +0000161 sqliteVdbeAddOp(v, OP_Clear, pIdx->tnum, pTab->isTemp);
drhcce7d172000-05-31 15:34:51 +0000162 }
163 }
drh0353ced2001-03-20 22:05:00 +0000164
165 /* The usual case: There is a WHERE clause so we have to scan through
166 ** the table an pick which records to delete.
167 */
168 else{
169 /* Begin the database scan
170 */
drhe3184742002-06-19 14:27:05 +0000171 pWInfo = sqliteWhereBegin(pParse, base, pTabList, pWhere, 1, 0);
drh0353ced2001-03-20 22:05:00 +0000172 if( pWInfo==0 ) goto delete_from_cleanup;
173
174 /* Remember the key of every item to be deleted.
175 */
drh99fcd712001-10-13 01:06:47 +0000176 sqliteVdbeAddOp(v, OP_ListWrite, 0, 0);
drh1bee3d72001-10-15 00:44:35 +0000177 if( db->flags & SQLITE_CountRows ){
178 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
179 }
drh0353ced2001-03-20 22:05:00 +0000180
181 /* End the database scan loop.
182 */
183 sqliteWhereEnd(pWInfo);
184
185 /* Delete every item whose key was written to the list during the
186 ** database scan. We have to delete items after the scan is complete
187 ** because deleting an item can change the scan order.
188 */
drh99fcd712001-10-13 01:06:47 +0000189 sqliteVdbeAddOp(v, OP_ListRewind, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000190 end = sqliteVdbeMakeLabel(v);
191
drhc977f7f2002-05-21 11:38:11 +0000192 /* This is the beginning of the delete loop when there are
193 ** row triggers.
194 */
danielk1977f29ce552002-05-19 23:43:12 +0000195 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000196 addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end);
197 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drh001bbcb2003-03-19 03:14:00 +0000198 sqliteVdbeAddOp(v, OP_Integer, pTab->isTemp, 0);
199 sqliteVdbeAddOp(v, OP_OpenRead, base, pTab->tnum);
danielk1977c3f9bad2002-05-15 08:30:12 +0000200 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
201 sqliteVdbeAddOp(v, OP_OpenTemp, oldIdx, 0);
202
203 sqliteVdbeAddOp(v, OP_Integer, 13, 0);
drhc977f7f2002-05-21 11:38:11 +0000204 for(i = 0; i<pTab->nCol; i++){
205 if( i==pTab->iPKey ){
drh9adf9ac2002-05-15 11:44:13 +0000206 sqliteVdbeAddOp(v, OP_Recno, base, 0);
danielk1977f29ce552002-05-19 23:43:12 +0000207 } else {
drhc977f7f2002-05-21 11:38:11 +0000208 sqliteVdbeAddOp(v, OP_Column, base, i);
danielk1977f29ce552002-05-19 23:43:12 +0000209 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000210 }
211 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
212 sqliteVdbeAddOp(v, OP_PutIntKey, oldIdx, 0);
213 sqliteVdbeAddOp(v, OP_Close, base, 0);
214 sqliteVdbeAddOp(v, OP_Rewind, oldIdx, 0);
215
216 sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_BEFORE, pTab, -1,
danielk19776f349032002-06-11 02:25:40 +0000217 oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default,
218 addr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000219 }
220
drhc977f7f2002-05-21 11:38:11 +0000221 /* Open cursors for the table we are deleting from and all its
222 ** indices. If there are row triggers, this happens inside the
223 ** OP_ListRead loop because the cursor have to all be closed
224 ** before the trigger fires. If there are no row triggers, the
225 ** cursors are opened only once on the outside the loop.
226 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000227 pParse->nTab = base + 1;
drh001bbcb2003-03-19 03:14:00 +0000228 sqliteVdbeAddOp(v, OP_Integer, pTab->isTemp, 0);
229 sqliteVdbeAddOp(v, OP_OpenWrite, base, pTab->tnum);
drh0353ced2001-03-20 22:05:00 +0000230 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
drh001bbcb2003-03-19 03:14:00 +0000231 sqliteVdbeAddOp(v, OP_Integer, pTab->isTemp, 0);
232 sqliteVdbeAddOp(v, OP_OpenWrite, pParse->nTab++, pIdx->tnum);
drh0353ced2001-03-20 22:05:00 +0000233 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000234
drhc977f7f2002-05-21 11:38:11 +0000235 /* This is the beginning of the delete loop when there are no
236 ** row triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000237 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000238 addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end);
danielk1977f29ce552002-05-19 23:43:12 +0000239 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000240
drhc977f7f2002-05-21 11:38:11 +0000241 /* Delete the row */
drh38640e12002-07-05 21:42:36 +0000242 sqliteGenerateRowDelete(db, v, pTab, base, pParse->trigStack==0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000243
drhc977f7f2002-05-21 11:38:11 +0000244 /* If there are row triggers, close all cursors then invoke
245 ** the AFTER triggers
246 */
danielk1977f29ce552002-05-19 23:43:12 +0000247 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000248 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
drh9adf9ac2002-05-15 11:44:13 +0000249 sqliteVdbeAddOp(v, OP_Close, base + i, pIdx->tnum);
danielk1977c3f9bad2002-05-15 08:30:12 +0000250 }
251 sqliteVdbeAddOp(v, OP_Close, base, 0);
252 sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_AFTER, pTab, -1,
danielk19776f349032002-06-11 02:25:40 +0000253 oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default,
254 addr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000255 }
256
drhc977f7f2002-05-21 11:38:11 +0000257 /* End of the delete loop */
drh99fcd712001-10-13 01:06:47 +0000258 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
259 sqliteVdbeResolveLabel(v, end);
drha8b38d22001-11-01 14:41:34 +0000260 sqliteVdbeAddOp(v, OP_ListReset, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000261
drhc977f7f2002-05-21 11:38:11 +0000262 /* Close the cursors after the loop if there are no row triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000263 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000264 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
drh9adf9ac2002-05-15 11:44:13 +0000265 sqliteVdbeAddOp(v, OP_Close, base + i, pIdx->tnum);
danielk1977c3f9bad2002-05-15 08:30:12 +0000266 }
267 sqliteVdbeAddOp(v, OP_Close, base, 0);
268 pParse->nTab = base;
269 }
drh0353ced2001-03-20 22:05:00 +0000270 }
drh1c928532002-01-31 15:54:21 +0000271 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +0000272
drh1bee3d72001-10-15 00:44:35 +0000273 /*
274 ** Return the number of rows that were deleted.
275 */
276 if( db->flags & SQLITE_CountRows ){
drh1bee3d72001-10-15 00:44:35 +0000277 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
278 sqliteVdbeChangeP3(v, -1, "rows deleted", P3_STATIC);
279 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
280 }
drhcce7d172000-05-31 15:34:51 +0000281
282delete_from_cleanup:
drhad3cab52002-05-24 02:04:32 +0000283 sqliteSrcListDelete(pTabList);
drhcce7d172000-05-31 15:34:51 +0000284 sqliteExprDelete(pWhere);
285 return;
286}
drh9cfcf5d2002-01-29 18:41:24 +0000287
288/*
289** This routine generates VDBE code that causes a single row of a
290** single table to be deleted.
291**
292** The VDBE must be in a particular state when this routine is called.
293** These are the requirements:
294**
295** 1. A read/write cursor pointing to pTab, the table containing the row
296** to be deleted, must be opened as cursor number "base".
297**
298** 2. Read/write cursors for all indices of pTab must be open as
299** cursor number base+i for the i-th index.
300**
301** 3. The record number of the row to be deleted must be on the top
302** of the stack.
303**
304** This routine pops the top of the stack to remove the record number
305** and then generates code to remove both the table record and all index
306** entries that point to that record.
307*/
308void sqliteGenerateRowDelete(
drh38640e12002-07-05 21:42:36 +0000309 sqlite *db, /* The database containing the index */
drh9cfcf5d2002-01-29 18:41:24 +0000310 Vdbe *v, /* Generate code into this VDBE */
311 Table *pTab, /* Table containing the row to be deleted */
drhc8d30ac2002-04-12 10:08:59 +0000312 int base, /* Cursor number for the table */
313 int count /* Increment the row change counter */
drh9cfcf5d2002-01-29 18:41:24 +0000314){
drh07d6e3a2002-05-23 12:50:18 +0000315 int addr;
316 addr = sqliteVdbeAddOp(v, OP_NotExists, base, 0);
drh38640e12002-07-05 21:42:36 +0000317 sqliteGenerateRowIndexDelete(db, v, pTab, base, 0);
drhc8d30ac2002-04-12 10:08:59 +0000318 sqliteVdbeAddOp(v, OP_Delete, base, count);
drh07d6e3a2002-05-23 12:50:18 +0000319 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drh0ca3e242002-01-29 23:07:02 +0000320}
321
322/*
323** This routine generates VDBE code that causes the deletion of all
324** index entries associated with a single row of a single table.
325**
326** The VDBE must be in a particular state when this routine is called.
327** These are the requirements:
328**
329** 1. A read/write cursor pointing to pTab, the table containing the row
330** to be deleted, must be opened as cursor number "base".
331**
332** 2. Read/write cursors for all indices of pTab must be open as
333** cursor number base+i for the i-th index.
334**
335** 3. The "base" cursor must be pointing to the row that is to be
336** deleted.
337*/
338void sqliteGenerateRowIndexDelete(
drh38640e12002-07-05 21:42:36 +0000339 sqlite *db, /* The database containing the index */
drh0ca3e242002-01-29 23:07:02 +0000340 Vdbe *v, /* Generate code into this VDBE */
341 Table *pTab, /* Table containing the row to be deleted */
342 int base, /* Cursor number for the table */
343 char *aIdxUsed /* Only delete if aIdxUsed!=0 && aIdxUsed[i]!=0 */
344){
drh9cfcf5d2002-01-29 18:41:24 +0000345 int i;
346 Index *pIdx;
347
drh0ca3e242002-01-29 23:07:02 +0000348 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
349 int j;
350 if( aIdxUsed!=0 && aIdxUsed[i-1]==0 ) continue;
351 sqliteVdbeAddOp(v, OP_Recno, base, 0);
352 for(j=0; j<pIdx->nColumn; j++){
353 int idx = pIdx->aiColumn[j];
354 if( idx==pTab->iPKey ){
355 sqliteVdbeAddOp(v, OP_Dup, j, 0);
356 }else{
357 sqliteVdbeAddOp(v, OP_Column, base, idx);
drh9cfcf5d2002-01-29 18:41:24 +0000358 }
drh9cfcf5d2002-01-29 18:41:24 +0000359 }
drh0ca3e242002-01-29 23:07:02 +0000360 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
drh491791a2002-07-18 00:34:09 +0000361 if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIdx);
drh0ca3e242002-01-29 23:07:02 +0000362 sqliteVdbeAddOp(v, OP_IdxDelete, base+i, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000363 }
drh9cfcf5d2002-01-29 18:41:24 +0000364}